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