blob: b8381120c42c3f445b8a8dbc0778ee69d059e7da [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,
Simon Hunt209155e2014-11-21 12:16:09 -0800178 deviceLabelCount = 2,
179 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() {
Simon Huntbb282f52014-11-10 11:08:19 -0800317 deviceLabelIndex = (deviceLabelIndex === network.deviceLabelCount - 1)
318 ? 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
1240 labels.unshift(''); // add 'no-label' to front of cycle
Simon Hunt99c13842014-11-06 18:23:12 -08001241
1242 // Augment as needed...
1243 node.class = 'device';
Simon Huntbb282f52014-11-10 11:08:19 -08001244 node.svgClass = device.online ? svgCls + ' online' : svgCls;
Simon Hunt99c13842014-11-06 18:23:12 -08001245 positionNode(node);
Simon Hunt99c13842014-11-06 18:23:12 -08001246 return node;
1247 }
1248
Simon Hunt56d51852014-11-09 13:03:35 -08001249 function createHostNode(host) {
1250 // start with the object as is
1251 var node = host;
1252
1253 // Augment as needed...
1254 node.class = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001255 if (!node.type) {
Simon Hunt209155e2014-11-21 12:16:09 -08001256 node.type = 'endstation';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001257 }
Simon Hunt7fa116d2014-11-17 14:16:55 -08001258 node.svgClass = 'node host ' + node.type;
Simon Hunt56d51852014-11-09 13:03:35 -08001259 positionNode(node);
Simon Hunt56d51852014-11-09 13:03:35 -08001260 return node;
1261 }
1262
Simon Hunt99c13842014-11-06 18:23:12 -08001263 function positionNode(node) {
1264 var meta = node.metaUi,
Simon Huntac9e24f2014-11-12 10:12:21 -08001265 x = meta && meta.x,
1266 y = meta && meta.y,
1267 xy;
Simon Hunt99c13842014-11-06 18:23:12 -08001268
Simon Huntac9e24f2014-11-12 10:12:21 -08001269 // If we have [x,y] already, use that...
Simon Hunt99c13842014-11-06 18:23:12 -08001270 if (x && y) {
1271 node.fixed = true;
Simon Huntac9e24f2014-11-12 10:12:21 -08001272 node.x = x;
1273 node.y = y;
1274 return;
Simon Hunt99c13842014-11-06 18:23:12 -08001275 }
Simon Huntac9e24f2014-11-12 10:12:21 -08001276
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001277 var location = node.location;
1278 if (location && location.type === 'latlng') {
1279 var coord = geoMapProjection([location.lng, location.lat]);
1280 node.fixed = true;
1281 node.x = coord[0];
1282 node.y = coord[1];
1283 return;
1284 }
1285
Simon Huntac9e24f2014-11-12 10:12:21 -08001286 // Note: Placing incoming unpinned nodes at exactly the same point
1287 // (center of the view) causes them to explode outwards when
1288 // the force layout kicks in. So, we spread them out a bit
1289 // initially, to provide a more serene layout convergence.
1290 // Additionally, if the node is a host, we place it near
1291 // the device it is connected to.
1292
1293 function spread(s) {
1294 return Math.floor((Math.random() * s) - s/2);
1295 }
1296
1297 function randDim(dim) {
1298 return dim / 2 + spread(dim * 0.7071);
1299 }
1300
1301 function rand() {
1302 return {
1303 x: randDim(network.view.width()),
1304 y: randDim(network.view.height())
1305 };
1306 }
1307
1308 function near(node) {
1309 var min = 12,
1310 dx = spread(12),
1311 dy = spread(12);
1312 return {
1313 x: node.x + min + dx,
1314 y: node.y + min + dy
1315 };
1316 }
1317
1318 function getDevice(cp) {
1319 var d = network.lookup[cp.device];
1320 return d || rand();
1321 }
1322
1323 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
1324 $.extend(node, xy);
Simon Hunt99c13842014-11-06 18:23:12 -08001325 }
1326
Thomas Vachuska89543292014-11-19 11:28:33 -08001327 function iconUrl(d) {
1328 return 'img/' + d.type + '.png';
Simon Hunt99c13842014-11-06 18:23:12 -08001329 }
1330
Simon Huntc72967b2014-11-20 09:21:42 -08001331 function iconGlyphUrl(d) {
1332 var which = d.type || 'unknown';
1333 return '#' + which;
1334 }
1335
Simon Hunt99c13842014-11-06 18:23:12 -08001336 // returns the newly computed bounding box of the rectangle
1337 function adjustRectToFitText(n) {
1338 var text = n.select('text'),
1339 box = text.node().getBBox(),
1340 lab = config.labels;
1341
1342 text.attr('text-anchor', 'middle')
1343 .attr('y', '-0.8em')
1344 .attr('x', lab.imgPad/2);
1345
1346 // translate the bbox so that it is centered on [x,y]
1347 box.x = -box.width / 2;
1348 box.y = -box.height / 2;
1349
1350 // add padding
1351 box.x -= (lab.padLR + lab.imgPad/2);
1352 box.width += lab.padLR * 2 + lab.imgPad;
1353 box.y -= lab.padTB;
1354 box.height += lab.padTB * 2;
1355
1356 return box;
1357 }
1358
Simon Hunt1a9eff92014-11-07 11:06:34 -08001359 function mkSvgClass(d) {
1360 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
1361 }
1362
Simon Hunt7cd48f32014-11-09 23:42:50 -08001363 function hostLabel(d) {
1364 var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0;
1365 return d.labels[idx];
1366 }
1367 function deviceLabel(d) {
1368 var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0;
1369 return d.labels[idx];
1370 }
Simon Huntc72967b2014-11-20 09:21:42 -08001371 function trimLabel(label) {
1372 return (label && label.trim()) || '';
1373 }
1374
1375 function emptyBox() {
1376 return {
1377 x: -2,
1378 y: -2,
1379 width: 4,
1380 height: 4
1381 };
Simon Hunt7cd48f32014-11-09 23:42:50 -08001382 }
1383
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001384 function updateDeviceLabel(d) {
Simon Huntc72967b2014-11-20 09:21:42 -08001385 var label = trimLabel(deviceLabel(d)),
1386 noLabel = !label,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001387 node = d.el,
Simon Huntc72967b2014-11-20 09:21:42 -08001388 box,
1389 dx,
1390 dy;
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001391
1392 node.select('text')
1393 .text(label)
1394 .style('opacity', 0)
1395 .transition()
1396 .style('opacity', 1);
1397
Simon Huntc72967b2014-11-20 09:21:42 -08001398 if (noLabel) {
1399 box = emptyBox();
1400 dx = -config.icons.device.dim/2;
1401 dy = -config.icons.device.dim/2;
1402 } else {
1403 box = adjustRectToFitText(node);
1404 dx = box.x + config.icons.xoff;
1405 dy = box.y + config.icons.yoff;
1406 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001407
1408 node.select('rect')
1409 .transition()
1410 .attr(box);
1411
Simon Huntc72967b2014-11-20 09:21:42 -08001412 node.select('g.deviceIcon')
Thomas Vachuska89543292014-11-19 11:28:33 -08001413 .transition()
Simon Huntc72967b2014-11-20 09:21:42 -08001414 .attr('transform', translate(dx, dy));
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001415 }
1416
1417 function updateHostLabel(d) {
1418 var label = hostLabel(d),
1419 host = d.el;
1420
1421 host.select('text').text(label);
1422 }
1423
Simon Hunta6a9fe72014-11-20 11:17:12 -08001424 // TODO: should be using updateNodes() to do the upates!
Simon Huntbb282f52014-11-10 11:08:19 -08001425 function updateDeviceState(nodeData) {
1426 nodeData.el.classed('online', nodeData.online);
1427 updateDeviceLabel(nodeData);
1428 // TODO: review what else might need to be updated
1429 }
1430
1431 function updateHostState(hostData) {
1432 updateHostLabel(hostData);
1433 // TODO: review what else might need to be updated
1434 }
1435
Simon Hunt6ac93f32014-11-13 12:17:27 -08001436 function nodeMouseOver(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001437 hovered = d;
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -08001438 if (trafficHover() && (d.class === 'host' || d.class === 'device')) {
Thomas Vachuska29617e52014-11-20 03:17:46 -08001439 showSelectTraffic();
1440 } else if (flowsHover() && (d.class === 'device')) {
1441 showDeviceLinkFlows();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001442 }
1443 }
1444
1445 function nodeMouseOut(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001446 hovered = null;
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -08001447 if (trafficHover() && (d.class === 'host' || d.class === 'device')) {
Thomas Vachuska29617e52014-11-20 03:17:46 -08001448 showSelectTraffic();
1449 } else if (flowsHover() && (d.class === 'device')) {
1450 showDeviceLinkFlows();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001451 }
1452 }
Simon Huntbb282f52014-11-10 11:08:19 -08001453
Simon Hunteb1514d2014-11-20 09:57:29 -08001454 function addHostIcon(node, radius, iid) {
Thomas Vachuska89543292014-11-19 11:28:33 -08001455 var dim = radius * 1.5,
1456 xlate = -dim / 2;
1457
Simon Hunteb1514d2014-11-20 09:57:29 -08001458 node.append('use').attr({
1459 class: 'glyphIcon hostIcon',
1460 transform: translate(xlate,xlate),
1461 'xlink:href': iid,
1462 width: dim,
1463 height: dim
1464 });
Thomas Vachuska89543292014-11-19 11:28:33 -08001465 }
1466
Simon Hunt99c13842014-11-06 18:23:12 -08001467 function updateNodes() {
1468 node = nodeG.selectAll('.node')
1469 .data(network.nodes, function (d) { return d.id; });
1470
Simon Huntc72967b2014-11-20 09:21:42 -08001471 // TODO: operate on existing nodes
Simon Hunt7cd48f32014-11-09 23:42:50 -08001472 // update host labels
Simon Hunt99c13842014-11-06 18:23:12 -08001473 //node .foo() .bar() ...
1474
1475 // operate on entering nodes:
1476 var entering = node.enter()
1477 .append('g')
1478 .attr({
1479 id: function (d) { return safeId(d.id); },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001480 class: mkSvgClass,
Simon Hunt99c13842014-11-06 18:23:12 -08001481 transform: function (d) { return translate(d.x, d.y); },
1482 opacity: 0
1483 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08001484 .call(network.drag)
Simon Hunt6ac93f32014-11-13 12:17:27 -08001485 .on('mouseover', nodeMouseOver)
1486 .on('mouseout', nodeMouseOut)
Simon Hunt99c13842014-11-06 18:23:12 -08001487 .transition()
1488 .attr('opacity', 1);
1489
1490 // augment device nodes...
1491 entering.filter('.device').each(function (d) {
1492 var node = d3.select(this),
Simon Huntc72967b2014-11-20 09:21:42 -08001493 label = trimLabel(deviceLabel(d)),
1494 noLabel = !label,
Simon Hunt99c13842014-11-06 18:23:12 -08001495 box;
1496
Simon Hunt7cd48f32014-11-09 23:42:50 -08001497 // provide ref to element from backing data....
1498 d.el = node;
1499
Simon Hunt99c13842014-11-06 18:23:12 -08001500 node.append('rect')
1501 .attr({
Simon Hunta3dd9572014-11-20 15:22:41 -08001502 rx: 5,
1503 ry: 5
Simon Hunt99c13842014-11-06 18:23:12 -08001504 });
1505
1506 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001507 .text(label)
Simon Hunt99c13842014-11-06 18:23:12 -08001508 .attr('dy', '1.1em');
1509
1510 box = adjustRectToFitText(node);
Simon Hunta3dd9572014-11-20 15:22:41 -08001511 node.select('rect').attr(box);
Simon Huntc72967b2014-11-20 09:21:42 -08001512 addDeviceIcon(node, box, noLabel, iconGlyphUrl(d));
Simon Huntc7ee0662014-11-05 16:44:37 -08001513 });
Simon Hunt934c3ce2014-11-05 11:45:07 -08001514
Thomas Vachuska89543292014-11-19 11:28:33 -08001515 // TODO: better place for this configuration state
1516 var defaultHostRadius = 9,
1517 hostRadius = {
1518 bgpSpeaker: 14,
1519 router: 14,
1520 host: 14
1521 },
Simon Hunteb1514d2014-11-20 09:57:29 -08001522 hostGlyphId = {
Thomas Vachuska89543292014-11-19 11:28:33 -08001523 bgpSpeaker: 'bgpSpeaker',
1524 router: 'router',
1525 host: 'host'
1526 };
1527
1528
Simon Hunt56d51852014-11-09 13:03:35 -08001529 // augment host nodes...
1530 entering.filter('.host').each(function (d) {
1531 var node = d3.select(this),
Thomas Vachuska89543292014-11-19 11:28:33 -08001532 r = hostRadius[d.type] || defaultHostRadius,
1533 textDy = r + 10,
Simon Hunteb1514d2014-11-20 09:57:29 -08001534 iid = iconGlyphUrl(d);
Simon Hunt56d51852014-11-09 13:03:35 -08001535
Simon Hunt7cd48f32014-11-09 23:42:50 -08001536 // provide ref to element from backing data....
1537 d.el = node;
1538
Thomas Vachuska89543292014-11-19 11:28:33 -08001539 node.append('circle')
1540 .attr('r', r);
Simon Hunt7fa116d2014-11-17 14:16:55 -08001541
Simon Hunteb1514d2014-11-20 09:57:29 -08001542 if (iid) {
1543 addHostIcon(node, r, iid);
Simon Hunt7fa116d2014-11-17 14:16:55 -08001544 }
Simon Hunt56d51852014-11-09 13:03:35 -08001545
Simon Hunt56d51852014-11-09 13:03:35 -08001546 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001547 .text(hostLabel)
Thomas Vachuska89543292014-11-19 11:28:33 -08001548 .attr('dy', textDy)
Simon Hunt7cd48f32014-11-09 23:42:50 -08001549 .attr('text-anchor', 'middle');
Simon Hunt56d51852014-11-09 13:03:35 -08001550
1551 // debug function to show the modelled x,y coordinates of nodes...
1552 if (debug('showNodeXY')) {
1553 node.select('circle').attr('fill-opacity', 0.5);
1554 node.append('circle')
1555 .attr({
1556 class: 'debug',
1557 cx: 0,
1558 cy: 0,
1559 r: '3px'
1560 });
1561 }
1562 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001563
Simon Hunt99c13842014-11-06 18:23:12 -08001564 // operate on both existing and new nodes, if necessary
1565 //node .foo() .bar() ...
Simon Huntc7ee0662014-11-05 16:44:37 -08001566
Simon Hunt99c13842014-11-06 18:23:12 -08001567 // operate on exiting nodes:
Simon Huntea80eb42014-11-11 13:46:57 -08001568 // Note that the node is removed after 2 seconds.
1569 // Sub element animations should be shorter than 2 seconds.
1570 var exiting = node.exit()
Simon Hunt44031102014-11-11 13:20:36 -08001571 .transition()
1572 .duration(2000)
Simon Huntea80eb42014-11-11 13:46:57 -08001573 .style('opacity', 0)
Simon Hunt99c13842014-11-06 18:23:12 -08001574 .remove();
Simon Huntea80eb42014-11-11 13:46:57 -08001575
1576 // host node exits....
1577 exiting.filter('.host').each(function (d) {
1578 var node = d3.select(this);
1579
1580 node.select('text')
1581 .style('opacity', 0.5)
1582 .transition()
1583 .duration(1000)
1584 .style('opacity', 0);
1585 // note, leave <g>.remove to remove this element
1586
Thomas Vachuska89543292014-11-19 11:28:33 -08001587 node.select('circle')
1588 .style('stroke-fill', '#555')
1589 .style('fill', '#888')
Simon Huntea80eb42014-11-11 13:46:57 -08001590 .style('opacity', 0.5)
1591 .transition()
1592 .duration(1500)
1593 .attr('r', 0);
1594 // note, leave <g>.remove to remove this element
1595
1596 });
1597
1598 // TODO: device node exits
Simon Huntc7ee0662014-11-05 16:44:37 -08001599 }
1600
Simon Huntc72967b2014-11-20 09:21:42 -08001601 function addDeviceIcon(node, box, noLabel, iid) {
1602 var cfg = config.icons.device,
1603 dx,
1604 dy,
1605 g;
1606
1607 if (noLabel) {
1608 box = emptyBox();
1609 dx = -cfg.dim/2;
1610 dy = -cfg.dim/2;
1611 } else {
1612 box = adjustRectToFitText(node);
1613 dx = box.x + config.icons.xoff;
1614 dy = box.y + config.icons.yoff;
1615 }
1616
Simon Hunteb1514d2014-11-20 09:57:29 -08001617 g = node.append('g')
1618 .attr('class', 'glyphIcon deviceIcon')
Simon Huntc72967b2014-11-20 09:21:42 -08001619 .attr('transform', translate(dx, dy));
1620
1621 g.append('rect').attr({
1622 x: 0,
1623 y: 0,
1624 rx: cfg.rx,
1625 width: cfg.dim,
1626 height: cfg.dim
1627 });
1628
1629 g.append('use').attr({
1630 'xlink:href': iid,
1631 width: cfg.dim,
1632 height: cfg.dim
1633 });
1634
Simon Huntc72967b2014-11-20 09:21:42 -08001635 }
1636
Simon Hunt8257f4c2014-11-16 19:34:54 -08001637 function find(key, array) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001638 for (var idx = 0, n = array.length; idx < n; idx++) {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001639 if (array[idx].key === key) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001640 return idx;
1641 }
1642 }
1643 return -1;
1644 }
1645
1646 function removeLinkElement(linkData) {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001647 var idx = find(linkData.key, network.links),
1648 removed;
1649 if (idx >=0) {
1650 // remove from links array
1651 removed = network.links.splice(idx, 1);
1652 // remove from lookup cache
1653 delete network.lookup[removed[0].key];
1654 updateLinks();
1655 network.force.resume();
1656 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001657 }
Simon Huntc7ee0662014-11-05 16:44:37 -08001658
Simon Hunt44031102014-11-11 13:20:36 -08001659 function removeHostElement(hostData) {
1660 // first, remove associated hostLink...
1661 removeLinkElement(hostData.linkData);
1662
1663 // remove from lookup cache
1664 delete network.lookup[hostData.id];
1665 // remove from nodes array
1666 var idx = find(hostData.id, network.nodes);
1667 network.nodes.splice(idx, 1);
1668 // remove from SVG
1669 updateNodes();
1670 network.force.resume();
1671 }
1672
1673
Simon Huntc7ee0662014-11-05 16:44:37 -08001674 function tick() {
1675 node.attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001676 transform: function (d) { return translate(d.x, d.y); }
Simon Huntc7ee0662014-11-05 16:44:37 -08001677 });
1678
1679 link.attr({
1680 x1: function (d) { return d.source.x; },
1681 y1: function (d) { return d.source.y; },
1682 x2: function (d) { return d.target.x; },
1683 y2: function (d) { return d.target.y; }
1684 });
Simon Hunte2575b62014-11-18 15:25:53 -08001685
1686 linkLabel.each(function (d) {
1687 var el = d3.select(this);
Thomas Vachuska4731f122014-11-20 04:56:19 -08001688 var lnk = findLinkById(d.key);
1689
1690 if (lnk) {
1691 var parms = {
Simon Hunte2575b62014-11-18 15:25:53 -08001692 x1: lnk.source.x,
1693 y1: lnk.source.y,
1694 x2: lnk.target.x,
1695 y2: lnk.target.y
1696 };
Thomas Vachuska4731f122014-11-20 04:56:19 -08001697 el.attr('transform', transformLabel(parms));
1698 }
Simon Hunte2575b62014-11-18 15:25:53 -08001699 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001700 }
Simon Hunt934c3ce2014-11-05 11:45:07 -08001701
1702 // ==============================
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001703 // Web-Socket for live data
1704
1705 function webSockUrl() {
1706 return document.location.toString()
1707 .replace(/\#.*/, '')
1708 .replace('http://', 'ws://')
1709 .replace('https://', 'wss://')
1710 .replace('index2.html', config.webSockUrl);
1711 }
1712
1713 webSock = {
1714 ws : null,
1715
1716 connect : function() {
1717 webSock.ws = new WebSocket(webSockUrl());
1718
1719 webSock.ws.onopen = function() {
Simon Hunt0c6d4192014-11-12 12:07:10 -08001720 noWebSock(false);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001721 };
1722
1723 webSock.ws.onmessage = function(m) {
1724 if (m.data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001725 wsTraceRx(m.data);
Thomas Vachuskad472c6e2014-11-07 19:11:05 -08001726 handleServerEvent(JSON.parse(m.data));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001727 }
1728 };
1729
1730 webSock.ws.onclose = function(m) {
1731 webSock.ws = null;
Simon Hunt0c6d4192014-11-12 12:07:10 -08001732 noWebSock(true);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001733 };
1734 },
1735
1736 send : function(text) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001737 if (text != null) {
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001738 webSock._send(text);
1739 }
1740 },
1741
1742 _send : function(message) {
1743 if (webSock.ws) {
1744 webSock.ws.send(message);
Simon Hunta255a2c2014-11-13 22:29:35 -08001745 } else if (config.useLiveData) {
Simon Hunt56d51852014-11-09 13:03:35 -08001746 network.view.alert('no web socket open\n\n' + message);
Simon Hunta255a2c2014-11-13 22:29:35 -08001747 } else {
1748 console.log('WS Send: ' + JSON.stringify(message));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001749 }
1750 }
1751
1752 };
1753
Simon Hunt0c6d4192014-11-12 12:07:10 -08001754 function noWebSock(b) {
1755 mask.style('display',b ? 'block' : 'none');
1756 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001757
1758 function sendMessage(evType, payload) {
1759 var toSend = {
Simon Huntbb282f52014-11-10 11:08:19 -08001760 event: evType,
1761 sid: ++sid,
1762 payload: payload
1763 },
1764 asText = JSON.stringify(toSend);
1765 wsTraceTx(asText);
1766 webSock.send(asText);
Simon Huntc76ae892014-11-18 17:31:51 -08001767
1768 // Temporary measure for debugging UI behavior ...
1769 if (!config.useLiveData) {
1770 handleTestSend(toSend);
1771 }
Simon Huntbb282f52014-11-10 11:08:19 -08001772 }
1773
1774 function wsTraceTx(msg) {
1775 wsTrace('tx', msg);
1776 }
1777 function wsTraceRx(msg) {
1778 wsTrace('rx', msg);
1779 }
1780 function wsTrace(rxtx, msg) {
Simon Huntbb282f52014-11-10 11:08:19 -08001781 console.log('[' + rxtx + '] ' + msg);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001782 }
1783
Simon Huntc76ae892014-11-18 17:31:51 -08001784 // NOTE: Temporary hardcoded example for showing detail pane
1785 // while we fine-
1786 // Probably should not merge this change...
1787 function handleTestSend(msg) {
1788 if (msg.event === 'requestDetails') {
1789 showDetails({
1790 event: 'showDetails',
1791 sid: 1001,
1792 payload: {
1793 "id": "of:0000ffffffffff09",
1794 "type": "roadm",
1795 "propOrder": [
1796 "Name",
1797 "Vendor",
1798 "H/W Version",
1799 "S/W Version",
1800 "-",
1801 "Latitude",
1802 "Longitude",
1803 "Ports"
1804 ],
1805 "props": {
1806 "Name": null,
1807 "Vendor": "Linc",
1808 "H/W Version": "OE",
1809 "S/W Version": "?",
1810 "-": "",
1811 "Latitude": "40.8",
1812 "Longitude": "73.1",
1813 "Ports": "2"
1814 }
1815 }
1816 });
1817 }
1818 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001819
1820 // ==============================
1821 // Selection stuff
1822
1823 function selectObject(obj, el) {
1824 var n,
Simon Hunt01095ff2014-11-13 16:37:29 -08001825 srcEv = d3.event.sourceEvent,
1826 meta = srcEv.metaKey,
1827 shift = srcEv.shiftKey;
1828
Simon Huntdeab4322014-11-13 18:49:07 -08001829 if ((panZoom() && !meta) || (!panZoom() && meta)) {
Simon Hunt01095ff2014-11-13 16:37:29 -08001830 return;
1831 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001832
1833 if (el) {
1834 n = d3.select(el);
1835 } else {
1836 node.each(function(d) {
1837 if (d == obj) {
1838 n = d3.select(el = this);
1839 }
1840 });
1841 }
1842 if (!n) return;
1843
Simon Hunt01095ff2014-11-13 16:37:29 -08001844 if (shift && n.classed('selected')) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001845 deselectObject(obj.id);
Simon Hunt61d04042014-11-11 17:27:16 -08001846 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001847 return;
1848 }
1849
Simon Hunt01095ff2014-11-13 16:37:29 -08001850 if (!shift) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001851 deselectAll();
1852 }
1853
Simon Huntc31d5692014-11-12 13:27:18 -08001854 selections[obj.id] = { obj: obj, el: el };
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001855 selectOrder.push(obj.id);
1856
1857 n.classed('selected', true);
Simon Hunt61d04042014-11-11 17:27:16 -08001858 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001859 }
1860
1861 function deselectObject(id) {
Simon Huntc31d5692014-11-12 13:27:18 -08001862 var obj = selections[id],
1863 idx;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001864 if (obj) {
1865 d3.select(obj.el).classed('selected', false);
Simon Hunt61d04042014-11-11 17:27:16 -08001866 delete selections[id];
Simon Huntc31d5692014-11-12 13:27:18 -08001867 idx = $.inArray(id, selectOrder);
1868 if (idx >= 0) {
1869 selectOrder.splice(idx, 1);
1870 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001871 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001872 }
1873
1874 function deselectAll() {
1875 // deselect all nodes in the network...
1876 node.classed('selected', false);
1877 selections = {};
1878 selectOrder = [];
Simon Hunt61d04042014-11-11 17:27:16 -08001879 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001880 }
1881
Simon Hunt61d04042014-11-11 17:27:16 -08001882 // update the state of the detail pane, based on current selections
1883 function updateDetailPane() {
1884 var nSel = selectOrder.length;
1885 if (!nSel) {
1886 detailPane.hide();
Simon Huntd72bc702014-11-13 18:38:04 -08001887 showTrafficAction(); // sends cancelTraffic event
Simon Hunt61d04042014-11-11 17:27:16 -08001888 } else if (nSel === 1) {
1889 singleSelect();
1890 } else {
1891 multiSelect();
1892 }
1893 }
1894
1895 function singleSelect() {
1896 requestDetails();
Simon Huntb53e0682014-11-12 13:32:01 -08001897 // NOTE: detail pane will be shown from showDetails event callback
Simon Hunt61d04042014-11-11 17:27:16 -08001898 }
1899
1900 function multiSelect() {
Simon Huntb53e0682014-11-12 13:32:01 -08001901 populateMultiSelect();
Simon Huntb53e0682014-11-12 13:32:01 -08001902 }
1903
1904 function addSep(tbody) {
1905 var tr = tbody.append('tr');
1906 $('<hr>').appendTo(tr.append('td').attr('colspan', 2));
1907 }
1908
1909 function addProp(tbody, label, value) {
1910 var tr = tbody.append('tr');
1911
1912 tr.append('td')
1913 .attr('class', 'label')
1914 .text(label + ' :');
1915
1916 tr.append('td')
1917 .attr('class', 'value')
1918 .text(value);
1919 }
1920
1921 function populateMultiSelect() {
1922 detailPane.empty();
1923
Simon Hunta3dd9572014-11-20 15:22:41 -08001924 var title = detailPane.append('h3'),
1925 table = detailPane.append('table'),
1926 tbody = table.append('tbody');
Simon Huntb53e0682014-11-12 13:32:01 -08001927
Thomas Vachuska4731f122014-11-20 04:56:19 -08001928 title.text('Selected Nodes');
Simon Huntb53e0682014-11-12 13:32:01 -08001929
1930 selectOrder.forEach(function (d, i) {
1931 addProp(tbody, i+1, d);
1932 });
Simon Huntd72bc702014-11-13 18:38:04 -08001933
1934 addMultiSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08001935 }
1936
1937 function populateDetails(data) {
1938 detailPane.empty();
1939
Simon Hunta6a9fe72014-11-20 11:17:12 -08001940 var svg = detailPane.append('svg'),
1941 iid = iconGlyphUrl(data);
1942
Simon Hunta3dd9572014-11-20 15:22:41 -08001943 var title = detailPane.append('h2'),
1944 table = detailPane.append('table'),
1945 tbody = table.append('tbody');
Simon Hunt61d04042014-11-11 17:27:16 -08001946
Simon Hunta6a9fe72014-11-20 11:17:12 -08001947 appendGlyph(svg, 0, 0, 40, iid);
1948 title.text(data.id);
Simon Hunt61d04042014-11-11 17:27:16 -08001949
1950 data.propOrder.forEach(function(p) {
1951 if (p === '-') {
1952 addSep(tbody);
1953 } else {
1954 addProp(tbody, p, data.props[p]);
1955 }
1956 });
Simon Huntd72bc702014-11-13 18:38:04 -08001957
Thomas Vachuska4731f122014-11-20 04:56:19 -08001958 addSingleSelectActions(data);
Simon Hunt61d04042014-11-11 17:27:16 -08001959 }
1960
Thomas Vachuska4731f122014-11-20 04:56:19 -08001961 function addSingleSelectActions(data) {
Simon Huntd72bc702014-11-13 18:38:04 -08001962 detailPane.append('hr');
1963 // always want to allow 'show traffic'
Thomas Vachuska4731f122014-11-20 04:56:19 -08001964 addAction(detailPane, 'Show Related Traffic', showTrafficAction);
1965
1966 if (data.type === 'switch') {
1967 addAction(detailPane, 'Show Device Flows', showDeviceLinkFlowsAction);
1968 }
Simon Huntd72bc702014-11-13 18:38:04 -08001969 }
1970
1971 function addMultiSelectActions() {
1972 detailPane.append('hr');
1973 // always want to allow 'show traffic'
Thomas Vachuska4731f122014-11-20 04:56:19 -08001974 addAction(detailPane, 'Show Related Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001975 // if exactly two hosts are selected, also want 'add host intent'
1976 if (nSel() === 2 && allSelectionsClass('host')) {
Thomas Vachuska4731f122014-11-20 04:56:19 -08001977 addAction(detailPane, 'Add Host-to-Host Intent', addIntentAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001978 }
1979 }
1980
Simon Hunta5e89142014-11-14 07:00:33 -08001981 function addAction(panel, text, cb) {
1982 panel.append('div')
Simon Huntd72bc702014-11-13 18:38:04 -08001983 .classed('actionBtn', true)
1984 .text(text)
1985 .on('click', cb);
1986 }
1987
1988
Paul Greysonfcba0e82014-11-13 10:21:16 -08001989 function zoomPan(scale, translate) {
1990 zoomPanContainer.attr("transform", "translate(" + translate + ")scale(" + scale + ")");
1991 // keep the map lines constant width while zooming
Thomas Vachuska89543292014-11-19 11:28:33 -08001992 bgImg.style("stroke-width", 2.0 / scale + "px");
Paul Greysonfcba0e82014-11-13 10:21:16 -08001993 }
1994
1995 function resetZoomPan() {
1996 zoomPan(1, [0,0]);
1997 zoom.scale(1).translate([0,0]);
1998 }
1999
2000 function setupZoomPan() {
2001 function zoomed() {
Simon Huntdeab4322014-11-13 18:49:07 -08002002 if (!panZoom() ^ !d3.event.sourceEvent.metaKey) {
Paul Greysonfcba0e82014-11-13 10:21:16 -08002003 zoomPan(d3.event.scale, d3.event.translate);
2004 }
2005 }
2006
2007 zoom = d3.behavior.zoom()
2008 .translate([0, 0])
2009 .scale(1)
2010 .scaleExtent([1, 8])
2011 .on("zoom", zoomed);
2012
2013 svg.call(zoom);
2014 }
2015
Simon Hunt61d04042014-11-11 17:27:16 -08002016 // ==============================
2017 // Test harness code
Simon Hunt56d51852014-11-09 13:03:35 -08002018
2019 function prepareScenario(view, ctx, dbg) {
2020 var sc = scenario,
2021 urlSc = sc.evDir + ctx + sc.evScenario;
2022
2023 if (!ctx) {
2024 view.alert("No scenario specified (null ctx)");
2025 return;
2026 }
2027
2028 sc.view = view;
2029 sc.ctx = ctx;
2030 sc.debug = dbg;
2031 sc.evNumber = 0;
2032
2033 d3.json(urlSc, function(err, data) {
Simon Huntbb282f52014-11-10 11:08:19 -08002034 var p = data && data.params || {},
2035 desc = data && data.description || null,
Simon Huntfc274c92014-11-11 11:05:46 -08002036 intro = data && data.title;
Simon Huntbb282f52014-11-10 11:08:19 -08002037
Simon Hunt56d51852014-11-09 13:03:35 -08002038 if (err) {
2039 view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err);
2040 } else {
2041 sc.params = p;
Simon Huntbb282f52014-11-10 11:08:19 -08002042 if (desc) {
2043 intro += '\n\n ' + desc.join('\n ');
2044 }
2045 view.alert(intro);
Simon Hunt56d51852014-11-09 13:03:35 -08002046 }
2047 });
2048
2049 }
2050
Simon Hunt01095ff2014-11-13 16:37:29 -08002051 // ==============================
2052 // Toggle Buttons in masthead
Simon Hunt0c6d4192014-11-12 12:07:10 -08002053
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002054 // TODO: toggle button (and other widgets in the masthead) should be provided
2055 // by the framework; not generated by the view.
2056
Simon Hunta3dd9572014-11-20 15:22:41 -08002057 var showInstances;
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002058
2059 function addButtonBar(view) {
2060 var bb = d3.select('#mast')
2061 .append('span').classed('right', true).attr('id', 'bb');
2062
Simon Hunta5e89142014-11-14 07:00:33 -08002063 function mkTogBtn(text, cb) {
2064 return bb.append('span')
2065 .classed('btn', true)
2066 .text(text)
2067 .on('click', cb);
2068 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002069
Simon Hunta5e89142014-11-14 07:00:33 -08002070 showInstances = mkTogBtn('Show Instances', toggleInst);
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002071 }
2072
Simon Hunta5e89142014-11-14 07:00:33 -08002073 function instShown() {
2074 return showInstances.classed('active');
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002075 }
Simon Hunta5e89142014-11-14 07:00:33 -08002076 function toggleInst() {
2077 showInstances.classed('active', !instShown());
2078 if (instShown()) {
2079 oiBox.show();
2080 } else {
2081 oiBox.hide();
2082 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002083 }
2084
Simon Huntdeab4322014-11-13 18:49:07 -08002085 function panZoom() {
Simon Hunte5b71752014-11-18 20:06:07 -08002086 return false;
Simon Hunta5e89142014-11-14 07:00:33 -08002087 }
2088
2089 function trafficHover() {
Thomas Vachuska29617e52014-11-20 03:17:46 -08002090 return hoverModes[hoverMode] === 'intents';
Simon Hunta5e89142014-11-14 07:00:33 -08002091 }
Thomas Vachuska29617e52014-11-20 03:17:46 -08002092
2093 function flowsHover() {
2094 return hoverModes[hoverMode] === 'flows';
2095 }
2096
Simon Hunt7fa116d2014-11-17 14:16:55 -08002097 function loadGlyphs(svg) {
2098 var defs = svg.append('defs');
2099 gly.defBird(defs);
2100 gly.defBullhorn(defs);
Simon Huntc72967b2014-11-20 09:21:42 -08002101 gly.defGlyphs(defs);
Simon Hunt7fa116d2014-11-17 14:16:55 -08002102 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002103
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002104 // ==============================
Simon Hunt142d0032014-11-04 20:13:09 -08002105 // View life-cycle callbacks
Simon Hunt195cb382014-11-03 17:50:51 -08002106
Simon Huntf67722a2014-11-10 09:32:06 -08002107 function preload(view, ctx, flags) {
Simon Hunt142d0032014-11-04 20:13:09 -08002108 var w = view.width(),
2109 h = view.height(),
Simon Huntc7ee0662014-11-05 16:44:37 -08002110 fcfg = config.force,
2111 fpad = fcfg.pad,
2112 forceDim = [w - 2*fpad, h - 2*fpad];
Simon Hunt195cb382014-11-03 17:50:51 -08002113
Simon Hunt142d0032014-11-04 20:13:09 -08002114 // NOTE: view.$div is a D3 selection of the view's div
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002115 var viewBox = '0 0 ' + config.logicalSize + ' ' + config.logicalSize;
2116 svg = view.$div.append('svg').attr('viewBox', viewBox);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002117 setSize(svg, view);
2118
Simon Hunt7fa116d2014-11-17 14:16:55 -08002119 loadGlyphs(svg);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002120
Paul Greysonfcba0e82014-11-13 10:21:16 -08002121 zoomPanContainer = svg.append('g').attr('id', 'zoomPanContainer');
Paul Greysonfcba0e82014-11-13 10:21:16 -08002122 setupZoomPan();
2123
Simon Hunt1a9eff92014-11-07 11:06:34 -08002124 // add blue glow filter to svg layer
Paul Greysonfcba0e82014-11-13 10:21:16 -08002125 d3u.appendGlow(zoomPanContainer);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002126
Simon Huntc7ee0662014-11-05 16:44:37 -08002127 // group for the topology
Paul Greysonfcba0e82014-11-13 10:21:16 -08002128 topoG = zoomPanContainer.append('g')
Simon Huntd3b7d512014-11-12 15:48:41 -08002129 .attr('id', 'topo-G')
Simon Huntc7ee0662014-11-05 16:44:37 -08002130 .attr('transform', fcfg.translate());
2131
Simon Hunte2575b62014-11-18 15:25:53 -08002132 // subgroups for links, link labels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002133 linkG = topoG.append('g').attr('id', 'links');
Simon Hunte2575b62014-11-18 15:25:53 -08002134 linkLabelG = topoG.append('g').attr('id', 'linkLabels');
Simon Huntc7ee0662014-11-05 16:44:37 -08002135 nodeG = topoG.append('g').attr('id', 'nodes');
2136
Simon Hunte2575b62014-11-18 15:25:53 -08002137 // selection of links, linkLabels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002138 link = linkG.selectAll('.link');
Simon Hunte2575b62014-11-18 15:25:53 -08002139 linkLabel = linkLabelG.selectAll('.linkLabel');
Simon Huntc7ee0662014-11-05 16:44:37 -08002140 node = nodeG.selectAll('.node');
2141
Simon Hunt7cd48f32014-11-09 23:42:50 -08002142 function chrg(d) {
2143 return fcfg.charge[d.class] || -12000;
2144 }
Simon Hunt99c13842014-11-06 18:23:12 -08002145 function ldist(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002146 return fcfg.linkDistance[d.type] || 50;
Simon Hunt99c13842014-11-06 18:23:12 -08002147 }
2148 function lstrg(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002149 // 0.0 - 1.0
2150 return fcfg.linkStrength[d.type] || 1.0;
Simon Hunt99c13842014-11-06 18:23:12 -08002151 }
2152
Simon Hunt1a9eff92014-11-07 11:06:34 -08002153 function selectCb(d, self) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002154 selectObject(d, self);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002155 }
2156
2157 function atDragEnd(d, self) {
Simon Hunt56d51852014-11-09 13:03:35 -08002158 // once we've finished moving, pin the node in position
2159 d.fixed = true;
2160 d3.select(self).classed('fixed', true);
2161 if (config.useLiveData) {
Simon Hunt902c9922014-11-11 11:59:31 -08002162 sendUpdateMeta(d);
Simon Hunta255a2c2014-11-13 22:29:35 -08002163 } else {
2164 console.log('Moving node ' + d.id + ' to [' + d.x + ',' + d.y + ']');
Simon Hunt1a9eff92014-11-07 11:06:34 -08002165 }
2166 }
2167
Simon Hunt902c9922014-11-11 11:59:31 -08002168 function sendUpdateMeta(d) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002169 sendMessage('updateMeta', {
2170 id: d.id,
2171 'class': d.class,
Simon Hunt902c9922014-11-11 11:59:31 -08002172 'memento': {
Simon Hunt01095ff2014-11-13 16:37:29 -08002173 x: d.x,
2174 y: d.y
Simon Hunt902c9922014-11-11 11:59:31 -08002175 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002176 });
2177 }
2178
Simon Huntc7ee0662014-11-05 16:44:37 -08002179 // set up the force layout
2180 network.force = d3.layout.force()
2181 .size(forceDim)
2182 .nodes(network.nodes)
2183 .links(network.links)
Simon Hunt7cd48f32014-11-09 23:42:50 -08002184 .gravity(0.4)
2185 .friction(0.7)
2186 .charge(chrg)
Simon Hunt99c13842014-11-06 18:23:12 -08002187 .linkDistance(ldist)
2188 .linkStrength(lstrg)
Simon Huntc7ee0662014-11-05 16:44:37 -08002189 .on('tick', tick);
Simon Hunt195cb382014-11-03 17:50:51 -08002190
Simon Hunt01095ff2014-11-13 16:37:29 -08002191 network.drag = d3u.createDragBehavior(network.force,
Simon Huntdeab4322014-11-13 18:49:07 -08002192 selectCb, atDragEnd, panZoom);
Simon Hunt0c6d4192014-11-12 12:07:10 -08002193
2194 // create mask layer for when we lose connection to server.
Simon Hunta5e89142014-11-14 07:00:33 -08002195 // TODO: this should be part of the framework
Simon Hunt0c6d4192014-11-12 12:07:10 -08002196 mask = view.$div.append('div').attr('id','topo-mask');
2197 para(mask, 'Oops!');
2198 para(mask, 'Web-socket connection to server closed...');
2199 para(mask, 'Try refreshing the page.');
Simon Hunt12ce12e2014-11-15 21:13:19 -08002200
2201 mask.append('svg')
2202 .attr({
2203 id: 'mask-bird',
2204 width: w,
2205 height: h
2206 })
2207 .append('g')
2208 .attr('transform', birdTranslate(w, h))
2209 .style('opacity', 0.3)
2210 .append('use')
2211 .attr({
2212 'xlink:href': '#bird',
2213 width: config.birdDim,
2214 height: config.birdDim,
2215 fill: '#111'
Thomas Vachuska89543292014-11-19 11:28:33 -08002216 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08002217 }
Simon Hunt195cb382014-11-03 17:50:51 -08002218
Simon Hunt01095ff2014-11-13 16:37:29 -08002219 function para(sel, text) {
2220 sel.append('p').text(text);
2221 }
2222
2223
Simon Hunt56d51852014-11-09 13:03:35 -08002224 function load(view, ctx, flags) {
Simon Huntf67722a2014-11-10 09:32:06 -08002225 // resize, in case the window was resized while we were not loaded
2226 resize(view, ctx, flags);
2227
Simon Hunt99c13842014-11-06 18:23:12 -08002228 // cache the view token, so network topo functions can access it
2229 network.view = view;
Simon Hunt56d51852014-11-09 13:03:35 -08002230 config.useLiveData = !flags.local;
2231
2232 if (!config.useLiveData) {
2233 prepareScenario(view, ctx, flags.debug);
2234 }
Simon Hunt99c13842014-11-06 18:23:12 -08002235
2236 // set our radio buttons and key bindings
Simon Hunt9462e8c2014-11-14 17:28:09 -08002237 layerBtnSet = view.setRadio(layerButtons);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002238 view.setKeys(keyDispatch);
Simon Hunt195cb382014-11-03 17:50:51 -08002239
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002240 // patch in our "button bar" for now
2241 // TODO: implement a more official frameworky way of doing this..
2242 addButtonBar(view);
2243
Simon Huntd3b7d512014-11-12 15:48:41 -08002244 // Load map data asynchronously; complete startup after that..
2245 loadGeoJsonData();
Simon Hunta255a2c2014-11-13 22:29:35 -08002246 }
2247
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002248 function startAntTimer() {
Thomas Vachuska4731f122014-11-20 04:56:19 -08002249 if (!antTimer) {
2250 var pulses = [5, 3, 1.2, 3],
2251 pulse = 0;
2252 antTimer = setInterval(function () {
2253 pulse = pulse + 1;
2254 pulse = pulse === pulses.length ? 0 : pulse;
2255 d3.selectAll('.animated').style('stroke-width', pulses[pulse]);
2256 }, 200);
2257 }
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002258 }
2259
2260 function stopAntTimer() {
Simon Hunta255a2c2014-11-13 22:29:35 -08002261 if (antTimer) {
2262 clearInterval(antTimer);
2263 antTimer = null;
2264 }
Simon Huntd3b7d512014-11-12 15:48:41 -08002265 }
2266
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002267 function unload(view, ctx, flags) {
2268 stopAntTimer();
2269 }
2270
Simon Huntd3b7d512014-11-12 15:48:41 -08002271 // TODO: move these to config/state portion of script
Simon Hunta6a9fe72014-11-20 11:17:12 -08002272 var geoJsonUrl = 'json/map/continental_us.json',
Simon Huntd3b7d512014-11-12 15:48:41 -08002273 geoJson;
2274
2275 function loadGeoJsonData() {
2276 d3.json(geoJsonUrl, function (err, data) {
2277 if (err) {
2278 // fall back to USA map background
2279 loadStaticMap();
2280 } else {
2281 geoJson = data;
2282 loadGeoMap();
2283 }
2284
2285 // finally, connect to the server...
2286 if (config.useLiveData) {
2287 webSock.connect();
2288 }
2289 });
2290 }
2291
2292 function showBg() {
2293 return config.options.showBackground ? 'visible' : 'hidden';
2294 }
2295
2296 function loadStaticMap() {
2297 fnTrace('loadStaticMap', config.backgroundUrl);
2298 var w = network.view.width(),
2299 h = network.view.height();
2300
2301 // load the background image
2302 bgImg = svg.insert('svg:image', '#topo-G')
2303 .attr({
2304 id: 'topo-bg',
2305 width: w,
2306 height: h,
2307 'xlink:href': config.backgroundUrl
2308 })
2309 .style({
2310 visibility: showBg()
2311 });
2312 }
2313
2314 function loadGeoMap() {
2315 fnTrace('loadGeoMap', geoJsonUrl);
Simon Huntd3b7d512014-11-12 15:48:41 -08002316
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002317 // extracts the topojson data into geocoordinate-based geometry
2318 var topoData = topojson.feature(geoJson, geoJson.objects.states);
Simon Huntd3b7d512014-11-12 15:48:41 -08002319
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002320 // see: http://bl.ocks.org/mbostock/4707858
2321 geoMapProjection = d3.geo.mercator();
2322 var path = d3.geo.path().projection(geoMapProjection);
Simon Huntd3b7d512014-11-12 15:48:41 -08002323
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002324 geoMapProjection
2325 .scale(1)
2326 .translate([0, 0]);
Simon Huntd3b7d512014-11-12 15:48:41 -08002327
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002328 // [[x1,y1],[x2,y2]]
2329 var b = path.bounds(topoData);
Paul Greysonfcba0e82014-11-13 10:21:16 -08002330 // size map to 95% of minimum dimension to fill space
2331 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 -08002332 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 -08002333
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002334 geoMapProjection
2335 .scale(s)
2336 .translate(t);
2337
Paul Greysonfcba0e82014-11-13 10:21:16 -08002338 bgImg = zoomPanContainer.insert("g", '#topo-G');
Thomas Vachuska89543292014-11-19 11:28:33 -08002339 bgImg.attr('id', 'map').selectAll('path')
2340 .data(topoData.features)
2341 .enter()
2342 .append('path')
2343 .attr('d', path);
Simon Hunt195cb382014-11-03 17:50:51 -08002344 }
2345
Simon Huntf67722a2014-11-10 09:32:06 -08002346 function resize(view, ctx, flags) {
Simon Hunt12ce12e2014-11-15 21:13:19 -08002347 var w = view.width(),
2348 h = view.height();
2349
Simon Hunt934c3ce2014-11-05 11:45:07 -08002350 setSize(svg, view);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002351
2352 d3.select('#mask-bird').attr({ width: w, height: h})
2353 .select('g').attr('transform', birdTranslate(w, h));
Simon Hunt142d0032014-11-04 20:13:09 -08002354 }
2355
Simon Hunt12ce12e2014-11-15 21:13:19 -08002356 function birdTranslate(w, h) {
2357 var bdim = config.birdDim;
2358 return 'translate('+((w-bdim)*.4)+','+((h-bdim)*.1)+')';
2359 }
Simon Hunt142d0032014-11-04 20:13:09 -08002360
2361 // ==============================
2362 // View registration
Simon Hunt195cb382014-11-03 17:50:51 -08002363
Simon Hunt25248912014-11-04 11:25:48 -08002364 onos.ui.addView('topo', {
Simon Hunt142d0032014-11-04 20:13:09 -08002365 preload: preload,
2366 load: load,
Simon Hunta255a2c2014-11-13 22:29:35 -08002367 unload: unload,
Simon Hunt142d0032014-11-04 20:13:09 -08002368 resize: resize
Simon Hunt195cb382014-11-03 17:50:51 -08002369 });
2370
Simon Hunt61d04042014-11-11 17:27:16 -08002371 detailPane = onos.ui.addFloatingPanel('topo-detail');
Simon Hunta5e89142014-11-14 07:00:33 -08002372 oiBox = onos.ui.addFloatingPanel('topo-oibox', 'TL');
Simon Hunt61d04042014-11-11 17:27:16 -08002373
Simon Hunt195cb382014-11-03 17:50:51 -08002374}(ONOS));