blob: f3a2d0ee0d540d1a8a596db2ba2f55c3611a16f6 [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,
163 d, lnk;
164
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 Hunt3a6eec02015-02-09 21:16:43 -0800177 lnk = tms.createHostLink(data);
Simon Hunt1894d792015-02-04 17:09:20 -0800178 if (lnk) {
Steven Burrows1c2a9682017-07-14 16:52:46 +0100179 d.linkData = lnk; // cache ref on its host
Simon Hunt1894d792015-02-04 17:09:20 -0800180 network.links.push(lnk);
181 lu[d.ingress] = lnk;
182 lu[d.egress] = lnk;
183 updateLinks();
184 }
Simon Hunta17fa672015-08-19 18:42:22 -0700185 fStart();
Simon Hunt1894d792015-02-04 17:09:20 -0800186 }
187
188 function updateHost(data) {
189 var id = data.id,
190 d = lu[id];
191 if (d) {
192 angular.extend(d, data);
Simon Hunt3a6eec02015-02-09 21:16:43 -0800193 if (tms.positionNode(d, true)) {
Simon Hunt445e8152015-02-06 13:00:12 -0800194 sendUpdateMeta(d);
Simon Hunt1894d792015-02-04 17:09:20 -0800195 }
196 updateNodes();
Simon Hunt1894d792015-02-04 17:09:20 -0800197 }
198 }
199
Simon Hunt95d56fd2015-11-12 11:06:44 -0800200 function moveHost(data) {
201 var id = data.id,
202 d = lu[id],
203 lnk;
204 if (d) {
205 // first remove the old host link
206 removeLinkElement(d.linkData);
207
208 // merge new data
209 angular.extend(d, data);
210 if (tms.positionNode(d, true)) {
211 sendUpdateMeta(d);
212 }
213
214 // now create a new host link
215 lnk = tms.createHostLink(data);
216 if (lnk) {
217 d.linkData = lnk;
218 network.links.push(lnk);
219 lu[d.ingress] = lnk;
220 lu[d.egress] = lnk;
221 }
222
223 updateNodes();
224 updateLinks();
225 fResume();
226 }
227 }
228
Simon Hunt1894d792015-02-04 17:09:20 -0800229 function removeHost(data) {
230 var id = data.id,
231 d = lu[id];
232 if (d) {
233 removeHostElement(d, true);
Simon Hunt1894d792015-02-04 17:09:20 -0800234 }
235 }
236
237 function addLink(data) {
Simon Huntdc6adea2015-02-09 22:29:36 -0800238 var result = tms.findLink(data, 'add'),
Simon Hunt1894d792015-02-04 17:09:20 -0800239 bad = result.badLogic,
240 d = result.ldata;
241
242 if (bad) {
Simon Hunteb18f522016-01-28 19:22:23 -0800243 $log.debug(bad + ': ' + link.id);
Simon Hunt1894d792015-02-04 17:09:20 -0800244 return;
245 }
246
247 if (d) {
248 // we already have a backing store link for src/dst nodes
249 addLinkUpdate(d, data);
250 return;
251 }
252
253 // no backing store link yet
Simon Hunt3a6eec02015-02-09 21:16:43 -0800254 d = tms.createLink(data);
Simon Hunt1894d792015-02-04 17:09:20 -0800255 if (d) {
256 network.links.push(d);
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700257 aggregateLink(d, data);
Simon Hunt1894d792015-02-04 17:09:20 -0800258 lu[d.key] = d;
259 updateLinks();
Simon Hunta17fa672015-08-19 18:42:22 -0700260 fStart();
Simon Hunt1894d792015-02-04 17:09:20 -0800261 }
262 }
263
264 function updateLink(data) {
Simon Huntdc6adea2015-02-09 22:29:36 -0800265 var result = tms.findLink(data, 'update'),
Simon Hunt1894d792015-02-04 17:09:20 -0800266 bad = result.badLogic;
267 if (bad) {
Simon Hunteb18f522016-01-28 19:22:23 -0800268 $log.debug(bad + ': ' + link.id);
Simon Hunt1894d792015-02-04 17:09:20 -0800269 return;
270 }
Simon Hunteb18f522016-01-28 19:22:23 -0800271 result.updateWith(data);
Simon Hunt1894d792015-02-04 17:09:20 -0800272 }
273
274 function removeLink(data) {
Simon Hunta4242de2015-02-24 17:11:55 -0800275 var result = tms.findLink(data, 'remove');
276
277 if (!result.badLogic) {
278 result.removeRawLink();
Simon Hunt1894d792015-02-04 17:09:20 -0800279 }
Simon Hunt1894d792015-02-04 17:09:20 -0800280 }
281
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700282 function topoStartDone(data) {
283 // called when the initial barrage of data has been sent from server
284 uplink.topoStartDone();
285 }
286
Simon Hunt1894d792015-02-04 17:09:20 -0800287 // ========================
288
Simon Hunt94f7dae2015-08-26 17:40:59 -0700289 function nodeById(id) {
290 return lu[id];
291 }
292
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700293 function makeNodeKey(node1, node2) {
294 return node1 + '-' + node2;
295 }
296
297 function findNodePair(key, keyRev) {
298 if (network.linksByDevice[key]) {
299 return key;
300 } else if (network.linksByDevice[keyRev]) {
301 return keyRev;
302 } else {
303 return false;
304 }
305 }
306
307 function aggregateLink(ldata, link) {
308 var key = makeNodeKey(link.src, link.dst),
309 keyRev = makeNodeKey(link.dst, link.src),
310 found = findNodePair(key, keyRev);
311
312 if (found) {
313 network.linksByDevice[found].push(ldata);
314 ldata.devicePair = found;
315 } else {
Steven Burrows1c2a9682017-07-14 16:52:46 +0100316 network.linksByDevice[key] = [ldata];
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700317 ldata.devicePair = key;
318 }
319 }
320
Simon Hunt1894d792015-02-04 17:09:20 -0800321 function addLinkUpdate(ldata, link) {
322 // add link event, but we already have the reverse link installed
323 ldata.fromTarget = link;
Simon Huntdc6adea2015-02-09 22:29:36 -0800324 rlk[link.id] = ldata.key;
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700325 // possible solution to el being undefined in restyleLinkElement:
Steven Burrows1c2a9682017-07-14 16:52:46 +0100326 // _updateLinks();
Simon Hunt1894d792015-02-04 17:09:20 -0800327 restyleLinkElement(ldata);
328 }
329
Simon Hunt1894d792015-02-04 17:09:20 -0800330
331 var widthRatio = 1.4,
332 linkScale = d3.scale.linear()
333 .domain([1, 12])
334 .range([widthRatio, 12 * widthRatio])
Simon Hunt5724fb42015-02-05 16:59:40 -0800335 .clamp(true),
Ray Milkeyb7f0f642016-01-22 16:08:14 -0800336 allLinkTypes = 'direct indirect optical tunnel',
337 allLinkSubTypes = 'inactive not-permitted';
Simon Hunt1894d792015-02-04 17:09:20 -0800338
Simon Hunta142dd22015-02-12 22:07:51 -0800339 function restyleLinkElement(ldata, immediate) {
Simon Hunt1894d792015-02-04 17:09:20 -0800340 // this fn's job is to look at raw links and decide what svg classes
341 // need to be applied to the line element in the DOM
342 var th = ts.theme(),
343 el = ldata.el,
344 type = ldata.type(),
345 lw = ldata.linkWidth(),
Simon Hunta142dd22015-02-12 22:07:51 -0800346 online = ldata.online(),
Ray Milkeyb7f0f642016-01-22 16:08:14 -0800347 modeCls = ldata.expected() ? 'inactive' : 'not-permitted',
Simon Hunta142dd22015-02-12 22:07:51 -0800348 delay = immediate ? 0 : 1000;
Simon Hunt1894d792015-02-04 17:09:20 -0800349
Simon Huntf44d7262016-06-14 14:46:56 -0700350 // NOTE: understand why el is sometimes undefined on addLink events...
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700351 // Investigated:
352 // el is undefined when it's a reverse link that is being added.
353 // updateLinks (which sets ldata.el) isn't called before this is called.
354 // Calling _updateLinks in addLinkUpdate fixes it, but there might be
355 // a more efficient way to fix it.
356 if (el && !el.empty()) {
Thomas Vachuskacb5016f2015-05-18 14:11:43 -0700357 el.classed('link', true);
Ray Milkeyb7f0f642016-01-22 16:08:14 -0800358 el.classed(allLinkSubTypes, false);
359 el.classed(modeCls, !online);
Thomas Vachuskacb5016f2015-05-18 14:11:43 -0700360 el.classed(allLinkTypes, false);
361 if (type) {
362 el.classed(type, true);
363 }
364 el.transition()
365 .duration(delay)
366 .attr('stroke-width', linkScale(lw))
367 .attr('stroke', linkConfig[th].baseColor);
Simon Hunt1894d792015-02-04 17:09:20 -0800368 }
Simon Hunt1894d792015-02-04 17:09:20 -0800369 }
370
Simon Hunt1894d792015-02-04 17:09:20 -0800371 function removeLinkElement(d) {
372 var idx = fs.find(d.key, network.links, 'key'),
373 removed;
374 if (idx >=0) {
375 // remove from links array
376 removed = network.links.splice(idx, 1);
377 // remove from lookup cache
378 delete lu[removed[0].key];
379 updateLinks();
Simon Hunta17fa672015-08-19 18:42:22 -0700380 fResume();
Simon Hunt1894d792015-02-04 17:09:20 -0800381 }
382 }
383
384 function removeHostElement(d, upd) {
385 // first, remove associated hostLink...
386 removeLinkElement(d.linkData);
387
388 // remove hostLink bindings
389 delete lu[d.ingress];
390 delete lu[d.egress];
391
392 // remove from lookup cache
393 delete lu[d.id];
394 // remove from nodes array
395 var idx = fs.find(d.id, network.nodes);
396 network.nodes.splice(idx, 1);
397
398 // remove from SVG
399 // NOTE: upd is false if we were called from removeDeviceElement()
400 if (upd) {
401 updateNodes();
Simon Hunta17fa672015-08-19 18:42:22 -0700402 fResume();
Simon Hunt1894d792015-02-04 17:09:20 -0800403 }
404 }
405
406 function removeDeviceElement(d) {
Bri Prebilic Cole8d003782015-07-31 15:33:06 -0700407 var id = d.id,
408 idx;
Simon Hunt1894d792015-02-04 17:09:20 -0800409 // first, remove associated hosts and links..
Simon Huntdc6adea2015-02-09 22:29:36 -0800410 tms.findAttachedHosts(id).forEach(removeHostElement);
411 tms.findAttachedLinks(id).forEach(removeLinkElement);
Simon Hunt1894d792015-02-04 17:09:20 -0800412
413 // remove from lookup cache
414 delete lu[id];
415 // remove from nodes array
Bri Prebilic Cole8d003782015-07-31 15:33:06 -0700416 idx = fs.find(id, network.nodes);
417 if (idx > -1) {
418 network.nodes.splice(idx, 1);
419 }
Simon Hunt1894d792015-02-04 17:09:20 -0800420
421 if (!network.nodes.length) {
Simon Huntdc6adea2015-02-09 22:29:36 -0800422 uplink.showNoDevs(true);
Simon Hunt1894d792015-02-04 17:09:20 -0800423 }
424
425 // remove from SVG
426 updateNodes();
Simon Hunta17fa672015-08-19 18:42:22 -0700427 fResume();
Simon Hunt1894d792015-02-04 17:09:20 -0800428 }
429
Simon Hunt5724fb42015-02-05 16:59:40 -0800430 function updateHostVisibility() {
Simon Hunt18bf9822015-02-12 17:35:45 -0800431 sus.visible(nodeG.selectAll('.host'), showHosts);
432 sus.visible(linkG.selectAll('.hostLink'), showHosts);
Simon Hunt8eb4d3a2015-02-23 18:23:29 -0800433 sus.visible(linkLabelG.selectAll('.hostLinkLabel'), showHosts);
Simon Hunt5724fb42015-02-05 16:59:40 -0800434 }
435
436 function updateOfflineVisibility(dev) {
437 function updDev(d, show) {
Simon Hunt8eb4d3a2015-02-23 18:23:29 -0800438 var b;
Simon Hunt18bf9822015-02-12 17:35:45 -0800439 sus.visible(d.el, show);
Simon Hunt5724fb42015-02-05 16:59:40 -0800440
Simon Huntdc6adea2015-02-09 22:29:36 -0800441 tms.findAttachedLinks(d.id).forEach(function (link) {
Simon Hunt5724fb42015-02-05 16:59:40 -0800442 b = show && ((link.type() !== 'hostLink') || showHosts);
Simon Hunt18bf9822015-02-12 17:35:45 -0800443 sus.visible(link.el, b);
Simon Hunt5724fb42015-02-05 16:59:40 -0800444 });
Simon Huntdc6adea2015-02-09 22:29:36 -0800445 tms.findAttachedHosts(d.id).forEach(function (host) {
Simon Hunt5724fb42015-02-05 16:59:40 -0800446 b = show && showHosts;
Simon Hunt18bf9822015-02-12 17:35:45 -0800447 sus.visible(host.el, b);
Simon Hunt5724fb42015-02-05 16:59:40 -0800448 });
449 }
450
451 if (dev) {
452 // updating a specific device that just toggled off/on-line
453 updDev(dev, dev.online || showOffline);
454 } else {
455 // updating all offline devices
Simon Huntdc6adea2015-02-09 22:29:36 -0800456 tms.findDevices(true).forEach(function (d) {
Simon Hunt5724fb42015-02-05 16:59:40 -0800457 updDev(d, showOffline);
458 });
459 }
460 }
461
Simon Hunt1894d792015-02-04 17:09:20 -0800462
Simon Hunt445e8152015-02-06 13:00:12 -0800463 function sendUpdateMeta(d, clearPos) {
Simon Huntac4c6f72015-02-03 19:50:53 -0800464 var metaUi = {},
465 ll;
466
Simon Hunt445e8152015-02-06 13:00:12 -0800467 // if we are not clearing the position data (unpinning),
Simon Huntfd7106c2016-02-09 15:05:26 -0800468 // attach the x, y, (and equivalent longitude, latitude)...
Simon Hunt445e8152015-02-06 13:00:12 -0800469 if (!clearPos) {
Simon Hunt3a6eec02015-02-09 21:16:43 -0800470 ll = tms.lngLatFromCoord([d.x, d.y]);
Simon Huntfd7106c2016-02-09 15:05:26 -0800471 metaUi = {
472 x: d.x,
473 y: d.y,
474 equivLoc: {
475 lng: ll[0],
Steven Burrows1c2a9682017-07-14 16:52:46 +0100476 lat: ll[1],
477 },
Simon Huntfd7106c2016-02-09 15:05:26 -0800478 };
Simon Hunt1894d792015-02-04 17:09:20 -0800479 }
480 d.metaUi = metaUi;
Simon Hunt237676b52015-03-10 19:04:26 -0700481 wss.sendEvent('updateMeta', {
Simon Hunt1894d792015-02-04 17:09:20 -0800482 id: d.id,
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700483 class: d.class,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100484 memento: metaUi,
Simon Hunt1894d792015-02-04 17:09:20 -0800485 });
Simon Huntac4c6f72015-02-03 19:50:53 -0800486 }
487
Simon Hunt1894d792015-02-04 17:09:20 -0800488
Simon Huntac4c6f72015-02-03 19:50:53 -0800489 function mkSvgClass(d) {
490 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
491 }
492
Simon Hunt5724fb42015-02-05 16:59:40 -0800493 function vis(b) {
Simon Hunt1603c692017-08-10 19:53:35 -0700494 return topoLion(b ? 'visible' : 'hidden');
Simon Hunt5724fb42015-02-05 16:59:40 -0800495 }
496
Simon Huntfcbde892015-04-16 12:05:28 -0700497 function toggleHosts(x) {
498 var kev = (x === 'keyev'),
499 on = kev ? !showHosts : !!x;
500
501 showHosts = on;
Simon Hunt5724fb42015-02-05 16:59:40 -0800502 updateHostVisibility();
Simon Hunte2d9dc72017-08-10 15:21:04 -0700503 flash.flash(topoLion('hosts') + ' ' + vis(on));
Simon Huntfcbde892015-04-16 12:05:28 -0700504 return on;
Simon Hunt5724fb42015-02-05 16:59:40 -0800505 }
506
Simon Huntfcbde892015-04-16 12:05:28 -0700507 function toggleOffline(x) {
508 var kev = (x === 'keyev'),
509 on = kev ? !showOffline : !!x;
510
511 showOffline = on;
Simon Hunt5724fb42015-02-05 16:59:40 -0800512 updateOfflineVisibility();
Simon Hunte2d9dc72017-08-10 15:21:04 -0700513 flash.flash(topoLion('fl_offline_devices') + ' ' + vis(on));
Simon Huntfcbde892015-04-16 12:05:28 -0700514 return on;
Simon Hunt5724fb42015-02-05 16:59:40 -0800515 }
516
517 function cycleDeviceLabels() {
Bri Prebilic Cole9cf1a8d2015-04-21 13:15:29 -0700518 flash.flash(td3.incDevLabIndex());
Simon Huntdc6adea2015-02-09 22:29:36 -0800519 tms.findDevices().forEach(function (d) {
Simon Hunta4242de2015-02-24 17:11:55 -0800520 td3.updateDeviceLabel(d);
Simon Hunt1c367112015-02-05 18:02:46 -0800521 });
Simon Hunt5724fb42015-02-05 16:59:40 -0800522 }
523
Simon Hunt10618f62017-06-15 19:30:52 -0700524 function cycleHostLabels() {
525 flash.flash(td3.incHostLabIndex());
526 tms.findHosts().forEach(function (d) {
527 td3.updateHostLabel(d);
528 });
529 }
530
Simon Hunt445e8152015-02-06 13:00:12 -0800531 function unpin() {
Simon Hunt08f841d02015-02-10 14:39:20 -0800532 var hov = tss.hovered();
533 if (hov) {
534 sendUpdateMeta(hov, true);
535 hov.fixed = false;
536 hov.el.classed('fixed', false);
Simon Hunt445e8152015-02-06 13:00:12 -0800537 fResume();
538 }
539 }
540
Simon Hunta142dd22015-02-12 22:07:51 -0800541 function showMastership(masterId) {
542 if (!masterId) {
543 restoreLayerState();
544 } else {
545 showMastershipFor(masterId);
546 }
547 }
548
549 function restoreLayerState() {
550 // NOTE: this level of indirection required, for when we have
551 // the layer filter functionality re-implemented
552 suppressLayers(false);
553 }
554
555 function showMastershipFor(id) {
556 suppressLayers(true);
557 node.each(function (n) {
558 if (n.master === id) {
Simon Hunt743a8492015-08-25 16:18:19 -0700559 n.el.classed('suppressedmax', false);
Simon Hunta142dd22015-02-12 22:07:51 -0800560 }
561 });
562 }
563
Simon Hunt743a8492015-08-25 16:18:19 -0700564 function supAmt(less) {
Steven Burrows1c2a9682017-07-14 16:52:46 +0100565 return less ? 'suppressed' : 'suppressedmax';
Simon Hunt743a8492015-08-25 16:18:19 -0700566 }
567
568 function suppressLayers(b, less) {
569 var cls = supAmt(less);
570 node.classed(cls, b);
571 link.classed(cls, b);
572 }
573
574 function unsuppressNode(id, less) {
575 var cls = supAmt(less);
576 node.each(function (n) {
577 if (n.id === id) {
578 n.el.classed(cls, false);
579 }
580 });
581 }
582
Simon Hunt94f7dae2015-08-26 17:40:59 -0700583 function unsuppressLink(key, less) {
Simon Hunt743a8492015-08-25 16:18:19 -0700584 var cls = supAmt(less);
585 link.each(function (n) {
Simon Hunt94f7dae2015-08-26 17:40:59 -0700586 if (n.key === key) {
Simon Hunt743a8492015-08-25 16:18:19 -0700587 n.el.classed(cls, false);
588 }
589 });
Simon Hunta142dd22015-02-12 22:07:51 -0800590 }
Simon Hunt445e8152015-02-06 13:00:12 -0800591
Simon Hunt86b7c882015-04-02 23:06:08 -0700592 function showBadLinks() {
593 var badLinks = tms.findBadLinks();
Simon Hunte2d9dc72017-08-10 15:21:04 -0700594 flash.flash(topoLion('fl_bad_links') + ': ' + badLinks.length);
Simon Hunt86b7c882015-04-02 23:06:08 -0700595 $log.debug('Bad Link List (' + badLinks.length + '):');
596 badLinks.forEach(function (d) {
597 $log.debug('bad link: (' + d.bad + ') ' + d.key, d);
598 if (d.el) {
599 d.el.attr('stroke-width', linkScale(2.8))
600 .attr('stroke', 'red');
601 }
602 });
603 // back to normal after 2 seconds...
604 $timeout(updateLinks, 2000);
605 }
606
Steven Burrowsf17f0ab2017-04-11 11:03:58 -0700607 function deviceScale() {
608 var scale = uplink.zoomer().scale(),
609 dim = devIconDim,
610 multiplier = 1;
611
612 if (dim * scale < devIconDimMin) {
613 multiplier = devIconDimMin / (dim * scale);
614 } else if (dim * scale > devIconDimMax) {
615 multiplier = devIconDimMax / (dim * scale);
616 }
617
618 return multiplier;
619 }
620
621 function linkWidthScale(scale) {
622 var scale = uplink.zoomer().scale();
623 return linkScale(widthRatio) / scale;
624 }
625
626 function portLabelScale(scale) {
627 var scale = uplink.zoomer().scale();
628 return portLabelDim / (portLabelDim * scale);
629 }
630
631 function setNodeScale(scale) {
632 // Scale the network nodes
633 _.each(network.nodes, function (node) {
634 if (node.class === 'host') {
635 node.el.selectAll('g').style('transform', 'scale(' + deviceScale(scale) + ')');
636 node.el.selectAll('text').style('transform', 'scale(' + deviceScale(scale) + ')');
637 return;
638 }
639 node.el.selectAll('*')
640 .style('transform', 'scale(' + deviceScale(scale) + ')');
641 });
642
643 // Scale the network links
644 _.each(network.links, function (link) {
645 link.el.style('stroke-width', linkWidthScale(scale) + 'px');
646 });
647
648 d3.select('#topo-portLabels')
649 .selectAll('.portLabel')
650 .selectAll('*')
651 .style('transform', 'scale(' + portLabelScale(scale) + ')');
652 }
653
Simon Huntfd7106c2016-02-09 15:05:26 -0800654 function resetAllLocations() {
655 tms.resetAllLocations();
656 updateNodes();
657 tick(); // force nodes to be redrawn in their new locations
Simon Hunte2d9dc72017-08-10 15:21:04 -0700658 flash.flash(topoLion('fl_reset_node_locations'));
Simon Huntfd7106c2016-02-09 15:05:26 -0800659 }
660
Simon Hunt5724fb42015-02-05 16:59:40 -0800661 // ==========================================
662
Simon Huntac4c6f72015-02-03 19:50:53 -0800663 function updateNodes() {
Thomas Vachuska1a989c12015-06-09 18:29:22 -0700664 if (fNodesTimer) {
665 $timeout.cancel(fNodesTimer);
666 }
667 fNodesTimer = $timeout(_updateNodes, 150);
668 }
669
Simon Hunta17fa672015-08-19 18:42:22 -0700670 // IMPLEMENTATION NOTE: _updateNodes() should NOT stop, start, or resume
671 // the force layout; that needs to be determined and implemented elsewhere
Thomas Vachuska1a989c12015-06-09 18:29:22 -0700672 function _updateNodes() {
Simon Hunt1894d792015-02-04 17:09:20 -0800673 // select all the nodes in the layout:
Simon Huntac4c6f72015-02-03 19:50:53 -0800674 node = nodeG.selectAll('.node')
675 .data(network.nodes, function (d) { return d.id; });
676
Simon Hunt1894d792015-02-04 17:09:20 -0800677 // operate on existing nodes:
Simon Hunta4242de2015-02-24 17:11:55 -0800678 node.filter('.device').each(td3.deviceExisting);
679 node.filter('.host').each(td3.hostExisting);
Simon Huntac4c6f72015-02-03 19:50:53 -0800680
681 // operate on entering nodes:
682 var entering = node.enter()
683 .append('g')
684 .attr({
685 id: function (d) { return sus.safeId(d.id); },
686 class: mkSvgClass,
Simon Hunta17fa672015-08-19 18:42:22 -0700687 transform: function (d) {
688 // Need to guard against NaN here ??
689 return sus.translate(d.x, d.y);
690 },
Steven Burrows1c2a9682017-07-14 16:52:46 +0100691 opacity: 0,
Simon Huntac4c6f72015-02-03 19:50:53 -0800692 })
693 .call(drag)
Simon Hunt08f841d02015-02-10 14:39:20 -0800694 .on('mouseover', tss.nodeMouseOver)
695 .on('mouseout', tss.nodeMouseOut)
Simon Huntac4c6f72015-02-03 19:50:53 -0800696 .transition()
697 .attr('opacity', 1);
698
Simon Hunt1894d792015-02-04 17:09:20 -0800699 // augment entering nodes:
Simon Hunta4242de2015-02-24 17:11:55 -0800700 entering.filter('.device').each(td3.deviceEnter);
701 entering.filter('.host').each(td3.hostEnter);
Simon Huntac4c6f72015-02-03 19:50:53 -0800702
Simon Hunt51056592015-02-03 21:48:07 -0800703 // operate on both existing and new nodes:
Simon Hunta4242de2015-02-24 17:11:55 -0800704 td3.updateDeviceColors();
Simon Huntac4c6f72015-02-03 19:50:53 -0800705
706 // operate on exiting nodes:
707 // Note that the node is removed after 2 seconds.
708 // Sub element animations should be shorter than 2 seconds.
709 var exiting = node.exit()
710 .transition()
711 .duration(2000)
712 .style('opacity', 0)
713 .remove();
714
Simon Hunt1894d792015-02-04 17:09:20 -0800715 // exiting node specifics:
Simon Hunta4242de2015-02-24 17:11:55 -0800716 exiting.filter('.host').each(td3.hostExit);
717 exiting.filter('.device').each(td3.deviceExit);
Simon Huntac4c6f72015-02-03 19:50:53 -0800718 }
719
Simon Hunt51056592015-02-03 21:48:07 -0800720 // ==========================
Simon Hunt1894d792015-02-04 17:09:20 -0800721
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700722 function getDefaultPos(link) {
723 return {
724 x1: link.source.x,
725 y1: link.source.y,
726 x2: link.target.x,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100727 y2: link.target.y,
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700728 };
729 }
730
731 // returns amount of adjustment along the normal for given link
732 function amt(numLinks, linkIdx) {
733 var gap = 6;
734 return (linkIdx - ((numLinks - 1) / 2)) * gap;
735 }
736
737 function calcMovement(d, amt, flipped) {
738 var pos = getDefaultPos(d),
739 mult = flipped ? -amt : amt,
740 dx = pos.x2 - pos.x1,
741 dy = pos.y2 - pos.y1,
742 length = Math.sqrt((dx * dx) + (dy * dy));
743
744 return {
745 x1: pos.x1 + (mult * dy / length),
746 y1: pos.y1 + (mult * -dx / length),
747 x2: pos.x2 + (mult * dy / length),
Steven Burrows1c2a9682017-07-14 16:52:46 +0100748 y2: pos.y2 + (mult * -dx / length),
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700749 };
750 }
751
752 function calcPosition() {
753 var lines = this,
754 linkSrcId;
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700755 linkNums = [];
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700756 lines.each(function (d) {
757 if (d.type() === 'hostLink') {
758 d.position = getDefaultPos(d);
759 }
760 });
761
762 function normalizeLinkSrc(link) {
763 // ensure source device is consistent across set of links
764 // temporary measure until link modeling is refactored
765 if (!linkSrcId) {
766 linkSrcId = link.source.id;
767 return false;
768 }
769
770 return link.source.id !== linkSrcId;
771 }
772
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700773 angular.forEach(network.linksByDevice, function (linkArr, key) {
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700774 var numLinks = linkArr.length,
775 link;
776
777 if (numLinks === 1) {
778 link = linkArr[0];
779 link.position = getDefaultPos(link);
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700780 link.position.multiLink = false;
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700781 } else if (numLinks >= 5) {
782 // this code is inefficient, in the future the way links
783 // are modeled will be changed
784 angular.forEach(linkArr, function (link) {
785 link.position = getDefaultPos(link);
786 link.position.multiLink = true;
787 });
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700788 linkNums.push({
789 id: key,
790 num: numLinks,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100791 linkCoords: linkArr[0].position,
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700792 });
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700793 } else {
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700794 linkSrcId = null;
795 angular.forEach(linkArr, function (link, index) {
796 var offsetAmt = amt(numLinks, index),
797 needToFlip = normalizeLinkSrc(link);
798 link.position = calcMovement(link, offsetAmt, needToFlip);
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700799 link.position.multiLink = false;
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700800 });
801 }
802 });
803 }
804
Simon Hunt1894d792015-02-04 17:09:20 -0800805 function updateLinks() {
Thomas Vachuska1a989c12015-06-09 18:29:22 -0700806 if (fLinksTimer) {
807 $timeout.cancel(fLinksTimer);
808 }
809 fLinksTimer = $timeout(_updateLinks, 150);
810 }
811
Simon Hunta17fa672015-08-19 18:42:22 -0700812 // IMPLEMENTATION NOTE: _updateLinks() should NOT stop, start, or resume
813 // the force layout; that needs to be determined and implemented elsewhere
Thomas Vachuska1a989c12015-06-09 18:29:22 -0700814 function _updateLinks() {
Simon Hunt1894d792015-02-04 17:09:20 -0800815 var th = ts.theme();
816
817 link = linkG.selectAll('.link')
818 .data(network.links, function (d) { return d.key; });
819
820 // operate on existing links:
Simon Huntd5264122015-02-25 10:17:43 -0800821 link.each(function (d) {
822 // this is supposed to be an existing link, but we have observed
823 // occasions (where links are deleted and added rapidly?) where
824 // the DOM element has not been defined. So protect against that...
825 if (d.el) {
826 restyleLinkElement(d, true);
827 }
828 });
Simon Hunt1894d792015-02-04 17:09:20 -0800829
830 // operate on entering links:
831 var entering = link.enter()
832 .append('line')
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700833 .call(calcPosition)
Simon Hunt1894d792015-02-04 17:09:20 -0800834 .attr({
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700835 x1: function (d) { return d.position.x1; },
836 y1: function (d) { return d.position.y1; },
837 x2: function (d) { return d.position.x2; },
838 y2: function (d) { return d.position.y2; },
Simon Hunt1894d792015-02-04 17:09:20 -0800839 stroke: linkConfig[th].inColor,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100840 'stroke-width': linkConfig.inWidth,
Simon Hunt1894d792015-02-04 17:09:20 -0800841 });
842
843 // augment links
Simon Hunta4242de2015-02-24 17:11:55 -0800844 entering.each(td3.linkEntering);
Simon Hunt1894d792015-02-04 17:09:20 -0800845
846 // operate on both existing and new links:
Steven Burrows1c2a9682017-07-14 16:52:46 +0100847 // link.each(...)
Simon Hunt1894d792015-02-04 17:09:20 -0800848
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700849 // add labels for how many links are in a thick line
850 td3.applyNumLinkLabels(linkNums, numLinkLblsG);
851
Simon Hunt1894d792015-02-04 17:09:20 -0800852 // apply or remove labels
Simon Hunta4242de2015-02-24 17:11:55 -0800853 td3.applyLinkLabels();
Simon Hunt1894d792015-02-04 17:09:20 -0800854
855 // operate on exiting links:
856 link.exit()
857 .attr('stroke-dasharray', '3 3')
Simon Hunt5724fb42015-02-05 16:59:40 -0800858 .attr('stroke', linkConfig[th].outColor)
Simon Hunt1894d792015-02-04 17:09:20 -0800859 .style('opacity', 0.5)
860 .transition()
861 .duration(1500)
862 .attr({
863 'stroke-dasharray': '3 12',
Steven Burrows1c2a9682017-07-14 16:52:46 +0100864 'stroke-width': linkConfig.outWidth,
Simon Hunt1894d792015-02-04 17:09:20 -0800865 })
866 .style('opacity', 0.0)
867 .remove();
Simon Hunt1894d792015-02-04 17:09:20 -0800868 }
869
Simon Huntac4c6f72015-02-03 19:50:53 -0800870
871 // ==========================
Simon Hunt737c89f2015-01-28 12:23:19 -0800872 // force layout tick function
Simon Hunt737c89f2015-01-28 12:23:19 -0800873
Simon Hunt5724fb42015-02-05 16:59:40 -0800874 function fResume() {
Simon Huntc3c5b672015-02-20 11:32:13 -0800875 if (!tos.isOblique()) {
Simon Hunt5724fb42015-02-05 16:59:40 -0800876 force.resume();
877 }
878 }
879
880 function fStart() {
Simon Huntc3c5b672015-02-20 11:32:13 -0800881 if (!tos.isOblique()) {
Simon Hunta17fa672015-08-19 18:42:22 -0700882 if (fTimer) {
883 $timeout.cancel(fTimer);
884 }
885 fTimer = $timeout(function () {
Steven Burrows1c2a9682017-07-14 16:52:46 +0100886 $log.debug('Starting force-layout');
Simon Hunta17fa672015-08-19 18:42:22 -0700887 force.start();
888 }, 200);
Simon Hunt5724fb42015-02-05 16:59:40 -0800889 }
890 }
891
892 var tickStuff = {
893 nodeAttr: {
Simon Hunta17fa672015-08-19 18:42:22 -0700894 transform: function (d) {
895 var dx = isNaN(d.x) ? 0 : d.x,
896 dy = isNaN(d.y) ? 0 : d.y;
897 return sus.translate(dx, dy);
Steven Burrows1c2a9682017-07-14 16:52:46 +0100898 },
Simon Hunt5724fb42015-02-05 16:59:40 -0800899 },
900 linkAttr: {
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700901 x1: function (d) { return d.position.x1; },
902 y1: function (d) { return d.position.y1; },
903 x2: function (d) { return d.position.x2; },
Steven Burrows1c2a9682017-07-14 16:52:46 +0100904 y2: function (d) { return d.position.y2; },
Simon Hunt5724fb42015-02-05 16:59:40 -0800905 },
906 linkLabelAttr: {
907 transform: function (d) {
Simon Huntdc6adea2015-02-09 22:29:36 -0800908 var lnk = tms.findLinkById(d.key);
Simon Hunt5724fb42015-02-05 16:59:40 -0800909 if (lnk) {
Carmelo Casconed01eda62016-08-02 10:19:15 -0700910 return td3.transformLabel(lnk.position, d.key);
Simon Hunt5724fb42015-02-05 16:59:40 -0800911 }
Steven Burrows1c2a9682017-07-14 16:52:46 +0100912 },
913 },
Simon Hunt5724fb42015-02-05 16:59:40 -0800914 };
915
916 function tick() {
Simon Hunt3ab20282015-02-26 20:32:19 -0800917 // guard against null (which can happen when our view pages out)...
Bri Prebilic Cole8d003782015-07-31 15:33:06 -0700918 if (node && node.size()) {
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700919 node.attr(tickStuff.nodeAttr);
920 }
Bri Prebilic Cole8d003782015-07-31 15:33:06 -0700921 if (link && link.size()) {
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700922 link.call(calcPosition)
923 .attr(tickStuff.linkAttr);
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700924 td3.applyNumLinkLabels(linkNums, numLinkLblsG);
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700925 }
Bri Prebilic Cole8d003782015-07-31 15:33:06 -0700926 if (linkLabel && linkLabel.size()) {
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700927 linkLabel.attr(tickStuff.linkLabelAttr);
928 }
Simon Hunt737c89f2015-01-28 12:23:19 -0800929 }
930
931
Simon Huntac4c6f72015-02-03 19:50:53 -0800932 // ==========================
933 // === MOUSE GESTURE HANDLERS
934
Simon Hunt205099e2015-02-07 13:12:01 -0800935 function zoomingOrPanning(ev) {
936 return ev.metaKey || ev.altKey;
Simon Hunt445e8152015-02-06 13:00:12 -0800937 }
938
939 function atDragEnd(d) {
940 // once we've finished moving, pin the node in position
941 d.fixed = true;
942 d3.select(this).classed('fixed', true);
943 sendUpdateMeta(d);
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700944 tss.clickConsumed(true);
Simon Hunt445e8152015-02-06 13:00:12 -0800945 }
946
947 // predicate that indicates when dragging is active
948 function dragEnabled() {
949 var ev = d3.event.sourceEvent;
950 // nodeLock means we aren't allowing nodes to be dragged...
Simon Hunt205099e2015-02-07 13:12:01 -0800951 return !nodeLock && !zoomingOrPanning(ev);
Simon Hunt445e8152015-02-06 13:00:12 -0800952 }
953
954 // predicate that indicates when clicking is active
955 function clickEnabled() {
956 return true;
957 }
Simon Hunt737c89f2015-01-28 12:23:19 -0800958
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700959 // =============================================
960 // function entry points for overlay module
Simon Huntf542d842015-02-11 16:20:33 -0800961
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700962 // TODO: find an automatic way of tracking via the "showHighlights" events
Simon Hunte50829c2015-06-09 08:39:28 -0700963 var allTrafficClasses = 'primary secondary optical animated ' +
Simon Hunt21281fd2017-03-30 22:28:28 -0700964 'port-traffic-green port-traffic-yellow port-traffic-orange ' +
965 'port-traffic-red';
Simon Huntf542d842015-02-11 16:20:33 -0800966
967 function clearLinkTrafficStyle() {
968 link.style('stroke-width', null)
969 .classed(allTrafficClasses, false);
970 }
971
972 function removeLinkLabels() {
973 network.links.forEach(function (d) {
974 d.label = '';
975 });
976 }
Simon Hunt737c89f2015-01-28 12:23:19 -0800977
Simon Hunte9343f32015-10-21 18:07:46 -0700978 function clearNodeDeco() {
979 node.selectAll('g.badge').remove();
980 }
981
982 function removeNodeBadges() {
983 network.nodes.forEach(function (d) {
984 d.badge = null;
985 });
986 }
987
Simon Hunta4242de2015-02-24 17:11:55 -0800988 function updateLinkLabelModel() {
989 // create the backing data for showing labels..
990 var data = [];
991 link.each(function (d) {
992 if (d.label) {
993 data.push({
994 id: 'lab-' + d.key,
995 key: d.key,
996 label: d.label,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100997 ldata: d,
Simon Hunta4242de2015-02-24 17:11:55 -0800998 });
999 }
1000 });
1001
1002 linkLabel = linkLabelG.selectAll('.linkLabel')
1003 .data(data, function (d) { return d.id; });
1004 }
1005
Simon Hunt737c89f2015-01-28 12:23:19 -08001006 // ==========================
Simon Huntac4c6f72015-02-03 19:50:53 -08001007 // Module definition
Simon Hunt737c89f2015-01-28 12:23:19 -08001008
Simon Huntdc6adea2015-02-09 22:29:36 -08001009 function mkModelApi(uplink) {
1010 return {
1011 projection: uplink.projection,
1012 network: network,
1013 restyleLinkElement: restyleLinkElement,
Steven Burrows1c2a9682017-07-14 16:52:46 +01001014 removeLinkElement: removeLinkElement,
Simon Huntdc6adea2015-02-09 22:29:36 -08001015 };
1016 }
1017
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001018 function mkD3Api() {
Simon Hunta4242de2015-02-24 17:11:55 -08001019 return {
1020 node: function () { return node; },
1021 link: function () { return link; },
1022 linkLabel: function () { return linkLabel; },
1023 instVisible: function () { return tis.isVisible(); },
1024 posNode: tms.positionNode,
1025 showHosts: function () { return showHosts; },
1026 restyleLinkElement: restyleLinkElement,
Bri Prebilic Cole80401762015-07-16 11:36:18 -07001027 updateLinkLabelModel: updateLinkLabelModel,
Steven Burrowsf17f0ab2017-04-11 11:03:58 -07001028 linkConfig: function () { return linkConfig; },
1029 deviceScale: deviceScale,
Steven Burrows1c2a9682017-07-14 16:52:46 +01001030 linkWidthScale: linkWidthScale,
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -07001031 };
Simon Hunta4242de2015-02-24 17:11:55 -08001032 }
1033
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001034 function mkSelectApi() {
Simon Hunt08f841d02015-02-10 14:39:20 -08001035 return {
1036 node: function () { return node; },
1037 zoomingOrPanning: zoomingOrPanning,
Simon Hunt0c6b2d32015-03-26 17:46:29 -07001038 updateDeviceColors: td3.updateDeviceColors,
Steven Burrows1c2a9682017-07-14 16:52:46 +01001039 deselectAllLinks: tls.deselectAllLinks,
Simon Hunt08f841d02015-02-10 14:39:20 -08001040 };
1041 }
1042
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001043 function mkTrafficApi() {
1044 return {
1045 hovered: tss.hovered,
1046 somethingSelected: tss.somethingSelected,
Steven Burrows1c2a9682017-07-14 16:52:46 +01001047 selectOrder: tss.selectOrder,
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001048 };
1049 }
1050
1051 function mkOverlayApi() {
Simon Huntf542d842015-02-11 16:20:33 -08001052 return {
Simon Hunte9343f32015-10-21 18:07:46 -07001053 clearNodeDeco: clearNodeDeco,
1054 removeNodeBadges: removeNodeBadges,
Simon Huntf542d842015-02-11 16:20:33 -08001055 clearLinkTrafficStyle: clearLinkTrafficStyle,
1056 removeLinkLabels: removeLinkLabels,
Simon Hunt743a8492015-08-25 16:18:19 -07001057 findLinkById: tms.findLinkById,
Simon Hunt94f7dae2015-08-26 17:40:59 -07001058 findNodeById: nodeById,
Simon Huntf542d842015-02-11 16:20:33 -08001059 updateLinks: updateLinks,
Simon Hunt743a8492015-08-25 16:18:19 -07001060 updateNodes: updateNodes,
1061 supLayers: suppressLayers,
1062 unsupNode: unsuppressNode,
Steven Burrows1c2a9682017-07-14 16:52:46 +01001063 unsupLink: unsuppressLink,
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -07001064 };
Simon Huntf542d842015-02-11 16:20:33 -08001065 }
1066
Simon Huntc3c5b672015-02-20 11:32:13 -08001067 function mkObliqueApi(uplink, fltr) {
Simon Hunt96f88c62015-02-19 17:57:25 -08001068 return {
Steven Burrows1c2a9682017-07-14 16:52:46 +01001069 force: function () { return force; },
Simon Huntc3c5b672015-02-20 11:32:13 -08001070 zoomLayer: uplink.zoomLayer,
Steven Burrows1c2a9682017-07-14 16:52:46 +01001071 nodeGBBox: function () { return nodeG.node().getBBox(); },
Simon Hunt96f88c62015-02-19 17:57:25 -08001072 node: function () { return node; },
Simon Huntc3c5b672015-02-20 11:32:13 -08001073 link: function () { return link; },
1074 linkLabel: function () { return linkLabel; },
1075 nodes: function () { return network.nodes; },
1076 tickStuff: tickStuff,
1077 nodeLock: function (b) {
1078 var old = nodeLock;
1079 nodeLock = b;
1080 return old;
1081 },
1082 opacifyMap: uplink.opacifyMap,
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -07001083 inLayer: fltr.inLayer,
Bri Prebilic Cole80401762015-07-16 11:36:18 -07001084 calcLinkPos: calcPosition,
1085 applyNumLinkLabels: function () {
1086 td3.applyNumLinkLabels(linkNums, numLinkLblsG);
Steven Burrows1c2a9682017-07-14 16:52:46 +01001087 },
Simon Hunt96f88c62015-02-19 17:57:25 -08001088 };
1089 }
1090
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001091 function mkFilterApi() {
Simon Hunteb0fa052015-02-17 19:20:28 -08001092 return {
1093 node: function () { return node; },
Steven Burrows1c2a9682017-07-14 16:52:46 +01001094 link: function () { return link; },
Simon Hunteb0fa052015-02-17 19:20:28 -08001095 };
1096 }
1097
Simon Hunt9e2104c2015-02-26 10:48:59 -08001098 function mkLinkApi(svg, uplink) {
Simon Huntfb8ea1f2015-02-24 21:38:09 -08001099 return {
1100 svg: svg,
Simon Huntfb8ea1f2015-02-24 21:38:09 -08001101 zoomer: uplink.zoomer(),
1102 network: network,
Simon Hunt1a5301e2015-02-25 15:31:25 -08001103 portLabelG: function () { return portLabelG; },
Steven Burrows1c2a9682017-07-14 16:52:46 +01001104 showHosts: function () { return showHosts; },
Simon Huntfb8ea1f2015-02-24 21:38:09 -08001105 };
1106 }
1107
Simon Huntf51bf462016-06-29 16:22:57 -07001108 function updateLinksAndNodes() {
1109 updateLinks();
1110 updateNodes();
1111 }
Steven Burrowsec1f45c2016-08-08 16:14:41 +01001112
Simon Hunte2d9dc72017-08-10 15:21:04 -07001113 // invoked after the localization bundle has been received from the server
1114 function setLionBundle(bundle) {
1115 topoLion = bundle;
1116 td3.setLionBundle(bundle);
1117 fltr.setLionBundle(bundle);
1118 tls.setLionBundle(bundle);
Simon Hunt1603c692017-08-10 19:53:35 -07001119 tos.setLionBundle(bundle);
1120 tov.setLionBundle(bundle);
Simon Huntcaed0412017-08-12 13:49:17 -07001121 tss.setLionBundle(bundle);
Simon Hunte2d9dc72017-08-10 15:21:04 -07001122 }
1123
Simon Hunt737c89f2015-01-28 12:23:19 -08001124 angular.module('ovTopo')
1125 .factory('TopoForceService',
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -07001126 ['$log', '$timeout', 'FnService', 'SvgUtilService',
Simon Hunt86b7c882015-04-02 23:06:08 -07001127 'ThemeService', 'FlashService', 'WebSocketService',
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001128 'TopoOverlayService', 'TopoInstService', 'TopoModelService',
Simon Hunta4242de2015-02-24 17:11:55 -08001129 'TopoD3Service', 'TopoSelectService', 'TopoTrafficService',
Simon Huntfb8ea1f2015-02-24 21:38:09 -08001130 'TopoObliqueService', 'TopoFilterService', 'TopoLinkService',
Andrea Campanella732ea832017-02-06 09:25:59 -08001131 'TopoProtectedIntentsService',
Simon Hunt737c89f2015-01-28 12:23:19 -08001132
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001133 function (_$log_, _$timeout_, _fs_, _sus_, _ts_, _flash_, _wss_, _tov_,
Andrea Campanella732ea832017-02-06 09:25:59 -08001134 _tis_, _tms_, _td3_, _tss_, _tts_, _tos_, _fltr_, _tls_, _tpis_) {
Simon Hunt737c89f2015-01-28 12:23:19 -08001135 $log = _$log_;
Simon Hunt86b7c882015-04-02 23:06:08 -07001136 $timeout = _$timeout_;
Simon Hunt1894d792015-02-04 17:09:20 -08001137 fs = _fs_;
Simon Hunt737c89f2015-01-28 12:23:19 -08001138 sus = _sus_;
Simon Huntac4c6f72015-02-03 19:50:53 -08001139 ts = _ts_;
Simon Hunt5724fb42015-02-05 16:59:40 -08001140 flash = _flash_;
Simon Hunt237676b52015-03-10 19:04:26 -07001141 wss = _wss_;
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001142 tov = _tov_;
Simon Huntac4c6f72015-02-03 19:50:53 -08001143 tis = _tis_;
Simon Hunt3a6eec02015-02-09 21:16:43 -08001144 tms = _tms_;
Simon Hunta4242de2015-02-24 17:11:55 -08001145 td3 = _td3_;
Simon Hunt08f841d02015-02-10 14:39:20 -08001146 tss = _tss_;
Simon Huntf542d842015-02-11 16:20:33 -08001147 tts = _tts_;
Simon Hunt96f88c62015-02-19 17:57:25 -08001148 tos = _tos_;
Simon Hunteb0fa052015-02-17 19:20:28 -08001149 fltr = _fltr_;
Simon Huntfb8ea1f2015-02-24 21:38:09 -08001150 tls = _tls_;
Andrea Campanella732ea832017-02-06 09:25:59 -08001151 tpis = _tpis_;
Simon Hunt737c89f2015-01-28 12:23:19 -08001152
Simon Huntf51bf462016-06-29 16:22:57 -07001153 ts.addListener(updateLinksAndNodes);
Simon Hunta142dd22015-02-12 22:07:51 -08001154
Simon Hunt737c89f2015-01-28 12:23:19 -08001155 // forceG is the SVG group to display the force layout in
Simon Huntdc6adea2015-02-09 22:29:36 -08001156 // uplink is the api from the main topo source file
Simon Hunt3a6eec02015-02-09 21:16:43 -08001157 // dim is the initial dimensions of the SVG as [w,h]
Simon Hunt737c89f2015-01-28 12:23:19 -08001158 // opts are, well, optional :)
Simon Hunt3ab20282015-02-26 20:32:19 -08001159 function initForce(_svg_, forceG, _uplink_, _dim_, opts) {
Simon Hunt1894d792015-02-04 17:09:20 -08001160 uplink = _uplink_;
Simon Hunt3a6eec02015-02-09 21:16:43 -08001161 dim = _dim_;
Simon Hunt3ab20282015-02-26 20:32:19 -08001162 svg = _svg_;
1163
1164 lu = network.lookup;
1165 rlk = network.revLinkToKey;
Simon Hunt3a6eec02015-02-09 21:16:43 -08001166
1167 $log.debug('initForce().. dim = ' + dim);
1168
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001169 tov.setApi(mkOverlayApi(), tss);
Simon Huntdc6adea2015-02-09 22:29:36 -08001170 tms.initModel(mkModelApi(uplink), dim);
Steven Burrowsf17f0ab2017-04-11 11:03:58 -07001171 td3.initD3(mkD3Api(), uplink.zoomer());
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001172 tss.initSelect(mkSelectApi());
1173 tts.initTraffic(mkTrafficApi());
Andrea Campanella732ea832017-02-06 09:25:59 -08001174 tpis.initProtectedIntents(mkTrafficApi());
Simon Huntc3c5b672015-02-20 11:32:13 -08001175 tos.initOblique(mkObliqueApi(uplink, fltr));
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001176 fltr.initFilter(mkFilterApi());
Simon Hunt9e2104c2015-02-26 10:48:59 -08001177 tls.initLink(mkLinkApi(svg, uplink), td3);
Simon Hunta11b4eb2015-01-28 16:20:50 -08001178
Simon Hunt737c89f2015-01-28 12:23:19 -08001179 settings = angular.extend({}, defaultSettings, opts);
1180
1181 linkG = forceG.append('g').attr('id', 'topo-links');
1182 linkLabelG = forceG.append('g').attr('id', 'topo-linkLabels');
Bri Prebilic Cole80401762015-07-16 11:36:18 -07001183 numLinkLblsG = forceG.append('g').attr('id', 'topo-numLinkLabels');
Simon Hunt737c89f2015-01-28 12:23:19 -08001184 nodeG = forceG.append('g').attr('id', 'topo-nodes');
Simon Hunt1a5301e2015-02-25 15:31:25 -08001185 portLabelG = forceG.append('g').attr('id', 'topo-portLabels');
Simon Hunt737c89f2015-01-28 12:23:19 -08001186
1187 link = linkG.selectAll('.link');
1188 linkLabel = linkLabelG.selectAll('.linkLabel');
1189 node = nodeG.selectAll('.node');
1190
1191 force = d3.layout.force()
Simon Hunt3a6eec02015-02-09 21:16:43 -08001192 .size(dim)
Simon Hunt737c89f2015-01-28 12:23:19 -08001193 .nodes(network.nodes)
1194 .links(network.links)
1195 .gravity(settings.gravity)
1196 .friction(settings.friction)
1197 .charge(settings.charge._def_)
1198 .linkDistance(settings.linkDistance._def_)
1199 .linkStrength(settings.linkStrength._def_)
1200 .on('tick', tick);
1201
1202 drag = sus.createDragBehavior(force,
Simon Hunt08f841d02015-02-10 14:39:20 -08001203 tss.selectObject, atDragEnd, dragEnabled, clickEnabled);
Simon Hunt737c89f2015-01-28 12:23:19 -08001204 }
1205
Simon Hunt3a6eec02015-02-09 21:16:43 -08001206 function newDim(_dim_) {
1207 dim = _dim_;
1208 force.size(dim);
1209 tms.newDim(dim);
Simon Hunt737c89f2015-01-28 12:23:19 -08001210 }
1211
Simon Hunt3a6eec02015-02-09 21:16:43 -08001212 function destroyForce() {
Simon Hunt3ab20282015-02-26 20:32:19 -08001213 force.stop();
1214
Simon Huntfb8ea1f2015-02-24 21:38:09 -08001215 tls.destroyLink();
Simon Hunt96f88c62015-02-19 17:57:25 -08001216 tos.destroyOblique();
Simon Huntf542d842015-02-11 16:20:33 -08001217 tts.destroyTraffic();
Andrea Campanella732ea832017-02-06 09:25:59 -08001218 tpis.destroyProtectedIntents();
Simon Huntf542d842015-02-11 16:20:33 -08001219 tss.destroySelect();
Simon Hunta4242de2015-02-24 17:11:55 -08001220 td3.destroyD3();
Simon Huntf542d842015-02-11 16:20:33 -08001221 tms.destroyModel();
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001222 // note: no need to destroy overlay service
Simon Huntf51bf462016-06-29 16:22:57 -07001223 ts.removeListener(updateLinksAndNodes);
Simon Hunt3ab20282015-02-26 20:32:19 -08001224
1225 // clean up the DOM
1226 svg.selectAll('g').remove();
1227 svg.selectAll('defs').remove();
1228
1229 // clean up internal state
1230 network.nodes = [];
1231 network.links = [];
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -07001232 network.linksByDevice = {};
Simon Hunt3ab20282015-02-26 20:32:19 -08001233 network.lookup = {};
1234 network.revLinkToKey = {};
1235
Bri Prebilic Cole80401762015-07-16 11:36:18 -07001236 linkNums = [];
1237
1238 linkG = linkLabelG = numLinkLblsG = nodeG = portLabelG = null;
Simon Hunt3ab20282015-02-26 20:32:19 -08001239 link = linkLabel = node = null;
1240 force = drag = null;
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -07001241
1242 // clean up $timeout promises
Simon Hunta17fa672015-08-19 18:42:22 -07001243 if (fTimer) {
1244 $timeout.cancel(fTimer);
1245 }
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -07001246 if (fNodesTimer) {
1247 $timeout.cancel(fNodesTimer);
1248 }
1249 if (fLinksTimer) {
1250 $timeout.cancel(fLinksTimer);
1251 }
Simon Hunt3a6eec02015-02-09 21:16:43 -08001252 }
1253
Simon Hunt737c89f2015-01-28 12:23:19 -08001254 return {
1255 initForce: initForce,
Simon Hunt3a6eec02015-02-09 21:16:43 -08001256 newDim: newDim,
1257 destroyForce: destroyForce,
Simon Huntac4c6f72015-02-03 19:50:53 -08001258
Simon Hunta4242de2015-02-24 17:11:55 -08001259 updateDeviceColors: td3.updateDeviceColors,
Simon Hunt5724fb42015-02-05 16:59:40 -08001260 toggleHosts: toggleHosts,
Simon Hunt9e2104c2015-02-26 10:48:59 -08001261 togglePorts: tls.togglePorts,
Simon Hunt5724fb42015-02-05 16:59:40 -08001262 toggleOffline: toggleOffline,
1263 cycleDeviceLabels: cycleDeviceLabels,
Simon Hunt10618f62017-06-15 19:30:52 -07001264 cycleHostLabels: cycleHostLabels,
Simon Hunt445e8152015-02-06 13:00:12 -08001265 unpin: unpin,
Simon Hunta142dd22015-02-12 22:07:51 -08001266 showMastership: showMastership,
Simon Hunt86b7c882015-04-02 23:06:08 -07001267 showBadLinks: showBadLinks,
Steven Burrowsf17f0ab2017-04-11 11:03:58 -07001268 setNodeScale: setNodeScale,
Simon Huntac4c6f72015-02-03 19:50:53 -08001269
Simon Huntfd7106c2016-02-09 15:05:26 -08001270 resetAllLocations: resetAllLocations,
Simon Huntac4c6f72015-02-03 19:50:53 -08001271 addDevice: addDevice,
Simon Hunt1894d792015-02-04 17:09:20 -08001272 updateDevice: updateDevice,
1273 removeDevice: removeDevice,
1274 addHost: addHost,
1275 updateHost: updateHost,
Simon Hunt95d56fd2015-11-12 11:06:44 -08001276 moveHost: moveHost,
Simon Hunt1894d792015-02-04 17:09:20 -08001277 removeHost: removeHost,
1278 addLink: addLink,
1279 updateLink: updateLink,
Simon Hunt4a6b54b2015-10-27 22:08:25 -07001280 removeLink: removeLink,
Steven Burrows1c2a9682017-07-14 16:52:46 +01001281 topoStartDone: topoStartDone,
Simon Hunte2d9dc72017-08-10 15:21:04 -07001282
1283 setLionBundle: setLionBundle,
Simon Hunt737c89f2015-01-28 12:23:19 -08001284 };
1285 }]);
1286}());