blob: c32d5089f6d9897a5cd36ebc313aa35eeb75f1f5 [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 Hunt56d51852014-11-09 13:03:35 -0800178 deviceLabelIndex = 0,
179 hostLabelIndex = 0,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800180 selections = {},
Simon Hunta5e89142014-11-14 07:00:33 -0800181 selectOrder = [],
Simon Hunt6ac93f32014-11-13 12:17:27 -0800182 hovered = null,
Simon Hunta5e89142014-11-14 07:00:33 -0800183 detailPane,
Simon Hunta255a2c2014-11-13 22:29:35 -0800184 antTimer = null,
Simon Hunta5e89142014-11-14 07:00:33 -0800185 onosInstances = {},
186 onosOrder = [],
187 oiBox,
Simon Hunt9462e8c2014-11-14 17:28:09 -0800188 oiShowMaster = false,
Thomas Vachuska29617e52014-11-20 03:17:46 -0800189 hoverModes = [ 'none', 'intents', 'flows'],
190 hoverMode = 0,
Simon Hunt195cb382014-11-03 17:50:51 -0800191 portLabelsOn = false;
192
Simon Hunt934c3ce2014-11-05 11:45:07 -0800193 // D3 selections
194 var svg,
Paul Greysonfcba0e82014-11-13 10:21:16 -0800195 zoomPanContainer,
Simon Hunt934c3ce2014-11-05 11:45:07 -0800196 bgImg,
Simon Huntc7ee0662014-11-05 16:44:37 -0800197 topoG,
198 nodeG,
199 linkG,
Simon Hunte2575b62014-11-18 15:25:53 -0800200 linkLabelG,
Simon Huntc7ee0662014-11-05 16:44:37 -0800201 node,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800202 link,
Simon Hunte2575b62014-11-18 15:25:53 -0800203 linkLabel,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800204 mask;
Simon Hunt195cb382014-11-03 17:50:51 -0800205
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800206 // the projection for the map background
207 var geoMapProjection;
208
Paul Greysonfcba0e82014-11-13 10:21:16 -0800209 // the zoom function
210 var zoom;
211
Simon Hunt142d0032014-11-04 20:13:09 -0800212 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800213 // For Debugging / Development
Simon Hunt195cb382014-11-03 17:50:51 -0800214
Simon Hunt99c13842014-11-06 18:23:12 -0800215 function note(label, msg) {
216 console.log('NOTE: ' + label + ': ' + msg);
Simon Hunt195cb382014-11-03 17:50:51 -0800217 }
218
Simon Hunt99c13842014-11-06 18:23:12 -0800219 function debug(what) {
220 return config.debugOn && config.debug[what];
Simon Hunt934c3ce2014-11-05 11:45:07 -0800221 }
222
Simon Huntfc274c92014-11-11 11:05:46 -0800223 function fnTrace(msg, id) {
224 if (config.fnTrace) {
225 console.log('FN: ' + msg + ' [' + id + ']');
226 }
227 }
Simon Hunt99c13842014-11-06 18:23:12 -0800228
Simon Hunta5e89142014-11-14 07:00:33 -0800229 function evTrace(data) {
230 fnTrace(data.event, data.payload.id);
231 }
232
Simon Hunt934c3ce2014-11-05 11:45:07 -0800233 // ==============================
234 // Key Callbacks
235
Simon Hunt99c13842014-11-06 18:23:12 -0800236 function testMe(view) {
Simon Hunta3dd9572014-11-20 15:22:41 -0800237 //view.alert('Theme is ' + view.theme());
238 //view.flash('This is some text');
Simon Hunt99c13842014-11-06 18:23:12 -0800239 }
240
Simon Hunt56d51852014-11-09 13:03:35 -0800241 function abortIfLive() {
Simon Hunt50128c02014-11-08 13:36:15 -0800242 if (config.useLiveData) {
Simon Huntb53e0682014-11-12 13:32:01 -0800243 network.view.alert("Sorry, currently using live data..");
Simon Hunt56d51852014-11-09 13:03:35 -0800244 return true;
Simon Hunt50128c02014-11-08 13:36:15 -0800245 }
Simon Hunt56d51852014-11-09 13:03:35 -0800246 return false;
247 }
Simon Hunt50128c02014-11-08 13:36:15 -0800248
Simon Hunt56d51852014-11-09 13:03:35 -0800249 function testDebug(msg) {
250 if (scenario.debug) {
251 scenario.view.alert(msg);
252 }
253 }
Simon Hunt99c13842014-11-06 18:23:12 -0800254
Simon Hunt56d51852014-11-09 13:03:35 -0800255 function injectTestEvent(view) {
256 if (abortIfLive()) { return; }
257 var sc = scenario,
258 evn = ++sc.evNumber,
259 pfx = sc.evDir + sc.ctx + sc.evPrefix + evn,
260 onosUrl = pfx + sc.evOnos,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800261 uiUrl = pfx + sc.evUi,
262 stack = [
263 { url: onosUrl, cb: handleServerEvent },
264 { url: uiUrl, cb: handleUiEvent }
265 ];
266 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800267 }
268
Simon Hunt7cd48f32014-11-09 23:42:50 -0800269 function recurseFetchEvent(stack, evn) {
270 var v = scenario.view,
271 frame;
272 if (stack.length === 0) {
Simon Huntfc274c92014-11-11 11:05:46 -0800273 v.alert('Oops!\n\nNo event #' + evn + ' found.');
Simon Hunt7cd48f32014-11-09 23:42:50 -0800274 return;
275 }
276 frame = stack.shift();
277
278 d3.json(frame.url, function (err, data) {
Simon Hunt99c13842014-11-06 18:23:12 -0800279 if (err) {
Simon Hunt56d51852014-11-09 13:03:35 -0800280 if (err.status === 404) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800281 // if we didn't find the data, try the next stack frame
282 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800283 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800284 v.alert('non-404 error:\n\n' + frame.url + '\n\n' + err);
Simon Hunt56d51852014-11-09 13:03:35 -0800285 }
Simon Hunt99c13842014-11-06 18:23:12 -0800286 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800287 testDebug('loaded: ' + frame.url);
Simon Hunt1712ed82014-11-17 12:56:00 -0800288 wsTrace('test', JSON.stringify(data));
Simon Hunt7cd48f32014-11-09 23:42:50 -0800289 frame.cb(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800290 }
291 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800292
Simon Hunt56d51852014-11-09 13:03:35 -0800293 }
Simon Hunt50128c02014-11-08 13:36:15 -0800294
Simon Hunt56d51852014-11-09 13:03:35 -0800295 function handleUiEvent(data) {
Simon Huntbb282f52014-11-10 11:08:19 -0800296 scenario.view.alert('UI Tx: ' + data.event + '\n\n' +
297 JSON.stringify(data));
Simon Hunt56d51852014-11-09 13:03:35 -0800298 }
299
300 function injectStartupEvents(view) {
301 var last = scenario.params.lastAuto || 0;
302 if (abortIfLive()) { return; }
303
304 while (scenario.evNumber < last) {
Simon Hunt1a9eff92014-11-07 11:06:34 -0800305 injectTestEvent(view);
306 }
307 }
308
Simon Hunt934c3ce2014-11-05 11:45:07 -0800309 function toggleBg() {
310 var vis = bgImg.style('visibility');
311 bgImg.style('visibility', (vis === 'hidden') ? 'visible' : 'hidden');
312 }
313
Simon Hunt99c13842014-11-06 18:23:12 -0800314 function cycleLabels() {
Simon Huntbb282f52014-11-10 11:08:19 -0800315 deviceLabelIndex = (deviceLabelIndex === network.deviceLabelCount - 1)
316 ? 0 : deviceLabelIndex + 1;
Simon Hunt5f36d342014-11-08 21:33:14 -0800317
Simon Hunt99c13842014-11-06 18:23:12 -0800318 network.nodes.forEach(function (d) {
Simon Huntbb282f52014-11-10 11:08:19 -0800319 if (d.class === 'device') {
320 updateDeviceLabel(d);
321 }
Simon Hunt99c13842014-11-06 18:23:12 -0800322 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800323 }
324
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800325 function cycleHoverMode(view) {
Thomas Vachuska29617e52014-11-20 03:17:46 -0800326 hoverMode++;
327 if (hoverMode === hoverModes.length) {
328 hoverMode = 0;
329 }
Simon Hunta3dd9572014-11-20 15:22:41 -0800330 view.flash('Hover Mode: ' + hoverModes[hoverMode]);
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -0800331 }
332
Simon Hunt934c3ce2014-11-05 11:45:07 -0800333 function togglePorts(view) {
Simon Hunt50128c02014-11-08 13:36:15 -0800334 view.alert('togglePorts() callback')
Simon Hunt934c3ce2014-11-05 11:45:07 -0800335 }
336
Simon Hunt6ac93f32014-11-13 12:17:27 -0800337 function unpin() {
338 if (hovered) {
339 hovered.fixed = false;
340 hovered.el.classed('fixed', false);
341 network.force.resume();
342 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800343 }
344
Simon Hunt9462e8c2014-11-14 17:28:09 -0800345 function handleEscape(view) {
346 if (oiShowMaster) {
347 cancelAffinity();
348 } else {
349 deselectAll();
350 }
351 }
352
Simon Hunt934c3ce2014-11-05 11:45:07 -0800353 // ==============================
354 // Radio Button Callbacks
355
Simon Hunta5e89142014-11-14 07:00:33 -0800356 var layerLookup = {
357 host: {
358 endstation: 'pkt', // default, if host event does not define type
Thomas Vachuska89543292014-11-19 11:28:33 -0800359 router: 'pkt',
Simon Hunta5e89142014-11-14 07:00:33 -0800360 bgpSpeaker: 'pkt'
361 },
362 device: {
363 switch: 'pkt',
364 roadm: 'opt'
365 },
366 link: {
367 hostLink: 'pkt',
368 direct: 'pkt',
Simon Hunt8257f4c2014-11-16 19:34:54 -0800369 indirect: '',
370 tunnel: '',
Simon Hunta5e89142014-11-14 07:00:33 -0800371 optical: 'opt'
372 }
373 };
374
375 function inLayer(d, layer) {
Simon Hunt8257f4c2014-11-16 19:34:54 -0800376 var type = d.class === 'link' ? d.type() : d.type,
377 look = layerLookup[d.class],
378 lyr = look && look[type];
Simon Hunta5e89142014-11-14 07:00:33 -0800379 return lyr === layer;
380 }
381
382 function unsuppressLayer(which) {
383 node.each(function (d) {
384 var node = d.el;
385 if (inLayer(d, which)) {
386 node.classed('suppressed', false);
387 }
388 });
389
390 link.each(function (d) {
391 var link = d.el;
392 if (inLayer(d, which)) {
393 link.classed('suppressed', false);
394 }
395 });
396 }
397
Simon Hunt9462e8c2014-11-14 17:28:09 -0800398 function suppressLayers(b) {
399 node.classed('suppressed', b);
400 link.classed('suppressed', b);
Simon Hunt142d0032014-11-04 20:13:09 -0800401// d3.selectAll('svg .port').classed('inactive', false);
402// d3.selectAll('svg .portText').classed('inactive', false);
Simon Hunt195cb382014-11-03 17:50:51 -0800403 }
404
Simon Hunt9462e8c2014-11-14 17:28:09 -0800405 function showAllLayers() {
406 suppressLayers(false);
407 }
408
Simon Hunt195cb382014-11-03 17:50:51 -0800409 function showPacketLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800410 node.classed('suppressed', true);
411 link.classed('suppressed', true);
412 unsuppressLayer('pkt');
Simon Hunt195cb382014-11-03 17:50:51 -0800413 }
414
415 function showOpticalLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800416 node.classed('suppressed', true);
417 link.classed('suppressed', true);
418 unsuppressLayer('opt');
Simon Hunt195cb382014-11-03 17:50:51 -0800419 }
420
Simon Hunt9462e8c2014-11-14 17:28:09 -0800421 function restoreLayerState() {
422 layerBtnDispatch[layerBtnSet.selected()]();
423 }
424
Simon Hunt142d0032014-11-04 20:13:09 -0800425 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800426 // Private functions
427
Simon Hunt99c13842014-11-06 18:23:12 -0800428 function safeId(s) {
429 return s.replace(/[^a-z0-9]/gi, '-');
430 }
431
Simon Huntc7ee0662014-11-05 16:44:37 -0800432 // set the size of the given element to that of the view (reduced if padded)
433 function setSize(el, view, pad) {
434 var padding = pad ? pad * 2 : 0;
Simon Hunt934c3ce2014-11-05 11:45:07 -0800435 el.attr({
Simon Huntc7ee0662014-11-05 16:44:37 -0800436 width: view.width() - padding,
437 height: view.height() - padding
Simon Hunt934c3ce2014-11-05 11:45:07 -0800438 });
439 }
440
Simon Hunt8257f4c2014-11-16 19:34:54 -0800441 function makeNodeKey(d, what) {
442 var port = what + 'Port';
443 return d[what] + '/' + d[port];
444 }
445
446 function makeLinkKey(d, flipped) {
447 var one = flipped ? makeNodeKey(d, 'dst') : makeNodeKey(d, 'src'),
448 two = flipped ? makeNodeKey(d, 'src') : makeNodeKey(d, 'dst');
449 return one + '-' + two;
450 }
451
Simon Hunt269670f2014-11-17 16:17:43 -0800452 function findLinkById(id) {
453 // check to see if this is a reverse lookup, else default to given id
454 var key = network.revLinkToKey[id] || id;
455 return key && network.lookup[key];
456 }
457
Simon Hunt8257f4c2014-11-16 19:34:54 -0800458 function findLink(linkData, op) {
459 var key = makeLinkKey(linkData),
460 keyrev = makeLinkKey(linkData, 1),
461 link = network.lookup[key],
462 linkRev = network.lookup[keyrev],
463 result = {},
464 ldata = link || linkRev,
465 rawLink;
466
467 if (op === 'add') {
468 if (link) {
469 // trying to add a link that we already know about
470 result.ldata = link;
471 result.badLogic = 'addLink: link already added';
472
473 } else if (linkRev) {
474 // we found the reverse of the link to be added
475 result.ldata = linkRev;
476 if (linkRev.fromTarget) {
477 result.badLogic = 'addLink: link already added';
478 }
479 }
480 } else if (op === 'update') {
481 if (!ldata) {
482 result.badLogic = 'updateLink: link not found';
483 } else {
484 rawLink = link ? ldata.fromSource : ldata.fromTarget;
485 result.updateWith = function (data) {
486 $.extend(rawLink, data);
487 restyleLinkElement(ldata);
488 }
489 }
490 } else if (op === 'remove') {
491 if (!ldata) {
492 result.badLogic = 'removeLink: link not found';
493 } else {
494 rawLink = link ? ldata.fromSource : ldata.fromTarget;
495
496 if (!rawLink) {
497 result.badLogic = 'removeLink: link not found';
498
499 } else {
500 result.removeRawLink = function () {
501 if (link) {
502 // remove fromSource
503 ldata.fromSource = null;
504 if (ldata.fromTarget) {
505 // promote target into source position
506 ldata.fromSource = ldata.fromTarget;
507 ldata.fromTarget = null;
508 ldata.key = keyrev;
509 delete network.lookup[key];
510 network.lookup[keyrev] = ldata;
Simon Hunt269670f2014-11-17 16:17:43 -0800511 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800512 }
513 } else {
514 // remove fromTarget
515 ldata.fromTarget = null;
Simon Hunt269670f2014-11-17 16:17:43 -0800516 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800517 }
518 if (ldata.fromSource) {
519 restyleLinkElement(ldata);
520 } else {
521 removeLinkElement(ldata);
522 }
523 }
524 }
525 }
526 }
527 return result;
528 }
529
530 function addLinkUpdate(ldata, link) {
531 // add link event, but we already have the reverse link installed
532 ldata.fromTarget = link;
Simon Hunt269670f2014-11-17 16:17:43 -0800533 network.revLinkToKey[link.id] = ldata.key;
Simon Hunt8257f4c2014-11-16 19:34:54 -0800534 restyleLinkElement(ldata);
535 }
536
537 var allLinkTypes = 'direct indirect optical tunnel',
538 defaultLinkType = 'direct';
539
540 function restyleLinkElement(ldata) {
541 // this fn's job is to look at raw links and decide what svg classes
542 // need to be applied to the line element in the DOM
543 var el = ldata.el,
544 type = ldata.type(),
545 lw = ldata.linkWidth(),
546 online = ldata.online();
547
548 el.classed('link', true);
549 el.classed('inactive', !online);
550 el.classed(allLinkTypes, false);
551 if (type) {
552 el.classed(type, true);
553 }
554 el.transition()
555 .duration(1000)
Thomas Vachuska89543292014-11-19 11:28:33 -0800556 .attr('stroke-width', linkScale(lw))
557 .attr('stroke', config.topo.linkBaseColor);
Simon Hunt8257f4c2014-11-16 19:34:54 -0800558 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800559
Simon Hunt99c13842014-11-06 18:23:12 -0800560 // ==============================
561 // Event handlers for server-pushed events
562
Simon Huntbb282f52014-11-10 11:08:19 -0800563 function logicError(msg) {
564 // TODO, report logic error to server, via websock, so it can be logged
Simon Huntcb56cff2014-11-17 11:42:26 -0800565 //network.view.alert('Logic Error:\n\n' + msg);
Simon Huntfc274c92014-11-11 11:05:46 -0800566 console.warn(msg);
Simon Huntbb282f52014-11-10 11:08:19 -0800567 }
568
Simon Hunt99c13842014-11-06 18:23:12 -0800569 var eventDispatch = {
Simon Hunta5e89142014-11-14 07:00:33 -0800570 addInstance: addInstance,
Simon Hunt99c13842014-11-06 18:23:12 -0800571 addDevice: addDevice,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800572 addLink: addLink,
Simon Hunt56d51852014-11-09 13:03:35 -0800573 addHost: addHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800574
Simon Huntfcfb46c2014-11-19 12:53:38 -0800575 updateInstance: updateInstance,
Simon Huntbb282f52014-11-10 11:08:19 -0800576 updateDevice: updateDevice,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800577 updateLink: updateLink,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800578 updateHost: updateHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800579
Simon Huntd72bc702014-11-13 18:38:04 -0800580 removeInstance: stillToImplement,
Simon Huntbb282f52014-11-10 11:08:19 -0800581 removeDevice: stillToImplement,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800582 removeLink: removeLink,
Simon Hunt44031102014-11-11 13:20:36 -0800583 removeHost: removeHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800584
Simon Hunt61d04042014-11-11 17:27:16 -0800585 showDetails: showDetails,
Simon Huntb53e0682014-11-12 13:32:01 -0800586 showTraffic: showTraffic
Simon Hunt99c13842014-11-06 18:23:12 -0800587 };
588
Simon Hunta5e89142014-11-14 07:00:33 -0800589 function addInstance(data) {
590 evTrace(data);
591 var inst = data.payload,
592 id = inst.id;
593 if (onosInstances[id]) {
594 logicError('ONOS instance already added: ' + id);
595 return;
596 }
597 onosInstances[id] = inst;
598 onosOrder.push(inst);
599 updateInstances();
600 }
601
Simon Hunt99c13842014-11-06 18:23:12 -0800602 function addDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800603 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800604 var device = data.payload,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800605 nodeData = createDeviceNode(device);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800606 network.nodes.push(nodeData);
607 network.lookup[nodeData.id] = nodeData;
Simon Hunt99c13842014-11-06 18:23:12 -0800608 updateNodes();
609 network.force.start();
610 }
611
Simon Hunt99c13842014-11-06 18:23:12 -0800612 function addLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800613 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800614 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800615 result = findLink(link, 'add'),
616 bad = result.badLogic,
617 ldata = result.ldata;
618
619 if (bad) {
620 logicError(bad + ': ' + link.id);
621 return;
622 }
623
624 if (ldata) {
625 // we already have a backing store link for src/dst nodes
626 addLinkUpdate(ldata, link);
627 return;
628 }
629
630 // no backing store link yet
631 ldata = createLink(link);
632 if (ldata) {
633 network.links.push(ldata);
634 network.lookup[ldata.key] = ldata;
Simon Hunt99c13842014-11-06 18:23:12 -0800635 updateLinks();
636 network.force.start();
637 }
638 }
639
Simon Hunt56d51852014-11-09 13:03:35 -0800640 function addHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800641 evTrace(data);
Simon Hunt56d51852014-11-09 13:03:35 -0800642 var host = data.payload,
643 node = createHostNode(host),
644 lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800645 network.nodes.push(node);
646 network.lookup[host.id] = node;
647 updateNodes();
648
649 lnk = createHostLink(host);
650 if (lnk) {
Simon Hunt44031102014-11-11 13:20:36 -0800651 node.linkData = lnk; // cache ref on its host
Simon Hunt56d51852014-11-09 13:03:35 -0800652 network.links.push(lnk);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800653 network.lookup[host.ingress] = lnk;
654 network.lookup[host.egress] = lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800655 updateLinks();
656 }
657 network.force.start();
658 }
659
Simon Hunt44031102014-11-11 13:20:36 -0800660 // TODO: fold updateX(...) methods into one base method; remove duplication
Simon Hunt56a2ea42014-11-19 12:39:31 -0800661
662 function updateInstance(data) {
663 evTrace(data);
664 var inst = data.payload,
665 id = inst.id,
666 instData = onosInstances[id];
667 if (instData) {
668 $.extend(instData, inst);
669 updateInstances();
Simon Hunt56a2ea42014-11-19 12:39:31 -0800670 } else {
671 logicError('updateInstance lookup fail. ID = "' + id + '"');
672 }
673 }
674
Simon Huntbb282f52014-11-10 11:08:19 -0800675 function updateDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800676 evTrace(data);
Simon Huntbb282f52014-11-10 11:08:19 -0800677 var device = data.payload,
678 id = device.id,
679 nodeData = network.lookup[id];
680 if (nodeData) {
681 $.extend(nodeData, device);
682 updateDeviceState(nodeData);
683 } else {
684 logicError('updateDevice lookup fail. ID = "' + id + '"');
685 }
686 }
687
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800688 function updateLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800689 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800690 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800691 result = findLink(link, 'update'),
692 bad = result.badLogic;
693 if (bad) {
694 logicError(bad + ': ' + link.id);
695 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800696 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800697 result.updateWith(link);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800698 }
699
Simon Hunt7cd48f32014-11-09 23:42:50 -0800700 function updateHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800701 evTrace(data);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800702 var host = data.payload,
Simon Huntbb282f52014-11-10 11:08:19 -0800703 id = host.id,
704 hostData = network.lookup[id];
705 if (hostData) {
706 $.extend(hostData, host);
707 updateHostState(hostData);
708 } else {
709 logicError('updateHost lookup fail. ID = "' + id + '"');
710 }
Simon Hunt7cd48f32014-11-09 23:42:50 -0800711 }
712
Simon Hunt44031102014-11-11 13:20:36 -0800713 // TODO: fold removeX(...) methods into base method - remove dup code
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800714 function removeLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800715 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800716 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800717 result = findLink(link, 'remove'),
718 bad = result.badLogic;
719 if (bad) {
720 logicError(bad + ': ' + link.id);
721 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800722 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800723 result.removeRawLink();
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800724 }
725
Simon Hunt44031102014-11-11 13:20:36 -0800726 function removeHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800727 evTrace(data);
Simon Hunt44031102014-11-11 13:20:36 -0800728 var host = data.payload,
729 id = host.id,
730 hostData = network.lookup[id];
731 if (hostData) {
732 removeHostElement(hostData);
733 } else {
734 logicError('removeHost lookup fail. ID = "' + id + '"');
735 }
736 }
737
Simon Hunt61d04042014-11-11 17:27:16 -0800738 function showDetails(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800739 evTrace(data);
Simon Hunt61d04042014-11-11 17:27:16 -0800740 populateDetails(data.payload);
741 detailPane.show();
742 }
743
Simon Huntb53e0682014-11-12 13:32:01 -0800744 function showTraffic(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800745 evTrace(data);
Thomas Vachuska4731f122014-11-20 04:56:19 -0800746 var paths = data.payload.paths,
747 hasTraffic = false;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800748
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800749 // Revert any links hilighted previously.
Thomas Vachuska4731f122014-11-20 04:56:19 -0800750 link.style('stroke-width', null)
Thomas Vachuskaa3148a72014-11-19 21:38:35 -0800751 .classed('primary secondary animated optical', false);
Simon Hunte2575b62014-11-18 15:25:53 -0800752 // Remove all previous labels.
753 removeLinkLabels();
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800754
Simon Hunte2575b62014-11-18 15:25:53 -0800755 // Now hilight all links in the paths payload, and attach
756 // labels to them, if they are defined.
Simon Hunta255a2c2014-11-13 22:29:35 -0800757 paths.forEach(function (p) {
Simon Hunte2575b62014-11-18 15:25:53 -0800758 var n = p.links.length,
759 i,
760 ldata;
761
Thomas Vachuska4731f122014-11-20 04:56:19 -0800762 hasTraffic = hasTraffic || p.traffic;
Simon Hunte2575b62014-11-18 15:25:53 -0800763 for (i=0; i<n; i++) {
764 ldata = findLinkById(p.links[i]);
Thomas Vachuska4731f122014-11-20 04:56:19 -0800765 if (ldata && ldata.el) {
Simon Hunte2575b62014-11-18 15:25:53 -0800766 ldata.el.classed(p.class, true);
767 ldata.label = p.labels[i];
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800768 }
Simon Hunte2575b62014-11-18 15:25:53 -0800769 }
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800770 });
Thomas Vachuska4731f122014-11-20 04:56:19 -0800771
Simon Hunte2575b62014-11-18 15:25:53 -0800772 updateLinks();
Thomas Vachuska4731f122014-11-20 04:56:19 -0800773
774 if (hasTraffic && !antTimer) {
775 startAntTimer();
776 } else if (!hasTraffic && antTimer) {
777 stopAntTimer();
778 }
Simon Huntb53e0682014-11-12 13:32:01 -0800779 }
780
Simon Hunt56d51852014-11-09 13:03:35 -0800781 // ...............................
782
783 function stillToImplement(data) {
784 var p = data.payload;
785 note(data.event, p.id);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800786 network.view.alert('Not yet implemented: "' + data.event + '"');
Simon Hunt56d51852014-11-09 13:03:35 -0800787 }
Simon Hunt99c13842014-11-06 18:23:12 -0800788
789 function unknownEvent(data) {
Simon Hunt50128c02014-11-08 13:36:15 -0800790 network.view.alert('Unknown event type: "' + data.event + '"');
Simon Hunt99c13842014-11-06 18:23:12 -0800791 }
792
793 function handleServerEvent(data) {
794 var fn = eventDispatch[data.event] || unknownEvent;
795 fn(data);
796 }
797
798 // ==============================
Simon Hunt61d04042014-11-11 17:27:16 -0800799 // Out-going messages...
800
Simon Huntb53e0682014-11-12 13:32:01 -0800801 function userFeedback(msg) {
802 // for now, use the alert pane as is. Maybe different alert style in
803 // the future (centered on view; dismiss button?)
804 network.view.alert(msg);
805 }
806
807 function nSel() {
808 return selectOrder.length;
809 }
Simon Hunt61d04042014-11-11 17:27:16 -0800810 function getSel(idx) {
811 return selections[selectOrder[idx]];
812 }
Simon Huntb53e0682014-11-12 13:32:01 -0800813 function getSelId(idx) {
814 return getSel(idx).obj.id;
815 }
816 function allSelectionsClass(cls) {
817 for (var i=0, n=nSel(); i<n; i++) {
818 if (getSel(i).obj.class !== cls) {
819 return false;
820 }
821 }
822 return true;
823 }
Simon Hunt61d04042014-11-11 17:27:16 -0800824
Simon Hunt61d04042014-11-11 17:27:16 -0800825 // request details for the selected element
Simon Huntd72bc702014-11-13 18:38:04 -0800826 // invoked from selection of a single node.
Simon Hunt61d04042014-11-11 17:27:16 -0800827 function requestDetails() {
828 var data = getSel(0).obj,
829 payload = {
830 id: data.id,
831 class: data.class
832 };
833 sendMessage('requestDetails', payload);
834 }
835
Simon Huntd72bc702014-11-13 18:38:04 -0800836 function addIntentAction() {
837 sendMessage('addHostIntent', {
838 one: getSelId(0),
Thomas Vachuska82f2c622014-11-17 12:23:18 -0800839 two: getSelId(1),
840 ids: [ getSelId(0), getSelId(1) ]
Simon Huntd72bc702014-11-13 18:38:04 -0800841 });
842 }
843
844 function showTrafficAction() {
Thomas Vachuska29617e52014-11-20 03:17:46 -0800845 // force intents hover mode
846 hoverMode = 1;
847 showSelectTraffic();
848 }
849
850 function showSelectTraffic() {
Simon Huntd72bc702014-11-13 18:38:04 -0800851 // if nothing is hovered over, and nothing selected, send cancel request
852 if (!hovered && nSel() === 0) {
853 sendMessage('cancelTraffic', {});
854 return;
855 }
856
857 // NOTE: hover is only populated if "show traffic on hover" is
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800858 // toggled on, and the item hovered is a host or a device...
859 var hoverId = (trafficHover() && hovered &&
860 (hovered.class === 'host' || hovered.class === 'device'))
Simon Huntd72bc702014-11-13 18:38:04 -0800861 ? hovered.id : '';
862 sendMessage('requestTraffic', {
863 ids: selectOrder,
864 hover: hoverId
865 });
866 }
867
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800868 function showAllTrafficAction() {
869 sendMessage('requestAllTraffic', {});
870 }
871
Thomas Vachuska29617e52014-11-20 03:17:46 -0800872 function showDeviceLinkFlowsAction() {
873 // force intents hover mode
874 hoverMode = 2;
875 showDeviceLinkFlows();
876 }
877
878 function showDeviceLinkFlows() {
879 // if nothing is hovered over, and nothing selected, send cancel request
880 if (!hovered && nSel() === 0) {
881 sendMessage('cancelTraffic', {});
882 return;
883 }
884 var hoverId = (flowsHover() && hovered && hovered.class === 'device') ?
885 hovered.id : '';
886 sendMessage('requestDeviceLinkFlows', {
887 ids: selectOrder,
888 hover: hoverId
889 });
890 }
Simon Huntd72bc702014-11-13 18:38:04 -0800891
Simon Hunta6a9fe72014-11-20 11:17:12 -0800892 // TODO: these should be moved out to utility module.
Simon Hunt95908012014-11-20 10:20:26 -0800893 function stripPx(s) {
894 return s.replace(/px$/,'');
895 }
Simon Hunta6a9fe72014-11-20 11:17:12 -0800896
897 function appendGlyph(svg, ox, oy, dim, iid) {
898 svg.append('use').attr({
899 class: 'glyphIcon',
900 transform: translate(ox,oy),
901 'xlink:href': iid,
902 width: dim,
903 height: dim
904
905 });
906 }
907
Simon Hunt61d04042014-11-11 17:27:16 -0800908 // ==============================
Simon Hunta5e89142014-11-14 07:00:33 -0800909 // onos instance panel functions
910
911 function updateInstances() {
912 var onoses = oiBox.el.selectAll('.onosInst')
913 .data(onosOrder, function (d) { return d.id; });
914
915 // operate on existing onoses if necessary
Simon Huntfcfb46c2014-11-19 12:53:38 -0800916 onoses.classed('online', function (d) { return d.online; });
Simon Hunta5e89142014-11-14 07:00:33 -0800917
918 var entering = onoses.enter()
919 .append('div')
920 .attr('class', 'onosInst')
921 .classed('online', function (d) { return d.online; })
Simon Hunt9c15eca2014-11-15 18:37:59 -0800922 .on('click', clickInst);
923
924 entering.each(function (d, i) {
925 var el = d3.select(this),
926 img;
Simon Hunt95908012014-11-20 10:20:26 -0800927 var css = window.getComputedStyle(this),
928 w = stripPx(css.width),
929 h = stripPx(css.height) / 2;
Simon Hunt9c15eca2014-11-15 18:37:59 -0800930
Simon Hunt95908012014-11-20 10:20:26 -0800931 var svg = el.append('svg').attr({
932 width: w,
933 height: h
934 });
935 var dim = 30;
Simon Hunta6a9fe72014-11-20 11:17:12 -0800936 appendGlyph(svg, 2, 2, 30, '#node');
Simon Hunt9c15eca2014-11-15 18:37:59 -0800937
938 $('<div>').attr('class', 'onosTitle').text(d.id).appendTo(el);
939
940 // is the UI attached to this instance?
941 // TODO: need uiAttached boolean in instance data
Simon Hunta6a9fe72014-11-20 11:17:12 -0800942 // TODO: use SVG glyph, not png..
Simon Hunt9c15eca2014-11-15 18:37:59 -0800943 //if (d.uiAttached) {
944 if (i === 0) {
945 $('<img src="img/ui.png">').attr('class','ui').appendTo(el);
946 }
947 });
Simon Hunta5e89142014-11-14 07:00:33 -0800948
949 // operate on existing + new onoses here
950
951 // the departed...
952 var exiting = onoses.exit()
953 .transition()
954 .style('opacity', 0)
955 .remove();
956 }
957
Simon Hunt9462e8c2014-11-14 17:28:09 -0800958 function clickInst(d) {
959 var el = d3.select(this),
960 aff = el.classed('affinity');
961 if (!aff) {
962 setAffinity(el, d);
963 } else {
964 cancelAffinity();
965 }
966 }
967
968 function setAffinity(el, d) {
969 d3.selectAll('.onosInst')
970 .classed('mastership', true)
971 .classed('affinity', false);
972 el.classed('affinity', true);
973
974 suppressLayers(true);
975 node.each(function (n) {
976 if (n.master === d.id) {
977 n.el.classed('suppressed', false);
978 }
979 });
980 oiShowMaster = true;
981 }
982
983 function cancelAffinity() {
984 d3.selectAll('.onosInst')
985 .classed('mastership affinity', false);
986 restoreLayerState();
987 oiShowMaster = false;
988 }
989
Simon Hunta5e89142014-11-14 07:00:33 -0800990 // ==============================
Simon Hunt99c13842014-11-06 18:23:12 -0800991 // force layout modification functions
992
993 function translate(x, y) {
994 return 'translate(' + x + ',' + y + ')';
995 }
996
Simon Hunte2575b62014-11-18 15:25:53 -0800997 function rotate(deg) {
998 return 'rotate(' + deg + ')';
999 }
1000
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001001 function missMsg(what, id) {
1002 return '\n[' + what + '] "' + id + '" missing ';
1003 }
1004
1005 function linkEndPoints(srcId, dstId) {
1006 var srcNode = network.lookup[srcId],
1007 dstNode = network.lookup[dstId],
1008 sMiss = !srcNode ? missMsg('src', srcId) : '',
1009 dMiss = !dstNode ? missMsg('dst', dstId) : '';
1010
1011 if (sMiss || dMiss) {
1012 logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
1013 return null;
1014 }
1015 return {
1016 source: srcNode,
1017 target: dstNode,
1018 x1: srcNode.x,
1019 y1: srcNode.y,
1020 x2: dstNode.x,
1021 y2: dstNode.y
1022 };
1023 }
1024
Simon Hunt56d51852014-11-09 13:03:35 -08001025 function createHostLink(host) {
1026 var src = host.id,
1027 dst = host.cp.device,
Simon Hunt7cd48f32014-11-09 23:42:50 -08001028 id = host.ingress,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001029 lnk = linkEndPoints(src, dst);
Simon Hunt56d51852014-11-09 13:03:35 -08001030
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001031 if (!lnk) {
Simon Hunt56d51852014-11-09 13:03:35 -08001032 return null;
1033 }
1034
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001035 // Synthesize link ...
1036 $.extend(lnk, {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001037 key: id,
Simon Hunt56d51852014-11-09 13:03:35 -08001038 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -08001039
1040 type: function () { return 'hostLink'; },
1041 // TODO: ideally, we should see if our edge switch is online...
1042 online: function () { return true; },
1043 linkWidth: function () { return 1; }
Simon Hunt7cd48f32014-11-09 23:42:50 -08001044 });
Simon Hunt99c13842014-11-06 18:23:12 -08001045 return lnk;
1046 }
1047
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001048 function createLink(link) {
Simon Hunta6a9fe72014-11-20 11:17:12 -08001049 var lnk = linkEndPoints(link.src, link.dst);
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001050
1051 if (!lnk) {
1052 return null;
1053 }
1054
Simon Hunt8257f4c2014-11-16 19:34:54 -08001055 $.extend(lnk, {
1056 key: link.id,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001057 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -08001058 fromSource: link,
1059
1060 // functions to aggregate dual link state
1061 type: function () {
1062 var s = lnk.fromSource,
1063 t = lnk.fromTarget;
1064 return (s && s.type) || (t && t.type) || defaultLinkType;
1065 },
1066 online: function () {
1067 var s = lnk.fromSource,
1068 t = lnk.fromTarget;
1069 return (s && s.online) || (t && t.online);
1070 },
1071 linkWidth: function () {
1072 var s = lnk.fromSource,
1073 t = lnk.fromTarget,
1074 ws = (s && s.linkWidth) || 0,
1075 wt = (t && t.linkWidth) || 0;
1076 return Math.max(ws, wt);
1077 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001078 });
1079 return lnk;
Simon Hunt1a9eff92014-11-07 11:06:34 -08001080 }
1081
Simon Hunte2575b62014-11-18 15:25:53 -08001082 function removeLinkLabels() {
1083 network.links.forEach(function (d) {
1084 d.label = '';
1085 });
1086 }
1087
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001088 var widthRatio = 1.4,
1089 linkScale = d3.scale.linear()
1090 .domain([1, 12])
1091 .range([widthRatio, 12 * widthRatio])
1092 .clamp(true);
1093
Simon Hunt99c13842014-11-06 18:23:12 -08001094 function updateLinks() {
1095 link = linkG.selectAll('.link')
Simon Hunt8257f4c2014-11-16 19:34:54 -08001096 .data(network.links, function (d) { return d.key; });
Simon Hunt99c13842014-11-06 18:23:12 -08001097
1098 // operate on existing links, if necessary
1099 // link .foo() .bar() ...
1100
1101 // operate on entering links:
1102 var entering = link.enter()
1103 .append('line')
1104 .attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001105 x1: function (d) { return d.x1; },
1106 y1: function (d) { return d.y1; },
1107 x2: function (d) { return d.x2; },
1108 y2: function (d) { return d.y2; },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001109 stroke: config.topo.linkInColor,
1110 'stroke-width': config.topo.linkInWidth
Simon Hunt99c13842014-11-06 18:23:12 -08001111 });
1112
1113 // augment links
Simon Hunt7cd48f32014-11-09 23:42:50 -08001114 entering.each(function (d) {
1115 var link = d3.select(this);
1116 // provide ref to element selection from backing data....
1117 d.el = link;
Simon Hunt8257f4c2014-11-16 19:34:54 -08001118 restyleLinkElement(d);
Simon Hunt7cd48f32014-11-09 23:42:50 -08001119 });
Thomas Vachuska4830d392014-11-09 17:09:56 -08001120
1121 // operate on both existing and new links, if necessary
1122 //link .foo() .bar() ...
1123
Simon Hunte2575b62014-11-18 15:25:53 -08001124 // apply or remove labels
1125 var labelData = getLabelData();
1126 applyLinkLabels(labelData);
1127
Thomas Vachuska4830d392014-11-09 17:09:56 -08001128 // operate on exiting links:
Thomas Vachuska4830d392014-11-09 17:09:56 -08001129 link.exit()
Simon Hunt13bf9c82014-11-18 07:26:44 -08001130 .attr('stroke-dasharray', '3, 3')
1131 .style('opacity', 0.5)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001132 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -08001133 .duration(1500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001134 .attr({
Simon Hunt13bf9c82014-11-18 07:26:44 -08001135 'stroke-dasharray': '3, 12',
1136 stroke: config.topo.linkOutColor,
1137 'stroke-width': config.topo.linkOutWidth
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001138 })
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001139 .style('opacity', 0.0)
Thomas Vachuska4830d392014-11-09 17:09:56 -08001140 .remove();
Simon Hunte2575b62014-11-18 15:25:53 -08001141
1142 // NOTE: invoke a single tick to force the labels to position
1143 // onto their links.
1144 tick();
1145 }
1146
1147 function getLabelData() {
1148 // create the backing data for showing labels..
1149 var data = [];
1150 link.each(function (d) {
1151 if (d.label) {
1152 data.push({
1153 id: 'lab-' + d.key,
1154 key: d.key,
1155 label: d.label,
1156 ldata: d
1157 });
1158 }
1159 });
1160 return data;
1161 }
1162
1163 var linkLabelOffset = '0.3em';
1164
1165 function applyLinkLabels(data) {
1166 var entering;
1167
1168 linkLabel = linkLabelG.selectAll('.linkLabel')
1169 .data(data, function (d) { return d.id; });
1170
Simon Hunt56a2ea42014-11-19 12:39:31 -08001171 // for elements already existing, we need to update the text
1172 // and adjust the rectangle size to fit
1173 linkLabel.each(function (d) {
1174 var el = d3.select(this),
1175 rect = el.select('rect'),
1176 text = el.select('text');
1177 text.text(d.label);
1178 rect.attr(rectAroundText(el));
1179 });
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -08001180
Simon Hunte2575b62014-11-18 15:25:53 -08001181 entering = linkLabel.enter().append('g')
1182 .classed('linkLabel', true)
1183 .attr('id', function (d) { return d.id; });
1184
1185 entering.each(function (d) {
1186 var el = d3.select(this),
1187 rect,
1188 text,
1189 parms = {
1190 x1: d.ldata.x1,
1191 y1: d.ldata.y1,
1192 x2: d.ldata.x2,
1193 y2: d.ldata.y2
1194 };
1195
1196 d.el = el;
1197 rect = el.append('rect');
1198 text = el.append('text').text(d.label);
1199 rect.attr(rectAroundText(el));
1200 text.attr('dy', linkLabelOffset);
1201
1202 el.attr('transform', transformLabel(parms));
1203 });
1204
1205 // Remove any links that are no longer required.
1206 linkLabel.exit().remove();
1207 }
1208
1209 function rectAroundText(el) {
1210 var text = el.select('text'),
1211 box = text.node().getBBox();
1212
1213 // translate the bbox so that it is centered on [x,y]
1214 box.x = -box.width / 2;
1215 box.y = -box.height / 2;
1216
1217 // add padding
1218 box.x -= 1;
1219 box.width += 2;
1220 return box;
1221 }
1222
1223 function transformLabel(p) {
1224 var dx = p.x2 - p.x1,
1225 dy = p.y2 - p.y1,
1226 xMid = dx/2 + p.x1,
1227 yMid = dy/2 + p.y1;
Simon Hunte2575b62014-11-18 15:25:53 -08001228 return translate(xMid, yMid);
Simon Hunt99c13842014-11-06 18:23:12 -08001229 }
1230
1231 function createDeviceNode(device) {
1232 // start with the object as is
1233 var node = device,
Simon Huntbb282f52014-11-10 11:08:19 -08001234 type = device.type,
Simon Huntc72967b2014-11-20 09:21:42 -08001235 svgCls = type ? 'node device ' + type : 'node device',
1236 labels = device.labels || [];
1237
1238 labels.unshift(''); // add 'no-label' to front of cycle
Simon Hunt99c13842014-11-06 18:23:12 -08001239
1240 // Augment as needed...
1241 node.class = 'device';
Simon Huntbb282f52014-11-10 11:08:19 -08001242 node.svgClass = device.online ? svgCls + ' online' : svgCls;
Simon Hunt99c13842014-11-06 18:23:12 -08001243 positionNode(node);
1244
1245 // cache label array length
Simon Huntc72967b2014-11-20 09:21:42 -08001246 // TODO: need a uiConfig event from the server to set things
1247 // like device labels count, host labels count, etc.
1248 // The current method (here) is a little fragile
Simon Hunt99c13842014-11-06 18:23:12 -08001249 network.deviceLabelCount = device.labels.length;
Simon Hunt99c13842014-11-06 18:23:12 -08001250 return node;
1251 }
1252
Simon Hunt56d51852014-11-09 13:03:35 -08001253 function createHostNode(host) {
1254 // start with the object as is
1255 var node = host;
1256
1257 // Augment as needed...
1258 node.class = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001259 if (!node.type) {
Simon Hunteb1514d2014-11-20 09:57:29 -08001260 node.type = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001261 }
Simon Hunt7fa116d2014-11-17 14:16:55 -08001262 node.svgClass = 'node host ' + node.type;
Simon Hunt56d51852014-11-09 13:03:35 -08001263 positionNode(node);
1264
1265 // cache label array length
1266 network.hostLabelCount = host.labels.length;
Simon Hunt56d51852014-11-09 13:03:35 -08001267 return node;
1268 }
1269
Simon Hunt99c13842014-11-06 18:23:12 -08001270 function positionNode(node) {
1271 var meta = node.metaUi,
Simon Huntac9e24f2014-11-12 10:12:21 -08001272 x = meta && meta.x,
1273 y = meta && meta.y,
1274 xy;
Simon Hunt99c13842014-11-06 18:23:12 -08001275
Simon Huntac9e24f2014-11-12 10:12:21 -08001276 // If we have [x,y] already, use that...
Simon Hunt99c13842014-11-06 18:23:12 -08001277 if (x && y) {
1278 node.fixed = true;
Simon Huntac9e24f2014-11-12 10:12:21 -08001279 node.x = x;
1280 node.y = y;
1281 return;
Simon Hunt99c13842014-11-06 18:23:12 -08001282 }
Simon Huntac9e24f2014-11-12 10:12:21 -08001283
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001284 var location = node.location;
1285 if (location && location.type === 'latlng') {
1286 var coord = geoMapProjection([location.lng, location.lat]);
1287 node.fixed = true;
1288 node.x = coord[0];
1289 node.y = coord[1];
1290 return;
1291 }
1292
Simon Huntac9e24f2014-11-12 10:12:21 -08001293 // Note: Placing incoming unpinned nodes at exactly the same point
1294 // (center of the view) causes them to explode outwards when
1295 // the force layout kicks in. So, we spread them out a bit
1296 // initially, to provide a more serene layout convergence.
1297 // Additionally, if the node is a host, we place it near
1298 // the device it is connected to.
1299
1300 function spread(s) {
1301 return Math.floor((Math.random() * s) - s/2);
1302 }
1303
1304 function randDim(dim) {
1305 return dim / 2 + spread(dim * 0.7071);
1306 }
1307
1308 function rand() {
1309 return {
1310 x: randDim(network.view.width()),
1311 y: randDim(network.view.height())
1312 };
1313 }
1314
1315 function near(node) {
1316 var min = 12,
1317 dx = spread(12),
1318 dy = spread(12);
1319 return {
1320 x: node.x + min + dx,
1321 y: node.y + min + dy
1322 };
1323 }
1324
1325 function getDevice(cp) {
1326 var d = network.lookup[cp.device];
1327 return d || rand();
1328 }
1329
1330 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
1331 $.extend(node, xy);
Simon Hunt99c13842014-11-06 18:23:12 -08001332 }
1333
Thomas Vachuska89543292014-11-19 11:28:33 -08001334 function iconUrl(d) {
1335 return 'img/' + d.type + '.png';
Simon Hunt99c13842014-11-06 18:23:12 -08001336 }
1337
Simon Huntc72967b2014-11-20 09:21:42 -08001338 function iconGlyphUrl(d) {
1339 var which = d.type || 'unknown';
1340 return '#' + which;
1341 }
1342
Simon Hunt99c13842014-11-06 18:23:12 -08001343 // returns the newly computed bounding box of the rectangle
1344 function adjustRectToFitText(n) {
1345 var text = n.select('text'),
1346 box = text.node().getBBox(),
1347 lab = config.labels;
1348
1349 text.attr('text-anchor', 'middle')
1350 .attr('y', '-0.8em')
1351 .attr('x', lab.imgPad/2);
1352
1353 // translate the bbox so that it is centered on [x,y]
1354 box.x = -box.width / 2;
1355 box.y = -box.height / 2;
1356
1357 // add padding
1358 box.x -= (lab.padLR + lab.imgPad/2);
1359 box.width += lab.padLR * 2 + lab.imgPad;
1360 box.y -= lab.padTB;
1361 box.height += lab.padTB * 2;
1362
1363 return box;
1364 }
1365
Simon Hunt1a9eff92014-11-07 11:06:34 -08001366 function mkSvgClass(d) {
1367 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
1368 }
1369
Simon Hunt7cd48f32014-11-09 23:42:50 -08001370 function hostLabel(d) {
1371 var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0;
1372 return d.labels[idx];
1373 }
1374 function deviceLabel(d) {
1375 var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0;
1376 return d.labels[idx];
1377 }
Simon Huntc72967b2014-11-20 09:21:42 -08001378 function trimLabel(label) {
1379 return (label && label.trim()) || '';
1380 }
1381
1382 function emptyBox() {
1383 return {
1384 x: -2,
1385 y: -2,
1386 width: 4,
1387 height: 4
1388 };
Simon Hunt7cd48f32014-11-09 23:42:50 -08001389 }
1390
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001391 function updateDeviceLabel(d) {
Simon Huntc72967b2014-11-20 09:21:42 -08001392 var label = trimLabel(deviceLabel(d)),
1393 noLabel = !label,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001394 node = d.el,
Simon Huntc72967b2014-11-20 09:21:42 -08001395 box,
1396 dx,
1397 dy;
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001398
1399 node.select('text')
1400 .text(label)
1401 .style('opacity', 0)
1402 .transition()
1403 .style('opacity', 1);
1404
Simon Huntc72967b2014-11-20 09:21:42 -08001405 if (noLabel) {
1406 box = emptyBox();
1407 dx = -config.icons.device.dim/2;
1408 dy = -config.icons.device.dim/2;
1409 } else {
1410 box = adjustRectToFitText(node);
1411 dx = box.x + config.icons.xoff;
1412 dy = box.y + config.icons.yoff;
1413 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001414
1415 node.select('rect')
1416 .transition()
1417 .attr(box);
1418
Simon Huntc72967b2014-11-20 09:21:42 -08001419 node.select('g.deviceIcon')
Thomas Vachuska89543292014-11-19 11:28:33 -08001420 .transition()
Simon Huntc72967b2014-11-20 09:21:42 -08001421 .attr('transform', translate(dx, dy));
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001422 }
1423
1424 function updateHostLabel(d) {
1425 var label = hostLabel(d),
1426 host = d.el;
1427
1428 host.select('text').text(label);
1429 }
1430
Simon Hunta6a9fe72014-11-20 11:17:12 -08001431 // TODO: should be using updateNodes() to do the upates!
Simon Huntbb282f52014-11-10 11:08:19 -08001432 function updateDeviceState(nodeData) {
1433 nodeData.el.classed('online', nodeData.online);
1434 updateDeviceLabel(nodeData);
1435 // TODO: review what else might need to be updated
1436 }
1437
1438 function updateHostState(hostData) {
1439 updateHostLabel(hostData);
1440 // TODO: review what else might need to be updated
1441 }
1442
Simon Hunt6ac93f32014-11-13 12:17:27 -08001443 function nodeMouseOver(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001444 hovered = d;
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -08001445 if (trafficHover() && (d.class === 'host' || d.class === 'device')) {
Thomas Vachuska29617e52014-11-20 03:17:46 -08001446 showSelectTraffic();
1447 } else if (flowsHover() && (d.class === 'device')) {
1448 showDeviceLinkFlows();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001449 }
1450 }
1451
1452 function nodeMouseOut(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001453 hovered = null;
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -08001454 if (trafficHover() && (d.class === 'host' || d.class === 'device')) {
Thomas Vachuska29617e52014-11-20 03:17:46 -08001455 showSelectTraffic();
1456 } else if (flowsHover() && (d.class === 'device')) {
1457 showDeviceLinkFlows();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001458 }
1459 }
Simon Huntbb282f52014-11-10 11:08:19 -08001460
Simon Hunteb1514d2014-11-20 09:57:29 -08001461 function addHostIcon(node, radius, iid) {
Thomas Vachuska89543292014-11-19 11:28:33 -08001462 var dim = radius * 1.5,
1463 xlate = -dim / 2;
1464
Simon Hunteb1514d2014-11-20 09:57:29 -08001465 node.append('use').attr({
1466 class: 'glyphIcon hostIcon',
1467 transform: translate(xlate,xlate),
1468 'xlink:href': iid,
1469 width: dim,
1470 height: dim
1471 });
Thomas Vachuska89543292014-11-19 11:28:33 -08001472 }
1473
Simon Hunt99c13842014-11-06 18:23:12 -08001474 function updateNodes() {
1475 node = nodeG.selectAll('.node')
1476 .data(network.nodes, function (d) { return d.id; });
1477
Simon Huntc72967b2014-11-20 09:21:42 -08001478 // TODO: operate on existing nodes
Simon Hunt7cd48f32014-11-09 23:42:50 -08001479 // update host labels
Simon Hunt99c13842014-11-06 18:23:12 -08001480 //node .foo() .bar() ...
1481
1482 // operate on entering nodes:
1483 var entering = node.enter()
1484 .append('g')
1485 .attr({
1486 id: function (d) { return safeId(d.id); },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001487 class: mkSvgClass,
Simon Hunt99c13842014-11-06 18:23:12 -08001488 transform: function (d) { return translate(d.x, d.y); },
1489 opacity: 0
1490 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08001491 .call(network.drag)
Simon Hunt6ac93f32014-11-13 12:17:27 -08001492 .on('mouseover', nodeMouseOver)
1493 .on('mouseout', nodeMouseOut)
Simon Hunt99c13842014-11-06 18:23:12 -08001494 .transition()
1495 .attr('opacity', 1);
1496
1497 // augment device nodes...
1498 entering.filter('.device').each(function (d) {
1499 var node = d3.select(this),
Simon Huntc72967b2014-11-20 09:21:42 -08001500 label = trimLabel(deviceLabel(d)),
1501 noLabel = !label,
Simon Hunt99c13842014-11-06 18:23:12 -08001502 box;
1503
Simon Hunt7cd48f32014-11-09 23:42:50 -08001504 // provide ref to element from backing data....
1505 d.el = node;
1506
Simon Hunt99c13842014-11-06 18:23:12 -08001507 node.append('rect')
1508 .attr({
Simon Hunta3dd9572014-11-20 15:22:41 -08001509 rx: 5,
1510 ry: 5
Simon Hunt99c13842014-11-06 18:23:12 -08001511 });
1512
1513 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001514 .text(label)
Simon Hunt99c13842014-11-06 18:23:12 -08001515 .attr('dy', '1.1em');
1516
1517 box = adjustRectToFitText(node);
Simon Hunta3dd9572014-11-20 15:22:41 -08001518 node.select('rect').attr(box);
Simon Huntc72967b2014-11-20 09:21:42 -08001519 addDeviceIcon(node, box, noLabel, iconGlyphUrl(d));
Simon Huntc7ee0662014-11-05 16:44:37 -08001520 });
Simon Hunt934c3ce2014-11-05 11:45:07 -08001521
Thomas Vachuska89543292014-11-19 11:28:33 -08001522 // TODO: better place for this configuration state
1523 var defaultHostRadius = 9,
1524 hostRadius = {
1525 bgpSpeaker: 14,
1526 router: 14,
1527 host: 14
1528 },
Simon Hunteb1514d2014-11-20 09:57:29 -08001529 hostGlyphId = {
Thomas Vachuska89543292014-11-19 11:28:33 -08001530 bgpSpeaker: 'bgpSpeaker',
1531 router: 'router',
1532 host: 'host'
1533 };
1534
1535
Simon Hunt56d51852014-11-09 13:03:35 -08001536 // augment host nodes...
1537 entering.filter('.host').each(function (d) {
1538 var node = d3.select(this),
Thomas Vachuska89543292014-11-19 11:28:33 -08001539 r = hostRadius[d.type] || defaultHostRadius,
1540 textDy = r + 10,
Simon Hunteb1514d2014-11-20 09:57:29 -08001541 iid = iconGlyphUrl(d);
Simon Hunt56d51852014-11-09 13:03:35 -08001542
Simon Hunt7cd48f32014-11-09 23:42:50 -08001543 // provide ref to element from backing data....
1544 d.el = node;
1545
Thomas Vachuska89543292014-11-19 11:28:33 -08001546 node.append('circle')
1547 .attr('r', r);
Simon Hunt7fa116d2014-11-17 14:16:55 -08001548
Simon Hunteb1514d2014-11-20 09:57:29 -08001549 if (iid) {
1550 addHostIcon(node, r, iid);
Simon Hunt7fa116d2014-11-17 14:16:55 -08001551 }
Simon Hunt56d51852014-11-09 13:03:35 -08001552
Simon Hunt56d51852014-11-09 13:03:35 -08001553 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001554 .text(hostLabel)
Thomas Vachuska89543292014-11-19 11:28:33 -08001555 .attr('dy', textDy)
Simon Hunt7cd48f32014-11-09 23:42:50 -08001556 .attr('text-anchor', 'middle');
Simon Hunt56d51852014-11-09 13:03:35 -08001557
1558 // debug function to show the modelled x,y coordinates of nodes...
1559 if (debug('showNodeXY')) {
1560 node.select('circle').attr('fill-opacity', 0.5);
1561 node.append('circle')
1562 .attr({
1563 class: 'debug',
1564 cx: 0,
1565 cy: 0,
1566 r: '3px'
1567 });
1568 }
1569 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001570
Simon Hunt99c13842014-11-06 18:23:12 -08001571 // operate on both existing and new nodes, if necessary
1572 //node .foo() .bar() ...
Simon Huntc7ee0662014-11-05 16:44:37 -08001573
Simon Hunt99c13842014-11-06 18:23:12 -08001574 // operate on exiting nodes:
Simon Huntea80eb42014-11-11 13:46:57 -08001575 // Note that the node is removed after 2 seconds.
1576 // Sub element animations should be shorter than 2 seconds.
1577 var exiting = node.exit()
Simon Hunt44031102014-11-11 13:20:36 -08001578 .transition()
1579 .duration(2000)
Simon Huntea80eb42014-11-11 13:46:57 -08001580 .style('opacity', 0)
Simon Hunt99c13842014-11-06 18:23:12 -08001581 .remove();
Simon Huntea80eb42014-11-11 13:46:57 -08001582
1583 // host node exits....
1584 exiting.filter('.host').each(function (d) {
1585 var node = d3.select(this);
1586
1587 node.select('text')
1588 .style('opacity', 0.5)
1589 .transition()
1590 .duration(1000)
1591 .style('opacity', 0);
1592 // note, leave <g>.remove to remove this element
1593
Thomas Vachuska89543292014-11-19 11:28:33 -08001594 node.select('circle')
1595 .style('stroke-fill', '#555')
1596 .style('fill', '#888')
Simon Huntea80eb42014-11-11 13:46:57 -08001597 .style('opacity', 0.5)
1598 .transition()
1599 .duration(1500)
1600 .attr('r', 0);
1601 // note, leave <g>.remove to remove this element
1602
1603 });
1604
1605 // TODO: device node exits
Simon Huntc7ee0662014-11-05 16:44:37 -08001606 }
1607
Simon Huntc72967b2014-11-20 09:21:42 -08001608 function addDeviceIcon(node, box, noLabel, iid) {
1609 var cfg = config.icons.device,
1610 dx,
1611 dy,
1612 g;
1613
1614 if (noLabel) {
1615 box = emptyBox();
1616 dx = -cfg.dim/2;
1617 dy = -cfg.dim/2;
1618 } else {
1619 box = adjustRectToFitText(node);
1620 dx = box.x + config.icons.xoff;
1621 dy = box.y + config.icons.yoff;
1622 }
1623
Simon Hunteb1514d2014-11-20 09:57:29 -08001624 g = node.append('g')
1625 .attr('class', 'glyphIcon deviceIcon')
Simon Huntc72967b2014-11-20 09:21:42 -08001626 .attr('transform', translate(dx, dy));
1627
1628 g.append('rect').attr({
1629 x: 0,
1630 y: 0,
1631 rx: cfg.rx,
1632 width: cfg.dim,
1633 height: cfg.dim
1634 });
1635
1636 g.append('use').attr({
1637 'xlink:href': iid,
1638 width: cfg.dim,
1639 height: cfg.dim
1640 });
1641
Simon Huntc72967b2014-11-20 09:21:42 -08001642 }
1643
Simon Hunt8257f4c2014-11-16 19:34:54 -08001644 function find(key, array) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001645 for (var idx = 0, n = array.length; idx < n; idx++) {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001646 if (array[idx].key === key) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001647 return idx;
1648 }
1649 }
1650 return -1;
1651 }
1652
1653 function removeLinkElement(linkData) {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001654 var idx = find(linkData.key, network.links),
1655 removed;
1656 if (idx >=0) {
1657 // remove from links array
1658 removed = network.links.splice(idx, 1);
1659 // remove from lookup cache
1660 delete network.lookup[removed[0].key];
1661 updateLinks();
1662 network.force.resume();
1663 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001664 }
Simon Huntc7ee0662014-11-05 16:44:37 -08001665
Simon Hunt44031102014-11-11 13:20:36 -08001666 function removeHostElement(hostData) {
1667 // first, remove associated hostLink...
1668 removeLinkElement(hostData.linkData);
1669
1670 // remove from lookup cache
1671 delete network.lookup[hostData.id];
1672 // remove from nodes array
1673 var idx = find(hostData.id, network.nodes);
1674 network.nodes.splice(idx, 1);
1675 // remove from SVG
1676 updateNodes();
1677 network.force.resume();
1678 }
1679
1680
Simon Huntc7ee0662014-11-05 16:44:37 -08001681 function tick() {
1682 node.attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001683 transform: function (d) { return translate(d.x, d.y); }
Simon Huntc7ee0662014-11-05 16:44:37 -08001684 });
1685
1686 link.attr({
1687 x1: function (d) { return d.source.x; },
1688 y1: function (d) { return d.source.y; },
1689 x2: function (d) { return d.target.x; },
1690 y2: function (d) { return d.target.y; }
1691 });
Simon Hunte2575b62014-11-18 15:25:53 -08001692
1693 linkLabel.each(function (d) {
1694 var el = d3.select(this);
Thomas Vachuska4731f122014-11-20 04:56:19 -08001695 var lnk = findLinkById(d.key);
1696
1697 if (lnk) {
1698 var parms = {
Simon Hunte2575b62014-11-18 15:25:53 -08001699 x1: lnk.source.x,
1700 y1: lnk.source.y,
1701 x2: lnk.target.x,
1702 y2: lnk.target.y
1703 };
Thomas Vachuska4731f122014-11-20 04:56:19 -08001704 el.attr('transform', transformLabel(parms));
1705 }
Simon Hunte2575b62014-11-18 15:25:53 -08001706 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001707 }
Simon Hunt934c3ce2014-11-05 11:45:07 -08001708
1709 // ==============================
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001710 // Web-Socket for live data
1711
1712 function webSockUrl() {
1713 return document.location.toString()
1714 .replace(/\#.*/, '')
1715 .replace('http://', 'ws://')
1716 .replace('https://', 'wss://')
1717 .replace('index2.html', config.webSockUrl);
1718 }
1719
1720 webSock = {
1721 ws : null,
1722
1723 connect : function() {
1724 webSock.ws = new WebSocket(webSockUrl());
1725
1726 webSock.ws.onopen = function() {
Simon Hunt0c6d4192014-11-12 12:07:10 -08001727 noWebSock(false);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001728 };
1729
1730 webSock.ws.onmessage = function(m) {
1731 if (m.data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001732 wsTraceRx(m.data);
Thomas Vachuskad472c6e2014-11-07 19:11:05 -08001733 handleServerEvent(JSON.parse(m.data));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001734 }
1735 };
1736
1737 webSock.ws.onclose = function(m) {
1738 webSock.ws = null;
Simon Hunt0c6d4192014-11-12 12:07:10 -08001739 noWebSock(true);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001740 };
1741 },
1742
1743 send : function(text) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001744 if (text != null) {
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001745 webSock._send(text);
1746 }
1747 },
1748
1749 _send : function(message) {
1750 if (webSock.ws) {
1751 webSock.ws.send(message);
Simon Hunta255a2c2014-11-13 22:29:35 -08001752 } else if (config.useLiveData) {
Simon Hunt56d51852014-11-09 13:03:35 -08001753 network.view.alert('no web socket open\n\n' + message);
Simon Hunta255a2c2014-11-13 22:29:35 -08001754 } else {
1755 console.log('WS Send: ' + JSON.stringify(message));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001756 }
1757 }
1758
1759 };
1760
Simon Hunt0c6d4192014-11-12 12:07:10 -08001761 function noWebSock(b) {
1762 mask.style('display',b ? 'block' : 'none');
1763 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001764
1765 function sendMessage(evType, payload) {
1766 var toSend = {
Simon Huntbb282f52014-11-10 11:08:19 -08001767 event: evType,
1768 sid: ++sid,
1769 payload: payload
1770 },
1771 asText = JSON.stringify(toSend);
1772 wsTraceTx(asText);
1773 webSock.send(asText);
Simon Huntc76ae892014-11-18 17:31:51 -08001774
1775 // Temporary measure for debugging UI behavior ...
1776 if (!config.useLiveData) {
1777 handleTestSend(toSend);
1778 }
Simon Huntbb282f52014-11-10 11:08:19 -08001779 }
1780
1781 function wsTraceTx(msg) {
1782 wsTrace('tx', msg);
1783 }
1784 function wsTraceRx(msg) {
1785 wsTrace('rx', msg);
1786 }
1787 function wsTrace(rxtx, msg) {
Simon Huntbb282f52014-11-10 11:08:19 -08001788 console.log('[' + rxtx + '] ' + msg);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001789 }
1790
Simon Huntc76ae892014-11-18 17:31:51 -08001791 // NOTE: Temporary hardcoded example for showing detail pane
1792 // while we fine-
1793 // Probably should not merge this change...
1794 function handleTestSend(msg) {
1795 if (msg.event === 'requestDetails') {
1796 showDetails({
1797 event: 'showDetails',
1798 sid: 1001,
1799 payload: {
1800 "id": "of:0000ffffffffff09",
1801 "type": "roadm",
1802 "propOrder": [
1803 "Name",
1804 "Vendor",
1805 "H/W Version",
1806 "S/W Version",
1807 "-",
1808 "Latitude",
1809 "Longitude",
1810 "Ports"
1811 ],
1812 "props": {
1813 "Name": null,
1814 "Vendor": "Linc",
1815 "H/W Version": "OE",
1816 "S/W Version": "?",
1817 "-": "",
1818 "Latitude": "40.8",
1819 "Longitude": "73.1",
1820 "Ports": "2"
1821 }
1822 }
1823 });
1824 }
1825 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001826
1827 // ==============================
1828 // Selection stuff
1829
1830 function selectObject(obj, el) {
1831 var n,
Simon Hunt01095ff2014-11-13 16:37:29 -08001832 srcEv = d3.event.sourceEvent,
1833 meta = srcEv.metaKey,
1834 shift = srcEv.shiftKey;
1835
Simon Huntdeab4322014-11-13 18:49:07 -08001836 if ((panZoom() && !meta) || (!panZoom() && meta)) {
Simon Hunt01095ff2014-11-13 16:37:29 -08001837 return;
1838 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001839
1840 if (el) {
1841 n = d3.select(el);
1842 } else {
1843 node.each(function(d) {
1844 if (d == obj) {
1845 n = d3.select(el = this);
1846 }
1847 });
1848 }
1849 if (!n) return;
1850
Simon Hunt01095ff2014-11-13 16:37:29 -08001851 if (shift && n.classed('selected')) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001852 deselectObject(obj.id);
Simon Hunt61d04042014-11-11 17:27:16 -08001853 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001854 return;
1855 }
1856
Simon Hunt01095ff2014-11-13 16:37:29 -08001857 if (!shift) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001858 deselectAll();
1859 }
1860
Simon Huntc31d5692014-11-12 13:27:18 -08001861 selections[obj.id] = { obj: obj, el: el };
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001862 selectOrder.push(obj.id);
1863
1864 n.classed('selected', true);
Simon Hunt61d04042014-11-11 17:27:16 -08001865 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001866 }
1867
1868 function deselectObject(id) {
Simon Huntc31d5692014-11-12 13:27:18 -08001869 var obj = selections[id],
1870 idx;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001871 if (obj) {
1872 d3.select(obj.el).classed('selected', false);
Simon Hunt61d04042014-11-11 17:27:16 -08001873 delete selections[id];
Simon Huntc31d5692014-11-12 13:27:18 -08001874 idx = $.inArray(id, selectOrder);
1875 if (idx >= 0) {
1876 selectOrder.splice(idx, 1);
1877 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001878 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001879 }
1880
1881 function deselectAll() {
1882 // deselect all nodes in the network...
1883 node.classed('selected', false);
1884 selections = {};
1885 selectOrder = [];
Simon Hunt61d04042014-11-11 17:27:16 -08001886 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001887 }
1888
Simon Hunt61d04042014-11-11 17:27:16 -08001889 // update the state of the detail pane, based on current selections
1890 function updateDetailPane() {
1891 var nSel = selectOrder.length;
1892 if (!nSel) {
1893 detailPane.hide();
Simon Huntd72bc702014-11-13 18:38:04 -08001894 showTrafficAction(); // sends cancelTraffic event
Simon Hunt61d04042014-11-11 17:27:16 -08001895 } else if (nSel === 1) {
1896 singleSelect();
1897 } else {
1898 multiSelect();
1899 }
1900 }
1901
1902 function singleSelect() {
1903 requestDetails();
Simon Huntb53e0682014-11-12 13:32:01 -08001904 // NOTE: detail pane will be shown from showDetails event callback
Simon Hunt61d04042014-11-11 17:27:16 -08001905 }
1906
1907 function multiSelect() {
Simon Huntb53e0682014-11-12 13:32:01 -08001908 populateMultiSelect();
Simon Huntb53e0682014-11-12 13:32:01 -08001909 }
1910
1911 function addSep(tbody) {
1912 var tr = tbody.append('tr');
1913 $('<hr>').appendTo(tr.append('td').attr('colspan', 2));
1914 }
1915
1916 function addProp(tbody, label, value) {
1917 var tr = tbody.append('tr');
1918
1919 tr.append('td')
1920 .attr('class', 'label')
1921 .text(label + ' :');
1922
1923 tr.append('td')
1924 .attr('class', 'value')
1925 .text(value);
1926 }
1927
1928 function populateMultiSelect() {
1929 detailPane.empty();
1930
Simon Hunta3dd9572014-11-20 15:22:41 -08001931 var title = detailPane.append('h3'),
1932 table = detailPane.append('table'),
1933 tbody = table.append('tbody');
Simon Huntb53e0682014-11-12 13:32:01 -08001934
Thomas Vachuska4731f122014-11-20 04:56:19 -08001935 title.text('Selected Nodes');
Simon Huntb53e0682014-11-12 13:32:01 -08001936
1937 selectOrder.forEach(function (d, i) {
1938 addProp(tbody, i+1, d);
1939 });
Simon Huntd72bc702014-11-13 18:38:04 -08001940
1941 addMultiSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08001942 }
1943
1944 function populateDetails(data) {
1945 detailPane.empty();
1946
Simon Hunta6a9fe72014-11-20 11:17:12 -08001947 var svg = detailPane.append('svg'),
1948 iid = iconGlyphUrl(data);
1949
Simon Hunta3dd9572014-11-20 15:22:41 -08001950 var title = detailPane.append('h2'),
1951 table = detailPane.append('table'),
1952 tbody = table.append('tbody');
Simon Hunt61d04042014-11-11 17:27:16 -08001953
Simon Hunta6a9fe72014-11-20 11:17:12 -08001954 appendGlyph(svg, 0, 0, 40, iid);
1955 title.text(data.id);
Simon Hunt61d04042014-11-11 17:27:16 -08001956
1957 data.propOrder.forEach(function(p) {
1958 if (p === '-') {
1959 addSep(tbody);
1960 } else {
1961 addProp(tbody, p, data.props[p]);
1962 }
1963 });
Simon Huntd72bc702014-11-13 18:38:04 -08001964
Thomas Vachuska4731f122014-11-20 04:56:19 -08001965 addSingleSelectActions(data);
Simon Hunt61d04042014-11-11 17:27:16 -08001966 }
1967
Thomas Vachuska4731f122014-11-20 04:56:19 -08001968 function addSingleSelectActions(data) {
Simon Huntd72bc702014-11-13 18:38:04 -08001969 detailPane.append('hr');
1970 // always want to allow 'show traffic'
Thomas Vachuska4731f122014-11-20 04:56:19 -08001971 addAction(detailPane, 'Show Related Traffic', showTrafficAction);
1972
1973 if (data.type === 'switch') {
1974 addAction(detailPane, 'Show Device Flows', showDeviceLinkFlowsAction);
1975 }
Simon Huntd72bc702014-11-13 18:38:04 -08001976 }
1977
1978 function addMultiSelectActions() {
1979 detailPane.append('hr');
1980 // always want to allow 'show traffic'
Thomas Vachuska4731f122014-11-20 04:56:19 -08001981 addAction(detailPane, 'Show Related Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001982 // if exactly two hosts are selected, also want 'add host intent'
1983 if (nSel() === 2 && allSelectionsClass('host')) {
Thomas Vachuska4731f122014-11-20 04:56:19 -08001984 addAction(detailPane, 'Add Host-to-Host Intent', addIntentAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001985 }
1986 }
1987
Simon Hunta5e89142014-11-14 07:00:33 -08001988 function addAction(panel, text, cb) {
1989 panel.append('div')
Simon Huntd72bc702014-11-13 18:38:04 -08001990 .classed('actionBtn', true)
1991 .text(text)
1992 .on('click', cb);
1993 }
1994
1995
Paul Greysonfcba0e82014-11-13 10:21:16 -08001996 function zoomPan(scale, translate) {
1997 zoomPanContainer.attr("transform", "translate(" + translate + ")scale(" + scale + ")");
1998 // keep the map lines constant width while zooming
Thomas Vachuska89543292014-11-19 11:28:33 -08001999 bgImg.style("stroke-width", 2.0 / scale + "px");
Paul Greysonfcba0e82014-11-13 10:21:16 -08002000 }
2001
2002 function resetZoomPan() {
2003 zoomPan(1, [0,0]);
2004 zoom.scale(1).translate([0,0]);
2005 }
2006
2007 function setupZoomPan() {
2008 function zoomed() {
Simon Huntdeab4322014-11-13 18:49:07 -08002009 if (!panZoom() ^ !d3.event.sourceEvent.metaKey) {
Paul Greysonfcba0e82014-11-13 10:21:16 -08002010 zoomPan(d3.event.scale, d3.event.translate);
2011 }
2012 }
2013
2014 zoom = d3.behavior.zoom()
2015 .translate([0, 0])
2016 .scale(1)
2017 .scaleExtent([1, 8])
2018 .on("zoom", zoomed);
2019
2020 svg.call(zoom);
2021 }
2022
Simon Hunt61d04042014-11-11 17:27:16 -08002023 // ==============================
2024 // Test harness code
Simon Hunt56d51852014-11-09 13:03:35 -08002025
2026 function prepareScenario(view, ctx, dbg) {
2027 var sc = scenario,
2028 urlSc = sc.evDir + ctx + sc.evScenario;
2029
2030 if (!ctx) {
2031 view.alert("No scenario specified (null ctx)");
2032 return;
2033 }
2034
2035 sc.view = view;
2036 sc.ctx = ctx;
2037 sc.debug = dbg;
2038 sc.evNumber = 0;
2039
2040 d3.json(urlSc, function(err, data) {
Simon Huntbb282f52014-11-10 11:08:19 -08002041 var p = data && data.params || {},
2042 desc = data && data.description || null,
Simon Huntfc274c92014-11-11 11:05:46 -08002043 intro = data && data.title;
Simon Huntbb282f52014-11-10 11:08:19 -08002044
Simon Hunt56d51852014-11-09 13:03:35 -08002045 if (err) {
2046 view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err);
2047 } else {
2048 sc.params = p;
Simon Huntbb282f52014-11-10 11:08:19 -08002049 if (desc) {
2050 intro += '\n\n ' + desc.join('\n ');
2051 }
2052 view.alert(intro);
Simon Hunt56d51852014-11-09 13:03:35 -08002053 }
2054 });
2055
2056 }
2057
Simon Hunt01095ff2014-11-13 16:37:29 -08002058 // ==============================
2059 // Toggle Buttons in masthead
Simon Hunt0c6d4192014-11-12 12:07:10 -08002060
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002061 // TODO: toggle button (and other widgets in the masthead) should be provided
2062 // by the framework; not generated by the view.
2063
Simon Hunta3dd9572014-11-20 15:22:41 -08002064 var showInstances;
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002065
2066 function addButtonBar(view) {
2067 var bb = d3.select('#mast')
2068 .append('span').classed('right', true).attr('id', 'bb');
2069
Simon Hunta5e89142014-11-14 07:00:33 -08002070 function mkTogBtn(text, cb) {
2071 return bb.append('span')
2072 .classed('btn', true)
2073 .text(text)
2074 .on('click', cb);
2075 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002076
Simon Hunta5e89142014-11-14 07:00:33 -08002077 showInstances = mkTogBtn('Show Instances', toggleInst);
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002078 }
2079
Simon Hunta5e89142014-11-14 07:00:33 -08002080 function instShown() {
2081 return showInstances.classed('active');
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002082 }
Simon Hunta5e89142014-11-14 07:00:33 -08002083 function toggleInst() {
2084 showInstances.classed('active', !instShown());
2085 if (instShown()) {
2086 oiBox.show();
2087 } else {
2088 oiBox.hide();
2089 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002090 }
2091
Simon Huntdeab4322014-11-13 18:49:07 -08002092 function panZoom() {
Simon Hunte5b71752014-11-18 20:06:07 -08002093 return false;
Simon Hunta5e89142014-11-14 07:00:33 -08002094 }
2095
2096 function trafficHover() {
Thomas Vachuska29617e52014-11-20 03:17:46 -08002097 return hoverModes[hoverMode] === 'intents';
Simon Hunta5e89142014-11-14 07:00:33 -08002098 }
Thomas Vachuska29617e52014-11-20 03:17:46 -08002099
2100 function flowsHover() {
2101 return hoverModes[hoverMode] === 'flows';
2102 }
2103
Simon Hunt7fa116d2014-11-17 14:16:55 -08002104 function loadGlyphs(svg) {
2105 var defs = svg.append('defs');
2106 gly.defBird(defs);
2107 gly.defBullhorn(defs);
Simon Huntc72967b2014-11-20 09:21:42 -08002108 gly.defGlyphs(defs);
Simon Hunt7fa116d2014-11-17 14:16:55 -08002109 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002110
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002111 // ==============================
Simon Hunt142d0032014-11-04 20:13:09 -08002112 // View life-cycle callbacks
Simon Hunt195cb382014-11-03 17:50:51 -08002113
Simon Huntf67722a2014-11-10 09:32:06 -08002114 function preload(view, ctx, flags) {
Simon Hunt142d0032014-11-04 20:13:09 -08002115 var w = view.width(),
2116 h = view.height(),
Simon Huntc7ee0662014-11-05 16:44:37 -08002117 fcfg = config.force,
2118 fpad = fcfg.pad,
2119 forceDim = [w - 2*fpad, h - 2*fpad];
Simon Hunt195cb382014-11-03 17:50:51 -08002120
Simon Hunt142d0032014-11-04 20:13:09 -08002121 // NOTE: view.$div is a D3 selection of the view's div
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002122 var viewBox = '0 0 ' + config.logicalSize + ' ' + config.logicalSize;
2123 svg = view.$div.append('svg').attr('viewBox', viewBox);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002124 setSize(svg, view);
2125
Simon Hunt7fa116d2014-11-17 14:16:55 -08002126 loadGlyphs(svg);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002127
Paul Greysonfcba0e82014-11-13 10:21:16 -08002128 zoomPanContainer = svg.append('g').attr('id', 'zoomPanContainer');
Paul Greysonfcba0e82014-11-13 10:21:16 -08002129 setupZoomPan();
2130
Simon Hunt1a9eff92014-11-07 11:06:34 -08002131 // add blue glow filter to svg layer
Paul Greysonfcba0e82014-11-13 10:21:16 -08002132 d3u.appendGlow(zoomPanContainer);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002133
Simon Huntc7ee0662014-11-05 16:44:37 -08002134 // group for the topology
Paul Greysonfcba0e82014-11-13 10:21:16 -08002135 topoG = zoomPanContainer.append('g')
Simon Huntd3b7d512014-11-12 15:48:41 -08002136 .attr('id', 'topo-G')
Simon Huntc7ee0662014-11-05 16:44:37 -08002137 .attr('transform', fcfg.translate());
2138
Simon Hunte2575b62014-11-18 15:25:53 -08002139 // subgroups for links, link labels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002140 linkG = topoG.append('g').attr('id', 'links');
Simon Hunte2575b62014-11-18 15:25:53 -08002141 linkLabelG = topoG.append('g').attr('id', 'linkLabels');
Simon Huntc7ee0662014-11-05 16:44:37 -08002142 nodeG = topoG.append('g').attr('id', 'nodes');
2143
Simon Hunte2575b62014-11-18 15:25:53 -08002144 // selection of links, linkLabels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002145 link = linkG.selectAll('.link');
Simon Hunte2575b62014-11-18 15:25:53 -08002146 linkLabel = linkLabelG.selectAll('.linkLabel');
Simon Huntc7ee0662014-11-05 16:44:37 -08002147 node = nodeG.selectAll('.node');
2148
Simon Hunt7cd48f32014-11-09 23:42:50 -08002149 function chrg(d) {
2150 return fcfg.charge[d.class] || -12000;
2151 }
Simon Hunt99c13842014-11-06 18:23:12 -08002152 function ldist(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002153 return fcfg.linkDistance[d.type] || 50;
Simon Hunt99c13842014-11-06 18:23:12 -08002154 }
2155 function lstrg(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002156 // 0.0 - 1.0
2157 return fcfg.linkStrength[d.type] || 1.0;
Simon Hunt99c13842014-11-06 18:23:12 -08002158 }
2159
Simon Hunt1a9eff92014-11-07 11:06:34 -08002160 function selectCb(d, self) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002161 selectObject(d, self);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002162 }
2163
2164 function atDragEnd(d, self) {
Simon Hunt56d51852014-11-09 13:03:35 -08002165 // once we've finished moving, pin the node in position
2166 d.fixed = true;
2167 d3.select(self).classed('fixed', true);
2168 if (config.useLiveData) {
Simon Hunt902c9922014-11-11 11:59:31 -08002169 sendUpdateMeta(d);
Simon Hunta255a2c2014-11-13 22:29:35 -08002170 } else {
2171 console.log('Moving node ' + d.id + ' to [' + d.x + ',' + d.y + ']');
Simon Hunt1a9eff92014-11-07 11:06:34 -08002172 }
2173 }
2174
Simon Hunt902c9922014-11-11 11:59:31 -08002175 function sendUpdateMeta(d) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002176 sendMessage('updateMeta', {
2177 id: d.id,
2178 'class': d.class,
Simon Hunt902c9922014-11-11 11:59:31 -08002179 'memento': {
Simon Hunt01095ff2014-11-13 16:37:29 -08002180 x: d.x,
2181 y: d.y
Simon Hunt902c9922014-11-11 11:59:31 -08002182 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002183 });
2184 }
2185
Simon Huntc7ee0662014-11-05 16:44:37 -08002186 // set up the force layout
2187 network.force = d3.layout.force()
2188 .size(forceDim)
2189 .nodes(network.nodes)
2190 .links(network.links)
Simon Hunt7cd48f32014-11-09 23:42:50 -08002191 .gravity(0.4)
2192 .friction(0.7)
2193 .charge(chrg)
Simon Hunt99c13842014-11-06 18:23:12 -08002194 .linkDistance(ldist)
2195 .linkStrength(lstrg)
Simon Huntc7ee0662014-11-05 16:44:37 -08002196 .on('tick', tick);
Simon Hunt195cb382014-11-03 17:50:51 -08002197
Simon Hunt01095ff2014-11-13 16:37:29 -08002198 network.drag = d3u.createDragBehavior(network.force,
Simon Huntdeab4322014-11-13 18:49:07 -08002199 selectCb, atDragEnd, panZoom);
Simon Hunt0c6d4192014-11-12 12:07:10 -08002200
2201 // create mask layer for when we lose connection to server.
Simon Hunta5e89142014-11-14 07:00:33 -08002202 // TODO: this should be part of the framework
Simon Hunt0c6d4192014-11-12 12:07:10 -08002203 mask = view.$div.append('div').attr('id','topo-mask');
2204 para(mask, 'Oops!');
2205 para(mask, 'Web-socket connection to server closed...');
2206 para(mask, 'Try refreshing the page.');
Simon Hunt12ce12e2014-11-15 21:13:19 -08002207
2208 mask.append('svg')
2209 .attr({
2210 id: 'mask-bird',
2211 width: w,
2212 height: h
2213 })
2214 .append('g')
2215 .attr('transform', birdTranslate(w, h))
2216 .style('opacity', 0.3)
2217 .append('use')
2218 .attr({
2219 'xlink:href': '#bird',
2220 width: config.birdDim,
2221 height: config.birdDim,
2222 fill: '#111'
Thomas Vachuska89543292014-11-19 11:28:33 -08002223 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08002224 }
Simon Hunt195cb382014-11-03 17:50:51 -08002225
Simon Hunt01095ff2014-11-13 16:37:29 -08002226 function para(sel, text) {
2227 sel.append('p').text(text);
2228 }
2229
2230
Simon Hunt56d51852014-11-09 13:03:35 -08002231 function load(view, ctx, flags) {
Simon Huntf67722a2014-11-10 09:32:06 -08002232 // resize, in case the window was resized while we were not loaded
2233 resize(view, ctx, flags);
2234
Simon Hunt99c13842014-11-06 18:23:12 -08002235 // cache the view token, so network topo functions can access it
2236 network.view = view;
Simon Hunt56d51852014-11-09 13:03:35 -08002237 config.useLiveData = !flags.local;
2238
2239 if (!config.useLiveData) {
2240 prepareScenario(view, ctx, flags.debug);
2241 }
Simon Hunt99c13842014-11-06 18:23:12 -08002242
2243 // set our radio buttons and key bindings
Simon Hunt9462e8c2014-11-14 17:28:09 -08002244 layerBtnSet = view.setRadio(layerButtons);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002245 view.setKeys(keyDispatch);
Simon Hunt195cb382014-11-03 17:50:51 -08002246
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002247 // patch in our "button bar" for now
2248 // TODO: implement a more official frameworky way of doing this..
2249 addButtonBar(view);
2250
Simon Huntd3b7d512014-11-12 15:48:41 -08002251 // Load map data asynchronously; complete startup after that..
2252 loadGeoJsonData();
Simon Hunta255a2c2014-11-13 22:29:35 -08002253 }
2254
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002255 function startAntTimer() {
Thomas Vachuska4731f122014-11-20 04:56:19 -08002256 if (!antTimer) {
2257 var pulses = [5, 3, 1.2, 3],
2258 pulse = 0;
2259 antTimer = setInterval(function () {
2260 pulse = pulse + 1;
2261 pulse = pulse === pulses.length ? 0 : pulse;
2262 d3.selectAll('.animated').style('stroke-width', pulses[pulse]);
2263 }, 200);
2264 }
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002265 }
2266
2267 function stopAntTimer() {
Simon Hunta255a2c2014-11-13 22:29:35 -08002268 if (antTimer) {
2269 clearInterval(antTimer);
2270 antTimer = null;
2271 }
Simon Huntd3b7d512014-11-12 15:48:41 -08002272 }
2273
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002274 function unload(view, ctx, flags) {
2275 stopAntTimer();
2276 }
2277
Simon Huntd3b7d512014-11-12 15:48:41 -08002278 // TODO: move these to config/state portion of script
Simon Hunta6a9fe72014-11-20 11:17:12 -08002279 var geoJsonUrl = 'json/map/continental_us.json',
Simon Huntd3b7d512014-11-12 15:48:41 -08002280 geoJson;
2281
2282 function loadGeoJsonData() {
2283 d3.json(geoJsonUrl, function (err, data) {
2284 if (err) {
2285 // fall back to USA map background
2286 loadStaticMap();
2287 } else {
2288 geoJson = data;
2289 loadGeoMap();
2290 }
2291
2292 // finally, connect to the server...
2293 if (config.useLiveData) {
2294 webSock.connect();
2295 }
2296 });
2297 }
2298
2299 function showBg() {
2300 return config.options.showBackground ? 'visible' : 'hidden';
2301 }
2302
2303 function loadStaticMap() {
2304 fnTrace('loadStaticMap', config.backgroundUrl);
2305 var w = network.view.width(),
2306 h = network.view.height();
2307
2308 // load the background image
2309 bgImg = svg.insert('svg:image', '#topo-G')
2310 .attr({
2311 id: 'topo-bg',
2312 width: w,
2313 height: h,
2314 'xlink:href': config.backgroundUrl
2315 })
2316 .style({
2317 visibility: showBg()
2318 });
2319 }
2320
2321 function loadGeoMap() {
2322 fnTrace('loadGeoMap', geoJsonUrl);
Simon Huntd3b7d512014-11-12 15:48:41 -08002323
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002324 // extracts the topojson data into geocoordinate-based geometry
2325 var topoData = topojson.feature(geoJson, geoJson.objects.states);
Simon Huntd3b7d512014-11-12 15:48:41 -08002326
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002327 // see: http://bl.ocks.org/mbostock/4707858
2328 geoMapProjection = d3.geo.mercator();
2329 var path = d3.geo.path().projection(geoMapProjection);
Simon Huntd3b7d512014-11-12 15:48:41 -08002330
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002331 geoMapProjection
2332 .scale(1)
2333 .translate([0, 0]);
Simon Huntd3b7d512014-11-12 15:48:41 -08002334
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002335 // [[x1,y1],[x2,y2]]
2336 var b = path.bounds(topoData);
Paul Greysonfcba0e82014-11-13 10:21:16 -08002337 // size map to 95% of minimum dimension to fill space
2338 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 -08002339 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 -08002340
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002341 geoMapProjection
2342 .scale(s)
2343 .translate(t);
2344
Paul Greysonfcba0e82014-11-13 10:21:16 -08002345 bgImg = zoomPanContainer.insert("g", '#topo-G');
Thomas Vachuska89543292014-11-19 11:28:33 -08002346 bgImg.attr('id', 'map').selectAll('path')
2347 .data(topoData.features)
2348 .enter()
2349 .append('path')
2350 .attr('d', path);
Simon Hunt195cb382014-11-03 17:50:51 -08002351 }
2352
Simon Huntf67722a2014-11-10 09:32:06 -08002353 function resize(view, ctx, flags) {
Simon Hunt12ce12e2014-11-15 21:13:19 -08002354 var w = view.width(),
2355 h = view.height();
2356
Simon Hunt934c3ce2014-11-05 11:45:07 -08002357 setSize(svg, view);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002358
2359 d3.select('#mask-bird').attr({ width: w, height: h})
2360 .select('g').attr('transform', birdTranslate(w, h));
Simon Hunt142d0032014-11-04 20:13:09 -08002361 }
2362
Simon Hunt12ce12e2014-11-15 21:13:19 -08002363 function birdTranslate(w, h) {
2364 var bdim = config.birdDim;
2365 return 'translate('+((w-bdim)*.4)+','+((h-bdim)*.1)+')';
2366 }
Simon Hunt142d0032014-11-04 20:13:09 -08002367
2368 // ==============================
2369 // View registration
Simon Hunt195cb382014-11-03 17:50:51 -08002370
Simon Hunt25248912014-11-04 11:25:48 -08002371 onos.ui.addView('topo', {
Simon Hunt142d0032014-11-04 20:13:09 -08002372 preload: preload,
2373 load: load,
Simon Hunta255a2c2014-11-13 22:29:35 -08002374 unload: unload,
Simon Hunt142d0032014-11-04 20:13:09 -08002375 resize: resize
Simon Hunt195cb382014-11-03 17:50:51 -08002376 });
2377
Simon Hunt61d04042014-11-11 17:27:16 -08002378 detailPane = onos.ui.addFloatingPanel('topo-detail');
Simon Hunta5e89142014-11-14 07:00:33 -08002379 oiBox = onos.ui.addFloatingPanel('topo-oibox', 'TL');
Simon Hunt61d04042014-11-11 17:27:16 -08002380
Simon Hunt195cb382014-11-03 17:50:51 -08002381}(ONOS));