blob: a629dfc8868b4fd2b295bdfb9b3a94424dea9f3e [file] [log] [blame]
Simon Hunt737c89f2015-01-28 12:23:19 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Simon Hunt737c89f2015-01-28 12:23:19 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
Simon Hunt3a6eec02015-02-09 21:16:43 -080018 ONOS GUI -- Topology Force Module.
19 Visualization of the topology in an SVG layer, using a D3 Force Layout.
Simon Hunt737c89f2015-01-28 12:23:19 -080020 */
21
22(function () {
23 'use strict';
24
25 // injected refs
Simon Hunt8d22c4b2015-08-06 16:24:43 -070026 var $log, $timeout, fs, sus, ts, flash, wss, tov,
Andrea Campanella732ea832017-02-06 09:25:59 -080027 tis, tms, td3, tss, tts, tos, fltr, tls, uplink, svg, tpis;
Simon Huntac4c6f72015-02-03 19:50:53 -080028
Simon Hunte2d9dc72017-08-10 15:21:04 -070029 // function to be replaced by the localization bundle function
30 var topoLion = function (x) {
31 return '#tfs#' + x + '#';
32 };
33
Simon Huntac4c6f72015-02-03 19:50:53 -080034 // configuration
Simon Hunt1894d792015-02-04 17:09:20 -080035 var linkConfig = {
36 light: {
Simon Huntf44d7262016-06-14 14:46:56 -070037 baseColor: '#939598',
Simon Hunt1894d792015-02-04 17:09:20 -080038 inColor: '#66f',
Steven Burrows1c2a9682017-07-14 16:52:46 +010039 outColor: '#f00',
Simon Hunt1894d792015-02-04 17:09:20 -080040 },
41 dark: {
Simon Huntf44d7262016-06-14 14:46:56 -070042 // TODO : theme
43 baseColor: '#939598',
Simon Hunt1894d792015-02-04 17:09:20 -080044 inColor: '#66f',
Steven Burrows1c2a9682017-07-14 16:52:46 +010045 outColor: '#f00',
Simon Hunt1894d792015-02-04 17:09:20 -080046 },
47 inWidth: 12,
Steven Burrows1c2a9682017-07-14 16:52:46 +010048 outWidth: 10,
Simon Hunt1894d792015-02-04 17:09:20 -080049 };
50
Simon Hunt737c89f2015-01-28 12:23:19 -080051 // internal state
Steven Burrows1c2a9682017-07-14 16:52:46 +010052 var settings, // merged default settings and options
53 force, // force layout object
54 drag, // drag behavior handler
Simon Hunt737c89f2015-01-28 12:23:19 -080055 network = {
56 nodes: [],
57 links: [],
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -070058 linksByDevice: {},
Simon Hunt737c89f2015-01-28 12:23:19 -080059 lookup: {},
Steven Burrows1c2a9682017-07-14 16:52:46 +010060 revLinkToKey: {},
Simon Huntac4c6f72015-02-03 19:50:53 -080061 },
Steven Burrows1c2a9682017-07-14 16:52:46 +010062 lu, // shorthand for lookup
63 rlk, // shorthand for revLinktoKey
64 showHosts = false, // whether hosts are displayed
65 showOffline = true, // whether offline devices are displayed
66 nodeLock = false, // whether nodes can be dragged or not (locked)
67 fTimer, // timer for delayed force layout
68 fNodesTimer, // timer for delayed nodes update
69 fLinksTimer, // timer for delayed links update
70 dim, // the dimensions of the force layout [w,h]
71 linkNums = [], // array of link number labels
72 devIconDim = 36, // node target dimension
73 devIconDimMin = 20, // node minimum dimension when zoomed out
74 devIconDimMax = 40, // node maximum dimension when zoomed in
Steven Burrowsf17f0ab2017-04-11 11:03:58 -070075 portLabelDim = 30;
Simon Hunt737c89f2015-01-28 12:23:19 -080076
77 // SVG elements;
Bri Prebilic Cole80401762015-07-16 11:36:18 -070078 var linkG, linkLabelG, numLinkLblsG, portLabelG, nodeG;
Simon Hunt737c89f2015-01-28 12:23:19 -080079
80 // D3 selections;
81 var link, linkLabel, node;
82
83 // default settings for force layout
84 var defaultSettings = {
85 gravity: 0.4,
86 friction: 0.7,
87 charge: {
88 // note: key is node.class
89 device: -8000,
90 host: -5000,
Steven Burrows1c2a9682017-07-14 16:52:46 +010091 _def_: -12000,
Simon Hunt737c89f2015-01-28 12:23:19 -080092 },
93 linkDistance: {
94 // note: key is link.type
95 direct: 100,
96 optical: 120,
97 hostLink: 3,
Steven Burrows1c2a9682017-07-14 16:52:46 +010098 _def_: 50,
Simon Hunt737c89f2015-01-28 12:23:19 -080099 },
100 linkStrength: {
101 // note: key is link.type
102 // range: {0.0 ... 1.0}
Steven Burrows1c2a9682017-07-14 16:52:46 +0100103 // direct: 1.0,
104 // optical: 1.0,
105 // hostLink: 1.0,
106 _def_: 1.0,
107 },
Simon Hunt737c89f2015-01-28 12:23:19 -0800108 };
109
110
Simon Huntac4c6f72015-02-03 19:50:53 -0800111 // ==========================
112 // === EVENT HANDLERS
113
114 function addDevice(data) {
115 var id = data.id,
116 d;
117
Simon Hunt1894d792015-02-04 17:09:20 -0800118 uplink.showNoDevs(false);
Simon Huntac4c6f72015-02-03 19:50:53 -0800119
120 // although this is an add device event, if we already have the
121 // device, treat it as an update instead..
Simon Hunt1894d792015-02-04 17:09:20 -0800122 if (lu[id]) {
Simon Huntac4c6f72015-02-03 19:50:53 -0800123 updateDevice(data);
124 return;
125 }
126
Simon Hunt3a6eec02015-02-09 21:16:43 -0800127 d = tms.createDeviceNode(data);
Simon Huntac4c6f72015-02-03 19:50:53 -0800128 network.nodes.push(d);
Simon Hunt1894d792015-02-04 17:09:20 -0800129 lu[id] = d;
Simon Huntac4c6f72015-02-03 19:50:53 -0800130 updateNodes();
Simon Hunta17fa672015-08-19 18:42:22 -0700131 fStart();
Simon Huntac4c6f72015-02-03 19:50:53 -0800132 }
133
134 function updateDevice(data) {
135 var id = data.id,
Simon Hunt1894d792015-02-04 17:09:20 -0800136 d = lu[id],
Simon Huntac4c6f72015-02-03 19:50:53 -0800137 wasOnline;
138
139 if (d) {
140 wasOnline = d.online;
141 angular.extend(d, data);
Simon Hunt3a6eec02015-02-09 21:16:43 -0800142 if (tms.positionNode(d, true)) {
Simon Hunt445e8152015-02-06 13:00:12 -0800143 sendUpdateMeta(d);
Simon Huntac4c6f72015-02-03 19:50:53 -0800144 }
145 updateNodes();
146 if (wasOnline !== d.online) {
Simon Huntdc6adea2015-02-09 22:29:36 -0800147 tms.findAttachedLinks(d.id).forEach(restyleLinkElement);
Simon Hunt5724fb42015-02-05 16:59:40 -0800148 updateOfflineVisibility(d);
Simon Huntac4c6f72015-02-03 19:50:53 -0800149 }
Simon Huntac4c6f72015-02-03 19:50:53 -0800150 }
151 }
152
Simon Hunt1894d792015-02-04 17:09:20 -0800153 function removeDevice(data) {
154 var id = data.id,
155 d = lu[id];
156 if (d) {
157 removeDeviceElement(d);
Simon Hunt1894d792015-02-04 17:09:20 -0800158 }
159 }
160
161 function addHost(data) {
162 var id = data.id,
Simon Hunt12c79ed2017-09-12 11:58:44 -0700163 d;
Simon Hunt1894d792015-02-04 17:09:20 -0800164
165 // although this is an add host event, if we already have the
166 // host, treat it as an update instead..
167 if (lu[id]) {
168 updateHost(data);
169 return;
170 }
171
Simon Hunt3a6eec02015-02-09 21:16:43 -0800172 d = tms.createHostNode(data);
Simon Hunt1894d792015-02-04 17:09:20 -0800173 network.nodes.push(d);
174 lu[id] = d;
Simon Hunt1894d792015-02-04 17:09:20 -0800175 updateNodes();
176
Simon Hunt12c79ed2017-09-12 11:58:44 -0700177 // need to handle possible multiple links (multi-homed host)
Simon Hunt7df764f2017-09-14 21:26:14 -0700178 createHostLinks(data.allCps, d);
Simon Hunt12c79ed2017-09-12 11:58:44 -0700179
180 if (d.links.length) {
Simon Hunt1894d792015-02-04 17:09:20 -0800181 updateLinks();
182 }
Simon Hunta17fa672015-08-19 18:42:22 -0700183 fStart();
Simon Hunt1894d792015-02-04 17:09:20 -0800184 }
185
186 function updateHost(data) {
187 var id = data.id,
188 d = lu[id];
189 if (d) {
190 angular.extend(d, data);
Simon Hunt3a6eec02015-02-09 21:16:43 -0800191 if (tms.positionNode(d, true)) {
Simon Hunt445e8152015-02-06 13:00:12 -0800192 sendUpdateMeta(d);
Simon Hunt1894d792015-02-04 17:09:20 -0800193 }
194 updateNodes();
Simon Hunt1894d792015-02-04 17:09:20 -0800195 }
196 }
197
Simon Hunt7df764f2017-09-14 21:26:14 -0700198 function createHostLinks(cps, model) {
199 model.links = [];
200 cps.forEach(function (cp) {
201 var linkData = {
202 key: model.id + '/0-' + cp.device + '/' + cp.port,
203 dst: cp.device,
204 dstPort: cp.port,
205 };
206 model.links.push(linkData);
207
208 var lnk = tms.createHostLink(model.id, cp.device, cp.port);
209 if (lnk) {
210 network.links.push(lnk);
211 lu[linkData.key] = lnk;
212 }
213 });
214 }
215
Simon Hunt95d56fd2015-11-12 11:06:44 -0800216 function moveHost(data) {
217 var id = data.id,
Simon Hunt7df764f2017-09-14 21:26:14 -0700218 d = lu[id];
Simon Hunt12c79ed2017-09-12 11:58:44 -0700219
Simon Hunt95d56fd2015-11-12 11:06:44 -0800220 if (d) {
Simon Hunt7df764f2017-09-14 21:26:14 -0700221 removeAllLinkElements(d.links);
Simon Hunt95d56fd2015-11-12 11:06:44 -0800222
223 // merge new data
224 angular.extend(d, data);
225 if (tms.positionNode(d, true)) {
226 sendUpdateMeta(d);
227 }
228
Simon Hunt7df764f2017-09-14 21:26:14 -0700229 // now create new host link(s)
230 createHostLinks(data.allCps, d);
Simon Hunt95d56fd2015-11-12 11:06:44 -0800231
232 updateNodes();
233 updateLinks();
234 fResume();
235 }
236 }
237
Simon Hunt1894d792015-02-04 17:09:20 -0800238 function removeHost(data) {
239 var id = data.id,
240 d = lu[id];
241 if (d) {
242 removeHostElement(d, true);
Simon Hunt1894d792015-02-04 17:09:20 -0800243 }
244 }
245
246 function addLink(data) {
Simon Huntdc6adea2015-02-09 22:29:36 -0800247 var result = tms.findLink(data, 'add'),
Simon Hunt1894d792015-02-04 17:09:20 -0800248 bad = result.badLogic,
249 d = result.ldata;
250
251 if (bad) {
Simon Hunteb18f522016-01-28 19:22:23 -0800252 $log.debug(bad + ': ' + link.id);
Simon Hunt1894d792015-02-04 17:09:20 -0800253 return;
254 }
255
256 if (d) {
257 // we already have a backing store link for src/dst nodes
258 addLinkUpdate(d, data);
259 return;
260 }
261
262 // no backing store link yet
Simon Hunt3a6eec02015-02-09 21:16:43 -0800263 d = tms.createLink(data);
Simon Hunt1894d792015-02-04 17:09:20 -0800264 if (d) {
265 network.links.push(d);
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700266 aggregateLink(d, data);
Simon Hunt1894d792015-02-04 17:09:20 -0800267 lu[d.key] = d;
268 updateLinks();
Simon Hunta17fa672015-08-19 18:42:22 -0700269 fStart();
Simon Hunt1894d792015-02-04 17:09:20 -0800270 }
271 }
272
273 function updateLink(data) {
Simon Huntdc6adea2015-02-09 22:29:36 -0800274 var result = tms.findLink(data, 'update'),
Simon Hunt1894d792015-02-04 17:09:20 -0800275 bad = result.badLogic;
276 if (bad) {
Simon Hunteb18f522016-01-28 19:22:23 -0800277 $log.debug(bad + ': ' + link.id);
Simon Hunt1894d792015-02-04 17:09:20 -0800278 return;
279 }
Simon Hunteb18f522016-01-28 19:22:23 -0800280 result.updateWith(data);
Simon Hunt1894d792015-02-04 17:09:20 -0800281 }
282
283 function removeLink(data) {
Simon Hunta4242de2015-02-24 17:11:55 -0800284 var result = tms.findLink(data, 'remove');
285
286 if (!result.badLogic) {
287 result.removeRawLink();
Simon Hunt1894d792015-02-04 17:09:20 -0800288 }
Simon Hunt1894d792015-02-04 17:09:20 -0800289 }
290
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700291 function topoStartDone(data) {
292 // called when the initial barrage of data has been sent from server
293 uplink.topoStartDone();
294 }
295
Simon Hunt1894d792015-02-04 17:09:20 -0800296 // ========================
297
Simon Hunt94f7dae2015-08-26 17:40:59 -0700298 function nodeById(id) {
299 return lu[id];
300 }
301
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700302 function makeNodeKey(node1, node2) {
303 return node1 + '-' + node2;
304 }
305
306 function findNodePair(key, keyRev) {
307 if (network.linksByDevice[key]) {
308 return key;
309 } else if (network.linksByDevice[keyRev]) {
310 return keyRev;
311 } else {
312 return false;
313 }
314 }
315
316 function aggregateLink(ldata, link) {
317 var key = makeNodeKey(link.src, link.dst),
318 keyRev = makeNodeKey(link.dst, link.src),
319 found = findNodePair(key, keyRev);
320
321 if (found) {
322 network.linksByDevice[found].push(ldata);
323 ldata.devicePair = found;
324 } else {
Steven Burrows1c2a9682017-07-14 16:52:46 +0100325 network.linksByDevice[key] = [ldata];
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700326 ldata.devicePair = key;
327 }
328 }
329
Simon Hunt1894d792015-02-04 17:09:20 -0800330 function addLinkUpdate(ldata, link) {
331 // add link event, but we already have the reverse link installed
332 ldata.fromTarget = link;
Simon Huntdc6adea2015-02-09 22:29:36 -0800333 rlk[link.id] = ldata.key;
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700334 // possible solution to el being undefined in restyleLinkElement:
Steven Burrows1c2a9682017-07-14 16:52:46 +0100335 // _updateLinks();
Simon Hunt1894d792015-02-04 17:09:20 -0800336 restyleLinkElement(ldata);
337 }
338
Simon Hunt1894d792015-02-04 17:09:20 -0800339
340 var widthRatio = 1.4,
341 linkScale = d3.scale.linear()
342 .domain([1, 12])
343 .range([widthRatio, 12 * widthRatio])
Simon Hunt5724fb42015-02-05 16:59:40 -0800344 .clamp(true),
Ray Milkeyb7f0f642016-01-22 16:08:14 -0800345 allLinkTypes = 'direct indirect optical tunnel',
346 allLinkSubTypes = 'inactive not-permitted';
Simon Hunt1894d792015-02-04 17:09:20 -0800347
Simon Hunta142dd22015-02-12 22:07:51 -0800348 function restyleLinkElement(ldata, immediate) {
Simon Hunt1894d792015-02-04 17:09:20 -0800349 // this fn's job is to look at raw links and decide what svg classes
350 // need to be applied to the line element in the DOM
351 var th = ts.theme(),
352 el = ldata.el,
353 type = ldata.type(),
354 lw = ldata.linkWidth(),
Simon Hunta142dd22015-02-12 22:07:51 -0800355 online = ldata.online(),
Ray Milkeyb7f0f642016-01-22 16:08:14 -0800356 modeCls = ldata.expected() ? 'inactive' : 'not-permitted',
Simon Hunta142dd22015-02-12 22:07:51 -0800357 delay = immediate ? 0 : 1000;
Simon Hunt1894d792015-02-04 17:09:20 -0800358
Simon Huntf44d7262016-06-14 14:46:56 -0700359 // NOTE: understand why el is sometimes undefined on addLink events...
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700360 // Investigated:
361 // el is undefined when it's a reverse link that is being added.
362 // updateLinks (which sets ldata.el) isn't called before this is called.
363 // Calling _updateLinks in addLinkUpdate fixes it, but there might be
364 // a more efficient way to fix it.
365 if (el && !el.empty()) {
Thomas Vachuskacb5016f2015-05-18 14:11:43 -0700366 el.classed('link', true);
Ray Milkeyb7f0f642016-01-22 16:08:14 -0800367 el.classed(allLinkSubTypes, false);
368 el.classed(modeCls, !online);
Thomas Vachuskacb5016f2015-05-18 14:11:43 -0700369 el.classed(allLinkTypes, false);
370 if (type) {
371 el.classed(type, true);
372 }
373 el.transition()
374 .duration(delay)
375 .attr('stroke-width', linkScale(lw))
376 .attr('stroke', linkConfig[th].baseColor);
Simon Hunt1894d792015-02-04 17:09:20 -0800377 }
Simon Hunt1894d792015-02-04 17:09:20 -0800378 }
379
Simon Hunt7df764f2017-09-14 21:26:14 -0700380 function removeAllLinkElements(links) {
381 links.forEach(function (lnk) {
382 removeLinkElement(lnk);
383 });
384 }
385
Simon Hunt1894d792015-02-04 17:09:20 -0800386 function removeLinkElement(d) {
387 var idx = fs.find(d.key, network.links, 'key'),
388 removed;
389 if (idx >=0) {
390 // remove from links array
391 removed = network.links.splice(idx, 1);
392 // remove from lookup cache
393 delete lu[removed[0].key];
394 updateLinks();
Simon Hunta17fa672015-08-19 18:42:22 -0700395 fResume();
Simon Hunt1894d792015-02-04 17:09:20 -0800396 }
397 }
398
399 function removeHostElement(d, upd) {
400 // first, remove associated hostLink...
401 removeLinkElement(d.linkData);
402
403 // remove hostLink bindings
404 delete lu[d.ingress];
405 delete lu[d.egress];
406
407 // remove from lookup cache
408 delete lu[d.id];
409 // remove from nodes array
410 var idx = fs.find(d.id, network.nodes);
411 network.nodes.splice(idx, 1);
412
413 // remove from SVG
414 // NOTE: upd is false if we were called from removeDeviceElement()
415 if (upd) {
416 updateNodes();
Simon Hunta17fa672015-08-19 18:42:22 -0700417 fResume();
Simon Hunt1894d792015-02-04 17:09:20 -0800418 }
419 }
420
421 function removeDeviceElement(d) {
Bri Prebilic Cole8d003782015-07-31 15:33:06 -0700422 var id = d.id,
423 idx;
Simon Hunt1894d792015-02-04 17:09:20 -0800424 // first, remove associated hosts and links..
Simon Huntdc6adea2015-02-09 22:29:36 -0800425 tms.findAttachedHosts(id).forEach(removeHostElement);
426 tms.findAttachedLinks(id).forEach(removeLinkElement);
Simon Hunt1894d792015-02-04 17:09:20 -0800427
428 // remove from lookup cache
429 delete lu[id];
430 // remove from nodes array
Bri Prebilic Cole8d003782015-07-31 15:33:06 -0700431 idx = fs.find(id, network.nodes);
432 if (idx > -1) {
433 network.nodes.splice(idx, 1);
434 }
Simon Hunt1894d792015-02-04 17:09:20 -0800435
436 if (!network.nodes.length) {
Simon Huntdc6adea2015-02-09 22:29:36 -0800437 uplink.showNoDevs(true);
Simon Hunt1894d792015-02-04 17:09:20 -0800438 }
439
440 // remove from SVG
441 updateNodes();
Simon Hunta17fa672015-08-19 18:42:22 -0700442 fResume();
Simon Hunt1894d792015-02-04 17:09:20 -0800443 }
444
Simon Hunt5724fb42015-02-05 16:59:40 -0800445 function updateHostVisibility() {
Simon Hunt18bf9822015-02-12 17:35:45 -0800446 sus.visible(nodeG.selectAll('.host'), showHosts);
447 sus.visible(linkG.selectAll('.hostLink'), showHosts);
Simon Hunt8eb4d3a2015-02-23 18:23:29 -0800448 sus.visible(linkLabelG.selectAll('.hostLinkLabel'), showHosts);
Simon Hunt5724fb42015-02-05 16:59:40 -0800449 }
450
451 function updateOfflineVisibility(dev) {
452 function updDev(d, show) {
Simon Hunt8eb4d3a2015-02-23 18:23:29 -0800453 var b;
Simon Hunt18bf9822015-02-12 17:35:45 -0800454 sus.visible(d.el, show);
Simon Hunt5724fb42015-02-05 16:59:40 -0800455
Simon Huntdc6adea2015-02-09 22:29:36 -0800456 tms.findAttachedLinks(d.id).forEach(function (link) {
Simon Hunt5724fb42015-02-05 16:59:40 -0800457 b = show && ((link.type() !== 'hostLink') || showHosts);
Simon Hunt18bf9822015-02-12 17:35:45 -0800458 sus.visible(link.el, b);
Simon Hunt5724fb42015-02-05 16:59:40 -0800459 });
Simon Huntdc6adea2015-02-09 22:29:36 -0800460 tms.findAttachedHosts(d.id).forEach(function (host) {
Simon Hunt5724fb42015-02-05 16:59:40 -0800461 b = show && showHosts;
Simon Hunt18bf9822015-02-12 17:35:45 -0800462 sus.visible(host.el, b);
Simon Hunt5724fb42015-02-05 16:59:40 -0800463 });
464 }
465
466 if (dev) {
467 // updating a specific device that just toggled off/on-line
468 updDev(dev, dev.online || showOffline);
469 } else {
470 // updating all offline devices
Simon Huntdc6adea2015-02-09 22:29:36 -0800471 tms.findDevices(true).forEach(function (d) {
Simon Hunt5724fb42015-02-05 16:59:40 -0800472 updDev(d, showOffline);
473 });
474 }
475 }
476
Simon Hunt1894d792015-02-04 17:09:20 -0800477
Simon Hunt445e8152015-02-06 13:00:12 -0800478 function sendUpdateMeta(d, clearPos) {
Simon Huntac4c6f72015-02-03 19:50:53 -0800479 var metaUi = {},
480 ll;
481
Simon Hunt445e8152015-02-06 13:00:12 -0800482 // if we are not clearing the position data (unpinning),
Simon Huntfd7106c2016-02-09 15:05:26 -0800483 // attach the x, y, (and equivalent longitude, latitude)...
Simon Hunt445e8152015-02-06 13:00:12 -0800484 if (!clearPos) {
Simon Hunt3a6eec02015-02-09 21:16:43 -0800485 ll = tms.lngLatFromCoord([d.x, d.y]);
Simon Huntfd7106c2016-02-09 15:05:26 -0800486 metaUi = {
487 x: d.x,
488 y: d.y,
489 equivLoc: {
490 lng: ll[0],
Steven Burrows1c2a9682017-07-14 16:52:46 +0100491 lat: ll[1],
492 },
Simon Huntfd7106c2016-02-09 15:05:26 -0800493 };
Simon Hunt1894d792015-02-04 17:09:20 -0800494 }
495 d.metaUi = metaUi;
Simon Hunt237676b52015-03-10 19:04:26 -0700496 wss.sendEvent('updateMeta', {
Simon Hunt1894d792015-02-04 17:09:20 -0800497 id: d.id,
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700498 class: d.class,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100499 memento: metaUi,
Simon Hunt1894d792015-02-04 17:09:20 -0800500 });
Simon Huntac4c6f72015-02-03 19:50:53 -0800501 }
502
Simon Hunt1894d792015-02-04 17:09:20 -0800503
Simon Huntac4c6f72015-02-03 19:50:53 -0800504 function mkSvgClass(d) {
505 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
506 }
507
Simon Hunt5724fb42015-02-05 16:59:40 -0800508 function vis(b) {
Simon Hunt1603c692017-08-10 19:53:35 -0700509 return topoLion(b ? 'visible' : 'hidden');
Simon Hunt5724fb42015-02-05 16:59:40 -0800510 }
511
Simon Huntfcbde892015-04-16 12:05:28 -0700512 function toggleHosts(x) {
513 var kev = (x === 'keyev'),
514 on = kev ? !showHosts : !!x;
515
516 showHosts = on;
Simon Hunt5724fb42015-02-05 16:59:40 -0800517 updateHostVisibility();
Simon Hunte2d9dc72017-08-10 15:21:04 -0700518 flash.flash(topoLion('hosts') + ' ' + vis(on));
Simon Huntfcbde892015-04-16 12:05:28 -0700519 return on;
Simon Hunt5724fb42015-02-05 16:59:40 -0800520 }
521
Simon Huntfcbde892015-04-16 12:05:28 -0700522 function toggleOffline(x) {
523 var kev = (x === 'keyev'),
524 on = kev ? !showOffline : !!x;
525
526 showOffline = on;
Simon Hunt5724fb42015-02-05 16:59:40 -0800527 updateOfflineVisibility();
Simon Hunte2d9dc72017-08-10 15:21:04 -0700528 flash.flash(topoLion('fl_offline_devices') + ' ' + vis(on));
Simon Huntfcbde892015-04-16 12:05:28 -0700529 return on;
Simon Hunt5724fb42015-02-05 16:59:40 -0800530 }
531
532 function cycleDeviceLabels() {
Bri Prebilic Cole9cf1a8d2015-04-21 13:15:29 -0700533 flash.flash(td3.incDevLabIndex());
Simon Huntdc6adea2015-02-09 22:29:36 -0800534 tms.findDevices().forEach(function (d) {
Simon Hunta4242de2015-02-24 17:11:55 -0800535 td3.updateDeviceLabel(d);
Simon Hunt1c367112015-02-05 18:02:46 -0800536 });
Simon Hunt5724fb42015-02-05 16:59:40 -0800537 }
538
Simon Hunt10618f62017-06-15 19:30:52 -0700539 function cycleHostLabels() {
540 flash.flash(td3.incHostLabIndex());
541 tms.findHosts().forEach(function (d) {
542 td3.updateHostLabel(d);
543 });
544 }
545
Simon Hunt445e8152015-02-06 13:00:12 -0800546 function unpin() {
Simon Hunt08f841d02015-02-10 14:39:20 -0800547 var hov = tss.hovered();
548 if (hov) {
549 sendUpdateMeta(hov, true);
550 hov.fixed = false;
551 hov.el.classed('fixed', false);
Simon Hunt445e8152015-02-06 13:00:12 -0800552 fResume();
553 }
554 }
555
Simon Hunta142dd22015-02-12 22:07:51 -0800556 function showMastership(masterId) {
557 if (!masterId) {
558 restoreLayerState();
559 } else {
560 showMastershipFor(masterId);
561 }
562 }
563
564 function restoreLayerState() {
565 // NOTE: this level of indirection required, for when we have
566 // the layer filter functionality re-implemented
567 suppressLayers(false);
568 }
569
570 function showMastershipFor(id) {
571 suppressLayers(true);
572 node.each(function (n) {
573 if (n.master === id) {
Simon Hunt743a8492015-08-25 16:18:19 -0700574 n.el.classed('suppressedmax', false);
Simon Hunta142dd22015-02-12 22:07:51 -0800575 }
576 });
577 }
578
Simon Hunt743a8492015-08-25 16:18:19 -0700579 function supAmt(less) {
Steven Burrows1c2a9682017-07-14 16:52:46 +0100580 return less ? 'suppressed' : 'suppressedmax';
Simon Hunt743a8492015-08-25 16:18:19 -0700581 }
582
583 function suppressLayers(b, less) {
584 var cls = supAmt(less);
585 node.classed(cls, b);
586 link.classed(cls, b);
587 }
588
589 function unsuppressNode(id, less) {
590 var cls = supAmt(less);
591 node.each(function (n) {
592 if (n.id === id) {
593 n.el.classed(cls, false);
594 }
595 });
596 }
597
Simon Hunt94f7dae2015-08-26 17:40:59 -0700598 function unsuppressLink(key, less) {
Simon Hunt743a8492015-08-25 16:18:19 -0700599 var cls = supAmt(less);
600 link.each(function (n) {
Simon Hunt94f7dae2015-08-26 17:40:59 -0700601 if (n.key === key) {
Simon Hunt743a8492015-08-25 16:18:19 -0700602 n.el.classed(cls, false);
603 }
604 });
Simon Hunta142dd22015-02-12 22:07:51 -0800605 }
Simon Hunt445e8152015-02-06 13:00:12 -0800606
Simon Hunt86b7c882015-04-02 23:06:08 -0700607 function showBadLinks() {
608 var badLinks = tms.findBadLinks();
Simon Hunte2d9dc72017-08-10 15:21:04 -0700609 flash.flash(topoLion('fl_bad_links') + ': ' + badLinks.length);
Simon Hunt86b7c882015-04-02 23:06:08 -0700610 $log.debug('Bad Link List (' + badLinks.length + '):');
611 badLinks.forEach(function (d) {
612 $log.debug('bad link: (' + d.bad + ') ' + d.key, d);
613 if (d.el) {
614 d.el.attr('stroke-width', linkScale(2.8))
615 .attr('stroke', 'red');
616 }
617 });
618 // back to normal after 2 seconds...
619 $timeout(updateLinks, 2000);
620 }
621
Steven Burrowsf17f0ab2017-04-11 11:03:58 -0700622 function deviceScale() {
623 var scale = uplink.zoomer().scale(),
624 dim = devIconDim,
625 multiplier = 1;
626
627 if (dim * scale < devIconDimMin) {
628 multiplier = devIconDimMin / (dim * scale);
629 } else if (dim * scale > devIconDimMax) {
630 multiplier = devIconDimMax / (dim * scale);
631 }
632
633 return multiplier;
634 }
635
636 function linkWidthScale(scale) {
637 var scale = uplink.zoomer().scale();
638 return linkScale(widthRatio) / scale;
639 }
640
641 function portLabelScale(scale) {
642 var scale = uplink.zoomer().scale();
643 return portLabelDim / (portLabelDim * scale);
644 }
645
646 function setNodeScale(scale) {
647 // Scale the network nodes
648 _.each(network.nodes, function (node) {
649 if (node.class === 'host') {
650 node.el.selectAll('g').style('transform', 'scale(' + deviceScale(scale) + ')');
651 node.el.selectAll('text').style('transform', 'scale(' + deviceScale(scale) + ')');
652 return;
653 }
654 node.el.selectAll('*')
655 .style('transform', 'scale(' + deviceScale(scale) + ')');
656 });
657
658 // Scale the network links
659 _.each(network.links, function (link) {
660 link.el.style('stroke-width', linkWidthScale(scale) + 'px');
661 });
662
663 d3.select('#topo-portLabels')
664 .selectAll('.portLabel')
665 .selectAll('*')
666 .style('transform', 'scale(' + portLabelScale(scale) + ')');
667 }
668
Simon Huntfd7106c2016-02-09 15:05:26 -0800669 function resetAllLocations() {
670 tms.resetAllLocations();
671 updateNodes();
672 tick(); // force nodes to be redrawn in their new locations
Simon Hunte2d9dc72017-08-10 15:21:04 -0700673 flash.flash(topoLion('fl_reset_node_locations'));
Simon Huntfd7106c2016-02-09 15:05:26 -0800674 }
675
Simon Hunt5724fb42015-02-05 16:59:40 -0800676 // ==========================================
677
Simon Huntac4c6f72015-02-03 19:50:53 -0800678 function updateNodes() {
Thomas Vachuska1a989c12015-06-09 18:29:22 -0700679 if (fNodesTimer) {
680 $timeout.cancel(fNodesTimer);
681 }
682 fNodesTimer = $timeout(_updateNodes, 150);
683 }
684
Simon Hunta17fa672015-08-19 18:42:22 -0700685 // IMPLEMENTATION NOTE: _updateNodes() should NOT stop, start, or resume
686 // the force layout; that needs to be determined and implemented elsewhere
Thomas Vachuska1a989c12015-06-09 18:29:22 -0700687 function _updateNodes() {
Simon Hunt1894d792015-02-04 17:09:20 -0800688 // select all the nodes in the layout:
Simon Huntac4c6f72015-02-03 19:50:53 -0800689 node = nodeG.selectAll('.node')
690 .data(network.nodes, function (d) { return d.id; });
691
Simon Hunt1894d792015-02-04 17:09:20 -0800692 // operate on existing nodes:
Simon Hunta4242de2015-02-24 17:11:55 -0800693 node.filter('.device').each(td3.deviceExisting);
694 node.filter('.host').each(td3.hostExisting);
Simon Huntac4c6f72015-02-03 19:50:53 -0800695
696 // operate on entering nodes:
697 var entering = node.enter()
698 .append('g')
699 .attr({
700 id: function (d) { return sus.safeId(d.id); },
701 class: mkSvgClass,
Simon Hunta17fa672015-08-19 18:42:22 -0700702 transform: function (d) {
703 // Need to guard against NaN here ??
704 return sus.translate(d.x, d.y);
705 },
Steven Burrows1c2a9682017-07-14 16:52:46 +0100706 opacity: 0,
Simon Huntac4c6f72015-02-03 19:50:53 -0800707 })
708 .call(drag)
Simon Hunt08f841d02015-02-10 14:39:20 -0800709 .on('mouseover', tss.nodeMouseOver)
710 .on('mouseout', tss.nodeMouseOut)
Simon Huntac4c6f72015-02-03 19:50:53 -0800711 .transition()
712 .attr('opacity', 1);
713
Simon Hunt1894d792015-02-04 17:09:20 -0800714 // augment entering nodes:
Simon Hunta4242de2015-02-24 17:11:55 -0800715 entering.filter('.device').each(td3.deviceEnter);
716 entering.filter('.host').each(td3.hostEnter);
Simon Huntac4c6f72015-02-03 19:50:53 -0800717
Simon Hunt51056592015-02-03 21:48:07 -0800718 // operate on both existing and new nodes:
Simon Hunta4242de2015-02-24 17:11:55 -0800719 td3.updateDeviceColors();
Simon Huntac4c6f72015-02-03 19:50:53 -0800720
721 // operate on exiting nodes:
722 // Note that the node is removed after 2 seconds.
723 // Sub element animations should be shorter than 2 seconds.
724 var exiting = node.exit()
725 .transition()
726 .duration(2000)
727 .style('opacity', 0)
728 .remove();
729
Simon Hunt1894d792015-02-04 17:09:20 -0800730 // exiting node specifics:
Simon Hunta4242de2015-02-24 17:11:55 -0800731 exiting.filter('.host').each(td3.hostExit);
732 exiting.filter('.device').each(td3.deviceExit);
Simon Huntac4c6f72015-02-03 19:50:53 -0800733 }
734
Simon Hunt51056592015-02-03 21:48:07 -0800735 // ==========================
Simon Hunt1894d792015-02-04 17:09:20 -0800736
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700737 function getDefaultPos(link) {
738 return {
739 x1: link.source.x,
740 y1: link.source.y,
741 x2: link.target.x,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100742 y2: link.target.y,
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700743 };
744 }
745
746 // returns amount of adjustment along the normal for given link
747 function amt(numLinks, linkIdx) {
748 var gap = 6;
749 return (linkIdx - ((numLinks - 1) / 2)) * gap;
750 }
751
752 function calcMovement(d, amt, flipped) {
753 var pos = getDefaultPos(d),
754 mult = flipped ? -amt : amt,
755 dx = pos.x2 - pos.x1,
756 dy = pos.y2 - pos.y1,
757 length = Math.sqrt((dx * dx) + (dy * dy));
758
759 return {
760 x1: pos.x1 + (mult * dy / length),
761 y1: pos.y1 + (mult * -dx / length),
762 x2: pos.x2 + (mult * dy / length),
Steven Burrows1c2a9682017-07-14 16:52:46 +0100763 y2: pos.y2 + (mult * -dx / length),
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700764 };
765 }
766
767 function calcPosition() {
768 var lines = this,
769 linkSrcId;
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700770 linkNums = [];
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700771 lines.each(function (d) {
772 if (d.type() === 'hostLink') {
773 d.position = getDefaultPos(d);
774 }
775 });
776
777 function normalizeLinkSrc(link) {
778 // ensure source device is consistent across set of links
779 // temporary measure until link modeling is refactored
780 if (!linkSrcId) {
781 linkSrcId = link.source.id;
782 return false;
783 }
784
785 return link.source.id !== linkSrcId;
786 }
787
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700788 angular.forEach(network.linksByDevice, function (linkArr, key) {
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700789 var numLinks = linkArr.length,
790 link;
791
792 if (numLinks === 1) {
793 link = linkArr[0];
794 link.position = getDefaultPos(link);
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700795 link.position.multiLink = false;
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700796 } else if (numLinks >= 5) {
797 // this code is inefficient, in the future the way links
798 // are modeled will be changed
799 angular.forEach(linkArr, function (link) {
800 link.position = getDefaultPos(link);
801 link.position.multiLink = true;
802 });
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700803 linkNums.push({
804 id: key,
805 num: numLinks,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100806 linkCoords: linkArr[0].position,
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700807 });
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700808 } else {
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700809 linkSrcId = null;
810 angular.forEach(linkArr, function (link, index) {
811 var offsetAmt = amt(numLinks, index),
812 needToFlip = normalizeLinkSrc(link);
813 link.position = calcMovement(link, offsetAmt, needToFlip);
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700814 link.position.multiLink = false;
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700815 });
816 }
817 });
818 }
819
Simon Hunt1894d792015-02-04 17:09:20 -0800820 function updateLinks() {
Thomas Vachuska1a989c12015-06-09 18:29:22 -0700821 if (fLinksTimer) {
822 $timeout.cancel(fLinksTimer);
823 }
824 fLinksTimer = $timeout(_updateLinks, 150);
825 }
826
Simon Hunta17fa672015-08-19 18:42:22 -0700827 // IMPLEMENTATION NOTE: _updateLinks() should NOT stop, start, or resume
828 // the force layout; that needs to be determined and implemented elsewhere
Thomas Vachuska1a989c12015-06-09 18:29:22 -0700829 function _updateLinks() {
Simon Hunt1894d792015-02-04 17:09:20 -0800830 var th = ts.theme();
831
832 link = linkG.selectAll('.link')
833 .data(network.links, function (d) { return d.key; });
834
835 // operate on existing links:
Simon Huntd5264122015-02-25 10:17:43 -0800836 link.each(function (d) {
837 // this is supposed to be an existing link, but we have observed
838 // occasions (where links are deleted and added rapidly?) where
839 // the DOM element has not been defined. So protect against that...
840 if (d.el) {
841 restyleLinkElement(d, true);
842 }
843 });
Simon Hunt1894d792015-02-04 17:09:20 -0800844
845 // operate on entering links:
846 var entering = link.enter()
847 .append('line')
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700848 .call(calcPosition)
Simon Hunt1894d792015-02-04 17:09:20 -0800849 .attr({
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700850 x1: function (d) { return d.position.x1; },
851 y1: function (d) { return d.position.y1; },
852 x2: function (d) { return d.position.x2; },
853 y2: function (d) { return d.position.y2; },
Simon Hunt1894d792015-02-04 17:09:20 -0800854 stroke: linkConfig[th].inColor,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100855 'stroke-width': linkConfig.inWidth,
Simon Hunt1894d792015-02-04 17:09:20 -0800856 });
857
858 // augment links
Simon Hunta4242de2015-02-24 17:11:55 -0800859 entering.each(td3.linkEntering);
Simon Hunt1894d792015-02-04 17:09:20 -0800860
861 // operate on both existing and new links:
Steven Burrows1c2a9682017-07-14 16:52:46 +0100862 // link.each(...)
Simon Hunt1894d792015-02-04 17:09:20 -0800863
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700864 // add labels for how many links are in a thick line
865 td3.applyNumLinkLabels(linkNums, numLinkLblsG);
866
Simon Hunt1894d792015-02-04 17:09:20 -0800867 // apply or remove labels
Simon Hunta4242de2015-02-24 17:11:55 -0800868 td3.applyLinkLabels();
Simon Hunt1894d792015-02-04 17:09:20 -0800869
870 // operate on exiting links:
871 link.exit()
872 .attr('stroke-dasharray', '3 3')
Simon Hunt5724fb42015-02-05 16:59:40 -0800873 .attr('stroke', linkConfig[th].outColor)
Simon Hunt1894d792015-02-04 17:09:20 -0800874 .style('opacity', 0.5)
875 .transition()
876 .duration(1500)
877 .attr({
878 'stroke-dasharray': '3 12',
Steven Burrows1c2a9682017-07-14 16:52:46 +0100879 'stroke-width': linkConfig.outWidth,
Simon Hunt1894d792015-02-04 17:09:20 -0800880 })
881 .style('opacity', 0.0)
882 .remove();
Simon Hunt1894d792015-02-04 17:09:20 -0800883 }
884
Simon Huntac4c6f72015-02-03 19:50:53 -0800885
886 // ==========================
Simon Hunt737c89f2015-01-28 12:23:19 -0800887 // force layout tick function
Simon Hunt737c89f2015-01-28 12:23:19 -0800888
Simon Hunt5724fb42015-02-05 16:59:40 -0800889 function fResume() {
Simon Huntc3c5b672015-02-20 11:32:13 -0800890 if (!tos.isOblique()) {
Simon Hunt5724fb42015-02-05 16:59:40 -0800891 force.resume();
892 }
893 }
894
895 function fStart() {
Simon Huntc3c5b672015-02-20 11:32:13 -0800896 if (!tos.isOblique()) {
Simon Hunta17fa672015-08-19 18:42:22 -0700897 if (fTimer) {
898 $timeout.cancel(fTimer);
899 }
900 fTimer = $timeout(function () {
Steven Burrows1c2a9682017-07-14 16:52:46 +0100901 $log.debug('Starting force-layout');
Simon Hunta17fa672015-08-19 18:42:22 -0700902 force.start();
903 }, 200);
Simon Hunt5724fb42015-02-05 16:59:40 -0800904 }
905 }
906
907 var tickStuff = {
908 nodeAttr: {
Simon Hunta17fa672015-08-19 18:42:22 -0700909 transform: function (d) {
910 var dx = isNaN(d.x) ? 0 : d.x,
911 dy = isNaN(d.y) ? 0 : d.y;
912 return sus.translate(dx, dy);
Steven Burrows1c2a9682017-07-14 16:52:46 +0100913 },
Simon Hunt5724fb42015-02-05 16:59:40 -0800914 },
915 linkAttr: {
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700916 x1: function (d) { return d.position.x1; },
917 y1: function (d) { return d.position.y1; },
918 x2: function (d) { return d.position.x2; },
Steven Burrows1c2a9682017-07-14 16:52:46 +0100919 y2: function (d) { return d.position.y2; },
Simon Hunt5724fb42015-02-05 16:59:40 -0800920 },
921 linkLabelAttr: {
922 transform: function (d) {
Simon Huntdc6adea2015-02-09 22:29:36 -0800923 var lnk = tms.findLinkById(d.key);
Simon Hunt5724fb42015-02-05 16:59:40 -0800924 if (lnk) {
Carmelo Casconed01eda62016-08-02 10:19:15 -0700925 return td3.transformLabel(lnk.position, d.key);
Simon Hunt5724fb42015-02-05 16:59:40 -0800926 }
Steven Burrows1c2a9682017-07-14 16:52:46 +0100927 },
928 },
Simon Hunt5724fb42015-02-05 16:59:40 -0800929 };
930
931 function tick() {
Simon Hunt3ab20282015-02-26 20:32:19 -0800932 // guard against null (which can happen when our view pages out)...
Bri Prebilic Cole8d003782015-07-31 15:33:06 -0700933 if (node && node.size()) {
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700934 node.attr(tickStuff.nodeAttr);
935 }
Bri Prebilic Cole8d003782015-07-31 15:33:06 -0700936 if (link && link.size()) {
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700937 link.call(calcPosition)
938 .attr(tickStuff.linkAttr);
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700939 td3.applyNumLinkLabels(linkNums, numLinkLblsG);
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700940 }
Bri Prebilic Cole8d003782015-07-31 15:33:06 -0700941 if (linkLabel && linkLabel.size()) {
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700942 linkLabel.attr(tickStuff.linkLabelAttr);
943 }
Simon Hunt737c89f2015-01-28 12:23:19 -0800944 }
945
946
Simon Huntac4c6f72015-02-03 19:50:53 -0800947 // ==========================
948 // === MOUSE GESTURE HANDLERS
949
Simon Hunt205099e2015-02-07 13:12:01 -0800950 function zoomingOrPanning(ev) {
951 return ev.metaKey || ev.altKey;
Simon Hunt445e8152015-02-06 13:00:12 -0800952 }
953
954 function atDragEnd(d) {
955 // once we've finished moving, pin the node in position
956 d.fixed = true;
957 d3.select(this).classed('fixed', true);
958 sendUpdateMeta(d);
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700959 tss.clickConsumed(true);
Simon Hunt445e8152015-02-06 13:00:12 -0800960 }
961
962 // predicate that indicates when dragging is active
963 function dragEnabled() {
964 var ev = d3.event.sourceEvent;
965 // nodeLock means we aren't allowing nodes to be dragged...
Simon Hunt205099e2015-02-07 13:12:01 -0800966 return !nodeLock && !zoomingOrPanning(ev);
Simon Hunt445e8152015-02-06 13:00:12 -0800967 }
968
969 // predicate that indicates when clicking is active
970 function clickEnabled() {
971 return true;
972 }
Simon Hunt737c89f2015-01-28 12:23:19 -0800973
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700974 // =============================================
975 // function entry points for overlay module
Simon Huntf542d842015-02-11 16:20:33 -0800976
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700977 // TODO: find an automatic way of tracking via the "showHighlights" events
Simon Hunte50829c2015-06-09 08:39:28 -0700978 var allTrafficClasses = 'primary secondary optical animated ' +
Simon Hunt21281fd2017-03-30 22:28:28 -0700979 'port-traffic-green port-traffic-yellow port-traffic-orange ' +
980 'port-traffic-red';
Simon Huntf542d842015-02-11 16:20:33 -0800981
982 function clearLinkTrafficStyle() {
983 link.style('stroke-width', null)
984 .classed(allTrafficClasses, false);
985 }
986
987 function removeLinkLabels() {
988 network.links.forEach(function (d) {
989 d.label = '';
990 });
991 }
Simon Hunt737c89f2015-01-28 12:23:19 -0800992
Simon Hunte9343f32015-10-21 18:07:46 -0700993 function clearNodeDeco() {
994 node.selectAll('g.badge').remove();
995 }
996
997 function removeNodeBadges() {
998 network.nodes.forEach(function (d) {
999 d.badge = null;
1000 });
1001 }
1002
Simon Hunta4242de2015-02-24 17:11:55 -08001003 function updateLinkLabelModel() {
1004 // create the backing data for showing labels..
1005 var data = [];
1006 link.each(function (d) {
1007 if (d.label) {
1008 data.push({
1009 id: 'lab-' + d.key,
1010 key: d.key,
1011 label: d.label,
Steven Burrows1c2a9682017-07-14 16:52:46 +01001012 ldata: d,
Simon Hunta4242de2015-02-24 17:11:55 -08001013 });
1014 }
1015 });
1016
1017 linkLabel = linkLabelG.selectAll('.linkLabel')
1018 .data(data, function (d) { return d.id; });
1019 }
1020
Simon Hunt737c89f2015-01-28 12:23:19 -08001021 // ==========================
Simon Huntac4c6f72015-02-03 19:50:53 -08001022 // Module definition
Simon Hunt737c89f2015-01-28 12:23:19 -08001023
Simon Huntdc6adea2015-02-09 22:29:36 -08001024 function mkModelApi(uplink) {
1025 return {
1026 projection: uplink.projection,
1027 network: network,
1028 restyleLinkElement: restyleLinkElement,
Steven Burrows1c2a9682017-07-14 16:52:46 +01001029 removeLinkElement: removeLinkElement,
Simon Huntdc6adea2015-02-09 22:29:36 -08001030 };
1031 }
1032
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001033 function mkD3Api() {
Simon Hunta4242de2015-02-24 17:11:55 -08001034 return {
1035 node: function () { return node; },
1036 link: function () { return link; },
1037 linkLabel: function () { return linkLabel; },
1038 instVisible: function () { return tis.isVisible(); },
1039 posNode: tms.positionNode,
1040 showHosts: function () { return showHosts; },
1041 restyleLinkElement: restyleLinkElement,
Bri Prebilic Cole80401762015-07-16 11:36:18 -07001042 updateLinkLabelModel: updateLinkLabelModel,
Steven Burrowsf17f0ab2017-04-11 11:03:58 -07001043 linkConfig: function () { return linkConfig; },
1044 deviceScale: deviceScale,
Steven Burrows1c2a9682017-07-14 16:52:46 +01001045 linkWidthScale: linkWidthScale,
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -07001046 };
Simon Hunta4242de2015-02-24 17:11:55 -08001047 }
1048
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001049 function mkSelectApi() {
Simon Hunt08f841d02015-02-10 14:39:20 -08001050 return {
1051 node: function () { return node; },
1052 zoomingOrPanning: zoomingOrPanning,
Simon Hunt0c6b2d32015-03-26 17:46:29 -07001053 updateDeviceColors: td3.updateDeviceColors,
Steven Burrows1c2a9682017-07-14 16:52:46 +01001054 deselectAllLinks: tls.deselectAllLinks,
Simon Hunt08f841d02015-02-10 14:39:20 -08001055 };
1056 }
1057
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001058 function mkTrafficApi() {
1059 return {
1060 hovered: tss.hovered,
1061 somethingSelected: tss.somethingSelected,
Steven Burrows1c2a9682017-07-14 16:52:46 +01001062 selectOrder: tss.selectOrder,
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001063 };
1064 }
1065
1066 function mkOverlayApi() {
Simon Huntf542d842015-02-11 16:20:33 -08001067 return {
Simon Hunte9343f32015-10-21 18:07:46 -07001068 clearNodeDeco: clearNodeDeco,
1069 removeNodeBadges: removeNodeBadges,
Simon Huntf542d842015-02-11 16:20:33 -08001070 clearLinkTrafficStyle: clearLinkTrafficStyle,
1071 removeLinkLabels: removeLinkLabels,
Simon Hunt743a8492015-08-25 16:18:19 -07001072 findLinkById: tms.findLinkById,
Simon Hunt94f7dae2015-08-26 17:40:59 -07001073 findNodeById: nodeById,
Simon Huntf542d842015-02-11 16:20:33 -08001074 updateLinks: updateLinks,
Simon Hunt743a8492015-08-25 16:18:19 -07001075 updateNodes: updateNodes,
1076 supLayers: suppressLayers,
1077 unsupNode: unsuppressNode,
Steven Burrows1c2a9682017-07-14 16:52:46 +01001078 unsupLink: unsuppressLink,
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -07001079 };
Simon Huntf542d842015-02-11 16:20:33 -08001080 }
1081
Simon Huntc3c5b672015-02-20 11:32:13 -08001082 function mkObliqueApi(uplink, fltr) {
Simon Hunt96f88c62015-02-19 17:57:25 -08001083 return {
Steven Burrows1c2a9682017-07-14 16:52:46 +01001084 force: function () { return force; },
Simon Huntc3c5b672015-02-20 11:32:13 -08001085 zoomLayer: uplink.zoomLayer,
Steven Burrows1c2a9682017-07-14 16:52:46 +01001086 nodeGBBox: function () { return nodeG.node().getBBox(); },
Simon Hunt96f88c62015-02-19 17:57:25 -08001087 node: function () { return node; },
Simon Huntc3c5b672015-02-20 11:32:13 -08001088 link: function () { return link; },
1089 linkLabel: function () { return linkLabel; },
1090 nodes: function () { return network.nodes; },
1091 tickStuff: tickStuff,
1092 nodeLock: function (b) {
1093 var old = nodeLock;
1094 nodeLock = b;
1095 return old;
1096 },
1097 opacifyMap: uplink.opacifyMap,
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -07001098 inLayer: fltr.inLayer,
Bri Prebilic Cole80401762015-07-16 11:36:18 -07001099 calcLinkPos: calcPosition,
1100 applyNumLinkLabels: function () {
1101 td3.applyNumLinkLabels(linkNums, numLinkLblsG);
Steven Burrows1c2a9682017-07-14 16:52:46 +01001102 },
Simon Hunt96f88c62015-02-19 17:57:25 -08001103 };
1104 }
1105
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001106 function mkFilterApi() {
Simon Hunteb0fa052015-02-17 19:20:28 -08001107 return {
1108 node: function () { return node; },
Steven Burrows1c2a9682017-07-14 16:52:46 +01001109 link: function () { return link; },
Simon Hunteb0fa052015-02-17 19:20:28 -08001110 };
1111 }
1112
Simon Hunt9e2104c2015-02-26 10:48:59 -08001113 function mkLinkApi(svg, uplink) {
Simon Huntfb8ea1f2015-02-24 21:38:09 -08001114 return {
1115 svg: svg,
Simon Huntfb8ea1f2015-02-24 21:38:09 -08001116 zoomer: uplink.zoomer(),
1117 network: network,
Simon Hunt1a5301e2015-02-25 15:31:25 -08001118 portLabelG: function () { return portLabelG; },
Steven Burrows1c2a9682017-07-14 16:52:46 +01001119 showHosts: function () { return showHosts; },
Simon Huntfb8ea1f2015-02-24 21:38:09 -08001120 };
1121 }
1122
Simon Huntf51bf462016-06-29 16:22:57 -07001123 function updateLinksAndNodes() {
1124 updateLinks();
1125 updateNodes();
1126 }
Steven Burrowsec1f45c2016-08-08 16:14:41 +01001127
Simon Hunte2d9dc72017-08-10 15:21:04 -07001128 // invoked after the localization bundle has been received from the server
1129 function setLionBundle(bundle) {
1130 topoLion = bundle;
1131 td3.setLionBundle(bundle);
1132 fltr.setLionBundle(bundle);
1133 tls.setLionBundle(bundle);
Simon Hunt1603c692017-08-10 19:53:35 -07001134 tos.setLionBundle(bundle);
1135 tov.setLionBundle(bundle);
Simon Huntcaed0412017-08-12 13:49:17 -07001136 tss.setLionBundle(bundle);
Simon Hunte2d9dc72017-08-10 15:21:04 -07001137 }
1138
Simon Hunt737c89f2015-01-28 12:23:19 -08001139 angular.module('ovTopo')
1140 .factory('TopoForceService',
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -07001141 ['$log', '$timeout', 'FnService', 'SvgUtilService',
Simon Hunt86b7c882015-04-02 23:06:08 -07001142 'ThemeService', 'FlashService', 'WebSocketService',
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001143 'TopoOverlayService', 'TopoInstService', 'TopoModelService',
Simon Hunta4242de2015-02-24 17:11:55 -08001144 'TopoD3Service', 'TopoSelectService', 'TopoTrafficService',
Simon Huntfb8ea1f2015-02-24 21:38:09 -08001145 'TopoObliqueService', 'TopoFilterService', 'TopoLinkService',
Andrea Campanella732ea832017-02-06 09:25:59 -08001146 'TopoProtectedIntentsService',
Simon Hunt737c89f2015-01-28 12:23:19 -08001147
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001148 function (_$log_, _$timeout_, _fs_, _sus_, _ts_, _flash_, _wss_, _tov_,
Andrea Campanella732ea832017-02-06 09:25:59 -08001149 _tis_, _tms_, _td3_, _tss_, _tts_, _tos_, _fltr_, _tls_, _tpis_) {
Simon Hunt737c89f2015-01-28 12:23:19 -08001150 $log = _$log_;
Simon Hunt86b7c882015-04-02 23:06:08 -07001151 $timeout = _$timeout_;
Simon Hunt1894d792015-02-04 17:09:20 -08001152 fs = _fs_;
Simon Hunt737c89f2015-01-28 12:23:19 -08001153 sus = _sus_;
Simon Huntac4c6f72015-02-03 19:50:53 -08001154 ts = _ts_;
Simon Hunt5724fb42015-02-05 16:59:40 -08001155 flash = _flash_;
Simon Hunt237676b52015-03-10 19:04:26 -07001156 wss = _wss_;
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001157 tov = _tov_;
Simon Huntac4c6f72015-02-03 19:50:53 -08001158 tis = _tis_;
Simon Hunt3a6eec02015-02-09 21:16:43 -08001159 tms = _tms_;
Simon Hunta4242de2015-02-24 17:11:55 -08001160 td3 = _td3_;
Simon Hunt08f841d02015-02-10 14:39:20 -08001161 tss = _tss_;
Simon Huntf542d842015-02-11 16:20:33 -08001162 tts = _tts_;
Simon Hunt96f88c62015-02-19 17:57:25 -08001163 tos = _tos_;
Simon Hunteb0fa052015-02-17 19:20:28 -08001164 fltr = _fltr_;
Simon Huntfb8ea1f2015-02-24 21:38:09 -08001165 tls = _tls_;
Andrea Campanella732ea832017-02-06 09:25:59 -08001166 tpis = _tpis_;
Simon Hunt737c89f2015-01-28 12:23:19 -08001167
Simon Huntf51bf462016-06-29 16:22:57 -07001168 ts.addListener(updateLinksAndNodes);
Simon Hunta142dd22015-02-12 22:07:51 -08001169
Simon Hunt737c89f2015-01-28 12:23:19 -08001170 // forceG is the SVG group to display the force layout in
Simon Huntdc6adea2015-02-09 22:29:36 -08001171 // uplink is the api from the main topo source file
Simon Hunt3a6eec02015-02-09 21:16:43 -08001172 // dim is the initial dimensions of the SVG as [w,h]
Simon Hunt737c89f2015-01-28 12:23:19 -08001173 // opts are, well, optional :)
Simon Hunt3ab20282015-02-26 20:32:19 -08001174 function initForce(_svg_, forceG, _uplink_, _dim_, opts) {
Simon Hunt1894d792015-02-04 17:09:20 -08001175 uplink = _uplink_;
Simon Hunt3a6eec02015-02-09 21:16:43 -08001176 dim = _dim_;
Simon Hunt3ab20282015-02-26 20:32:19 -08001177 svg = _svg_;
1178
1179 lu = network.lookup;
1180 rlk = network.revLinkToKey;
Simon Hunt3a6eec02015-02-09 21:16:43 -08001181
1182 $log.debug('initForce().. dim = ' + dim);
1183
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001184 tov.setApi(mkOverlayApi(), tss);
Simon Huntdc6adea2015-02-09 22:29:36 -08001185 tms.initModel(mkModelApi(uplink), dim);
Steven Burrowsf17f0ab2017-04-11 11:03:58 -07001186 td3.initD3(mkD3Api(), uplink.zoomer());
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001187 tss.initSelect(mkSelectApi());
1188 tts.initTraffic(mkTrafficApi());
Andrea Campanella732ea832017-02-06 09:25:59 -08001189 tpis.initProtectedIntents(mkTrafficApi());
Simon Huntc3c5b672015-02-20 11:32:13 -08001190 tos.initOblique(mkObliqueApi(uplink, fltr));
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001191 fltr.initFilter(mkFilterApi());
Simon Hunt9e2104c2015-02-26 10:48:59 -08001192 tls.initLink(mkLinkApi(svg, uplink), td3);
Simon Hunta11b4eb2015-01-28 16:20:50 -08001193
Simon Hunt737c89f2015-01-28 12:23:19 -08001194 settings = angular.extend({}, defaultSettings, opts);
1195
1196 linkG = forceG.append('g').attr('id', 'topo-links');
1197 linkLabelG = forceG.append('g').attr('id', 'topo-linkLabels');
Bri Prebilic Cole80401762015-07-16 11:36:18 -07001198 numLinkLblsG = forceG.append('g').attr('id', 'topo-numLinkLabels');
Simon Hunt737c89f2015-01-28 12:23:19 -08001199 nodeG = forceG.append('g').attr('id', 'topo-nodes');
Simon Hunt1a5301e2015-02-25 15:31:25 -08001200 portLabelG = forceG.append('g').attr('id', 'topo-portLabels');
Simon Hunt737c89f2015-01-28 12:23:19 -08001201
1202 link = linkG.selectAll('.link');
1203 linkLabel = linkLabelG.selectAll('.linkLabel');
1204 node = nodeG.selectAll('.node');
1205
1206 force = d3.layout.force()
Simon Hunt3a6eec02015-02-09 21:16:43 -08001207 .size(dim)
Simon Hunt737c89f2015-01-28 12:23:19 -08001208 .nodes(network.nodes)
1209 .links(network.links)
1210 .gravity(settings.gravity)
1211 .friction(settings.friction)
1212 .charge(settings.charge._def_)
1213 .linkDistance(settings.linkDistance._def_)
1214 .linkStrength(settings.linkStrength._def_)
1215 .on('tick', tick);
1216
1217 drag = sus.createDragBehavior(force,
Simon Hunt08f841d02015-02-10 14:39:20 -08001218 tss.selectObject, atDragEnd, dragEnabled, clickEnabled);
Simon Hunt737c89f2015-01-28 12:23:19 -08001219 }
1220
Simon Hunt3a6eec02015-02-09 21:16:43 -08001221 function newDim(_dim_) {
1222 dim = _dim_;
1223 force.size(dim);
1224 tms.newDim(dim);
Simon Hunt737c89f2015-01-28 12:23:19 -08001225 }
1226
Simon Hunt3a6eec02015-02-09 21:16:43 -08001227 function destroyForce() {
Simon Hunt3ab20282015-02-26 20:32:19 -08001228 force.stop();
1229
Simon Huntfb8ea1f2015-02-24 21:38:09 -08001230 tls.destroyLink();
Simon Hunt96f88c62015-02-19 17:57:25 -08001231 tos.destroyOblique();
Simon Huntf542d842015-02-11 16:20:33 -08001232 tts.destroyTraffic();
Andrea Campanella732ea832017-02-06 09:25:59 -08001233 tpis.destroyProtectedIntents();
Simon Huntf542d842015-02-11 16:20:33 -08001234 tss.destroySelect();
Simon Hunta4242de2015-02-24 17:11:55 -08001235 td3.destroyD3();
Simon Huntf542d842015-02-11 16:20:33 -08001236 tms.destroyModel();
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001237 // note: no need to destroy overlay service
Simon Huntf51bf462016-06-29 16:22:57 -07001238 ts.removeListener(updateLinksAndNodes);
Simon Hunt3ab20282015-02-26 20:32:19 -08001239
1240 // clean up the DOM
1241 svg.selectAll('g').remove();
1242 svg.selectAll('defs').remove();
1243
1244 // clean up internal state
1245 network.nodes = [];
1246 network.links = [];
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -07001247 network.linksByDevice = {};
Simon Hunt3ab20282015-02-26 20:32:19 -08001248 network.lookup = {};
1249 network.revLinkToKey = {};
1250
Bri Prebilic Cole80401762015-07-16 11:36:18 -07001251 linkNums = [];
1252
1253 linkG = linkLabelG = numLinkLblsG = nodeG = portLabelG = null;
Simon Hunt3ab20282015-02-26 20:32:19 -08001254 link = linkLabel = node = null;
1255 force = drag = null;
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -07001256
1257 // clean up $timeout promises
Simon Hunta17fa672015-08-19 18:42:22 -07001258 if (fTimer) {
1259 $timeout.cancel(fTimer);
1260 }
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -07001261 if (fNodesTimer) {
1262 $timeout.cancel(fNodesTimer);
1263 }
1264 if (fLinksTimer) {
1265 $timeout.cancel(fLinksTimer);
1266 }
Simon Hunt3a6eec02015-02-09 21:16:43 -08001267 }
1268
Simon Hunt737c89f2015-01-28 12:23:19 -08001269 return {
1270 initForce: initForce,
Simon Hunt3a6eec02015-02-09 21:16:43 -08001271 newDim: newDim,
1272 destroyForce: destroyForce,
Simon Huntac4c6f72015-02-03 19:50:53 -08001273
Simon Hunta4242de2015-02-24 17:11:55 -08001274 updateDeviceColors: td3.updateDeviceColors,
Simon Hunt5724fb42015-02-05 16:59:40 -08001275 toggleHosts: toggleHosts,
Simon Hunt9e2104c2015-02-26 10:48:59 -08001276 togglePorts: tls.togglePorts,
Simon Hunt5724fb42015-02-05 16:59:40 -08001277 toggleOffline: toggleOffline,
1278 cycleDeviceLabels: cycleDeviceLabels,
Simon Hunt10618f62017-06-15 19:30:52 -07001279 cycleHostLabels: cycleHostLabels,
Simon Hunt445e8152015-02-06 13:00:12 -08001280 unpin: unpin,
Simon Hunta142dd22015-02-12 22:07:51 -08001281 showMastership: showMastership,
Simon Hunt86b7c882015-04-02 23:06:08 -07001282 showBadLinks: showBadLinks,
Steven Burrowsf17f0ab2017-04-11 11:03:58 -07001283 setNodeScale: setNodeScale,
Simon Huntac4c6f72015-02-03 19:50:53 -08001284
Simon Huntfd7106c2016-02-09 15:05:26 -08001285 resetAllLocations: resetAllLocations,
Simon Huntac4c6f72015-02-03 19:50:53 -08001286 addDevice: addDevice,
Simon Hunt1894d792015-02-04 17:09:20 -08001287 updateDevice: updateDevice,
1288 removeDevice: removeDevice,
1289 addHost: addHost,
1290 updateHost: updateHost,
Simon Hunt95d56fd2015-11-12 11:06:44 -08001291 moveHost: moveHost,
Simon Hunt1894d792015-02-04 17:09:20 -08001292 removeHost: removeHost,
1293 addLink: addLink,
1294 updateLink: updateLink,
Simon Hunt4a6b54b2015-10-27 22:08:25 -07001295 removeLink: removeLink,
Steven Burrows1c2a9682017-07-14 16:52:46 +01001296 topoStartDone: topoStartDone,
Simon Hunte2d9dc72017-08-10 15:21:04 -07001297
1298 setLionBundle: setLionBundle,
Simon Hunt737c89f2015-01-28 12:23:19 -08001299 };
1300 }]);
1301}());