blob: 9b9a29cc11ccce35669cc7548304457472c5eb74 [file] [log] [blame]
Simon Hunt737c89f2015-01-28 12:23:19 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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
29 // configuration
Simon Hunt1894d792015-02-04 17:09:20 -080030 var linkConfig = {
31 light: {
Simon Huntf44d7262016-06-14 14:46:56 -070032 baseColor: '#939598',
Simon Hunt1894d792015-02-04 17:09:20 -080033 inColor: '#66f',
Simon Hunt3a6eec02015-02-09 21:16:43 -080034 outColor: '#f00'
Simon Hunt1894d792015-02-04 17:09:20 -080035 },
36 dark: {
Simon Huntf44d7262016-06-14 14:46:56 -070037 // TODO : theme
38 baseColor: '#939598',
Simon Hunt1894d792015-02-04 17:09:20 -080039 inColor: '#66f',
Simon Huntf44d7262016-06-14 14:46:56 -070040 outColor: '#f00'
Simon Hunt1894d792015-02-04 17:09:20 -080041 },
42 inWidth: 12,
43 outWidth: 10
44 };
45
Simon Hunt737c89f2015-01-28 12:23:19 -080046 // internal state
Simon Huntac4c6f72015-02-03 19:50:53 -080047 var settings, // merged default settings and options
Simon Hunt737c89f2015-01-28 12:23:19 -080048 force, // force layout object
49 drag, // drag behavior handler
50 network = {
51 nodes: [],
52 links: [],
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -070053 linksByDevice: {},
Simon Hunt737c89f2015-01-28 12:23:19 -080054 lookup: {},
55 revLinkToKey: {}
Simon Huntac4c6f72015-02-03 19:50:53 -080056 },
Simon Hunt3ab20282015-02-26 20:32:19 -080057 lu, // shorthand for lookup
58 rlk, // shorthand for revLinktoKey
Simon Hunta142dd22015-02-12 22:07:51 -080059 showHosts = false, // whether hosts are displayed
Simon Hunt5724fb42015-02-05 16:59:40 -080060 showOffline = true, // whether offline devices are displayed
Simon Hunt445e8152015-02-06 13:00:12 -080061 nodeLock = false, // whether nodes can be dragged or not (locked)
Simon Hunta17fa672015-08-19 18:42:22 -070062 fTimer, // timer for delayed force layout
Thomas Vachuska1a989c12015-06-09 18:29:22 -070063 fNodesTimer, // timer for delayed nodes update
64 fLinksTimer, // timer for delayed links update
Bri Prebilic Cole80401762015-07-16 11:36:18 -070065 dim, // the dimensions of the force layout [w,h]
Steven Burrowsf17f0ab2017-04-11 11:03:58 -070066 linkNums = [], // array of link number labels
67 devIconDim = 36, // node target dimension
68 devIconDimMin = 20, // node minimum dimension when zoomed out
69 devIconDimMax = 40, // node maximum dimension when zoomed in
70 portLabelDim = 30;
Simon Hunt737c89f2015-01-28 12:23:19 -080071
72 // SVG elements;
Bri Prebilic Cole80401762015-07-16 11:36:18 -070073 var linkG, linkLabelG, numLinkLblsG, portLabelG, nodeG;
Simon Hunt737c89f2015-01-28 12:23:19 -080074
75 // D3 selections;
76 var link, linkLabel, node;
77
78 // default settings for force layout
79 var defaultSettings = {
80 gravity: 0.4,
81 friction: 0.7,
82 charge: {
83 // note: key is node.class
84 device: -8000,
85 host: -5000,
86 _def_: -12000
87 },
88 linkDistance: {
89 // note: key is link.type
90 direct: 100,
91 optical: 120,
92 hostLink: 3,
93 _def_: 50
94 },
95 linkStrength: {
96 // note: key is link.type
97 // range: {0.0 ... 1.0}
98 //direct: 1.0,
99 //optical: 1.0,
100 //hostLink: 1.0,
101 _def_: 1.0
102 }
103 };
104
105
Simon Huntac4c6f72015-02-03 19:50:53 -0800106 // ==========================
107 // === EVENT HANDLERS
108
109 function addDevice(data) {
110 var id = data.id,
111 d;
112
Simon Hunt1894d792015-02-04 17:09:20 -0800113 uplink.showNoDevs(false);
Simon Huntac4c6f72015-02-03 19:50:53 -0800114
115 // although this is an add device event, if we already have the
116 // device, treat it as an update instead..
Simon Hunt1894d792015-02-04 17:09:20 -0800117 if (lu[id]) {
Simon Huntac4c6f72015-02-03 19:50:53 -0800118 updateDevice(data);
119 return;
120 }
121
Simon Hunt3a6eec02015-02-09 21:16:43 -0800122 d = tms.createDeviceNode(data);
Simon Huntac4c6f72015-02-03 19:50:53 -0800123 network.nodes.push(d);
Simon Hunt1894d792015-02-04 17:09:20 -0800124 lu[id] = d;
Simon Huntac4c6f72015-02-03 19:50:53 -0800125 updateNodes();
Simon Hunta17fa672015-08-19 18:42:22 -0700126 fStart();
Simon Huntac4c6f72015-02-03 19:50:53 -0800127 }
128
129 function updateDevice(data) {
130 var id = data.id,
Simon Hunt1894d792015-02-04 17:09:20 -0800131 d = lu[id],
Simon Huntac4c6f72015-02-03 19:50:53 -0800132 wasOnline;
133
134 if (d) {
135 wasOnline = d.online;
136 angular.extend(d, data);
Simon Hunt3a6eec02015-02-09 21:16:43 -0800137 if (tms.positionNode(d, true)) {
Simon Hunt445e8152015-02-06 13:00:12 -0800138 sendUpdateMeta(d);
Simon Huntac4c6f72015-02-03 19:50:53 -0800139 }
140 updateNodes();
141 if (wasOnline !== d.online) {
Simon Huntdc6adea2015-02-09 22:29:36 -0800142 tms.findAttachedLinks(d.id).forEach(restyleLinkElement);
Simon Hunt5724fb42015-02-05 16:59:40 -0800143 updateOfflineVisibility(d);
Simon Huntac4c6f72015-02-03 19:50:53 -0800144 }
Simon Huntac4c6f72015-02-03 19:50:53 -0800145 }
146 }
147
Simon Hunt1894d792015-02-04 17:09:20 -0800148 function removeDevice(data) {
149 var id = data.id,
150 d = lu[id];
151 if (d) {
152 removeDeviceElement(d);
Simon Hunt1894d792015-02-04 17:09:20 -0800153 }
154 }
155
156 function addHost(data) {
157 var id = data.id,
158 d, lnk;
159
160 // although this is an add host event, if we already have the
161 // host, treat it as an update instead..
162 if (lu[id]) {
163 updateHost(data);
164 return;
165 }
166
Simon Hunt3a6eec02015-02-09 21:16:43 -0800167 d = tms.createHostNode(data);
Simon Hunt1894d792015-02-04 17:09:20 -0800168 network.nodes.push(d);
169 lu[id] = d;
Simon Hunt1894d792015-02-04 17:09:20 -0800170 updateNodes();
171
Simon Hunt3a6eec02015-02-09 21:16:43 -0800172 lnk = tms.createHostLink(data);
Simon Hunt1894d792015-02-04 17:09:20 -0800173 if (lnk) {
Simon Hunt1894d792015-02-04 17:09:20 -0800174 d.linkData = lnk; // cache ref on its host
175 network.links.push(lnk);
176 lu[d.ingress] = lnk;
177 lu[d.egress] = lnk;
178 updateLinks();
179 }
Simon Hunta17fa672015-08-19 18:42:22 -0700180 fStart();
Simon Hunt1894d792015-02-04 17:09:20 -0800181 }
182
183 function updateHost(data) {
184 var id = data.id,
185 d = lu[id];
186 if (d) {
187 angular.extend(d, data);
Simon Hunt3a6eec02015-02-09 21:16:43 -0800188 if (tms.positionNode(d, true)) {
Simon Hunt445e8152015-02-06 13:00:12 -0800189 sendUpdateMeta(d);
Simon Hunt1894d792015-02-04 17:09:20 -0800190 }
191 updateNodes();
Simon Hunt1894d792015-02-04 17:09:20 -0800192 }
193 }
194
Simon Hunt95d56fd2015-11-12 11:06:44 -0800195 function moveHost(data) {
196 var id = data.id,
197 d = lu[id],
198 lnk;
199 if (d) {
200 // first remove the old host link
201 removeLinkElement(d.linkData);
202
203 // merge new data
204 angular.extend(d, data);
205 if (tms.positionNode(d, true)) {
206 sendUpdateMeta(d);
207 }
208
209 // now create a new host link
210 lnk = tms.createHostLink(data);
211 if (lnk) {
212 d.linkData = lnk;
213 network.links.push(lnk);
214 lu[d.ingress] = lnk;
215 lu[d.egress] = lnk;
216 }
217
218 updateNodes();
219 updateLinks();
220 fResume();
221 }
222 }
223
Simon Hunt1894d792015-02-04 17:09:20 -0800224 function removeHost(data) {
225 var id = data.id,
226 d = lu[id];
227 if (d) {
228 removeHostElement(d, true);
Simon Hunt1894d792015-02-04 17:09:20 -0800229 }
230 }
231
232 function addLink(data) {
Simon Huntdc6adea2015-02-09 22:29:36 -0800233 var result = tms.findLink(data, 'add'),
Simon Hunt1894d792015-02-04 17:09:20 -0800234 bad = result.badLogic,
235 d = result.ldata;
236
237 if (bad) {
Simon Hunteb18f522016-01-28 19:22:23 -0800238 $log.debug(bad + ': ' + link.id);
Simon Hunt1894d792015-02-04 17:09:20 -0800239 return;
240 }
241
242 if (d) {
243 // we already have a backing store link for src/dst nodes
244 addLinkUpdate(d, data);
245 return;
246 }
247
248 // no backing store link yet
Simon Hunt3a6eec02015-02-09 21:16:43 -0800249 d = tms.createLink(data);
Simon Hunt1894d792015-02-04 17:09:20 -0800250 if (d) {
251 network.links.push(d);
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700252 aggregateLink(d, data);
Simon Hunt1894d792015-02-04 17:09:20 -0800253 lu[d.key] = d;
254 updateLinks();
Simon Hunta17fa672015-08-19 18:42:22 -0700255 fStart();
Simon Hunt1894d792015-02-04 17:09:20 -0800256 }
257 }
258
259 function updateLink(data) {
Simon Huntdc6adea2015-02-09 22:29:36 -0800260 var result = tms.findLink(data, 'update'),
Simon Hunt1894d792015-02-04 17:09:20 -0800261 bad = result.badLogic;
262 if (bad) {
Simon Hunteb18f522016-01-28 19:22:23 -0800263 $log.debug(bad + ': ' + link.id);
Simon Hunt1894d792015-02-04 17:09:20 -0800264 return;
265 }
Simon Hunteb18f522016-01-28 19:22:23 -0800266 result.updateWith(data);
Simon Hunt1894d792015-02-04 17:09:20 -0800267 }
268
269 function removeLink(data) {
Simon Hunta4242de2015-02-24 17:11:55 -0800270 var result = tms.findLink(data, 'remove');
271
272 if (!result.badLogic) {
273 result.removeRawLink();
Simon Hunt1894d792015-02-04 17:09:20 -0800274 }
Simon Hunt1894d792015-02-04 17:09:20 -0800275 }
276
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700277 function topoStartDone(data) {
278 // called when the initial barrage of data has been sent from server
279 uplink.topoStartDone();
280 }
281
Simon Hunt1894d792015-02-04 17:09:20 -0800282 // ========================
283
Simon Hunt94f7dae2015-08-26 17:40:59 -0700284 function nodeById(id) {
285 return lu[id];
286 }
287
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700288 function makeNodeKey(node1, node2) {
289 return node1 + '-' + node2;
290 }
291
292 function findNodePair(key, keyRev) {
293 if (network.linksByDevice[key]) {
294 return key;
295 } else if (network.linksByDevice[keyRev]) {
296 return keyRev;
297 } else {
298 return false;
299 }
300 }
301
302 function aggregateLink(ldata, link) {
303 var key = makeNodeKey(link.src, link.dst),
304 keyRev = makeNodeKey(link.dst, link.src),
305 found = findNodePair(key, keyRev);
306
307 if (found) {
308 network.linksByDevice[found].push(ldata);
309 ldata.devicePair = found;
310 } else {
311 network.linksByDevice[key] = [ ldata ];
312 ldata.devicePair = key;
313 }
314 }
315
Simon Hunt1894d792015-02-04 17:09:20 -0800316 function addLinkUpdate(ldata, link) {
317 // add link event, but we already have the reverse link installed
318 ldata.fromTarget = link;
Simon Huntdc6adea2015-02-09 22:29:36 -0800319 rlk[link.id] = ldata.key;
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700320 // possible solution to el being undefined in restyleLinkElement:
321 //_updateLinks();
Simon Hunt1894d792015-02-04 17:09:20 -0800322 restyleLinkElement(ldata);
323 }
324
Simon Hunt1894d792015-02-04 17:09:20 -0800325
326 var widthRatio = 1.4,
327 linkScale = d3.scale.linear()
328 .domain([1, 12])
329 .range([widthRatio, 12 * widthRatio])
Simon Hunt5724fb42015-02-05 16:59:40 -0800330 .clamp(true),
Ray Milkeyb7f0f642016-01-22 16:08:14 -0800331 allLinkTypes = 'direct indirect optical tunnel',
332 allLinkSubTypes = 'inactive not-permitted';
Simon Hunt1894d792015-02-04 17:09:20 -0800333
Simon Hunta142dd22015-02-12 22:07:51 -0800334 function restyleLinkElement(ldata, immediate) {
Simon Hunt1894d792015-02-04 17:09:20 -0800335 // this fn's job is to look at raw links and decide what svg classes
336 // need to be applied to the line element in the DOM
337 var th = ts.theme(),
338 el = ldata.el,
339 type = ldata.type(),
340 lw = ldata.linkWidth(),
Simon Hunta142dd22015-02-12 22:07:51 -0800341 online = ldata.online(),
Ray Milkeyb7f0f642016-01-22 16:08:14 -0800342 modeCls = ldata.expected() ? 'inactive' : 'not-permitted',
Simon Hunta142dd22015-02-12 22:07:51 -0800343 delay = immediate ? 0 : 1000;
Simon Hunt1894d792015-02-04 17:09:20 -0800344
Simon Huntf44d7262016-06-14 14:46:56 -0700345 // NOTE: understand why el is sometimes undefined on addLink events...
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700346 // Investigated:
347 // el is undefined when it's a reverse link that is being added.
348 // updateLinks (which sets ldata.el) isn't called before this is called.
349 // Calling _updateLinks in addLinkUpdate fixes it, but there might be
350 // a more efficient way to fix it.
351 if (el && !el.empty()) {
Thomas Vachuskacb5016f2015-05-18 14:11:43 -0700352 el.classed('link', true);
Ray Milkeyb7f0f642016-01-22 16:08:14 -0800353 el.classed(allLinkSubTypes, false);
354 el.classed(modeCls, !online);
Thomas Vachuskacb5016f2015-05-18 14:11:43 -0700355 el.classed(allLinkTypes, false);
356 if (type) {
357 el.classed(type, true);
358 }
359 el.transition()
360 .duration(delay)
361 .attr('stroke-width', linkScale(lw))
362 .attr('stroke', linkConfig[th].baseColor);
Simon Hunt1894d792015-02-04 17:09:20 -0800363 }
Simon Hunt1894d792015-02-04 17:09:20 -0800364 }
365
Simon Hunt1894d792015-02-04 17:09:20 -0800366 function removeLinkElement(d) {
367 var idx = fs.find(d.key, network.links, 'key'),
368 removed;
369 if (idx >=0) {
370 // remove from links array
371 removed = network.links.splice(idx, 1);
372 // remove from lookup cache
373 delete lu[removed[0].key];
374 updateLinks();
Simon Hunta17fa672015-08-19 18:42:22 -0700375 fResume();
Simon Hunt1894d792015-02-04 17:09:20 -0800376 }
377 }
378
379 function removeHostElement(d, upd) {
380 // first, remove associated hostLink...
381 removeLinkElement(d.linkData);
382
383 // remove hostLink bindings
384 delete lu[d.ingress];
385 delete lu[d.egress];
386
387 // remove from lookup cache
388 delete lu[d.id];
389 // remove from nodes array
390 var idx = fs.find(d.id, network.nodes);
391 network.nodes.splice(idx, 1);
392
393 // remove from SVG
394 // NOTE: upd is false if we were called from removeDeviceElement()
395 if (upd) {
396 updateNodes();
Simon Hunta17fa672015-08-19 18:42:22 -0700397 fResume();
Simon Hunt1894d792015-02-04 17:09:20 -0800398 }
399 }
400
401 function removeDeviceElement(d) {
Bri Prebilic Cole8d003782015-07-31 15:33:06 -0700402 var id = d.id,
403 idx;
Simon Hunt1894d792015-02-04 17:09:20 -0800404 // first, remove associated hosts and links..
Simon Huntdc6adea2015-02-09 22:29:36 -0800405 tms.findAttachedHosts(id).forEach(removeHostElement);
406 tms.findAttachedLinks(id).forEach(removeLinkElement);
Simon Hunt1894d792015-02-04 17:09:20 -0800407
408 // remove from lookup cache
409 delete lu[id];
410 // remove from nodes array
Bri Prebilic Cole8d003782015-07-31 15:33:06 -0700411 idx = fs.find(id, network.nodes);
412 if (idx > -1) {
413 network.nodes.splice(idx, 1);
414 }
Simon Hunt1894d792015-02-04 17:09:20 -0800415
416 if (!network.nodes.length) {
Simon Huntdc6adea2015-02-09 22:29:36 -0800417 uplink.showNoDevs(true);
Simon Hunt1894d792015-02-04 17:09:20 -0800418 }
419
420 // remove from SVG
421 updateNodes();
Simon Hunta17fa672015-08-19 18:42:22 -0700422 fResume();
Simon Hunt1894d792015-02-04 17:09:20 -0800423 }
424
Simon Hunt5724fb42015-02-05 16:59:40 -0800425 function updateHostVisibility() {
Simon Hunt18bf9822015-02-12 17:35:45 -0800426 sus.visible(nodeG.selectAll('.host'), showHosts);
427 sus.visible(linkG.selectAll('.hostLink'), showHosts);
Simon Hunt8eb4d3a2015-02-23 18:23:29 -0800428 sus.visible(linkLabelG.selectAll('.hostLinkLabel'), showHosts);
Simon Hunt5724fb42015-02-05 16:59:40 -0800429 }
430
431 function updateOfflineVisibility(dev) {
432 function updDev(d, show) {
Simon Hunt8eb4d3a2015-02-23 18:23:29 -0800433 var b;
Simon Hunt18bf9822015-02-12 17:35:45 -0800434 sus.visible(d.el, show);
Simon Hunt5724fb42015-02-05 16:59:40 -0800435
Simon Huntdc6adea2015-02-09 22:29:36 -0800436 tms.findAttachedLinks(d.id).forEach(function (link) {
Simon Hunt5724fb42015-02-05 16:59:40 -0800437 b = show && ((link.type() !== 'hostLink') || showHosts);
Simon Hunt18bf9822015-02-12 17:35:45 -0800438 sus.visible(link.el, b);
Simon Hunt5724fb42015-02-05 16:59:40 -0800439 });
Simon Huntdc6adea2015-02-09 22:29:36 -0800440 tms.findAttachedHosts(d.id).forEach(function (host) {
Simon Hunt5724fb42015-02-05 16:59:40 -0800441 b = show && showHosts;
Simon Hunt18bf9822015-02-12 17:35:45 -0800442 sus.visible(host.el, b);
Simon Hunt5724fb42015-02-05 16:59:40 -0800443 });
444 }
445
446 if (dev) {
447 // updating a specific device that just toggled off/on-line
448 updDev(dev, dev.online || showOffline);
449 } else {
450 // updating all offline devices
Simon Huntdc6adea2015-02-09 22:29:36 -0800451 tms.findDevices(true).forEach(function (d) {
Simon Hunt5724fb42015-02-05 16:59:40 -0800452 updDev(d, showOffline);
453 });
454 }
455 }
456
Simon Hunt1894d792015-02-04 17:09:20 -0800457
Simon Hunt445e8152015-02-06 13:00:12 -0800458 function sendUpdateMeta(d, clearPos) {
Simon Huntac4c6f72015-02-03 19:50:53 -0800459 var metaUi = {},
460 ll;
461
Simon Hunt445e8152015-02-06 13:00:12 -0800462 // if we are not clearing the position data (unpinning),
Simon Huntfd7106c2016-02-09 15:05:26 -0800463 // attach the x, y, (and equivalent longitude, latitude)...
Simon Hunt445e8152015-02-06 13:00:12 -0800464 if (!clearPos) {
Simon Hunt3a6eec02015-02-09 21:16:43 -0800465 ll = tms.lngLatFromCoord([d.x, d.y]);
Simon Huntfd7106c2016-02-09 15:05:26 -0800466 metaUi = {
467 x: d.x,
468 y: d.y,
469 equivLoc: {
470 lng: ll[0],
471 lat: ll[1]
472 }
473 };
Simon Hunt1894d792015-02-04 17:09:20 -0800474 }
475 d.metaUi = metaUi;
Simon Hunt237676b52015-03-10 19:04:26 -0700476 wss.sendEvent('updateMeta', {
Simon Hunt1894d792015-02-04 17:09:20 -0800477 id: d.id,
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700478 class: d.class,
Simon Hunt1894d792015-02-04 17:09:20 -0800479 memento: metaUi
480 });
Simon Huntac4c6f72015-02-03 19:50:53 -0800481 }
482
Simon Hunt1894d792015-02-04 17:09:20 -0800483
Simon Huntac4c6f72015-02-03 19:50:53 -0800484 function mkSvgClass(d) {
485 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
486 }
487
Simon Hunt5724fb42015-02-05 16:59:40 -0800488 function vis(b) {
489 return b ? 'visible' : 'hidden';
490 }
491
Simon Huntfcbde892015-04-16 12:05:28 -0700492 function toggleHosts(x) {
493 var kev = (x === 'keyev'),
494 on = kev ? !showHosts : !!x;
495
496 showHosts = on;
Simon Hunt5724fb42015-02-05 16:59:40 -0800497 updateHostVisibility();
Simon Huntfcbde892015-04-16 12:05:28 -0700498 flash.flash('Hosts ' + vis(on));
499 return on;
Simon Hunt5724fb42015-02-05 16:59:40 -0800500 }
501
Simon Huntfcbde892015-04-16 12:05:28 -0700502 function toggleOffline(x) {
503 var kev = (x === 'keyev'),
504 on = kev ? !showOffline : !!x;
505
506 showOffline = on;
Simon Hunt5724fb42015-02-05 16:59:40 -0800507 updateOfflineVisibility();
Simon Huntfcbde892015-04-16 12:05:28 -0700508 flash.flash('Offline devices ' + vis(on));
509 return on;
Simon Hunt5724fb42015-02-05 16:59:40 -0800510 }
511
512 function cycleDeviceLabels() {
Bri Prebilic Cole9cf1a8d2015-04-21 13:15:29 -0700513 flash.flash(td3.incDevLabIndex());
Simon Huntdc6adea2015-02-09 22:29:36 -0800514 tms.findDevices().forEach(function (d) {
Simon Hunta4242de2015-02-24 17:11:55 -0800515 td3.updateDeviceLabel(d);
Simon Hunt1c367112015-02-05 18:02:46 -0800516 });
Simon Hunt5724fb42015-02-05 16:59:40 -0800517 }
518
Simon Hunt445e8152015-02-06 13:00:12 -0800519 function unpin() {
Simon Hunt08f841d02015-02-10 14:39:20 -0800520 var hov = tss.hovered();
521 if (hov) {
522 sendUpdateMeta(hov, true);
523 hov.fixed = false;
524 hov.el.classed('fixed', false);
Simon Hunt445e8152015-02-06 13:00:12 -0800525 fResume();
526 }
527 }
528
Simon Hunta142dd22015-02-12 22:07:51 -0800529 function showMastership(masterId) {
530 if (!masterId) {
531 restoreLayerState();
532 } else {
533 showMastershipFor(masterId);
534 }
535 }
536
537 function restoreLayerState() {
538 // NOTE: this level of indirection required, for when we have
539 // the layer filter functionality re-implemented
540 suppressLayers(false);
541 }
542
543 function showMastershipFor(id) {
544 suppressLayers(true);
545 node.each(function (n) {
546 if (n.master === id) {
Simon Hunt743a8492015-08-25 16:18:19 -0700547 n.el.classed('suppressedmax', false);
Simon Hunta142dd22015-02-12 22:07:51 -0800548 }
549 });
550 }
551
Simon Hunt743a8492015-08-25 16:18:19 -0700552 function supAmt(less) {
553 return less ? "suppressed" : "suppressedmax";
554 }
555
556 function suppressLayers(b, less) {
557 var cls = supAmt(less);
558 node.classed(cls, b);
559 link.classed(cls, b);
560 }
561
562 function unsuppressNode(id, less) {
563 var cls = supAmt(less);
564 node.each(function (n) {
565 if (n.id === id) {
566 n.el.classed(cls, false);
567 }
568 });
569 }
570
Simon Hunt94f7dae2015-08-26 17:40:59 -0700571 function unsuppressLink(key, less) {
Simon Hunt743a8492015-08-25 16:18:19 -0700572 var cls = supAmt(less);
573 link.each(function (n) {
Simon Hunt94f7dae2015-08-26 17:40:59 -0700574 if (n.key === key) {
Simon Hunt743a8492015-08-25 16:18:19 -0700575 n.el.classed(cls, false);
576 }
577 });
Simon Hunta142dd22015-02-12 22:07:51 -0800578 }
Simon Hunt445e8152015-02-06 13:00:12 -0800579
Simon Hunt86b7c882015-04-02 23:06:08 -0700580 function showBadLinks() {
581 var badLinks = tms.findBadLinks();
582 flash.flash('Bad Links: ' + badLinks.length);
583 $log.debug('Bad Link List (' + badLinks.length + '):');
584 badLinks.forEach(function (d) {
585 $log.debug('bad link: (' + d.bad + ') ' + d.key, d);
586 if (d.el) {
587 d.el.attr('stroke-width', linkScale(2.8))
588 .attr('stroke', 'red');
589 }
590 });
591 // back to normal after 2 seconds...
592 $timeout(updateLinks, 2000);
593 }
594
Steven Burrowsf17f0ab2017-04-11 11:03:58 -0700595 function deviceScale() {
596 var scale = uplink.zoomer().scale(),
597 dim = devIconDim,
598 multiplier = 1;
599
600 if (dim * scale < devIconDimMin) {
601 multiplier = devIconDimMin / (dim * scale);
602 } else if (dim * scale > devIconDimMax) {
603 multiplier = devIconDimMax / (dim * scale);
604 }
605
606 return multiplier;
607 }
608
609 function linkWidthScale(scale) {
610 var scale = uplink.zoomer().scale();
611 return linkScale(widthRatio) / scale;
612 }
613
614 function portLabelScale(scale) {
615 var scale = uplink.zoomer().scale();
616 return portLabelDim / (portLabelDim * scale);
617 }
618
619 function setNodeScale(scale) {
620 // Scale the network nodes
621 _.each(network.nodes, function (node) {
622 if (node.class === 'host') {
623 node.el.selectAll('g').style('transform', 'scale(' + deviceScale(scale) + ')');
624 node.el.selectAll('text').style('transform', 'scale(' + deviceScale(scale) + ')');
625 return;
626 }
627 node.el.selectAll('*')
628 .style('transform', 'scale(' + deviceScale(scale) + ')');
629 });
630
631 // Scale the network links
632 _.each(network.links, function (link) {
633 link.el.style('stroke-width', linkWidthScale(scale) + 'px');
634 });
635
636 d3.select('#topo-portLabels')
637 .selectAll('.portLabel')
638 .selectAll('*')
639 .style('transform', 'scale(' + portLabelScale(scale) + ')');
640 }
641
Simon Huntfd7106c2016-02-09 15:05:26 -0800642 function resetAllLocations() {
643 tms.resetAllLocations();
644 updateNodes();
645 tick(); // force nodes to be redrawn in their new locations
646 flash.flash('Reset Node Locations');
647 }
648
Simon Hunt5724fb42015-02-05 16:59:40 -0800649 // ==========================================
650
Simon Huntac4c6f72015-02-03 19:50:53 -0800651 function updateNodes() {
Thomas Vachuska1a989c12015-06-09 18:29:22 -0700652 if (fNodesTimer) {
653 $timeout.cancel(fNodesTimer);
654 }
655 fNodesTimer = $timeout(_updateNodes, 150);
656 }
657
Simon Hunta17fa672015-08-19 18:42:22 -0700658 // IMPLEMENTATION NOTE: _updateNodes() should NOT stop, start, or resume
659 // the force layout; that needs to be determined and implemented elsewhere
Thomas Vachuska1a989c12015-06-09 18:29:22 -0700660 function _updateNodes() {
Steven Burrowsf17f0ab2017-04-11 11:03:58 -0700661
662 var scale = uplink.zoomer().scale();
Simon Hunt1894d792015-02-04 17:09:20 -0800663 // select all the nodes in the layout:
Simon Huntac4c6f72015-02-03 19:50:53 -0800664 node = nodeG.selectAll('.node')
665 .data(network.nodes, function (d) { return d.id; });
666
Simon Hunt1894d792015-02-04 17:09:20 -0800667 // operate on existing nodes:
Simon Hunta4242de2015-02-24 17:11:55 -0800668 node.filter('.device').each(td3.deviceExisting);
669 node.filter('.host').each(td3.hostExisting);
Simon Huntac4c6f72015-02-03 19:50:53 -0800670
671 // operate on entering nodes:
672 var entering = node.enter()
673 .append('g')
674 .attr({
675 id: function (d) { return sus.safeId(d.id); },
676 class: mkSvgClass,
Simon Hunta17fa672015-08-19 18:42:22 -0700677 transform: function (d) {
678 // Need to guard against NaN here ??
679 return sus.translate(d.x, d.y);
680 },
Simon Huntac4c6f72015-02-03 19:50:53 -0800681 opacity: 0
682 })
683 .call(drag)
Simon Hunt08f841d02015-02-10 14:39:20 -0800684 .on('mouseover', tss.nodeMouseOver)
685 .on('mouseout', tss.nodeMouseOut)
Simon Huntac4c6f72015-02-03 19:50:53 -0800686 .transition()
687 .attr('opacity', 1);
688
Simon Hunt1894d792015-02-04 17:09:20 -0800689 // augment entering nodes:
Simon Hunta4242de2015-02-24 17:11:55 -0800690 entering.filter('.device').each(td3.deviceEnter);
691 entering.filter('.host').each(td3.hostEnter);
Simon Huntac4c6f72015-02-03 19:50:53 -0800692
Simon Hunt51056592015-02-03 21:48:07 -0800693 // operate on both existing and new nodes:
Simon Hunta4242de2015-02-24 17:11:55 -0800694 td3.updateDeviceColors();
Simon Huntac4c6f72015-02-03 19:50:53 -0800695
696 // operate on exiting nodes:
697 // Note that the node is removed after 2 seconds.
698 // Sub element animations should be shorter than 2 seconds.
699 var exiting = node.exit()
700 .transition()
701 .duration(2000)
702 .style('opacity', 0)
703 .remove();
704
Simon Hunt1894d792015-02-04 17:09:20 -0800705 // exiting node specifics:
Simon Hunta4242de2015-02-24 17:11:55 -0800706 exiting.filter('.host').each(td3.hostExit);
707 exiting.filter('.device').each(td3.deviceExit);
Simon Huntac4c6f72015-02-03 19:50:53 -0800708 }
709
Simon Hunt51056592015-02-03 21:48:07 -0800710 // ==========================
Simon Hunt1894d792015-02-04 17:09:20 -0800711
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700712 function getDefaultPos(link) {
713 return {
714 x1: link.source.x,
715 y1: link.source.y,
716 x2: link.target.x,
717 y2: link.target.y
718 };
719 }
720
721 // returns amount of adjustment along the normal for given link
722 function amt(numLinks, linkIdx) {
723 var gap = 6;
724 return (linkIdx - ((numLinks - 1) / 2)) * gap;
725 }
726
727 function calcMovement(d, amt, flipped) {
728 var pos = getDefaultPos(d),
729 mult = flipped ? -amt : amt,
730 dx = pos.x2 - pos.x1,
731 dy = pos.y2 - pos.y1,
732 length = Math.sqrt((dx * dx) + (dy * dy));
733
734 return {
735 x1: pos.x1 + (mult * dy / length),
736 y1: pos.y1 + (mult * -dx / length),
737 x2: pos.x2 + (mult * dy / length),
738 y2: pos.y2 + (mult * -dx / length)
739 };
740 }
741
742 function calcPosition() {
743 var lines = this,
744 linkSrcId;
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700745 linkNums = [];
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700746 lines.each(function (d) {
747 if (d.type() === 'hostLink') {
748 d.position = getDefaultPos(d);
749 }
750 });
751
752 function normalizeLinkSrc(link) {
753 // ensure source device is consistent across set of links
754 // temporary measure until link modeling is refactored
755 if (!linkSrcId) {
756 linkSrcId = link.source.id;
757 return false;
758 }
759
760 return link.source.id !== linkSrcId;
761 }
762
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700763 angular.forEach(network.linksByDevice, function (linkArr, key) {
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700764 var numLinks = linkArr.length,
765 link;
766
767 if (numLinks === 1) {
768 link = linkArr[0];
769 link.position = getDefaultPos(link);
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700770 link.position.multiLink = false;
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700771 } else if (numLinks >= 5) {
772 // this code is inefficient, in the future the way links
773 // are modeled will be changed
774 angular.forEach(linkArr, function (link) {
775 link.position = getDefaultPos(link);
776 link.position.multiLink = true;
777 });
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700778 linkNums.push({
779 id: key,
780 num: numLinks,
781 linkCoords: linkArr[0].position
782 });
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700783 } else {
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700784 linkSrcId = null;
785 angular.forEach(linkArr, function (link, index) {
786 var offsetAmt = amt(numLinks, index),
787 needToFlip = normalizeLinkSrc(link);
788 link.position = calcMovement(link, offsetAmt, needToFlip);
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700789 link.position.multiLink = false;
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700790 });
791 }
792 });
793 }
794
Simon Hunt1894d792015-02-04 17:09:20 -0800795 function updateLinks() {
Thomas Vachuska1a989c12015-06-09 18:29:22 -0700796 if (fLinksTimer) {
797 $timeout.cancel(fLinksTimer);
798 }
799 fLinksTimer = $timeout(_updateLinks, 150);
800 }
801
Simon Hunta17fa672015-08-19 18:42:22 -0700802 // IMPLEMENTATION NOTE: _updateLinks() should NOT stop, start, or resume
803 // the force layout; that needs to be determined and implemented elsewhere
Thomas Vachuska1a989c12015-06-09 18:29:22 -0700804 function _updateLinks() {
Simon Hunt1894d792015-02-04 17:09:20 -0800805 var th = ts.theme();
806
807 link = linkG.selectAll('.link')
808 .data(network.links, function (d) { return d.key; });
809
810 // operate on existing links:
Simon Huntd5264122015-02-25 10:17:43 -0800811 link.each(function (d) {
812 // this is supposed to be an existing link, but we have observed
813 // occasions (where links are deleted and added rapidly?) where
814 // the DOM element has not been defined. So protect against that...
815 if (d.el) {
816 restyleLinkElement(d, true);
817 }
818 });
Simon Hunt1894d792015-02-04 17:09:20 -0800819
820 // operate on entering links:
821 var entering = link.enter()
822 .append('line')
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700823 .call(calcPosition)
Simon Hunt1894d792015-02-04 17:09:20 -0800824 .attr({
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700825 x1: function (d) { return d.position.x1; },
826 y1: function (d) { return d.position.y1; },
827 x2: function (d) { return d.position.x2; },
828 y2: function (d) { return d.position.y2; },
Simon Hunt1894d792015-02-04 17:09:20 -0800829 stroke: linkConfig[th].inColor,
830 'stroke-width': linkConfig.inWidth
831 });
832
833 // augment links
Simon Hunta4242de2015-02-24 17:11:55 -0800834 entering.each(td3.linkEntering);
Simon Hunt1894d792015-02-04 17:09:20 -0800835
836 // operate on both existing and new links:
837 //link.each(...)
838
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700839 // add labels for how many links are in a thick line
840 td3.applyNumLinkLabels(linkNums, numLinkLblsG);
841
Simon Hunt1894d792015-02-04 17:09:20 -0800842 // apply or remove labels
Simon Hunta4242de2015-02-24 17:11:55 -0800843 td3.applyLinkLabels();
Simon Hunt1894d792015-02-04 17:09:20 -0800844
845 // operate on exiting links:
846 link.exit()
847 .attr('stroke-dasharray', '3 3')
Simon Hunt5724fb42015-02-05 16:59:40 -0800848 .attr('stroke', linkConfig[th].outColor)
Simon Hunt1894d792015-02-04 17:09:20 -0800849 .style('opacity', 0.5)
850 .transition()
851 .duration(1500)
852 .attr({
853 'stroke-dasharray': '3 12',
Simon Hunt1894d792015-02-04 17:09:20 -0800854 'stroke-width': linkConfig.outWidth
855 })
856 .style('opacity', 0.0)
857 .remove();
Simon Hunt1894d792015-02-04 17:09:20 -0800858 }
859
Simon Huntac4c6f72015-02-03 19:50:53 -0800860
861 // ==========================
Simon Hunt737c89f2015-01-28 12:23:19 -0800862 // force layout tick function
Simon Hunt737c89f2015-01-28 12:23:19 -0800863
Simon Hunt5724fb42015-02-05 16:59:40 -0800864 function fResume() {
Simon Huntc3c5b672015-02-20 11:32:13 -0800865 if (!tos.isOblique()) {
Simon Hunt5724fb42015-02-05 16:59:40 -0800866 force.resume();
867 }
868 }
869
870 function fStart() {
Simon Huntc3c5b672015-02-20 11:32:13 -0800871 if (!tos.isOblique()) {
Simon Hunta17fa672015-08-19 18:42:22 -0700872 if (fTimer) {
873 $timeout.cancel(fTimer);
874 }
875 fTimer = $timeout(function () {
876 $log.debug("Starting force-layout");
877 force.start();
878 }, 200);
Simon Hunt5724fb42015-02-05 16:59:40 -0800879 }
880 }
881
882 var tickStuff = {
883 nodeAttr: {
Simon Hunta17fa672015-08-19 18:42:22 -0700884 transform: function (d) {
885 var dx = isNaN(d.x) ? 0 : d.x,
886 dy = isNaN(d.y) ? 0 : d.y;
887 return sus.translate(dx, dy);
888 }
Simon Hunt5724fb42015-02-05 16:59:40 -0800889 },
890 linkAttr: {
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700891 x1: function (d) { return d.position.x1; },
892 y1: function (d) { return d.position.y1; },
893 x2: function (d) { return d.position.x2; },
894 y2: function (d) { return d.position.y2; }
Simon Hunt5724fb42015-02-05 16:59:40 -0800895 },
896 linkLabelAttr: {
897 transform: function (d) {
Simon Huntdc6adea2015-02-09 22:29:36 -0800898 var lnk = tms.findLinkById(d.key);
Simon Hunt5724fb42015-02-05 16:59:40 -0800899 if (lnk) {
Carmelo Casconed01eda62016-08-02 10:19:15 -0700900 return td3.transformLabel(lnk.position, d.key);
Simon Hunt5724fb42015-02-05 16:59:40 -0800901 }
902 }
903 }
904 };
905
906 function tick() {
Simon Hunt3ab20282015-02-26 20:32:19 -0800907 // guard against null (which can happen when our view pages out)...
Bri Prebilic Cole8d003782015-07-31 15:33:06 -0700908 if (node && node.size()) {
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700909 node.attr(tickStuff.nodeAttr);
910 }
Bri Prebilic Cole8d003782015-07-31 15:33:06 -0700911 if (link && link.size()) {
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700912 link.call(calcPosition)
913 .attr(tickStuff.linkAttr);
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700914 td3.applyNumLinkLabels(linkNums, numLinkLblsG);
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700915 }
Bri Prebilic Cole8d003782015-07-31 15:33:06 -0700916 if (linkLabel && linkLabel.size()) {
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700917 linkLabel.attr(tickStuff.linkLabelAttr);
918 }
Simon Hunt737c89f2015-01-28 12:23:19 -0800919 }
920
921
Simon Huntac4c6f72015-02-03 19:50:53 -0800922 // ==========================
923 // === MOUSE GESTURE HANDLERS
924
Simon Hunt205099e2015-02-07 13:12:01 -0800925 function zoomingOrPanning(ev) {
926 return ev.metaKey || ev.altKey;
Simon Hunt445e8152015-02-06 13:00:12 -0800927 }
928
929 function atDragEnd(d) {
930 // once we've finished moving, pin the node in position
931 d.fixed = true;
932 d3.select(this).classed('fixed', true);
933 sendUpdateMeta(d);
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700934 tss.clickConsumed(true);
Simon Hunt445e8152015-02-06 13:00:12 -0800935 }
936
937 // predicate that indicates when dragging is active
938 function dragEnabled() {
939 var ev = d3.event.sourceEvent;
940 // nodeLock means we aren't allowing nodes to be dragged...
Simon Hunt205099e2015-02-07 13:12:01 -0800941 return !nodeLock && !zoomingOrPanning(ev);
Simon Hunt445e8152015-02-06 13:00:12 -0800942 }
943
944 // predicate that indicates when clicking is active
945 function clickEnabled() {
946 return true;
947 }
Simon Hunt737c89f2015-01-28 12:23:19 -0800948
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700949 // =============================================
950 // function entry points for overlay module
Simon Huntf542d842015-02-11 16:20:33 -0800951
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700952 // TODO: find an automatic way of tracking via the "showHighlights" events
Simon Hunte50829c2015-06-09 08:39:28 -0700953 var allTrafficClasses = 'primary secondary optical animated ' +
Simon Hunt21281fd2017-03-30 22:28:28 -0700954 'port-traffic-green port-traffic-yellow port-traffic-orange ' +
955 'port-traffic-red';
Simon Huntf542d842015-02-11 16:20:33 -0800956
957 function clearLinkTrafficStyle() {
958 link.style('stroke-width', null)
959 .classed(allTrafficClasses, false);
960 }
961
962 function removeLinkLabels() {
963 network.links.forEach(function (d) {
964 d.label = '';
965 });
966 }
Simon Hunt737c89f2015-01-28 12:23:19 -0800967
Simon Hunte9343f32015-10-21 18:07:46 -0700968 function clearNodeDeco() {
969 node.selectAll('g.badge').remove();
970 }
971
972 function removeNodeBadges() {
973 network.nodes.forEach(function (d) {
974 d.badge = null;
975 });
976 }
977
Simon Hunta4242de2015-02-24 17:11:55 -0800978 function updateLinkLabelModel() {
979 // create the backing data for showing labels..
980 var data = [];
981 link.each(function (d) {
982 if (d.label) {
983 data.push({
984 id: 'lab-' + d.key,
985 key: d.key,
986 label: d.label,
987 ldata: d
988 });
989 }
990 });
991
992 linkLabel = linkLabelG.selectAll('.linkLabel')
993 .data(data, function (d) { return d.id; });
994 }
995
Simon Hunt737c89f2015-01-28 12:23:19 -0800996 // ==========================
Simon Huntac4c6f72015-02-03 19:50:53 -0800997 // Module definition
Simon Hunt737c89f2015-01-28 12:23:19 -0800998
Simon Huntdc6adea2015-02-09 22:29:36 -0800999 function mkModelApi(uplink) {
1000 return {
1001 projection: uplink.projection,
1002 network: network,
1003 restyleLinkElement: restyleLinkElement,
1004 removeLinkElement: removeLinkElement
1005 };
1006 }
1007
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001008 function mkD3Api() {
Simon Hunta4242de2015-02-24 17:11:55 -08001009 return {
1010 node: function () { return node; },
1011 link: function () { return link; },
1012 linkLabel: function () { return linkLabel; },
1013 instVisible: function () { return tis.isVisible(); },
1014 posNode: tms.positionNode,
1015 showHosts: function () { return showHosts; },
1016 restyleLinkElement: restyleLinkElement,
Bri Prebilic Cole80401762015-07-16 11:36:18 -07001017 updateLinkLabelModel: updateLinkLabelModel,
Steven Burrowsf17f0ab2017-04-11 11:03:58 -07001018 linkConfig: function () { return linkConfig; },
1019 deviceScale: deviceScale,
Simon Hunt4a850cc2017-06-08 14:30:04 -07001020 linkWidthScale: linkWidthScale
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -07001021 };
Simon Hunta4242de2015-02-24 17:11:55 -08001022 }
1023
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001024 function mkSelectApi() {
Simon Hunt08f841d02015-02-10 14:39:20 -08001025 return {
1026 node: function () { return node; },
1027 zoomingOrPanning: zoomingOrPanning,
Simon Hunt0c6b2d32015-03-26 17:46:29 -07001028 updateDeviceColors: td3.updateDeviceColors,
Prince Pereira46c82d42016-09-19 13:30:50 +05301029 deselectAllLinks: tls.deselectAllLinks
Simon Hunt08f841d02015-02-10 14:39:20 -08001030 };
1031 }
1032
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001033 function mkTrafficApi() {
1034 return {
1035 hovered: tss.hovered,
1036 somethingSelected: tss.somethingSelected,
1037 selectOrder: tss.selectOrder
1038 };
1039 }
1040
1041 function mkOverlayApi() {
Simon Huntf542d842015-02-11 16:20:33 -08001042 return {
Simon Hunte9343f32015-10-21 18:07:46 -07001043 clearNodeDeco: clearNodeDeco,
1044 removeNodeBadges: removeNodeBadges,
Simon Huntf542d842015-02-11 16:20:33 -08001045 clearLinkTrafficStyle: clearLinkTrafficStyle,
1046 removeLinkLabels: removeLinkLabels,
Simon Hunt743a8492015-08-25 16:18:19 -07001047 findLinkById: tms.findLinkById,
Simon Hunt94f7dae2015-08-26 17:40:59 -07001048 findNodeById: nodeById,
Simon Huntf542d842015-02-11 16:20:33 -08001049 updateLinks: updateLinks,
Simon Hunt743a8492015-08-25 16:18:19 -07001050 updateNodes: updateNodes,
1051 supLayers: suppressLayers,
1052 unsupNode: unsuppressNode,
1053 unsupLink: unsuppressLink
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -07001054 };
Simon Huntf542d842015-02-11 16:20:33 -08001055 }
1056
Simon Huntc3c5b672015-02-20 11:32:13 -08001057 function mkObliqueApi(uplink, fltr) {
Simon Hunt96f88c62015-02-19 17:57:25 -08001058 return {
Simon Huntc3c5b672015-02-20 11:32:13 -08001059 force: function() { return force; },
1060 zoomLayer: uplink.zoomLayer,
1061 nodeGBBox: function() { return nodeG.node().getBBox(); },
Simon Hunt96f88c62015-02-19 17:57:25 -08001062 node: function () { return node; },
Simon Huntc3c5b672015-02-20 11:32:13 -08001063 link: function () { return link; },
1064 linkLabel: function () { return linkLabel; },
1065 nodes: function () { return network.nodes; },
1066 tickStuff: tickStuff,
1067 nodeLock: function (b) {
1068 var old = nodeLock;
1069 nodeLock = b;
1070 return old;
1071 },
1072 opacifyMap: uplink.opacifyMap,
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -07001073 inLayer: fltr.inLayer,
Bri Prebilic Cole80401762015-07-16 11:36:18 -07001074 calcLinkPos: calcPosition,
1075 applyNumLinkLabels: function () {
1076 td3.applyNumLinkLabels(linkNums, numLinkLblsG);
1077 }
Simon Hunt96f88c62015-02-19 17:57:25 -08001078 };
1079 }
1080
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001081 function mkFilterApi() {
Simon Hunteb0fa052015-02-17 19:20:28 -08001082 return {
1083 node: function () { return node; },
1084 link: function () { return link; }
1085 };
1086 }
1087
Simon Hunt9e2104c2015-02-26 10:48:59 -08001088 function mkLinkApi(svg, uplink) {
Simon Huntfb8ea1f2015-02-24 21:38:09 -08001089 return {
1090 svg: svg,
Simon Huntfb8ea1f2015-02-24 21:38:09 -08001091 zoomer: uplink.zoomer(),
1092 network: network,
Simon Hunt1a5301e2015-02-25 15:31:25 -08001093 portLabelG: function () { return portLabelG; },
Simon Huntfb8ea1f2015-02-24 21:38:09 -08001094 showHosts: function () { return showHosts; }
1095 };
1096 }
1097
Simon Huntf51bf462016-06-29 16:22:57 -07001098 function updateLinksAndNodes() {
1099 updateLinks();
1100 updateNodes();
1101 }
Steven Burrowsec1f45c2016-08-08 16:14:41 +01001102
Simon Hunt737c89f2015-01-28 12:23:19 -08001103 angular.module('ovTopo')
1104 .factory('TopoForceService',
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -07001105 ['$log', '$timeout', 'FnService', 'SvgUtilService',
Simon Hunt86b7c882015-04-02 23:06:08 -07001106 'ThemeService', 'FlashService', 'WebSocketService',
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001107 'TopoOverlayService', 'TopoInstService', 'TopoModelService',
Simon Hunta4242de2015-02-24 17:11:55 -08001108 'TopoD3Service', 'TopoSelectService', 'TopoTrafficService',
Simon Huntfb8ea1f2015-02-24 21:38:09 -08001109 'TopoObliqueService', 'TopoFilterService', 'TopoLinkService',
Andrea Campanella732ea832017-02-06 09:25:59 -08001110 'TopoProtectedIntentsService',
Simon Hunt737c89f2015-01-28 12:23:19 -08001111
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001112 function (_$log_, _$timeout_, _fs_, _sus_, _ts_, _flash_, _wss_, _tov_,
Andrea Campanella732ea832017-02-06 09:25:59 -08001113 _tis_, _tms_, _td3_, _tss_, _tts_, _tos_, _fltr_, _tls_, _tpis_) {
Simon Hunt737c89f2015-01-28 12:23:19 -08001114 $log = _$log_;
Simon Hunt86b7c882015-04-02 23:06:08 -07001115 $timeout = _$timeout_;
Simon Hunt1894d792015-02-04 17:09:20 -08001116 fs = _fs_;
Simon Hunt737c89f2015-01-28 12:23:19 -08001117 sus = _sus_;
Simon Huntac4c6f72015-02-03 19:50:53 -08001118 ts = _ts_;
Simon Hunt5724fb42015-02-05 16:59:40 -08001119 flash = _flash_;
Simon Hunt237676b52015-03-10 19:04:26 -07001120 wss = _wss_;
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001121 tov = _tov_;
Simon Huntac4c6f72015-02-03 19:50:53 -08001122 tis = _tis_;
Simon Hunt3a6eec02015-02-09 21:16:43 -08001123 tms = _tms_;
Simon Hunta4242de2015-02-24 17:11:55 -08001124 td3 = _td3_;
Simon Hunt08f841d02015-02-10 14:39:20 -08001125 tss = _tss_;
Simon Huntf542d842015-02-11 16:20:33 -08001126 tts = _tts_;
Simon Hunt96f88c62015-02-19 17:57:25 -08001127 tos = _tos_;
Simon Hunteb0fa052015-02-17 19:20:28 -08001128 fltr = _fltr_;
Simon Huntfb8ea1f2015-02-24 21:38:09 -08001129 tls = _tls_;
Andrea Campanella732ea832017-02-06 09:25:59 -08001130 tpis = _tpis_;
Simon Hunt737c89f2015-01-28 12:23:19 -08001131
Simon Huntf51bf462016-06-29 16:22:57 -07001132 ts.addListener(updateLinksAndNodes);
Simon Hunta142dd22015-02-12 22:07:51 -08001133
Simon Hunt737c89f2015-01-28 12:23:19 -08001134 // forceG is the SVG group to display the force layout in
Simon Huntdc6adea2015-02-09 22:29:36 -08001135 // uplink is the api from the main topo source file
Simon Hunt3a6eec02015-02-09 21:16:43 -08001136 // dim is the initial dimensions of the SVG as [w,h]
Simon Hunt737c89f2015-01-28 12:23:19 -08001137 // opts are, well, optional :)
Simon Hunt3ab20282015-02-26 20:32:19 -08001138 function initForce(_svg_, forceG, _uplink_, _dim_, opts) {
Simon Hunt1894d792015-02-04 17:09:20 -08001139 uplink = _uplink_;
Simon Hunt3a6eec02015-02-09 21:16:43 -08001140 dim = _dim_;
Simon Hunt3ab20282015-02-26 20:32:19 -08001141 svg = _svg_;
1142
1143 lu = network.lookup;
1144 rlk = network.revLinkToKey;
Simon Hunt3a6eec02015-02-09 21:16:43 -08001145
1146 $log.debug('initForce().. dim = ' + dim);
1147
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001148 tov.setApi(mkOverlayApi(), tss);
Simon Huntdc6adea2015-02-09 22:29:36 -08001149 tms.initModel(mkModelApi(uplink), dim);
Steven Burrowsf17f0ab2017-04-11 11:03:58 -07001150 td3.initD3(mkD3Api(), uplink.zoomer());
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001151 tss.initSelect(mkSelectApi());
1152 tts.initTraffic(mkTrafficApi());
Andrea Campanella732ea832017-02-06 09:25:59 -08001153 tpis.initProtectedIntents(mkTrafficApi());
Simon Huntc3c5b672015-02-20 11:32:13 -08001154 tos.initOblique(mkObliqueApi(uplink, fltr));
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001155 fltr.initFilter(mkFilterApi());
Simon Hunt9e2104c2015-02-26 10:48:59 -08001156 tls.initLink(mkLinkApi(svg, uplink), td3);
Simon Hunta11b4eb2015-01-28 16:20:50 -08001157
Simon Hunt737c89f2015-01-28 12:23:19 -08001158 settings = angular.extend({}, defaultSettings, opts);
1159
1160 linkG = forceG.append('g').attr('id', 'topo-links');
1161 linkLabelG = forceG.append('g').attr('id', 'topo-linkLabels');
Bri Prebilic Cole80401762015-07-16 11:36:18 -07001162 numLinkLblsG = forceG.append('g').attr('id', 'topo-numLinkLabels');
Simon Hunt737c89f2015-01-28 12:23:19 -08001163 nodeG = forceG.append('g').attr('id', 'topo-nodes');
Simon Hunt1a5301e2015-02-25 15:31:25 -08001164 portLabelG = forceG.append('g').attr('id', 'topo-portLabels');
Simon Hunt737c89f2015-01-28 12:23:19 -08001165
1166 link = linkG.selectAll('.link');
1167 linkLabel = linkLabelG.selectAll('.linkLabel');
1168 node = nodeG.selectAll('.node');
1169
1170 force = d3.layout.force()
Simon Hunt3a6eec02015-02-09 21:16:43 -08001171 .size(dim)
Simon Hunt737c89f2015-01-28 12:23:19 -08001172 .nodes(network.nodes)
1173 .links(network.links)
1174 .gravity(settings.gravity)
1175 .friction(settings.friction)
1176 .charge(settings.charge._def_)
1177 .linkDistance(settings.linkDistance._def_)
1178 .linkStrength(settings.linkStrength._def_)
1179 .on('tick', tick);
1180
1181 drag = sus.createDragBehavior(force,
Simon Hunt08f841d02015-02-10 14:39:20 -08001182 tss.selectObject, atDragEnd, dragEnabled, clickEnabled);
Simon Hunt737c89f2015-01-28 12:23:19 -08001183 }
1184
Simon Hunt3a6eec02015-02-09 21:16:43 -08001185 function newDim(_dim_) {
1186 dim = _dim_;
1187 force.size(dim);
1188 tms.newDim(dim);
Simon Hunt737c89f2015-01-28 12:23:19 -08001189 }
1190
Simon Hunt3a6eec02015-02-09 21:16:43 -08001191 function destroyForce() {
Simon Hunt3ab20282015-02-26 20:32:19 -08001192 force.stop();
1193
Simon Huntfb8ea1f2015-02-24 21:38:09 -08001194 tls.destroyLink();
Simon Hunt96f88c62015-02-19 17:57:25 -08001195 tos.destroyOblique();
Simon Huntf542d842015-02-11 16:20:33 -08001196 tts.destroyTraffic();
Andrea Campanella732ea832017-02-06 09:25:59 -08001197 tpis.destroyProtectedIntents();
Simon Huntf542d842015-02-11 16:20:33 -08001198 tss.destroySelect();
Simon Hunta4242de2015-02-24 17:11:55 -08001199 td3.destroyD3();
Simon Huntf542d842015-02-11 16:20:33 -08001200 tms.destroyModel();
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001201 // note: no need to destroy overlay service
Simon Huntf51bf462016-06-29 16:22:57 -07001202 ts.removeListener(updateLinksAndNodes);
Simon Hunt3ab20282015-02-26 20:32:19 -08001203
1204 // clean up the DOM
1205 svg.selectAll('g').remove();
1206 svg.selectAll('defs').remove();
1207
1208 // clean up internal state
1209 network.nodes = [];
1210 network.links = [];
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -07001211 network.linksByDevice = {};
Simon Hunt3ab20282015-02-26 20:32:19 -08001212 network.lookup = {};
1213 network.revLinkToKey = {};
1214
Bri Prebilic Cole80401762015-07-16 11:36:18 -07001215 linkNums = [];
1216
1217 linkG = linkLabelG = numLinkLblsG = nodeG = portLabelG = null;
Simon Hunt3ab20282015-02-26 20:32:19 -08001218 link = linkLabel = node = null;
1219 force = drag = null;
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -07001220
1221 // clean up $timeout promises
Simon Hunta17fa672015-08-19 18:42:22 -07001222 if (fTimer) {
1223 $timeout.cancel(fTimer);
1224 }
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -07001225 if (fNodesTimer) {
1226 $timeout.cancel(fNodesTimer);
1227 }
1228 if (fLinksTimer) {
1229 $timeout.cancel(fLinksTimer);
1230 }
Simon Hunt3a6eec02015-02-09 21:16:43 -08001231 }
1232
Simon Hunt737c89f2015-01-28 12:23:19 -08001233 return {
1234 initForce: initForce,
Simon Hunt3a6eec02015-02-09 21:16:43 -08001235 newDim: newDim,
1236 destroyForce: destroyForce,
Simon Huntac4c6f72015-02-03 19:50:53 -08001237
Simon Hunta4242de2015-02-24 17:11:55 -08001238 updateDeviceColors: td3.updateDeviceColors,
Simon Hunt5724fb42015-02-05 16:59:40 -08001239 toggleHosts: toggleHosts,
Simon Hunt9e2104c2015-02-26 10:48:59 -08001240 togglePorts: tls.togglePorts,
Simon Hunt5724fb42015-02-05 16:59:40 -08001241 toggleOffline: toggleOffline,
1242 cycleDeviceLabels: cycleDeviceLabels,
Simon Hunt445e8152015-02-06 13:00:12 -08001243 unpin: unpin,
Simon Hunta142dd22015-02-12 22:07:51 -08001244 showMastership: showMastership,
Simon Hunt86b7c882015-04-02 23:06:08 -07001245 showBadLinks: showBadLinks,
Steven Burrowsf17f0ab2017-04-11 11:03:58 -07001246 setNodeScale: setNodeScale,
Simon Huntac4c6f72015-02-03 19:50:53 -08001247
Simon Huntfd7106c2016-02-09 15:05:26 -08001248 resetAllLocations: resetAllLocations,
Simon Huntac4c6f72015-02-03 19:50:53 -08001249 addDevice: addDevice,
Simon Hunt1894d792015-02-04 17:09:20 -08001250 updateDevice: updateDevice,
1251 removeDevice: removeDevice,
1252 addHost: addHost,
1253 updateHost: updateHost,
Simon Hunt95d56fd2015-11-12 11:06:44 -08001254 moveHost: moveHost,
Simon Hunt1894d792015-02-04 17:09:20 -08001255 removeHost: removeHost,
1256 addLink: addLink,
1257 updateLink: updateLink,
Simon Hunt4a6b54b2015-10-27 22:08:25 -07001258 removeLink: removeLink,
1259 topoStartDone: topoStartDone
Simon Hunt737c89f2015-01-28 12:23:19 -08001260 };
1261 }]);
1262}());