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