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