blob: 1222f9c2dad66b7e92d8aebf1cb18612708a2b0c [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 Hunt988c6fc2014-11-20 17:43:03 -0800139 // TODO: remove these "development only" bindings
140 M: testMe,
141 S: injectStartupEvents,
142 space: injectTestEvent,
Simon Hunt99c13842014-11-06 18:23:12 -0800143
Simon Hunt988c6fc2014-11-20 17:43:03 -0800144 B: [toggleBg, 'Toggle background image'],
145 L: [cycleLabels, 'Cycle Device labels'],
Simon Hunt934c3ce2014-11-05 11:45:07 -0800146 P: togglePorts,
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800147 U: [unpin, 'Unpin node'],
148 R: [resetZoomPan, 'Reset zoom/pan'],
149 H: [cycleHoverMode, 'Cycle hover mode'],
150 V: [showTrafficAction, 'Show traffic'],
151 A: [showAllTrafficAction, 'Show all traffic'],
152 F: [showDeviceLinkFlowsAction, 'Show device link flows'],
Simon Hunt9462e8c2014-11-14 17:28:09 -0800153 esc: handleEscape
Simon Hunt934c3ce2014-11-05 11:45:07 -0800154 };
Simon Hunt142d0032014-11-04 20:13:09 -0800155
Simon Hunt195cb382014-11-03 17:50:51 -0800156 // state variables
Simon Hunt99c13842014-11-06 18:23:12 -0800157 var network = {
Simon Hunt50128c02014-11-08 13:36:15 -0800158 view: null, // view token reference
Simon Hunt99c13842014-11-06 18:23:12 -0800159 nodes: [],
160 links: [],
Simon Hunt269670f2014-11-17 16:17:43 -0800161 lookup: {},
162 revLinkToKey: {}
Simon Hunt99c13842014-11-06 18:23:12 -0800163 },
Simon Hunt56d51852014-11-09 13:03:35 -0800164 scenario = {
165 evDir: 'json/ev/',
166 evScenario: '/scenario.json',
167 evPrefix: '/ev_',
168 evOnos: '_onos.json',
169 evUi: '_ui.json',
170 ctx: null,
171 params: {},
172 evNumber: 0,
173 view: null,
174 debug: false
175 },
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800176 webSock,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800177 sid = 0,
Thomas Vachuska60d72bf2014-11-21 13:02:00 -0800178 deviceLabelCount = 3,
Simon Hunt209155e2014-11-21 12:16:09 -0800179 hostLabelCount = 2,
Simon Hunt56d51852014-11-09 13:03:35 -0800180 deviceLabelIndex = 0,
181 hostLabelIndex = 0,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800182 selections = {},
Simon Hunta5e89142014-11-14 07:00:33 -0800183 selectOrder = [],
Simon Hunt6ac93f32014-11-13 12:17:27 -0800184 hovered = null,
Simon Hunta5e89142014-11-14 07:00:33 -0800185 detailPane,
Simon Hunta255a2c2014-11-13 22:29:35 -0800186 antTimer = null,
Simon Hunta5e89142014-11-14 07:00:33 -0800187 onosInstances = {},
188 onosOrder = [],
189 oiBox,
Simon Hunt9462e8c2014-11-14 17:28:09 -0800190 oiShowMaster = false,
Thomas Vachuska29617e52014-11-20 03:17:46 -0800191 hoverModes = [ 'none', 'intents', 'flows'],
192 hoverMode = 0,
Simon Hunt195cb382014-11-03 17:50:51 -0800193 portLabelsOn = false;
194
Simon Hunt934c3ce2014-11-05 11:45:07 -0800195 // D3 selections
196 var svg,
Paul Greysonfcba0e82014-11-13 10:21:16 -0800197 zoomPanContainer,
Simon Hunt934c3ce2014-11-05 11:45:07 -0800198 bgImg,
Simon Huntc7ee0662014-11-05 16:44:37 -0800199 topoG,
200 nodeG,
201 linkG,
Simon Hunte2575b62014-11-18 15:25:53 -0800202 linkLabelG,
Simon Huntc7ee0662014-11-05 16:44:37 -0800203 node,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800204 link,
Simon Hunte2575b62014-11-18 15:25:53 -0800205 linkLabel,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800206 mask;
Simon Hunt195cb382014-11-03 17:50:51 -0800207
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800208 // the projection for the map background
209 var geoMapProjection;
210
Paul Greysonfcba0e82014-11-13 10:21:16 -0800211 // the zoom function
212 var zoom;
213
Simon Hunt142d0032014-11-04 20:13:09 -0800214 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800215 // For Debugging / Development
Simon Hunt195cb382014-11-03 17:50:51 -0800216
Simon Hunt99c13842014-11-06 18:23:12 -0800217 function note(label, msg) {
218 console.log('NOTE: ' + label + ': ' + msg);
Simon Hunt195cb382014-11-03 17:50:51 -0800219 }
220
Simon Hunt99c13842014-11-06 18:23:12 -0800221 function debug(what) {
222 return config.debugOn && config.debug[what];
Simon Hunt934c3ce2014-11-05 11:45:07 -0800223 }
224
Simon Huntfc274c92014-11-11 11:05:46 -0800225 function fnTrace(msg, id) {
226 if (config.fnTrace) {
227 console.log('FN: ' + msg + ' [' + id + ']');
228 }
229 }
Simon Hunt99c13842014-11-06 18:23:12 -0800230
Simon Hunta5e89142014-11-14 07:00:33 -0800231 function evTrace(data) {
232 fnTrace(data.event, data.payload.id);
233 }
234
Simon Hunt934c3ce2014-11-05 11:45:07 -0800235 // ==============================
236 // Key Callbacks
237
Simon Hunt99c13842014-11-06 18:23:12 -0800238 function testMe(view) {
Simon Hunta3dd9572014-11-20 15:22:41 -0800239 //view.alert('Theme is ' + view.theme());
240 //view.flash('This is some text');
Simon Hunt99c13842014-11-06 18:23:12 -0800241 }
242
Simon Hunt56d51852014-11-09 13:03:35 -0800243 function abortIfLive() {
Simon Hunt50128c02014-11-08 13:36:15 -0800244 if (config.useLiveData) {
Simon Huntb53e0682014-11-12 13:32:01 -0800245 network.view.alert("Sorry, currently using live data..");
Simon Hunt56d51852014-11-09 13:03:35 -0800246 return true;
Simon Hunt50128c02014-11-08 13:36:15 -0800247 }
Simon Hunt56d51852014-11-09 13:03:35 -0800248 return false;
249 }
Simon Hunt50128c02014-11-08 13:36:15 -0800250
Simon Hunt56d51852014-11-09 13:03:35 -0800251 function testDebug(msg) {
252 if (scenario.debug) {
253 scenario.view.alert(msg);
254 }
255 }
Simon Hunt99c13842014-11-06 18:23:12 -0800256
Simon Hunt56d51852014-11-09 13:03:35 -0800257 function injectTestEvent(view) {
258 if (abortIfLive()) { return; }
259 var sc = scenario,
260 evn = ++sc.evNumber,
261 pfx = sc.evDir + sc.ctx + sc.evPrefix + evn,
262 onosUrl = pfx + sc.evOnos,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800263 uiUrl = pfx + sc.evUi,
264 stack = [
265 { url: onosUrl, cb: handleServerEvent },
266 { url: uiUrl, cb: handleUiEvent }
267 ];
268 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800269 }
270
Simon Hunt7cd48f32014-11-09 23:42:50 -0800271 function recurseFetchEvent(stack, evn) {
272 var v = scenario.view,
273 frame;
274 if (stack.length === 0) {
Simon Huntfc274c92014-11-11 11:05:46 -0800275 v.alert('Oops!\n\nNo event #' + evn + ' found.');
Simon Hunt7cd48f32014-11-09 23:42:50 -0800276 return;
277 }
278 frame = stack.shift();
279
280 d3.json(frame.url, function (err, data) {
Simon Hunt99c13842014-11-06 18:23:12 -0800281 if (err) {
Simon Hunt56d51852014-11-09 13:03:35 -0800282 if (err.status === 404) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800283 // if we didn't find the data, try the next stack frame
284 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800285 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800286 v.alert('non-404 error:\n\n' + frame.url + '\n\n' + err);
Simon Hunt56d51852014-11-09 13:03:35 -0800287 }
Simon Hunt99c13842014-11-06 18:23:12 -0800288 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800289 testDebug('loaded: ' + frame.url);
Simon Hunt1712ed82014-11-17 12:56:00 -0800290 wsTrace('test', JSON.stringify(data));
Simon Hunt7cd48f32014-11-09 23:42:50 -0800291 frame.cb(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800292 }
293 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800294
Simon Hunt56d51852014-11-09 13:03:35 -0800295 }
Simon Hunt50128c02014-11-08 13:36:15 -0800296
Simon Hunt56d51852014-11-09 13:03:35 -0800297 function handleUiEvent(data) {
Simon Huntbb282f52014-11-10 11:08:19 -0800298 scenario.view.alert('UI Tx: ' + data.event + '\n\n' +
299 JSON.stringify(data));
Simon Hunt56d51852014-11-09 13:03:35 -0800300 }
301
302 function injectStartupEvents(view) {
303 var last = scenario.params.lastAuto || 0;
304 if (abortIfLive()) { return; }
305
306 while (scenario.evNumber < last) {
Simon Hunt1a9eff92014-11-07 11:06:34 -0800307 injectTestEvent(view);
308 }
309 }
310
Simon Hunt934c3ce2014-11-05 11:45:07 -0800311 function toggleBg() {
312 var vis = bgImg.style('visibility');
313 bgImg.style('visibility', (vis === 'hidden') ? 'visible' : 'hidden');
314 }
315
Simon Hunt99c13842014-11-06 18:23:12 -0800316 function cycleLabels() {
Thomas Vachuska60d72bf2014-11-21 13:02:00 -0800317 deviceLabelIndex = (deviceLabelIndex === 2)
Simon Huntbb282f52014-11-10 11:08:19 -0800318 ? 0 : deviceLabelIndex + 1;
Simon Hunt5f36d342014-11-08 21:33:14 -0800319
Simon Hunt99c13842014-11-06 18:23:12 -0800320 network.nodes.forEach(function (d) {
Simon Huntbb282f52014-11-10 11:08:19 -0800321 if (d.class === 'device') {
322 updateDeviceLabel(d);
323 }
Simon Hunt99c13842014-11-06 18:23:12 -0800324 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800325 }
326
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800327 function cycleHoverMode(view) {
Thomas Vachuska29617e52014-11-20 03:17:46 -0800328 hoverMode++;
329 if (hoverMode === hoverModes.length) {
330 hoverMode = 0;
331 }
Simon Hunta3dd9572014-11-20 15:22:41 -0800332 view.flash('Hover Mode: ' + hoverModes[hoverMode]);
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -0800333 }
334
Simon Hunt934c3ce2014-11-05 11:45:07 -0800335 function togglePorts(view) {
Simon Hunt50128c02014-11-08 13:36:15 -0800336 view.alert('togglePorts() callback')
Simon Hunt934c3ce2014-11-05 11:45:07 -0800337 }
338
Simon Hunt6ac93f32014-11-13 12:17:27 -0800339 function unpin() {
340 if (hovered) {
341 hovered.fixed = false;
342 hovered.el.classed('fixed', false);
343 network.force.resume();
344 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800345 }
346
Simon Hunt9462e8c2014-11-14 17:28:09 -0800347 function handleEscape(view) {
348 if (oiShowMaster) {
349 cancelAffinity();
350 } else {
351 deselectAll();
352 }
353 }
354
Simon Hunt934c3ce2014-11-05 11:45:07 -0800355 // ==============================
356 // Radio Button Callbacks
357
Simon Hunta5e89142014-11-14 07:00:33 -0800358 var layerLookup = {
359 host: {
Simon Hunt209155e2014-11-21 12:16:09 -0800360 endstation: 'pkt', // default, if host event does not define type
Thomas Vachuska89543292014-11-19 11:28:33 -0800361 router: 'pkt',
Simon Hunta5e89142014-11-14 07:00:33 -0800362 bgpSpeaker: 'pkt'
363 },
364 device: {
365 switch: 'pkt',
366 roadm: 'opt'
367 },
368 link: {
369 hostLink: 'pkt',
370 direct: 'pkt',
Simon Hunt8257f4c2014-11-16 19:34:54 -0800371 indirect: '',
372 tunnel: '',
Simon Hunta5e89142014-11-14 07:00:33 -0800373 optical: 'opt'
374 }
375 };
376
377 function inLayer(d, layer) {
Simon Hunt8257f4c2014-11-16 19:34:54 -0800378 var type = d.class === 'link' ? d.type() : d.type,
379 look = layerLookup[d.class],
380 lyr = look && look[type];
Simon Hunta5e89142014-11-14 07:00:33 -0800381 return lyr === layer;
382 }
383
384 function unsuppressLayer(which) {
385 node.each(function (d) {
386 var node = d.el;
387 if (inLayer(d, which)) {
388 node.classed('suppressed', false);
389 }
390 });
391
392 link.each(function (d) {
393 var link = d.el;
394 if (inLayer(d, which)) {
395 link.classed('suppressed', false);
396 }
397 });
398 }
399
Simon Hunt9462e8c2014-11-14 17:28:09 -0800400 function suppressLayers(b) {
401 node.classed('suppressed', b);
402 link.classed('suppressed', b);
Simon Hunt142d0032014-11-04 20:13:09 -0800403// d3.selectAll('svg .port').classed('inactive', false);
404// d3.selectAll('svg .portText').classed('inactive', false);
Simon Hunt195cb382014-11-03 17:50:51 -0800405 }
406
Simon Hunt9462e8c2014-11-14 17:28:09 -0800407 function showAllLayers() {
408 suppressLayers(false);
409 }
410
Simon Hunt195cb382014-11-03 17:50:51 -0800411 function showPacketLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800412 node.classed('suppressed', true);
413 link.classed('suppressed', true);
414 unsuppressLayer('pkt');
Simon Hunt195cb382014-11-03 17:50:51 -0800415 }
416
417 function showOpticalLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800418 node.classed('suppressed', true);
419 link.classed('suppressed', true);
420 unsuppressLayer('opt');
Simon Hunt195cb382014-11-03 17:50:51 -0800421 }
422
Simon Hunt9462e8c2014-11-14 17:28:09 -0800423 function restoreLayerState() {
424 layerBtnDispatch[layerBtnSet.selected()]();
425 }
426
Simon Hunt142d0032014-11-04 20:13:09 -0800427 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800428 // Private functions
429
Simon Hunt99c13842014-11-06 18:23:12 -0800430 function safeId(s) {
431 return s.replace(/[^a-z0-9]/gi, '-');
432 }
433
Simon Huntc7ee0662014-11-05 16:44:37 -0800434 // set the size of the given element to that of the view (reduced if padded)
435 function setSize(el, view, pad) {
436 var padding = pad ? pad * 2 : 0;
Simon Hunt934c3ce2014-11-05 11:45:07 -0800437 el.attr({
Simon Huntc7ee0662014-11-05 16:44:37 -0800438 width: view.width() - padding,
439 height: view.height() - padding
Simon Hunt934c3ce2014-11-05 11:45:07 -0800440 });
441 }
442
Simon Hunt8257f4c2014-11-16 19:34:54 -0800443 function makeNodeKey(d, what) {
444 var port = what + 'Port';
445 return d[what] + '/' + d[port];
446 }
447
448 function makeLinkKey(d, flipped) {
449 var one = flipped ? makeNodeKey(d, 'dst') : makeNodeKey(d, 'src'),
450 two = flipped ? makeNodeKey(d, 'src') : makeNodeKey(d, 'dst');
451 return one + '-' + two;
452 }
453
Simon Hunt269670f2014-11-17 16:17:43 -0800454 function findLinkById(id) {
455 // check to see if this is a reverse lookup, else default to given id
456 var key = network.revLinkToKey[id] || id;
457 return key && network.lookup[key];
458 }
459
Simon Hunt8257f4c2014-11-16 19:34:54 -0800460 function findLink(linkData, op) {
461 var key = makeLinkKey(linkData),
462 keyrev = makeLinkKey(linkData, 1),
463 link = network.lookup[key],
464 linkRev = network.lookup[keyrev],
465 result = {},
466 ldata = link || linkRev,
467 rawLink;
468
469 if (op === 'add') {
470 if (link) {
471 // trying to add a link that we already know about
472 result.ldata = link;
473 result.badLogic = 'addLink: link already added';
474
475 } else if (linkRev) {
476 // we found the reverse of the link to be added
477 result.ldata = linkRev;
478 if (linkRev.fromTarget) {
479 result.badLogic = 'addLink: link already added';
480 }
481 }
482 } else if (op === 'update') {
483 if (!ldata) {
484 result.badLogic = 'updateLink: link not found';
485 } else {
486 rawLink = link ? ldata.fromSource : ldata.fromTarget;
487 result.updateWith = function (data) {
488 $.extend(rawLink, data);
489 restyleLinkElement(ldata);
490 }
491 }
492 } else if (op === 'remove') {
493 if (!ldata) {
494 result.badLogic = 'removeLink: link not found';
495 } else {
496 rawLink = link ? ldata.fromSource : ldata.fromTarget;
497
498 if (!rawLink) {
499 result.badLogic = 'removeLink: link not found';
500
501 } else {
502 result.removeRawLink = function () {
503 if (link) {
504 // remove fromSource
505 ldata.fromSource = null;
506 if (ldata.fromTarget) {
507 // promote target into source position
508 ldata.fromSource = ldata.fromTarget;
509 ldata.fromTarget = null;
510 ldata.key = keyrev;
511 delete network.lookup[key];
512 network.lookup[keyrev] = ldata;
Simon Hunt269670f2014-11-17 16:17:43 -0800513 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800514 }
515 } else {
516 // remove fromTarget
517 ldata.fromTarget = null;
Simon Hunt269670f2014-11-17 16:17:43 -0800518 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800519 }
520 if (ldata.fromSource) {
521 restyleLinkElement(ldata);
522 } else {
523 removeLinkElement(ldata);
524 }
525 }
526 }
527 }
528 }
529 return result;
530 }
531
532 function addLinkUpdate(ldata, link) {
533 // add link event, but we already have the reverse link installed
534 ldata.fromTarget = link;
Simon Hunt269670f2014-11-17 16:17:43 -0800535 network.revLinkToKey[link.id] = ldata.key;
Simon Hunt8257f4c2014-11-16 19:34:54 -0800536 restyleLinkElement(ldata);
537 }
538
539 var allLinkTypes = 'direct indirect optical tunnel',
540 defaultLinkType = 'direct';
541
542 function restyleLinkElement(ldata) {
543 // this fn's job is to look at raw links and decide what svg classes
544 // need to be applied to the line element in the DOM
545 var el = ldata.el,
546 type = ldata.type(),
547 lw = ldata.linkWidth(),
548 online = ldata.online();
549
550 el.classed('link', true);
551 el.classed('inactive', !online);
552 el.classed(allLinkTypes, false);
553 if (type) {
554 el.classed(type, true);
555 }
556 el.transition()
557 .duration(1000)
Thomas Vachuska89543292014-11-19 11:28:33 -0800558 .attr('stroke-width', linkScale(lw))
559 .attr('stroke', config.topo.linkBaseColor);
Simon Hunt8257f4c2014-11-16 19:34:54 -0800560 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800561
Simon Hunt99c13842014-11-06 18:23:12 -0800562 // ==============================
563 // Event handlers for server-pushed events
564
Simon Huntbb282f52014-11-10 11:08:19 -0800565 function logicError(msg) {
566 // TODO, report logic error to server, via websock, so it can be logged
Simon Huntcb56cff2014-11-17 11:42:26 -0800567 //network.view.alert('Logic Error:\n\n' + msg);
Simon Huntfc274c92014-11-11 11:05:46 -0800568 console.warn(msg);
Simon Huntbb282f52014-11-10 11:08:19 -0800569 }
570
Simon Hunt99c13842014-11-06 18:23:12 -0800571 var eventDispatch = {
Simon Hunta5e89142014-11-14 07:00:33 -0800572 addInstance: addInstance,
Simon Hunt99c13842014-11-06 18:23:12 -0800573 addDevice: addDevice,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800574 addLink: addLink,
Simon Hunt56d51852014-11-09 13:03:35 -0800575 addHost: addHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800576
Simon Huntfcfb46c2014-11-19 12:53:38 -0800577 updateInstance: updateInstance,
Simon Huntbb282f52014-11-10 11:08:19 -0800578 updateDevice: updateDevice,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800579 updateLink: updateLink,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800580 updateHost: updateHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800581
Simon Huntd72bc702014-11-13 18:38:04 -0800582 removeInstance: stillToImplement,
Simon Huntbb282f52014-11-10 11:08:19 -0800583 removeDevice: stillToImplement,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800584 removeLink: removeLink,
Simon Hunt44031102014-11-11 13:20:36 -0800585 removeHost: removeHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800586
Simon Hunt61d04042014-11-11 17:27:16 -0800587 showDetails: showDetails,
Simon Huntb53e0682014-11-12 13:32:01 -0800588 showTraffic: showTraffic
Simon Hunt99c13842014-11-06 18:23:12 -0800589 };
590
Simon Hunta5e89142014-11-14 07:00:33 -0800591 function addInstance(data) {
592 evTrace(data);
593 var inst = data.payload,
594 id = inst.id;
595 if (onosInstances[id]) {
596 logicError('ONOS instance already added: ' + id);
597 return;
598 }
599 onosInstances[id] = inst;
600 onosOrder.push(inst);
601 updateInstances();
602 }
603
Simon Hunt99c13842014-11-06 18:23:12 -0800604 function addDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800605 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800606 var device = data.payload,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800607 nodeData = createDeviceNode(device);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800608 network.nodes.push(nodeData);
609 network.lookup[nodeData.id] = nodeData;
Simon Hunt99c13842014-11-06 18:23:12 -0800610 updateNodes();
611 network.force.start();
612 }
613
Simon Hunt99c13842014-11-06 18:23:12 -0800614 function addLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800615 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800616 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800617 result = findLink(link, 'add'),
618 bad = result.badLogic,
619 ldata = result.ldata;
620
621 if (bad) {
622 logicError(bad + ': ' + link.id);
623 return;
624 }
625
626 if (ldata) {
627 // we already have a backing store link for src/dst nodes
628 addLinkUpdate(ldata, link);
629 return;
630 }
631
632 // no backing store link yet
633 ldata = createLink(link);
634 if (ldata) {
635 network.links.push(ldata);
636 network.lookup[ldata.key] = ldata;
Simon Hunt99c13842014-11-06 18:23:12 -0800637 updateLinks();
638 network.force.start();
639 }
640 }
641
Simon Hunt56d51852014-11-09 13:03:35 -0800642 function addHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800643 evTrace(data);
Simon Hunt56d51852014-11-09 13:03:35 -0800644 var host = data.payload,
645 node = createHostNode(host),
646 lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800647 network.nodes.push(node);
648 network.lookup[host.id] = node;
649 updateNodes();
650
651 lnk = createHostLink(host);
652 if (lnk) {
Simon Hunt44031102014-11-11 13:20:36 -0800653 node.linkData = lnk; // cache ref on its host
Simon Hunt56d51852014-11-09 13:03:35 -0800654 network.links.push(lnk);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800655 network.lookup[host.ingress] = lnk;
656 network.lookup[host.egress] = lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800657 updateLinks();
658 }
659 network.force.start();
660 }
661
Simon Hunt44031102014-11-11 13:20:36 -0800662 // TODO: fold updateX(...) methods into one base method; remove duplication
Simon Hunt56a2ea42014-11-19 12:39:31 -0800663
664 function updateInstance(data) {
665 evTrace(data);
666 var inst = data.payload,
667 id = inst.id,
668 instData = onosInstances[id];
669 if (instData) {
670 $.extend(instData, inst);
671 updateInstances();
Simon Hunt56a2ea42014-11-19 12:39:31 -0800672 } else {
673 logicError('updateInstance lookup fail. ID = "' + id + '"');
674 }
675 }
676
Simon Huntbb282f52014-11-10 11:08:19 -0800677 function updateDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800678 evTrace(data);
Simon Huntbb282f52014-11-10 11:08:19 -0800679 var device = data.payload,
680 id = device.id,
681 nodeData = network.lookup[id];
682 if (nodeData) {
683 $.extend(nodeData, device);
684 updateDeviceState(nodeData);
685 } else {
686 logicError('updateDevice lookup fail. ID = "' + id + '"');
687 }
688 }
689
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800690 function updateLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800691 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800692 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800693 result = findLink(link, 'update'),
694 bad = result.badLogic;
695 if (bad) {
696 logicError(bad + ': ' + link.id);
697 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800698 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800699 result.updateWith(link);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800700 }
701
Simon Hunt7cd48f32014-11-09 23:42:50 -0800702 function updateHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800703 evTrace(data);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800704 var host = data.payload,
Simon Huntbb282f52014-11-10 11:08:19 -0800705 id = host.id,
706 hostData = network.lookup[id];
707 if (hostData) {
708 $.extend(hostData, host);
709 updateHostState(hostData);
710 } else {
711 logicError('updateHost lookup fail. ID = "' + id + '"');
712 }
Simon Hunt7cd48f32014-11-09 23:42:50 -0800713 }
714
Simon Hunt44031102014-11-11 13:20:36 -0800715 // TODO: fold removeX(...) methods into base method - remove dup code
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800716 function removeLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800717 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800718 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800719 result = findLink(link, 'remove'),
720 bad = result.badLogic;
721 if (bad) {
722 logicError(bad + ': ' + link.id);
723 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800724 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800725 result.removeRawLink();
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800726 }
727
Simon Hunt44031102014-11-11 13:20:36 -0800728 function removeHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800729 evTrace(data);
Simon Hunt44031102014-11-11 13:20:36 -0800730 var host = data.payload,
731 id = host.id,
732 hostData = network.lookup[id];
733 if (hostData) {
734 removeHostElement(hostData);
735 } else {
736 logicError('removeHost lookup fail. ID = "' + id + '"');
737 }
738 }
739
Simon Hunt61d04042014-11-11 17:27:16 -0800740 function showDetails(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800741 evTrace(data);
Simon Hunt61d04042014-11-11 17:27:16 -0800742 populateDetails(data.payload);
743 detailPane.show();
744 }
745
Simon Huntb53e0682014-11-12 13:32:01 -0800746 function showTraffic(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800747 evTrace(data);
Thomas Vachuska4731f122014-11-20 04:56:19 -0800748 var paths = data.payload.paths,
749 hasTraffic = false;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800750
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800751 // Revert any links hilighted previously.
Thomas Vachuska4731f122014-11-20 04:56:19 -0800752 link.style('stroke-width', null)
Thomas Vachuskaa3148a72014-11-19 21:38:35 -0800753 .classed('primary secondary animated optical', false);
Simon Hunte2575b62014-11-18 15:25:53 -0800754 // Remove all previous labels.
755 removeLinkLabels();
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800756
Simon Hunte2575b62014-11-18 15:25:53 -0800757 // Now hilight all links in the paths payload, and attach
758 // labels to them, if they are defined.
Simon Hunta255a2c2014-11-13 22:29:35 -0800759 paths.forEach(function (p) {
Simon Hunte2575b62014-11-18 15:25:53 -0800760 var n = p.links.length,
761 i,
762 ldata;
763
Thomas Vachuska4731f122014-11-20 04:56:19 -0800764 hasTraffic = hasTraffic || p.traffic;
Simon Hunte2575b62014-11-18 15:25:53 -0800765 for (i=0; i<n; i++) {
766 ldata = findLinkById(p.links[i]);
Thomas Vachuska4731f122014-11-20 04:56:19 -0800767 if (ldata && ldata.el) {
Simon Hunte2575b62014-11-18 15:25:53 -0800768 ldata.el.classed(p.class, true);
769 ldata.label = p.labels[i];
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800770 }
Simon Hunte2575b62014-11-18 15:25:53 -0800771 }
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800772 });
Thomas Vachuska4731f122014-11-20 04:56:19 -0800773
Simon Hunte2575b62014-11-18 15:25:53 -0800774 updateLinks();
Thomas Vachuska4731f122014-11-20 04:56:19 -0800775
776 if (hasTraffic && !antTimer) {
777 startAntTimer();
778 } else if (!hasTraffic && antTimer) {
779 stopAntTimer();
780 }
Simon Huntb53e0682014-11-12 13:32:01 -0800781 }
782
Simon Hunt56d51852014-11-09 13:03:35 -0800783 // ...............................
784
785 function stillToImplement(data) {
786 var p = data.payload;
787 note(data.event, p.id);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800788 network.view.alert('Not yet implemented: "' + data.event + '"');
Simon Hunt56d51852014-11-09 13:03:35 -0800789 }
Simon Hunt99c13842014-11-06 18:23:12 -0800790
791 function unknownEvent(data) {
Simon Hunt50128c02014-11-08 13:36:15 -0800792 network.view.alert('Unknown event type: "' + data.event + '"');
Simon Hunt99c13842014-11-06 18:23:12 -0800793 }
794
795 function handleServerEvent(data) {
796 var fn = eventDispatch[data.event] || unknownEvent;
797 fn(data);
798 }
799
800 // ==============================
Simon Hunt61d04042014-11-11 17:27:16 -0800801 // Out-going messages...
802
Simon Huntb53e0682014-11-12 13:32:01 -0800803 function userFeedback(msg) {
804 // for now, use the alert pane as is. Maybe different alert style in
805 // the future (centered on view; dismiss button?)
806 network.view.alert(msg);
807 }
808
809 function nSel() {
810 return selectOrder.length;
811 }
Simon Hunt61d04042014-11-11 17:27:16 -0800812 function getSel(idx) {
813 return selections[selectOrder[idx]];
814 }
Simon Huntb53e0682014-11-12 13:32:01 -0800815 function getSelId(idx) {
816 return getSel(idx).obj.id;
817 }
818 function allSelectionsClass(cls) {
819 for (var i=0, n=nSel(); i<n; i++) {
820 if (getSel(i).obj.class !== cls) {
821 return false;
822 }
823 }
824 return true;
825 }
Simon Hunt61d04042014-11-11 17:27:16 -0800826
Simon Hunt61d04042014-11-11 17:27:16 -0800827 // request details for the selected element
Simon Huntd72bc702014-11-13 18:38:04 -0800828 // invoked from selection of a single node.
Simon Hunt61d04042014-11-11 17:27:16 -0800829 function requestDetails() {
830 var data = getSel(0).obj,
831 payload = {
832 id: data.id,
833 class: data.class
834 };
835 sendMessage('requestDetails', payload);
836 }
837
Simon Huntd72bc702014-11-13 18:38:04 -0800838 function addIntentAction() {
839 sendMessage('addHostIntent', {
840 one: getSelId(0),
Thomas Vachuska82f2c622014-11-17 12:23:18 -0800841 two: getSelId(1),
842 ids: [ getSelId(0), getSelId(1) ]
Simon Huntd72bc702014-11-13 18:38:04 -0800843 });
844 }
845
846 function showTrafficAction() {
Thomas Vachuska29617e52014-11-20 03:17:46 -0800847 // force intents hover mode
848 hoverMode = 1;
849 showSelectTraffic();
850 }
851
852 function showSelectTraffic() {
Simon Huntd72bc702014-11-13 18:38:04 -0800853 // if nothing is hovered over, and nothing selected, send cancel request
854 if (!hovered && nSel() === 0) {
855 sendMessage('cancelTraffic', {});
856 return;
857 }
858
859 // NOTE: hover is only populated if "show traffic on hover" is
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800860 // toggled on, and the item hovered is a host or a device...
861 var hoverId = (trafficHover() && hovered &&
862 (hovered.class === 'host' || hovered.class === 'device'))
Simon Huntd72bc702014-11-13 18:38:04 -0800863 ? hovered.id : '';
864 sendMessage('requestTraffic', {
865 ids: selectOrder,
866 hover: hoverId
867 });
868 }
869
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800870 function showAllTrafficAction() {
871 sendMessage('requestAllTraffic', {});
872 }
873
Thomas Vachuska29617e52014-11-20 03:17:46 -0800874 function showDeviceLinkFlowsAction() {
875 // force intents hover mode
876 hoverMode = 2;
877 showDeviceLinkFlows();
878 }
879
880 function showDeviceLinkFlows() {
881 // if nothing is hovered over, and nothing selected, send cancel request
882 if (!hovered && nSel() === 0) {
883 sendMessage('cancelTraffic', {});
884 return;
885 }
886 var hoverId = (flowsHover() && hovered && hovered.class === 'device') ?
887 hovered.id : '';
888 sendMessage('requestDeviceLinkFlows', {
889 ids: selectOrder,
890 hover: hoverId
891 });
892 }
Simon Huntd72bc702014-11-13 18:38:04 -0800893
Simon Hunta6a9fe72014-11-20 11:17:12 -0800894 // TODO: these should be moved out to utility module.
Simon Hunt95908012014-11-20 10:20:26 -0800895 function stripPx(s) {
896 return s.replace(/px$/,'');
897 }
Simon Hunta6a9fe72014-11-20 11:17:12 -0800898
899 function appendGlyph(svg, ox, oy, dim, iid) {
900 svg.append('use').attr({
901 class: 'glyphIcon',
902 transform: translate(ox,oy),
903 'xlink:href': iid,
904 width: dim,
905 height: dim
906
907 });
908 }
909
Simon Hunt61d04042014-11-11 17:27:16 -0800910 // ==============================
Simon Hunta5e89142014-11-14 07:00:33 -0800911 // onos instance panel functions
912
913 function updateInstances() {
914 var onoses = oiBox.el.selectAll('.onosInst')
915 .data(onosOrder, function (d) { return d.id; });
916
917 // operate on existing onoses if necessary
Simon Huntfcfb46c2014-11-19 12:53:38 -0800918 onoses.classed('online', function (d) { return d.online; });
Simon Hunta5e89142014-11-14 07:00:33 -0800919
920 var entering = onoses.enter()
921 .append('div')
922 .attr('class', 'onosInst')
923 .classed('online', function (d) { return d.online; })
Simon Hunt9c15eca2014-11-15 18:37:59 -0800924 .on('click', clickInst);
925
926 entering.each(function (d, i) {
927 var el = d3.select(this),
928 img;
Simon Hunt95908012014-11-20 10:20:26 -0800929 var css = window.getComputedStyle(this),
930 w = stripPx(css.width),
931 h = stripPx(css.height) / 2;
Simon Hunt9c15eca2014-11-15 18:37:59 -0800932
Simon Hunt95908012014-11-20 10:20:26 -0800933 var svg = el.append('svg').attr({
934 width: w,
935 height: h
936 });
937 var dim = 30;
Simon Hunta6a9fe72014-11-20 11:17:12 -0800938 appendGlyph(svg, 2, 2, 30, '#node');
Simon Hunt9c15eca2014-11-15 18:37:59 -0800939
940 $('<div>').attr('class', 'onosTitle').text(d.id).appendTo(el);
941
942 // is the UI attached to this instance?
943 // TODO: need uiAttached boolean in instance data
Simon Hunta6a9fe72014-11-20 11:17:12 -0800944 // TODO: use SVG glyph, not png..
Simon Hunt9c15eca2014-11-15 18:37:59 -0800945 //if (d.uiAttached) {
946 if (i === 0) {
947 $('<img src="img/ui.png">').attr('class','ui').appendTo(el);
948 }
949 });
Simon Hunta5e89142014-11-14 07:00:33 -0800950
951 // operate on existing + new onoses here
952
953 // the departed...
954 var exiting = onoses.exit()
955 .transition()
956 .style('opacity', 0)
957 .remove();
958 }
959
Simon Hunt9462e8c2014-11-14 17:28:09 -0800960 function clickInst(d) {
961 var el = d3.select(this),
962 aff = el.classed('affinity');
963 if (!aff) {
964 setAffinity(el, d);
965 } else {
966 cancelAffinity();
967 }
968 }
969
970 function setAffinity(el, d) {
971 d3.selectAll('.onosInst')
972 .classed('mastership', true)
973 .classed('affinity', false);
974 el.classed('affinity', true);
975
976 suppressLayers(true);
977 node.each(function (n) {
978 if (n.master === d.id) {
979 n.el.classed('suppressed', false);
980 }
981 });
982 oiShowMaster = true;
983 }
984
985 function cancelAffinity() {
986 d3.selectAll('.onosInst')
987 .classed('mastership affinity', false);
988 restoreLayerState();
989 oiShowMaster = false;
990 }
991
Simon Hunta5e89142014-11-14 07:00:33 -0800992 // ==============================
Simon Hunt99c13842014-11-06 18:23:12 -0800993 // force layout modification functions
994
995 function translate(x, y) {
996 return 'translate(' + x + ',' + y + ')';
997 }
998
Simon Hunte2575b62014-11-18 15:25:53 -0800999 function rotate(deg) {
1000 return 'rotate(' + deg + ')';
1001 }
1002
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001003 function missMsg(what, id) {
1004 return '\n[' + what + '] "' + id + '" missing ';
1005 }
1006
1007 function linkEndPoints(srcId, dstId) {
1008 var srcNode = network.lookup[srcId],
1009 dstNode = network.lookup[dstId],
1010 sMiss = !srcNode ? missMsg('src', srcId) : '',
1011 dMiss = !dstNode ? missMsg('dst', dstId) : '';
1012
1013 if (sMiss || dMiss) {
1014 logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
1015 return null;
1016 }
1017 return {
1018 source: srcNode,
1019 target: dstNode,
1020 x1: srcNode.x,
1021 y1: srcNode.y,
1022 x2: dstNode.x,
1023 y2: dstNode.y
1024 };
1025 }
1026
Simon Hunt56d51852014-11-09 13:03:35 -08001027 function createHostLink(host) {
1028 var src = host.id,
1029 dst = host.cp.device,
Simon Hunt7cd48f32014-11-09 23:42:50 -08001030 id = host.ingress,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001031 lnk = linkEndPoints(src, dst);
Simon Hunt56d51852014-11-09 13:03:35 -08001032
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001033 if (!lnk) {
Simon Hunt56d51852014-11-09 13:03:35 -08001034 return null;
1035 }
1036
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001037 // Synthesize link ...
1038 $.extend(lnk, {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001039 key: id,
Simon Hunt56d51852014-11-09 13:03:35 -08001040 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -08001041
1042 type: function () { return 'hostLink'; },
1043 // TODO: ideally, we should see if our edge switch is online...
1044 online: function () { return true; },
1045 linkWidth: function () { return 1; }
Simon Hunt7cd48f32014-11-09 23:42:50 -08001046 });
Simon Hunt99c13842014-11-06 18:23:12 -08001047 return lnk;
1048 }
1049
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001050 function createLink(link) {
Simon Hunta6a9fe72014-11-20 11:17:12 -08001051 var lnk = linkEndPoints(link.src, link.dst);
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001052
1053 if (!lnk) {
1054 return null;
1055 }
1056
Simon Hunt8257f4c2014-11-16 19:34:54 -08001057 $.extend(lnk, {
1058 key: link.id,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001059 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -08001060 fromSource: link,
1061
1062 // functions to aggregate dual link state
1063 type: function () {
1064 var s = lnk.fromSource,
1065 t = lnk.fromTarget;
1066 return (s && s.type) || (t && t.type) || defaultLinkType;
1067 },
1068 online: function () {
1069 var s = lnk.fromSource,
1070 t = lnk.fromTarget;
1071 return (s && s.online) || (t && t.online);
1072 },
1073 linkWidth: function () {
1074 var s = lnk.fromSource,
1075 t = lnk.fromTarget,
1076 ws = (s && s.linkWidth) || 0,
1077 wt = (t && t.linkWidth) || 0;
1078 return Math.max(ws, wt);
1079 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001080 });
1081 return lnk;
Simon Hunt1a9eff92014-11-07 11:06:34 -08001082 }
1083
Simon Hunte2575b62014-11-18 15:25:53 -08001084 function removeLinkLabels() {
1085 network.links.forEach(function (d) {
1086 d.label = '';
1087 });
1088 }
1089
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001090 var widthRatio = 1.4,
1091 linkScale = d3.scale.linear()
1092 .domain([1, 12])
1093 .range([widthRatio, 12 * widthRatio])
1094 .clamp(true);
1095
Simon Hunt99c13842014-11-06 18:23:12 -08001096 function updateLinks() {
1097 link = linkG.selectAll('.link')
Simon Hunt8257f4c2014-11-16 19:34:54 -08001098 .data(network.links, function (d) { return d.key; });
Simon Hunt99c13842014-11-06 18:23:12 -08001099
1100 // operate on existing links, if necessary
1101 // link .foo() .bar() ...
1102
1103 // operate on entering links:
1104 var entering = link.enter()
1105 .append('line')
1106 .attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001107 x1: function (d) { return d.x1; },
1108 y1: function (d) { return d.y1; },
1109 x2: function (d) { return d.x2; },
1110 y2: function (d) { return d.y2; },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001111 stroke: config.topo.linkInColor,
1112 'stroke-width': config.topo.linkInWidth
Simon Hunt99c13842014-11-06 18:23:12 -08001113 });
1114
1115 // augment links
Simon Hunt7cd48f32014-11-09 23:42:50 -08001116 entering.each(function (d) {
1117 var link = d3.select(this);
1118 // provide ref to element selection from backing data....
1119 d.el = link;
Simon Hunt8257f4c2014-11-16 19:34:54 -08001120 restyleLinkElement(d);
Simon Hunt7cd48f32014-11-09 23:42:50 -08001121 });
Thomas Vachuska4830d392014-11-09 17:09:56 -08001122
1123 // operate on both existing and new links, if necessary
1124 //link .foo() .bar() ...
1125
Simon Hunte2575b62014-11-18 15:25:53 -08001126 // apply or remove labels
1127 var labelData = getLabelData();
1128 applyLinkLabels(labelData);
1129
Thomas Vachuska4830d392014-11-09 17:09:56 -08001130 // operate on exiting links:
Thomas Vachuska4830d392014-11-09 17:09:56 -08001131 link.exit()
Simon Hunt13bf9c82014-11-18 07:26:44 -08001132 .attr('stroke-dasharray', '3, 3')
1133 .style('opacity', 0.5)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001134 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -08001135 .duration(1500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001136 .attr({
Simon Hunt13bf9c82014-11-18 07:26:44 -08001137 'stroke-dasharray': '3, 12',
1138 stroke: config.topo.linkOutColor,
1139 'stroke-width': config.topo.linkOutWidth
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001140 })
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001141 .style('opacity', 0.0)
Thomas Vachuska4830d392014-11-09 17:09:56 -08001142 .remove();
Simon Hunte2575b62014-11-18 15:25:53 -08001143
1144 // NOTE: invoke a single tick to force the labels to position
1145 // onto their links.
1146 tick();
1147 }
1148
1149 function getLabelData() {
1150 // create the backing data for showing labels..
1151 var data = [];
1152 link.each(function (d) {
1153 if (d.label) {
1154 data.push({
1155 id: 'lab-' + d.key,
1156 key: d.key,
1157 label: d.label,
1158 ldata: d
1159 });
1160 }
1161 });
1162 return data;
1163 }
1164
1165 var linkLabelOffset = '0.3em';
1166
1167 function applyLinkLabels(data) {
1168 var entering;
1169
1170 linkLabel = linkLabelG.selectAll('.linkLabel')
1171 .data(data, function (d) { return d.id; });
1172
Simon Hunt56a2ea42014-11-19 12:39:31 -08001173 // for elements already existing, we need to update the text
1174 // and adjust the rectangle size to fit
1175 linkLabel.each(function (d) {
1176 var el = d3.select(this),
1177 rect = el.select('rect'),
1178 text = el.select('text');
1179 text.text(d.label);
1180 rect.attr(rectAroundText(el));
1181 });
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -08001182
Simon Hunte2575b62014-11-18 15:25:53 -08001183 entering = linkLabel.enter().append('g')
1184 .classed('linkLabel', true)
1185 .attr('id', function (d) { return d.id; });
1186
1187 entering.each(function (d) {
1188 var el = d3.select(this),
1189 rect,
1190 text,
1191 parms = {
1192 x1: d.ldata.x1,
1193 y1: d.ldata.y1,
1194 x2: d.ldata.x2,
1195 y2: d.ldata.y2
1196 };
1197
1198 d.el = el;
1199 rect = el.append('rect');
1200 text = el.append('text').text(d.label);
1201 rect.attr(rectAroundText(el));
1202 text.attr('dy', linkLabelOffset);
1203
1204 el.attr('transform', transformLabel(parms));
1205 });
1206
1207 // Remove any links that are no longer required.
1208 linkLabel.exit().remove();
1209 }
1210
1211 function rectAroundText(el) {
1212 var text = el.select('text'),
1213 box = text.node().getBBox();
1214
1215 // translate the bbox so that it is centered on [x,y]
1216 box.x = -box.width / 2;
1217 box.y = -box.height / 2;
1218
1219 // add padding
1220 box.x -= 1;
1221 box.width += 2;
1222 return box;
1223 }
1224
1225 function transformLabel(p) {
1226 var dx = p.x2 - p.x1,
1227 dy = p.y2 - p.y1,
1228 xMid = dx/2 + p.x1,
1229 yMid = dy/2 + p.y1;
Simon Hunte2575b62014-11-18 15:25:53 -08001230 return translate(xMid, yMid);
Simon Hunt99c13842014-11-06 18:23:12 -08001231 }
1232
1233 function createDeviceNode(device) {
1234 // start with the object as is
1235 var node = device,
Simon Huntbb282f52014-11-10 11:08:19 -08001236 type = device.type,
Simon Huntc72967b2014-11-20 09:21:42 -08001237 svgCls = type ? 'node device ' + type : 'node device',
1238 labels = device.labels || [];
1239
Simon Hunt99c13842014-11-06 18:23:12 -08001240 // Augment as needed...
1241 node.class = 'device';
Simon Huntbb282f52014-11-10 11:08:19 -08001242 node.svgClass = device.online ? svgCls + ' online' : svgCls;
Simon Hunt99c13842014-11-06 18:23:12 -08001243 positionNode(node);
Simon Hunt99c13842014-11-06 18:23:12 -08001244 return node;
1245 }
1246
Simon Hunt56d51852014-11-09 13:03:35 -08001247 function createHostNode(host) {
1248 // start with the object as is
1249 var node = host;
1250
1251 // Augment as needed...
1252 node.class = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001253 if (!node.type) {
Simon Hunt209155e2014-11-21 12:16:09 -08001254 node.type = 'endstation';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001255 }
Simon Hunt7fa116d2014-11-17 14:16:55 -08001256 node.svgClass = 'node host ' + node.type;
Simon Hunt56d51852014-11-09 13:03:35 -08001257 positionNode(node);
Simon Hunt56d51852014-11-09 13:03:35 -08001258 return node;
1259 }
1260
Simon Hunt99c13842014-11-06 18:23:12 -08001261 function positionNode(node) {
1262 var meta = node.metaUi,
Simon Huntac9e24f2014-11-12 10:12:21 -08001263 x = meta && meta.x,
1264 y = meta && meta.y,
1265 xy;
Simon Hunt99c13842014-11-06 18:23:12 -08001266
Simon Huntac9e24f2014-11-12 10:12:21 -08001267 // If we have [x,y] already, use that...
Simon Hunt99c13842014-11-06 18:23:12 -08001268 if (x && y) {
1269 node.fixed = true;
Simon Huntac9e24f2014-11-12 10:12:21 -08001270 node.x = x;
1271 node.y = y;
1272 return;
Simon Hunt99c13842014-11-06 18:23:12 -08001273 }
Simon Huntac9e24f2014-11-12 10:12:21 -08001274
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001275 var location = node.location;
1276 if (location && location.type === 'latlng') {
1277 var coord = geoMapProjection([location.lng, location.lat]);
1278 node.fixed = true;
1279 node.x = coord[0];
1280 node.y = coord[1];
1281 return;
1282 }
1283
Simon Huntac9e24f2014-11-12 10:12:21 -08001284 // Note: Placing incoming unpinned nodes at exactly the same point
1285 // (center of the view) causes them to explode outwards when
1286 // the force layout kicks in. So, we spread them out a bit
1287 // initially, to provide a more serene layout convergence.
1288 // Additionally, if the node is a host, we place it near
1289 // the device it is connected to.
1290
1291 function spread(s) {
1292 return Math.floor((Math.random() * s) - s/2);
1293 }
1294
1295 function randDim(dim) {
1296 return dim / 2 + spread(dim * 0.7071);
1297 }
1298
1299 function rand() {
1300 return {
1301 x: randDim(network.view.width()),
1302 y: randDim(network.view.height())
1303 };
1304 }
1305
1306 function near(node) {
1307 var min = 12,
1308 dx = spread(12),
1309 dy = spread(12);
1310 return {
1311 x: node.x + min + dx,
1312 y: node.y + min + dy
1313 };
1314 }
1315
1316 function getDevice(cp) {
1317 var d = network.lookup[cp.device];
1318 return d || rand();
1319 }
1320
1321 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
1322 $.extend(node, xy);
Simon Hunt99c13842014-11-06 18:23:12 -08001323 }
1324
Thomas Vachuska89543292014-11-19 11:28:33 -08001325 function iconUrl(d) {
1326 return 'img/' + d.type + '.png';
Simon Hunt99c13842014-11-06 18:23:12 -08001327 }
1328
Simon Huntc72967b2014-11-20 09:21:42 -08001329 function iconGlyphUrl(d) {
1330 var which = d.type || 'unknown';
1331 return '#' + which;
1332 }
1333
Simon Hunt99c13842014-11-06 18:23:12 -08001334 // returns the newly computed bounding box of the rectangle
1335 function adjustRectToFitText(n) {
1336 var text = n.select('text'),
1337 box = text.node().getBBox(),
1338 lab = config.labels;
1339
1340 text.attr('text-anchor', 'middle')
1341 .attr('y', '-0.8em')
1342 .attr('x', lab.imgPad/2);
1343
1344 // translate the bbox so that it is centered on [x,y]
1345 box.x = -box.width / 2;
1346 box.y = -box.height / 2;
1347
1348 // add padding
1349 box.x -= (lab.padLR + lab.imgPad/2);
1350 box.width += lab.padLR * 2 + lab.imgPad;
1351 box.y -= lab.padTB;
1352 box.height += lab.padTB * 2;
1353
1354 return box;
1355 }
1356
Simon Hunt1a9eff92014-11-07 11:06:34 -08001357 function mkSvgClass(d) {
1358 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
1359 }
1360
Simon Hunt7cd48f32014-11-09 23:42:50 -08001361 function hostLabel(d) {
1362 var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0;
1363 return d.labels[idx];
1364 }
1365 function deviceLabel(d) {
1366 var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0;
1367 return d.labels[idx];
1368 }
Simon Huntc72967b2014-11-20 09:21:42 -08001369 function trimLabel(label) {
1370 return (label && label.trim()) || '';
1371 }
1372
1373 function emptyBox() {
1374 return {
1375 x: -2,
1376 y: -2,
1377 width: 4,
1378 height: 4
1379 };
Simon Hunt7cd48f32014-11-09 23:42:50 -08001380 }
1381
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001382 function updateDeviceLabel(d) {
Simon Huntc72967b2014-11-20 09:21:42 -08001383 var label = trimLabel(deviceLabel(d)),
1384 noLabel = !label,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001385 node = d.el,
Simon Huntc72967b2014-11-20 09:21:42 -08001386 box,
1387 dx,
1388 dy;
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001389
1390 node.select('text')
1391 .text(label)
1392 .style('opacity', 0)
1393 .transition()
1394 .style('opacity', 1);
1395
Simon Huntc72967b2014-11-20 09:21:42 -08001396 if (noLabel) {
1397 box = emptyBox();
1398 dx = -config.icons.device.dim/2;
1399 dy = -config.icons.device.dim/2;
1400 } else {
1401 box = adjustRectToFitText(node);
1402 dx = box.x + config.icons.xoff;
1403 dy = box.y + config.icons.yoff;
1404 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001405
1406 node.select('rect')
1407 .transition()
1408 .attr(box);
1409
Simon Huntc72967b2014-11-20 09:21:42 -08001410 node.select('g.deviceIcon')
Thomas Vachuska89543292014-11-19 11:28:33 -08001411 .transition()
Simon Huntc72967b2014-11-20 09:21:42 -08001412 .attr('transform', translate(dx, dy));
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001413 }
1414
1415 function updateHostLabel(d) {
1416 var label = hostLabel(d),
1417 host = d.el;
1418
1419 host.select('text').text(label);
1420 }
1421
Simon Hunta6a9fe72014-11-20 11:17:12 -08001422 // TODO: should be using updateNodes() to do the upates!
Simon Huntbb282f52014-11-10 11:08:19 -08001423 function updateDeviceState(nodeData) {
1424 nodeData.el.classed('online', nodeData.online);
1425 updateDeviceLabel(nodeData);
1426 // TODO: review what else might need to be updated
1427 }
1428
1429 function updateHostState(hostData) {
1430 updateHostLabel(hostData);
1431 // TODO: review what else might need to be updated
1432 }
1433
Simon Hunt6ac93f32014-11-13 12:17:27 -08001434 function nodeMouseOver(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001435 hovered = d;
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -08001436 if (trafficHover() && (d.class === 'host' || d.class === 'device')) {
Thomas Vachuska29617e52014-11-20 03:17:46 -08001437 showSelectTraffic();
1438 } else if (flowsHover() && (d.class === 'device')) {
1439 showDeviceLinkFlows();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001440 }
1441 }
1442
1443 function nodeMouseOut(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001444 hovered = null;
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -08001445 if (trafficHover() && (d.class === 'host' || d.class === 'device')) {
Thomas Vachuska29617e52014-11-20 03:17:46 -08001446 showSelectTraffic();
1447 } else if (flowsHover() && (d.class === 'device')) {
1448 showDeviceLinkFlows();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001449 }
1450 }
Simon Huntbb282f52014-11-10 11:08:19 -08001451
Simon Hunteb1514d2014-11-20 09:57:29 -08001452 function addHostIcon(node, radius, iid) {
Thomas Vachuska89543292014-11-19 11:28:33 -08001453 var dim = radius * 1.5,
1454 xlate = -dim / 2;
1455
Simon Hunteb1514d2014-11-20 09:57:29 -08001456 node.append('use').attr({
1457 class: 'glyphIcon hostIcon',
1458 transform: translate(xlate,xlate),
1459 'xlink:href': iid,
1460 width: dim,
1461 height: dim
1462 });
Thomas Vachuska89543292014-11-19 11:28:33 -08001463 }
1464
Simon Hunt99c13842014-11-06 18:23:12 -08001465 function updateNodes() {
1466 node = nodeG.selectAll('.node')
1467 .data(network.nodes, function (d) { return d.id; });
1468
Simon Huntc72967b2014-11-20 09:21:42 -08001469 // TODO: operate on existing nodes
Simon Hunt7cd48f32014-11-09 23:42:50 -08001470 // update host labels
Simon Hunt99c13842014-11-06 18:23:12 -08001471 //node .foo() .bar() ...
1472
1473 // operate on entering nodes:
1474 var entering = node.enter()
1475 .append('g')
1476 .attr({
1477 id: function (d) { return safeId(d.id); },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001478 class: mkSvgClass,
Simon Hunt99c13842014-11-06 18:23:12 -08001479 transform: function (d) { return translate(d.x, d.y); },
1480 opacity: 0
1481 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08001482 .call(network.drag)
Simon Hunt6ac93f32014-11-13 12:17:27 -08001483 .on('mouseover', nodeMouseOver)
1484 .on('mouseout', nodeMouseOut)
Simon Hunt99c13842014-11-06 18:23:12 -08001485 .transition()
1486 .attr('opacity', 1);
1487
1488 // augment device nodes...
1489 entering.filter('.device').each(function (d) {
1490 var node = d3.select(this),
Simon Huntc72967b2014-11-20 09:21:42 -08001491 label = trimLabel(deviceLabel(d)),
1492 noLabel = !label,
Simon Hunt99c13842014-11-06 18:23:12 -08001493 box;
1494
Simon Hunt7cd48f32014-11-09 23:42:50 -08001495 // provide ref to element from backing data....
1496 d.el = node;
1497
Simon Hunt99c13842014-11-06 18:23:12 -08001498 node.append('rect')
1499 .attr({
Simon Hunta3dd9572014-11-20 15:22:41 -08001500 rx: 5,
1501 ry: 5
Simon Hunt99c13842014-11-06 18:23:12 -08001502 });
1503
1504 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001505 .text(label)
Simon Hunt99c13842014-11-06 18:23:12 -08001506 .attr('dy', '1.1em');
1507
1508 box = adjustRectToFitText(node);
Simon Hunta3dd9572014-11-20 15:22:41 -08001509 node.select('rect').attr(box);
Simon Huntc72967b2014-11-20 09:21:42 -08001510 addDeviceIcon(node, box, noLabel, iconGlyphUrl(d));
Simon Huntc7ee0662014-11-05 16:44:37 -08001511 });
Simon Hunt934c3ce2014-11-05 11:45:07 -08001512
Thomas Vachuska89543292014-11-19 11:28:33 -08001513 // TODO: better place for this configuration state
1514 var defaultHostRadius = 9,
1515 hostRadius = {
1516 bgpSpeaker: 14,
1517 router: 14,
Thomas Vachuska60d72bf2014-11-21 13:02:00 -08001518 endstation: 14
Thomas Vachuska89543292014-11-19 11:28:33 -08001519 },
Simon Hunteb1514d2014-11-20 09:57:29 -08001520 hostGlyphId = {
Thomas Vachuska89543292014-11-19 11:28:33 -08001521 bgpSpeaker: 'bgpSpeaker',
1522 router: 'router',
Thomas Vachuska60d72bf2014-11-21 13:02:00 -08001523 endstation: 'endstation'
Thomas Vachuska89543292014-11-19 11:28:33 -08001524 };
1525
1526
Simon Hunt56d51852014-11-09 13:03:35 -08001527 // augment host nodes...
1528 entering.filter('.host').each(function (d) {
1529 var node = d3.select(this),
Thomas Vachuska89543292014-11-19 11:28:33 -08001530 r = hostRadius[d.type] || defaultHostRadius,
1531 textDy = r + 10,
Simon Hunteb1514d2014-11-20 09:57:29 -08001532 iid = iconGlyphUrl(d);
Simon Hunt56d51852014-11-09 13:03:35 -08001533
Simon Hunt7cd48f32014-11-09 23:42:50 -08001534 // provide ref to element from backing data....
1535 d.el = node;
1536
Thomas Vachuska89543292014-11-19 11:28:33 -08001537 node.append('circle')
1538 .attr('r', r);
Simon Hunt7fa116d2014-11-17 14:16:55 -08001539
Simon Hunteb1514d2014-11-20 09:57:29 -08001540 if (iid) {
1541 addHostIcon(node, r, iid);
Simon Hunt7fa116d2014-11-17 14:16:55 -08001542 }
Simon Hunt56d51852014-11-09 13:03:35 -08001543
Simon Hunt56d51852014-11-09 13:03:35 -08001544 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001545 .text(hostLabel)
Thomas Vachuska89543292014-11-19 11:28:33 -08001546 .attr('dy', textDy)
Simon Hunt7cd48f32014-11-09 23:42:50 -08001547 .attr('text-anchor', 'middle');
Simon Hunt56d51852014-11-09 13:03:35 -08001548
1549 // debug function to show the modelled x,y coordinates of nodes...
1550 if (debug('showNodeXY')) {
1551 node.select('circle').attr('fill-opacity', 0.5);
1552 node.append('circle')
1553 .attr({
1554 class: 'debug',
1555 cx: 0,
1556 cy: 0,
1557 r: '3px'
1558 });
1559 }
1560 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001561
Simon Hunt99c13842014-11-06 18:23:12 -08001562 // operate on both existing and new nodes, if necessary
1563 //node .foo() .bar() ...
Simon Huntc7ee0662014-11-05 16:44:37 -08001564
Simon Hunt99c13842014-11-06 18:23:12 -08001565 // operate on exiting nodes:
Simon Huntea80eb42014-11-11 13:46:57 -08001566 // Note that the node is removed after 2 seconds.
1567 // Sub element animations should be shorter than 2 seconds.
1568 var exiting = node.exit()
Simon Hunt44031102014-11-11 13:20:36 -08001569 .transition()
1570 .duration(2000)
Simon Huntea80eb42014-11-11 13:46:57 -08001571 .style('opacity', 0)
Simon Hunt99c13842014-11-06 18:23:12 -08001572 .remove();
Simon Huntea80eb42014-11-11 13:46:57 -08001573
1574 // host node exits....
1575 exiting.filter('.host').each(function (d) {
1576 var node = d3.select(this);
1577
1578 node.select('text')
1579 .style('opacity', 0.5)
1580 .transition()
1581 .duration(1000)
1582 .style('opacity', 0);
1583 // note, leave <g>.remove to remove this element
1584
Thomas Vachuska89543292014-11-19 11:28:33 -08001585 node.select('circle')
1586 .style('stroke-fill', '#555')
1587 .style('fill', '#888')
Simon Huntea80eb42014-11-11 13:46:57 -08001588 .style('opacity', 0.5)
1589 .transition()
1590 .duration(1500)
1591 .attr('r', 0);
1592 // note, leave <g>.remove to remove this element
1593
1594 });
1595
1596 // TODO: device node exits
Simon Huntc7ee0662014-11-05 16:44:37 -08001597 }
1598
Simon Huntc72967b2014-11-20 09:21:42 -08001599 function addDeviceIcon(node, box, noLabel, iid) {
1600 var cfg = config.icons.device,
1601 dx,
1602 dy,
1603 g;
1604
1605 if (noLabel) {
1606 box = emptyBox();
1607 dx = -cfg.dim/2;
1608 dy = -cfg.dim/2;
1609 } else {
1610 box = adjustRectToFitText(node);
1611 dx = box.x + config.icons.xoff;
1612 dy = box.y + config.icons.yoff;
1613 }
1614
Simon Hunteb1514d2014-11-20 09:57:29 -08001615 g = node.append('g')
1616 .attr('class', 'glyphIcon deviceIcon')
Simon Huntc72967b2014-11-20 09:21:42 -08001617 .attr('transform', translate(dx, dy));
1618
1619 g.append('rect').attr({
1620 x: 0,
1621 y: 0,
1622 rx: cfg.rx,
1623 width: cfg.dim,
1624 height: cfg.dim
1625 });
1626
1627 g.append('use').attr({
1628 'xlink:href': iid,
1629 width: cfg.dim,
1630 height: cfg.dim
1631 });
1632
Simon Huntc72967b2014-11-20 09:21:42 -08001633 }
1634
Simon Hunt8257f4c2014-11-16 19:34:54 -08001635 function find(key, array) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001636 for (var idx = 0, n = array.length; idx < n; idx++) {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001637 if (array[idx].key === key) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001638 return idx;
1639 }
1640 }
1641 return -1;
1642 }
1643
1644 function removeLinkElement(linkData) {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001645 var idx = find(linkData.key, network.links),
1646 removed;
1647 if (idx >=0) {
1648 // remove from links array
1649 removed = network.links.splice(idx, 1);
1650 // remove from lookup cache
1651 delete network.lookup[removed[0].key];
1652 updateLinks();
1653 network.force.resume();
1654 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001655 }
Simon Huntc7ee0662014-11-05 16:44:37 -08001656
Simon Hunt44031102014-11-11 13:20:36 -08001657 function removeHostElement(hostData) {
1658 // first, remove associated hostLink...
1659 removeLinkElement(hostData.linkData);
1660
1661 // remove from lookup cache
1662 delete network.lookup[hostData.id];
1663 // remove from nodes array
1664 var idx = find(hostData.id, network.nodes);
1665 network.nodes.splice(idx, 1);
1666 // remove from SVG
1667 updateNodes();
1668 network.force.resume();
1669 }
1670
1671
Simon Huntc7ee0662014-11-05 16:44:37 -08001672 function tick() {
1673 node.attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001674 transform: function (d) { return translate(d.x, d.y); }
Simon Huntc7ee0662014-11-05 16:44:37 -08001675 });
1676
1677 link.attr({
1678 x1: function (d) { return d.source.x; },
1679 y1: function (d) { return d.source.y; },
1680 x2: function (d) { return d.target.x; },
1681 y2: function (d) { return d.target.y; }
1682 });
Simon Hunte2575b62014-11-18 15:25:53 -08001683
1684 linkLabel.each(function (d) {
1685 var el = d3.select(this);
Thomas Vachuska4731f122014-11-20 04:56:19 -08001686 var lnk = findLinkById(d.key);
1687
1688 if (lnk) {
1689 var parms = {
Simon Hunte2575b62014-11-18 15:25:53 -08001690 x1: lnk.source.x,
1691 y1: lnk.source.y,
1692 x2: lnk.target.x,
1693 y2: lnk.target.y
1694 };
Thomas Vachuska4731f122014-11-20 04:56:19 -08001695 el.attr('transform', transformLabel(parms));
1696 }
Simon Hunte2575b62014-11-18 15:25:53 -08001697 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001698 }
Simon Hunt934c3ce2014-11-05 11:45:07 -08001699
1700 // ==============================
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001701 // Web-Socket for live data
1702
1703 function webSockUrl() {
1704 return document.location.toString()
1705 .replace(/\#.*/, '')
1706 .replace('http://', 'ws://')
1707 .replace('https://', 'wss://')
1708 .replace('index2.html', config.webSockUrl);
1709 }
1710
1711 webSock = {
1712 ws : null,
1713
1714 connect : function() {
1715 webSock.ws = new WebSocket(webSockUrl());
1716
1717 webSock.ws.onopen = function() {
Simon Hunt0c6d4192014-11-12 12:07:10 -08001718 noWebSock(false);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001719 };
1720
1721 webSock.ws.onmessage = function(m) {
1722 if (m.data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001723 wsTraceRx(m.data);
Thomas Vachuskad472c6e2014-11-07 19:11:05 -08001724 handleServerEvent(JSON.parse(m.data));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001725 }
1726 };
1727
1728 webSock.ws.onclose = function(m) {
1729 webSock.ws = null;
Simon Hunt0c6d4192014-11-12 12:07:10 -08001730 noWebSock(true);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001731 };
1732 },
1733
1734 send : function(text) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001735 if (text != null) {
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001736 webSock._send(text);
1737 }
1738 },
1739
1740 _send : function(message) {
1741 if (webSock.ws) {
1742 webSock.ws.send(message);
Simon Hunta255a2c2014-11-13 22:29:35 -08001743 } else if (config.useLiveData) {
Simon Hunt56d51852014-11-09 13:03:35 -08001744 network.view.alert('no web socket open\n\n' + message);
Simon Hunta255a2c2014-11-13 22:29:35 -08001745 } else {
1746 console.log('WS Send: ' + JSON.stringify(message));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001747 }
1748 }
1749
1750 };
1751
Simon Hunt0c6d4192014-11-12 12:07:10 -08001752 function noWebSock(b) {
1753 mask.style('display',b ? 'block' : 'none');
1754 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001755
1756 function sendMessage(evType, payload) {
1757 var toSend = {
Simon Huntbb282f52014-11-10 11:08:19 -08001758 event: evType,
1759 sid: ++sid,
1760 payload: payload
1761 },
1762 asText = JSON.stringify(toSend);
1763 wsTraceTx(asText);
1764 webSock.send(asText);
Simon Huntc76ae892014-11-18 17:31:51 -08001765
1766 // Temporary measure for debugging UI behavior ...
1767 if (!config.useLiveData) {
1768 handleTestSend(toSend);
1769 }
Simon Huntbb282f52014-11-10 11:08:19 -08001770 }
1771
1772 function wsTraceTx(msg) {
1773 wsTrace('tx', msg);
1774 }
1775 function wsTraceRx(msg) {
1776 wsTrace('rx', msg);
1777 }
1778 function wsTrace(rxtx, msg) {
Simon Huntbb282f52014-11-10 11:08:19 -08001779 console.log('[' + rxtx + '] ' + msg);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001780 }
1781
Simon Huntc76ae892014-11-18 17:31:51 -08001782 // NOTE: Temporary hardcoded example for showing detail pane
1783 // while we fine-
1784 // Probably should not merge this change...
1785 function handleTestSend(msg) {
1786 if (msg.event === 'requestDetails') {
1787 showDetails({
1788 event: 'showDetails',
1789 sid: 1001,
1790 payload: {
1791 "id": "of:0000ffffffffff09",
1792 "type": "roadm",
1793 "propOrder": [
1794 "Name",
1795 "Vendor",
1796 "H/W Version",
1797 "S/W Version",
1798 "-",
1799 "Latitude",
1800 "Longitude",
1801 "Ports"
1802 ],
1803 "props": {
1804 "Name": null,
1805 "Vendor": "Linc",
1806 "H/W Version": "OE",
1807 "S/W Version": "?",
1808 "-": "",
1809 "Latitude": "40.8",
1810 "Longitude": "73.1",
1811 "Ports": "2"
1812 }
1813 }
1814 });
1815 }
1816 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001817
1818 // ==============================
1819 // Selection stuff
1820
1821 function selectObject(obj, el) {
1822 var n,
Simon Hunt01095ff2014-11-13 16:37:29 -08001823 srcEv = d3.event.sourceEvent,
1824 meta = srcEv.metaKey,
1825 shift = srcEv.shiftKey;
1826
Simon Huntdeab4322014-11-13 18:49:07 -08001827 if ((panZoom() && !meta) || (!panZoom() && meta)) {
Simon Hunt01095ff2014-11-13 16:37:29 -08001828 return;
1829 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001830
1831 if (el) {
1832 n = d3.select(el);
1833 } else {
1834 node.each(function(d) {
1835 if (d == obj) {
1836 n = d3.select(el = this);
1837 }
1838 });
1839 }
1840 if (!n) return;
1841
Simon Hunt01095ff2014-11-13 16:37:29 -08001842 if (shift && n.classed('selected')) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001843 deselectObject(obj.id);
Simon Hunt61d04042014-11-11 17:27:16 -08001844 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001845 return;
1846 }
1847
Simon Hunt01095ff2014-11-13 16:37:29 -08001848 if (!shift) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001849 deselectAll();
1850 }
1851
Simon Huntc31d5692014-11-12 13:27:18 -08001852 selections[obj.id] = { obj: obj, el: el };
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001853 selectOrder.push(obj.id);
1854
1855 n.classed('selected', true);
Simon Hunt61d04042014-11-11 17:27:16 -08001856 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001857 }
1858
1859 function deselectObject(id) {
Simon Huntc31d5692014-11-12 13:27:18 -08001860 var obj = selections[id],
1861 idx;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001862 if (obj) {
1863 d3.select(obj.el).classed('selected', false);
Simon Hunt61d04042014-11-11 17:27:16 -08001864 delete selections[id];
Simon Huntc31d5692014-11-12 13:27:18 -08001865 idx = $.inArray(id, selectOrder);
1866 if (idx >= 0) {
1867 selectOrder.splice(idx, 1);
1868 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001869 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001870 }
1871
1872 function deselectAll() {
1873 // deselect all nodes in the network...
1874 node.classed('selected', false);
1875 selections = {};
1876 selectOrder = [];
Simon Hunt61d04042014-11-11 17:27:16 -08001877 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001878 }
1879
Simon Hunt61d04042014-11-11 17:27:16 -08001880 // update the state of the detail pane, based on current selections
1881 function updateDetailPane() {
1882 var nSel = selectOrder.length;
1883 if (!nSel) {
1884 detailPane.hide();
Simon Huntd72bc702014-11-13 18:38:04 -08001885 showTrafficAction(); // sends cancelTraffic event
Simon Hunt61d04042014-11-11 17:27:16 -08001886 } else if (nSel === 1) {
1887 singleSelect();
1888 } else {
1889 multiSelect();
1890 }
1891 }
1892
1893 function singleSelect() {
1894 requestDetails();
Simon Huntb53e0682014-11-12 13:32:01 -08001895 // NOTE: detail pane will be shown from showDetails event callback
Simon Hunt61d04042014-11-11 17:27:16 -08001896 }
1897
1898 function multiSelect() {
Simon Huntb53e0682014-11-12 13:32:01 -08001899 populateMultiSelect();
Simon Huntb53e0682014-11-12 13:32:01 -08001900 }
1901
1902 function addSep(tbody) {
1903 var tr = tbody.append('tr');
1904 $('<hr>').appendTo(tr.append('td').attr('colspan', 2));
1905 }
1906
1907 function addProp(tbody, label, value) {
1908 var tr = tbody.append('tr');
1909
1910 tr.append('td')
1911 .attr('class', 'label')
1912 .text(label + ' :');
1913
1914 tr.append('td')
1915 .attr('class', 'value')
1916 .text(value);
1917 }
1918
1919 function populateMultiSelect() {
1920 detailPane.empty();
1921
Simon Hunta3dd9572014-11-20 15:22:41 -08001922 var title = detailPane.append('h3'),
1923 table = detailPane.append('table'),
1924 tbody = table.append('tbody');
Simon Huntb53e0682014-11-12 13:32:01 -08001925
Thomas Vachuska4731f122014-11-20 04:56:19 -08001926 title.text('Selected Nodes');
Simon Huntb53e0682014-11-12 13:32:01 -08001927
1928 selectOrder.forEach(function (d, i) {
1929 addProp(tbody, i+1, d);
1930 });
Simon Huntd72bc702014-11-13 18:38:04 -08001931
1932 addMultiSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08001933 }
1934
1935 function populateDetails(data) {
1936 detailPane.empty();
1937
Simon Hunta6a9fe72014-11-20 11:17:12 -08001938 var svg = detailPane.append('svg'),
1939 iid = iconGlyphUrl(data);
1940
Simon Hunta3dd9572014-11-20 15:22:41 -08001941 var title = detailPane.append('h2'),
1942 table = detailPane.append('table'),
1943 tbody = table.append('tbody');
Simon Hunt61d04042014-11-11 17:27:16 -08001944
Simon Hunta6a9fe72014-11-20 11:17:12 -08001945 appendGlyph(svg, 0, 0, 40, iid);
1946 title.text(data.id);
Simon Hunt61d04042014-11-11 17:27:16 -08001947
1948 data.propOrder.forEach(function(p) {
1949 if (p === '-') {
1950 addSep(tbody);
1951 } else {
1952 addProp(tbody, p, data.props[p]);
1953 }
1954 });
Simon Huntd72bc702014-11-13 18:38:04 -08001955
Thomas Vachuska4731f122014-11-20 04:56:19 -08001956 addSingleSelectActions(data);
Simon Hunt61d04042014-11-11 17:27:16 -08001957 }
1958
Thomas Vachuska4731f122014-11-20 04:56:19 -08001959 function addSingleSelectActions(data) {
Simon Huntd72bc702014-11-13 18:38:04 -08001960 detailPane.append('hr');
1961 // always want to allow 'show traffic'
Thomas Vachuska4731f122014-11-20 04:56:19 -08001962 addAction(detailPane, 'Show Related Traffic', showTrafficAction);
1963
1964 if (data.type === 'switch') {
1965 addAction(detailPane, 'Show Device Flows', showDeviceLinkFlowsAction);
1966 }
Simon Huntd72bc702014-11-13 18:38:04 -08001967 }
1968
1969 function addMultiSelectActions() {
1970 detailPane.append('hr');
1971 // always want to allow 'show traffic'
Thomas Vachuska4731f122014-11-20 04:56:19 -08001972 addAction(detailPane, 'Show Related Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001973 // if exactly two hosts are selected, also want 'add host intent'
1974 if (nSel() === 2 && allSelectionsClass('host')) {
Thomas Vachuska4731f122014-11-20 04:56:19 -08001975 addAction(detailPane, 'Add Host-to-Host Intent', addIntentAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001976 }
1977 }
1978
Simon Hunta5e89142014-11-14 07:00:33 -08001979 function addAction(panel, text, cb) {
1980 panel.append('div')
Simon Huntd72bc702014-11-13 18:38:04 -08001981 .classed('actionBtn', true)
1982 .text(text)
1983 .on('click', cb);
1984 }
1985
1986
Paul Greysonfcba0e82014-11-13 10:21:16 -08001987 function zoomPan(scale, translate) {
1988 zoomPanContainer.attr("transform", "translate(" + translate + ")scale(" + scale + ")");
1989 // keep the map lines constant width while zooming
Thomas Vachuska89543292014-11-19 11:28:33 -08001990 bgImg.style("stroke-width", 2.0 / scale + "px");
Paul Greysonfcba0e82014-11-13 10:21:16 -08001991 }
1992
1993 function resetZoomPan() {
1994 zoomPan(1, [0,0]);
1995 zoom.scale(1).translate([0,0]);
1996 }
1997
1998 function setupZoomPan() {
1999 function zoomed() {
Simon Huntdeab4322014-11-13 18:49:07 -08002000 if (!panZoom() ^ !d3.event.sourceEvent.metaKey) {
Paul Greysonfcba0e82014-11-13 10:21:16 -08002001 zoomPan(d3.event.scale, d3.event.translate);
2002 }
2003 }
2004
2005 zoom = d3.behavior.zoom()
2006 .translate([0, 0])
2007 .scale(1)
2008 .scaleExtent([1, 8])
2009 .on("zoom", zoomed);
2010
2011 svg.call(zoom);
2012 }
2013
Simon Hunt61d04042014-11-11 17:27:16 -08002014 // ==============================
2015 // Test harness code
Simon Hunt56d51852014-11-09 13:03:35 -08002016
2017 function prepareScenario(view, ctx, dbg) {
2018 var sc = scenario,
2019 urlSc = sc.evDir + ctx + sc.evScenario;
2020
2021 if (!ctx) {
2022 view.alert("No scenario specified (null ctx)");
2023 return;
2024 }
2025
2026 sc.view = view;
2027 sc.ctx = ctx;
2028 sc.debug = dbg;
2029 sc.evNumber = 0;
2030
2031 d3.json(urlSc, function(err, data) {
Simon Huntbb282f52014-11-10 11:08:19 -08002032 var p = data && data.params || {},
2033 desc = data && data.description || null,
Simon Huntfc274c92014-11-11 11:05:46 -08002034 intro = data && data.title;
Simon Huntbb282f52014-11-10 11:08:19 -08002035
Simon Hunt56d51852014-11-09 13:03:35 -08002036 if (err) {
2037 view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err);
2038 } else {
2039 sc.params = p;
Simon Huntbb282f52014-11-10 11:08:19 -08002040 if (desc) {
2041 intro += '\n\n ' + desc.join('\n ');
2042 }
2043 view.alert(intro);
Simon Hunt56d51852014-11-09 13:03:35 -08002044 }
2045 });
2046
2047 }
2048
Simon Hunt01095ff2014-11-13 16:37:29 -08002049 // ==============================
2050 // Toggle Buttons in masthead
Simon Hunt0c6d4192014-11-12 12:07:10 -08002051
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002052 // TODO: toggle button (and other widgets in the masthead) should be provided
2053 // by the framework; not generated by the view.
2054
Simon Hunta3dd9572014-11-20 15:22:41 -08002055 var showInstances;
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002056
2057 function addButtonBar(view) {
2058 var bb = d3.select('#mast')
2059 .append('span').classed('right', true).attr('id', 'bb');
2060
Simon Hunta5e89142014-11-14 07:00:33 -08002061 function mkTogBtn(text, cb) {
2062 return bb.append('span')
2063 .classed('btn', true)
2064 .text(text)
2065 .on('click', cb);
2066 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002067
Simon Hunta5e89142014-11-14 07:00:33 -08002068 showInstances = mkTogBtn('Show Instances', toggleInst);
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002069 }
2070
Simon Hunta5e89142014-11-14 07:00:33 -08002071 function instShown() {
2072 return showInstances.classed('active');
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002073 }
Simon Hunta5e89142014-11-14 07:00:33 -08002074 function toggleInst() {
2075 showInstances.classed('active', !instShown());
2076 if (instShown()) {
2077 oiBox.show();
2078 } else {
2079 oiBox.hide();
2080 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002081 }
2082
Simon Huntdeab4322014-11-13 18:49:07 -08002083 function panZoom() {
Simon Hunte5b71752014-11-18 20:06:07 -08002084 return false;
Simon Hunta5e89142014-11-14 07:00:33 -08002085 }
2086
2087 function trafficHover() {
Thomas Vachuska29617e52014-11-20 03:17:46 -08002088 return hoverModes[hoverMode] === 'intents';
Simon Hunta5e89142014-11-14 07:00:33 -08002089 }
Thomas Vachuska29617e52014-11-20 03:17:46 -08002090
2091 function flowsHover() {
2092 return hoverModes[hoverMode] === 'flows';
2093 }
2094
Simon Hunt7fa116d2014-11-17 14:16:55 -08002095 function loadGlyphs(svg) {
2096 var defs = svg.append('defs');
2097 gly.defBird(defs);
2098 gly.defBullhorn(defs);
Simon Huntc72967b2014-11-20 09:21:42 -08002099 gly.defGlyphs(defs);
Simon Hunt7fa116d2014-11-17 14:16:55 -08002100 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002101
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002102 // ==============================
Simon Hunt142d0032014-11-04 20:13:09 -08002103 // View life-cycle callbacks
Simon Hunt195cb382014-11-03 17:50:51 -08002104
Simon Huntf67722a2014-11-10 09:32:06 -08002105 function preload(view, ctx, flags) {
Simon Hunt142d0032014-11-04 20:13:09 -08002106 var w = view.width(),
2107 h = view.height(),
Simon Huntc7ee0662014-11-05 16:44:37 -08002108 fcfg = config.force,
2109 fpad = fcfg.pad,
2110 forceDim = [w - 2*fpad, h - 2*fpad];
Simon Hunt195cb382014-11-03 17:50:51 -08002111
Simon Hunt142d0032014-11-04 20:13:09 -08002112 // NOTE: view.$div is a D3 selection of the view's div
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002113 var viewBox = '0 0 ' + config.logicalSize + ' ' + config.logicalSize;
2114 svg = view.$div.append('svg').attr('viewBox', viewBox);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002115 setSize(svg, view);
2116
Simon Hunt7fa116d2014-11-17 14:16:55 -08002117 loadGlyphs(svg);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002118
Paul Greysonfcba0e82014-11-13 10:21:16 -08002119 zoomPanContainer = svg.append('g').attr('id', 'zoomPanContainer');
Paul Greysonfcba0e82014-11-13 10:21:16 -08002120 setupZoomPan();
2121
Simon Hunt1a9eff92014-11-07 11:06:34 -08002122 // add blue glow filter to svg layer
Paul Greysonfcba0e82014-11-13 10:21:16 -08002123 d3u.appendGlow(zoomPanContainer);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002124
Simon Huntc7ee0662014-11-05 16:44:37 -08002125 // group for the topology
Paul Greysonfcba0e82014-11-13 10:21:16 -08002126 topoG = zoomPanContainer.append('g')
Simon Huntd3b7d512014-11-12 15:48:41 -08002127 .attr('id', 'topo-G')
Simon Huntc7ee0662014-11-05 16:44:37 -08002128 .attr('transform', fcfg.translate());
2129
Simon Hunte2575b62014-11-18 15:25:53 -08002130 // subgroups for links, link labels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002131 linkG = topoG.append('g').attr('id', 'links');
Simon Hunte2575b62014-11-18 15:25:53 -08002132 linkLabelG = topoG.append('g').attr('id', 'linkLabels');
Simon Huntc7ee0662014-11-05 16:44:37 -08002133 nodeG = topoG.append('g').attr('id', 'nodes');
2134
Simon Hunte2575b62014-11-18 15:25:53 -08002135 // selection of links, linkLabels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002136 link = linkG.selectAll('.link');
Simon Hunte2575b62014-11-18 15:25:53 -08002137 linkLabel = linkLabelG.selectAll('.linkLabel');
Simon Huntc7ee0662014-11-05 16:44:37 -08002138 node = nodeG.selectAll('.node');
2139
Simon Hunt7cd48f32014-11-09 23:42:50 -08002140 function chrg(d) {
2141 return fcfg.charge[d.class] || -12000;
2142 }
Simon Hunt99c13842014-11-06 18:23:12 -08002143 function ldist(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002144 return fcfg.linkDistance[d.type] || 50;
Simon Hunt99c13842014-11-06 18:23:12 -08002145 }
2146 function lstrg(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002147 // 0.0 - 1.0
2148 return fcfg.linkStrength[d.type] || 1.0;
Simon Hunt99c13842014-11-06 18:23:12 -08002149 }
2150
Simon Hunt1a9eff92014-11-07 11:06:34 -08002151 function selectCb(d, self) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002152 selectObject(d, self);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002153 }
2154
2155 function atDragEnd(d, self) {
Simon Hunt56d51852014-11-09 13:03:35 -08002156 // once we've finished moving, pin the node in position
2157 d.fixed = true;
2158 d3.select(self).classed('fixed', true);
2159 if (config.useLiveData) {
Simon Hunt902c9922014-11-11 11:59:31 -08002160 sendUpdateMeta(d);
Simon Hunta255a2c2014-11-13 22:29:35 -08002161 } else {
2162 console.log('Moving node ' + d.id + ' to [' + d.x + ',' + d.y + ']');
Simon Hunt1a9eff92014-11-07 11:06:34 -08002163 }
2164 }
2165
Simon Hunt902c9922014-11-11 11:59:31 -08002166 function sendUpdateMeta(d) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002167 sendMessage('updateMeta', {
2168 id: d.id,
2169 'class': d.class,
Simon Hunt902c9922014-11-11 11:59:31 -08002170 'memento': {
Simon Hunt01095ff2014-11-13 16:37:29 -08002171 x: d.x,
2172 y: d.y
Simon Hunt902c9922014-11-11 11:59:31 -08002173 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002174 });
2175 }
2176
Simon Huntc7ee0662014-11-05 16:44:37 -08002177 // set up the force layout
2178 network.force = d3.layout.force()
2179 .size(forceDim)
2180 .nodes(network.nodes)
2181 .links(network.links)
Simon Hunt7cd48f32014-11-09 23:42:50 -08002182 .gravity(0.4)
2183 .friction(0.7)
2184 .charge(chrg)
Simon Hunt99c13842014-11-06 18:23:12 -08002185 .linkDistance(ldist)
2186 .linkStrength(lstrg)
Simon Huntc7ee0662014-11-05 16:44:37 -08002187 .on('tick', tick);
Simon Hunt195cb382014-11-03 17:50:51 -08002188
Simon Hunt01095ff2014-11-13 16:37:29 -08002189 network.drag = d3u.createDragBehavior(network.force,
Simon Huntdeab4322014-11-13 18:49:07 -08002190 selectCb, atDragEnd, panZoom);
Simon Hunt0c6d4192014-11-12 12:07:10 -08002191
2192 // create mask layer for when we lose connection to server.
Simon Hunta5e89142014-11-14 07:00:33 -08002193 // TODO: this should be part of the framework
Simon Hunt0c6d4192014-11-12 12:07:10 -08002194 mask = view.$div.append('div').attr('id','topo-mask');
2195 para(mask, 'Oops!');
2196 para(mask, 'Web-socket connection to server closed...');
2197 para(mask, 'Try refreshing the page.');
Simon Hunt12ce12e2014-11-15 21:13:19 -08002198
2199 mask.append('svg')
2200 .attr({
2201 id: 'mask-bird',
2202 width: w,
2203 height: h
2204 })
2205 .append('g')
2206 .attr('transform', birdTranslate(w, h))
2207 .style('opacity', 0.3)
2208 .append('use')
2209 .attr({
2210 'xlink:href': '#bird',
2211 width: config.birdDim,
2212 height: config.birdDim,
2213 fill: '#111'
Thomas Vachuska89543292014-11-19 11:28:33 -08002214 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08002215 }
Simon Hunt195cb382014-11-03 17:50:51 -08002216
Simon Hunt01095ff2014-11-13 16:37:29 -08002217 function para(sel, text) {
2218 sel.append('p').text(text);
2219 }
2220
2221
Simon Hunt56d51852014-11-09 13:03:35 -08002222 function load(view, ctx, flags) {
Simon Huntf67722a2014-11-10 09:32:06 -08002223 // resize, in case the window was resized while we were not loaded
2224 resize(view, ctx, flags);
2225
Simon Hunt99c13842014-11-06 18:23:12 -08002226 // cache the view token, so network topo functions can access it
2227 network.view = view;
Simon Hunt56d51852014-11-09 13:03:35 -08002228 config.useLiveData = !flags.local;
2229
2230 if (!config.useLiveData) {
2231 prepareScenario(view, ctx, flags.debug);
2232 }
Simon Hunt99c13842014-11-06 18:23:12 -08002233
2234 // set our radio buttons and key bindings
Simon Hunt9462e8c2014-11-14 17:28:09 -08002235 layerBtnSet = view.setRadio(layerButtons);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002236 view.setKeys(keyDispatch);
Simon Hunt195cb382014-11-03 17:50:51 -08002237
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002238 // patch in our "button bar" for now
2239 // TODO: implement a more official frameworky way of doing this..
2240 addButtonBar(view);
2241
Simon Huntd3b7d512014-11-12 15:48:41 -08002242 // Load map data asynchronously; complete startup after that..
2243 loadGeoJsonData();
Simon Hunta255a2c2014-11-13 22:29:35 -08002244 }
2245
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002246 function startAntTimer() {
Thomas Vachuska4731f122014-11-20 04:56:19 -08002247 if (!antTimer) {
2248 var pulses = [5, 3, 1.2, 3],
2249 pulse = 0;
2250 antTimer = setInterval(function () {
2251 pulse = pulse + 1;
2252 pulse = pulse === pulses.length ? 0 : pulse;
2253 d3.selectAll('.animated').style('stroke-width', pulses[pulse]);
2254 }, 200);
2255 }
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002256 }
2257
2258 function stopAntTimer() {
Simon Hunta255a2c2014-11-13 22:29:35 -08002259 if (antTimer) {
2260 clearInterval(antTimer);
2261 antTimer = null;
2262 }
Simon Huntd3b7d512014-11-12 15:48:41 -08002263 }
2264
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002265 function unload(view, ctx, flags) {
2266 stopAntTimer();
2267 }
2268
Simon Huntd3b7d512014-11-12 15:48:41 -08002269 // TODO: move these to config/state portion of script
Simon Hunta6a9fe72014-11-20 11:17:12 -08002270 var geoJsonUrl = 'json/map/continental_us.json',
Simon Huntd3b7d512014-11-12 15:48:41 -08002271 geoJson;
2272
2273 function loadGeoJsonData() {
2274 d3.json(geoJsonUrl, function (err, data) {
2275 if (err) {
2276 // fall back to USA map background
2277 loadStaticMap();
2278 } else {
2279 geoJson = data;
2280 loadGeoMap();
2281 }
2282
2283 // finally, connect to the server...
2284 if (config.useLiveData) {
2285 webSock.connect();
2286 }
2287 });
2288 }
2289
2290 function showBg() {
2291 return config.options.showBackground ? 'visible' : 'hidden';
2292 }
2293
2294 function loadStaticMap() {
2295 fnTrace('loadStaticMap', config.backgroundUrl);
2296 var w = network.view.width(),
2297 h = network.view.height();
2298
2299 // load the background image
2300 bgImg = svg.insert('svg:image', '#topo-G')
2301 .attr({
2302 id: 'topo-bg',
2303 width: w,
2304 height: h,
2305 'xlink:href': config.backgroundUrl
2306 })
2307 .style({
2308 visibility: showBg()
2309 });
2310 }
2311
2312 function loadGeoMap() {
2313 fnTrace('loadGeoMap', geoJsonUrl);
Simon Huntd3b7d512014-11-12 15:48:41 -08002314
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002315 // extracts the topojson data into geocoordinate-based geometry
2316 var topoData = topojson.feature(geoJson, geoJson.objects.states);
Simon Huntd3b7d512014-11-12 15:48:41 -08002317
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002318 // see: http://bl.ocks.org/mbostock/4707858
2319 geoMapProjection = d3.geo.mercator();
2320 var path = d3.geo.path().projection(geoMapProjection);
Simon Huntd3b7d512014-11-12 15:48:41 -08002321
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002322 geoMapProjection
2323 .scale(1)
2324 .translate([0, 0]);
Simon Huntd3b7d512014-11-12 15:48:41 -08002325
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002326 // [[x1,y1],[x2,y2]]
2327 var b = path.bounds(topoData);
Paul Greysonfcba0e82014-11-13 10:21:16 -08002328 // size map to 95% of minimum dimension to fill space
2329 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 -08002330 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 -08002331
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002332 geoMapProjection
2333 .scale(s)
2334 .translate(t);
2335
Paul Greysonfcba0e82014-11-13 10:21:16 -08002336 bgImg = zoomPanContainer.insert("g", '#topo-G');
Thomas Vachuska89543292014-11-19 11:28:33 -08002337 bgImg.attr('id', 'map').selectAll('path')
2338 .data(topoData.features)
2339 .enter()
2340 .append('path')
2341 .attr('d', path);
Simon Hunt195cb382014-11-03 17:50:51 -08002342 }
2343
Simon Huntf67722a2014-11-10 09:32:06 -08002344 function resize(view, ctx, flags) {
Simon Hunt12ce12e2014-11-15 21:13:19 -08002345 var w = view.width(),
2346 h = view.height();
2347
Simon Hunt934c3ce2014-11-05 11:45:07 -08002348 setSize(svg, view);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002349
2350 d3.select('#mask-bird').attr({ width: w, height: h})
2351 .select('g').attr('transform', birdTranslate(w, h));
Simon Hunt142d0032014-11-04 20:13:09 -08002352 }
2353
Simon Hunt12ce12e2014-11-15 21:13:19 -08002354 function birdTranslate(w, h) {
2355 var bdim = config.birdDim;
2356 return 'translate('+((w-bdim)*.4)+','+((h-bdim)*.1)+')';
2357 }
Simon Hunt142d0032014-11-04 20:13:09 -08002358
2359 // ==============================
2360 // View registration
Simon Hunt195cb382014-11-03 17:50:51 -08002361
Simon Hunt25248912014-11-04 11:25:48 -08002362 onos.ui.addView('topo', {
Simon Hunt142d0032014-11-04 20:13:09 -08002363 preload: preload,
2364 load: load,
Simon Hunta255a2c2014-11-13 22:29:35 -08002365 unload: unload,
Simon Hunt142d0032014-11-04 20:13:09 -08002366 resize: resize
Simon Hunt195cb382014-11-03 17:50:51 -08002367 });
2368
Simon Hunt61d04042014-11-11 17:27:16 -08002369 detailPane = onos.ui.addFloatingPanel('topo-detail');
Simon Hunta5e89142014-11-14 07:00:33 -08002370 oiBox = onos.ui.addFloatingPanel('topo-oibox', 'TL');
Simon Hunt61d04042014-11-11 17:27:16 -08002371
Simon Hunt195cb382014-11-03 17:50:51 -08002372}(ONOS));