blob: e9a8c1e4b716980b886574aaaf5037dc413812dc [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,
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800144 V: showTrafficAction,
145 A: showAllTrafficAction,
Simon Hunt9462e8c2014-11-14 17:28:09 -0800146 esc: handleEscape
Simon Hunt934c3ce2014-11-05 11:45:07 -0800147 };
Simon Hunt142d0032014-11-04 20:13:09 -0800148
Simon Hunt195cb382014-11-03 17:50:51 -0800149 // state variables
Simon Hunt99c13842014-11-06 18:23:12 -0800150 var network = {
Simon Hunt50128c02014-11-08 13:36:15 -0800151 view: null, // view token reference
Simon Hunt99c13842014-11-06 18:23:12 -0800152 nodes: [],
153 links: [],
Simon Hunt269670f2014-11-17 16:17:43 -0800154 lookup: {},
155 revLinkToKey: {}
Simon Hunt99c13842014-11-06 18:23:12 -0800156 },
Simon Hunt56d51852014-11-09 13:03:35 -0800157 scenario = {
158 evDir: 'json/ev/',
159 evScenario: '/scenario.json',
160 evPrefix: '/ev_',
161 evOnos: '_onos.json',
162 evUi: '_ui.json',
163 ctx: null,
164 params: {},
165 evNumber: 0,
166 view: null,
167 debug: false
168 },
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800169 webSock,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800170 sid = 0,
Simon Hunt56d51852014-11-09 13:03:35 -0800171 deviceLabelIndex = 0,
172 hostLabelIndex = 0,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800173 selections = {},
Simon Hunta5e89142014-11-14 07:00:33 -0800174 selectOrder = [],
Simon Hunt6ac93f32014-11-13 12:17:27 -0800175 hovered = null,
Simon Hunta5e89142014-11-14 07:00:33 -0800176 detailPane,
Simon Hunta255a2c2014-11-13 22:29:35 -0800177 antTimer = null,
Simon Hunta5e89142014-11-14 07:00:33 -0800178 onosInstances = {},
179 onosOrder = [],
180 oiBox,
Simon Hunt9462e8c2014-11-14 17:28:09 -0800181 oiShowMaster = false,
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -0800182 hoverEnabled = false,
Simon Hunt195cb382014-11-03 17:50:51 -0800183 portLabelsOn = false;
184
Simon Hunt934c3ce2014-11-05 11:45:07 -0800185 // D3 selections
186 var svg,
Paul Greysonfcba0e82014-11-13 10:21:16 -0800187 zoomPanContainer,
Simon Hunt934c3ce2014-11-05 11:45:07 -0800188 bgImg,
Simon Huntc7ee0662014-11-05 16:44:37 -0800189 topoG,
190 nodeG,
191 linkG,
Simon Hunte2575b62014-11-18 15:25:53 -0800192 linkLabelG,
Simon Huntc7ee0662014-11-05 16:44:37 -0800193 node,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800194 link,
Simon Hunte2575b62014-11-18 15:25:53 -0800195 linkLabel,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800196 mask;
Simon Hunt195cb382014-11-03 17:50:51 -0800197
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800198 // the projection for the map background
199 var geoMapProjection;
200
Paul Greysonfcba0e82014-11-13 10:21:16 -0800201 // the zoom function
202 var zoom;
203
Simon Hunt142d0032014-11-04 20:13:09 -0800204 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800205 // For Debugging / Development
Simon Hunt195cb382014-11-03 17:50:51 -0800206
Simon Hunt99c13842014-11-06 18:23:12 -0800207 function note(label, msg) {
208 console.log('NOTE: ' + label + ': ' + msg);
Simon Hunt195cb382014-11-03 17:50:51 -0800209 }
210
Simon Hunt99c13842014-11-06 18:23:12 -0800211 function debug(what) {
212 return config.debugOn && config.debug[what];
Simon Hunt934c3ce2014-11-05 11:45:07 -0800213 }
214
Simon Huntfc274c92014-11-11 11:05:46 -0800215 function fnTrace(msg, id) {
216 if (config.fnTrace) {
217 console.log('FN: ' + msg + ' [' + id + ']');
218 }
219 }
Simon Hunt99c13842014-11-06 18:23:12 -0800220
Simon Hunta5e89142014-11-14 07:00:33 -0800221 function evTrace(data) {
222 fnTrace(data.event, data.payload.id);
223 }
224
Simon Hunt934c3ce2014-11-05 11:45:07 -0800225 // ==============================
226 // Key Callbacks
227
Simon Hunt99c13842014-11-06 18:23:12 -0800228 function testMe(view) {
Simon Hunt625dc402014-11-18 10:57:18 -0800229 view.alert('Theme is ' + view.theme());
Simon Hunt99c13842014-11-06 18:23:12 -0800230 }
231
Simon Hunt56d51852014-11-09 13:03:35 -0800232 function abortIfLive() {
Simon Hunt50128c02014-11-08 13:36:15 -0800233 if (config.useLiveData) {
Simon Huntb53e0682014-11-12 13:32:01 -0800234 network.view.alert("Sorry, currently using live data..");
Simon Hunt56d51852014-11-09 13:03:35 -0800235 return true;
Simon Hunt50128c02014-11-08 13:36:15 -0800236 }
Simon Hunt56d51852014-11-09 13:03:35 -0800237 return false;
238 }
Simon Hunt50128c02014-11-08 13:36:15 -0800239
Simon Hunt56d51852014-11-09 13:03:35 -0800240 function testDebug(msg) {
241 if (scenario.debug) {
242 scenario.view.alert(msg);
243 }
244 }
Simon Hunt99c13842014-11-06 18:23:12 -0800245
Simon Hunt56d51852014-11-09 13:03:35 -0800246 function injectTestEvent(view) {
247 if (abortIfLive()) { return; }
248 var sc = scenario,
249 evn = ++sc.evNumber,
250 pfx = sc.evDir + sc.ctx + sc.evPrefix + evn,
251 onosUrl = pfx + sc.evOnos,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800252 uiUrl = pfx + sc.evUi,
253 stack = [
254 { url: onosUrl, cb: handleServerEvent },
255 { url: uiUrl, cb: handleUiEvent }
256 ];
257 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800258 }
259
Simon Hunt7cd48f32014-11-09 23:42:50 -0800260 function recurseFetchEvent(stack, evn) {
261 var v = scenario.view,
262 frame;
263 if (stack.length === 0) {
Simon Huntfc274c92014-11-11 11:05:46 -0800264 v.alert('Oops!\n\nNo event #' + evn + ' found.');
Simon Hunt7cd48f32014-11-09 23:42:50 -0800265 return;
266 }
267 frame = stack.shift();
268
269 d3.json(frame.url, function (err, data) {
Simon Hunt99c13842014-11-06 18:23:12 -0800270 if (err) {
Simon Hunt56d51852014-11-09 13:03:35 -0800271 if (err.status === 404) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800272 // if we didn't find the data, try the next stack frame
273 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800274 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800275 v.alert('non-404 error:\n\n' + frame.url + '\n\n' + err);
Simon Hunt56d51852014-11-09 13:03:35 -0800276 }
Simon Hunt99c13842014-11-06 18:23:12 -0800277 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800278 testDebug('loaded: ' + frame.url);
Simon Hunt1712ed82014-11-17 12:56:00 -0800279 wsTrace('test', JSON.stringify(data));
Simon Hunt7cd48f32014-11-09 23:42:50 -0800280 frame.cb(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800281 }
282 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800283
Simon Hunt56d51852014-11-09 13:03:35 -0800284 }
Simon Hunt50128c02014-11-08 13:36:15 -0800285
Simon Hunt56d51852014-11-09 13:03:35 -0800286 function handleUiEvent(data) {
Simon Huntbb282f52014-11-10 11:08:19 -0800287 scenario.view.alert('UI Tx: ' + data.event + '\n\n' +
288 JSON.stringify(data));
Simon Hunt56d51852014-11-09 13:03:35 -0800289 }
290
291 function injectStartupEvents(view) {
292 var last = scenario.params.lastAuto || 0;
293 if (abortIfLive()) { return; }
294
295 while (scenario.evNumber < last) {
Simon Hunt1a9eff92014-11-07 11:06:34 -0800296 injectTestEvent(view);
297 }
298 }
299
Simon Hunt934c3ce2014-11-05 11:45:07 -0800300 function toggleBg() {
301 var vis = bgImg.style('visibility');
302 bgImg.style('visibility', (vis === 'hidden') ? 'visible' : 'hidden');
303 }
304
Simon Hunt99c13842014-11-06 18:23:12 -0800305 function cycleLabels() {
Simon Huntbb282f52014-11-10 11:08:19 -0800306 deviceLabelIndex = (deviceLabelIndex === network.deviceLabelCount - 1)
307 ? 0 : deviceLabelIndex + 1;
Simon Hunt5f36d342014-11-08 21:33:14 -0800308
Simon Hunt99c13842014-11-06 18:23:12 -0800309 network.nodes.forEach(function (d) {
Simon Huntbb282f52014-11-10 11:08:19 -0800310 if (d.class === 'device') {
311 updateDeviceLabel(d);
312 }
Simon Hunt99c13842014-11-06 18:23:12 -0800313 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800314 }
315
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -0800316 function toggleHover(view) {
317 hoverEnabled = !hoverEnabled;
318 }
319
Simon Hunt934c3ce2014-11-05 11:45:07 -0800320 function togglePorts(view) {
Simon Hunt50128c02014-11-08 13:36:15 -0800321 view.alert('togglePorts() callback')
Simon Hunt934c3ce2014-11-05 11:45:07 -0800322 }
323
Simon Hunt6ac93f32014-11-13 12:17:27 -0800324 function unpin() {
325 if (hovered) {
326 hovered.fixed = false;
327 hovered.el.classed('fixed', false);
328 network.force.resume();
329 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800330 }
331
Simon Hunt9462e8c2014-11-14 17:28:09 -0800332 function handleEscape(view) {
333 if (oiShowMaster) {
334 cancelAffinity();
335 } else {
336 deselectAll();
337 }
338 }
339
Simon Hunt934c3ce2014-11-05 11:45:07 -0800340 // ==============================
341 // Radio Button Callbacks
342
Simon Hunta5e89142014-11-14 07:00:33 -0800343 var layerLookup = {
344 host: {
345 endstation: 'pkt', // default, if host event does not define type
Thomas Vachuska89543292014-11-19 11:28:33 -0800346 router: 'pkt',
Simon Hunta5e89142014-11-14 07:00:33 -0800347 bgpSpeaker: 'pkt'
348 },
349 device: {
350 switch: 'pkt',
351 roadm: 'opt'
352 },
353 link: {
354 hostLink: 'pkt',
355 direct: 'pkt',
Simon Hunt8257f4c2014-11-16 19:34:54 -0800356 indirect: '',
357 tunnel: '',
Simon Hunta5e89142014-11-14 07:00:33 -0800358 optical: 'opt'
359 }
360 };
361
362 function inLayer(d, layer) {
Simon Hunt8257f4c2014-11-16 19:34:54 -0800363 var type = d.class === 'link' ? d.type() : d.type,
364 look = layerLookup[d.class],
365 lyr = look && look[type];
Simon Hunta5e89142014-11-14 07:00:33 -0800366 return lyr === layer;
367 }
368
369 function unsuppressLayer(which) {
370 node.each(function (d) {
371 var node = d.el;
372 if (inLayer(d, which)) {
373 node.classed('suppressed', false);
374 }
375 });
376
377 link.each(function (d) {
378 var link = d.el;
379 if (inLayer(d, which)) {
380 link.classed('suppressed', false);
381 }
382 });
383 }
384
Simon Hunt9462e8c2014-11-14 17:28:09 -0800385 function suppressLayers(b) {
386 node.classed('suppressed', b);
387 link.classed('suppressed', b);
Simon Hunt142d0032014-11-04 20:13:09 -0800388// d3.selectAll('svg .port').classed('inactive', false);
389// d3.selectAll('svg .portText').classed('inactive', false);
Simon Hunt195cb382014-11-03 17:50:51 -0800390 }
391
Simon Hunt9462e8c2014-11-14 17:28:09 -0800392 function showAllLayers() {
393 suppressLayers(false);
394 }
395
Simon Hunt195cb382014-11-03 17:50:51 -0800396 function showPacketLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800397 node.classed('suppressed', true);
398 link.classed('suppressed', true);
399 unsuppressLayer('pkt');
Simon Hunt195cb382014-11-03 17:50:51 -0800400 }
401
402 function showOpticalLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800403 node.classed('suppressed', true);
404 link.classed('suppressed', true);
405 unsuppressLayer('opt');
Simon Hunt195cb382014-11-03 17:50:51 -0800406 }
407
Simon Hunt9462e8c2014-11-14 17:28:09 -0800408 function restoreLayerState() {
409 layerBtnDispatch[layerBtnSet.selected()]();
410 }
411
Simon Hunt142d0032014-11-04 20:13:09 -0800412 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800413 // Private functions
414
Simon Hunt99c13842014-11-06 18:23:12 -0800415 function safeId(s) {
416 return s.replace(/[^a-z0-9]/gi, '-');
417 }
418
Simon Huntc7ee0662014-11-05 16:44:37 -0800419 // set the size of the given element to that of the view (reduced if padded)
420 function setSize(el, view, pad) {
421 var padding = pad ? pad * 2 : 0;
Simon Hunt934c3ce2014-11-05 11:45:07 -0800422 el.attr({
Simon Huntc7ee0662014-11-05 16:44:37 -0800423 width: view.width() - padding,
424 height: view.height() - padding
Simon Hunt934c3ce2014-11-05 11:45:07 -0800425 });
426 }
427
Simon Hunt8257f4c2014-11-16 19:34:54 -0800428 function makeNodeKey(d, what) {
429 var port = what + 'Port';
430 return d[what] + '/' + d[port];
431 }
432
433 function makeLinkKey(d, flipped) {
434 var one = flipped ? makeNodeKey(d, 'dst') : makeNodeKey(d, 'src'),
435 two = flipped ? makeNodeKey(d, 'src') : makeNodeKey(d, 'dst');
436 return one + '-' + two;
437 }
438
Simon Hunt269670f2014-11-17 16:17:43 -0800439 function findLinkById(id) {
440 // check to see if this is a reverse lookup, else default to given id
441 var key = network.revLinkToKey[id] || id;
442 return key && network.lookup[key];
443 }
444
Simon Hunt8257f4c2014-11-16 19:34:54 -0800445 function findLink(linkData, op) {
446 var key = makeLinkKey(linkData),
447 keyrev = makeLinkKey(linkData, 1),
448 link = network.lookup[key],
449 linkRev = network.lookup[keyrev],
450 result = {},
451 ldata = link || linkRev,
452 rawLink;
453
454 if (op === 'add') {
455 if (link) {
456 // trying to add a link that we already know about
457 result.ldata = link;
458 result.badLogic = 'addLink: link already added';
459
460 } else if (linkRev) {
461 // we found the reverse of the link to be added
462 result.ldata = linkRev;
463 if (linkRev.fromTarget) {
464 result.badLogic = 'addLink: link already added';
465 }
466 }
467 } else if (op === 'update') {
468 if (!ldata) {
469 result.badLogic = 'updateLink: link not found';
470 } else {
471 rawLink = link ? ldata.fromSource : ldata.fromTarget;
472 result.updateWith = function (data) {
473 $.extend(rawLink, data);
474 restyleLinkElement(ldata);
475 }
476 }
477 } else if (op === 'remove') {
478 if (!ldata) {
479 result.badLogic = 'removeLink: link not found';
480 } else {
481 rawLink = link ? ldata.fromSource : ldata.fromTarget;
482
483 if (!rawLink) {
484 result.badLogic = 'removeLink: link not found';
485
486 } else {
487 result.removeRawLink = function () {
488 if (link) {
489 // remove fromSource
490 ldata.fromSource = null;
491 if (ldata.fromTarget) {
492 // promote target into source position
493 ldata.fromSource = ldata.fromTarget;
494 ldata.fromTarget = null;
495 ldata.key = keyrev;
496 delete network.lookup[key];
497 network.lookup[keyrev] = ldata;
Simon Hunt269670f2014-11-17 16:17:43 -0800498 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800499 }
500 } else {
501 // remove fromTarget
502 ldata.fromTarget = null;
Simon Hunt269670f2014-11-17 16:17:43 -0800503 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800504 }
505 if (ldata.fromSource) {
506 restyleLinkElement(ldata);
507 } else {
508 removeLinkElement(ldata);
509 }
510 }
511 }
512 }
513 }
514 return result;
515 }
516
517 function addLinkUpdate(ldata, link) {
518 // add link event, but we already have the reverse link installed
519 ldata.fromTarget = link;
Simon Hunt269670f2014-11-17 16:17:43 -0800520 network.revLinkToKey[link.id] = ldata.key;
Simon Hunt8257f4c2014-11-16 19:34:54 -0800521 restyleLinkElement(ldata);
522 }
523
524 var allLinkTypes = 'direct indirect optical tunnel',
525 defaultLinkType = 'direct';
526
527 function restyleLinkElement(ldata) {
528 // this fn's job is to look at raw links and decide what svg classes
529 // need to be applied to the line element in the DOM
530 var el = ldata.el,
531 type = ldata.type(),
532 lw = ldata.linkWidth(),
533 online = ldata.online();
534
535 el.classed('link', true);
536 el.classed('inactive', !online);
537 el.classed(allLinkTypes, false);
538 if (type) {
539 el.classed(type, true);
540 }
541 el.transition()
542 .duration(1000)
Thomas Vachuska89543292014-11-19 11:28:33 -0800543 .attr('stroke-width', linkScale(lw))
544 .attr('stroke', config.topo.linkBaseColor);
Simon Hunt8257f4c2014-11-16 19:34:54 -0800545 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800546
Simon Hunt99c13842014-11-06 18:23:12 -0800547 // ==============================
548 // Event handlers for server-pushed events
549
Simon Huntbb282f52014-11-10 11:08:19 -0800550 function logicError(msg) {
551 // TODO, report logic error to server, via websock, so it can be logged
Simon Huntcb56cff2014-11-17 11:42:26 -0800552 //network.view.alert('Logic Error:\n\n' + msg);
Simon Huntfc274c92014-11-11 11:05:46 -0800553 console.warn(msg);
Simon Huntbb282f52014-11-10 11:08:19 -0800554 }
555
Simon Hunt99c13842014-11-06 18:23:12 -0800556 var eventDispatch = {
Simon Hunta5e89142014-11-14 07:00:33 -0800557 addInstance: addInstance,
Simon Hunt99c13842014-11-06 18:23:12 -0800558 addDevice: addDevice,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800559 addLink: addLink,
Simon Hunt56d51852014-11-09 13:03:35 -0800560 addHost: addHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800561
Simon Huntfcfb46c2014-11-19 12:53:38 -0800562 updateInstance: updateInstance,
Simon Huntbb282f52014-11-10 11:08:19 -0800563 updateDevice: updateDevice,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800564 updateLink: updateLink,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800565 updateHost: updateHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800566
Simon Huntd72bc702014-11-13 18:38:04 -0800567 removeInstance: stillToImplement,
Simon Huntbb282f52014-11-10 11:08:19 -0800568 removeDevice: stillToImplement,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800569 removeLink: removeLink,
Simon Hunt44031102014-11-11 13:20:36 -0800570 removeHost: removeHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800571
Simon Hunt61d04042014-11-11 17:27:16 -0800572 showDetails: showDetails,
Simon Huntb53e0682014-11-12 13:32:01 -0800573 showTraffic: showTraffic
Simon Hunt99c13842014-11-06 18:23:12 -0800574 };
575
Simon Hunta5e89142014-11-14 07:00:33 -0800576 function addInstance(data) {
577 evTrace(data);
578 var inst = data.payload,
579 id = inst.id;
580 if (onosInstances[id]) {
581 logicError('ONOS instance already added: ' + id);
582 return;
583 }
584 onosInstances[id] = inst;
585 onosOrder.push(inst);
586 updateInstances();
587 }
588
Simon Hunt99c13842014-11-06 18:23:12 -0800589 function addDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800590 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800591 var device = data.payload,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800592 nodeData = createDeviceNode(device);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800593 network.nodes.push(nodeData);
594 network.lookup[nodeData.id] = nodeData;
Simon Hunt99c13842014-11-06 18:23:12 -0800595 updateNodes();
596 network.force.start();
597 }
598
Simon Hunt99c13842014-11-06 18:23:12 -0800599 function addLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800600 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800601 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800602 result = findLink(link, 'add'),
603 bad = result.badLogic,
604 ldata = result.ldata;
605
606 if (bad) {
607 logicError(bad + ': ' + link.id);
608 return;
609 }
610
611 if (ldata) {
612 // we already have a backing store link for src/dst nodes
613 addLinkUpdate(ldata, link);
614 return;
615 }
616
617 // no backing store link yet
618 ldata = createLink(link);
619 if (ldata) {
620 network.links.push(ldata);
621 network.lookup[ldata.key] = ldata;
Simon Hunt99c13842014-11-06 18:23:12 -0800622 updateLinks();
623 network.force.start();
624 }
625 }
626
Simon Hunt56d51852014-11-09 13:03:35 -0800627 function addHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800628 evTrace(data);
Simon Hunt56d51852014-11-09 13:03:35 -0800629 var host = data.payload,
630 node = createHostNode(host),
631 lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800632 network.nodes.push(node);
633 network.lookup[host.id] = node;
634 updateNodes();
635
636 lnk = createHostLink(host);
637 if (lnk) {
Simon Hunt44031102014-11-11 13:20:36 -0800638 node.linkData = lnk; // cache ref on its host
Simon Hunt56d51852014-11-09 13:03:35 -0800639 network.links.push(lnk);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800640 network.lookup[host.ingress] = lnk;
641 network.lookup[host.egress] = lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800642 updateLinks();
643 }
644 network.force.start();
645 }
646
Simon Hunt44031102014-11-11 13:20:36 -0800647 // TODO: fold updateX(...) methods into one base method; remove duplication
Simon Hunt56a2ea42014-11-19 12:39:31 -0800648
649 function updateInstance(data) {
650 evTrace(data);
651 var inst = data.payload,
652 id = inst.id,
653 instData = onosInstances[id];
654 if (instData) {
655 $.extend(instData, inst);
656 updateInstances();
Simon Hunt56a2ea42014-11-19 12:39:31 -0800657 } else {
658 logicError('updateInstance lookup fail. ID = "' + id + '"');
659 }
660 }
661
Simon Huntbb282f52014-11-10 11:08:19 -0800662 function updateDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800663 evTrace(data);
Simon Huntbb282f52014-11-10 11:08:19 -0800664 var device = data.payload,
665 id = device.id,
666 nodeData = network.lookup[id];
667 if (nodeData) {
668 $.extend(nodeData, device);
669 updateDeviceState(nodeData);
670 } else {
671 logicError('updateDevice lookup fail. ID = "' + id + '"');
672 }
673 }
674
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800675 function updateLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800676 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800677 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800678 result = findLink(link, 'update'),
679 bad = result.badLogic;
680 if (bad) {
681 logicError(bad + ': ' + link.id);
682 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800683 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800684 result.updateWith(link);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800685 }
686
Simon Hunt7cd48f32014-11-09 23:42:50 -0800687 function updateHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800688 evTrace(data);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800689 var host = data.payload,
Simon Huntbb282f52014-11-10 11:08:19 -0800690 id = host.id,
691 hostData = network.lookup[id];
692 if (hostData) {
693 $.extend(hostData, host);
694 updateHostState(hostData);
695 } else {
696 logicError('updateHost lookup fail. ID = "' + id + '"');
697 }
Simon Hunt7cd48f32014-11-09 23:42:50 -0800698 }
699
Simon Hunt44031102014-11-11 13:20:36 -0800700 // TODO: fold removeX(...) methods into base method - remove dup code
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800701 function removeLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800702 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800703 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800704 result = findLink(link, 'remove'),
705 bad = result.badLogic;
706 if (bad) {
707 logicError(bad + ': ' + link.id);
708 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800709 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800710 result.removeRawLink();
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800711 }
712
Simon Hunt44031102014-11-11 13:20:36 -0800713 function removeHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800714 evTrace(data);
Simon Hunt44031102014-11-11 13:20:36 -0800715 var host = data.payload,
716 id = host.id,
717 hostData = network.lookup[id];
718 if (hostData) {
719 removeHostElement(hostData);
720 } else {
721 logicError('removeHost lookup fail. ID = "' + id + '"');
722 }
723 }
724
Simon Hunt61d04042014-11-11 17:27:16 -0800725 function showDetails(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800726 evTrace(data);
Simon Hunt61d04042014-11-11 17:27:16 -0800727 populateDetails(data.payload);
728 detailPane.show();
729 }
730
Simon Huntb53e0682014-11-12 13:32:01 -0800731 function showTraffic(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800732 evTrace(data);
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800733 var paths = data.payload.paths;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800734
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800735 // Revert any links hilighted previously.
Thomas Vachuskaa3148a72014-11-19 21:38:35 -0800736 link.attr('stroke-width', null)
737 .style('stroke-width', null)
738 .classed('primary secondary animated optical', false);
Simon Hunte2575b62014-11-18 15:25:53 -0800739 // Remove all previous labels.
740 removeLinkLabels();
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800741
Thomas Vachuskaa3148a72014-11-19 21:38:35 -0800742 if (paths.length && !antTimer) {
743 startAntTimer();
744 } else if (!paths.length && antTimer) {
745 stopAntTimer();
746 }
747
Simon Hunte2575b62014-11-18 15:25:53 -0800748 // Now hilight all links in the paths payload, and attach
749 // labels to them, if they are defined.
Simon Hunta255a2c2014-11-13 22:29:35 -0800750 paths.forEach(function (p) {
Simon Hunte2575b62014-11-18 15:25:53 -0800751 var n = p.links.length,
752 i,
753 ldata;
754
755 for (i=0; i<n; i++) {
756 ldata = findLinkById(p.links[i]);
757 if (ldata) {
758 ldata.el.classed(p.class, true);
759 ldata.label = p.labels[i];
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800760 }
Simon Hunte2575b62014-11-18 15:25:53 -0800761 }
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800762 });
Simon Hunte2575b62014-11-18 15:25:53 -0800763 updateLinks();
Simon Huntb53e0682014-11-12 13:32:01 -0800764 }
765
Simon Hunt56d51852014-11-09 13:03:35 -0800766 // ...............................
767
768 function stillToImplement(data) {
769 var p = data.payload;
770 note(data.event, p.id);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800771 network.view.alert('Not yet implemented: "' + data.event + '"');
Simon Hunt56d51852014-11-09 13:03:35 -0800772 }
Simon Hunt99c13842014-11-06 18:23:12 -0800773
774 function unknownEvent(data) {
Simon Hunt50128c02014-11-08 13:36:15 -0800775 network.view.alert('Unknown event type: "' + data.event + '"');
Simon Hunt99c13842014-11-06 18:23:12 -0800776 }
777
778 function handleServerEvent(data) {
779 var fn = eventDispatch[data.event] || unknownEvent;
780 fn(data);
781 }
782
783 // ==============================
Simon Hunt61d04042014-11-11 17:27:16 -0800784 // Out-going messages...
785
Simon Huntb53e0682014-11-12 13:32:01 -0800786 function userFeedback(msg) {
787 // for now, use the alert pane as is. Maybe different alert style in
788 // the future (centered on view; dismiss button?)
789 network.view.alert(msg);
790 }
791
792 function nSel() {
793 return selectOrder.length;
794 }
Simon Hunt61d04042014-11-11 17:27:16 -0800795 function getSel(idx) {
796 return selections[selectOrder[idx]];
797 }
Simon Huntb53e0682014-11-12 13:32:01 -0800798 function getSelId(idx) {
799 return getSel(idx).obj.id;
800 }
801 function allSelectionsClass(cls) {
802 for (var i=0, n=nSel(); i<n; i++) {
803 if (getSel(i).obj.class !== cls) {
804 return false;
805 }
806 }
807 return true;
808 }
Simon Hunt61d04042014-11-11 17:27:16 -0800809
Simon Hunt61d04042014-11-11 17:27:16 -0800810 // request details for the selected element
Simon Huntd72bc702014-11-13 18:38:04 -0800811 // invoked from selection of a single node.
Simon Hunt61d04042014-11-11 17:27:16 -0800812 function requestDetails() {
813 var data = getSel(0).obj,
814 payload = {
815 id: data.id,
816 class: data.class
817 };
818 sendMessage('requestDetails', payload);
819 }
820
Simon Huntd72bc702014-11-13 18:38:04 -0800821 function addIntentAction() {
822 sendMessage('addHostIntent', {
823 one: getSelId(0),
Thomas Vachuska82f2c622014-11-17 12:23:18 -0800824 two: getSelId(1),
825 ids: [ getSelId(0), getSelId(1) ]
Simon Huntd72bc702014-11-13 18:38:04 -0800826 });
827 }
828
829 function showTrafficAction() {
830 // if nothing is hovered over, and nothing selected, send cancel request
831 if (!hovered && nSel() === 0) {
832 sendMessage('cancelTraffic', {});
833 return;
834 }
835
836 // NOTE: hover is only populated if "show traffic on hover" is
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800837 // toggled on, and the item hovered is a host or a device...
838 var hoverId = (trafficHover() && hovered &&
839 (hovered.class === 'host' || hovered.class === 'device'))
Simon Huntd72bc702014-11-13 18:38:04 -0800840 ? hovered.id : '';
841 sendMessage('requestTraffic', {
842 ids: selectOrder,
843 hover: hoverId
844 });
845 }
846
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800847 function showAllTrafficAction() {
848 sendMessage('requestAllTraffic', {});
849 }
850
Simon Huntd72bc702014-11-13 18:38:04 -0800851
Simon Hunt61d04042014-11-11 17:27:16 -0800852 // ==============================
Simon Hunta5e89142014-11-14 07:00:33 -0800853 // onos instance panel functions
854
855 function updateInstances() {
856 var onoses = oiBox.el.selectAll('.onosInst')
857 .data(onosOrder, function (d) { return d.id; });
858
859 // operate on existing onoses if necessary
Simon Huntfcfb46c2014-11-19 12:53:38 -0800860 onoses.classed('online', function (d) { return d.online; });
Simon Hunta5e89142014-11-14 07:00:33 -0800861
862 var entering = onoses.enter()
863 .append('div')
864 .attr('class', 'onosInst')
865 .classed('online', function (d) { return d.online; })
Simon Hunt9c15eca2014-11-15 18:37:59 -0800866 .on('click', clickInst);
867
868 entering.each(function (d, i) {
869 var el = d3.select(this),
870 img;
871
Thomas Vachuskacd2920c2014-11-19 14:49:55 -0800872 $('<img src="img/node.png">').appendTo(el);
Simon Hunt9c15eca2014-11-15 18:37:59 -0800873 img = el.select('img')
874 .attr({
Simon Huntfcfb46c2014-11-19 12:53:38 -0800875 width: 30
Simon Hunt9c15eca2014-11-15 18:37:59 -0800876 });
877
878 $('<div>').attr('class', 'onosTitle').text(d.id).appendTo(el);
879
880 // is the UI attached to this instance?
881 // TODO: need uiAttached boolean in instance data
882 //if (d.uiAttached) {
883 if (i === 0) {
884 $('<img src="img/ui.png">').attr('class','ui').appendTo(el);
885 }
886 });
Simon Hunta5e89142014-11-14 07:00:33 -0800887
888 // operate on existing + new onoses here
889
890 // the departed...
891 var exiting = onoses.exit()
892 .transition()
893 .style('opacity', 0)
894 .remove();
895 }
896
Simon Hunt9462e8c2014-11-14 17:28:09 -0800897 function clickInst(d) {
898 var el = d3.select(this),
899 aff = el.classed('affinity');
900 if (!aff) {
901 setAffinity(el, d);
902 } else {
903 cancelAffinity();
904 }
905 }
906
907 function setAffinity(el, d) {
908 d3.selectAll('.onosInst')
909 .classed('mastership', true)
910 .classed('affinity', false);
911 el.classed('affinity', true);
912
913 suppressLayers(true);
914 node.each(function (n) {
915 if (n.master === d.id) {
916 n.el.classed('suppressed', false);
917 }
918 });
919 oiShowMaster = true;
920 }
921
922 function cancelAffinity() {
923 d3.selectAll('.onosInst')
924 .classed('mastership affinity', false);
925 restoreLayerState();
926 oiShowMaster = false;
927 }
928
Simon Hunta5e89142014-11-14 07:00:33 -0800929 // ==============================
Simon Hunt99c13842014-11-06 18:23:12 -0800930 // force layout modification functions
931
932 function translate(x, y) {
933 return 'translate(' + x + ',' + y + ')';
934 }
935
Simon Hunte2575b62014-11-18 15:25:53 -0800936 function rotate(deg) {
937 return 'rotate(' + deg + ')';
938 }
939
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800940 function missMsg(what, id) {
941 return '\n[' + what + '] "' + id + '" missing ';
942 }
943
944 function linkEndPoints(srcId, dstId) {
945 var srcNode = network.lookup[srcId],
946 dstNode = network.lookup[dstId],
947 sMiss = !srcNode ? missMsg('src', srcId) : '',
948 dMiss = !dstNode ? missMsg('dst', dstId) : '';
949
950 if (sMiss || dMiss) {
951 logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
952 return null;
953 }
954 return {
955 source: srcNode,
956 target: dstNode,
957 x1: srcNode.x,
958 y1: srcNode.y,
959 x2: dstNode.x,
960 y2: dstNode.y
961 };
962 }
963
Simon Hunt56d51852014-11-09 13:03:35 -0800964 function createHostLink(host) {
965 var src = host.id,
966 dst = host.cp.device,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800967 id = host.ingress,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800968 lnk = linkEndPoints(src, dst);
Simon Hunt56d51852014-11-09 13:03:35 -0800969
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800970 if (!lnk) {
Simon Hunt56d51852014-11-09 13:03:35 -0800971 return null;
972 }
973
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800974 // Synthesize link ...
975 $.extend(lnk, {
Simon Hunt8257f4c2014-11-16 19:34:54 -0800976 key: id,
Simon Hunt56d51852014-11-09 13:03:35 -0800977 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -0800978
979 type: function () { return 'hostLink'; },
980 // TODO: ideally, we should see if our edge switch is online...
981 online: function () { return true; },
982 linkWidth: function () { return 1; }
Simon Hunt7cd48f32014-11-09 23:42:50 -0800983 });
Simon Hunt99c13842014-11-06 18:23:12 -0800984 return lnk;
985 }
986
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800987 function createLink(link) {
988 var lnk = linkEndPoints(link.src, link.dst),
989 type = link.type;
990
991 if (!lnk) {
992 return null;
993 }
994
Simon Hunt8257f4c2014-11-16 19:34:54 -0800995 $.extend(lnk, {
996 key: link.id,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800997 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -0800998 fromSource: link,
999
1000 // functions to aggregate dual link state
1001 type: function () {
1002 var s = lnk.fromSource,
1003 t = lnk.fromTarget;
1004 return (s && s.type) || (t && t.type) || defaultLinkType;
1005 },
1006 online: function () {
1007 var s = lnk.fromSource,
1008 t = lnk.fromTarget;
1009 return (s && s.online) || (t && t.online);
1010 },
1011 linkWidth: function () {
1012 var s = lnk.fromSource,
1013 t = lnk.fromTarget,
1014 ws = (s && s.linkWidth) || 0,
1015 wt = (t && t.linkWidth) || 0;
1016 return Math.max(ws, wt);
1017 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001018 });
1019 return lnk;
Simon Hunt1a9eff92014-11-07 11:06:34 -08001020 }
1021
Simon Hunte2575b62014-11-18 15:25:53 -08001022 function removeLinkLabels() {
1023 network.links.forEach(function (d) {
1024 d.label = '';
1025 });
1026 }
1027
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001028 var widthRatio = 1.4,
1029 linkScale = d3.scale.linear()
1030 .domain([1, 12])
1031 .range([widthRatio, 12 * widthRatio])
1032 .clamp(true);
1033
Simon Hunt99c13842014-11-06 18:23:12 -08001034 function updateLinks() {
1035 link = linkG.selectAll('.link')
Simon Hunt8257f4c2014-11-16 19:34:54 -08001036 .data(network.links, function (d) { return d.key; });
Simon Hunt99c13842014-11-06 18:23:12 -08001037
1038 // operate on existing links, if necessary
1039 // link .foo() .bar() ...
1040
1041 // operate on entering links:
1042 var entering = link.enter()
1043 .append('line')
1044 .attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001045 x1: function (d) { return d.x1; },
1046 y1: function (d) { return d.y1; },
1047 x2: function (d) { return d.x2; },
1048 y2: function (d) { return d.y2; },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001049 stroke: config.topo.linkInColor,
1050 'stroke-width': config.topo.linkInWidth
Simon Hunt99c13842014-11-06 18:23:12 -08001051 });
1052
1053 // augment links
Simon Hunt7cd48f32014-11-09 23:42:50 -08001054 entering.each(function (d) {
1055 var link = d3.select(this);
1056 // provide ref to element selection from backing data....
1057 d.el = link;
Simon Hunt8257f4c2014-11-16 19:34:54 -08001058 restyleLinkElement(d);
Simon Hunt7cd48f32014-11-09 23:42:50 -08001059 });
Thomas Vachuska4830d392014-11-09 17:09:56 -08001060
1061 // operate on both existing and new links, if necessary
1062 //link .foo() .bar() ...
1063
Simon Hunte2575b62014-11-18 15:25:53 -08001064 // apply or remove labels
1065 var labelData = getLabelData();
1066 applyLinkLabels(labelData);
1067
Thomas Vachuska4830d392014-11-09 17:09:56 -08001068 // operate on exiting links:
Thomas Vachuska4830d392014-11-09 17:09:56 -08001069 link.exit()
Simon Hunt13bf9c82014-11-18 07:26:44 -08001070 .attr('stroke-dasharray', '3, 3')
1071 .style('opacity', 0.5)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001072 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -08001073 .duration(1500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001074 .attr({
Simon Hunt13bf9c82014-11-18 07:26:44 -08001075 'stroke-dasharray': '3, 12',
1076 stroke: config.topo.linkOutColor,
1077 'stroke-width': config.topo.linkOutWidth
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001078 })
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001079 .style('opacity', 0.0)
Thomas Vachuska4830d392014-11-09 17:09:56 -08001080 .remove();
Simon Hunte2575b62014-11-18 15:25:53 -08001081
1082 // NOTE: invoke a single tick to force the labels to position
1083 // onto their links.
1084 tick();
1085 }
1086
1087 function getLabelData() {
1088 // create the backing data for showing labels..
1089 var data = [];
1090 link.each(function (d) {
1091 if (d.label) {
1092 data.push({
1093 id: 'lab-' + d.key,
1094 key: d.key,
1095 label: d.label,
1096 ldata: d
1097 });
1098 }
1099 });
1100 return data;
1101 }
1102
1103 var linkLabelOffset = '0.3em';
1104
1105 function applyLinkLabels(data) {
1106 var entering;
1107
1108 linkLabel = linkLabelG.selectAll('.linkLabel')
1109 .data(data, function (d) { return d.id; });
1110
Simon Hunt56a2ea42014-11-19 12:39:31 -08001111 // for elements already existing, we need to update the text
1112 // and adjust the rectangle size to fit
1113 linkLabel.each(function (d) {
1114 var el = d3.select(this),
1115 rect = el.select('rect'),
1116 text = el.select('text');
1117 text.text(d.label);
1118 rect.attr(rectAroundText(el));
1119 });
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -08001120
Simon Hunte2575b62014-11-18 15:25:53 -08001121 entering = linkLabel.enter().append('g')
1122 .classed('linkLabel', true)
1123 .attr('id', function (d) { return d.id; });
1124
1125 entering.each(function (d) {
1126 var el = d3.select(this),
1127 rect,
1128 text,
1129 parms = {
1130 x1: d.ldata.x1,
1131 y1: d.ldata.y1,
1132 x2: d.ldata.x2,
1133 y2: d.ldata.y2
1134 };
1135
1136 d.el = el;
1137 rect = el.append('rect');
1138 text = el.append('text').text(d.label);
1139 rect.attr(rectAroundText(el));
1140 text.attr('dy', linkLabelOffset);
1141
1142 el.attr('transform', transformLabel(parms));
1143 });
1144
1145 // Remove any links that are no longer required.
1146 linkLabel.exit().remove();
1147 }
1148
1149 function rectAroundText(el) {
1150 var text = el.select('text'),
1151 box = text.node().getBBox();
1152
1153 // translate the bbox so that it is centered on [x,y]
1154 box.x = -box.width / 2;
1155 box.y = -box.height / 2;
1156
1157 // add padding
1158 box.x -= 1;
1159 box.width += 2;
1160 return box;
1161 }
1162
1163 function transformLabel(p) {
1164 var dx = p.x2 - p.x1,
1165 dy = p.y2 - p.y1,
1166 xMid = dx/2 + p.x1,
1167 yMid = dy/2 + p.y1;
1168 //length = Math.sqrt(dx*dx + dy*dy),
1169 //rads = Math.asin(dy/length),
1170 //degs = rads / (Math.PI*2) * 360;
1171
1172 return translate(xMid, yMid);
1173
1174 // TODO: consider making label parallel to line
1175 //return [
1176 // translate(xMid, yMid),
1177 // rotate(degs),
1178 // translate(0, 8)
1179 //].join('');
Simon Hunt99c13842014-11-06 18:23:12 -08001180 }
1181
1182 function createDeviceNode(device) {
1183 // start with the object as is
1184 var node = device,
Simon Huntbb282f52014-11-10 11:08:19 -08001185 type = device.type,
1186 svgCls = type ? 'node device ' + type : 'node device';
Simon Hunt99c13842014-11-06 18:23:12 -08001187
1188 // Augment as needed...
1189 node.class = 'device';
Simon Huntbb282f52014-11-10 11:08:19 -08001190 node.svgClass = device.online ? svgCls + ' online' : svgCls;
Simon Hunt99c13842014-11-06 18:23:12 -08001191 positionNode(node);
1192
1193 // cache label array length
1194 network.deviceLabelCount = device.labels.length;
Simon Hunt99c13842014-11-06 18:23:12 -08001195 return node;
1196 }
1197
Simon Hunt56d51852014-11-09 13:03:35 -08001198 function createHostNode(host) {
1199 // start with the object as is
1200 var node = host;
1201
1202 // Augment as needed...
1203 node.class = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001204 if (!node.type) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08001205 node.type = 'endstation';
1206 }
Simon Hunt7fa116d2014-11-17 14:16:55 -08001207 node.svgClass = 'node host ' + node.type;
Simon Hunt56d51852014-11-09 13:03:35 -08001208 positionNode(node);
1209
1210 // cache label array length
1211 network.hostLabelCount = host.labels.length;
Simon Hunt56d51852014-11-09 13:03:35 -08001212 return node;
1213 }
1214
Simon Hunt99c13842014-11-06 18:23:12 -08001215 function positionNode(node) {
1216 var meta = node.metaUi,
Simon Huntac9e24f2014-11-12 10:12:21 -08001217 x = meta && meta.x,
1218 y = meta && meta.y,
1219 xy;
Simon Hunt99c13842014-11-06 18:23:12 -08001220
Simon Huntac9e24f2014-11-12 10:12:21 -08001221 // If we have [x,y] already, use that...
Simon Hunt99c13842014-11-06 18:23:12 -08001222 if (x && y) {
1223 node.fixed = true;
Simon Huntac9e24f2014-11-12 10:12:21 -08001224 node.x = x;
1225 node.y = y;
1226 return;
Simon Hunt99c13842014-11-06 18:23:12 -08001227 }
Simon Huntac9e24f2014-11-12 10:12:21 -08001228
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001229 var location = node.location;
1230 if (location && location.type === 'latlng') {
1231 var coord = geoMapProjection([location.lng, location.lat]);
1232 node.fixed = true;
1233 node.x = coord[0];
1234 node.y = coord[1];
1235 return;
1236 }
1237
Simon Huntac9e24f2014-11-12 10:12:21 -08001238 // Note: Placing incoming unpinned nodes at exactly the same point
1239 // (center of the view) causes them to explode outwards when
1240 // the force layout kicks in. So, we spread them out a bit
1241 // initially, to provide a more serene layout convergence.
1242 // Additionally, if the node is a host, we place it near
1243 // the device it is connected to.
1244
1245 function spread(s) {
1246 return Math.floor((Math.random() * s) - s/2);
1247 }
1248
1249 function randDim(dim) {
1250 return dim / 2 + spread(dim * 0.7071);
1251 }
1252
1253 function rand() {
1254 return {
1255 x: randDim(network.view.width()),
1256 y: randDim(network.view.height())
1257 };
1258 }
1259
1260 function near(node) {
1261 var min = 12,
1262 dx = spread(12),
1263 dy = spread(12);
1264 return {
1265 x: node.x + min + dx,
1266 y: node.y + min + dy
1267 };
1268 }
1269
1270 function getDevice(cp) {
1271 var d = network.lookup[cp.device];
1272 return d || rand();
1273 }
1274
1275 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
1276 $.extend(node, xy);
Simon Hunt99c13842014-11-06 18:23:12 -08001277 }
1278
Thomas Vachuska89543292014-11-19 11:28:33 -08001279 function iconUrl(d) {
1280 return 'img/' + d.type + '.png';
Simon Hunt99c13842014-11-06 18:23:12 -08001281 }
1282
1283 // returns the newly computed bounding box of the rectangle
1284 function adjustRectToFitText(n) {
1285 var text = n.select('text'),
1286 box = text.node().getBBox(),
1287 lab = config.labels;
1288
1289 text.attr('text-anchor', 'middle')
1290 .attr('y', '-0.8em')
1291 .attr('x', lab.imgPad/2);
1292
1293 // translate the bbox so that it is centered on [x,y]
1294 box.x = -box.width / 2;
1295 box.y = -box.height / 2;
1296
1297 // add padding
1298 box.x -= (lab.padLR + lab.imgPad/2);
1299 box.width += lab.padLR * 2 + lab.imgPad;
1300 box.y -= lab.padTB;
1301 box.height += lab.padTB * 2;
1302
1303 return box;
1304 }
1305
Simon Hunt1a9eff92014-11-07 11:06:34 -08001306 function mkSvgClass(d) {
1307 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
1308 }
1309
Simon Hunt7cd48f32014-11-09 23:42:50 -08001310 function hostLabel(d) {
1311 var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0;
1312 return d.labels[idx];
1313 }
1314 function deviceLabel(d) {
1315 var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0;
1316 return d.labels[idx];
1317 }
1318 function niceLabel(label) {
1319 return (label && label.trim()) ? label : '.';
1320 }
1321
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001322 function updateDeviceLabel(d) {
1323 var label = niceLabel(deviceLabel(d)),
1324 node = d.el,
1325 box;
1326
1327 node.select('text')
1328 .text(label)
1329 .style('opacity', 0)
1330 .transition()
1331 .style('opacity', 1);
1332
1333 box = adjustRectToFitText(node);
1334
1335 node.select('rect')
1336 .transition()
1337 .attr(box);
1338
Simon Hunt56a2ea42014-11-19 12:39:31 -08001339 node.select('rect.iconUnderlay')
Thomas Vachuska89543292014-11-19 11:28:33 -08001340 .transition()
1341 .attr('x', box.x + config.icons.xoff)
1342 .attr('y', box.y + config.icons.yoff);
Simon Hunt56a2ea42014-11-19 12:39:31 -08001343
1344 node.select('image')
1345 .transition()
1346 .attr('x', box.x + config.icons.xoff + 2)
1347 .attr('y', box.y + config.icons.yoff + 2);
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001348 }
1349
1350 function updateHostLabel(d) {
1351 var label = hostLabel(d),
1352 host = d.el;
1353
1354 host.select('text').text(label);
1355 }
1356
Simon Huntbb282f52014-11-10 11:08:19 -08001357 function updateDeviceState(nodeData) {
1358 nodeData.el.classed('online', nodeData.online);
1359 updateDeviceLabel(nodeData);
1360 // TODO: review what else might need to be updated
1361 }
1362
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001363 function updateLinkState(linkData) {
1364 updateLinkWidth(linkData);
Thomas Vachuskabadb93f2014-11-15 23:51:17 -08001365 linkData.el.classed('inactive', !linkData.online);
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001366 // TODO: review what else might need to be updated
1367 // update label, if showing
1368 }
1369
Simon Huntbb282f52014-11-10 11:08:19 -08001370 function updateHostState(hostData) {
1371 updateHostLabel(hostData);
1372 // TODO: review what else might need to be updated
1373 }
1374
Simon Hunt6ac93f32014-11-13 12:17:27 -08001375 function nodeMouseOver(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001376 hovered = d;
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -08001377 if (trafficHover() && (d.class === 'host' || d.class === 'device')) {
Simon Huntd72bc702014-11-13 18:38:04 -08001378 showTrafficAction();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001379 }
1380 }
1381
1382 function nodeMouseOut(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001383 hovered = null;
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -08001384 if (trafficHover() && (d.class === 'host' || d.class === 'device')) {
Simon Huntd72bc702014-11-13 18:38:04 -08001385 showTrafficAction();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001386 }
1387 }
Simon Huntbb282f52014-11-10 11:08:19 -08001388
Thomas Vachuska89543292014-11-19 11:28:33 -08001389 function addHostIcon(node, radius, iconId) {
1390 var dim = radius * 1.5,
1391 xlate = -dim / 2;
1392
1393 node.append('svg:image')
1394 .attr('transform', translate(xlate,xlate))
1395 .attr('xlink:href', 'img/' + iconId + '.png')
1396 .attr('width', dim)
1397 .attr('height', dim);
1398 }
1399
Simon Hunt99c13842014-11-06 18:23:12 -08001400 function updateNodes() {
1401 node = nodeG.selectAll('.node')
1402 .data(network.nodes, function (d) { return d.id; });
1403
1404 // operate on existing nodes, if necessary
Simon Hunt7cd48f32014-11-09 23:42:50 -08001405 // update host labels
Simon Hunt99c13842014-11-06 18:23:12 -08001406 //node .foo() .bar() ...
1407
1408 // operate on entering nodes:
1409 var entering = node.enter()
1410 .append('g')
1411 .attr({
1412 id: function (d) { return safeId(d.id); },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001413 class: mkSvgClass,
Simon Hunt99c13842014-11-06 18:23:12 -08001414 transform: function (d) { return translate(d.x, d.y); },
1415 opacity: 0
1416 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08001417 .call(network.drag)
Simon Hunt6ac93f32014-11-13 12:17:27 -08001418 .on('mouseover', nodeMouseOver)
1419 .on('mouseout', nodeMouseOut)
Simon Hunt99c13842014-11-06 18:23:12 -08001420 .transition()
1421 .attr('opacity', 1);
1422
1423 // augment device nodes...
1424 entering.filter('.device').each(function (d) {
1425 var node = d3.select(this),
Thomas Vachuska89543292014-11-19 11:28:33 -08001426 icon = iconUrl(d),
Simon Hunt7cd48f32014-11-09 23:42:50 -08001427 label = niceLabel(deviceLabel(d)),
Simon Hunt99c13842014-11-06 18:23:12 -08001428 box;
1429
Simon Hunt7cd48f32014-11-09 23:42:50 -08001430 // provide ref to element from backing data....
1431 d.el = node;
1432
Simon Hunt99c13842014-11-06 18:23:12 -08001433 node.append('rect')
1434 .attr({
1435 'rx': 5,
1436 'ry': 5
1437 });
1438
1439 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001440 .text(label)
Simon Hunt99c13842014-11-06 18:23:12 -08001441 .attr('dy', '1.1em');
1442
1443 box = adjustRectToFitText(node);
1444
1445 node.select('rect')
1446 .attr(box);
1447
Thomas Vachuska89543292014-11-19 11:28:33 -08001448 if (icon) {
1449 var cfg = config.icons;
1450 node.append('rect')
Simon Hunt99c13842014-11-06 18:23:12 -08001451 .attr({
Simon Hunt56a2ea42014-11-19 12:39:31 -08001452 class: 'iconUnderlay',
Thomas Vachuska89543292014-11-19 11:28:33 -08001453 x: box.x + config.icons.xoff,
1454 y: box.y + config.icons.yoff,
1455 width: cfg.w,
1456 height: cfg.h,
1457 rx: 4
1458 }).style({
1459 stroke: '#000',
1460 fill: '#ddd'
1461 });
1462 node.append('svg:image')
1463 .attr({
1464 x: box.x + config.icons.xoff + 2,
1465 y: box.y + config.icons.yoff + 2,
1466 width: cfg.w - 4,
1467 height: cfg.h - 4,
1468 'xlink:href': icon
Simon Hunt99c13842014-11-06 18:23:12 -08001469 });
1470 }
1471
1472 // debug function to show the modelled x,y coordinates of nodes...
1473 if (debug('showNodeXY')) {
1474 node.select('rect').attr('fill-opacity', 0.5);
1475 node.append('circle')
1476 .attr({
1477 class: 'debug',
1478 cx: 0,
1479 cy: 0,
1480 r: '3px'
1481 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001482 }
1483 });
Simon Hunt934c3ce2014-11-05 11:45:07 -08001484
Thomas Vachuska89543292014-11-19 11:28:33 -08001485 // TODO: better place for this configuration state
1486 var defaultHostRadius = 9,
1487 hostRadius = {
1488 bgpSpeaker: 14,
1489 router: 14,
1490 host: 14
1491 },
1492 hostIcon = {
1493 bgpSpeaker: 'bgpSpeaker',
1494 router: 'router',
1495 host: 'host'
1496 };
1497
1498
Simon Hunt56d51852014-11-09 13:03:35 -08001499 // augment host nodes...
1500 entering.filter('.host').each(function (d) {
1501 var node = d3.select(this),
Thomas Vachuska89543292014-11-19 11:28:33 -08001502 r = hostRadius[d.type] || defaultHostRadius,
1503 textDy = r + 10,
1504 icon = hostIcon[d.type];
Simon Hunt56d51852014-11-09 13:03:35 -08001505
Simon Hunt7cd48f32014-11-09 23:42:50 -08001506 // provide ref to element from backing data....
1507 d.el = node;
1508
Thomas Vachuska89543292014-11-19 11:28:33 -08001509 node.append('circle')
1510 .attr('r', r);
Simon Hunt7fa116d2014-11-17 14:16:55 -08001511
Thomas Vachuska89543292014-11-19 11:28:33 -08001512 if (icon) {
1513 addHostIcon(node, r, icon);
Simon Hunt7fa116d2014-11-17 14:16:55 -08001514 }
Simon Hunt56d51852014-11-09 13:03:35 -08001515
Paul Greyson29cd58f2014-11-18 13:14:57 -08001516
Simon Hunt56d51852014-11-09 13:03:35 -08001517 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001518 .text(hostLabel)
Thomas Vachuska89543292014-11-19 11:28:33 -08001519 .attr('dy', textDy)
Simon Hunt7cd48f32014-11-09 23:42:50 -08001520 .attr('text-anchor', 'middle');
Simon Hunt56d51852014-11-09 13:03:35 -08001521
1522 // debug function to show the modelled x,y coordinates of nodes...
1523 if (debug('showNodeXY')) {
1524 node.select('circle').attr('fill-opacity', 0.5);
1525 node.append('circle')
1526 .attr({
1527 class: 'debug',
1528 cx: 0,
1529 cy: 0,
1530 r: '3px'
1531 });
1532 }
1533 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001534
Simon Hunt99c13842014-11-06 18:23:12 -08001535 // operate on both existing and new nodes, if necessary
1536 //node .foo() .bar() ...
Simon Huntc7ee0662014-11-05 16:44:37 -08001537
Simon Hunt99c13842014-11-06 18:23:12 -08001538 // operate on exiting nodes:
Simon Huntea80eb42014-11-11 13:46:57 -08001539 // Note that the node is removed after 2 seconds.
1540 // Sub element animations should be shorter than 2 seconds.
1541 var exiting = node.exit()
Simon Hunt44031102014-11-11 13:20:36 -08001542 .transition()
1543 .duration(2000)
Simon Huntea80eb42014-11-11 13:46:57 -08001544 .style('opacity', 0)
Simon Hunt99c13842014-11-06 18:23:12 -08001545 .remove();
Simon Huntea80eb42014-11-11 13:46:57 -08001546
1547 // host node exits....
1548 exiting.filter('.host').each(function (d) {
1549 var node = d3.select(this);
1550
1551 node.select('text')
1552 .style('opacity', 0.5)
1553 .transition()
1554 .duration(1000)
1555 .style('opacity', 0);
1556 // note, leave <g>.remove to remove this element
1557
Thomas Vachuska89543292014-11-19 11:28:33 -08001558 node.select('circle')
1559 .style('stroke-fill', '#555')
1560 .style('fill', '#888')
Simon Huntea80eb42014-11-11 13:46:57 -08001561 .style('opacity', 0.5)
1562 .transition()
1563 .duration(1500)
1564 .attr('r', 0);
1565 // note, leave <g>.remove to remove this element
1566
1567 });
1568
1569 // TODO: device node exits
Simon Huntc7ee0662014-11-05 16:44:37 -08001570 }
1571
Simon Hunt8257f4c2014-11-16 19:34:54 -08001572 function find(key, array) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001573 for (var idx = 0, n = array.length; idx < n; idx++) {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001574 if (array[idx].key === key) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001575 return idx;
1576 }
1577 }
1578 return -1;
1579 }
1580
1581 function removeLinkElement(linkData) {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001582 var idx = find(linkData.key, network.links),
1583 removed;
1584 if (idx >=0) {
1585 // remove from links array
1586 removed = network.links.splice(idx, 1);
1587 // remove from lookup cache
1588 delete network.lookup[removed[0].key];
1589 updateLinks();
1590 network.force.resume();
1591 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001592 }
Simon Huntc7ee0662014-11-05 16:44:37 -08001593
Simon Hunt44031102014-11-11 13:20:36 -08001594 function removeHostElement(hostData) {
1595 // first, remove associated hostLink...
1596 removeLinkElement(hostData.linkData);
1597
1598 // remove from lookup cache
1599 delete network.lookup[hostData.id];
1600 // remove from nodes array
1601 var idx = find(hostData.id, network.nodes);
1602 network.nodes.splice(idx, 1);
1603 // remove from SVG
1604 updateNodes();
1605 network.force.resume();
1606 }
1607
1608
Simon Huntc7ee0662014-11-05 16:44:37 -08001609 function tick() {
1610 node.attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001611 transform: function (d) { return translate(d.x, d.y); }
Simon Huntc7ee0662014-11-05 16:44:37 -08001612 });
1613
1614 link.attr({
1615 x1: function (d) { return d.source.x; },
1616 y1: function (d) { return d.source.y; },
1617 x2: function (d) { return d.target.x; },
1618 y2: function (d) { return d.target.y; }
1619 });
Simon Hunte2575b62014-11-18 15:25:53 -08001620
1621 linkLabel.each(function (d) {
1622 var el = d3.select(this);
1623 var lnk = findLinkById(d.key),
1624 parms = {
1625 x1: lnk.source.x,
1626 y1: lnk.source.y,
1627 x2: lnk.target.x,
1628 y2: lnk.target.y
1629 };
1630 el.attr('transform', transformLabel(parms));
1631 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001632 }
Simon Hunt934c3ce2014-11-05 11:45:07 -08001633
1634 // ==============================
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001635 // Web-Socket for live data
1636
1637 function webSockUrl() {
1638 return document.location.toString()
1639 .replace(/\#.*/, '')
1640 .replace('http://', 'ws://')
1641 .replace('https://', 'wss://')
1642 .replace('index2.html', config.webSockUrl);
1643 }
1644
1645 webSock = {
1646 ws : null,
1647
1648 connect : function() {
1649 webSock.ws = new WebSocket(webSockUrl());
1650
1651 webSock.ws.onopen = function() {
Simon Hunt0c6d4192014-11-12 12:07:10 -08001652 noWebSock(false);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001653 };
1654
1655 webSock.ws.onmessage = function(m) {
1656 if (m.data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001657 wsTraceRx(m.data);
Thomas Vachuskad472c6e2014-11-07 19:11:05 -08001658 handleServerEvent(JSON.parse(m.data));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001659 }
1660 };
1661
1662 webSock.ws.onclose = function(m) {
1663 webSock.ws = null;
Simon Hunt0c6d4192014-11-12 12:07:10 -08001664 noWebSock(true);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001665 };
1666 },
1667
1668 send : function(text) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001669 if (text != null) {
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001670 webSock._send(text);
1671 }
1672 },
1673
1674 _send : function(message) {
1675 if (webSock.ws) {
1676 webSock.ws.send(message);
Simon Hunta255a2c2014-11-13 22:29:35 -08001677 } else if (config.useLiveData) {
Simon Hunt56d51852014-11-09 13:03:35 -08001678 network.view.alert('no web socket open\n\n' + message);
Simon Hunta255a2c2014-11-13 22:29:35 -08001679 } else {
1680 console.log('WS Send: ' + JSON.stringify(message));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001681 }
1682 }
1683
1684 };
1685
Simon Hunt0c6d4192014-11-12 12:07:10 -08001686 function noWebSock(b) {
1687 mask.style('display',b ? 'block' : 'none');
1688 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001689
1690 function sendMessage(evType, payload) {
1691 var toSend = {
Simon Huntbb282f52014-11-10 11:08:19 -08001692 event: evType,
1693 sid: ++sid,
1694 payload: payload
1695 },
1696 asText = JSON.stringify(toSend);
1697 wsTraceTx(asText);
1698 webSock.send(asText);
Simon Huntc76ae892014-11-18 17:31:51 -08001699
1700 // Temporary measure for debugging UI behavior ...
1701 if (!config.useLiveData) {
1702 handleTestSend(toSend);
1703 }
Simon Huntbb282f52014-11-10 11:08:19 -08001704 }
1705
1706 function wsTraceTx(msg) {
1707 wsTrace('tx', msg);
1708 }
1709 function wsTraceRx(msg) {
1710 wsTrace('rx', msg);
1711 }
1712 function wsTrace(rxtx, msg) {
Simon Huntbb282f52014-11-10 11:08:19 -08001713 console.log('[' + rxtx + '] ' + msg);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001714 }
1715
Simon Huntc76ae892014-11-18 17:31:51 -08001716 // NOTE: Temporary hardcoded example for showing detail pane
1717 // while we fine-
1718 // Probably should not merge this change...
1719 function handleTestSend(msg) {
1720 if (msg.event === 'requestDetails') {
1721 showDetails({
1722 event: 'showDetails',
1723 sid: 1001,
1724 payload: {
1725 "id": "of:0000ffffffffff09",
1726 "type": "roadm",
1727 "propOrder": [
1728 "Name",
1729 "Vendor",
1730 "H/W Version",
1731 "S/W Version",
1732 "-",
1733 "Latitude",
1734 "Longitude",
1735 "Ports"
1736 ],
1737 "props": {
1738 "Name": null,
1739 "Vendor": "Linc",
1740 "H/W Version": "OE",
1741 "S/W Version": "?",
1742 "-": "",
1743 "Latitude": "40.8",
1744 "Longitude": "73.1",
1745 "Ports": "2"
1746 }
1747 }
1748 });
1749 }
1750 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001751
1752 // ==============================
1753 // Selection stuff
1754
1755 function selectObject(obj, el) {
1756 var n,
Simon Hunt01095ff2014-11-13 16:37:29 -08001757 srcEv = d3.event.sourceEvent,
1758 meta = srcEv.metaKey,
1759 shift = srcEv.shiftKey;
1760
Simon Huntdeab4322014-11-13 18:49:07 -08001761 if ((panZoom() && !meta) || (!panZoom() && meta)) {
Simon Hunt01095ff2014-11-13 16:37:29 -08001762 return;
1763 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001764
1765 if (el) {
1766 n = d3.select(el);
1767 } else {
1768 node.each(function(d) {
1769 if (d == obj) {
1770 n = d3.select(el = this);
1771 }
1772 });
1773 }
1774 if (!n) return;
1775
Simon Hunt01095ff2014-11-13 16:37:29 -08001776 if (shift && n.classed('selected')) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001777 deselectObject(obj.id);
Simon Hunt61d04042014-11-11 17:27:16 -08001778 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001779 return;
1780 }
1781
Simon Hunt01095ff2014-11-13 16:37:29 -08001782 if (!shift) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001783 deselectAll();
1784 }
1785
Simon Huntc31d5692014-11-12 13:27:18 -08001786 selections[obj.id] = { obj: obj, el: el };
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001787 selectOrder.push(obj.id);
1788
1789 n.classed('selected', true);
Simon Hunt61d04042014-11-11 17:27:16 -08001790 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001791 }
1792
1793 function deselectObject(id) {
Simon Huntc31d5692014-11-12 13:27:18 -08001794 var obj = selections[id],
1795 idx;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001796 if (obj) {
1797 d3.select(obj.el).classed('selected', false);
Simon Hunt61d04042014-11-11 17:27:16 -08001798 delete selections[id];
Simon Huntc31d5692014-11-12 13:27:18 -08001799 idx = $.inArray(id, selectOrder);
1800 if (idx >= 0) {
1801 selectOrder.splice(idx, 1);
1802 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001803 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001804 }
1805
1806 function deselectAll() {
1807 // deselect all nodes in the network...
1808 node.classed('selected', false);
1809 selections = {};
1810 selectOrder = [];
Simon Hunt61d04042014-11-11 17:27:16 -08001811 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001812 }
1813
Simon Hunt61d04042014-11-11 17:27:16 -08001814 // update the state of the detail pane, based on current selections
1815 function updateDetailPane() {
1816 var nSel = selectOrder.length;
1817 if (!nSel) {
1818 detailPane.hide();
Simon Huntd72bc702014-11-13 18:38:04 -08001819 showTrafficAction(); // sends cancelTraffic event
Simon Hunt61d04042014-11-11 17:27:16 -08001820 } else if (nSel === 1) {
1821 singleSelect();
1822 } else {
1823 multiSelect();
1824 }
1825 }
1826
1827 function singleSelect() {
1828 requestDetails();
Simon Huntb53e0682014-11-12 13:32:01 -08001829 // NOTE: detail pane will be shown from showDetails event callback
Simon Hunt61d04042014-11-11 17:27:16 -08001830 }
1831
1832 function multiSelect() {
Simon Huntb53e0682014-11-12 13:32:01 -08001833 populateMultiSelect();
Simon Huntb53e0682014-11-12 13:32:01 -08001834 }
1835
1836 function addSep(tbody) {
1837 var tr = tbody.append('tr');
1838 $('<hr>').appendTo(tr.append('td').attr('colspan', 2));
1839 }
1840
1841 function addProp(tbody, label, value) {
1842 var tr = tbody.append('tr');
1843
1844 tr.append('td')
1845 .attr('class', 'label')
1846 .text(label + ' :');
1847
1848 tr.append('td')
1849 .attr('class', 'value')
1850 .text(value);
1851 }
1852
1853 function populateMultiSelect() {
1854 detailPane.empty();
1855
1856 var title = detailPane.append("h2"),
1857 table = detailPane.append("table"),
1858 tbody = table.append("tbody");
1859
1860 title.text('Multi-Select...');
1861
1862 selectOrder.forEach(function (d, i) {
1863 addProp(tbody, i+1, d);
1864 });
Simon Huntd72bc702014-11-13 18:38:04 -08001865
1866 addMultiSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08001867 }
1868
1869 function populateDetails(data) {
1870 detailPane.empty();
1871
1872 var title = detailPane.append("h2"),
1873 table = detailPane.append("table"),
1874 tbody = table.append("tbody");
1875
1876 $('<img src="img/' + data.type + '.png">').appendTo(title);
1877 $('<span>').attr('class', 'icon').text(data.id).appendTo(title);
1878
1879 data.propOrder.forEach(function(p) {
1880 if (p === '-') {
1881 addSep(tbody);
1882 } else {
1883 addProp(tbody, p, data.props[p]);
1884 }
1885 });
Simon Huntd72bc702014-11-13 18:38:04 -08001886
1887 addSingleSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08001888 }
1889
Simon Huntd72bc702014-11-13 18:38:04 -08001890 function addSingleSelectActions() {
1891 detailPane.append('hr');
1892 // always want to allow 'show traffic'
Simon Hunta5e89142014-11-14 07:00:33 -08001893 addAction(detailPane, 'Show Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001894 }
1895
1896 function addMultiSelectActions() {
1897 detailPane.append('hr');
1898 // always want to allow 'show traffic'
Simon Hunta5e89142014-11-14 07:00:33 -08001899 addAction(detailPane, 'Show Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001900 // if exactly two hosts are selected, also want 'add host intent'
1901 if (nSel() === 2 && allSelectionsClass('host')) {
Simon Hunta5e89142014-11-14 07:00:33 -08001902 addAction(detailPane, 'Add Host Intent', addIntentAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001903 }
1904 }
1905
Simon Hunta5e89142014-11-14 07:00:33 -08001906 function addAction(panel, text, cb) {
1907 panel.append('div')
Simon Huntd72bc702014-11-13 18:38:04 -08001908 .classed('actionBtn', true)
1909 .text(text)
1910 .on('click', cb);
1911 }
1912
1913
Paul Greysonfcba0e82014-11-13 10:21:16 -08001914 function zoomPan(scale, translate) {
1915 zoomPanContainer.attr("transform", "translate(" + translate + ")scale(" + scale + ")");
1916 // keep the map lines constant width while zooming
Thomas Vachuska89543292014-11-19 11:28:33 -08001917 bgImg.style("stroke-width", 2.0 / scale + "px");
Paul Greysonfcba0e82014-11-13 10:21:16 -08001918 }
1919
1920 function resetZoomPan() {
1921 zoomPan(1, [0,0]);
1922 zoom.scale(1).translate([0,0]);
1923 }
1924
1925 function setupZoomPan() {
1926 function zoomed() {
Simon Huntdeab4322014-11-13 18:49:07 -08001927 if (!panZoom() ^ !d3.event.sourceEvent.metaKey) {
Paul Greysonfcba0e82014-11-13 10:21:16 -08001928 zoomPan(d3.event.scale, d3.event.translate);
1929 }
1930 }
1931
1932 zoom = d3.behavior.zoom()
1933 .translate([0, 0])
1934 .scale(1)
1935 .scaleExtent([1, 8])
1936 .on("zoom", zoomed);
1937
1938 svg.call(zoom);
1939 }
1940
Simon Hunt61d04042014-11-11 17:27:16 -08001941 // ==============================
1942 // Test harness code
Simon Hunt56d51852014-11-09 13:03:35 -08001943
1944 function prepareScenario(view, ctx, dbg) {
1945 var sc = scenario,
1946 urlSc = sc.evDir + ctx + sc.evScenario;
1947
1948 if (!ctx) {
1949 view.alert("No scenario specified (null ctx)");
1950 return;
1951 }
1952
1953 sc.view = view;
1954 sc.ctx = ctx;
1955 sc.debug = dbg;
1956 sc.evNumber = 0;
1957
1958 d3.json(urlSc, function(err, data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001959 var p = data && data.params || {},
1960 desc = data && data.description || null,
Simon Huntfc274c92014-11-11 11:05:46 -08001961 intro = data && data.title;
Simon Huntbb282f52014-11-10 11:08:19 -08001962
Simon Hunt56d51852014-11-09 13:03:35 -08001963 if (err) {
1964 view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err);
1965 } else {
1966 sc.params = p;
Simon Huntbb282f52014-11-10 11:08:19 -08001967 if (desc) {
1968 intro += '\n\n ' + desc.join('\n ');
1969 }
1970 view.alert(intro);
Simon Hunt56d51852014-11-09 13:03:35 -08001971 }
1972 });
1973
1974 }
1975
Simon Hunt01095ff2014-11-13 16:37:29 -08001976 // ==============================
1977 // Toggle Buttons in masthead
Simon Hunt0c6d4192014-11-12 12:07:10 -08001978
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001979 // TODO: toggle button (and other widgets in the masthead) should be provided
1980 // by the framework; not generated by the view.
1981
Simon Hunta5e89142014-11-14 07:00:33 -08001982 var showInstances,
1983 doPanZoom,
1984 showTrafficOnHover;
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001985
1986 function addButtonBar(view) {
1987 var bb = d3.select('#mast')
1988 .append('span').classed('right', true).attr('id', 'bb');
1989
Simon Hunta5e89142014-11-14 07:00:33 -08001990 function mkTogBtn(text, cb) {
1991 return bb.append('span')
1992 .classed('btn', true)
1993 .text(text)
1994 .on('click', cb);
1995 }
Simon Hunt01095ff2014-11-13 16:37:29 -08001996
Simon Hunta5e89142014-11-14 07:00:33 -08001997 showInstances = mkTogBtn('Show Instances', toggleInst);
Simon Hunte5b71752014-11-18 20:06:07 -08001998 //doPanZoom = mkTogBtn('Pan/Zoom', togglePanZoom);
1999 //showTrafficOnHover = mkTogBtn('Show traffic on hover', toggleTrafficHover);
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002000 }
2001
Simon Hunta5e89142014-11-14 07:00:33 -08002002 function instShown() {
2003 return showInstances.classed('active');
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002004 }
Simon Hunta5e89142014-11-14 07:00:33 -08002005 function toggleInst() {
2006 showInstances.classed('active', !instShown());
2007 if (instShown()) {
2008 oiBox.show();
2009 } else {
2010 oiBox.hide();
2011 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002012 }
2013
Simon Huntdeab4322014-11-13 18:49:07 -08002014 function panZoom() {
Simon Hunte5b71752014-11-18 20:06:07 -08002015 return false;
2016 //return doPanZoom.classed('active');
Simon Hunt01095ff2014-11-13 16:37:29 -08002017 }
Simon Hunta5e89142014-11-14 07:00:33 -08002018 function togglePanZoom() {
2019 doPanZoom.classed('active', !panZoom());
2020 }
2021
2022 function trafficHover() {
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -08002023 return hoverEnabled;
Simon Hunta5e89142014-11-14 07:00:33 -08002024 }
2025 function toggleTrafficHover() {
2026 showTrafficOnHover.classed('active', !trafficHover());
2027 }
2028
Simon Hunt7fa116d2014-11-17 14:16:55 -08002029 function loadGlyphs(svg) {
2030 var defs = svg.append('defs');
2031 gly.defBird(defs);
2032 gly.defBullhorn(defs);
2033 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002034
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002035 // ==============================
Simon Hunt142d0032014-11-04 20:13:09 -08002036 // View life-cycle callbacks
Simon Hunt195cb382014-11-03 17:50:51 -08002037
Simon Huntf67722a2014-11-10 09:32:06 -08002038 function preload(view, ctx, flags) {
Simon Hunt142d0032014-11-04 20:13:09 -08002039 var w = view.width(),
2040 h = view.height(),
Simon Huntc7ee0662014-11-05 16:44:37 -08002041 fcfg = config.force,
2042 fpad = fcfg.pad,
2043 forceDim = [w - 2*fpad, h - 2*fpad];
Simon Hunt195cb382014-11-03 17:50:51 -08002044
Simon Hunt142d0032014-11-04 20:13:09 -08002045 // NOTE: view.$div is a D3 selection of the view's div
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002046 var viewBox = '0 0 ' + config.logicalSize + ' ' + config.logicalSize;
2047 svg = view.$div.append('svg').attr('viewBox', viewBox);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002048 setSize(svg, view);
2049
Simon Hunt7fa116d2014-11-17 14:16:55 -08002050 loadGlyphs(svg);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002051
Paul Greysonfcba0e82014-11-13 10:21:16 -08002052 zoomPanContainer = svg.append('g').attr('id', 'zoomPanContainer');
Paul Greysonfcba0e82014-11-13 10:21:16 -08002053 setupZoomPan();
2054
Simon Hunt1a9eff92014-11-07 11:06:34 -08002055 // add blue glow filter to svg layer
Paul Greysonfcba0e82014-11-13 10:21:16 -08002056 d3u.appendGlow(zoomPanContainer);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002057
Simon Huntc7ee0662014-11-05 16:44:37 -08002058 // group for the topology
Paul Greysonfcba0e82014-11-13 10:21:16 -08002059 topoG = zoomPanContainer.append('g')
Simon Huntd3b7d512014-11-12 15:48:41 -08002060 .attr('id', 'topo-G')
Simon Huntc7ee0662014-11-05 16:44:37 -08002061 .attr('transform', fcfg.translate());
2062
Simon Hunte2575b62014-11-18 15:25:53 -08002063 // subgroups for links, link labels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002064 linkG = topoG.append('g').attr('id', 'links');
Simon Hunte2575b62014-11-18 15:25:53 -08002065 linkLabelG = topoG.append('g').attr('id', 'linkLabels');
Simon Huntc7ee0662014-11-05 16:44:37 -08002066 nodeG = topoG.append('g').attr('id', 'nodes');
2067
Simon Hunte2575b62014-11-18 15:25:53 -08002068 // selection of links, linkLabels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002069 link = linkG.selectAll('.link');
Simon Hunte2575b62014-11-18 15:25:53 -08002070 linkLabel = linkLabelG.selectAll('.linkLabel');
Simon Huntc7ee0662014-11-05 16:44:37 -08002071 node = nodeG.selectAll('.node');
2072
Simon Hunt7cd48f32014-11-09 23:42:50 -08002073 function chrg(d) {
2074 return fcfg.charge[d.class] || -12000;
2075 }
Simon Hunt99c13842014-11-06 18:23:12 -08002076 function ldist(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002077 return fcfg.linkDistance[d.type] || 50;
Simon Hunt99c13842014-11-06 18:23:12 -08002078 }
2079 function lstrg(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002080 // 0.0 - 1.0
2081 return fcfg.linkStrength[d.type] || 1.0;
Simon Hunt99c13842014-11-06 18:23:12 -08002082 }
2083
Simon Hunt1a9eff92014-11-07 11:06:34 -08002084 function selectCb(d, self) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002085 selectObject(d, self);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002086 }
2087
2088 function atDragEnd(d, self) {
Simon Hunt56d51852014-11-09 13:03:35 -08002089 // once we've finished moving, pin the node in position
2090 d.fixed = true;
2091 d3.select(self).classed('fixed', true);
2092 if (config.useLiveData) {
Simon Hunt902c9922014-11-11 11:59:31 -08002093 sendUpdateMeta(d);
Simon Hunta255a2c2014-11-13 22:29:35 -08002094 } else {
2095 console.log('Moving node ' + d.id + ' to [' + d.x + ',' + d.y + ']');
Simon Hunt1a9eff92014-11-07 11:06:34 -08002096 }
2097 }
2098
Simon Hunt902c9922014-11-11 11:59:31 -08002099 function sendUpdateMeta(d) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002100 sendMessage('updateMeta', {
2101 id: d.id,
2102 'class': d.class,
Simon Hunt902c9922014-11-11 11:59:31 -08002103 'memento': {
Simon Hunt01095ff2014-11-13 16:37:29 -08002104 x: d.x,
2105 y: d.y
Simon Hunt902c9922014-11-11 11:59:31 -08002106 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002107 });
2108 }
2109
Simon Huntc7ee0662014-11-05 16:44:37 -08002110 // set up the force layout
2111 network.force = d3.layout.force()
2112 .size(forceDim)
2113 .nodes(network.nodes)
2114 .links(network.links)
Simon Hunt7cd48f32014-11-09 23:42:50 -08002115 .gravity(0.4)
2116 .friction(0.7)
2117 .charge(chrg)
Simon Hunt99c13842014-11-06 18:23:12 -08002118 .linkDistance(ldist)
2119 .linkStrength(lstrg)
Simon Huntc7ee0662014-11-05 16:44:37 -08002120 .on('tick', tick);
Simon Hunt195cb382014-11-03 17:50:51 -08002121
Simon Hunt01095ff2014-11-13 16:37:29 -08002122 network.drag = d3u.createDragBehavior(network.force,
Simon Huntdeab4322014-11-13 18:49:07 -08002123 selectCb, atDragEnd, panZoom);
Simon Hunt0c6d4192014-11-12 12:07:10 -08002124
2125 // create mask layer for when we lose connection to server.
Simon Hunta5e89142014-11-14 07:00:33 -08002126 // TODO: this should be part of the framework
Simon Hunt0c6d4192014-11-12 12:07:10 -08002127 mask = view.$div.append('div').attr('id','topo-mask');
2128 para(mask, 'Oops!');
2129 para(mask, 'Web-socket connection to server closed...');
2130 para(mask, 'Try refreshing the page.');
Simon Hunt12ce12e2014-11-15 21:13:19 -08002131
2132 mask.append('svg')
2133 .attr({
2134 id: 'mask-bird',
2135 width: w,
2136 height: h
2137 })
2138 .append('g')
2139 .attr('transform', birdTranslate(w, h))
2140 .style('opacity', 0.3)
2141 .append('use')
2142 .attr({
2143 'xlink:href': '#bird',
2144 width: config.birdDim,
2145 height: config.birdDim,
2146 fill: '#111'
Thomas Vachuska89543292014-11-19 11:28:33 -08002147 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08002148 }
Simon Hunt195cb382014-11-03 17:50:51 -08002149
Simon Hunt01095ff2014-11-13 16:37:29 -08002150 function para(sel, text) {
2151 sel.append('p').text(text);
2152 }
2153
2154
Simon Hunt56d51852014-11-09 13:03:35 -08002155 function load(view, ctx, flags) {
Simon Huntf67722a2014-11-10 09:32:06 -08002156 // resize, in case the window was resized while we were not loaded
2157 resize(view, ctx, flags);
2158
Simon Hunt99c13842014-11-06 18:23:12 -08002159 // cache the view token, so network topo functions can access it
2160 network.view = view;
Simon Hunt56d51852014-11-09 13:03:35 -08002161 config.useLiveData = !flags.local;
2162
2163 if (!config.useLiveData) {
2164 prepareScenario(view, ctx, flags.debug);
2165 }
Simon Hunt99c13842014-11-06 18:23:12 -08002166
2167 // set our radio buttons and key bindings
Simon Hunt9462e8c2014-11-14 17:28:09 -08002168 layerBtnSet = view.setRadio(layerButtons);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002169 view.setKeys(keyDispatch);
Simon Hunt195cb382014-11-03 17:50:51 -08002170
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002171 // patch in our "button bar" for now
2172 // TODO: implement a more official frameworky way of doing this..
2173 addButtonBar(view);
2174
Simon Huntd3b7d512014-11-12 15:48:41 -08002175 // Load map data asynchronously; complete startup after that..
2176 loadGeoJsonData();
Simon Hunta255a2c2014-11-13 22:29:35 -08002177
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002178 //var dashIdx = 0;
2179 //antTimer = setInterval(function () {
2180 // // TODO: figure out how to choose Src-->Dst and Dst-->Src, per link
2181 // dashIdx = dashIdx === 0 ? 14 : dashIdx - 2;
2182 // d3.selectAll('.animated').style('stroke-dashoffset', dashIdx);
2183 //}, 35);
Simon Hunta255a2c2014-11-13 22:29:35 -08002184 }
2185
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002186 function startAntTimer() {
2187 var pulses = [ 5, 3, 1.2, 3 ],
2188 pulse = 0;
2189 antTimer = setInterval(function () {
2190 pulse = pulse + 1;
2191 pulse = pulse === pulses.length ? 0 : pulse;
2192 d3.selectAll('.animated').style('stroke-width', pulses[pulse]);
2193 }, 200);
2194 }
2195
2196 function stopAntTimer() {
Simon Hunta255a2c2014-11-13 22:29:35 -08002197 if (antTimer) {
2198 clearInterval(antTimer);
2199 antTimer = null;
2200 }
Simon Huntd3b7d512014-11-12 15:48:41 -08002201 }
2202
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002203 function unload(view, ctx, flags) {
2204 stopAntTimer();
2205 }
2206
Simon Huntd3b7d512014-11-12 15:48:41 -08002207 // TODO: move these to config/state portion of script
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002208 var geoJsonUrl = 'json/map/continental_us.json', // TODO: Paul
Simon Huntd3b7d512014-11-12 15:48:41 -08002209 geoJson;
2210
2211 function loadGeoJsonData() {
2212 d3.json(geoJsonUrl, function (err, data) {
2213 if (err) {
2214 // fall back to USA map background
2215 loadStaticMap();
2216 } else {
2217 geoJson = data;
2218 loadGeoMap();
2219 }
2220
2221 // finally, connect to the server...
2222 if (config.useLiveData) {
2223 webSock.connect();
2224 }
2225 });
2226 }
2227
2228 function showBg() {
2229 return config.options.showBackground ? 'visible' : 'hidden';
2230 }
2231
2232 function loadStaticMap() {
2233 fnTrace('loadStaticMap', config.backgroundUrl);
2234 var w = network.view.width(),
2235 h = network.view.height();
2236
2237 // load the background image
2238 bgImg = svg.insert('svg:image', '#topo-G')
2239 .attr({
2240 id: 'topo-bg',
2241 width: w,
2242 height: h,
2243 'xlink:href': config.backgroundUrl
2244 })
2245 .style({
2246 visibility: showBg()
2247 });
2248 }
2249
2250 function loadGeoMap() {
2251 fnTrace('loadGeoMap', geoJsonUrl);
Simon Huntd3b7d512014-11-12 15:48:41 -08002252
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002253 // extracts the topojson data into geocoordinate-based geometry
2254 var topoData = topojson.feature(geoJson, geoJson.objects.states);
Simon Huntd3b7d512014-11-12 15:48:41 -08002255
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002256 // see: http://bl.ocks.org/mbostock/4707858
2257 geoMapProjection = d3.geo.mercator();
2258 var path = d3.geo.path().projection(geoMapProjection);
Simon Huntd3b7d512014-11-12 15:48:41 -08002259
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002260 geoMapProjection
2261 .scale(1)
2262 .translate([0, 0]);
Simon Huntd3b7d512014-11-12 15:48:41 -08002263
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002264 // [[x1,y1],[x2,y2]]
2265 var b = path.bounds(topoData);
Paul Greysonfcba0e82014-11-13 10:21:16 -08002266 // size map to 95% of minimum dimension to fill space
2267 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 -08002268 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 -08002269
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002270 geoMapProjection
2271 .scale(s)
2272 .translate(t);
2273
Paul Greysonfcba0e82014-11-13 10:21:16 -08002274 bgImg = zoomPanContainer.insert("g", '#topo-G');
Thomas Vachuska89543292014-11-19 11:28:33 -08002275 bgImg.attr('id', 'map').selectAll('path')
2276 .data(topoData.features)
2277 .enter()
2278 .append('path')
2279 .attr('d', path);
Simon Hunt195cb382014-11-03 17:50:51 -08002280 }
2281
Simon Huntf67722a2014-11-10 09:32:06 -08002282 function resize(view, ctx, flags) {
Simon Hunt12ce12e2014-11-15 21:13:19 -08002283 var w = view.width(),
2284 h = view.height();
2285
Simon Hunt934c3ce2014-11-05 11:45:07 -08002286 setSize(svg, view);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002287
2288 d3.select('#mask-bird').attr({ width: w, height: h})
2289 .select('g').attr('transform', birdTranslate(w, h));
Simon Hunt142d0032014-11-04 20:13:09 -08002290 }
2291
Simon Hunt12ce12e2014-11-15 21:13:19 -08002292 function birdTranslate(w, h) {
2293 var bdim = config.birdDim;
2294 return 'translate('+((w-bdim)*.4)+','+((h-bdim)*.1)+')';
2295 }
Simon Hunt142d0032014-11-04 20:13:09 -08002296
2297 // ==============================
2298 // View registration
Simon Hunt195cb382014-11-03 17:50:51 -08002299
Simon Hunt25248912014-11-04 11:25:48 -08002300 onos.ui.addView('topo', {
Simon Hunt142d0032014-11-04 20:13:09 -08002301 preload: preload,
2302 load: load,
Simon Hunta255a2c2014-11-13 22:29:35 -08002303 unload: unload,
Simon Hunt142d0032014-11-04 20:13:09 -08002304 resize: resize
Simon Hunt195cb382014-11-03 17:50:51 -08002305 });
2306
Simon Hunt61d04042014-11-11 17:27:16 -08002307 detailPane = onos.ui.addFloatingPanel('topo-detail');
Simon Hunta5e89142014-11-14 07:00:33 -08002308 oiBox = onos.ui.addFloatingPanel('topo-oibox', 'TL');
Simon Hunt61d04042014-11-11 17:27:16 -08002309
Simon Hunt195cb382014-11-03 17:50:51 -08002310}(ONOS));