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 |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 27 | var d3u = onos.lib.d3util, |
Simon Hunt | 12ce12e | 2014-11-15 21:13:19 -0800 | [diff] [blame] | 28 | gly = onos.lib.glyphs, |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 29 | trace; |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 30 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 31 | // configuration data |
| 32 | var config = { |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 33 | useLiveData: true, |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 34 | fnTrace: true, |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 35 | debugOn: false, |
| 36 | debug: { |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 37 | showNodeXY: true, |
| 38 | showKeyHandler: false |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 39 | }, |
Simon Hunt | 12ce12e | 2014-11-15 21:13:19 -0800 | [diff] [blame] | 40 | birdDim: 400, |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 41 | options: { |
| 42 | layering: true, |
| 43 | collisionPrevention: true, |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 44 | showBackground: true |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 45 | }, |
| 46 | backgroundUrl: 'img/us-map.png', |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 47 | webSockUrl: 'ws/topology', |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 48 | data: { |
| 49 | live: { |
| 50 | jsonUrl: 'rs/topology/graph', |
| 51 | detailPrefix: 'rs/topology/graph/', |
| 52 | detailSuffix: '' |
| 53 | }, |
| 54 | fake: { |
| 55 | jsonUrl: 'json/network2.json', |
| 56 | detailPrefix: 'json/', |
| 57 | detailSuffix: '.json' |
| 58 | } |
| 59 | }, |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 60 | labels: { |
| 61 | imgPad: 16, |
| 62 | padLR: 4, |
| 63 | padTB: 3, |
| 64 | marginLR: 3, |
| 65 | marginTB: 2, |
| 66 | port: { |
| 67 | gap: 3, |
| 68 | width: 18, |
| 69 | height: 14 |
| 70 | } |
| 71 | }, |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 72 | topo: { |
| 73 | linkInColor: '#66f', |
| 74 | linkInWidth: 14 |
| 75 | }, |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 76 | icons: { |
| 77 | w: 28, |
| 78 | h: 28, |
| 79 | xoff: -12, |
| 80 | yoff: -8 |
| 81 | }, |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 82 | iconUrl: { |
| 83 | device: 'img/device.png', |
| 84 | host: 'img/host.png', |
| 85 | pkt: 'img/pkt.png', |
| 86 | opt: 'img/opt.png' |
| 87 | }, |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 88 | force: { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 89 | note_for_links: 'link.type is used to differentiate', |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 90 | linkDistance: { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 91 | direct: 100, |
| 92 | optical: 120, |
Thomas Vachuska | 3266abf | 2014-11-13 09:28:46 -0800 | [diff] [blame] | 93 | hostLink: 3 |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 94 | }, |
| 95 | linkStrength: { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 96 | direct: 1.0, |
| 97 | optical: 1.0, |
| 98 | hostLink: 1.0 |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 99 | }, |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 100 | note_for_nodes: 'node.class is used to differentiate', |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 101 | charge: { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 102 | device: -8000, |
Thomas Vachuska | 3266abf | 2014-11-13 09:28:46 -0800 | [diff] [blame] | 103 | host: -5000 |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 104 | }, |
| 105 | pad: 20, |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 106 | translate: function() { |
| 107 | return 'translate(' + |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 108 | config.force.pad + ',' + |
| 109 | config.force.pad + ')'; |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 110 | } |
Paul Greyson | 6cb8ca0 | 2014-11-12 18:09:02 -0800 | [diff] [blame] | 111 | }, |
| 112 | // see below in creation of viewBox on main svg |
| 113 | logicalSize: 1000 |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 114 | }; |
| 115 | |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 116 | // radio buttons |
Simon Hunt | 9462e8c | 2014-11-14 17:28:09 -0800 | [diff] [blame] | 117 | var layerButtons = [ |
| 118 | { text: 'All Layers', id: 'all', cb: showAllLayers }, |
| 119 | { text: 'Packet Only', id: 'pkt', cb: showPacketLayer }, |
| 120 | { text: 'Optical Only', id: 'opt', cb: showOpticalLayer } |
| 121 | ], |
| 122 | layerBtnSet, |
| 123 | layerBtnDispatch = { |
| 124 | all: showAllLayers, |
| 125 | pkt: showPacketLayer, |
| 126 | opt: showOpticalLayer |
| 127 | }; |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 128 | |
| 129 | // key bindings |
| 130 | var keyDispatch = { |
Simon Hunt | a255a2c | 2014-11-13 22:29:35 -0800 | [diff] [blame] | 131 | M: testMe, // TODO: remove (testing only) |
| 132 | S: injectStartupEvents, // TODO: remove (testing only) |
| 133 | space: injectTestEvent, // TODO: remove (testing only) |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 134 | |
Simon Hunt | 01095ff | 2014-11-13 16:37:29 -0800 | [diff] [blame] | 135 | B: toggleBg, |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 136 | L: cycleLabels, |
| 137 | P: togglePorts, |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 138 | U: unpin, |
Paul Greyson | fcba0e8 | 2014-11-13 10:21:16 -0800 | [diff] [blame] | 139 | R: resetZoomPan, |
Simon Hunt | 9462e8c | 2014-11-14 17:28:09 -0800 | [diff] [blame] | 140 | esc: handleEscape |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 141 | }; |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 142 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 143 | // state variables |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 144 | var network = { |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 145 | view: null, // view token reference |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 146 | nodes: [], |
| 147 | links: [], |
Simon Hunt | 269670f | 2014-11-17 16:17:43 -0800 | [diff] [blame] | 148 | lookup: {}, |
| 149 | revLinkToKey: {} |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 150 | }, |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 151 | scenario = { |
| 152 | evDir: 'json/ev/', |
| 153 | evScenario: '/scenario.json', |
| 154 | evPrefix: '/ev_', |
| 155 | evOnos: '_onos.json', |
| 156 | evUi: '_ui.json', |
| 157 | ctx: null, |
| 158 | params: {}, |
| 159 | evNumber: 0, |
| 160 | view: null, |
| 161 | debug: false |
| 162 | }, |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 163 | webSock, |
Simon Hunt | 0c6d419 | 2014-11-12 12:07:10 -0800 | [diff] [blame] | 164 | sid = 0, |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 165 | deviceLabelIndex = 0, |
| 166 | hostLabelIndex = 0, |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 167 | selections = {}, |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 168 | selectOrder = [], |
Simon Hunt | 6ac93f3 | 2014-11-13 12:17:27 -0800 | [diff] [blame] | 169 | hovered = null, |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 170 | detailPane, |
Simon Hunt | a255a2c | 2014-11-13 22:29:35 -0800 | [diff] [blame] | 171 | antTimer = null, |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 172 | onosInstances = {}, |
| 173 | onosOrder = [], |
| 174 | oiBox, |
Simon Hunt | 9462e8c | 2014-11-14 17:28:09 -0800 | [diff] [blame] | 175 | oiShowMaster = false, |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 176 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 177 | portLabelsOn = false; |
| 178 | |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 179 | // D3 selections |
| 180 | var svg, |
Paul Greyson | fcba0e8 | 2014-11-13 10:21:16 -0800 | [diff] [blame] | 181 | zoomPanContainer, |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 182 | bgImg, |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 183 | topoG, |
| 184 | nodeG, |
| 185 | linkG, |
| 186 | node, |
Simon Hunt | 0c6d419 | 2014-11-12 12:07:10 -0800 | [diff] [blame] | 187 | link, |
| 188 | mask; |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 189 | |
Paul Greyson | 6cb8ca0 | 2014-11-12 18:09:02 -0800 | [diff] [blame] | 190 | // the projection for the map background |
| 191 | var geoMapProjection; |
| 192 | |
Paul Greyson | fcba0e8 | 2014-11-13 10:21:16 -0800 | [diff] [blame] | 193 | // the zoom function |
| 194 | var zoom; |
| 195 | |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 196 | // ============================== |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 197 | // For Debugging / Development |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 198 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 199 | function note(label, msg) { |
| 200 | console.log('NOTE: ' + label + ': ' + msg); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 201 | } |
| 202 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 203 | function debug(what) { |
| 204 | return config.debugOn && config.debug[what]; |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 205 | } |
| 206 | |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 207 | function fnTrace(msg, id) { |
| 208 | if (config.fnTrace) { |
| 209 | console.log('FN: ' + msg + ' [' + id + ']'); |
| 210 | } |
| 211 | } |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 212 | |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 213 | function evTrace(data) { |
| 214 | fnTrace(data.event, data.payload.id); |
| 215 | } |
| 216 | |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 217 | // ============================== |
| 218 | // Key Callbacks |
| 219 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 220 | function testMe(view) { |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 221 | //view.alert('test'); |
Simon Hunt | 12ce12e | 2014-11-15 21:13:19 -0800 | [diff] [blame] | 222 | noWebSock(true); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 223 | } |
| 224 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 225 | function abortIfLive() { |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 226 | if (config.useLiveData) { |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 227 | network.view.alert("Sorry, currently using live data.."); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 228 | return true; |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 229 | } |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 230 | return false; |
| 231 | } |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 232 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 233 | function testDebug(msg) { |
| 234 | if (scenario.debug) { |
| 235 | scenario.view.alert(msg); |
| 236 | } |
| 237 | } |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 238 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 239 | function injectTestEvent(view) { |
| 240 | if (abortIfLive()) { return; } |
| 241 | var sc = scenario, |
| 242 | evn = ++sc.evNumber, |
| 243 | pfx = sc.evDir + sc.ctx + sc.evPrefix + evn, |
| 244 | onosUrl = pfx + sc.evOnos, |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 245 | uiUrl = pfx + sc.evUi, |
| 246 | stack = [ |
| 247 | { url: onosUrl, cb: handleServerEvent }, |
| 248 | { url: uiUrl, cb: handleUiEvent } |
| 249 | ]; |
| 250 | recurseFetchEvent(stack, evn); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 251 | } |
| 252 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 253 | function recurseFetchEvent(stack, evn) { |
| 254 | var v = scenario.view, |
| 255 | frame; |
| 256 | if (stack.length === 0) { |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 257 | v.alert('Oops!\n\nNo event #' + evn + ' found.'); |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 258 | return; |
| 259 | } |
| 260 | frame = stack.shift(); |
| 261 | |
| 262 | d3.json(frame.url, function (err, data) { |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 263 | if (err) { |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 264 | if (err.status === 404) { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 265 | // if we didn't find the data, try the next stack frame |
| 266 | recurseFetchEvent(stack, evn); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 267 | } else { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 268 | v.alert('non-404 error:\n\n' + frame.url + '\n\n' + err); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 269 | } |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 270 | } else { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 271 | testDebug('loaded: ' + frame.url); |
Simon Hunt | 1712ed8 | 2014-11-17 12:56:00 -0800 | [diff] [blame] | 272 | wsTrace('test', JSON.stringify(data)); |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 273 | frame.cb(data); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 274 | } |
| 275 | }); |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 276 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 277 | } |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 278 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 279 | function handleUiEvent(data) { |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 280 | scenario.view.alert('UI Tx: ' + data.event + '\n\n' + |
| 281 | JSON.stringify(data)); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | function injectStartupEvents(view) { |
| 285 | var last = scenario.params.lastAuto || 0; |
| 286 | if (abortIfLive()) { return; } |
| 287 | |
| 288 | while (scenario.evNumber < last) { |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 289 | injectTestEvent(view); |
| 290 | } |
| 291 | } |
| 292 | |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 293 | function toggleBg() { |
| 294 | var vis = bgImg.style('visibility'); |
| 295 | bgImg.style('visibility', (vis === 'hidden') ? 'visible' : 'hidden'); |
| 296 | } |
| 297 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 298 | function cycleLabels() { |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 299 | deviceLabelIndex = (deviceLabelIndex === network.deviceLabelCount - 1) |
| 300 | ? 0 : deviceLabelIndex + 1; |
Simon Hunt | 5f36d34 | 2014-11-08 21:33:14 -0800 | [diff] [blame] | 301 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 302 | network.nodes.forEach(function (d) { |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 303 | if (d.class === 'device') { |
| 304 | updateDeviceLabel(d); |
| 305 | } |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 306 | }); |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | function togglePorts(view) { |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 310 | view.alert('togglePorts() callback') |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 311 | } |
| 312 | |
Simon Hunt | 6ac93f3 | 2014-11-13 12:17:27 -0800 | [diff] [blame] | 313 | function unpin() { |
| 314 | if (hovered) { |
| 315 | hovered.fixed = false; |
| 316 | hovered.el.classed('fixed', false); |
| 317 | network.force.resume(); |
| 318 | } |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 319 | } |
| 320 | |
Simon Hunt | 9462e8c | 2014-11-14 17:28:09 -0800 | [diff] [blame] | 321 | function handleEscape(view) { |
| 322 | if (oiShowMaster) { |
| 323 | cancelAffinity(); |
| 324 | } else { |
| 325 | deselectAll(); |
| 326 | } |
| 327 | } |
| 328 | |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 329 | // ============================== |
| 330 | // Radio Button Callbacks |
| 331 | |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 332 | var layerLookup = { |
| 333 | host: { |
| 334 | endstation: 'pkt', // default, if host event does not define type |
| 335 | bgpSpeaker: 'pkt' |
| 336 | }, |
| 337 | device: { |
| 338 | switch: 'pkt', |
| 339 | roadm: 'opt' |
| 340 | }, |
| 341 | link: { |
| 342 | hostLink: 'pkt', |
| 343 | direct: 'pkt', |
Simon Hunt | 8257f4c | 2014-11-16 19:34:54 -0800 | [diff] [blame] | 344 | indirect: '', |
| 345 | tunnel: '', |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 346 | optical: 'opt' |
| 347 | } |
| 348 | }; |
| 349 | |
| 350 | function inLayer(d, layer) { |
Simon Hunt | 8257f4c | 2014-11-16 19:34:54 -0800 | [diff] [blame] | 351 | var type = d.class === 'link' ? d.type() : d.type, |
| 352 | look = layerLookup[d.class], |
| 353 | lyr = look && look[type]; |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 354 | return lyr === layer; |
| 355 | } |
| 356 | |
| 357 | function unsuppressLayer(which) { |
| 358 | node.each(function (d) { |
| 359 | var node = d.el; |
| 360 | if (inLayer(d, which)) { |
| 361 | node.classed('suppressed', false); |
| 362 | } |
| 363 | }); |
| 364 | |
| 365 | link.each(function (d) { |
| 366 | var link = d.el; |
| 367 | if (inLayer(d, which)) { |
| 368 | link.classed('suppressed', false); |
| 369 | } |
| 370 | }); |
| 371 | } |
| 372 | |
Simon Hunt | 9462e8c | 2014-11-14 17:28:09 -0800 | [diff] [blame] | 373 | function suppressLayers(b) { |
| 374 | node.classed('suppressed', b); |
| 375 | link.classed('suppressed', b); |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 376 | // d3.selectAll('svg .port').classed('inactive', false); |
| 377 | // d3.selectAll('svg .portText').classed('inactive', false); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 378 | } |
| 379 | |
Simon Hunt | 9462e8c | 2014-11-14 17:28:09 -0800 | [diff] [blame] | 380 | function showAllLayers() { |
| 381 | suppressLayers(false); |
| 382 | } |
| 383 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 384 | function showPacketLayer() { |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 385 | node.classed('suppressed', true); |
| 386 | link.classed('suppressed', true); |
| 387 | unsuppressLayer('pkt'); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | function showOpticalLayer() { |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 391 | node.classed('suppressed', true); |
| 392 | link.classed('suppressed', true); |
| 393 | unsuppressLayer('opt'); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 394 | } |
| 395 | |
Simon Hunt | 9462e8c | 2014-11-14 17:28:09 -0800 | [diff] [blame] | 396 | function restoreLayerState() { |
| 397 | layerBtnDispatch[layerBtnSet.selected()](); |
| 398 | } |
| 399 | |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 400 | // ============================== |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 401 | // Private functions |
| 402 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 403 | function safeId(s) { |
| 404 | return s.replace(/[^a-z0-9]/gi, '-'); |
| 405 | } |
| 406 | |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 407 | // set the size of the given element to that of the view (reduced if padded) |
| 408 | function setSize(el, view, pad) { |
| 409 | var padding = pad ? pad * 2 : 0; |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 410 | el.attr({ |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 411 | width: view.width() - padding, |
| 412 | height: view.height() - padding |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 413 | }); |
| 414 | } |
| 415 | |
Simon Hunt | 8257f4c | 2014-11-16 19:34:54 -0800 | [diff] [blame] | 416 | function makeNodeKey(d, what) { |
| 417 | var port = what + 'Port'; |
| 418 | return d[what] + '/' + d[port]; |
| 419 | } |
| 420 | |
| 421 | function makeLinkKey(d, flipped) { |
| 422 | var one = flipped ? makeNodeKey(d, 'dst') : makeNodeKey(d, 'src'), |
| 423 | two = flipped ? makeNodeKey(d, 'src') : makeNodeKey(d, 'dst'); |
| 424 | return one + '-' + two; |
| 425 | } |
| 426 | |
Simon Hunt | 269670f | 2014-11-17 16:17:43 -0800 | [diff] [blame] | 427 | function findLinkById(id) { |
| 428 | // check to see if this is a reverse lookup, else default to given id |
| 429 | var key = network.revLinkToKey[id] || id; |
| 430 | return key && network.lookup[key]; |
| 431 | } |
| 432 | |
Simon Hunt | 8257f4c | 2014-11-16 19:34:54 -0800 | [diff] [blame] | 433 | function findLink(linkData, op) { |
| 434 | var key = makeLinkKey(linkData), |
| 435 | keyrev = makeLinkKey(linkData, 1), |
| 436 | link = network.lookup[key], |
| 437 | linkRev = network.lookup[keyrev], |
| 438 | result = {}, |
| 439 | ldata = link || linkRev, |
| 440 | rawLink; |
| 441 | |
| 442 | if (op === 'add') { |
| 443 | if (link) { |
| 444 | // trying to add a link that we already know about |
| 445 | result.ldata = link; |
| 446 | result.badLogic = 'addLink: link already added'; |
| 447 | |
| 448 | } else if (linkRev) { |
| 449 | // we found the reverse of the link to be added |
| 450 | result.ldata = linkRev; |
| 451 | if (linkRev.fromTarget) { |
| 452 | result.badLogic = 'addLink: link already added'; |
| 453 | } |
| 454 | } |
| 455 | } else if (op === 'update') { |
| 456 | if (!ldata) { |
| 457 | result.badLogic = 'updateLink: link not found'; |
| 458 | } else { |
| 459 | rawLink = link ? ldata.fromSource : ldata.fromTarget; |
| 460 | result.updateWith = function (data) { |
| 461 | $.extend(rawLink, data); |
| 462 | restyleLinkElement(ldata); |
| 463 | } |
| 464 | } |
| 465 | } else if (op === 'remove') { |
| 466 | if (!ldata) { |
| 467 | result.badLogic = 'removeLink: link not found'; |
| 468 | } else { |
| 469 | rawLink = link ? ldata.fromSource : ldata.fromTarget; |
| 470 | |
| 471 | if (!rawLink) { |
| 472 | result.badLogic = 'removeLink: link not found'; |
| 473 | |
| 474 | } else { |
| 475 | result.removeRawLink = function () { |
| 476 | if (link) { |
| 477 | // remove fromSource |
| 478 | ldata.fromSource = null; |
| 479 | if (ldata.fromTarget) { |
| 480 | // promote target into source position |
| 481 | ldata.fromSource = ldata.fromTarget; |
| 482 | ldata.fromTarget = null; |
| 483 | ldata.key = keyrev; |
| 484 | delete network.lookup[key]; |
| 485 | network.lookup[keyrev] = ldata; |
Simon Hunt | 269670f | 2014-11-17 16:17:43 -0800 | [diff] [blame] | 486 | delete network.revLinkToKey[keyrev]; |
Simon Hunt | 8257f4c | 2014-11-16 19:34:54 -0800 | [diff] [blame] | 487 | } |
| 488 | } else { |
| 489 | // remove fromTarget |
| 490 | ldata.fromTarget = null; |
Simon Hunt | 269670f | 2014-11-17 16:17:43 -0800 | [diff] [blame] | 491 | delete network.revLinkToKey[keyrev]; |
Simon Hunt | 8257f4c | 2014-11-16 19:34:54 -0800 | [diff] [blame] | 492 | } |
| 493 | if (ldata.fromSource) { |
| 494 | restyleLinkElement(ldata); |
| 495 | } else { |
| 496 | removeLinkElement(ldata); |
| 497 | } |
| 498 | } |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | return result; |
| 503 | } |
| 504 | |
| 505 | function addLinkUpdate(ldata, link) { |
| 506 | // add link event, but we already have the reverse link installed |
| 507 | ldata.fromTarget = link; |
Simon Hunt | 269670f | 2014-11-17 16:17:43 -0800 | [diff] [blame] | 508 | network.revLinkToKey[link.id] = ldata.key; |
Simon Hunt | 8257f4c | 2014-11-16 19:34:54 -0800 | [diff] [blame] | 509 | restyleLinkElement(ldata); |
| 510 | } |
| 511 | |
| 512 | var allLinkTypes = 'direct indirect optical tunnel', |
| 513 | defaultLinkType = 'direct'; |
| 514 | |
| 515 | function restyleLinkElement(ldata) { |
| 516 | // this fn's job is to look at raw links and decide what svg classes |
| 517 | // need to be applied to the line element in the DOM |
| 518 | var el = ldata.el, |
| 519 | type = ldata.type(), |
| 520 | lw = ldata.linkWidth(), |
| 521 | online = ldata.online(); |
| 522 | |
| 523 | el.classed('link', true); |
| 524 | el.classed('inactive', !online); |
| 525 | el.classed(allLinkTypes, false); |
| 526 | if (type) { |
| 527 | el.classed(type, true); |
| 528 | } |
| 529 | el.transition() |
| 530 | .duration(1000) |
| 531 | .attr('stroke-width', linkScale(lw)) |
| 532 | .attr('stroke', '#666'); // TODO: remove explicit stroke (use CSS) |
| 533 | } |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 534 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 535 | // ============================== |
| 536 | // Event handlers for server-pushed events |
| 537 | |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 538 | function logicError(msg) { |
| 539 | // TODO, report logic error to server, via websock, so it can be logged |
Simon Hunt | cb56cff | 2014-11-17 11:42:26 -0800 | [diff] [blame] | 540 | //network.view.alert('Logic Error:\n\n' + msg); |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 541 | console.warn(msg); |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 542 | } |
| 543 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 544 | var eventDispatch = { |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 545 | addInstance: addInstance, |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 546 | addDevice: addDevice, |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 547 | addLink: addLink, |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 548 | addHost: addHost, |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 549 | |
Simon Hunt | d72bc70 | 2014-11-13 18:38:04 -0800 | [diff] [blame] | 550 | updateInstance: stillToImplement, |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 551 | updateDevice: updateDevice, |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 552 | updateLink: updateLink, |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 553 | updateHost: updateHost, |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 554 | |
Simon Hunt | d72bc70 | 2014-11-13 18:38:04 -0800 | [diff] [blame] | 555 | removeInstance: stillToImplement, |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 556 | removeDevice: stillToImplement, |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 557 | removeLink: removeLink, |
Simon Hunt | 4403110 | 2014-11-11 13:20:36 -0800 | [diff] [blame] | 558 | removeHost: removeHost, |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 559 | |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 560 | showDetails: showDetails, |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 561 | showTraffic: showTraffic |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 562 | }; |
| 563 | |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 564 | function addInstance(data) { |
| 565 | evTrace(data); |
| 566 | var inst = data.payload, |
| 567 | id = inst.id; |
| 568 | if (onosInstances[id]) { |
| 569 | logicError('ONOS instance already added: ' + id); |
| 570 | return; |
| 571 | } |
| 572 | onosInstances[id] = inst; |
| 573 | onosOrder.push(inst); |
| 574 | updateInstances(); |
| 575 | } |
| 576 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 577 | function addDevice(data) { |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 578 | evTrace(data); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 579 | var device = data.payload, |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 580 | nodeData = createDeviceNode(device); |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 581 | network.nodes.push(nodeData); |
| 582 | network.lookup[nodeData.id] = nodeData; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 583 | updateNodes(); |
| 584 | network.force.start(); |
| 585 | } |
| 586 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 587 | function addLink(data) { |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 588 | evTrace(data); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 589 | var link = data.payload, |
Simon Hunt | 8257f4c | 2014-11-16 19:34:54 -0800 | [diff] [blame] | 590 | result = findLink(link, 'add'), |
| 591 | bad = result.badLogic, |
| 592 | ldata = result.ldata; |
| 593 | |
| 594 | if (bad) { |
| 595 | logicError(bad + ': ' + link.id); |
| 596 | return; |
| 597 | } |
| 598 | |
| 599 | if (ldata) { |
| 600 | // we already have a backing store link for src/dst nodes |
| 601 | addLinkUpdate(ldata, link); |
| 602 | return; |
| 603 | } |
| 604 | |
| 605 | // no backing store link yet |
| 606 | ldata = createLink(link); |
| 607 | if (ldata) { |
| 608 | network.links.push(ldata); |
| 609 | network.lookup[ldata.key] = ldata; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 610 | updateLinks(); |
| 611 | network.force.start(); |
| 612 | } |
| 613 | } |
| 614 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 615 | function addHost(data) { |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 616 | evTrace(data); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 617 | var host = data.payload, |
| 618 | node = createHostNode(host), |
| 619 | lnk; |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 620 | network.nodes.push(node); |
| 621 | network.lookup[host.id] = node; |
| 622 | updateNodes(); |
| 623 | |
| 624 | lnk = createHostLink(host); |
| 625 | if (lnk) { |
Simon Hunt | 4403110 | 2014-11-11 13:20:36 -0800 | [diff] [blame] | 626 | node.linkData = lnk; // cache ref on its host |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 627 | network.links.push(lnk); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 628 | network.lookup[host.ingress] = lnk; |
| 629 | network.lookup[host.egress] = lnk; |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 630 | updateLinks(); |
| 631 | } |
| 632 | network.force.start(); |
| 633 | } |
| 634 | |
Simon Hunt | 4403110 | 2014-11-11 13:20:36 -0800 | [diff] [blame] | 635 | // TODO: fold updateX(...) methods into one base method; remove duplication |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 636 | function updateDevice(data) { |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 637 | evTrace(data); |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 638 | var device = data.payload, |
| 639 | id = device.id, |
| 640 | nodeData = network.lookup[id]; |
| 641 | if (nodeData) { |
| 642 | $.extend(nodeData, device); |
| 643 | updateDeviceState(nodeData); |
| 644 | } else { |
| 645 | logicError('updateDevice lookup fail. ID = "' + id + '"'); |
| 646 | } |
| 647 | } |
| 648 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 649 | function updateLink(data) { |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 650 | evTrace(data); |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 651 | var link = data.payload, |
Simon Hunt | 8257f4c | 2014-11-16 19:34:54 -0800 | [diff] [blame] | 652 | result = findLink(link, 'update'), |
| 653 | bad = result.badLogic; |
| 654 | if (bad) { |
| 655 | logicError(bad + ': ' + link.id); |
| 656 | return; |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 657 | } |
Simon Hunt | 8257f4c | 2014-11-16 19:34:54 -0800 | [diff] [blame] | 658 | result.updateWith(link); |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 659 | } |
| 660 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 661 | function updateHost(data) { |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 662 | evTrace(data); |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 663 | var host = data.payload, |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 664 | id = host.id, |
| 665 | hostData = network.lookup[id]; |
| 666 | if (hostData) { |
| 667 | $.extend(hostData, host); |
| 668 | updateHostState(hostData); |
| 669 | } else { |
| 670 | logicError('updateHost lookup fail. ID = "' + id + '"'); |
| 671 | } |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 672 | } |
| 673 | |
Simon Hunt | 4403110 | 2014-11-11 13:20:36 -0800 | [diff] [blame] | 674 | // TODO: fold removeX(...) methods into base method - remove dup code |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 675 | function removeLink(data) { |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 676 | evTrace(data); |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 677 | var link = data.payload, |
Simon Hunt | 8257f4c | 2014-11-16 19:34:54 -0800 | [diff] [blame] | 678 | result = findLink(link, 'remove'), |
| 679 | bad = result.badLogic; |
| 680 | if (bad) { |
| 681 | logicError(bad + ': ' + link.id); |
| 682 | return; |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 683 | } |
Simon Hunt | 8257f4c | 2014-11-16 19:34:54 -0800 | [diff] [blame] | 684 | result.removeRawLink(); |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 685 | } |
| 686 | |
Simon Hunt | 4403110 | 2014-11-11 13:20:36 -0800 | [diff] [blame] | 687 | function removeHost(data) { |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 688 | evTrace(data); |
Simon Hunt | 4403110 | 2014-11-11 13:20:36 -0800 | [diff] [blame] | 689 | var host = data.payload, |
| 690 | id = host.id, |
| 691 | hostData = network.lookup[id]; |
| 692 | if (hostData) { |
| 693 | removeHostElement(hostData); |
| 694 | } else { |
| 695 | logicError('removeHost lookup fail. ID = "' + id + '"'); |
| 696 | } |
| 697 | } |
| 698 | |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 699 | function showDetails(data) { |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 700 | evTrace(data); |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 701 | populateDetails(data.payload); |
| 702 | detailPane.show(); |
| 703 | } |
| 704 | |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 705 | function showTraffic(data) { |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 706 | evTrace(data); |
Thomas Vachuska | 3266abf | 2014-11-13 09:28:46 -0800 | [diff] [blame] | 707 | var paths = data.payload.paths; |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 708 | |
Thomas Vachuska | 3266abf | 2014-11-13 09:28:46 -0800 | [diff] [blame] | 709 | // Revert any links hilighted previously. |
Simon Hunt | a255a2c | 2014-11-13 22:29:35 -0800 | [diff] [blame] | 710 | link.classed('primary secondary animated optical', false); |
Thomas Vachuska | 3266abf | 2014-11-13 09:28:46 -0800 | [diff] [blame] | 711 | |
| 712 | // Now hilight all links in the paths payload. |
Simon Hunt | a255a2c | 2014-11-13 22:29:35 -0800 | [diff] [blame] | 713 | paths.forEach(function (p) { |
| 714 | var cls = p.class; |
| 715 | p.links.forEach(function (id) { |
Simon Hunt | 269670f | 2014-11-17 16:17:43 -0800 | [diff] [blame] | 716 | var lnk = findLinkById(id); |
Simon Hunt | a255a2c | 2014-11-13 22:29:35 -0800 | [diff] [blame] | 717 | if (lnk) { |
| 718 | lnk.el.classed(cls, true); |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 719 | } |
| 720 | }); |
| 721 | }); |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 722 | } |
| 723 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 724 | // ............................... |
| 725 | |
| 726 | function stillToImplement(data) { |
| 727 | var p = data.payload; |
| 728 | note(data.event, p.id); |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 729 | network.view.alert('Not yet implemented: "' + data.event + '"'); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 730 | } |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 731 | |
| 732 | function unknownEvent(data) { |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 733 | network.view.alert('Unknown event type: "' + data.event + '"'); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | function handleServerEvent(data) { |
| 737 | var fn = eventDispatch[data.event] || unknownEvent; |
| 738 | fn(data); |
| 739 | } |
| 740 | |
| 741 | // ============================== |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 742 | // Out-going messages... |
| 743 | |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 744 | function userFeedback(msg) { |
| 745 | // for now, use the alert pane as is. Maybe different alert style in |
| 746 | // the future (centered on view; dismiss button?) |
| 747 | network.view.alert(msg); |
| 748 | } |
| 749 | |
| 750 | function nSel() { |
| 751 | return selectOrder.length; |
| 752 | } |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 753 | function getSel(idx) { |
| 754 | return selections[selectOrder[idx]]; |
| 755 | } |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 756 | function getSelId(idx) { |
| 757 | return getSel(idx).obj.id; |
| 758 | } |
| 759 | function allSelectionsClass(cls) { |
| 760 | for (var i=0, n=nSel(); i<n; i++) { |
| 761 | if (getSel(i).obj.class !== cls) { |
| 762 | return false; |
| 763 | } |
| 764 | } |
| 765 | return true; |
| 766 | } |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 767 | |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 768 | // request details for the selected element |
Simon Hunt | d72bc70 | 2014-11-13 18:38:04 -0800 | [diff] [blame] | 769 | // invoked from selection of a single node. |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 770 | function requestDetails() { |
| 771 | var data = getSel(0).obj, |
| 772 | payload = { |
| 773 | id: data.id, |
| 774 | class: data.class |
| 775 | }; |
| 776 | sendMessage('requestDetails', payload); |
| 777 | } |
| 778 | |
Simon Hunt | d72bc70 | 2014-11-13 18:38:04 -0800 | [diff] [blame] | 779 | function addIntentAction() { |
| 780 | sendMessage('addHostIntent', { |
| 781 | one: getSelId(0), |
Thomas Vachuska | 82f2c62 | 2014-11-17 12:23:18 -0800 | [diff] [blame] | 782 | two: getSelId(1), |
| 783 | ids: [ getSelId(0), getSelId(1) ] |
Simon Hunt | d72bc70 | 2014-11-13 18:38:04 -0800 | [diff] [blame] | 784 | }); |
| 785 | } |
| 786 | |
| 787 | function showTrafficAction() { |
| 788 | // if nothing is hovered over, and nothing selected, send cancel request |
| 789 | if (!hovered && nSel() === 0) { |
| 790 | sendMessage('cancelTraffic', {}); |
| 791 | return; |
| 792 | } |
| 793 | |
| 794 | // NOTE: hover is only populated if "show traffic on hover" is |
| 795 | // toggled on, and the item hovered is a host... |
| 796 | var hoverId = (trafficHover() && hovered && hovered.class === 'host') |
| 797 | ? hovered.id : ''; |
| 798 | sendMessage('requestTraffic', { |
| 799 | ids: selectOrder, |
| 800 | hover: hoverId |
| 801 | }); |
| 802 | } |
| 803 | |
| 804 | |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 805 | // ============================== |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 806 | // onos instance panel functions |
| 807 | |
| 808 | function updateInstances() { |
| 809 | var onoses = oiBox.el.selectAll('.onosInst') |
| 810 | .data(onosOrder, function (d) { return d.id; }); |
| 811 | |
| 812 | // operate on existing onoses if necessary |
| 813 | |
| 814 | var entering = onoses.enter() |
| 815 | .append('div') |
| 816 | .attr('class', 'onosInst') |
| 817 | .classed('online', function (d) { return d.online; }) |
Simon Hunt | 9c15eca | 2014-11-15 18:37:59 -0800 | [diff] [blame] | 818 | .on('click', clickInst); |
| 819 | |
| 820 | entering.each(function (d, i) { |
| 821 | var el = d3.select(this), |
| 822 | img; |
| 823 | |
| 824 | $('<img src="img/host.png">').appendTo(el); |
| 825 | img = el.select('img') |
| 826 | .attr({ |
| 827 | width: 40, |
| 828 | top: -10, |
| 829 | left: -10 |
| 830 | }) |
| 831 | .style({ |
| 832 | }); |
| 833 | |
| 834 | $('<div>').attr('class', 'onosTitle').text(d.id).appendTo(el); |
| 835 | |
| 836 | // is the UI attached to this instance? |
| 837 | // TODO: need uiAttached boolean in instance data |
| 838 | //if (d.uiAttached) { |
| 839 | if (i === 0) { |
| 840 | $('<img src="img/ui.png">').attr('class','ui').appendTo(el); |
| 841 | } |
| 842 | }); |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 843 | |
| 844 | // operate on existing + new onoses here |
| 845 | |
| 846 | // the departed... |
| 847 | var exiting = onoses.exit() |
| 848 | .transition() |
| 849 | .style('opacity', 0) |
| 850 | .remove(); |
| 851 | } |
| 852 | |
Simon Hunt | 9462e8c | 2014-11-14 17:28:09 -0800 | [diff] [blame] | 853 | function clickInst(d) { |
| 854 | var el = d3.select(this), |
| 855 | aff = el.classed('affinity'); |
| 856 | if (!aff) { |
| 857 | setAffinity(el, d); |
| 858 | } else { |
| 859 | cancelAffinity(); |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | function setAffinity(el, d) { |
| 864 | d3.selectAll('.onosInst') |
| 865 | .classed('mastership', true) |
| 866 | .classed('affinity', false); |
| 867 | el.classed('affinity', true); |
| 868 | |
| 869 | suppressLayers(true); |
| 870 | node.each(function (n) { |
| 871 | if (n.master === d.id) { |
| 872 | n.el.classed('suppressed', false); |
| 873 | } |
| 874 | }); |
| 875 | oiShowMaster = true; |
| 876 | } |
| 877 | |
| 878 | function cancelAffinity() { |
| 879 | d3.selectAll('.onosInst') |
| 880 | .classed('mastership affinity', false); |
| 881 | restoreLayerState(); |
| 882 | oiShowMaster = false; |
| 883 | } |
| 884 | |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 885 | // ============================== |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 886 | // force layout modification functions |
| 887 | |
| 888 | function translate(x, y) { |
| 889 | return 'translate(' + x + ',' + y + ')'; |
| 890 | } |
| 891 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 892 | function missMsg(what, id) { |
| 893 | return '\n[' + what + '] "' + id + '" missing '; |
| 894 | } |
| 895 | |
| 896 | function linkEndPoints(srcId, dstId) { |
| 897 | var srcNode = network.lookup[srcId], |
| 898 | dstNode = network.lookup[dstId], |
| 899 | sMiss = !srcNode ? missMsg('src', srcId) : '', |
| 900 | dMiss = !dstNode ? missMsg('dst', dstId) : ''; |
| 901 | |
| 902 | if (sMiss || dMiss) { |
| 903 | logicError('Node(s) not on map for link:\n' + sMiss + dMiss); |
| 904 | return null; |
| 905 | } |
| 906 | return { |
| 907 | source: srcNode, |
| 908 | target: dstNode, |
| 909 | x1: srcNode.x, |
| 910 | y1: srcNode.y, |
| 911 | x2: dstNode.x, |
| 912 | y2: dstNode.y |
| 913 | }; |
| 914 | } |
| 915 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 916 | function createHostLink(host) { |
| 917 | var src = host.id, |
| 918 | dst = host.cp.device, |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 919 | id = host.ingress, |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 920 | lnk = linkEndPoints(src, dst); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 921 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 922 | if (!lnk) { |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 923 | return null; |
| 924 | } |
| 925 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 926 | // Synthesize link ... |
| 927 | $.extend(lnk, { |
Simon Hunt | 8257f4c | 2014-11-16 19:34:54 -0800 | [diff] [blame] | 928 | key: id, |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 929 | class: 'link', |
Simon Hunt | 8257f4c | 2014-11-16 19:34:54 -0800 | [diff] [blame] | 930 | |
| 931 | type: function () { return 'hostLink'; }, |
| 932 | // TODO: ideally, we should see if our edge switch is online... |
| 933 | online: function () { return true; }, |
| 934 | linkWidth: function () { return 1; } |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 935 | }); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 936 | return lnk; |
| 937 | } |
| 938 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 939 | function createLink(link) { |
| 940 | var lnk = linkEndPoints(link.src, link.dst), |
| 941 | type = link.type; |
| 942 | |
| 943 | if (!lnk) { |
| 944 | return null; |
| 945 | } |
| 946 | |
Simon Hunt | 8257f4c | 2014-11-16 19:34:54 -0800 | [diff] [blame] | 947 | $.extend(lnk, { |
| 948 | key: link.id, |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 949 | class: 'link', |
Simon Hunt | 8257f4c | 2014-11-16 19:34:54 -0800 | [diff] [blame] | 950 | fromSource: link, |
| 951 | |
| 952 | // functions to aggregate dual link state |
| 953 | type: function () { |
| 954 | var s = lnk.fromSource, |
| 955 | t = lnk.fromTarget; |
| 956 | return (s && s.type) || (t && t.type) || defaultLinkType; |
| 957 | }, |
| 958 | online: function () { |
| 959 | var s = lnk.fromSource, |
| 960 | t = lnk.fromTarget; |
| 961 | return (s && s.online) || (t && t.online); |
| 962 | }, |
| 963 | linkWidth: function () { |
| 964 | var s = lnk.fromSource, |
| 965 | t = lnk.fromTarget, |
| 966 | ws = (s && s.linkWidth) || 0, |
| 967 | wt = (t && t.linkWidth) || 0; |
| 968 | return Math.max(ws, wt); |
| 969 | } |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 970 | }); |
| 971 | return lnk; |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 972 | } |
| 973 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 974 | var widthRatio = 1.4, |
| 975 | linkScale = d3.scale.linear() |
| 976 | .domain([1, 12]) |
| 977 | .range([widthRatio, 12 * widthRatio]) |
| 978 | .clamp(true); |
| 979 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 980 | function updateLinks() { |
| 981 | link = linkG.selectAll('.link') |
Simon Hunt | 8257f4c | 2014-11-16 19:34:54 -0800 | [diff] [blame] | 982 | .data(network.links, function (d) { return d.key; }); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 983 | |
| 984 | // operate on existing links, if necessary |
| 985 | // link .foo() .bar() ... |
| 986 | |
| 987 | // operate on entering links: |
| 988 | var entering = link.enter() |
| 989 | .append('line') |
| 990 | .attr({ |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 991 | x1: function (d) { return d.x1; }, |
| 992 | y1: function (d) { return d.y1; }, |
| 993 | x2: function (d) { return d.x2; }, |
| 994 | y2: function (d) { return d.y2; }, |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 995 | stroke: config.topo.linkInColor, |
| 996 | 'stroke-width': config.topo.linkInWidth |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 997 | }); |
| 998 | |
| 999 | // augment links |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1000 | entering.each(function (d) { |
| 1001 | var link = d3.select(this); |
| 1002 | // provide ref to element selection from backing data.... |
| 1003 | d.el = link; |
Simon Hunt | 8257f4c | 2014-11-16 19:34:54 -0800 | [diff] [blame] | 1004 | restyleLinkElement(d); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1005 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1006 | // TODO: add src/dst port labels etc. |
| 1007 | }); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 1008 | |
| 1009 | // operate on both existing and new links, if necessary |
| 1010 | //link .foo() .bar() ... |
| 1011 | |
| 1012 | // operate on exiting links: |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 1013 | link.exit() |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 1014 | .attr({ |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 1015 | 'stroke-dasharray': '3, 3' |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 1016 | }) |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 1017 | .style('opacity', 0.4) |
| 1018 | .transition() |
Simon Hunt | ea80eb4 | 2014-11-11 13:46:57 -0800 | [diff] [blame] | 1019 | .duration(1500) |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 1020 | .attr({ |
| 1021 | 'stroke-dasharray': '3, 12' |
| 1022 | }) |
| 1023 | .transition() |
Simon Hunt | ea80eb4 | 2014-11-11 13:46:57 -0800 | [diff] [blame] | 1024 | .duration(500) |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 1025 | .style('opacity', 0.0) |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 1026 | .remove(); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1027 | } |
| 1028 | |
| 1029 | function createDeviceNode(device) { |
| 1030 | // start with the object as is |
| 1031 | var node = device, |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1032 | type = device.type, |
| 1033 | svgCls = type ? 'node device ' + type : 'node device'; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1034 | |
| 1035 | // Augment as needed... |
| 1036 | node.class = 'device'; |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1037 | node.svgClass = device.online ? svgCls + ' online' : svgCls; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1038 | positionNode(node); |
| 1039 | |
| 1040 | // cache label array length |
| 1041 | network.deviceLabelCount = device.labels.length; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1042 | return node; |
| 1043 | } |
| 1044 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1045 | function createHostNode(host) { |
| 1046 | // start with the object as is |
| 1047 | var node = host; |
| 1048 | |
| 1049 | // Augment as needed... |
| 1050 | node.class = 'host'; |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1051 | if (!node.type) { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1052 | node.type = 'endstation'; |
| 1053 | } |
Simon Hunt | 7fa116d | 2014-11-17 14:16:55 -0800 | [diff] [blame] | 1054 | node.svgClass = 'node host ' + node.type; |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1055 | positionNode(node); |
| 1056 | |
| 1057 | // cache label array length |
| 1058 | network.hostLabelCount = host.labels.length; |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1059 | return node; |
| 1060 | } |
| 1061 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1062 | function positionNode(node) { |
| 1063 | var meta = node.metaUi, |
Simon Hunt | ac9e24f | 2014-11-12 10:12:21 -0800 | [diff] [blame] | 1064 | x = meta && meta.x, |
| 1065 | y = meta && meta.y, |
| 1066 | xy; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1067 | |
Simon Hunt | ac9e24f | 2014-11-12 10:12:21 -0800 | [diff] [blame] | 1068 | // If we have [x,y] already, use that... |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1069 | if (x && y) { |
| 1070 | node.fixed = true; |
Simon Hunt | ac9e24f | 2014-11-12 10:12:21 -0800 | [diff] [blame] | 1071 | node.x = x; |
| 1072 | node.y = y; |
| 1073 | return; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1074 | } |
Simon Hunt | ac9e24f | 2014-11-12 10:12:21 -0800 | [diff] [blame] | 1075 | |
Paul Greyson | 6cb8ca0 | 2014-11-12 18:09:02 -0800 | [diff] [blame] | 1076 | var location = node.location; |
| 1077 | if (location && location.type === 'latlng') { |
| 1078 | var coord = geoMapProjection([location.lng, location.lat]); |
| 1079 | node.fixed = true; |
| 1080 | node.x = coord[0]; |
| 1081 | node.y = coord[1]; |
| 1082 | return; |
| 1083 | } |
| 1084 | |
Simon Hunt | ac9e24f | 2014-11-12 10:12:21 -0800 | [diff] [blame] | 1085 | // Note: Placing incoming unpinned nodes at exactly the same point |
| 1086 | // (center of the view) causes them to explode outwards when |
| 1087 | // the force layout kicks in. So, we spread them out a bit |
| 1088 | // initially, to provide a more serene layout convergence. |
| 1089 | // Additionally, if the node is a host, we place it near |
| 1090 | // the device it is connected to. |
| 1091 | |
| 1092 | function spread(s) { |
| 1093 | return Math.floor((Math.random() * s) - s/2); |
| 1094 | } |
| 1095 | |
| 1096 | function randDim(dim) { |
| 1097 | return dim / 2 + spread(dim * 0.7071); |
| 1098 | } |
| 1099 | |
| 1100 | function rand() { |
| 1101 | return { |
| 1102 | x: randDim(network.view.width()), |
| 1103 | y: randDim(network.view.height()) |
| 1104 | }; |
| 1105 | } |
| 1106 | |
| 1107 | function near(node) { |
| 1108 | var min = 12, |
| 1109 | dx = spread(12), |
| 1110 | dy = spread(12); |
| 1111 | return { |
| 1112 | x: node.x + min + dx, |
| 1113 | y: node.y + min + dy |
| 1114 | }; |
| 1115 | } |
| 1116 | |
| 1117 | function getDevice(cp) { |
| 1118 | var d = network.lookup[cp.device]; |
| 1119 | return d || rand(); |
| 1120 | } |
| 1121 | |
| 1122 | xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand(); |
| 1123 | $.extend(node, xy); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1124 | } |
| 1125 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1126 | function iconUrl(d) { |
| 1127 | return 'img/' + d.type + '.png'; |
| 1128 | } |
| 1129 | |
| 1130 | // returns the newly computed bounding box of the rectangle |
| 1131 | function adjustRectToFitText(n) { |
| 1132 | var text = n.select('text'), |
| 1133 | box = text.node().getBBox(), |
| 1134 | lab = config.labels; |
| 1135 | |
| 1136 | text.attr('text-anchor', 'middle') |
| 1137 | .attr('y', '-0.8em') |
| 1138 | .attr('x', lab.imgPad/2); |
| 1139 | |
| 1140 | // translate the bbox so that it is centered on [x,y] |
| 1141 | box.x = -box.width / 2; |
| 1142 | box.y = -box.height / 2; |
| 1143 | |
| 1144 | // add padding |
| 1145 | box.x -= (lab.padLR + lab.imgPad/2); |
| 1146 | box.width += lab.padLR * 2 + lab.imgPad; |
| 1147 | box.y -= lab.padTB; |
| 1148 | box.height += lab.padTB * 2; |
| 1149 | |
| 1150 | return box; |
| 1151 | } |
| 1152 | |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 1153 | function mkSvgClass(d) { |
| 1154 | return d.fixed ? d.svgClass + ' fixed' : d.svgClass; |
| 1155 | } |
| 1156 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1157 | function hostLabel(d) { |
| 1158 | var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0; |
| 1159 | return d.labels[idx]; |
| 1160 | } |
| 1161 | function deviceLabel(d) { |
| 1162 | var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0; |
| 1163 | return d.labels[idx]; |
| 1164 | } |
| 1165 | function niceLabel(label) { |
| 1166 | return (label && label.trim()) ? label : '.'; |
| 1167 | } |
| 1168 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 1169 | function updateDeviceLabel(d) { |
| 1170 | var label = niceLabel(deviceLabel(d)), |
| 1171 | node = d.el, |
| 1172 | box; |
| 1173 | |
| 1174 | node.select('text') |
| 1175 | .text(label) |
| 1176 | .style('opacity', 0) |
| 1177 | .transition() |
| 1178 | .style('opacity', 1); |
| 1179 | |
| 1180 | box = adjustRectToFitText(node); |
| 1181 | |
| 1182 | node.select('rect') |
| 1183 | .transition() |
| 1184 | .attr(box); |
| 1185 | |
| 1186 | node.select('image') |
| 1187 | .transition() |
| 1188 | .attr('x', box.x + config.icons.xoff) |
| 1189 | .attr('y', box.y + config.icons.yoff); |
| 1190 | } |
| 1191 | |
| 1192 | function updateHostLabel(d) { |
| 1193 | var label = hostLabel(d), |
| 1194 | host = d.el; |
| 1195 | |
| 1196 | host.select('text').text(label); |
| 1197 | } |
| 1198 | |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1199 | function updateDeviceState(nodeData) { |
| 1200 | nodeData.el.classed('online', nodeData.online); |
| 1201 | updateDeviceLabel(nodeData); |
| 1202 | // TODO: review what else might need to be updated |
| 1203 | } |
| 1204 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 1205 | function updateLinkState(linkData) { |
| 1206 | updateLinkWidth(linkData); |
Thomas Vachuska | badb93f | 2014-11-15 23:51:17 -0800 | [diff] [blame] | 1207 | linkData.el.classed('inactive', !linkData.online); |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 1208 | // TODO: review what else might need to be updated |
| 1209 | // update label, if showing |
| 1210 | } |
| 1211 | |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1212 | function updateHostState(hostData) { |
| 1213 | updateHostLabel(hostData); |
| 1214 | // TODO: review what else might need to be updated |
| 1215 | } |
| 1216 | |
Simon Hunt | 6ac93f3 | 2014-11-13 12:17:27 -0800 | [diff] [blame] | 1217 | function nodeMouseOver(d) { |
Simon Hunt | 6ac93f3 | 2014-11-13 12:17:27 -0800 | [diff] [blame] | 1218 | hovered = d; |
Simon Hunt | d72bc70 | 2014-11-13 18:38:04 -0800 | [diff] [blame] | 1219 | if (trafficHover() && d.class === 'host') { |
| 1220 | showTrafficAction(); |
Simon Hunt | 6ac93f3 | 2014-11-13 12:17:27 -0800 | [diff] [blame] | 1221 | } |
| 1222 | } |
| 1223 | |
| 1224 | function nodeMouseOut(d) { |
Simon Hunt | 6ac93f3 | 2014-11-13 12:17:27 -0800 | [diff] [blame] | 1225 | hovered = null; |
Simon Hunt | d72bc70 | 2014-11-13 18:38:04 -0800 | [diff] [blame] | 1226 | if (trafficHover() && d.class === 'host') { |
| 1227 | showTrafficAction(); |
Simon Hunt | 6ac93f3 | 2014-11-13 12:17:27 -0800 | [diff] [blame] | 1228 | } |
| 1229 | } |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1230 | |
Simon Hunt | 7fa116d | 2014-11-17 14:16:55 -0800 | [diff] [blame] | 1231 | function addHostIcon(node, radius, iconId) { |
| 1232 | var dim = radius * 1.5, |
| 1233 | xlate = -dim / 2; |
| 1234 | |
| 1235 | node.append('use') |
| 1236 | .classed('glyph', true) |
| 1237 | .attr('transform', translate(xlate,xlate)) |
| 1238 | .attr('xlink:href', '#' + iconId) |
| 1239 | .attr('width', dim) |
| 1240 | .attr('height', dim); |
| 1241 | } |
| 1242 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1243 | function updateNodes() { |
| 1244 | node = nodeG.selectAll('.node') |
| 1245 | .data(network.nodes, function (d) { return d.id; }); |
| 1246 | |
| 1247 | // operate on existing nodes, if necessary |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1248 | // update host labels |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1249 | //node .foo() .bar() ... |
| 1250 | |
| 1251 | // operate on entering nodes: |
| 1252 | var entering = node.enter() |
| 1253 | .append('g') |
| 1254 | .attr({ |
| 1255 | id: function (d) { return safeId(d.id); }, |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 1256 | class: mkSvgClass, |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1257 | transform: function (d) { return translate(d.x, d.y); }, |
| 1258 | opacity: 0 |
| 1259 | }) |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 1260 | .call(network.drag) |
Simon Hunt | 6ac93f3 | 2014-11-13 12:17:27 -0800 | [diff] [blame] | 1261 | .on('mouseover', nodeMouseOver) |
| 1262 | .on('mouseout', nodeMouseOut) |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1263 | .transition() |
| 1264 | .attr('opacity', 1); |
| 1265 | |
| 1266 | // augment device nodes... |
| 1267 | entering.filter('.device').each(function (d) { |
| 1268 | var node = d3.select(this), |
| 1269 | icon = iconUrl(d), |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1270 | label = niceLabel(deviceLabel(d)), |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1271 | box; |
| 1272 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1273 | // provide ref to element from backing data.... |
| 1274 | d.el = node; |
| 1275 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1276 | node.append('rect') |
| 1277 | .attr({ |
| 1278 | 'rx': 5, |
| 1279 | 'ry': 5 |
| 1280 | }); |
| 1281 | |
| 1282 | node.append('text') |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1283 | .text(label) |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1284 | .attr('dy', '1.1em'); |
| 1285 | |
| 1286 | box = adjustRectToFitText(node); |
| 1287 | |
| 1288 | node.select('rect') |
| 1289 | .attr(box); |
| 1290 | |
| 1291 | if (icon) { |
| 1292 | var cfg = config.icons; |
| 1293 | node.append('svg:image') |
| 1294 | .attr({ |
| 1295 | x: box.x + config.icons.xoff, |
| 1296 | y: box.y + config.icons.yoff, |
| 1297 | width: cfg.w, |
| 1298 | height: cfg.h, |
| 1299 | 'xlink:href': icon |
| 1300 | }); |
| 1301 | } |
| 1302 | |
| 1303 | // debug function to show the modelled x,y coordinates of nodes... |
| 1304 | if (debug('showNodeXY')) { |
| 1305 | node.select('rect').attr('fill-opacity', 0.5); |
| 1306 | node.append('circle') |
| 1307 | .attr({ |
| 1308 | class: 'debug', |
| 1309 | cx: 0, |
| 1310 | cy: 0, |
| 1311 | r: '3px' |
| 1312 | }); |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1313 | } |
| 1314 | }); |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 1315 | |
Simon Hunt | 7fa116d | 2014-11-17 14:16:55 -0800 | [diff] [blame] | 1316 | // TODO: better place for this configuration state |
| 1317 | var defaultHostRadius = 9, |
| 1318 | hostRadius = { |
| 1319 | bgpSpeaker: 20 |
| 1320 | }, |
| 1321 | hostIcon = { |
| 1322 | bgpSpeaker: 'bullhorn' |
| 1323 | }; |
| 1324 | |
| 1325 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1326 | // augment host nodes... |
| 1327 | entering.filter('.host').each(function (d) { |
| 1328 | var node = d3.select(this), |
Simon Hunt | 7fa116d | 2014-11-17 14:16:55 -0800 | [diff] [blame] | 1329 | r = hostRadius[d.type] || defaultHostRadius, |
| 1330 | textDy = r + 10, |
| 1331 | icon = hostIcon[d.type]; |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1332 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1333 | // provide ref to element from backing data.... |
| 1334 | d.el = node; |
| 1335 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1336 | node.append('circle') |
Simon Hunt | 7fa116d | 2014-11-17 14:16:55 -0800 | [diff] [blame] | 1337 | .attr('r', r); |
| 1338 | |
| 1339 | if (icon) { |
| 1340 | addHostIcon(node, r, icon); |
| 1341 | } |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1342 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1343 | node.append('text') |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1344 | .text(hostLabel) |
Simon Hunt | 7fa116d | 2014-11-17 14:16:55 -0800 | [diff] [blame] | 1345 | .attr('dy', textDy) |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1346 | .attr('text-anchor', 'middle'); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1347 | |
| 1348 | // debug function to show the modelled x,y coordinates of nodes... |
| 1349 | if (debug('showNodeXY')) { |
| 1350 | node.select('circle').attr('fill-opacity', 0.5); |
| 1351 | node.append('circle') |
| 1352 | .attr({ |
| 1353 | class: 'debug', |
| 1354 | cx: 0, |
| 1355 | cy: 0, |
| 1356 | r: '3px' |
| 1357 | }); |
| 1358 | } |
| 1359 | }); |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1360 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1361 | // operate on both existing and new nodes, if necessary |
| 1362 | //node .foo() .bar() ... |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1363 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1364 | // operate on exiting nodes: |
Simon Hunt | ea80eb4 | 2014-11-11 13:46:57 -0800 | [diff] [blame] | 1365 | // Note that the node is removed after 2 seconds. |
| 1366 | // Sub element animations should be shorter than 2 seconds. |
| 1367 | var exiting = node.exit() |
Simon Hunt | 4403110 | 2014-11-11 13:20:36 -0800 | [diff] [blame] | 1368 | .transition() |
| 1369 | .duration(2000) |
Simon Hunt | ea80eb4 | 2014-11-11 13:46:57 -0800 | [diff] [blame] | 1370 | .style('opacity', 0) |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1371 | .remove(); |
Simon Hunt | ea80eb4 | 2014-11-11 13:46:57 -0800 | [diff] [blame] | 1372 | |
| 1373 | // host node exits.... |
| 1374 | exiting.filter('.host').each(function (d) { |
| 1375 | var node = d3.select(this); |
| 1376 | |
| 1377 | node.select('text') |
| 1378 | .style('opacity', 0.5) |
| 1379 | .transition() |
| 1380 | .duration(1000) |
| 1381 | .style('opacity', 0); |
| 1382 | // note, leave <g>.remove to remove this element |
| 1383 | |
| 1384 | node.select('circle') |
| 1385 | .style('stroke-fill', '#555') |
| 1386 | .style('fill', '#888') |
| 1387 | .style('opacity', 0.5) |
| 1388 | .transition() |
| 1389 | .duration(1500) |
| 1390 | .attr('r', 0); |
| 1391 | // note, leave <g>.remove to remove this element |
| 1392 | |
| 1393 | }); |
| 1394 | |
| 1395 | // TODO: device node exits |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1396 | } |
| 1397 | |
Simon Hunt | 8257f4c | 2014-11-16 19:34:54 -0800 | [diff] [blame] | 1398 | function find(key, array) { |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 1399 | for (var idx = 0, n = array.length; idx < n; idx++) { |
Simon Hunt | 8257f4c | 2014-11-16 19:34:54 -0800 | [diff] [blame] | 1400 | if (array[idx].key === key) { |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 1401 | return idx; |
| 1402 | } |
| 1403 | } |
| 1404 | return -1; |
| 1405 | } |
| 1406 | |
| 1407 | function removeLinkElement(linkData) { |
Simon Hunt | 8257f4c | 2014-11-16 19:34:54 -0800 | [diff] [blame] | 1408 | var idx = find(linkData.key, network.links), |
| 1409 | removed; |
| 1410 | if (idx >=0) { |
| 1411 | // remove from links array |
| 1412 | removed = network.links.splice(idx, 1); |
| 1413 | // remove from lookup cache |
| 1414 | delete network.lookup[removed[0].key]; |
| 1415 | updateLinks(); |
| 1416 | network.force.resume(); |
| 1417 | } |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 1418 | } |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1419 | |
Simon Hunt | 4403110 | 2014-11-11 13:20:36 -0800 | [diff] [blame] | 1420 | function removeHostElement(hostData) { |
| 1421 | // first, remove associated hostLink... |
| 1422 | removeLinkElement(hostData.linkData); |
| 1423 | |
| 1424 | // remove from lookup cache |
| 1425 | delete network.lookup[hostData.id]; |
| 1426 | // remove from nodes array |
| 1427 | var idx = find(hostData.id, network.nodes); |
| 1428 | network.nodes.splice(idx, 1); |
| 1429 | // remove from SVG |
| 1430 | updateNodes(); |
| 1431 | network.force.resume(); |
| 1432 | } |
| 1433 | |
| 1434 | |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1435 | function tick() { |
| 1436 | node.attr({ |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1437 | transform: function (d) { return translate(d.x, d.y); } |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1438 | }); |
| 1439 | |
| 1440 | link.attr({ |
| 1441 | x1: function (d) { return d.source.x; }, |
| 1442 | y1: function (d) { return d.source.y; }, |
| 1443 | x2: function (d) { return d.target.x; }, |
| 1444 | y2: function (d) { return d.target.y; } |
| 1445 | }); |
| 1446 | } |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 1447 | |
| 1448 | // ============================== |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 1449 | // Web-Socket for live data |
| 1450 | |
| 1451 | function webSockUrl() { |
| 1452 | return document.location.toString() |
| 1453 | .replace(/\#.*/, '') |
| 1454 | .replace('http://', 'ws://') |
| 1455 | .replace('https://', 'wss://') |
| 1456 | .replace('index2.html', config.webSockUrl); |
| 1457 | } |
| 1458 | |
| 1459 | webSock = { |
| 1460 | ws : null, |
| 1461 | |
| 1462 | connect : function() { |
| 1463 | webSock.ws = new WebSocket(webSockUrl()); |
| 1464 | |
| 1465 | webSock.ws.onopen = function() { |
Simon Hunt | 0c6d419 | 2014-11-12 12:07:10 -0800 | [diff] [blame] | 1466 | noWebSock(false); |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 1467 | }; |
| 1468 | |
| 1469 | webSock.ws.onmessage = function(m) { |
| 1470 | if (m.data) { |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1471 | wsTraceRx(m.data); |
Thomas Vachuska | d472c6e | 2014-11-07 19:11:05 -0800 | [diff] [blame] | 1472 | handleServerEvent(JSON.parse(m.data)); |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 1473 | } |
| 1474 | }; |
| 1475 | |
| 1476 | webSock.ws.onclose = function(m) { |
| 1477 | webSock.ws = null; |
Simon Hunt | 0c6d419 | 2014-11-12 12:07:10 -0800 | [diff] [blame] | 1478 | noWebSock(true); |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 1479 | }; |
| 1480 | }, |
| 1481 | |
| 1482 | send : function(text) { |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1483 | if (text != null) { |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 1484 | webSock._send(text); |
| 1485 | } |
| 1486 | }, |
| 1487 | |
| 1488 | _send : function(message) { |
| 1489 | if (webSock.ws) { |
| 1490 | webSock.ws.send(message); |
Simon Hunt | a255a2c | 2014-11-13 22:29:35 -0800 | [diff] [blame] | 1491 | } else if (config.useLiveData) { |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1492 | network.view.alert('no web socket open\n\n' + message); |
Simon Hunt | a255a2c | 2014-11-13 22:29:35 -0800 | [diff] [blame] | 1493 | } else { |
| 1494 | console.log('WS Send: ' + JSON.stringify(message)); |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 1495 | } |
| 1496 | } |
| 1497 | |
| 1498 | }; |
| 1499 | |
Simon Hunt | 0c6d419 | 2014-11-12 12:07:10 -0800 | [diff] [blame] | 1500 | function noWebSock(b) { |
| 1501 | mask.style('display',b ? 'block' : 'none'); |
| 1502 | } |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1503 | |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1504 | // TODO: use cache of pending messages (key = sid) to reconcile responses |
| 1505 | |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1506 | function sendMessage(evType, payload) { |
| 1507 | var toSend = { |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1508 | event: evType, |
| 1509 | sid: ++sid, |
| 1510 | payload: payload |
| 1511 | }, |
| 1512 | asText = JSON.stringify(toSend); |
| 1513 | wsTraceTx(asText); |
| 1514 | webSock.send(asText); |
| 1515 | } |
| 1516 | |
| 1517 | function wsTraceTx(msg) { |
| 1518 | wsTrace('tx', msg); |
| 1519 | } |
| 1520 | function wsTraceRx(msg) { |
| 1521 | wsTrace('rx', msg); |
| 1522 | } |
| 1523 | function wsTrace(rxtx, msg) { |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1524 | console.log('[' + rxtx + '] ' + msg); |
| 1525 | // TODO: integrate with trace view |
| 1526 | //if (trace) { |
| 1527 | // trace.output(rxtx, msg); |
| 1528 | //} |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1529 | } |
| 1530 | |
| 1531 | |
| 1532 | // ============================== |
| 1533 | // Selection stuff |
| 1534 | |
| 1535 | function selectObject(obj, el) { |
| 1536 | var n, |
Simon Hunt | 01095ff | 2014-11-13 16:37:29 -0800 | [diff] [blame] | 1537 | srcEv = d3.event.sourceEvent, |
| 1538 | meta = srcEv.metaKey, |
| 1539 | shift = srcEv.shiftKey; |
| 1540 | |
Simon Hunt | deab432 | 2014-11-13 18:49:07 -0800 | [diff] [blame] | 1541 | if ((panZoom() && !meta) || (!panZoom() && meta)) { |
Simon Hunt | 01095ff | 2014-11-13 16:37:29 -0800 | [diff] [blame] | 1542 | return; |
| 1543 | } |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1544 | |
| 1545 | if (el) { |
| 1546 | n = d3.select(el); |
| 1547 | } else { |
| 1548 | node.each(function(d) { |
| 1549 | if (d == obj) { |
| 1550 | n = d3.select(el = this); |
| 1551 | } |
| 1552 | }); |
| 1553 | } |
| 1554 | if (!n) return; |
| 1555 | |
Simon Hunt | 01095ff | 2014-11-13 16:37:29 -0800 | [diff] [blame] | 1556 | if (shift && n.classed('selected')) { |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1557 | deselectObject(obj.id); |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1558 | updateDetailPane(); |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1559 | return; |
| 1560 | } |
| 1561 | |
Simon Hunt | 01095ff | 2014-11-13 16:37:29 -0800 | [diff] [blame] | 1562 | if (!shift) { |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1563 | deselectAll(); |
| 1564 | } |
| 1565 | |
Simon Hunt | c31d569 | 2014-11-12 13:27:18 -0800 | [diff] [blame] | 1566 | selections[obj.id] = { obj: obj, el: el }; |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1567 | selectOrder.push(obj.id); |
| 1568 | |
| 1569 | n.classed('selected', true); |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1570 | updateDetailPane(); |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1571 | } |
| 1572 | |
| 1573 | function deselectObject(id) { |
Simon Hunt | c31d569 | 2014-11-12 13:27:18 -0800 | [diff] [blame] | 1574 | var obj = selections[id], |
| 1575 | idx; |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1576 | if (obj) { |
| 1577 | d3.select(obj.el).classed('selected', false); |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1578 | delete selections[id]; |
Simon Hunt | c31d569 | 2014-11-12 13:27:18 -0800 | [diff] [blame] | 1579 | idx = $.inArray(id, selectOrder); |
| 1580 | if (idx >= 0) { |
| 1581 | selectOrder.splice(idx, 1); |
| 1582 | } |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1583 | } |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1584 | } |
| 1585 | |
| 1586 | function deselectAll() { |
| 1587 | // deselect all nodes in the network... |
| 1588 | node.classed('selected', false); |
| 1589 | selections = {}; |
| 1590 | selectOrder = []; |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1591 | updateDetailPane(); |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1592 | } |
| 1593 | |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1594 | // update the state of the detail pane, based on current selections |
| 1595 | function updateDetailPane() { |
| 1596 | var nSel = selectOrder.length; |
| 1597 | if (!nSel) { |
| 1598 | detailPane.hide(); |
Simon Hunt | d72bc70 | 2014-11-13 18:38:04 -0800 | [diff] [blame] | 1599 | showTrafficAction(); // sends cancelTraffic event |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1600 | } else if (nSel === 1) { |
| 1601 | singleSelect(); |
| 1602 | } else { |
| 1603 | multiSelect(); |
| 1604 | } |
| 1605 | } |
| 1606 | |
| 1607 | function singleSelect() { |
| 1608 | requestDetails(); |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 1609 | // NOTE: detail pane will be shown from showDetails event callback |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1610 | } |
| 1611 | |
| 1612 | function multiSelect() { |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 1613 | populateMultiSelect(); |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 1614 | } |
| 1615 | |
| 1616 | function addSep(tbody) { |
| 1617 | var tr = tbody.append('tr'); |
| 1618 | $('<hr>').appendTo(tr.append('td').attr('colspan', 2)); |
| 1619 | } |
| 1620 | |
| 1621 | function addProp(tbody, label, value) { |
| 1622 | var tr = tbody.append('tr'); |
| 1623 | |
| 1624 | tr.append('td') |
| 1625 | .attr('class', 'label') |
| 1626 | .text(label + ' :'); |
| 1627 | |
| 1628 | tr.append('td') |
| 1629 | .attr('class', 'value') |
| 1630 | .text(value); |
| 1631 | } |
| 1632 | |
| 1633 | function populateMultiSelect() { |
| 1634 | detailPane.empty(); |
| 1635 | |
| 1636 | var title = detailPane.append("h2"), |
| 1637 | table = detailPane.append("table"), |
| 1638 | tbody = table.append("tbody"); |
| 1639 | |
| 1640 | title.text('Multi-Select...'); |
| 1641 | |
| 1642 | selectOrder.forEach(function (d, i) { |
| 1643 | addProp(tbody, i+1, d); |
| 1644 | }); |
Simon Hunt | d72bc70 | 2014-11-13 18:38:04 -0800 | [diff] [blame] | 1645 | |
| 1646 | addMultiSelectActions(); |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1647 | } |
| 1648 | |
| 1649 | function populateDetails(data) { |
| 1650 | detailPane.empty(); |
| 1651 | |
| 1652 | var title = detailPane.append("h2"), |
| 1653 | table = detailPane.append("table"), |
| 1654 | tbody = table.append("tbody"); |
| 1655 | |
| 1656 | $('<img src="img/' + data.type + '.png">').appendTo(title); |
| 1657 | $('<span>').attr('class', 'icon').text(data.id).appendTo(title); |
| 1658 | |
| 1659 | data.propOrder.forEach(function(p) { |
| 1660 | if (p === '-') { |
| 1661 | addSep(tbody); |
| 1662 | } else { |
| 1663 | addProp(tbody, p, data.props[p]); |
| 1664 | } |
| 1665 | }); |
Simon Hunt | d72bc70 | 2014-11-13 18:38:04 -0800 | [diff] [blame] | 1666 | |
| 1667 | addSingleSelectActions(); |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1668 | } |
| 1669 | |
Simon Hunt | d72bc70 | 2014-11-13 18:38:04 -0800 | [diff] [blame] | 1670 | function addSingleSelectActions() { |
| 1671 | detailPane.append('hr'); |
| 1672 | // always want to allow 'show traffic' |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 1673 | addAction(detailPane, 'Show Traffic', showTrafficAction); |
Simon Hunt | d72bc70 | 2014-11-13 18:38:04 -0800 | [diff] [blame] | 1674 | } |
| 1675 | |
| 1676 | function addMultiSelectActions() { |
| 1677 | detailPane.append('hr'); |
| 1678 | // always want to allow 'show traffic' |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 1679 | addAction(detailPane, 'Show Traffic', showTrafficAction); |
Simon Hunt | d72bc70 | 2014-11-13 18:38:04 -0800 | [diff] [blame] | 1680 | // if exactly two hosts are selected, also want 'add host intent' |
| 1681 | if (nSel() === 2 && allSelectionsClass('host')) { |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 1682 | addAction(detailPane, 'Add Host Intent', addIntentAction); |
Simon Hunt | d72bc70 | 2014-11-13 18:38:04 -0800 | [diff] [blame] | 1683 | } |
| 1684 | } |
| 1685 | |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 1686 | function addAction(panel, text, cb) { |
| 1687 | panel.append('div') |
Simon Hunt | d72bc70 | 2014-11-13 18:38:04 -0800 | [diff] [blame] | 1688 | .classed('actionBtn', true) |
| 1689 | .text(text) |
| 1690 | .on('click', cb); |
| 1691 | } |
| 1692 | |
| 1693 | |
Paul Greyson | fcba0e8 | 2014-11-13 10:21:16 -0800 | [diff] [blame] | 1694 | function zoomPan(scale, translate) { |
| 1695 | zoomPanContainer.attr("transform", "translate(" + translate + ")scale(" + scale + ")"); |
| 1696 | // keep the map lines constant width while zooming |
| 1697 | bgImg.style("stroke-width", 2.0 / scale + "px"); |
| 1698 | } |
| 1699 | |
| 1700 | function resetZoomPan() { |
| 1701 | zoomPan(1, [0,0]); |
| 1702 | zoom.scale(1).translate([0,0]); |
| 1703 | } |
| 1704 | |
| 1705 | function setupZoomPan() { |
| 1706 | function zoomed() { |
Simon Hunt | deab432 | 2014-11-13 18:49:07 -0800 | [diff] [blame] | 1707 | if (!panZoom() ^ !d3.event.sourceEvent.metaKey) { |
Paul Greyson | fcba0e8 | 2014-11-13 10:21:16 -0800 | [diff] [blame] | 1708 | zoomPan(d3.event.scale, d3.event.translate); |
| 1709 | } |
| 1710 | } |
| 1711 | |
| 1712 | zoom = d3.behavior.zoom() |
| 1713 | .translate([0, 0]) |
| 1714 | .scale(1) |
| 1715 | .scaleExtent([1, 8]) |
| 1716 | .on("zoom", zoomed); |
| 1717 | |
| 1718 | svg.call(zoom); |
| 1719 | } |
| 1720 | |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1721 | // ============================== |
| 1722 | // Test harness code |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1723 | |
| 1724 | function prepareScenario(view, ctx, dbg) { |
| 1725 | var sc = scenario, |
| 1726 | urlSc = sc.evDir + ctx + sc.evScenario; |
| 1727 | |
| 1728 | if (!ctx) { |
| 1729 | view.alert("No scenario specified (null ctx)"); |
| 1730 | return; |
| 1731 | } |
| 1732 | |
| 1733 | sc.view = view; |
| 1734 | sc.ctx = ctx; |
| 1735 | sc.debug = dbg; |
| 1736 | sc.evNumber = 0; |
| 1737 | |
| 1738 | d3.json(urlSc, function(err, data) { |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1739 | var p = data && data.params || {}, |
| 1740 | desc = data && data.description || null, |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 1741 | intro = data && data.title; |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1742 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1743 | if (err) { |
| 1744 | view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err); |
| 1745 | } else { |
| 1746 | sc.params = p; |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1747 | if (desc) { |
| 1748 | intro += '\n\n ' + desc.join('\n '); |
| 1749 | } |
| 1750 | view.alert(intro); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1751 | } |
| 1752 | }); |
| 1753 | |
| 1754 | } |
| 1755 | |
Simon Hunt | 01095ff | 2014-11-13 16:37:29 -0800 | [diff] [blame] | 1756 | // ============================== |
| 1757 | // Toggle Buttons in masthead |
Simon Hunt | 0c6d419 | 2014-11-12 12:07:10 -0800 | [diff] [blame] | 1758 | |
Simon Hunt | f8e5b4e0 | 2014-11-13 11:17:57 -0800 | [diff] [blame] | 1759 | // TODO: toggle button (and other widgets in the masthead) should be provided |
| 1760 | // by the framework; not generated by the view. |
| 1761 | |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 1762 | var showInstances, |
| 1763 | doPanZoom, |
| 1764 | showTrafficOnHover; |
Simon Hunt | f8e5b4e0 | 2014-11-13 11:17:57 -0800 | [diff] [blame] | 1765 | |
| 1766 | function addButtonBar(view) { |
| 1767 | var bb = d3.select('#mast') |
| 1768 | .append('span').classed('right', true).attr('id', 'bb'); |
| 1769 | |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 1770 | function mkTogBtn(text, cb) { |
| 1771 | return bb.append('span') |
| 1772 | .classed('btn', true) |
| 1773 | .text(text) |
| 1774 | .on('click', cb); |
| 1775 | } |
Simon Hunt | 01095ff | 2014-11-13 16:37:29 -0800 | [diff] [blame] | 1776 | |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 1777 | showInstances = mkTogBtn('Show Instances', toggleInst); |
| 1778 | doPanZoom = mkTogBtn('Pan/Zoom', togglePanZoom); |
| 1779 | showTrafficOnHover = mkTogBtn('Show traffic on hover', toggleTrafficHover); |
Simon Hunt | f8e5b4e0 | 2014-11-13 11:17:57 -0800 | [diff] [blame] | 1780 | } |
| 1781 | |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 1782 | function instShown() { |
| 1783 | return showInstances.classed('active'); |
Simon Hunt | f8e5b4e0 | 2014-11-13 11:17:57 -0800 | [diff] [blame] | 1784 | } |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 1785 | function toggleInst() { |
| 1786 | showInstances.classed('active', !instShown()); |
| 1787 | if (instShown()) { |
| 1788 | oiBox.show(); |
| 1789 | } else { |
| 1790 | oiBox.hide(); |
| 1791 | } |
Simon Hunt | 01095ff | 2014-11-13 16:37:29 -0800 | [diff] [blame] | 1792 | } |
| 1793 | |
Simon Hunt | deab432 | 2014-11-13 18:49:07 -0800 | [diff] [blame] | 1794 | function panZoom() { |
| 1795 | return doPanZoom.classed('active'); |
Simon Hunt | 01095ff | 2014-11-13 16:37:29 -0800 | [diff] [blame] | 1796 | } |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 1797 | function togglePanZoom() { |
| 1798 | doPanZoom.classed('active', !panZoom()); |
| 1799 | } |
| 1800 | |
| 1801 | function trafficHover() { |
| 1802 | return showTrafficOnHover.classed('active'); |
| 1803 | } |
| 1804 | function toggleTrafficHover() { |
| 1805 | showTrafficOnHover.classed('active', !trafficHover()); |
| 1806 | } |
| 1807 | |
Simon Hunt | 7fa116d | 2014-11-17 14:16:55 -0800 | [diff] [blame] | 1808 | function loadGlyphs(svg) { |
| 1809 | var defs = svg.append('defs'); |
| 1810 | gly.defBird(defs); |
| 1811 | gly.defBullhorn(defs); |
| 1812 | } |
Simon Hunt | 01095ff | 2014-11-13 16:37:29 -0800 | [diff] [blame] | 1813 | |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 1814 | // ============================== |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 1815 | // View life-cycle callbacks |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1816 | |
Simon Hunt | f67722a | 2014-11-10 09:32:06 -0800 | [diff] [blame] | 1817 | function preload(view, ctx, flags) { |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 1818 | var w = view.width(), |
| 1819 | h = view.height(), |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1820 | fcfg = config.force, |
| 1821 | fpad = fcfg.pad, |
| 1822 | forceDim = [w - 2*fpad, h - 2*fpad]; |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1823 | |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 1824 | // NOTE: view.$div is a D3 selection of the view's div |
Paul Greyson | 6cb8ca0 | 2014-11-12 18:09:02 -0800 | [diff] [blame] | 1825 | var viewBox = '0 0 ' + config.logicalSize + ' ' + config.logicalSize; |
| 1826 | svg = view.$div.append('svg').attr('viewBox', viewBox); |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 1827 | setSize(svg, view); |
| 1828 | |
Simon Hunt | 7fa116d | 2014-11-17 14:16:55 -0800 | [diff] [blame] | 1829 | loadGlyphs(svg); |
Simon Hunt | 12ce12e | 2014-11-15 21:13:19 -0800 | [diff] [blame] | 1830 | |
Paul Greyson | fcba0e8 | 2014-11-13 10:21:16 -0800 | [diff] [blame] | 1831 | zoomPanContainer = svg.append('g').attr('id', 'zoomPanContainer'); |
Paul Greyson | fcba0e8 | 2014-11-13 10:21:16 -0800 | [diff] [blame] | 1832 | setupZoomPan(); |
| 1833 | |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 1834 | // add blue glow filter to svg layer |
Paul Greyson | fcba0e8 | 2014-11-13 10:21:16 -0800 | [diff] [blame] | 1835 | d3u.appendGlow(zoomPanContainer); |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 1836 | |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1837 | // group for the topology |
Paul Greyson | fcba0e8 | 2014-11-13 10:21:16 -0800 | [diff] [blame] | 1838 | topoG = zoomPanContainer.append('g') |
Simon Hunt | d3b7d51 | 2014-11-12 15:48:41 -0800 | [diff] [blame] | 1839 | .attr('id', 'topo-G') |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1840 | .attr('transform', fcfg.translate()); |
| 1841 | |
| 1842 | // subgroups for links and nodes |
| 1843 | linkG = topoG.append('g').attr('id', 'links'); |
| 1844 | nodeG = topoG.append('g').attr('id', 'nodes'); |
| 1845 | |
| 1846 | // selection of nodes and links |
| 1847 | link = linkG.selectAll('.link'); |
| 1848 | node = nodeG.selectAll('.node'); |
| 1849 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1850 | function chrg(d) { |
| 1851 | return fcfg.charge[d.class] || -12000; |
| 1852 | } |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1853 | function ldist(d) { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1854 | return fcfg.linkDistance[d.type] || 50; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1855 | } |
| 1856 | function lstrg(d) { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1857 | // 0.0 - 1.0 |
| 1858 | return fcfg.linkStrength[d.type] || 1.0; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1859 | } |
| 1860 | |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 1861 | function selectCb(d, self) { |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1862 | selectObject(d, self); |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 1863 | } |
| 1864 | |
| 1865 | function atDragEnd(d, self) { |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1866 | // once we've finished moving, pin the node in position |
| 1867 | d.fixed = true; |
| 1868 | d3.select(self).classed('fixed', true); |
| 1869 | if (config.useLiveData) { |
Simon Hunt | 902c992 | 2014-11-11 11:59:31 -0800 | [diff] [blame] | 1870 | sendUpdateMeta(d); |
Simon Hunt | a255a2c | 2014-11-13 22:29:35 -0800 | [diff] [blame] | 1871 | } else { |
| 1872 | console.log('Moving node ' + d.id + ' to [' + d.x + ',' + d.y + ']'); |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 1873 | } |
| 1874 | } |
| 1875 | |
Simon Hunt | 902c992 | 2014-11-11 11:59:31 -0800 | [diff] [blame] | 1876 | function sendUpdateMeta(d) { |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1877 | sendMessage('updateMeta', { |
| 1878 | id: d.id, |
| 1879 | 'class': d.class, |
Simon Hunt | 902c992 | 2014-11-11 11:59:31 -0800 | [diff] [blame] | 1880 | 'memento': { |
Simon Hunt | 01095ff | 2014-11-13 16:37:29 -0800 | [diff] [blame] | 1881 | x: d.x, |
| 1882 | y: d.y |
Simon Hunt | 902c992 | 2014-11-11 11:59:31 -0800 | [diff] [blame] | 1883 | } |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1884 | }); |
| 1885 | } |
| 1886 | |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1887 | // set up the force layout |
| 1888 | network.force = d3.layout.force() |
| 1889 | .size(forceDim) |
| 1890 | .nodes(network.nodes) |
| 1891 | .links(network.links) |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1892 | .gravity(0.4) |
| 1893 | .friction(0.7) |
| 1894 | .charge(chrg) |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1895 | .linkDistance(ldist) |
| 1896 | .linkStrength(lstrg) |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1897 | .on('tick', tick); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1898 | |
Simon Hunt | 01095ff | 2014-11-13 16:37:29 -0800 | [diff] [blame] | 1899 | network.drag = d3u.createDragBehavior(network.force, |
Simon Hunt | deab432 | 2014-11-13 18:49:07 -0800 | [diff] [blame] | 1900 | selectCb, atDragEnd, panZoom); |
Simon Hunt | 0c6d419 | 2014-11-12 12:07:10 -0800 | [diff] [blame] | 1901 | |
| 1902 | // create mask layer for when we lose connection to server. |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 1903 | // TODO: this should be part of the framework |
Simon Hunt | 0c6d419 | 2014-11-12 12:07:10 -0800 | [diff] [blame] | 1904 | mask = view.$div.append('div').attr('id','topo-mask'); |
| 1905 | para(mask, 'Oops!'); |
| 1906 | para(mask, 'Web-socket connection to server closed...'); |
| 1907 | para(mask, 'Try refreshing the page.'); |
Simon Hunt | 12ce12e | 2014-11-15 21:13:19 -0800 | [diff] [blame] | 1908 | |
| 1909 | mask.append('svg') |
| 1910 | .attr({ |
| 1911 | id: 'mask-bird', |
| 1912 | width: w, |
| 1913 | height: h |
| 1914 | }) |
| 1915 | .append('g') |
| 1916 | .attr('transform', birdTranslate(w, h)) |
| 1917 | .style('opacity', 0.3) |
| 1918 | .append('use') |
| 1919 | .attr({ |
| 1920 | 'xlink:href': '#bird', |
| 1921 | width: config.birdDim, |
| 1922 | height: config.birdDim, |
| 1923 | fill: '#111' |
| 1924 | }) |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 1925 | } |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1926 | |
Simon Hunt | 01095ff | 2014-11-13 16:37:29 -0800 | [diff] [blame] | 1927 | function para(sel, text) { |
| 1928 | sel.append('p').text(text); |
| 1929 | } |
| 1930 | |
| 1931 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1932 | function load(view, ctx, flags) { |
Simon Hunt | f67722a | 2014-11-10 09:32:06 -0800 | [diff] [blame] | 1933 | // resize, in case the window was resized while we were not loaded |
| 1934 | resize(view, ctx, flags); |
| 1935 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1936 | // cache the view token, so network topo functions can access it |
| 1937 | network.view = view; |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1938 | config.useLiveData = !flags.local; |
| 1939 | |
| 1940 | if (!config.useLiveData) { |
| 1941 | prepareScenario(view, ctx, flags.debug); |
| 1942 | } |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1943 | |
| 1944 | // set our radio buttons and key bindings |
Simon Hunt | 9462e8c | 2014-11-14 17:28:09 -0800 | [diff] [blame] | 1945 | layerBtnSet = view.setRadio(layerButtons); |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 1946 | view.setKeys(keyDispatch); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1947 | |
Simon Hunt | f8e5b4e0 | 2014-11-13 11:17:57 -0800 | [diff] [blame] | 1948 | // patch in our "button bar" for now |
| 1949 | // TODO: implement a more official frameworky way of doing this.. |
| 1950 | addButtonBar(view); |
| 1951 | |
Simon Hunt | d3b7d51 | 2014-11-12 15:48:41 -0800 | [diff] [blame] | 1952 | // Load map data asynchronously; complete startup after that.. |
| 1953 | loadGeoJsonData(); |
Simon Hunt | a255a2c | 2014-11-13 22:29:35 -0800 | [diff] [blame] | 1954 | |
| 1955 | // start the and timer |
| 1956 | var dashIdx = 0; |
| 1957 | antTimer = setInterval(function () { |
| 1958 | // TODO: figure out how to choose Src-->Dst and Dst-->Src, per link |
| 1959 | dashIdx = dashIdx === 0 ? 14 : dashIdx - 2; |
| 1960 | d3.selectAll('.animated').style('stroke-dashoffset', dashIdx); |
| 1961 | }, 35); |
| 1962 | } |
| 1963 | |
| 1964 | function unload(view, ctx, flags) { |
| 1965 | if (antTimer) { |
| 1966 | clearInterval(antTimer); |
| 1967 | antTimer = null; |
| 1968 | } |
Simon Hunt | d3b7d51 | 2014-11-12 15:48:41 -0800 | [diff] [blame] | 1969 | } |
| 1970 | |
| 1971 | // TODO: move these to config/state portion of script |
Paul Greyson | 6cb8ca0 | 2014-11-12 18:09:02 -0800 | [diff] [blame] | 1972 | var geoJsonUrl = 'json/map/continental_us.json', // TODO: Paul |
Simon Hunt | d3b7d51 | 2014-11-12 15:48:41 -0800 | [diff] [blame] | 1973 | geoJson; |
| 1974 | |
| 1975 | function loadGeoJsonData() { |
| 1976 | d3.json(geoJsonUrl, function (err, data) { |
| 1977 | if (err) { |
| 1978 | // fall back to USA map background |
| 1979 | loadStaticMap(); |
| 1980 | } else { |
| 1981 | geoJson = data; |
| 1982 | loadGeoMap(); |
| 1983 | } |
| 1984 | |
| 1985 | // finally, connect to the server... |
| 1986 | if (config.useLiveData) { |
| 1987 | webSock.connect(); |
| 1988 | } |
| 1989 | }); |
| 1990 | } |
| 1991 | |
| 1992 | function showBg() { |
| 1993 | return config.options.showBackground ? 'visible' : 'hidden'; |
| 1994 | } |
| 1995 | |
| 1996 | function loadStaticMap() { |
| 1997 | fnTrace('loadStaticMap', config.backgroundUrl); |
| 1998 | var w = network.view.width(), |
| 1999 | h = network.view.height(); |
| 2000 | |
| 2001 | // load the background image |
| 2002 | bgImg = svg.insert('svg:image', '#topo-G') |
| 2003 | .attr({ |
| 2004 | id: 'topo-bg', |
| 2005 | width: w, |
| 2006 | height: h, |
| 2007 | 'xlink:href': config.backgroundUrl |
| 2008 | }) |
| 2009 | .style({ |
| 2010 | visibility: showBg() |
| 2011 | }); |
| 2012 | } |
| 2013 | |
| 2014 | function loadGeoMap() { |
| 2015 | fnTrace('loadGeoMap', geoJsonUrl); |
Simon Hunt | d3b7d51 | 2014-11-12 15:48:41 -0800 | [diff] [blame] | 2016 | |
Paul Greyson | 6cb8ca0 | 2014-11-12 18:09:02 -0800 | [diff] [blame] | 2017 | // extracts the topojson data into geocoordinate-based geometry |
| 2018 | var topoData = topojson.feature(geoJson, geoJson.objects.states); |
Simon Hunt | d3b7d51 | 2014-11-12 15:48:41 -0800 | [diff] [blame] | 2019 | |
Paul Greyson | 6cb8ca0 | 2014-11-12 18:09:02 -0800 | [diff] [blame] | 2020 | // see: http://bl.ocks.org/mbostock/4707858 |
| 2021 | geoMapProjection = d3.geo.mercator(); |
| 2022 | var path = d3.geo.path().projection(geoMapProjection); |
Simon Hunt | d3b7d51 | 2014-11-12 15:48:41 -0800 | [diff] [blame] | 2023 | |
Paul Greyson | 6cb8ca0 | 2014-11-12 18:09:02 -0800 | [diff] [blame] | 2024 | geoMapProjection |
| 2025 | .scale(1) |
| 2026 | .translate([0, 0]); |
Simon Hunt | d3b7d51 | 2014-11-12 15:48:41 -0800 | [diff] [blame] | 2027 | |
Paul Greyson | 6cb8ca0 | 2014-11-12 18:09:02 -0800 | [diff] [blame] | 2028 | // [[x1,y1],[x2,y2]] |
| 2029 | var b = path.bounds(topoData); |
Paul Greyson | fcba0e8 | 2014-11-13 10:21:16 -0800 | [diff] [blame] | 2030 | // size map to 95% of minimum dimension to fill space |
| 2031 | var s = .95 / Math.min((b[1][0] - b[0][0]) / config.logicalSize, (b[1][1] - b[0][1]) / config.logicalSize); |
Paul Greyson | 6cb8ca0 | 2014-11-12 18:09:02 -0800 | [diff] [blame] | 2032 | var t = [(config.logicalSize - s * (b[1][0] + b[0][0])) / 2, (config.logicalSize - s * (b[1][1] + b[0][1])) / 2]; |
Simon Hunt | d3b7d51 | 2014-11-12 15:48:41 -0800 | [diff] [blame] | 2033 | |
Paul Greyson | 6cb8ca0 | 2014-11-12 18:09:02 -0800 | [diff] [blame] | 2034 | geoMapProjection |
| 2035 | .scale(s) |
| 2036 | .translate(t); |
| 2037 | |
Paul Greyson | fcba0e8 | 2014-11-13 10:21:16 -0800 | [diff] [blame] | 2038 | bgImg = zoomPanContainer.insert("g", '#topo-G'); |
Paul Greyson | 6cb8ca0 | 2014-11-12 18:09:02 -0800 | [diff] [blame] | 2039 | bgImg.attr('id', 'map').selectAll('path') |
| 2040 | .data(topoData.features) |
| 2041 | .enter() |
| 2042 | .append('path') |
| 2043 | .attr('d', path); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 2044 | } |
| 2045 | |
Simon Hunt | f67722a | 2014-11-10 09:32:06 -0800 | [diff] [blame] | 2046 | function resize(view, ctx, flags) { |
Simon Hunt | 12ce12e | 2014-11-15 21:13:19 -0800 | [diff] [blame] | 2047 | var w = view.width(), |
| 2048 | h = view.height(); |
| 2049 | |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 2050 | setSize(svg, view); |
Simon Hunt | 12ce12e | 2014-11-15 21:13:19 -0800 | [diff] [blame] | 2051 | |
| 2052 | d3.select('#mask-bird').attr({ width: w, height: h}) |
| 2053 | .select('g').attr('transform', birdTranslate(w, h)); |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 2054 | } |
| 2055 | |
Simon Hunt | 12ce12e | 2014-11-15 21:13:19 -0800 | [diff] [blame] | 2056 | function birdTranslate(w, h) { |
| 2057 | var bdim = config.birdDim; |
| 2058 | return 'translate('+((w-bdim)*.4)+','+((h-bdim)*.1)+')'; |
| 2059 | } |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 2060 | |
| 2061 | // ============================== |
| 2062 | // View registration |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 2063 | |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 2064 | onos.ui.addView('topo', { |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 2065 | preload: preload, |
| 2066 | load: load, |
Simon Hunt | a255a2c | 2014-11-13 22:29:35 -0800 | [diff] [blame] | 2067 | unload: unload, |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 2068 | resize: resize |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 2069 | }); |
| 2070 | |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 2071 | detailPane = onos.ui.addFloatingPanel('topo-detail'); |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 2072 | oiBox = onos.ui.addFloatingPanel('topo-oibox', 'TL'); |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 2073 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 2074 | }(ONOS)); |