blob: f3c053e74b10da30f37eee3e4386b9c0eed61414 [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) {
494 n.el.classed('suppressed', false);
495 }
496 });
497 }
498
499 function suppressLayers(b) {
500 node.classed('suppressed', b);
501 link.classed('suppressed', b);
502// d3.selectAll('svg .port').classed('inactive', b);
503// d3.selectAll('svg .portText').classed('inactive', b);
504 }
Simon Hunt445e8152015-02-06 13:00:12 -0800505
Simon Hunt86b7c882015-04-02 23:06:08 -0700506 function showBadLinks() {
507 var badLinks = tms.findBadLinks();
508 flash.flash('Bad Links: ' + badLinks.length);
509 $log.debug('Bad Link List (' + badLinks.length + '):');
510 badLinks.forEach(function (d) {
511 $log.debug('bad link: (' + d.bad + ') ' + d.key, d);
512 if (d.el) {
513 d.el.attr('stroke-width', linkScale(2.8))
514 .attr('stroke', 'red');
515 }
516 });
517 // back to normal after 2 seconds...
518 $timeout(updateLinks, 2000);
519 }
520
Simon Hunt5724fb42015-02-05 16:59:40 -0800521 // ==========================================
522
Simon Huntac4c6f72015-02-03 19:50:53 -0800523 function updateNodes() {
Thomas Vachuska1a989c12015-06-09 18:29:22 -0700524 if (fNodesTimer) {
525 $timeout.cancel(fNodesTimer);
526 }
527 fNodesTimer = $timeout(_updateNodes, 150);
528 }
529
Simon Hunta17fa672015-08-19 18:42:22 -0700530 // IMPLEMENTATION NOTE: _updateNodes() should NOT stop, start, or resume
531 // the force layout; that needs to be determined and implemented elsewhere
Thomas Vachuska1a989c12015-06-09 18:29:22 -0700532 function _updateNodes() {
Simon Hunt1894d792015-02-04 17:09:20 -0800533 // select all the nodes in the layout:
Simon Huntac4c6f72015-02-03 19:50:53 -0800534 node = nodeG.selectAll('.node')
535 .data(network.nodes, function (d) { return d.id; });
536
Simon Hunt1894d792015-02-04 17:09:20 -0800537 // operate on existing nodes:
Simon Hunta4242de2015-02-24 17:11:55 -0800538 node.filter('.device').each(td3.deviceExisting);
539 node.filter('.host').each(td3.hostExisting);
Simon Huntac4c6f72015-02-03 19:50:53 -0800540
541 // operate on entering nodes:
542 var entering = node.enter()
543 .append('g')
544 .attr({
545 id: function (d) { return sus.safeId(d.id); },
546 class: mkSvgClass,
Simon Hunta17fa672015-08-19 18:42:22 -0700547 transform: function (d) {
548 // Need to guard against NaN here ??
549 return sus.translate(d.x, d.y);
550 },
Simon Huntac4c6f72015-02-03 19:50:53 -0800551 opacity: 0
552 })
553 .call(drag)
Simon Hunt08f841d02015-02-10 14:39:20 -0800554 .on('mouseover', tss.nodeMouseOver)
555 .on('mouseout', tss.nodeMouseOut)
Simon Huntac4c6f72015-02-03 19:50:53 -0800556 .transition()
557 .attr('opacity', 1);
558
Simon Hunt1894d792015-02-04 17:09:20 -0800559 // augment entering nodes:
Simon Hunta4242de2015-02-24 17:11:55 -0800560 entering.filter('.device').each(td3.deviceEnter);
561 entering.filter('.host').each(td3.hostEnter);
Simon Huntac4c6f72015-02-03 19:50:53 -0800562
Simon Hunt51056592015-02-03 21:48:07 -0800563 // operate on both existing and new nodes:
Simon Hunta4242de2015-02-24 17:11:55 -0800564 td3.updateDeviceColors();
Simon Huntac4c6f72015-02-03 19:50:53 -0800565
566 // operate on exiting nodes:
567 // Note that the node is removed after 2 seconds.
568 // Sub element animations should be shorter than 2 seconds.
569 var exiting = node.exit()
570 .transition()
571 .duration(2000)
572 .style('opacity', 0)
573 .remove();
574
Simon Hunt1894d792015-02-04 17:09:20 -0800575 // exiting node specifics:
Simon Hunta4242de2015-02-24 17:11:55 -0800576 exiting.filter('.host').each(td3.hostExit);
577 exiting.filter('.device').each(td3.deviceExit);
Simon Huntac4c6f72015-02-03 19:50:53 -0800578 }
579
Simon Hunt51056592015-02-03 21:48:07 -0800580 // ==========================
Simon Hunt1894d792015-02-04 17:09:20 -0800581
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700582 function getDefaultPos(link) {
583 return {
584 x1: link.source.x,
585 y1: link.source.y,
586 x2: link.target.x,
587 y2: link.target.y
588 };
589 }
590
591 // returns amount of adjustment along the normal for given link
592 function amt(numLinks, linkIdx) {
593 var gap = 6;
594 return (linkIdx - ((numLinks - 1) / 2)) * gap;
595 }
596
597 function calcMovement(d, amt, flipped) {
598 var pos = getDefaultPos(d),
599 mult = flipped ? -amt : amt,
600 dx = pos.x2 - pos.x1,
601 dy = pos.y2 - pos.y1,
602 length = Math.sqrt((dx * dx) + (dy * dy));
603
604 return {
605 x1: pos.x1 + (mult * dy / length),
606 y1: pos.y1 + (mult * -dx / length),
607 x2: pos.x2 + (mult * dy / length),
608 y2: pos.y2 + (mult * -dx / length)
609 };
610 }
611
612 function calcPosition() {
613 var lines = this,
614 linkSrcId;
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700615 linkNums = [];
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700616 lines.each(function (d) {
617 if (d.type() === 'hostLink') {
618 d.position = getDefaultPos(d);
619 }
620 });
621
622 function normalizeLinkSrc(link) {
623 // ensure source device is consistent across set of links
624 // temporary measure until link modeling is refactored
625 if (!linkSrcId) {
626 linkSrcId = link.source.id;
627 return false;
628 }
629
630 return link.source.id !== linkSrcId;
631 }
632
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700633 angular.forEach(network.linksByDevice, function (linkArr, key) {
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700634 var numLinks = linkArr.length,
635 link;
636
637 if (numLinks === 1) {
638 link = linkArr[0];
639 link.position = getDefaultPos(link);
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700640 link.position.multiLink = false;
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700641 } else if (numLinks >= 5) {
642 // this code is inefficient, in the future the way links
643 // are modeled will be changed
644 angular.forEach(linkArr, function (link) {
645 link.position = getDefaultPos(link);
646 link.position.multiLink = true;
647 });
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700648 linkNums.push({
649 id: key,
650 num: numLinks,
651 linkCoords: linkArr[0].position
652 });
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700653 } else {
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700654 linkSrcId = null;
655 angular.forEach(linkArr, function (link, index) {
656 var offsetAmt = amt(numLinks, index),
657 needToFlip = normalizeLinkSrc(link);
658 link.position = calcMovement(link, offsetAmt, needToFlip);
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700659 link.position.multiLink = false;
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700660 });
661 }
662 });
663 }
664
Simon Hunt1894d792015-02-04 17:09:20 -0800665 function updateLinks() {
Thomas Vachuska1a989c12015-06-09 18:29:22 -0700666 if (fLinksTimer) {
667 $timeout.cancel(fLinksTimer);
668 }
669 fLinksTimer = $timeout(_updateLinks, 150);
670 }
671
Simon Hunta17fa672015-08-19 18:42:22 -0700672 // IMPLEMENTATION NOTE: _updateLinks() should NOT stop, start, or resume
673 // the force layout; that needs to be determined and implemented elsewhere
Thomas Vachuska1a989c12015-06-09 18:29:22 -0700674 function _updateLinks() {
Simon Hunt1894d792015-02-04 17:09:20 -0800675 var th = ts.theme();
676
677 link = linkG.selectAll('.link')
678 .data(network.links, function (d) { return d.key; });
679
680 // operate on existing links:
Simon Huntd5264122015-02-25 10:17:43 -0800681 link.each(function (d) {
682 // this is supposed to be an existing link, but we have observed
683 // occasions (where links are deleted and added rapidly?) where
684 // the DOM element has not been defined. So protect against that...
685 if (d.el) {
686 restyleLinkElement(d, true);
687 }
688 });
Simon Hunt1894d792015-02-04 17:09:20 -0800689
690 // operate on entering links:
691 var entering = link.enter()
692 .append('line')
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700693 .call(calcPosition)
Simon Hunt1894d792015-02-04 17:09:20 -0800694 .attr({
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700695 x1: function (d) { return d.position.x1; },
696 y1: function (d) { return d.position.y1; },
697 x2: function (d) { return d.position.x2; },
698 y2: function (d) { return d.position.y2; },
Simon Hunt1894d792015-02-04 17:09:20 -0800699 stroke: linkConfig[th].inColor,
700 'stroke-width': linkConfig.inWidth
701 });
702
703 // augment links
Simon Hunta4242de2015-02-24 17:11:55 -0800704 entering.each(td3.linkEntering);
Simon Hunt1894d792015-02-04 17:09:20 -0800705
706 // operate on both existing and new links:
707 //link.each(...)
708
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700709 // add labels for how many links are in a thick line
710 td3.applyNumLinkLabels(linkNums, numLinkLblsG);
711
Simon Hunt1894d792015-02-04 17:09:20 -0800712 // apply or remove labels
Simon Hunta4242de2015-02-24 17:11:55 -0800713 td3.applyLinkLabels();
Simon Hunt1894d792015-02-04 17:09:20 -0800714
715 // operate on exiting links:
716 link.exit()
717 .attr('stroke-dasharray', '3 3')
Simon Hunt5724fb42015-02-05 16:59:40 -0800718 .attr('stroke', linkConfig[th].outColor)
Simon Hunt1894d792015-02-04 17:09:20 -0800719 .style('opacity', 0.5)
720 .transition()
721 .duration(1500)
722 .attr({
723 'stroke-dasharray': '3 12',
Simon Hunt1894d792015-02-04 17:09:20 -0800724 'stroke-width': linkConfig.outWidth
725 })
726 .style('opacity', 0.0)
727 .remove();
Simon Hunt1894d792015-02-04 17:09:20 -0800728 }
729
Simon Huntac4c6f72015-02-03 19:50:53 -0800730
731 // ==========================
Simon Hunt737c89f2015-01-28 12:23:19 -0800732 // force layout tick function
Simon Hunt737c89f2015-01-28 12:23:19 -0800733
Simon Hunt5724fb42015-02-05 16:59:40 -0800734 function fResume() {
Simon Huntc3c5b672015-02-20 11:32:13 -0800735 if (!tos.isOblique()) {
Simon Hunt5724fb42015-02-05 16:59:40 -0800736 force.resume();
737 }
738 }
739
740 function fStart() {
Simon Huntc3c5b672015-02-20 11:32:13 -0800741 if (!tos.isOblique()) {
Simon Hunta17fa672015-08-19 18:42:22 -0700742 if (fTimer) {
743 $timeout.cancel(fTimer);
744 }
745 fTimer = $timeout(function () {
746 $log.debug("Starting force-layout");
747 force.start();
748 }, 200);
Simon Hunt5724fb42015-02-05 16:59:40 -0800749 }
750 }
751
752 var tickStuff = {
753 nodeAttr: {
Simon Hunta17fa672015-08-19 18:42:22 -0700754 transform: function (d) {
755 var dx = isNaN(d.x) ? 0 : d.x,
756 dy = isNaN(d.y) ? 0 : d.y;
757 return sus.translate(dx, dy);
758 }
Simon Hunt5724fb42015-02-05 16:59:40 -0800759 },
760 linkAttr: {
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700761 x1: function (d) { return d.position.x1; },
762 y1: function (d) { return d.position.y1; },
763 x2: function (d) { return d.position.x2; },
764 y2: function (d) { return d.position.y2; }
Simon Hunt5724fb42015-02-05 16:59:40 -0800765 },
766 linkLabelAttr: {
767 transform: function (d) {
Simon Huntdc6adea2015-02-09 22:29:36 -0800768 var lnk = tms.findLinkById(d.key);
Simon Hunt5724fb42015-02-05 16:59:40 -0800769 if (lnk) {
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700770 return td3.transformLabel(lnk.position);
Simon Hunt5724fb42015-02-05 16:59:40 -0800771 }
772 }
773 }
774 };
775
776 function tick() {
Simon Hunt3ab20282015-02-26 20:32:19 -0800777 // guard against null (which can happen when our view pages out)...
Bri Prebilic Cole8d003782015-07-31 15:33:06 -0700778 if (node && node.size()) {
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700779 node.attr(tickStuff.nodeAttr);
780 }
Bri Prebilic Cole8d003782015-07-31 15:33:06 -0700781 if (link && link.size()) {
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700782 link.call(calcPosition)
783 .attr(tickStuff.linkAttr);
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700784 td3.applyNumLinkLabels(linkNums, numLinkLblsG);
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700785 }
Bri Prebilic Cole8d003782015-07-31 15:33:06 -0700786 if (linkLabel && linkLabel.size()) {
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700787 linkLabel.attr(tickStuff.linkLabelAttr);
788 }
Simon Hunt737c89f2015-01-28 12:23:19 -0800789 }
790
791
Simon Huntac4c6f72015-02-03 19:50:53 -0800792 // ==========================
793 // === MOUSE GESTURE HANDLERS
794
Simon Hunt205099e2015-02-07 13:12:01 -0800795 function zoomingOrPanning(ev) {
796 return ev.metaKey || ev.altKey;
Simon Hunt445e8152015-02-06 13:00:12 -0800797 }
798
799 function atDragEnd(d) {
800 // once we've finished moving, pin the node in position
801 d.fixed = true;
802 d3.select(this).classed('fixed', true);
803 sendUpdateMeta(d);
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700804 tss.clickConsumed(true);
Simon Hunt445e8152015-02-06 13:00:12 -0800805 }
806
807 // predicate that indicates when dragging is active
808 function dragEnabled() {
809 var ev = d3.event.sourceEvent;
810 // nodeLock means we aren't allowing nodes to be dragged...
Simon Hunt205099e2015-02-07 13:12:01 -0800811 return !nodeLock && !zoomingOrPanning(ev);
Simon Hunt445e8152015-02-06 13:00:12 -0800812 }
813
814 // predicate that indicates when clicking is active
815 function clickEnabled() {
816 return true;
817 }
Simon Hunt737c89f2015-01-28 12:23:19 -0800818
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700819 // =============================================
820 // function entry points for overlay module
Simon Huntf542d842015-02-11 16:20:33 -0800821
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700822 // TODO: find an automatic way of tracking via the "showHighlights" events
Simon Hunte50829c2015-06-09 08:39:28 -0700823 var allTrafficClasses = 'primary secondary optical animated ' +
824 'port-traffic-Kbps port-traffic-Mbps port-traffic-Gbps ' +
825 'port-traffic-Gbps-choked';
Simon Huntf542d842015-02-11 16:20:33 -0800826
827 function clearLinkTrafficStyle() {
828 link.style('stroke-width', null)
829 .classed(allTrafficClasses, false);
830 }
831
832 function removeLinkLabels() {
833 network.links.forEach(function (d) {
834 d.label = '';
835 });
836 }
Simon Hunt737c89f2015-01-28 12:23:19 -0800837
Simon Hunta4242de2015-02-24 17:11:55 -0800838 function updateLinkLabelModel() {
839 // create the backing data for showing labels..
840 var data = [];
841 link.each(function (d) {
842 if (d.label) {
843 data.push({
844 id: 'lab-' + d.key,
845 key: d.key,
846 label: d.label,
847 ldata: d
848 });
849 }
850 });
851
852 linkLabel = linkLabelG.selectAll('.linkLabel')
853 .data(data, function (d) { return d.id; });
854 }
855
Simon Hunt737c89f2015-01-28 12:23:19 -0800856 // ==========================
Simon Huntac4c6f72015-02-03 19:50:53 -0800857 // Module definition
Simon Hunt737c89f2015-01-28 12:23:19 -0800858
Simon Huntdc6adea2015-02-09 22:29:36 -0800859 function mkModelApi(uplink) {
860 return {
861 projection: uplink.projection,
862 network: network,
863 restyleLinkElement: restyleLinkElement,
864 removeLinkElement: removeLinkElement
865 };
866 }
867
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700868 function mkD3Api() {
Simon Hunta4242de2015-02-24 17:11:55 -0800869 return {
870 node: function () { return node; },
871 link: function () { return link; },
872 linkLabel: function () { return linkLabel; },
873 instVisible: function () { return tis.isVisible(); },
874 posNode: tms.positionNode,
875 showHosts: function () { return showHosts; },
876 restyleLinkElement: restyleLinkElement,
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700877 updateLinkLabelModel: updateLinkLabelModel,
878 linkConfig: function () { return linkConfig; }
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700879 };
Simon Hunta4242de2015-02-24 17:11:55 -0800880 }
881
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700882 function mkSelectApi() {
Simon Hunt08f841d02015-02-10 14:39:20 -0800883 return {
884 node: function () { return node; },
885 zoomingOrPanning: zoomingOrPanning,
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700886 updateDeviceColors: td3.updateDeviceColors,
887 deselectLink: tls.deselectLink
Simon Hunt08f841d02015-02-10 14:39:20 -0800888 };
889 }
890
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700891 function mkTrafficApi() {
892 return {
893 hovered: tss.hovered,
894 somethingSelected: tss.somethingSelected,
895 selectOrder: tss.selectOrder
896 };
897 }
898
899 function mkOverlayApi() {
Simon Huntf542d842015-02-11 16:20:33 -0800900 return {
901 clearLinkTrafficStyle: clearLinkTrafficStyle,
902 removeLinkLabels: removeLinkLabels,
903 updateLinks: updateLinks,
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700904 findLinkById: tms.findLinkById
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700905 };
Simon Huntf542d842015-02-11 16:20:33 -0800906 }
907
Simon Huntc3c5b672015-02-20 11:32:13 -0800908 function mkObliqueApi(uplink, fltr) {
Simon Hunt96f88c62015-02-19 17:57:25 -0800909 return {
Simon Huntc3c5b672015-02-20 11:32:13 -0800910 force: function() { return force; },
911 zoomLayer: uplink.zoomLayer,
912 nodeGBBox: function() { return nodeG.node().getBBox(); },
Simon Hunt96f88c62015-02-19 17:57:25 -0800913 node: function () { return node; },
Simon Huntc3c5b672015-02-20 11:32:13 -0800914 link: function () { return link; },
915 linkLabel: function () { return linkLabel; },
916 nodes: function () { return network.nodes; },
917 tickStuff: tickStuff,
918 nodeLock: function (b) {
919 var old = nodeLock;
920 nodeLock = b;
921 return old;
922 },
923 opacifyMap: uplink.opacifyMap,
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700924 inLayer: fltr.inLayer,
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700925 calcLinkPos: calcPosition,
926 applyNumLinkLabels: function () {
927 td3.applyNumLinkLabels(linkNums, numLinkLblsG);
928 }
Simon Hunt96f88c62015-02-19 17:57:25 -0800929 };
930 }
931
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700932 function mkFilterApi() {
Simon Hunteb0fa052015-02-17 19:20:28 -0800933 return {
934 node: function () { return node; },
935 link: function () { return link; }
936 };
937 }
938
Simon Hunt9e2104c2015-02-26 10:48:59 -0800939 function mkLinkApi(svg, uplink) {
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800940 return {
941 svg: svg,
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800942 zoomer: uplink.zoomer(),
943 network: network,
Simon Hunt1a5301e2015-02-25 15:31:25 -0800944 portLabelG: function () { return portLabelG; },
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800945 showHosts: function () { return showHosts; }
946 };
947 }
948
Simon Hunt737c89f2015-01-28 12:23:19 -0800949 angular.module('ovTopo')
950 .factory('TopoForceService',
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700951 ['$log', '$timeout', 'FnService', 'SvgUtilService',
Simon Hunt86b7c882015-04-02 23:06:08 -0700952 'ThemeService', 'FlashService', 'WebSocketService',
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700953 'TopoOverlayService', 'TopoInstService', 'TopoModelService',
Simon Hunta4242de2015-02-24 17:11:55 -0800954 'TopoD3Service', 'TopoSelectService', 'TopoTrafficService',
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800955 'TopoObliqueService', 'TopoFilterService', 'TopoLinkService',
Simon Hunt737c89f2015-01-28 12:23:19 -0800956
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700957 function (_$log_, _$timeout_, _fs_, _sus_, _ts_, _flash_, _wss_, _tov_,
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800958 _tis_, _tms_, _td3_, _tss_, _tts_, _tos_, _fltr_, _tls_) {
Simon Hunt737c89f2015-01-28 12:23:19 -0800959 $log = _$log_;
Simon Hunt86b7c882015-04-02 23:06:08 -0700960 $timeout = _$timeout_;
Simon Hunt1894d792015-02-04 17:09:20 -0800961 fs = _fs_;
Simon Hunt737c89f2015-01-28 12:23:19 -0800962 sus = _sus_;
Simon Huntac4c6f72015-02-03 19:50:53 -0800963 ts = _ts_;
Simon Hunt5724fb42015-02-05 16:59:40 -0800964 flash = _flash_;
Simon Hunt237676b52015-03-10 19:04:26 -0700965 wss = _wss_;
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700966 tov = _tov_;
Simon Huntac4c6f72015-02-03 19:50:53 -0800967 tis = _tis_;
Simon Hunt3a6eec02015-02-09 21:16:43 -0800968 tms = _tms_;
Simon Hunta4242de2015-02-24 17:11:55 -0800969 td3 = _td3_;
Simon Hunt08f841d02015-02-10 14:39:20 -0800970 tss = _tss_;
Simon Huntf542d842015-02-11 16:20:33 -0800971 tts = _tts_;
Simon Hunt96f88c62015-02-19 17:57:25 -0800972 tos = _tos_;
Simon Hunteb0fa052015-02-17 19:20:28 -0800973 fltr = _fltr_;
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800974 tls = _tls_;
Simon Hunt737c89f2015-01-28 12:23:19 -0800975
Simon Hunta142dd22015-02-12 22:07:51 -0800976 var themeListener = ts.addListener(function () {
977 updateLinks();
978 updateNodes();
979 });
980
Simon Hunt737c89f2015-01-28 12:23:19 -0800981 // forceG is the SVG group to display the force layout in
Simon Huntdc6adea2015-02-09 22:29:36 -0800982 // uplink is the api from the main topo source file
Simon Hunt3a6eec02015-02-09 21:16:43 -0800983 // dim is the initial dimensions of the SVG as [w,h]
Simon Hunt737c89f2015-01-28 12:23:19 -0800984 // opts are, well, optional :)
Simon Hunt3ab20282015-02-26 20:32:19 -0800985 function initForce(_svg_, forceG, _uplink_, _dim_, opts) {
Simon Hunt1894d792015-02-04 17:09:20 -0800986 uplink = _uplink_;
Simon Hunt3a6eec02015-02-09 21:16:43 -0800987 dim = _dim_;
Simon Hunt3ab20282015-02-26 20:32:19 -0800988 svg = _svg_;
989
990 lu = network.lookup;
991 rlk = network.revLinkToKey;
Simon Hunt3a6eec02015-02-09 21:16:43 -0800992
993 $log.debug('initForce().. dim = ' + dim);
994
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700995 tov.setApi(mkOverlayApi(), tss);
Simon Huntdc6adea2015-02-09 22:29:36 -0800996 tms.initModel(mkModelApi(uplink), dim);
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700997 td3.initD3(mkD3Api());
998 tss.initSelect(mkSelectApi());
999 tts.initTraffic(mkTrafficApi());
Simon Huntc3c5b672015-02-20 11:32:13 -08001000 tos.initOblique(mkObliqueApi(uplink, fltr));
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001001 fltr.initFilter(mkFilterApi());
Simon Hunt9e2104c2015-02-26 10:48:59 -08001002 tls.initLink(mkLinkApi(svg, uplink), td3);
Simon Hunta11b4eb2015-01-28 16:20:50 -08001003
Simon Hunt737c89f2015-01-28 12:23:19 -08001004 settings = angular.extend({}, defaultSettings, opts);
1005
1006 linkG = forceG.append('g').attr('id', 'topo-links');
1007 linkLabelG = forceG.append('g').attr('id', 'topo-linkLabels');
Bri Prebilic Cole80401762015-07-16 11:36:18 -07001008 numLinkLblsG = forceG.append('g').attr('id', 'topo-numLinkLabels');
Simon Hunt737c89f2015-01-28 12:23:19 -08001009 nodeG = forceG.append('g').attr('id', 'topo-nodes');
Simon Hunt1a5301e2015-02-25 15:31:25 -08001010 portLabelG = forceG.append('g').attr('id', 'topo-portLabels');
Simon Hunt737c89f2015-01-28 12:23:19 -08001011
1012 link = linkG.selectAll('.link');
1013 linkLabel = linkLabelG.selectAll('.linkLabel');
1014 node = nodeG.selectAll('.node');
1015
1016 force = d3.layout.force()
Simon Hunt3a6eec02015-02-09 21:16:43 -08001017 .size(dim)
Simon Hunt737c89f2015-01-28 12:23:19 -08001018 .nodes(network.nodes)
1019 .links(network.links)
1020 .gravity(settings.gravity)
1021 .friction(settings.friction)
1022 .charge(settings.charge._def_)
1023 .linkDistance(settings.linkDistance._def_)
1024 .linkStrength(settings.linkStrength._def_)
1025 .on('tick', tick);
1026
1027 drag = sus.createDragBehavior(force,
Simon Hunt08f841d02015-02-10 14:39:20 -08001028 tss.selectObject, atDragEnd, dragEnabled, clickEnabled);
Simon Hunt737c89f2015-01-28 12:23:19 -08001029 }
1030
Simon Hunt3a6eec02015-02-09 21:16:43 -08001031 function newDim(_dim_) {
1032 dim = _dim_;
1033 force.size(dim);
1034 tms.newDim(dim);
Simon Hunt737c89f2015-01-28 12:23:19 -08001035 }
1036
Simon Hunt3a6eec02015-02-09 21:16:43 -08001037 function destroyForce() {
Simon Hunt3ab20282015-02-26 20:32:19 -08001038 force.stop();
1039
Simon Huntfb8ea1f2015-02-24 21:38:09 -08001040 tls.destroyLink();
Simon Hunt96f88c62015-02-19 17:57:25 -08001041 tos.destroyOblique();
Simon Huntf542d842015-02-11 16:20:33 -08001042 tts.destroyTraffic();
1043 tss.destroySelect();
Simon Hunta4242de2015-02-24 17:11:55 -08001044 td3.destroyD3();
Simon Huntf542d842015-02-11 16:20:33 -08001045 tms.destroyModel();
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001046 // note: no need to destroy overlay service
Simon Hunta142dd22015-02-12 22:07:51 -08001047 ts.removeListener(themeListener);
1048 themeListener = null;
Simon Hunt3ab20282015-02-26 20:32:19 -08001049
1050 // clean up the DOM
1051 svg.selectAll('g').remove();
1052 svg.selectAll('defs').remove();
1053
1054 // clean up internal state
1055 network.nodes = [];
1056 network.links = [];
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -07001057 network.linksByDevice = {};
Simon Hunt3ab20282015-02-26 20:32:19 -08001058 network.lookup = {};
1059 network.revLinkToKey = {};
1060
Bri Prebilic Cole80401762015-07-16 11:36:18 -07001061 linkNums = [];
1062
1063 linkG = linkLabelG = numLinkLblsG = nodeG = portLabelG = null;
Simon Hunt3ab20282015-02-26 20:32:19 -08001064 link = linkLabel = node = null;
1065 force = drag = null;
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -07001066
1067 // clean up $timeout promises
Simon Hunta17fa672015-08-19 18:42:22 -07001068 if (fTimer) {
1069 $timeout.cancel(fTimer);
1070 }
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -07001071 if (fNodesTimer) {
1072 $timeout.cancel(fNodesTimer);
1073 }
1074 if (fLinksTimer) {
1075 $timeout.cancel(fLinksTimer);
1076 }
Simon Hunt3a6eec02015-02-09 21:16:43 -08001077 }
1078
Simon Hunt737c89f2015-01-28 12:23:19 -08001079 return {
1080 initForce: initForce,
Simon Hunt3a6eec02015-02-09 21:16:43 -08001081 newDim: newDim,
1082 destroyForce: destroyForce,
Simon Huntac4c6f72015-02-03 19:50:53 -08001083
Simon Hunta4242de2015-02-24 17:11:55 -08001084 updateDeviceColors: td3.updateDeviceColors,
Simon Hunt5724fb42015-02-05 16:59:40 -08001085 toggleHosts: toggleHosts,
Simon Hunt9e2104c2015-02-26 10:48:59 -08001086 togglePorts: tls.togglePorts,
Simon Hunt5724fb42015-02-05 16:59:40 -08001087 toggleOffline: toggleOffline,
1088 cycleDeviceLabels: cycleDeviceLabels,
Simon Hunt445e8152015-02-06 13:00:12 -08001089 unpin: unpin,
Simon Hunta142dd22015-02-12 22:07:51 -08001090 showMastership: showMastership,
Simon Hunt86b7c882015-04-02 23:06:08 -07001091 showBadLinks: showBadLinks,
Simon Huntac4c6f72015-02-03 19:50:53 -08001092
1093 addDevice: addDevice,
Simon Hunt1894d792015-02-04 17:09:20 -08001094 updateDevice: updateDevice,
1095 removeDevice: removeDevice,
1096 addHost: addHost,
1097 updateHost: updateHost,
1098 removeHost: removeHost,
1099 addLink: addLink,
1100 updateLink: updateLink,
1101 removeLink: removeLink
Simon Hunt737c89f2015-01-28 12:23:19 -08001102 };
1103 }]);
1104}());