blob: 1acfd76dad99ed6f057cab4fa8e5271fe71e814e [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 Huntb0ecfa52014-11-23 21:05:12 -08001802 // note: these are the device icon colors without affinity
1803 var deviceColor = {
1804 sel: '#f90',
1805 light: {
1806 online: {
1807 glyph: '#000',
1808 rect: '#ddd',
1809 },
1810 offline: {
1811 glyph: '#888',
1812 rect: '#bbb'
1813 }
1814 },
1815 dark: {
1816 online: {
1817 glyph: '#000',
1818 rect: '#ddd'
1819 },
1820 offline: {
1821 glyph: '#888',
1822 rect: '#bbb'
1823 }
1824 }
1825 };
1826
1827 function devBaseColor(d) {
1828 var t = network.view.getTheme(),
1829 o = d.online ? 'online' : 'offline';
1830 return deviceColor[t][o];
1831 }
1832
1833 function setDeviceColor(d) {
1834 var o = d.online,
1835 s = d.el.classed('selected'),
1836 c = devBaseColor(d),
1837 a = instColor(d.master, o),
1838 g, r,
1839 icon = d.el.select('g.deviceIcon');
1840
1841 if (s) {
1842 g = c.glyph;
1843 r = deviceColor.sel;
1844 } else if (colorAffinity) {
1845 g = o ? a : c.glyph;
1846 r = o ? c.rect : a;
1847 } else {
1848 g = c.glyph;
1849 r = c.rect;
1850 }
1851
1852 icon.select('use')
1853 .style('fill', g);
1854 icon.select('rect')
1855 .style('fill', r);
1856 }
1857
Simon Huntc72967b2014-11-20 09:21:42 -08001858 function addDeviceIcon(node, box, noLabel, iid) {
1859 var cfg = config.icons.device,
1860 dx,
1861 dy,
1862 g;
1863
1864 if (noLabel) {
1865 box = emptyBox();
1866 dx = -cfg.dim/2;
1867 dy = -cfg.dim/2;
1868 } else {
1869 box = adjustRectToFitText(node);
Simon Hunt395a70c2014-11-22 23:17:40 -08001870 dx = box.x + cfg.xoff;
1871 dy = box.y + cfg.yoff;
Simon Huntc72967b2014-11-20 09:21:42 -08001872 }
1873
Simon Hunteb1514d2014-11-20 09:57:29 -08001874 g = node.append('g')
1875 .attr('class', 'glyphIcon deviceIcon')
Simon Huntc72967b2014-11-20 09:21:42 -08001876 .attr('transform', translate(dx, dy));
1877
1878 g.append('rect').attr({
1879 x: 0,
1880 y: 0,
1881 rx: cfg.rx,
1882 width: cfg.dim,
1883 height: cfg.dim
1884 });
1885
1886 g.append('use').attr({
1887 'xlink:href': iid,
1888 width: cfg.dim,
1889 height: cfg.dim
1890 });
1891
Simon Huntc72967b2014-11-20 09:21:42 -08001892 }
1893
Simon Hunt7b403bc2014-11-22 19:01:00 -08001894 function find(key, array, tag) {
1895 var _tag = tag || 'key',
1896 idx, n, d;
1897 for (idx = 0, n = array.length; idx < n; idx++) {
1898 d = array[idx];
1899 if (d[_tag] === key) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001900 return idx;
1901 }
1902 }
1903 return -1;
1904 }
1905
1906 function removeLinkElement(linkData) {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001907 var idx = find(linkData.key, network.links),
1908 removed;
1909 if (idx >=0) {
1910 // remove from links array
1911 removed = network.links.splice(idx, 1);
1912 // remove from lookup cache
1913 delete network.lookup[removed[0].key];
1914 updateLinks();
1915 network.force.resume();
1916 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001917 }
Simon Huntc7ee0662014-11-05 16:44:37 -08001918
Simon Hunt44031102014-11-11 13:20:36 -08001919 function removeHostElement(hostData) {
1920 // first, remove associated hostLink...
1921 removeLinkElement(hostData.linkData);
1922
1923 // remove from lookup cache
1924 delete network.lookup[hostData.id];
1925 // remove from nodes array
1926 var idx = find(hostData.id, network.nodes);
1927 network.nodes.splice(idx, 1);
1928 // remove from SVG
1929 updateNodes();
1930 network.force.resume();
1931 }
1932
1933
Simon Huntc7ee0662014-11-05 16:44:37 -08001934 function tick() {
1935 node.attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001936 transform: function (d) { return translate(d.x, d.y); }
Simon Huntc7ee0662014-11-05 16:44:37 -08001937 });
1938
1939 link.attr({
1940 x1: function (d) { return d.source.x; },
1941 y1: function (d) { return d.source.y; },
1942 x2: function (d) { return d.target.x; },
1943 y2: function (d) { return d.target.y; }
1944 });
Simon Hunte2575b62014-11-18 15:25:53 -08001945
1946 linkLabel.each(function (d) {
1947 var el = d3.select(this);
Thomas Vachuska4731f122014-11-20 04:56:19 -08001948 var lnk = findLinkById(d.key);
1949
1950 if (lnk) {
1951 var parms = {
Simon Hunte2575b62014-11-18 15:25:53 -08001952 x1: lnk.source.x,
1953 y1: lnk.source.y,
1954 x2: lnk.target.x,
1955 y2: lnk.target.y
1956 };
Thomas Vachuska4731f122014-11-20 04:56:19 -08001957 el.attr('transform', transformLabel(parms));
1958 }
Simon Hunte2575b62014-11-18 15:25:53 -08001959 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001960 }
Simon Hunt934c3ce2014-11-05 11:45:07 -08001961
1962 // ==============================
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001963 // Web-Socket for live data
1964
1965 function webSockUrl() {
1966 return document.location.toString()
1967 .replace(/\#.*/, '')
1968 .replace('http://', 'ws://')
1969 .replace('https://', 'wss://')
1970 .replace('index2.html', config.webSockUrl);
1971 }
1972
1973 webSock = {
1974 ws : null,
1975
1976 connect : function() {
1977 webSock.ws = new WebSocket(webSockUrl());
1978
1979 webSock.ws.onopen = function() {
Simon Hunt0c6d4192014-11-12 12:07:10 -08001980 noWebSock(false);
Thomas Vachuska47635c62014-11-22 01:21:36 -08001981 requestSummary();
Simon Huntb0ecfa52014-11-23 21:05:12 -08001982 showInstances();
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001983 };
1984
1985 webSock.ws.onmessage = function(m) {
1986 if (m.data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001987 wsTraceRx(m.data);
Thomas Vachuskad472c6e2014-11-07 19:11:05 -08001988 handleServerEvent(JSON.parse(m.data));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001989 }
1990 };
1991
1992 webSock.ws.onclose = function(m) {
1993 webSock.ws = null;
Simon Hunt0c6d4192014-11-12 12:07:10 -08001994 noWebSock(true);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001995 };
1996 },
1997
1998 send : function(text) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001999 if (text != null) {
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002000 webSock._send(text);
2001 }
2002 },
2003
2004 _send : function(message) {
2005 if (webSock.ws) {
2006 webSock.ws.send(message);
Simon Hunta255a2c2014-11-13 22:29:35 -08002007 } else if (config.useLiveData) {
Simon Hunt56d51852014-11-09 13:03:35 -08002008 network.view.alert('no web socket open\n\n' + message);
Simon Hunta255a2c2014-11-13 22:29:35 -08002009 } else {
2010 console.log('WS Send: ' + JSON.stringify(message));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002011 }
2012 }
2013
2014 };
2015
Simon Hunt0c6d4192014-11-12 12:07:10 -08002016 function noWebSock(b) {
2017 mask.style('display',b ? 'block' : 'none');
2018 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002019
2020 function sendMessage(evType, payload) {
2021 var toSend = {
Simon Huntbb282f52014-11-10 11:08:19 -08002022 event: evType,
2023 sid: ++sid,
2024 payload: payload
2025 },
2026 asText = JSON.stringify(toSend);
2027 wsTraceTx(asText);
2028 webSock.send(asText);
Simon Huntc76ae892014-11-18 17:31:51 -08002029
2030 // Temporary measure for debugging UI behavior ...
2031 if (!config.useLiveData) {
2032 handleTestSend(toSend);
2033 }
Simon Huntbb282f52014-11-10 11:08:19 -08002034 }
2035
2036 function wsTraceTx(msg) {
2037 wsTrace('tx', msg);
2038 }
2039 function wsTraceRx(msg) {
2040 wsTrace('rx', msg);
2041 }
2042 function wsTrace(rxtx, msg) {
Simon Huntbb282f52014-11-10 11:08:19 -08002043 console.log('[' + rxtx + '] ' + msg);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002044 }
2045
Simon Huntc76ae892014-11-18 17:31:51 -08002046 // NOTE: Temporary hardcoded example for showing detail pane
2047 // while we fine-
2048 // Probably should not merge this change...
2049 function handleTestSend(msg) {
2050 if (msg.event === 'requestDetails') {
2051 showDetails({
2052 event: 'showDetails',
2053 sid: 1001,
2054 payload: {
2055 "id": "of:0000ffffffffff09",
2056 "type": "roadm",
2057 "propOrder": [
2058 "Name",
2059 "Vendor",
2060 "H/W Version",
2061 "S/W Version",
2062 "-",
2063 "Latitude",
2064 "Longitude",
2065 "Ports"
2066 ],
2067 "props": {
2068 "Name": null,
2069 "Vendor": "Linc",
2070 "H/W Version": "OE",
2071 "S/W Version": "?",
2072 "-": "",
2073 "Latitude": "40.8",
2074 "Longitude": "73.1",
2075 "Ports": "2"
2076 }
2077 }
2078 });
2079 }
2080 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002081
2082 // ==============================
2083 // Selection stuff
2084
2085 function selectObject(obj, el) {
2086 var n,
Simon Hunt01095ff2014-11-13 16:37:29 -08002087 srcEv = d3.event.sourceEvent,
2088 meta = srcEv.metaKey,
2089 shift = srcEv.shiftKey;
2090
Simon Huntdeab4322014-11-13 18:49:07 -08002091 if ((panZoom() && !meta) || (!panZoom() && meta)) {
Simon Hunt01095ff2014-11-13 16:37:29 -08002092 return;
2093 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002094
2095 if (el) {
2096 n = d3.select(el);
2097 } else {
2098 node.each(function(d) {
2099 if (d == obj) {
2100 n = d3.select(el = this);
2101 }
2102 });
2103 }
2104 if (!n) return;
2105
Simon Hunt01095ff2014-11-13 16:37:29 -08002106 if (shift && n.classed('selected')) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002107 deselectObject(obj.id);
Simon Hunt61d04042014-11-11 17:27:16 -08002108 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002109 return;
2110 }
2111
Simon Hunt01095ff2014-11-13 16:37:29 -08002112 if (!shift) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002113 deselectAll();
2114 }
2115
Simon Huntc31d5692014-11-12 13:27:18 -08002116 selections[obj.id] = { obj: obj, el: el };
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002117 selectOrder.push(obj.id);
2118
2119 n.classed('selected', true);
Simon Huntb0ecfa52014-11-23 21:05:12 -08002120 updateDeviceColors(obj);
Simon Hunt61d04042014-11-11 17:27:16 -08002121 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002122 }
2123
2124 function deselectObject(id) {
Simon Huntc31d5692014-11-12 13:27:18 -08002125 var obj = selections[id],
2126 idx;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002127 if (obj) {
2128 d3.select(obj.el).classed('selected', false);
Simon Hunt61d04042014-11-11 17:27:16 -08002129 delete selections[id];
Simon Huntc31d5692014-11-12 13:27:18 -08002130 idx = $.inArray(id, selectOrder);
2131 if (idx >= 0) {
2132 selectOrder.splice(idx, 1);
2133 }
Simon Huntb0ecfa52014-11-23 21:05:12 -08002134 updateDeviceColors(obj.obj);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002135 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002136 }
2137
2138 function deselectAll() {
2139 // deselect all nodes in the network...
2140 node.classed('selected', false);
2141 selections = {};
2142 selectOrder = [];
Simon Huntb0ecfa52014-11-23 21:05:12 -08002143 updateDeviceColors();
Simon Hunt61d04042014-11-11 17:27:16 -08002144 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002145 }
2146
Simon Huntb0ecfa52014-11-23 21:05:12 -08002147 function updateDeviceColors(d) {
2148 if (d) {
2149 setDeviceColor(d);
2150 } else {
2151 node.filter('.device').each(function (d) {
2152 setDeviceColor(d);
2153 });
2154 }
Thomas Vachuska47635c62014-11-22 01:21:36 -08002155 }
2156
Simon Hunt61d04042014-11-11 17:27:16 -08002157 // update the state of the detail pane, based on current selections
2158 function updateDetailPane() {
2159 var nSel = selectOrder.length;
2160 if (!nSel) {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002161 emptySelect();
Simon Hunt61d04042014-11-11 17:27:16 -08002162 } else if (nSel === 1) {
2163 singleSelect();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002164 requestTrafficForMode();
Simon Hunt61d04042014-11-11 17:27:16 -08002165 } else {
2166 multiSelect();
2167 }
2168 }
2169
Thomas Vachuska9edca302014-11-22 17:06:42 -08002170 function emptySelect() {
2171 detailPane.hide();
2172 cancelTraffic();
2173 }
2174
Simon Hunt61d04042014-11-11 17:27:16 -08002175 function singleSelect() {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002176 // NOTE: detail is shown from showDetails event callback
Simon Hunt61d04042014-11-11 17:27:16 -08002177 requestDetails();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002178 requestTrafficForMode();
Simon Hunt61d04042014-11-11 17:27:16 -08002179 }
2180
2181 function multiSelect() {
Simon Huntb53e0682014-11-12 13:32:01 -08002182 populateMultiSelect();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002183 requestTrafficForMode();
Simon Huntb53e0682014-11-12 13:32:01 -08002184 }
2185
2186 function addSep(tbody) {
2187 var tr = tbody.append('tr');
2188 $('<hr>').appendTo(tr.append('td').attr('colspan', 2));
2189 }
2190
2191 function addProp(tbody, label, value) {
2192 var tr = tbody.append('tr');
2193
2194 tr.append('td')
2195 .attr('class', 'label')
2196 .text(label + ' :');
2197
2198 tr.append('td')
2199 .attr('class', 'value')
2200 .text(value);
2201 }
2202
2203 function populateMultiSelect() {
2204 detailPane.empty();
2205
Simon Hunta3dd9572014-11-20 15:22:41 -08002206 var title = detailPane.append('h3'),
2207 table = detailPane.append('table'),
2208 tbody = table.append('tbody');
Simon Huntb53e0682014-11-12 13:32:01 -08002209
Thomas Vachuska4731f122014-11-20 04:56:19 -08002210 title.text('Selected Nodes');
Simon Huntb53e0682014-11-12 13:32:01 -08002211
2212 selectOrder.forEach(function (d, i) {
2213 addProp(tbody, i+1, d);
2214 });
Simon Huntd72bc702014-11-13 18:38:04 -08002215
2216 addMultiSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08002217 }
2218
Thomas Vachuska47635c62014-11-22 01:21:36 -08002219 // TODO: refactor to consolidate with populateDetails
2220 function populateSummary(data) {
2221 summaryPane.empty();
2222
2223 var svg = summaryPane.append('svg'),
2224 iid = iconGlyphUrl(data);
2225
2226 var title = summaryPane.append('h2'),
2227 table = summaryPane.append('table'),
2228 tbody = table.append('tbody');
2229
2230 appendGlyph(svg, 0, 0, 40, iid);
2231
2232 svg.append('use')
2233 .attr({
2234 class: 'birdBadge',
2235 transform: translate(8,12),
2236 'xlink:href': '#bird',
2237 width: 24,
2238 height: 24,
2239 fill: '#fff'
2240 });
2241
2242 title.text('ONOS Summary');
2243
2244 data.propOrder.forEach(function(p) {
2245 if (p === '-') {
2246 addSep(tbody);
2247 } else {
2248 addProp(tbody, p, data.props[p]);
2249 }
2250 });
2251 }
2252
Simon Hunt61d04042014-11-11 17:27:16 -08002253 function populateDetails(data) {
2254 detailPane.empty();
2255
Simon Hunta6a9fe72014-11-20 11:17:12 -08002256 var svg = detailPane.append('svg'),
2257 iid = iconGlyphUrl(data);
2258
Simon Hunta3dd9572014-11-20 15:22:41 -08002259 var title = detailPane.append('h2'),
2260 table = detailPane.append('table'),
2261 tbody = table.append('tbody');
Simon Hunt61d04042014-11-11 17:27:16 -08002262
Simon Hunta6a9fe72014-11-20 11:17:12 -08002263 appendGlyph(svg, 0, 0, 40, iid);
2264 title.text(data.id);
Simon Hunt61d04042014-11-11 17:27:16 -08002265
2266 data.propOrder.forEach(function(p) {
2267 if (p === '-') {
2268 addSep(tbody);
2269 } else {
2270 addProp(tbody, p, data.props[p]);
2271 }
2272 });
Simon Huntd72bc702014-11-13 18:38:04 -08002273
Thomas Vachuska4731f122014-11-20 04:56:19 -08002274 addSingleSelectActions(data);
Simon Hunt61d04042014-11-11 17:27:16 -08002275 }
2276
Thomas Vachuska4731f122014-11-20 04:56:19 -08002277 function addSingleSelectActions(data) {
Simon Huntd72bc702014-11-13 18:38:04 -08002278 detailPane.append('hr');
2279 // always want to allow 'show traffic'
Thomas Vachuska4731f122014-11-20 04:56:19 -08002280 addAction(detailPane, 'Show Related Traffic', showTrafficAction);
2281
2282 if (data.type === 'switch') {
2283 addAction(detailPane, 'Show Device Flows', showDeviceLinkFlowsAction);
2284 }
Simon Huntd72bc702014-11-13 18:38:04 -08002285 }
2286
2287 function addMultiSelectActions() {
2288 detailPane.append('hr');
2289 // always want to allow 'show traffic'
Thomas Vachuska4731f122014-11-20 04:56:19 -08002290 addAction(detailPane, 'Show Related Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08002291 // if exactly two hosts are selected, also want 'add host intent'
2292 if (nSel() === 2 && allSelectionsClass('host')) {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002293 addAction(detailPane, 'Create Host-to-Host Flow', addHostIntentAction);
2294 } else if (nSel() >= 2 && allSelectionsClass('host')) {
2295 addAction(detailPane, 'Create Multi-Source Flow', addMultiSourceIntentAction);
Simon Huntd72bc702014-11-13 18:38:04 -08002296 }
2297 }
2298
Simon Hunta5e89142014-11-14 07:00:33 -08002299 function addAction(panel, text, cb) {
2300 panel.append('div')
Simon Huntd72bc702014-11-13 18:38:04 -08002301 .classed('actionBtn', true)
2302 .text(text)
2303 .on('click', cb);
2304 }
2305
2306
Paul Greysonfcba0e82014-11-13 10:21:16 -08002307 function zoomPan(scale, translate) {
2308 zoomPanContainer.attr("transform", "translate(" + translate + ")scale(" + scale + ")");
2309 // keep the map lines constant width while zooming
Thomas Vachuska89543292014-11-19 11:28:33 -08002310 bgImg.style("stroke-width", 2.0 / scale + "px");
Paul Greysonfcba0e82014-11-13 10:21:16 -08002311 }
2312
2313 function resetZoomPan() {
2314 zoomPan(1, [0,0]);
2315 zoom.scale(1).translate([0,0]);
2316 }
2317
2318 function setupZoomPan() {
2319 function zoomed() {
Simon Huntdeab4322014-11-13 18:49:07 -08002320 if (!panZoom() ^ !d3.event.sourceEvent.metaKey) {
Paul Greysonfcba0e82014-11-13 10:21:16 -08002321 zoomPan(d3.event.scale, d3.event.translate);
2322 }
2323 }
2324
2325 zoom = d3.behavior.zoom()
2326 .translate([0, 0])
2327 .scale(1)
2328 .scaleExtent([1, 8])
2329 .on("zoom", zoomed);
2330
2331 svg.call(zoom);
2332 }
2333
Simon Hunt61d04042014-11-11 17:27:16 -08002334 // ==============================
2335 // Test harness code
Simon Hunt56d51852014-11-09 13:03:35 -08002336
2337 function prepareScenario(view, ctx, dbg) {
2338 var sc = scenario,
2339 urlSc = sc.evDir + ctx + sc.evScenario;
2340
2341 if (!ctx) {
2342 view.alert("No scenario specified (null ctx)");
2343 return;
2344 }
2345
2346 sc.view = view;
2347 sc.ctx = ctx;
2348 sc.debug = dbg;
2349 sc.evNumber = 0;
2350
2351 d3.json(urlSc, function(err, data) {
Simon Huntbb282f52014-11-10 11:08:19 -08002352 var p = data && data.params || {},
2353 desc = data && data.description || null,
Simon Huntfc274c92014-11-11 11:05:46 -08002354 intro = data && data.title;
Simon Huntbb282f52014-11-10 11:08:19 -08002355
Simon Hunt56d51852014-11-09 13:03:35 -08002356 if (err) {
2357 view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err);
2358 } else {
2359 sc.params = p;
Simon Huntbb282f52014-11-10 11:08:19 -08002360 if (desc) {
2361 intro += '\n\n ' + desc.join('\n ');
2362 }
2363 view.alert(intro);
Simon Hunt56d51852014-11-09 13:03:35 -08002364 }
2365 });
2366
2367 }
2368
Simon Hunt01095ff2014-11-13 16:37:29 -08002369 // ==============================
2370 // Toggle Buttons in masthead
Simon Hunt0c6d4192014-11-12 12:07:10 -08002371
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002372 // TODO: toggle button (and other widgets in the masthead) should be provided
2373 // by the framework; not generated by the view.
2374
Thomas Vachuska47635c62014-11-22 01:21:36 -08002375 //var showInstances;
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002376
2377 function addButtonBar(view) {
2378 var bb = d3.select('#mast')
2379 .append('span').classed('right', true).attr('id', 'bb');
2380
Simon Hunta5e89142014-11-14 07:00:33 -08002381 function mkTogBtn(text, cb) {
2382 return bb.append('span')
2383 .classed('btn', true)
2384 .text(text)
2385 .on('click', cb);
2386 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002387
Thomas Vachuska47635c62014-11-22 01:21:36 -08002388 //showInstances = mkTogBtn('Show Instances', toggleInst);
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002389 }
2390
Simon Huntdeab4322014-11-13 18:49:07 -08002391 function panZoom() {
Simon Hunte5b71752014-11-18 20:06:07 -08002392 return false;
Simon Hunta5e89142014-11-14 07:00:33 -08002393 }
2394
Simon Hunt7fa116d2014-11-17 14:16:55 -08002395 function loadGlyphs(svg) {
2396 var defs = svg.append('defs');
2397 gly.defBird(defs);
Simon Huntc72967b2014-11-20 09:21:42 -08002398 gly.defGlyphs(defs);
Simon Huntb82f6902014-11-22 11:53:15 -08002399 gly.defBadges(defs);
Simon Hunt7fa116d2014-11-17 14:16:55 -08002400 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002401
Simon Hunt395a70c2014-11-22 23:17:40 -08002402 function sendUpdateMeta(d, store) {
2403 var metaUi = {},
2404 ll;
2405
2406 if (store) {
2407 ll = geoMapProjection.invert([d.x, d.y]);
Simon Hunt62c47542014-11-22 22:16:32 -08002408 metaUi = {
2409 x: d.x,
2410 y: d.y,
2411 lng: ll[0],
2412 lat: ll[1]
2413 };
Simon Hunt395a70c2014-11-22 23:17:40 -08002414 }
Simon Hunt62c47542014-11-22 22:16:32 -08002415 d.metaUi = metaUi;
2416 sendMessage('updateMeta', {
2417 id: d.id,
2418 'class': d.class,
2419 'memento': metaUi
2420 });
2421 }
2422
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002423 // ==============================
Simon Hunt142d0032014-11-04 20:13:09 -08002424 // View life-cycle callbacks
Simon Hunt195cb382014-11-03 17:50:51 -08002425
Simon Huntf67722a2014-11-10 09:32:06 -08002426 function preload(view, ctx, flags) {
Simon Hunt142d0032014-11-04 20:13:09 -08002427 var w = view.width(),
2428 h = view.height(),
Simon Huntc7ee0662014-11-05 16:44:37 -08002429 fcfg = config.force,
2430 fpad = fcfg.pad,
2431 forceDim = [w - 2*fpad, h - 2*fpad];
Simon Hunt195cb382014-11-03 17:50:51 -08002432
Simon Hunt142d0032014-11-04 20:13:09 -08002433 // NOTE: view.$div is a D3 selection of the view's div
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002434 var viewBox = '0 0 ' + config.logicalSize + ' ' + config.logicalSize;
2435 svg = view.$div.append('svg').attr('viewBox', viewBox);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002436 setSize(svg, view);
2437
Simon Hunt7fa116d2014-11-17 14:16:55 -08002438 loadGlyphs(svg);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002439
Paul Greysonfcba0e82014-11-13 10:21:16 -08002440 zoomPanContainer = svg.append('g').attr('id', 'zoomPanContainer');
Paul Greysonfcba0e82014-11-13 10:21:16 -08002441 setupZoomPan();
2442
Simon Hunt1a9eff92014-11-07 11:06:34 -08002443 // add blue glow filter to svg layer
Paul Greysonfcba0e82014-11-13 10:21:16 -08002444 d3u.appendGlow(zoomPanContainer);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002445
Simon Huntc7ee0662014-11-05 16:44:37 -08002446 // group for the topology
Paul Greysonfcba0e82014-11-13 10:21:16 -08002447 topoG = zoomPanContainer.append('g')
Simon Huntd3b7d512014-11-12 15:48:41 -08002448 .attr('id', 'topo-G')
Simon Huntc7ee0662014-11-05 16:44:37 -08002449 .attr('transform', fcfg.translate());
2450
Simon Hunte2575b62014-11-18 15:25:53 -08002451 // subgroups for links, link labels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002452 linkG = topoG.append('g').attr('id', 'links');
Simon Hunte2575b62014-11-18 15:25:53 -08002453 linkLabelG = topoG.append('g').attr('id', 'linkLabels');
Simon Huntc7ee0662014-11-05 16:44:37 -08002454 nodeG = topoG.append('g').attr('id', 'nodes');
2455
Simon Hunte2575b62014-11-18 15:25:53 -08002456 // selection of links, linkLabels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002457 link = linkG.selectAll('.link');
Simon Hunte2575b62014-11-18 15:25:53 -08002458 linkLabel = linkLabelG.selectAll('.linkLabel');
Simon Huntc7ee0662014-11-05 16:44:37 -08002459 node = nodeG.selectAll('.node');
2460
Simon Hunt7cd48f32014-11-09 23:42:50 -08002461 function chrg(d) {
2462 return fcfg.charge[d.class] || -12000;
2463 }
Simon Hunt99c13842014-11-06 18:23:12 -08002464 function ldist(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002465 return fcfg.linkDistance[d.type] || 50;
Simon Hunt99c13842014-11-06 18:23:12 -08002466 }
2467 function lstrg(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002468 // 0.0 - 1.0
2469 return fcfg.linkStrength[d.type] || 1.0;
Simon Hunt99c13842014-11-06 18:23:12 -08002470 }
2471
Simon Hunt1a9eff92014-11-07 11:06:34 -08002472 function selectCb(d, self) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002473 selectObject(d, self);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002474 }
2475
2476 function atDragEnd(d, self) {
Simon Hunt56d51852014-11-09 13:03:35 -08002477 // once we've finished moving, pin the node in position
2478 d.fixed = true;
2479 d3.select(self).classed('fixed', true);
2480 if (config.useLiveData) {
Simon Hunt395a70c2014-11-22 23:17:40 -08002481 sendUpdateMeta(d, true);
Simon Hunta255a2c2014-11-13 22:29:35 -08002482 } else {
2483 console.log('Moving node ' + d.id + ' to [' + d.x + ',' + d.y + ']');
Simon Hunt1a9eff92014-11-07 11:06:34 -08002484 }
2485 }
2486
Simon Huntc7ee0662014-11-05 16:44:37 -08002487 // set up the force layout
2488 network.force = d3.layout.force()
2489 .size(forceDim)
2490 .nodes(network.nodes)
2491 .links(network.links)
Simon Hunt7cd48f32014-11-09 23:42:50 -08002492 .gravity(0.4)
2493 .friction(0.7)
2494 .charge(chrg)
Simon Hunt99c13842014-11-06 18:23:12 -08002495 .linkDistance(ldist)
2496 .linkStrength(lstrg)
Simon Huntc7ee0662014-11-05 16:44:37 -08002497 .on('tick', tick);
Simon Hunt195cb382014-11-03 17:50:51 -08002498
Simon Hunt01095ff2014-11-13 16:37:29 -08002499 network.drag = d3u.createDragBehavior(network.force,
Simon Huntdeab4322014-11-13 18:49:07 -08002500 selectCb, atDragEnd, panZoom);
Simon Hunt0c6d4192014-11-12 12:07:10 -08002501
2502 // create mask layer for when we lose connection to server.
Simon Hunta5e89142014-11-14 07:00:33 -08002503 // TODO: this should be part of the framework
Simon Hunt0c6d4192014-11-12 12:07:10 -08002504 mask = view.$div.append('div').attr('id','topo-mask');
2505 para(mask, 'Oops!');
2506 para(mask, 'Web-socket connection to server closed...');
2507 para(mask, 'Try refreshing the page.');
Simon Hunt12ce12e2014-11-15 21:13:19 -08002508
2509 mask.append('svg')
2510 .attr({
2511 id: 'mask-bird',
2512 width: w,
2513 height: h
2514 })
2515 .append('g')
2516 .attr('transform', birdTranslate(w, h))
2517 .style('opacity', 0.3)
2518 .append('use')
2519 .attr({
2520 'xlink:href': '#bird',
2521 width: config.birdDim,
2522 height: config.birdDim,
2523 fill: '#111'
Thomas Vachuska89543292014-11-19 11:28:33 -08002524 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08002525 }
Simon Hunt195cb382014-11-03 17:50:51 -08002526
Simon Hunt01095ff2014-11-13 16:37:29 -08002527 function para(sel, text) {
2528 sel.append('p').text(text);
2529 }
2530
2531
Simon Hunt56d51852014-11-09 13:03:35 -08002532 function load(view, ctx, flags) {
Simon Huntf67722a2014-11-10 09:32:06 -08002533 // resize, in case the window was resized while we were not loaded
2534 resize(view, ctx, flags);
2535
Simon Hunt99c13842014-11-06 18:23:12 -08002536 // cache the view token, so network topo functions can access it
2537 network.view = view;
Simon Hunt56d51852014-11-09 13:03:35 -08002538 config.useLiveData = !flags.local;
2539
2540 if (!config.useLiveData) {
2541 prepareScenario(view, ctx, flags.debug);
2542 }
Simon Hunt99c13842014-11-06 18:23:12 -08002543
2544 // set our radio buttons and key bindings
Simon Hunt9462e8c2014-11-14 17:28:09 -08002545 layerBtnSet = view.setRadio(layerButtons);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002546 view.setKeys(keyDispatch);
Simon Hunt195cb382014-11-03 17:50:51 -08002547
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002548 // patch in our "button bar" for now
2549 // TODO: implement a more official frameworky way of doing this..
2550 addButtonBar(view);
2551
Simon Huntd3b7d512014-11-12 15:48:41 -08002552 // Load map data asynchronously; complete startup after that..
2553 loadGeoJsonData();
Simon Hunta255a2c2014-11-13 22:29:35 -08002554 }
2555
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002556 function startAntTimer() {
Thomas Vachuska4731f122014-11-20 04:56:19 -08002557 if (!antTimer) {
2558 var pulses = [5, 3, 1.2, 3],
2559 pulse = 0;
2560 antTimer = setInterval(function () {
2561 pulse = pulse + 1;
2562 pulse = pulse === pulses.length ? 0 : pulse;
2563 d3.selectAll('.animated').style('stroke-width', pulses[pulse]);
2564 }, 200);
2565 }
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002566 }
2567
2568 function stopAntTimer() {
Simon Hunta255a2c2014-11-13 22:29:35 -08002569 if (antTimer) {
2570 clearInterval(antTimer);
2571 antTimer = null;
2572 }
Simon Huntd3b7d512014-11-12 15:48:41 -08002573 }
2574
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002575 function unload(view, ctx, flags) {
2576 stopAntTimer();
2577 }
2578
Simon Huntd3b7d512014-11-12 15:48:41 -08002579 // TODO: move these to config/state portion of script
Simon Hunta6a9fe72014-11-20 11:17:12 -08002580 var geoJsonUrl = 'json/map/continental_us.json',
Simon Huntd3b7d512014-11-12 15:48:41 -08002581 geoJson;
2582
2583 function loadGeoJsonData() {
2584 d3.json(geoJsonUrl, function (err, data) {
2585 if (err) {
2586 // fall back to USA map background
2587 loadStaticMap();
2588 } else {
2589 geoJson = data;
2590 loadGeoMap();
2591 }
2592
2593 // finally, connect to the server...
2594 if (config.useLiveData) {
2595 webSock.connect();
2596 }
2597 });
2598 }
2599
2600 function showBg() {
2601 return config.options.showBackground ? 'visible' : 'hidden';
2602 }
2603
2604 function loadStaticMap() {
2605 fnTrace('loadStaticMap', config.backgroundUrl);
2606 var w = network.view.width(),
2607 h = network.view.height();
2608
2609 // load the background image
2610 bgImg = svg.insert('svg:image', '#topo-G')
2611 .attr({
2612 id: 'topo-bg',
2613 width: w,
2614 height: h,
2615 'xlink:href': config.backgroundUrl
2616 })
2617 .style({
2618 visibility: showBg()
2619 });
2620 }
2621
2622 function loadGeoMap() {
2623 fnTrace('loadGeoMap', geoJsonUrl);
Simon Huntd3b7d512014-11-12 15:48:41 -08002624
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002625 // extracts the topojson data into geocoordinate-based geometry
2626 var topoData = topojson.feature(geoJson, geoJson.objects.states);
Simon Huntd3b7d512014-11-12 15:48:41 -08002627
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002628 // see: http://bl.ocks.org/mbostock/4707858
2629 geoMapProjection = d3.geo.mercator();
2630 var path = d3.geo.path().projection(geoMapProjection);
Simon Huntd3b7d512014-11-12 15:48:41 -08002631
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002632 geoMapProjection
2633 .scale(1)
2634 .translate([0, 0]);
Simon Huntd3b7d512014-11-12 15:48:41 -08002635
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002636 // [[x1,y1],[x2,y2]]
2637 var b = path.bounds(topoData);
Paul Greysonfcba0e82014-11-13 10:21:16 -08002638 // size map to 95% of minimum dimension to fill space
2639 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 -08002640 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 -08002641
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002642 geoMapProjection
2643 .scale(s)
2644 .translate(t);
2645
Paul Greysonfcba0e82014-11-13 10:21:16 -08002646 bgImg = zoomPanContainer.insert("g", '#topo-G');
Thomas Vachuska89543292014-11-19 11:28:33 -08002647 bgImg.attr('id', 'map').selectAll('path')
2648 .data(topoData.features)
2649 .enter()
2650 .append('path')
2651 .attr('d', path);
Simon Hunt195cb382014-11-03 17:50:51 -08002652 }
2653
Simon Huntf67722a2014-11-10 09:32:06 -08002654 function resize(view, ctx, flags) {
Simon Hunt12ce12e2014-11-15 21:13:19 -08002655 var w = view.width(),
2656 h = view.height();
2657
Simon Hunt934c3ce2014-11-05 11:45:07 -08002658 setSize(svg, view);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002659
2660 d3.select('#mask-bird').attr({ width: w, height: h})
2661 .select('g').attr('transform', birdTranslate(w, h));
Simon Hunt142d0032014-11-04 20:13:09 -08002662 }
2663
Simon Hunt8f40cce2014-11-23 15:57:30 -08002664 function theme(view, ctx, flags) {
2665 updateInstances();
Simon Huntb0ecfa52014-11-23 21:05:12 -08002666 updateDeviceColors();
Simon Hunt8f40cce2014-11-23 15:57:30 -08002667 }
2668
Simon Hunt12ce12e2014-11-15 21:13:19 -08002669 function birdTranslate(w, h) {
2670 var bdim = config.birdDim;
2671 return 'translate('+((w-bdim)*.4)+','+((h-bdim)*.1)+')';
2672 }
Simon Hunt142d0032014-11-04 20:13:09 -08002673
2674 // ==============================
2675 // View registration
Simon Hunt195cb382014-11-03 17:50:51 -08002676
Simon Hunt25248912014-11-04 11:25:48 -08002677 onos.ui.addView('topo', {
Simon Hunt142d0032014-11-04 20:13:09 -08002678 preload: preload,
2679 load: load,
Simon Hunta255a2c2014-11-13 22:29:35 -08002680 unload: unload,
Simon Hunt8f40cce2014-11-23 15:57:30 -08002681 resize: resize,
2682 theme: theme
Simon Hunt195cb382014-11-03 17:50:51 -08002683 });
2684
Thomas Vachuska47635c62014-11-22 01:21:36 -08002685 summaryPane = onos.ui.addFloatingPanel('topo-summary');
Simon Hunt61d04042014-11-11 17:27:16 -08002686 detailPane = onos.ui.addFloatingPanel('topo-detail');
Simon Hunta5e89142014-11-14 07:00:33 -08002687 oiBox = onos.ui.addFloatingPanel('topo-oibox', 'TL');
Simon Huntb82f6902014-11-22 11:53:15 -08002688 oiBox.width(20);
Simon Hunt61d04042014-11-11 17:27:16 -08002689
Simon Hunt195cb382014-11-03 17:50:51 -08002690}(ONOS));