blob: a14ccbe1f0870f698a6209854233129515f16c2a [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
Simon Hunt1eee51d2016-02-26 19:12:13 -080071 // NOTE: this type of hack should go away once we have implemented
72 // the server-side UiModel code.
73 // {virtual -> cord} is for the E-CORD demo at ONS 2016
74 var remappedDeviceTypes = {
75 virtual: 'cord'
76 };
77
78 function mapDeviceTypeToGlyph(type) {
79 return remappedDeviceTypes[type] || type || 'unknown';
80 }
81
Simon Hunt5674db92015-10-22 16:12:48 -070082 function badgeStatus(badge) {
83 return status[badge.status] || status.i;
84 }
85
Simon Hunta4242de2015-02-24 17:11:55 -080086 // internal state
87 var deviceLabelIndex = 0,
88 hostLabelIndex = 0;
89
90
91 var dCol = {
92 black: '#000',
93 paleblue: '#acf',
94 offwhite: '#ddd',
95 darkgrey: '#444',
96 midgrey: '#888',
97 lightgrey: '#bbb',
98 orange: '#f90'
99 };
100
101 // note: these are the device icon colors without affinity
102 var dColTheme = {
103 light: {
104 rfill: dCol.offwhite,
105 online: {
106 glyph: dCol.darkgrey,
107 rect: dCol.paleblue
108 },
109 offline: {
110 glyph: dCol.midgrey,
111 rect: dCol.lightgrey
112 }
113 },
114 dark: {
115 rfill: dCol.midgrey,
116 online: {
117 glyph: dCol.darkgrey,
118 rect: dCol.paleblue
119 },
120 offline: {
121 glyph: dCol.midgrey,
122 rect: dCol.darkgrey
123 }
124 }
125 };
126
127 function devBaseColor(d) {
128 var o = d.online ? 'online' : 'offline';
129 return dColTheme[ts.theme()][o];
130 }
131
132 function setDeviceColor(d) {
133 var o = d.online,
134 s = d.el.classed('selected'),
135 c = devBaseColor(d),
136 a = instColor(d.master, o),
137 icon = d.el.select('g.deviceIcon'),
138 g, r;
139
140 if (s) {
141 g = c.glyph;
142 r = dCol.orange;
143 } else if (api.instVisible()) {
144 g = o ? a : c.glyph;
145 r = o ? c.rfill : a;
146 } else {
147 g = c.glyph;
148 r = c.rect;
149 }
150
151 icon.select('use').style('fill', g);
152 icon.select('rect').style('fill', r);
153 }
154
155 function instColor(id, online) {
156 return sus.cat7().getColor(id, !online, ts.theme());
157 }
158
159 // ====
160
161 function incDevLabIndex() {
162 deviceLabelIndex = (deviceLabelIndex+1) % 3;
Bri Prebilic Cole9cf1a8d2015-04-21 13:15:29 -0700163 switch(deviceLabelIndex) {
164 case 0: return 'Hide device labels';
165 case 1: return 'Show friendly device labels';
166 case 2: return 'Show device ID labels';
167 }
Simon Hunta4242de2015-02-24 17:11:55 -0800168 }
169
170 // Returns the newly computed bounding box of the rectangle
171 function adjustRectToFitText(n) {
172 var text = n.select('text'),
173 box = text.node().getBBox(),
174 lab = labelConfig;
175
176 text.attr('text-anchor', 'middle')
177 .attr('y', '-0.8em')
178 .attr('x', lab.imgPad/2);
179
180 // translate the bbox so that it is centered on [x,y]
181 box.x = -box.width / 2;
182 box.y = -box.height / 2;
183
184 // add padding
185 box.x -= (lab.padLR + lab.imgPad/2);
186 box.width += lab.padLR * 2 + lab.imgPad;
187 box.y -= lab.padTB;
188 box.height += lab.padTB * 2;
189
190 return box;
191 }
192
193 function hostLabel(d) {
194 var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0;
195 return d.labels[idx];
196 }
197 function deviceLabel(d) {
198 var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0;
199 return d.labels[idx];
200 }
201 function trimLabel(label) {
202 return (label && label.trim()) || '';
203 }
204
205 function emptyBox() {
206 return {
207 x: -2,
208 y: -2,
209 width: 4,
210 height: 4
211 };
212 }
213
Simon Hunt5674db92015-10-22 16:12:48 -0700214 function updateDeviceRendering(d) {
Simon Hunta4242de2015-02-24 17:11:55 -0800215 var label = trimLabel(deviceLabel(d)),
216 noLabel = !label,
217 node = d.el,
218 dim = icfg.device.dim,
Simon Huntd0f063a2016-01-14 17:29:25 -0800219 box, dx, dy,
220 bdg = d.badge;
Simon Hunta4242de2015-02-24 17:11:55 -0800221
222 node.select('text')
Simon Huntd0f063a2016-01-14 17:29:25 -0800223 .text(label);
Simon Hunta4242de2015-02-24 17:11:55 -0800224
225 if (noLabel) {
226 box = emptyBox();
227 dx = -dim/2;
228 dy = -dim/2;
229 } else {
230 box = adjustRectToFitText(node);
231 dx = box.x + devCfg.xoff;
232 dy = box.y + devCfg.yoff;
233 }
234
235 node.select('rect')
236 .transition()
237 .attr(box);
238
239 node.select('g.deviceIcon')
240 .transition()
241 .attr('transform', sus.translate(dx, dy));
Simon Hunta4242de2015-02-24 17:11:55 -0800242
Simon Hunt5674db92015-10-22 16:12:48 -0700243 // handle badge, if defined
244 if (bdg) {
Simon Huntc2bfe332015-12-04 11:01:24 -0800245 renderBadge(node, bdg, { dx: dx + dim, dy: dy });
Simon Hunte9343f32015-10-21 18:07:46 -0700246 }
247 }
248
Andrea Campanella52125412015-12-03 14:50:40 -0800249 function updateHostRendering(d) {
250 var node = d.el,
Simon Huntc2bfe332015-12-04 11:01:24 -0800251 bdg = d.badge;
Andrea Campanella52125412015-12-03 14:50:40 -0800252
253 updateHostLabel(d);
Andrea Campanella52125412015-12-03 14:50:40 -0800254
255 // handle badge, if defined
256 if (bdg) {
Simon Huntc2bfe332015-12-04 11:01:24 -0800257 renderBadge(node, bdg, icfg.host.badge);
258 }
259 }
Andrea Campanella52125412015-12-03 14:50:40 -0800260
Simon Huntc2bfe332015-12-04 11:01:24 -0800261 function renderBadge(node, bdg, boff) {
262 var bsel,
263 bcr = badgeConfig.radius,
264 bcgd = badgeConfig.gdelta;
Andrea Campanella52125412015-12-03 14:50:40 -0800265
Simon Huntc2bfe332015-12-04 11:01:24 -0800266 node.select('g.badge').remove();
Andrea Campanella52125412015-12-03 14:50:40 -0800267
Simon Huntc2bfe332015-12-04 11:01:24 -0800268 bsel = node.append('g')
269 .classed('badge', true)
270 .classed(badgeStatus(bdg), true)
271 .attr('transform', sus.translate(boff.dx, boff.dy));
Andrea Campanella52125412015-12-03 14:50:40 -0800272
Simon Huntc2bfe332015-12-04 11:01:24 -0800273 bsel.append('circle')
274 .attr('r', bcr);
275
276 if (bdg.txt) {
277 bsel.append('text')
278 .attr('dy', badgeConfig.yoff)
279 .attr('text-anchor', 'middle')
280 .text(bdg.txt);
281 } else if (bdg.gid) {
282 bsel.append('use')
283 .attr({
284 width: bcgd * 2,
285 height: bcgd * 2,
286 transform: sus.translate(-bcgd, -bcgd),
287 'xlink:href': '#' + bdg.gid
288 });
Andrea Campanella52125412015-12-03 14:50:40 -0800289 }
290 }
291
Simon Hunta4242de2015-02-24 17:11:55 -0800292 function updateHostLabel(d) {
293 var label = trimLabel(hostLabel(d));
294 d.el.select('text').text(label);
295 }
296
297 function updateDeviceColors(d) {
298 if (d) {
299 setDeviceColor(d);
300 } else {
301 api.node().filter('.device').each(function (d) {
302 setDeviceColor(d);
303 });
304 }
305 }
306
307
308 // ==========================
309 // updateNodes - subfunctions
310
311 function deviceExisting(d) {
312 var node = d.el;
313 node.classed('online', d.online);
Simon Hunt5674db92015-10-22 16:12:48 -0700314 updateDeviceRendering(d);
Simon Hunta4242de2015-02-24 17:11:55 -0800315 api.posNode(d, true);
316 }
317
318 function hostExisting(d) {
Andrea Campanella52125412015-12-03 14:50:40 -0800319 updateHostRendering(d);
Simon Hunta4242de2015-02-24 17:11:55 -0800320 api.posNode(d, true);
321 }
322
323 function deviceEnter(d) {
324 var node = d3.select(this),
Simon Hunt1eee51d2016-02-26 19:12:13 -0800325 glyphId = mapDeviceTypeToGlyph(d.type),
Simon Hunta4242de2015-02-24 17:11:55 -0800326 label = trimLabel(deviceLabel(d)),
Simon Hunta4242de2015-02-24 17:11:55 -0800327 noLabel = !label,
328 box, dx, dy, icon;
329
330 d.el = node;
331
332 node.append('rect').attr({ rx: 5, ry: 5 });
333 node.append('text').text(label).attr('dy', '1.1em');
334 box = adjustRectToFitText(node);
335 node.select('rect').attr(box);
336
337 icon = is.addDeviceIcon(node, glyphId);
338
339 if (noLabel) {
340 dx = -icon.dim/2;
341 dy = -icon.dim/2;
342 } else {
343 box = adjustRectToFitText(node);
344 dx = box.x + devCfg.xoff;
345 dy = box.y + devCfg.yoff;
346 }
347
348 icon.attr('transform', sus.translate(dx, dy));
349 }
350
351 function hostEnter(d) {
352 var node = d3.select(this),
353 gid = d.type || 'unknown',
354 rad = icfg.host.radius,
355 r = d.type ? rad.withGlyph : rad.noGlyph,
356 textDy = r + 10;
357
358 d.el = node;
359 sus.visible(node, api.showHosts());
360
361 is.addHostIcon(node, r, gid);
362
363 node.append('text')
364 .text(hostLabel)
365 .attr('dy', textDy)
366 .attr('text-anchor', 'middle');
367 }
368
369 function hostExit(d) {
370 var node = d.el;
371 node.select('use')
372 .style('opacity', 0.5)
373 .transition()
374 .duration(800)
375 .style('opacity', 0);
376
377 node.select('text')
378 .style('opacity', 0.5)
379 .transition()
380 .duration(800)
381 .style('opacity', 0);
382
383 node.select('circle')
384 .style('stroke-fill', '#555')
385 .style('fill', '#888')
386 .style('opacity', 0.5)
387 .transition()
388 .duration(1500)
389 .attr('r', 0);
390 }
391
392 function deviceExit(d) {
393 var node = d.el;
394 node.select('use')
395 .style('opacity', 0.5)
396 .transition()
397 .duration(800)
398 .style('opacity', 0);
399
400 node.selectAll('rect')
401 .style('stroke-fill', '#555')
402 .style('fill', '#888')
403 .style('opacity', 0.5);
404 }
405
406
407 // ==========================
408 // updateLinks - subfunctions
409
Simon Hunta4242de2015-02-24 17:11:55 -0800410 function linkEntering(d) {
411 var link = d3.select(this);
412 d.el = link;
413 api.restyleLinkElement(d);
414 if (d.type() === 'hostLink') {
415 sus.visible(link, api.showHosts());
416 }
417 }
418
419 var linkLabelOffset = '0.3em';
420
421 function applyLinkLabels() {
422 var entering;
423
424 api.updateLinkLabelModel();
425
426 // for elements already existing, we need to update the text
427 // and adjust the rectangle size to fit
428 api.linkLabel().each(function (d) {
429 var el = d3.select(this),
430 rect = el.select('rect'),
431 text = el.select('text');
432 text.text(d.label);
433 rect.attr(rectAroundText(el));
434 });
435
436 entering = api.linkLabel().enter().append('g')
437 .classed('linkLabel', true)
438 .attr('id', function (d) { return d.id; });
439
440 entering.each(function (d) {
441 var el = d3.select(this),
442 rect,
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700443 text;
Simon Hunta4242de2015-02-24 17:11:55 -0800444
445 if (d.ldata.type() === 'hostLink') {
446 el.classed('hostLinkLabel', true);
447 sus.visible(el, api.showHosts());
448 }
449
450 d.el = el;
451 rect = el.append('rect');
452 text = el.append('text').text(d.label);
453 rect.attr(rectAroundText(el));
454 text.attr('dy', linkLabelOffset);
455
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700456 el.attr('transform', transformLabel(d.ldata.position));
Simon Hunta4242de2015-02-24 17:11:55 -0800457 });
458
459 // Remove any labels that are no longer required.
460 api.linkLabel().exit().remove();
461 }
462
463 function rectAroundText(el) {
464 var text = el.select('text'),
465 box = text.node().getBBox();
466
467 // translate the bbox so that it is centered on [x,y]
468 box.x = -box.width / 2;
469 box.y = -box.height / 2;
470
471 // add padding
472 box.x -= 1;
473 box.width += 2;
474 return box;
475 }
476
477 function transformLabel(p) {
478 var dx = p.x2 - p.x1,
479 dy = p.y2 - p.y1,
480 xMid = dx/2 + p.x1,
481 yMid = dy/2 + p.y1;
482 return sus.translate(xMid, yMid);
483 }
484
Simon Hunt1a5301e2015-02-25 15:31:25 -0800485 function applyPortLabels(data, portLabelG) {
486 var entering = portLabelG.selectAll('.portLabel')
487 .data(data).enter().append('g')
488 .classed('portLabel', true)
489 .attr('id', function (d) { return d.id; });
490
491 entering.each(function (d) {
492 var el = d3.select(this),
493 rect = el.append('rect'),
494 text = el.append('text').text(d.num);
495
496 rect.attr(rectAroundText(el));
497 text.attr('dy', linkLabelOffset);
Simon Hunt969b3c92015-02-25 18:11:31 -0800498 el.attr('transform', sus.translate(d.x, d.y));
Simon Hunt1a5301e2015-02-25 15:31:25 -0800499 });
500 }
501
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700502 function labelPoint(linkPos) {
503 var lengthUpLine = 1 / 3,
504 dx = linkPos.x2 - linkPos.x1,
505 dy = linkPos.y2 - linkPos.y1,
506 movedX = dx * lengthUpLine,
507 movedY = dy * lengthUpLine;
508
509 return {
510 x: movedX,
511 y: movedY
512 };
513 }
514
515 function calcGroupPos(linkPos) {
516 var moved = labelPoint(linkPos);
517 return sus.translate(linkPos.x1 + moved.x, linkPos.y1 + moved.y);
518 }
519
520 // calculates where on the link that the hash line for 5+ label appears
521 function hashAttrs(linkPos) {
522 var hashLength = 25,
523 halfLength = hashLength / 2,
524 dx = linkPos.x2 - linkPos.x1,
525 dy = linkPos.y2 - linkPos.y1,
526 length = Math.sqrt((dx * dx) + (dy * dy)),
527 moveAmtX = (dx / length) * halfLength,
528 moveAmtY = (dy / length) * halfLength,
529 mid = labelPoint(linkPos),
530 angle = Math.atan(dy / dx) + 45;
531
532 return {
533 x1: mid.x - moveAmtX,
534 y1: mid.y - moveAmtY,
535 x2: mid.x + moveAmtX,
536 y2: mid.y + moveAmtY,
537 stroke: api.linkConfig()[ts.theme()].baseColor,
538 transform: 'rotate(' + angle + ',' + mid.x + ',' + mid.y + ')'
539 };
540 }
541
542 function textLabelPos(linkPos) {
543 var point = labelPoint(linkPos),
544 dist = 20;
545 return {
546 x: point.x + dist,
547 y: point.y + dist
548 };
549 }
550
551 function applyNumLinkLabels(data, lblsG) {
552 var labels = lblsG.selectAll('g.numLinkLabel')
553 .data(data, function (d) { return 'pair-' + d.id; }),
554 entering;
555
556 // update existing labels
557 labels.each(function (d) {
558 var el = d3.select(this);
559
560 el.attr({
561 transform: function (d) { return calcGroupPos(d.linkCoords); }
562 });
563 el.select('line')
564 .attr(hashAttrs(d.linkCoords));
565 el.select('text')
566 .attr(textLabelPos(d.linkCoords))
567 .text(d.num);
568 });
569
570 // add new labels
571 entering = labels
572 .enter()
573 .append('g')
574 .attr({
575 transform: function (d) { return calcGroupPos(d.linkCoords); },
576 id: function (d) { return 'pair-' + d.id; }
577 })
578 .classed('numLinkLabel', true);
579
580 entering.each(function (d) {
581 var el = d3.select(this);
582
583 el.append('line')
584 .classed('numLinkHash', true)
585 .attr(hashAttrs(d.linkCoords));
586 el.append('text')
587 .classed('numLinkText', true)
588 .attr(textLabelPos(d.linkCoords))
589 .text(d.num);
590 });
591
592 // remove old labels
593 labels.exit().remove();
594 }
595
Simon Hunta4242de2015-02-24 17:11:55 -0800596 // ==========================
597 // Module definition
598
599 angular.module('ovTopo')
600 .factory('TopoD3Service',
601 ['$log', 'FnService', 'SvgUtilService', 'IconService', 'ThemeService',
602
603 function (_$log_, _fs_, _sus_, _is_, _ts_) {
604 $log = _$log_;
605 fs = _fs_;
606 sus = _sus_;
607 is = _is_;
608 ts = _ts_;
609
610 icfg = is.iconConfig();
611
612 function initD3(_api_) {
613 api = _api_;
614 }
615
616 function destroyD3() { }
617
618 return {
619 initD3: initD3,
620 destroyD3: destroyD3,
621
622 incDevLabIndex: incDevLabIndex,
623 adjustRectToFitText: adjustRectToFitText,
624 hostLabel: hostLabel,
625 deviceLabel: deviceLabel,
626 trimLabel: trimLabel,
627
Simon Hunt5674db92015-10-22 16:12:48 -0700628 updateDeviceLabel: updateDeviceRendering,
Simon Hunta4242de2015-02-24 17:11:55 -0800629 updateHostLabel: updateHostLabel,
630 updateDeviceColors: updateDeviceColors,
631
632 deviceExisting: deviceExisting,
633 hostExisting: hostExisting,
634 deviceEnter: deviceEnter,
635 hostEnter: hostEnter,
636 hostExit: hostExit,
637 deviceExit: deviceExit,
638
Simon Hunta4242de2015-02-24 17:11:55 -0800639 linkEntering: linkEntering,
640 applyLinkLabels: applyLinkLabels,
Simon Hunt1a5301e2015-02-25 15:31:25 -0800641 transformLabel: transformLabel,
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700642 applyPortLabels: applyPortLabels,
643 applyNumLinkLabels: applyNumLinkLabels
Simon Hunta4242de2015-02-24 17:11:55 -0800644 };
645 }]);
646}());