blob: e6bea489c8e394dffee101d04fb538453754ea38 [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: {
Thomas Vachuska89543292014-11-19 11:28:33 -080073 linkBaseColor: '#666',
Simon Hunt1a9eff92014-11-07 11:06:34 -080074 linkInColor: '#66f',
Paul Greyson29cd58f2014-11-18 13:14:57 -080075 linkInWidth: 14,
Thomas Vachuska89543292014-11-19 11:28:33 -080076 linkOutColor: '#f00',
Simon Hunt13bf9c82014-11-18 07:26:44 -080077 linkOutWidth: 30
Simon Hunt1a9eff92014-11-07 11:06:34 -080078 },
Paul Greyson29cd58f2014-11-18 13:14:57 -080079 icons: {
Thomas Vachuska89543292014-11-19 11:28:33 -080080 w: 24,
81 h: 24,
82 xoff: -10,
83 yoff: -6
84 },
85 iconUrl: {
86 device: 'img/device.png',
87 host: 'img/host.png',
88 pkt: 'img/pkt.png',
89 opt: 'img/opt.png'
Simon Hunt195cb382014-11-03 17:50:51 -080090 },
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,
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -0800143 H: toggleHover,
Simon Hunt9462e8c2014-11-14 17:28:09 -0800144 esc: handleEscape
Simon Hunt934c3ce2014-11-05 11:45:07 -0800145 };
Simon Hunt142d0032014-11-04 20:13:09 -0800146
Simon Hunt195cb382014-11-03 17:50:51 -0800147 // state variables
Simon Hunt99c13842014-11-06 18:23:12 -0800148 var network = {
Simon Hunt50128c02014-11-08 13:36:15 -0800149 view: null, // view token reference
Simon Hunt99c13842014-11-06 18:23:12 -0800150 nodes: [],
151 links: [],
Simon Hunt269670f2014-11-17 16:17:43 -0800152 lookup: {},
153 revLinkToKey: {}
Simon Hunt99c13842014-11-06 18:23:12 -0800154 },
Simon Hunt56d51852014-11-09 13:03:35 -0800155 scenario = {
156 evDir: 'json/ev/',
157 evScenario: '/scenario.json',
158 evPrefix: '/ev_',
159 evOnos: '_onos.json',
160 evUi: '_ui.json',
161 ctx: null,
162 params: {},
163 evNumber: 0,
164 view: null,
165 debug: false
166 },
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800167 webSock,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800168 sid = 0,
Simon Hunt56d51852014-11-09 13:03:35 -0800169 deviceLabelIndex = 0,
170 hostLabelIndex = 0,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800171 selections = {},
Simon Hunta5e89142014-11-14 07:00:33 -0800172 selectOrder = [],
Simon Hunt6ac93f32014-11-13 12:17:27 -0800173 hovered = null,
Simon Hunta5e89142014-11-14 07:00:33 -0800174 detailPane,
Simon Hunta255a2c2014-11-13 22:29:35 -0800175 antTimer = null,
Simon Hunta5e89142014-11-14 07:00:33 -0800176 onosInstances = {},
177 onosOrder = [],
178 oiBox,
Simon Hunt9462e8c2014-11-14 17:28:09 -0800179 oiShowMaster = false,
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -0800180 hoverEnabled = false,
Simon Hunt195cb382014-11-03 17:50:51 -0800181 portLabelsOn = false;
182
Simon Hunt934c3ce2014-11-05 11:45:07 -0800183 // D3 selections
184 var svg,
Paul Greysonfcba0e82014-11-13 10:21:16 -0800185 zoomPanContainer,
Simon Hunt934c3ce2014-11-05 11:45:07 -0800186 bgImg,
Simon Huntc7ee0662014-11-05 16:44:37 -0800187 topoG,
188 nodeG,
189 linkG,
Simon Hunte2575b62014-11-18 15:25:53 -0800190 linkLabelG,
Simon Huntc7ee0662014-11-05 16:44:37 -0800191 node,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800192 link,
Simon Hunte2575b62014-11-18 15:25:53 -0800193 linkLabel,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800194 mask;
Simon Hunt195cb382014-11-03 17:50:51 -0800195
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800196 // the projection for the map background
197 var geoMapProjection;
198
Paul Greysonfcba0e82014-11-13 10:21:16 -0800199 // the zoom function
200 var zoom;
201
Simon Hunt142d0032014-11-04 20:13:09 -0800202 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800203 // For Debugging / Development
Simon Hunt195cb382014-11-03 17:50:51 -0800204
Simon Hunt99c13842014-11-06 18:23:12 -0800205 function note(label, msg) {
206 console.log('NOTE: ' + label + ': ' + msg);
Simon Hunt195cb382014-11-03 17:50:51 -0800207 }
208
Simon Hunt99c13842014-11-06 18:23:12 -0800209 function debug(what) {
210 return config.debugOn && config.debug[what];
Simon Hunt934c3ce2014-11-05 11:45:07 -0800211 }
212
Simon Huntfc274c92014-11-11 11:05:46 -0800213 function fnTrace(msg, id) {
214 if (config.fnTrace) {
215 console.log('FN: ' + msg + ' [' + id + ']');
216 }
217 }
Simon Hunt99c13842014-11-06 18:23:12 -0800218
Simon Hunta5e89142014-11-14 07:00:33 -0800219 function evTrace(data) {
220 fnTrace(data.event, data.payload.id);
221 }
222
Simon Hunt934c3ce2014-11-05 11:45:07 -0800223 // ==============================
224 // Key Callbacks
225
Simon Hunt99c13842014-11-06 18:23:12 -0800226 function testMe(view) {
Simon Hunt625dc402014-11-18 10:57:18 -0800227 view.alert('Theme is ' + view.theme());
Simon Hunt99c13842014-11-06 18:23:12 -0800228 }
229
Simon Hunt56d51852014-11-09 13:03:35 -0800230 function abortIfLive() {
Simon Hunt50128c02014-11-08 13:36:15 -0800231 if (config.useLiveData) {
Simon Huntb53e0682014-11-12 13:32:01 -0800232 network.view.alert("Sorry, currently using live data..");
Simon Hunt56d51852014-11-09 13:03:35 -0800233 return true;
Simon Hunt50128c02014-11-08 13:36:15 -0800234 }
Simon Hunt56d51852014-11-09 13:03:35 -0800235 return false;
236 }
Simon Hunt50128c02014-11-08 13:36:15 -0800237
Simon Hunt56d51852014-11-09 13:03:35 -0800238 function testDebug(msg) {
239 if (scenario.debug) {
240 scenario.view.alert(msg);
241 }
242 }
Simon Hunt99c13842014-11-06 18:23:12 -0800243
Simon Hunt56d51852014-11-09 13:03:35 -0800244 function injectTestEvent(view) {
245 if (abortIfLive()) { return; }
246 var sc = scenario,
247 evn = ++sc.evNumber,
248 pfx = sc.evDir + sc.ctx + sc.evPrefix + evn,
249 onosUrl = pfx + sc.evOnos,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800250 uiUrl = pfx + sc.evUi,
251 stack = [
252 { url: onosUrl, cb: handleServerEvent },
253 { url: uiUrl, cb: handleUiEvent }
254 ];
255 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800256 }
257
Simon Hunt7cd48f32014-11-09 23:42:50 -0800258 function recurseFetchEvent(stack, evn) {
259 var v = scenario.view,
260 frame;
261 if (stack.length === 0) {
Simon Huntfc274c92014-11-11 11:05:46 -0800262 v.alert('Oops!\n\nNo event #' + evn + ' found.');
Simon Hunt7cd48f32014-11-09 23:42:50 -0800263 return;
264 }
265 frame = stack.shift();
266
267 d3.json(frame.url, function (err, data) {
Simon Hunt99c13842014-11-06 18:23:12 -0800268 if (err) {
Simon Hunt56d51852014-11-09 13:03:35 -0800269 if (err.status === 404) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800270 // if we didn't find the data, try the next stack frame
271 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800272 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800273 v.alert('non-404 error:\n\n' + frame.url + '\n\n' + err);
Simon Hunt56d51852014-11-09 13:03:35 -0800274 }
Simon Hunt99c13842014-11-06 18:23:12 -0800275 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800276 testDebug('loaded: ' + frame.url);
Simon Hunt1712ed82014-11-17 12:56:00 -0800277 wsTrace('test', JSON.stringify(data));
Simon Hunt7cd48f32014-11-09 23:42:50 -0800278 frame.cb(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800279 }
280 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800281
Simon Hunt56d51852014-11-09 13:03:35 -0800282 }
Simon Hunt50128c02014-11-08 13:36:15 -0800283
Simon Hunt56d51852014-11-09 13:03:35 -0800284 function handleUiEvent(data) {
Simon Huntbb282f52014-11-10 11:08:19 -0800285 scenario.view.alert('UI Tx: ' + data.event + '\n\n' +
286 JSON.stringify(data));
Simon Hunt56d51852014-11-09 13:03:35 -0800287 }
288
289 function injectStartupEvents(view) {
290 var last = scenario.params.lastAuto || 0;
291 if (abortIfLive()) { return; }
292
293 while (scenario.evNumber < last) {
Simon Hunt1a9eff92014-11-07 11:06:34 -0800294 injectTestEvent(view);
295 }
296 }
297
Simon Hunt934c3ce2014-11-05 11:45:07 -0800298 function toggleBg() {
299 var vis = bgImg.style('visibility');
300 bgImg.style('visibility', (vis === 'hidden') ? 'visible' : 'hidden');
301 }
302
Simon Hunt99c13842014-11-06 18:23:12 -0800303 function cycleLabels() {
Simon Huntbb282f52014-11-10 11:08:19 -0800304 deviceLabelIndex = (deviceLabelIndex === network.deviceLabelCount - 1)
305 ? 0 : deviceLabelIndex + 1;
Simon Hunt5f36d342014-11-08 21:33:14 -0800306
Simon Hunt99c13842014-11-06 18:23:12 -0800307 network.nodes.forEach(function (d) {
Simon Huntbb282f52014-11-10 11:08:19 -0800308 if (d.class === 'device') {
309 updateDeviceLabel(d);
310 }
Simon Hunt99c13842014-11-06 18:23:12 -0800311 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800312 }
313
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -0800314 function toggleHover(view) {
315 hoverEnabled = !hoverEnabled;
316 }
317
Simon Hunt934c3ce2014-11-05 11:45:07 -0800318 function togglePorts(view) {
Simon Hunt50128c02014-11-08 13:36:15 -0800319 view.alert('togglePorts() callback')
Simon Hunt934c3ce2014-11-05 11:45:07 -0800320 }
321
Simon Hunt6ac93f32014-11-13 12:17:27 -0800322 function unpin() {
323 if (hovered) {
324 hovered.fixed = false;
325 hovered.el.classed('fixed', false);
326 network.force.resume();
327 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800328 }
329
Simon Hunt9462e8c2014-11-14 17:28:09 -0800330 function handleEscape(view) {
331 if (oiShowMaster) {
332 cancelAffinity();
333 } else {
334 deselectAll();
335 }
336 }
337
Simon Hunt934c3ce2014-11-05 11:45:07 -0800338 // ==============================
339 // Radio Button Callbacks
340
Simon Hunta5e89142014-11-14 07:00:33 -0800341 var layerLookup = {
342 host: {
343 endstation: 'pkt', // default, if host event does not define type
Thomas Vachuska89543292014-11-19 11:28:33 -0800344 router: 'pkt',
Simon Hunta5e89142014-11-14 07:00:33 -0800345 bgpSpeaker: 'pkt'
346 },
347 device: {
348 switch: 'pkt',
349 roadm: 'opt'
350 },
351 link: {
352 hostLink: 'pkt',
353 direct: 'pkt',
Simon Hunt8257f4c2014-11-16 19:34:54 -0800354 indirect: '',
355 tunnel: '',
Simon Hunta5e89142014-11-14 07:00:33 -0800356 optical: 'opt'
357 }
358 };
359
360 function inLayer(d, layer) {
Simon Hunt8257f4c2014-11-16 19:34:54 -0800361 var type = d.class === 'link' ? d.type() : d.type,
362 look = layerLookup[d.class],
363 lyr = look && look[type];
Simon Hunta5e89142014-11-14 07:00:33 -0800364 return lyr === layer;
365 }
366
367 function unsuppressLayer(which) {
368 node.each(function (d) {
369 var node = d.el;
370 if (inLayer(d, which)) {
371 node.classed('suppressed', false);
372 }
373 });
374
375 link.each(function (d) {
376 var link = d.el;
377 if (inLayer(d, which)) {
378 link.classed('suppressed', false);
379 }
380 });
381 }
382
Simon Hunt9462e8c2014-11-14 17:28:09 -0800383 function suppressLayers(b) {
384 node.classed('suppressed', b);
385 link.classed('suppressed', b);
Simon Hunt142d0032014-11-04 20:13:09 -0800386// d3.selectAll('svg .port').classed('inactive', false);
387// d3.selectAll('svg .portText').classed('inactive', false);
Simon Hunt195cb382014-11-03 17:50:51 -0800388 }
389
Simon Hunt9462e8c2014-11-14 17:28:09 -0800390 function showAllLayers() {
391 suppressLayers(false);
392 }
393
Simon Hunt195cb382014-11-03 17:50:51 -0800394 function showPacketLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800395 node.classed('suppressed', true);
396 link.classed('suppressed', true);
397 unsuppressLayer('pkt');
Simon Hunt195cb382014-11-03 17:50:51 -0800398 }
399
400 function showOpticalLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800401 node.classed('suppressed', true);
402 link.classed('suppressed', true);
403 unsuppressLayer('opt');
Simon Hunt195cb382014-11-03 17:50:51 -0800404 }
405
Simon Hunt9462e8c2014-11-14 17:28:09 -0800406 function restoreLayerState() {
407 layerBtnDispatch[layerBtnSet.selected()]();
408 }
409
Simon Hunt142d0032014-11-04 20:13:09 -0800410 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800411 // Private functions
412
Simon Hunt99c13842014-11-06 18:23:12 -0800413 function safeId(s) {
414 return s.replace(/[^a-z0-9]/gi, '-');
415 }
416
Simon Huntc7ee0662014-11-05 16:44:37 -0800417 // set the size of the given element to that of the view (reduced if padded)
418 function setSize(el, view, pad) {
419 var padding = pad ? pad * 2 : 0;
Simon Hunt934c3ce2014-11-05 11:45:07 -0800420 el.attr({
Simon Huntc7ee0662014-11-05 16:44:37 -0800421 width: view.width() - padding,
422 height: view.height() - padding
Simon Hunt934c3ce2014-11-05 11:45:07 -0800423 });
424 }
425
Simon Hunt8257f4c2014-11-16 19:34:54 -0800426 function makeNodeKey(d, what) {
427 var port = what + 'Port';
428 return d[what] + '/' + d[port];
429 }
430
431 function makeLinkKey(d, flipped) {
432 var one = flipped ? makeNodeKey(d, 'dst') : makeNodeKey(d, 'src'),
433 two = flipped ? makeNodeKey(d, 'src') : makeNodeKey(d, 'dst');
434 return one + '-' + two;
435 }
436
Simon Hunt269670f2014-11-17 16:17:43 -0800437 function findLinkById(id) {
438 // check to see if this is a reverse lookup, else default to given id
439 var key = network.revLinkToKey[id] || id;
440 return key && network.lookup[key];
441 }
442
Simon Hunt8257f4c2014-11-16 19:34:54 -0800443 function findLink(linkData, op) {
444 var key = makeLinkKey(linkData),
445 keyrev = makeLinkKey(linkData, 1),
446 link = network.lookup[key],
447 linkRev = network.lookup[keyrev],
448 result = {},
449 ldata = link || linkRev,
450 rawLink;
451
452 if (op === 'add') {
453 if (link) {
454 // trying to add a link that we already know about
455 result.ldata = link;
456 result.badLogic = 'addLink: link already added';
457
458 } else if (linkRev) {
459 // we found the reverse of the link to be added
460 result.ldata = linkRev;
461 if (linkRev.fromTarget) {
462 result.badLogic = 'addLink: link already added';
463 }
464 }
465 } else if (op === 'update') {
466 if (!ldata) {
467 result.badLogic = 'updateLink: link not found';
468 } else {
469 rawLink = link ? ldata.fromSource : ldata.fromTarget;
470 result.updateWith = function (data) {
471 $.extend(rawLink, data);
472 restyleLinkElement(ldata);
473 }
474 }
475 } else if (op === 'remove') {
476 if (!ldata) {
477 result.badLogic = 'removeLink: link not found';
478 } else {
479 rawLink = link ? ldata.fromSource : ldata.fromTarget;
480
481 if (!rawLink) {
482 result.badLogic = 'removeLink: link not found';
483
484 } else {
485 result.removeRawLink = function () {
486 if (link) {
487 // remove fromSource
488 ldata.fromSource = null;
489 if (ldata.fromTarget) {
490 // promote target into source position
491 ldata.fromSource = ldata.fromTarget;
492 ldata.fromTarget = null;
493 ldata.key = keyrev;
494 delete network.lookup[key];
495 network.lookup[keyrev] = ldata;
Simon Hunt269670f2014-11-17 16:17:43 -0800496 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800497 }
498 } else {
499 // remove fromTarget
500 ldata.fromTarget = null;
Simon Hunt269670f2014-11-17 16:17:43 -0800501 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800502 }
503 if (ldata.fromSource) {
504 restyleLinkElement(ldata);
505 } else {
506 removeLinkElement(ldata);
507 }
508 }
509 }
510 }
511 }
512 return result;
513 }
514
515 function addLinkUpdate(ldata, link) {
516 // add link event, but we already have the reverse link installed
517 ldata.fromTarget = link;
Simon Hunt269670f2014-11-17 16:17:43 -0800518 network.revLinkToKey[link.id] = ldata.key;
Simon Hunt8257f4c2014-11-16 19:34:54 -0800519 restyleLinkElement(ldata);
520 }
521
522 var allLinkTypes = 'direct indirect optical tunnel',
523 defaultLinkType = 'direct';
524
525 function restyleLinkElement(ldata) {
526 // this fn's job is to look at raw links and decide what svg classes
527 // need to be applied to the line element in the DOM
528 var el = ldata.el,
529 type = ldata.type(),
530 lw = ldata.linkWidth(),
531 online = ldata.online();
532
533 el.classed('link', true);
534 el.classed('inactive', !online);
535 el.classed(allLinkTypes, false);
536 if (type) {
537 el.classed(type, true);
538 }
539 el.transition()
540 .duration(1000)
Thomas Vachuska89543292014-11-19 11:28:33 -0800541 .attr('stroke-width', linkScale(lw))
542 .attr('stroke', config.topo.linkBaseColor);
Simon Hunt8257f4c2014-11-16 19:34:54 -0800543 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800544
Simon Hunt99c13842014-11-06 18:23:12 -0800545 // ==============================
546 // Event handlers for server-pushed events
547
Simon Huntbb282f52014-11-10 11:08:19 -0800548 function logicError(msg) {
549 // TODO, report logic error to server, via websock, so it can be logged
Simon Huntcb56cff2014-11-17 11:42:26 -0800550 //network.view.alert('Logic Error:\n\n' + msg);
Simon Huntfc274c92014-11-11 11:05:46 -0800551 console.warn(msg);
Simon Huntbb282f52014-11-10 11:08:19 -0800552 }
553
Simon Hunt99c13842014-11-06 18:23:12 -0800554 var eventDispatch = {
Simon Hunta5e89142014-11-14 07:00:33 -0800555 addInstance: addInstance,
Simon Hunt99c13842014-11-06 18:23:12 -0800556 addDevice: addDevice,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800557 addLink: addLink,
Simon Hunt56d51852014-11-09 13:03:35 -0800558 addHost: addHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800559
Simon Huntd72bc702014-11-13 18:38:04 -0800560 updateInstance: stillToImplement,
Simon Huntbb282f52014-11-10 11:08:19 -0800561 updateDevice: updateDevice,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800562 updateLink: updateLink,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800563 updateHost: updateHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800564
Simon Huntd72bc702014-11-13 18:38:04 -0800565 removeInstance: stillToImplement,
Simon Huntbb282f52014-11-10 11:08:19 -0800566 removeDevice: stillToImplement,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800567 removeLink: removeLink,
Simon Hunt44031102014-11-11 13:20:36 -0800568 removeHost: removeHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800569
Simon Hunt61d04042014-11-11 17:27:16 -0800570 showDetails: showDetails,
Simon Huntb53e0682014-11-12 13:32:01 -0800571 showTraffic: showTraffic
Simon Hunt99c13842014-11-06 18:23:12 -0800572 };
573
Simon Hunta5e89142014-11-14 07:00:33 -0800574 function addInstance(data) {
575 evTrace(data);
576 var inst = data.payload,
577 id = inst.id;
578 if (onosInstances[id]) {
579 logicError('ONOS instance already added: ' + id);
580 return;
581 }
582 onosInstances[id] = inst;
583 onosOrder.push(inst);
584 updateInstances();
585 }
586
Simon Hunt99c13842014-11-06 18:23:12 -0800587 function addDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800588 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800589 var device = data.payload,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800590 nodeData = createDeviceNode(device);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800591 network.nodes.push(nodeData);
592 network.lookup[nodeData.id] = nodeData;
Simon Hunt99c13842014-11-06 18:23:12 -0800593 updateNodes();
594 network.force.start();
595 }
596
Simon Hunt99c13842014-11-06 18:23:12 -0800597 function addLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800598 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800599 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800600 result = findLink(link, 'add'),
601 bad = result.badLogic,
602 ldata = result.ldata;
603
604 if (bad) {
605 logicError(bad + ': ' + link.id);
606 return;
607 }
608
609 if (ldata) {
610 // we already have a backing store link for src/dst nodes
611 addLinkUpdate(ldata, link);
612 return;
613 }
614
615 // no backing store link yet
616 ldata = createLink(link);
617 if (ldata) {
618 network.links.push(ldata);
619 network.lookup[ldata.key] = ldata;
Simon Hunt99c13842014-11-06 18:23:12 -0800620 updateLinks();
621 network.force.start();
622 }
623 }
624
Simon Hunt56d51852014-11-09 13:03:35 -0800625 function addHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800626 evTrace(data);
Simon Hunt56d51852014-11-09 13:03:35 -0800627 var host = data.payload,
628 node = createHostNode(host),
629 lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800630 network.nodes.push(node);
631 network.lookup[host.id] = node;
632 updateNodes();
633
634 lnk = createHostLink(host);
635 if (lnk) {
Simon Hunt44031102014-11-11 13:20:36 -0800636 node.linkData = lnk; // cache ref on its host
Simon Hunt56d51852014-11-09 13:03:35 -0800637 network.links.push(lnk);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800638 network.lookup[host.ingress] = lnk;
639 network.lookup[host.egress] = lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800640 updateLinks();
641 }
642 network.force.start();
643 }
644
Simon Hunt44031102014-11-11 13:20:36 -0800645 // TODO: fold updateX(...) methods into one base method; remove duplication
Simon Huntbb282f52014-11-10 11:08:19 -0800646 function updateDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800647 evTrace(data);
Simon Huntbb282f52014-11-10 11:08:19 -0800648 var device = data.payload,
649 id = device.id,
650 nodeData = network.lookup[id];
651 if (nodeData) {
652 $.extend(nodeData, device);
653 updateDeviceState(nodeData);
654 } else {
655 logicError('updateDevice lookup fail. ID = "' + id + '"');
656 }
657 }
658
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800659 function updateLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800660 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800661 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800662 result = findLink(link, 'update'),
663 bad = result.badLogic;
664 if (bad) {
665 logicError(bad + ': ' + link.id);
666 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800667 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800668 result.updateWith(link);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800669 }
670
Simon Hunt7cd48f32014-11-09 23:42:50 -0800671 function updateHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800672 evTrace(data);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800673 var host = data.payload,
Simon Huntbb282f52014-11-10 11:08:19 -0800674 id = host.id,
675 hostData = network.lookup[id];
676 if (hostData) {
677 $.extend(hostData, host);
678 updateHostState(hostData);
679 } else {
680 logicError('updateHost lookup fail. ID = "' + id + '"');
681 }
Simon Hunt7cd48f32014-11-09 23:42:50 -0800682 }
683
Simon Hunt44031102014-11-11 13:20:36 -0800684 // TODO: fold removeX(...) methods into base method - remove dup code
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800685 function removeLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800686 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800687 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800688 result = findLink(link, 'remove'),
689 bad = result.badLogic;
690 if (bad) {
691 logicError(bad + ': ' + link.id);
692 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800693 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800694 result.removeRawLink();
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800695 }
696
Simon Hunt44031102014-11-11 13:20:36 -0800697 function removeHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800698 evTrace(data);
Simon Hunt44031102014-11-11 13:20:36 -0800699 var host = data.payload,
700 id = host.id,
701 hostData = network.lookup[id];
702 if (hostData) {
703 removeHostElement(hostData);
704 } else {
705 logicError('removeHost lookup fail. ID = "' + id + '"');
706 }
707 }
708
Simon Hunt61d04042014-11-11 17:27:16 -0800709 function showDetails(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800710 evTrace(data);
Simon Hunt61d04042014-11-11 17:27:16 -0800711 populateDetails(data.payload);
712 detailPane.show();
713 }
714
Simon Huntb53e0682014-11-12 13:32:01 -0800715 function showTraffic(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800716 evTrace(data);
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800717 var paths = data.payload.paths;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800718
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800719 // Revert any links hilighted previously.
Simon Hunta255a2c2014-11-13 22:29:35 -0800720 link.classed('primary secondary animated optical', false);
Simon Hunte2575b62014-11-18 15:25:53 -0800721 // Remove all previous labels.
722 removeLinkLabels();
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800723
Simon Hunte2575b62014-11-18 15:25:53 -0800724 // Now hilight all links in the paths payload, and attach
725 // labels to them, if they are defined.
Simon Hunta255a2c2014-11-13 22:29:35 -0800726 paths.forEach(function (p) {
Simon Hunte2575b62014-11-18 15:25:53 -0800727 var n = p.links.length,
728 i,
729 ldata;
730
731 for (i=0; i<n; i++) {
732 ldata = findLinkById(p.links[i]);
733 if (ldata) {
734 ldata.el.classed(p.class, true);
735 ldata.label = p.labels[i];
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800736 }
Simon Hunte2575b62014-11-18 15:25:53 -0800737 }
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800738 });
Simon Hunte2575b62014-11-18 15:25:53 -0800739 updateLinks();
Simon Huntb53e0682014-11-12 13:32:01 -0800740 }
741
Simon Hunt56d51852014-11-09 13:03:35 -0800742 // ...............................
743
744 function stillToImplement(data) {
745 var p = data.payload;
746 note(data.event, p.id);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800747 network.view.alert('Not yet implemented: "' + data.event + '"');
Simon Hunt56d51852014-11-09 13:03:35 -0800748 }
Simon Hunt99c13842014-11-06 18:23:12 -0800749
750 function unknownEvent(data) {
Simon Hunt50128c02014-11-08 13:36:15 -0800751 network.view.alert('Unknown event type: "' + data.event + '"');
Simon Hunt99c13842014-11-06 18:23:12 -0800752 }
753
754 function handleServerEvent(data) {
755 var fn = eventDispatch[data.event] || unknownEvent;
756 fn(data);
757 }
758
759 // ==============================
Simon Hunt61d04042014-11-11 17:27:16 -0800760 // Out-going messages...
761
Simon Huntb53e0682014-11-12 13:32:01 -0800762 function userFeedback(msg) {
763 // for now, use the alert pane as is. Maybe different alert style in
764 // the future (centered on view; dismiss button?)
765 network.view.alert(msg);
766 }
767
768 function nSel() {
769 return selectOrder.length;
770 }
Simon Hunt61d04042014-11-11 17:27:16 -0800771 function getSel(idx) {
772 return selections[selectOrder[idx]];
773 }
Simon Huntb53e0682014-11-12 13:32:01 -0800774 function getSelId(idx) {
775 return getSel(idx).obj.id;
776 }
777 function allSelectionsClass(cls) {
778 for (var i=0, n=nSel(); i<n; i++) {
779 if (getSel(i).obj.class !== cls) {
780 return false;
781 }
782 }
783 return true;
784 }
Simon Hunt61d04042014-11-11 17:27:16 -0800785
Simon Hunt61d04042014-11-11 17:27:16 -0800786 // request details for the selected element
Simon Huntd72bc702014-11-13 18:38:04 -0800787 // invoked from selection of a single node.
Simon Hunt61d04042014-11-11 17:27:16 -0800788 function requestDetails() {
789 var data = getSel(0).obj,
790 payload = {
791 id: data.id,
792 class: data.class
793 };
794 sendMessage('requestDetails', payload);
795 }
796
Simon Huntd72bc702014-11-13 18:38:04 -0800797 function addIntentAction() {
798 sendMessage('addHostIntent', {
799 one: getSelId(0),
Thomas Vachuska82f2c622014-11-17 12:23:18 -0800800 two: getSelId(1),
801 ids: [ getSelId(0), getSelId(1) ]
Simon Huntd72bc702014-11-13 18:38:04 -0800802 });
803 }
804
805 function showTrafficAction() {
806 // if nothing is hovered over, and nothing selected, send cancel request
807 if (!hovered && nSel() === 0) {
808 sendMessage('cancelTraffic', {});
809 return;
810 }
811
812 // NOTE: hover is only populated if "show traffic on hover" is
813 // toggled on, and the item hovered is a host...
814 var hoverId = (trafficHover() && hovered && hovered.class === 'host')
815 ? hovered.id : '';
816 sendMessage('requestTraffic', {
817 ids: selectOrder,
818 hover: hoverId
819 });
820 }
821
822
Simon Hunt61d04042014-11-11 17:27:16 -0800823 // ==============================
Simon Hunta5e89142014-11-14 07:00:33 -0800824 // onos instance panel functions
825
826 function updateInstances() {
827 var onoses = oiBox.el.selectAll('.onosInst')
828 .data(onosOrder, function (d) { return d.id; });
829
830 // operate on existing onoses if necessary
831
832 var entering = onoses.enter()
833 .append('div')
834 .attr('class', 'onosInst')
835 .classed('online', function (d) { return d.online; })
Simon Hunt9c15eca2014-11-15 18:37:59 -0800836 .on('click', clickInst);
837
838 entering.each(function (d, i) {
839 var el = d3.select(this),
840 img;
841
842 $('<img src="img/host.png">').appendTo(el);
843 img = el.select('img')
844 .attr({
845 width: 40,
846 top: -10,
847 left: -10
848 })
849 .style({
850 });
851
852 $('<div>').attr('class', 'onosTitle').text(d.id).appendTo(el);
853
854 // is the UI attached to this instance?
855 // TODO: need uiAttached boolean in instance data
856 //if (d.uiAttached) {
857 if (i === 0) {
858 $('<img src="img/ui.png">').attr('class','ui').appendTo(el);
859 }
860 });
Simon Hunta5e89142014-11-14 07:00:33 -0800861
862 // operate on existing + new onoses here
863
864 // the departed...
865 var exiting = onoses.exit()
866 .transition()
867 .style('opacity', 0)
868 .remove();
869 }
870
Simon Hunt9462e8c2014-11-14 17:28:09 -0800871 function clickInst(d) {
872 var el = d3.select(this),
873 aff = el.classed('affinity');
874 if (!aff) {
875 setAffinity(el, d);
876 } else {
877 cancelAffinity();
878 }
879 }
880
881 function setAffinity(el, d) {
882 d3.selectAll('.onosInst')
883 .classed('mastership', true)
884 .classed('affinity', false);
885 el.classed('affinity', true);
886
887 suppressLayers(true);
888 node.each(function (n) {
889 if (n.master === d.id) {
890 n.el.classed('suppressed', false);
891 }
892 });
893 oiShowMaster = true;
894 }
895
896 function cancelAffinity() {
897 d3.selectAll('.onosInst')
898 .classed('mastership affinity', false);
899 restoreLayerState();
900 oiShowMaster = false;
901 }
902
Simon Hunta5e89142014-11-14 07:00:33 -0800903 // ==============================
Simon Hunt99c13842014-11-06 18:23:12 -0800904 // force layout modification functions
905
906 function translate(x, y) {
907 return 'translate(' + x + ',' + y + ')';
908 }
909
Simon Hunte2575b62014-11-18 15:25:53 -0800910 function rotate(deg) {
911 return 'rotate(' + deg + ')';
912 }
913
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800914 function missMsg(what, id) {
915 return '\n[' + what + '] "' + id + '" missing ';
916 }
917
918 function linkEndPoints(srcId, dstId) {
919 var srcNode = network.lookup[srcId],
920 dstNode = network.lookup[dstId],
921 sMiss = !srcNode ? missMsg('src', srcId) : '',
922 dMiss = !dstNode ? missMsg('dst', dstId) : '';
923
924 if (sMiss || dMiss) {
925 logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
926 return null;
927 }
928 return {
929 source: srcNode,
930 target: dstNode,
931 x1: srcNode.x,
932 y1: srcNode.y,
933 x2: dstNode.x,
934 y2: dstNode.y
935 };
936 }
937
Simon Hunt56d51852014-11-09 13:03:35 -0800938 function createHostLink(host) {
939 var src = host.id,
940 dst = host.cp.device,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800941 id = host.ingress,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800942 lnk = linkEndPoints(src, dst);
Simon Hunt56d51852014-11-09 13:03:35 -0800943
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800944 if (!lnk) {
Simon Hunt56d51852014-11-09 13:03:35 -0800945 return null;
946 }
947
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800948 // Synthesize link ...
949 $.extend(lnk, {
Simon Hunt8257f4c2014-11-16 19:34:54 -0800950 key: id,
Simon Hunt56d51852014-11-09 13:03:35 -0800951 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -0800952
953 type: function () { return 'hostLink'; },
954 // TODO: ideally, we should see if our edge switch is online...
955 online: function () { return true; },
956 linkWidth: function () { return 1; }
Simon Hunt7cd48f32014-11-09 23:42:50 -0800957 });
Simon Hunt99c13842014-11-06 18:23:12 -0800958 return lnk;
959 }
960
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800961 function createLink(link) {
962 var lnk = linkEndPoints(link.src, link.dst),
963 type = link.type;
964
965 if (!lnk) {
966 return null;
967 }
968
Simon Hunt8257f4c2014-11-16 19:34:54 -0800969 $.extend(lnk, {
970 key: link.id,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800971 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -0800972 fromSource: link,
973
974 // functions to aggregate dual link state
975 type: function () {
976 var s = lnk.fromSource,
977 t = lnk.fromTarget;
978 return (s && s.type) || (t && t.type) || defaultLinkType;
979 },
980 online: function () {
981 var s = lnk.fromSource,
982 t = lnk.fromTarget;
983 return (s && s.online) || (t && t.online);
984 },
985 linkWidth: function () {
986 var s = lnk.fromSource,
987 t = lnk.fromTarget,
988 ws = (s && s.linkWidth) || 0,
989 wt = (t && t.linkWidth) || 0;
990 return Math.max(ws, wt);
991 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800992 });
993 return lnk;
Simon Hunt1a9eff92014-11-07 11:06:34 -0800994 }
995
Simon Hunte2575b62014-11-18 15:25:53 -0800996 function removeLinkLabels() {
997 network.links.forEach(function (d) {
998 d.label = '';
999 });
1000 }
1001
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001002 var widthRatio = 1.4,
1003 linkScale = d3.scale.linear()
1004 .domain([1, 12])
1005 .range([widthRatio, 12 * widthRatio])
1006 .clamp(true);
1007
Simon Hunt99c13842014-11-06 18:23:12 -08001008 function updateLinks() {
1009 link = linkG.selectAll('.link')
Simon Hunt8257f4c2014-11-16 19:34:54 -08001010 .data(network.links, function (d) { return d.key; });
Simon Hunt99c13842014-11-06 18:23:12 -08001011
1012 // operate on existing links, if necessary
1013 // link .foo() .bar() ...
1014
1015 // operate on entering links:
1016 var entering = link.enter()
1017 .append('line')
1018 .attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001019 x1: function (d) { return d.x1; },
1020 y1: function (d) { return d.y1; },
1021 x2: function (d) { return d.x2; },
1022 y2: function (d) { return d.y2; },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001023 stroke: config.topo.linkInColor,
1024 'stroke-width': config.topo.linkInWidth
Simon Hunt99c13842014-11-06 18:23:12 -08001025 });
1026
1027 // augment links
Simon Hunt7cd48f32014-11-09 23:42:50 -08001028 entering.each(function (d) {
1029 var link = d3.select(this);
1030 // provide ref to element selection from backing data....
1031 d.el = link;
Simon Hunt8257f4c2014-11-16 19:34:54 -08001032 restyleLinkElement(d);
Simon Hunt7cd48f32014-11-09 23:42:50 -08001033 });
Thomas Vachuska4830d392014-11-09 17:09:56 -08001034
1035 // operate on both existing and new links, if necessary
1036 //link .foo() .bar() ...
1037
Simon Hunte2575b62014-11-18 15:25:53 -08001038 // apply or remove labels
1039 var labelData = getLabelData();
1040 applyLinkLabels(labelData);
1041
Thomas Vachuska4830d392014-11-09 17:09:56 -08001042 // operate on exiting links:
Thomas Vachuska4830d392014-11-09 17:09:56 -08001043 link.exit()
Simon Hunt13bf9c82014-11-18 07:26:44 -08001044 .attr('stroke-dasharray', '3, 3')
1045 .style('opacity', 0.5)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001046 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -08001047 .duration(1500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001048 .attr({
Simon Hunt13bf9c82014-11-18 07:26:44 -08001049 'stroke-dasharray': '3, 12',
1050 stroke: config.topo.linkOutColor,
1051 'stroke-width': config.topo.linkOutWidth
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001052 })
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001053 .style('opacity', 0.0)
Thomas Vachuska4830d392014-11-09 17:09:56 -08001054 .remove();
Simon Hunte2575b62014-11-18 15:25:53 -08001055
1056 // NOTE: invoke a single tick to force the labels to position
1057 // onto their links.
1058 tick();
1059 }
1060
1061 function getLabelData() {
1062 // create the backing data for showing labels..
1063 var data = [];
1064 link.each(function (d) {
1065 if (d.label) {
1066 data.push({
1067 id: 'lab-' + d.key,
1068 key: d.key,
1069 label: d.label,
1070 ldata: d
1071 });
1072 }
1073 });
1074 return data;
1075 }
1076
1077 var linkLabelOffset = '0.3em';
1078
1079 function applyLinkLabels(data) {
1080 var entering;
1081
1082 linkLabel = linkLabelG.selectAll('.linkLabel')
1083 .data(data, function (d) { return d.id; });
1084
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -08001085 linkLabel.text(function (d) { return d.label; });
1086
Simon Hunte2575b62014-11-18 15:25:53 -08001087 entering = linkLabel.enter().append('g')
1088 .classed('linkLabel', true)
1089 .attr('id', function (d) { return d.id; });
1090
1091 entering.each(function (d) {
1092 var el = d3.select(this),
1093 rect,
1094 text,
1095 parms = {
1096 x1: d.ldata.x1,
1097 y1: d.ldata.y1,
1098 x2: d.ldata.x2,
1099 y2: d.ldata.y2
1100 };
1101
1102 d.el = el;
1103 rect = el.append('rect');
1104 text = el.append('text').text(d.label);
1105 rect.attr(rectAroundText(el));
1106 text.attr('dy', linkLabelOffset);
1107
1108 el.attr('transform', transformLabel(parms));
1109 });
1110
1111 // Remove any links that are no longer required.
1112 linkLabel.exit().remove();
1113 }
1114
1115 function rectAroundText(el) {
1116 var text = el.select('text'),
1117 box = text.node().getBBox();
1118
1119 // translate the bbox so that it is centered on [x,y]
1120 box.x = -box.width / 2;
1121 box.y = -box.height / 2;
1122
1123 // add padding
1124 box.x -= 1;
1125 box.width += 2;
1126 return box;
1127 }
1128
1129 function transformLabel(p) {
1130 var dx = p.x2 - p.x1,
1131 dy = p.y2 - p.y1,
1132 xMid = dx/2 + p.x1,
1133 yMid = dy/2 + p.y1;
1134 //length = Math.sqrt(dx*dx + dy*dy),
1135 //rads = Math.asin(dy/length),
1136 //degs = rads / (Math.PI*2) * 360;
1137
1138 return translate(xMid, yMid);
1139
1140 // TODO: consider making label parallel to line
1141 //return [
1142 // translate(xMid, yMid),
1143 // rotate(degs),
1144 // translate(0, 8)
1145 //].join('');
Simon Hunt99c13842014-11-06 18:23:12 -08001146 }
1147
1148 function createDeviceNode(device) {
1149 // start with the object as is
1150 var node = device,
Simon Huntbb282f52014-11-10 11:08:19 -08001151 type = device.type,
1152 svgCls = type ? 'node device ' + type : 'node device';
Simon Hunt99c13842014-11-06 18:23:12 -08001153
1154 // Augment as needed...
1155 node.class = 'device';
Simon Huntbb282f52014-11-10 11:08:19 -08001156 node.svgClass = device.online ? svgCls + ' online' : svgCls;
Simon Hunt99c13842014-11-06 18:23:12 -08001157 positionNode(node);
1158
1159 // cache label array length
1160 network.deviceLabelCount = device.labels.length;
Simon Hunt99c13842014-11-06 18:23:12 -08001161 return node;
1162 }
1163
Simon Hunt56d51852014-11-09 13:03:35 -08001164 function createHostNode(host) {
1165 // start with the object as is
1166 var node = host;
1167
1168 // Augment as needed...
1169 node.class = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001170 if (!node.type) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08001171 node.type = 'endstation';
1172 }
Simon Hunt7fa116d2014-11-17 14:16:55 -08001173 node.svgClass = 'node host ' + node.type;
Simon Hunt56d51852014-11-09 13:03:35 -08001174 positionNode(node);
1175
1176 // cache label array length
1177 network.hostLabelCount = host.labels.length;
Simon Hunt56d51852014-11-09 13:03:35 -08001178 return node;
1179 }
1180
Simon Hunt99c13842014-11-06 18:23:12 -08001181 function positionNode(node) {
1182 var meta = node.metaUi,
Simon Huntac9e24f2014-11-12 10:12:21 -08001183 x = meta && meta.x,
1184 y = meta && meta.y,
1185 xy;
Simon Hunt99c13842014-11-06 18:23:12 -08001186
Simon Huntac9e24f2014-11-12 10:12:21 -08001187 // If we have [x,y] already, use that...
Simon Hunt99c13842014-11-06 18:23:12 -08001188 if (x && y) {
1189 node.fixed = true;
Simon Huntac9e24f2014-11-12 10:12:21 -08001190 node.x = x;
1191 node.y = y;
1192 return;
Simon Hunt99c13842014-11-06 18:23:12 -08001193 }
Simon Huntac9e24f2014-11-12 10:12:21 -08001194
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001195 var location = node.location;
1196 if (location && location.type === 'latlng') {
1197 var coord = geoMapProjection([location.lng, location.lat]);
1198 node.fixed = true;
1199 node.x = coord[0];
1200 node.y = coord[1];
1201 return;
1202 }
1203
Simon Huntac9e24f2014-11-12 10:12:21 -08001204 // Note: Placing incoming unpinned nodes at exactly the same point
1205 // (center of the view) causes them to explode outwards when
1206 // the force layout kicks in. So, we spread them out a bit
1207 // initially, to provide a more serene layout convergence.
1208 // Additionally, if the node is a host, we place it near
1209 // the device it is connected to.
1210
1211 function spread(s) {
1212 return Math.floor((Math.random() * s) - s/2);
1213 }
1214
1215 function randDim(dim) {
1216 return dim / 2 + spread(dim * 0.7071);
1217 }
1218
1219 function rand() {
1220 return {
1221 x: randDim(network.view.width()),
1222 y: randDim(network.view.height())
1223 };
1224 }
1225
1226 function near(node) {
1227 var min = 12,
1228 dx = spread(12),
1229 dy = spread(12);
1230 return {
1231 x: node.x + min + dx,
1232 y: node.y + min + dy
1233 };
1234 }
1235
1236 function getDevice(cp) {
1237 var d = network.lookup[cp.device];
1238 return d || rand();
1239 }
1240
1241 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
1242 $.extend(node, xy);
Simon Hunt99c13842014-11-06 18:23:12 -08001243 }
1244
Thomas Vachuska89543292014-11-19 11:28:33 -08001245 function iconUrl(d) {
1246 return 'img/' + d.type + '.png';
Simon Hunt99c13842014-11-06 18:23:12 -08001247 }
1248
1249 // returns the newly computed bounding box of the rectangle
1250 function adjustRectToFitText(n) {
1251 var text = n.select('text'),
1252 box = text.node().getBBox(),
1253 lab = config.labels;
1254
1255 text.attr('text-anchor', 'middle')
1256 .attr('y', '-0.8em')
1257 .attr('x', lab.imgPad/2);
1258
1259 // translate the bbox so that it is centered on [x,y]
1260 box.x = -box.width / 2;
1261 box.y = -box.height / 2;
1262
1263 // add padding
1264 box.x -= (lab.padLR + lab.imgPad/2);
1265 box.width += lab.padLR * 2 + lab.imgPad;
1266 box.y -= lab.padTB;
1267 box.height += lab.padTB * 2;
1268
1269 return box;
1270 }
1271
Simon Hunt1a9eff92014-11-07 11:06:34 -08001272 function mkSvgClass(d) {
1273 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
1274 }
1275
Simon Hunt7cd48f32014-11-09 23:42:50 -08001276 function hostLabel(d) {
1277 var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0;
1278 return d.labels[idx];
1279 }
1280 function deviceLabel(d) {
1281 var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0;
1282 return d.labels[idx];
1283 }
1284 function niceLabel(label) {
1285 return (label && label.trim()) ? label : '.';
1286 }
1287
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001288 function updateDeviceLabel(d) {
1289 var label = niceLabel(deviceLabel(d)),
1290 node = d.el,
1291 box;
1292
1293 node.select('text')
1294 .text(label)
1295 .style('opacity', 0)
1296 .transition()
1297 .style('opacity', 1);
1298
1299 box = adjustRectToFitText(node);
1300
1301 node.select('rect')
1302 .transition()
1303 .attr(box);
1304
Thomas Vachuska89543292014-11-19 11:28:33 -08001305 node.select('image')
1306 .transition()
1307 .attr('x', box.x + config.icons.xoff)
1308 .attr('y', box.y + config.icons.yoff);
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001309 }
1310
1311 function updateHostLabel(d) {
1312 var label = hostLabel(d),
1313 host = d.el;
1314
1315 host.select('text').text(label);
1316 }
1317
Simon Huntbb282f52014-11-10 11:08:19 -08001318 function updateDeviceState(nodeData) {
1319 nodeData.el.classed('online', nodeData.online);
1320 updateDeviceLabel(nodeData);
1321 // TODO: review what else might need to be updated
1322 }
1323
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001324 function updateLinkState(linkData) {
1325 updateLinkWidth(linkData);
Thomas Vachuskabadb93f2014-11-15 23:51:17 -08001326 linkData.el.classed('inactive', !linkData.online);
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001327 // TODO: review what else might need to be updated
1328 // update label, if showing
1329 }
1330
Simon Huntbb282f52014-11-10 11:08:19 -08001331 function updateHostState(hostData) {
1332 updateHostLabel(hostData);
1333 // TODO: review what else might need to be updated
1334 }
1335
Simon Hunt6ac93f32014-11-13 12:17:27 -08001336 function nodeMouseOver(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001337 hovered = d;
Simon Huntd72bc702014-11-13 18:38:04 -08001338 if (trafficHover() && d.class === 'host') {
1339 showTrafficAction();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001340 }
1341 }
1342
1343 function nodeMouseOut(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001344 hovered = null;
Simon Huntd72bc702014-11-13 18:38:04 -08001345 if (trafficHover() && d.class === 'host') {
1346 showTrafficAction();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001347 }
1348 }
Simon Huntbb282f52014-11-10 11:08:19 -08001349
Thomas Vachuska89543292014-11-19 11:28:33 -08001350 function addHostIcon(node, radius, iconId) {
1351 var dim = radius * 1.5,
1352 xlate = -dim / 2;
1353
1354 node.append('svg:image')
1355 .attr('transform', translate(xlate,xlate))
1356 .attr('xlink:href', 'img/' + iconId + '.png')
1357 .attr('width', dim)
1358 .attr('height', dim);
1359 }
1360
Simon Hunt99c13842014-11-06 18:23:12 -08001361 function updateNodes() {
1362 node = nodeG.selectAll('.node')
1363 .data(network.nodes, function (d) { return d.id; });
1364
1365 // operate on existing nodes, if necessary
Simon Hunt7cd48f32014-11-09 23:42:50 -08001366 // update host labels
Simon Hunt99c13842014-11-06 18:23:12 -08001367 //node .foo() .bar() ...
1368
1369 // operate on entering nodes:
1370 var entering = node.enter()
1371 .append('g')
1372 .attr({
1373 id: function (d) { return safeId(d.id); },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001374 class: mkSvgClass,
Simon Hunt99c13842014-11-06 18:23:12 -08001375 transform: function (d) { return translate(d.x, d.y); },
1376 opacity: 0
1377 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08001378 .call(network.drag)
Simon Hunt6ac93f32014-11-13 12:17:27 -08001379 .on('mouseover', nodeMouseOver)
1380 .on('mouseout', nodeMouseOut)
Simon Hunt99c13842014-11-06 18:23:12 -08001381 .transition()
1382 .attr('opacity', 1);
1383
1384 // augment device nodes...
1385 entering.filter('.device').each(function (d) {
1386 var node = d3.select(this),
Thomas Vachuska89543292014-11-19 11:28:33 -08001387 icon = iconUrl(d),
Simon Hunt7cd48f32014-11-09 23:42:50 -08001388 label = niceLabel(deviceLabel(d)),
Simon Hunt99c13842014-11-06 18:23:12 -08001389 box;
1390
Simon Hunt7cd48f32014-11-09 23:42:50 -08001391 // provide ref to element from backing data....
1392 d.el = node;
1393
Simon Hunt99c13842014-11-06 18:23:12 -08001394 node.append('rect')
1395 .attr({
1396 'rx': 5,
1397 'ry': 5
1398 });
1399
1400 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001401 .text(label)
Simon Hunt99c13842014-11-06 18:23:12 -08001402 .attr('dy', '1.1em');
1403
1404 box = adjustRectToFitText(node);
1405
1406 node.select('rect')
1407 .attr(box);
1408
Thomas Vachuska89543292014-11-19 11:28:33 -08001409 if (icon) {
1410 var cfg = config.icons;
1411 node.append('rect')
Simon Hunt99c13842014-11-06 18:23:12 -08001412 .attr({
Thomas Vachuska89543292014-11-19 11:28:33 -08001413 x: box.x + config.icons.xoff,
1414 y: box.y + config.icons.yoff,
1415 width: cfg.w,
1416 height: cfg.h,
1417 rx: 4
1418 }).style({
1419 stroke: '#000',
1420 fill: '#ddd'
1421 });
1422 node.append('svg:image')
1423 .attr({
1424 x: box.x + config.icons.xoff + 2,
1425 y: box.y + config.icons.yoff + 2,
1426 width: cfg.w - 4,
1427 height: cfg.h - 4,
1428 'xlink:href': icon
Simon Hunt99c13842014-11-06 18:23:12 -08001429 });
1430 }
1431
1432 // debug function to show the modelled x,y coordinates of nodes...
1433 if (debug('showNodeXY')) {
1434 node.select('rect').attr('fill-opacity', 0.5);
1435 node.append('circle')
1436 .attr({
1437 class: 'debug',
1438 cx: 0,
1439 cy: 0,
1440 r: '3px'
1441 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001442 }
1443 });
Simon Hunt934c3ce2014-11-05 11:45:07 -08001444
Thomas Vachuska89543292014-11-19 11:28:33 -08001445 // TODO: better place for this configuration state
1446 var defaultHostRadius = 9,
1447 hostRadius = {
1448 bgpSpeaker: 14,
1449 router: 14,
1450 host: 14
1451 },
1452 hostIcon = {
1453 bgpSpeaker: 'bgpSpeaker',
1454 router: 'router',
1455 host: 'host'
1456 };
1457
1458
Simon Hunt56d51852014-11-09 13:03:35 -08001459 // augment host nodes...
1460 entering.filter('.host').each(function (d) {
1461 var node = d3.select(this),
Thomas Vachuska89543292014-11-19 11:28:33 -08001462 r = hostRadius[d.type] || defaultHostRadius,
1463 textDy = r + 10,
1464 icon = hostIcon[d.type];
Simon Hunt56d51852014-11-09 13:03:35 -08001465
Simon Hunt7cd48f32014-11-09 23:42:50 -08001466 // provide ref to element from backing data....
1467 d.el = node;
1468
Thomas Vachuska89543292014-11-19 11:28:33 -08001469 node.append('circle')
1470 .attr('r', r);
Simon Hunt7fa116d2014-11-17 14:16:55 -08001471
Thomas Vachuska89543292014-11-19 11:28:33 -08001472 if (icon) {
1473 addHostIcon(node, r, icon);
Simon Hunt7fa116d2014-11-17 14:16:55 -08001474 }
Simon Hunt56d51852014-11-09 13:03:35 -08001475
Paul Greyson29cd58f2014-11-18 13:14:57 -08001476
Simon Hunt56d51852014-11-09 13:03:35 -08001477 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001478 .text(hostLabel)
Thomas Vachuska89543292014-11-19 11:28:33 -08001479 .attr('dy', textDy)
Simon Hunt7cd48f32014-11-09 23:42:50 -08001480 .attr('text-anchor', 'middle');
Simon Hunt56d51852014-11-09 13:03:35 -08001481
1482 // debug function to show the modelled x,y coordinates of nodes...
1483 if (debug('showNodeXY')) {
1484 node.select('circle').attr('fill-opacity', 0.5);
1485 node.append('circle')
1486 .attr({
1487 class: 'debug',
1488 cx: 0,
1489 cy: 0,
1490 r: '3px'
1491 });
1492 }
1493 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001494
Simon Hunt99c13842014-11-06 18:23:12 -08001495 // operate on both existing and new nodes, if necessary
1496 //node .foo() .bar() ...
Simon Huntc7ee0662014-11-05 16:44:37 -08001497
Simon Hunt99c13842014-11-06 18:23:12 -08001498 // operate on exiting nodes:
Simon Huntea80eb42014-11-11 13:46:57 -08001499 // Note that the node is removed after 2 seconds.
1500 // Sub element animations should be shorter than 2 seconds.
1501 var exiting = node.exit()
Simon Hunt44031102014-11-11 13:20:36 -08001502 .transition()
1503 .duration(2000)
Simon Huntea80eb42014-11-11 13:46:57 -08001504 .style('opacity', 0)
Simon Hunt99c13842014-11-06 18:23:12 -08001505 .remove();
Simon Huntea80eb42014-11-11 13:46:57 -08001506
1507 // host node exits....
1508 exiting.filter('.host').each(function (d) {
1509 var node = d3.select(this);
1510
1511 node.select('text')
1512 .style('opacity', 0.5)
1513 .transition()
1514 .duration(1000)
1515 .style('opacity', 0);
1516 // note, leave <g>.remove to remove this element
1517
Thomas Vachuska89543292014-11-19 11:28:33 -08001518 node.select('circle')
1519 .style('stroke-fill', '#555')
1520 .style('fill', '#888')
Simon Huntea80eb42014-11-11 13:46:57 -08001521 .style('opacity', 0.5)
1522 .transition()
1523 .duration(1500)
1524 .attr('r', 0);
1525 // note, leave <g>.remove to remove this element
1526
1527 });
1528
1529 // TODO: device node exits
Simon Huntc7ee0662014-11-05 16:44:37 -08001530 }
1531
Simon Hunt8257f4c2014-11-16 19:34:54 -08001532 function find(key, array) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001533 for (var idx = 0, n = array.length; idx < n; idx++) {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001534 if (array[idx].key === key) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001535 return idx;
1536 }
1537 }
1538 return -1;
1539 }
1540
1541 function removeLinkElement(linkData) {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001542 var idx = find(linkData.key, network.links),
1543 removed;
1544 if (idx >=0) {
1545 // remove from links array
1546 removed = network.links.splice(idx, 1);
1547 // remove from lookup cache
1548 delete network.lookup[removed[0].key];
1549 updateLinks();
1550 network.force.resume();
1551 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001552 }
Simon Huntc7ee0662014-11-05 16:44:37 -08001553
Simon Hunt44031102014-11-11 13:20:36 -08001554 function removeHostElement(hostData) {
1555 // first, remove associated hostLink...
1556 removeLinkElement(hostData.linkData);
1557
1558 // remove from lookup cache
1559 delete network.lookup[hostData.id];
1560 // remove from nodes array
1561 var idx = find(hostData.id, network.nodes);
1562 network.nodes.splice(idx, 1);
1563 // remove from SVG
1564 updateNodes();
1565 network.force.resume();
1566 }
1567
1568
Simon Huntc7ee0662014-11-05 16:44:37 -08001569 function tick() {
1570 node.attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001571 transform: function (d) { return translate(d.x, d.y); }
Simon Huntc7ee0662014-11-05 16:44:37 -08001572 });
1573
1574 link.attr({
1575 x1: function (d) { return d.source.x; },
1576 y1: function (d) { return d.source.y; },
1577 x2: function (d) { return d.target.x; },
1578 y2: function (d) { return d.target.y; }
1579 });
Simon Hunte2575b62014-11-18 15:25:53 -08001580
1581 linkLabel.each(function (d) {
1582 var el = d3.select(this);
1583 var lnk = findLinkById(d.key),
1584 parms = {
1585 x1: lnk.source.x,
1586 y1: lnk.source.y,
1587 x2: lnk.target.x,
1588 y2: lnk.target.y
1589 };
1590 el.attr('transform', transformLabel(parms));
1591 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001592 }
Simon Hunt934c3ce2014-11-05 11:45:07 -08001593
1594 // ==============================
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001595 // Web-Socket for live data
1596
1597 function webSockUrl() {
1598 return document.location.toString()
1599 .replace(/\#.*/, '')
1600 .replace('http://', 'ws://')
1601 .replace('https://', 'wss://')
1602 .replace('index2.html', config.webSockUrl);
1603 }
1604
1605 webSock = {
1606 ws : null,
1607
1608 connect : function() {
1609 webSock.ws = new WebSocket(webSockUrl());
1610
1611 webSock.ws.onopen = function() {
Simon Hunt0c6d4192014-11-12 12:07:10 -08001612 noWebSock(false);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001613 };
1614
1615 webSock.ws.onmessage = function(m) {
1616 if (m.data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001617 wsTraceRx(m.data);
Thomas Vachuskad472c6e2014-11-07 19:11:05 -08001618 handleServerEvent(JSON.parse(m.data));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001619 }
1620 };
1621
1622 webSock.ws.onclose = function(m) {
1623 webSock.ws = null;
Simon Hunt0c6d4192014-11-12 12:07:10 -08001624 noWebSock(true);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001625 };
1626 },
1627
1628 send : function(text) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001629 if (text != null) {
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001630 webSock._send(text);
1631 }
1632 },
1633
1634 _send : function(message) {
1635 if (webSock.ws) {
1636 webSock.ws.send(message);
Simon Hunta255a2c2014-11-13 22:29:35 -08001637 } else if (config.useLiveData) {
Simon Hunt56d51852014-11-09 13:03:35 -08001638 network.view.alert('no web socket open\n\n' + message);
Simon Hunta255a2c2014-11-13 22:29:35 -08001639 } else {
1640 console.log('WS Send: ' + JSON.stringify(message));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001641 }
1642 }
1643
1644 };
1645
Simon Hunt0c6d4192014-11-12 12:07:10 -08001646 function noWebSock(b) {
1647 mask.style('display',b ? 'block' : 'none');
1648 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001649
1650 function sendMessage(evType, payload) {
1651 var toSend = {
Simon Huntbb282f52014-11-10 11:08:19 -08001652 event: evType,
1653 sid: ++sid,
1654 payload: payload
1655 },
1656 asText = JSON.stringify(toSend);
1657 wsTraceTx(asText);
1658 webSock.send(asText);
Simon Huntc76ae892014-11-18 17:31:51 -08001659
1660 // Temporary measure for debugging UI behavior ...
1661 if (!config.useLiveData) {
1662 handleTestSend(toSend);
1663 }
Simon Huntbb282f52014-11-10 11:08:19 -08001664 }
1665
1666 function wsTraceTx(msg) {
1667 wsTrace('tx', msg);
1668 }
1669 function wsTraceRx(msg) {
1670 wsTrace('rx', msg);
1671 }
1672 function wsTrace(rxtx, msg) {
Simon Huntbb282f52014-11-10 11:08:19 -08001673 console.log('[' + rxtx + '] ' + msg);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001674 }
1675
Simon Huntc76ae892014-11-18 17:31:51 -08001676 // NOTE: Temporary hardcoded example for showing detail pane
1677 // while we fine-
1678 // Probably should not merge this change...
1679 function handleTestSend(msg) {
1680 if (msg.event === 'requestDetails') {
1681 showDetails({
1682 event: 'showDetails',
1683 sid: 1001,
1684 payload: {
1685 "id": "of:0000ffffffffff09",
1686 "type": "roadm",
1687 "propOrder": [
1688 "Name",
1689 "Vendor",
1690 "H/W Version",
1691 "S/W Version",
1692 "-",
1693 "Latitude",
1694 "Longitude",
1695 "Ports"
1696 ],
1697 "props": {
1698 "Name": null,
1699 "Vendor": "Linc",
1700 "H/W Version": "OE",
1701 "S/W Version": "?",
1702 "-": "",
1703 "Latitude": "40.8",
1704 "Longitude": "73.1",
1705 "Ports": "2"
1706 }
1707 }
1708 });
1709 }
1710 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001711
1712 // ==============================
1713 // Selection stuff
1714
1715 function selectObject(obj, el) {
1716 var n,
Simon Hunt01095ff2014-11-13 16:37:29 -08001717 srcEv = d3.event.sourceEvent,
1718 meta = srcEv.metaKey,
1719 shift = srcEv.shiftKey;
1720
Simon Huntdeab4322014-11-13 18:49:07 -08001721 if ((panZoom() && !meta) || (!panZoom() && meta)) {
Simon Hunt01095ff2014-11-13 16:37:29 -08001722 return;
1723 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001724
1725 if (el) {
1726 n = d3.select(el);
1727 } else {
1728 node.each(function(d) {
1729 if (d == obj) {
1730 n = d3.select(el = this);
1731 }
1732 });
1733 }
1734 if (!n) return;
1735
Simon Hunt01095ff2014-11-13 16:37:29 -08001736 if (shift && n.classed('selected')) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001737 deselectObject(obj.id);
Simon Hunt61d04042014-11-11 17:27:16 -08001738 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001739 return;
1740 }
1741
Simon Hunt01095ff2014-11-13 16:37:29 -08001742 if (!shift) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001743 deselectAll();
1744 }
1745
Simon Huntc31d5692014-11-12 13:27:18 -08001746 selections[obj.id] = { obj: obj, el: el };
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001747 selectOrder.push(obj.id);
1748
1749 n.classed('selected', true);
Simon Hunt61d04042014-11-11 17:27:16 -08001750 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001751 }
1752
1753 function deselectObject(id) {
Simon Huntc31d5692014-11-12 13:27:18 -08001754 var obj = selections[id],
1755 idx;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001756 if (obj) {
1757 d3.select(obj.el).classed('selected', false);
Simon Hunt61d04042014-11-11 17:27:16 -08001758 delete selections[id];
Simon Huntc31d5692014-11-12 13:27:18 -08001759 idx = $.inArray(id, selectOrder);
1760 if (idx >= 0) {
1761 selectOrder.splice(idx, 1);
1762 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001763 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001764 }
1765
1766 function deselectAll() {
1767 // deselect all nodes in the network...
1768 node.classed('selected', false);
1769 selections = {};
1770 selectOrder = [];
Simon Hunt61d04042014-11-11 17:27:16 -08001771 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001772 }
1773
Simon Hunt61d04042014-11-11 17:27:16 -08001774 // update the state of the detail pane, based on current selections
1775 function updateDetailPane() {
1776 var nSel = selectOrder.length;
1777 if (!nSel) {
1778 detailPane.hide();
Simon Huntd72bc702014-11-13 18:38:04 -08001779 showTrafficAction(); // sends cancelTraffic event
Simon Hunt61d04042014-11-11 17:27:16 -08001780 } else if (nSel === 1) {
1781 singleSelect();
1782 } else {
1783 multiSelect();
1784 }
1785 }
1786
1787 function singleSelect() {
1788 requestDetails();
Simon Huntb53e0682014-11-12 13:32:01 -08001789 // NOTE: detail pane will be shown from showDetails event callback
Simon Hunt61d04042014-11-11 17:27:16 -08001790 }
1791
1792 function multiSelect() {
Simon Huntb53e0682014-11-12 13:32:01 -08001793 populateMultiSelect();
Simon Huntb53e0682014-11-12 13:32:01 -08001794 }
1795
1796 function addSep(tbody) {
1797 var tr = tbody.append('tr');
1798 $('<hr>').appendTo(tr.append('td').attr('colspan', 2));
1799 }
1800
1801 function addProp(tbody, label, value) {
1802 var tr = tbody.append('tr');
1803
1804 tr.append('td')
1805 .attr('class', 'label')
1806 .text(label + ' :');
1807
1808 tr.append('td')
1809 .attr('class', 'value')
1810 .text(value);
1811 }
1812
1813 function populateMultiSelect() {
1814 detailPane.empty();
1815
1816 var title = detailPane.append("h2"),
1817 table = detailPane.append("table"),
1818 tbody = table.append("tbody");
1819
1820 title.text('Multi-Select...');
1821
1822 selectOrder.forEach(function (d, i) {
1823 addProp(tbody, i+1, d);
1824 });
Simon Huntd72bc702014-11-13 18:38:04 -08001825
1826 addMultiSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08001827 }
1828
1829 function populateDetails(data) {
1830 detailPane.empty();
1831
1832 var title = detailPane.append("h2"),
1833 table = detailPane.append("table"),
1834 tbody = table.append("tbody");
1835
1836 $('<img src="img/' + data.type + '.png">').appendTo(title);
1837 $('<span>').attr('class', 'icon').text(data.id).appendTo(title);
1838
1839 data.propOrder.forEach(function(p) {
1840 if (p === '-') {
1841 addSep(tbody);
1842 } else {
1843 addProp(tbody, p, data.props[p]);
1844 }
1845 });
Simon Huntd72bc702014-11-13 18:38:04 -08001846
1847 addSingleSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08001848 }
1849
Simon Huntd72bc702014-11-13 18:38:04 -08001850 function addSingleSelectActions() {
1851 detailPane.append('hr');
1852 // always want to allow 'show traffic'
Simon Hunta5e89142014-11-14 07:00:33 -08001853 addAction(detailPane, 'Show Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001854 }
1855
1856 function addMultiSelectActions() {
1857 detailPane.append('hr');
1858 // always want to allow 'show traffic'
Simon Hunta5e89142014-11-14 07:00:33 -08001859 addAction(detailPane, 'Show Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001860 // if exactly two hosts are selected, also want 'add host intent'
1861 if (nSel() === 2 && allSelectionsClass('host')) {
Simon Hunta5e89142014-11-14 07:00:33 -08001862 addAction(detailPane, 'Add Host Intent', addIntentAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001863 }
1864 }
1865
Simon Hunta5e89142014-11-14 07:00:33 -08001866 function addAction(panel, text, cb) {
1867 panel.append('div')
Simon Huntd72bc702014-11-13 18:38:04 -08001868 .classed('actionBtn', true)
1869 .text(text)
1870 .on('click', cb);
1871 }
1872
1873
Paul Greysonfcba0e82014-11-13 10:21:16 -08001874 function zoomPan(scale, translate) {
1875 zoomPanContainer.attr("transform", "translate(" + translate + ")scale(" + scale + ")");
1876 // keep the map lines constant width while zooming
Thomas Vachuska89543292014-11-19 11:28:33 -08001877 bgImg.style("stroke-width", 2.0 / scale + "px");
Paul Greysonfcba0e82014-11-13 10:21:16 -08001878 }
1879
1880 function resetZoomPan() {
1881 zoomPan(1, [0,0]);
1882 zoom.scale(1).translate([0,0]);
1883 }
1884
1885 function setupZoomPan() {
1886 function zoomed() {
Simon Huntdeab4322014-11-13 18:49:07 -08001887 if (!panZoom() ^ !d3.event.sourceEvent.metaKey) {
Paul Greysonfcba0e82014-11-13 10:21:16 -08001888 zoomPan(d3.event.scale, d3.event.translate);
1889 }
1890 }
1891
1892 zoom = d3.behavior.zoom()
1893 .translate([0, 0])
1894 .scale(1)
1895 .scaleExtent([1, 8])
1896 .on("zoom", zoomed);
1897
1898 svg.call(zoom);
1899 }
1900
Simon Hunt61d04042014-11-11 17:27:16 -08001901 // ==============================
1902 // Test harness code
Simon Hunt56d51852014-11-09 13:03:35 -08001903
1904 function prepareScenario(view, ctx, dbg) {
1905 var sc = scenario,
1906 urlSc = sc.evDir + ctx + sc.evScenario;
1907
1908 if (!ctx) {
1909 view.alert("No scenario specified (null ctx)");
1910 return;
1911 }
1912
1913 sc.view = view;
1914 sc.ctx = ctx;
1915 sc.debug = dbg;
1916 sc.evNumber = 0;
1917
1918 d3.json(urlSc, function(err, data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001919 var p = data && data.params || {},
1920 desc = data && data.description || null,
Simon Huntfc274c92014-11-11 11:05:46 -08001921 intro = data && data.title;
Simon Huntbb282f52014-11-10 11:08:19 -08001922
Simon Hunt56d51852014-11-09 13:03:35 -08001923 if (err) {
1924 view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err);
1925 } else {
1926 sc.params = p;
Simon Huntbb282f52014-11-10 11:08:19 -08001927 if (desc) {
1928 intro += '\n\n ' + desc.join('\n ');
1929 }
1930 view.alert(intro);
Simon Hunt56d51852014-11-09 13:03:35 -08001931 }
1932 });
1933
1934 }
1935
Simon Hunt01095ff2014-11-13 16:37:29 -08001936 // ==============================
1937 // Toggle Buttons in masthead
Simon Hunt0c6d4192014-11-12 12:07:10 -08001938
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001939 // TODO: toggle button (and other widgets in the masthead) should be provided
1940 // by the framework; not generated by the view.
1941
Simon Hunta5e89142014-11-14 07:00:33 -08001942 var showInstances,
1943 doPanZoom,
1944 showTrafficOnHover;
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001945
1946 function addButtonBar(view) {
1947 var bb = d3.select('#mast')
1948 .append('span').classed('right', true).attr('id', 'bb');
1949
Simon Hunta5e89142014-11-14 07:00:33 -08001950 function mkTogBtn(text, cb) {
1951 return bb.append('span')
1952 .classed('btn', true)
1953 .text(text)
1954 .on('click', cb);
1955 }
Simon Hunt01095ff2014-11-13 16:37:29 -08001956
Simon Hunta5e89142014-11-14 07:00:33 -08001957 showInstances = mkTogBtn('Show Instances', toggleInst);
Simon Hunte5b71752014-11-18 20:06:07 -08001958 //doPanZoom = mkTogBtn('Pan/Zoom', togglePanZoom);
1959 //showTrafficOnHover = mkTogBtn('Show traffic on hover', toggleTrafficHover);
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001960 }
1961
Simon Hunta5e89142014-11-14 07:00:33 -08001962 function instShown() {
1963 return showInstances.classed('active');
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001964 }
Simon Hunta5e89142014-11-14 07:00:33 -08001965 function toggleInst() {
1966 showInstances.classed('active', !instShown());
1967 if (instShown()) {
1968 oiBox.show();
1969 } else {
1970 oiBox.hide();
1971 }
Simon Hunt01095ff2014-11-13 16:37:29 -08001972 }
1973
Simon Huntdeab4322014-11-13 18:49:07 -08001974 function panZoom() {
Simon Hunte5b71752014-11-18 20:06:07 -08001975 return false;
1976 //return doPanZoom.classed('active');
Simon Hunt01095ff2014-11-13 16:37:29 -08001977 }
Simon Hunta5e89142014-11-14 07:00:33 -08001978 function togglePanZoom() {
1979 doPanZoom.classed('active', !panZoom());
1980 }
1981
1982 function trafficHover() {
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -08001983 return hoverEnabled;
Simon Hunta5e89142014-11-14 07:00:33 -08001984 }
1985 function toggleTrafficHover() {
1986 showTrafficOnHover.classed('active', !trafficHover());
1987 }
1988
Simon Hunt7fa116d2014-11-17 14:16:55 -08001989 function loadGlyphs(svg) {
1990 var defs = svg.append('defs');
1991 gly.defBird(defs);
1992 gly.defBullhorn(defs);
1993 }
Simon Hunt01095ff2014-11-13 16:37:29 -08001994
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001995 // ==============================
Simon Hunt142d0032014-11-04 20:13:09 -08001996 // View life-cycle callbacks
Simon Hunt195cb382014-11-03 17:50:51 -08001997
Simon Huntf67722a2014-11-10 09:32:06 -08001998 function preload(view, ctx, flags) {
Simon Hunt142d0032014-11-04 20:13:09 -08001999 var w = view.width(),
2000 h = view.height(),
Simon Huntc7ee0662014-11-05 16:44:37 -08002001 fcfg = config.force,
2002 fpad = fcfg.pad,
2003 forceDim = [w - 2*fpad, h - 2*fpad];
Simon Hunt195cb382014-11-03 17:50:51 -08002004
Simon Hunt142d0032014-11-04 20:13:09 -08002005 // NOTE: view.$div is a D3 selection of the view's div
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002006 var viewBox = '0 0 ' + config.logicalSize + ' ' + config.logicalSize;
2007 svg = view.$div.append('svg').attr('viewBox', viewBox);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002008 setSize(svg, view);
2009
Simon Hunt7fa116d2014-11-17 14:16:55 -08002010 loadGlyphs(svg);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002011
Paul Greysonfcba0e82014-11-13 10:21:16 -08002012 zoomPanContainer = svg.append('g').attr('id', 'zoomPanContainer');
Paul Greysonfcba0e82014-11-13 10:21:16 -08002013 setupZoomPan();
2014
Simon Hunt1a9eff92014-11-07 11:06:34 -08002015 // add blue glow filter to svg layer
Paul Greysonfcba0e82014-11-13 10:21:16 -08002016 d3u.appendGlow(zoomPanContainer);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002017
Simon Huntc7ee0662014-11-05 16:44:37 -08002018 // group for the topology
Paul Greysonfcba0e82014-11-13 10:21:16 -08002019 topoG = zoomPanContainer.append('g')
Simon Huntd3b7d512014-11-12 15:48:41 -08002020 .attr('id', 'topo-G')
Simon Huntc7ee0662014-11-05 16:44:37 -08002021 .attr('transform', fcfg.translate());
2022
Simon Hunte2575b62014-11-18 15:25:53 -08002023 // subgroups for links, link labels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002024 linkG = topoG.append('g').attr('id', 'links');
Simon Hunte2575b62014-11-18 15:25:53 -08002025 linkLabelG = topoG.append('g').attr('id', 'linkLabels');
Simon Huntc7ee0662014-11-05 16:44:37 -08002026 nodeG = topoG.append('g').attr('id', 'nodes');
2027
Simon Hunte2575b62014-11-18 15:25:53 -08002028 // selection of links, linkLabels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002029 link = linkG.selectAll('.link');
Simon Hunte2575b62014-11-18 15:25:53 -08002030 linkLabel = linkLabelG.selectAll('.linkLabel');
Simon Huntc7ee0662014-11-05 16:44:37 -08002031 node = nodeG.selectAll('.node');
2032
Simon Hunt7cd48f32014-11-09 23:42:50 -08002033 function chrg(d) {
2034 return fcfg.charge[d.class] || -12000;
2035 }
Simon Hunt99c13842014-11-06 18:23:12 -08002036 function ldist(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002037 return fcfg.linkDistance[d.type] || 50;
Simon Hunt99c13842014-11-06 18:23:12 -08002038 }
2039 function lstrg(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002040 // 0.0 - 1.0
2041 return fcfg.linkStrength[d.type] || 1.0;
Simon Hunt99c13842014-11-06 18:23:12 -08002042 }
2043
Simon Hunt1a9eff92014-11-07 11:06:34 -08002044 function selectCb(d, self) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002045 selectObject(d, self);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002046 }
2047
2048 function atDragEnd(d, self) {
Simon Hunt56d51852014-11-09 13:03:35 -08002049 // once we've finished moving, pin the node in position
2050 d.fixed = true;
2051 d3.select(self).classed('fixed', true);
2052 if (config.useLiveData) {
Simon Hunt902c9922014-11-11 11:59:31 -08002053 sendUpdateMeta(d);
Simon Hunta255a2c2014-11-13 22:29:35 -08002054 } else {
2055 console.log('Moving node ' + d.id + ' to [' + d.x + ',' + d.y + ']');
Simon Hunt1a9eff92014-11-07 11:06:34 -08002056 }
2057 }
2058
Simon Hunt902c9922014-11-11 11:59:31 -08002059 function sendUpdateMeta(d) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002060 sendMessage('updateMeta', {
2061 id: d.id,
2062 'class': d.class,
Simon Hunt902c9922014-11-11 11:59:31 -08002063 'memento': {
Simon Hunt01095ff2014-11-13 16:37:29 -08002064 x: d.x,
2065 y: d.y
Simon Hunt902c9922014-11-11 11:59:31 -08002066 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002067 });
2068 }
2069
Simon Huntc7ee0662014-11-05 16:44:37 -08002070 // set up the force layout
2071 network.force = d3.layout.force()
2072 .size(forceDim)
2073 .nodes(network.nodes)
2074 .links(network.links)
Simon Hunt7cd48f32014-11-09 23:42:50 -08002075 .gravity(0.4)
2076 .friction(0.7)
2077 .charge(chrg)
Simon Hunt99c13842014-11-06 18:23:12 -08002078 .linkDistance(ldist)
2079 .linkStrength(lstrg)
Simon Huntc7ee0662014-11-05 16:44:37 -08002080 .on('tick', tick);
Simon Hunt195cb382014-11-03 17:50:51 -08002081
Simon Hunt01095ff2014-11-13 16:37:29 -08002082 network.drag = d3u.createDragBehavior(network.force,
Simon Huntdeab4322014-11-13 18:49:07 -08002083 selectCb, atDragEnd, panZoom);
Simon Hunt0c6d4192014-11-12 12:07:10 -08002084
2085 // create mask layer for when we lose connection to server.
Simon Hunta5e89142014-11-14 07:00:33 -08002086 // TODO: this should be part of the framework
Simon Hunt0c6d4192014-11-12 12:07:10 -08002087 mask = view.$div.append('div').attr('id','topo-mask');
2088 para(mask, 'Oops!');
2089 para(mask, 'Web-socket connection to server closed...');
2090 para(mask, 'Try refreshing the page.');
Simon Hunt12ce12e2014-11-15 21:13:19 -08002091
2092 mask.append('svg')
2093 .attr({
2094 id: 'mask-bird',
2095 width: w,
2096 height: h
2097 })
2098 .append('g')
2099 .attr('transform', birdTranslate(w, h))
2100 .style('opacity', 0.3)
2101 .append('use')
2102 .attr({
2103 'xlink:href': '#bird',
2104 width: config.birdDim,
2105 height: config.birdDim,
2106 fill: '#111'
Thomas Vachuska89543292014-11-19 11:28:33 -08002107 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08002108 }
Simon Hunt195cb382014-11-03 17:50:51 -08002109
Simon Hunt01095ff2014-11-13 16:37:29 -08002110 function para(sel, text) {
2111 sel.append('p').text(text);
2112 }
2113
2114
Simon Hunt56d51852014-11-09 13:03:35 -08002115 function load(view, ctx, flags) {
Simon Huntf67722a2014-11-10 09:32:06 -08002116 // resize, in case the window was resized while we were not loaded
2117 resize(view, ctx, flags);
2118
Simon Hunt99c13842014-11-06 18:23:12 -08002119 // cache the view token, so network topo functions can access it
2120 network.view = view;
Simon Hunt56d51852014-11-09 13:03:35 -08002121 config.useLiveData = !flags.local;
2122
2123 if (!config.useLiveData) {
2124 prepareScenario(view, ctx, flags.debug);
2125 }
Simon Hunt99c13842014-11-06 18:23:12 -08002126
2127 // set our radio buttons and key bindings
Simon Hunt9462e8c2014-11-14 17:28:09 -08002128 layerBtnSet = view.setRadio(layerButtons);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002129 view.setKeys(keyDispatch);
Simon Hunt195cb382014-11-03 17:50:51 -08002130
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002131 // patch in our "button bar" for now
2132 // TODO: implement a more official frameworky way of doing this..
2133 addButtonBar(view);
2134
Simon Huntd3b7d512014-11-12 15:48:41 -08002135 // Load map data asynchronously; complete startup after that..
2136 loadGeoJsonData();
Simon Hunta255a2c2014-11-13 22:29:35 -08002137
2138 // start the and timer
2139 var dashIdx = 0;
2140 antTimer = setInterval(function () {
2141 // TODO: figure out how to choose Src-->Dst and Dst-->Src, per link
2142 dashIdx = dashIdx === 0 ? 14 : dashIdx - 2;
2143 d3.selectAll('.animated').style('stroke-dashoffset', dashIdx);
2144 }, 35);
2145 }
2146
2147 function unload(view, ctx, flags) {
2148 if (antTimer) {
2149 clearInterval(antTimer);
2150 antTimer = null;
2151 }
Simon Huntd3b7d512014-11-12 15:48:41 -08002152 }
2153
2154 // TODO: move these to config/state portion of script
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002155 var geoJsonUrl = 'json/map/continental_us.json', // TODO: Paul
Simon Huntd3b7d512014-11-12 15:48:41 -08002156 geoJson;
2157
2158 function loadGeoJsonData() {
2159 d3.json(geoJsonUrl, function (err, data) {
2160 if (err) {
2161 // fall back to USA map background
2162 loadStaticMap();
2163 } else {
2164 geoJson = data;
2165 loadGeoMap();
2166 }
2167
2168 // finally, connect to the server...
2169 if (config.useLiveData) {
2170 webSock.connect();
2171 }
2172 });
2173 }
2174
2175 function showBg() {
2176 return config.options.showBackground ? 'visible' : 'hidden';
2177 }
2178
2179 function loadStaticMap() {
2180 fnTrace('loadStaticMap', config.backgroundUrl);
2181 var w = network.view.width(),
2182 h = network.view.height();
2183
2184 // load the background image
2185 bgImg = svg.insert('svg:image', '#topo-G')
2186 .attr({
2187 id: 'topo-bg',
2188 width: w,
2189 height: h,
2190 'xlink:href': config.backgroundUrl
2191 })
2192 .style({
2193 visibility: showBg()
2194 });
2195 }
2196
2197 function loadGeoMap() {
2198 fnTrace('loadGeoMap', geoJsonUrl);
Simon Huntd3b7d512014-11-12 15:48:41 -08002199
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002200 // extracts the topojson data into geocoordinate-based geometry
2201 var topoData = topojson.feature(geoJson, geoJson.objects.states);
Simon Huntd3b7d512014-11-12 15:48:41 -08002202
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002203 // see: http://bl.ocks.org/mbostock/4707858
2204 geoMapProjection = d3.geo.mercator();
2205 var path = d3.geo.path().projection(geoMapProjection);
Simon Huntd3b7d512014-11-12 15:48:41 -08002206
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002207 geoMapProjection
2208 .scale(1)
2209 .translate([0, 0]);
Simon Huntd3b7d512014-11-12 15:48:41 -08002210
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002211 // [[x1,y1],[x2,y2]]
2212 var b = path.bounds(topoData);
Paul Greysonfcba0e82014-11-13 10:21:16 -08002213 // size map to 95% of minimum dimension to fill space
2214 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 -08002215 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 -08002216
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002217 geoMapProjection
2218 .scale(s)
2219 .translate(t);
2220
Paul Greysonfcba0e82014-11-13 10:21:16 -08002221 bgImg = zoomPanContainer.insert("g", '#topo-G');
Thomas Vachuska89543292014-11-19 11:28:33 -08002222 bgImg.attr('id', 'map').selectAll('path')
2223 .data(topoData.features)
2224 .enter()
2225 .append('path')
2226 .attr('d', path);
Simon Hunt195cb382014-11-03 17:50:51 -08002227 }
2228
Simon Huntf67722a2014-11-10 09:32:06 -08002229 function resize(view, ctx, flags) {
Simon Hunt12ce12e2014-11-15 21:13:19 -08002230 var w = view.width(),
2231 h = view.height();
2232
Simon Hunt934c3ce2014-11-05 11:45:07 -08002233 setSize(svg, view);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002234
2235 d3.select('#mask-bird').attr({ width: w, height: h})
2236 .select('g').attr('transform', birdTranslate(w, h));
Simon Hunt142d0032014-11-04 20:13:09 -08002237 }
2238
Simon Hunt12ce12e2014-11-15 21:13:19 -08002239 function birdTranslate(w, h) {
2240 var bdim = config.birdDim;
2241 return 'translate('+((w-bdim)*.4)+','+((h-bdim)*.1)+')';
2242 }
Simon Hunt142d0032014-11-04 20:13:09 -08002243
2244 // ==============================
2245 // View registration
Simon Hunt195cb382014-11-03 17:50:51 -08002246
Simon Hunt25248912014-11-04 11:25:48 -08002247 onos.ui.addView('topo', {
Simon Hunt142d0032014-11-04 20:13:09 -08002248 preload: preload,
2249 load: load,
Simon Hunta255a2c2014-11-13 22:29:35 -08002250 unload: unload,
Simon Hunt142d0032014-11-04 20:13:09 -08002251 resize: resize
Simon Hunt195cb382014-11-03 17:50:51 -08002252 });
2253
Simon Hunt61d04042014-11-11 17:27:16 -08002254 detailPane = onos.ui.addFloatingPanel('topo-detail');
Simon Hunta5e89142014-11-14 07:00:33 -08002255 oiBox = onos.ui.addFloatingPanel('topo-oibox', 'TL');
Simon Hunt61d04042014-11-11 17:27:16 -08002256
Simon Hunt195cb382014-11-03 17:50:51 -08002257}(ONOS));