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