-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTopologyManager.js
executable file
·396 lines (370 loc) · 11.4 KB
/
TopologyManager.js
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
const EventEmitter = require('events');
const DeviceInstanceManager = require('./DeviceInstanceManager');
const DB = require('./Database');
const Debounce = require('./utils/Debounce');
const TopologyAnalyzer = require('./TopologyAnalyzer');
const Log = require('debug')('topology');
class TopologyManager extends EventEmitter {
constructor() {
super();
this._entry = null;
this._topology = [];
this._order = [];
this.valid = false;
this.running = false;
const debounced = Debounce(() => this._buildLinkLags());
DeviceInstanceManager.on('update', evt => {
if (!evt.key || evt.key.startsWith('network.lags.')) {
debounced();
}
});
}
getTopology() {
return this._topology;
}
getAttachmentPoint() {
return this._entry;
}
getCaptureDevices() {
if (!this._entry) {
return [];
}
// Get a list of all devices capable of capture
const devices = DeviceInstanceManager.getAllDevices();
const caps = {};
devices.forEach(device => {
if (device.readKV('network.mirror.0', { depth: 1 })) {
caps[device._id] = device;
}
});
// Make sure there's a capturable path from the attachment point to each device.
for (let id in caps) {
const path = this.findPath(this._entry.device, caps[id]);
if (!path) {
delete caps[id];
}
else {
for (let i = 0; i < path.length; i++) {
const link = path[i];
if (!caps[link[0].device._id] || !caps[link[1].device._id]) {
delete caps[id];
break;
}
}
}
}
// Return only devices we can capture from.
return Object.values(caps);
}
// Find the path (a set of device/port to device/port links) between two devices.
findPath(fromDevice, toDevice) {
// Handle the empty path first.
if (fromDevice === toDevice) {
return [];
}
const walk = (from, to, links) => {
for (let i = 0; i < links.length; i++) {
const link = links[i];
if (link[0].device === from) {
if (link[1].device === to) {
return [ [ link[0], link[1] ] ];
}
else {
const nlinks = [].concat(links);
nlinks.splice(i, 1);
const r = walk(link[1].device, to, nlinks);
if (r) {
r.unshift([ link[0], link[1] ] );
return r;
}
}
}
else if (link[1].device === from) {
if (link[0].device === to) {
return [ [ link[1], link[0] ] ];
}
else {
const nlinks = [].concat(links);
nlinks.splice(i, 1);
const r = walk(link[0].device, to, nlinks);
if (r) {
r.unshift([ link[1], link[0] ]);
return r;
}
}
}
}
return null;
}
return walk(fromDevice, toDevice, this._topology);
}
// Find the link from the device/port
findLink(device, portnr) {
for (let i = 0; i < this._topology.length; i++) {
const link = this._topology[i];
if (link[0].device === device && link[0].lag.ports.indexOf(portnr) !== -1) {
// Return the link with the device:port first
return link;
}
if (link[1].device === device && link[1].lag.ports.indexOf(portnr) !== -1) {
return [ link[1], link[0] ];
}
}
return null;
}
// Find the link associated with the device/lag
findLinkLag(device, lagnr) {
for (let i = 0; i < this._topology.length; i++) {
const link = this._topology[i];
if (link[0].device === device && link[0].lag.group === lagnr) {
return link;
}
if (link[1].device === device && link[1].lag.group === lagnr) {
return [ link[1], link[0] ];
}
}
return null;
}
clear() {
this._topology = [];
this._entry = null;
this.valid = false;
}
setLinkLag(link, type) {
if (link[0].lag.type != type) {
for (let i = 0; i < link.length; i++) {
link[i].lag.type = type;
let group = 0;
if (type !== 'none') {
group = link[i].device.readKV(`network.lags.port.${link[i].port}.group`);
if (group === 0) {
// Pick a new group.
const lags = link[i].device.readKV(`network.lags`);
const nr = lags.types[type] || 0;
const available = {};
for (let i = 1; i <= nr; i++) {
available[i] = true;
}
for (let p in lags.port) {
delete available[lags.port[p].group];
}
group = Object.keys(available)[0];
if (group === undefined) {
// No groups available
throw new Error('no groups');
}
}
}
link[i].lag.ports.forEach(p => {
link[i].device.writeKV(`network.lags.port.${p}.type`, type);
link[i].device.writeKV(`network.lags.port.${p}.group`, group);
});
}
}
}
addLinkDevicePort(link, device, portnr) {
for (let i = 0; i < link.length; i++) {
if (link[i].device === device && link[i].lag.ports.indexOf(portnr) === -1) {
const lag = link[i].device.readKV(`network.lags.port.${link[i].port}`);
if (!link[i].device.writeKV(`network.lags.port.${portnr}.type`, lag.type)) {
return false;
}
link[i].device.writeKV(`network.lags.port.${portnr}.group`, lag.group);
return true;
}
}
return false;
}
removeLinkDevicePort(link, device, portnr) {
for (let i = 0; i < link.length; i++) {
if (link[i].device === device) {
const idx = link[i].lag.ports.indexOf(portnr);
if (idx !== -1) {
link[i].device.writeKV(`network.lags.port.${portnr}.type`, 'none');
link[i].device.writeKV(`network.lags.port.${portnr}.group`, 0);
return true;
}
}
}
return false;
}
//
// Read the LAG information for each link and build (or rebuild) the lag state.
//
_buildLinkLags() {
// Build a mapping for each device from port to lag group.
const dev2portmap = this._buildDevicesPortmap(DeviceInstanceManager.getAuthenticatedDevices());
// Walk each link and add the appropriate lag information.
let update = false;
this._topology.forEach(link => {
link.forEach(point => {
const oldLag = JSON.stringify(point.lag);
const portmap = dev2portmap[point.device._id];
if (portmap) {
const map = portmap[point.port];
if (map) {
point.lag = { type: map.type, ports: map.ports, group: map.group };
}
else {
point.lag = { type: 'none', ports: [ point.port ], group: 0 };
}
}
else {
point.lag = { type: 'none', ports: [ point.port ], group: 0 };
}
if (JSON.stringify(point.lag) !== oldLag) {
update = true;
}
});
});
if (update) {
this.emit('update');
}
}
//
// Build an order for each node in the topology, with the entry node being the 0th.
//
_buildOrder() {
const devices = DeviceInstanceManager.getAuthenticatedDevices();
if (this._entry) {
const order = [];
devices.forEach(device => {
const path = this.findPath(this._entry.device, device);
order.push({ order: path ? path.length : Number.MAX_SAFE_INTEGER, device: device });
});
order.sort((a, b) => a.order - b.order);
this._order = order.map(value => value.device);
}
else {
this._order = devices;
}
}
order(devices, direction) {
const ndevices = this._order.filter(dev => devices.indexOf(dev) !== -1);
switch (direction) {
case 'far-to-near':
ndevices.reverse();
break;
case 'near-to-far':
default:
break;
}
return ndevices;
}
//
// Build a device-to-portmap for all lags
//
_buildDevicesPortmap(devices) {
const dev2portmap = {};
devices.forEach(device => {
const lags = device.readKV('network.lags');
if (lags) {
const portmap = {};
const ports = lags.port;
const groups = {};
for (let p in ports) {
if (ports[p].group) {
const portnr = parseInt(p);
const group = ports[p].group;
if (group in groups) {
groups[group].ports.push(portnr);
}
else {
groups[group] = {
type: ports[p].type,
port: portnr,
ports: [ portnr ],
group: group
};
}
portmap[portnr] = groups[group];
}
}
dev2portmap[device._id] = portmap;
}
});
//Log('portmap:', JSON.stringify(dev2portmap, null, 1));
return dev2portmap;
}
cancel() {
this.running = false;
if (this.analyser) {
this.analyser.stop();
this.analyser.removeAllListeners();
this.analyser = null;
}
}
// Discover the topology of the network. This is how the switches
// interconnect, but does not include any clients.
async discoverNetworkTopology() {
this.clear();
this.analyser = new TopologyAnalyzer();
this.analyser.on('status', event => this.emit('status', event));
this.running = true;
const results = await this.analyser.analyze();
this.running = false;
this.valid = results.success;
if (!this.valid) {
this.emit('status', { op: 'complete', success: false, reason: results.reason });
}
else {
this._entry = results.entry;
this._topology = results.topology;
this._buildLinkLags();
this._buildOrder();
DB.updateTopology(this.toDB());
this.emit('status', { op: 'complete', success: true, topology: this._topology });
this.emit('update');
}
}
toDB() {
return {
_id: 'topology',
valid: this.valid,
entry: this._entry ? { deviceId: this._entry.device._id, port: this._entry.port } : null,
topology: this._topology.map(link => [
{ deviceId: link[0].device._id, port: link[0].port } , { deviceId: link[1].device._id, port: link[1].port }
])
};
}
fromDB(dbTopology) {
this.valid = false;
if (dbTopology) {
try {
this._entry = { device: DeviceInstanceManager.getDeviceById(dbTopology.entry.deviceId), port: dbTopology.entry.port },
this._topology = dbTopology.topology.map(link => {
const d0 = DeviceInstanceManager.getDeviceById(link[0].deviceId);
const d1 = DeviceInstanceManager.getDeviceById(link[1].deviceId);
if (!d0 || !d1) {
throw Error();
}
return [{ device: d0, port: link[0].port }, { device: d1, port: link[1].port }];
});
this.valid = dbTopology.valid;
}
catch (_) {
// Failed to rebuild topology - device deleted?
this._entry = null;
this._topology = [];
this.valid = false;
}
}
}
async start() {
this.fromDB(await DB.getTopology());
this._buildLinkLags();
this._buildOrder();
this._invalid = () => {
this.valid = false;
DB.updateTopology(this.toDB());
this.emit('update');
}
DeviceInstanceManager.on('add', this._invalid);
DeviceInstanceManager.on('remove', this._invalid);
}
stop() {
DeviceInstanceManager.off('add', this._invalid);
DeviceInstanceManager.off('remove', this._invalid);
}
}
module.exports = new TopologyManager();