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