blob: 1d2c5b10fe9551f5b18b7a56455839c856ee41ef [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
203
Simon Hunt5674db92015-10-22 16:12:48 -0700204 function updateDeviceRendering(d) {
Simon Hunta4242de2015-02-24 17:11:55 -0800205 var label = trimLabel(deviceLabel(d)),
206 noLabel = !label,
207 node = d.el,
208 dim = icfg.device.dim,
Simon Hunt5674db92015-10-22 16:12:48 -0700209 box, dx, dy, bsel,
210 bdg = d.badge,
Simon Hunt004fc2c2015-10-23 11:55:58 -0700211 bcr = badgeConfig.radius,
212 bcgd = badgeConfig.gdelta;
Simon Hunta4242de2015-02-24 17:11:55 -0800213
214 node.select('text')
215 .text(label)
216 .style('opacity', 0)
217 .transition()
218 .style('opacity', 1);
219
220 if (noLabel) {
221 box = emptyBox();
222 dx = -dim/2;
223 dy = -dim/2;
224 } else {
225 box = adjustRectToFitText(node);
226 dx = box.x + devCfg.xoff;
227 dy = box.y + devCfg.yoff;
228 }
229
230 node.select('rect')
231 .transition()
232 .attr(box);
233
234 node.select('g.deviceIcon')
235 .transition()
236 .attr('transform', sus.translate(dx, dy));
Simon Hunta4242de2015-02-24 17:11:55 -0800237
Simon Hunt5674db92015-10-22 16:12:48 -0700238 // handle badge, if defined
239 if (bdg) {
Simon Hunt004fc2c2015-10-23 11:55:58 -0700240 node.select('g.badge').remove();
241
Simon Hunte9343f32015-10-21 18:07:46 -0700242 bsel = node.append('g')
243 .classed('badge', true)
Simon Hunt5674db92015-10-22 16:12:48 -0700244 .classed(badgeStatus(bdg), true)
245 .attr('transform', sus.translate(dx + dim, dy));
Simon Hunte9343f32015-10-21 18:07:46 -0700246
247 bsel.append('circle')
Simon Hunt5674db92015-10-22 16:12:48 -0700248 .attr('r', bcr);
249
250 if (bdg.txt) {
251 bsel.append('text')
252 .attr('dy', badgeConfig.yoff)
253 .attr('text-anchor', 'middle')
254 .text(bdg.txt);
255 } else if (bdg.gid) {
256 bsel.append('use')
257 .attr({
Simon Hunt004fc2c2015-10-23 11:55:58 -0700258 width: bcgd * 2,
259 height: bcgd * 2,
260 transform: sus.translate(-bcgd, -bcgd),
Simon Hunt5674db92015-10-22 16:12:48 -0700261 'xlink:href': '#' + bdg.gid
262 });
263
264 }
Simon Hunte9343f32015-10-21 18:07:46 -0700265 }
266 }
267
Simon Hunta4242de2015-02-24 17:11:55 -0800268 function updateHostLabel(d) {
269 var label = trimLabel(hostLabel(d));
270 d.el.select('text').text(label);
271 }
272
273 function updateDeviceColors(d) {
274 if (d) {
275 setDeviceColor(d);
276 } else {
277 api.node().filter('.device').each(function (d) {
278 setDeviceColor(d);
279 });
280 }
281 }
282
283
284 // ==========================
285 // updateNodes - subfunctions
286
287 function deviceExisting(d) {
288 var node = d.el;
289 node.classed('online', d.online);
Simon Hunt5674db92015-10-22 16:12:48 -0700290 updateDeviceRendering(d);
Simon Hunta4242de2015-02-24 17:11:55 -0800291 api.posNode(d, true);
292 }
293
294 function hostExisting(d) {
295 updateHostLabel(d);
296 api.posNode(d, true);
297 }
298
299 function deviceEnter(d) {
300 var node = d3.select(this),
301 glyphId = d.type || 'unknown',
302 label = trimLabel(deviceLabel(d)),
303 //devCfg = deviceIconConfig,
304 noLabel = !label,
305 box, dx, dy, icon;
306
307 d.el = node;
308
309 node.append('rect').attr({ rx: 5, ry: 5 });
310 node.append('text').text(label).attr('dy', '1.1em');
311 box = adjustRectToFitText(node);
312 node.select('rect').attr(box);
313
314 icon = is.addDeviceIcon(node, glyphId);
315
316 if (noLabel) {
317 dx = -icon.dim/2;
318 dy = -icon.dim/2;
319 } else {
320 box = adjustRectToFitText(node);
321 dx = box.x + devCfg.xoff;
322 dy = box.y + devCfg.yoff;
323 }
324
325 icon.attr('transform', sus.translate(dx, dy));
326 }
327
328 function hostEnter(d) {
329 var node = d3.select(this),
330 gid = d.type || 'unknown',
331 rad = icfg.host.radius,
332 r = d.type ? rad.withGlyph : rad.noGlyph,
333 textDy = r + 10;
334
335 d.el = node;
336 sus.visible(node, api.showHosts());
337
338 is.addHostIcon(node, r, gid);
339
340 node.append('text')
341 .text(hostLabel)
342 .attr('dy', textDy)
343 .attr('text-anchor', 'middle');
344 }
345
346 function hostExit(d) {
347 var node = d.el;
348 node.select('use')
349 .style('opacity', 0.5)
350 .transition()
351 .duration(800)
352 .style('opacity', 0);
353
354 node.select('text')
355 .style('opacity', 0.5)
356 .transition()
357 .duration(800)
358 .style('opacity', 0);
359
360 node.select('circle')
361 .style('stroke-fill', '#555')
362 .style('fill', '#888')
363 .style('opacity', 0.5)
364 .transition()
365 .duration(1500)
366 .attr('r', 0);
367 }
368
369 function deviceExit(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.selectAll('rect')
378 .style('stroke-fill', '#555')
379 .style('fill', '#888')
380 .style('opacity', 0.5);
381 }
382
383
384 // ==========================
385 // updateLinks - subfunctions
386
Simon Hunta4242de2015-02-24 17:11:55 -0800387 function linkEntering(d) {
388 var link = d3.select(this);
389 d.el = link;
390 api.restyleLinkElement(d);
391 if (d.type() === 'hostLink') {
392 sus.visible(link, api.showHosts());
393 }
394 }
395
396 var linkLabelOffset = '0.3em';
397
398 function applyLinkLabels() {
399 var entering;
400
401 api.updateLinkLabelModel();
402
403 // for elements already existing, we need to update the text
404 // and adjust the rectangle size to fit
405 api.linkLabel().each(function (d) {
406 var el = d3.select(this),
407 rect = el.select('rect'),
408 text = el.select('text');
409 text.text(d.label);
410 rect.attr(rectAroundText(el));
411 });
412
413 entering = api.linkLabel().enter().append('g')
414 .classed('linkLabel', true)
415 .attr('id', function (d) { return d.id; });
416
417 entering.each(function (d) {
418 var el = d3.select(this),
419 rect,
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700420 text;
Simon Hunta4242de2015-02-24 17:11:55 -0800421
422 if (d.ldata.type() === 'hostLink') {
423 el.classed('hostLinkLabel', true);
424 sus.visible(el, api.showHosts());
425 }
426
427 d.el = el;
428 rect = el.append('rect');
429 text = el.append('text').text(d.label);
430 rect.attr(rectAroundText(el));
431 text.attr('dy', linkLabelOffset);
432
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700433 el.attr('transform', transformLabel(d.ldata.position));
Simon Hunta4242de2015-02-24 17:11:55 -0800434 });
435
436 // Remove any labels that are no longer required.
437 api.linkLabel().exit().remove();
438 }
439
440 function rectAroundText(el) {
441 var text = el.select('text'),
442 box = text.node().getBBox();
443
444 // translate the bbox so that it is centered on [x,y]
445 box.x = -box.width / 2;
446 box.y = -box.height / 2;
447
448 // add padding
449 box.x -= 1;
450 box.width += 2;
451 return box;
452 }
453
454 function transformLabel(p) {
455 var dx = p.x2 - p.x1,
456 dy = p.y2 - p.y1,
457 xMid = dx/2 + p.x1,
458 yMid = dy/2 + p.y1;
459 return sus.translate(xMid, yMid);
460 }
461
Simon Hunt1a5301e2015-02-25 15:31:25 -0800462 function applyPortLabels(data, portLabelG) {
463 var entering = portLabelG.selectAll('.portLabel')
464 .data(data).enter().append('g')
465 .classed('portLabel', true)
466 .attr('id', function (d) { return d.id; });
467
468 entering.each(function (d) {
469 var el = d3.select(this),
470 rect = el.append('rect'),
471 text = el.append('text').text(d.num);
472
473 rect.attr(rectAroundText(el));
474 text.attr('dy', linkLabelOffset);
Simon Hunt969b3c92015-02-25 18:11:31 -0800475 el.attr('transform', sus.translate(d.x, d.y));
Simon Hunt1a5301e2015-02-25 15:31:25 -0800476 });
477 }
478
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700479 function labelPoint(linkPos) {
480 var lengthUpLine = 1 / 3,
481 dx = linkPos.x2 - linkPos.x1,
482 dy = linkPos.y2 - linkPos.y1,
483 movedX = dx * lengthUpLine,
484 movedY = dy * lengthUpLine;
485
486 return {
487 x: movedX,
488 y: movedY
489 };
490 }
491
492 function calcGroupPos(linkPos) {
493 var moved = labelPoint(linkPos);
494 return sus.translate(linkPos.x1 + moved.x, linkPos.y1 + moved.y);
495 }
496
497 // calculates where on the link that the hash line for 5+ label appears
498 function hashAttrs(linkPos) {
499 var hashLength = 25,
500 halfLength = hashLength / 2,
501 dx = linkPos.x2 - linkPos.x1,
502 dy = linkPos.y2 - linkPos.y1,
503 length = Math.sqrt((dx * dx) + (dy * dy)),
504 moveAmtX = (dx / length) * halfLength,
505 moveAmtY = (dy / length) * halfLength,
506 mid = labelPoint(linkPos),
507 angle = Math.atan(dy / dx) + 45;
508
509 return {
510 x1: mid.x - moveAmtX,
511 y1: mid.y - moveAmtY,
512 x2: mid.x + moveAmtX,
513 y2: mid.y + moveAmtY,
514 stroke: api.linkConfig()[ts.theme()].baseColor,
515 transform: 'rotate(' + angle + ',' + mid.x + ',' + mid.y + ')'
516 };
517 }
518
519 function textLabelPos(linkPos) {
520 var point = labelPoint(linkPos),
521 dist = 20;
522 return {
523 x: point.x + dist,
524 y: point.y + dist
525 };
526 }
527
528 function applyNumLinkLabels(data, lblsG) {
529 var labels = lblsG.selectAll('g.numLinkLabel')
530 .data(data, function (d) { return 'pair-' + d.id; }),
531 entering;
532
533 // update existing labels
534 labels.each(function (d) {
535 var el = d3.select(this);
536
537 el.attr({
538 transform: function (d) { return calcGroupPos(d.linkCoords); }
539 });
540 el.select('line')
541 .attr(hashAttrs(d.linkCoords));
542 el.select('text')
543 .attr(textLabelPos(d.linkCoords))
544 .text(d.num);
545 });
546
547 // add new labels
548 entering = labels
549 .enter()
550 .append('g')
551 .attr({
552 transform: function (d) { return calcGroupPos(d.linkCoords); },
553 id: function (d) { return 'pair-' + d.id; }
554 })
555 .classed('numLinkLabel', true);
556
557 entering.each(function (d) {
558 var el = d3.select(this);
559
560 el.append('line')
561 .classed('numLinkHash', true)
562 .attr(hashAttrs(d.linkCoords));
563 el.append('text')
564 .classed('numLinkText', true)
565 .attr(textLabelPos(d.linkCoords))
566 .text(d.num);
567 });
568
569 // remove old labels
570 labels.exit().remove();
571 }
572
Simon Hunta4242de2015-02-24 17:11:55 -0800573 // ==========================
574 // Module definition
575
576 angular.module('ovTopo')
577 .factory('TopoD3Service',
578 ['$log', 'FnService', 'SvgUtilService', 'IconService', 'ThemeService',
579
580 function (_$log_, _fs_, _sus_, _is_, _ts_) {
581 $log = _$log_;
582 fs = _fs_;
583 sus = _sus_;
584 is = _is_;
585 ts = _ts_;
586
587 icfg = is.iconConfig();
588
589 function initD3(_api_) {
590 api = _api_;
591 }
592
593 function destroyD3() { }
594
595 return {
596 initD3: initD3,
597 destroyD3: destroyD3,
598
599 incDevLabIndex: incDevLabIndex,
600 adjustRectToFitText: adjustRectToFitText,
601 hostLabel: hostLabel,
602 deviceLabel: deviceLabel,
603 trimLabel: trimLabel,
604
Simon Hunt5674db92015-10-22 16:12:48 -0700605 updateDeviceLabel: updateDeviceRendering,
Simon Hunta4242de2015-02-24 17:11:55 -0800606 updateHostLabel: updateHostLabel,
607 updateDeviceColors: updateDeviceColors,
608
609 deviceExisting: deviceExisting,
610 hostExisting: hostExisting,
611 deviceEnter: deviceEnter,
612 hostEnter: hostEnter,
613 hostExit: hostExit,
614 deviceExit: deviceExit,
615
Simon Hunta4242de2015-02-24 17:11:55 -0800616 linkEntering: linkEntering,
617 applyLinkLabels: applyLinkLabels,
Simon Hunt1a5301e2015-02-25 15:31:25 -0800618 transformLabel: transformLabel,
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700619 applyPortLabels: applyPortLabels,
620 applyNumLinkLabels: applyNumLinkLabels
Simon Hunta4242de2015-02-24 17:11:55 -0800621 };
622 }]);
623}());