blob: 11f4934429682b7390bef4fbedc9339132b80fb6 [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 Hunt237676b52015-03-10 19:04:26 -070026 var $log, fs, sus, is, ts, flash, wss,
27 tis, tms, td3, tss, tts, tos, fltr, tls,
Simon Hunt3ab20282015-02-26 20:32:19 -080028 icfg, uplink, svg;
Simon Huntac4c6f72015-02-03 19:50:53 -080029
30 // configuration
Simon Hunt1894d792015-02-04 17:09:20 -080031 var linkConfig = {
32 light: {
33 baseColor: '#666',
34 inColor: '#66f',
Simon Hunt3a6eec02015-02-09 21:16:43 -080035 outColor: '#f00'
Simon Hunt1894d792015-02-04 17:09:20 -080036 },
37 dark: {
Simon Hunt5724fb42015-02-05 16:59:40 -080038 baseColor: '#aaa',
Simon Hunt1894d792015-02-04 17:09:20 -080039 inColor: '#66f',
Simon Hunt5724fb42015-02-05 16:59:40 -080040 outColor: '#f66'
Simon Hunt1894d792015-02-04 17:09:20 -080041 },
42 inWidth: 12,
43 outWidth: 10
44 };
45
Simon Hunt737c89f2015-01-28 12:23:19 -080046 // internal state
Simon Huntac4c6f72015-02-03 19:50:53 -080047 var settings, // merged default settings and options
Simon Hunt737c89f2015-01-28 12:23:19 -080048 force, // force layout object
49 drag, // drag behavior handler
50 network = {
51 nodes: [],
52 links: [],
53 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 Hunt08f841d02015-02-10 14:39:20 -080061 dim; // the dimensions of the force layout [w,h]
Simon Hunt737c89f2015-01-28 12:23:19 -080062
63 // SVG elements;
Simon Hunt1a5301e2015-02-25 15:31:25 -080064 var linkG, linkLabelG, portLabelG, nodeG;
Simon Hunt737c89f2015-01-28 12:23:19 -080065
66 // D3 selections;
67 var link, linkLabel, node;
68
69 // default settings for force layout
70 var defaultSettings = {
71 gravity: 0.4,
72 friction: 0.7,
73 charge: {
74 // note: key is node.class
75 device: -8000,
76 host: -5000,
77 _def_: -12000
78 },
79 linkDistance: {
80 // note: key is link.type
81 direct: 100,
82 optical: 120,
83 hostLink: 3,
84 _def_: 50
85 },
86 linkStrength: {
87 // note: key is link.type
88 // range: {0.0 ... 1.0}
89 //direct: 1.0,
90 //optical: 1.0,
91 //hostLink: 1.0,
92 _def_: 1.0
93 }
94 };
95
96
Simon Huntac4c6f72015-02-03 19:50:53 -080097 // ==========================
98 // === EVENT HANDLERS
99
100 function addDevice(data) {
101 var id = data.id,
102 d;
103
Simon Hunt1894d792015-02-04 17:09:20 -0800104 uplink.showNoDevs(false);
Simon Huntac4c6f72015-02-03 19:50:53 -0800105
106 // although this is an add device event, if we already have the
107 // device, treat it as an update instead..
Simon Hunt1894d792015-02-04 17:09:20 -0800108 if (lu[id]) {
Simon Huntac4c6f72015-02-03 19:50:53 -0800109 updateDevice(data);
110 return;
111 }
112
Simon Hunt3a6eec02015-02-09 21:16:43 -0800113 d = tms.createDeviceNode(data);
Simon Huntac4c6f72015-02-03 19:50:53 -0800114 network.nodes.push(d);
Simon Hunt1894d792015-02-04 17:09:20 -0800115 lu[id] = d;
Simon Huntac4c6f72015-02-03 19:50:53 -0800116 updateNodes();
117 fStart();
118 }
119
120 function updateDevice(data) {
121 var id = data.id,
Simon Hunt1894d792015-02-04 17:09:20 -0800122 d = lu[id],
Simon Huntac4c6f72015-02-03 19:50:53 -0800123 wasOnline;
124
125 if (d) {
126 wasOnline = d.online;
127 angular.extend(d, data);
Simon Hunt3a6eec02015-02-09 21:16:43 -0800128 if (tms.positionNode(d, true)) {
Simon Hunt445e8152015-02-06 13:00:12 -0800129 sendUpdateMeta(d);
Simon Huntac4c6f72015-02-03 19:50:53 -0800130 }
131 updateNodes();
132 if (wasOnline !== d.online) {
Simon Huntdc6adea2015-02-09 22:29:36 -0800133 tms.findAttachedLinks(d.id).forEach(restyleLinkElement);
Simon Hunt5724fb42015-02-05 16:59:40 -0800134 updateOfflineVisibility(d);
Simon Huntac4c6f72015-02-03 19:50:53 -0800135 }
Simon Huntac4c6f72015-02-03 19:50:53 -0800136 }
137 }
138
Simon Hunt1894d792015-02-04 17:09:20 -0800139 function removeDevice(data) {
140 var id = data.id,
141 d = lu[id];
142 if (d) {
143 removeDeviceElement(d);
Simon Hunt1894d792015-02-04 17:09:20 -0800144 }
145 }
146
147 function addHost(data) {
148 var id = data.id,
149 d, lnk;
150
151 // although this is an add host event, if we already have the
152 // host, treat it as an update instead..
153 if (lu[id]) {
154 updateHost(data);
155 return;
156 }
157
Simon Hunt3a6eec02015-02-09 21:16:43 -0800158 d = tms.createHostNode(data);
Simon Hunt1894d792015-02-04 17:09:20 -0800159 network.nodes.push(d);
160 lu[id] = d;
Simon Hunt1894d792015-02-04 17:09:20 -0800161 updateNodes();
162
Simon Hunt3a6eec02015-02-09 21:16:43 -0800163 lnk = tms.createHostLink(data);
Simon Hunt1894d792015-02-04 17:09:20 -0800164 if (lnk) {
Simon Hunt1894d792015-02-04 17:09:20 -0800165 d.linkData = lnk; // cache ref on its host
166 network.links.push(lnk);
167 lu[d.ingress] = lnk;
168 lu[d.egress] = lnk;
169 updateLinks();
170 }
171
172 fStart();
173 }
174
175 function updateHost(data) {
176 var id = data.id,
177 d = lu[id];
178 if (d) {
179 angular.extend(d, data);
Simon Hunt3a6eec02015-02-09 21:16:43 -0800180 if (tms.positionNode(d, true)) {
Simon Hunt445e8152015-02-06 13:00:12 -0800181 sendUpdateMeta(d);
Simon Hunt1894d792015-02-04 17:09:20 -0800182 }
183 updateNodes();
Simon Hunt1894d792015-02-04 17:09:20 -0800184 }
185 }
186
187 function removeHost(data) {
188 var id = data.id,
189 d = lu[id];
190 if (d) {
191 removeHostElement(d, true);
Simon Hunt1894d792015-02-04 17:09:20 -0800192 }
193 }
194
195 function addLink(data) {
Simon Huntdc6adea2015-02-09 22:29:36 -0800196 var result = tms.findLink(data, 'add'),
Simon Hunt1894d792015-02-04 17:09:20 -0800197 bad = result.badLogic,
198 d = result.ldata;
199
200 if (bad) {
201 //logicError(bad + ': ' + link.id);
202 return;
203 }
204
205 if (d) {
206 // we already have a backing store link for src/dst nodes
207 addLinkUpdate(d, data);
208 return;
209 }
210
211 // no backing store link yet
Simon Hunt3a6eec02015-02-09 21:16:43 -0800212 d = tms.createLink(data);
Simon Hunt1894d792015-02-04 17:09:20 -0800213 if (d) {
214 network.links.push(d);
215 lu[d.key] = d;
216 updateLinks();
217 fStart();
218 }
219 }
220
221 function updateLink(data) {
Simon Huntdc6adea2015-02-09 22:29:36 -0800222 var result = tms.findLink(data, 'update'),
Simon Hunt1894d792015-02-04 17:09:20 -0800223 bad = result.badLogic;
224 if (bad) {
225 //logicError(bad + ': ' + link.id);
226 return;
227 }
228 result.updateWith(link);
229 }
230
231 function removeLink(data) {
Simon Hunta4242de2015-02-24 17:11:55 -0800232 var result = tms.findLink(data, 'remove');
233
234 if (!result.badLogic) {
235 result.removeRawLink();
Simon Hunt1894d792015-02-04 17:09:20 -0800236 }
Simon Hunt1894d792015-02-04 17:09:20 -0800237 }
238
239 // ========================
240
241 function addLinkUpdate(ldata, link) {
242 // add link event, but we already have the reverse link installed
243 ldata.fromTarget = link;
Simon Huntdc6adea2015-02-09 22:29:36 -0800244 rlk[link.id] = ldata.key;
Simon Hunt1894d792015-02-04 17:09:20 -0800245 restyleLinkElement(ldata);
246 }
247
Simon Hunt1894d792015-02-04 17:09:20 -0800248
249 var widthRatio = 1.4,
250 linkScale = d3.scale.linear()
251 .domain([1, 12])
252 .range([widthRatio, 12 * widthRatio])
Simon Hunt5724fb42015-02-05 16:59:40 -0800253 .clamp(true),
Simon Hunt3a6eec02015-02-09 21:16:43 -0800254 allLinkTypes = 'direct indirect optical tunnel';
Simon Hunt1894d792015-02-04 17:09:20 -0800255
Simon Hunta142dd22015-02-12 22:07:51 -0800256 function restyleLinkElement(ldata, immediate) {
Simon Hunt1894d792015-02-04 17:09:20 -0800257 // this fn's job is to look at raw links and decide what svg classes
258 // need to be applied to the line element in the DOM
259 var th = ts.theme(),
260 el = ldata.el,
261 type = ldata.type(),
262 lw = ldata.linkWidth(),
Simon Hunta142dd22015-02-12 22:07:51 -0800263 online = ldata.online(),
264 delay = immediate ? 0 : 1000;
Simon Hunt1894d792015-02-04 17:09:20 -0800265
266 el.classed('link', true);
267 el.classed('inactive', !online);
268 el.classed(allLinkTypes, false);
269 if (type) {
270 el.classed(type, true);
271 }
272 el.transition()
Simon Hunta142dd22015-02-12 22:07:51 -0800273 .duration(delay)
Simon Hunt1894d792015-02-04 17:09:20 -0800274 .attr('stroke-width', linkScale(lw))
275 .attr('stroke', linkConfig[th].baseColor);
276 }
277
Simon Hunt1894d792015-02-04 17:09:20 -0800278 function removeLinkElement(d) {
279 var idx = fs.find(d.key, network.links, 'key'),
280 removed;
281 if (idx >=0) {
282 // remove from links array
283 removed = network.links.splice(idx, 1);
284 // remove from lookup cache
285 delete lu[removed[0].key];
286 updateLinks();
287 fResume();
288 }
289 }
290
291 function removeHostElement(d, upd) {
292 // first, remove associated hostLink...
293 removeLinkElement(d.linkData);
294
295 // remove hostLink bindings
296 delete lu[d.ingress];
297 delete lu[d.egress];
298
299 // remove from lookup cache
300 delete lu[d.id];
301 // remove from nodes array
302 var idx = fs.find(d.id, network.nodes);
303 network.nodes.splice(idx, 1);
304
305 // remove from SVG
306 // NOTE: upd is false if we were called from removeDeviceElement()
307 if (upd) {
308 updateNodes();
309 fResume();
310 }
311 }
312
313 function removeDeviceElement(d) {
314 var id = d.id;
315 // first, remove associated hosts and links..
Simon Huntdc6adea2015-02-09 22:29:36 -0800316 tms.findAttachedHosts(id).forEach(removeHostElement);
317 tms.findAttachedLinks(id).forEach(removeLinkElement);
Simon Hunt1894d792015-02-04 17:09:20 -0800318
319 // remove from lookup cache
320 delete lu[id];
321 // remove from nodes array
322 var idx = fs.find(id, network.nodes);
323 network.nodes.splice(idx, 1);
324
325 if (!network.nodes.length) {
Simon Huntdc6adea2015-02-09 22:29:36 -0800326 uplink.showNoDevs(true);
Simon Hunt1894d792015-02-04 17:09:20 -0800327 }
328
329 // remove from SVG
330 updateNodes();
331 fResume();
332 }
333
Simon Hunt5724fb42015-02-05 16:59:40 -0800334 function updateHostVisibility() {
Simon Hunt18bf9822015-02-12 17:35:45 -0800335 sus.visible(nodeG.selectAll('.host'), showHosts);
336 sus.visible(linkG.selectAll('.hostLink'), showHosts);
Simon Hunt8eb4d3a2015-02-23 18:23:29 -0800337 sus.visible(linkLabelG.selectAll('.hostLinkLabel'), showHosts);
Simon Hunt5724fb42015-02-05 16:59:40 -0800338 }
339
340 function updateOfflineVisibility(dev) {
341 function updDev(d, show) {
Simon Hunt8eb4d3a2015-02-23 18:23:29 -0800342 var b;
Simon Hunt18bf9822015-02-12 17:35:45 -0800343 sus.visible(d.el, show);
Simon Hunt5724fb42015-02-05 16:59:40 -0800344
Simon Huntdc6adea2015-02-09 22:29:36 -0800345 tms.findAttachedLinks(d.id).forEach(function (link) {
Simon Hunt5724fb42015-02-05 16:59:40 -0800346 b = show && ((link.type() !== 'hostLink') || showHosts);
Simon Hunt18bf9822015-02-12 17:35:45 -0800347 sus.visible(link.el, b);
Simon Hunt5724fb42015-02-05 16:59:40 -0800348 });
Simon Huntdc6adea2015-02-09 22:29:36 -0800349 tms.findAttachedHosts(d.id).forEach(function (host) {
Simon Hunt5724fb42015-02-05 16:59:40 -0800350 b = show && showHosts;
Simon Hunt18bf9822015-02-12 17:35:45 -0800351 sus.visible(host.el, b);
Simon Hunt5724fb42015-02-05 16:59:40 -0800352 });
353 }
354
355 if (dev) {
356 // updating a specific device that just toggled off/on-line
357 updDev(dev, dev.online || showOffline);
358 } else {
359 // updating all offline devices
Simon Huntdc6adea2015-02-09 22:29:36 -0800360 tms.findDevices(true).forEach(function (d) {
Simon Hunt5724fb42015-02-05 16:59:40 -0800361 updDev(d, showOffline);
362 });
363 }
364 }
365
Simon Hunt1894d792015-02-04 17:09:20 -0800366
Simon Hunt445e8152015-02-06 13:00:12 -0800367 function sendUpdateMeta(d, clearPos) {
Simon Huntac4c6f72015-02-03 19:50:53 -0800368 var metaUi = {},
369 ll;
370
Simon Hunt445e8152015-02-06 13:00:12 -0800371 // if we are not clearing the position data (unpinning),
372 // attach the x, y, longitude, latitude...
373 if (!clearPos) {
Simon Hunt3a6eec02015-02-09 21:16:43 -0800374 ll = tms.lngLatFromCoord([d.x, d.y]);
Simon Huntdc6adea2015-02-09 22:29:36 -0800375 metaUi = {x: d.x, y: d.y, lng: ll[0], lat: ll[1]};
Simon Hunt1894d792015-02-04 17:09:20 -0800376 }
377 d.metaUi = metaUi;
Simon Hunt237676b52015-03-10 19:04:26 -0700378 wss.sendEvent('updateMeta', {
Simon Hunt1894d792015-02-04 17:09:20 -0800379 id: d.id,
380 'class': d.class,
381 memento: metaUi
382 });
Simon Huntac4c6f72015-02-03 19:50:53 -0800383 }
384
Simon Hunt1894d792015-02-04 17:09:20 -0800385
Simon Huntac4c6f72015-02-03 19:50:53 -0800386 function mkSvgClass(d) {
387 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
388 }
389
Simon Hunt5724fb42015-02-05 16:59:40 -0800390 function vis(b) {
391 return b ? 'visible' : 'hidden';
392 }
393
394 function toggleHosts() {
395 showHosts = !showHosts;
396 updateHostVisibility();
397 flash.flash('Hosts ' + vis(showHosts));
398 }
399
400 function toggleOffline() {
401 showOffline = !showOffline;
402 updateOfflineVisibility();
403 flash.flash('Offline devices ' + vis(showOffline));
404 }
405
406 function cycleDeviceLabels() {
Simon Hunta4242de2015-02-24 17:11:55 -0800407 td3.incDevLabIndex();
Simon Huntdc6adea2015-02-09 22:29:36 -0800408 tms.findDevices().forEach(function (d) {
Simon Hunta4242de2015-02-24 17:11:55 -0800409 td3.updateDeviceLabel(d);
Simon Hunt1c367112015-02-05 18:02:46 -0800410 });
Simon Hunt5724fb42015-02-05 16:59:40 -0800411 }
412
Simon Hunt445e8152015-02-06 13:00:12 -0800413 function unpin() {
Simon Hunt08f841d02015-02-10 14:39:20 -0800414 var hov = tss.hovered();
415 if (hov) {
416 sendUpdateMeta(hov, true);
417 hov.fixed = false;
418 hov.el.classed('fixed', false);
Simon Hunt445e8152015-02-06 13:00:12 -0800419 fResume();
420 }
421 }
422
Simon Hunta142dd22015-02-12 22:07:51 -0800423 function showMastership(masterId) {
424 if (!masterId) {
425 restoreLayerState();
426 } else {
427 showMastershipFor(masterId);
428 }
429 }
430
431 function restoreLayerState() {
432 // NOTE: this level of indirection required, for when we have
433 // the layer filter functionality re-implemented
434 suppressLayers(false);
435 }
436
437 function showMastershipFor(id) {
438 suppressLayers(true);
439 node.each(function (n) {
440 if (n.master === id) {
441 n.el.classed('suppressed', false);
442 }
443 });
444 }
445
446 function suppressLayers(b) {
447 node.classed('suppressed', b);
448 link.classed('suppressed', b);
449// d3.selectAll('svg .port').classed('inactive', b);
450// d3.selectAll('svg .portText').classed('inactive', b);
451 }
Simon Hunt445e8152015-02-06 13:00:12 -0800452
Simon Hunt5724fb42015-02-05 16:59:40 -0800453 // ==========================================
454
Simon Huntac4c6f72015-02-03 19:50:53 -0800455 function updateNodes() {
Simon Hunt1894d792015-02-04 17:09:20 -0800456 // select all the nodes in the layout:
Simon Huntac4c6f72015-02-03 19:50:53 -0800457 node = nodeG.selectAll('.node')
458 .data(network.nodes, function (d) { return d.id; });
459
Simon Hunt1894d792015-02-04 17:09:20 -0800460 // operate on existing nodes:
Simon Hunta4242de2015-02-24 17:11:55 -0800461 node.filter('.device').each(td3.deviceExisting);
462 node.filter('.host').each(td3.hostExisting);
Simon Huntac4c6f72015-02-03 19:50:53 -0800463
464 // operate on entering nodes:
465 var entering = node.enter()
466 .append('g')
467 .attr({
468 id: function (d) { return sus.safeId(d.id); },
469 class: mkSvgClass,
470 transform: function (d) { return sus.translate(d.x, d.y); },
471 opacity: 0
472 })
473 .call(drag)
Simon Hunt08f841d02015-02-10 14:39:20 -0800474 .on('mouseover', tss.nodeMouseOver)
475 .on('mouseout', tss.nodeMouseOut)
Simon Huntac4c6f72015-02-03 19:50:53 -0800476 .transition()
477 .attr('opacity', 1);
478
Simon Hunt1894d792015-02-04 17:09:20 -0800479 // augment entering nodes:
Simon Hunta4242de2015-02-24 17:11:55 -0800480 entering.filter('.device').each(td3.deviceEnter);
481 entering.filter('.host').each(td3.hostEnter);
Simon Huntac4c6f72015-02-03 19:50:53 -0800482
Simon Hunt51056592015-02-03 21:48:07 -0800483 // operate on both existing and new nodes:
Simon Hunta4242de2015-02-24 17:11:55 -0800484 td3.updateDeviceColors();
Simon Huntac4c6f72015-02-03 19:50:53 -0800485
486 // operate on exiting nodes:
487 // Note that the node is removed after 2 seconds.
488 // Sub element animations should be shorter than 2 seconds.
489 var exiting = node.exit()
490 .transition()
491 .duration(2000)
492 .style('opacity', 0)
493 .remove();
494
Simon Hunt1894d792015-02-04 17:09:20 -0800495 // exiting node specifics:
Simon Hunta4242de2015-02-24 17:11:55 -0800496 exiting.filter('.host').each(td3.hostExit);
497 exiting.filter('.device').each(td3.deviceExit);
Simon Huntac4c6f72015-02-03 19:50:53 -0800498
Simon Hunt51056592015-02-03 21:48:07 -0800499 // finally, resume the force layout
Simon Huntac4c6f72015-02-03 19:50:53 -0800500 fResume();
501 }
502
Simon Hunt51056592015-02-03 21:48:07 -0800503 // ==========================
Simon Hunt1894d792015-02-04 17:09:20 -0800504
505 function updateLinks() {
506 var th = ts.theme();
507
508 link = linkG.selectAll('.link')
509 .data(network.links, function (d) { return d.key; });
510
511 // operate on existing links:
Simon Huntd5264122015-02-25 10:17:43 -0800512 link.each(function (d) {
513 // this is supposed to be an existing link, but we have observed
514 // occasions (where links are deleted and added rapidly?) where
515 // the DOM element has not been defined. So protect against that...
516 if (d.el) {
517 restyleLinkElement(d, true);
518 }
519 });
Simon Hunt1894d792015-02-04 17:09:20 -0800520
521 // operate on entering links:
522 var entering = link.enter()
523 .append('line')
524 .attr({
Simon Huntd5264122015-02-25 10:17:43 -0800525 x1: function (d) { return d.source.x; },
526 y1: function (d) { return d.source.y; },
527 x2: function (d) { return d.target.x; },
528 y2: function (d) { return d.target.y; },
Simon Hunt1894d792015-02-04 17:09:20 -0800529 stroke: linkConfig[th].inColor,
530 'stroke-width': linkConfig.inWidth
531 });
532
533 // augment links
Simon Hunta4242de2015-02-24 17:11:55 -0800534 entering.each(td3.linkEntering);
Simon Hunt1894d792015-02-04 17:09:20 -0800535
536 // operate on both existing and new links:
537 //link.each(...)
538
539 // apply or remove labels
Simon Hunta4242de2015-02-24 17:11:55 -0800540 td3.applyLinkLabels();
Simon Hunt1894d792015-02-04 17:09:20 -0800541
542 // operate on exiting links:
543 link.exit()
544 .attr('stroke-dasharray', '3 3')
Simon Hunt5724fb42015-02-05 16:59:40 -0800545 .attr('stroke', linkConfig[th].outColor)
Simon Hunt1894d792015-02-04 17:09:20 -0800546 .style('opacity', 0.5)
547 .transition()
548 .duration(1500)
549 .attr({
550 'stroke-dasharray': '3 12',
Simon Hunt1894d792015-02-04 17:09:20 -0800551 'stroke-width': linkConfig.outWidth
552 })
553 .style('opacity', 0.0)
554 .remove();
Simon Hunt1894d792015-02-04 17:09:20 -0800555 }
556
Simon Huntac4c6f72015-02-03 19:50:53 -0800557
558 // ==========================
Simon Hunt737c89f2015-01-28 12:23:19 -0800559 // force layout tick function
Simon Hunt737c89f2015-01-28 12:23:19 -0800560
Simon Hunt5724fb42015-02-05 16:59:40 -0800561 function fResume() {
Simon Huntc3c5b672015-02-20 11:32:13 -0800562 if (!tos.isOblique()) {
Simon Hunt5724fb42015-02-05 16:59:40 -0800563 force.resume();
564 }
565 }
566
567 function fStart() {
Simon Huntc3c5b672015-02-20 11:32:13 -0800568 if (!tos.isOblique()) {
Simon Hunt5724fb42015-02-05 16:59:40 -0800569 force.start();
570 }
571 }
572
573 var tickStuff = {
574 nodeAttr: {
575 transform: function (d) { return sus.translate(d.x, d.y); }
576 },
577 linkAttr: {
578 x1: function (d) { return d.source.x; },
579 y1: function (d) { return d.source.y; },
580 x2: function (d) { return d.target.x; },
581 y2: function (d) { return d.target.y; }
582 },
583 linkLabelAttr: {
584 transform: function (d) {
Simon Huntdc6adea2015-02-09 22:29:36 -0800585 var lnk = tms.findLinkById(d.key);
Simon Hunt5724fb42015-02-05 16:59:40 -0800586 if (lnk) {
Simon Hunta4242de2015-02-24 17:11:55 -0800587 return td3.transformLabel({
Simon Hunt5724fb42015-02-05 16:59:40 -0800588 x1: lnk.source.x,
589 y1: lnk.source.y,
590 x2: lnk.target.x,
591 y2: lnk.target.y
592 });
593 }
594 }
595 }
596 };
597
598 function tick() {
Simon Hunt3ab20282015-02-26 20:32:19 -0800599 // guard against null (which can happen when our view pages out)...
600 if (node) node.attr(tickStuff.nodeAttr);
601 if (link) link.attr(tickStuff.linkAttr);
602 if (linkLabel) linkLabel.attr(tickStuff.linkLabelAttr);
Simon Hunt737c89f2015-01-28 12:23:19 -0800603 }
604
605
Simon Huntac4c6f72015-02-03 19:50:53 -0800606 // ==========================
607 // === MOUSE GESTURE HANDLERS
608
Simon Hunt205099e2015-02-07 13:12:01 -0800609 function zoomingOrPanning(ev) {
610 return ev.metaKey || ev.altKey;
Simon Hunt445e8152015-02-06 13:00:12 -0800611 }
612
613 function atDragEnd(d) {
614 // once we've finished moving, pin the node in position
615 d.fixed = true;
616 d3.select(this).classed('fixed', true);
617 sendUpdateMeta(d);
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700618 tss.clickConsumed(true);
Simon Hunt445e8152015-02-06 13:00:12 -0800619 }
620
621 // predicate that indicates when dragging is active
622 function dragEnabled() {
623 var ev = d3.event.sourceEvent;
624 // nodeLock means we aren't allowing nodes to be dragged...
Simon Hunt205099e2015-02-07 13:12:01 -0800625 return !nodeLock && !zoomingOrPanning(ev);
Simon Hunt445e8152015-02-06 13:00:12 -0800626 }
627
628 // predicate that indicates when clicking is active
629 function clickEnabled() {
630 return true;
631 }
Simon Hunt737c89f2015-01-28 12:23:19 -0800632
Simon Huntf542d842015-02-11 16:20:33 -0800633 // ==========================
634 // function entry points for traffic module
635
636 var allTrafficClasses = 'primary secondary animated optical';
637
638 function clearLinkTrafficStyle() {
639 link.style('stroke-width', null)
640 .classed(allTrafficClasses, false);
641 }
642
643 function removeLinkLabels() {
644 network.links.forEach(function (d) {
645 d.label = '';
646 });
647 }
Simon Hunt737c89f2015-01-28 12:23:19 -0800648
Simon Hunta4242de2015-02-24 17:11:55 -0800649 function updateLinkLabelModel() {
650 // create the backing data for showing labels..
651 var data = [];
652 link.each(function (d) {
653 if (d.label) {
654 data.push({
655 id: 'lab-' + d.key,
656 key: d.key,
657 label: d.label,
658 ldata: d
659 });
660 }
661 });
662
663 linkLabel = linkLabelG.selectAll('.linkLabel')
664 .data(data, function (d) { return d.id; });
665 }
666
Simon Hunt737c89f2015-01-28 12:23:19 -0800667 // ==========================
Simon Huntac4c6f72015-02-03 19:50:53 -0800668 // Module definition
Simon Hunt737c89f2015-01-28 12:23:19 -0800669
Simon Huntdc6adea2015-02-09 22:29:36 -0800670 function mkModelApi(uplink) {
671 return {
672 projection: uplink.projection,
673 network: network,
674 restyleLinkElement: restyleLinkElement,
675 removeLinkElement: removeLinkElement
676 };
677 }
678
Simon Hunta4242de2015-02-24 17:11:55 -0800679 function mkD3Api(uplink) {
680 return {
681 node: function () { return node; },
682 link: function () { return link; },
683 linkLabel: function () { return linkLabel; },
684 instVisible: function () { return tis.isVisible(); },
685 posNode: tms.positionNode,
686 showHosts: function () { return showHosts; },
687 restyleLinkElement: restyleLinkElement,
688 updateLinkLabelModel: updateLinkLabelModel
689 }
690 }
691
Simon Hunt08f841d02015-02-10 14:39:20 -0800692 function mkSelectApi(uplink) {
693 return {
694 node: function () { return node; },
695 zoomingOrPanning: zoomingOrPanning,
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700696 updateDeviceColors: td3.updateDeviceColors,
697 deselectLink: tls.deselectLink
Simon Hunt08f841d02015-02-10 14:39:20 -0800698 };
699 }
700
Simon Huntf542d842015-02-11 16:20:33 -0800701 function mkTrafficApi(uplink) {
702 return {
703 clearLinkTrafficStyle: clearLinkTrafficStyle,
704 removeLinkLabels: removeLinkLabels,
705 updateLinks: updateLinks,
706 findLinkById: tms.findLinkById,
707 hovered: tss.hovered,
708 validateSelectionContext: tss.validateSelectionContext,
Simon Hunt237676b52015-03-10 19:04:26 -0700709 selectOrder: tss.selectOrder
Simon Huntf542d842015-02-11 16:20:33 -0800710 }
711 }
712
Simon Huntc3c5b672015-02-20 11:32:13 -0800713 function mkObliqueApi(uplink, fltr) {
Simon Hunt96f88c62015-02-19 17:57:25 -0800714 return {
Simon Huntc3c5b672015-02-20 11:32:13 -0800715 force: function() { return force; },
716 zoomLayer: uplink.zoomLayer,
717 nodeGBBox: function() { return nodeG.node().getBBox(); },
Simon Hunt96f88c62015-02-19 17:57:25 -0800718 node: function () { return node; },
Simon Huntc3c5b672015-02-20 11:32:13 -0800719 link: function () { return link; },
720 linkLabel: function () { return linkLabel; },
721 nodes: function () { return network.nodes; },
722 tickStuff: tickStuff,
723 nodeLock: function (b) {
724 var old = nodeLock;
725 nodeLock = b;
726 return old;
727 },
728 opacifyMap: uplink.opacifyMap,
729 inLayer: fltr.inLayer
Simon Hunt96f88c62015-02-19 17:57:25 -0800730 };
731 }
732
Simon Hunteb0fa052015-02-17 19:20:28 -0800733 function mkFilterApi(uplink) {
734 return {
735 node: function () { return node; },
736 link: function () { return link; }
737 };
738 }
739
Simon Hunt9e2104c2015-02-26 10:48:59 -0800740 function mkLinkApi(svg, uplink) {
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800741 return {
742 svg: svg,
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800743 zoomer: uplink.zoomer(),
744 network: network,
Simon Hunt1a5301e2015-02-25 15:31:25 -0800745 portLabelG: function () { return portLabelG; },
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800746 showHosts: function () { return showHosts; }
747 };
748 }
749
Simon Hunt737c89f2015-01-28 12:23:19 -0800750 angular.module('ovTopo')
751 .factory('TopoForceService',
Simon Hunt1894d792015-02-04 17:09:20 -0800752 ['$log', 'FnService', 'SvgUtilService', 'IconService', 'ThemeService',
Simon Hunt237676b52015-03-10 19:04:26 -0700753 'FlashService', 'WebSocketService',
754 'TopoInstService', 'TopoModelService',
Simon Hunta4242de2015-02-24 17:11:55 -0800755 'TopoD3Service', 'TopoSelectService', 'TopoTrafficService',
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800756 'TopoObliqueService', 'TopoFilterService', 'TopoLinkService',
Simon Hunt737c89f2015-01-28 12:23:19 -0800757
Simon Hunt237676b52015-03-10 19:04:26 -0700758 function (_$log_, _fs_, _sus_, _is_, _ts_, _flash_, _wss_,
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800759 _tis_, _tms_, _td3_, _tss_, _tts_, _tos_, _fltr_, _tls_) {
Simon Hunt737c89f2015-01-28 12:23:19 -0800760 $log = _$log_;
Simon Hunt1894d792015-02-04 17:09:20 -0800761 fs = _fs_;
Simon Hunt737c89f2015-01-28 12:23:19 -0800762 sus = _sus_;
Simon Huntac4c6f72015-02-03 19:50:53 -0800763 is = _is_;
764 ts = _ts_;
Simon Hunt5724fb42015-02-05 16:59:40 -0800765 flash = _flash_;
Simon Hunt237676b52015-03-10 19:04:26 -0700766 wss = _wss_;
Simon Huntac4c6f72015-02-03 19:50:53 -0800767 tis = _tis_;
Simon Hunt3a6eec02015-02-09 21:16:43 -0800768 tms = _tms_;
Simon Hunta4242de2015-02-24 17:11:55 -0800769 td3 = _td3_;
Simon Hunt08f841d02015-02-10 14:39:20 -0800770 tss = _tss_;
Simon Huntf542d842015-02-11 16:20:33 -0800771 tts = _tts_;
Simon Hunt96f88c62015-02-19 17:57:25 -0800772 tos = _tos_;
Simon Hunteb0fa052015-02-17 19:20:28 -0800773 fltr = _fltr_;
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800774 tls = _tls_;
Simon Hunt737c89f2015-01-28 12:23:19 -0800775
Simon Hunt1894d792015-02-04 17:09:20 -0800776 icfg = is.iconConfig();
777
Simon Hunta142dd22015-02-12 22:07:51 -0800778 var themeListener = ts.addListener(function () {
779 updateLinks();
780 updateNodes();
781 });
782
Simon Hunt737c89f2015-01-28 12:23:19 -0800783 // forceG is the SVG group to display the force layout in
Simon Huntdc6adea2015-02-09 22:29:36 -0800784 // uplink is the api from the main topo source file
Simon Hunt3a6eec02015-02-09 21:16:43 -0800785 // dim is the initial dimensions of the SVG as [w,h]
Simon Hunt737c89f2015-01-28 12:23:19 -0800786 // opts are, well, optional :)
Simon Hunt3ab20282015-02-26 20:32:19 -0800787 function initForce(_svg_, forceG, _uplink_, _dim_, opts) {
Simon Hunt1894d792015-02-04 17:09:20 -0800788 uplink = _uplink_;
Simon Hunt3a6eec02015-02-09 21:16:43 -0800789 dim = _dim_;
Simon Hunt3ab20282015-02-26 20:32:19 -0800790 svg = _svg_;
791
792 lu = network.lookup;
793 rlk = network.revLinkToKey;
Simon Hunt3a6eec02015-02-09 21:16:43 -0800794
795 $log.debug('initForce().. dim = ' + dim);
796
Simon Huntdc6adea2015-02-09 22:29:36 -0800797 tms.initModel(mkModelApi(uplink), dim);
Simon Hunta4242de2015-02-24 17:11:55 -0800798 td3.initD3(mkD3Api(uplink));
Simon Hunt08f841d02015-02-10 14:39:20 -0800799 tss.initSelect(mkSelectApi(uplink));
Simon Huntf542d842015-02-11 16:20:33 -0800800 tts.initTraffic(mkTrafficApi(uplink));
Simon Huntc3c5b672015-02-20 11:32:13 -0800801 tos.initOblique(mkObliqueApi(uplink, fltr));
Simon Hunteb0fa052015-02-17 19:20:28 -0800802 fltr.initFilter(mkFilterApi(uplink), d3.select('#mast-right'));
Simon Hunt9e2104c2015-02-26 10:48:59 -0800803 tls.initLink(mkLinkApi(svg, uplink), td3);
Simon Hunta11b4eb2015-01-28 16:20:50 -0800804
Simon Hunt737c89f2015-01-28 12:23:19 -0800805 settings = angular.extend({}, defaultSettings, opts);
806
807 linkG = forceG.append('g').attr('id', 'topo-links');
808 linkLabelG = forceG.append('g').attr('id', 'topo-linkLabels');
809 nodeG = forceG.append('g').attr('id', 'topo-nodes');
Simon Hunt1a5301e2015-02-25 15:31:25 -0800810 portLabelG = forceG.append('g').attr('id', 'topo-portLabels');
Simon Hunt737c89f2015-01-28 12:23:19 -0800811
812 link = linkG.selectAll('.link');
813 linkLabel = linkLabelG.selectAll('.linkLabel');
814 node = nodeG.selectAll('.node');
815
816 force = d3.layout.force()
Simon Hunt3a6eec02015-02-09 21:16:43 -0800817 .size(dim)
Simon Hunt737c89f2015-01-28 12:23:19 -0800818 .nodes(network.nodes)
819 .links(network.links)
820 .gravity(settings.gravity)
821 .friction(settings.friction)
822 .charge(settings.charge._def_)
823 .linkDistance(settings.linkDistance._def_)
824 .linkStrength(settings.linkStrength._def_)
825 .on('tick', tick);
826
827 drag = sus.createDragBehavior(force,
Simon Hunt08f841d02015-02-10 14:39:20 -0800828 tss.selectObject, atDragEnd, dragEnabled, clickEnabled);
Simon Hunt737c89f2015-01-28 12:23:19 -0800829 }
830
Simon Hunt3a6eec02015-02-09 21:16:43 -0800831 function newDim(_dim_) {
832 dim = _dim_;
833 force.size(dim);
834 tms.newDim(dim);
Simon Hunt737c89f2015-01-28 12:23:19 -0800835 }
836
Simon Hunt3a6eec02015-02-09 21:16:43 -0800837 function destroyForce() {
Simon Hunt3ab20282015-02-26 20:32:19 -0800838 force.stop();
839
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800840 tls.destroyLink();
Simon Hunteb0fa052015-02-17 19:20:28 -0800841 fltr.destroyFilter();
Simon Hunt96f88c62015-02-19 17:57:25 -0800842 tos.destroyOblique();
Simon Huntf542d842015-02-11 16:20:33 -0800843 tts.destroyTraffic();
844 tss.destroySelect();
Simon Hunta4242de2015-02-24 17:11:55 -0800845 td3.destroyD3();
Simon Huntf542d842015-02-11 16:20:33 -0800846 tms.destroyModel();
Simon Hunta142dd22015-02-12 22:07:51 -0800847 ts.removeListener(themeListener);
848 themeListener = null;
Simon Hunt3ab20282015-02-26 20:32:19 -0800849
850 // clean up the DOM
851 svg.selectAll('g').remove();
852 svg.selectAll('defs').remove();
853
854 // clean up internal state
855 network.nodes = [];
856 network.links = [];
857 network.lookup = {};
858 network.revLinkToKey = {};
859
860 linkG = linkLabelG = nodeG = portLabelG = null;
861 link = linkLabel = node = null;
862 force = drag = null;
Simon Hunt3a6eec02015-02-09 21:16:43 -0800863 }
864
Simon Hunt737c89f2015-01-28 12:23:19 -0800865 return {
866 initForce: initForce,
Simon Hunt3a6eec02015-02-09 21:16:43 -0800867 newDim: newDim,
868 destroyForce: destroyForce,
Simon Huntac4c6f72015-02-03 19:50:53 -0800869
Simon Hunta4242de2015-02-24 17:11:55 -0800870 updateDeviceColors: td3.updateDeviceColors,
Simon Hunt5724fb42015-02-05 16:59:40 -0800871 toggleHosts: toggleHosts,
Simon Hunt9e2104c2015-02-26 10:48:59 -0800872 togglePorts: tls.togglePorts,
Simon Hunt5724fb42015-02-05 16:59:40 -0800873 toggleOffline: toggleOffline,
874 cycleDeviceLabels: cycleDeviceLabels,
Simon Hunt445e8152015-02-06 13:00:12 -0800875 unpin: unpin,
Simon Hunta142dd22015-02-12 22:07:51 -0800876 showMastership: showMastership,
Simon Huntac4c6f72015-02-03 19:50:53 -0800877
878 addDevice: addDevice,
Simon Hunt1894d792015-02-04 17:09:20 -0800879 updateDevice: updateDevice,
880 removeDevice: removeDevice,
881 addHost: addHost,
882 updateHost: updateHost,
883 removeHost: removeHost,
884 addLink: addLink,
885 updateLink: updateLink,
886 removeLink: removeLink
Simon Hunt737c89f2015-01-28 12:23:19 -0800887 };
888 }]);
889}());