blob: eb05dbfc266a2401f2873151670cd42c0d0b5650 [file] [log] [blame]
Simon Hunt195cb382014-11-03 17:50:51 -08001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
Simon Hunt142d0032014-11-04 20:13:09 -080018 ONOS network topology viewer - version 1.1
Simon Hunt195cb382014-11-03 17:50:51 -080019
20 @author Simon Hunt
Simon Hunta6a9fe72014-11-20 11:17:12 -080021 @author Thomas Vachuska
Simon Hunt195cb382014-11-03 17:50:51 -080022 */
23
24(function (onos) {
25 'use strict';
26
Simon Hunt1a9eff92014-11-07 11:06:34 -080027 // shorter names for library APIs
Simon Huntbb282f52014-11-10 11:08:19 -080028 var d3u = onos.lib.d3util,
Simon Hunta6a9fe72014-11-20 11:17:12 -080029 gly = onos.lib.glyphs;
Simon Hunt1a9eff92014-11-07 11:06:34 -080030
Simon Hunt195cb382014-11-03 17:50:51 -080031 // configuration data
32 var config = {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -080033 useLiveData: true,
Simon Huntfc274c92014-11-11 11:05:46 -080034 fnTrace: true,
Simon Hunt195cb382014-11-03 17:50:51 -080035 debugOn: false,
36 debug: {
Simon Hunt99c13842014-11-06 18:23:12 -080037 showNodeXY: true,
38 showKeyHandler: false
Simon Hunt195cb382014-11-03 17:50:51 -080039 },
Simon Hunt12ce12e2014-11-15 21:13:19 -080040 birdDim: 400,
Simon Hunt195cb382014-11-03 17:50:51 -080041 options: {
42 layering: true,
43 collisionPrevention: true,
Simon Hunt142d0032014-11-04 20:13:09 -080044 showBackground: true
Simon Hunt195cb382014-11-03 17:50:51 -080045 },
46 backgroundUrl: 'img/us-map.png',
Thomas Vachuska7d638d32014-11-07 10:24:43 -080047 webSockUrl: 'ws/topology',
Simon Hunt195cb382014-11-03 17:50:51 -080048 data: {
49 live: {
50 jsonUrl: 'rs/topology/graph',
51 detailPrefix: 'rs/topology/graph/',
52 detailSuffix: ''
53 },
54 fake: {
55 jsonUrl: 'json/network2.json',
56 detailPrefix: 'json/',
57 detailSuffix: '.json'
58 }
59 },
Simon Hunt99c13842014-11-06 18:23:12 -080060 labels: {
61 imgPad: 16,
62 padLR: 4,
63 padTB: 3,
64 marginLR: 3,
65 marginTB: 2,
66 port: {
67 gap: 3,
68 width: 18,
69 height: 14
70 }
71 },
Simon Hunt1a9eff92014-11-07 11:06:34 -080072 topo: {
Thomas Vachuska89543292014-11-19 11:28:33 -080073 linkBaseColor: '#666',
Simon Hunt1a9eff92014-11-07 11:06:34 -080074 linkInColor: '#66f',
Thomas Vachuska9edca302014-11-22 17:06:42 -080075 linkInWidth: 12,
Thomas Vachuska89543292014-11-19 11:28:33 -080076 linkOutColor: '#f00',
Thomas Vachuska9edca302014-11-22 17:06:42 -080077 linkOutWidth: 10
Simon Hunt1a9eff92014-11-07 11:06:34 -080078 },
Paul Greyson29cd58f2014-11-18 13:14:57 -080079 icons: {
Simon Huntc72967b2014-11-20 09:21:42 -080080 device: {
Simon Hunt395a70c2014-11-22 23:17:40 -080081 dim: 36,
82 rx: 4,
83 xoff: -20,
84 yoff: -18
85 },
86 host: {
87 defaultRadius: 9,
88 radius: {
89 endstation: 14,
90 bgpSpeaker: 14,
91 router: 14
92 }
Simon Huntc72967b2014-11-20 09:21:42 -080093 }
Thomas Vachuska89543292014-11-19 11:28:33 -080094 },
Simon Hunt195cb382014-11-03 17:50:51 -080095 force: {
Simon Hunt7cd48f32014-11-09 23:42:50 -080096 note_for_links: 'link.type is used to differentiate',
Simon Huntc7ee0662014-11-05 16:44:37 -080097 linkDistance: {
Simon Hunt7cd48f32014-11-09 23:42:50 -080098 direct: 100,
99 optical: 120,
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800100 hostLink: 3
Simon Huntc7ee0662014-11-05 16:44:37 -0800101 },
102 linkStrength: {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800103 direct: 1.0,
104 optical: 1.0,
105 hostLink: 1.0
Simon Huntc7ee0662014-11-05 16:44:37 -0800106 },
Simon Hunt7cd48f32014-11-09 23:42:50 -0800107 note_for_nodes: 'node.class is used to differentiate',
Simon Huntc7ee0662014-11-05 16:44:37 -0800108 charge: {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800109 device: -8000,
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800110 host: -5000
Simon Huntc7ee0662014-11-05 16:44:37 -0800111 },
112 pad: 20,
Simon Hunt195cb382014-11-03 17:50:51 -0800113 translate: function() {
114 return 'translate(' +
Simon Huntc7ee0662014-11-05 16:44:37 -0800115 config.force.pad + ',' +
116 config.force.pad + ')';
Simon Hunt195cb382014-11-03 17:50:51 -0800117 }
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800118 },
119 // see below in creation of viewBox on main svg
120 logicalSize: 1000
Simon Hunt195cb382014-11-03 17:50:51 -0800121 };
122
Simon Hunt142d0032014-11-04 20:13:09 -0800123 // radio buttons
Simon Hunt9462e8c2014-11-14 17:28:09 -0800124 var layerButtons = [
125 { text: 'All Layers', id: 'all', cb: showAllLayers },
126 { text: 'Packet Only', id: 'pkt', cb: showPacketLayer },
127 { text: 'Optical Only', id: 'opt', cb: showOpticalLayer }
128 ],
129 layerBtnSet,
130 layerBtnDispatch = {
131 all: showAllLayers,
132 pkt: showPacketLayer,
133 opt: showOpticalLayer
134 };
Simon Hunt934c3ce2014-11-05 11:45:07 -0800135
136 // key bindings
137 var keyDispatch = {
Simon Hunt988c6fc2014-11-20 17:43:03 -0800138 // TODO: remove these "development only" bindings
Simon Hunt8f40cce2014-11-23 15:57:30 -0800139 0: testMe,
140 equals: injectStartupEvents,
141 dash: injectTestEvent,
Simon Hunt99c13842014-11-06 18:23:12 -0800142
Thomas Vachuska47635c62014-11-22 01:21:36 -0800143 O: [toggleSummary, 'Toggle ONOS summary pane'],
144 I: [toggleInstances, 'Toggle ONOS instances pane'],
Simon Hunt988c6fc2014-11-20 17:43:03 -0800145 B: [toggleBg, 'Toggle background image'],
Simon Hunt434cf142014-11-24 11:10:28 -0800146 H: [toggleHosts, 'Toggle host visibility'],
Simon Hunt988c6fc2014-11-20 17:43:03 -0800147 L: [cycleLabels, 'Cycle Device labels'],
Simon Hunt934c3ce2014-11-05 11:45:07 -0800148 P: togglePorts,
Simon Hunt87514342014-11-24 16:41:27 -0800149 U: [unpin, 'Unpin node (hover mouse over)'],
150 R: [resetZoomPan, 'Reset zoom / pan'],
Thomas Vachuska9edca302014-11-22 17:06:42 -0800151 V: [showTrafficAction, 'Show related traffic'],
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800152 A: [showAllTrafficAction, 'Show all traffic'],
153 F: [showDeviceLinkFlowsAction, 'Show device link flows'],
Simon Hunt9462e8c2014-11-14 17:28:09 -0800154 esc: handleEscape
Simon Hunt934c3ce2014-11-05 11:45:07 -0800155 };
Simon Hunt142d0032014-11-04 20:13:09 -0800156
Simon Hunt87514342014-11-24 16:41:27 -0800157 // mouse gestures
158 var gestures = [
159 ['click', 'Select the item and show details'],
160 ['shift-click', 'Toggle selection state'],
161 ['drag', 'Reposition (and pin) device / host'],
162 ['cmd-scroll', 'Zoom in / out'],
163 ['cmd-drag', 'Pan']
164 ];
165
Simon Hunt195cb382014-11-03 17:50:51 -0800166 // state variables
Simon Hunt99c13842014-11-06 18:23:12 -0800167 var network = {
Simon Hunt50128c02014-11-08 13:36:15 -0800168 view: null, // view token reference
Simon Hunt99c13842014-11-06 18:23:12 -0800169 nodes: [],
170 links: [],
Simon Hunt269670f2014-11-17 16:17:43 -0800171 lookup: {},
172 revLinkToKey: {}
Simon Hunt99c13842014-11-06 18:23:12 -0800173 },
Simon Hunt56d51852014-11-09 13:03:35 -0800174 scenario = {
175 evDir: 'json/ev/',
176 evScenario: '/scenario.json',
177 evPrefix: '/ev_',
178 evOnos: '_onos.json',
179 evUi: '_ui.json',
180 ctx: null,
181 params: {},
182 evNumber: 0,
Simon Hunt434cf142014-11-24 11:10:28 -0800183 view: null
Simon Hunt56d51852014-11-09 13:03:35 -0800184 },
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800185 webSock,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800186 sid = 0,
Thomas Vachuska60d72bf2014-11-21 13:02:00 -0800187 deviceLabelCount = 3,
Simon Hunt209155e2014-11-21 12:16:09 -0800188 hostLabelCount = 2,
Simon Hunt56d51852014-11-09 13:03:35 -0800189 deviceLabelIndex = 0,
190 hostLabelIndex = 0,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800191 selections = {},
Simon Hunta5e89142014-11-14 07:00:33 -0800192 selectOrder = [],
Simon Hunt6ac93f32014-11-13 12:17:27 -0800193 hovered = null,
Thomas Vachuska47635c62014-11-22 01:21:36 -0800194 summaryPane,
Simon Hunta5e89142014-11-14 07:00:33 -0800195 detailPane,
Simon Hunta255a2c2014-11-13 22:29:35 -0800196 antTimer = null,
Simon Hunta5e89142014-11-14 07:00:33 -0800197 onosInstances = {},
198 onosOrder = [],
199 oiBox,
Simon Hunt9462e8c2014-11-14 17:28:09 -0800200 oiShowMaster = false,
Simon Hunt8f40cce2014-11-23 15:57:30 -0800201 portLabelsOn = false,
Simon Huntb0ecfa52014-11-23 21:05:12 -0800202 cat7 = d3u.cat7(),
Simon Hunt434cf142014-11-24 11:10:28 -0800203 colorAffinity = false,
204 showHosts = false;
Simon Hunt195cb382014-11-03 17:50:51 -0800205
Simon Hunt434cf142014-11-24 11:10:28 -0800206 // constants
Thomas Vachuska9edca302014-11-22 17:06:42 -0800207 var hoverModeAll = 1,
208 hoverModeFlows = 2,
209 hoverModeIntents = 3,
210 hoverMode = hoverModeFlows;
211
Simon Hunt934c3ce2014-11-05 11:45:07 -0800212 // D3 selections
213 var svg,
Paul Greysonfcba0e82014-11-13 10:21:16 -0800214 zoomPanContainer,
Simon Hunt934c3ce2014-11-05 11:45:07 -0800215 bgImg,
Simon Huntc7ee0662014-11-05 16:44:37 -0800216 topoG,
217 nodeG,
218 linkG,
Simon Hunte2575b62014-11-18 15:25:53 -0800219 linkLabelG,
Simon Huntc7ee0662014-11-05 16:44:37 -0800220 node,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800221 link,
Simon Hunte2575b62014-11-18 15:25:53 -0800222 linkLabel,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800223 mask;
Simon Hunt195cb382014-11-03 17:50:51 -0800224
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800225 // the projection for the map background
226 var geoMapProjection;
227
Paul Greysonfcba0e82014-11-13 10:21:16 -0800228 // the zoom function
229 var zoom;
230
Simon Hunt142d0032014-11-04 20:13:09 -0800231 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800232 // For Debugging / Development
Simon Hunt195cb382014-11-03 17:50:51 -0800233
Simon Hunt99c13842014-11-06 18:23:12 -0800234 function note(label, msg) {
235 console.log('NOTE: ' + label + ': ' + msg);
Simon Hunt195cb382014-11-03 17:50:51 -0800236 }
237
Simon Hunt99c13842014-11-06 18:23:12 -0800238 function debug(what) {
239 return config.debugOn && config.debug[what];
Simon Hunt934c3ce2014-11-05 11:45:07 -0800240 }
241
Simon Huntfc274c92014-11-11 11:05:46 -0800242 function fnTrace(msg, id) {
243 if (config.fnTrace) {
244 console.log('FN: ' + msg + ' [' + id + ']');
245 }
246 }
Simon Hunt99c13842014-11-06 18:23:12 -0800247
Simon Hunta5e89142014-11-14 07:00:33 -0800248 function evTrace(data) {
249 fnTrace(data.event, data.payload.id);
250 }
251
Simon Hunt934c3ce2014-11-05 11:45:07 -0800252 // ==============================
253 // Key Callbacks
254
Simon Hunt99c13842014-11-06 18:23:12 -0800255 function testMe(view) {
Simon Hunt8f40cce2014-11-23 15:57:30 -0800256 //view.alert('Theme is ' + view.getTheme());
Simon Hunta3dd9572014-11-20 15:22:41 -0800257 //view.flash('This is some text');
Simon Hunt8f40cce2014-11-23 15:57:30 -0800258 cat7.testCard(svg);
Simon Hunt99c13842014-11-06 18:23:12 -0800259 }
260
Simon Hunt56d51852014-11-09 13:03:35 -0800261 function injectTestEvent(view) {
Simon Hunt434cf142014-11-24 11:10:28 -0800262 if (config.useLiveData) { return; }
263
Simon Hunt56d51852014-11-09 13:03:35 -0800264 var sc = scenario,
265 evn = ++sc.evNumber,
266 pfx = sc.evDir + sc.ctx + sc.evPrefix + evn,
267 onosUrl = pfx + sc.evOnos,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800268 uiUrl = pfx + sc.evUi,
269 stack = [
270 { url: onosUrl, cb: handleServerEvent },
271 { url: uiUrl, cb: handleUiEvent }
272 ];
273 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800274 }
275
Simon Hunt7cd48f32014-11-09 23:42:50 -0800276 function recurseFetchEvent(stack, evn) {
277 var v = scenario.view,
278 frame;
279 if (stack.length === 0) {
Simon Huntfc274c92014-11-11 11:05:46 -0800280 v.alert('Oops!\n\nNo event #' + evn + ' found.');
Simon Hunt7cd48f32014-11-09 23:42:50 -0800281 return;
282 }
283 frame = stack.shift();
284
285 d3.json(frame.url, function (err, data) {
Simon Hunt99c13842014-11-06 18:23:12 -0800286 if (err) {
Simon Hunt56d51852014-11-09 13:03:35 -0800287 if (err.status === 404) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800288 // if we didn't find the data, try the next stack frame
289 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800290 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800291 v.alert('non-404 error:\n\n' + frame.url + '\n\n' + err);
Simon Hunt56d51852014-11-09 13:03:35 -0800292 }
Simon Hunt99c13842014-11-06 18:23:12 -0800293 } else {
Simon Hunt1712ed82014-11-17 12:56:00 -0800294 wsTrace('test', JSON.stringify(data));
Simon Hunt7cd48f32014-11-09 23:42:50 -0800295 frame.cb(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800296 }
297 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800298
Simon Hunt56d51852014-11-09 13:03:35 -0800299 }
Simon Hunt50128c02014-11-08 13:36:15 -0800300
Simon Hunt56d51852014-11-09 13:03:35 -0800301 function handleUiEvent(data) {
Simon Huntbb282f52014-11-10 11:08:19 -0800302 scenario.view.alert('UI Tx: ' + data.event + '\n\n' +
303 JSON.stringify(data));
Simon Hunt56d51852014-11-09 13:03:35 -0800304 }
305
306 function injectStartupEvents(view) {
307 var last = scenario.params.lastAuto || 0;
Simon Hunt434cf142014-11-24 11:10:28 -0800308 if (config.useLiveData) { return; }
Simon Hunt56d51852014-11-09 13:03:35 -0800309
310 while (scenario.evNumber < last) {
Simon Hunt1a9eff92014-11-07 11:06:34 -0800311 injectTestEvent(view);
312 }
313 }
314
Simon Hunt934c3ce2014-11-05 11:45:07 -0800315 function toggleBg() {
316 var vis = bgImg.style('visibility');
Simon Hunt434cf142014-11-24 11:10:28 -0800317 bgImg.style('visibility', visVal(vis === 'hidden'));
318 }
319
320 function toggleHosts() {
321 showHosts = !showHosts;
322 updateHostVisibility();
323 network.view.flash('Hosts ' + visVal(showHosts));
Simon Hunt934c3ce2014-11-05 11:45:07 -0800324 }
325
Simon Hunt99c13842014-11-06 18:23:12 -0800326 function cycleLabels() {
Thomas Vachuska60d72bf2014-11-21 13:02:00 -0800327 deviceLabelIndex = (deviceLabelIndex === 2)
Simon Huntbb282f52014-11-10 11:08:19 -0800328 ? 0 : deviceLabelIndex + 1;
Simon Hunt5f36d342014-11-08 21:33:14 -0800329
Simon Hunt99c13842014-11-06 18:23:12 -0800330 network.nodes.forEach(function (d) {
Simon Huntbb282f52014-11-10 11:08:19 -0800331 if (d.class === 'device') {
332 updateDeviceLabel(d);
333 }
Simon Hunt99c13842014-11-06 18:23:12 -0800334 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800335 }
336
337 function togglePorts(view) {
Simon Hunt434cf142014-11-24 11:10:28 -0800338 //view.alert('togglePorts() callback')
Simon Hunt934c3ce2014-11-05 11:45:07 -0800339 }
340
Simon Hunt6ac93f32014-11-13 12:17:27 -0800341 function unpin() {
342 if (hovered) {
Simon Hunt395a70c2014-11-22 23:17:40 -0800343 sendUpdateMeta(hovered);
Simon Hunt6ac93f32014-11-13 12:17:27 -0800344 hovered.fixed = false;
345 hovered.el.classed('fixed', false);
346 network.force.resume();
347 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800348 }
349
Simon Hunt9462e8c2014-11-14 17:28:09 -0800350 function handleEscape(view) {
351 if (oiShowMaster) {
352 cancelAffinity();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800353 } else if (detailPane.isVisible()) {
Simon Hunt9462e8c2014-11-14 17:28:09 -0800354 deselectAll();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800355 } else if (oiBox.isVisible()) {
Simon Huntb0ecfa52014-11-23 21:05:12 -0800356 hideInstances();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800357 } else if (summaryPane.isVisible()) {
358 cancelSummary();
Simon Hunt9462e8c2014-11-14 17:28:09 -0800359 }
360 }
361
Simon Hunt934c3ce2014-11-05 11:45:07 -0800362 // ==============================
363 // Radio Button Callbacks
364
Simon Hunta5e89142014-11-14 07:00:33 -0800365 var layerLookup = {
366 host: {
Simon Hunt209155e2014-11-21 12:16:09 -0800367 endstation: 'pkt', // default, if host event does not define type
Thomas Vachuska89543292014-11-19 11:28:33 -0800368 router: 'pkt',
Simon Hunta5e89142014-11-14 07:00:33 -0800369 bgpSpeaker: 'pkt'
370 },
371 device: {
372 switch: 'pkt',
373 roadm: 'opt'
374 },
375 link: {
376 hostLink: 'pkt',
377 direct: 'pkt',
Simon Hunt8257f4c2014-11-16 19:34:54 -0800378 indirect: '',
379 tunnel: '',
Simon Hunta5e89142014-11-14 07:00:33 -0800380 optical: 'opt'
381 }
382 };
383
384 function inLayer(d, layer) {
Simon Hunt8257f4c2014-11-16 19:34:54 -0800385 var type = d.class === 'link' ? d.type() : d.type,
386 look = layerLookup[d.class],
387 lyr = look && look[type];
Simon Hunta5e89142014-11-14 07:00:33 -0800388 return lyr === layer;
389 }
390
391 function unsuppressLayer(which) {
392 node.each(function (d) {
393 var node = d.el;
394 if (inLayer(d, which)) {
395 node.classed('suppressed', false);
396 }
397 });
398
399 link.each(function (d) {
400 var link = d.el;
401 if (inLayer(d, which)) {
402 link.classed('suppressed', false);
403 }
404 });
405 }
406
Simon Hunt9462e8c2014-11-14 17:28:09 -0800407 function suppressLayers(b) {
408 node.classed('suppressed', b);
409 link.classed('suppressed', b);
Simon Hunt142d0032014-11-04 20:13:09 -0800410// d3.selectAll('svg .port').classed('inactive', false);
411// d3.selectAll('svg .portText').classed('inactive', false);
Simon Hunt195cb382014-11-03 17:50:51 -0800412 }
413
Simon Hunt9462e8c2014-11-14 17:28:09 -0800414 function showAllLayers() {
415 suppressLayers(false);
416 }
417
Simon Hunt195cb382014-11-03 17:50:51 -0800418 function showPacketLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800419 node.classed('suppressed', true);
420 link.classed('suppressed', true);
421 unsuppressLayer('pkt');
Simon Hunt195cb382014-11-03 17:50:51 -0800422 }
423
424 function showOpticalLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800425 node.classed('suppressed', true);
426 link.classed('suppressed', true);
427 unsuppressLayer('opt');
Simon Hunt195cb382014-11-03 17:50:51 -0800428 }
429
Simon Hunt9462e8c2014-11-14 17:28:09 -0800430 function restoreLayerState() {
431 layerBtnDispatch[layerBtnSet.selected()]();
432 }
433
Simon Hunt142d0032014-11-04 20:13:09 -0800434 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800435 // Private functions
436
Simon Hunt99c13842014-11-06 18:23:12 -0800437 function safeId(s) {
438 return s.replace(/[^a-z0-9]/gi, '-');
439 }
440
Simon Huntc7ee0662014-11-05 16:44:37 -0800441 // set the size of the given element to that of the view (reduced if padded)
442 function setSize(el, view, pad) {
443 var padding = pad ? pad * 2 : 0;
Simon Hunt934c3ce2014-11-05 11:45:07 -0800444 el.attr({
Simon Huntc7ee0662014-11-05 16:44:37 -0800445 width: view.width() - padding,
446 height: view.height() - padding
Simon Hunt934c3ce2014-11-05 11:45:07 -0800447 });
448 }
449
Simon Hunt8257f4c2014-11-16 19:34:54 -0800450 function makeNodeKey(d, what) {
451 var port = what + 'Port';
452 return d[what] + '/' + d[port];
453 }
454
455 function makeLinkKey(d, flipped) {
456 var one = flipped ? makeNodeKey(d, 'dst') : makeNodeKey(d, 'src'),
457 two = flipped ? makeNodeKey(d, 'src') : makeNodeKey(d, 'dst');
458 return one + '-' + two;
459 }
460
Simon Hunt269670f2014-11-17 16:17:43 -0800461 function findLinkById(id) {
462 // check to see if this is a reverse lookup, else default to given id
463 var key = network.revLinkToKey[id] || id;
464 return key && network.lookup[key];
465 }
466
Simon Hunt8257f4c2014-11-16 19:34:54 -0800467 function findLink(linkData, op) {
468 var key = makeLinkKey(linkData),
469 keyrev = makeLinkKey(linkData, 1),
470 link = network.lookup[key],
471 linkRev = network.lookup[keyrev],
472 result = {},
473 ldata = link || linkRev,
474 rawLink;
475
476 if (op === 'add') {
477 if (link) {
478 // trying to add a link that we already know about
479 result.ldata = link;
480 result.badLogic = 'addLink: link already added';
481
482 } else if (linkRev) {
483 // we found the reverse of the link to be added
484 result.ldata = linkRev;
485 if (linkRev.fromTarget) {
486 result.badLogic = 'addLink: link already added';
487 }
488 }
489 } else if (op === 'update') {
490 if (!ldata) {
491 result.badLogic = 'updateLink: link not found';
492 } else {
493 rawLink = link ? ldata.fromSource : ldata.fromTarget;
494 result.updateWith = function (data) {
495 $.extend(rawLink, data);
496 restyleLinkElement(ldata);
497 }
498 }
499 } else if (op === 'remove') {
500 if (!ldata) {
501 result.badLogic = 'removeLink: link not found';
502 } else {
503 rawLink = link ? ldata.fromSource : ldata.fromTarget;
504
505 if (!rawLink) {
506 result.badLogic = 'removeLink: link not found';
507
508 } else {
509 result.removeRawLink = function () {
510 if (link) {
511 // remove fromSource
512 ldata.fromSource = null;
513 if (ldata.fromTarget) {
514 // promote target into source position
515 ldata.fromSource = ldata.fromTarget;
516 ldata.fromTarget = null;
517 ldata.key = keyrev;
518 delete network.lookup[key];
519 network.lookup[keyrev] = ldata;
Simon Hunt269670f2014-11-17 16:17:43 -0800520 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800521 }
522 } else {
523 // remove fromTarget
524 ldata.fromTarget = null;
Simon Hunt269670f2014-11-17 16:17:43 -0800525 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800526 }
527 if (ldata.fromSource) {
528 restyleLinkElement(ldata);
529 } else {
530 removeLinkElement(ldata);
531 }
532 }
533 }
534 }
535 }
536 return result;
537 }
538
539 function addLinkUpdate(ldata, link) {
540 // add link event, but we already have the reverse link installed
541 ldata.fromTarget = link;
Simon Hunt269670f2014-11-17 16:17:43 -0800542 network.revLinkToKey[link.id] = ldata.key;
Simon Hunt8257f4c2014-11-16 19:34:54 -0800543 restyleLinkElement(ldata);
544 }
545
546 var allLinkTypes = 'direct indirect optical tunnel',
547 defaultLinkType = 'direct';
548
549 function restyleLinkElement(ldata) {
550 // this fn's job is to look at raw links and decide what svg classes
551 // need to be applied to the line element in the DOM
552 var el = ldata.el,
553 type = ldata.type(),
554 lw = ldata.linkWidth(),
555 online = ldata.online();
556
557 el.classed('link', true);
558 el.classed('inactive', !online);
559 el.classed(allLinkTypes, false);
560 if (type) {
561 el.classed(type, true);
562 }
563 el.transition()
564 .duration(1000)
Thomas Vachuska89543292014-11-19 11:28:33 -0800565 .attr('stroke-width', linkScale(lw))
566 .attr('stroke', config.topo.linkBaseColor);
Simon Hunt8257f4c2014-11-16 19:34:54 -0800567 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800568
Simon Hunt99c13842014-11-06 18:23:12 -0800569 // ==============================
570 // Event handlers for server-pushed events
571
Simon Huntbb282f52014-11-10 11:08:19 -0800572 function logicError(msg) {
573 // TODO, report logic error to server, via websock, so it can be logged
Simon Huntfc274c92014-11-11 11:05:46 -0800574 console.warn(msg);
Simon Huntbb282f52014-11-10 11:08:19 -0800575 }
576
Simon Hunt99c13842014-11-06 18:23:12 -0800577 var eventDispatch = {
Simon Hunta5e89142014-11-14 07:00:33 -0800578 addInstance: addInstance,
Simon Hunt99c13842014-11-06 18:23:12 -0800579 addDevice: addDevice,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800580 addLink: addLink,
Simon Hunt56d51852014-11-09 13:03:35 -0800581 addHost: addHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800582
Simon Huntfcfb46c2014-11-19 12:53:38 -0800583 updateInstance: updateInstance,
Simon Huntbb282f52014-11-10 11:08:19 -0800584 updateDevice: updateDevice,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800585 updateLink: updateLink,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800586 updateHost: updateHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800587
Simon Hunt7b403bc2014-11-22 19:01:00 -0800588 removeInstance: removeInstance,
Simon Huntbb282f52014-11-10 11:08:19 -0800589 removeDevice: stillToImplement,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800590 removeLink: removeLink,
Simon Hunt44031102014-11-11 13:20:36 -0800591 removeHost: removeHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800592
Simon Hunt61d04042014-11-11 17:27:16 -0800593 showDetails: showDetails,
Thomas Vachuska47635c62014-11-22 01:21:36 -0800594 showSummary: showSummary,
Simon Huntb53e0682014-11-12 13:32:01 -0800595 showTraffic: showTraffic
Simon Hunt99c13842014-11-06 18:23:12 -0800596 };
597
Simon Hunta5e89142014-11-14 07:00:33 -0800598 function addInstance(data) {
599 evTrace(data);
600 var inst = data.payload,
601 id = inst.id;
602 if (onosInstances[id]) {
603 logicError('ONOS instance already added: ' + id);
604 return;
605 }
606 onosInstances[id] = inst;
607 onosOrder.push(inst);
608 updateInstances();
609 }
610
Simon Hunt99c13842014-11-06 18:23:12 -0800611 function addDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800612 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800613 var device = data.payload,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800614 nodeData = createDeviceNode(device);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800615 network.nodes.push(nodeData);
616 network.lookup[nodeData.id] = nodeData;
Simon Hunt99c13842014-11-06 18:23:12 -0800617 updateNodes();
618 network.force.start();
619 }
620
Simon Hunt99c13842014-11-06 18:23:12 -0800621 function addLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800622 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800623 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800624 result = findLink(link, 'add'),
625 bad = result.badLogic,
626 ldata = result.ldata;
627
628 if (bad) {
629 logicError(bad + ': ' + link.id);
630 return;
631 }
632
633 if (ldata) {
634 // we already have a backing store link for src/dst nodes
635 addLinkUpdate(ldata, link);
636 return;
637 }
638
639 // no backing store link yet
640 ldata = createLink(link);
641 if (ldata) {
642 network.links.push(ldata);
643 network.lookup[ldata.key] = ldata;
Simon Hunt99c13842014-11-06 18:23:12 -0800644 updateLinks();
645 network.force.start();
646 }
647 }
648
Simon Hunt56d51852014-11-09 13:03:35 -0800649 function addHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800650 evTrace(data);
Simon Hunt56d51852014-11-09 13:03:35 -0800651 var host = data.payload,
652 node = createHostNode(host),
653 lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800654 network.nodes.push(node);
655 network.lookup[host.id] = node;
656 updateNodes();
657
658 lnk = createHostLink(host);
659 if (lnk) {
Simon Hunt44031102014-11-11 13:20:36 -0800660 node.linkData = lnk; // cache ref on its host
Simon Hunt56d51852014-11-09 13:03:35 -0800661 network.links.push(lnk);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800662 network.lookup[host.ingress] = lnk;
663 network.lookup[host.egress] = lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800664 updateLinks();
665 }
666 network.force.start();
667 }
668
Simon Hunt44031102014-11-11 13:20:36 -0800669 // TODO: fold updateX(...) methods into one base method; remove duplication
Simon Hunt56a2ea42014-11-19 12:39:31 -0800670
671 function updateInstance(data) {
672 evTrace(data);
673 var inst = data.payload,
674 id = inst.id,
675 instData = onosInstances[id];
676 if (instData) {
677 $.extend(instData, inst);
678 updateInstances();
Simon Hunt56a2ea42014-11-19 12:39:31 -0800679 } else {
680 logicError('updateInstance lookup fail. ID = "' + id + '"');
681 }
682 }
683
Simon Huntbb282f52014-11-10 11:08:19 -0800684 function updateDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800685 evTrace(data);
Simon Huntbb282f52014-11-10 11:08:19 -0800686 var device = data.payload,
687 id = device.id,
Simon Hunt62c47542014-11-22 22:16:32 -0800688 d = network.lookup[id];
689 if (d) {
690 $.extend(d, device);
691 if (positionNode(d, true)) {
Simon Hunt395a70c2014-11-22 23:17:40 -0800692 sendUpdateMeta(d, true);
Simon Hunt62c47542014-11-22 22:16:32 -0800693 }
694 updateNodes();
Simon Huntbb282f52014-11-10 11:08:19 -0800695 } else {
696 logicError('updateDevice lookup fail. ID = "' + id + '"');
697 }
698 }
699
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800700 function updateLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800701 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800702 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800703 result = findLink(link, 'update'),
704 bad = result.badLogic;
705 if (bad) {
706 logicError(bad + ': ' + link.id);
707 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800708 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800709 result.updateWith(link);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800710 }
711
Simon Hunt7cd48f32014-11-09 23:42:50 -0800712 function updateHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800713 evTrace(data);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800714 var host = data.payload,
Simon Huntbb282f52014-11-10 11:08:19 -0800715 id = host.id,
716 hostData = network.lookup[id];
717 if (hostData) {
718 $.extend(hostData, host);
719 updateHostState(hostData);
720 } else {
721 logicError('updateHost lookup fail. ID = "' + id + '"');
722 }
Simon Hunt7cd48f32014-11-09 23:42:50 -0800723 }
724
Simon Hunt44031102014-11-11 13:20:36 -0800725 // TODO: fold removeX(...) methods into base method - remove dup code
Simon Hunt7b403bc2014-11-22 19:01:00 -0800726 function removeInstance(data) {
727 evTrace(data);
728 var inst = data.payload,
729 id = inst.id,
730 instData = onosInstances[id];
731 if (instData) {
732 var idx = find(id, onosOrder, 'id');
733 if (idx >= 0) {
734 onosOrder.splice(idx, 1);
735 }
736 delete onosInstances[id];
737 updateInstances();
738 } else {
739 logicError('updateInstance lookup fail. ID = "' + id + '"');
740 }
741 }
742
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800743 function removeLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800744 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800745 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800746 result = findLink(link, 'remove'),
747 bad = result.badLogic;
748 if (bad) {
749 logicError(bad + ': ' + link.id);
750 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800751 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800752 result.removeRawLink();
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800753 }
754
Simon Hunt44031102014-11-11 13:20:36 -0800755 function removeHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800756 evTrace(data);
Simon Hunt44031102014-11-11 13:20:36 -0800757 var host = data.payload,
758 id = host.id,
759 hostData = network.lookup[id];
760 if (hostData) {
761 removeHostElement(hostData);
762 } else {
763 logicError('removeHost lookup fail. ID = "' + id + '"');
764 }
765 }
766
Thomas Vachuska47635c62014-11-22 01:21:36 -0800767 function showSummary(data) {
768 evTrace(data);
769 populateSummary(data.payload);
770 summaryPane.show();
771 }
772
Simon Hunt61d04042014-11-11 17:27:16 -0800773 function showDetails(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800774 evTrace(data);
Simon Hunt61d04042014-11-11 17:27:16 -0800775 populateDetails(data.payload);
776 detailPane.show();
777 }
778
Simon Huntb53e0682014-11-12 13:32:01 -0800779 function showTraffic(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800780 evTrace(data);
Thomas Vachuska4731f122014-11-20 04:56:19 -0800781 var paths = data.payload.paths,
782 hasTraffic = false;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800783
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800784 // Revert any links hilighted previously.
Thomas Vachuska4731f122014-11-20 04:56:19 -0800785 link.style('stroke-width', null)
Thomas Vachuskaa3148a72014-11-19 21:38:35 -0800786 .classed('primary secondary animated optical', false);
Simon Hunte2575b62014-11-18 15:25:53 -0800787 // Remove all previous labels.
788 removeLinkLabels();
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800789
Simon Hunte2575b62014-11-18 15:25:53 -0800790 // Now hilight all links in the paths payload, and attach
791 // labels to them, if they are defined.
Simon Hunta255a2c2014-11-13 22:29:35 -0800792 paths.forEach(function (p) {
Simon Hunte2575b62014-11-18 15:25:53 -0800793 var n = p.links.length,
794 i,
795 ldata;
796
Thomas Vachuska4731f122014-11-20 04:56:19 -0800797 hasTraffic = hasTraffic || p.traffic;
Simon Hunte2575b62014-11-18 15:25:53 -0800798 for (i=0; i<n; i++) {
799 ldata = findLinkById(p.links[i]);
Thomas Vachuska4731f122014-11-20 04:56:19 -0800800 if (ldata && ldata.el) {
Simon Hunte2575b62014-11-18 15:25:53 -0800801 ldata.el.classed(p.class, true);
802 ldata.label = p.labels[i];
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800803 }
Simon Hunte2575b62014-11-18 15:25:53 -0800804 }
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800805 });
Thomas Vachuska4731f122014-11-20 04:56:19 -0800806
Simon Hunte2575b62014-11-18 15:25:53 -0800807 updateLinks();
Thomas Vachuska4731f122014-11-20 04:56:19 -0800808
809 if (hasTraffic && !antTimer) {
810 startAntTimer();
811 } else if (!hasTraffic && antTimer) {
812 stopAntTimer();
813 }
Simon Huntb53e0682014-11-12 13:32:01 -0800814 }
815
Simon Hunt56d51852014-11-09 13:03:35 -0800816 // ...............................
817
818 function stillToImplement(data) {
819 var p = data.payload;
820 note(data.event, p.id);
Simon Hunt7b403bc2014-11-22 19:01:00 -0800821 if (!config.useLiveData) {
822 network.view.alert('Not yet implemented: "' + data.event + '"');
823 }
Simon Hunt56d51852014-11-09 13:03:35 -0800824 }
Simon Hunt99c13842014-11-06 18:23:12 -0800825
826 function unknownEvent(data) {
Simon Hunt434cf142014-11-24 11:10:28 -0800827 console.warn('Unknown event type: "' + data.event + '"', data);
Simon Hunt99c13842014-11-06 18:23:12 -0800828 }
829
830 function handleServerEvent(data) {
831 var fn = eventDispatch[data.event] || unknownEvent;
832 fn(data);
833 }
834
835 // ==============================
Simon Hunt61d04042014-11-11 17:27:16 -0800836 // Out-going messages...
837
Simon Huntb53e0682014-11-12 13:32:01 -0800838 function nSel() {
839 return selectOrder.length;
840 }
Simon Hunt61d04042014-11-11 17:27:16 -0800841 function getSel(idx) {
842 return selections[selectOrder[idx]];
843 }
Simon Huntb53e0682014-11-12 13:32:01 -0800844 function getSelId(idx) {
845 return getSel(idx).obj.id;
846 }
Thomas Vachuska9edca302014-11-22 17:06:42 -0800847 function getSelIds(start, endOffset) {
848 var end = selectOrder.length - endOffset;
849 var ids = [];
850 selectOrder.slice(start, end).forEach(function (d) {
851 ids.push(getSelId(d));
852 });
853 return ids;
854 }
Simon Huntb53e0682014-11-12 13:32:01 -0800855 function allSelectionsClass(cls) {
856 for (var i=0, n=nSel(); i<n; i++) {
857 if (getSel(i).obj.class !== cls) {
858 return false;
859 }
860 }
861 return true;
862 }
Simon Hunt61d04042014-11-11 17:27:16 -0800863
Thomas Vachuska47635c62014-11-22 01:21:36 -0800864 function toggleInstances() {
865 if (!oiBox.isVisible()) {
Simon Huntb0ecfa52014-11-23 21:05:12 -0800866 showInstances();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800867 } else {
Simon Huntb0ecfa52014-11-23 21:05:12 -0800868 hideInstances();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800869 }
870 }
871
Simon Huntb0ecfa52014-11-23 21:05:12 -0800872 function showInstances() {
873 oiBox.show();
874 colorAffinity = true;
875 updateDeviceColors();
876 }
877
878 function hideInstances() {
879 oiBox.hide();
880 colorAffinity = false;
881 cancelAffinity();
882 updateDeviceColors();
883 }
884
885
Thomas Vachuska47635c62014-11-22 01:21:36 -0800886 function toggleSummary() {
887 if (!summaryPane.isVisible()) {
888 requestSummary();
889 } else {
890 cancelSummary();
891 }
892 }
893
894 // request overall summary data
895 function requestSummary() {
896 sendMessage('requestSummary', {});
897 }
898
899 function cancelSummary() {
900 sendMessage('cancelSummary', {});
901 summaryPane.hide();
902 }
903
Simon Hunt61d04042014-11-11 17:27:16 -0800904 // request details for the selected element
Simon Huntd72bc702014-11-13 18:38:04 -0800905 // invoked from selection of a single node.
Simon Hunt61d04042014-11-11 17:27:16 -0800906 function requestDetails() {
907 var data = getSel(0).obj,
908 payload = {
909 id: data.id,
910 class: data.class
911 };
912 sendMessage('requestDetails', payload);
913 }
914
Thomas Vachuska9edca302014-11-22 17:06:42 -0800915 function addHostIntentAction() {
Simon Huntd72bc702014-11-13 18:38:04 -0800916 sendMessage('addHostIntent', {
Thomas Vachuska9edca302014-11-22 17:06:42 -0800917 one: selectOrder[0],
918 two: selectOrder[1],
919 ids: selectOrder
Simon Huntd72bc702014-11-13 18:38:04 -0800920 });
Thomas Vachuska9edca302014-11-22 17:06:42 -0800921 network.view.flash('Host-to-Host flow added');
Simon Huntd72bc702014-11-13 18:38:04 -0800922 }
923
Thomas Vachuska9edca302014-11-22 17:06:42 -0800924 function addMultiSourceIntentAction() {
925 sendMessage('addMultiSourceIntent', {
926 src: selectOrder.slice(0, selectOrder.length - 1),
927 dst: selectOrder[selectOrder.length - 1],
928 ids: selectOrder
929 });
930 network.view.flash('Multi-Source flow added');
Thomas Vachuska29617e52014-11-20 03:17:46 -0800931 }
932
Thomas Vachuska9edca302014-11-22 17:06:42 -0800933
Thomas Vachuska47635c62014-11-22 01:21:36 -0800934 function cancelTraffic() {
935 sendMessage('cancelTraffic', {});
936 }
937
Thomas Vachuska9edca302014-11-22 17:06:42 -0800938 function requestTrafficForMode() {
939 if (hoverMode === hoverModeAll) {
940 requestAllTraffic();
941 } else if (hoverMode === hoverModeFlows) {
942 requestDeviceLinkFlows();
943 } else if (hoverMode === hoverModeIntents) {
944 requestSelectTraffic();
Simon Huntd72bc702014-11-13 18:38:04 -0800945 }
Thomas Vachuska9edca302014-11-22 17:06:42 -0800946 }
Simon Huntd72bc702014-11-13 18:38:04 -0800947
Thomas Vachuska9edca302014-11-22 17:06:42 -0800948 function showTrafficAction() {
949 hoverMode = hoverModeIntents;
950 requestSelectTraffic();
951 network.view.flash('Related Traffic');
952 }
953
954 function requestSelectTraffic() {
955 if (validateSelectionContext()) {
956 var hoverId = (hoverMode === hoverModeIntents && hovered &&
957 (hovered.class === 'host' || hovered.class === 'device'))
Simon Huntd72bc702014-11-13 18:38:04 -0800958 ? hovered.id : '';
Thomas Vachuska9edca302014-11-22 17:06:42 -0800959 sendMessage('requestTraffic', {
960 ids: selectOrder,
961 hover: hoverId
962 });
963 }
Simon Huntd72bc702014-11-13 18:38:04 -0800964 }
965
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800966
Thomas Vachuska29617e52014-11-20 03:17:46 -0800967 function showDeviceLinkFlowsAction() {
Thomas Vachuska9edca302014-11-22 17:06:42 -0800968 hoverMode = hoverModeFlows;
969 requestDeviceLinkFlows();
Thomas Vachuskac59658c2014-11-21 13:13:36 -0800970 network.view.flash('Device Flows');
Thomas Vachuska29617e52014-11-20 03:17:46 -0800971 }
972
Thomas Vachuska9edca302014-11-22 17:06:42 -0800973 function requestDeviceLinkFlows() {
974 if (validateSelectionContext()) {
975 var hoverId = (hoverMode === hoverModeFlows && hovered &&
976 (hovered.class === 'device')) ? hovered.id : '';
977 sendMessage('requestDeviceLinkFlows', {
978 ids: selectOrder,
979 hover: hoverId
980 });
981 }
982 }
983
984
985 function showAllTrafficAction() {
986 hoverMode = hoverModeAll;
987 requestAllTraffic();
988 network.view.flash('All Traffic');
989 }
990
991 function requestAllTraffic() {
992 sendMessage('requestAllTraffic', {});
993 }
994
995 function validateSelectionContext() {
Thomas Vachuska29617e52014-11-20 03:17:46 -0800996 if (!hovered && nSel() === 0) {
Thomas Vachuska47635c62014-11-22 01:21:36 -0800997 cancelTraffic();
Thomas Vachuska9edca302014-11-22 17:06:42 -0800998 return false;
Thomas Vachuska29617e52014-11-20 03:17:46 -0800999 }
Thomas Vachuska9edca302014-11-22 17:06:42 -08001000 return true;
Thomas Vachuska29617e52014-11-20 03:17:46 -08001001 }
Simon Huntd72bc702014-11-13 18:38:04 -08001002
Simon Hunta6a9fe72014-11-20 11:17:12 -08001003
Simon Hunt61d04042014-11-11 17:27:16 -08001004 // ==============================
Simon Hunta5e89142014-11-14 07:00:33 -08001005 // onos instance panel functions
Simon Huntb82f6902014-11-22 11:53:15 -08001006
Simon Hunt7b403bc2014-11-22 19:01:00 -08001007 var instCfg = {
1008 rectPad: 8,
1009 nodeOx: 9,
1010 nodeOy: 9,
1011 nodeDim: 40,
1012 birdOx: 19,
1013 birdOy: 21,
1014 birdDim: 21,
1015 uiDy: 45,
1016 titleDy: 30,
1017 textYOff: 20,
1018 textYSpc: 15
1019 };
1020
1021 function viewBox(dim) {
1022 return '0 0 ' + dim.w + ' ' + dim.h;
1023 }
1024
1025 function instRectAttr(dim) {
1026 var pad = instCfg.rectPad;
1027 return {
1028 x: pad,
1029 y: pad,
1030 width: dim.w - pad*2,
1031 height: dim.h - pad*2,
Simon Huntb0ecfa52014-11-23 21:05:12 -08001032 rx: 6
Simon Hunt7b403bc2014-11-22 19:01:00 -08001033 };
1034 }
1035
1036 function computeDim(self) {
1037 var css = window.getComputedStyle(self);
1038 return {
1039 w: stripPx(css.width),
1040 h: stripPx(css.height)
1041 };
Simon Huntb82f6902014-11-22 11:53:15 -08001042 }
Simon Hunta5e89142014-11-14 07:00:33 -08001043
1044 function updateInstances() {
1045 var onoses = oiBox.el.selectAll('.onosInst')
Simon Huntb82f6902014-11-22 11:53:15 -08001046 .data(onosOrder, function (d) { return d.id; }),
Simon Hunt7b403bc2014-11-22 19:01:00 -08001047 instDim = {w:0,h:0},
1048 c = instCfg;
Simon Hunta5e89142014-11-14 07:00:33 -08001049
Simon Hunt7b403bc2014-11-22 19:01:00 -08001050 function nSw(n) {
1051 return '# Switches: ' + n;
1052 }
Simon Hunta5e89142014-11-14 07:00:33 -08001053
Simon Huntb82f6902014-11-22 11:53:15 -08001054 // operate on existing onos instances if necessary
1055 onoses.each(function (d) {
1056 var el = d3.select(this),
1057 svg = el.select('svg');
Simon Hunt7b403bc2014-11-22 19:01:00 -08001058 instDim = computeDim(this);
Simon Huntb82f6902014-11-22 11:53:15 -08001059
1060 // update online state
1061 el.classed('online', d.online);
1062
1063 // update ui-attached state
1064 svg.select('use.uiBadge').remove();
1065 if (d.uiAttached) {
1066 attachUiBadge(svg);
1067 }
1068
Simon Hunt7b403bc2014-11-22 19:01:00 -08001069 function updAttr(id, value) {
1070 svg.select('text.instLabel.'+id).text(value);
1071 }
1072
1073 updAttr('ip', d.ip);
1074 updAttr('ns', nSw(d.switches));
Simon Huntb82f6902014-11-22 11:53:15 -08001075 });
1076
1077
1078 // operate on new onos instances
Simon Hunta5e89142014-11-14 07:00:33 -08001079 var entering = onoses.enter()
1080 .append('div')
1081 .attr('class', 'onosInst')
1082 .classed('online', function (d) { return d.online; })
Simon Hunt9c15eca2014-11-15 18:37:59 -08001083 .on('click', clickInst);
1084
Simon Huntb82f6902014-11-22 11:53:15 -08001085 entering.each(function (d) {
Simon Hunt9c15eca2014-11-15 18:37:59 -08001086 var el = d3.select(this),
Simon Hunt7b403bc2014-11-22 19:01:00 -08001087 rectAttr,
1088 svg;
1089 instDim = computeDim(this);
1090 rectAttr = instRectAttr(instDim);
Simon Hunt9c15eca2014-11-15 18:37:59 -08001091
Simon Hunt7b403bc2014-11-22 19:01:00 -08001092 svg = el.append('svg').attr({
1093 width: instDim.w,
1094 height: instDim.h,
1095 viewBox: viewBox(instDim)
Simon Hunt95908012014-11-20 10:20:26 -08001096 });
Simon Huntb82f6902014-11-22 11:53:15 -08001097
Simon Hunt7b403bc2014-11-22 19:01:00 -08001098 svg.append('rect').attr(rectAttr);
Simon Hunt9c15eca2014-11-15 18:37:59 -08001099
Thomas Vachuskae02e11c2014-11-24 16:13:52 -08001100 //appendGlyph(svg, c.nodeOx, c.nodeOy, c.nodeDim, '#node');
1101 appendBadge(svg, 14, 14, 28, '#bird');
Simon Huntb82f6902014-11-22 11:53:15 -08001102
1103 if (d.uiAttached) {
1104 attachUiBadge(svg);
1105 }
1106
Simon Hunt7b403bc2014-11-22 19:01:00 -08001107 var left = c.nodeOx + c.nodeDim,
1108 len = rectAttr.width - left,
1109 hlen = len / 2,
1110 midline = hlen + left;
Simon Hunt9c15eca2014-11-15 18:37:59 -08001111
Simon Hunt7b403bc2014-11-22 19:01:00 -08001112 // title
1113 svg.append('text')
1114 .attr({
1115 class: 'instTitle',
1116 x: midline,
1117 y: c.titleDy
1118 })
1119 .text(d.id);
1120
1121 // a couple of attributes
1122 var ty = c.titleDy + c.textYOff;
1123
1124 function addAttr(id, label) {
1125 svg.append('text').attr({
1126 class: 'instLabel ' + id,
1127 x: midline,
1128 y: ty
1129 }).text(label);
1130 ty += c.textYSpc;
1131 }
1132
1133 addAttr('ip', d.ip);
1134 addAttr('ns', nSw(d.switches));
Simon Hunt9c15eca2014-11-15 18:37:59 -08001135 });
Simon Hunta5e89142014-11-14 07:00:33 -08001136
1137 // operate on existing + new onoses here
Simon Hunt8f40cce2014-11-23 15:57:30 -08001138 // set the affinity colors...
1139 onoses.each(function (d) {
1140 var el = d3.select(this),
1141 rect = el.select('svg').select('rect'),
1142 col = instColor(d.id, d.online);
1143 rect.style('fill', col);
1144 });
Simon Hunta5e89142014-11-14 07:00:33 -08001145
Simon Hunt7b403bc2014-11-22 19:01:00 -08001146 // adjust the panel size appropriately...
1147 oiBox.width(instDim.w * onosOrder.length);
1148 oiBox.height(instDim.h);
1149
1150 // remove any outgoing instances
1151 onoses.exit().remove();
Simon Hunta5e89142014-11-14 07:00:33 -08001152 }
1153
Simon Hunt8f40cce2014-11-23 15:57:30 -08001154 function instColor(id, online) {
1155 return cat7.get(id, !online, network.view.getTheme());
1156 }
1157
Simon Hunt9462e8c2014-11-14 17:28:09 -08001158 function clickInst(d) {
1159 var el = d3.select(this),
1160 aff = el.classed('affinity');
1161 if (!aff) {
1162 setAffinity(el, d);
1163 } else {
1164 cancelAffinity();
1165 }
1166 }
1167
1168 function setAffinity(el, d) {
1169 d3.selectAll('.onosInst')
1170 .classed('mastership', true)
1171 .classed('affinity', false);
1172 el.classed('affinity', true);
1173
1174 suppressLayers(true);
1175 node.each(function (n) {
1176 if (n.master === d.id) {
1177 n.el.classed('suppressed', false);
1178 }
1179 });
1180 oiShowMaster = true;
1181 }
1182
1183 function cancelAffinity() {
1184 d3.selectAll('.onosInst')
1185 .classed('mastership affinity', false);
1186 restoreLayerState();
1187 oiShowMaster = false;
1188 }
1189
Simon Hunt7b403bc2014-11-22 19:01:00 -08001190 // TODO: these should be moved out to utility module.
1191 function stripPx(s) {
1192 return s.replace(/px$/,'');
1193 }
1194
1195 function appendUse(svg, ox, oy, dim, iid, cls) {
1196 var use = svg.append('use').attr({
1197 transform: translate(ox,oy),
1198 'xlink:href': iid,
1199 width: dim,
1200 height: dim
1201 });
1202 if (cls) {
1203 use.classed(cls, true);
1204 }
1205 return use;
1206 }
1207
1208 function appendGlyph(svg, ox, oy, dim, iid, cls) {
1209 appendUse(svg, ox, oy, dim, iid, cls).classed('glyphIcon', true);
1210 }
1211
1212 function appendBadge(svg, ox, oy, dim, iid, cls) {
1213 appendUse(svg, ox, oy, dim, iid,cls ).classed('badgeIcon', true);
1214 }
1215
1216 function attachUiBadge(svg) {
1217 appendBadge(svg, 12, instCfg.uiDy, 30, '#uiAttached', 'uiBadge');
1218 }
1219
Simon Hunt434cf142014-11-24 11:10:28 -08001220 function visVal(b) {
1221 return b ? 'visible' : 'hidden';
1222 }
1223
Simon Hunta5e89142014-11-14 07:00:33 -08001224 // ==============================
Simon Hunt99c13842014-11-06 18:23:12 -08001225 // force layout modification functions
1226
1227 function translate(x, y) {
1228 return 'translate(' + x + ',' + y + ')';
1229 }
1230
Simon Hunte2575b62014-11-18 15:25:53 -08001231 function rotate(deg) {
1232 return 'rotate(' + deg + ')';
1233 }
1234
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001235 function missMsg(what, id) {
1236 return '\n[' + what + '] "' + id + '" missing ';
1237 }
1238
1239 function linkEndPoints(srcId, dstId) {
1240 var srcNode = network.lookup[srcId],
1241 dstNode = network.lookup[dstId],
1242 sMiss = !srcNode ? missMsg('src', srcId) : '',
1243 dMiss = !dstNode ? missMsg('dst', dstId) : '';
1244
1245 if (sMiss || dMiss) {
1246 logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
1247 return null;
1248 }
1249 return {
1250 source: srcNode,
1251 target: dstNode,
1252 x1: srcNode.x,
1253 y1: srcNode.y,
1254 x2: dstNode.x,
1255 y2: dstNode.y
1256 };
1257 }
1258
Simon Hunt56d51852014-11-09 13:03:35 -08001259 function createHostLink(host) {
1260 var src = host.id,
1261 dst = host.cp.device,
Simon Hunt7cd48f32014-11-09 23:42:50 -08001262 id = host.ingress,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001263 lnk = linkEndPoints(src, dst);
Simon Hunt56d51852014-11-09 13:03:35 -08001264
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001265 if (!lnk) {
Simon Hunt56d51852014-11-09 13:03:35 -08001266 return null;
1267 }
1268
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001269 // Synthesize link ...
1270 $.extend(lnk, {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001271 key: id,
Simon Hunt56d51852014-11-09 13:03:35 -08001272 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -08001273
1274 type: function () { return 'hostLink'; },
1275 // TODO: ideally, we should see if our edge switch is online...
1276 online: function () { return true; },
1277 linkWidth: function () { return 1; }
Simon Hunt7cd48f32014-11-09 23:42:50 -08001278 });
Simon Hunt99c13842014-11-06 18:23:12 -08001279 return lnk;
1280 }
1281
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001282 function createLink(link) {
Simon Hunta6a9fe72014-11-20 11:17:12 -08001283 var lnk = linkEndPoints(link.src, link.dst);
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001284
1285 if (!lnk) {
1286 return null;
1287 }
1288
Simon Hunt8257f4c2014-11-16 19:34:54 -08001289 $.extend(lnk, {
1290 key: link.id,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001291 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -08001292 fromSource: link,
1293
1294 // functions to aggregate dual link state
1295 type: function () {
1296 var s = lnk.fromSource,
1297 t = lnk.fromTarget;
1298 return (s && s.type) || (t && t.type) || defaultLinkType;
1299 },
1300 online: function () {
1301 var s = lnk.fromSource,
1302 t = lnk.fromTarget;
1303 return (s && s.online) || (t && t.online);
1304 },
1305 linkWidth: function () {
1306 var s = lnk.fromSource,
1307 t = lnk.fromTarget,
1308 ws = (s && s.linkWidth) || 0,
1309 wt = (t && t.linkWidth) || 0;
1310 return Math.max(ws, wt);
1311 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001312 });
1313 return lnk;
Simon Hunt1a9eff92014-11-07 11:06:34 -08001314 }
1315
Simon Hunte2575b62014-11-18 15:25:53 -08001316 function removeLinkLabels() {
1317 network.links.forEach(function (d) {
1318 d.label = '';
1319 });
1320 }
1321
Simon Hunt434cf142014-11-24 11:10:28 -08001322 function showHostVis(el) {
1323 el.style('visibility', visVal(showHosts));
1324 }
1325
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001326 var widthRatio = 1.4,
1327 linkScale = d3.scale.linear()
1328 .domain([1, 12])
1329 .range([widthRatio, 12 * widthRatio])
1330 .clamp(true);
1331
Simon Hunt99c13842014-11-06 18:23:12 -08001332 function updateLinks() {
1333 link = linkG.selectAll('.link')
Simon Hunt8257f4c2014-11-16 19:34:54 -08001334 .data(network.links, function (d) { return d.key; });
Simon Hunt99c13842014-11-06 18:23:12 -08001335
1336 // operate on existing links, if necessary
1337 // link .foo() .bar() ...
1338
1339 // operate on entering links:
1340 var entering = link.enter()
1341 .append('line')
1342 .attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001343 x1: function (d) { return d.x1; },
1344 y1: function (d) { return d.y1; },
1345 x2: function (d) { return d.x2; },
1346 y2: function (d) { return d.y2; },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001347 stroke: config.topo.linkInColor,
1348 'stroke-width': config.topo.linkInWidth
Simon Hunt99c13842014-11-06 18:23:12 -08001349 });
1350
1351 // augment links
Simon Hunt7cd48f32014-11-09 23:42:50 -08001352 entering.each(function (d) {
1353 var link = d3.select(this);
1354 // provide ref to element selection from backing data....
1355 d.el = link;
Simon Hunt8257f4c2014-11-16 19:34:54 -08001356 restyleLinkElement(d);
Simon Hunt434cf142014-11-24 11:10:28 -08001357 if (d.type() === 'hostLink') {
1358 showHostVis(link);
1359 }
Simon Hunt7cd48f32014-11-09 23:42:50 -08001360 });
Thomas Vachuska4830d392014-11-09 17:09:56 -08001361
1362 // operate on both existing and new links, if necessary
1363 //link .foo() .bar() ...
1364
Simon Hunte2575b62014-11-18 15:25:53 -08001365 // apply or remove labels
1366 var labelData = getLabelData();
1367 applyLinkLabels(labelData);
1368
Thomas Vachuska4830d392014-11-09 17:09:56 -08001369 // operate on exiting links:
Thomas Vachuska4830d392014-11-09 17:09:56 -08001370 link.exit()
Simon Hunt13bf9c82014-11-18 07:26:44 -08001371 .attr('stroke-dasharray', '3, 3')
1372 .style('opacity', 0.5)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001373 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -08001374 .duration(1500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001375 .attr({
Simon Hunt13bf9c82014-11-18 07:26:44 -08001376 'stroke-dasharray': '3, 12',
1377 stroke: config.topo.linkOutColor,
1378 'stroke-width': config.topo.linkOutWidth
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001379 })
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001380 .style('opacity', 0.0)
Thomas Vachuska4830d392014-11-09 17:09:56 -08001381 .remove();
Simon Hunte2575b62014-11-18 15:25:53 -08001382
1383 // NOTE: invoke a single tick to force the labels to position
1384 // onto their links.
1385 tick();
1386 }
1387
1388 function getLabelData() {
1389 // create the backing data for showing labels..
1390 var data = [];
1391 link.each(function (d) {
1392 if (d.label) {
1393 data.push({
1394 id: 'lab-' + d.key,
1395 key: d.key,
1396 label: d.label,
1397 ldata: d
1398 });
1399 }
1400 });
1401 return data;
1402 }
1403
1404 var linkLabelOffset = '0.3em';
1405
1406 function applyLinkLabels(data) {
1407 var entering;
1408
1409 linkLabel = linkLabelG.selectAll('.linkLabel')
1410 .data(data, function (d) { return d.id; });
1411
Simon Hunt56a2ea42014-11-19 12:39:31 -08001412 // for elements already existing, we need to update the text
1413 // and adjust the rectangle size to fit
1414 linkLabel.each(function (d) {
1415 var el = d3.select(this),
1416 rect = el.select('rect'),
1417 text = el.select('text');
1418 text.text(d.label);
1419 rect.attr(rectAroundText(el));
1420 });
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -08001421
Simon Hunte2575b62014-11-18 15:25:53 -08001422 entering = linkLabel.enter().append('g')
1423 .classed('linkLabel', true)
1424 .attr('id', function (d) { return d.id; });
1425
1426 entering.each(function (d) {
1427 var el = d3.select(this),
1428 rect,
1429 text,
1430 parms = {
1431 x1: d.ldata.x1,
1432 y1: d.ldata.y1,
1433 x2: d.ldata.x2,
1434 y2: d.ldata.y2
1435 };
1436
1437 d.el = el;
1438 rect = el.append('rect');
1439 text = el.append('text').text(d.label);
1440 rect.attr(rectAroundText(el));
1441 text.attr('dy', linkLabelOffset);
1442
1443 el.attr('transform', transformLabel(parms));
1444 });
1445
1446 // Remove any links that are no longer required.
1447 linkLabel.exit().remove();
1448 }
1449
1450 function rectAroundText(el) {
1451 var text = el.select('text'),
1452 box = text.node().getBBox();
1453
1454 // translate the bbox so that it is centered on [x,y]
1455 box.x = -box.width / 2;
1456 box.y = -box.height / 2;
1457
1458 // add padding
1459 box.x -= 1;
1460 box.width += 2;
1461 return box;
1462 }
1463
1464 function transformLabel(p) {
1465 var dx = p.x2 - p.x1,
1466 dy = p.y2 - p.y1,
1467 xMid = dx/2 + p.x1,
1468 yMid = dy/2 + p.y1;
Simon Hunte2575b62014-11-18 15:25:53 -08001469 return translate(xMid, yMid);
Simon Hunt99c13842014-11-06 18:23:12 -08001470 }
1471
1472 function createDeviceNode(device) {
1473 // start with the object as is
1474 var node = device,
Simon Huntbb282f52014-11-10 11:08:19 -08001475 type = device.type,
Simon Huntc72967b2014-11-20 09:21:42 -08001476 svgCls = type ? 'node device ' + type : 'node device',
1477 labels = device.labels || [];
1478
Simon Hunt99c13842014-11-06 18:23:12 -08001479 // Augment as needed...
1480 node.class = 'device';
Simon Huntbb282f52014-11-10 11:08:19 -08001481 node.svgClass = device.online ? svgCls + ' online' : svgCls;
Simon Hunt99c13842014-11-06 18:23:12 -08001482 positionNode(node);
Simon Hunt99c13842014-11-06 18:23:12 -08001483 return node;
1484 }
1485
Simon Hunt56d51852014-11-09 13:03:35 -08001486 function createHostNode(host) {
1487 // start with the object as is
1488 var node = host;
1489
1490 // Augment as needed...
1491 node.class = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001492 if (!node.type) {
Simon Hunt209155e2014-11-21 12:16:09 -08001493 node.type = 'endstation';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001494 }
Simon Hunt7fa116d2014-11-17 14:16:55 -08001495 node.svgClass = 'node host ' + node.type;
Simon Hunt56d51852014-11-09 13:03:35 -08001496 positionNode(node);
Simon Hunt56d51852014-11-09 13:03:35 -08001497 return node;
1498 }
1499
Simon Hunt62c47542014-11-22 22:16:32 -08001500 function positionNode(node, forUpdate) {
Simon Hunt99c13842014-11-06 18:23:12 -08001501 var meta = node.metaUi,
Simon Huntac9e24f2014-11-12 10:12:21 -08001502 x = meta && meta.x,
1503 y = meta && meta.y,
1504 xy;
Simon Hunt99c13842014-11-06 18:23:12 -08001505
Simon Huntac9e24f2014-11-12 10:12:21 -08001506 // If we have [x,y] already, use that...
Simon Hunt99c13842014-11-06 18:23:12 -08001507 if (x && y) {
1508 node.fixed = true;
Simon Hunt62c47542014-11-22 22:16:32 -08001509 node.px = node.x = x;
1510 node.py = node.y = y;
1511 //node.px = x;
1512 //node.py = y;
Simon Huntac9e24f2014-11-12 10:12:21 -08001513 return;
Simon Hunt99c13842014-11-06 18:23:12 -08001514 }
Simon Huntac9e24f2014-11-12 10:12:21 -08001515
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001516 var location = node.location;
1517 if (location && location.type === 'latlng') {
1518 var coord = geoMapProjection([location.lng, location.lat]);
1519 node.fixed = true;
Simon Hunt62c47542014-11-22 22:16:32 -08001520 node.px = node.x = coord[0];
1521 node.py = node.y = coord[1];
1522 //node.x = coord[0];
1523 //node.y = coord[1];
1524 return true;
1525 }
1526
1527 // if this is a node update (not a node add).. skip randomizer
1528 if (forUpdate) {
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001529 return;
1530 }
1531
Simon Huntac9e24f2014-11-12 10:12:21 -08001532 // Note: Placing incoming unpinned nodes at exactly the same point
1533 // (center of the view) causes them to explode outwards when
1534 // the force layout kicks in. So, we spread them out a bit
1535 // initially, to provide a more serene layout convergence.
1536 // Additionally, if the node is a host, we place it near
1537 // the device it is connected to.
1538
1539 function spread(s) {
1540 return Math.floor((Math.random() * s) - s/2);
1541 }
1542
1543 function randDim(dim) {
1544 return dim / 2 + spread(dim * 0.7071);
1545 }
1546
1547 function rand() {
1548 return {
1549 x: randDim(network.view.width()),
1550 y: randDim(network.view.height())
1551 };
1552 }
1553
1554 function near(node) {
1555 var min = 12,
1556 dx = spread(12),
1557 dy = spread(12);
1558 return {
1559 x: node.x + min + dx,
1560 y: node.y + min + dy
1561 };
1562 }
1563
1564 function getDevice(cp) {
1565 var d = network.lookup[cp.device];
1566 return d || rand();
1567 }
1568
1569 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
1570 $.extend(node, xy);
Simon Hunt99c13842014-11-06 18:23:12 -08001571 }
1572
Simon Hunt99c13842014-11-06 18:23:12 -08001573
Simon Huntc72967b2014-11-20 09:21:42 -08001574 function iconGlyphUrl(d) {
1575 var which = d.type || 'unknown';
1576 return '#' + which;
1577 }
1578
Simon Hunt99c13842014-11-06 18:23:12 -08001579 // returns the newly computed bounding box of the rectangle
1580 function adjustRectToFitText(n) {
1581 var text = n.select('text'),
1582 box = text.node().getBBox(),
1583 lab = config.labels;
1584
1585 text.attr('text-anchor', 'middle')
1586 .attr('y', '-0.8em')
1587 .attr('x', lab.imgPad/2);
1588
1589 // translate the bbox so that it is centered on [x,y]
1590 box.x = -box.width / 2;
1591 box.y = -box.height / 2;
1592
1593 // add padding
1594 box.x -= (lab.padLR + lab.imgPad/2);
1595 box.width += lab.padLR * 2 + lab.imgPad;
1596 box.y -= lab.padTB;
1597 box.height += lab.padTB * 2;
1598
1599 return box;
1600 }
1601
Simon Hunt1a9eff92014-11-07 11:06:34 -08001602 function mkSvgClass(d) {
1603 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
1604 }
1605
Simon Hunt7cd48f32014-11-09 23:42:50 -08001606 function hostLabel(d) {
1607 var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0;
1608 return d.labels[idx];
1609 }
1610 function deviceLabel(d) {
1611 var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0;
1612 return d.labels[idx];
1613 }
Simon Huntc72967b2014-11-20 09:21:42 -08001614 function trimLabel(label) {
1615 return (label && label.trim()) || '';
1616 }
1617
1618 function emptyBox() {
1619 return {
1620 x: -2,
1621 y: -2,
1622 width: 4,
1623 height: 4
1624 };
Simon Hunt7cd48f32014-11-09 23:42:50 -08001625 }
1626
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001627 function updateDeviceLabel(d) {
Simon Huntc72967b2014-11-20 09:21:42 -08001628 var label = trimLabel(deviceLabel(d)),
1629 noLabel = !label,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001630 node = d.el,
Simon Huntc72967b2014-11-20 09:21:42 -08001631 box,
1632 dx,
Simon Hunt395a70c2014-11-22 23:17:40 -08001633 dy,
1634 cfg = config.icons.device;
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001635
1636 node.select('text')
1637 .text(label)
1638 .style('opacity', 0)
1639 .transition()
1640 .style('opacity', 1);
1641
Simon Huntc72967b2014-11-20 09:21:42 -08001642 if (noLabel) {
1643 box = emptyBox();
Simon Hunt395a70c2014-11-22 23:17:40 -08001644 dx = -cfg.dim/2;
1645 dy = -cfg.dim/2;
Simon Huntc72967b2014-11-20 09:21:42 -08001646 } else {
1647 box = adjustRectToFitText(node);
Simon Hunt395a70c2014-11-22 23:17:40 -08001648 dx = box.x + cfg.xoff;
1649 dy = box.y + cfg.yoff;
Simon Huntc72967b2014-11-20 09:21:42 -08001650 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001651
1652 node.select('rect')
1653 .transition()
1654 .attr(box);
1655
Simon Huntc72967b2014-11-20 09:21:42 -08001656 node.select('g.deviceIcon')
Thomas Vachuska89543292014-11-19 11:28:33 -08001657 .transition()
Simon Huntc72967b2014-11-20 09:21:42 -08001658 .attr('transform', translate(dx, dy));
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001659 }
1660
1661 function updateHostLabel(d) {
1662 var label = hostLabel(d),
1663 host = d.el;
1664
1665 host.select('text').text(label);
1666 }
1667
Simon Hunt62c47542014-11-22 22:16:32 -08001668 // FIXME : fold this into updateNodes.
Simon Huntbb282f52014-11-10 11:08:19 -08001669 function updateHostState(hostData) {
1670 updateHostLabel(hostData);
Simon Huntbb282f52014-11-10 11:08:19 -08001671 }
1672
Simon Hunt434cf142014-11-24 11:10:28 -08001673 function updateHostVisibility() {
1674 var v = visVal(showHosts);
1675 nodeG.selectAll('.host').style('visibility', v);
1676 linkG.selectAll('.hostLink').style('visibility', v);
1677 }
1678
Simon Hunt6ac93f32014-11-13 12:17:27 -08001679 function nodeMouseOver(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001680 hovered = d;
Thomas Vachuska9edca302014-11-22 17:06:42 -08001681 requestTrafficForMode();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001682 }
1683
1684 function nodeMouseOut(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001685 hovered = null;
Thomas Vachuska9edca302014-11-22 17:06:42 -08001686 requestTrafficForMode();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001687 }
Simon Huntbb282f52014-11-10 11:08:19 -08001688
Simon Hunteb1514d2014-11-20 09:57:29 -08001689 function addHostIcon(node, radius, iid) {
Thomas Vachuska89543292014-11-19 11:28:33 -08001690 var dim = radius * 1.5,
1691 xlate = -dim / 2;
1692
Simon Hunteb1514d2014-11-20 09:57:29 -08001693 node.append('use').attr({
1694 class: 'glyphIcon hostIcon',
1695 transform: translate(xlate,xlate),
1696 'xlink:href': iid,
1697 width: dim,
1698 height: dim
1699 });
Thomas Vachuska89543292014-11-19 11:28:33 -08001700 }
1701
Simon Hunt99c13842014-11-06 18:23:12 -08001702 function updateNodes() {
1703 node = nodeG.selectAll('.node')
1704 .data(network.nodes, function (d) { return d.id; });
1705
Simon Hunt62c47542014-11-22 22:16:32 -08001706 // operate on existing nodes...
1707 node.filter('.device').each(function (d) {
Simon Hunt62c47542014-11-22 22:16:32 -08001708 var node = d.el;
1709 node.classed('online', d.online);
1710 updateDeviceLabel(d);
1711 positionNode(d, true);
1712 });
1713
1714 node.filter('.host').each(function (d) {
Simon Hunt62c47542014-11-22 22:16:32 -08001715 var node = d.el;
1716 // TODO: appropriate update of host visuals
1717 });
Simon Hunt99c13842014-11-06 18:23:12 -08001718
1719 // operate on entering nodes:
1720 var entering = node.enter()
1721 .append('g')
1722 .attr({
1723 id: function (d) { return safeId(d.id); },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001724 class: mkSvgClass,
Simon Hunt99c13842014-11-06 18:23:12 -08001725 transform: function (d) { return translate(d.x, d.y); },
1726 opacity: 0
1727 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08001728 .call(network.drag)
Simon Hunt6ac93f32014-11-13 12:17:27 -08001729 .on('mouseover', nodeMouseOver)
1730 .on('mouseout', nodeMouseOut)
Simon Hunt99c13842014-11-06 18:23:12 -08001731 .transition()
1732 .attr('opacity', 1);
1733
1734 // augment device nodes...
1735 entering.filter('.device').each(function (d) {
1736 var node = d3.select(this),
Simon Huntc72967b2014-11-20 09:21:42 -08001737 label = trimLabel(deviceLabel(d)),
1738 noLabel = !label,
Simon Hunt99c13842014-11-06 18:23:12 -08001739 box;
1740
Simon Hunt7cd48f32014-11-09 23:42:50 -08001741 // provide ref to element from backing data....
1742 d.el = node;
1743
Simon Hunt62c47542014-11-22 22:16:32 -08001744 node.append('rect').attr({ rx: 5, ry: 5 });
1745 node.append('text').text(label).attr('dy', '1.1em');
Simon Hunt99c13842014-11-06 18:23:12 -08001746 box = adjustRectToFitText(node);
Simon Hunta3dd9572014-11-20 15:22:41 -08001747 node.select('rect').attr(box);
Simon Huntc72967b2014-11-20 09:21:42 -08001748 addDeviceIcon(node, box, noLabel, iconGlyphUrl(d));
Simon Huntc7ee0662014-11-05 16:44:37 -08001749 });
Simon Hunt934c3ce2014-11-05 11:45:07 -08001750
Thomas Vachuska89543292014-11-19 11:28:33 -08001751
Simon Hunt56d51852014-11-09 13:03:35 -08001752 // augment host nodes...
1753 entering.filter('.host').each(function (d) {
1754 var node = d3.select(this),
Simon Hunt395a70c2014-11-22 23:17:40 -08001755 cfg = config.icons.host,
1756 r = cfg.radius[d.type] || cfg.defaultRadius,
Thomas Vachuska89543292014-11-19 11:28:33 -08001757 textDy = r + 10,
Simon Hunteb1514d2014-11-20 09:57:29 -08001758 iid = iconGlyphUrl(d);
Simon Hunt56d51852014-11-09 13:03:35 -08001759
Simon Hunt7cd48f32014-11-09 23:42:50 -08001760 // provide ref to element from backing data....
1761 d.el = node;
Simon Hunt434cf142014-11-24 11:10:28 -08001762 showHostVis(node);
Simon Hunt7cd48f32014-11-09 23:42:50 -08001763
Simon Hunt62c47542014-11-22 22:16:32 -08001764 node.append('circle').attr('r', r);
Simon Hunteb1514d2014-11-20 09:57:29 -08001765 if (iid) {
1766 addHostIcon(node, r, iid);
Simon Hunt7fa116d2014-11-17 14:16:55 -08001767 }
Simon Hunt56d51852014-11-09 13:03:35 -08001768 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001769 .text(hostLabel)
Thomas Vachuska89543292014-11-19 11:28:33 -08001770 .attr('dy', textDy)
Simon Hunt7cd48f32014-11-09 23:42:50 -08001771 .attr('text-anchor', 'middle');
Simon Hunt56d51852014-11-09 13:03:35 -08001772 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001773
Simon Hunt99c13842014-11-06 18:23:12 -08001774 // operate on both existing and new nodes, if necessary
Simon Huntb0ecfa52014-11-23 21:05:12 -08001775 updateDeviceColors();
Simon Huntc7ee0662014-11-05 16:44:37 -08001776
Simon Hunt99c13842014-11-06 18:23:12 -08001777 // operate on exiting nodes:
Simon Huntea80eb42014-11-11 13:46:57 -08001778 // Note that the node is removed after 2 seconds.
1779 // Sub element animations should be shorter than 2 seconds.
1780 var exiting = node.exit()
Simon Hunt44031102014-11-11 13:20:36 -08001781 .transition()
1782 .duration(2000)
Simon Huntea80eb42014-11-11 13:46:57 -08001783 .style('opacity', 0)
Simon Hunt99c13842014-11-06 18:23:12 -08001784 .remove();
Simon Huntea80eb42014-11-11 13:46:57 -08001785
1786 // host node exits....
1787 exiting.filter('.host').each(function (d) {
1788 var node = d3.select(this);
1789
1790 node.select('text')
1791 .style('opacity', 0.5)
1792 .transition()
1793 .duration(1000)
1794 .style('opacity', 0);
1795 // note, leave <g>.remove to remove this element
1796
Thomas Vachuska89543292014-11-19 11:28:33 -08001797 node.select('circle')
1798 .style('stroke-fill', '#555')
1799 .style('fill', '#888')
Simon Huntea80eb42014-11-11 13:46:57 -08001800 .style('opacity', 0.5)
1801 .transition()
1802 .duration(1500)
1803 .attr('r', 0);
1804 // note, leave <g>.remove to remove this element
1805
1806 });
1807
Simon Hunt62c47542014-11-22 22:16:32 -08001808 // TODO: device node exit animation
1809
1810 network.force.resume();
Simon Huntc7ee0662014-11-05 16:44:37 -08001811 }
1812
Simon Hunt95dad922014-11-24 09:43:31 -08001813 var dCol = {
1814 black: '#000',
1815 paleblue: '#acf',
1816 offwhite: '#ddd',
1817 midgrey: '#888',
1818 lightgrey: '#bbb',
1819 orange: '#f90'
1820 };
1821
Simon Huntb0ecfa52014-11-23 21:05:12 -08001822 // note: these are the device icon colors without affinity
Simon Hunt95dad922014-11-24 09:43:31 -08001823 var dColTheme = {
Simon Huntb0ecfa52014-11-23 21:05:12 -08001824 light: {
1825 online: {
Simon Hunt95dad922014-11-24 09:43:31 -08001826 glyph: dCol.black,
1827 rect: dCol.paleblue
Simon Huntb0ecfa52014-11-23 21:05:12 -08001828 },
1829 offline: {
Simon Hunt95dad922014-11-24 09:43:31 -08001830 glyph: dCol.midgrey,
1831 rect: dCol.lightgrey
Simon Huntb0ecfa52014-11-23 21:05:12 -08001832 }
1833 },
Simon Hunt95dad922014-11-24 09:43:31 -08001834 // TODO: theme
Simon Huntb0ecfa52014-11-23 21:05:12 -08001835 dark: {
1836 online: {
Simon Hunt95dad922014-11-24 09:43:31 -08001837 glyph: dCol.black,
1838 rect: dCol.paleblue
Simon Huntb0ecfa52014-11-23 21:05:12 -08001839 },
1840 offline: {
Simon Hunt95dad922014-11-24 09:43:31 -08001841 glyph: dCol.midgrey,
1842 rect: dCol.lightgrey
Simon Huntb0ecfa52014-11-23 21:05:12 -08001843 }
1844 }
1845 };
1846
1847 function devBaseColor(d) {
1848 var t = network.view.getTheme(),
1849 o = d.online ? 'online' : 'offline';
Simon Hunt95dad922014-11-24 09:43:31 -08001850 return dColTheme[t][o];
Simon Huntb0ecfa52014-11-23 21:05:12 -08001851 }
1852
1853 function setDeviceColor(d) {
1854 var o = d.online,
1855 s = d.el.classed('selected'),
1856 c = devBaseColor(d),
1857 a = instColor(d.master, o),
1858 g, r,
1859 icon = d.el.select('g.deviceIcon');
1860
1861 if (s) {
1862 g = c.glyph;
Simon Hunt95dad922014-11-24 09:43:31 -08001863 r = dColTheme.sel;
Simon Huntb0ecfa52014-11-23 21:05:12 -08001864 } else if (colorAffinity) {
1865 g = o ? a : c.glyph;
Simon Hunt95dad922014-11-24 09:43:31 -08001866 r = o ? dCol.offwhite : a;
Simon Huntb0ecfa52014-11-23 21:05:12 -08001867 } else {
1868 g = c.glyph;
1869 r = c.rect;
1870 }
1871
1872 icon.select('use')
1873 .style('fill', g);
1874 icon.select('rect')
1875 .style('fill', r);
1876 }
1877
Simon Huntc72967b2014-11-20 09:21:42 -08001878 function addDeviceIcon(node, box, noLabel, iid) {
1879 var cfg = config.icons.device,
1880 dx,
1881 dy,
1882 g;
1883
1884 if (noLabel) {
1885 box = emptyBox();
1886 dx = -cfg.dim/2;
1887 dy = -cfg.dim/2;
1888 } else {
1889 box = adjustRectToFitText(node);
Simon Hunt395a70c2014-11-22 23:17:40 -08001890 dx = box.x + cfg.xoff;
1891 dy = box.y + cfg.yoff;
Simon Huntc72967b2014-11-20 09:21:42 -08001892 }
1893
Simon Hunteb1514d2014-11-20 09:57:29 -08001894 g = node.append('g')
1895 .attr('class', 'glyphIcon deviceIcon')
Simon Huntc72967b2014-11-20 09:21:42 -08001896 .attr('transform', translate(dx, dy));
1897
1898 g.append('rect').attr({
1899 x: 0,
1900 y: 0,
1901 rx: cfg.rx,
1902 width: cfg.dim,
1903 height: cfg.dim
1904 });
1905
1906 g.append('use').attr({
1907 'xlink:href': iid,
1908 width: cfg.dim,
1909 height: cfg.dim
1910 });
1911
Simon Huntc72967b2014-11-20 09:21:42 -08001912 }
1913
Simon Hunt7b403bc2014-11-22 19:01:00 -08001914 function find(key, array, tag) {
1915 var _tag = tag || 'key',
1916 idx, n, d;
1917 for (idx = 0, n = array.length; idx < n; idx++) {
1918 d = array[idx];
1919 if (d[_tag] === key) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001920 return idx;
1921 }
1922 }
1923 return -1;
1924 }
1925
1926 function removeLinkElement(linkData) {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001927 var idx = find(linkData.key, network.links),
1928 removed;
1929 if (idx >=0) {
1930 // remove from links array
1931 removed = network.links.splice(idx, 1);
1932 // remove from lookup cache
1933 delete network.lookup[removed[0].key];
1934 updateLinks();
1935 network.force.resume();
1936 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001937 }
Simon Huntc7ee0662014-11-05 16:44:37 -08001938
Simon Hunt44031102014-11-11 13:20:36 -08001939 function removeHostElement(hostData) {
1940 // first, remove associated hostLink...
1941 removeLinkElement(hostData.linkData);
1942
1943 // remove from lookup cache
1944 delete network.lookup[hostData.id];
1945 // remove from nodes array
1946 var idx = find(hostData.id, network.nodes);
1947 network.nodes.splice(idx, 1);
1948 // remove from SVG
1949 updateNodes();
1950 network.force.resume();
1951 }
1952
1953
Simon Huntc7ee0662014-11-05 16:44:37 -08001954 function tick() {
1955 node.attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001956 transform: function (d) { return translate(d.x, d.y); }
Simon Huntc7ee0662014-11-05 16:44:37 -08001957 });
1958
1959 link.attr({
1960 x1: function (d) { return d.source.x; },
1961 y1: function (d) { return d.source.y; },
1962 x2: function (d) { return d.target.x; },
1963 y2: function (d) { return d.target.y; }
1964 });
Simon Hunte2575b62014-11-18 15:25:53 -08001965
1966 linkLabel.each(function (d) {
1967 var el = d3.select(this);
Thomas Vachuska4731f122014-11-20 04:56:19 -08001968 var lnk = findLinkById(d.key);
1969
1970 if (lnk) {
1971 var parms = {
Simon Hunte2575b62014-11-18 15:25:53 -08001972 x1: lnk.source.x,
1973 y1: lnk.source.y,
1974 x2: lnk.target.x,
1975 y2: lnk.target.y
1976 };
Thomas Vachuska4731f122014-11-20 04:56:19 -08001977 el.attr('transform', transformLabel(parms));
1978 }
Simon Hunte2575b62014-11-18 15:25:53 -08001979 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001980 }
Simon Hunt934c3ce2014-11-05 11:45:07 -08001981
1982 // ==============================
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001983 // Web-Socket for live data
1984
1985 function webSockUrl() {
1986 return document.location.toString()
1987 .replace(/\#.*/, '')
1988 .replace('http://', 'ws://')
1989 .replace('https://', 'wss://')
Simon Hunte5ab1382014-11-25 10:28:51 -08001990 .replace('index.html', config.webSockUrl);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001991 }
1992
1993 webSock = {
1994 ws : null,
1995
1996 connect : function() {
1997 webSock.ws = new WebSocket(webSockUrl());
1998
1999 webSock.ws.onopen = function() {
Simon Hunt0c6d4192014-11-12 12:07:10 -08002000 noWebSock(false);
Thomas Vachuska47635c62014-11-22 01:21:36 -08002001 requestSummary();
Simon Huntb0ecfa52014-11-23 21:05:12 -08002002 showInstances();
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002003 };
2004
2005 webSock.ws.onmessage = function(m) {
2006 if (m.data) {
Simon Huntbb282f52014-11-10 11:08:19 -08002007 wsTraceRx(m.data);
Thomas Vachuskad472c6e2014-11-07 19:11:05 -08002008 handleServerEvent(JSON.parse(m.data));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002009 }
2010 };
2011
2012 webSock.ws.onclose = function(m) {
2013 webSock.ws = null;
Simon Hunt0c6d4192014-11-12 12:07:10 -08002014 noWebSock(true);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002015 };
2016 },
2017
2018 send : function(text) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002019 if (text != null) {
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002020 webSock._send(text);
2021 }
2022 },
2023
2024 _send : function(message) {
2025 if (webSock.ws) {
2026 webSock.ws.send(message);
Simon Hunta255a2c2014-11-13 22:29:35 -08002027 } else if (config.useLiveData) {
Simon Hunt434cf142014-11-24 11:10:28 -08002028 console.warn('no web socket open', message);
Simon Hunta255a2c2014-11-13 22:29:35 -08002029 } else {
Simon Hunt434cf142014-11-24 11:10:28 -08002030 console.log('WS Send: ', message);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002031 }
2032 }
2033
2034 };
2035
Simon Hunt0c6d4192014-11-12 12:07:10 -08002036 function noWebSock(b) {
2037 mask.style('display',b ? 'block' : 'none');
2038 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002039
2040 function sendMessage(evType, payload) {
2041 var toSend = {
Simon Huntbb282f52014-11-10 11:08:19 -08002042 event: evType,
2043 sid: ++sid,
2044 payload: payload
2045 },
2046 asText = JSON.stringify(toSend);
2047 wsTraceTx(asText);
2048 webSock.send(asText);
Simon Huntc76ae892014-11-18 17:31:51 -08002049
2050 // Temporary measure for debugging UI behavior ...
2051 if (!config.useLiveData) {
2052 handleTestSend(toSend);
2053 }
Simon Huntbb282f52014-11-10 11:08:19 -08002054 }
2055
2056 function wsTraceTx(msg) {
2057 wsTrace('tx', msg);
2058 }
2059 function wsTraceRx(msg) {
2060 wsTrace('rx', msg);
2061 }
2062 function wsTrace(rxtx, msg) {
Simon Huntbb282f52014-11-10 11:08:19 -08002063 console.log('[' + rxtx + '] ' + msg);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002064 }
2065
Simon Huntc76ae892014-11-18 17:31:51 -08002066 // NOTE: Temporary hardcoded example for showing detail pane
2067 // while we fine-
2068 // Probably should not merge this change...
2069 function handleTestSend(msg) {
2070 if (msg.event === 'requestDetails') {
2071 showDetails({
2072 event: 'showDetails',
2073 sid: 1001,
2074 payload: {
2075 "id": "of:0000ffffffffff09",
2076 "type": "roadm",
2077 "propOrder": [
2078 "Name",
2079 "Vendor",
2080 "H/W Version",
2081 "S/W Version",
2082 "-",
2083 "Latitude",
2084 "Longitude",
2085 "Ports"
2086 ],
2087 "props": {
2088 "Name": null,
2089 "Vendor": "Linc",
2090 "H/W Version": "OE",
2091 "S/W Version": "?",
2092 "-": "",
2093 "Latitude": "40.8",
2094 "Longitude": "73.1",
2095 "Ports": "2"
2096 }
2097 }
2098 });
2099 }
2100 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002101
2102 // ==============================
2103 // Selection stuff
2104
2105 function selectObject(obj, el) {
2106 var n,
Simon Hunt01095ff2014-11-13 16:37:29 -08002107 srcEv = d3.event.sourceEvent,
2108 meta = srcEv.metaKey,
2109 shift = srcEv.shiftKey;
2110
Simon Huntdeab4322014-11-13 18:49:07 -08002111 if ((panZoom() && !meta) || (!panZoom() && meta)) {
Simon Hunt01095ff2014-11-13 16:37:29 -08002112 return;
2113 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002114
2115 if (el) {
2116 n = d3.select(el);
2117 } else {
2118 node.each(function(d) {
2119 if (d == obj) {
2120 n = d3.select(el = this);
2121 }
2122 });
2123 }
2124 if (!n) return;
2125
Simon Hunt01095ff2014-11-13 16:37:29 -08002126 if (shift && n.classed('selected')) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002127 deselectObject(obj.id);
Simon Hunt61d04042014-11-11 17:27:16 -08002128 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002129 return;
2130 }
2131
Simon Hunt01095ff2014-11-13 16:37:29 -08002132 if (!shift) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002133 deselectAll();
2134 }
2135
Simon Huntc31d5692014-11-12 13:27:18 -08002136 selections[obj.id] = { obj: obj, el: el };
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002137 selectOrder.push(obj.id);
2138
2139 n.classed('selected', true);
Simon Huntb0ecfa52014-11-23 21:05:12 -08002140 updateDeviceColors(obj);
Simon Hunt61d04042014-11-11 17:27:16 -08002141 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002142 }
2143
2144 function deselectObject(id) {
Simon Huntc31d5692014-11-12 13:27:18 -08002145 var obj = selections[id],
2146 idx;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002147 if (obj) {
2148 d3.select(obj.el).classed('selected', false);
Simon Hunt61d04042014-11-11 17:27:16 -08002149 delete selections[id];
Simon Huntc31d5692014-11-12 13:27:18 -08002150 idx = $.inArray(id, selectOrder);
2151 if (idx >= 0) {
2152 selectOrder.splice(idx, 1);
2153 }
Simon Huntb0ecfa52014-11-23 21:05:12 -08002154 updateDeviceColors(obj.obj);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002155 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002156 }
2157
2158 function deselectAll() {
2159 // deselect all nodes in the network...
2160 node.classed('selected', false);
2161 selections = {};
2162 selectOrder = [];
Simon Huntb0ecfa52014-11-23 21:05:12 -08002163 updateDeviceColors();
Simon Hunt61d04042014-11-11 17:27:16 -08002164 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002165 }
2166
Simon Huntb0ecfa52014-11-23 21:05:12 -08002167 function updateDeviceColors(d) {
2168 if (d) {
2169 setDeviceColor(d);
2170 } else {
2171 node.filter('.device').each(function (d) {
2172 setDeviceColor(d);
2173 });
2174 }
Thomas Vachuska47635c62014-11-22 01:21:36 -08002175 }
2176
Simon Hunt61d04042014-11-11 17:27:16 -08002177 // update the state of the detail pane, based on current selections
2178 function updateDetailPane() {
2179 var nSel = selectOrder.length;
2180 if (!nSel) {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002181 emptySelect();
Simon Hunt61d04042014-11-11 17:27:16 -08002182 } else if (nSel === 1) {
2183 singleSelect();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002184 requestTrafficForMode();
Simon Hunt61d04042014-11-11 17:27:16 -08002185 } else {
2186 multiSelect();
2187 }
2188 }
2189
Thomas Vachuska9edca302014-11-22 17:06:42 -08002190 function emptySelect() {
2191 detailPane.hide();
2192 cancelTraffic();
2193 }
2194
Simon Hunt61d04042014-11-11 17:27:16 -08002195 function singleSelect() {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002196 // NOTE: detail is shown from showDetails event callback
Simon Hunt61d04042014-11-11 17:27:16 -08002197 requestDetails();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002198 requestTrafficForMode();
Simon Hunt61d04042014-11-11 17:27:16 -08002199 }
2200
2201 function multiSelect() {
Simon Huntb53e0682014-11-12 13:32:01 -08002202 populateMultiSelect();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002203 requestTrafficForMode();
Simon Huntb53e0682014-11-12 13:32:01 -08002204 }
2205
2206 function addSep(tbody) {
2207 var tr = tbody.append('tr');
2208 $('<hr>').appendTo(tr.append('td').attr('colspan', 2));
2209 }
2210
2211 function addProp(tbody, label, value) {
2212 var tr = tbody.append('tr');
2213
2214 tr.append('td')
2215 .attr('class', 'label')
2216 .text(label + ' :');
2217
2218 tr.append('td')
2219 .attr('class', 'value')
2220 .text(value);
2221 }
2222
2223 function populateMultiSelect() {
2224 detailPane.empty();
2225
Simon Hunta3dd9572014-11-20 15:22:41 -08002226 var title = detailPane.append('h3'),
2227 table = detailPane.append('table'),
2228 tbody = table.append('tbody');
Simon Huntb53e0682014-11-12 13:32:01 -08002229
Thomas Vachuska4731f122014-11-20 04:56:19 -08002230 title.text('Selected Nodes');
Simon Huntb53e0682014-11-12 13:32:01 -08002231
2232 selectOrder.forEach(function (d, i) {
2233 addProp(tbody, i+1, d);
2234 });
Simon Huntd72bc702014-11-13 18:38:04 -08002235
2236 addMultiSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08002237 }
2238
Thomas Vachuska47635c62014-11-22 01:21:36 -08002239 // TODO: refactor to consolidate with populateDetails
2240 function populateSummary(data) {
2241 summaryPane.empty();
2242
2243 var svg = summaryPane.append('svg'),
2244 iid = iconGlyphUrl(data);
2245
2246 var title = summaryPane.append('h2'),
2247 table = summaryPane.append('table'),
2248 tbody = table.append('tbody');
2249
2250 appendGlyph(svg, 0, 0, 40, iid);
2251
2252 svg.append('use')
2253 .attr({
2254 class: 'birdBadge',
2255 transform: translate(8,12),
2256 'xlink:href': '#bird',
2257 width: 24,
2258 height: 24,
2259 fill: '#fff'
2260 });
2261
2262 title.text('ONOS Summary');
2263
2264 data.propOrder.forEach(function(p) {
2265 if (p === '-') {
2266 addSep(tbody);
2267 } else {
2268 addProp(tbody, p, data.props[p]);
2269 }
2270 });
2271 }
2272
Simon Hunt61d04042014-11-11 17:27:16 -08002273 function populateDetails(data) {
2274 detailPane.empty();
2275
Simon Hunta6a9fe72014-11-20 11:17:12 -08002276 var svg = detailPane.append('svg'),
2277 iid = iconGlyphUrl(data);
2278
Simon Hunta3dd9572014-11-20 15:22:41 -08002279 var title = detailPane.append('h2'),
2280 table = detailPane.append('table'),
2281 tbody = table.append('tbody');
Simon Hunt61d04042014-11-11 17:27:16 -08002282
Simon Hunta6a9fe72014-11-20 11:17:12 -08002283 appendGlyph(svg, 0, 0, 40, iid);
2284 title.text(data.id);
Simon Hunt61d04042014-11-11 17:27:16 -08002285
2286 data.propOrder.forEach(function(p) {
2287 if (p === '-') {
2288 addSep(tbody);
2289 } else {
2290 addProp(tbody, p, data.props[p]);
2291 }
2292 });
Simon Huntd72bc702014-11-13 18:38:04 -08002293
Thomas Vachuska4731f122014-11-20 04:56:19 -08002294 addSingleSelectActions(data);
Simon Hunt61d04042014-11-11 17:27:16 -08002295 }
2296
Thomas Vachuska4731f122014-11-20 04:56:19 -08002297 function addSingleSelectActions(data) {
Simon Huntd72bc702014-11-13 18:38:04 -08002298 detailPane.append('hr');
2299 // always want to allow 'show traffic'
Thomas Vachuska4731f122014-11-20 04:56:19 -08002300 addAction(detailPane, 'Show Related Traffic', showTrafficAction);
2301
2302 if (data.type === 'switch') {
2303 addAction(detailPane, 'Show Device Flows', showDeviceLinkFlowsAction);
2304 }
Simon Huntd72bc702014-11-13 18:38:04 -08002305 }
2306
2307 function addMultiSelectActions() {
2308 detailPane.append('hr');
2309 // always want to allow 'show traffic'
Thomas Vachuska4731f122014-11-20 04:56:19 -08002310 addAction(detailPane, 'Show Related Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08002311 // if exactly two hosts are selected, also want 'add host intent'
2312 if (nSel() === 2 && allSelectionsClass('host')) {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002313 addAction(detailPane, 'Create Host-to-Host Flow', addHostIntentAction);
2314 } else if (nSel() >= 2 && allSelectionsClass('host')) {
2315 addAction(detailPane, 'Create Multi-Source Flow', addMultiSourceIntentAction);
Simon Huntd72bc702014-11-13 18:38:04 -08002316 }
2317 }
2318
Simon Hunta5e89142014-11-14 07:00:33 -08002319 function addAction(panel, text, cb) {
2320 panel.append('div')
Simon Huntd72bc702014-11-13 18:38:04 -08002321 .classed('actionBtn', true)
2322 .text(text)
2323 .on('click', cb);
2324 }
2325
2326
Paul Greysonfcba0e82014-11-13 10:21:16 -08002327 function zoomPan(scale, translate) {
2328 zoomPanContainer.attr("transform", "translate(" + translate + ")scale(" + scale + ")");
2329 // keep the map lines constant width while zooming
Thomas Vachuska89543292014-11-19 11:28:33 -08002330 bgImg.style("stroke-width", 2.0 / scale + "px");
Paul Greysonfcba0e82014-11-13 10:21:16 -08002331 }
2332
2333 function resetZoomPan() {
2334 zoomPan(1, [0,0]);
2335 zoom.scale(1).translate([0,0]);
2336 }
2337
2338 function setupZoomPan() {
2339 function zoomed() {
Simon Huntdeab4322014-11-13 18:49:07 -08002340 if (!panZoom() ^ !d3.event.sourceEvent.metaKey) {
Paul Greysonfcba0e82014-11-13 10:21:16 -08002341 zoomPan(d3.event.scale, d3.event.translate);
2342 }
2343 }
2344
2345 zoom = d3.behavior.zoom()
2346 .translate([0, 0])
2347 .scale(1)
2348 .scaleExtent([1, 8])
2349 .on("zoom", zoomed);
2350
2351 svg.call(zoom);
2352 }
2353
Simon Hunt61d04042014-11-11 17:27:16 -08002354 // ==============================
2355 // Test harness code
Simon Hunt56d51852014-11-09 13:03:35 -08002356
2357 function prepareScenario(view, ctx, dbg) {
2358 var sc = scenario,
2359 urlSc = sc.evDir + ctx + sc.evScenario;
2360
2361 if (!ctx) {
2362 view.alert("No scenario specified (null ctx)");
2363 return;
2364 }
2365
2366 sc.view = view;
2367 sc.ctx = ctx;
2368 sc.debug = dbg;
2369 sc.evNumber = 0;
2370
2371 d3.json(urlSc, function(err, data) {
Simon Huntbb282f52014-11-10 11:08:19 -08002372 var p = data && data.params || {},
2373 desc = data && data.description || null,
Simon Huntfc274c92014-11-11 11:05:46 -08002374 intro = data && data.title;
Simon Huntbb282f52014-11-10 11:08:19 -08002375
Simon Hunt56d51852014-11-09 13:03:35 -08002376 if (err) {
2377 view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err);
2378 } else {
2379 sc.params = p;
Simon Huntbb282f52014-11-10 11:08:19 -08002380 if (desc) {
2381 intro += '\n\n ' + desc.join('\n ');
2382 }
2383 view.alert(intro);
Simon Hunt56d51852014-11-09 13:03:35 -08002384 }
2385 });
2386
2387 }
2388
Simon Hunt01095ff2014-11-13 16:37:29 -08002389 // ==============================
2390 // Toggle Buttons in masthead
Simon Hunt0c6d4192014-11-12 12:07:10 -08002391
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002392 // TODO: toggle button (and other widgets in the masthead) should be provided
2393 // by the framework; not generated by the view.
2394
Thomas Vachuska47635c62014-11-22 01:21:36 -08002395 //var showInstances;
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002396
Simon Hunt87514342014-11-24 16:41:27 -08002397/*
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002398 function addButtonBar(view) {
2399 var bb = d3.select('#mast')
2400 .append('span').classed('right', true).attr('id', 'bb');
2401
Simon Hunta5e89142014-11-14 07:00:33 -08002402 function mkTogBtn(text, cb) {
2403 return bb.append('span')
2404 .classed('btn', true)
2405 .text(text)
2406 .on('click', cb);
2407 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002408
Thomas Vachuska47635c62014-11-22 01:21:36 -08002409 //showInstances = mkTogBtn('Show Instances', toggleInst);
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002410 }
Simon Hunt87514342014-11-24 16:41:27 -08002411*/
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002412
Simon Huntdeab4322014-11-13 18:49:07 -08002413 function panZoom() {
Simon Hunte5b71752014-11-18 20:06:07 -08002414 return false;
Simon Hunta5e89142014-11-14 07:00:33 -08002415 }
2416
Simon Hunt7fa116d2014-11-17 14:16:55 -08002417 function loadGlyphs(svg) {
2418 var defs = svg.append('defs');
2419 gly.defBird(defs);
Simon Huntc72967b2014-11-20 09:21:42 -08002420 gly.defGlyphs(defs);
Simon Huntb82f6902014-11-22 11:53:15 -08002421 gly.defBadges(defs);
Simon Hunt7fa116d2014-11-17 14:16:55 -08002422 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002423
Simon Hunt395a70c2014-11-22 23:17:40 -08002424 function sendUpdateMeta(d, store) {
2425 var metaUi = {},
2426 ll;
2427
2428 if (store) {
2429 ll = geoMapProjection.invert([d.x, d.y]);
Simon Hunt62c47542014-11-22 22:16:32 -08002430 metaUi = {
2431 x: d.x,
2432 y: d.y,
2433 lng: ll[0],
2434 lat: ll[1]
2435 };
Simon Hunt395a70c2014-11-22 23:17:40 -08002436 }
Simon Hunt62c47542014-11-22 22:16:32 -08002437 d.metaUi = metaUi;
2438 sendMessage('updateMeta', {
2439 id: d.id,
2440 'class': d.class,
2441 'memento': metaUi
2442 });
2443 }
2444
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002445 // ==============================
Simon Hunt142d0032014-11-04 20:13:09 -08002446 // View life-cycle callbacks
Simon Hunt195cb382014-11-03 17:50:51 -08002447
Simon Huntf67722a2014-11-10 09:32:06 -08002448 function preload(view, ctx, flags) {
Simon Hunt142d0032014-11-04 20:13:09 -08002449 var w = view.width(),
2450 h = view.height(),
Simon Huntc7ee0662014-11-05 16:44:37 -08002451 fcfg = config.force,
2452 fpad = fcfg.pad,
2453 forceDim = [w - 2*fpad, h - 2*fpad];
Simon Hunt195cb382014-11-03 17:50:51 -08002454
Simon Hunt142d0032014-11-04 20:13:09 -08002455 // NOTE: view.$div is a D3 selection of the view's div
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002456 var viewBox = '0 0 ' + config.logicalSize + ' ' + config.logicalSize;
2457 svg = view.$div.append('svg').attr('viewBox', viewBox);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002458 setSize(svg, view);
2459
Simon Hunt7fa116d2014-11-17 14:16:55 -08002460 loadGlyphs(svg);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002461
Paul Greysonfcba0e82014-11-13 10:21:16 -08002462 zoomPanContainer = svg.append('g').attr('id', 'zoomPanContainer');
Paul Greysonfcba0e82014-11-13 10:21:16 -08002463 setupZoomPan();
2464
Simon Hunt1a9eff92014-11-07 11:06:34 -08002465 // add blue glow filter to svg layer
Paul Greysonfcba0e82014-11-13 10:21:16 -08002466 d3u.appendGlow(zoomPanContainer);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002467
Simon Huntc7ee0662014-11-05 16:44:37 -08002468 // group for the topology
Paul Greysonfcba0e82014-11-13 10:21:16 -08002469 topoG = zoomPanContainer.append('g')
Simon Huntd3b7d512014-11-12 15:48:41 -08002470 .attr('id', 'topo-G')
Simon Huntc7ee0662014-11-05 16:44:37 -08002471 .attr('transform', fcfg.translate());
2472
Simon Hunte2575b62014-11-18 15:25:53 -08002473 // subgroups for links, link labels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002474 linkG = topoG.append('g').attr('id', 'links');
Simon Hunte2575b62014-11-18 15:25:53 -08002475 linkLabelG = topoG.append('g').attr('id', 'linkLabels');
Simon Huntc7ee0662014-11-05 16:44:37 -08002476 nodeG = topoG.append('g').attr('id', 'nodes');
2477
Simon Hunte2575b62014-11-18 15:25:53 -08002478 // selection of links, linkLabels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002479 link = linkG.selectAll('.link');
Simon Hunte2575b62014-11-18 15:25:53 -08002480 linkLabel = linkLabelG.selectAll('.linkLabel');
Simon Huntc7ee0662014-11-05 16:44:37 -08002481 node = nodeG.selectAll('.node');
2482
Simon Hunt7cd48f32014-11-09 23:42:50 -08002483 function chrg(d) {
2484 return fcfg.charge[d.class] || -12000;
2485 }
Simon Hunt99c13842014-11-06 18:23:12 -08002486 function ldist(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002487 return fcfg.linkDistance[d.type] || 50;
Simon Hunt99c13842014-11-06 18:23:12 -08002488 }
2489 function lstrg(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002490 // 0.0 - 1.0
2491 return fcfg.linkStrength[d.type] || 1.0;
Simon Hunt99c13842014-11-06 18:23:12 -08002492 }
2493
Simon Hunt1a9eff92014-11-07 11:06:34 -08002494 function selectCb(d, self) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002495 selectObject(d, self);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002496 }
2497
2498 function atDragEnd(d, self) {
Simon Hunt56d51852014-11-09 13:03:35 -08002499 // once we've finished moving, pin the node in position
2500 d.fixed = true;
2501 d3.select(self).classed('fixed', true);
2502 if (config.useLiveData) {
Simon Hunt395a70c2014-11-22 23:17:40 -08002503 sendUpdateMeta(d, true);
Simon Hunta255a2c2014-11-13 22:29:35 -08002504 } else {
2505 console.log('Moving node ' + d.id + ' to [' + d.x + ',' + d.y + ']');
Simon Hunt1a9eff92014-11-07 11:06:34 -08002506 }
2507 }
2508
Simon Huntc7ee0662014-11-05 16:44:37 -08002509 // set up the force layout
2510 network.force = d3.layout.force()
2511 .size(forceDim)
2512 .nodes(network.nodes)
2513 .links(network.links)
Simon Hunt7cd48f32014-11-09 23:42:50 -08002514 .gravity(0.4)
2515 .friction(0.7)
2516 .charge(chrg)
Simon Hunt99c13842014-11-06 18:23:12 -08002517 .linkDistance(ldist)
2518 .linkStrength(lstrg)
Simon Huntc7ee0662014-11-05 16:44:37 -08002519 .on('tick', tick);
Simon Hunt195cb382014-11-03 17:50:51 -08002520
Simon Hunt01095ff2014-11-13 16:37:29 -08002521 network.drag = d3u.createDragBehavior(network.force,
Simon Huntdeab4322014-11-13 18:49:07 -08002522 selectCb, atDragEnd, panZoom);
Simon Hunt0c6d4192014-11-12 12:07:10 -08002523
2524 // create mask layer for when we lose connection to server.
Simon Hunta5e89142014-11-14 07:00:33 -08002525 // TODO: this should be part of the framework
Simon Hunt0c6d4192014-11-12 12:07:10 -08002526 mask = view.$div.append('div').attr('id','topo-mask');
2527 para(mask, 'Oops!');
2528 para(mask, 'Web-socket connection to server closed...');
2529 para(mask, 'Try refreshing the page.');
Simon Hunt12ce12e2014-11-15 21:13:19 -08002530
2531 mask.append('svg')
2532 .attr({
2533 id: 'mask-bird',
2534 width: w,
2535 height: h
2536 })
2537 .append('g')
2538 .attr('transform', birdTranslate(w, h))
2539 .style('opacity', 0.3)
2540 .append('use')
2541 .attr({
2542 'xlink:href': '#bird',
2543 width: config.birdDim,
2544 height: config.birdDim,
2545 fill: '#111'
Thomas Vachuska89543292014-11-19 11:28:33 -08002546 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08002547 }
Simon Hunt195cb382014-11-03 17:50:51 -08002548
Simon Hunt01095ff2014-11-13 16:37:29 -08002549 function para(sel, text) {
2550 sel.append('p').text(text);
2551 }
2552
2553
Simon Hunt56d51852014-11-09 13:03:35 -08002554 function load(view, ctx, flags) {
Simon Huntf67722a2014-11-10 09:32:06 -08002555 // resize, in case the window was resized while we were not loaded
2556 resize(view, ctx, flags);
2557
Simon Hunt99c13842014-11-06 18:23:12 -08002558 // cache the view token, so network topo functions can access it
2559 network.view = view;
Simon Hunt56d51852014-11-09 13:03:35 -08002560 config.useLiveData = !flags.local;
2561
2562 if (!config.useLiveData) {
2563 prepareScenario(view, ctx, flags.debug);
2564 }
Simon Hunt99c13842014-11-06 18:23:12 -08002565
2566 // set our radio buttons and key bindings
Simon Hunt9462e8c2014-11-14 17:28:09 -08002567 layerBtnSet = view.setRadio(layerButtons);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002568 view.setKeys(keyDispatch);
Simon Hunt87514342014-11-24 16:41:27 -08002569 view.setGestures(gestures);
Simon Hunt195cb382014-11-03 17:50:51 -08002570
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002571 // patch in our "button bar" for now
2572 // TODO: implement a more official frameworky way of doing this..
Simon Hunt87514342014-11-24 16:41:27 -08002573 //addButtonBar(view);
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002574
Simon Huntd3b7d512014-11-12 15:48:41 -08002575 // Load map data asynchronously; complete startup after that..
2576 loadGeoJsonData();
Simon Hunta255a2c2014-11-13 22:29:35 -08002577 }
2578
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002579 function startAntTimer() {
Thomas Vachuska4731f122014-11-20 04:56:19 -08002580 if (!antTimer) {
2581 var pulses = [5, 3, 1.2, 3],
2582 pulse = 0;
2583 antTimer = setInterval(function () {
2584 pulse = pulse + 1;
2585 pulse = pulse === pulses.length ? 0 : pulse;
2586 d3.selectAll('.animated').style('stroke-width', pulses[pulse]);
2587 }, 200);
2588 }
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002589 }
2590
2591 function stopAntTimer() {
Simon Hunta255a2c2014-11-13 22:29:35 -08002592 if (antTimer) {
2593 clearInterval(antTimer);
2594 antTimer = null;
2595 }
Simon Huntd3b7d512014-11-12 15:48:41 -08002596 }
2597
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002598 function unload(view, ctx, flags) {
2599 stopAntTimer();
2600 }
2601
Simon Huntd3b7d512014-11-12 15:48:41 -08002602 // TODO: move these to config/state portion of script
Simon Hunta6a9fe72014-11-20 11:17:12 -08002603 var geoJsonUrl = 'json/map/continental_us.json',
Simon Huntd3b7d512014-11-12 15:48:41 -08002604 geoJson;
2605
2606 function loadGeoJsonData() {
2607 d3.json(geoJsonUrl, function (err, data) {
2608 if (err) {
2609 // fall back to USA map background
2610 loadStaticMap();
2611 } else {
2612 geoJson = data;
2613 loadGeoMap();
2614 }
2615
2616 // finally, connect to the server...
2617 if (config.useLiveData) {
2618 webSock.connect();
2619 }
2620 });
2621 }
2622
2623 function showBg() {
Simon Hunt434cf142014-11-24 11:10:28 -08002624 return visVal(config.options.showBackground);
Simon Huntd3b7d512014-11-12 15:48:41 -08002625 }
2626
2627 function loadStaticMap() {
2628 fnTrace('loadStaticMap', config.backgroundUrl);
2629 var w = network.view.width(),
2630 h = network.view.height();
2631
2632 // load the background image
2633 bgImg = svg.insert('svg:image', '#topo-G')
2634 .attr({
2635 id: 'topo-bg',
2636 width: w,
2637 height: h,
2638 'xlink:href': config.backgroundUrl
2639 })
2640 .style({
2641 visibility: showBg()
2642 });
2643 }
2644
2645 function loadGeoMap() {
2646 fnTrace('loadGeoMap', geoJsonUrl);
Simon Huntd3b7d512014-11-12 15:48:41 -08002647
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002648 // extracts the topojson data into geocoordinate-based geometry
2649 var topoData = topojson.feature(geoJson, geoJson.objects.states);
Simon Huntd3b7d512014-11-12 15:48:41 -08002650
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002651 // see: http://bl.ocks.org/mbostock/4707858
2652 geoMapProjection = d3.geo.mercator();
2653 var path = d3.geo.path().projection(geoMapProjection);
Simon Huntd3b7d512014-11-12 15:48:41 -08002654
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002655 geoMapProjection
2656 .scale(1)
2657 .translate([0, 0]);
Simon Huntd3b7d512014-11-12 15:48:41 -08002658
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002659 // [[x1,y1],[x2,y2]]
2660 var b = path.bounds(topoData);
Paul Greysonfcba0e82014-11-13 10:21:16 -08002661 // size map to 95% of minimum dimension to fill space
2662 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 -08002663 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 -08002664
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002665 geoMapProjection
2666 .scale(s)
2667 .translate(t);
2668
Paul Greysonfcba0e82014-11-13 10:21:16 -08002669 bgImg = zoomPanContainer.insert("g", '#topo-G');
Thomas Vachuska89543292014-11-19 11:28:33 -08002670 bgImg.attr('id', 'map').selectAll('path')
2671 .data(topoData.features)
2672 .enter()
2673 .append('path')
2674 .attr('d', path);
Simon Hunt195cb382014-11-03 17:50:51 -08002675 }
2676
Simon Huntf67722a2014-11-10 09:32:06 -08002677 function resize(view, ctx, flags) {
Simon Hunt12ce12e2014-11-15 21:13:19 -08002678 var w = view.width(),
2679 h = view.height();
2680
Simon Hunt934c3ce2014-11-05 11:45:07 -08002681 setSize(svg, view);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002682
2683 d3.select('#mask-bird').attr({ width: w, height: h})
2684 .select('g').attr('transform', birdTranslate(w, h));
Simon Hunt142d0032014-11-04 20:13:09 -08002685 }
2686
Simon Hunt8f40cce2014-11-23 15:57:30 -08002687 function theme(view, ctx, flags) {
2688 updateInstances();
Simon Huntb0ecfa52014-11-23 21:05:12 -08002689 updateDeviceColors();
Simon Hunt8f40cce2014-11-23 15:57:30 -08002690 }
2691
Simon Hunt12ce12e2014-11-15 21:13:19 -08002692 function birdTranslate(w, h) {
2693 var bdim = config.birdDim;
2694 return 'translate('+((w-bdim)*.4)+','+((h-bdim)*.1)+')';
2695 }
Simon Hunt142d0032014-11-04 20:13:09 -08002696
2697 // ==============================
2698 // View registration
Simon Hunt195cb382014-11-03 17:50:51 -08002699
Simon Hunt25248912014-11-04 11:25:48 -08002700 onos.ui.addView('topo', {
Simon Hunt142d0032014-11-04 20:13:09 -08002701 preload: preload,
2702 load: load,
Simon Hunta255a2c2014-11-13 22:29:35 -08002703 unload: unload,
Simon Hunt8f40cce2014-11-23 15:57:30 -08002704 resize: resize,
2705 theme: theme
Simon Hunt195cb382014-11-03 17:50:51 -08002706 });
2707
Thomas Vachuska47635c62014-11-22 01:21:36 -08002708 summaryPane = onos.ui.addFloatingPanel('topo-summary');
Simon Hunt61d04042014-11-11 17:27:16 -08002709 detailPane = onos.ui.addFloatingPanel('topo-detail');
Simon Hunta5e89142014-11-14 07:00:33 -08002710 oiBox = onos.ui.addFloatingPanel('topo-oibox', 'TL');
Simon Huntb82f6902014-11-22 11:53:15 -08002711 oiBox.width(20);
Simon Hunt61d04042014-11-11 17:27:16 -08002712
Simon Hunt195cb382014-11-03 17:50:51 -08002713}(ONOS));