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 = { |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 31 | useLiveData: true, |
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 | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 85 | note_for_links: 'link.type is used to differentiate', |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 86 | linkDistance: { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 87 | direct: 100, |
| 88 | optical: 120, |
Simon Hunt | f67722a | 2014-11-10 09:32:06 -0800 | [diff] [blame] | 89 | hostLink: 5 |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 90 | }, |
| 91 | linkStrength: { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 92 | direct: 1.0, |
| 93 | optical: 1.0, |
| 94 | hostLink: 1.0 |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 95 | }, |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 96 | note_for_nodes: 'node.class is used to differentiate', |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 97 | charge: { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 98 | device: -8000, |
| 99 | host: -300 |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 100 | }, |
| 101 | pad: 20, |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 102 | translate: function() { |
| 103 | return 'translate(' + |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 104 | config.force.pad + ',' + |
| 105 | config.force.pad + ')'; |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 106 | } |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 107 | } |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 108 | }; |
| 109 | |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 110 | // radio buttons |
| 111 | var btnSet = [ |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 112 | { text: 'All Layers', cb: showAllLayers }, |
| 113 | { text: 'Packet Only', cb: showPacketLayer }, |
| 114 | { text: 'Optical Only', cb: showOpticalLayer } |
| 115 | ]; |
| 116 | |
| 117 | // key bindings |
| 118 | var keyDispatch = { |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 119 | M: testMe, // TODO: remove (testing only) |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 120 | S: injectStartupEvents, // TODO: remove (testing only) |
| 121 | space: injectTestEvent, // TODO: remove (testing only) |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 122 | |
Thomas Vachuska | 65368e3 | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 123 | B: toggleBg, // TODO: do we really need this? |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 124 | L: cycleLabels, |
| 125 | P: togglePorts, |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 126 | U: unpin, |
| 127 | |
| 128 | X: requestPath |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 129 | }; |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 130 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 131 | // state variables |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 132 | var network = { |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 133 | view: null, // view token reference |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 134 | nodes: [], |
| 135 | links: [], |
| 136 | lookup: {} |
| 137 | }, |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 138 | scenario = { |
| 139 | evDir: 'json/ev/', |
| 140 | evScenario: '/scenario.json', |
| 141 | evPrefix: '/ev_', |
| 142 | evOnos: '_onos.json', |
| 143 | evUi: '_ui.json', |
| 144 | ctx: null, |
| 145 | params: {}, |
| 146 | evNumber: 0, |
| 147 | view: null, |
| 148 | debug: false |
| 149 | }, |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 150 | webSock, |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 151 | deviceLabelIndex = 0, |
| 152 | hostLabelIndex = 0, |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 153 | |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 154 | selectOrder = [], |
| 155 | selections = {}, |
| 156 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 157 | highlighted = null, |
| 158 | hovered = null, |
| 159 | viewMode = 'showAll', |
| 160 | portLabelsOn = false; |
| 161 | |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 162 | // D3 selections |
| 163 | var svg, |
| 164 | bgImg, |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 165 | topoG, |
| 166 | nodeG, |
| 167 | linkG, |
| 168 | node, |
| 169 | link; |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 170 | |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 171 | // ============================== |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 172 | // For Debugging / Development |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 173 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 174 | function note(label, msg) { |
| 175 | console.log('NOTE: ' + label + ': ' + msg); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 176 | } |
| 177 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 178 | function debug(what) { |
| 179 | return config.debugOn && config.debug[what]; |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 182 | |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 183 | // ============================== |
| 184 | // Key Callbacks |
| 185 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 186 | function testMe(view) { |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 187 | view.alert('test'); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 188 | } |
| 189 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 190 | function abortIfLive() { |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 191 | if (config.useLiveData) { |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 192 | scenario.view.alert("Sorry, currently using live data.."); |
| 193 | return true; |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 194 | } |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 195 | return false; |
| 196 | } |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 197 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 198 | function testDebug(msg) { |
| 199 | if (scenario.debug) { |
| 200 | scenario.view.alert(msg); |
| 201 | } |
| 202 | } |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 203 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 204 | function injectTestEvent(view) { |
| 205 | if (abortIfLive()) { return; } |
| 206 | var sc = scenario, |
| 207 | evn = ++sc.evNumber, |
| 208 | pfx = sc.evDir + sc.ctx + sc.evPrefix + evn, |
| 209 | onosUrl = pfx + sc.evOnos, |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 210 | uiUrl = pfx + sc.evUi, |
| 211 | stack = [ |
| 212 | { url: onosUrl, cb: handleServerEvent }, |
| 213 | { url: uiUrl, cb: handleUiEvent } |
| 214 | ]; |
| 215 | recurseFetchEvent(stack, evn); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 216 | } |
| 217 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 218 | function recurseFetchEvent(stack, evn) { |
| 219 | var v = scenario.view, |
| 220 | frame; |
| 221 | if (stack.length === 0) { |
| 222 | v.alert('Error:\n\nNo event #' + evn + ' found.'); |
| 223 | return; |
| 224 | } |
| 225 | frame = stack.shift(); |
| 226 | |
| 227 | d3.json(frame.url, function (err, data) { |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 228 | if (err) { |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 229 | if (err.status === 404) { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 230 | // if we didn't find the data, try the next stack frame |
| 231 | recurseFetchEvent(stack, evn); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 232 | } else { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 233 | v.alert('non-404 error:\n\n' + frame.url + '\n\n' + err); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 234 | } |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 235 | } else { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 236 | testDebug('loaded: ' + frame.url); |
| 237 | frame.cb(data); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 238 | } |
| 239 | }); |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 240 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 241 | } |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 242 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 243 | function handleUiEvent(data) { |
| 244 | testDebug('handleUiEvent(): ' + data.event); |
| 245 | // TODO: |
| 246 | } |
| 247 | |
| 248 | function injectStartupEvents(view) { |
| 249 | var last = scenario.params.lastAuto || 0; |
| 250 | if (abortIfLive()) { return; } |
| 251 | |
| 252 | while (scenario.evNumber < last) { |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 253 | injectTestEvent(view); |
| 254 | } |
| 255 | } |
| 256 | |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 257 | function toggleBg() { |
| 258 | var vis = bgImg.style('visibility'); |
| 259 | bgImg.style('visibility', (vis === 'hidden') ? 'visible' : 'hidden'); |
| 260 | } |
| 261 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 262 | function cycleLabels() { |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 263 | deviceLabelIndex = (deviceLabelIndex === network.deviceLabelCount - 1) ? 0 : deviceLabelIndex + 1; |
Simon Hunt | 5f36d34 | 2014-11-08 21:33:14 -0800 | [diff] [blame] | 264 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 265 | network.nodes.forEach(function (d) { |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 266 | if (d.class !== 'device') { return; } |
| 267 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 268 | var label = niceLabel(deviceLabel(d)), |
| 269 | node = d.el, |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 270 | box; |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 271 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 272 | node.select('text') |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 273 | .text(label) |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 274 | .style('opacity', 0) |
| 275 | .transition() |
| 276 | .style('opacity', 1); |
| 277 | |
| 278 | box = adjustRectToFitText(node); |
| 279 | |
| 280 | node.select('rect') |
| 281 | .transition() |
| 282 | .attr(box); |
| 283 | |
| 284 | node.select('image') |
| 285 | .transition() |
| 286 | .attr('x', box.x + config.icons.xoff) |
| 287 | .attr('y', box.y + config.icons.yoff); |
| 288 | }); |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | function togglePorts(view) { |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 292 | view.alert('togglePorts() callback') |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | function unpin(view) { |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 296 | view.alert('unpin() callback') |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 297 | } |
| 298 | |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 299 | function requestPath(view) { |
| 300 | var payload = { |
| 301 | one: selections[selectOrder[0]].obj.id, |
| 302 | two: selections[selectOrder[1]].obj.id |
| 303 | } |
| 304 | sendMessage('requestPath', payload); |
| 305 | } |
| 306 | |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 307 | // ============================== |
| 308 | // Radio Button Callbacks |
| 309 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 310 | function showAllLayers() { |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 311 | // network.node.classed('inactive', false); |
| 312 | // network.link.classed('inactive', false); |
| 313 | // d3.selectAll('svg .port').classed('inactive', false); |
| 314 | // d3.selectAll('svg .portText').classed('inactive', false); |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 315 | // TODO ... |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 316 | network.view.alert('showAllLayers() callback'); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | function showPacketLayer() { |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 320 | showAllLayers(); |
| 321 | // TODO ... |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 322 | network.view.alert('showPacketLayer() callback'); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | function showOpticalLayer() { |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 326 | showAllLayers(); |
| 327 | // TODO ... |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 328 | network.view.alert('showOpticalLayer() callback'); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 329 | } |
| 330 | |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 331 | // ============================== |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 332 | // Private functions |
| 333 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 334 | function safeId(s) { |
| 335 | return s.replace(/[^a-z0-9]/gi, '-'); |
| 336 | } |
| 337 | |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 338 | // set the size of the given element to that of the view (reduced if padded) |
| 339 | function setSize(el, view, pad) { |
| 340 | var padding = pad ? pad * 2 : 0; |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 341 | el.attr({ |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 342 | width: view.width() - padding, |
| 343 | height: view.height() - padding |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 344 | }); |
| 345 | } |
| 346 | |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 347 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 348 | // ============================== |
| 349 | // Event handlers for server-pushed events |
| 350 | |
| 351 | var eventDispatch = { |
| 352 | addDevice: addDevice, |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 353 | updateDevice: stillToImplement, |
| 354 | removeDevice: stillToImplement, |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 355 | addLink: addLink, |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 356 | updateLink: stillToImplement, |
| 357 | removeLink: stillToImplement, |
| 358 | addHost: addHost, |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 359 | updateHost: updateHost, |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 360 | removeHost: stillToImplement, |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 361 | showPath: showPath |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 362 | }; |
| 363 | |
| 364 | function addDevice(data) { |
| 365 | var device = data.payload, |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 366 | nodeData = createDeviceNode(device); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 367 | note('addDevice', device.id); |
| 368 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 369 | network.nodes.push(nodeData); |
| 370 | network.lookup[nodeData.id] = nodeData; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 371 | updateNodes(); |
| 372 | network.force.start(); |
| 373 | } |
| 374 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 375 | function addLink(data) { |
| 376 | var link = data.payload, |
| 377 | lnk = createLink(link); |
| 378 | |
| 379 | if (lnk) { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 380 | note('addLink', link.id); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 381 | |
| 382 | network.links.push(lnk); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 383 | network.lookup[lnk.id] = lnk; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 384 | updateLinks(); |
| 385 | network.force.start(); |
| 386 | } |
| 387 | } |
| 388 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 389 | function addHost(data) { |
| 390 | var host = data.payload, |
| 391 | node = createHostNode(host), |
| 392 | lnk; |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 393 | note('addHost', node.id); |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 394 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 395 | network.nodes.push(node); |
| 396 | network.lookup[host.id] = node; |
| 397 | updateNodes(); |
| 398 | |
| 399 | lnk = createHostLink(host); |
| 400 | if (lnk) { |
| 401 | network.links.push(lnk); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 402 | network.lookup[host.ingress] = lnk; |
| 403 | network.lookup[host.egress] = lnk; |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 404 | updateLinks(); |
| 405 | } |
| 406 | network.force.start(); |
| 407 | } |
| 408 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 409 | function updateHost(data) { |
| 410 | var host = data.payload, |
| 411 | hostData = network.lookup[host.id]; |
| 412 | note('updateHost', host.id); |
| 413 | |
| 414 | $.extend(hostData, host); |
| 415 | updateNodes(); |
| 416 | } |
| 417 | |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 418 | function showPath(data) { |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 419 | var links = data.payload.links, |
| 420 | s = [ data.event + "\n" + links.length ]; |
| 421 | links.forEach(function (d, i) { |
| 422 | s.push(d); |
| 423 | }); |
| 424 | network.view.alert(s.join('\n')); |
| 425 | |
| 426 | links.forEach(function (d, i) { |
| 427 | var link = network.lookup[d]; |
| 428 | if (link) { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 429 | link.el.classed('showPath', true); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 430 | } |
| 431 | }); |
| 432 | |
| 433 | // TODO: add selection-highlite lines to links |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 434 | } |
| 435 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 436 | // ............................... |
| 437 | |
| 438 | function stillToImplement(data) { |
| 439 | var p = data.payload; |
| 440 | note(data.event, p.id); |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 441 | network.view.alert('Not yet implemented: "' + data.event + '"'); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 442 | } |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 443 | |
| 444 | function unknownEvent(data) { |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 445 | network.view.alert('Unknown event type: "' + data.event + '"'); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | function handleServerEvent(data) { |
| 449 | var fn = eventDispatch[data.event] || unknownEvent; |
| 450 | fn(data); |
| 451 | } |
| 452 | |
| 453 | // ============================== |
| 454 | // force layout modification functions |
| 455 | |
| 456 | function translate(x, y) { |
| 457 | return 'translate(' + x + ',' + y + ')'; |
| 458 | } |
| 459 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 460 | function createHostLink(host) { |
| 461 | var src = host.id, |
| 462 | dst = host.cp.device, |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 463 | id = host.ingress, |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 464 | srcNode = network.lookup[src], |
| 465 | dstNode = network.lookup[dst], |
| 466 | lnk; |
| 467 | |
| 468 | if (!dstNode) { |
| 469 | // TODO: send warning message back to server on websocket |
| 470 | network.view.alert('switch not on map for link\n\n' + |
| 471 | 'src = ' + src + '\ndst = ' + dst); |
| 472 | return null; |
| 473 | } |
| 474 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 475 | // Compose link ... |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 476 | lnk = { |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 477 | id: id, |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 478 | source: srcNode, |
| 479 | target: dstNode, |
| 480 | class: 'link', |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 481 | type: 'hostLink', |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 482 | svgClass: 'link hostLink', |
| 483 | x1: srcNode.x, |
| 484 | y1: srcNode.y, |
| 485 | x2: dstNode.x, |
| 486 | y2: dstNode.y, |
| 487 | width: 1 |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 488 | } |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 489 | return lnk; |
| 490 | } |
| 491 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 492 | function createLink(link) { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 493 | // start with the link object as is |
| 494 | var lnk = link, |
| 495 | type = link.type, |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 496 | src = link.src, |
| 497 | dst = link.dst, |
| 498 | w = link.linkWidth, |
| 499 | srcNode = network.lookup[src], |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 500 | dstNode = network.lookup[dst]; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 501 | |
| 502 | if (!(srcNode && dstNode)) { |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 503 | // TODO: send warning message back to server on websocket |
| 504 | network.view.alert('nodes not on map for link\n\n' + |
| 505 | 'src = ' + src + '\ndst = ' + dst); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 506 | return null; |
| 507 | } |
| 508 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 509 | // Augment as needed... |
| 510 | $.extend(lnk, { |
| 511 | source: srcNode, |
| 512 | target: dstNode, |
| 513 | class: 'link', |
| 514 | svgClass: type ? 'link ' + type : 'link', |
| 515 | x1: srcNode.x, |
| 516 | y1: srcNode.y, |
| 517 | x2: dstNode.x, |
| 518 | y2: dstNode.y, |
| 519 | width: w |
| 520 | }); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 521 | return lnk; |
| 522 | } |
| 523 | |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 524 | function linkWidth(w) { |
| 525 | // w is number of links between nodes. Scale appropriately. |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 526 | // TODO: use a d3.scale (linear, log, ... ?) |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 527 | return w * 1.2; |
| 528 | } |
| 529 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 530 | function updateLinks() { |
| 531 | link = linkG.selectAll('.link') |
| 532 | .data(network.links, function (d) { return d.id; }); |
| 533 | |
| 534 | // operate on existing links, if necessary |
| 535 | // link .foo() .bar() ... |
| 536 | |
| 537 | // operate on entering links: |
| 538 | var entering = link.enter() |
| 539 | .append('line') |
| 540 | .attr({ |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 541 | class: function (d) { return d.svgClass; }, |
| 542 | x1: function (d) { return d.x1; }, |
| 543 | y1: function (d) { return d.y1; }, |
| 544 | x2: function (d) { return d.x2; }, |
| 545 | y2: function (d) { return d.y2; }, |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 546 | stroke: config.topo.linkInColor, |
| 547 | 'stroke-width': config.topo.linkInWidth |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 548 | }) |
| 549 | .transition().duration(1000) |
| 550 | .attr({ |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 551 | 'stroke-width': function (d) { return linkWidth(d.width); }, |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 552 | stroke: '#666' // TODO: remove explicit stroke, rather... |
| 553 | }); |
| 554 | |
| 555 | // augment links |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 556 | entering.each(function (d) { |
| 557 | var link = d3.select(this); |
| 558 | // provide ref to element selection from backing data.... |
| 559 | d.el = link; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 560 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 561 | // TODO: add src/dst port labels etc. |
| 562 | }); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 563 | |
| 564 | // operate on both existing and new links, if necessary |
| 565 | //link .foo() .bar() ... |
| 566 | |
| 567 | // operate on exiting links: |
| 568 | // TODO: figure out how to remove the node 'g' AND its children |
| 569 | link.exit() |
| 570 | .transition() |
| 571 | .duration(750) |
| 572 | .attr({ |
| 573 | opacity: 0 |
| 574 | }) |
| 575 | .remove(); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | function createDeviceNode(device) { |
| 579 | // start with the object as is |
| 580 | var node = device, |
| 581 | type = device.type; |
| 582 | |
| 583 | // Augment as needed... |
| 584 | node.class = 'device'; |
| 585 | node.svgClass = type ? 'node device ' + type : 'node device'; |
| 586 | positionNode(node); |
| 587 | |
| 588 | // cache label array length |
| 589 | network.deviceLabelCount = device.labels.length; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 590 | return node; |
| 591 | } |
| 592 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 593 | function createHostNode(host) { |
| 594 | // start with the object as is |
| 595 | var node = host; |
| 596 | |
| 597 | // Augment as needed... |
| 598 | node.class = 'host'; |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 599 | if (!node.type) { |
| 600 | // TODO: perhaps type would be: {phone, tablet, laptop, endstation} ? |
| 601 | node.type = 'endstation'; |
| 602 | } |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 603 | node.svgClass = 'node host'; |
| 604 | // TODO: consider placing near its switch, if [x,y] not defined |
| 605 | positionNode(node); |
| 606 | |
| 607 | // cache label array length |
| 608 | network.hostLabelCount = host.labels.length; |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 609 | return node; |
| 610 | } |
| 611 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 612 | function positionNode(node) { |
| 613 | var meta = node.metaUi, |
| 614 | x = 0, |
| 615 | y = 0; |
| 616 | |
| 617 | if (meta) { |
| 618 | x = meta.x; |
| 619 | y = meta.y; |
| 620 | } |
| 621 | if (x && y) { |
| 622 | node.fixed = true; |
| 623 | } |
| 624 | node.x = x || network.view.width() / 2; |
| 625 | node.y = y || network.view.height() / 2; |
| 626 | } |
| 627 | |
| 628 | |
| 629 | function iconUrl(d) { |
| 630 | return 'img/' + d.type + '.png'; |
| 631 | } |
| 632 | |
| 633 | // returns the newly computed bounding box of the rectangle |
| 634 | function adjustRectToFitText(n) { |
| 635 | var text = n.select('text'), |
| 636 | box = text.node().getBBox(), |
| 637 | lab = config.labels; |
| 638 | |
| 639 | text.attr('text-anchor', 'middle') |
| 640 | .attr('y', '-0.8em') |
| 641 | .attr('x', lab.imgPad/2); |
| 642 | |
| 643 | // translate the bbox so that it is centered on [x,y] |
| 644 | box.x = -box.width / 2; |
| 645 | box.y = -box.height / 2; |
| 646 | |
| 647 | // add padding |
| 648 | box.x -= (lab.padLR + lab.imgPad/2); |
| 649 | box.width += lab.padLR * 2 + lab.imgPad; |
| 650 | box.y -= lab.padTB; |
| 651 | box.height += lab.padTB * 2; |
| 652 | |
| 653 | return box; |
| 654 | } |
| 655 | |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 656 | function mkSvgClass(d) { |
| 657 | return d.fixed ? d.svgClass + ' fixed' : d.svgClass; |
| 658 | } |
| 659 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 660 | function hostLabel(d) { |
| 661 | var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0; |
| 662 | return d.labels[idx]; |
| 663 | } |
| 664 | function deviceLabel(d) { |
| 665 | var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0; |
| 666 | return d.labels[idx]; |
| 667 | } |
| 668 | function niceLabel(label) { |
| 669 | return (label && label.trim()) ? label : '.'; |
| 670 | } |
| 671 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 672 | function updateNodes() { |
| 673 | node = nodeG.selectAll('.node') |
| 674 | .data(network.nodes, function (d) { return d.id; }); |
| 675 | |
| 676 | // operate on existing nodes, if necessary |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 677 | // update host labels |
| 678 | node.filter('.host').select('text') |
| 679 | .text(hostLabel); |
| 680 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 681 | //node .foo() .bar() ... |
| 682 | |
| 683 | // operate on entering nodes: |
| 684 | var entering = node.enter() |
| 685 | .append('g') |
| 686 | .attr({ |
| 687 | id: function (d) { return safeId(d.id); }, |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 688 | class: mkSvgClass, |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 689 | transform: function (d) { return translate(d.x, d.y); }, |
| 690 | opacity: 0 |
| 691 | }) |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 692 | .call(network.drag) |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 693 | //.on('mouseover', function (d) {}) |
| 694 | //.on('mouseover', function (d) {}) |
| 695 | .transition() |
| 696 | .attr('opacity', 1); |
| 697 | |
| 698 | // augment device nodes... |
| 699 | entering.filter('.device').each(function (d) { |
| 700 | var node = d3.select(this), |
| 701 | icon = iconUrl(d), |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 702 | label = niceLabel(deviceLabel(d)), |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 703 | box; |
| 704 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 705 | // provide ref to element from backing data.... |
| 706 | d.el = node; |
| 707 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 708 | node.append('rect') |
| 709 | .attr({ |
| 710 | 'rx': 5, |
| 711 | 'ry': 5 |
| 712 | }); |
| 713 | |
| 714 | node.append('text') |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 715 | .text(label) |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 716 | .attr('dy', '1.1em'); |
| 717 | |
| 718 | box = adjustRectToFitText(node); |
| 719 | |
| 720 | node.select('rect') |
| 721 | .attr(box); |
| 722 | |
| 723 | if (icon) { |
| 724 | var cfg = config.icons; |
| 725 | node.append('svg:image') |
| 726 | .attr({ |
| 727 | x: box.x + config.icons.xoff, |
| 728 | y: box.y + config.icons.yoff, |
| 729 | width: cfg.w, |
| 730 | height: cfg.h, |
| 731 | 'xlink:href': icon |
| 732 | }); |
| 733 | } |
| 734 | |
| 735 | // debug function to show the modelled x,y coordinates of nodes... |
| 736 | if (debug('showNodeXY')) { |
| 737 | node.select('rect').attr('fill-opacity', 0.5); |
| 738 | node.append('circle') |
| 739 | .attr({ |
| 740 | class: 'debug', |
| 741 | cx: 0, |
| 742 | cy: 0, |
| 743 | r: '3px' |
| 744 | }); |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 745 | } |
| 746 | }); |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 747 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 748 | // augment host nodes... |
| 749 | entering.filter('.host').each(function (d) { |
| 750 | var node = d3.select(this), |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 751 | box; |
| 752 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 753 | // provide ref to element from backing data.... |
| 754 | d.el = node; |
| 755 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 756 | node.append('circle') |
| 757 | .attr('r', 8); // TODO: define host circle radius |
| 758 | |
| 759 | // TODO: are we attaching labels to hosts? |
| 760 | node.append('text') |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 761 | .text(hostLabel) |
| 762 | .attr('dy', '1.3em') |
| 763 | .attr('text-anchor', 'middle'); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 764 | |
| 765 | // debug function to show the modelled x,y coordinates of nodes... |
| 766 | if (debug('showNodeXY')) { |
| 767 | node.select('circle').attr('fill-opacity', 0.5); |
| 768 | node.append('circle') |
| 769 | .attr({ |
| 770 | class: 'debug', |
| 771 | cx: 0, |
| 772 | cy: 0, |
| 773 | r: '3px' |
| 774 | }); |
| 775 | } |
| 776 | }); |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 777 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 778 | // operate on both existing and new nodes, if necessary |
| 779 | //node .foo() .bar() ... |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 780 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 781 | // operate on exiting nodes: |
| 782 | // TODO: figure out how to remove the node 'g' AND its children |
| 783 | node.exit() |
| 784 | .transition() |
| 785 | .duration(750) |
| 786 | .attr({ |
| 787 | opacity: 0, |
| 788 | cx: 0, |
| 789 | cy: 0, |
| 790 | r: 0 |
| 791 | }) |
| 792 | .remove(); |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | |
| 796 | function tick() { |
| 797 | node.attr({ |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 798 | transform: function (d) { return translate(d.x, d.y); } |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 799 | }); |
| 800 | |
| 801 | link.attr({ |
| 802 | x1: function (d) { return d.source.x; }, |
| 803 | y1: function (d) { return d.source.y; }, |
| 804 | x2: function (d) { return d.target.x; }, |
| 805 | y2: function (d) { return d.target.y; } |
| 806 | }); |
| 807 | } |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 808 | |
| 809 | // ============================== |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 810 | // Web-Socket for live data |
| 811 | |
| 812 | function webSockUrl() { |
| 813 | return document.location.toString() |
| 814 | .replace(/\#.*/, '') |
| 815 | .replace('http://', 'ws://') |
| 816 | .replace('https://', 'wss://') |
| 817 | .replace('index2.html', config.webSockUrl); |
| 818 | } |
| 819 | |
| 820 | webSock = { |
| 821 | ws : null, |
| 822 | |
| 823 | connect : function() { |
| 824 | webSock.ws = new WebSocket(webSockUrl()); |
| 825 | |
| 826 | webSock.ws.onopen = function() { |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 827 | }; |
| 828 | |
| 829 | webSock.ws.onmessage = function(m) { |
| 830 | if (m.data) { |
| 831 | console.log(m.data); |
Thomas Vachuska | d472c6e | 2014-11-07 19:11:05 -0800 | [diff] [blame] | 832 | handleServerEvent(JSON.parse(m.data)); |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 833 | } |
| 834 | }; |
| 835 | |
| 836 | webSock.ws.onclose = function(m) { |
| 837 | webSock.ws = null; |
| 838 | }; |
| 839 | }, |
| 840 | |
| 841 | send : function(text) { |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 842 | if (text != null) { |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 843 | webSock._send(text); |
| 844 | } |
| 845 | }, |
| 846 | |
| 847 | _send : function(message) { |
| 848 | if (webSock.ws) { |
| 849 | webSock.ws.send(message); |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 850 | } else { |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 851 | network.view.alert('no web socket open\n\n' + message); |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 852 | } |
| 853 | } |
| 854 | |
| 855 | }; |
| 856 | |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 857 | var sid = 0; |
| 858 | |
| 859 | function sendMessage(evType, payload) { |
| 860 | var toSend = { |
| 861 | event: evType, |
| 862 | sid: ++sid, |
| 863 | payload: payload |
| 864 | }; |
| 865 | webSock.send(JSON.stringify(toSend)); |
| 866 | } |
| 867 | |
| 868 | |
| 869 | // ============================== |
| 870 | // Selection stuff |
| 871 | |
| 872 | function selectObject(obj, el) { |
| 873 | var n, |
| 874 | meta = d3.event.sourceEvent.metaKey; |
| 875 | |
| 876 | if (el) { |
| 877 | n = d3.select(el); |
| 878 | } else { |
| 879 | node.each(function(d) { |
| 880 | if (d == obj) { |
| 881 | n = d3.select(el = this); |
| 882 | } |
| 883 | }); |
| 884 | } |
| 885 | if (!n) return; |
| 886 | |
| 887 | if (meta && n.classed('selected')) { |
| 888 | deselectObject(obj.id); |
| 889 | //flyinPane(null); |
| 890 | return; |
| 891 | } |
| 892 | |
| 893 | if (!meta) { |
| 894 | deselectAll(); |
| 895 | } |
| 896 | |
Simon Hunt | 5f36d34 | 2014-11-08 21:33:14 -0800 | [diff] [blame] | 897 | selections[obj.id] = { obj: obj, el : el}; |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 898 | selectOrder.push(obj.id); |
| 899 | |
| 900 | n.classed('selected', true); |
| 901 | //flyinPane(obj); |
| 902 | } |
| 903 | |
| 904 | function deselectObject(id) { |
| 905 | var obj = selections[id]; |
| 906 | if (obj) { |
| 907 | d3.select(obj.el).classed('selected', false); |
| 908 | selections[id] = null; |
| 909 | // TODO: use splice to remove element |
| 910 | } |
| 911 | //flyinPane(null); |
| 912 | } |
| 913 | |
| 914 | function deselectAll() { |
| 915 | // deselect all nodes in the network... |
| 916 | node.classed('selected', false); |
| 917 | selections = {}; |
| 918 | selectOrder = []; |
| 919 | //flyinPane(null); |
| 920 | } |
| 921 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 922 | // TODO: this click handler does not get unloaded when the view does |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 923 | $('#view').on('click', function(e) { |
| 924 | if (!$(e.target).closest('.node').length) { |
| 925 | if (!e.metaKey) { |
| 926 | deselectAll(); |
| 927 | } |
| 928 | } |
| 929 | }); |
| 930 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 931 | |
| 932 | function prepareScenario(view, ctx, dbg) { |
| 933 | var sc = scenario, |
| 934 | urlSc = sc.evDir + ctx + sc.evScenario; |
| 935 | |
| 936 | if (!ctx) { |
| 937 | view.alert("No scenario specified (null ctx)"); |
| 938 | return; |
| 939 | } |
| 940 | |
| 941 | sc.view = view; |
| 942 | sc.ctx = ctx; |
| 943 | sc.debug = dbg; |
| 944 | sc.evNumber = 0; |
| 945 | |
| 946 | d3.json(urlSc, function(err, data) { |
| 947 | var p = data && data.params || {}; |
| 948 | if (err) { |
| 949 | view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err); |
| 950 | } else { |
| 951 | sc.params = p; |
| 952 | view.alert("Scenario loaded: " + ctx + '\n\n' + data.title); |
| 953 | } |
| 954 | }); |
| 955 | |
| 956 | } |
| 957 | |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 958 | // ============================== |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 959 | // View life-cycle callbacks |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 960 | |
Simon Hunt | f67722a | 2014-11-10 09:32:06 -0800 | [diff] [blame] | 961 | function preload(view, ctx, flags) { |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 962 | var w = view.width(), |
| 963 | h = view.height(), |
| 964 | idBg = view.uid('bg'), |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 965 | showBg = config.options.showBackground ? 'visible' : 'hidden', |
| 966 | fcfg = config.force, |
| 967 | fpad = fcfg.pad, |
| 968 | forceDim = [w - 2*fpad, h - 2*fpad]; |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 969 | |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 970 | // NOTE: view.$div is a D3 selection of the view's div |
| 971 | svg = view.$div.append('svg'); |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 972 | setSize(svg, view); |
| 973 | |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 974 | // add blue glow filter to svg layer |
| 975 | d3u.appendGlow(svg); |
| 976 | |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 977 | // load the background image |
| 978 | bgImg = svg.append('svg:image') |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 979 | .attr({ |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 980 | id: idBg, |
| 981 | width: w, |
| 982 | height: h, |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 983 | 'xlink:href': config.backgroundUrl |
| 984 | }) |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 985 | .style({ |
| 986 | visibility: showBg |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 987 | }); |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 988 | |
| 989 | // group for the topology |
| 990 | topoG = svg.append('g') |
| 991 | .attr('transform', fcfg.translate()); |
| 992 | |
| 993 | // subgroups for links and nodes |
| 994 | linkG = topoG.append('g').attr('id', 'links'); |
| 995 | nodeG = topoG.append('g').attr('id', 'nodes'); |
| 996 | |
| 997 | // selection of nodes and links |
| 998 | link = linkG.selectAll('.link'); |
| 999 | node = nodeG.selectAll('.node'); |
| 1000 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1001 | function chrg(d) { |
| 1002 | return fcfg.charge[d.class] || -12000; |
| 1003 | } |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1004 | function ldist(d) { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1005 | return fcfg.linkDistance[d.type] || 50; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1006 | } |
| 1007 | function lstrg(d) { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1008 | // 0.0 - 1.0 |
| 1009 | return fcfg.linkStrength[d.type] || 1.0; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1010 | } |
| 1011 | |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 1012 | function selectCb(d, self) { |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1013 | selectObject(d, self); |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | function atDragEnd(d, self) { |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1017 | // once we've finished moving, pin the node in position |
| 1018 | d.fixed = true; |
| 1019 | d3.select(self).classed('fixed', true); |
| 1020 | if (config.useLiveData) { |
| 1021 | tellServerCoords(d); |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 1022 | } |
| 1023 | } |
| 1024 | |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1025 | function tellServerCoords(d) { |
| 1026 | sendMessage('updateMeta', { |
| 1027 | id: d.id, |
| 1028 | 'class': d.class, |
Simon Hunt | 5f36d34 | 2014-11-08 21:33:14 -0800 | [diff] [blame] | 1029 | x: Math.floor(d.x), |
| 1030 | y: Math.floor(d.y) |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1031 | }); |
| 1032 | } |
| 1033 | |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1034 | // set up the force layout |
| 1035 | network.force = d3.layout.force() |
| 1036 | .size(forceDim) |
| 1037 | .nodes(network.nodes) |
| 1038 | .links(network.links) |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1039 | .gravity(0.4) |
| 1040 | .friction(0.7) |
| 1041 | .charge(chrg) |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1042 | .linkDistance(ldist) |
| 1043 | .linkStrength(lstrg) |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1044 | .on('tick', tick); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1045 | |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 1046 | network.drag = d3u.createDragBehavior(network.force, selectCb, atDragEnd); |
| 1047 | } |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1048 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1049 | function load(view, ctx, flags) { |
Simon Hunt | f67722a | 2014-11-10 09:32:06 -0800 | [diff] [blame] | 1050 | // resize, in case the window was resized while we were not loaded |
| 1051 | resize(view, ctx, flags); |
| 1052 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1053 | // cache the view token, so network topo functions can access it |
| 1054 | network.view = view; |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1055 | config.useLiveData = !flags.local; |
| 1056 | |
| 1057 | if (!config.useLiveData) { |
| 1058 | prepareScenario(view, ctx, flags.debug); |
| 1059 | } |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1060 | |
| 1061 | // set our radio buttons and key bindings |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 1062 | view.setRadio(btnSet); |
| 1063 | view.setKeys(keyDispatch); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1064 | |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 1065 | if (config.useLiveData) { |
| 1066 | webSock.connect(); |
| 1067 | } |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1068 | } |
| 1069 | |
Simon Hunt | f67722a | 2014-11-10 09:32:06 -0800 | [diff] [blame] | 1070 | function resize(view, ctx, flags) { |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 1071 | setSize(svg, view); |
| 1072 | setSize(bgImg, view); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1073 | |
| 1074 | // TODO: hook to recompute layout, perhaps? work with zoom/pan code |
| 1075 | // adjust force layout size |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 1076 | } |
| 1077 | |
| 1078 | |
| 1079 | // ============================== |
| 1080 | // View registration |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1081 | |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 1082 | onos.ui.addView('topo', { |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 1083 | preload: preload, |
| 1084 | load: load, |
| 1085 | resize: resize |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1086 | }); |
| 1087 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1088 | }(ONOS)); |