blob: 82f71947516c36e09f850c03e99776d9210ba616 [file] [log] [blame]
Simon Hunt195cb382014-11-03 17:50:51 -08001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
Simon Hunt142d0032014-11-04 20:13:09 -080018 ONOS network topology viewer - version 1.1
Simon Hunt195cb382014-11-03 17:50:51 -080019
20 @author Simon Hunt
21 */
22
23(function (onos) {
24 'use strict';
25
Simon Hunt1a9eff92014-11-07 11:06:34 -080026 // shorter names for library APIs
Simon Huntbb282f52014-11-10 11:08:19 -080027 var d3u = onos.lib.d3util,
Simon Hunt12ce12e2014-11-15 21:13:19 -080028 gly = onos.lib.glyphs,
Simon Huntbb282f52014-11-10 11:08:19 -080029 trace;
Simon Hunt1a9eff92014-11-07 11:06:34 -080030
Simon Hunt195cb382014-11-03 17:50:51 -080031 // configuration data
32 var config = {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -080033 useLiveData: true,
Simon Huntfc274c92014-11-11 11:05:46 -080034 fnTrace: true,
Simon Hunt195cb382014-11-03 17:50:51 -080035 debugOn: false,
36 debug: {
Simon Hunt99c13842014-11-06 18:23:12 -080037 showNodeXY: true,
38 showKeyHandler: false
Simon Hunt195cb382014-11-03 17:50:51 -080039 },
Simon Hunt12ce12e2014-11-15 21:13:19 -080040 birdDim: 400,
Simon Hunt195cb382014-11-03 17:50:51 -080041 options: {
42 layering: true,
43 collisionPrevention: true,
Simon Hunt142d0032014-11-04 20:13:09 -080044 showBackground: true
Simon Hunt195cb382014-11-03 17:50:51 -080045 },
46 backgroundUrl: 'img/us-map.png',
Thomas Vachuska7d638d32014-11-07 10:24:43 -080047 webSockUrl: 'ws/topology',
Simon Hunt195cb382014-11-03 17:50:51 -080048 data: {
49 live: {
50 jsonUrl: 'rs/topology/graph',
51 detailPrefix: 'rs/topology/graph/',
52 detailSuffix: ''
53 },
54 fake: {
55 jsonUrl: 'json/network2.json',
56 detailPrefix: 'json/',
57 detailSuffix: '.json'
58 }
59 },
Simon Hunt99c13842014-11-06 18:23:12 -080060 labels: {
61 imgPad: 16,
62 padLR: 4,
63 padTB: 3,
64 marginLR: 3,
65 marginTB: 2,
66 port: {
67 gap: 3,
68 width: 18,
69 height: 14
70 }
71 },
Simon Hunt1a9eff92014-11-07 11:06:34 -080072 topo: {
Thomas Vachuska89543292014-11-19 11:28:33 -080073 linkBaseColor: '#666',
Simon Hunt1a9eff92014-11-07 11:06:34 -080074 linkInColor: '#66f',
Paul Greyson29cd58f2014-11-18 13:14:57 -080075 linkInWidth: 14,
Thomas Vachuska89543292014-11-19 11:28:33 -080076 linkOutColor: '#f00',
Simon Hunt13bf9c82014-11-18 07:26:44 -080077 linkOutWidth: 30
Simon Hunt1a9eff92014-11-07 11:06:34 -080078 },
Paul Greyson29cd58f2014-11-18 13:14:57 -080079 icons: {
Thomas Vachuskaece59ee2014-11-19 19:06:11 -080080 w: 30,
81 h: 30,
82 xoff: -16,
83 yoff: -14
Thomas Vachuska89543292014-11-19 11:28:33 -080084 },
85 iconUrl: {
86 device: 'img/device.png',
87 host: 'img/host.png',
88 pkt: 'img/pkt.png',
89 opt: 'img/opt.png'
Simon Hunt195cb382014-11-03 17:50:51 -080090 },
Simon Hunt195cb382014-11-03 17:50:51 -080091 force: {
Simon Hunt7cd48f32014-11-09 23:42:50 -080092 note_for_links: 'link.type is used to differentiate',
Simon Huntc7ee0662014-11-05 16:44:37 -080093 linkDistance: {
Simon Hunt7cd48f32014-11-09 23:42:50 -080094 direct: 100,
95 optical: 120,
Thomas Vachuska3266abf2014-11-13 09:28:46 -080096 hostLink: 3
Simon Huntc7ee0662014-11-05 16:44:37 -080097 },
98 linkStrength: {
Simon Hunt7cd48f32014-11-09 23:42:50 -080099 direct: 1.0,
100 optical: 1.0,
101 hostLink: 1.0
Simon Huntc7ee0662014-11-05 16:44:37 -0800102 },
Simon Hunt7cd48f32014-11-09 23:42:50 -0800103 note_for_nodes: 'node.class is used to differentiate',
Simon Huntc7ee0662014-11-05 16:44:37 -0800104 charge: {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800105 device: -8000,
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800106 host: -5000
Simon Huntc7ee0662014-11-05 16:44:37 -0800107 },
108 pad: 20,
Simon Hunt195cb382014-11-03 17:50:51 -0800109 translate: function() {
110 return 'translate(' +
Simon Huntc7ee0662014-11-05 16:44:37 -0800111 config.force.pad + ',' +
112 config.force.pad + ')';
Simon Hunt195cb382014-11-03 17:50:51 -0800113 }
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800114 },
115 // see below in creation of viewBox on main svg
116 logicalSize: 1000
Simon Hunt195cb382014-11-03 17:50:51 -0800117 };
118
Simon Hunt142d0032014-11-04 20:13:09 -0800119 // radio buttons
Simon Hunt9462e8c2014-11-14 17:28:09 -0800120 var layerButtons = [
121 { text: 'All Layers', id: 'all', cb: showAllLayers },
122 { text: 'Packet Only', id: 'pkt', cb: showPacketLayer },
123 { text: 'Optical Only', id: 'opt', cb: showOpticalLayer }
124 ],
125 layerBtnSet,
126 layerBtnDispatch = {
127 all: showAllLayers,
128 pkt: showPacketLayer,
129 opt: showOpticalLayer
130 };
Simon Hunt934c3ce2014-11-05 11:45:07 -0800131
132 // key bindings
133 var keyDispatch = {
Simon Hunta255a2c2014-11-13 22:29:35 -0800134 M: testMe, // TODO: remove (testing only)
135 S: injectStartupEvents, // TODO: remove (testing only)
136 space: injectTestEvent, // TODO: remove (testing only)
Simon Hunt99c13842014-11-06 18:23:12 -0800137
Simon Hunt01095ff2014-11-13 16:37:29 -0800138 B: toggleBg,
Simon Hunt934c3ce2014-11-05 11:45:07 -0800139 L: cycleLabels,
140 P: togglePorts,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800141 U: unpin,
Paul Greysonfcba0e82014-11-13 10:21:16 -0800142 R: resetZoomPan,
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -0800143 H: toggleHover,
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800144 V: showTrafficAction,
145 A: showAllTrafficAction,
Thomas Vachuska29617e52014-11-20 03:17:46 -0800146 F: showDeviceLinkFlowsAction,
Simon Hunt9462e8c2014-11-14 17:28:09 -0800147 esc: handleEscape
Simon Hunt934c3ce2014-11-05 11:45:07 -0800148 };
Simon Hunt142d0032014-11-04 20:13:09 -0800149
Simon Hunt195cb382014-11-03 17:50:51 -0800150 // state variables
Simon Hunt99c13842014-11-06 18:23:12 -0800151 var network = {
Simon Hunt50128c02014-11-08 13:36:15 -0800152 view: null, // view token reference
Simon Hunt99c13842014-11-06 18:23:12 -0800153 nodes: [],
154 links: [],
Simon Hunt269670f2014-11-17 16:17:43 -0800155 lookup: {},
156 revLinkToKey: {}
Simon Hunt99c13842014-11-06 18:23:12 -0800157 },
Simon Hunt56d51852014-11-09 13:03:35 -0800158 scenario = {
159 evDir: 'json/ev/',
160 evScenario: '/scenario.json',
161 evPrefix: '/ev_',
162 evOnos: '_onos.json',
163 evUi: '_ui.json',
164 ctx: null,
165 params: {},
166 evNumber: 0,
167 view: null,
168 debug: false
169 },
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800170 webSock,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800171 sid = 0,
Simon Hunt56d51852014-11-09 13:03:35 -0800172 deviceLabelIndex = 0,
173 hostLabelIndex = 0,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800174 selections = {},
Simon Hunta5e89142014-11-14 07:00:33 -0800175 selectOrder = [],
Simon Hunt6ac93f32014-11-13 12:17:27 -0800176 hovered = null,
Simon Hunta5e89142014-11-14 07:00:33 -0800177 detailPane,
Simon Hunta255a2c2014-11-13 22:29:35 -0800178 antTimer = null,
Simon Hunta5e89142014-11-14 07:00:33 -0800179 onosInstances = {},
180 onosOrder = [],
181 oiBox,
Simon Hunt9462e8c2014-11-14 17:28:09 -0800182 oiShowMaster = false,
Thomas Vachuska29617e52014-11-20 03:17:46 -0800183 hoverModes = [ 'none', 'intents', 'flows'],
184 hoverMode = 0,
Simon Hunt195cb382014-11-03 17:50:51 -0800185 portLabelsOn = false;
186
Simon Hunt934c3ce2014-11-05 11:45:07 -0800187 // D3 selections
188 var svg,
Paul Greysonfcba0e82014-11-13 10:21:16 -0800189 zoomPanContainer,
Simon Hunt934c3ce2014-11-05 11:45:07 -0800190 bgImg,
Simon Huntc7ee0662014-11-05 16:44:37 -0800191 topoG,
192 nodeG,
193 linkG,
Simon Hunte2575b62014-11-18 15:25:53 -0800194 linkLabelG,
Simon Huntc7ee0662014-11-05 16:44:37 -0800195 node,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800196 link,
Simon Hunte2575b62014-11-18 15:25:53 -0800197 linkLabel,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800198 mask;
Simon Hunt195cb382014-11-03 17:50:51 -0800199
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800200 // the projection for the map background
201 var geoMapProjection;
202
Paul Greysonfcba0e82014-11-13 10:21:16 -0800203 // the zoom function
204 var zoom;
205
Simon Hunt142d0032014-11-04 20:13:09 -0800206 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800207 // For Debugging / Development
Simon Hunt195cb382014-11-03 17:50:51 -0800208
Simon Hunt99c13842014-11-06 18:23:12 -0800209 function note(label, msg) {
210 console.log('NOTE: ' + label + ': ' + msg);
Simon Hunt195cb382014-11-03 17:50:51 -0800211 }
212
Simon Hunt99c13842014-11-06 18:23:12 -0800213 function debug(what) {
214 return config.debugOn && config.debug[what];
Simon Hunt934c3ce2014-11-05 11:45:07 -0800215 }
216
Simon Huntfc274c92014-11-11 11:05:46 -0800217 function fnTrace(msg, id) {
218 if (config.fnTrace) {
219 console.log('FN: ' + msg + ' [' + id + ']');
220 }
221 }
Simon Hunt99c13842014-11-06 18:23:12 -0800222
Simon Hunta5e89142014-11-14 07:00:33 -0800223 function evTrace(data) {
224 fnTrace(data.event, data.payload.id);
225 }
226
Simon Hunt934c3ce2014-11-05 11:45:07 -0800227 // ==============================
228 // Key Callbacks
229
Simon Hunt99c13842014-11-06 18:23:12 -0800230 function testMe(view) {
Simon Hunt625dc402014-11-18 10:57:18 -0800231 view.alert('Theme is ' + view.theme());
Simon Hunt99c13842014-11-06 18:23:12 -0800232 }
233
Simon Hunt56d51852014-11-09 13:03:35 -0800234 function abortIfLive() {
Simon Hunt50128c02014-11-08 13:36:15 -0800235 if (config.useLiveData) {
Simon Huntb53e0682014-11-12 13:32:01 -0800236 network.view.alert("Sorry, currently using live data..");
Simon Hunt56d51852014-11-09 13:03:35 -0800237 return true;
Simon Hunt50128c02014-11-08 13:36:15 -0800238 }
Simon Hunt56d51852014-11-09 13:03:35 -0800239 return false;
240 }
Simon Hunt50128c02014-11-08 13:36:15 -0800241
Simon Hunt56d51852014-11-09 13:03:35 -0800242 function testDebug(msg) {
243 if (scenario.debug) {
244 scenario.view.alert(msg);
245 }
246 }
Simon Hunt99c13842014-11-06 18:23:12 -0800247
Simon Hunt56d51852014-11-09 13:03:35 -0800248 function injectTestEvent(view) {
249 if (abortIfLive()) { return; }
250 var sc = scenario,
251 evn = ++sc.evNumber,
252 pfx = sc.evDir + sc.ctx + sc.evPrefix + evn,
253 onosUrl = pfx + sc.evOnos,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800254 uiUrl = pfx + sc.evUi,
255 stack = [
256 { url: onosUrl, cb: handleServerEvent },
257 { url: uiUrl, cb: handleUiEvent }
258 ];
259 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800260 }
261
Simon Hunt7cd48f32014-11-09 23:42:50 -0800262 function recurseFetchEvent(stack, evn) {
263 var v = scenario.view,
264 frame;
265 if (stack.length === 0) {
Simon Huntfc274c92014-11-11 11:05:46 -0800266 v.alert('Oops!\n\nNo event #' + evn + ' found.');
Simon Hunt7cd48f32014-11-09 23:42:50 -0800267 return;
268 }
269 frame = stack.shift();
270
271 d3.json(frame.url, function (err, data) {
Simon Hunt99c13842014-11-06 18:23:12 -0800272 if (err) {
Simon Hunt56d51852014-11-09 13:03:35 -0800273 if (err.status === 404) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800274 // if we didn't find the data, try the next stack frame
275 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800276 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800277 v.alert('non-404 error:\n\n' + frame.url + '\n\n' + err);
Simon Hunt56d51852014-11-09 13:03:35 -0800278 }
Simon Hunt99c13842014-11-06 18:23:12 -0800279 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800280 testDebug('loaded: ' + frame.url);
Simon Hunt1712ed82014-11-17 12:56:00 -0800281 wsTrace('test', JSON.stringify(data));
Simon Hunt7cd48f32014-11-09 23:42:50 -0800282 frame.cb(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800283 }
284 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800285
Simon Hunt56d51852014-11-09 13:03:35 -0800286 }
Simon Hunt50128c02014-11-08 13:36:15 -0800287
Simon Hunt56d51852014-11-09 13:03:35 -0800288 function handleUiEvent(data) {
Simon Huntbb282f52014-11-10 11:08:19 -0800289 scenario.view.alert('UI Tx: ' + data.event + '\n\n' +
290 JSON.stringify(data));
Simon Hunt56d51852014-11-09 13:03:35 -0800291 }
292
293 function injectStartupEvents(view) {
294 var last = scenario.params.lastAuto || 0;
295 if (abortIfLive()) { return; }
296
297 while (scenario.evNumber < last) {
Simon Hunt1a9eff92014-11-07 11:06:34 -0800298 injectTestEvent(view);
299 }
300 }
301
Simon Hunt934c3ce2014-11-05 11:45:07 -0800302 function toggleBg() {
303 var vis = bgImg.style('visibility');
304 bgImg.style('visibility', (vis === 'hidden') ? 'visible' : 'hidden');
305 }
306
Simon Hunt99c13842014-11-06 18:23:12 -0800307 function cycleLabels() {
Simon Huntbb282f52014-11-10 11:08:19 -0800308 deviceLabelIndex = (deviceLabelIndex === network.deviceLabelCount - 1)
309 ? 0 : deviceLabelIndex + 1;
Simon Hunt5f36d342014-11-08 21:33:14 -0800310
Simon Hunt99c13842014-11-06 18:23:12 -0800311 network.nodes.forEach(function (d) {
Simon Huntbb282f52014-11-10 11:08:19 -0800312 if (d.class === 'device') {
313 updateDeviceLabel(d);
314 }
Simon Hunt99c13842014-11-06 18:23:12 -0800315 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800316 }
317
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -0800318 function toggleHover(view) {
Thomas Vachuska29617e52014-11-20 03:17:46 -0800319 hoverMode++;
320 if (hoverMode === hoverModes.length) {
321 hoverMode = 0;
322 }
323 console.log('Hover Mode:' + hoverMode + ': ' + hoverModes[hoverMode]);
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -0800324 }
325
Simon Hunt934c3ce2014-11-05 11:45:07 -0800326 function togglePorts(view) {
Simon Hunt50128c02014-11-08 13:36:15 -0800327 view.alert('togglePorts() callback')
Simon Hunt934c3ce2014-11-05 11:45:07 -0800328 }
329
Simon Hunt6ac93f32014-11-13 12:17:27 -0800330 function unpin() {
331 if (hovered) {
332 hovered.fixed = false;
333 hovered.el.classed('fixed', false);
334 network.force.resume();
335 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800336 }
337
Simon Hunt9462e8c2014-11-14 17:28:09 -0800338 function handleEscape(view) {
339 if (oiShowMaster) {
340 cancelAffinity();
341 } else {
342 deselectAll();
343 }
344 }
345
Simon Hunt934c3ce2014-11-05 11:45:07 -0800346 // ==============================
347 // Radio Button Callbacks
348
Simon Hunta5e89142014-11-14 07:00:33 -0800349 var layerLookup = {
350 host: {
351 endstation: 'pkt', // default, if host event does not define type
Thomas Vachuska89543292014-11-19 11:28:33 -0800352 router: 'pkt',
Simon Hunta5e89142014-11-14 07:00:33 -0800353 bgpSpeaker: 'pkt'
354 },
355 device: {
356 switch: 'pkt',
357 roadm: 'opt'
358 },
359 link: {
360 hostLink: 'pkt',
361 direct: 'pkt',
Simon Hunt8257f4c2014-11-16 19:34:54 -0800362 indirect: '',
363 tunnel: '',
Simon Hunta5e89142014-11-14 07:00:33 -0800364 optical: 'opt'
365 }
366 };
367
368 function inLayer(d, layer) {
Simon Hunt8257f4c2014-11-16 19:34:54 -0800369 var type = d.class === 'link' ? d.type() : d.type,
370 look = layerLookup[d.class],
371 lyr = look && look[type];
Simon Hunta5e89142014-11-14 07:00:33 -0800372 return lyr === layer;
373 }
374
375 function unsuppressLayer(which) {
376 node.each(function (d) {
377 var node = d.el;
378 if (inLayer(d, which)) {
379 node.classed('suppressed', false);
380 }
381 });
382
383 link.each(function (d) {
384 var link = d.el;
385 if (inLayer(d, which)) {
386 link.classed('suppressed', false);
387 }
388 });
389 }
390
Simon Hunt9462e8c2014-11-14 17:28:09 -0800391 function suppressLayers(b) {
392 node.classed('suppressed', b);
393 link.classed('suppressed', b);
Simon Hunt142d0032014-11-04 20:13:09 -0800394// d3.selectAll('svg .port').classed('inactive', false);
395// d3.selectAll('svg .portText').classed('inactive', false);
Simon Hunt195cb382014-11-03 17:50:51 -0800396 }
397
Simon Hunt9462e8c2014-11-14 17:28:09 -0800398 function showAllLayers() {
399 suppressLayers(false);
400 }
401
Simon Hunt195cb382014-11-03 17:50:51 -0800402 function showPacketLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800403 node.classed('suppressed', true);
404 link.classed('suppressed', true);
405 unsuppressLayer('pkt');
Simon Hunt195cb382014-11-03 17:50:51 -0800406 }
407
408 function showOpticalLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800409 node.classed('suppressed', true);
410 link.classed('suppressed', true);
411 unsuppressLayer('opt');
Simon Hunt195cb382014-11-03 17:50:51 -0800412 }
413
Simon Hunt9462e8c2014-11-14 17:28:09 -0800414 function restoreLayerState() {
415 layerBtnDispatch[layerBtnSet.selected()]();
416 }
417
Simon Hunt142d0032014-11-04 20:13:09 -0800418 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800419 // Private functions
420
Simon Hunt99c13842014-11-06 18:23:12 -0800421 function safeId(s) {
422 return s.replace(/[^a-z0-9]/gi, '-');
423 }
424
Simon Huntc7ee0662014-11-05 16:44:37 -0800425 // set the size of the given element to that of the view (reduced if padded)
426 function setSize(el, view, pad) {
427 var padding = pad ? pad * 2 : 0;
Simon Hunt934c3ce2014-11-05 11:45:07 -0800428 el.attr({
Simon Huntc7ee0662014-11-05 16:44:37 -0800429 width: view.width() - padding,
430 height: view.height() - padding
Simon Hunt934c3ce2014-11-05 11:45:07 -0800431 });
432 }
433
Simon Hunt8257f4c2014-11-16 19:34:54 -0800434 function makeNodeKey(d, what) {
435 var port = what + 'Port';
436 return d[what] + '/' + d[port];
437 }
438
439 function makeLinkKey(d, flipped) {
440 var one = flipped ? makeNodeKey(d, 'dst') : makeNodeKey(d, 'src'),
441 two = flipped ? makeNodeKey(d, 'src') : makeNodeKey(d, 'dst');
442 return one + '-' + two;
443 }
444
Simon Hunt269670f2014-11-17 16:17:43 -0800445 function findLinkById(id) {
446 // check to see if this is a reverse lookup, else default to given id
447 var key = network.revLinkToKey[id] || id;
448 return key && network.lookup[key];
449 }
450
Simon Hunt8257f4c2014-11-16 19:34:54 -0800451 function findLink(linkData, op) {
452 var key = makeLinkKey(linkData),
453 keyrev = makeLinkKey(linkData, 1),
454 link = network.lookup[key],
455 linkRev = network.lookup[keyrev],
456 result = {},
457 ldata = link || linkRev,
458 rawLink;
459
460 if (op === 'add') {
461 if (link) {
462 // trying to add a link that we already know about
463 result.ldata = link;
464 result.badLogic = 'addLink: link already added';
465
466 } else if (linkRev) {
467 // we found the reverse of the link to be added
468 result.ldata = linkRev;
469 if (linkRev.fromTarget) {
470 result.badLogic = 'addLink: link already added';
471 }
472 }
473 } else if (op === 'update') {
474 if (!ldata) {
475 result.badLogic = 'updateLink: link not found';
476 } else {
477 rawLink = link ? ldata.fromSource : ldata.fromTarget;
478 result.updateWith = function (data) {
479 $.extend(rawLink, data);
480 restyleLinkElement(ldata);
481 }
482 }
483 } else if (op === 'remove') {
484 if (!ldata) {
485 result.badLogic = 'removeLink: link not found';
486 } else {
487 rawLink = link ? ldata.fromSource : ldata.fromTarget;
488
489 if (!rawLink) {
490 result.badLogic = 'removeLink: link not found';
491
492 } else {
493 result.removeRawLink = function () {
494 if (link) {
495 // remove fromSource
496 ldata.fromSource = null;
497 if (ldata.fromTarget) {
498 // promote target into source position
499 ldata.fromSource = ldata.fromTarget;
500 ldata.fromTarget = null;
501 ldata.key = keyrev;
502 delete network.lookup[key];
503 network.lookup[keyrev] = ldata;
Simon Hunt269670f2014-11-17 16:17:43 -0800504 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800505 }
506 } else {
507 // remove fromTarget
508 ldata.fromTarget = null;
Simon Hunt269670f2014-11-17 16:17:43 -0800509 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800510 }
511 if (ldata.fromSource) {
512 restyleLinkElement(ldata);
513 } else {
514 removeLinkElement(ldata);
515 }
516 }
517 }
518 }
519 }
520 return result;
521 }
522
523 function addLinkUpdate(ldata, link) {
524 // add link event, but we already have the reverse link installed
525 ldata.fromTarget = link;
Simon Hunt269670f2014-11-17 16:17:43 -0800526 network.revLinkToKey[link.id] = ldata.key;
Simon Hunt8257f4c2014-11-16 19:34:54 -0800527 restyleLinkElement(ldata);
528 }
529
530 var allLinkTypes = 'direct indirect optical tunnel',
531 defaultLinkType = 'direct';
532
533 function restyleLinkElement(ldata) {
534 // this fn's job is to look at raw links and decide what svg classes
535 // need to be applied to the line element in the DOM
536 var el = ldata.el,
537 type = ldata.type(),
538 lw = ldata.linkWidth(),
539 online = ldata.online();
540
541 el.classed('link', true);
542 el.classed('inactive', !online);
543 el.classed(allLinkTypes, false);
544 if (type) {
545 el.classed(type, true);
546 }
547 el.transition()
548 .duration(1000)
Thomas Vachuska89543292014-11-19 11:28:33 -0800549 .attr('stroke-width', linkScale(lw))
550 .attr('stroke', config.topo.linkBaseColor);
Simon Hunt8257f4c2014-11-16 19:34:54 -0800551 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800552
Simon Hunt99c13842014-11-06 18:23:12 -0800553 // ==============================
554 // Event handlers for server-pushed events
555
Simon Huntbb282f52014-11-10 11:08:19 -0800556 function logicError(msg) {
557 // TODO, report logic error to server, via websock, so it can be logged
Simon Huntcb56cff2014-11-17 11:42:26 -0800558 //network.view.alert('Logic Error:\n\n' + msg);
Simon Huntfc274c92014-11-11 11:05:46 -0800559 console.warn(msg);
Simon Huntbb282f52014-11-10 11:08:19 -0800560 }
561
Simon Hunt99c13842014-11-06 18:23:12 -0800562 var eventDispatch = {
Simon Hunta5e89142014-11-14 07:00:33 -0800563 addInstance: addInstance,
Simon Hunt99c13842014-11-06 18:23:12 -0800564 addDevice: addDevice,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800565 addLink: addLink,
Simon Hunt56d51852014-11-09 13:03:35 -0800566 addHost: addHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800567
Simon Huntfcfb46c2014-11-19 12:53:38 -0800568 updateInstance: updateInstance,
Simon Huntbb282f52014-11-10 11:08:19 -0800569 updateDevice: updateDevice,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800570 updateLink: updateLink,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800571 updateHost: updateHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800572
Simon Huntd72bc702014-11-13 18:38:04 -0800573 removeInstance: stillToImplement,
Simon Huntbb282f52014-11-10 11:08:19 -0800574 removeDevice: stillToImplement,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800575 removeLink: removeLink,
Simon Hunt44031102014-11-11 13:20:36 -0800576 removeHost: removeHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800577
Simon Hunt61d04042014-11-11 17:27:16 -0800578 showDetails: showDetails,
Simon Huntb53e0682014-11-12 13:32:01 -0800579 showTraffic: showTraffic
Simon Hunt99c13842014-11-06 18:23:12 -0800580 };
581
Simon Hunta5e89142014-11-14 07:00:33 -0800582 function addInstance(data) {
583 evTrace(data);
584 var inst = data.payload,
585 id = inst.id;
586 if (onosInstances[id]) {
587 logicError('ONOS instance already added: ' + id);
588 return;
589 }
590 onosInstances[id] = inst;
591 onosOrder.push(inst);
592 updateInstances();
593 }
594
Simon Hunt99c13842014-11-06 18:23:12 -0800595 function addDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800596 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800597 var device = data.payload,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800598 nodeData = createDeviceNode(device);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800599 network.nodes.push(nodeData);
600 network.lookup[nodeData.id] = nodeData;
Simon Hunt99c13842014-11-06 18:23:12 -0800601 updateNodes();
602 network.force.start();
603 }
604
Simon Hunt99c13842014-11-06 18:23:12 -0800605 function addLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800606 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800607 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800608 result = findLink(link, 'add'),
609 bad = result.badLogic,
610 ldata = result.ldata;
611
612 if (bad) {
613 logicError(bad + ': ' + link.id);
614 return;
615 }
616
617 if (ldata) {
618 // we already have a backing store link for src/dst nodes
619 addLinkUpdate(ldata, link);
620 return;
621 }
622
623 // no backing store link yet
624 ldata = createLink(link);
625 if (ldata) {
626 network.links.push(ldata);
627 network.lookup[ldata.key] = ldata;
Simon Hunt99c13842014-11-06 18:23:12 -0800628 updateLinks();
629 network.force.start();
630 }
631 }
632
Simon Hunt56d51852014-11-09 13:03:35 -0800633 function addHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800634 evTrace(data);
Simon Hunt56d51852014-11-09 13:03:35 -0800635 var host = data.payload,
636 node = createHostNode(host),
637 lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800638 network.nodes.push(node);
639 network.lookup[host.id] = node;
640 updateNodes();
641
642 lnk = createHostLink(host);
643 if (lnk) {
Simon Hunt44031102014-11-11 13:20:36 -0800644 node.linkData = lnk; // cache ref on its host
Simon Hunt56d51852014-11-09 13:03:35 -0800645 network.links.push(lnk);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800646 network.lookup[host.ingress] = lnk;
647 network.lookup[host.egress] = lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800648 updateLinks();
649 }
650 network.force.start();
651 }
652
Simon Hunt44031102014-11-11 13:20:36 -0800653 // TODO: fold updateX(...) methods into one base method; remove duplication
Simon Hunt56a2ea42014-11-19 12:39:31 -0800654
655 function updateInstance(data) {
656 evTrace(data);
657 var inst = data.payload,
658 id = inst.id,
659 instData = onosInstances[id];
660 if (instData) {
661 $.extend(instData, inst);
662 updateInstances();
Simon Hunt56a2ea42014-11-19 12:39:31 -0800663 } else {
664 logicError('updateInstance lookup fail. ID = "' + id + '"');
665 }
666 }
667
Simon Huntbb282f52014-11-10 11:08:19 -0800668 function updateDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800669 evTrace(data);
Simon Huntbb282f52014-11-10 11:08:19 -0800670 var device = data.payload,
671 id = device.id,
672 nodeData = network.lookup[id];
673 if (nodeData) {
674 $.extend(nodeData, device);
675 updateDeviceState(nodeData);
676 } else {
677 logicError('updateDevice lookup fail. ID = "' + id + '"');
678 }
679 }
680
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800681 function updateLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800682 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800683 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800684 result = findLink(link, 'update'),
685 bad = result.badLogic;
686 if (bad) {
687 logicError(bad + ': ' + link.id);
688 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800689 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800690 result.updateWith(link);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800691 }
692
Simon Hunt7cd48f32014-11-09 23:42:50 -0800693 function updateHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800694 evTrace(data);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800695 var host = data.payload,
Simon Huntbb282f52014-11-10 11:08:19 -0800696 id = host.id,
697 hostData = network.lookup[id];
698 if (hostData) {
699 $.extend(hostData, host);
700 updateHostState(hostData);
701 } else {
702 logicError('updateHost lookup fail. ID = "' + id + '"');
703 }
Simon Hunt7cd48f32014-11-09 23:42:50 -0800704 }
705
Simon Hunt44031102014-11-11 13:20:36 -0800706 // TODO: fold removeX(...) methods into base method - remove dup code
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800707 function removeLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800708 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800709 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800710 result = findLink(link, 'remove'),
711 bad = result.badLogic;
712 if (bad) {
713 logicError(bad + ': ' + link.id);
714 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800715 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800716 result.removeRawLink();
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800717 }
718
Simon Hunt44031102014-11-11 13:20:36 -0800719 function removeHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800720 evTrace(data);
Simon Hunt44031102014-11-11 13:20:36 -0800721 var host = data.payload,
722 id = host.id,
723 hostData = network.lookup[id];
724 if (hostData) {
725 removeHostElement(hostData);
726 } else {
727 logicError('removeHost lookup fail. ID = "' + id + '"');
728 }
729 }
730
Simon Hunt61d04042014-11-11 17:27:16 -0800731 function showDetails(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800732 evTrace(data);
Simon Hunt61d04042014-11-11 17:27:16 -0800733 populateDetails(data.payload);
734 detailPane.show();
735 }
736
Simon Huntb53e0682014-11-12 13:32:01 -0800737 function showTraffic(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800738 evTrace(data);
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800739 var paths = data.payload.paths;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800740
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800741 // Revert any links hilighted previously.
Thomas Vachuskaa3148a72014-11-19 21:38:35 -0800742 link.attr('stroke-width', null)
743 .style('stroke-width', null)
744 .classed('primary secondary animated optical', false);
Simon Hunte2575b62014-11-18 15:25:53 -0800745 // Remove all previous labels.
746 removeLinkLabels();
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800747
Thomas Vachuskaa3148a72014-11-19 21:38:35 -0800748 if (paths.length && !antTimer) {
749 startAntTimer();
750 } else if (!paths.length && antTimer) {
751 stopAntTimer();
752 }
753
Simon Hunte2575b62014-11-18 15:25:53 -0800754 // Now hilight all links in the paths payload, and attach
755 // labels to them, if they are defined.
Simon Hunta255a2c2014-11-13 22:29:35 -0800756 paths.forEach(function (p) {
Simon Hunte2575b62014-11-18 15:25:53 -0800757 var n = p.links.length,
758 i,
759 ldata;
760
761 for (i=0; i<n; i++) {
762 ldata = findLinkById(p.links[i]);
763 if (ldata) {
764 ldata.el.classed(p.class, true);
765 ldata.label = p.labels[i];
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800766 }
Simon Hunte2575b62014-11-18 15:25:53 -0800767 }
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800768 });
Simon Hunte2575b62014-11-18 15:25:53 -0800769 updateLinks();
Simon Huntb53e0682014-11-12 13:32:01 -0800770 }
771
Simon Hunt56d51852014-11-09 13:03:35 -0800772 // ...............................
773
774 function stillToImplement(data) {
775 var p = data.payload;
776 note(data.event, p.id);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800777 network.view.alert('Not yet implemented: "' + data.event + '"');
Simon Hunt56d51852014-11-09 13:03:35 -0800778 }
Simon Hunt99c13842014-11-06 18:23:12 -0800779
780 function unknownEvent(data) {
Simon Hunt50128c02014-11-08 13:36:15 -0800781 network.view.alert('Unknown event type: "' + data.event + '"');
Simon Hunt99c13842014-11-06 18:23:12 -0800782 }
783
784 function handleServerEvent(data) {
785 var fn = eventDispatch[data.event] || unknownEvent;
786 fn(data);
787 }
788
789 // ==============================
Simon Hunt61d04042014-11-11 17:27:16 -0800790 // Out-going messages...
791
Simon Huntb53e0682014-11-12 13:32:01 -0800792 function userFeedback(msg) {
793 // for now, use the alert pane as is. Maybe different alert style in
794 // the future (centered on view; dismiss button?)
795 network.view.alert(msg);
796 }
797
798 function nSel() {
799 return selectOrder.length;
800 }
Simon Hunt61d04042014-11-11 17:27:16 -0800801 function getSel(idx) {
802 return selections[selectOrder[idx]];
803 }
Simon Huntb53e0682014-11-12 13:32:01 -0800804 function getSelId(idx) {
805 return getSel(idx).obj.id;
806 }
807 function allSelectionsClass(cls) {
808 for (var i=0, n=nSel(); i<n; i++) {
809 if (getSel(i).obj.class !== cls) {
810 return false;
811 }
812 }
813 return true;
814 }
Simon Hunt61d04042014-11-11 17:27:16 -0800815
Simon Hunt61d04042014-11-11 17:27:16 -0800816 // request details for the selected element
Simon Huntd72bc702014-11-13 18:38:04 -0800817 // invoked from selection of a single node.
Simon Hunt61d04042014-11-11 17:27:16 -0800818 function requestDetails() {
819 var data = getSel(0).obj,
820 payload = {
821 id: data.id,
822 class: data.class
823 };
824 sendMessage('requestDetails', payload);
825 }
826
Simon Huntd72bc702014-11-13 18:38:04 -0800827 function addIntentAction() {
828 sendMessage('addHostIntent', {
829 one: getSelId(0),
Thomas Vachuska82f2c622014-11-17 12:23:18 -0800830 two: getSelId(1),
831 ids: [ getSelId(0), getSelId(1) ]
Simon Huntd72bc702014-11-13 18:38:04 -0800832 });
833 }
834
835 function showTrafficAction() {
Thomas Vachuska29617e52014-11-20 03:17:46 -0800836 // force intents hover mode
837 hoverMode = 1;
838 showSelectTraffic();
839 }
840
841 function showSelectTraffic() {
Simon Huntd72bc702014-11-13 18:38:04 -0800842 // if nothing is hovered over, and nothing selected, send cancel request
843 if (!hovered && nSel() === 0) {
844 sendMessage('cancelTraffic', {});
845 return;
846 }
847
848 // NOTE: hover is only populated if "show traffic on hover" is
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800849 // toggled on, and the item hovered is a host or a device...
850 var hoverId = (trafficHover() && hovered &&
851 (hovered.class === 'host' || hovered.class === 'device'))
Simon Huntd72bc702014-11-13 18:38:04 -0800852 ? hovered.id : '';
853 sendMessage('requestTraffic', {
854 ids: selectOrder,
855 hover: hoverId
856 });
857 }
858
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800859 function showAllTrafficAction() {
860 sendMessage('requestAllTraffic', {});
861 }
862
Thomas Vachuska29617e52014-11-20 03:17:46 -0800863 function showDeviceLinkFlowsAction() {
864 // force intents hover mode
865 hoverMode = 2;
866 showDeviceLinkFlows();
867 }
868
869 function showDeviceLinkFlows() {
870 // if nothing is hovered over, and nothing selected, send cancel request
871 if (!hovered && nSel() === 0) {
872 sendMessage('cancelTraffic', {});
873 return;
874 }
875 var hoverId = (flowsHover() && hovered && hovered.class === 'device') ?
876 hovered.id : '';
877 sendMessage('requestDeviceLinkFlows', {
878 ids: selectOrder,
879 hover: hoverId
880 });
881 }
Simon Huntd72bc702014-11-13 18:38:04 -0800882
Simon Hunt61d04042014-11-11 17:27:16 -0800883 // ==============================
Simon Hunta5e89142014-11-14 07:00:33 -0800884 // onos instance panel functions
885
886 function updateInstances() {
887 var onoses = oiBox.el.selectAll('.onosInst')
888 .data(onosOrder, function (d) { return d.id; });
889
890 // operate on existing onoses if necessary
Simon Huntfcfb46c2014-11-19 12:53:38 -0800891 onoses.classed('online', function (d) { return d.online; });
Simon Hunta5e89142014-11-14 07:00:33 -0800892
893 var entering = onoses.enter()
894 .append('div')
895 .attr('class', 'onosInst')
896 .classed('online', function (d) { return d.online; })
Simon Hunt9c15eca2014-11-15 18:37:59 -0800897 .on('click', clickInst);
898
899 entering.each(function (d, i) {
900 var el = d3.select(this),
901 img;
902
Thomas Vachuskacd2920c2014-11-19 14:49:55 -0800903 $('<img src="img/node.png">').appendTo(el);
Simon Hunt9c15eca2014-11-15 18:37:59 -0800904 img = el.select('img')
905 .attr({
Simon Huntfcfb46c2014-11-19 12:53:38 -0800906 width: 30
Simon Hunt9c15eca2014-11-15 18:37:59 -0800907 });
908
909 $('<div>').attr('class', 'onosTitle').text(d.id).appendTo(el);
910
911 // is the UI attached to this instance?
912 // TODO: need uiAttached boolean in instance data
913 //if (d.uiAttached) {
914 if (i === 0) {
915 $('<img src="img/ui.png">').attr('class','ui').appendTo(el);
916 }
917 });
Simon Hunta5e89142014-11-14 07:00:33 -0800918
919 // operate on existing + new onoses here
920
921 // the departed...
922 var exiting = onoses.exit()
923 .transition()
924 .style('opacity', 0)
925 .remove();
926 }
927
Simon Hunt9462e8c2014-11-14 17:28:09 -0800928 function clickInst(d) {
929 var el = d3.select(this),
930 aff = el.classed('affinity');
931 if (!aff) {
932 setAffinity(el, d);
933 } else {
934 cancelAffinity();
935 }
936 }
937
938 function setAffinity(el, d) {
939 d3.selectAll('.onosInst')
940 .classed('mastership', true)
941 .classed('affinity', false);
942 el.classed('affinity', true);
943
944 suppressLayers(true);
945 node.each(function (n) {
946 if (n.master === d.id) {
947 n.el.classed('suppressed', false);
948 }
949 });
950 oiShowMaster = true;
951 }
952
953 function cancelAffinity() {
954 d3.selectAll('.onosInst')
955 .classed('mastership affinity', false);
956 restoreLayerState();
957 oiShowMaster = false;
958 }
959
Simon Hunta5e89142014-11-14 07:00:33 -0800960 // ==============================
Simon Hunt99c13842014-11-06 18:23:12 -0800961 // force layout modification functions
962
963 function translate(x, y) {
964 return 'translate(' + x + ',' + y + ')';
965 }
966
Simon Hunte2575b62014-11-18 15:25:53 -0800967 function rotate(deg) {
968 return 'rotate(' + deg + ')';
969 }
970
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800971 function missMsg(what, id) {
972 return '\n[' + what + '] "' + id + '" missing ';
973 }
974
975 function linkEndPoints(srcId, dstId) {
976 var srcNode = network.lookup[srcId],
977 dstNode = network.lookup[dstId],
978 sMiss = !srcNode ? missMsg('src', srcId) : '',
979 dMiss = !dstNode ? missMsg('dst', dstId) : '';
980
981 if (sMiss || dMiss) {
982 logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
983 return null;
984 }
985 return {
986 source: srcNode,
987 target: dstNode,
988 x1: srcNode.x,
989 y1: srcNode.y,
990 x2: dstNode.x,
991 y2: dstNode.y
992 };
993 }
994
Simon Hunt56d51852014-11-09 13:03:35 -0800995 function createHostLink(host) {
996 var src = host.id,
997 dst = host.cp.device,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800998 id = host.ingress,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800999 lnk = linkEndPoints(src, dst);
Simon Hunt56d51852014-11-09 13:03:35 -08001000
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001001 if (!lnk) {
Simon Hunt56d51852014-11-09 13:03:35 -08001002 return null;
1003 }
1004
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001005 // Synthesize link ...
1006 $.extend(lnk, {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001007 key: id,
Simon Hunt56d51852014-11-09 13:03:35 -08001008 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -08001009
1010 type: function () { return 'hostLink'; },
1011 // TODO: ideally, we should see if our edge switch is online...
1012 online: function () { return true; },
1013 linkWidth: function () { return 1; }
Simon Hunt7cd48f32014-11-09 23:42:50 -08001014 });
Simon Hunt99c13842014-11-06 18:23:12 -08001015 return lnk;
1016 }
1017
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001018 function createLink(link) {
1019 var lnk = linkEndPoints(link.src, link.dst),
1020 type = link.type;
1021
1022 if (!lnk) {
1023 return null;
1024 }
1025
Simon Hunt8257f4c2014-11-16 19:34:54 -08001026 $.extend(lnk, {
1027 key: link.id,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001028 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -08001029 fromSource: link,
1030
1031 // functions to aggregate dual link state
1032 type: function () {
1033 var s = lnk.fromSource,
1034 t = lnk.fromTarget;
1035 return (s && s.type) || (t && t.type) || defaultLinkType;
1036 },
1037 online: function () {
1038 var s = lnk.fromSource,
1039 t = lnk.fromTarget;
1040 return (s && s.online) || (t && t.online);
1041 },
1042 linkWidth: function () {
1043 var s = lnk.fromSource,
1044 t = lnk.fromTarget,
1045 ws = (s && s.linkWidth) || 0,
1046 wt = (t && t.linkWidth) || 0;
1047 return Math.max(ws, wt);
1048 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001049 });
1050 return lnk;
Simon Hunt1a9eff92014-11-07 11:06:34 -08001051 }
1052
Simon Hunte2575b62014-11-18 15:25:53 -08001053 function removeLinkLabels() {
1054 network.links.forEach(function (d) {
1055 d.label = '';
1056 });
1057 }
1058
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001059 var widthRatio = 1.4,
1060 linkScale = d3.scale.linear()
1061 .domain([1, 12])
1062 .range([widthRatio, 12 * widthRatio])
1063 .clamp(true);
1064
Simon Hunt99c13842014-11-06 18:23:12 -08001065 function updateLinks() {
1066 link = linkG.selectAll('.link')
Simon Hunt8257f4c2014-11-16 19:34:54 -08001067 .data(network.links, function (d) { return d.key; });
Simon Hunt99c13842014-11-06 18:23:12 -08001068
1069 // operate on existing links, if necessary
1070 // link .foo() .bar() ...
1071
1072 // operate on entering links:
1073 var entering = link.enter()
1074 .append('line')
1075 .attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001076 x1: function (d) { return d.x1; },
1077 y1: function (d) { return d.y1; },
1078 x2: function (d) { return d.x2; },
1079 y2: function (d) { return d.y2; },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001080 stroke: config.topo.linkInColor,
1081 'stroke-width': config.topo.linkInWidth
Simon Hunt99c13842014-11-06 18:23:12 -08001082 });
1083
1084 // augment links
Simon Hunt7cd48f32014-11-09 23:42:50 -08001085 entering.each(function (d) {
1086 var link = d3.select(this);
1087 // provide ref to element selection from backing data....
1088 d.el = link;
Simon Hunt8257f4c2014-11-16 19:34:54 -08001089 restyleLinkElement(d);
Simon Hunt7cd48f32014-11-09 23:42:50 -08001090 });
Thomas Vachuska4830d392014-11-09 17:09:56 -08001091
1092 // operate on both existing and new links, if necessary
1093 //link .foo() .bar() ...
1094
Simon Hunte2575b62014-11-18 15:25:53 -08001095 // apply or remove labels
1096 var labelData = getLabelData();
1097 applyLinkLabels(labelData);
1098
Thomas Vachuska4830d392014-11-09 17:09:56 -08001099 // operate on exiting links:
Thomas Vachuska4830d392014-11-09 17:09:56 -08001100 link.exit()
Simon Hunt13bf9c82014-11-18 07:26:44 -08001101 .attr('stroke-dasharray', '3, 3')
1102 .style('opacity', 0.5)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001103 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -08001104 .duration(1500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001105 .attr({
Simon Hunt13bf9c82014-11-18 07:26:44 -08001106 'stroke-dasharray': '3, 12',
1107 stroke: config.topo.linkOutColor,
1108 'stroke-width': config.topo.linkOutWidth
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001109 })
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001110 .style('opacity', 0.0)
Thomas Vachuska4830d392014-11-09 17:09:56 -08001111 .remove();
Simon Hunte2575b62014-11-18 15:25:53 -08001112
1113 // NOTE: invoke a single tick to force the labels to position
1114 // onto their links.
1115 tick();
1116 }
1117
1118 function getLabelData() {
1119 // create the backing data for showing labels..
1120 var data = [];
1121 link.each(function (d) {
1122 if (d.label) {
1123 data.push({
1124 id: 'lab-' + d.key,
1125 key: d.key,
1126 label: d.label,
1127 ldata: d
1128 });
1129 }
1130 });
1131 return data;
1132 }
1133
1134 var linkLabelOffset = '0.3em';
1135
1136 function applyLinkLabels(data) {
1137 var entering;
1138
1139 linkLabel = linkLabelG.selectAll('.linkLabel')
1140 .data(data, function (d) { return d.id; });
1141
Simon Hunt56a2ea42014-11-19 12:39:31 -08001142 // for elements already existing, we need to update the text
1143 // and adjust the rectangle size to fit
1144 linkLabel.each(function (d) {
1145 var el = d3.select(this),
1146 rect = el.select('rect'),
1147 text = el.select('text');
1148 text.text(d.label);
1149 rect.attr(rectAroundText(el));
1150 });
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -08001151
Simon Hunte2575b62014-11-18 15:25:53 -08001152 entering = linkLabel.enter().append('g')
1153 .classed('linkLabel', true)
1154 .attr('id', function (d) { return d.id; });
1155
1156 entering.each(function (d) {
1157 var el = d3.select(this),
1158 rect,
1159 text,
1160 parms = {
1161 x1: d.ldata.x1,
1162 y1: d.ldata.y1,
1163 x2: d.ldata.x2,
1164 y2: d.ldata.y2
1165 };
1166
1167 d.el = el;
1168 rect = el.append('rect');
1169 text = el.append('text').text(d.label);
1170 rect.attr(rectAroundText(el));
1171 text.attr('dy', linkLabelOffset);
1172
1173 el.attr('transform', transformLabel(parms));
1174 });
1175
1176 // Remove any links that are no longer required.
1177 linkLabel.exit().remove();
1178 }
1179
1180 function rectAroundText(el) {
1181 var text = el.select('text'),
1182 box = text.node().getBBox();
1183
1184 // translate the bbox so that it is centered on [x,y]
1185 box.x = -box.width / 2;
1186 box.y = -box.height / 2;
1187
1188 // add padding
1189 box.x -= 1;
1190 box.width += 2;
1191 return box;
1192 }
1193
1194 function transformLabel(p) {
1195 var dx = p.x2 - p.x1,
1196 dy = p.y2 - p.y1,
1197 xMid = dx/2 + p.x1,
1198 yMid = dy/2 + p.y1;
1199 //length = Math.sqrt(dx*dx + dy*dy),
1200 //rads = Math.asin(dy/length),
1201 //degs = rads / (Math.PI*2) * 360;
1202
1203 return translate(xMid, yMid);
1204
1205 // TODO: consider making label parallel to line
1206 //return [
1207 // translate(xMid, yMid),
1208 // rotate(degs),
1209 // translate(0, 8)
1210 //].join('');
Simon Hunt99c13842014-11-06 18:23:12 -08001211 }
1212
1213 function createDeviceNode(device) {
1214 // start with the object as is
1215 var node = device,
Simon Huntbb282f52014-11-10 11:08:19 -08001216 type = device.type,
1217 svgCls = type ? 'node device ' + type : 'node device';
Simon Hunt99c13842014-11-06 18:23:12 -08001218
1219 // Augment as needed...
1220 node.class = 'device';
Simon Huntbb282f52014-11-10 11:08:19 -08001221 node.svgClass = device.online ? svgCls + ' online' : svgCls;
Simon Hunt99c13842014-11-06 18:23:12 -08001222 positionNode(node);
1223
1224 // cache label array length
1225 network.deviceLabelCount = device.labels.length;
Simon Hunt99c13842014-11-06 18:23:12 -08001226 return node;
1227 }
1228
Simon Hunt56d51852014-11-09 13:03:35 -08001229 function createHostNode(host) {
1230 // start with the object as is
1231 var node = host;
1232
1233 // Augment as needed...
1234 node.class = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001235 if (!node.type) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08001236 node.type = 'endstation';
1237 }
Simon Hunt7fa116d2014-11-17 14:16:55 -08001238 node.svgClass = 'node host ' + node.type;
Simon Hunt56d51852014-11-09 13:03:35 -08001239 positionNode(node);
1240
1241 // cache label array length
1242 network.hostLabelCount = host.labels.length;
Simon Hunt56d51852014-11-09 13:03:35 -08001243 return node;
1244 }
1245
Simon Hunt99c13842014-11-06 18:23:12 -08001246 function positionNode(node) {
1247 var meta = node.metaUi,
Simon Huntac9e24f2014-11-12 10:12:21 -08001248 x = meta && meta.x,
1249 y = meta && meta.y,
1250 xy;
Simon Hunt99c13842014-11-06 18:23:12 -08001251
Simon Huntac9e24f2014-11-12 10:12:21 -08001252 // If we have [x,y] already, use that...
Simon Hunt99c13842014-11-06 18:23:12 -08001253 if (x && y) {
1254 node.fixed = true;
Simon Huntac9e24f2014-11-12 10:12:21 -08001255 node.x = x;
1256 node.y = y;
1257 return;
Simon Hunt99c13842014-11-06 18:23:12 -08001258 }
Simon Huntac9e24f2014-11-12 10:12:21 -08001259
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001260 var location = node.location;
1261 if (location && location.type === 'latlng') {
1262 var coord = geoMapProjection([location.lng, location.lat]);
1263 node.fixed = true;
1264 node.x = coord[0];
1265 node.y = coord[1];
1266 return;
1267 }
1268
Simon Huntac9e24f2014-11-12 10:12:21 -08001269 // Note: Placing incoming unpinned nodes at exactly the same point
1270 // (center of the view) causes them to explode outwards when
1271 // the force layout kicks in. So, we spread them out a bit
1272 // initially, to provide a more serene layout convergence.
1273 // Additionally, if the node is a host, we place it near
1274 // the device it is connected to.
1275
1276 function spread(s) {
1277 return Math.floor((Math.random() * s) - s/2);
1278 }
1279
1280 function randDim(dim) {
1281 return dim / 2 + spread(dim * 0.7071);
1282 }
1283
1284 function rand() {
1285 return {
1286 x: randDim(network.view.width()),
1287 y: randDim(network.view.height())
1288 };
1289 }
1290
1291 function near(node) {
1292 var min = 12,
1293 dx = spread(12),
1294 dy = spread(12);
1295 return {
1296 x: node.x + min + dx,
1297 y: node.y + min + dy
1298 };
1299 }
1300
1301 function getDevice(cp) {
1302 var d = network.lookup[cp.device];
1303 return d || rand();
1304 }
1305
1306 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
1307 $.extend(node, xy);
Simon Hunt99c13842014-11-06 18:23:12 -08001308 }
1309
Thomas Vachuska89543292014-11-19 11:28:33 -08001310 function iconUrl(d) {
1311 return 'img/' + d.type + '.png';
Simon Hunt99c13842014-11-06 18:23:12 -08001312 }
1313
1314 // returns the newly computed bounding box of the rectangle
1315 function adjustRectToFitText(n) {
1316 var text = n.select('text'),
1317 box = text.node().getBBox(),
1318 lab = config.labels;
1319
1320 text.attr('text-anchor', 'middle')
1321 .attr('y', '-0.8em')
1322 .attr('x', lab.imgPad/2);
1323
1324 // translate the bbox so that it is centered on [x,y]
1325 box.x = -box.width / 2;
1326 box.y = -box.height / 2;
1327
1328 // add padding
1329 box.x -= (lab.padLR + lab.imgPad/2);
1330 box.width += lab.padLR * 2 + lab.imgPad;
1331 box.y -= lab.padTB;
1332 box.height += lab.padTB * 2;
1333
1334 return box;
1335 }
1336
Simon Hunt1a9eff92014-11-07 11:06:34 -08001337 function mkSvgClass(d) {
1338 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
1339 }
1340
Simon Hunt7cd48f32014-11-09 23:42:50 -08001341 function hostLabel(d) {
1342 var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0;
1343 return d.labels[idx];
1344 }
1345 function deviceLabel(d) {
1346 var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0;
1347 return d.labels[idx];
1348 }
1349 function niceLabel(label) {
1350 return (label && label.trim()) ? label : '.';
1351 }
1352
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001353 function updateDeviceLabel(d) {
1354 var label = niceLabel(deviceLabel(d)),
1355 node = d.el,
1356 box;
1357
1358 node.select('text')
1359 .text(label)
1360 .style('opacity', 0)
1361 .transition()
1362 .style('opacity', 1);
1363
1364 box = adjustRectToFitText(node);
1365
1366 node.select('rect')
1367 .transition()
1368 .attr(box);
1369
Simon Hunt56a2ea42014-11-19 12:39:31 -08001370 node.select('rect.iconUnderlay')
Thomas Vachuska89543292014-11-19 11:28:33 -08001371 .transition()
1372 .attr('x', box.x + config.icons.xoff)
1373 .attr('y', box.y + config.icons.yoff);
Simon Hunt56a2ea42014-11-19 12:39:31 -08001374
1375 node.select('image')
1376 .transition()
1377 .attr('x', box.x + config.icons.xoff + 2)
1378 .attr('y', box.y + config.icons.yoff + 2);
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001379 }
1380
1381 function updateHostLabel(d) {
1382 var label = hostLabel(d),
1383 host = d.el;
1384
1385 host.select('text').text(label);
1386 }
1387
Simon Huntbb282f52014-11-10 11:08:19 -08001388 function updateDeviceState(nodeData) {
1389 nodeData.el.classed('online', nodeData.online);
1390 updateDeviceLabel(nodeData);
1391 // TODO: review what else might need to be updated
1392 }
1393
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001394 function updateLinkState(linkData) {
1395 updateLinkWidth(linkData);
Thomas Vachuskabadb93f2014-11-15 23:51:17 -08001396 linkData.el.classed('inactive', !linkData.online);
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001397 // TODO: review what else might need to be updated
1398 // update label, if showing
1399 }
1400
Simon Huntbb282f52014-11-10 11:08:19 -08001401 function updateHostState(hostData) {
1402 updateHostLabel(hostData);
1403 // TODO: review what else might need to be updated
1404 }
1405
Simon Hunt6ac93f32014-11-13 12:17:27 -08001406 function nodeMouseOver(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001407 hovered = d;
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -08001408 if (trafficHover() && (d.class === 'host' || d.class === 'device')) {
Thomas Vachuska29617e52014-11-20 03:17:46 -08001409 showSelectTraffic();
1410 } else if (flowsHover() && (d.class === 'device')) {
1411 showDeviceLinkFlows();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001412 }
1413 }
1414
1415 function nodeMouseOut(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001416 hovered = null;
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -08001417 if (trafficHover() && (d.class === 'host' || d.class === 'device')) {
Thomas Vachuska29617e52014-11-20 03:17:46 -08001418 showSelectTraffic();
1419 } else if (flowsHover() && (d.class === 'device')) {
1420 showDeviceLinkFlows();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001421 }
1422 }
Simon Huntbb282f52014-11-10 11:08:19 -08001423
Thomas Vachuska89543292014-11-19 11:28:33 -08001424 function addHostIcon(node, radius, iconId) {
1425 var dim = radius * 1.5,
1426 xlate = -dim / 2;
1427
1428 node.append('svg:image')
1429 .attr('transform', translate(xlate,xlate))
1430 .attr('xlink:href', 'img/' + iconId + '.png')
1431 .attr('width', dim)
1432 .attr('height', dim);
1433 }
1434
Simon Hunt99c13842014-11-06 18:23:12 -08001435 function updateNodes() {
1436 node = nodeG.selectAll('.node')
1437 .data(network.nodes, function (d) { return d.id; });
1438
1439 // operate on existing nodes, if necessary
Simon Hunt7cd48f32014-11-09 23:42:50 -08001440 // update host labels
Simon Hunt99c13842014-11-06 18:23:12 -08001441 //node .foo() .bar() ...
1442
1443 // operate on entering nodes:
1444 var entering = node.enter()
1445 .append('g')
1446 .attr({
1447 id: function (d) { return safeId(d.id); },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001448 class: mkSvgClass,
Simon Hunt99c13842014-11-06 18:23:12 -08001449 transform: function (d) { return translate(d.x, d.y); },
1450 opacity: 0
1451 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08001452 .call(network.drag)
Simon Hunt6ac93f32014-11-13 12:17:27 -08001453 .on('mouseover', nodeMouseOver)
1454 .on('mouseout', nodeMouseOut)
Simon Hunt99c13842014-11-06 18:23:12 -08001455 .transition()
1456 .attr('opacity', 1);
1457
1458 // augment device nodes...
1459 entering.filter('.device').each(function (d) {
1460 var node = d3.select(this),
Thomas Vachuska89543292014-11-19 11:28:33 -08001461 icon = iconUrl(d),
Simon Hunt7cd48f32014-11-09 23:42:50 -08001462 label = niceLabel(deviceLabel(d)),
Simon Hunt99c13842014-11-06 18:23:12 -08001463 box;
1464
Simon Hunt7cd48f32014-11-09 23:42:50 -08001465 // provide ref to element from backing data....
1466 d.el = node;
1467
Simon Hunt99c13842014-11-06 18:23:12 -08001468 node.append('rect')
1469 .attr({
1470 'rx': 5,
1471 'ry': 5
1472 });
1473
1474 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001475 .text(label)
Simon Hunt99c13842014-11-06 18:23:12 -08001476 .attr('dy', '1.1em');
1477
1478 box = adjustRectToFitText(node);
1479
1480 node.select('rect')
1481 .attr(box);
1482
Thomas Vachuska89543292014-11-19 11:28:33 -08001483 if (icon) {
1484 var cfg = config.icons;
1485 node.append('rect')
Simon Hunt99c13842014-11-06 18:23:12 -08001486 .attr({
Simon Hunt56a2ea42014-11-19 12:39:31 -08001487 class: 'iconUnderlay',
Thomas Vachuska89543292014-11-19 11:28:33 -08001488 x: box.x + config.icons.xoff,
1489 y: box.y + config.icons.yoff,
1490 width: cfg.w,
1491 height: cfg.h,
1492 rx: 4
1493 }).style({
1494 stroke: '#000',
1495 fill: '#ddd'
1496 });
1497 node.append('svg:image')
1498 .attr({
1499 x: box.x + config.icons.xoff + 2,
1500 y: box.y + config.icons.yoff + 2,
1501 width: cfg.w - 4,
1502 height: cfg.h - 4,
1503 'xlink:href': icon
Simon Hunt99c13842014-11-06 18:23:12 -08001504 });
1505 }
1506
1507 // debug function to show the modelled x,y coordinates of nodes...
1508 if (debug('showNodeXY')) {
1509 node.select('rect').attr('fill-opacity', 0.5);
1510 node.append('circle')
1511 .attr({
1512 class: 'debug',
1513 cx: 0,
1514 cy: 0,
1515 r: '3px'
1516 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001517 }
1518 });
Simon Hunt934c3ce2014-11-05 11:45:07 -08001519
Thomas Vachuska89543292014-11-19 11:28:33 -08001520 // TODO: better place for this configuration state
1521 var defaultHostRadius = 9,
1522 hostRadius = {
1523 bgpSpeaker: 14,
1524 router: 14,
1525 host: 14
1526 },
1527 hostIcon = {
1528 bgpSpeaker: 'bgpSpeaker',
1529 router: 'router',
1530 host: 'host'
1531 };
1532
1533
Simon Hunt56d51852014-11-09 13:03:35 -08001534 // augment host nodes...
1535 entering.filter('.host').each(function (d) {
1536 var node = d3.select(this),
Thomas Vachuska89543292014-11-19 11:28:33 -08001537 r = hostRadius[d.type] || defaultHostRadius,
1538 textDy = r + 10,
1539 icon = hostIcon[d.type];
Simon Hunt56d51852014-11-09 13:03:35 -08001540
Simon Hunt7cd48f32014-11-09 23:42:50 -08001541 // provide ref to element from backing data....
1542 d.el = node;
1543
Thomas Vachuska89543292014-11-19 11:28:33 -08001544 node.append('circle')
1545 .attr('r', r);
Simon Hunt7fa116d2014-11-17 14:16:55 -08001546
Thomas Vachuska89543292014-11-19 11:28:33 -08001547 if (icon) {
1548 addHostIcon(node, r, icon);
Simon Hunt7fa116d2014-11-17 14:16:55 -08001549 }
Simon Hunt56d51852014-11-09 13:03:35 -08001550
Paul Greyson29cd58f2014-11-18 13:14:57 -08001551
Simon Hunt56d51852014-11-09 13:03:35 -08001552 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001553 .text(hostLabel)
Thomas Vachuska89543292014-11-19 11:28:33 -08001554 .attr('dy', textDy)
Simon Hunt7cd48f32014-11-09 23:42:50 -08001555 .attr('text-anchor', 'middle');
Simon Hunt56d51852014-11-09 13:03:35 -08001556
1557 // debug function to show the modelled x,y coordinates of nodes...
1558 if (debug('showNodeXY')) {
1559 node.select('circle').attr('fill-opacity', 0.5);
1560 node.append('circle')
1561 .attr({
1562 class: 'debug',
1563 cx: 0,
1564 cy: 0,
1565 r: '3px'
1566 });
1567 }
1568 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001569
Simon Hunt99c13842014-11-06 18:23:12 -08001570 // operate on both existing and new nodes, if necessary
1571 //node .foo() .bar() ...
Simon Huntc7ee0662014-11-05 16:44:37 -08001572
Simon Hunt99c13842014-11-06 18:23:12 -08001573 // operate on exiting nodes:
Simon Huntea80eb42014-11-11 13:46:57 -08001574 // Note that the node is removed after 2 seconds.
1575 // Sub element animations should be shorter than 2 seconds.
1576 var exiting = node.exit()
Simon Hunt44031102014-11-11 13:20:36 -08001577 .transition()
1578 .duration(2000)
Simon Huntea80eb42014-11-11 13:46:57 -08001579 .style('opacity', 0)
Simon Hunt99c13842014-11-06 18:23:12 -08001580 .remove();
Simon Huntea80eb42014-11-11 13:46:57 -08001581
1582 // host node exits....
1583 exiting.filter('.host').each(function (d) {
1584 var node = d3.select(this);
1585
1586 node.select('text')
1587 .style('opacity', 0.5)
1588 .transition()
1589 .duration(1000)
1590 .style('opacity', 0);
1591 // note, leave <g>.remove to remove this element
1592
Thomas Vachuska89543292014-11-19 11:28:33 -08001593 node.select('circle')
1594 .style('stroke-fill', '#555')
1595 .style('fill', '#888')
Simon Huntea80eb42014-11-11 13:46:57 -08001596 .style('opacity', 0.5)
1597 .transition()
1598 .duration(1500)
1599 .attr('r', 0);
1600 // note, leave <g>.remove to remove this element
1601
1602 });
1603
1604 // TODO: device node exits
Simon Huntc7ee0662014-11-05 16:44:37 -08001605 }
1606
Simon Hunt8257f4c2014-11-16 19:34:54 -08001607 function find(key, array) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001608 for (var idx = 0, n = array.length; idx < n; idx++) {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001609 if (array[idx].key === key) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001610 return idx;
1611 }
1612 }
1613 return -1;
1614 }
1615
1616 function removeLinkElement(linkData) {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001617 var idx = find(linkData.key, network.links),
1618 removed;
1619 if (idx >=0) {
1620 // remove from links array
1621 removed = network.links.splice(idx, 1);
1622 // remove from lookup cache
1623 delete network.lookup[removed[0].key];
1624 updateLinks();
1625 network.force.resume();
1626 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001627 }
Simon Huntc7ee0662014-11-05 16:44:37 -08001628
Simon Hunt44031102014-11-11 13:20:36 -08001629 function removeHostElement(hostData) {
1630 // first, remove associated hostLink...
1631 removeLinkElement(hostData.linkData);
1632
1633 // remove from lookup cache
1634 delete network.lookup[hostData.id];
1635 // remove from nodes array
1636 var idx = find(hostData.id, network.nodes);
1637 network.nodes.splice(idx, 1);
1638 // remove from SVG
1639 updateNodes();
1640 network.force.resume();
1641 }
1642
1643
Simon Huntc7ee0662014-11-05 16:44:37 -08001644 function tick() {
1645 node.attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001646 transform: function (d) { return translate(d.x, d.y); }
Simon Huntc7ee0662014-11-05 16:44:37 -08001647 });
1648
1649 link.attr({
1650 x1: function (d) { return d.source.x; },
1651 y1: function (d) { return d.source.y; },
1652 x2: function (d) { return d.target.x; },
1653 y2: function (d) { return d.target.y; }
1654 });
Simon Hunte2575b62014-11-18 15:25:53 -08001655
1656 linkLabel.each(function (d) {
1657 var el = d3.select(this);
1658 var lnk = findLinkById(d.key),
1659 parms = {
1660 x1: lnk.source.x,
1661 y1: lnk.source.y,
1662 x2: lnk.target.x,
1663 y2: lnk.target.y
1664 };
1665 el.attr('transform', transformLabel(parms));
1666 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001667 }
Simon Hunt934c3ce2014-11-05 11:45:07 -08001668
1669 // ==============================
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001670 // Web-Socket for live data
1671
1672 function webSockUrl() {
1673 return document.location.toString()
1674 .replace(/\#.*/, '')
1675 .replace('http://', 'ws://')
1676 .replace('https://', 'wss://')
1677 .replace('index2.html', config.webSockUrl);
1678 }
1679
1680 webSock = {
1681 ws : null,
1682
1683 connect : function() {
1684 webSock.ws = new WebSocket(webSockUrl());
1685
1686 webSock.ws.onopen = function() {
Simon Hunt0c6d4192014-11-12 12:07:10 -08001687 noWebSock(false);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001688 };
1689
1690 webSock.ws.onmessage = function(m) {
1691 if (m.data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001692 wsTraceRx(m.data);
Thomas Vachuskad472c6e2014-11-07 19:11:05 -08001693 handleServerEvent(JSON.parse(m.data));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001694 }
1695 };
1696
1697 webSock.ws.onclose = function(m) {
1698 webSock.ws = null;
Simon Hunt0c6d4192014-11-12 12:07:10 -08001699 noWebSock(true);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001700 };
1701 },
1702
1703 send : function(text) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001704 if (text != null) {
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001705 webSock._send(text);
1706 }
1707 },
1708
1709 _send : function(message) {
1710 if (webSock.ws) {
1711 webSock.ws.send(message);
Simon Hunta255a2c2014-11-13 22:29:35 -08001712 } else if (config.useLiveData) {
Simon Hunt56d51852014-11-09 13:03:35 -08001713 network.view.alert('no web socket open\n\n' + message);
Simon Hunta255a2c2014-11-13 22:29:35 -08001714 } else {
1715 console.log('WS Send: ' + JSON.stringify(message));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001716 }
1717 }
1718
1719 };
1720
Simon Hunt0c6d4192014-11-12 12:07:10 -08001721 function noWebSock(b) {
1722 mask.style('display',b ? 'block' : 'none');
1723 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001724
1725 function sendMessage(evType, payload) {
1726 var toSend = {
Simon Huntbb282f52014-11-10 11:08:19 -08001727 event: evType,
1728 sid: ++sid,
1729 payload: payload
1730 },
1731 asText = JSON.stringify(toSend);
1732 wsTraceTx(asText);
1733 webSock.send(asText);
Simon Huntc76ae892014-11-18 17:31:51 -08001734
1735 // Temporary measure for debugging UI behavior ...
1736 if (!config.useLiveData) {
1737 handleTestSend(toSend);
1738 }
Simon Huntbb282f52014-11-10 11:08:19 -08001739 }
1740
1741 function wsTraceTx(msg) {
1742 wsTrace('tx', msg);
1743 }
1744 function wsTraceRx(msg) {
1745 wsTrace('rx', msg);
1746 }
1747 function wsTrace(rxtx, msg) {
Simon Huntbb282f52014-11-10 11:08:19 -08001748 console.log('[' + rxtx + '] ' + msg);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001749 }
1750
Simon Huntc76ae892014-11-18 17:31:51 -08001751 // NOTE: Temporary hardcoded example for showing detail pane
1752 // while we fine-
1753 // Probably should not merge this change...
1754 function handleTestSend(msg) {
1755 if (msg.event === 'requestDetails') {
1756 showDetails({
1757 event: 'showDetails',
1758 sid: 1001,
1759 payload: {
1760 "id": "of:0000ffffffffff09",
1761 "type": "roadm",
1762 "propOrder": [
1763 "Name",
1764 "Vendor",
1765 "H/W Version",
1766 "S/W Version",
1767 "-",
1768 "Latitude",
1769 "Longitude",
1770 "Ports"
1771 ],
1772 "props": {
1773 "Name": null,
1774 "Vendor": "Linc",
1775 "H/W Version": "OE",
1776 "S/W Version": "?",
1777 "-": "",
1778 "Latitude": "40.8",
1779 "Longitude": "73.1",
1780 "Ports": "2"
1781 }
1782 }
1783 });
1784 }
1785 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001786
1787 // ==============================
1788 // Selection stuff
1789
1790 function selectObject(obj, el) {
1791 var n,
Simon Hunt01095ff2014-11-13 16:37:29 -08001792 srcEv = d3.event.sourceEvent,
1793 meta = srcEv.metaKey,
1794 shift = srcEv.shiftKey;
1795
Simon Huntdeab4322014-11-13 18:49:07 -08001796 if ((panZoom() && !meta) || (!panZoom() && meta)) {
Simon Hunt01095ff2014-11-13 16:37:29 -08001797 return;
1798 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001799
1800 if (el) {
1801 n = d3.select(el);
1802 } else {
1803 node.each(function(d) {
1804 if (d == obj) {
1805 n = d3.select(el = this);
1806 }
1807 });
1808 }
1809 if (!n) return;
1810
Simon Hunt01095ff2014-11-13 16:37:29 -08001811 if (shift && n.classed('selected')) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001812 deselectObject(obj.id);
Simon Hunt61d04042014-11-11 17:27:16 -08001813 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001814 return;
1815 }
1816
Simon Hunt01095ff2014-11-13 16:37:29 -08001817 if (!shift) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001818 deselectAll();
1819 }
1820
Simon Huntc31d5692014-11-12 13:27:18 -08001821 selections[obj.id] = { obj: obj, el: el };
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001822 selectOrder.push(obj.id);
1823
1824 n.classed('selected', true);
Simon Hunt61d04042014-11-11 17:27:16 -08001825 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001826 }
1827
1828 function deselectObject(id) {
Simon Huntc31d5692014-11-12 13:27:18 -08001829 var obj = selections[id],
1830 idx;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001831 if (obj) {
1832 d3.select(obj.el).classed('selected', false);
Simon Hunt61d04042014-11-11 17:27:16 -08001833 delete selections[id];
Simon Huntc31d5692014-11-12 13:27:18 -08001834 idx = $.inArray(id, selectOrder);
1835 if (idx >= 0) {
1836 selectOrder.splice(idx, 1);
1837 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001838 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001839 }
1840
1841 function deselectAll() {
1842 // deselect all nodes in the network...
1843 node.classed('selected', false);
1844 selections = {};
1845 selectOrder = [];
Simon Hunt61d04042014-11-11 17:27:16 -08001846 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001847 }
1848
Simon Hunt61d04042014-11-11 17:27:16 -08001849 // update the state of the detail pane, based on current selections
1850 function updateDetailPane() {
1851 var nSel = selectOrder.length;
1852 if (!nSel) {
1853 detailPane.hide();
Simon Huntd72bc702014-11-13 18:38:04 -08001854 showTrafficAction(); // sends cancelTraffic event
Simon Hunt61d04042014-11-11 17:27:16 -08001855 } else if (nSel === 1) {
1856 singleSelect();
1857 } else {
1858 multiSelect();
1859 }
1860 }
1861
1862 function singleSelect() {
1863 requestDetails();
Simon Huntb53e0682014-11-12 13:32:01 -08001864 // NOTE: detail pane will be shown from showDetails event callback
Simon Hunt61d04042014-11-11 17:27:16 -08001865 }
1866
1867 function multiSelect() {
Simon Huntb53e0682014-11-12 13:32:01 -08001868 populateMultiSelect();
Simon Huntb53e0682014-11-12 13:32:01 -08001869 }
1870
1871 function addSep(tbody) {
1872 var tr = tbody.append('tr');
1873 $('<hr>').appendTo(tr.append('td').attr('colspan', 2));
1874 }
1875
1876 function addProp(tbody, label, value) {
1877 var tr = tbody.append('tr');
1878
1879 tr.append('td')
1880 .attr('class', 'label')
1881 .text(label + ' :');
1882
1883 tr.append('td')
1884 .attr('class', 'value')
1885 .text(value);
1886 }
1887
1888 function populateMultiSelect() {
1889 detailPane.empty();
1890
1891 var title = detailPane.append("h2"),
1892 table = detailPane.append("table"),
1893 tbody = table.append("tbody");
1894
1895 title.text('Multi-Select...');
1896
1897 selectOrder.forEach(function (d, i) {
1898 addProp(tbody, i+1, d);
1899 });
Simon Huntd72bc702014-11-13 18:38:04 -08001900
1901 addMultiSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08001902 }
1903
1904 function populateDetails(data) {
1905 detailPane.empty();
1906
1907 var title = detailPane.append("h2"),
1908 table = detailPane.append("table"),
1909 tbody = table.append("tbody");
1910
1911 $('<img src="img/' + data.type + '.png">').appendTo(title);
1912 $('<span>').attr('class', 'icon').text(data.id).appendTo(title);
1913
1914 data.propOrder.forEach(function(p) {
1915 if (p === '-') {
1916 addSep(tbody);
1917 } else {
1918 addProp(tbody, p, data.props[p]);
1919 }
1920 });
Simon Huntd72bc702014-11-13 18:38:04 -08001921
1922 addSingleSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08001923 }
1924
Simon Huntd72bc702014-11-13 18:38:04 -08001925 function addSingleSelectActions() {
1926 detailPane.append('hr');
1927 // always want to allow 'show traffic'
Simon Hunta5e89142014-11-14 07:00:33 -08001928 addAction(detailPane, 'Show Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001929 }
1930
1931 function addMultiSelectActions() {
1932 detailPane.append('hr');
1933 // always want to allow 'show traffic'
Simon Hunta5e89142014-11-14 07:00:33 -08001934 addAction(detailPane, 'Show Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001935 // if exactly two hosts are selected, also want 'add host intent'
1936 if (nSel() === 2 && allSelectionsClass('host')) {
Simon Hunta5e89142014-11-14 07:00:33 -08001937 addAction(detailPane, 'Add Host Intent', addIntentAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001938 }
1939 }
1940
Simon Hunta5e89142014-11-14 07:00:33 -08001941 function addAction(panel, text, cb) {
1942 panel.append('div')
Simon Huntd72bc702014-11-13 18:38:04 -08001943 .classed('actionBtn', true)
1944 .text(text)
1945 .on('click', cb);
1946 }
1947
1948
Paul Greysonfcba0e82014-11-13 10:21:16 -08001949 function zoomPan(scale, translate) {
1950 zoomPanContainer.attr("transform", "translate(" + translate + ")scale(" + scale + ")");
1951 // keep the map lines constant width while zooming
Thomas Vachuska89543292014-11-19 11:28:33 -08001952 bgImg.style("stroke-width", 2.0 / scale + "px");
Paul Greysonfcba0e82014-11-13 10:21:16 -08001953 }
1954
1955 function resetZoomPan() {
1956 zoomPan(1, [0,0]);
1957 zoom.scale(1).translate([0,0]);
1958 }
1959
1960 function setupZoomPan() {
1961 function zoomed() {
Simon Huntdeab4322014-11-13 18:49:07 -08001962 if (!panZoom() ^ !d3.event.sourceEvent.metaKey) {
Paul Greysonfcba0e82014-11-13 10:21:16 -08001963 zoomPan(d3.event.scale, d3.event.translate);
1964 }
1965 }
1966
1967 zoom = d3.behavior.zoom()
1968 .translate([0, 0])
1969 .scale(1)
1970 .scaleExtent([1, 8])
1971 .on("zoom", zoomed);
1972
1973 svg.call(zoom);
1974 }
1975
Simon Hunt61d04042014-11-11 17:27:16 -08001976 // ==============================
1977 // Test harness code
Simon Hunt56d51852014-11-09 13:03:35 -08001978
1979 function prepareScenario(view, ctx, dbg) {
1980 var sc = scenario,
1981 urlSc = sc.evDir + ctx + sc.evScenario;
1982
1983 if (!ctx) {
1984 view.alert("No scenario specified (null ctx)");
1985 return;
1986 }
1987
1988 sc.view = view;
1989 sc.ctx = ctx;
1990 sc.debug = dbg;
1991 sc.evNumber = 0;
1992
1993 d3.json(urlSc, function(err, data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001994 var p = data && data.params || {},
1995 desc = data && data.description || null,
Simon Huntfc274c92014-11-11 11:05:46 -08001996 intro = data && data.title;
Simon Huntbb282f52014-11-10 11:08:19 -08001997
Simon Hunt56d51852014-11-09 13:03:35 -08001998 if (err) {
1999 view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err);
2000 } else {
2001 sc.params = p;
Simon Huntbb282f52014-11-10 11:08:19 -08002002 if (desc) {
2003 intro += '\n\n ' + desc.join('\n ');
2004 }
2005 view.alert(intro);
Simon Hunt56d51852014-11-09 13:03:35 -08002006 }
2007 });
2008
2009 }
2010
Simon Hunt01095ff2014-11-13 16:37:29 -08002011 // ==============================
2012 // Toggle Buttons in masthead
Simon Hunt0c6d4192014-11-12 12:07:10 -08002013
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002014 // TODO: toggle button (and other widgets in the masthead) should be provided
2015 // by the framework; not generated by the view.
2016
Simon Hunta5e89142014-11-14 07:00:33 -08002017 var showInstances,
2018 doPanZoom,
2019 showTrafficOnHover;
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002020
2021 function addButtonBar(view) {
2022 var bb = d3.select('#mast')
2023 .append('span').classed('right', true).attr('id', 'bb');
2024
Simon Hunta5e89142014-11-14 07:00:33 -08002025 function mkTogBtn(text, cb) {
2026 return bb.append('span')
2027 .classed('btn', true)
2028 .text(text)
2029 .on('click', cb);
2030 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002031
Simon Hunta5e89142014-11-14 07:00:33 -08002032 showInstances = mkTogBtn('Show Instances', toggleInst);
Simon Hunte5b71752014-11-18 20:06:07 -08002033 //doPanZoom = mkTogBtn('Pan/Zoom', togglePanZoom);
2034 //showTrafficOnHover = mkTogBtn('Show traffic on hover', toggleTrafficHover);
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002035 }
2036
Simon Hunta5e89142014-11-14 07:00:33 -08002037 function instShown() {
2038 return showInstances.classed('active');
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002039 }
Simon Hunta5e89142014-11-14 07:00:33 -08002040 function toggleInst() {
2041 showInstances.classed('active', !instShown());
2042 if (instShown()) {
2043 oiBox.show();
2044 } else {
2045 oiBox.hide();
2046 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002047 }
2048
Simon Huntdeab4322014-11-13 18:49:07 -08002049 function panZoom() {
Simon Hunte5b71752014-11-18 20:06:07 -08002050 return false;
2051 //return doPanZoom.classed('active');
Simon Hunt01095ff2014-11-13 16:37:29 -08002052 }
Simon Hunta5e89142014-11-14 07:00:33 -08002053 function togglePanZoom() {
2054 doPanZoom.classed('active', !panZoom());
2055 }
2056
2057 function trafficHover() {
Thomas Vachuska29617e52014-11-20 03:17:46 -08002058 return hoverModes[hoverMode] === 'intents';
Simon Hunta5e89142014-11-14 07:00:33 -08002059 }
Thomas Vachuska29617e52014-11-20 03:17:46 -08002060
2061 function flowsHover() {
2062 return hoverModes[hoverMode] === 'flows';
2063 }
2064
Simon Hunta5e89142014-11-14 07:00:33 -08002065 function toggleTrafficHover() {
2066 showTrafficOnHover.classed('active', !trafficHover());
2067 }
2068
Simon Hunt7fa116d2014-11-17 14:16:55 -08002069 function loadGlyphs(svg) {
2070 var defs = svg.append('defs');
2071 gly.defBird(defs);
2072 gly.defBullhorn(defs);
2073 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002074
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002075 // ==============================
Simon Hunt142d0032014-11-04 20:13:09 -08002076 // View life-cycle callbacks
Simon Hunt195cb382014-11-03 17:50:51 -08002077
Simon Huntf67722a2014-11-10 09:32:06 -08002078 function preload(view, ctx, flags) {
Simon Hunt142d0032014-11-04 20:13:09 -08002079 var w = view.width(),
2080 h = view.height(),
Simon Huntc7ee0662014-11-05 16:44:37 -08002081 fcfg = config.force,
2082 fpad = fcfg.pad,
2083 forceDim = [w - 2*fpad, h - 2*fpad];
Simon Hunt195cb382014-11-03 17:50:51 -08002084
Simon Hunt142d0032014-11-04 20:13:09 -08002085 // NOTE: view.$div is a D3 selection of the view's div
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002086 var viewBox = '0 0 ' + config.logicalSize + ' ' + config.logicalSize;
2087 svg = view.$div.append('svg').attr('viewBox', viewBox);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002088 setSize(svg, view);
2089
Simon Hunt7fa116d2014-11-17 14:16:55 -08002090 loadGlyphs(svg);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002091
Paul Greysonfcba0e82014-11-13 10:21:16 -08002092 zoomPanContainer = svg.append('g').attr('id', 'zoomPanContainer');
Paul Greysonfcba0e82014-11-13 10:21:16 -08002093 setupZoomPan();
2094
Simon Hunt1a9eff92014-11-07 11:06:34 -08002095 // add blue glow filter to svg layer
Paul Greysonfcba0e82014-11-13 10:21:16 -08002096 d3u.appendGlow(zoomPanContainer);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002097
Simon Huntc7ee0662014-11-05 16:44:37 -08002098 // group for the topology
Paul Greysonfcba0e82014-11-13 10:21:16 -08002099 topoG = zoomPanContainer.append('g')
Simon Huntd3b7d512014-11-12 15:48:41 -08002100 .attr('id', 'topo-G')
Simon Huntc7ee0662014-11-05 16:44:37 -08002101 .attr('transform', fcfg.translate());
2102
Simon Hunte2575b62014-11-18 15:25:53 -08002103 // subgroups for links, link labels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002104 linkG = topoG.append('g').attr('id', 'links');
Simon Hunte2575b62014-11-18 15:25:53 -08002105 linkLabelG = topoG.append('g').attr('id', 'linkLabels');
Simon Huntc7ee0662014-11-05 16:44:37 -08002106 nodeG = topoG.append('g').attr('id', 'nodes');
2107
Simon Hunte2575b62014-11-18 15:25:53 -08002108 // selection of links, linkLabels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002109 link = linkG.selectAll('.link');
Simon Hunte2575b62014-11-18 15:25:53 -08002110 linkLabel = linkLabelG.selectAll('.linkLabel');
Simon Huntc7ee0662014-11-05 16:44:37 -08002111 node = nodeG.selectAll('.node');
2112
Simon Hunt7cd48f32014-11-09 23:42:50 -08002113 function chrg(d) {
2114 return fcfg.charge[d.class] || -12000;
2115 }
Simon Hunt99c13842014-11-06 18:23:12 -08002116 function ldist(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002117 return fcfg.linkDistance[d.type] || 50;
Simon Hunt99c13842014-11-06 18:23:12 -08002118 }
2119 function lstrg(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002120 // 0.0 - 1.0
2121 return fcfg.linkStrength[d.type] || 1.0;
Simon Hunt99c13842014-11-06 18:23:12 -08002122 }
2123
Simon Hunt1a9eff92014-11-07 11:06:34 -08002124 function selectCb(d, self) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002125 selectObject(d, self);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002126 }
2127
2128 function atDragEnd(d, self) {
Simon Hunt56d51852014-11-09 13:03:35 -08002129 // once we've finished moving, pin the node in position
2130 d.fixed = true;
2131 d3.select(self).classed('fixed', true);
2132 if (config.useLiveData) {
Simon Hunt902c9922014-11-11 11:59:31 -08002133 sendUpdateMeta(d);
Simon Hunta255a2c2014-11-13 22:29:35 -08002134 } else {
2135 console.log('Moving node ' + d.id + ' to [' + d.x + ',' + d.y + ']');
Simon Hunt1a9eff92014-11-07 11:06:34 -08002136 }
2137 }
2138
Simon Hunt902c9922014-11-11 11:59:31 -08002139 function sendUpdateMeta(d) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002140 sendMessage('updateMeta', {
2141 id: d.id,
2142 'class': d.class,
Simon Hunt902c9922014-11-11 11:59:31 -08002143 'memento': {
Simon Hunt01095ff2014-11-13 16:37:29 -08002144 x: d.x,
2145 y: d.y
Simon Hunt902c9922014-11-11 11:59:31 -08002146 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002147 });
2148 }
2149
Simon Huntc7ee0662014-11-05 16:44:37 -08002150 // set up the force layout
2151 network.force = d3.layout.force()
2152 .size(forceDim)
2153 .nodes(network.nodes)
2154 .links(network.links)
Simon Hunt7cd48f32014-11-09 23:42:50 -08002155 .gravity(0.4)
2156 .friction(0.7)
2157 .charge(chrg)
Simon Hunt99c13842014-11-06 18:23:12 -08002158 .linkDistance(ldist)
2159 .linkStrength(lstrg)
Simon Huntc7ee0662014-11-05 16:44:37 -08002160 .on('tick', tick);
Simon Hunt195cb382014-11-03 17:50:51 -08002161
Simon Hunt01095ff2014-11-13 16:37:29 -08002162 network.drag = d3u.createDragBehavior(network.force,
Simon Huntdeab4322014-11-13 18:49:07 -08002163 selectCb, atDragEnd, panZoom);
Simon Hunt0c6d4192014-11-12 12:07:10 -08002164
2165 // create mask layer for when we lose connection to server.
Simon Hunta5e89142014-11-14 07:00:33 -08002166 // TODO: this should be part of the framework
Simon Hunt0c6d4192014-11-12 12:07:10 -08002167 mask = view.$div.append('div').attr('id','topo-mask');
2168 para(mask, 'Oops!');
2169 para(mask, 'Web-socket connection to server closed...');
2170 para(mask, 'Try refreshing the page.');
Simon Hunt12ce12e2014-11-15 21:13:19 -08002171
2172 mask.append('svg')
2173 .attr({
2174 id: 'mask-bird',
2175 width: w,
2176 height: h
2177 })
2178 .append('g')
2179 .attr('transform', birdTranslate(w, h))
2180 .style('opacity', 0.3)
2181 .append('use')
2182 .attr({
2183 'xlink:href': '#bird',
2184 width: config.birdDim,
2185 height: config.birdDim,
2186 fill: '#111'
Thomas Vachuska89543292014-11-19 11:28:33 -08002187 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08002188 }
Simon Hunt195cb382014-11-03 17:50:51 -08002189
Simon Hunt01095ff2014-11-13 16:37:29 -08002190 function para(sel, text) {
2191 sel.append('p').text(text);
2192 }
2193
2194
Simon Hunt56d51852014-11-09 13:03:35 -08002195 function load(view, ctx, flags) {
Simon Huntf67722a2014-11-10 09:32:06 -08002196 // resize, in case the window was resized while we were not loaded
2197 resize(view, ctx, flags);
2198
Simon Hunt99c13842014-11-06 18:23:12 -08002199 // cache the view token, so network topo functions can access it
2200 network.view = view;
Simon Hunt56d51852014-11-09 13:03:35 -08002201 config.useLiveData = !flags.local;
2202
2203 if (!config.useLiveData) {
2204 prepareScenario(view, ctx, flags.debug);
2205 }
Simon Hunt99c13842014-11-06 18:23:12 -08002206
2207 // set our radio buttons and key bindings
Simon Hunt9462e8c2014-11-14 17:28:09 -08002208 layerBtnSet = view.setRadio(layerButtons);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002209 view.setKeys(keyDispatch);
Simon Hunt195cb382014-11-03 17:50:51 -08002210
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002211 // patch in our "button bar" for now
2212 // TODO: implement a more official frameworky way of doing this..
2213 addButtonBar(view);
2214
Simon Huntd3b7d512014-11-12 15:48:41 -08002215 // Load map data asynchronously; complete startup after that..
2216 loadGeoJsonData();
Simon Hunta255a2c2014-11-13 22:29:35 -08002217
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002218 //var dashIdx = 0;
2219 //antTimer = setInterval(function () {
2220 // // TODO: figure out how to choose Src-->Dst and Dst-->Src, per link
2221 // dashIdx = dashIdx === 0 ? 14 : dashIdx - 2;
2222 // d3.selectAll('.animated').style('stroke-dashoffset', dashIdx);
2223 //}, 35);
Simon Hunta255a2c2014-11-13 22:29:35 -08002224 }
2225
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002226 function startAntTimer() {
2227 var pulses = [ 5, 3, 1.2, 3 ],
2228 pulse = 0;
2229 antTimer = setInterval(function () {
2230 pulse = pulse + 1;
2231 pulse = pulse === pulses.length ? 0 : pulse;
2232 d3.selectAll('.animated').style('stroke-width', pulses[pulse]);
2233 }, 200);
2234 }
2235
2236 function stopAntTimer() {
Simon Hunta255a2c2014-11-13 22:29:35 -08002237 if (antTimer) {
2238 clearInterval(antTimer);
2239 antTimer = null;
2240 }
Simon Huntd3b7d512014-11-12 15:48:41 -08002241 }
2242
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002243 function unload(view, ctx, flags) {
2244 stopAntTimer();
2245 }
2246
Simon Huntd3b7d512014-11-12 15:48:41 -08002247 // TODO: move these to config/state portion of script
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002248 var geoJsonUrl = 'json/map/continental_us.json', // TODO: Paul
Simon Huntd3b7d512014-11-12 15:48:41 -08002249 geoJson;
2250
2251 function loadGeoJsonData() {
2252 d3.json(geoJsonUrl, function (err, data) {
2253 if (err) {
2254 // fall back to USA map background
2255 loadStaticMap();
2256 } else {
2257 geoJson = data;
2258 loadGeoMap();
2259 }
2260
2261 // finally, connect to the server...
2262 if (config.useLiveData) {
2263 webSock.connect();
2264 }
2265 });
2266 }
2267
2268 function showBg() {
2269 return config.options.showBackground ? 'visible' : 'hidden';
2270 }
2271
2272 function loadStaticMap() {
2273 fnTrace('loadStaticMap', config.backgroundUrl);
2274 var w = network.view.width(),
2275 h = network.view.height();
2276
2277 // load the background image
2278 bgImg = svg.insert('svg:image', '#topo-G')
2279 .attr({
2280 id: 'topo-bg',
2281 width: w,
2282 height: h,
2283 'xlink:href': config.backgroundUrl
2284 })
2285 .style({
2286 visibility: showBg()
2287 });
2288 }
2289
2290 function loadGeoMap() {
2291 fnTrace('loadGeoMap', geoJsonUrl);
Simon Huntd3b7d512014-11-12 15:48:41 -08002292
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002293 // extracts the topojson data into geocoordinate-based geometry
2294 var topoData = topojson.feature(geoJson, geoJson.objects.states);
Simon Huntd3b7d512014-11-12 15:48:41 -08002295
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002296 // see: http://bl.ocks.org/mbostock/4707858
2297 geoMapProjection = d3.geo.mercator();
2298 var path = d3.geo.path().projection(geoMapProjection);
Simon Huntd3b7d512014-11-12 15:48:41 -08002299
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002300 geoMapProjection
2301 .scale(1)
2302 .translate([0, 0]);
Simon Huntd3b7d512014-11-12 15:48:41 -08002303
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002304 // [[x1,y1],[x2,y2]]
2305 var b = path.bounds(topoData);
Paul Greysonfcba0e82014-11-13 10:21:16 -08002306 // size map to 95% of minimum dimension to fill space
2307 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 -08002308 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 -08002309
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002310 geoMapProjection
2311 .scale(s)
2312 .translate(t);
2313
Paul Greysonfcba0e82014-11-13 10:21:16 -08002314 bgImg = zoomPanContainer.insert("g", '#topo-G');
Thomas Vachuska89543292014-11-19 11:28:33 -08002315 bgImg.attr('id', 'map').selectAll('path')
2316 .data(topoData.features)
2317 .enter()
2318 .append('path')
2319 .attr('d', path);
Simon Hunt195cb382014-11-03 17:50:51 -08002320 }
2321
Simon Huntf67722a2014-11-10 09:32:06 -08002322 function resize(view, ctx, flags) {
Simon Hunt12ce12e2014-11-15 21:13:19 -08002323 var w = view.width(),
2324 h = view.height();
2325
Simon Hunt934c3ce2014-11-05 11:45:07 -08002326 setSize(svg, view);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002327
2328 d3.select('#mask-bird').attr({ width: w, height: h})
2329 .select('g').attr('transform', birdTranslate(w, h));
Simon Hunt142d0032014-11-04 20:13:09 -08002330 }
2331
Simon Hunt12ce12e2014-11-15 21:13:19 -08002332 function birdTranslate(w, h) {
2333 var bdim = config.birdDim;
2334 return 'translate('+((w-bdim)*.4)+','+((h-bdim)*.1)+')';
2335 }
Simon Hunt142d0032014-11-04 20:13:09 -08002336
2337 // ==============================
2338 // View registration
Simon Hunt195cb382014-11-03 17:50:51 -08002339
Simon Hunt25248912014-11-04 11:25:48 -08002340 onos.ui.addView('topo', {
Simon Hunt142d0032014-11-04 20:13:09 -08002341 preload: preload,
2342 load: load,
Simon Hunta255a2c2014-11-13 22:29:35 -08002343 unload: unload,
Simon Hunt142d0032014-11-04 20:13:09 -08002344 resize: resize
Simon Hunt195cb382014-11-03 17:50:51 -08002345 });
2346
Simon Hunt61d04042014-11-11 17:27:16 -08002347 detailPane = onos.ui.addFloatingPanel('topo-detail');
Simon Hunta5e89142014-11-14 07:00:33 -08002348 oiBox = onos.ui.addFloatingPanel('topo-oibox', 'TL');
Simon Hunt61d04042014-11-11 17:27:16 -08002349
Simon Hunt195cb382014-11-03 17:50:51 -08002350}(ONOS));