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