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