blob: 66366c70559d2319deb56f74e577463cafb71cca [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,
Simon Hunte2575b62014-11-18 15:25:53 -0800189 linkLabelG,
Simon Huntc7ee0662014-11-05 16:44:37 -0800190 node,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800191 link,
Simon Hunte2575b62014-11-18 15:25:53 -0800192 linkLabel,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800193 mask;
Simon Hunt195cb382014-11-03 17:50:51 -0800194
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800195 // the projection for the map background
196 var geoMapProjection;
197
Paul Greysonfcba0e82014-11-13 10:21:16 -0800198 // the zoom function
199 var zoom;
200
Simon Hunt142d0032014-11-04 20:13:09 -0800201 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800202 // For Debugging / Development
Simon Hunt195cb382014-11-03 17:50:51 -0800203
Simon Hunt99c13842014-11-06 18:23:12 -0800204 function note(label, msg) {
205 console.log('NOTE: ' + label + ': ' + msg);
Simon Hunt195cb382014-11-03 17:50:51 -0800206 }
207
Simon Hunt99c13842014-11-06 18:23:12 -0800208 function debug(what) {
209 return config.debugOn && config.debug[what];
Simon Hunt934c3ce2014-11-05 11:45:07 -0800210 }
211
Simon Huntfc274c92014-11-11 11:05:46 -0800212 function fnTrace(msg, id) {
213 if (config.fnTrace) {
214 console.log('FN: ' + msg + ' [' + id + ']');
215 }
216 }
Simon Hunt99c13842014-11-06 18:23:12 -0800217
Simon Hunta5e89142014-11-14 07:00:33 -0800218 function evTrace(data) {
219 fnTrace(data.event, data.payload.id);
220 }
221
Simon Hunt934c3ce2014-11-05 11:45:07 -0800222 // ==============================
223 // Key Callbacks
224
Simon Hunt99c13842014-11-06 18:23:12 -0800225 function testMe(view) {
Simon Hunt625dc402014-11-18 10:57:18 -0800226 view.alert('Theme is ' + view.theme());
Simon Hunt99c13842014-11-06 18:23:12 -0800227 }
228
Simon Hunt56d51852014-11-09 13:03:35 -0800229 function abortIfLive() {
Simon Hunt50128c02014-11-08 13:36:15 -0800230 if (config.useLiveData) {
Simon Huntb53e0682014-11-12 13:32:01 -0800231 network.view.alert("Sorry, currently using live data..");
Simon Hunt56d51852014-11-09 13:03:35 -0800232 return true;
Simon Hunt50128c02014-11-08 13:36:15 -0800233 }
Simon Hunt56d51852014-11-09 13:03:35 -0800234 return false;
235 }
Simon Hunt50128c02014-11-08 13:36:15 -0800236
Simon Hunt56d51852014-11-09 13:03:35 -0800237 function testDebug(msg) {
238 if (scenario.debug) {
239 scenario.view.alert(msg);
240 }
241 }
Simon Hunt99c13842014-11-06 18:23:12 -0800242
Simon Hunt56d51852014-11-09 13:03:35 -0800243 function injectTestEvent(view) {
244 if (abortIfLive()) { return; }
245 var sc = scenario,
246 evn = ++sc.evNumber,
247 pfx = sc.evDir + sc.ctx + sc.evPrefix + evn,
248 onosUrl = pfx + sc.evOnos,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800249 uiUrl = pfx + sc.evUi,
250 stack = [
251 { url: onosUrl, cb: handleServerEvent },
252 { url: uiUrl, cb: handleUiEvent }
253 ];
254 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800255 }
256
Simon Hunt7cd48f32014-11-09 23:42:50 -0800257 function recurseFetchEvent(stack, evn) {
258 var v = scenario.view,
259 frame;
260 if (stack.length === 0) {
Simon Huntfc274c92014-11-11 11:05:46 -0800261 v.alert('Oops!\n\nNo event #' + evn + ' found.');
Simon Hunt7cd48f32014-11-09 23:42:50 -0800262 return;
263 }
264 frame = stack.shift();
265
266 d3.json(frame.url, function (err, data) {
Simon Hunt99c13842014-11-06 18:23:12 -0800267 if (err) {
Simon Hunt56d51852014-11-09 13:03:35 -0800268 if (err.status === 404) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800269 // if we didn't find the data, try the next stack frame
270 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800271 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800272 v.alert('non-404 error:\n\n' + frame.url + '\n\n' + err);
Simon Hunt56d51852014-11-09 13:03:35 -0800273 }
Simon Hunt99c13842014-11-06 18:23:12 -0800274 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800275 testDebug('loaded: ' + frame.url);
Simon Hunt1712ed82014-11-17 12:56:00 -0800276 wsTrace('test', JSON.stringify(data));
Simon Hunt7cd48f32014-11-09 23:42:50 -0800277 frame.cb(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800278 }
279 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800280
Simon Hunt56d51852014-11-09 13:03:35 -0800281 }
Simon Hunt50128c02014-11-08 13:36:15 -0800282
Simon Hunt56d51852014-11-09 13:03:35 -0800283 function handleUiEvent(data) {
Simon Huntbb282f52014-11-10 11:08:19 -0800284 scenario.view.alert('UI Tx: ' + data.event + '\n\n' +
285 JSON.stringify(data));
Simon Hunt56d51852014-11-09 13:03:35 -0800286 }
287
288 function injectStartupEvents(view) {
289 var last = scenario.params.lastAuto || 0;
290 if (abortIfLive()) { return; }
291
292 while (scenario.evNumber < last) {
Simon Hunt1a9eff92014-11-07 11:06:34 -0800293 injectTestEvent(view);
294 }
295 }
296
Simon Hunt934c3ce2014-11-05 11:45:07 -0800297 function toggleBg() {
298 var vis = bgImg.style('visibility');
299 bgImg.style('visibility', (vis === 'hidden') ? 'visible' : 'hidden');
300 }
301
Simon Hunt99c13842014-11-06 18:23:12 -0800302 function cycleLabels() {
Simon Huntbb282f52014-11-10 11:08:19 -0800303 deviceLabelIndex = (deviceLabelIndex === network.deviceLabelCount - 1)
304 ? 0 : deviceLabelIndex + 1;
Simon Hunt5f36d342014-11-08 21:33:14 -0800305
Simon Hunt99c13842014-11-06 18:23:12 -0800306 network.nodes.forEach(function (d) {
Simon Huntbb282f52014-11-10 11:08:19 -0800307 if (d.class === 'device') {
308 updateDeviceLabel(d);
309 }
Simon Hunt99c13842014-11-06 18:23:12 -0800310 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800311 }
312
313 function togglePorts(view) {
Simon Hunt50128c02014-11-08 13:36:15 -0800314 view.alert('togglePorts() callback')
Simon Hunt934c3ce2014-11-05 11:45:07 -0800315 }
316
Simon Hunt6ac93f32014-11-13 12:17:27 -0800317 function unpin() {
318 if (hovered) {
319 hovered.fixed = false;
320 hovered.el.classed('fixed', false);
321 network.force.resume();
322 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800323 }
324
Simon Hunt9462e8c2014-11-14 17:28:09 -0800325 function handleEscape(view) {
326 if (oiShowMaster) {
327 cancelAffinity();
328 } else {
329 deselectAll();
330 }
331 }
332
Simon Hunt934c3ce2014-11-05 11:45:07 -0800333 // ==============================
334 // Radio Button Callbacks
335
Simon Hunta5e89142014-11-14 07:00:33 -0800336 var layerLookup = {
337 host: {
338 endstation: 'pkt', // default, if host event does not define type
339 bgpSpeaker: 'pkt'
340 },
341 device: {
342 switch: 'pkt',
343 roadm: 'opt'
344 },
345 link: {
346 hostLink: 'pkt',
347 direct: 'pkt',
Simon Hunt8257f4c2014-11-16 19:34:54 -0800348 indirect: '',
349 tunnel: '',
Simon Hunta5e89142014-11-14 07:00:33 -0800350 optical: 'opt'
351 }
352 };
353
354 function inLayer(d, layer) {
Simon Hunt8257f4c2014-11-16 19:34:54 -0800355 var type = d.class === 'link' ? d.type() : d.type,
356 look = layerLookup[d.class],
357 lyr = look && look[type];
Simon Hunta5e89142014-11-14 07:00:33 -0800358 return lyr === layer;
359 }
360
361 function unsuppressLayer(which) {
362 node.each(function (d) {
363 var node = d.el;
364 if (inLayer(d, which)) {
365 node.classed('suppressed', false);
366 }
367 });
368
369 link.each(function (d) {
370 var link = d.el;
371 if (inLayer(d, which)) {
372 link.classed('suppressed', false);
373 }
374 });
375 }
376
Simon Hunt9462e8c2014-11-14 17:28:09 -0800377 function suppressLayers(b) {
378 node.classed('suppressed', b);
379 link.classed('suppressed', b);
Simon Hunt142d0032014-11-04 20:13:09 -0800380// d3.selectAll('svg .port').classed('inactive', false);
381// d3.selectAll('svg .portText').classed('inactive', false);
Simon Hunt195cb382014-11-03 17:50:51 -0800382 }
383
Simon Hunt9462e8c2014-11-14 17:28:09 -0800384 function showAllLayers() {
385 suppressLayers(false);
386 }
387
Simon Hunt195cb382014-11-03 17:50:51 -0800388 function showPacketLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800389 node.classed('suppressed', true);
390 link.classed('suppressed', true);
391 unsuppressLayer('pkt');
Simon Hunt195cb382014-11-03 17:50:51 -0800392 }
393
394 function showOpticalLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800395 node.classed('suppressed', true);
396 link.classed('suppressed', true);
397 unsuppressLayer('opt');
Simon Hunt195cb382014-11-03 17:50:51 -0800398 }
399
Simon Hunt9462e8c2014-11-14 17:28:09 -0800400 function restoreLayerState() {
401 layerBtnDispatch[layerBtnSet.selected()]();
402 }
403
Simon Hunt142d0032014-11-04 20:13:09 -0800404 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800405 // Private functions
406
Simon Hunt99c13842014-11-06 18:23:12 -0800407 function safeId(s) {
408 return s.replace(/[^a-z0-9]/gi, '-');
409 }
410
Simon Huntc7ee0662014-11-05 16:44:37 -0800411 // set the size of the given element to that of the view (reduced if padded)
412 function setSize(el, view, pad) {
413 var padding = pad ? pad * 2 : 0;
Simon Hunt934c3ce2014-11-05 11:45:07 -0800414 el.attr({
Simon Huntc7ee0662014-11-05 16:44:37 -0800415 width: view.width() - padding,
416 height: view.height() - padding
Simon Hunt934c3ce2014-11-05 11:45:07 -0800417 });
418 }
419
Simon Hunt8257f4c2014-11-16 19:34:54 -0800420 function makeNodeKey(d, what) {
421 var port = what + 'Port';
422 return d[what] + '/' + d[port];
423 }
424
425 function makeLinkKey(d, flipped) {
426 var one = flipped ? makeNodeKey(d, 'dst') : makeNodeKey(d, 'src'),
427 two = flipped ? makeNodeKey(d, 'src') : makeNodeKey(d, 'dst');
428 return one + '-' + two;
429 }
430
Simon Hunt269670f2014-11-17 16:17:43 -0800431 function findLinkById(id) {
432 // check to see if this is a reverse lookup, else default to given id
433 var key = network.revLinkToKey[id] || id;
434 return key && network.lookup[key];
435 }
436
Simon Hunt8257f4c2014-11-16 19:34:54 -0800437 function findLink(linkData, op) {
438 var key = makeLinkKey(linkData),
439 keyrev = makeLinkKey(linkData, 1),
440 link = network.lookup[key],
441 linkRev = network.lookup[keyrev],
442 result = {},
443 ldata = link || linkRev,
444 rawLink;
445
446 if (op === 'add') {
447 if (link) {
448 // trying to add a link that we already know about
449 result.ldata = link;
450 result.badLogic = 'addLink: link already added';
451
452 } else if (linkRev) {
453 // we found the reverse of the link to be added
454 result.ldata = linkRev;
455 if (linkRev.fromTarget) {
456 result.badLogic = 'addLink: link already added';
457 }
458 }
459 } else if (op === 'update') {
460 if (!ldata) {
461 result.badLogic = 'updateLink: link not found';
462 } else {
463 rawLink = link ? ldata.fromSource : ldata.fromTarget;
464 result.updateWith = function (data) {
465 $.extend(rawLink, data);
466 restyleLinkElement(ldata);
467 }
468 }
469 } else if (op === 'remove') {
470 if (!ldata) {
471 result.badLogic = 'removeLink: link not found';
472 } else {
473 rawLink = link ? ldata.fromSource : ldata.fromTarget;
474
475 if (!rawLink) {
476 result.badLogic = 'removeLink: link not found';
477
478 } else {
479 result.removeRawLink = function () {
480 if (link) {
481 // remove fromSource
482 ldata.fromSource = null;
483 if (ldata.fromTarget) {
484 // promote target into source position
485 ldata.fromSource = ldata.fromTarget;
486 ldata.fromTarget = null;
487 ldata.key = keyrev;
488 delete network.lookup[key];
489 network.lookup[keyrev] = ldata;
Simon Hunt269670f2014-11-17 16:17:43 -0800490 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800491 }
492 } else {
493 // remove fromTarget
494 ldata.fromTarget = null;
Simon Hunt269670f2014-11-17 16:17:43 -0800495 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800496 }
497 if (ldata.fromSource) {
498 restyleLinkElement(ldata);
499 } else {
500 removeLinkElement(ldata);
501 }
502 }
503 }
504 }
505 }
506 return result;
507 }
508
509 function addLinkUpdate(ldata, link) {
510 // add link event, but we already have the reverse link installed
511 ldata.fromTarget = link;
Simon Hunt269670f2014-11-17 16:17:43 -0800512 network.revLinkToKey[link.id] = ldata.key;
Simon Hunt8257f4c2014-11-16 19:34:54 -0800513 restyleLinkElement(ldata);
514 }
515
516 var allLinkTypes = 'direct indirect optical tunnel',
517 defaultLinkType = 'direct';
518
519 function restyleLinkElement(ldata) {
520 // this fn's job is to look at raw links and decide what svg classes
521 // need to be applied to the line element in the DOM
522 var el = ldata.el,
523 type = ldata.type(),
524 lw = ldata.linkWidth(),
525 online = ldata.online();
526
527 el.classed('link', true);
528 el.classed('inactive', !online);
529 el.classed(allLinkTypes, false);
530 if (type) {
531 el.classed(type, true);
532 }
533 el.transition()
534 .duration(1000)
535 .attr('stroke-width', linkScale(lw))
Simon Hunt13bf9c82014-11-18 07:26:44 -0800536 .attr('stroke', config.topo.linkBaseColor);
Simon Hunt8257f4c2014-11-16 19:34:54 -0800537 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800538
Simon Hunt99c13842014-11-06 18:23:12 -0800539 // ==============================
540 // Event handlers for server-pushed events
541
Simon Huntbb282f52014-11-10 11:08:19 -0800542 function logicError(msg) {
543 // TODO, report logic error to server, via websock, so it can be logged
Simon Huntcb56cff2014-11-17 11:42:26 -0800544 //network.view.alert('Logic Error:\n\n' + msg);
Simon Huntfc274c92014-11-11 11:05:46 -0800545 console.warn(msg);
Simon Huntbb282f52014-11-10 11:08:19 -0800546 }
547
Simon Hunt99c13842014-11-06 18:23:12 -0800548 var eventDispatch = {
Simon Hunta5e89142014-11-14 07:00:33 -0800549 addInstance: addInstance,
Simon Hunt99c13842014-11-06 18:23:12 -0800550 addDevice: addDevice,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800551 addLink: addLink,
Simon Hunt56d51852014-11-09 13:03:35 -0800552 addHost: addHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800553
Simon Huntd72bc702014-11-13 18:38:04 -0800554 updateInstance: stillToImplement,
Simon Huntbb282f52014-11-10 11:08:19 -0800555 updateDevice: updateDevice,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800556 updateLink: updateLink,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800557 updateHost: updateHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800558
Simon Huntd72bc702014-11-13 18:38:04 -0800559 removeInstance: stillToImplement,
Simon Huntbb282f52014-11-10 11:08:19 -0800560 removeDevice: stillToImplement,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800561 removeLink: removeLink,
Simon Hunt44031102014-11-11 13:20:36 -0800562 removeHost: removeHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800563
Simon Hunt61d04042014-11-11 17:27:16 -0800564 showDetails: showDetails,
Simon Huntb53e0682014-11-12 13:32:01 -0800565 showTraffic: showTraffic
Simon Hunt99c13842014-11-06 18:23:12 -0800566 };
567
Simon Hunta5e89142014-11-14 07:00:33 -0800568 function addInstance(data) {
569 evTrace(data);
570 var inst = data.payload,
571 id = inst.id;
572 if (onosInstances[id]) {
573 logicError('ONOS instance already added: ' + id);
574 return;
575 }
576 onosInstances[id] = inst;
577 onosOrder.push(inst);
578 updateInstances();
579 }
580
Simon Hunt99c13842014-11-06 18:23:12 -0800581 function addDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800582 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800583 var device = data.payload,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800584 nodeData = createDeviceNode(device);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800585 network.nodes.push(nodeData);
586 network.lookup[nodeData.id] = nodeData;
Simon Hunt99c13842014-11-06 18:23:12 -0800587 updateNodes();
588 network.force.start();
589 }
590
Simon Hunt99c13842014-11-06 18:23:12 -0800591 function addLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800592 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800593 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800594 result = findLink(link, 'add'),
595 bad = result.badLogic,
596 ldata = result.ldata;
597
598 if (bad) {
599 logicError(bad + ': ' + link.id);
600 return;
601 }
602
603 if (ldata) {
604 // we already have a backing store link for src/dst nodes
605 addLinkUpdate(ldata, link);
606 return;
607 }
608
609 // no backing store link yet
610 ldata = createLink(link);
611 if (ldata) {
612 network.links.push(ldata);
613 network.lookup[ldata.key] = ldata;
Simon Hunt99c13842014-11-06 18:23:12 -0800614 updateLinks();
615 network.force.start();
616 }
617 }
618
Simon Hunt56d51852014-11-09 13:03:35 -0800619 function addHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800620 evTrace(data);
Simon Hunt56d51852014-11-09 13:03:35 -0800621 var host = data.payload,
622 node = createHostNode(host),
623 lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800624 network.nodes.push(node);
625 network.lookup[host.id] = node;
626 updateNodes();
627
628 lnk = createHostLink(host);
629 if (lnk) {
Simon Hunt44031102014-11-11 13:20:36 -0800630 node.linkData = lnk; // cache ref on its host
Simon Hunt56d51852014-11-09 13:03:35 -0800631 network.links.push(lnk);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800632 network.lookup[host.ingress] = lnk;
633 network.lookup[host.egress] = lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800634 updateLinks();
635 }
636 network.force.start();
637 }
638
Simon Hunt44031102014-11-11 13:20:36 -0800639 // TODO: fold updateX(...) methods into one base method; remove duplication
Simon Huntbb282f52014-11-10 11:08:19 -0800640 function updateDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800641 evTrace(data);
Simon Huntbb282f52014-11-10 11:08:19 -0800642 var device = data.payload,
643 id = device.id,
644 nodeData = network.lookup[id];
645 if (nodeData) {
646 $.extend(nodeData, device);
647 updateDeviceState(nodeData);
648 } else {
649 logicError('updateDevice lookup fail. ID = "' + id + '"');
650 }
651 }
652
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800653 function updateLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800654 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800655 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800656 result = findLink(link, 'update'),
657 bad = result.badLogic;
658 if (bad) {
659 logicError(bad + ': ' + link.id);
660 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800661 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800662 result.updateWith(link);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800663 }
664
Simon Hunt7cd48f32014-11-09 23:42:50 -0800665 function updateHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800666 evTrace(data);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800667 var host = data.payload,
Simon Huntbb282f52014-11-10 11:08:19 -0800668 id = host.id,
669 hostData = network.lookup[id];
670 if (hostData) {
671 $.extend(hostData, host);
672 updateHostState(hostData);
673 } else {
674 logicError('updateHost lookup fail. ID = "' + id + '"');
675 }
Simon Hunt7cd48f32014-11-09 23:42:50 -0800676 }
677
Simon Hunt44031102014-11-11 13:20:36 -0800678 // TODO: fold removeX(...) methods into base method - remove dup code
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800679 function removeLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800680 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800681 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800682 result = findLink(link, 'remove'),
683 bad = result.badLogic;
684 if (bad) {
685 logicError(bad + ': ' + link.id);
686 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800687 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800688 result.removeRawLink();
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800689 }
690
Simon Hunt44031102014-11-11 13:20:36 -0800691 function removeHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800692 evTrace(data);
Simon Hunt44031102014-11-11 13:20:36 -0800693 var host = data.payload,
694 id = host.id,
695 hostData = network.lookup[id];
696 if (hostData) {
697 removeHostElement(hostData);
698 } else {
699 logicError('removeHost lookup fail. ID = "' + id + '"');
700 }
701 }
702
Simon Hunt61d04042014-11-11 17:27:16 -0800703 function showDetails(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800704 evTrace(data);
Simon Hunt61d04042014-11-11 17:27:16 -0800705 populateDetails(data.payload);
706 detailPane.show();
707 }
708
Simon Huntb53e0682014-11-12 13:32:01 -0800709 function showTraffic(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800710 evTrace(data);
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800711 var paths = data.payload.paths;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800712
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800713 // Revert any links hilighted previously.
Simon Hunta255a2c2014-11-13 22:29:35 -0800714 link.classed('primary secondary animated optical', false);
Simon Hunte2575b62014-11-18 15:25:53 -0800715 // Remove all previous labels.
716 removeLinkLabels();
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800717
Simon Hunte2575b62014-11-18 15:25:53 -0800718 // Now hilight all links in the paths payload, and attach
719 // labels to them, if they are defined.
Simon Hunta255a2c2014-11-13 22:29:35 -0800720 paths.forEach(function (p) {
Simon Hunte2575b62014-11-18 15:25:53 -0800721 var n = p.links.length,
722 i,
723 ldata;
724
725 for (i=0; i<n; i++) {
726 ldata = findLinkById(p.links[i]);
727 if (ldata) {
728 ldata.el.classed(p.class, true);
729 ldata.label = p.labels[i];
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800730 }
Simon Hunte2575b62014-11-18 15:25:53 -0800731 }
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800732 });
Simon Hunte2575b62014-11-18 15:25:53 -0800733 updateLinks();
Simon Huntb53e0682014-11-12 13:32:01 -0800734 }
735
Simon Hunt56d51852014-11-09 13:03:35 -0800736 // ...............................
737
738 function stillToImplement(data) {
739 var p = data.payload;
740 note(data.event, p.id);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800741 network.view.alert('Not yet implemented: "' + data.event + '"');
Simon Hunt56d51852014-11-09 13:03:35 -0800742 }
Simon Hunt99c13842014-11-06 18:23:12 -0800743
744 function unknownEvent(data) {
Simon Hunt50128c02014-11-08 13:36:15 -0800745 network.view.alert('Unknown event type: "' + data.event + '"');
Simon Hunt99c13842014-11-06 18:23:12 -0800746 }
747
748 function handleServerEvent(data) {
749 var fn = eventDispatch[data.event] || unknownEvent;
750 fn(data);
751 }
752
753 // ==============================
Simon Hunt61d04042014-11-11 17:27:16 -0800754 // Out-going messages...
755
Simon Huntb53e0682014-11-12 13:32:01 -0800756 function userFeedback(msg) {
757 // for now, use the alert pane as is. Maybe different alert style in
758 // the future (centered on view; dismiss button?)
759 network.view.alert(msg);
760 }
761
762 function nSel() {
763 return selectOrder.length;
764 }
Simon Hunt61d04042014-11-11 17:27:16 -0800765 function getSel(idx) {
766 return selections[selectOrder[idx]];
767 }
Simon Huntb53e0682014-11-12 13:32:01 -0800768 function getSelId(idx) {
769 return getSel(idx).obj.id;
770 }
771 function allSelectionsClass(cls) {
772 for (var i=0, n=nSel(); i<n; i++) {
773 if (getSel(i).obj.class !== cls) {
774 return false;
775 }
776 }
777 return true;
778 }
Simon Hunt61d04042014-11-11 17:27:16 -0800779
Simon Hunt61d04042014-11-11 17:27:16 -0800780 // request details for the selected element
Simon Huntd72bc702014-11-13 18:38:04 -0800781 // invoked from selection of a single node.
Simon Hunt61d04042014-11-11 17:27:16 -0800782 function requestDetails() {
783 var data = getSel(0).obj,
784 payload = {
785 id: data.id,
786 class: data.class
787 };
788 sendMessage('requestDetails', payload);
789 }
790
Simon Huntd72bc702014-11-13 18:38:04 -0800791 function addIntentAction() {
792 sendMessage('addHostIntent', {
793 one: getSelId(0),
Thomas Vachuska82f2c622014-11-17 12:23:18 -0800794 two: getSelId(1),
795 ids: [ getSelId(0), getSelId(1) ]
Simon Huntd72bc702014-11-13 18:38:04 -0800796 });
797 }
798
799 function showTrafficAction() {
800 // if nothing is hovered over, and nothing selected, send cancel request
801 if (!hovered && nSel() === 0) {
802 sendMessage('cancelTraffic', {});
803 return;
804 }
805
806 // NOTE: hover is only populated if "show traffic on hover" is
807 // toggled on, and the item hovered is a host...
808 var hoverId = (trafficHover() && hovered && hovered.class === 'host')
809 ? hovered.id : '';
810 sendMessage('requestTraffic', {
811 ids: selectOrder,
812 hover: hoverId
813 });
814 }
815
816
Simon Hunt61d04042014-11-11 17:27:16 -0800817 // ==============================
Simon Hunta5e89142014-11-14 07:00:33 -0800818 // onos instance panel functions
819
820 function updateInstances() {
821 var onoses = oiBox.el.selectAll('.onosInst')
822 .data(onosOrder, function (d) { return d.id; });
823
824 // operate on existing onoses if necessary
825
826 var entering = onoses.enter()
827 .append('div')
828 .attr('class', 'onosInst')
829 .classed('online', function (d) { return d.online; })
Simon Hunt9c15eca2014-11-15 18:37:59 -0800830 .on('click', clickInst);
831
832 entering.each(function (d, i) {
833 var el = d3.select(this),
834 img;
835
836 $('<img src="img/host.png">').appendTo(el);
837 img = el.select('img')
838 .attr({
839 width: 40,
840 top: -10,
841 left: -10
842 })
843 .style({
844 });
845
846 $('<div>').attr('class', 'onosTitle').text(d.id).appendTo(el);
847
848 // is the UI attached to this instance?
849 // TODO: need uiAttached boolean in instance data
850 //if (d.uiAttached) {
851 if (i === 0) {
852 $('<img src="img/ui.png">').attr('class','ui').appendTo(el);
853 }
854 });
Simon Hunta5e89142014-11-14 07:00:33 -0800855
856 // operate on existing + new onoses here
857
858 // the departed...
859 var exiting = onoses.exit()
860 .transition()
861 .style('opacity', 0)
862 .remove();
863 }
864
Simon Hunt9462e8c2014-11-14 17:28:09 -0800865 function clickInst(d) {
866 var el = d3.select(this),
867 aff = el.classed('affinity');
868 if (!aff) {
869 setAffinity(el, d);
870 } else {
871 cancelAffinity();
872 }
873 }
874
875 function setAffinity(el, d) {
876 d3.selectAll('.onosInst')
877 .classed('mastership', true)
878 .classed('affinity', false);
879 el.classed('affinity', true);
880
881 suppressLayers(true);
882 node.each(function (n) {
883 if (n.master === d.id) {
884 n.el.classed('suppressed', false);
885 }
886 });
887 oiShowMaster = true;
888 }
889
890 function cancelAffinity() {
891 d3.selectAll('.onosInst')
892 .classed('mastership affinity', false);
893 restoreLayerState();
894 oiShowMaster = false;
895 }
896
Simon Hunta5e89142014-11-14 07:00:33 -0800897 // ==============================
Simon Hunt99c13842014-11-06 18:23:12 -0800898 // force layout modification functions
899
900 function translate(x, y) {
901 return 'translate(' + x + ',' + y + ')';
902 }
903
Simon Hunte2575b62014-11-18 15:25:53 -0800904 function rotate(deg) {
905 return 'rotate(' + deg + ')';
906 }
907
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800908 function missMsg(what, id) {
909 return '\n[' + what + '] "' + id + '" missing ';
910 }
911
912 function linkEndPoints(srcId, dstId) {
913 var srcNode = network.lookup[srcId],
914 dstNode = network.lookup[dstId],
915 sMiss = !srcNode ? missMsg('src', srcId) : '',
916 dMiss = !dstNode ? missMsg('dst', dstId) : '';
917
918 if (sMiss || dMiss) {
919 logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
920 return null;
921 }
922 return {
923 source: srcNode,
924 target: dstNode,
925 x1: srcNode.x,
926 y1: srcNode.y,
927 x2: dstNode.x,
928 y2: dstNode.y
929 };
930 }
931
Simon Hunt56d51852014-11-09 13:03:35 -0800932 function createHostLink(host) {
933 var src = host.id,
934 dst = host.cp.device,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800935 id = host.ingress,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800936 lnk = linkEndPoints(src, dst);
Simon Hunt56d51852014-11-09 13:03:35 -0800937
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800938 if (!lnk) {
Simon Hunt56d51852014-11-09 13:03:35 -0800939 return null;
940 }
941
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800942 // Synthesize link ...
943 $.extend(lnk, {
Simon Hunt8257f4c2014-11-16 19:34:54 -0800944 key: id,
Simon Hunt56d51852014-11-09 13:03:35 -0800945 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -0800946
947 type: function () { return 'hostLink'; },
948 // TODO: ideally, we should see if our edge switch is online...
949 online: function () { return true; },
950 linkWidth: function () { return 1; }
Simon Hunt7cd48f32014-11-09 23:42:50 -0800951 });
Simon Hunt99c13842014-11-06 18:23:12 -0800952 return lnk;
953 }
954
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800955 function createLink(link) {
956 var lnk = linkEndPoints(link.src, link.dst),
957 type = link.type;
958
959 if (!lnk) {
960 return null;
961 }
962
Simon Hunt8257f4c2014-11-16 19:34:54 -0800963 $.extend(lnk, {
964 key: link.id,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800965 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -0800966 fromSource: link,
967
968 // functions to aggregate dual link state
969 type: function () {
970 var s = lnk.fromSource,
971 t = lnk.fromTarget;
972 return (s && s.type) || (t && t.type) || defaultLinkType;
973 },
974 online: function () {
975 var s = lnk.fromSource,
976 t = lnk.fromTarget;
977 return (s && s.online) || (t && t.online);
978 },
979 linkWidth: function () {
980 var s = lnk.fromSource,
981 t = lnk.fromTarget,
982 ws = (s && s.linkWidth) || 0,
983 wt = (t && t.linkWidth) || 0;
984 return Math.max(ws, wt);
985 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800986 });
987 return lnk;
Simon Hunt1a9eff92014-11-07 11:06:34 -0800988 }
989
Simon Hunte2575b62014-11-18 15:25:53 -0800990 function removeLinkLabels() {
991 network.links.forEach(function (d) {
992 d.label = '';
993 });
994 }
995
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800996 var widthRatio = 1.4,
997 linkScale = d3.scale.linear()
998 .domain([1, 12])
999 .range([widthRatio, 12 * widthRatio])
1000 .clamp(true);
1001
Simon Hunt99c13842014-11-06 18:23:12 -08001002 function updateLinks() {
1003 link = linkG.selectAll('.link')
Simon Hunt8257f4c2014-11-16 19:34:54 -08001004 .data(network.links, function (d) { return d.key; });
Simon Hunt99c13842014-11-06 18:23:12 -08001005
1006 // operate on existing links, if necessary
1007 // link .foo() .bar() ...
1008
1009 // operate on entering links:
1010 var entering = link.enter()
1011 .append('line')
1012 .attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001013 x1: function (d) { return d.x1; },
1014 y1: function (d) { return d.y1; },
1015 x2: function (d) { return d.x2; },
1016 y2: function (d) { return d.y2; },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001017 stroke: config.topo.linkInColor,
1018 'stroke-width': config.topo.linkInWidth
Simon Hunt99c13842014-11-06 18:23:12 -08001019 });
1020
1021 // augment links
Simon Hunt7cd48f32014-11-09 23:42:50 -08001022 entering.each(function (d) {
1023 var link = d3.select(this);
1024 // provide ref to element selection from backing data....
1025 d.el = link;
Simon Hunt8257f4c2014-11-16 19:34:54 -08001026 restyleLinkElement(d);
Simon Hunt7cd48f32014-11-09 23:42:50 -08001027 });
Thomas Vachuska4830d392014-11-09 17:09:56 -08001028
1029 // operate on both existing and new links, if necessary
1030 //link .foo() .bar() ...
1031
Simon Hunte2575b62014-11-18 15:25:53 -08001032 // apply or remove labels
1033 var labelData = getLabelData();
1034 applyLinkLabels(labelData);
1035
Thomas Vachuska4830d392014-11-09 17:09:56 -08001036 // operate on exiting links:
Thomas Vachuska4830d392014-11-09 17:09:56 -08001037 link.exit()
Simon Hunt13bf9c82014-11-18 07:26:44 -08001038 .attr('stroke-dasharray', '3, 3')
1039 .style('opacity', 0.5)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001040 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -08001041 .duration(1500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001042 .attr({
Simon Hunt13bf9c82014-11-18 07:26:44 -08001043 'stroke-dasharray': '3, 12',
1044 stroke: config.topo.linkOutColor,
1045 'stroke-width': config.topo.linkOutWidth
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001046 })
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001047 .style('opacity', 0.0)
Thomas Vachuska4830d392014-11-09 17:09:56 -08001048 .remove();
Simon Hunte2575b62014-11-18 15:25:53 -08001049
1050 // NOTE: invoke a single tick to force the labels to position
1051 // onto their links.
1052 tick();
1053 }
1054
1055 function getLabelData() {
1056 // create the backing data for showing labels..
1057 var data = [];
1058 link.each(function (d) {
1059 if (d.label) {
1060 data.push({
1061 id: 'lab-' + d.key,
1062 key: d.key,
1063 label: d.label,
1064 ldata: d
1065 });
1066 }
1067 });
1068 return data;
1069 }
1070
1071 var linkLabelOffset = '0.3em';
1072
1073 function applyLinkLabels(data) {
1074 var entering;
1075
1076 linkLabel = linkLabelG.selectAll('.linkLabel')
1077 .data(data, function (d) { return d.id; });
1078
1079 entering = linkLabel.enter().append('g')
1080 .classed('linkLabel', true)
1081 .attr('id', function (d) { return d.id; });
1082
1083 entering.each(function (d) {
1084 var el = d3.select(this),
1085 rect,
1086 text,
1087 parms = {
1088 x1: d.ldata.x1,
1089 y1: d.ldata.y1,
1090 x2: d.ldata.x2,
1091 y2: d.ldata.y2
1092 };
1093
1094 d.el = el;
1095 rect = el.append('rect');
1096 text = el.append('text').text(d.label);
1097 rect.attr(rectAroundText(el));
1098 text.attr('dy', linkLabelOffset);
1099
1100 el.attr('transform', transformLabel(parms));
1101 });
1102
1103 // Remove any links that are no longer required.
1104 linkLabel.exit().remove();
1105 }
1106
1107 function rectAroundText(el) {
1108 var text = el.select('text'),
1109 box = text.node().getBBox();
1110
1111 // translate the bbox so that it is centered on [x,y]
1112 box.x = -box.width / 2;
1113 box.y = -box.height / 2;
1114
1115 // add padding
1116 box.x -= 1;
1117 box.width += 2;
1118 return box;
1119 }
1120
1121 function transformLabel(p) {
1122 var dx = p.x2 - p.x1,
1123 dy = p.y2 - p.y1,
1124 xMid = dx/2 + p.x1,
1125 yMid = dy/2 + p.y1;
1126 //length = Math.sqrt(dx*dx + dy*dy),
1127 //rads = Math.asin(dy/length),
1128 //degs = rads / (Math.PI*2) * 360;
1129
1130 return translate(xMid, yMid);
1131
1132 // TODO: consider making label parallel to line
1133 //return [
1134 // translate(xMid, yMid),
1135 // rotate(degs),
1136 // translate(0, 8)
1137 //].join('');
Simon Hunt99c13842014-11-06 18:23:12 -08001138 }
1139
1140 function createDeviceNode(device) {
1141 // start with the object as is
1142 var node = device,
Simon Huntbb282f52014-11-10 11:08:19 -08001143 type = device.type,
1144 svgCls = type ? 'node device ' + type : 'node device';
Simon Hunt99c13842014-11-06 18:23:12 -08001145
1146 // Augment as needed...
1147 node.class = 'device';
Simon Huntbb282f52014-11-10 11:08:19 -08001148 node.svgClass = device.online ? svgCls + ' online' : svgCls;
Simon Hunt99c13842014-11-06 18:23:12 -08001149 positionNode(node);
1150
1151 // cache label array length
1152 network.deviceLabelCount = device.labels.length;
Simon Hunt99c13842014-11-06 18:23:12 -08001153 return node;
1154 }
1155
Simon Hunt56d51852014-11-09 13:03:35 -08001156 function createHostNode(host) {
1157 // start with the object as is
1158 var node = host;
1159
1160 // Augment as needed...
1161 node.class = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001162 if (!node.type) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08001163 node.type = 'endstation';
1164 }
Simon Hunt7fa116d2014-11-17 14:16:55 -08001165 node.svgClass = 'node host ' + node.type;
Simon Hunt56d51852014-11-09 13:03:35 -08001166 positionNode(node);
1167
1168 // cache label array length
1169 network.hostLabelCount = host.labels.length;
Simon Hunt56d51852014-11-09 13:03:35 -08001170 return node;
1171 }
1172
Simon Hunt99c13842014-11-06 18:23:12 -08001173 function positionNode(node) {
1174 var meta = node.metaUi,
Simon Huntac9e24f2014-11-12 10:12:21 -08001175 x = meta && meta.x,
1176 y = meta && meta.y,
1177 xy;
Simon Hunt99c13842014-11-06 18:23:12 -08001178
Simon Huntac9e24f2014-11-12 10:12:21 -08001179 // If we have [x,y] already, use that...
Simon Hunt99c13842014-11-06 18:23:12 -08001180 if (x && y) {
1181 node.fixed = true;
Simon Huntac9e24f2014-11-12 10:12:21 -08001182 node.x = x;
1183 node.y = y;
1184 return;
Simon Hunt99c13842014-11-06 18:23:12 -08001185 }
Simon Huntac9e24f2014-11-12 10:12:21 -08001186
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001187 var location = node.location;
1188 if (location && location.type === 'latlng') {
1189 var coord = geoMapProjection([location.lng, location.lat]);
1190 node.fixed = true;
1191 node.x = coord[0];
1192 node.y = coord[1];
1193 return;
1194 }
1195
Simon Huntac9e24f2014-11-12 10:12:21 -08001196 // Note: Placing incoming unpinned nodes at exactly the same point
1197 // (center of the view) causes them to explode outwards when
1198 // the force layout kicks in. So, we spread them out a bit
1199 // initially, to provide a more serene layout convergence.
1200 // Additionally, if the node is a host, we place it near
1201 // the device it is connected to.
1202
1203 function spread(s) {
1204 return Math.floor((Math.random() * s) - s/2);
1205 }
1206
1207 function randDim(dim) {
1208 return dim / 2 + spread(dim * 0.7071);
1209 }
1210
1211 function rand() {
1212 return {
1213 x: randDim(network.view.width()),
1214 y: randDim(network.view.height())
1215 };
1216 }
1217
1218 function near(node) {
1219 var min = 12,
1220 dx = spread(12),
1221 dy = spread(12);
1222 return {
1223 x: node.x + min + dx,
1224 y: node.y + min + dy
1225 };
1226 }
1227
1228 function getDevice(cp) {
1229 var d = network.lookup[cp.device];
1230 return d || rand();
1231 }
1232
1233 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
1234 $.extend(node, xy);
Simon Hunt99c13842014-11-06 18:23:12 -08001235 }
1236
Simon Hunt99c13842014-11-06 18:23:12 -08001237 function iconUrl(d) {
1238 return 'img/' + d.type + '.png';
1239 }
1240
1241 // returns the newly computed bounding box of the rectangle
1242 function adjustRectToFitText(n) {
1243 var text = n.select('text'),
1244 box = text.node().getBBox(),
1245 lab = config.labels;
1246
1247 text.attr('text-anchor', 'middle')
1248 .attr('y', '-0.8em')
1249 .attr('x', lab.imgPad/2);
1250
1251 // translate the bbox so that it is centered on [x,y]
1252 box.x = -box.width / 2;
1253 box.y = -box.height / 2;
1254
1255 // add padding
1256 box.x -= (lab.padLR + lab.imgPad/2);
1257 box.width += lab.padLR * 2 + lab.imgPad;
1258 box.y -= lab.padTB;
1259 box.height += lab.padTB * 2;
1260
1261 return box;
1262 }
1263
Simon Hunt1a9eff92014-11-07 11:06:34 -08001264 function mkSvgClass(d) {
1265 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
1266 }
1267
Simon Hunt7cd48f32014-11-09 23:42:50 -08001268 function hostLabel(d) {
1269 var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0;
1270 return d.labels[idx];
1271 }
1272 function deviceLabel(d) {
1273 var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0;
1274 return d.labels[idx];
1275 }
1276 function niceLabel(label) {
1277 return (label && label.trim()) ? label : '.';
1278 }
1279
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001280 function updateDeviceLabel(d) {
1281 var label = niceLabel(deviceLabel(d)),
1282 node = d.el,
1283 box;
1284
1285 node.select('text')
1286 .text(label)
1287 .style('opacity', 0)
1288 .transition()
1289 .style('opacity', 1);
1290
1291 box = adjustRectToFitText(node);
1292
1293 node.select('rect')
1294 .transition()
1295 .attr(box);
1296
1297 node.select('image')
1298 .transition()
1299 .attr('x', box.x + config.icons.xoff)
1300 .attr('y', box.y + config.icons.yoff);
1301 }
1302
1303 function updateHostLabel(d) {
1304 var label = hostLabel(d),
1305 host = d.el;
1306
1307 host.select('text').text(label);
1308 }
1309
Simon Huntbb282f52014-11-10 11:08:19 -08001310 function updateDeviceState(nodeData) {
1311 nodeData.el.classed('online', nodeData.online);
1312 updateDeviceLabel(nodeData);
1313 // TODO: review what else might need to be updated
1314 }
1315
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001316 function updateLinkState(linkData) {
1317 updateLinkWidth(linkData);
Thomas Vachuskabadb93f2014-11-15 23:51:17 -08001318 linkData.el.classed('inactive', !linkData.online);
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001319 // TODO: review what else might need to be updated
1320 // update label, if showing
1321 }
1322
Simon Huntbb282f52014-11-10 11:08:19 -08001323 function updateHostState(hostData) {
1324 updateHostLabel(hostData);
1325 // TODO: review what else might need to be updated
1326 }
1327
Simon Hunt6ac93f32014-11-13 12:17:27 -08001328 function nodeMouseOver(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001329 hovered = d;
Simon Huntd72bc702014-11-13 18:38:04 -08001330 if (trafficHover() && d.class === 'host') {
1331 showTrafficAction();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001332 }
1333 }
1334
1335 function nodeMouseOut(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001336 hovered = null;
Simon Huntd72bc702014-11-13 18:38:04 -08001337 if (trafficHover() && d.class === 'host') {
1338 showTrafficAction();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001339 }
1340 }
Simon Huntbb282f52014-11-10 11:08:19 -08001341
Simon Hunt7fa116d2014-11-17 14:16:55 -08001342 function addHostIcon(node, radius, iconId) {
1343 var dim = radius * 1.5,
1344 xlate = -dim / 2;
1345
1346 node.append('use')
1347 .classed('glyph', true)
1348 .attr('transform', translate(xlate,xlate))
1349 .attr('xlink:href', '#' + iconId)
1350 .attr('width', dim)
1351 .attr('height', dim);
1352 }
1353
Simon Hunt99c13842014-11-06 18:23:12 -08001354 function updateNodes() {
1355 node = nodeG.selectAll('.node')
1356 .data(network.nodes, function (d) { return d.id; });
1357
1358 // operate on existing nodes, if necessary
Simon Hunt7cd48f32014-11-09 23:42:50 -08001359 // update host labels
Simon Hunt99c13842014-11-06 18:23:12 -08001360 //node .foo() .bar() ...
1361
1362 // operate on entering nodes:
1363 var entering = node.enter()
1364 .append('g')
1365 .attr({
1366 id: function (d) { return safeId(d.id); },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001367 class: mkSvgClass,
Simon Hunt99c13842014-11-06 18:23:12 -08001368 transform: function (d) { return translate(d.x, d.y); },
1369 opacity: 0
1370 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08001371 .call(network.drag)
Simon Hunt6ac93f32014-11-13 12:17:27 -08001372 .on('mouseover', nodeMouseOver)
1373 .on('mouseout', nodeMouseOut)
Simon Hunt99c13842014-11-06 18:23:12 -08001374 .transition()
1375 .attr('opacity', 1);
1376
1377 // augment device nodes...
1378 entering.filter('.device').each(function (d) {
1379 var node = d3.select(this),
1380 icon = iconUrl(d),
Simon Hunt7cd48f32014-11-09 23:42:50 -08001381 label = niceLabel(deviceLabel(d)),
Simon Hunt99c13842014-11-06 18:23:12 -08001382 box;
1383
Simon Hunt7cd48f32014-11-09 23:42:50 -08001384 // provide ref to element from backing data....
1385 d.el = node;
1386
Simon Hunt99c13842014-11-06 18:23:12 -08001387 node.append('rect')
1388 .attr({
1389 'rx': 5,
1390 'ry': 5
1391 });
1392
1393 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001394 .text(label)
Simon Hunt99c13842014-11-06 18:23:12 -08001395 .attr('dy', '1.1em');
1396
1397 box = adjustRectToFitText(node);
1398
1399 node.select('rect')
1400 .attr(box);
1401
1402 if (icon) {
1403 var cfg = config.icons;
1404 node.append('svg:image')
1405 .attr({
1406 x: box.x + config.icons.xoff,
1407 y: box.y + config.icons.yoff,
1408 width: cfg.w,
1409 height: cfg.h,
1410 'xlink:href': icon
1411 });
1412 }
1413
1414 // debug function to show the modelled x,y coordinates of nodes...
1415 if (debug('showNodeXY')) {
1416 node.select('rect').attr('fill-opacity', 0.5);
1417 node.append('circle')
1418 .attr({
1419 class: 'debug',
1420 cx: 0,
1421 cy: 0,
1422 r: '3px'
1423 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001424 }
1425 });
Simon Hunt934c3ce2014-11-05 11:45:07 -08001426
Simon Hunt7fa116d2014-11-17 14:16:55 -08001427 // TODO: better place for this configuration state
1428 var defaultHostRadius = 9,
1429 hostRadius = {
1430 bgpSpeaker: 20
1431 },
1432 hostIcon = {
1433 bgpSpeaker: 'bullhorn'
1434 };
1435
1436
Simon Hunt56d51852014-11-09 13:03:35 -08001437 // augment host nodes...
1438 entering.filter('.host').each(function (d) {
1439 var node = d3.select(this),
Simon Hunt7fa116d2014-11-17 14:16:55 -08001440 r = hostRadius[d.type] || defaultHostRadius,
1441 textDy = r + 10,
1442 icon = hostIcon[d.type];
Simon Hunt56d51852014-11-09 13:03:35 -08001443
Simon Hunt7cd48f32014-11-09 23:42:50 -08001444 // provide ref to element from backing data....
1445 d.el = node;
1446
Simon Hunt56d51852014-11-09 13:03:35 -08001447 node.append('circle')
Simon Hunt7fa116d2014-11-17 14:16:55 -08001448 .attr('r', r);
1449
1450 if (icon) {
1451 addHostIcon(node, r, icon);
1452 }
Simon Hunt56d51852014-11-09 13:03:35 -08001453
Simon Hunt56d51852014-11-09 13:03:35 -08001454 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001455 .text(hostLabel)
Simon Hunt7fa116d2014-11-17 14:16:55 -08001456 .attr('dy', textDy)
Simon Hunt7cd48f32014-11-09 23:42:50 -08001457 .attr('text-anchor', 'middle');
Simon Hunt56d51852014-11-09 13:03:35 -08001458
1459 // debug function to show the modelled x,y coordinates of nodes...
1460 if (debug('showNodeXY')) {
1461 node.select('circle').attr('fill-opacity', 0.5);
1462 node.append('circle')
1463 .attr({
1464 class: 'debug',
1465 cx: 0,
1466 cy: 0,
1467 r: '3px'
1468 });
1469 }
1470 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001471
Simon Hunt99c13842014-11-06 18:23:12 -08001472 // operate on both existing and new nodes, if necessary
1473 //node .foo() .bar() ...
Simon Huntc7ee0662014-11-05 16:44:37 -08001474
Simon Hunt99c13842014-11-06 18:23:12 -08001475 // operate on exiting nodes:
Simon Huntea80eb42014-11-11 13:46:57 -08001476 // Note that the node is removed after 2 seconds.
1477 // Sub element animations should be shorter than 2 seconds.
1478 var exiting = node.exit()
Simon Hunt44031102014-11-11 13:20:36 -08001479 .transition()
1480 .duration(2000)
Simon Huntea80eb42014-11-11 13:46:57 -08001481 .style('opacity', 0)
Simon Hunt99c13842014-11-06 18:23:12 -08001482 .remove();
Simon Huntea80eb42014-11-11 13:46:57 -08001483
1484 // host node exits....
1485 exiting.filter('.host').each(function (d) {
1486 var node = d3.select(this);
1487
1488 node.select('text')
1489 .style('opacity', 0.5)
1490 .transition()
1491 .duration(1000)
1492 .style('opacity', 0);
1493 // note, leave <g>.remove to remove this element
1494
1495 node.select('circle')
1496 .style('stroke-fill', '#555')
1497 .style('fill', '#888')
1498 .style('opacity', 0.5)
1499 .transition()
1500 .duration(1500)
1501 .attr('r', 0);
1502 // note, leave <g>.remove to remove this element
1503
1504 });
1505
1506 // TODO: device node exits
Simon Huntc7ee0662014-11-05 16:44:37 -08001507 }
1508
Simon Hunt8257f4c2014-11-16 19:34:54 -08001509 function find(key, array) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001510 for (var idx = 0, n = array.length; idx < n; idx++) {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001511 if (array[idx].key === key) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001512 return idx;
1513 }
1514 }
1515 return -1;
1516 }
1517
1518 function removeLinkElement(linkData) {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001519 var idx = find(linkData.key, network.links),
1520 removed;
1521 if (idx >=0) {
1522 // remove from links array
1523 removed = network.links.splice(idx, 1);
1524 // remove from lookup cache
1525 delete network.lookup[removed[0].key];
1526 updateLinks();
1527 network.force.resume();
1528 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001529 }
Simon Huntc7ee0662014-11-05 16:44:37 -08001530
Simon Hunt44031102014-11-11 13:20:36 -08001531 function removeHostElement(hostData) {
1532 // first, remove associated hostLink...
1533 removeLinkElement(hostData.linkData);
1534
1535 // remove from lookup cache
1536 delete network.lookup[hostData.id];
1537 // remove from nodes array
1538 var idx = find(hostData.id, network.nodes);
1539 network.nodes.splice(idx, 1);
1540 // remove from SVG
1541 updateNodes();
1542 network.force.resume();
1543 }
1544
1545
Simon Huntc7ee0662014-11-05 16:44:37 -08001546 function tick() {
1547 node.attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001548 transform: function (d) { return translate(d.x, d.y); }
Simon Huntc7ee0662014-11-05 16:44:37 -08001549 });
1550
1551 link.attr({
1552 x1: function (d) { return d.source.x; },
1553 y1: function (d) { return d.source.y; },
1554 x2: function (d) { return d.target.x; },
1555 y2: function (d) { return d.target.y; }
1556 });
Simon Hunte2575b62014-11-18 15:25:53 -08001557
1558 linkLabel.each(function (d) {
1559 var el = d3.select(this);
1560 var lnk = findLinkById(d.key),
1561 parms = {
1562 x1: lnk.source.x,
1563 y1: lnk.source.y,
1564 x2: lnk.target.x,
1565 y2: lnk.target.y
1566 };
1567 el.attr('transform', transformLabel(parms));
1568 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001569 }
Simon Hunt934c3ce2014-11-05 11:45:07 -08001570
1571 // ==============================
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001572 // Web-Socket for live data
1573
1574 function webSockUrl() {
1575 return document.location.toString()
1576 .replace(/\#.*/, '')
1577 .replace('http://', 'ws://')
1578 .replace('https://', 'wss://')
1579 .replace('index2.html', config.webSockUrl);
1580 }
1581
1582 webSock = {
1583 ws : null,
1584
1585 connect : function() {
1586 webSock.ws = new WebSocket(webSockUrl());
1587
1588 webSock.ws.onopen = function() {
Simon Hunt0c6d4192014-11-12 12:07:10 -08001589 noWebSock(false);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001590 };
1591
1592 webSock.ws.onmessage = function(m) {
1593 if (m.data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001594 wsTraceRx(m.data);
Thomas Vachuskad472c6e2014-11-07 19:11:05 -08001595 handleServerEvent(JSON.parse(m.data));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001596 }
1597 };
1598
1599 webSock.ws.onclose = function(m) {
1600 webSock.ws = null;
Simon Hunt0c6d4192014-11-12 12:07:10 -08001601 noWebSock(true);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001602 };
1603 },
1604
1605 send : function(text) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001606 if (text != null) {
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001607 webSock._send(text);
1608 }
1609 },
1610
1611 _send : function(message) {
1612 if (webSock.ws) {
1613 webSock.ws.send(message);
Simon Hunta255a2c2014-11-13 22:29:35 -08001614 } else if (config.useLiveData) {
Simon Hunt56d51852014-11-09 13:03:35 -08001615 network.view.alert('no web socket open\n\n' + message);
Simon Hunta255a2c2014-11-13 22:29:35 -08001616 } else {
1617 console.log('WS Send: ' + JSON.stringify(message));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001618 }
1619 }
1620
1621 };
1622
Simon Hunt0c6d4192014-11-12 12:07:10 -08001623 function noWebSock(b) {
1624 mask.style('display',b ? 'block' : 'none');
1625 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001626
Simon Hunt61d04042014-11-11 17:27:16 -08001627 // TODO: use cache of pending messages (key = sid) to reconcile responses
1628
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001629 function sendMessage(evType, payload) {
1630 var toSend = {
Simon Huntbb282f52014-11-10 11:08:19 -08001631 event: evType,
1632 sid: ++sid,
1633 payload: payload
1634 },
1635 asText = JSON.stringify(toSend);
1636 wsTraceTx(asText);
1637 webSock.send(asText);
1638 }
1639
1640 function wsTraceTx(msg) {
1641 wsTrace('tx', msg);
1642 }
1643 function wsTraceRx(msg) {
1644 wsTrace('rx', msg);
1645 }
1646 function wsTrace(rxtx, msg) {
Simon Huntbb282f52014-11-10 11:08:19 -08001647 console.log('[' + rxtx + '] ' + msg);
1648 // TODO: integrate with trace view
1649 //if (trace) {
1650 // trace.output(rxtx, msg);
1651 //}
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001652 }
1653
1654
1655 // ==============================
1656 // Selection stuff
1657
1658 function selectObject(obj, el) {
1659 var n,
Simon Hunt01095ff2014-11-13 16:37:29 -08001660 srcEv = d3.event.sourceEvent,
1661 meta = srcEv.metaKey,
1662 shift = srcEv.shiftKey;
1663
Simon Huntdeab4322014-11-13 18:49:07 -08001664 if ((panZoom() && !meta) || (!panZoom() && meta)) {
Simon Hunt01095ff2014-11-13 16:37:29 -08001665 return;
1666 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001667
1668 if (el) {
1669 n = d3.select(el);
1670 } else {
1671 node.each(function(d) {
1672 if (d == obj) {
1673 n = d3.select(el = this);
1674 }
1675 });
1676 }
1677 if (!n) return;
1678
Simon Hunt01095ff2014-11-13 16:37:29 -08001679 if (shift && n.classed('selected')) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001680 deselectObject(obj.id);
Simon Hunt61d04042014-11-11 17:27:16 -08001681 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001682 return;
1683 }
1684
Simon Hunt01095ff2014-11-13 16:37:29 -08001685 if (!shift) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001686 deselectAll();
1687 }
1688
Simon Huntc31d5692014-11-12 13:27:18 -08001689 selections[obj.id] = { obj: obj, el: el };
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001690 selectOrder.push(obj.id);
1691
1692 n.classed('selected', true);
Simon Hunt61d04042014-11-11 17:27:16 -08001693 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001694 }
1695
1696 function deselectObject(id) {
Simon Huntc31d5692014-11-12 13:27:18 -08001697 var obj = selections[id],
1698 idx;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001699 if (obj) {
1700 d3.select(obj.el).classed('selected', false);
Simon Hunt61d04042014-11-11 17:27:16 -08001701 delete selections[id];
Simon Huntc31d5692014-11-12 13:27:18 -08001702 idx = $.inArray(id, selectOrder);
1703 if (idx >= 0) {
1704 selectOrder.splice(idx, 1);
1705 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001706 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001707 }
1708
1709 function deselectAll() {
1710 // deselect all nodes in the network...
1711 node.classed('selected', false);
1712 selections = {};
1713 selectOrder = [];
Simon Hunt61d04042014-11-11 17:27:16 -08001714 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001715 }
1716
Simon Hunt61d04042014-11-11 17:27:16 -08001717 // update the state of the detail pane, based on current selections
1718 function updateDetailPane() {
1719 var nSel = selectOrder.length;
1720 if (!nSel) {
1721 detailPane.hide();
Simon Huntd72bc702014-11-13 18:38:04 -08001722 showTrafficAction(); // sends cancelTraffic event
Simon Hunt61d04042014-11-11 17:27:16 -08001723 } else if (nSel === 1) {
1724 singleSelect();
1725 } else {
1726 multiSelect();
1727 }
1728 }
1729
1730 function singleSelect() {
1731 requestDetails();
Simon Huntb53e0682014-11-12 13:32:01 -08001732 // NOTE: detail pane will be shown from showDetails event callback
Simon Hunt61d04042014-11-11 17:27:16 -08001733 }
1734
1735 function multiSelect() {
Simon Huntb53e0682014-11-12 13:32:01 -08001736 populateMultiSelect();
Simon Huntb53e0682014-11-12 13:32:01 -08001737 }
1738
1739 function addSep(tbody) {
1740 var tr = tbody.append('tr');
1741 $('<hr>').appendTo(tr.append('td').attr('colspan', 2));
1742 }
1743
1744 function addProp(tbody, label, value) {
1745 var tr = tbody.append('tr');
1746
1747 tr.append('td')
1748 .attr('class', 'label')
1749 .text(label + ' :');
1750
1751 tr.append('td')
1752 .attr('class', 'value')
1753 .text(value);
1754 }
1755
1756 function populateMultiSelect() {
1757 detailPane.empty();
1758
1759 var title = detailPane.append("h2"),
1760 table = detailPane.append("table"),
1761 tbody = table.append("tbody");
1762
1763 title.text('Multi-Select...');
1764
1765 selectOrder.forEach(function (d, i) {
1766 addProp(tbody, i+1, d);
1767 });
Simon Huntd72bc702014-11-13 18:38:04 -08001768
1769 addMultiSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08001770 }
1771
1772 function populateDetails(data) {
1773 detailPane.empty();
1774
1775 var title = detailPane.append("h2"),
1776 table = detailPane.append("table"),
1777 tbody = table.append("tbody");
1778
1779 $('<img src="img/' + data.type + '.png">').appendTo(title);
1780 $('<span>').attr('class', 'icon').text(data.id).appendTo(title);
1781
1782 data.propOrder.forEach(function(p) {
1783 if (p === '-') {
1784 addSep(tbody);
1785 } else {
1786 addProp(tbody, p, data.props[p]);
1787 }
1788 });
Simon Huntd72bc702014-11-13 18:38:04 -08001789
1790 addSingleSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08001791 }
1792
Simon Huntd72bc702014-11-13 18:38:04 -08001793 function addSingleSelectActions() {
1794 detailPane.append('hr');
1795 // always want to allow 'show traffic'
Simon Hunta5e89142014-11-14 07:00:33 -08001796 addAction(detailPane, 'Show Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001797 }
1798
1799 function addMultiSelectActions() {
1800 detailPane.append('hr');
1801 // always want to allow 'show traffic'
Simon Hunta5e89142014-11-14 07:00:33 -08001802 addAction(detailPane, 'Show Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001803 // if exactly two hosts are selected, also want 'add host intent'
1804 if (nSel() === 2 && allSelectionsClass('host')) {
Simon Hunta5e89142014-11-14 07:00:33 -08001805 addAction(detailPane, 'Add Host Intent', addIntentAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001806 }
1807 }
1808
Simon Hunta5e89142014-11-14 07:00:33 -08001809 function addAction(panel, text, cb) {
1810 panel.append('div')
Simon Huntd72bc702014-11-13 18:38:04 -08001811 .classed('actionBtn', true)
1812 .text(text)
1813 .on('click', cb);
1814 }
1815
1816
Paul Greysonfcba0e82014-11-13 10:21:16 -08001817 function zoomPan(scale, translate) {
1818 zoomPanContainer.attr("transform", "translate(" + translate + ")scale(" + scale + ")");
1819 // keep the map lines constant width while zooming
1820 bgImg.style("stroke-width", 2.0 / scale + "px");
1821 }
1822
1823 function resetZoomPan() {
1824 zoomPan(1, [0,0]);
1825 zoom.scale(1).translate([0,0]);
1826 }
1827
1828 function setupZoomPan() {
1829 function zoomed() {
Simon Huntdeab4322014-11-13 18:49:07 -08001830 if (!panZoom() ^ !d3.event.sourceEvent.metaKey) {
Paul Greysonfcba0e82014-11-13 10:21:16 -08001831 zoomPan(d3.event.scale, d3.event.translate);
1832 }
1833 }
1834
1835 zoom = d3.behavior.zoom()
1836 .translate([0, 0])
1837 .scale(1)
1838 .scaleExtent([1, 8])
1839 .on("zoom", zoomed);
1840
1841 svg.call(zoom);
1842 }
1843
Simon Hunt61d04042014-11-11 17:27:16 -08001844 // ==============================
1845 // Test harness code
Simon Hunt56d51852014-11-09 13:03:35 -08001846
1847 function prepareScenario(view, ctx, dbg) {
1848 var sc = scenario,
1849 urlSc = sc.evDir + ctx + sc.evScenario;
1850
1851 if (!ctx) {
1852 view.alert("No scenario specified (null ctx)");
1853 return;
1854 }
1855
1856 sc.view = view;
1857 sc.ctx = ctx;
1858 sc.debug = dbg;
1859 sc.evNumber = 0;
1860
1861 d3.json(urlSc, function(err, data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001862 var p = data && data.params || {},
1863 desc = data && data.description || null,
Simon Huntfc274c92014-11-11 11:05:46 -08001864 intro = data && data.title;
Simon Huntbb282f52014-11-10 11:08:19 -08001865
Simon Hunt56d51852014-11-09 13:03:35 -08001866 if (err) {
1867 view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err);
1868 } else {
1869 sc.params = p;
Simon Huntbb282f52014-11-10 11:08:19 -08001870 if (desc) {
1871 intro += '\n\n ' + desc.join('\n ');
1872 }
1873 view.alert(intro);
Simon Hunt56d51852014-11-09 13:03:35 -08001874 }
1875 });
1876
1877 }
1878
Simon Hunt01095ff2014-11-13 16:37:29 -08001879 // ==============================
1880 // Toggle Buttons in masthead
Simon Hunt0c6d4192014-11-12 12:07:10 -08001881
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001882 // TODO: toggle button (and other widgets in the masthead) should be provided
1883 // by the framework; not generated by the view.
1884
Simon Hunta5e89142014-11-14 07:00:33 -08001885 var showInstances,
1886 doPanZoom,
1887 showTrafficOnHover;
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001888
1889 function addButtonBar(view) {
1890 var bb = d3.select('#mast')
1891 .append('span').classed('right', true).attr('id', 'bb');
1892
Simon Hunta5e89142014-11-14 07:00:33 -08001893 function mkTogBtn(text, cb) {
1894 return bb.append('span')
1895 .classed('btn', true)
1896 .text(text)
1897 .on('click', cb);
1898 }
Simon Hunt01095ff2014-11-13 16:37:29 -08001899
Simon Hunta5e89142014-11-14 07:00:33 -08001900 showInstances = mkTogBtn('Show Instances', toggleInst);
1901 doPanZoom = mkTogBtn('Pan/Zoom', togglePanZoom);
1902 showTrafficOnHover = mkTogBtn('Show traffic on hover', toggleTrafficHover);
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001903 }
1904
Simon Hunta5e89142014-11-14 07:00:33 -08001905 function instShown() {
1906 return showInstances.classed('active');
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001907 }
Simon Hunta5e89142014-11-14 07:00:33 -08001908 function toggleInst() {
1909 showInstances.classed('active', !instShown());
1910 if (instShown()) {
1911 oiBox.show();
1912 } else {
1913 oiBox.hide();
1914 }
Simon Hunt01095ff2014-11-13 16:37:29 -08001915 }
1916
Simon Huntdeab4322014-11-13 18:49:07 -08001917 function panZoom() {
1918 return doPanZoom.classed('active');
Simon Hunt01095ff2014-11-13 16:37:29 -08001919 }
Simon Hunta5e89142014-11-14 07:00:33 -08001920 function togglePanZoom() {
1921 doPanZoom.classed('active', !panZoom());
1922 }
1923
1924 function trafficHover() {
1925 return showTrafficOnHover.classed('active');
1926 }
1927 function toggleTrafficHover() {
1928 showTrafficOnHover.classed('active', !trafficHover());
1929 }
1930
Simon Hunt7fa116d2014-11-17 14:16:55 -08001931 function loadGlyphs(svg) {
1932 var defs = svg.append('defs');
1933 gly.defBird(defs);
1934 gly.defBullhorn(defs);
1935 }
Simon Hunt01095ff2014-11-13 16:37:29 -08001936
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001937 // ==============================
Simon Hunt142d0032014-11-04 20:13:09 -08001938 // View life-cycle callbacks
Simon Hunt195cb382014-11-03 17:50:51 -08001939
Simon Huntf67722a2014-11-10 09:32:06 -08001940 function preload(view, ctx, flags) {
Simon Hunt142d0032014-11-04 20:13:09 -08001941 var w = view.width(),
1942 h = view.height(),
Simon Huntc7ee0662014-11-05 16:44:37 -08001943 fcfg = config.force,
1944 fpad = fcfg.pad,
1945 forceDim = [w - 2*fpad, h - 2*fpad];
Simon Hunt195cb382014-11-03 17:50:51 -08001946
Simon Hunt142d0032014-11-04 20:13:09 -08001947 // NOTE: view.$div is a D3 selection of the view's div
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001948 var viewBox = '0 0 ' + config.logicalSize + ' ' + config.logicalSize;
1949 svg = view.$div.append('svg').attr('viewBox', viewBox);
Simon Hunt934c3ce2014-11-05 11:45:07 -08001950 setSize(svg, view);
1951
Simon Hunt7fa116d2014-11-17 14:16:55 -08001952 loadGlyphs(svg);
Simon Hunt12ce12e2014-11-15 21:13:19 -08001953
Paul Greysonfcba0e82014-11-13 10:21:16 -08001954 zoomPanContainer = svg.append('g').attr('id', 'zoomPanContainer');
Paul Greysonfcba0e82014-11-13 10:21:16 -08001955 setupZoomPan();
1956
Simon Hunt1a9eff92014-11-07 11:06:34 -08001957 // add blue glow filter to svg layer
Paul Greysonfcba0e82014-11-13 10:21:16 -08001958 d3u.appendGlow(zoomPanContainer);
Simon Hunt1a9eff92014-11-07 11:06:34 -08001959
Simon Huntc7ee0662014-11-05 16:44:37 -08001960 // group for the topology
Paul Greysonfcba0e82014-11-13 10:21:16 -08001961 topoG = zoomPanContainer.append('g')
Simon Huntd3b7d512014-11-12 15:48:41 -08001962 .attr('id', 'topo-G')
Simon Huntc7ee0662014-11-05 16:44:37 -08001963 .attr('transform', fcfg.translate());
1964
Simon Hunte2575b62014-11-18 15:25:53 -08001965 // subgroups for links, link labels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08001966 linkG = topoG.append('g').attr('id', 'links');
Simon Hunte2575b62014-11-18 15:25:53 -08001967 linkLabelG = topoG.append('g').attr('id', 'linkLabels');
Simon Huntc7ee0662014-11-05 16:44:37 -08001968 nodeG = topoG.append('g').attr('id', 'nodes');
1969
Simon Hunte2575b62014-11-18 15:25:53 -08001970 // selection of links, linkLabels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08001971 link = linkG.selectAll('.link');
Simon Hunte2575b62014-11-18 15:25:53 -08001972 linkLabel = linkLabelG.selectAll('.linkLabel');
Simon Huntc7ee0662014-11-05 16:44:37 -08001973 node = nodeG.selectAll('.node');
1974
Simon Hunt7cd48f32014-11-09 23:42:50 -08001975 function chrg(d) {
1976 return fcfg.charge[d.class] || -12000;
1977 }
Simon Hunt99c13842014-11-06 18:23:12 -08001978 function ldist(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08001979 return fcfg.linkDistance[d.type] || 50;
Simon Hunt99c13842014-11-06 18:23:12 -08001980 }
1981 function lstrg(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08001982 // 0.0 - 1.0
1983 return fcfg.linkStrength[d.type] || 1.0;
Simon Hunt99c13842014-11-06 18:23:12 -08001984 }
1985
Simon Hunt1a9eff92014-11-07 11:06:34 -08001986 function selectCb(d, self) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001987 selectObject(d, self);
Simon Hunt1a9eff92014-11-07 11:06:34 -08001988 }
1989
1990 function atDragEnd(d, self) {
Simon Hunt56d51852014-11-09 13:03:35 -08001991 // once we've finished moving, pin the node in position
1992 d.fixed = true;
1993 d3.select(self).classed('fixed', true);
1994 if (config.useLiveData) {
Simon Hunt902c9922014-11-11 11:59:31 -08001995 sendUpdateMeta(d);
Simon Hunta255a2c2014-11-13 22:29:35 -08001996 } else {
1997 console.log('Moving node ' + d.id + ' to [' + d.x + ',' + d.y + ']');
Simon Hunt1a9eff92014-11-07 11:06:34 -08001998 }
1999 }
2000
Simon Hunt902c9922014-11-11 11:59:31 -08002001 function sendUpdateMeta(d) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002002 sendMessage('updateMeta', {
2003 id: d.id,
2004 'class': d.class,
Simon Hunt902c9922014-11-11 11:59:31 -08002005 'memento': {
Simon Hunt01095ff2014-11-13 16:37:29 -08002006 x: d.x,
2007 y: d.y
Simon Hunt902c9922014-11-11 11:59:31 -08002008 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002009 });
2010 }
2011
Simon Huntc7ee0662014-11-05 16:44:37 -08002012 // set up the force layout
2013 network.force = d3.layout.force()
2014 .size(forceDim)
2015 .nodes(network.nodes)
2016 .links(network.links)
Simon Hunt7cd48f32014-11-09 23:42:50 -08002017 .gravity(0.4)
2018 .friction(0.7)
2019 .charge(chrg)
Simon Hunt99c13842014-11-06 18:23:12 -08002020 .linkDistance(ldist)
2021 .linkStrength(lstrg)
Simon Huntc7ee0662014-11-05 16:44:37 -08002022 .on('tick', tick);
Simon Hunt195cb382014-11-03 17:50:51 -08002023
Simon Hunt01095ff2014-11-13 16:37:29 -08002024 network.drag = d3u.createDragBehavior(network.force,
Simon Huntdeab4322014-11-13 18:49:07 -08002025 selectCb, atDragEnd, panZoom);
Simon Hunt0c6d4192014-11-12 12:07:10 -08002026
2027 // create mask layer for when we lose connection to server.
Simon Hunta5e89142014-11-14 07:00:33 -08002028 // TODO: this should be part of the framework
Simon Hunt0c6d4192014-11-12 12:07:10 -08002029 mask = view.$div.append('div').attr('id','topo-mask');
2030 para(mask, 'Oops!');
2031 para(mask, 'Web-socket connection to server closed...');
2032 para(mask, 'Try refreshing the page.');
Simon Hunt12ce12e2014-11-15 21:13:19 -08002033
2034 mask.append('svg')
2035 .attr({
2036 id: 'mask-bird',
2037 width: w,
2038 height: h
2039 })
2040 .append('g')
2041 .attr('transform', birdTranslate(w, h))
2042 .style('opacity', 0.3)
2043 .append('use')
2044 .attr({
2045 'xlink:href': '#bird',
2046 width: config.birdDim,
2047 height: config.birdDim,
2048 fill: '#111'
2049 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08002050 }
Simon Hunt195cb382014-11-03 17:50:51 -08002051
Simon Hunt01095ff2014-11-13 16:37:29 -08002052 function para(sel, text) {
2053 sel.append('p').text(text);
2054 }
2055
2056
Simon Hunt56d51852014-11-09 13:03:35 -08002057 function load(view, ctx, flags) {
Simon Huntf67722a2014-11-10 09:32:06 -08002058 // resize, in case the window was resized while we were not loaded
2059 resize(view, ctx, flags);
2060
Simon Hunt99c13842014-11-06 18:23:12 -08002061 // cache the view token, so network topo functions can access it
2062 network.view = view;
Simon Hunt56d51852014-11-09 13:03:35 -08002063 config.useLiveData = !flags.local;
2064
2065 if (!config.useLiveData) {
2066 prepareScenario(view, ctx, flags.debug);
2067 }
Simon Hunt99c13842014-11-06 18:23:12 -08002068
2069 // set our radio buttons and key bindings
Simon Hunt9462e8c2014-11-14 17:28:09 -08002070 layerBtnSet = view.setRadio(layerButtons);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002071 view.setKeys(keyDispatch);
Simon Hunt195cb382014-11-03 17:50:51 -08002072
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002073 // patch in our "button bar" for now
2074 // TODO: implement a more official frameworky way of doing this..
2075 addButtonBar(view);
2076
Simon Huntd3b7d512014-11-12 15:48:41 -08002077 // Load map data asynchronously; complete startup after that..
2078 loadGeoJsonData();
Simon Hunta255a2c2014-11-13 22:29:35 -08002079
2080 // start the and timer
2081 var dashIdx = 0;
2082 antTimer = setInterval(function () {
2083 // TODO: figure out how to choose Src-->Dst and Dst-->Src, per link
2084 dashIdx = dashIdx === 0 ? 14 : dashIdx - 2;
2085 d3.selectAll('.animated').style('stroke-dashoffset', dashIdx);
2086 }, 35);
2087 }
2088
2089 function unload(view, ctx, flags) {
2090 if (antTimer) {
2091 clearInterval(antTimer);
2092 antTimer = null;
2093 }
Simon Huntd3b7d512014-11-12 15:48:41 -08002094 }
2095
2096 // TODO: move these to config/state portion of script
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002097 var geoJsonUrl = 'json/map/continental_us.json', // TODO: Paul
Simon Huntd3b7d512014-11-12 15:48:41 -08002098 geoJson;
2099
2100 function loadGeoJsonData() {
2101 d3.json(geoJsonUrl, function (err, data) {
2102 if (err) {
2103 // fall back to USA map background
2104 loadStaticMap();
2105 } else {
2106 geoJson = data;
2107 loadGeoMap();
2108 }
2109
2110 // finally, connect to the server...
2111 if (config.useLiveData) {
2112 webSock.connect();
2113 }
2114 });
2115 }
2116
2117 function showBg() {
2118 return config.options.showBackground ? 'visible' : 'hidden';
2119 }
2120
2121 function loadStaticMap() {
2122 fnTrace('loadStaticMap', config.backgroundUrl);
2123 var w = network.view.width(),
2124 h = network.view.height();
2125
2126 // load the background image
2127 bgImg = svg.insert('svg:image', '#topo-G')
2128 .attr({
2129 id: 'topo-bg',
2130 width: w,
2131 height: h,
2132 'xlink:href': config.backgroundUrl
2133 })
2134 .style({
2135 visibility: showBg()
2136 });
2137 }
2138
2139 function loadGeoMap() {
2140 fnTrace('loadGeoMap', geoJsonUrl);
Simon Huntd3b7d512014-11-12 15:48:41 -08002141
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002142 // extracts the topojson data into geocoordinate-based geometry
2143 var topoData = topojson.feature(geoJson, geoJson.objects.states);
Simon Huntd3b7d512014-11-12 15:48:41 -08002144
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002145 // see: http://bl.ocks.org/mbostock/4707858
2146 geoMapProjection = d3.geo.mercator();
2147 var path = d3.geo.path().projection(geoMapProjection);
Simon Huntd3b7d512014-11-12 15:48:41 -08002148
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002149 geoMapProjection
2150 .scale(1)
2151 .translate([0, 0]);
Simon Huntd3b7d512014-11-12 15:48:41 -08002152
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002153 // [[x1,y1],[x2,y2]]
2154 var b = path.bounds(topoData);
Paul Greysonfcba0e82014-11-13 10:21:16 -08002155 // size map to 95% of minimum dimension to fill space
2156 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 -08002157 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 -08002158
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002159 geoMapProjection
2160 .scale(s)
2161 .translate(t);
2162
Paul Greysonfcba0e82014-11-13 10:21:16 -08002163 bgImg = zoomPanContainer.insert("g", '#topo-G');
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002164 bgImg.attr('id', 'map').selectAll('path')
2165 .data(topoData.features)
2166 .enter()
2167 .append('path')
2168 .attr('d', path);
Simon Hunt195cb382014-11-03 17:50:51 -08002169 }
2170
Simon Huntf67722a2014-11-10 09:32:06 -08002171 function resize(view, ctx, flags) {
Simon Hunt12ce12e2014-11-15 21:13:19 -08002172 var w = view.width(),
2173 h = view.height();
2174
Simon Hunt934c3ce2014-11-05 11:45:07 -08002175 setSize(svg, view);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002176
2177 d3.select('#mask-bird').attr({ width: w, height: h})
2178 .select('g').attr('transform', birdTranslate(w, h));
Simon Hunt142d0032014-11-04 20:13:09 -08002179 }
2180
Simon Hunt12ce12e2014-11-15 21:13:19 -08002181 function birdTranslate(w, h) {
2182 var bdim = config.birdDim;
2183 return 'translate('+((w-bdim)*.4)+','+((h-bdim)*.1)+')';
2184 }
Simon Hunt142d0032014-11-04 20:13:09 -08002185
2186 // ==============================
2187 // View registration
Simon Hunt195cb382014-11-03 17:50:51 -08002188
Simon Hunt25248912014-11-04 11:25:48 -08002189 onos.ui.addView('topo', {
Simon Hunt142d0032014-11-04 20:13:09 -08002190 preload: preload,
2191 load: load,
Simon Hunta255a2c2014-11-13 22:29:35 -08002192 unload: unload,
Simon Hunt142d0032014-11-04 20:13:09 -08002193 resize: resize
Simon Hunt195cb382014-11-03 17:50:51 -08002194 });
2195
Simon Hunt61d04042014-11-11 17:27:16 -08002196 detailPane = onos.ui.addFloatingPanel('topo-detail');
Simon Hunta5e89142014-11-14 07:00:33 -08002197 oiBox = onos.ui.addFloatingPanel('topo-oibox', 'TL');
Simon Hunt61d04042014-11-11 17:27:16 -08002198
Simon Hunt195cb382014-11-03 17:50:51 -08002199}(ONOS));