blob: b8f3a4854b23419043999008a7c27bb9b9e5ade8 [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 Hunte2d9dc72017-08-10 15:21:04 -0700494 var lionKey = b ? 'visible' : 'hidden';
495 return topoLion(lionKey);
Simon Hunt5724fb42015-02-05 16:59:40 -0800496 }
497
Simon Huntfcbde892015-04-16 12:05:28 -0700498 function toggleHosts(x) {
499 var kev = (x === 'keyev'),
500 on = kev ? !showHosts : !!x;
501
502 showHosts = on;
Simon Hunt5724fb42015-02-05 16:59:40 -0800503 updateHostVisibility();
Simon Hunte2d9dc72017-08-10 15:21:04 -0700504 flash.flash(topoLion('hosts') + ' ' + vis(on));
Simon Huntfcbde892015-04-16 12:05:28 -0700505 return on;
Simon Hunt5724fb42015-02-05 16:59:40 -0800506 }
507
Simon Huntfcbde892015-04-16 12:05:28 -0700508 function toggleOffline(x) {
509 var kev = (x === 'keyev'),
510 on = kev ? !showOffline : !!x;
511
512 showOffline = on;
Simon Hunt5724fb42015-02-05 16:59:40 -0800513 updateOfflineVisibility();
Simon Hunte2d9dc72017-08-10 15:21:04 -0700514 flash.flash(topoLion('fl_offline_devices') + ' ' + vis(on));
Simon Huntfcbde892015-04-16 12:05:28 -0700515 return on;
Simon Hunt5724fb42015-02-05 16:59:40 -0800516 }
517
518 function cycleDeviceLabels() {
Bri Prebilic Cole9cf1a8d2015-04-21 13:15:29 -0700519 flash.flash(td3.incDevLabIndex());
Simon Huntdc6adea2015-02-09 22:29:36 -0800520 tms.findDevices().forEach(function (d) {
Simon Hunta4242de2015-02-24 17:11:55 -0800521 td3.updateDeviceLabel(d);
Simon Hunt1c367112015-02-05 18:02:46 -0800522 });
Simon Hunt5724fb42015-02-05 16:59:40 -0800523 }
524
Simon Hunt10618f62017-06-15 19:30:52 -0700525 function cycleHostLabels() {
526 flash.flash(td3.incHostLabIndex());
527 tms.findHosts().forEach(function (d) {
528 td3.updateHostLabel(d);
529 });
530 }
531
Simon Hunt445e8152015-02-06 13:00:12 -0800532 function unpin() {
Simon Hunt08f841d02015-02-10 14:39:20 -0800533 var hov = tss.hovered();
534 if (hov) {
535 sendUpdateMeta(hov, true);
536 hov.fixed = false;
537 hov.el.classed('fixed', false);
Simon Hunt445e8152015-02-06 13:00:12 -0800538 fResume();
539 }
540 }
541
Simon Hunta142dd22015-02-12 22:07:51 -0800542 function showMastership(masterId) {
543 if (!masterId) {
544 restoreLayerState();
545 } else {
546 showMastershipFor(masterId);
547 }
548 }
549
550 function restoreLayerState() {
551 // NOTE: this level of indirection required, for when we have
552 // the layer filter functionality re-implemented
553 suppressLayers(false);
554 }
555
556 function showMastershipFor(id) {
557 suppressLayers(true);
558 node.each(function (n) {
559 if (n.master === id) {
Simon Hunt743a8492015-08-25 16:18:19 -0700560 n.el.classed('suppressedmax', false);
Simon Hunta142dd22015-02-12 22:07:51 -0800561 }
562 });
563 }
564
Simon Hunt743a8492015-08-25 16:18:19 -0700565 function supAmt(less) {
Steven Burrows1c2a9682017-07-14 16:52:46 +0100566 return less ? 'suppressed' : 'suppressedmax';
Simon Hunt743a8492015-08-25 16:18:19 -0700567 }
568
569 function suppressLayers(b, less) {
570 var cls = supAmt(less);
571 node.classed(cls, b);
572 link.classed(cls, b);
573 }
574
575 function unsuppressNode(id, less) {
576 var cls = supAmt(less);
577 node.each(function (n) {
578 if (n.id === id) {
579 n.el.classed(cls, false);
580 }
581 });
582 }
583
Simon Hunt94f7dae2015-08-26 17:40:59 -0700584 function unsuppressLink(key, less) {
Simon Hunt743a8492015-08-25 16:18:19 -0700585 var cls = supAmt(less);
586 link.each(function (n) {
Simon Hunt94f7dae2015-08-26 17:40:59 -0700587 if (n.key === key) {
Simon Hunt743a8492015-08-25 16:18:19 -0700588 n.el.classed(cls, false);
589 }
590 });
Simon Hunta142dd22015-02-12 22:07:51 -0800591 }
Simon Hunt445e8152015-02-06 13:00:12 -0800592
Simon Hunt86b7c882015-04-02 23:06:08 -0700593 function showBadLinks() {
594 var badLinks = tms.findBadLinks();
Simon Hunte2d9dc72017-08-10 15:21:04 -0700595 flash.flash(topoLion('fl_bad_links') + ': ' + badLinks.length);
Simon Hunt86b7c882015-04-02 23:06:08 -0700596 $log.debug('Bad Link List (' + badLinks.length + '):');
597 badLinks.forEach(function (d) {
598 $log.debug('bad link: (' + d.bad + ') ' + d.key, d);
599 if (d.el) {
600 d.el.attr('stroke-width', linkScale(2.8))
601 .attr('stroke', 'red');
602 }
603 });
604 // back to normal after 2 seconds...
605 $timeout(updateLinks, 2000);
606 }
607
Steven Burrowsf17f0ab2017-04-11 11:03:58 -0700608 function deviceScale() {
609 var scale = uplink.zoomer().scale(),
610 dim = devIconDim,
611 multiplier = 1;
612
613 if (dim * scale < devIconDimMin) {
614 multiplier = devIconDimMin / (dim * scale);
615 } else if (dim * scale > devIconDimMax) {
616 multiplier = devIconDimMax / (dim * scale);
617 }
618
619 return multiplier;
620 }
621
622 function linkWidthScale(scale) {
623 var scale = uplink.zoomer().scale();
624 return linkScale(widthRatio) / scale;
625 }
626
627 function portLabelScale(scale) {
628 var scale = uplink.zoomer().scale();
629 return portLabelDim / (portLabelDim * scale);
630 }
631
632 function setNodeScale(scale) {
633 // Scale the network nodes
634 _.each(network.nodes, function (node) {
635 if (node.class === 'host') {
636 node.el.selectAll('g').style('transform', 'scale(' + deviceScale(scale) + ')');
637 node.el.selectAll('text').style('transform', 'scale(' + deviceScale(scale) + ')');
638 return;
639 }
640 node.el.selectAll('*')
641 .style('transform', 'scale(' + deviceScale(scale) + ')');
642 });
643
644 // Scale the network links
645 _.each(network.links, function (link) {
646 link.el.style('stroke-width', linkWidthScale(scale) + 'px');
647 });
648
649 d3.select('#topo-portLabels')
650 .selectAll('.portLabel')
651 .selectAll('*')
652 .style('transform', 'scale(' + portLabelScale(scale) + ')');
653 }
654
Simon Huntfd7106c2016-02-09 15:05:26 -0800655 function resetAllLocations() {
656 tms.resetAllLocations();
657 updateNodes();
658 tick(); // force nodes to be redrawn in their new locations
Simon Hunte2d9dc72017-08-10 15:21:04 -0700659 flash.flash(topoLion('fl_reset_node_locations'));
Simon Huntfd7106c2016-02-09 15:05:26 -0800660 }
661
Simon Hunt5724fb42015-02-05 16:59:40 -0800662 // ==========================================
663
Simon Huntac4c6f72015-02-03 19:50:53 -0800664 function updateNodes() {
Thomas Vachuska1a989c12015-06-09 18:29:22 -0700665 if (fNodesTimer) {
666 $timeout.cancel(fNodesTimer);
667 }
668 fNodesTimer = $timeout(_updateNodes, 150);
669 }
670
Simon Hunta17fa672015-08-19 18:42:22 -0700671 // IMPLEMENTATION NOTE: _updateNodes() should NOT stop, start, or resume
672 // the force layout; that needs to be determined and implemented elsewhere
Thomas Vachuska1a989c12015-06-09 18:29:22 -0700673 function _updateNodes() {
Simon Hunt1894d792015-02-04 17:09:20 -0800674 // select all the nodes in the layout:
Simon Huntac4c6f72015-02-03 19:50:53 -0800675 node = nodeG.selectAll('.node')
676 .data(network.nodes, function (d) { return d.id; });
677
Simon Hunt1894d792015-02-04 17:09:20 -0800678 // operate on existing nodes:
Simon Hunta4242de2015-02-24 17:11:55 -0800679 node.filter('.device').each(td3.deviceExisting);
680 node.filter('.host').each(td3.hostExisting);
Simon Huntac4c6f72015-02-03 19:50:53 -0800681
682 // operate on entering nodes:
683 var entering = node.enter()
684 .append('g')
685 .attr({
686 id: function (d) { return sus.safeId(d.id); },
687 class: mkSvgClass,
Simon Hunta17fa672015-08-19 18:42:22 -0700688 transform: function (d) {
689 // Need to guard against NaN here ??
690 return sus.translate(d.x, d.y);
691 },
Steven Burrows1c2a9682017-07-14 16:52:46 +0100692 opacity: 0,
Simon Huntac4c6f72015-02-03 19:50:53 -0800693 })
694 .call(drag)
Simon Hunt08f841d02015-02-10 14:39:20 -0800695 .on('mouseover', tss.nodeMouseOver)
696 .on('mouseout', tss.nodeMouseOut)
Simon Huntac4c6f72015-02-03 19:50:53 -0800697 .transition()
698 .attr('opacity', 1);
699
Simon Hunt1894d792015-02-04 17:09:20 -0800700 // augment entering nodes:
Simon Hunta4242de2015-02-24 17:11:55 -0800701 entering.filter('.device').each(td3.deviceEnter);
702 entering.filter('.host').each(td3.hostEnter);
Simon Huntac4c6f72015-02-03 19:50:53 -0800703
Simon Hunt51056592015-02-03 21:48:07 -0800704 // operate on both existing and new nodes:
Simon Hunta4242de2015-02-24 17:11:55 -0800705 td3.updateDeviceColors();
Simon Huntac4c6f72015-02-03 19:50:53 -0800706
707 // operate on exiting nodes:
708 // Note that the node is removed after 2 seconds.
709 // Sub element animations should be shorter than 2 seconds.
710 var exiting = node.exit()
711 .transition()
712 .duration(2000)
713 .style('opacity', 0)
714 .remove();
715
Simon Hunt1894d792015-02-04 17:09:20 -0800716 // exiting node specifics:
Simon Hunta4242de2015-02-24 17:11:55 -0800717 exiting.filter('.host').each(td3.hostExit);
718 exiting.filter('.device').each(td3.deviceExit);
Simon Huntac4c6f72015-02-03 19:50:53 -0800719 }
720
Simon Hunt51056592015-02-03 21:48:07 -0800721 // ==========================
Simon Hunt1894d792015-02-04 17:09:20 -0800722
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700723 function getDefaultPos(link) {
724 return {
725 x1: link.source.x,
726 y1: link.source.y,
727 x2: link.target.x,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100728 y2: link.target.y,
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700729 };
730 }
731
732 // returns amount of adjustment along the normal for given link
733 function amt(numLinks, linkIdx) {
734 var gap = 6;
735 return (linkIdx - ((numLinks - 1) / 2)) * gap;
736 }
737
738 function calcMovement(d, amt, flipped) {
739 var pos = getDefaultPos(d),
740 mult = flipped ? -amt : amt,
741 dx = pos.x2 - pos.x1,
742 dy = pos.y2 - pos.y1,
743 length = Math.sqrt((dx * dx) + (dy * dy));
744
745 return {
746 x1: pos.x1 + (mult * dy / length),
747 y1: pos.y1 + (mult * -dx / length),
748 x2: pos.x2 + (mult * dy / length),
Steven Burrows1c2a9682017-07-14 16:52:46 +0100749 y2: pos.y2 + (mult * -dx / length),
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700750 };
751 }
752
753 function calcPosition() {
754 var lines = this,
755 linkSrcId;
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700756 linkNums = [];
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700757 lines.each(function (d) {
758 if (d.type() === 'hostLink') {
759 d.position = getDefaultPos(d);
760 }
761 });
762
763 function normalizeLinkSrc(link) {
764 // ensure source device is consistent across set of links
765 // temporary measure until link modeling is refactored
766 if (!linkSrcId) {
767 linkSrcId = link.source.id;
768 return false;
769 }
770
771 return link.source.id !== linkSrcId;
772 }
773
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700774 angular.forEach(network.linksByDevice, function (linkArr, key) {
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700775 var numLinks = linkArr.length,
776 link;
777
778 if (numLinks === 1) {
779 link = linkArr[0];
780 link.position = getDefaultPos(link);
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700781 link.position.multiLink = false;
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700782 } else if (numLinks >= 5) {
783 // this code is inefficient, in the future the way links
784 // are modeled will be changed
785 angular.forEach(linkArr, function (link) {
786 link.position = getDefaultPos(link);
787 link.position.multiLink = true;
788 });
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700789 linkNums.push({
790 id: key,
791 num: numLinks,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100792 linkCoords: linkArr[0].position,
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700793 });
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700794 } else {
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700795 linkSrcId = null;
796 angular.forEach(linkArr, function (link, index) {
797 var offsetAmt = amt(numLinks, index),
798 needToFlip = normalizeLinkSrc(link);
799 link.position = calcMovement(link, offsetAmt, needToFlip);
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700800 link.position.multiLink = false;
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700801 });
802 }
803 });
804 }
805
Simon Hunt1894d792015-02-04 17:09:20 -0800806 function updateLinks() {
Thomas Vachuska1a989c12015-06-09 18:29:22 -0700807 if (fLinksTimer) {
808 $timeout.cancel(fLinksTimer);
809 }
810 fLinksTimer = $timeout(_updateLinks, 150);
811 }
812
Simon Hunta17fa672015-08-19 18:42:22 -0700813 // IMPLEMENTATION NOTE: _updateLinks() should NOT stop, start, or resume
814 // the force layout; that needs to be determined and implemented elsewhere
Thomas Vachuska1a989c12015-06-09 18:29:22 -0700815 function _updateLinks() {
Simon Hunt1894d792015-02-04 17:09:20 -0800816 var th = ts.theme();
817
818 link = linkG.selectAll('.link')
819 .data(network.links, function (d) { return d.key; });
820
821 // operate on existing links:
Simon Huntd5264122015-02-25 10:17:43 -0800822 link.each(function (d) {
823 // this is supposed to be an existing link, but we have observed
824 // occasions (where links are deleted and added rapidly?) where
825 // the DOM element has not been defined. So protect against that...
826 if (d.el) {
827 restyleLinkElement(d, true);
828 }
829 });
Simon Hunt1894d792015-02-04 17:09:20 -0800830
831 // operate on entering links:
832 var entering = link.enter()
833 .append('line')
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700834 .call(calcPosition)
Simon Hunt1894d792015-02-04 17:09:20 -0800835 .attr({
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700836 x1: function (d) { return d.position.x1; },
837 y1: function (d) { return d.position.y1; },
838 x2: function (d) { return d.position.x2; },
839 y2: function (d) { return d.position.y2; },
Simon Hunt1894d792015-02-04 17:09:20 -0800840 stroke: linkConfig[th].inColor,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100841 'stroke-width': linkConfig.inWidth,
Simon Hunt1894d792015-02-04 17:09:20 -0800842 });
843
844 // augment links
Simon Hunta4242de2015-02-24 17:11:55 -0800845 entering.each(td3.linkEntering);
Simon Hunt1894d792015-02-04 17:09:20 -0800846
847 // operate on both existing and new links:
Steven Burrows1c2a9682017-07-14 16:52:46 +0100848 // link.each(...)
Simon Hunt1894d792015-02-04 17:09:20 -0800849
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700850 // add labels for how many links are in a thick line
851 td3.applyNumLinkLabels(linkNums, numLinkLblsG);
852
Simon Hunt1894d792015-02-04 17:09:20 -0800853 // apply or remove labels
Simon Hunta4242de2015-02-24 17:11:55 -0800854 td3.applyLinkLabels();
Simon Hunt1894d792015-02-04 17:09:20 -0800855
856 // operate on exiting links:
857 link.exit()
858 .attr('stroke-dasharray', '3 3')
Simon Hunt5724fb42015-02-05 16:59:40 -0800859 .attr('stroke', linkConfig[th].outColor)
Simon Hunt1894d792015-02-04 17:09:20 -0800860 .style('opacity', 0.5)
861 .transition()
862 .duration(1500)
863 .attr({
864 'stroke-dasharray': '3 12',
Steven Burrows1c2a9682017-07-14 16:52:46 +0100865 'stroke-width': linkConfig.outWidth,
Simon Hunt1894d792015-02-04 17:09:20 -0800866 })
867 .style('opacity', 0.0)
868 .remove();
Simon Hunt1894d792015-02-04 17:09:20 -0800869 }
870
Simon Huntac4c6f72015-02-03 19:50:53 -0800871
872 // ==========================
Simon Hunt737c89f2015-01-28 12:23:19 -0800873 // force layout tick function
Simon Hunt737c89f2015-01-28 12:23:19 -0800874
Simon Hunt5724fb42015-02-05 16:59:40 -0800875 function fResume() {
Simon Huntc3c5b672015-02-20 11:32:13 -0800876 if (!tos.isOblique()) {
Simon Hunt5724fb42015-02-05 16:59:40 -0800877 force.resume();
878 }
879 }
880
881 function fStart() {
Simon Huntc3c5b672015-02-20 11:32:13 -0800882 if (!tos.isOblique()) {
Simon Hunta17fa672015-08-19 18:42:22 -0700883 if (fTimer) {
884 $timeout.cancel(fTimer);
885 }
886 fTimer = $timeout(function () {
Steven Burrows1c2a9682017-07-14 16:52:46 +0100887 $log.debug('Starting force-layout');
Simon Hunta17fa672015-08-19 18:42:22 -0700888 force.start();
889 }, 200);
Simon Hunt5724fb42015-02-05 16:59:40 -0800890 }
891 }
892
893 var tickStuff = {
894 nodeAttr: {
Simon Hunta17fa672015-08-19 18:42:22 -0700895 transform: function (d) {
896 var dx = isNaN(d.x) ? 0 : d.x,
897 dy = isNaN(d.y) ? 0 : d.y;
898 return sus.translate(dx, dy);
Steven Burrows1c2a9682017-07-14 16:52:46 +0100899 },
Simon Hunt5724fb42015-02-05 16:59:40 -0800900 },
901 linkAttr: {
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700902 x1: function (d) { return d.position.x1; },
903 y1: function (d) { return d.position.y1; },
904 x2: function (d) { return d.position.x2; },
Steven Burrows1c2a9682017-07-14 16:52:46 +0100905 y2: function (d) { return d.position.y2; },
Simon Hunt5724fb42015-02-05 16:59:40 -0800906 },
907 linkLabelAttr: {
908 transform: function (d) {
Simon Huntdc6adea2015-02-09 22:29:36 -0800909 var lnk = tms.findLinkById(d.key);
Simon Hunt5724fb42015-02-05 16:59:40 -0800910 if (lnk) {
Carmelo Casconed01eda62016-08-02 10:19:15 -0700911 return td3.transformLabel(lnk.position, d.key);
Simon Hunt5724fb42015-02-05 16:59:40 -0800912 }
Steven Burrows1c2a9682017-07-14 16:52:46 +0100913 },
914 },
Simon Hunt5724fb42015-02-05 16:59:40 -0800915 };
916
917 function tick() {
Simon Hunt3ab20282015-02-26 20:32:19 -0800918 // guard against null (which can happen when our view pages out)...
Bri Prebilic Cole8d003782015-07-31 15:33:06 -0700919 if (node && node.size()) {
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700920 node.attr(tickStuff.nodeAttr);
921 }
Bri Prebilic Cole8d003782015-07-31 15:33:06 -0700922 if (link && link.size()) {
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700923 link.call(calcPosition)
924 .attr(tickStuff.linkAttr);
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700925 td3.applyNumLinkLabels(linkNums, numLinkLblsG);
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700926 }
Bri Prebilic Cole8d003782015-07-31 15:33:06 -0700927 if (linkLabel && linkLabel.size()) {
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700928 linkLabel.attr(tickStuff.linkLabelAttr);
929 }
Simon Hunt737c89f2015-01-28 12:23:19 -0800930 }
931
932
Simon Huntac4c6f72015-02-03 19:50:53 -0800933 // ==========================
934 // === MOUSE GESTURE HANDLERS
935
Simon Hunt205099e2015-02-07 13:12:01 -0800936 function zoomingOrPanning(ev) {
937 return ev.metaKey || ev.altKey;
Simon Hunt445e8152015-02-06 13:00:12 -0800938 }
939
940 function atDragEnd(d) {
941 // once we've finished moving, pin the node in position
942 d.fixed = true;
943 d3.select(this).classed('fixed', true);
944 sendUpdateMeta(d);
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700945 tss.clickConsumed(true);
Simon Hunt445e8152015-02-06 13:00:12 -0800946 }
947
948 // predicate that indicates when dragging is active
949 function dragEnabled() {
950 var ev = d3.event.sourceEvent;
951 // nodeLock means we aren't allowing nodes to be dragged...
Simon Hunt205099e2015-02-07 13:12:01 -0800952 return !nodeLock && !zoomingOrPanning(ev);
Simon Hunt445e8152015-02-06 13:00:12 -0800953 }
954
955 // predicate that indicates when clicking is active
956 function clickEnabled() {
957 return true;
958 }
Simon Hunt737c89f2015-01-28 12:23:19 -0800959
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700960 // =============================================
961 // function entry points for overlay module
Simon Huntf542d842015-02-11 16:20:33 -0800962
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700963 // TODO: find an automatic way of tracking via the "showHighlights" events
Simon Hunte50829c2015-06-09 08:39:28 -0700964 var allTrafficClasses = 'primary secondary optical animated ' +
Simon Hunt21281fd2017-03-30 22:28:28 -0700965 'port-traffic-green port-traffic-yellow port-traffic-orange ' +
966 'port-traffic-red';
Simon Huntf542d842015-02-11 16:20:33 -0800967
968 function clearLinkTrafficStyle() {
969 link.style('stroke-width', null)
970 .classed(allTrafficClasses, false);
971 }
972
973 function removeLinkLabels() {
974 network.links.forEach(function (d) {
975 d.label = '';
976 });
977 }
Simon Hunt737c89f2015-01-28 12:23:19 -0800978
Simon Hunte9343f32015-10-21 18:07:46 -0700979 function clearNodeDeco() {
980 node.selectAll('g.badge').remove();
981 }
982
983 function removeNodeBadges() {
984 network.nodes.forEach(function (d) {
985 d.badge = null;
986 });
987 }
988
Simon Hunta4242de2015-02-24 17:11:55 -0800989 function updateLinkLabelModel() {
990 // create the backing data for showing labels..
991 var data = [];
992 link.each(function (d) {
993 if (d.label) {
994 data.push({
995 id: 'lab-' + d.key,
996 key: d.key,
997 label: d.label,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100998 ldata: d,
Simon Hunta4242de2015-02-24 17:11:55 -0800999 });
1000 }
1001 });
1002
1003 linkLabel = linkLabelG.selectAll('.linkLabel')
1004 .data(data, function (d) { return d.id; });
1005 }
1006
Simon Hunt737c89f2015-01-28 12:23:19 -08001007 // ==========================
Simon Huntac4c6f72015-02-03 19:50:53 -08001008 // Module definition
Simon Hunt737c89f2015-01-28 12:23:19 -08001009
Simon Huntdc6adea2015-02-09 22:29:36 -08001010 function mkModelApi(uplink) {
1011 return {
1012 projection: uplink.projection,
1013 network: network,
1014 restyleLinkElement: restyleLinkElement,
Steven Burrows1c2a9682017-07-14 16:52:46 +01001015 removeLinkElement: removeLinkElement,
Simon Huntdc6adea2015-02-09 22:29:36 -08001016 };
1017 }
1018
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001019 function mkD3Api() {
Simon Hunta4242de2015-02-24 17:11:55 -08001020 return {
1021 node: function () { return node; },
1022 link: function () { return link; },
1023 linkLabel: function () { return linkLabel; },
1024 instVisible: function () { return tis.isVisible(); },
1025 posNode: tms.positionNode,
1026 showHosts: function () { return showHosts; },
1027 restyleLinkElement: restyleLinkElement,
Bri Prebilic Cole80401762015-07-16 11:36:18 -07001028 updateLinkLabelModel: updateLinkLabelModel,
Steven Burrowsf17f0ab2017-04-11 11:03:58 -07001029 linkConfig: function () { return linkConfig; },
1030 deviceScale: deviceScale,
Steven Burrows1c2a9682017-07-14 16:52:46 +01001031 linkWidthScale: linkWidthScale,
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -07001032 };
Simon Hunta4242de2015-02-24 17:11:55 -08001033 }
1034
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001035 function mkSelectApi() {
Simon Hunt08f841d02015-02-10 14:39:20 -08001036 return {
1037 node: function () { return node; },
1038 zoomingOrPanning: zoomingOrPanning,
Simon Hunt0c6b2d32015-03-26 17:46:29 -07001039 updateDeviceColors: td3.updateDeviceColors,
Steven Burrows1c2a9682017-07-14 16:52:46 +01001040 deselectAllLinks: tls.deselectAllLinks,
Simon Hunt08f841d02015-02-10 14:39:20 -08001041 };
1042 }
1043
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001044 function mkTrafficApi() {
1045 return {
1046 hovered: tss.hovered,
1047 somethingSelected: tss.somethingSelected,
Steven Burrows1c2a9682017-07-14 16:52:46 +01001048 selectOrder: tss.selectOrder,
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001049 };
1050 }
1051
1052 function mkOverlayApi() {
Simon Huntf542d842015-02-11 16:20:33 -08001053 return {
Simon Hunte9343f32015-10-21 18:07:46 -07001054 clearNodeDeco: clearNodeDeco,
1055 removeNodeBadges: removeNodeBadges,
Simon Huntf542d842015-02-11 16:20:33 -08001056 clearLinkTrafficStyle: clearLinkTrafficStyle,
1057 removeLinkLabels: removeLinkLabels,
Simon Hunt743a8492015-08-25 16:18:19 -07001058 findLinkById: tms.findLinkById,
Simon Hunt94f7dae2015-08-26 17:40:59 -07001059 findNodeById: nodeById,
Simon Huntf542d842015-02-11 16:20:33 -08001060 updateLinks: updateLinks,
Simon Hunt743a8492015-08-25 16:18:19 -07001061 updateNodes: updateNodes,
1062 supLayers: suppressLayers,
1063 unsupNode: unsuppressNode,
Steven Burrows1c2a9682017-07-14 16:52:46 +01001064 unsupLink: unsuppressLink,
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -07001065 };
Simon Huntf542d842015-02-11 16:20:33 -08001066 }
1067
Simon Huntc3c5b672015-02-20 11:32:13 -08001068 function mkObliqueApi(uplink, fltr) {
Simon Hunt96f88c62015-02-19 17:57:25 -08001069 return {
Steven Burrows1c2a9682017-07-14 16:52:46 +01001070 force: function () { return force; },
Simon Huntc3c5b672015-02-20 11:32:13 -08001071 zoomLayer: uplink.zoomLayer,
Steven Burrows1c2a9682017-07-14 16:52:46 +01001072 nodeGBBox: function () { return nodeG.node().getBBox(); },
Simon Hunt96f88c62015-02-19 17:57:25 -08001073 node: function () { return node; },
Simon Huntc3c5b672015-02-20 11:32:13 -08001074 link: function () { return link; },
1075 linkLabel: function () { return linkLabel; },
1076 nodes: function () { return network.nodes; },
1077 tickStuff: tickStuff,
1078 nodeLock: function (b) {
1079 var old = nodeLock;
1080 nodeLock = b;
1081 return old;
1082 },
1083 opacifyMap: uplink.opacifyMap,
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -07001084 inLayer: fltr.inLayer,
Bri Prebilic Cole80401762015-07-16 11:36:18 -07001085 calcLinkPos: calcPosition,
1086 applyNumLinkLabels: function () {
1087 td3.applyNumLinkLabels(linkNums, numLinkLblsG);
Steven Burrows1c2a9682017-07-14 16:52:46 +01001088 },
Simon Hunt96f88c62015-02-19 17:57:25 -08001089 };
1090 }
1091
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001092 function mkFilterApi() {
Simon Hunteb0fa052015-02-17 19:20:28 -08001093 return {
1094 node: function () { return node; },
Steven Burrows1c2a9682017-07-14 16:52:46 +01001095 link: function () { return link; },
Simon Hunteb0fa052015-02-17 19:20:28 -08001096 };
1097 }
1098
Simon Hunt9e2104c2015-02-26 10:48:59 -08001099 function mkLinkApi(svg, uplink) {
Simon Huntfb8ea1f2015-02-24 21:38:09 -08001100 return {
1101 svg: svg,
Simon Huntfb8ea1f2015-02-24 21:38:09 -08001102 zoomer: uplink.zoomer(),
1103 network: network,
Simon Hunt1a5301e2015-02-25 15:31:25 -08001104 portLabelG: function () { return portLabelG; },
Steven Burrows1c2a9682017-07-14 16:52:46 +01001105 showHosts: function () { return showHosts; },
Simon Huntfb8ea1f2015-02-24 21:38:09 -08001106 };
1107 }
1108
Simon Huntf51bf462016-06-29 16:22:57 -07001109 function updateLinksAndNodes() {
1110 updateLinks();
1111 updateNodes();
1112 }
Steven Burrowsec1f45c2016-08-08 16:14:41 +01001113
Simon Hunte2d9dc72017-08-10 15:21:04 -07001114 // invoked after the localization bundle has been received from the server
1115 function setLionBundle(bundle) {
1116 topoLion = bundle;
1117 td3.setLionBundle(bundle);
1118 fltr.setLionBundle(bundle);
1119 tls.setLionBundle(bundle);
1120 }
1121
Simon Hunt737c89f2015-01-28 12:23:19 -08001122 angular.module('ovTopo')
1123 .factory('TopoForceService',
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -07001124 ['$log', '$timeout', 'FnService', 'SvgUtilService',
Simon Hunt86b7c882015-04-02 23:06:08 -07001125 'ThemeService', 'FlashService', 'WebSocketService',
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001126 'TopoOverlayService', 'TopoInstService', 'TopoModelService',
Simon Hunta4242de2015-02-24 17:11:55 -08001127 'TopoD3Service', 'TopoSelectService', 'TopoTrafficService',
Simon Huntfb8ea1f2015-02-24 21:38:09 -08001128 'TopoObliqueService', 'TopoFilterService', 'TopoLinkService',
Andrea Campanella732ea832017-02-06 09:25:59 -08001129 'TopoProtectedIntentsService',
Simon Hunt737c89f2015-01-28 12:23:19 -08001130
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001131 function (_$log_, _$timeout_, _fs_, _sus_, _ts_, _flash_, _wss_, _tov_,
Andrea Campanella732ea832017-02-06 09:25:59 -08001132 _tis_, _tms_, _td3_, _tss_, _tts_, _tos_, _fltr_, _tls_, _tpis_) {
Simon Hunt737c89f2015-01-28 12:23:19 -08001133 $log = _$log_;
Simon Hunt86b7c882015-04-02 23:06:08 -07001134 $timeout = _$timeout_;
Simon Hunt1894d792015-02-04 17:09:20 -08001135 fs = _fs_;
Simon Hunt737c89f2015-01-28 12:23:19 -08001136 sus = _sus_;
Simon Huntac4c6f72015-02-03 19:50:53 -08001137 ts = _ts_;
Simon Hunt5724fb42015-02-05 16:59:40 -08001138 flash = _flash_;
Simon Hunt237676b52015-03-10 19:04:26 -07001139 wss = _wss_;
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001140 tov = _tov_;
Simon Huntac4c6f72015-02-03 19:50:53 -08001141 tis = _tis_;
Simon Hunt3a6eec02015-02-09 21:16:43 -08001142 tms = _tms_;
Simon Hunta4242de2015-02-24 17:11:55 -08001143 td3 = _td3_;
Simon Hunt08f841d02015-02-10 14:39:20 -08001144 tss = _tss_;
Simon Huntf542d842015-02-11 16:20:33 -08001145 tts = _tts_;
Simon Hunt96f88c62015-02-19 17:57:25 -08001146 tos = _tos_;
Simon Hunteb0fa052015-02-17 19:20:28 -08001147 fltr = _fltr_;
Simon Huntfb8ea1f2015-02-24 21:38:09 -08001148 tls = _tls_;
Andrea Campanella732ea832017-02-06 09:25:59 -08001149 tpis = _tpis_;
Simon Hunt737c89f2015-01-28 12:23:19 -08001150
Simon Huntf51bf462016-06-29 16:22:57 -07001151 ts.addListener(updateLinksAndNodes);
Simon Hunta142dd22015-02-12 22:07:51 -08001152
Simon Hunt737c89f2015-01-28 12:23:19 -08001153 // forceG is the SVG group to display the force layout in
Simon Huntdc6adea2015-02-09 22:29:36 -08001154 // uplink is the api from the main topo source file
Simon Hunt3a6eec02015-02-09 21:16:43 -08001155 // dim is the initial dimensions of the SVG as [w,h]
Simon Hunt737c89f2015-01-28 12:23:19 -08001156 // opts are, well, optional :)
Simon Hunt3ab20282015-02-26 20:32:19 -08001157 function initForce(_svg_, forceG, _uplink_, _dim_, opts) {
Simon Hunt1894d792015-02-04 17:09:20 -08001158 uplink = _uplink_;
Simon Hunt3a6eec02015-02-09 21:16:43 -08001159 dim = _dim_;
Simon Hunt3ab20282015-02-26 20:32:19 -08001160 svg = _svg_;
1161
1162 lu = network.lookup;
1163 rlk = network.revLinkToKey;
Simon Hunt3a6eec02015-02-09 21:16:43 -08001164
1165 $log.debug('initForce().. dim = ' + dim);
1166
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001167 tov.setApi(mkOverlayApi(), tss);
Simon Huntdc6adea2015-02-09 22:29:36 -08001168 tms.initModel(mkModelApi(uplink), dim);
Steven Burrowsf17f0ab2017-04-11 11:03:58 -07001169 td3.initD3(mkD3Api(), uplink.zoomer());
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001170 tss.initSelect(mkSelectApi());
1171 tts.initTraffic(mkTrafficApi());
Andrea Campanella732ea832017-02-06 09:25:59 -08001172 tpis.initProtectedIntents(mkTrafficApi());
Simon Huntc3c5b672015-02-20 11:32:13 -08001173 tos.initOblique(mkObliqueApi(uplink, fltr));
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001174 fltr.initFilter(mkFilterApi());
Simon Hunt9e2104c2015-02-26 10:48:59 -08001175 tls.initLink(mkLinkApi(svg, uplink), td3);
Simon Hunta11b4eb2015-01-28 16:20:50 -08001176
Simon Hunt737c89f2015-01-28 12:23:19 -08001177 settings = angular.extend({}, defaultSettings, opts);
1178
1179 linkG = forceG.append('g').attr('id', 'topo-links');
1180 linkLabelG = forceG.append('g').attr('id', 'topo-linkLabels');
Bri Prebilic Cole80401762015-07-16 11:36:18 -07001181 numLinkLblsG = forceG.append('g').attr('id', 'topo-numLinkLabels');
Simon Hunt737c89f2015-01-28 12:23:19 -08001182 nodeG = forceG.append('g').attr('id', 'topo-nodes');
Simon Hunt1a5301e2015-02-25 15:31:25 -08001183 portLabelG = forceG.append('g').attr('id', 'topo-portLabels');
Simon Hunt737c89f2015-01-28 12:23:19 -08001184
1185 link = linkG.selectAll('.link');
1186 linkLabel = linkLabelG.selectAll('.linkLabel');
1187 node = nodeG.selectAll('.node');
1188
1189 force = d3.layout.force()
Simon Hunt3a6eec02015-02-09 21:16:43 -08001190 .size(dim)
Simon Hunt737c89f2015-01-28 12:23:19 -08001191 .nodes(network.nodes)
1192 .links(network.links)
1193 .gravity(settings.gravity)
1194 .friction(settings.friction)
1195 .charge(settings.charge._def_)
1196 .linkDistance(settings.linkDistance._def_)
1197 .linkStrength(settings.linkStrength._def_)
1198 .on('tick', tick);
1199
1200 drag = sus.createDragBehavior(force,
Simon Hunt08f841d02015-02-10 14:39:20 -08001201 tss.selectObject, atDragEnd, dragEnabled, clickEnabled);
Simon Hunt737c89f2015-01-28 12:23:19 -08001202 }
1203
Simon Hunt3a6eec02015-02-09 21:16:43 -08001204 function newDim(_dim_) {
1205 dim = _dim_;
1206 force.size(dim);
1207 tms.newDim(dim);
Simon Hunt737c89f2015-01-28 12:23:19 -08001208 }
1209
Simon Hunt3a6eec02015-02-09 21:16:43 -08001210 function destroyForce() {
Simon Hunt3ab20282015-02-26 20:32:19 -08001211 force.stop();
1212
Simon Huntfb8ea1f2015-02-24 21:38:09 -08001213 tls.destroyLink();
Simon Hunt96f88c62015-02-19 17:57:25 -08001214 tos.destroyOblique();
Simon Huntf542d842015-02-11 16:20:33 -08001215 tts.destroyTraffic();
Andrea Campanella732ea832017-02-06 09:25:59 -08001216 tpis.destroyProtectedIntents();
Simon Huntf542d842015-02-11 16:20:33 -08001217 tss.destroySelect();
Simon Hunta4242de2015-02-24 17:11:55 -08001218 td3.destroyD3();
Simon Huntf542d842015-02-11 16:20:33 -08001219 tms.destroyModel();
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001220 // note: no need to destroy overlay service
Simon Huntf51bf462016-06-29 16:22:57 -07001221 ts.removeListener(updateLinksAndNodes);
Simon Hunt3ab20282015-02-26 20:32:19 -08001222
1223 // clean up the DOM
1224 svg.selectAll('g').remove();
1225 svg.selectAll('defs').remove();
1226
1227 // clean up internal state
1228 network.nodes = [];
1229 network.links = [];
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -07001230 network.linksByDevice = {};
Simon Hunt3ab20282015-02-26 20:32:19 -08001231 network.lookup = {};
1232 network.revLinkToKey = {};
1233
Bri Prebilic Cole80401762015-07-16 11:36:18 -07001234 linkNums = [];
1235
1236 linkG = linkLabelG = numLinkLblsG = nodeG = portLabelG = null;
Simon Hunt3ab20282015-02-26 20:32:19 -08001237 link = linkLabel = node = null;
1238 force = drag = null;
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -07001239
1240 // clean up $timeout promises
Simon Hunta17fa672015-08-19 18:42:22 -07001241 if (fTimer) {
1242 $timeout.cancel(fTimer);
1243 }
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -07001244 if (fNodesTimer) {
1245 $timeout.cancel(fNodesTimer);
1246 }
1247 if (fLinksTimer) {
1248 $timeout.cancel(fLinksTimer);
1249 }
Simon Hunt3a6eec02015-02-09 21:16:43 -08001250 }
1251
Simon Hunt737c89f2015-01-28 12:23:19 -08001252 return {
1253 initForce: initForce,
Simon Hunt3a6eec02015-02-09 21:16:43 -08001254 newDim: newDim,
1255 destroyForce: destroyForce,
Simon Huntac4c6f72015-02-03 19:50:53 -08001256
Simon Hunta4242de2015-02-24 17:11:55 -08001257 updateDeviceColors: td3.updateDeviceColors,
Simon Hunt5724fb42015-02-05 16:59:40 -08001258 toggleHosts: toggleHosts,
Simon Hunt9e2104c2015-02-26 10:48:59 -08001259 togglePorts: tls.togglePorts,
Simon Hunt5724fb42015-02-05 16:59:40 -08001260 toggleOffline: toggleOffline,
1261 cycleDeviceLabels: cycleDeviceLabels,
Simon Hunt10618f62017-06-15 19:30:52 -07001262 cycleHostLabels: cycleHostLabels,
Simon Hunt445e8152015-02-06 13:00:12 -08001263 unpin: unpin,
Simon Hunta142dd22015-02-12 22:07:51 -08001264 showMastership: showMastership,
Simon Hunt86b7c882015-04-02 23:06:08 -07001265 showBadLinks: showBadLinks,
Steven Burrowsf17f0ab2017-04-11 11:03:58 -07001266 setNodeScale: setNodeScale,
Simon Huntac4c6f72015-02-03 19:50:53 -08001267
Simon Huntfd7106c2016-02-09 15:05:26 -08001268 resetAllLocations: resetAllLocations,
Simon Huntac4c6f72015-02-03 19:50:53 -08001269 addDevice: addDevice,
Simon Hunt1894d792015-02-04 17:09:20 -08001270 updateDevice: updateDevice,
1271 removeDevice: removeDevice,
1272 addHost: addHost,
1273 updateHost: updateHost,
Simon Hunt95d56fd2015-11-12 11:06:44 -08001274 moveHost: moveHost,
Simon Hunt1894d792015-02-04 17:09:20 -08001275 removeHost: removeHost,
1276 addLink: addLink,
1277 updateLink: updateLink,
Simon Hunt4a6b54b2015-10-27 22:08:25 -07001278 removeLink: removeLink,
Steven Burrows1c2a9682017-07-14 16:52:46 +01001279 topoStartDone: topoStartDone,
Simon Hunte2d9dc72017-08-10 15:21:04 -07001280
1281 setLionBundle: setLionBundle,
Simon Hunt737c89f2015-01-28 12:23:19 -08001282 };
1283 }]);
1284}());