blob: 555fa503287501c0289a5746bb83256f86cdc946 [file] [log] [blame]
Simon Hunt737c89f2015-01-28 12:23:19 -08001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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,
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -070027 tis, tms, td3, tss, tts, tos, fltr, tls, uplink, svg;
Simon Huntac4c6f72015-02-03 19:50:53 -080028
29 // configuration
Simon Hunt1894d792015-02-04 17:09:20 -080030 var linkConfig = {
31 light: {
32 baseColor: '#666',
33 inColor: '#66f',
Simon Hunt3a6eec02015-02-09 21:16:43 -080034 outColor: '#f00'
Simon Hunt1894d792015-02-04 17:09:20 -080035 },
36 dark: {
Simon Hunt5724fb42015-02-05 16:59:40 -080037 baseColor: '#aaa',
Simon Hunt1894d792015-02-04 17:09:20 -080038 inColor: '#66f',
Simon Hunt5724fb42015-02-05 16:59:40 -080039 outColor: '#f66'
Simon Hunt1894d792015-02-04 17:09:20 -080040 },
41 inWidth: 12,
42 outWidth: 10
43 };
44
Simon Hunt737c89f2015-01-28 12:23:19 -080045 // internal state
Simon Huntac4c6f72015-02-03 19:50:53 -080046 var settings, // merged default settings and options
Simon Hunt737c89f2015-01-28 12:23:19 -080047 force, // force layout object
48 drag, // drag behavior handler
49 network = {
50 nodes: [],
51 links: [],
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -070052 linksByDevice: {},
Simon Hunt737c89f2015-01-28 12:23:19 -080053 lookup: {},
54 revLinkToKey: {}
Simon Huntac4c6f72015-02-03 19:50:53 -080055 },
Simon Hunt3ab20282015-02-26 20:32:19 -080056 lu, // shorthand for lookup
57 rlk, // shorthand for revLinktoKey
Simon Hunta142dd22015-02-12 22:07:51 -080058 showHosts = false, // whether hosts are displayed
Simon Hunt5724fb42015-02-05 16:59:40 -080059 showOffline = true, // whether offline devices are displayed
Simon Hunt445e8152015-02-06 13:00:12 -080060 nodeLock = false, // whether nodes can be dragged or not (locked)
Simon Hunta17fa672015-08-19 18:42:22 -070061 fTimer, // timer for delayed force layout
Thomas Vachuska1a989c12015-06-09 18:29:22 -070062 fNodesTimer, // timer for delayed nodes update
63 fLinksTimer, // timer for delayed links update
Bri Prebilic Cole80401762015-07-16 11:36:18 -070064 dim, // the dimensions of the force layout [w,h]
65 linkNums = []; // array of link number labels
Simon Hunt737c89f2015-01-28 12:23:19 -080066
67 // SVG elements;
Bri Prebilic Cole80401762015-07-16 11:36:18 -070068 var linkG, linkLabelG, numLinkLblsG, portLabelG, nodeG;
Simon Hunt737c89f2015-01-28 12:23:19 -080069
70 // D3 selections;
71 var link, linkLabel, node;
72
73 // default settings for force layout
74 var defaultSettings = {
75 gravity: 0.4,
76 friction: 0.7,
77 charge: {
78 // note: key is node.class
79 device: -8000,
80 host: -5000,
81 _def_: -12000
82 },
83 linkDistance: {
84 // note: key is link.type
85 direct: 100,
86 optical: 120,
87 hostLink: 3,
88 _def_: 50
89 },
90 linkStrength: {
91 // note: key is link.type
92 // range: {0.0 ... 1.0}
93 //direct: 1.0,
94 //optical: 1.0,
95 //hostLink: 1.0,
96 _def_: 1.0
97 }
98 };
99
100
Simon Huntac4c6f72015-02-03 19:50:53 -0800101 // ==========================
102 // === EVENT HANDLERS
103
104 function addDevice(data) {
105 var id = data.id,
106 d;
107
Simon Hunt1894d792015-02-04 17:09:20 -0800108 uplink.showNoDevs(false);
Simon Huntac4c6f72015-02-03 19:50:53 -0800109
110 // although this is an add device event, if we already have the
111 // device, treat it as an update instead..
Simon Hunt1894d792015-02-04 17:09:20 -0800112 if (lu[id]) {
Simon Huntac4c6f72015-02-03 19:50:53 -0800113 updateDevice(data);
114 return;
115 }
116
Simon Hunt3a6eec02015-02-09 21:16:43 -0800117 d = tms.createDeviceNode(data);
Simon Huntac4c6f72015-02-03 19:50:53 -0800118 network.nodes.push(d);
Simon Hunt1894d792015-02-04 17:09:20 -0800119 lu[id] = d;
Simon Huntac4c6f72015-02-03 19:50:53 -0800120 updateNodes();
Simon Hunta17fa672015-08-19 18:42:22 -0700121 fStart();
Simon Huntac4c6f72015-02-03 19:50:53 -0800122 }
123
124 function updateDevice(data) {
125 var id = data.id,
Simon Hunt1894d792015-02-04 17:09:20 -0800126 d = lu[id],
Simon Huntac4c6f72015-02-03 19:50:53 -0800127 wasOnline;
128
129 if (d) {
130 wasOnline = d.online;
131 angular.extend(d, data);
Simon Hunt3a6eec02015-02-09 21:16:43 -0800132 if (tms.positionNode(d, true)) {
Simon Hunt445e8152015-02-06 13:00:12 -0800133 sendUpdateMeta(d);
Simon Huntac4c6f72015-02-03 19:50:53 -0800134 }
135 updateNodes();
136 if (wasOnline !== d.online) {
Simon Huntdc6adea2015-02-09 22:29:36 -0800137 tms.findAttachedLinks(d.id).forEach(restyleLinkElement);
Simon Hunt5724fb42015-02-05 16:59:40 -0800138 updateOfflineVisibility(d);
Simon Huntac4c6f72015-02-03 19:50:53 -0800139 }
Simon Huntac4c6f72015-02-03 19:50:53 -0800140 }
141 }
142
Simon Hunt1894d792015-02-04 17:09:20 -0800143 function removeDevice(data) {
144 var id = data.id,
145 d = lu[id];
146 if (d) {
147 removeDeviceElement(d);
Simon Hunt1894d792015-02-04 17:09:20 -0800148 }
149 }
150
151 function addHost(data) {
152 var id = data.id,
153 d, lnk;
154
155 // although this is an add host event, if we already have the
156 // host, treat it as an update instead..
157 if (lu[id]) {
158 updateHost(data);
159 return;
160 }
161
Simon Hunt3a6eec02015-02-09 21:16:43 -0800162 d = tms.createHostNode(data);
Simon Hunt1894d792015-02-04 17:09:20 -0800163 network.nodes.push(d);
164 lu[id] = d;
Simon Hunt1894d792015-02-04 17:09:20 -0800165 updateNodes();
166
Simon Hunt3a6eec02015-02-09 21:16:43 -0800167 lnk = tms.createHostLink(data);
Simon Hunt1894d792015-02-04 17:09:20 -0800168 if (lnk) {
Simon Hunt1894d792015-02-04 17:09:20 -0800169 d.linkData = lnk; // cache ref on its host
170 network.links.push(lnk);
171 lu[d.ingress] = lnk;
172 lu[d.egress] = lnk;
173 updateLinks();
174 }
Simon Hunta17fa672015-08-19 18:42:22 -0700175 fStart();
Simon Hunt1894d792015-02-04 17:09:20 -0800176 }
177
178 function updateHost(data) {
179 var id = data.id,
180 d = lu[id];
181 if (d) {
182 angular.extend(d, data);
Simon Hunt3a6eec02015-02-09 21:16:43 -0800183 if (tms.positionNode(d, true)) {
Simon Hunt445e8152015-02-06 13:00:12 -0800184 sendUpdateMeta(d);
Simon Hunt1894d792015-02-04 17:09:20 -0800185 }
186 updateNodes();
Simon Hunt1894d792015-02-04 17:09:20 -0800187 }
188 }
189
190 function removeHost(data) {
191 var id = data.id,
192 d = lu[id];
193 if (d) {
194 removeHostElement(d, true);
Simon Hunt1894d792015-02-04 17:09:20 -0800195 }
196 }
197
198 function addLink(data) {
Simon Huntdc6adea2015-02-09 22:29:36 -0800199 var result = tms.findLink(data, 'add'),
Simon Hunt1894d792015-02-04 17:09:20 -0800200 bad = result.badLogic,
201 d = result.ldata;
202
203 if (bad) {
204 //logicError(bad + ': ' + link.id);
205 return;
206 }
207
208 if (d) {
209 // we already have a backing store link for src/dst nodes
210 addLinkUpdate(d, data);
211 return;
212 }
213
214 // no backing store link yet
Simon Hunt3a6eec02015-02-09 21:16:43 -0800215 d = tms.createLink(data);
Simon Hunt1894d792015-02-04 17:09:20 -0800216 if (d) {
217 network.links.push(d);
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700218 aggregateLink(d, data);
Simon Hunt1894d792015-02-04 17:09:20 -0800219 lu[d.key] = d;
220 updateLinks();
Simon Hunta17fa672015-08-19 18:42:22 -0700221 fStart();
Simon Hunt1894d792015-02-04 17:09:20 -0800222 }
223 }
224
225 function updateLink(data) {
Simon Huntdc6adea2015-02-09 22:29:36 -0800226 var result = tms.findLink(data, 'update'),
Simon Hunt1894d792015-02-04 17:09:20 -0800227 bad = result.badLogic;
228 if (bad) {
229 //logicError(bad + ': ' + link.id);
230 return;
231 }
232 result.updateWith(link);
233 }
234
235 function removeLink(data) {
Simon Hunta4242de2015-02-24 17:11:55 -0800236 var result = tms.findLink(data, 'remove');
237
238 if (!result.badLogic) {
239 result.removeRawLink();
Simon Hunt1894d792015-02-04 17:09:20 -0800240 }
Simon Hunt1894d792015-02-04 17:09:20 -0800241 }
242
243 // ========================
244
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700245 function makeNodeKey(node1, node2) {
246 return node1 + '-' + node2;
247 }
248
249 function findNodePair(key, keyRev) {
250 if (network.linksByDevice[key]) {
251 return key;
252 } else if (network.linksByDevice[keyRev]) {
253 return keyRev;
254 } else {
255 return false;
256 }
257 }
258
259 function aggregateLink(ldata, link) {
260 var key = makeNodeKey(link.src, link.dst),
261 keyRev = makeNodeKey(link.dst, link.src),
262 found = findNodePair(key, keyRev);
263
264 if (found) {
265 network.linksByDevice[found].push(ldata);
266 ldata.devicePair = found;
267 } else {
268 network.linksByDevice[key] = [ ldata ];
269 ldata.devicePair = key;
270 }
271 }
272
Simon Hunt1894d792015-02-04 17:09:20 -0800273 function addLinkUpdate(ldata, link) {
274 // add link event, but we already have the reverse link installed
275 ldata.fromTarget = link;
Simon Huntdc6adea2015-02-09 22:29:36 -0800276 rlk[link.id] = ldata.key;
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700277 // possible solution to el being undefined in restyleLinkElement:
278 //_updateLinks();
Simon Hunt1894d792015-02-04 17:09:20 -0800279 restyleLinkElement(ldata);
280 }
281
Simon Hunt1894d792015-02-04 17:09:20 -0800282
283 var widthRatio = 1.4,
284 linkScale = d3.scale.linear()
285 .domain([1, 12])
286 .range([widthRatio, 12 * widthRatio])
Simon Hunt5724fb42015-02-05 16:59:40 -0800287 .clamp(true),
Simon Hunt3a6eec02015-02-09 21:16:43 -0800288 allLinkTypes = 'direct indirect optical tunnel';
Simon Hunt1894d792015-02-04 17:09:20 -0800289
Simon Hunta142dd22015-02-12 22:07:51 -0800290 function restyleLinkElement(ldata, immediate) {
Simon Hunt1894d792015-02-04 17:09:20 -0800291 // this fn's job is to look at raw links and decide what svg classes
292 // need to be applied to the line element in the DOM
293 var th = ts.theme(),
294 el = ldata.el,
295 type = ldata.type(),
296 lw = ldata.linkWidth(),
Simon Hunta142dd22015-02-12 22:07:51 -0800297 online = ldata.online(),
298 delay = immediate ? 0 : 1000;
Simon Hunt1894d792015-02-04 17:09:20 -0800299
Thomas Vachuskacb5016f2015-05-18 14:11:43 -0700300 // FIXME: understand why el is sometimes undefined on addLink events...
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700301 // Investigated:
302 // el is undefined when it's a reverse link that is being added.
303 // updateLinks (which sets ldata.el) isn't called before this is called.
304 // Calling _updateLinks in addLinkUpdate fixes it, but there might be
305 // a more efficient way to fix it.
306 if (el && !el.empty()) {
Thomas Vachuskacb5016f2015-05-18 14:11:43 -0700307 el.classed('link', true);
308 el.classed('inactive', !online);
309 el.classed(allLinkTypes, false);
310 if (type) {
311 el.classed(type, true);
312 }
313 el.transition()
314 .duration(delay)
315 .attr('stroke-width', linkScale(lw))
316 .attr('stroke', linkConfig[th].baseColor);
Simon Hunt1894d792015-02-04 17:09:20 -0800317 }
Simon Hunt1894d792015-02-04 17:09:20 -0800318 }
319
Simon Hunt1894d792015-02-04 17:09:20 -0800320 function removeLinkElement(d) {
321 var idx = fs.find(d.key, network.links, 'key'),
322 removed;
323 if (idx >=0) {
324 // remove from links array
325 removed = network.links.splice(idx, 1);
326 // remove from lookup cache
327 delete lu[removed[0].key];
328 updateLinks();
Simon Hunta17fa672015-08-19 18:42:22 -0700329 fResume();
Simon Hunt1894d792015-02-04 17:09:20 -0800330 }
331 }
332
333 function removeHostElement(d, upd) {
334 // first, remove associated hostLink...
335 removeLinkElement(d.linkData);
336
337 // remove hostLink bindings
338 delete lu[d.ingress];
339 delete lu[d.egress];
340
341 // remove from lookup cache
342 delete lu[d.id];
343 // remove from nodes array
344 var idx = fs.find(d.id, network.nodes);
345 network.nodes.splice(idx, 1);
346
347 // remove from SVG
348 // NOTE: upd is false if we were called from removeDeviceElement()
349 if (upd) {
350 updateNodes();
Simon Hunta17fa672015-08-19 18:42:22 -0700351 fResume();
Simon Hunt1894d792015-02-04 17:09:20 -0800352 }
353 }
354
355 function removeDeviceElement(d) {
Bri Prebilic Cole8d003782015-07-31 15:33:06 -0700356 var id = d.id,
357 idx;
Simon Hunt1894d792015-02-04 17:09:20 -0800358 // first, remove associated hosts and links..
Simon Huntdc6adea2015-02-09 22:29:36 -0800359 tms.findAttachedHosts(id).forEach(removeHostElement);
360 tms.findAttachedLinks(id).forEach(removeLinkElement);
Simon Hunt1894d792015-02-04 17:09:20 -0800361
362 // remove from lookup cache
363 delete lu[id];
364 // remove from nodes array
Bri Prebilic Cole8d003782015-07-31 15:33:06 -0700365 idx = fs.find(id, network.nodes);
366 if (idx > -1) {
367 network.nodes.splice(idx, 1);
368 }
Simon Hunt1894d792015-02-04 17:09:20 -0800369
370 if (!network.nodes.length) {
Simon Huntdc6adea2015-02-09 22:29:36 -0800371 uplink.showNoDevs(true);
Simon Hunt1894d792015-02-04 17:09:20 -0800372 }
373
374 // remove from SVG
375 updateNodes();
Simon Hunta17fa672015-08-19 18:42:22 -0700376 fResume();
Simon Hunt1894d792015-02-04 17:09:20 -0800377 }
378
Simon Hunt5724fb42015-02-05 16:59:40 -0800379 function updateHostVisibility() {
Simon Hunt18bf9822015-02-12 17:35:45 -0800380 sus.visible(nodeG.selectAll('.host'), showHosts);
381 sus.visible(linkG.selectAll('.hostLink'), showHosts);
Simon Hunt8eb4d3a2015-02-23 18:23:29 -0800382 sus.visible(linkLabelG.selectAll('.hostLinkLabel'), showHosts);
Simon Hunt5724fb42015-02-05 16:59:40 -0800383 }
384
385 function updateOfflineVisibility(dev) {
386 function updDev(d, show) {
Simon Hunt8eb4d3a2015-02-23 18:23:29 -0800387 var b;
Simon Hunt18bf9822015-02-12 17:35:45 -0800388 sus.visible(d.el, show);
Simon Hunt5724fb42015-02-05 16:59:40 -0800389
Simon Huntdc6adea2015-02-09 22:29:36 -0800390 tms.findAttachedLinks(d.id).forEach(function (link) {
Simon Hunt5724fb42015-02-05 16:59:40 -0800391 b = show && ((link.type() !== 'hostLink') || showHosts);
Simon Hunt18bf9822015-02-12 17:35:45 -0800392 sus.visible(link.el, b);
Simon Hunt5724fb42015-02-05 16:59:40 -0800393 });
Simon Huntdc6adea2015-02-09 22:29:36 -0800394 tms.findAttachedHosts(d.id).forEach(function (host) {
Simon Hunt5724fb42015-02-05 16:59:40 -0800395 b = show && showHosts;
Simon Hunt18bf9822015-02-12 17:35:45 -0800396 sus.visible(host.el, b);
Simon Hunt5724fb42015-02-05 16:59:40 -0800397 });
398 }
399
400 if (dev) {
401 // updating a specific device that just toggled off/on-line
402 updDev(dev, dev.online || showOffline);
403 } else {
404 // updating all offline devices
Simon Huntdc6adea2015-02-09 22:29:36 -0800405 tms.findDevices(true).forEach(function (d) {
Simon Hunt5724fb42015-02-05 16:59:40 -0800406 updDev(d, showOffline);
407 });
408 }
409 }
410
Simon Hunt1894d792015-02-04 17:09:20 -0800411
Simon Hunt445e8152015-02-06 13:00:12 -0800412 function sendUpdateMeta(d, clearPos) {
Simon Huntac4c6f72015-02-03 19:50:53 -0800413 var metaUi = {},
414 ll;
415
Simon Hunt445e8152015-02-06 13:00:12 -0800416 // if we are not clearing the position data (unpinning),
417 // attach the x, y, longitude, latitude...
418 if (!clearPos) {
Simon Hunt3a6eec02015-02-09 21:16:43 -0800419 ll = tms.lngLatFromCoord([d.x, d.y]);
Simon Huntdc6adea2015-02-09 22:29:36 -0800420 metaUi = {x: d.x, y: d.y, lng: ll[0], lat: ll[1]};
Simon Hunt1894d792015-02-04 17:09:20 -0800421 }
422 d.metaUi = metaUi;
Simon Hunt237676b52015-03-10 19:04:26 -0700423 wss.sendEvent('updateMeta', {
Simon Hunt1894d792015-02-04 17:09:20 -0800424 id: d.id,
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700425 class: d.class,
Simon Hunt1894d792015-02-04 17:09:20 -0800426 memento: metaUi
427 });
Simon Huntac4c6f72015-02-03 19:50:53 -0800428 }
429
Simon Hunt1894d792015-02-04 17:09:20 -0800430
Simon Huntac4c6f72015-02-03 19:50:53 -0800431 function mkSvgClass(d) {
432 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
433 }
434
Simon Hunt5724fb42015-02-05 16:59:40 -0800435 function vis(b) {
436 return b ? 'visible' : 'hidden';
437 }
438
Simon Huntfcbde892015-04-16 12:05:28 -0700439 function toggleHosts(x) {
440 var kev = (x === 'keyev'),
441 on = kev ? !showHosts : !!x;
442
443 showHosts = on;
Simon Hunt5724fb42015-02-05 16:59:40 -0800444 updateHostVisibility();
Simon Huntfcbde892015-04-16 12:05:28 -0700445 flash.flash('Hosts ' + vis(on));
446 return on;
Simon Hunt5724fb42015-02-05 16:59:40 -0800447 }
448
Simon Huntfcbde892015-04-16 12:05:28 -0700449 function toggleOffline(x) {
450 var kev = (x === 'keyev'),
451 on = kev ? !showOffline : !!x;
452
453 showOffline = on;
Simon Hunt5724fb42015-02-05 16:59:40 -0800454 updateOfflineVisibility();
Simon Huntfcbde892015-04-16 12:05:28 -0700455 flash.flash('Offline devices ' + vis(on));
456 return on;
Simon Hunt5724fb42015-02-05 16:59:40 -0800457 }
458
459 function cycleDeviceLabels() {
Bri Prebilic Cole9cf1a8d2015-04-21 13:15:29 -0700460 flash.flash(td3.incDevLabIndex());
Simon Huntdc6adea2015-02-09 22:29:36 -0800461 tms.findDevices().forEach(function (d) {
Simon Hunta4242de2015-02-24 17:11:55 -0800462 td3.updateDeviceLabel(d);
Simon Hunt1c367112015-02-05 18:02:46 -0800463 });
Simon Hunt5724fb42015-02-05 16:59:40 -0800464 }
465
Simon Hunt445e8152015-02-06 13:00:12 -0800466 function unpin() {
Simon Hunt08f841d02015-02-10 14:39:20 -0800467 var hov = tss.hovered();
468 if (hov) {
469 sendUpdateMeta(hov, true);
470 hov.fixed = false;
471 hov.el.classed('fixed', false);
Simon Hunt445e8152015-02-06 13:00:12 -0800472 fResume();
473 }
474 }
475
Simon Hunta142dd22015-02-12 22:07:51 -0800476 function showMastership(masterId) {
477 if (!masterId) {
478 restoreLayerState();
479 } else {
480 showMastershipFor(masterId);
481 }
482 }
483
484 function restoreLayerState() {
485 // NOTE: this level of indirection required, for when we have
486 // the layer filter functionality re-implemented
487 suppressLayers(false);
488 }
489
490 function showMastershipFor(id) {
491 suppressLayers(true);
492 node.each(function (n) {
493 if (n.master === id) {
Simon Hunt743a8492015-08-25 16:18:19 -0700494 n.el.classed('suppressedmax', false);
Simon Hunta142dd22015-02-12 22:07:51 -0800495 }
496 });
497 }
498
Simon Hunt743a8492015-08-25 16:18:19 -0700499 function supAmt(less) {
500 return less ? "suppressed" : "suppressedmax";
501 }
502
503 function suppressLayers(b, less) {
504 var cls = supAmt(less);
505 node.classed(cls, b);
506 link.classed(cls, b);
507 }
508
509 function unsuppressNode(id, less) {
510 var cls = supAmt(less);
511 node.each(function (n) {
512 if (n.id === id) {
513 n.el.classed(cls, false);
514 }
515 });
516 }
517
518 function unsuppressLink(id, less) {
519 var cls = supAmt(less);
520 link.each(function (n) {
521 if (n.id === id) {
522 n.el.classed(cls, false);
523 }
524 });
Simon Hunta142dd22015-02-12 22:07:51 -0800525 }
Simon Hunt445e8152015-02-06 13:00:12 -0800526
Simon Hunt86b7c882015-04-02 23:06:08 -0700527 function showBadLinks() {
528 var badLinks = tms.findBadLinks();
529 flash.flash('Bad Links: ' + badLinks.length);
530 $log.debug('Bad Link List (' + badLinks.length + '):');
531 badLinks.forEach(function (d) {
532 $log.debug('bad link: (' + d.bad + ') ' + d.key, d);
533 if (d.el) {
534 d.el.attr('stroke-width', linkScale(2.8))
535 .attr('stroke', 'red');
536 }
537 });
538 // back to normal after 2 seconds...
539 $timeout(updateLinks, 2000);
540 }
541
Simon Hunt5724fb42015-02-05 16:59:40 -0800542 // ==========================================
543
Simon Huntac4c6f72015-02-03 19:50:53 -0800544 function updateNodes() {
Thomas Vachuska1a989c12015-06-09 18:29:22 -0700545 if (fNodesTimer) {
546 $timeout.cancel(fNodesTimer);
547 }
548 fNodesTimer = $timeout(_updateNodes, 150);
549 }
550
Simon Hunta17fa672015-08-19 18:42:22 -0700551 // IMPLEMENTATION NOTE: _updateNodes() should NOT stop, start, or resume
552 // the force layout; that needs to be determined and implemented elsewhere
Thomas Vachuska1a989c12015-06-09 18:29:22 -0700553 function _updateNodes() {
Simon Hunt1894d792015-02-04 17:09:20 -0800554 // select all the nodes in the layout:
Simon Huntac4c6f72015-02-03 19:50:53 -0800555 node = nodeG.selectAll('.node')
556 .data(network.nodes, function (d) { return d.id; });
557
Simon Hunt1894d792015-02-04 17:09:20 -0800558 // operate on existing nodes:
Simon Hunta4242de2015-02-24 17:11:55 -0800559 node.filter('.device').each(td3.deviceExisting);
560 node.filter('.host').each(td3.hostExisting);
Simon Huntac4c6f72015-02-03 19:50:53 -0800561
562 // operate on entering nodes:
563 var entering = node.enter()
564 .append('g')
565 .attr({
566 id: function (d) { return sus.safeId(d.id); },
567 class: mkSvgClass,
Simon Hunta17fa672015-08-19 18:42:22 -0700568 transform: function (d) {
569 // Need to guard against NaN here ??
570 return sus.translate(d.x, d.y);
571 },
Simon Huntac4c6f72015-02-03 19:50:53 -0800572 opacity: 0
573 })
574 .call(drag)
Simon Hunt08f841d02015-02-10 14:39:20 -0800575 .on('mouseover', tss.nodeMouseOver)
576 .on('mouseout', tss.nodeMouseOut)
Simon Huntac4c6f72015-02-03 19:50:53 -0800577 .transition()
578 .attr('opacity', 1);
579
Simon Hunt1894d792015-02-04 17:09:20 -0800580 // augment entering nodes:
Simon Hunta4242de2015-02-24 17:11:55 -0800581 entering.filter('.device').each(td3.deviceEnter);
582 entering.filter('.host').each(td3.hostEnter);
Simon Huntac4c6f72015-02-03 19:50:53 -0800583
Simon Hunt51056592015-02-03 21:48:07 -0800584 // operate on both existing and new nodes:
Simon Hunta4242de2015-02-24 17:11:55 -0800585 td3.updateDeviceColors();
Simon Huntac4c6f72015-02-03 19:50:53 -0800586
587 // operate on exiting nodes:
588 // Note that the node is removed after 2 seconds.
589 // Sub element animations should be shorter than 2 seconds.
590 var exiting = node.exit()
591 .transition()
592 .duration(2000)
593 .style('opacity', 0)
594 .remove();
595
Simon Hunt1894d792015-02-04 17:09:20 -0800596 // exiting node specifics:
Simon Hunta4242de2015-02-24 17:11:55 -0800597 exiting.filter('.host').each(td3.hostExit);
598 exiting.filter('.device').each(td3.deviceExit);
Simon Huntac4c6f72015-02-03 19:50:53 -0800599 }
600
Simon Hunt51056592015-02-03 21:48:07 -0800601 // ==========================
Simon Hunt1894d792015-02-04 17:09:20 -0800602
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700603 function getDefaultPos(link) {
604 return {
605 x1: link.source.x,
606 y1: link.source.y,
607 x2: link.target.x,
608 y2: link.target.y
609 };
610 }
611
612 // returns amount of adjustment along the normal for given link
613 function amt(numLinks, linkIdx) {
614 var gap = 6;
615 return (linkIdx - ((numLinks - 1) / 2)) * gap;
616 }
617
618 function calcMovement(d, amt, flipped) {
619 var pos = getDefaultPos(d),
620 mult = flipped ? -amt : amt,
621 dx = pos.x2 - pos.x1,
622 dy = pos.y2 - pos.y1,
623 length = Math.sqrt((dx * dx) + (dy * dy));
624
625 return {
626 x1: pos.x1 + (mult * dy / length),
627 y1: pos.y1 + (mult * -dx / length),
628 x2: pos.x2 + (mult * dy / length),
629 y2: pos.y2 + (mult * -dx / length)
630 };
631 }
632
633 function calcPosition() {
634 var lines = this,
635 linkSrcId;
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700636 linkNums = [];
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700637 lines.each(function (d) {
638 if (d.type() === 'hostLink') {
639 d.position = getDefaultPos(d);
640 }
641 });
642
643 function normalizeLinkSrc(link) {
644 // ensure source device is consistent across set of links
645 // temporary measure until link modeling is refactored
646 if (!linkSrcId) {
647 linkSrcId = link.source.id;
648 return false;
649 }
650
651 return link.source.id !== linkSrcId;
652 }
653
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700654 angular.forEach(network.linksByDevice, function (linkArr, key) {
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700655 var numLinks = linkArr.length,
656 link;
657
658 if (numLinks === 1) {
659 link = linkArr[0];
660 link.position = getDefaultPos(link);
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700661 link.position.multiLink = false;
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700662 } else if (numLinks >= 5) {
663 // this code is inefficient, in the future the way links
664 // are modeled will be changed
665 angular.forEach(linkArr, function (link) {
666 link.position = getDefaultPos(link);
667 link.position.multiLink = true;
668 });
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700669 linkNums.push({
670 id: key,
671 num: numLinks,
672 linkCoords: linkArr[0].position
673 });
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700674 } else {
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700675 linkSrcId = null;
676 angular.forEach(linkArr, function (link, index) {
677 var offsetAmt = amt(numLinks, index),
678 needToFlip = normalizeLinkSrc(link);
679 link.position = calcMovement(link, offsetAmt, needToFlip);
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700680 link.position.multiLink = false;
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700681 });
682 }
683 });
684 }
685
Simon Hunt1894d792015-02-04 17:09:20 -0800686 function updateLinks() {
Thomas Vachuska1a989c12015-06-09 18:29:22 -0700687 if (fLinksTimer) {
688 $timeout.cancel(fLinksTimer);
689 }
690 fLinksTimer = $timeout(_updateLinks, 150);
691 }
692
Simon Hunta17fa672015-08-19 18:42:22 -0700693 // IMPLEMENTATION NOTE: _updateLinks() should NOT stop, start, or resume
694 // the force layout; that needs to be determined and implemented elsewhere
Thomas Vachuska1a989c12015-06-09 18:29:22 -0700695 function _updateLinks() {
Simon Hunt1894d792015-02-04 17:09:20 -0800696 var th = ts.theme();
697
698 link = linkG.selectAll('.link')
699 .data(network.links, function (d) { return d.key; });
700
701 // operate on existing links:
Simon Huntd5264122015-02-25 10:17:43 -0800702 link.each(function (d) {
703 // this is supposed to be an existing link, but we have observed
704 // occasions (where links are deleted and added rapidly?) where
705 // the DOM element has not been defined. So protect against that...
706 if (d.el) {
707 restyleLinkElement(d, true);
708 }
709 });
Simon Hunt1894d792015-02-04 17:09:20 -0800710
711 // operate on entering links:
712 var entering = link.enter()
713 .append('line')
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700714 .call(calcPosition)
Simon Hunt1894d792015-02-04 17:09:20 -0800715 .attr({
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700716 x1: function (d) { return d.position.x1; },
717 y1: function (d) { return d.position.y1; },
718 x2: function (d) { return d.position.x2; },
719 y2: function (d) { return d.position.y2; },
Simon Hunt1894d792015-02-04 17:09:20 -0800720 stroke: linkConfig[th].inColor,
721 'stroke-width': linkConfig.inWidth
722 });
723
724 // augment links
Simon Hunta4242de2015-02-24 17:11:55 -0800725 entering.each(td3.linkEntering);
Simon Hunt1894d792015-02-04 17:09:20 -0800726
727 // operate on both existing and new links:
728 //link.each(...)
729
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700730 // add labels for how many links are in a thick line
731 td3.applyNumLinkLabels(linkNums, numLinkLblsG);
732
Simon Hunt1894d792015-02-04 17:09:20 -0800733 // apply or remove labels
Simon Hunta4242de2015-02-24 17:11:55 -0800734 td3.applyLinkLabels();
Simon Hunt1894d792015-02-04 17:09:20 -0800735
736 // operate on exiting links:
737 link.exit()
738 .attr('stroke-dasharray', '3 3')
Simon Hunt5724fb42015-02-05 16:59:40 -0800739 .attr('stroke', linkConfig[th].outColor)
Simon Hunt1894d792015-02-04 17:09:20 -0800740 .style('opacity', 0.5)
741 .transition()
742 .duration(1500)
743 .attr({
744 'stroke-dasharray': '3 12',
Simon Hunt1894d792015-02-04 17:09:20 -0800745 'stroke-width': linkConfig.outWidth
746 })
747 .style('opacity', 0.0)
748 .remove();
Simon Hunt1894d792015-02-04 17:09:20 -0800749 }
750
Simon Huntac4c6f72015-02-03 19:50:53 -0800751
752 // ==========================
Simon Hunt737c89f2015-01-28 12:23:19 -0800753 // force layout tick function
Simon Hunt737c89f2015-01-28 12:23:19 -0800754
Simon Hunt5724fb42015-02-05 16:59:40 -0800755 function fResume() {
Simon Huntc3c5b672015-02-20 11:32:13 -0800756 if (!tos.isOblique()) {
Simon Hunt5724fb42015-02-05 16:59:40 -0800757 force.resume();
758 }
759 }
760
761 function fStart() {
Simon Huntc3c5b672015-02-20 11:32:13 -0800762 if (!tos.isOblique()) {
Simon Hunta17fa672015-08-19 18:42:22 -0700763 if (fTimer) {
764 $timeout.cancel(fTimer);
765 }
766 fTimer = $timeout(function () {
767 $log.debug("Starting force-layout");
768 force.start();
769 }, 200);
Simon Hunt5724fb42015-02-05 16:59:40 -0800770 }
771 }
772
773 var tickStuff = {
774 nodeAttr: {
Simon Hunta17fa672015-08-19 18:42:22 -0700775 transform: function (d) {
776 var dx = isNaN(d.x) ? 0 : d.x,
777 dy = isNaN(d.y) ? 0 : d.y;
778 return sus.translate(dx, dy);
779 }
Simon Hunt5724fb42015-02-05 16:59:40 -0800780 },
781 linkAttr: {
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700782 x1: function (d) { return d.position.x1; },
783 y1: function (d) { return d.position.y1; },
784 x2: function (d) { return d.position.x2; },
785 y2: function (d) { return d.position.y2; }
Simon Hunt5724fb42015-02-05 16:59:40 -0800786 },
787 linkLabelAttr: {
788 transform: function (d) {
Simon Huntdc6adea2015-02-09 22:29:36 -0800789 var lnk = tms.findLinkById(d.key);
Simon Hunt5724fb42015-02-05 16:59:40 -0800790 if (lnk) {
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700791 return td3.transformLabel(lnk.position);
Simon Hunt5724fb42015-02-05 16:59:40 -0800792 }
793 }
794 }
795 };
796
797 function tick() {
Simon Hunt3ab20282015-02-26 20:32:19 -0800798 // guard against null (which can happen when our view pages out)...
Bri Prebilic Cole8d003782015-07-31 15:33:06 -0700799 if (node && node.size()) {
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700800 node.attr(tickStuff.nodeAttr);
801 }
Bri Prebilic Cole8d003782015-07-31 15:33:06 -0700802 if (link && link.size()) {
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700803 link.call(calcPosition)
804 .attr(tickStuff.linkAttr);
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700805 td3.applyNumLinkLabels(linkNums, numLinkLblsG);
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700806 }
Bri Prebilic Cole8d003782015-07-31 15:33:06 -0700807 if (linkLabel && linkLabel.size()) {
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700808 linkLabel.attr(tickStuff.linkLabelAttr);
809 }
Simon Hunt737c89f2015-01-28 12:23:19 -0800810 }
811
812
Simon Huntac4c6f72015-02-03 19:50:53 -0800813 // ==========================
814 // === MOUSE GESTURE HANDLERS
815
Simon Hunt205099e2015-02-07 13:12:01 -0800816 function zoomingOrPanning(ev) {
817 return ev.metaKey || ev.altKey;
Simon Hunt445e8152015-02-06 13:00:12 -0800818 }
819
820 function atDragEnd(d) {
821 // once we've finished moving, pin the node in position
822 d.fixed = true;
823 d3.select(this).classed('fixed', true);
824 sendUpdateMeta(d);
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700825 tss.clickConsumed(true);
Simon Hunt445e8152015-02-06 13:00:12 -0800826 }
827
828 // predicate that indicates when dragging is active
829 function dragEnabled() {
830 var ev = d3.event.sourceEvent;
831 // nodeLock means we aren't allowing nodes to be dragged...
Simon Hunt205099e2015-02-07 13:12:01 -0800832 return !nodeLock && !zoomingOrPanning(ev);
Simon Hunt445e8152015-02-06 13:00:12 -0800833 }
834
835 // predicate that indicates when clicking is active
836 function clickEnabled() {
837 return true;
838 }
Simon Hunt737c89f2015-01-28 12:23:19 -0800839
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700840 // =============================================
841 // function entry points for overlay module
Simon Huntf542d842015-02-11 16:20:33 -0800842
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700843 // TODO: find an automatic way of tracking via the "showHighlights" events
Simon Hunte50829c2015-06-09 08:39:28 -0700844 var allTrafficClasses = 'primary secondary optical animated ' +
845 'port-traffic-Kbps port-traffic-Mbps port-traffic-Gbps ' +
846 'port-traffic-Gbps-choked';
Simon Huntf542d842015-02-11 16:20:33 -0800847
848 function clearLinkTrafficStyle() {
849 link.style('stroke-width', null)
850 .classed(allTrafficClasses, false);
851 }
852
853 function removeLinkLabels() {
854 network.links.forEach(function (d) {
855 d.label = '';
856 });
857 }
Simon Hunt737c89f2015-01-28 12:23:19 -0800858
Simon Hunta4242de2015-02-24 17:11:55 -0800859 function updateLinkLabelModel() {
860 // create the backing data for showing labels..
861 var data = [];
862 link.each(function (d) {
863 if (d.label) {
864 data.push({
865 id: 'lab-' + d.key,
866 key: d.key,
867 label: d.label,
868 ldata: d
869 });
870 }
871 });
872
873 linkLabel = linkLabelG.selectAll('.linkLabel')
874 .data(data, function (d) { return d.id; });
875 }
876
Simon Hunt737c89f2015-01-28 12:23:19 -0800877 // ==========================
Simon Huntac4c6f72015-02-03 19:50:53 -0800878 // Module definition
Simon Hunt737c89f2015-01-28 12:23:19 -0800879
Simon Huntdc6adea2015-02-09 22:29:36 -0800880 function mkModelApi(uplink) {
881 return {
882 projection: uplink.projection,
883 network: network,
884 restyleLinkElement: restyleLinkElement,
885 removeLinkElement: removeLinkElement
886 };
887 }
888
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700889 function mkD3Api() {
Simon Hunta4242de2015-02-24 17:11:55 -0800890 return {
891 node: function () { return node; },
892 link: function () { return link; },
893 linkLabel: function () { return linkLabel; },
894 instVisible: function () { return tis.isVisible(); },
895 posNode: tms.positionNode,
896 showHosts: function () { return showHosts; },
897 restyleLinkElement: restyleLinkElement,
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700898 updateLinkLabelModel: updateLinkLabelModel,
899 linkConfig: function () { return linkConfig; }
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700900 };
Simon Hunta4242de2015-02-24 17:11:55 -0800901 }
902
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700903 function mkSelectApi() {
Simon Hunt08f841d02015-02-10 14:39:20 -0800904 return {
905 node: function () { return node; },
906 zoomingOrPanning: zoomingOrPanning,
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700907 updateDeviceColors: td3.updateDeviceColors,
908 deselectLink: tls.deselectLink
Simon Hunt08f841d02015-02-10 14:39:20 -0800909 };
910 }
911
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700912 function mkTrafficApi() {
913 return {
914 hovered: tss.hovered,
915 somethingSelected: tss.somethingSelected,
916 selectOrder: tss.selectOrder
917 };
918 }
919
920 function mkOverlayApi() {
Simon Huntf542d842015-02-11 16:20:33 -0800921 return {
922 clearLinkTrafficStyle: clearLinkTrafficStyle,
923 removeLinkLabels: removeLinkLabels,
Simon Hunt743a8492015-08-25 16:18:19 -0700924 findLinkById: tms.findLinkById,
Simon Huntf542d842015-02-11 16:20:33 -0800925 updateLinks: updateLinks,
Simon Hunt743a8492015-08-25 16:18:19 -0700926 updateNodes: updateNodes,
927 supLayers: suppressLayers,
928 unsupNode: unsuppressNode,
929 unsupLink: unsuppressLink
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700930 };
Simon Huntf542d842015-02-11 16:20:33 -0800931 }
932
Simon Huntc3c5b672015-02-20 11:32:13 -0800933 function mkObliqueApi(uplink, fltr) {
Simon Hunt96f88c62015-02-19 17:57:25 -0800934 return {
Simon Huntc3c5b672015-02-20 11:32:13 -0800935 force: function() { return force; },
936 zoomLayer: uplink.zoomLayer,
937 nodeGBBox: function() { return nodeG.node().getBBox(); },
Simon Hunt96f88c62015-02-19 17:57:25 -0800938 node: function () { return node; },
Simon Huntc3c5b672015-02-20 11:32:13 -0800939 link: function () { return link; },
940 linkLabel: function () { return linkLabel; },
941 nodes: function () { return network.nodes; },
942 tickStuff: tickStuff,
943 nodeLock: function (b) {
944 var old = nodeLock;
945 nodeLock = b;
946 return old;
947 },
948 opacifyMap: uplink.opacifyMap,
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700949 inLayer: fltr.inLayer,
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700950 calcLinkPos: calcPosition,
951 applyNumLinkLabels: function () {
952 td3.applyNumLinkLabels(linkNums, numLinkLblsG);
953 }
Simon Hunt96f88c62015-02-19 17:57:25 -0800954 };
955 }
956
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700957 function mkFilterApi() {
Simon Hunteb0fa052015-02-17 19:20:28 -0800958 return {
959 node: function () { return node; },
960 link: function () { return link; }
961 };
962 }
963
Simon Hunt9e2104c2015-02-26 10:48:59 -0800964 function mkLinkApi(svg, uplink) {
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800965 return {
966 svg: svg,
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800967 zoomer: uplink.zoomer(),
968 network: network,
Simon Hunt1a5301e2015-02-25 15:31:25 -0800969 portLabelG: function () { return portLabelG; },
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800970 showHosts: function () { return showHosts; }
971 };
972 }
973
Simon Hunt737c89f2015-01-28 12:23:19 -0800974 angular.module('ovTopo')
975 .factory('TopoForceService',
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700976 ['$log', '$timeout', 'FnService', 'SvgUtilService',
Simon Hunt86b7c882015-04-02 23:06:08 -0700977 'ThemeService', 'FlashService', 'WebSocketService',
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700978 'TopoOverlayService', 'TopoInstService', 'TopoModelService',
Simon Hunta4242de2015-02-24 17:11:55 -0800979 'TopoD3Service', 'TopoSelectService', 'TopoTrafficService',
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800980 'TopoObliqueService', 'TopoFilterService', 'TopoLinkService',
Simon Hunt737c89f2015-01-28 12:23:19 -0800981
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700982 function (_$log_, _$timeout_, _fs_, _sus_, _ts_, _flash_, _wss_, _tov_,
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800983 _tis_, _tms_, _td3_, _tss_, _tts_, _tos_, _fltr_, _tls_) {
Simon Hunt737c89f2015-01-28 12:23:19 -0800984 $log = _$log_;
Simon Hunt86b7c882015-04-02 23:06:08 -0700985 $timeout = _$timeout_;
Simon Hunt1894d792015-02-04 17:09:20 -0800986 fs = _fs_;
Simon Hunt737c89f2015-01-28 12:23:19 -0800987 sus = _sus_;
Simon Huntac4c6f72015-02-03 19:50:53 -0800988 ts = _ts_;
Simon Hunt5724fb42015-02-05 16:59:40 -0800989 flash = _flash_;
Simon Hunt237676b52015-03-10 19:04:26 -0700990 wss = _wss_;
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700991 tov = _tov_;
Simon Huntac4c6f72015-02-03 19:50:53 -0800992 tis = _tis_;
Simon Hunt3a6eec02015-02-09 21:16:43 -0800993 tms = _tms_;
Simon Hunta4242de2015-02-24 17:11:55 -0800994 td3 = _td3_;
Simon Hunt08f841d02015-02-10 14:39:20 -0800995 tss = _tss_;
Simon Huntf542d842015-02-11 16:20:33 -0800996 tts = _tts_;
Simon Hunt96f88c62015-02-19 17:57:25 -0800997 tos = _tos_;
Simon Hunteb0fa052015-02-17 19:20:28 -0800998 fltr = _fltr_;
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800999 tls = _tls_;
Simon Hunt737c89f2015-01-28 12:23:19 -08001000
Simon Hunta142dd22015-02-12 22:07:51 -08001001 var themeListener = ts.addListener(function () {
1002 updateLinks();
1003 updateNodes();
1004 });
1005
Simon Hunt737c89f2015-01-28 12:23:19 -08001006 // forceG is the SVG group to display the force layout in
Simon Huntdc6adea2015-02-09 22:29:36 -08001007 // uplink is the api from the main topo source file
Simon Hunt3a6eec02015-02-09 21:16:43 -08001008 // dim is the initial dimensions of the SVG as [w,h]
Simon Hunt737c89f2015-01-28 12:23:19 -08001009 // opts are, well, optional :)
Simon Hunt3ab20282015-02-26 20:32:19 -08001010 function initForce(_svg_, forceG, _uplink_, _dim_, opts) {
Simon Hunt1894d792015-02-04 17:09:20 -08001011 uplink = _uplink_;
Simon Hunt3a6eec02015-02-09 21:16:43 -08001012 dim = _dim_;
Simon Hunt3ab20282015-02-26 20:32:19 -08001013 svg = _svg_;
1014
1015 lu = network.lookup;
1016 rlk = network.revLinkToKey;
Simon Hunt3a6eec02015-02-09 21:16:43 -08001017
1018 $log.debug('initForce().. dim = ' + dim);
1019
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001020 tov.setApi(mkOverlayApi(), tss);
Simon Huntdc6adea2015-02-09 22:29:36 -08001021 tms.initModel(mkModelApi(uplink), dim);
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001022 td3.initD3(mkD3Api());
1023 tss.initSelect(mkSelectApi());
1024 tts.initTraffic(mkTrafficApi());
Simon Huntc3c5b672015-02-20 11:32:13 -08001025 tos.initOblique(mkObliqueApi(uplink, fltr));
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001026 fltr.initFilter(mkFilterApi());
Simon Hunt9e2104c2015-02-26 10:48:59 -08001027 tls.initLink(mkLinkApi(svg, uplink), td3);
Simon Hunta11b4eb2015-01-28 16:20:50 -08001028
Simon Hunt737c89f2015-01-28 12:23:19 -08001029 settings = angular.extend({}, defaultSettings, opts);
1030
1031 linkG = forceG.append('g').attr('id', 'topo-links');
1032 linkLabelG = forceG.append('g').attr('id', 'topo-linkLabels');
Bri Prebilic Cole80401762015-07-16 11:36:18 -07001033 numLinkLblsG = forceG.append('g').attr('id', 'topo-numLinkLabels');
Simon Hunt737c89f2015-01-28 12:23:19 -08001034 nodeG = forceG.append('g').attr('id', 'topo-nodes');
Simon Hunt1a5301e2015-02-25 15:31:25 -08001035 portLabelG = forceG.append('g').attr('id', 'topo-portLabels');
Simon Hunt737c89f2015-01-28 12:23:19 -08001036
1037 link = linkG.selectAll('.link');
1038 linkLabel = linkLabelG.selectAll('.linkLabel');
1039 node = nodeG.selectAll('.node');
1040
1041 force = d3.layout.force()
Simon Hunt3a6eec02015-02-09 21:16:43 -08001042 .size(dim)
Simon Hunt737c89f2015-01-28 12:23:19 -08001043 .nodes(network.nodes)
1044 .links(network.links)
1045 .gravity(settings.gravity)
1046 .friction(settings.friction)
1047 .charge(settings.charge._def_)
1048 .linkDistance(settings.linkDistance._def_)
1049 .linkStrength(settings.linkStrength._def_)
1050 .on('tick', tick);
1051
1052 drag = sus.createDragBehavior(force,
Simon Hunt08f841d02015-02-10 14:39:20 -08001053 tss.selectObject, atDragEnd, dragEnabled, clickEnabled);
Simon Hunt737c89f2015-01-28 12:23:19 -08001054 }
1055
Simon Hunt3a6eec02015-02-09 21:16:43 -08001056 function newDim(_dim_) {
1057 dim = _dim_;
1058 force.size(dim);
1059 tms.newDim(dim);
Simon Hunt737c89f2015-01-28 12:23:19 -08001060 }
1061
Simon Hunt3a6eec02015-02-09 21:16:43 -08001062 function destroyForce() {
Simon Hunt3ab20282015-02-26 20:32:19 -08001063 force.stop();
1064
Simon Huntfb8ea1f2015-02-24 21:38:09 -08001065 tls.destroyLink();
Simon Hunt96f88c62015-02-19 17:57:25 -08001066 tos.destroyOblique();
Simon Huntf542d842015-02-11 16:20:33 -08001067 tts.destroyTraffic();
1068 tss.destroySelect();
Simon Hunta4242de2015-02-24 17:11:55 -08001069 td3.destroyD3();
Simon Huntf542d842015-02-11 16:20:33 -08001070 tms.destroyModel();
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001071 // note: no need to destroy overlay service
Simon Hunta142dd22015-02-12 22:07:51 -08001072 ts.removeListener(themeListener);
1073 themeListener = null;
Simon Hunt3ab20282015-02-26 20:32:19 -08001074
1075 // clean up the DOM
1076 svg.selectAll('g').remove();
1077 svg.selectAll('defs').remove();
1078
1079 // clean up internal state
1080 network.nodes = [];
1081 network.links = [];
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -07001082 network.linksByDevice = {};
Simon Hunt3ab20282015-02-26 20:32:19 -08001083 network.lookup = {};
1084 network.revLinkToKey = {};
1085
Bri Prebilic Cole80401762015-07-16 11:36:18 -07001086 linkNums = [];
1087
1088 linkG = linkLabelG = numLinkLblsG = nodeG = portLabelG = null;
Simon Hunt3ab20282015-02-26 20:32:19 -08001089 link = linkLabel = node = null;
1090 force = drag = null;
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -07001091
1092 // clean up $timeout promises
Simon Hunta17fa672015-08-19 18:42:22 -07001093 if (fTimer) {
1094 $timeout.cancel(fTimer);
1095 }
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -07001096 if (fNodesTimer) {
1097 $timeout.cancel(fNodesTimer);
1098 }
1099 if (fLinksTimer) {
1100 $timeout.cancel(fLinksTimer);
1101 }
Simon Hunt3a6eec02015-02-09 21:16:43 -08001102 }
1103
Simon Hunt737c89f2015-01-28 12:23:19 -08001104 return {
1105 initForce: initForce,
Simon Hunt3a6eec02015-02-09 21:16:43 -08001106 newDim: newDim,
1107 destroyForce: destroyForce,
Simon Huntac4c6f72015-02-03 19:50:53 -08001108
Simon Hunta4242de2015-02-24 17:11:55 -08001109 updateDeviceColors: td3.updateDeviceColors,
Simon Hunt5724fb42015-02-05 16:59:40 -08001110 toggleHosts: toggleHosts,
Simon Hunt9e2104c2015-02-26 10:48:59 -08001111 togglePorts: tls.togglePorts,
Simon Hunt5724fb42015-02-05 16:59:40 -08001112 toggleOffline: toggleOffline,
1113 cycleDeviceLabels: cycleDeviceLabels,
Simon Hunt445e8152015-02-06 13:00:12 -08001114 unpin: unpin,
Simon Hunta142dd22015-02-12 22:07:51 -08001115 showMastership: showMastership,
Simon Hunt86b7c882015-04-02 23:06:08 -07001116 showBadLinks: showBadLinks,
Simon Huntac4c6f72015-02-03 19:50:53 -08001117
1118 addDevice: addDevice,
Simon Hunt1894d792015-02-04 17:09:20 -08001119 updateDevice: updateDevice,
1120 removeDevice: removeDevice,
1121 addHost: addHost,
1122 updateHost: updateHost,
1123 removeHost: removeHost,
1124 addLink: addLink,
1125 updateLink: updateLink,
1126 removeLink: removeLink
Simon Hunt737c89f2015-01-28 12:23:19 -08001127 };
1128 }]);
1129}());