blob: 25782f9408411c9fcfaed2a56c4e9dd6aa68d2e5 [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 Huntfcfb46c2014-11-19 12:53:38 -0800560 updateInstance: updateInstance,
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();
Simon Hunt56a2ea42014-11-19 12:39:31 -0800655 } else {
656 logicError('updateInstance lookup fail. ID = "' + id + '"');
657 }
658 }
659
Simon Huntbb282f52014-11-10 11:08:19 -0800660 function updateDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800661 evTrace(data);
Simon Huntbb282f52014-11-10 11:08:19 -0800662 var device = data.payload,
663 id = device.id,
664 nodeData = network.lookup[id];
665 if (nodeData) {
666 $.extend(nodeData, device);
667 updateDeviceState(nodeData);
668 } else {
669 logicError('updateDevice lookup fail. ID = "' + id + '"');
670 }
671 }
672
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800673 function updateLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800674 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800675 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800676 result = findLink(link, 'update'),
677 bad = result.badLogic;
678 if (bad) {
679 logicError(bad + ': ' + link.id);
680 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800681 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800682 result.updateWith(link);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800683 }
684
Simon Hunt7cd48f32014-11-09 23:42:50 -0800685 function updateHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800686 evTrace(data);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800687 var host = data.payload,
Simon Huntbb282f52014-11-10 11:08:19 -0800688 id = host.id,
689 hostData = network.lookup[id];
690 if (hostData) {
691 $.extend(hostData, host);
692 updateHostState(hostData);
693 } else {
694 logicError('updateHost lookup fail. ID = "' + id + '"');
695 }
Simon Hunt7cd48f32014-11-09 23:42:50 -0800696 }
697
Simon Hunt44031102014-11-11 13:20:36 -0800698 // TODO: fold removeX(...) methods into base method - remove dup code
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800699 function removeLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800700 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800701 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800702 result = findLink(link, 'remove'),
703 bad = result.badLogic;
704 if (bad) {
705 logicError(bad + ': ' + link.id);
706 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800707 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800708 result.removeRawLink();
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800709 }
710
Simon Hunt44031102014-11-11 13:20:36 -0800711 function removeHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800712 evTrace(data);
Simon Hunt44031102014-11-11 13:20:36 -0800713 var host = data.payload,
714 id = host.id,
715 hostData = network.lookup[id];
716 if (hostData) {
717 removeHostElement(hostData);
718 } else {
719 logicError('removeHost lookup fail. ID = "' + id + '"');
720 }
721 }
722
Simon Hunt61d04042014-11-11 17:27:16 -0800723 function showDetails(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800724 evTrace(data);
Simon Hunt61d04042014-11-11 17:27:16 -0800725 populateDetails(data.payload);
726 detailPane.show();
727 }
728
Simon Huntb53e0682014-11-12 13:32:01 -0800729 function showTraffic(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800730 evTrace(data);
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800731 var paths = data.payload.paths;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800732
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800733 // Revert any links hilighted previously.
Simon Hunta255a2c2014-11-13 22:29:35 -0800734 link.classed('primary secondary animated optical', false);
Simon Hunte2575b62014-11-18 15:25:53 -0800735 // Remove all previous labels.
736 removeLinkLabels();
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800737
Simon Hunte2575b62014-11-18 15:25:53 -0800738 // Now hilight all links in the paths payload, and attach
739 // labels to them, if they are defined.
Simon Hunta255a2c2014-11-13 22:29:35 -0800740 paths.forEach(function (p) {
Simon Hunte2575b62014-11-18 15:25:53 -0800741 var n = p.links.length,
742 i,
743 ldata;
744
745 for (i=0; i<n; i++) {
746 ldata = findLinkById(p.links[i]);
747 if (ldata) {
748 ldata.el.classed(p.class, true);
749 ldata.label = p.labels[i];
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800750 }
Simon Hunte2575b62014-11-18 15:25:53 -0800751 }
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800752 });
Simon Hunte2575b62014-11-18 15:25:53 -0800753 updateLinks();
Simon Huntb53e0682014-11-12 13:32:01 -0800754 }
755
Simon Hunt56d51852014-11-09 13:03:35 -0800756 // ...............................
757
758 function stillToImplement(data) {
759 var p = data.payload;
760 note(data.event, p.id);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800761 network.view.alert('Not yet implemented: "' + data.event + '"');
Simon Hunt56d51852014-11-09 13:03:35 -0800762 }
Simon Hunt99c13842014-11-06 18:23:12 -0800763
764 function unknownEvent(data) {
Simon Hunt50128c02014-11-08 13:36:15 -0800765 network.view.alert('Unknown event type: "' + data.event + '"');
Simon Hunt99c13842014-11-06 18:23:12 -0800766 }
767
768 function handleServerEvent(data) {
769 var fn = eventDispatch[data.event] || unknownEvent;
770 fn(data);
771 }
772
773 // ==============================
Simon Hunt61d04042014-11-11 17:27:16 -0800774 // Out-going messages...
775
Simon Huntb53e0682014-11-12 13:32:01 -0800776 function userFeedback(msg) {
777 // for now, use the alert pane as is. Maybe different alert style in
778 // the future (centered on view; dismiss button?)
779 network.view.alert(msg);
780 }
781
782 function nSel() {
783 return selectOrder.length;
784 }
Simon Hunt61d04042014-11-11 17:27:16 -0800785 function getSel(idx) {
786 return selections[selectOrder[idx]];
787 }
Simon Huntb53e0682014-11-12 13:32:01 -0800788 function getSelId(idx) {
789 return getSel(idx).obj.id;
790 }
791 function allSelectionsClass(cls) {
792 for (var i=0, n=nSel(); i<n; i++) {
793 if (getSel(i).obj.class !== cls) {
794 return false;
795 }
796 }
797 return true;
798 }
Simon Hunt61d04042014-11-11 17:27:16 -0800799
Simon Hunt61d04042014-11-11 17:27:16 -0800800 // request details for the selected element
Simon Huntd72bc702014-11-13 18:38:04 -0800801 // invoked from selection of a single node.
Simon Hunt61d04042014-11-11 17:27:16 -0800802 function requestDetails() {
803 var data = getSel(0).obj,
804 payload = {
805 id: data.id,
806 class: data.class
807 };
808 sendMessage('requestDetails', payload);
809 }
810
Simon Huntd72bc702014-11-13 18:38:04 -0800811 function addIntentAction() {
812 sendMessage('addHostIntent', {
813 one: getSelId(0),
Thomas Vachuska82f2c622014-11-17 12:23:18 -0800814 two: getSelId(1),
815 ids: [ getSelId(0), getSelId(1) ]
Simon Huntd72bc702014-11-13 18:38:04 -0800816 });
817 }
818
819 function showTrafficAction() {
820 // if nothing is hovered over, and nothing selected, send cancel request
821 if (!hovered && nSel() === 0) {
822 sendMessage('cancelTraffic', {});
823 return;
824 }
825
826 // NOTE: hover is only populated if "show traffic on hover" is
827 // toggled on, and the item hovered is a host...
828 var hoverId = (trafficHover() && hovered && hovered.class === 'host')
829 ? hovered.id : '';
830 sendMessage('requestTraffic', {
831 ids: selectOrder,
832 hover: hoverId
833 });
834 }
835
836
Simon Hunt61d04042014-11-11 17:27:16 -0800837 // ==============================
Simon Hunta5e89142014-11-14 07:00:33 -0800838 // onos instance panel functions
839
840 function updateInstances() {
841 var onoses = oiBox.el.selectAll('.onosInst')
842 .data(onosOrder, function (d) { return d.id; });
843
844 // operate on existing onoses if necessary
Simon Huntfcfb46c2014-11-19 12:53:38 -0800845 onoses.classed('online', function (d) { return d.online; });
Simon Hunta5e89142014-11-14 07:00:33 -0800846
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({
Simon Huntfcfb46c2014-11-19 12:53:38 -0800860 width: 30
Simon Hunt9c15eca2014-11-15 18:37:59 -0800861 });
862
863 $('<div>').attr('class', 'onosTitle').text(d.id).appendTo(el);
864
865 // is the UI attached to this instance?
866 // TODO: need uiAttached boolean in instance data
867 //if (d.uiAttached) {
868 if (i === 0) {
869 $('<img src="img/ui.png">').attr('class','ui').appendTo(el);
870 }
871 });
Simon Hunta5e89142014-11-14 07:00:33 -0800872
873 // operate on existing + new onoses here
874
875 // the departed...
876 var exiting = onoses.exit()
877 .transition()
878 .style('opacity', 0)
879 .remove();
880 }
881
Simon Hunt9462e8c2014-11-14 17:28:09 -0800882 function clickInst(d) {
883 var el = d3.select(this),
884 aff = el.classed('affinity');
885 if (!aff) {
886 setAffinity(el, d);
887 } else {
888 cancelAffinity();
889 }
890 }
891
892 function setAffinity(el, d) {
893 d3.selectAll('.onosInst')
894 .classed('mastership', true)
895 .classed('affinity', false);
896 el.classed('affinity', true);
897
898 suppressLayers(true);
899 node.each(function (n) {
900 if (n.master === d.id) {
901 n.el.classed('suppressed', false);
902 }
903 });
904 oiShowMaster = true;
905 }
906
907 function cancelAffinity() {
908 d3.selectAll('.onosInst')
909 .classed('mastership affinity', false);
910 restoreLayerState();
911 oiShowMaster = false;
912 }
913
Simon Hunta5e89142014-11-14 07:00:33 -0800914 // ==============================
Simon Hunt99c13842014-11-06 18:23:12 -0800915 // force layout modification functions
916
917 function translate(x, y) {
918 return 'translate(' + x + ',' + y + ')';
919 }
920
Simon Hunte2575b62014-11-18 15:25:53 -0800921 function rotate(deg) {
922 return 'rotate(' + deg + ')';
923 }
924
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800925 function missMsg(what, id) {
926 return '\n[' + what + '] "' + id + '" missing ';
927 }
928
929 function linkEndPoints(srcId, dstId) {
930 var srcNode = network.lookup[srcId],
931 dstNode = network.lookup[dstId],
932 sMiss = !srcNode ? missMsg('src', srcId) : '',
933 dMiss = !dstNode ? missMsg('dst', dstId) : '';
934
935 if (sMiss || dMiss) {
936 logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
937 return null;
938 }
939 return {
940 source: srcNode,
941 target: dstNode,
942 x1: srcNode.x,
943 y1: srcNode.y,
944 x2: dstNode.x,
945 y2: dstNode.y
946 };
947 }
948
Simon Hunt56d51852014-11-09 13:03:35 -0800949 function createHostLink(host) {
950 var src = host.id,
951 dst = host.cp.device,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800952 id = host.ingress,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800953 lnk = linkEndPoints(src, dst);
Simon Hunt56d51852014-11-09 13:03:35 -0800954
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800955 if (!lnk) {
Simon Hunt56d51852014-11-09 13:03:35 -0800956 return null;
957 }
958
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800959 // Synthesize link ...
960 $.extend(lnk, {
Simon Hunt8257f4c2014-11-16 19:34:54 -0800961 key: id,
Simon Hunt56d51852014-11-09 13:03:35 -0800962 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -0800963
964 type: function () { return 'hostLink'; },
965 // TODO: ideally, we should see if our edge switch is online...
966 online: function () { return true; },
967 linkWidth: function () { return 1; }
Simon Hunt7cd48f32014-11-09 23:42:50 -0800968 });
Simon Hunt99c13842014-11-06 18:23:12 -0800969 return lnk;
970 }
971
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800972 function createLink(link) {
973 var lnk = linkEndPoints(link.src, link.dst),
974 type = link.type;
975
976 if (!lnk) {
977 return null;
978 }
979
Simon Hunt8257f4c2014-11-16 19:34:54 -0800980 $.extend(lnk, {
981 key: link.id,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800982 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -0800983 fromSource: link,
984
985 // functions to aggregate dual link state
986 type: function () {
987 var s = lnk.fromSource,
988 t = lnk.fromTarget;
989 return (s && s.type) || (t && t.type) || defaultLinkType;
990 },
991 online: function () {
992 var s = lnk.fromSource,
993 t = lnk.fromTarget;
994 return (s && s.online) || (t && t.online);
995 },
996 linkWidth: function () {
997 var s = lnk.fromSource,
998 t = lnk.fromTarget,
999 ws = (s && s.linkWidth) || 0,
1000 wt = (t && t.linkWidth) || 0;
1001 return Math.max(ws, wt);
1002 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001003 });
1004 return lnk;
Simon Hunt1a9eff92014-11-07 11:06:34 -08001005 }
1006
Simon Hunte2575b62014-11-18 15:25:53 -08001007 function removeLinkLabels() {
1008 network.links.forEach(function (d) {
1009 d.label = '';
1010 });
1011 }
1012
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001013 var widthRatio = 1.4,
1014 linkScale = d3.scale.linear()
1015 .domain([1, 12])
1016 .range([widthRatio, 12 * widthRatio])
1017 .clamp(true);
1018
Simon Hunt99c13842014-11-06 18:23:12 -08001019 function updateLinks() {
1020 link = linkG.selectAll('.link')
Simon Hunt8257f4c2014-11-16 19:34:54 -08001021 .data(network.links, function (d) { return d.key; });
Simon Hunt99c13842014-11-06 18:23:12 -08001022
1023 // operate on existing links, if necessary
1024 // link .foo() .bar() ...
1025
1026 // operate on entering links:
1027 var entering = link.enter()
1028 .append('line')
1029 .attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001030 x1: function (d) { return d.x1; },
1031 y1: function (d) { return d.y1; },
1032 x2: function (d) { return d.x2; },
1033 y2: function (d) { return d.y2; },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001034 stroke: config.topo.linkInColor,
1035 'stroke-width': config.topo.linkInWidth
Simon Hunt99c13842014-11-06 18:23:12 -08001036 });
1037
1038 // augment links
Simon Hunt7cd48f32014-11-09 23:42:50 -08001039 entering.each(function (d) {
1040 var link = d3.select(this);
1041 // provide ref to element selection from backing data....
1042 d.el = link;
Simon Hunt8257f4c2014-11-16 19:34:54 -08001043 restyleLinkElement(d);
Simon Hunt7cd48f32014-11-09 23:42:50 -08001044 });
Thomas Vachuska4830d392014-11-09 17:09:56 -08001045
1046 // operate on both existing and new links, if necessary
1047 //link .foo() .bar() ...
1048
Simon Hunte2575b62014-11-18 15:25:53 -08001049 // apply or remove labels
1050 var labelData = getLabelData();
1051 applyLinkLabels(labelData);
1052
Thomas Vachuska4830d392014-11-09 17:09:56 -08001053 // operate on exiting links:
Thomas Vachuska4830d392014-11-09 17:09:56 -08001054 link.exit()
Simon Hunt13bf9c82014-11-18 07:26:44 -08001055 .attr('stroke-dasharray', '3, 3')
1056 .style('opacity', 0.5)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001057 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -08001058 .duration(1500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001059 .attr({
Simon Hunt13bf9c82014-11-18 07:26:44 -08001060 'stroke-dasharray': '3, 12',
1061 stroke: config.topo.linkOutColor,
1062 'stroke-width': config.topo.linkOutWidth
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001063 })
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001064 .style('opacity', 0.0)
Thomas Vachuska4830d392014-11-09 17:09:56 -08001065 .remove();
Simon Hunte2575b62014-11-18 15:25:53 -08001066
1067 // NOTE: invoke a single tick to force the labels to position
1068 // onto their links.
1069 tick();
1070 }
1071
1072 function getLabelData() {
1073 // create the backing data for showing labels..
1074 var data = [];
1075 link.each(function (d) {
1076 if (d.label) {
1077 data.push({
1078 id: 'lab-' + d.key,
1079 key: d.key,
1080 label: d.label,
1081 ldata: d
1082 });
1083 }
1084 });
1085 return data;
1086 }
1087
1088 var linkLabelOffset = '0.3em';
1089
1090 function applyLinkLabels(data) {
1091 var entering;
1092
1093 linkLabel = linkLabelG.selectAll('.linkLabel')
1094 .data(data, function (d) { return d.id; });
1095
Simon Hunt56a2ea42014-11-19 12:39:31 -08001096 // for elements already existing, we need to update the text
1097 // and adjust the rectangle size to fit
1098 linkLabel.each(function (d) {
1099 var el = d3.select(this),
1100 rect = el.select('rect'),
1101 text = el.select('text');
1102 text.text(d.label);
1103 rect.attr(rectAroundText(el));
1104 });
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -08001105
Simon Hunte2575b62014-11-18 15:25:53 -08001106 entering = linkLabel.enter().append('g')
1107 .classed('linkLabel', true)
1108 .attr('id', function (d) { return d.id; });
1109
1110 entering.each(function (d) {
1111 var el = d3.select(this),
1112 rect,
1113 text,
1114 parms = {
1115 x1: d.ldata.x1,
1116 y1: d.ldata.y1,
1117 x2: d.ldata.x2,
1118 y2: d.ldata.y2
1119 };
1120
1121 d.el = el;
1122 rect = el.append('rect');
1123 text = el.append('text').text(d.label);
1124 rect.attr(rectAroundText(el));
1125 text.attr('dy', linkLabelOffset);
1126
1127 el.attr('transform', transformLabel(parms));
1128 });
1129
1130 // Remove any links that are no longer required.
1131 linkLabel.exit().remove();
1132 }
1133
1134 function rectAroundText(el) {
1135 var text = el.select('text'),
1136 box = text.node().getBBox();
1137
1138 // translate the bbox so that it is centered on [x,y]
1139 box.x = -box.width / 2;
1140 box.y = -box.height / 2;
1141
1142 // add padding
1143 box.x -= 1;
1144 box.width += 2;
1145 return box;
1146 }
1147
1148 function transformLabel(p) {
1149 var dx = p.x2 - p.x1,
1150 dy = p.y2 - p.y1,
1151 xMid = dx/2 + p.x1,
1152 yMid = dy/2 + p.y1;
1153 //length = Math.sqrt(dx*dx + dy*dy),
1154 //rads = Math.asin(dy/length),
1155 //degs = rads / (Math.PI*2) * 360;
1156
1157 return translate(xMid, yMid);
1158
1159 // TODO: consider making label parallel to line
1160 //return [
1161 // translate(xMid, yMid),
1162 // rotate(degs),
1163 // translate(0, 8)
1164 //].join('');
Simon Hunt99c13842014-11-06 18:23:12 -08001165 }
1166
1167 function createDeviceNode(device) {
1168 // start with the object as is
1169 var node = device,
Simon Huntbb282f52014-11-10 11:08:19 -08001170 type = device.type,
1171 svgCls = type ? 'node device ' + type : 'node device';
Simon Hunt99c13842014-11-06 18:23:12 -08001172
1173 // Augment as needed...
1174 node.class = 'device';
Simon Huntbb282f52014-11-10 11:08:19 -08001175 node.svgClass = device.online ? svgCls + ' online' : svgCls;
Simon Hunt99c13842014-11-06 18:23:12 -08001176 positionNode(node);
1177
1178 // cache label array length
1179 network.deviceLabelCount = device.labels.length;
Simon Hunt99c13842014-11-06 18:23:12 -08001180 return node;
1181 }
1182
Simon Hunt56d51852014-11-09 13:03:35 -08001183 function createHostNode(host) {
1184 // start with the object as is
1185 var node = host;
1186
1187 // Augment as needed...
1188 node.class = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001189 if (!node.type) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08001190 node.type = 'endstation';
1191 }
Simon Hunt7fa116d2014-11-17 14:16:55 -08001192 node.svgClass = 'node host ' + node.type;
Simon Hunt56d51852014-11-09 13:03:35 -08001193 positionNode(node);
1194
1195 // cache label array length
1196 network.hostLabelCount = host.labels.length;
Simon Hunt56d51852014-11-09 13:03:35 -08001197 return node;
1198 }
1199
Simon Hunt99c13842014-11-06 18:23:12 -08001200 function positionNode(node) {
1201 var meta = node.metaUi,
Simon Huntac9e24f2014-11-12 10:12:21 -08001202 x = meta && meta.x,
1203 y = meta && meta.y,
1204 xy;
Simon Hunt99c13842014-11-06 18:23:12 -08001205
Simon Huntac9e24f2014-11-12 10:12:21 -08001206 // If we have [x,y] already, use that...
Simon Hunt99c13842014-11-06 18:23:12 -08001207 if (x && y) {
1208 node.fixed = true;
Simon Huntac9e24f2014-11-12 10:12:21 -08001209 node.x = x;
1210 node.y = y;
1211 return;
Simon Hunt99c13842014-11-06 18:23:12 -08001212 }
Simon Huntac9e24f2014-11-12 10:12:21 -08001213
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001214 var location = node.location;
1215 if (location && location.type === 'latlng') {
1216 var coord = geoMapProjection([location.lng, location.lat]);
1217 node.fixed = true;
1218 node.x = coord[0];
1219 node.y = coord[1];
1220 return;
1221 }
1222
Simon Huntac9e24f2014-11-12 10:12:21 -08001223 // Note: Placing incoming unpinned nodes at exactly the same point
1224 // (center of the view) causes them to explode outwards when
1225 // the force layout kicks in. So, we spread them out a bit
1226 // initially, to provide a more serene layout convergence.
1227 // Additionally, if the node is a host, we place it near
1228 // the device it is connected to.
1229
1230 function spread(s) {
1231 return Math.floor((Math.random() * s) - s/2);
1232 }
1233
1234 function randDim(dim) {
1235 return dim / 2 + spread(dim * 0.7071);
1236 }
1237
1238 function rand() {
1239 return {
1240 x: randDim(network.view.width()),
1241 y: randDim(network.view.height())
1242 };
1243 }
1244
1245 function near(node) {
1246 var min = 12,
1247 dx = spread(12),
1248 dy = spread(12);
1249 return {
1250 x: node.x + min + dx,
1251 y: node.y + min + dy
1252 };
1253 }
1254
1255 function getDevice(cp) {
1256 var d = network.lookup[cp.device];
1257 return d || rand();
1258 }
1259
1260 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
1261 $.extend(node, xy);
Simon Hunt99c13842014-11-06 18:23:12 -08001262 }
1263
Thomas Vachuska89543292014-11-19 11:28:33 -08001264 function iconUrl(d) {
1265 return 'img/' + d.type + '.png';
Simon Hunt99c13842014-11-06 18:23:12 -08001266 }
1267
1268 // returns the newly computed bounding box of the rectangle
1269 function adjustRectToFitText(n) {
1270 var text = n.select('text'),
1271 box = text.node().getBBox(),
1272 lab = config.labels;
1273
1274 text.attr('text-anchor', 'middle')
1275 .attr('y', '-0.8em')
1276 .attr('x', lab.imgPad/2);
1277
1278 // translate the bbox so that it is centered on [x,y]
1279 box.x = -box.width / 2;
1280 box.y = -box.height / 2;
1281
1282 // add padding
1283 box.x -= (lab.padLR + lab.imgPad/2);
1284 box.width += lab.padLR * 2 + lab.imgPad;
1285 box.y -= lab.padTB;
1286 box.height += lab.padTB * 2;
1287
1288 return box;
1289 }
1290
Simon Hunt1a9eff92014-11-07 11:06:34 -08001291 function mkSvgClass(d) {
1292 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
1293 }
1294
Simon Hunt7cd48f32014-11-09 23:42:50 -08001295 function hostLabel(d) {
1296 var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0;
1297 return d.labels[idx];
1298 }
1299 function deviceLabel(d) {
1300 var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0;
1301 return d.labels[idx];
1302 }
1303 function niceLabel(label) {
1304 return (label && label.trim()) ? label : '.';
1305 }
1306
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001307 function updateDeviceLabel(d) {
1308 var label = niceLabel(deviceLabel(d)),
1309 node = d.el,
1310 box;
1311
1312 node.select('text')
1313 .text(label)
1314 .style('opacity', 0)
1315 .transition()
1316 .style('opacity', 1);
1317
1318 box = adjustRectToFitText(node);
1319
1320 node.select('rect')
1321 .transition()
1322 .attr(box);
1323
Simon Hunt56a2ea42014-11-19 12:39:31 -08001324 node.select('rect.iconUnderlay')
Thomas Vachuska89543292014-11-19 11:28:33 -08001325 .transition()
1326 .attr('x', box.x + config.icons.xoff)
1327 .attr('y', box.y + config.icons.yoff);
Simon Hunt56a2ea42014-11-19 12:39:31 -08001328
1329 node.select('image')
1330 .transition()
1331 .attr('x', box.x + config.icons.xoff + 2)
1332 .attr('y', box.y + config.icons.yoff + 2);
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001333 }
1334
1335 function updateHostLabel(d) {
1336 var label = hostLabel(d),
1337 host = d.el;
1338
1339 host.select('text').text(label);
1340 }
1341
Simon Huntbb282f52014-11-10 11:08:19 -08001342 function updateDeviceState(nodeData) {
1343 nodeData.el.classed('online', nodeData.online);
1344 updateDeviceLabel(nodeData);
1345 // TODO: review what else might need to be updated
1346 }
1347
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001348 function updateLinkState(linkData) {
1349 updateLinkWidth(linkData);
Thomas Vachuskabadb93f2014-11-15 23:51:17 -08001350 linkData.el.classed('inactive', !linkData.online);
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001351 // TODO: review what else might need to be updated
1352 // update label, if showing
1353 }
1354
Simon Huntbb282f52014-11-10 11:08:19 -08001355 function updateHostState(hostData) {
1356 updateHostLabel(hostData);
1357 // TODO: review what else might need to be updated
1358 }
1359
Simon Hunt6ac93f32014-11-13 12:17:27 -08001360 function nodeMouseOver(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001361 hovered = d;
Simon Huntd72bc702014-11-13 18:38:04 -08001362 if (trafficHover() && d.class === 'host') {
1363 showTrafficAction();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001364 }
1365 }
1366
1367 function nodeMouseOut(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001368 hovered = null;
Simon Huntd72bc702014-11-13 18:38:04 -08001369 if (trafficHover() && d.class === 'host') {
1370 showTrafficAction();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001371 }
1372 }
Simon Huntbb282f52014-11-10 11:08:19 -08001373
Thomas Vachuska89543292014-11-19 11:28:33 -08001374 function addHostIcon(node, radius, iconId) {
1375 var dim = radius * 1.5,
1376 xlate = -dim / 2;
1377
1378 node.append('svg:image')
1379 .attr('transform', translate(xlate,xlate))
1380 .attr('xlink:href', 'img/' + iconId + '.png')
1381 .attr('width', dim)
1382 .attr('height', dim);
1383 }
1384
Simon Hunt99c13842014-11-06 18:23:12 -08001385 function updateNodes() {
1386 node = nodeG.selectAll('.node')
1387 .data(network.nodes, function (d) { return d.id; });
1388
1389 // operate on existing nodes, if necessary
Simon Hunt7cd48f32014-11-09 23:42:50 -08001390 // update host labels
Simon Hunt99c13842014-11-06 18:23:12 -08001391 //node .foo() .bar() ...
1392
1393 // operate on entering nodes:
1394 var entering = node.enter()
1395 .append('g')
1396 .attr({
1397 id: function (d) { return safeId(d.id); },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001398 class: mkSvgClass,
Simon Hunt99c13842014-11-06 18:23:12 -08001399 transform: function (d) { return translate(d.x, d.y); },
1400 opacity: 0
1401 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08001402 .call(network.drag)
Simon Hunt6ac93f32014-11-13 12:17:27 -08001403 .on('mouseover', nodeMouseOver)
1404 .on('mouseout', nodeMouseOut)
Simon Hunt99c13842014-11-06 18:23:12 -08001405 .transition()
1406 .attr('opacity', 1);
1407
1408 // augment device nodes...
1409 entering.filter('.device').each(function (d) {
1410 var node = d3.select(this),
Thomas Vachuska89543292014-11-19 11:28:33 -08001411 icon = iconUrl(d),
Simon Hunt7cd48f32014-11-09 23:42:50 -08001412 label = niceLabel(deviceLabel(d)),
Simon Hunt99c13842014-11-06 18:23:12 -08001413 box;
1414
Simon Hunt7cd48f32014-11-09 23:42:50 -08001415 // provide ref to element from backing data....
1416 d.el = node;
1417
Simon Hunt99c13842014-11-06 18:23:12 -08001418 node.append('rect')
1419 .attr({
1420 'rx': 5,
1421 'ry': 5
1422 });
1423
1424 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001425 .text(label)
Simon Hunt99c13842014-11-06 18:23:12 -08001426 .attr('dy', '1.1em');
1427
1428 box = adjustRectToFitText(node);
1429
1430 node.select('rect')
1431 .attr(box);
1432
Thomas Vachuska89543292014-11-19 11:28:33 -08001433 if (icon) {
1434 var cfg = config.icons;
1435 node.append('rect')
Simon Hunt99c13842014-11-06 18:23:12 -08001436 .attr({
Simon Hunt56a2ea42014-11-19 12:39:31 -08001437 class: 'iconUnderlay',
Thomas Vachuska89543292014-11-19 11:28:33 -08001438 x: box.x + config.icons.xoff,
1439 y: box.y + config.icons.yoff,
1440 width: cfg.w,
1441 height: cfg.h,
1442 rx: 4
1443 }).style({
1444 stroke: '#000',
1445 fill: '#ddd'
1446 });
1447 node.append('svg:image')
1448 .attr({
1449 x: box.x + config.icons.xoff + 2,
1450 y: box.y + config.icons.yoff + 2,
1451 width: cfg.w - 4,
1452 height: cfg.h - 4,
1453 'xlink:href': icon
Simon Hunt99c13842014-11-06 18:23:12 -08001454 });
1455 }
1456
1457 // debug function to show the modelled x,y coordinates of nodes...
1458 if (debug('showNodeXY')) {
1459 node.select('rect').attr('fill-opacity', 0.5);
1460 node.append('circle')
1461 .attr({
1462 class: 'debug',
1463 cx: 0,
1464 cy: 0,
1465 r: '3px'
1466 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001467 }
1468 });
Simon Hunt934c3ce2014-11-05 11:45:07 -08001469
Thomas Vachuska89543292014-11-19 11:28:33 -08001470 // TODO: better place for this configuration state
1471 var defaultHostRadius = 9,
1472 hostRadius = {
1473 bgpSpeaker: 14,
1474 router: 14,
1475 host: 14
1476 },
1477 hostIcon = {
1478 bgpSpeaker: 'bgpSpeaker',
1479 router: 'router',
1480 host: 'host'
1481 };
1482
1483
Simon Hunt56d51852014-11-09 13:03:35 -08001484 // augment host nodes...
1485 entering.filter('.host').each(function (d) {
1486 var node = d3.select(this),
Thomas Vachuska89543292014-11-19 11:28:33 -08001487 r = hostRadius[d.type] || defaultHostRadius,
1488 textDy = r + 10,
1489 icon = hostIcon[d.type];
Simon Hunt56d51852014-11-09 13:03:35 -08001490
Simon Hunt7cd48f32014-11-09 23:42:50 -08001491 // provide ref to element from backing data....
1492 d.el = node;
1493
Thomas Vachuska89543292014-11-19 11:28:33 -08001494 node.append('circle')
1495 .attr('r', r);
Simon Hunt7fa116d2014-11-17 14:16:55 -08001496
Thomas Vachuska89543292014-11-19 11:28:33 -08001497 if (icon) {
1498 addHostIcon(node, r, icon);
Simon Hunt7fa116d2014-11-17 14:16:55 -08001499 }
Simon Hunt56d51852014-11-09 13:03:35 -08001500
Paul Greyson29cd58f2014-11-18 13:14:57 -08001501
Simon Hunt56d51852014-11-09 13:03:35 -08001502 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001503 .text(hostLabel)
Thomas Vachuska89543292014-11-19 11:28:33 -08001504 .attr('dy', textDy)
Simon Hunt7cd48f32014-11-09 23:42:50 -08001505 .attr('text-anchor', 'middle');
Simon Hunt56d51852014-11-09 13:03:35 -08001506
1507 // debug function to show the modelled x,y coordinates of nodes...
1508 if (debug('showNodeXY')) {
1509 node.select('circle').attr('fill-opacity', 0.5);
1510 node.append('circle')
1511 .attr({
1512 class: 'debug',
1513 cx: 0,
1514 cy: 0,
1515 r: '3px'
1516 });
1517 }
1518 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001519
Simon Hunt99c13842014-11-06 18:23:12 -08001520 // operate on both existing and new nodes, if necessary
1521 //node .foo() .bar() ...
Simon Huntc7ee0662014-11-05 16:44:37 -08001522
Simon Hunt99c13842014-11-06 18:23:12 -08001523 // operate on exiting nodes:
Simon Huntea80eb42014-11-11 13:46:57 -08001524 // Note that the node is removed after 2 seconds.
1525 // Sub element animations should be shorter than 2 seconds.
1526 var exiting = node.exit()
Simon Hunt44031102014-11-11 13:20:36 -08001527 .transition()
1528 .duration(2000)
Simon Huntea80eb42014-11-11 13:46:57 -08001529 .style('opacity', 0)
Simon Hunt99c13842014-11-06 18:23:12 -08001530 .remove();
Simon Huntea80eb42014-11-11 13:46:57 -08001531
1532 // host node exits....
1533 exiting.filter('.host').each(function (d) {
1534 var node = d3.select(this);
1535
1536 node.select('text')
1537 .style('opacity', 0.5)
1538 .transition()
1539 .duration(1000)
1540 .style('opacity', 0);
1541 // note, leave <g>.remove to remove this element
1542
Thomas Vachuska89543292014-11-19 11:28:33 -08001543 node.select('circle')
1544 .style('stroke-fill', '#555')
1545 .style('fill', '#888')
Simon Huntea80eb42014-11-11 13:46:57 -08001546 .style('opacity', 0.5)
1547 .transition()
1548 .duration(1500)
1549 .attr('r', 0);
1550 // note, leave <g>.remove to remove this element
1551
1552 });
1553
1554 // TODO: device node exits
Simon Huntc7ee0662014-11-05 16:44:37 -08001555 }
1556
Simon Hunt8257f4c2014-11-16 19:34:54 -08001557 function find(key, array) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001558 for (var idx = 0, n = array.length; idx < n; idx++) {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001559 if (array[idx].key === key) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001560 return idx;
1561 }
1562 }
1563 return -1;
1564 }
1565
1566 function removeLinkElement(linkData) {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001567 var idx = find(linkData.key, network.links),
1568 removed;
1569 if (idx >=0) {
1570 // remove from links array
1571 removed = network.links.splice(idx, 1);
1572 // remove from lookup cache
1573 delete network.lookup[removed[0].key];
1574 updateLinks();
1575 network.force.resume();
1576 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001577 }
Simon Huntc7ee0662014-11-05 16:44:37 -08001578
Simon Hunt44031102014-11-11 13:20:36 -08001579 function removeHostElement(hostData) {
1580 // first, remove associated hostLink...
1581 removeLinkElement(hostData.linkData);
1582
1583 // remove from lookup cache
1584 delete network.lookup[hostData.id];
1585 // remove from nodes array
1586 var idx = find(hostData.id, network.nodes);
1587 network.nodes.splice(idx, 1);
1588 // remove from SVG
1589 updateNodes();
1590 network.force.resume();
1591 }
1592
1593
Simon Huntc7ee0662014-11-05 16:44:37 -08001594 function tick() {
1595 node.attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001596 transform: function (d) { return translate(d.x, d.y); }
Simon Huntc7ee0662014-11-05 16:44:37 -08001597 });
1598
1599 link.attr({
1600 x1: function (d) { return d.source.x; },
1601 y1: function (d) { return d.source.y; },
1602 x2: function (d) { return d.target.x; },
1603 y2: function (d) { return d.target.y; }
1604 });
Simon Hunte2575b62014-11-18 15:25:53 -08001605
1606 linkLabel.each(function (d) {
1607 var el = d3.select(this);
1608 var lnk = findLinkById(d.key),
1609 parms = {
1610 x1: lnk.source.x,
1611 y1: lnk.source.y,
1612 x2: lnk.target.x,
1613 y2: lnk.target.y
1614 };
1615 el.attr('transform', transformLabel(parms));
1616 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001617 }
Simon Hunt934c3ce2014-11-05 11:45:07 -08001618
1619 // ==============================
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001620 // Web-Socket for live data
1621
1622 function webSockUrl() {
1623 return document.location.toString()
1624 .replace(/\#.*/, '')
1625 .replace('http://', 'ws://')
1626 .replace('https://', 'wss://')
1627 .replace('index2.html', config.webSockUrl);
1628 }
1629
1630 webSock = {
1631 ws : null,
1632
1633 connect : function() {
1634 webSock.ws = new WebSocket(webSockUrl());
1635
1636 webSock.ws.onopen = function() {
Simon Hunt0c6d4192014-11-12 12:07:10 -08001637 noWebSock(false);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001638 };
1639
1640 webSock.ws.onmessage = function(m) {
1641 if (m.data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001642 wsTraceRx(m.data);
Thomas Vachuskad472c6e2014-11-07 19:11:05 -08001643 handleServerEvent(JSON.parse(m.data));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001644 }
1645 };
1646
1647 webSock.ws.onclose = function(m) {
1648 webSock.ws = null;
Simon Hunt0c6d4192014-11-12 12:07:10 -08001649 noWebSock(true);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001650 };
1651 },
1652
1653 send : function(text) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001654 if (text != null) {
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001655 webSock._send(text);
1656 }
1657 },
1658
1659 _send : function(message) {
1660 if (webSock.ws) {
1661 webSock.ws.send(message);
Simon Hunta255a2c2014-11-13 22:29:35 -08001662 } else if (config.useLiveData) {
Simon Hunt56d51852014-11-09 13:03:35 -08001663 network.view.alert('no web socket open\n\n' + message);
Simon Hunta255a2c2014-11-13 22:29:35 -08001664 } else {
1665 console.log('WS Send: ' + JSON.stringify(message));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001666 }
1667 }
1668
1669 };
1670
Simon Hunt0c6d4192014-11-12 12:07:10 -08001671 function noWebSock(b) {
1672 mask.style('display',b ? 'block' : 'none');
1673 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001674
1675 function sendMessage(evType, payload) {
1676 var toSend = {
Simon Huntbb282f52014-11-10 11:08:19 -08001677 event: evType,
1678 sid: ++sid,
1679 payload: payload
1680 },
1681 asText = JSON.stringify(toSend);
1682 wsTraceTx(asText);
1683 webSock.send(asText);
Simon Huntc76ae892014-11-18 17:31:51 -08001684
1685 // Temporary measure for debugging UI behavior ...
1686 if (!config.useLiveData) {
1687 handleTestSend(toSend);
1688 }
Simon Huntbb282f52014-11-10 11:08:19 -08001689 }
1690
1691 function wsTraceTx(msg) {
1692 wsTrace('tx', msg);
1693 }
1694 function wsTraceRx(msg) {
1695 wsTrace('rx', msg);
1696 }
1697 function wsTrace(rxtx, msg) {
Simon Huntbb282f52014-11-10 11:08:19 -08001698 console.log('[' + rxtx + '] ' + msg);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001699 }
1700
Simon Huntc76ae892014-11-18 17:31:51 -08001701 // NOTE: Temporary hardcoded example for showing detail pane
1702 // while we fine-
1703 // Probably should not merge this change...
1704 function handleTestSend(msg) {
1705 if (msg.event === 'requestDetails') {
1706 showDetails({
1707 event: 'showDetails',
1708 sid: 1001,
1709 payload: {
1710 "id": "of:0000ffffffffff09",
1711 "type": "roadm",
1712 "propOrder": [
1713 "Name",
1714 "Vendor",
1715 "H/W Version",
1716 "S/W Version",
1717 "-",
1718 "Latitude",
1719 "Longitude",
1720 "Ports"
1721 ],
1722 "props": {
1723 "Name": null,
1724 "Vendor": "Linc",
1725 "H/W Version": "OE",
1726 "S/W Version": "?",
1727 "-": "",
1728 "Latitude": "40.8",
1729 "Longitude": "73.1",
1730 "Ports": "2"
1731 }
1732 }
1733 });
1734 }
1735 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001736
1737 // ==============================
1738 // Selection stuff
1739
1740 function selectObject(obj, el) {
1741 var n,
Simon Hunt01095ff2014-11-13 16:37:29 -08001742 srcEv = d3.event.sourceEvent,
1743 meta = srcEv.metaKey,
1744 shift = srcEv.shiftKey;
1745
Simon Huntdeab4322014-11-13 18:49:07 -08001746 if ((panZoom() && !meta) || (!panZoom() && meta)) {
Simon Hunt01095ff2014-11-13 16:37:29 -08001747 return;
1748 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001749
1750 if (el) {
1751 n = d3.select(el);
1752 } else {
1753 node.each(function(d) {
1754 if (d == obj) {
1755 n = d3.select(el = this);
1756 }
1757 });
1758 }
1759 if (!n) return;
1760
Simon Hunt01095ff2014-11-13 16:37:29 -08001761 if (shift && n.classed('selected')) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001762 deselectObject(obj.id);
Simon Hunt61d04042014-11-11 17:27:16 -08001763 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001764 return;
1765 }
1766
Simon Hunt01095ff2014-11-13 16:37:29 -08001767 if (!shift) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001768 deselectAll();
1769 }
1770
Simon Huntc31d5692014-11-12 13:27:18 -08001771 selections[obj.id] = { obj: obj, el: el };
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001772 selectOrder.push(obj.id);
1773
1774 n.classed('selected', true);
Simon Hunt61d04042014-11-11 17:27:16 -08001775 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001776 }
1777
1778 function deselectObject(id) {
Simon Huntc31d5692014-11-12 13:27:18 -08001779 var obj = selections[id],
1780 idx;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001781 if (obj) {
1782 d3.select(obj.el).classed('selected', false);
Simon Hunt61d04042014-11-11 17:27:16 -08001783 delete selections[id];
Simon Huntc31d5692014-11-12 13:27:18 -08001784 idx = $.inArray(id, selectOrder);
1785 if (idx >= 0) {
1786 selectOrder.splice(idx, 1);
1787 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001788 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001789 }
1790
1791 function deselectAll() {
1792 // deselect all nodes in the network...
1793 node.classed('selected', false);
1794 selections = {};
1795 selectOrder = [];
Simon Hunt61d04042014-11-11 17:27:16 -08001796 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001797 }
1798
Simon Hunt61d04042014-11-11 17:27:16 -08001799 // update the state of the detail pane, based on current selections
1800 function updateDetailPane() {
1801 var nSel = selectOrder.length;
1802 if (!nSel) {
1803 detailPane.hide();
Simon Huntd72bc702014-11-13 18:38:04 -08001804 showTrafficAction(); // sends cancelTraffic event
Simon Hunt61d04042014-11-11 17:27:16 -08001805 } else if (nSel === 1) {
1806 singleSelect();
1807 } else {
1808 multiSelect();
1809 }
1810 }
1811
1812 function singleSelect() {
1813 requestDetails();
Simon Huntb53e0682014-11-12 13:32:01 -08001814 // NOTE: detail pane will be shown from showDetails event callback
Simon Hunt61d04042014-11-11 17:27:16 -08001815 }
1816
1817 function multiSelect() {
Simon Huntb53e0682014-11-12 13:32:01 -08001818 populateMultiSelect();
Simon Huntb53e0682014-11-12 13:32:01 -08001819 }
1820
1821 function addSep(tbody) {
1822 var tr = tbody.append('tr');
1823 $('<hr>').appendTo(tr.append('td').attr('colspan', 2));
1824 }
1825
1826 function addProp(tbody, label, value) {
1827 var tr = tbody.append('tr');
1828
1829 tr.append('td')
1830 .attr('class', 'label')
1831 .text(label + ' :');
1832
1833 tr.append('td')
1834 .attr('class', 'value')
1835 .text(value);
1836 }
1837
1838 function populateMultiSelect() {
1839 detailPane.empty();
1840
1841 var title = detailPane.append("h2"),
1842 table = detailPane.append("table"),
1843 tbody = table.append("tbody");
1844
1845 title.text('Multi-Select...');
1846
1847 selectOrder.forEach(function (d, i) {
1848 addProp(tbody, i+1, d);
1849 });
Simon Huntd72bc702014-11-13 18:38:04 -08001850
1851 addMultiSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08001852 }
1853
1854 function populateDetails(data) {
1855 detailPane.empty();
1856
1857 var title = detailPane.append("h2"),
1858 table = detailPane.append("table"),
1859 tbody = table.append("tbody");
1860
1861 $('<img src="img/' + data.type + '.png">').appendTo(title);
1862 $('<span>').attr('class', 'icon').text(data.id).appendTo(title);
1863
1864 data.propOrder.forEach(function(p) {
1865 if (p === '-') {
1866 addSep(tbody);
1867 } else {
1868 addProp(tbody, p, data.props[p]);
1869 }
1870 });
Simon Huntd72bc702014-11-13 18:38:04 -08001871
1872 addSingleSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08001873 }
1874
Simon Huntd72bc702014-11-13 18:38:04 -08001875 function addSingleSelectActions() {
1876 detailPane.append('hr');
1877 // always want to allow 'show traffic'
Simon Hunta5e89142014-11-14 07:00:33 -08001878 addAction(detailPane, 'Show Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001879 }
1880
1881 function addMultiSelectActions() {
1882 detailPane.append('hr');
1883 // always want to allow 'show traffic'
Simon Hunta5e89142014-11-14 07:00:33 -08001884 addAction(detailPane, 'Show Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001885 // if exactly two hosts are selected, also want 'add host intent'
1886 if (nSel() === 2 && allSelectionsClass('host')) {
Simon Hunta5e89142014-11-14 07:00:33 -08001887 addAction(detailPane, 'Add Host Intent', addIntentAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001888 }
1889 }
1890
Simon Hunta5e89142014-11-14 07:00:33 -08001891 function addAction(panel, text, cb) {
1892 panel.append('div')
Simon Huntd72bc702014-11-13 18:38:04 -08001893 .classed('actionBtn', true)
1894 .text(text)
1895 .on('click', cb);
1896 }
1897
1898
Paul Greysonfcba0e82014-11-13 10:21:16 -08001899 function zoomPan(scale, translate) {
1900 zoomPanContainer.attr("transform", "translate(" + translate + ")scale(" + scale + ")");
1901 // keep the map lines constant width while zooming
Thomas Vachuska89543292014-11-19 11:28:33 -08001902 bgImg.style("stroke-width", 2.0 / scale + "px");
Paul Greysonfcba0e82014-11-13 10:21:16 -08001903 }
1904
1905 function resetZoomPan() {
1906 zoomPan(1, [0,0]);
1907 zoom.scale(1).translate([0,0]);
1908 }
1909
1910 function setupZoomPan() {
1911 function zoomed() {
Simon Huntdeab4322014-11-13 18:49:07 -08001912 if (!panZoom() ^ !d3.event.sourceEvent.metaKey) {
Paul Greysonfcba0e82014-11-13 10:21:16 -08001913 zoomPan(d3.event.scale, d3.event.translate);
1914 }
1915 }
1916
1917 zoom = d3.behavior.zoom()
1918 .translate([0, 0])
1919 .scale(1)
1920 .scaleExtent([1, 8])
1921 .on("zoom", zoomed);
1922
1923 svg.call(zoom);
1924 }
1925
Simon Hunt61d04042014-11-11 17:27:16 -08001926 // ==============================
1927 // Test harness code
Simon Hunt56d51852014-11-09 13:03:35 -08001928
1929 function prepareScenario(view, ctx, dbg) {
1930 var sc = scenario,
1931 urlSc = sc.evDir + ctx + sc.evScenario;
1932
1933 if (!ctx) {
1934 view.alert("No scenario specified (null ctx)");
1935 return;
1936 }
1937
1938 sc.view = view;
1939 sc.ctx = ctx;
1940 sc.debug = dbg;
1941 sc.evNumber = 0;
1942
1943 d3.json(urlSc, function(err, data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001944 var p = data && data.params || {},
1945 desc = data && data.description || null,
Simon Huntfc274c92014-11-11 11:05:46 -08001946 intro = data && data.title;
Simon Huntbb282f52014-11-10 11:08:19 -08001947
Simon Hunt56d51852014-11-09 13:03:35 -08001948 if (err) {
1949 view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err);
1950 } else {
1951 sc.params = p;
Simon Huntbb282f52014-11-10 11:08:19 -08001952 if (desc) {
1953 intro += '\n\n ' + desc.join('\n ');
1954 }
1955 view.alert(intro);
Simon Hunt56d51852014-11-09 13:03:35 -08001956 }
1957 });
1958
1959 }
1960
Simon Hunt01095ff2014-11-13 16:37:29 -08001961 // ==============================
1962 // Toggle Buttons in masthead
Simon Hunt0c6d4192014-11-12 12:07:10 -08001963
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001964 // TODO: toggle button (and other widgets in the masthead) should be provided
1965 // by the framework; not generated by the view.
1966
Simon Hunta5e89142014-11-14 07:00:33 -08001967 var showInstances,
1968 doPanZoom,
1969 showTrafficOnHover;
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001970
1971 function addButtonBar(view) {
1972 var bb = d3.select('#mast')
1973 .append('span').classed('right', true).attr('id', 'bb');
1974
Simon Hunta5e89142014-11-14 07:00:33 -08001975 function mkTogBtn(text, cb) {
1976 return bb.append('span')
1977 .classed('btn', true)
1978 .text(text)
1979 .on('click', cb);
1980 }
Simon Hunt01095ff2014-11-13 16:37:29 -08001981
Simon Hunta5e89142014-11-14 07:00:33 -08001982 showInstances = mkTogBtn('Show Instances', toggleInst);
Simon Hunte5b71752014-11-18 20:06:07 -08001983 //doPanZoom = mkTogBtn('Pan/Zoom', togglePanZoom);
1984 //showTrafficOnHover = mkTogBtn('Show traffic on hover', toggleTrafficHover);
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001985 }
1986
Simon Hunta5e89142014-11-14 07:00:33 -08001987 function instShown() {
1988 return showInstances.classed('active');
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001989 }
Simon Hunta5e89142014-11-14 07:00:33 -08001990 function toggleInst() {
1991 showInstances.classed('active', !instShown());
1992 if (instShown()) {
1993 oiBox.show();
1994 } else {
1995 oiBox.hide();
1996 }
Simon Hunt01095ff2014-11-13 16:37:29 -08001997 }
1998
Simon Huntdeab4322014-11-13 18:49:07 -08001999 function panZoom() {
Simon Hunte5b71752014-11-18 20:06:07 -08002000 return false;
2001 //return doPanZoom.classed('active');
Simon Hunt01095ff2014-11-13 16:37:29 -08002002 }
Simon Hunta5e89142014-11-14 07:00:33 -08002003 function togglePanZoom() {
2004 doPanZoom.classed('active', !panZoom());
2005 }
2006
2007 function trafficHover() {
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -08002008 return hoverEnabled;
Simon Hunta5e89142014-11-14 07:00:33 -08002009 }
2010 function toggleTrafficHover() {
2011 showTrafficOnHover.classed('active', !trafficHover());
2012 }
2013
Simon Hunt7fa116d2014-11-17 14:16:55 -08002014 function loadGlyphs(svg) {
2015 var defs = svg.append('defs');
2016 gly.defBird(defs);
2017 gly.defBullhorn(defs);
2018 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002019
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002020 // ==============================
Simon Hunt142d0032014-11-04 20:13:09 -08002021 // View life-cycle callbacks
Simon Hunt195cb382014-11-03 17:50:51 -08002022
Simon Huntf67722a2014-11-10 09:32:06 -08002023 function preload(view, ctx, flags) {
Simon Hunt142d0032014-11-04 20:13:09 -08002024 var w = view.width(),
2025 h = view.height(),
Simon Huntc7ee0662014-11-05 16:44:37 -08002026 fcfg = config.force,
2027 fpad = fcfg.pad,
2028 forceDim = [w - 2*fpad, h - 2*fpad];
Simon Hunt195cb382014-11-03 17:50:51 -08002029
Simon Hunt142d0032014-11-04 20:13:09 -08002030 // NOTE: view.$div is a D3 selection of the view's div
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002031 var viewBox = '0 0 ' + config.logicalSize + ' ' + config.logicalSize;
2032 svg = view.$div.append('svg').attr('viewBox', viewBox);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002033 setSize(svg, view);
2034
Simon Hunt7fa116d2014-11-17 14:16:55 -08002035 loadGlyphs(svg);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002036
Paul Greysonfcba0e82014-11-13 10:21:16 -08002037 zoomPanContainer = svg.append('g').attr('id', 'zoomPanContainer');
Paul Greysonfcba0e82014-11-13 10:21:16 -08002038 setupZoomPan();
2039
Simon Hunt1a9eff92014-11-07 11:06:34 -08002040 // add blue glow filter to svg layer
Paul Greysonfcba0e82014-11-13 10:21:16 -08002041 d3u.appendGlow(zoomPanContainer);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002042
Simon Huntc7ee0662014-11-05 16:44:37 -08002043 // group for the topology
Paul Greysonfcba0e82014-11-13 10:21:16 -08002044 topoG = zoomPanContainer.append('g')
Simon Huntd3b7d512014-11-12 15:48:41 -08002045 .attr('id', 'topo-G')
Simon Huntc7ee0662014-11-05 16:44:37 -08002046 .attr('transform', fcfg.translate());
2047
Simon Hunte2575b62014-11-18 15:25:53 -08002048 // subgroups for links, link labels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002049 linkG = topoG.append('g').attr('id', 'links');
Simon Hunte2575b62014-11-18 15:25:53 -08002050 linkLabelG = topoG.append('g').attr('id', 'linkLabels');
Simon Huntc7ee0662014-11-05 16:44:37 -08002051 nodeG = topoG.append('g').attr('id', 'nodes');
2052
Simon Hunte2575b62014-11-18 15:25:53 -08002053 // selection of links, linkLabels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002054 link = linkG.selectAll('.link');
Simon Hunte2575b62014-11-18 15:25:53 -08002055 linkLabel = linkLabelG.selectAll('.linkLabel');
Simon Huntc7ee0662014-11-05 16:44:37 -08002056 node = nodeG.selectAll('.node');
2057
Simon Hunt7cd48f32014-11-09 23:42:50 -08002058 function chrg(d) {
2059 return fcfg.charge[d.class] || -12000;
2060 }
Simon Hunt99c13842014-11-06 18:23:12 -08002061 function ldist(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002062 return fcfg.linkDistance[d.type] || 50;
Simon Hunt99c13842014-11-06 18:23:12 -08002063 }
2064 function lstrg(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002065 // 0.0 - 1.0
2066 return fcfg.linkStrength[d.type] || 1.0;
Simon Hunt99c13842014-11-06 18:23:12 -08002067 }
2068
Simon Hunt1a9eff92014-11-07 11:06:34 -08002069 function selectCb(d, self) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002070 selectObject(d, self);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002071 }
2072
2073 function atDragEnd(d, self) {
Simon Hunt56d51852014-11-09 13:03:35 -08002074 // once we've finished moving, pin the node in position
2075 d.fixed = true;
2076 d3.select(self).classed('fixed', true);
2077 if (config.useLiveData) {
Simon Hunt902c9922014-11-11 11:59:31 -08002078 sendUpdateMeta(d);
Simon Hunta255a2c2014-11-13 22:29:35 -08002079 } else {
2080 console.log('Moving node ' + d.id + ' to [' + d.x + ',' + d.y + ']');
Simon Hunt1a9eff92014-11-07 11:06:34 -08002081 }
2082 }
2083
Simon Hunt902c9922014-11-11 11:59:31 -08002084 function sendUpdateMeta(d) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002085 sendMessage('updateMeta', {
2086 id: d.id,
2087 'class': d.class,
Simon Hunt902c9922014-11-11 11:59:31 -08002088 'memento': {
Simon Hunt01095ff2014-11-13 16:37:29 -08002089 x: d.x,
2090 y: d.y
Simon Hunt902c9922014-11-11 11:59:31 -08002091 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002092 });
2093 }
2094
Simon Huntc7ee0662014-11-05 16:44:37 -08002095 // set up the force layout
2096 network.force = d3.layout.force()
2097 .size(forceDim)
2098 .nodes(network.nodes)
2099 .links(network.links)
Simon Hunt7cd48f32014-11-09 23:42:50 -08002100 .gravity(0.4)
2101 .friction(0.7)
2102 .charge(chrg)
Simon Hunt99c13842014-11-06 18:23:12 -08002103 .linkDistance(ldist)
2104 .linkStrength(lstrg)
Simon Huntc7ee0662014-11-05 16:44:37 -08002105 .on('tick', tick);
Simon Hunt195cb382014-11-03 17:50:51 -08002106
Simon Hunt01095ff2014-11-13 16:37:29 -08002107 network.drag = d3u.createDragBehavior(network.force,
Simon Huntdeab4322014-11-13 18:49:07 -08002108 selectCb, atDragEnd, panZoom);
Simon Hunt0c6d4192014-11-12 12:07:10 -08002109
2110 // create mask layer for when we lose connection to server.
Simon Hunta5e89142014-11-14 07:00:33 -08002111 // TODO: this should be part of the framework
Simon Hunt0c6d4192014-11-12 12:07:10 -08002112 mask = view.$div.append('div').attr('id','topo-mask');
2113 para(mask, 'Oops!');
2114 para(mask, 'Web-socket connection to server closed...');
2115 para(mask, 'Try refreshing the page.');
Simon Hunt12ce12e2014-11-15 21:13:19 -08002116
2117 mask.append('svg')
2118 .attr({
2119 id: 'mask-bird',
2120 width: w,
2121 height: h
2122 })
2123 .append('g')
2124 .attr('transform', birdTranslate(w, h))
2125 .style('opacity', 0.3)
2126 .append('use')
2127 .attr({
2128 'xlink:href': '#bird',
2129 width: config.birdDim,
2130 height: config.birdDim,
2131 fill: '#111'
Thomas Vachuska89543292014-11-19 11:28:33 -08002132 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08002133 }
Simon Hunt195cb382014-11-03 17:50:51 -08002134
Simon Hunt01095ff2014-11-13 16:37:29 -08002135 function para(sel, text) {
2136 sel.append('p').text(text);
2137 }
2138
2139
Simon Hunt56d51852014-11-09 13:03:35 -08002140 function load(view, ctx, flags) {
Simon Huntf67722a2014-11-10 09:32:06 -08002141 // resize, in case the window was resized while we were not loaded
2142 resize(view, ctx, flags);
2143
Simon Hunt99c13842014-11-06 18:23:12 -08002144 // cache the view token, so network topo functions can access it
2145 network.view = view;
Simon Hunt56d51852014-11-09 13:03:35 -08002146 config.useLiveData = !flags.local;
2147
2148 if (!config.useLiveData) {
2149 prepareScenario(view, ctx, flags.debug);
2150 }
Simon Hunt99c13842014-11-06 18:23:12 -08002151
2152 // set our radio buttons and key bindings
Simon Hunt9462e8c2014-11-14 17:28:09 -08002153 layerBtnSet = view.setRadio(layerButtons);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002154 view.setKeys(keyDispatch);
Simon Hunt195cb382014-11-03 17:50:51 -08002155
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002156 // patch in our "button bar" for now
2157 // TODO: implement a more official frameworky way of doing this..
2158 addButtonBar(view);
2159
Simon Huntd3b7d512014-11-12 15:48:41 -08002160 // Load map data asynchronously; complete startup after that..
2161 loadGeoJsonData();
Simon Hunta255a2c2014-11-13 22:29:35 -08002162
2163 // start the and timer
2164 var dashIdx = 0;
2165 antTimer = setInterval(function () {
2166 // TODO: figure out how to choose Src-->Dst and Dst-->Src, per link
2167 dashIdx = dashIdx === 0 ? 14 : dashIdx - 2;
2168 d3.selectAll('.animated').style('stroke-dashoffset', dashIdx);
2169 }, 35);
2170 }
2171
2172 function unload(view, ctx, flags) {
2173 if (antTimer) {
2174 clearInterval(antTimer);
2175 antTimer = null;
2176 }
Simon Huntd3b7d512014-11-12 15:48:41 -08002177 }
2178
2179 // TODO: move these to config/state portion of script
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002180 var geoJsonUrl = 'json/map/continental_us.json', // TODO: Paul
Simon Huntd3b7d512014-11-12 15:48:41 -08002181 geoJson;
2182
2183 function loadGeoJsonData() {
2184 d3.json(geoJsonUrl, function (err, data) {
2185 if (err) {
2186 // fall back to USA map background
2187 loadStaticMap();
2188 } else {
2189 geoJson = data;
2190 loadGeoMap();
2191 }
2192
2193 // finally, connect to the server...
2194 if (config.useLiveData) {
2195 webSock.connect();
2196 }
2197 });
2198 }
2199
2200 function showBg() {
2201 return config.options.showBackground ? 'visible' : 'hidden';
2202 }
2203
2204 function loadStaticMap() {
2205 fnTrace('loadStaticMap', config.backgroundUrl);
2206 var w = network.view.width(),
2207 h = network.view.height();
2208
2209 // load the background image
2210 bgImg = svg.insert('svg:image', '#topo-G')
2211 .attr({
2212 id: 'topo-bg',
2213 width: w,
2214 height: h,
2215 'xlink:href': config.backgroundUrl
2216 })
2217 .style({
2218 visibility: showBg()
2219 });
2220 }
2221
2222 function loadGeoMap() {
2223 fnTrace('loadGeoMap', geoJsonUrl);
Simon Huntd3b7d512014-11-12 15:48:41 -08002224
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002225 // extracts the topojson data into geocoordinate-based geometry
2226 var topoData = topojson.feature(geoJson, geoJson.objects.states);
Simon Huntd3b7d512014-11-12 15:48:41 -08002227
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002228 // see: http://bl.ocks.org/mbostock/4707858
2229 geoMapProjection = d3.geo.mercator();
2230 var path = d3.geo.path().projection(geoMapProjection);
Simon Huntd3b7d512014-11-12 15:48:41 -08002231
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002232 geoMapProjection
2233 .scale(1)
2234 .translate([0, 0]);
Simon Huntd3b7d512014-11-12 15:48:41 -08002235
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002236 // [[x1,y1],[x2,y2]]
2237 var b = path.bounds(topoData);
Paul Greysonfcba0e82014-11-13 10:21:16 -08002238 // size map to 95% of minimum dimension to fill space
2239 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 -08002240 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 -08002241
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002242 geoMapProjection
2243 .scale(s)
2244 .translate(t);
2245
Paul Greysonfcba0e82014-11-13 10:21:16 -08002246 bgImg = zoomPanContainer.insert("g", '#topo-G');
Thomas Vachuska89543292014-11-19 11:28:33 -08002247 bgImg.attr('id', 'map').selectAll('path')
2248 .data(topoData.features)
2249 .enter()
2250 .append('path')
2251 .attr('d', path);
Simon Hunt195cb382014-11-03 17:50:51 -08002252 }
2253
Simon Huntf67722a2014-11-10 09:32:06 -08002254 function resize(view, ctx, flags) {
Simon Hunt12ce12e2014-11-15 21:13:19 -08002255 var w = view.width(),
2256 h = view.height();
2257
Simon Hunt934c3ce2014-11-05 11:45:07 -08002258 setSize(svg, view);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002259
2260 d3.select('#mask-bird').attr({ width: w, height: h})
2261 .select('g').attr('transform', birdTranslate(w, h));
Simon Hunt142d0032014-11-04 20:13:09 -08002262 }
2263
Simon Hunt12ce12e2014-11-15 21:13:19 -08002264 function birdTranslate(w, h) {
2265 var bdim = config.birdDim;
2266 return 'translate('+((w-bdim)*.4)+','+((h-bdim)*.1)+')';
2267 }
Simon Hunt142d0032014-11-04 20:13:09 -08002268
2269 // ==============================
2270 // View registration
Simon Hunt195cb382014-11-03 17:50:51 -08002271
Simon Hunt25248912014-11-04 11:25:48 -08002272 onos.ui.addView('topo', {
Simon Hunt142d0032014-11-04 20:13:09 -08002273 preload: preload,
2274 load: load,
Simon Hunta255a2c2014-11-13 22:29:35 -08002275 unload: unload,
Simon Hunt142d0032014-11-04 20:13:09 -08002276 resize: resize
Simon Hunt195cb382014-11-03 17:50:51 -08002277 });
2278
Simon Hunt61d04042014-11-11 17:27:16 -08002279 detailPane = onos.ui.addFloatingPanel('topo-detail');
Simon Hunta5e89142014-11-14 07:00:33 -08002280 oiBox = onos.ui.addFloatingPanel('topo-oibox', 'TL');
Simon Hunt61d04042014-11-11 17:27:16 -08002281
Simon Hunt195cb382014-11-03 17:50:51 -08002282}(ONOS));