blob: e233f8c78e702ece6814c3a0b9aa5df96d8175cc [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 Hunt27d322d2014-11-28 10:45:43 -0800145 D: [toggleDetails, 'Disable / enable details pane'],
Simon Hunt988c6fc2014-11-20 17:43:03 -0800146 B: [toggleBg, 'Toggle background image'],
Simon Hunt434cf142014-11-24 11:10:28 -0800147 H: [toggleHosts, 'Toggle host visibility'],
Simon Hunt27d322d2014-11-28 10:45:43 -0800148 L: [cycleLabels, 'Cycle device labels'],
Simon Hunt934c3ce2014-11-05 11:45:07 -0800149 P: togglePorts,
Simon Hunt87514342014-11-24 16:41:27 -0800150 U: [unpin, 'Unpin node (hover mouse over)'],
151 R: [resetZoomPan, 'Reset zoom / pan'],
Thomas Vachuska9edca302014-11-22 17:06:42 -0800152 V: [showTrafficAction, 'Show related traffic'],
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800153 A: [showAllTrafficAction, 'Show all traffic'],
154 F: [showDeviceLinkFlowsAction, 'Show device link flows'],
Simon Hunt9462e8c2014-11-14 17:28:09 -0800155 esc: handleEscape
Simon Hunt934c3ce2014-11-05 11:45:07 -0800156 };
Simon Hunt142d0032014-11-04 20:13:09 -0800157
Simon Hunt87514342014-11-24 16:41:27 -0800158 // mouse gestures
159 var gestures = [
160 ['click', 'Select the item and show details'],
161 ['shift-click', 'Toggle selection state'],
162 ['drag', 'Reposition (and pin) device / host'],
163 ['cmd-scroll', 'Zoom in / out'],
164 ['cmd-drag', 'Pan']
165 ];
166
Simon Hunt195cb382014-11-03 17:50:51 -0800167 // state variables
Simon Hunt99c13842014-11-06 18:23:12 -0800168 var network = {
Simon Hunt50128c02014-11-08 13:36:15 -0800169 view: null, // view token reference
Simon Hunt99c13842014-11-06 18:23:12 -0800170 nodes: [],
171 links: [],
Simon Hunt269670f2014-11-17 16:17:43 -0800172 lookup: {},
173 revLinkToKey: {}
Simon Hunt99c13842014-11-06 18:23:12 -0800174 },
Simon Hunt56d51852014-11-09 13:03:35 -0800175 scenario = {
176 evDir: 'json/ev/',
177 evScenario: '/scenario.json',
178 evPrefix: '/ev_',
179 evOnos: '_onos.json',
180 evUi: '_ui.json',
181 ctx: null,
182 params: {},
183 evNumber: 0,
Simon Hunt434cf142014-11-24 11:10:28 -0800184 view: null
Simon Hunt56d51852014-11-09 13:03:35 -0800185 },
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800186 webSock,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800187 sid = 0,
Thomas Vachuska60d72bf2014-11-21 13:02:00 -0800188 deviceLabelCount = 3,
Simon Hunt209155e2014-11-21 12:16:09 -0800189 hostLabelCount = 2,
Simon Hunt56d51852014-11-09 13:03:35 -0800190 deviceLabelIndex = 0,
191 hostLabelIndex = 0,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800192 selections = {},
Simon Hunta5e89142014-11-14 07:00:33 -0800193 selectOrder = [],
Simon Hunt6ac93f32014-11-13 12:17:27 -0800194 hovered = null,
Thomas Vachuska47635c62014-11-22 01:21:36 -0800195 summaryPane,
Simon Hunta5e89142014-11-14 07:00:33 -0800196 detailPane,
Simon Hunta255a2c2014-11-13 22:29:35 -0800197 antTimer = null,
Simon Hunta5e89142014-11-14 07:00:33 -0800198 onosInstances = {},
199 onosOrder = [],
200 oiBox,
Simon Hunt9462e8c2014-11-14 17:28:09 -0800201 oiShowMaster = false,
Simon Hunt8f40cce2014-11-23 15:57:30 -0800202 portLabelsOn = false,
Simon Huntb0ecfa52014-11-23 21:05:12 -0800203 cat7 = d3u.cat7(),
Simon Hunt434cf142014-11-24 11:10:28 -0800204 colorAffinity = false,
Simon Hunt27d322d2014-11-28 10:45:43 -0800205 showHosts = false,
206 useDetails = true,
207 haveDetails = false;
Simon Hunt195cb382014-11-03 17:50:51 -0800208
Simon Hunt434cf142014-11-24 11:10:28 -0800209 // constants
Thomas Vachuska9edca302014-11-22 17:06:42 -0800210 var hoverModeAll = 1,
211 hoverModeFlows = 2,
212 hoverModeIntents = 3,
213 hoverMode = hoverModeFlows;
214
Simon Hunt934c3ce2014-11-05 11:45:07 -0800215 // D3 selections
216 var svg,
Paul Greysonfcba0e82014-11-13 10:21:16 -0800217 zoomPanContainer,
Simon Hunt934c3ce2014-11-05 11:45:07 -0800218 bgImg,
Simon Huntc7ee0662014-11-05 16:44:37 -0800219 topoG,
220 nodeG,
221 linkG,
Simon Hunte2575b62014-11-18 15:25:53 -0800222 linkLabelG,
Simon Huntc7ee0662014-11-05 16:44:37 -0800223 node,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800224 link,
Simon Hunte2575b62014-11-18 15:25:53 -0800225 linkLabel,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800226 mask;
Simon Hunt195cb382014-11-03 17:50:51 -0800227
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800228 // the projection for the map background
229 var geoMapProjection;
230
Paul Greysonfcba0e82014-11-13 10:21:16 -0800231 // the zoom function
232 var zoom;
233
Simon Hunt142d0032014-11-04 20:13:09 -0800234 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800235 // For Debugging / Development
Simon Hunt195cb382014-11-03 17:50:51 -0800236
Simon Hunt99c13842014-11-06 18:23:12 -0800237 function note(label, msg) {
238 console.log('NOTE: ' + label + ': ' + msg);
Simon Hunt195cb382014-11-03 17:50:51 -0800239 }
240
Simon Hunt99c13842014-11-06 18:23:12 -0800241 function debug(what) {
242 return config.debugOn && config.debug[what];
Simon Hunt934c3ce2014-11-05 11:45:07 -0800243 }
244
Simon Huntfc274c92014-11-11 11:05:46 -0800245 function fnTrace(msg, id) {
246 if (config.fnTrace) {
247 console.log('FN: ' + msg + ' [' + id + ']');
248 }
249 }
Simon Hunt99c13842014-11-06 18:23:12 -0800250
Simon Hunta5e89142014-11-14 07:00:33 -0800251 function evTrace(data) {
252 fnTrace(data.event, data.payload.id);
253 }
254
Simon Hunt934c3ce2014-11-05 11:45:07 -0800255 // ==============================
256 // Key Callbacks
257
Simon Hunt27d322d2014-11-28 10:45:43 -0800258 function flash(txt) {
259 network.view.flash(txt);
260 }
261
Simon Hunt99c13842014-11-06 18:23:12 -0800262 function testMe(view) {
Simon Hunt8f40cce2014-11-23 15:57:30 -0800263 //view.alert('Theme is ' + view.getTheme());
Simon Hunta3dd9572014-11-20 15:22:41 -0800264 //view.flash('This is some text');
Simon Hunt8f40cce2014-11-23 15:57:30 -0800265 cat7.testCard(svg);
Simon Hunt99c13842014-11-06 18:23:12 -0800266 }
267
Simon Hunt56d51852014-11-09 13:03:35 -0800268 function injectTestEvent(view) {
Simon Hunt434cf142014-11-24 11:10:28 -0800269 if (config.useLiveData) { return; }
270
Simon Hunt56d51852014-11-09 13:03:35 -0800271 var sc = scenario,
272 evn = ++sc.evNumber,
273 pfx = sc.evDir + sc.ctx + sc.evPrefix + evn,
274 onosUrl = pfx + sc.evOnos,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800275 uiUrl = pfx + sc.evUi,
276 stack = [
277 { url: onosUrl, cb: handleServerEvent },
278 { url: uiUrl, cb: handleUiEvent }
279 ];
280 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800281 }
282
Simon Hunt7cd48f32014-11-09 23:42:50 -0800283 function recurseFetchEvent(stack, evn) {
284 var v = scenario.view,
285 frame;
286 if (stack.length === 0) {
Simon Huntfc274c92014-11-11 11:05:46 -0800287 v.alert('Oops!\n\nNo event #' + evn + ' found.');
Simon Hunt7cd48f32014-11-09 23:42:50 -0800288 return;
289 }
290 frame = stack.shift();
291
292 d3.json(frame.url, function (err, data) {
Simon Hunt99c13842014-11-06 18:23:12 -0800293 if (err) {
Simon Hunt56d51852014-11-09 13:03:35 -0800294 if (err.status === 404) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800295 // if we didn't find the data, try the next stack frame
296 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800297 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800298 v.alert('non-404 error:\n\n' + frame.url + '\n\n' + err);
Simon Hunt56d51852014-11-09 13:03:35 -0800299 }
Simon Hunt99c13842014-11-06 18:23:12 -0800300 } else {
Simon Hunt1712ed82014-11-17 12:56:00 -0800301 wsTrace('test', JSON.stringify(data));
Simon Hunt7cd48f32014-11-09 23:42:50 -0800302 frame.cb(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800303 }
304 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800305
Simon Hunt56d51852014-11-09 13:03:35 -0800306 }
Simon Hunt50128c02014-11-08 13:36:15 -0800307
Simon Hunt56d51852014-11-09 13:03:35 -0800308 function handleUiEvent(data) {
Simon Huntbb282f52014-11-10 11:08:19 -0800309 scenario.view.alert('UI Tx: ' + data.event + '\n\n' +
310 JSON.stringify(data));
Simon Hunt56d51852014-11-09 13:03:35 -0800311 }
312
313 function injectStartupEvents(view) {
314 var last = scenario.params.lastAuto || 0;
Simon Hunt434cf142014-11-24 11:10:28 -0800315 if (config.useLiveData) { return; }
Simon Hunt56d51852014-11-09 13:03:35 -0800316
317 while (scenario.evNumber < last) {
Simon Hunt1a9eff92014-11-07 11:06:34 -0800318 injectTestEvent(view);
319 }
320 }
321
Simon Hunt934c3ce2014-11-05 11:45:07 -0800322 function toggleBg() {
323 var vis = bgImg.style('visibility');
Simon Hunt434cf142014-11-24 11:10:28 -0800324 bgImg.style('visibility', visVal(vis === 'hidden'));
325 }
326
327 function toggleHosts() {
328 showHosts = !showHosts;
329 updateHostVisibility();
Simon Hunt27d322d2014-11-28 10:45:43 -0800330 flash('Hosts ' + visVal(showHosts));
Simon Hunt934c3ce2014-11-05 11:45:07 -0800331 }
332
Simon Hunt99c13842014-11-06 18:23:12 -0800333 function cycleLabels() {
Thomas Vachuska60d72bf2014-11-21 13:02:00 -0800334 deviceLabelIndex = (deviceLabelIndex === 2)
Simon Huntbb282f52014-11-10 11:08:19 -0800335 ? 0 : deviceLabelIndex + 1;
Simon Hunt5f36d342014-11-08 21:33:14 -0800336
Simon Hunt99c13842014-11-06 18:23:12 -0800337 network.nodes.forEach(function (d) {
Simon Huntbb282f52014-11-10 11:08:19 -0800338 if (d.class === 'device') {
339 updateDeviceLabel(d);
340 }
Simon Hunt99c13842014-11-06 18:23:12 -0800341 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800342 }
343
344 function togglePorts(view) {
Simon Hunt434cf142014-11-24 11:10:28 -0800345 //view.alert('togglePorts() callback')
Simon Hunt934c3ce2014-11-05 11:45:07 -0800346 }
347
Simon Hunt6ac93f32014-11-13 12:17:27 -0800348 function unpin() {
349 if (hovered) {
Simon Hunt395a70c2014-11-22 23:17:40 -0800350 sendUpdateMeta(hovered);
Simon Hunt6ac93f32014-11-13 12:17:27 -0800351 hovered.fixed = false;
352 hovered.el.classed('fixed', false);
353 network.force.resume();
354 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800355 }
356
Simon Hunt9462e8c2014-11-14 17:28:09 -0800357 function handleEscape(view) {
358 if (oiShowMaster) {
359 cancelAffinity();
Simon Hunt27d322d2014-11-28 10:45:43 -0800360 } else if (haveDetails) {
Simon Hunt9462e8c2014-11-14 17:28:09 -0800361 deselectAll();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800362 } else if (oiBox.isVisible()) {
Simon Huntb0ecfa52014-11-23 21:05:12 -0800363 hideInstances();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800364 } else if (summaryPane.isVisible()) {
365 cancelSummary();
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800366 stopAntTimer();
367 } else {
368 hoverMode = hoverModeFlows;
Simon Hunt9462e8c2014-11-14 17:28:09 -0800369 }
370 }
371
Simon Hunt934c3ce2014-11-05 11:45:07 -0800372 // ==============================
373 // Radio Button Callbacks
374
Simon Hunta5e89142014-11-14 07:00:33 -0800375 var layerLookup = {
376 host: {
Simon Hunt209155e2014-11-21 12:16:09 -0800377 endstation: 'pkt', // default, if host event does not define type
Thomas Vachuska89543292014-11-19 11:28:33 -0800378 router: 'pkt',
Simon Hunta5e89142014-11-14 07:00:33 -0800379 bgpSpeaker: 'pkt'
380 },
381 device: {
382 switch: 'pkt',
383 roadm: 'opt'
384 },
385 link: {
386 hostLink: 'pkt',
387 direct: 'pkt',
Simon Hunt8257f4c2014-11-16 19:34:54 -0800388 indirect: '',
389 tunnel: '',
Simon Hunta5e89142014-11-14 07:00:33 -0800390 optical: 'opt'
391 }
392 };
393
394 function inLayer(d, layer) {
Simon Hunt8257f4c2014-11-16 19:34:54 -0800395 var type = d.class === 'link' ? d.type() : d.type,
396 look = layerLookup[d.class],
397 lyr = look && look[type];
Simon Hunta5e89142014-11-14 07:00:33 -0800398 return lyr === layer;
399 }
400
401 function unsuppressLayer(which) {
402 node.each(function (d) {
403 var node = d.el;
404 if (inLayer(d, which)) {
405 node.classed('suppressed', false);
406 }
407 });
408
409 link.each(function (d) {
410 var link = d.el;
411 if (inLayer(d, which)) {
412 link.classed('suppressed', false);
413 }
414 });
415 }
416
Simon Hunt9462e8c2014-11-14 17:28:09 -0800417 function suppressLayers(b) {
418 node.classed('suppressed', b);
419 link.classed('suppressed', b);
Simon Hunt142d0032014-11-04 20:13:09 -0800420// d3.selectAll('svg .port').classed('inactive', false);
421// d3.selectAll('svg .portText').classed('inactive', false);
Simon Hunt195cb382014-11-03 17:50:51 -0800422 }
423
Simon Hunt9462e8c2014-11-14 17:28:09 -0800424 function showAllLayers() {
425 suppressLayers(false);
426 }
427
Simon Hunt195cb382014-11-03 17:50:51 -0800428 function showPacketLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800429 node.classed('suppressed', true);
430 link.classed('suppressed', true);
431 unsuppressLayer('pkt');
Simon Hunt195cb382014-11-03 17:50:51 -0800432 }
433
434 function showOpticalLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800435 node.classed('suppressed', true);
436 link.classed('suppressed', true);
437 unsuppressLayer('opt');
Simon Hunt195cb382014-11-03 17:50:51 -0800438 }
439
Simon Hunt9462e8c2014-11-14 17:28:09 -0800440 function restoreLayerState() {
441 layerBtnDispatch[layerBtnSet.selected()]();
442 }
443
Simon Hunt142d0032014-11-04 20:13:09 -0800444 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800445 // Private functions
446
Simon Hunt99c13842014-11-06 18:23:12 -0800447 function safeId(s) {
448 return s.replace(/[^a-z0-9]/gi, '-');
449 }
450
Simon Huntc7ee0662014-11-05 16:44:37 -0800451 // set the size of the given element to that of the view (reduced if padded)
452 function setSize(el, view, pad) {
453 var padding = pad ? pad * 2 : 0;
Simon Hunt934c3ce2014-11-05 11:45:07 -0800454 el.attr({
Simon Huntc7ee0662014-11-05 16:44:37 -0800455 width: view.width() - padding,
456 height: view.height() - padding
Simon Hunt934c3ce2014-11-05 11:45:07 -0800457 });
458 }
459
Simon Hunt8257f4c2014-11-16 19:34:54 -0800460 function makeNodeKey(d, what) {
461 var port = what + 'Port';
462 return d[what] + '/' + d[port];
463 }
464
465 function makeLinkKey(d, flipped) {
466 var one = flipped ? makeNodeKey(d, 'dst') : makeNodeKey(d, 'src'),
467 two = flipped ? makeNodeKey(d, 'src') : makeNodeKey(d, 'dst');
468 return one + '-' + two;
469 }
470
Simon Hunt269670f2014-11-17 16:17:43 -0800471 function findLinkById(id) {
472 // check to see if this is a reverse lookup, else default to given id
473 var key = network.revLinkToKey[id] || id;
474 return key && network.lookup[key];
475 }
476
Simon Hunt8257f4c2014-11-16 19:34:54 -0800477 function findLink(linkData, op) {
478 var key = makeLinkKey(linkData),
479 keyrev = makeLinkKey(linkData, 1),
480 link = network.lookup[key],
481 linkRev = network.lookup[keyrev],
482 result = {},
483 ldata = link || linkRev,
484 rawLink;
485
486 if (op === 'add') {
487 if (link) {
488 // trying to add a link that we already know about
489 result.ldata = link;
490 result.badLogic = 'addLink: link already added';
491
492 } else if (linkRev) {
493 // we found the reverse of the link to be added
494 result.ldata = linkRev;
495 if (linkRev.fromTarget) {
496 result.badLogic = 'addLink: link already added';
497 }
498 }
499 } else if (op === 'update') {
500 if (!ldata) {
501 result.badLogic = 'updateLink: link not found';
502 } else {
503 rawLink = link ? ldata.fromSource : ldata.fromTarget;
504 result.updateWith = function (data) {
505 $.extend(rawLink, data);
506 restyleLinkElement(ldata);
507 }
508 }
509 } else if (op === 'remove') {
510 if (!ldata) {
511 result.badLogic = 'removeLink: link not found';
512 } else {
513 rawLink = link ? ldata.fromSource : ldata.fromTarget;
514
515 if (!rawLink) {
516 result.badLogic = 'removeLink: link not found';
517
518 } else {
519 result.removeRawLink = function () {
520 if (link) {
521 // remove fromSource
522 ldata.fromSource = null;
523 if (ldata.fromTarget) {
524 // promote target into source position
525 ldata.fromSource = ldata.fromTarget;
526 ldata.fromTarget = null;
527 ldata.key = keyrev;
528 delete network.lookup[key];
529 network.lookup[keyrev] = ldata;
Simon Hunt269670f2014-11-17 16:17:43 -0800530 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800531 }
532 } else {
533 // remove fromTarget
534 ldata.fromTarget = null;
Simon Hunt269670f2014-11-17 16:17:43 -0800535 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800536 }
537 if (ldata.fromSource) {
538 restyleLinkElement(ldata);
539 } else {
540 removeLinkElement(ldata);
541 }
542 }
543 }
544 }
545 }
546 return result;
547 }
548
549 function addLinkUpdate(ldata, link) {
550 // add link event, but we already have the reverse link installed
551 ldata.fromTarget = link;
Simon Hunt269670f2014-11-17 16:17:43 -0800552 network.revLinkToKey[link.id] = ldata.key;
Simon Hunt8257f4c2014-11-16 19:34:54 -0800553 restyleLinkElement(ldata);
554 }
555
556 var allLinkTypes = 'direct indirect optical tunnel',
557 defaultLinkType = 'direct';
558
559 function restyleLinkElement(ldata) {
560 // this fn's job is to look at raw links and decide what svg classes
561 // need to be applied to the line element in the DOM
562 var el = ldata.el,
563 type = ldata.type(),
564 lw = ldata.linkWidth(),
565 online = ldata.online();
566
567 el.classed('link', true);
568 el.classed('inactive', !online);
569 el.classed(allLinkTypes, false);
570 if (type) {
571 el.classed(type, true);
572 }
573 el.transition()
574 .duration(1000)
Thomas Vachuska89543292014-11-19 11:28:33 -0800575 .attr('stroke-width', linkScale(lw))
576 .attr('stroke', config.topo.linkBaseColor);
Simon Hunt8257f4c2014-11-16 19:34:54 -0800577 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800578
Simon Hunt99c13842014-11-06 18:23:12 -0800579 // ==============================
580 // Event handlers for server-pushed events
581
Simon Huntbb282f52014-11-10 11:08:19 -0800582 function logicError(msg) {
583 // TODO, report logic error to server, via websock, so it can be logged
Simon Huntfc274c92014-11-11 11:05:46 -0800584 console.warn(msg);
Simon Huntbb282f52014-11-10 11:08:19 -0800585 }
586
Simon Hunt99c13842014-11-06 18:23:12 -0800587 var eventDispatch = {
Simon Hunta5e89142014-11-14 07:00:33 -0800588 addInstance: addInstance,
Simon Hunt99c13842014-11-06 18:23:12 -0800589 addDevice: addDevice,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800590 addLink: addLink,
Simon Hunt56d51852014-11-09 13:03:35 -0800591 addHost: addHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800592
Simon Huntfcfb46c2014-11-19 12:53:38 -0800593 updateInstance: updateInstance,
Simon Huntbb282f52014-11-10 11:08:19 -0800594 updateDevice: updateDevice,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800595 updateLink: updateLink,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800596 updateHost: updateHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800597
Simon Hunt7b403bc2014-11-22 19:01:00 -0800598 removeInstance: removeInstance,
Simon Huntbb282f52014-11-10 11:08:19 -0800599 removeDevice: stillToImplement,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800600 removeLink: removeLink,
Simon Hunt44031102014-11-11 13:20:36 -0800601 removeHost: removeHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800602
Simon Hunt61d04042014-11-11 17:27:16 -0800603 showDetails: showDetails,
Thomas Vachuska47635c62014-11-22 01:21:36 -0800604 showSummary: showSummary,
Simon Huntb53e0682014-11-12 13:32:01 -0800605 showTraffic: showTraffic
Simon Hunt99c13842014-11-06 18:23:12 -0800606 };
607
Simon Hunta5e89142014-11-14 07:00:33 -0800608 function addInstance(data) {
609 evTrace(data);
610 var inst = data.payload,
611 id = inst.id;
612 if (onosInstances[id]) {
613 logicError('ONOS instance already added: ' + id);
614 return;
615 }
616 onosInstances[id] = inst;
617 onosOrder.push(inst);
618 updateInstances();
619 }
620
Simon Hunt99c13842014-11-06 18:23:12 -0800621 function addDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800622 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800623 var device = data.payload,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800624 nodeData = createDeviceNode(device);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800625 network.nodes.push(nodeData);
626 network.lookup[nodeData.id] = nodeData;
Simon Hunt99c13842014-11-06 18:23:12 -0800627 updateNodes();
628 network.force.start();
629 }
630
Simon Hunt99c13842014-11-06 18:23:12 -0800631 function addLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800632 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800633 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800634 result = findLink(link, 'add'),
635 bad = result.badLogic,
636 ldata = result.ldata;
637
638 if (bad) {
639 logicError(bad + ': ' + link.id);
640 return;
641 }
642
643 if (ldata) {
644 // we already have a backing store link for src/dst nodes
645 addLinkUpdate(ldata, link);
646 return;
647 }
648
649 // no backing store link yet
650 ldata = createLink(link);
651 if (ldata) {
652 network.links.push(ldata);
653 network.lookup[ldata.key] = ldata;
Simon Hunt99c13842014-11-06 18:23:12 -0800654 updateLinks();
655 network.force.start();
656 }
657 }
658
Simon Hunt56d51852014-11-09 13:03:35 -0800659 function addHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800660 evTrace(data);
Simon Hunt56d51852014-11-09 13:03:35 -0800661 var host = data.payload,
662 node = createHostNode(host),
663 lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800664 network.nodes.push(node);
665 network.lookup[host.id] = node;
666 updateNodes();
667
668 lnk = createHostLink(host);
669 if (lnk) {
Simon Hunt44031102014-11-11 13:20:36 -0800670 node.linkData = lnk; // cache ref on its host
Simon Hunt56d51852014-11-09 13:03:35 -0800671 network.links.push(lnk);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800672 network.lookup[host.ingress] = lnk;
673 network.lookup[host.egress] = lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800674 updateLinks();
675 }
676 network.force.start();
677 }
678
Simon Hunt44031102014-11-11 13:20:36 -0800679 // TODO: fold updateX(...) methods into one base method; remove duplication
Simon Hunt56a2ea42014-11-19 12:39:31 -0800680
681 function updateInstance(data) {
682 evTrace(data);
683 var inst = data.payload,
684 id = inst.id,
685 instData = onosInstances[id];
686 if (instData) {
687 $.extend(instData, inst);
688 updateInstances();
Simon Hunt56a2ea42014-11-19 12:39:31 -0800689 } else {
690 logicError('updateInstance lookup fail. ID = "' + id + '"');
691 }
692 }
693
Simon Huntbb282f52014-11-10 11:08:19 -0800694 function updateDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800695 evTrace(data);
Simon Huntbb282f52014-11-10 11:08:19 -0800696 var device = data.payload,
697 id = device.id,
Simon Hunt62c47542014-11-22 22:16:32 -0800698 d = network.lookup[id];
699 if (d) {
700 $.extend(d, device);
701 if (positionNode(d, true)) {
Simon Hunt395a70c2014-11-22 23:17:40 -0800702 sendUpdateMeta(d, true);
Simon Hunt62c47542014-11-22 22:16:32 -0800703 }
704 updateNodes();
Simon Huntbb282f52014-11-10 11:08:19 -0800705 } else {
706 logicError('updateDevice lookup fail. ID = "' + id + '"');
707 }
708 }
709
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800710 function updateLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800711 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800712 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800713 result = findLink(link, 'update'),
714 bad = result.badLogic;
715 if (bad) {
716 logicError(bad + ': ' + link.id);
717 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800718 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800719 result.updateWith(link);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800720 }
721
Simon Hunt7cd48f32014-11-09 23:42:50 -0800722 function updateHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800723 evTrace(data);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800724 var host = data.payload,
Simon Huntbb282f52014-11-10 11:08:19 -0800725 id = host.id,
726 hostData = network.lookup[id];
727 if (hostData) {
728 $.extend(hostData, host);
729 updateHostState(hostData);
730 } else {
731 logicError('updateHost lookup fail. ID = "' + id + '"');
732 }
Simon Hunt7cd48f32014-11-09 23:42:50 -0800733 }
734
Simon Hunt44031102014-11-11 13:20:36 -0800735 // TODO: fold removeX(...) methods into base method - remove dup code
Simon Hunt7b403bc2014-11-22 19:01:00 -0800736 function removeInstance(data) {
737 evTrace(data);
738 var inst = data.payload,
739 id = inst.id,
740 instData = onosInstances[id];
741 if (instData) {
742 var idx = find(id, onosOrder, 'id');
743 if (idx >= 0) {
744 onosOrder.splice(idx, 1);
745 }
746 delete onosInstances[id];
747 updateInstances();
748 } else {
749 logicError('updateInstance lookup fail. ID = "' + id + '"');
750 }
751 }
752
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800753 function removeLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800754 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800755 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800756 result = findLink(link, 'remove'),
757 bad = result.badLogic;
758 if (bad) {
759 logicError(bad + ': ' + link.id);
760 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800761 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800762 result.removeRawLink();
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800763 }
764
Simon Hunt44031102014-11-11 13:20:36 -0800765 function removeHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800766 evTrace(data);
Simon Hunt44031102014-11-11 13:20:36 -0800767 var host = data.payload,
768 id = host.id,
769 hostData = network.lookup[id];
770 if (hostData) {
771 removeHostElement(hostData);
772 } else {
773 logicError('removeHost lookup fail. ID = "' + id + '"');
774 }
775 }
776
Thomas Vachuska47635c62014-11-22 01:21:36 -0800777 function showSummary(data) {
778 evTrace(data);
779 populateSummary(data.payload);
Simon Hunt06811b72014-11-25 18:54:48 -0800780 showSummaryPane();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800781 }
782
Simon Hunt61d04042014-11-11 17:27:16 -0800783 function showDetails(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800784 evTrace(data);
Simon Hunt27d322d2014-11-28 10:45:43 -0800785 haveDetails = true;
Simon Hunt61d04042014-11-11 17:27:16 -0800786 populateDetails(data.payload);
Simon Hunt27d322d2014-11-28 10:45:43 -0800787 if (useDetails) {
788 showDetailPane();
789 }
Simon Hunt61d04042014-11-11 17:27:16 -0800790 }
791
Simon Huntb53e0682014-11-12 13:32:01 -0800792 function showTraffic(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800793 evTrace(data);
Thomas Vachuska4731f122014-11-20 04:56:19 -0800794 var paths = data.payload.paths,
795 hasTraffic = false;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800796
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800797 // Revert any links hilighted previously.
Thomas Vachuska4731f122014-11-20 04:56:19 -0800798 link.style('stroke-width', null)
Thomas Vachuskaa3148a72014-11-19 21:38:35 -0800799 .classed('primary secondary animated optical', false);
Simon Hunte2575b62014-11-18 15:25:53 -0800800 // Remove all previous labels.
801 removeLinkLabels();
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800802
Simon Hunte2575b62014-11-18 15:25:53 -0800803 // Now hilight all links in the paths payload, and attach
804 // labels to them, if they are defined.
Simon Hunta255a2c2014-11-13 22:29:35 -0800805 paths.forEach(function (p) {
Simon Hunte2575b62014-11-18 15:25:53 -0800806 var n = p.links.length,
807 i,
808 ldata;
809
Thomas Vachuska4731f122014-11-20 04:56:19 -0800810 hasTraffic = hasTraffic || p.traffic;
Simon Hunte2575b62014-11-18 15:25:53 -0800811 for (i=0; i<n; i++) {
812 ldata = findLinkById(p.links[i]);
Thomas Vachuska4731f122014-11-20 04:56:19 -0800813 if (ldata && ldata.el) {
Simon Hunte2575b62014-11-18 15:25:53 -0800814 ldata.el.classed(p.class, true);
815 ldata.label = p.labels[i];
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800816 }
Simon Hunte2575b62014-11-18 15:25:53 -0800817 }
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800818 });
Thomas Vachuska4731f122014-11-20 04:56:19 -0800819
Simon Hunte2575b62014-11-18 15:25:53 -0800820 updateLinks();
Thomas Vachuska4731f122014-11-20 04:56:19 -0800821
822 if (hasTraffic && !antTimer) {
823 startAntTimer();
824 } else if (!hasTraffic && antTimer) {
825 stopAntTimer();
826 }
Simon Huntb53e0682014-11-12 13:32:01 -0800827 }
828
Simon Hunt56d51852014-11-09 13:03:35 -0800829 // ...............................
830
831 function stillToImplement(data) {
832 var p = data.payload;
833 note(data.event, p.id);
Simon Hunt7b403bc2014-11-22 19:01:00 -0800834 if (!config.useLiveData) {
835 network.view.alert('Not yet implemented: "' + data.event + '"');
836 }
Simon Hunt56d51852014-11-09 13:03:35 -0800837 }
Simon Hunt99c13842014-11-06 18:23:12 -0800838
839 function unknownEvent(data) {
Simon Hunt434cf142014-11-24 11:10:28 -0800840 console.warn('Unknown event type: "' + data.event + '"', data);
Simon Hunt99c13842014-11-06 18:23:12 -0800841 }
842
843 function handleServerEvent(data) {
844 var fn = eventDispatch[data.event] || unknownEvent;
845 fn(data);
846 }
847
848 // ==============================
Simon Hunt61d04042014-11-11 17:27:16 -0800849 // Out-going messages...
850
Simon Huntb53e0682014-11-12 13:32:01 -0800851 function nSel() {
852 return selectOrder.length;
853 }
Simon Hunt61d04042014-11-11 17:27:16 -0800854 function getSel(idx) {
855 return selections[selectOrder[idx]];
856 }
Simon Huntb53e0682014-11-12 13:32:01 -0800857 function getSelId(idx) {
858 return getSel(idx).obj.id;
859 }
Thomas Vachuska9edca302014-11-22 17:06:42 -0800860 function getSelIds(start, endOffset) {
861 var end = selectOrder.length - endOffset;
862 var ids = [];
863 selectOrder.slice(start, end).forEach(function (d) {
864 ids.push(getSelId(d));
865 });
866 return ids;
867 }
Simon Huntb53e0682014-11-12 13:32:01 -0800868 function allSelectionsClass(cls) {
869 for (var i=0, n=nSel(); i<n; i++) {
870 if (getSel(i).obj.class !== cls) {
871 return false;
872 }
873 }
874 return true;
875 }
Simon Hunt61d04042014-11-11 17:27:16 -0800876
Thomas Vachuska47635c62014-11-22 01:21:36 -0800877 function toggleInstances() {
878 if (!oiBox.isVisible()) {
Simon Huntb0ecfa52014-11-23 21:05:12 -0800879 showInstances();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800880 } else {
Simon Huntb0ecfa52014-11-23 21:05:12 -0800881 hideInstances();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800882 }
883 }
884
Simon Huntb0ecfa52014-11-23 21:05:12 -0800885 function showInstances() {
886 oiBox.show();
887 colorAffinity = true;
888 updateDeviceColors();
889 }
890
891 function hideInstances() {
892 oiBox.hide();
893 colorAffinity = false;
894 cancelAffinity();
895 updateDeviceColors();
896 }
897
898
Thomas Vachuska47635c62014-11-22 01:21:36 -0800899 function toggleSummary() {
900 if (!summaryPane.isVisible()) {
901 requestSummary();
902 } else {
903 cancelSummary();
904 }
905 }
906
907 // request overall summary data
908 function requestSummary() {
909 sendMessage('requestSummary', {});
910 }
911
912 function cancelSummary() {
913 sendMessage('cancelSummary', {});
Simon Hunt06811b72014-11-25 18:54:48 -0800914 hideSummaryPane();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800915 }
916
Simon Hunt27d322d2014-11-28 10:45:43 -0800917 function toggleDetails() {
918 useDetails = !useDetails;
919 if (useDetails) {
920 flash('Enable details pane');
921 if (haveDetails) {
922 showDetailPane();
923 }
924 } else {
925 flash('Disable details pane');
926 hideDetailPane();
927 }
928 }
929
Simon Hunt06811b72014-11-25 18:54:48 -0800930 // encapsulate interaction between summary and details panes
931 function showSummaryPane() {
932 if (detailPane.isVisible()) {
933 detailPane.down(summaryPane.show);
934 } else {
935 summaryPane.show();
936 }
937 }
938
939 function hideSummaryPane() {
940 summaryPane.hide(function () {
941 if (detailPane.isVisible()) {
942 detailPane.up();
943 }
944 });
945 }
946
947 function showDetailPane() {
948 if (summaryPane.isVisible()) {
949 detailPane.down(detailPane.show);
950 } else {
951 detailPane.up(detailPane.show);
952 }
953 }
954
955 function hideDetailPane() {
956 detailPane.hide();
957 }
958
959
Simon Hunt61d04042014-11-11 17:27:16 -0800960 // request details for the selected element
Simon Huntd72bc702014-11-13 18:38:04 -0800961 // invoked from selection of a single node.
Simon Hunt61d04042014-11-11 17:27:16 -0800962 function requestDetails() {
963 var data = getSel(0).obj,
964 payload = {
965 id: data.id,
966 class: data.class
967 };
968 sendMessage('requestDetails', payload);
969 }
970
Thomas Vachuska9edca302014-11-22 17:06:42 -0800971 function addHostIntentAction() {
Simon Huntd72bc702014-11-13 18:38:04 -0800972 sendMessage('addHostIntent', {
Thomas Vachuska9edca302014-11-22 17:06:42 -0800973 one: selectOrder[0],
974 two: selectOrder[1],
975 ids: selectOrder
Simon Huntd72bc702014-11-13 18:38:04 -0800976 });
Simon Hunt27d322d2014-11-28 10:45:43 -0800977 flash('Host-to-Host flow added');
Simon Huntd72bc702014-11-13 18:38:04 -0800978 }
979
Thomas Vachuska9edca302014-11-22 17:06:42 -0800980 function addMultiSourceIntentAction() {
981 sendMessage('addMultiSourceIntent', {
982 src: selectOrder.slice(0, selectOrder.length - 1),
983 dst: selectOrder[selectOrder.length - 1],
984 ids: selectOrder
985 });
Simon Hunt27d322d2014-11-28 10:45:43 -0800986 flash('Multi-Source flow added');
Thomas Vachuska29617e52014-11-20 03:17:46 -0800987 }
988
Thomas Vachuska9edca302014-11-22 17:06:42 -0800989
Thomas Vachuska47635c62014-11-22 01:21:36 -0800990 function cancelTraffic() {
991 sendMessage('cancelTraffic', {});
992 }
993
Thomas Vachuska9edca302014-11-22 17:06:42 -0800994 function requestTrafficForMode() {
995 if (hoverMode === hoverModeAll) {
996 requestAllTraffic();
997 } else if (hoverMode === hoverModeFlows) {
998 requestDeviceLinkFlows();
999 } else if (hoverMode === hoverModeIntents) {
1000 requestSelectTraffic();
Simon Huntd72bc702014-11-13 18:38:04 -08001001 }
Thomas Vachuska9edca302014-11-22 17:06:42 -08001002 }
Simon Huntd72bc702014-11-13 18:38:04 -08001003
Thomas Vachuska9edca302014-11-22 17:06:42 -08001004 function showTrafficAction() {
1005 hoverMode = hoverModeIntents;
1006 requestSelectTraffic();
Simon Hunt27d322d2014-11-28 10:45:43 -08001007 flash('Related Traffic');
Thomas Vachuska9edca302014-11-22 17:06:42 -08001008 }
1009
1010 function requestSelectTraffic() {
1011 if (validateSelectionContext()) {
1012 var hoverId = (hoverMode === hoverModeIntents && hovered &&
1013 (hovered.class === 'host' || hovered.class === 'device'))
Simon Huntd72bc702014-11-13 18:38:04 -08001014 ? hovered.id : '';
Thomas Vachuska9edca302014-11-22 17:06:42 -08001015 sendMessage('requestTraffic', {
1016 ids: selectOrder,
1017 hover: hoverId
1018 });
1019 }
Simon Huntd72bc702014-11-13 18:38:04 -08001020 }
1021
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -08001022
Thomas Vachuska29617e52014-11-20 03:17:46 -08001023 function showDeviceLinkFlowsAction() {
Thomas Vachuska9edca302014-11-22 17:06:42 -08001024 hoverMode = hoverModeFlows;
1025 requestDeviceLinkFlows();
Simon Hunt27d322d2014-11-28 10:45:43 -08001026 flash('Device Flows');
Thomas Vachuska29617e52014-11-20 03:17:46 -08001027 }
1028
Thomas Vachuska9edca302014-11-22 17:06:42 -08001029 function requestDeviceLinkFlows() {
1030 if (validateSelectionContext()) {
1031 var hoverId = (hoverMode === hoverModeFlows && hovered &&
1032 (hovered.class === 'device')) ? hovered.id : '';
1033 sendMessage('requestDeviceLinkFlows', {
1034 ids: selectOrder,
1035 hover: hoverId
1036 });
1037 }
1038 }
1039
1040
1041 function showAllTrafficAction() {
1042 hoverMode = hoverModeAll;
1043 requestAllTraffic();
Simon Hunt27d322d2014-11-28 10:45:43 -08001044 flash('All Traffic');
Thomas Vachuska9edca302014-11-22 17:06:42 -08001045 }
1046
1047 function requestAllTraffic() {
1048 sendMessage('requestAllTraffic', {});
1049 }
1050
1051 function validateSelectionContext() {
Thomas Vachuska29617e52014-11-20 03:17:46 -08001052 if (!hovered && nSel() === 0) {
Thomas Vachuska47635c62014-11-22 01:21:36 -08001053 cancelTraffic();
Thomas Vachuska9edca302014-11-22 17:06:42 -08001054 return false;
Thomas Vachuska29617e52014-11-20 03:17:46 -08001055 }
Thomas Vachuska9edca302014-11-22 17:06:42 -08001056 return true;
Thomas Vachuska29617e52014-11-20 03:17:46 -08001057 }
Simon Huntd72bc702014-11-13 18:38:04 -08001058
Simon Hunta6a9fe72014-11-20 11:17:12 -08001059
Simon Hunt61d04042014-11-11 17:27:16 -08001060 // ==============================
Simon Hunta5e89142014-11-14 07:00:33 -08001061 // onos instance panel functions
Simon Huntb82f6902014-11-22 11:53:15 -08001062
Simon Hunt7b403bc2014-11-22 19:01:00 -08001063 var instCfg = {
1064 rectPad: 8,
1065 nodeOx: 9,
1066 nodeOy: 9,
1067 nodeDim: 40,
1068 birdOx: 19,
1069 birdOy: 21,
1070 birdDim: 21,
1071 uiDy: 45,
1072 titleDy: 30,
1073 textYOff: 20,
1074 textYSpc: 15
1075 };
1076
1077 function viewBox(dim) {
1078 return '0 0 ' + dim.w + ' ' + dim.h;
1079 }
1080
1081 function instRectAttr(dim) {
1082 var pad = instCfg.rectPad;
1083 return {
1084 x: pad,
1085 y: pad,
1086 width: dim.w - pad*2,
1087 height: dim.h - pad*2,
Simon Huntb0ecfa52014-11-23 21:05:12 -08001088 rx: 6
Simon Hunt7b403bc2014-11-22 19:01:00 -08001089 };
1090 }
1091
1092 function computeDim(self) {
1093 var css = window.getComputedStyle(self);
1094 return {
1095 w: stripPx(css.width),
1096 h: stripPx(css.height)
1097 };
Simon Huntb82f6902014-11-22 11:53:15 -08001098 }
Simon Hunta5e89142014-11-14 07:00:33 -08001099
1100 function updateInstances() {
1101 var onoses = oiBox.el.selectAll('.onosInst')
Simon Huntb82f6902014-11-22 11:53:15 -08001102 .data(onosOrder, function (d) { return d.id; }),
Simon Hunt7b403bc2014-11-22 19:01:00 -08001103 instDim = {w:0,h:0},
1104 c = instCfg;
Simon Hunta5e89142014-11-14 07:00:33 -08001105
Simon Hunt7b403bc2014-11-22 19:01:00 -08001106 function nSw(n) {
1107 return '# Switches: ' + n;
1108 }
Simon Hunta5e89142014-11-14 07:00:33 -08001109
Simon Huntb82f6902014-11-22 11:53:15 -08001110 // operate on existing onos instances if necessary
1111 onoses.each(function (d) {
1112 var el = d3.select(this),
1113 svg = el.select('svg');
Simon Hunt7b403bc2014-11-22 19:01:00 -08001114 instDim = computeDim(this);
Simon Huntb82f6902014-11-22 11:53:15 -08001115
1116 // update online state
1117 el.classed('online', d.online);
1118
1119 // update ui-attached state
1120 svg.select('use.uiBadge').remove();
1121 if (d.uiAttached) {
1122 attachUiBadge(svg);
1123 }
1124
Simon Hunt7b403bc2014-11-22 19:01:00 -08001125 function updAttr(id, value) {
1126 svg.select('text.instLabel.'+id).text(value);
1127 }
1128
1129 updAttr('ip', d.ip);
1130 updAttr('ns', nSw(d.switches));
Simon Huntb82f6902014-11-22 11:53:15 -08001131 });
1132
1133
1134 // operate on new onos instances
Simon Hunta5e89142014-11-14 07:00:33 -08001135 var entering = onoses.enter()
1136 .append('div')
1137 .attr('class', 'onosInst')
1138 .classed('online', function (d) { return d.online; })
Simon Hunt9c15eca2014-11-15 18:37:59 -08001139 .on('click', clickInst);
1140
Simon Huntb82f6902014-11-22 11:53:15 -08001141 entering.each(function (d) {
Simon Hunt9c15eca2014-11-15 18:37:59 -08001142 var el = d3.select(this),
Simon Hunt7b403bc2014-11-22 19:01:00 -08001143 rectAttr,
1144 svg;
1145 instDim = computeDim(this);
1146 rectAttr = instRectAttr(instDim);
Simon Hunt9c15eca2014-11-15 18:37:59 -08001147
Simon Hunt7b403bc2014-11-22 19:01:00 -08001148 svg = el.append('svg').attr({
1149 width: instDim.w,
1150 height: instDim.h,
1151 viewBox: viewBox(instDim)
Simon Hunt95908012014-11-20 10:20:26 -08001152 });
Simon Huntb82f6902014-11-22 11:53:15 -08001153
Simon Hunt7b403bc2014-11-22 19:01:00 -08001154 svg.append('rect').attr(rectAttr);
Simon Hunt9c15eca2014-11-15 18:37:59 -08001155
Thomas Vachuskae02e11c2014-11-24 16:13:52 -08001156 //appendGlyph(svg, c.nodeOx, c.nodeOy, c.nodeDim, '#node');
1157 appendBadge(svg, 14, 14, 28, '#bird');
Simon Huntb82f6902014-11-22 11:53:15 -08001158
1159 if (d.uiAttached) {
1160 attachUiBadge(svg);
1161 }
1162
Simon Hunt7b403bc2014-11-22 19:01:00 -08001163 var left = c.nodeOx + c.nodeDim,
1164 len = rectAttr.width - left,
1165 hlen = len / 2,
1166 midline = hlen + left;
Simon Hunt9c15eca2014-11-15 18:37:59 -08001167
Simon Hunt7b403bc2014-11-22 19:01:00 -08001168 // title
1169 svg.append('text')
1170 .attr({
1171 class: 'instTitle',
1172 x: midline,
1173 y: c.titleDy
1174 })
1175 .text(d.id);
1176
1177 // a couple of attributes
1178 var ty = c.titleDy + c.textYOff;
1179
1180 function addAttr(id, label) {
1181 svg.append('text').attr({
1182 class: 'instLabel ' + id,
1183 x: midline,
1184 y: ty
1185 }).text(label);
1186 ty += c.textYSpc;
1187 }
1188
1189 addAttr('ip', d.ip);
1190 addAttr('ns', nSw(d.switches));
Simon Hunt9c15eca2014-11-15 18:37:59 -08001191 });
Simon Hunta5e89142014-11-14 07:00:33 -08001192
1193 // operate on existing + new onoses here
Simon Hunt8f40cce2014-11-23 15:57:30 -08001194 // set the affinity colors...
1195 onoses.each(function (d) {
1196 var el = d3.select(this),
1197 rect = el.select('svg').select('rect'),
1198 col = instColor(d.id, d.online);
1199 rect.style('fill', col);
1200 });
Simon Hunta5e89142014-11-14 07:00:33 -08001201
Simon Hunt7b403bc2014-11-22 19:01:00 -08001202 // adjust the panel size appropriately...
1203 oiBox.width(instDim.w * onosOrder.length);
1204 oiBox.height(instDim.h);
1205
1206 // remove any outgoing instances
1207 onoses.exit().remove();
Simon Hunta5e89142014-11-14 07:00:33 -08001208 }
1209
Simon Hunt8f40cce2014-11-23 15:57:30 -08001210 function instColor(id, online) {
1211 return cat7.get(id, !online, network.view.getTheme());
1212 }
1213
Simon Hunt9462e8c2014-11-14 17:28:09 -08001214 function clickInst(d) {
1215 var el = d3.select(this),
1216 aff = el.classed('affinity');
1217 if (!aff) {
1218 setAffinity(el, d);
1219 } else {
1220 cancelAffinity();
1221 }
1222 }
1223
1224 function setAffinity(el, d) {
1225 d3.selectAll('.onosInst')
1226 .classed('mastership', true)
1227 .classed('affinity', false);
1228 el.classed('affinity', true);
1229
1230 suppressLayers(true);
1231 node.each(function (n) {
1232 if (n.master === d.id) {
1233 n.el.classed('suppressed', false);
1234 }
1235 });
1236 oiShowMaster = true;
1237 }
1238
1239 function cancelAffinity() {
1240 d3.selectAll('.onosInst')
1241 .classed('mastership affinity', false);
1242 restoreLayerState();
1243 oiShowMaster = false;
1244 }
1245
Simon Hunt7b403bc2014-11-22 19:01:00 -08001246 // TODO: these should be moved out to utility module.
1247 function stripPx(s) {
1248 return s.replace(/px$/,'');
1249 }
1250
1251 function appendUse(svg, ox, oy, dim, iid, cls) {
1252 var use = svg.append('use').attr({
1253 transform: translate(ox,oy),
1254 'xlink:href': iid,
1255 width: dim,
1256 height: dim
1257 });
1258 if (cls) {
1259 use.classed(cls, true);
1260 }
1261 return use;
1262 }
1263
1264 function appendGlyph(svg, ox, oy, dim, iid, cls) {
1265 appendUse(svg, ox, oy, dim, iid, cls).classed('glyphIcon', true);
1266 }
1267
1268 function appendBadge(svg, ox, oy, dim, iid, cls) {
1269 appendUse(svg, ox, oy, dim, iid,cls ).classed('badgeIcon', true);
1270 }
1271
1272 function attachUiBadge(svg) {
1273 appendBadge(svg, 12, instCfg.uiDy, 30, '#uiAttached', 'uiBadge');
1274 }
1275
Simon Hunt434cf142014-11-24 11:10:28 -08001276 function visVal(b) {
1277 return b ? 'visible' : 'hidden';
1278 }
1279
Simon Hunta5e89142014-11-14 07:00:33 -08001280 // ==============================
Simon Hunt99c13842014-11-06 18:23:12 -08001281 // force layout modification functions
1282
1283 function translate(x, y) {
1284 return 'translate(' + x + ',' + y + ')';
1285 }
1286
Simon Hunte2575b62014-11-18 15:25:53 -08001287 function rotate(deg) {
1288 return 'rotate(' + deg + ')';
1289 }
1290
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001291 function missMsg(what, id) {
1292 return '\n[' + what + '] "' + id + '" missing ';
1293 }
1294
1295 function linkEndPoints(srcId, dstId) {
1296 var srcNode = network.lookup[srcId],
1297 dstNode = network.lookup[dstId],
1298 sMiss = !srcNode ? missMsg('src', srcId) : '',
1299 dMiss = !dstNode ? missMsg('dst', dstId) : '';
1300
1301 if (sMiss || dMiss) {
1302 logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
1303 return null;
1304 }
1305 return {
1306 source: srcNode,
1307 target: dstNode,
1308 x1: srcNode.x,
1309 y1: srcNode.y,
1310 x2: dstNode.x,
1311 y2: dstNode.y
1312 };
1313 }
1314
Simon Hunt56d51852014-11-09 13:03:35 -08001315 function createHostLink(host) {
1316 var src = host.id,
1317 dst = host.cp.device,
Simon Hunt7cd48f32014-11-09 23:42:50 -08001318 id = host.ingress,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001319 lnk = linkEndPoints(src, dst);
Simon Hunt56d51852014-11-09 13:03:35 -08001320
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001321 if (!lnk) {
Simon Hunt56d51852014-11-09 13:03:35 -08001322 return null;
1323 }
1324
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001325 // Synthesize link ...
1326 $.extend(lnk, {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001327 key: id,
Simon Hunt56d51852014-11-09 13:03:35 -08001328 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -08001329
1330 type: function () { return 'hostLink'; },
1331 // TODO: ideally, we should see if our edge switch is online...
1332 online: function () { return true; },
1333 linkWidth: function () { return 1; }
Simon Hunt7cd48f32014-11-09 23:42:50 -08001334 });
Simon Hunt99c13842014-11-06 18:23:12 -08001335 return lnk;
1336 }
1337
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001338 function createLink(link) {
Simon Hunta6a9fe72014-11-20 11:17:12 -08001339 var lnk = linkEndPoints(link.src, link.dst);
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001340
1341 if (!lnk) {
1342 return null;
1343 }
1344
Simon Hunt8257f4c2014-11-16 19:34:54 -08001345 $.extend(lnk, {
1346 key: link.id,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001347 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -08001348 fromSource: link,
1349
1350 // functions to aggregate dual link state
1351 type: function () {
1352 var s = lnk.fromSource,
1353 t = lnk.fromTarget;
1354 return (s && s.type) || (t && t.type) || defaultLinkType;
1355 },
1356 online: function () {
1357 var s = lnk.fromSource,
1358 t = lnk.fromTarget;
1359 return (s && s.online) || (t && t.online);
1360 },
1361 linkWidth: function () {
1362 var s = lnk.fromSource,
1363 t = lnk.fromTarget,
1364 ws = (s && s.linkWidth) || 0,
1365 wt = (t && t.linkWidth) || 0;
1366 return Math.max(ws, wt);
1367 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001368 });
1369 return lnk;
Simon Hunt1a9eff92014-11-07 11:06:34 -08001370 }
1371
Simon Hunte2575b62014-11-18 15:25:53 -08001372 function removeLinkLabels() {
1373 network.links.forEach(function (d) {
1374 d.label = '';
1375 });
1376 }
1377
Simon Hunt434cf142014-11-24 11:10:28 -08001378 function showHostVis(el) {
1379 el.style('visibility', visVal(showHosts));
1380 }
1381
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001382 var widthRatio = 1.4,
1383 linkScale = d3.scale.linear()
1384 .domain([1, 12])
1385 .range([widthRatio, 12 * widthRatio])
1386 .clamp(true);
1387
Simon Hunt99c13842014-11-06 18:23:12 -08001388 function updateLinks() {
1389 link = linkG.selectAll('.link')
Simon Hunt8257f4c2014-11-16 19:34:54 -08001390 .data(network.links, function (d) { return d.key; });
Simon Hunt99c13842014-11-06 18:23:12 -08001391
1392 // operate on existing links, if necessary
1393 // link .foo() .bar() ...
1394
1395 // operate on entering links:
1396 var entering = link.enter()
1397 .append('line')
1398 .attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001399 x1: function (d) { return d.x1; },
1400 y1: function (d) { return d.y1; },
1401 x2: function (d) { return d.x2; },
1402 y2: function (d) { return d.y2; },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001403 stroke: config.topo.linkInColor,
1404 'stroke-width': config.topo.linkInWidth
Simon Hunt99c13842014-11-06 18:23:12 -08001405 });
1406
1407 // augment links
Simon Hunt7cd48f32014-11-09 23:42:50 -08001408 entering.each(function (d) {
1409 var link = d3.select(this);
1410 // provide ref to element selection from backing data....
1411 d.el = link;
Simon Hunt8257f4c2014-11-16 19:34:54 -08001412 restyleLinkElement(d);
Simon Hunt434cf142014-11-24 11:10:28 -08001413 if (d.type() === 'hostLink') {
1414 showHostVis(link);
1415 }
Simon Hunt7cd48f32014-11-09 23:42:50 -08001416 });
Thomas Vachuska4830d392014-11-09 17:09:56 -08001417
1418 // operate on both existing and new links, if necessary
1419 //link .foo() .bar() ...
1420
Simon Hunte2575b62014-11-18 15:25:53 -08001421 // apply or remove labels
1422 var labelData = getLabelData();
1423 applyLinkLabels(labelData);
1424
Thomas Vachuska4830d392014-11-09 17:09:56 -08001425 // operate on exiting links:
Thomas Vachuska4830d392014-11-09 17:09:56 -08001426 link.exit()
Simon Hunt13bf9c82014-11-18 07:26:44 -08001427 .attr('stroke-dasharray', '3, 3')
1428 .style('opacity', 0.5)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001429 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -08001430 .duration(1500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001431 .attr({
Simon Hunt13bf9c82014-11-18 07:26:44 -08001432 'stroke-dasharray': '3, 12',
1433 stroke: config.topo.linkOutColor,
1434 'stroke-width': config.topo.linkOutWidth
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001435 })
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001436 .style('opacity', 0.0)
Thomas Vachuska4830d392014-11-09 17:09:56 -08001437 .remove();
Simon Hunte2575b62014-11-18 15:25:53 -08001438
1439 // NOTE: invoke a single tick to force the labels to position
1440 // onto their links.
1441 tick();
1442 }
1443
1444 function getLabelData() {
1445 // create the backing data for showing labels..
1446 var data = [];
1447 link.each(function (d) {
1448 if (d.label) {
1449 data.push({
1450 id: 'lab-' + d.key,
1451 key: d.key,
1452 label: d.label,
1453 ldata: d
1454 });
1455 }
1456 });
1457 return data;
1458 }
1459
1460 var linkLabelOffset = '0.3em';
1461
1462 function applyLinkLabels(data) {
1463 var entering;
1464
1465 linkLabel = linkLabelG.selectAll('.linkLabel')
1466 .data(data, function (d) { return d.id; });
1467
Simon Hunt56a2ea42014-11-19 12:39:31 -08001468 // for elements already existing, we need to update the text
1469 // and adjust the rectangle size to fit
1470 linkLabel.each(function (d) {
1471 var el = d3.select(this),
1472 rect = el.select('rect'),
1473 text = el.select('text');
1474 text.text(d.label);
1475 rect.attr(rectAroundText(el));
1476 });
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -08001477
Simon Hunte2575b62014-11-18 15:25:53 -08001478 entering = linkLabel.enter().append('g')
1479 .classed('linkLabel', true)
1480 .attr('id', function (d) { return d.id; });
1481
1482 entering.each(function (d) {
1483 var el = d3.select(this),
1484 rect,
1485 text,
1486 parms = {
1487 x1: d.ldata.x1,
1488 y1: d.ldata.y1,
1489 x2: d.ldata.x2,
1490 y2: d.ldata.y2
1491 };
1492
1493 d.el = el;
1494 rect = el.append('rect');
1495 text = el.append('text').text(d.label);
1496 rect.attr(rectAroundText(el));
1497 text.attr('dy', linkLabelOffset);
1498
1499 el.attr('transform', transformLabel(parms));
1500 });
1501
1502 // Remove any links that are no longer required.
1503 linkLabel.exit().remove();
1504 }
1505
1506 function rectAroundText(el) {
1507 var text = el.select('text'),
1508 box = text.node().getBBox();
1509
1510 // translate the bbox so that it is centered on [x,y]
1511 box.x = -box.width / 2;
1512 box.y = -box.height / 2;
1513
1514 // add padding
1515 box.x -= 1;
1516 box.width += 2;
1517 return box;
1518 }
1519
1520 function transformLabel(p) {
1521 var dx = p.x2 - p.x1,
1522 dy = p.y2 - p.y1,
1523 xMid = dx/2 + p.x1,
1524 yMid = dy/2 + p.y1;
Simon Hunte2575b62014-11-18 15:25:53 -08001525 return translate(xMid, yMid);
Simon Hunt99c13842014-11-06 18:23:12 -08001526 }
1527
1528 function createDeviceNode(device) {
1529 // start with the object as is
1530 var node = device,
Simon Huntbb282f52014-11-10 11:08:19 -08001531 type = device.type,
Simon Huntc72967b2014-11-20 09:21:42 -08001532 svgCls = type ? 'node device ' + type : 'node device',
1533 labels = device.labels || [];
1534
Simon Hunt99c13842014-11-06 18:23:12 -08001535 // Augment as needed...
1536 node.class = 'device';
Simon Huntbb282f52014-11-10 11:08:19 -08001537 node.svgClass = device.online ? svgCls + ' online' : svgCls;
Simon Hunt99c13842014-11-06 18:23:12 -08001538 positionNode(node);
Simon Hunt99c13842014-11-06 18:23:12 -08001539 return node;
1540 }
1541
Simon Hunt56d51852014-11-09 13:03:35 -08001542 function createHostNode(host) {
1543 // start with the object as is
1544 var node = host;
1545
1546 // Augment as needed...
1547 node.class = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001548 if (!node.type) {
Simon Hunt209155e2014-11-21 12:16:09 -08001549 node.type = 'endstation';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001550 }
Simon Hunt7fa116d2014-11-17 14:16:55 -08001551 node.svgClass = 'node host ' + node.type;
Simon Hunt56d51852014-11-09 13:03:35 -08001552 positionNode(node);
Simon Hunt56d51852014-11-09 13:03:35 -08001553 return node;
1554 }
1555
Simon Hunt62c47542014-11-22 22:16:32 -08001556 function positionNode(node, forUpdate) {
Simon Hunt99c13842014-11-06 18:23:12 -08001557 var meta = node.metaUi,
Simon Huntac9e24f2014-11-12 10:12:21 -08001558 x = meta && meta.x,
1559 y = meta && meta.y,
1560 xy;
Simon Hunt99c13842014-11-06 18:23:12 -08001561
Simon Huntac9e24f2014-11-12 10:12:21 -08001562 // If we have [x,y] already, use that...
Simon Hunt99c13842014-11-06 18:23:12 -08001563 if (x && y) {
1564 node.fixed = true;
Simon Hunt62c47542014-11-22 22:16:32 -08001565 node.px = node.x = x;
1566 node.py = node.y = y;
1567 //node.px = x;
1568 //node.py = y;
Simon Huntac9e24f2014-11-12 10:12:21 -08001569 return;
Simon Hunt99c13842014-11-06 18:23:12 -08001570 }
Simon Huntac9e24f2014-11-12 10:12:21 -08001571
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001572 var location = node.location;
1573 if (location && location.type === 'latlng') {
1574 var coord = geoMapProjection([location.lng, location.lat]);
1575 node.fixed = true;
Simon Hunt62c47542014-11-22 22:16:32 -08001576 node.px = node.x = coord[0];
1577 node.py = node.y = coord[1];
1578 //node.x = coord[0];
1579 //node.y = coord[1];
1580 return true;
1581 }
1582
1583 // if this is a node update (not a node add).. skip randomizer
1584 if (forUpdate) {
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001585 return;
1586 }
1587
Simon Huntac9e24f2014-11-12 10:12:21 -08001588 // Note: Placing incoming unpinned nodes at exactly the same point
1589 // (center of the view) causes them to explode outwards when
1590 // the force layout kicks in. So, we spread them out a bit
1591 // initially, to provide a more serene layout convergence.
1592 // Additionally, if the node is a host, we place it near
1593 // the device it is connected to.
1594
1595 function spread(s) {
1596 return Math.floor((Math.random() * s) - s/2);
1597 }
1598
1599 function randDim(dim) {
1600 return dim / 2 + spread(dim * 0.7071);
1601 }
1602
1603 function rand() {
1604 return {
1605 x: randDim(network.view.width()),
1606 y: randDim(network.view.height())
1607 };
1608 }
1609
1610 function near(node) {
1611 var min = 12,
1612 dx = spread(12),
1613 dy = spread(12);
1614 return {
1615 x: node.x + min + dx,
1616 y: node.y + min + dy
1617 };
1618 }
1619
1620 function getDevice(cp) {
1621 var d = network.lookup[cp.device];
1622 return d || rand();
1623 }
1624
1625 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
1626 $.extend(node, xy);
Simon Hunt99c13842014-11-06 18:23:12 -08001627 }
1628
Simon Hunt99c13842014-11-06 18:23:12 -08001629
Simon Huntc72967b2014-11-20 09:21:42 -08001630 function iconGlyphUrl(d) {
1631 var which = d.type || 'unknown';
1632 return '#' + which;
1633 }
1634
Simon Hunt99c13842014-11-06 18:23:12 -08001635 // returns the newly computed bounding box of the rectangle
1636 function adjustRectToFitText(n) {
1637 var text = n.select('text'),
1638 box = text.node().getBBox(),
1639 lab = config.labels;
1640
1641 text.attr('text-anchor', 'middle')
1642 .attr('y', '-0.8em')
1643 .attr('x', lab.imgPad/2);
1644
1645 // translate the bbox so that it is centered on [x,y]
1646 box.x = -box.width / 2;
1647 box.y = -box.height / 2;
1648
1649 // add padding
1650 box.x -= (lab.padLR + lab.imgPad/2);
1651 box.width += lab.padLR * 2 + lab.imgPad;
1652 box.y -= lab.padTB;
1653 box.height += lab.padTB * 2;
1654
1655 return box;
1656 }
1657
Simon Hunt1a9eff92014-11-07 11:06:34 -08001658 function mkSvgClass(d) {
1659 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
1660 }
1661
Simon Hunt7cd48f32014-11-09 23:42:50 -08001662 function hostLabel(d) {
1663 var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0;
1664 return d.labels[idx];
1665 }
1666 function deviceLabel(d) {
1667 var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0;
1668 return d.labels[idx];
1669 }
Simon Huntc72967b2014-11-20 09:21:42 -08001670 function trimLabel(label) {
1671 return (label && label.trim()) || '';
1672 }
1673
1674 function emptyBox() {
1675 return {
1676 x: -2,
1677 y: -2,
1678 width: 4,
1679 height: 4
1680 };
Simon Hunt7cd48f32014-11-09 23:42:50 -08001681 }
1682
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001683 function updateDeviceLabel(d) {
Simon Huntc72967b2014-11-20 09:21:42 -08001684 var label = trimLabel(deviceLabel(d)),
1685 noLabel = !label,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001686 node = d.el,
Simon Huntc72967b2014-11-20 09:21:42 -08001687 box,
1688 dx,
Simon Hunt395a70c2014-11-22 23:17:40 -08001689 dy,
1690 cfg = config.icons.device;
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001691
1692 node.select('text')
1693 .text(label)
1694 .style('opacity', 0)
1695 .transition()
1696 .style('opacity', 1);
1697
Simon Huntc72967b2014-11-20 09:21:42 -08001698 if (noLabel) {
1699 box = emptyBox();
Simon Hunt395a70c2014-11-22 23:17:40 -08001700 dx = -cfg.dim/2;
1701 dy = -cfg.dim/2;
Simon Huntc72967b2014-11-20 09:21:42 -08001702 } else {
1703 box = adjustRectToFitText(node);
Simon Hunt395a70c2014-11-22 23:17:40 -08001704 dx = box.x + cfg.xoff;
1705 dy = box.y + cfg.yoff;
Simon Huntc72967b2014-11-20 09:21:42 -08001706 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001707
1708 node.select('rect')
1709 .transition()
1710 .attr(box);
1711
Simon Huntc72967b2014-11-20 09:21:42 -08001712 node.select('g.deviceIcon')
Thomas Vachuska89543292014-11-19 11:28:33 -08001713 .transition()
Simon Huntc72967b2014-11-20 09:21:42 -08001714 .attr('transform', translate(dx, dy));
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001715 }
1716
1717 function updateHostLabel(d) {
1718 var label = hostLabel(d),
1719 host = d.el;
1720
1721 host.select('text').text(label);
1722 }
1723
Simon Hunt62c47542014-11-22 22:16:32 -08001724 // FIXME : fold this into updateNodes.
Simon Huntbb282f52014-11-10 11:08:19 -08001725 function updateHostState(hostData) {
1726 updateHostLabel(hostData);
Simon Huntbb282f52014-11-10 11:08:19 -08001727 }
1728
Simon Hunt434cf142014-11-24 11:10:28 -08001729 function updateHostVisibility() {
1730 var v = visVal(showHosts);
1731 nodeG.selectAll('.host').style('visibility', v);
1732 linkG.selectAll('.hostLink').style('visibility', v);
1733 }
1734
Simon Hunt6ac93f32014-11-13 12:17:27 -08001735 function nodeMouseOver(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001736 hovered = d;
Thomas Vachuska9edca302014-11-22 17:06:42 -08001737 requestTrafficForMode();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001738 }
1739
1740 function nodeMouseOut(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001741 hovered = null;
Thomas Vachuska9edca302014-11-22 17:06:42 -08001742 requestTrafficForMode();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001743 }
Simon Huntbb282f52014-11-10 11:08:19 -08001744
Simon Hunteb1514d2014-11-20 09:57:29 -08001745 function addHostIcon(node, radius, iid) {
Thomas Vachuska89543292014-11-19 11:28:33 -08001746 var dim = radius * 1.5,
1747 xlate = -dim / 2;
1748
Simon Hunteb1514d2014-11-20 09:57:29 -08001749 node.append('use').attr({
1750 class: 'glyphIcon hostIcon',
1751 transform: translate(xlate,xlate),
1752 'xlink:href': iid,
1753 width: dim,
1754 height: dim
1755 });
Thomas Vachuska89543292014-11-19 11:28:33 -08001756 }
1757
Simon Hunt99c13842014-11-06 18:23:12 -08001758 function updateNodes() {
1759 node = nodeG.selectAll('.node')
1760 .data(network.nodes, function (d) { return d.id; });
1761
Simon Hunt62c47542014-11-22 22:16:32 -08001762 // operate on existing nodes...
1763 node.filter('.device').each(function (d) {
Simon Hunt62c47542014-11-22 22:16:32 -08001764 var node = d.el;
1765 node.classed('online', d.online);
1766 updateDeviceLabel(d);
1767 positionNode(d, true);
1768 });
1769
1770 node.filter('.host').each(function (d) {
Simon Hunt62c47542014-11-22 22:16:32 -08001771 var node = d.el;
1772 // TODO: appropriate update of host visuals
1773 });
Simon Hunt99c13842014-11-06 18:23:12 -08001774
1775 // operate on entering nodes:
1776 var entering = node.enter()
1777 .append('g')
1778 .attr({
1779 id: function (d) { return safeId(d.id); },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001780 class: mkSvgClass,
Simon Hunt99c13842014-11-06 18:23:12 -08001781 transform: function (d) { return translate(d.x, d.y); },
1782 opacity: 0
1783 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08001784 .call(network.drag)
Simon Hunt6ac93f32014-11-13 12:17:27 -08001785 .on('mouseover', nodeMouseOver)
1786 .on('mouseout', nodeMouseOut)
Simon Hunt99c13842014-11-06 18:23:12 -08001787 .transition()
1788 .attr('opacity', 1);
1789
1790 // augment device nodes...
1791 entering.filter('.device').each(function (d) {
1792 var node = d3.select(this),
Simon Huntc72967b2014-11-20 09:21:42 -08001793 label = trimLabel(deviceLabel(d)),
1794 noLabel = !label,
Simon Hunt99c13842014-11-06 18:23:12 -08001795 box;
1796
Simon Hunt7cd48f32014-11-09 23:42:50 -08001797 // provide ref to element from backing data....
1798 d.el = node;
1799
Simon Hunt62c47542014-11-22 22:16:32 -08001800 node.append('rect').attr({ rx: 5, ry: 5 });
1801 node.append('text').text(label).attr('dy', '1.1em');
Simon Hunt99c13842014-11-06 18:23:12 -08001802 box = adjustRectToFitText(node);
Simon Hunta3dd9572014-11-20 15:22:41 -08001803 node.select('rect').attr(box);
Simon Huntc72967b2014-11-20 09:21:42 -08001804 addDeviceIcon(node, box, noLabel, iconGlyphUrl(d));
Simon Huntc7ee0662014-11-05 16:44:37 -08001805 });
Simon Hunt934c3ce2014-11-05 11:45:07 -08001806
Thomas Vachuska89543292014-11-19 11:28:33 -08001807
Simon Hunt56d51852014-11-09 13:03:35 -08001808 // augment host nodes...
1809 entering.filter('.host').each(function (d) {
1810 var node = d3.select(this),
Simon Hunt395a70c2014-11-22 23:17:40 -08001811 cfg = config.icons.host,
1812 r = cfg.radius[d.type] || cfg.defaultRadius,
Thomas Vachuska89543292014-11-19 11:28:33 -08001813 textDy = r + 10,
Simon Hunteb1514d2014-11-20 09:57:29 -08001814 iid = iconGlyphUrl(d);
Simon Hunt56d51852014-11-09 13:03:35 -08001815
Simon Hunt7cd48f32014-11-09 23:42:50 -08001816 // provide ref to element from backing data....
1817 d.el = node;
Simon Hunt434cf142014-11-24 11:10:28 -08001818 showHostVis(node);
Simon Hunt7cd48f32014-11-09 23:42:50 -08001819
Simon Hunt62c47542014-11-22 22:16:32 -08001820 node.append('circle').attr('r', r);
Simon Hunteb1514d2014-11-20 09:57:29 -08001821 if (iid) {
1822 addHostIcon(node, r, iid);
Simon Hunt7fa116d2014-11-17 14:16:55 -08001823 }
Simon Hunt56d51852014-11-09 13:03:35 -08001824 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001825 .text(hostLabel)
Thomas Vachuska89543292014-11-19 11:28:33 -08001826 .attr('dy', textDy)
Simon Hunt7cd48f32014-11-09 23:42:50 -08001827 .attr('text-anchor', 'middle');
Simon Hunt56d51852014-11-09 13:03:35 -08001828 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001829
Simon Hunt99c13842014-11-06 18:23:12 -08001830 // operate on both existing and new nodes, if necessary
Simon Huntb0ecfa52014-11-23 21:05:12 -08001831 updateDeviceColors();
Simon Huntc7ee0662014-11-05 16:44:37 -08001832
Simon Hunt99c13842014-11-06 18:23:12 -08001833 // operate on exiting nodes:
Simon Huntea80eb42014-11-11 13:46:57 -08001834 // Note that the node is removed after 2 seconds.
1835 // Sub element animations should be shorter than 2 seconds.
1836 var exiting = node.exit()
Simon Hunt44031102014-11-11 13:20:36 -08001837 .transition()
1838 .duration(2000)
Simon Huntea80eb42014-11-11 13:46:57 -08001839 .style('opacity', 0)
Simon Hunt99c13842014-11-06 18:23:12 -08001840 .remove();
Simon Huntea80eb42014-11-11 13:46:57 -08001841
1842 // host node exits....
1843 exiting.filter('.host').each(function (d) {
1844 var node = d3.select(this);
1845
1846 node.select('text')
1847 .style('opacity', 0.5)
1848 .transition()
1849 .duration(1000)
1850 .style('opacity', 0);
1851 // note, leave <g>.remove to remove this element
1852
Thomas Vachuska89543292014-11-19 11:28:33 -08001853 node.select('circle')
1854 .style('stroke-fill', '#555')
1855 .style('fill', '#888')
Simon Huntea80eb42014-11-11 13:46:57 -08001856 .style('opacity', 0.5)
1857 .transition()
1858 .duration(1500)
1859 .attr('r', 0);
1860 // note, leave <g>.remove to remove this element
1861
1862 });
1863
Simon Hunt62c47542014-11-22 22:16:32 -08001864 // TODO: device node exit animation
1865
1866 network.force.resume();
Simon Huntc7ee0662014-11-05 16:44:37 -08001867 }
1868
Simon Hunt95dad922014-11-24 09:43:31 -08001869 var dCol = {
1870 black: '#000',
1871 paleblue: '#acf',
1872 offwhite: '#ddd',
1873 midgrey: '#888',
1874 lightgrey: '#bbb',
1875 orange: '#f90'
1876 };
1877
Simon Huntb0ecfa52014-11-23 21:05:12 -08001878 // note: these are the device icon colors without affinity
Simon Hunt95dad922014-11-24 09:43:31 -08001879 var dColTheme = {
Simon Huntb0ecfa52014-11-23 21:05:12 -08001880 light: {
1881 online: {
Simon Hunt95dad922014-11-24 09:43:31 -08001882 glyph: dCol.black,
1883 rect: dCol.paleblue
Simon Huntb0ecfa52014-11-23 21:05:12 -08001884 },
1885 offline: {
Simon Hunt95dad922014-11-24 09:43:31 -08001886 glyph: dCol.midgrey,
1887 rect: dCol.lightgrey
Simon Huntb0ecfa52014-11-23 21:05:12 -08001888 }
1889 },
Simon Hunt95dad922014-11-24 09:43:31 -08001890 // TODO: theme
Simon Huntb0ecfa52014-11-23 21:05:12 -08001891 dark: {
1892 online: {
Simon Hunt95dad922014-11-24 09:43:31 -08001893 glyph: dCol.black,
1894 rect: dCol.paleblue
Simon Huntb0ecfa52014-11-23 21:05:12 -08001895 },
1896 offline: {
Simon Hunt95dad922014-11-24 09:43:31 -08001897 glyph: dCol.midgrey,
1898 rect: dCol.lightgrey
Simon Huntb0ecfa52014-11-23 21:05:12 -08001899 }
1900 }
1901 };
1902
1903 function devBaseColor(d) {
1904 var t = network.view.getTheme(),
1905 o = d.online ? 'online' : 'offline';
Simon Hunt95dad922014-11-24 09:43:31 -08001906 return dColTheme[t][o];
Simon Huntb0ecfa52014-11-23 21:05:12 -08001907 }
1908
1909 function setDeviceColor(d) {
1910 var o = d.online,
1911 s = d.el.classed('selected'),
1912 c = devBaseColor(d),
1913 a = instColor(d.master, o),
1914 g, r,
1915 icon = d.el.select('g.deviceIcon');
1916
1917 if (s) {
1918 g = c.glyph;
Simon Hunt95dad922014-11-24 09:43:31 -08001919 r = dColTheme.sel;
Simon Huntb0ecfa52014-11-23 21:05:12 -08001920 } else if (colorAffinity) {
1921 g = o ? a : c.glyph;
Simon Hunt95dad922014-11-24 09:43:31 -08001922 r = o ? dCol.offwhite : a;
Simon Huntb0ecfa52014-11-23 21:05:12 -08001923 } else {
1924 g = c.glyph;
1925 r = c.rect;
1926 }
1927
1928 icon.select('use')
1929 .style('fill', g);
1930 icon.select('rect')
1931 .style('fill', r);
1932 }
1933
Simon Huntc72967b2014-11-20 09:21:42 -08001934 function addDeviceIcon(node, box, noLabel, iid) {
1935 var cfg = config.icons.device,
1936 dx,
1937 dy,
1938 g;
1939
1940 if (noLabel) {
1941 box = emptyBox();
1942 dx = -cfg.dim/2;
1943 dy = -cfg.dim/2;
1944 } else {
1945 box = adjustRectToFitText(node);
Simon Hunt395a70c2014-11-22 23:17:40 -08001946 dx = box.x + cfg.xoff;
1947 dy = box.y + cfg.yoff;
Simon Huntc72967b2014-11-20 09:21:42 -08001948 }
1949
Simon Hunteb1514d2014-11-20 09:57:29 -08001950 g = node.append('g')
1951 .attr('class', 'glyphIcon deviceIcon')
Simon Huntc72967b2014-11-20 09:21:42 -08001952 .attr('transform', translate(dx, dy));
1953
1954 g.append('rect').attr({
1955 x: 0,
1956 y: 0,
1957 rx: cfg.rx,
1958 width: cfg.dim,
1959 height: cfg.dim
1960 });
1961
1962 g.append('use').attr({
1963 'xlink:href': iid,
1964 width: cfg.dim,
1965 height: cfg.dim
1966 });
1967
Simon Huntc72967b2014-11-20 09:21:42 -08001968 }
1969
Simon Hunt7b403bc2014-11-22 19:01:00 -08001970 function find(key, array, tag) {
1971 var _tag = tag || 'key',
1972 idx, n, d;
1973 for (idx = 0, n = array.length; idx < n; idx++) {
1974 d = array[idx];
1975 if (d[_tag] === key) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001976 return idx;
1977 }
1978 }
1979 return -1;
1980 }
1981
1982 function removeLinkElement(linkData) {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001983 var idx = find(linkData.key, network.links),
1984 removed;
1985 if (idx >=0) {
1986 // remove from links array
1987 removed = network.links.splice(idx, 1);
1988 // remove from lookup cache
1989 delete network.lookup[removed[0].key];
1990 updateLinks();
1991 network.force.resume();
1992 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001993 }
Simon Huntc7ee0662014-11-05 16:44:37 -08001994
Simon Hunt44031102014-11-11 13:20:36 -08001995 function removeHostElement(hostData) {
1996 // first, remove associated hostLink...
1997 removeLinkElement(hostData.linkData);
1998
1999 // remove from lookup cache
2000 delete network.lookup[hostData.id];
2001 // remove from nodes array
2002 var idx = find(hostData.id, network.nodes);
2003 network.nodes.splice(idx, 1);
2004 // remove from SVG
2005 updateNodes();
2006 network.force.resume();
2007 }
2008
2009
Simon Huntc7ee0662014-11-05 16:44:37 -08002010 function tick() {
2011 node.attr({
Simon Hunt99c13842014-11-06 18:23:12 -08002012 transform: function (d) { return translate(d.x, d.y); }
Simon Huntc7ee0662014-11-05 16:44:37 -08002013 });
2014
2015 link.attr({
2016 x1: function (d) { return d.source.x; },
2017 y1: function (d) { return d.source.y; },
2018 x2: function (d) { return d.target.x; },
2019 y2: function (d) { return d.target.y; }
2020 });
Simon Hunte2575b62014-11-18 15:25:53 -08002021
2022 linkLabel.each(function (d) {
2023 var el = d3.select(this);
Thomas Vachuska4731f122014-11-20 04:56:19 -08002024 var lnk = findLinkById(d.key);
2025
2026 if (lnk) {
2027 var parms = {
Simon Hunte2575b62014-11-18 15:25:53 -08002028 x1: lnk.source.x,
2029 y1: lnk.source.y,
2030 x2: lnk.target.x,
2031 y2: lnk.target.y
2032 };
Thomas Vachuska4731f122014-11-20 04:56:19 -08002033 el.attr('transform', transformLabel(parms));
2034 }
Simon Hunte2575b62014-11-18 15:25:53 -08002035 });
Simon Huntc7ee0662014-11-05 16:44:37 -08002036 }
Simon Hunt934c3ce2014-11-05 11:45:07 -08002037
2038 // ==============================
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002039 // Web-Socket for live data
2040
2041 function webSockUrl() {
2042 return document.location.toString()
2043 .replace(/\#.*/, '')
2044 .replace('http://', 'ws://')
2045 .replace('https://', 'wss://')
Simon Hunte5ab1382014-11-25 10:28:51 -08002046 .replace('index.html', config.webSockUrl);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002047 }
2048
2049 webSock = {
2050 ws : null,
2051
2052 connect : function() {
2053 webSock.ws = new WebSocket(webSockUrl());
2054
2055 webSock.ws.onopen = function() {
Simon Hunt0c6d4192014-11-12 12:07:10 -08002056 noWebSock(false);
Thomas Vachuska47635c62014-11-22 01:21:36 -08002057 requestSummary();
Simon Huntb0ecfa52014-11-23 21:05:12 -08002058 showInstances();
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002059 };
2060
2061 webSock.ws.onmessage = function(m) {
2062 if (m.data) {
Simon Huntbb282f52014-11-10 11:08:19 -08002063 wsTraceRx(m.data);
Thomas Vachuskad472c6e2014-11-07 19:11:05 -08002064 handleServerEvent(JSON.parse(m.data));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002065 }
2066 };
2067
2068 webSock.ws.onclose = function(m) {
2069 webSock.ws = null;
Simon Hunt0c6d4192014-11-12 12:07:10 -08002070 noWebSock(true);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002071 };
2072 },
2073
2074 send : function(text) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002075 if (text != null) {
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002076 webSock._send(text);
2077 }
2078 },
2079
2080 _send : function(message) {
2081 if (webSock.ws) {
2082 webSock.ws.send(message);
Simon Hunta255a2c2014-11-13 22:29:35 -08002083 } else if (config.useLiveData) {
Simon Hunt434cf142014-11-24 11:10:28 -08002084 console.warn('no web socket open', message);
Simon Hunta255a2c2014-11-13 22:29:35 -08002085 } else {
Simon Hunt434cf142014-11-24 11:10:28 -08002086 console.log('WS Send: ', message);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002087 }
2088 }
2089
2090 };
2091
Simon Hunt0c6d4192014-11-12 12:07:10 -08002092 function noWebSock(b) {
2093 mask.style('display',b ? 'block' : 'none');
2094 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002095
2096 function sendMessage(evType, payload) {
2097 var toSend = {
Simon Huntbb282f52014-11-10 11:08:19 -08002098 event: evType,
2099 sid: ++sid,
2100 payload: payload
2101 },
2102 asText = JSON.stringify(toSend);
2103 wsTraceTx(asText);
2104 webSock.send(asText);
Simon Huntc76ae892014-11-18 17:31:51 -08002105
2106 // Temporary measure for debugging UI behavior ...
2107 if (!config.useLiveData) {
2108 handleTestSend(toSend);
2109 }
Simon Huntbb282f52014-11-10 11:08:19 -08002110 }
2111
2112 function wsTraceTx(msg) {
2113 wsTrace('tx', msg);
2114 }
2115 function wsTraceRx(msg) {
2116 wsTrace('rx', msg);
2117 }
2118 function wsTrace(rxtx, msg) {
Simon Huntbb282f52014-11-10 11:08:19 -08002119 console.log('[' + rxtx + '] ' + msg);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002120 }
2121
Simon Huntc76ae892014-11-18 17:31:51 -08002122 // NOTE: Temporary hardcoded example for showing detail pane
2123 // while we fine-
2124 // Probably should not merge this change...
2125 function handleTestSend(msg) {
2126 if (msg.event === 'requestDetails') {
2127 showDetails({
2128 event: 'showDetails',
2129 sid: 1001,
2130 payload: {
2131 "id": "of:0000ffffffffff09",
2132 "type": "roadm",
2133 "propOrder": [
2134 "Name",
2135 "Vendor",
2136 "H/W Version",
2137 "S/W Version",
2138 "-",
2139 "Latitude",
2140 "Longitude",
2141 "Ports"
2142 ],
2143 "props": {
2144 "Name": null,
2145 "Vendor": "Linc",
2146 "H/W Version": "OE",
2147 "S/W Version": "?",
2148 "-": "",
2149 "Latitude": "40.8",
2150 "Longitude": "73.1",
2151 "Ports": "2"
2152 }
2153 }
2154 });
2155 }
2156 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002157
2158 // ==============================
2159 // Selection stuff
2160
2161 function selectObject(obj, el) {
2162 var n,
Simon Hunt01095ff2014-11-13 16:37:29 -08002163 srcEv = d3.event.sourceEvent,
2164 meta = srcEv.metaKey,
2165 shift = srcEv.shiftKey;
2166
Simon Huntdeab4322014-11-13 18:49:07 -08002167 if ((panZoom() && !meta) || (!panZoom() && meta)) {
Simon Hunt01095ff2014-11-13 16:37:29 -08002168 return;
2169 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002170
2171 if (el) {
2172 n = d3.select(el);
2173 } else {
2174 node.each(function(d) {
2175 if (d == obj) {
2176 n = d3.select(el = this);
2177 }
2178 });
2179 }
2180 if (!n) return;
2181
Simon Hunt01095ff2014-11-13 16:37:29 -08002182 if (shift && n.classed('selected')) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002183 deselectObject(obj.id);
Simon Hunt61d04042014-11-11 17:27:16 -08002184 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002185 return;
2186 }
2187
Simon Hunt01095ff2014-11-13 16:37:29 -08002188 if (!shift) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002189 deselectAll();
2190 }
2191
Simon Huntc31d5692014-11-12 13:27:18 -08002192 selections[obj.id] = { obj: obj, el: el };
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002193 selectOrder.push(obj.id);
2194
2195 n.classed('selected', true);
Simon Huntb0ecfa52014-11-23 21:05:12 -08002196 updateDeviceColors(obj);
Simon Hunt61d04042014-11-11 17:27:16 -08002197 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002198 }
2199
2200 function deselectObject(id) {
Simon Huntc31d5692014-11-12 13:27:18 -08002201 var obj = selections[id],
2202 idx;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002203 if (obj) {
2204 d3.select(obj.el).classed('selected', false);
Simon Hunt61d04042014-11-11 17:27:16 -08002205 delete selections[id];
Simon Huntc31d5692014-11-12 13:27:18 -08002206 idx = $.inArray(id, selectOrder);
2207 if (idx >= 0) {
2208 selectOrder.splice(idx, 1);
2209 }
Simon Huntb0ecfa52014-11-23 21:05:12 -08002210 updateDeviceColors(obj.obj);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002211 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002212 }
2213
2214 function deselectAll() {
2215 // deselect all nodes in the network...
2216 node.classed('selected', false);
2217 selections = {};
2218 selectOrder = [];
Simon Huntb0ecfa52014-11-23 21:05:12 -08002219 updateDeviceColors();
Simon Hunt61d04042014-11-11 17:27:16 -08002220 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002221 }
2222
Simon Huntb0ecfa52014-11-23 21:05:12 -08002223 function updateDeviceColors(d) {
2224 if (d) {
2225 setDeviceColor(d);
2226 } else {
2227 node.filter('.device').each(function (d) {
2228 setDeviceColor(d);
2229 });
2230 }
Thomas Vachuska47635c62014-11-22 01:21:36 -08002231 }
2232
Simon Hunt61d04042014-11-11 17:27:16 -08002233 // update the state of the detail pane, based on current selections
2234 function updateDetailPane() {
2235 var nSel = selectOrder.length;
2236 if (!nSel) {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002237 emptySelect();
Simon Hunt61d04042014-11-11 17:27:16 -08002238 } else if (nSel === 1) {
2239 singleSelect();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002240 requestTrafficForMode();
Simon Hunt61d04042014-11-11 17:27:16 -08002241 } else {
2242 multiSelect();
2243 }
2244 }
2245
Thomas Vachuska9edca302014-11-22 17:06:42 -08002246 function emptySelect() {
Simon Hunt27d322d2014-11-28 10:45:43 -08002247 haveDetails = false;
Simon Hunt06811b72014-11-25 18:54:48 -08002248 hideDetailPane();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002249 cancelTraffic();
2250 }
2251
Simon Hunt61d04042014-11-11 17:27:16 -08002252 function singleSelect() {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002253 // NOTE: detail is shown from showDetails event callback
Simon Hunt61d04042014-11-11 17:27:16 -08002254 requestDetails();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002255 requestTrafficForMode();
Simon Hunt61d04042014-11-11 17:27:16 -08002256 }
2257
2258 function multiSelect() {
Simon Hunt27d322d2014-11-28 10:45:43 -08002259 haveDetails = true;
Simon Huntb53e0682014-11-12 13:32:01 -08002260 populateMultiSelect();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002261 requestTrafficForMode();
Simon Huntb53e0682014-11-12 13:32:01 -08002262 }
2263
2264 function addSep(tbody) {
2265 var tr = tbody.append('tr');
2266 $('<hr>').appendTo(tr.append('td').attr('colspan', 2));
2267 }
2268
2269 function addProp(tbody, label, value) {
2270 var tr = tbody.append('tr');
2271
2272 tr.append('td')
2273 .attr('class', 'label')
2274 .text(label + ' :');
2275
2276 tr.append('td')
2277 .attr('class', 'value')
2278 .text(value);
2279 }
2280
2281 function populateMultiSelect() {
2282 detailPane.empty();
2283
Simon Hunta3dd9572014-11-20 15:22:41 -08002284 var title = detailPane.append('h3'),
2285 table = detailPane.append('table'),
2286 tbody = table.append('tbody');
Simon Huntb53e0682014-11-12 13:32:01 -08002287
Thomas Vachuska4731f122014-11-20 04:56:19 -08002288 title.text('Selected Nodes');
Simon Huntb53e0682014-11-12 13:32:01 -08002289
2290 selectOrder.forEach(function (d, i) {
2291 addProp(tbody, i+1, d);
2292 });
Simon Huntd72bc702014-11-13 18:38:04 -08002293
2294 addMultiSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08002295 }
2296
Thomas Vachuska47635c62014-11-22 01:21:36 -08002297 // TODO: refactor to consolidate with populateDetails
2298 function populateSummary(data) {
2299 summaryPane.empty();
2300
2301 var svg = summaryPane.append('svg'),
2302 iid = iconGlyphUrl(data);
2303
2304 var title = summaryPane.append('h2'),
2305 table = summaryPane.append('table'),
2306 tbody = table.append('tbody');
2307
2308 appendGlyph(svg, 0, 0, 40, iid);
2309
2310 svg.append('use')
2311 .attr({
2312 class: 'birdBadge',
2313 transform: translate(8,12),
2314 'xlink:href': '#bird',
2315 width: 24,
2316 height: 24,
2317 fill: '#fff'
2318 });
2319
2320 title.text('ONOS Summary');
2321
2322 data.propOrder.forEach(function(p) {
2323 if (p === '-') {
2324 addSep(tbody);
2325 } else {
2326 addProp(tbody, p, data.props[p]);
2327 }
2328 });
2329 }
2330
Simon Hunt61d04042014-11-11 17:27:16 -08002331 function populateDetails(data) {
2332 detailPane.empty();
2333
Simon Hunta6a9fe72014-11-20 11:17:12 -08002334 var svg = detailPane.append('svg'),
2335 iid = iconGlyphUrl(data);
2336
Simon Hunta3dd9572014-11-20 15:22:41 -08002337 var title = detailPane.append('h2'),
2338 table = detailPane.append('table'),
2339 tbody = table.append('tbody');
Simon Hunt61d04042014-11-11 17:27:16 -08002340
Simon Hunta6a9fe72014-11-20 11:17:12 -08002341 appendGlyph(svg, 0, 0, 40, iid);
2342 title.text(data.id);
Simon Hunt61d04042014-11-11 17:27:16 -08002343
2344 data.propOrder.forEach(function(p) {
2345 if (p === '-') {
2346 addSep(tbody);
2347 } else {
2348 addProp(tbody, p, data.props[p]);
2349 }
2350 });
Simon Huntd72bc702014-11-13 18:38:04 -08002351
Thomas Vachuska4731f122014-11-20 04:56:19 -08002352 addSingleSelectActions(data);
Simon Hunt61d04042014-11-11 17:27:16 -08002353 }
2354
Thomas Vachuska4731f122014-11-20 04:56:19 -08002355 function addSingleSelectActions(data) {
Simon Huntd72bc702014-11-13 18:38:04 -08002356 detailPane.append('hr');
2357 // always want to allow 'show traffic'
Thomas Vachuska4731f122014-11-20 04:56:19 -08002358 addAction(detailPane, 'Show Related Traffic', showTrafficAction);
2359
2360 if (data.type === 'switch') {
2361 addAction(detailPane, 'Show Device Flows', showDeviceLinkFlowsAction);
2362 }
Simon Huntd72bc702014-11-13 18:38:04 -08002363 }
2364
2365 function addMultiSelectActions() {
2366 detailPane.append('hr');
2367 // always want to allow 'show traffic'
Thomas Vachuska4731f122014-11-20 04:56:19 -08002368 addAction(detailPane, 'Show Related Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08002369 // if exactly two hosts are selected, also want 'add host intent'
2370 if (nSel() === 2 && allSelectionsClass('host')) {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002371 addAction(detailPane, 'Create Host-to-Host Flow', addHostIntentAction);
2372 } else if (nSel() >= 2 && allSelectionsClass('host')) {
2373 addAction(detailPane, 'Create Multi-Source Flow', addMultiSourceIntentAction);
Simon Huntd72bc702014-11-13 18:38:04 -08002374 }
2375 }
2376
Simon Hunta5e89142014-11-14 07:00:33 -08002377 function addAction(panel, text, cb) {
2378 panel.append('div')
Simon Huntd72bc702014-11-13 18:38:04 -08002379 .classed('actionBtn', true)
2380 .text(text)
2381 .on('click', cb);
2382 }
2383
2384
Paul Greysonfcba0e82014-11-13 10:21:16 -08002385 function zoomPan(scale, translate) {
2386 zoomPanContainer.attr("transform", "translate(" + translate + ")scale(" + scale + ")");
2387 // keep the map lines constant width while zooming
Thomas Vachuska89543292014-11-19 11:28:33 -08002388 bgImg.style("stroke-width", 2.0 / scale + "px");
Paul Greysonfcba0e82014-11-13 10:21:16 -08002389 }
2390
2391 function resetZoomPan() {
2392 zoomPan(1, [0,0]);
2393 zoom.scale(1).translate([0,0]);
2394 }
2395
2396 function setupZoomPan() {
2397 function zoomed() {
Simon Huntdeab4322014-11-13 18:49:07 -08002398 if (!panZoom() ^ !d3.event.sourceEvent.metaKey) {
Paul Greysonfcba0e82014-11-13 10:21:16 -08002399 zoomPan(d3.event.scale, d3.event.translate);
2400 }
2401 }
2402
2403 zoom = d3.behavior.zoom()
2404 .translate([0, 0])
2405 .scale(1)
2406 .scaleExtent([1, 8])
2407 .on("zoom", zoomed);
2408
2409 svg.call(zoom);
2410 }
2411
Simon Hunt61d04042014-11-11 17:27:16 -08002412 // ==============================
2413 // Test harness code
Simon Hunt56d51852014-11-09 13:03:35 -08002414
2415 function prepareScenario(view, ctx, dbg) {
2416 var sc = scenario,
2417 urlSc = sc.evDir + ctx + sc.evScenario;
2418
2419 if (!ctx) {
2420 view.alert("No scenario specified (null ctx)");
2421 return;
2422 }
2423
2424 sc.view = view;
2425 sc.ctx = ctx;
2426 sc.debug = dbg;
2427 sc.evNumber = 0;
2428
2429 d3.json(urlSc, function(err, data) {
Simon Huntbb282f52014-11-10 11:08:19 -08002430 var p = data && data.params || {},
2431 desc = data && data.description || null,
Simon Huntfc274c92014-11-11 11:05:46 -08002432 intro = data && data.title;
Simon Huntbb282f52014-11-10 11:08:19 -08002433
Simon Hunt56d51852014-11-09 13:03:35 -08002434 if (err) {
2435 view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err);
2436 } else {
2437 sc.params = p;
Simon Huntbb282f52014-11-10 11:08:19 -08002438 if (desc) {
2439 intro += '\n\n ' + desc.join('\n ');
2440 }
2441 view.alert(intro);
Simon Hunt56d51852014-11-09 13:03:35 -08002442 }
2443 });
2444
2445 }
2446
Simon Hunt01095ff2014-11-13 16:37:29 -08002447 // ==============================
2448 // Toggle Buttons in masthead
Simon Hunt0c6d4192014-11-12 12:07:10 -08002449
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002450 // TODO: toggle button (and other widgets in the masthead) should be provided
2451 // by the framework; not generated by the view.
2452
Thomas Vachuska47635c62014-11-22 01:21:36 -08002453 //var showInstances;
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002454
Simon Hunt87514342014-11-24 16:41:27 -08002455/*
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002456 function addButtonBar(view) {
2457 var bb = d3.select('#mast')
2458 .append('span').classed('right', true).attr('id', 'bb');
2459
Simon Hunta5e89142014-11-14 07:00:33 -08002460 function mkTogBtn(text, cb) {
2461 return bb.append('span')
2462 .classed('btn', true)
2463 .text(text)
2464 .on('click', cb);
2465 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002466
Thomas Vachuska47635c62014-11-22 01:21:36 -08002467 //showInstances = mkTogBtn('Show Instances', toggleInst);
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002468 }
Simon Hunt87514342014-11-24 16:41:27 -08002469*/
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002470
Simon Huntdeab4322014-11-13 18:49:07 -08002471 function panZoom() {
Simon Hunte5b71752014-11-18 20:06:07 -08002472 return false;
Simon Hunta5e89142014-11-14 07:00:33 -08002473 }
2474
Simon Hunt7fa116d2014-11-17 14:16:55 -08002475 function loadGlyphs(svg) {
2476 var defs = svg.append('defs');
2477 gly.defBird(defs);
Simon Huntc72967b2014-11-20 09:21:42 -08002478 gly.defGlyphs(defs);
Simon Huntb82f6902014-11-22 11:53:15 -08002479 gly.defBadges(defs);
Simon Hunt7fa116d2014-11-17 14:16:55 -08002480 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002481
Simon Hunt395a70c2014-11-22 23:17:40 -08002482 function sendUpdateMeta(d, store) {
2483 var metaUi = {},
2484 ll;
2485
2486 if (store) {
2487 ll = geoMapProjection.invert([d.x, d.y]);
Simon Hunt62c47542014-11-22 22:16:32 -08002488 metaUi = {
2489 x: d.x,
2490 y: d.y,
2491 lng: ll[0],
2492 lat: ll[1]
2493 };
Simon Hunt395a70c2014-11-22 23:17:40 -08002494 }
Simon Hunt62c47542014-11-22 22:16:32 -08002495 d.metaUi = metaUi;
2496 sendMessage('updateMeta', {
2497 id: d.id,
2498 'class': d.class,
2499 'memento': metaUi
2500 });
2501 }
2502
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002503 // ==============================
Simon Hunt142d0032014-11-04 20:13:09 -08002504 // View life-cycle callbacks
Simon Hunt195cb382014-11-03 17:50:51 -08002505
Simon Huntf67722a2014-11-10 09:32:06 -08002506 function preload(view, ctx, flags) {
Simon Hunt142d0032014-11-04 20:13:09 -08002507 var w = view.width(),
2508 h = view.height(),
Simon Huntc7ee0662014-11-05 16:44:37 -08002509 fcfg = config.force,
2510 fpad = fcfg.pad,
2511 forceDim = [w - 2*fpad, h - 2*fpad];
Simon Hunt195cb382014-11-03 17:50:51 -08002512
Simon Hunt142d0032014-11-04 20:13:09 -08002513 // NOTE: view.$div is a D3 selection of the view's div
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002514 var viewBox = '0 0 ' + config.logicalSize + ' ' + config.logicalSize;
2515 svg = view.$div.append('svg').attr('viewBox', viewBox);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002516 setSize(svg, view);
2517
Simon Hunt7fa116d2014-11-17 14:16:55 -08002518 loadGlyphs(svg);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002519
Paul Greysonfcba0e82014-11-13 10:21:16 -08002520 zoomPanContainer = svg.append('g').attr('id', 'zoomPanContainer');
Paul Greysonfcba0e82014-11-13 10:21:16 -08002521 setupZoomPan();
2522
Simon Hunt1a9eff92014-11-07 11:06:34 -08002523 // add blue glow filter to svg layer
Paul Greysonfcba0e82014-11-13 10:21:16 -08002524 d3u.appendGlow(zoomPanContainer);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002525
Simon Huntc7ee0662014-11-05 16:44:37 -08002526 // group for the topology
Paul Greysonfcba0e82014-11-13 10:21:16 -08002527 topoG = zoomPanContainer.append('g')
Simon Huntd3b7d512014-11-12 15:48:41 -08002528 .attr('id', 'topo-G')
Simon Huntc7ee0662014-11-05 16:44:37 -08002529 .attr('transform', fcfg.translate());
2530
Simon Hunte2575b62014-11-18 15:25:53 -08002531 // subgroups for links, link labels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002532 linkG = topoG.append('g').attr('id', 'links');
Simon Hunte2575b62014-11-18 15:25:53 -08002533 linkLabelG = topoG.append('g').attr('id', 'linkLabels');
Simon Huntc7ee0662014-11-05 16:44:37 -08002534 nodeG = topoG.append('g').attr('id', 'nodes');
2535
Simon Hunte2575b62014-11-18 15:25:53 -08002536 // selection of links, linkLabels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002537 link = linkG.selectAll('.link');
Simon Hunte2575b62014-11-18 15:25:53 -08002538 linkLabel = linkLabelG.selectAll('.linkLabel');
Simon Huntc7ee0662014-11-05 16:44:37 -08002539 node = nodeG.selectAll('.node');
2540
Simon Hunt7cd48f32014-11-09 23:42:50 -08002541 function chrg(d) {
2542 return fcfg.charge[d.class] || -12000;
2543 }
Simon Hunt99c13842014-11-06 18:23:12 -08002544 function ldist(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002545 return fcfg.linkDistance[d.type] || 50;
Simon Hunt99c13842014-11-06 18:23:12 -08002546 }
2547 function lstrg(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002548 // 0.0 - 1.0
2549 return fcfg.linkStrength[d.type] || 1.0;
Simon Hunt99c13842014-11-06 18:23:12 -08002550 }
2551
Simon Hunt1a9eff92014-11-07 11:06:34 -08002552 function selectCb(d, self) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002553 selectObject(d, self);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002554 }
2555
2556 function atDragEnd(d, self) {
Simon Hunt56d51852014-11-09 13:03:35 -08002557 // once we've finished moving, pin the node in position
2558 d.fixed = true;
2559 d3.select(self).classed('fixed', true);
2560 if (config.useLiveData) {
Simon Hunt395a70c2014-11-22 23:17:40 -08002561 sendUpdateMeta(d, true);
Simon Hunta255a2c2014-11-13 22:29:35 -08002562 } else {
2563 console.log('Moving node ' + d.id + ' to [' + d.x + ',' + d.y + ']');
Simon Hunt1a9eff92014-11-07 11:06:34 -08002564 }
2565 }
2566
Simon Huntc7ee0662014-11-05 16:44:37 -08002567 // set up the force layout
2568 network.force = d3.layout.force()
2569 .size(forceDim)
2570 .nodes(network.nodes)
2571 .links(network.links)
Simon Hunt7cd48f32014-11-09 23:42:50 -08002572 .gravity(0.4)
2573 .friction(0.7)
2574 .charge(chrg)
Simon Hunt99c13842014-11-06 18:23:12 -08002575 .linkDistance(ldist)
2576 .linkStrength(lstrg)
Simon Huntc7ee0662014-11-05 16:44:37 -08002577 .on('tick', tick);
Simon Hunt195cb382014-11-03 17:50:51 -08002578
Simon Hunt01095ff2014-11-13 16:37:29 -08002579 network.drag = d3u.createDragBehavior(network.force,
Simon Huntdeab4322014-11-13 18:49:07 -08002580 selectCb, atDragEnd, panZoom);
Simon Hunt0c6d4192014-11-12 12:07:10 -08002581
2582 // create mask layer for when we lose connection to server.
Simon Hunta5e89142014-11-14 07:00:33 -08002583 // TODO: this should be part of the framework
Simon Hunt0c6d4192014-11-12 12:07:10 -08002584 mask = view.$div.append('div').attr('id','topo-mask');
2585 para(mask, 'Oops!');
2586 para(mask, 'Web-socket connection to server closed...');
2587 para(mask, 'Try refreshing the page.');
Simon Hunt12ce12e2014-11-15 21:13:19 -08002588
2589 mask.append('svg')
2590 .attr({
2591 id: 'mask-bird',
2592 width: w,
2593 height: h
2594 })
2595 .append('g')
2596 .attr('transform', birdTranslate(w, h))
2597 .style('opacity', 0.3)
2598 .append('use')
2599 .attr({
2600 'xlink:href': '#bird',
2601 width: config.birdDim,
2602 height: config.birdDim,
2603 fill: '#111'
Thomas Vachuska89543292014-11-19 11:28:33 -08002604 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08002605 }
Simon Hunt195cb382014-11-03 17:50:51 -08002606
Simon Hunt01095ff2014-11-13 16:37:29 -08002607 function para(sel, text) {
2608 sel.append('p').text(text);
2609 }
2610
2611
Simon Hunt56d51852014-11-09 13:03:35 -08002612 function load(view, ctx, flags) {
Simon Huntf67722a2014-11-10 09:32:06 -08002613 // resize, in case the window was resized while we were not loaded
2614 resize(view, ctx, flags);
2615
Simon Hunt99c13842014-11-06 18:23:12 -08002616 // cache the view token, so network topo functions can access it
2617 network.view = view;
Simon Hunt56d51852014-11-09 13:03:35 -08002618 config.useLiveData = !flags.local;
2619
2620 if (!config.useLiveData) {
2621 prepareScenario(view, ctx, flags.debug);
2622 }
Simon Hunt99c13842014-11-06 18:23:12 -08002623
2624 // set our radio buttons and key bindings
Simon Hunt9462e8c2014-11-14 17:28:09 -08002625 layerBtnSet = view.setRadio(layerButtons);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002626 view.setKeys(keyDispatch);
Simon Hunt87514342014-11-24 16:41:27 -08002627 view.setGestures(gestures);
Simon Hunt195cb382014-11-03 17:50:51 -08002628
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002629 // patch in our "button bar" for now
2630 // TODO: implement a more official frameworky way of doing this..
Simon Hunt87514342014-11-24 16:41:27 -08002631 //addButtonBar(view);
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002632
Simon Huntd3b7d512014-11-12 15:48:41 -08002633 // Load map data asynchronously; complete startup after that..
2634 loadGeoJsonData();
Simon Hunta255a2c2014-11-13 22:29:35 -08002635 }
2636
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002637 function startAntTimer() {
Thomas Vachuska4731f122014-11-20 04:56:19 -08002638 if (!antTimer) {
2639 var pulses = [5, 3, 1.2, 3],
2640 pulse = 0;
2641 antTimer = setInterval(function () {
2642 pulse = pulse + 1;
2643 pulse = pulse === pulses.length ? 0 : pulse;
2644 d3.selectAll('.animated').style('stroke-width', pulses[pulse]);
2645 }, 200);
2646 }
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002647 }
2648
2649 function stopAntTimer() {
Simon Hunta255a2c2014-11-13 22:29:35 -08002650 if (antTimer) {
2651 clearInterval(antTimer);
2652 antTimer = null;
2653 }
Simon Huntd3b7d512014-11-12 15:48:41 -08002654 }
2655
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002656 function unload(view, ctx, flags) {
2657 stopAntTimer();
2658 }
2659
Simon Huntd3b7d512014-11-12 15:48:41 -08002660 // TODO: move these to config/state portion of script
Simon Hunta6a9fe72014-11-20 11:17:12 -08002661 var geoJsonUrl = 'json/map/continental_us.json',
Simon Huntd3b7d512014-11-12 15:48:41 -08002662 geoJson;
2663
2664 function loadGeoJsonData() {
2665 d3.json(geoJsonUrl, function (err, data) {
2666 if (err) {
2667 // fall back to USA map background
2668 loadStaticMap();
2669 } else {
2670 geoJson = data;
2671 loadGeoMap();
2672 }
2673
2674 // finally, connect to the server...
2675 if (config.useLiveData) {
2676 webSock.connect();
2677 }
2678 });
2679 }
2680
2681 function showBg() {
Simon Hunt434cf142014-11-24 11:10:28 -08002682 return visVal(config.options.showBackground);
Simon Huntd3b7d512014-11-12 15:48:41 -08002683 }
2684
2685 function loadStaticMap() {
2686 fnTrace('loadStaticMap', config.backgroundUrl);
2687 var w = network.view.width(),
2688 h = network.view.height();
2689
2690 // load the background image
2691 bgImg = svg.insert('svg:image', '#topo-G')
2692 .attr({
2693 id: 'topo-bg',
2694 width: w,
2695 height: h,
2696 'xlink:href': config.backgroundUrl
2697 })
2698 .style({
2699 visibility: showBg()
2700 });
2701 }
2702
2703 function loadGeoMap() {
2704 fnTrace('loadGeoMap', geoJsonUrl);
Simon Huntd3b7d512014-11-12 15:48:41 -08002705
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002706 // extracts the topojson data into geocoordinate-based geometry
2707 var topoData = topojson.feature(geoJson, geoJson.objects.states);
Simon Huntd3b7d512014-11-12 15:48:41 -08002708
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002709 // see: http://bl.ocks.org/mbostock/4707858
2710 geoMapProjection = d3.geo.mercator();
2711 var path = d3.geo.path().projection(geoMapProjection);
Simon Huntd3b7d512014-11-12 15:48:41 -08002712
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002713 geoMapProjection
2714 .scale(1)
2715 .translate([0, 0]);
Simon Huntd3b7d512014-11-12 15:48:41 -08002716
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002717 // [[x1,y1],[x2,y2]]
2718 var b = path.bounds(topoData);
Paul Greysonfcba0e82014-11-13 10:21:16 -08002719 // size map to 95% of minimum dimension to fill space
2720 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 -08002721 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 -08002722
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002723 geoMapProjection
2724 .scale(s)
2725 .translate(t);
2726
Paul Greysonfcba0e82014-11-13 10:21:16 -08002727 bgImg = zoomPanContainer.insert("g", '#topo-G');
Thomas Vachuska89543292014-11-19 11:28:33 -08002728 bgImg.attr('id', 'map').selectAll('path')
2729 .data(topoData.features)
2730 .enter()
2731 .append('path')
2732 .attr('d', path);
Simon Hunt195cb382014-11-03 17:50:51 -08002733 }
2734
Simon Huntf67722a2014-11-10 09:32:06 -08002735 function resize(view, ctx, flags) {
Simon Hunt12ce12e2014-11-15 21:13:19 -08002736 var w = view.width(),
2737 h = view.height();
2738
Simon Hunt934c3ce2014-11-05 11:45:07 -08002739 setSize(svg, view);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002740
2741 d3.select('#mask-bird').attr({ width: w, height: h})
2742 .select('g').attr('transform', birdTranslate(w, h));
Simon Hunt142d0032014-11-04 20:13:09 -08002743 }
2744
Simon Hunt8f40cce2014-11-23 15:57:30 -08002745 function theme(view, ctx, flags) {
2746 updateInstances();
Simon Huntb0ecfa52014-11-23 21:05:12 -08002747 updateDeviceColors();
Simon Hunt8f40cce2014-11-23 15:57:30 -08002748 }
2749
Simon Hunt12ce12e2014-11-15 21:13:19 -08002750 function birdTranslate(w, h) {
2751 var bdim = config.birdDim;
2752 return 'translate('+((w-bdim)*.4)+','+((h-bdim)*.1)+')';
2753 }
Simon Hunt142d0032014-11-04 20:13:09 -08002754
Simon Hunt06811b72014-11-25 18:54:48 -08002755 function isF(f) { return $.isFunction(f) ? f : null; }
2756 function noop() {}
2757
2758 function augmentDetailPane() {
2759 var dp = detailPane;
2760 dp.ypos = { up: 64, down: 320, current: 320};
2761
2762 dp._move = function (y, cb) {
2763 var endCb = isF(cb) || noop,
2764 yp = dp.ypos;
2765 if (yp.current !== y) {
2766 yp.current = y;
2767 dp.el.transition().duration(300)
2768 .each('end', endCb)
2769 .style('top', yp.current + 'px');
2770 } else {
2771 endCb();
2772 }
2773 };
2774
2775 dp.down = function (cb) { dp._move(dp.ypos.down, cb); };
2776 dp.up = function (cb) { dp._move(dp.ypos.up, cb); };
2777 }
2778
Simon Hunt142d0032014-11-04 20:13:09 -08002779 // ==============================
2780 // View registration
Simon Hunt195cb382014-11-03 17:50:51 -08002781
Simon Hunt25248912014-11-04 11:25:48 -08002782 onos.ui.addView('topo', {
Simon Hunt142d0032014-11-04 20:13:09 -08002783 preload: preload,
2784 load: load,
Simon Hunta255a2c2014-11-13 22:29:35 -08002785 unload: unload,
Simon Hunt8f40cce2014-11-23 15:57:30 -08002786 resize: resize,
2787 theme: theme
Simon Hunt195cb382014-11-03 17:50:51 -08002788 });
2789
Thomas Vachuska47635c62014-11-22 01:21:36 -08002790 summaryPane = onos.ui.addFloatingPanel('topo-summary');
Simon Hunt61d04042014-11-11 17:27:16 -08002791 detailPane = onos.ui.addFloatingPanel('topo-detail');
Simon Hunt06811b72014-11-25 18:54:48 -08002792 augmentDetailPane();
Simon Hunta5e89142014-11-14 07:00:33 -08002793 oiBox = onos.ui.addFloatingPanel('topo-oibox', 'TL');
Simon Huntb82f6902014-11-22 11:53:15 -08002794 oiBox.width(20);
Simon Hunt61d04042014-11-11 17:27:16 -08002795
Simon Hunt195cb382014-11-03 17:50:51 -08002796}(ONOS));