Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 1 | /* |
| 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 Cole | 9cf1a8d | 2015-04-21 13:15:29 -0700 | [diff] [blame] | 137 | 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 Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 142 | } |
| 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 Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 340 | 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, |
Bri Prebilic Cole | 038aedd | 2015-07-13 15:25:16 -0700 | [diff] [blame] | 373 | text; |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 374 | |
| 375 | if (d.ldata.type() === 'hostLink') { |
| 376 | el.classed('hostLinkLabel', true); |
| 377 | sus.visible(el, api.showHosts()); |
| 378 | } |
| 379 | |
| 380 | d.el = el; |
| 381 | rect = el.append('rect'); |
| 382 | text = el.append('text').text(d.label); |
| 383 | rect.attr(rectAroundText(el)); |
| 384 | text.attr('dy', linkLabelOffset); |
| 385 | |
Bri Prebilic Cole | 038aedd | 2015-07-13 15:25:16 -0700 | [diff] [blame] | 386 | el.attr('transform', transformLabel(d.ldata.position)); |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 387 | }); |
| 388 | |
| 389 | // Remove any labels that are no longer required. |
| 390 | api.linkLabel().exit().remove(); |
| 391 | } |
| 392 | |
| 393 | function rectAroundText(el) { |
| 394 | var text = el.select('text'), |
| 395 | box = text.node().getBBox(); |
| 396 | |
| 397 | // translate the bbox so that it is centered on [x,y] |
| 398 | box.x = -box.width / 2; |
| 399 | box.y = -box.height / 2; |
| 400 | |
| 401 | // add padding |
| 402 | box.x -= 1; |
| 403 | box.width += 2; |
| 404 | return box; |
| 405 | } |
| 406 | |
| 407 | function transformLabel(p) { |
| 408 | var dx = p.x2 - p.x1, |
| 409 | dy = p.y2 - p.y1, |
| 410 | xMid = dx/2 + p.x1, |
| 411 | yMid = dy/2 + p.y1; |
| 412 | return sus.translate(xMid, yMid); |
| 413 | } |
| 414 | |
Simon Hunt | 1a5301e | 2015-02-25 15:31:25 -0800 | [diff] [blame] | 415 | function applyPortLabels(data, portLabelG) { |
| 416 | var entering = portLabelG.selectAll('.portLabel') |
| 417 | .data(data).enter().append('g') |
| 418 | .classed('portLabel', true) |
| 419 | .attr('id', function (d) { return d.id; }); |
| 420 | |
| 421 | entering.each(function (d) { |
| 422 | var el = d3.select(this), |
| 423 | rect = el.append('rect'), |
| 424 | text = el.append('text').text(d.num); |
| 425 | |
| 426 | rect.attr(rectAroundText(el)); |
| 427 | text.attr('dy', linkLabelOffset); |
Simon Hunt | 969b3c9 | 2015-02-25 18:11:31 -0800 | [diff] [blame] | 428 | el.attr('transform', sus.translate(d.x, d.y)); |
Simon Hunt | 1a5301e | 2015-02-25 15:31:25 -0800 | [diff] [blame] | 429 | }); |
| 430 | } |
| 431 | |
Bri Prebilic Cole | 8040176 | 2015-07-16 11:36:18 -0700 | [diff] [blame] | 432 | function labelPoint(linkPos) { |
| 433 | var lengthUpLine = 1 / 3, |
| 434 | dx = linkPos.x2 - linkPos.x1, |
| 435 | dy = linkPos.y2 - linkPos.y1, |
| 436 | movedX = dx * lengthUpLine, |
| 437 | movedY = dy * lengthUpLine; |
| 438 | |
| 439 | return { |
| 440 | x: movedX, |
| 441 | y: movedY |
| 442 | }; |
| 443 | } |
| 444 | |
| 445 | function calcGroupPos(linkPos) { |
| 446 | var moved = labelPoint(linkPos); |
| 447 | return sus.translate(linkPos.x1 + moved.x, linkPos.y1 + moved.y); |
| 448 | } |
| 449 | |
| 450 | // calculates where on the link that the hash line for 5+ label appears |
| 451 | function hashAttrs(linkPos) { |
| 452 | var hashLength = 25, |
| 453 | halfLength = hashLength / 2, |
| 454 | dx = linkPos.x2 - linkPos.x1, |
| 455 | dy = linkPos.y2 - linkPos.y1, |
| 456 | length = Math.sqrt((dx * dx) + (dy * dy)), |
| 457 | moveAmtX = (dx / length) * halfLength, |
| 458 | moveAmtY = (dy / length) * halfLength, |
| 459 | mid = labelPoint(linkPos), |
| 460 | angle = Math.atan(dy / dx) + 45; |
| 461 | |
| 462 | return { |
| 463 | x1: mid.x - moveAmtX, |
| 464 | y1: mid.y - moveAmtY, |
| 465 | x2: mid.x + moveAmtX, |
| 466 | y2: mid.y + moveAmtY, |
| 467 | stroke: api.linkConfig()[ts.theme()].baseColor, |
| 468 | transform: 'rotate(' + angle + ',' + mid.x + ',' + mid.y + ')' |
| 469 | }; |
| 470 | } |
| 471 | |
| 472 | function textLabelPos(linkPos) { |
| 473 | var point = labelPoint(linkPos), |
| 474 | dist = 20; |
| 475 | return { |
| 476 | x: point.x + dist, |
| 477 | y: point.y + dist |
| 478 | }; |
| 479 | } |
| 480 | |
| 481 | function applyNumLinkLabels(data, lblsG) { |
| 482 | var labels = lblsG.selectAll('g.numLinkLabel') |
| 483 | .data(data, function (d) { return 'pair-' + d.id; }), |
| 484 | entering; |
| 485 | |
| 486 | // update existing labels |
| 487 | labels.each(function (d) { |
| 488 | var el = d3.select(this); |
| 489 | |
| 490 | el.attr({ |
| 491 | transform: function (d) { return calcGroupPos(d.linkCoords); } |
| 492 | }); |
| 493 | el.select('line') |
| 494 | .attr(hashAttrs(d.linkCoords)); |
| 495 | el.select('text') |
| 496 | .attr(textLabelPos(d.linkCoords)) |
| 497 | .text(d.num); |
| 498 | }); |
| 499 | |
| 500 | // add new labels |
| 501 | entering = labels |
| 502 | .enter() |
| 503 | .append('g') |
| 504 | .attr({ |
| 505 | transform: function (d) { return calcGroupPos(d.linkCoords); }, |
| 506 | id: function (d) { return 'pair-' + d.id; } |
| 507 | }) |
| 508 | .classed('numLinkLabel', true); |
| 509 | |
| 510 | entering.each(function (d) { |
| 511 | var el = d3.select(this); |
| 512 | |
| 513 | el.append('line') |
| 514 | .classed('numLinkHash', true) |
| 515 | .attr(hashAttrs(d.linkCoords)); |
| 516 | el.append('text') |
| 517 | .classed('numLinkText', true) |
| 518 | .attr(textLabelPos(d.linkCoords)) |
| 519 | .text(d.num); |
| 520 | }); |
| 521 | |
| 522 | // remove old labels |
| 523 | labels.exit().remove(); |
| 524 | } |
| 525 | |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 526 | // ========================== |
| 527 | // Module definition |
| 528 | |
| 529 | angular.module('ovTopo') |
| 530 | .factory('TopoD3Service', |
| 531 | ['$log', 'FnService', 'SvgUtilService', 'IconService', 'ThemeService', |
| 532 | |
| 533 | function (_$log_, _fs_, _sus_, _is_, _ts_) { |
| 534 | $log = _$log_; |
| 535 | fs = _fs_; |
| 536 | sus = _sus_; |
| 537 | is = _is_; |
| 538 | ts = _ts_; |
| 539 | |
| 540 | icfg = is.iconConfig(); |
| 541 | |
| 542 | function initD3(_api_) { |
| 543 | api = _api_; |
| 544 | } |
| 545 | |
| 546 | function destroyD3() { } |
| 547 | |
| 548 | return { |
| 549 | initD3: initD3, |
| 550 | destroyD3: destroyD3, |
| 551 | |
| 552 | incDevLabIndex: incDevLabIndex, |
| 553 | adjustRectToFitText: adjustRectToFitText, |
| 554 | hostLabel: hostLabel, |
| 555 | deviceLabel: deviceLabel, |
| 556 | trimLabel: trimLabel, |
| 557 | |
| 558 | updateDeviceLabel: updateDeviceLabel, |
| 559 | updateHostLabel: updateHostLabel, |
| 560 | updateDeviceColors: updateDeviceColors, |
| 561 | |
| 562 | deviceExisting: deviceExisting, |
| 563 | hostExisting: hostExisting, |
| 564 | deviceEnter: deviceEnter, |
| 565 | hostEnter: hostEnter, |
| 566 | hostExit: hostExit, |
| 567 | deviceExit: deviceExit, |
| 568 | |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 569 | linkEntering: linkEntering, |
| 570 | applyLinkLabels: applyLinkLabels, |
Simon Hunt | 1a5301e | 2015-02-25 15:31:25 -0800 | [diff] [blame] | 571 | transformLabel: transformLabel, |
Bri Prebilic Cole | 8040176 | 2015-07-16 11:36:18 -0700 | [diff] [blame] | 572 | applyPortLabels: applyPortLabels, |
| 573 | applyNumLinkLabels: applyNumLinkLabels |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 574 | }; |
| 575 | }]); |
| 576 | }()); |