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