blob: 2509290fbd3dd5b171d97d9b138484b87d3b4e58 [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 Huntfb8ea1f2015-02-24 21:38:09 -080026 var $log, fs, sus, is, ts, flash, tis, tms, td3, tss, tts, tos, fltr, tls,
Simon Hunteb0fa052015-02-17 19:20:28 -080027 icfg, uplink;
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: [],
52 lookup: {},
53 revLinkToKey: {}
Simon Huntac4c6f72015-02-03 19:50:53 -080054 },
Simon Hunt1894d792015-02-04 17:09:20 -080055 lu = network.lookup, // shorthand
Simon Huntdc6adea2015-02-09 22:29:36 -080056 rlk = network.revLinkToKey,
Simon Hunta142dd22015-02-12 22:07:51 -080057 showHosts = false, // whether hosts are displayed
Simon Hunt5724fb42015-02-05 16:59:40 -080058 showOffline = true, // whether offline devices are displayed
Simon Hunt445e8152015-02-06 13:00:12 -080059 nodeLock = false, // whether nodes can be dragged or not (locked)
Simon Hunt08f841d02015-02-10 14:39:20 -080060 dim; // the dimensions of the force layout [w,h]
Simon Hunt737c89f2015-01-28 12:23:19 -080061
62 // SVG elements;
63 var linkG, linkLabelG, nodeG;
64
65 // D3 selections;
66 var link, linkLabel, node;
67
68 // default settings for force layout
69 var defaultSettings = {
70 gravity: 0.4,
71 friction: 0.7,
72 charge: {
73 // note: key is node.class
74 device: -8000,
75 host: -5000,
76 _def_: -12000
77 },
78 linkDistance: {
79 // note: key is link.type
80 direct: 100,
81 optical: 120,
82 hostLink: 3,
83 _def_: 50
84 },
85 linkStrength: {
86 // note: key is link.type
87 // range: {0.0 ... 1.0}
88 //direct: 1.0,
89 //optical: 1.0,
90 //hostLink: 1.0,
91 _def_: 1.0
92 }
93 };
94
95
Simon Huntac4c6f72015-02-03 19:50:53 -080096 // ==========================
97 // === EVENT HANDLERS
98
99 function addDevice(data) {
100 var id = data.id,
101 d;
102
Simon Hunt1894d792015-02-04 17:09:20 -0800103 uplink.showNoDevs(false);
Simon Huntac4c6f72015-02-03 19:50:53 -0800104
105 // although this is an add device event, if we already have the
106 // device, treat it as an update instead..
Simon Hunt1894d792015-02-04 17:09:20 -0800107 if (lu[id]) {
Simon Huntac4c6f72015-02-03 19:50:53 -0800108 updateDevice(data);
109 return;
110 }
111
Simon Hunt3a6eec02015-02-09 21:16:43 -0800112 d = tms.createDeviceNode(data);
Simon Huntac4c6f72015-02-03 19:50:53 -0800113 network.nodes.push(d);
Simon Hunt1894d792015-02-04 17:09:20 -0800114 lu[id] = d;
Simon Huntac4c6f72015-02-03 19:50:53 -0800115 updateNodes();
116 fStart();
117 }
118
119 function updateDevice(data) {
120 var id = data.id,
Simon Hunt1894d792015-02-04 17:09:20 -0800121 d = lu[id],
Simon Huntac4c6f72015-02-03 19:50:53 -0800122 wasOnline;
123
124 if (d) {
125 wasOnline = d.online;
126 angular.extend(d, data);
Simon Hunt3a6eec02015-02-09 21:16:43 -0800127 if (tms.positionNode(d, true)) {
Simon Hunt445e8152015-02-06 13:00:12 -0800128 sendUpdateMeta(d);
Simon Huntac4c6f72015-02-03 19:50:53 -0800129 }
130 updateNodes();
131 if (wasOnline !== d.online) {
Simon Huntdc6adea2015-02-09 22:29:36 -0800132 tms.findAttachedLinks(d.id).forEach(restyleLinkElement);
Simon Hunt5724fb42015-02-05 16:59:40 -0800133 updateOfflineVisibility(d);
Simon Huntac4c6f72015-02-03 19:50:53 -0800134 }
Simon Huntac4c6f72015-02-03 19:50:53 -0800135 }
136 }
137
Simon Hunt1894d792015-02-04 17:09:20 -0800138 function removeDevice(data) {
139 var id = data.id,
140 d = lu[id];
141 if (d) {
142 removeDeviceElement(d);
Simon Hunt1894d792015-02-04 17:09:20 -0800143 }
144 }
145
146 function addHost(data) {
147 var id = data.id,
148 d, lnk;
149
150 // although this is an add host event, if we already have the
151 // host, treat it as an update instead..
152 if (lu[id]) {
153 updateHost(data);
154 return;
155 }
156
Simon Hunt3a6eec02015-02-09 21:16:43 -0800157 d = tms.createHostNode(data);
Simon Hunt1894d792015-02-04 17:09:20 -0800158 network.nodes.push(d);
159 lu[id] = d;
Simon Hunt1894d792015-02-04 17:09:20 -0800160 updateNodes();
161
Simon Hunt3a6eec02015-02-09 21:16:43 -0800162 lnk = tms.createHostLink(data);
Simon Hunt1894d792015-02-04 17:09:20 -0800163 if (lnk) {
Simon Hunt1894d792015-02-04 17:09:20 -0800164 d.linkData = lnk; // cache ref on its host
165 network.links.push(lnk);
166 lu[d.ingress] = lnk;
167 lu[d.egress] = lnk;
168 updateLinks();
169 }
170
171 fStart();
172 }
173
174 function updateHost(data) {
175 var id = data.id,
176 d = lu[id];
177 if (d) {
178 angular.extend(d, data);
Simon Hunt3a6eec02015-02-09 21:16:43 -0800179 if (tms.positionNode(d, true)) {
Simon Hunt445e8152015-02-06 13:00:12 -0800180 sendUpdateMeta(d);
Simon Hunt1894d792015-02-04 17:09:20 -0800181 }
182 updateNodes();
Simon Hunt1894d792015-02-04 17:09:20 -0800183 }
184 }
185
186 function removeHost(data) {
187 var id = data.id,
188 d = lu[id];
189 if (d) {
190 removeHostElement(d, true);
Simon Hunt1894d792015-02-04 17:09:20 -0800191 }
192 }
193
194 function addLink(data) {
Simon Huntdc6adea2015-02-09 22:29:36 -0800195 var result = tms.findLink(data, 'add'),
Simon Hunt1894d792015-02-04 17:09:20 -0800196 bad = result.badLogic,
197 d = result.ldata;
198
199 if (bad) {
200 //logicError(bad + ': ' + link.id);
201 return;
202 }
203
204 if (d) {
205 // we already have a backing store link for src/dst nodes
206 addLinkUpdate(d, data);
207 return;
208 }
209
210 // no backing store link yet
Simon Hunt3a6eec02015-02-09 21:16:43 -0800211 d = tms.createLink(data);
Simon Hunt1894d792015-02-04 17:09:20 -0800212 if (d) {
213 network.links.push(d);
214 lu[d.key] = d;
215 updateLinks();
216 fStart();
217 }
218 }
219
220 function updateLink(data) {
Simon Huntdc6adea2015-02-09 22:29:36 -0800221 var result = tms.findLink(data, 'update'),
Simon Hunt1894d792015-02-04 17:09:20 -0800222 bad = result.badLogic;
223 if (bad) {
224 //logicError(bad + ': ' + link.id);
225 return;
226 }
227 result.updateWith(link);
228 }
229
230 function removeLink(data) {
Simon Hunta4242de2015-02-24 17:11:55 -0800231 var result = tms.findLink(data, 'remove');
232
233 if (!result.badLogic) {
234 result.removeRawLink();
Simon Hunt1894d792015-02-04 17:09:20 -0800235 }
Simon Hunt1894d792015-02-04 17:09:20 -0800236 }
237
238 // ========================
239
240 function addLinkUpdate(ldata, link) {
241 // add link event, but we already have the reverse link installed
242 ldata.fromTarget = link;
Simon Huntdc6adea2015-02-09 22:29:36 -0800243 rlk[link.id] = ldata.key;
Simon Hunt1894d792015-02-04 17:09:20 -0800244 restyleLinkElement(ldata);
245 }
246
Simon Hunt1894d792015-02-04 17:09:20 -0800247
248 var widthRatio = 1.4,
249 linkScale = d3.scale.linear()
250 .domain([1, 12])
251 .range([widthRatio, 12 * widthRatio])
Simon Hunt5724fb42015-02-05 16:59:40 -0800252 .clamp(true),
Simon Hunt3a6eec02015-02-09 21:16:43 -0800253 allLinkTypes = 'direct indirect optical tunnel';
Simon Hunt1894d792015-02-04 17:09:20 -0800254
Simon Hunta142dd22015-02-12 22:07:51 -0800255 function restyleLinkElement(ldata, immediate) {
Simon Hunt1894d792015-02-04 17:09:20 -0800256 // this fn's job is to look at raw links and decide what svg classes
257 // need to be applied to the line element in the DOM
258 var th = ts.theme(),
259 el = ldata.el,
260 type = ldata.type(),
261 lw = ldata.linkWidth(),
Simon Hunta142dd22015-02-12 22:07:51 -0800262 online = ldata.online(),
263 delay = immediate ? 0 : 1000;
Simon Hunt1894d792015-02-04 17:09:20 -0800264
265 el.classed('link', true);
266 el.classed('inactive', !online);
267 el.classed(allLinkTypes, false);
268 if (type) {
269 el.classed(type, true);
270 }
271 el.transition()
Simon Hunta142dd22015-02-12 22:07:51 -0800272 .duration(delay)
Simon Hunt1894d792015-02-04 17:09:20 -0800273 .attr('stroke-width', linkScale(lw))
274 .attr('stroke', linkConfig[th].baseColor);
275 }
276
Simon Hunt1894d792015-02-04 17:09:20 -0800277 function removeLinkElement(d) {
278 var idx = fs.find(d.key, network.links, 'key'),
279 removed;
280 if (idx >=0) {
281 // remove from links array
282 removed = network.links.splice(idx, 1);
283 // remove from lookup cache
284 delete lu[removed[0].key];
285 updateLinks();
286 fResume();
287 }
288 }
289
290 function removeHostElement(d, upd) {
291 // first, remove associated hostLink...
292 removeLinkElement(d.linkData);
293
294 // remove hostLink bindings
295 delete lu[d.ingress];
296 delete lu[d.egress];
297
298 // remove from lookup cache
299 delete lu[d.id];
300 // remove from nodes array
301 var idx = fs.find(d.id, network.nodes);
302 network.nodes.splice(idx, 1);
303
304 // remove from SVG
305 // NOTE: upd is false if we were called from removeDeviceElement()
306 if (upd) {
307 updateNodes();
308 fResume();
309 }
310 }
311
312 function removeDeviceElement(d) {
313 var id = d.id;
314 // first, remove associated hosts and links..
Simon Huntdc6adea2015-02-09 22:29:36 -0800315 tms.findAttachedHosts(id).forEach(removeHostElement);
316 tms.findAttachedLinks(id).forEach(removeLinkElement);
Simon Hunt1894d792015-02-04 17:09:20 -0800317
318 // remove from lookup cache
319 delete lu[id];
320 // remove from nodes array
321 var idx = fs.find(id, network.nodes);
322 network.nodes.splice(idx, 1);
323
324 if (!network.nodes.length) {
Simon Huntdc6adea2015-02-09 22:29:36 -0800325 uplink.showNoDevs(true);
Simon Hunt1894d792015-02-04 17:09:20 -0800326 }
327
328 // remove from SVG
329 updateNodes();
330 fResume();
331 }
332
Simon Hunt5724fb42015-02-05 16:59:40 -0800333 function updateHostVisibility() {
Simon Hunt18bf9822015-02-12 17:35:45 -0800334 sus.visible(nodeG.selectAll('.host'), showHosts);
335 sus.visible(linkG.selectAll('.hostLink'), showHosts);
Simon Hunt8eb4d3a2015-02-23 18:23:29 -0800336 sus.visible(linkLabelG.selectAll('.hostLinkLabel'), showHosts);
Simon Hunt5724fb42015-02-05 16:59:40 -0800337 }
338
339 function updateOfflineVisibility(dev) {
340 function updDev(d, show) {
Simon Hunt8eb4d3a2015-02-23 18:23:29 -0800341 var b;
Simon Hunt18bf9822015-02-12 17:35:45 -0800342 sus.visible(d.el, show);
Simon Hunt5724fb42015-02-05 16:59:40 -0800343
Simon Huntdc6adea2015-02-09 22:29:36 -0800344 tms.findAttachedLinks(d.id).forEach(function (link) {
Simon Hunt5724fb42015-02-05 16:59:40 -0800345 b = show && ((link.type() !== 'hostLink') || showHosts);
Simon Hunt18bf9822015-02-12 17:35:45 -0800346 sus.visible(link.el, b);
Simon Hunt5724fb42015-02-05 16:59:40 -0800347 });
Simon Huntdc6adea2015-02-09 22:29:36 -0800348 tms.findAttachedHosts(d.id).forEach(function (host) {
Simon Hunt5724fb42015-02-05 16:59:40 -0800349 b = show && showHosts;
Simon Hunt18bf9822015-02-12 17:35:45 -0800350 sus.visible(host.el, b);
Simon Hunt5724fb42015-02-05 16:59:40 -0800351 });
352 }
353
354 if (dev) {
355 // updating a specific device that just toggled off/on-line
356 updDev(dev, dev.online || showOffline);
357 } else {
358 // updating all offline devices
Simon Huntdc6adea2015-02-09 22:29:36 -0800359 tms.findDevices(true).forEach(function (d) {
Simon Hunt5724fb42015-02-05 16:59:40 -0800360 updDev(d, showOffline);
361 });
362 }
363 }
364
Simon Hunt1894d792015-02-04 17:09:20 -0800365
Simon Hunt445e8152015-02-06 13:00:12 -0800366 function sendUpdateMeta(d, clearPos) {
Simon Huntac4c6f72015-02-03 19:50:53 -0800367 var metaUi = {},
368 ll;
369
Simon Hunt445e8152015-02-06 13:00:12 -0800370 // if we are not clearing the position data (unpinning),
371 // attach the x, y, longitude, latitude...
372 if (!clearPos) {
Simon Hunt3a6eec02015-02-09 21:16:43 -0800373 ll = tms.lngLatFromCoord([d.x, d.y]);
Simon Huntdc6adea2015-02-09 22:29:36 -0800374 metaUi = {x: d.x, y: d.y, lng: ll[0], lat: ll[1]};
Simon Hunt1894d792015-02-04 17:09:20 -0800375 }
376 d.metaUi = metaUi;
377 uplink.sendEvent('updateMeta', {
378 id: d.id,
379 'class': d.class,
380 memento: metaUi
381 });
Simon Huntac4c6f72015-02-03 19:50:53 -0800382 }
383
Simon Hunt1894d792015-02-04 17:09:20 -0800384
Simon Huntac4c6f72015-02-03 19:50:53 -0800385 function mkSvgClass(d) {
386 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
387 }
388
Simon Hunt5724fb42015-02-05 16:59:40 -0800389 function vis(b) {
390 return b ? 'visible' : 'hidden';
391 }
392
393 function toggleHosts() {
394 showHosts = !showHosts;
395 updateHostVisibility();
396 flash.flash('Hosts ' + vis(showHosts));
397 }
398
399 function toggleOffline() {
400 showOffline = !showOffline;
401 updateOfflineVisibility();
402 flash.flash('Offline devices ' + vis(showOffline));
403 }
404
405 function cycleDeviceLabels() {
Simon Hunta4242de2015-02-24 17:11:55 -0800406 td3.incDevLabIndex();
Simon Huntdc6adea2015-02-09 22:29:36 -0800407 tms.findDevices().forEach(function (d) {
Simon Hunta4242de2015-02-24 17:11:55 -0800408 td3.updateDeviceLabel(d);
Simon Hunt1c367112015-02-05 18:02:46 -0800409 });
Simon Hunt5724fb42015-02-05 16:59:40 -0800410 }
411
Simon Hunt445e8152015-02-06 13:00:12 -0800412 function unpin() {
Simon Hunt08f841d02015-02-10 14:39:20 -0800413 var hov = tss.hovered();
414 if (hov) {
415 sendUpdateMeta(hov, true);
416 hov.fixed = false;
417 hov.el.classed('fixed', false);
Simon Hunt445e8152015-02-06 13:00:12 -0800418 fResume();
419 }
420 }
421
Simon Hunta142dd22015-02-12 22:07:51 -0800422 function showMastership(masterId) {
423 if (!masterId) {
424 restoreLayerState();
425 } else {
426 showMastershipFor(masterId);
427 }
428 }
429
430 function restoreLayerState() {
431 // NOTE: this level of indirection required, for when we have
432 // the layer filter functionality re-implemented
433 suppressLayers(false);
434 }
435
436 function showMastershipFor(id) {
437 suppressLayers(true);
438 node.each(function (n) {
439 if (n.master === id) {
440 n.el.classed('suppressed', false);
441 }
442 });
443 }
444
445 function suppressLayers(b) {
446 node.classed('suppressed', b);
447 link.classed('suppressed', b);
448// d3.selectAll('svg .port').classed('inactive', b);
449// d3.selectAll('svg .portText').classed('inactive', b);
450 }
Simon Hunt445e8152015-02-06 13:00:12 -0800451
Simon Hunt5724fb42015-02-05 16:59:40 -0800452 // ==========================================
453
Simon Huntac4c6f72015-02-03 19:50:53 -0800454 function updateNodes() {
Simon Hunt1894d792015-02-04 17:09:20 -0800455 // select all the nodes in the layout:
Simon Huntac4c6f72015-02-03 19:50:53 -0800456 node = nodeG.selectAll('.node')
457 .data(network.nodes, function (d) { return d.id; });
458
Simon Hunt1894d792015-02-04 17:09:20 -0800459 // operate on existing nodes:
Simon Hunta4242de2015-02-24 17:11:55 -0800460 node.filter('.device').each(td3.deviceExisting);
461 node.filter('.host').each(td3.hostExisting);
Simon Huntac4c6f72015-02-03 19:50:53 -0800462
463 // operate on entering nodes:
464 var entering = node.enter()
465 .append('g')
466 .attr({
467 id: function (d) { return sus.safeId(d.id); },
468 class: mkSvgClass,
469 transform: function (d) { return sus.translate(d.x, d.y); },
470 opacity: 0
471 })
472 .call(drag)
Simon Hunt08f841d02015-02-10 14:39:20 -0800473 .on('mouseover', tss.nodeMouseOver)
474 .on('mouseout', tss.nodeMouseOut)
Simon Huntac4c6f72015-02-03 19:50:53 -0800475 .transition()
476 .attr('opacity', 1);
477
Simon Hunt1894d792015-02-04 17:09:20 -0800478 // augment entering nodes:
Simon Hunta4242de2015-02-24 17:11:55 -0800479 entering.filter('.device').each(td3.deviceEnter);
480 entering.filter('.host').each(td3.hostEnter);
Simon Huntac4c6f72015-02-03 19:50:53 -0800481
Simon Hunt51056592015-02-03 21:48:07 -0800482 // operate on both existing and new nodes:
Simon Hunta4242de2015-02-24 17:11:55 -0800483 td3.updateDeviceColors();
Simon Huntac4c6f72015-02-03 19:50:53 -0800484
485 // operate on exiting nodes:
486 // Note that the node is removed after 2 seconds.
487 // Sub element animations should be shorter than 2 seconds.
488 var exiting = node.exit()
489 .transition()
490 .duration(2000)
491 .style('opacity', 0)
492 .remove();
493
Simon Hunt1894d792015-02-04 17:09:20 -0800494 // exiting node specifics:
Simon Hunta4242de2015-02-24 17:11:55 -0800495 exiting.filter('.host').each(td3.hostExit);
496 exiting.filter('.device').each(td3.deviceExit);
Simon Huntac4c6f72015-02-03 19:50:53 -0800497
Simon Hunt51056592015-02-03 21:48:07 -0800498 // finally, resume the force layout
Simon Huntac4c6f72015-02-03 19:50:53 -0800499 fResume();
500 }
501
Simon Hunt51056592015-02-03 21:48:07 -0800502 // ==========================
Simon Hunt1894d792015-02-04 17:09:20 -0800503
504 function updateLinks() {
505 var th = ts.theme();
506
507 link = linkG.selectAll('.link')
508 .data(network.links, function (d) { return d.key; });
509
510 // operate on existing links:
Simon Hunta4242de2015-02-24 17:11:55 -0800511 link.each(td3.linkExisting);
Simon Hunt1894d792015-02-04 17:09:20 -0800512
513 // operate on entering links:
514 var entering = link.enter()
515 .append('line')
516 .attr({
517 x1: function (d) { return d.x1; },
518 y1: function (d) { return d.y1; },
519 x2: function (d) { return d.x2; },
520 y2: function (d) { return d.y2; },
521 stroke: linkConfig[th].inColor,
522 'stroke-width': linkConfig.inWidth
523 });
524
525 // augment links
Simon Hunta4242de2015-02-24 17:11:55 -0800526 entering.each(td3.linkEntering);
Simon Hunt1894d792015-02-04 17:09:20 -0800527
528 // operate on both existing and new links:
529 //link.each(...)
530
531 // apply or remove labels
Simon Hunta4242de2015-02-24 17:11:55 -0800532 td3.applyLinkLabels();
Simon Hunt1894d792015-02-04 17:09:20 -0800533
534 // operate on exiting links:
535 link.exit()
536 .attr('stroke-dasharray', '3 3')
Simon Hunt5724fb42015-02-05 16:59:40 -0800537 .attr('stroke', linkConfig[th].outColor)
Simon Hunt1894d792015-02-04 17:09:20 -0800538 .style('opacity', 0.5)
539 .transition()
540 .duration(1500)
541 .attr({
542 'stroke-dasharray': '3 12',
Simon Hunt1894d792015-02-04 17:09:20 -0800543 'stroke-width': linkConfig.outWidth
544 })
545 .style('opacity', 0.0)
546 .remove();
Simon Hunt1894d792015-02-04 17:09:20 -0800547 }
548
Simon Huntac4c6f72015-02-03 19:50:53 -0800549
550 // ==========================
Simon Hunt737c89f2015-01-28 12:23:19 -0800551 // force layout tick function
Simon Hunt737c89f2015-01-28 12:23:19 -0800552
Simon Hunt5724fb42015-02-05 16:59:40 -0800553 function fResume() {
Simon Huntc3c5b672015-02-20 11:32:13 -0800554 if (!tos.isOblique()) {
Simon Hunt5724fb42015-02-05 16:59:40 -0800555 force.resume();
556 }
557 }
558
559 function fStart() {
Simon Huntc3c5b672015-02-20 11:32:13 -0800560 if (!tos.isOblique()) {
Simon Hunt5724fb42015-02-05 16:59:40 -0800561 force.start();
562 }
563 }
564
565 var tickStuff = {
566 nodeAttr: {
567 transform: function (d) { return sus.translate(d.x, d.y); }
568 },
569 linkAttr: {
570 x1: function (d) { return d.source.x; },
571 y1: function (d) { return d.source.y; },
572 x2: function (d) { return d.target.x; },
573 y2: function (d) { return d.target.y; }
574 },
575 linkLabelAttr: {
576 transform: function (d) {
Simon Huntdc6adea2015-02-09 22:29:36 -0800577 var lnk = tms.findLinkById(d.key);
Simon Hunt5724fb42015-02-05 16:59:40 -0800578 if (lnk) {
Simon Hunta4242de2015-02-24 17:11:55 -0800579 return td3.transformLabel({
Simon Hunt5724fb42015-02-05 16:59:40 -0800580 x1: lnk.source.x,
581 y1: lnk.source.y,
582 x2: lnk.target.x,
583 y2: lnk.target.y
584 });
585 }
586 }
587 }
588 };
589
590 function tick() {
591 node.attr(tickStuff.nodeAttr);
592 link.attr(tickStuff.linkAttr);
593 linkLabel.attr(tickStuff.linkLabelAttr);
Simon Hunt737c89f2015-01-28 12:23:19 -0800594 }
595
596
Simon Huntac4c6f72015-02-03 19:50:53 -0800597 // ==========================
598 // === MOUSE GESTURE HANDLERS
599
Simon Hunt205099e2015-02-07 13:12:01 -0800600 function zoomingOrPanning(ev) {
601 return ev.metaKey || ev.altKey;
Simon Hunt445e8152015-02-06 13:00:12 -0800602 }
603
604 function atDragEnd(d) {
605 // once we've finished moving, pin the node in position
606 d.fixed = true;
607 d3.select(this).classed('fixed', true);
608 sendUpdateMeta(d);
609 }
610
611 // predicate that indicates when dragging is active
612 function dragEnabled() {
613 var ev = d3.event.sourceEvent;
614 // nodeLock means we aren't allowing nodes to be dragged...
Simon Hunt205099e2015-02-07 13:12:01 -0800615 return !nodeLock && !zoomingOrPanning(ev);
Simon Hunt445e8152015-02-06 13:00:12 -0800616 }
617
618 // predicate that indicates when clicking is active
619 function clickEnabled() {
620 return true;
621 }
Simon Hunt737c89f2015-01-28 12:23:19 -0800622
Simon Huntf542d842015-02-11 16:20:33 -0800623 // ==========================
624 // function entry points for traffic module
625
626 var allTrafficClasses = 'primary secondary animated optical';
627
628 function clearLinkTrafficStyle() {
629 link.style('stroke-width', null)
630 .classed(allTrafficClasses, false);
631 }
632
633 function removeLinkLabels() {
634 network.links.forEach(function (d) {
635 d.label = '';
636 });
637 }
Simon Hunt737c89f2015-01-28 12:23:19 -0800638
Simon Hunta4242de2015-02-24 17:11:55 -0800639 function updateLinkLabelModel() {
640 // create the backing data for showing labels..
641 var data = [];
642 link.each(function (d) {
643 if (d.label) {
644 data.push({
645 id: 'lab-' + d.key,
646 key: d.key,
647 label: d.label,
648 ldata: d
649 });
650 }
651 });
652
653 linkLabel = linkLabelG.selectAll('.linkLabel')
654 .data(data, function (d) { return d.id; });
655 }
656
Simon Hunt737c89f2015-01-28 12:23:19 -0800657 // ==========================
Simon Huntac4c6f72015-02-03 19:50:53 -0800658 // Module definition
Simon Hunt737c89f2015-01-28 12:23:19 -0800659
Simon Huntdc6adea2015-02-09 22:29:36 -0800660 function mkModelApi(uplink) {
661 return {
662 projection: uplink.projection,
663 network: network,
664 restyleLinkElement: restyleLinkElement,
665 removeLinkElement: removeLinkElement
666 };
667 }
668
Simon Hunta4242de2015-02-24 17:11:55 -0800669 function mkD3Api(uplink) {
670 return {
671 node: function () { return node; },
672 link: function () { return link; },
673 linkLabel: function () { return linkLabel; },
674 instVisible: function () { return tis.isVisible(); },
675 posNode: tms.positionNode,
676 showHosts: function () { return showHosts; },
677 restyleLinkElement: restyleLinkElement,
678 updateLinkLabelModel: updateLinkLabelModel
679 }
680 }
681
Simon Hunt08f841d02015-02-10 14:39:20 -0800682 function mkSelectApi(uplink) {
683 return {
684 node: function () { return node; },
685 zoomingOrPanning: zoomingOrPanning,
Simon Hunta4242de2015-02-24 17:11:55 -0800686 updateDeviceColors: td3.updateDeviceColors,
Simon Hunt08f841d02015-02-10 14:39:20 -0800687 sendEvent: uplink.sendEvent
688 };
689 }
690
Simon Huntf542d842015-02-11 16:20:33 -0800691 function mkTrafficApi(uplink) {
692 return {
693 clearLinkTrafficStyle: clearLinkTrafficStyle,
694 removeLinkLabels: removeLinkLabels,
695 updateLinks: updateLinks,
696 findLinkById: tms.findLinkById,
697 hovered: tss.hovered,
698 validateSelectionContext: tss.validateSelectionContext,
699 selectOrder: tss.selectOrder,
700 sendEvent: uplink.sendEvent
701 }
702 }
703
Simon Huntc3c5b672015-02-20 11:32:13 -0800704 function mkObliqueApi(uplink, fltr) {
Simon Hunt96f88c62015-02-19 17:57:25 -0800705 return {
Simon Huntc3c5b672015-02-20 11:32:13 -0800706 force: function() { return force; },
707 zoomLayer: uplink.zoomLayer,
708 nodeGBBox: function() { return nodeG.node().getBBox(); },
Simon Hunt96f88c62015-02-19 17:57:25 -0800709 node: function () { return node; },
Simon Huntc3c5b672015-02-20 11:32:13 -0800710 link: function () { return link; },
711 linkLabel: function () { return linkLabel; },
712 nodes: function () { return network.nodes; },
713 tickStuff: tickStuff,
714 nodeLock: function (b) {
715 var old = nodeLock;
716 nodeLock = b;
717 return old;
718 },
719 opacifyMap: uplink.opacifyMap,
720 inLayer: fltr.inLayer
Simon Hunt96f88c62015-02-19 17:57:25 -0800721 };
722 }
723
Simon Hunteb0fa052015-02-17 19:20:28 -0800724 function mkFilterApi(uplink) {
725 return {
726 node: function () { return node; },
727 link: function () { return link; }
728 };
729 }
730
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800731 function mkLinkApi(svg, forceG, uplink) {
732 return {
733 svg: svg,
734 forceG: forceG,
735 zoomer: uplink.zoomer(),
736 network: network,
737 showHosts: function () { return showHosts; }
738 };
739 }
740
Simon Hunt737c89f2015-01-28 12:23:19 -0800741 angular.module('ovTopo')
742 .factory('TopoForceService',
Simon Hunt1894d792015-02-04 17:09:20 -0800743 ['$log', 'FnService', 'SvgUtilService', 'IconService', 'ThemeService',
Simon Hunt3a6eec02015-02-09 21:16:43 -0800744 'FlashService', 'TopoInstService', 'TopoModelService',
Simon Hunta4242de2015-02-24 17:11:55 -0800745 'TopoD3Service', 'TopoSelectService', 'TopoTrafficService',
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800746 'TopoObliqueService', 'TopoFilterService', 'TopoLinkService',
Simon Hunt737c89f2015-01-28 12:23:19 -0800747
Simon Huntf542d842015-02-11 16:20:33 -0800748 function (_$log_, _fs_, _sus_, _is_, _ts_, _flash_,
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800749 _tis_, _tms_, _td3_, _tss_, _tts_, _tos_, _fltr_, _tls_) {
Simon Hunt737c89f2015-01-28 12:23:19 -0800750 $log = _$log_;
Simon Hunt1894d792015-02-04 17:09:20 -0800751 fs = _fs_;
Simon Hunt737c89f2015-01-28 12:23:19 -0800752 sus = _sus_;
Simon Huntac4c6f72015-02-03 19:50:53 -0800753 is = _is_;
754 ts = _ts_;
Simon Hunt5724fb42015-02-05 16:59:40 -0800755 flash = _flash_;
Simon Huntac4c6f72015-02-03 19:50:53 -0800756 tis = _tis_;
Simon Hunt3a6eec02015-02-09 21:16:43 -0800757 tms = _tms_;
Simon Hunta4242de2015-02-24 17:11:55 -0800758 td3 = _td3_;
Simon Hunt08f841d02015-02-10 14:39:20 -0800759 tss = _tss_;
Simon Huntf542d842015-02-11 16:20:33 -0800760 tts = _tts_;
Simon Hunt96f88c62015-02-19 17:57:25 -0800761 tos = _tos_;
Simon Hunteb0fa052015-02-17 19:20:28 -0800762 fltr = _fltr_;
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800763 tls = _tls_;
Simon Hunt737c89f2015-01-28 12:23:19 -0800764
Simon Hunt1894d792015-02-04 17:09:20 -0800765 icfg = is.iconConfig();
766
Simon Hunta142dd22015-02-12 22:07:51 -0800767 var themeListener = ts.addListener(function () {
768 updateLinks();
769 updateNodes();
770 });
771
Simon Hunt737c89f2015-01-28 12:23:19 -0800772 // forceG is the SVG group to display the force layout in
Simon Huntdc6adea2015-02-09 22:29:36 -0800773 // uplink is the api from the main topo source file
Simon Hunt3a6eec02015-02-09 21:16:43 -0800774 // dim is the initial dimensions of the SVG as [w,h]
Simon Hunt737c89f2015-01-28 12:23:19 -0800775 // opts are, well, optional :)
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800776 function initForce(svg, forceG, _uplink_, _dim_, opts) {
Simon Hunt1894d792015-02-04 17:09:20 -0800777 uplink = _uplink_;
Simon Hunt3a6eec02015-02-09 21:16:43 -0800778 dim = _dim_;
779
780 $log.debug('initForce().. dim = ' + dim);
781
Simon Huntdc6adea2015-02-09 22:29:36 -0800782 tms.initModel(mkModelApi(uplink), dim);
Simon Hunta4242de2015-02-24 17:11:55 -0800783 td3.initD3(mkD3Api(uplink));
Simon Hunt08f841d02015-02-10 14:39:20 -0800784 tss.initSelect(mkSelectApi(uplink));
Simon Huntf542d842015-02-11 16:20:33 -0800785 tts.initTraffic(mkTrafficApi(uplink));
Simon Huntc3c5b672015-02-20 11:32:13 -0800786 tos.initOblique(mkObliqueApi(uplink, fltr));
Simon Hunteb0fa052015-02-17 19:20:28 -0800787 fltr.initFilter(mkFilterApi(uplink), d3.select('#mast-right'));
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800788 tls.initLink(mkLinkApi(svg, forceG, uplink));
Simon Hunta11b4eb2015-01-28 16:20:50 -0800789
Simon Hunt737c89f2015-01-28 12:23:19 -0800790 settings = angular.extend({}, defaultSettings, opts);
791
792 linkG = forceG.append('g').attr('id', 'topo-links');
793 linkLabelG = forceG.append('g').attr('id', 'topo-linkLabels');
794 nodeG = forceG.append('g').attr('id', 'topo-nodes');
795
796 link = linkG.selectAll('.link');
797 linkLabel = linkLabelG.selectAll('.linkLabel');
798 node = nodeG.selectAll('.node');
799
800 force = d3.layout.force()
Simon Hunt3a6eec02015-02-09 21:16:43 -0800801 .size(dim)
Simon Hunt737c89f2015-01-28 12:23:19 -0800802 .nodes(network.nodes)
803 .links(network.links)
804 .gravity(settings.gravity)
805 .friction(settings.friction)
806 .charge(settings.charge._def_)
807 .linkDistance(settings.linkDistance._def_)
808 .linkStrength(settings.linkStrength._def_)
809 .on('tick', tick);
810
811 drag = sus.createDragBehavior(force,
Simon Hunt08f841d02015-02-10 14:39:20 -0800812 tss.selectObject, atDragEnd, dragEnabled, clickEnabled);
Simon Hunt737c89f2015-01-28 12:23:19 -0800813 }
814
Simon Hunt3a6eec02015-02-09 21:16:43 -0800815 function newDim(_dim_) {
816 dim = _dim_;
817 force.size(dim);
818 tms.newDim(dim);
Simon Hunt737c89f2015-01-28 12:23:19 -0800819 // Review -- do we need to nudge the layout ?
Simon Hunt737c89f2015-01-28 12:23:19 -0800820 }
821
Simon Hunt3a6eec02015-02-09 21:16:43 -0800822 function destroyForce() {
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800823 tls.destroyLink();
Simon Hunteb0fa052015-02-17 19:20:28 -0800824 fltr.destroyFilter();
Simon Hunt96f88c62015-02-19 17:57:25 -0800825 tos.destroyOblique();
Simon Huntf542d842015-02-11 16:20:33 -0800826 tts.destroyTraffic();
827 tss.destroySelect();
Simon Hunta4242de2015-02-24 17:11:55 -0800828 td3.destroyD3();
Simon Huntf542d842015-02-11 16:20:33 -0800829 tms.destroyModel();
Simon Hunta142dd22015-02-12 22:07:51 -0800830 ts.removeListener(themeListener);
831 themeListener = null;
Simon Hunt3a6eec02015-02-09 21:16:43 -0800832 }
833
Simon Hunt737c89f2015-01-28 12:23:19 -0800834 return {
835 initForce: initForce,
Simon Hunt3a6eec02015-02-09 21:16:43 -0800836 newDim: newDim,
837 destroyForce: destroyForce,
Simon Huntac4c6f72015-02-03 19:50:53 -0800838
Simon Hunta4242de2015-02-24 17:11:55 -0800839 updateDeviceColors: td3.updateDeviceColors,
Simon Hunt5724fb42015-02-05 16:59:40 -0800840 toggleHosts: toggleHosts,
841 toggleOffline: toggleOffline,
842 cycleDeviceLabels: cycleDeviceLabels,
Simon Hunt445e8152015-02-06 13:00:12 -0800843 unpin: unpin,
Simon Hunta142dd22015-02-12 22:07:51 -0800844 showMastership: showMastership,
Simon Huntac4c6f72015-02-03 19:50:53 -0800845
846 addDevice: addDevice,
Simon Hunt1894d792015-02-04 17:09:20 -0800847 updateDevice: updateDevice,
848 removeDevice: removeDevice,
849 addHost: addHost,
850 updateHost: updateHost,
851 removeHost: removeHost,
852 addLink: addLink,
853 updateLink: updateLink,
854 removeLink: removeLink
Simon Hunt737c89f2015-01-28 12:23:19 -0800855 };
856 }]);
857}());