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