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