blob: 3c2f512cc654e78c531bc00794f0974e24958776 [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'],
146 L: [cycleLabels, 'Cycle Device labels'],
Simon Hunt934c3ce2014-11-05 11:45:07 -0800147 P: togglePorts,
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800148 U: [unpin, 'Unpin node'],
149 R: [resetZoomPan, 'Reset zoom/pan'],
Thomas Vachuska9edca302014-11-22 17:06:42 -0800150 V: [showTrafficAction, 'Show related traffic'],
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800151 A: [showAllTrafficAction, 'Show all traffic'],
152 F: [showDeviceLinkFlowsAction, 'Show device link flows'],
Simon Hunt9462e8c2014-11-14 17:28:09 -0800153 esc: handleEscape
Simon Hunt934c3ce2014-11-05 11:45:07 -0800154 };
Simon Hunt142d0032014-11-04 20:13:09 -0800155
Simon Hunt195cb382014-11-03 17:50:51 -0800156 // state variables
Simon Hunt99c13842014-11-06 18:23:12 -0800157 var network = {
Simon Hunt50128c02014-11-08 13:36:15 -0800158 view: null, // view token reference
Simon Hunt99c13842014-11-06 18:23:12 -0800159 nodes: [],
160 links: [],
Simon Hunt269670f2014-11-17 16:17:43 -0800161 lookup: {},
162 revLinkToKey: {}
Simon Hunt99c13842014-11-06 18:23:12 -0800163 },
Simon Hunt56d51852014-11-09 13:03:35 -0800164 scenario = {
165 evDir: 'json/ev/',
166 evScenario: '/scenario.json',
167 evPrefix: '/ev_',
168 evOnos: '_onos.json',
169 evUi: '_ui.json',
170 ctx: null,
171 params: {},
172 evNumber: 0,
173 view: null,
174 debug: false
175 },
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800176 webSock,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800177 sid = 0,
Thomas Vachuska60d72bf2014-11-21 13:02:00 -0800178 deviceLabelCount = 3,
Simon Hunt209155e2014-11-21 12:16:09 -0800179 hostLabelCount = 2,
Simon Hunt56d51852014-11-09 13:03:35 -0800180 deviceLabelIndex = 0,
181 hostLabelIndex = 0,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800182 selections = {},
Simon Hunta5e89142014-11-14 07:00:33 -0800183 selectOrder = [],
Simon Hunt6ac93f32014-11-13 12:17:27 -0800184 hovered = null,
Thomas Vachuska47635c62014-11-22 01:21:36 -0800185 summaryPane,
Simon Hunta5e89142014-11-14 07:00:33 -0800186 detailPane,
Simon Hunta255a2c2014-11-13 22:29:35 -0800187 antTimer = null,
Simon Hunta5e89142014-11-14 07:00:33 -0800188 onosInstances = {},
189 onosOrder = [],
190 oiBox,
Simon Hunt9462e8c2014-11-14 17:28:09 -0800191 oiShowMaster = false,
Simon Hunt8f40cce2014-11-23 15:57:30 -0800192 portLabelsOn = false,
Simon Huntb0ecfa52014-11-23 21:05:12 -0800193 cat7 = d3u.cat7(),
194 colorAffinity = false;
Simon Hunt195cb382014-11-03 17:50:51 -0800195
Thomas Vachuska9edca302014-11-22 17:06:42 -0800196 var hoverModeAll = 1,
197 hoverModeFlows = 2,
198 hoverModeIntents = 3,
199 hoverMode = hoverModeFlows;
200
Simon Hunt934c3ce2014-11-05 11:45:07 -0800201 // D3 selections
202 var svg,
Paul Greysonfcba0e82014-11-13 10:21:16 -0800203 zoomPanContainer,
Simon Hunt934c3ce2014-11-05 11:45:07 -0800204 bgImg,
Simon Huntc7ee0662014-11-05 16:44:37 -0800205 topoG,
206 nodeG,
207 linkG,
Simon Hunte2575b62014-11-18 15:25:53 -0800208 linkLabelG,
Simon Huntc7ee0662014-11-05 16:44:37 -0800209 node,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800210 link,
Simon Hunte2575b62014-11-18 15:25:53 -0800211 linkLabel,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800212 mask;
Simon Hunt195cb382014-11-03 17:50:51 -0800213
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800214 // the projection for the map background
215 var geoMapProjection;
216
Paul Greysonfcba0e82014-11-13 10:21:16 -0800217 // the zoom function
218 var zoom;
219
Simon Hunt142d0032014-11-04 20:13:09 -0800220 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800221 // For Debugging / Development
Simon Hunt195cb382014-11-03 17:50:51 -0800222
Simon Hunt99c13842014-11-06 18:23:12 -0800223 function note(label, msg) {
224 console.log('NOTE: ' + label + ': ' + msg);
Simon Hunt195cb382014-11-03 17:50:51 -0800225 }
226
Simon Hunt99c13842014-11-06 18:23:12 -0800227 function debug(what) {
228 return config.debugOn && config.debug[what];
Simon Hunt934c3ce2014-11-05 11:45:07 -0800229 }
230
Simon Huntfc274c92014-11-11 11:05:46 -0800231 function fnTrace(msg, id) {
232 if (config.fnTrace) {
233 console.log('FN: ' + msg + ' [' + id + ']');
234 }
235 }
Simon Hunt99c13842014-11-06 18:23:12 -0800236
Simon Hunta5e89142014-11-14 07:00:33 -0800237 function evTrace(data) {
238 fnTrace(data.event, data.payload.id);
239 }
240
Simon Hunt934c3ce2014-11-05 11:45:07 -0800241 // ==============================
242 // Key Callbacks
243
Simon Hunt99c13842014-11-06 18:23:12 -0800244 function testMe(view) {
Simon Hunt8f40cce2014-11-23 15:57:30 -0800245 //view.alert('Theme is ' + view.getTheme());
Simon Hunta3dd9572014-11-20 15:22:41 -0800246 //view.flash('This is some text');
Simon Hunt8f40cce2014-11-23 15:57:30 -0800247 cat7.testCard(svg);
Simon Hunt99c13842014-11-06 18:23:12 -0800248 }
249
Simon Hunt56d51852014-11-09 13:03:35 -0800250 function abortIfLive() {
Simon Hunt50128c02014-11-08 13:36:15 -0800251 if (config.useLiveData) {
Simon Huntb53e0682014-11-12 13:32:01 -0800252 network.view.alert("Sorry, currently using live data..");
Simon Hunt56d51852014-11-09 13:03:35 -0800253 return true;
Simon Hunt50128c02014-11-08 13:36:15 -0800254 }
Simon Hunt56d51852014-11-09 13:03:35 -0800255 return false;
256 }
Simon Hunt50128c02014-11-08 13:36:15 -0800257
Simon Hunt56d51852014-11-09 13:03:35 -0800258 function testDebug(msg) {
259 if (scenario.debug) {
260 scenario.view.alert(msg);
261 }
262 }
Simon Hunt99c13842014-11-06 18:23:12 -0800263
Simon Hunt56d51852014-11-09 13:03:35 -0800264 function injectTestEvent(view) {
265 if (abortIfLive()) { return; }
266 var sc = scenario,
267 evn = ++sc.evNumber,
268 pfx = sc.evDir + sc.ctx + sc.evPrefix + evn,
269 onosUrl = pfx + sc.evOnos,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800270 uiUrl = pfx + sc.evUi,
271 stack = [
272 { url: onosUrl, cb: handleServerEvent },
273 { url: uiUrl, cb: handleUiEvent }
274 ];
275 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800276 }
277
Simon Hunt7cd48f32014-11-09 23:42:50 -0800278 function recurseFetchEvent(stack, evn) {
279 var v = scenario.view,
280 frame;
281 if (stack.length === 0) {
Simon Huntfc274c92014-11-11 11:05:46 -0800282 v.alert('Oops!\n\nNo event #' + evn + ' found.');
Simon Hunt7cd48f32014-11-09 23:42:50 -0800283 return;
284 }
285 frame = stack.shift();
286
287 d3.json(frame.url, function (err, data) {
Simon Hunt99c13842014-11-06 18:23:12 -0800288 if (err) {
Simon Hunt56d51852014-11-09 13:03:35 -0800289 if (err.status === 404) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800290 // if we didn't find the data, try the next stack frame
291 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800292 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800293 v.alert('non-404 error:\n\n' + frame.url + '\n\n' + err);
Simon Hunt56d51852014-11-09 13:03:35 -0800294 }
Simon Hunt99c13842014-11-06 18:23:12 -0800295 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800296 testDebug('loaded: ' + frame.url);
Simon Hunt1712ed82014-11-17 12:56:00 -0800297 wsTrace('test', JSON.stringify(data));
Simon Hunt7cd48f32014-11-09 23:42:50 -0800298 frame.cb(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800299 }
300 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800301
Simon Hunt56d51852014-11-09 13:03:35 -0800302 }
Simon Hunt50128c02014-11-08 13:36:15 -0800303
Simon Hunt56d51852014-11-09 13:03:35 -0800304 function handleUiEvent(data) {
Simon Huntbb282f52014-11-10 11:08:19 -0800305 scenario.view.alert('UI Tx: ' + data.event + '\n\n' +
306 JSON.stringify(data));
Simon Hunt56d51852014-11-09 13:03:35 -0800307 }
308
309 function injectStartupEvents(view) {
310 var last = scenario.params.lastAuto || 0;
311 if (abortIfLive()) { return; }
312
313 while (scenario.evNumber < last) {
Simon Hunt1a9eff92014-11-07 11:06:34 -0800314 injectTestEvent(view);
315 }
316 }
317
Simon Hunt934c3ce2014-11-05 11:45:07 -0800318 function toggleBg() {
319 var vis = bgImg.style('visibility');
320 bgImg.style('visibility', (vis === 'hidden') ? 'visible' : 'hidden');
321 }
322
Simon Hunt99c13842014-11-06 18:23:12 -0800323 function cycleLabels() {
Thomas Vachuska60d72bf2014-11-21 13:02:00 -0800324 deviceLabelIndex = (deviceLabelIndex === 2)
Simon Huntbb282f52014-11-10 11:08:19 -0800325 ? 0 : deviceLabelIndex + 1;
Simon Hunt5f36d342014-11-08 21:33:14 -0800326
Simon Hunt99c13842014-11-06 18:23:12 -0800327 network.nodes.forEach(function (d) {
Simon Huntbb282f52014-11-10 11:08:19 -0800328 if (d.class === 'device') {
329 updateDeviceLabel(d);
330 }
Simon Hunt99c13842014-11-06 18:23:12 -0800331 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800332 }
333
334 function togglePorts(view) {
Simon Hunt50128c02014-11-08 13:36:15 -0800335 view.alert('togglePorts() callback')
Simon Hunt934c3ce2014-11-05 11:45:07 -0800336 }
337
Simon Hunt6ac93f32014-11-13 12:17:27 -0800338 function unpin() {
339 if (hovered) {
Simon Hunt395a70c2014-11-22 23:17:40 -0800340 sendUpdateMeta(hovered);
Simon Hunt6ac93f32014-11-13 12:17:27 -0800341 hovered.fixed = false;
342 hovered.el.classed('fixed', false);
343 network.force.resume();
344 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800345 }
346
Simon Hunt9462e8c2014-11-14 17:28:09 -0800347 function handleEscape(view) {
348 if (oiShowMaster) {
349 cancelAffinity();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800350 } else if (detailPane.isVisible()) {
Simon Hunt9462e8c2014-11-14 17:28:09 -0800351 deselectAll();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800352 } else if (oiBox.isVisible()) {
Simon Huntb0ecfa52014-11-23 21:05:12 -0800353 hideInstances();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800354 } else if (summaryPane.isVisible()) {
355 cancelSummary();
Simon Hunt9462e8c2014-11-14 17:28:09 -0800356 }
357 }
358
Simon Hunt934c3ce2014-11-05 11:45:07 -0800359 // ==============================
360 // Radio Button Callbacks
361
Simon Hunta5e89142014-11-14 07:00:33 -0800362 var layerLookup = {
363 host: {
Simon Hunt209155e2014-11-21 12:16:09 -0800364 endstation: 'pkt', // default, if host event does not define type
Thomas Vachuska89543292014-11-19 11:28:33 -0800365 router: 'pkt',
Simon Hunta5e89142014-11-14 07:00:33 -0800366 bgpSpeaker: 'pkt'
367 },
368 device: {
369 switch: 'pkt',
370 roadm: 'opt'
371 },
372 link: {
373 hostLink: 'pkt',
374 direct: 'pkt',
Simon Hunt8257f4c2014-11-16 19:34:54 -0800375 indirect: '',
376 tunnel: '',
Simon Hunta5e89142014-11-14 07:00:33 -0800377 optical: 'opt'
378 }
379 };
380
381 function inLayer(d, layer) {
Simon Hunt8257f4c2014-11-16 19:34:54 -0800382 var type = d.class === 'link' ? d.type() : d.type,
383 look = layerLookup[d.class],
384 lyr = look && look[type];
Simon Hunta5e89142014-11-14 07:00:33 -0800385 return lyr === layer;
386 }
387
388 function unsuppressLayer(which) {
389 node.each(function (d) {
390 var node = d.el;
391 if (inLayer(d, which)) {
392 node.classed('suppressed', false);
393 }
394 });
395
396 link.each(function (d) {
397 var link = d.el;
398 if (inLayer(d, which)) {
399 link.classed('suppressed', false);
400 }
401 });
402 }
403
Simon Hunt9462e8c2014-11-14 17:28:09 -0800404 function suppressLayers(b) {
405 node.classed('suppressed', b);
406 link.classed('suppressed', b);
Simon Hunt142d0032014-11-04 20:13:09 -0800407// d3.selectAll('svg .port').classed('inactive', false);
408// d3.selectAll('svg .portText').classed('inactive', false);
Simon Hunt195cb382014-11-03 17:50:51 -0800409 }
410
Simon Hunt9462e8c2014-11-14 17:28:09 -0800411 function showAllLayers() {
412 suppressLayers(false);
413 }
414
Simon Hunt195cb382014-11-03 17:50:51 -0800415 function showPacketLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800416 node.classed('suppressed', true);
417 link.classed('suppressed', true);
418 unsuppressLayer('pkt');
Simon Hunt195cb382014-11-03 17:50:51 -0800419 }
420
421 function showOpticalLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800422 node.classed('suppressed', true);
423 link.classed('suppressed', true);
424 unsuppressLayer('opt');
Simon Hunt195cb382014-11-03 17:50:51 -0800425 }
426
Simon Hunt9462e8c2014-11-14 17:28:09 -0800427 function restoreLayerState() {
428 layerBtnDispatch[layerBtnSet.selected()]();
429 }
430
Simon Hunt142d0032014-11-04 20:13:09 -0800431 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800432 // Private functions
433
Simon Hunt99c13842014-11-06 18:23:12 -0800434 function safeId(s) {
435 return s.replace(/[^a-z0-9]/gi, '-');
436 }
437
Simon Huntc7ee0662014-11-05 16:44:37 -0800438 // set the size of the given element to that of the view (reduced if padded)
439 function setSize(el, view, pad) {
440 var padding = pad ? pad * 2 : 0;
Simon Hunt934c3ce2014-11-05 11:45:07 -0800441 el.attr({
Simon Huntc7ee0662014-11-05 16:44:37 -0800442 width: view.width() - padding,
443 height: view.height() - padding
Simon Hunt934c3ce2014-11-05 11:45:07 -0800444 });
445 }
446
Simon Hunt8257f4c2014-11-16 19:34:54 -0800447 function makeNodeKey(d, what) {
448 var port = what + 'Port';
449 return d[what] + '/' + d[port];
450 }
451
452 function makeLinkKey(d, flipped) {
453 var one = flipped ? makeNodeKey(d, 'dst') : makeNodeKey(d, 'src'),
454 two = flipped ? makeNodeKey(d, 'src') : makeNodeKey(d, 'dst');
455 return one + '-' + two;
456 }
457
Simon Hunt269670f2014-11-17 16:17:43 -0800458 function findLinkById(id) {
459 // check to see if this is a reverse lookup, else default to given id
460 var key = network.revLinkToKey[id] || id;
461 return key && network.lookup[key];
462 }
463
Simon Hunt8257f4c2014-11-16 19:34:54 -0800464 function findLink(linkData, op) {
465 var key = makeLinkKey(linkData),
466 keyrev = makeLinkKey(linkData, 1),
467 link = network.lookup[key],
468 linkRev = network.lookup[keyrev],
469 result = {},
470 ldata = link || linkRev,
471 rawLink;
472
473 if (op === 'add') {
474 if (link) {
475 // trying to add a link that we already know about
476 result.ldata = link;
477 result.badLogic = 'addLink: link already added';
478
479 } else if (linkRev) {
480 // we found the reverse of the link to be added
481 result.ldata = linkRev;
482 if (linkRev.fromTarget) {
483 result.badLogic = 'addLink: link already added';
484 }
485 }
486 } else if (op === 'update') {
487 if (!ldata) {
488 result.badLogic = 'updateLink: link not found';
489 } else {
490 rawLink = link ? ldata.fromSource : ldata.fromTarget;
491 result.updateWith = function (data) {
492 $.extend(rawLink, data);
493 restyleLinkElement(ldata);
494 }
495 }
496 } else if (op === 'remove') {
497 if (!ldata) {
498 result.badLogic = 'removeLink: link not found';
499 } else {
500 rawLink = link ? ldata.fromSource : ldata.fromTarget;
501
502 if (!rawLink) {
503 result.badLogic = 'removeLink: link not found';
504
505 } else {
506 result.removeRawLink = function () {
507 if (link) {
508 // remove fromSource
509 ldata.fromSource = null;
510 if (ldata.fromTarget) {
511 // promote target into source position
512 ldata.fromSource = ldata.fromTarget;
513 ldata.fromTarget = null;
514 ldata.key = keyrev;
515 delete network.lookup[key];
516 network.lookup[keyrev] = ldata;
Simon Hunt269670f2014-11-17 16:17:43 -0800517 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800518 }
519 } else {
520 // remove fromTarget
521 ldata.fromTarget = null;
Simon Hunt269670f2014-11-17 16:17:43 -0800522 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800523 }
524 if (ldata.fromSource) {
525 restyleLinkElement(ldata);
526 } else {
527 removeLinkElement(ldata);
528 }
529 }
530 }
531 }
532 }
533 return result;
534 }
535
536 function addLinkUpdate(ldata, link) {
537 // add link event, but we already have the reverse link installed
538 ldata.fromTarget = link;
Simon Hunt269670f2014-11-17 16:17:43 -0800539 network.revLinkToKey[link.id] = ldata.key;
Simon Hunt8257f4c2014-11-16 19:34:54 -0800540 restyleLinkElement(ldata);
541 }
542
543 var allLinkTypes = 'direct indirect optical tunnel',
544 defaultLinkType = 'direct';
545
546 function restyleLinkElement(ldata) {
547 // this fn's job is to look at raw links and decide what svg classes
548 // need to be applied to the line element in the DOM
549 var el = ldata.el,
550 type = ldata.type(),
551 lw = ldata.linkWidth(),
552 online = ldata.online();
553
554 el.classed('link', true);
555 el.classed('inactive', !online);
556 el.classed(allLinkTypes, false);
557 if (type) {
558 el.classed(type, true);
559 }
560 el.transition()
561 .duration(1000)
Thomas Vachuska89543292014-11-19 11:28:33 -0800562 .attr('stroke-width', linkScale(lw))
563 .attr('stroke', config.topo.linkBaseColor);
Simon Hunt8257f4c2014-11-16 19:34:54 -0800564 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800565
Simon Hunt99c13842014-11-06 18:23:12 -0800566 // ==============================
567 // Event handlers for server-pushed events
568
Simon Huntbb282f52014-11-10 11:08:19 -0800569 function logicError(msg) {
570 // TODO, report logic error to server, via websock, so it can be logged
Simon Huntcb56cff2014-11-17 11:42:26 -0800571 //network.view.alert('Logic Error:\n\n' + msg);
Simon Huntfc274c92014-11-11 11:05:46 -0800572 console.warn(msg);
Simon Huntbb282f52014-11-10 11:08:19 -0800573 }
574
Simon Hunt99c13842014-11-06 18:23:12 -0800575 var eventDispatch = {
Simon Hunta5e89142014-11-14 07:00:33 -0800576 addInstance: addInstance,
Simon Hunt99c13842014-11-06 18:23:12 -0800577 addDevice: addDevice,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800578 addLink: addLink,
Simon Hunt56d51852014-11-09 13:03:35 -0800579 addHost: addHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800580
Simon Huntfcfb46c2014-11-19 12:53:38 -0800581 updateInstance: updateInstance,
Simon Huntbb282f52014-11-10 11:08:19 -0800582 updateDevice: updateDevice,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800583 updateLink: updateLink,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800584 updateHost: updateHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800585
Simon Hunt7b403bc2014-11-22 19:01:00 -0800586 removeInstance: removeInstance,
Simon Huntbb282f52014-11-10 11:08:19 -0800587 removeDevice: stillToImplement,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800588 removeLink: removeLink,
Simon Hunt44031102014-11-11 13:20:36 -0800589 removeHost: removeHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800590
Simon Hunt61d04042014-11-11 17:27:16 -0800591 showDetails: showDetails,
Thomas Vachuska47635c62014-11-22 01:21:36 -0800592 showSummary: showSummary,
Simon Huntb53e0682014-11-12 13:32:01 -0800593 showTraffic: showTraffic
Simon Hunt99c13842014-11-06 18:23:12 -0800594 };
595
Simon Hunta5e89142014-11-14 07:00:33 -0800596 function addInstance(data) {
597 evTrace(data);
598 var inst = data.payload,
599 id = inst.id;
600 if (onosInstances[id]) {
601 logicError('ONOS instance already added: ' + id);
602 return;
603 }
604 onosInstances[id] = inst;
605 onosOrder.push(inst);
606 updateInstances();
607 }
608
Simon Hunt99c13842014-11-06 18:23:12 -0800609 function addDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800610 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800611 var device = data.payload,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800612 nodeData = createDeviceNode(device);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800613 network.nodes.push(nodeData);
614 network.lookup[nodeData.id] = nodeData;
Simon Hunt99c13842014-11-06 18:23:12 -0800615 updateNodes();
616 network.force.start();
617 }
618
Simon Hunt99c13842014-11-06 18:23:12 -0800619 function addLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800620 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800621 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800622 result = findLink(link, 'add'),
623 bad = result.badLogic,
624 ldata = result.ldata;
625
626 if (bad) {
627 logicError(bad + ': ' + link.id);
628 return;
629 }
630
631 if (ldata) {
632 // we already have a backing store link for src/dst nodes
633 addLinkUpdate(ldata, link);
634 return;
635 }
636
637 // no backing store link yet
638 ldata = createLink(link);
639 if (ldata) {
640 network.links.push(ldata);
641 network.lookup[ldata.key] = ldata;
Simon Hunt99c13842014-11-06 18:23:12 -0800642 updateLinks();
643 network.force.start();
644 }
645 }
646
Simon Hunt56d51852014-11-09 13:03:35 -0800647 function addHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800648 evTrace(data);
Simon Hunt56d51852014-11-09 13:03:35 -0800649 var host = data.payload,
650 node = createHostNode(host),
651 lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800652 network.nodes.push(node);
653 network.lookup[host.id] = node;
654 updateNodes();
655
656 lnk = createHostLink(host);
657 if (lnk) {
Simon Hunt44031102014-11-11 13:20:36 -0800658 node.linkData = lnk; // cache ref on its host
Simon Hunt56d51852014-11-09 13:03:35 -0800659 network.links.push(lnk);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800660 network.lookup[host.ingress] = lnk;
661 network.lookup[host.egress] = lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800662 updateLinks();
663 }
664 network.force.start();
665 }
666
Simon Hunt44031102014-11-11 13:20:36 -0800667 // TODO: fold updateX(...) methods into one base method; remove duplication
Simon Hunt56a2ea42014-11-19 12:39:31 -0800668
669 function updateInstance(data) {
670 evTrace(data);
671 var inst = data.payload,
672 id = inst.id,
673 instData = onosInstances[id];
674 if (instData) {
675 $.extend(instData, inst);
676 updateInstances();
Simon Hunt56a2ea42014-11-19 12:39:31 -0800677 } else {
678 logicError('updateInstance lookup fail. ID = "' + id + '"');
679 }
680 }
681
Simon Huntbb282f52014-11-10 11:08:19 -0800682 function updateDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800683 evTrace(data);
Simon Huntbb282f52014-11-10 11:08:19 -0800684 var device = data.payload,
685 id = device.id,
Simon Hunt62c47542014-11-22 22:16:32 -0800686 d = network.lookup[id];
687 if (d) {
688 $.extend(d, device);
689 if (positionNode(d, true)) {
Simon Hunt395a70c2014-11-22 23:17:40 -0800690 sendUpdateMeta(d, true);
Simon Hunt62c47542014-11-22 22:16:32 -0800691 }
692 updateNodes();
Simon Huntbb282f52014-11-10 11:08:19 -0800693 } else {
694 logicError('updateDevice lookup fail. ID = "' + id + '"');
695 }
696 }
697
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800698 function updateLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800699 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800700 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800701 result = findLink(link, 'update'),
702 bad = result.badLogic;
703 if (bad) {
704 logicError(bad + ': ' + link.id);
705 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800706 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800707 result.updateWith(link);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800708 }
709
Simon Hunt7cd48f32014-11-09 23:42:50 -0800710 function updateHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800711 evTrace(data);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800712 var host = data.payload,
Simon Huntbb282f52014-11-10 11:08:19 -0800713 id = host.id,
714 hostData = network.lookup[id];
715 if (hostData) {
716 $.extend(hostData, host);
717 updateHostState(hostData);
718 } else {
719 logicError('updateHost lookup fail. ID = "' + id + '"');
720 }
Simon Hunt7cd48f32014-11-09 23:42:50 -0800721 }
722
Simon Hunt44031102014-11-11 13:20:36 -0800723 // TODO: fold removeX(...) methods into base method - remove dup code
Simon Hunt7b403bc2014-11-22 19:01:00 -0800724 function removeInstance(data) {
725 evTrace(data);
726 var inst = data.payload,
727 id = inst.id,
728 instData = onosInstances[id];
729 if (instData) {
730 var idx = find(id, onosOrder, 'id');
731 if (idx >= 0) {
732 onosOrder.splice(idx, 1);
733 }
734 delete onosInstances[id];
735 updateInstances();
736 } else {
737 logicError('updateInstance lookup fail. ID = "' + id + '"');
738 }
739 }
740
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800741 function removeLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800742 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800743 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800744 result = findLink(link, 'remove'),
745 bad = result.badLogic;
746 if (bad) {
747 logicError(bad + ': ' + link.id);
748 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800749 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800750 result.removeRawLink();
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800751 }
752
Simon Hunt44031102014-11-11 13:20:36 -0800753 function removeHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800754 evTrace(data);
Simon Hunt44031102014-11-11 13:20:36 -0800755 var host = data.payload,
756 id = host.id,
757 hostData = network.lookup[id];
758 if (hostData) {
759 removeHostElement(hostData);
760 } else {
761 logicError('removeHost lookup fail. ID = "' + id + '"');
762 }
763 }
764
Thomas Vachuska47635c62014-11-22 01:21:36 -0800765 function showSummary(data) {
766 evTrace(data);
767 populateSummary(data.payload);
768 summaryPane.show();
769 }
770
Simon Hunt61d04042014-11-11 17:27:16 -0800771 function showDetails(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800772 evTrace(data);
Simon Hunt61d04042014-11-11 17:27:16 -0800773 populateDetails(data.payload);
774 detailPane.show();
775 }
776
Simon Huntb53e0682014-11-12 13:32:01 -0800777 function showTraffic(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800778 evTrace(data);
Thomas Vachuska4731f122014-11-20 04:56:19 -0800779 var paths = data.payload.paths,
780 hasTraffic = false;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800781
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800782 // Revert any links hilighted previously.
Thomas Vachuska4731f122014-11-20 04:56:19 -0800783 link.style('stroke-width', null)
Thomas Vachuskaa3148a72014-11-19 21:38:35 -0800784 .classed('primary secondary animated optical', false);
Simon Hunte2575b62014-11-18 15:25:53 -0800785 // Remove all previous labels.
786 removeLinkLabels();
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800787
Simon Hunte2575b62014-11-18 15:25:53 -0800788 // Now hilight all links in the paths payload, and attach
789 // labels to them, if they are defined.
Simon Hunta255a2c2014-11-13 22:29:35 -0800790 paths.forEach(function (p) {
Simon Hunte2575b62014-11-18 15:25:53 -0800791 var n = p.links.length,
792 i,
793 ldata;
794
Thomas Vachuska4731f122014-11-20 04:56:19 -0800795 hasTraffic = hasTraffic || p.traffic;
Simon Hunte2575b62014-11-18 15:25:53 -0800796 for (i=0; i<n; i++) {
797 ldata = findLinkById(p.links[i]);
Thomas Vachuska4731f122014-11-20 04:56:19 -0800798 if (ldata && ldata.el) {
Simon Hunte2575b62014-11-18 15:25:53 -0800799 ldata.el.classed(p.class, true);
800 ldata.label = p.labels[i];
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800801 }
Simon Hunte2575b62014-11-18 15:25:53 -0800802 }
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800803 });
Thomas Vachuska4731f122014-11-20 04:56:19 -0800804
Simon Hunte2575b62014-11-18 15:25:53 -0800805 updateLinks();
Thomas Vachuska4731f122014-11-20 04:56:19 -0800806
807 if (hasTraffic && !antTimer) {
808 startAntTimer();
809 } else if (!hasTraffic && antTimer) {
810 stopAntTimer();
811 }
Simon Huntb53e0682014-11-12 13:32:01 -0800812 }
813
Simon Hunt56d51852014-11-09 13:03:35 -0800814 // ...............................
815
816 function stillToImplement(data) {
817 var p = data.payload;
818 note(data.event, p.id);
Simon Hunt7b403bc2014-11-22 19:01:00 -0800819 if (!config.useLiveData) {
820 network.view.alert('Not yet implemented: "' + data.event + '"');
821 }
Simon Hunt56d51852014-11-09 13:03:35 -0800822 }
Simon Hunt99c13842014-11-06 18:23:12 -0800823
824 function unknownEvent(data) {
Simon Hunt50128c02014-11-08 13:36:15 -0800825 network.view.alert('Unknown event type: "' + data.event + '"');
Simon Hunt99c13842014-11-06 18:23:12 -0800826 }
827
828 function handleServerEvent(data) {
829 var fn = eventDispatch[data.event] || unknownEvent;
830 fn(data);
831 }
832
833 // ==============================
Simon Hunt61d04042014-11-11 17:27:16 -0800834 // Out-going messages...
835
Simon Huntb53e0682014-11-12 13:32:01 -0800836 function userFeedback(msg) {
837 // for now, use the alert pane as is. Maybe different alert style in
838 // the future (centered on view; dismiss button?)
839 network.view.alert(msg);
840 }
841
842 function nSel() {
843 return selectOrder.length;
844 }
Simon Hunt61d04042014-11-11 17:27:16 -0800845 function getSel(idx) {
846 return selections[selectOrder[idx]];
847 }
Simon Huntb53e0682014-11-12 13:32:01 -0800848 function getSelId(idx) {
849 return getSel(idx).obj.id;
850 }
Thomas Vachuska9edca302014-11-22 17:06:42 -0800851 function getSelIds(start, endOffset) {
852 var end = selectOrder.length - endOffset;
853 var ids = [];
854 selectOrder.slice(start, end).forEach(function (d) {
855 ids.push(getSelId(d));
856 });
857 return ids;
858 }
Simon Huntb53e0682014-11-12 13:32:01 -0800859 function allSelectionsClass(cls) {
860 for (var i=0, n=nSel(); i<n; i++) {
861 if (getSel(i).obj.class !== cls) {
862 return false;
863 }
864 }
865 return true;
866 }
Simon Hunt61d04042014-11-11 17:27:16 -0800867
Thomas Vachuska47635c62014-11-22 01:21:36 -0800868 function toggleInstances() {
869 if (!oiBox.isVisible()) {
Simon Huntb0ecfa52014-11-23 21:05:12 -0800870 showInstances();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800871 } else {
Simon Huntb0ecfa52014-11-23 21:05:12 -0800872 hideInstances();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800873 }
874 }
875
Simon Huntb0ecfa52014-11-23 21:05:12 -0800876 function showInstances() {
877 oiBox.show();
878 colorAffinity = true;
879 updateDeviceColors();
880 }
881
882 function hideInstances() {
883 oiBox.hide();
884 colorAffinity = false;
885 cancelAffinity();
886 updateDeviceColors();
887 }
888
889
Thomas Vachuska47635c62014-11-22 01:21:36 -0800890 function toggleSummary() {
891 if (!summaryPane.isVisible()) {
892 requestSummary();
893 } else {
894 cancelSummary();
895 }
896 }
897
898 // request overall summary data
899 function requestSummary() {
900 sendMessage('requestSummary', {});
901 }
902
903 function cancelSummary() {
904 sendMessage('cancelSummary', {});
905 summaryPane.hide();
906 }
907
Simon Hunt61d04042014-11-11 17:27:16 -0800908 // request details for the selected element
Simon Huntd72bc702014-11-13 18:38:04 -0800909 // invoked from selection of a single node.
Simon Hunt61d04042014-11-11 17:27:16 -0800910 function requestDetails() {
911 var data = getSel(0).obj,
912 payload = {
913 id: data.id,
914 class: data.class
915 };
916 sendMessage('requestDetails', payload);
917 }
918
Thomas Vachuska9edca302014-11-22 17:06:42 -0800919 function addHostIntentAction() {
Simon Huntd72bc702014-11-13 18:38:04 -0800920 sendMessage('addHostIntent', {
Thomas Vachuska9edca302014-11-22 17:06:42 -0800921 one: selectOrder[0],
922 two: selectOrder[1],
923 ids: selectOrder
Simon Huntd72bc702014-11-13 18:38:04 -0800924 });
Thomas Vachuska9edca302014-11-22 17:06:42 -0800925 network.view.flash('Host-to-Host flow added');
Simon Huntd72bc702014-11-13 18:38:04 -0800926 }
927
Thomas Vachuska9edca302014-11-22 17:06:42 -0800928 function addMultiSourceIntentAction() {
929 sendMessage('addMultiSourceIntent', {
930 src: selectOrder.slice(0, selectOrder.length - 1),
931 dst: selectOrder[selectOrder.length - 1],
932 ids: selectOrder
933 });
934 network.view.flash('Multi-Source flow added');
Thomas Vachuska29617e52014-11-20 03:17:46 -0800935 }
936
Thomas Vachuska9edca302014-11-22 17:06:42 -0800937
Thomas Vachuska47635c62014-11-22 01:21:36 -0800938 function cancelTraffic() {
939 sendMessage('cancelTraffic', {});
940 }
941
Thomas Vachuska9edca302014-11-22 17:06:42 -0800942 function requestTrafficForMode() {
943 if (hoverMode === hoverModeAll) {
944 requestAllTraffic();
945 } else if (hoverMode === hoverModeFlows) {
946 requestDeviceLinkFlows();
947 } else if (hoverMode === hoverModeIntents) {
948 requestSelectTraffic();
Simon Huntd72bc702014-11-13 18:38:04 -0800949 }
Thomas Vachuska9edca302014-11-22 17:06:42 -0800950 }
Simon Huntd72bc702014-11-13 18:38:04 -0800951
Thomas Vachuska9edca302014-11-22 17:06:42 -0800952 function showTrafficAction() {
953 hoverMode = hoverModeIntents;
954 requestSelectTraffic();
955 network.view.flash('Related Traffic');
956 }
957
958 function requestSelectTraffic() {
959 if (validateSelectionContext()) {
960 var hoverId = (hoverMode === hoverModeIntents && hovered &&
961 (hovered.class === 'host' || hovered.class === 'device'))
Simon Huntd72bc702014-11-13 18:38:04 -0800962 ? hovered.id : '';
Thomas Vachuska9edca302014-11-22 17:06:42 -0800963 sendMessage('requestTraffic', {
964 ids: selectOrder,
965 hover: hoverId
966 });
967 }
Simon Huntd72bc702014-11-13 18:38:04 -0800968 }
969
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800970
Thomas Vachuska29617e52014-11-20 03:17:46 -0800971 function showDeviceLinkFlowsAction() {
Thomas Vachuska9edca302014-11-22 17:06:42 -0800972 hoverMode = hoverModeFlows;
973 requestDeviceLinkFlows();
Thomas Vachuskac59658c2014-11-21 13:13:36 -0800974 network.view.flash('Device Flows');
Thomas Vachuska29617e52014-11-20 03:17:46 -0800975 }
976
Thomas Vachuska9edca302014-11-22 17:06:42 -0800977 function requestDeviceLinkFlows() {
978 if (validateSelectionContext()) {
979 var hoverId = (hoverMode === hoverModeFlows && hovered &&
980 (hovered.class === 'device')) ? hovered.id : '';
981 sendMessage('requestDeviceLinkFlows', {
982 ids: selectOrder,
983 hover: hoverId
984 });
985 }
986 }
987
988
989 function showAllTrafficAction() {
990 hoverMode = hoverModeAll;
991 requestAllTraffic();
992 network.view.flash('All Traffic');
993 }
994
995 function requestAllTraffic() {
996 sendMessage('requestAllTraffic', {});
997 }
998
999 function validateSelectionContext() {
Thomas Vachuska29617e52014-11-20 03:17:46 -08001000 if (!hovered && nSel() === 0) {
Thomas Vachuska47635c62014-11-22 01:21:36 -08001001 cancelTraffic();
Thomas Vachuska9edca302014-11-22 17:06:42 -08001002 return false;
Thomas Vachuska29617e52014-11-20 03:17:46 -08001003 }
Thomas Vachuska9edca302014-11-22 17:06:42 -08001004 return true;
Thomas Vachuska29617e52014-11-20 03:17:46 -08001005 }
Simon Huntd72bc702014-11-13 18:38:04 -08001006
Simon Hunta6a9fe72014-11-20 11:17:12 -08001007
Simon Hunt61d04042014-11-11 17:27:16 -08001008 // ==============================
Simon Hunta5e89142014-11-14 07:00:33 -08001009 // onos instance panel functions
Simon Huntb82f6902014-11-22 11:53:15 -08001010
Simon Hunt7b403bc2014-11-22 19:01:00 -08001011 var instCfg = {
1012 rectPad: 8,
1013 nodeOx: 9,
1014 nodeOy: 9,
1015 nodeDim: 40,
1016 birdOx: 19,
1017 birdOy: 21,
1018 birdDim: 21,
1019 uiDy: 45,
1020 titleDy: 30,
1021 textYOff: 20,
1022 textYSpc: 15
1023 };
1024
1025 function viewBox(dim) {
1026 return '0 0 ' + dim.w + ' ' + dim.h;
1027 }
1028
1029 function instRectAttr(dim) {
1030 var pad = instCfg.rectPad;
1031 return {
1032 x: pad,
1033 y: pad,
1034 width: dim.w - pad*2,
1035 height: dim.h - pad*2,
Simon Huntb0ecfa52014-11-23 21:05:12 -08001036 rx: 6
Simon Hunt7b403bc2014-11-22 19:01:00 -08001037 };
1038 }
1039
1040 function computeDim(self) {
1041 var css = window.getComputedStyle(self);
1042 return {
1043 w: stripPx(css.width),
1044 h: stripPx(css.height)
1045 };
Simon Huntb82f6902014-11-22 11:53:15 -08001046 }
Simon Hunta5e89142014-11-14 07:00:33 -08001047
1048 function updateInstances() {
1049 var onoses = oiBox.el.selectAll('.onosInst')
Simon Huntb82f6902014-11-22 11:53:15 -08001050 .data(onosOrder, function (d) { return d.id; }),
Simon Hunt7b403bc2014-11-22 19:01:00 -08001051 instDim = {w:0,h:0},
1052 c = instCfg;
Simon Hunta5e89142014-11-14 07:00:33 -08001053
Simon Hunt7b403bc2014-11-22 19:01:00 -08001054 function nSw(n) {
1055 return '# Switches: ' + n;
1056 }
Simon Hunta5e89142014-11-14 07:00:33 -08001057
Simon Huntb82f6902014-11-22 11:53:15 -08001058 // operate on existing onos instances if necessary
1059 onoses.each(function (d) {
1060 var el = d3.select(this),
1061 svg = el.select('svg');
Simon Hunt7b403bc2014-11-22 19:01:00 -08001062 instDim = computeDim(this);
Simon Huntb82f6902014-11-22 11:53:15 -08001063
1064 // update online state
1065 el.classed('online', d.online);
1066
1067 // update ui-attached state
1068 svg.select('use.uiBadge').remove();
1069 if (d.uiAttached) {
1070 attachUiBadge(svg);
1071 }
1072
Simon Hunt7b403bc2014-11-22 19:01:00 -08001073 function updAttr(id, value) {
1074 svg.select('text.instLabel.'+id).text(value);
1075 }
1076
1077 updAttr('ip', d.ip);
1078 updAttr('ns', nSw(d.switches));
Simon Huntb82f6902014-11-22 11:53:15 -08001079 });
1080
1081
1082 // operate on new onos instances
Simon Hunta5e89142014-11-14 07:00:33 -08001083 var entering = onoses.enter()
1084 .append('div')
1085 .attr('class', 'onosInst')
1086 .classed('online', function (d) { return d.online; })
Simon Hunt9c15eca2014-11-15 18:37:59 -08001087 .on('click', clickInst);
1088
Simon Huntb82f6902014-11-22 11:53:15 -08001089 entering.each(function (d) {
Simon Hunt9c15eca2014-11-15 18:37:59 -08001090 var el = d3.select(this),
Simon Hunt7b403bc2014-11-22 19:01:00 -08001091 rectAttr,
1092 svg;
1093 instDim = computeDim(this);
1094 rectAttr = instRectAttr(instDim);
Simon Hunt9c15eca2014-11-15 18:37:59 -08001095
Simon Hunt7b403bc2014-11-22 19:01:00 -08001096 svg = el.append('svg').attr({
1097 width: instDim.w,
1098 height: instDim.h,
1099 viewBox: viewBox(instDim)
Simon Hunt95908012014-11-20 10:20:26 -08001100 });
Simon Huntb82f6902014-11-22 11:53:15 -08001101
Simon Hunt7b403bc2014-11-22 19:01:00 -08001102 svg.append('rect').attr(rectAttr);
Simon Hunt9c15eca2014-11-15 18:37:59 -08001103
Simon Hunt7b403bc2014-11-22 19:01:00 -08001104 appendGlyph(svg, c.nodeOx, c.nodeOy, c.nodeDim, '#node');
1105 appendBadge(svg, c.birdOx, c.birdOy, c.birdDim, '#bird');
Simon Huntb82f6902014-11-22 11:53:15 -08001106
1107 if (d.uiAttached) {
1108 attachUiBadge(svg);
1109 }
1110
Simon Hunt7b403bc2014-11-22 19:01:00 -08001111 var left = c.nodeOx + c.nodeDim,
1112 len = rectAttr.width - left,
1113 hlen = len / 2,
1114 midline = hlen + left;
Simon Hunt9c15eca2014-11-15 18:37:59 -08001115
Simon Hunt7b403bc2014-11-22 19:01:00 -08001116 // title
1117 svg.append('text')
1118 .attr({
1119 class: 'instTitle',
1120 x: midline,
1121 y: c.titleDy
1122 })
1123 .text(d.id);
1124
1125 // a couple of attributes
1126 var ty = c.titleDy + c.textYOff;
1127
1128 function addAttr(id, label) {
1129 svg.append('text').attr({
1130 class: 'instLabel ' + id,
1131 x: midline,
1132 y: ty
1133 }).text(label);
1134 ty += c.textYSpc;
1135 }
1136
1137 addAttr('ip', d.ip);
1138 addAttr('ns', nSw(d.switches));
Simon Hunt9c15eca2014-11-15 18:37:59 -08001139 });
Simon Hunta5e89142014-11-14 07:00:33 -08001140
1141 // operate on existing + new onoses here
Simon Hunt8f40cce2014-11-23 15:57:30 -08001142 // set the affinity colors...
1143 onoses.each(function (d) {
1144 var el = d3.select(this),
1145 rect = el.select('svg').select('rect'),
1146 col = instColor(d.id, d.online);
1147 rect.style('fill', col);
1148 });
Simon Hunta5e89142014-11-14 07:00:33 -08001149
Simon Hunt7b403bc2014-11-22 19:01:00 -08001150 // adjust the panel size appropriately...
1151 oiBox.width(instDim.w * onosOrder.length);
1152 oiBox.height(instDim.h);
1153
1154 // remove any outgoing instances
1155 onoses.exit().remove();
Simon Hunta5e89142014-11-14 07:00:33 -08001156 }
1157
Simon Hunt8f40cce2014-11-23 15:57:30 -08001158 function instColor(id, online) {
1159 return cat7.get(id, !online, network.view.getTheme());
1160 }
1161
Simon Hunt9462e8c2014-11-14 17:28:09 -08001162 function clickInst(d) {
1163 var el = d3.select(this),
1164 aff = el.classed('affinity');
1165 if (!aff) {
1166 setAffinity(el, d);
1167 } else {
1168 cancelAffinity();
1169 }
1170 }
1171
1172 function setAffinity(el, d) {
1173 d3.selectAll('.onosInst')
1174 .classed('mastership', true)
1175 .classed('affinity', false);
1176 el.classed('affinity', true);
1177
1178 suppressLayers(true);
1179 node.each(function (n) {
1180 if (n.master === d.id) {
1181 n.el.classed('suppressed', false);
1182 }
1183 });
1184 oiShowMaster = true;
1185 }
1186
1187 function cancelAffinity() {
1188 d3.selectAll('.onosInst')
1189 .classed('mastership affinity', false);
1190 restoreLayerState();
1191 oiShowMaster = false;
1192 }
1193
Simon Hunt7b403bc2014-11-22 19:01:00 -08001194 // TODO: these should be moved out to utility module.
1195 function stripPx(s) {
1196 return s.replace(/px$/,'');
1197 }
1198
1199 function appendUse(svg, ox, oy, dim, iid, cls) {
1200 var use = svg.append('use').attr({
1201 transform: translate(ox,oy),
1202 'xlink:href': iid,
1203 width: dim,
1204 height: dim
1205 });
1206 if (cls) {
1207 use.classed(cls, true);
1208 }
1209 return use;
1210 }
1211
1212 function appendGlyph(svg, ox, oy, dim, iid, cls) {
1213 appendUse(svg, ox, oy, dim, iid, cls).classed('glyphIcon', true);
1214 }
1215
1216 function appendBadge(svg, ox, oy, dim, iid, cls) {
1217 appendUse(svg, ox, oy, dim, iid,cls ).classed('badgeIcon', true);
1218 }
1219
1220 function attachUiBadge(svg) {
1221 appendBadge(svg, 12, instCfg.uiDy, 30, '#uiAttached', 'uiBadge');
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 Hunt3f03d4a2014-11-10 20:14:37 -08001322 var widthRatio = 1.4,
1323 linkScale = d3.scale.linear()
1324 .domain([1, 12])
1325 .range([widthRatio, 12 * widthRatio])
1326 .clamp(true);
1327
Simon Hunt99c13842014-11-06 18:23:12 -08001328 function updateLinks() {
1329 link = linkG.selectAll('.link')
Simon Hunt8257f4c2014-11-16 19:34:54 -08001330 .data(network.links, function (d) { return d.key; });
Simon Hunt99c13842014-11-06 18:23:12 -08001331
1332 // operate on existing links, if necessary
1333 // link .foo() .bar() ...
1334
1335 // operate on entering links:
1336 var entering = link.enter()
1337 .append('line')
1338 .attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001339 x1: function (d) { return d.x1; },
1340 y1: function (d) { return d.y1; },
1341 x2: function (d) { return d.x2; },
1342 y2: function (d) { return d.y2; },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001343 stroke: config.topo.linkInColor,
1344 'stroke-width': config.topo.linkInWidth
Simon Hunt99c13842014-11-06 18:23:12 -08001345 });
1346
1347 // augment links
Simon Hunt7cd48f32014-11-09 23:42:50 -08001348 entering.each(function (d) {
1349 var link = d3.select(this);
1350 // provide ref to element selection from backing data....
1351 d.el = link;
Simon Hunt8257f4c2014-11-16 19:34:54 -08001352 restyleLinkElement(d);
Simon Hunt7cd48f32014-11-09 23:42:50 -08001353 });
Thomas Vachuska4830d392014-11-09 17:09:56 -08001354
1355 // operate on both existing and new links, if necessary
1356 //link .foo() .bar() ...
1357
Simon Hunte2575b62014-11-18 15:25:53 -08001358 // apply or remove labels
1359 var labelData = getLabelData();
1360 applyLinkLabels(labelData);
1361
Thomas Vachuska4830d392014-11-09 17:09:56 -08001362 // operate on exiting links:
Thomas Vachuska4830d392014-11-09 17:09:56 -08001363 link.exit()
Simon Hunt13bf9c82014-11-18 07:26:44 -08001364 .attr('stroke-dasharray', '3, 3')
1365 .style('opacity', 0.5)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001366 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -08001367 .duration(1500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001368 .attr({
Simon Hunt13bf9c82014-11-18 07:26:44 -08001369 'stroke-dasharray': '3, 12',
1370 stroke: config.topo.linkOutColor,
1371 'stroke-width': config.topo.linkOutWidth
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001372 })
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001373 .style('opacity', 0.0)
Thomas Vachuska4830d392014-11-09 17:09:56 -08001374 .remove();
Simon Hunte2575b62014-11-18 15:25:53 -08001375
1376 // NOTE: invoke a single tick to force the labels to position
1377 // onto their links.
1378 tick();
1379 }
1380
1381 function getLabelData() {
1382 // create the backing data for showing labels..
1383 var data = [];
1384 link.each(function (d) {
1385 if (d.label) {
1386 data.push({
1387 id: 'lab-' + d.key,
1388 key: d.key,
1389 label: d.label,
1390 ldata: d
1391 });
1392 }
1393 });
1394 return data;
1395 }
1396
1397 var linkLabelOffset = '0.3em';
1398
1399 function applyLinkLabels(data) {
1400 var entering;
1401
1402 linkLabel = linkLabelG.selectAll('.linkLabel')
1403 .data(data, function (d) { return d.id; });
1404
Simon Hunt56a2ea42014-11-19 12:39:31 -08001405 // for elements already existing, we need to update the text
1406 // and adjust the rectangle size to fit
1407 linkLabel.each(function (d) {
1408 var el = d3.select(this),
1409 rect = el.select('rect'),
1410 text = el.select('text');
1411 text.text(d.label);
1412 rect.attr(rectAroundText(el));
1413 });
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -08001414
Simon Hunte2575b62014-11-18 15:25:53 -08001415 entering = linkLabel.enter().append('g')
1416 .classed('linkLabel', true)
1417 .attr('id', function (d) { return d.id; });
1418
1419 entering.each(function (d) {
1420 var el = d3.select(this),
1421 rect,
1422 text,
1423 parms = {
1424 x1: d.ldata.x1,
1425 y1: d.ldata.y1,
1426 x2: d.ldata.x2,
1427 y2: d.ldata.y2
1428 };
1429
1430 d.el = el;
1431 rect = el.append('rect');
1432 text = el.append('text').text(d.label);
1433 rect.attr(rectAroundText(el));
1434 text.attr('dy', linkLabelOffset);
1435
1436 el.attr('transform', transformLabel(parms));
1437 });
1438
1439 // Remove any links that are no longer required.
1440 linkLabel.exit().remove();
1441 }
1442
1443 function rectAroundText(el) {
1444 var text = el.select('text'),
1445 box = text.node().getBBox();
1446
1447 // translate the bbox so that it is centered on [x,y]
1448 box.x = -box.width / 2;
1449 box.y = -box.height / 2;
1450
1451 // add padding
1452 box.x -= 1;
1453 box.width += 2;
1454 return box;
1455 }
1456
1457 function transformLabel(p) {
1458 var dx = p.x2 - p.x1,
1459 dy = p.y2 - p.y1,
1460 xMid = dx/2 + p.x1,
1461 yMid = dy/2 + p.y1;
Simon Hunte2575b62014-11-18 15:25:53 -08001462 return translate(xMid, yMid);
Simon Hunt99c13842014-11-06 18:23:12 -08001463 }
1464
1465 function createDeviceNode(device) {
1466 // start with the object as is
1467 var node = device,
Simon Huntbb282f52014-11-10 11:08:19 -08001468 type = device.type,
Simon Huntc72967b2014-11-20 09:21:42 -08001469 svgCls = type ? 'node device ' + type : 'node device',
1470 labels = device.labels || [];
1471
Simon Hunt99c13842014-11-06 18:23:12 -08001472 // Augment as needed...
1473 node.class = 'device';
Simon Huntbb282f52014-11-10 11:08:19 -08001474 node.svgClass = device.online ? svgCls + ' online' : svgCls;
Simon Hunt99c13842014-11-06 18:23:12 -08001475 positionNode(node);
Simon Hunt99c13842014-11-06 18:23:12 -08001476 return node;
1477 }
1478
Simon Hunt56d51852014-11-09 13:03:35 -08001479 function createHostNode(host) {
1480 // start with the object as is
1481 var node = host;
1482
1483 // Augment as needed...
1484 node.class = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001485 if (!node.type) {
Simon Hunt209155e2014-11-21 12:16:09 -08001486 node.type = 'endstation';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001487 }
Simon Hunt7fa116d2014-11-17 14:16:55 -08001488 node.svgClass = 'node host ' + node.type;
Simon Hunt56d51852014-11-09 13:03:35 -08001489 positionNode(node);
Simon Hunt56d51852014-11-09 13:03:35 -08001490 return node;
1491 }
1492
Simon Hunt62c47542014-11-22 22:16:32 -08001493 function positionNode(node, forUpdate) {
Simon Hunt99c13842014-11-06 18:23:12 -08001494 var meta = node.metaUi,
Simon Huntac9e24f2014-11-12 10:12:21 -08001495 x = meta && meta.x,
1496 y = meta && meta.y,
1497 xy;
Simon Hunt99c13842014-11-06 18:23:12 -08001498
Simon Huntac9e24f2014-11-12 10:12:21 -08001499 // If we have [x,y] already, use that...
Simon Hunt99c13842014-11-06 18:23:12 -08001500 if (x && y) {
1501 node.fixed = true;
Simon Hunt62c47542014-11-22 22:16:32 -08001502 node.px = node.x = x;
1503 node.py = node.y = y;
1504 //node.px = x;
1505 //node.py = y;
Simon Huntac9e24f2014-11-12 10:12:21 -08001506 return;
Simon Hunt99c13842014-11-06 18:23:12 -08001507 }
Simon Huntac9e24f2014-11-12 10:12:21 -08001508
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001509 var location = node.location;
1510 if (location && location.type === 'latlng') {
1511 var coord = geoMapProjection([location.lng, location.lat]);
1512 node.fixed = true;
Simon Hunt62c47542014-11-22 22:16:32 -08001513 node.px = node.x = coord[0];
1514 node.py = node.y = coord[1];
1515 //node.x = coord[0];
1516 //node.y = coord[1];
1517 return true;
1518 }
1519
1520 // if this is a node update (not a node add).. skip randomizer
1521 if (forUpdate) {
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001522 return;
1523 }
1524
Simon Huntac9e24f2014-11-12 10:12:21 -08001525 // Note: Placing incoming unpinned nodes at exactly the same point
1526 // (center of the view) causes them to explode outwards when
1527 // the force layout kicks in. So, we spread them out a bit
1528 // initially, to provide a more serene layout convergence.
1529 // Additionally, if the node is a host, we place it near
1530 // the device it is connected to.
1531
1532 function spread(s) {
1533 return Math.floor((Math.random() * s) - s/2);
1534 }
1535
1536 function randDim(dim) {
1537 return dim / 2 + spread(dim * 0.7071);
1538 }
1539
1540 function rand() {
1541 return {
1542 x: randDim(network.view.width()),
1543 y: randDim(network.view.height())
1544 };
1545 }
1546
1547 function near(node) {
1548 var min = 12,
1549 dx = spread(12),
1550 dy = spread(12);
1551 return {
1552 x: node.x + min + dx,
1553 y: node.y + min + dy
1554 };
1555 }
1556
1557 function getDevice(cp) {
1558 var d = network.lookup[cp.device];
1559 return d || rand();
1560 }
1561
1562 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
1563 $.extend(node, xy);
Simon Hunt99c13842014-11-06 18:23:12 -08001564 }
1565
Simon Hunt99c13842014-11-06 18:23:12 -08001566
Simon Huntc72967b2014-11-20 09:21:42 -08001567 function iconGlyphUrl(d) {
1568 var which = d.type || 'unknown';
1569 return '#' + which;
1570 }
1571
Simon Hunt99c13842014-11-06 18:23:12 -08001572 // returns the newly computed bounding box of the rectangle
1573 function adjustRectToFitText(n) {
1574 var text = n.select('text'),
1575 box = text.node().getBBox(),
1576 lab = config.labels;
1577
1578 text.attr('text-anchor', 'middle')
1579 .attr('y', '-0.8em')
1580 .attr('x', lab.imgPad/2);
1581
1582 // translate the bbox so that it is centered on [x,y]
1583 box.x = -box.width / 2;
1584 box.y = -box.height / 2;
1585
1586 // add padding
1587 box.x -= (lab.padLR + lab.imgPad/2);
1588 box.width += lab.padLR * 2 + lab.imgPad;
1589 box.y -= lab.padTB;
1590 box.height += lab.padTB * 2;
1591
1592 return box;
1593 }
1594
Simon Hunt1a9eff92014-11-07 11:06:34 -08001595 function mkSvgClass(d) {
1596 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
1597 }
1598
Simon Hunt7cd48f32014-11-09 23:42:50 -08001599 function hostLabel(d) {
1600 var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0;
1601 return d.labels[idx];
1602 }
1603 function deviceLabel(d) {
1604 var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0;
1605 return d.labels[idx];
1606 }
Simon Huntc72967b2014-11-20 09:21:42 -08001607 function trimLabel(label) {
1608 return (label && label.trim()) || '';
1609 }
1610
1611 function emptyBox() {
1612 return {
1613 x: -2,
1614 y: -2,
1615 width: 4,
1616 height: 4
1617 };
Simon Hunt7cd48f32014-11-09 23:42:50 -08001618 }
1619
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001620 function updateDeviceLabel(d) {
Simon Huntc72967b2014-11-20 09:21:42 -08001621 var label = trimLabel(deviceLabel(d)),
1622 noLabel = !label,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001623 node = d.el,
Simon Huntc72967b2014-11-20 09:21:42 -08001624 box,
1625 dx,
Simon Hunt395a70c2014-11-22 23:17:40 -08001626 dy,
1627 cfg = config.icons.device;
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001628
1629 node.select('text')
1630 .text(label)
1631 .style('opacity', 0)
1632 .transition()
1633 .style('opacity', 1);
1634
Simon Huntc72967b2014-11-20 09:21:42 -08001635 if (noLabel) {
1636 box = emptyBox();
Simon Hunt395a70c2014-11-22 23:17:40 -08001637 dx = -cfg.dim/2;
1638 dy = -cfg.dim/2;
Simon Huntc72967b2014-11-20 09:21:42 -08001639 } else {
1640 box = adjustRectToFitText(node);
Simon Hunt395a70c2014-11-22 23:17:40 -08001641 dx = box.x + cfg.xoff;
1642 dy = box.y + cfg.yoff;
Simon Huntc72967b2014-11-20 09:21:42 -08001643 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001644
1645 node.select('rect')
1646 .transition()
1647 .attr(box);
1648
Simon Huntc72967b2014-11-20 09:21:42 -08001649 node.select('g.deviceIcon')
Thomas Vachuska89543292014-11-19 11:28:33 -08001650 .transition()
Simon Huntc72967b2014-11-20 09:21:42 -08001651 .attr('transform', translate(dx, dy));
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001652 }
1653
1654 function updateHostLabel(d) {
1655 var label = hostLabel(d),
1656 host = d.el;
1657
1658 host.select('text').text(label);
1659 }
1660
Simon Hunt62c47542014-11-22 22:16:32 -08001661 // FIXME : fold this into updateNodes.
Simon Huntbb282f52014-11-10 11:08:19 -08001662 function updateHostState(hostData) {
1663 updateHostLabel(hostData);
Simon Huntbb282f52014-11-10 11:08:19 -08001664 }
1665
Simon Hunt6ac93f32014-11-13 12:17:27 -08001666 function nodeMouseOver(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001667 hovered = d;
Thomas Vachuska9edca302014-11-22 17:06:42 -08001668 requestTrafficForMode();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001669 }
1670
1671 function nodeMouseOut(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001672 hovered = null;
Thomas Vachuska9edca302014-11-22 17:06:42 -08001673 requestTrafficForMode();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001674 }
Simon Huntbb282f52014-11-10 11:08:19 -08001675
Simon Hunteb1514d2014-11-20 09:57:29 -08001676 function addHostIcon(node, radius, iid) {
Thomas Vachuska89543292014-11-19 11:28:33 -08001677 var dim = radius * 1.5,
1678 xlate = -dim / 2;
1679
Simon Hunteb1514d2014-11-20 09:57:29 -08001680 node.append('use').attr({
1681 class: 'glyphIcon hostIcon',
1682 transform: translate(xlate,xlate),
1683 'xlink:href': iid,
1684 width: dim,
1685 height: dim
1686 });
Thomas Vachuska89543292014-11-19 11:28:33 -08001687 }
1688
Simon Hunt99c13842014-11-06 18:23:12 -08001689 function updateNodes() {
1690 node = nodeG.selectAll('.node')
1691 .data(network.nodes, function (d) { return d.id; });
1692
Simon Hunt62c47542014-11-22 22:16:32 -08001693 // operate on existing nodes...
1694 node.filter('.device').each(function (d) {
1695 //var node = d3.select(this);
1696 var node = d.el;
1697 node.classed('online', d.online);
1698 updateDeviceLabel(d);
1699 positionNode(d, true);
1700 });
1701
1702 node.filter('.host').each(function (d) {
Simon Hunt62c47542014-11-22 22:16:32 -08001703 var node = d.el;
1704 // TODO: appropriate update of host visuals
1705 });
Simon Hunt99c13842014-11-06 18:23:12 -08001706
1707 // operate on entering nodes:
1708 var entering = node.enter()
1709 .append('g')
1710 .attr({
1711 id: function (d) { return safeId(d.id); },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001712 class: mkSvgClass,
Simon Hunt99c13842014-11-06 18:23:12 -08001713 transform: function (d) { return translate(d.x, d.y); },
1714 opacity: 0
1715 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08001716 .call(network.drag)
Simon Hunt6ac93f32014-11-13 12:17:27 -08001717 .on('mouseover', nodeMouseOver)
1718 .on('mouseout', nodeMouseOut)
Simon Hunt99c13842014-11-06 18:23:12 -08001719 .transition()
1720 .attr('opacity', 1);
1721
1722 // augment device nodes...
1723 entering.filter('.device').each(function (d) {
1724 var node = d3.select(this),
Simon Huntc72967b2014-11-20 09:21:42 -08001725 label = trimLabel(deviceLabel(d)),
1726 noLabel = !label,
Simon Hunt99c13842014-11-06 18:23:12 -08001727 box;
1728
Simon Hunt7cd48f32014-11-09 23:42:50 -08001729 // provide ref to element from backing data....
1730 d.el = node;
1731
Simon Hunt62c47542014-11-22 22:16:32 -08001732 node.append('rect').attr({ rx: 5, ry: 5 });
1733 node.append('text').text(label).attr('dy', '1.1em');
Simon Hunt99c13842014-11-06 18:23:12 -08001734 box = adjustRectToFitText(node);
Simon Hunta3dd9572014-11-20 15:22:41 -08001735 node.select('rect').attr(box);
Simon Huntc72967b2014-11-20 09:21:42 -08001736 addDeviceIcon(node, box, noLabel, iconGlyphUrl(d));
Simon Huntc7ee0662014-11-05 16:44:37 -08001737 });
Simon Hunt934c3ce2014-11-05 11:45:07 -08001738
Thomas Vachuska89543292014-11-19 11:28:33 -08001739
Simon Hunt56d51852014-11-09 13:03:35 -08001740 // augment host nodes...
1741 entering.filter('.host').each(function (d) {
1742 var node = d3.select(this),
Simon Hunt395a70c2014-11-22 23:17:40 -08001743 cfg = config.icons.host,
1744 r = cfg.radius[d.type] || cfg.defaultRadius,
Thomas Vachuska89543292014-11-19 11:28:33 -08001745 textDy = r + 10,
Simon Hunteb1514d2014-11-20 09:57:29 -08001746 iid = iconGlyphUrl(d);
Simon Hunt56d51852014-11-09 13:03:35 -08001747
Simon Hunt7cd48f32014-11-09 23:42:50 -08001748 // provide ref to element from backing data....
1749 d.el = node;
1750
Simon Hunt62c47542014-11-22 22:16:32 -08001751 node.append('circle').attr('r', r);
Simon Hunt7fa116d2014-11-17 14:16:55 -08001752
Simon Hunteb1514d2014-11-20 09:57:29 -08001753 if (iid) {
1754 addHostIcon(node, r, iid);
Simon Hunt7fa116d2014-11-17 14:16:55 -08001755 }
Simon Hunt56d51852014-11-09 13:03:35 -08001756
Simon Hunt56d51852014-11-09 13:03:35 -08001757 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001758 .text(hostLabel)
Thomas Vachuska89543292014-11-19 11:28:33 -08001759 .attr('dy', textDy)
Simon Hunt7cd48f32014-11-09 23:42:50 -08001760 .attr('text-anchor', 'middle');
Simon Hunt56d51852014-11-09 13:03:35 -08001761 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001762
Simon Hunt99c13842014-11-06 18:23:12 -08001763 // operate on both existing and new nodes, if necessary
Simon Huntb0ecfa52014-11-23 21:05:12 -08001764 updateDeviceColors();
Simon Huntc7ee0662014-11-05 16:44:37 -08001765
Simon Hunt99c13842014-11-06 18:23:12 -08001766 // operate on exiting nodes:
Simon Huntea80eb42014-11-11 13:46:57 -08001767 // Note that the node is removed after 2 seconds.
1768 // Sub element animations should be shorter than 2 seconds.
1769 var exiting = node.exit()
Simon Hunt44031102014-11-11 13:20:36 -08001770 .transition()
1771 .duration(2000)
Simon Huntea80eb42014-11-11 13:46:57 -08001772 .style('opacity', 0)
Simon Hunt99c13842014-11-06 18:23:12 -08001773 .remove();
Simon Huntea80eb42014-11-11 13:46:57 -08001774
1775 // host node exits....
1776 exiting.filter('.host').each(function (d) {
1777 var node = d3.select(this);
1778
1779 node.select('text')
1780 .style('opacity', 0.5)
1781 .transition()
1782 .duration(1000)
1783 .style('opacity', 0);
1784 // note, leave <g>.remove to remove this element
1785
Thomas Vachuska89543292014-11-19 11:28:33 -08001786 node.select('circle')
1787 .style('stroke-fill', '#555')
1788 .style('fill', '#888')
Simon Huntea80eb42014-11-11 13:46:57 -08001789 .style('opacity', 0.5)
1790 .transition()
1791 .duration(1500)
1792 .attr('r', 0);
1793 // note, leave <g>.remove to remove this element
1794
1795 });
1796
Simon Hunt62c47542014-11-22 22:16:32 -08001797 // TODO: device node exit animation
1798
1799 network.force.resume();
Simon Huntc7ee0662014-11-05 16:44:37 -08001800 }
1801
Simon Hunt95dad922014-11-24 09:43:31 -08001802 var dCol = {
1803 black: '#000',
1804 paleblue: '#acf',
1805 offwhite: '#ddd',
1806 midgrey: '#888',
1807 lightgrey: '#bbb',
1808 orange: '#f90'
1809 };
1810
Simon Huntb0ecfa52014-11-23 21:05:12 -08001811 // note: these are the device icon colors without affinity
Simon Hunt95dad922014-11-24 09:43:31 -08001812 var dColTheme = {
Simon Huntb0ecfa52014-11-23 21:05:12 -08001813 light: {
1814 online: {
Simon Hunt95dad922014-11-24 09:43:31 -08001815 glyph: dCol.black,
1816 rect: dCol.paleblue
Simon Huntb0ecfa52014-11-23 21:05:12 -08001817 },
1818 offline: {
Simon Hunt95dad922014-11-24 09:43:31 -08001819 glyph: dCol.midgrey,
1820 rect: dCol.lightgrey
Simon Huntb0ecfa52014-11-23 21:05:12 -08001821 }
1822 },
Simon Hunt95dad922014-11-24 09:43:31 -08001823 // TODO: theme
Simon Huntb0ecfa52014-11-23 21:05:12 -08001824 dark: {
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 }
1834 };
1835
1836 function devBaseColor(d) {
1837 var t = network.view.getTheme(),
1838 o = d.online ? 'online' : 'offline';
Simon Hunt95dad922014-11-24 09:43:31 -08001839 return dColTheme[t][o];
Simon Huntb0ecfa52014-11-23 21:05:12 -08001840 }
1841
1842 function setDeviceColor(d) {
1843 var o = d.online,
1844 s = d.el.classed('selected'),
1845 c = devBaseColor(d),
1846 a = instColor(d.master, o),
1847 g, r,
1848 icon = d.el.select('g.deviceIcon');
1849
1850 if (s) {
1851 g = c.glyph;
Simon Hunt95dad922014-11-24 09:43:31 -08001852 r = dColTheme.sel;
Simon Huntb0ecfa52014-11-23 21:05:12 -08001853 } else if (colorAffinity) {
1854 g = o ? a : c.glyph;
Simon Hunt95dad922014-11-24 09:43:31 -08001855 r = o ? dCol.offwhite : a;
Simon Huntb0ecfa52014-11-23 21:05:12 -08001856 } else {
1857 g = c.glyph;
1858 r = c.rect;
1859 }
1860
1861 icon.select('use')
1862 .style('fill', g);
1863 icon.select('rect')
1864 .style('fill', r);
1865 }
1866
Simon Huntc72967b2014-11-20 09:21:42 -08001867 function addDeviceIcon(node, box, noLabel, iid) {
1868 var cfg = config.icons.device,
1869 dx,
1870 dy,
1871 g;
1872
1873 if (noLabel) {
1874 box = emptyBox();
1875 dx = -cfg.dim/2;
1876 dy = -cfg.dim/2;
1877 } else {
1878 box = adjustRectToFitText(node);
Simon Hunt395a70c2014-11-22 23:17:40 -08001879 dx = box.x + cfg.xoff;
1880 dy = box.y + cfg.yoff;
Simon Huntc72967b2014-11-20 09:21:42 -08001881 }
1882
Simon Hunteb1514d2014-11-20 09:57:29 -08001883 g = node.append('g')
1884 .attr('class', 'glyphIcon deviceIcon')
Simon Huntc72967b2014-11-20 09:21:42 -08001885 .attr('transform', translate(dx, dy));
1886
1887 g.append('rect').attr({
1888 x: 0,
1889 y: 0,
1890 rx: cfg.rx,
1891 width: cfg.dim,
1892 height: cfg.dim
1893 });
1894
1895 g.append('use').attr({
1896 'xlink:href': iid,
1897 width: cfg.dim,
1898 height: cfg.dim
1899 });
1900
Simon Huntc72967b2014-11-20 09:21:42 -08001901 }
1902
Simon Hunt7b403bc2014-11-22 19:01:00 -08001903 function find(key, array, tag) {
1904 var _tag = tag || 'key',
1905 idx, n, d;
1906 for (idx = 0, n = array.length; idx < n; idx++) {
1907 d = array[idx];
1908 if (d[_tag] === key) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001909 return idx;
1910 }
1911 }
1912 return -1;
1913 }
1914
1915 function removeLinkElement(linkData) {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001916 var idx = find(linkData.key, network.links),
1917 removed;
1918 if (idx >=0) {
1919 // remove from links array
1920 removed = network.links.splice(idx, 1);
1921 // remove from lookup cache
1922 delete network.lookup[removed[0].key];
1923 updateLinks();
1924 network.force.resume();
1925 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001926 }
Simon Huntc7ee0662014-11-05 16:44:37 -08001927
Simon Hunt44031102014-11-11 13:20:36 -08001928 function removeHostElement(hostData) {
1929 // first, remove associated hostLink...
1930 removeLinkElement(hostData.linkData);
1931
1932 // remove from lookup cache
1933 delete network.lookup[hostData.id];
1934 // remove from nodes array
1935 var idx = find(hostData.id, network.nodes);
1936 network.nodes.splice(idx, 1);
1937 // remove from SVG
1938 updateNodes();
1939 network.force.resume();
1940 }
1941
1942
Simon Huntc7ee0662014-11-05 16:44:37 -08001943 function tick() {
1944 node.attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001945 transform: function (d) { return translate(d.x, d.y); }
Simon Huntc7ee0662014-11-05 16:44:37 -08001946 });
1947
1948 link.attr({
1949 x1: function (d) { return d.source.x; },
1950 y1: function (d) { return d.source.y; },
1951 x2: function (d) { return d.target.x; },
1952 y2: function (d) { return d.target.y; }
1953 });
Simon Hunte2575b62014-11-18 15:25:53 -08001954
1955 linkLabel.each(function (d) {
1956 var el = d3.select(this);
Thomas Vachuska4731f122014-11-20 04:56:19 -08001957 var lnk = findLinkById(d.key);
1958
1959 if (lnk) {
1960 var parms = {
Simon Hunte2575b62014-11-18 15:25:53 -08001961 x1: lnk.source.x,
1962 y1: lnk.source.y,
1963 x2: lnk.target.x,
1964 y2: lnk.target.y
1965 };
Thomas Vachuska4731f122014-11-20 04:56:19 -08001966 el.attr('transform', transformLabel(parms));
1967 }
Simon Hunte2575b62014-11-18 15:25:53 -08001968 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001969 }
Simon Hunt934c3ce2014-11-05 11:45:07 -08001970
1971 // ==============================
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001972 // Web-Socket for live data
1973
1974 function webSockUrl() {
1975 return document.location.toString()
1976 .replace(/\#.*/, '')
1977 .replace('http://', 'ws://')
1978 .replace('https://', 'wss://')
1979 .replace('index2.html', config.webSockUrl);
1980 }
1981
1982 webSock = {
1983 ws : null,
1984
1985 connect : function() {
1986 webSock.ws = new WebSocket(webSockUrl());
1987
1988 webSock.ws.onopen = function() {
Simon Hunt0c6d4192014-11-12 12:07:10 -08001989 noWebSock(false);
Thomas Vachuska47635c62014-11-22 01:21:36 -08001990 requestSummary();
Simon Huntb0ecfa52014-11-23 21:05:12 -08001991 showInstances();
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001992 };
1993
1994 webSock.ws.onmessage = function(m) {
1995 if (m.data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001996 wsTraceRx(m.data);
Thomas Vachuskad472c6e2014-11-07 19:11:05 -08001997 handleServerEvent(JSON.parse(m.data));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001998 }
1999 };
2000
2001 webSock.ws.onclose = function(m) {
2002 webSock.ws = null;
Simon Hunt0c6d4192014-11-12 12:07:10 -08002003 noWebSock(true);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002004 };
2005 },
2006
2007 send : function(text) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002008 if (text != null) {
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002009 webSock._send(text);
2010 }
2011 },
2012
2013 _send : function(message) {
2014 if (webSock.ws) {
2015 webSock.ws.send(message);
Simon Hunta255a2c2014-11-13 22:29:35 -08002016 } else if (config.useLiveData) {
Simon Hunt56d51852014-11-09 13:03:35 -08002017 network.view.alert('no web socket open\n\n' + message);
Simon Hunta255a2c2014-11-13 22:29:35 -08002018 } else {
2019 console.log('WS Send: ' + JSON.stringify(message));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002020 }
2021 }
2022
2023 };
2024
Simon Hunt0c6d4192014-11-12 12:07:10 -08002025 function noWebSock(b) {
2026 mask.style('display',b ? 'block' : 'none');
2027 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002028
2029 function sendMessage(evType, payload) {
2030 var toSend = {
Simon Huntbb282f52014-11-10 11:08:19 -08002031 event: evType,
2032 sid: ++sid,
2033 payload: payload
2034 },
2035 asText = JSON.stringify(toSend);
2036 wsTraceTx(asText);
2037 webSock.send(asText);
Simon Huntc76ae892014-11-18 17:31:51 -08002038
2039 // Temporary measure for debugging UI behavior ...
2040 if (!config.useLiveData) {
2041 handleTestSend(toSend);
2042 }
Simon Huntbb282f52014-11-10 11:08:19 -08002043 }
2044
2045 function wsTraceTx(msg) {
2046 wsTrace('tx', msg);
2047 }
2048 function wsTraceRx(msg) {
2049 wsTrace('rx', msg);
2050 }
2051 function wsTrace(rxtx, msg) {
Simon Huntbb282f52014-11-10 11:08:19 -08002052 console.log('[' + rxtx + '] ' + msg);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002053 }
2054
Simon Huntc76ae892014-11-18 17:31:51 -08002055 // NOTE: Temporary hardcoded example for showing detail pane
2056 // while we fine-
2057 // Probably should not merge this change...
2058 function handleTestSend(msg) {
2059 if (msg.event === 'requestDetails') {
2060 showDetails({
2061 event: 'showDetails',
2062 sid: 1001,
2063 payload: {
2064 "id": "of:0000ffffffffff09",
2065 "type": "roadm",
2066 "propOrder": [
2067 "Name",
2068 "Vendor",
2069 "H/W Version",
2070 "S/W Version",
2071 "-",
2072 "Latitude",
2073 "Longitude",
2074 "Ports"
2075 ],
2076 "props": {
2077 "Name": null,
2078 "Vendor": "Linc",
2079 "H/W Version": "OE",
2080 "S/W Version": "?",
2081 "-": "",
2082 "Latitude": "40.8",
2083 "Longitude": "73.1",
2084 "Ports": "2"
2085 }
2086 }
2087 });
2088 }
2089 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002090
2091 // ==============================
2092 // Selection stuff
2093
2094 function selectObject(obj, el) {
2095 var n,
Simon Hunt01095ff2014-11-13 16:37:29 -08002096 srcEv = d3.event.sourceEvent,
2097 meta = srcEv.metaKey,
2098 shift = srcEv.shiftKey;
2099
Simon Huntdeab4322014-11-13 18:49:07 -08002100 if ((panZoom() && !meta) || (!panZoom() && meta)) {
Simon Hunt01095ff2014-11-13 16:37:29 -08002101 return;
2102 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002103
2104 if (el) {
2105 n = d3.select(el);
2106 } else {
2107 node.each(function(d) {
2108 if (d == obj) {
2109 n = d3.select(el = this);
2110 }
2111 });
2112 }
2113 if (!n) return;
2114
Simon Hunt01095ff2014-11-13 16:37:29 -08002115 if (shift && n.classed('selected')) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002116 deselectObject(obj.id);
Simon Hunt61d04042014-11-11 17:27:16 -08002117 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002118 return;
2119 }
2120
Simon Hunt01095ff2014-11-13 16:37:29 -08002121 if (!shift) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002122 deselectAll();
2123 }
2124
Simon Huntc31d5692014-11-12 13:27:18 -08002125 selections[obj.id] = { obj: obj, el: el };
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002126 selectOrder.push(obj.id);
2127
2128 n.classed('selected', true);
Simon Huntb0ecfa52014-11-23 21:05:12 -08002129 updateDeviceColors(obj);
Simon Hunt61d04042014-11-11 17:27:16 -08002130 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002131 }
2132
2133 function deselectObject(id) {
Simon Huntc31d5692014-11-12 13:27:18 -08002134 var obj = selections[id],
2135 idx;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002136 if (obj) {
2137 d3.select(obj.el).classed('selected', false);
Simon Hunt61d04042014-11-11 17:27:16 -08002138 delete selections[id];
Simon Huntc31d5692014-11-12 13:27:18 -08002139 idx = $.inArray(id, selectOrder);
2140 if (idx >= 0) {
2141 selectOrder.splice(idx, 1);
2142 }
Simon Huntb0ecfa52014-11-23 21:05:12 -08002143 updateDeviceColors(obj.obj);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002144 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002145 }
2146
2147 function deselectAll() {
2148 // deselect all nodes in the network...
2149 node.classed('selected', false);
2150 selections = {};
2151 selectOrder = [];
Simon Huntb0ecfa52014-11-23 21:05:12 -08002152 updateDeviceColors();
Simon Hunt61d04042014-11-11 17:27:16 -08002153 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002154 }
2155
Simon Huntb0ecfa52014-11-23 21:05:12 -08002156 function updateDeviceColors(d) {
2157 if (d) {
2158 setDeviceColor(d);
2159 } else {
2160 node.filter('.device').each(function (d) {
2161 setDeviceColor(d);
2162 });
2163 }
Thomas Vachuska47635c62014-11-22 01:21:36 -08002164 }
2165
Simon Hunt61d04042014-11-11 17:27:16 -08002166 // update the state of the detail pane, based on current selections
2167 function updateDetailPane() {
2168 var nSel = selectOrder.length;
2169 if (!nSel) {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002170 emptySelect();
Simon Hunt61d04042014-11-11 17:27:16 -08002171 } else if (nSel === 1) {
2172 singleSelect();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002173 requestTrafficForMode();
Simon Hunt61d04042014-11-11 17:27:16 -08002174 } else {
2175 multiSelect();
2176 }
2177 }
2178
Thomas Vachuska9edca302014-11-22 17:06:42 -08002179 function emptySelect() {
2180 detailPane.hide();
2181 cancelTraffic();
2182 }
2183
Simon Hunt61d04042014-11-11 17:27:16 -08002184 function singleSelect() {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002185 // NOTE: detail is shown from showDetails event callback
Simon Hunt61d04042014-11-11 17:27:16 -08002186 requestDetails();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002187 requestTrafficForMode();
Simon Hunt61d04042014-11-11 17:27:16 -08002188 }
2189
2190 function multiSelect() {
Simon Huntb53e0682014-11-12 13:32:01 -08002191 populateMultiSelect();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002192 requestTrafficForMode();
Simon Huntb53e0682014-11-12 13:32:01 -08002193 }
2194
2195 function addSep(tbody) {
2196 var tr = tbody.append('tr');
2197 $('<hr>').appendTo(tr.append('td').attr('colspan', 2));
2198 }
2199
2200 function addProp(tbody, label, value) {
2201 var tr = tbody.append('tr');
2202
2203 tr.append('td')
2204 .attr('class', 'label')
2205 .text(label + ' :');
2206
2207 tr.append('td')
2208 .attr('class', 'value')
2209 .text(value);
2210 }
2211
2212 function populateMultiSelect() {
2213 detailPane.empty();
2214
Simon Hunta3dd9572014-11-20 15:22:41 -08002215 var title = detailPane.append('h3'),
2216 table = detailPane.append('table'),
2217 tbody = table.append('tbody');
Simon Huntb53e0682014-11-12 13:32:01 -08002218
Thomas Vachuska4731f122014-11-20 04:56:19 -08002219 title.text('Selected Nodes');
Simon Huntb53e0682014-11-12 13:32:01 -08002220
2221 selectOrder.forEach(function (d, i) {
2222 addProp(tbody, i+1, d);
2223 });
Simon Huntd72bc702014-11-13 18:38:04 -08002224
2225 addMultiSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08002226 }
2227
Thomas Vachuska47635c62014-11-22 01:21:36 -08002228 // TODO: refactor to consolidate with populateDetails
2229 function populateSummary(data) {
2230 summaryPane.empty();
2231
2232 var svg = summaryPane.append('svg'),
2233 iid = iconGlyphUrl(data);
2234
2235 var title = summaryPane.append('h2'),
2236 table = summaryPane.append('table'),
2237 tbody = table.append('tbody');
2238
2239 appendGlyph(svg, 0, 0, 40, iid);
2240
2241 svg.append('use')
2242 .attr({
2243 class: 'birdBadge',
2244 transform: translate(8,12),
2245 'xlink:href': '#bird',
2246 width: 24,
2247 height: 24,
2248 fill: '#fff'
2249 });
2250
2251 title.text('ONOS Summary');
2252
2253 data.propOrder.forEach(function(p) {
2254 if (p === '-') {
2255 addSep(tbody);
2256 } else {
2257 addProp(tbody, p, data.props[p]);
2258 }
2259 });
2260 }
2261
Simon Hunt61d04042014-11-11 17:27:16 -08002262 function populateDetails(data) {
2263 detailPane.empty();
2264
Simon Hunta6a9fe72014-11-20 11:17:12 -08002265 var svg = detailPane.append('svg'),
2266 iid = iconGlyphUrl(data);
2267
Simon Hunta3dd9572014-11-20 15:22:41 -08002268 var title = detailPane.append('h2'),
2269 table = detailPane.append('table'),
2270 tbody = table.append('tbody');
Simon Hunt61d04042014-11-11 17:27:16 -08002271
Simon Hunta6a9fe72014-11-20 11:17:12 -08002272 appendGlyph(svg, 0, 0, 40, iid);
2273 title.text(data.id);
Simon Hunt61d04042014-11-11 17:27:16 -08002274
2275 data.propOrder.forEach(function(p) {
2276 if (p === '-') {
2277 addSep(tbody);
2278 } else {
2279 addProp(tbody, p, data.props[p]);
2280 }
2281 });
Simon Huntd72bc702014-11-13 18:38:04 -08002282
Thomas Vachuska4731f122014-11-20 04:56:19 -08002283 addSingleSelectActions(data);
Simon Hunt61d04042014-11-11 17:27:16 -08002284 }
2285
Thomas Vachuska4731f122014-11-20 04:56:19 -08002286 function addSingleSelectActions(data) {
Simon Huntd72bc702014-11-13 18:38:04 -08002287 detailPane.append('hr');
2288 // always want to allow 'show traffic'
Thomas Vachuska4731f122014-11-20 04:56:19 -08002289 addAction(detailPane, 'Show Related Traffic', showTrafficAction);
2290
2291 if (data.type === 'switch') {
2292 addAction(detailPane, 'Show Device Flows', showDeviceLinkFlowsAction);
2293 }
Simon Huntd72bc702014-11-13 18:38:04 -08002294 }
2295
2296 function addMultiSelectActions() {
2297 detailPane.append('hr');
2298 // always want to allow 'show traffic'
Thomas Vachuska4731f122014-11-20 04:56:19 -08002299 addAction(detailPane, 'Show Related Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08002300 // if exactly two hosts are selected, also want 'add host intent'
2301 if (nSel() === 2 && allSelectionsClass('host')) {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002302 addAction(detailPane, 'Create Host-to-Host Flow', addHostIntentAction);
2303 } else if (nSel() >= 2 && allSelectionsClass('host')) {
2304 addAction(detailPane, 'Create Multi-Source Flow', addMultiSourceIntentAction);
Simon Huntd72bc702014-11-13 18:38:04 -08002305 }
2306 }
2307
Simon Hunta5e89142014-11-14 07:00:33 -08002308 function addAction(panel, text, cb) {
2309 panel.append('div')
Simon Huntd72bc702014-11-13 18:38:04 -08002310 .classed('actionBtn', true)
2311 .text(text)
2312 .on('click', cb);
2313 }
2314
2315
Paul Greysonfcba0e82014-11-13 10:21:16 -08002316 function zoomPan(scale, translate) {
2317 zoomPanContainer.attr("transform", "translate(" + translate + ")scale(" + scale + ")");
2318 // keep the map lines constant width while zooming
Thomas Vachuska89543292014-11-19 11:28:33 -08002319 bgImg.style("stroke-width", 2.0 / scale + "px");
Paul Greysonfcba0e82014-11-13 10:21:16 -08002320 }
2321
2322 function resetZoomPan() {
2323 zoomPan(1, [0,0]);
2324 zoom.scale(1).translate([0,0]);
2325 }
2326
2327 function setupZoomPan() {
2328 function zoomed() {
Simon Huntdeab4322014-11-13 18:49:07 -08002329 if (!panZoom() ^ !d3.event.sourceEvent.metaKey) {
Paul Greysonfcba0e82014-11-13 10:21:16 -08002330 zoomPan(d3.event.scale, d3.event.translate);
2331 }
2332 }
2333
2334 zoom = d3.behavior.zoom()
2335 .translate([0, 0])
2336 .scale(1)
2337 .scaleExtent([1, 8])
2338 .on("zoom", zoomed);
2339
2340 svg.call(zoom);
2341 }
2342
Simon Hunt61d04042014-11-11 17:27:16 -08002343 // ==============================
2344 // Test harness code
Simon Hunt56d51852014-11-09 13:03:35 -08002345
2346 function prepareScenario(view, ctx, dbg) {
2347 var sc = scenario,
2348 urlSc = sc.evDir + ctx + sc.evScenario;
2349
2350 if (!ctx) {
2351 view.alert("No scenario specified (null ctx)");
2352 return;
2353 }
2354
2355 sc.view = view;
2356 sc.ctx = ctx;
2357 sc.debug = dbg;
2358 sc.evNumber = 0;
2359
2360 d3.json(urlSc, function(err, data) {
Simon Huntbb282f52014-11-10 11:08:19 -08002361 var p = data && data.params || {},
2362 desc = data && data.description || null,
Simon Huntfc274c92014-11-11 11:05:46 -08002363 intro = data && data.title;
Simon Huntbb282f52014-11-10 11:08:19 -08002364
Simon Hunt56d51852014-11-09 13:03:35 -08002365 if (err) {
2366 view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err);
2367 } else {
2368 sc.params = p;
Simon Huntbb282f52014-11-10 11:08:19 -08002369 if (desc) {
2370 intro += '\n\n ' + desc.join('\n ');
2371 }
2372 view.alert(intro);
Simon Hunt56d51852014-11-09 13:03:35 -08002373 }
2374 });
2375
2376 }
2377
Simon Hunt01095ff2014-11-13 16:37:29 -08002378 // ==============================
2379 // Toggle Buttons in masthead
Simon Hunt0c6d4192014-11-12 12:07:10 -08002380
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002381 // TODO: toggle button (and other widgets in the masthead) should be provided
2382 // by the framework; not generated by the view.
2383
Thomas Vachuska47635c62014-11-22 01:21:36 -08002384 //var showInstances;
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002385
2386 function addButtonBar(view) {
2387 var bb = d3.select('#mast')
2388 .append('span').classed('right', true).attr('id', 'bb');
2389
Simon Hunta5e89142014-11-14 07:00:33 -08002390 function mkTogBtn(text, cb) {
2391 return bb.append('span')
2392 .classed('btn', true)
2393 .text(text)
2394 .on('click', cb);
2395 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002396
Thomas Vachuska47635c62014-11-22 01:21:36 -08002397 //showInstances = mkTogBtn('Show Instances', toggleInst);
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002398 }
2399
Simon Huntdeab4322014-11-13 18:49:07 -08002400 function panZoom() {
Simon Hunte5b71752014-11-18 20:06:07 -08002401 return false;
Simon Hunta5e89142014-11-14 07:00:33 -08002402 }
2403
Simon Hunt7fa116d2014-11-17 14:16:55 -08002404 function loadGlyphs(svg) {
2405 var defs = svg.append('defs');
2406 gly.defBird(defs);
Simon Huntc72967b2014-11-20 09:21:42 -08002407 gly.defGlyphs(defs);
Simon Huntb82f6902014-11-22 11:53:15 -08002408 gly.defBadges(defs);
Simon Hunt7fa116d2014-11-17 14:16:55 -08002409 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002410
Simon Hunt395a70c2014-11-22 23:17:40 -08002411 function sendUpdateMeta(d, store) {
2412 var metaUi = {},
2413 ll;
2414
2415 if (store) {
2416 ll = geoMapProjection.invert([d.x, d.y]);
Simon Hunt62c47542014-11-22 22:16:32 -08002417 metaUi = {
2418 x: d.x,
2419 y: d.y,
2420 lng: ll[0],
2421 lat: ll[1]
2422 };
Simon Hunt395a70c2014-11-22 23:17:40 -08002423 }
Simon Hunt62c47542014-11-22 22:16:32 -08002424 d.metaUi = metaUi;
2425 sendMessage('updateMeta', {
2426 id: d.id,
2427 'class': d.class,
2428 'memento': metaUi
2429 });
2430 }
2431
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002432 // ==============================
Simon Hunt142d0032014-11-04 20:13:09 -08002433 // View life-cycle callbacks
Simon Hunt195cb382014-11-03 17:50:51 -08002434
Simon Huntf67722a2014-11-10 09:32:06 -08002435 function preload(view, ctx, flags) {
Simon Hunt142d0032014-11-04 20:13:09 -08002436 var w = view.width(),
2437 h = view.height(),
Simon Huntc7ee0662014-11-05 16:44:37 -08002438 fcfg = config.force,
2439 fpad = fcfg.pad,
2440 forceDim = [w - 2*fpad, h - 2*fpad];
Simon Hunt195cb382014-11-03 17:50:51 -08002441
Simon Hunt142d0032014-11-04 20:13:09 -08002442 // NOTE: view.$div is a D3 selection of the view's div
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002443 var viewBox = '0 0 ' + config.logicalSize + ' ' + config.logicalSize;
2444 svg = view.$div.append('svg').attr('viewBox', viewBox);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002445 setSize(svg, view);
2446
Simon Hunt7fa116d2014-11-17 14:16:55 -08002447 loadGlyphs(svg);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002448
Paul Greysonfcba0e82014-11-13 10:21:16 -08002449 zoomPanContainer = svg.append('g').attr('id', 'zoomPanContainer');
Paul Greysonfcba0e82014-11-13 10:21:16 -08002450 setupZoomPan();
2451
Simon Hunt1a9eff92014-11-07 11:06:34 -08002452 // add blue glow filter to svg layer
Paul Greysonfcba0e82014-11-13 10:21:16 -08002453 d3u.appendGlow(zoomPanContainer);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002454
Simon Huntc7ee0662014-11-05 16:44:37 -08002455 // group for the topology
Paul Greysonfcba0e82014-11-13 10:21:16 -08002456 topoG = zoomPanContainer.append('g')
Simon Huntd3b7d512014-11-12 15:48:41 -08002457 .attr('id', 'topo-G')
Simon Huntc7ee0662014-11-05 16:44:37 -08002458 .attr('transform', fcfg.translate());
2459
Simon Hunte2575b62014-11-18 15:25:53 -08002460 // subgroups for links, link labels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002461 linkG = topoG.append('g').attr('id', 'links');
Simon Hunte2575b62014-11-18 15:25:53 -08002462 linkLabelG = topoG.append('g').attr('id', 'linkLabels');
Simon Huntc7ee0662014-11-05 16:44:37 -08002463 nodeG = topoG.append('g').attr('id', 'nodes');
2464
Simon Hunte2575b62014-11-18 15:25:53 -08002465 // selection of links, linkLabels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002466 link = linkG.selectAll('.link');
Simon Hunte2575b62014-11-18 15:25:53 -08002467 linkLabel = linkLabelG.selectAll('.linkLabel');
Simon Huntc7ee0662014-11-05 16:44:37 -08002468 node = nodeG.selectAll('.node');
2469
Simon Hunt7cd48f32014-11-09 23:42:50 -08002470 function chrg(d) {
2471 return fcfg.charge[d.class] || -12000;
2472 }
Simon Hunt99c13842014-11-06 18:23:12 -08002473 function ldist(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002474 return fcfg.linkDistance[d.type] || 50;
Simon Hunt99c13842014-11-06 18:23:12 -08002475 }
2476 function lstrg(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002477 // 0.0 - 1.0
2478 return fcfg.linkStrength[d.type] || 1.0;
Simon Hunt99c13842014-11-06 18:23:12 -08002479 }
2480
Simon Hunt1a9eff92014-11-07 11:06:34 -08002481 function selectCb(d, self) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002482 selectObject(d, self);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002483 }
2484
2485 function atDragEnd(d, self) {
Simon Hunt56d51852014-11-09 13:03:35 -08002486 // once we've finished moving, pin the node in position
2487 d.fixed = true;
2488 d3.select(self).classed('fixed', true);
2489 if (config.useLiveData) {
Simon Hunt395a70c2014-11-22 23:17:40 -08002490 sendUpdateMeta(d, true);
Simon Hunta255a2c2014-11-13 22:29:35 -08002491 } else {
2492 console.log('Moving node ' + d.id + ' to [' + d.x + ',' + d.y + ']');
Simon Hunt1a9eff92014-11-07 11:06:34 -08002493 }
2494 }
2495
Simon Huntc7ee0662014-11-05 16:44:37 -08002496 // set up the force layout
2497 network.force = d3.layout.force()
2498 .size(forceDim)
2499 .nodes(network.nodes)
2500 .links(network.links)
Simon Hunt7cd48f32014-11-09 23:42:50 -08002501 .gravity(0.4)
2502 .friction(0.7)
2503 .charge(chrg)
Simon Hunt99c13842014-11-06 18:23:12 -08002504 .linkDistance(ldist)
2505 .linkStrength(lstrg)
Simon Huntc7ee0662014-11-05 16:44:37 -08002506 .on('tick', tick);
Simon Hunt195cb382014-11-03 17:50:51 -08002507
Simon Hunt01095ff2014-11-13 16:37:29 -08002508 network.drag = d3u.createDragBehavior(network.force,
Simon Huntdeab4322014-11-13 18:49:07 -08002509 selectCb, atDragEnd, panZoom);
Simon Hunt0c6d4192014-11-12 12:07:10 -08002510
2511 // create mask layer for when we lose connection to server.
Simon Hunta5e89142014-11-14 07:00:33 -08002512 // TODO: this should be part of the framework
Simon Hunt0c6d4192014-11-12 12:07:10 -08002513 mask = view.$div.append('div').attr('id','topo-mask');
2514 para(mask, 'Oops!');
2515 para(mask, 'Web-socket connection to server closed...');
2516 para(mask, 'Try refreshing the page.');
Simon Hunt12ce12e2014-11-15 21:13:19 -08002517
2518 mask.append('svg')
2519 .attr({
2520 id: 'mask-bird',
2521 width: w,
2522 height: h
2523 })
2524 .append('g')
2525 .attr('transform', birdTranslate(w, h))
2526 .style('opacity', 0.3)
2527 .append('use')
2528 .attr({
2529 'xlink:href': '#bird',
2530 width: config.birdDim,
2531 height: config.birdDim,
2532 fill: '#111'
Thomas Vachuska89543292014-11-19 11:28:33 -08002533 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08002534 }
Simon Hunt195cb382014-11-03 17:50:51 -08002535
Simon Hunt01095ff2014-11-13 16:37:29 -08002536 function para(sel, text) {
2537 sel.append('p').text(text);
2538 }
2539
2540
Simon Hunt56d51852014-11-09 13:03:35 -08002541 function load(view, ctx, flags) {
Simon Huntf67722a2014-11-10 09:32:06 -08002542 // resize, in case the window was resized while we were not loaded
2543 resize(view, ctx, flags);
2544
Simon Hunt99c13842014-11-06 18:23:12 -08002545 // cache the view token, so network topo functions can access it
2546 network.view = view;
Simon Hunt56d51852014-11-09 13:03:35 -08002547 config.useLiveData = !flags.local;
2548
2549 if (!config.useLiveData) {
2550 prepareScenario(view, ctx, flags.debug);
2551 }
Simon Hunt99c13842014-11-06 18:23:12 -08002552
2553 // set our radio buttons and key bindings
Simon Hunt9462e8c2014-11-14 17:28:09 -08002554 layerBtnSet = view.setRadio(layerButtons);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002555 view.setKeys(keyDispatch);
Simon Hunt195cb382014-11-03 17:50:51 -08002556
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002557 // patch in our "button bar" for now
2558 // TODO: implement a more official frameworky way of doing this..
2559 addButtonBar(view);
2560
Simon Huntd3b7d512014-11-12 15:48:41 -08002561 // Load map data asynchronously; complete startup after that..
2562 loadGeoJsonData();
Simon Hunta255a2c2014-11-13 22:29:35 -08002563 }
2564
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002565 function startAntTimer() {
Thomas Vachuska4731f122014-11-20 04:56:19 -08002566 if (!antTimer) {
2567 var pulses = [5, 3, 1.2, 3],
2568 pulse = 0;
2569 antTimer = setInterval(function () {
2570 pulse = pulse + 1;
2571 pulse = pulse === pulses.length ? 0 : pulse;
2572 d3.selectAll('.animated').style('stroke-width', pulses[pulse]);
2573 }, 200);
2574 }
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002575 }
2576
2577 function stopAntTimer() {
Simon Hunta255a2c2014-11-13 22:29:35 -08002578 if (antTimer) {
2579 clearInterval(antTimer);
2580 antTimer = null;
2581 }
Simon Huntd3b7d512014-11-12 15:48:41 -08002582 }
2583
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002584 function unload(view, ctx, flags) {
2585 stopAntTimer();
2586 }
2587
Simon Huntd3b7d512014-11-12 15:48:41 -08002588 // TODO: move these to config/state portion of script
Simon Hunta6a9fe72014-11-20 11:17:12 -08002589 var geoJsonUrl = 'json/map/continental_us.json',
Simon Huntd3b7d512014-11-12 15:48:41 -08002590 geoJson;
2591
2592 function loadGeoJsonData() {
2593 d3.json(geoJsonUrl, function (err, data) {
2594 if (err) {
2595 // fall back to USA map background
2596 loadStaticMap();
2597 } else {
2598 geoJson = data;
2599 loadGeoMap();
2600 }
2601
2602 // finally, connect to the server...
2603 if (config.useLiveData) {
2604 webSock.connect();
2605 }
2606 });
2607 }
2608
2609 function showBg() {
2610 return config.options.showBackground ? 'visible' : 'hidden';
2611 }
2612
2613 function loadStaticMap() {
2614 fnTrace('loadStaticMap', config.backgroundUrl);
2615 var w = network.view.width(),
2616 h = network.view.height();
2617
2618 // load the background image
2619 bgImg = svg.insert('svg:image', '#topo-G')
2620 .attr({
2621 id: 'topo-bg',
2622 width: w,
2623 height: h,
2624 'xlink:href': config.backgroundUrl
2625 })
2626 .style({
2627 visibility: showBg()
2628 });
2629 }
2630
2631 function loadGeoMap() {
2632 fnTrace('loadGeoMap', geoJsonUrl);
Simon Huntd3b7d512014-11-12 15:48:41 -08002633
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002634 // extracts the topojson data into geocoordinate-based geometry
2635 var topoData = topojson.feature(geoJson, geoJson.objects.states);
Simon Huntd3b7d512014-11-12 15:48:41 -08002636
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002637 // see: http://bl.ocks.org/mbostock/4707858
2638 geoMapProjection = d3.geo.mercator();
2639 var path = d3.geo.path().projection(geoMapProjection);
Simon Huntd3b7d512014-11-12 15:48:41 -08002640
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002641 geoMapProjection
2642 .scale(1)
2643 .translate([0, 0]);
Simon Huntd3b7d512014-11-12 15:48:41 -08002644
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002645 // [[x1,y1],[x2,y2]]
2646 var b = path.bounds(topoData);
Paul Greysonfcba0e82014-11-13 10:21:16 -08002647 // size map to 95% of minimum dimension to fill space
2648 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 -08002649 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 -08002650
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002651 geoMapProjection
2652 .scale(s)
2653 .translate(t);
2654
Paul Greysonfcba0e82014-11-13 10:21:16 -08002655 bgImg = zoomPanContainer.insert("g", '#topo-G');
Thomas Vachuska89543292014-11-19 11:28:33 -08002656 bgImg.attr('id', 'map').selectAll('path')
2657 .data(topoData.features)
2658 .enter()
2659 .append('path')
2660 .attr('d', path);
Simon Hunt195cb382014-11-03 17:50:51 -08002661 }
2662
Simon Huntf67722a2014-11-10 09:32:06 -08002663 function resize(view, ctx, flags) {
Simon Hunt12ce12e2014-11-15 21:13:19 -08002664 var w = view.width(),
2665 h = view.height();
2666
Simon Hunt934c3ce2014-11-05 11:45:07 -08002667 setSize(svg, view);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002668
2669 d3.select('#mask-bird').attr({ width: w, height: h})
2670 .select('g').attr('transform', birdTranslate(w, h));
Simon Hunt142d0032014-11-04 20:13:09 -08002671 }
2672
Simon Hunt8f40cce2014-11-23 15:57:30 -08002673 function theme(view, ctx, flags) {
2674 updateInstances();
Simon Huntb0ecfa52014-11-23 21:05:12 -08002675 updateDeviceColors();
Simon Hunt8f40cce2014-11-23 15:57:30 -08002676 }
2677
Simon Hunt12ce12e2014-11-15 21:13:19 -08002678 function birdTranslate(w, h) {
2679 var bdim = config.birdDim;
2680 return 'translate('+((w-bdim)*.4)+','+((h-bdim)*.1)+')';
2681 }
Simon Hunt142d0032014-11-04 20:13:09 -08002682
2683 // ==============================
2684 // View registration
Simon Hunt195cb382014-11-03 17:50:51 -08002685
Simon Hunt25248912014-11-04 11:25:48 -08002686 onos.ui.addView('topo', {
Simon Hunt142d0032014-11-04 20:13:09 -08002687 preload: preload,
2688 load: load,
Simon Hunta255a2c2014-11-13 22:29:35 -08002689 unload: unload,
Simon Hunt8f40cce2014-11-23 15:57:30 -08002690 resize: resize,
2691 theme: theme
Simon Hunt195cb382014-11-03 17:50:51 -08002692 });
2693
Thomas Vachuska47635c62014-11-22 01:21:36 -08002694 summaryPane = onos.ui.addFloatingPanel('topo-summary');
Simon Hunt61d04042014-11-11 17:27:16 -08002695 detailPane = onos.ui.addFloatingPanel('topo-detail');
Simon Hunta5e89142014-11-14 07:00:33 -08002696 oiBox = onos.ui.addFloatingPanel('topo-oibox', 'TL');
Simon Huntb82f6902014-11-22 11:53:15 -08002697 oiBox.width(20);
Simon Hunt61d04042014-11-11 17:27:16 -08002698
Simon Hunt195cb382014-11-03 17:50:51 -08002699}(ONOS));