blob: 58c11672668ad92f8b69ea74be18e945484d3bb2 [file] [log] [blame]
Simon Hunt195cb382014-11-03 17:50:51 -08001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
Simon Hunt142d0032014-11-04 20:13:09 -080018 ONOS network topology viewer - version 1.1
Simon Hunt195cb382014-11-03 17:50:51 -080019
20 @author Simon Hunt
Simon Hunta6a9fe72014-11-20 11:17:12 -080021 @author Thomas Vachuska
Simon Hunt195cb382014-11-03 17:50:51 -080022 */
23
24(function (onos) {
25 'use strict';
26
Simon Hunt1a9eff92014-11-07 11:06:34 -080027 // shorter names for library APIs
Simon Huntbb282f52014-11-10 11:08:19 -080028 var d3u = onos.lib.d3util,
Simon Hunta6a9fe72014-11-20 11:17:12 -080029 gly = onos.lib.glyphs;
Simon Hunt1a9eff92014-11-07 11:06:34 -080030
Simon Hunt195cb382014-11-03 17:50:51 -080031 // configuration data
32 var config = {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -080033 useLiveData: true,
Simon Huntfc274c92014-11-11 11:05:46 -080034 fnTrace: true,
Simon Hunt195cb382014-11-03 17:50:51 -080035 debugOn: false,
36 debug: {
Simon Hunt99c13842014-11-06 18:23:12 -080037 showNodeXY: true,
38 showKeyHandler: false
Simon Hunt195cb382014-11-03 17:50:51 -080039 },
Simon Hunt12ce12e2014-11-15 21:13:19 -080040 birdDim: 400,
Simon Hunt195cb382014-11-03 17:50:51 -080041 options: {
42 layering: true,
43 collisionPrevention: true,
Simon Hunt142d0032014-11-04 20:13:09 -080044 showBackground: true
Simon Hunt195cb382014-11-03 17:50:51 -080045 },
46 backgroundUrl: 'img/us-map.png',
Thomas Vachuska7d638d32014-11-07 10:24:43 -080047 webSockUrl: 'ws/topology',
Simon Hunt195cb382014-11-03 17:50:51 -080048 data: {
49 live: {
50 jsonUrl: 'rs/topology/graph',
51 detailPrefix: 'rs/topology/graph/',
52 detailSuffix: ''
53 },
54 fake: {
55 jsonUrl: 'json/network2.json',
56 detailPrefix: 'json/',
57 detailSuffix: '.json'
58 }
59 },
Simon Hunt99c13842014-11-06 18:23:12 -080060 labels: {
61 imgPad: 16,
62 padLR: 4,
63 padTB: 3,
64 marginLR: 3,
65 marginTB: 2,
66 port: {
67 gap: 3,
68 width: 18,
69 height: 14
70 }
71 },
Simon Hunt1a9eff92014-11-07 11:06:34 -080072 topo: {
Thomas Vachuska89543292014-11-19 11:28:33 -080073 linkBaseColor: '#666',
Simon Hunt1a9eff92014-11-07 11:06:34 -080074 linkInColor: '#66f',
Paul Greyson29cd58f2014-11-18 13:14:57 -080075 linkInWidth: 14,
Thomas Vachuska89543292014-11-19 11:28:33 -080076 linkOutColor: '#f00',
Simon Hunt95908012014-11-20 10:20:26 -080077 linkOutWidth: 14
Simon Hunt1a9eff92014-11-07 11:06:34 -080078 },
Paul Greyson29cd58f2014-11-18 13:14:57 -080079 icons: {
Thomas Vachuskaece59ee2014-11-19 19:06:11 -080080 w: 30,
81 h: 30,
82 xoff: -16,
Simon Huntc72967b2014-11-20 09:21:42 -080083 yoff: -14,
84
85 device: {
86 dim: 30,
87 rx: 4
88 }
Thomas Vachuska89543292014-11-19 11:28:33 -080089 },
90 iconUrl: {
91 device: 'img/device.png',
92 host: 'img/host.png',
93 pkt: 'img/pkt.png',
94 opt: 'img/opt.png'
Simon Hunt195cb382014-11-03 17:50:51 -080095 },
Simon Hunt195cb382014-11-03 17:50:51 -080096 force: {
Simon Hunt7cd48f32014-11-09 23:42:50 -080097 note_for_links: 'link.type is used to differentiate',
Simon Huntc7ee0662014-11-05 16:44:37 -080098 linkDistance: {
Simon Hunt7cd48f32014-11-09 23:42:50 -080099 direct: 100,
100 optical: 120,
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800101 hostLink: 3
Simon Huntc7ee0662014-11-05 16:44:37 -0800102 },
103 linkStrength: {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800104 direct: 1.0,
105 optical: 1.0,
106 hostLink: 1.0
Simon Huntc7ee0662014-11-05 16:44:37 -0800107 },
Simon Hunt7cd48f32014-11-09 23:42:50 -0800108 note_for_nodes: 'node.class is used to differentiate',
Simon Huntc7ee0662014-11-05 16:44:37 -0800109 charge: {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800110 device: -8000,
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800111 host: -5000
Simon Huntc7ee0662014-11-05 16:44:37 -0800112 },
113 pad: 20,
Simon Hunt195cb382014-11-03 17:50:51 -0800114 translate: function() {
115 return 'translate(' +
Simon Huntc7ee0662014-11-05 16:44:37 -0800116 config.force.pad + ',' +
117 config.force.pad + ')';
Simon Hunt195cb382014-11-03 17:50:51 -0800118 }
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800119 },
120 // see below in creation of viewBox on main svg
121 logicalSize: 1000
Simon Hunt195cb382014-11-03 17:50:51 -0800122 };
123
Simon Hunt142d0032014-11-04 20:13:09 -0800124 // radio buttons
Simon Hunt9462e8c2014-11-14 17:28:09 -0800125 var layerButtons = [
126 { text: 'All Layers', id: 'all', cb: showAllLayers },
127 { text: 'Packet Only', id: 'pkt', cb: showPacketLayer },
128 { text: 'Optical Only', id: 'opt', cb: showOpticalLayer }
129 ],
130 layerBtnSet,
131 layerBtnDispatch = {
132 all: showAllLayers,
133 pkt: showPacketLayer,
134 opt: showOpticalLayer
135 };
Simon Hunt934c3ce2014-11-05 11:45:07 -0800136
137 // key bindings
138 var keyDispatch = {
Simon Hunta255a2c2014-11-13 22:29:35 -0800139 M: testMe, // TODO: remove (testing only)
140 S: injectStartupEvents, // TODO: remove (testing only)
141 space: injectTestEvent, // TODO: remove (testing only)
Simon Hunt99c13842014-11-06 18:23:12 -0800142
Simon Hunt01095ff2014-11-13 16:37:29 -0800143 B: toggleBg,
Simon Hunt934c3ce2014-11-05 11:45:07 -0800144 L: cycleLabels,
145 P: togglePorts,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800146 U: unpin,
Paul Greysonfcba0e82014-11-13 10:21:16 -0800147 R: resetZoomPan,
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -0800148 H: toggleHover,
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800149 V: showTrafficAction,
150 A: showAllTrafficAction,
Thomas Vachuska29617e52014-11-20 03:17:46 -0800151 F: showDeviceLinkFlowsAction,
Simon Hunt9462e8c2014-11-14 17:28:09 -0800152 esc: handleEscape
Simon Hunt934c3ce2014-11-05 11:45:07 -0800153 };
Simon Hunt142d0032014-11-04 20:13:09 -0800154
Simon Hunt195cb382014-11-03 17:50:51 -0800155 // state variables
Simon Hunt99c13842014-11-06 18:23:12 -0800156 var network = {
Simon Hunt50128c02014-11-08 13:36:15 -0800157 view: null, // view token reference
Simon Hunt99c13842014-11-06 18:23:12 -0800158 nodes: [],
159 links: [],
Simon Hunt269670f2014-11-17 16:17:43 -0800160 lookup: {},
161 revLinkToKey: {}
Simon Hunt99c13842014-11-06 18:23:12 -0800162 },
Simon Hunt56d51852014-11-09 13:03:35 -0800163 scenario = {
164 evDir: 'json/ev/',
165 evScenario: '/scenario.json',
166 evPrefix: '/ev_',
167 evOnos: '_onos.json',
168 evUi: '_ui.json',
169 ctx: null,
170 params: {},
171 evNumber: 0,
172 view: null,
173 debug: false
174 },
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800175 webSock,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800176 sid = 0,
Simon Hunt56d51852014-11-09 13:03:35 -0800177 deviceLabelIndex = 0,
178 hostLabelIndex = 0,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800179 selections = {},
Simon Hunta5e89142014-11-14 07:00:33 -0800180 selectOrder = [],
Simon Hunt6ac93f32014-11-13 12:17:27 -0800181 hovered = null,
Simon Hunta5e89142014-11-14 07:00:33 -0800182 detailPane,
Simon Hunta255a2c2014-11-13 22:29:35 -0800183 antTimer = null,
Simon Hunta5e89142014-11-14 07:00:33 -0800184 onosInstances = {},
185 onosOrder = [],
186 oiBox,
Simon Hunt9462e8c2014-11-14 17:28:09 -0800187 oiShowMaster = false,
Thomas Vachuska29617e52014-11-20 03:17:46 -0800188 hoverModes = [ 'none', 'intents', 'flows'],
189 hoverMode = 0,
Simon Hunt195cb382014-11-03 17:50:51 -0800190 portLabelsOn = false;
191
Simon Hunt934c3ce2014-11-05 11:45:07 -0800192 // D3 selections
193 var svg,
Paul Greysonfcba0e82014-11-13 10:21:16 -0800194 zoomPanContainer,
Simon Hunt934c3ce2014-11-05 11:45:07 -0800195 bgImg,
Simon Huntc7ee0662014-11-05 16:44:37 -0800196 topoG,
197 nodeG,
198 linkG,
Simon Hunte2575b62014-11-18 15:25:53 -0800199 linkLabelG,
Simon Huntc7ee0662014-11-05 16:44:37 -0800200 node,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800201 link,
Simon Hunte2575b62014-11-18 15:25:53 -0800202 linkLabel,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800203 mask;
Simon Hunt195cb382014-11-03 17:50:51 -0800204
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800205 // the projection for the map background
206 var geoMapProjection;
207
Paul Greysonfcba0e82014-11-13 10:21:16 -0800208 // the zoom function
209 var zoom;
210
Simon Hunt142d0032014-11-04 20:13:09 -0800211 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800212 // For Debugging / Development
Simon Hunt195cb382014-11-03 17:50:51 -0800213
Simon Hunt99c13842014-11-06 18:23:12 -0800214 function note(label, msg) {
215 console.log('NOTE: ' + label + ': ' + msg);
Simon Hunt195cb382014-11-03 17:50:51 -0800216 }
217
Simon Hunt99c13842014-11-06 18:23:12 -0800218 function debug(what) {
219 return config.debugOn && config.debug[what];
Simon Hunt934c3ce2014-11-05 11:45:07 -0800220 }
221
Simon Huntfc274c92014-11-11 11:05:46 -0800222 function fnTrace(msg, id) {
223 if (config.fnTrace) {
224 console.log('FN: ' + msg + ' [' + id + ']');
225 }
226 }
Simon Hunt99c13842014-11-06 18:23:12 -0800227
Simon Hunta5e89142014-11-14 07:00:33 -0800228 function evTrace(data) {
229 fnTrace(data.event, data.payload.id);
230 }
231
Simon Hunt934c3ce2014-11-05 11:45:07 -0800232 // ==============================
233 // Key Callbacks
234
Simon Hunt99c13842014-11-06 18:23:12 -0800235 function testMe(view) {
Simon Hunt625dc402014-11-18 10:57:18 -0800236 view.alert('Theme is ' + view.theme());
Simon Hunt99c13842014-11-06 18:23:12 -0800237 }
238
Simon Hunt56d51852014-11-09 13:03:35 -0800239 function abortIfLive() {
Simon Hunt50128c02014-11-08 13:36:15 -0800240 if (config.useLiveData) {
Simon Huntb53e0682014-11-12 13:32:01 -0800241 network.view.alert("Sorry, currently using live data..");
Simon Hunt56d51852014-11-09 13:03:35 -0800242 return true;
Simon Hunt50128c02014-11-08 13:36:15 -0800243 }
Simon Hunt56d51852014-11-09 13:03:35 -0800244 return false;
245 }
Simon Hunt50128c02014-11-08 13:36:15 -0800246
Simon Hunt56d51852014-11-09 13:03:35 -0800247 function testDebug(msg) {
248 if (scenario.debug) {
249 scenario.view.alert(msg);
250 }
251 }
Simon Hunt99c13842014-11-06 18:23:12 -0800252
Simon Hunt56d51852014-11-09 13:03:35 -0800253 function injectTestEvent(view) {
254 if (abortIfLive()) { return; }
255 var sc = scenario,
256 evn = ++sc.evNumber,
257 pfx = sc.evDir + sc.ctx + sc.evPrefix + evn,
258 onosUrl = pfx + sc.evOnos,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800259 uiUrl = pfx + sc.evUi,
260 stack = [
261 { url: onosUrl, cb: handleServerEvent },
262 { url: uiUrl, cb: handleUiEvent }
263 ];
264 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800265 }
266
Simon Hunt7cd48f32014-11-09 23:42:50 -0800267 function recurseFetchEvent(stack, evn) {
268 var v = scenario.view,
269 frame;
270 if (stack.length === 0) {
Simon Huntfc274c92014-11-11 11:05:46 -0800271 v.alert('Oops!\n\nNo event #' + evn + ' found.');
Simon Hunt7cd48f32014-11-09 23:42:50 -0800272 return;
273 }
274 frame = stack.shift();
275
276 d3.json(frame.url, function (err, data) {
Simon Hunt99c13842014-11-06 18:23:12 -0800277 if (err) {
Simon Hunt56d51852014-11-09 13:03:35 -0800278 if (err.status === 404) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800279 // if we didn't find the data, try the next stack frame
280 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800281 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800282 v.alert('non-404 error:\n\n' + frame.url + '\n\n' + err);
Simon Hunt56d51852014-11-09 13:03:35 -0800283 }
Simon Hunt99c13842014-11-06 18:23:12 -0800284 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800285 testDebug('loaded: ' + frame.url);
Simon Hunt1712ed82014-11-17 12:56:00 -0800286 wsTrace('test', JSON.stringify(data));
Simon Hunt7cd48f32014-11-09 23:42:50 -0800287 frame.cb(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800288 }
289 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800290
Simon Hunt56d51852014-11-09 13:03:35 -0800291 }
Simon Hunt50128c02014-11-08 13:36:15 -0800292
Simon Hunt56d51852014-11-09 13:03:35 -0800293 function handleUiEvent(data) {
Simon Huntbb282f52014-11-10 11:08:19 -0800294 scenario.view.alert('UI Tx: ' + data.event + '\n\n' +
295 JSON.stringify(data));
Simon Hunt56d51852014-11-09 13:03:35 -0800296 }
297
298 function injectStartupEvents(view) {
299 var last = scenario.params.lastAuto || 0;
300 if (abortIfLive()) { return; }
301
302 while (scenario.evNumber < last) {
Simon Hunt1a9eff92014-11-07 11:06:34 -0800303 injectTestEvent(view);
304 }
305 }
306
Simon Hunt934c3ce2014-11-05 11:45:07 -0800307 function toggleBg() {
308 var vis = bgImg.style('visibility');
309 bgImg.style('visibility', (vis === 'hidden') ? 'visible' : 'hidden');
310 }
311
Simon Hunt99c13842014-11-06 18:23:12 -0800312 function cycleLabels() {
Simon Huntbb282f52014-11-10 11:08:19 -0800313 deviceLabelIndex = (deviceLabelIndex === network.deviceLabelCount - 1)
314 ? 0 : deviceLabelIndex + 1;
Simon Hunt5f36d342014-11-08 21:33:14 -0800315
Simon Hunt99c13842014-11-06 18:23:12 -0800316 network.nodes.forEach(function (d) {
Simon Huntbb282f52014-11-10 11:08:19 -0800317 if (d.class === 'device') {
318 updateDeviceLabel(d);
319 }
Simon Hunt99c13842014-11-06 18:23:12 -0800320 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800321 }
322
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -0800323 function toggleHover(view) {
Thomas Vachuska29617e52014-11-20 03:17:46 -0800324 hoverMode++;
325 if (hoverMode === hoverModes.length) {
326 hoverMode = 0;
327 }
328 console.log('Hover Mode:' + hoverMode + ': ' + hoverModes[hoverMode]);
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -0800329 }
330
Simon Hunt934c3ce2014-11-05 11:45:07 -0800331 function togglePorts(view) {
Simon Hunt50128c02014-11-08 13:36:15 -0800332 view.alert('togglePorts() callback')
Simon Hunt934c3ce2014-11-05 11:45:07 -0800333 }
334
Simon Hunt6ac93f32014-11-13 12:17:27 -0800335 function unpin() {
336 if (hovered) {
337 hovered.fixed = false;
338 hovered.el.classed('fixed', false);
339 network.force.resume();
340 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800341 }
342
Simon Hunt9462e8c2014-11-14 17:28:09 -0800343 function handleEscape(view) {
344 if (oiShowMaster) {
345 cancelAffinity();
346 } else {
347 deselectAll();
348 }
349 }
350
Simon Hunt934c3ce2014-11-05 11:45:07 -0800351 // ==============================
352 // Radio Button Callbacks
353
Simon Hunta5e89142014-11-14 07:00:33 -0800354 var layerLookup = {
355 host: {
356 endstation: 'pkt', // default, if host event does not define type
Thomas Vachuska89543292014-11-19 11:28:33 -0800357 router: 'pkt',
Simon Hunta5e89142014-11-14 07:00:33 -0800358 bgpSpeaker: 'pkt'
359 },
360 device: {
361 switch: 'pkt',
362 roadm: 'opt'
363 },
364 link: {
365 hostLink: 'pkt',
366 direct: 'pkt',
Simon Hunt8257f4c2014-11-16 19:34:54 -0800367 indirect: '',
368 tunnel: '',
Simon Hunta5e89142014-11-14 07:00:33 -0800369 optical: 'opt'
370 }
371 };
372
373 function inLayer(d, layer) {
Simon Hunt8257f4c2014-11-16 19:34:54 -0800374 var type = d.class === 'link' ? d.type() : d.type,
375 look = layerLookup[d.class],
376 lyr = look && look[type];
Simon Hunta5e89142014-11-14 07:00:33 -0800377 return lyr === layer;
378 }
379
380 function unsuppressLayer(which) {
381 node.each(function (d) {
382 var node = d.el;
383 if (inLayer(d, which)) {
384 node.classed('suppressed', false);
385 }
386 });
387
388 link.each(function (d) {
389 var link = d.el;
390 if (inLayer(d, which)) {
391 link.classed('suppressed', false);
392 }
393 });
394 }
395
Simon Hunt9462e8c2014-11-14 17:28:09 -0800396 function suppressLayers(b) {
397 node.classed('suppressed', b);
398 link.classed('suppressed', b);
Simon Hunt142d0032014-11-04 20:13:09 -0800399// d3.selectAll('svg .port').classed('inactive', false);
400// d3.selectAll('svg .portText').classed('inactive', false);
Simon Hunt195cb382014-11-03 17:50:51 -0800401 }
402
Simon Hunt9462e8c2014-11-14 17:28:09 -0800403 function showAllLayers() {
404 suppressLayers(false);
405 }
406
Simon Hunt195cb382014-11-03 17:50:51 -0800407 function showPacketLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800408 node.classed('suppressed', true);
409 link.classed('suppressed', true);
410 unsuppressLayer('pkt');
Simon Hunt195cb382014-11-03 17:50:51 -0800411 }
412
413 function showOpticalLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800414 node.classed('suppressed', true);
415 link.classed('suppressed', true);
416 unsuppressLayer('opt');
Simon Hunt195cb382014-11-03 17:50:51 -0800417 }
418
Simon Hunt9462e8c2014-11-14 17:28:09 -0800419 function restoreLayerState() {
420 layerBtnDispatch[layerBtnSet.selected()]();
421 }
422
Simon Hunt142d0032014-11-04 20:13:09 -0800423 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800424 // Private functions
425
Simon Hunt99c13842014-11-06 18:23:12 -0800426 function safeId(s) {
427 return s.replace(/[^a-z0-9]/gi, '-');
428 }
429
Simon Huntc7ee0662014-11-05 16:44:37 -0800430 // set the size of the given element to that of the view (reduced if padded)
431 function setSize(el, view, pad) {
432 var padding = pad ? pad * 2 : 0;
Simon Hunt934c3ce2014-11-05 11:45:07 -0800433 el.attr({
Simon Huntc7ee0662014-11-05 16:44:37 -0800434 width: view.width() - padding,
435 height: view.height() - padding
Simon Hunt934c3ce2014-11-05 11:45:07 -0800436 });
437 }
438
Simon Hunt8257f4c2014-11-16 19:34:54 -0800439 function makeNodeKey(d, what) {
440 var port = what + 'Port';
441 return d[what] + '/' + d[port];
442 }
443
444 function makeLinkKey(d, flipped) {
445 var one = flipped ? makeNodeKey(d, 'dst') : makeNodeKey(d, 'src'),
446 two = flipped ? makeNodeKey(d, 'src') : makeNodeKey(d, 'dst');
447 return one + '-' + two;
448 }
449
Simon Hunt269670f2014-11-17 16:17:43 -0800450 function findLinkById(id) {
451 // check to see if this is a reverse lookup, else default to given id
452 var key = network.revLinkToKey[id] || id;
453 return key && network.lookup[key];
454 }
455
Simon Hunt8257f4c2014-11-16 19:34:54 -0800456 function findLink(linkData, op) {
457 var key = makeLinkKey(linkData),
458 keyrev = makeLinkKey(linkData, 1),
459 link = network.lookup[key],
460 linkRev = network.lookup[keyrev],
461 result = {},
462 ldata = link || linkRev,
463 rawLink;
464
465 if (op === 'add') {
466 if (link) {
467 // trying to add a link that we already know about
468 result.ldata = link;
469 result.badLogic = 'addLink: link already added';
470
471 } else if (linkRev) {
472 // we found the reverse of the link to be added
473 result.ldata = linkRev;
474 if (linkRev.fromTarget) {
475 result.badLogic = 'addLink: link already added';
476 }
477 }
478 } else if (op === 'update') {
479 if (!ldata) {
480 result.badLogic = 'updateLink: link not found';
481 } else {
482 rawLink = link ? ldata.fromSource : ldata.fromTarget;
483 result.updateWith = function (data) {
484 $.extend(rawLink, data);
485 restyleLinkElement(ldata);
486 }
487 }
488 } else if (op === 'remove') {
489 if (!ldata) {
490 result.badLogic = 'removeLink: link not found';
491 } else {
492 rawLink = link ? ldata.fromSource : ldata.fromTarget;
493
494 if (!rawLink) {
495 result.badLogic = 'removeLink: link not found';
496
497 } else {
498 result.removeRawLink = function () {
499 if (link) {
500 // remove fromSource
501 ldata.fromSource = null;
502 if (ldata.fromTarget) {
503 // promote target into source position
504 ldata.fromSource = ldata.fromTarget;
505 ldata.fromTarget = null;
506 ldata.key = keyrev;
507 delete network.lookup[key];
508 network.lookup[keyrev] = ldata;
Simon Hunt269670f2014-11-17 16:17:43 -0800509 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800510 }
511 } else {
512 // remove fromTarget
513 ldata.fromTarget = null;
Simon Hunt269670f2014-11-17 16:17:43 -0800514 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800515 }
516 if (ldata.fromSource) {
517 restyleLinkElement(ldata);
518 } else {
519 removeLinkElement(ldata);
520 }
521 }
522 }
523 }
524 }
525 return result;
526 }
527
528 function addLinkUpdate(ldata, link) {
529 // add link event, but we already have the reverse link installed
530 ldata.fromTarget = link;
Simon Hunt269670f2014-11-17 16:17:43 -0800531 network.revLinkToKey[link.id] = ldata.key;
Simon Hunt8257f4c2014-11-16 19:34:54 -0800532 restyleLinkElement(ldata);
533 }
534
535 var allLinkTypes = 'direct indirect optical tunnel',
536 defaultLinkType = 'direct';
537
538 function restyleLinkElement(ldata) {
539 // this fn's job is to look at raw links and decide what svg classes
540 // need to be applied to the line element in the DOM
541 var el = ldata.el,
542 type = ldata.type(),
543 lw = ldata.linkWidth(),
544 online = ldata.online();
545
546 el.classed('link', true);
547 el.classed('inactive', !online);
548 el.classed(allLinkTypes, false);
549 if (type) {
550 el.classed(type, true);
551 }
552 el.transition()
553 .duration(1000)
Thomas Vachuska89543292014-11-19 11:28:33 -0800554 .attr('stroke-width', linkScale(lw))
555 .attr('stroke', config.topo.linkBaseColor);
Simon Hunt8257f4c2014-11-16 19:34:54 -0800556 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800557
Simon Hunt99c13842014-11-06 18:23:12 -0800558 // ==============================
559 // Event handlers for server-pushed events
560
Simon Huntbb282f52014-11-10 11:08:19 -0800561 function logicError(msg) {
562 // TODO, report logic error to server, via websock, so it can be logged
Simon Huntcb56cff2014-11-17 11:42:26 -0800563 //network.view.alert('Logic Error:\n\n' + msg);
Simon Huntfc274c92014-11-11 11:05:46 -0800564 console.warn(msg);
Simon Huntbb282f52014-11-10 11:08:19 -0800565 }
566
Simon Hunt99c13842014-11-06 18:23:12 -0800567 var eventDispatch = {
Simon Hunta5e89142014-11-14 07:00:33 -0800568 addInstance: addInstance,
Simon Hunt99c13842014-11-06 18:23:12 -0800569 addDevice: addDevice,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800570 addLink: addLink,
Simon Hunt56d51852014-11-09 13:03:35 -0800571 addHost: addHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800572
Simon Huntfcfb46c2014-11-19 12:53:38 -0800573 updateInstance: updateInstance,
Simon Huntbb282f52014-11-10 11:08:19 -0800574 updateDevice: updateDevice,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800575 updateLink: updateLink,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800576 updateHost: updateHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800577
Simon Huntd72bc702014-11-13 18:38:04 -0800578 removeInstance: stillToImplement,
Simon Huntbb282f52014-11-10 11:08:19 -0800579 removeDevice: stillToImplement,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800580 removeLink: removeLink,
Simon Hunt44031102014-11-11 13:20:36 -0800581 removeHost: removeHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800582
Simon Hunt61d04042014-11-11 17:27:16 -0800583 showDetails: showDetails,
Simon Huntb53e0682014-11-12 13:32:01 -0800584 showTraffic: showTraffic
Simon Hunt99c13842014-11-06 18:23:12 -0800585 };
586
Simon Hunta5e89142014-11-14 07:00:33 -0800587 function addInstance(data) {
588 evTrace(data);
589 var inst = data.payload,
590 id = inst.id;
591 if (onosInstances[id]) {
592 logicError('ONOS instance already added: ' + id);
593 return;
594 }
595 onosInstances[id] = inst;
596 onosOrder.push(inst);
597 updateInstances();
598 }
599
Simon Hunt99c13842014-11-06 18:23:12 -0800600 function addDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800601 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800602 var device = data.payload,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800603 nodeData = createDeviceNode(device);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800604 network.nodes.push(nodeData);
605 network.lookup[nodeData.id] = nodeData;
Simon Hunt99c13842014-11-06 18:23:12 -0800606 updateNodes();
607 network.force.start();
608 }
609
Simon Hunt99c13842014-11-06 18:23:12 -0800610 function addLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800611 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800612 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800613 result = findLink(link, 'add'),
614 bad = result.badLogic,
615 ldata = result.ldata;
616
617 if (bad) {
618 logicError(bad + ': ' + link.id);
619 return;
620 }
621
622 if (ldata) {
623 // we already have a backing store link for src/dst nodes
624 addLinkUpdate(ldata, link);
625 return;
626 }
627
628 // no backing store link yet
629 ldata = createLink(link);
630 if (ldata) {
631 network.links.push(ldata);
632 network.lookup[ldata.key] = ldata;
Simon Hunt99c13842014-11-06 18:23:12 -0800633 updateLinks();
634 network.force.start();
635 }
636 }
637
Simon Hunt56d51852014-11-09 13:03:35 -0800638 function addHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800639 evTrace(data);
Simon Hunt56d51852014-11-09 13:03:35 -0800640 var host = data.payload,
641 node = createHostNode(host),
642 lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800643 network.nodes.push(node);
644 network.lookup[host.id] = node;
645 updateNodes();
646
647 lnk = createHostLink(host);
648 if (lnk) {
Simon Hunt44031102014-11-11 13:20:36 -0800649 node.linkData = lnk; // cache ref on its host
Simon Hunt56d51852014-11-09 13:03:35 -0800650 network.links.push(lnk);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800651 network.lookup[host.ingress] = lnk;
652 network.lookup[host.egress] = lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800653 updateLinks();
654 }
655 network.force.start();
656 }
657
Simon Hunt44031102014-11-11 13:20:36 -0800658 // TODO: fold updateX(...) methods into one base method; remove duplication
Simon Hunt56a2ea42014-11-19 12:39:31 -0800659
660 function updateInstance(data) {
661 evTrace(data);
662 var inst = data.payload,
663 id = inst.id,
664 instData = onosInstances[id];
665 if (instData) {
666 $.extend(instData, inst);
667 updateInstances();
Simon Hunt56a2ea42014-11-19 12:39:31 -0800668 } else {
669 logicError('updateInstance lookup fail. ID = "' + id + '"');
670 }
671 }
672
Simon Huntbb282f52014-11-10 11:08:19 -0800673 function updateDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800674 evTrace(data);
Simon Huntbb282f52014-11-10 11:08:19 -0800675 var device = data.payload,
676 id = device.id,
677 nodeData = network.lookup[id];
678 if (nodeData) {
679 $.extend(nodeData, device);
680 updateDeviceState(nodeData);
681 } else {
682 logicError('updateDevice lookup fail. ID = "' + id + '"');
683 }
684 }
685
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800686 function updateLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800687 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800688 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800689 result = findLink(link, 'update'),
690 bad = result.badLogic;
691 if (bad) {
692 logicError(bad + ': ' + link.id);
693 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800694 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800695 result.updateWith(link);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800696 }
697
Simon Hunt7cd48f32014-11-09 23:42:50 -0800698 function updateHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800699 evTrace(data);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800700 var host = data.payload,
Simon Huntbb282f52014-11-10 11:08:19 -0800701 id = host.id,
702 hostData = network.lookup[id];
703 if (hostData) {
704 $.extend(hostData, host);
705 updateHostState(hostData);
706 } else {
707 logicError('updateHost lookup fail. ID = "' + id + '"');
708 }
Simon Hunt7cd48f32014-11-09 23:42:50 -0800709 }
710
Simon Hunt44031102014-11-11 13:20:36 -0800711 // TODO: fold removeX(...) methods into base method - remove dup code
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800712 function removeLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800713 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800714 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800715 result = findLink(link, 'remove'),
716 bad = result.badLogic;
717 if (bad) {
718 logicError(bad + ': ' + link.id);
719 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800720 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800721 result.removeRawLink();
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800722 }
723
Simon Hunt44031102014-11-11 13:20:36 -0800724 function removeHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800725 evTrace(data);
Simon Hunt44031102014-11-11 13:20:36 -0800726 var host = data.payload,
727 id = host.id,
728 hostData = network.lookup[id];
729 if (hostData) {
730 removeHostElement(hostData);
731 } else {
732 logicError('removeHost lookup fail. ID = "' + id + '"');
733 }
734 }
735
Simon Hunt61d04042014-11-11 17:27:16 -0800736 function showDetails(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800737 evTrace(data);
Simon Hunt61d04042014-11-11 17:27:16 -0800738 populateDetails(data.payload);
739 detailPane.show();
740 }
741
Simon Huntb53e0682014-11-12 13:32:01 -0800742 function showTraffic(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800743 evTrace(data);
Thomas Vachuska4731f122014-11-20 04:56:19 -0800744 var paths = data.payload.paths,
745 hasTraffic = false;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800746
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800747 // Revert any links hilighted previously.
Thomas Vachuska4731f122014-11-20 04:56:19 -0800748 link.style('stroke-width', null)
Thomas Vachuskaa3148a72014-11-19 21:38:35 -0800749 .classed('primary secondary animated optical', false);
Simon Hunte2575b62014-11-18 15:25:53 -0800750 // Remove all previous labels.
751 removeLinkLabels();
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800752
Simon Hunte2575b62014-11-18 15:25:53 -0800753 // Now hilight all links in the paths payload, and attach
754 // labels to them, if they are defined.
Simon Hunta255a2c2014-11-13 22:29:35 -0800755 paths.forEach(function (p) {
Simon Hunte2575b62014-11-18 15:25:53 -0800756 var n = p.links.length,
757 i,
758 ldata;
759
Thomas Vachuska4731f122014-11-20 04:56:19 -0800760 hasTraffic = hasTraffic || p.traffic;
Simon Hunte2575b62014-11-18 15:25:53 -0800761 for (i=0; i<n; i++) {
762 ldata = findLinkById(p.links[i]);
Thomas Vachuska4731f122014-11-20 04:56:19 -0800763 if (ldata && ldata.el) {
Simon Hunte2575b62014-11-18 15:25:53 -0800764 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 });
Thomas Vachuska4731f122014-11-20 04:56:19 -0800769
Simon Hunte2575b62014-11-18 15:25:53 -0800770 updateLinks();
Thomas Vachuska4731f122014-11-20 04:56:19 -0800771
772 if (hasTraffic && !antTimer) {
773 startAntTimer();
774 } else if (!hasTraffic && antTimer) {
775 stopAntTimer();
776 }
Simon Huntb53e0682014-11-12 13:32:01 -0800777 }
778
Simon Hunt56d51852014-11-09 13:03:35 -0800779 // ...............................
780
781 function stillToImplement(data) {
782 var p = data.payload;
783 note(data.event, p.id);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800784 network.view.alert('Not yet implemented: "' + data.event + '"');
Simon Hunt56d51852014-11-09 13:03:35 -0800785 }
Simon Hunt99c13842014-11-06 18:23:12 -0800786
787 function unknownEvent(data) {
Simon Hunt50128c02014-11-08 13:36:15 -0800788 network.view.alert('Unknown event type: "' + data.event + '"');
Simon Hunt99c13842014-11-06 18:23:12 -0800789 }
790
791 function handleServerEvent(data) {
792 var fn = eventDispatch[data.event] || unknownEvent;
793 fn(data);
794 }
795
796 // ==============================
Simon Hunt61d04042014-11-11 17:27:16 -0800797 // Out-going messages...
798
Simon Huntb53e0682014-11-12 13:32:01 -0800799 function userFeedback(msg) {
800 // for now, use the alert pane as is. Maybe different alert style in
801 // the future (centered on view; dismiss button?)
802 network.view.alert(msg);
803 }
804
805 function nSel() {
806 return selectOrder.length;
807 }
Simon Hunt61d04042014-11-11 17:27:16 -0800808 function getSel(idx) {
809 return selections[selectOrder[idx]];
810 }
Simon Huntb53e0682014-11-12 13:32:01 -0800811 function getSelId(idx) {
812 return getSel(idx).obj.id;
813 }
814 function allSelectionsClass(cls) {
815 for (var i=0, n=nSel(); i<n; i++) {
816 if (getSel(i).obj.class !== cls) {
817 return false;
818 }
819 }
820 return true;
821 }
Simon Hunt61d04042014-11-11 17:27:16 -0800822
Simon Hunt61d04042014-11-11 17:27:16 -0800823 // request details for the selected element
Simon Huntd72bc702014-11-13 18:38:04 -0800824 // invoked from selection of a single node.
Simon Hunt61d04042014-11-11 17:27:16 -0800825 function requestDetails() {
826 var data = getSel(0).obj,
827 payload = {
828 id: data.id,
829 class: data.class
830 };
831 sendMessage('requestDetails', payload);
832 }
833
Simon Huntd72bc702014-11-13 18:38:04 -0800834 function addIntentAction() {
835 sendMessage('addHostIntent', {
836 one: getSelId(0),
Thomas Vachuska82f2c622014-11-17 12:23:18 -0800837 two: getSelId(1),
838 ids: [ getSelId(0), getSelId(1) ]
Simon Huntd72bc702014-11-13 18:38:04 -0800839 });
840 }
841
842 function showTrafficAction() {
Thomas Vachuska29617e52014-11-20 03:17:46 -0800843 // force intents hover mode
844 hoverMode = 1;
845 showSelectTraffic();
846 }
847
848 function showSelectTraffic() {
Simon Huntd72bc702014-11-13 18:38:04 -0800849 // if nothing is hovered over, and nothing selected, send cancel request
850 if (!hovered && nSel() === 0) {
851 sendMessage('cancelTraffic', {});
852 return;
853 }
854
855 // NOTE: hover is only populated if "show traffic on hover" is
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800856 // toggled on, and the item hovered is a host or a device...
857 var hoverId = (trafficHover() && hovered &&
858 (hovered.class === 'host' || hovered.class === 'device'))
Simon Huntd72bc702014-11-13 18:38:04 -0800859 ? hovered.id : '';
860 sendMessage('requestTraffic', {
861 ids: selectOrder,
862 hover: hoverId
863 });
864 }
865
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800866 function showAllTrafficAction() {
867 sendMessage('requestAllTraffic', {});
868 }
869
Thomas Vachuska29617e52014-11-20 03:17:46 -0800870 function showDeviceLinkFlowsAction() {
871 // force intents hover mode
872 hoverMode = 2;
873 showDeviceLinkFlows();
874 }
875
876 function showDeviceLinkFlows() {
877 // if nothing is hovered over, and nothing selected, send cancel request
878 if (!hovered && nSel() === 0) {
879 sendMessage('cancelTraffic', {});
880 return;
881 }
882 var hoverId = (flowsHover() && hovered && hovered.class === 'device') ?
883 hovered.id : '';
884 sendMessage('requestDeviceLinkFlows', {
885 ids: selectOrder,
886 hover: hoverId
887 });
888 }
Simon Huntd72bc702014-11-13 18:38:04 -0800889
Simon Hunta6a9fe72014-11-20 11:17:12 -0800890 // TODO: these should be moved out to utility module.
Simon Hunt95908012014-11-20 10:20:26 -0800891 function stripPx(s) {
892 return s.replace(/px$/,'');
893 }
Simon Hunta6a9fe72014-11-20 11:17:12 -0800894
895 function appendGlyph(svg, ox, oy, dim, iid) {
896 svg.append('use').attr({
897 class: 'glyphIcon',
898 transform: translate(ox,oy),
899 'xlink:href': iid,
900 width: dim,
901 height: dim
902
903 });
904 }
905
Simon Hunt61d04042014-11-11 17:27:16 -0800906 // ==============================
Simon Hunta5e89142014-11-14 07:00:33 -0800907 // onos instance panel functions
908
909 function updateInstances() {
910 var onoses = oiBox.el.selectAll('.onosInst')
911 .data(onosOrder, function (d) { return d.id; });
912
913 // operate on existing onoses if necessary
Simon Huntfcfb46c2014-11-19 12:53:38 -0800914 onoses.classed('online', function (d) { return d.online; });
Simon Hunta5e89142014-11-14 07:00:33 -0800915
916 var entering = onoses.enter()
917 .append('div')
918 .attr('class', 'onosInst')
919 .classed('online', function (d) { return d.online; })
Simon Hunt9c15eca2014-11-15 18:37:59 -0800920 .on('click', clickInst);
921
922 entering.each(function (d, i) {
923 var el = d3.select(this),
924 img;
Simon Hunt95908012014-11-20 10:20:26 -0800925 var css = window.getComputedStyle(this),
926 w = stripPx(css.width),
927 h = stripPx(css.height) / 2;
Simon Hunt9c15eca2014-11-15 18:37:59 -0800928
Simon Hunt95908012014-11-20 10:20:26 -0800929 var svg = el.append('svg').attr({
930 width: w,
931 height: h
932 });
933 var dim = 30;
Simon Hunta6a9fe72014-11-20 11:17:12 -0800934 appendGlyph(svg, 2, 2, 30, '#node');
Simon Hunt9c15eca2014-11-15 18:37:59 -0800935
936 $('<div>').attr('class', 'onosTitle').text(d.id).appendTo(el);
937
938 // is the UI attached to this instance?
939 // TODO: need uiAttached boolean in instance data
Simon Hunta6a9fe72014-11-20 11:17:12 -0800940 // TODO: use SVG glyph, not png..
Simon Hunt9c15eca2014-11-15 18:37:59 -0800941 //if (d.uiAttached) {
942 if (i === 0) {
943 $('<img src="img/ui.png">').attr('class','ui').appendTo(el);
944 }
945 });
Simon Hunta5e89142014-11-14 07:00:33 -0800946
947 // operate on existing + new onoses here
948
949 // the departed...
950 var exiting = onoses.exit()
951 .transition()
952 .style('opacity', 0)
953 .remove();
954 }
955
Simon Hunt9462e8c2014-11-14 17:28:09 -0800956 function clickInst(d) {
957 var el = d3.select(this),
958 aff = el.classed('affinity');
959 if (!aff) {
960 setAffinity(el, d);
961 } else {
962 cancelAffinity();
963 }
964 }
965
966 function setAffinity(el, d) {
967 d3.selectAll('.onosInst')
968 .classed('mastership', true)
969 .classed('affinity', false);
970 el.classed('affinity', true);
971
972 suppressLayers(true);
973 node.each(function (n) {
974 if (n.master === d.id) {
975 n.el.classed('suppressed', false);
976 }
977 });
978 oiShowMaster = true;
979 }
980
981 function cancelAffinity() {
982 d3.selectAll('.onosInst')
983 .classed('mastership affinity', false);
984 restoreLayerState();
985 oiShowMaster = false;
986 }
987
Simon Hunta5e89142014-11-14 07:00:33 -0800988 // ==============================
Simon Hunt99c13842014-11-06 18:23:12 -0800989 // force layout modification functions
990
991 function translate(x, y) {
992 return 'translate(' + x + ',' + y + ')';
993 }
994
Simon Hunte2575b62014-11-18 15:25:53 -0800995 function rotate(deg) {
996 return 'rotate(' + deg + ')';
997 }
998
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800999 function missMsg(what, id) {
1000 return '\n[' + what + '] "' + id + '" missing ';
1001 }
1002
1003 function linkEndPoints(srcId, dstId) {
1004 var srcNode = network.lookup[srcId],
1005 dstNode = network.lookup[dstId],
1006 sMiss = !srcNode ? missMsg('src', srcId) : '',
1007 dMiss = !dstNode ? missMsg('dst', dstId) : '';
1008
1009 if (sMiss || dMiss) {
1010 logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
1011 return null;
1012 }
1013 return {
1014 source: srcNode,
1015 target: dstNode,
1016 x1: srcNode.x,
1017 y1: srcNode.y,
1018 x2: dstNode.x,
1019 y2: dstNode.y
1020 };
1021 }
1022
Simon Hunt56d51852014-11-09 13:03:35 -08001023 function createHostLink(host) {
1024 var src = host.id,
1025 dst = host.cp.device,
Simon Hunt7cd48f32014-11-09 23:42:50 -08001026 id = host.ingress,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001027 lnk = linkEndPoints(src, dst);
Simon Hunt56d51852014-11-09 13:03:35 -08001028
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001029 if (!lnk) {
Simon Hunt56d51852014-11-09 13:03:35 -08001030 return null;
1031 }
1032
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001033 // Synthesize link ...
1034 $.extend(lnk, {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001035 key: id,
Simon Hunt56d51852014-11-09 13:03:35 -08001036 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -08001037
1038 type: function () { return 'hostLink'; },
1039 // TODO: ideally, we should see if our edge switch is online...
1040 online: function () { return true; },
1041 linkWidth: function () { return 1; }
Simon Hunt7cd48f32014-11-09 23:42:50 -08001042 });
Simon Hunt99c13842014-11-06 18:23:12 -08001043 return lnk;
1044 }
1045
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001046 function createLink(link) {
Simon Hunta6a9fe72014-11-20 11:17:12 -08001047 var lnk = linkEndPoints(link.src, link.dst);
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001048
1049 if (!lnk) {
1050 return null;
1051 }
1052
Simon Hunt8257f4c2014-11-16 19:34:54 -08001053 $.extend(lnk, {
1054 key: link.id,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001055 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -08001056 fromSource: link,
1057
1058 // functions to aggregate dual link state
1059 type: function () {
1060 var s = lnk.fromSource,
1061 t = lnk.fromTarget;
1062 return (s && s.type) || (t && t.type) || defaultLinkType;
1063 },
1064 online: function () {
1065 var s = lnk.fromSource,
1066 t = lnk.fromTarget;
1067 return (s && s.online) || (t && t.online);
1068 },
1069 linkWidth: function () {
1070 var s = lnk.fromSource,
1071 t = lnk.fromTarget,
1072 ws = (s && s.linkWidth) || 0,
1073 wt = (t && t.linkWidth) || 0;
1074 return Math.max(ws, wt);
1075 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001076 });
1077 return lnk;
Simon Hunt1a9eff92014-11-07 11:06:34 -08001078 }
1079
Simon Hunte2575b62014-11-18 15:25:53 -08001080 function removeLinkLabels() {
1081 network.links.forEach(function (d) {
1082 d.label = '';
1083 });
1084 }
1085
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001086 var widthRatio = 1.4,
1087 linkScale = d3.scale.linear()
1088 .domain([1, 12])
1089 .range([widthRatio, 12 * widthRatio])
1090 .clamp(true);
1091
Simon Hunt99c13842014-11-06 18:23:12 -08001092 function updateLinks() {
1093 link = linkG.selectAll('.link')
Simon Hunt8257f4c2014-11-16 19:34:54 -08001094 .data(network.links, function (d) { return d.key; });
Simon Hunt99c13842014-11-06 18:23:12 -08001095
1096 // operate on existing links, if necessary
1097 // link .foo() .bar() ...
1098
1099 // operate on entering links:
1100 var entering = link.enter()
1101 .append('line')
1102 .attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001103 x1: function (d) { return d.x1; },
1104 y1: function (d) { return d.y1; },
1105 x2: function (d) { return d.x2; },
1106 y2: function (d) { return d.y2; },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001107 stroke: config.topo.linkInColor,
1108 'stroke-width': config.topo.linkInWidth
Simon Hunt99c13842014-11-06 18:23:12 -08001109 });
1110
1111 // augment links
Simon Hunt7cd48f32014-11-09 23:42:50 -08001112 entering.each(function (d) {
1113 var link = d3.select(this);
1114 // provide ref to element selection from backing data....
1115 d.el = link;
Simon Hunt8257f4c2014-11-16 19:34:54 -08001116 restyleLinkElement(d);
Simon Hunt7cd48f32014-11-09 23:42:50 -08001117 });
Thomas Vachuska4830d392014-11-09 17:09:56 -08001118
1119 // operate on both existing and new links, if necessary
1120 //link .foo() .bar() ...
1121
Simon Hunte2575b62014-11-18 15:25:53 -08001122 // apply or remove labels
1123 var labelData = getLabelData();
1124 applyLinkLabels(labelData);
1125
Thomas Vachuska4830d392014-11-09 17:09:56 -08001126 // operate on exiting links:
Thomas Vachuska4830d392014-11-09 17:09:56 -08001127 link.exit()
Simon Hunt13bf9c82014-11-18 07:26:44 -08001128 .attr('stroke-dasharray', '3, 3')
1129 .style('opacity', 0.5)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001130 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -08001131 .duration(1500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001132 .attr({
Simon Hunt13bf9c82014-11-18 07:26:44 -08001133 'stroke-dasharray': '3, 12',
1134 stroke: config.topo.linkOutColor,
1135 'stroke-width': config.topo.linkOutWidth
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001136 })
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001137 .style('opacity', 0.0)
Thomas Vachuska4830d392014-11-09 17:09:56 -08001138 .remove();
Simon Hunte2575b62014-11-18 15:25:53 -08001139
1140 // NOTE: invoke a single tick to force the labels to position
1141 // onto their links.
1142 tick();
1143 }
1144
1145 function getLabelData() {
1146 // create the backing data for showing labels..
1147 var data = [];
1148 link.each(function (d) {
1149 if (d.label) {
1150 data.push({
1151 id: 'lab-' + d.key,
1152 key: d.key,
1153 label: d.label,
1154 ldata: d
1155 });
1156 }
1157 });
1158 return data;
1159 }
1160
1161 var linkLabelOffset = '0.3em';
1162
1163 function applyLinkLabels(data) {
1164 var entering;
1165
1166 linkLabel = linkLabelG.selectAll('.linkLabel')
1167 .data(data, function (d) { return d.id; });
1168
Simon Hunt56a2ea42014-11-19 12:39:31 -08001169 // for elements already existing, we need to update the text
1170 // and adjust the rectangle size to fit
1171 linkLabel.each(function (d) {
1172 var el = d3.select(this),
1173 rect = el.select('rect'),
1174 text = el.select('text');
1175 text.text(d.label);
1176 rect.attr(rectAroundText(el));
1177 });
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -08001178
Simon Hunte2575b62014-11-18 15:25:53 -08001179 entering = linkLabel.enter().append('g')
1180 .classed('linkLabel', true)
1181 .attr('id', function (d) { return d.id; });
1182
1183 entering.each(function (d) {
1184 var el = d3.select(this),
1185 rect,
1186 text,
1187 parms = {
1188 x1: d.ldata.x1,
1189 y1: d.ldata.y1,
1190 x2: d.ldata.x2,
1191 y2: d.ldata.y2
1192 };
1193
1194 d.el = el;
1195 rect = el.append('rect');
1196 text = el.append('text').text(d.label);
1197 rect.attr(rectAroundText(el));
1198 text.attr('dy', linkLabelOffset);
1199
1200 el.attr('transform', transformLabel(parms));
1201 });
1202
1203 // Remove any links that are no longer required.
1204 linkLabel.exit().remove();
1205 }
1206
1207 function rectAroundText(el) {
1208 var text = el.select('text'),
1209 box = text.node().getBBox();
1210
1211 // translate the bbox so that it is centered on [x,y]
1212 box.x = -box.width / 2;
1213 box.y = -box.height / 2;
1214
1215 // add padding
1216 box.x -= 1;
1217 box.width += 2;
1218 return box;
1219 }
1220
1221 function transformLabel(p) {
1222 var dx = p.x2 - p.x1,
1223 dy = p.y2 - p.y1,
1224 xMid = dx/2 + p.x1,
1225 yMid = dy/2 + p.y1;
Simon Hunte2575b62014-11-18 15:25:53 -08001226 return translate(xMid, yMid);
Simon Hunt99c13842014-11-06 18:23:12 -08001227 }
1228
1229 function createDeviceNode(device) {
1230 // start with the object as is
1231 var node = device,
Simon Huntbb282f52014-11-10 11:08:19 -08001232 type = device.type,
Simon Huntc72967b2014-11-20 09:21:42 -08001233 svgCls = type ? 'node device ' + type : 'node device',
1234 labels = device.labels || [];
1235
1236 labels.unshift(''); // add 'no-label' to front of cycle
Simon Hunt99c13842014-11-06 18:23:12 -08001237
1238 // Augment as needed...
1239 node.class = 'device';
Simon Huntbb282f52014-11-10 11:08:19 -08001240 node.svgClass = device.online ? svgCls + ' online' : svgCls;
Simon Hunt99c13842014-11-06 18:23:12 -08001241 positionNode(node);
1242
1243 // cache label array length
Simon Huntc72967b2014-11-20 09:21:42 -08001244 // TODO: need a uiConfig event from the server to set things
1245 // like device labels count, host labels count, etc.
1246 // The current method (here) is a little fragile
Simon Hunt99c13842014-11-06 18:23:12 -08001247 network.deviceLabelCount = device.labels.length;
Simon Hunt99c13842014-11-06 18:23:12 -08001248 return node;
1249 }
1250
Simon Hunt56d51852014-11-09 13:03:35 -08001251 function createHostNode(host) {
1252 // start with the object as is
1253 var node = host;
1254
1255 // Augment as needed...
1256 node.class = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001257 if (!node.type) {
Simon Hunteb1514d2014-11-20 09:57:29 -08001258 node.type = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001259 }
Simon Hunt7fa116d2014-11-17 14:16:55 -08001260 node.svgClass = 'node host ' + node.type;
Simon Hunt56d51852014-11-09 13:03:35 -08001261 positionNode(node);
1262
1263 // cache label array length
1264 network.hostLabelCount = host.labels.length;
Simon Hunt56d51852014-11-09 13:03:35 -08001265 return node;
1266 }
1267
Simon Hunt99c13842014-11-06 18:23:12 -08001268 function positionNode(node) {
1269 var meta = node.metaUi,
Simon Huntac9e24f2014-11-12 10:12:21 -08001270 x = meta && meta.x,
1271 y = meta && meta.y,
1272 xy;
Simon Hunt99c13842014-11-06 18:23:12 -08001273
Simon Huntac9e24f2014-11-12 10:12:21 -08001274 // If we have [x,y] already, use that...
Simon Hunt99c13842014-11-06 18:23:12 -08001275 if (x && y) {
1276 node.fixed = true;
Simon Huntac9e24f2014-11-12 10:12:21 -08001277 node.x = x;
1278 node.y = y;
1279 return;
Simon Hunt99c13842014-11-06 18:23:12 -08001280 }
Simon Huntac9e24f2014-11-12 10:12:21 -08001281
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001282 var location = node.location;
1283 if (location && location.type === 'latlng') {
1284 var coord = geoMapProjection([location.lng, location.lat]);
1285 node.fixed = true;
1286 node.x = coord[0];
1287 node.y = coord[1];
1288 return;
1289 }
1290
Simon Huntac9e24f2014-11-12 10:12:21 -08001291 // Note: Placing incoming unpinned nodes at exactly the same point
1292 // (center of the view) causes them to explode outwards when
1293 // the force layout kicks in. So, we spread them out a bit
1294 // initially, to provide a more serene layout convergence.
1295 // Additionally, if the node is a host, we place it near
1296 // the device it is connected to.
1297
1298 function spread(s) {
1299 return Math.floor((Math.random() * s) - s/2);
1300 }
1301
1302 function randDim(dim) {
1303 return dim / 2 + spread(dim * 0.7071);
1304 }
1305
1306 function rand() {
1307 return {
1308 x: randDim(network.view.width()),
1309 y: randDim(network.view.height())
1310 };
1311 }
1312
1313 function near(node) {
1314 var min = 12,
1315 dx = spread(12),
1316 dy = spread(12);
1317 return {
1318 x: node.x + min + dx,
1319 y: node.y + min + dy
1320 };
1321 }
1322
1323 function getDevice(cp) {
1324 var d = network.lookup[cp.device];
1325 return d || rand();
1326 }
1327
1328 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
1329 $.extend(node, xy);
Simon Hunt99c13842014-11-06 18:23:12 -08001330 }
1331
Thomas Vachuska89543292014-11-19 11:28:33 -08001332 function iconUrl(d) {
1333 return 'img/' + d.type + '.png';
Simon Hunt99c13842014-11-06 18:23:12 -08001334 }
1335
Simon Huntc72967b2014-11-20 09:21:42 -08001336 function iconGlyphUrl(d) {
1337 var which = d.type || 'unknown';
1338 return '#' + which;
1339 }
1340
Simon Hunt99c13842014-11-06 18:23:12 -08001341 // returns the newly computed bounding box of the rectangle
1342 function adjustRectToFitText(n) {
1343 var text = n.select('text'),
1344 box = text.node().getBBox(),
1345 lab = config.labels;
1346
1347 text.attr('text-anchor', 'middle')
1348 .attr('y', '-0.8em')
1349 .attr('x', lab.imgPad/2);
1350
1351 // translate the bbox so that it is centered on [x,y]
1352 box.x = -box.width / 2;
1353 box.y = -box.height / 2;
1354
1355 // add padding
1356 box.x -= (lab.padLR + lab.imgPad/2);
1357 box.width += lab.padLR * 2 + lab.imgPad;
1358 box.y -= lab.padTB;
1359 box.height += lab.padTB * 2;
1360
1361 return box;
1362 }
1363
Simon Hunt1a9eff92014-11-07 11:06:34 -08001364 function mkSvgClass(d) {
1365 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
1366 }
1367
Simon Hunt7cd48f32014-11-09 23:42:50 -08001368 function hostLabel(d) {
1369 var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0;
1370 return d.labels[idx];
1371 }
1372 function deviceLabel(d) {
1373 var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0;
1374 return d.labels[idx];
1375 }
Simon Huntc72967b2014-11-20 09:21:42 -08001376 function trimLabel(label) {
1377 return (label && label.trim()) || '';
1378 }
1379
1380 function emptyBox() {
1381 return {
1382 x: -2,
1383 y: -2,
1384 width: 4,
1385 height: 4
1386 };
Simon Hunt7cd48f32014-11-09 23:42:50 -08001387 }
1388
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001389 function updateDeviceLabel(d) {
Simon Huntc72967b2014-11-20 09:21:42 -08001390 var label = trimLabel(deviceLabel(d)),
1391 noLabel = !label,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001392 node = d.el,
Simon Huntc72967b2014-11-20 09:21:42 -08001393 box,
1394 dx,
1395 dy;
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001396
1397 node.select('text')
1398 .text(label)
1399 .style('opacity', 0)
1400 .transition()
1401 .style('opacity', 1);
1402
Simon Huntc72967b2014-11-20 09:21:42 -08001403 if (noLabel) {
1404 box = emptyBox();
1405 dx = -config.icons.device.dim/2;
1406 dy = -config.icons.device.dim/2;
1407 } else {
1408 box = adjustRectToFitText(node);
1409 dx = box.x + config.icons.xoff;
1410 dy = box.y + config.icons.yoff;
1411 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001412
1413 node.select('rect')
1414 .transition()
1415 .attr(box);
1416
Simon Huntc72967b2014-11-20 09:21:42 -08001417 node.select('g.deviceIcon')
Thomas Vachuska89543292014-11-19 11:28:33 -08001418 .transition()
Simon Huntc72967b2014-11-20 09:21:42 -08001419 .attr('transform', translate(dx, dy));
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001420 }
1421
1422 function updateHostLabel(d) {
1423 var label = hostLabel(d),
1424 host = d.el;
1425
1426 host.select('text').text(label);
1427 }
1428
Simon Hunta6a9fe72014-11-20 11:17:12 -08001429 // TODO: should be using updateNodes() to do the upates!
Simon Huntbb282f52014-11-10 11:08:19 -08001430 function updateDeviceState(nodeData) {
1431 nodeData.el.classed('online', nodeData.online);
1432 updateDeviceLabel(nodeData);
1433 // TODO: review what else might need to be updated
1434 }
1435
1436 function updateHostState(hostData) {
1437 updateHostLabel(hostData);
1438 // TODO: review what else might need to be updated
1439 }
1440
Simon Hunt6ac93f32014-11-13 12:17:27 -08001441 function nodeMouseOver(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001442 hovered = d;
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -08001443 if (trafficHover() && (d.class === 'host' || d.class === 'device')) {
Thomas Vachuska29617e52014-11-20 03:17:46 -08001444 showSelectTraffic();
1445 } else if (flowsHover() && (d.class === 'device')) {
1446 showDeviceLinkFlows();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001447 }
1448 }
1449
1450 function nodeMouseOut(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001451 hovered = null;
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -08001452 if (trafficHover() && (d.class === 'host' || d.class === 'device')) {
Thomas Vachuska29617e52014-11-20 03:17:46 -08001453 showSelectTraffic();
1454 } else if (flowsHover() && (d.class === 'device')) {
1455 showDeviceLinkFlows();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001456 }
1457 }
Simon Huntbb282f52014-11-10 11:08:19 -08001458
Simon Hunteb1514d2014-11-20 09:57:29 -08001459 function addHostIcon(node, radius, iid) {
Thomas Vachuska89543292014-11-19 11:28:33 -08001460 var dim = radius * 1.5,
1461 xlate = -dim / 2;
1462
Simon Hunteb1514d2014-11-20 09:57:29 -08001463 node.append('use').attr({
1464 class: 'glyphIcon hostIcon',
1465 transform: translate(xlate,xlate),
1466 'xlink:href': iid,
1467 width: dim,
1468 height: dim
1469 });
Thomas Vachuska89543292014-11-19 11:28:33 -08001470 }
1471
Simon Hunt99c13842014-11-06 18:23:12 -08001472 function updateNodes() {
1473 node = nodeG.selectAll('.node')
1474 .data(network.nodes, function (d) { return d.id; });
1475
Simon Huntc72967b2014-11-20 09:21:42 -08001476 // TODO: operate on existing nodes
Simon Hunt7cd48f32014-11-09 23:42:50 -08001477 // update host labels
Simon Hunt99c13842014-11-06 18:23:12 -08001478 //node .foo() .bar() ...
1479
1480 // operate on entering nodes:
1481 var entering = node.enter()
1482 .append('g')
1483 .attr({
1484 id: function (d) { return safeId(d.id); },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001485 class: mkSvgClass,
Simon Hunt99c13842014-11-06 18:23:12 -08001486 transform: function (d) { return translate(d.x, d.y); },
1487 opacity: 0
1488 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08001489 .call(network.drag)
Simon Hunt6ac93f32014-11-13 12:17:27 -08001490 .on('mouseover', nodeMouseOver)
1491 .on('mouseout', nodeMouseOut)
Simon Hunt99c13842014-11-06 18:23:12 -08001492 .transition()
1493 .attr('opacity', 1);
1494
1495 // augment device nodes...
1496 entering.filter('.device').each(function (d) {
1497 var node = d3.select(this),
Simon Huntc72967b2014-11-20 09:21:42 -08001498 label = trimLabel(deviceLabel(d)),
1499 noLabel = !label,
Simon Hunt99c13842014-11-06 18:23:12 -08001500 box;
1501
Simon Hunt7cd48f32014-11-09 23:42:50 -08001502 // provide ref to element from backing data....
1503 d.el = node;
1504
Simon Hunt99c13842014-11-06 18:23:12 -08001505 node.append('rect')
1506 .attr({
1507 'rx': 5,
1508 'ry': 5
1509 });
1510
1511 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001512 .text(label)
Simon Hunt99c13842014-11-06 18:23:12 -08001513 .attr('dy', '1.1em');
1514
1515 box = adjustRectToFitText(node);
1516
1517 node.select('rect')
1518 .attr(box);
1519
Simon Huntc72967b2014-11-20 09:21:42 -08001520 addDeviceIcon(node, box, noLabel, iconGlyphUrl(d));
Simon Hunt99c13842014-11-06 18:23:12 -08001521
Simon Huntc7ee0662014-11-05 16:44:37 -08001522 });
Simon Hunt934c3ce2014-11-05 11:45:07 -08001523
Thomas Vachuska89543292014-11-19 11:28:33 -08001524 // TODO: better place for this configuration state
1525 var defaultHostRadius = 9,
1526 hostRadius = {
1527 bgpSpeaker: 14,
1528 router: 14,
1529 host: 14
1530 },
Simon Hunteb1514d2014-11-20 09:57:29 -08001531 hostGlyphId = {
Thomas Vachuska89543292014-11-19 11:28:33 -08001532 bgpSpeaker: 'bgpSpeaker',
1533 router: 'router',
1534 host: 'host'
1535 };
1536
1537
Simon Hunt56d51852014-11-09 13:03:35 -08001538 // augment host nodes...
1539 entering.filter('.host').each(function (d) {
1540 var node = d3.select(this),
Thomas Vachuska89543292014-11-19 11:28:33 -08001541 r = hostRadius[d.type] || defaultHostRadius,
1542 textDy = r + 10,
Simon Hunteb1514d2014-11-20 09:57:29 -08001543 iid = iconGlyphUrl(d);
Simon Hunt56d51852014-11-09 13:03:35 -08001544
Simon Hunt7cd48f32014-11-09 23:42:50 -08001545 // provide ref to element from backing data....
1546 d.el = node;
1547
Thomas Vachuska89543292014-11-19 11:28:33 -08001548 node.append('circle')
1549 .attr('r', r);
Simon Hunt7fa116d2014-11-17 14:16:55 -08001550
Simon Hunteb1514d2014-11-20 09:57:29 -08001551 if (iid) {
1552 addHostIcon(node, r, iid);
Simon Hunt7fa116d2014-11-17 14:16:55 -08001553 }
Simon Hunt56d51852014-11-09 13:03:35 -08001554
Simon Hunt56d51852014-11-09 13:03:35 -08001555 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001556 .text(hostLabel)
Thomas Vachuska89543292014-11-19 11:28:33 -08001557 .attr('dy', textDy)
Simon Hunt7cd48f32014-11-09 23:42:50 -08001558 .attr('text-anchor', 'middle');
Simon Hunt56d51852014-11-09 13:03:35 -08001559
1560 // debug function to show the modelled x,y coordinates of nodes...
1561 if (debug('showNodeXY')) {
1562 node.select('circle').attr('fill-opacity', 0.5);
1563 node.append('circle')
1564 .attr({
1565 class: 'debug',
1566 cx: 0,
1567 cy: 0,
1568 r: '3px'
1569 });
1570 }
1571 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001572
Simon Hunt99c13842014-11-06 18:23:12 -08001573 // operate on both existing and new nodes, if necessary
1574 //node .foo() .bar() ...
Simon Huntc7ee0662014-11-05 16:44:37 -08001575
Simon Hunt99c13842014-11-06 18:23:12 -08001576 // operate on exiting nodes:
Simon Huntea80eb42014-11-11 13:46:57 -08001577 // Note that the node is removed after 2 seconds.
1578 // Sub element animations should be shorter than 2 seconds.
1579 var exiting = node.exit()
Simon Hunt44031102014-11-11 13:20:36 -08001580 .transition()
1581 .duration(2000)
Simon Huntea80eb42014-11-11 13:46:57 -08001582 .style('opacity', 0)
Simon Hunt99c13842014-11-06 18:23:12 -08001583 .remove();
Simon Huntea80eb42014-11-11 13:46:57 -08001584
1585 // host node exits....
1586 exiting.filter('.host').each(function (d) {
1587 var node = d3.select(this);
1588
1589 node.select('text')
1590 .style('opacity', 0.5)
1591 .transition()
1592 .duration(1000)
1593 .style('opacity', 0);
1594 // note, leave <g>.remove to remove this element
1595
Thomas Vachuska89543292014-11-19 11:28:33 -08001596 node.select('circle')
1597 .style('stroke-fill', '#555')
1598 .style('fill', '#888')
Simon Huntea80eb42014-11-11 13:46:57 -08001599 .style('opacity', 0.5)
1600 .transition()
1601 .duration(1500)
1602 .attr('r', 0);
1603 // note, leave <g>.remove to remove this element
1604
1605 });
1606
1607 // TODO: device node exits
Simon Huntc7ee0662014-11-05 16:44:37 -08001608 }
1609
Simon Huntc72967b2014-11-20 09:21:42 -08001610 function addDeviceIcon(node, box, noLabel, iid) {
1611 var cfg = config.icons.device,
1612 dx,
1613 dy,
1614 g;
1615
1616 if (noLabel) {
1617 box = emptyBox();
1618 dx = -cfg.dim/2;
1619 dy = -cfg.dim/2;
1620 } else {
1621 box = adjustRectToFitText(node);
1622 dx = box.x + config.icons.xoff;
1623 dy = box.y + config.icons.yoff;
1624 }
1625
Simon Hunteb1514d2014-11-20 09:57:29 -08001626 g = node.append('g')
1627 .attr('class', 'glyphIcon deviceIcon')
Simon Huntc72967b2014-11-20 09:21:42 -08001628 .attr('transform', translate(dx, dy));
1629
1630 g.append('rect').attr({
1631 x: 0,
1632 y: 0,
1633 rx: cfg.rx,
1634 width: cfg.dim,
1635 height: cfg.dim
1636 });
1637
1638 g.append('use').attr({
1639 'xlink:href': iid,
1640 width: cfg.dim,
1641 height: cfg.dim
1642 });
1643
Simon Huntc72967b2014-11-20 09:21:42 -08001644 }
1645
Simon Hunt8257f4c2014-11-16 19:34:54 -08001646 function find(key, array) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001647 for (var idx = 0, n = array.length; idx < n; idx++) {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001648 if (array[idx].key === key) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001649 return idx;
1650 }
1651 }
1652 return -1;
1653 }
1654
1655 function removeLinkElement(linkData) {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001656 var idx = find(linkData.key, network.links),
1657 removed;
1658 if (idx >=0) {
1659 // remove from links array
1660 removed = network.links.splice(idx, 1);
1661 // remove from lookup cache
1662 delete network.lookup[removed[0].key];
1663 updateLinks();
1664 network.force.resume();
1665 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001666 }
Simon Huntc7ee0662014-11-05 16:44:37 -08001667
Simon Hunt44031102014-11-11 13:20:36 -08001668 function removeHostElement(hostData) {
1669 // first, remove associated hostLink...
1670 removeLinkElement(hostData.linkData);
1671
1672 // remove from lookup cache
1673 delete network.lookup[hostData.id];
1674 // remove from nodes array
1675 var idx = find(hostData.id, network.nodes);
1676 network.nodes.splice(idx, 1);
1677 // remove from SVG
1678 updateNodes();
1679 network.force.resume();
1680 }
1681
1682
Simon Huntc7ee0662014-11-05 16:44:37 -08001683 function tick() {
1684 node.attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001685 transform: function (d) { return translate(d.x, d.y); }
Simon Huntc7ee0662014-11-05 16:44:37 -08001686 });
1687
1688 link.attr({
1689 x1: function (d) { return d.source.x; },
1690 y1: function (d) { return d.source.y; },
1691 x2: function (d) { return d.target.x; },
1692 y2: function (d) { return d.target.y; }
1693 });
Simon Hunte2575b62014-11-18 15:25:53 -08001694
1695 linkLabel.each(function (d) {
1696 var el = d3.select(this);
Thomas Vachuska4731f122014-11-20 04:56:19 -08001697 var lnk = findLinkById(d.key);
1698
1699 if (lnk) {
1700 var parms = {
Simon Hunte2575b62014-11-18 15:25:53 -08001701 x1: lnk.source.x,
1702 y1: lnk.source.y,
1703 x2: lnk.target.x,
1704 y2: lnk.target.y
1705 };
Thomas Vachuska4731f122014-11-20 04:56:19 -08001706 el.attr('transform', transformLabel(parms));
1707 }
Simon Hunte2575b62014-11-18 15:25:53 -08001708 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001709 }
Simon Hunt934c3ce2014-11-05 11:45:07 -08001710
1711 // ==============================
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001712 // Web-Socket for live data
1713
1714 function webSockUrl() {
1715 return document.location.toString()
1716 .replace(/\#.*/, '')
1717 .replace('http://', 'ws://')
1718 .replace('https://', 'wss://')
1719 .replace('index2.html', config.webSockUrl);
1720 }
1721
1722 webSock = {
1723 ws : null,
1724
1725 connect : function() {
1726 webSock.ws = new WebSocket(webSockUrl());
1727
1728 webSock.ws.onopen = function() {
Simon Hunt0c6d4192014-11-12 12:07:10 -08001729 noWebSock(false);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001730 };
1731
1732 webSock.ws.onmessage = function(m) {
1733 if (m.data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001734 wsTraceRx(m.data);
Thomas Vachuskad472c6e2014-11-07 19:11:05 -08001735 handleServerEvent(JSON.parse(m.data));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001736 }
1737 };
1738
1739 webSock.ws.onclose = function(m) {
1740 webSock.ws = null;
Simon Hunt0c6d4192014-11-12 12:07:10 -08001741 noWebSock(true);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001742 };
1743 },
1744
1745 send : function(text) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001746 if (text != null) {
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001747 webSock._send(text);
1748 }
1749 },
1750
1751 _send : function(message) {
1752 if (webSock.ws) {
1753 webSock.ws.send(message);
Simon Hunta255a2c2014-11-13 22:29:35 -08001754 } else if (config.useLiveData) {
Simon Hunt56d51852014-11-09 13:03:35 -08001755 network.view.alert('no web socket open\n\n' + message);
Simon Hunta255a2c2014-11-13 22:29:35 -08001756 } else {
1757 console.log('WS Send: ' + JSON.stringify(message));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001758 }
1759 }
1760
1761 };
1762
Simon Hunt0c6d4192014-11-12 12:07:10 -08001763 function noWebSock(b) {
1764 mask.style('display',b ? 'block' : 'none');
1765 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001766
1767 function sendMessage(evType, payload) {
1768 var toSend = {
Simon Huntbb282f52014-11-10 11:08:19 -08001769 event: evType,
1770 sid: ++sid,
1771 payload: payload
1772 },
1773 asText = JSON.stringify(toSend);
1774 wsTraceTx(asText);
1775 webSock.send(asText);
Simon Huntc76ae892014-11-18 17:31:51 -08001776
1777 // Temporary measure for debugging UI behavior ...
1778 if (!config.useLiveData) {
1779 handleTestSend(toSend);
1780 }
Simon Huntbb282f52014-11-10 11:08:19 -08001781 }
1782
1783 function wsTraceTx(msg) {
1784 wsTrace('tx', msg);
1785 }
1786 function wsTraceRx(msg) {
1787 wsTrace('rx', msg);
1788 }
1789 function wsTrace(rxtx, msg) {
Simon Huntbb282f52014-11-10 11:08:19 -08001790 console.log('[' + rxtx + '] ' + msg);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001791 }
1792
Simon Huntc76ae892014-11-18 17:31:51 -08001793 // NOTE: Temporary hardcoded example for showing detail pane
1794 // while we fine-
1795 // Probably should not merge this change...
1796 function handleTestSend(msg) {
1797 if (msg.event === 'requestDetails') {
1798 showDetails({
1799 event: 'showDetails',
1800 sid: 1001,
1801 payload: {
1802 "id": "of:0000ffffffffff09",
1803 "type": "roadm",
1804 "propOrder": [
1805 "Name",
1806 "Vendor",
1807 "H/W Version",
1808 "S/W Version",
1809 "-",
1810 "Latitude",
1811 "Longitude",
1812 "Ports"
1813 ],
1814 "props": {
1815 "Name": null,
1816 "Vendor": "Linc",
1817 "H/W Version": "OE",
1818 "S/W Version": "?",
1819 "-": "",
1820 "Latitude": "40.8",
1821 "Longitude": "73.1",
1822 "Ports": "2"
1823 }
1824 }
1825 });
1826 }
1827 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001828
1829 // ==============================
1830 // Selection stuff
1831
1832 function selectObject(obj, el) {
1833 var n,
Simon Hunt01095ff2014-11-13 16:37:29 -08001834 srcEv = d3.event.sourceEvent,
1835 meta = srcEv.metaKey,
1836 shift = srcEv.shiftKey;
1837
Simon Huntdeab4322014-11-13 18:49:07 -08001838 if ((panZoom() && !meta) || (!panZoom() && meta)) {
Simon Hunt01095ff2014-11-13 16:37:29 -08001839 return;
1840 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001841
1842 if (el) {
1843 n = d3.select(el);
1844 } else {
1845 node.each(function(d) {
1846 if (d == obj) {
1847 n = d3.select(el = this);
1848 }
1849 });
1850 }
1851 if (!n) return;
1852
Simon Hunt01095ff2014-11-13 16:37:29 -08001853 if (shift && n.classed('selected')) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001854 deselectObject(obj.id);
Simon Hunt61d04042014-11-11 17:27:16 -08001855 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001856 return;
1857 }
1858
Simon Hunt01095ff2014-11-13 16:37:29 -08001859 if (!shift) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001860 deselectAll();
1861 }
1862
Simon Huntc31d5692014-11-12 13:27:18 -08001863 selections[obj.id] = { obj: obj, el: el };
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001864 selectOrder.push(obj.id);
1865
1866 n.classed('selected', true);
Simon Hunt61d04042014-11-11 17:27:16 -08001867 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001868 }
1869
1870 function deselectObject(id) {
Simon Huntc31d5692014-11-12 13:27:18 -08001871 var obj = selections[id],
1872 idx;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001873 if (obj) {
1874 d3.select(obj.el).classed('selected', false);
Simon Hunt61d04042014-11-11 17:27:16 -08001875 delete selections[id];
Simon Huntc31d5692014-11-12 13:27:18 -08001876 idx = $.inArray(id, selectOrder);
1877 if (idx >= 0) {
1878 selectOrder.splice(idx, 1);
1879 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001880 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001881 }
1882
1883 function deselectAll() {
1884 // deselect all nodes in the network...
1885 node.classed('selected', false);
1886 selections = {};
1887 selectOrder = [];
Simon Hunt61d04042014-11-11 17:27:16 -08001888 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001889 }
1890
Simon Hunt61d04042014-11-11 17:27:16 -08001891 // update the state of the detail pane, based on current selections
1892 function updateDetailPane() {
1893 var nSel = selectOrder.length;
1894 if (!nSel) {
1895 detailPane.hide();
Simon Huntd72bc702014-11-13 18:38:04 -08001896 showTrafficAction(); // sends cancelTraffic event
Simon Hunt61d04042014-11-11 17:27:16 -08001897 } else if (nSel === 1) {
1898 singleSelect();
1899 } else {
1900 multiSelect();
1901 }
1902 }
1903
1904 function singleSelect() {
1905 requestDetails();
Simon Huntb53e0682014-11-12 13:32:01 -08001906 // NOTE: detail pane will be shown from showDetails event callback
Simon Hunt61d04042014-11-11 17:27:16 -08001907 }
1908
1909 function multiSelect() {
Simon Huntb53e0682014-11-12 13:32:01 -08001910 populateMultiSelect();
Simon Huntb53e0682014-11-12 13:32:01 -08001911 }
1912
1913 function addSep(tbody) {
1914 var tr = tbody.append('tr');
1915 $('<hr>').appendTo(tr.append('td').attr('colspan', 2));
1916 }
1917
1918 function addProp(tbody, label, value) {
1919 var tr = tbody.append('tr');
1920
1921 tr.append('td')
1922 .attr('class', 'label')
1923 .text(label + ' :');
1924
1925 tr.append('td')
1926 .attr('class', 'value')
1927 .text(value);
1928 }
1929
1930 function populateMultiSelect() {
1931 detailPane.empty();
1932
1933 var title = detailPane.append("h2"),
1934 table = detailPane.append("table"),
1935 tbody = table.append("tbody");
1936
Thomas Vachuska4731f122014-11-20 04:56:19 -08001937 title.text('Selected Nodes');
Simon Huntb53e0682014-11-12 13:32:01 -08001938
1939 selectOrder.forEach(function (d, i) {
1940 addProp(tbody, i+1, d);
1941 });
Simon Huntd72bc702014-11-13 18:38:04 -08001942
1943 addMultiSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08001944 }
1945
1946 function populateDetails(data) {
1947 detailPane.empty();
1948
Simon Hunta6a9fe72014-11-20 11:17:12 -08001949 var svg = detailPane.append('svg'),
1950 iid = iconGlyphUrl(data);
1951
Simon Hunt61d04042014-11-11 17:27:16 -08001952 var title = detailPane.append("h2"),
1953 table = detailPane.append("table"),
1954 tbody = table.append("tbody");
1955
Simon Hunta6a9fe72014-11-20 11:17:12 -08001956 appendGlyph(svg, 0, 0, 40, iid);
1957 title.text(data.id);
Simon Hunt61d04042014-11-11 17:27:16 -08001958
1959 data.propOrder.forEach(function(p) {
1960 if (p === '-') {
1961 addSep(tbody);
1962 } else {
1963 addProp(tbody, p, data.props[p]);
1964 }
1965 });
Simon Huntd72bc702014-11-13 18:38:04 -08001966
Thomas Vachuska4731f122014-11-20 04:56:19 -08001967 addSingleSelectActions(data);
Simon Hunt61d04042014-11-11 17:27:16 -08001968 }
1969
Thomas Vachuska4731f122014-11-20 04:56:19 -08001970 function addSingleSelectActions(data) {
Simon Huntd72bc702014-11-13 18:38:04 -08001971 detailPane.append('hr');
1972 // always want to allow 'show traffic'
Thomas Vachuska4731f122014-11-20 04:56:19 -08001973 addAction(detailPane, 'Show Related Traffic', showTrafficAction);
1974
1975 if (data.type === 'switch') {
1976 addAction(detailPane, 'Show Device Flows', showDeviceLinkFlowsAction);
1977 }
Simon Huntd72bc702014-11-13 18:38:04 -08001978 }
1979
1980 function addMultiSelectActions() {
1981 detailPane.append('hr');
1982 // always want to allow 'show traffic'
Thomas Vachuska4731f122014-11-20 04:56:19 -08001983 addAction(detailPane, 'Show Related Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001984 // if exactly two hosts are selected, also want 'add host intent'
1985 if (nSel() === 2 && allSelectionsClass('host')) {
Thomas Vachuska4731f122014-11-20 04:56:19 -08001986 addAction(detailPane, 'Add Host-to-Host Intent', addIntentAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001987 }
1988 }
1989
Simon Hunta5e89142014-11-14 07:00:33 -08001990 function addAction(panel, text, cb) {
1991 panel.append('div')
Simon Huntd72bc702014-11-13 18:38:04 -08001992 .classed('actionBtn', true)
1993 .text(text)
1994 .on('click', cb);
1995 }
1996
1997
Paul Greysonfcba0e82014-11-13 10:21:16 -08001998 function zoomPan(scale, translate) {
1999 zoomPanContainer.attr("transform", "translate(" + translate + ")scale(" + scale + ")");
2000 // keep the map lines constant width while zooming
Thomas Vachuska89543292014-11-19 11:28:33 -08002001 bgImg.style("stroke-width", 2.0 / scale + "px");
Paul Greysonfcba0e82014-11-13 10:21:16 -08002002 }
2003
2004 function resetZoomPan() {
2005 zoomPan(1, [0,0]);
2006 zoom.scale(1).translate([0,0]);
2007 }
2008
2009 function setupZoomPan() {
2010 function zoomed() {
Simon Huntdeab4322014-11-13 18:49:07 -08002011 if (!panZoom() ^ !d3.event.sourceEvent.metaKey) {
Paul Greysonfcba0e82014-11-13 10:21:16 -08002012 zoomPan(d3.event.scale, d3.event.translate);
2013 }
2014 }
2015
2016 zoom = d3.behavior.zoom()
2017 .translate([0, 0])
2018 .scale(1)
2019 .scaleExtent([1, 8])
2020 .on("zoom", zoomed);
2021
2022 svg.call(zoom);
2023 }
2024
Simon Hunt61d04042014-11-11 17:27:16 -08002025 // ==============================
2026 // Test harness code
Simon Hunt56d51852014-11-09 13:03:35 -08002027
2028 function prepareScenario(view, ctx, dbg) {
2029 var sc = scenario,
2030 urlSc = sc.evDir + ctx + sc.evScenario;
2031
2032 if (!ctx) {
2033 view.alert("No scenario specified (null ctx)");
2034 return;
2035 }
2036
2037 sc.view = view;
2038 sc.ctx = ctx;
2039 sc.debug = dbg;
2040 sc.evNumber = 0;
2041
2042 d3.json(urlSc, function(err, data) {
Simon Huntbb282f52014-11-10 11:08:19 -08002043 var p = data && data.params || {},
2044 desc = data && data.description || null,
Simon Huntfc274c92014-11-11 11:05:46 -08002045 intro = data && data.title;
Simon Huntbb282f52014-11-10 11:08:19 -08002046
Simon Hunt56d51852014-11-09 13:03:35 -08002047 if (err) {
2048 view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err);
2049 } else {
2050 sc.params = p;
Simon Huntbb282f52014-11-10 11:08:19 -08002051 if (desc) {
2052 intro += '\n\n ' + desc.join('\n ');
2053 }
2054 view.alert(intro);
Simon Hunt56d51852014-11-09 13:03:35 -08002055 }
2056 });
2057
2058 }
2059
Simon Hunt01095ff2014-11-13 16:37:29 -08002060 // ==============================
2061 // Toggle Buttons in masthead
Simon Hunt0c6d4192014-11-12 12:07:10 -08002062
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002063 // TODO: toggle button (and other widgets in the masthead) should be provided
2064 // by the framework; not generated by the view.
2065
Simon Hunta5e89142014-11-14 07:00:33 -08002066 var showInstances,
2067 doPanZoom,
2068 showTrafficOnHover;
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002069
2070 function addButtonBar(view) {
2071 var bb = d3.select('#mast')
2072 .append('span').classed('right', true).attr('id', 'bb');
2073
Simon Hunta5e89142014-11-14 07:00:33 -08002074 function mkTogBtn(text, cb) {
2075 return bb.append('span')
2076 .classed('btn', true)
2077 .text(text)
2078 .on('click', cb);
2079 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002080
Simon Hunta5e89142014-11-14 07:00:33 -08002081 showInstances = mkTogBtn('Show Instances', toggleInst);
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002082 }
2083
Simon Hunta5e89142014-11-14 07:00:33 -08002084 function instShown() {
2085 return showInstances.classed('active');
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002086 }
Simon Hunta5e89142014-11-14 07:00:33 -08002087 function toggleInst() {
2088 showInstances.classed('active', !instShown());
2089 if (instShown()) {
2090 oiBox.show();
2091 } else {
2092 oiBox.hide();
2093 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002094 }
2095
Simon Huntdeab4322014-11-13 18:49:07 -08002096 function panZoom() {
Simon Hunte5b71752014-11-18 20:06:07 -08002097 return false;
Simon Hunta5e89142014-11-14 07:00:33 -08002098 }
2099
2100 function trafficHover() {
Thomas Vachuska29617e52014-11-20 03:17:46 -08002101 return hoverModes[hoverMode] === 'intents';
Simon Hunta5e89142014-11-14 07:00:33 -08002102 }
Thomas Vachuska29617e52014-11-20 03:17:46 -08002103
2104 function flowsHover() {
2105 return hoverModes[hoverMode] === 'flows';
2106 }
2107
Simon Hunt7fa116d2014-11-17 14:16:55 -08002108 function loadGlyphs(svg) {
2109 var defs = svg.append('defs');
2110 gly.defBird(defs);
2111 gly.defBullhorn(defs);
Simon Huntc72967b2014-11-20 09:21:42 -08002112 gly.defGlyphs(defs);
Simon Hunt7fa116d2014-11-17 14:16:55 -08002113 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002114
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002115 // ==============================
Simon Hunt142d0032014-11-04 20:13:09 -08002116 // View life-cycle callbacks
Simon Hunt195cb382014-11-03 17:50:51 -08002117
Simon Huntf67722a2014-11-10 09:32:06 -08002118 function preload(view, ctx, flags) {
Simon Hunt142d0032014-11-04 20:13:09 -08002119 var w = view.width(),
2120 h = view.height(),
Simon Huntc7ee0662014-11-05 16:44:37 -08002121 fcfg = config.force,
2122 fpad = fcfg.pad,
2123 forceDim = [w - 2*fpad, h - 2*fpad];
Simon Hunt195cb382014-11-03 17:50:51 -08002124
Simon Hunt142d0032014-11-04 20:13:09 -08002125 // NOTE: view.$div is a D3 selection of the view's div
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002126 var viewBox = '0 0 ' + config.logicalSize + ' ' + config.logicalSize;
2127 svg = view.$div.append('svg').attr('viewBox', viewBox);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002128 setSize(svg, view);
2129
Simon Hunt7fa116d2014-11-17 14:16:55 -08002130 loadGlyphs(svg);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002131
Paul Greysonfcba0e82014-11-13 10:21:16 -08002132 zoomPanContainer = svg.append('g').attr('id', 'zoomPanContainer');
Paul Greysonfcba0e82014-11-13 10:21:16 -08002133 setupZoomPan();
2134
Simon Hunt1a9eff92014-11-07 11:06:34 -08002135 // add blue glow filter to svg layer
Paul Greysonfcba0e82014-11-13 10:21:16 -08002136 d3u.appendGlow(zoomPanContainer);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002137
Simon Huntc7ee0662014-11-05 16:44:37 -08002138 // group for the topology
Paul Greysonfcba0e82014-11-13 10:21:16 -08002139 topoG = zoomPanContainer.append('g')
Simon Huntd3b7d512014-11-12 15:48:41 -08002140 .attr('id', 'topo-G')
Simon Huntc7ee0662014-11-05 16:44:37 -08002141 .attr('transform', fcfg.translate());
2142
Simon Hunte2575b62014-11-18 15:25:53 -08002143 // subgroups for links, link labels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002144 linkG = topoG.append('g').attr('id', 'links');
Simon Hunte2575b62014-11-18 15:25:53 -08002145 linkLabelG = topoG.append('g').attr('id', 'linkLabels');
Simon Huntc7ee0662014-11-05 16:44:37 -08002146 nodeG = topoG.append('g').attr('id', 'nodes');
2147
Simon Hunte2575b62014-11-18 15:25:53 -08002148 // selection of links, linkLabels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002149 link = linkG.selectAll('.link');
Simon Hunte2575b62014-11-18 15:25:53 -08002150 linkLabel = linkLabelG.selectAll('.linkLabel');
Simon Huntc7ee0662014-11-05 16:44:37 -08002151 node = nodeG.selectAll('.node');
2152
Simon Hunt7cd48f32014-11-09 23:42:50 -08002153 function chrg(d) {
2154 return fcfg.charge[d.class] || -12000;
2155 }
Simon Hunt99c13842014-11-06 18:23:12 -08002156 function ldist(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002157 return fcfg.linkDistance[d.type] || 50;
Simon Hunt99c13842014-11-06 18:23:12 -08002158 }
2159 function lstrg(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002160 // 0.0 - 1.0
2161 return fcfg.linkStrength[d.type] || 1.0;
Simon Hunt99c13842014-11-06 18:23:12 -08002162 }
2163
Simon Hunt1a9eff92014-11-07 11:06:34 -08002164 function selectCb(d, self) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002165 selectObject(d, self);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002166 }
2167
2168 function atDragEnd(d, self) {
Simon Hunt56d51852014-11-09 13:03:35 -08002169 // once we've finished moving, pin the node in position
2170 d.fixed = true;
2171 d3.select(self).classed('fixed', true);
2172 if (config.useLiveData) {
Simon Hunt902c9922014-11-11 11:59:31 -08002173 sendUpdateMeta(d);
Simon Hunta255a2c2014-11-13 22:29:35 -08002174 } else {
2175 console.log('Moving node ' + d.id + ' to [' + d.x + ',' + d.y + ']');
Simon Hunt1a9eff92014-11-07 11:06:34 -08002176 }
2177 }
2178
Simon Hunt902c9922014-11-11 11:59:31 -08002179 function sendUpdateMeta(d) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002180 sendMessage('updateMeta', {
2181 id: d.id,
2182 'class': d.class,
Simon Hunt902c9922014-11-11 11:59:31 -08002183 'memento': {
Simon Hunt01095ff2014-11-13 16:37:29 -08002184 x: d.x,
2185 y: d.y
Simon Hunt902c9922014-11-11 11:59:31 -08002186 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002187 });
2188 }
2189
Simon Huntc7ee0662014-11-05 16:44:37 -08002190 // set up the force layout
2191 network.force = d3.layout.force()
2192 .size(forceDim)
2193 .nodes(network.nodes)
2194 .links(network.links)
Simon Hunt7cd48f32014-11-09 23:42:50 -08002195 .gravity(0.4)
2196 .friction(0.7)
2197 .charge(chrg)
Simon Hunt99c13842014-11-06 18:23:12 -08002198 .linkDistance(ldist)
2199 .linkStrength(lstrg)
Simon Huntc7ee0662014-11-05 16:44:37 -08002200 .on('tick', tick);
Simon Hunt195cb382014-11-03 17:50:51 -08002201
Simon Hunt01095ff2014-11-13 16:37:29 -08002202 network.drag = d3u.createDragBehavior(network.force,
Simon Huntdeab4322014-11-13 18:49:07 -08002203 selectCb, atDragEnd, panZoom);
Simon Hunt0c6d4192014-11-12 12:07:10 -08002204
2205 // create mask layer for when we lose connection to server.
Simon Hunta5e89142014-11-14 07:00:33 -08002206 // TODO: this should be part of the framework
Simon Hunt0c6d4192014-11-12 12:07:10 -08002207 mask = view.$div.append('div').attr('id','topo-mask');
2208 para(mask, 'Oops!');
2209 para(mask, 'Web-socket connection to server closed...');
2210 para(mask, 'Try refreshing the page.');
Simon Hunt12ce12e2014-11-15 21:13:19 -08002211
2212 mask.append('svg')
2213 .attr({
2214 id: 'mask-bird',
2215 width: w,
2216 height: h
2217 })
2218 .append('g')
2219 .attr('transform', birdTranslate(w, h))
2220 .style('opacity', 0.3)
2221 .append('use')
2222 .attr({
2223 'xlink:href': '#bird',
2224 width: config.birdDim,
2225 height: config.birdDim,
2226 fill: '#111'
Thomas Vachuska89543292014-11-19 11:28:33 -08002227 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08002228 }
Simon Hunt195cb382014-11-03 17:50:51 -08002229
Simon Hunt01095ff2014-11-13 16:37:29 -08002230 function para(sel, text) {
2231 sel.append('p').text(text);
2232 }
2233
2234
Simon Hunt56d51852014-11-09 13:03:35 -08002235 function load(view, ctx, flags) {
Simon Huntf67722a2014-11-10 09:32:06 -08002236 // resize, in case the window was resized while we were not loaded
2237 resize(view, ctx, flags);
2238
Simon Hunt99c13842014-11-06 18:23:12 -08002239 // cache the view token, so network topo functions can access it
2240 network.view = view;
Simon Hunt56d51852014-11-09 13:03:35 -08002241 config.useLiveData = !flags.local;
2242
2243 if (!config.useLiveData) {
2244 prepareScenario(view, ctx, flags.debug);
2245 }
Simon Hunt99c13842014-11-06 18:23:12 -08002246
2247 // set our radio buttons and key bindings
Simon Hunt9462e8c2014-11-14 17:28:09 -08002248 layerBtnSet = view.setRadio(layerButtons);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002249 view.setKeys(keyDispatch);
Simon Hunt195cb382014-11-03 17:50:51 -08002250
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002251 // patch in our "button bar" for now
2252 // TODO: implement a more official frameworky way of doing this..
2253 addButtonBar(view);
2254
Simon Huntd3b7d512014-11-12 15:48:41 -08002255 // Load map data asynchronously; complete startup after that..
2256 loadGeoJsonData();
Simon Hunta255a2c2014-11-13 22:29:35 -08002257 }
2258
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002259 function startAntTimer() {
Thomas Vachuska4731f122014-11-20 04:56:19 -08002260 if (!antTimer) {
2261 var pulses = [5, 3, 1.2, 3],
2262 pulse = 0;
2263 antTimer = setInterval(function () {
2264 pulse = pulse + 1;
2265 pulse = pulse === pulses.length ? 0 : pulse;
2266 d3.selectAll('.animated').style('stroke-width', pulses[pulse]);
2267 }, 200);
2268 }
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002269 }
2270
2271 function stopAntTimer() {
Simon Hunta255a2c2014-11-13 22:29:35 -08002272 if (antTimer) {
2273 clearInterval(antTimer);
2274 antTimer = null;
2275 }
Simon Huntd3b7d512014-11-12 15:48:41 -08002276 }
2277
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002278 function unload(view, ctx, flags) {
2279 stopAntTimer();
2280 }
2281
Simon Huntd3b7d512014-11-12 15:48:41 -08002282 // TODO: move these to config/state portion of script
Simon Hunta6a9fe72014-11-20 11:17:12 -08002283 var geoJsonUrl = 'json/map/continental_us.json',
Simon Huntd3b7d512014-11-12 15:48:41 -08002284 geoJson;
2285
2286 function loadGeoJsonData() {
2287 d3.json(geoJsonUrl, function (err, data) {
2288 if (err) {
2289 // fall back to USA map background
2290 loadStaticMap();
2291 } else {
2292 geoJson = data;
2293 loadGeoMap();
2294 }
2295
2296 // finally, connect to the server...
2297 if (config.useLiveData) {
2298 webSock.connect();
2299 }
2300 });
2301 }
2302
2303 function showBg() {
2304 return config.options.showBackground ? 'visible' : 'hidden';
2305 }
2306
2307 function loadStaticMap() {
2308 fnTrace('loadStaticMap', config.backgroundUrl);
2309 var w = network.view.width(),
2310 h = network.view.height();
2311
2312 // load the background image
2313 bgImg = svg.insert('svg:image', '#topo-G')
2314 .attr({
2315 id: 'topo-bg',
2316 width: w,
2317 height: h,
2318 'xlink:href': config.backgroundUrl
2319 })
2320 .style({
2321 visibility: showBg()
2322 });
2323 }
2324
2325 function loadGeoMap() {
2326 fnTrace('loadGeoMap', geoJsonUrl);
Simon Huntd3b7d512014-11-12 15:48:41 -08002327
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002328 // extracts the topojson data into geocoordinate-based geometry
2329 var topoData = topojson.feature(geoJson, geoJson.objects.states);
Simon Huntd3b7d512014-11-12 15:48:41 -08002330
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002331 // see: http://bl.ocks.org/mbostock/4707858
2332 geoMapProjection = d3.geo.mercator();
2333 var path = d3.geo.path().projection(geoMapProjection);
Simon Huntd3b7d512014-11-12 15:48:41 -08002334
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002335 geoMapProjection
2336 .scale(1)
2337 .translate([0, 0]);
Simon Huntd3b7d512014-11-12 15:48:41 -08002338
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002339 // [[x1,y1],[x2,y2]]
2340 var b = path.bounds(topoData);
Paul Greysonfcba0e82014-11-13 10:21:16 -08002341 // size map to 95% of minimum dimension to fill space
2342 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 -08002343 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 -08002344
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002345 geoMapProjection
2346 .scale(s)
2347 .translate(t);
2348
Paul Greysonfcba0e82014-11-13 10:21:16 -08002349 bgImg = zoomPanContainer.insert("g", '#topo-G');
Thomas Vachuska89543292014-11-19 11:28:33 -08002350 bgImg.attr('id', 'map').selectAll('path')
2351 .data(topoData.features)
2352 .enter()
2353 .append('path')
2354 .attr('d', path);
Simon Hunt195cb382014-11-03 17:50:51 -08002355 }
2356
Simon Huntf67722a2014-11-10 09:32:06 -08002357 function resize(view, ctx, flags) {
Simon Hunt12ce12e2014-11-15 21:13:19 -08002358 var w = view.width(),
2359 h = view.height();
2360
Simon Hunt934c3ce2014-11-05 11:45:07 -08002361 setSize(svg, view);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002362
2363 d3.select('#mask-bird').attr({ width: w, height: h})
2364 .select('g').attr('transform', birdTranslate(w, h));
Simon Hunt142d0032014-11-04 20:13:09 -08002365 }
2366
Simon Hunt12ce12e2014-11-15 21:13:19 -08002367 function birdTranslate(w, h) {
2368 var bdim = config.birdDim;
2369 return 'translate('+((w-bdim)*.4)+','+((h-bdim)*.1)+')';
2370 }
Simon Hunt142d0032014-11-04 20:13:09 -08002371
2372 // ==============================
2373 // View registration
Simon Hunt195cb382014-11-03 17:50:51 -08002374
Simon Hunt25248912014-11-04 11:25:48 -08002375 onos.ui.addView('topo', {
Simon Hunt142d0032014-11-04 20:13:09 -08002376 preload: preload,
2377 load: load,
Simon Hunta255a2c2014-11-13 22:29:35 -08002378 unload: unload,
Simon Hunt142d0032014-11-04 20:13:09 -08002379 resize: resize
Simon Hunt195cb382014-11-03 17:50:51 -08002380 });
2381
Simon Hunt61d04042014-11-11 17:27:16 -08002382 detailPane = onos.ui.addFloatingPanel('topo-detail');
Simon Hunta5e89142014-11-14 07:00:33 -08002383 oiBox = onos.ui.addFloatingPanel('topo-oibox', 'TL');
Simon Hunt61d04042014-11-11 17:27:16 -08002384
Simon Hunt195cb382014-11-03 17:50:51 -08002385}(ONOS));