blob: e212f95198f8cc2f8f4477c62b4e8045c9cb7536 [file] [log] [blame]
Simon Hunt195cb382014-11-03 17:50:51 -08001/*
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 Hunt142d0032014-11-04 20:13:09 -080018 ONOS network topology viewer - version 1.1
Simon Hunt195cb382014-11-03 17:50:51 -080019
20 @author Simon Hunt
Simon Hunta6a9fe72014-11-20 11:17:12 -080021 @author Thomas Vachuska
Simon Hunt195cb382014-11-03 17:50:51 -080022 */
23
24(function (onos) {
25 'use strict';
26
Simon Hunt1a9eff92014-11-07 11:06:34 -080027 // shorter names for library APIs
Simon Huntbb282f52014-11-10 11:08:19 -080028 var d3u = onos.lib.d3util,
Simon Hunta6a9fe72014-11-20 11:17:12 -080029 gly = onos.lib.glyphs;
Simon Hunt1a9eff92014-11-07 11:06:34 -080030
Simon Hunt195cb382014-11-03 17:50:51 -080031 // configuration data
32 var config = {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -080033 useLiveData: true,
Simon Huntfc274c92014-11-11 11:05:46 -080034 fnTrace: true,
Simon Hunt195cb382014-11-03 17:50:51 -080035 debugOn: false,
36 debug: {
Simon Hunt99c13842014-11-06 18:23:12 -080037 showNodeXY: true,
38 showKeyHandler: false
Simon Hunt195cb382014-11-03 17:50:51 -080039 },
Simon Hunt12ce12e2014-11-15 21:13:19 -080040 birdDim: 400,
Simon Hunt195cb382014-11-03 17:50:51 -080041 options: {
42 layering: true,
43 collisionPrevention: true,
Simon Hunt142d0032014-11-04 20:13:09 -080044 showBackground: true
Simon Hunt195cb382014-11-03 17:50:51 -080045 },
46 backgroundUrl: 'img/us-map.png',
Thomas Vachuska7d638d32014-11-07 10:24:43 -080047 webSockUrl: 'ws/topology',
Simon Hunt195cb382014-11-03 17:50:51 -080048 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 Hunt99c13842014-11-06 18:23:12 -080060 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 Hunt1a9eff92014-11-07 11:06:34 -080072 topo: {
Thomas Vachuska89543292014-11-19 11:28:33 -080073 linkBaseColor: '#666',
Simon Hunt1a9eff92014-11-07 11:06:34 -080074 linkInColor: '#66f',
Thomas Vachuska9edca302014-11-22 17:06:42 -080075 linkInWidth: 12,
Thomas Vachuska89543292014-11-19 11:28:33 -080076 linkOutColor: '#f00',
Thomas Vachuska9edca302014-11-22 17:06:42 -080077 linkOutWidth: 10
Simon Hunt1a9eff92014-11-07 11:06:34 -080078 },
Paul Greyson29cd58f2014-11-18 13:14:57 -080079 icons: {
Simon Huntc72967b2014-11-20 09:21:42 -080080 device: {
Simon Hunt395a70c2014-11-22 23:17:40 -080081 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 Huntc72967b2014-11-20 09:21:42 -080093 }
Thomas Vachuska89543292014-11-19 11:28:33 -080094 },
Simon Hunt195cb382014-11-03 17:50:51 -080095 force: {
Simon Hunt7cd48f32014-11-09 23:42:50 -080096 note_for_links: 'link.type is used to differentiate',
Simon Huntc7ee0662014-11-05 16:44:37 -080097 linkDistance: {
Simon Hunt7cd48f32014-11-09 23:42:50 -080098 direct: 100,
99 optical: 120,
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800100 hostLink: 3
Simon Huntc7ee0662014-11-05 16:44:37 -0800101 },
102 linkStrength: {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800103 direct: 1.0,
104 optical: 1.0,
105 hostLink: 1.0
Simon Huntc7ee0662014-11-05 16:44:37 -0800106 },
Simon Hunt7cd48f32014-11-09 23:42:50 -0800107 note_for_nodes: 'node.class is used to differentiate',
Simon Huntc7ee0662014-11-05 16:44:37 -0800108 charge: {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800109 device: -8000,
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800110 host: -5000
Simon Huntc7ee0662014-11-05 16:44:37 -0800111 },
112 pad: 20,
Simon Hunt195cb382014-11-03 17:50:51 -0800113 translate: function() {
114 return 'translate(' +
Simon Huntc7ee0662014-11-05 16:44:37 -0800115 config.force.pad + ',' +
116 config.force.pad + ')';
Simon Hunt195cb382014-11-03 17:50:51 -0800117 }
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800118 },
119 // see below in creation of viewBox on main svg
120 logicalSize: 1000
Simon Hunt195cb382014-11-03 17:50:51 -0800121 };
122
Simon Hunt142d0032014-11-04 20:13:09 -0800123 // radio buttons
Simon Hunt9462e8c2014-11-14 17:28:09 -0800124 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 Hunt934c3ce2014-11-05 11:45:07 -0800135
136 // key bindings
137 var keyDispatch = {
Simon Hunt988c6fc2014-11-20 17:43:03 -0800138 // TODO: remove these "development only" bindings
Simon Hunt8f40cce2014-11-23 15:57:30 -0800139 0: testMe,
140 equals: injectStartupEvents,
141 dash: injectTestEvent,
Simon Hunt99c13842014-11-06 18:23:12 -0800142
Thomas Vachuska47635c62014-11-22 01:21:36 -0800143 O: [toggleSummary, 'Toggle ONOS summary pane'],
144 I: [toggleInstances, 'Toggle ONOS instances pane'],
Simon Hunt988c6fc2014-11-20 17:43:03 -0800145 B: [toggleBg, 'Toggle background image'],
Simon Hunt434cf142014-11-24 11:10:28 -0800146 H: [toggleHosts, 'Toggle host visibility'],
Simon Hunt988c6fc2014-11-20 17:43:03 -0800147 L: [cycleLabels, 'Cycle Device labels'],
Simon Hunt934c3ce2014-11-05 11:45:07 -0800148 P: togglePorts,
Simon Hunt87514342014-11-24 16:41:27 -0800149 U: [unpin, 'Unpin node (hover mouse over)'],
150 R: [resetZoomPan, 'Reset zoom / pan'],
Thomas Vachuska9edca302014-11-22 17:06:42 -0800151 V: [showTrafficAction, 'Show related traffic'],
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800152 A: [showAllTrafficAction, 'Show all traffic'],
153 F: [showDeviceLinkFlowsAction, 'Show device link flows'],
Simon Hunt9462e8c2014-11-14 17:28:09 -0800154 esc: handleEscape
Simon Hunt934c3ce2014-11-05 11:45:07 -0800155 };
Simon Hunt142d0032014-11-04 20:13:09 -0800156
Simon Hunt87514342014-11-24 16:41:27 -0800157 // mouse gestures
158 var gestures = [
159 ['click', 'Select the item and show details'],
160 ['shift-click', 'Toggle selection state'],
161 ['drag', 'Reposition (and pin) device / host'],
162 ['cmd-scroll', 'Zoom in / out'],
163 ['cmd-drag', 'Pan']
164 ];
165
Simon Hunt195cb382014-11-03 17:50:51 -0800166 // state variables
Simon Hunt99c13842014-11-06 18:23:12 -0800167 var network = {
Simon Hunt50128c02014-11-08 13:36:15 -0800168 view: null, // view token reference
Simon Hunt99c13842014-11-06 18:23:12 -0800169 nodes: [],
170 links: [],
Simon Hunt269670f2014-11-17 16:17:43 -0800171 lookup: {},
172 revLinkToKey: {}
Simon Hunt99c13842014-11-06 18:23:12 -0800173 },
Simon Hunt56d51852014-11-09 13:03:35 -0800174 scenario = {
175 evDir: 'json/ev/',
176 evScenario: '/scenario.json',
177 evPrefix: '/ev_',
178 evOnos: '_onos.json',
179 evUi: '_ui.json',
180 ctx: null,
181 params: {},
182 evNumber: 0,
Simon Hunt434cf142014-11-24 11:10:28 -0800183 view: null
Simon Hunt56d51852014-11-09 13:03:35 -0800184 },
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800185 webSock,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800186 sid = 0,
Thomas Vachuska60d72bf2014-11-21 13:02:00 -0800187 deviceLabelCount = 3,
Simon Hunt209155e2014-11-21 12:16:09 -0800188 hostLabelCount = 2,
Simon Hunt56d51852014-11-09 13:03:35 -0800189 deviceLabelIndex = 0,
190 hostLabelIndex = 0,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800191 selections = {},
Simon Hunta5e89142014-11-14 07:00:33 -0800192 selectOrder = [],
Simon Hunt6ac93f32014-11-13 12:17:27 -0800193 hovered = null,
Thomas Vachuska47635c62014-11-22 01:21:36 -0800194 summaryPane,
Simon Hunta5e89142014-11-14 07:00:33 -0800195 detailPane,
Simon Hunta255a2c2014-11-13 22:29:35 -0800196 antTimer = null,
Simon Hunta5e89142014-11-14 07:00:33 -0800197 onosInstances = {},
198 onosOrder = [],
199 oiBox,
Simon Hunt9462e8c2014-11-14 17:28:09 -0800200 oiShowMaster = false,
Simon Hunt8f40cce2014-11-23 15:57:30 -0800201 portLabelsOn = false,
Simon Huntb0ecfa52014-11-23 21:05:12 -0800202 cat7 = d3u.cat7(),
Simon Hunt434cf142014-11-24 11:10:28 -0800203 colorAffinity = false,
204 showHosts = false;
Simon Hunt195cb382014-11-03 17:50:51 -0800205
Simon Hunt434cf142014-11-24 11:10:28 -0800206 // constants
Thomas Vachuska9edca302014-11-22 17:06:42 -0800207 var hoverModeAll = 1,
208 hoverModeFlows = 2,
209 hoverModeIntents = 3,
210 hoverMode = hoverModeFlows;
211
Simon Hunt934c3ce2014-11-05 11:45:07 -0800212 // D3 selections
213 var svg,
Paul Greysonfcba0e82014-11-13 10:21:16 -0800214 zoomPanContainer,
Simon Hunt934c3ce2014-11-05 11:45:07 -0800215 bgImg,
Simon Huntc7ee0662014-11-05 16:44:37 -0800216 topoG,
217 nodeG,
218 linkG,
Simon Hunte2575b62014-11-18 15:25:53 -0800219 linkLabelG,
Simon Huntc7ee0662014-11-05 16:44:37 -0800220 node,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800221 link,
Simon Hunte2575b62014-11-18 15:25:53 -0800222 linkLabel,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800223 mask;
Simon Hunt195cb382014-11-03 17:50:51 -0800224
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800225 // the projection for the map background
226 var geoMapProjection;
227
Paul Greysonfcba0e82014-11-13 10:21:16 -0800228 // the zoom function
229 var zoom;
230
Simon Hunt142d0032014-11-04 20:13:09 -0800231 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800232 // For Debugging / Development
Simon Hunt195cb382014-11-03 17:50:51 -0800233
Simon Hunt99c13842014-11-06 18:23:12 -0800234 function note(label, msg) {
235 console.log('NOTE: ' + label + ': ' + msg);
Simon Hunt195cb382014-11-03 17:50:51 -0800236 }
237
Simon Hunt99c13842014-11-06 18:23:12 -0800238 function debug(what) {
239 return config.debugOn && config.debug[what];
Simon Hunt934c3ce2014-11-05 11:45:07 -0800240 }
241
Simon Huntfc274c92014-11-11 11:05:46 -0800242 function fnTrace(msg, id) {
243 if (config.fnTrace) {
244 console.log('FN: ' + msg + ' [' + id + ']');
245 }
246 }
Simon Hunt99c13842014-11-06 18:23:12 -0800247
Simon Hunta5e89142014-11-14 07:00:33 -0800248 function evTrace(data) {
249 fnTrace(data.event, data.payload.id);
250 }
251
Simon Hunt934c3ce2014-11-05 11:45:07 -0800252 // ==============================
253 // Key Callbacks
254
Simon Hunt99c13842014-11-06 18:23:12 -0800255 function testMe(view) {
Simon Hunt8f40cce2014-11-23 15:57:30 -0800256 //view.alert('Theme is ' + view.getTheme());
Simon Hunta3dd9572014-11-20 15:22:41 -0800257 //view.flash('This is some text');
Simon Hunt8f40cce2014-11-23 15:57:30 -0800258 cat7.testCard(svg);
Simon Hunt99c13842014-11-06 18:23:12 -0800259 }
260
Simon Hunt56d51852014-11-09 13:03:35 -0800261 function injectTestEvent(view) {
Simon Hunt434cf142014-11-24 11:10:28 -0800262 if (config.useLiveData) { return; }
263
Simon Hunt56d51852014-11-09 13:03:35 -0800264 var sc = scenario,
265 evn = ++sc.evNumber,
266 pfx = sc.evDir + sc.ctx + sc.evPrefix + evn,
267 onosUrl = pfx + sc.evOnos,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800268 uiUrl = pfx + sc.evUi,
269 stack = [
270 { url: onosUrl, cb: handleServerEvent },
271 { url: uiUrl, cb: handleUiEvent }
272 ];
273 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800274 }
275
Simon Hunt7cd48f32014-11-09 23:42:50 -0800276 function recurseFetchEvent(stack, evn) {
277 var v = scenario.view,
278 frame;
279 if (stack.length === 0) {
Simon Huntfc274c92014-11-11 11:05:46 -0800280 v.alert('Oops!\n\nNo event #' + evn + ' found.');
Simon Hunt7cd48f32014-11-09 23:42:50 -0800281 return;
282 }
283 frame = stack.shift();
284
285 d3.json(frame.url, function (err, data) {
Simon Hunt99c13842014-11-06 18:23:12 -0800286 if (err) {
Simon Hunt56d51852014-11-09 13:03:35 -0800287 if (err.status === 404) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800288 // if we didn't find the data, try the next stack frame
289 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800290 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800291 v.alert('non-404 error:\n\n' + frame.url + '\n\n' + err);
Simon Hunt56d51852014-11-09 13:03:35 -0800292 }
Simon Hunt99c13842014-11-06 18:23:12 -0800293 } else {
Simon Hunt1712ed82014-11-17 12:56:00 -0800294 wsTrace('test', JSON.stringify(data));
Simon Hunt7cd48f32014-11-09 23:42:50 -0800295 frame.cb(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800296 }
297 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800298
Simon Hunt56d51852014-11-09 13:03:35 -0800299 }
Simon Hunt50128c02014-11-08 13:36:15 -0800300
Simon Hunt56d51852014-11-09 13:03:35 -0800301 function handleUiEvent(data) {
Simon Huntbb282f52014-11-10 11:08:19 -0800302 scenario.view.alert('UI Tx: ' + data.event + '\n\n' +
303 JSON.stringify(data));
Simon Hunt56d51852014-11-09 13:03:35 -0800304 }
305
306 function injectStartupEvents(view) {
307 var last = scenario.params.lastAuto || 0;
Simon Hunt434cf142014-11-24 11:10:28 -0800308 if (config.useLiveData) { return; }
Simon Hunt56d51852014-11-09 13:03:35 -0800309
310 while (scenario.evNumber < last) {
Simon Hunt1a9eff92014-11-07 11:06:34 -0800311 injectTestEvent(view);
312 }
313 }
314
Simon Hunt934c3ce2014-11-05 11:45:07 -0800315 function toggleBg() {
316 var vis = bgImg.style('visibility');
Simon Hunt434cf142014-11-24 11:10:28 -0800317 bgImg.style('visibility', visVal(vis === 'hidden'));
318 }
319
320 function toggleHosts() {
321 showHosts = !showHosts;
322 updateHostVisibility();
323 network.view.flash('Hosts ' + visVal(showHosts));
Simon Hunt934c3ce2014-11-05 11:45:07 -0800324 }
325
Simon Hunt99c13842014-11-06 18:23:12 -0800326 function cycleLabels() {
Thomas Vachuska60d72bf2014-11-21 13:02:00 -0800327 deviceLabelIndex = (deviceLabelIndex === 2)
Simon Huntbb282f52014-11-10 11:08:19 -0800328 ? 0 : deviceLabelIndex + 1;
Simon Hunt5f36d342014-11-08 21:33:14 -0800329
Simon Hunt99c13842014-11-06 18:23:12 -0800330 network.nodes.forEach(function (d) {
Simon Huntbb282f52014-11-10 11:08:19 -0800331 if (d.class === 'device') {
332 updateDeviceLabel(d);
333 }
Simon Hunt99c13842014-11-06 18:23:12 -0800334 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800335 }
336
337 function togglePorts(view) {
Simon Hunt434cf142014-11-24 11:10:28 -0800338 //view.alert('togglePorts() callback')
Simon Hunt934c3ce2014-11-05 11:45:07 -0800339 }
340
Simon Hunt6ac93f32014-11-13 12:17:27 -0800341 function unpin() {
342 if (hovered) {
Simon Hunt395a70c2014-11-22 23:17:40 -0800343 sendUpdateMeta(hovered);
Simon Hunt6ac93f32014-11-13 12:17:27 -0800344 hovered.fixed = false;
345 hovered.el.classed('fixed', false);
346 network.force.resume();
347 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800348 }
349
Simon Hunt9462e8c2014-11-14 17:28:09 -0800350 function handleEscape(view) {
351 if (oiShowMaster) {
352 cancelAffinity();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800353 } else if (detailPane.isVisible()) {
Simon Hunt9462e8c2014-11-14 17:28:09 -0800354 deselectAll();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800355 } else if (oiBox.isVisible()) {
Simon Huntb0ecfa52014-11-23 21:05:12 -0800356 hideInstances();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800357 } else if (summaryPane.isVisible()) {
358 cancelSummary();
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800359 stopAntTimer();
360 } else {
361 hoverMode = hoverModeFlows;
Simon Hunt9462e8c2014-11-14 17:28:09 -0800362 }
363 }
364
Simon Hunt934c3ce2014-11-05 11:45:07 -0800365 // ==============================
366 // Radio Button Callbacks
367
Simon Hunta5e89142014-11-14 07:00:33 -0800368 var layerLookup = {
369 host: {
Simon Hunt209155e2014-11-21 12:16:09 -0800370 endstation: 'pkt', // default, if host event does not define type
Thomas Vachuska89543292014-11-19 11:28:33 -0800371 router: 'pkt',
Simon Hunta5e89142014-11-14 07:00:33 -0800372 bgpSpeaker: 'pkt'
373 },
374 device: {
375 switch: 'pkt',
376 roadm: 'opt'
377 },
378 link: {
379 hostLink: 'pkt',
380 direct: 'pkt',
Simon Hunt8257f4c2014-11-16 19:34:54 -0800381 indirect: '',
382 tunnel: '',
Simon Hunta5e89142014-11-14 07:00:33 -0800383 optical: 'opt'
384 }
385 };
386
387 function inLayer(d, layer) {
Simon Hunt8257f4c2014-11-16 19:34:54 -0800388 var type = d.class === 'link' ? d.type() : d.type,
389 look = layerLookup[d.class],
390 lyr = look && look[type];
Simon Hunta5e89142014-11-14 07:00:33 -0800391 return lyr === layer;
392 }
393
394 function unsuppressLayer(which) {
395 node.each(function (d) {
396 var node = d.el;
397 if (inLayer(d, which)) {
398 node.classed('suppressed', false);
399 }
400 });
401
402 link.each(function (d) {
403 var link = d.el;
404 if (inLayer(d, which)) {
405 link.classed('suppressed', false);
406 }
407 });
408 }
409
Simon Hunt9462e8c2014-11-14 17:28:09 -0800410 function suppressLayers(b) {
411 node.classed('suppressed', b);
412 link.classed('suppressed', b);
Simon Hunt142d0032014-11-04 20:13:09 -0800413// d3.selectAll('svg .port').classed('inactive', false);
414// d3.selectAll('svg .portText').classed('inactive', false);
Simon Hunt195cb382014-11-03 17:50:51 -0800415 }
416
Simon Hunt9462e8c2014-11-14 17:28:09 -0800417 function showAllLayers() {
418 suppressLayers(false);
419 }
420
Simon Hunt195cb382014-11-03 17:50:51 -0800421 function showPacketLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800422 node.classed('suppressed', true);
423 link.classed('suppressed', true);
424 unsuppressLayer('pkt');
Simon Hunt195cb382014-11-03 17:50:51 -0800425 }
426
427 function showOpticalLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800428 node.classed('suppressed', true);
429 link.classed('suppressed', true);
430 unsuppressLayer('opt');
Simon Hunt195cb382014-11-03 17:50:51 -0800431 }
432
Simon Hunt9462e8c2014-11-14 17:28:09 -0800433 function restoreLayerState() {
434 layerBtnDispatch[layerBtnSet.selected()]();
435 }
436
Simon Hunt142d0032014-11-04 20:13:09 -0800437 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800438 // Private functions
439
Simon Hunt99c13842014-11-06 18:23:12 -0800440 function safeId(s) {
441 return s.replace(/[^a-z0-9]/gi, '-');
442 }
443
Simon Huntc7ee0662014-11-05 16:44:37 -0800444 // set the size of the given element to that of the view (reduced if padded)
445 function setSize(el, view, pad) {
446 var padding = pad ? pad * 2 : 0;
Simon Hunt934c3ce2014-11-05 11:45:07 -0800447 el.attr({
Simon Huntc7ee0662014-11-05 16:44:37 -0800448 width: view.width() - padding,
449 height: view.height() - padding
Simon Hunt934c3ce2014-11-05 11:45:07 -0800450 });
451 }
452
Simon Hunt8257f4c2014-11-16 19:34:54 -0800453 function makeNodeKey(d, what) {
454 var port = what + 'Port';
455 return d[what] + '/' + d[port];
456 }
457
458 function makeLinkKey(d, flipped) {
459 var one = flipped ? makeNodeKey(d, 'dst') : makeNodeKey(d, 'src'),
460 two = flipped ? makeNodeKey(d, 'src') : makeNodeKey(d, 'dst');
461 return one + '-' + two;
462 }
463
Simon Hunt269670f2014-11-17 16:17:43 -0800464 function findLinkById(id) {
465 // check to see if this is a reverse lookup, else default to given id
466 var key = network.revLinkToKey[id] || id;
467 return key && network.lookup[key];
468 }
469
Simon Hunt8257f4c2014-11-16 19:34:54 -0800470 function findLink(linkData, op) {
471 var key = makeLinkKey(linkData),
472 keyrev = makeLinkKey(linkData, 1),
473 link = network.lookup[key],
474 linkRev = network.lookup[keyrev],
475 result = {},
476 ldata = link || linkRev,
477 rawLink;
478
479 if (op === 'add') {
480 if (link) {
481 // trying to add a link that we already know about
482 result.ldata = link;
483 result.badLogic = 'addLink: link already added';
484
485 } else if (linkRev) {
486 // we found the reverse of the link to be added
487 result.ldata = linkRev;
488 if (linkRev.fromTarget) {
489 result.badLogic = 'addLink: link already added';
490 }
491 }
492 } else if (op === 'update') {
493 if (!ldata) {
494 result.badLogic = 'updateLink: link not found';
495 } else {
496 rawLink = link ? ldata.fromSource : ldata.fromTarget;
497 result.updateWith = function (data) {
498 $.extend(rawLink, data);
499 restyleLinkElement(ldata);
500 }
501 }
502 } else if (op === 'remove') {
503 if (!ldata) {
504 result.badLogic = 'removeLink: link not found';
505 } else {
506 rawLink = link ? ldata.fromSource : ldata.fromTarget;
507
508 if (!rawLink) {
509 result.badLogic = 'removeLink: link not found';
510
511 } else {
512 result.removeRawLink = function () {
513 if (link) {
514 // remove fromSource
515 ldata.fromSource = null;
516 if (ldata.fromTarget) {
517 // promote target into source position
518 ldata.fromSource = ldata.fromTarget;
519 ldata.fromTarget = null;
520 ldata.key = keyrev;
521 delete network.lookup[key];
522 network.lookup[keyrev] = ldata;
Simon Hunt269670f2014-11-17 16:17:43 -0800523 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800524 }
525 } else {
526 // remove fromTarget
527 ldata.fromTarget = null;
Simon Hunt269670f2014-11-17 16:17:43 -0800528 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800529 }
530 if (ldata.fromSource) {
531 restyleLinkElement(ldata);
532 } else {
533 removeLinkElement(ldata);
534 }
535 }
536 }
537 }
538 }
539 return result;
540 }
541
542 function addLinkUpdate(ldata, link) {
543 // add link event, but we already have the reverse link installed
544 ldata.fromTarget = link;
Simon Hunt269670f2014-11-17 16:17:43 -0800545 network.revLinkToKey[link.id] = ldata.key;
Simon Hunt8257f4c2014-11-16 19:34:54 -0800546 restyleLinkElement(ldata);
547 }
548
549 var allLinkTypes = 'direct indirect optical tunnel',
550 defaultLinkType = 'direct';
551
552 function restyleLinkElement(ldata) {
553 // this fn's job is to look at raw links and decide what svg classes
554 // need to be applied to the line element in the DOM
555 var el = ldata.el,
556 type = ldata.type(),
557 lw = ldata.linkWidth(),
558 online = ldata.online();
559
560 el.classed('link', true);
561 el.classed('inactive', !online);
562 el.classed(allLinkTypes, false);
563 if (type) {
564 el.classed(type, true);
565 }
566 el.transition()
567 .duration(1000)
Thomas Vachuska89543292014-11-19 11:28:33 -0800568 .attr('stroke-width', linkScale(lw))
569 .attr('stroke', config.topo.linkBaseColor);
Simon Hunt8257f4c2014-11-16 19:34:54 -0800570 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800571
Simon Hunt99c13842014-11-06 18:23:12 -0800572 // ==============================
573 // Event handlers for server-pushed events
574
Simon Huntbb282f52014-11-10 11:08:19 -0800575 function logicError(msg) {
576 // TODO, report logic error to server, via websock, so it can be logged
Simon Huntfc274c92014-11-11 11:05:46 -0800577 console.warn(msg);
Simon Huntbb282f52014-11-10 11:08:19 -0800578 }
579
Simon Hunt99c13842014-11-06 18:23:12 -0800580 var eventDispatch = {
Simon Hunta5e89142014-11-14 07:00:33 -0800581 addInstance: addInstance,
Simon Hunt99c13842014-11-06 18:23:12 -0800582 addDevice: addDevice,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800583 addLink: addLink,
Simon Hunt56d51852014-11-09 13:03:35 -0800584 addHost: addHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800585
Simon Huntfcfb46c2014-11-19 12:53:38 -0800586 updateInstance: updateInstance,
Simon Huntbb282f52014-11-10 11:08:19 -0800587 updateDevice: updateDevice,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800588 updateLink: updateLink,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800589 updateHost: updateHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800590
Simon Hunt7b403bc2014-11-22 19:01:00 -0800591 removeInstance: removeInstance,
Simon Huntbb282f52014-11-10 11:08:19 -0800592 removeDevice: stillToImplement,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800593 removeLink: removeLink,
Simon Hunt44031102014-11-11 13:20:36 -0800594 removeHost: removeHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800595
Simon Hunt61d04042014-11-11 17:27:16 -0800596 showDetails: showDetails,
Thomas Vachuska47635c62014-11-22 01:21:36 -0800597 showSummary: showSummary,
Simon Huntb53e0682014-11-12 13:32:01 -0800598 showTraffic: showTraffic
Simon Hunt99c13842014-11-06 18:23:12 -0800599 };
600
Simon Hunta5e89142014-11-14 07:00:33 -0800601 function addInstance(data) {
602 evTrace(data);
603 var inst = data.payload,
604 id = inst.id;
605 if (onosInstances[id]) {
606 logicError('ONOS instance already added: ' + id);
607 return;
608 }
609 onosInstances[id] = inst;
610 onosOrder.push(inst);
611 updateInstances();
612 }
613
Simon Hunt99c13842014-11-06 18:23:12 -0800614 function addDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800615 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800616 var device = data.payload,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800617 nodeData = createDeviceNode(device);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800618 network.nodes.push(nodeData);
619 network.lookup[nodeData.id] = nodeData;
Simon Hunt99c13842014-11-06 18:23:12 -0800620 updateNodes();
621 network.force.start();
622 }
623
Simon Hunt99c13842014-11-06 18:23:12 -0800624 function addLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800625 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800626 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800627 result = findLink(link, 'add'),
628 bad = result.badLogic,
629 ldata = result.ldata;
630
631 if (bad) {
632 logicError(bad + ': ' + link.id);
633 return;
634 }
635
636 if (ldata) {
637 // we already have a backing store link for src/dst nodes
638 addLinkUpdate(ldata, link);
639 return;
640 }
641
642 // no backing store link yet
643 ldata = createLink(link);
644 if (ldata) {
645 network.links.push(ldata);
646 network.lookup[ldata.key] = ldata;
Simon Hunt99c13842014-11-06 18:23:12 -0800647 updateLinks();
648 network.force.start();
649 }
650 }
651
Simon Hunt56d51852014-11-09 13:03:35 -0800652 function addHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800653 evTrace(data);
Simon Hunt56d51852014-11-09 13:03:35 -0800654 var host = data.payload,
655 node = createHostNode(host),
656 lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800657 network.nodes.push(node);
658 network.lookup[host.id] = node;
659 updateNodes();
660
661 lnk = createHostLink(host);
662 if (lnk) {
Simon Hunt44031102014-11-11 13:20:36 -0800663 node.linkData = lnk; // cache ref on its host
Simon Hunt56d51852014-11-09 13:03:35 -0800664 network.links.push(lnk);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800665 network.lookup[host.ingress] = lnk;
666 network.lookup[host.egress] = lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800667 updateLinks();
668 }
669 network.force.start();
670 }
671
Simon Hunt44031102014-11-11 13:20:36 -0800672 // TODO: fold updateX(...) methods into one base method; remove duplication
Simon Hunt56a2ea42014-11-19 12:39:31 -0800673
674 function updateInstance(data) {
675 evTrace(data);
676 var inst = data.payload,
677 id = inst.id,
678 instData = onosInstances[id];
679 if (instData) {
680 $.extend(instData, inst);
681 updateInstances();
Simon Hunt56a2ea42014-11-19 12:39:31 -0800682 } else {
683 logicError('updateInstance lookup fail. ID = "' + id + '"');
684 }
685 }
686
Simon Huntbb282f52014-11-10 11:08:19 -0800687 function updateDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800688 evTrace(data);
Simon Huntbb282f52014-11-10 11:08:19 -0800689 var device = data.payload,
690 id = device.id,
Simon Hunt62c47542014-11-22 22:16:32 -0800691 d = network.lookup[id];
692 if (d) {
693 $.extend(d, device);
694 if (positionNode(d, true)) {
Simon Hunt395a70c2014-11-22 23:17:40 -0800695 sendUpdateMeta(d, true);
Simon Hunt62c47542014-11-22 22:16:32 -0800696 }
697 updateNodes();
Simon Huntbb282f52014-11-10 11:08:19 -0800698 } else {
699 logicError('updateDevice lookup fail. ID = "' + id + '"');
700 }
701 }
702
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800703 function updateLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800704 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800705 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800706 result = findLink(link, 'update'),
707 bad = result.badLogic;
708 if (bad) {
709 logicError(bad + ': ' + link.id);
710 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800711 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800712 result.updateWith(link);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800713 }
714
Simon Hunt7cd48f32014-11-09 23:42:50 -0800715 function updateHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800716 evTrace(data);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800717 var host = data.payload,
Simon Huntbb282f52014-11-10 11:08:19 -0800718 id = host.id,
719 hostData = network.lookup[id];
720 if (hostData) {
721 $.extend(hostData, host);
722 updateHostState(hostData);
723 } else {
724 logicError('updateHost lookup fail. ID = "' + id + '"');
725 }
Simon Hunt7cd48f32014-11-09 23:42:50 -0800726 }
727
Simon Hunt44031102014-11-11 13:20:36 -0800728 // TODO: fold removeX(...) methods into base method - remove dup code
Simon Hunt7b403bc2014-11-22 19:01:00 -0800729 function removeInstance(data) {
730 evTrace(data);
731 var inst = data.payload,
732 id = inst.id,
733 instData = onosInstances[id];
734 if (instData) {
735 var idx = find(id, onosOrder, 'id');
736 if (idx >= 0) {
737 onosOrder.splice(idx, 1);
738 }
739 delete onosInstances[id];
740 updateInstances();
741 } else {
742 logicError('updateInstance lookup fail. ID = "' + id + '"');
743 }
744 }
745
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800746 function removeLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800747 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800748 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800749 result = findLink(link, 'remove'),
750 bad = result.badLogic;
751 if (bad) {
752 logicError(bad + ': ' + link.id);
753 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800754 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800755 result.removeRawLink();
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800756 }
757
Simon Hunt44031102014-11-11 13:20:36 -0800758 function removeHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800759 evTrace(data);
Simon Hunt44031102014-11-11 13:20:36 -0800760 var host = data.payload,
761 id = host.id,
762 hostData = network.lookup[id];
763 if (hostData) {
764 removeHostElement(hostData);
765 } else {
766 logicError('removeHost lookup fail. ID = "' + id + '"');
767 }
768 }
769
Thomas Vachuska47635c62014-11-22 01:21:36 -0800770 function showSummary(data) {
771 evTrace(data);
772 populateSummary(data.payload);
773 summaryPane.show();
774 }
775
Simon Hunt61d04042014-11-11 17:27:16 -0800776 function showDetails(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800777 evTrace(data);
Simon Hunt61d04042014-11-11 17:27:16 -0800778 populateDetails(data.payload);
779 detailPane.show();
780 }
781
Simon Huntb53e0682014-11-12 13:32:01 -0800782 function showTraffic(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800783 evTrace(data);
Thomas Vachuska4731f122014-11-20 04:56:19 -0800784 var paths = data.payload.paths,
785 hasTraffic = false;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800786
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800787 // Revert any links hilighted previously.
Thomas Vachuska4731f122014-11-20 04:56:19 -0800788 link.style('stroke-width', null)
Thomas Vachuskaa3148a72014-11-19 21:38:35 -0800789 .classed('primary secondary animated optical', false);
Simon Hunte2575b62014-11-18 15:25:53 -0800790 // Remove all previous labels.
791 removeLinkLabels();
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800792
Simon Hunte2575b62014-11-18 15:25:53 -0800793 // Now hilight all links in the paths payload, and attach
794 // labels to them, if they are defined.
Simon Hunta255a2c2014-11-13 22:29:35 -0800795 paths.forEach(function (p) {
Simon Hunte2575b62014-11-18 15:25:53 -0800796 var n = p.links.length,
797 i,
798 ldata;
799
Thomas Vachuska4731f122014-11-20 04:56:19 -0800800 hasTraffic = hasTraffic || p.traffic;
Simon Hunte2575b62014-11-18 15:25:53 -0800801 for (i=0; i<n; i++) {
802 ldata = findLinkById(p.links[i]);
Thomas Vachuska4731f122014-11-20 04:56:19 -0800803 if (ldata && ldata.el) {
Simon Hunte2575b62014-11-18 15:25:53 -0800804 ldata.el.classed(p.class, true);
805 ldata.label = p.labels[i];
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800806 }
Simon Hunte2575b62014-11-18 15:25:53 -0800807 }
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800808 });
Thomas Vachuska4731f122014-11-20 04:56:19 -0800809
Simon Hunte2575b62014-11-18 15:25:53 -0800810 updateLinks();
Thomas Vachuska4731f122014-11-20 04:56:19 -0800811
812 if (hasTraffic && !antTimer) {
813 startAntTimer();
814 } else if (!hasTraffic && antTimer) {
815 stopAntTimer();
816 }
Simon Huntb53e0682014-11-12 13:32:01 -0800817 }
818
Simon Hunt56d51852014-11-09 13:03:35 -0800819 // ...............................
820
821 function stillToImplement(data) {
822 var p = data.payload;
823 note(data.event, p.id);
Simon Hunt7b403bc2014-11-22 19:01:00 -0800824 if (!config.useLiveData) {
825 network.view.alert('Not yet implemented: "' + data.event + '"');
826 }
Simon Hunt56d51852014-11-09 13:03:35 -0800827 }
Simon Hunt99c13842014-11-06 18:23:12 -0800828
829 function unknownEvent(data) {
Simon Hunt434cf142014-11-24 11:10:28 -0800830 console.warn('Unknown event type: "' + data.event + '"', data);
Simon Hunt99c13842014-11-06 18:23:12 -0800831 }
832
833 function handleServerEvent(data) {
834 var fn = eventDispatch[data.event] || unknownEvent;
835 fn(data);
836 }
837
838 // ==============================
Simon Hunt61d04042014-11-11 17:27:16 -0800839 // Out-going messages...
840
Simon Huntb53e0682014-11-12 13:32:01 -0800841 function nSel() {
842 return selectOrder.length;
843 }
Simon Hunt61d04042014-11-11 17:27:16 -0800844 function getSel(idx) {
845 return selections[selectOrder[idx]];
846 }
Simon Huntb53e0682014-11-12 13:32:01 -0800847 function getSelId(idx) {
848 return getSel(idx).obj.id;
849 }
Thomas Vachuska9edca302014-11-22 17:06:42 -0800850 function getSelIds(start, endOffset) {
851 var end = selectOrder.length - endOffset;
852 var ids = [];
853 selectOrder.slice(start, end).forEach(function (d) {
854 ids.push(getSelId(d));
855 });
856 return ids;
857 }
Simon Huntb53e0682014-11-12 13:32:01 -0800858 function allSelectionsClass(cls) {
859 for (var i=0, n=nSel(); i<n; i++) {
860 if (getSel(i).obj.class !== cls) {
861 return false;
862 }
863 }
864 return true;
865 }
Simon Hunt61d04042014-11-11 17:27:16 -0800866
Thomas Vachuska47635c62014-11-22 01:21:36 -0800867 function toggleInstances() {
868 if (!oiBox.isVisible()) {
Simon Huntb0ecfa52014-11-23 21:05:12 -0800869 showInstances();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800870 } else {
Simon Huntb0ecfa52014-11-23 21:05:12 -0800871 hideInstances();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800872 }
873 }
874
Simon Huntb0ecfa52014-11-23 21:05:12 -0800875 function showInstances() {
876 oiBox.show();
877 colorAffinity = true;
878 updateDeviceColors();
879 }
880
881 function hideInstances() {
882 oiBox.hide();
883 colorAffinity = false;
884 cancelAffinity();
885 updateDeviceColors();
886 }
887
888
Thomas Vachuska47635c62014-11-22 01:21:36 -0800889 function toggleSummary() {
890 if (!summaryPane.isVisible()) {
891 requestSummary();
892 } else {
893 cancelSummary();
894 }
895 }
896
897 // request overall summary data
898 function requestSummary() {
899 sendMessage('requestSummary', {});
900 }
901
902 function cancelSummary() {
903 sendMessage('cancelSummary', {});
904 summaryPane.hide();
905 }
906
Simon Hunt61d04042014-11-11 17:27:16 -0800907 // request details for the selected element
Simon Huntd72bc702014-11-13 18:38:04 -0800908 // invoked from selection of a single node.
Simon Hunt61d04042014-11-11 17:27:16 -0800909 function requestDetails() {
910 var data = getSel(0).obj,
911 payload = {
912 id: data.id,
913 class: data.class
914 };
915 sendMessage('requestDetails', payload);
916 }
917
Thomas Vachuska9edca302014-11-22 17:06:42 -0800918 function addHostIntentAction() {
Simon Huntd72bc702014-11-13 18:38:04 -0800919 sendMessage('addHostIntent', {
Thomas Vachuska9edca302014-11-22 17:06:42 -0800920 one: selectOrder[0],
921 two: selectOrder[1],
922 ids: selectOrder
Simon Huntd72bc702014-11-13 18:38:04 -0800923 });
Thomas Vachuska9edca302014-11-22 17:06:42 -0800924 network.view.flash('Host-to-Host flow added');
Simon Huntd72bc702014-11-13 18:38:04 -0800925 }
926
Thomas Vachuska9edca302014-11-22 17:06:42 -0800927 function addMultiSourceIntentAction() {
928 sendMessage('addMultiSourceIntent', {
929 src: selectOrder.slice(0, selectOrder.length - 1),
930 dst: selectOrder[selectOrder.length - 1],
931 ids: selectOrder
932 });
933 network.view.flash('Multi-Source flow added');
Thomas Vachuska29617e52014-11-20 03:17:46 -0800934 }
935
Thomas Vachuska9edca302014-11-22 17:06:42 -0800936
Thomas Vachuska47635c62014-11-22 01:21:36 -0800937 function cancelTraffic() {
938 sendMessage('cancelTraffic', {});
939 }
940
Thomas Vachuska9edca302014-11-22 17:06:42 -0800941 function requestTrafficForMode() {
942 if (hoverMode === hoverModeAll) {
943 requestAllTraffic();
944 } else if (hoverMode === hoverModeFlows) {
945 requestDeviceLinkFlows();
946 } else if (hoverMode === hoverModeIntents) {
947 requestSelectTraffic();
Simon Huntd72bc702014-11-13 18:38:04 -0800948 }
Thomas Vachuska9edca302014-11-22 17:06:42 -0800949 }
Simon Huntd72bc702014-11-13 18:38:04 -0800950
Thomas Vachuska9edca302014-11-22 17:06:42 -0800951 function showTrafficAction() {
952 hoverMode = hoverModeIntents;
953 requestSelectTraffic();
954 network.view.flash('Related Traffic');
955 }
956
957 function requestSelectTraffic() {
958 if (validateSelectionContext()) {
959 var hoverId = (hoverMode === hoverModeIntents && hovered &&
960 (hovered.class === 'host' || hovered.class === 'device'))
Simon Huntd72bc702014-11-13 18:38:04 -0800961 ? hovered.id : '';
Thomas Vachuska9edca302014-11-22 17:06:42 -0800962 sendMessage('requestTraffic', {
963 ids: selectOrder,
964 hover: hoverId
965 });
966 }
Simon Huntd72bc702014-11-13 18:38:04 -0800967 }
968
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800969
Thomas Vachuska29617e52014-11-20 03:17:46 -0800970 function showDeviceLinkFlowsAction() {
Thomas Vachuska9edca302014-11-22 17:06:42 -0800971 hoverMode = hoverModeFlows;
972 requestDeviceLinkFlows();
Thomas Vachuskac59658c2014-11-21 13:13:36 -0800973 network.view.flash('Device Flows');
Thomas Vachuska29617e52014-11-20 03:17:46 -0800974 }
975
Thomas Vachuska9edca302014-11-22 17:06:42 -0800976 function requestDeviceLinkFlows() {
977 if (validateSelectionContext()) {
978 var hoverId = (hoverMode === hoverModeFlows && hovered &&
979 (hovered.class === 'device')) ? hovered.id : '';
980 sendMessage('requestDeviceLinkFlows', {
981 ids: selectOrder,
982 hover: hoverId
983 });
984 }
985 }
986
987
988 function showAllTrafficAction() {
989 hoverMode = hoverModeAll;
990 requestAllTraffic();
991 network.view.flash('All Traffic');
992 }
993
994 function requestAllTraffic() {
995 sendMessage('requestAllTraffic', {});
996 }
997
998 function validateSelectionContext() {
Thomas Vachuska29617e52014-11-20 03:17:46 -0800999 if (!hovered && nSel() === 0) {
Thomas Vachuska47635c62014-11-22 01:21:36 -08001000 cancelTraffic();
Thomas Vachuska9edca302014-11-22 17:06:42 -08001001 return false;
Thomas Vachuska29617e52014-11-20 03:17:46 -08001002 }
Thomas Vachuska9edca302014-11-22 17:06:42 -08001003 return true;
Thomas Vachuska29617e52014-11-20 03:17:46 -08001004 }
Simon Huntd72bc702014-11-13 18:38:04 -08001005
Simon Hunta6a9fe72014-11-20 11:17:12 -08001006
Simon Hunt61d04042014-11-11 17:27:16 -08001007 // ==============================
Simon Hunta5e89142014-11-14 07:00:33 -08001008 // onos instance panel functions
Simon Huntb82f6902014-11-22 11:53:15 -08001009
Simon Hunt7b403bc2014-11-22 19:01:00 -08001010 var instCfg = {
1011 rectPad: 8,
1012 nodeOx: 9,
1013 nodeOy: 9,
1014 nodeDim: 40,
1015 birdOx: 19,
1016 birdOy: 21,
1017 birdDim: 21,
1018 uiDy: 45,
1019 titleDy: 30,
1020 textYOff: 20,
1021 textYSpc: 15
1022 };
1023
1024 function viewBox(dim) {
1025 return '0 0 ' + dim.w + ' ' + dim.h;
1026 }
1027
1028 function instRectAttr(dim) {
1029 var pad = instCfg.rectPad;
1030 return {
1031 x: pad,
1032 y: pad,
1033 width: dim.w - pad*2,
1034 height: dim.h - pad*2,
Simon Huntb0ecfa52014-11-23 21:05:12 -08001035 rx: 6
Simon Hunt7b403bc2014-11-22 19:01:00 -08001036 };
1037 }
1038
1039 function computeDim(self) {
1040 var css = window.getComputedStyle(self);
1041 return {
1042 w: stripPx(css.width),
1043 h: stripPx(css.height)
1044 };
Simon Huntb82f6902014-11-22 11:53:15 -08001045 }
Simon Hunta5e89142014-11-14 07:00:33 -08001046
1047 function updateInstances() {
1048 var onoses = oiBox.el.selectAll('.onosInst')
Simon Huntb82f6902014-11-22 11:53:15 -08001049 .data(onosOrder, function (d) { return d.id; }),
Simon Hunt7b403bc2014-11-22 19:01:00 -08001050 instDim = {w:0,h:0},
1051 c = instCfg;
Simon Hunta5e89142014-11-14 07:00:33 -08001052
Simon Hunt7b403bc2014-11-22 19:01:00 -08001053 function nSw(n) {
1054 return '# Switches: ' + n;
1055 }
Simon Hunta5e89142014-11-14 07:00:33 -08001056
Simon Huntb82f6902014-11-22 11:53:15 -08001057 // operate on existing onos instances if necessary
1058 onoses.each(function (d) {
1059 var el = d3.select(this),
1060 svg = el.select('svg');
Simon Hunt7b403bc2014-11-22 19:01:00 -08001061 instDim = computeDim(this);
Simon Huntb82f6902014-11-22 11:53:15 -08001062
1063 // update online state
1064 el.classed('online', d.online);
1065
1066 // update ui-attached state
1067 svg.select('use.uiBadge').remove();
1068 if (d.uiAttached) {
1069 attachUiBadge(svg);
1070 }
1071
Simon Hunt7b403bc2014-11-22 19:01:00 -08001072 function updAttr(id, value) {
1073 svg.select('text.instLabel.'+id).text(value);
1074 }
1075
1076 updAttr('ip', d.ip);
1077 updAttr('ns', nSw(d.switches));
Simon Huntb82f6902014-11-22 11:53:15 -08001078 });
1079
1080
1081 // operate on new onos instances
Simon Hunta5e89142014-11-14 07:00:33 -08001082 var entering = onoses.enter()
1083 .append('div')
1084 .attr('class', 'onosInst')
1085 .classed('online', function (d) { return d.online; })
Simon Hunt9c15eca2014-11-15 18:37:59 -08001086 .on('click', clickInst);
1087
Simon Huntb82f6902014-11-22 11:53:15 -08001088 entering.each(function (d) {
Simon Hunt9c15eca2014-11-15 18:37:59 -08001089 var el = d3.select(this),
Simon Hunt7b403bc2014-11-22 19:01:00 -08001090 rectAttr,
1091 svg;
1092 instDim = computeDim(this);
1093 rectAttr = instRectAttr(instDim);
Simon Hunt9c15eca2014-11-15 18:37:59 -08001094
Simon Hunt7b403bc2014-11-22 19:01:00 -08001095 svg = el.append('svg').attr({
1096 width: instDim.w,
1097 height: instDim.h,
1098 viewBox: viewBox(instDim)
Simon Hunt95908012014-11-20 10:20:26 -08001099 });
Simon Huntb82f6902014-11-22 11:53:15 -08001100
Simon Hunt7b403bc2014-11-22 19:01:00 -08001101 svg.append('rect').attr(rectAttr);
Simon Hunt9c15eca2014-11-15 18:37:59 -08001102
Thomas Vachuskae02e11c2014-11-24 16:13:52 -08001103 //appendGlyph(svg, c.nodeOx, c.nodeOy, c.nodeDim, '#node');
1104 appendBadge(svg, 14, 14, 28, '#bird');
Simon Huntb82f6902014-11-22 11:53:15 -08001105
1106 if (d.uiAttached) {
1107 attachUiBadge(svg);
1108 }
1109
Simon Hunt7b403bc2014-11-22 19:01:00 -08001110 var left = c.nodeOx + c.nodeDim,
1111 len = rectAttr.width - left,
1112 hlen = len / 2,
1113 midline = hlen + left;
Simon Hunt9c15eca2014-11-15 18:37:59 -08001114
Simon Hunt7b403bc2014-11-22 19:01:00 -08001115 // title
1116 svg.append('text')
1117 .attr({
1118 class: 'instTitle',
1119 x: midline,
1120 y: c.titleDy
1121 })
1122 .text(d.id);
1123
1124 // a couple of attributes
1125 var ty = c.titleDy + c.textYOff;
1126
1127 function addAttr(id, label) {
1128 svg.append('text').attr({
1129 class: 'instLabel ' + id,
1130 x: midline,
1131 y: ty
1132 }).text(label);
1133 ty += c.textYSpc;
1134 }
1135
1136 addAttr('ip', d.ip);
1137 addAttr('ns', nSw(d.switches));
Simon Hunt9c15eca2014-11-15 18:37:59 -08001138 });
Simon Hunta5e89142014-11-14 07:00:33 -08001139
1140 // operate on existing + new onoses here
Simon Hunt8f40cce2014-11-23 15:57:30 -08001141 // set the affinity colors...
1142 onoses.each(function (d) {
1143 var el = d3.select(this),
1144 rect = el.select('svg').select('rect'),
1145 col = instColor(d.id, d.online);
1146 rect.style('fill', col);
1147 });
Simon Hunta5e89142014-11-14 07:00:33 -08001148
Simon Hunt7b403bc2014-11-22 19:01:00 -08001149 // adjust the panel size appropriately...
1150 oiBox.width(instDim.w * onosOrder.length);
1151 oiBox.height(instDim.h);
1152
1153 // remove any outgoing instances
1154 onoses.exit().remove();
Simon Hunta5e89142014-11-14 07:00:33 -08001155 }
1156
Simon Hunt8f40cce2014-11-23 15:57:30 -08001157 function instColor(id, online) {
1158 return cat7.get(id, !online, network.view.getTheme());
1159 }
1160
Simon Hunt9462e8c2014-11-14 17:28:09 -08001161 function clickInst(d) {
1162 var el = d3.select(this),
1163 aff = el.classed('affinity');
1164 if (!aff) {
1165 setAffinity(el, d);
1166 } else {
1167 cancelAffinity();
1168 }
1169 }
1170
1171 function setAffinity(el, d) {
1172 d3.selectAll('.onosInst')
1173 .classed('mastership', true)
1174 .classed('affinity', false);
1175 el.classed('affinity', true);
1176
1177 suppressLayers(true);
1178 node.each(function (n) {
1179 if (n.master === d.id) {
1180 n.el.classed('suppressed', false);
1181 }
1182 });
1183 oiShowMaster = true;
1184 }
1185
1186 function cancelAffinity() {
1187 d3.selectAll('.onosInst')
1188 .classed('mastership affinity', false);
1189 restoreLayerState();
1190 oiShowMaster = false;
1191 }
1192
Simon Hunt7b403bc2014-11-22 19:01:00 -08001193 // TODO: these should be moved out to utility module.
1194 function stripPx(s) {
1195 return s.replace(/px$/,'');
1196 }
1197
1198 function appendUse(svg, ox, oy, dim, iid, cls) {
1199 var use = svg.append('use').attr({
1200 transform: translate(ox,oy),
1201 'xlink:href': iid,
1202 width: dim,
1203 height: dim
1204 });
1205 if (cls) {
1206 use.classed(cls, true);
1207 }
1208 return use;
1209 }
1210
1211 function appendGlyph(svg, ox, oy, dim, iid, cls) {
1212 appendUse(svg, ox, oy, dim, iid, cls).classed('glyphIcon', true);
1213 }
1214
1215 function appendBadge(svg, ox, oy, dim, iid, cls) {
1216 appendUse(svg, ox, oy, dim, iid,cls ).classed('badgeIcon', true);
1217 }
1218
1219 function attachUiBadge(svg) {
1220 appendBadge(svg, 12, instCfg.uiDy, 30, '#uiAttached', 'uiBadge');
1221 }
1222
Simon Hunt434cf142014-11-24 11:10:28 -08001223 function visVal(b) {
1224 return b ? 'visible' : 'hidden';
1225 }
1226
Simon Hunta5e89142014-11-14 07:00:33 -08001227 // ==============================
Simon Hunt99c13842014-11-06 18:23:12 -08001228 // force layout modification functions
1229
1230 function translate(x, y) {
1231 return 'translate(' + x + ',' + y + ')';
1232 }
1233
Simon Hunte2575b62014-11-18 15:25:53 -08001234 function rotate(deg) {
1235 return 'rotate(' + deg + ')';
1236 }
1237
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001238 function missMsg(what, id) {
1239 return '\n[' + what + '] "' + id + '" missing ';
1240 }
1241
1242 function linkEndPoints(srcId, dstId) {
1243 var srcNode = network.lookup[srcId],
1244 dstNode = network.lookup[dstId],
1245 sMiss = !srcNode ? missMsg('src', srcId) : '',
1246 dMiss = !dstNode ? missMsg('dst', dstId) : '';
1247
1248 if (sMiss || dMiss) {
1249 logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
1250 return null;
1251 }
1252 return {
1253 source: srcNode,
1254 target: dstNode,
1255 x1: srcNode.x,
1256 y1: srcNode.y,
1257 x2: dstNode.x,
1258 y2: dstNode.y
1259 };
1260 }
1261
Simon Hunt56d51852014-11-09 13:03:35 -08001262 function createHostLink(host) {
1263 var src = host.id,
1264 dst = host.cp.device,
Simon Hunt7cd48f32014-11-09 23:42:50 -08001265 id = host.ingress,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001266 lnk = linkEndPoints(src, dst);
Simon Hunt56d51852014-11-09 13:03:35 -08001267
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001268 if (!lnk) {
Simon Hunt56d51852014-11-09 13:03:35 -08001269 return null;
1270 }
1271
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001272 // Synthesize link ...
1273 $.extend(lnk, {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001274 key: id,
Simon Hunt56d51852014-11-09 13:03:35 -08001275 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -08001276
1277 type: function () { return 'hostLink'; },
1278 // TODO: ideally, we should see if our edge switch is online...
1279 online: function () { return true; },
1280 linkWidth: function () { return 1; }
Simon Hunt7cd48f32014-11-09 23:42:50 -08001281 });
Simon Hunt99c13842014-11-06 18:23:12 -08001282 return lnk;
1283 }
1284
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001285 function createLink(link) {
Simon Hunta6a9fe72014-11-20 11:17:12 -08001286 var lnk = linkEndPoints(link.src, link.dst);
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001287
1288 if (!lnk) {
1289 return null;
1290 }
1291
Simon Hunt8257f4c2014-11-16 19:34:54 -08001292 $.extend(lnk, {
1293 key: link.id,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001294 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -08001295 fromSource: link,
1296
1297 // functions to aggregate dual link state
1298 type: function () {
1299 var s = lnk.fromSource,
1300 t = lnk.fromTarget;
1301 return (s && s.type) || (t && t.type) || defaultLinkType;
1302 },
1303 online: function () {
1304 var s = lnk.fromSource,
1305 t = lnk.fromTarget;
1306 return (s && s.online) || (t && t.online);
1307 },
1308 linkWidth: function () {
1309 var s = lnk.fromSource,
1310 t = lnk.fromTarget,
1311 ws = (s && s.linkWidth) || 0,
1312 wt = (t && t.linkWidth) || 0;
1313 return Math.max(ws, wt);
1314 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001315 });
1316 return lnk;
Simon Hunt1a9eff92014-11-07 11:06:34 -08001317 }
1318
Simon Hunte2575b62014-11-18 15:25:53 -08001319 function removeLinkLabels() {
1320 network.links.forEach(function (d) {
1321 d.label = '';
1322 });
1323 }
1324
Simon Hunt434cf142014-11-24 11:10:28 -08001325 function showHostVis(el) {
1326 el.style('visibility', visVal(showHosts));
1327 }
1328
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001329 var widthRatio = 1.4,
1330 linkScale = d3.scale.linear()
1331 .domain([1, 12])
1332 .range([widthRatio, 12 * widthRatio])
1333 .clamp(true);
1334
Simon Hunt99c13842014-11-06 18:23:12 -08001335 function updateLinks() {
1336 link = linkG.selectAll('.link')
Simon Hunt8257f4c2014-11-16 19:34:54 -08001337 .data(network.links, function (d) { return d.key; });
Simon Hunt99c13842014-11-06 18:23:12 -08001338
1339 // operate on existing links, if necessary
1340 // link .foo() .bar() ...
1341
1342 // operate on entering links:
1343 var entering = link.enter()
1344 .append('line')
1345 .attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001346 x1: function (d) { return d.x1; },
1347 y1: function (d) { return d.y1; },
1348 x2: function (d) { return d.x2; },
1349 y2: function (d) { return d.y2; },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001350 stroke: config.topo.linkInColor,
1351 'stroke-width': config.topo.linkInWidth
Simon Hunt99c13842014-11-06 18:23:12 -08001352 });
1353
1354 // augment links
Simon Hunt7cd48f32014-11-09 23:42:50 -08001355 entering.each(function (d) {
1356 var link = d3.select(this);
1357 // provide ref to element selection from backing data....
1358 d.el = link;
Simon Hunt8257f4c2014-11-16 19:34:54 -08001359 restyleLinkElement(d);
Simon Hunt434cf142014-11-24 11:10:28 -08001360 if (d.type() === 'hostLink') {
1361 showHostVis(link);
1362 }
Simon Hunt7cd48f32014-11-09 23:42:50 -08001363 });
Thomas Vachuska4830d392014-11-09 17:09:56 -08001364
1365 // operate on both existing and new links, if necessary
1366 //link .foo() .bar() ...
1367
Simon Hunte2575b62014-11-18 15:25:53 -08001368 // apply or remove labels
1369 var labelData = getLabelData();
1370 applyLinkLabels(labelData);
1371
Thomas Vachuska4830d392014-11-09 17:09:56 -08001372 // operate on exiting links:
Thomas Vachuska4830d392014-11-09 17:09:56 -08001373 link.exit()
Simon Hunt13bf9c82014-11-18 07:26:44 -08001374 .attr('stroke-dasharray', '3, 3')
1375 .style('opacity', 0.5)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001376 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -08001377 .duration(1500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001378 .attr({
Simon Hunt13bf9c82014-11-18 07:26:44 -08001379 'stroke-dasharray': '3, 12',
1380 stroke: config.topo.linkOutColor,
1381 'stroke-width': config.topo.linkOutWidth
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001382 })
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001383 .style('opacity', 0.0)
Thomas Vachuska4830d392014-11-09 17:09:56 -08001384 .remove();
Simon Hunte2575b62014-11-18 15:25:53 -08001385
1386 // NOTE: invoke a single tick to force the labels to position
1387 // onto their links.
1388 tick();
1389 }
1390
1391 function getLabelData() {
1392 // create the backing data for showing labels..
1393 var data = [];
1394 link.each(function (d) {
1395 if (d.label) {
1396 data.push({
1397 id: 'lab-' + d.key,
1398 key: d.key,
1399 label: d.label,
1400 ldata: d
1401 });
1402 }
1403 });
1404 return data;
1405 }
1406
1407 var linkLabelOffset = '0.3em';
1408
1409 function applyLinkLabels(data) {
1410 var entering;
1411
1412 linkLabel = linkLabelG.selectAll('.linkLabel')
1413 .data(data, function (d) { return d.id; });
1414
Simon Hunt56a2ea42014-11-19 12:39:31 -08001415 // for elements already existing, we need to update the text
1416 // and adjust the rectangle size to fit
1417 linkLabel.each(function (d) {
1418 var el = d3.select(this),
1419 rect = el.select('rect'),
1420 text = el.select('text');
1421 text.text(d.label);
1422 rect.attr(rectAroundText(el));
1423 });
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -08001424
Simon Hunte2575b62014-11-18 15:25:53 -08001425 entering = linkLabel.enter().append('g')
1426 .classed('linkLabel', true)
1427 .attr('id', function (d) { return d.id; });
1428
1429 entering.each(function (d) {
1430 var el = d3.select(this),
1431 rect,
1432 text,
1433 parms = {
1434 x1: d.ldata.x1,
1435 y1: d.ldata.y1,
1436 x2: d.ldata.x2,
1437 y2: d.ldata.y2
1438 };
1439
1440 d.el = el;
1441 rect = el.append('rect');
1442 text = el.append('text').text(d.label);
1443 rect.attr(rectAroundText(el));
1444 text.attr('dy', linkLabelOffset);
1445
1446 el.attr('transform', transformLabel(parms));
1447 });
1448
1449 // Remove any links that are no longer required.
1450 linkLabel.exit().remove();
1451 }
1452
1453 function rectAroundText(el) {
1454 var text = el.select('text'),
1455 box = text.node().getBBox();
1456
1457 // translate the bbox so that it is centered on [x,y]
1458 box.x = -box.width / 2;
1459 box.y = -box.height / 2;
1460
1461 // add padding
1462 box.x -= 1;
1463 box.width += 2;
1464 return box;
1465 }
1466
1467 function transformLabel(p) {
1468 var dx = p.x2 - p.x1,
1469 dy = p.y2 - p.y1,
1470 xMid = dx/2 + p.x1,
1471 yMid = dy/2 + p.y1;
Simon Hunte2575b62014-11-18 15:25:53 -08001472 return translate(xMid, yMid);
Simon Hunt99c13842014-11-06 18:23:12 -08001473 }
1474
1475 function createDeviceNode(device) {
1476 // start with the object as is
1477 var node = device,
Simon Huntbb282f52014-11-10 11:08:19 -08001478 type = device.type,
Simon Huntc72967b2014-11-20 09:21:42 -08001479 svgCls = type ? 'node device ' + type : 'node device',
1480 labels = device.labels || [];
1481
Simon Hunt99c13842014-11-06 18:23:12 -08001482 // Augment as needed...
1483 node.class = 'device';
Simon Huntbb282f52014-11-10 11:08:19 -08001484 node.svgClass = device.online ? svgCls + ' online' : svgCls;
Simon Hunt99c13842014-11-06 18:23:12 -08001485 positionNode(node);
Simon Hunt99c13842014-11-06 18:23:12 -08001486 return node;
1487 }
1488
Simon Hunt56d51852014-11-09 13:03:35 -08001489 function createHostNode(host) {
1490 // start with the object as is
1491 var node = host;
1492
1493 // Augment as needed...
1494 node.class = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001495 if (!node.type) {
Simon Hunt209155e2014-11-21 12:16:09 -08001496 node.type = 'endstation';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001497 }
Simon Hunt7fa116d2014-11-17 14:16:55 -08001498 node.svgClass = 'node host ' + node.type;
Simon Hunt56d51852014-11-09 13:03:35 -08001499 positionNode(node);
Simon Hunt56d51852014-11-09 13:03:35 -08001500 return node;
1501 }
1502
Simon Hunt62c47542014-11-22 22:16:32 -08001503 function positionNode(node, forUpdate) {
Simon Hunt99c13842014-11-06 18:23:12 -08001504 var meta = node.metaUi,
Simon Huntac9e24f2014-11-12 10:12:21 -08001505 x = meta && meta.x,
1506 y = meta && meta.y,
1507 xy;
Simon Hunt99c13842014-11-06 18:23:12 -08001508
Simon Huntac9e24f2014-11-12 10:12:21 -08001509 // If we have [x,y] already, use that...
Simon Hunt99c13842014-11-06 18:23:12 -08001510 if (x && y) {
1511 node.fixed = true;
Simon Hunt62c47542014-11-22 22:16:32 -08001512 node.px = node.x = x;
1513 node.py = node.y = y;
1514 //node.px = x;
1515 //node.py = y;
Simon Huntac9e24f2014-11-12 10:12:21 -08001516 return;
Simon Hunt99c13842014-11-06 18:23:12 -08001517 }
Simon Huntac9e24f2014-11-12 10:12:21 -08001518
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001519 var location = node.location;
1520 if (location && location.type === 'latlng') {
1521 var coord = geoMapProjection([location.lng, location.lat]);
1522 node.fixed = true;
Simon Hunt62c47542014-11-22 22:16:32 -08001523 node.px = node.x = coord[0];
1524 node.py = node.y = coord[1];
1525 //node.x = coord[0];
1526 //node.y = coord[1];
1527 return true;
1528 }
1529
1530 // if this is a node update (not a node add).. skip randomizer
1531 if (forUpdate) {
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001532 return;
1533 }
1534
Simon Huntac9e24f2014-11-12 10:12:21 -08001535 // Note: Placing incoming unpinned nodes at exactly the same point
1536 // (center of the view) causes them to explode outwards when
1537 // the force layout kicks in. So, we spread them out a bit
1538 // initially, to provide a more serene layout convergence.
1539 // Additionally, if the node is a host, we place it near
1540 // the device it is connected to.
1541
1542 function spread(s) {
1543 return Math.floor((Math.random() * s) - s/2);
1544 }
1545
1546 function randDim(dim) {
1547 return dim / 2 + spread(dim * 0.7071);
1548 }
1549
1550 function rand() {
1551 return {
1552 x: randDim(network.view.width()),
1553 y: randDim(network.view.height())
1554 };
1555 }
1556
1557 function near(node) {
1558 var min = 12,
1559 dx = spread(12),
1560 dy = spread(12);
1561 return {
1562 x: node.x + min + dx,
1563 y: node.y + min + dy
1564 };
1565 }
1566
1567 function getDevice(cp) {
1568 var d = network.lookup[cp.device];
1569 return d || rand();
1570 }
1571
1572 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
1573 $.extend(node, xy);
Simon Hunt99c13842014-11-06 18:23:12 -08001574 }
1575
Simon Hunt99c13842014-11-06 18:23:12 -08001576
Simon Huntc72967b2014-11-20 09:21:42 -08001577 function iconGlyphUrl(d) {
1578 var which = d.type || 'unknown';
1579 return '#' + which;
1580 }
1581
Simon Hunt99c13842014-11-06 18:23:12 -08001582 // returns the newly computed bounding box of the rectangle
1583 function adjustRectToFitText(n) {
1584 var text = n.select('text'),
1585 box = text.node().getBBox(),
1586 lab = config.labels;
1587
1588 text.attr('text-anchor', 'middle')
1589 .attr('y', '-0.8em')
1590 .attr('x', lab.imgPad/2);
1591
1592 // translate the bbox so that it is centered on [x,y]
1593 box.x = -box.width / 2;
1594 box.y = -box.height / 2;
1595
1596 // add padding
1597 box.x -= (lab.padLR + lab.imgPad/2);
1598 box.width += lab.padLR * 2 + lab.imgPad;
1599 box.y -= lab.padTB;
1600 box.height += lab.padTB * 2;
1601
1602 return box;
1603 }
1604
Simon Hunt1a9eff92014-11-07 11:06:34 -08001605 function mkSvgClass(d) {
1606 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
1607 }
1608
Simon Hunt7cd48f32014-11-09 23:42:50 -08001609 function hostLabel(d) {
1610 var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0;
1611 return d.labels[idx];
1612 }
1613 function deviceLabel(d) {
1614 var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0;
1615 return d.labels[idx];
1616 }
Simon Huntc72967b2014-11-20 09:21:42 -08001617 function trimLabel(label) {
1618 return (label && label.trim()) || '';
1619 }
1620
1621 function emptyBox() {
1622 return {
1623 x: -2,
1624 y: -2,
1625 width: 4,
1626 height: 4
1627 };
Simon Hunt7cd48f32014-11-09 23:42:50 -08001628 }
1629
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001630 function updateDeviceLabel(d) {
Simon Huntc72967b2014-11-20 09:21:42 -08001631 var label = trimLabel(deviceLabel(d)),
1632 noLabel = !label,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001633 node = d.el,
Simon Huntc72967b2014-11-20 09:21:42 -08001634 box,
1635 dx,
Simon Hunt395a70c2014-11-22 23:17:40 -08001636 dy,
1637 cfg = config.icons.device;
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001638
1639 node.select('text')
1640 .text(label)
1641 .style('opacity', 0)
1642 .transition()
1643 .style('opacity', 1);
1644
Simon Huntc72967b2014-11-20 09:21:42 -08001645 if (noLabel) {
1646 box = emptyBox();
Simon Hunt395a70c2014-11-22 23:17:40 -08001647 dx = -cfg.dim/2;
1648 dy = -cfg.dim/2;
Simon Huntc72967b2014-11-20 09:21:42 -08001649 } else {
1650 box = adjustRectToFitText(node);
Simon Hunt395a70c2014-11-22 23:17:40 -08001651 dx = box.x + cfg.xoff;
1652 dy = box.y + cfg.yoff;
Simon Huntc72967b2014-11-20 09:21:42 -08001653 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001654
1655 node.select('rect')
1656 .transition()
1657 .attr(box);
1658
Simon Huntc72967b2014-11-20 09:21:42 -08001659 node.select('g.deviceIcon')
Thomas Vachuska89543292014-11-19 11:28:33 -08001660 .transition()
Simon Huntc72967b2014-11-20 09:21:42 -08001661 .attr('transform', translate(dx, dy));
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001662 }
1663
1664 function updateHostLabel(d) {
1665 var label = hostLabel(d),
1666 host = d.el;
1667
1668 host.select('text').text(label);
1669 }
1670
Simon Hunt62c47542014-11-22 22:16:32 -08001671 // FIXME : fold this into updateNodes.
Simon Huntbb282f52014-11-10 11:08:19 -08001672 function updateHostState(hostData) {
1673 updateHostLabel(hostData);
Simon Huntbb282f52014-11-10 11:08:19 -08001674 }
1675
Simon Hunt434cf142014-11-24 11:10:28 -08001676 function updateHostVisibility() {
1677 var v = visVal(showHosts);
1678 nodeG.selectAll('.host').style('visibility', v);
1679 linkG.selectAll('.hostLink').style('visibility', v);
1680 }
1681
Simon Hunt6ac93f32014-11-13 12:17:27 -08001682 function nodeMouseOver(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001683 hovered = d;
Thomas Vachuska9edca302014-11-22 17:06:42 -08001684 requestTrafficForMode();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001685 }
1686
1687 function nodeMouseOut(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001688 hovered = null;
Thomas Vachuska9edca302014-11-22 17:06:42 -08001689 requestTrafficForMode();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001690 }
Simon Huntbb282f52014-11-10 11:08:19 -08001691
Simon Hunteb1514d2014-11-20 09:57:29 -08001692 function addHostIcon(node, radius, iid) {
Thomas Vachuska89543292014-11-19 11:28:33 -08001693 var dim = radius * 1.5,
1694 xlate = -dim / 2;
1695
Simon Hunteb1514d2014-11-20 09:57:29 -08001696 node.append('use').attr({
1697 class: 'glyphIcon hostIcon',
1698 transform: translate(xlate,xlate),
1699 'xlink:href': iid,
1700 width: dim,
1701 height: dim
1702 });
Thomas Vachuska89543292014-11-19 11:28:33 -08001703 }
1704
Simon Hunt99c13842014-11-06 18:23:12 -08001705 function updateNodes() {
1706 node = nodeG.selectAll('.node')
1707 .data(network.nodes, function (d) { return d.id; });
1708
Simon Hunt62c47542014-11-22 22:16:32 -08001709 // operate on existing nodes...
1710 node.filter('.device').each(function (d) {
Simon Hunt62c47542014-11-22 22:16:32 -08001711 var node = d.el;
1712 node.classed('online', d.online);
1713 updateDeviceLabel(d);
1714 positionNode(d, true);
1715 });
1716
1717 node.filter('.host').each(function (d) {
Simon Hunt62c47542014-11-22 22:16:32 -08001718 var node = d.el;
1719 // TODO: appropriate update of host visuals
1720 });
Simon Hunt99c13842014-11-06 18:23:12 -08001721
1722 // operate on entering nodes:
1723 var entering = node.enter()
1724 .append('g')
1725 .attr({
1726 id: function (d) { return safeId(d.id); },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001727 class: mkSvgClass,
Simon Hunt99c13842014-11-06 18:23:12 -08001728 transform: function (d) { return translate(d.x, d.y); },
1729 opacity: 0
1730 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08001731 .call(network.drag)
Simon Hunt6ac93f32014-11-13 12:17:27 -08001732 .on('mouseover', nodeMouseOver)
1733 .on('mouseout', nodeMouseOut)
Simon Hunt99c13842014-11-06 18:23:12 -08001734 .transition()
1735 .attr('opacity', 1);
1736
1737 // augment device nodes...
1738 entering.filter('.device').each(function (d) {
1739 var node = d3.select(this),
Simon Huntc72967b2014-11-20 09:21:42 -08001740 label = trimLabel(deviceLabel(d)),
1741 noLabel = !label,
Simon Hunt99c13842014-11-06 18:23:12 -08001742 box;
1743
Simon Hunt7cd48f32014-11-09 23:42:50 -08001744 // provide ref to element from backing data....
1745 d.el = node;
1746
Simon Hunt62c47542014-11-22 22:16:32 -08001747 node.append('rect').attr({ rx: 5, ry: 5 });
1748 node.append('text').text(label).attr('dy', '1.1em');
Simon Hunt99c13842014-11-06 18:23:12 -08001749 box = adjustRectToFitText(node);
Simon Hunta3dd9572014-11-20 15:22:41 -08001750 node.select('rect').attr(box);
Simon Huntc72967b2014-11-20 09:21:42 -08001751 addDeviceIcon(node, box, noLabel, iconGlyphUrl(d));
Simon Huntc7ee0662014-11-05 16:44:37 -08001752 });
Simon Hunt934c3ce2014-11-05 11:45:07 -08001753
Thomas Vachuska89543292014-11-19 11:28:33 -08001754
Simon Hunt56d51852014-11-09 13:03:35 -08001755 // augment host nodes...
1756 entering.filter('.host').each(function (d) {
1757 var node = d3.select(this),
Simon Hunt395a70c2014-11-22 23:17:40 -08001758 cfg = config.icons.host,
1759 r = cfg.radius[d.type] || cfg.defaultRadius,
Thomas Vachuska89543292014-11-19 11:28:33 -08001760 textDy = r + 10,
Simon Hunteb1514d2014-11-20 09:57:29 -08001761 iid = iconGlyphUrl(d);
Simon Hunt56d51852014-11-09 13:03:35 -08001762
Simon Hunt7cd48f32014-11-09 23:42:50 -08001763 // provide ref to element from backing data....
1764 d.el = node;
Simon Hunt434cf142014-11-24 11:10:28 -08001765 showHostVis(node);
Simon Hunt7cd48f32014-11-09 23:42:50 -08001766
Simon Hunt62c47542014-11-22 22:16:32 -08001767 node.append('circle').attr('r', r);
Simon Hunteb1514d2014-11-20 09:57:29 -08001768 if (iid) {
1769 addHostIcon(node, r, iid);
Simon Hunt7fa116d2014-11-17 14:16:55 -08001770 }
Simon Hunt56d51852014-11-09 13:03:35 -08001771 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001772 .text(hostLabel)
Thomas Vachuska89543292014-11-19 11:28:33 -08001773 .attr('dy', textDy)
Simon Hunt7cd48f32014-11-09 23:42:50 -08001774 .attr('text-anchor', 'middle');
Simon Hunt56d51852014-11-09 13:03:35 -08001775 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001776
Simon Hunt99c13842014-11-06 18:23:12 -08001777 // operate on both existing and new nodes, if necessary
Simon Huntb0ecfa52014-11-23 21:05:12 -08001778 updateDeviceColors();
Simon Huntc7ee0662014-11-05 16:44:37 -08001779
Simon Hunt99c13842014-11-06 18:23:12 -08001780 // operate on exiting nodes:
Simon Huntea80eb42014-11-11 13:46:57 -08001781 // Note that the node is removed after 2 seconds.
1782 // Sub element animations should be shorter than 2 seconds.
1783 var exiting = node.exit()
Simon Hunt44031102014-11-11 13:20:36 -08001784 .transition()
1785 .duration(2000)
Simon Huntea80eb42014-11-11 13:46:57 -08001786 .style('opacity', 0)
Simon Hunt99c13842014-11-06 18:23:12 -08001787 .remove();
Simon Huntea80eb42014-11-11 13:46:57 -08001788
1789 // host node exits....
1790 exiting.filter('.host').each(function (d) {
1791 var node = d3.select(this);
1792
1793 node.select('text')
1794 .style('opacity', 0.5)
1795 .transition()
1796 .duration(1000)
1797 .style('opacity', 0);
1798 // note, leave <g>.remove to remove this element
1799
Thomas Vachuska89543292014-11-19 11:28:33 -08001800 node.select('circle')
1801 .style('stroke-fill', '#555')
1802 .style('fill', '#888')
Simon Huntea80eb42014-11-11 13:46:57 -08001803 .style('opacity', 0.5)
1804 .transition()
1805 .duration(1500)
1806 .attr('r', 0);
1807 // note, leave <g>.remove to remove this element
1808
1809 });
1810
Simon Hunt62c47542014-11-22 22:16:32 -08001811 // TODO: device node exit animation
1812
1813 network.force.resume();
Simon Huntc7ee0662014-11-05 16:44:37 -08001814 }
1815
Simon Hunt95dad922014-11-24 09:43:31 -08001816 var dCol = {
1817 black: '#000',
1818 paleblue: '#acf',
1819 offwhite: '#ddd',
1820 midgrey: '#888',
1821 lightgrey: '#bbb',
1822 orange: '#f90'
1823 };
1824
Simon Huntb0ecfa52014-11-23 21:05:12 -08001825 // note: these are the device icon colors without affinity
Simon Hunt95dad922014-11-24 09:43:31 -08001826 var dColTheme = {
Simon Huntb0ecfa52014-11-23 21:05:12 -08001827 light: {
1828 online: {
Simon Hunt95dad922014-11-24 09:43:31 -08001829 glyph: dCol.black,
1830 rect: dCol.paleblue
Simon Huntb0ecfa52014-11-23 21:05:12 -08001831 },
1832 offline: {
Simon Hunt95dad922014-11-24 09:43:31 -08001833 glyph: dCol.midgrey,
1834 rect: dCol.lightgrey
Simon Huntb0ecfa52014-11-23 21:05:12 -08001835 }
1836 },
Simon Hunt95dad922014-11-24 09:43:31 -08001837 // TODO: theme
Simon Huntb0ecfa52014-11-23 21:05:12 -08001838 dark: {
1839 online: {
Simon Hunt95dad922014-11-24 09:43:31 -08001840 glyph: dCol.black,
1841 rect: dCol.paleblue
Simon Huntb0ecfa52014-11-23 21:05:12 -08001842 },
1843 offline: {
Simon Hunt95dad922014-11-24 09:43:31 -08001844 glyph: dCol.midgrey,
1845 rect: dCol.lightgrey
Simon Huntb0ecfa52014-11-23 21:05:12 -08001846 }
1847 }
1848 };
1849
1850 function devBaseColor(d) {
1851 var t = network.view.getTheme(),
1852 o = d.online ? 'online' : 'offline';
Simon Hunt95dad922014-11-24 09:43:31 -08001853 return dColTheme[t][o];
Simon Huntb0ecfa52014-11-23 21:05:12 -08001854 }
1855
1856 function setDeviceColor(d) {
1857 var o = d.online,
1858 s = d.el.classed('selected'),
1859 c = devBaseColor(d),
1860 a = instColor(d.master, o),
1861 g, r,
1862 icon = d.el.select('g.deviceIcon');
1863
1864 if (s) {
1865 g = c.glyph;
Simon Hunt95dad922014-11-24 09:43:31 -08001866 r = dColTheme.sel;
Simon Huntb0ecfa52014-11-23 21:05:12 -08001867 } else if (colorAffinity) {
1868 g = o ? a : c.glyph;
Simon Hunt95dad922014-11-24 09:43:31 -08001869 r = o ? dCol.offwhite : a;
Simon Huntb0ecfa52014-11-23 21:05:12 -08001870 } else {
1871 g = c.glyph;
1872 r = c.rect;
1873 }
1874
1875 icon.select('use')
1876 .style('fill', g);
1877 icon.select('rect')
1878 .style('fill', r);
1879 }
1880
Simon Huntc72967b2014-11-20 09:21:42 -08001881 function addDeviceIcon(node, box, noLabel, iid) {
1882 var cfg = config.icons.device,
1883 dx,
1884 dy,
1885 g;
1886
1887 if (noLabel) {
1888 box = emptyBox();
1889 dx = -cfg.dim/2;
1890 dy = -cfg.dim/2;
1891 } else {
1892 box = adjustRectToFitText(node);
Simon Hunt395a70c2014-11-22 23:17:40 -08001893 dx = box.x + cfg.xoff;
1894 dy = box.y + cfg.yoff;
Simon Huntc72967b2014-11-20 09:21:42 -08001895 }
1896
Simon Hunteb1514d2014-11-20 09:57:29 -08001897 g = node.append('g')
1898 .attr('class', 'glyphIcon deviceIcon')
Simon Huntc72967b2014-11-20 09:21:42 -08001899 .attr('transform', translate(dx, dy));
1900
1901 g.append('rect').attr({
1902 x: 0,
1903 y: 0,
1904 rx: cfg.rx,
1905 width: cfg.dim,
1906 height: cfg.dim
1907 });
1908
1909 g.append('use').attr({
1910 'xlink:href': iid,
1911 width: cfg.dim,
1912 height: cfg.dim
1913 });
1914
Simon Huntc72967b2014-11-20 09:21:42 -08001915 }
1916
Simon Hunt7b403bc2014-11-22 19:01:00 -08001917 function find(key, array, tag) {
1918 var _tag = tag || 'key',
1919 idx, n, d;
1920 for (idx = 0, n = array.length; idx < n; idx++) {
1921 d = array[idx];
1922 if (d[_tag] === key) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001923 return idx;
1924 }
1925 }
1926 return -1;
1927 }
1928
1929 function removeLinkElement(linkData) {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001930 var idx = find(linkData.key, network.links),
1931 removed;
1932 if (idx >=0) {
1933 // remove from links array
1934 removed = network.links.splice(idx, 1);
1935 // remove from lookup cache
1936 delete network.lookup[removed[0].key];
1937 updateLinks();
1938 network.force.resume();
1939 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001940 }
Simon Huntc7ee0662014-11-05 16:44:37 -08001941
Simon Hunt44031102014-11-11 13:20:36 -08001942 function removeHostElement(hostData) {
1943 // first, remove associated hostLink...
1944 removeLinkElement(hostData.linkData);
1945
1946 // remove from lookup cache
1947 delete network.lookup[hostData.id];
1948 // remove from nodes array
1949 var idx = find(hostData.id, network.nodes);
1950 network.nodes.splice(idx, 1);
1951 // remove from SVG
1952 updateNodes();
1953 network.force.resume();
1954 }
1955
1956
Simon Huntc7ee0662014-11-05 16:44:37 -08001957 function tick() {
1958 node.attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001959 transform: function (d) { return translate(d.x, d.y); }
Simon Huntc7ee0662014-11-05 16:44:37 -08001960 });
1961
1962 link.attr({
1963 x1: function (d) { return d.source.x; },
1964 y1: function (d) { return d.source.y; },
1965 x2: function (d) { return d.target.x; },
1966 y2: function (d) { return d.target.y; }
1967 });
Simon Hunte2575b62014-11-18 15:25:53 -08001968
1969 linkLabel.each(function (d) {
1970 var el = d3.select(this);
Thomas Vachuska4731f122014-11-20 04:56:19 -08001971 var lnk = findLinkById(d.key);
1972
1973 if (lnk) {
1974 var parms = {
Simon Hunte2575b62014-11-18 15:25:53 -08001975 x1: lnk.source.x,
1976 y1: lnk.source.y,
1977 x2: lnk.target.x,
1978 y2: lnk.target.y
1979 };
Thomas Vachuska4731f122014-11-20 04:56:19 -08001980 el.attr('transform', transformLabel(parms));
1981 }
Simon Hunte2575b62014-11-18 15:25:53 -08001982 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001983 }
Simon Hunt934c3ce2014-11-05 11:45:07 -08001984
1985 // ==============================
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001986 // Web-Socket for live data
1987
1988 function webSockUrl() {
1989 return document.location.toString()
1990 .replace(/\#.*/, '')
1991 .replace('http://', 'ws://')
1992 .replace('https://', 'wss://')
Simon Hunte5ab1382014-11-25 10:28:51 -08001993 .replace('index.html', config.webSockUrl);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001994 }
1995
1996 webSock = {
1997 ws : null,
1998
1999 connect : function() {
2000 webSock.ws = new WebSocket(webSockUrl());
2001
2002 webSock.ws.onopen = function() {
Simon Hunt0c6d4192014-11-12 12:07:10 -08002003 noWebSock(false);
Thomas Vachuska47635c62014-11-22 01:21:36 -08002004 requestSummary();
Simon Huntb0ecfa52014-11-23 21:05:12 -08002005 showInstances();
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002006 };
2007
2008 webSock.ws.onmessage = function(m) {
2009 if (m.data) {
Simon Huntbb282f52014-11-10 11:08:19 -08002010 wsTraceRx(m.data);
Thomas Vachuskad472c6e2014-11-07 19:11:05 -08002011 handleServerEvent(JSON.parse(m.data));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002012 }
2013 };
2014
2015 webSock.ws.onclose = function(m) {
2016 webSock.ws = null;
Simon Hunt0c6d4192014-11-12 12:07:10 -08002017 noWebSock(true);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002018 };
2019 },
2020
2021 send : function(text) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002022 if (text != null) {
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002023 webSock._send(text);
2024 }
2025 },
2026
2027 _send : function(message) {
2028 if (webSock.ws) {
2029 webSock.ws.send(message);
Simon Hunta255a2c2014-11-13 22:29:35 -08002030 } else if (config.useLiveData) {
Simon Hunt434cf142014-11-24 11:10:28 -08002031 console.warn('no web socket open', message);
Simon Hunta255a2c2014-11-13 22:29:35 -08002032 } else {
Simon Hunt434cf142014-11-24 11:10:28 -08002033 console.log('WS Send: ', message);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002034 }
2035 }
2036
2037 };
2038
Simon Hunt0c6d4192014-11-12 12:07:10 -08002039 function noWebSock(b) {
2040 mask.style('display',b ? 'block' : 'none');
2041 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002042
2043 function sendMessage(evType, payload) {
2044 var toSend = {
Simon Huntbb282f52014-11-10 11:08:19 -08002045 event: evType,
2046 sid: ++sid,
2047 payload: payload
2048 },
2049 asText = JSON.stringify(toSend);
2050 wsTraceTx(asText);
2051 webSock.send(asText);
Simon Huntc76ae892014-11-18 17:31:51 -08002052
2053 // Temporary measure for debugging UI behavior ...
2054 if (!config.useLiveData) {
2055 handleTestSend(toSend);
2056 }
Simon Huntbb282f52014-11-10 11:08:19 -08002057 }
2058
2059 function wsTraceTx(msg) {
2060 wsTrace('tx', msg);
2061 }
2062 function wsTraceRx(msg) {
2063 wsTrace('rx', msg);
2064 }
2065 function wsTrace(rxtx, msg) {
Simon Huntbb282f52014-11-10 11:08:19 -08002066 console.log('[' + rxtx + '] ' + msg);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002067 }
2068
Simon Huntc76ae892014-11-18 17:31:51 -08002069 // NOTE: Temporary hardcoded example for showing detail pane
2070 // while we fine-
2071 // Probably should not merge this change...
2072 function handleTestSend(msg) {
2073 if (msg.event === 'requestDetails') {
2074 showDetails({
2075 event: 'showDetails',
2076 sid: 1001,
2077 payload: {
2078 "id": "of:0000ffffffffff09",
2079 "type": "roadm",
2080 "propOrder": [
2081 "Name",
2082 "Vendor",
2083 "H/W Version",
2084 "S/W Version",
2085 "-",
2086 "Latitude",
2087 "Longitude",
2088 "Ports"
2089 ],
2090 "props": {
2091 "Name": null,
2092 "Vendor": "Linc",
2093 "H/W Version": "OE",
2094 "S/W Version": "?",
2095 "-": "",
2096 "Latitude": "40.8",
2097 "Longitude": "73.1",
2098 "Ports": "2"
2099 }
2100 }
2101 });
2102 }
2103 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002104
2105 // ==============================
2106 // Selection stuff
2107
2108 function selectObject(obj, el) {
2109 var n,
Simon Hunt01095ff2014-11-13 16:37:29 -08002110 srcEv = d3.event.sourceEvent,
2111 meta = srcEv.metaKey,
2112 shift = srcEv.shiftKey;
2113
Simon Huntdeab4322014-11-13 18:49:07 -08002114 if ((panZoom() && !meta) || (!panZoom() && meta)) {
Simon Hunt01095ff2014-11-13 16:37:29 -08002115 return;
2116 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002117
2118 if (el) {
2119 n = d3.select(el);
2120 } else {
2121 node.each(function(d) {
2122 if (d == obj) {
2123 n = d3.select(el = this);
2124 }
2125 });
2126 }
2127 if (!n) return;
2128
Simon Hunt01095ff2014-11-13 16:37:29 -08002129 if (shift && n.classed('selected')) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002130 deselectObject(obj.id);
Simon Hunt61d04042014-11-11 17:27:16 -08002131 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002132 return;
2133 }
2134
Simon Hunt01095ff2014-11-13 16:37:29 -08002135 if (!shift) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002136 deselectAll();
2137 }
2138
Simon Huntc31d5692014-11-12 13:27:18 -08002139 selections[obj.id] = { obj: obj, el: el };
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002140 selectOrder.push(obj.id);
2141
2142 n.classed('selected', true);
Simon Huntb0ecfa52014-11-23 21:05:12 -08002143 updateDeviceColors(obj);
Simon Hunt61d04042014-11-11 17:27:16 -08002144 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002145 }
2146
2147 function deselectObject(id) {
Simon Huntc31d5692014-11-12 13:27:18 -08002148 var obj = selections[id],
2149 idx;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002150 if (obj) {
2151 d3.select(obj.el).classed('selected', false);
Simon Hunt61d04042014-11-11 17:27:16 -08002152 delete selections[id];
Simon Huntc31d5692014-11-12 13:27:18 -08002153 idx = $.inArray(id, selectOrder);
2154 if (idx >= 0) {
2155 selectOrder.splice(idx, 1);
2156 }
Simon Huntb0ecfa52014-11-23 21:05:12 -08002157 updateDeviceColors(obj.obj);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002158 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002159 }
2160
2161 function deselectAll() {
2162 // deselect all nodes in the network...
2163 node.classed('selected', false);
2164 selections = {};
2165 selectOrder = [];
Simon Huntb0ecfa52014-11-23 21:05:12 -08002166 updateDeviceColors();
Simon Hunt61d04042014-11-11 17:27:16 -08002167 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002168 }
2169
Simon Huntb0ecfa52014-11-23 21:05:12 -08002170 function updateDeviceColors(d) {
2171 if (d) {
2172 setDeviceColor(d);
2173 } else {
2174 node.filter('.device').each(function (d) {
2175 setDeviceColor(d);
2176 });
2177 }
Thomas Vachuska47635c62014-11-22 01:21:36 -08002178 }
2179
Simon Hunt61d04042014-11-11 17:27:16 -08002180 // update the state of the detail pane, based on current selections
2181 function updateDetailPane() {
2182 var nSel = selectOrder.length;
2183 if (!nSel) {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002184 emptySelect();
Simon Hunt61d04042014-11-11 17:27:16 -08002185 } else if (nSel === 1) {
2186 singleSelect();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002187 requestTrafficForMode();
Simon Hunt61d04042014-11-11 17:27:16 -08002188 } else {
2189 multiSelect();
2190 }
2191 }
2192
Thomas Vachuska9edca302014-11-22 17:06:42 -08002193 function emptySelect() {
2194 detailPane.hide();
2195 cancelTraffic();
2196 }
2197
Simon Hunt61d04042014-11-11 17:27:16 -08002198 function singleSelect() {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002199 // NOTE: detail is shown from showDetails event callback
Simon Hunt61d04042014-11-11 17:27:16 -08002200 requestDetails();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002201 requestTrafficForMode();
Simon Hunt61d04042014-11-11 17:27:16 -08002202 }
2203
2204 function multiSelect() {
Simon Huntb53e0682014-11-12 13:32:01 -08002205 populateMultiSelect();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002206 requestTrafficForMode();
Simon Huntb53e0682014-11-12 13:32:01 -08002207 }
2208
2209 function addSep(tbody) {
2210 var tr = tbody.append('tr');
2211 $('<hr>').appendTo(tr.append('td').attr('colspan', 2));
2212 }
2213
2214 function addProp(tbody, label, value) {
2215 var tr = tbody.append('tr');
2216
2217 tr.append('td')
2218 .attr('class', 'label')
2219 .text(label + ' :');
2220
2221 tr.append('td')
2222 .attr('class', 'value')
2223 .text(value);
2224 }
2225
2226 function populateMultiSelect() {
2227 detailPane.empty();
2228
Simon Hunta3dd9572014-11-20 15:22:41 -08002229 var title = detailPane.append('h3'),
2230 table = detailPane.append('table'),
2231 tbody = table.append('tbody');
Simon Huntb53e0682014-11-12 13:32:01 -08002232
Thomas Vachuska4731f122014-11-20 04:56:19 -08002233 title.text('Selected Nodes');
Simon Huntb53e0682014-11-12 13:32:01 -08002234
2235 selectOrder.forEach(function (d, i) {
2236 addProp(tbody, i+1, d);
2237 });
Simon Huntd72bc702014-11-13 18:38:04 -08002238
2239 addMultiSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08002240 }
2241
Thomas Vachuska47635c62014-11-22 01:21:36 -08002242 // TODO: refactor to consolidate with populateDetails
2243 function populateSummary(data) {
2244 summaryPane.empty();
2245
2246 var svg = summaryPane.append('svg'),
2247 iid = iconGlyphUrl(data);
2248
2249 var title = summaryPane.append('h2'),
2250 table = summaryPane.append('table'),
2251 tbody = table.append('tbody');
2252
2253 appendGlyph(svg, 0, 0, 40, iid);
2254
2255 svg.append('use')
2256 .attr({
2257 class: 'birdBadge',
2258 transform: translate(8,12),
2259 'xlink:href': '#bird',
2260 width: 24,
2261 height: 24,
2262 fill: '#fff'
2263 });
2264
2265 title.text('ONOS Summary');
2266
2267 data.propOrder.forEach(function(p) {
2268 if (p === '-') {
2269 addSep(tbody);
2270 } else {
2271 addProp(tbody, p, data.props[p]);
2272 }
2273 });
2274 }
2275
Simon Hunt61d04042014-11-11 17:27:16 -08002276 function populateDetails(data) {
2277 detailPane.empty();
2278
Simon Hunta6a9fe72014-11-20 11:17:12 -08002279 var svg = detailPane.append('svg'),
2280 iid = iconGlyphUrl(data);
2281
Simon Hunta3dd9572014-11-20 15:22:41 -08002282 var title = detailPane.append('h2'),
2283 table = detailPane.append('table'),
2284 tbody = table.append('tbody');
Simon Hunt61d04042014-11-11 17:27:16 -08002285
Simon Hunta6a9fe72014-11-20 11:17:12 -08002286 appendGlyph(svg, 0, 0, 40, iid);
2287 title.text(data.id);
Simon Hunt61d04042014-11-11 17:27:16 -08002288
2289 data.propOrder.forEach(function(p) {
2290 if (p === '-') {
2291 addSep(tbody);
2292 } else {
2293 addProp(tbody, p, data.props[p]);
2294 }
2295 });
Simon Huntd72bc702014-11-13 18:38:04 -08002296
Thomas Vachuska4731f122014-11-20 04:56:19 -08002297 addSingleSelectActions(data);
Simon Hunt61d04042014-11-11 17:27:16 -08002298 }
2299
Thomas Vachuska4731f122014-11-20 04:56:19 -08002300 function addSingleSelectActions(data) {
Simon Huntd72bc702014-11-13 18:38:04 -08002301 detailPane.append('hr');
2302 // always want to allow 'show traffic'
Thomas Vachuska4731f122014-11-20 04:56:19 -08002303 addAction(detailPane, 'Show Related Traffic', showTrafficAction);
2304
2305 if (data.type === 'switch') {
2306 addAction(detailPane, 'Show Device Flows', showDeviceLinkFlowsAction);
2307 }
Simon Huntd72bc702014-11-13 18:38:04 -08002308 }
2309
2310 function addMultiSelectActions() {
2311 detailPane.append('hr');
2312 // always want to allow 'show traffic'
Thomas Vachuska4731f122014-11-20 04:56:19 -08002313 addAction(detailPane, 'Show Related Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08002314 // if exactly two hosts are selected, also want 'add host intent'
2315 if (nSel() === 2 && allSelectionsClass('host')) {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002316 addAction(detailPane, 'Create Host-to-Host Flow', addHostIntentAction);
2317 } else if (nSel() >= 2 && allSelectionsClass('host')) {
2318 addAction(detailPane, 'Create Multi-Source Flow', addMultiSourceIntentAction);
Simon Huntd72bc702014-11-13 18:38:04 -08002319 }
2320 }
2321
Simon Hunta5e89142014-11-14 07:00:33 -08002322 function addAction(panel, text, cb) {
2323 panel.append('div')
Simon Huntd72bc702014-11-13 18:38:04 -08002324 .classed('actionBtn', true)
2325 .text(text)
2326 .on('click', cb);
2327 }
2328
2329
Paul Greysonfcba0e82014-11-13 10:21:16 -08002330 function zoomPan(scale, translate) {
2331 zoomPanContainer.attr("transform", "translate(" + translate + ")scale(" + scale + ")");
2332 // keep the map lines constant width while zooming
Thomas Vachuska89543292014-11-19 11:28:33 -08002333 bgImg.style("stroke-width", 2.0 / scale + "px");
Paul Greysonfcba0e82014-11-13 10:21:16 -08002334 }
2335
2336 function resetZoomPan() {
2337 zoomPan(1, [0,0]);
2338 zoom.scale(1).translate([0,0]);
2339 }
2340
2341 function setupZoomPan() {
2342 function zoomed() {
Simon Huntdeab4322014-11-13 18:49:07 -08002343 if (!panZoom() ^ !d3.event.sourceEvent.metaKey) {
Paul Greysonfcba0e82014-11-13 10:21:16 -08002344 zoomPan(d3.event.scale, d3.event.translate);
2345 }
2346 }
2347
2348 zoom = d3.behavior.zoom()
2349 .translate([0, 0])
2350 .scale(1)
2351 .scaleExtent([1, 8])
2352 .on("zoom", zoomed);
2353
2354 svg.call(zoom);
2355 }
2356
Simon Hunt61d04042014-11-11 17:27:16 -08002357 // ==============================
2358 // Test harness code
Simon Hunt56d51852014-11-09 13:03:35 -08002359
2360 function prepareScenario(view, ctx, dbg) {
2361 var sc = scenario,
2362 urlSc = sc.evDir + ctx + sc.evScenario;
2363
2364 if (!ctx) {
2365 view.alert("No scenario specified (null ctx)");
2366 return;
2367 }
2368
2369 sc.view = view;
2370 sc.ctx = ctx;
2371 sc.debug = dbg;
2372 sc.evNumber = 0;
2373
2374 d3.json(urlSc, function(err, data) {
Simon Huntbb282f52014-11-10 11:08:19 -08002375 var p = data && data.params || {},
2376 desc = data && data.description || null,
Simon Huntfc274c92014-11-11 11:05:46 -08002377 intro = data && data.title;
Simon Huntbb282f52014-11-10 11:08:19 -08002378
Simon Hunt56d51852014-11-09 13:03:35 -08002379 if (err) {
2380 view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err);
2381 } else {
2382 sc.params = p;
Simon Huntbb282f52014-11-10 11:08:19 -08002383 if (desc) {
2384 intro += '\n\n ' + desc.join('\n ');
2385 }
2386 view.alert(intro);
Simon Hunt56d51852014-11-09 13:03:35 -08002387 }
2388 });
2389
2390 }
2391
Simon Hunt01095ff2014-11-13 16:37:29 -08002392 // ==============================
2393 // Toggle Buttons in masthead
Simon Hunt0c6d4192014-11-12 12:07:10 -08002394
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002395 // TODO: toggle button (and other widgets in the masthead) should be provided
2396 // by the framework; not generated by the view.
2397
Thomas Vachuska47635c62014-11-22 01:21:36 -08002398 //var showInstances;
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002399
Simon Hunt87514342014-11-24 16:41:27 -08002400/*
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002401 function addButtonBar(view) {
2402 var bb = d3.select('#mast')
2403 .append('span').classed('right', true).attr('id', 'bb');
2404
Simon Hunta5e89142014-11-14 07:00:33 -08002405 function mkTogBtn(text, cb) {
2406 return bb.append('span')
2407 .classed('btn', true)
2408 .text(text)
2409 .on('click', cb);
2410 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002411
Thomas Vachuska47635c62014-11-22 01:21:36 -08002412 //showInstances = mkTogBtn('Show Instances', toggleInst);
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002413 }
Simon Hunt87514342014-11-24 16:41:27 -08002414*/
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002415
Simon Huntdeab4322014-11-13 18:49:07 -08002416 function panZoom() {
Simon Hunte5b71752014-11-18 20:06:07 -08002417 return false;
Simon Hunta5e89142014-11-14 07:00:33 -08002418 }
2419
Simon Hunt7fa116d2014-11-17 14:16:55 -08002420 function loadGlyphs(svg) {
2421 var defs = svg.append('defs');
2422 gly.defBird(defs);
Simon Huntc72967b2014-11-20 09:21:42 -08002423 gly.defGlyphs(defs);
Simon Huntb82f6902014-11-22 11:53:15 -08002424 gly.defBadges(defs);
Simon Hunt7fa116d2014-11-17 14:16:55 -08002425 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002426
Simon Hunt395a70c2014-11-22 23:17:40 -08002427 function sendUpdateMeta(d, store) {
2428 var metaUi = {},
2429 ll;
2430
2431 if (store) {
2432 ll = geoMapProjection.invert([d.x, d.y]);
Simon Hunt62c47542014-11-22 22:16:32 -08002433 metaUi = {
2434 x: d.x,
2435 y: d.y,
2436 lng: ll[0],
2437 lat: ll[1]
2438 };
Simon Hunt395a70c2014-11-22 23:17:40 -08002439 }
Simon Hunt62c47542014-11-22 22:16:32 -08002440 d.metaUi = metaUi;
2441 sendMessage('updateMeta', {
2442 id: d.id,
2443 'class': d.class,
2444 'memento': metaUi
2445 });
2446 }
2447
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002448 // ==============================
Simon Hunt142d0032014-11-04 20:13:09 -08002449 // View life-cycle callbacks
Simon Hunt195cb382014-11-03 17:50:51 -08002450
Simon Huntf67722a2014-11-10 09:32:06 -08002451 function preload(view, ctx, flags) {
Simon Hunt142d0032014-11-04 20:13:09 -08002452 var w = view.width(),
2453 h = view.height(),
Simon Huntc7ee0662014-11-05 16:44:37 -08002454 fcfg = config.force,
2455 fpad = fcfg.pad,
2456 forceDim = [w - 2*fpad, h - 2*fpad];
Simon Hunt195cb382014-11-03 17:50:51 -08002457
Simon Hunt142d0032014-11-04 20:13:09 -08002458 // NOTE: view.$div is a D3 selection of the view's div
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002459 var viewBox = '0 0 ' + config.logicalSize + ' ' + config.logicalSize;
2460 svg = view.$div.append('svg').attr('viewBox', viewBox);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002461 setSize(svg, view);
2462
Simon Hunt7fa116d2014-11-17 14:16:55 -08002463 loadGlyphs(svg);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002464
Paul Greysonfcba0e82014-11-13 10:21:16 -08002465 zoomPanContainer = svg.append('g').attr('id', 'zoomPanContainer');
Paul Greysonfcba0e82014-11-13 10:21:16 -08002466 setupZoomPan();
2467
Simon Hunt1a9eff92014-11-07 11:06:34 -08002468 // add blue glow filter to svg layer
Paul Greysonfcba0e82014-11-13 10:21:16 -08002469 d3u.appendGlow(zoomPanContainer);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002470
Simon Huntc7ee0662014-11-05 16:44:37 -08002471 // group for the topology
Paul Greysonfcba0e82014-11-13 10:21:16 -08002472 topoG = zoomPanContainer.append('g')
Simon Huntd3b7d512014-11-12 15:48:41 -08002473 .attr('id', 'topo-G')
Simon Huntc7ee0662014-11-05 16:44:37 -08002474 .attr('transform', fcfg.translate());
2475
Simon Hunte2575b62014-11-18 15:25:53 -08002476 // subgroups for links, link labels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002477 linkG = topoG.append('g').attr('id', 'links');
Simon Hunte2575b62014-11-18 15:25:53 -08002478 linkLabelG = topoG.append('g').attr('id', 'linkLabels');
Simon Huntc7ee0662014-11-05 16:44:37 -08002479 nodeG = topoG.append('g').attr('id', 'nodes');
2480
Simon Hunte2575b62014-11-18 15:25:53 -08002481 // selection of links, linkLabels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002482 link = linkG.selectAll('.link');
Simon Hunte2575b62014-11-18 15:25:53 -08002483 linkLabel = linkLabelG.selectAll('.linkLabel');
Simon Huntc7ee0662014-11-05 16:44:37 -08002484 node = nodeG.selectAll('.node');
2485
Simon Hunt7cd48f32014-11-09 23:42:50 -08002486 function chrg(d) {
2487 return fcfg.charge[d.class] || -12000;
2488 }
Simon Hunt99c13842014-11-06 18:23:12 -08002489 function ldist(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002490 return fcfg.linkDistance[d.type] || 50;
Simon Hunt99c13842014-11-06 18:23:12 -08002491 }
2492 function lstrg(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002493 // 0.0 - 1.0
2494 return fcfg.linkStrength[d.type] || 1.0;
Simon Hunt99c13842014-11-06 18:23:12 -08002495 }
2496
Simon Hunt1a9eff92014-11-07 11:06:34 -08002497 function selectCb(d, self) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002498 selectObject(d, self);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002499 }
2500
2501 function atDragEnd(d, self) {
Simon Hunt56d51852014-11-09 13:03:35 -08002502 // once we've finished moving, pin the node in position
2503 d.fixed = true;
2504 d3.select(self).classed('fixed', true);
2505 if (config.useLiveData) {
Simon Hunt395a70c2014-11-22 23:17:40 -08002506 sendUpdateMeta(d, true);
Simon Hunta255a2c2014-11-13 22:29:35 -08002507 } else {
2508 console.log('Moving node ' + d.id + ' to [' + d.x + ',' + d.y + ']');
Simon Hunt1a9eff92014-11-07 11:06:34 -08002509 }
2510 }
2511
Simon Huntc7ee0662014-11-05 16:44:37 -08002512 // set up the force layout
2513 network.force = d3.layout.force()
2514 .size(forceDim)
2515 .nodes(network.nodes)
2516 .links(network.links)
Simon Hunt7cd48f32014-11-09 23:42:50 -08002517 .gravity(0.4)
2518 .friction(0.7)
2519 .charge(chrg)
Simon Hunt99c13842014-11-06 18:23:12 -08002520 .linkDistance(ldist)
2521 .linkStrength(lstrg)
Simon Huntc7ee0662014-11-05 16:44:37 -08002522 .on('tick', tick);
Simon Hunt195cb382014-11-03 17:50:51 -08002523
Simon Hunt01095ff2014-11-13 16:37:29 -08002524 network.drag = d3u.createDragBehavior(network.force,
Simon Huntdeab4322014-11-13 18:49:07 -08002525 selectCb, atDragEnd, panZoom);
Simon Hunt0c6d4192014-11-12 12:07:10 -08002526
2527 // create mask layer for when we lose connection to server.
Simon Hunta5e89142014-11-14 07:00:33 -08002528 // TODO: this should be part of the framework
Simon Hunt0c6d4192014-11-12 12:07:10 -08002529 mask = view.$div.append('div').attr('id','topo-mask');
2530 para(mask, 'Oops!');
2531 para(mask, 'Web-socket connection to server closed...');
2532 para(mask, 'Try refreshing the page.');
Simon Hunt12ce12e2014-11-15 21:13:19 -08002533
2534 mask.append('svg')
2535 .attr({
2536 id: 'mask-bird',
2537 width: w,
2538 height: h
2539 })
2540 .append('g')
2541 .attr('transform', birdTranslate(w, h))
2542 .style('opacity', 0.3)
2543 .append('use')
2544 .attr({
2545 'xlink:href': '#bird',
2546 width: config.birdDim,
2547 height: config.birdDim,
2548 fill: '#111'
Thomas Vachuska89543292014-11-19 11:28:33 -08002549 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08002550 }
Simon Hunt195cb382014-11-03 17:50:51 -08002551
Simon Hunt01095ff2014-11-13 16:37:29 -08002552 function para(sel, text) {
2553 sel.append('p').text(text);
2554 }
2555
2556
Simon Hunt56d51852014-11-09 13:03:35 -08002557 function load(view, ctx, flags) {
Simon Huntf67722a2014-11-10 09:32:06 -08002558 // resize, in case the window was resized while we were not loaded
2559 resize(view, ctx, flags);
2560
Simon Hunt99c13842014-11-06 18:23:12 -08002561 // cache the view token, so network topo functions can access it
2562 network.view = view;
Simon Hunt56d51852014-11-09 13:03:35 -08002563 config.useLiveData = !flags.local;
2564
2565 if (!config.useLiveData) {
2566 prepareScenario(view, ctx, flags.debug);
2567 }
Simon Hunt99c13842014-11-06 18:23:12 -08002568
2569 // set our radio buttons and key bindings
Simon Hunt9462e8c2014-11-14 17:28:09 -08002570 layerBtnSet = view.setRadio(layerButtons);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002571 view.setKeys(keyDispatch);
Simon Hunt87514342014-11-24 16:41:27 -08002572 view.setGestures(gestures);
Simon Hunt195cb382014-11-03 17:50:51 -08002573
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002574 // patch in our "button bar" for now
2575 // TODO: implement a more official frameworky way of doing this..
Simon Hunt87514342014-11-24 16:41:27 -08002576 //addButtonBar(view);
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002577
Simon Huntd3b7d512014-11-12 15:48:41 -08002578 // Load map data asynchronously; complete startup after that..
2579 loadGeoJsonData();
Simon Hunta255a2c2014-11-13 22:29:35 -08002580 }
2581
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002582 function startAntTimer() {
Thomas Vachuska4731f122014-11-20 04:56:19 -08002583 if (!antTimer) {
2584 var pulses = [5, 3, 1.2, 3],
2585 pulse = 0;
2586 antTimer = setInterval(function () {
2587 pulse = pulse + 1;
2588 pulse = pulse === pulses.length ? 0 : pulse;
2589 d3.selectAll('.animated').style('stroke-width', pulses[pulse]);
2590 }, 200);
2591 }
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002592 }
2593
2594 function stopAntTimer() {
Simon Hunta255a2c2014-11-13 22:29:35 -08002595 if (antTimer) {
2596 clearInterval(antTimer);
2597 antTimer = null;
2598 }
Simon Huntd3b7d512014-11-12 15:48:41 -08002599 }
2600
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002601 function unload(view, ctx, flags) {
2602 stopAntTimer();
2603 }
2604
Simon Huntd3b7d512014-11-12 15:48:41 -08002605 // TODO: move these to config/state portion of script
Simon Hunta6a9fe72014-11-20 11:17:12 -08002606 var geoJsonUrl = 'json/map/continental_us.json',
Simon Huntd3b7d512014-11-12 15:48:41 -08002607 geoJson;
2608
2609 function loadGeoJsonData() {
2610 d3.json(geoJsonUrl, function (err, data) {
2611 if (err) {
2612 // fall back to USA map background
2613 loadStaticMap();
2614 } else {
2615 geoJson = data;
2616 loadGeoMap();
2617 }
2618
2619 // finally, connect to the server...
2620 if (config.useLiveData) {
2621 webSock.connect();
2622 }
2623 });
2624 }
2625
2626 function showBg() {
Simon Hunt434cf142014-11-24 11:10:28 -08002627 return visVal(config.options.showBackground);
Simon Huntd3b7d512014-11-12 15:48:41 -08002628 }
2629
2630 function loadStaticMap() {
2631 fnTrace('loadStaticMap', config.backgroundUrl);
2632 var w = network.view.width(),
2633 h = network.view.height();
2634
2635 // load the background image
2636 bgImg = svg.insert('svg:image', '#topo-G')
2637 .attr({
2638 id: 'topo-bg',
2639 width: w,
2640 height: h,
2641 'xlink:href': config.backgroundUrl
2642 })
2643 .style({
2644 visibility: showBg()
2645 });
2646 }
2647
2648 function loadGeoMap() {
2649 fnTrace('loadGeoMap', geoJsonUrl);
Simon Huntd3b7d512014-11-12 15:48:41 -08002650
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002651 // extracts the topojson data into geocoordinate-based geometry
2652 var topoData = topojson.feature(geoJson, geoJson.objects.states);
Simon Huntd3b7d512014-11-12 15:48:41 -08002653
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002654 // see: http://bl.ocks.org/mbostock/4707858
2655 geoMapProjection = d3.geo.mercator();
2656 var path = d3.geo.path().projection(geoMapProjection);
Simon Huntd3b7d512014-11-12 15:48:41 -08002657
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002658 geoMapProjection
2659 .scale(1)
2660 .translate([0, 0]);
Simon Huntd3b7d512014-11-12 15:48:41 -08002661
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002662 // [[x1,y1],[x2,y2]]
2663 var b = path.bounds(topoData);
Paul Greysonfcba0e82014-11-13 10:21:16 -08002664 // size map to 95% of minimum dimension to fill space
2665 var s = .95 / Math.min((b[1][0] - b[0][0]) / config.logicalSize, (b[1][1] - b[0][1]) / config.logicalSize);
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002666 var t = [(config.logicalSize - s * (b[1][0] + b[0][0])) / 2, (config.logicalSize - s * (b[1][1] + b[0][1])) / 2];
Simon Huntd3b7d512014-11-12 15:48:41 -08002667
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002668 geoMapProjection
2669 .scale(s)
2670 .translate(t);
2671
Paul Greysonfcba0e82014-11-13 10:21:16 -08002672 bgImg = zoomPanContainer.insert("g", '#topo-G');
Thomas Vachuska89543292014-11-19 11:28:33 -08002673 bgImg.attr('id', 'map').selectAll('path')
2674 .data(topoData.features)
2675 .enter()
2676 .append('path')
2677 .attr('d', path);
Simon Hunt195cb382014-11-03 17:50:51 -08002678 }
2679
Simon Huntf67722a2014-11-10 09:32:06 -08002680 function resize(view, ctx, flags) {
Simon Hunt12ce12e2014-11-15 21:13:19 -08002681 var w = view.width(),
2682 h = view.height();
2683
Simon Hunt934c3ce2014-11-05 11:45:07 -08002684 setSize(svg, view);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002685
2686 d3.select('#mask-bird').attr({ width: w, height: h})
2687 .select('g').attr('transform', birdTranslate(w, h));
Simon Hunt142d0032014-11-04 20:13:09 -08002688 }
2689
Simon Hunt8f40cce2014-11-23 15:57:30 -08002690 function theme(view, ctx, flags) {
2691 updateInstances();
Simon Huntb0ecfa52014-11-23 21:05:12 -08002692 updateDeviceColors();
Simon Hunt8f40cce2014-11-23 15:57:30 -08002693 }
2694
Simon Hunt12ce12e2014-11-15 21:13:19 -08002695 function birdTranslate(w, h) {
2696 var bdim = config.birdDim;
2697 return 'translate('+((w-bdim)*.4)+','+((h-bdim)*.1)+')';
2698 }
Simon Hunt142d0032014-11-04 20:13:09 -08002699
2700 // ==============================
2701 // View registration
Simon Hunt195cb382014-11-03 17:50:51 -08002702
Simon Hunt25248912014-11-04 11:25:48 -08002703 onos.ui.addView('topo', {
Simon Hunt142d0032014-11-04 20:13:09 -08002704 preload: preload,
2705 load: load,
Simon Hunta255a2c2014-11-13 22:29:35 -08002706 unload: unload,
Simon Hunt8f40cce2014-11-23 15:57:30 -08002707 resize: resize,
2708 theme: theme
Simon Hunt195cb382014-11-03 17:50:51 -08002709 });
2710
Thomas Vachuska47635c62014-11-22 01:21:36 -08002711 summaryPane = onos.ui.addFloatingPanel('topo-summary');
Simon Hunt61d04042014-11-11 17:27:16 -08002712 detailPane = onos.ui.addFloatingPanel('topo-detail');
Simon Hunta5e89142014-11-14 07:00:33 -08002713 oiBox = onos.ui.addFloatingPanel('topo-oibox', 'TL');
Simon Huntb82f6902014-11-22 11:53:15 -08002714 oiBox.width(20);
Simon Hunt61d04042014-11-11 17:27:16 -08002715
Simon Hunt195cb382014-11-03 17:50:51 -08002716}(ONOS));