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