blob: cd3033440361dc8ea1f2a32e05435d38885c6c88 [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',
Thomas Vachuska9edca302014-11-22 17:06:42 -080075 linkInWidth: 12,
Thomas Vachuska89543292014-11-19 11:28:33 -080076 linkOutColor: '#f00',
Thomas Vachuska9edca302014-11-22 17:06:42 -080077 linkOutWidth: 10
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
Thomas Vachuska47635c62014-11-22 01:21:36 -0800144 O: [toggleSummary, 'Toggle ONOS summary pane'],
145 I: [toggleInstances, 'Toggle ONOS instances pane'],
Simon Hunt988c6fc2014-11-20 17:43:03 -0800146 B: [toggleBg, 'Toggle background image'],
147 L: [cycleLabels, 'Cycle Device labels'],
Simon Hunt934c3ce2014-11-05 11:45:07 -0800148 P: togglePorts,
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800149 U: [unpin, 'Unpin node'],
150 R: [resetZoomPan, 'Reset zoom/pan'],
Thomas Vachuska9edca302014-11-22 17:06:42 -0800151 V: [showTrafficAction, 'Show related traffic'],
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800152 A: [showAllTrafficAction, 'Show all traffic'],
153 F: [showDeviceLinkFlowsAction, 'Show device link flows'],
Simon Hunt9462e8c2014-11-14 17:28:09 -0800154 esc: handleEscape
Simon Hunt934c3ce2014-11-05 11:45:07 -0800155 };
Simon Hunt142d0032014-11-04 20:13:09 -0800156
Simon Hunt195cb382014-11-03 17:50:51 -0800157 // state variables
Simon Hunt99c13842014-11-06 18:23:12 -0800158 var network = {
Simon Hunt50128c02014-11-08 13:36:15 -0800159 view: null, // view token reference
Simon Hunt99c13842014-11-06 18:23:12 -0800160 nodes: [],
161 links: [],
Simon Hunt269670f2014-11-17 16:17:43 -0800162 lookup: {},
163 revLinkToKey: {}
Simon Hunt99c13842014-11-06 18:23:12 -0800164 },
Simon Hunt56d51852014-11-09 13:03:35 -0800165 scenario = {
166 evDir: 'json/ev/',
167 evScenario: '/scenario.json',
168 evPrefix: '/ev_',
169 evOnos: '_onos.json',
170 evUi: '_ui.json',
171 ctx: null,
172 params: {},
173 evNumber: 0,
174 view: null,
175 debug: false
176 },
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800177 webSock,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800178 sid = 0,
Thomas Vachuska60d72bf2014-11-21 13:02:00 -0800179 deviceLabelCount = 3,
Simon Hunt209155e2014-11-21 12:16:09 -0800180 hostLabelCount = 2,
Simon Hunt56d51852014-11-09 13:03:35 -0800181 deviceLabelIndex = 0,
182 hostLabelIndex = 0,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800183 selections = {},
Simon Hunta5e89142014-11-14 07:00:33 -0800184 selectOrder = [],
Simon Hunt6ac93f32014-11-13 12:17:27 -0800185 hovered = null,
Thomas Vachuska47635c62014-11-22 01:21:36 -0800186 summaryPane,
Simon Hunta5e89142014-11-14 07:00:33 -0800187 detailPane,
Simon Hunta255a2c2014-11-13 22:29:35 -0800188 antTimer = null,
Simon Hunta5e89142014-11-14 07:00:33 -0800189 onosInstances = {},
190 onosOrder = [],
191 oiBox,
Simon Hunt9462e8c2014-11-14 17:28:09 -0800192 oiShowMaster = false,
Simon Hunt195cb382014-11-03 17:50:51 -0800193 portLabelsOn = false;
194
Thomas Vachuska9edca302014-11-22 17:06:42 -0800195 var hoverModeAll = 1,
196 hoverModeFlows = 2,
197 hoverModeIntents = 3,
198 hoverMode = hoverModeFlows;
199
Simon Hunt934c3ce2014-11-05 11:45:07 -0800200 // D3 selections
201 var svg,
Paul Greysonfcba0e82014-11-13 10:21:16 -0800202 zoomPanContainer,
Simon Hunt934c3ce2014-11-05 11:45:07 -0800203 bgImg,
Simon Huntc7ee0662014-11-05 16:44:37 -0800204 topoG,
205 nodeG,
206 linkG,
Simon Hunte2575b62014-11-18 15:25:53 -0800207 linkLabelG,
Simon Huntc7ee0662014-11-05 16:44:37 -0800208 node,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800209 link,
Simon Hunte2575b62014-11-18 15:25:53 -0800210 linkLabel,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800211 mask;
Simon Hunt195cb382014-11-03 17:50:51 -0800212
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800213 // the projection for the map background
214 var geoMapProjection;
215
Paul Greysonfcba0e82014-11-13 10:21:16 -0800216 // the zoom function
217 var zoom;
218
Simon Hunt142d0032014-11-04 20:13:09 -0800219 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800220 // For Debugging / Development
Simon Hunt195cb382014-11-03 17:50:51 -0800221
Simon Hunt99c13842014-11-06 18:23:12 -0800222 function note(label, msg) {
223 console.log('NOTE: ' + label + ': ' + msg);
Simon Hunt195cb382014-11-03 17:50:51 -0800224 }
225
Simon Hunt99c13842014-11-06 18:23:12 -0800226 function debug(what) {
227 return config.debugOn && config.debug[what];
Simon Hunt934c3ce2014-11-05 11:45:07 -0800228 }
229
Simon Huntfc274c92014-11-11 11:05:46 -0800230 function fnTrace(msg, id) {
231 if (config.fnTrace) {
232 console.log('FN: ' + msg + ' [' + id + ']');
233 }
234 }
Simon Hunt99c13842014-11-06 18:23:12 -0800235
Simon Hunta5e89142014-11-14 07:00:33 -0800236 function evTrace(data) {
237 fnTrace(data.event, data.payload.id);
238 }
239
Simon Hunt934c3ce2014-11-05 11:45:07 -0800240 // ==============================
241 // Key Callbacks
242
Simon Hunt99c13842014-11-06 18:23:12 -0800243 function testMe(view) {
Simon Hunta3dd9572014-11-20 15:22:41 -0800244 //view.alert('Theme is ' + view.theme());
245 //view.flash('This is some text');
Simon Hunt99c13842014-11-06 18:23:12 -0800246 }
247
Simon Hunt56d51852014-11-09 13:03:35 -0800248 function abortIfLive() {
Simon Hunt50128c02014-11-08 13:36:15 -0800249 if (config.useLiveData) {
Simon Huntb53e0682014-11-12 13:32:01 -0800250 network.view.alert("Sorry, currently using live data..");
Simon Hunt56d51852014-11-09 13:03:35 -0800251 return true;
Simon Hunt50128c02014-11-08 13:36:15 -0800252 }
Simon Hunt56d51852014-11-09 13:03:35 -0800253 return false;
254 }
Simon Hunt50128c02014-11-08 13:36:15 -0800255
Simon Hunt56d51852014-11-09 13:03:35 -0800256 function testDebug(msg) {
257 if (scenario.debug) {
258 scenario.view.alert(msg);
259 }
260 }
Simon Hunt99c13842014-11-06 18:23:12 -0800261
Simon Hunt56d51852014-11-09 13:03:35 -0800262 function injectTestEvent(view) {
263 if (abortIfLive()) { return; }
264 var sc = scenario,
265 evn = ++sc.evNumber,
266 pfx = sc.evDir + sc.ctx + sc.evPrefix + evn,
267 onosUrl = pfx + sc.evOnos,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800268 uiUrl = pfx + sc.evUi,
269 stack = [
270 { url: onosUrl, cb: handleServerEvent },
271 { url: uiUrl, cb: handleUiEvent }
272 ];
273 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800274 }
275
Simon Hunt7cd48f32014-11-09 23:42:50 -0800276 function recurseFetchEvent(stack, evn) {
277 var v = scenario.view,
278 frame;
279 if (stack.length === 0) {
Simon Huntfc274c92014-11-11 11:05:46 -0800280 v.alert('Oops!\n\nNo event #' + evn + ' found.');
Simon Hunt7cd48f32014-11-09 23:42:50 -0800281 return;
282 }
283 frame = stack.shift();
284
285 d3.json(frame.url, function (err, data) {
Simon Hunt99c13842014-11-06 18:23:12 -0800286 if (err) {
Simon Hunt56d51852014-11-09 13:03:35 -0800287 if (err.status === 404) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800288 // if we didn't find the data, try the next stack frame
289 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800290 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800291 v.alert('non-404 error:\n\n' + frame.url + '\n\n' + err);
Simon Hunt56d51852014-11-09 13:03:35 -0800292 }
Simon Hunt99c13842014-11-06 18:23:12 -0800293 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800294 testDebug('loaded: ' + frame.url);
Simon Hunt1712ed82014-11-17 12:56:00 -0800295 wsTrace('test', JSON.stringify(data));
Simon Hunt7cd48f32014-11-09 23:42:50 -0800296 frame.cb(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800297 }
298 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800299
Simon Hunt56d51852014-11-09 13:03:35 -0800300 }
Simon Hunt50128c02014-11-08 13:36:15 -0800301
Simon Hunt56d51852014-11-09 13:03:35 -0800302 function handleUiEvent(data) {
Simon Huntbb282f52014-11-10 11:08:19 -0800303 scenario.view.alert('UI Tx: ' + data.event + '\n\n' +
304 JSON.stringify(data));
Simon Hunt56d51852014-11-09 13:03:35 -0800305 }
306
307 function injectStartupEvents(view) {
308 var last = scenario.params.lastAuto || 0;
309 if (abortIfLive()) { return; }
310
311 while (scenario.evNumber < last) {
Simon Hunt1a9eff92014-11-07 11:06:34 -0800312 injectTestEvent(view);
313 }
314 }
315
Simon Hunt934c3ce2014-11-05 11:45:07 -0800316 function toggleBg() {
317 var vis = bgImg.style('visibility');
318 bgImg.style('visibility', (vis === 'hidden') ? 'visible' : 'hidden');
319 }
320
Simon Hunt99c13842014-11-06 18:23:12 -0800321 function cycleLabels() {
Thomas Vachuska60d72bf2014-11-21 13:02:00 -0800322 deviceLabelIndex = (deviceLabelIndex === 2)
Simon Huntbb282f52014-11-10 11:08:19 -0800323 ? 0 : deviceLabelIndex + 1;
Simon Hunt5f36d342014-11-08 21:33:14 -0800324
Simon Hunt99c13842014-11-06 18:23:12 -0800325 network.nodes.forEach(function (d) {
Simon Huntbb282f52014-11-10 11:08:19 -0800326 if (d.class === 'device') {
327 updateDeviceLabel(d);
328 }
Simon Hunt99c13842014-11-06 18:23:12 -0800329 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800330 }
331
332 function togglePorts(view) {
Simon Hunt50128c02014-11-08 13:36:15 -0800333 view.alert('togglePorts() callback')
Simon Hunt934c3ce2014-11-05 11:45:07 -0800334 }
335
Simon Hunt6ac93f32014-11-13 12:17:27 -0800336 function unpin() {
337 if (hovered) {
338 hovered.fixed = false;
339 hovered.el.classed('fixed', false);
340 network.force.resume();
341 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800342 }
343
Simon Hunt9462e8c2014-11-14 17:28:09 -0800344 function handleEscape(view) {
345 if (oiShowMaster) {
346 cancelAffinity();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800347 } else if (detailPane.isVisible()) {
Simon Hunt9462e8c2014-11-14 17:28:09 -0800348 deselectAll();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800349 } else if (oiBox.isVisible()) {
350 oiBox.hide();
351 } else if (summaryPane.isVisible()) {
352 cancelSummary();
Simon Hunt9462e8c2014-11-14 17:28:09 -0800353 }
354 }
355
Simon Hunt934c3ce2014-11-05 11:45:07 -0800356 // ==============================
357 // Radio Button Callbacks
358
Simon Hunta5e89142014-11-14 07:00:33 -0800359 var layerLookup = {
360 host: {
Simon Hunt209155e2014-11-21 12:16:09 -0800361 endstation: 'pkt', // default, if host event does not define type
Thomas Vachuska89543292014-11-19 11:28:33 -0800362 router: 'pkt',
Simon Hunta5e89142014-11-14 07:00:33 -0800363 bgpSpeaker: 'pkt'
364 },
365 device: {
366 switch: 'pkt',
367 roadm: 'opt'
368 },
369 link: {
370 hostLink: 'pkt',
371 direct: 'pkt',
Simon Hunt8257f4c2014-11-16 19:34:54 -0800372 indirect: '',
373 tunnel: '',
Simon Hunta5e89142014-11-14 07:00:33 -0800374 optical: 'opt'
375 }
376 };
377
378 function inLayer(d, layer) {
Simon Hunt8257f4c2014-11-16 19:34:54 -0800379 var type = d.class === 'link' ? d.type() : d.type,
380 look = layerLookup[d.class],
381 lyr = look && look[type];
Simon Hunta5e89142014-11-14 07:00:33 -0800382 return lyr === layer;
383 }
384
385 function unsuppressLayer(which) {
386 node.each(function (d) {
387 var node = d.el;
388 if (inLayer(d, which)) {
389 node.classed('suppressed', false);
390 }
391 });
392
393 link.each(function (d) {
394 var link = d.el;
395 if (inLayer(d, which)) {
396 link.classed('suppressed', false);
397 }
398 });
399 }
400
Simon Hunt9462e8c2014-11-14 17:28:09 -0800401 function suppressLayers(b) {
402 node.classed('suppressed', b);
403 link.classed('suppressed', b);
Simon Hunt142d0032014-11-04 20:13:09 -0800404// d3.selectAll('svg .port').classed('inactive', false);
405// d3.selectAll('svg .portText').classed('inactive', false);
Simon Hunt195cb382014-11-03 17:50:51 -0800406 }
407
Simon Hunt9462e8c2014-11-14 17:28:09 -0800408 function showAllLayers() {
409 suppressLayers(false);
410 }
411
Simon Hunt195cb382014-11-03 17:50:51 -0800412 function showPacketLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800413 node.classed('suppressed', true);
414 link.classed('suppressed', true);
415 unsuppressLayer('pkt');
Simon Hunt195cb382014-11-03 17:50:51 -0800416 }
417
418 function showOpticalLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800419 node.classed('suppressed', true);
420 link.classed('suppressed', true);
421 unsuppressLayer('opt');
Simon Hunt195cb382014-11-03 17:50:51 -0800422 }
423
Simon Hunt9462e8c2014-11-14 17:28:09 -0800424 function restoreLayerState() {
425 layerBtnDispatch[layerBtnSet.selected()]();
426 }
427
Simon Hunt142d0032014-11-04 20:13:09 -0800428 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800429 // Private functions
430
Simon Hunt99c13842014-11-06 18:23:12 -0800431 function safeId(s) {
432 return s.replace(/[^a-z0-9]/gi, '-');
433 }
434
Simon Huntc7ee0662014-11-05 16:44:37 -0800435 // set the size of the given element to that of the view (reduced if padded)
436 function setSize(el, view, pad) {
437 var padding = pad ? pad * 2 : 0;
Simon Hunt934c3ce2014-11-05 11:45:07 -0800438 el.attr({
Simon Huntc7ee0662014-11-05 16:44:37 -0800439 width: view.width() - padding,
440 height: view.height() - padding
Simon Hunt934c3ce2014-11-05 11:45:07 -0800441 });
442 }
443
Simon Hunt8257f4c2014-11-16 19:34:54 -0800444 function makeNodeKey(d, what) {
445 var port = what + 'Port';
446 return d[what] + '/' + d[port];
447 }
448
449 function makeLinkKey(d, flipped) {
450 var one = flipped ? makeNodeKey(d, 'dst') : makeNodeKey(d, 'src'),
451 two = flipped ? makeNodeKey(d, 'src') : makeNodeKey(d, 'dst');
452 return one + '-' + two;
453 }
454
Simon Hunt269670f2014-11-17 16:17:43 -0800455 function findLinkById(id) {
456 // check to see if this is a reverse lookup, else default to given id
457 var key = network.revLinkToKey[id] || id;
458 return key && network.lookup[key];
459 }
460
Simon Hunt8257f4c2014-11-16 19:34:54 -0800461 function findLink(linkData, op) {
462 var key = makeLinkKey(linkData),
463 keyrev = makeLinkKey(linkData, 1),
464 link = network.lookup[key],
465 linkRev = network.lookup[keyrev],
466 result = {},
467 ldata = link || linkRev,
468 rawLink;
469
470 if (op === 'add') {
471 if (link) {
472 // trying to add a link that we already know about
473 result.ldata = link;
474 result.badLogic = 'addLink: link already added';
475
476 } else if (linkRev) {
477 // we found the reverse of the link to be added
478 result.ldata = linkRev;
479 if (linkRev.fromTarget) {
480 result.badLogic = 'addLink: link already added';
481 }
482 }
483 } else if (op === 'update') {
484 if (!ldata) {
485 result.badLogic = 'updateLink: link not found';
486 } else {
487 rawLink = link ? ldata.fromSource : ldata.fromTarget;
488 result.updateWith = function (data) {
489 $.extend(rawLink, data);
490 restyleLinkElement(ldata);
491 }
492 }
493 } else if (op === 'remove') {
494 if (!ldata) {
495 result.badLogic = 'removeLink: link not found';
496 } else {
497 rawLink = link ? ldata.fromSource : ldata.fromTarget;
498
499 if (!rawLink) {
500 result.badLogic = 'removeLink: link not found';
501
502 } else {
503 result.removeRawLink = function () {
504 if (link) {
505 // remove fromSource
506 ldata.fromSource = null;
507 if (ldata.fromTarget) {
508 // promote target into source position
509 ldata.fromSource = ldata.fromTarget;
510 ldata.fromTarget = null;
511 ldata.key = keyrev;
512 delete network.lookup[key];
513 network.lookup[keyrev] = ldata;
Simon Hunt269670f2014-11-17 16:17:43 -0800514 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800515 }
516 } else {
517 // remove fromTarget
518 ldata.fromTarget = null;
Simon Hunt269670f2014-11-17 16:17:43 -0800519 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800520 }
521 if (ldata.fromSource) {
522 restyleLinkElement(ldata);
523 } else {
524 removeLinkElement(ldata);
525 }
526 }
527 }
528 }
529 }
530 return result;
531 }
532
533 function addLinkUpdate(ldata, link) {
534 // add link event, but we already have the reverse link installed
535 ldata.fromTarget = link;
Simon Hunt269670f2014-11-17 16:17:43 -0800536 network.revLinkToKey[link.id] = ldata.key;
Simon Hunt8257f4c2014-11-16 19:34:54 -0800537 restyleLinkElement(ldata);
538 }
539
540 var allLinkTypes = 'direct indirect optical tunnel',
541 defaultLinkType = 'direct';
542
543 function restyleLinkElement(ldata) {
544 // this fn's job is to look at raw links and decide what svg classes
545 // need to be applied to the line element in the DOM
546 var el = ldata.el,
547 type = ldata.type(),
548 lw = ldata.linkWidth(),
549 online = ldata.online();
550
551 el.classed('link', true);
552 el.classed('inactive', !online);
553 el.classed(allLinkTypes, false);
554 if (type) {
555 el.classed(type, true);
556 }
557 el.transition()
558 .duration(1000)
Thomas Vachuska89543292014-11-19 11:28:33 -0800559 .attr('stroke-width', linkScale(lw))
560 .attr('stroke', config.topo.linkBaseColor);
Simon Hunt8257f4c2014-11-16 19:34:54 -0800561 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800562
Simon Hunt99c13842014-11-06 18:23:12 -0800563 // ==============================
564 // Event handlers for server-pushed events
565
Simon Huntbb282f52014-11-10 11:08:19 -0800566 function logicError(msg) {
567 // TODO, report logic error to server, via websock, so it can be logged
Simon Huntcb56cff2014-11-17 11:42:26 -0800568 //network.view.alert('Logic Error:\n\n' + msg);
Simon Huntfc274c92014-11-11 11:05:46 -0800569 console.warn(msg);
Simon Huntbb282f52014-11-10 11:08:19 -0800570 }
571
Simon Hunt99c13842014-11-06 18:23:12 -0800572 var eventDispatch = {
Simon Hunta5e89142014-11-14 07:00:33 -0800573 addInstance: addInstance,
Simon Hunt99c13842014-11-06 18:23:12 -0800574 addDevice: addDevice,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800575 addLink: addLink,
Simon Hunt56d51852014-11-09 13:03:35 -0800576 addHost: addHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800577
Simon Huntfcfb46c2014-11-19 12:53:38 -0800578 updateInstance: updateInstance,
Simon Huntbb282f52014-11-10 11:08:19 -0800579 updateDevice: updateDevice,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800580 updateLink: updateLink,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800581 updateHost: updateHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800582
Simon Huntd72bc702014-11-13 18:38:04 -0800583 removeInstance: stillToImplement,
Simon Huntbb282f52014-11-10 11:08:19 -0800584 removeDevice: stillToImplement,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800585 removeLink: removeLink,
Simon Hunt44031102014-11-11 13:20:36 -0800586 removeHost: removeHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800587
Simon Hunt61d04042014-11-11 17:27:16 -0800588 showDetails: showDetails,
Thomas Vachuska47635c62014-11-22 01:21:36 -0800589 showSummary: showSummary,
Simon Huntb53e0682014-11-12 13:32:01 -0800590 showTraffic: showTraffic
Simon Hunt99c13842014-11-06 18:23:12 -0800591 };
592
Simon Hunta5e89142014-11-14 07:00:33 -0800593 function addInstance(data) {
594 evTrace(data);
595 var inst = data.payload,
596 id = inst.id;
597 if (onosInstances[id]) {
598 logicError('ONOS instance already added: ' + id);
599 return;
600 }
601 onosInstances[id] = inst;
602 onosOrder.push(inst);
603 updateInstances();
604 }
605
Simon Hunt99c13842014-11-06 18:23:12 -0800606 function addDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800607 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800608 var device = data.payload,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800609 nodeData = createDeviceNode(device);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800610 network.nodes.push(nodeData);
611 network.lookup[nodeData.id] = nodeData;
Simon Hunt99c13842014-11-06 18:23:12 -0800612 updateNodes();
613 network.force.start();
614 }
615
Simon Hunt99c13842014-11-06 18:23:12 -0800616 function addLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800617 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800618 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800619 result = findLink(link, 'add'),
620 bad = result.badLogic,
621 ldata = result.ldata;
622
623 if (bad) {
624 logicError(bad + ': ' + link.id);
625 return;
626 }
627
628 if (ldata) {
629 // we already have a backing store link for src/dst nodes
630 addLinkUpdate(ldata, link);
631 return;
632 }
633
634 // no backing store link yet
635 ldata = createLink(link);
636 if (ldata) {
637 network.links.push(ldata);
638 network.lookup[ldata.key] = ldata;
Simon Hunt99c13842014-11-06 18:23:12 -0800639 updateLinks();
640 network.force.start();
641 }
642 }
643
Simon Hunt56d51852014-11-09 13:03:35 -0800644 function addHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800645 evTrace(data);
Simon Hunt56d51852014-11-09 13:03:35 -0800646 var host = data.payload,
647 node = createHostNode(host),
648 lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800649 network.nodes.push(node);
650 network.lookup[host.id] = node;
651 updateNodes();
652
653 lnk = createHostLink(host);
654 if (lnk) {
Simon Hunt44031102014-11-11 13:20:36 -0800655 node.linkData = lnk; // cache ref on its host
Simon Hunt56d51852014-11-09 13:03:35 -0800656 network.links.push(lnk);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800657 network.lookup[host.ingress] = lnk;
658 network.lookup[host.egress] = lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800659 updateLinks();
660 }
661 network.force.start();
662 }
663
Simon Hunt44031102014-11-11 13:20:36 -0800664 // TODO: fold updateX(...) methods into one base method; remove duplication
Simon Hunt56a2ea42014-11-19 12:39:31 -0800665
666 function updateInstance(data) {
667 evTrace(data);
668 var inst = data.payload,
669 id = inst.id,
670 instData = onosInstances[id];
671 if (instData) {
672 $.extend(instData, inst);
673 updateInstances();
Simon Hunt56a2ea42014-11-19 12:39:31 -0800674 } else {
675 logicError('updateInstance lookup fail. ID = "' + id + '"');
676 }
677 }
678
Simon Huntbb282f52014-11-10 11:08:19 -0800679 function updateDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800680 evTrace(data);
Simon Huntbb282f52014-11-10 11:08:19 -0800681 var device = data.payload,
682 id = device.id,
683 nodeData = network.lookup[id];
684 if (nodeData) {
685 $.extend(nodeData, device);
686 updateDeviceState(nodeData);
687 } else {
688 logicError('updateDevice lookup fail. ID = "' + id + '"');
689 }
690 }
691
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800692 function updateLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800693 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800694 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800695 result = findLink(link, 'update'),
696 bad = result.badLogic;
697 if (bad) {
698 logicError(bad + ': ' + link.id);
699 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800700 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800701 result.updateWith(link);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800702 }
703
Simon Hunt7cd48f32014-11-09 23:42:50 -0800704 function updateHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800705 evTrace(data);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800706 var host = data.payload,
Simon Huntbb282f52014-11-10 11:08:19 -0800707 id = host.id,
708 hostData = network.lookup[id];
709 if (hostData) {
710 $.extend(hostData, host);
711 updateHostState(hostData);
712 } else {
713 logicError('updateHost lookup fail. ID = "' + id + '"');
714 }
Simon Hunt7cd48f32014-11-09 23:42:50 -0800715 }
716
Simon Hunt44031102014-11-11 13:20:36 -0800717 // TODO: fold removeX(...) methods into base method - remove dup code
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800718 function removeLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800719 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800720 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800721 result = findLink(link, 'remove'),
722 bad = result.badLogic;
723 if (bad) {
724 logicError(bad + ': ' + link.id);
725 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800726 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800727 result.removeRawLink();
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800728 }
729
Simon Hunt44031102014-11-11 13:20:36 -0800730 function removeHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800731 evTrace(data);
Simon Hunt44031102014-11-11 13:20:36 -0800732 var host = data.payload,
733 id = host.id,
734 hostData = network.lookup[id];
735 if (hostData) {
736 removeHostElement(hostData);
737 } else {
738 logicError('removeHost lookup fail. ID = "' + id + '"');
739 }
740 }
741
Thomas Vachuska47635c62014-11-22 01:21:36 -0800742 function showSummary(data) {
743 evTrace(data);
744 populateSummary(data.payload);
745 summaryPane.show();
746 }
747
Simon Hunt61d04042014-11-11 17:27:16 -0800748 function showDetails(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800749 evTrace(data);
Simon Hunt61d04042014-11-11 17:27:16 -0800750 populateDetails(data.payload);
751 detailPane.show();
752 }
753
Simon Huntb53e0682014-11-12 13:32:01 -0800754 function showTraffic(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800755 evTrace(data);
Thomas Vachuska4731f122014-11-20 04:56:19 -0800756 var paths = data.payload.paths,
757 hasTraffic = false;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800758
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800759 // Revert any links hilighted previously.
Thomas Vachuska4731f122014-11-20 04:56:19 -0800760 link.style('stroke-width', null)
Thomas Vachuskaa3148a72014-11-19 21:38:35 -0800761 .classed('primary secondary animated optical', false);
Simon Hunte2575b62014-11-18 15:25:53 -0800762 // Remove all previous labels.
763 removeLinkLabels();
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800764
Simon Hunte2575b62014-11-18 15:25:53 -0800765 // Now hilight all links in the paths payload, and attach
766 // labels to them, if they are defined.
Simon Hunta255a2c2014-11-13 22:29:35 -0800767 paths.forEach(function (p) {
Simon Hunte2575b62014-11-18 15:25:53 -0800768 var n = p.links.length,
769 i,
770 ldata;
771
Thomas Vachuska4731f122014-11-20 04:56:19 -0800772 hasTraffic = hasTraffic || p.traffic;
Simon Hunte2575b62014-11-18 15:25:53 -0800773 for (i=0; i<n; i++) {
774 ldata = findLinkById(p.links[i]);
Thomas Vachuska4731f122014-11-20 04:56:19 -0800775 if (ldata && ldata.el) {
Simon Hunte2575b62014-11-18 15:25:53 -0800776 ldata.el.classed(p.class, true);
777 ldata.label = p.labels[i];
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800778 }
Simon Hunte2575b62014-11-18 15:25:53 -0800779 }
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800780 });
Thomas Vachuska4731f122014-11-20 04:56:19 -0800781
Simon Hunte2575b62014-11-18 15:25:53 -0800782 updateLinks();
Thomas Vachuska4731f122014-11-20 04:56:19 -0800783
784 if (hasTraffic && !antTimer) {
785 startAntTimer();
786 } else if (!hasTraffic && antTimer) {
787 stopAntTimer();
788 }
Simon Huntb53e0682014-11-12 13:32:01 -0800789 }
790
Simon Hunt56d51852014-11-09 13:03:35 -0800791 // ...............................
792
793 function stillToImplement(data) {
794 var p = data.payload;
795 note(data.event, p.id);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800796 network.view.alert('Not yet implemented: "' + data.event + '"');
Simon Hunt56d51852014-11-09 13:03:35 -0800797 }
Simon Hunt99c13842014-11-06 18:23:12 -0800798
799 function unknownEvent(data) {
Simon Hunt50128c02014-11-08 13:36:15 -0800800 network.view.alert('Unknown event type: "' + data.event + '"');
Simon Hunt99c13842014-11-06 18:23:12 -0800801 }
802
803 function handleServerEvent(data) {
804 var fn = eventDispatch[data.event] || unknownEvent;
805 fn(data);
806 }
807
808 // ==============================
Simon Hunt61d04042014-11-11 17:27:16 -0800809 // Out-going messages...
810
Simon Huntb53e0682014-11-12 13:32:01 -0800811 function userFeedback(msg) {
812 // for now, use the alert pane as is. Maybe different alert style in
813 // the future (centered on view; dismiss button?)
814 network.view.alert(msg);
815 }
816
817 function nSel() {
818 return selectOrder.length;
819 }
Simon Hunt61d04042014-11-11 17:27:16 -0800820 function getSel(idx) {
821 return selections[selectOrder[idx]];
822 }
Simon Huntb53e0682014-11-12 13:32:01 -0800823 function getSelId(idx) {
824 return getSel(idx).obj.id;
825 }
Thomas Vachuska9edca302014-11-22 17:06:42 -0800826 function getSelIds(start, endOffset) {
827 var end = selectOrder.length - endOffset;
828 var ids = [];
829 selectOrder.slice(start, end).forEach(function (d) {
830 ids.push(getSelId(d));
831 });
832 return ids;
833 }
Simon Huntb53e0682014-11-12 13:32:01 -0800834 function allSelectionsClass(cls) {
835 for (var i=0, n=nSel(); i<n; i++) {
836 if (getSel(i).obj.class !== cls) {
837 return false;
838 }
839 }
840 return true;
841 }
Simon Hunt61d04042014-11-11 17:27:16 -0800842
Thomas Vachuska47635c62014-11-22 01:21:36 -0800843
844 function toggleInstances() {
845 if (!oiBox.isVisible()) {
846 oiBox.show();
847 } else {
848 oiBox.hide();
849 }
850 }
851
852 function toggleSummary() {
853 if (!summaryPane.isVisible()) {
854 requestSummary();
855 } else {
856 cancelSummary();
857 }
858 }
859
860 // request overall summary data
861 function requestSummary() {
862 sendMessage('requestSummary', {});
863 }
864
865 function cancelSummary() {
866 sendMessage('cancelSummary', {});
867 summaryPane.hide();
868 }
869
Simon Hunt61d04042014-11-11 17:27:16 -0800870 // request details for the selected element
Simon Huntd72bc702014-11-13 18:38:04 -0800871 // invoked from selection of a single node.
Simon Hunt61d04042014-11-11 17:27:16 -0800872 function requestDetails() {
873 var data = getSel(0).obj,
874 payload = {
875 id: data.id,
876 class: data.class
877 };
878 sendMessage('requestDetails', payload);
879 }
880
Thomas Vachuska9edca302014-11-22 17:06:42 -0800881 function addHostIntentAction() {
Simon Huntd72bc702014-11-13 18:38:04 -0800882 sendMessage('addHostIntent', {
Thomas Vachuska9edca302014-11-22 17:06:42 -0800883 one: selectOrder[0],
884 two: selectOrder[1],
885 ids: selectOrder
Simon Huntd72bc702014-11-13 18:38:04 -0800886 });
Thomas Vachuska9edca302014-11-22 17:06:42 -0800887 network.view.flash('Host-to-Host flow added');
Simon Huntd72bc702014-11-13 18:38:04 -0800888 }
889
Thomas Vachuska9edca302014-11-22 17:06:42 -0800890 function addMultiSourceIntentAction() {
891 sendMessage('addMultiSourceIntent', {
892 src: selectOrder.slice(0, selectOrder.length - 1),
893 dst: selectOrder[selectOrder.length - 1],
894 ids: selectOrder
895 });
896 network.view.flash('Multi-Source flow added');
Thomas Vachuska29617e52014-11-20 03:17:46 -0800897 }
898
Thomas Vachuska9edca302014-11-22 17:06:42 -0800899
Thomas Vachuska47635c62014-11-22 01:21:36 -0800900 function cancelTraffic() {
901 sendMessage('cancelTraffic', {});
902 }
903
Thomas Vachuska9edca302014-11-22 17:06:42 -0800904 function requestTrafficForMode() {
905 if (hoverMode === hoverModeAll) {
906 requestAllTraffic();
907 } else if (hoverMode === hoverModeFlows) {
908 requestDeviceLinkFlows();
909 } else if (hoverMode === hoverModeIntents) {
910 requestSelectTraffic();
Simon Huntd72bc702014-11-13 18:38:04 -0800911 }
Thomas Vachuska9edca302014-11-22 17:06:42 -0800912 }
Simon Huntd72bc702014-11-13 18:38:04 -0800913
Thomas Vachuska9edca302014-11-22 17:06:42 -0800914 function showTrafficAction() {
915 hoverMode = hoverModeIntents;
916 requestSelectTraffic();
917 network.view.flash('Related Traffic');
918 }
919
920 function requestSelectTraffic() {
921 if (validateSelectionContext()) {
922 var hoverId = (hoverMode === hoverModeIntents && hovered &&
923 (hovered.class === 'host' || hovered.class === 'device'))
Simon Huntd72bc702014-11-13 18:38:04 -0800924 ? hovered.id : '';
Thomas Vachuska9edca302014-11-22 17:06:42 -0800925 sendMessage('requestTraffic', {
926 ids: selectOrder,
927 hover: hoverId
928 });
929 }
Simon Huntd72bc702014-11-13 18:38:04 -0800930 }
931
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800932
Thomas Vachuska29617e52014-11-20 03:17:46 -0800933 function showDeviceLinkFlowsAction() {
Thomas Vachuska9edca302014-11-22 17:06:42 -0800934 hoverMode = hoverModeFlows;
935 requestDeviceLinkFlows();
Thomas Vachuskac59658c2014-11-21 13:13:36 -0800936 network.view.flash('Device Flows');
Thomas Vachuska29617e52014-11-20 03:17:46 -0800937 }
938
Thomas Vachuska9edca302014-11-22 17:06:42 -0800939 function requestDeviceLinkFlows() {
940 if (validateSelectionContext()) {
941 var hoverId = (hoverMode === hoverModeFlows && hovered &&
942 (hovered.class === 'device')) ? hovered.id : '';
943 sendMessage('requestDeviceLinkFlows', {
944 ids: selectOrder,
945 hover: hoverId
946 });
947 }
948 }
949
950
951 function showAllTrafficAction() {
952 hoverMode = hoverModeAll;
953 requestAllTraffic();
954 network.view.flash('All Traffic');
955 }
956
957 function requestAllTraffic() {
958 sendMessage('requestAllTraffic', {});
959 }
960
961 function validateSelectionContext() {
Thomas Vachuska29617e52014-11-20 03:17:46 -0800962 if (!hovered && nSel() === 0) {
Thomas Vachuska47635c62014-11-22 01:21:36 -0800963 cancelTraffic();
Thomas Vachuska9edca302014-11-22 17:06:42 -0800964 return false;
Thomas Vachuska29617e52014-11-20 03:17:46 -0800965 }
Thomas Vachuska9edca302014-11-22 17:06:42 -0800966 return true;
Thomas Vachuska29617e52014-11-20 03:17:46 -0800967 }
Simon Huntd72bc702014-11-13 18:38:04 -0800968
Simon Hunta6a9fe72014-11-20 11:17:12 -0800969 // TODO: these should be moved out to utility module.
Simon Hunt95908012014-11-20 10:20:26 -0800970 function stripPx(s) {
971 return s.replace(/px$/,'');
972 }
Simon Hunta6a9fe72014-11-20 11:17:12 -0800973
Simon Huntb82f6902014-11-22 11:53:15 -0800974 function appendUse(svg, ox, oy, dim, iid, cls) {
975 var use = svg.append('use').attr({
Simon Hunta6a9fe72014-11-20 11:17:12 -0800976 transform: translate(ox,oy),
977 'xlink:href': iid,
978 width: dim,
979 height: dim
Simon Hunta6a9fe72014-11-20 11:17:12 -0800980 });
Simon Huntb82f6902014-11-22 11:53:15 -0800981 if (cls) {
982 use.classed(cls, true);
983 }
984 return use;
985 }
986
987 function appendGlyph(svg, ox, oy, dim, iid, cls) {
988 appendUse(svg, ox, oy, dim, iid, cls).classed('glyphIcon', true);
989 }
990
991 function appendBadge(svg, ox, oy, dim, iid, cls) {
992 appendUse(svg, ox, oy, dim, iid,cls ).classed('badgeIcon', true);
993 }
994
995 function attachUiBadge(svg) {
996 appendBadge(svg, 12, 50, 30, '#uiAttached', 'uiBadge');
Simon Hunta6a9fe72014-11-20 11:17:12 -0800997 }
998
Simon Hunt61d04042014-11-11 17:27:16 -0800999 // ==============================
Simon Hunta5e89142014-11-14 07:00:33 -08001000 // onos instance panel functions
Simon Huntb82f6902014-11-22 11:53:15 -08001001 var instW = 120;
1002
1003 function viewBox(w, h) {
1004 return '0 0 ' + w + ' ' + h;
1005 }
Simon Hunta5e89142014-11-14 07:00:33 -08001006
1007 function updateInstances() {
1008 var onoses = oiBox.el.selectAll('.onosInst')
Simon Huntb82f6902014-11-22 11:53:15 -08001009 .data(onosOrder, function (d) { return d.id; }),
1010 boxW = instW * onosOrder.length;
Simon Hunta5e89142014-11-14 07:00:33 -08001011
Simon Huntb82f6902014-11-22 11:53:15 -08001012 // adjust the width of the panel based on number of instances...
1013 oiBox.width(boxW);
Simon Hunta5e89142014-11-14 07:00:33 -08001014
Simon Huntb82f6902014-11-22 11:53:15 -08001015 // operate on existing onos instances if necessary
1016 onoses.each(function (d) {
1017 var el = d3.select(this),
1018 svg = el.select('svg');
1019
1020 // update online state
1021 el.classed('online', d.online);
1022
1023 // update ui-attached state
1024 svg.select('use.uiBadge').remove();
1025 if (d.uiAttached) {
1026 attachUiBadge(svg);
1027 }
1028
1029 // TODO: update title and property values
1030 });
1031
1032
1033 // operate on new onos instances
Simon Hunta5e89142014-11-14 07:00:33 -08001034 var entering = onoses.enter()
1035 .append('div')
1036 .attr('class', 'onosInst')
1037 .classed('online', function (d) { return d.online; })
Simon Hunt9c15eca2014-11-15 18:37:59 -08001038 .on('click', clickInst);
1039
Simon Huntb82f6902014-11-22 11:53:15 -08001040 entering.each(function (d) {
Simon Hunt9c15eca2014-11-15 18:37:59 -08001041 var el = d3.select(this),
Simon Huntb82f6902014-11-22 11:53:15 -08001042 css = window.getComputedStyle(this),
Simon Hunt95908012014-11-20 10:20:26 -08001043 w = stripPx(css.width),
Simon Huntb82f6902014-11-22 11:53:15 -08001044 h = stripPx(css.height);
Simon Hunt9c15eca2014-11-15 18:37:59 -08001045
Simon Hunt95908012014-11-20 10:20:26 -08001046 var svg = el.append('svg').attr({
1047 width: w,
Simon Huntb82f6902014-11-22 11:53:15 -08001048 height: h,
1049 viewBox: viewBox(w, h)
Simon Hunt95908012014-11-20 10:20:26 -08001050 });
Simon Huntb82f6902014-11-22 11:53:15 -08001051
1052 svg.append('rect')
Thomas Vachuska47635c62014-11-22 01:21:36 -08001053 .attr({
Simon Huntb82f6902014-11-22 11:53:15 -08001054 x: 8,
1055 y: 8,
1056 width: 104,
1057 height: 84,
1058 rx: 12
Thomas Vachuska47635c62014-11-22 01:21:36 -08001059 });
Simon Hunt9c15eca2014-11-15 18:37:59 -08001060
Simon Huntb82f6902014-11-22 11:53:15 -08001061
1062 appendGlyph(svg, 9, 9, 36, '#node');
1063 appendBadge(svg, 17, 19, 21, '#bird');
1064
1065 if (d.uiAttached) {
1066 attachUiBadge(svg);
1067 }
1068
1069 //svg.append('use')
1070 // .attr({
1071 // class: 'birdBadge',
1072 // transform: translate(8,10),
1073 // 'xlink:href': '#bird',
1074 // width: 18,
1075 // height: 18,
1076 // fill: '#fff'
1077 // });
1078 //
1079 //$('<div>').attr('class', 'onosTitle').text(d.id).appendTo(el);
Simon Hunt9c15eca2014-11-15 18:37:59 -08001080
1081 // is the UI attached to this instance?
1082 // TODO: need uiAttached boolean in instance data
Simon Hunta6a9fe72014-11-20 11:17:12 -08001083 // TODO: use SVG glyph, not png..
Simon Hunt9c15eca2014-11-15 18:37:59 -08001084 //if (d.uiAttached) {
Simon Huntb82f6902014-11-22 11:53:15 -08001085 //if (i === 0) {
1086 // $('<img src="img/ui.png">').attr('class','ui').appendTo(el);
1087 //}
Simon Hunt9c15eca2014-11-15 18:37:59 -08001088 });
Simon Hunta5e89142014-11-14 07:00:33 -08001089
1090 // operate on existing + new onoses here
1091
1092 // the departed...
1093 var exiting = onoses.exit()
1094 .transition()
1095 .style('opacity', 0)
1096 .remove();
1097 }
1098
Simon Hunt9462e8c2014-11-14 17:28:09 -08001099 function clickInst(d) {
1100 var el = d3.select(this),
1101 aff = el.classed('affinity');
1102 if (!aff) {
1103 setAffinity(el, d);
1104 } else {
1105 cancelAffinity();
1106 }
1107 }
1108
1109 function setAffinity(el, d) {
1110 d3.selectAll('.onosInst')
1111 .classed('mastership', true)
1112 .classed('affinity', false);
1113 el.classed('affinity', true);
1114
1115 suppressLayers(true);
1116 node.each(function (n) {
1117 if (n.master === d.id) {
1118 n.el.classed('suppressed', false);
1119 }
1120 });
1121 oiShowMaster = true;
1122 }
1123
1124 function cancelAffinity() {
1125 d3.selectAll('.onosInst')
1126 .classed('mastership affinity', false);
1127 restoreLayerState();
1128 oiShowMaster = false;
1129 }
1130
Simon Hunta5e89142014-11-14 07:00:33 -08001131 // ==============================
Simon Hunt99c13842014-11-06 18:23:12 -08001132 // force layout modification functions
1133
1134 function translate(x, y) {
1135 return 'translate(' + x + ',' + y + ')';
1136 }
1137
Simon Hunte2575b62014-11-18 15:25:53 -08001138 function rotate(deg) {
1139 return 'rotate(' + deg + ')';
1140 }
1141
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001142 function missMsg(what, id) {
1143 return '\n[' + what + '] "' + id + '" missing ';
1144 }
1145
1146 function linkEndPoints(srcId, dstId) {
1147 var srcNode = network.lookup[srcId],
1148 dstNode = network.lookup[dstId],
1149 sMiss = !srcNode ? missMsg('src', srcId) : '',
1150 dMiss = !dstNode ? missMsg('dst', dstId) : '';
1151
1152 if (sMiss || dMiss) {
1153 logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
1154 return null;
1155 }
1156 return {
1157 source: srcNode,
1158 target: dstNode,
1159 x1: srcNode.x,
1160 y1: srcNode.y,
1161 x2: dstNode.x,
1162 y2: dstNode.y
1163 };
1164 }
1165
Simon Hunt56d51852014-11-09 13:03:35 -08001166 function createHostLink(host) {
1167 var src = host.id,
1168 dst = host.cp.device,
Simon Hunt7cd48f32014-11-09 23:42:50 -08001169 id = host.ingress,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001170 lnk = linkEndPoints(src, dst);
Simon Hunt56d51852014-11-09 13:03:35 -08001171
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001172 if (!lnk) {
Simon Hunt56d51852014-11-09 13:03:35 -08001173 return null;
1174 }
1175
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001176 // Synthesize link ...
1177 $.extend(lnk, {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001178 key: id,
Simon Hunt56d51852014-11-09 13:03:35 -08001179 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -08001180
1181 type: function () { return 'hostLink'; },
1182 // TODO: ideally, we should see if our edge switch is online...
1183 online: function () { return true; },
1184 linkWidth: function () { return 1; }
Simon Hunt7cd48f32014-11-09 23:42:50 -08001185 });
Simon Hunt99c13842014-11-06 18:23:12 -08001186 return lnk;
1187 }
1188
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001189 function createLink(link) {
Simon Hunta6a9fe72014-11-20 11:17:12 -08001190 var lnk = linkEndPoints(link.src, link.dst);
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001191
1192 if (!lnk) {
1193 return null;
1194 }
1195
Simon Hunt8257f4c2014-11-16 19:34:54 -08001196 $.extend(lnk, {
1197 key: link.id,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001198 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -08001199 fromSource: link,
1200
1201 // functions to aggregate dual link state
1202 type: function () {
1203 var s = lnk.fromSource,
1204 t = lnk.fromTarget;
1205 return (s && s.type) || (t && t.type) || defaultLinkType;
1206 },
1207 online: function () {
1208 var s = lnk.fromSource,
1209 t = lnk.fromTarget;
1210 return (s && s.online) || (t && t.online);
1211 },
1212 linkWidth: function () {
1213 var s = lnk.fromSource,
1214 t = lnk.fromTarget,
1215 ws = (s && s.linkWidth) || 0,
1216 wt = (t && t.linkWidth) || 0;
1217 return Math.max(ws, wt);
1218 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001219 });
1220 return lnk;
Simon Hunt1a9eff92014-11-07 11:06:34 -08001221 }
1222
Simon Hunte2575b62014-11-18 15:25:53 -08001223 function removeLinkLabels() {
1224 network.links.forEach(function (d) {
1225 d.label = '';
1226 });
1227 }
1228
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001229 var widthRatio = 1.4,
1230 linkScale = d3.scale.linear()
1231 .domain([1, 12])
1232 .range([widthRatio, 12 * widthRatio])
1233 .clamp(true);
1234
Simon Hunt99c13842014-11-06 18:23:12 -08001235 function updateLinks() {
1236 link = linkG.selectAll('.link')
Simon Hunt8257f4c2014-11-16 19:34:54 -08001237 .data(network.links, function (d) { return d.key; });
Simon Hunt99c13842014-11-06 18:23:12 -08001238
1239 // operate on existing links, if necessary
1240 // link .foo() .bar() ...
1241
1242 // operate on entering links:
1243 var entering = link.enter()
1244 .append('line')
1245 .attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001246 x1: function (d) { return d.x1; },
1247 y1: function (d) { return d.y1; },
1248 x2: function (d) { return d.x2; },
1249 y2: function (d) { return d.y2; },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001250 stroke: config.topo.linkInColor,
1251 'stroke-width': config.topo.linkInWidth
Simon Hunt99c13842014-11-06 18:23:12 -08001252 });
1253
1254 // augment links
Simon Hunt7cd48f32014-11-09 23:42:50 -08001255 entering.each(function (d) {
1256 var link = d3.select(this);
1257 // provide ref to element selection from backing data....
1258 d.el = link;
Simon Hunt8257f4c2014-11-16 19:34:54 -08001259 restyleLinkElement(d);
Simon Hunt7cd48f32014-11-09 23:42:50 -08001260 });
Thomas Vachuska4830d392014-11-09 17:09:56 -08001261
1262 // operate on both existing and new links, if necessary
1263 //link .foo() .bar() ...
1264
Simon Hunte2575b62014-11-18 15:25:53 -08001265 // apply or remove labels
1266 var labelData = getLabelData();
1267 applyLinkLabels(labelData);
1268
Thomas Vachuska4830d392014-11-09 17:09:56 -08001269 // operate on exiting links:
Thomas Vachuska4830d392014-11-09 17:09:56 -08001270 link.exit()
Simon Hunt13bf9c82014-11-18 07:26:44 -08001271 .attr('stroke-dasharray', '3, 3')
1272 .style('opacity', 0.5)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001273 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -08001274 .duration(1500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001275 .attr({
Simon Hunt13bf9c82014-11-18 07:26:44 -08001276 'stroke-dasharray': '3, 12',
1277 stroke: config.topo.linkOutColor,
1278 'stroke-width': config.topo.linkOutWidth
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001279 })
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001280 .style('opacity', 0.0)
Thomas Vachuska4830d392014-11-09 17:09:56 -08001281 .remove();
Simon Hunte2575b62014-11-18 15:25:53 -08001282
1283 // NOTE: invoke a single tick to force the labels to position
1284 // onto their links.
1285 tick();
1286 }
1287
1288 function getLabelData() {
1289 // create the backing data for showing labels..
1290 var data = [];
1291 link.each(function (d) {
1292 if (d.label) {
1293 data.push({
1294 id: 'lab-' + d.key,
1295 key: d.key,
1296 label: d.label,
1297 ldata: d
1298 });
1299 }
1300 });
1301 return data;
1302 }
1303
1304 var linkLabelOffset = '0.3em';
1305
1306 function applyLinkLabels(data) {
1307 var entering;
1308
1309 linkLabel = linkLabelG.selectAll('.linkLabel')
1310 .data(data, function (d) { return d.id; });
1311
Simon Hunt56a2ea42014-11-19 12:39:31 -08001312 // for elements already existing, we need to update the text
1313 // and adjust the rectangle size to fit
1314 linkLabel.each(function (d) {
1315 var el = d3.select(this),
1316 rect = el.select('rect'),
1317 text = el.select('text');
1318 text.text(d.label);
1319 rect.attr(rectAroundText(el));
1320 });
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -08001321
Simon Hunte2575b62014-11-18 15:25:53 -08001322 entering = linkLabel.enter().append('g')
1323 .classed('linkLabel', true)
1324 .attr('id', function (d) { return d.id; });
1325
1326 entering.each(function (d) {
1327 var el = d3.select(this),
1328 rect,
1329 text,
1330 parms = {
1331 x1: d.ldata.x1,
1332 y1: d.ldata.y1,
1333 x2: d.ldata.x2,
1334 y2: d.ldata.y2
1335 };
1336
1337 d.el = el;
1338 rect = el.append('rect');
1339 text = el.append('text').text(d.label);
1340 rect.attr(rectAroundText(el));
1341 text.attr('dy', linkLabelOffset);
1342
1343 el.attr('transform', transformLabel(parms));
1344 });
1345
1346 // Remove any links that are no longer required.
1347 linkLabel.exit().remove();
1348 }
1349
1350 function rectAroundText(el) {
1351 var text = el.select('text'),
1352 box = text.node().getBBox();
1353
1354 // translate the bbox so that it is centered on [x,y]
1355 box.x = -box.width / 2;
1356 box.y = -box.height / 2;
1357
1358 // add padding
1359 box.x -= 1;
1360 box.width += 2;
1361 return box;
1362 }
1363
1364 function transformLabel(p) {
1365 var dx = p.x2 - p.x1,
1366 dy = p.y2 - p.y1,
1367 xMid = dx/2 + p.x1,
1368 yMid = dy/2 + p.y1;
Simon Hunte2575b62014-11-18 15:25:53 -08001369 return translate(xMid, yMid);
Simon Hunt99c13842014-11-06 18:23:12 -08001370 }
1371
1372 function createDeviceNode(device) {
1373 // start with the object as is
1374 var node = device,
Simon Huntbb282f52014-11-10 11:08:19 -08001375 type = device.type,
Simon Huntc72967b2014-11-20 09:21:42 -08001376 svgCls = type ? 'node device ' + type : 'node device',
1377 labels = device.labels || [];
1378
Simon Hunt99c13842014-11-06 18:23:12 -08001379 // Augment as needed...
1380 node.class = 'device';
Simon Huntbb282f52014-11-10 11:08:19 -08001381 node.svgClass = device.online ? svgCls + ' online' : svgCls;
Simon Hunt99c13842014-11-06 18:23:12 -08001382 positionNode(node);
Simon Hunt99c13842014-11-06 18:23:12 -08001383 return node;
1384 }
1385
Simon Hunt56d51852014-11-09 13:03:35 -08001386 function createHostNode(host) {
1387 // start with the object as is
1388 var node = host;
1389
1390 // Augment as needed...
1391 node.class = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001392 if (!node.type) {
Simon Hunt209155e2014-11-21 12:16:09 -08001393 node.type = 'endstation';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001394 }
Simon Hunt7fa116d2014-11-17 14:16:55 -08001395 node.svgClass = 'node host ' + node.type;
Simon Hunt56d51852014-11-09 13:03:35 -08001396 positionNode(node);
Simon Hunt56d51852014-11-09 13:03:35 -08001397 return node;
1398 }
1399
Simon Hunt99c13842014-11-06 18:23:12 -08001400 function positionNode(node) {
1401 var meta = node.metaUi,
Simon Huntac9e24f2014-11-12 10:12:21 -08001402 x = meta && meta.x,
1403 y = meta && meta.y,
1404 xy;
Simon Hunt99c13842014-11-06 18:23:12 -08001405
Simon Huntac9e24f2014-11-12 10:12:21 -08001406 // If we have [x,y] already, use that...
Simon Hunt99c13842014-11-06 18:23:12 -08001407 if (x && y) {
1408 node.fixed = true;
Simon Huntac9e24f2014-11-12 10:12:21 -08001409 node.x = x;
1410 node.y = y;
1411 return;
Simon Hunt99c13842014-11-06 18:23:12 -08001412 }
Simon Huntac9e24f2014-11-12 10:12:21 -08001413
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001414 var location = node.location;
1415 if (location && location.type === 'latlng') {
1416 var coord = geoMapProjection([location.lng, location.lat]);
1417 node.fixed = true;
1418 node.x = coord[0];
1419 node.y = coord[1];
1420 return;
1421 }
1422
Simon Huntac9e24f2014-11-12 10:12:21 -08001423 // Note: Placing incoming unpinned nodes at exactly the same point
1424 // (center of the view) causes them to explode outwards when
1425 // the force layout kicks in. So, we spread them out a bit
1426 // initially, to provide a more serene layout convergence.
1427 // Additionally, if the node is a host, we place it near
1428 // the device it is connected to.
1429
1430 function spread(s) {
1431 return Math.floor((Math.random() * s) - s/2);
1432 }
1433
1434 function randDim(dim) {
1435 return dim / 2 + spread(dim * 0.7071);
1436 }
1437
1438 function rand() {
1439 return {
1440 x: randDim(network.view.width()),
1441 y: randDim(network.view.height())
1442 };
1443 }
1444
1445 function near(node) {
1446 var min = 12,
1447 dx = spread(12),
1448 dy = spread(12);
1449 return {
1450 x: node.x + min + dx,
1451 y: node.y + min + dy
1452 };
1453 }
1454
1455 function getDevice(cp) {
1456 var d = network.lookup[cp.device];
1457 return d || rand();
1458 }
1459
1460 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
1461 $.extend(node, xy);
Simon Hunt99c13842014-11-06 18:23:12 -08001462 }
1463
Thomas Vachuska89543292014-11-19 11:28:33 -08001464 function iconUrl(d) {
1465 return 'img/' + d.type + '.png';
Simon Hunt99c13842014-11-06 18:23:12 -08001466 }
1467
Simon Huntc72967b2014-11-20 09:21:42 -08001468 function iconGlyphUrl(d) {
1469 var which = d.type || 'unknown';
1470 return '#' + which;
1471 }
1472
Simon Hunt99c13842014-11-06 18:23:12 -08001473 // returns the newly computed bounding box of the rectangle
1474 function adjustRectToFitText(n) {
1475 var text = n.select('text'),
1476 box = text.node().getBBox(),
1477 lab = config.labels;
1478
1479 text.attr('text-anchor', 'middle')
1480 .attr('y', '-0.8em')
1481 .attr('x', lab.imgPad/2);
1482
1483 // translate the bbox so that it is centered on [x,y]
1484 box.x = -box.width / 2;
1485 box.y = -box.height / 2;
1486
1487 // add padding
1488 box.x -= (lab.padLR + lab.imgPad/2);
1489 box.width += lab.padLR * 2 + lab.imgPad;
1490 box.y -= lab.padTB;
1491 box.height += lab.padTB * 2;
1492
1493 return box;
1494 }
1495
Simon Hunt1a9eff92014-11-07 11:06:34 -08001496 function mkSvgClass(d) {
1497 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
1498 }
1499
Simon Hunt7cd48f32014-11-09 23:42:50 -08001500 function hostLabel(d) {
1501 var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0;
1502 return d.labels[idx];
1503 }
1504 function deviceLabel(d) {
1505 var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0;
1506 return d.labels[idx];
1507 }
Simon Huntc72967b2014-11-20 09:21:42 -08001508 function trimLabel(label) {
1509 return (label && label.trim()) || '';
1510 }
1511
1512 function emptyBox() {
1513 return {
1514 x: -2,
1515 y: -2,
1516 width: 4,
1517 height: 4
1518 };
Simon Hunt7cd48f32014-11-09 23:42:50 -08001519 }
1520
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001521 function updateDeviceLabel(d) {
Simon Huntc72967b2014-11-20 09:21:42 -08001522 var label = trimLabel(deviceLabel(d)),
1523 noLabel = !label,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001524 node = d.el,
Simon Huntc72967b2014-11-20 09:21:42 -08001525 box,
1526 dx,
1527 dy;
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001528
1529 node.select('text')
1530 .text(label)
1531 .style('opacity', 0)
1532 .transition()
1533 .style('opacity', 1);
1534
Simon Huntc72967b2014-11-20 09:21:42 -08001535 if (noLabel) {
1536 box = emptyBox();
1537 dx = -config.icons.device.dim/2;
1538 dy = -config.icons.device.dim/2;
1539 } else {
1540 box = adjustRectToFitText(node);
1541 dx = box.x + config.icons.xoff;
1542 dy = box.y + config.icons.yoff;
1543 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001544
1545 node.select('rect')
1546 .transition()
1547 .attr(box);
1548
Simon Huntc72967b2014-11-20 09:21:42 -08001549 node.select('g.deviceIcon')
Thomas Vachuska89543292014-11-19 11:28:33 -08001550 .transition()
Simon Huntc72967b2014-11-20 09:21:42 -08001551 .attr('transform', translate(dx, dy));
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001552 }
1553
1554 function updateHostLabel(d) {
1555 var label = hostLabel(d),
1556 host = d.el;
1557
1558 host.select('text').text(label);
1559 }
1560
Simon Hunta6a9fe72014-11-20 11:17:12 -08001561 // TODO: should be using updateNodes() to do the upates!
Simon Huntbb282f52014-11-10 11:08:19 -08001562 function updateDeviceState(nodeData) {
1563 nodeData.el.classed('online', nodeData.online);
1564 updateDeviceLabel(nodeData);
1565 // TODO: review what else might need to be updated
1566 }
1567
1568 function updateHostState(hostData) {
1569 updateHostLabel(hostData);
1570 // TODO: review what else might need to be updated
1571 }
1572
Simon Hunt6ac93f32014-11-13 12:17:27 -08001573 function nodeMouseOver(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001574 hovered = d;
Thomas Vachuska9edca302014-11-22 17:06:42 -08001575 requestTrafficForMode();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001576 }
1577
1578 function nodeMouseOut(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001579 hovered = null;
Thomas Vachuska9edca302014-11-22 17:06:42 -08001580 requestTrafficForMode();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001581 }
Simon Huntbb282f52014-11-10 11:08:19 -08001582
Simon Hunteb1514d2014-11-20 09:57:29 -08001583 function addHostIcon(node, radius, iid) {
Thomas Vachuska89543292014-11-19 11:28:33 -08001584 var dim = radius * 1.5,
1585 xlate = -dim / 2;
1586
Simon Hunteb1514d2014-11-20 09:57:29 -08001587 node.append('use').attr({
1588 class: 'glyphIcon hostIcon',
1589 transform: translate(xlate,xlate),
1590 'xlink:href': iid,
1591 width: dim,
1592 height: dim
1593 });
Thomas Vachuska89543292014-11-19 11:28:33 -08001594 }
1595
Simon Hunt99c13842014-11-06 18:23:12 -08001596 function updateNodes() {
1597 node = nodeG.selectAll('.node')
1598 .data(network.nodes, function (d) { return d.id; });
1599
Simon Huntc72967b2014-11-20 09:21:42 -08001600 // TODO: operate on existing nodes
Simon Hunt7cd48f32014-11-09 23:42:50 -08001601 // update host labels
Simon Hunt99c13842014-11-06 18:23:12 -08001602 //node .foo() .bar() ...
1603
1604 // operate on entering nodes:
1605 var entering = node.enter()
1606 .append('g')
1607 .attr({
1608 id: function (d) { return safeId(d.id); },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001609 class: mkSvgClass,
Simon Hunt99c13842014-11-06 18:23:12 -08001610 transform: function (d) { return translate(d.x, d.y); },
1611 opacity: 0
1612 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08001613 .call(network.drag)
Simon Hunt6ac93f32014-11-13 12:17:27 -08001614 .on('mouseover', nodeMouseOver)
1615 .on('mouseout', nodeMouseOut)
Simon Hunt99c13842014-11-06 18:23:12 -08001616 .transition()
1617 .attr('opacity', 1);
1618
1619 // augment device nodes...
1620 entering.filter('.device').each(function (d) {
1621 var node = d3.select(this),
Simon Huntc72967b2014-11-20 09:21:42 -08001622 label = trimLabel(deviceLabel(d)),
1623 noLabel = !label,
Simon Hunt99c13842014-11-06 18:23:12 -08001624 box;
1625
Simon Hunt7cd48f32014-11-09 23:42:50 -08001626 // provide ref to element from backing data....
1627 d.el = node;
1628
Simon Hunt99c13842014-11-06 18:23:12 -08001629 node.append('rect')
1630 .attr({
Simon Hunta3dd9572014-11-20 15:22:41 -08001631 rx: 5,
1632 ry: 5
Simon Hunt99c13842014-11-06 18:23:12 -08001633 });
1634
1635 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001636 .text(label)
Simon Hunt99c13842014-11-06 18:23:12 -08001637 .attr('dy', '1.1em');
1638
1639 box = adjustRectToFitText(node);
Simon Hunta3dd9572014-11-20 15:22:41 -08001640 node.select('rect').attr(box);
Simon Huntc72967b2014-11-20 09:21:42 -08001641 addDeviceIcon(node, box, noLabel, iconGlyphUrl(d));
Simon Huntc7ee0662014-11-05 16:44:37 -08001642 });
Simon Hunt934c3ce2014-11-05 11:45:07 -08001643
Thomas Vachuska89543292014-11-19 11:28:33 -08001644 // TODO: better place for this configuration state
1645 var defaultHostRadius = 9,
1646 hostRadius = {
1647 bgpSpeaker: 14,
1648 router: 14,
Thomas Vachuska60d72bf2014-11-21 13:02:00 -08001649 endstation: 14
Thomas Vachuska89543292014-11-19 11:28:33 -08001650 },
Simon Hunteb1514d2014-11-20 09:57:29 -08001651 hostGlyphId = {
Thomas Vachuska89543292014-11-19 11:28:33 -08001652 bgpSpeaker: 'bgpSpeaker',
1653 router: 'router',
Thomas Vachuska60d72bf2014-11-21 13:02:00 -08001654 endstation: 'endstation'
Thomas Vachuska89543292014-11-19 11:28:33 -08001655 };
1656
1657
Simon Hunt56d51852014-11-09 13:03:35 -08001658 // augment host nodes...
1659 entering.filter('.host').each(function (d) {
1660 var node = d3.select(this),
Thomas Vachuska89543292014-11-19 11:28:33 -08001661 r = hostRadius[d.type] || defaultHostRadius,
1662 textDy = r + 10,
Simon Hunteb1514d2014-11-20 09:57:29 -08001663 iid = iconGlyphUrl(d);
Simon Hunt56d51852014-11-09 13:03:35 -08001664
Simon Hunt7cd48f32014-11-09 23:42:50 -08001665 // provide ref to element from backing data....
1666 d.el = node;
1667
Thomas Vachuska89543292014-11-19 11:28:33 -08001668 node.append('circle')
1669 .attr('r', r);
Simon Hunt7fa116d2014-11-17 14:16:55 -08001670
Simon Hunteb1514d2014-11-20 09:57:29 -08001671 if (iid) {
1672 addHostIcon(node, r, iid);
Simon Hunt7fa116d2014-11-17 14:16:55 -08001673 }
Simon Hunt56d51852014-11-09 13:03:35 -08001674
Simon Hunt56d51852014-11-09 13:03:35 -08001675 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001676 .text(hostLabel)
Thomas Vachuska89543292014-11-19 11:28:33 -08001677 .attr('dy', textDy)
Simon Hunt7cd48f32014-11-09 23:42:50 -08001678 .attr('text-anchor', 'middle');
Simon Hunt56d51852014-11-09 13:03:35 -08001679
1680 // debug function to show the modelled x,y coordinates of nodes...
1681 if (debug('showNodeXY')) {
1682 node.select('circle').attr('fill-opacity', 0.5);
1683 node.append('circle')
1684 .attr({
1685 class: 'debug',
1686 cx: 0,
1687 cy: 0,
1688 r: '3px'
1689 });
1690 }
1691 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001692
Simon Hunt99c13842014-11-06 18:23:12 -08001693 // operate on both existing and new nodes, if necessary
1694 //node .foo() .bar() ...
Simon Huntc7ee0662014-11-05 16:44:37 -08001695
Simon Hunt99c13842014-11-06 18:23:12 -08001696 // operate on exiting nodes:
Simon Huntea80eb42014-11-11 13:46:57 -08001697 // Note that the node is removed after 2 seconds.
1698 // Sub element animations should be shorter than 2 seconds.
1699 var exiting = node.exit()
Simon Hunt44031102014-11-11 13:20:36 -08001700 .transition()
1701 .duration(2000)
Simon Huntea80eb42014-11-11 13:46:57 -08001702 .style('opacity', 0)
Simon Hunt99c13842014-11-06 18:23:12 -08001703 .remove();
Simon Huntea80eb42014-11-11 13:46:57 -08001704
1705 // host node exits....
1706 exiting.filter('.host').each(function (d) {
1707 var node = d3.select(this);
1708
1709 node.select('text')
1710 .style('opacity', 0.5)
1711 .transition()
1712 .duration(1000)
1713 .style('opacity', 0);
1714 // note, leave <g>.remove to remove this element
1715
Thomas Vachuska89543292014-11-19 11:28:33 -08001716 node.select('circle')
1717 .style('stroke-fill', '#555')
1718 .style('fill', '#888')
Simon Huntea80eb42014-11-11 13:46:57 -08001719 .style('opacity', 0.5)
1720 .transition()
1721 .duration(1500)
1722 .attr('r', 0);
1723 // note, leave <g>.remove to remove this element
1724
1725 });
1726
1727 // TODO: device node exits
Simon Huntc7ee0662014-11-05 16:44:37 -08001728 }
1729
Simon Huntc72967b2014-11-20 09:21:42 -08001730 function addDeviceIcon(node, box, noLabel, iid) {
1731 var cfg = config.icons.device,
1732 dx,
1733 dy,
1734 g;
1735
1736 if (noLabel) {
1737 box = emptyBox();
1738 dx = -cfg.dim/2;
1739 dy = -cfg.dim/2;
1740 } else {
1741 box = adjustRectToFitText(node);
1742 dx = box.x + config.icons.xoff;
1743 dy = box.y + config.icons.yoff;
1744 }
1745
Simon Hunteb1514d2014-11-20 09:57:29 -08001746 g = node.append('g')
1747 .attr('class', 'glyphIcon deviceIcon')
Simon Huntc72967b2014-11-20 09:21:42 -08001748 .attr('transform', translate(dx, dy));
1749
1750 g.append('rect').attr({
1751 x: 0,
1752 y: 0,
1753 rx: cfg.rx,
1754 width: cfg.dim,
1755 height: cfg.dim
1756 });
1757
1758 g.append('use').attr({
1759 'xlink:href': iid,
1760 width: cfg.dim,
1761 height: cfg.dim
1762 });
1763
Simon Huntc72967b2014-11-20 09:21:42 -08001764 }
1765
Simon Hunt8257f4c2014-11-16 19:34:54 -08001766 function find(key, array) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001767 for (var idx = 0, n = array.length; idx < n; idx++) {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001768 if (array[idx].key === key) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001769 return idx;
1770 }
1771 }
1772 return -1;
1773 }
1774
1775 function removeLinkElement(linkData) {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001776 var idx = find(linkData.key, network.links),
1777 removed;
1778 if (idx >=0) {
1779 // remove from links array
1780 removed = network.links.splice(idx, 1);
1781 // remove from lookup cache
1782 delete network.lookup[removed[0].key];
1783 updateLinks();
1784 network.force.resume();
1785 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001786 }
Simon Huntc7ee0662014-11-05 16:44:37 -08001787
Simon Hunt44031102014-11-11 13:20:36 -08001788 function removeHostElement(hostData) {
1789 // first, remove associated hostLink...
1790 removeLinkElement(hostData.linkData);
1791
1792 // remove from lookup cache
1793 delete network.lookup[hostData.id];
1794 // remove from nodes array
1795 var idx = find(hostData.id, network.nodes);
1796 network.nodes.splice(idx, 1);
1797 // remove from SVG
1798 updateNodes();
1799 network.force.resume();
1800 }
1801
1802
Simon Huntc7ee0662014-11-05 16:44:37 -08001803 function tick() {
1804 node.attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001805 transform: function (d) { return translate(d.x, d.y); }
Simon Huntc7ee0662014-11-05 16:44:37 -08001806 });
1807
1808 link.attr({
1809 x1: function (d) { return d.source.x; },
1810 y1: function (d) { return d.source.y; },
1811 x2: function (d) { return d.target.x; },
1812 y2: function (d) { return d.target.y; }
1813 });
Simon Hunte2575b62014-11-18 15:25:53 -08001814
1815 linkLabel.each(function (d) {
1816 var el = d3.select(this);
Thomas Vachuska4731f122014-11-20 04:56:19 -08001817 var lnk = findLinkById(d.key);
1818
1819 if (lnk) {
1820 var parms = {
Simon Hunte2575b62014-11-18 15:25:53 -08001821 x1: lnk.source.x,
1822 y1: lnk.source.y,
1823 x2: lnk.target.x,
1824 y2: lnk.target.y
1825 };
Thomas Vachuska4731f122014-11-20 04:56:19 -08001826 el.attr('transform', transformLabel(parms));
1827 }
Simon Hunte2575b62014-11-18 15:25:53 -08001828 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001829 }
Simon Hunt934c3ce2014-11-05 11:45:07 -08001830
1831 // ==============================
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001832 // Web-Socket for live data
1833
1834 function webSockUrl() {
1835 return document.location.toString()
1836 .replace(/\#.*/, '')
1837 .replace('http://', 'ws://')
1838 .replace('https://', 'wss://')
1839 .replace('index2.html', config.webSockUrl);
1840 }
1841
1842 webSock = {
1843 ws : null,
1844
1845 connect : function() {
1846 webSock.ws = new WebSocket(webSockUrl());
1847
1848 webSock.ws.onopen = function() {
Simon Hunt0c6d4192014-11-12 12:07:10 -08001849 noWebSock(false);
Thomas Vachuska47635c62014-11-22 01:21:36 -08001850 requestSummary();
1851 oiBox.show();
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001852 };
1853
1854 webSock.ws.onmessage = function(m) {
1855 if (m.data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001856 wsTraceRx(m.data);
Thomas Vachuskad472c6e2014-11-07 19:11:05 -08001857 handleServerEvent(JSON.parse(m.data));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001858 }
1859 };
1860
1861 webSock.ws.onclose = function(m) {
1862 webSock.ws = null;
Simon Hunt0c6d4192014-11-12 12:07:10 -08001863 noWebSock(true);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001864 };
1865 },
1866
1867 send : function(text) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001868 if (text != null) {
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001869 webSock._send(text);
1870 }
1871 },
1872
1873 _send : function(message) {
1874 if (webSock.ws) {
1875 webSock.ws.send(message);
Simon Hunta255a2c2014-11-13 22:29:35 -08001876 } else if (config.useLiveData) {
Simon Hunt56d51852014-11-09 13:03:35 -08001877 network.view.alert('no web socket open\n\n' + message);
Simon Hunta255a2c2014-11-13 22:29:35 -08001878 } else {
1879 console.log('WS Send: ' + JSON.stringify(message));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001880 }
1881 }
1882
1883 };
1884
Simon Hunt0c6d4192014-11-12 12:07:10 -08001885 function noWebSock(b) {
1886 mask.style('display',b ? 'block' : 'none');
1887 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001888
1889 function sendMessage(evType, payload) {
1890 var toSend = {
Simon Huntbb282f52014-11-10 11:08:19 -08001891 event: evType,
1892 sid: ++sid,
1893 payload: payload
1894 },
1895 asText = JSON.stringify(toSend);
1896 wsTraceTx(asText);
1897 webSock.send(asText);
Simon Huntc76ae892014-11-18 17:31:51 -08001898
1899 // Temporary measure for debugging UI behavior ...
1900 if (!config.useLiveData) {
1901 handleTestSend(toSend);
1902 }
Simon Huntbb282f52014-11-10 11:08:19 -08001903 }
1904
1905 function wsTraceTx(msg) {
1906 wsTrace('tx', msg);
1907 }
1908 function wsTraceRx(msg) {
1909 wsTrace('rx', msg);
1910 }
1911 function wsTrace(rxtx, msg) {
Simon Huntbb282f52014-11-10 11:08:19 -08001912 console.log('[' + rxtx + '] ' + msg);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001913 }
1914
Simon Huntc76ae892014-11-18 17:31:51 -08001915 // NOTE: Temporary hardcoded example for showing detail pane
1916 // while we fine-
1917 // Probably should not merge this change...
1918 function handleTestSend(msg) {
1919 if (msg.event === 'requestDetails') {
1920 showDetails({
1921 event: 'showDetails',
1922 sid: 1001,
1923 payload: {
1924 "id": "of:0000ffffffffff09",
1925 "type": "roadm",
1926 "propOrder": [
1927 "Name",
1928 "Vendor",
1929 "H/W Version",
1930 "S/W Version",
1931 "-",
1932 "Latitude",
1933 "Longitude",
1934 "Ports"
1935 ],
1936 "props": {
1937 "Name": null,
1938 "Vendor": "Linc",
1939 "H/W Version": "OE",
1940 "S/W Version": "?",
1941 "-": "",
1942 "Latitude": "40.8",
1943 "Longitude": "73.1",
1944 "Ports": "2"
1945 }
1946 }
1947 });
1948 }
1949 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001950
1951 // ==============================
1952 // Selection stuff
1953
1954 function selectObject(obj, el) {
1955 var n,
Simon Hunt01095ff2014-11-13 16:37:29 -08001956 srcEv = d3.event.sourceEvent,
1957 meta = srcEv.metaKey,
1958 shift = srcEv.shiftKey;
1959
Simon Huntdeab4322014-11-13 18:49:07 -08001960 if ((panZoom() && !meta) || (!panZoom() && meta)) {
Simon Hunt01095ff2014-11-13 16:37:29 -08001961 return;
1962 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001963
1964 if (el) {
1965 n = d3.select(el);
1966 } else {
1967 node.each(function(d) {
1968 if (d == obj) {
1969 n = d3.select(el = this);
1970 }
1971 });
1972 }
1973 if (!n) return;
1974
Simon Hunt01095ff2014-11-13 16:37:29 -08001975 if (shift && n.classed('selected')) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001976 deselectObject(obj.id);
Simon Hunt61d04042014-11-11 17:27:16 -08001977 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001978 return;
1979 }
1980
Simon Hunt01095ff2014-11-13 16:37:29 -08001981 if (!shift) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001982 deselectAll();
1983 }
1984
Simon Huntc31d5692014-11-12 13:27:18 -08001985 selections[obj.id] = { obj: obj, el: el };
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001986 selectOrder.push(obj.id);
1987
1988 n.classed('selected', true);
Simon Hunt61d04042014-11-11 17:27:16 -08001989 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001990 }
1991
1992 function deselectObject(id) {
Simon Huntc31d5692014-11-12 13:27:18 -08001993 var obj = selections[id],
1994 idx;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001995 if (obj) {
1996 d3.select(obj.el).classed('selected', false);
Simon Hunt61d04042014-11-11 17:27:16 -08001997 delete selections[id];
Simon Huntc31d5692014-11-12 13:27:18 -08001998 idx = $.inArray(id, selectOrder);
1999 if (idx >= 0) {
2000 selectOrder.splice(idx, 1);
2001 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002002 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002003 }
2004
2005 function deselectAll() {
2006 // deselect all nodes in the network...
2007 node.classed('selected', false);
2008 selections = {};
2009 selectOrder = [];
Simon Hunt61d04042014-11-11 17:27:16 -08002010 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002011 }
2012
Thomas Vachuska47635c62014-11-22 01:21:36 -08002013 // update the state of the sumary pane
2014 function updateSummaryPane() {
2015
2016 }
2017
Simon Hunt61d04042014-11-11 17:27:16 -08002018 // update the state of the detail pane, based on current selections
2019 function updateDetailPane() {
2020 var nSel = selectOrder.length;
2021 if (!nSel) {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002022 emptySelect();
Simon Hunt61d04042014-11-11 17:27:16 -08002023 } else if (nSel === 1) {
2024 singleSelect();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002025 requestTrafficForMode();
Simon Hunt61d04042014-11-11 17:27:16 -08002026 } else {
2027 multiSelect();
2028 }
2029 }
2030
Thomas Vachuska9edca302014-11-22 17:06:42 -08002031 function emptySelect() {
2032 detailPane.hide();
2033 cancelTraffic();
2034 }
2035
Simon Hunt61d04042014-11-11 17:27:16 -08002036 function singleSelect() {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002037 // NOTE: detail is shown from showDetails event callback
Simon Hunt61d04042014-11-11 17:27:16 -08002038 requestDetails();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002039 requestTrafficForMode();
Simon Hunt61d04042014-11-11 17:27:16 -08002040 }
2041
2042 function multiSelect() {
Simon Huntb53e0682014-11-12 13:32:01 -08002043 populateMultiSelect();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002044 requestTrafficForMode();
Simon Huntb53e0682014-11-12 13:32:01 -08002045 }
2046
2047 function addSep(tbody) {
2048 var tr = tbody.append('tr');
2049 $('<hr>').appendTo(tr.append('td').attr('colspan', 2));
2050 }
2051
2052 function addProp(tbody, label, value) {
2053 var tr = tbody.append('tr');
2054
2055 tr.append('td')
2056 .attr('class', 'label')
2057 .text(label + ' :');
2058
2059 tr.append('td')
2060 .attr('class', 'value')
2061 .text(value);
2062 }
2063
2064 function populateMultiSelect() {
2065 detailPane.empty();
2066
Simon Hunta3dd9572014-11-20 15:22:41 -08002067 var title = detailPane.append('h3'),
2068 table = detailPane.append('table'),
2069 tbody = table.append('tbody');
Simon Huntb53e0682014-11-12 13:32:01 -08002070
Thomas Vachuska4731f122014-11-20 04:56:19 -08002071 title.text('Selected Nodes');
Simon Huntb53e0682014-11-12 13:32:01 -08002072
2073 selectOrder.forEach(function (d, i) {
2074 addProp(tbody, i+1, d);
2075 });
Simon Huntd72bc702014-11-13 18:38:04 -08002076
2077 addMultiSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08002078 }
2079
Thomas Vachuska47635c62014-11-22 01:21:36 -08002080 // TODO: refactor to consolidate with populateDetails
2081 function populateSummary(data) {
2082 summaryPane.empty();
2083
2084 var svg = summaryPane.append('svg'),
2085 iid = iconGlyphUrl(data);
2086
2087 var title = summaryPane.append('h2'),
2088 table = summaryPane.append('table'),
2089 tbody = table.append('tbody');
2090
2091 appendGlyph(svg, 0, 0, 40, iid);
2092
2093 svg.append('use')
2094 .attr({
2095 class: 'birdBadge',
2096 transform: translate(8,12),
2097 'xlink:href': '#bird',
2098 width: 24,
2099 height: 24,
2100 fill: '#fff'
2101 });
2102
2103 title.text('ONOS Summary');
2104
2105 data.propOrder.forEach(function(p) {
2106 if (p === '-') {
2107 addSep(tbody);
2108 } else {
2109 addProp(tbody, p, data.props[p]);
2110 }
2111 });
2112 }
2113
Simon Hunt61d04042014-11-11 17:27:16 -08002114 function populateDetails(data) {
2115 detailPane.empty();
2116
Simon Hunta6a9fe72014-11-20 11:17:12 -08002117 var svg = detailPane.append('svg'),
2118 iid = iconGlyphUrl(data);
2119
Simon Hunta3dd9572014-11-20 15:22:41 -08002120 var title = detailPane.append('h2'),
2121 table = detailPane.append('table'),
2122 tbody = table.append('tbody');
Simon Hunt61d04042014-11-11 17:27:16 -08002123
Simon Hunta6a9fe72014-11-20 11:17:12 -08002124 appendGlyph(svg, 0, 0, 40, iid);
2125 title.text(data.id);
Simon Hunt61d04042014-11-11 17:27:16 -08002126
2127 data.propOrder.forEach(function(p) {
2128 if (p === '-') {
2129 addSep(tbody);
2130 } else {
2131 addProp(tbody, p, data.props[p]);
2132 }
2133 });
Simon Huntd72bc702014-11-13 18:38:04 -08002134
Thomas Vachuska4731f122014-11-20 04:56:19 -08002135 addSingleSelectActions(data);
Simon Hunt61d04042014-11-11 17:27:16 -08002136 }
2137
Thomas Vachuska4731f122014-11-20 04:56:19 -08002138 function addSingleSelectActions(data) {
Simon Huntd72bc702014-11-13 18:38:04 -08002139 detailPane.append('hr');
2140 // always want to allow 'show traffic'
Thomas Vachuska4731f122014-11-20 04:56:19 -08002141 addAction(detailPane, 'Show Related Traffic', showTrafficAction);
2142
2143 if (data.type === 'switch') {
2144 addAction(detailPane, 'Show Device Flows', showDeviceLinkFlowsAction);
2145 }
Simon Huntd72bc702014-11-13 18:38:04 -08002146 }
2147
2148 function addMultiSelectActions() {
2149 detailPane.append('hr');
2150 // always want to allow 'show traffic'
Thomas Vachuska4731f122014-11-20 04:56:19 -08002151 addAction(detailPane, 'Show Related Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08002152 // if exactly two hosts are selected, also want 'add host intent'
2153 if (nSel() === 2 && allSelectionsClass('host')) {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002154 addAction(detailPane, 'Create Host-to-Host Flow', addHostIntentAction);
2155 } else if (nSel() >= 2 && allSelectionsClass('host')) {
2156 addAction(detailPane, 'Create Multi-Source Flow', addMultiSourceIntentAction);
Simon Huntd72bc702014-11-13 18:38:04 -08002157 }
2158 }
2159
Simon Hunta5e89142014-11-14 07:00:33 -08002160 function addAction(panel, text, cb) {
2161 panel.append('div')
Simon Huntd72bc702014-11-13 18:38:04 -08002162 .classed('actionBtn', true)
2163 .text(text)
2164 .on('click', cb);
2165 }
2166
2167
Paul Greysonfcba0e82014-11-13 10:21:16 -08002168 function zoomPan(scale, translate) {
2169 zoomPanContainer.attr("transform", "translate(" + translate + ")scale(" + scale + ")");
2170 // keep the map lines constant width while zooming
Thomas Vachuska89543292014-11-19 11:28:33 -08002171 bgImg.style("stroke-width", 2.0 / scale + "px");
Paul Greysonfcba0e82014-11-13 10:21:16 -08002172 }
2173
2174 function resetZoomPan() {
2175 zoomPan(1, [0,0]);
2176 zoom.scale(1).translate([0,0]);
2177 }
2178
2179 function setupZoomPan() {
2180 function zoomed() {
Simon Huntdeab4322014-11-13 18:49:07 -08002181 if (!panZoom() ^ !d3.event.sourceEvent.metaKey) {
Paul Greysonfcba0e82014-11-13 10:21:16 -08002182 zoomPan(d3.event.scale, d3.event.translate);
2183 }
2184 }
2185
2186 zoom = d3.behavior.zoom()
2187 .translate([0, 0])
2188 .scale(1)
2189 .scaleExtent([1, 8])
2190 .on("zoom", zoomed);
2191
2192 svg.call(zoom);
2193 }
2194
Simon Hunt61d04042014-11-11 17:27:16 -08002195 // ==============================
2196 // Test harness code
Simon Hunt56d51852014-11-09 13:03:35 -08002197
2198 function prepareScenario(view, ctx, dbg) {
2199 var sc = scenario,
2200 urlSc = sc.evDir + ctx + sc.evScenario;
2201
2202 if (!ctx) {
2203 view.alert("No scenario specified (null ctx)");
2204 return;
2205 }
2206
2207 sc.view = view;
2208 sc.ctx = ctx;
2209 sc.debug = dbg;
2210 sc.evNumber = 0;
2211
2212 d3.json(urlSc, function(err, data) {
Simon Huntbb282f52014-11-10 11:08:19 -08002213 var p = data && data.params || {},
2214 desc = data && data.description || null,
Simon Huntfc274c92014-11-11 11:05:46 -08002215 intro = data && data.title;
Simon Huntbb282f52014-11-10 11:08:19 -08002216
Simon Hunt56d51852014-11-09 13:03:35 -08002217 if (err) {
2218 view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err);
2219 } else {
2220 sc.params = p;
Simon Huntbb282f52014-11-10 11:08:19 -08002221 if (desc) {
2222 intro += '\n\n ' + desc.join('\n ');
2223 }
2224 view.alert(intro);
Simon Hunt56d51852014-11-09 13:03:35 -08002225 }
2226 });
2227
2228 }
2229
Simon Hunt01095ff2014-11-13 16:37:29 -08002230 // ==============================
2231 // Toggle Buttons in masthead
Simon Hunt0c6d4192014-11-12 12:07:10 -08002232
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002233 // TODO: toggle button (and other widgets in the masthead) should be provided
2234 // by the framework; not generated by the view.
2235
Thomas Vachuska47635c62014-11-22 01:21:36 -08002236 //var showInstances;
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002237
2238 function addButtonBar(view) {
2239 var bb = d3.select('#mast')
2240 .append('span').classed('right', true).attr('id', 'bb');
2241
Simon Hunta5e89142014-11-14 07:00:33 -08002242 function mkTogBtn(text, cb) {
2243 return bb.append('span')
2244 .classed('btn', true)
2245 .text(text)
2246 .on('click', cb);
2247 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002248
Thomas Vachuska47635c62014-11-22 01:21:36 -08002249 //showInstances = mkTogBtn('Show Instances', toggleInst);
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002250 }
2251
Thomas Vachuska47635c62014-11-22 01:21:36 -08002252 //function instShown() {
2253 // return showInstances.classed('active');
2254 //}
2255 //function toggleInst() {
2256 // showInstances.classed('active', !instShown());
2257 // if (instShown()) {
2258 // oiBox.show();
2259 // } else {
2260 // oiBox.hide();
2261 // }
2262 //}
Simon Hunt01095ff2014-11-13 16:37:29 -08002263
Simon Huntdeab4322014-11-13 18:49:07 -08002264 function panZoom() {
Simon Hunte5b71752014-11-18 20:06:07 -08002265 return false;
Simon Hunta5e89142014-11-14 07:00:33 -08002266 }
2267
Simon Hunt7fa116d2014-11-17 14:16:55 -08002268 function loadGlyphs(svg) {
2269 var defs = svg.append('defs');
2270 gly.defBird(defs);
Simon Huntc72967b2014-11-20 09:21:42 -08002271 gly.defGlyphs(defs);
Simon Huntb82f6902014-11-22 11:53:15 -08002272 gly.defBadges(defs);
Simon Hunt7fa116d2014-11-17 14:16:55 -08002273 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002274
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002275 // ==============================
Simon Hunt142d0032014-11-04 20:13:09 -08002276 // View life-cycle callbacks
Simon Hunt195cb382014-11-03 17:50:51 -08002277
Simon Huntf67722a2014-11-10 09:32:06 -08002278 function preload(view, ctx, flags) {
Simon Hunt142d0032014-11-04 20:13:09 -08002279 var w = view.width(),
2280 h = view.height(),
Simon Huntc7ee0662014-11-05 16:44:37 -08002281 fcfg = config.force,
2282 fpad = fcfg.pad,
2283 forceDim = [w - 2*fpad, h - 2*fpad];
Simon Hunt195cb382014-11-03 17:50:51 -08002284
Simon Hunt142d0032014-11-04 20:13:09 -08002285 // NOTE: view.$div is a D3 selection of the view's div
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002286 var viewBox = '0 0 ' + config.logicalSize + ' ' + config.logicalSize;
2287 svg = view.$div.append('svg').attr('viewBox', viewBox);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002288 setSize(svg, view);
2289
Simon Hunt7fa116d2014-11-17 14:16:55 -08002290 loadGlyphs(svg);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002291
Paul Greysonfcba0e82014-11-13 10:21:16 -08002292 zoomPanContainer = svg.append('g').attr('id', 'zoomPanContainer');
Paul Greysonfcba0e82014-11-13 10:21:16 -08002293 setupZoomPan();
2294
Simon Hunt1a9eff92014-11-07 11:06:34 -08002295 // add blue glow filter to svg layer
Paul Greysonfcba0e82014-11-13 10:21:16 -08002296 d3u.appendGlow(zoomPanContainer);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002297
Simon Huntc7ee0662014-11-05 16:44:37 -08002298 // group for the topology
Paul Greysonfcba0e82014-11-13 10:21:16 -08002299 topoG = zoomPanContainer.append('g')
Simon Huntd3b7d512014-11-12 15:48:41 -08002300 .attr('id', 'topo-G')
Simon Huntc7ee0662014-11-05 16:44:37 -08002301 .attr('transform', fcfg.translate());
2302
Simon Hunte2575b62014-11-18 15:25:53 -08002303 // subgroups for links, link labels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002304 linkG = topoG.append('g').attr('id', 'links');
Simon Hunte2575b62014-11-18 15:25:53 -08002305 linkLabelG = topoG.append('g').attr('id', 'linkLabels');
Simon Huntc7ee0662014-11-05 16:44:37 -08002306 nodeG = topoG.append('g').attr('id', 'nodes');
2307
Simon Hunte2575b62014-11-18 15:25:53 -08002308 // selection of links, linkLabels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002309 link = linkG.selectAll('.link');
Simon Hunte2575b62014-11-18 15:25:53 -08002310 linkLabel = linkLabelG.selectAll('.linkLabel');
Simon Huntc7ee0662014-11-05 16:44:37 -08002311 node = nodeG.selectAll('.node');
2312
Simon Hunt7cd48f32014-11-09 23:42:50 -08002313 function chrg(d) {
2314 return fcfg.charge[d.class] || -12000;
2315 }
Simon Hunt99c13842014-11-06 18:23:12 -08002316 function ldist(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002317 return fcfg.linkDistance[d.type] || 50;
Simon Hunt99c13842014-11-06 18:23:12 -08002318 }
2319 function lstrg(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002320 // 0.0 - 1.0
2321 return fcfg.linkStrength[d.type] || 1.0;
Simon Hunt99c13842014-11-06 18:23:12 -08002322 }
2323
Simon Hunt1a9eff92014-11-07 11:06:34 -08002324 function selectCb(d, self) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002325 selectObject(d, self);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002326 }
2327
2328 function atDragEnd(d, self) {
Simon Hunt56d51852014-11-09 13:03:35 -08002329 // once we've finished moving, pin the node in position
2330 d.fixed = true;
2331 d3.select(self).classed('fixed', true);
2332 if (config.useLiveData) {
Simon Hunt902c9922014-11-11 11:59:31 -08002333 sendUpdateMeta(d);
Simon Hunta255a2c2014-11-13 22:29:35 -08002334 } else {
2335 console.log('Moving node ' + d.id + ' to [' + d.x + ',' + d.y + ']');
Simon Hunt1a9eff92014-11-07 11:06:34 -08002336 }
2337 }
2338
Simon Hunt902c9922014-11-11 11:59:31 -08002339 function sendUpdateMeta(d) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002340 sendMessage('updateMeta', {
2341 id: d.id,
2342 'class': d.class,
Simon Hunt902c9922014-11-11 11:59:31 -08002343 'memento': {
Simon Hunt01095ff2014-11-13 16:37:29 -08002344 x: d.x,
2345 y: d.y
Simon Hunt902c9922014-11-11 11:59:31 -08002346 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002347 });
2348 }
2349
Simon Huntc7ee0662014-11-05 16:44:37 -08002350 // set up the force layout
2351 network.force = d3.layout.force()
2352 .size(forceDim)
2353 .nodes(network.nodes)
2354 .links(network.links)
Simon Hunt7cd48f32014-11-09 23:42:50 -08002355 .gravity(0.4)
2356 .friction(0.7)
2357 .charge(chrg)
Simon Hunt99c13842014-11-06 18:23:12 -08002358 .linkDistance(ldist)
2359 .linkStrength(lstrg)
Simon Huntc7ee0662014-11-05 16:44:37 -08002360 .on('tick', tick);
Simon Hunt195cb382014-11-03 17:50:51 -08002361
Simon Hunt01095ff2014-11-13 16:37:29 -08002362 network.drag = d3u.createDragBehavior(network.force,
Simon Huntdeab4322014-11-13 18:49:07 -08002363 selectCb, atDragEnd, panZoom);
Simon Hunt0c6d4192014-11-12 12:07:10 -08002364
2365 // create mask layer for when we lose connection to server.
Simon Hunta5e89142014-11-14 07:00:33 -08002366 // TODO: this should be part of the framework
Simon Hunt0c6d4192014-11-12 12:07:10 -08002367 mask = view.$div.append('div').attr('id','topo-mask');
2368 para(mask, 'Oops!');
2369 para(mask, 'Web-socket connection to server closed...');
2370 para(mask, 'Try refreshing the page.');
Simon Hunt12ce12e2014-11-15 21:13:19 -08002371
2372 mask.append('svg')
2373 .attr({
2374 id: 'mask-bird',
2375 width: w,
2376 height: h
2377 })
2378 .append('g')
2379 .attr('transform', birdTranslate(w, h))
2380 .style('opacity', 0.3)
2381 .append('use')
2382 .attr({
2383 'xlink:href': '#bird',
2384 width: config.birdDim,
2385 height: config.birdDim,
2386 fill: '#111'
Thomas Vachuska89543292014-11-19 11:28:33 -08002387 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08002388 }
Simon Hunt195cb382014-11-03 17:50:51 -08002389
Simon Hunt01095ff2014-11-13 16:37:29 -08002390 function para(sel, text) {
2391 sel.append('p').text(text);
2392 }
2393
2394
Simon Hunt56d51852014-11-09 13:03:35 -08002395 function load(view, ctx, flags) {
Simon Huntf67722a2014-11-10 09:32:06 -08002396 // resize, in case the window was resized while we were not loaded
2397 resize(view, ctx, flags);
2398
Simon Hunt99c13842014-11-06 18:23:12 -08002399 // cache the view token, so network topo functions can access it
2400 network.view = view;
Simon Hunt56d51852014-11-09 13:03:35 -08002401 config.useLiveData = !flags.local;
2402
2403 if (!config.useLiveData) {
2404 prepareScenario(view, ctx, flags.debug);
2405 }
Simon Hunt99c13842014-11-06 18:23:12 -08002406
2407 // set our radio buttons and key bindings
Simon Hunt9462e8c2014-11-14 17:28:09 -08002408 layerBtnSet = view.setRadio(layerButtons);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002409 view.setKeys(keyDispatch);
Simon Hunt195cb382014-11-03 17:50:51 -08002410
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002411 // patch in our "button bar" for now
2412 // TODO: implement a more official frameworky way of doing this..
2413 addButtonBar(view);
2414
Simon Huntd3b7d512014-11-12 15:48:41 -08002415 // Load map data asynchronously; complete startup after that..
2416 loadGeoJsonData();
Simon Hunta255a2c2014-11-13 22:29:35 -08002417 }
2418
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002419 function startAntTimer() {
Thomas Vachuska4731f122014-11-20 04:56:19 -08002420 if (!antTimer) {
2421 var pulses = [5, 3, 1.2, 3],
2422 pulse = 0;
2423 antTimer = setInterval(function () {
2424 pulse = pulse + 1;
2425 pulse = pulse === pulses.length ? 0 : pulse;
2426 d3.selectAll('.animated').style('stroke-width', pulses[pulse]);
2427 }, 200);
2428 }
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002429 }
2430
2431 function stopAntTimer() {
Simon Hunta255a2c2014-11-13 22:29:35 -08002432 if (antTimer) {
2433 clearInterval(antTimer);
2434 antTimer = null;
2435 }
Simon Huntd3b7d512014-11-12 15:48:41 -08002436 }
2437
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002438 function unload(view, ctx, flags) {
2439 stopAntTimer();
2440 }
2441
Simon Huntd3b7d512014-11-12 15:48:41 -08002442 // TODO: move these to config/state portion of script
Simon Hunta6a9fe72014-11-20 11:17:12 -08002443 var geoJsonUrl = 'json/map/continental_us.json',
Simon Huntd3b7d512014-11-12 15:48:41 -08002444 geoJson;
2445
2446 function loadGeoJsonData() {
2447 d3.json(geoJsonUrl, function (err, data) {
2448 if (err) {
2449 // fall back to USA map background
2450 loadStaticMap();
2451 } else {
2452 geoJson = data;
2453 loadGeoMap();
2454 }
2455
2456 // finally, connect to the server...
2457 if (config.useLiveData) {
2458 webSock.connect();
2459 }
2460 });
2461 }
2462
2463 function showBg() {
2464 return config.options.showBackground ? 'visible' : 'hidden';
2465 }
2466
2467 function loadStaticMap() {
2468 fnTrace('loadStaticMap', config.backgroundUrl);
2469 var w = network.view.width(),
2470 h = network.view.height();
2471
2472 // load the background image
2473 bgImg = svg.insert('svg:image', '#topo-G')
2474 .attr({
2475 id: 'topo-bg',
2476 width: w,
2477 height: h,
2478 'xlink:href': config.backgroundUrl
2479 })
2480 .style({
2481 visibility: showBg()
2482 });
2483 }
2484
2485 function loadGeoMap() {
2486 fnTrace('loadGeoMap', geoJsonUrl);
Simon Huntd3b7d512014-11-12 15:48:41 -08002487
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002488 // extracts the topojson data into geocoordinate-based geometry
2489 var topoData = topojson.feature(geoJson, geoJson.objects.states);
Simon Huntd3b7d512014-11-12 15:48:41 -08002490
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002491 // see: http://bl.ocks.org/mbostock/4707858
2492 geoMapProjection = d3.geo.mercator();
2493 var path = d3.geo.path().projection(geoMapProjection);
Simon Huntd3b7d512014-11-12 15:48:41 -08002494
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002495 geoMapProjection
2496 .scale(1)
2497 .translate([0, 0]);
Simon Huntd3b7d512014-11-12 15:48:41 -08002498
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002499 // [[x1,y1],[x2,y2]]
2500 var b = path.bounds(topoData);
Paul Greysonfcba0e82014-11-13 10:21:16 -08002501 // size map to 95% of minimum dimension to fill space
2502 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 -08002503 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 -08002504
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002505 geoMapProjection
2506 .scale(s)
2507 .translate(t);
2508
Paul Greysonfcba0e82014-11-13 10:21:16 -08002509 bgImg = zoomPanContainer.insert("g", '#topo-G');
Thomas Vachuska89543292014-11-19 11:28:33 -08002510 bgImg.attr('id', 'map').selectAll('path')
2511 .data(topoData.features)
2512 .enter()
2513 .append('path')
2514 .attr('d', path);
Simon Hunt195cb382014-11-03 17:50:51 -08002515 }
2516
Simon Huntf67722a2014-11-10 09:32:06 -08002517 function resize(view, ctx, flags) {
Simon Hunt12ce12e2014-11-15 21:13:19 -08002518 var w = view.width(),
2519 h = view.height();
2520
Simon Hunt934c3ce2014-11-05 11:45:07 -08002521 setSize(svg, view);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002522
2523 d3.select('#mask-bird').attr({ width: w, height: h})
2524 .select('g').attr('transform', birdTranslate(w, h));
Simon Hunt142d0032014-11-04 20:13:09 -08002525 }
2526
Simon Hunt12ce12e2014-11-15 21:13:19 -08002527 function birdTranslate(w, h) {
2528 var bdim = config.birdDim;
2529 return 'translate('+((w-bdim)*.4)+','+((h-bdim)*.1)+')';
2530 }
Simon Hunt142d0032014-11-04 20:13:09 -08002531
2532 // ==============================
2533 // View registration
Simon Hunt195cb382014-11-03 17:50:51 -08002534
Simon Hunt25248912014-11-04 11:25:48 -08002535 onos.ui.addView('topo', {
Simon Hunt142d0032014-11-04 20:13:09 -08002536 preload: preload,
2537 load: load,
Simon Hunta255a2c2014-11-13 22:29:35 -08002538 unload: unload,
Simon Hunt142d0032014-11-04 20:13:09 -08002539 resize: resize
Simon Hunt195cb382014-11-03 17:50:51 -08002540 });
2541
Thomas Vachuska47635c62014-11-22 01:21:36 -08002542 summaryPane = onos.ui.addFloatingPanel('topo-summary');
Simon Hunt61d04042014-11-11 17:27:16 -08002543 detailPane = onos.ui.addFloatingPanel('topo-detail');
Simon Hunta5e89142014-11-14 07:00:33 -08002544 oiBox = onos.ui.addFloatingPanel('topo-oibox', 'TL');
Simon Huntb82f6902014-11-22 11:53:15 -08002545 oiBox.width(20);
Simon Hunt61d04042014-11-11 17:27:16 -08002546
Simon Hunt195cb382014-11-03 17:50:51 -08002547}(ONOS));