Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | /* |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 18 | ONOS network topology viewer - version 1.1 |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 19 | |
| 20 | @author Simon Hunt |
| 21 | */ |
| 22 | |
| 23 | (function (onos) { |
| 24 | 'use strict'; |
| 25 | |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 26 | // shorter names for library APIs |
| 27 | var d3u = onos.lib.d3util; |
| 28 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 29 | // configuration data |
| 30 | var config = { |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 31 | useLiveData: false, |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 32 | debugOn: false, |
| 33 | debug: { |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 34 | showNodeXY: true, |
| 35 | showKeyHandler: false |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 36 | }, |
| 37 | options: { |
| 38 | layering: true, |
| 39 | collisionPrevention: true, |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 40 | showBackground: true |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 41 | }, |
| 42 | backgroundUrl: 'img/us-map.png', |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 43 | webSockUrl: 'ws/topology', |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 44 | data: { |
| 45 | live: { |
| 46 | jsonUrl: 'rs/topology/graph', |
| 47 | detailPrefix: 'rs/topology/graph/', |
| 48 | detailSuffix: '' |
| 49 | }, |
| 50 | fake: { |
| 51 | jsonUrl: 'json/network2.json', |
| 52 | detailPrefix: 'json/', |
| 53 | detailSuffix: '.json' |
| 54 | } |
| 55 | }, |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 56 | labels: { |
| 57 | imgPad: 16, |
| 58 | padLR: 4, |
| 59 | padTB: 3, |
| 60 | marginLR: 3, |
| 61 | marginTB: 2, |
| 62 | port: { |
| 63 | gap: 3, |
| 64 | width: 18, |
| 65 | height: 14 |
| 66 | } |
| 67 | }, |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 68 | topo: { |
| 69 | linkInColor: '#66f', |
| 70 | linkInWidth: 14 |
| 71 | }, |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 72 | icons: { |
| 73 | w: 28, |
| 74 | h: 28, |
| 75 | xoff: -12, |
| 76 | yoff: -8 |
| 77 | }, |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 78 | iconUrl: { |
| 79 | device: 'img/device.png', |
| 80 | host: 'img/host.png', |
| 81 | pkt: 'img/pkt.png', |
| 82 | opt: 'img/opt.png' |
| 83 | }, |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 84 | force: { |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 85 | note: 'node.class or link.class is used to differentiate', |
| 86 | linkDistance: { |
| 87 | infra: 200, |
| 88 | host: 40 |
| 89 | }, |
| 90 | linkStrength: { |
| 91 | infra: 1.0, |
| 92 | host: 1.0 |
| 93 | }, |
| 94 | charge: { |
| 95 | device: -400, |
| 96 | host: -100 |
| 97 | }, |
| 98 | pad: 20, |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 99 | translate: function() { |
| 100 | return 'translate(' + |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 101 | config.force.pad + ',' + |
| 102 | config.force.pad + ')'; |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 103 | } |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 104 | } |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 105 | }; |
| 106 | |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 107 | // radio buttons |
| 108 | var btnSet = [ |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 109 | { text: 'All Layers', cb: showAllLayers }, |
| 110 | { text: 'Packet Only', cb: showPacketLayer }, |
| 111 | { text: 'Optical Only', cb: showOpticalLayer } |
| 112 | ]; |
| 113 | |
| 114 | // key bindings |
| 115 | var keyDispatch = { |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 116 | space: injectTestEvent, // TODO: remove (testing only) |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 117 | S: injectStartupEvents, // TODO: remove (testing only) |
| 118 | A: testAlert, // TODO: remove (testing only) |
| 119 | M: testMe, // TODO: remove (testing only) |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 120 | |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 121 | B: toggleBg, |
| 122 | G: toggleLayout, |
| 123 | L: cycleLabels, |
| 124 | P: togglePorts, |
| 125 | U: unpin |
| 126 | }; |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 127 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 128 | // state variables |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 129 | var network = { |
| 130 | nodes: [], |
| 131 | links: [], |
| 132 | lookup: {} |
| 133 | }, |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 134 | webSock, |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 135 | labelIdx = 0, |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 136 | selected = {}, |
| 137 | highlighted = null, |
| 138 | hovered = null, |
| 139 | viewMode = 'showAll', |
| 140 | portLabelsOn = false; |
| 141 | |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 142 | // D3 selections |
| 143 | var svg, |
| 144 | bgImg, |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 145 | topoG, |
| 146 | nodeG, |
| 147 | linkG, |
| 148 | node, |
| 149 | link; |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 150 | |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 151 | // ============================== |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 152 | // For Debugging / Development |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 153 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 154 | var eventPrefix = 'json/eventTest_', |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 155 | eventNumber = 0, |
| 156 | alertNumber = 0; |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 157 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 158 | function note(label, msg) { |
| 159 | console.log('NOTE: ' + label + ': ' + msg); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 162 | function debug(what) { |
| 163 | return config.debugOn && config.debug[what]; |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 166 | |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 167 | // ============================== |
| 168 | // Key Callbacks |
| 169 | |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 170 | function testAlert(view) { |
| 171 | alertNumber++; |
| 172 | view.alert("Test me! -- " + alertNumber); |
| 173 | } |
| 174 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 175 | function testMe(view) { |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | function injectTestEvent(view) { |
| 179 | eventNumber++; |
| 180 | var eventUrl = eventPrefix + eventNumber + '.json'; |
| 181 | |
| 182 | console.log('Fetching JSON: ' + eventUrl); |
| 183 | d3.json(eventUrl, function(err, data) { |
| 184 | if (err) { |
| 185 | view.dataLoadError(err, eventUrl); |
| 186 | } else { |
| 187 | handleServerEvent(data); |
| 188 | } |
| 189 | }); |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 190 | } |
| 191 | |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 192 | function injectStartupEvents(view) { |
| 193 | var lastStartupEvent = 32; |
| 194 | while (eventNumber < lastStartupEvent) { |
| 195 | injectTestEvent(view); |
| 196 | } |
| 197 | } |
| 198 | |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 199 | function toggleBg() { |
| 200 | var vis = bgImg.style('visibility'); |
| 201 | bgImg.style('visibility', (vis === 'hidden') ? 'visible' : 'hidden'); |
| 202 | } |
| 203 | |
| 204 | function toggleLayout(view) { |
| 205 | |
| 206 | } |
| 207 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 208 | function cycleLabels() { |
| 209 | labelIdx = (labelIdx === network.deviceLabelCount - 1) ? 0 : labelIdx + 1; |
| 210 | network.nodes.forEach(function (d) { |
| 211 | var idx = (labelIdx < d.labels.length) ? labelIdx : 0, |
| 212 | node = d3.select('#' + safeId(d.id)), |
| 213 | box; |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 214 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 215 | node.select('text') |
| 216 | .text(d.labels[idx]) |
| 217 | .style('opacity', 0) |
| 218 | .transition() |
| 219 | .style('opacity', 1); |
| 220 | |
| 221 | box = adjustRectToFitText(node); |
| 222 | |
| 223 | node.select('rect') |
| 224 | .transition() |
| 225 | .attr(box); |
| 226 | |
| 227 | node.select('image') |
| 228 | .transition() |
| 229 | .attr('x', box.x + config.icons.xoff) |
| 230 | .attr('y', box.y + config.icons.yoff); |
| 231 | }); |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | function togglePorts(view) { |
| 235 | |
| 236 | } |
| 237 | |
| 238 | function unpin(view) { |
| 239 | |
| 240 | } |
| 241 | |
| 242 | // ============================== |
| 243 | // Radio Button Callbacks |
| 244 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 245 | function showAllLayers() { |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 246 | // network.node.classed('inactive', false); |
| 247 | // network.link.classed('inactive', false); |
| 248 | // d3.selectAll('svg .port').classed('inactive', false); |
| 249 | // d3.selectAll('svg .portText').classed('inactive', false); |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 250 | // TODO ... |
| 251 | console.log('showAllLayers()'); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | function showPacketLayer() { |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 255 | showAllLayers(); |
| 256 | // TODO ... |
| 257 | console.log('showPacketLayer()'); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | function showOpticalLayer() { |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 261 | showAllLayers(); |
| 262 | // TODO ... |
| 263 | console.log('showOpticalLayer()'); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 264 | } |
| 265 | |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 266 | // ============================== |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 267 | // Private functions |
| 268 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 269 | function safeId(s) { |
| 270 | return s.replace(/[^a-z0-9]/gi, '-'); |
| 271 | } |
| 272 | |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 273 | // set the size of the given element to that of the view (reduced if padded) |
| 274 | function setSize(el, view, pad) { |
| 275 | var padding = pad ? pad * 2 : 0; |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 276 | el.attr({ |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 277 | width: view.width() - padding, |
| 278 | height: view.height() - padding |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 279 | }); |
| 280 | } |
| 281 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 282 | function establishWebSocket() { |
| 283 | // TODO: establish a real web-socket |
| 284 | // NOTE, for now, we are using the 'Q' key to artificially inject |
| 285 | // "events" from the server. |
| 286 | } |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 287 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 288 | // ============================== |
| 289 | // Event handlers for server-pushed events |
| 290 | |
| 291 | var eventDispatch = { |
| 292 | addDevice: addDevice, |
| 293 | updateDevice: updateDevice, |
| 294 | removeDevice: removeDevice, |
| 295 | addLink: addLink |
| 296 | }; |
| 297 | |
| 298 | function addDevice(data) { |
| 299 | var device = data.payload, |
| 300 | node = createDeviceNode(device); |
| 301 | note('addDevice', device.id); |
| 302 | |
| 303 | network.nodes.push(node); |
| 304 | network.lookup[node.id] = node; |
| 305 | updateNodes(); |
| 306 | network.force.start(); |
| 307 | } |
| 308 | |
| 309 | function updateDevice(data) { |
| 310 | var device = data.payload; |
| 311 | note('updateDevice', device.id); |
| 312 | |
| 313 | } |
| 314 | |
| 315 | function removeDevice(data) { |
| 316 | var device = data.payload; |
| 317 | note('removeDevice', device.id); |
| 318 | |
| 319 | } |
| 320 | |
| 321 | function addLink(data) { |
| 322 | var link = data.payload, |
| 323 | lnk = createLink(link); |
| 324 | |
| 325 | if (lnk) { |
| 326 | note('addLink', lnk.id); |
| 327 | |
| 328 | network.links.push(lnk); |
| 329 | updateLinks(); |
| 330 | network.force.start(); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | // .... |
| 335 | |
| 336 | function unknownEvent(data) { |
| 337 | // TODO: use dialog, not alert |
| 338 | alert('Unknown event type: "' + data.event + '"'); |
| 339 | } |
| 340 | |
| 341 | function handleServerEvent(data) { |
| 342 | var fn = eventDispatch[data.event] || unknownEvent; |
| 343 | fn(data); |
| 344 | } |
| 345 | |
| 346 | // ============================== |
| 347 | // force layout modification functions |
| 348 | |
| 349 | function translate(x, y) { |
| 350 | return 'translate(' + x + ',' + y + ')'; |
| 351 | } |
| 352 | |
| 353 | function createLink(link) { |
| 354 | var type = link.type, |
| 355 | src = link.src, |
| 356 | dst = link.dst, |
| 357 | w = link.linkWidth, |
| 358 | srcNode = network.lookup[src], |
| 359 | dstNode = network.lookup[dst], |
| 360 | lnk; |
| 361 | |
| 362 | if (!(srcNode && dstNode)) { |
| 363 | alert('nodes not on map'); |
| 364 | return null; |
| 365 | } |
| 366 | |
| 367 | lnk = { |
| 368 | id: safeId(src) + '~' + safeId(dst), |
| 369 | source: srcNode, |
| 370 | target: dstNode, |
| 371 | class: 'link', |
| 372 | svgClass: type ? 'link ' + type : 'link', |
| 373 | x1: srcNode.x, |
| 374 | y1: srcNode.y, |
| 375 | x2: dstNode.x, |
| 376 | y2: dstNode.y, |
| 377 | width: w |
| 378 | }; |
| 379 | return lnk; |
| 380 | } |
| 381 | |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 382 | function linkWidth(w) { |
| 383 | // w is number of links between nodes. Scale appropriately. |
| 384 | return w * 1.2; |
| 385 | } |
| 386 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 387 | function updateLinks() { |
| 388 | link = linkG.selectAll('.link') |
| 389 | .data(network.links, function (d) { return d.id; }); |
| 390 | |
| 391 | // operate on existing links, if necessary |
| 392 | // link .foo() .bar() ... |
| 393 | |
| 394 | // operate on entering links: |
| 395 | var entering = link.enter() |
| 396 | .append('line') |
| 397 | .attr({ |
| 398 | id: function (d) { return d.id; }, |
| 399 | class: function (d) { return d.svgClass; }, |
| 400 | x1: function (d) { return d.x1; }, |
| 401 | y1: function (d) { return d.y1; }, |
| 402 | x2: function (d) { return d.x2; }, |
| 403 | y2: function (d) { return d.y2; }, |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 404 | stroke: config.topo.linkInColor, |
| 405 | 'stroke-width': config.topo.linkInWidth |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 406 | }) |
| 407 | .transition().duration(1000) |
| 408 | .attr({ |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 409 | 'stroke-width': function (d) { return linkWidth(d.width); }, |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 410 | stroke: '#666' // TODO: remove explicit stroke, rather... |
| 411 | }); |
| 412 | |
| 413 | // augment links |
| 414 | // TODO: add src/dst port labels etc. |
| 415 | |
| 416 | } |
| 417 | |
| 418 | function createDeviceNode(device) { |
| 419 | // start with the object as is |
| 420 | var node = device, |
| 421 | type = device.type; |
| 422 | |
| 423 | // Augment as needed... |
| 424 | node.class = 'device'; |
| 425 | node.svgClass = type ? 'node device ' + type : 'node device'; |
| 426 | positionNode(node); |
| 427 | |
| 428 | // cache label array length |
| 429 | network.deviceLabelCount = device.labels.length; |
| 430 | |
| 431 | return node; |
| 432 | } |
| 433 | |
| 434 | function positionNode(node) { |
| 435 | var meta = node.metaUi, |
| 436 | x = 0, |
| 437 | y = 0; |
| 438 | |
| 439 | if (meta) { |
| 440 | x = meta.x; |
| 441 | y = meta.y; |
| 442 | } |
| 443 | if (x && y) { |
| 444 | node.fixed = true; |
| 445 | } |
| 446 | node.x = x || network.view.width() / 2; |
| 447 | node.y = y || network.view.height() / 2; |
| 448 | } |
| 449 | |
| 450 | |
| 451 | function iconUrl(d) { |
| 452 | return 'img/' + d.type + '.png'; |
| 453 | } |
| 454 | |
| 455 | // returns the newly computed bounding box of the rectangle |
| 456 | function adjustRectToFitText(n) { |
| 457 | var text = n.select('text'), |
| 458 | box = text.node().getBBox(), |
| 459 | lab = config.labels; |
| 460 | |
| 461 | text.attr('text-anchor', 'middle') |
| 462 | .attr('y', '-0.8em') |
| 463 | .attr('x', lab.imgPad/2); |
| 464 | |
| 465 | // translate the bbox so that it is centered on [x,y] |
| 466 | box.x = -box.width / 2; |
| 467 | box.y = -box.height / 2; |
| 468 | |
| 469 | // add padding |
| 470 | box.x -= (lab.padLR + lab.imgPad/2); |
| 471 | box.width += lab.padLR * 2 + lab.imgPad; |
| 472 | box.y -= lab.padTB; |
| 473 | box.height += lab.padTB * 2; |
| 474 | |
| 475 | return box; |
| 476 | } |
| 477 | |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 478 | function mkSvgClass(d) { |
| 479 | return d.fixed ? d.svgClass + ' fixed' : d.svgClass; |
| 480 | } |
| 481 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 482 | function updateNodes() { |
| 483 | node = nodeG.selectAll('.node') |
| 484 | .data(network.nodes, function (d) { return d.id; }); |
| 485 | |
| 486 | // operate on existing nodes, if necessary |
| 487 | //node .foo() .bar() ... |
| 488 | |
| 489 | // operate on entering nodes: |
| 490 | var entering = node.enter() |
| 491 | .append('g') |
| 492 | .attr({ |
| 493 | id: function (d) { return safeId(d.id); }, |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 494 | class: mkSvgClass, |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 495 | transform: function (d) { return translate(d.x, d.y); }, |
| 496 | opacity: 0 |
| 497 | }) |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 498 | .call(network.drag) |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 499 | //.on('mouseover', function (d) {}) |
| 500 | //.on('mouseover', function (d) {}) |
| 501 | .transition() |
| 502 | .attr('opacity', 1); |
| 503 | |
| 504 | // augment device nodes... |
| 505 | entering.filter('.device').each(function (d) { |
| 506 | var node = d3.select(this), |
| 507 | icon = iconUrl(d), |
| 508 | idx = (labelIdx < d.labels.length) ? labelIdx : 0, |
| 509 | box; |
| 510 | |
| 511 | node.append('rect') |
| 512 | .attr({ |
| 513 | 'rx': 5, |
| 514 | 'ry': 5 |
| 515 | }); |
| 516 | |
| 517 | node.append('text') |
| 518 | .text(d.labels[idx]) |
| 519 | .attr('dy', '1.1em'); |
| 520 | |
| 521 | box = adjustRectToFitText(node); |
| 522 | |
| 523 | node.select('rect') |
| 524 | .attr(box); |
| 525 | |
| 526 | if (icon) { |
| 527 | var cfg = config.icons; |
| 528 | node.append('svg:image') |
| 529 | .attr({ |
| 530 | x: box.x + config.icons.xoff, |
| 531 | y: box.y + config.icons.yoff, |
| 532 | width: cfg.w, |
| 533 | height: cfg.h, |
| 534 | 'xlink:href': icon |
| 535 | }); |
| 536 | } |
| 537 | |
| 538 | // debug function to show the modelled x,y coordinates of nodes... |
| 539 | if (debug('showNodeXY')) { |
| 540 | node.select('rect').attr('fill-opacity', 0.5); |
| 541 | node.append('circle') |
| 542 | .attr({ |
| 543 | class: 'debug', |
| 544 | cx: 0, |
| 545 | cy: 0, |
| 546 | r: '3px' |
| 547 | }); |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 548 | } |
| 549 | }); |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 550 | |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 551 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 552 | // operate on both existing and new nodes, if necessary |
| 553 | //node .foo() .bar() ... |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 554 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 555 | // operate on exiting nodes: |
| 556 | // TODO: figure out how to remove the node 'g' AND its children |
| 557 | node.exit() |
| 558 | .transition() |
| 559 | .duration(750) |
| 560 | .attr({ |
| 561 | opacity: 0, |
| 562 | cx: 0, |
| 563 | cy: 0, |
| 564 | r: 0 |
| 565 | }) |
| 566 | .remove(); |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | |
| 570 | function tick() { |
| 571 | node.attr({ |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 572 | transform: function (d) { return translate(d.x, d.y); } |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 573 | }); |
| 574 | |
| 575 | link.attr({ |
| 576 | x1: function (d) { return d.source.x; }, |
| 577 | y1: function (d) { return d.source.y; }, |
| 578 | x2: function (d) { return d.target.x; }, |
| 579 | y2: function (d) { return d.target.y; } |
| 580 | }); |
| 581 | } |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 582 | |
| 583 | // ============================== |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 584 | // Web-Socket for live data |
| 585 | |
| 586 | function webSockUrl() { |
| 587 | return document.location.toString() |
| 588 | .replace(/\#.*/, '') |
| 589 | .replace('http://', 'ws://') |
| 590 | .replace('https://', 'wss://') |
| 591 | .replace('index2.html', config.webSockUrl); |
| 592 | } |
| 593 | |
| 594 | webSock = { |
| 595 | ws : null, |
| 596 | |
| 597 | connect : function() { |
| 598 | webSock.ws = new WebSocket(webSockUrl()); |
| 599 | |
| 600 | webSock.ws.onopen = function() { |
| 601 | webSock._send("Hi there!"); |
| 602 | }; |
| 603 | |
| 604 | webSock.ws.onmessage = function(m) { |
| 605 | if (m.data) { |
| 606 | console.log(m.data); |
| 607 | } |
| 608 | }; |
| 609 | |
| 610 | webSock.ws.onclose = function(m) { |
| 611 | webSock.ws = null; |
| 612 | }; |
| 613 | }, |
| 614 | |
| 615 | send : function(text) { |
| 616 | if (text != null && text.length > 0) { |
| 617 | webSock._send(text); |
| 618 | } |
| 619 | }, |
| 620 | |
| 621 | _send : function(message) { |
| 622 | if (webSock.ws) { |
| 623 | webSock.ws.send(message); |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | }; |
| 628 | |
| 629 | // ============================== |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 630 | // View life-cycle callbacks |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 631 | |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 632 | function preload(view, ctx) { |
| 633 | var w = view.width(), |
| 634 | h = view.height(), |
| 635 | idBg = view.uid('bg'), |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 636 | showBg = config.options.showBackground ? 'visible' : 'hidden', |
| 637 | fcfg = config.force, |
| 638 | fpad = fcfg.pad, |
| 639 | forceDim = [w - 2*fpad, h - 2*fpad]; |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 640 | |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 641 | // NOTE: view.$div is a D3 selection of the view's div |
| 642 | svg = view.$div.append('svg'); |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 643 | setSize(svg, view); |
| 644 | |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 645 | // add blue glow filter to svg layer |
| 646 | d3u.appendGlow(svg); |
| 647 | |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 648 | // load the background image |
| 649 | bgImg = svg.append('svg:image') |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 650 | .attr({ |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 651 | id: idBg, |
| 652 | width: w, |
| 653 | height: h, |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 654 | 'xlink:href': config.backgroundUrl |
| 655 | }) |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 656 | .style({ |
| 657 | visibility: showBg |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 658 | }); |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 659 | |
| 660 | // group for the topology |
| 661 | topoG = svg.append('g') |
| 662 | .attr('transform', fcfg.translate()); |
| 663 | |
| 664 | // subgroups for links and nodes |
| 665 | linkG = topoG.append('g').attr('id', 'links'); |
| 666 | nodeG = topoG.append('g').attr('id', 'nodes'); |
| 667 | |
| 668 | // selection of nodes and links |
| 669 | link = linkG.selectAll('.link'); |
| 670 | node = nodeG.selectAll('.node'); |
| 671 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 672 | function ldist(d) { |
| 673 | return fcfg.linkDistance[d.class] || 150; |
| 674 | } |
| 675 | function lstrg(d) { |
| 676 | return fcfg.linkStrength[d.class] || 1; |
| 677 | } |
| 678 | function lchrg(d) { |
| 679 | return fcfg.charge[d.class] || -200; |
| 680 | } |
| 681 | |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 682 | function selectCb(d, self) { |
| 683 | // TODO: selectObject(d, self); |
| 684 | } |
| 685 | |
| 686 | function atDragEnd(d, self) { |
| 687 | // once we've finished moving, pin the node in position, |
| 688 | // if it is a device (not a host) |
| 689 | if (d.class === 'device') { |
| 690 | d.fixed = true; |
| 691 | d3.select(self).classed('fixed', true) |
| 692 | // TODO: send new [x,y] back to server, via websocket. |
| 693 | } |
| 694 | } |
| 695 | |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 696 | // set up the force layout |
| 697 | network.force = d3.layout.force() |
| 698 | .size(forceDim) |
| 699 | .nodes(network.nodes) |
| 700 | .links(network.links) |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 701 | .charge(lchrg) |
| 702 | .linkDistance(ldist) |
| 703 | .linkStrength(lstrg) |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 704 | .on('tick', tick); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 705 | |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 706 | network.drag = d3u.createDragBehavior(network.force, selectCb, atDragEnd); |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 707 | webSock.connect(); |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 708 | } |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 709 | |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 710 | function load(view, ctx) { |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 711 | // cache the view token, so network topo functions can access it |
| 712 | network.view = view; |
| 713 | |
| 714 | // set our radio buttons and key bindings |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 715 | view.setRadio(btnSet); |
| 716 | view.setKeys(keyDispatch); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 717 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 718 | establishWebSocket(); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 719 | } |
| 720 | |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 721 | function resize(view, ctx) { |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 722 | setSize(svg, view); |
| 723 | setSize(bgImg, view); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 724 | |
| 725 | // TODO: hook to recompute layout, perhaps? work with zoom/pan code |
| 726 | // adjust force layout size |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | |
| 730 | // ============================== |
| 731 | // View registration |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 732 | |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 733 | onos.ui.addView('topo', { |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 734 | preload: preload, |
| 735 | load: load, |
| 736 | resize: resize |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 737 | }); |
| 738 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 739 | }(ONOS)); |