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