blob: 3cd4d2efa5daa71d7658fa1426dbec87c20d9da7 [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 Hunt56a2ea42014-11-19 12:39:31 -0800646
647 function updateInstance(data) {
648 evTrace(data);
649 var inst = data.payload,
650 id = inst.id,
651 instData = onosInstances[id];
652 if (instData) {
653 $.extend(instData, inst);
654 updateInstances();
655 //updateInstanceState(instData);
656 } else {
657 logicError('updateInstance lookup fail. ID = "' + id + '"');
658 }
659 }
660
Simon Huntbb282f52014-11-10 11:08:19 -0800661 function updateDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800662 evTrace(data);
Simon Huntbb282f52014-11-10 11:08:19 -0800663 var device = data.payload,
664 id = device.id,
665 nodeData = network.lookup[id];
666 if (nodeData) {
667 $.extend(nodeData, device);
668 updateDeviceState(nodeData);
669 } else {
670 logicError('updateDevice lookup fail. ID = "' + id + '"');
671 }
672 }
673
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800674 function updateLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800675 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800676 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800677 result = findLink(link, 'update'),
678 bad = result.badLogic;
679 if (bad) {
680 logicError(bad + ': ' + link.id);
681 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800682 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800683 result.updateWith(link);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800684 }
685
Simon Hunt7cd48f32014-11-09 23:42:50 -0800686 function updateHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800687 evTrace(data);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800688 var host = data.payload,
Simon Huntbb282f52014-11-10 11:08:19 -0800689 id = host.id,
690 hostData = network.lookup[id];
691 if (hostData) {
692 $.extend(hostData, host);
693 updateHostState(hostData);
694 } else {
695 logicError('updateHost lookup fail. ID = "' + id + '"');
696 }
Simon Hunt7cd48f32014-11-09 23:42:50 -0800697 }
698
Simon Hunt44031102014-11-11 13:20:36 -0800699 // TODO: fold removeX(...) methods into base method - remove dup code
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800700 function removeLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800701 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800702 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800703 result = findLink(link, 'remove'),
704 bad = result.badLogic;
705 if (bad) {
706 logicError(bad + ': ' + link.id);
707 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800708 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800709 result.removeRawLink();
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800710 }
711
Simon Hunt44031102014-11-11 13:20:36 -0800712 function removeHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800713 evTrace(data);
Simon Hunt44031102014-11-11 13:20:36 -0800714 var host = data.payload,
715 id = host.id,
716 hostData = network.lookup[id];
717 if (hostData) {
718 removeHostElement(hostData);
719 } else {
720 logicError('removeHost lookup fail. ID = "' + id + '"');
721 }
722 }
723
Simon Hunt61d04042014-11-11 17:27:16 -0800724 function showDetails(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800725 evTrace(data);
Simon Hunt61d04042014-11-11 17:27:16 -0800726 populateDetails(data.payload);
727 detailPane.show();
728 }
729
Simon Huntb53e0682014-11-12 13:32:01 -0800730 function showTraffic(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800731 evTrace(data);
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800732 var paths = data.payload.paths;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800733
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800734 // Revert any links hilighted previously.
Simon Hunta255a2c2014-11-13 22:29:35 -0800735 link.classed('primary secondary animated optical', false);
Simon Hunte2575b62014-11-18 15:25:53 -0800736 // Remove all previous labels.
737 removeLinkLabels();
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800738
Simon Hunte2575b62014-11-18 15:25:53 -0800739 // Now hilight all links in the paths payload, and attach
740 // labels to them, if they are defined.
Simon Hunta255a2c2014-11-13 22:29:35 -0800741 paths.forEach(function (p) {
Simon Hunte2575b62014-11-18 15:25:53 -0800742 var n = p.links.length,
743 i,
744 ldata;
745
746 for (i=0; i<n; i++) {
747 ldata = findLinkById(p.links[i]);
748 if (ldata) {
749 ldata.el.classed(p.class, true);
750 ldata.label = p.labels[i];
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800751 }
Simon Hunte2575b62014-11-18 15:25:53 -0800752 }
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800753 });
Simon Hunte2575b62014-11-18 15:25:53 -0800754 updateLinks();
Simon Huntb53e0682014-11-12 13:32:01 -0800755 }
756
Simon Hunt56d51852014-11-09 13:03:35 -0800757 // ...............................
758
759 function stillToImplement(data) {
760 var p = data.payload;
761 note(data.event, p.id);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800762 network.view.alert('Not yet implemented: "' + data.event + '"');
Simon Hunt56d51852014-11-09 13:03:35 -0800763 }
Simon Hunt99c13842014-11-06 18:23:12 -0800764
765 function unknownEvent(data) {
Simon Hunt50128c02014-11-08 13:36:15 -0800766 network.view.alert('Unknown event type: "' + data.event + '"');
Simon Hunt99c13842014-11-06 18:23:12 -0800767 }
768
769 function handleServerEvent(data) {
770 var fn = eventDispatch[data.event] || unknownEvent;
771 fn(data);
772 }
773
774 // ==============================
Simon Hunt61d04042014-11-11 17:27:16 -0800775 // Out-going messages...
776
Simon Huntb53e0682014-11-12 13:32:01 -0800777 function userFeedback(msg) {
778 // for now, use the alert pane as is. Maybe different alert style in
779 // the future (centered on view; dismiss button?)
780 network.view.alert(msg);
781 }
782
783 function nSel() {
784 return selectOrder.length;
785 }
Simon Hunt61d04042014-11-11 17:27:16 -0800786 function getSel(idx) {
787 return selections[selectOrder[idx]];
788 }
Simon Huntb53e0682014-11-12 13:32:01 -0800789 function getSelId(idx) {
790 return getSel(idx).obj.id;
791 }
792 function allSelectionsClass(cls) {
793 for (var i=0, n=nSel(); i<n; i++) {
794 if (getSel(i).obj.class !== cls) {
795 return false;
796 }
797 }
798 return true;
799 }
Simon Hunt61d04042014-11-11 17:27:16 -0800800
Simon Hunt61d04042014-11-11 17:27:16 -0800801 // request details for the selected element
Simon Huntd72bc702014-11-13 18:38:04 -0800802 // invoked from selection of a single node.
Simon Hunt61d04042014-11-11 17:27:16 -0800803 function requestDetails() {
804 var data = getSel(0).obj,
805 payload = {
806 id: data.id,
807 class: data.class
808 };
809 sendMessage('requestDetails', payload);
810 }
811
Simon Huntd72bc702014-11-13 18:38:04 -0800812 function addIntentAction() {
813 sendMessage('addHostIntent', {
814 one: getSelId(0),
Thomas Vachuska82f2c622014-11-17 12:23:18 -0800815 two: getSelId(1),
816 ids: [ getSelId(0), getSelId(1) ]
Simon Huntd72bc702014-11-13 18:38:04 -0800817 });
818 }
819
820 function showTrafficAction() {
821 // if nothing is hovered over, and nothing selected, send cancel request
822 if (!hovered && nSel() === 0) {
823 sendMessage('cancelTraffic', {});
824 return;
825 }
826
827 // NOTE: hover is only populated if "show traffic on hover" is
828 // toggled on, and the item hovered is a host...
829 var hoverId = (trafficHover() && hovered && hovered.class === 'host')
830 ? hovered.id : '';
831 sendMessage('requestTraffic', {
832 ids: selectOrder,
833 hover: hoverId
834 });
835 }
836
837
Simon Hunt61d04042014-11-11 17:27:16 -0800838 // ==============================
Simon Hunta5e89142014-11-14 07:00:33 -0800839 // onos instance panel functions
840
841 function updateInstances() {
842 var onoses = oiBox.el.selectAll('.onosInst')
843 .data(onosOrder, function (d) { return d.id; });
844
845 // operate on existing onoses if necessary
846
847 var entering = onoses.enter()
848 .append('div')
849 .attr('class', 'onosInst')
850 .classed('online', function (d) { return d.online; })
Simon Hunt9c15eca2014-11-15 18:37:59 -0800851 .on('click', clickInst);
852
853 entering.each(function (d, i) {
854 var el = d3.select(this),
855 img;
856
857 $('<img src="img/host.png">').appendTo(el);
858 img = el.select('img')
859 .attr({
860 width: 40,
861 top: -10,
862 left: -10
863 })
864 .style({
865 });
866
867 $('<div>').attr('class', 'onosTitle').text(d.id).appendTo(el);
868
869 // is the UI attached to this instance?
870 // TODO: need uiAttached boolean in instance data
871 //if (d.uiAttached) {
872 if (i === 0) {
873 $('<img src="img/ui.png">').attr('class','ui').appendTo(el);
874 }
875 });
Simon Hunta5e89142014-11-14 07:00:33 -0800876
877 // operate on existing + new onoses here
878
879 // the departed...
880 var exiting = onoses.exit()
881 .transition()
882 .style('opacity', 0)
883 .remove();
884 }
885
Simon Hunt9462e8c2014-11-14 17:28:09 -0800886 function clickInst(d) {
887 var el = d3.select(this),
888 aff = el.classed('affinity');
889 if (!aff) {
890 setAffinity(el, d);
891 } else {
892 cancelAffinity();
893 }
894 }
895
896 function setAffinity(el, d) {
897 d3.selectAll('.onosInst')
898 .classed('mastership', true)
899 .classed('affinity', false);
900 el.classed('affinity', true);
901
902 suppressLayers(true);
903 node.each(function (n) {
904 if (n.master === d.id) {
905 n.el.classed('suppressed', false);
906 }
907 });
908 oiShowMaster = true;
909 }
910
911 function cancelAffinity() {
912 d3.selectAll('.onosInst')
913 .classed('mastership affinity', false);
914 restoreLayerState();
915 oiShowMaster = false;
916 }
917
Simon Hunta5e89142014-11-14 07:00:33 -0800918 // ==============================
Simon Hunt99c13842014-11-06 18:23:12 -0800919 // force layout modification functions
920
921 function translate(x, y) {
922 return 'translate(' + x + ',' + y + ')';
923 }
924
Simon Hunte2575b62014-11-18 15:25:53 -0800925 function rotate(deg) {
926 return 'rotate(' + deg + ')';
927 }
928
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800929 function missMsg(what, id) {
930 return '\n[' + what + '] "' + id + '" missing ';
931 }
932
933 function linkEndPoints(srcId, dstId) {
934 var srcNode = network.lookup[srcId],
935 dstNode = network.lookup[dstId],
936 sMiss = !srcNode ? missMsg('src', srcId) : '',
937 dMiss = !dstNode ? missMsg('dst', dstId) : '';
938
939 if (sMiss || dMiss) {
940 logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
941 return null;
942 }
943 return {
944 source: srcNode,
945 target: dstNode,
946 x1: srcNode.x,
947 y1: srcNode.y,
948 x2: dstNode.x,
949 y2: dstNode.y
950 };
951 }
952
Simon Hunt56d51852014-11-09 13:03:35 -0800953 function createHostLink(host) {
954 var src = host.id,
955 dst = host.cp.device,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800956 id = host.ingress,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800957 lnk = linkEndPoints(src, dst);
Simon Hunt56d51852014-11-09 13:03:35 -0800958
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800959 if (!lnk) {
Simon Hunt56d51852014-11-09 13:03:35 -0800960 return null;
961 }
962
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800963 // Synthesize link ...
964 $.extend(lnk, {
Simon Hunt8257f4c2014-11-16 19:34:54 -0800965 key: id,
Simon Hunt56d51852014-11-09 13:03:35 -0800966 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -0800967
968 type: function () { return 'hostLink'; },
969 // TODO: ideally, we should see if our edge switch is online...
970 online: function () { return true; },
971 linkWidth: function () { return 1; }
Simon Hunt7cd48f32014-11-09 23:42:50 -0800972 });
Simon Hunt99c13842014-11-06 18:23:12 -0800973 return lnk;
974 }
975
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800976 function createLink(link) {
977 var lnk = linkEndPoints(link.src, link.dst),
978 type = link.type;
979
980 if (!lnk) {
981 return null;
982 }
983
Simon Hunt8257f4c2014-11-16 19:34:54 -0800984 $.extend(lnk, {
985 key: link.id,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800986 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -0800987 fromSource: link,
988
989 // functions to aggregate dual link state
990 type: function () {
991 var s = lnk.fromSource,
992 t = lnk.fromTarget;
993 return (s && s.type) || (t && t.type) || defaultLinkType;
994 },
995 online: function () {
996 var s = lnk.fromSource,
997 t = lnk.fromTarget;
998 return (s && s.online) || (t && t.online);
999 },
1000 linkWidth: function () {
1001 var s = lnk.fromSource,
1002 t = lnk.fromTarget,
1003 ws = (s && s.linkWidth) || 0,
1004 wt = (t && t.linkWidth) || 0;
1005 return Math.max(ws, wt);
1006 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001007 });
1008 return lnk;
Simon Hunt1a9eff92014-11-07 11:06:34 -08001009 }
1010
Simon Hunte2575b62014-11-18 15:25:53 -08001011 function removeLinkLabels() {
1012 network.links.forEach(function (d) {
1013 d.label = '';
1014 });
1015 }
1016
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001017 var widthRatio = 1.4,
1018 linkScale = d3.scale.linear()
1019 .domain([1, 12])
1020 .range([widthRatio, 12 * widthRatio])
1021 .clamp(true);
1022
Simon Hunt99c13842014-11-06 18:23:12 -08001023 function updateLinks() {
1024 link = linkG.selectAll('.link')
Simon Hunt8257f4c2014-11-16 19:34:54 -08001025 .data(network.links, function (d) { return d.key; });
Simon Hunt99c13842014-11-06 18:23:12 -08001026
1027 // operate on existing links, if necessary
1028 // link .foo() .bar() ...
1029
1030 // operate on entering links:
1031 var entering = link.enter()
1032 .append('line')
1033 .attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001034 x1: function (d) { return d.x1; },
1035 y1: function (d) { return d.y1; },
1036 x2: function (d) { return d.x2; },
1037 y2: function (d) { return d.y2; },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001038 stroke: config.topo.linkInColor,
1039 'stroke-width': config.topo.linkInWidth
Simon Hunt99c13842014-11-06 18:23:12 -08001040 });
1041
1042 // augment links
Simon Hunt7cd48f32014-11-09 23:42:50 -08001043 entering.each(function (d) {
1044 var link = d3.select(this);
1045 // provide ref to element selection from backing data....
1046 d.el = link;
Simon Hunt8257f4c2014-11-16 19:34:54 -08001047 restyleLinkElement(d);
Simon Hunt7cd48f32014-11-09 23:42:50 -08001048 });
Thomas Vachuska4830d392014-11-09 17:09:56 -08001049
1050 // operate on both existing and new links, if necessary
1051 //link .foo() .bar() ...
1052
Simon Hunte2575b62014-11-18 15:25:53 -08001053 // apply or remove labels
1054 var labelData = getLabelData();
1055 applyLinkLabels(labelData);
1056
Thomas Vachuska4830d392014-11-09 17:09:56 -08001057 // operate on exiting links:
Thomas Vachuska4830d392014-11-09 17:09:56 -08001058 link.exit()
Simon Hunt13bf9c82014-11-18 07:26:44 -08001059 .attr('stroke-dasharray', '3, 3')
1060 .style('opacity', 0.5)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001061 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -08001062 .duration(1500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001063 .attr({
Simon Hunt13bf9c82014-11-18 07:26:44 -08001064 'stroke-dasharray': '3, 12',
1065 stroke: config.topo.linkOutColor,
1066 'stroke-width': config.topo.linkOutWidth
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001067 })
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001068 .style('opacity', 0.0)
Thomas Vachuska4830d392014-11-09 17:09:56 -08001069 .remove();
Simon Hunte2575b62014-11-18 15:25:53 -08001070
1071 // NOTE: invoke a single tick to force the labels to position
1072 // onto their links.
1073 tick();
1074 }
1075
1076 function getLabelData() {
1077 // create the backing data for showing labels..
1078 var data = [];
1079 link.each(function (d) {
1080 if (d.label) {
1081 data.push({
1082 id: 'lab-' + d.key,
1083 key: d.key,
1084 label: d.label,
1085 ldata: d
1086 });
1087 }
1088 });
1089 return data;
1090 }
1091
1092 var linkLabelOffset = '0.3em';
1093
1094 function applyLinkLabels(data) {
1095 var entering;
1096
1097 linkLabel = linkLabelG.selectAll('.linkLabel')
1098 .data(data, function (d) { return d.id; });
1099
Simon Hunt56a2ea42014-11-19 12:39:31 -08001100 // for elements already existing, we need to update the text
1101 // and adjust the rectangle size to fit
1102 linkLabel.each(function (d) {
1103 var el = d3.select(this),
1104 rect = el.select('rect'),
1105 text = el.select('text');
1106 text.text(d.label);
1107 rect.attr(rectAroundText(el));
1108 });
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -08001109
Simon Hunte2575b62014-11-18 15:25:53 -08001110 entering = linkLabel.enter().append('g')
1111 .classed('linkLabel', true)
1112 .attr('id', function (d) { return d.id; });
1113
1114 entering.each(function (d) {
1115 var el = d3.select(this),
1116 rect,
1117 text,
1118 parms = {
1119 x1: d.ldata.x1,
1120 y1: d.ldata.y1,
1121 x2: d.ldata.x2,
1122 y2: d.ldata.y2
1123 };
1124
1125 d.el = el;
1126 rect = el.append('rect');
1127 text = el.append('text').text(d.label);
1128 rect.attr(rectAroundText(el));
1129 text.attr('dy', linkLabelOffset);
1130
1131 el.attr('transform', transformLabel(parms));
1132 });
1133
1134 // Remove any links that are no longer required.
1135 linkLabel.exit().remove();
1136 }
1137
1138 function rectAroundText(el) {
1139 var text = el.select('text'),
1140 box = text.node().getBBox();
1141
1142 // translate the bbox so that it is centered on [x,y]
1143 box.x = -box.width / 2;
1144 box.y = -box.height / 2;
1145
1146 // add padding
1147 box.x -= 1;
1148 box.width += 2;
1149 return box;
1150 }
1151
1152 function transformLabel(p) {
1153 var dx = p.x2 - p.x1,
1154 dy = p.y2 - p.y1,
1155 xMid = dx/2 + p.x1,
1156 yMid = dy/2 + p.y1;
1157 //length = Math.sqrt(dx*dx + dy*dy),
1158 //rads = Math.asin(dy/length),
1159 //degs = rads / (Math.PI*2) * 360;
1160
1161 return translate(xMid, yMid);
1162
1163 // TODO: consider making label parallel to line
1164 //return [
1165 // translate(xMid, yMid),
1166 // rotate(degs),
1167 // translate(0, 8)
1168 //].join('');
Simon Hunt99c13842014-11-06 18:23:12 -08001169 }
1170
1171 function createDeviceNode(device) {
1172 // start with the object as is
1173 var node = device,
Simon Huntbb282f52014-11-10 11:08:19 -08001174 type = device.type,
1175 svgCls = type ? 'node device ' + type : 'node device';
Simon Hunt99c13842014-11-06 18:23:12 -08001176
1177 // Augment as needed...
1178 node.class = 'device';
Simon Huntbb282f52014-11-10 11:08:19 -08001179 node.svgClass = device.online ? svgCls + ' online' : svgCls;
Simon Hunt99c13842014-11-06 18:23:12 -08001180 positionNode(node);
1181
1182 // cache label array length
1183 network.deviceLabelCount = device.labels.length;
Simon Hunt99c13842014-11-06 18:23:12 -08001184 return node;
1185 }
1186
Simon Hunt56d51852014-11-09 13:03:35 -08001187 function createHostNode(host) {
1188 // start with the object as is
1189 var node = host;
1190
1191 // Augment as needed...
1192 node.class = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001193 if (!node.type) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08001194 node.type = 'endstation';
1195 }
Simon Hunt7fa116d2014-11-17 14:16:55 -08001196 node.svgClass = 'node host ' + node.type;
Simon Hunt56d51852014-11-09 13:03:35 -08001197 positionNode(node);
1198
1199 // cache label array length
1200 network.hostLabelCount = host.labels.length;
Simon Hunt56d51852014-11-09 13:03:35 -08001201 return node;
1202 }
1203
Simon Hunt99c13842014-11-06 18:23:12 -08001204 function positionNode(node) {
1205 var meta = node.metaUi,
Simon Huntac9e24f2014-11-12 10:12:21 -08001206 x = meta && meta.x,
1207 y = meta && meta.y,
1208 xy;
Simon Hunt99c13842014-11-06 18:23:12 -08001209
Simon Huntac9e24f2014-11-12 10:12:21 -08001210 // If we have [x,y] already, use that...
Simon Hunt99c13842014-11-06 18:23:12 -08001211 if (x && y) {
1212 node.fixed = true;
Simon Huntac9e24f2014-11-12 10:12:21 -08001213 node.x = x;
1214 node.y = y;
1215 return;
Simon Hunt99c13842014-11-06 18:23:12 -08001216 }
Simon Huntac9e24f2014-11-12 10:12:21 -08001217
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001218 var location = node.location;
1219 if (location && location.type === 'latlng') {
1220 var coord = geoMapProjection([location.lng, location.lat]);
1221 node.fixed = true;
1222 node.x = coord[0];
1223 node.y = coord[1];
1224 return;
1225 }
1226
Simon Huntac9e24f2014-11-12 10:12:21 -08001227 // Note: Placing incoming unpinned nodes at exactly the same point
1228 // (center of the view) causes them to explode outwards when
1229 // the force layout kicks in. So, we spread them out a bit
1230 // initially, to provide a more serene layout convergence.
1231 // Additionally, if the node is a host, we place it near
1232 // the device it is connected to.
1233
1234 function spread(s) {
1235 return Math.floor((Math.random() * s) - s/2);
1236 }
1237
1238 function randDim(dim) {
1239 return dim / 2 + spread(dim * 0.7071);
1240 }
1241
1242 function rand() {
1243 return {
1244 x: randDim(network.view.width()),
1245 y: randDim(network.view.height())
1246 };
1247 }
1248
1249 function near(node) {
1250 var min = 12,
1251 dx = spread(12),
1252 dy = spread(12);
1253 return {
1254 x: node.x + min + dx,
1255 y: node.y + min + dy
1256 };
1257 }
1258
1259 function getDevice(cp) {
1260 var d = network.lookup[cp.device];
1261 return d || rand();
1262 }
1263
1264 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
1265 $.extend(node, xy);
Simon Hunt99c13842014-11-06 18:23:12 -08001266 }
1267
Thomas Vachuska89543292014-11-19 11:28:33 -08001268 function iconUrl(d) {
1269 return 'img/' + d.type + '.png';
Simon Hunt99c13842014-11-06 18:23:12 -08001270 }
1271
1272 // returns the newly computed bounding box of the rectangle
1273 function adjustRectToFitText(n) {
1274 var text = n.select('text'),
1275 box = text.node().getBBox(),
1276 lab = config.labels;
1277
1278 text.attr('text-anchor', 'middle')
1279 .attr('y', '-0.8em')
1280 .attr('x', lab.imgPad/2);
1281
1282 // translate the bbox so that it is centered on [x,y]
1283 box.x = -box.width / 2;
1284 box.y = -box.height / 2;
1285
1286 // add padding
1287 box.x -= (lab.padLR + lab.imgPad/2);
1288 box.width += lab.padLR * 2 + lab.imgPad;
1289 box.y -= lab.padTB;
1290 box.height += lab.padTB * 2;
1291
1292 return box;
1293 }
1294
Simon Hunt1a9eff92014-11-07 11:06:34 -08001295 function mkSvgClass(d) {
1296 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
1297 }
1298
Simon Hunt7cd48f32014-11-09 23:42:50 -08001299 function hostLabel(d) {
1300 var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0;
1301 return d.labels[idx];
1302 }
1303 function deviceLabel(d) {
1304 var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0;
1305 return d.labels[idx];
1306 }
1307 function niceLabel(label) {
1308 return (label && label.trim()) ? label : '.';
1309 }
1310
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001311 function updateDeviceLabel(d) {
1312 var label = niceLabel(deviceLabel(d)),
1313 node = d.el,
1314 box;
1315
1316 node.select('text')
1317 .text(label)
1318 .style('opacity', 0)
1319 .transition()
1320 .style('opacity', 1);
1321
1322 box = adjustRectToFitText(node);
1323
1324 node.select('rect')
1325 .transition()
1326 .attr(box);
1327
Simon Hunt56a2ea42014-11-19 12:39:31 -08001328 node.select('rect.iconUnderlay')
Thomas Vachuska89543292014-11-19 11:28:33 -08001329 .transition()
1330 .attr('x', box.x + config.icons.xoff)
1331 .attr('y', box.y + config.icons.yoff);
Simon Hunt56a2ea42014-11-19 12:39:31 -08001332
1333 node.select('image')
1334 .transition()
1335 .attr('x', box.x + config.icons.xoff + 2)
1336 .attr('y', box.y + config.icons.yoff + 2);
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001337 }
1338
1339 function updateHostLabel(d) {
1340 var label = hostLabel(d),
1341 host = d.el;
1342
1343 host.select('text').text(label);
1344 }
1345
Simon Huntbb282f52014-11-10 11:08:19 -08001346 function updateDeviceState(nodeData) {
1347 nodeData.el.classed('online', nodeData.online);
1348 updateDeviceLabel(nodeData);
1349 // TODO: review what else might need to be updated
1350 }
1351
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001352 function updateLinkState(linkData) {
1353 updateLinkWidth(linkData);
Thomas Vachuskabadb93f2014-11-15 23:51:17 -08001354 linkData.el.classed('inactive', !linkData.online);
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001355 // TODO: review what else might need to be updated
1356 // update label, if showing
1357 }
1358
Simon Huntbb282f52014-11-10 11:08:19 -08001359 function updateHostState(hostData) {
1360 updateHostLabel(hostData);
1361 // TODO: review what else might need to be updated
1362 }
1363
Simon Hunt6ac93f32014-11-13 12:17:27 -08001364 function nodeMouseOver(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001365 hovered = d;
Simon Huntd72bc702014-11-13 18:38:04 -08001366 if (trafficHover() && d.class === 'host') {
1367 showTrafficAction();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001368 }
1369 }
1370
1371 function nodeMouseOut(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001372 hovered = null;
Simon Huntd72bc702014-11-13 18:38:04 -08001373 if (trafficHover() && d.class === 'host') {
1374 showTrafficAction();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001375 }
1376 }
Simon Huntbb282f52014-11-10 11:08:19 -08001377
Thomas Vachuska89543292014-11-19 11:28:33 -08001378 function addHostIcon(node, radius, iconId) {
1379 var dim = radius * 1.5,
1380 xlate = -dim / 2;
1381
1382 node.append('svg:image')
1383 .attr('transform', translate(xlate,xlate))
1384 .attr('xlink:href', 'img/' + iconId + '.png')
1385 .attr('width', dim)
1386 .attr('height', dim);
1387 }
1388
Simon Hunt99c13842014-11-06 18:23:12 -08001389 function updateNodes() {
1390 node = nodeG.selectAll('.node')
1391 .data(network.nodes, function (d) { return d.id; });
1392
1393 // operate on existing nodes, if necessary
Simon Hunt7cd48f32014-11-09 23:42:50 -08001394 // update host labels
Simon Hunt99c13842014-11-06 18:23:12 -08001395 //node .foo() .bar() ...
1396
1397 // operate on entering nodes:
1398 var entering = node.enter()
1399 .append('g')
1400 .attr({
1401 id: function (d) { return safeId(d.id); },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001402 class: mkSvgClass,
Simon Hunt99c13842014-11-06 18:23:12 -08001403 transform: function (d) { return translate(d.x, d.y); },
1404 opacity: 0
1405 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08001406 .call(network.drag)
Simon Hunt6ac93f32014-11-13 12:17:27 -08001407 .on('mouseover', nodeMouseOver)
1408 .on('mouseout', nodeMouseOut)
Simon Hunt99c13842014-11-06 18:23:12 -08001409 .transition()
1410 .attr('opacity', 1);
1411
1412 // augment device nodes...
1413 entering.filter('.device').each(function (d) {
1414 var node = d3.select(this),
Thomas Vachuska89543292014-11-19 11:28:33 -08001415 icon = iconUrl(d),
Simon Hunt7cd48f32014-11-09 23:42:50 -08001416 label = niceLabel(deviceLabel(d)),
Simon Hunt99c13842014-11-06 18:23:12 -08001417 box;
1418
Simon Hunt7cd48f32014-11-09 23:42:50 -08001419 // provide ref to element from backing data....
1420 d.el = node;
1421
Simon Hunt99c13842014-11-06 18:23:12 -08001422 node.append('rect')
1423 .attr({
1424 'rx': 5,
1425 'ry': 5
1426 });
1427
1428 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001429 .text(label)
Simon Hunt99c13842014-11-06 18:23:12 -08001430 .attr('dy', '1.1em');
1431
1432 box = adjustRectToFitText(node);
1433
1434 node.select('rect')
1435 .attr(box);
1436
Thomas Vachuska89543292014-11-19 11:28:33 -08001437 if (icon) {
1438 var cfg = config.icons;
1439 node.append('rect')
Simon Hunt99c13842014-11-06 18:23:12 -08001440 .attr({
Simon Hunt56a2ea42014-11-19 12:39:31 -08001441 class: 'iconUnderlay',
Thomas Vachuska89543292014-11-19 11:28:33 -08001442 x: box.x + config.icons.xoff,
1443 y: box.y + config.icons.yoff,
1444 width: cfg.w,
1445 height: cfg.h,
1446 rx: 4
1447 }).style({
1448 stroke: '#000',
1449 fill: '#ddd'
1450 });
1451 node.append('svg:image')
1452 .attr({
1453 x: box.x + config.icons.xoff + 2,
1454 y: box.y + config.icons.yoff + 2,
1455 width: cfg.w - 4,
1456 height: cfg.h - 4,
1457 'xlink:href': icon
Simon Hunt99c13842014-11-06 18:23:12 -08001458 });
1459 }
1460
1461 // debug function to show the modelled x,y coordinates of nodes...
1462 if (debug('showNodeXY')) {
1463 node.select('rect').attr('fill-opacity', 0.5);
1464 node.append('circle')
1465 .attr({
1466 class: 'debug',
1467 cx: 0,
1468 cy: 0,
1469 r: '3px'
1470 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001471 }
1472 });
Simon Hunt934c3ce2014-11-05 11:45:07 -08001473
Thomas Vachuska89543292014-11-19 11:28:33 -08001474 // TODO: better place for this configuration state
1475 var defaultHostRadius = 9,
1476 hostRadius = {
1477 bgpSpeaker: 14,
1478 router: 14,
1479 host: 14
1480 },
1481 hostIcon = {
1482 bgpSpeaker: 'bgpSpeaker',
1483 router: 'router',
1484 host: 'host'
1485 };
1486
1487
Simon Hunt56d51852014-11-09 13:03:35 -08001488 // augment host nodes...
1489 entering.filter('.host').each(function (d) {
1490 var node = d3.select(this),
Thomas Vachuska89543292014-11-19 11:28:33 -08001491 r = hostRadius[d.type] || defaultHostRadius,
1492 textDy = r + 10,
1493 icon = hostIcon[d.type];
Simon Hunt56d51852014-11-09 13:03:35 -08001494
Simon Hunt7cd48f32014-11-09 23:42:50 -08001495 // provide ref to element from backing data....
1496 d.el = node;
1497
Thomas Vachuska89543292014-11-19 11:28:33 -08001498 node.append('circle')
1499 .attr('r', r);
Simon Hunt7fa116d2014-11-17 14:16:55 -08001500
Thomas Vachuska89543292014-11-19 11:28:33 -08001501 if (icon) {
1502 addHostIcon(node, r, icon);
Simon Hunt7fa116d2014-11-17 14:16:55 -08001503 }
Simon Hunt56d51852014-11-09 13:03:35 -08001504
Paul Greyson29cd58f2014-11-18 13:14:57 -08001505
Simon Hunt56d51852014-11-09 13:03:35 -08001506 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001507 .text(hostLabel)
Thomas Vachuska89543292014-11-19 11:28:33 -08001508 .attr('dy', textDy)
Simon Hunt7cd48f32014-11-09 23:42:50 -08001509 .attr('text-anchor', 'middle');
Simon Hunt56d51852014-11-09 13:03:35 -08001510
1511 // debug function to show the modelled x,y coordinates of nodes...
1512 if (debug('showNodeXY')) {
1513 node.select('circle').attr('fill-opacity', 0.5);
1514 node.append('circle')
1515 .attr({
1516 class: 'debug',
1517 cx: 0,
1518 cy: 0,
1519 r: '3px'
1520 });
1521 }
1522 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001523
Simon Hunt99c13842014-11-06 18:23:12 -08001524 // operate on both existing and new nodes, if necessary
1525 //node .foo() .bar() ...
Simon Huntc7ee0662014-11-05 16:44:37 -08001526
Simon Hunt99c13842014-11-06 18:23:12 -08001527 // operate on exiting nodes:
Simon Huntea80eb42014-11-11 13:46:57 -08001528 // Note that the node is removed after 2 seconds.
1529 // Sub element animations should be shorter than 2 seconds.
1530 var exiting = node.exit()
Simon Hunt44031102014-11-11 13:20:36 -08001531 .transition()
1532 .duration(2000)
Simon Huntea80eb42014-11-11 13:46:57 -08001533 .style('opacity', 0)
Simon Hunt99c13842014-11-06 18:23:12 -08001534 .remove();
Simon Huntea80eb42014-11-11 13:46:57 -08001535
1536 // host node exits....
1537 exiting.filter('.host').each(function (d) {
1538 var node = d3.select(this);
1539
1540 node.select('text')
1541 .style('opacity', 0.5)
1542 .transition()
1543 .duration(1000)
1544 .style('opacity', 0);
1545 // note, leave <g>.remove to remove this element
1546
Thomas Vachuska89543292014-11-19 11:28:33 -08001547 node.select('circle')
1548 .style('stroke-fill', '#555')
1549 .style('fill', '#888')
Simon Huntea80eb42014-11-11 13:46:57 -08001550 .style('opacity', 0.5)
1551 .transition()
1552 .duration(1500)
1553 .attr('r', 0);
1554 // note, leave <g>.remove to remove this element
1555
1556 });
1557
1558 // TODO: device node exits
Simon Huntc7ee0662014-11-05 16:44:37 -08001559 }
1560
Simon Hunt8257f4c2014-11-16 19:34:54 -08001561 function find(key, array) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001562 for (var idx = 0, n = array.length; idx < n; idx++) {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001563 if (array[idx].key === key) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001564 return idx;
1565 }
1566 }
1567 return -1;
1568 }
1569
1570 function removeLinkElement(linkData) {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001571 var idx = find(linkData.key, network.links),
1572 removed;
1573 if (idx >=0) {
1574 // remove from links array
1575 removed = network.links.splice(idx, 1);
1576 // remove from lookup cache
1577 delete network.lookup[removed[0].key];
1578 updateLinks();
1579 network.force.resume();
1580 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001581 }
Simon Huntc7ee0662014-11-05 16:44:37 -08001582
Simon Hunt44031102014-11-11 13:20:36 -08001583 function removeHostElement(hostData) {
1584 // first, remove associated hostLink...
1585 removeLinkElement(hostData.linkData);
1586
1587 // remove from lookup cache
1588 delete network.lookup[hostData.id];
1589 // remove from nodes array
1590 var idx = find(hostData.id, network.nodes);
1591 network.nodes.splice(idx, 1);
1592 // remove from SVG
1593 updateNodes();
1594 network.force.resume();
1595 }
1596
1597
Simon Huntc7ee0662014-11-05 16:44:37 -08001598 function tick() {
1599 node.attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001600 transform: function (d) { return translate(d.x, d.y); }
Simon Huntc7ee0662014-11-05 16:44:37 -08001601 });
1602
1603 link.attr({
1604 x1: function (d) { return d.source.x; },
1605 y1: function (d) { return d.source.y; },
1606 x2: function (d) { return d.target.x; },
1607 y2: function (d) { return d.target.y; }
1608 });
Simon Hunte2575b62014-11-18 15:25:53 -08001609
1610 linkLabel.each(function (d) {
1611 var el = d3.select(this);
1612 var lnk = findLinkById(d.key),
1613 parms = {
1614 x1: lnk.source.x,
1615 y1: lnk.source.y,
1616 x2: lnk.target.x,
1617 y2: lnk.target.y
1618 };
1619 el.attr('transform', transformLabel(parms));
1620 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001621 }
Simon Hunt934c3ce2014-11-05 11:45:07 -08001622
1623 // ==============================
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001624 // Web-Socket for live data
1625
1626 function webSockUrl() {
1627 return document.location.toString()
1628 .replace(/\#.*/, '')
1629 .replace('http://', 'ws://')
1630 .replace('https://', 'wss://')
1631 .replace('index2.html', config.webSockUrl);
1632 }
1633
1634 webSock = {
1635 ws : null,
1636
1637 connect : function() {
1638 webSock.ws = new WebSocket(webSockUrl());
1639
1640 webSock.ws.onopen = function() {
Simon Hunt0c6d4192014-11-12 12:07:10 -08001641 noWebSock(false);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001642 };
1643
1644 webSock.ws.onmessage = function(m) {
1645 if (m.data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001646 wsTraceRx(m.data);
Thomas Vachuskad472c6e2014-11-07 19:11:05 -08001647 handleServerEvent(JSON.parse(m.data));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001648 }
1649 };
1650
1651 webSock.ws.onclose = function(m) {
1652 webSock.ws = null;
Simon Hunt0c6d4192014-11-12 12:07:10 -08001653 noWebSock(true);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001654 };
1655 },
1656
1657 send : function(text) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001658 if (text != null) {
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001659 webSock._send(text);
1660 }
1661 },
1662
1663 _send : function(message) {
1664 if (webSock.ws) {
1665 webSock.ws.send(message);
Simon Hunta255a2c2014-11-13 22:29:35 -08001666 } else if (config.useLiveData) {
Simon Hunt56d51852014-11-09 13:03:35 -08001667 network.view.alert('no web socket open\n\n' + message);
Simon Hunta255a2c2014-11-13 22:29:35 -08001668 } else {
1669 console.log('WS Send: ' + JSON.stringify(message));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001670 }
1671 }
1672
1673 };
1674
Simon Hunt0c6d4192014-11-12 12:07:10 -08001675 function noWebSock(b) {
1676 mask.style('display',b ? 'block' : 'none');
1677 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001678
1679 function sendMessage(evType, payload) {
1680 var toSend = {
Simon Huntbb282f52014-11-10 11:08:19 -08001681 event: evType,
1682 sid: ++sid,
1683 payload: payload
1684 },
1685 asText = JSON.stringify(toSend);
1686 wsTraceTx(asText);
1687 webSock.send(asText);
Simon Huntc76ae892014-11-18 17:31:51 -08001688
1689 // Temporary measure for debugging UI behavior ...
1690 if (!config.useLiveData) {
1691 handleTestSend(toSend);
1692 }
Simon Huntbb282f52014-11-10 11:08:19 -08001693 }
1694
1695 function wsTraceTx(msg) {
1696 wsTrace('tx', msg);
1697 }
1698 function wsTraceRx(msg) {
1699 wsTrace('rx', msg);
1700 }
1701 function wsTrace(rxtx, msg) {
Simon Huntbb282f52014-11-10 11:08:19 -08001702 console.log('[' + rxtx + '] ' + msg);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001703 }
1704
Simon Huntc76ae892014-11-18 17:31:51 -08001705 // NOTE: Temporary hardcoded example for showing detail pane
1706 // while we fine-
1707 // Probably should not merge this change...
1708 function handleTestSend(msg) {
1709 if (msg.event === 'requestDetails') {
1710 showDetails({
1711 event: 'showDetails',
1712 sid: 1001,
1713 payload: {
1714 "id": "of:0000ffffffffff09",
1715 "type": "roadm",
1716 "propOrder": [
1717 "Name",
1718 "Vendor",
1719 "H/W Version",
1720 "S/W Version",
1721 "-",
1722 "Latitude",
1723 "Longitude",
1724 "Ports"
1725 ],
1726 "props": {
1727 "Name": null,
1728 "Vendor": "Linc",
1729 "H/W Version": "OE",
1730 "S/W Version": "?",
1731 "-": "",
1732 "Latitude": "40.8",
1733 "Longitude": "73.1",
1734 "Ports": "2"
1735 }
1736 }
1737 });
1738 }
1739 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001740
1741 // ==============================
1742 // Selection stuff
1743
1744 function selectObject(obj, el) {
1745 var n,
Simon Hunt01095ff2014-11-13 16:37:29 -08001746 srcEv = d3.event.sourceEvent,
1747 meta = srcEv.metaKey,
1748 shift = srcEv.shiftKey;
1749
Simon Huntdeab4322014-11-13 18:49:07 -08001750 if ((panZoom() && !meta) || (!panZoom() && meta)) {
Simon Hunt01095ff2014-11-13 16:37:29 -08001751 return;
1752 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001753
1754 if (el) {
1755 n = d3.select(el);
1756 } else {
1757 node.each(function(d) {
1758 if (d == obj) {
1759 n = d3.select(el = this);
1760 }
1761 });
1762 }
1763 if (!n) return;
1764
Simon Hunt01095ff2014-11-13 16:37:29 -08001765 if (shift && n.classed('selected')) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001766 deselectObject(obj.id);
Simon Hunt61d04042014-11-11 17:27:16 -08001767 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001768 return;
1769 }
1770
Simon Hunt01095ff2014-11-13 16:37:29 -08001771 if (!shift) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001772 deselectAll();
1773 }
1774
Simon Huntc31d5692014-11-12 13:27:18 -08001775 selections[obj.id] = { obj: obj, el: el };
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001776 selectOrder.push(obj.id);
1777
1778 n.classed('selected', true);
Simon Hunt61d04042014-11-11 17:27:16 -08001779 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001780 }
1781
1782 function deselectObject(id) {
Simon Huntc31d5692014-11-12 13:27:18 -08001783 var obj = selections[id],
1784 idx;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001785 if (obj) {
1786 d3.select(obj.el).classed('selected', false);
Simon Hunt61d04042014-11-11 17:27:16 -08001787 delete selections[id];
Simon Huntc31d5692014-11-12 13:27:18 -08001788 idx = $.inArray(id, selectOrder);
1789 if (idx >= 0) {
1790 selectOrder.splice(idx, 1);
1791 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001792 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001793 }
1794
1795 function deselectAll() {
1796 // deselect all nodes in the network...
1797 node.classed('selected', false);
1798 selections = {};
1799 selectOrder = [];
Simon Hunt61d04042014-11-11 17:27:16 -08001800 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001801 }
1802
Simon Hunt61d04042014-11-11 17:27:16 -08001803 // update the state of the detail pane, based on current selections
1804 function updateDetailPane() {
1805 var nSel = selectOrder.length;
1806 if (!nSel) {
1807 detailPane.hide();
Simon Huntd72bc702014-11-13 18:38:04 -08001808 showTrafficAction(); // sends cancelTraffic event
Simon Hunt61d04042014-11-11 17:27:16 -08001809 } else if (nSel === 1) {
1810 singleSelect();
1811 } else {
1812 multiSelect();
1813 }
1814 }
1815
1816 function singleSelect() {
1817 requestDetails();
Simon Huntb53e0682014-11-12 13:32:01 -08001818 // NOTE: detail pane will be shown from showDetails event callback
Simon Hunt61d04042014-11-11 17:27:16 -08001819 }
1820
1821 function multiSelect() {
Simon Huntb53e0682014-11-12 13:32:01 -08001822 populateMultiSelect();
Simon Huntb53e0682014-11-12 13:32:01 -08001823 }
1824
1825 function addSep(tbody) {
1826 var tr = tbody.append('tr');
1827 $('<hr>').appendTo(tr.append('td').attr('colspan', 2));
1828 }
1829
1830 function addProp(tbody, label, value) {
1831 var tr = tbody.append('tr');
1832
1833 tr.append('td')
1834 .attr('class', 'label')
1835 .text(label + ' :');
1836
1837 tr.append('td')
1838 .attr('class', 'value')
1839 .text(value);
1840 }
1841
1842 function populateMultiSelect() {
1843 detailPane.empty();
1844
1845 var title = detailPane.append("h2"),
1846 table = detailPane.append("table"),
1847 tbody = table.append("tbody");
1848
1849 title.text('Multi-Select...');
1850
1851 selectOrder.forEach(function (d, i) {
1852 addProp(tbody, i+1, d);
1853 });
Simon Huntd72bc702014-11-13 18:38:04 -08001854
1855 addMultiSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08001856 }
1857
1858 function populateDetails(data) {
1859 detailPane.empty();
1860
1861 var title = detailPane.append("h2"),
1862 table = detailPane.append("table"),
1863 tbody = table.append("tbody");
1864
1865 $('<img src="img/' + data.type + '.png">').appendTo(title);
1866 $('<span>').attr('class', 'icon').text(data.id).appendTo(title);
1867
1868 data.propOrder.forEach(function(p) {
1869 if (p === '-') {
1870 addSep(tbody);
1871 } else {
1872 addProp(tbody, p, data.props[p]);
1873 }
1874 });
Simon Huntd72bc702014-11-13 18:38:04 -08001875
1876 addSingleSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08001877 }
1878
Simon Huntd72bc702014-11-13 18:38:04 -08001879 function addSingleSelectActions() {
1880 detailPane.append('hr');
1881 // always want to allow 'show traffic'
Simon Hunta5e89142014-11-14 07:00:33 -08001882 addAction(detailPane, 'Show Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001883 }
1884
1885 function addMultiSelectActions() {
1886 detailPane.append('hr');
1887 // always want to allow 'show traffic'
Simon Hunta5e89142014-11-14 07:00:33 -08001888 addAction(detailPane, 'Show Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001889 // if exactly two hosts are selected, also want 'add host intent'
1890 if (nSel() === 2 && allSelectionsClass('host')) {
Simon Hunta5e89142014-11-14 07:00:33 -08001891 addAction(detailPane, 'Add Host Intent', addIntentAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001892 }
1893 }
1894
Simon Hunta5e89142014-11-14 07:00:33 -08001895 function addAction(panel, text, cb) {
1896 panel.append('div')
Simon Huntd72bc702014-11-13 18:38:04 -08001897 .classed('actionBtn', true)
1898 .text(text)
1899 .on('click', cb);
1900 }
1901
1902
Paul Greysonfcba0e82014-11-13 10:21:16 -08001903 function zoomPan(scale, translate) {
1904 zoomPanContainer.attr("transform", "translate(" + translate + ")scale(" + scale + ")");
1905 // keep the map lines constant width while zooming
Thomas Vachuska89543292014-11-19 11:28:33 -08001906 bgImg.style("stroke-width", 2.0 / scale + "px");
Paul Greysonfcba0e82014-11-13 10:21:16 -08001907 }
1908
1909 function resetZoomPan() {
1910 zoomPan(1, [0,0]);
1911 zoom.scale(1).translate([0,0]);
1912 }
1913
1914 function setupZoomPan() {
1915 function zoomed() {
Simon Huntdeab4322014-11-13 18:49:07 -08001916 if (!panZoom() ^ !d3.event.sourceEvent.metaKey) {
Paul Greysonfcba0e82014-11-13 10:21:16 -08001917 zoomPan(d3.event.scale, d3.event.translate);
1918 }
1919 }
1920
1921 zoom = d3.behavior.zoom()
1922 .translate([0, 0])
1923 .scale(1)
1924 .scaleExtent([1, 8])
1925 .on("zoom", zoomed);
1926
1927 svg.call(zoom);
1928 }
1929
Simon Hunt61d04042014-11-11 17:27:16 -08001930 // ==============================
1931 // Test harness code
Simon Hunt56d51852014-11-09 13:03:35 -08001932
1933 function prepareScenario(view, ctx, dbg) {
1934 var sc = scenario,
1935 urlSc = sc.evDir + ctx + sc.evScenario;
1936
1937 if (!ctx) {
1938 view.alert("No scenario specified (null ctx)");
1939 return;
1940 }
1941
1942 sc.view = view;
1943 sc.ctx = ctx;
1944 sc.debug = dbg;
1945 sc.evNumber = 0;
1946
1947 d3.json(urlSc, function(err, data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001948 var p = data && data.params || {},
1949 desc = data && data.description || null,
Simon Huntfc274c92014-11-11 11:05:46 -08001950 intro = data && data.title;
Simon Huntbb282f52014-11-10 11:08:19 -08001951
Simon Hunt56d51852014-11-09 13:03:35 -08001952 if (err) {
1953 view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err);
1954 } else {
1955 sc.params = p;
Simon Huntbb282f52014-11-10 11:08:19 -08001956 if (desc) {
1957 intro += '\n\n ' + desc.join('\n ');
1958 }
1959 view.alert(intro);
Simon Hunt56d51852014-11-09 13:03:35 -08001960 }
1961 });
1962
1963 }
1964
Simon Hunt01095ff2014-11-13 16:37:29 -08001965 // ==============================
1966 // Toggle Buttons in masthead
Simon Hunt0c6d4192014-11-12 12:07:10 -08001967
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001968 // TODO: toggle button (and other widgets in the masthead) should be provided
1969 // by the framework; not generated by the view.
1970
Simon Hunta5e89142014-11-14 07:00:33 -08001971 var showInstances,
1972 doPanZoom,
1973 showTrafficOnHover;
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001974
1975 function addButtonBar(view) {
1976 var bb = d3.select('#mast')
1977 .append('span').classed('right', true).attr('id', 'bb');
1978
Simon Hunta5e89142014-11-14 07:00:33 -08001979 function mkTogBtn(text, cb) {
1980 return bb.append('span')
1981 .classed('btn', true)
1982 .text(text)
1983 .on('click', cb);
1984 }
Simon Hunt01095ff2014-11-13 16:37:29 -08001985
Simon Hunta5e89142014-11-14 07:00:33 -08001986 showInstances = mkTogBtn('Show Instances', toggleInst);
Simon Hunte5b71752014-11-18 20:06:07 -08001987 //doPanZoom = mkTogBtn('Pan/Zoom', togglePanZoom);
1988 //showTrafficOnHover = mkTogBtn('Show traffic on hover', toggleTrafficHover);
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001989 }
1990
Simon Hunta5e89142014-11-14 07:00:33 -08001991 function instShown() {
1992 return showInstances.classed('active');
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001993 }
Simon Hunta5e89142014-11-14 07:00:33 -08001994 function toggleInst() {
1995 showInstances.classed('active', !instShown());
1996 if (instShown()) {
1997 oiBox.show();
1998 } else {
1999 oiBox.hide();
2000 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002001 }
2002
Simon Huntdeab4322014-11-13 18:49:07 -08002003 function panZoom() {
Simon Hunte5b71752014-11-18 20:06:07 -08002004 return false;
2005 //return doPanZoom.classed('active');
Simon Hunt01095ff2014-11-13 16:37:29 -08002006 }
Simon Hunta5e89142014-11-14 07:00:33 -08002007 function togglePanZoom() {
2008 doPanZoom.classed('active', !panZoom());
2009 }
2010
2011 function trafficHover() {
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -08002012 return hoverEnabled;
Simon Hunta5e89142014-11-14 07:00:33 -08002013 }
2014 function toggleTrafficHover() {
2015 showTrafficOnHover.classed('active', !trafficHover());
2016 }
2017
Simon Hunt7fa116d2014-11-17 14:16:55 -08002018 function loadGlyphs(svg) {
2019 var defs = svg.append('defs');
2020 gly.defBird(defs);
2021 gly.defBullhorn(defs);
2022 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002023
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002024 // ==============================
Simon Hunt142d0032014-11-04 20:13:09 -08002025 // View life-cycle callbacks
Simon Hunt195cb382014-11-03 17:50:51 -08002026
Simon Huntf67722a2014-11-10 09:32:06 -08002027 function preload(view, ctx, flags) {
Simon Hunt142d0032014-11-04 20:13:09 -08002028 var w = view.width(),
2029 h = view.height(),
Simon Huntc7ee0662014-11-05 16:44:37 -08002030 fcfg = config.force,
2031 fpad = fcfg.pad,
2032 forceDim = [w - 2*fpad, h - 2*fpad];
Simon Hunt195cb382014-11-03 17:50:51 -08002033
Simon Hunt142d0032014-11-04 20:13:09 -08002034 // NOTE: view.$div is a D3 selection of the view's div
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002035 var viewBox = '0 0 ' + config.logicalSize + ' ' + config.logicalSize;
2036 svg = view.$div.append('svg').attr('viewBox', viewBox);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002037 setSize(svg, view);
2038
Simon Hunt7fa116d2014-11-17 14:16:55 -08002039 loadGlyphs(svg);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002040
Paul Greysonfcba0e82014-11-13 10:21:16 -08002041 zoomPanContainer = svg.append('g').attr('id', 'zoomPanContainer');
Paul Greysonfcba0e82014-11-13 10:21:16 -08002042 setupZoomPan();
2043
Simon Hunt1a9eff92014-11-07 11:06:34 -08002044 // add blue glow filter to svg layer
Paul Greysonfcba0e82014-11-13 10:21:16 -08002045 d3u.appendGlow(zoomPanContainer);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002046
Simon Huntc7ee0662014-11-05 16:44:37 -08002047 // group for the topology
Paul Greysonfcba0e82014-11-13 10:21:16 -08002048 topoG = zoomPanContainer.append('g')
Simon Huntd3b7d512014-11-12 15:48:41 -08002049 .attr('id', 'topo-G')
Simon Huntc7ee0662014-11-05 16:44:37 -08002050 .attr('transform', fcfg.translate());
2051
Simon Hunte2575b62014-11-18 15:25:53 -08002052 // subgroups for links, link labels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002053 linkG = topoG.append('g').attr('id', 'links');
Simon Hunte2575b62014-11-18 15:25:53 -08002054 linkLabelG = topoG.append('g').attr('id', 'linkLabels');
Simon Huntc7ee0662014-11-05 16:44:37 -08002055 nodeG = topoG.append('g').attr('id', 'nodes');
2056
Simon Hunte2575b62014-11-18 15:25:53 -08002057 // selection of links, linkLabels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002058 link = linkG.selectAll('.link');
Simon Hunte2575b62014-11-18 15:25:53 -08002059 linkLabel = linkLabelG.selectAll('.linkLabel');
Simon Huntc7ee0662014-11-05 16:44:37 -08002060 node = nodeG.selectAll('.node');
2061
Simon Hunt7cd48f32014-11-09 23:42:50 -08002062 function chrg(d) {
2063 return fcfg.charge[d.class] || -12000;
2064 }
Simon Hunt99c13842014-11-06 18:23:12 -08002065 function ldist(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002066 return fcfg.linkDistance[d.type] || 50;
Simon Hunt99c13842014-11-06 18:23:12 -08002067 }
2068 function lstrg(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002069 // 0.0 - 1.0
2070 return fcfg.linkStrength[d.type] || 1.0;
Simon Hunt99c13842014-11-06 18:23:12 -08002071 }
2072
Simon Hunt1a9eff92014-11-07 11:06:34 -08002073 function selectCb(d, self) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002074 selectObject(d, self);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002075 }
2076
2077 function atDragEnd(d, self) {
Simon Hunt56d51852014-11-09 13:03:35 -08002078 // once we've finished moving, pin the node in position
2079 d.fixed = true;
2080 d3.select(self).classed('fixed', true);
2081 if (config.useLiveData) {
Simon Hunt902c9922014-11-11 11:59:31 -08002082 sendUpdateMeta(d);
Simon Hunta255a2c2014-11-13 22:29:35 -08002083 } else {
2084 console.log('Moving node ' + d.id + ' to [' + d.x + ',' + d.y + ']');
Simon Hunt1a9eff92014-11-07 11:06:34 -08002085 }
2086 }
2087
Simon Hunt902c9922014-11-11 11:59:31 -08002088 function sendUpdateMeta(d) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002089 sendMessage('updateMeta', {
2090 id: d.id,
2091 'class': d.class,
Simon Hunt902c9922014-11-11 11:59:31 -08002092 'memento': {
Simon Hunt01095ff2014-11-13 16:37:29 -08002093 x: d.x,
2094 y: d.y
Simon Hunt902c9922014-11-11 11:59:31 -08002095 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002096 });
2097 }
2098
Simon Huntc7ee0662014-11-05 16:44:37 -08002099 // set up the force layout
2100 network.force = d3.layout.force()
2101 .size(forceDim)
2102 .nodes(network.nodes)
2103 .links(network.links)
Simon Hunt7cd48f32014-11-09 23:42:50 -08002104 .gravity(0.4)
2105 .friction(0.7)
2106 .charge(chrg)
Simon Hunt99c13842014-11-06 18:23:12 -08002107 .linkDistance(ldist)
2108 .linkStrength(lstrg)
Simon Huntc7ee0662014-11-05 16:44:37 -08002109 .on('tick', tick);
Simon Hunt195cb382014-11-03 17:50:51 -08002110
Simon Hunt01095ff2014-11-13 16:37:29 -08002111 network.drag = d3u.createDragBehavior(network.force,
Simon Huntdeab4322014-11-13 18:49:07 -08002112 selectCb, atDragEnd, panZoom);
Simon Hunt0c6d4192014-11-12 12:07:10 -08002113
2114 // create mask layer for when we lose connection to server.
Simon Hunta5e89142014-11-14 07:00:33 -08002115 // TODO: this should be part of the framework
Simon Hunt0c6d4192014-11-12 12:07:10 -08002116 mask = view.$div.append('div').attr('id','topo-mask');
2117 para(mask, 'Oops!');
2118 para(mask, 'Web-socket connection to server closed...');
2119 para(mask, 'Try refreshing the page.');
Simon Hunt12ce12e2014-11-15 21:13:19 -08002120
2121 mask.append('svg')
2122 .attr({
2123 id: 'mask-bird',
2124 width: w,
2125 height: h
2126 })
2127 .append('g')
2128 .attr('transform', birdTranslate(w, h))
2129 .style('opacity', 0.3)
2130 .append('use')
2131 .attr({
2132 'xlink:href': '#bird',
2133 width: config.birdDim,
2134 height: config.birdDim,
2135 fill: '#111'
Thomas Vachuska89543292014-11-19 11:28:33 -08002136 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08002137 }
Simon Hunt195cb382014-11-03 17:50:51 -08002138
Simon Hunt01095ff2014-11-13 16:37:29 -08002139 function para(sel, text) {
2140 sel.append('p').text(text);
2141 }
2142
2143
Simon Hunt56d51852014-11-09 13:03:35 -08002144 function load(view, ctx, flags) {
Simon Huntf67722a2014-11-10 09:32:06 -08002145 // resize, in case the window was resized while we were not loaded
2146 resize(view, ctx, flags);
2147
Simon Hunt99c13842014-11-06 18:23:12 -08002148 // cache the view token, so network topo functions can access it
2149 network.view = view;
Simon Hunt56d51852014-11-09 13:03:35 -08002150 config.useLiveData = !flags.local;
2151
2152 if (!config.useLiveData) {
2153 prepareScenario(view, ctx, flags.debug);
2154 }
Simon Hunt99c13842014-11-06 18:23:12 -08002155
2156 // set our radio buttons and key bindings
Simon Hunt9462e8c2014-11-14 17:28:09 -08002157 layerBtnSet = view.setRadio(layerButtons);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002158 view.setKeys(keyDispatch);
Simon Hunt195cb382014-11-03 17:50:51 -08002159
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002160 // patch in our "button bar" for now
2161 // TODO: implement a more official frameworky way of doing this..
2162 addButtonBar(view);
2163
Simon Huntd3b7d512014-11-12 15:48:41 -08002164 // Load map data asynchronously; complete startup after that..
2165 loadGeoJsonData();
Simon Hunta255a2c2014-11-13 22:29:35 -08002166
2167 // start the and timer
2168 var dashIdx = 0;
2169 antTimer = setInterval(function () {
2170 // TODO: figure out how to choose Src-->Dst and Dst-->Src, per link
2171 dashIdx = dashIdx === 0 ? 14 : dashIdx - 2;
2172 d3.selectAll('.animated').style('stroke-dashoffset', dashIdx);
2173 }, 35);
2174 }
2175
2176 function unload(view, ctx, flags) {
2177 if (antTimer) {
2178 clearInterval(antTimer);
2179 antTimer = null;
2180 }
Simon Huntd3b7d512014-11-12 15:48:41 -08002181 }
2182
2183 // TODO: move these to config/state portion of script
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002184 var geoJsonUrl = 'json/map/continental_us.json', // TODO: Paul
Simon Huntd3b7d512014-11-12 15:48:41 -08002185 geoJson;
2186
2187 function loadGeoJsonData() {
2188 d3.json(geoJsonUrl, function (err, data) {
2189 if (err) {
2190 // fall back to USA map background
2191 loadStaticMap();
2192 } else {
2193 geoJson = data;
2194 loadGeoMap();
2195 }
2196
2197 // finally, connect to the server...
2198 if (config.useLiveData) {
2199 webSock.connect();
2200 }
2201 });
2202 }
2203
2204 function showBg() {
2205 return config.options.showBackground ? 'visible' : 'hidden';
2206 }
2207
2208 function loadStaticMap() {
2209 fnTrace('loadStaticMap', config.backgroundUrl);
2210 var w = network.view.width(),
2211 h = network.view.height();
2212
2213 // load the background image
2214 bgImg = svg.insert('svg:image', '#topo-G')
2215 .attr({
2216 id: 'topo-bg',
2217 width: w,
2218 height: h,
2219 'xlink:href': config.backgroundUrl
2220 })
2221 .style({
2222 visibility: showBg()
2223 });
2224 }
2225
2226 function loadGeoMap() {
2227 fnTrace('loadGeoMap', geoJsonUrl);
Simon Huntd3b7d512014-11-12 15:48:41 -08002228
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002229 // extracts the topojson data into geocoordinate-based geometry
2230 var topoData = topojson.feature(geoJson, geoJson.objects.states);
Simon Huntd3b7d512014-11-12 15:48:41 -08002231
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002232 // see: http://bl.ocks.org/mbostock/4707858
2233 geoMapProjection = d3.geo.mercator();
2234 var path = d3.geo.path().projection(geoMapProjection);
Simon Huntd3b7d512014-11-12 15:48:41 -08002235
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002236 geoMapProjection
2237 .scale(1)
2238 .translate([0, 0]);
Simon Huntd3b7d512014-11-12 15:48:41 -08002239
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002240 // [[x1,y1],[x2,y2]]
2241 var b = path.bounds(topoData);
Paul Greysonfcba0e82014-11-13 10:21:16 -08002242 // size map to 95% of minimum dimension to fill space
2243 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 -08002244 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 -08002245
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002246 geoMapProjection
2247 .scale(s)
2248 .translate(t);
2249
Paul Greysonfcba0e82014-11-13 10:21:16 -08002250 bgImg = zoomPanContainer.insert("g", '#topo-G');
Thomas Vachuska89543292014-11-19 11:28:33 -08002251 bgImg.attr('id', 'map').selectAll('path')
2252 .data(topoData.features)
2253 .enter()
2254 .append('path')
2255 .attr('d', path);
Simon Hunt195cb382014-11-03 17:50:51 -08002256 }
2257
Simon Huntf67722a2014-11-10 09:32:06 -08002258 function resize(view, ctx, flags) {
Simon Hunt12ce12e2014-11-15 21:13:19 -08002259 var w = view.width(),
2260 h = view.height();
2261
Simon Hunt934c3ce2014-11-05 11:45:07 -08002262 setSize(svg, view);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002263
2264 d3.select('#mask-bird').attr({ width: w, height: h})
2265 .select('g').attr('transform', birdTranslate(w, h));
Simon Hunt142d0032014-11-04 20:13:09 -08002266 }
2267
Simon Hunt12ce12e2014-11-15 21:13:19 -08002268 function birdTranslate(w, h) {
2269 var bdim = config.birdDim;
2270 return 'translate('+((w-bdim)*.4)+','+((h-bdim)*.1)+')';
2271 }
Simon Hunt142d0032014-11-04 20:13:09 -08002272
2273 // ==============================
2274 // View registration
Simon Hunt195cb382014-11-03 17:50:51 -08002275
Simon Hunt25248912014-11-04 11:25:48 -08002276 onos.ui.addView('topo', {
Simon Hunt142d0032014-11-04 20:13:09 -08002277 preload: preload,
2278 load: load,
Simon Hunta255a2c2014-11-13 22:29:35 -08002279 unload: unload,
Simon Hunt142d0032014-11-04 20:13:09 -08002280 resize: resize
Simon Hunt195cb382014-11-03 17:50:51 -08002281 });
2282
Simon Hunt61d04042014-11-11 17:27:16 -08002283 detailPane = onos.ui.addFloatingPanel('topo-detail');
Simon Hunta5e89142014-11-14 07:00:33 -08002284 oiBox = onos.ui.addFloatingPanel('topo-oibox', 'TL');
Simon Hunt61d04042014-11-11 17:27:16 -08002285
Simon Hunt195cb382014-11-03 17:50:51 -08002286}(ONOS));