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