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