blob: 4013adef9e7c6b6ec715be12fdd37fc62033f93a [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
Simon Hunta6a9fe72014-11-20 11:17:12 -080021 @author Thomas Vachuska
Simon Hunt195cb382014-11-03 17:50:51 -080022 */
23
24(function (onos) {
25 'use strict';
26
Simon Hunt1a9eff92014-11-07 11:06:34 -080027 // shorter names for library APIs
Simon Huntbb282f52014-11-10 11:08:19 -080028 var d3u = onos.lib.d3util,
Simon Hunta6a9fe72014-11-20 11:17:12 -080029 gly = onos.lib.glyphs;
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 Hunta3dd9572014-11-20 15:22:41 -0800236 //view.alert('Theme is ' + view.theme());
237 //view.flash('This is some text');
Simon Hunt99c13842014-11-06 18:23:12 -0800238 }
239
Simon Hunt56d51852014-11-09 13:03:35 -0800240 function abortIfLive() {
Simon Hunt50128c02014-11-08 13:36:15 -0800241 if (config.useLiveData) {
Simon Huntb53e0682014-11-12 13:32:01 -0800242 network.view.alert("Sorry, currently using live data..");
Simon Hunt56d51852014-11-09 13:03:35 -0800243 return true;
Simon Hunt50128c02014-11-08 13:36:15 -0800244 }
Simon Hunt56d51852014-11-09 13:03:35 -0800245 return false;
246 }
Simon Hunt50128c02014-11-08 13:36:15 -0800247
Simon Hunt56d51852014-11-09 13:03:35 -0800248 function testDebug(msg) {
249 if (scenario.debug) {
250 scenario.view.alert(msg);
251 }
252 }
Simon Hunt99c13842014-11-06 18:23:12 -0800253
Simon Hunt56d51852014-11-09 13:03:35 -0800254 function injectTestEvent(view) {
255 if (abortIfLive()) { return; }
256 var sc = scenario,
257 evn = ++sc.evNumber,
258 pfx = sc.evDir + sc.ctx + sc.evPrefix + evn,
259 onosUrl = pfx + sc.evOnos,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800260 uiUrl = pfx + sc.evUi,
261 stack = [
262 { url: onosUrl, cb: handleServerEvent },
263 { url: uiUrl, cb: handleUiEvent }
264 ];
265 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800266 }
267
Simon Hunt7cd48f32014-11-09 23:42:50 -0800268 function recurseFetchEvent(stack, evn) {
269 var v = scenario.view,
270 frame;
271 if (stack.length === 0) {
Simon Huntfc274c92014-11-11 11:05:46 -0800272 v.alert('Oops!\n\nNo event #' + evn + ' found.');
Simon Hunt7cd48f32014-11-09 23:42:50 -0800273 return;
274 }
275 frame = stack.shift();
276
277 d3.json(frame.url, function (err, data) {
Simon Hunt99c13842014-11-06 18:23:12 -0800278 if (err) {
Simon Hunt56d51852014-11-09 13:03:35 -0800279 if (err.status === 404) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800280 // if we didn't find the data, try the next stack frame
281 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800282 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800283 v.alert('non-404 error:\n\n' + frame.url + '\n\n' + err);
Simon Hunt56d51852014-11-09 13:03:35 -0800284 }
Simon Hunt99c13842014-11-06 18:23:12 -0800285 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800286 testDebug('loaded: ' + frame.url);
Simon Hunt1712ed82014-11-17 12:56:00 -0800287 wsTrace('test', JSON.stringify(data));
Simon Hunt7cd48f32014-11-09 23:42:50 -0800288 frame.cb(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800289 }
290 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800291
Simon Hunt56d51852014-11-09 13:03:35 -0800292 }
Simon Hunt50128c02014-11-08 13:36:15 -0800293
Simon Hunt56d51852014-11-09 13:03:35 -0800294 function handleUiEvent(data) {
Simon Huntbb282f52014-11-10 11:08:19 -0800295 scenario.view.alert('UI Tx: ' + data.event + '\n\n' +
296 JSON.stringify(data));
Simon Hunt56d51852014-11-09 13:03:35 -0800297 }
298
299 function injectStartupEvents(view) {
300 var last = scenario.params.lastAuto || 0;
301 if (abortIfLive()) { return; }
302
303 while (scenario.evNumber < last) {
Simon Hunt1a9eff92014-11-07 11:06:34 -0800304 injectTestEvent(view);
305 }
306 }
307
Simon Hunt934c3ce2014-11-05 11:45:07 -0800308 function toggleBg() {
309 var vis = bgImg.style('visibility');
310 bgImg.style('visibility', (vis === 'hidden') ? 'visible' : 'hidden');
311 }
312
Simon Hunt99c13842014-11-06 18:23:12 -0800313 function cycleLabels() {
Simon Huntbb282f52014-11-10 11:08:19 -0800314 deviceLabelIndex = (deviceLabelIndex === network.deviceLabelCount - 1)
315 ? 0 : deviceLabelIndex + 1;
Simon Hunt5f36d342014-11-08 21:33:14 -0800316
Simon Hunt99c13842014-11-06 18:23:12 -0800317 network.nodes.forEach(function (d) {
Simon Huntbb282f52014-11-10 11:08:19 -0800318 if (d.class === 'device') {
319 updateDeviceLabel(d);
320 }
Simon Hunt99c13842014-11-06 18:23:12 -0800321 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800322 }
323
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -0800324 function toggleHover(view) {
Thomas Vachuska29617e52014-11-20 03:17:46 -0800325 hoverMode++;
326 if (hoverMode === hoverModes.length) {
327 hoverMode = 0;
328 }
Simon Hunta3dd9572014-11-20 15:22:41 -0800329 view.flash('Hover Mode: ' + hoverModes[hoverMode]);
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -0800330 }
331
Simon Hunt934c3ce2014-11-05 11:45:07 -0800332 function togglePorts(view) {
Simon Hunt50128c02014-11-08 13:36:15 -0800333 view.alert('togglePorts() callback')
Simon Hunt934c3ce2014-11-05 11:45:07 -0800334 }
335
Simon Hunt6ac93f32014-11-13 12:17:27 -0800336 function unpin() {
337 if (hovered) {
338 hovered.fixed = false;
339 hovered.el.classed('fixed', false);
340 network.force.resume();
341 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800342 }
343
Simon Hunt9462e8c2014-11-14 17:28:09 -0800344 function handleEscape(view) {
345 if (oiShowMaster) {
346 cancelAffinity();
347 } else {
348 deselectAll();
349 }
350 }
351
Simon Hunt934c3ce2014-11-05 11:45:07 -0800352 // ==============================
353 // Radio Button Callbacks
354
Simon Hunta5e89142014-11-14 07:00:33 -0800355 var layerLookup = {
356 host: {
357 endstation: 'pkt', // default, if host event does not define type
Thomas Vachuska89543292014-11-19 11:28:33 -0800358 router: 'pkt',
Simon Hunta5e89142014-11-14 07:00:33 -0800359 bgpSpeaker: 'pkt'
360 },
361 device: {
362 switch: 'pkt',
363 roadm: 'opt'
364 },
365 link: {
366 hostLink: 'pkt',
367 direct: 'pkt',
Simon Hunt8257f4c2014-11-16 19:34:54 -0800368 indirect: '',
369 tunnel: '',
Simon Hunta5e89142014-11-14 07:00:33 -0800370 optical: 'opt'
371 }
372 };
373
374 function inLayer(d, layer) {
Simon Hunt8257f4c2014-11-16 19:34:54 -0800375 var type = d.class === 'link' ? d.type() : d.type,
376 look = layerLookup[d.class],
377 lyr = look && look[type];
Simon Hunta5e89142014-11-14 07:00:33 -0800378 return lyr === layer;
379 }
380
381 function unsuppressLayer(which) {
382 node.each(function (d) {
383 var node = d.el;
384 if (inLayer(d, which)) {
385 node.classed('suppressed', false);
386 }
387 });
388
389 link.each(function (d) {
390 var link = d.el;
391 if (inLayer(d, which)) {
392 link.classed('suppressed', false);
393 }
394 });
395 }
396
Simon Hunt9462e8c2014-11-14 17:28:09 -0800397 function suppressLayers(b) {
398 node.classed('suppressed', b);
399 link.classed('suppressed', b);
Simon Hunt142d0032014-11-04 20:13:09 -0800400// d3.selectAll('svg .port').classed('inactive', false);
401// d3.selectAll('svg .portText').classed('inactive', false);
Simon Hunt195cb382014-11-03 17:50:51 -0800402 }
403
Simon Hunt9462e8c2014-11-14 17:28:09 -0800404 function showAllLayers() {
405 suppressLayers(false);
406 }
407
Simon Hunt195cb382014-11-03 17:50:51 -0800408 function showPacketLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800409 node.classed('suppressed', true);
410 link.classed('suppressed', true);
411 unsuppressLayer('pkt');
Simon Hunt195cb382014-11-03 17:50:51 -0800412 }
413
414 function showOpticalLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800415 node.classed('suppressed', true);
416 link.classed('suppressed', true);
417 unsuppressLayer('opt');
Simon Hunt195cb382014-11-03 17:50:51 -0800418 }
419
Simon Hunt9462e8c2014-11-14 17:28:09 -0800420 function restoreLayerState() {
421 layerBtnDispatch[layerBtnSet.selected()]();
422 }
423
Simon Hunt142d0032014-11-04 20:13:09 -0800424 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800425 // Private functions
426
Simon Hunt99c13842014-11-06 18:23:12 -0800427 function safeId(s) {
428 return s.replace(/[^a-z0-9]/gi, '-');
429 }
430
Simon Huntc7ee0662014-11-05 16:44:37 -0800431 // set the size of the given element to that of the view (reduced if padded)
432 function setSize(el, view, pad) {
433 var padding = pad ? pad * 2 : 0;
Simon Hunt934c3ce2014-11-05 11:45:07 -0800434 el.attr({
Simon Huntc7ee0662014-11-05 16:44:37 -0800435 width: view.width() - padding,
436 height: view.height() - padding
Simon Hunt934c3ce2014-11-05 11:45:07 -0800437 });
438 }
439
Simon Hunt8257f4c2014-11-16 19:34:54 -0800440 function makeNodeKey(d, what) {
441 var port = what + 'Port';
442 return d[what] + '/' + d[port];
443 }
444
445 function makeLinkKey(d, flipped) {
446 var one = flipped ? makeNodeKey(d, 'dst') : makeNodeKey(d, 'src'),
447 two = flipped ? makeNodeKey(d, 'src') : makeNodeKey(d, 'dst');
448 return one + '-' + two;
449 }
450
Simon Hunt269670f2014-11-17 16:17:43 -0800451 function findLinkById(id) {
452 // check to see if this is a reverse lookup, else default to given id
453 var key = network.revLinkToKey[id] || id;
454 return key && network.lookup[key];
455 }
456
Simon Hunt8257f4c2014-11-16 19:34:54 -0800457 function findLink(linkData, op) {
458 var key = makeLinkKey(linkData),
459 keyrev = makeLinkKey(linkData, 1),
460 link = network.lookup[key],
461 linkRev = network.lookup[keyrev],
462 result = {},
463 ldata = link || linkRev,
464 rawLink;
465
466 if (op === 'add') {
467 if (link) {
468 // trying to add a link that we already know about
469 result.ldata = link;
470 result.badLogic = 'addLink: link already added';
471
472 } else if (linkRev) {
473 // we found the reverse of the link to be added
474 result.ldata = linkRev;
475 if (linkRev.fromTarget) {
476 result.badLogic = 'addLink: link already added';
477 }
478 }
479 } else if (op === 'update') {
480 if (!ldata) {
481 result.badLogic = 'updateLink: link not found';
482 } else {
483 rawLink = link ? ldata.fromSource : ldata.fromTarget;
484 result.updateWith = function (data) {
485 $.extend(rawLink, data);
486 restyleLinkElement(ldata);
487 }
488 }
489 } else if (op === 'remove') {
490 if (!ldata) {
491 result.badLogic = 'removeLink: link not found';
492 } else {
493 rawLink = link ? ldata.fromSource : ldata.fromTarget;
494
495 if (!rawLink) {
496 result.badLogic = 'removeLink: link not found';
497
498 } else {
499 result.removeRawLink = function () {
500 if (link) {
501 // remove fromSource
502 ldata.fromSource = null;
503 if (ldata.fromTarget) {
504 // promote target into source position
505 ldata.fromSource = ldata.fromTarget;
506 ldata.fromTarget = null;
507 ldata.key = keyrev;
508 delete network.lookup[key];
509 network.lookup[keyrev] = ldata;
Simon Hunt269670f2014-11-17 16:17:43 -0800510 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800511 }
512 } else {
513 // remove fromTarget
514 ldata.fromTarget = null;
Simon Hunt269670f2014-11-17 16:17:43 -0800515 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800516 }
517 if (ldata.fromSource) {
518 restyleLinkElement(ldata);
519 } else {
520 removeLinkElement(ldata);
521 }
522 }
523 }
524 }
525 }
526 return result;
527 }
528
529 function addLinkUpdate(ldata, link) {
530 // add link event, but we already have the reverse link installed
531 ldata.fromTarget = link;
Simon Hunt269670f2014-11-17 16:17:43 -0800532 network.revLinkToKey[link.id] = ldata.key;
Simon Hunt8257f4c2014-11-16 19:34:54 -0800533 restyleLinkElement(ldata);
534 }
535
536 var allLinkTypes = 'direct indirect optical tunnel',
537 defaultLinkType = 'direct';
538
539 function restyleLinkElement(ldata) {
540 // this fn's job is to look at raw links and decide what svg classes
541 // need to be applied to the line element in the DOM
542 var el = ldata.el,
543 type = ldata.type(),
544 lw = ldata.linkWidth(),
545 online = ldata.online();
546
547 el.classed('link', true);
548 el.classed('inactive', !online);
549 el.classed(allLinkTypes, false);
550 if (type) {
551 el.classed(type, true);
552 }
553 el.transition()
554 .duration(1000)
Thomas Vachuska89543292014-11-19 11:28:33 -0800555 .attr('stroke-width', linkScale(lw))
556 .attr('stroke', config.topo.linkBaseColor);
Simon Hunt8257f4c2014-11-16 19:34:54 -0800557 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800558
Simon Hunt99c13842014-11-06 18:23:12 -0800559 // ==============================
560 // Event handlers for server-pushed events
561
Simon Huntbb282f52014-11-10 11:08:19 -0800562 function logicError(msg) {
563 // TODO, report logic error to server, via websock, so it can be logged
Simon Huntcb56cff2014-11-17 11:42:26 -0800564 //network.view.alert('Logic Error:\n\n' + msg);
Simon Huntfc274c92014-11-11 11:05:46 -0800565 console.warn(msg);
Simon Huntbb282f52014-11-10 11:08:19 -0800566 }
567
Simon Hunt99c13842014-11-06 18:23:12 -0800568 var eventDispatch = {
Simon Hunta5e89142014-11-14 07:00:33 -0800569 addInstance: addInstance,
Simon Hunt99c13842014-11-06 18:23:12 -0800570 addDevice: addDevice,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800571 addLink: addLink,
Simon Hunt56d51852014-11-09 13:03:35 -0800572 addHost: addHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800573
Simon Huntfcfb46c2014-11-19 12:53:38 -0800574 updateInstance: updateInstance,
Simon Huntbb282f52014-11-10 11:08:19 -0800575 updateDevice: updateDevice,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800576 updateLink: updateLink,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800577 updateHost: updateHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800578
Simon Huntd72bc702014-11-13 18:38:04 -0800579 removeInstance: stillToImplement,
Simon Huntbb282f52014-11-10 11:08:19 -0800580 removeDevice: stillToImplement,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800581 removeLink: removeLink,
Simon Hunt44031102014-11-11 13:20:36 -0800582 removeHost: removeHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800583
Simon Hunt61d04042014-11-11 17:27:16 -0800584 showDetails: showDetails,
Simon Huntb53e0682014-11-12 13:32:01 -0800585 showTraffic: showTraffic
Simon Hunt99c13842014-11-06 18:23:12 -0800586 };
587
Simon Hunta5e89142014-11-14 07:00:33 -0800588 function addInstance(data) {
589 evTrace(data);
590 var inst = data.payload,
591 id = inst.id;
592 if (onosInstances[id]) {
593 logicError('ONOS instance already added: ' + id);
594 return;
595 }
596 onosInstances[id] = inst;
597 onosOrder.push(inst);
598 updateInstances();
599 }
600
Simon Hunt99c13842014-11-06 18:23:12 -0800601 function addDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800602 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800603 var device = data.payload,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800604 nodeData = createDeviceNode(device);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800605 network.nodes.push(nodeData);
606 network.lookup[nodeData.id] = nodeData;
Simon Hunt99c13842014-11-06 18:23:12 -0800607 updateNodes();
608 network.force.start();
609 }
610
Simon Hunt99c13842014-11-06 18:23:12 -0800611 function addLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800612 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800613 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800614 result = findLink(link, 'add'),
615 bad = result.badLogic,
616 ldata = result.ldata;
617
618 if (bad) {
619 logicError(bad + ': ' + link.id);
620 return;
621 }
622
623 if (ldata) {
624 // we already have a backing store link for src/dst nodes
625 addLinkUpdate(ldata, link);
626 return;
627 }
628
629 // no backing store link yet
630 ldata = createLink(link);
631 if (ldata) {
632 network.links.push(ldata);
633 network.lookup[ldata.key] = ldata;
Simon Hunt99c13842014-11-06 18:23:12 -0800634 updateLinks();
635 network.force.start();
636 }
637 }
638
Simon Hunt56d51852014-11-09 13:03:35 -0800639 function addHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800640 evTrace(data);
Simon Hunt56d51852014-11-09 13:03:35 -0800641 var host = data.payload,
642 node = createHostNode(host),
643 lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800644 network.nodes.push(node);
645 network.lookup[host.id] = node;
646 updateNodes();
647
648 lnk = createHostLink(host);
649 if (lnk) {
Simon Hunt44031102014-11-11 13:20:36 -0800650 node.linkData = lnk; // cache ref on its host
Simon Hunt56d51852014-11-09 13:03:35 -0800651 network.links.push(lnk);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800652 network.lookup[host.ingress] = lnk;
653 network.lookup[host.egress] = lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800654 updateLinks();
655 }
656 network.force.start();
657 }
658
Simon Hunt44031102014-11-11 13:20:36 -0800659 // TODO: fold updateX(...) methods into one base method; remove duplication
Simon Hunt56a2ea42014-11-19 12:39:31 -0800660
661 function updateInstance(data) {
662 evTrace(data);
663 var inst = data.payload,
664 id = inst.id,
665 instData = onosInstances[id];
666 if (instData) {
667 $.extend(instData, inst);
668 updateInstances();
Simon Hunt56a2ea42014-11-19 12:39:31 -0800669 } else {
670 logicError('updateInstance lookup fail. ID = "' + id + '"');
671 }
672 }
673
Simon Huntbb282f52014-11-10 11:08:19 -0800674 function updateDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800675 evTrace(data);
Simon Huntbb282f52014-11-10 11:08:19 -0800676 var device = data.payload,
677 id = device.id,
678 nodeData = network.lookup[id];
679 if (nodeData) {
680 $.extend(nodeData, device);
681 updateDeviceState(nodeData);
682 } else {
683 logicError('updateDevice lookup fail. ID = "' + id + '"');
684 }
685 }
686
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800687 function updateLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800688 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800689 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800690 result = findLink(link, 'update'),
691 bad = result.badLogic;
692 if (bad) {
693 logicError(bad + ': ' + link.id);
694 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800695 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800696 result.updateWith(link);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800697 }
698
Simon Hunt7cd48f32014-11-09 23:42:50 -0800699 function updateHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800700 evTrace(data);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800701 var host = data.payload,
Simon Huntbb282f52014-11-10 11:08:19 -0800702 id = host.id,
703 hostData = network.lookup[id];
704 if (hostData) {
705 $.extend(hostData, host);
706 updateHostState(hostData);
707 } else {
708 logicError('updateHost lookup fail. ID = "' + id + '"');
709 }
Simon Hunt7cd48f32014-11-09 23:42:50 -0800710 }
711
Simon Hunt44031102014-11-11 13:20:36 -0800712 // TODO: fold removeX(...) methods into base method - remove dup code
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800713 function removeLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800714 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800715 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800716 result = findLink(link, 'remove'),
717 bad = result.badLogic;
718 if (bad) {
719 logicError(bad + ': ' + link.id);
720 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800721 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800722 result.removeRawLink();
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800723 }
724
Simon Hunt44031102014-11-11 13:20:36 -0800725 function removeHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800726 evTrace(data);
Simon Hunt44031102014-11-11 13:20:36 -0800727 var host = data.payload,
728 id = host.id,
729 hostData = network.lookup[id];
730 if (hostData) {
731 removeHostElement(hostData);
732 } else {
733 logicError('removeHost lookup fail. ID = "' + id + '"');
734 }
735 }
736
Simon Hunt61d04042014-11-11 17:27:16 -0800737 function showDetails(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800738 evTrace(data);
Simon Hunt61d04042014-11-11 17:27:16 -0800739 populateDetails(data.payload);
740 detailPane.show();
741 }
742
Simon Huntb53e0682014-11-12 13:32:01 -0800743 function showTraffic(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800744 evTrace(data);
Thomas Vachuska4731f122014-11-20 04:56:19 -0800745 var paths = data.payload.paths,
746 hasTraffic = false;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800747
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800748 // Revert any links hilighted previously.
Thomas Vachuska4731f122014-11-20 04:56:19 -0800749 link.style('stroke-width', null)
Thomas Vachuskaa3148a72014-11-19 21:38:35 -0800750 .classed('primary secondary animated optical', false);
Simon Hunte2575b62014-11-18 15:25:53 -0800751 // Remove all previous labels.
752 removeLinkLabels();
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800753
Simon Hunte2575b62014-11-18 15:25:53 -0800754 // Now hilight all links in the paths payload, and attach
755 // labels to them, if they are defined.
Simon Hunta255a2c2014-11-13 22:29:35 -0800756 paths.forEach(function (p) {
Simon Hunte2575b62014-11-18 15:25:53 -0800757 var n = p.links.length,
758 i,
759 ldata;
760
Thomas Vachuska4731f122014-11-20 04:56:19 -0800761 hasTraffic = hasTraffic || p.traffic;
Simon Hunte2575b62014-11-18 15:25:53 -0800762 for (i=0; i<n; i++) {
763 ldata = findLinkById(p.links[i]);
Thomas Vachuska4731f122014-11-20 04:56:19 -0800764 if (ldata && ldata.el) {
Simon Hunte2575b62014-11-18 15:25:53 -0800765 ldata.el.classed(p.class, true);
766 ldata.label = p.labels[i];
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800767 }
Simon Hunte2575b62014-11-18 15:25:53 -0800768 }
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800769 });
Thomas Vachuska4731f122014-11-20 04:56:19 -0800770
Simon Hunte2575b62014-11-18 15:25:53 -0800771 updateLinks();
Thomas Vachuska4731f122014-11-20 04:56:19 -0800772
773 if (hasTraffic && !antTimer) {
774 startAntTimer();
775 } else if (!hasTraffic && antTimer) {
776 stopAntTimer();
777 }
Simon Huntb53e0682014-11-12 13:32:01 -0800778 }
779
Simon Hunt56d51852014-11-09 13:03:35 -0800780 // ...............................
781
782 function stillToImplement(data) {
783 var p = data.payload;
784 note(data.event, p.id);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800785 network.view.alert('Not yet implemented: "' + data.event + '"');
Simon Hunt56d51852014-11-09 13:03:35 -0800786 }
Simon Hunt99c13842014-11-06 18:23:12 -0800787
788 function unknownEvent(data) {
Simon Hunt50128c02014-11-08 13:36:15 -0800789 network.view.alert('Unknown event type: "' + data.event + '"');
Simon Hunt99c13842014-11-06 18:23:12 -0800790 }
791
792 function handleServerEvent(data) {
793 var fn = eventDispatch[data.event] || unknownEvent;
794 fn(data);
795 }
796
797 // ==============================
Simon Hunt61d04042014-11-11 17:27:16 -0800798 // Out-going messages...
799
Simon Huntb53e0682014-11-12 13:32:01 -0800800 function userFeedback(msg) {
801 // for now, use the alert pane as is. Maybe different alert style in
802 // the future (centered on view; dismiss button?)
803 network.view.alert(msg);
804 }
805
806 function nSel() {
807 return selectOrder.length;
808 }
Simon Hunt61d04042014-11-11 17:27:16 -0800809 function getSel(idx) {
810 return selections[selectOrder[idx]];
811 }
Simon Huntb53e0682014-11-12 13:32:01 -0800812 function getSelId(idx) {
813 return getSel(idx).obj.id;
814 }
815 function allSelectionsClass(cls) {
816 for (var i=0, n=nSel(); i<n; i++) {
817 if (getSel(i).obj.class !== cls) {
818 return false;
819 }
820 }
821 return true;
822 }
Simon Hunt61d04042014-11-11 17:27:16 -0800823
Simon Hunt61d04042014-11-11 17:27:16 -0800824 // request details for the selected element
Simon Huntd72bc702014-11-13 18:38:04 -0800825 // invoked from selection of a single node.
Simon Hunt61d04042014-11-11 17:27:16 -0800826 function requestDetails() {
827 var data = getSel(0).obj,
828 payload = {
829 id: data.id,
830 class: data.class
831 };
832 sendMessage('requestDetails', payload);
833 }
834
Simon Huntd72bc702014-11-13 18:38:04 -0800835 function addIntentAction() {
836 sendMessage('addHostIntent', {
837 one: getSelId(0),
Thomas Vachuska82f2c622014-11-17 12:23:18 -0800838 two: getSelId(1),
839 ids: [ getSelId(0), getSelId(1) ]
Simon Huntd72bc702014-11-13 18:38:04 -0800840 });
841 }
842
843 function showTrafficAction() {
Thomas Vachuska29617e52014-11-20 03:17:46 -0800844 // force intents hover mode
845 hoverMode = 1;
846 showSelectTraffic();
847 }
848
849 function showSelectTraffic() {
Simon Huntd72bc702014-11-13 18:38:04 -0800850 // if nothing is hovered over, and nothing selected, send cancel request
851 if (!hovered && nSel() === 0) {
852 sendMessage('cancelTraffic', {});
853 return;
854 }
855
856 // NOTE: hover is only populated if "show traffic on hover" is
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800857 // toggled on, and the item hovered is a host or a device...
858 var hoverId = (trafficHover() && hovered &&
859 (hovered.class === 'host' || hovered.class === 'device'))
Simon Huntd72bc702014-11-13 18:38:04 -0800860 ? hovered.id : '';
861 sendMessage('requestTraffic', {
862 ids: selectOrder,
863 hover: hoverId
864 });
865 }
866
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800867 function showAllTrafficAction() {
868 sendMessage('requestAllTraffic', {});
869 }
870
Thomas Vachuska29617e52014-11-20 03:17:46 -0800871 function showDeviceLinkFlowsAction() {
872 // force intents hover mode
873 hoverMode = 2;
874 showDeviceLinkFlows();
875 }
876
877 function showDeviceLinkFlows() {
878 // if nothing is hovered over, and nothing selected, send cancel request
879 if (!hovered && nSel() === 0) {
880 sendMessage('cancelTraffic', {});
881 return;
882 }
883 var hoverId = (flowsHover() && hovered && hovered.class === 'device') ?
884 hovered.id : '';
885 sendMessage('requestDeviceLinkFlows', {
886 ids: selectOrder,
887 hover: hoverId
888 });
889 }
Simon Huntd72bc702014-11-13 18:38:04 -0800890
Simon Hunta6a9fe72014-11-20 11:17:12 -0800891 // TODO: these should be moved out to utility module.
Simon Hunt95908012014-11-20 10:20:26 -0800892 function stripPx(s) {
893 return s.replace(/px$/,'');
894 }
Simon Hunta6a9fe72014-11-20 11:17:12 -0800895
896 function appendGlyph(svg, ox, oy, dim, iid) {
897 svg.append('use').attr({
898 class: 'glyphIcon',
899 transform: translate(ox,oy),
900 'xlink:href': iid,
901 width: dim,
902 height: dim
903
904 });
905 }
906
Simon Hunt61d04042014-11-11 17:27:16 -0800907 // ==============================
Simon Hunta5e89142014-11-14 07:00:33 -0800908 // onos instance panel functions
909
910 function updateInstances() {
911 var onoses = oiBox.el.selectAll('.onosInst')
912 .data(onosOrder, function (d) { return d.id; });
913
914 // operate on existing onoses if necessary
Simon Huntfcfb46c2014-11-19 12:53:38 -0800915 onoses.classed('online', function (d) { return d.online; });
Simon Hunta5e89142014-11-14 07:00:33 -0800916
917 var entering = onoses.enter()
918 .append('div')
919 .attr('class', 'onosInst')
920 .classed('online', function (d) { return d.online; })
Simon Hunt9c15eca2014-11-15 18:37:59 -0800921 .on('click', clickInst);
922
923 entering.each(function (d, i) {
924 var el = d3.select(this),
925 img;
Simon Hunt95908012014-11-20 10:20:26 -0800926 var css = window.getComputedStyle(this),
927 w = stripPx(css.width),
928 h = stripPx(css.height) / 2;
Simon Hunt9c15eca2014-11-15 18:37:59 -0800929
Simon Hunt95908012014-11-20 10:20:26 -0800930 var svg = el.append('svg').attr({
931 width: w,
932 height: h
933 });
934 var dim = 30;
Simon Hunta6a9fe72014-11-20 11:17:12 -0800935 appendGlyph(svg, 2, 2, 30, '#node');
Simon Hunt9c15eca2014-11-15 18:37:59 -0800936
937 $('<div>').attr('class', 'onosTitle').text(d.id).appendTo(el);
938
939 // is the UI attached to this instance?
940 // TODO: need uiAttached boolean in instance data
Simon Hunta6a9fe72014-11-20 11:17:12 -0800941 // TODO: use SVG glyph, not png..
Simon Hunt9c15eca2014-11-15 18:37:59 -0800942 //if (d.uiAttached) {
943 if (i === 0) {
944 $('<img src="img/ui.png">').attr('class','ui').appendTo(el);
945 }
946 });
Simon Hunta5e89142014-11-14 07:00:33 -0800947
948 // operate on existing + new onoses here
949
950 // the departed...
951 var exiting = onoses.exit()
952 .transition()
953 .style('opacity', 0)
954 .remove();
955 }
956
Simon Hunt9462e8c2014-11-14 17:28:09 -0800957 function clickInst(d) {
958 var el = d3.select(this),
959 aff = el.classed('affinity');
960 if (!aff) {
961 setAffinity(el, d);
962 } else {
963 cancelAffinity();
964 }
965 }
966
967 function setAffinity(el, d) {
968 d3.selectAll('.onosInst')
969 .classed('mastership', true)
970 .classed('affinity', false);
971 el.classed('affinity', true);
972
973 suppressLayers(true);
974 node.each(function (n) {
975 if (n.master === d.id) {
976 n.el.classed('suppressed', false);
977 }
978 });
979 oiShowMaster = true;
980 }
981
982 function cancelAffinity() {
983 d3.selectAll('.onosInst')
984 .classed('mastership affinity', false);
985 restoreLayerState();
986 oiShowMaster = false;
987 }
988
Simon Hunta5e89142014-11-14 07:00:33 -0800989 // ==============================
Simon Hunt99c13842014-11-06 18:23:12 -0800990 // force layout modification functions
991
992 function translate(x, y) {
993 return 'translate(' + x + ',' + y + ')';
994 }
995
Simon Hunte2575b62014-11-18 15:25:53 -0800996 function rotate(deg) {
997 return 'rotate(' + deg + ')';
998 }
999
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001000 function missMsg(what, id) {
1001 return '\n[' + what + '] "' + id + '" missing ';
1002 }
1003
1004 function linkEndPoints(srcId, dstId) {
1005 var srcNode = network.lookup[srcId],
1006 dstNode = network.lookup[dstId],
1007 sMiss = !srcNode ? missMsg('src', srcId) : '',
1008 dMiss = !dstNode ? missMsg('dst', dstId) : '';
1009
1010 if (sMiss || dMiss) {
1011 logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
1012 return null;
1013 }
1014 return {
1015 source: srcNode,
1016 target: dstNode,
1017 x1: srcNode.x,
1018 y1: srcNode.y,
1019 x2: dstNode.x,
1020 y2: dstNode.y
1021 };
1022 }
1023
Simon Hunt56d51852014-11-09 13:03:35 -08001024 function createHostLink(host) {
1025 var src = host.id,
1026 dst = host.cp.device,
Simon Hunt7cd48f32014-11-09 23:42:50 -08001027 id = host.ingress,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001028 lnk = linkEndPoints(src, dst);
Simon Hunt56d51852014-11-09 13:03:35 -08001029
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001030 if (!lnk) {
Simon Hunt56d51852014-11-09 13:03:35 -08001031 return null;
1032 }
1033
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001034 // Synthesize link ...
1035 $.extend(lnk, {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001036 key: id,
Simon Hunt56d51852014-11-09 13:03:35 -08001037 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -08001038
1039 type: function () { return 'hostLink'; },
1040 // TODO: ideally, we should see if our edge switch is online...
1041 online: function () { return true; },
1042 linkWidth: function () { return 1; }
Simon Hunt7cd48f32014-11-09 23:42:50 -08001043 });
Simon Hunt99c13842014-11-06 18:23:12 -08001044 return lnk;
1045 }
1046
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001047 function createLink(link) {
Simon Hunta6a9fe72014-11-20 11:17:12 -08001048 var lnk = linkEndPoints(link.src, link.dst);
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001049
1050 if (!lnk) {
1051 return null;
1052 }
1053
Simon Hunt8257f4c2014-11-16 19:34:54 -08001054 $.extend(lnk, {
1055 key: link.id,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001056 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -08001057 fromSource: link,
1058
1059 // functions to aggregate dual link state
1060 type: function () {
1061 var s = lnk.fromSource,
1062 t = lnk.fromTarget;
1063 return (s && s.type) || (t && t.type) || defaultLinkType;
1064 },
1065 online: function () {
1066 var s = lnk.fromSource,
1067 t = lnk.fromTarget;
1068 return (s && s.online) || (t && t.online);
1069 },
1070 linkWidth: function () {
1071 var s = lnk.fromSource,
1072 t = lnk.fromTarget,
1073 ws = (s && s.linkWidth) || 0,
1074 wt = (t && t.linkWidth) || 0;
1075 return Math.max(ws, wt);
1076 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001077 });
1078 return lnk;
Simon Hunt1a9eff92014-11-07 11:06:34 -08001079 }
1080
Simon Hunte2575b62014-11-18 15:25:53 -08001081 function removeLinkLabels() {
1082 network.links.forEach(function (d) {
1083 d.label = '';
1084 });
1085 }
1086
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001087 var widthRatio = 1.4,
1088 linkScale = d3.scale.linear()
1089 .domain([1, 12])
1090 .range([widthRatio, 12 * widthRatio])
1091 .clamp(true);
1092
Simon Hunt99c13842014-11-06 18:23:12 -08001093 function updateLinks() {
1094 link = linkG.selectAll('.link')
Simon Hunt8257f4c2014-11-16 19:34:54 -08001095 .data(network.links, function (d) { return d.key; });
Simon Hunt99c13842014-11-06 18:23:12 -08001096
1097 // operate on existing links, if necessary
1098 // link .foo() .bar() ...
1099
1100 // operate on entering links:
1101 var entering = link.enter()
1102 .append('line')
1103 .attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001104 x1: function (d) { return d.x1; },
1105 y1: function (d) { return d.y1; },
1106 x2: function (d) { return d.x2; },
1107 y2: function (d) { return d.y2; },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001108 stroke: config.topo.linkInColor,
1109 'stroke-width': config.topo.linkInWidth
Simon Hunt99c13842014-11-06 18:23:12 -08001110 });
1111
1112 // augment links
Simon Hunt7cd48f32014-11-09 23:42:50 -08001113 entering.each(function (d) {
1114 var link = d3.select(this);
1115 // provide ref to element selection from backing data....
1116 d.el = link;
Simon Hunt8257f4c2014-11-16 19:34:54 -08001117 restyleLinkElement(d);
Simon Hunt7cd48f32014-11-09 23:42:50 -08001118 });
Thomas Vachuska4830d392014-11-09 17:09:56 -08001119
1120 // operate on both existing and new links, if necessary
1121 //link .foo() .bar() ...
1122
Simon Hunte2575b62014-11-18 15:25:53 -08001123 // apply or remove labels
1124 var labelData = getLabelData();
1125 applyLinkLabels(labelData);
1126
Thomas Vachuska4830d392014-11-09 17:09:56 -08001127 // operate on exiting links:
Thomas Vachuska4830d392014-11-09 17:09:56 -08001128 link.exit()
Simon Hunt13bf9c82014-11-18 07:26:44 -08001129 .attr('stroke-dasharray', '3, 3')
1130 .style('opacity', 0.5)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001131 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -08001132 .duration(1500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001133 .attr({
Simon Hunt13bf9c82014-11-18 07:26:44 -08001134 'stroke-dasharray': '3, 12',
1135 stroke: config.topo.linkOutColor,
1136 'stroke-width': config.topo.linkOutWidth
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001137 })
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001138 .style('opacity', 0.0)
Thomas Vachuska4830d392014-11-09 17:09:56 -08001139 .remove();
Simon Hunte2575b62014-11-18 15:25:53 -08001140
1141 // NOTE: invoke a single tick to force the labels to position
1142 // onto their links.
1143 tick();
1144 }
1145
1146 function getLabelData() {
1147 // create the backing data for showing labels..
1148 var data = [];
1149 link.each(function (d) {
1150 if (d.label) {
1151 data.push({
1152 id: 'lab-' + d.key,
1153 key: d.key,
1154 label: d.label,
1155 ldata: d
1156 });
1157 }
1158 });
1159 return data;
1160 }
1161
1162 var linkLabelOffset = '0.3em';
1163
1164 function applyLinkLabels(data) {
1165 var entering;
1166
1167 linkLabel = linkLabelG.selectAll('.linkLabel')
1168 .data(data, function (d) { return d.id; });
1169
Simon Hunt56a2ea42014-11-19 12:39:31 -08001170 // for elements already existing, we need to update the text
1171 // and adjust the rectangle size to fit
1172 linkLabel.each(function (d) {
1173 var el = d3.select(this),
1174 rect = el.select('rect'),
1175 text = el.select('text');
1176 text.text(d.label);
1177 rect.attr(rectAroundText(el));
1178 });
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -08001179
Simon Hunte2575b62014-11-18 15:25:53 -08001180 entering = linkLabel.enter().append('g')
1181 .classed('linkLabel', true)
1182 .attr('id', function (d) { return d.id; });
1183
1184 entering.each(function (d) {
1185 var el = d3.select(this),
1186 rect,
1187 text,
1188 parms = {
1189 x1: d.ldata.x1,
1190 y1: d.ldata.y1,
1191 x2: d.ldata.x2,
1192 y2: d.ldata.y2
1193 };
1194
1195 d.el = el;
1196 rect = el.append('rect');
1197 text = el.append('text').text(d.label);
1198 rect.attr(rectAroundText(el));
1199 text.attr('dy', linkLabelOffset);
1200
1201 el.attr('transform', transformLabel(parms));
1202 });
1203
1204 // Remove any links that are no longer required.
1205 linkLabel.exit().remove();
1206 }
1207
1208 function rectAroundText(el) {
1209 var text = el.select('text'),
1210 box = text.node().getBBox();
1211
1212 // translate the bbox so that it is centered on [x,y]
1213 box.x = -box.width / 2;
1214 box.y = -box.height / 2;
1215
1216 // add padding
1217 box.x -= 1;
1218 box.width += 2;
1219 return box;
1220 }
1221
1222 function transformLabel(p) {
1223 var dx = p.x2 - p.x1,
1224 dy = p.y2 - p.y1,
1225 xMid = dx/2 + p.x1,
1226 yMid = dy/2 + p.y1;
Simon Hunte2575b62014-11-18 15:25:53 -08001227 return translate(xMid, yMid);
Simon Hunt99c13842014-11-06 18:23:12 -08001228 }
1229
1230 function createDeviceNode(device) {
1231 // start with the object as is
1232 var node = device,
Simon Huntbb282f52014-11-10 11:08:19 -08001233 type = device.type,
Simon Huntc72967b2014-11-20 09:21:42 -08001234 svgCls = type ? 'node device ' + type : 'node device',
1235 labels = device.labels || [];
1236
1237 labels.unshift(''); // add 'no-label' to front of cycle
Simon Hunt99c13842014-11-06 18:23:12 -08001238
1239 // Augment as needed...
1240 node.class = 'device';
Simon Huntbb282f52014-11-10 11:08:19 -08001241 node.svgClass = device.online ? svgCls + ' online' : svgCls;
Simon Hunt99c13842014-11-06 18:23:12 -08001242 positionNode(node);
1243
1244 // cache label array length
Simon Huntc72967b2014-11-20 09:21:42 -08001245 // TODO: need a uiConfig event from the server to set things
1246 // like device labels count, host labels count, etc.
1247 // The current method (here) is a little fragile
Simon Hunt99c13842014-11-06 18:23:12 -08001248 network.deviceLabelCount = device.labels.length;
Simon Hunt99c13842014-11-06 18:23:12 -08001249 return node;
1250 }
1251
Simon Hunt56d51852014-11-09 13:03:35 -08001252 function createHostNode(host) {
1253 // start with the object as is
1254 var node = host;
1255
1256 // Augment as needed...
1257 node.class = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001258 if (!node.type) {
Simon Hunteb1514d2014-11-20 09:57:29 -08001259 node.type = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001260 }
Simon Hunt7fa116d2014-11-17 14:16:55 -08001261 node.svgClass = 'node host ' + node.type;
Simon Hunt56d51852014-11-09 13:03:35 -08001262 positionNode(node);
1263
1264 // cache label array length
1265 network.hostLabelCount = host.labels.length;
Simon Hunt56d51852014-11-09 13:03:35 -08001266 return node;
1267 }
1268
Simon Hunt99c13842014-11-06 18:23:12 -08001269 function positionNode(node) {
1270 var meta = node.metaUi,
Simon Huntac9e24f2014-11-12 10:12:21 -08001271 x = meta && meta.x,
1272 y = meta && meta.y,
1273 xy;
Simon Hunt99c13842014-11-06 18:23:12 -08001274
Simon Huntac9e24f2014-11-12 10:12:21 -08001275 // If we have [x,y] already, use that...
Simon Hunt99c13842014-11-06 18:23:12 -08001276 if (x && y) {
1277 node.fixed = true;
Simon Huntac9e24f2014-11-12 10:12:21 -08001278 node.x = x;
1279 node.y = y;
1280 return;
Simon Hunt99c13842014-11-06 18:23:12 -08001281 }
Simon Huntac9e24f2014-11-12 10:12:21 -08001282
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001283 var location = node.location;
1284 if (location && location.type === 'latlng') {
1285 var coord = geoMapProjection([location.lng, location.lat]);
1286 node.fixed = true;
1287 node.x = coord[0];
1288 node.y = coord[1];
1289 return;
1290 }
1291
Simon Huntac9e24f2014-11-12 10:12:21 -08001292 // Note: Placing incoming unpinned nodes at exactly the same point
1293 // (center of the view) causes them to explode outwards when
1294 // the force layout kicks in. So, we spread them out a bit
1295 // initially, to provide a more serene layout convergence.
1296 // Additionally, if the node is a host, we place it near
1297 // the device it is connected to.
1298
1299 function spread(s) {
1300 return Math.floor((Math.random() * s) - s/2);
1301 }
1302
1303 function randDim(dim) {
1304 return dim / 2 + spread(dim * 0.7071);
1305 }
1306
1307 function rand() {
1308 return {
1309 x: randDim(network.view.width()),
1310 y: randDim(network.view.height())
1311 };
1312 }
1313
1314 function near(node) {
1315 var min = 12,
1316 dx = spread(12),
1317 dy = spread(12);
1318 return {
1319 x: node.x + min + dx,
1320 y: node.y + min + dy
1321 };
1322 }
1323
1324 function getDevice(cp) {
1325 var d = network.lookup[cp.device];
1326 return d || rand();
1327 }
1328
1329 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
1330 $.extend(node, xy);
Simon Hunt99c13842014-11-06 18:23:12 -08001331 }
1332
Thomas Vachuska89543292014-11-19 11:28:33 -08001333 function iconUrl(d) {
1334 return 'img/' + d.type + '.png';
Simon Hunt99c13842014-11-06 18:23:12 -08001335 }
1336
Simon Huntc72967b2014-11-20 09:21:42 -08001337 function iconGlyphUrl(d) {
1338 var which = d.type || 'unknown';
1339 return '#' + which;
1340 }
1341
Simon Hunt99c13842014-11-06 18:23:12 -08001342 // returns the newly computed bounding box of the rectangle
1343 function adjustRectToFitText(n) {
1344 var text = n.select('text'),
1345 box = text.node().getBBox(),
1346 lab = config.labels;
1347
1348 text.attr('text-anchor', 'middle')
1349 .attr('y', '-0.8em')
1350 .attr('x', lab.imgPad/2);
1351
1352 // translate the bbox so that it is centered on [x,y]
1353 box.x = -box.width / 2;
1354 box.y = -box.height / 2;
1355
1356 // add padding
1357 box.x -= (lab.padLR + lab.imgPad/2);
1358 box.width += lab.padLR * 2 + lab.imgPad;
1359 box.y -= lab.padTB;
1360 box.height += lab.padTB * 2;
1361
1362 return box;
1363 }
1364
Simon Hunt1a9eff92014-11-07 11:06:34 -08001365 function mkSvgClass(d) {
1366 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
1367 }
1368
Simon Hunt7cd48f32014-11-09 23:42:50 -08001369 function hostLabel(d) {
1370 var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0;
1371 return d.labels[idx];
1372 }
1373 function deviceLabel(d) {
1374 var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0;
1375 return d.labels[idx];
1376 }
Simon Huntc72967b2014-11-20 09:21:42 -08001377 function trimLabel(label) {
1378 return (label && label.trim()) || '';
1379 }
1380
1381 function emptyBox() {
1382 return {
1383 x: -2,
1384 y: -2,
1385 width: 4,
1386 height: 4
1387 };
Simon Hunt7cd48f32014-11-09 23:42:50 -08001388 }
1389
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001390 function updateDeviceLabel(d) {
Simon Huntc72967b2014-11-20 09:21:42 -08001391 var label = trimLabel(deviceLabel(d)),
1392 noLabel = !label,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001393 node = d.el,
Simon Huntc72967b2014-11-20 09:21:42 -08001394 box,
1395 dx,
1396 dy;
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001397
1398 node.select('text')
1399 .text(label)
1400 .style('opacity', 0)
1401 .transition()
1402 .style('opacity', 1);
1403
Simon Huntc72967b2014-11-20 09:21:42 -08001404 if (noLabel) {
1405 box = emptyBox();
1406 dx = -config.icons.device.dim/2;
1407 dy = -config.icons.device.dim/2;
1408 } else {
1409 box = adjustRectToFitText(node);
1410 dx = box.x + config.icons.xoff;
1411 dy = box.y + config.icons.yoff;
1412 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001413
1414 node.select('rect')
1415 .transition()
1416 .attr(box);
1417
Simon Huntc72967b2014-11-20 09:21:42 -08001418 node.select('g.deviceIcon')
Thomas Vachuska89543292014-11-19 11:28:33 -08001419 .transition()
Simon Huntc72967b2014-11-20 09:21:42 -08001420 .attr('transform', translate(dx, dy));
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001421 }
1422
1423 function updateHostLabel(d) {
1424 var label = hostLabel(d),
1425 host = d.el;
1426
1427 host.select('text').text(label);
1428 }
1429
Simon Hunta6a9fe72014-11-20 11:17:12 -08001430 // TODO: should be using updateNodes() to do the upates!
Simon Huntbb282f52014-11-10 11:08:19 -08001431 function updateDeviceState(nodeData) {
1432 nodeData.el.classed('online', nodeData.online);
1433 updateDeviceLabel(nodeData);
1434 // TODO: review what else might need to be updated
1435 }
1436
1437 function updateHostState(hostData) {
1438 updateHostLabel(hostData);
1439 // TODO: review what else might need to be updated
1440 }
1441
Simon Hunt6ac93f32014-11-13 12:17:27 -08001442 function nodeMouseOver(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001443 hovered = d;
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -08001444 if (trafficHover() && (d.class === 'host' || d.class === 'device')) {
Thomas Vachuska29617e52014-11-20 03:17:46 -08001445 showSelectTraffic();
1446 } else if (flowsHover() && (d.class === 'device')) {
1447 showDeviceLinkFlows();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001448 }
1449 }
1450
1451 function nodeMouseOut(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001452 hovered = null;
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -08001453 if (trafficHover() && (d.class === 'host' || d.class === 'device')) {
Thomas Vachuska29617e52014-11-20 03:17:46 -08001454 showSelectTraffic();
1455 } else if (flowsHover() && (d.class === 'device')) {
1456 showDeviceLinkFlows();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001457 }
1458 }
Simon Huntbb282f52014-11-10 11:08:19 -08001459
Simon Hunteb1514d2014-11-20 09:57:29 -08001460 function addHostIcon(node, radius, iid) {
Thomas Vachuska89543292014-11-19 11:28:33 -08001461 var dim = radius * 1.5,
1462 xlate = -dim / 2;
1463
Simon Hunteb1514d2014-11-20 09:57:29 -08001464 node.append('use').attr({
1465 class: 'glyphIcon hostIcon',
1466 transform: translate(xlate,xlate),
1467 'xlink:href': iid,
1468 width: dim,
1469 height: dim
1470 });
Thomas Vachuska89543292014-11-19 11:28:33 -08001471 }
1472
Simon Hunt99c13842014-11-06 18:23:12 -08001473 function updateNodes() {
1474 node = nodeG.selectAll('.node')
1475 .data(network.nodes, function (d) { return d.id; });
1476
Simon Huntc72967b2014-11-20 09:21:42 -08001477 // TODO: operate on existing nodes
Simon Hunt7cd48f32014-11-09 23:42:50 -08001478 // update host labels
Simon Hunt99c13842014-11-06 18:23:12 -08001479 //node .foo() .bar() ...
1480
1481 // operate on entering nodes:
1482 var entering = node.enter()
1483 .append('g')
1484 .attr({
1485 id: function (d) { return safeId(d.id); },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001486 class: mkSvgClass,
Simon Hunt99c13842014-11-06 18:23:12 -08001487 transform: function (d) { return translate(d.x, d.y); },
1488 opacity: 0
1489 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08001490 .call(network.drag)
Simon Hunt6ac93f32014-11-13 12:17:27 -08001491 .on('mouseover', nodeMouseOver)
1492 .on('mouseout', nodeMouseOut)
Simon Hunt99c13842014-11-06 18:23:12 -08001493 .transition()
1494 .attr('opacity', 1);
1495
1496 // augment device nodes...
1497 entering.filter('.device').each(function (d) {
1498 var node = d3.select(this),
Simon Huntc72967b2014-11-20 09:21:42 -08001499 label = trimLabel(deviceLabel(d)),
1500 noLabel = !label,
Simon Hunt99c13842014-11-06 18:23:12 -08001501 box;
1502
Simon Hunt7cd48f32014-11-09 23:42:50 -08001503 // provide ref to element from backing data....
1504 d.el = node;
1505
Simon Hunt99c13842014-11-06 18:23:12 -08001506 node.append('rect')
1507 .attr({
Simon Hunta3dd9572014-11-20 15:22:41 -08001508 rx: 5,
1509 ry: 5
Simon Hunt99c13842014-11-06 18:23:12 -08001510 });
1511
1512 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001513 .text(label)
Simon Hunt99c13842014-11-06 18:23:12 -08001514 .attr('dy', '1.1em');
1515
1516 box = adjustRectToFitText(node);
Simon Hunta3dd9572014-11-20 15:22:41 -08001517 node.select('rect').attr(box);
Simon Huntc72967b2014-11-20 09:21:42 -08001518 addDeviceIcon(node, box, noLabel, iconGlyphUrl(d));
Simon Huntc7ee0662014-11-05 16:44:37 -08001519 });
Simon Hunt934c3ce2014-11-05 11:45:07 -08001520
Thomas Vachuska89543292014-11-19 11:28:33 -08001521 // TODO: better place for this configuration state
1522 var defaultHostRadius = 9,
1523 hostRadius = {
1524 bgpSpeaker: 14,
1525 router: 14,
1526 host: 14
1527 },
Simon Hunteb1514d2014-11-20 09:57:29 -08001528 hostGlyphId = {
Thomas Vachuska89543292014-11-19 11:28:33 -08001529 bgpSpeaker: 'bgpSpeaker',
1530 router: 'router',
1531 host: 'host'
1532 };
1533
1534
Simon Hunt56d51852014-11-09 13:03:35 -08001535 // augment host nodes...
1536 entering.filter('.host').each(function (d) {
1537 var node = d3.select(this),
Thomas Vachuska89543292014-11-19 11:28:33 -08001538 r = hostRadius[d.type] || defaultHostRadius,
1539 textDy = r + 10,
Simon Hunteb1514d2014-11-20 09:57:29 -08001540 iid = iconGlyphUrl(d);
Simon Hunt56d51852014-11-09 13:03:35 -08001541
Simon Hunt7cd48f32014-11-09 23:42:50 -08001542 // provide ref to element from backing data....
1543 d.el = node;
1544
Thomas Vachuska89543292014-11-19 11:28:33 -08001545 node.append('circle')
1546 .attr('r', r);
Simon Hunt7fa116d2014-11-17 14:16:55 -08001547
Simon Hunteb1514d2014-11-20 09:57:29 -08001548 if (iid) {
1549 addHostIcon(node, r, iid);
Simon Hunt7fa116d2014-11-17 14:16:55 -08001550 }
Simon Hunt56d51852014-11-09 13:03:35 -08001551
Simon Hunt56d51852014-11-09 13:03:35 -08001552 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001553 .text(hostLabel)
Thomas Vachuska89543292014-11-19 11:28:33 -08001554 .attr('dy', textDy)
Simon Hunt7cd48f32014-11-09 23:42:50 -08001555 .attr('text-anchor', 'middle');
Simon Hunt56d51852014-11-09 13:03:35 -08001556
1557 // debug function to show the modelled x,y coordinates of nodes...
1558 if (debug('showNodeXY')) {
1559 node.select('circle').attr('fill-opacity', 0.5);
1560 node.append('circle')
1561 .attr({
1562 class: 'debug',
1563 cx: 0,
1564 cy: 0,
1565 r: '3px'
1566 });
1567 }
1568 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001569
Simon Hunt99c13842014-11-06 18:23:12 -08001570 // operate on both existing and new nodes, if necessary
1571 //node .foo() .bar() ...
Simon Huntc7ee0662014-11-05 16:44:37 -08001572
Simon Hunt99c13842014-11-06 18:23:12 -08001573 // operate on exiting nodes:
Simon Huntea80eb42014-11-11 13:46:57 -08001574 // Note that the node is removed after 2 seconds.
1575 // Sub element animations should be shorter than 2 seconds.
1576 var exiting = node.exit()
Simon Hunt44031102014-11-11 13:20:36 -08001577 .transition()
1578 .duration(2000)
Simon Huntea80eb42014-11-11 13:46:57 -08001579 .style('opacity', 0)
Simon Hunt99c13842014-11-06 18:23:12 -08001580 .remove();
Simon Huntea80eb42014-11-11 13:46:57 -08001581
1582 // host node exits....
1583 exiting.filter('.host').each(function (d) {
1584 var node = d3.select(this);
1585
1586 node.select('text')
1587 .style('opacity', 0.5)
1588 .transition()
1589 .duration(1000)
1590 .style('opacity', 0);
1591 // note, leave <g>.remove to remove this element
1592
Thomas Vachuska89543292014-11-19 11:28:33 -08001593 node.select('circle')
1594 .style('stroke-fill', '#555')
1595 .style('fill', '#888')
Simon Huntea80eb42014-11-11 13:46:57 -08001596 .style('opacity', 0.5)
1597 .transition()
1598 .duration(1500)
1599 .attr('r', 0);
1600 // note, leave <g>.remove to remove this element
1601
1602 });
1603
1604 // TODO: device node exits
Simon Huntc7ee0662014-11-05 16:44:37 -08001605 }
1606
Simon Huntc72967b2014-11-20 09:21:42 -08001607 function addDeviceIcon(node, box, noLabel, iid) {
1608 var cfg = config.icons.device,
1609 dx,
1610 dy,
1611 g;
1612
1613 if (noLabel) {
1614 box = emptyBox();
1615 dx = -cfg.dim/2;
1616 dy = -cfg.dim/2;
1617 } else {
1618 box = adjustRectToFitText(node);
1619 dx = box.x + config.icons.xoff;
1620 dy = box.y + config.icons.yoff;
1621 }
1622
Simon Hunteb1514d2014-11-20 09:57:29 -08001623 g = node.append('g')
1624 .attr('class', 'glyphIcon deviceIcon')
Simon Huntc72967b2014-11-20 09:21:42 -08001625 .attr('transform', translate(dx, dy));
1626
1627 g.append('rect').attr({
1628 x: 0,
1629 y: 0,
1630 rx: cfg.rx,
1631 width: cfg.dim,
1632 height: cfg.dim
1633 });
1634
1635 g.append('use').attr({
1636 'xlink:href': iid,
1637 width: cfg.dim,
1638 height: cfg.dim
1639 });
1640
Simon Huntc72967b2014-11-20 09:21:42 -08001641 }
1642
Simon Hunt8257f4c2014-11-16 19:34:54 -08001643 function find(key, array) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001644 for (var idx = 0, n = array.length; idx < n; idx++) {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001645 if (array[idx].key === key) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001646 return idx;
1647 }
1648 }
1649 return -1;
1650 }
1651
1652 function removeLinkElement(linkData) {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001653 var idx = find(linkData.key, network.links),
1654 removed;
1655 if (idx >=0) {
1656 // remove from links array
1657 removed = network.links.splice(idx, 1);
1658 // remove from lookup cache
1659 delete network.lookup[removed[0].key];
1660 updateLinks();
1661 network.force.resume();
1662 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001663 }
Simon Huntc7ee0662014-11-05 16:44:37 -08001664
Simon Hunt44031102014-11-11 13:20:36 -08001665 function removeHostElement(hostData) {
1666 // first, remove associated hostLink...
1667 removeLinkElement(hostData.linkData);
1668
1669 // remove from lookup cache
1670 delete network.lookup[hostData.id];
1671 // remove from nodes array
1672 var idx = find(hostData.id, network.nodes);
1673 network.nodes.splice(idx, 1);
1674 // remove from SVG
1675 updateNodes();
1676 network.force.resume();
1677 }
1678
1679
Simon Huntc7ee0662014-11-05 16:44:37 -08001680 function tick() {
1681 node.attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001682 transform: function (d) { return translate(d.x, d.y); }
Simon Huntc7ee0662014-11-05 16:44:37 -08001683 });
1684
1685 link.attr({
1686 x1: function (d) { return d.source.x; },
1687 y1: function (d) { return d.source.y; },
1688 x2: function (d) { return d.target.x; },
1689 y2: function (d) { return d.target.y; }
1690 });
Simon Hunte2575b62014-11-18 15:25:53 -08001691
1692 linkLabel.each(function (d) {
1693 var el = d3.select(this);
Thomas Vachuska4731f122014-11-20 04:56:19 -08001694 var lnk = findLinkById(d.key);
1695
1696 if (lnk) {
1697 var parms = {
Simon Hunte2575b62014-11-18 15:25:53 -08001698 x1: lnk.source.x,
1699 y1: lnk.source.y,
1700 x2: lnk.target.x,
1701 y2: lnk.target.y
1702 };
Thomas Vachuska4731f122014-11-20 04:56:19 -08001703 el.attr('transform', transformLabel(parms));
1704 }
Simon Hunte2575b62014-11-18 15:25:53 -08001705 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001706 }
Simon Hunt934c3ce2014-11-05 11:45:07 -08001707
1708 // ==============================
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001709 // Web-Socket for live data
1710
1711 function webSockUrl() {
1712 return document.location.toString()
1713 .replace(/\#.*/, '')
1714 .replace('http://', 'ws://')
1715 .replace('https://', 'wss://')
1716 .replace('index2.html', config.webSockUrl);
1717 }
1718
1719 webSock = {
1720 ws : null,
1721
1722 connect : function() {
1723 webSock.ws = new WebSocket(webSockUrl());
1724
1725 webSock.ws.onopen = function() {
Simon Hunt0c6d4192014-11-12 12:07:10 -08001726 noWebSock(false);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001727 };
1728
1729 webSock.ws.onmessage = function(m) {
1730 if (m.data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001731 wsTraceRx(m.data);
Thomas Vachuskad472c6e2014-11-07 19:11:05 -08001732 handleServerEvent(JSON.parse(m.data));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001733 }
1734 };
1735
1736 webSock.ws.onclose = function(m) {
1737 webSock.ws = null;
Simon Hunt0c6d4192014-11-12 12:07:10 -08001738 noWebSock(true);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001739 };
1740 },
1741
1742 send : function(text) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001743 if (text != null) {
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001744 webSock._send(text);
1745 }
1746 },
1747
1748 _send : function(message) {
1749 if (webSock.ws) {
1750 webSock.ws.send(message);
Simon Hunta255a2c2014-11-13 22:29:35 -08001751 } else if (config.useLiveData) {
Simon Hunt56d51852014-11-09 13:03:35 -08001752 network.view.alert('no web socket open\n\n' + message);
Simon Hunta255a2c2014-11-13 22:29:35 -08001753 } else {
1754 console.log('WS Send: ' + JSON.stringify(message));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001755 }
1756 }
1757
1758 };
1759
Simon Hunt0c6d4192014-11-12 12:07:10 -08001760 function noWebSock(b) {
1761 mask.style('display',b ? 'block' : 'none');
1762 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001763
1764 function sendMessage(evType, payload) {
1765 var toSend = {
Simon Huntbb282f52014-11-10 11:08:19 -08001766 event: evType,
1767 sid: ++sid,
1768 payload: payload
1769 },
1770 asText = JSON.stringify(toSend);
1771 wsTraceTx(asText);
1772 webSock.send(asText);
Simon Huntc76ae892014-11-18 17:31:51 -08001773
1774 // Temporary measure for debugging UI behavior ...
1775 if (!config.useLiveData) {
1776 handleTestSend(toSend);
1777 }
Simon Huntbb282f52014-11-10 11:08:19 -08001778 }
1779
1780 function wsTraceTx(msg) {
1781 wsTrace('tx', msg);
1782 }
1783 function wsTraceRx(msg) {
1784 wsTrace('rx', msg);
1785 }
1786 function wsTrace(rxtx, msg) {
Simon Huntbb282f52014-11-10 11:08:19 -08001787 console.log('[' + rxtx + '] ' + msg);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001788 }
1789
Simon Huntc76ae892014-11-18 17:31:51 -08001790 // NOTE: Temporary hardcoded example for showing detail pane
1791 // while we fine-
1792 // Probably should not merge this change...
1793 function handleTestSend(msg) {
1794 if (msg.event === 'requestDetails') {
1795 showDetails({
1796 event: 'showDetails',
1797 sid: 1001,
1798 payload: {
1799 "id": "of:0000ffffffffff09",
1800 "type": "roadm",
1801 "propOrder": [
1802 "Name",
1803 "Vendor",
1804 "H/W Version",
1805 "S/W Version",
1806 "-",
1807 "Latitude",
1808 "Longitude",
1809 "Ports"
1810 ],
1811 "props": {
1812 "Name": null,
1813 "Vendor": "Linc",
1814 "H/W Version": "OE",
1815 "S/W Version": "?",
1816 "-": "",
1817 "Latitude": "40.8",
1818 "Longitude": "73.1",
1819 "Ports": "2"
1820 }
1821 }
1822 });
1823 }
1824 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001825
1826 // ==============================
1827 // Selection stuff
1828
1829 function selectObject(obj, el) {
1830 var n,
Simon Hunt01095ff2014-11-13 16:37:29 -08001831 srcEv = d3.event.sourceEvent,
1832 meta = srcEv.metaKey,
1833 shift = srcEv.shiftKey;
1834
Simon Huntdeab4322014-11-13 18:49:07 -08001835 if ((panZoom() && !meta) || (!panZoom() && meta)) {
Simon Hunt01095ff2014-11-13 16:37:29 -08001836 return;
1837 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001838
1839 if (el) {
1840 n = d3.select(el);
1841 } else {
1842 node.each(function(d) {
1843 if (d == obj) {
1844 n = d3.select(el = this);
1845 }
1846 });
1847 }
1848 if (!n) return;
1849
Simon Hunt01095ff2014-11-13 16:37:29 -08001850 if (shift && n.classed('selected')) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001851 deselectObject(obj.id);
Simon Hunt61d04042014-11-11 17:27:16 -08001852 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001853 return;
1854 }
1855
Simon Hunt01095ff2014-11-13 16:37:29 -08001856 if (!shift) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001857 deselectAll();
1858 }
1859
Simon Huntc31d5692014-11-12 13:27:18 -08001860 selections[obj.id] = { obj: obj, el: el };
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001861 selectOrder.push(obj.id);
1862
1863 n.classed('selected', true);
Simon Hunt61d04042014-11-11 17:27:16 -08001864 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001865 }
1866
1867 function deselectObject(id) {
Simon Huntc31d5692014-11-12 13:27:18 -08001868 var obj = selections[id],
1869 idx;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001870 if (obj) {
1871 d3.select(obj.el).classed('selected', false);
Simon Hunt61d04042014-11-11 17:27:16 -08001872 delete selections[id];
Simon Huntc31d5692014-11-12 13:27:18 -08001873 idx = $.inArray(id, selectOrder);
1874 if (idx >= 0) {
1875 selectOrder.splice(idx, 1);
1876 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001877 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001878 }
1879
1880 function deselectAll() {
1881 // deselect all nodes in the network...
1882 node.classed('selected', false);
1883 selections = {};
1884 selectOrder = [];
Simon Hunt61d04042014-11-11 17:27:16 -08001885 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001886 }
1887
Simon Hunt61d04042014-11-11 17:27:16 -08001888 // update the state of the detail pane, based on current selections
1889 function updateDetailPane() {
1890 var nSel = selectOrder.length;
1891 if (!nSel) {
1892 detailPane.hide();
Simon Huntd72bc702014-11-13 18:38:04 -08001893 showTrafficAction(); // sends cancelTraffic event
Simon Hunt61d04042014-11-11 17:27:16 -08001894 } else if (nSel === 1) {
1895 singleSelect();
1896 } else {
1897 multiSelect();
1898 }
1899 }
1900
1901 function singleSelect() {
1902 requestDetails();
Simon Huntb53e0682014-11-12 13:32:01 -08001903 // NOTE: detail pane will be shown from showDetails event callback
Simon Hunt61d04042014-11-11 17:27:16 -08001904 }
1905
1906 function multiSelect() {
Simon Huntb53e0682014-11-12 13:32:01 -08001907 populateMultiSelect();
Simon Huntb53e0682014-11-12 13:32:01 -08001908 }
1909
1910 function addSep(tbody) {
1911 var tr = tbody.append('tr');
1912 $('<hr>').appendTo(tr.append('td').attr('colspan', 2));
1913 }
1914
1915 function addProp(tbody, label, value) {
1916 var tr = tbody.append('tr');
1917
1918 tr.append('td')
1919 .attr('class', 'label')
1920 .text(label + ' :');
1921
1922 tr.append('td')
1923 .attr('class', 'value')
1924 .text(value);
1925 }
1926
1927 function populateMultiSelect() {
1928 detailPane.empty();
1929
Simon Hunta3dd9572014-11-20 15:22:41 -08001930 var title = detailPane.append('h3'),
1931 table = detailPane.append('table'),
1932 tbody = table.append('tbody');
Simon Huntb53e0682014-11-12 13:32:01 -08001933
Thomas Vachuska4731f122014-11-20 04:56:19 -08001934 title.text('Selected Nodes');
Simon Huntb53e0682014-11-12 13:32:01 -08001935
1936 selectOrder.forEach(function (d, i) {
1937 addProp(tbody, i+1, d);
1938 });
Simon Huntd72bc702014-11-13 18:38:04 -08001939
1940 addMultiSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08001941 }
1942
1943 function populateDetails(data) {
1944 detailPane.empty();
1945
Simon Hunta6a9fe72014-11-20 11:17:12 -08001946 var svg = detailPane.append('svg'),
1947 iid = iconGlyphUrl(data);
1948
Simon Hunta3dd9572014-11-20 15:22:41 -08001949 var title = detailPane.append('h2'),
1950 table = detailPane.append('table'),
1951 tbody = table.append('tbody');
Simon Hunt61d04042014-11-11 17:27:16 -08001952
Simon Hunta6a9fe72014-11-20 11:17:12 -08001953 appendGlyph(svg, 0, 0, 40, iid);
1954 title.text(data.id);
Simon Hunt61d04042014-11-11 17:27:16 -08001955
1956 data.propOrder.forEach(function(p) {
1957 if (p === '-') {
1958 addSep(tbody);
1959 } else {
1960 addProp(tbody, p, data.props[p]);
1961 }
1962 });
Simon Huntd72bc702014-11-13 18:38:04 -08001963
Thomas Vachuska4731f122014-11-20 04:56:19 -08001964 addSingleSelectActions(data);
Simon Hunt61d04042014-11-11 17:27:16 -08001965 }
1966
Thomas Vachuska4731f122014-11-20 04:56:19 -08001967 function addSingleSelectActions(data) {
Simon Huntd72bc702014-11-13 18:38:04 -08001968 detailPane.append('hr');
1969 // always want to allow 'show traffic'
Thomas Vachuska4731f122014-11-20 04:56:19 -08001970 addAction(detailPane, 'Show Related Traffic', showTrafficAction);
1971
1972 if (data.type === 'switch') {
1973 addAction(detailPane, 'Show Device Flows', showDeviceLinkFlowsAction);
1974 }
Simon Huntd72bc702014-11-13 18:38:04 -08001975 }
1976
1977 function addMultiSelectActions() {
1978 detailPane.append('hr');
1979 // always want to allow 'show traffic'
Thomas Vachuska4731f122014-11-20 04:56:19 -08001980 addAction(detailPane, 'Show Related Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001981 // if exactly two hosts are selected, also want 'add host intent'
1982 if (nSel() === 2 && allSelectionsClass('host')) {
Thomas Vachuska4731f122014-11-20 04:56:19 -08001983 addAction(detailPane, 'Add Host-to-Host Intent', addIntentAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001984 }
1985 }
1986
Simon Hunta5e89142014-11-14 07:00:33 -08001987 function addAction(panel, text, cb) {
1988 panel.append('div')
Simon Huntd72bc702014-11-13 18:38:04 -08001989 .classed('actionBtn', true)
1990 .text(text)
1991 .on('click', cb);
1992 }
1993
1994
Paul Greysonfcba0e82014-11-13 10:21:16 -08001995 function zoomPan(scale, translate) {
1996 zoomPanContainer.attr("transform", "translate(" + translate + ")scale(" + scale + ")");
1997 // keep the map lines constant width while zooming
Thomas Vachuska89543292014-11-19 11:28:33 -08001998 bgImg.style("stroke-width", 2.0 / scale + "px");
Paul Greysonfcba0e82014-11-13 10:21:16 -08001999 }
2000
2001 function resetZoomPan() {
2002 zoomPan(1, [0,0]);
2003 zoom.scale(1).translate([0,0]);
2004 }
2005
2006 function setupZoomPan() {
2007 function zoomed() {
Simon Huntdeab4322014-11-13 18:49:07 -08002008 if (!panZoom() ^ !d3.event.sourceEvent.metaKey) {
Paul Greysonfcba0e82014-11-13 10:21:16 -08002009 zoomPan(d3.event.scale, d3.event.translate);
2010 }
2011 }
2012
2013 zoom = d3.behavior.zoom()
2014 .translate([0, 0])
2015 .scale(1)
2016 .scaleExtent([1, 8])
2017 .on("zoom", zoomed);
2018
2019 svg.call(zoom);
2020 }
2021
Simon Hunt61d04042014-11-11 17:27:16 -08002022 // ==============================
2023 // Test harness code
Simon Hunt56d51852014-11-09 13:03:35 -08002024
2025 function prepareScenario(view, ctx, dbg) {
2026 var sc = scenario,
2027 urlSc = sc.evDir + ctx + sc.evScenario;
2028
2029 if (!ctx) {
2030 view.alert("No scenario specified (null ctx)");
2031 return;
2032 }
2033
2034 sc.view = view;
2035 sc.ctx = ctx;
2036 sc.debug = dbg;
2037 sc.evNumber = 0;
2038
2039 d3.json(urlSc, function(err, data) {
Simon Huntbb282f52014-11-10 11:08:19 -08002040 var p = data && data.params || {},
2041 desc = data && data.description || null,
Simon Huntfc274c92014-11-11 11:05:46 -08002042 intro = data && data.title;
Simon Huntbb282f52014-11-10 11:08:19 -08002043
Simon Hunt56d51852014-11-09 13:03:35 -08002044 if (err) {
2045 view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err);
2046 } else {
2047 sc.params = p;
Simon Huntbb282f52014-11-10 11:08:19 -08002048 if (desc) {
2049 intro += '\n\n ' + desc.join('\n ');
2050 }
2051 view.alert(intro);
Simon Hunt56d51852014-11-09 13:03:35 -08002052 }
2053 });
2054
2055 }
2056
Simon Hunt01095ff2014-11-13 16:37:29 -08002057 // ==============================
2058 // Toggle Buttons in masthead
Simon Hunt0c6d4192014-11-12 12:07:10 -08002059
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002060 // TODO: toggle button (and other widgets in the masthead) should be provided
2061 // by the framework; not generated by the view.
2062
Simon Hunta3dd9572014-11-20 15:22:41 -08002063 var showInstances;
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002064
2065 function addButtonBar(view) {
2066 var bb = d3.select('#mast')
2067 .append('span').classed('right', true).attr('id', 'bb');
2068
Simon Hunta5e89142014-11-14 07:00:33 -08002069 function mkTogBtn(text, cb) {
2070 return bb.append('span')
2071 .classed('btn', true)
2072 .text(text)
2073 .on('click', cb);
2074 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002075
Simon Hunta5e89142014-11-14 07:00:33 -08002076 showInstances = mkTogBtn('Show Instances', toggleInst);
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002077 }
2078
Simon Hunta5e89142014-11-14 07:00:33 -08002079 function instShown() {
2080 return showInstances.classed('active');
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002081 }
Simon Hunta5e89142014-11-14 07:00:33 -08002082 function toggleInst() {
2083 showInstances.classed('active', !instShown());
2084 if (instShown()) {
2085 oiBox.show();
2086 } else {
2087 oiBox.hide();
2088 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002089 }
2090
Simon Huntdeab4322014-11-13 18:49:07 -08002091 function panZoom() {
Simon Hunte5b71752014-11-18 20:06:07 -08002092 return false;
Simon Hunta5e89142014-11-14 07:00:33 -08002093 }
2094
2095 function trafficHover() {
Thomas Vachuska29617e52014-11-20 03:17:46 -08002096 return hoverModes[hoverMode] === 'intents';
Simon Hunta5e89142014-11-14 07:00:33 -08002097 }
Thomas Vachuska29617e52014-11-20 03:17:46 -08002098
2099 function flowsHover() {
2100 return hoverModes[hoverMode] === 'flows';
2101 }
2102
Simon Hunt7fa116d2014-11-17 14:16:55 -08002103 function loadGlyphs(svg) {
2104 var defs = svg.append('defs');
2105 gly.defBird(defs);
2106 gly.defBullhorn(defs);
Simon Huntc72967b2014-11-20 09:21:42 -08002107 gly.defGlyphs(defs);
Simon Hunt7fa116d2014-11-17 14:16:55 -08002108 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002109
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002110 // ==============================
Simon Hunt142d0032014-11-04 20:13:09 -08002111 // View life-cycle callbacks
Simon Hunt195cb382014-11-03 17:50:51 -08002112
Simon Huntf67722a2014-11-10 09:32:06 -08002113 function preload(view, ctx, flags) {
Simon Hunt142d0032014-11-04 20:13:09 -08002114 var w = view.width(),
2115 h = view.height(),
Simon Huntc7ee0662014-11-05 16:44:37 -08002116 fcfg = config.force,
2117 fpad = fcfg.pad,
2118 forceDim = [w - 2*fpad, h - 2*fpad];
Simon Hunt195cb382014-11-03 17:50:51 -08002119
Simon Hunt142d0032014-11-04 20:13:09 -08002120 // NOTE: view.$div is a D3 selection of the view's div
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002121 var viewBox = '0 0 ' + config.logicalSize + ' ' + config.logicalSize;
2122 svg = view.$div.append('svg').attr('viewBox', viewBox);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002123 setSize(svg, view);
2124
Simon Hunt7fa116d2014-11-17 14:16:55 -08002125 loadGlyphs(svg);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002126
Paul Greysonfcba0e82014-11-13 10:21:16 -08002127 zoomPanContainer = svg.append('g').attr('id', 'zoomPanContainer');
Paul Greysonfcba0e82014-11-13 10:21:16 -08002128 setupZoomPan();
2129
Simon Hunt1a9eff92014-11-07 11:06:34 -08002130 // add blue glow filter to svg layer
Paul Greysonfcba0e82014-11-13 10:21:16 -08002131 d3u.appendGlow(zoomPanContainer);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002132
Simon Huntc7ee0662014-11-05 16:44:37 -08002133 // group for the topology
Paul Greysonfcba0e82014-11-13 10:21:16 -08002134 topoG = zoomPanContainer.append('g')
Simon Huntd3b7d512014-11-12 15:48:41 -08002135 .attr('id', 'topo-G')
Simon Huntc7ee0662014-11-05 16:44:37 -08002136 .attr('transform', fcfg.translate());
2137
Simon Hunte2575b62014-11-18 15:25:53 -08002138 // subgroups for links, link labels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002139 linkG = topoG.append('g').attr('id', 'links');
Simon Hunte2575b62014-11-18 15:25:53 -08002140 linkLabelG = topoG.append('g').attr('id', 'linkLabels');
Simon Huntc7ee0662014-11-05 16:44:37 -08002141 nodeG = topoG.append('g').attr('id', 'nodes');
2142
Simon Hunte2575b62014-11-18 15:25:53 -08002143 // selection of links, linkLabels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002144 link = linkG.selectAll('.link');
Simon Hunte2575b62014-11-18 15:25:53 -08002145 linkLabel = linkLabelG.selectAll('.linkLabel');
Simon Huntc7ee0662014-11-05 16:44:37 -08002146 node = nodeG.selectAll('.node');
2147
Simon Hunt7cd48f32014-11-09 23:42:50 -08002148 function chrg(d) {
2149 return fcfg.charge[d.class] || -12000;
2150 }
Simon Hunt99c13842014-11-06 18:23:12 -08002151 function ldist(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002152 return fcfg.linkDistance[d.type] || 50;
Simon Hunt99c13842014-11-06 18:23:12 -08002153 }
2154 function lstrg(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002155 // 0.0 - 1.0
2156 return fcfg.linkStrength[d.type] || 1.0;
Simon Hunt99c13842014-11-06 18:23:12 -08002157 }
2158
Simon Hunt1a9eff92014-11-07 11:06:34 -08002159 function selectCb(d, self) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002160 selectObject(d, self);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002161 }
2162
2163 function atDragEnd(d, self) {
Simon Hunt56d51852014-11-09 13:03:35 -08002164 // once we've finished moving, pin the node in position
2165 d.fixed = true;
2166 d3.select(self).classed('fixed', true);
2167 if (config.useLiveData) {
Simon Hunt902c9922014-11-11 11:59:31 -08002168 sendUpdateMeta(d);
Simon Hunta255a2c2014-11-13 22:29:35 -08002169 } else {
2170 console.log('Moving node ' + d.id + ' to [' + d.x + ',' + d.y + ']');
Simon Hunt1a9eff92014-11-07 11:06:34 -08002171 }
2172 }
2173
Simon Hunt902c9922014-11-11 11:59:31 -08002174 function sendUpdateMeta(d) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002175 sendMessage('updateMeta', {
2176 id: d.id,
2177 'class': d.class,
Simon Hunt902c9922014-11-11 11:59:31 -08002178 'memento': {
Simon Hunt01095ff2014-11-13 16:37:29 -08002179 x: d.x,
2180 y: d.y
Simon Hunt902c9922014-11-11 11:59:31 -08002181 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002182 });
2183 }
2184
Simon Huntc7ee0662014-11-05 16:44:37 -08002185 // set up the force layout
2186 network.force = d3.layout.force()
2187 .size(forceDim)
2188 .nodes(network.nodes)
2189 .links(network.links)
Simon Hunt7cd48f32014-11-09 23:42:50 -08002190 .gravity(0.4)
2191 .friction(0.7)
2192 .charge(chrg)
Simon Hunt99c13842014-11-06 18:23:12 -08002193 .linkDistance(ldist)
2194 .linkStrength(lstrg)
Simon Huntc7ee0662014-11-05 16:44:37 -08002195 .on('tick', tick);
Simon Hunt195cb382014-11-03 17:50:51 -08002196
Simon Hunt01095ff2014-11-13 16:37:29 -08002197 network.drag = d3u.createDragBehavior(network.force,
Simon Huntdeab4322014-11-13 18:49:07 -08002198 selectCb, atDragEnd, panZoom);
Simon Hunt0c6d4192014-11-12 12:07:10 -08002199
2200 // create mask layer for when we lose connection to server.
Simon Hunta5e89142014-11-14 07:00:33 -08002201 // TODO: this should be part of the framework
Simon Hunt0c6d4192014-11-12 12:07:10 -08002202 mask = view.$div.append('div').attr('id','topo-mask');
2203 para(mask, 'Oops!');
2204 para(mask, 'Web-socket connection to server closed...');
2205 para(mask, 'Try refreshing the page.');
Simon Hunt12ce12e2014-11-15 21:13:19 -08002206
2207 mask.append('svg')
2208 .attr({
2209 id: 'mask-bird',
2210 width: w,
2211 height: h
2212 })
2213 .append('g')
2214 .attr('transform', birdTranslate(w, h))
2215 .style('opacity', 0.3)
2216 .append('use')
2217 .attr({
2218 'xlink:href': '#bird',
2219 width: config.birdDim,
2220 height: config.birdDim,
2221 fill: '#111'
Thomas Vachuska89543292014-11-19 11:28:33 -08002222 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08002223 }
Simon Hunt195cb382014-11-03 17:50:51 -08002224
Simon Hunt01095ff2014-11-13 16:37:29 -08002225 function para(sel, text) {
2226 sel.append('p').text(text);
2227 }
2228
2229
Simon Hunt56d51852014-11-09 13:03:35 -08002230 function load(view, ctx, flags) {
Simon Huntf67722a2014-11-10 09:32:06 -08002231 // resize, in case the window was resized while we were not loaded
2232 resize(view, ctx, flags);
2233
Simon Hunt99c13842014-11-06 18:23:12 -08002234 // cache the view token, so network topo functions can access it
2235 network.view = view;
Simon Hunt56d51852014-11-09 13:03:35 -08002236 config.useLiveData = !flags.local;
2237
2238 if (!config.useLiveData) {
2239 prepareScenario(view, ctx, flags.debug);
2240 }
Simon Hunt99c13842014-11-06 18:23:12 -08002241
2242 // set our radio buttons and key bindings
Simon Hunt9462e8c2014-11-14 17:28:09 -08002243 layerBtnSet = view.setRadio(layerButtons);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002244 view.setKeys(keyDispatch);
Simon Hunt195cb382014-11-03 17:50:51 -08002245
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002246 // patch in our "button bar" for now
2247 // TODO: implement a more official frameworky way of doing this..
2248 addButtonBar(view);
2249
Simon Huntd3b7d512014-11-12 15:48:41 -08002250 // Load map data asynchronously; complete startup after that..
2251 loadGeoJsonData();
Simon Hunta255a2c2014-11-13 22:29:35 -08002252 }
2253
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002254 function startAntTimer() {
Thomas Vachuska4731f122014-11-20 04:56:19 -08002255 if (!antTimer) {
2256 var pulses = [5, 3, 1.2, 3],
2257 pulse = 0;
2258 antTimer = setInterval(function () {
2259 pulse = pulse + 1;
2260 pulse = pulse === pulses.length ? 0 : pulse;
2261 d3.selectAll('.animated').style('stroke-width', pulses[pulse]);
2262 }, 200);
2263 }
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002264 }
2265
2266 function stopAntTimer() {
Simon Hunta255a2c2014-11-13 22:29:35 -08002267 if (antTimer) {
2268 clearInterval(antTimer);
2269 antTimer = null;
2270 }
Simon Huntd3b7d512014-11-12 15:48:41 -08002271 }
2272
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002273 function unload(view, ctx, flags) {
2274 stopAntTimer();
2275 }
2276
Simon Huntd3b7d512014-11-12 15:48:41 -08002277 // TODO: move these to config/state portion of script
Simon Hunta6a9fe72014-11-20 11:17:12 -08002278 var geoJsonUrl = 'json/map/continental_us.json',
Simon Huntd3b7d512014-11-12 15:48:41 -08002279 geoJson;
2280
2281 function loadGeoJsonData() {
2282 d3.json(geoJsonUrl, function (err, data) {
2283 if (err) {
2284 // fall back to USA map background
2285 loadStaticMap();
2286 } else {
2287 geoJson = data;
2288 loadGeoMap();
2289 }
2290
2291 // finally, connect to the server...
2292 if (config.useLiveData) {
2293 webSock.connect();
2294 }
2295 });
2296 }
2297
2298 function showBg() {
2299 return config.options.showBackground ? 'visible' : 'hidden';
2300 }
2301
2302 function loadStaticMap() {
2303 fnTrace('loadStaticMap', config.backgroundUrl);
2304 var w = network.view.width(),
2305 h = network.view.height();
2306
2307 // load the background image
2308 bgImg = svg.insert('svg:image', '#topo-G')
2309 .attr({
2310 id: 'topo-bg',
2311 width: w,
2312 height: h,
2313 'xlink:href': config.backgroundUrl
2314 })
2315 .style({
2316 visibility: showBg()
2317 });
2318 }
2319
2320 function loadGeoMap() {
2321 fnTrace('loadGeoMap', geoJsonUrl);
Simon Huntd3b7d512014-11-12 15:48:41 -08002322
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002323 // extracts the topojson data into geocoordinate-based geometry
2324 var topoData = topojson.feature(geoJson, geoJson.objects.states);
Simon Huntd3b7d512014-11-12 15:48:41 -08002325
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002326 // see: http://bl.ocks.org/mbostock/4707858
2327 geoMapProjection = d3.geo.mercator();
2328 var path = d3.geo.path().projection(geoMapProjection);
Simon Huntd3b7d512014-11-12 15:48:41 -08002329
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002330 geoMapProjection
2331 .scale(1)
2332 .translate([0, 0]);
Simon Huntd3b7d512014-11-12 15:48:41 -08002333
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002334 // [[x1,y1],[x2,y2]]
2335 var b = path.bounds(topoData);
Paul Greysonfcba0e82014-11-13 10:21:16 -08002336 // size map to 95% of minimum dimension to fill space
2337 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 -08002338 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 -08002339
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002340 geoMapProjection
2341 .scale(s)
2342 .translate(t);
2343
Paul Greysonfcba0e82014-11-13 10:21:16 -08002344 bgImg = zoomPanContainer.insert("g", '#topo-G');
Thomas Vachuska89543292014-11-19 11:28:33 -08002345 bgImg.attr('id', 'map').selectAll('path')
2346 .data(topoData.features)
2347 .enter()
2348 .append('path')
2349 .attr('d', path);
Simon Hunt195cb382014-11-03 17:50:51 -08002350 }
2351
Simon Huntf67722a2014-11-10 09:32:06 -08002352 function resize(view, ctx, flags) {
Simon Hunt12ce12e2014-11-15 21:13:19 -08002353 var w = view.width(),
2354 h = view.height();
2355
Simon Hunt934c3ce2014-11-05 11:45:07 -08002356 setSize(svg, view);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002357
2358 d3.select('#mask-bird').attr({ width: w, height: h})
2359 .select('g').attr('transform', birdTranslate(w, h));
Simon Hunt142d0032014-11-04 20:13:09 -08002360 }
2361
Simon Hunt12ce12e2014-11-15 21:13:19 -08002362 function birdTranslate(w, h) {
2363 var bdim = config.birdDim;
2364 return 'translate('+((w-bdim)*.4)+','+((h-bdim)*.1)+')';
2365 }
Simon Hunt142d0032014-11-04 20:13:09 -08002366
2367 // ==============================
2368 // View registration
Simon Hunt195cb382014-11-03 17:50:51 -08002369
Simon Hunt25248912014-11-04 11:25:48 -08002370 onos.ui.addView('topo', {
Simon Hunt142d0032014-11-04 20:13:09 -08002371 preload: preload,
2372 load: load,
Simon Hunta255a2c2014-11-13 22:29:35 -08002373 unload: unload,
Simon Hunt142d0032014-11-04 20:13:09 -08002374 resize: resize
Simon Hunt195cb382014-11-03 17:50:51 -08002375 });
2376
Simon Hunt61d04042014-11-11 17:27:16 -08002377 detailPane = onos.ui.addFloatingPanel('topo-detail');
Simon Hunta5e89142014-11-14 07:00:33 -08002378 oiBox = onos.ui.addFloatingPanel('topo-oibox', 'TL');
Simon Hunt61d04042014-11-11 17:27:16 -08002379
Simon Hunt195cb382014-11-03 17:50:51 -08002380}(ONOS));