blob: 1b0a12878268e613dd87fda3460ef97cc35091f0 [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 },
58 icfg;
59
60 // internal state
61 var deviceLabelIndex = 0,
62 hostLabelIndex = 0;
63
64
65 var dCol = {
66 black: '#000',
67 paleblue: '#acf',
68 offwhite: '#ddd',
69 darkgrey: '#444',
70 midgrey: '#888',
71 lightgrey: '#bbb',
72 orange: '#f90'
73 };
74
75 // note: these are the device icon colors without affinity
76 var dColTheme = {
77 light: {
78 rfill: dCol.offwhite,
79 online: {
80 glyph: dCol.darkgrey,
81 rect: dCol.paleblue
82 },
83 offline: {
84 glyph: dCol.midgrey,
85 rect: dCol.lightgrey
86 }
87 },
88 dark: {
89 rfill: dCol.midgrey,
90 online: {
91 glyph: dCol.darkgrey,
92 rect: dCol.paleblue
93 },
94 offline: {
95 glyph: dCol.midgrey,
96 rect: dCol.darkgrey
97 }
98 }
99 };
100
101 function devBaseColor(d) {
102 var o = d.online ? 'online' : 'offline';
103 return dColTheme[ts.theme()][o];
104 }
105
106 function setDeviceColor(d) {
107 var o = d.online,
108 s = d.el.classed('selected'),
109 c = devBaseColor(d),
110 a = instColor(d.master, o),
111 icon = d.el.select('g.deviceIcon'),
112 g, r;
113
114 if (s) {
115 g = c.glyph;
116 r = dCol.orange;
117 } else if (api.instVisible()) {
118 g = o ? a : c.glyph;
119 r = o ? c.rfill : a;
120 } else {
121 g = c.glyph;
122 r = c.rect;
123 }
124
125 icon.select('use').style('fill', g);
126 icon.select('rect').style('fill', r);
127 }
128
129 function instColor(id, online) {
130 return sus.cat7().getColor(id, !online, ts.theme());
131 }
132
133 // ====
134
135 function incDevLabIndex() {
136 deviceLabelIndex = (deviceLabelIndex+1) % 3;
Bri Prebilic Cole9cf1a8d2015-04-21 13:15:29 -0700137 switch(deviceLabelIndex) {
138 case 0: return 'Hide device labels';
139 case 1: return 'Show friendly device labels';
140 case 2: return 'Show device ID labels';
141 }
Simon Hunta4242de2015-02-24 17:11:55 -0800142 }
143
144 // Returns the newly computed bounding box of the rectangle
145 function adjustRectToFitText(n) {
146 var text = n.select('text'),
147 box = text.node().getBBox(),
148 lab = labelConfig;
149
150 text.attr('text-anchor', 'middle')
151 .attr('y', '-0.8em')
152 .attr('x', lab.imgPad/2);
153
154 // translate the bbox so that it is centered on [x,y]
155 box.x = -box.width / 2;
156 box.y = -box.height / 2;
157
158 // add padding
159 box.x -= (lab.padLR + lab.imgPad/2);
160 box.width += lab.padLR * 2 + lab.imgPad;
161 box.y -= lab.padTB;
162 box.height += lab.padTB * 2;
163
164 return box;
165 }
166
167 function hostLabel(d) {
168 var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0;
169 return d.labels[idx];
170 }
171 function deviceLabel(d) {
172 var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0;
173 return d.labels[idx];
174 }
175 function trimLabel(label) {
176 return (label && label.trim()) || '';
177 }
178
179 function emptyBox() {
180 return {
181 x: -2,
182 y: -2,
183 width: 4,
184 height: 4
185 };
186 }
187
188
189 function updateDeviceLabel(d) {
190 var label = trimLabel(deviceLabel(d)),
191 noLabel = !label,
192 node = d.el,
193 dim = icfg.device.dim,
194 box, dx, dy;
195
196 node.select('text')
197 .text(label)
198 .style('opacity', 0)
199 .transition()
200 .style('opacity', 1);
201
202 if (noLabel) {
203 box = emptyBox();
204 dx = -dim/2;
205 dy = -dim/2;
206 } else {
207 box = adjustRectToFitText(node);
208 dx = box.x + devCfg.xoff;
209 dy = box.y + devCfg.yoff;
210 }
211
212 node.select('rect')
213 .transition()
214 .attr(box);
215
216 node.select('g.deviceIcon')
217 .transition()
218 .attr('transform', sus.translate(dx, dy));
219 }
220
221 function updateHostLabel(d) {
222 var label = trimLabel(hostLabel(d));
223 d.el.select('text').text(label);
224 }
225
226 function updateDeviceColors(d) {
227 if (d) {
228 setDeviceColor(d);
229 } else {
230 api.node().filter('.device').each(function (d) {
231 setDeviceColor(d);
232 });
233 }
234 }
235
236
237 // ==========================
238 // updateNodes - subfunctions
239
240 function deviceExisting(d) {
241 var node = d.el;
242 node.classed('online', d.online);
243 updateDeviceLabel(d);
244 api.posNode(d, true);
245 }
246
247 function hostExisting(d) {
248 updateHostLabel(d);
249 api.posNode(d, true);
250 }
251
252 function deviceEnter(d) {
253 var node = d3.select(this),
254 glyphId = d.type || 'unknown',
255 label = trimLabel(deviceLabel(d)),
256 //devCfg = deviceIconConfig,
257 noLabel = !label,
258 box, dx, dy, icon;
259
260 d.el = node;
261
262 node.append('rect').attr({ rx: 5, ry: 5 });
263 node.append('text').text(label).attr('dy', '1.1em');
264 box = adjustRectToFitText(node);
265 node.select('rect').attr(box);
266
267 icon = is.addDeviceIcon(node, glyphId);
268
269 if (noLabel) {
270 dx = -icon.dim/2;
271 dy = -icon.dim/2;
272 } else {
273 box = adjustRectToFitText(node);
274 dx = box.x + devCfg.xoff;
275 dy = box.y + devCfg.yoff;
276 }
277
278 icon.attr('transform', sus.translate(dx, dy));
279 }
280
281 function hostEnter(d) {
282 var node = d3.select(this),
283 gid = d.type || 'unknown',
284 rad = icfg.host.radius,
285 r = d.type ? rad.withGlyph : rad.noGlyph,
286 textDy = r + 10;
287
288 d.el = node;
289 sus.visible(node, api.showHosts());
290
291 is.addHostIcon(node, r, gid);
292
293 node.append('text')
294 .text(hostLabel)
295 .attr('dy', textDy)
296 .attr('text-anchor', 'middle');
297 }
298
299 function hostExit(d) {
300 var node = d.el;
301 node.select('use')
302 .style('opacity', 0.5)
303 .transition()
304 .duration(800)
305 .style('opacity', 0);
306
307 node.select('text')
308 .style('opacity', 0.5)
309 .transition()
310 .duration(800)
311 .style('opacity', 0);
312
313 node.select('circle')
314 .style('stroke-fill', '#555')
315 .style('fill', '#888')
316 .style('opacity', 0.5)
317 .transition()
318 .duration(1500)
319 .attr('r', 0);
320 }
321
322 function deviceExit(d) {
323 var node = d.el;
324 node.select('use')
325 .style('opacity', 0.5)
326 .transition()
327 .duration(800)
328 .style('opacity', 0);
329
330 node.selectAll('rect')
331 .style('stroke-fill', '#555')
332 .style('fill', '#888')
333 .style('opacity', 0.5);
334 }
335
336
337 // ==========================
338 // updateLinks - subfunctions
339
Simon Hunta4242de2015-02-24 17:11:55 -0800340 function linkEntering(d) {
341 var link = d3.select(this);
342 d.el = link;
343 api.restyleLinkElement(d);
344 if (d.type() === 'hostLink') {
345 sus.visible(link, api.showHosts());
346 }
347 }
348
349 var linkLabelOffset = '0.3em';
350
351 function applyLinkLabels() {
352 var entering;
353
354 api.updateLinkLabelModel();
355
356 // for elements already existing, we need to update the text
357 // and adjust the rectangle size to fit
358 api.linkLabel().each(function (d) {
359 var el = d3.select(this),
360 rect = el.select('rect'),
361 text = el.select('text');
362 text.text(d.label);
363 rect.attr(rectAroundText(el));
364 });
365
366 entering = api.linkLabel().enter().append('g')
367 .classed('linkLabel', true)
368 .attr('id', function (d) { return d.id; });
369
370 entering.each(function (d) {
371 var el = d3.select(this),
372 rect,
373 text,
374 parms = {
375 x1: d.ldata.source.x,
376 y1: d.ldata.source.y,
377 x2: d.ldata.target.x,
378 y2: d.ldata.target.y
379 };
380
381 if (d.ldata.type() === 'hostLink') {
382 el.classed('hostLinkLabel', true);
383 sus.visible(el, api.showHosts());
384 }
385
386 d.el = el;
387 rect = el.append('rect');
388 text = el.append('text').text(d.label);
389 rect.attr(rectAroundText(el));
390 text.attr('dy', linkLabelOffset);
391
392 el.attr('transform', transformLabel(parms));
393 });
394
395 // Remove any labels that are no longer required.
396 api.linkLabel().exit().remove();
397 }
398
399 function rectAroundText(el) {
400 var text = el.select('text'),
401 box = text.node().getBBox();
402
403 // translate the bbox so that it is centered on [x,y]
404 box.x = -box.width / 2;
405 box.y = -box.height / 2;
406
407 // add padding
408 box.x -= 1;
409 box.width += 2;
410 return box;
411 }
412
413 function transformLabel(p) {
414 var dx = p.x2 - p.x1,
415 dy = p.y2 - p.y1,
416 xMid = dx/2 + p.x1,
417 yMid = dy/2 + p.y1;
418 return sus.translate(xMid, yMid);
419 }
420
Simon Hunt1a5301e2015-02-25 15:31:25 -0800421 function applyPortLabels(data, portLabelG) {
422 var entering = portLabelG.selectAll('.portLabel')
423 .data(data).enter().append('g')
424 .classed('portLabel', true)
425 .attr('id', function (d) { return d.id; });
426
427 entering.each(function (d) {
428 var el = d3.select(this),
429 rect = el.append('rect'),
430 text = el.append('text').text(d.num);
431
432 rect.attr(rectAroundText(el));
433 text.attr('dy', linkLabelOffset);
Simon Hunt969b3c92015-02-25 18:11:31 -0800434 el.attr('transform', sus.translate(d.x, d.y));
Simon Hunt1a5301e2015-02-25 15:31:25 -0800435 });
436 }
437
Simon Hunta4242de2015-02-24 17:11:55 -0800438 // ==========================
439 // Module definition
440
441 angular.module('ovTopo')
442 .factory('TopoD3Service',
443 ['$log', 'FnService', 'SvgUtilService', 'IconService', 'ThemeService',
444
445 function (_$log_, _fs_, _sus_, _is_, _ts_) {
446 $log = _$log_;
447 fs = _fs_;
448 sus = _sus_;
449 is = _is_;
450 ts = _ts_;
451
452 icfg = is.iconConfig();
453
454 function initD3(_api_) {
455 api = _api_;
456 }
457
458 function destroyD3() { }
459
460 return {
461 initD3: initD3,
462 destroyD3: destroyD3,
463
464 incDevLabIndex: incDevLabIndex,
465 adjustRectToFitText: adjustRectToFitText,
466 hostLabel: hostLabel,
467 deviceLabel: deviceLabel,
468 trimLabel: trimLabel,
469
470 updateDeviceLabel: updateDeviceLabel,
471 updateHostLabel: updateHostLabel,
472 updateDeviceColors: updateDeviceColors,
473
474 deviceExisting: deviceExisting,
475 hostExisting: hostExisting,
476 deviceEnter: deviceEnter,
477 hostEnter: hostEnter,
478 hostExit: hostExit,
479 deviceExit: deviceExit,
480
Simon Hunta4242de2015-02-24 17:11:55 -0800481 linkEntering: linkEntering,
482 applyLinkLabels: applyLinkLabels,
Simon Hunt1a5301e2015-02-25 15:31:25 -0800483 transformLabel: transformLabel,
484 applyPortLabels: applyPortLabels
Simon Hunta4242de2015-02-24 17:11:55 -0800485 };
486 }]);
487}());