Skip to content

Commit

Permalink
First commit - Refactored from beehive.
Browse files Browse the repository at this point in the history
  • Loading branch information
soheilhy committed Sep 7, 2014
1 parent dc1fcc1 commit 8e98665
Show file tree
Hide file tree
Showing 19 changed files with 24,343 additions and 0 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Soheil Hassas Yeganeh <[email protected]>
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
BeeHive Network Controller
==========================
This is a distributed network controller built on top of BeeHive. It supports
OpenFlow but can be easily extended for other southband protocols.
14 changes: 14 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import (
"github.com/soheilhy/beehive-netctrl/openflow"
"github.com/soheilhy/beehive/bh"
)

func main() {
h := bh.NewHive()
openflow.StartOpenFlow(h)

ch := make(chan bool)
h.Start(ch)
}
7 changes: 7 additions & 0 deletions nom/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Package nom contains the Network Object Model: an abstraction for computer
// networks. In essence it is a graph of networking nodes similar to what is
// proposed in ONIX.
//
// NOM models a network as a graph of nodes. Each node has a set of ports with
// outgoing links that are connected to other ports.
package nom
21 changes: 21 additions & 0 deletions nom/id.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package nom

import "strings"

// UID is a unique ID of a NOM object. Unlike UUID/GUID, this ID contains
// redundant information about the object. For example a port's UID contains its
// network and node ID along with an ID for the port.
type UID string

// UIDSeparator is the token added in between the parts of a UID.
const UIDSeparator = "$$"

// UIDJoin joins an array of IDs into a UID.
func UIDJoin(ids ...string) UID {
return UID(strings.Join(ids, UIDSeparator))
}

// UIDSplit splits a UID into its smaller IDs.
func UIDSplit(id UID) []string {
return strings.Split(string(id), UIDSeparator)
}
58 changes: 58 additions & 0 deletions nom/link.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package nom

import (
"bytes"
"encoding/gob"
"encoding/json"
)

// Link represents an outgoing link from a port.
type Link struct {
ID LinkID // Link's ID.
From UID // From is the link's port.
To []UID // To stores the port(s) connected to From using this link.
}

// LinkID is a link's ID which is unique among the outgoing links of a port.
type LinkID string

// UID returns the UID of the link in the form of
// net_id$$node_id$$port_id$$link_id.
func (l Link) UID() UID {
return UIDJoin(string(l.From), string(l.ID))
}

// ParseLinkUID parses a link UID into the respetive network, node, port, and
// link ids.
func ParseLinkUID(id UID) (NetworkID, NodeID, PortID, LinkID) {
s := UIDSplit(id)
return NetworkID(s[0]), NodeID(s[1]), PortID(s[2]), LinkID(s[3])
}

// GOBDecode decodes the link from b using GOB.
func (l *Link) GOBDecode(b []byte) error {
buf := bytes.NewBuffer(b)
dec := gob.NewDecoder(buf)
return dec.Decode(l)
}

// GOBEncode encodes the node into a byte array using GOB.
func (l *Link) GOBEncode() ([]byte, error) {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err := enc.Encode(l)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}

// JSONDecode decodes the node from a byte array using JSON.
func (l *Link) JSONDecode(b []byte) error {
return json.Unmarshal(b, l)
}

// JSONEncode encodes the node into a byte array using JSON.
func (l *Link) JSONEncode() ([]byte, error) {
return json.Marshal(l)
}
55 changes: 55 additions & 0 deletions nom/net.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package nom

import (
"bytes"
"encoding/gob"
"encoding/json"
)

// Network represents a virtual or physical network in NOM.
type Network struct {
ID NetworkID // The id of the network.
Desc string // A human-readable description of the network.
}

// NetworkID is the ID of the network.
type NetworkID string

// UID returns the UID of the network.
func (n Network) UID() UID {
return UID(n.ID)
}

// ParseNetworkUID parses a network UID into the network ID. Note that for
// network these IDs of the same.
func ParseNetworkUID(id UID) NetworkID {
return NetworkID(id)
}

// GOBDecode decodes the network from b using GOB.
func (n *Network) GOBDecode(b []byte) error {
buf := bytes.NewBuffer(b)
dec := gob.NewDecoder(buf)
return dec.Decode(n)
}

// GOBEncode encodes the network into a byte array using GOB.
func (n *Network) GOBEncode() ([]byte, error) {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err := enc.Encode(n)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}

// JSONDecode decodes the network from a byte array using JSON.
func (n *Network) JSONDecode(b []byte) error {
return json.Unmarshal(b, n)
}

// JSONEncode encodes the network into a byte array using JSON.
func (n *Network) JSONEncode() ([]byte, error) {
return json.Marshal(n)
}
61 changes: 61 additions & 0 deletions nom/node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package nom

import (
"bytes"
"encoding/gob"
"encoding/json"

"github.com/soheilhy/beehive/bh"
)

// Node represents a forwarding element, such as switches and routers.
type Node struct {
ID NodeID
Net UID
Ports map[PortID]Port
Driver bh.BeeId
}

// NodeID is the ID of a node. This must be unique among all nodes in the
// network.
type NodeID string

// UID returns the node's unique ID. This id is in the form of net_id$$node_id.
func (n Node) UID() UID {
return UIDJoin(string(n.Net), string(n.ID))
}

// ParseNodeUID parses a UID of a node and returns the respective network and
// node IDs.
func ParseNodeUID(id UID) (NetworkID, NodeID) {
s := UIDSplit(id)
return NetworkID(s[0]), NodeID(s[1])
}

// GOBDecode decodes the node from b using GOB.
func (n *Node) GOBDecode(b []byte) error {
buf := bytes.NewBuffer(b)
dec := gob.NewDecoder(buf)
return dec.Decode(n)
}

// GOBEncode encodes the node into a byte array using GOB.
func (n *Node) GOBEncode() ([]byte, error) {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err := enc.Encode(n)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}

// JSONDecode decodes the node from a byte array using JSON.
func (n *Node) JSONDecode(b []byte) error {
return json.Unmarshal(b, n)
}

// JSONEncode encodes the node into a byte array using JSON.
func (n *Node) JSONEncode() ([]byte, error) {
return json.Marshal(n)
}
16 changes: 16 additions & 0 deletions nom/object.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package nom

// Object is the interface of all structs in the network object model.
type Object interface {
// GOBDecode decodes the object from a byte array using the GOB encoding.
GOBDecode(b []byte) error
// GOBEncode encodes the object into a byte array using the GOB encoding.
GOBEncode() ([]byte, error)
// JSONDecode decodes the object from a byte array using the JSON encoding.
JSONDecode(b []byte) error
// JSONEncode encodes the object into a byte array using the JSON encoding.
JSONEncode() ([]byte, error)
// UID returns a unique ID of this object. This ID is unique in the network
// among all other objects.
UID() UID
}
70 changes: 70 additions & 0 deletions nom/port.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package nom

import (
"bytes"
"encoding/gob"
"encoding/json"
"fmt"
)

// Port is either a physical or a virtual port of a node.
type Port struct {
ID PortID
Node UID
Links []UID
}

// PortID is the ID of a port and is unique among the ports of a node.
type PortID string

// UID returns the unique ID of the port in the form of
// net_id$$node_id$$port_id.
func (p Port) UID() UID {
return UIDJoin(string(p.Node), string(p.ID))
}

// ParsePortUID parses a UID of a port and returns the respective network, node,
// and port IDs.
func ParsePortUID(id UID) (NetworkID, NodeID, PortID) {
s := UIDSplit(id)
return NetworkID(s[0]), NodeID(s[1]), PortID(s[2])
}

// AddLink adds an outgoing link to the port. Return an error if the link does
// not belong to this port.
func (p *Port) AddLink(l Link) error {
if l.From != p.UID() {
return fmt.Errorf("Link is outgoing from %s not from %s", l.From, p.UID())
}

p.Links = append(p.Links, l.UID())
return nil
}

// GOBDecode decodes the port from b using GOB.
func (p *Port) GOBDecode(b []byte) error {
buf := bytes.NewBuffer(b)
dec := gob.NewDecoder(buf)
return dec.Decode(p)
}

// GOBEncode encodes the port into a byte array using GOB.
func (p *Port) GOBEncode() ([]byte, error) {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err := enc.Encode(p)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}

// JSONDecode decodes the port from a byte array using JSON.
func (p *Port) JSONDecode(b []byte) error {
return json.Unmarshal(b, p)
}

// JSONEncode encodes the port into a byte array using JSON.
func (p *Port) JSONEncode() ([]byte, error) {
return json.Marshal(p)
}
Loading

0 comments on commit 8e98665

Please sign in to comment.