blob: 1e3e88613533b54f5a0ca544adb40de5338b5625 [file] [log] [blame]
Simon Hunt195cb382014-11-03 17:50:51 -08001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
Simon Hunt142d0032014-11-04 20:13:09 -080018 ONOS network topology viewer - version 1.1
Simon Hunt195cb382014-11-03 17:50:51 -080019
20 @author Simon Hunt
21 */
22
23(function (onos) {
24 'use strict';
25
Simon Hunt1a9eff92014-11-07 11:06:34 -080026 // shorter names for library APIs
Simon Huntbb282f52014-11-10 11:08:19 -080027 var d3u = onos.lib.d3util,
Simon Hunt12ce12e2014-11-15 21:13:19 -080028 gly = onos.lib.glyphs,
Simon Huntbb282f52014-11-10 11:08:19 -080029 trace;
Simon Hunt1a9eff92014-11-07 11:06:34 -080030
Simon Hunt195cb382014-11-03 17:50:51 -080031 // configuration data
32 var config = {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -080033 useLiveData: true,
Simon Huntfc274c92014-11-11 11:05:46 -080034 fnTrace: true,
Simon Hunt195cb382014-11-03 17:50:51 -080035 debugOn: false,
36 debug: {
Simon Hunt99c13842014-11-06 18:23:12 -080037 showNodeXY: true,
38 showKeyHandler: false
Simon Hunt195cb382014-11-03 17:50:51 -080039 },
Simon Hunt12ce12e2014-11-15 21:13:19 -080040 birdDim: 400,
Simon Hunt195cb382014-11-03 17:50:51 -080041 options: {
42 layering: true,
43 collisionPrevention: true,
Simon Hunt142d0032014-11-04 20:13:09 -080044 showBackground: true
Simon Hunt195cb382014-11-03 17:50:51 -080045 },
46 backgroundUrl: 'img/us-map.png',
Thomas Vachuska7d638d32014-11-07 10:24:43 -080047 webSockUrl: 'ws/topology',
Simon Hunt195cb382014-11-03 17:50:51 -080048 data: {
49 live: {
50 jsonUrl: 'rs/topology/graph',
51 detailPrefix: 'rs/topology/graph/',
52 detailSuffix: ''
53 },
54 fake: {
55 jsonUrl: 'json/network2.json',
56 detailPrefix: 'json/',
57 detailSuffix: '.json'
58 }
59 },
Simon Hunt99c13842014-11-06 18:23:12 -080060 labels: {
61 imgPad: 16,
62 padLR: 4,
63 padTB: 3,
64 marginLR: 3,
65 marginTB: 2,
66 port: {
67 gap: 3,
68 width: 18,
69 height: 14
70 }
71 },
Simon Hunt1a9eff92014-11-07 11:06:34 -080072 topo: {
Thomas Vachuska89543292014-11-19 11:28:33 -080073 linkBaseColor: '#666',
Simon Hunt1a9eff92014-11-07 11:06:34 -080074 linkInColor: '#66f',
Paul Greyson29cd58f2014-11-18 13:14:57 -080075 linkInWidth: 14,
Thomas Vachuska89543292014-11-19 11:28:33 -080076 linkOutColor: '#f00',
Simon Hunt13bf9c82014-11-18 07:26:44 -080077 linkOutWidth: 30
Simon Hunt1a9eff92014-11-07 11:06:34 -080078 },
Paul Greyson29cd58f2014-11-18 13:14:57 -080079 icons: {
Thomas Vachuskaece59ee2014-11-19 19:06:11 -080080 w: 30,
81 h: 30,
82 xoff: -16,
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 Hunt61d04042014-11-11 17:27:16 -0800888 // ==============================
Simon Hunta5e89142014-11-14 07:00:33 -0800889 // onos instance panel functions
890
891 function updateInstances() {
892 var onoses = oiBox.el.selectAll('.onosInst')
893 .data(onosOrder, function (d) { return d.id; });
894
895 // operate on existing onoses if necessary
Simon Huntfcfb46c2014-11-19 12:53:38 -0800896 onoses.classed('online', function (d) { return d.online; });
Simon Hunta5e89142014-11-14 07:00:33 -0800897
898 var entering = onoses.enter()
899 .append('div')
900 .attr('class', 'onosInst')
901 .classed('online', function (d) { return d.online; })
Simon Hunt9c15eca2014-11-15 18:37:59 -0800902 .on('click', clickInst);
903
904 entering.each(function (d, i) {
905 var el = d3.select(this),
906 img;
907
Thomas Vachuskacd2920c2014-11-19 14:49:55 -0800908 $('<img src="img/node.png">').appendTo(el);
Simon Hunt9c15eca2014-11-15 18:37:59 -0800909 img = el.select('img')
910 .attr({
Simon Huntfcfb46c2014-11-19 12:53:38 -0800911 width: 30
Simon Hunt9c15eca2014-11-15 18:37:59 -0800912 });
913
914 $('<div>').attr('class', 'onosTitle').text(d.id).appendTo(el);
915
916 // is the UI attached to this instance?
917 // TODO: need uiAttached boolean in instance data
918 //if (d.uiAttached) {
919 if (i === 0) {
920 $('<img src="img/ui.png">').attr('class','ui').appendTo(el);
921 }
922 });
Simon Hunta5e89142014-11-14 07:00:33 -0800923
924 // operate on existing + new onoses here
925
926 // the departed...
927 var exiting = onoses.exit()
928 .transition()
929 .style('opacity', 0)
930 .remove();
931 }
932
Simon Hunt9462e8c2014-11-14 17:28:09 -0800933 function clickInst(d) {
934 var el = d3.select(this),
935 aff = el.classed('affinity');
936 if (!aff) {
937 setAffinity(el, d);
938 } else {
939 cancelAffinity();
940 }
941 }
942
943 function setAffinity(el, d) {
944 d3.selectAll('.onosInst')
945 .classed('mastership', true)
946 .classed('affinity', false);
947 el.classed('affinity', true);
948
949 suppressLayers(true);
950 node.each(function (n) {
951 if (n.master === d.id) {
952 n.el.classed('suppressed', false);
953 }
954 });
955 oiShowMaster = true;
956 }
957
958 function cancelAffinity() {
959 d3.selectAll('.onosInst')
960 .classed('mastership affinity', false);
961 restoreLayerState();
962 oiShowMaster = false;
963 }
964
Simon Hunta5e89142014-11-14 07:00:33 -0800965 // ==============================
Simon Hunt99c13842014-11-06 18:23:12 -0800966 // force layout modification functions
967
968 function translate(x, y) {
969 return 'translate(' + x + ',' + y + ')';
970 }
971
Simon Hunte2575b62014-11-18 15:25:53 -0800972 function rotate(deg) {
973 return 'rotate(' + deg + ')';
974 }
975
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800976 function missMsg(what, id) {
977 return '\n[' + what + '] "' + id + '" missing ';
978 }
979
980 function linkEndPoints(srcId, dstId) {
981 var srcNode = network.lookup[srcId],
982 dstNode = network.lookup[dstId],
983 sMiss = !srcNode ? missMsg('src', srcId) : '',
984 dMiss = !dstNode ? missMsg('dst', dstId) : '';
985
986 if (sMiss || dMiss) {
987 logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
988 return null;
989 }
990 return {
991 source: srcNode,
992 target: dstNode,
993 x1: srcNode.x,
994 y1: srcNode.y,
995 x2: dstNode.x,
996 y2: dstNode.y
997 };
998 }
999
Simon Hunt56d51852014-11-09 13:03:35 -08001000 function createHostLink(host) {
1001 var src = host.id,
1002 dst = host.cp.device,
Simon Hunt7cd48f32014-11-09 23:42:50 -08001003 id = host.ingress,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001004 lnk = linkEndPoints(src, dst);
Simon Hunt56d51852014-11-09 13:03:35 -08001005
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001006 if (!lnk) {
Simon Hunt56d51852014-11-09 13:03:35 -08001007 return null;
1008 }
1009
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001010 // Synthesize link ...
1011 $.extend(lnk, {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001012 key: id,
Simon Hunt56d51852014-11-09 13:03:35 -08001013 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -08001014
1015 type: function () { return 'hostLink'; },
1016 // TODO: ideally, we should see if our edge switch is online...
1017 online: function () { return true; },
1018 linkWidth: function () { return 1; }
Simon Hunt7cd48f32014-11-09 23:42:50 -08001019 });
Simon Hunt99c13842014-11-06 18:23:12 -08001020 return lnk;
1021 }
1022
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001023 function createLink(link) {
1024 var lnk = linkEndPoints(link.src, link.dst),
1025 type = link.type;
1026
1027 if (!lnk) {
1028 return null;
1029 }
1030
Simon Hunt8257f4c2014-11-16 19:34:54 -08001031 $.extend(lnk, {
1032 key: link.id,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001033 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -08001034 fromSource: link,
1035
1036 // functions to aggregate dual link state
1037 type: function () {
1038 var s = lnk.fromSource,
1039 t = lnk.fromTarget;
1040 return (s && s.type) || (t && t.type) || defaultLinkType;
1041 },
1042 online: function () {
1043 var s = lnk.fromSource,
1044 t = lnk.fromTarget;
1045 return (s && s.online) || (t && t.online);
1046 },
1047 linkWidth: function () {
1048 var s = lnk.fromSource,
1049 t = lnk.fromTarget,
1050 ws = (s && s.linkWidth) || 0,
1051 wt = (t && t.linkWidth) || 0;
1052 return Math.max(ws, wt);
1053 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001054 });
1055 return lnk;
Simon Hunt1a9eff92014-11-07 11:06:34 -08001056 }
1057
Simon Hunte2575b62014-11-18 15:25:53 -08001058 function removeLinkLabels() {
1059 network.links.forEach(function (d) {
1060 d.label = '';
1061 });
1062 }
1063
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001064 var widthRatio = 1.4,
1065 linkScale = d3.scale.linear()
1066 .domain([1, 12])
1067 .range([widthRatio, 12 * widthRatio])
1068 .clamp(true);
1069
Simon Hunt99c13842014-11-06 18:23:12 -08001070 function updateLinks() {
1071 link = linkG.selectAll('.link')
Simon Hunt8257f4c2014-11-16 19:34:54 -08001072 .data(network.links, function (d) { return d.key; });
Simon Hunt99c13842014-11-06 18:23:12 -08001073
1074 // operate on existing links, if necessary
1075 // link .foo() .bar() ...
1076
1077 // operate on entering links:
1078 var entering = link.enter()
1079 .append('line')
1080 .attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001081 x1: function (d) { return d.x1; },
1082 y1: function (d) { return d.y1; },
1083 x2: function (d) { return d.x2; },
1084 y2: function (d) { return d.y2; },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001085 stroke: config.topo.linkInColor,
1086 'stroke-width': config.topo.linkInWidth
Simon Hunt99c13842014-11-06 18:23:12 -08001087 });
1088
1089 // augment links
Simon Hunt7cd48f32014-11-09 23:42:50 -08001090 entering.each(function (d) {
1091 var link = d3.select(this);
1092 // provide ref to element selection from backing data....
1093 d.el = link;
Simon Hunt8257f4c2014-11-16 19:34:54 -08001094 restyleLinkElement(d);
Simon Hunt7cd48f32014-11-09 23:42:50 -08001095 });
Thomas Vachuska4830d392014-11-09 17:09:56 -08001096
1097 // operate on both existing and new links, if necessary
1098 //link .foo() .bar() ...
1099
Simon Hunte2575b62014-11-18 15:25:53 -08001100 // apply or remove labels
1101 var labelData = getLabelData();
1102 applyLinkLabels(labelData);
1103
Thomas Vachuska4830d392014-11-09 17:09:56 -08001104 // operate on exiting links:
Thomas Vachuska4830d392014-11-09 17:09:56 -08001105 link.exit()
Simon Hunt13bf9c82014-11-18 07:26:44 -08001106 .attr('stroke-dasharray', '3, 3')
1107 .style('opacity', 0.5)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001108 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -08001109 .duration(1500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001110 .attr({
Simon Hunt13bf9c82014-11-18 07:26:44 -08001111 'stroke-dasharray': '3, 12',
1112 stroke: config.topo.linkOutColor,
1113 'stroke-width': config.topo.linkOutWidth
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001114 })
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001115 .style('opacity', 0.0)
Thomas Vachuska4830d392014-11-09 17:09:56 -08001116 .remove();
Simon Hunte2575b62014-11-18 15:25:53 -08001117
1118 // NOTE: invoke a single tick to force the labels to position
1119 // onto their links.
1120 tick();
1121 }
1122
1123 function getLabelData() {
1124 // create the backing data for showing labels..
1125 var data = [];
1126 link.each(function (d) {
1127 if (d.label) {
1128 data.push({
1129 id: 'lab-' + d.key,
1130 key: d.key,
1131 label: d.label,
1132 ldata: d
1133 });
1134 }
1135 });
1136 return data;
1137 }
1138
1139 var linkLabelOffset = '0.3em';
1140
1141 function applyLinkLabels(data) {
1142 var entering;
1143
1144 linkLabel = linkLabelG.selectAll('.linkLabel')
1145 .data(data, function (d) { return d.id; });
1146
Simon Hunt56a2ea42014-11-19 12:39:31 -08001147 // for elements already existing, we need to update the text
1148 // and adjust the rectangle size to fit
1149 linkLabel.each(function (d) {
1150 var el = d3.select(this),
1151 rect = el.select('rect'),
1152 text = el.select('text');
1153 text.text(d.label);
1154 rect.attr(rectAroundText(el));
1155 });
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -08001156
Simon Hunte2575b62014-11-18 15:25:53 -08001157 entering = linkLabel.enter().append('g')
1158 .classed('linkLabel', true)
1159 .attr('id', function (d) { return d.id; });
1160
1161 entering.each(function (d) {
1162 var el = d3.select(this),
1163 rect,
1164 text,
1165 parms = {
1166 x1: d.ldata.x1,
1167 y1: d.ldata.y1,
1168 x2: d.ldata.x2,
1169 y2: d.ldata.y2
1170 };
1171
1172 d.el = el;
1173 rect = el.append('rect');
1174 text = el.append('text').text(d.label);
1175 rect.attr(rectAroundText(el));
1176 text.attr('dy', linkLabelOffset);
1177
1178 el.attr('transform', transformLabel(parms));
1179 });
1180
1181 // Remove any links that are no longer required.
1182 linkLabel.exit().remove();
1183 }
1184
1185 function rectAroundText(el) {
1186 var text = el.select('text'),
1187 box = text.node().getBBox();
1188
1189 // translate the bbox so that it is centered on [x,y]
1190 box.x = -box.width / 2;
1191 box.y = -box.height / 2;
1192
1193 // add padding
1194 box.x -= 1;
1195 box.width += 2;
1196 return box;
1197 }
1198
1199 function transformLabel(p) {
1200 var dx = p.x2 - p.x1,
1201 dy = p.y2 - p.y1,
1202 xMid = dx/2 + p.x1,
1203 yMid = dy/2 + p.y1;
1204 //length = Math.sqrt(dx*dx + dy*dy),
1205 //rads = Math.asin(dy/length),
1206 //degs = rads / (Math.PI*2) * 360;
1207
1208 return translate(xMid, yMid);
1209
1210 // TODO: consider making label parallel to line
1211 //return [
1212 // translate(xMid, yMid),
1213 // rotate(degs),
1214 // translate(0, 8)
1215 //].join('');
Simon Hunt99c13842014-11-06 18:23:12 -08001216 }
1217
1218 function createDeviceNode(device) {
1219 // start with the object as is
1220 var node = device,
Simon Huntbb282f52014-11-10 11:08:19 -08001221 type = device.type,
Simon Huntc72967b2014-11-20 09:21:42 -08001222 svgCls = type ? 'node device ' + type : 'node device',
1223 labels = device.labels || [];
1224
1225 labels.unshift(''); // add 'no-label' to front of cycle
Simon Hunt99c13842014-11-06 18:23:12 -08001226
1227 // Augment as needed...
1228 node.class = 'device';
Simon Huntbb282f52014-11-10 11:08:19 -08001229 node.svgClass = device.online ? svgCls + ' online' : svgCls;
Simon Hunt99c13842014-11-06 18:23:12 -08001230 positionNode(node);
1231
1232 // cache label array length
Simon Huntc72967b2014-11-20 09:21:42 -08001233 // TODO: need a uiConfig event from the server to set things
1234 // like device labels count, host labels count, etc.
1235 // The current method (here) is a little fragile
Simon Hunt99c13842014-11-06 18:23:12 -08001236 network.deviceLabelCount = device.labels.length;
Simon Hunt99c13842014-11-06 18:23:12 -08001237 return node;
1238 }
1239
Simon Hunt56d51852014-11-09 13:03:35 -08001240 function createHostNode(host) {
1241 // start with the object as is
1242 var node = host;
1243
1244 // Augment as needed...
1245 node.class = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001246 if (!node.type) {
Simon Hunteb1514d2014-11-20 09:57:29 -08001247 node.type = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001248 }
Simon Hunt7fa116d2014-11-17 14:16:55 -08001249 node.svgClass = 'node host ' + node.type;
Simon Hunt56d51852014-11-09 13:03:35 -08001250 positionNode(node);
1251
1252 // cache label array length
1253 network.hostLabelCount = host.labels.length;
Simon Hunt56d51852014-11-09 13:03:35 -08001254 return node;
1255 }
1256
Simon Hunt99c13842014-11-06 18:23:12 -08001257 function positionNode(node) {
1258 var meta = node.metaUi,
Simon Huntac9e24f2014-11-12 10:12:21 -08001259 x = meta && meta.x,
1260 y = meta && meta.y,
1261 xy;
Simon Hunt99c13842014-11-06 18:23:12 -08001262
Simon Huntac9e24f2014-11-12 10:12:21 -08001263 // If we have [x,y] already, use that...
Simon Hunt99c13842014-11-06 18:23:12 -08001264 if (x && y) {
1265 node.fixed = true;
Simon Huntac9e24f2014-11-12 10:12:21 -08001266 node.x = x;
1267 node.y = y;
1268 return;
Simon Hunt99c13842014-11-06 18:23:12 -08001269 }
Simon Huntac9e24f2014-11-12 10:12:21 -08001270
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001271 var location = node.location;
1272 if (location && location.type === 'latlng') {
1273 var coord = geoMapProjection([location.lng, location.lat]);
1274 node.fixed = true;
1275 node.x = coord[0];
1276 node.y = coord[1];
1277 return;
1278 }
1279
Simon Huntac9e24f2014-11-12 10:12:21 -08001280 // Note: Placing incoming unpinned nodes at exactly the same point
1281 // (center of the view) causes them to explode outwards when
1282 // the force layout kicks in. So, we spread them out a bit
1283 // initially, to provide a more serene layout convergence.
1284 // Additionally, if the node is a host, we place it near
1285 // the device it is connected to.
1286
1287 function spread(s) {
1288 return Math.floor((Math.random() * s) - s/2);
1289 }
1290
1291 function randDim(dim) {
1292 return dim / 2 + spread(dim * 0.7071);
1293 }
1294
1295 function rand() {
1296 return {
1297 x: randDim(network.view.width()),
1298 y: randDim(network.view.height())
1299 };
1300 }
1301
1302 function near(node) {
1303 var min = 12,
1304 dx = spread(12),
1305 dy = spread(12);
1306 return {
1307 x: node.x + min + dx,
1308 y: node.y + min + dy
1309 };
1310 }
1311
1312 function getDevice(cp) {
1313 var d = network.lookup[cp.device];
1314 return d || rand();
1315 }
1316
1317 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
1318 $.extend(node, xy);
Simon Hunt99c13842014-11-06 18:23:12 -08001319 }
1320
Thomas Vachuska89543292014-11-19 11:28:33 -08001321 function iconUrl(d) {
1322 return 'img/' + d.type + '.png';
Simon Hunt99c13842014-11-06 18:23:12 -08001323 }
1324
Simon Huntc72967b2014-11-20 09:21:42 -08001325 function iconGlyphUrl(d) {
1326 var which = d.type || 'unknown';
1327 return '#' + which;
1328 }
1329
Simon Hunt99c13842014-11-06 18:23:12 -08001330 // returns the newly computed bounding box of the rectangle
1331 function adjustRectToFitText(n) {
1332 var text = n.select('text'),
1333 box = text.node().getBBox(),
1334 lab = config.labels;
1335
1336 text.attr('text-anchor', 'middle')
1337 .attr('y', '-0.8em')
1338 .attr('x', lab.imgPad/2);
1339
1340 // translate the bbox so that it is centered on [x,y]
1341 box.x = -box.width / 2;
1342 box.y = -box.height / 2;
1343
1344 // add padding
1345 box.x -= (lab.padLR + lab.imgPad/2);
1346 box.width += lab.padLR * 2 + lab.imgPad;
1347 box.y -= lab.padTB;
1348 box.height += lab.padTB * 2;
1349
1350 return box;
1351 }
1352
Simon Hunt1a9eff92014-11-07 11:06:34 -08001353 function mkSvgClass(d) {
1354 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
1355 }
1356
Simon Hunt7cd48f32014-11-09 23:42:50 -08001357 function hostLabel(d) {
1358 var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0;
1359 return d.labels[idx];
1360 }
1361 function deviceLabel(d) {
1362 var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0;
1363 return d.labels[idx];
1364 }
Simon Huntc72967b2014-11-20 09:21:42 -08001365 function trimLabel(label) {
1366 return (label && label.trim()) || '';
1367 }
1368
1369 function emptyBox() {
1370 return {
1371 x: -2,
1372 y: -2,
1373 width: 4,
1374 height: 4
1375 };
Simon Hunt7cd48f32014-11-09 23:42:50 -08001376 }
1377
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001378 function updateDeviceLabel(d) {
Simon Huntc72967b2014-11-20 09:21:42 -08001379 var label = trimLabel(deviceLabel(d)),
1380 noLabel = !label,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001381 node = d.el,
Simon Huntc72967b2014-11-20 09:21:42 -08001382 box,
1383 dx,
1384 dy;
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001385
1386 node.select('text')
1387 .text(label)
1388 .style('opacity', 0)
1389 .transition()
1390 .style('opacity', 1);
1391
Simon Huntc72967b2014-11-20 09:21:42 -08001392 if (noLabel) {
1393 box = emptyBox();
1394 dx = -config.icons.device.dim/2;
1395 dy = -config.icons.device.dim/2;
1396 } else {
1397 box = adjustRectToFitText(node);
1398 dx = box.x + config.icons.xoff;
1399 dy = box.y + config.icons.yoff;
1400 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001401
1402 node.select('rect')
1403 .transition()
1404 .attr(box);
1405
Simon Huntc72967b2014-11-20 09:21:42 -08001406 node.select('g.deviceIcon')
Thomas Vachuska89543292014-11-19 11:28:33 -08001407 .transition()
Simon Huntc72967b2014-11-20 09:21:42 -08001408 .attr('transform', translate(dx, dy));
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001409 }
1410
1411 function updateHostLabel(d) {
1412 var label = hostLabel(d),
1413 host = d.el;
1414
1415 host.select('text').text(label);
1416 }
1417
Simon Huntbb282f52014-11-10 11:08:19 -08001418 function updateDeviceState(nodeData) {
1419 nodeData.el.classed('online', nodeData.online);
1420 updateDeviceLabel(nodeData);
1421 // TODO: review what else might need to be updated
1422 }
1423
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001424 function updateLinkState(linkData) {
1425 updateLinkWidth(linkData);
Thomas Vachuskabadb93f2014-11-15 23:51:17 -08001426 linkData.el.classed('inactive', !linkData.online);
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001427 // TODO: review what else might need to be updated
1428 // update label, if showing
1429 }
1430
Simon Huntbb282f52014-11-10 11:08:19 -08001431 function updateHostState(hostData) {
1432 updateHostLabel(hostData);
1433 // TODO: review what else might need to be updated
1434 }
1435
Simon Hunt6ac93f32014-11-13 12:17:27 -08001436 function nodeMouseOver(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001437 hovered = d;
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -08001438 if (trafficHover() && (d.class === 'host' || d.class === 'device')) {
Thomas Vachuska29617e52014-11-20 03:17:46 -08001439 showSelectTraffic();
1440 } else if (flowsHover() && (d.class === 'device')) {
1441 showDeviceLinkFlows();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001442 }
1443 }
1444
1445 function nodeMouseOut(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001446 hovered = null;
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -08001447 if (trafficHover() && (d.class === 'host' || d.class === 'device')) {
Thomas Vachuska29617e52014-11-20 03:17:46 -08001448 showSelectTraffic();
1449 } else if (flowsHover() && (d.class === 'device')) {
1450 showDeviceLinkFlows();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001451 }
1452 }
Simon Huntbb282f52014-11-10 11:08:19 -08001453
Simon Hunteb1514d2014-11-20 09:57:29 -08001454 function addHostIcon(node, radius, iid) {
Thomas Vachuska89543292014-11-19 11:28:33 -08001455 var dim = radius * 1.5,
1456 xlate = -dim / 2;
1457
Simon Hunteb1514d2014-11-20 09:57:29 -08001458 node.append('use').attr({
1459 class: 'glyphIcon hostIcon',
1460 transform: translate(xlate,xlate),
1461 'xlink:href': iid,
1462 width: dim,
1463 height: dim
1464 });
Thomas Vachuska89543292014-11-19 11:28:33 -08001465 }
1466
Simon Hunt99c13842014-11-06 18:23:12 -08001467 function updateNodes() {
1468 node = nodeG.selectAll('.node')
1469 .data(network.nodes, function (d) { return d.id; });
1470
Simon Huntc72967b2014-11-20 09:21:42 -08001471 // TODO: operate on existing nodes
Simon Hunt7cd48f32014-11-09 23:42:50 -08001472 // update host labels
Simon Hunt99c13842014-11-06 18:23:12 -08001473 //node .foo() .bar() ...
1474
1475 // operate on entering nodes:
1476 var entering = node.enter()
1477 .append('g')
1478 .attr({
1479 id: function (d) { return safeId(d.id); },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001480 class: mkSvgClass,
Simon Hunt99c13842014-11-06 18:23:12 -08001481 transform: function (d) { return translate(d.x, d.y); },
1482 opacity: 0
1483 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08001484 .call(network.drag)
Simon Hunt6ac93f32014-11-13 12:17:27 -08001485 .on('mouseover', nodeMouseOver)
1486 .on('mouseout', nodeMouseOut)
Simon Hunt99c13842014-11-06 18:23:12 -08001487 .transition()
1488 .attr('opacity', 1);
1489
1490 // augment device nodes...
1491 entering.filter('.device').each(function (d) {
1492 var node = d3.select(this),
Simon Huntc72967b2014-11-20 09:21:42 -08001493 label = trimLabel(deviceLabel(d)),
1494 noLabel = !label,
Simon Hunt99c13842014-11-06 18:23:12 -08001495 box;
1496
Simon Hunt7cd48f32014-11-09 23:42:50 -08001497 // provide ref to element from backing data....
1498 d.el = node;
1499
Simon Hunt99c13842014-11-06 18:23:12 -08001500 node.append('rect')
1501 .attr({
1502 'rx': 5,
1503 'ry': 5
1504 });
1505
1506 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001507 .text(label)
Simon Hunt99c13842014-11-06 18:23:12 -08001508 .attr('dy', '1.1em');
1509
1510 box = adjustRectToFitText(node);
1511
1512 node.select('rect')
1513 .attr(box);
1514
Simon Huntc72967b2014-11-20 09:21:42 -08001515 addDeviceIcon(node, box, noLabel, iconGlyphUrl(d));
Simon Hunt99c13842014-11-06 18:23:12 -08001516
Simon Huntc7ee0662014-11-05 16:44:37 -08001517 });
Simon Hunt934c3ce2014-11-05 11:45:07 -08001518
Thomas Vachuska89543292014-11-19 11:28:33 -08001519 // TODO: better place for this configuration state
1520 var defaultHostRadius = 9,
1521 hostRadius = {
1522 bgpSpeaker: 14,
1523 router: 14,
1524 host: 14
1525 },
Simon Hunteb1514d2014-11-20 09:57:29 -08001526 hostGlyphId = {
Thomas Vachuska89543292014-11-19 11:28:33 -08001527 bgpSpeaker: 'bgpSpeaker',
1528 router: 'router',
1529 host: 'host'
1530 };
1531
1532
Simon Hunt56d51852014-11-09 13:03:35 -08001533 // augment host nodes...
1534 entering.filter('.host').each(function (d) {
1535 var node = d3.select(this),
Thomas Vachuska89543292014-11-19 11:28:33 -08001536 r = hostRadius[d.type] || defaultHostRadius,
1537 textDy = r + 10,
Simon Hunteb1514d2014-11-20 09:57:29 -08001538 iid = iconGlyphUrl(d);
Simon Hunt56d51852014-11-09 13:03:35 -08001539
Simon Hunt7cd48f32014-11-09 23:42:50 -08001540 // provide ref to element from backing data....
1541 d.el = node;
1542
Thomas Vachuska89543292014-11-19 11:28:33 -08001543 node.append('circle')
1544 .attr('r', r);
Simon Hunt7fa116d2014-11-17 14:16:55 -08001545
Simon Hunteb1514d2014-11-20 09:57:29 -08001546 if (iid) {
1547 addHostIcon(node, r, iid);
Simon Hunt7fa116d2014-11-17 14:16:55 -08001548 }
Simon Hunt56d51852014-11-09 13:03:35 -08001549
Paul Greyson29cd58f2014-11-18 13:14:57 -08001550
Simon Hunt56d51852014-11-09 13:03:35 -08001551 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001552 .text(hostLabel)
Thomas Vachuska89543292014-11-19 11:28:33 -08001553 .attr('dy', textDy)
Simon Hunt7cd48f32014-11-09 23:42:50 -08001554 .attr('text-anchor', 'middle');
Simon Hunt56d51852014-11-09 13:03:35 -08001555
1556 // debug function to show the modelled x,y coordinates of nodes...
1557 if (debug('showNodeXY')) {
1558 node.select('circle').attr('fill-opacity', 0.5);
1559 node.append('circle')
1560 .attr({
1561 class: 'debug',
1562 cx: 0,
1563 cy: 0,
1564 r: '3px'
1565 });
1566 }
1567 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001568
Simon Hunt99c13842014-11-06 18:23:12 -08001569 // operate on both existing and new nodes, if necessary
1570 //node .foo() .bar() ...
Simon Huntc7ee0662014-11-05 16:44:37 -08001571
Simon Hunt99c13842014-11-06 18:23:12 -08001572 // operate on exiting nodes:
Simon Huntea80eb42014-11-11 13:46:57 -08001573 // Note that the node is removed after 2 seconds.
1574 // Sub element animations should be shorter than 2 seconds.
1575 var exiting = node.exit()
Simon Hunt44031102014-11-11 13:20:36 -08001576 .transition()
1577 .duration(2000)
Simon Huntea80eb42014-11-11 13:46:57 -08001578 .style('opacity', 0)
Simon Hunt99c13842014-11-06 18:23:12 -08001579 .remove();
Simon Huntea80eb42014-11-11 13:46:57 -08001580
1581 // host node exits....
1582 exiting.filter('.host').each(function (d) {
1583 var node = d3.select(this);
1584
1585 node.select('text')
1586 .style('opacity', 0.5)
1587 .transition()
1588 .duration(1000)
1589 .style('opacity', 0);
1590 // note, leave <g>.remove to remove this element
1591
Thomas Vachuska89543292014-11-19 11:28:33 -08001592 node.select('circle')
1593 .style('stroke-fill', '#555')
1594 .style('fill', '#888')
Simon Huntea80eb42014-11-11 13:46:57 -08001595 .style('opacity', 0.5)
1596 .transition()
1597 .duration(1500)
1598 .attr('r', 0);
1599 // note, leave <g>.remove to remove this element
1600
1601 });
1602
1603 // TODO: device node exits
Simon Huntc7ee0662014-11-05 16:44:37 -08001604 }
1605
Simon Huntc72967b2014-11-20 09:21:42 -08001606 function addDeviceIcon(node, box, noLabel, iid) {
1607 var cfg = config.icons.device,
1608 dx,
1609 dy,
1610 g;
1611
1612 if (noLabel) {
1613 box = emptyBox();
1614 dx = -cfg.dim/2;
1615 dy = -cfg.dim/2;
1616 } else {
1617 box = adjustRectToFitText(node);
1618 dx = box.x + config.icons.xoff;
1619 dy = box.y + config.icons.yoff;
1620 }
1621
Simon Hunteb1514d2014-11-20 09:57:29 -08001622 g = node.append('g')
1623 .attr('class', 'glyphIcon deviceIcon')
Simon Huntc72967b2014-11-20 09:21:42 -08001624 .attr('transform', translate(dx, dy));
1625
1626 g.append('rect').attr({
1627 x: 0,
1628 y: 0,
1629 rx: cfg.rx,
1630 width: cfg.dim,
1631 height: cfg.dim
1632 });
1633
1634 g.append('use').attr({
1635 'xlink:href': iid,
1636 width: cfg.dim,
1637 height: cfg.dim
1638 });
1639
1640/*
1641 if (icon) {
1642 node.append('rect')
1643 .attr({
1644 class: 'iconUnderlay',
1645 x: box.x + config.icons.xoff,
1646 y: box.y + config.icons.yoff,
1647 width: cfg.w,
1648 height: cfg.h,
1649 rx: 4
1650 }).style({
1651 stroke: '#000',
1652 fill: '#ddd'
1653 });
1654 node.append('svg:image')
1655 .attr({
1656 x: box.x + config.icons.xoff + 2,
1657 y: box.y + config.icons.yoff + 2,
1658 width: cfg.w - 4,
1659 height: cfg.h - 4,
1660 'xlink:href': icon
1661 });
1662 }
1663*/
1664 }
1665
1666
1667
Simon Hunt8257f4c2014-11-16 19:34:54 -08001668 function find(key, array) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001669 for (var idx = 0, n = array.length; idx < n; idx++) {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001670 if (array[idx].key === key) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001671 return idx;
1672 }
1673 }
1674 return -1;
1675 }
1676
1677 function removeLinkElement(linkData) {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001678 var idx = find(linkData.key, network.links),
1679 removed;
1680 if (idx >=0) {
1681 // remove from links array
1682 removed = network.links.splice(idx, 1);
1683 // remove from lookup cache
1684 delete network.lookup[removed[0].key];
1685 updateLinks();
1686 network.force.resume();
1687 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001688 }
Simon Huntc7ee0662014-11-05 16:44:37 -08001689
Simon Hunt44031102014-11-11 13:20:36 -08001690 function removeHostElement(hostData) {
1691 // first, remove associated hostLink...
1692 removeLinkElement(hostData.linkData);
1693
1694 // remove from lookup cache
1695 delete network.lookup[hostData.id];
1696 // remove from nodes array
1697 var idx = find(hostData.id, network.nodes);
1698 network.nodes.splice(idx, 1);
1699 // remove from SVG
1700 updateNodes();
1701 network.force.resume();
1702 }
1703
1704
Simon Huntc7ee0662014-11-05 16:44:37 -08001705 function tick() {
1706 node.attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001707 transform: function (d) { return translate(d.x, d.y); }
Simon Huntc7ee0662014-11-05 16:44:37 -08001708 });
1709
1710 link.attr({
1711 x1: function (d) { return d.source.x; },
1712 y1: function (d) { return d.source.y; },
1713 x2: function (d) { return d.target.x; },
1714 y2: function (d) { return d.target.y; }
1715 });
Simon Hunte2575b62014-11-18 15:25:53 -08001716
1717 linkLabel.each(function (d) {
1718 var el = d3.select(this);
1719 var lnk = findLinkById(d.key),
1720 parms = {
1721 x1: lnk.source.x,
1722 y1: lnk.source.y,
1723 x2: lnk.target.x,
1724 y2: lnk.target.y
1725 };
1726 el.attr('transform', transformLabel(parms));
1727 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001728 }
Simon Hunt934c3ce2014-11-05 11:45:07 -08001729
1730 // ==============================
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001731 // Web-Socket for live data
1732
1733 function webSockUrl() {
1734 return document.location.toString()
1735 .replace(/\#.*/, '')
1736 .replace('http://', 'ws://')
1737 .replace('https://', 'wss://')
1738 .replace('index2.html', config.webSockUrl);
1739 }
1740
1741 webSock = {
1742 ws : null,
1743
1744 connect : function() {
1745 webSock.ws = new WebSocket(webSockUrl());
1746
1747 webSock.ws.onopen = function() {
Simon Hunt0c6d4192014-11-12 12:07:10 -08001748 noWebSock(false);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001749 };
1750
1751 webSock.ws.onmessage = function(m) {
1752 if (m.data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001753 wsTraceRx(m.data);
Thomas Vachuskad472c6e2014-11-07 19:11:05 -08001754 handleServerEvent(JSON.parse(m.data));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001755 }
1756 };
1757
1758 webSock.ws.onclose = function(m) {
1759 webSock.ws = null;
Simon Hunt0c6d4192014-11-12 12:07:10 -08001760 noWebSock(true);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001761 };
1762 },
1763
1764 send : function(text) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001765 if (text != null) {
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001766 webSock._send(text);
1767 }
1768 },
1769
1770 _send : function(message) {
1771 if (webSock.ws) {
1772 webSock.ws.send(message);
Simon Hunta255a2c2014-11-13 22:29:35 -08001773 } else if (config.useLiveData) {
Simon Hunt56d51852014-11-09 13:03:35 -08001774 network.view.alert('no web socket open\n\n' + message);
Simon Hunta255a2c2014-11-13 22:29:35 -08001775 } else {
1776 console.log('WS Send: ' + JSON.stringify(message));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001777 }
1778 }
1779
1780 };
1781
Simon Hunt0c6d4192014-11-12 12:07:10 -08001782 function noWebSock(b) {
1783 mask.style('display',b ? 'block' : 'none');
1784 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001785
1786 function sendMessage(evType, payload) {
1787 var toSend = {
Simon Huntbb282f52014-11-10 11:08:19 -08001788 event: evType,
1789 sid: ++sid,
1790 payload: payload
1791 },
1792 asText = JSON.stringify(toSend);
1793 wsTraceTx(asText);
1794 webSock.send(asText);
Simon Huntc76ae892014-11-18 17:31:51 -08001795
1796 // Temporary measure for debugging UI behavior ...
1797 if (!config.useLiveData) {
1798 handleTestSend(toSend);
1799 }
Simon Huntbb282f52014-11-10 11:08:19 -08001800 }
1801
1802 function wsTraceTx(msg) {
1803 wsTrace('tx', msg);
1804 }
1805 function wsTraceRx(msg) {
1806 wsTrace('rx', msg);
1807 }
1808 function wsTrace(rxtx, msg) {
Simon Huntbb282f52014-11-10 11:08:19 -08001809 console.log('[' + rxtx + '] ' + msg);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001810 }
1811
Simon Huntc76ae892014-11-18 17:31:51 -08001812 // NOTE: Temporary hardcoded example for showing detail pane
1813 // while we fine-
1814 // Probably should not merge this change...
1815 function handleTestSend(msg) {
1816 if (msg.event === 'requestDetails') {
1817 showDetails({
1818 event: 'showDetails',
1819 sid: 1001,
1820 payload: {
1821 "id": "of:0000ffffffffff09",
1822 "type": "roadm",
1823 "propOrder": [
1824 "Name",
1825 "Vendor",
1826 "H/W Version",
1827 "S/W Version",
1828 "-",
1829 "Latitude",
1830 "Longitude",
1831 "Ports"
1832 ],
1833 "props": {
1834 "Name": null,
1835 "Vendor": "Linc",
1836 "H/W Version": "OE",
1837 "S/W Version": "?",
1838 "-": "",
1839 "Latitude": "40.8",
1840 "Longitude": "73.1",
1841 "Ports": "2"
1842 }
1843 }
1844 });
1845 }
1846 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001847
1848 // ==============================
1849 // Selection stuff
1850
1851 function selectObject(obj, el) {
1852 var n,
Simon Hunt01095ff2014-11-13 16:37:29 -08001853 srcEv = d3.event.sourceEvent,
1854 meta = srcEv.metaKey,
1855 shift = srcEv.shiftKey;
1856
Simon Huntdeab4322014-11-13 18:49:07 -08001857 if ((panZoom() && !meta) || (!panZoom() && meta)) {
Simon Hunt01095ff2014-11-13 16:37:29 -08001858 return;
1859 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001860
1861 if (el) {
1862 n = d3.select(el);
1863 } else {
1864 node.each(function(d) {
1865 if (d == obj) {
1866 n = d3.select(el = this);
1867 }
1868 });
1869 }
1870 if (!n) return;
1871
Simon Hunt01095ff2014-11-13 16:37:29 -08001872 if (shift && n.classed('selected')) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001873 deselectObject(obj.id);
Simon Hunt61d04042014-11-11 17:27:16 -08001874 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001875 return;
1876 }
1877
Simon Hunt01095ff2014-11-13 16:37:29 -08001878 if (!shift) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001879 deselectAll();
1880 }
1881
Simon Huntc31d5692014-11-12 13:27:18 -08001882 selections[obj.id] = { obj: obj, el: el };
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001883 selectOrder.push(obj.id);
1884
1885 n.classed('selected', true);
Simon Hunt61d04042014-11-11 17:27:16 -08001886 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001887 }
1888
1889 function deselectObject(id) {
Simon Huntc31d5692014-11-12 13:27:18 -08001890 var obj = selections[id],
1891 idx;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001892 if (obj) {
1893 d3.select(obj.el).classed('selected', false);
Simon Hunt61d04042014-11-11 17:27:16 -08001894 delete selections[id];
Simon Huntc31d5692014-11-12 13:27:18 -08001895 idx = $.inArray(id, selectOrder);
1896 if (idx >= 0) {
1897 selectOrder.splice(idx, 1);
1898 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001899 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001900 }
1901
1902 function deselectAll() {
1903 // deselect all nodes in the network...
1904 node.classed('selected', false);
1905 selections = {};
1906 selectOrder = [];
Simon Hunt61d04042014-11-11 17:27:16 -08001907 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001908 }
1909
Simon Hunt61d04042014-11-11 17:27:16 -08001910 // update the state of the detail pane, based on current selections
1911 function updateDetailPane() {
1912 var nSel = selectOrder.length;
1913 if (!nSel) {
1914 detailPane.hide();
Simon Huntd72bc702014-11-13 18:38:04 -08001915 showTrafficAction(); // sends cancelTraffic event
Simon Hunt61d04042014-11-11 17:27:16 -08001916 } else if (nSel === 1) {
1917 singleSelect();
1918 } else {
1919 multiSelect();
1920 }
1921 }
1922
1923 function singleSelect() {
1924 requestDetails();
Simon Huntb53e0682014-11-12 13:32:01 -08001925 // NOTE: detail pane will be shown from showDetails event callback
Simon Hunt61d04042014-11-11 17:27:16 -08001926 }
1927
1928 function multiSelect() {
Simon Huntb53e0682014-11-12 13:32:01 -08001929 populateMultiSelect();
Simon Huntb53e0682014-11-12 13:32:01 -08001930 }
1931
1932 function addSep(tbody) {
1933 var tr = tbody.append('tr');
1934 $('<hr>').appendTo(tr.append('td').attr('colspan', 2));
1935 }
1936
1937 function addProp(tbody, label, value) {
1938 var tr = tbody.append('tr');
1939
1940 tr.append('td')
1941 .attr('class', 'label')
1942 .text(label + ' :');
1943
1944 tr.append('td')
1945 .attr('class', 'value')
1946 .text(value);
1947 }
1948
1949 function populateMultiSelect() {
1950 detailPane.empty();
1951
1952 var title = detailPane.append("h2"),
1953 table = detailPane.append("table"),
1954 tbody = table.append("tbody");
1955
1956 title.text('Multi-Select...');
1957
1958 selectOrder.forEach(function (d, i) {
1959 addProp(tbody, i+1, d);
1960 });
Simon Huntd72bc702014-11-13 18:38:04 -08001961
1962 addMultiSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08001963 }
1964
1965 function populateDetails(data) {
1966 detailPane.empty();
1967
1968 var title = detailPane.append("h2"),
1969 table = detailPane.append("table"),
1970 tbody = table.append("tbody");
1971
1972 $('<img src="img/' + data.type + '.png">').appendTo(title);
1973 $('<span>').attr('class', 'icon').text(data.id).appendTo(title);
1974
1975 data.propOrder.forEach(function(p) {
1976 if (p === '-') {
1977 addSep(tbody);
1978 } else {
1979 addProp(tbody, p, data.props[p]);
1980 }
1981 });
Simon Huntd72bc702014-11-13 18:38:04 -08001982
1983 addSingleSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08001984 }
1985
Simon Huntd72bc702014-11-13 18:38:04 -08001986 function addSingleSelectActions() {
1987 detailPane.append('hr');
1988 // always want to allow 'show traffic'
Simon Hunta5e89142014-11-14 07:00:33 -08001989 addAction(detailPane, 'Show Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001990 }
1991
1992 function addMultiSelectActions() {
1993 detailPane.append('hr');
1994 // always want to allow 'show traffic'
Simon Hunta5e89142014-11-14 07:00:33 -08001995 addAction(detailPane, 'Show Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001996 // if exactly two hosts are selected, also want 'add host intent'
1997 if (nSel() === 2 && allSelectionsClass('host')) {
Simon Hunta5e89142014-11-14 07:00:33 -08001998 addAction(detailPane, 'Add Host Intent', addIntentAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001999 }
2000 }
2001
Simon Hunta5e89142014-11-14 07:00:33 -08002002 function addAction(panel, text, cb) {
2003 panel.append('div')
Simon Huntd72bc702014-11-13 18:38:04 -08002004 .classed('actionBtn', true)
2005 .text(text)
2006 .on('click', cb);
2007 }
2008
2009
Paul Greysonfcba0e82014-11-13 10:21:16 -08002010 function zoomPan(scale, translate) {
2011 zoomPanContainer.attr("transform", "translate(" + translate + ")scale(" + scale + ")");
2012 // keep the map lines constant width while zooming
Thomas Vachuska89543292014-11-19 11:28:33 -08002013 bgImg.style("stroke-width", 2.0 / scale + "px");
Paul Greysonfcba0e82014-11-13 10:21:16 -08002014 }
2015
2016 function resetZoomPan() {
2017 zoomPan(1, [0,0]);
2018 zoom.scale(1).translate([0,0]);
2019 }
2020
2021 function setupZoomPan() {
2022 function zoomed() {
Simon Huntdeab4322014-11-13 18:49:07 -08002023 if (!panZoom() ^ !d3.event.sourceEvent.metaKey) {
Paul Greysonfcba0e82014-11-13 10:21:16 -08002024 zoomPan(d3.event.scale, d3.event.translate);
2025 }
2026 }
2027
2028 zoom = d3.behavior.zoom()
2029 .translate([0, 0])
2030 .scale(1)
2031 .scaleExtent([1, 8])
2032 .on("zoom", zoomed);
2033
2034 svg.call(zoom);
2035 }
2036
Simon Hunt61d04042014-11-11 17:27:16 -08002037 // ==============================
2038 // Test harness code
Simon Hunt56d51852014-11-09 13:03:35 -08002039
2040 function prepareScenario(view, ctx, dbg) {
2041 var sc = scenario,
2042 urlSc = sc.evDir + ctx + sc.evScenario;
2043
2044 if (!ctx) {
2045 view.alert("No scenario specified (null ctx)");
2046 return;
2047 }
2048
2049 sc.view = view;
2050 sc.ctx = ctx;
2051 sc.debug = dbg;
2052 sc.evNumber = 0;
2053
2054 d3.json(urlSc, function(err, data) {
Simon Huntbb282f52014-11-10 11:08:19 -08002055 var p = data && data.params || {},
2056 desc = data && data.description || null,
Simon Huntfc274c92014-11-11 11:05:46 -08002057 intro = data && data.title;
Simon Huntbb282f52014-11-10 11:08:19 -08002058
Simon Hunt56d51852014-11-09 13:03:35 -08002059 if (err) {
2060 view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err);
2061 } else {
2062 sc.params = p;
Simon Huntbb282f52014-11-10 11:08:19 -08002063 if (desc) {
2064 intro += '\n\n ' + desc.join('\n ');
2065 }
2066 view.alert(intro);
Simon Hunt56d51852014-11-09 13:03:35 -08002067 }
2068 });
2069
2070 }
2071
Simon Hunt01095ff2014-11-13 16:37:29 -08002072 // ==============================
2073 // Toggle Buttons in masthead
Simon Hunt0c6d4192014-11-12 12:07:10 -08002074
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002075 // TODO: toggle button (and other widgets in the masthead) should be provided
2076 // by the framework; not generated by the view.
2077
Simon Hunta5e89142014-11-14 07:00:33 -08002078 var showInstances,
2079 doPanZoom,
2080 showTrafficOnHover;
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002081
2082 function addButtonBar(view) {
2083 var bb = d3.select('#mast')
2084 .append('span').classed('right', true).attr('id', 'bb');
2085
Simon Hunta5e89142014-11-14 07:00:33 -08002086 function mkTogBtn(text, cb) {
2087 return bb.append('span')
2088 .classed('btn', true)
2089 .text(text)
2090 .on('click', cb);
2091 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002092
Simon Hunta5e89142014-11-14 07:00:33 -08002093 showInstances = mkTogBtn('Show Instances', toggleInst);
Simon Hunte5b71752014-11-18 20:06:07 -08002094 //doPanZoom = mkTogBtn('Pan/Zoom', togglePanZoom);
2095 //showTrafficOnHover = mkTogBtn('Show traffic on hover', toggleTrafficHover);
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002096 }
2097
Simon Hunta5e89142014-11-14 07:00:33 -08002098 function instShown() {
2099 return showInstances.classed('active');
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002100 }
Simon Hunta5e89142014-11-14 07:00:33 -08002101 function toggleInst() {
2102 showInstances.classed('active', !instShown());
2103 if (instShown()) {
2104 oiBox.show();
2105 } else {
2106 oiBox.hide();
2107 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002108 }
2109
Simon Huntdeab4322014-11-13 18:49:07 -08002110 function panZoom() {
Simon Hunte5b71752014-11-18 20:06:07 -08002111 return false;
2112 //return doPanZoom.classed('active');
Simon Hunt01095ff2014-11-13 16:37:29 -08002113 }
Simon Hunta5e89142014-11-14 07:00:33 -08002114 function togglePanZoom() {
2115 doPanZoom.classed('active', !panZoom());
2116 }
2117
2118 function trafficHover() {
Thomas Vachuska29617e52014-11-20 03:17:46 -08002119 return hoverModes[hoverMode] === 'intents';
Simon Hunta5e89142014-11-14 07:00:33 -08002120 }
Thomas Vachuska29617e52014-11-20 03:17:46 -08002121
2122 function flowsHover() {
2123 return hoverModes[hoverMode] === 'flows';
2124 }
2125
Simon Hunta5e89142014-11-14 07:00:33 -08002126 function toggleTrafficHover() {
2127 showTrafficOnHover.classed('active', !trafficHover());
2128 }
2129
Simon Hunt7fa116d2014-11-17 14:16:55 -08002130 function loadGlyphs(svg) {
2131 var defs = svg.append('defs');
2132 gly.defBird(defs);
2133 gly.defBullhorn(defs);
Simon Huntc72967b2014-11-20 09:21:42 -08002134 gly.defGlyphs(defs);
Simon Hunt7fa116d2014-11-17 14:16:55 -08002135 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002136
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002137 // ==============================
Simon Hunt142d0032014-11-04 20:13:09 -08002138 // View life-cycle callbacks
Simon Hunt195cb382014-11-03 17:50:51 -08002139
Simon Huntf67722a2014-11-10 09:32:06 -08002140 function preload(view, ctx, flags) {
Simon Hunt142d0032014-11-04 20:13:09 -08002141 var w = view.width(),
2142 h = view.height(),
Simon Huntc7ee0662014-11-05 16:44:37 -08002143 fcfg = config.force,
2144 fpad = fcfg.pad,
2145 forceDim = [w - 2*fpad, h - 2*fpad];
Simon Hunt195cb382014-11-03 17:50:51 -08002146
Simon Hunt142d0032014-11-04 20:13:09 -08002147 // NOTE: view.$div is a D3 selection of the view's div
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002148 var viewBox = '0 0 ' + config.logicalSize + ' ' + config.logicalSize;
2149 svg = view.$div.append('svg').attr('viewBox', viewBox);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002150 setSize(svg, view);
2151
Simon Hunt7fa116d2014-11-17 14:16:55 -08002152 loadGlyphs(svg);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002153
Paul Greysonfcba0e82014-11-13 10:21:16 -08002154 zoomPanContainer = svg.append('g').attr('id', 'zoomPanContainer');
Paul Greysonfcba0e82014-11-13 10:21:16 -08002155 setupZoomPan();
2156
Simon Hunt1a9eff92014-11-07 11:06:34 -08002157 // add blue glow filter to svg layer
Paul Greysonfcba0e82014-11-13 10:21:16 -08002158 d3u.appendGlow(zoomPanContainer);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002159
Simon Huntc7ee0662014-11-05 16:44:37 -08002160 // group for the topology
Paul Greysonfcba0e82014-11-13 10:21:16 -08002161 topoG = zoomPanContainer.append('g')
Simon Huntd3b7d512014-11-12 15:48:41 -08002162 .attr('id', 'topo-G')
Simon Huntc7ee0662014-11-05 16:44:37 -08002163 .attr('transform', fcfg.translate());
2164
Simon Hunte2575b62014-11-18 15:25:53 -08002165 // subgroups for links, link labels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002166 linkG = topoG.append('g').attr('id', 'links');
Simon Hunte2575b62014-11-18 15:25:53 -08002167 linkLabelG = topoG.append('g').attr('id', 'linkLabels');
Simon Huntc7ee0662014-11-05 16:44:37 -08002168 nodeG = topoG.append('g').attr('id', 'nodes');
2169
Simon Hunte2575b62014-11-18 15:25:53 -08002170 // selection of links, linkLabels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002171 link = linkG.selectAll('.link');
Simon Hunte2575b62014-11-18 15:25:53 -08002172 linkLabel = linkLabelG.selectAll('.linkLabel');
Simon Huntc7ee0662014-11-05 16:44:37 -08002173 node = nodeG.selectAll('.node');
2174
Simon Hunt7cd48f32014-11-09 23:42:50 -08002175 function chrg(d) {
2176 return fcfg.charge[d.class] || -12000;
2177 }
Simon Hunt99c13842014-11-06 18:23:12 -08002178 function ldist(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002179 return fcfg.linkDistance[d.type] || 50;
Simon Hunt99c13842014-11-06 18:23:12 -08002180 }
2181 function lstrg(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002182 // 0.0 - 1.0
2183 return fcfg.linkStrength[d.type] || 1.0;
Simon Hunt99c13842014-11-06 18:23:12 -08002184 }
2185
Simon Hunt1a9eff92014-11-07 11:06:34 -08002186 function selectCb(d, self) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002187 selectObject(d, self);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002188 }
2189
2190 function atDragEnd(d, self) {
Simon Hunt56d51852014-11-09 13:03:35 -08002191 // once we've finished moving, pin the node in position
2192 d.fixed = true;
2193 d3.select(self).classed('fixed', true);
2194 if (config.useLiveData) {
Simon Hunt902c9922014-11-11 11:59:31 -08002195 sendUpdateMeta(d);
Simon Hunta255a2c2014-11-13 22:29:35 -08002196 } else {
2197 console.log('Moving node ' + d.id + ' to [' + d.x + ',' + d.y + ']');
Simon Hunt1a9eff92014-11-07 11:06:34 -08002198 }
2199 }
2200
Simon Hunt902c9922014-11-11 11:59:31 -08002201 function sendUpdateMeta(d) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002202 sendMessage('updateMeta', {
2203 id: d.id,
2204 'class': d.class,
Simon Hunt902c9922014-11-11 11:59:31 -08002205 'memento': {
Simon Hunt01095ff2014-11-13 16:37:29 -08002206 x: d.x,
2207 y: d.y
Simon Hunt902c9922014-11-11 11:59:31 -08002208 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002209 });
2210 }
2211
Simon Huntc7ee0662014-11-05 16:44:37 -08002212 // set up the force layout
2213 network.force = d3.layout.force()
2214 .size(forceDim)
2215 .nodes(network.nodes)
2216 .links(network.links)
Simon Hunt7cd48f32014-11-09 23:42:50 -08002217 .gravity(0.4)
2218 .friction(0.7)
2219 .charge(chrg)
Simon Hunt99c13842014-11-06 18:23:12 -08002220 .linkDistance(ldist)
2221 .linkStrength(lstrg)
Simon Huntc7ee0662014-11-05 16:44:37 -08002222 .on('tick', tick);
Simon Hunt195cb382014-11-03 17:50:51 -08002223
Simon Hunt01095ff2014-11-13 16:37:29 -08002224 network.drag = d3u.createDragBehavior(network.force,
Simon Huntdeab4322014-11-13 18:49:07 -08002225 selectCb, atDragEnd, panZoom);
Simon Hunt0c6d4192014-11-12 12:07:10 -08002226
2227 // create mask layer for when we lose connection to server.
Simon Hunta5e89142014-11-14 07:00:33 -08002228 // TODO: this should be part of the framework
Simon Hunt0c6d4192014-11-12 12:07:10 -08002229 mask = view.$div.append('div').attr('id','topo-mask');
2230 para(mask, 'Oops!');
2231 para(mask, 'Web-socket connection to server closed...');
2232 para(mask, 'Try refreshing the page.');
Simon Hunt12ce12e2014-11-15 21:13:19 -08002233
2234 mask.append('svg')
2235 .attr({
2236 id: 'mask-bird',
2237 width: w,
2238 height: h
2239 })
2240 .append('g')
2241 .attr('transform', birdTranslate(w, h))
2242 .style('opacity', 0.3)
2243 .append('use')
2244 .attr({
2245 'xlink:href': '#bird',
2246 width: config.birdDim,
2247 height: config.birdDim,
2248 fill: '#111'
Thomas Vachuska89543292014-11-19 11:28:33 -08002249 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08002250 }
Simon Hunt195cb382014-11-03 17:50:51 -08002251
Simon Hunt01095ff2014-11-13 16:37:29 -08002252 function para(sel, text) {
2253 sel.append('p').text(text);
2254 }
2255
2256
Simon Hunt56d51852014-11-09 13:03:35 -08002257 function load(view, ctx, flags) {
Simon Huntf67722a2014-11-10 09:32:06 -08002258 // resize, in case the window was resized while we were not loaded
2259 resize(view, ctx, flags);
2260
Simon Hunt99c13842014-11-06 18:23:12 -08002261 // cache the view token, so network topo functions can access it
2262 network.view = view;
Simon Hunt56d51852014-11-09 13:03:35 -08002263 config.useLiveData = !flags.local;
2264
2265 if (!config.useLiveData) {
2266 prepareScenario(view, ctx, flags.debug);
2267 }
Simon Hunt99c13842014-11-06 18:23:12 -08002268
2269 // set our radio buttons and key bindings
Simon Hunt9462e8c2014-11-14 17:28:09 -08002270 layerBtnSet = view.setRadio(layerButtons);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002271 view.setKeys(keyDispatch);
Simon Hunt195cb382014-11-03 17:50:51 -08002272
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002273 // patch in our "button bar" for now
2274 // TODO: implement a more official frameworky way of doing this..
2275 addButtonBar(view);
2276
Simon Huntd3b7d512014-11-12 15:48:41 -08002277 // Load map data asynchronously; complete startup after that..
2278 loadGeoJsonData();
Simon Hunta255a2c2014-11-13 22:29:35 -08002279
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002280 //var dashIdx = 0;
2281 //antTimer = setInterval(function () {
2282 // // TODO: figure out how to choose Src-->Dst and Dst-->Src, per link
2283 // dashIdx = dashIdx === 0 ? 14 : dashIdx - 2;
2284 // d3.selectAll('.animated').style('stroke-dashoffset', dashIdx);
2285 //}, 35);
Simon Hunta255a2c2014-11-13 22:29:35 -08002286 }
2287
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002288 function startAntTimer() {
2289 var pulses = [ 5, 3, 1.2, 3 ],
2290 pulse = 0;
2291 antTimer = setInterval(function () {
2292 pulse = pulse + 1;
2293 pulse = pulse === pulses.length ? 0 : pulse;
2294 d3.selectAll('.animated').style('stroke-width', pulses[pulse]);
2295 }, 200);
2296 }
2297
2298 function stopAntTimer() {
Simon Hunta255a2c2014-11-13 22:29:35 -08002299 if (antTimer) {
2300 clearInterval(antTimer);
2301 antTimer = null;
2302 }
Simon Huntd3b7d512014-11-12 15:48:41 -08002303 }
2304
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002305 function unload(view, ctx, flags) {
2306 stopAntTimer();
2307 }
2308
Simon Huntd3b7d512014-11-12 15:48:41 -08002309 // TODO: move these to config/state portion of script
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002310 var geoJsonUrl = 'json/map/continental_us.json', // TODO: Paul
Simon Huntd3b7d512014-11-12 15:48:41 -08002311 geoJson;
2312
2313 function loadGeoJsonData() {
2314 d3.json(geoJsonUrl, function (err, data) {
2315 if (err) {
2316 // fall back to USA map background
2317 loadStaticMap();
2318 } else {
2319 geoJson = data;
2320 loadGeoMap();
2321 }
2322
2323 // finally, connect to the server...
2324 if (config.useLiveData) {
2325 webSock.connect();
2326 }
2327 });
2328 }
2329
2330 function showBg() {
2331 return config.options.showBackground ? 'visible' : 'hidden';
2332 }
2333
2334 function loadStaticMap() {
2335 fnTrace('loadStaticMap', config.backgroundUrl);
2336 var w = network.view.width(),
2337 h = network.view.height();
2338
2339 // load the background image
2340 bgImg = svg.insert('svg:image', '#topo-G')
2341 .attr({
2342 id: 'topo-bg',
2343 width: w,
2344 height: h,
2345 'xlink:href': config.backgroundUrl
2346 })
2347 .style({
2348 visibility: showBg()
2349 });
2350 }
2351
2352 function loadGeoMap() {
2353 fnTrace('loadGeoMap', geoJsonUrl);
Simon Huntd3b7d512014-11-12 15:48:41 -08002354
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002355 // extracts the topojson data into geocoordinate-based geometry
2356 var topoData = topojson.feature(geoJson, geoJson.objects.states);
Simon Huntd3b7d512014-11-12 15:48:41 -08002357
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002358 // see: http://bl.ocks.org/mbostock/4707858
2359 geoMapProjection = d3.geo.mercator();
2360 var path = d3.geo.path().projection(geoMapProjection);
Simon Huntd3b7d512014-11-12 15:48:41 -08002361
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002362 geoMapProjection
2363 .scale(1)
2364 .translate([0, 0]);
Simon Huntd3b7d512014-11-12 15:48:41 -08002365
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002366 // [[x1,y1],[x2,y2]]
2367 var b = path.bounds(topoData);
Paul Greysonfcba0e82014-11-13 10:21:16 -08002368 // size map to 95% of minimum dimension to fill space
2369 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 -08002370 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 -08002371
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002372 geoMapProjection
2373 .scale(s)
2374 .translate(t);
2375
Paul Greysonfcba0e82014-11-13 10:21:16 -08002376 bgImg = zoomPanContainer.insert("g", '#topo-G');
Thomas Vachuska89543292014-11-19 11:28:33 -08002377 bgImg.attr('id', 'map').selectAll('path')
2378 .data(topoData.features)
2379 .enter()
2380 .append('path')
2381 .attr('d', path);
Simon Hunt195cb382014-11-03 17:50:51 -08002382 }
2383
Simon Huntf67722a2014-11-10 09:32:06 -08002384 function resize(view, ctx, flags) {
Simon Hunt12ce12e2014-11-15 21:13:19 -08002385 var w = view.width(),
2386 h = view.height();
2387
Simon Hunt934c3ce2014-11-05 11:45:07 -08002388 setSize(svg, view);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002389
2390 d3.select('#mask-bird').attr({ width: w, height: h})
2391 .select('g').attr('transform', birdTranslate(w, h));
Simon Hunt142d0032014-11-04 20:13:09 -08002392 }
2393
Simon Hunt12ce12e2014-11-15 21:13:19 -08002394 function birdTranslate(w, h) {
2395 var bdim = config.birdDim;
2396 return 'translate('+((w-bdim)*.4)+','+((h-bdim)*.1)+')';
2397 }
Simon Hunt142d0032014-11-04 20:13:09 -08002398
2399 // ==============================
2400 // View registration
Simon Hunt195cb382014-11-03 17:50:51 -08002401
Simon Hunt25248912014-11-04 11:25:48 -08002402 onos.ui.addView('topo', {
Simon Hunt142d0032014-11-04 20:13:09 -08002403 preload: preload,
2404 load: load,
Simon Hunta255a2c2014-11-13 22:29:35 -08002405 unload: unload,
Simon Hunt142d0032014-11-04 20:13:09 -08002406 resize: resize
Simon Hunt195cb382014-11-03 17:50:51 -08002407 });
2408
Simon Hunt61d04042014-11-11 17:27:16 -08002409 detailPane = onos.ui.addFloatingPanel('topo-detail');
Simon Hunta5e89142014-11-14 07:00:33 -08002410 oiBox = onos.ui.addFloatingPanel('topo-oibox', 'TL');
Simon Hunt61d04042014-11-11 17:27:16 -08002411
Simon Hunt195cb382014-11-03 17:50:51 -08002412}(ONOS));