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