forked from kandoo/beehive-netctrl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
105 lines (85 loc) · 2.4 KB
/
node.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package nom
import (
"encoding/json"
"fmt"
)
// NodeConnected is a message emitted when a node connects to a driver.
type NodeConnected struct {
Node Node
Driver Driver
}
// NodeDisconnected is a message emitted when a node disconnects from its
// driver.
type NodeDisconnected struct {
Node Node
Driver Driver
}
// NodeJoined is a message emitted when a node joins the network through the
// controller. It is always emitted after processing NodeConnected in the
// controller.
type NodeJoined Node
// NodeLeft is a message emitted when a node disconnects from its driver. It is
// always emitted after processing NodeDisconnected in the controller.
type NodeLeft Node
// NodeRoleChanged is a message emitted when a driver's role is changed for a
// node.
type DriverRoleChanged struct {
Node UID
Driver Driver
}
// Node represents a forwarding element, such as switches and routers.
type Node struct {
ID NodeID
Net UID
Capabilities []NodeCapability
MACAddr MACAddr
}
func (n Node) String() string {
return fmt.Sprintf("Node %s (mac=%v)", string(n.ID), n.MACAddr)
}
// NodeID is the ID of a node. This must be unique among all nodes in the
// network.
type NodeID string
// UID converts id into a UID.
func (id NodeID) UID() UID {
return UID(id)
}
// UID returns the node's unique ID. This id is in the form of net_id$$node_id.
func (n Node) UID() UID {
return UID(string(n.ID))
}
// ParseNodeUID parses a UID of a node and returns the respective node IDs.
func ParseNodeUID(id UID) NodeID {
s := UIDSplit(id)
return NodeID(s[0])
}
// GoDecode decodes the node from b using Gob.
func (n *Node) GoDecode(b []byte) error {
return ObjGoDecode(n, b)
}
// GoEncode encodes the node into a byte array using Gob.
func (n *Node) GoEncode() ([]byte, error) {
return ObjGoEncode(n)
}
// 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)
}
func (n Node) HasCapability(c NodeCapability) bool {
for _, nc := range n.Capabilities {
if c == nc {
return true
}
}
return false
}
// NodeCapability is a capability of a NOM node.
type NodeCapability uint32
// Valid values for NodeCapability.
const (
CapDriverRole NodeCapability = 1 << iota // Node can set the driver's role.
)