blob: c1315ff4b521373093d2d607decb43aa507002b7 [file] [log] [blame]
Simon Hunta4242de2015-02-24 17:11:55 -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/*
18 ONOS GUI -- Topology D3 Module.
19 Functions for manipulating the D3 visualizations of the Topology
20 */
21
22(function () {
23 'use strict';
24
25 // injected refs
26 var $log, fs, sus, is, ts;
27
28 // api to topoForce
29 var api;
30 /*
31 node() // get ref to D3 selection of nodes
32 link() // get ref to D3 selection of links
33 linkLabel() // get ref to D3 selection of link labels
34 instVisible() // true if instances panel is visible
35 posNode() // position node
36 showHosts() // true if hosts are to be shown
37 restyleLinkElement() // update link styles based on backing data
38 updateLinkLabelModel() // update backing data for link labels
39 */
40
41 // configuration
42 var devCfg = {
43 xoff: -20,
44 yoff: -18
45 },
46 labelConfig = {
47 imgPad: 16,
48 padLR: 4,
49 padTB: 3,
50 marginLR: 3,
51 marginTB: 2,
52 port: {
53 gap: 3,
54 width: 18,
55 height: 14
56 }
57 },
Simon Hunt5674db92015-10-22 16:12:48 -070058 badgeConfig = {
59 radius: 12,
Simon Hunt004fc2c2015-10-23 11:55:58 -070060 yoff: 5,
61 gdelta: 10
Simon Hunt5674db92015-10-22 16:12:48 -070062 },
Simon Hunta4242de2015-02-24 17:11:55 -080063 icfg;
64
Simon Hunt5674db92015-10-22 16:12:48 -070065 var status = {
66 i: 'badgeInfo',
67 w: 'badgeWarn',
68 e: 'badgeError'
69 };
70
71 function badgeStatus(badge) {
72 return status[badge.status] || status.i;
73 }
74
Simon Hunta4242de2015-02-24 17:11:55 -080075 // internal state
76 var deviceLabelIndex = 0,
77 hostLabelIndex = 0;
78
79
80 var dCol = {
81 black: '#000',
82 paleblue: '#acf',
83 offwhite: '#ddd',
84 darkgrey: '#444',
85 midgrey: '#888',
86 lightgrey: '#bbb',
87 orange: '#f90'
88 };
89
90 // note: these are the device icon colors without affinity
91 var dColTheme = {
92 light: {
93 rfill: dCol.offwhite,
94 online: {
95 glyph: dCol.darkgrey,
96 rect: dCol.paleblue
97 },
98 offline: {
99 glyph: dCol.midgrey,
100 rect: dCol.lightgrey
101 }
102 },
103 dark: {
104 rfill: dCol.midgrey,
105 online: {
106 glyph: dCol.darkgrey,
107 rect: dCol.paleblue
108 },
109 offline: {
110 glyph: dCol.midgrey,
111 rect: dCol.darkgrey
112 }
113 }
114 };
115
116 function devBaseColor(d) {
117 var o = d.online ? 'online' : 'offline';
118 return dColTheme[ts.theme()][o];
119 }
120
121 function setDeviceColor(d) {
122 var o = d.online,
123 s = d.el.classed('selected'),
124 c = devBaseColor(d),
125 a = instColor(d.master, o),
126 icon = d.el.select('g.deviceIcon'),
127 g, r;
128
129 if (s) {
130 g = c.glyph;
131 r = dCol.orange;
132 } else if (api.instVisible()) {
133 g = o ? a : c.glyph;
134 r = o ? c.rfill : a;
135 } else {
136 g = c.glyph;
137 r = c.rect;
138 }
139
140 icon.select('use').style('fill', g);
141 icon.select('rect').style('fill', r);
142 }
143
144 function instColor(id, online) {
145 return sus.cat7().getColor(id, !online, ts.theme());
146 }
147
148 // ====
149
150 function incDevLabIndex() {
151 deviceLabelIndex = (deviceLabelIndex+1) % 3;
Bri Prebilic Cole9cf1a8d2015-04-21 13:15:29 -0700152 switch(deviceLabelIndex) {
153 case 0: return 'Hide device labels';
154 case 1: return 'Show friendly device labels';
155 case 2: return 'Show device ID labels';
156 }
Simon Hunta4242de2015-02-24 17:11:55 -0800157 }
158
159 // Returns the newly computed bounding box of the rectangle
160 function adjustRectToFitText(n) {
161 var text = n.select('text'),
162 box = text.node().getBBox(),
163 lab = labelConfig;
164
165 text.attr('text-anchor', 'middle')
166 .attr('y', '-0.8em')
167 .attr('x', lab.imgPad/2);
168
169 // translate the bbox so that it is centered on [x,y]
170 box.x = -box.width / 2;
171 box.y = -box.height / 2;
172
173 // add padding
174 box.x -= (lab.padLR + lab.imgPad/2);
175 box.width += lab.padLR * 2 + lab.imgPad;
176 box.y -= lab.padTB;
177 box.height += lab.padTB * 2;
178
179 return box;
180 }
181
182 function hostLabel(d) {
183 var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0;
184 return d.labels[idx];
185 }
186 function deviceLabel(d) {
187 var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0;
188 return d.labels[idx];
189 }
190 function trimLabel(label) {
191 return (label && label.trim()) || '';
192 }
193
194 function emptyBox() {
195 return {
196 x: -2,
197 y: -2,
198 width: 4,
199 height: 4
200 };
201 }
202
Simon Hunt5674db92015-10-22 16:12:48 -0700203 function updateDeviceRendering(d) {
Simon Hunta4242de2015-02-24 17:11:55 -0800204 var label = trimLabel(deviceLabel(d)),
205 noLabel = !label,
206 node = d.el,
207 dim = icfg.device.dim,
Simon Huntd0f063a2016-01-14 17:29:25 -0800208 box, dx, dy,
209 bdg = d.badge;
Simon Hunta4242de2015-02-24 17:11:55 -0800210
211 node.select('text')
Simon Huntd0f063a2016-01-14 17:29:25 -0800212 .text(label);
Simon Hunta4242de2015-02-24 17:11:55 -0800213
214 if (noLabel) {
215 box = emptyBox();
216 dx = -dim/2;
217 dy = -dim/2;
218 } else {
219 box = adjustRectToFitText(node);
220 dx = box.x + devCfg.xoff;
221 dy = box.y + devCfg.yoff;
222 }
223
224 node.select('rect')
225 .transition()
226 .attr(box);
227
228 node.select('g.deviceIcon')
229 .transition()
230 .attr('transform', sus.translate(dx, dy));
Simon Hunta4242de2015-02-24 17:11:55 -0800231
Simon Hunt5674db92015-10-22 16:12:48 -0700232 // handle badge, if defined
233 if (bdg) {
Simon Huntc2bfe332015-12-04 11:01:24 -0800234 renderBadge(node, bdg, { dx: dx + dim, dy: dy });
Simon Hunte9343f32015-10-21 18:07:46 -0700235 }
236 }
237
Andrea Campanella52125412015-12-03 14:50:40 -0800238 function updateHostRendering(d) {
239 var node = d.el,
Simon Huntc2bfe332015-12-04 11:01:24 -0800240 bdg = d.badge;
Andrea Campanella52125412015-12-03 14:50:40 -0800241
242 updateHostLabel(d);
Andrea Campanella52125412015-12-03 14:50:40 -0800243
244 // handle badge, if defined
245 if (bdg) {
Simon Huntc2bfe332015-12-04 11:01:24 -0800246 renderBadge(node, bdg, icfg.host.badge);
247 }
248 }
Andrea Campanella52125412015-12-03 14:50:40 -0800249
Simon Huntc2bfe332015-12-04 11:01:24 -0800250 function renderBadge(node, bdg, boff) {
251 var bsel,
252 bcr = badgeConfig.radius,
253 bcgd = badgeConfig.gdelta;
Andrea Campanella52125412015-12-03 14:50:40 -0800254
Simon Huntc2bfe332015-12-04 11:01:24 -0800255 node.select('g.badge').remove();
Andrea Campanella52125412015-12-03 14:50:40 -0800256
Simon Huntc2bfe332015-12-04 11:01:24 -0800257 bsel = node.append('g')
258 .classed('badge', true)
259 .classed(badgeStatus(bdg), true)
260 .attr('transform', sus.translate(boff.dx, boff.dy));
Andrea Campanella52125412015-12-03 14:50:40 -0800261
Simon Huntc2bfe332015-12-04 11:01:24 -0800262 bsel.append('circle')
263 .attr('r', bcr);
264
265 if (bdg.txt) {
266 bsel.append('text')
267 .attr('dy', badgeConfig.yoff)
268 .attr('text-anchor', 'middle')
269 .text(bdg.txt);
270 } else if (bdg.gid) {
271 bsel.append('use')
272 .attr({
273 width: bcgd * 2,
274 height: bcgd * 2,
275 transform: sus.translate(-bcgd, -bcgd),
276 'xlink:href': '#' + bdg.gid
277 });
Andrea Campanella52125412015-12-03 14:50:40 -0800278 }
279 }
280
Simon Hunta4242de2015-02-24 17:11:55 -0800281 function updateHostLabel(d) {
282 var label = trimLabel(hostLabel(d));
283 d.el.select('text').text(label);
284 }
285
286 function updateDeviceColors(d) {
287 if (d) {
288 setDeviceColor(d);
289 } else {
290 api.node().filter('.device').each(function (d) {
291 setDeviceColor(d);
292 });
293 }
294 }
295
296
297 // ==========================
298 // updateNodes - subfunctions
299
300 function deviceExisting(d) {
301 var node = d.el;
302 node.classed('online', d.online);
Simon Hunt5674db92015-10-22 16:12:48 -0700303 updateDeviceRendering(d);
Simon Hunta4242de2015-02-24 17:11:55 -0800304 api.posNode(d, true);
305 }
306
307 function hostExisting(d) {
Andrea Campanella52125412015-12-03 14:50:40 -0800308 updateHostRendering(d);
Simon Hunta4242de2015-02-24 17:11:55 -0800309 api.posNode(d, true);
310 }
311
312 function deviceEnter(d) {
313 var node = d3.select(this),
314 glyphId = d.type || 'unknown',
315 label = trimLabel(deviceLabel(d)),
316 //devCfg = deviceIconConfig,
317 noLabel = !label,
318 box, dx, dy, icon;
319
320 d.el = node;
321
322 node.append('rect').attr({ rx: 5, ry: 5 });
323 node.append('text').text(label).attr('dy', '1.1em');
324 box = adjustRectToFitText(node);
325 node.select('rect').attr(box);
326
327 icon = is.addDeviceIcon(node, glyphId);
328
329 if (noLabel) {
330 dx = -icon.dim/2;
331 dy = -icon.dim/2;
332 } else {
333 box = adjustRectToFitText(node);
334 dx = box.x + devCfg.xoff;
335 dy = box.y + devCfg.yoff;
336 }
337
338 icon.attr('transform', sus.translate(dx, dy));
339 }
340
341 function hostEnter(d) {
342 var node = d3.select(this),
343 gid = d.type || 'unknown',
344 rad = icfg.host.radius,
345 r = d.type ? rad.withGlyph : rad.noGlyph,
346 textDy = r + 10;
347
348 d.el = node;
349 sus.visible(node, api.showHosts());
350
351 is.addHostIcon(node, r, gid);
352
353 node.append('text')
354 .text(hostLabel)
355 .attr('dy', textDy)
356 .attr('text-anchor', 'middle');
357 }
358
359 function hostExit(d) {
360 var node = d.el;
361 node.select('use')
362 .style('opacity', 0.5)
363 .transition()
364 .duration(800)
365 .style('opacity', 0);
366
367 node.select('text')
368 .style('opacity', 0.5)
369 .transition()
370 .duration(800)
371 .style('opacity', 0);
372
373 node.select('circle')
374 .style('stroke-fill', '#555')
375 .style('fill', '#888')
376 .style('opacity', 0.5)
377 .transition()
378 .duration(1500)
379 .attr('r', 0);
380 }
381
382 function deviceExit(d) {
383 var node = d.el;
384 node.select('use')
385 .style('opacity', 0.5)
386 .transition()
387 .duration(800)
388 .style('opacity', 0);
389
390 node.selectAll('rect')
391 .style('stroke-fill', '#555')
392 .style('fill', '#888')
393 .style('opacity', 0.5);
394 }
395
396
397 // ==========================
398 // updateLinks - subfunctions
399
Simon Hunta4242de2015-02-24 17:11:55 -0800400 function linkEntering(d) {
401 var link = d3.select(this);
402 d.el = link;
403 api.restyleLinkElement(d);
404 if (d.type() === 'hostLink') {
405 sus.visible(link, api.showHosts());
406 }
407 }
408
409 var linkLabelOffset = '0.3em';
410
411 function applyLinkLabels() {
412 var entering;
413
414 api.updateLinkLabelModel();
415
416 // for elements already existing, we need to update the text
417 // and adjust the rectangle size to fit
418 api.linkLabel().each(function (d) {
419 var el = d3.select(this),
420 rect = el.select('rect'),
421 text = el.select('text');
422 text.text(d.label);
423 rect.attr(rectAroundText(el));
424 });
425
426 entering = api.linkLabel().enter().append('g')
427 .classed('linkLabel', true)
428 .attr('id', function (d) { return d.id; });
429
430 entering.each(function (d) {
431 var el = d3.select(this),
432 rect,
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700433 text;
Simon Hunta4242de2015-02-24 17:11:55 -0800434
435 if (d.ldata.type() === 'hostLink') {
436 el.classed('hostLinkLabel', true);
437 sus.visible(el, api.showHosts());
438 }
439
440 d.el = el;
441 rect = el.append('rect');
442 text = el.append('text').text(d.label);
443 rect.attr(rectAroundText(el));
444 text.attr('dy', linkLabelOffset);
445
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700446 el.attr('transform', transformLabel(d.ldata.position));
Simon Hunta4242de2015-02-24 17:11:55 -0800447 });
448
449 // Remove any labels that are no longer required.
450 api.linkLabel().exit().remove();
451 }
452
453 function rectAroundText(el) {
454 var text = el.select('text'),
455 box = text.node().getBBox();
456
457 // translate the bbox so that it is centered on [x,y]
458 box.x = -box.width / 2;
459 box.y = -box.height / 2;
460
461 // add padding
462 box.x -= 1;
463 box.width += 2;
464 return box;
465 }
466
467 function transformLabel(p) {
468 var dx = p.x2 - p.x1,
469 dy = p.y2 - p.y1,
470 xMid = dx/2 + p.x1,
471 yMid = dy/2 + p.y1;
472 return sus.translate(xMid, yMid);
473 }
474
Simon Hunt1a5301e2015-02-25 15:31:25 -0800475 function applyPortLabels(data, portLabelG) {
476 var entering = portLabelG.selectAll('.portLabel')
477 .data(data).enter().append('g')
478 .classed('portLabel', true)
479 .attr('id', function (d) { return d.id; });
480
481 entering.each(function (d) {
482 var el = d3.select(this),
483 rect = el.append('rect'),
484 text = el.append('text').text(d.num);
485
486 rect.attr(rectAroundText(el));
487 text.attr('dy', linkLabelOffset);
Simon Hunt969b3c92015-02-25 18:11:31 -0800488 el.attr('transform', sus.translate(d.x, d.y));
Simon Hunt1a5301e2015-02-25 15:31:25 -0800489 });
490 }
491
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700492 function labelPoint(linkPos) {
493 var lengthUpLine = 1 / 3,
494 dx = linkPos.x2 - linkPos.x1,
495 dy = linkPos.y2 - linkPos.y1,
496 movedX = dx * lengthUpLine,
497 movedY = dy * lengthUpLine;
498
499 return {
500 x: movedX,
501 y: movedY
502 };
503 }
504
505 function calcGroupPos(linkPos) {
506 var moved = labelPoint(linkPos);
507 return sus.translate(linkPos.x1 + moved.x, linkPos.y1 + moved.y);
508 }
509
510 // calculates where on the link that the hash line for 5+ label appears
511 function hashAttrs(linkPos) {
512 var hashLength = 25,
513 halfLength = hashLength / 2,
514 dx = linkPos.x2 - linkPos.x1,
515 dy = linkPos.y2 - linkPos.y1,
516 length = Math.sqrt((dx * dx) + (dy * dy)),
517 moveAmtX = (dx / length) * halfLength,
518 moveAmtY = (dy / length) * halfLength,
519 mid = labelPoint(linkPos),
520 angle = Math.atan(dy / dx) + 45;
521
522 return {
523 x1: mid.x - moveAmtX,
524 y1: mid.y - moveAmtY,
525 x2: mid.x + moveAmtX,
526 y2: mid.y + moveAmtY,
527 stroke: api.linkConfig()[ts.theme()].baseColor,
528 transform: 'rotate(' + angle + ',' + mid.x + ',' + mid.y + ')'
529 };
530 }
531
532 function textLabelPos(linkPos) {
533 var point = labelPoint(linkPos),
534 dist = 20;
535 return {
536 x: point.x + dist,
537 y: point.y + dist
538 };
539 }
540
541 function applyNumLinkLabels(data, lblsG) {
542 var labels = lblsG.selectAll('g.numLinkLabel')
543 .data(data, function (d) { return 'pair-' + d.id; }),
544 entering;
545
546 // update existing labels
547 labels.each(function (d) {
548 var el = d3.select(this);
549
550 el.attr({
551 transform: function (d) { return calcGroupPos(d.linkCoords); }
552 });
553 el.select('line')
554 .attr(hashAttrs(d.linkCoords));
555 el.select('text')
556 .attr(textLabelPos(d.linkCoords))
557 .text(d.num);
558 });
559
560 // add new labels
561 entering = labels
562 .enter()
563 .append('g')
564 .attr({
565 transform: function (d) { return calcGroupPos(d.linkCoords); },
566 id: function (d) { return 'pair-' + d.id; }
567 })
568 .classed('numLinkLabel', true);
569
570 entering.each(function (d) {
571 var el = d3.select(this);
572
573 el.append('line')
574 .classed('numLinkHash', true)
575 .attr(hashAttrs(d.linkCoords));
576 el.append('text')
577 .classed('numLinkText', true)
578 .attr(textLabelPos(d.linkCoords))
579 .text(d.num);
580 });
581
582 // remove old labels
583 labels.exit().remove();
584 }
585
Simon Hunta4242de2015-02-24 17:11:55 -0800586 // ==========================
587 // Module definition
588
589 angular.module('ovTopo')
590 .factory('TopoD3Service',
591 ['$log', 'FnService', 'SvgUtilService', 'IconService', 'ThemeService',
592
593 function (_$log_, _fs_, _sus_, _is_, _ts_) {
594 $log = _$log_;
595 fs = _fs_;
596 sus = _sus_;
597 is = _is_;
598 ts = _ts_;
599
600 icfg = is.iconConfig();
601
602 function initD3(_api_) {
603 api = _api_;
604 }
605
606 function destroyD3() { }
607
608 return {
609 initD3: initD3,
610 destroyD3: destroyD3,
611
612 incDevLabIndex: incDevLabIndex,
613 adjustRectToFitText: adjustRectToFitText,
614 hostLabel: hostLabel,
615 deviceLabel: deviceLabel,
616 trimLabel: trimLabel,
617
Simon Hunt5674db92015-10-22 16:12:48 -0700618 updateDeviceLabel: updateDeviceRendering,
Simon Hunta4242de2015-02-24 17:11:55 -0800619 updateHostLabel: updateHostLabel,
620 updateDeviceColors: updateDeviceColors,
621
622 deviceExisting: deviceExisting,
623 hostExisting: hostExisting,
624 deviceEnter: deviceEnter,
625 hostEnter: hostEnter,
626 hostExit: hostExit,
627 deviceExit: deviceExit,
628
Simon Hunta4242de2015-02-24 17:11:55 -0800629 linkEntering: linkEntering,
630 applyLinkLabels: applyLinkLabels,
Simon Hunt1a5301e2015-02-25 15:31:25 -0800631 transformLabel: transformLabel,
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700632 applyPortLabels: applyPortLabels,
633 applyNumLinkLabels: applyNumLinkLabels
Simon Hunta4242de2015-02-24 17:11:55 -0800634 };
635 }]);
636}());