blob: aed6087ee011b89078e4ea2060c8f2b3757e1da8 [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 Vachuska3266abf2014-11-13 09:28:46 -0800744 var paths = data.payload.paths;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800745
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800746 // Revert any links hilighted previously.
Thomas Vachuskaa3148a72014-11-19 21:38:35 -0800747 link.attr('stroke-width', null)
748 .style('stroke-width', null)
749 .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
Thomas Vachuskaa3148a72014-11-19 21:38:35 -0800753 if (paths.length && !antTimer) {
754 startAntTimer();
755 } else if (!paths.length && antTimer) {
756 stopAntTimer();
757 }
758
Simon Hunte2575b62014-11-18 15:25:53 -0800759 // Now hilight all links in the paths payload, and attach
760 // labels to them, if they are defined.
Simon Hunta255a2c2014-11-13 22:29:35 -0800761 paths.forEach(function (p) {
Simon Hunte2575b62014-11-18 15:25:53 -0800762 var n = p.links.length,
763 i,
764 ldata;
765
766 for (i=0; i<n; i++) {
767 ldata = findLinkById(p.links[i]);
768 if (ldata) {
769 ldata.el.classed(p.class, true);
770 ldata.label = p.labels[i];
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800771 }
Simon Hunte2575b62014-11-18 15:25:53 -0800772 }
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800773 });
Simon Hunte2575b62014-11-18 15:25:53 -0800774 updateLinks();
Simon Huntb53e0682014-11-12 13:32:01 -0800775 }
776
Simon Hunt56d51852014-11-09 13:03:35 -0800777 // ...............................
778
779 function stillToImplement(data) {
780 var p = data.payload;
781 note(data.event, p.id);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800782 network.view.alert('Not yet implemented: "' + data.event + '"');
Simon Hunt56d51852014-11-09 13:03:35 -0800783 }
Simon Hunt99c13842014-11-06 18:23:12 -0800784
785 function unknownEvent(data) {
Simon Hunt50128c02014-11-08 13:36:15 -0800786 network.view.alert('Unknown event type: "' + data.event + '"');
Simon Hunt99c13842014-11-06 18:23:12 -0800787 }
788
789 function handleServerEvent(data) {
790 var fn = eventDispatch[data.event] || unknownEvent;
791 fn(data);
792 }
793
794 // ==============================
Simon Hunt61d04042014-11-11 17:27:16 -0800795 // Out-going messages...
796
Simon Huntb53e0682014-11-12 13:32:01 -0800797 function userFeedback(msg) {
798 // for now, use the alert pane as is. Maybe different alert style in
799 // the future (centered on view; dismiss button?)
800 network.view.alert(msg);
801 }
802
803 function nSel() {
804 return selectOrder.length;
805 }
Simon Hunt61d04042014-11-11 17:27:16 -0800806 function getSel(idx) {
807 return selections[selectOrder[idx]];
808 }
Simon Huntb53e0682014-11-12 13:32:01 -0800809 function getSelId(idx) {
810 return getSel(idx).obj.id;
811 }
812 function allSelectionsClass(cls) {
813 for (var i=0, n=nSel(); i<n; i++) {
814 if (getSel(i).obj.class !== cls) {
815 return false;
816 }
817 }
818 return true;
819 }
Simon Hunt61d04042014-11-11 17:27:16 -0800820
Simon Hunt61d04042014-11-11 17:27:16 -0800821 // request details for the selected element
Simon Huntd72bc702014-11-13 18:38:04 -0800822 // invoked from selection of a single node.
Simon Hunt61d04042014-11-11 17:27:16 -0800823 function requestDetails() {
824 var data = getSel(0).obj,
825 payload = {
826 id: data.id,
827 class: data.class
828 };
829 sendMessage('requestDetails', payload);
830 }
831
Simon Huntd72bc702014-11-13 18:38:04 -0800832 function addIntentAction() {
833 sendMessage('addHostIntent', {
834 one: getSelId(0),
Thomas Vachuska82f2c622014-11-17 12:23:18 -0800835 two: getSelId(1),
836 ids: [ getSelId(0), getSelId(1) ]
Simon Huntd72bc702014-11-13 18:38:04 -0800837 });
838 }
839
840 function showTrafficAction() {
Thomas Vachuska29617e52014-11-20 03:17:46 -0800841 // force intents hover mode
842 hoverMode = 1;
843 showSelectTraffic();
844 }
845
846 function showSelectTraffic() {
Simon Huntd72bc702014-11-13 18:38:04 -0800847 // if nothing is hovered over, and nothing selected, send cancel request
848 if (!hovered && nSel() === 0) {
849 sendMessage('cancelTraffic', {});
850 return;
851 }
852
853 // NOTE: hover is only populated if "show traffic on hover" is
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800854 // toggled on, and the item hovered is a host or a device...
855 var hoverId = (trafficHover() && hovered &&
856 (hovered.class === 'host' || hovered.class === 'device'))
Simon Huntd72bc702014-11-13 18:38:04 -0800857 ? hovered.id : '';
858 sendMessage('requestTraffic', {
859 ids: selectOrder,
860 hover: hoverId
861 });
862 }
863
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800864 function showAllTrafficAction() {
865 sendMessage('requestAllTraffic', {});
866 }
867
Thomas Vachuska29617e52014-11-20 03:17:46 -0800868 function showDeviceLinkFlowsAction() {
869 // force intents hover mode
870 hoverMode = 2;
871 showDeviceLinkFlows();
872 }
873
874 function showDeviceLinkFlows() {
875 // if nothing is hovered over, and nothing selected, send cancel request
876 if (!hovered && nSel() === 0) {
877 sendMessage('cancelTraffic', {});
878 return;
879 }
880 var hoverId = (flowsHover() && hovered && hovered.class === 'device') ?
881 hovered.id : '';
882 sendMessage('requestDeviceLinkFlows', {
883 ids: selectOrder,
884 hover: hoverId
885 });
886 }
Simon Huntd72bc702014-11-13 18:38:04 -0800887
Simon Hunt95908012014-11-20 10:20:26 -0800888 function stripPx(s) {
889 return s.replace(/px$/,'');
890 }
Simon Hunt61d04042014-11-11 17:27:16 -0800891 // ==============================
Simon Hunta5e89142014-11-14 07:00:33 -0800892 // onos instance panel functions
893
894 function updateInstances() {
895 var onoses = oiBox.el.selectAll('.onosInst')
896 .data(onosOrder, function (d) { return d.id; });
897
898 // operate on existing onoses if necessary
Simon Huntfcfb46c2014-11-19 12:53:38 -0800899 onoses.classed('online', function (d) { return d.online; });
Simon Hunta5e89142014-11-14 07:00:33 -0800900
901 var entering = onoses.enter()
902 .append('div')
903 .attr('class', 'onosInst')
904 .classed('online', function (d) { return d.online; })
Simon Hunt9c15eca2014-11-15 18:37:59 -0800905 .on('click', clickInst);
906
907 entering.each(function (d, i) {
908 var el = d3.select(this),
909 img;
Simon Hunt95908012014-11-20 10:20:26 -0800910 var css = window.getComputedStyle(this),
911 w = stripPx(css.width),
912 h = stripPx(css.height) / 2;
Simon Hunt9c15eca2014-11-15 18:37:59 -0800913
Simon Hunt95908012014-11-20 10:20:26 -0800914 var svg = el.append('svg').attr({
915 width: w,
916 height: h
917 });
918 var dim = 30;
919 svg.append('use').attr({
920 class: 'glyphIcon',
921 transform: translate(2,2),
922 'xlink:href': '#node',
923 width: dim,
924 height: dim
925
926 });
927
928 //$('<img src="img/node.png">').appendTo(el);
929 //img = el.select('img')
930 // .attr({
931 // width: 30
932 // });
Simon Hunt9c15eca2014-11-15 18:37:59 -0800933
934 $('<div>').attr('class', 'onosTitle').text(d.id).appendTo(el);
935
936 // is the UI attached to this instance?
937 // TODO: need uiAttached boolean in instance data
938 //if (d.uiAttached) {
939 if (i === 0) {
940 $('<img src="img/ui.png">').attr('class','ui').appendTo(el);
941 }
942 });
Simon Hunta5e89142014-11-14 07:00:33 -0800943
944 // operate on existing + new onoses here
945
946 // the departed...
947 var exiting = onoses.exit()
948 .transition()
949 .style('opacity', 0)
950 .remove();
951 }
952
Simon Hunt9462e8c2014-11-14 17:28:09 -0800953 function clickInst(d) {
954 var el = d3.select(this),
955 aff = el.classed('affinity');
956 if (!aff) {
957 setAffinity(el, d);
958 } else {
959 cancelAffinity();
960 }
961 }
962
963 function setAffinity(el, d) {
964 d3.selectAll('.onosInst')
965 .classed('mastership', true)
966 .classed('affinity', false);
967 el.classed('affinity', true);
968
969 suppressLayers(true);
970 node.each(function (n) {
971 if (n.master === d.id) {
972 n.el.classed('suppressed', false);
973 }
974 });
975 oiShowMaster = true;
976 }
977
978 function cancelAffinity() {
979 d3.selectAll('.onosInst')
980 .classed('mastership affinity', false);
981 restoreLayerState();
982 oiShowMaster = false;
983 }
984
Simon Hunta5e89142014-11-14 07:00:33 -0800985 // ==============================
Simon Hunt99c13842014-11-06 18:23:12 -0800986 // force layout modification functions
987
988 function translate(x, y) {
989 return 'translate(' + x + ',' + y + ')';
990 }
991
Simon Hunte2575b62014-11-18 15:25:53 -0800992 function rotate(deg) {
993 return 'rotate(' + deg + ')';
994 }
995
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800996 function missMsg(what, id) {
997 return '\n[' + what + '] "' + id + '" missing ';
998 }
999
1000 function linkEndPoints(srcId, dstId) {
1001 var srcNode = network.lookup[srcId],
1002 dstNode = network.lookup[dstId],
1003 sMiss = !srcNode ? missMsg('src', srcId) : '',
1004 dMiss = !dstNode ? missMsg('dst', dstId) : '';
1005
1006 if (sMiss || dMiss) {
1007 logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
1008 return null;
1009 }
1010 return {
1011 source: srcNode,
1012 target: dstNode,
1013 x1: srcNode.x,
1014 y1: srcNode.y,
1015 x2: dstNode.x,
1016 y2: dstNode.y
1017 };
1018 }
1019
Simon Hunt56d51852014-11-09 13:03:35 -08001020 function createHostLink(host) {
1021 var src = host.id,
1022 dst = host.cp.device,
Simon Hunt7cd48f32014-11-09 23:42:50 -08001023 id = host.ingress,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001024 lnk = linkEndPoints(src, dst);
Simon Hunt56d51852014-11-09 13:03:35 -08001025
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001026 if (!lnk) {
Simon Hunt56d51852014-11-09 13:03:35 -08001027 return null;
1028 }
1029
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001030 // Synthesize link ...
1031 $.extend(lnk, {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001032 key: id,
Simon Hunt56d51852014-11-09 13:03:35 -08001033 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -08001034
1035 type: function () { return 'hostLink'; },
1036 // TODO: ideally, we should see if our edge switch is online...
1037 online: function () { return true; },
1038 linkWidth: function () { return 1; }
Simon Hunt7cd48f32014-11-09 23:42:50 -08001039 });
Simon Hunt99c13842014-11-06 18:23:12 -08001040 return lnk;
1041 }
1042
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001043 function createLink(link) {
1044 var lnk = linkEndPoints(link.src, link.dst),
1045 type = link.type;
1046
1047 if (!lnk) {
1048 return null;
1049 }
1050
Simon Hunt8257f4c2014-11-16 19:34:54 -08001051 $.extend(lnk, {
1052 key: link.id,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001053 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -08001054 fromSource: link,
1055
1056 // functions to aggregate dual link state
1057 type: function () {
1058 var s = lnk.fromSource,
1059 t = lnk.fromTarget;
1060 return (s && s.type) || (t && t.type) || defaultLinkType;
1061 },
1062 online: function () {
1063 var s = lnk.fromSource,
1064 t = lnk.fromTarget;
1065 return (s && s.online) || (t && t.online);
1066 },
1067 linkWidth: function () {
1068 var s = lnk.fromSource,
1069 t = lnk.fromTarget,
1070 ws = (s && s.linkWidth) || 0,
1071 wt = (t && t.linkWidth) || 0;
1072 return Math.max(ws, wt);
1073 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001074 });
1075 return lnk;
Simon Hunt1a9eff92014-11-07 11:06:34 -08001076 }
1077
Simon Hunte2575b62014-11-18 15:25:53 -08001078 function removeLinkLabels() {
1079 network.links.forEach(function (d) {
1080 d.label = '';
1081 });
1082 }
1083
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001084 var widthRatio = 1.4,
1085 linkScale = d3.scale.linear()
1086 .domain([1, 12])
1087 .range([widthRatio, 12 * widthRatio])
1088 .clamp(true);
1089
Simon Hunt99c13842014-11-06 18:23:12 -08001090 function updateLinks() {
1091 link = linkG.selectAll('.link')
Simon Hunt8257f4c2014-11-16 19:34:54 -08001092 .data(network.links, function (d) { return d.key; });
Simon Hunt99c13842014-11-06 18:23:12 -08001093
1094 // operate on existing links, if necessary
1095 // link .foo() .bar() ...
1096
1097 // operate on entering links:
1098 var entering = link.enter()
1099 .append('line')
1100 .attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001101 x1: function (d) { return d.x1; },
1102 y1: function (d) { return d.y1; },
1103 x2: function (d) { return d.x2; },
1104 y2: function (d) { return d.y2; },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001105 stroke: config.topo.linkInColor,
1106 'stroke-width': config.topo.linkInWidth
Simon Hunt99c13842014-11-06 18:23:12 -08001107 });
1108
1109 // augment links
Simon Hunt7cd48f32014-11-09 23:42:50 -08001110 entering.each(function (d) {
1111 var link = d3.select(this);
1112 // provide ref to element selection from backing data....
1113 d.el = link;
Simon Hunt8257f4c2014-11-16 19:34:54 -08001114 restyleLinkElement(d);
Simon Hunt7cd48f32014-11-09 23:42:50 -08001115 });
Thomas Vachuska4830d392014-11-09 17:09:56 -08001116
1117 // operate on both existing and new links, if necessary
1118 //link .foo() .bar() ...
1119
Simon Hunte2575b62014-11-18 15:25:53 -08001120 // apply or remove labels
1121 var labelData = getLabelData();
1122 applyLinkLabels(labelData);
1123
Thomas Vachuska4830d392014-11-09 17:09:56 -08001124 // operate on exiting links:
Thomas Vachuska4830d392014-11-09 17:09:56 -08001125 link.exit()
Simon Hunt13bf9c82014-11-18 07:26:44 -08001126 .attr('stroke-dasharray', '3, 3')
1127 .style('opacity', 0.5)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001128 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -08001129 .duration(1500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001130 .attr({
Simon Hunt13bf9c82014-11-18 07:26:44 -08001131 'stroke-dasharray': '3, 12',
1132 stroke: config.topo.linkOutColor,
1133 'stroke-width': config.topo.linkOutWidth
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001134 })
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001135 .style('opacity', 0.0)
Thomas Vachuska4830d392014-11-09 17:09:56 -08001136 .remove();
Simon Hunte2575b62014-11-18 15:25:53 -08001137
1138 // NOTE: invoke a single tick to force the labels to position
1139 // onto their links.
1140 tick();
1141 }
1142
1143 function getLabelData() {
1144 // create the backing data for showing labels..
1145 var data = [];
1146 link.each(function (d) {
1147 if (d.label) {
1148 data.push({
1149 id: 'lab-' + d.key,
1150 key: d.key,
1151 label: d.label,
1152 ldata: d
1153 });
1154 }
1155 });
1156 return data;
1157 }
1158
1159 var linkLabelOffset = '0.3em';
1160
1161 function applyLinkLabels(data) {
1162 var entering;
1163
1164 linkLabel = linkLabelG.selectAll('.linkLabel')
1165 .data(data, function (d) { return d.id; });
1166
Simon Hunt56a2ea42014-11-19 12:39:31 -08001167 // for elements already existing, we need to update the text
1168 // and adjust the rectangle size to fit
1169 linkLabel.each(function (d) {
1170 var el = d3.select(this),
1171 rect = el.select('rect'),
1172 text = el.select('text');
1173 text.text(d.label);
1174 rect.attr(rectAroundText(el));
1175 });
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -08001176
Simon Hunte2575b62014-11-18 15:25:53 -08001177 entering = linkLabel.enter().append('g')
1178 .classed('linkLabel', true)
1179 .attr('id', function (d) { return d.id; });
1180
1181 entering.each(function (d) {
1182 var el = d3.select(this),
1183 rect,
1184 text,
1185 parms = {
1186 x1: d.ldata.x1,
1187 y1: d.ldata.y1,
1188 x2: d.ldata.x2,
1189 y2: d.ldata.y2
1190 };
1191
1192 d.el = el;
1193 rect = el.append('rect');
1194 text = el.append('text').text(d.label);
1195 rect.attr(rectAroundText(el));
1196 text.attr('dy', linkLabelOffset);
1197
1198 el.attr('transform', transformLabel(parms));
1199 });
1200
1201 // Remove any links that are no longer required.
1202 linkLabel.exit().remove();
1203 }
1204
1205 function rectAroundText(el) {
1206 var text = el.select('text'),
1207 box = text.node().getBBox();
1208
1209 // translate the bbox so that it is centered on [x,y]
1210 box.x = -box.width / 2;
1211 box.y = -box.height / 2;
1212
1213 // add padding
1214 box.x -= 1;
1215 box.width += 2;
1216 return box;
1217 }
1218
1219 function transformLabel(p) {
1220 var dx = p.x2 - p.x1,
1221 dy = p.y2 - p.y1,
1222 xMid = dx/2 + p.x1,
1223 yMid = dy/2 + p.y1;
1224 //length = Math.sqrt(dx*dx + dy*dy),
1225 //rads = Math.asin(dy/length),
1226 //degs = rads / (Math.PI*2) * 360;
1227
1228 return translate(xMid, yMid);
1229
1230 // TODO: consider making label parallel to line
1231 //return [
1232 // translate(xMid, yMid),
1233 // rotate(degs),
1234 // translate(0, 8)
1235 //].join('');
Simon Hunt99c13842014-11-06 18:23:12 -08001236 }
1237
1238 function createDeviceNode(device) {
1239 // start with the object as is
1240 var node = device,
Simon Huntbb282f52014-11-10 11:08:19 -08001241 type = device.type,
Simon Huntc72967b2014-11-20 09:21:42 -08001242 svgCls = type ? 'node device ' + type : 'node device',
1243 labels = device.labels || [];
1244
1245 labels.unshift(''); // add 'no-label' to front of cycle
Simon Hunt99c13842014-11-06 18:23:12 -08001246
1247 // Augment as needed...
1248 node.class = 'device';
Simon Huntbb282f52014-11-10 11:08:19 -08001249 node.svgClass = device.online ? svgCls + ' online' : svgCls;
Simon Hunt99c13842014-11-06 18:23:12 -08001250 positionNode(node);
1251
1252 // cache label array length
Simon Huntc72967b2014-11-20 09:21:42 -08001253 // TODO: need a uiConfig event from the server to set things
1254 // like device labels count, host labels count, etc.
1255 // The current method (here) is a little fragile
Simon Hunt99c13842014-11-06 18:23:12 -08001256 network.deviceLabelCount = device.labels.length;
Simon Hunt99c13842014-11-06 18:23:12 -08001257 return node;
1258 }
1259
Simon Hunt56d51852014-11-09 13:03:35 -08001260 function createHostNode(host) {
1261 // start with the object as is
1262 var node = host;
1263
1264 // Augment as needed...
1265 node.class = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001266 if (!node.type) {
Simon Hunteb1514d2014-11-20 09:57:29 -08001267 node.type = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001268 }
Simon Hunt7fa116d2014-11-17 14:16:55 -08001269 node.svgClass = 'node host ' + node.type;
Simon Hunt56d51852014-11-09 13:03:35 -08001270 positionNode(node);
1271
1272 // cache label array length
1273 network.hostLabelCount = host.labels.length;
Simon Hunt56d51852014-11-09 13:03:35 -08001274 return node;
1275 }
1276
Simon Hunt99c13842014-11-06 18:23:12 -08001277 function positionNode(node) {
1278 var meta = node.metaUi,
Simon Huntac9e24f2014-11-12 10:12:21 -08001279 x = meta && meta.x,
1280 y = meta && meta.y,
1281 xy;
Simon Hunt99c13842014-11-06 18:23:12 -08001282
Simon Huntac9e24f2014-11-12 10:12:21 -08001283 // If we have [x,y] already, use that...
Simon Hunt99c13842014-11-06 18:23:12 -08001284 if (x && y) {
1285 node.fixed = true;
Simon Huntac9e24f2014-11-12 10:12:21 -08001286 node.x = x;
1287 node.y = y;
1288 return;
Simon Hunt99c13842014-11-06 18:23:12 -08001289 }
Simon Huntac9e24f2014-11-12 10:12:21 -08001290
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001291 var location = node.location;
1292 if (location && location.type === 'latlng') {
1293 var coord = geoMapProjection([location.lng, location.lat]);
1294 node.fixed = true;
1295 node.x = coord[0];
1296 node.y = coord[1];
1297 return;
1298 }
1299
Simon Huntac9e24f2014-11-12 10:12:21 -08001300 // Note: Placing incoming unpinned nodes at exactly the same point
1301 // (center of the view) causes them to explode outwards when
1302 // the force layout kicks in. So, we spread them out a bit
1303 // initially, to provide a more serene layout convergence.
1304 // Additionally, if the node is a host, we place it near
1305 // the device it is connected to.
1306
1307 function spread(s) {
1308 return Math.floor((Math.random() * s) - s/2);
1309 }
1310
1311 function randDim(dim) {
1312 return dim / 2 + spread(dim * 0.7071);
1313 }
1314
1315 function rand() {
1316 return {
1317 x: randDim(network.view.width()),
1318 y: randDim(network.view.height())
1319 };
1320 }
1321
1322 function near(node) {
1323 var min = 12,
1324 dx = spread(12),
1325 dy = spread(12);
1326 return {
1327 x: node.x + min + dx,
1328 y: node.y + min + dy
1329 };
1330 }
1331
1332 function getDevice(cp) {
1333 var d = network.lookup[cp.device];
1334 return d || rand();
1335 }
1336
1337 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
1338 $.extend(node, xy);
Simon Hunt99c13842014-11-06 18:23:12 -08001339 }
1340
Thomas Vachuska89543292014-11-19 11:28:33 -08001341 function iconUrl(d) {
1342 return 'img/' + d.type + '.png';
Simon Hunt99c13842014-11-06 18:23:12 -08001343 }
1344
Simon Huntc72967b2014-11-20 09:21:42 -08001345 function iconGlyphUrl(d) {
1346 var which = d.type || 'unknown';
1347 return '#' + which;
1348 }
1349
Simon Hunt99c13842014-11-06 18:23:12 -08001350 // returns the newly computed bounding box of the rectangle
1351 function adjustRectToFitText(n) {
1352 var text = n.select('text'),
1353 box = text.node().getBBox(),
1354 lab = config.labels;
1355
1356 text.attr('text-anchor', 'middle')
1357 .attr('y', '-0.8em')
1358 .attr('x', lab.imgPad/2);
1359
1360 // translate the bbox so that it is centered on [x,y]
1361 box.x = -box.width / 2;
1362 box.y = -box.height / 2;
1363
1364 // add padding
1365 box.x -= (lab.padLR + lab.imgPad/2);
1366 box.width += lab.padLR * 2 + lab.imgPad;
1367 box.y -= lab.padTB;
1368 box.height += lab.padTB * 2;
1369
1370 return box;
1371 }
1372
Simon Hunt1a9eff92014-11-07 11:06:34 -08001373 function mkSvgClass(d) {
1374 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
1375 }
1376
Simon Hunt7cd48f32014-11-09 23:42:50 -08001377 function hostLabel(d) {
1378 var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0;
1379 return d.labels[idx];
1380 }
1381 function deviceLabel(d) {
1382 var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0;
1383 return d.labels[idx];
1384 }
Simon Huntc72967b2014-11-20 09:21:42 -08001385 function trimLabel(label) {
1386 return (label && label.trim()) || '';
1387 }
1388
1389 function emptyBox() {
1390 return {
1391 x: -2,
1392 y: -2,
1393 width: 4,
1394 height: 4
1395 };
Simon Hunt7cd48f32014-11-09 23:42:50 -08001396 }
1397
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001398 function updateDeviceLabel(d) {
Simon Huntc72967b2014-11-20 09:21:42 -08001399 var label = trimLabel(deviceLabel(d)),
1400 noLabel = !label,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001401 node = d.el,
Simon Huntc72967b2014-11-20 09:21:42 -08001402 box,
1403 dx,
1404 dy;
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001405
1406 node.select('text')
1407 .text(label)
1408 .style('opacity', 0)
1409 .transition()
1410 .style('opacity', 1);
1411
Simon Huntc72967b2014-11-20 09:21:42 -08001412 if (noLabel) {
1413 box = emptyBox();
1414 dx = -config.icons.device.dim/2;
1415 dy = -config.icons.device.dim/2;
1416 } else {
1417 box = adjustRectToFitText(node);
1418 dx = box.x + config.icons.xoff;
1419 dy = box.y + config.icons.yoff;
1420 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001421
1422 node.select('rect')
1423 .transition()
1424 .attr(box);
1425
Simon Huntc72967b2014-11-20 09:21:42 -08001426 node.select('g.deviceIcon')
Thomas Vachuska89543292014-11-19 11:28:33 -08001427 .transition()
Simon Huntc72967b2014-11-20 09:21:42 -08001428 .attr('transform', translate(dx, dy));
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001429 }
1430
1431 function updateHostLabel(d) {
1432 var label = hostLabel(d),
1433 host = d.el;
1434
1435 host.select('text').text(label);
1436 }
1437
Simon Huntbb282f52014-11-10 11:08:19 -08001438 function updateDeviceState(nodeData) {
1439 nodeData.el.classed('online', nodeData.online);
1440 updateDeviceLabel(nodeData);
1441 // TODO: review what else might need to be updated
1442 }
1443
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001444 function updateLinkState(linkData) {
1445 updateLinkWidth(linkData);
Thomas Vachuskabadb93f2014-11-15 23:51:17 -08001446 linkData.el.classed('inactive', !linkData.online);
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001447 // TODO: review what else might need to be updated
1448 // update label, if showing
1449 }
1450
Simon Huntbb282f52014-11-10 11:08:19 -08001451 function updateHostState(hostData) {
1452 updateHostLabel(hostData);
1453 // TODO: review what else might need to be updated
1454 }
1455
Simon Hunt6ac93f32014-11-13 12:17:27 -08001456 function nodeMouseOver(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001457 hovered = d;
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -08001458 if (trafficHover() && (d.class === 'host' || d.class === 'device')) {
Thomas Vachuska29617e52014-11-20 03:17:46 -08001459 showSelectTraffic();
1460 } else if (flowsHover() && (d.class === 'device')) {
1461 showDeviceLinkFlows();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001462 }
1463 }
1464
1465 function nodeMouseOut(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001466 hovered = null;
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -08001467 if (trafficHover() && (d.class === 'host' || d.class === 'device')) {
Thomas Vachuska29617e52014-11-20 03:17:46 -08001468 showSelectTraffic();
1469 } else if (flowsHover() && (d.class === 'device')) {
1470 showDeviceLinkFlows();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001471 }
1472 }
Simon Huntbb282f52014-11-10 11:08:19 -08001473
Simon Hunteb1514d2014-11-20 09:57:29 -08001474 function addHostIcon(node, radius, iid) {
Thomas Vachuska89543292014-11-19 11:28:33 -08001475 var dim = radius * 1.5,
1476 xlate = -dim / 2;
1477
Simon Hunteb1514d2014-11-20 09:57:29 -08001478 node.append('use').attr({
1479 class: 'glyphIcon hostIcon',
1480 transform: translate(xlate,xlate),
1481 'xlink:href': iid,
1482 width: dim,
1483 height: dim
1484 });
Thomas Vachuska89543292014-11-19 11:28:33 -08001485 }
1486
Simon Hunt99c13842014-11-06 18:23:12 -08001487 function updateNodes() {
1488 node = nodeG.selectAll('.node')
1489 .data(network.nodes, function (d) { return d.id; });
1490
Simon Huntc72967b2014-11-20 09:21:42 -08001491 // TODO: operate on existing nodes
Simon Hunt7cd48f32014-11-09 23:42:50 -08001492 // update host labels
Simon Hunt99c13842014-11-06 18:23:12 -08001493 //node .foo() .bar() ...
1494
1495 // operate on entering nodes:
1496 var entering = node.enter()
1497 .append('g')
1498 .attr({
1499 id: function (d) { return safeId(d.id); },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001500 class: mkSvgClass,
Simon Hunt99c13842014-11-06 18:23:12 -08001501 transform: function (d) { return translate(d.x, d.y); },
1502 opacity: 0
1503 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08001504 .call(network.drag)
Simon Hunt6ac93f32014-11-13 12:17:27 -08001505 .on('mouseover', nodeMouseOver)
1506 .on('mouseout', nodeMouseOut)
Simon Hunt99c13842014-11-06 18:23:12 -08001507 .transition()
1508 .attr('opacity', 1);
1509
1510 // augment device nodes...
1511 entering.filter('.device').each(function (d) {
1512 var node = d3.select(this),
Simon Huntc72967b2014-11-20 09:21:42 -08001513 label = trimLabel(deviceLabel(d)),
1514 noLabel = !label,
Simon Hunt99c13842014-11-06 18:23:12 -08001515 box;
1516
Simon Hunt7cd48f32014-11-09 23:42:50 -08001517 // provide ref to element from backing data....
1518 d.el = node;
1519
Simon Hunt99c13842014-11-06 18:23:12 -08001520 node.append('rect')
1521 .attr({
1522 'rx': 5,
1523 'ry': 5
1524 });
1525
1526 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001527 .text(label)
Simon Hunt99c13842014-11-06 18:23:12 -08001528 .attr('dy', '1.1em');
1529
1530 box = adjustRectToFitText(node);
1531
1532 node.select('rect')
1533 .attr(box);
1534
Simon Huntc72967b2014-11-20 09:21:42 -08001535 addDeviceIcon(node, box, noLabel, iconGlyphUrl(d));
Simon Hunt99c13842014-11-06 18:23:12 -08001536
Simon Huntc7ee0662014-11-05 16:44:37 -08001537 });
Simon Hunt934c3ce2014-11-05 11:45:07 -08001538
Thomas Vachuska89543292014-11-19 11:28:33 -08001539 // TODO: better place for this configuration state
1540 var defaultHostRadius = 9,
1541 hostRadius = {
1542 bgpSpeaker: 14,
1543 router: 14,
1544 host: 14
1545 },
Simon Hunteb1514d2014-11-20 09:57:29 -08001546 hostGlyphId = {
Thomas Vachuska89543292014-11-19 11:28:33 -08001547 bgpSpeaker: 'bgpSpeaker',
1548 router: 'router',
1549 host: 'host'
1550 };
1551
1552
Simon Hunt56d51852014-11-09 13:03:35 -08001553 // augment host nodes...
1554 entering.filter('.host').each(function (d) {
1555 var node = d3.select(this),
Thomas Vachuska89543292014-11-19 11:28:33 -08001556 r = hostRadius[d.type] || defaultHostRadius,
1557 textDy = r + 10,
Simon Hunteb1514d2014-11-20 09:57:29 -08001558 iid = iconGlyphUrl(d);
Simon Hunt56d51852014-11-09 13:03:35 -08001559
Simon Hunt7cd48f32014-11-09 23:42:50 -08001560 // provide ref to element from backing data....
1561 d.el = node;
1562
Thomas Vachuska89543292014-11-19 11:28:33 -08001563 node.append('circle')
1564 .attr('r', r);
Simon Hunt7fa116d2014-11-17 14:16:55 -08001565
Simon Hunteb1514d2014-11-20 09:57:29 -08001566 if (iid) {
1567 addHostIcon(node, r, iid);
Simon Hunt7fa116d2014-11-17 14:16:55 -08001568 }
Simon Hunt56d51852014-11-09 13:03:35 -08001569
Paul Greyson29cd58f2014-11-18 13:14:57 -08001570
Simon Hunt56d51852014-11-09 13:03:35 -08001571 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001572 .text(hostLabel)
Thomas Vachuska89543292014-11-19 11:28:33 -08001573 .attr('dy', textDy)
Simon Hunt7cd48f32014-11-09 23:42:50 -08001574 .attr('text-anchor', 'middle');
Simon Hunt56d51852014-11-09 13:03:35 -08001575
1576 // debug function to show the modelled x,y coordinates of nodes...
1577 if (debug('showNodeXY')) {
1578 node.select('circle').attr('fill-opacity', 0.5);
1579 node.append('circle')
1580 .attr({
1581 class: 'debug',
1582 cx: 0,
1583 cy: 0,
1584 r: '3px'
1585 });
1586 }
1587 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001588
Simon Hunt99c13842014-11-06 18:23:12 -08001589 // operate on both existing and new nodes, if necessary
1590 //node .foo() .bar() ...
Simon Huntc7ee0662014-11-05 16:44:37 -08001591
Simon Hunt99c13842014-11-06 18:23:12 -08001592 // operate on exiting nodes:
Simon Huntea80eb42014-11-11 13:46:57 -08001593 // Note that the node is removed after 2 seconds.
1594 // Sub element animations should be shorter than 2 seconds.
1595 var exiting = node.exit()
Simon Hunt44031102014-11-11 13:20:36 -08001596 .transition()
1597 .duration(2000)
Simon Huntea80eb42014-11-11 13:46:57 -08001598 .style('opacity', 0)
Simon Hunt99c13842014-11-06 18:23:12 -08001599 .remove();
Simon Huntea80eb42014-11-11 13:46:57 -08001600
1601 // host node exits....
1602 exiting.filter('.host').each(function (d) {
1603 var node = d3.select(this);
1604
1605 node.select('text')
1606 .style('opacity', 0.5)
1607 .transition()
1608 .duration(1000)
1609 .style('opacity', 0);
1610 // note, leave <g>.remove to remove this element
1611
Thomas Vachuska89543292014-11-19 11:28:33 -08001612 node.select('circle')
1613 .style('stroke-fill', '#555')
1614 .style('fill', '#888')
Simon Huntea80eb42014-11-11 13:46:57 -08001615 .style('opacity', 0.5)
1616 .transition()
1617 .duration(1500)
1618 .attr('r', 0);
1619 // note, leave <g>.remove to remove this element
1620
1621 });
1622
1623 // TODO: device node exits
Simon Huntc7ee0662014-11-05 16:44:37 -08001624 }
1625
Simon Huntc72967b2014-11-20 09:21:42 -08001626 function addDeviceIcon(node, box, noLabel, iid) {
1627 var cfg = config.icons.device,
1628 dx,
1629 dy,
1630 g;
1631
1632 if (noLabel) {
1633 box = emptyBox();
1634 dx = -cfg.dim/2;
1635 dy = -cfg.dim/2;
1636 } else {
1637 box = adjustRectToFitText(node);
1638 dx = box.x + config.icons.xoff;
1639 dy = box.y + config.icons.yoff;
1640 }
1641
Simon Hunteb1514d2014-11-20 09:57:29 -08001642 g = node.append('g')
1643 .attr('class', 'glyphIcon deviceIcon')
Simon Huntc72967b2014-11-20 09:21:42 -08001644 .attr('transform', translate(dx, dy));
1645
1646 g.append('rect').attr({
1647 x: 0,
1648 y: 0,
1649 rx: cfg.rx,
1650 width: cfg.dim,
1651 height: cfg.dim
1652 });
1653
1654 g.append('use').attr({
1655 'xlink:href': iid,
1656 width: cfg.dim,
1657 height: cfg.dim
1658 });
1659
1660/*
1661 if (icon) {
1662 node.append('rect')
1663 .attr({
1664 class: 'iconUnderlay',
1665 x: box.x + config.icons.xoff,
1666 y: box.y + config.icons.yoff,
1667 width: cfg.w,
1668 height: cfg.h,
1669 rx: 4
1670 }).style({
1671 stroke: '#000',
1672 fill: '#ddd'
1673 });
1674 node.append('svg:image')
1675 .attr({
1676 x: box.x + config.icons.xoff + 2,
1677 y: box.y + config.icons.yoff + 2,
1678 width: cfg.w - 4,
1679 height: cfg.h - 4,
1680 'xlink:href': icon
1681 });
1682 }
1683*/
1684 }
1685
1686
1687
Simon Hunt8257f4c2014-11-16 19:34:54 -08001688 function find(key, array) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001689 for (var idx = 0, n = array.length; idx < n; idx++) {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001690 if (array[idx].key === key) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001691 return idx;
1692 }
1693 }
1694 return -1;
1695 }
1696
1697 function removeLinkElement(linkData) {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001698 var idx = find(linkData.key, network.links),
1699 removed;
1700 if (idx >=0) {
1701 // remove from links array
1702 removed = network.links.splice(idx, 1);
1703 // remove from lookup cache
1704 delete network.lookup[removed[0].key];
1705 updateLinks();
1706 network.force.resume();
1707 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001708 }
Simon Huntc7ee0662014-11-05 16:44:37 -08001709
Simon Hunt44031102014-11-11 13:20:36 -08001710 function removeHostElement(hostData) {
1711 // first, remove associated hostLink...
1712 removeLinkElement(hostData.linkData);
1713
1714 // remove from lookup cache
1715 delete network.lookup[hostData.id];
1716 // remove from nodes array
1717 var idx = find(hostData.id, network.nodes);
1718 network.nodes.splice(idx, 1);
1719 // remove from SVG
1720 updateNodes();
1721 network.force.resume();
1722 }
1723
1724
Simon Huntc7ee0662014-11-05 16:44:37 -08001725 function tick() {
1726 node.attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001727 transform: function (d) { return translate(d.x, d.y); }
Simon Huntc7ee0662014-11-05 16:44:37 -08001728 });
1729
1730 link.attr({
1731 x1: function (d) { return d.source.x; },
1732 y1: function (d) { return d.source.y; },
1733 x2: function (d) { return d.target.x; },
1734 y2: function (d) { return d.target.y; }
1735 });
Simon Hunte2575b62014-11-18 15:25:53 -08001736
1737 linkLabel.each(function (d) {
1738 var el = d3.select(this);
1739 var lnk = findLinkById(d.key),
1740 parms = {
1741 x1: lnk.source.x,
1742 y1: lnk.source.y,
1743 x2: lnk.target.x,
1744 y2: lnk.target.y
1745 };
1746 el.attr('transform', transformLabel(parms));
1747 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001748 }
Simon Hunt934c3ce2014-11-05 11:45:07 -08001749
1750 // ==============================
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001751 // Web-Socket for live data
1752
1753 function webSockUrl() {
1754 return document.location.toString()
1755 .replace(/\#.*/, '')
1756 .replace('http://', 'ws://')
1757 .replace('https://', 'wss://')
1758 .replace('index2.html', config.webSockUrl);
1759 }
1760
1761 webSock = {
1762 ws : null,
1763
1764 connect : function() {
1765 webSock.ws = new WebSocket(webSockUrl());
1766
1767 webSock.ws.onopen = function() {
Simon Hunt0c6d4192014-11-12 12:07:10 -08001768 noWebSock(false);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001769 };
1770
1771 webSock.ws.onmessage = function(m) {
1772 if (m.data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001773 wsTraceRx(m.data);
Thomas Vachuskad472c6e2014-11-07 19:11:05 -08001774 handleServerEvent(JSON.parse(m.data));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001775 }
1776 };
1777
1778 webSock.ws.onclose = function(m) {
1779 webSock.ws = null;
Simon Hunt0c6d4192014-11-12 12:07:10 -08001780 noWebSock(true);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001781 };
1782 },
1783
1784 send : function(text) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001785 if (text != null) {
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001786 webSock._send(text);
1787 }
1788 },
1789
1790 _send : function(message) {
1791 if (webSock.ws) {
1792 webSock.ws.send(message);
Simon Hunta255a2c2014-11-13 22:29:35 -08001793 } else if (config.useLiveData) {
Simon Hunt56d51852014-11-09 13:03:35 -08001794 network.view.alert('no web socket open\n\n' + message);
Simon Hunta255a2c2014-11-13 22:29:35 -08001795 } else {
1796 console.log('WS Send: ' + JSON.stringify(message));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001797 }
1798 }
1799
1800 };
1801
Simon Hunt0c6d4192014-11-12 12:07:10 -08001802 function noWebSock(b) {
1803 mask.style('display',b ? 'block' : 'none');
1804 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001805
1806 function sendMessage(evType, payload) {
1807 var toSend = {
Simon Huntbb282f52014-11-10 11:08:19 -08001808 event: evType,
1809 sid: ++sid,
1810 payload: payload
1811 },
1812 asText = JSON.stringify(toSend);
1813 wsTraceTx(asText);
1814 webSock.send(asText);
Simon Huntc76ae892014-11-18 17:31:51 -08001815
1816 // Temporary measure for debugging UI behavior ...
1817 if (!config.useLiveData) {
1818 handleTestSend(toSend);
1819 }
Simon Huntbb282f52014-11-10 11:08:19 -08001820 }
1821
1822 function wsTraceTx(msg) {
1823 wsTrace('tx', msg);
1824 }
1825 function wsTraceRx(msg) {
1826 wsTrace('rx', msg);
1827 }
1828 function wsTrace(rxtx, msg) {
Simon Huntbb282f52014-11-10 11:08:19 -08001829 console.log('[' + rxtx + '] ' + msg);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001830 }
1831
Simon Huntc76ae892014-11-18 17:31:51 -08001832 // NOTE: Temporary hardcoded example for showing detail pane
1833 // while we fine-
1834 // Probably should not merge this change...
1835 function handleTestSend(msg) {
1836 if (msg.event === 'requestDetails') {
1837 showDetails({
1838 event: 'showDetails',
1839 sid: 1001,
1840 payload: {
1841 "id": "of:0000ffffffffff09",
1842 "type": "roadm",
1843 "propOrder": [
1844 "Name",
1845 "Vendor",
1846 "H/W Version",
1847 "S/W Version",
1848 "-",
1849 "Latitude",
1850 "Longitude",
1851 "Ports"
1852 ],
1853 "props": {
1854 "Name": null,
1855 "Vendor": "Linc",
1856 "H/W Version": "OE",
1857 "S/W Version": "?",
1858 "-": "",
1859 "Latitude": "40.8",
1860 "Longitude": "73.1",
1861 "Ports": "2"
1862 }
1863 }
1864 });
1865 }
1866 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001867
1868 // ==============================
1869 // Selection stuff
1870
1871 function selectObject(obj, el) {
1872 var n,
Simon Hunt01095ff2014-11-13 16:37:29 -08001873 srcEv = d3.event.sourceEvent,
1874 meta = srcEv.metaKey,
1875 shift = srcEv.shiftKey;
1876
Simon Huntdeab4322014-11-13 18:49:07 -08001877 if ((panZoom() && !meta) || (!panZoom() && meta)) {
Simon Hunt01095ff2014-11-13 16:37:29 -08001878 return;
1879 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001880
1881 if (el) {
1882 n = d3.select(el);
1883 } else {
1884 node.each(function(d) {
1885 if (d == obj) {
1886 n = d3.select(el = this);
1887 }
1888 });
1889 }
1890 if (!n) return;
1891
Simon Hunt01095ff2014-11-13 16:37:29 -08001892 if (shift && n.classed('selected')) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001893 deselectObject(obj.id);
Simon Hunt61d04042014-11-11 17:27:16 -08001894 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001895 return;
1896 }
1897
Simon Hunt01095ff2014-11-13 16:37:29 -08001898 if (!shift) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001899 deselectAll();
1900 }
1901
Simon Huntc31d5692014-11-12 13:27:18 -08001902 selections[obj.id] = { obj: obj, el: el };
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001903 selectOrder.push(obj.id);
1904
1905 n.classed('selected', true);
Simon Hunt61d04042014-11-11 17:27:16 -08001906 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001907 }
1908
1909 function deselectObject(id) {
Simon Huntc31d5692014-11-12 13:27:18 -08001910 var obj = selections[id],
1911 idx;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001912 if (obj) {
1913 d3.select(obj.el).classed('selected', false);
Simon Hunt61d04042014-11-11 17:27:16 -08001914 delete selections[id];
Simon Huntc31d5692014-11-12 13:27:18 -08001915 idx = $.inArray(id, selectOrder);
1916 if (idx >= 0) {
1917 selectOrder.splice(idx, 1);
1918 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001919 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001920 }
1921
1922 function deselectAll() {
1923 // deselect all nodes in the network...
1924 node.classed('selected', false);
1925 selections = {};
1926 selectOrder = [];
Simon Hunt61d04042014-11-11 17:27:16 -08001927 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001928 }
1929
Simon Hunt61d04042014-11-11 17:27:16 -08001930 // update the state of the detail pane, based on current selections
1931 function updateDetailPane() {
1932 var nSel = selectOrder.length;
1933 if (!nSel) {
1934 detailPane.hide();
Simon Huntd72bc702014-11-13 18:38:04 -08001935 showTrafficAction(); // sends cancelTraffic event
Simon Hunt61d04042014-11-11 17:27:16 -08001936 } else if (nSel === 1) {
1937 singleSelect();
1938 } else {
1939 multiSelect();
1940 }
1941 }
1942
1943 function singleSelect() {
1944 requestDetails();
Simon Huntb53e0682014-11-12 13:32:01 -08001945 // NOTE: detail pane will be shown from showDetails event callback
Simon Hunt61d04042014-11-11 17:27:16 -08001946 }
1947
1948 function multiSelect() {
Simon Huntb53e0682014-11-12 13:32:01 -08001949 populateMultiSelect();
Simon Huntb53e0682014-11-12 13:32:01 -08001950 }
1951
1952 function addSep(tbody) {
1953 var tr = tbody.append('tr');
1954 $('<hr>').appendTo(tr.append('td').attr('colspan', 2));
1955 }
1956
1957 function addProp(tbody, label, value) {
1958 var tr = tbody.append('tr');
1959
1960 tr.append('td')
1961 .attr('class', 'label')
1962 .text(label + ' :');
1963
1964 tr.append('td')
1965 .attr('class', 'value')
1966 .text(value);
1967 }
1968
1969 function populateMultiSelect() {
1970 detailPane.empty();
1971
1972 var title = detailPane.append("h2"),
1973 table = detailPane.append("table"),
1974 tbody = table.append("tbody");
1975
1976 title.text('Multi-Select...');
1977
1978 selectOrder.forEach(function (d, i) {
1979 addProp(tbody, i+1, d);
1980 });
Simon Huntd72bc702014-11-13 18:38:04 -08001981
1982 addMultiSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08001983 }
1984
1985 function populateDetails(data) {
1986 detailPane.empty();
1987
1988 var title = detailPane.append("h2"),
1989 table = detailPane.append("table"),
1990 tbody = table.append("tbody");
1991
1992 $('<img src="img/' + data.type + '.png">').appendTo(title);
1993 $('<span>').attr('class', 'icon').text(data.id).appendTo(title);
1994
1995 data.propOrder.forEach(function(p) {
1996 if (p === '-') {
1997 addSep(tbody);
1998 } else {
1999 addProp(tbody, p, data.props[p]);
2000 }
2001 });
Simon Huntd72bc702014-11-13 18:38:04 -08002002
2003 addSingleSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08002004 }
2005
Simon Huntd72bc702014-11-13 18:38:04 -08002006 function addSingleSelectActions() {
2007 detailPane.append('hr');
2008 // always want to allow 'show traffic'
Simon Hunta5e89142014-11-14 07:00:33 -08002009 addAction(detailPane, 'Show Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08002010 }
2011
2012 function addMultiSelectActions() {
2013 detailPane.append('hr');
2014 // always want to allow 'show traffic'
Simon Hunta5e89142014-11-14 07:00:33 -08002015 addAction(detailPane, 'Show Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08002016 // if exactly two hosts are selected, also want 'add host intent'
2017 if (nSel() === 2 && allSelectionsClass('host')) {
Simon Hunta5e89142014-11-14 07:00:33 -08002018 addAction(detailPane, 'Add Host Intent', addIntentAction);
Simon Huntd72bc702014-11-13 18:38:04 -08002019 }
2020 }
2021
Simon Hunta5e89142014-11-14 07:00:33 -08002022 function addAction(panel, text, cb) {
2023 panel.append('div')
Simon Huntd72bc702014-11-13 18:38:04 -08002024 .classed('actionBtn', true)
2025 .text(text)
2026 .on('click', cb);
2027 }
2028
2029
Paul Greysonfcba0e82014-11-13 10:21:16 -08002030 function zoomPan(scale, translate) {
2031 zoomPanContainer.attr("transform", "translate(" + translate + ")scale(" + scale + ")");
2032 // keep the map lines constant width while zooming
Thomas Vachuska89543292014-11-19 11:28:33 -08002033 bgImg.style("stroke-width", 2.0 / scale + "px");
Paul Greysonfcba0e82014-11-13 10:21:16 -08002034 }
2035
2036 function resetZoomPan() {
2037 zoomPan(1, [0,0]);
2038 zoom.scale(1).translate([0,0]);
2039 }
2040
2041 function setupZoomPan() {
2042 function zoomed() {
Simon Huntdeab4322014-11-13 18:49:07 -08002043 if (!panZoom() ^ !d3.event.sourceEvent.metaKey) {
Paul Greysonfcba0e82014-11-13 10:21:16 -08002044 zoomPan(d3.event.scale, d3.event.translate);
2045 }
2046 }
2047
2048 zoom = d3.behavior.zoom()
2049 .translate([0, 0])
2050 .scale(1)
2051 .scaleExtent([1, 8])
2052 .on("zoom", zoomed);
2053
2054 svg.call(zoom);
2055 }
2056
Simon Hunt61d04042014-11-11 17:27:16 -08002057 // ==============================
2058 // Test harness code
Simon Hunt56d51852014-11-09 13:03:35 -08002059
2060 function prepareScenario(view, ctx, dbg) {
2061 var sc = scenario,
2062 urlSc = sc.evDir + ctx + sc.evScenario;
2063
2064 if (!ctx) {
2065 view.alert("No scenario specified (null ctx)");
2066 return;
2067 }
2068
2069 sc.view = view;
2070 sc.ctx = ctx;
2071 sc.debug = dbg;
2072 sc.evNumber = 0;
2073
2074 d3.json(urlSc, function(err, data) {
Simon Huntbb282f52014-11-10 11:08:19 -08002075 var p = data && data.params || {},
2076 desc = data && data.description || null,
Simon Huntfc274c92014-11-11 11:05:46 -08002077 intro = data && data.title;
Simon Huntbb282f52014-11-10 11:08:19 -08002078
Simon Hunt56d51852014-11-09 13:03:35 -08002079 if (err) {
2080 view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err);
2081 } else {
2082 sc.params = p;
Simon Huntbb282f52014-11-10 11:08:19 -08002083 if (desc) {
2084 intro += '\n\n ' + desc.join('\n ');
2085 }
2086 view.alert(intro);
Simon Hunt56d51852014-11-09 13:03:35 -08002087 }
2088 });
2089
2090 }
2091
Simon Hunt01095ff2014-11-13 16:37:29 -08002092 // ==============================
2093 // Toggle Buttons in masthead
Simon Hunt0c6d4192014-11-12 12:07:10 -08002094
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002095 // TODO: toggle button (and other widgets in the masthead) should be provided
2096 // by the framework; not generated by the view.
2097
Simon Hunta5e89142014-11-14 07:00:33 -08002098 var showInstances,
2099 doPanZoom,
2100 showTrafficOnHover;
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002101
2102 function addButtonBar(view) {
2103 var bb = d3.select('#mast')
2104 .append('span').classed('right', true).attr('id', 'bb');
2105
Simon Hunta5e89142014-11-14 07:00:33 -08002106 function mkTogBtn(text, cb) {
2107 return bb.append('span')
2108 .classed('btn', true)
2109 .text(text)
2110 .on('click', cb);
2111 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002112
Simon Hunta5e89142014-11-14 07:00:33 -08002113 showInstances = mkTogBtn('Show Instances', toggleInst);
Simon Hunte5b71752014-11-18 20:06:07 -08002114 //doPanZoom = mkTogBtn('Pan/Zoom', togglePanZoom);
2115 //showTrafficOnHover = mkTogBtn('Show traffic on hover', toggleTrafficHover);
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002116 }
2117
Simon Hunta5e89142014-11-14 07:00:33 -08002118 function instShown() {
2119 return showInstances.classed('active');
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002120 }
Simon Hunta5e89142014-11-14 07:00:33 -08002121 function toggleInst() {
2122 showInstances.classed('active', !instShown());
2123 if (instShown()) {
2124 oiBox.show();
2125 } else {
2126 oiBox.hide();
2127 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002128 }
2129
Simon Huntdeab4322014-11-13 18:49:07 -08002130 function panZoom() {
Simon Hunte5b71752014-11-18 20:06:07 -08002131 return false;
2132 //return doPanZoom.classed('active');
Simon Hunt01095ff2014-11-13 16:37:29 -08002133 }
Simon Hunta5e89142014-11-14 07:00:33 -08002134 function togglePanZoom() {
2135 doPanZoom.classed('active', !panZoom());
2136 }
2137
2138 function trafficHover() {
Thomas Vachuska29617e52014-11-20 03:17:46 -08002139 return hoverModes[hoverMode] === 'intents';
Simon Hunta5e89142014-11-14 07:00:33 -08002140 }
Thomas Vachuska29617e52014-11-20 03:17:46 -08002141
2142 function flowsHover() {
2143 return hoverModes[hoverMode] === 'flows';
2144 }
2145
Simon Hunta5e89142014-11-14 07:00:33 -08002146 function toggleTrafficHover() {
2147 showTrafficOnHover.classed('active', !trafficHover());
2148 }
2149
Simon Hunt7fa116d2014-11-17 14:16:55 -08002150 function loadGlyphs(svg) {
2151 var defs = svg.append('defs');
2152 gly.defBird(defs);
2153 gly.defBullhorn(defs);
Simon Huntc72967b2014-11-20 09:21:42 -08002154 gly.defGlyphs(defs);
Simon Hunt7fa116d2014-11-17 14:16:55 -08002155 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002156
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002157 // ==============================
Simon Hunt142d0032014-11-04 20:13:09 -08002158 // View life-cycle callbacks
Simon Hunt195cb382014-11-03 17:50:51 -08002159
Simon Huntf67722a2014-11-10 09:32:06 -08002160 function preload(view, ctx, flags) {
Simon Hunt142d0032014-11-04 20:13:09 -08002161 var w = view.width(),
2162 h = view.height(),
Simon Huntc7ee0662014-11-05 16:44:37 -08002163 fcfg = config.force,
2164 fpad = fcfg.pad,
2165 forceDim = [w - 2*fpad, h - 2*fpad];
Simon Hunt195cb382014-11-03 17:50:51 -08002166
Simon Hunt142d0032014-11-04 20:13:09 -08002167 // NOTE: view.$div is a D3 selection of the view's div
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002168 var viewBox = '0 0 ' + config.logicalSize + ' ' + config.logicalSize;
2169 svg = view.$div.append('svg').attr('viewBox', viewBox);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002170 setSize(svg, view);
2171
Simon Hunt7fa116d2014-11-17 14:16:55 -08002172 loadGlyphs(svg);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002173
Paul Greysonfcba0e82014-11-13 10:21:16 -08002174 zoomPanContainer = svg.append('g').attr('id', 'zoomPanContainer');
Paul Greysonfcba0e82014-11-13 10:21:16 -08002175 setupZoomPan();
2176
Simon Hunt1a9eff92014-11-07 11:06:34 -08002177 // add blue glow filter to svg layer
Paul Greysonfcba0e82014-11-13 10:21:16 -08002178 d3u.appendGlow(zoomPanContainer);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002179
Simon Huntc7ee0662014-11-05 16:44:37 -08002180 // group for the topology
Paul Greysonfcba0e82014-11-13 10:21:16 -08002181 topoG = zoomPanContainer.append('g')
Simon Huntd3b7d512014-11-12 15:48:41 -08002182 .attr('id', 'topo-G')
Simon Huntc7ee0662014-11-05 16:44:37 -08002183 .attr('transform', fcfg.translate());
2184
Simon Hunte2575b62014-11-18 15:25:53 -08002185 // subgroups for links, link labels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002186 linkG = topoG.append('g').attr('id', 'links');
Simon Hunte2575b62014-11-18 15:25:53 -08002187 linkLabelG = topoG.append('g').attr('id', 'linkLabels');
Simon Huntc7ee0662014-11-05 16:44:37 -08002188 nodeG = topoG.append('g').attr('id', 'nodes');
2189
Simon Hunte2575b62014-11-18 15:25:53 -08002190 // selection of links, linkLabels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002191 link = linkG.selectAll('.link');
Simon Hunte2575b62014-11-18 15:25:53 -08002192 linkLabel = linkLabelG.selectAll('.linkLabel');
Simon Huntc7ee0662014-11-05 16:44:37 -08002193 node = nodeG.selectAll('.node');
2194
Simon Hunt7cd48f32014-11-09 23:42:50 -08002195 function chrg(d) {
2196 return fcfg.charge[d.class] || -12000;
2197 }
Simon Hunt99c13842014-11-06 18:23:12 -08002198 function ldist(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002199 return fcfg.linkDistance[d.type] || 50;
Simon Hunt99c13842014-11-06 18:23:12 -08002200 }
2201 function lstrg(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002202 // 0.0 - 1.0
2203 return fcfg.linkStrength[d.type] || 1.0;
Simon Hunt99c13842014-11-06 18:23:12 -08002204 }
2205
Simon Hunt1a9eff92014-11-07 11:06:34 -08002206 function selectCb(d, self) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002207 selectObject(d, self);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002208 }
2209
2210 function atDragEnd(d, self) {
Simon Hunt56d51852014-11-09 13:03:35 -08002211 // once we've finished moving, pin the node in position
2212 d.fixed = true;
2213 d3.select(self).classed('fixed', true);
2214 if (config.useLiveData) {
Simon Hunt902c9922014-11-11 11:59:31 -08002215 sendUpdateMeta(d);
Simon Hunta255a2c2014-11-13 22:29:35 -08002216 } else {
2217 console.log('Moving node ' + d.id + ' to [' + d.x + ',' + d.y + ']');
Simon Hunt1a9eff92014-11-07 11:06:34 -08002218 }
2219 }
2220
Simon Hunt902c9922014-11-11 11:59:31 -08002221 function sendUpdateMeta(d) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002222 sendMessage('updateMeta', {
2223 id: d.id,
2224 'class': d.class,
Simon Hunt902c9922014-11-11 11:59:31 -08002225 'memento': {
Simon Hunt01095ff2014-11-13 16:37:29 -08002226 x: d.x,
2227 y: d.y
Simon Hunt902c9922014-11-11 11:59:31 -08002228 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002229 });
2230 }
2231
Simon Huntc7ee0662014-11-05 16:44:37 -08002232 // set up the force layout
2233 network.force = d3.layout.force()
2234 .size(forceDim)
2235 .nodes(network.nodes)
2236 .links(network.links)
Simon Hunt7cd48f32014-11-09 23:42:50 -08002237 .gravity(0.4)
2238 .friction(0.7)
2239 .charge(chrg)
Simon Hunt99c13842014-11-06 18:23:12 -08002240 .linkDistance(ldist)
2241 .linkStrength(lstrg)
Simon Huntc7ee0662014-11-05 16:44:37 -08002242 .on('tick', tick);
Simon Hunt195cb382014-11-03 17:50:51 -08002243
Simon Hunt01095ff2014-11-13 16:37:29 -08002244 network.drag = d3u.createDragBehavior(network.force,
Simon Huntdeab4322014-11-13 18:49:07 -08002245 selectCb, atDragEnd, panZoom);
Simon Hunt0c6d4192014-11-12 12:07:10 -08002246
2247 // create mask layer for when we lose connection to server.
Simon Hunta5e89142014-11-14 07:00:33 -08002248 // TODO: this should be part of the framework
Simon Hunt0c6d4192014-11-12 12:07:10 -08002249 mask = view.$div.append('div').attr('id','topo-mask');
2250 para(mask, 'Oops!');
2251 para(mask, 'Web-socket connection to server closed...');
2252 para(mask, 'Try refreshing the page.');
Simon Hunt12ce12e2014-11-15 21:13:19 -08002253
2254 mask.append('svg')
2255 .attr({
2256 id: 'mask-bird',
2257 width: w,
2258 height: h
2259 })
2260 .append('g')
2261 .attr('transform', birdTranslate(w, h))
2262 .style('opacity', 0.3)
2263 .append('use')
2264 .attr({
2265 'xlink:href': '#bird',
2266 width: config.birdDim,
2267 height: config.birdDim,
2268 fill: '#111'
Thomas Vachuska89543292014-11-19 11:28:33 -08002269 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08002270 }
Simon Hunt195cb382014-11-03 17:50:51 -08002271
Simon Hunt01095ff2014-11-13 16:37:29 -08002272 function para(sel, text) {
2273 sel.append('p').text(text);
2274 }
2275
2276
Simon Hunt56d51852014-11-09 13:03:35 -08002277 function load(view, ctx, flags) {
Simon Huntf67722a2014-11-10 09:32:06 -08002278 // resize, in case the window was resized while we were not loaded
2279 resize(view, ctx, flags);
2280
Simon Hunt99c13842014-11-06 18:23:12 -08002281 // cache the view token, so network topo functions can access it
2282 network.view = view;
Simon Hunt56d51852014-11-09 13:03:35 -08002283 config.useLiveData = !flags.local;
2284
2285 if (!config.useLiveData) {
2286 prepareScenario(view, ctx, flags.debug);
2287 }
Simon Hunt99c13842014-11-06 18:23:12 -08002288
2289 // set our radio buttons and key bindings
Simon Hunt9462e8c2014-11-14 17:28:09 -08002290 layerBtnSet = view.setRadio(layerButtons);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002291 view.setKeys(keyDispatch);
Simon Hunt195cb382014-11-03 17:50:51 -08002292
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002293 // patch in our "button bar" for now
2294 // TODO: implement a more official frameworky way of doing this..
2295 addButtonBar(view);
2296
Simon Huntd3b7d512014-11-12 15:48:41 -08002297 // Load map data asynchronously; complete startup after that..
2298 loadGeoJsonData();
Simon Hunta255a2c2014-11-13 22:29:35 -08002299
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002300 //var dashIdx = 0;
2301 //antTimer = setInterval(function () {
2302 // // TODO: figure out how to choose Src-->Dst and Dst-->Src, per link
2303 // dashIdx = dashIdx === 0 ? 14 : dashIdx - 2;
2304 // d3.selectAll('.animated').style('stroke-dashoffset', dashIdx);
2305 //}, 35);
Simon Hunta255a2c2014-11-13 22:29:35 -08002306 }
2307
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002308 function startAntTimer() {
2309 var pulses = [ 5, 3, 1.2, 3 ],
2310 pulse = 0;
2311 antTimer = setInterval(function () {
2312 pulse = pulse + 1;
2313 pulse = pulse === pulses.length ? 0 : pulse;
2314 d3.selectAll('.animated').style('stroke-width', pulses[pulse]);
2315 }, 200);
2316 }
2317
2318 function stopAntTimer() {
Simon Hunta255a2c2014-11-13 22:29:35 -08002319 if (antTimer) {
2320 clearInterval(antTimer);
2321 antTimer = null;
2322 }
Simon Huntd3b7d512014-11-12 15:48:41 -08002323 }
2324
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002325 function unload(view, ctx, flags) {
2326 stopAntTimer();
2327 }
2328
Simon Huntd3b7d512014-11-12 15:48:41 -08002329 // TODO: move these to config/state portion of script
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002330 var geoJsonUrl = 'json/map/continental_us.json', // TODO: Paul
Simon Huntd3b7d512014-11-12 15:48:41 -08002331 geoJson;
2332
2333 function loadGeoJsonData() {
2334 d3.json(geoJsonUrl, function (err, data) {
2335 if (err) {
2336 // fall back to USA map background
2337 loadStaticMap();
2338 } else {
2339 geoJson = data;
2340 loadGeoMap();
2341 }
2342
2343 // finally, connect to the server...
2344 if (config.useLiveData) {
2345 webSock.connect();
2346 }
2347 });
2348 }
2349
2350 function showBg() {
2351 return config.options.showBackground ? 'visible' : 'hidden';
2352 }
2353
2354 function loadStaticMap() {
2355 fnTrace('loadStaticMap', config.backgroundUrl);
2356 var w = network.view.width(),
2357 h = network.view.height();
2358
2359 // load the background image
2360 bgImg = svg.insert('svg:image', '#topo-G')
2361 .attr({
2362 id: 'topo-bg',
2363 width: w,
2364 height: h,
2365 'xlink:href': config.backgroundUrl
2366 })
2367 .style({
2368 visibility: showBg()
2369 });
2370 }
2371
2372 function loadGeoMap() {
2373 fnTrace('loadGeoMap', geoJsonUrl);
Simon Huntd3b7d512014-11-12 15:48:41 -08002374
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002375 // extracts the topojson data into geocoordinate-based geometry
2376 var topoData = topojson.feature(geoJson, geoJson.objects.states);
Simon Huntd3b7d512014-11-12 15:48:41 -08002377
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002378 // see: http://bl.ocks.org/mbostock/4707858
2379 geoMapProjection = d3.geo.mercator();
2380 var path = d3.geo.path().projection(geoMapProjection);
Simon Huntd3b7d512014-11-12 15:48:41 -08002381
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002382 geoMapProjection
2383 .scale(1)
2384 .translate([0, 0]);
Simon Huntd3b7d512014-11-12 15:48:41 -08002385
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002386 // [[x1,y1],[x2,y2]]
2387 var b = path.bounds(topoData);
Paul Greysonfcba0e82014-11-13 10:21:16 -08002388 // size map to 95% of minimum dimension to fill space
2389 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 -08002390 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 -08002391
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002392 geoMapProjection
2393 .scale(s)
2394 .translate(t);
2395
Paul Greysonfcba0e82014-11-13 10:21:16 -08002396 bgImg = zoomPanContainer.insert("g", '#topo-G');
Thomas Vachuska89543292014-11-19 11:28:33 -08002397 bgImg.attr('id', 'map').selectAll('path')
2398 .data(topoData.features)
2399 .enter()
2400 .append('path')
2401 .attr('d', path);
Simon Hunt195cb382014-11-03 17:50:51 -08002402 }
2403
Simon Huntf67722a2014-11-10 09:32:06 -08002404 function resize(view, ctx, flags) {
Simon Hunt12ce12e2014-11-15 21:13:19 -08002405 var w = view.width(),
2406 h = view.height();
2407
Simon Hunt934c3ce2014-11-05 11:45:07 -08002408 setSize(svg, view);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002409
2410 d3.select('#mask-bird').attr({ width: w, height: h})
2411 .select('g').attr('transform', birdTranslate(w, h));
Simon Hunt142d0032014-11-04 20:13:09 -08002412 }
2413
Simon Hunt12ce12e2014-11-15 21:13:19 -08002414 function birdTranslate(w, h) {
2415 var bdim = config.birdDim;
2416 return 'translate('+((w-bdim)*.4)+','+((h-bdim)*.1)+')';
2417 }
Simon Hunt142d0032014-11-04 20:13:09 -08002418
2419 // ==============================
2420 // View registration
Simon Hunt195cb382014-11-03 17:50:51 -08002421
Simon Hunt25248912014-11-04 11:25:48 -08002422 onos.ui.addView('topo', {
Simon Hunt142d0032014-11-04 20:13:09 -08002423 preload: preload,
2424 load: load,
Simon Hunta255a2c2014-11-13 22:29:35 -08002425 unload: unload,
Simon Hunt142d0032014-11-04 20:13:09 -08002426 resize: resize
Simon Hunt195cb382014-11-03 17:50:51 -08002427 });
2428
Simon Hunt61d04042014-11-11 17:27:16 -08002429 detailPane = onos.ui.addFloatingPanel('topo-detail');
Simon Hunta5e89142014-11-14 07:00:33 -08002430 oiBox = onos.ui.addFloatingPanel('topo-oibox', 'TL');
Simon Hunt61d04042014-11-11 17:27:16 -08002431
Simon Hunt195cb382014-11-03 17:50:51 -08002432}(ONOS));