blob: 2a36dfd7c60239d8cd85589a947634aa9fb0b6e2 [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 Vachuskaece59ee2014-11-19 19:06:11 -080080 w: 30,
81 h: 30,
82 xoff: -16,
83 yoff: -14
Thomas Vachuska89543292014-11-19 11:28:33 -080084 },
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.
Thomas Vachuskaa3148a72014-11-19 21:38:35 -0800734 link.attr('stroke-width', null)
735 .style('stroke-width', null)
736 .classed('primary secondary animated optical', false);
Simon Hunte2575b62014-11-18 15:25:53 -0800737 // Remove all previous labels.
738 removeLinkLabels();
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800739
Thomas Vachuskaa3148a72014-11-19 21:38:35 -0800740 if (paths.length && !antTimer) {
741 startAntTimer();
742 } else if (!paths.length && antTimer) {
743 stopAntTimer();
744 }
745
Simon Hunte2575b62014-11-18 15:25:53 -0800746 // Now hilight all links in the paths payload, and attach
747 // labels to them, if they are defined.
Simon Hunta255a2c2014-11-13 22:29:35 -0800748 paths.forEach(function (p) {
Simon Hunte2575b62014-11-18 15:25:53 -0800749 var n = p.links.length,
750 i,
751 ldata;
752
753 for (i=0; i<n; i++) {
754 ldata = findLinkById(p.links[i]);
755 if (ldata) {
756 ldata.el.classed(p.class, true);
757 ldata.label = p.labels[i];
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800758 }
Simon Hunte2575b62014-11-18 15:25:53 -0800759 }
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800760 });
Simon Hunte2575b62014-11-18 15:25:53 -0800761 updateLinks();
Simon Huntb53e0682014-11-12 13:32:01 -0800762 }
763
Simon Hunt56d51852014-11-09 13:03:35 -0800764 // ...............................
765
766 function stillToImplement(data) {
767 var p = data.payload;
768 note(data.event, p.id);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800769 network.view.alert('Not yet implemented: "' + data.event + '"');
Simon Hunt56d51852014-11-09 13:03:35 -0800770 }
Simon Hunt99c13842014-11-06 18:23:12 -0800771
772 function unknownEvent(data) {
Simon Hunt50128c02014-11-08 13:36:15 -0800773 network.view.alert('Unknown event type: "' + data.event + '"');
Simon Hunt99c13842014-11-06 18:23:12 -0800774 }
775
776 function handleServerEvent(data) {
777 var fn = eventDispatch[data.event] || unknownEvent;
778 fn(data);
779 }
780
781 // ==============================
Simon Hunt61d04042014-11-11 17:27:16 -0800782 // Out-going messages...
783
Simon Huntb53e0682014-11-12 13:32:01 -0800784 function userFeedback(msg) {
785 // for now, use the alert pane as is. Maybe different alert style in
786 // the future (centered on view; dismiss button?)
787 network.view.alert(msg);
788 }
789
790 function nSel() {
791 return selectOrder.length;
792 }
Simon Hunt61d04042014-11-11 17:27:16 -0800793 function getSel(idx) {
794 return selections[selectOrder[idx]];
795 }
Simon Huntb53e0682014-11-12 13:32:01 -0800796 function getSelId(idx) {
797 return getSel(idx).obj.id;
798 }
799 function allSelectionsClass(cls) {
800 for (var i=0, n=nSel(); i<n; i++) {
801 if (getSel(i).obj.class !== cls) {
802 return false;
803 }
804 }
805 return true;
806 }
Simon Hunt61d04042014-11-11 17:27:16 -0800807
Simon Hunt61d04042014-11-11 17:27:16 -0800808 // request details for the selected element
Simon Huntd72bc702014-11-13 18:38:04 -0800809 // invoked from selection of a single node.
Simon Hunt61d04042014-11-11 17:27:16 -0800810 function requestDetails() {
811 var data = getSel(0).obj,
812 payload = {
813 id: data.id,
814 class: data.class
815 };
816 sendMessage('requestDetails', payload);
817 }
818
Simon Huntd72bc702014-11-13 18:38:04 -0800819 function addIntentAction() {
820 sendMessage('addHostIntent', {
821 one: getSelId(0),
Thomas Vachuska82f2c622014-11-17 12:23:18 -0800822 two: getSelId(1),
823 ids: [ getSelId(0), getSelId(1) ]
Simon Huntd72bc702014-11-13 18:38:04 -0800824 });
825 }
826
827 function showTrafficAction() {
828 // if nothing is hovered over, and nothing selected, send cancel request
829 if (!hovered && nSel() === 0) {
830 sendMessage('cancelTraffic', {});
831 return;
832 }
833
834 // NOTE: hover is only populated if "show traffic on hover" is
835 // toggled on, and the item hovered is a host...
836 var hoverId = (trafficHover() && hovered && hovered.class === 'host')
837 ? hovered.id : '';
838 sendMessage('requestTraffic', {
839 ids: selectOrder,
840 hover: hoverId
841 });
842 }
843
844
Simon Hunt61d04042014-11-11 17:27:16 -0800845 // ==============================
Simon Hunta5e89142014-11-14 07:00:33 -0800846 // onos instance panel functions
847
848 function updateInstances() {
849 var onoses = oiBox.el.selectAll('.onosInst')
850 .data(onosOrder, function (d) { return d.id; });
851
852 // operate on existing onoses if necessary
Simon Huntfcfb46c2014-11-19 12:53:38 -0800853 onoses.classed('online', function (d) { return d.online; });
Simon Hunta5e89142014-11-14 07:00:33 -0800854
855 var entering = onoses.enter()
856 .append('div')
857 .attr('class', 'onosInst')
858 .classed('online', function (d) { return d.online; })
Simon Hunt9c15eca2014-11-15 18:37:59 -0800859 .on('click', clickInst);
860
861 entering.each(function (d, i) {
862 var el = d3.select(this),
863 img;
864
Thomas Vachuskacd2920c2014-11-19 14:49:55 -0800865 $('<img src="img/node.png">').appendTo(el);
Simon Hunt9c15eca2014-11-15 18:37:59 -0800866 img = el.select('img')
867 .attr({
Simon Huntfcfb46c2014-11-19 12:53:38 -0800868 width: 30
Simon Hunt9c15eca2014-11-15 18:37:59 -0800869 });
870
871 $('<div>').attr('class', 'onosTitle').text(d.id).appendTo(el);
872
873 // is the UI attached to this instance?
874 // TODO: need uiAttached boolean in instance data
875 //if (d.uiAttached) {
876 if (i === 0) {
877 $('<img src="img/ui.png">').attr('class','ui').appendTo(el);
878 }
879 });
Simon Hunta5e89142014-11-14 07:00:33 -0800880
881 // operate on existing + new onoses here
882
883 // the departed...
884 var exiting = onoses.exit()
885 .transition()
886 .style('opacity', 0)
887 .remove();
888 }
889
Simon Hunt9462e8c2014-11-14 17:28:09 -0800890 function clickInst(d) {
891 var el = d3.select(this),
892 aff = el.classed('affinity');
893 if (!aff) {
894 setAffinity(el, d);
895 } else {
896 cancelAffinity();
897 }
898 }
899
900 function setAffinity(el, d) {
901 d3.selectAll('.onosInst')
902 .classed('mastership', true)
903 .classed('affinity', false);
904 el.classed('affinity', true);
905
906 suppressLayers(true);
907 node.each(function (n) {
908 if (n.master === d.id) {
909 n.el.classed('suppressed', false);
910 }
911 });
912 oiShowMaster = true;
913 }
914
915 function cancelAffinity() {
916 d3.selectAll('.onosInst')
917 .classed('mastership affinity', false);
918 restoreLayerState();
919 oiShowMaster = false;
920 }
921
Simon Hunta5e89142014-11-14 07:00:33 -0800922 // ==============================
Simon Hunt99c13842014-11-06 18:23:12 -0800923 // force layout modification functions
924
925 function translate(x, y) {
926 return 'translate(' + x + ',' + y + ')';
927 }
928
Simon Hunte2575b62014-11-18 15:25:53 -0800929 function rotate(deg) {
930 return 'rotate(' + deg + ')';
931 }
932
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800933 function missMsg(what, id) {
934 return '\n[' + what + '] "' + id + '" missing ';
935 }
936
937 function linkEndPoints(srcId, dstId) {
938 var srcNode = network.lookup[srcId],
939 dstNode = network.lookup[dstId],
940 sMiss = !srcNode ? missMsg('src', srcId) : '',
941 dMiss = !dstNode ? missMsg('dst', dstId) : '';
942
943 if (sMiss || dMiss) {
944 logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
945 return null;
946 }
947 return {
948 source: srcNode,
949 target: dstNode,
950 x1: srcNode.x,
951 y1: srcNode.y,
952 x2: dstNode.x,
953 y2: dstNode.y
954 };
955 }
956
Simon Hunt56d51852014-11-09 13:03:35 -0800957 function createHostLink(host) {
958 var src = host.id,
959 dst = host.cp.device,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800960 id = host.ingress,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800961 lnk = linkEndPoints(src, dst);
Simon Hunt56d51852014-11-09 13:03:35 -0800962
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800963 if (!lnk) {
Simon Hunt56d51852014-11-09 13:03:35 -0800964 return null;
965 }
966
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800967 // Synthesize link ...
968 $.extend(lnk, {
Simon Hunt8257f4c2014-11-16 19:34:54 -0800969 key: id,
Simon Hunt56d51852014-11-09 13:03:35 -0800970 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -0800971
972 type: function () { return 'hostLink'; },
973 // TODO: ideally, we should see if our edge switch is online...
974 online: function () { return true; },
975 linkWidth: function () { return 1; }
Simon Hunt7cd48f32014-11-09 23:42:50 -0800976 });
Simon Hunt99c13842014-11-06 18:23:12 -0800977 return lnk;
978 }
979
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800980 function createLink(link) {
981 var lnk = linkEndPoints(link.src, link.dst),
982 type = link.type;
983
984 if (!lnk) {
985 return null;
986 }
987
Simon Hunt8257f4c2014-11-16 19:34:54 -0800988 $.extend(lnk, {
989 key: link.id,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800990 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -0800991 fromSource: link,
992
993 // functions to aggregate dual link state
994 type: function () {
995 var s = lnk.fromSource,
996 t = lnk.fromTarget;
997 return (s && s.type) || (t && t.type) || defaultLinkType;
998 },
999 online: function () {
1000 var s = lnk.fromSource,
1001 t = lnk.fromTarget;
1002 return (s && s.online) || (t && t.online);
1003 },
1004 linkWidth: function () {
1005 var s = lnk.fromSource,
1006 t = lnk.fromTarget,
1007 ws = (s && s.linkWidth) || 0,
1008 wt = (t && t.linkWidth) || 0;
1009 return Math.max(ws, wt);
1010 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001011 });
1012 return lnk;
Simon Hunt1a9eff92014-11-07 11:06:34 -08001013 }
1014
Simon Hunte2575b62014-11-18 15:25:53 -08001015 function removeLinkLabels() {
1016 network.links.forEach(function (d) {
1017 d.label = '';
1018 });
1019 }
1020
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001021 var widthRatio = 1.4,
1022 linkScale = d3.scale.linear()
1023 .domain([1, 12])
1024 .range([widthRatio, 12 * widthRatio])
1025 .clamp(true);
1026
Simon Hunt99c13842014-11-06 18:23:12 -08001027 function updateLinks() {
1028 link = linkG.selectAll('.link')
Simon Hunt8257f4c2014-11-16 19:34:54 -08001029 .data(network.links, function (d) { return d.key; });
Simon Hunt99c13842014-11-06 18:23:12 -08001030
1031 // operate on existing links, if necessary
1032 // link .foo() .bar() ...
1033
1034 // operate on entering links:
1035 var entering = link.enter()
1036 .append('line')
1037 .attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001038 x1: function (d) { return d.x1; },
1039 y1: function (d) { return d.y1; },
1040 x2: function (d) { return d.x2; },
1041 y2: function (d) { return d.y2; },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001042 stroke: config.topo.linkInColor,
1043 'stroke-width': config.topo.linkInWidth
Simon Hunt99c13842014-11-06 18:23:12 -08001044 });
1045
1046 // augment links
Simon Hunt7cd48f32014-11-09 23:42:50 -08001047 entering.each(function (d) {
1048 var link = d3.select(this);
1049 // provide ref to element selection from backing data....
1050 d.el = link;
Simon Hunt8257f4c2014-11-16 19:34:54 -08001051 restyleLinkElement(d);
Simon Hunt7cd48f32014-11-09 23:42:50 -08001052 });
Thomas Vachuska4830d392014-11-09 17:09:56 -08001053
1054 // operate on both existing and new links, if necessary
1055 //link .foo() .bar() ...
1056
Simon Hunte2575b62014-11-18 15:25:53 -08001057 // apply or remove labels
1058 var labelData = getLabelData();
1059 applyLinkLabels(labelData);
1060
Thomas Vachuska4830d392014-11-09 17:09:56 -08001061 // operate on exiting links:
Thomas Vachuska4830d392014-11-09 17:09:56 -08001062 link.exit()
Simon Hunt13bf9c82014-11-18 07:26:44 -08001063 .attr('stroke-dasharray', '3, 3')
1064 .style('opacity', 0.5)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001065 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -08001066 .duration(1500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001067 .attr({
Simon Hunt13bf9c82014-11-18 07:26:44 -08001068 'stroke-dasharray': '3, 12',
1069 stroke: config.topo.linkOutColor,
1070 'stroke-width': config.topo.linkOutWidth
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001071 })
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001072 .style('opacity', 0.0)
Thomas Vachuska4830d392014-11-09 17:09:56 -08001073 .remove();
Simon Hunte2575b62014-11-18 15:25:53 -08001074
1075 // NOTE: invoke a single tick to force the labels to position
1076 // onto their links.
1077 tick();
1078 }
1079
1080 function getLabelData() {
1081 // create the backing data for showing labels..
1082 var data = [];
1083 link.each(function (d) {
1084 if (d.label) {
1085 data.push({
1086 id: 'lab-' + d.key,
1087 key: d.key,
1088 label: d.label,
1089 ldata: d
1090 });
1091 }
1092 });
1093 return data;
1094 }
1095
1096 var linkLabelOffset = '0.3em';
1097
1098 function applyLinkLabels(data) {
1099 var entering;
1100
1101 linkLabel = linkLabelG.selectAll('.linkLabel')
1102 .data(data, function (d) { return d.id; });
1103
Simon Hunt56a2ea42014-11-19 12:39:31 -08001104 // for elements already existing, we need to update the text
1105 // and adjust the rectangle size to fit
1106 linkLabel.each(function (d) {
1107 var el = d3.select(this),
1108 rect = el.select('rect'),
1109 text = el.select('text');
1110 text.text(d.label);
1111 rect.attr(rectAroundText(el));
1112 });
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -08001113
Simon Hunte2575b62014-11-18 15:25:53 -08001114 entering = linkLabel.enter().append('g')
1115 .classed('linkLabel', true)
1116 .attr('id', function (d) { return d.id; });
1117
1118 entering.each(function (d) {
1119 var el = d3.select(this),
1120 rect,
1121 text,
1122 parms = {
1123 x1: d.ldata.x1,
1124 y1: d.ldata.y1,
1125 x2: d.ldata.x2,
1126 y2: d.ldata.y2
1127 };
1128
1129 d.el = el;
1130 rect = el.append('rect');
1131 text = el.append('text').text(d.label);
1132 rect.attr(rectAroundText(el));
1133 text.attr('dy', linkLabelOffset);
1134
1135 el.attr('transform', transformLabel(parms));
1136 });
1137
1138 // Remove any links that are no longer required.
1139 linkLabel.exit().remove();
1140 }
1141
1142 function rectAroundText(el) {
1143 var text = el.select('text'),
1144 box = text.node().getBBox();
1145
1146 // translate the bbox so that it is centered on [x,y]
1147 box.x = -box.width / 2;
1148 box.y = -box.height / 2;
1149
1150 // add padding
1151 box.x -= 1;
1152 box.width += 2;
1153 return box;
1154 }
1155
1156 function transformLabel(p) {
1157 var dx = p.x2 - p.x1,
1158 dy = p.y2 - p.y1,
1159 xMid = dx/2 + p.x1,
1160 yMid = dy/2 + p.y1;
1161 //length = Math.sqrt(dx*dx + dy*dy),
1162 //rads = Math.asin(dy/length),
1163 //degs = rads / (Math.PI*2) * 360;
1164
1165 return translate(xMid, yMid);
1166
1167 // TODO: consider making label parallel to line
1168 //return [
1169 // translate(xMid, yMid),
1170 // rotate(degs),
1171 // translate(0, 8)
1172 //].join('');
Simon Hunt99c13842014-11-06 18:23:12 -08001173 }
1174
1175 function createDeviceNode(device) {
1176 // start with the object as is
1177 var node = device,
Simon Huntbb282f52014-11-10 11:08:19 -08001178 type = device.type,
1179 svgCls = type ? 'node device ' + type : 'node device';
Simon Hunt99c13842014-11-06 18:23:12 -08001180
1181 // Augment as needed...
1182 node.class = 'device';
Simon Huntbb282f52014-11-10 11:08:19 -08001183 node.svgClass = device.online ? svgCls + ' online' : svgCls;
Simon Hunt99c13842014-11-06 18:23:12 -08001184 positionNode(node);
1185
1186 // cache label array length
1187 network.deviceLabelCount = device.labels.length;
Simon Hunt99c13842014-11-06 18:23:12 -08001188 return node;
1189 }
1190
Simon Hunt56d51852014-11-09 13:03:35 -08001191 function createHostNode(host) {
1192 // start with the object as is
1193 var node = host;
1194
1195 // Augment as needed...
1196 node.class = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001197 if (!node.type) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08001198 node.type = 'endstation';
1199 }
Simon Hunt7fa116d2014-11-17 14:16:55 -08001200 node.svgClass = 'node host ' + node.type;
Simon Hunt56d51852014-11-09 13:03:35 -08001201 positionNode(node);
1202
1203 // cache label array length
1204 network.hostLabelCount = host.labels.length;
Simon Hunt56d51852014-11-09 13:03:35 -08001205 return node;
1206 }
1207
Simon Hunt99c13842014-11-06 18:23:12 -08001208 function positionNode(node) {
1209 var meta = node.metaUi,
Simon Huntac9e24f2014-11-12 10:12:21 -08001210 x = meta && meta.x,
1211 y = meta && meta.y,
1212 xy;
Simon Hunt99c13842014-11-06 18:23:12 -08001213
Simon Huntac9e24f2014-11-12 10:12:21 -08001214 // If we have [x,y] already, use that...
Simon Hunt99c13842014-11-06 18:23:12 -08001215 if (x && y) {
1216 node.fixed = true;
Simon Huntac9e24f2014-11-12 10:12:21 -08001217 node.x = x;
1218 node.y = y;
1219 return;
Simon Hunt99c13842014-11-06 18:23:12 -08001220 }
Simon Huntac9e24f2014-11-12 10:12:21 -08001221
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001222 var location = node.location;
1223 if (location && location.type === 'latlng') {
1224 var coord = geoMapProjection([location.lng, location.lat]);
1225 node.fixed = true;
1226 node.x = coord[0];
1227 node.y = coord[1];
1228 return;
1229 }
1230
Simon Huntac9e24f2014-11-12 10:12:21 -08001231 // Note: Placing incoming unpinned nodes at exactly the same point
1232 // (center of the view) causes them to explode outwards when
1233 // the force layout kicks in. So, we spread them out a bit
1234 // initially, to provide a more serene layout convergence.
1235 // Additionally, if the node is a host, we place it near
1236 // the device it is connected to.
1237
1238 function spread(s) {
1239 return Math.floor((Math.random() * s) - s/2);
1240 }
1241
1242 function randDim(dim) {
1243 return dim / 2 + spread(dim * 0.7071);
1244 }
1245
1246 function rand() {
1247 return {
1248 x: randDim(network.view.width()),
1249 y: randDim(network.view.height())
1250 };
1251 }
1252
1253 function near(node) {
1254 var min = 12,
1255 dx = spread(12),
1256 dy = spread(12);
1257 return {
1258 x: node.x + min + dx,
1259 y: node.y + min + dy
1260 };
1261 }
1262
1263 function getDevice(cp) {
1264 var d = network.lookup[cp.device];
1265 return d || rand();
1266 }
1267
1268 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
1269 $.extend(node, xy);
Simon Hunt99c13842014-11-06 18:23:12 -08001270 }
1271
Thomas Vachuska89543292014-11-19 11:28:33 -08001272 function iconUrl(d) {
1273 return 'img/' + d.type + '.png';
Simon Hunt99c13842014-11-06 18:23:12 -08001274 }
1275
1276 // returns the newly computed bounding box of the rectangle
1277 function adjustRectToFitText(n) {
1278 var text = n.select('text'),
1279 box = text.node().getBBox(),
1280 lab = config.labels;
1281
1282 text.attr('text-anchor', 'middle')
1283 .attr('y', '-0.8em')
1284 .attr('x', lab.imgPad/2);
1285
1286 // translate the bbox so that it is centered on [x,y]
1287 box.x = -box.width / 2;
1288 box.y = -box.height / 2;
1289
1290 // add padding
1291 box.x -= (lab.padLR + lab.imgPad/2);
1292 box.width += lab.padLR * 2 + lab.imgPad;
1293 box.y -= lab.padTB;
1294 box.height += lab.padTB * 2;
1295
1296 return box;
1297 }
1298
Simon Hunt1a9eff92014-11-07 11:06:34 -08001299 function mkSvgClass(d) {
1300 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
1301 }
1302
Simon Hunt7cd48f32014-11-09 23:42:50 -08001303 function hostLabel(d) {
1304 var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0;
1305 return d.labels[idx];
1306 }
1307 function deviceLabel(d) {
1308 var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0;
1309 return d.labels[idx];
1310 }
1311 function niceLabel(label) {
1312 return (label && label.trim()) ? label : '.';
1313 }
1314
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001315 function updateDeviceLabel(d) {
1316 var label = niceLabel(deviceLabel(d)),
1317 node = d.el,
1318 box;
1319
1320 node.select('text')
1321 .text(label)
1322 .style('opacity', 0)
1323 .transition()
1324 .style('opacity', 1);
1325
1326 box = adjustRectToFitText(node);
1327
1328 node.select('rect')
1329 .transition()
1330 .attr(box);
1331
Simon Hunt56a2ea42014-11-19 12:39:31 -08001332 node.select('rect.iconUnderlay')
Thomas Vachuska89543292014-11-19 11:28:33 -08001333 .transition()
1334 .attr('x', box.x + config.icons.xoff)
1335 .attr('y', box.y + config.icons.yoff);
Simon Hunt56a2ea42014-11-19 12:39:31 -08001336
1337 node.select('image')
1338 .transition()
1339 .attr('x', box.x + config.icons.xoff + 2)
1340 .attr('y', box.y + config.icons.yoff + 2);
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001341 }
1342
1343 function updateHostLabel(d) {
1344 var label = hostLabel(d),
1345 host = d.el;
1346
1347 host.select('text').text(label);
1348 }
1349
Simon Huntbb282f52014-11-10 11:08:19 -08001350 function updateDeviceState(nodeData) {
1351 nodeData.el.classed('online', nodeData.online);
1352 updateDeviceLabel(nodeData);
1353 // TODO: review what else might need to be updated
1354 }
1355
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001356 function updateLinkState(linkData) {
1357 updateLinkWidth(linkData);
Thomas Vachuskabadb93f2014-11-15 23:51:17 -08001358 linkData.el.classed('inactive', !linkData.online);
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001359 // TODO: review what else might need to be updated
1360 // update label, if showing
1361 }
1362
Simon Huntbb282f52014-11-10 11:08:19 -08001363 function updateHostState(hostData) {
1364 updateHostLabel(hostData);
1365 // TODO: review what else might need to be updated
1366 }
1367
Simon Hunt6ac93f32014-11-13 12:17:27 -08001368 function nodeMouseOver(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001369 hovered = d;
Simon Huntd72bc702014-11-13 18:38:04 -08001370 if (trafficHover() && d.class === 'host') {
1371 showTrafficAction();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001372 }
1373 }
1374
1375 function nodeMouseOut(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001376 hovered = null;
Simon Huntd72bc702014-11-13 18:38:04 -08001377 if (trafficHover() && d.class === 'host') {
1378 showTrafficAction();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001379 }
1380 }
Simon Huntbb282f52014-11-10 11:08:19 -08001381
Thomas Vachuska89543292014-11-19 11:28:33 -08001382 function addHostIcon(node, radius, iconId) {
1383 var dim = radius * 1.5,
1384 xlate = -dim / 2;
1385
1386 node.append('svg:image')
1387 .attr('transform', translate(xlate,xlate))
1388 .attr('xlink:href', 'img/' + iconId + '.png')
1389 .attr('width', dim)
1390 .attr('height', dim);
1391 }
1392
Simon Hunt99c13842014-11-06 18:23:12 -08001393 function updateNodes() {
1394 node = nodeG.selectAll('.node')
1395 .data(network.nodes, function (d) { return d.id; });
1396
1397 // operate on existing nodes, if necessary
Simon Hunt7cd48f32014-11-09 23:42:50 -08001398 // update host labels
Simon Hunt99c13842014-11-06 18:23:12 -08001399 //node .foo() .bar() ...
1400
1401 // operate on entering nodes:
1402 var entering = node.enter()
1403 .append('g')
1404 .attr({
1405 id: function (d) { return safeId(d.id); },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001406 class: mkSvgClass,
Simon Hunt99c13842014-11-06 18:23:12 -08001407 transform: function (d) { return translate(d.x, d.y); },
1408 opacity: 0
1409 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08001410 .call(network.drag)
Simon Hunt6ac93f32014-11-13 12:17:27 -08001411 .on('mouseover', nodeMouseOver)
1412 .on('mouseout', nodeMouseOut)
Simon Hunt99c13842014-11-06 18:23:12 -08001413 .transition()
1414 .attr('opacity', 1);
1415
1416 // augment device nodes...
1417 entering.filter('.device').each(function (d) {
1418 var node = d3.select(this),
Thomas Vachuska89543292014-11-19 11:28:33 -08001419 icon = iconUrl(d),
Simon Hunt7cd48f32014-11-09 23:42:50 -08001420 label = niceLabel(deviceLabel(d)),
Simon Hunt99c13842014-11-06 18:23:12 -08001421 box;
1422
Simon Hunt7cd48f32014-11-09 23:42:50 -08001423 // provide ref to element from backing data....
1424 d.el = node;
1425
Simon Hunt99c13842014-11-06 18:23:12 -08001426 node.append('rect')
1427 .attr({
1428 'rx': 5,
1429 'ry': 5
1430 });
1431
1432 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001433 .text(label)
Simon Hunt99c13842014-11-06 18:23:12 -08001434 .attr('dy', '1.1em');
1435
1436 box = adjustRectToFitText(node);
1437
1438 node.select('rect')
1439 .attr(box);
1440
Thomas Vachuska89543292014-11-19 11:28:33 -08001441 if (icon) {
1442 var cfg = config.icons;
1443 node.append('rect')
Simon Hunt99c13842014-11-06 18:23:12 -08001444 .attr({
Simon Hunt56a2ea42014-11-19 12:39:31 -08001445 class: 'iconUnderlay',
Thomas Vachuska89543292014-11-19 11:28:33 -08001446 x: box.x + config.icons.xoff,
1447 y: box.y + config.icons.yoff,
1448 width: cfg.w,
1449 height: cfg.h,
1450 rx: 4
1451 }).style({
1452 stroke: '#000',
1453 fill: '#ddd'
1454 });
1455 node.append('svg:image')
1456 .attr({
1457 x: box.x + config.icons.xoff + 2,
1458 y: box.y + config.icons.yoff + 2,
1459 width: cfg.w - 4,
1460 height: cfg.h - 4,
1461 'xlink:href': icon
Simon Hunt99c13842014-11-06 18:23:12 -08001462 });
1463 }
1464
1465 // debug function to show the modelled x,y coordinates of nodes...
1466 if (debug('showNodeXY')) {
1467 node.select('rect').attr('fill-opacity', 0.5);
1468 node.append('circle')
1469 .attr({
1470 class: 'debug',
1471 cx: 0,
1472 cy: 0,
1473 r: '3px'
1474 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001475 }
1476 });
Simon Hunt934c3ce2014-11-05 11:45:07 -08001477
Thomas Vachuska89543292014-11-19 11:28:33 -08001478 // TODO: better place for this configuration state
1479 var defaultHostRadius = 9,
1480 hostRadius = {
1481 bgpSpeaker: 14,
1482 router: 14,
1483 host: 14
1484 },
1485 hostIcon = {
1486 bgpSpeaker: 'bgpSpeaker',
1487 router: 'router',
1488 host: 'host'
1489 };
1490
1491
Simon Hunt56d51852014-11-09 13:03:35 -08001492 // augment host nodes...
1493 entering.filter('.host').each(function (d) {
1494 var node = d3.select(this),
Thomas Vachuska89543292014-11-19 11:28:33 -08001495 r = hostRadius[d.type] || defaultHostRadius,
1496 textDy = r + 10,
1497 icon = hostIcon[d.type];
Simon Hunt56d51852014-11-09 13:03:35 -08001498
Simon Hunt7cd48f32014-11-09 23:42:50 -08001499 // provide ref to element from backing data....
1500 d.el = node;
1501
Thomas Vachuska89543292014-11-19 11:28:33 -08001502 node.append('circle')
1503 .attr('r', r);
Simon Hunt7fa116d2014-11-17 14:16:55 -08001504
Thomas Vachuska89543292014-11-19 11:28:33 -08001505 if (icon) {
1506 addHostIcon(node, r, icon);
Simon Hunt7fa116d2014-11-17 14:16:55 -08001507 }
Simon Hunt56d51852014-11-09 13:03:35 -08001508
Paul Greyson29cd58f2014-11-18 13:14:57 -08001509
Simon Hunt56d51852014-11-09 13:03:35 -08001510 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001511 .text(hostLabel)
Thomas Vachuska89543292014-11-19 11:28:33 -08001512 .attr('dy', textDy)
Simon Hunt7cd48f32014-11-09 23:42:50 -08001513 .attr('text-anchor', 'middle');
Simon Hunt56d51852014-11-09 13:03:35 -08001514
1515 // debug function to show the modelled x,y coordinates of nodes...
1516 if (debug('showNodeXY')) {
1517 node.select('circle').attr('fill-opacity', 0.5);
1518 node.append('circle')
1519 .attr({
1520 class: 'debug',
1521 cx: 0,
1522 cy: 0,
1523 r: '3px'
1524 });
1525 }
1526 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001527
Simon Hunt99c13842014-11-06 18:23:12 -08001528 // operate on both existing and new nodes, if necessary
1529 //node .foo() .bar() ...
Simon Huntc7ee0662014-11-05 16:44:37 -08001530
Simon Hunt99c13842014-11-06 18:23:12 -08001531 // operate on exiting nodes:
Simon Huntea80eb42014-11-11 13:46:57 -08001532 // Note that the node is removed after 2 seconds.
1533 // Sub element animations should be shorter than 2 seconds.
1534 var exiting = node.exit()
Simon Hunt44031102014-11-11 13:20:36 -08001535 .transition()
1536 .duration(2000)
Simon Huntea80eb42014-11-11 13:46:57 -08001537 .style('opacity', 0)
Simon Hunt99c13842014-11-06 18:23:12 -08001538 .remove();
Simon Huntea80eb42014-11-11 13:46:57 -08001539
1540 // host node exits....
1541 exiting.filter('.host').each(function (d) {
1542 var node = d3.select(this);
1543
1544 node.select('text')
1545 .style('opacity', 0.5)
1546 .transition()
1547 .duration(1000)
1548 .style('opacity', 0);
1549 // note, leave <g>.remove to remove this element
1550
Thomas Vachuska89543292014-11-19 11:28:33 -08001551 node.select('circle')
1552 .style('stroke-fill', '#555')
1553 .style('fill', '#888')
Simon Huntea80eb42014-11-11 13:46:57 -08001554 .style('opacity', 0.5)
1555 .transition()
1556 .duration(1500)
1557 .attr('r', 0);
1558 // note, leave <g>.remove to remove this element
1559
1560 });
1561
1562 // TODO: device node exits
Simon Huntc7ee0662014-11-05 16:44:37 -08001563 }
1564
Simon Hunt8257f4c2014-11-16 19:34:54 -08001565 function find(key, array) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001566 for (var idx = 0, n = array.length; idx < n; idx++) {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001567 if (array[idx].key === key) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001568 return idx;
1569 }
1570 }
1571 return -1;
1572 }
1573
1574 function removeLinkElement(linkData) {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001575 var idx = find(linkData.key, network.links),
1576 removed;
1577 if (idx >=0) {
1578 // remove from links array
1579 removed = network.links.splice(idx, 1);
1580 // remove from lookup cache
1581 delete network.lookup[removed[0].key];
1582 updateLinks();
1583 network.force.resume();
1584 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001585 }
Simon Huntc7ee0662014-11-05 16:44:37 -08001586
Simon Hunt44031102014-11-11 13:20:36 -08001587 function removeHostElement(hostData) {
1588 // first, remove associated hostLink...
1589 removeLinkElement(hostData.linkData);
1590
1591 // remove from lookup cache
1592 delete network.lookup[hostData.id];
1593 // remove from nodes array
1594 var idx = find(hostData.id, network.nodes);
1595 network.nodes.splice(idx, 1);
1596 // remove from SVG
1597 updateNodes();
1598 network.force.resume();
1599 }
1600
1601
Simon Huntc7ee0662014-11-05 16:44:37 -08001602 function tick() {
1603 node.attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001604 transform: function (d) { return translate(d.x, d.y); }
Simon Huntc7ee0662014-11-05 16:44:37 -08001605 });
1606
1607 link.attr({
1608 x1: function (d) { return d.source.x; },
1609 y1: function (d) { return d.source.y; },
1610 x2: function (d) { return d.target.x; },
1611 y2: function (d) { return d.target.y; }
1612 });
Simon Hunte2575b62014-11-18 15:25:53 -08001613
1614 linkLabel.each(function (d) {
1615 var el = d3.select(this);
1616 var lnk = findLinkById(d.key),
1617 parms = {
1618 x1: lnk.source.x,
1619 y1: lnk.source.y,
1620 x2: lnk.target.x,
1621 y2: lnk.target.y
1622 };
1623 el.attr('transform', transformLabel(parms));
1624 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001625 }
Simon Hunt934c3ce2014-11-05 11:45:07 -08001626
1627 // ==============================
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001628 // Web-Socket for live data
1629
1630 function webSockUrl() {
1631 return document.location.toString()
1632 .replace(/\#.*/, '')
1633 .replace('http://', 'ws://')
1634 .replace('https://', 'wss://')
1635 .replace('index2.html', config.webSockUrl);
1636 }
1637
1638 webSock = {
1639 ws : null,
1640
1641 connect : function() {
1642 webSock.ws = new WebSocket(webSockUrl());
1643
1644 webSock.ws.onopen = function() {
Simon Hunt0c6d4192014-11-12 12:07:10 -08001645 noWebSock(false);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001646 };
1647
1648 webSock.ws.onmessage = function(m) {
1649 if (m.data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001650 wsTraceRx(m.data);
Thomas Vachuskad472c6e2014-11-07 19:11:05 -08001651 handleServerEvent(JSON.parse(m.data));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001652 }
1653 };
1654
1655 webSock.ws.onclose = function(m) {
1656 webSock.ws = null;
Simon Hunt0c6d4192014-11-12 12:07:10 -08001657 noWebSock(true);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001658 };
1659 },
1660
1661 send : function(text) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001662 if (text != null) {
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001663 webSock._send(text);
1664 }
1665 },
1666
1667 _send : function(message) {
1668 if (webSock.ws) {
1669 webSock.ws.send(message);
Simon Hunta255a2c2014-11-13 22:29:35 -08001670 } else if (config.useLiveData) {
Simon Hunt56d51852014-11-09 13:03:35 -08001671 network.view.alert('no web socket open\n\n' + message);
Simon Hunta255a2c2014-11-13 22:29:35 -08001672 } else {
1673 console.log('WS Send: ' + JSON.stringify(message));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001674 }
1675 }
1676
1677 };
1678
Simon Hunt0c6d4192014-11-12 12:07:10 -08001679 function noWebSock(b) {
1680 mask.style('display',b ? 'block' : 'none');
1681 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001682
1683 function sendMessage(evType, payload) {
1684 var toSend = {
Simon Huntbb282f52014-11-10 11:08:19 -08001685 event: evType,
1686 sid: ++sid,
1687 payload: payload
1688 },
1689 asText = JSON.stringify(toSend);
1690 wsTraceTx(asText);
1691 webSock.send(asText);
Simon Huntc76ae892014-11-18 17:31:51 -08001692
1693 // Temporary measure for debugging UI behavior ...
1694 if (!config.useLiveData) {
1695 handleTestSend(toSend);
1696 }
Simon Huntbb282f52014-11-10 11:08:19 -08001697 }
1698
1699 function wsTraceTx(msg) {
1700 wsTrace('tx', msg);
1701 }
1702 function wsTraceRx(msg) {
1703 wsTrace('rx', msg);
1704 }
1705 function wsTrace(rxtx, msg) {
Simon Huntbb282f52014-11-10 11:08:19 -08001706 console.log('[' + rxtx + '] ' + msg);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001707 }
1708
Simon Huntc76ae892014-11-18 17:31:51 -08001709 // NOTE: Temporary hardcoded example for showing detail pane
1710 // while we fine-
1711 // Probably should not merge this change...
1712 function handleTestSend(msg) {
1713 if (msg.event === 'requestDetails') {
1714 showDetails({
1715 event: 'showDetails',
1716 sid: 1001,
1717 payload: {
1718 "id": "of:0000ffffffffff09",
1719 "type": "roadm",
1720 "propOrder": [
1721 "Name",
1722 "Vendor",
1723 "H/W Version",
1724 "S/W Version",
1725 "-",
1726 "Latitude",
1727 "Longitude",
1728 "Ports"
1729 ],
1730 "props": {
1731 "Name": null,
1732 "Vendor": "Linc",
1733 "H/W Version": "OE",
1734 "S/W Version": "?",
1735 "-": "",
1736 "Latitude": "40.8",
1737 "Longitude": "73.1",
1738 "Ports": "2"
1739 }
1740 }
1741 });
1742 }
1743 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001744
1745 // ==============================
1746 // Selection stuff
1747
1748 function selectObject(obj, el) {
1749 var n,
Simon Hunt01095ff2014-11-13 16:37:29 -08001750 srcEv = d3.event.sourceEvent,
1751 meta = srcEv.metaKey,
1752 shift = srcEv.shiftKey;
1753
Simon Huntdeab4322014-11-13 18:49:07 -08001754 if ((panZoom() && !meta) || (!panZoom() && meta)) {
Simon Hunt01095ff2014-11-13 16:37:29 -08001755 return;
1756 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001757
1758 if (el) {
1759 n = d3.select(el);
1760 } else {
1761 node.each(function(d) {
1762 if (d == obj) {
1763 n = d3.select(el = this);
1764 }
1765 });
1766 }
1767 if (!n) return;
1768
Simon Hunt01095ff2014-11-13 16:37:29 -08001769 if (shift && n.classed('selected')) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001770 deselectObject(obj.id);
Simon Hunt61d04042014-11-11 17:27:16 -08001771 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001772 return;
1773 }
1774
Simon Hunt01095ff2014-11-13 16:37:29 -08001775 if (!shift) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001776 deselectAll();
1777 }
1778
Simon Huntc31d5692014-11-12 13:27:18 -08001779 selections[obj.id] = { obj: obj, el: el };
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001780 selectOrder.push(obj.id);
1781
1782 n.classed('selected', true);
Simon Hunt61d04042014-11-11 17:27:16 -08001783 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001784 }
1785
1786 function deselectObject(id) {
Simon Huntc31d5692014-11-12 13:27:18 -08001787 var obj = selections[id],
1788 idx;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001789 if (obj) {
1790 d3.select(obj.el).classed('selected', false);
Simon Hunt61d04042014-11-11 17:27:16 -08001791 delete selections[id];
Simon Huntc31d5692014-11-12 13:27:18 -08001792 idx = $.inArray(id, selectOrder);
1793 if (idx >= 0) {
1794 selectOrder.splice(idx, 1);
1795 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001796 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001797 }
1798
1799 function deselectAll() {
1800 // deselect all nodes in the network...
1801 node.classed('selected', false);
1802 selections = {};
1803 selectOrder = [];
Simon Hunt61d04042014-11-11 17:27:16 -08001804 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001805 }
1806
Simon Hunt61d04042014-11-11 17:27:16 -08001807 // update the state of the detail pane, based on current selections
1808 function updateDetailPane() {
1809 var nSel = selectOrder.length;
1810 if (!nSel) {
1811 detailPane.hide();
Simon Huntd72bc702014-11-13 18:38:04 -08001812 showTrafficAction(); // sends cancelTraffic event
Simon Hunt61d04042014-11-11 17:27:16 -08001813 } else if (nSel === 1) {
1814 singleSelect();
1815 } else {
1816 multiSelect();
1817 }
1818 }
1819
1820 function singleSelect() {
1821 requestDetails();
Simon Huntb53e0682014-11-12 13:32:01 -08001822 // NOTE: detail pane will be shown from showDetails event callback
Simon Hunt61d04042014-11-11 17:27:16 -08001823 }
1824
1825 function multiSelect() {
Simon Huntb53e0682014-11-12 13:32:01 -08001826 populateMultiSelect();
Simon Huntb53e0682014-11-12 13:32:01 -08001827 }
1828
1829 function addSep(tbody) {
1830 var tr = tbody.append('tr');
1831 $('<hr>').appendTo(tr.append('td').attr('colspan', 2));
1832 }
1833
1834 function addProp(tbody, label, value) {
1835 var tr = tbody.append('tr');
1836
1837 tr.append('td')
1838 .attr('class', 'label')
1839 .text(label + ' :');
1840
1841 tr.append('td')
1842 .attr('class', 'value')
1843 .text(value);
1844 }
1845
1846 function populateMultiSelect() {
1847 detailPane.empty();
1848
1849 var title = detailPane.append("h2"),
1850 table = detailPane.append("table"),
1851 tbody = table.append("tbody");
1852
1853 title.text('Multi-Select...');
1854
1855 selectOrder.forEach(function (d, i) {
1856 addProp(tbody, i+1, d);
1857 });
Simon Huntd72bc702014-11-13 18:38:04 -08001858
1859 addMultiSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08001860 }
1861
1862 function populateDetails(data) {
1863 detailPane.empty();
1864
1865 var title = detailPane.append("h2"),
1866 table = detailPane.append("table"),
1867 tbody = table.append("tbody");
1868
1869 $('<img src="img/' + data.type + '.png">').appendTo(title);
1870 $('<span>').attr('class', 'icon').text(data.id).appendTo(title);
1871
1872 data.propOrder.forEach(function(p) {
1873 if (p === '-') {
1874 addSep(tbody);
1875 } else {
1876 addProp(tbody, p, data.props[p]);
1877 }
1878 });
Simon Huntd72bc702014-11-13 18:38:04 -08001879
1880 addSingleSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08001881 }
1882
Simon Huntd72bc702014-11-13 18:38:04 -08001883 function addSingleSelectActions() {
1884 detailPane.append('hr');
1885 // always want to allow 'show traffic'
Simon Hunta5e89142014-11-14 07:00:33 -08001886 addAction(detailPane, 'Show Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001887 }
1888
1889 function addMultiSelectActions() {
1890 detailPane.append('hr');
1891 // always want to allow 'show traffic'
Simon Hunta5e89142014-11-14 07:00:33 -08001892 addAction(detailPane, 'Show Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001893 // if exactly two hosts are selected, also want 'add host intent'
1894 if (nSel() === 2 && allSelectionsClass('host')) {
Simon Hunta5e89142014-11-14 07:00:33 -08001895 addAction(detailPane, 'Add Host Intent', addIntentAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001896 }
1897 }
1898
Simon Hunta5e89142014-11-14 07:00:33 -08001899 function addAction(panel, text, cb) {
1900 panel.append('div')
Simon Huntd72bc702014-11-13 18:38:04 -08001901 .classed('actionBtn', true)
1902 .text(text)
1903 .on('click', cb);
1904 }
1905
1906
Paul Greysonfcba0e82014-11-13 10:21:16 -08001907 function zoomPan(scale, translate) {
1908 zoomPanContainer.attr("transform", "translate(" + translate + ")scale(" + scale + ")");
1909 // keep the map lines constant width while zooming
Thomas Vachuska89543292014-11-19 11:28:33 -08001910 bgImg.style("stroke-width", 2.0 / scale + "px");
Paul Greysonfcba0e82014-11-13 10:21:16 -08001911 }
1912
1913 function resetZoomPan() {
1914 zoomPan(1, [0,0]);
1915 zoom.scale(1).translate([0,0]);
1916 }
1917
1918 function setupZoomPan() {
1919 function zoomed() {
Simon Huntdeab4322014-11-13 18:49:07 -08001920 if (!panZoom() ^ !d3.event.sourceEvent.metaKey) {
Paul Greysonfcba0e82014-11-13 10:21:16 -08001921 zoomPan(d3.event.scale, d3.event.translate);
1922 }
1923 }
1924
1925 zoom = d3.behavior.zoom()
1926 .translate([0, 0])
1927 .scale(1)
1928 .scaleExtent([1, 8])
1929 .on("zoom", zoomed);
1930
1931 svg.call(zoom);
1932 }
1933
Simon Hunt61d04042014-11-11 17:27:16 -08001934 // ==============================
1935 // Test harness code
Simon Hunt56d51852014-11-09 13:03:35 -08001936
1937 function prepareScenario(view, ctx, dbg) {
1938 var sc = scenario,
1939 urlSc = sc.evDir + ctx + sc.evScenario;
1940
1941 if (!ctx) {
1942 view.alert("No scenario specified (null ctx)");
1943 return;
1944 }
1945
1946 sc.view = view;
1947 sc.ctx = ctx;
1948 sc.debug = dbg;
1949 sc.evNumber = 0;
1950
1951 d3.json(urlSc, function(err, data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001952 var p = data && data.params || {},
1953 desc = data && data.description || null,
Simon Huntfc274c92014-11-11 11:05:46 -08001954 intro = data && data.title;
Simon Huntbb282f52014-11-10 11:08:19 -08001955
Simon Hunt56d51852014-11-09 13:03:35 -08001956 if (err) {
1957 view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err);
1958 } else {
1959 sc.params = p;
Simon Huntbb282f52014-11-10 11:08:19 -08001960 if (desc) {
1961 intro += '\n\n ' + desc.join('\n ');
1962 }
1963 view.alert(intro);
Simon Hunt56d51852014-11-09 13:03:35 -08001964 }
1965 });
1966
1967 }
1968
Simon Hunt01095ff2014-11-13 16:37:29 -08001969 // ==============================
1970 // Toggle Buttons in masthead
Simon Hunt0c6d4192014-11-12 12:07:10 -08001971
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001972 // TODO: toggle button (and other widgets in the masthead) should be provided
1973 // by the framework; not generated by the view.
1974
Simon Hunta5e89142014-11-14 07:00:33 -08001975 var showInstances,
1976 doPanZoom,
1977 showTrafficOnHover;
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001978
1979 function addButtonBar(view) {
1980 var bb = d3.select('#mast')
1981 .append('span').classed('right', true).attr('id', 'bb');
1982
Simon Hunta5e89142014-11-14 07:00:33 -08001983 function mkTogBtn(text, cb) {
1984 return bb.append('span')
1985 .classed('btn', true)
1986 .text(text)
1987 .on('click', cb);
1988 }
Simon Hunt01095ff2014-11-13 16:37:29 -08001989
Simon Hunta5e89142014-11-14 07:00:33 -08001990 showInstances = mkTogBtn('Show Instances', toggleInst);
Simon Hunte5b71752014-11-18 20:06:07 -08001991 //doPanZoom = mkTogBtn('Pan/Zoom', togglePanZoom);
1992 //showTrafficOnHover = mkTogBtn('Show traffic on hover', toggleTrafficHover);
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001993 }
1994
Simon Hunta5e89142014-11-14 07:00:33 -08001995 function instShown() {
1996 return showInstances.classed('active');
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001997 }
Simon Hunta5e89142014-11-14 07:00:33 -08001998 function toggleInst() {
1999 showInstances.classed('active', !instShown());
2000 if (instShown()) {
2001 oiBox.show();
2002 } else {
2003 oiBox.hide();
2004 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002005 }
2006
Simon Huntdeab4322014-11-13 18:49:07 -08002007 function panZoom() {
Simon Hunte5b71752014-11-18 20:06:07 -08002008 return false;
2009 //return doPanZoom.classed('active');
Simon Hunt01095ff2014-11-13 16:37:29 -08002010 }
Simon Hunta5e89142014-11-14 07:00:33 -08002011 function togglePanZoom() {
2012 doPanZoom.classed('active', !panZoom());
2013 }
2014
2015 function trafficHover() {
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -08002016 return hoverEnabled;
Simon Hunta5e89142014-11-14 07:00:33 -08002017 }
2018 function toggleTrafficHover() {
2019 showTrafficOnHover.classed('active', !trafficHover());
2020 }
2021
Simon Hunt7fa116d2014-11-17 14:16:55 -08002022 function loadGlyphs(svg) {
2023 var defs = svg.append('defs');
2024 gly.defBird(defs);
2025 gly.defBullhorn(defs);
2026 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002027
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002028 // ==============================
Simon Hunt142d0032014-11-04 20:13:09 -08002029 // View life-cycle callbacks
Simon Hunt195cb382014-11-03 17:50:51 -08002030
Simon Huntf67722a2014-11-10 09:32:06 -08002031 function preload(view, ctx, flags) {
Simon Hunt142d0032014-11-04 20:13:09 -08002032 var w = view.width(),
2033 h = view.height(),
Simon Huntc7ee0662014-11-05 16:44:37 -08002034 fcfg = config.force,
2035 fpad = fcfg.pad,
2036 forceDim = [w - 2*fpad, h - 2*fpad];
Simon Hunt195cb382014-11-03 17:50:51 -08002037
Simon Hunt142d0032014-11-04 20:13:09 -08002038 // NOTE: view.$div is a D3 selection of the view's div
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002039 var viewBox = '0 0 ' + config.logicalSize + ' ' + config.logicalSize;
2040 svg = view.$div.append('svg').attr('viewBox', viewBox);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002041 setSize(svg, view);
2042
Simon Hunt7fa116d2014-11-17 14:16:55 -08002043 loadGlyphs(svg);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002044
Paul Greysonfcba0e82014-11-13 10:21:16 -08002045 zoomPanContainer = svg.append('g').attr('id', 'zoomPanContainer');
Paul Greysonfcba0e82014-11-13 10:21:16 -08002046 setupZoomPan();
2047
Simon Hunt1a9eff92014-11-07 11:06:34 -08002048 // add blue glow filter to svg layer
Paul Greysonfcba0e82014-11-13 10:21:16 -08002049 d3u.appendGlow(zoomPanContainer);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002050
Simon Huntc7ee0662014-11-05 16:44:37 -08002051 // group for the topology
Paul Greysonfcba0e82014-11-13 10:21:16 -08002052 topoG = zoomPanContainer.append('g')
Simon Huntd3b7d512014-11-12 15:48:41 -08002053 .attr('id', 'topo-G')
Simon Huntc7ee0662014-11-05 16:44:37 -08002054 .attr('transform', fcfg.translate());
2055
Simon Hunte2575b62014-11-18 15:25:53 -08002056 // subgroups for links, link labels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002057 linkG = topoG.append('g').attr('id', 'links');
Simon Hunte2575b62014-11-18 15:25:53 -08002058 linkLabelG = topoG.append('g').attr('id', 'linkLabels');
Simon Huntc7ee0662014-11-05 16:44:37 -08002059 nodeG = topoG.append('g').attr('id', 'nodes');
2060
Simon Hunte2575b62014-11-18 15:25:53 -08002061 // selection of links, linkLabels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002062 link = linkG.selectAll('.link');
Simon Hunte2575b62014-11-18 15:25:53 -08002063 linkLabel = linkLabelG.selectAll('.linkLabel');
Simon Huntc7ee0662014-11-05 16:44:37 -08002064 node = nodeG.selectAll('.node');
2065
Simon Hunt7cd48f32014-11-09 23:42:50 -08002066 function chrg(d) {
2067 return fcfg.charge[d.class] || -12000;
2068 }
Simon Hunt99c13842014-11-06 18:23:12 -08002069 function ldist(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002070 return fcfg.linkDistance[d.type] || 50;
Simon Hunt99c13842014-11-06 18:23:12 -08002071 }
2072 function lstrg(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002073 // 0.0 - 1.0
2074 return fcfg.linkStrength[d.type] || 1.0;
Simon Hunt99c13842014-11-06 18:23:12 -08002075 }
2076
Simon Hunt1a9eff92014-11-07 11:06:34 -08002077 function selectCb(d, self) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002078 selectObject(d, self);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002079 }
2080
2081 function atDragEnd(d, self) {
Simon Hunt56d51852014-11-09 13:03:35 -08002082 // once we've finished moving, pin the node in position
2083 d.fixed = true;
2084 d3.select(self).classed('fixed', true);
2085 if (config.useLiveData) {
Simon Hunt902c9922014-11-11 11:59:31 -08002086 sendUpdateMeta(d);
Simon Hunta255a2c2014-11-13 22:29:35 -08002087 } else {
2088 console.log('Moving node ' + d.id + ' to [' + d.x + ',' + d.y + ']');
Simon Hunt1a9eff92014-11-07 11:06:34 -08002089 }
2090 }
2091
Simon Hunt902c9922014-11-11 11:59:31 -08002092 function sendUpdateMeta(d) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002093 sendMessage('updateMeta', {
2094 id: d.id,
2095 'class': d.class,
Simon Hunt902c9922014-11-11 11:59:31 -08002096 'memento': {
Simon Hunt01095ff2014-11-13 16:37:29 -08002097 x: d.x,
2098 y: d.y
Simon Hunt902c9922014-11-11 11:59:31 -08002099 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002100 });
2101 }
2102
Simon Huntc7ee0662014-11-05 16:44:37 -08002103 // set up the force layout
2104 network.force = d3.layout.force()
2105 .size(forceDim)
2106 .nodes(network.nodes)
2107 .links(network.links)
Simon Hunt7cd48f32014-11-09 23:42:50 -08002108 .gravity(0.4)
2109 .friction(0.7)
2110 .charge(chrg)
Simon Hunt99c13842014-11-06 18:23:12 -08002111 .linkDistance(ldist)
2112 .linkStrength(lstrg)
Simon Huntc7ee0662014-11-05 16:44:37 -08002113 .on('tick', tick);
Simon Hunt195cb382014-11-03 17:50:51 -08002114
Simon Hunt01095ff2014-11-13 16:37:29 -08002115 network.drag = d3u.createDragBehavior(network.force,
Simon Huntdeab4322014-11-13 18:49:07 -08002116 selectCb, atDragEnd, panZoom);
Simon Hunt0c6d4192014-11-12 12:07:10 -08002117
2118 // create mask layer for when we lose connection to server.
Simon Hunta5e89142014-11-14 07:00:33 -08002119 // TODO: this should be part of the framework
Simon Hunt0c6d4192014-11-12 12:07:10 -08002120 mask = view.$div.append('div').attr('id','topo-mask');
2121 para(mask, 'Oops!');
2122 para(mask, 'Web-socket connection to server closed...');
2123 para(mask, 'Try refreshing the page.');
Simon Hunt12ce12e2014-11-15 21:13:19 -08002124
2125 mask.append('svg')
2126 .attr({
2127 id: 'mask-bird',
2128 width: w,
2129 height: h
2130 })
2131 .append('g')
2132 .attr('transform', birdTranslate(w, h))
2133 .style('opacity', 0.3)
2134 .append('use')
2135 .attr({
2136 'xlink:href': '#bird',
2137 width: config.birdDim,
2138 height: config.birdDim,
2139 fill: '#111'
Thomas Vachuska89543292014-11-19 11:28:33 -08002140 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08002141 }
Simon Hunt195cb382014-11-03 17:50:51 -08002142
Simon Hunt01095ff2014-11-13 16:37:29 -08002143 function para(sel, text) {
2144 sel.append('p').text(text);
2145 }
2146
2147
Simon Hunt56d51852014-11-09 13:03:35 -08002148 function load(view, ctx, flags) {
Simon Huntf67722a2014-11-10 09:32:06 -08002149 // resize, in case the window was resized while we were not loaded
2150 resize(view, ctx, flags);
2151
Simon Hunt99c13842014-11-06 18:23:12 -08002152 // cache the view token, so network topo functions can access it
2153 network.view = view;
Simon Hunt56d51852014-11-09 13:03:35 -08002154 config.useLiveData = !flags.local;
2155
2156 if (!config.useLiveData) {
2157 prepareScenario(view, ctx, flags.debug);
2158 }
Simon Hunt99c13842014-11-06 18:23:12 -08002159
2160 // set our radio buttons and key bindings
Simon Hunt9462e8c2014-11-14 17:28:09 -08002161 layerBtnSet = view.setRadio(layerButtons);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002162 view.setKeys(keyDispatch);
Simon Hunt195cb382014-11-03 17:50:51 -08002163
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002164 // patch in our "button bar" for now
2165 // TODO: implement a more official frameworky way of doing this..
2166 addButtonBar(view);
2167
Simon Huntd3b7d512014-11-12 15:48:41 -08002168 // Load map data asynchronously; complete startup after that..
2169 loadGeoJsonData();
Simon Hunta255a2c2014-11-13 22:29:35 -08002170
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002171 //var dashIdx = 0;
2172 //antTimer = setInterval(function () {
2173 // // TODO: figure out how to choose Src-->Dst and Dst-->Src, per link
2174 // dashIdx = dashIdx === 0 ? 14 : dashIdx - 2;
2175 // d3.selectAll('.animated').style('stroke-dashoffset', dashIdx);
2176 //}, 35);
Simon Hunta255a2c2014-11-13 22:29:35 -08002177 }
2178
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002179 function startAntTimer() {
2180 var pulses = [ 5, 3, 1.2, 3 ],
2181 pulse = 0;
2182 antTimer = setInterval(function () {
2183 pulse = pulse + 1;
2184 pulse = pulse === pulses.length ? 0 : pulse;
2185 d3.selectAll('.animated').style('stroke-width', pulses[pulse]);
2186 }, 200);
2187 }
2188
2189 function stopAntTimer() {
Simon Hunta255a2c2014-11-13 22:29:35 -08002190 if (antTimer) {
2191 clearInterval(antTimer);
2192 antTimer = null;
2193 }
Simon Huntd3b7d512014-11-12 15:48:41 -08002194 }
2195
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002196 function unload(view, ctx, flags) {
2197 stopAntTimer();
2198 }
2199
Simon Huntd3b7d512014-11-12 15:48:41 -08002200 // TODO: move these to config/state portion of script
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002201 var geoJsonUrl = 'json/map/continental_us.json', // TODO: Paul
Simon Huntd3b7d512014-11-12 15:48:41 -08002202 geoJson;
2203
2204 function loadGeoJsonData() {
2205 d3.json(geoJsonUrl, function (err, data) {
2206 if (err) {
2207 // fall back to USA map background
2208 loadStaticMap();
2209 } else {
2210 geoJson = data;
2211 loadGeoMap();
2212 }
2213
2214 // finally, connect to the server...
2215 if (config.useLiveData) {
2216 webSock.connect();
2217 }
2218 });
2219 }
2220
2221 function showBg() {
2222 return config.options.showBackground ? 'visible' : 'hidden';
2223 }
2224
2225 function loadStaticMap() {
2226 fnTrace('loadStaticMap', config.backgroundUrl);
2227 var w = network.view.width(),
2228 h = network.view.height();
2229
2230 // load the background image
2231 bgImg = svg.insert('svg:image', '#topo-G')
2232 .attr({
2233 id: 'topo-bg',
2234 width: w,
2235 height: h,
2236 'xlink:href': config.backgroundUrl
2237 })
2238 .style({
2239 visibility: showBg()
2240 });
2241 }
2242
2243 function loadGeoMap() {
2244 fnTrace('loadGeoMap', geoJsonUrl);
Simon Huntd3b7d512014-11-12 15:48:41 -08002245
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002246 // extracts the topojson data into geocoordinate-based geometry
2247 var topoData = topojson.feature(geoJson, geoJson.objects.states);
Simon Huntd3b7d512014-11-12 15:48:41 -08002248
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002249 // see: http://bl.ocks.org/mbostock/4707858
2250 geoMapProjection = d3.geo.mercator();
2251 var path = d3.geo.path().projection(geoMapProjection);
Simon Huntd3b7d512014-11-12 15:48:41 -08002252
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002253 geoMapProjection
2254 .scale(1)
2255 .translate([0, 0]);
Simon Huntd3b7d512014-11-12 15:48:41 -08002256
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002257 // [[x1,y1],[x2,y2]]
2258 var b = path.bounds(topoData);
Paul Greysonfcba0e82014-11-13 10:21:16 -08002259 // size map to 95% of minimum dimension to fill space
2260 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 -08002261 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 -08002262
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002263 geoMapProjection
2264 .scale(s)
2265 .translate(t);
2266
Paul Greysonfcba0e82014-11-13 10:21:16 -08002267 bgImg = zoomPanContainer.insert("g", '#topo-G');
Thomas Vachuska89543292014-11-19 11:28:33 -08002268 bgImg.attr('id', 'map').selectAll('path')
2269 .data(topoData.features)
2270 .enter()
2271 .append('path')
2272 .attr('d', path);
Simon Hunt195cb382014-11-03 17:50:51 -08002273 }
2274
Simon Huntf67722a2014-11-10 09:32:06 -08002275 function resize(view, ctx, flags) {
Simon Hunt12ce12e2014-11-15 21:13:19 -08002276 var w = view.width(),
2277 h = view.height();
2278
Simon Hunt934c3ce2014-11-05 11:45:07 -08002279 setSize(svg, view);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002280
2281 d3.select('#mask-bird').attr({ width: w, height: h})
2282 .select('g').attr('transform', birdTranslate(w, h));
Simon Hunt142d0032014-11-04 20:13:09 -08002283 }
2284
Simon Hunt12ce12e2014-11-15 21:13:19 -08002285 function birdTranslate(w, h) {
2286 var bdim = config.birdDim;
2287 return 'translate('+((w-bdim)*.4)+','+((h-bdim)*.1)+')';
2288 }
Simon Hunt142d0032014-11-04 20:13:09 -08002289
2290 // ==============================
2291 // View registration
Simon Hunt195cb382014-11-03 17:50:51 -08002292
Simon Hunt25248912014-11-04 11:25:48 -08002293 onos.ui.addView('topo', {
Simon Hunt142d0032014-11-04 20:13:09 -08002294 preload: preload,
2295 load: load,
Simon Hunta255a2c2014-11-13 22:29:35 -08002296 unload: unload,
Simon Hunt142d0032014-11-04 20:13:09 -08002297 resize: resize
Simon Hunt195cb382014-11-03 17:50:51 -08002298 });
2299
Simon Hunt61d04042014-11-11 17:27:16 -08002300 detailPane = onos.ui.addFloatingPanel('topo-detail');
Simon Hunta5e89142014-11-14 07:00:33 -08002301 oiBox = onos.ui.addFloatingPanel('topo-oibox', 'TL');
Simon Hunt61d04042014-11-11 17:27:16 -08002302
Simon Hunt195cb382014-11-03 17:50:51 -08002303}(ONOS));