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