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