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