forked from sandia-minimega/discovery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.go
315 lines (272 loc) · 6.17 KB
/
graph.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// Copyright 2018 National Technology & Engineering Solutions of Sandia, LLC
// (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S.
// Government retains certain rights in this software.
package minigraph
import (
"encoding/gob"
"fmt"
"io"
"sort"
"strings"
"sync"
log "pkg/minilog"
)
const (
TYPE_NODE = iota
TYPE_ENDPOINT
TYPE_NETWORK
)
const (
UNCONNECTED = -1
)
type Graph struct {
lock sync.Mutex
Nodes map[int]Node
maxID int
}
type Node interface {
ID() int
Type() int
Connected(int) bool
Neighbors() []int
Match(string, string) bool
setID(int)
Data() map[string]string
}
type networks []*Network
func (n networks) Len() int { return len(n) }
func (n networks) Swap(i, j int) { n[i], n[j] = n[j], n[i] }
func (n networks) Less(i, j int) bool { return n[i].ID() < n[j].ID() }
type endpoints []*Endpoint
func (n endpoints) Len() int { return len(n) }
func (n endpoints) Swap(i, j int) { n[i], n[j] = n[j], n[i] }
func (n endpoints) Less(i, j int) bool { return n[i].ID() < n[j].ID() }
func init() {
gob.Register(&Endpoint{})
gob.Register(&Network{})
gob.Register(Edge{})
}
func New() *Graph {
return &Graph{
Nodes: make(map[int]Node),
}
}
// read graph contents from an io.Reader. If an io.EOF occurs without reading
// any data, assume an empty reader and return a new graph.
func Read(f io.Reader) (*Graph, error) {
g := New()
g.lock.Lock()
defer g.lock.Unlock()
dec := gob.NewDecoder(f)
err := dec.Decode(g)
if err != nil && err != io.EOF {
return nil, err
}
return g, nil
}
func (g *Graph) Write(f io.Writer) error {
g.lock.Lock()
defer g.lock.Unlock()
enc := gob.NewEncoder(f)
err := enc.Encode(g)
if err != nil {
return err
}
return nil
}
func (g *Graph) NewEndpoint() *Endpoint {
n := &Endpoint{
NID: g.newID(),
D: make(map[string]string),
}
g.Nodes[n.ID()] = n
return n
}
func (g *Graph) NewNetwork() *Network {
n := &Network{
NID: g.newID(),
D: make(map[string]string),
}
g.Nodes[n.ID()] = n
return n
}
func (g *Graph) newID() int {
g.lock.Lock()
defer g.lock.Unlock()
if g.maxID == 0 {
for k, _ := range g.Nodes {
if g.maxID < k {
g.maxID = k
}
}
}
g.maxID++
log.Debug("new id: %v", g.maxID)
return g.maxID
}
func (g *Graph) Insert(n Node) (Node, error) {
// add an ID if the current ID is 0
if n.ID() == 0 {
n.setID(g.newID())
}
if _, ok := g.Nodes[n.ID()]; !ok {
g.Nodes[n.ID()] = n
return n, nil
}
return n, fmt.Errorf("node %v already exists", n)
}
// Delete a node from the graph and remove all references to it in other nodes.
func (g *Graph) Delete(n Node) error {
if _, ok := g.Nodes[n.ID()]; ok {
for _, v := range n.Neighbors() {
err := g.Disconnect(n, g.Nodes[v])
if err != nil {
return err
}
}
delete(g.Nodes, n.ID())
return nil
}
return fmt.Errorf("no such node %v", n)
}
func (g *Graph) GetNodes() []Node {
var ret []Node
for _, v := range g.Nodes {
ret = append(ret, v)
}
return ret
}
func (g *Graph) GetEndpoints() []*Endpoint {
var ret []*Endpoint
for _, v := range g.Nodes {
switch v.(type) {
case *Endpoint:
ret = append(ret, v.(*Endpoint))
}
}
return ret
}
func (g *Graph) GetNetworks() []*Network {
var ret []*Network
for _, v := range g.Nodes {
switch v.(type) {
case *Network:
ret = append(ret, v.(*Network))
}
}
sort.Sort(networks(ret))
return ret
}
// return a list of nodes that contain the string v in the key k
func (g *Graph) FindNodes(k string, v string) []Node {
k = strings.ToLower(k)
var ret []Node
for _, n := range g.Nodes {
if n.Match(k, v) {
ret = append(ret, n)
}
}
return ret
}
func (g *Graph) FindEndpoints(k string, v string) []*Endpoint {
k = strings.ToLower(k)
var ret []*Endpoint
for _, n := range g.GetEndpoints() {
if n.Match(k, v) {
ret = append(ret, n)
}
}
sort.Sort(endpoints(ret))
return ret
}
func (g *Graph) FindNetworks(k string, v string) []*Network {
k = strings.ToLower(k)
var ret []*Network
for _, n := range g.GetNetworks() {
if n.Match(k, v) {
ret = append(ret, n)
}
}
return ret
}
// HasNode returns true if a given node is in the graph
func (g *Graph) HasNode(n Node) bool {
_, ok := g.Nodes[n.ID()]
return ok
}
func (g *Graph) Update(n Node) (Node, error) {
if !g.HasNode(n) {
return nil, fmt.Errorf("no such node %v", n)
}
g.Nodes[n.ID()] = n
return n, nil
}
// connect an endpoint to a network on a given edge. A node may only have a
// single connection to a given network. The edge must belong to e.
func (g *Graph) Connect(e, n Node, edge *Edge) error {
if !g.HasNode(e) {
return fmt.Errorf("node %v not in graph", e)
}
if !g.HasNode(n) {
return fmt.Errorf("node %v not in graph", n)
}
if e.Type() != TYPE_ENDPOINT {
return fmt.Errorf("node %v not an endpoint", e)
}
if n.Type() != TYPE_NETWORK {
return fmt.Errorf("node %v not a network", n)
}
var endpoint *Endpoint
var network *Network
endpoint = e.(*Endpoint)
network = n.(*Network)
if !endpoint.HasEdge(edge) {
return fmt.Errorf("edge %v not in endpoint %v", edge, endpoint)
}
if endpoint.Connected(network.ID()) {
return fmt.Errorf("endpoint %v already connected to net %v", endpoint, network)
}
edge.N = network.ID()
network.Endpoints = append(network.Endpoints, endpoint.ID())
return nil
}
// disconnect a node from a network.
func (g *Graph) Disconnect(n1, n2 Node) error {
if !g.HasNode(n1) {
return fmt.Errorf("node %v not in graph", n1)
}
if !g.HasNode(n2) {
return fmt.Errorf("node %v not in graph", n2)
}
if !n1.Connected(n2.ID()) {
return fmt.Errorf("node %v not connected to node %v", n1, n2)
}
var endpoint *Endpoint
var network *Network
if n1.Type() == TYPE_ENDPOINT {
endpoint = n1.(*Endpoint)
} else {
network = n1.(*Network)
}
if n2.Type() == TYPE_ENDPOINT {
endpoint = n2.(*Endpoint)
} else {
network = n2.(*Network)
}
if endpoint == nil || network == nil {
return fmt.Errorf("node type mismatch: %v, %v", n1.Type(), n2.Type())
}
for _, v := range endpoint.Edges {
if v.N == network.ID() {
v.N = UNCONNECTED
break
}
}
for i, v := range network.Endpoints {
if v == endpoint.ID() {
network.Endpoints = append(network.Endpoints[:i], network.Endpoints[i+1:]...)
break
}
}
return nil
}