blob: 21137afb20a30917ad2bed6b90516209b147a2a9 [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
Simon Hunt95d56fd2015-11-12 11:06:44 -0800190 function moveHost(data) {
191 var id = data.id,
192 d = lu[id],
193 lnk;
194 if (d) {
195 // first remove the old host link
196 removeLinkElement(d.linkData);
197
198 // merge new data
199 angular.extend(d, data);
200 if (tms.positionNode(d, true)) {
201 sendUpdateMeta(d);
202 }
203
204 // now create a new host link
205 lnk = tms.createHostLink(data);
206 if (lnk) {
207 d.linkData = lnk;
208 network.links.push(lnk);
209 lu[d.ingress] = lnk;
210 lu[d.egress] = lnk;
211 }
212
213 updateNodes();
214 updateLinks();
215 fResume();
216 }
217 }
218
Simon Hunt1894d792015-02-04 17:09:20 -0800219 function removeHost(data) {
220 var id = data.id,
221 d = lu[id];
222 if (d) {
223 removeHostElement(d, true);
Simon Hunt1894d792015-02-04 17:09:20 -0800224 }
225 }
226
227 function addLink(data) {
Simon Huntdc6adea2015-02-09 22:29:36 -0800228 var result = tms.findLink(data, 'add'),
Simon Hunt1894d792015-02-04 17:09:20 -0800229 bad = result.badLogic,
230 d = result.ldata;
231
232 if (bad) {
Simon Hunteb18f522016-01-28 19:22:23 -0800233 $log.debug(bad + ': ' + link.id);
Simon Hunt1894d792015-02-04 17:09:20 -0800234 return;
235 }
236
237 if (d) {
238 // we already have a backing store link for src/dst nodes
239 addLinkUpdate(d, data);
240 return;
241 }
242
243 // no backing store link yet
Simon Hunt3a6eec02015-02-09 21:16:43 -0800244 d = tms.createLink(data);
Simon Hunt1894d792015-02-04 17:09:20 -0800245 if (d) {
246 network.links.push(d);
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700247 aggregateLink(d, data);
Simon Hunt1894d792015-02-04 17:09:20 -0800248 lu[d.key] = d;
249 updateLinks();
Simon Hunta17fa672015-08-19 18:42:22 -0700250 fStart();
Simon Hunt1894d792015-02-04 17:09:20 -0800251 }
252 }
253
254 function updateLink(data) {
Simon Huntdc6adea2015-02-09 22:29:36 -0800255 var result = tms.findLink(data, 'update'),
Simon Hunt1894d792015-02-04 17:09:20 -0800256 bad = result.badLogic;
257 if (bad) {
Simon Hunteb18f522016-01-28 19:22:23 -0800258 $log.debug(bad + ': ' + link.id);
Simon Hunt1894d792015-02-04 17:09:20 -0800259 return;
260 }
Simon Hunteb18f522016-01-28 19:22:23 -0800261 result.updateWith(data);
Simon Hunt1894d792015-02-04 17:09:20 -0800262 }
263
264 function removeLink(data) {
Simon Hunta4242de2015-02-24 17:11:55 -0800265 var result = tms.findLink(data, 'remove');
266
267 if (!result.badLogic) {
268 result.removeRawLink();
Simon Hunt1894d792015-02-04 17:09:20 -0800269 }
Simon Hunt1894d792015-02-04 17:09:20 -0800270 }
271
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700272 function topoStartDone(data) {
273 // called when the initial barrage of data has been sent from server
274 uplink.topoStartDone();
275 }
276
Simon Hunt1894d792015-02-04 17:09:20 -0800277 // ========================
278
Simon Hunt94f7dae2015-08-26 17:40:59 -0700279 function nodeById(id) {
280 return lu[id];
281 }
282
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700283 function makeNodeKey(node1, node2) {
284 return node1 + '-' + node2;
285 }
286
287 function findNodePair(key, keyRev) {
288 if (network.linksByDevice[key]) {
289 return key;
290 } else if (network.linksByDevice[keyRev]) {
291 return keyRev;
292 } else {
293 return false;
294 }
295 }
296
297 function aggregateLink(ldata, link) {
298 var key = makeNodeKey(link.src, link.dst),
299 keyRev = makeNodeKey(link.dst, link.src),
300 found = findNodePair(key, keyRev);
301
302 if (found) {
303 network.linksByDevice[found].push(ldata);
304 ldata.devicePair = found;
305 } else {
306 network.linksByDevice[key] = [ ldata ];
307 ldata.devicePair = key;
308 }
309 }
310
Simon Hunt1894d792015-02-04 17:09:20 -0800311 function addLinkUpdate(ldata, link) {
312 // add link event, but we already have the reverse link installed
313 ldata.fromTarget = link;
Simon Huntdc6adea2015-02-09 22:29:36 -0800314 rlk[link.id] = ldata.key;
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700315 // possible solution to el being undefined in restyleLinkElement:
316 //_updateLinks();
Simon Hunt1894d792015-02-04 17:09:20 -0800317 restyleLinkElement(ldata);
318 }
319
Simon Hunt1894d792015-02-04 17:09:20 -0800320
321 var widthRatio = 1.4,
322 linkScale = d3.scale.linear()
323 .domain([1, 12])
324 .range([widthRatio, 12 * widthRatio])
Simon Hunt5724fb42015-02-05 16:59:40 -0800325 .clamp(true),
Ray Milkeyb7f0f642016-01-22 16:08:14 -0800326 allLinkTypes = 'direct indirect optical tunnel',
327 allLinkSubTypes = 'inactive not-permitted';
Simon Hunt1894d792015-02-04 17:09:20 -0800328
Simon Hunta142dd22015-02-12 22:07:51 -0800329 function restyleLinkElement(ldata, immediate) {
Simon Hunt1894d792015-02-04 17:09:20 -0800330 // this fn's job is to look at raw links and decide what svg classes
331 // need to be applied to the line element in the DOM
332 var th = ts.theme(),
333 el = ldata.el,
334 type = ldata.type(),
335 lw = ldata.linkWidth(),
Simon Hunta142dd22015-02-12 22:07:51 -0800336 online = ldata.online(),
Ray Milkeyb7f0f642016-01-22 16:08:14 -0800337 modeCls = ldata.expected() ? 'inactive' : 'not-permitted',
Simon Hunta142dd22015-02-12 22:07:51 -0800338 delay = immediate ? 0 : 1000;
Simon Hunt1894d792015-02-04 17:09:20 -0800339
Thomas Vachuskacb5016f2015-05-18 14:11:43 -0700340 // FIXME: understand why el is sometimes undefined on addLink events...
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700341 // Investigated:
342 // el is undefined when it's a reverse link that is being added.
343 // updateLinks (which sets ldata.el) isn't called before this is called.
344 // Calling _updateLinks in addLinkUpdate fixes it, but there might be
345 // a more efficient way to fix it.
346 if (el && !el.empty()) {
Thomas Vachuskacb5016f2015-05-18 14:11:43 -0700347 el.classed('link', true);
Ray Milkeyb7f0f642016-01-22 16:08:14 -0800348 el.classed(allLinkSubTypes, false);
349 el.classed(modeCls, !online);
Thomas Vachuskacb5016f2015-05-18 14:11:43 -0700350 el.classed(allLinkTypes, false);
351 if (type) {
352 el.classed(type, true);
353 }
354 el.transition()
355 .duration(delay)
356 .attr('stroke-width', linkScale(lw))
357 .attr('stroke', linkConfig[th].baseColor);
Simon Hunt1894d792015-02-04 17:09:20 -0800358 }
Simon Hunt1894d792015-02-04 17:09:20 -0800359 }
360
Simon Hunt1894d792015-02-04 17:09:20 -0800361 function removeLinkElement(d) {
362 var idx = fs.find(d.key, network.links, 'key'),
363 removed;
364 if (idx >=0) {
365 // remove from links array
366 removed = network.links.splice(idx, 1);
367 // remove from lookup cache
368 delete lu[removed[0].key];
369 updateLinks();
Simon Hunta17fa672015-08-19 18:42:22 -0700370 fResume();
Simon Hunt1894d792015-02-04 17:09:20 -0800371 }
372 }
373
374 function removeHostElement(d, upd) {
375 // first, remove associated hostLink...
376 removeLinkElement(d.linkData);
377
378 // remove hostLink bindings
379 delete lu[d.ingress];
380 delete lu[d.egress];
381
382 // remove from lookup cache
383 delete lu[d.id];
384 // remove from nodes array
385 var idx = fs.find(d.id, network.nodes);
386 network.nodes.splice(idx, 1);
387
388 // remove from SVG
389 // NOTE: upd is false if we were called from removeDeviceElement()
390 if (upd) {
391 updateNodes();
Simon Hunta17fa672015-08-19 18:42:22 -0700392 fResume();
Simon Hunt1894d792015-02-04 17:09:20 -0800393 }
394 }
395
396 function removeDeviceElement(d) {
Bri Prebilic Cole8d003782015-07-31 15:33:06 -0700397 var id = d.id,
398 idx;
Simon Hunt1894d792015-02-04 17:09:20 -0800399 // first, remove associated hosts and links..
Simon Huntdc6adea2015-02-09 22:29:36 -0800400 tms.findAttachedHosts(id).forEach(removeHostElement);
401 tms.findAttachedLinks(id).forEach(removeLinkElement);
Simon Hunt1894d792015-02-04 17:09:20 -0800402
403 // remove from lookup cache
404 delete lu[id];
405 // remove from nodes array
Bri Prebilic Cole8d003782015-07-31 15:33:06 -0700406 idx = fs.find(id, network.nodes);
407 if (idx > -1) {
408 network.nodes.splice(idx, 1);
409 }
Simon Hunt1894d792015-02-04 17:09:20 -0800410
411 if (!network.nodes.length) {
Simon Huntdc6adea2015-02-09 22:29:36 -0800412 uplink.showNoDevs(true);
Simon Hunt1894d792015-02-04 17:09:20 -0800413 }
414
415 // remove from SVG
416 updateNodes();
Simon Hunta17fa672015-08-19 18:42:22 -0700417 fResume();
Simon Hunt1894d792015-02-04 17:09:20 -0800418 }
419
Simon Hunt5724fb42015-02-05 16:59:40 -0800420 function updateHostVisibility() {
Simon Hunt18bf9822015-02-12 17:35:45 -0800421 sus.visible(nodeG.selectAll('.host'), showHosts);
422 sus.visible(linkG.selectAll('.hostLink'), showHosts);
Simon Hunt8eb4d3a2015-02-23 18:23:29 -0800423 sus.visible(linkLabelG.selectAll('.hostLinkLabel'), showHosts);
Simon Hunt5724fb42015-02-05 16:59:40 -0800424 }
425
426 function updateOfflineVisibility(dev) {
427 function updDev(d, show) {
Simon Hunt8eb4d3a2015-02-23 18:23:29 -0800428 var b;
Simon Hunt18bf9822015-02-12 17:35:45 -0800429 sus.visible(d.el, show);
Simon Hunt5724fb42015-02-05 16:59:40 -0800430
Simon Huntdc6adea2015-02-09 22:29:36 -0800431 tms.findAttachedLinks(d.id).forEach(function (link) {
Simon Hunt5724fb42015-02-05 16:59:40 -0800432 b = show && ((link.type() !== 'hostLink') || showHosts);
Simon Hunt18bf9822015-02-12 17:35:45 -0800433 sus.visible(link.el, b);
Simon Hunt5724fb42015-02-05 16:59:40 -0800434 });
Simon Huntdc6adea2015-02-09 22:29:36 -0800435 tms.findAttachedHosts(d.id).forEach(function (host) {
Simon Hunt5724fb42015-02-05 16:59:40 -0800436 b = show && showHosts;
Simon Hunt18bf9822015-02-12 17:35:45 -0800437 sus.visible(host.el, b);
Simon Hunt5724fb42015-02-05 16:59:40 -0800438 });
439 }
440
441 if (dev) {
442 // updating a specific device that just toggled off/on-line
443 updDev(dev, dev.online || showOffline);
444 } else {
445 // updating all offline devices
Simon Huntdc6adea2015-02-09 22:29:36 -0800446 tms.findDevices(true).forEach(function (d) {
Simon Hunt5724fb42015-02-05 16:59:40 -0800447 updDev(d, showOffline);
448 });
449 }
450 }
451
Simon Hunt1894d792015-02-04 17:09:20 -0800452
Simon Hunt445e8152015-02-06 13:00:12 -0800453 function sendUpdateMeta(d, clearPos) {
Simon Huntac4c6f72015-02-03 19:50:53 -0800454 var metaUi = {},
455 ll;
456
Simon Hunt445e8152015-02-06 13:00:12 -0800457 // if we are not clearing the position data (unpinning),
458 // attach the x, y, longitude, latitude...
459 if (!clearPos) {
Simon Hunt3a6eec02015-02-09 21:16:43 -0800460 ll = tms.lngLatFromCoord([d.x, d.y]);
Simon Huntdc6adea2015-02-09 22:29:36 -0800461 metaUi = {x: d.x, y: d.y, lng: ll[0], lat: ll[1]};
Simon Hunt1894d792015-02-04 17:09:20 -0800462 }
463 d.metaUi = metaUi;
Simon Hunt237676b52015-03-10 19:04:26 -0700464 wss.sendEvent('updateMeta', {
Simon Hunt1894d792015-02-04 17:09:20 -0800465 id: d.id,
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700466 class: d.class,
Simon Hunt1894d792015-02-04 17:09:20 -0800467 memento: metaUi
468 });
Simon Huntac4c6f72015-02-03 19:50:53 -0800469 }
470
Simon Hunt1894d792015-02-04 17:09:20 -0800471
Simon Huntac4c6f72015-02-03 19:50:53 -0800472 function mkSvgClass(d) {
473 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
474 }
475
Simon Hunt5724fb42015-02-05 16:59:40 -0800476 function vis(b) {
477 return b ? 'visible' : 'hidden';
478 }
479
Simon Huntfcbde892015-04-16 12:05:28 -0700480 function toggleHosts(x) {
481 var kev = (x === 'keyev'),
482 on = kev ? !showHosts : !!x;
483
484 showHosts = on;
Simon Hunt5724fb42015-02-05 16:59:40 -0800485 updateHostVisibility();
Simon Huntfcbde892015-04-16 12:05:28 -0700486 flash.flash('Hosts ' + vis(on));
487 return on;
Simon Hunt5724fb42015-02-05 16:59:40 -0800488 }
489
Simon Huntfcbde892015-04-16 12:05:28 -0700490 function toggleOffline(x) {
491 var kev = (x === 'keyev'),
492 on = kev ? !showOffline : !!x;
493
494 showOffline = on;
Simon Hunt5724fb42015-02-05 16:59:40 -0800495 updateOfflineVisibility();
Simon Huntfcbde892015-04-16 12:05:28 -0700496 flash.flash('Offline devices ' + vis(on));
497 return on;
Simon Hunt5724fb42015-02-05 16:59:40 -0800498 }
499
500 function cycleDeviceLabels() {
Bri Prebilic Cole9cf1a8d2015-04-21 13:15:29 -0700501 flash.flash(td3.incDevLabIndex());
Simon Huntdc6adea2015-02-09 22:29:36 -0800502 tms.findDevices().forEach(function (d) {
Simon Hunta4242de2015-02-24 17:11:55 -0800503 td3.updateDeviceLabel(d);
Simon Hunt1c367112015-02-05 18:02:46 -0800504 });
Simon Hunt5724fb42015-02-05 16:59:40 -0800505 }
506
Simon Hunt445e8152015-02-06 13:00:12 -0800507 function unpin() {
Simon Hunt08f841d02015-02-10 14:39:20 -0800508 var hov = tss.hovered();
509 if (hov) {
510 sendUpdateMeta(hov, true);
511 hov.fixed = false;
512 hov.el.classed('fixed', false);
Simon Hunt445e8152015-02-06 13:00:12 -0800513 fResume();
514 }
515 }
516
Simon Hunta142dd22015-02-12 22:07:51 -0800517 function showMastership(masterId) {
518 if (!masterId) {
519 restoreLayerState();
520 } else {
521 showMastershipFor(masterId);
522 }
523 }
524
525 function restoreLayerState() {
526 // NOTE: this level of indirection required, for when we have
527 // the layer filter functionality re-implemented
528 suppressLayers(false);
529 }
530
531 function showMastershipFor(id) {
532 suppressLayers(true);
533 node.each(function (n) {
534 if (n.master === id) {
Simon Hunt743a8492015-08-25 16:18:19 -0700535 n.el.classed('suppressedmax', false);
Simon Hunta142dd22015-02-12 22:07:51 -0800536 }
537 });
538 }
539
Simon Hunt743a8492015-08-25 16:18:19 -0700540 function supAmt(less) {
541 return less ? "suppressed" : "suppressedmax";
542 }
543
544 function suppressLayers(b, less) {
545 var cls = supAmt(less);
546 node.classed(cls, b);
547 link.classed(cls, b);
548 }
549
550 function unsuppressNode(id, less) {
551 var cls = supAmt(less);
552 node.each(function (n) {
553 if (n.id === id) {
554 n.el.classed(cls, false);
555 }
556 });
557 }
558
Simon Hunt94f7dae2015-08-26 17:40:59 -0700559 function unsuppressLink(key, less) {
Simon Hunt743a8492015-08-25 16:18:19 -0700560 var cls = supAmt(less);
561 link.each(function (n) {
Simon Hunt94f7dae2015-08-26 17:40:59 -0700562 if (n.key === key) {
Simon Hunt743a8492015-08-25 16:18:19 -0700563 n.el.classed(cls, false);
564 }
565 });
Simon Hunta142dd22015-02-12 22:07:51 -0800566 }
Simon Hunt445e8152015-02-06 13:00:12 -0800567
Simon Hunt86b7c882015-04-02 23:06:08 -0700568 function showBadLinks() {
569 var badLinks = tms.findBadLinks();
570 flash.flash('Bad Links: ' + badLinks.length);
571 $log.debug('Bad Link List (' + badLinks.length + '):');
572 badLinks.forEach(function (d) {
573 $log.debug('bad link: (' + d.bad + ') ' + d.key, d);
574 if (d.el) {
575 d.el.attr('stroke-width', linkScale(2.8))
576 .attr('stroke', 'red');
577 }
578 });
579 // back to normal after 2 seconds...
580 $timeout(updateLinks, 2000);
581 }
582
Simon Hunt5724fb42015-02-05 16:59:40 -0800583 // ==========================================
584
Simon Huntac4c6f72015-02-03 19:50:53 -0800585 function updateNodes() {
Thomas Vachuska1a989c12015-06-09 18:29:22 -0700586 if (fNodesTimer) {
587 $timeout.cancel(fNodesTimer);
588 }
589 fNodesTimer = $timeout(_updateNodes, 150);
590 }
591
Simon Hunta17fa672015-08-19 18:42:22 -0700592 // IMPLEMENTATION NOTE: _updateNodes() should NOT stop, start, or resume
593 // the force layout; that needs to be determined and implemented elsewhere
Thomas Vachuska1a989c12015-06-09 18:29:22 -0700594 function _updateNodes() {
Simon Hunt1894d792015-02-04 17:09:20 -0800595 // select all the nodes in the layout:
Simon Huntac4c6f72015-02-03 19:50:53 -0800596 node = nodeG.selectAll('.node')
597 .data(network.nodes, function (d) { return d.id; });
598
Simon Hunt1894d792015-02-04 17:09:20 -0800599 // operate on existing nodes:
Simon Hunta4242de2015-02-24 17:11:55 -0800600 node.filter('.device').each(td3.deviceExisting);
601 node.filter('.host').each(td3.hostExisting);
Simon Huntac4c6f72015-02-03 19:50:53 -0800602
603 // operate on entering nodes:
604 var entering = node.enter()
605 .append('g')
606 .attr({
607 id: function (d) { return sus.safeId(d.id); },
608 class: mkSvgClass,
Simon Hunta17fa672015-08-19 18:42:22 -0700609 transform: function (d) {
610 // Need to guard against NaN here ??
611 return sus.translate(d.x, d.y);
612 },
Simon Huntac4c6f72015-02-03 19:50:53 -0800613 opacity: 0
614 })
615 .call(drag)
Simon Hunt08f841d02015-02-10 14:39:20 -0800616 .on('mouseover', tss.nodeMouseOver)
617 .on('mouseout', tss.nodeMouseOut)
Simon Huntac4c6f72015-02-03 19:50:53 -0800618 .transition()
619 .attr('opacity', 1);
620
Simon Hunt1894d792015-02-04 17:09:20 -0800621 // augment entering nodes:
Simon Hunta4242de2015-02-24 17:11:55 -0800622 entering.filter('.device').each(td3.deviceEnter);
623 entering.filter('.host').each(td3.hostEnter);
Simon Huntac4c6f72015-02-03 19:50:53 -0800624
Simon Hunt51056592015-02-03 21:48:07 -0800625 // operate on both existing and new nodes:
Simon Hunta4242de2015-02-24 17:11:55 -0800626 td3.updateDeviceColors();
Simon Huntac4c6f72015-02-03 19:50:53 -0800627
628 // operate on exiting nodes:
629 // Note that the node is removed after 2 seconds.
630 // Sub element animations should be shorter than 2 seconds.
631 var exiting = node.exit()
632 .transition()
633 .duration(2000)
634 .style('opacity', 0)
635 .remove();
636
Simon Hunt1894d792015-02-04 17:09:20 -0800637 // exiting node specifics:
Simon Hunta4242de2015-02-24 17:11:55 -0800638 exiting.filter('.host').each(td3.hostExit);
639 exiting.filter('.device').each(td3.deviceExit);
Simon Huntac4c6f72015-02-03 19:50:53 -0800640 }
641
Simon Hunt51056592015-02-03 21:48:07 -0800642 // ==========================
Simon Hunt1894d792015-02-04 17:09:20 -0800643
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700644 function getDefaultPos(link) {
645 return {
646 x1: link.source.x,
647 y1: link.source.y,
648 x2: link.target.x,
649 y2: link.target.y
650 };
651 }
652
653 // returns amount of adjustment along the normal for given link
654 function amt(numLinks, linkIdx) {
655 var gap = 6;
656 return (linkIdx - ((numLinks - 1) / 2)) * gap;
657 }
658
659 function calcMovement(d, amt, flipped) {
660 var pos = getDefaultPos(d),
661 mult = flipped ? -amt : amt,
662 dx = pos.x2 - pos.x1,
663 dy = pos.y2 - pos.y1,
664 length = Math.sqrt((dx * dx) + (dy * dy));
665
666 return {
667 x1: pos.x1 + (mult * dy / length),
668 y1: pos.y1 + (mult * -dx / length),
669 x2: pos.x2 + (mult * dy / length),
670 y2: pos.y2 + (mult * -dx / length)
671 };
672 }
673
674 function calcPosition() {
675 var lines = this,
676 linkSrcId;
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700677 linkNums = [];
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700678 lines.each(function (d) {
679 if (d.type() === 'hostLink') {
680 d.position = getDefaultPos(d);
681 }
682 });
683
684 function normalizeLinkSrc(link) {
685 // ensure source device is consistent across set of links
686 // temporary measure until link modeling is refactored
687 if (!linkSrcId) {
688 linkSrcId = link.source.id;
689 return false;
690 }
691
692 return link.source.id !== linkSrcId;
693 }
694
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700695 angular.forEach(network.linksByDevice, function (linkArr, key) {
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700696 var numLinks = linkArr.length,
697 link;
698
699 if (numLinks === 1) {
700 link = linkArr[0];
701 link.position = getDefaultPos(link);
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700702 link.position.multiLink = false;
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700703 } else if (numLinks >= 5) {
704 // this code is inefficient, in the future the way links
705 // are modeled will be changed
706 angular.forEach(linkArr, function (link) {
707 link.position = getDefaultPos(link);
708 link.position.multiLink = true;
709 });
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700710 linkNums.push({
711 id: key,
712 num: numLinks,
713 linkCoords: linkArr[0].position
714 });
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700715 } else {
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700716 linkSrcId = null;
717 angular.forEach(linkArr, function (link, index) {
718 var offsetAmt = amt(numLinks, index),
719 needToFlip = normalizeLinkSrc(link);
720 link.position = calcMovement(link, offsetAmt, needToFlip);
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700721 link.position.multiLink = false;
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700722 });
723 }
724 });
725 }
726
Simon Hunt1894d792015-02-04 17:09:20 -0800727 function updateLinks() {
Thomas Vachuska1a989c12015-06-09 18:29:22 -0700728 if (fLinksTimer) {
729 $timeout.cancel(fLinksTimer);
730 }
731 fLinksTimer = $timeout(_updateLinks, 150);
732 }
733
Simon Hunta17fa672015-08-19 18:42:22 -0700734 // IMPLEMENTATION NOTE: _updateLinks() should NOT stop, start, or resume
735 // the force layout; that needs to be determined and implemented elsewhere
Thomas Vachuska1a989c12015-06-09 18:29:22 -0700736 function _updateLinks() {
Simon Hunt1894d792015-02-04 17:09:20 -0800737 var th = ts.theme();
738
739 link = linkG.selectAll('.link')
740 .data(network.links, function (d) { return d.key; });
741
742 // operate on existing links:
Simon Huntd5264122015-02-25 10:17:43 -0800743 link.each(function (d) {
744 // this is supposed to be an existing link, but we have observed
745 // occasions (where links are deleted and added rapidly?) where
746 // the DOM element has not been defined. So protect against that...
747 if (d.el) {
748 restyleLinkElement(d, true);
749 }
750 });
Simon Hunt1894d792015-02-04 17:09:20 -0800751
752 // operate on entering links:
753 var entering = link.enter()
754 .append('line')
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700755 .call(calcPosition)
Simon Hunt1894d792015-02-04 17:09:20 -0800756 .attr({
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700757 x1: function (d) { return d.position.x1; },
758 y1: function (d) { return d.position.y1; },
759 x2: function (d) { return d.position.x2; },
760 y2: function (d) { return d.position.y2; },
Simon Hunt1894d792015-02-04 17:09:20 -0800761 stroke: linkConfig[th].inColor,
762 'stroke-width': linkConfig.inWidth
763 });
764
765 // augment links
Simon Hunta4242de2015-02-24 17:11:55 -0800766 entering.each(td3.linkEntering);
Simon Hunt1894d792015-02-04 17:09:20 -0800767
768 // operate on both existing and new links:
769 //link.each(...)
770
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700771 // add labels for how many links are in a thick line
772 td3.applyNumLinkLabels(linkNums, numLinkLblsG);
773
Simon Hunt1894d792015-02-04 17:09:20 -0800774 // apply or remove labels
Simon Hunta4242de2015-02-24 17:11:55 -0800775 td3.applyLinkLabels();
Simon Hunt1894d792015-02-04 17:09:20 -0800776
777 // operate on exiting links:
778 link.exit()
779 .attr('stroke-dasharray', '3 3')
Simon Hunt5724fb42015-02-05 16:59:40 -0800780 .attr('stroke', linkConfig[th].outColor)
Simon Hunt1894d792015-02-04 17:09:20 -0800781 .style('opacity', 0.5)
782 .transition()
783 .duration(1500)
784 .attr({
785 'stroke-dasharray': '3 12',
Simon Hunt1894d792015-02-04 17:09:20 -0800786 'stroke-width': linkConfig.outWidth
787 })
788 .style('opacity', 0.0)
789 .remove();
Simon Hunt1894d792015-02-04 17:09:20 -0800790 }
791
Simon Huntac4c6f72015-02-03 19:50:53 -0800792
793 // ==========================
Simon Hunt737c89f2015-01-28 12:23:19 -0800794 // force layout tick function
Simon Hunt737c89f2015-01-28 12:23:19 -0800795
Simon Hunt5724fb42015-02-05 16:59:40 -0800796 function fResume() {
Simon Huntc3c5b672015-02-20 11:32:13 -0800797 if (!tos.isOblique()) {
Simon Hunt5724fb42015-02-05 16:59:40 -0800798 force.resume();
799 }
800 }
801
802 function fStart() {
Simon Huntc3c5b672015-02-20 11:32:13 -0800803 if (!tos.isOblique()) {
Simon Hunta17fa672015-08-19 18:42:22 -0700804 if (fTimer) {
805 $timeout.cancel(fTimer);
806 }
807 fTimer = $timeout(function () {
808 $log.debug("Starting force-layout");
809 force.start();
810 }, 200);
Simon Hunt5724fb42015-02-05 16:59:40 -0800811 }
812 }
813
814 var tickStuff = {
815 nodeAttr: {
Simon Hunta17fa672015-08-19 18:42:22 -0700816 transform: function (d) {
817 var dx = isNaN(d.x) ? 0 : d.x,
818 dy = isNaN(d.y) ? 0 : d.y;
819 return sus.translate(dx, dy);
820 }
Simon Hunt5724fb42015-02-05 16:59:40 -0800821 },
822 linkAttr: {
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700823 x1: function (d) { return d.position.x1; },
824 y1: function (d) { return d.position.y1; },
825 x2: function (d) { return d.position.x2; },
826 y2: function (d) { return d.position.y2; }
Simon Hunt5724fb42015-02-05 16:59:40 -0800827 },
828 linkLabelAttr: {
829 transform: function (d) {
Simon Huntdc6adea2015-02-09 22:29:36 -0800830 var lnk = tms.findLinkById(d.key);
Simon Hunt5724fb42015-02-05 16:59:40 -0800831 if (lnk) {
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700832 return td3.transformLabel(lnk.position);
Simon Hunt5724fb42015-02-05 16:59:40 -0800833 }
834 }
835 }
836 };
837
838 function tick() {
Simon Hunt3ab20282015-02-26 20:32:19 -0800839 // guard against null (which can happen when our view pages out)...
Bri Prebilic Cole8d003782015-07-31 15:33:06 -0700840 if (node && node.size()) {
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700841 node.attr(tickStuff.nodeAttr);
842 }
Bri Prebilic Cole8d003782015-07-31 15:33:06 -0700843 if (link && link.size()) {
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700844 link.call(calcPosition)
845 .attr(tickStuff.linkAttr);
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700846 td3.applyNumLinkLabels(linkNums, numLinkLblsG);
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700847 }
Bri Prebilic Cole8d003782015-07-31 15:33:06 -0700848 if (linkLabel && linkLabel.size()) {
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700849 linkLabel.attr(tickStuff.linkLabelAttr);
850 }
Simon Hunt737c89f2015-01-28 12:23:19 -0800851 }
852
853
Simon Huntac4c6f72015-02-03 19:50:53 -0800854 // ==========================
855 // === MOUSE GESTURE HANDLERS
856
Simon Hunt205099e2015-02-07 13:12:01 -0800857 function zoomingOrPanning(ev) {
858 return ev.metaKey || ev.altKey;
Simon Hunt445e8152015-02-06 13:00:12 -0800859 }
860
861 function atDragEnd(d) {
862 // once we've finished moving, pin the node in position
863 d.fixed = true;
864 d3.select(this).classed('fixed', true);
865 sendUpdateMeta(d);
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700866 tss.clickConsumed(true);
Simon Hunt445e8152015-02-06 13:00:12 -0800867 }
868
869 // predicate that indicates when dragging is active
870 function dragEnabled() {
871 var ev = d3.event.sourceEvent;
872 // nodeLock means we aren't allowing nodes to be dragged...
Simon Hunt205099e2015-02-07 13:12:01 -0800873 return !nodeLock && !zoomingOrPanning(ev);
Simon Hunt445e8152015-02-06 13:00:12 -0800874 }
875
876 // predicate that indicates when clicking is active
877 function clickEnabled() {
878 return true;
879 }
Simon Hunt737c89f2015-01-28 12:23:19 -0800880
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700881 // =============================================
882 // function entry points for overlay module
Simon Huntf542d842015-02-11 16:20:33 -0800883
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700884 // TODO: find an automatic way of tracking via the "showHighlights" events
Simon Hunte50829c2015-06-09 08:39:28 -0700885 var allTrafficClasses = 'primary secondary optical animated ' +
886 'port-traffic-Kbps port-traffic-Mbps port-traffic-Gbps ' +
887 'port-traffic-Gbps-choked';
Simon Huntf542d842015-02-11 16:20:33 -0800888
889 function clearLinkTrafficStyle() {
890 link.style('stroke-width', null)
891 .classed(allTrafficClasses, false);
892 }
893
894 function removeLinkLabels() {
895 network.links.forEach(function (d) {
896 d.label = '';
897 });
898 }
Simon Hunt737c89f2015-01-28 12:23:19 -0800899
Simon Hunte9343f32015-10-21 18:07:46 -0700900 function clearNodeDeco() {
901 node.selectAll('g.badge').remove();
902 }
903
904 function removeNodeBadges() {
905 network.nodes.forEach(function (d) {
906 d.badge = null;
907 });
908 }
909
Simon Hunta4242de2015-02-24 17:11:55 -0800910 function updateLinkLabelModel() {
911 // create the backing data for showing labels..
912 var data = [];
913 link.each(function (d) {
914 if (d.label) {
915 data.push({
916 id: 'lab-' + d.key,
917 key: d.key,
918 label: d.label,
919 ldata: d
920 });
921 }
922 });
923
924 linkLabel = linkLabelG.selectAll('.linkLabel')
925 .data(data, function (d) { return d.id; });
926 }
927
Simon Hunt737c89f2015-01-28 12:23:19 -0800928 // ==========================
Simon Huntac4c6f72015-02-03 19:50:53 -0800929 // Module definition
Simon Hunt737c89f2015-01-28 12:23:19 -0800930
Simon Huntdc6adea2015-02-09 22:29:36 -0800931 function mkModelApi(uplink) {
932 return {
933 projection: uplink.projection,
934 network: network,
935 restyleLinkElement: restyleLinkElement,
936 removeLinkElement: removeLinkElement
937 };
938 }
939
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700940 function mkD3Api() {
Simon Hunta4242de2015-02-24 17:11:55 -0800941 return {
942 node: function () { return node; },
943 link: function () { return link; },
944 linkLabel: function () { return linkLabel; },
945 instVisible: function () { return tis.isVisible(); },
946 posNode: tms.positionNode,
947 showHosts: function () { return showHosts; },
948 restyleLinkElement: restyleLinkElement,
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700949 updateLinkLabelModel: updateLinkLabelModel,
950 linkConfig: function () { return linkConfig; }
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700951 };
Simon Hunta4242de2015-02-24 17:11:55 -0800952 }
953
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700954 function mkSelectApi() {
Simon Hunt08f841d02015-02-10 14:39:20 -0800955 return {
956 node: function () { return node; },
957 zoomingOrPanning: zoomingOrPanning,
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700958 updateDeviceColors: td3.updateDeviceColors,
959 deselectLink: tls.deselectLink
Simon Hunt08f841d02015-02-10 14:39:20 -0800960 };
961 }
962
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700963 function mkTrafficApi() {
964 return {
965 hovered: tss.hovered,
966 somethingSelected: tss.somethingSelected,
967 selectOrder: tss.selectOrder
968 };
969 }
970
971 function mkOverlayApi() {
Simon Huntf542d842015-02-11 16:20:33 -0800972 return {
Simon Hunte9343f32015-10-21 18:07:46 -0700973 clearNodeDeco: clearNodeDeco,
974 removeNodeBadges: removeNodeBadges,
Simon Huntf542d842015-02-11 16:20:33 -0800975 clearLinkTrafficStyle: clearLinkTrafficStyle,
976 removeLinkLabels: removeLinkLabels,
Simon Hunt743a8492015-08-25 16:18:19 -0700977 findLinkById: tms.findLinkById,
Simon Hunt94f7dae2015-08-26 17:40:59 -0700978 findNodeById: nodeById,
Simon Huntf542d842015-02-11 16:20:33 -0800979 updateLinks: updateLinks,
Simon Hunt743a8492015-08-25 16:18:19 -0700980 updateNodes: updateNodes,
981 supLayers: suppressLayers,
982 unsupNode: unsuppressNode,
983 unsupLink: unsuppressLink
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700984 };
Simon Huntf542d842015-02-11 16:20:33 -0800985 }
986
Simon Huntc3c5b672015-02-20 11:32:13 -0800987 function mkObliqueApi(uplink, fltr) {
Simon Hunt96f88c62015-02-19 17:57:25 -0800988 return {
Simon Huntc3c5b672015-02-20 11:32:13 -0800989 force: function() { return force; },
990 zoomLayer: uplink.zoomLayer,
991 nodeGBBox: function() { return nodeG.node().getBBox(); },
Simon Hunt96f88c62015-02-19 17:57:25 -0800992 node: function () { return node; },
Simon Huntc3c5b672015-02-20 11:32:13 -0800993 link: function () { return link; },
994 linkLabel: function () { return linkLabel; },
995 nodes: function () { return network.nodes; },
996 tickStuff: tickStuff,
997 nodeLock: function (b) {
998 var old = nodeLock;
999 nodeLock = b;
1000 return old;
1001 },
1002 opacifyMap: uplink.opacifyMap,
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -07001003 inLayer: fltr.inLayer,
Bri Prebilic Cole80401762015-07-16 11:36:18 -07001004 calcLinkPos: calcPosition,
1005 applyNumLinkLabels: function () {
1006 td3.applyNumLinkLabels(linkNums, numLinkLblsG);
1007 }
Simon Hunt96f88c62015-02-19 17:57:25 -08001008 };
1009 }
1010
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001011 function mkFilterApi() {
Simon Hunteb0fa052015-02-17 19:20:28 -08001012 return {
1013 node: function () { return node; },
1014 link: function () { return link; }
1015 };
1016 }
1017
Simon Hunt9e2104c2015-02-26 10:48:59 -08001018 function mkLinkApi(svg, uplink) {
Simon Huntfb8ea1f2015-02-24 21:38:09 -08001019 return {
1020 svg: svg,
Simon Huntfb8ea1f2015-02-24 21:38:09 -08001021 zoomer: uplink.zoomer(),
1022 network: network,
Simon Hunt1a5301e2015-02-25 15:31:25 -08001023 portLabelG: function () { return portLabelG; },
Simon Huntfb8ea1f2015-02-24 21:38:09 -08001024 showHosts: function () { return showHosts; }
1025 };
1026 }
1027
Simon Hunt737c89f2015-01-28 12:23:19 -08001028 angular.module('ovTopo')
1029 .factory('TopoForceService',
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -07001030 ['$log', '$timeout', 'FnService', 'SvgUtilService',
Simon Hunt86b7c882015-04-02 23:06:08 -07001031 'ThemeService', 'FlashService', 'WebSocketService',
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001032 'TopoOverlayService', 'TopoInstService', 'TopoModelService',
Simon Hunta4242de2015-02-24 17:11:55 -08001033 'TopoD3Service', 'TopoSelectService', 'TopoTrafficService',
Simon Huntfb8ea1f2015-02-24 21:38:09 -08001034 'TopoObliqueService', 'TopoFilterService', 'TopoLinkService',
Simon Hunt737c89f2015-01-28 12:23:19 -08001035
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001036 function (_$log_, _$timeout_, _fs_, _sus_, _ts_, _flash_, _wss_, _tov_,
Simon Huntfb8ea1f2015-02-24 21:38:09 -08001037 _tis_, _tms_, _td3_, _tss_, _tts_, _tos_, _fltr_, _tls_) {
Simon Hunt737c89f2015-01-28 12:23:19 -08001038 $log = _$log_;
Simon Hunt86b7c882015-04-02 23:06:08 -07001039 $timeout = _$timeout_;
Simon Hunt1894d792015-02-04 17:09:20 -08001040 fs = _fs_;
Simon Hunt737c89f2015-01-28 12:23:19 -08001041 sus = _sus_;
Simon Huntac4c6f72015-02-03 19:50:53 -08001042 ts = _ts_;
Simon Hunt5724fb42015-02-05 16:59:40 -08001043 flash = _flash_;
Simon Hunt237676b52015-03-10 19:04:26 -07001044 wss = _wss_;
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001045 tov = _tov_;
Simon Huntac4c6f72015-02-03 19:50:53 -08001046 tis = _tis_;
Simon Hunt3a6eec02015-02-09 21:16:43 -08001047 tms = _tms_;
Simon Hunta4242de2015-02-24 17:11:55 -08001048 td3 = _td3_;
Simon Hunt08f841d02015-02-10 14:39:20 -08001049 tss = _tss_;
Simon Huntf542d842015-02-11 16:20:33 -08001050 tts = _tts_;
Simon Hunt96f88c62015-02-19 17:57:25 -08001051 tos = _tos_;
Simon Hunteb0fa052015-02-17 19:20:28 -08001052 fltr = _fltr_;
Simon Huntfb8ea1f2015-02-24 21:38:09 -08001053 tls = _tls_;
Simon Hunt737c89f2015-01-28 12:23:19 -08001054
Simon Hunta142dd22015-02-12 22:07:51 -08001055 var themeListener = ts.addListener(function () {
1056 updateLinks();
1057 updateNodes();
1058 });
1059
Simon Hunt737c89f2015-01-28 12:23:19 -08001060 // forceG is the SVG group to display the force layout in
Simon Huntdc6adea2015-02-09 22:29:36 -08001061 // uplink is the api from the main topo source file
Simon Hunt3a6eec02015-02-09 21:16:43 -08001062 // dim is the initial dimensions of the SVG as [w,h]
Simon Hunt737c89f2015-01-28 12:23:19 -08001063 // opts are, well, optional :)
Simon Hunt3ab20282015-02-26 20:32:19 -08001064 function initForce(_svg_, forceG, _uplink_, _dim_, opts) {
Simon Hunt1894d792015-02-04 17:09:20 -08001065 uplink = _uplink_;
Simon Hunt3a6eec02015-02-09 21:16:43 -08001066 dim = _dim_;
Simon Hunt3ab20282015-02-26 20:32:19 -08001067 svg = _svg_;
1068
1069 lu = network.lookup;
1070 rlk = network.revLinkToKey;
Simon Hunt3a6eec02015-02-09 21:16:43 -08001071
1072 $log.debug('initForce().. dim = ' + dim);
1073
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001074 tov.setApi(mkOverlayApi(), tss);
Simon Huntdc6adea2015-02-09 22:29:36 -08001075 tms.initModel(mkModelApi(uplink), dim);
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001076 td3.initD3(mkD3Api());
1077 tss.initSelect(mkSelectApi());
1078 tts.initTraffic(mkTrafficApi());
Simon Huntc3c5b672015-02-20 11:32:13 -08001079 tos.initOblique(mkObliqueApi(uplink, fltr));
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001080 fltr.initFilter(mkFilterApi());
Simon Hunt9e2104c2015-02-26 10:48:59 -08001081 tls.initLink(mkLinkApi(svg, uplink), td3);
Simon Hunta11b4eb2015-01-28 16:20:50 -08001082
Simon Hunt737c89f2015-01-28 12:23:19 -08001083 settings = angular.extend({}, defaultSettings, opts);
1084
1085 linkG = forceG.append('g').attr('id', 'topo-links');
1086 linkLabelG = forceG.append('g').attr('id', 'topo-linkLabels');
Bri Prebilic Cole80401762015-07-16 11:36:18 -07001087 numLinkLblsG = forceG.append('g').attr('id', 'topo-numLinkLabels');
Simon Hunt737c89f2015-01-28 12:23:19 -08001088 nodeG = forceG.append('g').attr('id', 'topo-nodes');
Simon Hunt1a5301e2015-02-25 15:31:25 -08001089 portLabelG = forceG.append('g').attr('id', 'topo-portLabels');
Simon Hunt737c89f2015-01-28 12:23:19 -08001090
1091 link = linkG.selectAll('.link');
1092 linkLabel = linkLabelG.selectAll('.linkLabel');
1093 node = nodeG.selectAll('.node');
1094
1095 force = d3.layout.force()
Simon Hunt3a6eec02015-02-09 21:16:43 -08001096 .size(dim)
Simon Hunt737c89f2015-01-28 12:23:19 -08001097 .nodes(network.nodes)
1098 .links(network.links)
1099 .gravity(settings.gravity)
1100 .friction(settings.friction)
1101 .charge(settings.charge._def_)
1102 .linkDistance(settings.linkDistance._def_)
1103 .linkStrength(settings.linkStrength._def_)
1104 .on('tick', tick);
1105
1106 drag = sus.createDragBehavior(force,
Simon Hunt08f841d02015-02-10 14:39:20 -08001107 tss.selectObject, atDragEnd, dragEnabled, clickEnabled);
Simon Hunt737c89f2015-01-28 12:23:19 -08001108 }
1109
Simon Hunt3a6eec02015-02-09 21:16:43 -08001110 function newDim(_dim_) {
1111 dim = _dim_;
1112 force.size(dim);
1113 tms.newDim(dim);
Simon Hunt737c89f2015-01-28 12:23:19 -08001114 }
1115
Simon Hunt3a6eec02015-02-09 21:16:43 -08001116 function destroyForce() {
Simon Hunt3ab20282015-02-26 20:32:19 -08001117 force.stop();
1118
Simon Huntfb8ea1f2015-02-24 21:38:09 -08001119 tls.destroyLink();
Simon Hunt96f88c62015-02-19 17:57:25 -08001120 tos.destroyOblique();
Simon Huntf542d842015-02-11 16:20:33 -08001121 tts.destroyTraffic();
1122 tss.destroySelect();
Simon Hunta4242de2015-02-24 17:11:55 -08001123 td3.destroyD3();
Simon Huntf542d842015-02-11 16:20:33 -08001124 tms.destroyModel();
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001125 // note: no need to destroy overlay service
Simon Hunta142dd22015-02-12 22:07:51 -08001126 ts.removeListener(themeListener);
1127 themeListener = null;
Simon Hunt3ab20282015-02-26 20:32:19 -08001128
1129 // clean up the DOM
1130 svg.selectAll('g').remove();
1131 svg.selectAll('defs').remove();
1132
1133 // clean up internal state
1134 network.nodes = [];
1135 network.links = [];
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -07001136 network.linksByDevice = {};
Simon Hunt3ab20282015-02-26 20:32:19 -08001137 network.lookup = {};
1138 network.revLinkToKey = {};
1139
Bri Prebilic Cole80401762015-07-16 11:36:18 -07001140 linkNums = [];
1141
1142 linkG = linkLabelG = numLinkLblsG = nodeG = portLabelG = null;
Simon Hunt3ab20282015-02-26 20:32:19 -08001143 link = linkLabel = node = null;
1144 force = drag = null;
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -07001145
1146 // clean up $timeout promises
Simon Hunta17fa672015-08-19 18:42:22 -07001147 if (fTimer) {
1148 $timeout.cancel(fTimer);
1149 }
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -07001150 if (fNodesTimer) {
1151 $timeout.cancel(fNodesTimer);
1152 }
1153 if (fLinksTimer) {
1154 $timeout.cancel(fLinksTimer);
1155 }
Simon Hunt3a6eec02015-02-09 21:16:43 -08001156 }
1157
Simon Hunt737c89f2015-01-28 12:23:19 -08001158 return {
1159 initForce: initForce,
Simon Hunt3a6eec02015-02-09 21:16:43 -08001160 newDim: newDim,
1161 destroyForce: destroyForce,
Simon Huntac4c6f72015-02-03 19:50:53 -08001162
Simon Hunta4242de2015-02-24 17:11:55 -08001163 updateDeviceColors: td3.updateDeviceColors,
Simon Hunt5724fb42015-02-05 16:59:40 -08001164 toggleHosts: toggleHosts,
Simon Hunt9e2104c2015-02-26 10:48:59 -08001165 togglePorts: tls.togglePorts,
Simon Hunt5724fb42015-02-05 16:59:40 -08001166 toggleOffline: toggleOffline,
1167 cycleDeviceLabels: cycleDeviceLabels,
Simon Hunt445e8152015-02-06 13:00:12 -08001168 unpin: unpin,
Simon Hunta142dd22015-02-12 22:07:51 -08001169 showMastership: showMastership,
Simon Hunt86b7c882015-04-02 23:06:08 -07001170 showBadLinks: showBadLinks,
Simon Huntac4c6f72015-02-03 19:50:53 -08001171
1172 addDevice: addDevice,
Simon Hunt1894d792015-02-04 17:09:20 -08001173 updateDevice: updateDevice,
1174 removeDevice: removeDevice,
1175 addHost: addHost,
1176 updateHost: updateHost,
Simon Hunt95d56fd2015-11-12 11:06:44 -08001177 moveHost: moveHost,
Simon Hunt1894d792015-02-04 17:09:20 -08001178 removeHost: removeHost,
1179 addLink: addLink,
1180 updateLink: updateLink,
Simon Hunt4a6b54b2015-10-27 22:08:25 -07001181 removeLink: removeLink,
1182 topoStartDone: topoStartDone
Simon Hunt737c89f2015-01-28 12:23:19 -08001183 };
1184 }]);
1185}());