blob: 06246774132cd88e4299e7d5976062c166c246ac [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
21 */
22
23(function (onos) {
24 'use strict';
25
Simon Hunt1a9eff92014-11-07 11:06:34 -080026 // shorter names for library APIs
Simon Huntbb282f52014-11-10 11:08:19 -080027 var d3u = onos.lib.d3util,
Simon Hunt12ce12e2014-11-15 21:13:19 -080028 gly = onos.lib.glyphs,
Simon Huntbb282f52014-11-10 11:08:19 -080029 trace;
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: {
73 linkInColor: '#66f',
74 linkInWidth: 14
75 },
Simon Hunt99c13842014-11-06 18:23:12 -080076 icons: {
77 w: 28,
78 h: 28,
79 xoff: -12,
80 yoff: -8
81 },
Simon Hunt195cb382014-11-03 17:50:51 -080082 iconUrl: {
83 device: 'img/device.png',
84 host: 'img/host.png',
85 pkt: 'img/pkt.png',
86 opt: 'img/opt.png'
87 },
Simon Hunt195cb382014-11-03 17:50:51 -080088 force: {
Simon Hunt7cd48f32014-11-09 23:42:50 -080089 note_for_links: 'link.type is used to differentiate',
Simon Huntc7ee0662014-11-05 16:44:37 -080090 linkDistance: {
Simon Hunt7cd48f32014-11-09 23:42:50 -080091 direct: 100,
92 optical: 120,
Thomas Vachuska3266abf2014-11-13 09:28:46 -080093 hostLink: 3
Simon Huntc7ee0662014-11-05 16:44:37 -080094 },
95 linkStrength: {
Simon Hunt7cd48f32014-11-09 23:42:50 -080096 direct: 1.0,
97 optical: 1.0,
98 hostLink: 1.0
Simon Huntc7ee0662014-11-05 16:44:37 -080099 },
Simon Hunt7cd48f32014-11-09 23:42:50 -0800100 note_for_nodes: 'node.class is used to differentiate',
Simon Huntc7ee0662014-11-05 16:44:37 -0800101 charge: {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800102 device: -8000,
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800103 host: -5000
Simon Huntc7ee0662014-11-05 16:44:37 -0800104 },
105 pad: 20,
Simon Hunt195cb382014-11-03 17:50:51 -0800106 translate: function() {
107 return 'translate(' +
Simon Huntc7ee0662014-11-05 16:44:37 -0800108 config.force.pad + ',' +
109 config.force.pad + ')';
Simon Hunt195cb382014-11-03 17:50:51 -0800110 }
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800111 },
112 // see below in creation of viewBox on main svg
113 logicalSize: 1000
Simon Hunt195cb382014-11-03 17:50:51 -0800114 };
115
Simon Hunt142d0032014-11-04 20:13:09 -0800116 // radio buttons
Simon Hunt9462e8c2014-11-14 17:28:09 -0800117 var layerButtons = [
118 { text: 'All Layers', id: 'all', cb: showAllLayers },
119 { text: 'Packet Only', id: 'pkt', cb: showPacketLayer },
120 { text: 'Optical Only', id: 'opt', cb: showOpticalLayer }
121 ],
122 layerBtnSet,
123 layerBtnDispatch = {
124 all: showAllLayers,
125 pkt: showPacketLayer,
126 opt: showOpticalLayer
127 };
Simon Hunt934c3ce2014-11-05 11:45:07 -0800128
129 // key bindings
130 var keyDispatch = {
Simon Hunta255a2c2014-11-13 22:29:35 -0800131 M: testMe, // TODO: remove (testing only)
132 S: injectStartupEvents, // TODO: remove (testing only)
133 space: injectTestEvent, // TODO: remove (testing only)
Simon Hunt99c13842014-11-06 18:23:12 -0800134
Simon Hunt01095ff2014-11-13 16:37:29 -0800135 B: toggleBg,
Simon Hunt934c3ce2014-11-05 11:45:07 -0800136 L: cycleLabels,
137 P: togglePorts,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800138 U: unpin,
Paul Greysonfcba0e82014-11-13 10:21:16 -0800139 R: resetZoomPan,
Simon Hunt9462e8c2014-11-14 17:28:09 -0800140 esc: handleEscape
Simon Hunt934c3ce2014-11-05 11:45:07 -0800141 };
Simon Hunt142d0032014-11-04 20:13:09 -0800142
Simon Hunt195cb382014-11-03 17:50:51 -0800143 // state variables
Simon Hunt99c13842014-11-06 18:23:12 -0800144 var network = {
Simon Hunt50128c02014-11-08 13:36:15 -0800145 view: null, // view token reference
Simon Hunt99c13842014-11-06 18:23:12 -0800146 nodes: [],
147 links: [],
Simon Hunt269670f2014-11-17 16:17:43 -0800148 lookup: {},
149 revLinkToKey: {}
Simon Hunt99c13842014-11-06 18:23:12 -0800150 },
Simon Hunt56d51852014-11-09 13:03:35 -0800151 scenario = {
152 evDir: 'json/ev/',
153 evScenario: '/scenario.json',
154 evPrefix: '/ev_',
155 evOnos: '_onos.json',
156 evUi: '_ui.json',
157 ctx: null,
158 params: {},
159 evNumber: 0,
160 view: null,
161 debug: false
162 },
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800163 webSock,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800164 sid = 0,
Simon Hunt56d51852014-11-09 13:03:35 -0800165 deviceLabelIndex = 0,
166 hostLabelIndex = 0,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800167 selections = {},
Simon Hunta5e89142014-11-14 07:00:33 -0800168 selectOrder = [],
Simon Hunt6ac93f32014-11-13 12:17:27 -0800169 hovered = null,
Simon Hunta5e89142014-11-14 07:00:33 -0800170 detailPane,
Simon Hunta255a2c2014-11-13 22:29:35 -0800171 antTimer = null,
Simon Hunta5e89142014-11-14 07:00:33 -0800172 onosInstances = {},
173 onosOrder = [],
174 oiBox,
Simon Hunt9462e8c2014-11-14 17:28:09 -0800175 oiShowMaster = false,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800176
Simon Hunt195cb382014-11-03 17:50:51 -0800177 portLabelsOn = false;
178
Simon Hunt934c3ce2014-11-05 11:45:07 -0800179 // D3 selections
180 var svg,
Paul Greysonfcba0e82014-11-13 10:21:16 -0800181 zoomPanContainer,
Simon Hunt934c3ce2014-11-05 11:45:07 -0800182 bgImg,
Simon Huntc7ee0662014-11-05 16:44:37 -0800183 topoG,
184 nodeG,
185 linkG,
186 node,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800187 link,
188 mask;
Simon Hunt195cb382014-11-03 17:50:51 -0800189
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800190 // the projection for the map background
191 var geoMapProjection;
192
Paul Greysonfcba0e82014-11-13 10:21:16 -0800193 // the zoom function
194 var zoom;
195
Simon Hunt142d0032014-11-04 20:13:09 -0800196 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800197 // For Debugging / Development
Simon Hunt195cb382014-11-03 17:50:51 -0800198
Simon Hunt99c13842014-11-06 18:23:12 -0800199 function note(label, msg) {
200 console.log('NOTE: ' + label + ': ' + msg);
Simon Hunt195cb382014-11-03 17:50:51 -0800201 }
202
Simon Hunt99c13842014-11-06 18:23:12 -0800203 function debug(what) {
204 return config.debugOn && config.debug[what];
Simon Hunt934c3ce2014-11-05 11:45:07 -0800205 }
206
Simon Huntfc274c92014-11-11 11:05:46 -0800207 function fnTrace(msg, id) {
208 if (config.fnTrace) {
209 console.log('FN: ' + msg + ' [' + id + ']');
210 }
211 }
Simon Hunt99c13842014-11-06 18:23:12 -0800212
Simon Hunta5e89142014-11-14 07:00:33 -0800213 function evTrace(data) {
214 fnTrace(data.event, data.payload.id);
215 }
216
Simon Hunt934c3ce2014-11-05 11:45:07 -0800217 // ==============================
218 // Key Callbacks
219
Simon Hunt99c13842014-11-06 18:23:12 -0800220 function testMe(view) {
Simon Hunta5e89142014-11-14 07:00:33 -0800221 //view.alert('test');
Simon Hunt12ce12e2014-11-15 21:13:19 -0800222 noWebSock(true);
Simon Hunt99c13842014-11-06 18:23:12 -0800223 }
224
Simon Hunt56d51852014-11-09 13:03:35 -0800225 function abortIfLive() {
Simon Hunt50128c02014-11-08 13:36:15 -0800226 if (config.useLiveData) {
Simon Huntb53e0682014-11-12 13:32:01 -0800227 network.view.alert("Sorry, currently using live data..");
Simon Hunt56d51852014-11-09 13:03:35 -0800228 return true;
Simon Hunt50128c02014-11-08 13:36:15 -0800229 }
Simon Hunt56d51852014-11-09 13:03:35 -0800230 return false;
231 }
Simon Hunt50128c02014-11-08 13:36:15 -0800232
Simon Hunt56d51852014-11-09 13:03:35 -0800233 function testDebug(msg) {
234 if (scenario.debug) {
235 scenario.view.alert(msg);
236 }
237 }
Simon Hunt99c13842014-11-06 18:23:12 -0800238
Simon Hunt56d51852014-11-09 13:03:35 -0800239 function injectTestEvent(view) {
240 if (abortIfLive()) { return; }
241 var sc = scenario,
242 evn = ++sc.evNumber,
243 pfx = sc.evDir + sc.ctx + sc.evPrefix + evn,
244 onosUrl = pfx + sc.evOnos,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800245 uiUrl = pfx + sc.evUi,
246 stack = [
247 { url: onosUrl, cb: handleServerEvent },
248 { url: uiUrl, cb: handleUiEvent }
249 ];
250 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800251 }
252
Simon Hunt7cd48f32014-11-09 23:42:50 -0800253 function recurseFetchEvent(stack, evn) {
254 var v = scenario.view,
255 frame;
256 if (stack.length === 0) {
Simon Huntfc274c92014-11-11 11:05:46 -0800257 v.alert('Oops!\n\nNo event #' + evn + ' found.');
Simon Hunt7cd48f32014-11-09 23:42:50 -0800258 return;
259 }
260 frame = stack.shift();
261
262 d3.json(frame.url, function (err, data) {
Simon Hunt99c13842014-11-06 18:23:12 -0800263 if (err) {
Simon Hunt56d51852014-11-09 13:03:35 -0800264 if (err.status === 404) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800265 // if we didn't find the data, try the next stack frame
266 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800267 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800268 v.alert('non-404 error:\n\n' + frame.url + '\n\n' + err);
Simon Hunt56d51852014-11-09 13:03:35 -0800269 }
Simon Hunt99c13842014-11-06 18:23:12 -0800270 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800271 testDebug('loaded: ' + frame.url);
Simon Hunt1712ed82014-11-17 12:56:00 -0800272 wsTrace('test', JSON.stringify(data));
Simon Hunt7cd48f32014-11-09 23:42:50 -0800273 frame.cb(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800274 }
275 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800276
Simon Hunt56d51852014-11-09 13:03:35 -0800277 }
Simon Hunt50128c02014-11-08 13:36:15 -0800278
Simon Hunt56d51852014-11-09 13:03:35 -0800279 function handleUiEvent(data) {
Simon Huntbb282f52014-11-10 11:08:19 -0800280 scenario.view.alert('UI Tx: ' + data.event + '\n\n' +
281 JSON.stringify(data));
Simon Hunt56d51852014-11-09 13:03:35 -0800282 }
283
284 function injectStartupEvents(view) {
285 var last = scenario.params.lastAuto || 0;
286 if (abortIfLive()) { return; }
287
288 while (scenario.evNumber < last) {
Simon Hunt1a9eff92014-11-07 11:06:34 -0800289 injectTestEvent(view);
290 }
291 }
292
Simon Hunt934c3ce2014-11-05 11:45:07 -0800293 function toggleBg() {
294 var vis = bgImg.style('visibility');
295 bgImg.style('visibility', (vis === 'hidden') ? 'visible' : 'hidden');
296 }
297
Simon Hunt99c13842014-11-06 18:23:12 -0800298 function cycleLabels() {
Simon Huntbb282f52014-11-10 11:08:19 -0800299 deviceLabelIndex = (deviceLabelIndex === network.deviceLabelCount - 1)
300 ? 0 : deviceLabelIndex + 1;
Simon Hunt5f36d342014-11-08 21:33:14 -0800301
Simon Hunt99c13842014-11-06 18:23:12 -0800302 network.nodes.forEach(function (d) {
Simon Huntbb282f52014-11-10 11:08:19 -0800303 if (d.class === 'device') {
304 updateDeviceLabel(d);
305 }
Simon Hunt99c13842014-11-06 18:23:12 -0800306 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800307 }
308
309 function togglePorts(view) {
Simon Hunt50128c02014-11-08 13:36:15 -0800310 view.alert('togglePorts() callback')
Simon Hunt934c3ce2014-11-05 11:45:07 -0800311 }
312
Simon Hunt6ac93f32014-11-13 12:17:27 -0800313 function unpin() {
314 if (hovered) {
315 hovered.fixed = false;
316 hovered.el.classed('fixed', false);
317 network.force.resume();
318 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800319 }
320
Simon Hunt9462e8c2014-11-14 17:28:09 -0800321 function handleEscape(view) {
322 if (oiShowMaster) {
323 cancelAffinity();
324 } else {
325 deselectAll();
326 }
327 }
328
Simon Hunt934c3ce2014-11-05 11:45:07 -0800329 // ==============================
330 // Radio Button Callbacks
331
Simon Hunta5e89142014-11-14 07:00:33 -0800332 var layerLookup = {
333 host: {
334 endstation: 'pkt', // default, if host event does not define type
335 bgpSpeaker: 'pkt'
336 },
337 device: {
338 switch: 'pkt',
339 roadm: 'opt'
340 },
341 link: {
342 hostLink: 'pkt',
343 direct: 'pkt',
Simon Hunt8257f4c2014-11-16 19:34:54 -0800344 indirect: '',
345 tunnel: '',
Simon Hunta5e89142014-11-14 07:00:33 -0800346 optical: 'opt'
347 }
348 };
349
350 function inLayer(d, layer) {
Simon Hunt8257f4c2014-11-16 19:34:54 -0800351 var type = d.class === 'link' ? d.type() : d.type,
352 look = layerLookup[d.class],
353 lyr = look && look[type];
Simon Hunta5e89142014-11-14 07:00:33 -0800354 return lyr === layer;
355 }
356
357 function unsuppressLayer(which) {
358 node.each(function (d) {
359 var node = d.el;
360 if (inLayer(d, which)) {
361 node.classed('suppressed', false);
362 }
363 });
364
365 link.each(function (d) {
366 var link = d.el;
367 if (inLayer(d, which)) {
368 link.classed('suppressed', false);
369 }
370 });
371 }
372
Simon Hunt9462e8c2014-11-14 17:28:09 -0800373 function suppressLayers(b) {
374 node.classed('suppressed', b);
375 link.classed('suppressed', b);
Simon Hunt142d0032014-11-04 20:13:09 -0800376// d3.selectAll('svg .port').classed('inactive', false);
377// d3.selectAll('svg .portText').classed('inactive', false);
Simon Hunt195cb382014-11-03 17:50:51 -0800378 }
379
Simon Hunt9462e8c2014-11-14 17:28:09 -0800380 function showAllLayers() {
381 suppressLayers(false);
382 }
383
Simon Hunt195cb382014-11-03 17:50:51 -0800384 function showPacketLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800385 node.classed('suppressed', true);
386 link.classed('suppressed', true);
387 unsuppressLayer('pkt');
Simon Hunt195cb382014-11-03 17:50:51 -0800388 }
389
390 function showOpticalLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800391 node.classed('suppressed', true);
392 link.classed('suppressed', true);
393 unsuppressLayer('opt');
Simon Hunt195cb382014-11-03 17:50:51 -0800394 }
395
Simon Hunt9462e8c2014-11-14 17:28:09 -0800396 function restoreLayerState() {
397 layerBtnDispatch[layerBtnSet.selected()]();
398 }
399
Simon Hunt142d0032014-11-04 20:13:09 -0800400 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800401 // Private functions
402
Simon Hunt99c13842014-11-06 18:23:12 -0800403 function safeId(s) {
404 return s.replace(/[^a-z0-9]/gi, '-');
405 }
406
Simon Huntc7ee0662014-11-05 16:44:37 -0800407 // set the size of the given element to that of the view (reduced if padded)
408 function setSize(el, view, pad) {
409 var padding = pad ? pad * 2 : 0;
Simon Hunt934c3ce2014-11-05 11:45:07 -0800410 el.attr({
Simon Huntc7ee0662014-11-05 16:44:37 -0800411 width: view.width() - padding,
412 height: view.height() - padding
Simon Hunt934c3ce2014-11-05 11:45:07 -0800413 });
414 }
415
Simon Hunt8257f4c2014-11-16 19:34:54 -0800416 function makeNodeKey(d, what) {
417 var port = what + 'Port';
418 return d[what] + '/' + d[port];
419 }
420
421 function makeLinkKey(d, flipped) {
422 var one = flipped ? makeNodeKey(d, 'dst') : makeNodeKey(d, 'src'),
423 two = flipped ? makeNodeKey(d, 'src') : makeNodeKey(d, 'dst');
424 return one + '-' + two;
425 }
426
Simon Hunt269670f2014-11-17 16:17:43 -0800427 function findLinkById(id) {
428 // check to see if this is a reverse lookup, else default to given id
429 var key = network.revLinkToKey[id] || id;
430 return key && network.lookup[key];
431 }
432
Simon Hunt8257f4c2014-11-16 19:34:54 -0800433 function findLink(linkData, op) {
434 var key = makeLinkKey(linkData),
435 keyrev = makeLinkKey(linkData, 1),
436 link = network.lookup[key],
437 linkRev = network.lookup[keyrev],
438 result = {},
439 ldata = link || linkRev,
440 rawLink;
441
442 if (op === 'add') {
443 if (link) {
444 // trying to add a link that we already know about
445 result.ldata = link;
446 result.badLogic = 'addLink: link already added';
447
448 } else if (linkRev) {
449 // we found the reverse of the link to be added
450 result.ldata = linkRev;
451 if (linkRev.fromTarget) {
452 result.badLogic = 'addLink: link already added';
453 }
454 }
455 } else if (op === 'update') {
456 if (!ldata) {
457 result.badLogic = 'updateLink: link not found';
458 } else {
459 rawLink = link ? ldata.fromSource : ldata.fromTarget;
460 result.updateWith = function (data) {
461 $.extend(rawLink, data);
462 restyleLinkElement(ldata);
463 }
464 }
465 } else if (op === 'remove') {
466 if (!ldata) {
467 result.badLogic = 'removeLink: link not found';
468 } else {
469 rawLink = link ? ldata.fromSource : ldata.fromTarget;
470
471 if (!rawLink) {
472 result.badLogic = 'removeLink: link not found';
473
474 } else {
475 result.removeRawLink = function () {
476 if (link) {
477 // remove fromSource
478 ldata.fromSource = null;
479 if (ldata.fromTarget) {
480 // promote target into source position
481 ldata.fromSource = ldata.fromTarget;
482 ldata.fromTarget = null;
483 ldata.key = keyrev;
484 delete network.lookup[key];
485 network.lookup[keyrev] = ldata;
Simon Hunt269670f2014-11-17 16:17:43 -0800486 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800487 }
488 } else {
489 // remove fromTarget
490 ldata.fromTarget = null;
Simon Hunt269670f2014-11-17 16:17:43 -0800491 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800492 }
493 if (ldata.fromSource) {
494 restyleLinkElement(ldata);
495 } else {
496 removeLinkElement(ldata);
497 }
498 }
499 }
500 }
501 }
502 return result;
503 }
504
505 function addLinkUpdate(ldata, link) {
506 // add link event, but we already have the reverse link installed
507 ldata.fromTarget = link;
Simon Hunt269670f2014-11-17 16:17:43 -0800508 network.revLinkToKey[link.id] = ldata.key;
Simon Hunt8257f4c2014-11-16 19:34:54 -0800509 restyleLinkElement(ldata);
510 }
511
512 var allLinkTypes = 'direct indirect optical tunnel',
513 defaultLinkType = 'direct';
514
515 function restyleLinkElement(ldata) {
516 // this fn's job is to look at raw links and decide what svg classes
517 // need to be applied to the line element in the DOM
518 var el = ldata.el,
519 type = ldata.type(),
520 lw = ldata.linkWidth(),
521 online = ldata.online();
522
523 el.classed('link', true);
524 el.classed('inactive', !online);
525 el.classed(allLinkTypes, false);
526 if (type) {
527 el.classed(type, true);
528 }
529 el.transition()
530 .duration(1000)
531 .attr('stroke-width', linkScale(lw))
532 .attr('stroke', '#666'); // TODO: remove explicit stroke (use CSS)
533 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800534
Simon Hunt99c13842014-11-06 18:23:12 -0800535 // ==============================
536 // Event handlers for server-pushed events
537
Simon Huntbb282f52014-11-10 11:08:19 -0800538 function logicError(msg) {
539 // TODO, report logic error to server, via websock, so it can be logged
Simon Huntcb56cff2014-11-17 11:42:26 -0800540 //network.view.alert('Logic Error:\n\n' + msg);
Simon Huntfc274c92014-11-11 11:05:46 -0800541 console.warn(msg);
Simon Huntbb282f52014-11-10 11:08:19 -0800542 }
543
Simon Hunt99c13842014-11-06 18:23:12 -0800544 var eventDispatch = {
Simon Hunta5e89142014-11-14 07:00:33 -0800545 addInstance: addInstance,
Simon Hunt99c13842014-11-06 18:23:12 -0800546 addDevice: addDevice,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800547 addLink: addLink,
Simon Hunt56d51852014-11-09 13:03:35 -0800548 addHost: addHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800549
Simon Huntd72bc702014-11-13 18:38:04 -0800550 updateInstance: stillToImplement,
Simon Huntbb282f52014-11-10 11:08:19 -0800551 updateDevice: updateDevice,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800552 updateLink: updateLink,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800553 updateHost: updateHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800554
Simon Huntd72bc702014-11-13 18:38:04 -0800555 removeInstance: stillToImplement,
Simon Huntbb282f52014-11-10 11:08:19 -0800556 removeDevice: stillToImplement,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800557 removeLink: removeLink,
Simon Hunt44031102014-11-11 13:20:36 -0800558 removeHost: removeHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800559
Simon Hunt61d04042014-11-11 17:27:16 -0800560 showDetails: showDetails,
Simon Huntb53e0682014-11-12 13:32:01 -0800561 showTraffic: showTraffic
Simon Hunt99c13842014-11-06 18:23:12 -0800562 };
563
Simon Hunta5e89142014-11-14 07:00:33 -0800564 function addInstance(data) {
565 evTrace(data);
566 var inst = data.payload,
567 id = inst.id;
568 if (onosInstances[id]) {
569 logicError('ONOS instance already added: ' + id);
570 return;
571 }
572 onosInstances[id] = inst;
573 onosOrder.push(inst);
574 updateInstances();
575 }
576
Simon Hunt99c13842014-11-06 18:23:12 -0800577 function addDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800578 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800579 var device = data.payload,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800580 nodeData = createDeviceNode(device);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800581 network.nodes.push(nodeData);
582 network.lookup[nodeData.id] = nodeData;
Simon Hunt99c13842014-11-06 18:23:12 -0800583 updateNodes();
584 network.force.start();
585 }
586
Simon Hunt99c13842014-11-06 18:23:12 -0800587 function addLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800588 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800589 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800590 result = findLink(link, 'add'),
591 bad = result.badLogic,
592 ldata = result.ldata;
593
594 if (bad) {
595 logicError(bad + ': ' + link.id);
596 return;
597 }
598
599 if (ldata) {
600 // we already have a backing store link for src/dst nodes
601 addLinkUpdate(ldata, link);
602 return;
603 }
604
605 // no backing store link yet
606 ldata = createLink(link);
607 if (ldata) {
608 network.links.push(ldata);
609 network.lookup[ldata.key] = ldata;
Simon Hunt99c13842014-11-06 18:23:12 -0800610 updateLinks();
611 network.force.start();
612 }
613 }
614
Simon Hunt56d51852014-11-09 13:03:35 -0800615 function addHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800616 evTrace(data);
Simon Hunt56d51852014-11-09 13:03:35 -0800617 var host = data.payload,
618 node = createHostNode(host),
619 lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800620 network.nodes.push(node);
621 network.lookup[host.id] = node;
622 updateNodes();
623
624 lnk = createHostLink(host);
625 if (lnk) {
Simon Hunt44031102014-11-11 13:20:36 -0800626 node.linkData = lnk; // cache ref on its host
Simon Hunt56d51852014-11-09 13:03:35 -0800627 network.links.push(lnk);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800628 network.lookup[host.ingress] = lnk;
629 network.lookup[host.egress] = lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800630 updateLinks();
631 }
632 network.force.start();
633 }
634
Simon Hunt44031102014-11-11 13:20:36 -0800635 // TODO: fold updateX(...) methods into one base method; remove duplication
Simon Huntbb282f52014-11-10 11:08:19 -0800636 function updateDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800637 evTrace(data);
Simon Huntbb282f52014-11-10 11:08:19 -0800638 var device = data.payload,
639 id = device.id,
640 nodeData = network.lookup[id];
641 if (nodeData) {
642 $.extend(nodeData, device);
643 updateDeviceState(nodeData);
644 } else {
645 logicError('updateDevice lookup fail. ID = "' + id + '"');
646 }
647 }
648
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800649 function updateLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800650 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800651 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800652 result = findLink(link, 'update'),
653 bad = result.badLogic;
654 if (bad) {
655 logicError(bad + ': ' + link.id);
656 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800657 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800658 result.updateWith(link);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800659 }
660
Simon Hunt7cd48f32014-11-09 23:42:50 -0800661 function updateHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800662 evTrace(data);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800663 var host = data.payload,
Simon Huntbb282f52014-11-10 11:08:19 -0800664 id = host.id,
665 hostData = network.lookup[id];
666 if (hostData) {
667 $.extend(hostData, host);
668 updateHostState(hostData);
669 } else {
670 logicError('updateHost lookup fail. ID = "' + id + '"');
671 }
Simon Hunt7cd48f32014-11-09 23:42:50 -0800672 }
673
Simon Hunt44031102014-11-11 13:20:36 -0800674 // TODO: fold removeX(...) methods into base method - remove dup code
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800675 function removeLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800676 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800677 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800678 result = findLink(link, 'remove'),
679 bad = result.badLogic;
680 if (bad) {
681 logicError(bad + ': ' + link.id);
682 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800683 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800684 result.removeRawLink();
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800685 }
686
Simon Hunt44031102014-11-11 13:20:36 -0800687 function removeHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800688 evTrace(data);
Simon Hunt44031102014-11-11 13:20:36 -0800689 var host = data.payload,
690 id = host.id,
691 hostData = network.lookup[id];
692 if (hostData) {
693 removeHostElement(hostData);
694 } else {
695 logicError('removeHost lookup fail. ID = "' + id + '"');
696 }
697 }
698
Simon Hunt61d04042014-11-11 17:27:16 -0800699 function showDetails(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800700 evTrace(data);
Simon Hunt61d04042014-11-11 17:27:16 -0800701 populateDetails(data.payload);
702 detailPane.show();
703 }
704
Simon Huntb53e0682014-11-12 13:32:01 -0800705 function showTraffic(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800706 evTrace(data);
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800707 var paths = data.payload.paths;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800708
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800709 // Revert any links hilighted previously.
Simon Hunta255a2c2014-11-13 22:29:35 -0800710 link.classed('primary secondary animated optical', false);
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800711
712 // Now hilight all links in the paths payload.
Simon Hunta255a2c2014-11-13 22:29:35 -0800713 paths.forEach(function (p) {
714 var cls = p.class;
715 p.links.forEach(function (id) {
Simon Hunt269670f2014-11-17 16:17:43 -0800716 var lnk = findLinkById(id);
Simon Hunta255a2c2014-11-13 22:29:35 -0800717 if (lnk) {
718 lnk.el.classed(cls, true);
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800719 }
720 });
721 });
Simon Huntb53e0682014-11-12 13:32:01 -0800722 }
723
Simon Hunt56d51852014-11-09 13:03:35 -0800724 // ...............................
725
726 function stillToImplement(data) {
727 var p = data.payload;
728 note(data.event, p.id);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800729 network.view.alert('Not yet implemented: "' + data.event + '"');
Simon Hunt56d51852014-11-09 13:03:35 -0800730 }
Simon Hunt99c13842014-11-06 18:23:12 -0800731
732 function unknownEvent(data) {
Simon Hunt50128c02014-11-08 13:36:15 -0800733 network.view.alert('Unknown event type: "' + data.event + '"');
Simon Hunt99c13842014-11-06 18:23:12 -0800734 }
735
736 function handleServerEvent(data) {
737 var fn = eventDispatch[data.event] || unknownEvent;
738 fn(data);
739 }
740
741 // ==============================
Simon Hunt61d04042014-11-11 17:27:16 -0800742 // Out-going messages...
743
Simon Huntb53e0682014-11-12 13:32:01 -0800744 function userFeedback(msg) {
745 // for now, use the alert pane as is. Maybe different alert style in
746 // the future (centered on view; dismiss button?)
747 network.view.alert(msg);
748 }
749
750 function nSel() {
751 return selectOrder.length;
752 }
Simon Hunt61d04042014-11-11 17:27:16 -0800753 function getSel(idx) {
754 return selections[selectOrder[idx]];
755 }
Simon Huntb53e0682014-11-12 13:32:01 -0800756 function getSelId(idx) {
757 return getSel(idx).obj.id;
758 }
759 function allSelectionsClass(cls) {
760 for (var i=0, n=nSel(); i<n; i++) {
761 if (getSel(i).obj.class !== cls) {
762 return false;
763 }
764 }
765 return true;
766 }
Simon Hunt61d04042014-11-11 17:27:16 -0800767
Simon Hunt61d04042014-11-11 17:27:16 -0800768 // request details for the selected element
Simon Huntd72bc702014-11-13 18:38:04 -0800769 // invoked from selection of a single node.
Simon Hunt61d04042014-11-11 17:27:16 -0800770 function requestDetails() {
771 var data = getSel(0).obj,
772 payload = {
773 id: data.id,
774 class: data.class
775 };
776 sendMessage('requestDetails', payload);
777 }
778
Simon Huntd72bc702014-11-13 18:38:04 -0800779 function addIntentAction() {
780 sendMessage('addHostIntent', {
781 one: getSelId(0),
Thomas Vachuska82f2c622014-11-17 12:23:18 -0800782 two: getSelId(1),
783 ids: [ getSelId(0), getSelId(1) ]
Simon Huntd72bc702014-11-13 18:38:04 -0800784 });
785 }
786
787 function showTrafficAction() {
788 // if nothing is hovered over, and nothing selected, send cancel request
789 if (!hovered && nSel() === 0) {
790 sendMessage('cancelTraffic', {});
791 return;
792 }
793
794 // NOTE: hover is only populated if "show traffic on hover" is
795 // toggled on, and the item hovered is a host...
796 var hoverId = (trafficHover() && hovered && hovered.class === 'host')
797 ? hovered.id : '';
798 sendMessage('requestTraffic', {
799 ids: selectOrder,
800 hover: hoverId
801 });
802 }
803
804
Simon Hunt61d04042014-11-11 17:27:16 -0800805 // ==============================
Simon Hunta5e89142014-11-14 07:00:33 -0800806 // onos instance panel functions
807
808 function updateInstances() {
809 var onoses = oiBox.el.selectAll('.onosInst')
810 .data(onosOrder, function (d) { return d.id; });
811
812 // operate on existing onoses if necessary
813
814 var entering = onoses.enter()
815 .append('div')
816 .attr('class', 'onosInst')
817 .classed('online', function (d) { return d.online; })
Simon Hunt9c15eca2014-11-15 18:37:59 -0800818 .on('click', clickInst);
819
820 entering.each(function (d, i) {
821 var el = d3.select(this),
822 img;
823
824 $('<img src="img/host.png">').appendTo(el);
825 img = el.select('img')
826 .attr({
827 width: 40,
828 top: -10,
829 left: -10
830 })
831 .style({
832 });
833
834 $('<div>').attr('class', 'onosTitle').text(d.id).appendTo(el);
835
836 // is the UI attached to this instance?
837 // TODO: need uiAttached boolean in instance data
838 //if (d.uiAttached) {
839 if (i === 0) {
840 $('<img src="img/ui.png">').attr('class','ui').appendTo(el);
841 }
842 });
Simon Hunta5e89142014-11-14 07:00:33 -0800843
844 // operate on existing + new onoses here
845
846 // the departed...
847 var exiting = onoses.exit()
848 .transition()
849 .style('opacity', 0)
850 .remove();
851 }
852
Simon Hunt9462e8c2014-11-14 17:28:09 -0800853 function clickInst(d) {
854 var el = d3.select(this),
855 aff = el.classed('affinity');
856 if (!aff) {
857 setAffinity(el, d);
858 } else {
859 cancelAffinity();
860 }
861 }
862
863 function setAffinity(el, d) {
864 d3.selectAll('.onosInst')
865 .classed('mastership', true)
866 .classed('affinity', false);
867 el.classed('affinity', true);
868
869 suppressLayers(true);
870 node.each(function (n) {
871 if (n.master === d.id) {
872 n.el.classed('suppressed', false);
873 }
874 });
875 oiShowMaster = true;
876 }
877
878 function cancelAffinity() {
879 d3.selectAll('.onosInst')
880 .classed('mastership affinity', false);
881 restoreLayerState();
882 oiShowMaster = false;
883 }
884
Simon Hunta5e89142014-11-14 07:00:33 -0800885 // ==============================
Simon Hunt99c13842014-11-06 18:23:12 -0800886 // force layout modification functions
887
888 function translate(x, y) {
889 return 'translate(' + x + ',' + y + ')';
890 }
891
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800892 function missMsg(what, id) {
893 return '\n[' + what + '] "' + id + '" missing ';
894 }
895
896 function linkEndPoints(srcId, dstId) {
897 var srcNode = network.lookup[srcId],
898 dstNode = network.lookup[dstId],
899 sMiss = !srcNode ? missMsg('src', srcId) : '',
900 dMiss = !dstNode ? missMsg('dst', dstId) : '';
901
902 if (sMiss || dMiss) {
903 logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
904 return null;
905 }
906 return {
907 source: srcNode,
908 target: dstNode,
909 x1: srcNode.x,
910 y1: srcNode.y,
911 x2: dstNode.x,
912 y2: dstNode.y
913 };
914 }
915
Simon Hunt56d51852014-11-09 13:03:35 -0800916 function createHostLink(host) {
917 var src = host.id,
918 dst = host.cp.device,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800919 id = host.ingress,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800920 lnk = linkEndPoints(src, dst);
Simon Hunt56d51852014-11-09 13:03:35 -0800921
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800922 if (!lnk) {
Simon Hunt56d51852014-11-09 13:03:35 -0800923 return null;
924 }
925
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800926 // Synthesize link ...
927 $.extend(lnk, {
Simon Hunt8257f4c2014-11-16 19:34:54 -0800928 key: id,
Simon Hunt56d51852014-11-09 13:03:35 -0800929 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -0800930
931 type: function () { return 'hostLink'; },
932 // TODO: ideally, we should see if our edge switch is online...
933 online: function () { return true; },
934 linkWidth: function () { return 1; }
Simon Hunt7cd48f32014-11-09 23:42:50 -0800935 });
Simon Hunt99c13842014-11-06 18:23:12 -0800936 return lnk;
937 }
938
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800939 function createLink(link) {
940 var lnk = linkEndPoints(link.src, link.dst),
941 type = link.type;
942
943 if (!lnk) {
944 return null;
945 }
946
Simon Hunt8257f4c2014-11-16 19:34:54 -0800947 $.extend(lnk, {
948 key: link.id,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800949 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -0800950 fromSource: link,
951
952 // functions to aggregate dual link state
953 type: function () {
954 var s = lnk.fromSource,
955 t = lnk.fromTarget;
956 return (s && s.type) || (t && t.type) || defaultLinkType;
957 },
958 online: function () {
959 var s = lnk.fromSource,
960 t = lnk.fromTarget;
961 return (s && s.online) || (t && t.online);
962 },
963 linkWidth: function () {
964 var s = lnk.fromSource,
965 t = lnk.fromTarget,
966 ws = (s && s.linkWidth) || 0,
967 wt = (t && t.linkWidth) || 0;
968 return Math.max(ws, wt);
969 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800970 });
971 return lnk;
Simon Hunt1a9eff92014-11-07 11:06:34 -0800972 }
973
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800974 var widthRatio = 1.4,
975 linkScale = d3.scale.linear()
976 .domain([1, 12])
977 .range([widthRatio, 12 * widthRatio])
978 .clamp(true);
979
Simon Hunt99c13842014-11-06 18:23:12 -0800980 function updateLinks() {
981 link = linkG.selectAll('.link')
Simon Hunt8257f4c2014-11-16 19:34:54 -0800982 .data(network.links, function (d) { return d.key; });
Simon Hunt99c13842014-11-06 18:23:12 -0800983
984 // operate on existing links, if necessary
985 // link .foo() .bar() ...
986
987 // operate on entering links:
988 var entering = link.enter()
989 .append('line')
990 .attr({
Simon Hunt99c13842014-11-06 18:23:12 -0800991 x1: function (d) { return d.x1; },
992 y1: function (d) { return d.y1; },
993 x2: function (d) { return d.x2; },
994 y2: function (d) { return d.y2; },
Simon Hunt1a9eff92014-11-07 11:06:34 -0800995 stroke: config.topo.linkInColor,
996 'stroke-width': config.topo.linkInWidth
Simon Hunt99c13842014-11-06 18:23:12 -0800997 });
998
999 // augment links
Simon Hunt7cd48f32014-11-09 23:42:50 -08001000 entering.each(function (d) {
1001 var link = d3.select(this);
1002 // provide ref to element selection from backing data....
1003 d.el = link;
Simon Hunt8257f4c2014-11-16 19:34:54 -08001004 restyleLinkElement(d);
Simon Hunt99c13842014-11-06 18:23:12 -08001005
Simon Hunt7cd48f32014-11-09 23:42:50 -08001006 // TODO: add src/dst port labels etc.
1007 });
Thomas Vachuska4830d392014-11-09 17:09:56 -08001008
1009 // operate on both existing and new links, if necessary
1010 //link .foo() .bar() ...
1011
1012 // operate on exiting links:
Thomas Vachuska4830d392014-11-09 17:09:56 -08001013 link.exit()
Thomas Vachuska4830d392014-11-09 17:09:56 -08001014 .attr({
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001015 'stroke-dasharray': '3, 3'
Thomas Vachuska4830d392014-11-09 17:09:56 -08001016 })
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001017 .style('opacity', 0.4)
1018 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -08001019 .duration(1500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001020 .attr({
1021 'stroke-dasharray': '3, 12'
1022 })
1023 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -08001024 .duration(500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001025 .style('opacity', 0.0)
Thomas Vachuska4830d392014-11-09 17:09:56 -08001026 .remove();
Simon Hunt99c13842014-11-06 18:23:12 -08001027 }
1028
1029 function createDeviceNode(device) {
1030 // start with the object as is
1031 var node = device,
Simon Huntbb282f52014-11-10 11:08:19 -08001032 type = device.type,
1033 svgCls = type ? 'node device ' + type : 'node device';
Simon Hunt99c13842014-11-06 18:23:12 -08001034
1035 // Augment as needed...
1036 node.class = 'device';
Simon Huntbb282f52014-11-10 11:08:19 -08001037 node.svgClass = device.online ? svgCls + ' online' : svgCls;
Simon Hunt99c13842014-11-06 18:23:12 -08001038 positionNode(node);
1039
1040 // cache label array length
1041 network.deviceLabelCount = device.labels.length;
Simon Hunt99c13842014-11-06 18:23:12 -08001042 return node;
1043 }
1044
Simon Hunt56d51852014-11-09 13:03:35 -08001045 function createHostNode(host) {
1046 // start with the object as is
1047 var node = host;
1048
1049 // Augment as needed...
1050 node.class = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001051 if (!node.type) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08001052 node.type = 'endstation';
1053 }
Simon Hunt7fa116d2014-11-17 14:16:55 -08001054 node.svgClass = 'node host ' + node.type;
Simon Hunt56d51852014-11-09 13:03:35 -08001055 positionNode(node);
1056
1057 // cache label array length
1058 network.hostLabelCount = host.labels.length;
Simon Hunt56d51852014-11-09 13:03:35 -08001059 return node;
1060 }
1061
Simon Hunt99c13842014-11-06 18:23:12 -08001062 function positionNode(node) {
1063 var meta = node.metaUi,
Simon Huntac9e24f2014-11-12 10:12:21 -08001064 x = meta && meta.x,
1065 y = meta && meta.y,
1066 xy;
Simon Hunt99c13842014-11-06 18:23:12 -08001067
Simon Huntac9e24f2014-11-12 10:12:21 -08001068 // If we have [x,y] already, use that...
Simon Hunt99c13842014-11-06 18:23:12 -08001069 if (x && y) {
1070 node.fixed = true;
Simon Huntac9e24f2014-11-12 10:12:21 -08001071 node.x = x;
1072 node.y = y;
1073 return;
Simon Hunt99c13842014-11-06 18:23:12 -08001074 }
Simon Huntac9e24f2014-11-12 10:12:21 -08001075
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001076 var location = node.location;
1077 if (location && location.type === 'latlng') {
1078 var coord = geoMapProjection([location.lng, location.lat]);
1079 node.fixed = true;
1080 node.x = coord[0];
1081 node.y = coord[1];
1082 return;
1083 }
1084
Simon Huntac9e24f2014-11-12 10:12:21 -08001085 // Note: Placing incoming unpinned nodes at exactly the same point
1086 // (center of the view) causes them to explode outwards when
1087 // the force layout kicks in. So, we spread them out a bit
1088 // initially, to provide a more serene layout convergence.
1089 // Additionally, if the node is a host, we place it near
1090 // the device it is connected to.
1091
1092 function spread(s) {
1093 return Math.floor((Math.random() * s) - s/2);
1094 }
1095
1096 function randDim(dim) {
1097 return dim / 2 + spread(dim * 0.7071);
1098 }
1099
1100 function rand() {
1101 return {
1102 x: randDim(network.view.width()),
1103 y: randDim(network.view.height())
1104 };
1105 }
1106
1107 function near(node) {
1108 var min = 12,
1109 dx = spread(12),
1110 dy = spread(12);
1111 return {
1112 x: node.x + min + dx,
1113 y: node.y + min + dy
1114 };
1115 }
1116
1117 function getDevice(cp) {
1118 var d = network.lookup[cp.device];
1119 return d || rand();
1120 }
1121
1122 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
1123 $.extend(node, xy);
Simon Hunt99c13842014-11-06 18:23:12 -08001124 }
1125
Simon Hunt99c13842014-11-06 18:23:12 -08001126 function iconUrl(d) {
1127 return 'img/' + d.type + '.png';
1128 }
1129
1130 // returns the newly computed bounding box of the rectangle
1131 function adjustRectToFitText(n) {
1132 var text = n.select('text'),
1133 box = text.node().getBBox(),
1134 lab = config.labels;
1135
1136 text.attr('text-anchor', 'middle')
1137 .attr('y', '-0.8em')
1138 .attr('x', lab.imgPad/2);
1139
1140 // translate the bbox so that it is centered on [x,y]
1141 box.x = -box.width / 2;
1142 box.y = -box.height / 2;
1143
1144 // add padding
1145 box.x -= (lab.padLR + lab.imgPad/2);
1146 box.width += lab.padLR * 2 + lab.imgPad;
1147 box.y -= lab.padTB;
1148 box.height += lab.padTB * 2;
1149
1150 return box;
1151 }
1152
Simon Hunt1a9eff92014-11-07 11:06:34 -08001153 function mkSvgClass(d) {
1154 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
1155 }
1156
Simon Hunt7cd48f32014-11-09 23:42:50 -08001157 function hostLabel(d) {
1158 var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0;
1159 return d.labels[idx];
1160 }
1161 function deviceLabel(d) {
1162 var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0;
1163 return d.labels[idx];
1164 }
1165 function niceLabel(label) {
1166 return (label && label.trim()) ? label : '.';
1167 }
1168
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001169 function updateDeviceLabel(d) {
1170 var label = niceLabel(deviceLabel(d)),
1171 node = d.el,
1172 box;
1173
1174 node.select('text')
1175 .text(label)
1176 .style('opacity', 0)
1177 .transition()
1178 .style('opacity', 1);
1179
1180 box = adjustRectToFitText(node);
1181
1182 node.select('rect')
1183 .transition()
1184 .attr(box);
1185
1186 node.select('image')
1187 .transition()
1188 .attr('x', box.x + config.icons.xoff)
1189 .attr('y', box.y + config.icons.yoff);
1190 }
1191
1192 function updateHostLabel(d) {
1193 var label = hostLabel(d),
1194 host = d.el;
1195
1196 host.select('text').text(label);
1197 }
1198
Simon Huntbb282f52014-11-10 11:08:19 -08001199 function updateDeviceState(nodeData) {
1200 nodeData.el.classed('online', nodeData.online);
1201 updateDeviceLabel(nodeData);
1202 // TODO: review what else might need to be updated
1203 }
1204
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001205 function updateLinkState(linkData) {
1206 updateLinkWidth(linkData);
Thomas Vachuskabadb93f2014-11-15 23:51:17 -08001207 linkData.el.classed('inactive', !linkData.online);
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001208 // TODO: review what else might need to be updated
1209 // update label, if showing
1210 }
1211
Simon Huntbb282f52014-11-10 11:08:19 -08001212 function updateHostState(hostData) {
1213 updateHostLabel(hostData);
1214 // TODO: review what else might need to be updated
1215 }
1216
Simon Hunt6ac93f32014-11-13 12:17:27 -08001217 function nodeMouseOver(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001218 hovered = d;
Simon Huntd72bc702014-11-13 18:38:04 -08001219 if (trafficHover() && d.class === 'host') {
1220 showTrafficAction();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001221 }
1222 }
1223
1224 function nodeMouseOut(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001225 hovered = null;
Simon Huntd72bc702014-11-13 18:38:04 -08001226 if (trafficHover() && d.class === 'host') {
1227 showTrafficAction();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001228 }
1229 }
Simon Huntbb282f52014-11-10 11:08:19 -08001230
Simon Hunt7fa116d2014-11-17 14:16:55 -08001231 function addHostIcon(node, radius, iconId) {
1232 var dim = radius * 1.5,
1233 xlate = -dim / 2;
1234
1235 node.append('use')
1236 .classed('glyph', true)
1237 .attr('transform', translate(xlate,xlate))
1238 .attr('xlink:href', '#' + iconId)
1239 .attr('width', dim)
1240 .attr('height', dim);
1241 }
1242
Simon Hunt99c13842014-11-06 18:23:12 -08001243 function updateNodes() {
1244 node = nodeG.selectAll('.node')
1245 .data(network.nodes, function (d) { return d.id; });
1246
1247 // operate on existing nodes, if necessary
Simon Hunt7cd48f32014-11-09 23:42:50 -08001248 // update host labels
Simon Hunt99c13842014-11-06 18:23:12 -08001249 //node .foo() .bar() ...
1250
1251 // operate on entering nodes:
1252 var entering = node.enter()
1253 .append('g')
1254 .attr({
1255 id: function (d) { return safeId(d.id); },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001256 class: mkSvgClass,
Simon Hunt99c13842014-11-06 18:23:12 -08001257 transform: function (d) { return translate(d.x, d.y); },
1258 opacity: 0
1259 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08001260 .call(network.drag)
Simon Hunt6ac93f32014-11-13 12:17:27 -08001261 .on('mouseover', nodeMouseOver)
1262 .on('mouseout', nodeMouseOut)
Simon Hunt99c13842014-11-06 18:23:12 -08001263 .transition()
1264 .attr('opacity', 1);
1265
1266 // augment device nodes...
1267 entering.filter('.device').each(function (d) {
1268 var node = d3.select(this),
1269 icon = iconUrl(d),
Simon Hunt7cd48f32014-11-09 23:42:50 -08001270 label = niceLabel(deviceLabel(d)),
Simon Hunt99c13842014-11-06 18:23:12 -08001271 box;
1272
Simon Hunt7cd48f32014-11-09 23:42:50 -08001273 // provide ref to element from backing data....
1274 d.el = node;
1275
Simon Hunt99c13842014-11-06 18:23:12 -08001276 node.append('rect')
1277 .attr({
1278 'rx': 5,
1279 'ry': 5
1280 });
1281
1282 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001283 .text(label)
Simon Hunt99c13842014-11-06 18:23:12 -08001284 .attr('dy', '1.1em');
1285
1286 box = adjustRectToFitText(node);
1287
1288 node.select('rect')
1289 .attr(box);
1290
1291 if (icon) {
1292 var cfg = config.icons;
1293 node.append('svg:image')
1294 .attr({
1295 x: box.x + config.icons.xoff,
1296 y: box.y + config.icons.yoff,
1297 width: cfg.w,
1298 height: cfg.h,
1299 'xlink:href': icon
1300 });
1301 }
1302
1303 // debug function to show the modelled x,y coordinates of nodes...
1304 if (debug('showNodeXY')) {
1305 node.select('rect').attr('fill-opacity', 0.5);
1306 node.append('circle')
1307 .attr({
1308 class: 'debug',
1309 cx: 0,
1310 cy: 0,
1311 r: '3px'
1312 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001313 }
1314 });
Simon Hunt934c3ce2014-11-05 11:45:07 -08001315
Simon Hunt7fa116d2014-11-17 14:16:55 -08001316 // TODO: better place for this configuration state
1317 var defaultHostRadius = 9,
1318 hostRadius = {
1319 bgpSpeaker: 20
1320 },
1321 hostIcon = {
1322 bgpSpeaker: 'bullhorn'
1323 };
1324
1325
Simon Hunt56d51852014-11-09 13:03:35 -08001326 // augment host nodes...
1327 entering.filter('.host').each(function (d) {
1328 var node = d3.select(this),
Simon Hunt7fa116d2014-11-17 14:16:55 -08001329 r = hostRadius[d.type] || defaultHostRadius,
1330 textDy = r + 10,
1331 icon = hostIcon[d.type];
Simon Hunt56d51852014-11-09 13:03:35 -08001332
Simon Hunt7cd48f32014-11-09 23:42:50 -08001333 // provide ref to element from backing data....
1334 d.el = node;
1335
Simon Hunt56d51852014-11-09 13:03:35 -08001336 node.append('circle')
Simon Hunt7fa116d2014-11-17 14:16:55 -08001337 .attr('r', r);
1338
1339 if (icon) {
1340 addHostIcon(node, r, icon);
1341 }
Simon Hunt56d51852014-11-09 13:03:35 -08001342
Simon Hunt56d51852014-11-09 13:03:35 -08001343 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001344 .text(hostLabel)
Simon Hunt7fa116d2014-11-17 14:16:55 -08001345 .attr('dy', textDy)
Simon Hunt7cd48f32014-11-09 23:42:50 -08001346 .attr('text-anchor', 'middle');
Simon Hunt56d51852014-11-09 13:03:35 -08001347
1348 // debug function to show the modelled x,y coordinates of nodes...
1349 if (debug('showNodeXY')) {
1350 node.select('circle').attr('fill-opacity', 0.5);
1351 node.append('circle')
1352 .attr({
1353 class: 'debug',
1354 cx: 0,
1355 cy: 0,
1356 r: '3px'
1357 });
1358 }
1359 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001360
Simon Hunt99c13842014-11-06 18:23:12 -08001361 // operate on both existing and new nodes, if necessary
1362 //node .foo() .bar() ...
Simon Huntc7ee0662014-11-05 16:44:37 -08001363
Simon Hunt99c13842014-11-06 18:23:12 -08001364 // operate on exiting nodes:
Simon Huntea80eb42014-11-11 13:46:57 -08001365 // Note that the node is removed after 2 seconds.
1366 // Sub element animations should be shorter than 2 seconds.
1367 var exiting = node.exit()
Simon Hunt44031102014-11-11 13:20:36 -08001368 .transition()
1369 .duration(2000)
Simon Huntea80eb42014-11-11 13:46:57 -08001370 .style('opacity', 0)
Simon Hunt99c13842014-11-06 18:23:12 -08001371 .remove();
Simon Huntea80eb42014-11-11 13:46:57 -08001372
1373 // host node exits....
1374 exiting.filter('.host').each(function (d) {
1375 var node = d3.select(this);
1376
1377 node.select('text')
1378 .style('opacity', 0.5)
1379 .transition()
1380 .duration(1000)
1381 .style('opacity', 0);
1382 // note, leave <g>.remove to remove this element
1383
1384 node.select('circle')
1385 .style('stroke-fill', '#555')
1386 .style('fill', '#888')
1387 .style('opacity', 0.5)
1388 .transition()
1389 .duration(1500)
1390 .attr('r', 0);
1391 // note, leave <g>.remove to remove this element
1392
1393 });
1394
1395 // TODO: device node exits
Simon Huntc7ee0662014-11-05 16:44:37 -08001396 }
1397
Simon Hunt8257f4c2014-11-16 19:34:54 -08001398 function find(key, array) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001399 for (var idx = 0, n = array.length; idx < n; idx++) {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001400 if (array[idx].key === key) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001401 return idx;
1402 }
1403 }
1404 return -1;
1405 }
1406
1407 function removeLinkElement(linkData) {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001408 var idx = find(linkData.key, network.links),
1409 removed;
1410 if (idx >=0) {
1411 // remove from links array
1412 removed = network.links.splice(idx, 1);
1413 // remove from lookup cache
1414 delete network.lookup[removed[0].key];
1415 updateLinks();
1416 network.force.resume();
1417 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001418 }
Simon Huntc7ee0662014-11-05 16:44:37 -08001419
Simon Hunt44031102014-11-11 13:20:36 -08001420 function removeHostElement(hostData) {
1421 // first, remove associated hostLink...
1422 removeLinkElement(hostData.linkData);
1423
1424 // remove from lookup cache
1425 delete network.lookup[hostData.id];
1426 // remove from nodes array
1427 var idx = find(hostData.id, network.nodes);
1428 network.nodes.splice(idx, 1);
1429 // remove from SVG
1430 updateNodes();
1431 network.force.resume();
1432 }
1433
1434
Simon Huntc7ee0662014-11-05 16:44:37 -08001435 function tick() {
1436 node.attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001437 transform: function (d) { return translate(d.x, d.y); }
Simon Huntc7ee0662014-11-05 16:44:37 -08001438 });
1439
1440 link.attr({
1441 x1: function (d) { return d.source.x; },
1442 y1: function (d) { return d.source.y; },
1443 x2: function (d) { return d.target.x; },
1444 y2: function (d) { return d.target.y; }
1445 });
1446 }
Simon Hunt934c3ce2014-11-05 11:45:07 -08001447
1448 // ==============================
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001449 // Web-Socket for live data
1450
1451 function webSockUrl() {
1452 return document.location.toString()
1453 .replace(/\#.*/, '')
1454 .replace('http://', 'ws://')
1455 .replace('https://', 'wss://')
1456 .replace('index2.html', config.webSockUrl);
1457 }
1458
1459 webSock = {
1460 ws : null,
1461
1462 connect : function() {
1463 webSock.ws = new WebSocket(webSockUrl());
1464
1465 webSock.ws.onopen = function() {
Simon Hunt0c6d4192014-11-12 12:07:10 -08001466 noWebSock(false);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001467 };
1468
1469 webSock.ws.onmessage = function(m) {
1470 if (m.data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001471 wsTraceRx(m.data);
Thomas Vachuskad472c6e2014-11-07 19:11:05 -08001472 handleServerEvent(JSON.parse(m.data));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001473 }
1474 };
1475
1476 webSock.ws.onclose = function(m) {
1477 webSock.ws = null;
Simon Hunt0c6d4192014-11-12 12:07:10 -08001478 noWebSock(true);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001479 };
1480 },
1481
1482 send : function(text) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001483 if (text != null) {
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001484 webSock._send(text);
1485 }
1486 },
1487
1488 _send : function(message) {
1489 if (webSock.ws) {
1490 webSock.ws.send(message);
Simon Hunta255a2c2014-11-13 22:29:35 -08001491 } else if (config.useLiveData) {
Simon Hunt56d51852014-11-09 13:03:35 -08001492 network.view.alert('no web socket open\n\n' + message);
Simon Hunta255a2c2014-11-13 22:29:35 -08001493 } else {
1494 console.log('WS Send: ' + JSON.stringify(message));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001495 }
1496 }
1497
1498 };
1499
Simon Hunt0c6d4192014-11-12 12:07:10 -08001500 function noWebSock(b) {
1501 mask.style('display',b ? 'block' : 'none');
1502 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001503
Simon Hunt61d04042014-11-11 17:27:16 -08001504 // TODO: use cache of pending messages (key = sid) to reconcile responses
1505
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001506 function sendMessage(evType, payload) {
1507 var toSend = {
Simon Huntbb282f52014-11-10 11:08:19 -08001508 event: evType,
1509 sid: ++sid,
1510 payload: payload
1511 },
1512 asText = JSON.stringify(toSend);
1513 wsTraceTx(asText);
1514 webSock.send(asText);
1515 }
1516
1517 function wsTraceTx(msg) {
1518 wsTrace('tx', msg);
1519 }
1520 function wsTraceRx(msg) {
1521 wsTrace('rx', msg);
1522 }
1523 function wsTrace(rxtx, msg) {
Simon Huntbb282f52014-11-10 11:08:19 -08001524 console.log('[' + rxtx + '] ' + msg);
1525 // TODO: integrate with trace view
1526 //if (trace) {
1527 // trace.output(rxtx, msg);
1528 //}
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001529 }
1530
1531
1532 // ==============================
1533 // Selection stuff
1534
1535 function selectObject(obj, el) {
1536 var n,
Simon Hunt01095ff2014-11-13 16:37:29 -08001537 srcEv = d3.event.sourceEvent,
1538 meta = srcEv.metaKey,
1539 shift = srcEv.shiftKey;
1540
Simon Huntdeab4322014-11-13 18:49:07 -08001541 if ((panZoom() && !meta) || (!panZoom() && meta)) {
Simon Hunt01095ff2014-11-13 16:37:29 -08001542 return;
1543 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001544
1545 if (el) {
1546 n = d3.select(el);
1547 } else {
1548 node.each(function(d) {
1549 if (d == obj) {
1550 n = d3.select(el = this);
1551 }
1552 });
1553 }
1554 if (!n) return;
1555
Simon Hunt01095ff2014-11-13 16:37:29 -08001556 if (shift && n.classed('selected')) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001557 deselectObject(obj.id);
Simon Hunt61d04042014-11-11 17:27:16 -08001558 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001559 return;
1560 }
1561
Simon Hunt01095ff2014-11-13 16:37:29 -08001562 if (!shift) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001563 deselectAll();
1564 }
1565
Simon Huntc31d5692014-11-12 13:27:18 -08001566 selections[obj.id] = { obj: obj, el: el };
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001567 selectOrder.push(obj.id);
1568
1569 n.classed('selected', true);
Simon Hunt61d04042014-11-11 17:27:16 -08001570 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001571 }
1572
1573 function deselectObject(id) {
Simon Huntc31d5692014-11-12 13:27:18 -08001574 var obj = selections[id],
1575 idx;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001576 if (obj) {
1577 d3.select(obj.el).classed('selected', false);
Simon Hunt61d04042014-11-11 17:27:16 -08001578 delete selections[id];
Simon Huntc31d5692014-11-12 13:27:18 -08001579 idx = $.inArray(id, selectOrder);
1580 if (idx >= 0) {
1581 selectOrder.splice(idx, 1);
1582 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001583 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001584 }
1585
1586 function deselectAll() {
1587 // deselect all nodes in the network...
1588 node.classed('selected', false);
1589 selections = {};
1590 selectOrder = [];
Simon Hunt61d04042014-11-11 17:27:16 -08001591 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001592 }
1593
Simon Hunt61d04042014-11-11 17:27:16 -08001594 // update the state of the detail pane, based on current selections
1595 function updateDetailPane() {
1596 var nSel = selectOrder.length;
1597 if (!nSel) {
1598 detailPane.hide();
Simon Huntd72bc702014-11-13 18:38:04 -08001599 showTrafficAction(); // sends cancelTraffic event
Simon Hunt61d04042014-11-11 17:27:16 -08001600 } else if (nSel === 1) {
1601 singleSelect();
1602 } else {
1603 multiSelect();
1604 }
1605 }
1606
1607 function singleSelect() {
1608 requestDetails();
Simon Huntb53e0682014-11-12 13:32:01 -08001609 // NOTE: detail pane will be shown from showDetails event callback
Simon Hunt61d04042014-11-11 17:27:16 -08001610 }
1611
1612 function multiSelect() {
Simon Huntb53e0682014-11-12 13:32:01 -08001613 populateMultiSelect();
Simon Huntb53e0682014-11-12 13:32:01 -08001614 }
1615
1616 function addSep(tbody) {
1617 var tr = tbody.append('tr');
1618 $('<hr>').appendTo(tr.append('td').attr('colspan', 2));
1619 }
1620
1621 function addProp(tbody, label, value) {
1622 var tr = tbody.append('tr');
1623
1624 tr.append('td')
1625 .attr('class', 'label')
1626 .text(label + ' :');
1627
1628 tr.append('td')
1629 .attr('class', 'value')
1630 .text(value);
1631 }
1632
1633 function populateMultiSelect() {
1634 detailPane.empty();
1635
1636 var title = detailPane.append("h2"),
1637 table = detailPane.append("table"),
1638 tbody = table.append("tbody");
1639
1640 title.text('Multi-Select...');
1641
1642 selectOrder.forEach(function (d, i) {
1643 addProp(tbody, i+1, d);
1644 });
Simon Huntd72bc702014-11-13 18:38:04 -08001645
1646 addMultiSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08001647 }
1648
1649 function populateDetails(data) {
1650 detailPane.empty();
1651
1652 var title = detailPane.append("h2"),
1653 table = detailPane.append("table"),
1654 tbody = table.append("tbody");
1655
1656 $('<img src="img/' + data.type + '.png">').appendTo(title);
1657 $('<span>').attr('class', 'icon').text(data.id).appendTo(title);
1658
1659 data.propOrder.forEach(function(p) {
1660 if (p === '-') {
1661 addSep(tbody);
1662 } else {
1663 addProp(tbody, p, data.props[p]);
1664 }
1665 });
Simon Huntd72bc702014-11-13 18:38:04 -08001666
1667 addSingleSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08001668 }
1669
Simon Huntd72bc702014-11-13 18:38:04 -08001670 function addSingleSelectActions() {
1671 detailPane.append('hr');
1672 // always want to allow 'show traffic'
Simon Hunta5e89142014-11-14 07:00:33 -08001673 addAction(detailPane, 'Show Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001674 }
1675
1676 function addMultiSelectActions() {
1677 detailPane.append('hr');
1678 // always want to allow 'show traffic'
Simon Hunta5e89142014-11-14 07:00:33 -08001679 addAction(detailPane, 'Show Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001680 // if exactly two hosts are selected, also want 'add host intent'
1681 if (nSel() === 2 && allSelectionsClass('host')) {
Simon Hunta5e89142014-11-14 07:00:33 -08001682 addAction(detailPane, 'Add Host Intent', addIntentAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001683 }
1684 }
1685
Simon Hunta5e89142014-11-14 07:00:33 -08001686 function addAction(panel, text, cb) {
1687 panel.append('div')
Simon Huntd72bc702014-11-13 18:38:04 -08001688 .classed('actionBtn', true)
1689 .text(text)
1690 .on('click', cb);
1691 }
1692
1693
Paul Greysonfcba0e82014-11-13 10:21:16 -08001694 function zoomPan(scale, translate) {
1695 zoomPanContainer.attr("transform", "translate(" + translate + ")scale(" + scale + ")");
1696 // keep the map lines constant width while zooming
1697 bgImg.style("stroke-width", 2.0 / scale + "px");
1698 }
1699
1700 function resetZoomPan() {
1701 zoomPan(1, [0,0]);
1702 zoom.scale(1).translate([0,0]);
1703 }
1704
1705 function setupZoomPan() {
1706 function zoomed() {
Simon Huntdeab4322014-11-13 18:49:07 -08001707 if (!panZoom() ^ !d3.event.sourceEvent.metaKey) {
Paul Greysonfcba0e82014-11-13 10:21:16 -08001708 zoomPan(d3.event.scale, d3.event.translate);
1709 }
1710 }
1711
1712 zoom = d3.behavior.zoom()
1713 .translate([0, 0])
1714 .scale(1)
1715 .scaleExtent([1, 8])
1716 .on("zoom", zoomed);
1717
1718 svg.call(zoom);
1719 }
1720
Simon Hunt61d04042014-11-11 17:27:16 -08001721 // ==============================
1722 // Test harness code
Simon Hunt56d51852014-11-09 13:03:35 -08001723
1724 function prepareScenario(view, ctx, dbg) {
1725 var sc = scenario,
1726 urlSc = sc.evDir + ctx + sc.evScenario;
1727
1728 if (!ctx) {
1729 view.alert("No scenario specified (null ctx)");
1730 return;
1731 }
1732
1733 sc.view = view;
1734 sc.ctx = ctx;
1735 sc.debug = dbg;
1736 sc.evNumber = 0;
1737
1738 d3.json(urlSc, function(err, data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001739 var p = data && data.params || {},
1740 desc = data && data.description || null,
Simon Huntfc274c92014-11-11 11:05:46 -08001741 intro = data && data.title;
Simon Huntbb282f52014-11-10 11:08:19 -08001742
Simon Hunt56d51852014-11-09 13:03:35 -08001743 if (err) {
1744 view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err);
1745 } else {
1746 sc.params = p;
Simon Huntbb282f52014-11-10 11:08:19 -08001747 if (desc) {
1748 intro += '\n\n ' + desc.join('\n ');
1749 }
1750 view.alert(intro);
Simon Hunt56d51852014-11-09 13:03:35 -08001751 }
1752 });
1753
1754 }
1755
Simon Hunt01095ff2014-11-13 16:37:29 -08001756 // ==============================
1757 // Toggle Buttons in masthead
Simon Hunt0c6d4192014-11-12 12:07:10 -08001758
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001759 // TODO: toggle button (and other widgets in the masthead) should be provided
1760 // by the framework; not generated by the view.
1761
Simon Hunta5e89142014-11-14 07:00:33 -08001762 var showInstances,
1763 doPanZoom,
1764 showTrafficOnHover;
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001765
1766 function addButtonBar(view) {
1767 var bb = d3.select('#mast')
1768 .append('span').classed('right', true).attr('id', 'bb');
1769
Simon Hunta5e89142014-11-14 07:00:33 -08001770 function mkTogBtn(text, cb) {
1771 return bb.append('span')
1772 .classed('btn', true)
1773 .text(text)
1774 .on('click', cb);
1775 }
Simon Hunt01095ff2014-11-13 16:37:29 -08001776
Simon Hunta5e89142014-11-14 07:00:33 -08001777 showInstances = mkTogBtn('Show Instances', toggleInst);
1778 doPanZoom = mkTogBtn('Pan/Zoom', togglePanZoom);
1779 showTrafficOnHover = mkTogBtn('Show traffic on hover', toggleTrafficHover);
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001780 }
1781
Simon Hunta5e89142014-11-14 07:00:33 -08001782 function instShown() {
1783 return showInstances.classed('active');
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001784 }
Simon Hunta5e89142014-11-14 07:00:33 -08001785 function toggleInst() {
1786 showInstances.classed('active', !instShown());
1787 if (instShown()) {
1788 oiBox.show();
1789 } else {
1790 oiBox.hide();
1791 }
Simon Hunt01095ff2014-11-13 16:37:29 -08001792 }
1793
Simon Huntdeab4322014-11-13 18:49:07 -08001794 function panZoom() {
1795 return doPanZoom.classed('active');
Simon Hunt01095ff2014-11-13 16:37:29 -08001796 }
Simon Hunta5e89142014-11-14 07:00:33 -08001797 function togglePanZoom() {
1798 doPanZoom.classed('active', !panZoom());
1799 }
1800
1801 function trafficHover() {
1802 return showTrafficOnHover.classed('active');
1803 }
1804 function toggleTrafficHover() {
1805 showTrafficOnHover.classed('active', !trafficHover());
1806 }
1807
Simon Hunt7fa116d2014-11-17 14:16:55 -08001808 function loadGlyphs(svg) {
1809 var defs = svg.append('defs');
1810 gly.defBird(defs);
1811 gly.defBullhorn(defs);
1812 }
Simon Hunt01095ff2014-11-13 16:37:29 -08001813
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001814 // ==============================
Simon Hunt142d0032014-11-04 20:13:09 -08001815 // View life-cycle callbacks
Simon Hunt195cb382014-11-03 17:50:51 -08001816
Simon Huntf67722a2014-11-10 09:32:06 -08001817 function preload(view, ctx, flags) {
Simon Hunt142d0032014-11-04 20:13:09 -08001818 var w = view.width(),
1819 h = view.height(),
Simon Huntc7ee0662014-11-05 16:44:37 -08001820 fcfg = config.force,
1821 fpad = fcfg.pad,
1822 forceDim = [w - 2*fpad, h - 2*fpad];
Simon Hunt195cb382014-11-03 17:50:51 -08001823
Simon Hunt142d0032014-11-04 20:13:09 -08001824 // NOTE: view.$div is a D3 selection of the view's div
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001825 var viewBox = '0 0 ' + config.logicalSize + ' ' + config.logicalSize;
1826 svg = view.$div.append('svg').attr('viewBox', viewBox);
Simon Hunt934c3ce2014-11-05 11:45:07 -08001827 setSize(svg, view);
1828
Simon Hunt7fa116d2014-11-17 14:16:55 -08001829 loadGlyphs(svg);
Simon Hunt12ce12e2014-11-15 21:13:19 -08001830
Paul Greysonfcba0e82014-11-13 10:21:16 -08001831 zoomPanContainer = svg.append('g').attr('id', 'zoomPanContainer');
Paul Greysonfcba0e82014-11-13 10:21:16 -08001832 setupZoomPan();
1833
Simon Hunt1a9eff92014-11-07 11:06:34 -08001834 // add blue glow filter to svg layer
Paul Greysonfcba0e82014-11-13 10:21:16 -08001835 d3u.appendGlow(zoomPanContainer);
Simon Hunt1a9eff92014-11-07 11:06:34 -08001836
Simon Huntc7ee0662014-11-05 16:44:37 -08001837 // group for the topology
Paul Greysonfcba0e82014-11-13 10:21:16 -08001838 topoG = zoomPanContainer.append('g')
Simon Huntd3b7d512014-11-12 15:48:41 -08001839 .attr('id', 'topo-G')
Simon Huntc7ee0662014-11-05 16:44:37 -08001840 .attr('transform', fcfg.translate());
1841
1842 // subgroups for links and nodes
1843 linkG = topoG.append('g').attr('id', 'links');
1844 nodeG = topoG.append('g').attr('id', 'nodes');
1845
1846 // selection of nodes and links
1847 link = linkG.selectAll('.link');
1848 node = nodeG.selectAll('.node');
1849
Simon Hunt7cd48f32014-11-09 23:42:50 -08001850 function chrg(d) {
1851 return fcfg.charge[d.class] || -12000;
1852 }
Simon Hunt99c13842014-11-06 18:23:12 -08001853 function ldist(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08001854 return fcfg.linkDistance[d.type] || 50;
Simon Hunt99c13842014-11-06 18:23:12 -08001855 }
1856 function lstrg(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08001857 // 0.0 - 1.0
1858 return fcfg.linkStrength[d.type] || 1.0;
Simon Hunt99c13842014-11-06 18:23:12 -08001859 }
1860
Simon Hunt1a9eff92014-11-07 11:06:34 -08001861 function selectCb(d, self) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001862 selectObject(d, self);
Simon Hunt1a9eff92014-11-07 11:06:34 -08001863 }
1864
1865 function atDragEnd(d, self) {
Simon Hunt56d51852014-11-09 13:03:35 -08001866 // once we've finished moving, pin the node in position
1867 d.fixed = true;
1868 d3.select(self).classed('fixed', true);
1869 if (config.useLiveData) {
Simon Hunt902c9922014-11-11 11:59:31 -08001870 sendUpdateMeta(d);
Simon Hunta255a2c2014-11-13 22:29:35 -08001871 } else {
1872 console.log('Moving node ' + d.id + ' to [' + d.x + ',' + d.y + ']');
Simon Hunt1a9eff92014-11-07 11:06:34 -08001873 }
1874 }
1875
Simon Hunt902c9922014-11-11 11:59:31 -08001876 function sendUpdateMeta(d) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001877 sendMessage('updateMeta', {
1878 id: d.id,
1879 'class': d.class,
Simon Hunt902c9922014-11-11 11:59:31 -08001880 'memento': {
Simon Hunt01095ff2014-11-13 16:37:29 -08001881 x: d.x,
1882 y: d.y
Simon Hunt902c9922014-11-11 11:59:31 -08001883 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001884 });
1885 }
1886
Simon Huntc7ee0662014-11-05 16:44:37 -08001887 // set up the force layout
1888 network.force = d3.layout.force()
1889 .size(forceDim)
1890 .nodes(network.nodes)
1891 .links(network.links)
Simon Hunt7cd48f32014-11-09 23:42:50 -08001892 .gravity(0.4)
1893 .friction(0.7)
1894 .charge(chrg)
Simon Hunt99c13842014-11-06 18:23:12 -08001895 .linkDistance(ldist)
1896 .linkStrength(lstrg)
Simon Huntc7ee0662014-11-05 16:44:37 -08001897 .on('tick', tick);
Simon Hunt195cb382014-11-03 17:50:51 -08001898
Simon Hunt01095ff2014-11-13 16:37:29 -08001899 network.drag = d3u.createDragBehavior(network.force,
Simon Huntdeab4322014-11-13 18:49:07 -08001900 selectCb, atDragEnd, panZoom);
Simon Hunt0c6d4192014-11-12 12:07:10 -08001901
1902 // create mask layer for when we lose connection to server.
Simon Hunta5e89142014-11-14 07:00:33 -08001903 // TODO: this should be part of the framework
Simon Hunt0c6d4192014-11-12 12:07:10 -08001904 mask = view.$div.append('div').attr('id','topo-mask');
1905 para(mask, 'Oops!');
1906 para(mask, 'Web-socket connection to server closed...');
1907 para(mask, 'Try refreshing the page.');
Simon Hunt12ce12e2014-11-15 21:13:19 -08001908
1909 mask.append('svg')
1910 .attr({
1911 id: 'mask-bird',
1912 width: w,
1913 height: h
1914 })
1915 .append('g')
1916 .attr('transform', birdTranslate(w, h))
1917 .style('opacity', 0.3)
1918 .append('use')
1919 .attr({
1920 'xlink:href': '#bird',
1921 width: config.birdDim,
1922 height: config.birdDim,
1923 fill: '#111'
1924 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08001925 }
Simon Hunt195cb382014-11-03 17:50:51 -08001926
Simon Hunt01095ff2014-11-13 16:37:29 -08001927 function para(sel, text) {
1928 sel.append('p').text(text);
1929 }
1930
1931
Simon Hunt56d51852014-11-09 13:03:35 -08001932 function load(view, ctx, flags) {
Simon Huntf67722a2014-11-10 09:32:06 -08001933 // resize, in case the window was resized while we were not loaded
1934 resize(view, ctx, flags);
1935
Simon Hunt99c13842014-11-06 18:23:12 -08001936 // cache the view token, so network topo functions can access it
1937 network.view = view;
Simon Hunt56d51852014-11-09 13:03:35 -08001938 config.useLiveData = !flags.local;
1939
1940 if (!config.useLiveData) {
1941 prepareScenario(view, ctx, flags.debug);
1942 }
Simon Hunt99c13842014-11-06 18:23:12 -08001943
1944 // set our radio buttons and key bindings
Simon Hunt9462e8c2014-11-14 17:28:09 -08001945 layerBtnSet = view.setRadio(layerButtons);
Simon Hunt934c3ce2014-11-05 11:45:07 -08001946 view.setKeys(keyDispatch);
Simon Hunt195cb382014-11-03 17:50:51 -08001947
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001948 // patch in our "button bar" for now
1949 // TODO: implement a more official frameworky way of doing this..
1950 addButtonBar(view);
1951
Simon Huntd3b7d512014-11-12 15:48:41 -08001952 // Load map data asynchronously; complete startup after that..
1953 loadGeoJsonData();
Simon Hunta255a2c2014-11-13 22:29:35 -08001954
1955 // start the and timer
1956 var dashIdx = 0;
1957 antTimer = setInterval(function () {
1958 // TODO: figure out how to choose Src-->Dst and Dst-->Src, per link
1959 dashIdx = dashIdx === 0 ? 14 : dashIdx - 2;
1960 d3.selectAll('.animated').style('stroke-dashoffset', dashIdx);
1961 }, 35);
1962 }
1963
1964 function unload(view, ctx, flags) {
1965 if (antTimer) {
1966 clearInterval(antTimer);
1967 antTimer = null;
1968 }
Simon Huntd3b7d512014-11-12 15:48:41 -08001969 }
1970
1971 // TODO: move these to config/state portion of script
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001972 var geoJsonUrl = 'json/map/continental_us.json', // TODO: Paul
Simon Huntd3b7d512014-11-12 15:48:41 -08001973 geoJson;
1974
1975 function loadGeoJsonData() {
1976 d3.json(geoJsonUrl, function (err, data) {
1977 if (err) {
1978 // fall back to USA map background
1979 loadStaticMap();
1980 } else {
1981 geoJson = data;
1982 loadGeoMap();
1983 }
1984
1985 // finally, connect to the server...
1986 if (config.useLiveData) {
1987 webSock.connect();
1988 }
1989 });
1990 }
1991
1992 function showBg() {
1993 return config.options.showBackground ? 'visible' : 'hidden';
1994 }
1995
1996 function loadStaticMap() {
1997 fnTrace('loadStaticMap', config.backgroundUrl);
1998 var w = network.view.width(),
1999 h = network.view.height();
2000
2001 // load the background image
2002 bgImg = svg.insert('svg:image', '#topo-G')
2003 .attr({
2004 id: 'topo-bg',
2005 width: w,
2006 height: h,
2007 'xlink:href': config.backgroundUrl
2008 })
2009 .style({
2010 visibility: showBg()
2011 });
2012 }
2013
2014 function loadGeoMap() {
2015 fnTrace('loadGeoMap', geoJsonUrl);
Simon Huntd3b7d512014-11-12 15:48:41 -08002016
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002017 // extracts the topojson data into geocoordinate-based geometry
2018 var topoData = topojson.feature(geoJson, geoJson.objects.states);
Simon Huntd3b7d512014-11-12 15:48:41 -08002019
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002020 // see: http://bl.ocks.org/mbostock/4707858
2021 geoMapProjection = d3.geo.mercator();
2022 var path = d3.geo.path().projection(geoMapProjection);
Simon Huntd3b7d512014-11-12 15:48:41 -08002023
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002024 geoMapProjection
2025 .scale(1)
2026 .translate([0, 0]);
Simon Huntd3b7d512014-11-12 15:48:41 -08002027
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002028 // [[x1,y1],[x2,y2]]
2029 var b = path.bounds(topoData);
Paul Greysonfcba0e82014-11-13 10:21:16 -08002030 // size map to 95% of minimum dimension to fill space
2031 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 -08002032 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 -08002033
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002034 geoMapProjection
2035 .scale(s)
2036 .translate(t);
2037
Paul Greysonfcba0e82014-11-13 10:21:16 -08002038 bgImg = zoomPanContainer.insert("g", '#topo-G');
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002039 bgImg.attr('id', 'map').selectAll('path')
2040 .data(topoData.features)
2041 .enter()
2042 .append('path')
2043 .attr('d', path);
Simon Hunt195cb382014-11-03 17:50:51 -08002044 }
2045
Simon Huntf67722a2014-11-10 09:32:06 -08002046 function resize(view, ctx, flags) {
Simon Hunt12ce12e2014-11-15 21:13:19 -08002047 var w = view.width(),
2048 h = view.height();
2049
Simon Hunt934c3ce2014-11-05 11:45:07 -08002050 setSize(svg, view);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002051
2052 d3.select('#mask-bird').attr({ width: w, height: h})
2053 .select('g').attr('transform', birdTranslate(w, h));
Simon Hunt142d0032014-11-04 20:13:09 -08002054 }
2055
Simon Hunt12ce12e2014-11-15 21:13:19 -08002056 function birdTranslate(w, h) {
2057 var bdim = config.birdDim;
2058 return 'translate('+((w-bdim)*.4)+','+((h-bdim)*.1)+')';
2059 }
Simon Hunt142d0032014-11-04 20:13:09 -08002060
2061 // ==============================
2062 // View registration
Simon Hunt195cb382014-11-03 17:50:51 -08002063
Simon Hunt25248912014-11-04 11:25:48 -08002064 onos.ui.addView('topo', {
Simon Hunt142d0032014-11-04 20:13:09 -08002065 preload: preload,
2066 load: load,
Simon Hunta255a2c2014-11-13 22:29:35 -08002067 unload: unload,
Simon Hunt142d0032014-11-04 20:13:09 -08002068 resize: resize
Simon Hunt195cb382014-11-03 17:50:51 -08002069 });
2070
Simon Hunt61d04042014-11-11 17:27:16 -08002071 detailPane = onos.ui.addFloatingPanel('topo-detail');
Simon Hunta5e89142014-11-14 07:00:33 -08002072 oiBox = onos.ui.addFloatingPanel('topo-oibox', 'TL');
Simon Hunt61d04042014-11-11 17:27:16 -08002073
Simon Hunt195cb382014-11-03 17:50:51 -08002074}(ONOS));