blob: 5b384835f10c85fc89f0bd636c249ece68b8f8ba [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: {
Simon Huntc72967b2014-11-20 09:21:42 -080080 device: {
Simon Hunt395a70c2014-11-22 23:17:40 -080081 dim: 36,
82 rx: 4,
83 xoff: -20,
84 yoff: -18
85 },
86 host: {
87 defaultRadius: 9,
88 radius: {
89 endstation: 14,
90 bgpSpeaker: 14,
91 router: 14
92 }
Simon Huntc72967b2014-11-20 09:21:42 -080093 }
Thomas Vachuska89543292014-11-19 11:28:33 -080094 },
Simon Hunt195cb382014-11-03 17:50:51 -080095 force: {
Simon Hunt7cd48f32014-11-09 23:42:50 -080096 note_for_links: 'link.type is used to differentiate',
Simon Huntc7ee0662014-11-05 16:44:37 -080097 linkDistance: {
Simon Hunt7cd48f32014-11-09 23:42:50 -080098 direct: 100,
99 optical: 120,
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800100 hostLink: 3
Simon Huntc7ee0662014-11-05 16:44:37 -0800101 },
102 linkStrength: {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800103 direct: 1.0,
104 optical: 1.0,
105 hostLink: 1.0
Simon Huntc7ee0662014-11-05 16:44:37 -0800106 },
Simon Hunt7cd48f32014-11-09 23:42:50 -0800107 note_for_nodes: 'node.class is used to differentiate',
Simon Huntc7ee0662014-11-05 16:44:37 -0800108 charge: {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800109 device: -8000,
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800110 host: -5000
Simon Huntc7ee0662014-11-05 16:44:37 -0800111 },
112 pad: 20,
Simon Hunt195cb382014-11-03 17:50:51 -0800113 translate: function() {
114 return 'translate(' +
Simon Huntc7ee0662014-11-05 16:44:37 -0800115 config.force.pad + ',' +
116 config.force.pad + ')';
Simon Hunt195cb382014-11-03 17:50:51 -0800117 }
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800118 },
119 // see below in creation of viewBox on main svg
120 logicalSize: 1000
Simon Hunt195cb382014-11-03 17:50:51 -0800121 };
122
Simon Hunt142d0032014-11-04 20:13:09 -0800123 // radio buttons
Simon Hunt9462e8c2014-11-14 17:28:09 -0800124 var layerButtons = [
125 { text: 'All Layers', id: 'all', cb: showAllLayers },
126 { text: 'Packet Only', id: 'pkt', cb: showPacketLayer },
127 { text: 'Optical Only', id: 'opt', cb: showOpticalLayer }
128 ],
129 layerBtnSet,
130 layerBtnDispatch = {
131 all: showAllLayers,
132 pkt: showPacketLayer,
133 opt: showOpticalLayer
134 };
Simon Hunt934c3ce2014-11-05 11:45:07 -0800135
136 // key bindings
137 var keyDispatch = {
Simon Hunt988c6fc2014-11-20 17:43:03 -0800138 // TODO: remove these "development only" bindings
Simon Hunt8f40cce2014-11-23 15:57:30 -0800139 0: testMe,
140 equals: injectStartupEvents,
141 dash: injectTestEvent,
Simon Hunt99c13842014-11-06 18:23:12 -0800142
Thomas Vachuska47635c62014-11-22 01:21:36 -0800143 O: [toggleSummary, 'Toggle ONOS summary pane'],
144 I: [toggleInstances, 'Toggle ONOS instances pane'],
Simon Hunt988c6fc2014-11-20 17:43:03 -0800145 B: [toggleBg, 'Toggle background image'],
Simon Hunt434cf142014-11-24 11:10:28 -0800146 H: [toggleHosts, 'Toggle host visibility'],
Simon Hunt988c6fc2014-11-20 17:43:03 -0800147 L: [cycleLabels, 'Cycle Device labels'],
Simon Hunt934c3ce2014-11-05 11:45:07 -0800148 P: togglePorts,
Simon Hunt87514342014-11-24 16:41:27 -0800149 U: [unpin, 'Unpin node (hover mouse over)'],
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 Hunt87514342014-11-24 16:41:27 -0800157 // mouse gestures
158 var gestures = [
159 ['click', 'Select the item and show details'],
160 ['shift-click', 'Toggle selection state'],
161 ['drag', 'Reposition (and pin) device / host'],
162 ['cmd-scroll', 'Zoom in / out'],
163 ['cmd-drag', 'Pan']
164 ];
165
Simon Hunt195cb382014-11-03 17:50:51 -0800166 // state variables
Simon Hunt99c13842014-11-06 18:23:12 -0800167 var network = {
Simon Hunt50128c02014-11-08 13:36:15 -0800168 view: null, // view token reference
Simon Hunt99c13842014-11-06 18:23:12 -0800169 nodes: [],
170 links: [],
Simon Hunt269670f2014-11-17 16:17:43 -0800171 lookup: {},
172 revLinkToKey: {}
Simon Hunt99c13842014-11-06 18:23:12 -0800173 },
Simon Hunt56d51852014-11-09 13:03:35 -0800174 scenario = {
175 evDir: 'json/ev/',
176 evScenario: '/scenario.json',
177 evPrefix: '/ev_',
178 evOnos: '_onos.json',
179 evUi: '_ui.json',
180 ctx: null,
181 params: {},
182 evNumber: 0,
Simon Hunt434cf142014-11-24 11:10:28 -0800183 view: null
Simon Hunt56d51852014-11-09 13:03:35 -0800184 },
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800185 webSock,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800186 sid = 0,
Thomas Vachuska60d72bf2014-11-21 13:02:00 -0800187 deviceLabelCount = 3,
Simon Hunt209155e2014-11-21 12:16:09 -0800188 hostLabelCount = 2,
Simon Hunt56d51852014-11-09 13:03:35 -0800189 deviceLabelIndex = 0,
190 hostLabelIndex = 0,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800191 selections = {},
Simon Hunta5e89142014-11-14 07:00:33 -0800192 selectOrder = [],
Simon Hunt6ac93f32014-11-13 12:17:27 -0800193 hovered = null,
Thomas Vachuska47635c62014-11-22 01:21:36 -0800194 summaryPane,
Simon Hunta5e89142014-11-14 07:00:33 -0800195 detailPane,
Simon Hunta255a2c2014-11-13 22:29:35 -0800196 antTimer = null,
Simon Hunta5e89142014-11-14 07:00:33 -0800197 onosInstances = {},
198 onosOrder = [],
199 oiBox,
Simon Hunt9462e8c2014-11-14 17:28:09 -0800200 oiShowMaster = false,
Simon Hunt8f40cce2014-11-23 15:57:30 -0800201 portLabelsOn = false,
Simon Huntb0ecfa52014-11-23 21:05:12 -0800202 cat7 = d3u.cat7(),
Simon Hunt434cf142014-11-24 11:10:28 -0800203 colorAffinity = false,
204 showHosts = false;
Simon Hunt195cb382014-11-03 17:50:51 -0800205
Simon Hunt434cf142014-11-24 11:10:28 -0800206 // constants
Thomas Vachuska9edca302014-11-22 17:06:42 -0800207 var hoverModeAll = 1,
208 hoverModeFlows = 2,
209 hoverModeIntents = 3,
210 hoverMode = hoverModeFlows;
211
Simon Hunt934c3ce2014-11-05 11:45:07 -0800212 // D3 selections
213 var svg,
Paul Greysonfcba0e82014-11-13 10:21:16 -0800214 zoomPanContainer,
Simon Hunt934c3ce2014-11-05 11:45:07 -0800215 bgImg,
Simon Huntc7ee0662014-11-05 16:44:37 -0800216 topoG,
217 nodeG,
218 linkG,
Simon Hunte2575b62014-11-18 15:25:53 -0800219 linkLabelG,
Simon Huntc7ee0662014-11-05 16:44:37 -0800220 node,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800221 link,
Simon Hunte2575b62014-11-18 15:25:53 -0800222 linkLabel,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800223 mask;
Simon Hunt195cb382014-11-03 17:50:51 -0800224
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800225 // the projection for the map background
226 var geoMapProjection;
227
Paul Greysonfcba0e82014-11-13 10:21:16 -0800228 // the zoom function
229 var zoom;
230
Simon Hunt142d0032014-11-04 20:13:09 -0800231 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800232 // For Debugging / Development
Simon Hunt195cb382014-11-03 17:50:51 -0800233
Simon Hunt99c13842014-11-06 18:23:12 -0800234 function note(label, msg) {
235 console.log('NOTE: ' + label + ': ' + msg);
Simon Hunt195cb382014-11-03 17:50:51 -0800236 }
237
Simon Hunt99c13842014-11-06 18:23:12 -0800238 function debug(what) {
239 return config.debugOn && config.debug[what];
Simon Hunt934c3ce2014-11-05 11:45:07 -0800240 }
241
Simon Huntfc274c92014-11-11 11:05:46 -0800242 function fnTrace(msg, id) {
243 if (config.fnTrace) {
244 console.log('FN: ' + msg + ' [' + id + ']');
245 }
246 }
Simon Hunt99c13842014-11-06 18:23:12 -0800247
Simon Hunta5e89142014-11-14 07:00:33 -0800248 function evTrace(data) {
249 fnTrace(data.event, data.payload.id);
250 }
251
Simon Hunt934c3ce2014-11-05 11:45:07 -0800252 // ==============================
253 // Key Callbacks
254
Simon Hunt99c13842014-11-06 18:23:12 -0800255 function testMe(view) {
Simon Hunt8f40cce2014-11-23 15:57:30 -0800256 //view.alert('Theme is ' + view.getTheme());
Simon Hunta3dd9572014-11-20 15:22:41 -0800257 //view.flash('This is some text');
Simon Hunt8f40cce2014-11-23 15:57:30 -0800258 cat7.testCard(svg);
Simon Hunt99c13842014-11-06 18:23:12 -0800259 }
260
Simon Hunt56d51852014-11-09 13:03:35 -0800261 function injectTestEvent(view) {
Simon Hunt434cf142014-11-24 11:10:28 -0800262 if (config.useLiveData) { return; }
263
Simon Hunt56d51852014-11-09 13:03:35 -0800264 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 Hunt1712ed82014-11-17 12:56:00 -0800294 wsTrace('test', JSON.stringify(data));
Simon Hunt7cd48f32014-11-09 23:42:50 -0800295 frame.cb(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800296 }
297 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800298
Simon Hunt56d51852014-11-09 13:03:35 -0800299 }
Simon Hunt50128c02014-11-08 13:36:15 -0800300
Simon Hunt56d51852014-11-09 13:03:35 -0800301 function handleUiEvent(data) {
Simon Huntbb282f52014-11-10 11:08:19 -0800302 scenario.view.alert('UI Tx: ' + data.event + '\n\n' +
303 JSON.stringify(data));
Simon Hunt56d51852014-11-09 13:03:35 -0800304 }
305
306 function injectStartupEvents(view) {
307 var last = scenario.params.lastAuto || 0;
Simon Hunt434cf142014-11-24 11:10:28 -0800308 if (config.useLiveData) { return; }
Simon Hunt56d51852014-11-09 13:03:35 -0800309
310 while (scenario.evNumber < last) {
Simon Hunt1a9eff92014-11-07 11:06:34 -0800311 injectTestEvent(view);
312 }
313 }
314
Simon Hunt934c3ce2014-11-05 11:45:07 -0800315 function toggleBg() {
316 var vis = bgImg.style('visibility');
Simon Hunt434cf142014-11-24 11:10:28 -0800317 bgImg.style('visibility', visVal(vis === 'hidden'));
318 }
319
320 function toggleHosts() {
321 showHosts = !showHosts;
322 updateHostVisibility();
323 network.view.flash('Hosts ' + visVal(showHosts));
Simon Hunt934c3ce2014-11-05 11:45:07 -0800324 }
325
Simon Hunt99c13842014-11-06 18:23:12 -0800326 function cycleLabels() {
Thomas Vachuska60d72bf2014-11-21 13:02:00 -0800327 deviceLabelIndex = (deviceLabelIndex === 2)
Simon Huntbb282f52014-11-10 11:08:19 -0800328 ? 0 : deviceLabelIndex + 1;
Simon Hunt5f36d342014-11-08 21:33:14 -0800329
Simon Hunt99c13842014-11-06 18:23:12 -0800330 network.nodes.forEach(function (d) {
Simon Huntbb282f52014-11-10 11:08:19 -0800331 if (d.class === 'device') {
332 updateDeviceLabel(d);
333 }
Simon Hunt99c13842014-11-06 18:23:12 -0800334 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800335 }
336
337 function togglePorts(view) {
Simon Hunt434cf142014-11-24 11:10:28 -0800338 //view.alert('togglePorts() callback')
Simon Hunt934c3ce2014-11-05 11:45:07 -0800339 }
340
Simon Hunt6ac93f32014-11-13 12:17:27 -0800341 function unpin() {
342 if (hovered) {
Simon Hunt395a70c2014-11-22 23:17:40 -0800343 sendUpdateMeta(hovered);
Simon Hunt6ac93f32014-11-13 12:17:27 -0800344 hovered.fixed = false;
345 hovered.el.classed('fixed', false);
346 network.force.resume();
347 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800348 }
349
Simon Hunt9462e8c2014-11-14 17:28:09 -0800350 function handleEscape(view) {
351 if (oiShowMaster) {
352 cancelAffinity();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800353 } else if (detailPane.isVisible()) {
Simon Hunt9462e8c2014-11-14 17:28:09 -0800354 deselectAll();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800355 } else if (oiBox.isVisible()) {
Simon Huntb0ecfa52014-11-23 21:05:12 -0800356 hideInstances();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800357 } else if (summaryPane.isVisible()) {
358 cancelSummary();
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800359 stopAntTimer();
360 } else {
361 hoverMode = hoverModeFlows;
Simon Hunt9462e8c2014-11-14 17:28:09 -0800362 }
363 }
364
Simon Hunt934c3ce2014-11-05 11:45:07 -0800365 // ==============================
366 // Radio Button Callbacks
367
Simon Hunta5e89142014-11-14 07:00:33 -0800368 var layerLookup = {
369 host: {
Simon Hunt209155e2014-11-21 12:16:09 -0800370 endstation: 'pkt', // default, if host event does not define type
Thomas Vachuska89543292014-11-19 11:28:33 -0800371 router: 'pkt',
Simon Hunta5e89142014-11-14 07:00:33 -0800372 bgpSpeaker: 'pkt'
373 },
374 device: {
375 switch: 'pkt',
376 roadm: 'opt'
377 },
378 link: {
379 hostLink: 'pkt',
380 direct: 'pkt',
Simon Hunt8257f4c2014-11-16 19:34:54 -0800381 indirect: '',
382 tunnel: '',
Simon Hunta5e89142014-11-14 07:00:33 -0800383 optical: 'opt'
384 }
385 };
386
387 function inLayer(d, layer) {
Simon Hunt8257f4c2014-11-16 19:34:54 -0800388 var type = d.class === 'link' ? d.type() : d.type,
389 look = layerLookup[d.class],
390 lyr = look && look[type];
Simon Hunta5e89142014-11-14 07:00:33 -0800391 return lyr === layer;
392 }
393
394 function unsuppressLayer(which) {
395 node.each(function (d) {
396 var node = d.el;
397 if (inLayer(d, which)) {
398 node.classed('suppressed', false);
399 }
400 });
401
402 link.each(function (d) {
403 var link = d.el;
404 if (inLayer(d, which)) {
405 link.classed('suppressed', false);
406 }
407 });
408 }
409
Simon Hunt9462e8c2014-11-14 17:28:09 -0800410 function suppressLayers(b) {
411 node.classed('suppressed', b);
412 link.classed('suppressed', b);
Simon Hunt142d0032014-11-04 20:13:09 -0800413// d3.selectAll('svg .port').classed('inactive', false);
414// d3.selectAll('svg .portText').classed('inactive', false);
Simon Hunt195cb382014-11-03 17:50:51 -0800415 }
416
Simon Hunt9462e8c2014-11-14 17:28:09 -0800417 function showAllLayers() {
418 suppressLayers(false);
419 }
420
Simon Hunt195cb382014-11-03 17:50:51 -0800421 function showPacketLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800422 node.classed('suppressed', true);
423 link.classed('suppressed', true);
424 unsuppressLayer('pkt');
Simon Hunt195cb382014-11-03 17:50:51 -0800425 }
426
427 function showOpticalLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800428 node.classed('suppressed', true);
429 link.classed('suppressed', true);
430 unsuppressLayer('opt');
Simon Hunt195cb382014-11-03 17:50:51 -0800431 }
432
Simon Hunt9462e8c2014-11-14 17:28:09 -0800433 function restoreLayerState() {
434 layerBtnDispatch[layerBtnSet.selected()]();
435 }
436
Simon Hunt142d0032014-11-04 20:13:09 -0800437 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800438 // Private functions
439
Simon Hunt99c13842014-11-06 18:23:12 -0800440 function safeId(s) {
441 return s.replace(/[^a-z0-9]/gi, '-');
442 }
443
Simon Huntc7ee0662014-11-05 16:44:37 -0800444 // set the size of the given element to that of the view (reduced if padded)
445 function setSize(el, view, pad) {
446 var padding = pad ? pad * 2 : 0;
Simon Hunt934c3ce2014-11-05 11:45:07 -0800447 el.attr({
Simon Huntc7ee0662014-11-05 16:44:37 -0800448 width: view.width() - padding,
449 height: view.height() - padding
Simon Hunt934c3ce2014-11-05 11:45:07 -0800450 });
451 }
452
Simon Hunt8257f4c2014-11-16 19:34:54 -0800453 function makeNodeKey(d, what) {
454 var port = what + 'Port';
455 return d[what] + '/' + d[port];
456 }
457
458 function makeLinkKey(d, flipped) {
459 var one = flipped ? makeNodeKey(d, 'dst') : makeNodeKey(d, 'src'),
460 two = flipped ? makeNodeKey(d, 'src') : makeNodeKey(d, 'dst');
461 return one + '-' + two;
462 }
463
Simon Hunt269670f2014-11-17 16:17:43 -0800464 function findLinkById(id) {
465 // check to see if this is a reverse lookup, else default to given id
466 var key = network.revLinkToKey[id] || id;
467 return key && network.lookup[key];
468 }
469
Simon Hunt8257f4c2014-11-16 19:34:54 -0800470 function findLink(linkData, op) {
471 var key = makeLinkKey(linkData),
472 keyrev = makeLinkKey(linkData, 1),
473 link = network.lookup[key],
474 linkRev = network.lookup[keyrev],
475 result = {},
476 ldata = link || linkRev,
477 rawLink;
478
479 if (op === 'add') {
480 if (link) {
481 // trying to add a link that we already know about
482 result.ldata = link;
483 result.badLogic = 'addLink: link already added';
484
485 } else if (linkRev) {
486 // we found the reverse of the link to be added
487 result.ldata = linkRev;
488 if (linkRev.fromTarget) {
489 result.badLogic = 'addLink: link already added';
490 }
491 }
492 } else if (op === 'update') {
493 if (!ldata) {
494 result.badLogic = 'updateLink: link not found';
495 } else {
496 rawLink = link ? ldata.fromSource : ldata.fromTarget;
497 result.updateWith = function (data) {
498 $.extend(rawLink, data);
499 restyleLinkElement(ldata);
500 }
501 }
502 } else if (op === 'remove') {
503 if (!ldata) {
504 result.badLogic = 'removeLink: link not found';
505 } else {
506 rawLink = link ? ldata.fromSource : ldata.fromTarget;
507
508 if (!rawLink) {
509 result.badLogic = 'removeLink: link not found';
510
511 } else {
512 result.removeRawLink = function () {
513 if (link) {
514 // remove fromSource
515 ldata.fromSource = null;
516 if (ldata.fromTarget) {
517 // promote target into source position
518 ldata.fromSource = ldata.fromTarget;
519 ldata.fromTarget = null;
520 ldata.key = keyrev;
521 delete network.lookup[key];
522 network.lookup[keyrev] = ldata;
Simon Hunt269670f2014-11-17 16:17:43 -0800523 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800524 }
525 } else {
526 // remove fromTarget
527 ldata.fromTarget = null;
Simon Hunt269670f2014-11-17 16:17:43 -0800528 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800529 }
530 if (ldata.fromSource) {
531 restyleLinkElement(ldata);
532 } else {
533 removeLinkElement(ldata);
534 }
535 }
536 }
537 }
538 }
539 return result;
540 }
541
542 function addLinkUpdate(ldata, link) {
543 // add link event, but we already have the reverse link installed
544 ldata.fromTarget = link;
Simon Hunt269670f2014-11-17 16:17:43 -0800545 network.revLinkToKey[link.id] = ldata.key;
Simon Hunt8257f4c2014-11-16 19:34:54 -0800546 restyleLinkElement(ldata);
547 }
548
549 var allLinkTypes = 'direct indirect optical tunnel',
550 defaultLinkType = 'direct';
551
552 function restyleLinkElement(ldata) {
553 // this fn's job is to look at raw links and decide what svg classes
554 // need to be applied to the line element in the DOM
555 var el = ldata.el,
556 type = ldata.type(),
557 lw = ldata.linkWidth(),
558 online = ldata.online();
559
560 el.classed('link', true);
561 el.classed('inactive', !online);
562 el.classed(allLinkTypes, false);
563 if (type) {
564 el.classed(type, true);
565 }
566 el.transition()
567 .duration(1000)
Thomas Vachuska89543292014-11-19 11:28:33 -0800568 .attr('stroke-width', linkScale(lw))
569 .attr('stroke', config.topo.linkBaseColor);
Simon Hunt8257f4c2014-11-16 19:34:54 -0800570 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800571
Simon Hunt99c13842014-11-06 18:23:12 -0800572 // ==============================
573 // Event handlers for server-pushed events
574
Simon Huntbb282f52014-11-10 11:08:19 -0800575 function logicError(msg) {
576 // TODO, report logic error to server, via websock, so it can be logged
Simon Huntfc274c92014-11-11 11:05:46 -0800577 console.warn(msg);
Simon Huntbb282f52014-11-10 11:08:19 -0800578 }
579
Simon Hunt99c13842014-11-06 18:23:12 -0800580 var eventDispatch = {
Simon Hunta5e89142014-11-14 07:00:33 -0800581 addInstance: addInstance,
Simon Hunt99c13842014-11-06 18:23:12 -0800582 addDevice: addDevice,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800583 addLink: addLink,
Simon Hunt56d51852014-11-09 13:03:35 -0800584 addHost: addHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800585
Simon Huntfcfb46c2014-11-19 12:53:38 -0800586 updateInstance: updateInstance,
Simon Huntbb282f52014-11-10 11:08:19 -0800587 updateDevice: updateDevice,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800588 updateLink: updateLink,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800589 updateHost: updateHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800590
Simon Hunt7b403bc2014-11-22 19:01:00 -0800591 removeInstance: removeInstance,
Simon Huntbb282f52014-11-10 11:08:19 -0800592 removeDevice: stillToImplement,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800593 removeLink: removeLink,
Simon Hunt44031102014-11-11 13:20:36 -0800594 removeHost: removeHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800595
Simon Hunt61d04042014-11-11 17:27:16 -0800596 showDetails: showDetails,
Thomas Vachuska47635c62014-11-22 01:21:36 -0800597 showSummary: showSummary,
Simon Huntb53e0682014-11-12 13:32:01 -0800598 showTraffic: showTraffic
Simon Hunt99c13842014-11-06 18:23:12 -0800599 };
600
Simon Hunta5e89142014-11-14 07:00:33 -0800601 function addInstance(data) {
602 evTrace(data);
603 var inst = data.payload,
604 id = inst.id;
605 if (onosInstances[id]) {
606 logicError('ONOS instance already added: ' + id);
607 return;
608 }
609 onosInstances[id] = inst;
610 onosOrder.push(inst);
611 updateInstances();
612 }
613
Simon Hunt99c13842014-11-06 18:23:12 -0800614 function addDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800615 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800616 var device = data.payload,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800617 nodeData = createDeviceNode(device);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800618 network.nodes.push(nodeData);
619 network.lookup[nodeData.id] = nodeData;
Simon Hunt99c13842014-11-06 18:23:12 -0800620 updateNodes();
621 network.force.start();
622 }
623
Simon Hunt99c13842014-11-06 18:23:12 -0800624 function addLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800625 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800626 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800627 result = findLink(link, 'add'),
628 bad = result.badLogic,
629 ldata = result.ldata;
630
631 if (bad) {
632 logicError(bad + ': ' + link.id);
633 return;
634 }
635
636 if (ldata) {
637 // we already have a backing store link for src/dst nodes
638 addLinkUpdate(ldata, link);
639 return;
640 }
641
642 // no backing store link yet
643 ldata = createLink(link);
644 if (ldata) {
645 network.links.push(ldata);
646 network.lookup[ldata.key] = ldata;
Simon Hunt99c13842014-11-06 18:23:12 -0800647 updateLinks();
648 network.force.start();
649 }
650 }
651
Simon Hunt56d51852014-11-09 13:03:35 -0800652 function addHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800653 evTrace(data);
Simon Hunt56d51852014-11-09 13:03:35 -0800654 var host = data.payload,
655 node = createHostNode(host),
656 lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800657 network.nodes.push(node);
658 network.lookup[host.id] = node;
659 updateNodes();
660
661 lnk = createHostLink(host);
662 if (lnk) {
Simon Hunt44031102014-11-11 13:20:36 -0800663 node.linkData = lnk; // cache ref on its host
Simon Hunt56d51852014-11-09 13:03:35 -0800664 network.links.push(lnk);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800665 network.lookup[host.ingress] = lnk;
666 network.lookup[host.egress] = lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800667 updateLinks();
668 }
669 network.force.start();
670 }
671
Simon Hunt44031102014-11-11 13:20:36 -0800672 // TODO: fold updateX(...) methods into one base method; remove duplication
Simon Hunt56a2ea42014-11-19 12:39:31 -0800673
674 function updateInstance(data) {
675 evTrace(data);
676 var inst = data.payload,
677 id = inst.id,
678 instData = onosInstances[id];
679 if (instData) {
680 $.extend(instData, inst);
681 updateInstances();
Simon Hunt56a2ea42014-11-19 12:39:31 -0800682 } else {
683 logicError('updateInstance lookup fail. ID = "' + id + '"');
684 }
685 }
686
Simon Huntbb282f52014-11-10 11:08:19 -0800687 function updateDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800688 evTrace(data);
Simon Huntbb282f52014-11-10 11:08:19 -0800689 var device = data.payload,
690 id = device.id,
Simon Hunt62c47542014-11-22 22:16:32 -0800691 d = network.lookup[id];
692 if (d) {
693 $.extend(d, device);
694 if (positionNode(d, true)) {
Simon Hunt395a70c2014-11-22 23:17:40 -0800695 sendUpdateMeta(d, true);
Simon Hunt62c47542014-11-22 22:16:32 -0800696 }
697 updateNodes();
Simon Huntbb282f52014-11-10 11:08:19 -0800698 } else {
699 logicError('updateDevice lookup fail. ID = "' + id + '"');
700 }
701 }
702
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800703 function updateLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800704 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800705 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800706 result = findLink(link, 'update'),
707 bad = result.badLogic;
708 if (bad) {
709 logicError(bad + ': ' + link.id);
710 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800711 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800712 result.updateWith(link);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800713 }
714
Simon Hunt7cd48f32014-11-09 23:42:50 -0800715 function updateHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800716 evTrace(data);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800717 var host = data.payload,
Simon Huntbb282f52014-11-10 11:08:19 -0800718 id = host.id,
719 hostData = network.lookup[id];
720 if (hostData) {
721 $.extend(hostData, host);
722 updateHostState(hostData);
723 } else {
724 logicError('updateHost lookup fail. ID = "' + id + '"');
725 }
Simon Hunt7cd48f32014-11-09 23:42:50 -0800726 }
727
Simon Hunt44031102014-11-11 13:20:36 -0800728 // TODO: fold removeX(...) methods into base method - remove dup code
Simon Hunt7b403bc2014-11-22 19:01:00 -0800729 function removeInstance(data) {
730 evTrace(data);
731 var inst = data.payload,
732 id = inst.id,
733 instData = onosInstances[id];
734 if (instData) {
735 var idx = find(id, onosOrder, 'id');
736 if (idx >= 0) {
737 onosOrder.splice(idx, 1);
738 }
739 delete onosInstances[id];
740 updateInstances();
741 } else {
742 logicError('updateInstance lookup fail. ID = "' + id + '"');
743 }
744 }
745
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800746 function removeLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800747 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800748 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800749 result = findLink(link, 'remove'),
750 bad = result.badLogic;
751 if (bad) {
752 logicError(bad + ': ' + link.id);
753 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800754 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800755 result.removeRawLink();
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800756 }
757
Simon Hunt44031102014-11-11 13:20:36 -0800758 function removeHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800759 evTrace(data);
Simon Hunt44031102014-11-11 13:20:36 -0800760 var host = data.payload,
761 id = host.id,
762 hostData = network.lookup[id];
763 if (hostData) {
764 removeHostElement(hostData);
765 } else {
766 logicError('removeHost lookup fail. ID = "' + id + '"');
767 }
768 }
769
Thomas Vachuska47635c62014-11-22 01:21:36 -0800770 function showSummary(data) {
771 evTrace(data);
772 populateSummary(data.payload);
Simon Hunt06811b72014-11-25 18:54:48 -0800773 showSummaryPane();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800774 }
775
Simon Hunt61d04042014-11-11 17:27:16 -0800776 function showDetails(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800777 evTrace(data);
Simon Hunt61d04042014-11-11 17:27:16 -0800778 populateDetails(data.payload);
Simon Hunt06811b72014-11-25 18:54:48 -0800779 showDetailPane();
Simon Hunt61d04042014-11-11 17:27:16 -0800780 }
781
Simon Huntb53e0682014-11-12 13:32:01 -0800782 function showTraffic(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800783 evTrace(data);
Thomas Vachuska4731f122014-11-20 04:56:19 -0800784 var paths = data.payload.paths,
785 hasTraffic = false;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800786
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800787 // Revert any links hilighted previously.
Thomas Vachuska4731f122014-11-20 04:56:19 -0800788 link.style('stroke-width', null)
Thomas Vachuskaa3148a72014-11-19 21:38:35 -0800789 .classed('primary secondary animated optical', false);
Simon Hunte2575b62014-11-18 15:25:53 -0800790 // Remove all previous labels.
791 removeLinkLabels();
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800792
Simon Hunte2575b62014-11-18 15:25:53 -0800793 // Now hilight all links in the paths payload, and attach
794 // labels to them, if they are defined.
Simon Hunta255a2c2014-11-13 22:29:35 -0800795 paths.forEach(function (p) {
Simon Hunte2575b62014-11-18 15:25:53 -0800796 var n = p.links.length,
797 i,
798 ldata;
799
Thomas Vachuska4731f122014-11-20 04:56:19 -0800800 hasTraffic = hasTraffic || p.traffic;
Simon Hunte2575b62014-11-18 15:25:53 -0800801 for (i=0; i<n; i++) {
802 ldata = findLinkById(p.links[i]);
Thomas Vachuska4731f122014-11-20 04:56:19 -0800803 if (ldata && ldata.el) {
Simon Hunte2575b62014-11-18 15:25:53 -0800804 ldata.el.classed(p.class, true);
805 ldata.label = p.labels[i];
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800806 }
Simon Hunte2575b62014-11-18 15:25:53 -0800807 }
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800808 });
Thomas Vachuska4731f122014-11-20 04:56:19 -0800809
Simon Hunte2575b62014-11-18 15:25:53 -0800810 updateLinks();
Thomas Vachuska4731f122014-11-20 04:56:19 -0800811
812 if (hasTraffic && !antTimer) {
813 startAntTimer();
814 } else if (!hasTraffic && antTimer) {
815 stopAntTimer();
816 }
Simon Huntb53e0682014-11-12 13:32:01 -0800817 }
818
Simon Hunt56d51852014-11-09 13:03:35 -0800819 // ...............................
820
821 function stillToImplement(data) {
822 var p = data.payload;
823 note(data.event, p.id);
Simon Hunt7b403bc2014-11-22 19:01:00 -0800824 if (!config.useLiveData) {
825 network.view.alert('Not yet implemented: "' + data.event + '"');
826 }
Simon Hunt56d51852014-11-09 13:03:35 -0800827 }
Simon Hunt99c13842014-11-06 18:23:12 -0800828
829 function unknownEvent(data) {
Simon Hunt434cf142014-11-24 11:10:28 -0800830 console.warn('Unknown event type: "' + data.event + '"', data);
Simon Hunt99c13842014-11-06 18:23:12 -0800831 }
832
833 function handleServerEvent(data) {
834 var fn = eventDispatch[data.event] || unknownEvent;
835 fn(data);
836 }
837
838 // ==============================
Simon Hunt61d04042014-11-11 17:27:16 -0800839 // Out-going messages...
840
Simon Huntb53e0682014-11-12 13:32:01 -0800841 function nSel() {
842 return selectOrder.length;
843 }
Simon Hunt61d04042014-11-11 17:27:16 -0800844 function getSel(idx) {
845 return selections[selectOrder[idx]];
846 }
Simon Huntb53e0682014-11-12 13:32:01 -0800847 function getSelId(idx) {
848 return getSel(idx).obj.id;
849 }
Thomas Vachuska9edca302014-11-22 17:06:42 -0800850 function getSelIds(start, endOffset) {
851 var end = selectOrder.length - endOffset;
852 var ids = [];
853 selectOrder.slice(start, end).forEach(function (d) {
854 ids.push(getSelId(d));
855 });
856 return ids;
857 }
Simon Huntb53e0682014-11-12 13:32:01 -0800858 function allSelectionsClass(cls) {
859 for (var i=0, n=nSel(); i<n; i++) {
860 if (getSel(i).obj.class !== cls) {
861 return false;
862 }
863 }
864 return true;
865 }
Simon Hunt61d04042014-11-11 17:27:16 -0800866
Thomas Vachuska47635c62014-11-22 01:21:36 -0800867 function toggleInstances() {
868 if (!oiBox.isVisible()) {
Simon Huntb0ecfa52014-11-23 21:05:12 -0800869 showInstances();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800870 } else {
Simon Huntb0ecfa52014-11-23 21:05:12 -0800871 hideInstances();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800872 }
873 }
874
Simon Huntb0ecfa52014-11-23 21:05:12 -0800875 function showInstances() {
876 oiBox.show();
877 colorAffinity = true;
878 updateDeviceColors();
879 }
880
881 function hideInstances() {
882 oiBox.hide();
883 colorAffinity = false;
884 cancelAffinity();
885 updateDeviceColors();
886 }
887
888
Thomas Vachuska47635c62014-11-22 01:21:36 -0800889 function toggleSummary() {
890 if (!summaryPane.isVisible()) {
891 requestSummary();
892 } else {
893 cancelSummary();
894 }
895 }
896
897 // request overall summary data
898 function requestSummary() {
899 sendMessage('requestSummary', {});
900 }
901
902 function cancelSummary() {
903 sendMessage('cancelSummary', {});
Simon Hunt06811b72014-11-25 18:54:48 -0800904 hideSummaryPane();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800905 }
906
Simon Hunt06811b72014-11-25 18:54:48 -0800907 // encapsulate interaction between summary and details panes
908 function showSummaryPane() {
909 if (detailPane.isVisible()) {
910 detailPane.down(summaryPane.show);
911 } else {
912 summaryPane.show();
913 }
914 }
915
916 function hideSummaryPane() {
917 summaryPane.hide(function () {
918 if (detailPane.isVisible()) {
919 detailPane.up();
920 }
921 });
922 }
923
924 function showDetailPane() {
925 if (summaryPane.isVisible()) {
926 detailPane.down(detailPane.show);
927 } else {
928 detailPane.up(detailPane.show);
929 }
930 }
931
932 function hideDetailPane() {
933 detailPane.hide();
934 }
935
936
Simon Hunt61d04042014-11-11 17:27:16 -0800937 // request details for the selected element
Simon Huntd72bc702014-11-13 18:38:04 -0800938 // invoked from selection of a single node.
Simon Hunt61d04042014-11-11 17:27:16 -0800939 function requestDetails() {
940 var data = getSel(0).obj,
941 payload = {
942 id: data.id,
943 class: data.class
944 };
945 sendMessage('requestDetails', payload);
946 }
947
Thomas Vachuska9edca302014-11-22 17:06:42 -0800948 function addHostIntentAction() {
Simon Huntd72bc702014-11-13 18:38:04 -0800949 sendMessage('addHostIntent', {
Thomas Vachuska9edca302014-11-22 17:06:42 -0800950 one: selectOrder[0],
951 two: selectOrder[1],
952 ids: selectOrder
Simon Huntd72bc702014-11-13 18:38:04 -0800953 });
Thomas Vachuska9edca302014-11-22 17:06:42 -0800954 network.view.flash('Host-to-Host flow added');
Simon Huntd72bc702014-11-13 18:38:04 -0800955 }
956
Thomas Vachuska9edca302014-11-22 17:06:42 -0800957 function addMultiSourceIntentAction() {
958 sendMessage('addMultiSourceIntent', {
959 src: selectOrder.slice(0, selectOrder.length - 1),
960 dst: selectOrder[selectOrder.length - 1],
961 ids: selectOrder
962 });
963 network.view.flash('Multi-Source flow added');
Thomas Vachuska29617e52014-11-20 03:17:46 -0800964 }
965
Thomas Vachuska9edca302014-11-22 17:06:42 -0800966
Thomas Vachuska47635c62014-11-22 01:21:36 -0800967 function cancelTraffic() {
968 sendMessage('cancelTraffic', {});
969 }
970
Thomas Vachuska9edca302014-11-22 17:06:42 -0800971 function requestTrafficForMode() {
972 if (hoverMode === hoverModeAll) {
973 requestAllTraffic();
974 } else if (hoverMode === hoverModeFlows) {
975 requestDeviceLinkFlows();
976 } else if (hoverMode === hoverModeIntents) {
977 requestSelectTraffic();
Simon Huntd72bc702014-11-13 18:38:04 -0800978 }
Thomas Vachuska9edca302014-11-22 17:06:42 -0800979 }
Simon Huntd72bc702014-11-13 18:38:04 -0800980
Thomas Vachuska9edca302014-11-22 17:06:42 -0800981 function showTrafficAction() {
982 hoverMode = hoverModeIntents;
983 requestSelectTraffic();
984 network.view.flash('Related Traffic');
985 }
986
987 function requestSelectTraffic() {
988 if (validateSelectionContext()) {
989 var hoverId = (hoverMode === hoverModeIntents && hovered &&
990 (hovered.class === 'host' || hovered.class === 'device'))
Simon Huntd72bc702014-11-13 18:38:04 -0800991 ? hovered.id : '';
Thomas Vachuska9edca302014-11-22 17:06:42 -0800992 sendMessage('requestTraffic', {
993 ids: selectOrder,
994 hover: hoverId
995 });
996 }
Simon Huntd72bc702014-11-13 18:38:04 -0800997 }
998
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800999
Thomas Vachuska29617e52014-11-20 03:17:46 -08001000 function showDeviceLinkFlowsAction() {
Thomas Vachuska9edca302014-11-22 17:06:42 -08001001 hoverMode = hoverModeFlows;
1002 requestDeviceLinkFlows();
Thomas Vachuskac59658c2014-11-21 13:13:36 -08001003 network.view.flash('Device Flows');
Thomas Vachuska29617e52014-11-20 03:17:46 -08001004 }
1005
Thomas Vachuska9edca302014-11-22 17:06:42 -08001006 function requestDeviceLinkFlows() {
1007 if (validateSelectionContext()) {
1008 var hoverId = (hoverMode === hoverModeFlows && hovered &&
1009 (hovered.class === 'device')) ? hovered.id : '';
1010 sendMessage('requestDeviceLinkFlows', {
1011 ids: selectOrder,
1012 hover: hoverId
1013 });
1014 }
1015 }
1016
1017
1018 function showAllTrafficAction() {
1019 hoverMode = hoverModeAll;
1020 requestAllTraffic();
1021 network.view.flash('All Traffic');
1022 }
1023
1024 function requestAllTraffic() {
1025 sendMessage('requestAllTraffic', {});
1026 }
1027
1028 function validateSelectionContext() {
Thomas Vachuska29617e52014-11-20 03:17:46 -08001029 if (!hovered && nSel() === 0) {
Thomas Vachuska47635c62014-11-22 01:21:36 -08001030 cancelTraffic();
Thomas Vachuska9edca302014-11-22 17:06:42 -08001031 return false;
Thomas Vachuska29617e52014-11-20 03:17:46 -08001032 }
Thomas Vachuska9edca302014-11-22 17:06:42 -08001033 return true;
Thomas Vachuska29617e52014-11-20 03:17:46 -08001034 }
Simon Huntd72bc702014-11-13 18:38:04 -08001035
Simon Hunta6a9fe72014-11-20 11:17:12 -08001036
Simon Hunt61d04042014-11-11 17:27:16 -08001037 // ==============================
Simon Hunta5e89142014-11-14 07:00:33 -08001038 // onos instance panel functions
Simon Huntb82f6902014-11-22 11:53:15 -08001039
Simon Hunt7b403bc2014-11-22 19:01:00 -08001040 var instCfg = {
1041 rectPad: 8,
1042 nodeOx: 9,
1043 nodeOy: 9,
1044 nodeDim: 40,
1045 birdOx: 19,
1046 birdOy: 21,
1047 birdDim: 21,
1048 uiDy: 45,
1049 titleDy: 30,
1050 textYOff: 20,
1051 textYSpc: 15
1052 };
1053
1054 function viewBox(dim) {
1055 return '0 0 ' + dim.w + ' ' + dim.h;
1056 }
1057
1058 function instRectAttr(dim) {
1059 var pad = instCfg.rectPad;
1060 return {
1061 x: pad,
1062 y: pad,
1063 width: dim.w - pad*2,
1064 height: dim.h - pad*2,
Simon Huntb0ecfa52014-11-23 21:05:12 -08001065 rx: 6
Simon Hunt7b403bc2014-11-22 19:01:00 -08001066 };
1067 }
1068
1069 function computeDim(self) {
1070 var css = window.getComputedStyle(self);
1071 return {
1072 w: stripPx(css.width),
1073 h: stripPx(css.height)
1074 };
Simon Huntb82f6902014-11-22 11:53:15 -08001075 }
Simon Hunta5e89142014-11-14 07:00:33 -08001076
1077 function updateInstances() {
1078 var onoses = oiBox.el.selectAll('.onosInst')
Simon Huntb82f6902014-11-22 11:53:15 -08001079 .data(onosOrder, function (d) { return d.id; }),
Simon Hunt7b403bc2014-11-22 19:01:00 -08001080 instDim = {w:0,h:0},
1081 c = instCfg;
Simon Hunta5e89142014-11-14 07:00:33 -08001082
Simon Hunt7b403bc2014-11-22 19:01:00 -08001083 function nSw(n) {
1084 return '# Switches: ' + n;
1085 }
Simon Hunta5e89142014-11-14 07:00:33 -08001086
Simon Huntb82f6902014-11-22 11:53:15 -08001087 // operate on existing onos instances if necessary
1088 onoses.each(function (d) {
1089 var el = d3.select(this),
1090 svg = el.select('svg');
Simon Hunt7b403bc2014-11-22 19:01:00 -08001091 instDim = computeDim(this);
Simon Huntb82f6902014-11-22 11:53:15 -08001092
1093 // update online state
1094 el.classed('online', d.online);
1095
1096 // update ui-attached state
1097 svg.select('use.uiBadge').remove();
1098 if (d.uiAttached) {
1099 attachUiBadge(svg);
1100 }
1101
Simon Hunt7b403bc2014-11-22 19:01:00 -08001102 function updAttr(id, value) {
1103 svg.select('text.instLabel.'+id).text(value);
1104 }
1105
1106 updAttr('ip', d.ip);
1107 updAttr('ns', nSw(d.switches));
Simon Huntb82f6902014-11-22 11:53:15 -08001108 });
1109
1110
1111 // operate on new onos instances
Simon Hunta5e89142014-11-14 07:00:33 -08001112 var entering = onoses.enter()
1113 .append('div')
1114 .attr('class', 'onosInst')
1115 .classed('online', function (d) { return d.online; })
Simon Hunt9c15eca2014-11-15 18:37:59 -08001116 .on('click', clickInst);
1117
Simon Huntb82f6902014-11-22 11:53:15 -08001118 entering.each(function (d) {
Simon Hunt9c15eca2014-11-15 18:37:59 -08001119 var el = d3.select(this),
Simon Hunt7b403bc2014-11-22 19:01:00 -08001120 rectAttr,
1121 svg;
1122 instDim = computeDim(this);
1123 rectAttr = instRectAttr(instDim);
Simon Hunt9c15eca2014-11-15 18:37:59 -08001124
Simon Hunt7b403bc2014-11-22 19:01:00 -08001125 svg = el.append('svg').attr({
1126 width: instDim.w,
1127 height: instDim.h,
1128 viewBox: viewBox(instDim)
Simon Hunt95908012014-11-20 10:20:26 -08001129 });
Simon Huntb82f6902014-11-22 11:53:15 -08001130
Simon Hunt7b403bc2014-11-22 19:01:00 -08001131 svg.append('rect').attr(rectAttr);
Simon Hunt9c15eca2014-11-15 18:37:59 -08001132
Thomas Vachuskae02e11c2014-11-24 16:13:52 -08001133 //appendGlyph(svg, c.nodeOx, c.nodeOy, c.nodeDim, '#node');
1134 appendBadge(svg, 14, 14, 28, '#bird');
Simon Huntb82f6902014-11-22 11:53:15 -08001135
1136 if (d.uiAttached) {
1137 attachUiBadge(svg);
1138 }
1139
Simon Hunt7b403bc2014-11-22 19:01:00 -08001140 var left = c.nodeOx + c.nodeDim,
1141 len = rectAttr.width - left,
1142 hlen = len / 2,
1143 midline = hlen + left;
Simon Hunt9c15eca2014-11-15 18:37:59 -08001144
Simon Hunt7b403bc2014-11-22 19:01:00 -08001145 // title
1146 svg.append('text')
1147 .attr({
1148 class: 'instTitle',
1149 x: midline,
1150 y: c.titleDy
1151 })
1152 .text(d.id);
1153
1154 // a couple of attributes
1155 var ty = c.titleDy + c.textYOff;
1156
1157 function addAttr(id, label) {
1158 svg.append('text').attr({
1159 class: 'instLabel ' + id,
1160 x: midline,
1161 y: ty
1162 }).text(label);
1163 ty += c.textYSpc;
1164 }
1165
1166 addAttr('ip', d.ip);
1167 addAttr('ns', nSw(d.switches));
Simon Hunt9c15eca2014-11-15 18:37:59 -08001168 });
Simon Hunta5e89142014-11-14 07:00:33 -08001169
1170 // operate on existing + new onoses here
Simon Hunt8f40cce2014-11-23 15:57:30 -08001171 // set the affinity colors...
1172 onoses.each(function (d) {
1173 var el = d3.select(this),
1174 rect = el.select('svg').select('rect'),
1175 col = instColor(d.id, d.online);
1176 rect.style('fill', col);
1177 });
Simon Hunta5e89142014-11-14 07:00:33 -08001178
Simon Hunt7b403bc2014-11-22 19:01:00 -08001179 // adjust the panel size appropriately...
1180 oiBox.width(instDim.w * onosOrder.length);
1181 oiBox.height(instDim.h);
1182
1183 // remove any outgoing instances
1184 onoses.exit().remove();
Simon Hunta5e89142014-11-14 07:00:33 -08001185 }
1186
Simon Hunt8f40cce2014-11-23 15:57:30 -08001187 function instColor(id, online) {
1188 return cat7.get(id, !online, network.view.getTheme());
1189 }
1190
Simon Hunt9462e8c2014-11-14 17:28:09 -08001191 function clickInst(d) {
1192 var el = d3.select(this),
1193 aff = el.classed('affinity');
1194 if (!aff) {
1195 setAffinity(el, d);
1196 } else {
1197 cancelAffinity();
1198 }
1199 }
1200
1201 function setAffinity(el, d) {
1202 d3.selectAll('.onosInst')
1203 .classed('mastership', true)
1204 .classed('affinity', false);
1205 el.classed('affinity', true);
1206
1207 suppressLayers(true);
1208 node.each(function (n) {
1209 if (n.master === d.id) {
1210 n.el.classed('suppressed', false);
1211 }
1212 });
1213 oiShowMaster = true;
1214 }
1215
1216 function cancelAffinity() {
1217 d3.selectAll('.onosInst')
1218 .classed('mastership affinity', false);
1219 restoreLayerState();
1220 oiShowMaster = false;
1221 }
1222
Simon Hunt7b403bc2014-11-22 19:01:00 -08001223 // TODO: these should be moved out to utility module.
1224 function stripPx(s) {
1225 return s.replace(/px$/,'');
1226 }
1227
1228 function appendUse(svg, ox, oy, dim, iid, cls) {
1229 var use = svg.append('use').attr({
1230 transform: translate(ox,oy),
1231 'xlink:href': iid,
1232 width: dim,
1233 height: dim
1234 });
1235 if (cls) {
1236 use.classed(cls, true);
1237 }
1238 return use;
1239 }
1240
1241 function appendGlyph(svg, ox, oy, dim, iid, cls) {
1242 appendUse(svg, ox, oy, dim, iid, cls).classed('glyphIcon', true);
1243 }
1244
1245 function appendBadge(svg, ox, oy, dim, iid, cls) {
1246 appendUse(svg, ox, oy, dim, iid,cls ).classed('badgeIcon', true);
1247 }
1248
1249 function attachUiBadge(svg) {
1250 appendBadge(svg, 12, instCfg.uiDy, 30, '#uiAttached', 'uiBadge');
1251 }
1252
Simon Hunt434cf142014-11-24 11:10:28 -08001253 function visVal(b) {
1254 return b ? 'visible' : 'hidden';
1255 }
1256
Simon Hunta5e89142014-11-14 07:00:33 -08001257 // ==============================
Simon Hunt99c13842014-11-06 18:23:12 -08001258 // force layout modification functions
1259
1260 function translate(x, y) {
1261 return 'translate(' + x + ',' + y + ')';
1262 }
1263
Simon Hunte2575b62014-11-18 15:25:53 -08001264 function rotate(deg) {
1265 return 'rotate(' + deg + ')';
1266 }
1267
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001268 function missMsg(what, id) {
1269 return '\n[' + what + '] "' + id + '" missing ';
1270 }
1271
1272 function linkEndPoints(srcId, dstId) {
1273 var srcNode = network.lookup[srcId],
1274 dstNode = network.lookup[dstId],
1275 sMiss = !srcNode ? missMsg('src', srcId) : '',
1276 dMiss = !dstNode ? missMsg('dst', dstId) : '';
1277
1278 if (sMiss || dMiss) {
1279 logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
1280 return null;
1281 }
1282 return {
1283 source: srcNode,
1284 target: dstNode,
1285 x1: srcNode.x,
1286 y1: srcNode.y,
1287 x2: dstNode.x,
1288 y2: dstNode.y
1289 };
1290 }
1291
Simon Hunt56d51852014-11-09 13:03:35 -08001292 function createHostLink(host) {
1293 var src = host.id,
1294 dst = host.cp.device,
Simon Hunt7cd48f32014-11-09 23:42:50 -08001295 id = host.ingress,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001296 lnk = linkEndPoints(src, dst);
Simon Hunt56d51852014-11-09 13:03:35 -08001297
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001298 if (!lnk) {
Simon Hunt56d51852014-11-09 13:03:35 -08001299 return null;
1300 }
1301
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001302 // Synthesize link ...
1303 $.extend(lnk, {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001304 key: id,
Simon Hunt56d51852014-11-09 13:03:35 -08001305 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -08001306
1307 type: function () { return 'hostLink'; },
1308 // TODO: ideally, we should see if our edge switch is online...
1309 online: function () { return true; },
1310 linkWidth: function () { return 1; }
Simon Hunt7cd48f32014-11-09 23:42:50 -08001311 });
Simon Hunt99c13842014-11-06 18:23:12 -08001312 return lnk;
1313 }
1314
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001315 function createLink(link) {
Simon Hunta6a9fe72014-11-20 11:17:12 -08001316 var lnk = linkEndPoints(link.src, link.dst);
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001317
1318 if (!lnk) {
1319 return null;
1320 }
1321
Simon Hunt8257f4c2014-11-16 19:34:54 -08001322 $.extend(lnk, {
1323 key: link.id,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001324 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -08001325 fromSource: link,
1326
1327 // functions to aggregate dual link state
1328 type: function () {
1329 var s = lnk.fromSource,
1330 t = lnk.fromTarget;
1331 return (s && s.type) || (t && t.type) || defaultLinkType;
1332 },
1333 online: function () {
1334 var s = lnk.fromSource,
1335 t = lnk.fromTarget;
1336 return (s && s.online) || (t && t.online);
1337 },
1338 linkWidth: function () {
1339 var s = lnk.fromSource,
1340 t = lnk.fromTarget,
1341 ws = (s && s.linkWidth) || 0,
1342 wt = (t && t.linkWidth) || 0;
1343 return Math.max(ws, wt);
1344 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001345 });
1346 return lnk;
Simon Hunt1a9eff92014-11-07 11:06:34 -08001347 }
1348
Simon Hunte2575b62014-11-18 15:25:53 -08001349 function removeLinkLabels() {
1350 network.links.forEach(function (d) {
1351 d.label = '';
1352 });
1353 }
1354
Simon Hunt434cf142014-11-24 11:10:28 -08001355 function showHostVis(el) {
1356 el.style('visibility', visVal(showHosts));
1357 }
1358
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001359 var widthRatio = 1.4,
1360 linkScale = d3.scale.linear()
1361 .domain([1, 12])
1362 .range([widthRatio, 12 * widthRatio])
1363 .clamp(true);
1364
Simon Hunt99c13842014-11-06 18:23:12 -08001365 function updateLinks() {
1366 link = linkG.selectAll('.link')
Simon Hunt8257f4c2014-11-16 19:34:54 -08001367 .data(network.links, function (d) { return d.key; });
Simon Hunt99c13842014-11-06 18:23:12 -08001368
1369 // operate on existing links, if necessary
1370 // link .foo() .bar() ...
1371
1372 // operate on entering links:
1373 var entering = link.enter()
1374 .append('line')
1375 .attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001376 x1: function (d) { return d.x1; },
1377 y1: function (d) { return d.y1; },
1378 x2: function (d) { return d.x2; },
1379 y2: function (d) { return d.y2; },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001380 stroke: config.topo.linkInColor,
1381 'stroke-width': config.topo.linkInWidth
Simon Hunt99c13842014-11-06 18:23:12 -08001382 });
1383
1384 // augment links
Simon Hunt7cd48f32014-11-09 23:42:50 -08001385 entering.each(function (d) {
1386 var link = d3.select(this);
1387 // provide ref to element selection from backing data....
1388 d.el = link;
Simon Hunt8257f4c2014-11-16 19:34:54 -08001389 restyleLinkElement(d);
Simon Hunt434cf142014-11-24 11:10:28 -08001390 if (d.type() === 'hostLink') {
1391 showHostVis(link);
1392 }
Simon Hunt7cd48f32014-11-09 23:42:50 -08001393 });
Thomas Vachuska4830d392014-11-09 17:09:56 -08001394
1395 // operate on both existing and new links, if necessary
1396 //link .foo() .bar() ...
1397
Simon Hunte2575b62014-11-18 15:25:53 -08001398 // apply or remove labels
1399 var labelData = getLabelData();
1400 applyLinkLabels(labelData);
1401
Thomas Vachuska4830d392014-11-09 17:09:56 -08001402 // operate on exiting links:
Thomas Vachuska4830d392014-11-09 17:09:56 -08001403 link.exit()
Simon Hunt13bf9c82014-11-18 07:26:44 -08001404 .attr('stroke-dasharray', '3, 3')
1405 .style('opacity', 0.5)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001406 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -08001407 .duration(1500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001408 .attr({
Simon Hunt13bf9c82014-11-18 07:26:44 -08001409 'stroke-dasharray': '3, 12',
1410 stroke: config.topo.linkOutColor,
1411 'stroke-width': config.topo.linkOutWidth
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001412 })
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001413 .style('opacity', 0.0)
Thomas Vachuska4830d392014-11-09 17:09:56 -08001414 .remove();
Simon Hunte2575b62014-11-18 15:25:53 -08001415
1416 // NOTE: invoke a single tick to force the labels to position
1417 // onto their links.
1418 tick();
1419 }
1420
1421 function getLabelData() {
1422 // create the backing data for showing labels..
1423 var data = [];
1424 link.each(function (d) {
1425 if (d.label) {
1426 data.push({
1427 id: 'lab-' + d.key,
1428 key: d.key,
1429 label: d.label,
1430 ldata: d
1431 });
1432 }
1433 });
1434 return data;
1435 }
1436
1437 var linkLabelOffset = '0.3em';
1438
1439 function applyLinkLabels(data) {
1440 var entering;
1441
1442 linkLabel = linkLabelG.selectAll('.linkLabel')
1443 .data(data, function (d) { return d.id; });
1444
Simon Hunt56a2ea42014-11-19 12:39:31 -08001445 // for elements already existing, we need to update the text
1446 // and adjust the rectangle size to fit
1447 linkLabel.each(function (d) {
1448 var el = d3.select(this),
1449 rect = el.select('rect'),
1450 text = el.select('text');
1451 text.text(d.label);
1452 rect.attr(rectAroundText(el));
1453 });
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -08001454
Simon Hunte2575b62014-11-18 15:25:53 -08001455 entering = linkLabel.enter().append('g')
1456 .classed('linkLabel', true)
1457 .attr('id', function (d) { return d.id; });
1458
1459 entering.each(function (d) {
1460 var el = d3.select(this),
1461 rect,
1462 text,
1463 parms = {
1464 x1: d.ldata.x1,
1465 y1: d.ldata.y1,
1466 x2: d.ldata.x2,
1467 y2: d.ldata.y2
1468 };
1469
1470 d.el = el;
1471 rect = el.append('rect');
1472 text = el.append('text').text(d.label);
1473 rect.attr(rectAroundText(el));
1474 text.attr('dy', linkLabelOffset);
1475
1476 el.attr('transform', transformLabel(parms));
1477 });
1478
1479 // Remove any links that are no longer required.
1480 linkLabel.exit().remove();
1481 }
1482
1483 function rectAroundText(el) {
1484 var text = el.select('text'),
1485 box = text.node().getBBox();
1486
1487 // translate the bbox so that it is centered on [x,y]
1488 box.x = -box.width / 2;
1489 box.y = -box.height / 2;
1490
1491 // add padding
1492 box.x -= 1;
1493 box.width += 2;
1494 return box;
1495 }
1496
1497 function transformLabel(p) {
1498 var dx = p.x2 - p.x1,
1499 dy = p.y2 - p.y1,
1500 xMid = dx/2 + p.x1,
1501 yMid = dy/2 + p.y1;
Simon Hunte2575b62014-11-18 15:25:53 -08001502 return translate(xMid, yMid);
Simon Hunt99c13842014-11-06 18:23:12 -08001503 }
1504
1505 function createDeviceNode(device) {
1506 // start with the object as is
1507 var node = device,
Simon Huntbb282f52014-11-10 11:08:19 -08001508 type = device.type,
Simon Huntc72967b2014-11-20 09:21:42 -08001509 svgCls = type ? 'node device ' + type : 'node device',
1510 labels = device.labels || [];
1511
Simon Hunt99c13842014-11-06 18:23:12 -08001512 // Augment as needed...
1513 node.class = 'device';
Simon Huntbb282f52014-11-10 11:08:19 -08001514 node.svgClass = device.online ? svgCls + ' online' : svgCls;
Simon Hunt99c13842014-11-06 18:23:12 -08001515 positionNode(node);
Simon Hunt99c13842014-11-06 18:23:12 -08001516 return node;
1517 }
1518
Simon Hunt56d51852014-11-09 13:03:35 -08001519 function createHostNode(host) {
1520 // start with the object as is
1521 var node = host;
1522
1523 // Augment as needed...
1524 node.class = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001525 if (!node.type) {
Simon Hunt209155e2014-11-21 12:16:09 -08001526 node.type = 'endstation';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001527 }
Simon Hunt7fa116d2014-11-17 14:16:55 -08001528 node.svgClass = 'node host ' + node.type;
Simon Hunt56d51852014-11-09 13:03:35 -08001529 positionNode(node);
Simon Hunt56d51852014-11-09 13:03:35 -08001530 return node;
1531 }
1532
Simon Hunt62c47542014-11-22 22:16:32 -08001533 function positionNode(node, forUpdate) {
Simon Hunt99c13842014-11-06 18:23:12 -08001534 var meta = node.metaUi,
Simon Huntac9e24f2014-11-12 10:12:21 -08001535 x = meta && meta.x,
1536 y = meta && meta.y,
1537 xy;
Simon Hunt99c13842014-11-06 18:23:12 -08001538
Simon Huntac9e24f2014-11-12 10:12:21 -08001539 // If we have [x,y] already, use that...
Simon Hunt99c13842014-11-06 18:23:12 -08001540 if (x && y) {
1541 node.fixed = true;
Simon Hunt62c47542014-11-22 22:16:32 -08001542 node.px = node.x = x;
1543 node.py = node.y = y;
1544 //node.px = x;
1545 //node.py = y;
Simon Huntac9e24f2014-11-12 10:12:21 -08001546 return;
Simon Hunt99c13842014-11-06 18:23:12 -08001547 }
Simon Huntac9e24f2014-11-12 10:12:21 -08001548
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001549 var location = node.location;
1550 if (location && location.type === 'latlng') {
1551 var coord = geoMapProjection([location.lng, location.lat]);
1552 node.fixed = true;
Simon Hunt62c47542014-11-22 22:16:32 -08001553 node.px = node.x = coord[0];
1554 node.py = node.y = coord[1];
1555 //node.x = coord[0];
1556 //node.y = coord[1];
1557 return true;
1558 }
1559
1560 // if this is a node update (not a node add).. skip randomizer
1561 if (forUpdate) {
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001562 return;
1563 }
1564
Simon Huntac9e24f2014-11-12 10:12:21 -08001565 // Note: Placing incoming unpinned nodes at exactly the same point
1566 // (center of the view) causes them to explode outwards when
1567 // the force layout kicks in. So, we spread them out a bit
1568 // initially, to provide a more serene layout convergence.
1569 // Additionally, if the node is a host, we place it near
1570 // the device it is connected to.
1571
1572 function spread(s) {
1573 return Math.floor((Math.random() * s) - s/2);
1574 }
1575
1576 function randDim(dim) {
1577 return dim / 2 + spread(dim * 0.7071);
1578 }
1579
1580 function rand() {
1581 return {
1582 x: randDim(network.view.width()),
1583 y: randDim(network.view.height())
1584 };
1585 }
1586
1587 function near(node) {
1588 var min = 12,
1589 dx = spread(12),
1590 dy = spread(12);
1591 return {
1592 x: node.x + min + dx,
1593 y: node.y + min + dy
1594 };
1595 }
1596
1597 function getDevice(cp) {
1598 var d = network.lookup[cp.device];
1599 return d || rand();
1600 }
1601
1602 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
1603 $.extend(node, xy);
Simon Hunt99c13842014-11-06 18:23:12 -08001604 }
1605
Simon Hunt99c13842014-11-06 18:23:12 -08001606
Simon Huntc72967b2014-11-20 09:21:42 -08001607 function iconGlyphUrl(d) {
1608 var which = d.type || 'unknown';
1609 return '#' + which;
1610 }
1611
Simon Hunt99c13842014-11-06 18:23:12 -08001612 // returns the newly computed bounding box of the rectangle
1613 function adjustRectToFitText(n) {
1614 var text = n.select('text'),
1615 box = text.node().getBBox(),
1616 lab = config.labels;
1617
1618 text.attr('text-anchor', 'middle')
1619 .attr('y', '-0.8em')
1620 .attr('x', lab.imgPad/2);
1621
1622 // translate the bbox so that it is centered on [x,y]
1623 box.x = -box.width / 2;
1624 box.y = -box.height / 2;
1625
1626 // add padding
1627 box.x -= (lab.padLR + lab.imgPad/2);
1628 box.width += lab.padLR * 2 + lab.imgPad;
1629 box.y -= lab.padTB;
1630 box.height += lab.padTB * 2;
1631
1632 return box;
1633 }
1634
Simon Hunt1a9eff92014-11-07 11:06:34 -08001635 function mkSvgClass(d) {
1636 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
1637 }
1638
Simon Hunt7cd48f32014-11-09 23:42:50 -08001639 function hostLabel(d) {
1640 var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0;
1641 return d.labels[idx];
1642 }
1643 function deviceLabel(d) {
1644 var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0;
1645 return d.labels[idx];
1646 }
Simon Huntc72967b2014-11-20 09:21:42 -08001647 function trimLabel(label) {
1648 return (label && label.trim()) || '';
1649 }
1650
1651 function emptyBox() {
1652 return {
1653 x: -2,
1654 y: -2,
1655 width: 4,
1656 height: 4
1657 };
Simon Hunt7cd48f32014-11-09 23:42:50 -08001658 }
1659
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001660 function updateDeviceLabel(d) {
Simon Huntc72967b2014-11-20 09:21:42 -08001661 var label = trimLabel(deviceLabel(d)),
1662 noLabel = !label,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001663 node = d.el,
Simon Huntc72967b2014-11-20 09:21:42 -08001664 box,
1665 dx,
Simon Hunt395a70c2014-11-22 23:17:40 -08001666 dy,
1667 cfg = config.icons.device;
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001668
1669 node.select('text')
1670 .text(label)
1671 .style('opacity', 0)
1672 .transition()
1673 .style('opacity', 1);
1674
Simon Huntc72967b2014-11-20 09:21:42 -08001675 if (noLabel) {
1676 box = emptyBox();
Simon Hunt395a70c2014-11-22 23:17:40 -08001677 dx = -cfg.dim/2;
1678 dy = -cfg.dim/2;
Simon Huntc72967b2014-11-20 09:21:42 -08001679 } else {
1680 box = adjustRectToFitText(node);
Simon Hunt395a70c2014-11-22 23:17:40 -08001681 dx = box.x + cfg.xoff;
1682 dy = box.y + cfg.yoff;
Simon Huntc72967b2014-11-20 09:21:42 -08001683 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001684
1685 node.select('rect')
1686 .transition()
1687 .attr(box);
1688
Simon Huntc72967b2014-11-20 09:21:42 -08001689 node.select('g.deviceIcon')
Thomas Vachuska89543292014-11-19 11:28:33 -08001690 .transition()
Simon Huntc72967b2014-11-20 09:21:42 -08001691 .attr('transform', translate(dx, dy));
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001692 }
1693
1694 function updateHostLabel(d) {
1695 var label = hostLabel(d),
1696 host = d.el;
1697
1698 host.select('text').text(label);
1699 }
1700
Simon Hunt62c47542014-11-22 22:16:32 -08001701 // FIXME : fold this into updateNodes.
Simon Huntbb282f52014-11-10 11:08:19 -08001702 function updateHostState(hostData) {
1703 updateHostLabel(hostData);
Simon Huntbb282f52014-11-10 11:08:19 -08001704 }
1705
Simon Hunt434cf142014-11-24 11:10:28 -08001706 function updateHostVisibility() {
1707 var v = visVal(showHosts);
1708 nodeG.selectAll('.host').style('visibility', v);
1709 linkG.selectAll('.hostLink').style('visibility', v);
1710 }
1711
Simon Hunt6ac93f32014-11-13 12:17:27 -08001712 function nodeMouseOver(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001713 hovered = d;
Thomas Vachuska9edca302014-11-22 17:06:42 -08001714 requestTrafficForMode();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001715 }
1716
1717 function nodeMouseOut(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001718 hovered = null;
Thomas Vachuska9edca302014-11-22 17:06:42 -08001719 requestTrafficForMode();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001720 }
Simon Huntbb282f52014-11-10 11:08:19 -08001721
Simon Hunteb1514d2014-11-20 09:57:29 -08001722 function addHostIcon(node, radius, iid) {
Thomas Vachuska89543292014-11-19 11:28:33 -08001723 var dim = radius * 1.5,
1724 xlate = -dim / 2;
1725
Simon Hunteb1514d2014-11-20 09:57:29 -08001726 node.append('use').attr({
1727 class: 'glyphIcon hostIcon',
1728 transform: translate(xlate,xlate),
1729 'xlink:href': iid,
1730 width: dim,
1731 height: dim
1732 });
Thomas Vachuska89543292014-11-19 11:28:33 -08001733 }
1734
Simon Hunt99c13842014-11-06 18:23:12 -08001735 function updateNodes() {
1736 node = nodeG.selectAll('.node')
1737 .data(network.nodes, function (d) { return d.id; });
1738
Simon Hunt62c47542014-11-22 22:16:32 -08001739 // operate on existing nodes...
1740 node.filter('.device').each(function (d) {
Simon Hunt62c47542014-11-22 22:16:32 -08001741 var node = d.el;
1742 node.classed('online', d.online);
1743 updateDeviceLabel(d);
1744 positionNode(d, true);
1745 });
1746
1747 node.filter('.host').each(function (d) {
Simon Hunt62c47542014-11-22 22:16:32 -08001748 var node = d.el;
1749 // TODO: appropriate update of host visuals
1750 });
Simon Hunt99c13842014-11-06 18:23:12 -08001751
1752 // operate on entering nodes:
1753 var entering = node.enter()
1754 .append('g')
1755 .attr({
1756 id: function (d) { return safeId(d.id); },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001757 class: mkSvgClass,
Simon Hunt99c13842014-11-06 18:23:12 -08001758 transform: function (d) { return translate(d.x, d.y); },
1759 opacity: 0
1760 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08001761 .call(network.drag)
Simon Hunt6ac93f32014-11-13 12:17:27 -08001762 .on('mouseover', nodeMouseOver)
1763 .on('mouseout', nodeMouseOut)
Simon Hunt99c13842014-11-06 18:23:12 -08001764 .transition()
1765 .attr('opacity', 1);
1766
1767 // augment device nodes...
1768 entering.filter('.device').each(function (d) {
1769 var node = d3.select(this),
Simon Huntc72967b2014-11-20 09:21:42 -08001770 label = trimLabel(deviceLabel(d)),
1771 noLabel = !label,
Simon Hunt99c13842014-11-06 18:23:12 -08001772 box;
1773
Simon Hunt7cd48f32014-11-09 23:42:50 -08001774 // provide ref to element from backing data....
1775 d.el = node;
1776
Simon Hunt62c47542014-11-22 22:16:32 -08001777 node.append('rect').attr({ rx: 5, ry: 5 });
1778 node.append('text').text(label).attr('dy', '1.1em');
Simon Hunt99c13842014-11-06 18:23:12 -08001779 box = adjustRectToFitText(node);
Simon Hunta3dd9572014-11-20 15:22:41 -08001780 node.select('rect').attr(box);
Simon Huntc72967b2014-11-20 09:21:42 -08001781 addDeviceIcon(node, box, noLabel, iconGlyphUrl(d));
Simon Huntc7ee0662014-11-05 16:44:37 -08001782 });
Simon Hunt934c3ce2014-11-05 11:45:07 -08001783
Thomas Vachuska89543292014-11-19 11:28:33 -08001784
Simon Hunt56d51852014-11-09 13:03:35 -08001785 // augment host nodes...
1786 entering.filter('.host').each(function (d) {
1787 var node = d3.select(this),
Simon Hunt395a70c2014-11-22 23:17:40 -08001788 cfg = config.icons.host,
1789 r = cfg.radius[d.type] || cfg.defaultRadius,
Thomas Vachuska89543292014-11-19 11:28:33 -08001790 textDy = r + 10,
Simon Hunteb1514d2014-11-20 09:57:29 -08001791 iid = iconGlyphUrl(d);
Simon Hunt56d51852014-11-09 13:03:35 -08001792
Simon Hunt7cd48f32014-11-09 23:42:50 -08001793 // provide ref to element from backing data....
1794 d.el = node;
Simon Hunt434cf142014-11-24 11:10:28 -08001795 showHostVis(node);
Simon Hunt7cd48f32014-11-09 23:42:50 -08001796
Simon Hunt62c47542014-11-22 22:16:32 -08001797 node.append('circle').attr('r', r);
Simon Hunteb1514d2014-11-20 09:57:29 -08001798 if (iid) {
1799 addHostIcon(node, r, iid);
Simon Hunt7fa116d2014-11-17 14:16:55 -08001800 }
Simon Hunt56d51852014-11-09 13:03:35 -08001801 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001802 .text(hostLabel)
Thomas Vachuska89543292014-11-19 11:28:33 -08001803 .attr('dy', textDy)
Simon Hunt7cd48f32014-11-09 23:42:50 -08001804 .attr('text-anchor', 'middle');
Simon Hunt56d51852014-11-09 13:03:35 -08001805 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001806
Simon Hunt99c13842014-11-06 18:23:12 -08001807 // operate on both existing and new nodes, if necessary
Simon Huntb0ecfa52014-11-23 21:05:12 -08001808 updateDeviceColors();
Simon Huntc7ee0662014-11-05 16:44:37 -08001809
Simon Hunt99c13842014-11-06 18:23:12 -08001810 // operate on exiting nodes:
Simon Huntea80eb42014-11-11 13:46:57 -08001811 // Note that the node is removed after 2 seconds.
1812 // Sub element animations should be shorter than 2 seconds.
1813 var exiting = node.exit()
Simon Hunt44031102014-11-11 13:20:36 -08001814 .transition()
1815 .duration(2000)
Simon Huntea80eb42014-11-11 13:46:57 -08001816 .style('opacity', 0)
Simon Hunt99c13842014-11-06 18:23:12 -08001817 .remove();
Simon Huntea80eb42014-11-11 13:46:57 -08001818
1819 // host node exits....
1820 exiting.filter('.host').each(function (d) {
1821 var node = d3.select(this);
1822
1823 node.select('text')
1824 .style('opacity', 0.5)
1825 .transition()
1826 .duration(1000)
1827 .style('opacity', 0);
1828 // note, leave <g>.remove to remove this element
1829
Thomas Vachuska89543292014-11-19 11:28:33 -08001830 node.select('circle')
1831 .style('stroke-fill', '#555')
1832 .style('fill', '#888')
Simon Huntea80eb42014-11-11 13:46:57 -08001833 .style('opacity', 0.5)
1834 .transition()
1835 .duration(1500)
1836 .attr('r', 0);
1837 // note, leave <g>.remove to remove this element
1838
1839 });
1840
Simon Hunt62c47542014-11-22 22:16:32 -08001841 // TODO: device node exit animation
1842
1843 network.force.resume();
Simon Huntc7ee0662014-11-05 16:44:37 -08001844 }
1845
Simon Hunt95dad922014-11-24 09:43:31 -08001846 var dCol = {
1847 black: '#000',
1848 paleblue: '#acf',
1849 offwhite: '#ddd',
1850 midgrey: '#888',
1851 lightgrey: '#bbb',
1852 orange: '#f90'
1853 };
1854
Simon Huntb0ecfa52014-11-23 21:05:12 -08001855 // note: these are the device icon colors without affinity
Simon Hunt95dad922014-11-24 09:43:31 -08001856 var dColTheme = {
Simon Huntb0ecfa52014-11-23 21:05:12 -08001857 light: {
1858 online: {
Simon Hunt95dad922014-11-24 09:43:31 -08001859 glyph: dCol.black,
1860 rect: dCol.paleblue
Simon Huntb0ecfa52014-11-23 21:05:12 -08001861 },
1862 offline: {
Simon Hunt95dad922014-11-24 09:43:31 -08001863 glyph: dCol.midgrey,
1864 rect: dCol.lightgrey
Simon Huntb0ecfa52014-11-23 21:05:12 -08001865 }
1866 },
Simon Hunt95dad922014-11-24 09:43:31 -08001867 // TODO: theme
Simon Huntb0ecfa52014-11-23 21:05:12 -08001868 dark: {
1869 online: {
Simon Hunt95dad922014-11-24 09:43:31 -08001870 glyph: dCol.black,
1871 rect: dCol.paleblue
Simon Huntb0ecfa52014-11-23 21:05:12 -08001872 },
1873 offline: {
Simon Hunt95dad922014-11-24 09:43:31 -08001874 glyph: dCol.midgrey,
1875 rect: dCol.lightgrey
Simon Huntb0ecfa52014-11-23 21:05:12 -08001876 }
1877 }
1878 };
1879
1880 function devBaseColor(d) {
1881 var t = network.view.getTheme(),
1882 o = d.online ? 'online' : 'offline';
Simon Hunt95dad922014-11-24 09:43:31 -08001883 return dColTheme[t][o];
Simon Huntb0ecfa52014-11-23 21:05:12 -08001884 }
1885
1886 function setDeviceColor(d) {
1887 var o = d.online,
1888 s = d.el.classed('selected'),
1889 c = devBaseColor(d),
1890 a = instColor(d.master, o),
1891 g, r,
1892 icon = d.el.select('g.deviceIcon');
1893
1894 if (s) {
1895 g = c.glyph;
Simon Hunt95dad922014-11-24 09:43:31 -08001896 r = dColTheme.sel;
Simon Huntb0ecfa52014-11-23 21:05:12 -08001897 } else if (colorAffinity) {
1898 g = o ? a : c.glyph;
Simon Hunt95dad922014-11-24 09:43:31 -08001899 r = o ? dCol.offwhite : a;
Simon Huntb0ecfa52014-11-23 21:05:12 -08001900 } else {
1901 g = c.glyph;
1902 r = c.rect;
1903 }
1904
1905 icon.select('use')
1906 .style('fill', g);
1907 icon.select('rect')
1908 .style('fill', r);
1909 }
1910
Simon Huntc72967b2014-11-20 09:21:42 -08001911 function addDeviceIcon(node, box, noLabel, iid) {
1912 var cfg = config.icons.device,
1913 dx,
1914 dy,
1915 g;
1916
1917 if (noLabel) {
1918 box = emptyBox();
1919 dx = -cfg.dim/2;
1920 dy = -cfg.dim/2;
1921 } else {
1922 box = adjustRectToFitText(node);
Simon Hunt395a70c2014-11-22 23:17:40 -08001923 dx = box.x + cfg.xoff;
1924 dy = box.y + cfg.yoff;
Simon Huntc72967b2014-11-20 09:21:42 -08001925 }
1926
Simon Hunteb1514d2014-11-20 09:57:29 -08001927 g = node.append('g')
1928 .attr('class', 'glyphIcon deviceIcon')
Simon Huntc72967b2014-11-20 09:21:42 -08001929 .attr('transform', translate(dx, dy));
1930
1931 g.append('rect').attr({
1932 x: 0,
1933 y: 0,
1934 rx: cfg.rx,
1935 width: cfg.dim,
1936 height: cfg.dim
1937 });
1938
1939 g.append('use').attr({
1940 'xlink:href': iid,
1941 width: cfg.dim,
1942 height: cfg.dim
1943 });
1944
Simon Huntc72967b2014-11-20 09:21:42 -08001945 }
1946
Simon Hunt7b403bc2014-11-22 19:01:00 -08001947 function find(key, array, tag) {
1948 var _tag = tag || 'key',
1949 idx, n, d;
1950 for (idx = 0, n = array.length; idx < n; idx++) {
1951 d = array[idx];
1952 if (d[_tag] === key) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001953 return idx;
1954 }
1955 }
1956 return -1;
1957 }
1958
1959 function removeLinkElement(linkData) {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001960 var idx = find(linkData.key, network.links),
1961 removed;
1962 if (idx >=0) {
1963 // remove from links array
1964 removed = network.links.splice(idx, 1);
1965 // remove from lookup cache
1966 delete network.lookup[removed[0].key];
1967 updateLinks();
1968 network.force.resume();
1969 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001970 }
Simon Huntc7ee0662014-11-05 16:44:37 -08001971
Simon Hunt44031102014-11-11 13:20:36 -08001972 function removeHostElement(hostData) {
1973 // first, remove associated hostLink...
1974 removeLinkElement(hostData.linkData);
1975
1976 // remove from lookup cache
1977 delete network.lookup[hostData.id];
1978 // remove from nodes array
1979 var idx = find(hostData.id, network.nodes);
1980 network.nodes.splice(idx, 1);
1981 // remove from SVG
1982 updateNodes();
1983 network.force.resume();
1984 }
1985
1986
Simon Huntc7ee0662014-11-05 16:44:37 -08001987 function tick() {
1988 node.attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001989 transform: function (d) { return translate(d.x, d.y); }
Simon Huntc7ee0662014-11-05 16:44:37 -08001990 });
1991
1992 link.attr({
1993 x1: function (d) { return d.source.x; },
1994 y1: function (d) { return d.source.y; },
1995 x2: function (d) { return d.target.x; },
1996 y2: function (d) { return d.target.y; }
1997 });
Simon Hunte2575b62014-11-18 15:25:53 -08001998
1999 linkLabel.each(function (d) {
2000 var el = d3.select(this);
Thomas Vachuska4731f122014-11-20 04:56:19 -08002001 var lnk = findLinkById(d.key);
2002
2003 if (lnk) {
2004 var parms = {
Simon Hunte2575b62014-11-18 15:25:53 -08002005 x1: lnk.source.x,
2006 y1: lnk.source.y,
2007 x2: lnk.target.x,
2008 y2: lnk.target.y
2009 };
Thomas Vachuska4731f122014-11-20 04:56:19 -08002010 el.attr('transform', transformLabel(parms));
2011 }
Simon Hunte2575b62014-11-18 15:25:53 -08002012 });
Simon Huntc7ee0662014-11-05 16:44:37 -08002013 }
Simon Hunt934c3ce2014-11-05 11:45:07 -08002014
2015 // ==============================
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002016 // Web-Socket for live data
2017
2018 function webSockUrl() {
2019 return document.location.toString()
2020 .replace(/\#.*/, '')
2021 .replace('http://', 'ws://')
2022 .replace('https://', 'wss://')
Simon Hunte5ab1382014-11-25 10:28:51 -08002023 .replace('index.html', config.webSockUrl);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002024 }
2025
2026 webSock = {
2027 ws : null,
2028
2029 connect : function() {
2030 webSock.ws = new WebSocket(webSockUrl());
2031
2032 webSock.ws.onopen = function() {
Simon Hunt0c6d4192014-11-12 12:07:10 -08002033 noWebSock(false);
Thomas Vachuska47635c62014-11-22 01:21:36 -08002034 requestSummary();
Simon Huntb0ecfa52014-11-23 21:05:12 -08002035 showInstances();
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002036 };
2037
2038 webSock.ws.onmessage = function(m) {
2039 if (m.data) {
Simon Huntbb282f52014-11-10 11:08:19 -08002040 wsTraceRx(m.data);
Thomas Vachuskad472c6e2014-11-07 19:11:05 -08002041 handleServerEvent(JSON.parse(m.data));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002042 }
2043 };
2044
2045 webSock.ws.onclose = function(m) {
2046 webSock.ws = null;
Simon Hunt0c6d4192014-11-12 12:07:10 -08002047 noWebSock(true);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002048 };
2049 },
2050
2051 send : function(text) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002052 if (text != null) {
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002053 webSock._send(text);
2054 }
2055 },
2056
2057 _send : function(message) {
2058 if (webSock.ws) {
2059 webSock.ws.send(message);
Simon Hunta255a2c2014-11-13 22:29:35 -08002060 } else if (config.useLiveData) {
Simon Hunt434cf142014-11-24 11:10:28 -08002061 console.warn('no web socket open', message);
Simon Hunta255a2c2014-11-13 22:29:35 -08002062 } else {
Simon Hunt434cf142014-11-24 11:10:28 -08002063 console.log('WS Send: ', message);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002064 }
2065 }
2066
2067 };
2068
Simon Hunt0c6d4192014-11-12 12:07:10 -08002069 function noWebSock(b) {
2070 mask.style('display',b ? 'block' : 'none');
2071 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002072
2073 function sendMessage(evType, payload) {
2074 var toSend = {
Simon Huntbb282f52014-11-10 11:08:19 -08002075 event: evType,
2076 sid: ++sid,
2077 payload: payload
2078 },
2079 asText = JSON.stringify(toSend);
2080 wsTraceTx(asText);
2081 webSock.send(asText);
Simon Huntc76ae892014-11-18 17:31:51 -08002082
2083 // Temporary measure for debugging UI behavior ...
2084 if (!config.useLiveData) {
2085 handleTestSend(toSend);
2086 }
Simon Huntbb282f52014-11-10 11:08:19 -08002087 }
2088
2089 function wsTraceTx(msg) {
2090 wsTrace('tx', msg);
2091 }
2092 function wsTraceRx(msg) {
2093 wsTrace('rx', msg);
2094 }
2095 function wsTrace(rxtx, msg) {
Simon Huntbb282f52014-11-10 11:08:19 -08002096 console.log('[' + rxtx + '] ' + msg);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002097 }
2098
Simon Huntc76ae892014-11-18 17:31:51 -08002099 // NOTE: Temporary hardcoded example for showing detail pane
2100 // while we fine-
2101 // Probably should not merge this change...
2102 function handleTestSend(msg) {
2103 if (msg.event === 'requestDetails') {
2104 showDetails({
2105 event: 'showDetails',
2106 sid: 1001,
2107 payload: {
2108 "id": "of:0000ffffffffff09",
2109 "type": "roadm",
2110 "propOrder": [
2111 "Name",
2112 "Vendor",
2113 "H/W Version",
2114 "S/W Version",
2115 "-",
2116 "Latitude",
2117 "Longitude",
2118 "Ports"
2119 ],
2120 "props": {
2121 "Name": null,
2122 "Vendor": "Linc",
2123 "H/W Version": "OE",
2124 "S/W Version": "?",
2125 "-": "",
2126 "Latitude": "40.8",
2127 "Longitude": "73.1",
2128 "Ports": "2"
2129 }
2130 }
2131 });
2132 }
2133 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002134
2135 // ==============================
2136 // Selection stuff
2137
2138 function selectObject(obj, el) {
2139 var n,
Simon Hunt01095ff2014-11-13 16:37:29 -08002140 srcEv = d3.event.sourceEvent,
2141 meta = srcEv.metaKey,
2142 shift = srcEv.shiftKey;
2143
Simon Huntdeab4322014-11-13 18:49:07 -08002144 if ((panZoom() && !meta) || (!panZoom() && meta)) {
Simon Hunt01095ff2014-11-13 16:37:29 -08002145 return;
2146 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002147
2148 if (el) {
2149 n = d3.select(el);
2150 } else {
2151 node.each(function(d) {
2152 if (d == obj) {
2153 n = d3.select(el = this);
2154 }
2155 });
2156 }
2157 if (!n) return;
2158
Simon Hunt01095ff2014-11-13 16:37:29 -08002159 if (shift && n.classed('selected')) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002160 deselectObject(obj.id);
Simon Hunt61d04042014-11-11 17:27:16 -08002161 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002162 return;
2163 }
2164
Simon Hunt01095ff2014-11-13 16:37:29 -08002165 if (!shift) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002166 deselectAll();
2167 }
2168
Simon Huntc31d5692014-11-12 13:27:18 -08002169 selections[obj.id] = { obj: obj, el: el };
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002170 selectOrder.push(obj.id);
2171
2172 n.classed('selected', true);
Simon Huntb0ecfa52014-11-23 21:05:12 -08002173 updateDeviceColors(obj);
Simon Hunt61d04042014-11-11 17:27:16 -08002174 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002175 }
2176
2177 function deselectObject(id) {
Simon Huntc31d5692014-11-12 13:27:18 -08002178 var obj = selections[id],
2179 idx;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002180 if (obj) {
2181 d3.select(obj.el).classed('selected', false);
Simon Hunt61d04042014-11-11 17:27:16 -08002182 delete selections[id];
Simon Huntc31d5692014-11-12 13:27:18 -08002183 idx = $.inArray(id, selectOrder);
2184 if (idx >= 0) {
2185 selectOrder.splice(idx, 1);
2186 }
Simon Huntb0ecfa52014-11-23 21:05:12 -08002187 updateDeviceColors(obj.obj);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002188 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002189 }
2190
2191 function deselectAll() {
2192 // deselect all nodes in the network...
2193 node.classed('selected', false);
2194 selections = {};
2195 selectOrder = [];
Simon Huntb0ecfa52014-11-23 21:05:12 -08002196 updateDeviceColors();
Simon Hunt61d04042014-11-11 17:27:16 -08002197 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002198 }
2199
Simon Huntb0ecfa52014-11-23 21:05:12 -08002200 function updateDeviceColors(d) {
2201 if (d) {
2202 setDeviceColor(d);
2203 } else {
2204 node.filter('.device').each(function (d) {
2205 setDeviceColor(d);
2206 });
2207 }
Thomas Vachuska47635c62014-11-22 01:21:36 -08002208 }
2209
Simon Hunt61d04042014-11-11 17:27:16 -08002210 // update the state of the detail pane, based on current selections
2211 function updateDetailPane() {
2212 var nSel = selectOrder.length;
2213 if (!nSel) {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002214 emptySelect();
Simon Hunt61d04042014-11-11 17:27:16 -08002215 } else if (nSel === 1) {
2216 singleSelect();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002217 requestTrafficForMode();
Simon Hunt61d04042014-11-11 17:27:16 -08002218 } else {
2219 multiSelect();
2220 }
2221 }
2222
Thomas Vachuska9edca302014-11-22 17:06:42 -08002223 function emptySelect() {
Simon Hunt06811b72014-11-25 18:54:48 -08002224 hideDetailPane();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002225 cancelTraffic();
2226 }
2227
Simon Hunt61d04042014-11-11 17:27:16 -08002228 function singleSelect() {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002229 // NOTE: detail is shown from showDetails event callback
Simon Hunt61d04042014-11-11 17:27:16 -08002230 requestDetails();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002231 requestTrafficForMode();
Simon Hunt61d04042014-11-11 17:27:16 -08002232 }
2233
2234 function multiSelect() {
Simon Huntb53e0682014-11-12 13:32:01 -08002235 populateMultiSelect();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002236 requestTrafficForMode();
Simon Huntb53e0682014-11-12 13:32:01 -08002237 }
2238
2239 function addSep(tbody) {
2240 var tr = tbody.append('tr');
2241 $('<hr>').appendTo(tr.append('td').attr('colspan', 2));
2242 }
2243
2244 function addProp(tbody, label, value) {
2245 var tr = tbody.append('tr');
2246
2247 tr.append('td')
2248 .attr('class', 'label')
2249 .text(label + ' :');
2250
2251 tr.append('td')
2252 .attr('class', 'value')
2253 .text(value);
2254 }
2255
2256 function populateMultiSelect() {
2257 detailPane.empty();
2258
Simon Hunta3dd9572014-11-20 15:22:41 -08002259 var title = detailPane.append('h3'),
2260 table = detailPane.append('table'),
2261 tbody = table.append('tbody');
Simon Huntb53e0682014-11-12 13:32:01 -08002262
Thomas Vachuska4731f122014-11-20 04:56:19 -08002263 title.text('Selected Nodes');
Simon Huntb53e0682014-11-12 13:32:01 -08002264
2265 selectOrder.forEach(function (d, i) {
2266 addProp(tbody, i+1, d);
2267 });
Simon Huntd72bc702014-11-13 18:38:04 -08002268
2269 addMultiSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08002270 }
2271
Thomas Vachuska47635c62014-11-22 01:21:36 -08002272 // TODO: refactor to consolidate with populateDetails
2273 function populateSummary(data) {
2274 summaryPane.empty();
2275
2276 var svg = summaryPane.append('svg'),
2277 iid = iconGlyphUrl(data);
2278
2279 var title = summaryPane.append('h2'),
2280 table = summaryPane.append('table'),
2281 tbody = table.append('tbody');
2282
2283 appendGlyph(svg, 0, 0, 40, iid);
2284
2285 svg.append('use')
2286 .attr({
2287 class: 'birdBadge',
2288 transform: translate(8,12),
2289 'xlink:href': '#bird',
2290 width: 24,
2291 height: 24,
2292 fill: '#fff'
2293 });
2294
2295 title.text('ONOS Summary');
2296
2297 data.propOrder.forEach(function(p) {
2298 if (p === '-') {
2299 addSep(tbody);
2300 } else {
2301 addProp(tbody, p, data.props[p]);
2302 }
2303 });
2304 }
2305
Simon Hunt61d04042014-11-11 17:27:16 -08002306 function populateDetails(data) {
2307 detailPane.empty();
2308
Simon Hunta6a9fe72014-11-20 11:17:12 -08002309 var svg = detailPane.append('svg'),
2310 iid = iconGlyphUrl(data);
2311
Simon Hunta3dd9572014-11-20 15:22:41 -08002312 var title = detailPane.append('h2'),
2313 table = detailPane.append('table'),
2314 tbody = table.append('tbody');
Simon Hunt61d04042014-11-11 17:27:16 -08002315
Simon Hunta6a9fe72014-11-20 11:17:12 -08002316 appendGlyph(svg, 0, 0, 40, iid);
2317 title.text(data.id);
Simon Hunt61d04042014-11-11 17:27:16 -08002318
2319 data.propOrder.forEach(function(p) {
2320 if (p === '-') {
2321 addSep(tbody);
2322 } else {
2323 addProp(tbody, p, data.props[p]);
2324 }
2325 });
Simon Huntd72bc702014-11-13 18:38:04 -08002326
Thomas Vachuska4731f122014-11-20 04:56:19 -08002327 addSingleSelectActions(data);
Simon Hunt61d04042014-11-11 17:27:16 -08002328 }
2329
Thomas Vachuska4731f122014-11-20 04:56:19 -08002330 function addSingleSelectActions(data) {
Simon Huntd72bc702014-11-13 18:38:04 -08002331 detailPane.append('hr');
2332 // always want to allow 'show traffic'
Thomas Vachuska4731f122014-11-20 04:56:19 -08002333 addAction(detailPane, 'Show Related Traffic', showTrafficAction);
2334
2335 if (data.type === 'switch') {
2336 addAction(detailPane, 'Show Device Flows', showDeviceLinkFlowsAction);
2337 }
Simon Huntd72bc702014-11-13 18:38:04 -08002338 }
2339
2340 function addMultiSelectActions() {
2341 detailPane.append('hr');
2342 // always want to allow 'show traffic'
Thomas Vachuska4731f122014-11-20 04:56:19 -08002343 addAction(detailPane, 'Show Related Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08002344 // if exactly two hosts are selected, also want 'add host intent'
2345 if (nSel() === 2 && allSelectionsClass('host')) {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002346 addAction(detailPane, 'Create Host-to-Host Flow', addHostIntentAction);
2347 } else if (nSel() >= 2 && allSelectionsClass('host')) {
2348 addAction(detailPane, 'Create Multi-Source Flow', addMultiSourceIntentAction);
Simon Huntd72bc702014-11-13 18:38:04 -08002349 }
2350 }
2351
Simon Hunta5e89142014-11-14 07:00:33 -08002352 function addAction(panel, text, cb) {
2353 panel.append('div')
Simon Huntd72bc702014-11-13 18:38:04 -08002354 .classed('actionBtn', true)
2355 .text(text)
2356 .on('click', cb);
2357 }
2358
2359
Paul Greysonfcba0e82014-11-13 10:21:16 -08002360 function zoomPan(scale, translate) {
2361 zoomPanContainer.attr("transform", "translate(" + translate + ")scale(" + scale + ")");
2362 // keep the map lines constant width while zooming
Thomas Vachuska89543292014-11-19 11:28:33 -08002363 bgImg.style("stroke-width", 2.0 / scale + "px");
Paul Greysonfcba0e82014-11-13 10:21:16 -08002364 }
2365
2366 function resetZoomPan() {
2367 zoomPan(1, [0,0]);
2368 zoom.scale(1).translate([0,0]);
2369 }
2370
2371 function setupZoomPan() {
2372 function zoomed() {
Simon Huntdeab4322014-11-13 18:49:07 -08002373 if (!panZoom() ^ !d3.event.sourceEvent.metaKey) {
Paul Greysonfcba0e82014-11-13 10:21:16 -08002374 zoomPan(d3.event.scale, d3.event.translate);
2375 }
2376 }
2377
2378 zoom = d3.behavior.zoom()
2379 .translate([0, 0])
2380 .scale(1)
2381 .scaleExtent([1, 8])
2382 .on("zoom", zoomed);
2383
2384 svg.call(zoom);
2385 }
2386
Simon Hunt61d04042014-11-11 17:27:16 -08002387 // ==============================
2388 // Test harness code
Simon Hunt56d51852014-11-09 13:03:35 -08002389
2390 function prepareScenario(view, ctx, dbg) {
2391 var sc = scenario,
2392 urlSc = sc.evDir + ctx + sc.evScenario;
2393
2394 if (!ctx) {
2395 view.alert("No scenario specified (null ctx)");
2396 return;
2397 }
2398
2399 sc.view = view;
2400 sc.ctx = ctx;
2401 sc.debug = dbg;
2402 sc.evNumber = 0;
2403
2404 d3.json(urlSc, function(err, data) {
Simon Huntbb282f52014-11-10 11:08:19 -08002405 var p = data && data.params || {},
2406 desc = data && data.description || null,
Simon Huntfc274c92014-11-11 11:05:46 -08002407 intro = data && data.title;
Simon Huntbb282f52014-11-10 11:08:19 -08002408
Simon Hunt56d51852014-11-09 13:03:35 -08002409 if (err) {
2410 view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err);
2411 } else {
2412 sc.params = p;
Simon Huntbb282f52014-11-10 11:08:19 -08002413 if (desc) {
2414 intro += '\n\n ' + desc.join('\n ');
2415 }
2416 view.alert(intro);
Simon Hunt56d51852014-11-09 13:03:35 -08002417 }
2418 });
2419
2420 }
2421
Simon Hunt01095ff2014-11-13 16:37:29 -08002422 // ==============================
2423 // Toggle Buttons in masthead
Simon Hunt0c6d4192014-11-12 12:07:10 -08002424
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002425 // TODO: toggle button (and other widgets in the masthead) should be provided
2426 // by the framework; not generated by the view.
2427
Thomas Vachuska47635c62014-11-22 01:21:36 -08002428 //var showInstances;
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002429
Simon Hunt87514342014-11-24 16:41:27 -08002430/*
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002431 function addButtonBar(view) {
2432 var bb = d3.select('#mast')
2433 .append('span').classed('right', true).attr('id', 'bb');
2434
Simon Hunta5e89142014-11-14 07:00:33 -08002435 function mkTogBtn(text, cb) {
2436 return bb.append('span')
2437 .classed('btn', true)
2438 .text(text)
2439 .on('click', cb);
2440 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002441
Thomas Vachuska47635c62014-11-22 01:21:36 -08002442 //showInstances = mkTogBtn('Show Instances', toggleInst);
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002443 }
Simon Hunt87514342014-11-24 16:41:27 -08002444*/
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002445
Simon Huntdeab4322014-11-13 18:49:07 -08002446 function panZoom() {
Simon Hunte5b71752014-11-18 20:06:07 -08002447 return false;
Simon Hunta5e89142014-11-14 07:00:33 -08002448 }
2449
Simon Hunt7fa116d2014-11-17 14:16:55 -08002450 function loadGlyphs(svg) {
2451 var defs = svg.append('defs');
2452 gly.defBird(defs);
Simon Huntc72967b2014-11-20 09:21:42 -08002453 gly.defGlyphs(defs);
Simon Huntb82f6902014-11-22 11:53:15 -08002454 gly.defBadges(defs);
Simon Hunt7fa116d2014-11-17 14:16:55 -08002455 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002456
Simon Hunt395a70c2014-11-22 23:17:40 -08002457 function sendUpdateMeta(d, store) {
2458 var metaUi = {},
2459 ll;
2460
2461 if (store) {
2462 ll = geoMapProjection.invert([d.x, d.y]);
Simon Hunt62c47542014-11-22 22:16:32 -08002463 metaUi = {
2464 x: d.x,
2465 y: d.y,
2466 lng: ll[0],
2467 lat: ll[1]
2468 };
Simon Hunt395a70c2014-11-22 23:17:40 -08002469 }
Simon Hunt62c47542014-11-22 22:16:32 -08002470 d.metaUi = metaUi;
2471 sendMessage('updateMeta', {
2472 id: d.id,
2473 'class': d.class,
2474 'memento': metaUi
2475 });
2476 }
2477
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002478 // ==============================
Simon Hunt142d0032014-11-04 20:13:09 -08002479 // View life-cycle callbacks
Simon Hunt195cb382014-11-03 17:50:51 -08002480
Simon Huntf67722a2014-11-10 09:32:06 -08002481 function preload(view, ctx, flags) {
Simon Hunt142d0032014-11-04 20:13:09 -08002482 var w = view.width(),
2483 h = view.height(),
Simon Huntc7ee0662014-11-05 16:44:37 -08002484 fcfg = config.force,
2485 fpad = fcfg.pad,
2486 forceDim = [w - 2*fpad, h - 2*fpad];
Simon Hunt195cb382014-11-03 17:50:51 -08002487
Simon Hunt142d0032014-11-04 20:13:09 -08002488 // NOTE: view.$div is a D3 selection of the view's div
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002489 var viewBox = '0 0 ' + config.logicalSize + ' ' + config.logicalSize;
2490 svg = view.$div.append('svg').attr('viewBox', viewBox);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002491 setSize(svg, view);
2492
Simon Hunt7fa116d2014-11-17 14:16:55 -08002493 loadGlyphs(svg);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002494
Paul Greysonfcba0e82014-11-13 10:21:16 -08002495 zoomPanContainer = svg.append('g').attr('id', 'zoomPanContainer');
Paul Greysonfcba0e82014-11-13 10:21:16 -08002496 setupZoomPan();
2497
Simon Hunt1a9eff92014-11-07 11:06:34 -08002498 // add blue glow filter to svg layer
Paul Greysonfcba0e82014-11-13 10:21:16 -08002499 d3u.appendGlow(zoomPanContainer);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002500
Simon Huntc7ee0662014-11-05 16:44:37 -08002501 // group for the topology
Paul Greysonfcba0e82014-11-13 10:21:16 -08002502 topoG = zoomPanContainer.append('g')
Simon Huntd3b7d512014-11-12 15:48:41 -08002503 .attr('id', 'topo-G')
Simon Huntc7ee0662014-11-05 16:44:37 -08002504 .attr('transform', fcfg.translate());
2505
Simon Hunte2575b62014-11-18 15:25:53 -08002506 // subgroups for links, link labels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002507 linkG = topoG.append('g').attr('id', 'links');
Simon Hunte2575b62014-11-18 15:25:53 -08002508 linkLabelG = topoG.append('g').attr('id', 'linkLabels');
Simon Huntc7ee0662014-11-05 16:44:37 -08002509 nodeG = topoG.append('g').attr('id', 'nodes');
2510
Simon Hunte2575b62014-11-18 15:25:53 -08002511 // selection of links, linkLabels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002512 link = linkG.selectAll('.link');
Simon Hunte2575b62014-11-18 15:25:53 -08002513 linkLabel = linkLabelG.selectAll('.linkLabel');
Simon Huntc7ee0662014-11-05 16:44:37 -08002514 node = nodeG.selectAll('.node');
2515
Simon Hunt7cd48f32014-11-09 23:42:50 -08002516 function chrg(d) {
2517 return fcfg.charge[d.class] || -12000;
2518 }
Simon Hunt99c13842014-11-06 18:23:12 -08002519 function ldist(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002520 return fcfg.linkDistance[d.type] || 50;
Simon Hunt99c13842014-11-06 18:23:12 -08002521 }
2522 function lstrg(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002523 // 0.0 - 1.0
2524 return fcfg.linkStrength[d.type] || 1.0;
Simon Hunt99c13842014-11-06 18:23:12 -08002525 }
2526
Simon Hunt1a9eff92014-11-07 11:06:34 -08002527 function selectCb(d, self) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002528 selectObject(d, self);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002529 }
2530
2531 function atDragEnd(d, self) {
Simon Hunt56d51852014-11-09 13:03:35 -08002532 // once we've finished moving, pin the node in position
2533 d.fixed = true;
2534 d3.select(self).classed('fixed', true);
2535 if (config.useLiveData) {
Simon Hunt395a70c2014-11-22 23:17:40 -08002536 sendUpdateMeta(d, true);
Simon Hunta255a2c2014-11-13 22:29:35 -08002537 } else {
2538 console.log('Moving node ' + d.id + ' to [' + d.x + ',' + d.y + ']');
Simon Hunt1a9eff92014-11-07 11:06:34 -08002539 }
2540 }
2541
Simon Huntc7ee0662014-11-05 16:44:37 -08002542 // set up the force layout
2543 network.force = d3.layout.force()
2544 .size(forceDim)
2545 .nodes(network.nodes)
2546 .links(network.links)
Simon Hunt7cd48f32014-11-09 23:42:50 -08002547 .gravity(0.4)
2548 .friction(0.7)
2549 .charge(chrg)
Simon Hunt99c13842014-11-06 18:23:12 -08002550 .linkDistance(ldist)
2551 .linkStrength(lstrg)
Simon Huntc7ee0662014-11-05 16:44:37 -08002552 .on('tick', tick);
Simon Hunt195cb382014-11-03 17:50:51 -08002553
Simon Hunt01095ff2014-11-13 16:37:29 -08002554 network.drag = d3u.createDragBehavior(network.force,
Simon Huntdeab4322014-11-13 18:49:07 -08002555 selectCb, atDragEnd, panZoom);
Simon Hunt0c6d4192014-11-12 12:07:10 -08002556
2557 // create mask layer for when we lose connection to server.
Simon Hunta5e89142014-11-14 07:00:33 -08002558 // TODO: this should be part of the framework
Simon Hunt0c6d4192014-11-12 12:07:10 -08002559 mask = view.$div.append('div').attr('id','topo-mask');
2560 para(mask, 'Oops!');
2561 para(mask, 'Web-socket connection to server closed...');
2562 para(mask, 'Try refreshing the page.');
Simon Hunt12ce12e2014-11-15 21:13:19 -08002563
2564 mask.append('svg')
2565 .attr({
2566 id: 'mask-bird',
2567 width: w,
2568 height: h
2569 })
2570 .append('g')
2571 .attr('transform', birdTranslate(w, h))
2572 .style('opacity', 0.3)
2573 .append('use')
2574 .attr({
2575 'xlink:href': '#bird',
2576 width: config.birdDim,
2577 height: config.birdDim,
2578 fill: '#111'
Thomas Vachuska89543292014-11-19 11:28:33 -08002579 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08002580 }
Simon Hunt195cb382014-11-03 17:50:51 -08002581
Simon Hunt01095ff2014-11-13 16:37:29 -08002582 function para(sel, text) {
2583 sel.append('p').text(text);
2584 }
2585
2586
Simon Hunt56d51852014-11-09 13:03:35 -08002587 function load(view, ctx, flags) {
Simon Huntf67722a2014-11-10 09:32:06 -08002588 // resize, in case the window was resized while we were not loaded
2589 resize(view, ctx, flags);
2590
Simon Hunt99c13842014-11-06 18:23:12 -08002591 // cache the view token, so network topo functions can access it
2592 network.view = view;
Simon Hunt56d51852014-11-09 13:03:35 -08002593 config.useLiveData = !flags.local;
2594
2595 if (!config.useLiveData) {
2596 prepareScenario(view, ctx, flags.debug);
2597 }
Simon Hunt99c13842014-11-06 18:23:12 -08002598
2599 // set our radio buttons and key bindings
Simon Hunt9462e8c2014-11-14 17:28:09 -08002600 layerBtnSet = view.setRadio(layerButtons);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002601 view.setKeys(keyDispatch);
Simon Hunt87514342014-11-24 16:41:27 -08002602 view.setGestures(gestures);
Simon Hunt195cb382014-11-03 17:50:51 -08002603
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002604 // patch in our "button bar" for now
2605 // TODO: implement a more official frameworky way of doing this..
Simon Hunt87514342014-11-24 16:41:27 -08002606 //addButtonBar(view);
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002607
Simon Huntd3b7d512014-11-12 15:48:41 -08002608 // Load map data asynchronously; complete startup after that..
2609 loadGeoJsonData();
Simon Hunta255a2c2014-11-13 22:29:35 -08002610 }
2611
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002612 function startAntTimer() {
Thomas Vachuska4731f122014-11-20 04:56:19 -08002613 if (!antTimer) {
2614 var pulses = [5, 3, 1.2, 3],
2615 pulse = 0;
2616 antTimer = setInterval(function () {
2617 pulse = pulse + 1;
2618 pulse = pulse === pulses.length ? 0 : pulse;
2619 d3.selectAll('.animated').style('stroke-width', pulses[pulse]);
2620 }, 200);
2621 }
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002622 }
2623
2624 function stopAntTimer() {
Simon Hunta255a2c2014-11-13 22:29:35 -08002625 if (antTimer) {
2626 clearInterval(antTimer);
2627 antTimer = null;
2628 }
Simon Huntd3b7d512014-11-12 15:48:41 -08002629 }
2630
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002631 function unload(view, ctx, flags) {
2632 stopAntTimer();
2633 }
2634
Simon Huntd3b7d512014-11-12 15:48:41 -08002635 // TODO: move these to config/state portion of script
Simon Hunta6a9fe72014-11-20 11:17:12 -08002636 var geoJsonUrl = 'json/map/continental_us.json',
Simon Huntd3b7d512014-11-12 15:48:41 -08002637 geoJson;
2638
2639 function loadGeoJsonData() {
2640 d3.json(geoJsonUrl, function (err, data) {
2641 if (err) {
2642 // fall back to USA map background
2643 loadStaticMap();
2644 } else {
2645 geoJson = data;
2646 loadGeoMap();
2647 }
2648
2649 // finally, connect to the server...
2650 if (config.useLiveData) {
2651 webSock.connect();
2652 }
2653 });
2654 }
2655
2656 function showBg() {
Simon Hunt434cf142014-11-24 11:10:28 -08002657 return visVal(config.options.showBackground);
Simon Huntd3b7d512014-11-12 15:48:41 -08002658 }
2659
2660 function loadStaticMap() {
2661 fnTrace('loadStaticMap', config.backgroundUrl);
2662 var w = network.view.width(),
2663 h = network.view.height();
2664
2665 // load the background image
2666 bgImg = svg.insert('svg:image', '#topo-G')
2667 .attr({
2668 id: 'topo-bg',
2669 width: w,
2670 height: h,
2671 'xlink:href': config.backgroundUrl
2672 })
2673 .style({
2674 visibility: showBg()
2675 });
2676 }
2677
2678 function loadGeoMap() {
2679 fnTrace('loadGeoMap', geoJsonUrl);
Simon Huntd3b7d512014-11-12 15:48:41 -08002680
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002681 // extracts the topojson data into geocoordinate-based geometry
2682 var topoData = topojson.feature(geoJson, geoJson.objects.states);
Simon Huntd3b7d512014-11-12 15:48:41 -08002683
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002684 // see: http://bl.ocks.org/mbostock/4707858
2685 geoMapProjection = d3.geo.mercator();
2686 var path = d3.geo.path().projection(geoMapProjection);
Simon Huntd3b7d512014-11-12 15:48:41 -08002687
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002688 geoMapProjection
2689 .scale(1)
2690 .translate([0, 0]);
Simon Huntd3b7d512014-11-12 15:48:41 -08002691
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002692 // [[x1,y1],[x2,y2]]
2693 var b = path.bounds(topoData);
Paul Greysonfcba0e82014-11-13 10:21:16 -08002694 // size map to 95% of minimum dimension to fill space
2695 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 -08002696 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 -08002697
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002698 geoMapProjection
2699 .scale(s)
2700 .translate(t);
2701
Paul Greysonfcba0e82014-11-13 10:21:16 -08002702 bgImg = zoomPanContainer.insert("g", '#topo-G');
Thomas Vachuska89543292014-11-19 11:28:33 -08002703 bgImg.attr('id', 'map').selectAll('path')
2704 .data(topoData.features)
2705 .enter()
2706 .append('path')
2707 .attr('d', path);
Simon Hunt195cb382014-11-03 17:50:51 -08002708 }
2709
Simon Huntf67722a2014-11-10 09:32:06 -08002710 function resize(view, ctx, flags) {
Simon Hunt12ce12e2014-11-15 21:13:19 -08002711 var w = view.width(),
2712 h = view.height();
2713
Simon Hunt934c3ce2014-11-05 11:45:07 -08002714 setSize(svg, view);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002715
2716 d3.select('#mask-bird').attr({ width: w, height: h})
2717 .select('g').attr('transform', birdTranslate(w, h));
Simon Hunt142d0032014-11-04 20:13:09 -08002718 }
2719
Simon Hunt8f40cce2014-11-23 15:57:30 -08002720 function theme(view, ctx, flags) {
2721 updateInstances();
Simon Huntb0ecfa52014-11-23 21:05:12 -08002722 updateDeviceColors();
Simon Hunt8f40cce2014-11-23 15:57:30 -08002723 }
2724
Simon Hunt12ce12e2014-11-15 21:13:19 -08002725 function birdTranslate(w, h) {
2726 var bdim = config.birdDim;
2727 return 'translate('+((w-bdim)*.4)+','+((h-bdim)*.1)+')';
2728 }
Simon Hunt142d0032014-11-04 20:13:09 -08002729
Simon Hunt06811b72014-11-25 18:54:48 -08002730 function isF(f) { return $.isFunction(f) ? f : null; }
2731 function noop() {}
2732
2733 function augmentDetailPane() {
2734 var dp = detailPane;
2735 dp.ypos = { up: 64, down: 320, current: 320};
2736
2737 dp._move = function (y, cb) {
2738 var endCb = isF(cb) || noop,
2739 yp = dp.ypos;
2740 if (yp.current !== y) {
2741 yp.current = y;
2742 dp.el.transition().duration(300)
2743 .each('end', endCb)
2744 .style('top', yp.current + 'px');
2745 } else {
2746 endCb();
2747 }
2748 };
2749
2750 dp.down = function (cb) { dp._move(dp.ypos.down, cb); };
2751 dp.up = function (cb) { dp._move(dp.ypos.up, cb); };
2752 }
2753
Simon Hunt142d0032014-11-04 20:13:09 -08002754 // ==============================
2755 // View registration
Simon Hunt195cb382014-11-03 17:50:51 -08002756
Simon Hunt25248912014-11-04 11:25:48 -08002757 onos.ui.addView('topo', {
Simon Hunt142d0032014-11-04 20:13:09 -08002758 preload: preload,
2759 load: load,
Simon Hunta255a2c2014-11-13 22:29:35 -08002760 unload: unload,
Simon Hunt8f40cce2014-11-23 15:57:30 -08002761 resize: resize,
2762 theme: theme
Simon Hunt195cb382014-11-03 17:50:51 -08002763 });
2764
Thomas Vachuska47635c62014-11-22 01:21:36 -08002765 summaryPane = onos.ui.addFloatingPanel('topo-summary');
Simon Hunt61d04042014-11-11 17:27:16 -08002766 detailPane = onos.ui.addFloatingPanel('topo-detail');
Simon Hunt06811b72014-11-25 18:54:48 -08002767 augmentDetailPane();
Simon Hunta5e89142014-11-14 07:00:33 -08002768 oiBox = onos.ui.addFloatingPanel('topo-oibox', 'TL');
Simon Huntb82f6902014-11-22 11:53:15 -08002769 oiBox.width(20);
Simon Hunt61d04042014-11-11 17:27:16 -08002770
Simon Hunt195cb382014-11-03 17:50:51 -08002771}(ONOS));