blob: b1495bfd9d47dc8fa9870ea3845bcb3a8625e45e [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: {
Simon Hunt13bf9c82014-11-18 07:26:44 -080073 linkBaseColor: '#666',
Simon Hunt1a9eff92014-11-07 11:06:34 -080074 linkInColor: '#66f',
Simon Hunt13bf9c82014-11-18 07:26:44 -080075 linkInWidth: 14,
76 linkOutColor: '#f00',
77 linkOutWidth: 30
Simon Hunt1a9eff92014-11-07 11:06:34 -080078 },
Simon Hunt99c13842014-11-06 18:23:12 -080079 icons: {
80 w: 28,
81 h: 28,
82 xoff: -12,
83 yoff: -8
84 },
Simon Hunt195cb382014-11-03 17:50:51 -080085 iconUrl: {
86 device: 'img/device.png',
87 host: 'img/host.png',
88 pkt: 'img/pkt.png',
89 opt: 'img/opt.png'
90 },
Simon Hunt195cb382014-11-03 17:50:51 -080091 force: {
Simon Hunt7cd48f32014-11-09 23:42:50 -080092 note_for_links: 'link.type is used to differentiate',
Simon Huntc7ee0662014-11-05 16:44:37 -080093 linkDistance: {
Simon Hunt7cd48f32014-11-09 23:42:50 -080094 direct: 100,
95 optical: 120,
Thomas Vachuska3266abf2014-11-13 09:28:46 -080096 hostLink: 3
Simon Huntc7ee0662014-11-05 16:44:37 -080097 },
98 linkStrength: {
Simon Hunt7cd48f32014-11-09 23:42:50 -080099 direct: 1.0,
100 optical: 1.0,
101 hostLink: 1.0
Simon Huntc7ee0662014-11-05 16:44:37 -0800102 },
Simon Hunt7cd48f32014-11-09 23:42:50 -0800103 note_for_nodes: 'node.class is used to differentiate',
Simon Huntc7ee0662014-11-05 16:44:37 -0800104 charge: {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800105 device: -8000,
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800106 host: -5000
Simon Huntc7ee0662014-11-05 16:44:37 -0800107 },
108 pad: 20,
Simon Hunt195cb382014-11-03 17:50:51 -0800109 translate: function() {
110 return 'translate(' +
Simon Huntc7ee0662014-11-05 16:44:37 -0800111 config.force.pad + ',' +
112 config.force.pad + ')';
Simon Hunt195cb382014-11-03 17:50:51 -0800113 }
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800114 },
115 // see below in creation of viewBox on main svg
116 logicalSize: 1000
Simon Hunt195cb382014-11-03 17:50:51 -0800117 };
118
Simon Hunt142d0032014-11-04 20:13:09 -0800119 // radio buttons
Simon Hunt9462e8c2014-11-14 17:28:09 -0800120 var layerButtons = [
121 { text: 'All Layers', id: 'all', cb: showAllLayers },
122 { text: 'Packet Only', id: 'pkt', cb: showPacketLayer },
123 { text: 'Optical Only', id: 'opt', cb: showOpticalLayer }
124 ],
125 layerBtnSet,
126 layerBtnDispatch = {
127 all: showAllLayers,
128 pkt: showPacketLayer,
129 opt: showOpticalLayer
130 };
Simon Hunt934c3ce2014-11-05 11:45:07 -0800131
132 // key bindings
133 var keyDispatch = {
Simon Hunta255a2c2014-11-13 22:29:35 -0800134 M: testMe, // TODO: remove (testing only)
135 S: injectStartupEvents, // TODO: remove (testing only)
136 space: injectTestEvent, // TODO: remove (testing only)
Simon Hunt99c13842014-11-06 18:23:12 -0800137
Simon Hunt01095ff2014-11-13 16:37:29 -0800138 B: toggleBg,
Simon Hunt934c3ce2014-11-05 11:45:07 -0800139 L: cycleLabels,
140 P: togglePorts,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800141 U: unpin,
Paul Greysonfcba0e82014-11-13 10:21:16 -0800142 R: resetZoomPan,
Simon Hunt9462e8c2014-11-14 17:28:09 -0800143 esc: handleEscape
Simon Hunt934c3ce2014-11-05 11:45:07 -0800144 };
Simon Hunt142d0032014-11-04 20:13:09 -0800145
Simon Hunt195cb382014-11-03 17:50:51 -0800146 // state variables
Simon Hunt99c13842014-11-06 18:23:12 -0800147 var network = {
Simon Hunt50128c02014-11-08 13:36:15 -0800148 view: null, // view token reference
Simon Hunt99c13842014-11-06 18:23:12 -0800149 nodes: [],
150 links: [],
Simon Hunt269670f2014-11-17 16:17:43 -0800151 lookup: {},
152 revLinkToKey: {}
Simon Hunt99c13842014-11-06 18:23:12 -0800153 },
Simon Hunt56d51852014-11-09 13:03:35 -0800154 scenario = {
155 evDir: 'json/ev/',
156 evScenario: '/scenario.json',
157 evPrefix: '/ev_',
158 evOnos: '_onos.json',
159 evUi: '_ui.json',
160 ctx: null,
161 params: {},
162 evNumber: 0,
163 view: null,
164 debug: false
165 },
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800166 webSock,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800167 sid = 0,
Simon Hunt56d51852014-11-09 13:03:35 -0800168 deviceLabelIndex = 0,
169 hostLabelIndex = 0,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800170 selections = {},
Simon Hunta5e89142014-11-14 07:00:33 -0800171 selectOrder = [],
Simon Hunt6ac93f32014-11-13 12:17:27 -0800172 hovered = null,
Simon Hunta5e89142014-11-14 07:00:33 -0800173 detailPane,
Simon Hunta255a2c2014-11-13 22:29:35 -0800174 antTimer = null,
Simon Hunta5e89142014-11-14 07:00:33 -0800175 onosInstances = {},
176 onosOrder = [],
177 oiBox,
Simon Hunt9462e8c2014-11-14 17:28:09 -0800178 oiShowMaster = false,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800179
Simon Hunt195cb382014-11-03 17:50:51 -0800180 portLabelsOn = false;
181
Simon Hunt934c3ce2014-11-05 11:45:07 -0800182 // D3 selections
183 var svg,
Paul Greysonfcba0e82014-11-13 10:21:16 -0800184 zoomPanContainer,
Simon Hunt934c3ce2014-11-05 11:45:07 -0800185 bgImg,
Simon Huntc7ee0662014-11-05 16:44:37 -0800186 topoG,
187 nodeG,
188 linkG,
189 node,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800190 link,
191 mask;
Simon Hunt195cb382014-11-03 17:50:51 -0800192
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800193 // the projection for the map background
194 var geoMapProjection;
195
Paul Greysonfcba0e82014-11-13 10:21:16 -0800196 // the zoom function
197 var zoom;
198
Simon Hunt142d0032014-11-04 20:13:09 -0800199 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800200 // For Debugging / Development
Simon Hunt195cb382014-11-03 17:50:51 -0800201
Simon Hunt99c13842014-11-06 18:23:12 -0800202 function note(label, msg) {
203 console.log('NOTE: ' + label + ': ' + msg);
Simon Hunt195cb382014-11-03 17:50:51 -0800204 }
205
Simon Hunt99c13842014-11-06 18:23:12 -0800206 function debug(what) {
207 return config.debugOn && config.debug[what];
Simon Hunt934c3ce2014-11-05 11:45:07 -0800208 }
209
Simon Huntfc274c92014-11-11 11:05:46 -0800210 function fnTrace(msg, id) {
211 if (config.fnTrace) {
212 console.log('FN: ' + msg + ' [' + id + ']');
213 }
214 }
Simon Hunt99c13842014-11-06 18:23:12 -0800215
Simon Hunta5e89142014-11-14 07:00:33 -0800216 function evTrace(data) {
217 fnTrace(data.event, data.payload.id);
218 }
219
Simon Hunt934c3ce2014-11-05 11:45:07 -0800220 // ==============================
221 // Key Callbacks
222
Simon Hunt99c13842014-11-06 18:23:12 -0800223 function testMe(view) {
Simon Hunta5e89142014-11-14 07:00:33 -0800224 //view.alert('test');
Simon Hunt12ce12e2014-11-15 21:13:19 -0800225 noWebSock(true);
Simon Hunt99c13842014-11-06 18:23:12 -0800226 }
227
Simon Hunt56d51852014-11-09 13:03:35 -0800228 function abortIfLive() {
Simon Hunt50128c02014-11-08 13:36:15 -0800229 if (config.useLiveData) {
Simon Huntb53e0682014-11-12 13:32:01 -0800230 network.view.alert("Sorry, currently using live data..");
Simon Hunt56d51852014-11-09 13:03:35 -0800231 return true;
Simon Hunt50128c02014-11-08 13:36:15 -0800232 }
Simon Hunt56d51852014-11-09 13:03:35 -0800233 return false;
234 }
Simon Hunt50128c02014-11-08 13:36:15 -0800235
Simon Hunt56d51852014-11-09 13:03:35 -0800236 function testDebug(msg) {
237 if (scenario.debug) {
238 scenario.view.alert(msg);
239 }
240 }
Simon Hunt99c13842014-11-06 18:23:12 -0800241
Simon Hunt56d51852014-11-09 13:03:35 -0800242 function injectTestEvent(view) {
243 if (abortIfLive()) { return; }
244 var sc = scenario,
245 evn = ++sc.evNumber,
246 pfx = sc.evDir + sc.ctx + sc.evPrefix + evn,
247 onosUrl = pfx + sc.evOnos,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800248 uiUrl = pfx + sc.evUi,
249 stack = [
250 { url: onosUrl, cb: handleServerEvent },
251 { url: uiUrl, cb: handleUiEvent }
252 ];
253 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800254 }
255
Simon Hunt7cd48f32014-11-09 23:42:50 -0800256 function recurseFetchEvent(stack, evn) {
257 var v = scenario.view,
258 frame;
259 if (stack.length === 0) {
Simon Huntfc274c92014-11-11 11:05:46 -0800260 v.alert('Oops!\n\nNo event #' + evn + ' found.');
Simon Hunt7cd48f32014-11-09 23:42:50 -0800261 return;
262 }
263 frame = stack.shift();
264
265 d3.json(frame.url, function (err, data) {
Simon Hunt99c13842014-11-06 18:23:12 -0800266 if (err) {
Simon Hunt56d51852014-11-09 13:03:35 -0800267 if (err.status === 404) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800268 // if we didn't find the data, try the next stack frame
269 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800270 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800271 v.alert('non-404 error:\n\n' + frame.url + '\n\n' + err);
Simon Hunt56d51852014-11-09 13:03:35 -0800272 }
Simon Hunt99c13842014-11-06 18:23:12 -0800273 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800274 testDebug('loaded: ' + frame.url);
Simon Hunt1712ed82014-11-17 12:56:00 -0800275 wsTrace('test', JSON.stringify(data));
Simon Hunt7cd48f32014-11-09 23:42:50 -0800276 frame.cb(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800277 }
278 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800279
Simon Hunt56d51852014-11-09 13:03:35 -0800280 }
Simon Hunt50128c02014-11-08 13:36:15 -0800281
Simon Hunt56d51852014-11-09 13:03:35 -0800282 function handleUiEvent(data) {
Simon Huntbb282f52014-11-10 11:08:19 -0800283 scenario.view.alert('UI Tx: ' + data.event + '\n\n' +
284 JSON.stringify(data));
Simon Hunt56d51852014-11-09 13:03:35 -0800285 }
286
287 function injectStartupEvents(view) {
288 var last = scenario.params.lastAuto || 0;
289 if (abortIfLive()) { return; }
290
291 while (scenario.evNumber < last) {
Simon Hunt1a9eff92014-11-07 11:06:34 -0800292 injectTestEvent(view);
293 }
294 }
295
Simon Hunt934c3ce2014-11-05 11:45:07 -0800296 function toggleBg() {
297 var vis = bgImg.style('visibility');
298 bgImg.style('visibility', (vis === 'hidden') ? 'visible' : 'hidden');
299 }
300
Simon Hunt99c13842014-11-06 18:23:12 -0800301 function cycleLabels() {
Simon Huntbb282f52014-11-10 11:08:19 -0800302 deviceLabelIndex = (deviceLabelIndex === network.deviceLabelCount - 1)
303 ? 0 : deviceLabelIndex + 1;
Simon Hunt5f36d342014-11-08 21:33:14 -0800304
Simon Hunt99c13842014-11-06 18:23:12 -0800305 network.nodes.forEach(function (d) {
Simon Huntbb282f52014-11-10 11:08:19 -0800306 if (d.class === 'device') {
307 updateDeviceLabel(d);
308 }
Simon Hunt99c13842014-11-06 18:23:12 -0800309 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800310 }
311
312 function togglePorts(view) {
Simon Hunt50128c02014-11-08 13:36:15 -0800313 view.alert('togglePorts() callback')
Simon Hunt934c3ce2014-11-05 11:45:07 -0800314 }
315
Simon Hunt6ac93f32014-11-13 12:17:27 -0800316 function unpin() {
317 if (hovered) {
318 hovered.fixed = false;
319 hovered.el.classed('fixed', false);
320 network.force.resume();
321 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800322 }
323
Simon Hunt9462e8c2014-11-14 17:28:09 -0800324 function handleEscape(view) {
325 if (oiShowMaster) {
326 cancelAffinity();
327 } else {
328 deselectAll();
329 }
330 }
331
Simon Hunt934c3ce2014-11-05 11:45:07 -0800332 // ==============================
333 // Radio Button Callbacks
334
Simon Hunta5e89142014-11-14 07:00:33 -0800335 var layerLookup = {
336 host: {
337 endstation: 'pkt', // default, if host event does not define type
338 bgpSpeaker: 'pkt'
339 },
340 device: {
341 switch: 'pkt',
342 roadm: 'opt'
343 },
344 link: {
345 hostLink: 'pkt',
346 direct: 'pkt',
Simon Hunt8257f4c2014-11-16 19:34:54 -0800347 indirect: '',
348 tunnel: '',
Simon Hunta5e89142014-11-14 07:00:33 -0800349 optical: 'opt'
350 }
351 };
352
353 function inLayer(d, layer) {
Simon Hunt8257f4c2014-11-16 19:34:54 -0800354 var type = d.class === 'link' ? d.type() : d.type,
355 look = layerLookup[d.class],
356 lyr = look && look[type];
Simon Hunta5e89142014-11-14 07:00:33 -0800357 return lyr === layer;
358 }
359
360 function unsuppressLayer(which) {
361 node.each(function (d) {
362 var node = d.el;
363 if (inLayer(d, which)) {
364 node.classed('suppressed', false);
365 }
366 });
367
368 link.each(function (d) {
369 var link = d.el;
370 if (inLayer(d, which)) {
371 link.classed('suppressed', false);
372 }
373 });
374 }
375
Simon Hunt9462e8c2014-11-14 17:28:09 -0800376 function suppressLayers(b) {
377 node.classed('suppressed', b);
378 link.classed('suppressed', b);
Simon Hunt142d0032014-11-04 20:13:09 -0800379// d3.selectAll('svg .port').classed('inactive', false);
380// d3.selectAll('svg .portText').classed('inactive', false);
Simon Hunt195cb382014-11-03 17:50:51 -0800381 }
382
Simon Hunt9462e8c2014-11-14 17:28:09 -0800383 function showAllLayers() {
384 suppressLayers(false);
385 }
386
Simon Hunt195cb382014-11-03 17:50:51 -0800387 function showPacketLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800388 node.classed('suppressed', true);
389 link.classed('suppressed', true);
390 unsuppressLayer('pkt');
Simon Hunt195cb382014-11-03 17:50:51 -0800391 }
392
393 function showOpticalLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800394 node.classed('suppressed', true);
395 link.classed('suppressed', true);
396 unsuppressLayer('opt');
Simon Hunt195cb382014-11-03 17:50:51 -0800397 }
398
Simon Hunt9462e8c2014-11-14 17:28:09 -0800399 function restoreLayerState() {
400 layerBtnDispatch[layerBtnSet.selected()]();
401 }
402
Simon Hunt142d0032014-11-04 20:13:09 -0800403 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800404 // Private functions
405
Simon Hunt99c13842014-11-06 18:23:12 -0800406 function safeId(s) {
407 return s.replace(/[^a-z0-9]/gi, '-');
408 }
409
Simon Huntc7ee0662014-11-05 16:44:37 -0800410 // set the size of the given element to that of the view (reduced if padded)
411 function setSize(el, view, pad) {
412 var padding = pad ? pad * 2 : 0;
Simon Hunt934c3ce2014-11-05 11:45:07 -0800413 el.attr({
Simon Huntc7ee0662014-11-05 16:44:37 -0800414 width: view.width() - padding,
415 height: view.height() - padding
Simon Hunt934c3ce2014-11-05 11:45:07 -0800416 });
417 }
418
Simon Hunt8257f4c2014-11-16 19:34:54 -0800419 function makeNodeKey(d, what) {
420 var port = what + 'Port';
421 return d[what] + '/' + d[port];
422 }
423
424 function makeLinkKey(d, flipped) {
425 var one = flipped ? makeNodeKey(d, 'dst') : makeNodeKey(d, 'src'),
426 two = flipped ? makeNodeKey(d, 'src') : makeNodeKey(d, 'dst');
427 return one + '-' + two;
428 }
429
Simon Hunt269670f2014-11-17 16:17:43 -0800430 function findLinkById(id) {
431 // check to see if this is a reverse lookup, else default to given id
432 var key = network.revLinkToKey[id] || id;
433 return key && network.lookup[key];
434 }
435
Simon Hunt8257f4c2014-11-16 19:34:54 -0800436 function findLink(linkData, op) {
437 var key = makeLinkKey(linkData),
438 keyrev = makeLinkKey(linkData, 1),
439 link = network.lookup[key],
440 linkRev = network.lookup[keyrev],
441 result = {},
442 ldata = link || linkRev,
443 rawLink;
444
445 if (op === 'add') {
446 if (link) {
447 // trying to add a link that we already know about
448 result.ldata = link;
449 result.badLogic = 'addLink: link already added';
450
451 } else if (linkRev) {
452 // we found the reverse of the link to be added
453 result.ldata = linkRev;
454 if (linkRev.fromTarget) {
455 result.badLogic = 'addLink: link already added';
456 }
457 }
458 } else if (op === 'update') {
459 if (!ldata) {
460 result.badLogic = 'updateLink: link not found';
461 } else {
462 rawLink = link ? ldata.fromSource : ldata.fromTarget;
463 result.updateWith = function (data) {
464 $.extend(rawLink, data);
465 restyleLinkElement(ldata);
466 }
467 }
468 } else if (op === 'remove') {
469 if (!ldata) {
470 result.badLogic = 'removeLink: link not found';
471 } else {
472 rawLink = link ? ldata.fromSource : ldata.fromTarget;
473
474 if (!rawLink) {
475 result.badLogic = 'removeLink: link not found';
476
477 } else {
478 result.removeRawLink = function () {
479 if (link) {
480 // remove fromSource
481 ldata.fromSource = null;
482 if (ldata.fromTarget) {
483 // promote target into source position
484 ldata.fromSource = ldata.fromTarget;
485 ldata.fromTarget = null;
486 ldata.key = keyrev;
487 delete network.lookup[key];
488 network.lookup[keyrev] = ldata;
Simon Hunt269670f2014-11-17 16:17:43 -0800489 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800490 }
491 } else {
492 // remove fromTarget
493 ldata.fromTarget = null;
Simon Hunt269670f2014-11-17 16:17:43 -0800494 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800495 }
496 if (ldata.fromSource) {
497 restyleLinkElement(ldata);
498 } else {
499 removeLinkElement(ldata);
500 }
501 }
502 }
503 }
504 }
505 return result;
506 }
507
508 function addLinkUpdate(ldata, link) {
509 // add link event, but we already have the reverse link installed
510 ldata.fromTarget = link;
Simon Hunt269670f2014-11-17 16:17:43 -0800511 network.revLinkToKey[link.id] = ldata.key;
Simon Hunt8257f4c2014-11-16 19:34:54 -0800512 restyleLinkElement(ldata);
513 }
514
515 var allLinkTypes = 'direct indirect optical tunnel',
516 defaultLinkType = 'direct';
517
518 function restyleLinkElement(ldata) {
519 // this fn's job is to look at raw links and decide what svg classes
520 // need to be applied to the line element in the DOM
521 var el = ldata.el,
522 type = ldata.type(),
523 lw = ldata.linkWidth(),
524 online = ldata.online();
525
526 el.classed('link', true);
527 el.classed('inactive', !online);
528 el.classed(allLinkTypes, false);
529 if (type) {
530 el.classed(type, true);
531 }
532 el.transition()
533 .duration(1000)
534 .attr('stroke-width', linkScale(lw))
Simon Hunt13bf9c82014-11-18 07:26:44 -0800535 .attr('stroke', config.topo.linkBaseColor);
Simon Hunt8257f4c2014-11-16 19:34:54 -0800536 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800537
Simon Hunt99c13842014-11-06 18:23:12 -0800538 // ==============================
539 // Event handlers for server-pushed events
540
Simon Huntbb282f52014-11-10 11:08:19 -0800541 function logicError(msg) {
542 // TODO, report logic error to server, via websock, so it can be logged
Simon Huntcb56cff2014-11-17 11:42:26 -0800543 //network.view.alert('Logic Error:\n\n' + msg);
Simon Huntfc274c92014-11-11 11:05:46 -0800544 console.warn(msg);
Simon Huntbb282f52014-11-10 11:08:19 -0800545 }
546
Simon Hunt99c13842014-11-06 18:23:12 -0800547 var eventDispatch = {
Simon Hunta5e89142014-11-14 07:00:33 -0800548 addInstance: addInstance,
Simon Hunt99c13842014-11-06 18:23:12 -0800549 addDevice: addDevice,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800550 addLink: addLink,
Simon Hunt56d51852014-11-09 13:03:35 -0800551 addHost: addHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800552
Simon Huntd72bc702014-11-13 18:38:04 -0800553 updateInstance: stillToImplement,
Simon Huntbb282f52014-11-10 11:08:19 -0800554 updateDevice: updateDevice,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800555 updateLink: updateLink,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800556 updateHost: updateHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800557
Simon Huntd72bc702014-11-13 18:38:04 -0800558 removeInstance: stillToImplement,
Simon Huntbb282f52014-11-10 11:08:19 -0800559 removeDevice: stillToImplement,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800560 removeLink: removeLink,
Simon Hunt44031102014-11-11 13:20:36 -0800561 removeHost: removeHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800562
Simon Hunt61d04042014-11-11 17:27:16 -0800563 showDetails: showDetails,
Simon Huntb53e0682014-11-12 13:32:01 -0800564 showTraffic: showTraffic
Simon Hunt99c13842014-11-06 18:23:12 -0800565 };
566
Simon Hunta5e89142014-11-14 07:00:33 -0800567 function addInstance(data) {
568 evTrace(data);
569 var inst = data.payload,
570 id = inst.id;
571 if (onosInstances[id]) {
572 logicError('ONOS instance already added: ' + id);
573 return;
574 }
575 onosInstances[id] = inst;
576 onosOrder.push(inst);
577 updateInstances();
578 }
579
Simon Hunt99c13842014-11-06 18:23:12 -0800580 function addDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800581 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800582 var device = data.payload,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800583 nodeData = createDeviceNode(device);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800584 network.nodes.push(nodeData);
585 network.lookup[nodeData.id] = nodeData;
Simon Hunt99c13842014-11-06 18:23:12 -0800586 updateNodes();
587 network.force.start();
588 }
589
Simon Hunt99c13842014-11-06 18:23:12 -0800590 function addLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800591 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800592 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800593 result = findLink(link, 'add'),
594 bad = result.badLogic,
595 ldata = result.ldata;
596
597 if (bad) {
598 logicError(bad + ': ' + link.id);
599 return;
600 }
601
602 if (ldata) {
603 // we already have a backing store link for src/dst nodes
604 addLinkUpdate(ldata, link);
605 return;
606 }
607
608 // no backing store link yet
609 ldata = createLink(link);
610 if (ldata) {
611 network.links.push(ldata);
612 network.lookup[ldata.key] = ldata;
Simon Hunt99c13842014-11-06 18:23:12 -0800613 updateLinks();
614 network.force.start();
615 }
616 }
617
Simon Hunt56d51852014-11-09 13:03:35 -0800618 function addHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800619 evTrace(data);
Simon Hunt56d51852014-11-09 13:03:35 -0800620 var host = data.payload,
621 node = createHostNode(host),
622 lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800623 network.nodes.push(node);
624 network.lookup[host.id] = node;
625 updateNodes();
626
627 lnk = createHostLink(host);
628 if (lnk) {
Simon Hunt44031102014-11-11 13:20:36 -0800629 node.linkData = lnk; // cache ref on its host
Simon Hunt56d51852014-11-09 13:03:35 -0800630 network.links.push(lnk);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800631 network.lookup[host.ingress] = lnk;
632 network.lookup[host.egress] = lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800633 updateLinks();
634 }
635 network.force.start();
636 }
637
Simon Hunt44031102014-11-11 13:20:36 -0800638 // TODO: fold updateX(...) methods into one base method; remove duplication
Simon Huntbb282f52014-11-10 11:08:19 -0800639 function updateDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800640 evTrace(data);
Simon Huntbb282f52014-11-10 11:08:19 -0800641 var device = data.payload,
642 id = device.id,
643 nodeData = network.lookup[id];
644 if (nodeData) {
645 $.extend(nodeData, device);
646 updateDeviceState(nodeData);
647 } else {
648 logicError('updateDevice lookup fail. ID = "' + id + '"');
649 }
650 }
651
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800652 function updateLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800653 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800654 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800655 result = findLink(link, 'update'),
656 bad = result.badLogic;
657 if (bad) {
658 logicError(bad + ': ' + link.id);
659 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800660 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800661 result.updateWith(link);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800662 }
663
Simon Hunt7cd48f32014-11-09 23:42:50 -0800664 function updateHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800665 evTrace(data);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800666 var host = data.payload,
Simon Huntbb282f52014-11-10 11:08:19 -0800667 id = host.id,
668 hostData = network.lookup[id];
669 if (hostData) {
670 $.extend(hostData, host);
671 updateHostState(hostData);
672 } else {
673 logicError('updateHost lookup fail. ID = "' + id + '"');
674 }
Simon Hunt7cd48f32014-11-09 23:42:50 -0800675 }
676
Simon Hunt44031102014-11-11 13:20:36 -0800677 // TODO: fold removeX(...) methods into base method - remove dup code
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800678 function removeLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800679 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800680 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800681 result = findLink(link, 'remove'),
682 bad = result.badLogic;
683 if (bad) {
684 logicError(bad + ': ' + link.id);
685 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800686 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800687 result.removeRawLink();
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800688 }
689
Simon Hunt44031102014-11-11 13:20:36 -0800690 function removeHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800691 evTrace(data);
Simon Hunt44031102014-11-11 13:20:36 -0800692 var host = data.payload,
693 id = host.id,
694 hostData = network.lookup[id];
695 if (hostData) {
696 removeHostElement(hostData);
697 } else {
698 logicError('removeHost lookup fail. ID = "' + id + '"');
699 }
700 }
701
Simon Hunt61d04042014-11-11 17:27:16 -0800702 function showDetails(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800703 evTrace(data);
Simon Hunt61d04042014-11-11 17:27:16 -0800704 populateDetails(data.payload);
705 detailPane.show();
706 }
707
Simon Huntb53e0682014-11-12 13:32:01 -0800708 function showTraffic(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800709 evTrace(data);
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800710 var paths = data.payload.paths;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800711
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800712 // Revert any links hilighted previously.
Simon Hunta255a2c2014-11-13 22:29:35 -0800713 link.classed('primary secondary animated optical', false);
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800714
715 // Now hilight all links in the paths payload.
Simon Hunta255a2c2014-11-13 22:29:35 -0800716 paths.forEach(function (p) {
717 var cls = p.class;
718 p.links.forEach(function (id) {
Simon Hunt269670f2014-11-17 16:17:43 -0800719 var lnk = findLinkById(id);
Simon Hunta255a2c2014-11-13 22:29:35 -0800720 if (lnk) {
721 lnk.el.classed(cls, true);
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800722 }
723 });
724 });
Simon Huntb53e0682014-11-12 13:32:01 -0800725 }
726
Simon Hunt56d51852014-11-09 13:03:35 -0800727 // ...............................
728
729 function stillToImplement(data) {
730 var p = data.payload;
731 note(data.event, p.id);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800732 network.view.alert('Not yet implemented: "' + data.event + '"');
Simon Hunt56d51852014-11-09 13:03:35 -0800733 }
Simon Hunt99c13842014-11-06 18:23:12 -0800734
735 function unknownEvent(data) {
Simon Hunt50128c02014-11-08 13:36:15 -0800736 network.view.alert('Unknown event type: "' + data.event + '"');
Simon Hunt99c13842014-11-06 18:23:12 -0800737 }
738
739 function handleServerEvent(data) {
740 var fn = eventDispatch[data.event] || unknownEvent;
741 fn(data);
742 }
743
744 // ==============================
Simon Hunt61d04042014-11-11 17:27:16 -0800745 // Out-going messages...
746
Simon Huntb53e0682014-11-12 13:32:01 -0800747 function userFeedback(msg) {
748 // for now, use the alert pane as is. Maybe different alert style in
749 // the future (centered on view; dismiss button?)
750 network.view.alert(msg);
751 }
752
753 function nSel() {
754 return selectOrder.length;
755 }
Simon Hunt61d04042014-11-11 17:27:16 -0800756 function getSel(idx) {
757 return selections[selectOrder[idx]];
758 }
Simon Huntb53e0682014-11-12 13:32:01 -0800759 function getSelId(idx) {
760 return getSel(idx).obj.id;
761 }
762 function allSelectionsClass(cls) {
763 for (var i=0, n=nSel(); i<n; i++) {
764 if (getSel(i).obj.class !== cls) {
765 return false;
766 }
767 }
768 return true;
769 }
Simon Hunt61d04042014-11-11 17:27:16 -0800770
Simon Hunt61d04042014-11-11 17:27:16 -0800771 // request details for the selected element
Simon Huntd72bc702014-11-13 18:38:04 -0800772 // invoked from selection of a single node.
Simon Hunt61d04042014-11-11 17:27:16 -0800773 function requestDetails() {
774 var data = getSel(0).obj,
775 payload = {
776 id: data.id,
777 class: data.class
778 };
779 sendMessage('requestDetails', payload);
780 }
781
Simon Huntd72bc702014-11-13 18:38:04 -0800782 function addIntentAction() {
783 sendMessage('addHostIntent', {
784 one: getSelId(0),
Thomas Vachuska82f2c622014-11-17 12:23:18 -0800785 two: getSelId(1),
786 ids: [ getSelId(0), getSelId(1) ]
Simon Huntd72bc702014-11-13 18:38:04 -0800787 });
788 }
789
790 function showTrafficAction() {
791 // if nothing is hovered over, and nothing selected, send cancel request
792 if (!hovered && nSel() === 0) {
793 sendMessage('cancelTraffic', {});
794 return;
795 }
796
797 // NOTE: hover is only populated if "show traffic on hover" is
798 // toggled on, and the item hovered is a host...
799 var hoverId = (trafficHover() && hovered && hovered.class === 'host')
800 ? hovered.id : '';
801 sendMessage('requestTraffic', {
802 ids: selectOrder,
803 hover: hoverId
804 });
805 }
806
807
Simon Hunt61d04042014-11-11 17:27:16 -0800808 // ==============================
Simon Hunta5e89142014-11-14 07:00:33 -0800809 // onos instance panel functions
810
811 function updateInstances() {
812 var onoses = oiBox.el.selectAll('.onosInst')
813 .data(onosOrder, function (d) { return d.id; });
814
815 // operate on existing onoses if necessary
816
817 var entering = onoses.enter()
818 .append('div')
819 .attr('class', 'onosInst')
820 .classed('online', function (d) { return d.online; })
Simon Hunt9c15eca2014-11-15 18:37:59 -0800821 .on('click', clickInst);
822
823 entering.each(function (d, i) {
824 var el = d3.select(this),
825 img;
826
827 $('<img src="img/host.png">').appendTo(el);
828 img = el.select('img')
829 .attr({
830 width: 40,
831 top: -10,
832 left: -10
833 })
834 .style({
835 });
836
837 $('<div>').attr('class', 'onosTitle').text(d.id).appendTo(el);
838
839 // is the UI attached to this instance?
840 // TODO: need uiAttached boolean in instance data
841 //if (d.uiAttached) {
842 if (i === 0) {
843 $('<img src="img/ui.png">').attr('class','ui').appendTo(el);
844 }
845 });
Simon Hunta5e89142014-11-14 07:00:33 -0800846
847 // operate on existing + new onoses here
848
849 // the departed...
850 var exiting = onoses.exit()
851 .transition()
852 .style('opacity', 0)
853 .remove();
854 }
855
Simon Hunt9462e8c2014-11-14 17:28:09 -0800856 function clickInst(d) {
857 var el = d3.select(this),
858 aff = el.classed('affinity');
859 if (!aff) {
860 setAffinity(el, d);
861 } else {
862 cancelAffinity();
863 }
864 }
865
866 function setAffinity(el, d) {
867 d3.selectAll('.onosInst')
868 .classed('mastership', true)
869 .classed('affinity', false);
870 el.classed('affinity', true);
871
872 suppressLayers(true);
873 node.each(function (n) {
874 if (n.master === d.id) {
875 n.el.classed('suppressed', false);
876 }
877 });
878 oiShowMaster = true;
879 }
880
881 function cancelAffinity() {
882 d3.selectAll('.onosInst')
883 .classed('mastership affinity', false);
884 restoreLayerState();
885 oiShowMaster = false;
886 }
887
Simon Hunta5e89142014-11-14 07:00:33 -0800888 // ==============================
Simon Hunt99c13842014-11-06 18:23:12 -0800889 // force layout modification functions
890
891 function translate(x, y) {
892 return 'translate(' + x + ',' + y + ')';
893 }
894
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800895 function missMsg(what, id) {
896 return '\n[' + what + '] "' + id + '" missing ';
897 }
898
899 function linkEndPoints(srcId, dstId) {
900 var srcNode = network.lookup[srcId],
901 dstNode = network.lookup[dstId],
902 sMiss = !srcNode ? missMsg('src', srcId) : '',
903 dMiss = !dstNode ? missMsg('dst', dstId) : '';
904
905 if (sMiss || dMiss) {
906 logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
907 return null;
908 }
909 return {
910 source: srcNode,
911 target: dstNode,
912 x1: srcNode.x,
913 y1: srcNode.y,
914 x2: dstNode.x,
915 y2: dstNode.y
916 };
917 }
918
Simon Hunt56d51852014-11-09 13:03:35 -0800919 function createHostLink(host) {
920 var src = host.id,
921 dst = host.cp.device,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800922 id = host.ingress,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800923 lnk = linkEndPoints(src, dst);
Simon Hunt56d51852014-11-09 13:03:35 -0800924
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800925 if (!lnk) {
Simon Hunt56d51852014-11-09 13:03:35 -0800926 return null;
927 }
928
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800929 // Synthesize link ...
930 $.extend(lnk, {
Simon Hunt8257f4c2014-11-16 19:34:54 -0800931 key: id,
Simon Hunt56d51852014-11-09 13:03:35 -0800932 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -0800933
934 type: function () { return 'hostLink'; },
935 // TODO: ideally, we should see if our edge switch is online...
936 online: function () { return true; },
937 linkWidth: function () { return 1; }
Simon Hunt7cd48f32014-11-09 23:42:50 -0800938 });
Simon Hunt99c13842014-11-06 18:23:12 -0800939 return lnk;
940 }
941
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800942 function createLink(link) {
943 var lnk = linkEndPoints(link.src, link.dst),
944 type = link.type;
945
946 if (!lnk) {
947 return null;
948 }
949
Simon Hunt8257f4c2014-11-16 19:34:54 -0800950 $.extend(lnk, {
951 key: link.id,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800952 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -0800953 fromSource: link,
954
955 // functions to aggregate dual link state
956 type: function () {
957 var s = lnk.fromSource,
958 t = lnk.fromTarget;
959 return (s && s.type) || (t && t.type) || defaultLinkType;
960 },
961 online: function () {
962 var s = lnk.fromSource,
963 t = lnk.fromTarget;
964 return (s && s.online) || (t && t.online);
965 },
966 linkWidth: function () {
967 var s = lnk.fromSource,
968 t = lnk.fromTarget,
969 ws = (s && s.linkWidth) || 0,
970 wt = (t && t.linkWidth) || 0;
971 return Math.max(ws, wt);
972 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800973 });
974 return lnk;
Simon Hunt1a9eff92014-11-07 11:06:34 -0800975 }
976
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800977 var widthRatio = 1.4,
978 linkScale = d3.scale.linear()
979 .domain([1, 12])
980 .range([widthRatio, 12 * widthRatio])
981 .clamp(true);
982
Simon Hunt99c13842014-11-06 18:23:12 -0800983 function updateLinks() {
984 link = linkG.selectAll('.link')
Simon Hunt8257f4c2014-11-16 19:34:54 -0800985 .data(network.links, function (d) { return d.key; });
Simon Hunt99c13842014-11-06 18:23:12 -0800986
987 // operate on existing links, if necessary
988 // link .foo() .bar() ...
989
990 // operate on entering links:
991 var entering = link.enter()
992 .append('line')
993 .attr({
Simon Hunt99c13842014-11-06 18:23:12 -0800994 x1: function (d) { return d.x1; },
995 y1: function (d) { return d.y1; },
996 x2: function (d) { return d.x2; },
997 y2: function (d) { return d.y2; },
Simon Hunt1a9eff92014-11-07 11:06:34 -0800998 stroke: config.topo.linkInColor,
999 'stroke-width': config.topo.linkInWidth
Simon Hunt99c13842014-11-06 18:23:12 -08001000 });
1001
1002 // augment links
Simon Hunt7cd48f32014-11-09 23:42:50 -08001003 entering.each(function (d) {
1004 var link = d3.select(this);
1005 // provide ref to element selection from backing data....
1006 d.el = link;
Simon Hunt8257f4c2014-11-16 19:34:54 -08001007 restyleLinkElement(d);
Simon Hunt99c13842014-11-06 18:23:12 -08001008
Simon Hunt7cd48f32014-11-09 23:42:50 -08001009 // TODO: add src/dst port labels etc.
1010 });
Thomas Vachuska4830d392014-11-09 17:09:56 -08001011
1012 // operate on both existing and new links, if necessary
1013 //link .foo() .bar() ...
1014
1015 // operate on exiting links:
Thomas Vachuska4830d392014-11-09 17:09:56 -08001016 link.exit()
Simon Hunt13bf9c82014-11-18 07:26:44 -08001017 .attr('stroke-dasharray', '3, 3')
1018 .style('opacity', 0.5)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001019 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -08001020 .duration(1500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001021 .attr({
Simon Hunt13bf9c82014-11-18 07:26:44 -08001022 'stroke-dasharray': '3, 12',
1023 stroke: config.topo.linkOutColor,
1024 'stroke-width': config.topo.linkOutWidth
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001025 })
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001026 .style('opacity', 0.0)
Thomas Vachuska4830d392014-11-09 17:09:56 -08001027 .remove();
Simon Hunt99c13842014-11-06 18:23:12 -08001028 }
1029
1030 function createDeviceNode(device) {
1031 // start with the object as is
1032 var node = device,
Simon Huntbb282f52014-11-10 11:08:19 -08001033 type = device.type,
1034 svgCls = type ? 'node device ' + type : 'node device';
Simon Hunt99c13842014-11-06 18:23:12 -08001035
1036 // Augment as needed...
1037 node.class = 'device';
Simon Huntbb282f52014-11-10 11:08:19 -08001038 node.svgClass = device.online ? svgCls + ' online' : svgCls;
Simon Hunt99c13842014-11-06 18:23:12 -08001039 positionNode(node);
1040
1041 // cache label array length
1042 network.deviceLabelCount = device.labels.length;
Simon Hunt99c13842014-11-06 18:23:12 -08001043 return node;
1044 }
1045
Simon Hunt56d51852014-11-09 13:03:35 -08001046 function createHostNode(host) {
1047 // start with the object as is
1048 var node = host;
1049
1050 // Augment as needed...
1051 node.class = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001052 if (!node.type) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08001053 node.type = 'endstation';
1054 }
Simon Hunt7fa116d2014-11-17 14:16:55 -08001055 node.svgClass = 'node host ' + node.type;
Simon Hunt56d51852014-11-09 13:03:35 -08001056 positionNode(node);
1057
1058 // cache label array length
1059 network.hostLabelCount = host.labels.length;
Simon Hunt56d51852014-11-09 13:03:35 -08001060 return node;
1061 }
1062
Simon Hunt99c13842014-11-06 18:23:12 -08001063 function positionNode(node) {
1064 var meta = node.metaUi,
Simon Huntac9e24f2014-11-12 10:12:21 -08001065 x = meta && meta.x,
1066 y = meta && meta.y,
1067 xy;
Simon Hunt99c13842014-11-06 18:23:12 -08001068
Simon Huntac9e24f2014-11-12 10:12:21 -08001069 // If we have [x,y] already, use that...
Simon Hunt99c13842014-11-06 18:23:12 -08001070 if (x && y) {
1071 node.fixed = true;
Simon Huntac9e24f2014-11-12 10:12:21 -08001072 node.x = x;
1073 node.y = y;
1074 return;
Simon Hunt99c13842014-11-06 18:23:12 -08001075 }
Simon Huntac9e24f2014-11-12 10:12:21 -08001076
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001077 var location = node.location;
1078 if (location && location.type === 'latlng') {
1079 var coord = geoMapProjection([location.lng, location.lat]);
1080 node.fixed = true;
1081 node.x = coord[0];
1082 node.y = coord[1];
1083 return;
1084 }
1085
Simon Huntac9e24f2014-11-12 10:12:21 -08001086 // Note: Placing incoming unpinned nodes at exactly the same point
1087 // (center of the view) causes them to explode outwards when
1088 // the force layout kicks in. So, we spread them out a bit
1089 // initially, to provide a more serene layout convergence.
1090 // Additionally, if the node is a host, we place it near
1091 // the device it is connected to.
1092
1093 function spread(s) {
1094 return Math.floor((Math.random() * s) - s/2);
1095 }
1096
1097 function randDim(dim) {
1098 return dim / 2 + spread(dim * 0.7071);
1099 }
1100
1101 function rand() {
1102 return {
1103 x: randDim(network.view.width()),
1104 y: randDim(network.view.height())
1105 };
1106 }
1107
1108 function near(node) {
1109 var min = 12,
1110 dx = spread(12),
1111 dy = spread(12);
1112 return {
1113 x: node.x + min + dx,
1114 y: node.y + min + dy
1115 };
1116 }
1117
1118 function getDevice(cp) {
1119 var d = network.lookup[cp.device];
1120 return d || rand();
1121 }
1122
1123 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
1124 $.extend(node, xy);
Simon Hunt99c13842014-11-06 18:23:12 -08001125 }
1126
Simon Hunt99c13842014-11-06 18:23:12 -08001127 function iconUrl(d) {
1128 return 'img/' + d.type + '.png';
1129 }
1130
1131 // returns the newly computed bounding box of the rectangle
1132 function adjustRectToFitText(n) {
1133 var text = n.select('text'),
1134 box = text.node().getBBox(),
1135 lab = config.labels;
1136
1137 text.attr('text-anchor', 'middle')
1138 .attr('y', '-0.8em')
1139 .attr('x', lab.imgPad/2);
1140
1141 // translate the bbox so that it is centered on [x,y]
1142 box.x = -box.width / 2;
1143 box.y = -box.height / 2;
1144
1145 // add padding
1146 box.x -= (lab.padLR + lab.imgPad/2);
1147 box.width += lab.padLR * 2 + lab.imgPad;
1148 box.y -= lab.padTB;
1149 box.height += lab.padTB * 2;
1150
1151 return box;
1152 }
1153
Simon Hunt1a9eff92014-11-07 11:06:34 -08001154 function mkSvgClass(d) {
1155 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
1156 }
1157
Simon Hunt7cd48f32014-11-09 23:42:50 -08001158 function hostLabel(d) {
1159 var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0;
1160 return d.labels[idx];
1161 }
1162 function deviceLabel(d) {
1163 var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0;
1164 return d.labels[idx];
1165 }
1166 function niceLabel(label) {
1167 return (label && label.trim()) ? label : '.';
1168 }
1169
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001170 function updateDeviceLabel(d) {
1171 var label = niceLabel(deviceLabel(d)),
1172 node = d.el,
1173 box;
1174
1175 node.select('text')
1176 .text(label)
1177 .style('opacity', 0)
1178 .transition()
1179 .style('opacity', 1);
1180
1181 box = adjustRectToFitText(node);
1182
1183 node.select('rect')
1184 .transition()
1185 .attr(box);
1186
1187 node.select('image')
1188 .transition()
1189 .attr('x', box.x + config.icons.xoff)
1190 .attr('y', box.y + config.icons.yoff);
1191 }
1192
1193 function updateHostLabel(d) {
1194 var label = hostLabel(d),
1195 host = d.el;
1196
1197 host.select('text').text(label);
1198 }
1199
Simon Huntbb282f52014-11-10 11:08:19 -08001200 function updateDeviceState(nodeData) {
1201 nodeData.el.classed('online', nodeData.online);
1202 updateDeviceLabel(nodeData);
1203 // TODO: review what else might need to be updated
1204 }
1205
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001206 function updateLinkState(linkData) {
1207 updateLinkWidth(linkData);
Thomas Vachuskabadb93f2014-11-15 23:51:17 -08001208 linkData.el.classed('inactive', !linkData.online);
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001209 // TODO: review what else might need to be updated
1210 // update label, if showing
1211 }
1212
Simon Huntbb282f52014-11-10 11:08:19 -08001213 function updateHostState(hostData) {
1214 updateHostLabel(hostData);
1215 // TODO: review what else might need to be updated
1216 }
1217
Simon Hunt6ac93f32014-11-13 12:17:27 -08001218 function nodeMouseOver(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001219 hovered = d;
Simon Huntd72bc702014-11-13 18:38:04 -08001220 if (trafficHover() && d.class === 'host') {
1221 showTrafficAction();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001222 }
1223 }
1224
1225 function nodeMouseOut(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001226 hovered = null;
Simon Huntd72bc702014-11-13 18:38:04 -08001227 if (trafficHover() && d.class === 'host') {
1228 showTrafficAction();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001229 }
1230 }
Simon Huntbb282f52014-11-10 11:08:19 -08001231
Simon Hunt7fa116d2014-11-17 14:16:55 -08001232 function addHostIcon(node, radius, iconId) {
1233 var dim = radius * 1.5,
1234 xlate = -dim / 2;
1235
1236 node.append('use')
1237 .classed('glyph', true)
1238 .attr('transform', translate(xlate,xlate))
1239 .attr('xlink:href', '#' + iconId)
1240 .attr('width', dim)
1241 .attr('height', dim);
1242 }
1243
Simon Hunt99c13842014-11-06 18:23:12 -08001244 function updateNodes() {
1245 node = nodeG.selectAll('.node')
1246 .data(network.nodes, function (d) { return d.id; });
1247
1248 // operate on existing nodes, if necessary
Simon Hunt7cd48f32014-11-09 23:42:50 -08001249 // update host labels
Simon Hunt99c13842014-11-06 18:23:12 -08001250 //node .foo() .bar() ...
1251
1252 // operate on entering nodes:
1253 var entering = node.enter()
1254 .append('g')
1255 .attr({
1256 id: function (d) { return safeId(d.id); },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001257 class: mkSvgClass,
Simon Hunt99c13842014-11-06 18:23:12 -08001258 transform: function (d) { return translate(d.x, d.y); },
1259 opacity: 0
1260 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08001261 .call(network.drag)
Simon Hunt6ac93f32014-11-13 12:17:27 -08001262 .on('mouseover', nodeMouseOver)
1263 .on('mouseout', nodeMouseOut)
Simon Hunt99c13842014-11-06 18:23:12 -08001264 .transition()
1265 .attr('opacity', 1);
1266
1267 // augment device nodes...
1268 entering.filter('.device').each(function (d) {
1269 var node = d3.select(this),
1270 icon = iconUrl(d),
Simon Hunt7cd48f32014-11-09 23:42:50 -08001271 label = niceLabel(deviceLabel(d)),
Simon Hunt99c13842014-11-06 18:23:12 -08001272 box;
1273
Simon Hunt7cd48f32014-11-09 23:42:50 -08001274 // provide ref to element from backing data....
1275 d.el = node;
1276
Simon Hunt99c13842014-11-06 18:23:12 -08001277 node.append('rect')
1278 .attr({
1279 'rx': 5,
1280 'ry': 5
1281 });
1282
1283 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001284 .text(label)
Simon Hunt99c13842014-11-06 18:23:12 -08001285 .attr('dy', '1.1em');
1286
1287 box = adjustRectToFitText(node);
1288
1289 node.select('rect')
1290 .attr(box);
1291
1292 if (icon) {
1293 var cfg = config.icons;
1294 node.append('svg:image')
1295 .attr({
1296 x: box.x + config.icons.xoff,
1297 y: box.y + config.icons.yoff,
1298 width: cfg.w,
1299 height: cfg.h,
1300 'xlink:href': icon
1301 });
1302 }
1303
1304 // debug function to show the modelled x,y coordinates of nodes...
1305 if (debug('showNodeXY')) {
1306 node.select('rect').attr('fill-opacity', 0.5);
1307 node.append('circle')
1308 .attr({
1309 class: 'debug',
1310 cx: 0,
1311 cy: 0,
1312 r: '3px'
1313 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001314 }
1315 });
Simon Hunt934c3ce2014-11-05 11:45:07 -08001316
Simon Hunt7fa116d2014-11-17 14:16:55 -08001317 // TODO: better place for this configuration state
1318 var defaultHostRadius = 9,
1319 hostRadius = {
1320 bgpSpeaker: 20
1321 },
1322 hostIcon = {
1323 bgpSpeaker: 'bullhorn'
1324 };
1325
1326
Simon Hunt56d51852014-11-09 13:03:35 -08001327 // augment host nodes...
1328 entering.filter('.host').each(function (d) {
1329 var node = d3.select(this),
Simon Hunt7fa116d2014-11-17 14:16:55 -08001330 r = hostRadius[d.type] || defaultHostRadius,
1331 textDy = r + 10,
1332 icon = hostIcon[d.type];
Simon Hunt56d51852014-11-09 13:03:35 -08001333
Simon Hunt7cd48f32014-11-09 23:42:50 -08001334 // provide ref to element from backing data....
1335 d.el = node;
1336
Simon Hunt56d51852014-11-09 13:03:35 -08001337 node.append('circle')
Simon Hunt7fa116d2014-11-17 14:16:55 -08001338 .attr('r', r);
1339
1340 if (icon) {
1341 addHostIcon(node, r, icon);
1342 }
Simon Hunt56d51852014-11-09 13:03:35 -08001343
Simon Hunt56d51852014-11-09 13:03:35 -08001344 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001345 .text(hostLabel)
Simon Hunt7fa116d2014-11-17 14:16:55 -08001346 .attr('dy', textDy)
Simon Hunt7cd48f32014-11-09 23:42:50 -08001347 .attr('text-anchor', 'middle');
Simon Hunt56d51852014-11-09 13:03:35 -08001348
1349 // debug function to show the modelled x,y coordinates of nodes...
1350 if (debug('showNodeXY')) {
1351 node.select('circle').attr('fill-opacity', 0.5);
1352 node.append('circle')
1353 .attr({
1354 class: 'debug',
1355 cx: 0,
1356 cy: 0,
1357 r: '3px'
1358 });
1359 }
1360 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001361
Simon Hunt99c13842014-11-06 18:23:12 -08001362 // operate on both existing and new nodes, if necessary
1363 //node .foo() .bar() ...
Simon Huntc7ee0662014-11-05 16:44:37 -08001364
Simon Hunt99c13842014-11-06 18:23:12 -08001365 // operate on exiting nodes:
Simon Huntea80eb42014-11-11 13:46:57 -08001366 // Note that the node is removed after 2 seconds.
1367 // Sub element animations should be shorter than 2 seconds.
1368 var exiting = node.exit()
Simon Hunt44031102014-11-11 13:20:36 -08001369 .transition()
1370 .duration(2000)
Simon Huntea80eb42014-11-11 13:46:57 -08001371 .style('opacity', 0)
Simon Hunt99c13842014-11-06 18:23:12 -08001372 .remove();
Simon Huntea80eb42014-11-11 13:46:57 -08001373
1374 // host node exits....
1375 exiting.filter('.host').each(function (d) {
1376 var node = d3.select(this);
1377
1378 node.select('text')
1379 .style('opacity', 0.5)
1380 .transition()
1381 .duration(1000)
1382 .style('opacity', 0);
1383 // note, leave <g>.remove to remove this element
1384
1385 node.select('circle')
1386 .style('stroke-fill', '#555')
1387 .style('fill', '#888')
1388 .style('opacity', 0.5)
1389 .transition()
1390 .duration(1500)
1391 .attr('r', 0);
1392 // note, leave <g>.remove to remove this element
1393
1394 });
1395
1396 // TODO: device node exits
Simon Huntc7ee0662014-11-05 16:44:37 -08001397 }
1398
Simon Hunt8257f4c2014-11-16 19:34:54 -08001399 function find(key, array) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001400 for (var idx = 0, n = array.length; idx < n; idx++) {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001401 if (array[idx].key === key) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001402 return idx;
1403 }
1404 }
1405 return -1;
1406 }
1407
1408 function removeLinkElement(linkData) {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001409 var idx = find(linkData.key, network.links),
1410 removed;
1411 if (idx >=0) {
1412 // remove from links array
1413 removed = network.links.splice(idx, 1);
1414 // remove from lookup cache
1415 delete network.lookup[removed[0].key];
1416 updateLinks();
1417 network.force.resume();
1418 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001419 }
Simon Huntc7ee0662014-11-05 16:44:37 -08001420
Simon Hunt44031102014-11-11 13:20:36 -08001421 function removeHostElement(hostData) {
1422 // first, remove associated hostLink...
1423 removeLinkElement(hostData.linkData);
1424
1425 // remove from lookup cache
1426 delete network.lookup[hostData.id];
1427 // remove from nodes array
1428 var idx = find(hostData.id, network.nodes);
1429 network.nodes.splice(idx, 1);
1430 // remove from SVG
1431 updateNodes();
1432 network.force.resume();
1433 }
1434
1435
Simon Huntc7ee0662014-11-05 16:44:37 -08001436 function tick() {
1437 node.attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001438 transform: function (d) { return translate(d.x, d.y); }
Simon Huntc7ee0662014-11-05 16:44:37 -08001439 });
1440
1441 link.attr({
1442 x1: function (d) { return d.source.x; },
1443 y1: function (d) { return d.source.y; },
1444 x2: function (d) { return d.target.x; },
1445 y2: function (d) { return d.target.y; }
1446 });
1447 }
Simon Hunt934c3ce2014-11-05 11:45:07 -08001448
1449 // ==============================
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001450 // Web-Socket for live data
1451
1452 function webSockUrl() {
1453 return document.location.toString()
1454 .replace(/\#.*/, '')
1455 .replace('http://', 'ws://')
1456 .replace('https://', 'wss://')
1457 .replace('index2.html', config.webSockUrl);
1458 }
1459
1460 webSock = {
1461 ws : null,
1462
1463 connect : function() {
1464 webSock.ws = new WebSocket(webSockUrl());
1465
1466 webSock.ws.onopen = function() {
Simon Hunt0c6d4192014-11-12 12:07:10 -08001467 noWebSock(false);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001468 };
1469
1470 webSock.ws.onmessage = function(m) {
1471 if (m.data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001472 wsTraceRx(m.data);
Thomas Vachuskad472c6e2014-11-07 19:11:05 -08001473 handleServerEvent(JSON.parse(m.data));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001474 }
1475 };
1476
1477 webSock.ws.onclose = function(m) {
1478 webSock.ws = null;
Simon Hunt0c6d4192014-11-12 12:07:10 -08001479 noWebSock(true);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001480 };
1481 },
1482
1483 send : function(text) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001484 if (text != null) {
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001485 webSock._send(text);
1486 }
1487 },
1488
1489 _send : function(message) {
1490 if (webSock.ws) {
1491 webSock.ws.send(message);
Simon Hunta255a2c2014-11-13 22:29:35 -08001492 } else if (config.useLiveData) {
Simon Hunt56d51852014-11-09 13:03:35 -08001493 network.view.alert('no web socket open\n\n' + message);
Simon Hunta255a2c2014-11-13 22:29:35 -08001494 } else {
1495 console.log('WS Send: ' + JSON.stringify(message));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001496 }
1497 }
1498
1499 };
1500
Simon Hunt0c6d4192014-11-12 12:07:10 -08001501 function noWebSock(b) {
1502 mask.style('display',b ? 'block' : 'none');
1503 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001504
Simon Hunt61d04042014-11-11 17:27:16 -08001505 // TODO: use cache of pending messages (key = sid) to reconcile responses
1506
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001507 function sendMessage(evType, payload) {
1508 var toSend = {
Simon Huntbb282f52014-11-10 11:08:19 -08001509 event: evType,
1510 sid: ++sid,
1511 payload: payload
1512 },
1513 asText = JSON.stringify(toSend);
1514 wsTraceTx(asText);
1515 webSock.send(asText);
1516 }
1517
1518 function wsTraceTx(msg) {
1519 wsTrace('tx', msg);
1520 }
1521 function wsTraceRx(msg) {
1522 wsTrace('rx', msg);
1523 }
1524 function wsTrace(rxtx, msg) {
Simon Huntbb282f52014-11-10 11:08:19 -08001525 console.log('[' + rxtx + '] ' + msg);
1526 // TODO: integrate with trace view
1527 //if (trace) {
1528 // trace.output(rxtx, msg);
1529 //}
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001530 }
1531
1532
1533 // ==============================
1534 // Selection stuff
1535
1536 function selectObject(obj, el) {
1537 var n,
Simon Hunt01095ff2014-11-13 16:37:29 -08001538 srcEv = d3.event.sourceEvent,
1539 meta = srcEv.metaKey,
1540 shift = srcEv.shiftKey;
1541
Simon Huntdeab4322014-11-13 18:49:07 -08001542 if ((panZoom() && !meta) || (!panZoom() && meta)) {
Simon Hunt01095ff2014-11-13 16:37:29 -08001543 return;
1544 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001545
1546 if (el) {
1547 n = d3.select(el);
1548 } else {
1549 node.each(function(d) {
1550 if (d == obj) {
1551 n = d3.select(el = this);
1552 }
1553 });
1554 }
1555 if (!n) return;
1556
Simon Hunt01095ff2014-11-13 16:37:29 -08001557 if (shift && n.classed('selected')) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001558 deselectObject(obj.id);
Simon Hunt61d04042014-11-11 17:27:16 -08001559 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001560 return;
1561 }
1562
Simon Hunt01095ff2014-11-13 16:37:29 -08001563 if (!shift) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001564 deselectAll();
1565 }
1566
Simon Huntc31d5692014-11-12 13:27:18 -08001567 selections[obj.id] = { obj: obj, el: el };
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001568 selectOrder.push(obj.id);
1569
1570 n.classed('selected', true);
Simon Hunt61d04042014-11-11 17:27:16 -08001571 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001572 }
1573
1574 function deselectObject(id) {
Simon Huntc31d5692014-11-12 13:27:18 -08001575 var obj = selections[id],
1576 idx;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001577 if (obj) {
1578 d3.select(obj.el).classed('selected', false);
Simon Hunt61d04042014-11-11 17:27:16 -08001579 delete selections[id];
Simon Huntc31d5692014-11-12 13:27:18 -08001580 idx = $.inArray(id, selectOrder);
1581 if (idx >= 0) {
1582 selectOrder.splice(idx, 1);
1583 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001584 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001585 }
1586
1587 function deselectAll() {
1588 // deselect all nodes in the network...
1589 node.classed('selected', false);
1590 selections = {};
1591 selectOrder = [];
Simon Hunt61d04042014-11-11 17:27:16 -08001592 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001593 }
1594
Simon Hunt61d04042014-11-11 17:27:16 -08001595 // update the state of the detail pane, based on current selections
1596 function updateDetailPane() {
1597 var nSel = selectOrder.length;
1598 if (!nSel) {
1599 detailPane.hide();
Simon Huntd72bc702014-11-13 18:38:04 -08001600 showTrafficAction(); // sends cancelTraffic event
Simon Hunt61d04042014-11-11 17:27:16 -08001601 } else if (nSel === 1) {
1602 singleSelect();
1603 } else {
1604 multiSelect();
1605 }
1606 }
1607
1608 function singleSelect() {
1609 requestDetails();
Simon Huntb53e0682014-11-12 13:32:01 -08001610 // NOTE: detail pane will be shown from showDetails event callback
Simon Hunt61d04042014-11-11 17:27:16 -08001611 }
1612
1613 function multiSelect() {
Simon Huntb53e0682014-11-12 13:32:01 -08001614 populateMultiSelect();
Simon Huntb53e0682014-11-12 13:32:01 -08001615 }
1616
1617 function addSep(tbody) {
1618 var tr = tbody.append('tr');
1619 $('<hr>').appendTo(tr.append('td').attr('colspan', 2));
1620 }
1621
1622 function addProp(tbody, label, value) {
1623 var tr = tbody.append('tr');
1624
1625 tr.append('td')
1626 .attr('class', 'label')
1627 .text(label + ' :');
1628
1629 tr.append('td')
1630 .attr('class', 'value')
1631 .text(value);
1632 }
1633
1634 function populateMultiSelect() {
1635 detailPane.empty();
1636
1637 var title = detailPane.append("h2"),
1638 table = detailPane.append("table"),
1639 tbody = table.append("tbody");
1640
1641 title.text('Multi-Select...');
1642
1643 selectOrder.forEach(function (d, i) {
1644 addProp(tbody, i+1, d);
1645 });
Simon Huntd72bc702014-11-13 18:38:04 -08001646
1647 addMultiSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08001648 }
1649
1650 function populateDetails(data) {
1651 detailPane.empty();
1652
1653 var title = detailPane.append("h2"),
1654 table = detailPane.append("table"),
1655 tbody = table.append("tbody");
1656
1657 $('<img src="img/' + data.type + '.png">').appendTo(title);
1658 $('<span>').attr('class', 'icon').text(data.id).appendTo(title);
1659
1660 data.propOrder.forEach(function(p) {
1661 if (p === '-') {
1662 addSep(tbody);
1663 } else {
1664 addProp(tbody, p, data.props[p]);
1665 }
1666 });
Simon Huntd72bc702014-11-13 18:38:04 -08001667
1668 addSingleSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08001669 }
1670
Simon Huntd72bc702014-11-13 18:38:04 -08001671 function addSingleSelectActions() {
1672 detailPane.append('hr');
1673 // always want to allow 'show traffic'
Simon Hunta5e89142014-11-14 07:00:33 -08001674 addAction(detailPane, 'Show Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001675 }
1676
1677 function addMultiSelectActions() {
1678 detailPane.append('hr');
1679 // always want to allow 'show traffic'
Simon Hunta5e89142014-11-14 07:00:33 -08001680 addAction(detailPane, 'Show Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001681 // if exactly two hosts are selected, also want 'add host intent'
1682 if (nSel() === 2 && allSelectionsClass('host')) {
Simon Hunta5e89142014-11-14 07:00:33 -08001683 addAction(detailPane, 'Add Host Intent', addIntentAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001684 }
1685 }
1686
Simon Hunta5e89142014-11-14 07:00:33 -08001687 function addAction(panel, text, cb) {
1688 panel.append('div')
Simon Huntd72bc702014-11-13 18:38:04 -08001689 .classed('actionBtn', true)
1690 .text(text)
1691 .on('click', cb);
1692 }
1693
1694
Paul Greysonfcba0e82014-11-13 10:21:16 -08001695 function zoomPan(scale, translate) {
1696 zoomPanContainer.attr("transform", "translate(" + translate + ")scale(" + scale + ")");
1697 // keep the map lines constant width while zooming
1698 bgImg.style("stroke-width", 2.0 / scale + "px");
1699 }
1700
1701 function resetZoomPan() {
1702 zoomPan(1, [0,0]);
1703 zoom.scale(1).translate([0,0]);
1704 }
1705
1706 function setupZoomPan() {
1707 function zoomed() {
Simon Huntdeab4322014-11-13 18:49:07 -08001708 if (!panZoom() ^ !d3.event.sourceEvent.metaKey) {
Paul Greysonfcba0e82014-11-13 10:21:16 -08001709 zoomPan(d3.event.scale, d3.event.translate);
1710 }
1711 }
1712
1713 zoom = d3.behavior.zoom()
1714 .translate([0, 0])
1715 .scale(1)
1716 .scaleExtent([1, 8])
1717 .on("zoom", zoomed);
1718
1719 svg.call(zoom);
1720 }
1721
Simon Hunt61d04042014-11-11 17:27:16 -08001722 // ==============================
1723 // Test harness code
Simon Hunt56d51852014-11-09 13:03:35 -08001724
1725 function prepareScenario(view, ctx, dbg) {
1726 var sc = scenario,
1727 urlSc = sc.evDir + ctx + sc.evScenario;
1728
1729 if (!ctx) {
1730 view.alert("No scenario specified (null ctx)");
1731 return;
1732 }
1733
1734 sc.view = view;
1735 sc.ctx = ctx;
1736 sc.debug = dbg;
1737 sc.evNumber = 0;
1738
1739 d3.json(urlSc, function(err, data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001740 var p = data && data.params || {},
1741 desc = data && data.description || null,
Simon Huntfc274c92014-11-11 11:05:46 -08001742 intro = data && data.title;
Simon Huntbb282f52014-11-10 11:08:19 -08001743
Simon Hunt56d51852014-11-09 13:03:35 -08001744 if (err) {
1745 view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err);
1746 } else {
1747 sc.params = p;
Simon Huntbb282f52014-11-10 11:08:19 -08001748 if (desc) {
1749 intro += '\n\n ' + desc.join('\n ');
1750 }
1751 view.alert(intro);
Simon Hunt56d51852014-11-09 13:03:35 -08001752 }
1753 });
1754
1755 }
1756
Simon Hunt01095ff2014-11-13 16:37:29 -08001757 // ==============================
1758 // Toggle Buttons in masthead
Simon Hunt0c6d4192014-11-12 12:07:10 -08001759
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001760 // TODO: toggle button (and other widgets in the masthead) should be provided
1761 // by the framework; not generated by the view.
1762
Simon Hunta5e89142014-11-14 07:00:33 -08001763 var showInstances,
1764 doPanZoom,
1765 showTrafficOnHover;
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001766
1767 function addButtonBar(view) {
1768 var bb = d3.select('#mast')
1769 .append('span').classed('right', true).attr('id', 'bb');
1770
Simon Hunta5e89142014-11-14 07:00:33 -08001771 function mkTogBtn(text, cb) {
1772 return bb.append('span')
1773 .classed('btn', true)
1774 .text(text)
1775 .on('click', cb);
1776 }
Simon Hunt01095ff2014-11-13 16:37:29 -08001777
Simon Hunta5e89142014-11-14 07:00:33 -08001778 showInstances = mkTogBtn('Show Instances', toggleInst);
1779 doPanZoom = mkTogBtn('Pan/Zoom', togglePanZoom);
1780 showTrafficOnHover = mkTogBtn('Show traffic on hover', toggleTrafficHover);
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001781 }
1782
Simon Hunta5e89142014-11-14 07:00:33 -08001783 function instShown() {
1784 return showInstances.classed('active');
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001785 }
Simon Hunta5e89142014-11-14 07:00:33 -08001786 function toggleInst() {
1787 showInstances.classed('active', !instShown());
1788 if (instShown()) {
1789 oiBox.show();
1790 } else {
1791 oiBox.hide();
1792 }
Simon Hunt01095ff2014-11-13 16:37:29 -08001793 }
1794
Simon Huntdeab4322014-11-13 18:49:07 -08001795 function panZoom() {
1796 return doPanZoom.classed('active');
Simon Hunt01095ff2014-11-13 16:37:29 -08001797 }
Simon Hunta5e89142014-11-14 07:00:33 -08001798 function togglePanZoom() {
1799 doPanZoom.classed('active', !panZoom());
1800 }
1801
1802 function trafficHover() {
1803 return showTrafficOnHover.classed('active');
1804 }
1805 function toggleTrafficHover() {
1806 showTrafficOnHover.classed('active', !trafficHover());
1807 }
1808
Simon Hunt7fa116d2014-11-17 14:16:55 -08001809 function loadGlyphs(svg) {
1810 var defs = svg.append('defs');
1811 gly.defBird(defs);
1812 gly.defBullhorn(defs);
1813 }
Simon Hunt01095ff2014-11-13 16:37:29 -08001814
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001815 // ==============================
Simon Hunt142d0032014-11-04 20:13:09 -08001816 // View life-cycle callbacks
Simon Hunt195cb382014-11-03 17:50:51 -08001817
Simon Huntf67722a2014-11-10 09:32:06 -08001818 function preload(view, ctx, flags) {
Simon Hunt142d0032014-11-04 20:13:09 -08001819 var w = view.width(),
1820 h = view.height(),
Simon Huntc7ee0662014-11-05 16:44:37 -08001821 fcfg = config.force,
1822 fpad = fcfg.pad,
1823 forceDim = [w - 2*fpad, h - 2*fpad];
Simon Hunt195cb382014-11-03 17:50:51 -08001824
Simon Hunt142d0032014-11-04 20:13:09 -08001825 // NOTE: view.$div is a D3 selection of the view's div
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001826 var viewBox = '0 0 ' + config.logicalSize + ' ' + config.logicalSize;
1827 svg = view.$div.append('svg').attr('viewBox', viewBox);
Simon Hunt934c3ce2014-11-05 11:45:07 -08001828 setSize(svg, view);
1829
Simon Hunt7fa116d2014-11-17 14:16:55 -08001830 loadGlyphs(svg);
Simon Hunt12ce12e2014-11-15 21:13:19 -08001831
Paul Greysonfcba0e82014-11-13 10:21:16 -08001832 zoomPanContainer = svg.append('g').attr('id', 'zoomPanContainer');
Paul Greysonfcba0e82014-11-13 10:21:16 -08001833 setupZoomPan();
1834
Simon Hunt1a9eff92014-11-07 11:06:34 -08001835 // add blue glow filter to svg layer
Paul Greysonfcba0e82014-11-13 10:21:16 -08001836 d3u.appendGlow(zoomPanContainer);
Simon Hunt1a9eff92014-11-07 11:06:34 -08001837
Simon Huntc7ee0662014-11-05 16:44:37 -08001838 // group for the topology
Paul Greysonfcba0e82014-11-13 10:21:16 -08001839 topoG = zoomPanContainer.append('g')
Simon Huntd3b7d512014-11-12 15:48:41 -08001840 .attr('id', 'topo-G')
Simon Huntc7ee0662014-11-05 16:44:37 -08001841 .attr('transform', fcfg.translate());
1842
1843 // subgroups for links and nodes
1844 linkG = topoG.append('g').attr('id', 'links');
1845 nodeG = topoG.append('g').attr('id', 'nodes');
1846
1847 // selection of nodes and links
1848 link = linkG.selectAll('.link');
1849 node = nodeG.selectAll('.node');
1850
Simon Hunt7cd48f32014-11-09 23:42:50 -08001851 function chrg(d) {
1852 return fcfg.charge[d.class] || -12000;
1853 }
Simon Hunt99c13842014-11-06 18:23:12 -08001854 function ldist(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08001855 return fcfg.linkDistance[d.type] || 50;
Simon Hunt99c13842014-11-06 18:23:12 -08001856 }
1857 function lstrg(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08001858 // 0.0 - 1.0
1859 return fcfg.linkStrength[d.type] || 1.0;
Simon Hunt99c13842014-11-06 18:23:12 -08001860 }
1861
Simon Hunt1a9eff92014-11-07 11:06:34 -08001862 function selectCb(d, self) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001863 selectObject(d, self);
Simon Hunt1a9eff92014-11-07 11:06:34 -08001864 }
1865
1866 function atDragEnd(d, self) {
Simon Hunt56d51852014-11-09 13:03:35 -08001867 // once we've finished moving, pin the node in position
1868 d.fixed = true;
1869 d3.select(self).classed('fixed', true);
1870 if (config.useLiveData) {
Simon Hunt902c9922014-11-11 11:59:31 -08001871 sendUpdateMeta(d);
Simon Hunta255a2c2014-11-13 22:29:35 -08001872 } else {
1873 console.log('Moving node ' + d.id + ' to [' + d.x + ',' + d.y + ']');
Simon Hunt1a9eff92014-11-07 11:06:34 -08001874 }
1875 }
1876
Simon Hunt902c9922014-11-11 11:59:31 -08001877 function sendUpdateMeta(d) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001878 sendMessage('updateMeta', {
1879 id: d.id,
1880 'class': d.class,
Simon Hunt902c9922014-11-11 11:59:31 -08001881 'memento': {
Simon Hunt01095ff2014-11-13 16:37:29 -08001882 x: d.x,
1883 y: d.y
Simon Hunt902c9922014-11-11 11:59:31 -08001884 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001885 });
1886 }
1887
Simon Huntc7ee0662014-11-05 16:44:37 -08001888 // set up the force layout
1889 network.force = d3.layout.force()
1890 .size(forceDim)
1891 .nodes(network.nodes)
1892 .links(network.links)
Simon Hunt7cd48f32014-11-09 23:42:50 -08001893 .gravity(0.4)
1894 .friction(0.7)
1895 .charge(chrg)
Simon Hunt99c13842014-11-06 18:23:12 -08001896 .linkDistance(ldist)
1897 .linkStrength(lstrg)
Simon Huntc7ee0662014-11-05 16:44:37 -08001898 .on('tick', tick);
Simon Hunt195cb382014-11-03 17:50:51 -08001899
Simon Hunt01095ff2014-11-13 16:37:29 -08001900 network.drag = d3u.createDragBehavior(network.force,
Simon Huntdeab4322014-11-13 18:49:07 -08001901 selectCb, atDragEnd, panZoom);
Simon Hunt0c6d4192014-11-12 12:07:10 -08001902
1903 // create mask layer for when we lose connection to server.
Simon Hunta5e89142014-11-14 07:00:33 -08001904 // TODO: this should be part of the framework
Simon Hunt0c6d4192014-11-12 12:07:10 -08001905 mask = view.$div.append('div').attr('id','topo-mask');
1906 para(mask, 'Oops!');
1907 para(mask, 'Web-socket connection to server closed...');
1908 para(mask, 'Try refreshing the page.');
Simon Hunt12ce12e2014-11-15 21:13:19 -08001909
1910 mask.append('svg')
1911 .attr({
1912 id: 'mask-bird',
1913 width: w,
1914 height: h
1915 })
1916 .append('g')
1917 .attr('transform', birdTranslate(w, h))
1918 .style('opacity', 0.3)
1919 .append('use')
1920 .attr({
1921 'xlink:href': '#bird',
1922 width: config.birdDim,
1923 height: config.birdDim,
1924 fill: '#111'
1925 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08001926 }
Simon Hunt195cb382014-11-03 17:50:51 -08001927
Simon Hunt01095ff2014-11-13 16:37:29 -08001928 function para(sel, text) {
1929 sel.append('p').text(text);
1930 }
1931
1932
Simon Hunt56d51852014-11-09 13:03:35 -08001933 function load(view, ctx, flags) {
Simon Huntf67722a2014-11-10 09:32:06 -08001934 // resize, in case the window was resized while we were not loaded
1935 resize(view, ctx, flags);
1936
Simon Hunt99c13842014-11-06 18:23:12 -08001937 // cache the view token, so network topo functions can access it
1938 network.view = view;
Simon Hunt56d51852014-11-09 13:03:35 -08001939 config.useLiveData = !flags.local;
1940
1941 if (!config.useLiveData) {
1942 prepareScenario(view, ctx, flags.debug);
1943 }
Simon Hunt99c13842014-11-06 18:23:12 -08001944
1945 // set our radio buttons and key bindings
Simon Hunt9462e8c2014-11-14 17:28:09 -08001946 layerBtnSet = view.setRadio(layerButtons);
Simon Hunt934c3ce2014-11-05 11:45:07 -08001947 view.setKeys(keyDispatch);
Simon Hunt195cb382014-11-03 17:50:51 -08001948
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001949 // patch in our "button bar" for now
1950 // TODO: implement a more official frameworky way of doing this..
1951 addButtonBar(view);
1952
Simon Huntd3b7d512014-11-12 15:48:41 -08001953 // Load map data asynchronously; complete startup after that..
1954 loadGeoJsonData();
Simon Hunta255a2c2014-11-13 22:29:35 -08001955
1956 // start the and timer
1957 var dashIdx = 0;
1958 antTimer = setInterval(function () {
1959 // TODO: figure out how to choose Src-->Dst and Dst-->Src, per link
1960 dashIdx = dashIdx === 0 ? 14 : dashIdx - 2;
1961 d3.selectAll('.animated').style('stroke-dashoffset', dashIdx);
1962 }, 35);
1963 }
1964
1965 function unload(view, ctx, flags) {
1966 if (antTimer) {
1967 clearInterval(antTimer);
1968 antTimer = null;
1969 }
Simon Huntd3b7d512014-11-12 15:48:41 -08001970 }
1971
1972 // TODO: move these to config/state portion of script
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001973 var geoJsonUrl = 'json/map/continental_us.json', // TODO: Paul
Simon Huntd3b7d512014-11-12 15:48:41 -08001974 geoJson;
1975
1976 function loadGeoJsonData() {
1977 d3.json(geoJsonUrl, function (err, data) {
1978 if (err) {
1979 // fall back to USA map background
1980 loadStaticMap();
1981 } else {
1982 geoJson = data;
1983 loadGeoMap();
1984 }
1985
1986 // finally, connect to the server...
1987 if (config.useLiveData) {
1988 webSock.connect();
1989 }
1990 });
1991 }
1992
1993 function showBg() {
1994 return config.options.showBackground ? 'visible' : 'hidden';
1995 }
1996
1997 function loadStaticMap() {
1998 fnTrace('loadStaticMap', config.backgroundUrl);
1999 var w = network.view.width(),
2000 h = network.view.height();
2001
2002 // load the background image
2003 bgImg = svg.insert('svg:image', '#topo-G')
2004 .attr({
2005 id: 'topo-bg',
2006 width: w,
2007 height: h,
2008 'xlink:href': config.backgroundUrl
2009 })
2010 .style({
2011 visibility: showBg()
2012 });
2013 }
2014
2015 function loadGeoMap() {
2016 fnTrace('loadGeoMap', geoJsonUrl);
Simon Huntd3b7d512014-11-12 15:48:41 -08002017
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002018 // extracts the topojson data into geocoordinate-based geometry
2019 var topoData = topojson.feature(geoJson, geoJson.objects.states);
Simon Huntd3b7d512014-11-12 15:48:41 -08002020
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002021 // see: http://bl.ocks.org/mbostock/4707858
2022 geoMapProjection = d3.geo.mercator();
2023 var path = d3.geo.path().projection(geoMapProjection);
Simon Huntd3b7d512014-11-12 15:48:41 -08002024
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002025 geoMapProjection
2026 .scale(1)
2027 .translate([0, 0]);
Simon Huntd3b7d512014-11-12 15:48:41 -08002028
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002029 // [[x1,y1],[x2,y2]]
2030 var b = path.bounds(topoData);
Paul Greysonfcba0e82014-11-13 10:21:16 -08002031 // size map to 95% of minimum dimension to fill space
2032 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 -08002033 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 -08002034
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002035 geoMapProjection
2036 .scale(s)
2037 .translate(t);
2038
Paul Greysonfcba0e82014-11-13 10:21:16 -08002039 bgImg = zoomPanContainer.insert("g", '#topo-G');
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002040 bgImg.attr('id', 'map').selectAll('path')
2041 .data(topoData.features)
2042 .enter()
2043 .append('path')
2044 .attr('d', path);
Simon Hunt195cb382014-11-03 17:50:51 -08002045 }
2046
Simon Huntf67722a2014-11-10 09:32:06 -08002047 function resize(view, ctx, flags) {
Simon Hunt12ce12e2014-11-15 21:13:19 -08002048 var w = view.width(),
2049 h = view.height();
2050
Simon Hunt934c3ce2014-11-05 11:45:07 -08002051 setSize(svg, view);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002052
2053 d3.select('#mask-bird').attr({ width: w, height: h})
2054 .select('g').attr('transform', birdTranslate(w, h));
Simon Hunt142d0032014-11-04 20:13:09 -08002055 }
2056
Simon Hunt12ce12e2014-11-15 21:13:19 -08002057 function birdTranslate(w, h) {
2058 var bdim = config.birdDim;
2059 return 'translate('+((w-bdim)*.4)+','+((h-bdim)*.1)+')';
2060 }
Simon Hunt142d0032014-11-04 20:13:09 -08002061
2062 // ==============================
2063 // View registration
Simon Hunt195cb382014-11-03 17:50:51 -08002064
Simon Hunt25248912014-11-04 11:25:48 -08002065 onos.ui.addView('topo', {
Simon Hunt142d0032014-11-04 20:13:09 -08002066 preload: preload,
2067 load: load,
Simon Hunta255a2c2014-11-13 22:29:35 -08002068 unload: unload,
Simon Hunt142d0032014-11-04 20:13:09 -08002069 resize: resize
Simon Hunt195cb382014-11-03 17:50:51 -08002070 });
2071
Simon Hunt61d04042014-11-11 17:27:16 -08002072 detailPane = onos.ui.addFloatingPanel('topo-detail');
Simon Hunta5e89142014-11-14 07:00:33 -08002073 oiBox = onos.ui.addFloatingPanel('topo-oibox', 'TL');
Simon Hunt61d04042014-11-11 17:27:16 -08002074
Simon Hunt195cb382014-11-03 17:50:51 -08002075}(ONOS));