blob: ba21295e15a5b018f519d59aabf4009ceeb7ceaf [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 Hunt56ef0fe2014-11-21 08:24:43 -0800149 U: [unpin, 'Unpin node'],
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 Hunt195cb382014-11-03 17:50:51 -0800157 // state variables
Simon Hunt99c13842014-11-06 18:23:12 -0800158 var network = {
Simon Hunt50128c02014-11-08 13:36:15 -0800159 view: null, // view token reference
Simon Hunt99c13842014-11-06 18:23:12 -0800160 nodes: [],
161 links: [],
Simon Hunt269670f2014-11-17 16:17:43 -0800162 lookup: {},
163 revLinkToKey: {}
Simon Hunt99c13842014-11-06 18:23:12 -0800164 },
Simon Hunt56d51852014-11-09 13:03:35 -0800165 scenario = {
166 evDir: 'json/ev/',
167 evScenario: '/scenario.json',
168 evPrefix: '/ev_',
169 evOnos: '_onos.json',
170 evUi: '_ui.json',
171 ctx: null,
172 params: {},
173 evNumber: 0,
Simon Hunt434cf142014-11-24 11:10:28 -0800174 view: null
Simon Hunt56d51852014-11-09 13:03:35 -0800175 },
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800176 webSock,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800177 sid = 0,
Thomas Vachuska60d72bf2014-11-21 13:02:00 -0800178 deviceLabelCount = 3,
Simon Hunt209155e2014-11-21 12:16:09 -0800179 hostLabelCount = 2,
Simon Hunt56d51852014-11-09 13:03:35 -0800180 deviceLabelIndex = 0,
181 hostLabelIndex = 0,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800182 selections = {},
Simon Hunta5e89142014-11-14 07:00:33 -0800183 selectOrder = [],
Simon Hunt6ac93f32014-11-13 12:17:27 -0800184 hovered = null,
Thomas Vachuska47635c62014-11-22 01:21:36 -0800185 summaryPane,
Simon Hunta5e89142014-11-14 07:00:33 -0800186 detailPane,
Simon Hunta255a2c2014-11-13 22:29:35 -0800187 antTimer = null,
Simon Hunta5e89142014-11-14 07:00:33 -0800188 onosInstances = {},
189 onosOrder = [],
190 oiBox,
Simon Hunt9462e8c2014-11-14 17:28:09 -0800191 oiShowMaster = false,
Simon Hunt8f40cce2014-11-23 15:57:30 -0800192 portLabelsOn = false,
Simon Huntb0ecfa52014-11-23 21:05:12 -0800193 cat7 = d3u.cat7(),
Simon Hunt434cf142014-11-24 11:10:28 -0800194 colorAffinity = false,
195 showHosts = false;
Simon Hunt195cb382014-11-03 17:50:51 -0800196
Simon Hunt434cf142014-11-24 11:10:28 -0800197 // constants
Thomas Vachuska9edca302014-11-22 17:06:42 -0800198 var hoverModeAll = 1,
199 hoverModeFlows = 2,
200 hoverModeIntents = 3,
201 hoverMode = hoverModeFlows;
202
Simon Hunt934c3ce2014-11-05 11:45:07 -0800203 // D3 selections
204 var svg,
Paul Greysonfcba0e82014-11-13 10:21:16 -0800205 zoomPanContainer,
Simon Hunt934c3ce2014-11-05 11:45:07 -0800206 bgImg,
Simon Huntc7ee0662014-11-05 16:44:37 -0800207 topoG,
208 nodeG,
209 linkG,
Simon Hunte2575b62014-11-18 15:25:53 -0800210 linkLabelG,
Simon Huntc7ee0662014-11-05 16:44:37 -0800211 node,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800212 link,
Simon Hunte2575b62014-11-18 15:25:53 -0800213 linkLabel,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800214 mask;
Simon Hunt195cb382014-11-03 17:50:51 -0800215
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800216 // the projection for the map background
217 var geoMapProjection;
218
Paul Greysonfcba0e82014-11-13 10:21:16 -0800219 // the zoom function
220 var zoom;
221
Simon Hunt142d0032014-11-04 20:13:09 -0800222 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800223 // For Debugging / Development
Simon Hunt195cb382014-11-03 17:50:51 -0800224
Simon Hunt99c13842014-11-06 18:23:12 -0800225 function note(label, msg) {
226 console.log('NOTE: ' + label + ': ' + msg);
Simon Hunt195cb382014-11-03 17:50:51 -0800227 }
228
Simon Hunt99c13842014-11-06 18:23:12 -0800229 function debug(what) {
230 return config.debugOn && config.debug[what];
Simon Hunt934c3ce2014-11-05 11:45:07 -0800231 }
232
Simon Huntfc274c92014-11-11 11:05:46 -0800233 function fnTrace(msg, id) {
234 if (config.fnTrace) {
235 console.log('FN: ' + msg + ' [' + id + ']');
236 }
237 }
Simon Hunt99c13842014-11-06 18:23:12 -0800238
Simon Hunta5e89142014-11-14 07:00:33 -0800239 function evTrace(data) {
240 fnTrace(data.event, data.payload.id);
241 }
242
Simon Hunt934c3ce2014-11-05 11:45:07 -0800243 // ==============================
244 // Key Callbacks
245
Simon Hunt99c13842014-11-06 18:23:12 -0800246 function testMe(view) {
Simon Hunt8f40cce2014-11-23 15:57:30 -0800247 //view.alert('Theme is ' + view.getTheme());
Simon Hunta3dd9572014-11-20 15:22:41 -0800248 //view.flash('This is some text');
Simon Hunt8f40cce2014-11-23 15:57:30 -0800249 cat7.testCard(svg);
Simon Hunt99c13842014-11-06 18:23:12 -0800250 }
251
Simon Hunt56d51852014-11-09 13:03:35 -0800252 function injectTestEvent(view) {
Simon Hunt434cf142014-11-24 11:10:28 -0800253 if (config.useLiveData) { return; }
254
Simon Hunt56d51852014-11-09 13:03:35 -0800255 var sc = scenario,
256 evn = ++sc.evNumber,
257 pfx = sc.evDir + sc.ctx + sc.evPrefix + evn,
258 onosUrl = pfx + sc.evOnos,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800259 uiUrl = pfx + sc.evUi,
260 stack = [
261 { url: onosUrl, cb: handleServerEvent },
262 { url: uiUrl, cb: handleUiEvent }
263 ];
264 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800265 }
266
Simon Hunt7cd48f32014-11-09 23:42:50 -0800267 function recurseFetchEvent(stack, evn) {
268 var v = scenario.view,
269 frame;
270 if (stack.length === 0) {
Simon Huntfc274c92014-11-11 11:05:46 -0800271 v.alert('Oops!\n\nNo event #' + evn + ' found.');
Simon Hunt7cd48f32014-11-09 23:42:50 -0800272 return;
273 }
274 frame = stack.shift();
275
276 d3.json(frame.url, function (err, data) {
Simon Hunt99c13842014-11-06 18:23:12 -0800277 if (err) {
Simon Hunt56d51852014-11-09 13:03:35 -0800278 if (err.status === 404) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800279 // if we didn't find the data, try the next stack frame
280 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800281 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800282 v.alert('non-404 error:\n\n' + frame.url + '\n\n' + err);
Simon Hunt56d51852014-11-09 13:03:35 -0800283 }
Simon Hunt99c13842014-11-06 18:23:12 -0800284 } else {
Simon Hunt1712ed82014-11-17 12:56:00 -0800285 wsTrace('test', JSON.stringify(data));
Simon Hunt7cd48f32014-11-09 23:42:50 -0800286 frame.cb(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800287 }
288 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800289
Simon Hunt56d51852014-11-09 13:03:35 -0800290 }
Simon Hunt50128c02014-11-08 13:36:15 -0800291
Simon Hunt56d51852014-11-09 13:03:35 -0800292 function handleUiEvent(data) {
Simon Huntbb282f52014-11-10 11:08:19 -0800293 scenario.view.alert('UI Tx: ' + data.event + '\n\n' +
294 JSON.stringify(data));
Simon Hunt56d51852014-11-09 13:03:35 -0800295 }
296
297 function injectStartupEvents(view) {
298 var last = scenario.params.lastAuto || 0;
Simon Hunt434cf142014-11-24 11:10:28 -0800299 if (config.useLiveData) { return; }
Simon Hunt56d51852014-11-09 13:03:35 -0800300
301 while (scenario.evNumber < last) {
Simon Hunt1a9eff92014-11-07 11:06:34 -0800302 injectTestEvent(view);
303 }
304 }
305
Simon Hunt934c3ce2014-11-05 11:45:07 -0800306 function toggleBg() {
307 var vis = bgImg.style('visibility');
Simon Hunt434cf142014-11-24 11:10:28 -0800308 bgImg.style('visibility', visVal(vis === 'hidden'));
309 }
310
311 function toggleHosts() {
312 showHosts = !showHosts;
313 updateHostVisibility();
314 network.view.flash('Hosts ' + visVal(showHosts));
Simon Hunt934c3ce2014-11-05 11:45:07 -0800315 }
316
Simon Hunt99c13842014-11-06 18:23:12 -0800317 function cycleLabels() {
Thomas Vachuska60d72bf2014-11-21 13:02:00 -0800318 deviceLabelIndex = (deviceLabelIndex === 2)
Simon Huntbb282f52014-11-10 11:08:19 -0800319 ? 0 : deviceLabelIndex + 1;
Simon Hunt5f36d342014-11-08 21:33:14 -0800320
Simon Hunt99c13842014-11-06 18:23:12 -0800321 network.nodes.forEach(function (d) {
Simon Huntbb282f52014-11-10 11:08:19 -0800322 if (d.class === 'device') {
323 updateDeviceLabel(d);
324 }
Simon Hunt99c13842014-11-06 18:23:12 -0800325 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800326 }
327
328 function togglePorts(view) {
Simon Hunt434cf142014-11-24 11:10:28 -0800329 //view.alert('togglePorts() callback')
Simon Hunt934c3ce2014-11-05 11:45:07 -0800330 }
331
Simon Hunt6ac93f32014-11-13 12:17:27 -0800332 function unpin() {
333 if (hovered) {
Simon Hunt395a70c2014-11-22 23:17:40 -0800334 sendUpdateMeta(hovered);
Simon Hunt6ac93f32014-11-13 12:17:27 -0800335 hovered.fixed = false;
336 hovered.el.classed('fixed', false);
337 network.force.resume();
338 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800339 }
340
Simon Hunt9462e8c2014-11-14 17:28:09 -0800341 function handleEscape(view) {
342 if (oiShowMaster) {
343 cancelAffinity();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800344 } else if (detailPane.isVisible()) {
Simon Hunt9462e8c2014-11-14 17:28:09 -0800345 deselectAll();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800346 } else if (oiBox.isVisible()) {
Simon Huntb0ecfa52014-11-23 21:05:12 -0800347 hideInstances();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800348 } else if (summaryPane.isVisible()) {
349 cancelSummary();
Simon Hunt9462e8c2014-11-14 17:28:09 -0800350 }
351 }
352
Simon Hunt934c3ce2014-11-05 11:45:07 -0800353 // ==============================
354 // Radio Button Callbacks
355
Simon Hunta5e89142014-11-14 07:00:33 -0800356 var layerLookup = {
357 host: {
Simon Hunt209155e2014-11-21 12:16:09 -0800358 endstation: 'pkt', // default, if host event does not define type
Thomas Vachuska89543292014-11-19 11:28:33 -0800359 router: 'pkt',
Simon Hunta5e89142014-11-14 07:00:33 -0800360 bgpSpeaker: 'pkt'
361 },
362 device: {
363 switch: 'pkt',
364 roadm: 'opt'
365 },
366 link: {
367 hostLink: 'pkt',
368 direct: 'pkt',
Simon Hunt8257f4c2014-11-16 19:34:54 -0800369 indirect: '',
370 tunnel: '',
Simon Hunta5e89142014-11-14 07:00:33 -0800371 optical: 'opt'
372 }
373 };
374
375 function inLayer(d, layer) {
Simon Hunt8257f4c2014-11-16 19:34:54 -0800376 var type = d.class === 'link' ? d.type() : d.type,
377 look = layerLookup[d.class],
378 lyr = look && look[type];
Simon Hunta5e89142014-11-14 07:00:33 -0800379 return lyr === layer;
380 }
381
382 function unsuppressLayer(which) {
383 node.each(function (d) {
384 var node = d.el;
385 if (inLayer(d, which)) {
386 node.classed('suppressed', false);
387 }
388 });
389
390 link.each(function (d) {
391 var link = d.el;
392 if (inLayer(d, which)) {
393 link.classed('suppressed', false);
394 }
395 });
396 }
397
Simon Hunt9462e8c2014-11-14 17:28:09 -0800398 function suppressLayers(b) {
399 node.classed('suppressed', b);
400 link.classed('suppressed', b);
Simon Hunt142d0032014-11-04 20:13:09 -0800401// d3.selectAll('svg .port').classed('inactive', false);
402// d3.selectAll('svg .portText').classed('inactive', false);
Simon Hunt195cb382014-11-03 17:50:51 -0800403 }
404
Simon Hunt9462e8c2014-11-14 17:28:09 -0800405 function showAllLayers() {
406 suppressLayers(false);
407 }
408
Simon Hunt195cb382014-11-03 17:50:51 -0800409 function showPacketLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800410 node.classed('suppressed', true);
411 link.classed('suppressed', true);
412 unsuppressLayer('pkt');
Simon Hunt195cb382014-11-03 17:50:51 -0800413 }
414
415 function showOpticalLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800416 node.classed('suppressed', true);
417 link.classed('suppressed', true);
418 unsuppressLayer('opt');
Simon Hunt195cb382014-11-03 17:50:51 -0800419 }
420
Simon Hunt9462e8c2014-11-14 17:28:09 -0800421 function restoreLayerState() {
422 layerBtnDispatch[layerBtnSet.selected()]();
423 }
424
Simon Hunt142d0032014-11-04 20:13:09 -0800425 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800426 // Private functions
427
Simon Hunt99c13842014-11-06 18:23:12 -0800428 function safeId(s) {
429 return s.replace(/[^a-z0-9]/gi, '-');
430 }
431
Simon Huntc7ee0662014-11-05 16:44:37 -0800432 // set the size of the given element to that of the view (reduced if padded)
433 function setSize(el, view, pad) {
434 var padding = pad ? pad * 2 : 0;
Simon Hunt934c3ce2014-11-05 11:45:07 -0800435 el.attr({
Simon Huntc7ee0662014-11-05 16:44:37 -0800436 width: view.width() - padding,
437 height: view.height() - padding
Simon Hunt934c3ce2014-11-05 11:45:07 -0800438 });
439 }
440
Simon Hunt8257f4c2014-11-16 19:34:54 -0800441 function makeNodeKey(d, what) {
442 var port = what + 'Port';
443 return d[what] + '/' + d[port];
444 }
445
446 function makeLinkKey(d, flipped) {
447 var one = flipped ? makeNodeKey(d, 'dst') : makeNodeKey(d, 'src'),
448 two = flipped ? makeNodeKey(d, 'src') : makeNodeKey(d, 'dst');
449 return one + '-' + two;
450 }
451
Simon Hunt269670f2014-11-17 16:17:43 -0800452 function findLinkById(id) {
453 // check to see if this is a reverse lookup, else default to given id
454 var key = network.revLinkToKey[id] || id;
455 return key && network.lookup[key];
456 }
457
Simon Hunt8257f4c2014-11-16 19:34:54 -0800458 function findLink(linkData, op) {
459 var key = makeLinkKey(linkData),
460 keyrev = makeLinkKey(linkData, 1),
461 link = network.lookup[key],
462 linkRev = network.lookup[keyrev],
463 result = {},
464 ldata = link || linkRev,
465 rawLink;
466
467 if (op === 'add') {
468 if (link) {
469 // trying to add a link that we already know about
470 result.ldata = link;
471 result.badLogic = 'addLink: link already added';
472
473 } else if (linkRev) {
474 // we found the reverse of the link to be added
475 result.ldata = linkRev;
476 if (linkRev.fromTarget) {
477 result.badLogic = 'addLink: link already added';
478 }
479 }
480 } else if (op === 'update') {
481 if (!ldata) {
482 result.badLogic = 'updateLink: link not found';
483 } else {
484 rawLink = link ? ldata.fromSource : ldata.fromTarget;
485 result.updateWith = function (data) {
486 $.extend(rawLink, data);
487 restyleLinkElement(ldata);
488 }
489 }
490 } else if (op === 'remove') {
491 if (!ldata) {
492 result.badLogic = 'removeLink: link not found';
493 } else {
494 rawLink = link ? ldata.fromSource : ldata.fromTarget;
495
496 if (!rawLink) {
497 result.badLogic = 'removeLink: link not found';
498
499 } else {
500 result.removeRawLink = function () {
501 if (link) {
502 // remove fromSource
503 ldata.fromSource = null;
504 if (ldata.fromTarget) {
505 // promote target into source position
506 ldata.fromSource = ldata.fromTarget;
507 ldata.fromTarget = null;
508 ldata.key = keyrev;
509 delete network.lookup[key];
510 network.lookup[keyrev] = ldata;
Simon Hunt269670f2014-11-17 16:17:43 -0800511 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800512 }
513 } else {
514 // remove fromTarget
515 ldata.fromTarget = null;
Simon Hunt269670f2014-11-17 16:17:43 -0800516 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800517 }
518 if (ldata.fromSource) {
519 restyleLinkElement(ldata);
520 } else {
521 removeLinkElement(ldata);
522 }
523 }
524 }
525 }
526 }
527 return result;
528 }
529
530 function addLinkUpdate(ldata, link) {
531 // add link event, but we already have the reverse link installed
532 ldata.fromTarget = link;
Simon Hunt269670f2014-11-17 16:17:43 -0800533 network.revLinkToKey[link.id] = ldata.key;
Simon Hunt8257f4c2014-11-16 19:34:54 -0800534 restyleLinkElement(ldata);
535 }
536
537 var allLinkTypes = 'direct indirect optical tunnel',
538 defaultLinkType = 'direct';
539
540 function restyleLinkElement(ldata) {
541 // this fn's job is to look at raw links and decide what svg classes
542 // need to be applied to the line element in the DOM
543 var el = ldata.el,
544 type = ldata.type(),
545 lw = ldata.linkWidth(),
546 online = ldata.online();
547
548 el.classed('link', true);
549 el.classed('inactive', !online);
550 el.classed(allLinkTypes, false);
551 if (type) {
552 el.classed(type, true);
553 }
554 el.transition()
555 .duration(1000)
Thomas Vachuska89543292014-11-19 11:28:33 -0800556 .attr('stroke-width', linkScale(lw))
557 .attr('stroke', config.topo.linkBaseColor);
Simon Hunt8257f4c2014-11-16 19:34:54 -0800558 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800559
Simon Hunt99c13842014-11-06 18:23:12 -0800560 // ==============================
561 // Event handlers for server-pushed events
562
Simon Huntbb282f52014-11-10 11:08:19 -0800563 function logicError(msg) {
564 // TODO, report logic error to server, via websock, so it can be logged
Simon Huntfc274c92014-11-11 11:05:46 -0800565 console.warn(msg);
Simon Huntbb282f52014-11-10 11:08:19 -0800566 }
567
Simon Hunt99c13842014-11-06 18:23:12 -0800568 var eventDispatch = {
Simon Hunta5e89142014-11-14 07:00:33 -0800569 addInstance: addInstance,
Simon Hunt99c13842014-11-06 18:23:12 -0800570 addDevice: addDevice,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800571 addLink: addLink,
Simon Hunt56d51852014-11-09 13:03:35 -0800572 addHost: addHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800573
Simon Huntfcfb46c2014-11-19 12:53:38 -0800574 updateInstance: updateInstance,
Simon Huntbb282f52014-11-10 11:08:19 -0800575 updateDevice: updateDevice,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800576 updateLink: updateLink,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800577 updateHost: updateHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800578
Simon Hunt7b403bc2014-11-22 19:01:00 -0800579 removeInstance: removeInstance,
Simon Huntbb282f52014-11-10 11:08:19 -0800580 removeDevice: stillToImplement,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800581 removeLink: removeLink,
Simon Hunt44031102014-11-11 13:20:36 -0800582 removeHost: removeHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800583
Simon Hunt61d04042014-11-11 17:27:16 -0800584 showDetails: showDetails,
Thomas Vachuska47635c62014-11-22 01:21:36 -0800585 showSummary: showSummary,
Simon Huntb53e0682014-11-12 13:32:01 -0800586 showTraffic: showTraffic
Simon Hunt99c13842014-11-06 18:23:12 -0800587 };
588
Simon Hunta5e89142014-11-14 07:00:33 -0800589 function addInstance(data) {
590 evTrace(data);
591 var inst = data.payload,
592 id = inst.id;
593 if (onosInstances[id]) {
594 logicError('ONOS instance already added: ' + id);
595 return;
596 }
597 onosInstances[id] = inst;
598 onosOrder.push(inst);
599 updateInstances();
600 }
601
Simon Hunt99c13842014-11-06 18:23:12 -0800602 function addDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800603 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800604 var device = data.payload,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800605 nodeData = createDeviceNode(device);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800606 network.nodes.push(nodeData);
607 network.lookup[nodeData.id] = nodeData;
Simon Hunt99c13842014-11-06 18:23:12 -0800608 updateNodes();
609 network.force.start();
610 }
611
Simon Hunt99c13842014-11-06 18:23:12 -0800612 function addLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800613 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800614 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800615 result = findLink(link, 'add'),
616 bad = result.badLogic,
617 ldata = result.ldata;
618
619 if (bad) {
620 logicError(bad + ': ' + link.id);
621 return;
622 }
623
624 if (ldata) {
625 // we already have a backing store link for src/dst nodes
626 addLinkUpdate(ldata, link);
627 return;
628 }
629
630 // no backing store link yet
631 ldata = createLink(link);
632 if (ldata) {
633 network.links.push(ldata);
634 network.lookup[ldata.key] = ldata;
Simon Hunt99c13842014-11-06 18:23:12 -0800635 updateLinks();
636 network.force.start();
637 }
638 }
639
Simon Hunt56d51852014-11-09 13:03:35 -0800640 function addHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800641 evTrace(data);
Simon Hunt56d51852014-11-09 13:03:35 -0800642 var host = data.payload,
643 node = createHostNode(host),
644 lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800645 network.nodes.push(node);
646 network.lookup[host.id] = node;
647 updateNodes();
648
649 lnk = createHostLink(host);
650 if (lnk) {
Simon Hunt44031102014-11-11 13:20:36 -0800651 node.linkData = lnk; // cache ref on its host
Simon Hunt56d51852014-11-09 13:03:35 -0800652 network.links.push(lnk);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800653 network.lookup[host.ingress] = lnk;
654 network.lookup[host.egress] = lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800655 updateLinks();
656 }
657 network.force.start();
658 }
659
Simon Hunt44031102014-11-11 13:20:36 -0800660 // TODO: fold updateX(...) methods into one base method; remove duplication
Simon Hunt56a2ea42014-11-19 12:39:31 -0800661
662 function updateInstance(data) {
663 evTrace(data);
664 var inst = data.payload,
665 id = inst.id,
666 instData = onosInstances[id];
667 if (instData) {
668 $.extend(instData, inst);
669 updateInstances();
Simon Hunt56a2ea42014-11-19 12:39:31 -0800670 } else {
671 logicError('updateInstance lookup fail. ID = "' + id + '"');
672 }
673 }
674
Simon Huntbb282f52014-11-10 11:08:19 -0800675 function updateDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800676 evTrace(data);
Simon Huntbb282f52014-11-10 11:08:19 -0800677 var device = data.payload,
678 id = device.id,
Simon Hunt62c47542014-11-22 22:16:32 -0800679 d = network.lookup[id];
680 if (d) {
681 $.extend(d, device);
682 if (positionNode(d, true)) {
Simon Hunt395a70c2014-11-22 23:17:40 -0800683 sendUpdateMeta(d, true);
Simon Hunt62c47542014-11-22 22:16:32 -0800684 }
685 updateNodes();
Simon Huntbb282f52014-11-10 11:08:19 -0800686 } else {
687 logicError('updateDevice lookup fail. ID = "' + id + '"');
688 }
689 }
690
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800691 function updateLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800692 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800693 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800694 result = findLink(link, 'update'),
695 bad = result.badLogic;
696 if (bad) {
697 logicError(bad + ': ' + link.id);
698 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800699 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800700 result.updateWith(link);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800701 }
702
Simon Hunt7cd48f32014-11-09 23:42:50 -0800703 function updateHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800704 evTrace(data);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800705 var host = data.payload,
Simon Huntbb282f52014-11-10 11:08:19 -0800706 id = host.id,
707 hostData = network.lookup[id];
708 if (hostData) {
709 $.extend(hostData, host);
710 updateHostState(hostData);
711 } else {
712 logicError('updateHost lookup fail. ID = "' + id + '"');
713 }
Simon Hunt7cd48f32014-11-09 23:42:50 -0800714 }
715
Simon Hunt44031102014-11-11 13:20:36 -0800716 // TODO: fold removeX(...) methods into base method - remove dup code
Simon Hunt7b403bc2014-11-22 19:01:00 -0800717 function removeInstance(data) {
718 evTrace(data);
719 var inst = data.payload,
720 id = inst.id,
721 instData = onosInstances[id];
722 if (instData) {
723 var idx = find(id, onosOrder, 'id');
724 if (idx >= 0) {
725 onosOrder.splice(idx, 1);
726 }
727 delete onosInstances[id];
728 updateInstances();
729 } else {
730 logicError('updateInstance lookup fail. ID = "' + id + '"');
731 }
732 }
733
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800734 function removeLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800735 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800736 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800737 result = findLink(link, 'remove'),
738 bad = result.badLogic;
739 if (bad) {
740 logicError(bad + ': ' + link.id);
741 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800742 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800743 result.removeRawLink();
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800744 }
745
Simon Hunt44031102014-11-11 13:20:36 -0800746 function removeHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800747 evTrace(data);
Simon Hunt44031102014-11-11 13:20:36 -0800748 var host = data.payload,
749 id = host.id,
750 hostData = network.lookup[id];
751 if (hostData) {
752 removeHostElement(hostData);
753 } else {
754 logicError('removeHost lookup fail. ID = "' + id + '"');
755 }
756 }
757
Thomas Vachuska47635c62014-11-22 01:21:36 -0800758 function showSummary(data) {
759 evTrace(data);
760 populateSummary(data.payload);
761 summaryPane.show();
762 }
763
Simon Hunt61d04042014-11-11 17:27:16 -0800764 function showDetails(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800765 evTrace(data);
Simon Hunt61d04042014-11-11 17:27:16 -0800766 populateDetails(data.payload);
767 detailPane.show();
768 }
769
Simon Huntb53e0682014-11-12 13:32:01 -0800770 function showTraffic(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800771 evTrace(data);
Thomas Vachuska4731f122014-11-20 04:56:19 -0800772 var paths = data.payload.paths,
773 hasTraffic = false;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800774
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800775 // Revert any links hilighted previously.
Thomas Vachuska4731f122014-11-20 04:56:19 -0800776 link.style('stroke-width', null)
Thomas Vachuskaa3148a72014-11-19 21:38:35 -0800777 .classed('primary secondary animated optical', false);
Simon Hunte2575b62014-11-18 15:25:53 -0800778 // Remove all previous labels.
779 removeLinkLabels();
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800780
Simon Hunte2575b62014-11-18 15:25:53 -0800781 // Now hilight all links in the paths payload, and attach
782 // labels to them, if they are defined.
Simon Hunta255a2c2014-11-13 22:29:35 -0800783 paths.forEach(function (p) {
Simon Hunte2575b62014-11-18 15:25:53 -0800784 var n = p.links.length,
785 i,
786 ldata;
787
Thomas Vachuska4731f122014-11-20 04:56:19 -0800788 hasTraffic = hasTraffic || p.traffic;
Simon Hunte2575b62014-11-18 15:25:53 -0800789 for (i=0; i<n; i++) {
790 ldata = findLinkById(p.links[i]);
Thomas Vachuska4731f122014-11-20 04:56:19 -0800791 if (ldata && ldata.el) {
Simon Hunte2575b62014-11-18 15:25:53 -0800792 ldata.el.classed(p.class, true);
793 ldata.label = p.labels[i];
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800794 }
Simon Hunte2575b62014-11-18 15:25:53 -0800795 }
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800796 });
Thomas Vachuska4731f122014-11-20 04:56:19 -0800797
Simon Hunte2575b62014-11-18 15:25:53 -0800798 updateLinks();
Thomas Vachuska4731f122014-11-20 04:56:19 -0800799
800 if (hasTraffic && !antTimer) {
801 startAntTimer();
802 } else if (!hasTraffic && antTimer) {
803 stopAntTimer();
804 }
Simon Huntb53e0682014-11-12 13:32:01 -0800805 }
806
Simon Hunt56d51852014-11-09 13:03:35 -0800807 // ...............................
808
809 function stillToImplement(data) {
810 var p = data.payload;
811 note(data.event, p.id);
Simon Hunt7b403bc2014-11-22 19:01:00 -0800812 if (!config.useLiveData) {
813 network.view.alert('Not yet implemented: "' + data.event + '"');
814 }
Simon Hunt56d51852014-11-09 13:03:35 -0800815 }
Simon Hunt99c13842014-11-06 18:23:12 -0800816
817 function unknownEvent(data) {
Simon Hunt434cf142014-11-24 11:10:28 -0800818 console.warn('Unknown event type: "' + data.event + '"', data);
Simon Hunt99c13842014-11-06 18:23:12 -0800819 }
820
821 function handleServerEvent(data) {
822 var fn = eventDispatch[data.event] || unknownEvent;
823 fn(data);
824 }
825
826 // ==============================
Simon Hunt61d04042014-11-11 17:27:16 -0800827 // Out-going messages...
828
Simon Huntb53e0682014-11-12 13:32:01 -0800829 function nSel() {
830 return selectOrder.length;
831 }
Simon Hunt61d04042014-11-11 17:27:16 -0800832 function getSel(idx) {
833 return selections[selectOrder[idx]];
834 }
Simon Huntb53e0682014-11-12 13:32:01 -0800835 function getSelId(idx) {
836 return getSel(idx).obj.id;
837 }
Thomas Vachuska9edca302014-11-22 17:06:42 -0800838 function getSelIds(start, endOffset) {
839 var end = selectOrder.length - endOffset;
840 var ids = [];
841 selectOrder.slice(start, end).forEach(function (d) {
842 ids.push(getSelId(d));
843 });
844 return ids;
845 }
Simon Huntb53e0682014-11-12 13:32:01 -0800846 function allSelectionsClass(cls) {
847 for (var i=0, n=nSel(); i<n; i++) {
848 if (getSel(i).obj.class !== cls) {
849 return false;
850 }
851 }
852 return true;
853 }
Simon Hunt61d04042014-11-11 17:27:16 -0800854
Thomas Vachuska47635c62014-11-22 01:21:36 -0800855 function toggleInstances() {
856 if (!oiBox.isVisible()) {
Simon Huntb0ecfa52014-11-23 21:05:12 -0800857 showInstances();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800858 } else {
Simon Huntb0ecfa52014-11-23 21:05:12 -0800859 hideInstances();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800860 }
861 }
862
Simon Huntb0ecfa52014-11-23 21:05:12 -0800863 function showInstances() {
864 oiBox.show();
865 colorAffinity = true;
866 updateDeviceColors();
867 }
868
869 function hideInstances() {
870 oiBox.hide();
871 colorAffinity = false;
872 cancelAffinity();
873 updateDeviceColors();
874 }
875
876
Thomas Vachuska47635c62014-11-22 01:21:36 -0800877 function toggleSummary() {
878 if (!summaryPane.isVisible()) {
879 requestSummary();
880 } else {
881 cancelSummary();
882 }
883 }
884
885 // request overall summary data
886 function requestSummary() {
887 sendMessage('requestSummary', {});
888 }
889
890 function cancelSummary() {
891 sendMessage('cancelSummary', {});
892 summaryPane.hide();
893 }
894
Simon Hunt61d04042014-11-11 17:27:16 -0800895 // request details for the selected element
Simon Huntd72bc702014-11-13 18:38:04 -0800896 // invoked from selection of a single node.
Simon Hunt61d04042014-11-11 17:27:16 -0800897 function requestDetails() {
898 var data = getSel(0).obj,
899 payload = {
900 id: data.id,
901 class: data.class
902 };
903 sendMessage('requestDetails', payload);
904 }
905
Thomas Vachuska9edca302014-11-22 17:06:42 -0800906 function addHostIntentAction() {
Simon Huntd72bc702014-11-13 18:38:04 -0800907 sendMessage('addHostIntent', {
Thomas Vachuska9edca302014-11-22 17:06:42 -0800908 one: selectOrder[0],
909 two: selectOrder[1],
910 ids: selectOrder
Simon Huntd72bc702014-11-13 18:38:04 -0800911 });
Thomas Vachuska9edca302014-11-22 17:06:42 -0800912 network.view.flash('Host-to-Host flow added');
Simon Huntd72bc702014-11-13 18:38:04 -0800913 }
914
Thomas Vachuska9edca302014-11-22 17:06:42 -0800915 function addMultiSourceIntentAction() {
916 sendMessage('addMultiSourceIntent', {
917 src: selectOrder.slice(0, selectOrder.length - 1),
918 dst: selectOrder[selectOrder.length - 1],
919 ids: selectOrder
920 });
921 network.view.flash('Multi-Source flow added');
Thomas Vachuska29617e52014-11-20 03:17:46 -0800922 }
923
Thomas Vachuska9edca302014-11-22 17:06:42 -0800924
Thomas Vachuska47635c62014-11-22 01:21:36 -0800925 function cancelTraffic() {
926 sendMessage('cancelTraffic', {});
927 }
928
Thomas Vachuska9edca302014-11-22 17:06:42 -0800929 function requestTrafficForMode() {
930 if (hoverMode === hoverModeAll) {
931 requestAllTraffic();
932 } else if (hoverMode === hoverModeFlows) {
933 requestDeviceLinkFlows();
934 } else if (hoverMode === hoverModeIntents) {
935 requestSelectTraffic();
Simon Huntd72bc702014-11-13 18:38:04 -0800936 }
Thomas Vachuska9edca302014-11-22 17:06:42 -0800937 }
Simon Huntd72bc702014-11-13 18:38:04 -0800938
Thomas Vachuska9edca302014-11-22 17:06:42 -0800939 function showTrafficAction() {
940 hoverMode = hoverModeIntents;
941 requestSelectTraffic();
942 network.view.flash('Related Traffic');
943 }
944
945 function requestSelectTraffic() {
946 if (validateSelectionContext()) {
947 var hoverId = (hoverMode === hoverModeIntents && hovered &&
948 (hovered.class === 'host' || hovered.class === 'device'))
Simon Huntd72bc702014-11-13 18:38:04 -0800949 ? hovered.id : '';
Thomas Vachuska9edca302014-11-22 17:06:42 -0800950 sendMessage('requestTraffic', {
951 ids: selectOrder,
952 hover: hoverId
953 });
954 }
Simon Huntd72bc702014-11-13 18:38:04 -0800955 }
956
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800957
Thomas Vachuska29617e52014-11-20 03:17:46 -0800958 function showDeviceLinkFlowsAction() {
Thomas Vachuska9edca302014-11-22 17:06:42 -0800959 hoverMode = hoverModeFlows;
960 requestDeviceLinkFlows();
Thomas Vachuskac59658c2014-11-21 13:13:36 -0800961 network.view.flash('Device Flows');
Thomas Vachuska29617e52014-11-20 03:17:46 -0800962 }
963
Thomas Vachuska9edca302014-11-22 17:06:42 -0800964 function requestDeviceLinkFlows() {
965 if (validateSelectionContext()) {
966 var hoverId = (hoverMode === hoverModeFlows && hovered &&
967 (hovered.class === 'device')) ? hovered.id : '';
968 sendMessage('requestDeviceLinkFlows', {
969 ids: selectOrder,
970 hover: hoverId
971 });
972 }
973 }
974
975
976 function showAllTrafficAction() {
977 hoverMode = hoverModeAll;
978 requestAllTraffic();
979 network.view.flash('All Traffic');
980 }
981
982 function requestAllTraffic() {
983 sendMessage('requestAllTraffic', {});
984 }
985
986 function validateSelectionContext() {
Thomas Vachuska29617e52014-11-20 03:17:46 -0800987 if (!hovered && nSel() === 0) {
Thomas Vachuska47635c62014-11-22 01:21:36 -0800988 cancelTraffic();
Thomas Vachuska9edca302014-11-22 17:06:42 -0800989 return false;
Thomas Vachuska29617e52014-11-20 03:17:46 -0800990 }
Thomas Vachuska9edca302014-11-22 17:06:42 -0800991 return true;
Thomas Vachuska29617e52014-11-20 03:17:46 -0800992 }
Simon Huntd72bc702014-11-13 18:38:04 -0800993
Simon Hunta6a9fe72014-11-20 11:17:12 -0800994
Simon Hunt61d04042014-11-11 17:27:16 -0800995 // ==============================
Simon Hunta5e89142014-11-14 07:00:33 -0800996 // onos instance panel functions
Simon Huntb82f6902014-11-22 11:53:15 -0800997
Simon Hunt7b403bc2014-11-22 19:01:00 -0800998 var instCfg = {
999 rectPad: 8,
1000 nodeOx: 9,
1001 nodeOy: 9,
1002 nodeDim: 40,
1003 birdOx: 19,
1004 birdOy: 21,
1005 birdDim: 21,
1006 uiDy: 45,
1007 titleDy: 30,
1008 textYOff: 20,
1009 textYSpc: 15
1010 };
1011
1012 function viewBox(dim) {
1013 return '0 0 ' + dim.w + ' ' + dim.h;
1014 }
1015
1016 function instRectAttr(dim) {
1017 var pad = instCfg.rectPad;
1018 return {
1019 x: pad,
1020 y: pad,
1021 width: dim.w - pad*2,
1022 height: dim.h - pad*2,
Simon Huntb0ecfa52014-11-23 21:05:12 -08001023 rx: 6
Simon Hunt7b403bc2014-11-22 19:01:00 -08001024 };
1025 }
1026
1027 function computeDim(self) {
1028 var css = window.getComputedStyle(self);
1029 return {
1030 w: stripPx(css.width),
1031 h: stripPx(css.height)
1032 };
Simon Huntb82f6902014-11-22 11:53:15 -08001033 }
Simon Hunta5e89142014-11-14 07:00:33 -08001034
1035 function updateInstances() {
1036 var onoses = oiBox.el.selectAll('.onosInst')
Simon Huntb82f6902014-11-22 11:53:15 -08001037 .data(onosOrder, function (d) { return d.id; }),
Simon Hunt7b403bc2014-11-22 19:01:00 -08001038 instDim = {w:0,h:0},
1039 c = instCfg;
Simon Hunta5e89142014-11-14 07:00:33 -08001040
Simon Hunt7b403bc2014-11-22 19:01:00 -08001041 function nSw(n) {
1042 return '# Switches: ' + n;
1043 }
Simon Hunta5e89142014-11-14 07:00:33 -08001044
Simon Huntb82f6902014-11-22 11:53:15 -08001045 // operate on existing onos instances if necessary
1046 onoses.each(function (d) {
1047 var el = d3.select(this),
1048 svg = el.select('svg');
Simon Hunt7b403bc2014-11-22 19:01:00 -08001049 instDim = computeDim(this);
Simon Huntb82f6902014-11-22 11:53:15 -08001050
1051 // update online state
1052 el.classed('online', d.online);
1053
1054 // update ui-attached state
1055 svg.select('use.uiBadge').remove();
1056 if (d.uiAttached) {
1057 attachUiBadge(svg);
1058 }
1059
Simon Hunt7b403bc2014-11-22 19:01:00 -08001060 function updAttr(id, value) {
1061 svg.select('text.instLabel.'+id).text(value);
1062 }
1063
1064 updAttr('ip', d.ip);
1065 updAttr('ns', nSw(d.switches));
Simon Huntb82f6902014-11-22 11:53:15 -08001066 });
1067
1068
1069 // operate on new onos instances
Simon Hunta5e89142014-11-14 07:00:33 -08001070 var entering = onoses.enter()
1071 .append('div')
1072 .attr('class', 'onosInst')
1073 .classed('online', function (d) { return d.online; })
Simon Hunt9c15eca2014-11-15 18:37:59 -08001074 .on('click', clickInst);
1075
Simon Huntb82f6902014-11-22 11:53:15 -08001076 entering.each(function (d) {
Simon Hunt9c15eca2014-11-15 18:37:59 -08001077 var el = d3.select(this),
Simon Hunt7b403bc2014-11-22 19:01:00 -08001078 rectAttr,
1079 svg;
1080 instDim = computeDim(this);
1081 rectAttr = instRectAttr(instDim);
Simon Hunt9c15eca2014-11-15 18:37:59 -08001082
Simon Hunt7b403bc2014-11-22 19:01:00 -08001083 svg = el.append('svg').attr({
1084 width: instDim.w,
1085 height: instDim.h,
1086 viewBox: viewBox(instDim)
Simon Hunt95908012014-11-20 10:20:26 -08001087 });
Simon Huntb82f6902014-11-22 11:53:15 -08001088
Simon Hunt7b403bc2014-11-22 19:01:00 -08001089 svg.append('rect').attr(rectAttr);
Simon Hunt9c15eca2014-11-15 18:37:59 -08001090
Simon Hunt7b403bc2014-11-22 19:01:00 -08001091 appendGlyph(svg, c.nodeOx, c.nodeOy, c.nodeDim, '#node');
1092 appendBadge(svg, c.birdOx, c.birdOy, c.birdDim, '#bird');
Simon Huntb82f6902014-11-22 11:53:15 -08001093
1094 if (d.uiAttached) {
1095 attachUiBadge(svg);
1096 }
1097
Simon Hunt7b403bc2014-11-22 19:01:00 -08001098 var left = c.nodeOx + c.nodeDim,
1099 len = rectAttr.width - left,
1100 hlen = len / 2,
1101 midline = hlen + left;
Simon Hunt9c15eca2014-11-15 18:37:59 -08001102
Simon Hunt7b403bc2014-11-22 19:01:00 -08001103 // title
1104 svg.append('text')
1105 .attr({
1106 class: 'instTitle',
1107 x: midline,
1108 y: c.titleDy
1109 })
1110 .text(d.id);
1111
1112 // a couple of attributes
1113 var ty = c.titleDy + c.textYOff;
1114
1115 function addAttr(id, label) {
1116 svg.append('text').attr({
1117 class: 'instLabel ' + id,
1118 x: midline,
1119 y: ty
1120 }).text(label);
1121 ty += c.textYSpc;
1122 }
1123
1124 addAttr('ip', d.ip);
1125 addAttr('ns', nSw(d.switches));
Simon Hunt9c15eca2014-11-15 18:37:59 -08001126 });
Simon Hunta5e89142014-11-14 07:00:33 -08001127
1128 // operate on existing + new onoses here
Simon Hunt8f40cce2014-11-23 15:57:30 -08001129 // set the affinity colors...
1130 onoses.each(function (d) {
1131 var el = d3.select(this),
1132 rect = el.select('svg').select('rect'),
1133 col = instColor(d.id, d.online);
1134 rect.style('fill', col);
1135 });
Simon Hunta5e89142014-11-14 07:00:33 -08001136
Simon Hunt7b403bc2014-11-22 19:01:00 -08001137 // adjust the panel size appropriately...
1138 oiBox.width(instDim.w * onosOrder.length);
1139 oiBox.height(instDim.h);
1140
1141 // remove any outgoing instances
1142 onoses.exit().remove();
Simon Hunta5e89142014-11-14 07:00:33 -08001143 }
1144
Simon Hunt8f40cce2014-11-23 15:57:30 -08001145 function instColor(id, online) {
1146 return cat7.get(id, !online, network.view.getTheme());
1147 }
1148
Simon Hunt9462e8c2014-11-14 17:28:09 -08001149 function clickInst(d) {
1150 var el = d3.select(this),
1151 aff = el.classed('affinity');
1152 if (!aff) {
1153 setAffinity(el, d);
1154 } else {
1155 cancelAffinity();
1156 }
1157 }
1158
1159 function setAffinity(el, d) {
1160 d3.selectAll('.onosInst')
1161 .classed('mastership', true)
1162 .classed('affinity', false);
1163 el.classed('affinity', true);
1164
1165 suppressLayers(true);
1166 node.each(function (n) {
1167 if (n.master === d.id) {
1168 n.el.classed('suppressed', false);
1169 }
1170 });
1171 oiShowMaster = true;
1172 }
1173
1174 function cancelAffinity() {
1175 d3.selectAll('.onosInst')
1176 .classed('mastership affinity', false);
1177 restoreLayerState();
1178 oiShowMaster = false;
1179 }
1180
Simon Hunt7b403bc2014-11-22 19:01:00 -08001181 // TODO: these should be moved out to utility module.
1182 function stripPx(s) {
1183 return s.replace(/px$/,'');
1184 }
1185
1186 function appendUse(svg, ox, oy, dim, iid, cls) {
1187 var use = svg.append('use').attr({
1188 transform: translate(ox,oy),
1189 'xlink:href': iid,
1190 width: dim,
1191 height: dim
1192 });
1193 if (cls) {
1194 use.classed(cls, true);
1195 }
1196 return use;
1197 }
1198
1199 function appendGlyph(svg, ox, oy, dim, iid, cls) {
1200 appendUse(svg, ox, oy, dim, iid, cls).classed('glyphIcon', true);
1201 }
1202
1203 function appendBadge(svg, ox, oy, dim, iid, cls) {
1204 appendUse(svg, ox, oy, dim, iid,cls ).classed('badgeIcon', true);
1205 }
1206
1207 function attachUiBadge(svg) {
1208 appendBadge(svg, 12, instCfg.uiDy, 30, '#uiAttached', 'uiBadge');
1209 }
1210
Simon Hunt434cf142014-11-24 11:10:28 -08001211 function visVal(b) {
1212 return b ? 'visible' : 'hidden';
1213 }
1214
Simon Hunta5e89142014-11-14 07:00:33 -08001215 // ==============================
Simon Hunt99c13842014-11-06 18:23:12 -08001216 // force layout modification functions
1217
1218 function translate(x, y) {
1219 return 'translate(' + x + ',' + y + ')';
1220 }
1221
Simon Hunte2575b62014-11-18 15:25:53 -08001222 function rotate(deg) {
1223 return 'rotate(' + deg + ')';
1224 }
1225
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001226 function missMsg(what, id) {
1227 return '\n[' + what + '] "' + id + '" missing ';
1228 }
1229
1230 function linkEndPoints(srcId, dstId) {
1231 var srcNode = network.lookup[srcId],
1232 dstNode = network.lookup[dstId],
1233 sMiss = !srcNode ? missMsg('src', srcId) : '',
1234 dMiss = !dstNode ? missMsg('dst', dstId) : '';
1235
1236 if (sMiss || dMiss) {
1237 logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
1238 return null;
1239 }
1240 return {
1241 source: srcNode,
1242 target: dstNode,
1243 x1: srcNode.x,
1244 y1: srcNode.y,
1245 x2: dstNode.x,
1246 y2: dstNode.y
1247 };
1248 }
1249
Simon Hunt56d51852014-11-09 13:03:35 -08001250 function createHostLink(host) {
1251 var src = host.id,
1252 dst = host.cp.device,
Simon Hunt7cd48f32014-11-09 23:42:50 -08001253 id = host.ingress,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001254 lnk = linkEndPoints(src, dst);
Simon Hunt56d51852014-11-09 13:03:35 -08001255
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001256 if (!lnk) {
Simon Hunt56d51852014-11-09 13:03:35 -08001257 return null;
1258 }
1259
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001260 // Synthesize link ...
1261 $.extend(lnk, {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001262 key: id,
Simon Hunt56d51852014-11-09 13:03:35 -08001263 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -08001264
1265 type: function () { return 'hostLink'; },
1266 // TODO: ideally, we should see if our edge switch is online...
1267 online: function () { return true; },
1268 linkWidth: function () { return 1; }
Simon Hunt7cd48f32014-11-09 23:42:50 -08001269 });
Simon Hunt99c13842014-11-06 18:23:12 -08001270 return lnk;
1271 }
1272
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001273 function createLink(link) {
Simon Hunta6a9fe72014-11-20 11:17:12 -08001274 var lnk = linkEndPoints(link.src, link.dst);
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001275
1276 if (!lnk) {
1277 return null;
1278 }
1279
Simon Hunt8257f4c2014-11-16 19:34:54 -08001280 $.extend(lnk, {
1281 key: link.id,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001282 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -08001283 fromSource: link,
1284
1285 // functions to aggregate dual link state
1286 type: function () {
1287 var s = lnk.fromSource,
1288 t = lnk.fromTarget;
1289 return (s && s.type) || (t && t.type) || defaultLinkType;
1290 },
1291 online: function () {
1292 var s = lnk.fromSource,
1293 t = lnk.fromTarget;
1294 return (s && s.online) || (t && t.online);
1295 },
1296 linkWidth: function () {
1297 var s = lnk.fromSource,
1298 t = lnk.fromTarget,
1299 ws = (s && s.linkWidth) || 0,
1300 wt = (t && t.linkWidth) || 0;
1301 return Math.max(ws, wt);
1302 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001303 });
1304 return lnk;
Simon Hunt1a9eff92014-11-07 11:06:34 -08001305 }
1306
Simon Hunte2575b62014-11-18 15:25:53 -08001307 function removeLinkLabels() {
1308 network.links.forEach(function (d) {
1309 d.label = '';
1310 });
1311 }
1312
Simon Hunt434cf142014-11-24 11:10:28 -08001313 function showHostVis(el) {
1314 el.style('visibility', visVal(showHosts));
1315 }
1316
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001317 var widthRatio = 1.4,
1318 linkScale = d3.scale.linear()
1319 .domain([1, 12])
1320 .range([widthRatio, 12 * widthRatio])
1321 .clamp(true);
1322
Simon Hunt99c13842014-11-06 18:23:12 -08001323 function updateLinks() {
1324 link = linkG.selectAll('.link')
Simon Hunt8257f4c2014-11-16 19:34:54 -08001325 .data(network.links, function (d) { return d.key; });
Simon Hunt99c13842014-11-06 18:23:12 -08001326
1327 // operate on existing links, if necessary
1328 // link .foo() .bar() ...
1329
1330 // operate on entering links:
1331 var entering = link.enter()
1332 .append('line')
1333 .attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001334 x1: function (d) { return d.x1; },
1335 y1: function (d) { return d.y1; },
1336 x2: function (d) { return d.x2; },
1337 y2: function (d) { return d.y2; },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001338 stroke: config.topo.linkInColor,
1339 'stroke-width': config.topo.linkInWidth
Simon Hunt99c13842014-11-06 18:23:12 -08001340 });
1341
1342 // augment links
Simon Hunt7cd48f32014-11-09 23:42:50 -08001343 entering.each(function (d) {
1344 var link = d3.select(this);
1345 // provide ref to element selection from backing data....
1346 d.el = link;
Simon Hunt8257f4c2014-11-16 19:34:54 -08001347 restyleLinkElement(d);
Simon Hunt434cf142014-11-24 11:10:28 -08001348 if (d.type() === 'hostLink') {
1349 showHostVis(link);
1350 }
Simon Hunt7cd48f32014-11-09 23:42:50 -08001351 });
Thomas Vachuska4830d392014-11-09 17:09:56 -08001352
1353 // operate on both existing and new links, if necessary
1354 //link .foo() .bar() ...
1355
Simon Hunte2575b62014-11-18 15:25:53 -08001356 // apply or remove labels
1357 var labelData = getLabelData();
1358 applyLinkLabels(labelData);
1359
Thomas Vachuska4830d392014-11-09 17:09:56 -08001360 // operate on exiting links:
Thomas Vachuska4830d392014-11-09 17:09:56 -08001361 link.exit()
Simon Hunt13bf9c82014-11-18 07:26:44 -08001362 .attr('stroke-dasharray', '3, 3')
1363 .style('opacity', 0.5)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001364 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -08001365 .duration(1500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001366 .attr({
Simon Hunt13bf9c82014-11-18 07:26:44 -08001367 'stroke-dasharray': '3, 12',
1368 stroke: config.topo.linkOutColor,
1369 'stroke-width': config.topo.linkOutWidth
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001370 })
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001371 .style('opacity', 0.0)
Thomas Vachuska4830d392014-11-09 17:09:56 -08001372 .remove();
Simon Hunte2575b62014-11-18 15:25:53 -08001373
1374 // NOTE: invoke a single tick to force the labels to position
1375 // onto their links.
1376 tick();
1377 }
1378
1379 function getLabelData() {
1380 // create the backing data for showing labels..
1381 var data = [];
1382 link.each(function (d) {
1383 if (d.label) {
1384 data.push({
1385 id: 'lab-' + d.key,
1386 key: d.key,
1387 label: d.label,
1388 ldata: d
1389 });
1390 }
1391 });
1392 return data;
1393 }
1394
1395 var linkLabelOffset = '0.3em';
1396
1397 function applyLinkLabels(data) {
1398 var entering;
1399
1400 linkLabel = linkLabelG.selectAll('.linkLabel')
1401 .data(data, function (d) { return d.id; });
1402
Simon Hunt56a2ea42014-11-19 12:39:31 -08001403 // for elements already existing, we need to update the text
1404 // and adjust the rectangle size to fit
1405 linkLabel.each(function (d) {
1406 var el = d3.select(this),
1407 rect = el.select('rect'),
1408 text = el.select('text');
1409 text.text(d.label);
1410 rect.attr(rectAroundText(el));
1411 });
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -08001412
Simon Hunte2575b62014-11-18 15:25:53 -08001413 entering = linkLabel.enter().append('g')
1414 .classed('linkLabel', true)
1415 .attr('id', function (d) { return d.id; });
1416
1417 entering.each(function (d) {
1418 var el = d3.select(this),
1419 rect,
1420 text,
1421 parms = {
1422 x1: d.ldata.x1,
1423 y1: d.ldata.y1,
1424 x2: d.ldata.x2,
1425 y2: d.ldata.y2
1426 };
1427
1428 d.el = el;
1429 rect = el.append('rect');
1430 text = el.append('text').text(d.label);
1431 rect.attr(rectAroundText(el));
1432 text.attr('dy', linkLabelOffset);
1433
1434 el.attr('transform', transformLabel(parms));
1435 });
1436
1437 // Remove any links that are no longer required.
1438 linkLabel.exit().remove();
1439 }
1440
1441 function rectAroundText(el) {
1442 var text = el.select('text'),
1443 box = text.node().getBBox();
1444
1445 // translate the bbox so that it is centered on [x,y]
1446 box.x = -box.width / 2;
1447 box.y = -box.height / 2;
1448
1449 // add padding
1450 box.x -= 1;
1451 box.width += 2;
1452 return box;
1453 }
1454
1455 function transformLabel(p) {
1456 var dx = p.x2 - p.x1,
1457 dy = p.y2 - p.y1,
1458 xMid = dx/2 + p.x1,
1459 yMid = dy/2 + p.y1;
Simon Hunte2575b62014-11-18 15:25:53 -08001460 return translate(xMid, yMid);
Simon Hunt99c13842014-11-06 18:23:12 -08001461 }
1462
1463 function createDeviceNode(device) {
1464 // start with the object as is
1465 var node = device,
Simon Huntbb282f52014-11-10 11:08:19 -08001466 type = device.type,
Simon Huntc72967b2014-11-20 09:21:42 -08001467 svgCls = type ? 'node device ' + type : 'node device',
1468 labels = device.labels || [];
1469
Simon Hunt99c13842014-11-06 18:23:12 -08001470 // Augment as needed...
1471 node.class = 'device';
Simon Huntbb282f52014-11-10 11:08:19 -08001472 node.svgClass = device.online ? svgCls + ' online' : svgCls;
Simon Hunt99c13842014-11-06 18:23:12 -08001473 positionNode(node);
Simon Hunt99c13842014-11-06 18:23:12 -08001474 return node;
1475 }
1476
Simon Hunt56d51852014-11-09 13:03:35 -08001477 function createHostNode(host) {
1478 // start with the object as is
1479 var node = host;
1480
1481 // Augment as needed...
1482 node.class = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001483 if (!node.type) {
Simon Hunt209155e2014-11-21 12:16:09 -08001484 node.type = 'endstation';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001485 }
Simon Hunt7fa116d2014-11-17 14:16:55 -08001486 node.svgClass = 'node host ' + node.type;
Simon Hunt56d51852014-11-09 13:03:35 -08001487 positionNode(node);
Simon Hunt56d51852014-11-09 13:03:35 -08001488 return node;
1489 }
1490
Simon Hunt62c47542014-11-22 22:16:32 -08001491 function positionNode(node, forUpdate) {
Simon Hunt99c13842014-11-06 18:23:12 -08001492 var meta = node.metaUi,
Simon Huntac9e24f2014-11-12 10:12:21 -08001493 x = meta && meta.x,
1494 y = meta && meta.y,
1495 xy;
Simon Hunt99c13842014-11-06 18:23:12 -08001496
Simon Huntac9e24f2014-11-12 10:12:21 -08001497 // If we have [x,y] already, use that...
Simon Hunt99c13842014-11-06 18:23:12 -08001498 if (x && y) {
1499 node.fixed = true;
Simon Hunt62c47542014-11-22 22:16:32 -08001500 node.px = node.x = x;
1501 node.py = node.y = y;
1502 //node.px = x;
1503 //node.py = y;
Simon Huntac9e24f2014-11-12 10:12:21 -08001504 return;
Simon Hunt99c13842014-11-06 18:23:12 -08001505 }
Simon Huntac9e24f2014-11-12 10:12:21 -08001506
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001507 var location = node.location;
1508 if (location && location.type === 'latlng') {
1509 var coord = geoMapProjection([location.lng, location.lat]);
1510 node.fixed = true;
Simon Hunt62c47542014-11-22 22:16:32 -08001511 node.px = node.x = coord[0];
1512 node.py = node.y = coord[1];
1513 //node.x = coord[0];
1514 //node.y = coord[1];
1515 return true;
1516 }
1517
1518 // if this is a node update (not a node add).. skip randomizer
1519 if (forUpdate) {
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001520 return;
1521 }
1522
Simon Huntac9e24f2014-11-12 10:12:21 -08001523 // Note: Placing incoming unpinned nodes at exactly the same point
1524 // (center of the view) causes them to explode outwards when
1525 // the force layout kicks in. So, we spread them out a bit
1526 // initially, to provide a more serene layout convergence.
1527 // Additionally, if the node is a host, we place it near
1528 // the device it is connected to.
1529
1530 function spread(s) {
1531 return Math.floor((Math.random() * s) - s/2);
1532 }
1533
1534 function randDim(dim) {
1535 return dim / 2 + spread(dim * 0.7071);
1536 }
1537
1538 function rand() {
1539 return {
1540 x: randDim(network.view.width()),
1541 y: randDim(network.view.height())
1542 };
1543 }
1544
1545 function near(node) {
1546 var min = 12,
1547 dx = spread(12),
1548 dy = spread(12);
1549 return {
1550 x: node.x + min + dx,
1551 y: node.y + min + dy
1552 };
1553 }
1554
1555 function getDevice(cp) {
1556 var d = network.lookup[cp.device];
1557 return d || rand();
1558 }
1559
1560 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
1561 $.extend(node, xy);
Simon Hunt99c13842014-11-06 18:23:12 -08001562 }
1563
Simon Hunt99c13842014-11-06 18:23:12 -08001564
Simon Huntc72967b2014-11-20 09:21:42 -08001565 function iconGlyphUrl(d) {
1566 var which = d.type || 'unknown';
1567 return '#' + which;
1568 }
1569
Simon Hunt99c13842014-11-06 18:23:12 -08001570 // returns the newly computed bounding box of the rectangle
1571 function adjustRectToFitText(n) {
1572 var text = n.select('text'),
1573 box = text.node().getBBox(),
1574 lab = config.labels;
1575
1576 text.attr('text-anchor', 'middle')
1577 .attr('y', '-0.8em')
1578 .attr('x', lab.imgPad/2);
1579
1580 // translate the bbox so that it is centered on [x,y]
1581 box.x = -box.width / 2;
1582 box.y = -box.height / 2;
1583
1584 // add padding
1585 box.x -= (lab.padLR + lab.imgPad/2);
1586 box.width += lab.padLR * 2 + lab.imgPad;
1587 box.y -= lab.padTB;
1588 box.height += lab.padTB * 2;
1589
1590 return box;
1591 }
1592
Simon Hunt1a9eff92014-11-07 11:06:34 -08001593 function mkSvgClass(d) {
1594 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
1595 }
1596
Simon Hunt7cd48f32014-11-09 23:42:50 -08001597 function hostLabel(d) {
1598 var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0;
1599 return d.labels[idx];
1600 }
1601 function deviceLabel(d) {
1602 var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0;
1603 return d.labels[idx];
1604 }
Simon Huntc72967b2014-11-20 09:21:42 -08001605 function trimLabel(label) {
1606 return (label && label.trim()) || '';
1607 }
1608
1609 function emptyBox() {
1610 return {
1611 x: -2,
1612 y: -2,
1613 width: 4,
1614 height: 4
1615 };
Simon Hunt7cd48f32014-11-09 23:42:50 -08001616 }
1617
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001618 function updateDeviceLabel(d) {
Simon Huntc72967b2014-11-20 09:21:42 -08001619 var label = trimLabel(deviceLabel(d)),
1620 noLabel = !label,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001621 node = d.el,
Simon Huntc72967b2014-11-20 09:21:42 -08001622 box,
1623 dx,
Simon Hunt395a70c2014-11-22 23:17:40 -08001624 dy,
1625 cfg = config.icons.device;
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001626
1627 node.select('text')
1628 .text(label)
1629 .style('opacity', 0)
1630 .transition()
1631 .style('opacity', 1);
1632
Simon Huntc72967b2014-11-20 09:21:42 -08001633 if (noLabel) {
1634 box = emptyBox();
Simon Hunt395a70c2014-11-22 23:17:40 -08001635 dx = -cfg.dim/2;
1636 dy = -cfg.dim/2;
Simon Huntc72967b2014-11-20 09:21:42 -08001637 } else {
1638 box = adjustRectToFitText(node);
Simon Hunt395a70c2014-11-22 23:17:40 -08001639 dx = box.x + cfg.xoff;
1640 dy = box.y + cfg.yoff;
Simon Huntc72967b2014-11-20 09:21:42 -08001641 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001642
1643 node.select('rect')
1644 .transition()
1645 .attr(box);
1646
Simon Huntc72967b2014-11-20 09:21:42 -08001647 node.select('g.deviceIcon')
Thomas Vachuska89543292014-11-19 11:28:33 -08001648 .transition()
Simon Huntc72967b2014-11-20 09:21:42 -08001649 .attr('transform', translate(dx, dy));
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001650 }
1651
1652 function updateHostLabel(d) {
1653 var label = hostLabel(d),
1654 host = d.el;
1655
1656 host.select('text').text(label);
1657 }
1658
Simon Hunt62c47542014-11-22 22:16:32 -08001659 // FIXME : fold this into updateNodes.
Simon Huntbb282f52014-11-10 11:08:19 -08001660 function updateHostState(hostData) {
1661 updateHostLabel(hostData);
Simon Huntbb282f52014-11-10 11:08:19 -08001662 }
1663
Simon Hunt434cf142014-11-24 11:10:28 -08001664 function updateHostVisibility() {
1665 var v = visVal(showHosts);
1666 nodeG.selectAll('.host').style('visibility', v);
1667 linkG.selectAll('.hostLink').style('visibility', v);
1668 }
1669
Simon Hunt6ac93f32014-11-13 12:17:27 -08001670 function nodeMouseOver(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001671 hovered = d;
Thomas Vachuska9edca302014-11-22 17:06:42 -08001672 requestTrafficForMode();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001673 }
1674
1675 function nodeMouseOut(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001676 hovered = null;
Thomas Vachuska9edca302014-11-22 17:06:42 -08001677 requestTrafficForMode();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001678 }
Simon Huntbb282f52014-11-10 11:08:19 -08001679
Simon Hunteb1514d2014-11-20 09:57:29 -08001680 function addHostIcon(node, radius, iid) {
Thomas Vachuska89543292014-11-19 11:28:33 -08001681 var dim = radius * 1.5,
1682 xlate = -dim / 2;
1683
Simon Hunteb1514d2014-11-20 09:57:29 -08001684 node.append('use').attr({
1685 class: 'glyphIcon hostIcon',
1686 transform: translate(xlate,xlate),
1687 'xlink:href': iid,
1688 width: dim,
1689 height: dim
1690 });
Thomas Vachuska89543292014-11-19 11:28:33 -08001691 }
1692
Simon Hunt99c13842014-11-06 18:23:12 -08001693 function updateNodes() {
1694 node = nodeG.selectAll('.node')
1695 .data(network.nodes, function (d) { return d.id; });
1696
Simon Hunt62c47542014-11-22 22:16:32 -08001697 // operate on existing nodes...
1698 node.filter('.device').each(function (d) {
Simon Hunt62c47542014-11-22 22:16:32 -08001699 var node = d.el;
1700 node.classed('online', d.online);
1701 updateDeviceLabel(d);
1702 positionNode(d, true);
1703 });
1704
1705 node.filter('.host').each(function (d) {
Simon Hunt62c47542014-11-22 22:16:32 -08001706 var node = d.el;
1707 // TODO: appropriate update of host visuals
1708 });
Simon Hunt99c13842014-11-06 18:23:12 -08001709
1710 // operate on entering nodes:
1711 var entering = node.enter()
1712 .append('g')
1713 .attr({
1714 id: function (d) { return safeId(d.id); },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001715 class: mkSvgClass,
Simon Hunt99c13842014-11-06 18:23:12 -08001716 transform: function (d) { return translate(d.x, d.y); },
1717 opacity: 0
1718 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08001719 .call(network.drag)
Simon Hunt6ac93f32014-11-13 12:17:27 -08001720 .on('mouseover', nodeMouseOver)
1721 .on('mouseout', nodeMouseOut)
Simon Hunt99c13842014-11-06 18:23:12 -08001722 .transition()
1723 .attr('opacity', 1);
1724
1725 // augment device nodes...
1726 entering.filter('.device').each(function (d) {
1727 var node = d3.select(this),
Simon Huntc72967b2014-11-20 09:21:42 -08001728 label = trimLabel(deviceLabel(d)),
1729 noLabel = !label,
Simon Hunt99c13842014-11-06 18:23:12 -08001730 box;
1731
Simon Hunt7cd48f32014-11-09 23:42:50 -08001732 // provide ref to element from backing data....
1733 d.el = node;
1734
Simon Hunt62c47542014-11-22 22:16:32 -08001735 node.append('rect').attr({ rx: 5, ry: 5 });
1736 node.append('text').text(label).attr('dy', '1.1em');
Simon Hunt99c13842014-11-06 18:23:12 -08001737 box = adjustRectToFitText(node);
Simon Hunta3dd9572014-11-20 15:22:41 -08001738 node.select('rect').attr(box);
Simon Huntc72967b2014-11-20 09:21:42 -08001739 addDeviceIcon(node, box, noLabel, iconGlyphUrl(d));
Simon Huntc7ee0662014-11-05 16:44:37 -08001740 });
Simon Hunt934c3ce2014-11-05 11:45:07 -08001741
Thomas Vachuska89543292014-11-19 11:28:33 -08001742
Simon Hunt56d51852014-11-09 13:03:35 -08001743 // augment host nodes...
1744 entering.filter('.host').each(function (d) {
1745 var node = d3.select(this),
Simon Hunt395a70c2014-11-22 23:17:40 -08001746 cfg = config.icons.host,
1747 r = cfg.radius[d.type] || cfg.defaultRadius,
Thomas Vachuska89543292014-11-19 11:28:33 -08001748 textDy = r + 10,
Simon Hunteb1514d2014-11-20 09:57:29 -08001749 iid = iconGlyphUrl(d);
Simon Hunt56d51852014-11-09 13:03:35 -08001750
Simon Hunt7cd48f32014-11-09 23:42:50 -08001751 // provide ref to element from backing data....
1752 d.el = node;
Simon Hunt434cf142014-11-24 11:10:28 -08001753 showHostVis(node);
Simon Hunt7cd48f32014-11-09 23:42:50 -08001754
Simon Hunt62c47542014-11-22 22:16:32 -08001755 node.append('circle').attr('r', r);
Simon Hunteb1514d2014-11-20 09:57:29 -08001756 if (iid) {
1757 addHostIcon(node, r, iid);
Simon Hunt7fa116d2014-11-17 14:16:55 -08001758 }
Simon Hunt56d51852014-11-09 13:03:35 -08001759 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001760 .text(hostLabel)
Thomas Vachuska89543292014-11-19 11:28:33 -08001761 .attr('dy', textDy)
Simon Hunt7cd48f32014-11-09 23:42:50 -08001762 .attr('text-anchor', 'middle');
Simon Hunt56d51852014-11-09 13:03:35 -08001763 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001764
Simon Hunt99c13842014-11-06 18:23:12 -08001765 // operate on both existing and new nodes, if necessary
Simon Huntb0ecfa52014-11-23 21:05:12 -08001766 updateDeviceColors();
Simon Huntc7ee0662014-11-05 16:44:37 -08001767
Simon Hunt99c13842014-11-06 18:23:12 -08001768 // operate on exiting nodes:
Simon Huntea80eb42014-11-11 13:46:57 -08001769 // Note that the node is removed after 2 seconds.
1770 // Sub element animations should be shorter than 2 seconds.
1771 var exiting = node.exit()
Simon Hunt44031102014-11-11 13:20:36 -08001772 .transition()
1773 .duration(2000)
Simon Huntea80eb42014-11-11 13:46:57 -08001774 .style('opacity', 0)
Simon Hunt99c13842014-11-06 18:23:12 -08001775 .remove();
Simon Huntea80eb42014-11-11 13:46:57 -08001776
1777 // host node exits....
1778 exiting.filter('.host').each(function (d) {
1779 var node = d3.select(this);
1780
1781 node.select('text')
1782 .style('opacity', 0.5)
1783 .transition()
1784 .duration(1000)
1785 .style('opacity', 0);
1786 // note, leave <g>.remove to remove this element
1787
Thomas Vachuska89543292014-11-19 11:28:33 -08001788 node.select('circle')
1789 .style('stroke-fill', '#555')
1790 .style('fill', '#888')
Simon Huntea80eb42014-11-11 13:46:57 -08001791 .style('opacity', 0.5)
1792 .transition()
1793 .duration(1500)
1794 .attr('r', 0);
1795 // note, leave <g>.remove to remove this element
1796
1797 });
1798
Simon Hunt62c47542014-11-22 22:16:32 -08001799 // TODO: device node exit animation
1800
1801 network.force.resume();
Simon Huntc7ee0662014-11-05 16:44:37 -08001802 }
1803
Simon Hunt95dad922014-11-24 09:43:31 -08001804 var dCol = {
1805 black: '#000',
1806 paleblue: '#acf',
1807 offwhite: '#ddd',
1808 midgrey: '#888',
1809 lightgrey: '#bbb',
1810 orange: '#f90'
1811 };
1812
Simon Huntb0ecfa52014-11-23 21:05:12 -08001813 // note: these are the device icon colors without affinity
Simon Hunt95dad922014-11-24 09:43:31 -08001814 var dColTheme = {
Simon Huntb0ecfa52014-11-23 21:05:12 -08001815 light: {
1816 online: {
Simon Hunt95dad922014-11-24 09:43:31 -08001817 glyph: dCol.black,
1818 rect: dCol.paleblue
Simon Huntb0ecfa52014-11-23 21:05:12 -08001819 },
1820 offline: {
Simon Hunt95dad922014-11-24 09:43:31 -08001821 glyph: dCol.midgrey,
1822 rect: dCol.lightgrey
Simon Huntb0ecfa52014-11-23 21:05:12 -08001823 }
1824 },
Simon Hunt95dad922014-11-24 09:43:31 -08001825 // TODO: theme
Simon Huntb0ecfa52014-11-23 21:05:12 -08001826 dark: {
1827 online: {
Simon Hunt95dad922014-11-24 09:43:31 -08001828 glyph: dCol.black,
1829 rect: dCol.paleblue
Simon Huntb0ecfa52014-11-23 21:05:12 -08001830 },
1831 offline: {
Simon Hunt95dad922014-11-24 09:43:31 -08001832 glyph: dCol.midgrey,
1833 rect: dCol.lightgrey
Simon Huntb0ecfa52014-11-23 21:05:12 -08001834 }
1835 }
1836 };
1837
1838 function devBaseColor(d) {
1839 var t = network.view.getTheme(),
1840 o = d.online ? 'online' : 'offline';
Simon Hunt95dad922014-11-24 09:43:31 -08001841 return dColTheme[t][o];
Simon Huntb0ecfa52014-11-23 21:05:12 -08001842 }
1843
1844 function setDeviceColor(d) {
1845 var o = d.online,
1846 s = d.el.classed('selected'),
1847 c = devBaseColor(d),
1848 a = instColor(d.master, o),
1849 g, r,
1850 icon = d.el.select('g.deviceIcon');
1851
1852 if (s) {
1853 g = c.glyph;
Simon Hunt95dad922014-11-24 09:43:31 -08001854 r = dColTheme.sel;
Simon Huntb0ecfa52014-11-23 21:05:12 -08001855 } else if (colorAffinity) {
1856 g = o ? a : c.glyph;
Simon Hunt95dad922014-11-24 09:43:31 -08001857 r = o ? dCol.offwhite : a;
Simon Huntb0ecfa52014-11-23 21:05:12 -08001858 } else {
1859 g = c.glyph;
1860 r = c.rect;
1861 }
1862
1863 icon.select('use')
1864 .style('fill', g);
1865 icon.select('rect')
1866 .style('fill', r);
1867 }
1868
Simon Huntc72967b2014-11-20 09:21:42 -08001869 function addDeviceIcon(node, box, noLabel, iid) {
1870 var cfg = config.icons.device,
1871 dx,
1872 dy,
1873 g;
1874
1875 if (noLabel) {
1876 box = emptyBox();
1877 dx = -cfg.dim/2;
1878 dy = -cfg.dim/2;
1879 } else {
1880 box = adjustRectToFitText(node);
Simon Hunt395a70c2014-11-22 23:17:40 -08001881 dx = box.x + cfg.xoff;
1882 dy = box.y + cfg.yoff;
Simon Huntc72967b2014-11-20 09:21:42 -08001883 }
1884
Simon Hunteb1514d2014-11-20 09:57:29 -08001885 g = node.append('g')
1886 .attr('class', 'glyphIcon deviceIcon')
Simon Huntc72967b2014-11-20 09:21:42 -08001887 .attr('transform', translate(dx, dy));
1888
1889 g.append('rect').attr({
1890 x: 0,
1891 y: 0,
1892 rx: cfg.rx,
1893 width: cfg.dim,
1894 height: cfg.dim
1895 });
1896
1897 g.append('use').attr({
1898 'xlink:href': iid,
1899 width: cfg.dim,
1900 height: cfg.dim
1901 });
1902
Simon Huntc72967b2014-11-20 09:21:42 -08001903 }
1904
Simon Hunt7b403bc2014-11-22 19:01:00 -08001905 function find(key, array, tag) {
1906 var _tag = tag || 'key',
1907 idx, n, d;
1908 for (idx = 0, n = array.length; idx < n; idx++) {
1909 d = array[idx];
1910 if (d[_tag] === key) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001911 return idx;
1912 }
1913 }
1914 return -1;
1915 }
1916
1917 function removeLinkElement(linkData) {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001918 var idx = find(linkData.key, network.links),
1919 removed;
1920 if (idx >=0) {
1921 // remove from links array
1922 removed = network.links.splice(idx, 1);
1923 // remove from lookup cache
1924 delete network.lookup[removed[0].key];
1925 updateLinks();
1926 network.force.resume();
1927 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001928 }
Simon Huntc7ee0662014-11-05 16:44:37 -08001929
Simon Hunt44031102014-11-11 13:20:36 -08001930 function removeHostElement(hostData) {
1931 // first, remove associated hostLink...
1932 removeLinkElement(hostData.linkData);
1933
1934 // remove from lookup cache
1935 delete network.lookup[hostData.id];
1936 // remove from nodes array
1937 var idx = find(hostData.id, network.nodes);
1938 network.nodes.splice(idx, 1);
1939 // remove from SVG
1940 updateNodes();
1941 network.force.resume();
1942 }
1943
1944
Simon Huntc7ee0662014-11-05 16:44:37 -08001945 function tick() {
1946 node.attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001947 transform: function (d) { return translate(d.x, d.y); }
Simon Huntc7ee0662014-11-05 16:44:37 -08001948 });
1949
1950 link.attr({
1951 x1: function (d) { return d.source.x; },
1952 y1: function (d) { return d.source.y; },
1953 x2: function (d) { return d.target.x; },
1954 y2: function (d) { return d.target.y; }
1955 });
Simon Hunte2575b62014-11-18 15:25:53 -08001956
1957 linkLabel.each(function (d) {
1958 var el = d3.select(this);
Thomas Vachuska4731f122014-11-20 04:56:19 -08001959 var lnk = findLinkById(d.key);
1960
1961 if (lnk) {
1962 var parms = {
Simon Hunte2575b62014-11-18 15:25:53 -08001963 x1: lnk.source.x,
1964 y1: lnk.source.y,
1965 x2: lnk.target.x,
1966 y2: lnk.target.y
1967 };
Thomas Vachuska4731f122014-11-20 04:56:19 -08001968 el.attr('transform', transformLabel(parms));
1969 }
Simon Hunte2575b62014-11-18 15:25:53 -08001970 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001971 }
Simon Hunt934c3ce2014-11-05 11:45:07 -08001972
1973 // ==============================
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001974 // Web-Socket for live data
1975
1976 function webSockUrl() {
1977 return document.location.toString()
1978 .replace(/\#.*/, '')
1979 .replace('http://', 'ws://')
1980 .replace('https://', 'wss://')
1981 .replace('index2.html', config.webSockUrl);
1982 }
1983
1984 webSock = {
1985 ws : null,
1986
1987 connect : function() {
1988 webSock.ws = new WebSocket(webSockUrl());
1989
1990 webSock.ws.onopen = function() {
Simon Hunt0c6d4192014-11-12 12:07:10 -08001991 noWebSock(false);
Thomas Vachuska47635c62014-11-22 01:21:36 -08001992 requestSummary();
Simon Huntb0ecfa52014-11-23 21:05:12 -08001993 showInstances();
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001994 };
1995
1996 webSock.ws.onmessage = function(m) {
1997 if (m.data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001998 wsTraceRx(m.data);
Thomas Vachuskad472c6e2014-11-07 19:11:05 -08001999 handleServerEvent(JSON.parse(m.data));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002000 }
2001 };
2002
2003 webSock.ws.onclose = function(m) {
2004 webSock.ws = null;
Simon Hunt0c6d4192014-11-12 12:07:10 -08002005 noWebSock(true);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002006 };
2007 },
2008
2009 send : function(text) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002010 if (text != null) {
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002011 webSock._send(text);
2012 }
2013 },
2014
2015 _send : function(message) {
2016 if (webSock.ws) {
2017 webSock.ws.send(message);
Simon Hunta255a2c2014-11-13 22:29:35 -08002018 } else if (config.useLiveData) {
Simon Hunt434cf142014-11-24 11:10:28 -08002019 console.warn('no web socket open', message);
Simon Hunta255a2c2014-11-13 22:29:35 -08002020 } else {
Simon Hunt434cf142014-11-24 11:10:28 -08002021 console.log('WS Send: ', message);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002022 }
2023 }
2024
2025 };
2026
Simon Hunt0c6d4192014-11-12 12:07:10 -08002027 function noWebSock(b) {
2028 mask.style('display',b ? 'block' : 'none');
2029 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002030
2031 function sendMessage(evType, payload) {
2032 var toSend = {
Simon Huntbb282f52014-11-10 11:08:19 -08002033 event: evType,
2034 sid: ++sid,
2035 payload: payload
2036 },
2037 asText = JSON.stringify(toSend);
2038 wsTraceTx(asText);
2039 webSock.send(asText);
Simon Huntc76ae892014-11-18 17:31:51 -08002040
2041 // Temporary measure for debugging UI behavior ...
2042 if (!config.useLiveData) {
2043 handleTestSend(toSend);
2044 }
Simon Huntbb282f52014-11-10 11:08:19 -08002045 }
2046
2047 function wsTraceTx(msg) {
2048 wsTrace('tx', msg);
2049 }
2050 function wsTraceRx(msg) {
2051 wsTrace('rx', msg);
2052 }
2053 function wsTrace(rxtx, msg) {
Simon Huntbb282f52014-11-10 11:08:19 -08002054 console.log('[' + rxtx + '] ' + msg);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002055 }
2056
Simon Huntc76ae892014-11-18 17:31:51 -08002057 // NOTE: Temporary hardcoded example for showing detail pane
2058 // while we fine-
2059 // Probably should not merge this change...
2060 function handleTestSend(msg) {
2061 if (msg.event === 'requestDetails') {
2062 showDetails({
2063 event: 'showDetails',
2064 sid: 1001,
2065 payload: {
2066 "id": "of:0000ffffffffff09",
2067 "type": "roadm",
2068 "propOrder": [
2069 "Name",
2070 "Vendor",
2071 "H/W Version",
2072 "S/W Version",
2073 "-",
2074 "Latitude",
2075 "Longitude",
2076 "Ports"
2077 ],
2078 "props": {
2079 "Name": null,
2080 "Vendor": "Linc",
2081 "H/W Version": "OE",
2082 "S/W Version": "?",
2083 "-": "",
2084 "Latitude": "40.8",
2085 "Longitude": "73.1",
2086 "Ports": "2"
2087 }
2088 }
2089 });
2090 }
2091 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002092
2093 // ==============================
2094 // Selection stuff
2095
2096 function selectObject(obj, el) {
2097 var n,
Simon Hunt01095ff2014-11-13 16:37:29 -08002098 srcEv = d3.event.sourceEvent,
2099 meta = srcEv.metaKey,
2100 shift = srcEv.shiftKey;
2101
Simon Huntdeab4322014-11-13 18:49:07 -08002102 if ((panZoom() && !meta) || (!panZoom() && meta)) {
Simon Hunt01095ff2014-11-13 16:37:29 -08002103 return;
2104 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002105
2106 if (el) {
2107 n = d3.select(el);
2108 } else {
2109 node.each(function(d) {
2110 if (d == obj) {
2111 n = d3.select(el = this);
2112 }
2113 });
2114 }
2115 if (!n) return;
2116
Simon Hunt01095ff2014-11-13 16:37:29 -08002117 if (shift && n.classed('selected')) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002118 deselectObject(obj.id);
Simon Hunt61d04042014-11-11 17:27:16 -08002119 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002120 return;
2121 }
2122
Simon Hunt01095ff2014-11-13 16:37:29 -08002123 if (!shift) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002124 deselectAll();
2125 }
2126
Simon Huntc31d5692014-11-12 13:27:18 -08002127 selections[obj.id] = { obj: obj, el: el };
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002128 selectOrder.push(obj.id);
2129
2130 n.classed('selected', true);
Simon Huntb0ecfa52014-11-23 21:05:12 -08002131 updateDeviceColors(obj);
Simon Hunt61d04042014-11-11 17:27:16 -08002132 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002133 }
2134
2135 function deselectObject(id) {
Simon Huntc31d5692014-11-12 13:27:18 -08002136 var obj = selections[id],
2137 idx;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002138 if (obj) {
2139 d3.select(obj.el).classed('selected', false);
Simon Hunt61d04042014-11-11 17:27:16 -08002140 delete selections[id];
Simon Huntc31d5692014-11-12 13:27:18 -08002141 idx = $.inArray(id, selectOrder);
2142 if (idx >= 0) {
2143 selectOrder.splice(idx, 1);
2144 }
Simon Huntb0ecfa52014-11-23 21:05:12 -08002145 updateDeviceColors(obj.obj);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002146 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002147 }
2148
2149 function deselectAll() {
2150 // deselect all nodes in the network...
2151 node.classed('selected', false);
2152 selections = {};
2153 selectOrder = [];
Simon Huntb0ecfa52014-11-23 21:05:12 -08002154 updateDeviceColors();
Simon Hunt61d04042014-11-11 17:27:16 -08002155 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002156 }
2157
Simon Huntb0ecfa52014-11-23 21:05:12 -08002158 function updateDeviceColors(d) {
2159 if (d) {
2160 setDeviceColor(d);
2161 } else {
2162 node.filter('.device').each(function (d) {
2163 setDeviceColor(d);
2164 });
2165 }
Thomas Vachuska47635c62014-11-22 01:21:36 -08002166 }
2167
Simon Hunt61d04042014-11-11 17:27:16 -08002168 // update the state of the detail pane, based on current selections
2169 function updateDetailPane() {
2170 var nSel = selectOrder.length;
2171 if (!nSel) {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002172 emptySelect();
Simon Hunt61d04042014-11-11 17:27:16 -08002173 } else if (nSel === 1) {
2174 singleSelect();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002175 requestTrafficForMode();
Simon Hunt61d04042014-11-11 17:27:16 -08002176 } else {
2177 multiSelect();
2178 }
2179 }
2180
Thomas Vachuska9edca302014-11-22 17:06:42 -08002181 function emptySelect() {
2182 detailPane.hide();
2183 cancelTraffic();
2184 }
2185
Simon Hunt61d04042014-11-11 17:27:16 -08002186 function singleSelect() {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002187 // NOTE: detail is shown from showDetails event callback
Simon Hunt61d04042014-11-11 17:27:16 -08002188 requestDetails();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002189 requestTrafficForMode();
Simon Hunt61d04042014-11-11 17:27:16 -08002190 }
2191
2192 function multiSelect() {
Simon Huntb53e0682014-11-12 13:32:01 -08002193 populateMultiSelect();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002194 requestTrafficForMode();
Simon Huntb53e0682014-11-12 13:32:01 -08002195 }
2196
2197 function addSep(tbody) {
2198 var tr = tbody.append('tr');
2199 $('<hr>').appendTo(tr.append('td').attr('colspan', 2));
2200 }
2201
2202 function addProp(tbody, label, value) {
2203 var tr = tbody.append('tr');
2204
2205 tr.append('td')
2206 .attr('class', 'label')
2207 .text(label + ' :');
2208
2209 tr.append('td')
2210 .attr('class', 'value')
2211 .text(value);
2212 }
2213
2214 function populateMultiSelect() {
2215 detailPane.empty();
2216
Simon Hunta3dd9572014-11-20 15:22:41 -08002217 var title = detailPane.append('h3'),
2218 table = detailPane.append('table'),
2219 tbody = table.append('tbody');
Simon Huntb53e0682014-11-12 13:32:01 -08002220
Thomas Vachuska4731f122014-11-20 04:56:19 -08002221 title.text('Selected Nodes');
Simon Huntb53e0682014-11-12 13:32:01 -08002222
2223 selectOrder.forEach(function (d, i) {
2224 addProp(tbody, i+1, d);
2225 });
Simon Huntd72bc702014-11-13 18:38:04 -08002226
2227 addMultiSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08002228 }
2229
Thomas Vachuska47635c62014-11-22 01:21:36 -08002230 // TODO: refactor to consolidate with populateDetails
2231 function populateSummary(data) {
2232 summaryPane.empty();
2233
2234 var svg = summaryPane.append('svg'),
2235 iid = iconGlyphUrl(data);
2236
2237 var title = summaryPane.append('h2'),
2238 table = summaryPane.append('table'),
2239 tbody = table.append('tbody');
2240
2241 appendGlyph(svg, 0, 0, 40, iid);
2242
2243 svg.append('use')
2244 .attr({
2245 class: 'birdBadge',
2246 transform: translate(8,12),
2247 'xlink:href': '#bird',
2248 width: 24,
2249 height: 24,
2250 fill: '#fff'
2251 });
2252
2253 title.text('ONOS Summary');
2254
2255 data.propOrder.forEach(function(p) {
2256 if (p === '-') {
2257 addSep(tbody);
2258 } else {
2259 addProp(tbody, p, data.props[p]);
2260 }
2261 });
2262 }
2263
Simon Hunt61d04042014-11-11 17:27:16 -08002264 function populateDetails(data) {
2265 detailPane.empty();
2266
Simon Hunta6a9fe72014-11-20 11:17:12 -08002267 var svg = detailPane.append('svg'),
2268 iid = iconGlyphUrl(data);
2269
Simon Hunta3dd9572014-11-20 15:22:41 -08002270 var title = detailPane.append('h2'),
2271 table = detailPane.append('table'),
2272 tbody = table.append('tbody');
Simon Hunt61d04042014-11-11 17:27:16 -08002273
Simon Hunta6a9fe72014-11-20 11:17:12 -08002274 appendGlyph(svg, 0, 0, 40, iid);
2275 title.text(data.id);
Simon Hunt61d04042014-11-11 17:27:16 -08002276
2277 data.propOrder.forEach(function(p) {
2278 if (p === '-') {
2279 addSep(tbody);
2280 } else {
2281 addProp(tbody, p, data.props[p]);
2282 }
2283 });
Simon Huntd72bc702014-11-13 18:38:04 -08002284
Thomas Vachuska4731f122014-11-20 04:56:19 -08002285 addSingleSelectActions(data);
Simon Hunt61d04042014-11-11 17:27:16 -08002286 }
2287
Thomas Vachuska4731f122014-11-20 04:56:19 -08002288 function addSingleSelectActions(data) {
Simon Huntd72bc702014-11-13 18:38:04 -08002289 detailPane.append('hr');
2290 // always want to allow 'show traffic'
Thomas Vachuska4731f122014-11-20 04:56:19 -08002291 addAction(detailPane, 'Show Related Traffic', showTrafficAction);
2292
2293 if (data.type === 'switch') {
2294 addAction(detailPane, 'Show Device Flows', showDeviceLinkFlowsAction);
2295 }
Simon Huntd72bc702014-11-13 18:38:04 -08002296 }
2297
2298 function addMultiSelectActions() {
2299 detailPane.append('hr');
2300 // always want to allow 'show traffic'
Thomas Vachuska4731f122014-11-20 04:56:19 -08002301 addAction(detailPane, 'Show Related Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08002302 // if exactly two hosts are selected, also want 'add host intent'
2303 if (nSel() === 2 && allSelectionsClass('host')) {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002304 addAction(detailPane, 'Create Host-to-Host Flow', addHostIntentAction);
2305 } else if (nSel() >= 2 && allSelectionsClass('host')) {
2306 addAction(detailPane, 'Create Multi-Source Flow', addMultiSourceIntentAction);
Simon Huntd72bc702014-11-13 18:38:04 -08002307 }
2308 }
2309
Simon Hunta5e89142014-11-14 07:00:33 -08002310 function addAction(panel, text, cb) {
2311 panel.append('div')
Simon Huntd72bc702014-11-13 18:38:04 -08002312 .classed('actionBtn', true)
2313 .text(text)
2314 .on('click', cb);
2315 }
2316
2317
Paul Greysonfcba0e82014-11-13 10:21:16 -08002318 function zoomPan(scale, translate) {
2319 zoomPanContainer.attr("transform", "translate(" + translate + ")scale(" + scale + ")");
2320 // keep the map lines constant width while zooming
Thomas Vachuska89543292014-11-19 11:28:33 -08002321 bgImg.style("stroke-width", 2.0 / scale + "px");
Paul Greysonfcba0e82014-11-13 10:21:16 -08002322 }
2323
2324 function resetZoomPan() {
2325 zoomPan(1, [0,0]);
2326 zoom.scale(1).translate([0,0]);
2327 }
2328
2329 function setupZoomPan() {
2330 function zoomed() {
Simon Huntdeab4322014-11-13 18:49:07 -08002331 if (!panZoom() ^ !d3.event.sourceEvent.metaKey) {
Paul Greysonfcba0e82014-11-13 10:21:16 -08002332 zoomPan(d3.event.scale, d3.event.translate);
2333 }
2334 }
2335
2336 zoom = d3.behavior.zoom()
2337 .translate([0, 0])
2338 .scale(1)
2339 .scaleExtent([1, 8])
2340 .on("zoom", zoomed);
2341
2342 svg.call(zoom);
2343 }
2344
Simon Hunt61d04042014-11-11 17:27:16 -08002345 // ==============================
2346 // Test harness code
Simon Hunt56d51852014-11-09 13:03:35 -08002347
2348 function prepareScenario(view, ctx, dbg) {
2349 var sc = scenario,
2350 urlSc = sc.evDir + ctx + sc.evScenario;
2351
2352 if (!ctx) {
2353 view.alert("No scenario specified (null ctx)");
2354 return;
2355 }
2356
2357 sc.view = view;
2358 sc.ctx = ctx;
2359 sc.debug = dbg;
2360 sc.evNumber = 0;
2361
2362 d3.json(urlSc, function(err, data) {
Simon Huntbb282f52014-11-10 11:08:19 -08002363 var p = data && data.params || {},
2364 desc = data && data.description || null,
Simon Huntfc274c92014-11-11 11:05:46 -08002365 intro = data && data.title;
Simon Huntbb282f52014-11-10 11:08:19 -08002366
Simon Hunt56d51852014-11-09 13:03:35 -08002367 if (err) {
2368 view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err);
2369 } else {
2370 sc.params = p;
Simon Huntbb282f52014-11-10 11:08:19 -08002371 if (desc) {
2372 intro += '\n\n ' + desc.join('\n ');
2373 }
2374 view.alert(intro);
Simon Hunt56d51852014-11-09 13:03:35 -08002375 }
2376 });
2377
2378 }
2379
Simon Hunt01095ff2014-11-13 16:37:29 -08002380 // ==============================
2381 // Toggle Buttons in masthead
Simon Hunt0c6d4192014-11-12 12:07:10 -08002382
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002383 // TODO: toggle button (and other widgets in the masthead) should be provided
2384 // by the framework; not generated by the view.
2385
Thomas Vachuska47635c62014-11-22 01:21:36 -08002386 //var showInstances;
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002387
2388 function addButtonBar(view) {
2389 var bb = d3.select('#mast')
2390 .append('span').classed('right', true).attr('id', 'bb');
2391
Simon Hunta5e89142014-11-14 07:00:33 -08002392 function mkTogBtn(text, cb) {
2393 return bb.append('span')
2394 .classed('btn', true)
2395 .text(text)
2396 .on('click', cb);
2397 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002398
Thomas Vachuska47635c62014-11-22 01:21:36 -08002399 //showInstances = mkTogBtn('Show Instances', toggleInst);
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002400 }
2401
Simon Huntdeab4322014-11-13 18:49:07 -08002402 function panZoom() {
Simon Hunte5b71752014-11-18 20:06:07 -08002403 return false;
Simon Hunta5e89142014-11-14 07:00:33 -08002404 }
2405
Simon Hunt7fa116d2014-11-17 14:16:55 -08002406 function loadGlyphs(svg) {
2407 var defs = svg.append('defs');
2408 gly.defBird(defs);
Simon Huntc72967b2014-11-20 09:21:42 -08002409 gly.defGlyphs(defs);
Simon Huntb82f6902014-11-22 11:53:15 -08002410 gly.defBadges(defs);
Simon Hunt7fa116d2014-11-17 14:16:55 -08002411 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002412
Simon Hunt395a70c2014-11-22 23:17:40 -08002413 function sendUpdateMeta(d, store) {
2414 var metaUi = {},
2415 ll;
2416
2417 if (store) {
2418 ll = geoMapProjection.invert([d.x, d.y]);
Simon Hunt62c47542014-11-22 22:16:32 -08002419 metaUi = {
2420 x: d.x,
2421 y: d.y,
2422 lng: ll[0],
2423 lat: ll[1]
2424 };
Simon Hunt395a70c2014-11-22 23:17:40 -08002425 }
Simon Hunt62c47542014-11-22 22:16:32 -08002426 d.metaUi = metaUi;
2427 sendMessage('updateMeta', {
2428 id: d.id,
2429 'class': d.class,
2430 'memento': metaUi
2431 });
2432 }
2433
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002434 // ==============================
Simon Hunt142d0032014-11-04 20:13:09 -08002435 // View life-cycle callbacks
Simon Hunt195cb382014-11-03 17:50:51 -08002436
Simon Huntf67722a2014-11-10 09:32:06 -08002437 function preload(view, ctx, flags) {
Simon Hunt142d0032014-11-04 20:13:09 -08002438 var w = view.width(),
2439 h = view.height(),
Simon Huntc7ee0662014-11-05 16:44:37 -08002440 fcfg = config.force,
2441 fpad = fcfg.pad,
2442 forceDim = [w - 2*fpad, h - 2*fpad];
Simon Hunt195cb382014-11-03 17:50:51 -08002443
Simon Hunt142d0032014-11-04 20:13:09 -08002444 // NOTE: view.$div is a D3 selection of the view's div
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002445 var viewBox = '0 0 ' + config.logicalSize + ' ' + config.logicalSize;
2446 svg = view.$div.append('svg').attr('viewBox', viewBox);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002447 setSize(svg, view);
2448
Simon Hunt7fa116d2014-11-17 14:16:55 -08002449 loadGlyphs(svg);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002450
Paul Greysonfcba0e82014-11-13 10:21:16 -08002451 zoomPanContainer = svg.append('g').attr('id', 'zoomPanContainer');
Paul Greysonfcba0e82014-11-13 10:21:16 -08002452 setupZoomPan();
2453
Simon Hunt1a9eff92014-11-07 11:06:34 -08002454 // add blue glow filter to svg layer
Paul Greysonfcba0e82014-11-13 10:21:16 -08002455 d3u.appendGlow(zoomPanContainer);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002456
Simon Huntc7ee0662014-11-05 16:44:37 -08002457 // group for the topology
Paul Greysonfcba0e82014-11-13 10:21:16 -08002458 topoG = zoomPanContainer.append('g')
Simon Huntd3b7d512014-11-12 15:48:41 -08002459 .attr('id', 'topo-G')
Simon Huntc7ee0662014-11-05 16:44:37 -08002460 .attr('transform', fcfg.translate());
2461
Simon Hunte2575b62014-11-18 15:25:53 -08002462 // subgroups for links, link labels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002463 linkG = topoG.append('g').attr('id', 'links');
Simon Hunte2575b62014-11-18 15:25:53 -08002464 linkLabelG = topoG.append('g').attr('id', 'linkLabels');
Simon Huntc7ee0662014-11-05 16:44:37 -08002465 nodeG = topoG.append('g').attr('id', 'nodes');
2466
Simon Hunte2575b62014-11-18 15:25:53 -08002467 // selection of links, linkLabels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002468 link = linkG.selectAll('.link');
Simon Hunte2575b62014-11-18 15:25:53 -08002469 linkLabel = linkLabelG.selectAll('.linkLabel');
Simon Huntc7ee0662014-11-05 16:44:37 -08002470 node = nodeG.selectAll('.node');
2471
Simon Hunt7cd48f32014-11-09 23:42:50 -08002472 function chrg(d) {
2473 return fcfg.charge[d.class] || -12000;
2474 }
Simon Hunt99c13842014-11-06 18:23:12 -08002475 function ldist(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002476 return fcfg.linkDistance[d.type] || 50;
Simon Hunt99c13842014-11-06 18:23:12 -08002477 }
2478 function lstrg(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002479 // 0.0 - 1.0
2480 return fcfg.linkStrength[d.type] || 1.0;
Simon Hunt99c13842014-11-06 18:23:12 -08002481 }
2482
Simon Hunt1a9eff92014-11-07 11:06:34 -08002483 function selectCb(d, self) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002484 selectObject(d, self);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002485 }
2486
2487 function atDragEnd(d, self) {
Simon Hunt56d51852014-11-09 13:03:35 -08002488 // once we've finished moving, pin the node in position
2489 d.fixed = true;
2490 d3.select(self).classed('fixed', true);
2491 if (config.useLiveData) {
Simon Hunt395a70c2014-11-22 23:17:40 -08002492 sendUpdateMeta(d, true);
Simon Hunta255a2c2014-11-13 22:29:35 -08002493 } else {
2494 console.log('Moving node ' + d.id + ' to [' + d.x + ',' + d.y + ']');
Simon Hunt1a9eff92014-11-07 11:06:34 -08002495 }
2496 }
2497
Simon Huntc7ee0662014-11-05 16:44:37 -08002498 // set up the force layout
2499 network.force = d3.layout.force()
2500 .size(forceDim)
2501 .nodes(network.nodes)
2502 .links(network.links)
Simon Hunt7cd48f32014-11-09 23:42:50 -08002503 .gravity(0.4)
2504 .friction(0.7)
2505 .charge(chrg)
Simon Hunt99c13842014-11-06 18:23:12 -08002506 .linkDistance(ldist)
2507 .linkStrength(lstrg)
Simon Huntc7ee0662014-11-05 16:44:37 -08002508 .on('tick', tick);
Simon Hunt195cb382014-11-03 17:50:51 -08002509
Simon Hunt01095ff2014-11-13 16:37:29 -08002510 network.drag = d3u.createDragBehavior(network.force,
Simon Huntdeab4322014-11-13 18:49:07 -08002511 selectCb, atDragEnd, panZoom);
Simon Hunt0c6d4192014-11-12 12:07:10 -08002512
2513 // create mask layer for when we lose connection to server.
Simon Hunta5e89142014-11-14 07:00:33 -08002514 // TODO: this should be part of the framework
Simon Hunt0c6d4192014-11-12 12:07:10 -08002515 mask = view.$div.append('div').attr('id','topo-mask');
2516 para(mask, 'Oops!');
2517 para(mask, 'Web-socket connection to server closed...');
2518 para(mask, 'Try refreshing the page.');
Simon Hunt12ce12e2014-11-15 21:13:19 -08002519
2520 mask.append('svg')
2521 .attr({
2522 id: 'mask-bird',
2523 width: w,
2524 height: h
2525 })
2526 .append('g')
2527 .attr('transform', birdTranslate(w, h))
2528 .style('opacity', 0.3)
2529 .append('use')
2530 .attr({
2531 'xlink:href': '#bird',
2532 width: config.birdDim,
2533 height: config.birdDim,
2534 fill: '#111'
Thomas Vachuska89543292014-11-19 11:28:33 -08002535 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08002536 }
Simon Hunt195cb382014-11-03 17:50:51 -08002537
Simon Hunt01095ff2014-11-13 16:37:29 -08002538 function para(sel, text) {
2539 sel.append('p').text(text);
2540 }
2541
2542
Simon Hunt56d51852014-11-09 13:03:35 -08002543 function load(view, ctx, flags) {
Simon Huntf67722a2014-11-10 09:32:06 -08002544 // resize, in case the window was resized while we were not loaded
2545 resize(view, ctx, flags);
2546
Simon Hunt99c13842014-11-06 18:23:12 -08002547 // cache the view token, so network topo functions can access it
2548 network.view = view;
Simon Hunt56d51852014-11-09 13:03:35 -08002549 config.useLiveData = !flags.local;
2550
2551 if (!config.useLiveData) {
2552 prepareScenario(view, ctx, flags.debug);
2553 }
Simon Hunt99c13842014-11-06 18:23:12 -08002554
2555 // set our radio buttons and key bindings
Simon Hunt9462e8c2014-11-14 17:28:09 -08002556 layerBtnSet = view.setRadio(layerButtons);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002557 view.setKeys(keyDispatch);
Simon Hunt195cb382014-11-03 17:50:51 -08002558
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002559 // patch in our "button bar" for now
2560 // TODO: implement a more official frameworky way of doing this..
2561 addButtonBar(view);
2562
Simon Huntd3b7d512014-11-12 15:48:41 -08002563 // Load map data asynchronously; complete startup after that..
2564 loadGeoJsonData();
Simon Hunta255a2c2014-11-13 22:29:35 -08002565 }
2566
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002567 function startAntTimer() {
Thomas Vachuska4731f122014-11-20 04:56:19 -08002568 if (!antTimer) {
2569 var pulses = [5, 3, 1.2, 3],
2570 pulse = 0;
2571 antTimer = setInterval(function () {
2572 pulse = pulse + 1;
2573 pulse = pulse === pulses.length ? 0 : pulse;
2574 d3.selectAll('.animated').style('stroke-width', pulses[pulse]);
2575 }, 200);
2576 }
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002577 }
2578
2579 function stopAntTimer() {
Simon Hunta255a2c2014-11-13 22:29:35 -08002580 if (antTimer) {
2581 clearInterval(antTimer);
2582 antTimer = null;
2583 }
Simon Huntd3b7d512014-11-12 15:48:41 -08002584 }
2585
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002586 function unload(view, ctx, flags) {
2587 stopAntTimer();
2588 }
2589
Simon Huntd3b7d512014-11-12 15:48:41 -08002590 // TODO: move these to config/state portion of script
Simon Hunta6a9fe72014-11-20 11:17:12 -08002591 var geoJsonUrl = 'json/map/continental_us.json',
Simon Huntd3b7d512014-11-12 15:48:41 -08002592 geoJson;
2593
2594 function loadGeoJsonData() {
2595 d3.json(geoJsonUrl, function (err, data) {
2596 if (err) {
2597 // fall back to USA map background
2598 loadStaticMap();
2599 } else {
2600 geoJson = data;
2601 loadGeoMap();
2602 }
2603
2604 // finally, connect to the server...
2605 if (config.useLiveData) {
2606 webSock.connect();
2607 }
2608 });
2609 }
2610
2611 function showBg() {
Simon Hunt434cf142014-11-24 11:10:28 -08002612 return visVal(config.options.showBackground);
Simon Huntd3b7d512014-11-12 15:48:41 -08002613 }
2614
2615 function loadStaticMap() {
2616 fnTrace('loadStaticMap', config.backgroundUrl);
2617 var w = network.view.width(),
2618 h = network.view.height();
2619
2620 // load the background image
2621 bgImg = svg.insert('svg:image', '#topo-G')
2622 .attr({
2623 id: 'topo-bg',
2624 width: w,
2625 height: h,
2626 'xlink:href': config.backgroundUrl
2627 })
2628 .style({
2629 visibility: showBg()
2630 });
2631 }
2632
2633 function loadGeoMap() {
2634 fnTrace('loadGeoMap', geoJsonUrl);
Simon Huntd3b7d512014-11-12 15:48:41 -08002635
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002636 // extracts the topojson data into geocoordinate-based geometry
2637 var topoData = topojson.feature(geoJson, geoJson.objects.states);
Simon Huntd3b7d512014-11-12 15:48:41 -08002638
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002639 // see: http://bl.ocks.org/mbostock/4707858
2640 geoMapProjection = d3.geo.mercator();
2641 var path = d3.geo.path().projection(geoMapProjection);
Simon Huntd3b7d512014-11-12 15:48:41 -08002642
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002643 geoMapProjection
2644 .scale(1)
2645 .translate([0, 0]);
Simon Huntd3b7d512014-11-12 15:48:41 -08002646
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002647 // [[x1,y1],[x2,y2]]
2648 var b = path.bounds(topoData);
Paul Greysonfcba0e82014-11-13 10:21:16 -08002649 // size map to 95% of minimum dimension to fill space
2650 var s = .95 / Math.min((b[1][0] - b[0][0]) / config.logicalSize, (b[1][1] - b[0][1]) / config.logicalSize);
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002651 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 -08002652
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002653 geoMapProjection
2654 .scale(s)
2655 .translate(t);
2656
Paul Greysonfcba0e82014-11-13 10:21:16 -08002657 bgImg = zoomPanContainer.insert("g", '#topo-G');
Thomas Vachuska89543292014-11-19 11:28:33 -08002658 bgImg.attr('id', 'map').selectAll('path')
2659 .data(topoData.features)
2660 .enter()
2661 .append('path')
2662 .attr('d', path);
Simon Hunt195cb382014-11-03 17:50:51 -08002663 }
2664
Simon Huntf67722a2014-11-10 09:32:06 -08002665 function resize(view, ctx, flags) {
Simon Hunt12ce12e2014-11-15 21:13:19 -08002666 var w = view.width(),
2667 h = view.height();
2668
Simon Hunt934c3ce2014-11-05 11:45:07 -08002669 setSize(svg, view);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002670
2671 d3.select('#mask-bird').attr({ width: w, height: h})
2672 .select('g').attr('transform', birdTranslate(w, h));
Simon Hunt142d0032014-11-04 20:13:09 -08002673 }
2674
Simon Hunt8f40cce2014-11-23 15:57:30 -08002675 function theme(view, ctx, flags) {
2676 updateInstances();
Simon Huntb0ecfa52014-11-23 21:05:12 -08002677 updateDeviceColors();
Simon Hunt8f40cce2014-11-23 15:57:30 -08002678 }
2679
Simon Hunt12ce12e2014-11-15 21:13:19 -08002680 function birdTranslate(w, h) {
2681 var bdim = config.birdDim;
2682 return 'translate('+((w-bdim)*.4)+','+((h-bdim)*.1)+')';
2683 }
Simon Hunt142d0032014-11-04 20:13:09 -08002684
2685 // ==============================
2686 // View registration
Simon Hunt195cb382014-11-03 17:50:51 -08002687
Simon Hunt25248912014-11-04 11:25:48 -08002688 onos.ui.addView('topo', {
Simon Hunt142d0032014-11-04 20:13:09 -08002689 preload: preload,
2690 load: load,
Simon Hunta255a2c2014-11-13 22:29:35 -08002691 unload: unload,
Simon Hunt8f40cce2014-11-23 15:57:30 -08002692 resize: resize,
2693 theme: theme
Simon Hunt195cb382014-11-03 17:50:51 -08002694 });
2695
Thomas Vachuska47635c62014-11-22 01:21:36 -08002696 summaryPane = onos.ui.addFloatingPanel('topo-summary');
Simon Hunt61d04042014-11-11 17:27:16 -08002697 detailPane = onos.ui.addFloatingPanel('topo-detail');
Simon Hunta5e89142014-11-14 07:00:33 -08002698 oiBox = onos.ui.addFloatingPanel('topo-oibox', 'TL');
Simon Huntb82f6902014-11-22 11:53:15 -08002699 oiBox.width(20);
Simon Hunt61d04042014-11-11 17:27:16 -08002700
Simon Hunt195cb382014-11-03 17:50:51 -08002701}(ONOS));