blob: c273f811e20a6ea846286a3e7f803c7f474fda49 [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 Hunt95908012014-11-20 10:20:26 -080077 linkOutWidth: 14
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,
Simon Huntc72967b2014-11-20 09:21:42 -080083 yoff: -14,
84
85 device: {
86 dim: 30,
87 rx: 4
88 }
Thomas Vachuska89543292014-11-19 11:28:33 -080089 },
90 iconUrl: {
91 device: 'img/device.png',
92 host: 'img/host.png',
93 pkt: 'img/pkt.png',
94 opt: 'img/opt.png'
Simon Hunt195cb382014-11-03 17:50:51 -080095 },
Simon Hunt195cb382014-11-03 17:50:51 -080096 force: {
Simon Hunt7cd48f32014-11-09 23:42:50 -080097 note_for_links: 'link.type is used to differentiate',
Simon Huntc7ee0662014-11-05 16:44:37 -080098 linkDistance: {
Simon Hunt7cd48f32014-11-09 23:42:50 -080099 direct: 100,
100 optical: 120,
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800101 hostLink: 3
Simon Huntc7ee0662014-11-05 16:44:37 -0800102 },
103 linkStrength: {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800104 direct: 1.0,
105 optical: 1.0,
106 hostLink: 1.0
Simon Huntc7ee0662014-11-05 16:44:37 -0800107 },
Simon Hunt7cd48f32014-11-09 23:42:50 -0800108 note_for_nodes: 'node.class is used to differentiate',
Simon Huntc7ee0662014-11-05 16:44:37 -0800109 charge: {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800110 device: -8000,
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800111 host: -5000
Simon Huntc7ee0662014-11-05 16:44:37 -0800112 },
113 pad: 20,
Simon Hunt195cb382014-11-03 17:50:51 -0800114 translate: function() {
115 return 'translate(' +
Simon Huntc7ee0662014-11-05 16:44:37 -0800116 config.force.pad + ',' +
117 config.force.pad + ')';
Simon Hunt195cb382014-11-03 17:50:51 -0800118 }
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800119 },
120 // see below in creation of viewBox on main svg
121 logicalSize: 1000
Simon Hunt195cb382014-11-03 17:50:51 -0800122 };
123
Simon Hunt142d0032014-11-04 20:13:09 -0800124 // radio buttons
Simon Hunt9462e8c2014-11-14 17:28:09 -0800125 var layerButtons = [
126 { text: 'All Layers', id: 'all', cb: showAllLayers },
127 { text: 'Packet Only', id: 'pkt', cb: showPacketLayer },
128 { text: 'Optical Only', id: 'opt', cb: showOpticalLayer }
129 ],
130 layerBtnSet,
131 layerBtnDispatch = {
132 all: showAllLayers,
133 pkt: showPacketLayer,
134 opt: showOpticalLayer
135 };
Simon Hunt934c3ce2014-11-05 11:45:07 -0800136
137 // key bindings
138 var keyDispatch = {
Simon Hunta255a2c2014-11-13 22:29:35 -0800139 M: testMe, // TODO: remove (testing only)
140 S: injectStartupEvents, // TODO: remove (testing only)
141 space: injectTestEvent, // TODO: remove (testing only)
Simon Hunt99c13842014-11-06 18:23:12 -0800142
Simon Hunt01095ff2014-11-13 16:37:29 -0800143 B: toggleBg,
Simon Hunt934c3ce2014-11-05 11:45:07 -0800144 L: cycleLabels,
145 P: togglePorts,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800146 U: unpin,
Paul Greysonfcba0e82014-11-13 10:21:16 -0800147 R: resetZoomPan,
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -0800148 H: toggleHover,
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800149 V: showTrafficAction,
150 A: showAllTrafficAction,
Thomas Vachuska29617e52014-11-20 03:17:46 -0800151 F: showDeviceLinkFlowsAction,
Simon Hunt9462e8c2014-11-14 17:28:09 -0800152 esc: handleEscape
Simon Hunt934c3ce2014-11-05 11:45:07 -0800153 };
Simon Hunt142d0032014-11-04 20:13:09 -0800154
Simon Hunt195cb382014-11-03 17:50:51 -0800155 // state variables
Simon Hunt99c13842014-11-06 18:23:12 -0800156 var network = {
Simon Hunt50128c02014-11-08 13:36:15 -0800157 view: null, // view token reference
Simon Hunt99c13842014-11-06 18:23:12 -0800158 nodes: [],
159 links: [],
Simon Hunt269670f2014-11-17 16:17:43 -0800160 lookup: {},
161 revLinkToKey: {}
Simon Hunt99c13842014-11-06 18:23:12 -0800162 },
Simon Hunt56d51852014-11-09 13:03:35 -0800163 scenario = {
164 evDir: 'json/ev/',
165 evScenario: '/scenario.json',
166 evPrefix: '/ev_',
167 evOnos: '_onos.json',
168 evUi: '_ui.json',
169 ctx: null,
170 params: {},
171 evNumber: 0,
172 view: null,
173 debug: false
174 },
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800175 webSock,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800176 sid = 0,
Simon Hunt56d51852014-11-09 13:03:35 -0800177 deviceLabelIndex = 0,
178 hostLabelIndex = 0,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800179 selections = {},
Simon Hunta5e89142014-11-14 07:00:33 -0800180 selectOrder = [],
Simon Hunt6ac93f32014-11-13 12:17:27 -0800181 hovered = null,
Simon Hunta5e89142014-11-14 07:00:33 -0800182 detailPane,
Simon Hunta255a2c2014-11-13 22:29:35 -0800183 antTimer = null,
Simon Hunta5e89142014-11-14 07:00:33 -0800184 onosInstances = {},
185 onosOrder = [],
186 oiBox,
Simon Hunt9462e8c2014-11-14 17:28:09 -0800187 oiShowMaster = false,
Thomas Vachuska29617e52014-11-20 03:17:46 -0800188 hoverModes = [ 'none', 'intents', 'flows'],
189 hoverMode = 0,
Simon Hunt195cb382014-11-03 17:50:51 -0800190 portLabelsOn = false;
191
Simon Hunt934c3ce2014-11-05 11:45:07 -0800192 // D3 selections
193 var svg,
Paul Greysonfcba0e82014-11-13 10:21:16 -0800194 zoomPanContainer,
Simon Hunt934c3ce2014-11-05 11:45:07 -0800195 bgImg,
Simon Huntc7ee0662014-11-05 16:44:37 -0800196 topoG,
197 nodeG,
198 linkG,
Simon Hunte2575b62014-11-18 15:25:53 -0800199 linkLabelG,
Simon Huntc7ee0662014-11-05 16:44:37 -0800200 node,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800201 link,
Simon Hunte2575b62014-11-18 15:25:53 -0800202 linkLabel,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800203 mask;
Simon Hunt195cb382014-11-03 17:50:51 -0800204
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800205 // the projection for the map background
206 var geoMapProjection;
207
Paul Greysonfcba0e82014-11-13 10:21:16 -0800208 // the zoom function
209 var zoom;
210
Simon Hunt142d0032014-11-04 20:13:09 -0800211 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800212 // For Debugging / Development
Simon Hunt195cb382014-11-03 17:50:51 -0800213
Simon Hunt99c13842014-11-06 18:23:12 -0800214 function note(label, msg) {
215 console.log('NOTE: ' + label + ': ' + msg);
Simon Hunt195cb382014-11-03 17:50:51 -0800216 }
217
Simon Hunt99c13842014-11-06 18:23:12 -0800218 function debug(what) {
219 return config.debugOn && config.debug[what];
Simon Hunt934c3ce2014-11-05 11:45:07 -0800220 }
221
Simon Huntfc274c92014-11-11 11:05:46 -0800222 function fnTrace(msg, id) {
223 if (config.fnTrace) {
224 console.log('FN: ' + msg + ' [' + id + ']');
225 }
226 }
Simon Hunt99c13842014-11-06 18:23:12 -0800227
Simon Hunta5e89142014-11-14 07:00:33 -0800228 function evTrace(data) {
229 fnTrace(data.event, data.payload.id);
230 }
231
Simon Hunt934c3ce2014-11-05 11:45:07 -0800232 // ==============================
233 // Key Callbacks
234
Simon Hunt99c13842014-11-06 18:23:12 -0800235 function testMe(view) {
Simon Hunt625dc402014-11-18 10:57:18 -0800236 view.alert('Theme is ' + view.theme());
Simon Hunt99c13842014-11-06 18:23:12 -0800237 }
238
Simon Hunt56d51852014-11-09 13:03:35 -0800239 function abortIfLive() {
Simon Hunt50128c02014-11-08 13:36:15 -0800240 if (config.useLiveData) {
Simon Huntb53e0682014-11-12 13:32:01 -0800241 network.view.alert("Sorry, currently using live data..");
Simon Hunt56d51852014-11-09 13:03:35 -0800242 return true;
Simon Hunt50128c02014-11-08 13:36:15 -0800243 }
Simon Hunt56d51852014-11-09 13:03:35 -0800244 return false;
245 }
Simon Hunt50128c02014-11-08 13:36:15 -0800246
Simon Hunt56d51852014-11-09 13:03:35 -0800247 function testDebug(msg) {
248 if (scenario.debug) {
249 scenario.view.alert(msg);
250 }
251 }
Simon Hunt99c13842014-11-06 18:23:12 -0800252
Simon Hunt56d51852014-11-09 13:03:35 -0800253 function injectTestEvent(view) {
254 if (abortIfLive()) { return; }
255 var sc = scenario,
256 evn = ++sc.evNumber,
257 pfx = sc.evDir + sc.ctx + sc.evPrefix + evn,
258 onosUrl = pfx + sc.evOnos,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800259 uiUrl = pfx + sc.evUi,
260 stack = [
261 { url: onosUrl, cb: handleServerEvent },
262 { url: uiUrl, cb: handleUiEvent }
263 ];
264 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800265 }
266
Simon Hunt7cd48f32014-11-09 23:42:50 -0800267 function recurseFetchEvent(stack, evn) {
268 var v = scenario.view,
269 frame;
270 if (stack.length === 0) {
Simon Huntfc274c92014-11-11 11:05:46 -0800271 v.alert('Oops!\n\nNo event #' + evn + ' found.');
Simon Hunt7cd48f32014-11-09 23:42:50 -0800272 return;
273 }
274 frame = stack.shift();
275
276 d3.json(frame.url, function (err, data) {
Simon Hunt99c13842014-11-06 18:23:12 -0800277 if (err) {
Simon Hunt56d51852014-11-09 13:03:35 -0800278 if (err.status === 404) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800279 // if we didn't find the data, try the next stack frame
280 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800281 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800282 v.alert('non-404 error:\n\n' + frame.url + '\n\n' + err);
Simon Hunt56d51852014-11-09 13:03:35 -0800283 }
Simon Hunt99c13842014-11-06 18:23:12 -0800284 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800285 testDebug('loaded: ' + frame.url);
Simon Hunt1712ed82014-11-17 12:56:00 -0800286 wsTrace('test', JSON.stringify(data));
Simon Hunt7cd48f32014-11-09 23:42:50 -0800287 frame.cb(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800288 }
289 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800290
Simon Hunt56d51852014-11-09 13:03:35 -0800291 }
Simon Hunt50128c02014-11-08 13:36:15 -0800292
Simon Hunt56d51852014-11-09 13:03:35 -0800293 function handleUiEvent(data) {
Simon Huntbb282f52014-11-10 11:08:19 -0800294 scenario.view.alert('UI Tx: ' + data.event + '\n\n' +
295 JSON.stringify(data));
Simon Hunt56d51852014-11-09 13:03:35 -0800296 }
297
298 function injectStartupEvents(view) {
299 var last = scenario.params.lastAuto || 0;
300 if (abortIfLive()) { return; }
301
302 while (scenario.evNumber < last) {
Simon Hunt1a9eff92014-11-07 11:06:34 -0800303 injectTestEvent(view);
304 }
305 }
306
Simon Hunt934c3ce2014-11-05 11:45:07 -0800307 function toggleBg() {
308 var vis = bgImg.style('visibility');
309 bgImg.style('visibility', (vis === 'hidden') ? 'visible' : 'hidden');
310 }
311
Simon Hunt99c13842014-11-06 18:23:12 -0800312 function cycleLabels() {
Simon Huntbb282f52014-11-10 11:08:19 -0800313 deviceLabelIndex = (deviceLabelIndex === network.deviceLabelCount - 1)
314 ? 0 : deviceLabelIndex + 1;
Simon Hunt5f36d342014-11-08 21:33:14 -0800315
Simon Hunt99c13842014-11-06 18:23:12 -0800316 network.nodes.forEach(function (d) {
Simon Huntbb282f52014-11-10 11:08:19 -0800317 if (d.class === 'device') {
318 updateDeviceLabel(d);
319 }
Simon Hunt99c13842014-11-06 18:23:12 -0800320 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800321 }
322
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -0800323 function toggleHover(view) {
Thomas Vachuska29617e52014-11-20 03:17:46 -0800324 hoverMode++;
325 if (hoverMode === hoverModes.length) {
326 hoverMode = 0;
327 }
328 console.log('Hover Mode:' + hoverMode + ': ' + hoverModes[hoverMode]);
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -0800329 }
330
Simon Hunt934c3ce2014-11-05 11:45:07 -0800331 function togglePorts(view) {
Simon Hunt50128c02014-11-08 13:36:15 -0800332 view.alert('togglePorts() callback')
Simon Hunt934c3ce2014-11-05 11:45:07 -0800333 }
334
Simon Hunt6ac93f32014-11-13 12:17:27 -0800335 function unpin() {
336 if (hovered) {
337 hovered.fixed = false;
338 hovered.el.classed('fixed', false);
339 network.force.resume();
340 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800341 }
342
Simon Hunt9462e8c2014-11-14 17:28:09 -0800343 function handleEscape(view) {
344 if (oiShowMaster) {
345 cancelAffinity();
346 } else {
347 deselectAll();
348 }
349 }
350
Simon Hunt934c3ce2014-11-05 11:45:07 -0800351 // ==============================
352 // Radio Button Callbacks
353
Simon Hunta5e89142014-11-14 07:00:33 -0800354 var layerLookup = {
355 host: {
356 endstation: 'pkt', // default, if host event does not define type
Thomas Vachuska89543292014-11-19 11:28:33 -0800357 router: 'pkt',
Simon Hunta5e89142014-11-14 07:00:33 -0800358 bgpSpeaker: 'pkt'
359 },
360 device: {
361 switch: 'pkt',
362 roadm: 'opt'
363 },
364 link: {
365 hostLink: 'pkt',
366 direct: 'pkt',
Simon Hunt8257f4c2014-11-16 19:34:54 -0800367 indirect: '',
368 tunnel: '',
Simon Hunta5e89142014-11-14 07:00:33 -0800369 optical: 'opt'
370 }
371 };
372
373 function inLayer(d, layer) {
Simon Hunt8257f4c2014-11-16 19:34:54 -0800374 var type = d.class === 'link' ? d.type() : d.type,
375 look = layerLookup[d.class],
376 lyr = look && look[type];
Simon Hunta5e89142014-11-14 07:00:33 -0800377 return lyr === layer;
378 }
379
380 function unsuppressLayer(which) {
381 node.each(function (d) {
382 var node = d.el;
383 if (inLayer(d, which)) {
384 node.classed('suppressed', false);
385 }
386 });
387
388 link.each(function (d) {
389 var link = d.el;
390 if (inLayer(d, which)) {
391 link.classed('suppressed', false);
392 }
393 });
394 }
395
Simon Hunt9462e8c2014-11-14 17:28:09 -0800396 function suppressLayers(b) {
397 node.classed('suppressed', b);
398 link.classed('suppressed', b);
Simon Hunt142d0032014-11-04 20:13:09 -0800399// d3.selectAll('svg .port').classed('inactive', false);
400// d3.selectAll('svg .portText').classed('inactive', false);
Simon Hunt195cb382014-11-03 17:50:51 -0800401 }
402
Simon Hunt9462e8c2014-11-14 17:28:09 -0800403 function showAllLayers() {
404 suppressLayers(false);
405 }
406
Simon Hunt195cb382014-11-03 17:50:51 -0800407 function showPacketLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800408 node.classed('suppressed', true);
409 link.classed('suppressed', true);
410 unsuppressLayer('pkt');
Simon Hunt195cb382014-11-03 17:50:51 -0800411 }
412
413 function showOpticalLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800414 node.classed('suppressed', true);
415 link.classed('suppressed', true);
416 unsuppressLayer('opt');
Simon Hunt195cb382014-11-03 17:50:51 -0800417 }
418
Simon Hunt9462e8c2014-11-14 17:28:09 -0800419 function restoreLayerState() {
420 layerBtnDispatch[layerBtnSet.selected()]();
421 }
422
Simon Hunt142d0032014-11-04 20:13:09 -0800423 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800424 // Private functions
425
Simon Hunt99c13842014-11-06 18:23:12 -0800426 function safeId(s) {
427 return s.replace(/[^a-z0-9]/gi, '-');
428 }
429
Simon Huntc7ee0662014-11-05 16:44:37 -0800430 // set the size of the given element to that of the view (reduced if padded)
431 function setSize(el, view, pad) {
432 var padding = pad ? pad * 2 : 0;
Simon Hunt934c3ce2014-11-05 11:45:07 -0800433 el.attr({
Simon Huntc7ee0662014-11-05 16:44:37 -0800434 width: view.width() - padding,
435 height: view.height() - padding
Simon Hunt934c3ce2014-11-05 11:45:07 -0800436 });
437 }
438
Simon Hunt8257f4c2014-11-16 19:34:54 -0800439 function makeNodeKey(d, what) {
440 var port = what + 'Port';
441 return d[what] + '/' + d[port];
442 }
443
444 function makeLinkKey(d, flipped) {
445 var one = flipped ? makeNodeKey(d, 'dst') : makeNodeKey(d, 'src'),
446 two = flipped ? makeNodeKey(d, 'src') : makeNodeKey(d, 'dst');
447 return one + '-' + two;
448 }
449
Simon Hunt269670f2014-11-17 16:17:43 -0800450 function findLinkById(id) {
451 // check to see if this is a reverse lookup, else default to given id
452 var key = network.revLinkToKey[id] || id;
453 return key && network.lookup[key];
454 }
455
Simon Hunt8257f4c2014-11-16 19:34:54 -0800456 function findLink(linkData, op) {
457 var key = makeLinkKey(linkData),
458 keyrev = makeLinkKey(linkData, 1),
459 link = network.lookup[key],
460 linkRev = network.lookup[keyrev],
461 result = {},
462 ldata = link || linkRev,
463 rawLink;
464
465 if (op === 'add') {
466 if (link) {
467 // trying to add a link that we already know about
468 result.ldata = link;
469 result.badLogic = 'addLink: link already added';
470
471 } else if (linkRev) {
472 // we found the reverse of the link to be added
473 result.ldata = linkRev;
474 if (linkRev.fromTarget) {
475 result.badLogic = 'addLink: link already added';
476 }
477 }
478 } else if (op === 'update') {
479 if (!ldata) {
480 result.badLogic = 'updateLink: link not found';
481 } else {
482 rawLink = link ? ldata.fromSource : ldata.fromTarget;
483 result.updateWith = function (data) {
484 $.extend(rawLink, data);
485 restyleLinkElement(ldata);
486 }
487 }
488 } else if (op === 'remove') {
489 if (!ldata) {
490 result.badLogic = 'removeLink: link not found';
491 } else {
492 rawLink = link ? ldata.fromSource : ldata.fromTarget;
493
494 if (!rawLink) {
495 result.badLogic = 'removeLink: link not found';
496
497 } else {
498 result.removeRawLink = function () {
499 if (link) {
500 // remove fromSource
501 ldata.fromSource = null;
502 if (ldata.fromTarget) {
503 // promote target into source position
504 ldata.fromSource = ldata.fromTarget;
505 ldata.fromTarget = null;
506 ldata.key = keyrev;
507 delete network.lookup[key];
508 network.lookup[keyrev] = ldata;
Simon Hunt269670f2014-11-17 16:17:43 -0800509 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800510 }
511 } else {
512 // remove fromTarget
513 ldata.fromTarget = null;
Simon Hunt269670f2014-11-17 16:17:43 -0800514 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800515 }
516 if (ldata.fromSource) {
517 restyleLinkElement(ldata);
518 } else {
519 removeLinkElement(ldata);
520 }
521 }
522 }
523 }
524 }
525 return result;
526 }
527
528 function addLinkUpdate(ldata, link) {
529 // add link event, but we already have the reverse link installed
530 ldata.fromTarget = link;
Simon Hunt269670f2014-11-17 16:17:43 -0800531 network.revLinkToKey[link.id] = ldata.key;
Simon Hunt8257f4c2014-11-16 19:34:54 -0800532 restyleLinkElement(ldata);
533 }
534
535 var allLinkTypes = 'direct indirect optical tunnel',
536 defaultLinkType = 'direct';
537
538 function restyleLinkElement(ldata) {
539 // this fn's job is to look at raw links and decide what svg classes
540 // need to be applied to the line element in the DOM
541 var el = ldata.el,
542 type = ldata.type(),
543 lw = ldata.linkWidth(),
544 online = ldata.online();
545
546 el.classed('link', true);
547 el.classed('inactive', !online);
548 el.classed(allLinkTypes, false);
549 if (type) {
550 el.classed(type, true);
551 }
552 el.transition()
553 .duration(1000)
Thomas Vachuska89543292014-11-19 11:28:33 -0800554 .attr('stroke-width', linkScale(lw))
555 .attr('stroke', config.topo.linkBaseColor);
Simon Hunt8257f4c2014-11-16 19:34:54 -0800556 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800557
Simon Hunt99c13842014-11-06 18:23:12 -0800558 // ==============================
559 // Event handlers for server-pushed events
560
Simon Huntbb282f52014-11-10 11:08:19 -0800561 function logicError(msg) {
562 // TODO, report logic error to server, via websock, so it can be logged
Simon Huntcb56cff2014-11-17 11:42:26 -0800563 //network.view.alert('Logic Error:\n\n' + msg);
Simon Huntfc274c92014-11-11 11:05:46 -0800564 console.warn(msg);
Simon Huntbb282f52014-11-10 11:08:19 -0800565 }
566
Simon Hunt99c13842014-11-06 18:23:12 -0800567 var eventDispatch = {
Simon Hunta5e89142014-11-14 07:00:33 -0800568 addInstance: addInstance,
Simon Hunt99c13842014-11-06 18:23:12 -0800569 addDevice: addDevice,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800570 addLink: addLink,
Simon Hunt56d51852014-11-09 13:03:35 -0800571 addHost: addHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800572
Simon Huntfcfb46c2014-11-19 12:53:38 -0800573 updateInstance: updateInstance,
Simon Huntbb282f52014-11-10 11:08:19 -0800574 updateDevice: updateDevice,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800575 updateLink: updateLink,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800576 updateHost: updateHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800577
Simon Huntd72bc702014-11-13 18:38:04 -0800578 removeInstance: stillToImplement,
Simon Huntbb282f52014-11-10 11:08:19 -0800579 removeDevice: stillToImplement,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800580 removeLink: removeLink,
Simon Hunt44031102014-11-11 13:20:36 -0800581 removeHost: removeHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800582
Simon Hunt61d04042014-11-11 17:27:16 -0800583 showDetails: showDetails,
Simon Huntb53e0682014-11-12 13:32:01 -0800584 showTraffic: showTraffic
Simon Hunt99c13842014-11-06 18:23:12 -0800585 };
586
Simon Hunta5e89142014-11-14 07:00:33 -0800587 function addInstance(data) {
588 evTrace(data);
589 var inst = data.payload,
590 id = inst.id;
591 if (onosInstances[id]) {
592 logicError('ONOS instance already added: ' + id);
593 return;
594 }
595 onosInstances[id] = inst;
596 onosOrder.push(inst);
597 updateInstances();
598 }
599
Simon Hunt99c13842014-11-06 18:23:12 -0800600 function addDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800601 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800602 var device = data.payload,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800603 nodeData = createDeviceNode(device);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800604 network.nodes.push(nodeData);
605 network.lookup[nodeData.id] = nodeData;
Simon Hunt99c13842014-11-06 18:23:12 -0800606 updateNodes();
607 network.force.start();
608 }
609
Simon Hunt99c13842014-11-06 18:23:12 -0800610 function addLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800611 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800612 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800613 result = findLink(link, 'add'),
614 bad = result.badLogic,
615 ldata = result.ldata;
616
617 if (bad) {
618 logicError(bad + ': ' + link.id);
619 return;
620 }
621
622 if (ldata) {
623 // we already have a backing store link for src/dst nodes
624 addLinkUpdate(ldata, link);
625 return;
626 }
627
628 // no backing store link yet
629 ldata = createLink(link);
630 if (ldata) {
631 network.links.push(ldata);
632 network.lookup[ldata.key] = ldata;
Simon Hunt99c13842014-11-06 18:23:12 -0800633 updateLinks();
634 network.force.start();
635 }
636 }
637
Simon Hunt56d51852014-11-09 13:03:35 -0800638 function addHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800639 evTrace(data);
Simon Hunt56d51852014-11-09 13:03:35 -0800640 var host = data.payload,
641 node = createHostNode(host),
642 lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800643 network.nodes.push(node);
644 network.lookup[host.id] = node;
645 updateNodes();
646
647 lnk = createHostLink(host);
648 if (lnk) {
Simon Hunt44031102014-11-11 13:20:36 -0800649 node.linkData = lnk; // cache ref on its host
Simon Hunt56d51852014-11-09 13:03:35 -0800650 network.links.push(lnk);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800651 network.lookup[host.ingress] = lnk;
652 network.lookup[host.egress] = lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800653 updateLinks();
654 }
655 network.force.start();
656 }
657
Simon Hunt44031102014-11-11 13:20:36 -0800658 // TODO: fold updateX(...) methods into one base method; remove duplication
Simon Hunt56a2ea42014-11-19 12:39:31 -0800659
660 function updateInstance(data) {
661 evTrace(data);
662 var inst = data.payload,
663 id = inst.id,
664 instData = onosInstances[id];
665 if (instData) {
666 $.extend(instData, inst);
667 updateInstances();
Simon Hunt56a2ea42014-11-19 12:39:31 -0800668 } else {
669 logicError('updateInstance lookup fail. ID = "' + id + '"');
670 }
671 }
672
Simon Huntbb282f52014-11-10 11:08:19 -0800673 function updateDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800674 evTrace(data);
Simon Huntbb282f52014-11-10 11:08:19 -0800675 var device = data.payload,
676 id = device.id,
677 nodeData = network.lookup[id];
678 if (nodeData) {
679 $.extend(nodeData, device);
680 updateDeviceState(nodeData);
681 } else {
682 logicError('updateDevice lookup fail. ID = "' + id + '"');
683 }
684 }
685
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800686 function updateLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800687 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800688 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800689 result = findLink(link, 'update'),
690 bad = result.badLogic;
691 if (bad) {
692 logicError(bad + ': ' + link.id);
693 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800694 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800695 result.updateWith(link);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800696 }
697
Simon Hunt7cd48f32014-11-09 23:42:50 -0800698 function updateHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800699 evTrace(data);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800700 var host = data.payload,
Simon Huntbb282f52014-11-10 11:08:19 -0800701 id = host.id,
702 hostData = network.lookup[id];
703 if (hostData) {
704 $.extend(hostData, host);
705 updateHostState(hostData);
706 } else {
707 logicError('updateHost lookup fail. ID = "' + id + '"');
708 }
Simon Hunt7cd48f32014-11-09 23:42:50 -0800709 }
710
Simon Hunt44031102014-11-11 13:20:36 -0800711 // TODO: fold removeX(...) methods into base method - remove dup code
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800712 function removeLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800713 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800714 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800715 result = findLink(link, 'remove'),
716 bad = result.badLogic;
717 if (bad) {
718 logicError(bad + ': ' + link.id);
719 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800720 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800721 result.removeRawLink();
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800722 }
723
Simon Hunt44031102014-11-11 13:20:36 -0800724 function removeHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800725 evTrace(data);
Simon Hunt44031102014-11-11 13:20:36 -0800726 var host = data.payload,
727 id = host.id,
728 hostData = network.lookup[id];
729 if (hostData) {
730 removeHostElement(hostData);
731 } else {
732 logicError('removeHost lookup fail. ID = "' + id + '"');
733 }
734 }
735
Simon Hunt61d04042014-11-11 17:27:16 -0800736 function showDetails(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800737 evTrace(data);
Simon Hunt61d04042014-11-11 17:27:16 -0800738 populateDetails(data.payload);
739 detailPane.show();
740 }
741
Simon Huntb53e0682014-11-12 13:32:01 -0800742 function showTraffic(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800743 evTrace(data);
Thomas Vachuska4731f122014-11-20 04:56:19 -0800744 var paths = data.payload.paths,
745 hasTraffic = false;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800746
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800747 // Revert any links hilighted previously.
Thomas Vachuska4731f122014-11-20 04:56:19 -0800748 link.style('stroke-width', null)
Thomas Vachuskaa3148a72014-11-19 21:38:35 -0800749 .classed('primary secondary animated optical', false);
Simon Hunte2575b62014-11-18 15:25:53 -0800750 // Remove all previous labels.
751 removeLinkLabels();
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800752
Simon Hunte2575b62014-11-18 15:25:53 -0800753 // Now hilight all links in the paths payload, and attach
754 // labels to them, if they are defined.
Simon Hunta255a2c2014-11-13 22:29:35 -0800755 paths.forEach(function (p) {
Simon Hunte2575b62014-11-18 15:25:53 -0800756 var n = p.links.length,
757 i,
758 ldata;
759
Thomas Vachuska4731f122014-11-20 04:56:19 -0800760 hasTraffic = hasTraffic || p.traffic;
Simon Hunte2575b62014-11-18 15:25:53 -0800761 for (i=0; i<n; i++) {
762 ldata = findLinkById(p.links[i]);
Thomas Vachuska4731f122014-11-20 04:56:19 -0800763 if (ldata && ldata.el) {
Simon Hunte2575b62014-11-18 15:25:53 -0800764 ldata.el.classed(p.class, true);
765 ldata.label = p.labels[i];
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800766 }
Simon Hunte2575b62014-11-18 15:25:53 -0800767 }
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800768 });
Thomas Vachuska4731f122014-11-20 04:56:19 -0800769
Simon Hunte2575b62014-11-18 15:25:53 -0800770 updateLinks();
Thomas Vachuska4731f122014-11-20 04:56:19 -0800771
772 if (hasTraffic && !antTimer) {
773 startAntTimer();
774 } else if (!hasTraffic && antTimer) {
775 stopAntTimer();
776 }
Simon Huntb53e0682014-11-12 13:32:01 -0800777 }
778
Simon Hunt56d51852014-11-09 13:03:35 -0800779 // ...............................
780
781 function stillToImplement(data) {
782 var p = data.payload;
783 note(data.event, p.id);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800784 network.view.alert('Not yet implemented: "' + data.event + '"');
Simon Hunt56d51852014-11-09 13:03:35 -0800785 }
Simon Hunt99c13842014-11-06 18:23:12 -0800786
787 function unknownEvent(data) {
Simon Hunt50128c02014-11-08 13:36:15 -0800788 network.view.alert('Unknown event type: "' + data.event + '"');
Simon Hunt99c13842014-11-06 18:23:12 -0800789 }
790
791 function handleServerEvent(data) {
792 var fn = eventDispatch[data.event] || unknownEvent;
793 fn(data);
794 }
795
796 // ==============================
Simon Hunt61d04042014-11-11 17:27:16 -0800797 // Out-going messages...
798
Simon Huntb53e0682014-11-12 13:32:01 -0800799 function userFeedback(msg) {
800 // for now, use the alert pane as is. Maybe different alert style in
801 // the future (centered on view; dismiss button?)
802 network.view.alert(msg);
803 }
804
805 function nSel() {
806 return selectOrder.length;
807 }
Simon Hunt61d04042014-11-11 17:27:16 -0800808 function getSel(idx) {
809 return selections[selectOrder[idx]];
810 }
Simon Huntb53e0682014-11-12 13:32:01 -0800811 function getSelId(idx) {
812 return getSel(idx).obj.id;
813 }
814 function allSelectionsClass(cls) {
815 for (var i=0, n=nSel(); i<n; i++) {
816 if (getSel(i).obj.class !== cls) {
817 return false;
818 }
819 }
820 return true;
821 }
Simon Hunt61d04042014-11-11 17:27:16 -0800822
Simon Hunt61d04042014-11-11 17:27:16 -0800823 // request details for the selected element
Simon Huntd72bc702014-11-13 18:38:04 -0800824 // invoked from selection of a single node.
Simon Hunt61d04042014-11-11 17:27:16 -0800825 function requestDetails() {
826 var data = getSel(0).obj,
827 payload = {
828 id: data.id,
829 class: data.class
830 };
831 sendMessage('requestDetails', payload);
832 }
833
Simon Huntd72bc702014-11-13 18:38:04 -0800834 function addIntentAction() {
835 sendMessage('addHostIntent', {
836 one: getSelId(0),
Thomas Vachuska82f2c622014-11-17 12:23:18 -0800837 two: getSelId(1),
838 ids: [ getSelId(0), getSelId(1) ]
Simon Huntd72bc702014-11-13 18:38:04 -0800839 });
840 }
841
842 function showTrafficAction() {
Thomas Vachuska29617e52014-11-20 03:17:46 -0800843 // force intents hover mode
844 hoverMode = 1;
845 showSelectTraffic();
846 }
847
848 function showSelectTraffic() {
Simon Huntd72bc702014-11-13 18:38:04 -0800849 // if nothing is hovered over, and nothing selected, send cancel request
850 if (!hovered && nSel() === 0) {
851 sendMessage('cancelTraffic', {});
852 return;
853 }
854
855 // NOTE: hover is only populated if "show traffic on hover" is
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800856 // toggled on, and the item hovered is a host or a device...
857 var hoverId = (trafficHover() && hovered &&
858 (hovered.class === 'host' || hovered.class === 'device'))
Simon Huntd72bc702014-11-13 18:38:04 -0800859 ? hovered.id : '';
860 sendMessage('requestTraffic', {
861 ids: selectOrder,
862 hover: hoverId
863 });
864 }
865
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800866 function showAllTrafficAction() {
867 sendMessage('requestAllTraffic', {});
868 }
869
Thomas Vachuska29617e52014-11-20 03:17:46 -0800870 function showDeviceLinkFlowsAction() {
871 // force intents hover mode
872 hoverMode = 2;
873 showDeviceLinkFlows();
874 }
875
876 function showDeviceLinkFlows() {
877 // if nothing is hovered over, and nothing selected, send cancel request
878 if (!hovered && nSel() === 0) {
879 sendMessage('cancelTraffic', {});
880 return;
881 }
882 var hoverId = (flowsHover() && hovered && hovered.class === 'device') ?
883 hovered.id : '';
884 sendMessage('requestDeviceLinkFlows', {
885 ids: selectOrder,
886 hover: hoverId
887 });
888 }
Simon Huntd72bc702014-11-13 18:38:04 -0800889
Simon Hunt95908012014-11-20 10:20:26 -0800890 function stripPx(s) {
891 return s.replace(/px$/,'');
892 }
Simon Hunt61d04042014-11-11 17:27:16 -0800893 // ==============================
Simon Hunta5e89142014-11-14 07:00:33 -0800894 // onos instance panel functions
895
896 function updateInstances() {
897 var onoses = oiBox.el.selectAll('.onosInst')
898 .data(onosOrder, function (d) { return d.id; });
899
900 // operate on existing onoses if necessary
Simon Huntfcfb46c2014-11-19 12:53:38 -0800901 onoses.classed('online', function (d) { return d.online; });
Simon Hunta5e89142014-11-14 07:00:33 -0800902
903 var entering = onoses.enter()
904 .append('div')
905 .attr('class', 'onosInst')
906 .classed('online', function (d) { return d.online; })
Simon Hunt9c15eca2014-11-15 18:37:59 -0800907 .on('click', clickInst);
908
909 entering.each(function (d, i) {
910 var el = d3.select(this),
911 img;
Simon Hunt95908012014-11-20 10:20:26 -0800912 var css = window.getComputedStyle(this),
913 w = stripPx(css.width),
914 h = stripPx(css.height) / 2;
Simon Hunt9c15eca2014-11-15 18:37:59 -0800915
Simon Hunt95908012014-11-20 10:20:26 -0800916 var svg = el.append('svg').attr({
917 width: w,
918 height: h
919 });
920 var dim = 30;
921 svg.append('use').attr({
922 class: 'glyphIcon',
923 transform: translate(2,2),
924 'xlink:href': '#node',
925 width: dim,
926 height: dim
927
928 });
929
930 //$('<img src="img/node.png">').appendTo(el);
931 //img = el.select('img')
932 // .attr({
933 // width: 30
934 // });
Simon Hunt9c15eca2014-11-15 18:37:59 -0800935
936 $('<div>').attr('class', 'onosTitle').text(d.id).appendTo(el);
937
938 // is the UI attached to this instance?
939 // TODO: need uiAttached boolean in instance data
940 //if (d.uiAttached) {
941 if (i === 0) {
942 $('<img src="img/ui.png">').attr('class','ui').appendTo(el);
943 }
944 });
Simon Hunta5e89142014-11-14 07:00:33 -0800945
946 // operate on existing + new onoses here
947
948 // the departed...
949 var exiting = onoses.exit()
950 .transition()
951 .style('opacity', 0)
952 .remove();
953 }
954
Simon Hunt9462e8c2014-11-14 17:28:09 -0800955 function clickInst(d) {
956 var el = d3.select(this),
957 aff = el.classed('affinity');
958 if (!aff) {
959 setAffinity(el, d);
960 } else {
961 cancelAffinity();
962 }
963 }
964
965 function setAffinity(el, d) {
966 d3.selectAll('.onosInst')
967 .classed('mastership', true)
968 .classed('affinity', false);
969 el.classed('affinity', true);
970
971 suppressLayers(true);
972 node.each(function (n) {
973 if (n.master === d.id) {
974 n.el.classed('suppressed', false);
975 }
976 });
977 oiShowMaster = true;
978 }
979
980 function cancelAffinity() {
981 d3.selectAll('.onosInst')
982 .classed('mastership affinity', false);
983 restoreLayerState();
984 oiShowMaster = false;
985 }
986
Simon Hunta5e89142014-11-14 07:00:33 -0800987 // ==============================
Simon Hunt99c13842014-11-06 18:23:12 -0800988 // force layout modification functions
989
990 function translate(x, y) {
991 return 'translate(' + x + ',' + y + ')';
992 }
993
Simon Hunte2575b62014-11-18 15:25:53 -0800994 function rotate(deg) {
995 return 'rotate(' + deg + ')';
996 }
997
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800998 function missMsg(what, id) {
999 return '\n[' + what + '] "' + id + '" missing ';
1000 }
1001
1002 function linkEndPoints(srcId, dstId) {
1003 var srcNode = network.lookup[srcId],
1004 dstNode = network.lookup[dstId],
1005 sMiss = !srcNode ? missMsg('src', srcId) : '',
1006 dMiss = !dstNode ? missMsg('dst', dstId) : '';
1007
1008 if (sMiss || dMiss) {
1009 logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
1010 return null;
1011 }
1012 return {
1013 source: srcNode,
1014 target: dstNode,
1015 x1: srcNode.x,
1016 y1: srcNode.y,
1017 x2: dstNode.x,
1018 y2: dstNode.y
1019 };
1020 }
1021
Simon Hunt56d51852014-11-09 13:03:35 -08001022 function createHostLink(host) {
1023 var src = host.id,
1024 dst = host.cp.device,
Simon Hunt7cd48f32014-11-09 23:42:50 -08001025 id = host.ingress,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001026 lnk = linkEndPoints(src, dst);
Simon Hunt56d51852014-11-09 13:03:35 -08001027
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001028 if (!lnk) {
Simon Hunt56d51852014-11-09 13:03:35 -08001029 return null;
1030 }
1031
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001032 // Synthesize link ...
1033 $.extend(lnk, {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001034 key: id,
Simon Hunt56d51852014-11-09 13:03:35 -08001035 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -08001036
1037 type: function () { return 'hostLink'; },
1038 // TODO: ideally, we should see if our edge switch is online...
1039 online: function () { return true; },
1040 linkWidth: function () { return 1; }
Simon Hunt7cd48f32014-11-09 23:42:50 -08001041 });
Simon Hunt99c13842014-11-06 18:23:12 -08001042 return lnk;
1043 }
1044
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001045 function createLink(link) {
1046 var lnk = linkEndPoints(link.src, link.dst),
1047 type = link.type;
1048
1049 if (!lnk) {
1050 return null;
1051 }
1052
Simon Hunt8257f4c2014-11-16 19:34:54 -08001053 $.extend(lnk, {
1054 key: link.id,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001055 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -08001056 fromSource: link,
1057
1058 // functions to aggregate dual link state
1059 type: function () {
1060 var s = lnk.fromSource,
1061 t = lnk.fromTarget;
1062 return (s && s.type) || (t && t.type) || defaultLinkType;
1063 },
1064 online: function () {
1065 var s = lnk.fromSource,
1066 t = lnk.fromTarget;
1067 return (s && s.online) || (t && t.online);
1068 },
1069 linkWidth: function () {
1070 var s = lnk.fromSource,
1071 t = lnk.fromTarget,
1072 ws = (s && s.linkWidth) || 0,
1073 wt = (t && t.linkWidth) || 0;
1074 return Math.max(ws, wt);
1075 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001076 });
1077 return lnk;
Simon Hunt1a9eff92014-11-07 11:06:34 -08001078 }
1079
Simon Hunte2575b62014-11-18 15:25:53 -08001080 function removeLinkLabels() {
1081 network.links.forEach(function (d) {
1082 d.label = '';
1083 });
1084 }
1085
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001086 var widthRatio = 1.4,
1087 linkScale = d3.scale.linear()
1088 .domain([1, 12])
1089 .range([widthRatio, 12 * widthRatio])
1090 .clamp(true);
1091
Simon Hunt99c13842014-11-06 18:23:12 -08001092 function updateLinks() {
1093 link = linkG.selectAll('.link')
Simon Hunt8257f4c2014-11-16 19:34:54 -08001094 .data(network.links, function (d) { return d.key; });
Simon Hunt99c13842014-11-06 18:23:12 -08001095
1096 // operate on existing links, if necessary
1097 // link .foo() .bar() ...
1098
1099 // operate on entering links:
1100 var entering = link.enter()
1101 .append('line')
1102 .attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001103 x1: function (d) { return d.x1; },
1104 y1: function (d) { return d.y1; },
1105 x2: function (d) { return d.x2; },
1106 y2: function (d) { return d.y2; },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001107 stroke: config.topo.linkInColor,
1108 'stroke-width': config.topo.linkInWidth
Simon Hunt99c13842014-11-06 18:23:12 -08001109 });
1110
1111 // augment links
Simon Hunt7cd48f32014-11-09 23:42:50 -08001112 entering.each(function (d) {
1113 var link = d3.select(this);
1114 // provide ref to element selection from backing data....
1115 d.el = link;
Simon Hunt8257f4c2014-11-16 19:34:54 -08001116 restyleLinkElement(d);
Simon Hunt7cd48f32014-11-09 23:42:50 -08001117 });
Thomas Vachuska4830d392014-11-09 17:09:56 -08001118
1119 // operate on both existing and new links, if necessary
1120 //link .foo() .bar() ...
1121
Simon Hunte2575b62014-11-18 15:25:53 -08001122 // apply or remove labels
1123 var labelData = getLabelData();
1124 applyLinkLabels(labelData);
1125
Thomas Vachuska4830d392014-11-09 17:09:56 -08001126 // operate on exiting links:
Thomas Vachuska4830d392014-11-09 17:09:56 -08001127 link.exit()
Simon Hunt13bf9c82014-11-18 07:26:44 -08001128 .attr('stroke-dasharray', '3, 3')
1129 .style('opacity', 0.5)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001130 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -08001131 .duration(1500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001132 .attr({
Simon Hunt13bf9c82014-11-18 07:26:44 -08001133 'stroke-dasharray': '3, 12',
1134 stroke: config.topo.linkOutColor,
1135 'stroke-width': config.topo.linkOutWidth
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001136 })
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001137 .style('opacity', 0.0)
Thomas Vachuska4830d392014-11-09 17:09:56 -08001138 .remove();
Simon Hunte2575b62014-11-18 15:25:53 -08001139
1140 // NOTE: invoke a single tick to force the labels to position
1141 // onto their links.
1142 tick();
1143 }
1144
1145 function getLabelData() {
1146 // create the backing data for showing labels..
1147 var data = [];
1148 link.each(function (d) {
1149 if (d.label) {
1150 data.push({
1151 id: 'lab-' + d.key,
1152 key: d.key,
1153 label: d.label,
1154 ldata: d
1155 });
1156 }
1157 });
1158 return data;
1159 }
1160
1161 var linkLabelOffset = '0.3em';
1162
1163 function applyLinkLabels(data) {
1164 var entering;
1165
1166 linkLabel = linkLabelG.selectAll('.linkLabel')
1167 .data(data, function (d) { return d.id; });
1168
Simon Hunt56a2ea42014-11-19 12:39:31 -08001169 // for elements already existing, we need to update the text
1170 // and adjust the rectangle size to fit
1171 linkLabel.each(function (d) {
1172 var el = d3.select(this),
1173 rect = el.select('rect'),
1174 text = el.select('text');
1175 text.text(d.label);
1176 rect.attr(rectAroundText(el));
1177 });
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -08001178
Simon Hunte2575b62014-11-18 15:25:53 -08001179 entering = linkLabel.enter().append('g')
1180 .classed('linkLabel', true)
1181 .attr('id', function (d) { return d.id; });
1182
1183 entering.each(function (d) {
1184 var el = d3.select(this),
1185 rect,
1186 text,
1187 parms = {
1188 x1: d.ldata.x1,
1189 y1: d.ldata.y1,
1190 x2: d.ldata.x2,
1191 y2: d.ldata.y2
1192 };
1193
1194 d.el = el;
1195 rect = el.append('rect');
1196 text = el.append('text').text(d.label);
1197 rect.attr(rectAroundText(el));
1198 text.attr('dy', linkLabelOffset);
1199
1200 el.attr('transform', transformLabel(parms));
1201 });
1202
1203 // Remove any links that are no longer required.
1204 linkLabel.exit().remove();
1205 }
1206
1207 function rectAroundText(el) {
1208 var text = el.select('text'),
1209 box = text.node().getBBox();
1210
1211 // translate the bbox so that it is centered on [x,y]
1212 box.x = -box.width / 2;
1213 box.y = -box.height / 2;
1214
1215 // add padding
1216 box.x -= 1;
1217 box.width += 2;
1218 return box;
1219 }
1220
1221 function transformLabel(p) {
1222 var dx = p.x2 - p.x1,
1223 dy = p.y2 - p.y1,
1224 xMid = dx/2 + p.x1,
1225 yMid = dy/2 + p.y1;
1226 //length = Math.sqrt(dx*dx + dy*dy),
1227 //rads = Math.asin(dy/length),
1228 //degs = rads / (Math.PI*2) * 360;
1229
1230 return translate(xMid, yMid);
1231
1232 // TODO: consider making label parallel to line
1233 //return [
1234 // translate(xMid, yMid),
1235 // rotate(degs),
1236 // translate(0, 8)
1237 //].join('');
Simon Hunt99c13842014-11-06 18:23:12 -08001238 }
1239
1240 function createDeviceNode(device) {
1241 // start with the object as is
1242 var node = device,
Simon Huntbb282f52014-11-10 11:08:19 -08001243 type = device.type,
Simon Huntc72967b2014-11-20 09:21:42 -08001244 svgCls = type ? 'node device ' + type : 'node device',
1245 labels = device.labels || [];
1246
1247 labels.unshift(''); // add 'no-label' to front of cycle
Simon Hunt99c13842014-11-06 18:23:12 -08001248
1249 // Augment as needed...
1250 node.class = 'device';
Simon Huntbb282f52014-11-10 11:08:19 -08001251 node.svgClass = device.online ? svgCls + ' online' : svgCls;
Simon Hunt99c13842014-11-06 18:23:12 -08001252 positionNode(node);
1253
1254 // cache label array length
Simon Huntc72967b2014-11-20 09:21:42 -08001255 // TODO: need a uiConfig event from the server to set things
1256 // like device labels count, host labels count, etc.
1257 // The current method (here) is a little fragile
Simon Hunt99c13842014-11-06 18:23:12 -08001258 network.deviceLabelCount = device.labels.length;
Simon Hunt99c13842014-11-06 18:23:12 -08001259 return node;
1260 }
1261
Simon Hunt56d51852014-11-09 13:03:35 -08001262 function createHostNode(host) {
1263 // start with the object as is
1264 var node = host;
1265
1266 // Augment as needed...
1267 node.class = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001268 if (!node.type) {
Simon Hunteb1514d2014-11-20 09:57:29 -08001269 node.type = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001270 }
Simon Hunt7fa116d2014-11-17 14:16:55 -08001271 node.svgClass = 'node host ' + node.type;
Simon Hunt56d51852014-11-09 13:03:35 -08001272 positionNode(node);
1273
1274 // cache label array length
1275 network.hostLabelCount = host.labels.length;
Simon Hunt56d51852014-11-09 13:03:35 -08001276 return node;
1277 }
1278
Simon Hunt99c13842014-11-06 18:23:12 -08001279 function positionNode(node) {
1280 var meta = node.metaUi,
Simon Huntac9e24f2014-11-12 10:12:21 -08001281 x = meta && meta.x,
1282 y = meta && meta.y,
1283 xy;
Simon Hunt99c13842014-11-06 18:23:12 -08001284
Simon Huntac9e24f2014-11-12 10:12:21 -08001285 // If we have [x,y] already, use that...
Simon Hunt99c13842014-11-06 18:23:12 -08001286 if (x && y) {
1287 node.fixed = true;
Simon Huntac9e24f2014-11-12 10:12:21 -08001288 node.x = x;
1289 node.y = y;
1290 return;
Simon Hunt99c13842014-11-06 18:23:12 -08001291 }
Simon Huntac9e24f2014-11-12 10:12:21 -08001292
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001293 var location = node.location;
1294 if (location && location.type === 'latlng') {
1295 var coord = geoMapProjection([location.lng, location.lat]);
1296 node.fixed = true;
1297 node.x = coord[0];
1298 node.y = coord[1];
1299 return;
1300 }
1301
Simon Huntac9e24f2014-11-12 10:12:21 -08001302 // Note: Placing incoming unpinned nodes at exactly the same point
1303 // (center of the view) causes them to explode outwards when
1304 // the force layout kicks in. So, we spread them out a bit
1305 // initially, to provide a more serene layout convergence.
1306 // Additionally, if the node is a host, we place it near
1307 // the device it is connected to.
1308
1309 function spread(s) {
1310 return Math.floor((Math.random() * s) - s/2);
1311 }
1312
1313 function randDim(dim) {
1314 return dim / 2 + spread(dim * 0.7071);
1315 }
1316
1317 function rand() {
1318 return {
1319 x: randDim(network.view.width()),
1320 y: randDim(network.view.height())
1321 };
1322 }
1323
1324 function near(node) {
1325 var min = 12,
1326 dx = spread(12),
1327 dy = spread(12);
1328 return {
1329 x: node.x + min + dx,
1330 y: node.y + min + dy
1331 };
1332 }
1333
1334 function getDevice(cp) {
1335 var d = network.lookup[cp.device];
1336 return d || rand();
1337 }
1338
1339 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
1340 $.extend(node, xy);
Simon Hunt99c13842014-11-06 18:23:12 -08001341 }
1342
Thomas Vachuska89543292014-11-19 11:28:33 -08001343 function iconUrl(d) {
1344 return 'img/' + d.type + '.png';
Simon Hunt99c13842014-11-06 18:23:12 -08001345 }
1346
Simon Huntc72967b2014-11-20 09:21:42 -08001347 function iconGlyphUrl(d) {
1348 var which = d.type || 'unknown';
1349 return '#' + which;
1350 }
1351
Simon Hunt99c13842014-11-06 18:23:12 -08001352 // returns the newly computed bounding box of the rectangle
1353 function adjustRectToFitText(n) {
1354 var text = n.select('text'),
1355 box = text.node().getBBox(),
1356 lab = config.labels;
1357
1358 text.attr('text-anchor', 'middle')
1359 .attr('y', '-0.8em')
1360 .attr('x', lab.imgPad/2);
1361
1362 // translate the bbox so that it is centered on [x,y]
1363 box.x = -box.width / 2;
1364 box.y = -box.height / 2;
1365
1366 // add padding
1367 box.x -= (lab.padLR + lab.imgPad/2);
1368 box.width += lab.padLR * 2 + lab.imgPad;
1369 box.y -= lab.padTB;
1370 box.height += lab.padTB * 2;
1371
1372 return box;
1373 }
1374
Simon Hunt1a9eff92014-11-07 11:06:34 -08001375 function mkSvgClass(d) {
1376 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
1377 }
1378
Simon Hunt7cd48f32014-11-09 23:42:50 -08001379 function hostLabel(d) {
1380 var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0;
1381 return d.labels[idx];
1382 }
1383 function deviceLabel(d) {
1384 var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0;
1385 return d.labels[idx];
1386 }
Simon Huntc72967b2014-11-20 09:21:42 -08001387 function trimLabel(label) {
1388 return (label && label.trim()) || '';
1389 }
1390
1391 function emptyBox() {
1392 return {
1393 x: -2,
1394 y: -2,
1395 width: 4,
1396 height: 4
1397 };
Simon Hunt7cd48f32014-11-09 23:42:50 -08001398 }
1399
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001400 function updateDeviceLabel(d) {
Simon Huntc72967b2014-11-20 09:21:42 -08001401 var label = trimLabel(deviceLabel(d)),
1402 noLabel = !label,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001403 node = d.el,
Simon Huntc72967b2014-11-20 09:21:42 -08001404 box,
1405 dx,
1406 dy;
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001407
1408 node.select('text')
1409 .text(label)
1410 .style('opacity', 0)
1411 .transition()
1412 .style('opacity', 1);
1413
Simon Huntc72967b2014-11-20 09:21:42 -08001414 if (noLabel) {
1415 box = emptyBox();
1416 dx = -config.icons.device.dim/2;
1417 dy = -config.icons.device.dim/2;
1418 } else {
1419 box = adjustRectToFitText(node);
1420 dx = box.x + config.icons.xoff;
1421 dy = box.y + config.icons.yoff;
1422 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001423
1424 node.select('rect')
1425 .transition()
1426 .attr(box);
1427
Simon Huntc72967b2014-11-20 09:21:42 -08001428 node.select('g.deviceIcon')
Thomas Vachuska89543292014-11-19 11:28:33 -08001429 .transition()
Simon Huntc72967b2014-11-20 09:21:42 -08001430 .attr('transform', translate(dx, dy));
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001431 }
1432
1433 function updateHostLabel(d) {
1434 var label = hostLabel(d),
1435 host = d.el;
1436
1437 host.select('text').text(label);
1438 }
1439
Simon Huntbb282f52014-11-10 11:08:19 -08001440 function updateDeviceState(nodeData) {
1441 nodeData.el.classed('online', nodeData.online);
1442 updateDeviceLabel(nodeData);
1443 // TODO: review what else might need to be updated
1444 }
1445
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001446 function updateLinkState(linkData) {
1447 updateLinkWidth(linkData);
Thomas Vachuskabadb93f2014-11-15 23:51:17 -08001448 linkData.el.classed('inactive', !linkData.online);
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001449 // TODO: review what else might need to be updated
1450 // update label, if showing
1451 }
1452
Simon Huntbb282f52014-11-10 11:08:19 -08001453 function updateHostState(hostData) {
1454 updateHostLabel(hostData);
1455 // TODO: review what else might need to be updated
1456 }
1457
Simon Hunt6ac93f32014-11-13 12:17:27 -08001458 function nodeMouseOver(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001459 hovered = d;
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -08001460 if (trafficHover() && (d.class === 'host' || d.class === 'device')) {
Thomas Vachuska29617e52014-11-20 03:17:46 -08001461 showSelectTraffic();
1462 } else if (flowsHover() && (d.class === 'device')) {
1463 showDeviceLinkFlows();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001464 }
1465 }
1466
1467 function nodeMouseOut(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001468 hovered = null;
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -08001469 if (trafficHover() && (d.class === 'host' || d.class === 'device')) {
Thomas Vachuska29617e52014-11-20 03:17:46 -08001470 showSelectTraffic();
1471 } else if (flowsHover() && (d.class === 'device')) {
1472 showDeviceLinkFlows();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001473 }
1474 }
Simon Huntbb282f52014-11-10 11:08:19 -08001475
Simon Hunteb1514d2014-11-20 09:57:29 -08001476 function addHostIcon(node, radius, iid) {
Thomas Vachuska89543292014-11-19 11:28:33 -08001477 var dim = radius * 1.5,
1478 xlate = -dim / 2;
1479
Simon Hunteb1514d2014-11-20 09:57:29 -08001480 node.append('use').attr({
1481 class: 'glyphIcon hostIcon',
1482 transform: translate(xlate,xlate),
1483 'xlink:href': iid,
1484 width: dim,
1485 height: dim
1486 });
Thomas Vachuska89543292014-11-19 11:28:33 -08001487 }
1488
Simon Hunt99c13842014-11-06 18:23:12 -08001489 function updateNodes() {
1490 node = nodeG.selectAll('.node')
1491 .data(network.nodes, function (d) { return d.id; });
1492
Simon Huntc72967b2014-11-20 09:21:42 -08001493 // TODO: operate on existing nodes
Simon Hunt7cd48f32014-11-09 23:42:50 -08001494 // update host labels
Simon Hunt99c13842014-11-06 18:23:12 -08001495 //node .foo() .bar() ...
1496
1497 // operate on entering nodes:
1498 var entering = node.enter()
1499 .append('g')
1500 .attr({
1501 id: function (d) { return safeId(d.id); },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001502 class: mkSvgClass,
Simon Hunt99c13842014-11-06 18:23:12 -08001503 transform: function (d) { return translate(d.x, d.y); },
1504 opacity: 0
1505 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08001506 .call(network.drag)
Simon Hunt6ac93f32014-11-13 12:17:27 -08001507 .on('mouseover', nodeMouseOver)
1508 .on('mouseout', nodeMouseOut)
Simon Hunt99c13842014-11-06 18:23:12 -08001509 .transition()
1510 .attr('opacity', 1);
1511
1512 // augment device nodes...
1513 entering.filter('.device').each(function (d) {
1514 var node = d3.select(this),
Simon Huntc72967b2014-11-20 09:21:42 -08001515 label = trimLabel(deviceLabel(d)),
1516 noLabel = !label,
Simon Hunt99c13842014-11-06 18:23:12 -08001517 box;
1518
Simon Hunt7cd48f32014-11-09 23:42:50 -08001519 // provide ref to element from backing data....
1520 d.el = node;
1521
Simon Hunt99c13842014-11-06 18:23:12 -08001522 node.append('rect')
1523 .attr({
1524 'rx': 5,
1525 'ry': 5
1526 });
1527
1528 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001529 .text(label)
Simon Hunt99c13842014-11-06 18:23:12 -08001530 .attr('dy', '1.1em');
1531
1532 box = adjustRectToFitText(node);
1533
1534 node.select('rect')
1535 .attr(box);
1536
Simon Huntc72967b2014-11-20 09:21:42 -08001537 addDeviceIcon(node, box, noLabel, iconGlyphUrl(d));
Simon Hunt99c13842014-11-06 18:23:12 -08001538
Simon Huntc7ee0662014-11-05 16:44:37 -08001539 });
Simon Hunt934c3ce2014-11-05 11:45:07 -08001540
Thomas Vachuska89543292014-11-19 11:28:33 -08001541 // TODO: better place for this configuration state
1542 var defaultHostRadius = 9,
1543 hostRadius = {
1544 bgpSpeaker: 14,
1545 router: 14,
1546 host: 14
1547 },
Simon Hunteb1514d2014-11-20 09:57:29 -08001548 hostGlyphId = {
Thomas Vachuska89543292014-11-19 11:28:33 -08001549 bgpSpeaker: 'bgpSpeaker',
1550 router: 'router',
1551 host: 'host'
1552 };
1553
1554
Simon Hunt56d51852014-11-09 13:03:35 -08001555 // augment host nodes...
1556 entering.filter('.host').each(function (d) {
1557 var node = d3.select(this),
Thomas Vachuska89543292014-11-19 11:28:33 -08001558 r = hostRadius[d.type] || defaultHostRadius,
1559 textDy = r + 10,
Simon Hunteb1514d2014-11-20 09:57:29 -08001560 iid = iconGlyphUrl(d);
Simon Hunt56d51852014-11-09 13:03:35 -08001561
Simon Hunt7cd48f32014-11-09 23:42:50 -08001562 // provide ref to element from backing data....
1563 d.el = node;
1564
Thomas Vachuska89543292014-11-19 11:28:33 -08001565 node.append('circle')
1566 .attr('r', r);
Simon Hunt7fa116d2014-11-17 14:16:55 -08001567
Simon Hunteb1514d2014-11-20 09:57:29 -08001568 if (iid) {
1569 addHostIcon(node, r, iid);
Simon Hunt7fa116d2014-11-17 14:16:55 -08001570 }
Simon Hunt56d51852014-11-09 13:03:35 -08001571
Paul Greyson29cd58f2014-11-18 13:14:57 -08001572
Simon Hunt56d51852014-11-09 13:03:35 -08001573 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001574 .text(hostLabel)
Thomas Vachuska89543292014-11-19 11:28:33 -08001575 .attr('dy', textDy)
Simon Hunt7cd48f32014-11-09 23:42:50 -08001576 .attr('text-anchor', 'middle');
Simon Hunt56d51852014-11-09 13:03:35 -08001577
1578 // debug function to show the modelled x,y coordinates of nodes...
1579 if (debug('showNodeXY')) {
1580 node.select('circle').attr('fill-opacity', 0.5);
1581 node.append('circle')
1582 .attr({
1583 class: 'debug',
1584 cx: 0,
1585 cy: 0,
1586 r: '3px'
1587 });
1588 }
1589 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001590
Simon Hunt99c13842014-11-06 18:23:12 -08001591 // operate on both existing and new nodes, if necessary
1592 //node .foo() .bar() ...
Simon Huntc7ee0662014-11-05 16:44:37 -08001593
Simon Hunt99c13842014-11-06 18:23:12 -08001594 // operate on exiting nodes:
Simon Huntea80eb42014-11-11 13:46:57 -08001595 // Note that the node is removed after 2 seconds.
1596 // Sub element animations should be shorter than 2 seconds.
1597 var exiting = node.exit()
Simon Hunt44031102014-11-11 13:20:36 -08001598 .transition()
1599 .duration(2000)
Simon Huntea80eb42014-11-11 13:46:57 -08001600 .style('opacity', 0)
Simon Hunt99c13842014-11-06 18:23:12 -08001601 .remove();
Simon Huntea80eb42014-11-11 13:46:57 -08001602
1603 // host node exits....
1604 exiting.filter('.host').each(function (d) {
1605 var node = d3.select(this);
1606
1607 node.select('text')
1608 .style('opacity', 0.5)
1609 .transition()
1610 .duration(1000)
1611 .style('opacity', 0);
1612 // note, leave <g>.remove to remove this element
1613
Thomas Vachuska89543292014-11-19 11:28:33 -08001614 node.select('circle')
1615 .style('stroke-fill', '#555')
1616 .style('fill', '#888')
Simon Huntea80eb42014-11-11 13:46:57 -08001617 .style('opacity', 0.5)
1618 .transition()
1619 .duration(1500)
1620 .attr('r', 0);
1621 // note, leave <g>.remove to remove this element
1622
1623 });
1624
1625 // TODO: device node exits
Simon Huntc7ee0662014-11-05 16:44:37 -08001626 }
1627
Simon Huntc72967b2014-11-20 09:21:42 -08001628 function addDeviceIcon(node, box, noLabel, iid) {
1629 var cfg = config.icons.device,
1630 dx,
1631 dy,
1632 g;
1633
1634 if (noLabel) {
1635 box = emptyBox();
1636 dx = -cfg.dim/2;
1637 dy = -cfg.dim/2;
1638 } else {
1639 box = adjustRectToFitText(node);
1640 dx = box.x + config.icons.xoff;
1641 dy = box.y + config.icons.yoff;
1642 }
1643
Simon Hunteb1514d2014-11-20 09:57:29 -08001644 g = node.append('g')
1645 .attr('class', 'glyphIcon deviceIcon')
Simon Huntc72967b2014-11-20 09:21:42 -08001646 .attr('transform', translate(dx, dy));
1647
1648 g.append('rect').attr({
1649 x: 0,
1650 y: 0,
1651 rx: cfg.rx,
1652 width: cfg.dim,
1653 height: cfg.dim
1654 });
1655
1656 g.append('use').attr({
1657 'xlink:href': iid,
1658 width: cfg.dim,
1659 height: cfg.dim
1660 });
1661
1662/*
1663 if (icon) {
1664 node.append('rect')
1665 .attr({
1666 class: 'iconUnderlay',
1667 x: box.x + config.icons.xoff,
1668 y: box.y + config.icons.yoff,
1669 width: cfg.w,
1670 height: cfg.h,
1671 rx: 4
1672 }).style({
1673 stroke: '#000',
1674 fill: '#ddd'
1675 });
1676 node.append('svg:image')
1677 .attr({
1678 x: box.x + config.icons.xoff + 2,
1679 y: box.y + config.icons.yoff + 2,
1680 width: cfg.w - 4,
1681 height: cfg.h - 4,
1682 'xlink:href': icon
1683 });
1684 }
1685*/
1686 }
1687
1688
1689
Simon Hunt8257f4c2014-11-16 19:34:54 -08001690 function find(key, array) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001691 for (var idx = 0, n = array.length; idx < n; idx++) {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001692 if (array[idx].key === key) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001693 return idx;
1694 }
1695 }
1696 return -1;
1697 }
1698
1699 function removeLinkElement(linkData) {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001700 var idx = find(linkData.key, network.links),
1701 removed;
1702 if (idx >=0) {
1703 // remove from links array
1704 removed = network.links.splice(idx, 1);
1705 // remove from lookup cache
1706 delete network.lookup[removed[0].key];
1707 updateLinks();
1708 network.force.resume();
1709 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001710 }
Simon Huntc7ee0662014-11-05 16:44:37 -08001711
Simon Hunt44031102014-11-11 13:20:36 -08001712 function removeHostElement(hostData) {
1713 // first, remove associated hostLink...
1714 removeLinkElement(hostData.linkData);
1715
1716 // remove from lookup cache
1717 delete network.lookup[hostData.id];
1718 // remove from nodes array
1719 var idx = find(hostData.id, network.nodes);
1720 network.nodes.splice(idx, 1);
1721 // remove from SVG
1722 updateNodes();
1723 network.force.resume();
1724 }
1725
1726
Simon Huntc7ee0662014-11-05 16:44:37 -08001727 function tick() {
1728 node.attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001729 transform: function (d) { return translate(d.x, d.y); }
Simon Huntc7ee0662014-11-05 16:44:37 -08001730 });
1731
1732 link.attr({
1733 x1: function (d) { return d.source.x; },
1734 y1: function (d) { return d.source.y; },
1735 x2: function (d) { return d.target.x; },
1736 y2: function (d) { return d.target.y; }
1737 });
Simon Hunte2575b62014-11-18 15:25:53 -08001738
1739 linkLabel.each(function (d) {
1740 var el = d3.select(this);
Thomas Vachuska4731f122014-11-20 04:56:19 -08001741 var lnk = findLinkById(d.key);
1742
1743 if (lnk) {
1744 var parms = {
Simon Hunte2575b62014-11-18 15:25:53 -08001745 x1: lnk.source.x,
1746 y1: lnk.source.y,
1747 x2: lnk.target.x,
1748 y2: lnk.target.y
1749 };
Thomas Vachuska4731f122014-11-20 04:56:19 -08001750 el.attr('transform', transformLabel(parms));
1751 }
Simon Hunte2575b62014-11-18 15:25:53 -08001752 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001753 }
Simon Hunt934c3ce2014-11-05 11:45:07 -08001754
1755 // ==============================
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001756 // Web-Socket for live data
1757
1758 function webSockUrl() {
1759 return document.location.toString()
1760 .replace(/\#.*/, '')
1761 .replace('http://', 'ws://')
1762 .replace('https://', 'wss://')
1763 .replace('index2.html', config.webSockUrl);
1764 }
1765
1766 webSock = {
1767 ws : null,
1768
1769 connect : function() {
1770 webSock.ws = new WebSocket(webSockUrl());
1771
1772 webSock.ws.onopen = function() {
Simon Hunt0c6d4192014-11-12 12:07:10 -08001773 noWebSock(false);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001774 };
1775
1776 webSock.ws.onmessage = function(m) {
1777 if (m.data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001778 wsTraceRx(m.data);
Thomas Vachuskad472c6e2014-11-07 19:11:05 -08001779 handleServerEvent(JSON.parse(m.data));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001780 }
1781 };
1782
1783 webSock.ws.onclose = function(m) {
1784 webSock.ws = null;
Simon Hunt0c6d4192014-11-12 12:07:10 -08001785 noWebSock(true);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001786 };
1787 },
1788
1789 send : function(text) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001790 if (text != null) {
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001791 webSock._send(text);
1792 }
1793 },
1794
1795 _send : function(message) {
1796 if (webSock.ws) {
1797 webSock.ws.send(message);
Simon Hunta255a2c2014-11-13 22:29:35 -08001798 } else if (config.useLiveData) {
Simon Hunt56d51852014-11-09 13:03:35 -08001799 network.view.alert('no web socket open\n\n' + message);
Simon Hunta255a2c2014-11-13 22:29:35 -08001800 } else {
1801 console.log('WS Send: ' + JSON.stringify(message));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001802 }
1803 }
1804
1805 };
1806
Simon Hunt0c6d4192014-11-12 12:07:10 -08001807 function noWebSock(b) {
1808 mask.style('display',b ? 'block' : 'none');
1809 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001810
1811 function sendMessage(evType, payload) {
1812 var toSend = {
Simon Huntbb282f52014-11-10 11:08:19 -08001813 event: evType,
1814 sid: ++sid,
1815 payload: payload
1816 },
1817 asText = JSON.stringify(toSend);
1818 wsTraceTx(asText);
1819 webSock.send(asText);
Simon Huntc76ae892014-11-18 17:31:51 -08001820
1821 // Temporary measure for debugging UI behavior ...
1822 if (!config.useLiveData) {
1823 handleTestSend(toSend);
1824 }
Simon Huntbb282f52014-11-10 11:08:19 -08001825 }
1826
1827 function wsTraceTx(msg) {
1828 wsTrace('tx', msg);
1829 }
1830 function wsTraceRx(msg) {
1831 wsTrace('rx', msg);
1832 }
1833 function wsTrace(rxtx, msg) {
Simon Huntbb282f52014-11-10 11:08:19 -08001834 console.log('[' + rxtx + '] ' + msg);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001835 }
1836
Simon Huntc76ae892014-11-18 17:31:51 -08001837 // NOTE: Temporary hardcoded example for showing detail pane
1838 // while we fine-
1839 // Probably should not merge this change...
1840 function handleTestSend(msg) {
1841 if (msg.event === 'requestDetails') {
1842 showDetails({
1843 event: 'showDetails',
1844 sid: 1001,
1845 payload: {
1846 "id": "of:0000ffffffffff09",
1847 "type": "roadm",
1848 "propOrder": [
1849 "Name",
1850 "Vendor",
1851 "H/W Version",
1852 "S/W Version",
1853 "-",
1854 "Latitude",
1855 "Longitude",
1856 "Ports"
1857 ],
1858 "props": {
1859 "Name": null,
1860 "Vendor": "Linc",
1861 "H/W Version": "OE",
1862 "S/W Version": "?",
1863 "-": "",
1864 "Latitude": "40.8",
1865 "Longitude": "73.1",
1866 "Ports": "2"
1867 }
1868 }
1869 });
1870 }
1871 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001872
1873 // ==============================
1874 // Selection stuff
1875
1876 function selectObject(obj, el) {
1877 var n,
Simon Hunt01095ff2014-11-13 16:37:29 -08001878 srcEv = d3.event.sourceEvent,
1879 meta = srcEv.metaKey,
1880 shift = srcEv.shiftKey;
1881
Simon Huntdeab4322014-11-13 18:49:07 -08001882 if ((panZoom() && !meta) || (!panZoom() && meta)) {
Simon Hunt01095ff2014-11-13 16:37:29 -08001883 return;
1884 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001885
1886 if (el) {
1887 n = d3.select(el);
1888 } else {
1889 node.each(function(d) {
1890 if (d == obj) {
1891 n = d3.select(el = this);
1892 }
1893 });
1894 }
1895 if (!n) return;
1896
Simon Hunt01095ff2014-11-13 16:37:29 -08001897 if (shift && n.classed('selected')) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001898 deselectObject(obj.id);
Simon Hunt61d04042014-11-11 17:27:16 -08001899 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001900 return;
1901 }
1902
Simon Hunt01095ff2014-11-13 16:37:29 -08001903 if (!shift) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001904 deselectAll();
1905 }
1906
Simon Huntc31d5692014-11-12 13:27:18 -08001907 selections[obj.id] = { obj: obj, el: el };
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001908 selectOrder.push(obj.id);
1909
1910 n.classed('selected', true);
Simon Hunt61d04042014-11-11 17:27:16 -08001911 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001912 }
1913
1914 function deselectObject(id) {
Simon Huntc31d5692014-11-12 13:27:18 -08001915 var obj = selections[id],
1916 idx;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001917 if (obj) {
1918 d3.select(obj.el).classed('selected', false);
Simon Hunt61d04042014-11-11 17:27:16 -08001919 delete selections[id];
Simon Huntc31d5692014-11-12 13:27:18 -08001920 idx = $.inArray(id, selectOrder);
1921 if (idx >= 0) {
1922 selectOrder.splice(idx, 1);
1923 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001924 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001925 }
1926
1927 function deselectAll() {
1928 // deselect all nodes in the network...
1929 node.classed('selected', false);
1930 selections = {};
1931 selectOrder = [];
Simon Hunt61d04042014-11-11 17:27:16 -08001932 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001933 }
1934
Simon Hunt61d04042014-11-11 17:27:16 -08001935 // update the state of the detail pane, based on current selections
1936 function updateDetailPane() {
1937 var nSel = selectOrder.length;
1938 if (!nSel) {
1939 detailPane.hide();
Simon Huntd72bc702014-11-13 18:38:04 -08001940 showTrafficAction(); // sends cancelTraffic event
Simon Hunt61d04042014-11-11 17:27:16 -08001941 } else if (nSel === 1) {
1942 singleSelect();
1943 } else {
1944 multiSelect();
1945 }
1946 }
1947
1948 function singleSelect() {
1949 requestDetails();
Simon Huntb53e0682014-11-12 13:32:01 -08001950 // NOTE: detail pane will be shown from showDetails event callback
Simon Hunt61d04042014-11-11 17:27:16 -08001951 }
1952
1953 function multiSelect() {
Simon Huntb53e0682014-11-12 13:32:01 -08001954 populateMultiSelect();
Simon Huntb53e0682014-11-12 13:32:01 -08001955 }
1956
1957 function addSep(tbody) {
1958 var tr = tbody.append('tr');
1959 $('<hr>').appendTo(tr.append('td').attr('colspan', 2));
1960 }
1961
1962 function addProp(tbody, label, value) {
1963 var tr = tbody.append('tr');
1964
1965 tr.append('td')
1966 .attr('class', 'label')
1967 .text(label + ' :');
1968
1969 tr.append('td')
1970 .attr('class', 'value')
1971 .text(value);
1972 }
1973
1974 function populateMultiSelect() {
1975 detailPane.empty();
1976
1977 var title = detailPane.append("h2"),
1978 table = detailPane.append("table"),
1979 tbody = table.append("tbody");
1980
Thomas Vachuska4731f122014-11-20 04:56:19 -08001981 title.text('Selected Nodes');
Simon Huntb53e0682014-11-12 13:32:01 -08001982
1983 selectOrder.forEach(function (d, i) {
1984 addProp(tbody, i+1, d);
1985 });
Simon Huntd72bc702014-11-13 18:38:04 -08001986
1987 addMultiSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08001988 }
1989
1990 function populateDetails(data) {
1991 detailPane.empty();
1992
1993 var title = detailPane.append("h2"),
1994 table = detailPane.append("table"),
1995 tbody = table.append("tbody");
1996
1997 $('<img src="img/' + data.type + '.png">').appendTo(title);
1998 $('<span>').attr('class', 'icon').text(data.id).appendTo(title);
1999
2000 data.propOrder.forEach(function(p) {
2001 if (p === '-') {
2002 addSep(tbody);
2003 } else {
2004 addProp(tbody, p, data.props[p]);
2005 }
2006 });
Simon Huntd72bc702014-11-13 18:38:04 -08002007
Thomas Vachuska4731f122014-11-20 04:56:19 -08002008 addSingleSelectActions(data);
Simon Hunt61d04042014-11-11 17:27:16 -08002009 }
2010
Thomas Vachuska4731f122014-11-20 04:56:19 -08002011 function addSingleSelectActions(data) {
Simon Huntd72bc702014-11-13 18:38:04 -08002012 detailPane.append('hr');
2013 // always want to allow 'show traffic'
Thomas Vachuska4731f122014-11-20 04:56:19 -08002014 addAction(detailPane, 'Show Related Traffic', showTrafficAction);
2015
2016 if (data.type === 'switch') {
2017 addAction(detailPane, 'Show Device Flows', showDeviceLinkFlowsAction);
2018 }
Simon Huntd72bc702014-11-13 18:38:04 -08002019 }
2020
2021 function addMultiSelectActions() {
2022 detailPane.append('hr');
2023 // always want to allow 'show traffic'
Thomas Vachuska4731f122014-11-20 04:56:19 -08002024 addAction(detailPane, 'Show Related Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08002025 // if exactly two hosts are selected, also want 'add host intent'
2026 if (nSel() === 2 && allSelectionsClass('host')) {
Thomas Vachuska4731f122014-11-20 04:56:19 -08002027 addAction(detailPane, 'Add Host-to-Host Intent', addIntentAction);
Simon Huntd72bc702014-11-13 18:38:04 -08002028 }
2029 }
2030
Simon Hunta5e89142014-11-14 07:00:33 -08002031 function addAction(panel, text, cb) {
2032 panel.append('div')
Simon Huntd72bc702014-11-13 18:38:04 -08002033 .classed('actionBtn', true)
2034 .text(text)
2035 .on('click', cb);
2036 }
2037
2038
Paul Greysonfcba0e82014-11-13 10:21:16 -08002039 function zoomPan(scale, translate) {
2040 zoomPanContainer.attr("transform", "translate(" + translate + ")scale(" + scale + ")");
2041 // keep the map lines constant width while zooming
Thomas Vachuska89543292014-11-19 11:28:33 -08002042 bgImg.style("stroke-width", 2.0 / scale + "px");
Paul Greysonfcba0e82014-11-13 10:21:16 -08002043 }
2044
2045 function resetZoomPan() {
2046 zoomPan(1, [0,0]);
2047 zoom.scale(1).translate([0,0]);
2048 }
2049
2050 function setupZoomPan() {
2051 function zoomed() {
Simon Huntdeab4322014-11-13 18:49:07 -08002052 if (!panZoom() ^ !d3.event.sourceEvent.metaKey) {
Paul Greysonfcba0e82014-11-13 10:21:16 -08002053 zoomPan(d3.event.scale, d3.event.translate);
2054 }
2055 }
2056
2057 zoom = d3.behavior.zoom()
2058 .translate([0, 0])
2059 .scale(1)
2060 .scaleExtent([1, 8])
2061 .on("zoom", zoomed);
2062
2063 svg.call(zoom);
2064 }
2065
Simon Hunt61d04042014-11-11 17:27:16 -08002066 // ==============================
2067 // Test harness code
Simon Hunt56d51852014-11-09 13:03:35 -08002068
2069 function prepareScenario(view, ctx, dbg) {
2070 var sc = scenario,
2071 urlSc = sc.evDir + ctx + sc.evScenario;
2072
2073 if (!ctx) {
2074 view.alert("No scenario specified (null ctx)");
2075 return;
2076 }
2077
2078 sc.view = view;
2079 sc.ctx = ctx;
2080 sc.debug = dbg;
2081 sc.evNumber = 0;
2082
2083 d3.json(urlSc, function(err, data) {
Simon Huntbb282f52014-11-10 11:08:19 -08002084 var p = data && data.params || {},
2085 desc = data && data.description || null,
Simon Huntfc274c92014-11-11 11:05:46 -08002086 intro = data && data.title;
Simon Huntbb282f52014-11-10 11:08:19 -08002087
Simon Hunt56d51852014-11-09 13:03:35 -08002088 if (err) {
2089 view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err);
2090 } else {
2091 sc.params = p;
Simon Huntbb282f52014-11-10 11:08:19 -08002092 if (desc) {
2093 intro += '\n\n ' + desc.join('\n ');
2094 }
2095 view.alert(intro);
Simon Hunt56d51852014-11-09 13:03:35 -08002096 }
2097 });
2098
2099 }
2100
Simon Hunt01095ff2014-11-13 16:37:29 -08002101 // ==============================
2102 // Toggle Buttons in masthead
Simon Hunt0c6d4192014-11-12 12:07:10 -08002103
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002104 // TODO: toggle button (and other widgets in the masthead) should be provided
2105 // by the framework; not generated by the view.
2106
Simon Hunta5e89142014-11-14 07:00:33 -08002107 var showInstances,
2108 doPanZoom,
2109 showTrafficOnHover;
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002110
2111 function addButtonBar(view) {
2112 var bb = d3.select('#mast')
2113 .append('span').classed('right', true).attr('id', 'bb');
2114
Simon Hunta5e89142014-11-14 07:00:33 -08002115 function mkTogBtn(text, cb) {
2116 return bb.append('span')
2117 .classed('btn', true)
2118 .text(text)
2119 .on('click', cb);
2120 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002121
Simon Hunta5e89142014-11-14 07:00:33 -08002122 showInstances = mkTogBtn('Show Instances', toggleInst);
Simon Hunte5b71752014-11-18 20:06:07 -08002123 //doPanZoom = mkTogBtn('Pan/Zoom', togglePanZoom);
2124 //showTrafficOnHover = mkTogBtn('Show traffic on hover', toggleTrafficHover);
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002125 }
2126
Simon Hunta5e89142014-11-14 07:00:33 -08002127 function instShown() {
2128 return showInstances.classed('active');
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002129 }
Simon Hunta5e89142014-11-14 07:00:33 -08002130 function toggleInst() {
2131 showInstances.classed('active', !instShown());
2132 if (instShown()) {
2133 oiBox.show();
2134 } else {
2135 oiBox.hide();
2136 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002137 }
2138
Simon Huntdeab4322014-11-13 18:49:07 -08002139 function panZoom() {
Simon Hunte5b71752014-11-18 20:06:07 -08002140 return false;
2141 //return doPanZoom.classed('active');
Simon Hunt01095ff2014-11-13 16:37:29 -08002142 }
Simon Hunta5e89142014-11-14 07:00:33 -08002143 function togglePanZoom() {
2144 doPanZoom.classed('active', !panZoom());
2145 }
2146
2147 function trafficHover() {
Thomas Vachuska29617e52014-11-20 03:17:46 -08002148 return hoverModes[hoverMode] === 'intents';
Simon Hunta5e89142014-11-14 07:00:33 -08002149 }
Thomas Vachuska29617e52014-11-20 03:17:46 -08002150
2151 function flowsHover() {
2152 return hoverModes[hoverMode] === 'flows';
2153 }
2154
Simon Hunta5e89142014-11-14 07:00:33 -08002155 function toggleTrafficHover() {
2156 showTrafficOnHover.classed('active', !trafficHover());
2157 }
2158
Simon Hunt7fa116d2014-11-17 14:16:55 -08002159 function loadGlyphs(svg) {
2160 var defs = svg.append('defs');
2161 gly.defBird(defs);
2162 gly.defBullhorn(defs);
Simon Huntc72967b2014-11-20 09:21:42 -08002163 gly.defGlyphs(defs);
Simon Hunt7fa116d2014-11-17 14:16:55 -08002164 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002165
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002166 // ==============================
Simon Hunt142d0032014-11-04 20:13:09 -08002167 // View life-cycle callbacks
Simon Hunt195cb382014-11-03 17:50:51 -08002168
Simon Huntf67722a2014-11-10 09:32:06 -08002169 function preload(view, ctx, flags) {
Simon Hunt142d0032014-11-04 20:13:09 -08002170 var w = view.width(),
2171 h = view.height(),
Simon Huntc7ee0662014-11-05 16:44:37 -08002172 fcfg = config.force,
2173 fpad = fcfg.pad,
2174 forceDim = [w - 2*fpad, h - 2*fpad];
Simon Hunt195cb382014-11-03 17:50:51 -08002175
Simon Hunt142d0032014-11-04 20:13:09 -08002176 // NOTE: view.$div is a D3 selection of the view's div
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002177 var viewBox = '0 0 ' + config.logicalSize + ' ' + config.logicalSize;
2178 svg = view.$div.append('svg').attr('viewBox', viewBox);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002179 setSize(svg, view);
2180
Simon Hunt7fa116d2014-11-17 14:16:55 -08002181 loadGlyphs(svg);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002182
Paul Greysonfcba0e82014-11-13 10:21:16 -08002183 zoomPanContainer = svg.append('g').attr('id', 'zoomPanContainer');
Paul Greysonfcba0e82014-11-13 10:21:16 -08002184 setupZoomPan();
2185
Simon Hunt1a9eff92014-11-07 11:06:34 -08002186 // add blue glow filter to svg layer
Paul Greysonfcba0e82014-11-13 10:21:16 -08002187 d3u.appendGlow(zoomPanContainer);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002188
Simon Huntc7ee0662014-11-05 16:44:37 -08002189 // group for the topology
Paul Greysonfcba0e82014-11-13 10:21:16 -08002190 topoG = zoomPanContainer.append('g')
Simon Huntd3b7d512014-11-12 15:48:41 -08002191 .attr('id', 'topo-G')
Simon Huntc7ee0662014-11-05 16:44:37 -08002192 .attr('transform', fcfg.translate());
2193
Simon Hunte2575b62014-11-18 15:25:53 -08002194 // subgroups for links, link labels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002195 linkG = topoG.append('g').attr('id', 'links');
Simon Hunte2575b62014-11-18 15:25:53 -08002196 linkLabelG = topoG.append('g').attr('id', 'linkLabels');
Simon Huntc7ee0662014-11-05 16:44:37 -08002197 nodeG = topoG.append('g').attr('id', 'nodes');
2198
Simon Hunte2575b62014-11-18 15:25:53 -08002199 // selection of links, linkLabels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002200 link = linkG.selectAll('.link');
Simon Hunte2575b62014-11-18 15:25:53 -08002201 linkLabel = linkLabelG.selectAll('.linkLabel');
Simon Huntc7ee0662014-11-05 16:44:37 -08002202 node = nodeG.selectAll('.node');
2203
Simon Hunt7cd48f32014-11-09 23:42:50 -08002204 function chrg(d) {
2205 return fcfg.charge[d.class] || -12000;
2206 }
Simon Hunt99c13842014-11-06 18:23:12 -08002207 function ldist(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002208 return fcfg.linkDistance[d.type] || 50;
Simon Hunt99c13842014-11-06 18:23:12 -08002209 }
2210 function lstrg(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002211 // 0.0 - 1.0
2212 return fcfg.linkStrength[d.type] || 1.0;
Simon Hunt99c13842014-11-06 18:23:12 -08002213 }
2214
Simon Hunt1a9eff92014-11-07 11:06:34 -08002215 function selectCb(d, self) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002216 selectObject(d, self);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002217 }
2218
2219 function atDragEnd(d, self) {
Simon Hunt56d51852014-11-09 13:03:35 -08002220 // once we've finished moving, pin the node in position
2221 d.fixed = true;
2222 d3.select(self).classed('fixed', true);
2223 if (config.useLiveData) {
Simon Hunt902c9922014-11-11 11:59:31 -08002224 sendUpdateMeta(d);
Simon Hunta255a2c2014-11-13 22:29:35 -08002225 } else {
2226 console.log('Moving node ' + d.id + ' to [' + d.x + ',' + d.y + ']');
Simon Hunt1a9eff92014-11-07 11:06:34 -08002227 }
2228 }
2229
Simon Hunt902c9922014-11-11 11:59:31 -08002230 function sendUpdateMeta(d) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002231 sendMessage('updateMeta', {
2232 id: d.id,
2233 'class': d.class,
Simon Hunt902c9922014-11-11 11:59:31 -08002234 'memento': {
Simon Hunt01095ff2014-11-13 16:37:29 -08002235 x: d.x,
2236 y: d.y
Simon Hunt902c9922014-11-11 11:59:31 -08002237 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002238 });
2239 }
2240
Simon Huntc7ee0662014-11-05 16:44:37 -08002241 // set up the force layout
2242 network.force = d3.layout.force()
2243 .size(forceDim)
2244 .nodes(network.nodes)
2245 .links(network.links)
Simon Hunt7cd48f32014-11-09 23:42:50 -08002246 .gravity(0.4)
2247 .friction(0.7)
2248 .charge(chrg)
Simon Hunt99c13842014-11-06 18:23:12 -08002249 .linkDistance(ldist)
2250 .linkStrength(lstrg)
Simon Huntc7ee0662014-11-05 16:44:37 -08002251 .on('tick', tick);
Simon Hunt195cb382014-11-03 17:50:51 -08002252
Simon Hunt01095ff2014-11-13 16:37:29 -08002253 network.drag = d3u.createDragBehavior(network.force,
Simon Huntdeab4322014-11-13 18:49:07 -08002254 selectCb, atDragEnd, panZoom);
Simon Hunt0c6d4192014-11-12 12:07:10 -08002255
2256 // create mask layer for when we lose connection to server.
Simon Hunta5e89142014-11-14 07:00:33 -08002257 // TODO: this should be part of the framework
Simon Hunt0c6d4192014-11-12 12:07:10 -08002258 mask = view.$div.append('div').attr('id','topo-mask');
2259 para(mask, 'Oops!');
2260 para(mask, 'Web-socket connection to server closed...');
2261 para(mask, 'Try refreshing the page.');
Simon Hunt12ce12e2014-11-15 21:13:19 -08002262
2263 mask.append('svg')
2264 .attr({
2265 id: 'mask-bird',
2266 width: w,
2267 height: h
2268 })
2269 .append('g')
2270 .attr('transform', birdTranslate(w, h))
2271 .style('opacity', 0.3)
2272 .append('use')
2273 .attr({
2274 'xlink:href': '#bird',
2275 width: config.birdDim,
2276 height: config.birdDim,
2277 fill: '#111'
Thomas Vachuska89543292014-11-19 11:28:33 -08002278 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08002279 }
Simon Hunt195cb382014-11-03 17:50:51 -08002280
Simon Hunt01095ff2014-11-13 16:37:29 -08002281 function para(sel, text) {
2282 sel.append('p').text(text);
2283 }
2284
2285
Simon Hunt56d51852014-11-09 13:03:35 -08002286 function load(view, ctx, flags) {
Simon Huntf67722a2014-11-10 09:32:06 -08002287 // resize, in case the window was resized while we were not loaded
2288 resize(view, ctx, flags);
2289
Simon Hunt99c13842014-11-06 18:23:12 -08002290 // cache the view token, so network topo functions can access it
2291 network.view = view;
Simon Hunt56d51852014-11-09 13:03:35 -08002292 config.useLiveData = !flags.local;
2293
2294 if (!config.useLiveData) {
2295 prepareScenario(view, ctx, flags.debug);
2296 }
Simon Hunt99c13842014-11-06 18:23:12 -08002297
2298 // set our radio buttons and key bindings
Simon Hunt9462e8c2014-11-14 17:28:09 -08002299 layerBtnSet = view.setRadio(layerButtons);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002300 view.setKeys(keyDispatch);
Simon Hunt195cb382014-11-03 17:50:51 -08002301
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002302 // patch in our "button bar" for now
2303 // TODO: implement a more official frameworky way of doing this..
2304 addButtonBar(view);
2305
Simon Huntd3b7d512014-11-12 15:48:41 -08002306 // Load map data asynchronously; complete startup after that..
2307 loadGeoJsonData();
Simon Hunta255a2c2014-11-13 22:29:35 -08002308
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002309 //var dashIdx = 0;
2310 //antTimer = setInterval(function () {
2311 // // TODO: figure out how to choose Src-->Dst and Dst-->Src, per link
2312 // dashIdx = dashIdx === 0 ? 14 : dashIdx - 2;
2313 // d3.selectAll('.animated').style('stroke-dashoffset', dashIdx);
2314 //}, 35);
Simon Hunta255a2c2014-11-13 22:29:35 -08002315 }
2316
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002317 function startAntTimer() {
Thomas Vachuska4731f122014-11-20 04:56:19 -08002318 if (!antTimer) {
2319 var pulses = [5, 3, 1.2, 3],
2320 pulse = 0;
2321 antTimer = setInterval(function () {
2322 pulse = pulse + 1;
2323 pulse = pulse === pulses.length ? 0 : pulse;
2324 d3.selectAll('.animated').style('stroke-width', pulses[pulse]);
2325 }, 200);
2326 }
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002327 }
2328
2329 function stopAntTimer() {
Simon Hunta255a2c2014-11-13 22:29:35 -08002330 if (antTimer) {
2331 clearInterval(antTimer);
2332 antTimer = null;
2333 }
Simon Huntd3b7d512014-11-12 15:48:41 -08002334 }
2335
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002336 function unload(view, ctx, flags) {
2337 stopAntTimer();
2338 }
2339
Simon Huntd3b7d512014-11-12 15:48:41 -08002340 // TODO: move these to config/state portion of script
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002341 var geoJsonUrl = 'json/map/continental_us.json', // TODO: Paul
Simon Huntd3b7d512014-11-12 15:48:41 -08002342 geoJson;
2343
2344 function loadGeoJsonData() {
2345 d3.json(geoJsonUrl, function (err, data) {
2346 if (err) {
2347 // fall back to USA map background
2348 loadStaticMap();
2349 } else {
2350 geoJson = data;
2351 loadGeoMap();
2352 }
2353
2354 // finally, connect to the server...
2355 if (config.useLiveData) {
2356 webSock.connect();
2357 }
2358 });
2359 }
2360
2361 function showBg() {
2362 return config.options.showBackground ? 'visible' : 'hidden';
2363 }
2364
2365 function loadStaticMap() {
2366 fnTrace('loadStaticMap', config.backgroundUrl);
2367 var w = network.view.width(),
2368 h = network.view.height();
2369
2370 // load the background image
2371 bgImg = svg.insert('svg:image', '#topo-G')
2372 .attr({
2373 id: 'topo-bg',
2374 width: w,
2375 height: h,
2376 'xlink:href': config.backgroundUrl
2377 })
2378 .style({
2379 visibility: showBg()
2380 });
2381 }
2382
2383 function loadGeoMap() {
2384 fnTrace('loadGeoMap', geoJsonUrl);
Simon Huntd3b7d512014-11-12 15:48:41 -08002385
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002386 // extracts the topojson data into geocoordinate-based geometry
2387 var topoData = topojson.feature(geoJson, geoJson.objects.states);
Simon Huntd3b7d512014-11-12 15:48:41 -08002388
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002389 // see: http://bl.ocks.org/mbostock/4707858
2390 geoMapProjection = d3.geo.mercator();
2391 var path = d3.geo.path().projection(geoMapProjection);
Simon Huntd3b7d512014-11-12 15:48:41 -08002392
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002393 geoMapProjection
2394 .scale(1)
2395 .translate([0, 0]);
Simon Huntd3b7d512014-11-12 15:48:41 -08002396
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002397 // [[x1,y1],[x2,y2]]
2398 var b = path.bounds(topoData);
Paul Greysonfcba0e82014-11-13 10:21:16 -08002399 // size map to 95% of minimum dimension to fill space
2400 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 -08002401 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 -08002402
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002403 geoMapProjection
2404 .scale(s)
2405 .translate(t);
2406
Paul Greysonfcba0e82014-11-13 10:21:16 -08002407 bgImg = zoomPanContainer.insert("g", '#topo-G');
Thomas Vachuska89543292014-11-19 11:28:33 -08002408 bgImg.attr('id', 'map').selectAll('path')
2409 .data(topoData.features)
2410 .enter()
2411 .append('path')
2412 .attr('d', path);
Simon Hunt195cb382014-11-03 17:50:51 -08002413 }
2414
Simon Huntf67722a2014-11-10 09:32:06 -08002415 function resize(view, ctx, flags) {
Simon Hunt12ce12e2014-11-15 21:13:19 -08002416 var w = view.width(),
2417 h = view.height();
2418
Simon Hunt934c3ce2014-11-05 11:45:07 -08002419 setSize(svg, view);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002420
2421 d3.select('#mask-bird').attr({ width: w, height: h})
2422 .select('g').attr('transform', birdTranslate(w, h));
Simon Hunt142d0032014-11-04 20:13:09 -08002423 }
2424
Simon Hunt12ce12e2014-11-15 21:13:19 -08002425 function birdTranslate(w, h) {
2426 var bdim = config.birdDim;
2427 return 'translate('+((w-bdim)*.4)+','+((h-bdim)*.1)+')';
2428 }
Simon Hunt142d0032014-11-04 20:13:09 -08002429
2430 // ==============================
2431 // View registration
Simon Hunt195cb382014-11-03 17:50:51 -08002432
Simon Hunt25248912014-11-04 11:25:48 -08002433 onos.ui.addView('topo', {
Simon Hunt142d0032014-11-04 20:13:09 -08002434 preload: preload,
2435 load: load,
Simon Hunta255a2c2014-11-13 22:29:35 -08002436 unload: unload,
Simon Hunt142d0032014-11-04 20:13:09 -08002437 resize: resize
Simon Hunt195cb382014-11-03 17:50:51 -08002438 });
2439
Simon Hunt61d04042014-11-11 17:27:16 -08002440 detailPane = onos.ui.addFloatingPanel('topo-detail');
Simon Hunta5e89142014-11-14 07:00:33 -08002441 oiBox = onos.ui.addFloatingPanel('topo-oibox', 'TL');
Simon Hunt61d04042014-11-11 17:27:16 -08002442
Simon Hunt195cb382014-11-03 17:50:51 -08002443}(ONOS));