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