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