blob: 6673b288fbb9bdc834c8e0e0c4313e5affcf84db [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 Hunt6d9bd032014-11-28 22:16:40 -0800148 M: [toggleOffline, 'Toggle offline visibility'],
Simon Hunt27d322d2014-11-28 10:45:43 -0800149 L: [cycleLabels, 'Cycle device labels'],
Simon Hunt934c3ce2014-11-05 11:45:07 -0800150 P: togglePorts,
Simon Hunt87514342014-11-24 16:41:27 -0800151 U: [unpin, 'Unpin node (hover mouse over)'],
152 R: [resetZoomPan, 'Reset zoom / pan'],
Thomas Vachuska9edca302014-11-22 17:06:42 -0800153 V: [showTrafficAction, 'Show related traffic'],
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800154 A: [showAllTrafficAction, 'Show all traffic'],
155 F: [showDeviceLinkFlowsAction, 'Show device link flows'],
Simon Hunt9462e8c2014-11-14 17:28:09 -0800156 esc: handleEscape
Simon Hunt934c3ce2014-11-05 11:45:07 -0800157 };
Simon Hunt142d0032014-11-04 20:13:09 -0800158
Simon Hunt87514342014-11-24 16:41:27 -0800159 // mouse gestures
160 var gestures = [
161 ['click', 'Select the item and show details'],
162 ['shift-click', 'Toggle selection state'],
163 ['drag', 'Reposition (and pin) device / host'],
164 ['cmd-scroll', 'Zoom in / out'],
165 ['cmd-drag', 'Pan']
166 ];
167
Simon Hunt195cb382014-11-03 17:50:51 -0800168 // state variables
Simon Hunt99c13842014-11-06 18:23:12 -0800169 var network = {
Simon Hunt50128c02014-11-08 13:36:15 -0800170 view: null, // view token reference
Simon Hunt99c13842014-11-06 18:23:12 -0800171 nodes: [],
172 links: [],
Simon Hunt269670f2014-11-17 16:17:43 -0800173 lookup: {},
174 revLinkToKey: {}
Simon Hunt99c13842014-11-06 18:23:12 -0800175 },
Simon Hunt56d51852014-11-09 13:03:35 -0800176 scenario = {
177 evDir: 'json/ev/',
178 evScenario: '/scenario.json',
179 evPrefix: '/ev_',
180 evOnos: '_onos.json',
181 evUi: '_ui.json',
182 ctx: null,
183 params: {},
184 evNumber: 0,
Simon Hunt434cf142014-11-24 11:10:28 -0800185 view: null
Simon Hunt56d51852014-11-09 13:03:35 -0800186 },
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800187 webSock,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800188 sid = 0,
Thomas Vachuska60d72bf2014-11-21 13:02:00 -0800189 deviceLabelCount = 3,
Simon Hunt209155e2014-11-21 12:16:09 -0800190 hostLabelCount = 2,
Simon Hunt56d51852014-11-09 13:03:35 -0800191 deviceLabelIndex = 0,
192 hostLabelIndex = 0,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800193 selections = {},
Simon Hunta5e89142014-11-14 07:00:33 -0800194 selectOrder = [],
Simon Hunt6ac93f32014-11-13 12:17:27 -0800195 hovered = null,
Thomas Vachuska47635c62014-11-22 01:21:36 -0800196 summaryPane,
Simon Hunta5e89142014-11-14 07:00:33 -0800197 detailPane,
Simon Hunta255a2c2014-11-13 22:29:35 -0800198 antTimer = null,
Simon Hunta5e89142014-11-14 07:00:33 -0800199 onosInstances = {},
200 onosOrder = [],
201 oiBox,
Simon Hunt9462e8c2014-11-14 17:28:09 -0800202 oiShowMaster = false,
Simon Hunt8f40cce2014-11-23 15:57:30 -0800203 portLabelsOn = false,
Simon Huntb0ecfa52014-11-23 21:05:12 -0800204 cat7 = d3u.cat7(),
Simon Hunt434cf142014-11-24 11:10:28 -0800205 colorAffinity = false,
Simon Hunt27d322d2014-11-28 10:45:43 -0800206 showHosts = false,
Simon Hunt6d9bd032014-11-28 22:16:40 -0800207 showOffline = true,
Simon Hunt27d322d2014-11-28 10:45:43 -0800208 useDetails = true,
Simon Hunt6e18fe32014-11-29 13:35:41 -0800209 haveDetails = false,
210 dragAllowed = true;
Simon Hunt195cb382014-11-03 17:50:51 -0800211
Simon Hunt434cf142014-11-24 11:10:28 -0800212 // constants
Thomas Vachuska9edca302014-11-22 17:06:42 -0800213 var hoverModeAll = 1,
214 hoverModeFlows = 2,
215 hoverModeIntents = 3,
216 hoverMode = hoverModeFlows;
217
Simon Hunt934c3ce2014-11-05 11:45:07 -0800218 // D3 selections
219 var svg,
Paul Greysonfcba0e82014-11-13 10:21:16 -0800220 zoomPanContainer,
Simon Hunt934c3ce2014-11-05 11:45:07 -0800221 bgImg,
Simon Huntc7ee0662014-11-05 16:44:37 -0800222 topoG,
223 nodeG,
224 linkG,
Simon Hunte2575b62014-11-18 15:25:53 -0800225 linkLabelG,
Simon Huntc7ee0662014-11-05 16:44:37 -0800226 node,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800227 link,
Simon Hunte2575b62014-11-18 15:25:53 -0800228 linkLabel,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800229 mask;
Simon Hunt195cb382014-11-03 17:50:51 -0800230
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800231 // the projection for the map background
232 var geoMapProjection;
233
Paul Greysonfcba0e82014-11-13 10:21:16 -0800234 // the zoom function
235 var zoom;
236
Simon Hunt142d0032014-11-04 20:13:09 -0800237 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800238 // For Debugging / Development
Simon Hunt195cb382014-11-03 17:50:51 -0800239
Simon Hunt99c13842014-11-06 18:23:12 -0800240 function note(label, msg) {
241 console.log('NOTE: ' + label + ': ' + msg);
Simon Hunt195cb382014-11-03 17:50:51 -0800242 }
243
Simon Hunt99c13842014-11-06 18:23:12 -0800244 function debug(what) {
245 return config.debugOn && config.debug[what];
Simon Hunt934c3ce2014-11-05 11:45:07 -0800246 }
247
Simon Huntfc274c92014-11-11 11:05:46 -0800248 function fnTrace(msg, id) {
249 if (config.fnTrace) {
250 console.log('FN: ' + msg + ' [' + id + ']');
251 }
252 }
Simon Hunt99c13842014-11-06 18:23:12 -0800253
Simon Hunta5e89142014-11-14 07:00:33 -0800254 function evTrace(data) {
255 fnTrace(data.event, data.payload.id);
256 }
257
Simon Hunt934c3ce2014-11-05 11:45:07 -0800258 // ==============================
259 // Key Callbacks
260
Simon Hunt27d322d2014-11-28 10:45:43 -0800261 function flash(txt) {
262 network.view.flash(txt);
263 }
264
Simon Hunt99c13842014-11-06 18:23:12 -0800265 function testMe(view) {
Simon Hunt8f40cce2014-11-23 15:57:30 -0800266 //view.alert('Theme is ' + view.getTheme());
Simon Hunta3dd9572014-11-20 15:22:41 -0800267 //view.flash('This is some text');
Simon Hunt8f40cce2014-11-23 15:57:30 -0800268 cat7.testCard(svg);
Simon Hunt99c13842014-11-06 18:23:12 -0800269 }
270
Simon Hunt56d51852014-11-09 13:03:35 -0800271 function injectTestEvent(view) {
Simon Hunt434cf142014-11-24 11:10:28 -0800272 if (config.useLiveData) { return; }
273
Simon Hunt56d51852014-11-09 13:03:35 -0800274 var sc = scenario,
275 evn = ++sc.evNumber,
276 pfx = sc.evDir + sc.ctx + sc.evPrefix + evn,
277 onosUrl = pfx + sc.evOnos,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800278 uiUrl = pfx + sc.evUi,
279 stack = [
280 { url: onosUrl, cb: handleServerEvent },
281 { url: uiUrl, cb: handleUiEvent }
282 ];
283 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800284 }
285
Simon Hunt7cd48f32014-11-09 23:42:50 -0800286 function recurseFetchEvent(stack, evn) {
287 var v = scenario.view,
288 frame;
289 if (stack.length === 0) {
Simon Huntfc274c92014-11-11 11:05:46 -0800290 v.alert('Oops!\n\nNo event #' + evn + ' found.');
Simon Hunt7cd48f32014-11-09 23:42:50 -0800291 return;
292 }
293 frame = stack.shift();
294
295 d3.json(frame.url, function (err, data) {
Simon Hunt99c13842014-11-06 18:23:12 -0800296 if (err) {
Simon Hunt56d51852014-11-09 13:03:35 -0800297 if (err.status === 404) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800298 // if we didn't find the data, try the next stack frame
299 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800300 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800301 v.alert('non-404 error:\n\n' + frame.url + '\n\n' + err);
Simon Hunt56d51852014-11-09 13:03:35 -0800302 }
Simon Hunt99c13842014-11-06 18:23:12 -0800303 } else {
Simon Hunt1712ed82014-11-17 12:56:00 -0800304 wsTrace('test', JSON.stringify(data));
Simon Hunt7cd48f32014-11-09 23:42:50 -0800305 frame.cb(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800306 }
307 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800308
Simon Hunt56d51852014-11-09 13:03:35 -0800309 }
Simon Hunt50128c02014-11-08 13:36:15 -0800310
Simon Hunt56d51852014-11-09 13:03:35 -0800311 function handleUiEvent(data) {
Simon Huntbb282f52014-11-10 11:08:19 -0800312 scenario.view.alert('UI Tx: ' + data.event + '\n\n' +
313 JSON.stringify(data));
Simon Hunt56d51852014-11-09 13:03:35 -0800314 }
315
316 function injectStartupEvents(view) {
317 var last = scenario.params.lastAuto || 0;
Simon Hunt434cf142014-11-24 11:10:28 -0800318 if (config.useLiveData) { return; }
Simon Hunt56d51852014-11-09 13:03:35 -0800319
320 while (scenario.evNumber < last) {
Simon Hunt1a9eff92014-11-07 11:06:34 -0800321 injectTestEvent(view);
322 }
323 }
324
Simon Hunt934c3ce2014-11-05 11:45:07 -0800325 function toggleBg() {
326 var vis = bgImg.style('visibility');
Simon Hunt434cf142014-11-24 11:10:28 -0800327 bgImg.style('visibility', visVal(vis === 'hidden'));
328 }
329
330 function toggleHosts() {
331 showHosts = !showHosts;
332 updateHostVisibility();
Simon Hunt27d322d2014-11-28 10:45:43 -0800333 flash('Hosts ' + visVal(showHosts));
Simon Hunt934c3ce2014-11-05 11:45:07 -0800334 }
335
Simon Hunt6d9bd032014-11-28 22:16:40 -0800336 function toggleOffline() {
337 showOffline = !showOffline;
338 updateOfflineVisibility();
339 flash('Offline devices ' + visVal(showOffline));
340 }
341
Simon Hunt99c13842014-11-06 18:23:12 -0800342 function cycleLabels() {
Thomas Vachuska60d72bf2014-11-21 13:02:00 -0800343 deviceLabelIndex = (deviceLabelIndex === 2)
Simon Huntbb282f52014-11-10 11:08:19 -0800344 ? 0 : deviceLabelIndex + 1;
Simon Hunt5f36d342014-11-08 21:33:14 -0800345
Simon Hunt99c13842014-11-06 18:23:12 -0800346 network.nodes.forEach(function (d) {
Simon Huntbb282f52014-11-10 11:08:19 -0800347 if (d.class === 'device') {
348 updateDeviceLabel(d);
349 }
Simon Hunt99c13842014-11-06 18:23:12 -0800350 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800351 }
352
353 function togglePorts(view) {
Simon Hunt434cf142014-11-24 11:10:28 -0800354 //view.alert('togglePorts() callback')
Simon Hunt934c3ce2014-11-05 11:45:07 -0800355 }
356
Simon Hunt6ac93f32014-11-13 12:17:27 -0800357 function unpin() {
358 if (hovered) {
Simon Hunt395a70c2014-11-22 23:17:40 -0800359 sendUpdateMeta(hovered);
Simon Hunt6ac93f32014-11-13 12:17:27 -0800360 hovered.fixed = false;
361 hovered.el.classed('fixed', false);
362 network.force.resume();
363 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800364 }
365
Simon Hunt9462e8c2014-11-14 17:28:09 -0800366 function handleEscape(view) {
367 if (oiShowMaster) {
368 cancelAffinity();
Simon Hunt27d322d2014-11-28 10:45:43 -0800369 } else if (haveDetails) {
Simon Hunt9462e8c2014-11-14 17:28:09 -0800370 deselectAll();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800371 } else if (oiBox.isVisible()) {
Simon Huntb0ecfa52014-11-23 21:05:12 -0800372 hideInstances();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800373 } else if (summaryPane.isVisible()) {
374 cancelSummary();
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800375 stopAntTimer();
376 } else {
377 hoverMode = hoverModeFlows;
Simon Hunt9462e8c2014-11-14 17:28:09 -0800378 }
379 }
380
Simon Hunt934c3ce2014-11-05 11:45:07 -0800381 // ==============================
382 // Radio Button Callbacks
383
Simon Hunta5e89142014-11-14 07:00:33 -0800384 var layerLookup = {
385 host: {
Simon Hunt209155e2014-11-21 12:16:09 -0800386 endstation: 'pkt', // default, if host event does not define type
Thomas Vachuska89543292014-11-19 11:28:33 -0800387 router: 'pkt',
Simon Hunta5e89142014-11-14 07:00:33 -0800388 bgpSpeaker: 'pkt'
389 },
390 device: {
391 switch: 'pkt',
392 roadm: 'opt'
393 },
394 link: {
395 hostLink: 'pkt',
396 direct: 'pkt',
Simon Hunt8257f4c2014-11-16 19:34:54 -0800397 indirect: '',
398 tunnel: '',
Simon Hunta5e89142014-11-14 07:00:33 -0800399 optical: 'opt'
400 }
401 };
402
403 function inLayer(d, layer) {
Simon Hunt8257f4c2014-11-16 19:34:54 -0800404 var type = d.class === 'link' ? d.type() : d.type,
405 look = layerLookup[d.class],
406 lyr = look && look[type];
Simon Hunta5e89142014-11-14 07:00:33 -0800407 return lyr === layer;
408 }
409
410 function unsuppressLayer(which) {
411 node.each(function (d) {
412 var node = d.el;
413 if (inLayer(d, which)) {
414 node.classed('suppressed', false);
415 }
416 });
417
418 link.each(function (d) {
419 var link = d.el;
420 if (inLayer(d, which)) {
421 link.classed('suppressed', false);
422 }
423 });
424 }
425
Simon Hunt9462e8c2014-11-14 17:28:09 -0800426 function suppressLayers(b) {
427 node.classed('suppressed', b);
428 link.classed('suppressed', b);
Simon Hunt142d0032014-11-04 20:13:09 -0800429// d3.selectAll('svg .port').classed('inactive', false);
430// d3.selectAll('svg .portText').classed('inactive', false);
Simon Hunt195cb382014-11-03 17:50:51 -0800431 }
432
Simon Hunt9462e8c2014-11-14 17:28:09 -0800433 function showAllLayers() {
434 suppressLayers(false);
435 }
436
Simon Hunt195cb382014-11-03 17:50:51 -0800437 function showPacketLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800438 node.classed('suppressed', true);
439 link.classed('suppressed', true);
440 unsuppressLayer('pkt');
Simon Hunt195cb382014-11-03 17:50:51 -0800441 }
442
443 function showOpticalLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800444 node.classed('suppressed', true);
445 link.classed('suppressed', true);
446 unsuppressLayer('opt');
Simon Hunt195cb382014-11-03 17:50:51 -0800447 }
448
Simon Hunt9462e8c2014-11-14 17:28:09 -0800449 function restoreLayerState() {
450 layerBtnDispatch[layerBtnSet.selected()]();
451 }
452
Simon Hunt142d0032014-11-04 20:13:09 -0800453 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800454 // Private functions
455
Simon Hunt99c13842014-11-06 18:23:12 -0800456 function safeId(s) {
457 return s.replace(/[^a-z0-9]/gi, '-');
458 }
459
Simon Huntc7ee0662014-11-05 16:44:37 -0800460 // set the size of the given element to that of the view (reduced if padded)
461 function setSize(el, view, pad) {
462 var padding = pad ? pad * 2 : 0;
Simon Hunt934c3ce2014-11-05 11:45:07 -0800463 el.attr({
Simon Huntc7ee0662014-11-05 16:44:37 -0800464 width: view.width() - padding,
465 height: view.height() - padding
Simon Hunt934c3ce2014-11-05 11:45:07 -0800466 });
467 }
468
Simon Hunt8257f4c2014-11-16 19:34:54 -0800469 function makeNodeKey(d, what) {
470 var port = what + 'Port';
471 return d[what] + '/' + d[port];
472 }
473
474 function makeLinkKey(d, flipped) {
475 var one = flipped ? makeNodeKey(d, 'dst') : makeNodeKey(d, 'src'),
476 two = flipped ? makeNodeKey(d, 'src') : makeNodeKey(d, 'dst');
477 return one + '-' + two;
478 }
479
Simon Hunt269670f2014-11-17 16:17:43 -0800480 function findLinkById(id) {
481 // check to see if this is a reverse lookup, else default to given id
482 var key = network.revLinkToKey[id] || id;
483 return key && network.lookup[key];
484 }
485
Simon Hunt8257f4c2014-11-16 19:34:54 -0800486 function findLink(linkData, op) {
487 var key = makeLinkKey(linkData),
488 keyrev = makeLinkKey(linkData, 1),
489 link = network.lookup[key],
490 linkRev = network.lookup[keyrev],
491 result = {},
492 ldata = link || linkRev,
493 rawLink;
494
495 if (op === 'add') {
496 if (link) {
497 // trying to add a link that we already know about
498 result.ldata = link;
499 result.badLogic = 'addLink: link already added';
500
501 } else if (linkRev) {
502 // we found the reverse of the link to be added
503 result.ldata = linkRev;
504 if (linkRev.fromTarget) {
505 result.badLogic = 'addLink: link already added';
506 }
507 }
508 } else if (op === 'update') {
509 if (!ldata) {
510 result.badLogic = 'updateLink: link not found';
511 } else {
512 rawLink = link ? ldata.fromSource : ldata.fromTarget;
513 result.updateWith = function (data) {
514 $.extend(rawLink, data);
515 restyleLinkElement(ldata);
516 }
517 }
518 } else if (op === 'remove') {
519 if (!ldata) {
520 result.badLogic = 'removeLink: link not found';
521 } else {
522 rawLink = link ? ldata.fromSource : ldata.fromTarget;
523
524 if (!rawLink) {
525 result.badLogic = 'removeLink: link not found';
526
527 } else {
528 result.removeRawLink = function () {
529 if (link) {
530 // remove fromSource
531 ldata.fromSource = null;
532 if (ldata.fromTarget) {
533 // promote target into source position
534 ldata.fromSource = ldata.fromTarget;
535 ldata.fromTarget = null;
536 ldata.key = keyrev;
537 delete network.lookup[key];
538 network.lookup[keyrev] = ldata;
Simon Hunt269670f2014-11-17 16:17:43 -0800539 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800540 }
541 } else {
542 // remove fromTarget
543 ldata.fromTarget = null;
Simon Hunt269670f2014-11-17 16:17:43 -0800544 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800545 }
546 if (ldata.fromSource) {
547 restyleLinkElement(ldata);
548 } else {
549 removeLinkElement(ldata);
550 }
551 }
552 }
553 }
554 }
555 return result;
556 }
557
558 function addLinkUpdate(ldata, link) {
559 // add link event, but we already have the reverse link installed
560 ldata.fromTarget = link;
Simon Hunt269670f2014-11-17 16:17:43 -0800561 network.revLinkToKey[link.id] = ldata.key;
Simon Hunt8257f4c2014-11-16 19:34:54 -0800562 restyleLinkElement(ldata);
563 }
564
565 var allLinkTypes = 'direct indirect optical tunnel',
566 defaultLinkType = 'direct';
567
568 function restyleLinkElement(ldata) {
569 // this fn's job is to look at raw links and decide what svg classes
570 // need to be applied to the line element in the DOM
571 var el = ldata.el,
572 type = ldata.type(),
573 lw = ldata.linkWidth(),
574 online = ldata.online();
575
576 el.classed('link', true);
577 el.classed('inactive', !online);
578 el.classed(allLinkTypes, false);
579 if (type) {
580 el.classed(type, true);
581 }
582 el.transition()
583 .duration(1000)
Thomas Vachuska89543292014-11-19 11:28:33 -0800584 .attr('stroke-width', linkScale(lw))
585 .attr('stroke', config.topo.linkBaseColor);
Simon Hunt8257f4c2014-11-16 19:34:54 -0800586 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800587
Simon Hunt99c13842014-11-06 18:23:12 -0800588 // ==============================
589 // Event handlers for server-pushed events
590
Simon Huntbb282f52014-11-10 11:08:19 -0800591 function logicError(msg) {
592 // TODO, report logic error to server, via websock, so it can be logged
Simon Huntfc274c92014-11-11 11:05:46 -0800593 console.warn(msg);
Simon Huntbb282f52014-11-10 11:08:19 -0800594 }
595
Simon Hunt99c13842014-11-06 18:23:12 -0800596 var eventDispatch = {
Simon Hunta5e89142014-11-14 07:00:33 -0800597 addInstance: addInstance,
Simon Hunt99c13842014-11-06 18:23:12 -0800598 addDevice: addDevice,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800599 addLink: addLink,
Simon Hunt56d51852014-11-09 13:03:35 -0800600 addHost: addHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800601
Simon Huntfcfb46c2014-11-19 12:53:38 -0800602 updateInstance: updateInstance,
Simon Huntbb282f52014-11-10 11:08:19 -0800603 updateDevice: updateDevice,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800604 updateLink: updateLink,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800605 updateHost: updateHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800606
Simon Hunt7b403bc2014-11-22 19:01:00 -0800607 removeInstance: removeInstance,
Simon Huntca867ac2014-11-28 18:07:35 -0800608 removeDevice: removeDevice,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800609 removeLink: removeLink,
Simon Hunt44031102014-11-11 13:20:36 -0800610 removeHost: removeHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800611
Simon Hunt61d04042014-11-11 17:27:16 -0800612 showDetails: showDetails,
Thomas Vachuska47635c62014-11-22 01:21:36 -0800613 showSummary: showSummary,
Simon Huntb53e0682014-11-12 13:32:01 -0800614 showTraffic: showTraffic
Simon Hunt99c13842014-11-06 18:23:12 -0800615 };
616
Simon Hunta5e89142014-11-14 07:00:33 -0800617 function addInstance(data) {
618 evTrace(data);
619 var inst = data.payload,
620 id = inst.id;
621 if (onosInstances[id]) {
622 logicError('ONOS instance already added: ' + id);
623 return;
624 }
625 onosInstances[id] = inst;
626 onosOrder.push(inst);
627 updateInstances();
628 }
629
Simon Hunt99c13842014-11-06 18:23:12 -0800630 function addDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800631 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800632 var device = data.payload,
Simon Huntca867ac2014-11-28 18:07:35 -0800633 id = device.id,
634 d;
635
636 if (network.lookup[id]) {
637 logicError('Device already added: ' + id);
638 return;
639 }
640
641 d = createDeviceNode(device);
642 network.nodes.push(d);
643 network.lookup[id] = d;
Simon Hunt99c13842014-11-06 18:23:12 -0800644 updateNodes();
645 network.force.start();
646 }
647
Simon Hunt99c13842014-11-06 18:23:12 -0800648 function addLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800649 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800650 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800651 result = findLink(link, 'add'),
652 bad = result.badLogic,
Simon Huntca867ac2014-11-28 18:07:35 -0800653 d = result.ldata;
Simon Hunt8257f4c2014-11-16 19:34:54 -0800654
655 if (bad) {
656 logicError(bad + ': ' + link.id);
657 return;
658 }
659
Simon Huntca867ac2014-11-28 18:07:35 -0800660 if (d) {
Simon Hunt8257f4c2014-11-16 19:34:54 -0800661 // we already have a backing store link for src/dst nodes
Simon Huntca867ac2014-11-28 18:07:35 -0800662 addLinkUpdate(d, link);
Simon Hunt8257f4c2014-11-16 19:34:54 -0800663 return;
664 }
665
666 // no backing store link yet
Simon Huntca867ac2014-11-28 18:07:35 -0800667 d = createLink(link);
668 if (d) {
669 network.links.push(d);
670 network.lookup[d.key] = d;
Simon Hunt99c13842014-11-06 18:23:12 -0800671 updateLinks();
672 network.force.start();
673 }
674 }
675
Simon Hunt56d51852014-11-09 13:03:35 -0800676 function addHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800677 evTrace(data);
Simon Hunt56d51852014-11-09 13:03:35 -0800678 var host = data.payload,
Simon Huntca867ac2014-11-28 18:07:35 -0800679 id = host.id,
680 d,
Simon Hunt56d51852014-11-09 13:03:35 -0800681 lnk;
Simon Huntca867ac2014-11-28 18:07:35 -0800682
683 if (network.lookup[id]) {
684 logicError('Host already added: ' + id);
685 return;
686 }
687
688 d = createHostNode(host);
689 network.nodes.push(d);
690 network.lookup[host.id] = d;
Simon Hunt56d51852014-11-09 13:03:35 -0800691 updateNodes();
692
693 lnk = createHostLink(host);
694 if (lnk) {
Simon Huntca867ac2014-11-28 18:07:35 -0800695 d.linkData = lnk; // cache ref on its host
Simon Hunt56d51852014-11-09 13:03:35 -0800696 network.links.push(lnk);
Simon Huntca867ac2014-11-28 18:07:35 -0800697 network.lookup[d.ingress] = lnk;
698 network.lookup[d.egress] = lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800699 updateLinks();
700 }
701 network.force.start();
702 }
703
Simon Hunt44031102014-11-11 13:20:36 -0800704 // TODO: fold updateX(...) methods into one base method; remove duplication
Simon Hunt56a2ea42014-11-19 12:39:31 -0800705
706 function updateInstance(data) {
707 evTrace(data);
708 var inst = data.payload,
709 id = inst.id,
Simon Huntca867ac2014-11-28 18:07:35 -0800710 d = onosInstances[id];
711 if (d) {
712 $.extend(d, inst);
Simon Hunt56a2ea42014-11-19 12:39:31 -0800713 updateInstances();
Simon Hunt56a2ea42014-11-19 12:39:31 -0800714 } else {
715 logicError('updateInstance lookup fail. ID = "' + id + '"');
716 }
717 }
718
Simon Huntbb282f52014-11-10 11:08:19 -0800719 function updateDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800720 evTrace(data);
Simon Huntbb282f52014-11-10 11:08:19 -0800721 var device = data.payload,
722 id = device.id,
Simon Hunt6d9bd032014-11-28 22:16:40 -0800723 d = network.lookup[id],
724 wasOnline;
725
Simon Hunt62c47542014-11-22 22:16:32 -0800726 if (d) {
Simon Hunt6d9bd032014-11-28 22:16:40 -0800727 wasOnline = d.online;
Simon Hunt62c47542014-11-22 22:16:32 -0800728 $.extend(d, device);
729 if (positionNode(d, true)) {
Simon Hunt395a70c2014-11-22 23:17:40 -0800730 sendUpdateMeta(d, true);
Simon Hunt62c47542014-11-22 22:16:32 -0800731 }
732 updateNodes();
Simon Hunt6d9bd032014-11-28 22:16:40 -0800733 if (wasOnline !== d.online) {
734 findAttachedLinks(d.id).forEach(restyleLinkElement);
735 updateOfflineVisibility(d);
736 }
Simon Huntbb282f52014-11-10 11:08:19 -0800737 } else {
738 logicError('updateDevice lookup fail. ID = "' + id + '"');
739 }
740 }
741
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800742 function updateLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800743 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800744 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800745 result = findLink(link, 'update'),
746 bad = result.badLogic;
747 if (bad) {
748 logicError(bad + ': ' + link.id);
749 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800750 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800751 result.updateWith(link);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800752 }
753
Simon Hunt7cd48f32014-11-09 23:42:50 -0800754 function updateHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800755 evTrace(data);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800756 var host = data.payload,
Simon Huntbb282f52014-11-10 11:08:19 -0800757 id = host.id,
Simon Huntca867ac2014-11-28 18:07:35 -0800758 d = network.lookup[id];
759 if (d) {
760 $.extend(d, host);
Simon Hunt6d9bd032014-11-28 22:16:40 -0800761 if (positionNode(d, true)) {
762 sendUpdateMeta(d, true);
763 }
764 updateNodes(d);
Simon Huntbb282f52014-11-10 11:08:19 -0800765 } else {
766 logicError('updateHost lookup fail. ID = "' + id + '"');
767 }
Simon Hunt7cd48f32014-11-09 23:42:50 -0800768 }
769
Simon Hunt44031102014-11-11 13:20:36 -0800770 // TODO: fold removeX(...) methods into base method - remove dup code
Simon Hunt7b403bc2014-11-22 19:01:00 -0800771 function removeInstance(data) {
772 evTrace(data);
773 var inst = data.payload,
774 id = inst.id,
Simon Huntca867ac2014-11-28 18:07:35 -0800775 d = onosInstances[id];
776 if (d) {
777 var idx = find(id, onosOrder);
Simon Hunt7b403bc2014-11-22 19:01:00 -0800778 if (idx >= 0) {
779 onosOrder.splice(idx, 1);
780 }
781 delete onosInstances[id];
782 updateInstances();
783 } else {
784 logicError('updateInstance lookup fail. ID = "' + id + '"');
785 }
786 }
787
Simon Huntca867ac2014-11-28 18:07:35 -0800788 function removeDevice(data) {
789 evTrace(data);
790 var device = data.payload,
791 id = device.id,
792 d = network.lookup[id];
793 if (d) {
794 removeDeviceElement(d);
795 } else {
796 logicError('removeDevice lookup fail. ID = "' + id + '"');
797 }
798 }
799
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800800 function removeLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800801 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800802 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800803 result = findLink(link, 'remove'),
804 bad = result.badLogic;
805 if (bad) {
Simon Huntca867ac2014-11-28 18:07:35 -0800806 // may have already removed link, if attached to removed device
807 console.warn(bad + ': ' + link.id);
Simon Hunt8257f4c2014-11-16 19:34:54 -0800808 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800809 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800810 result.removeRawLink();
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800811 }
812
Simon Hunt44031102014-11-11 13:20:36 -0800813 function removeHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800814 evTrace(data);
Simon Hunt44031102014-11-11 13:20:36 -0800815 var host = data.payload,
816 id = host.id,
Simon Huntca867ac2014-11-28 18:07:35 -0800817 d = network.lookup[id];
818 if (d) {
819 removeHostElement(d, true);
Simon Hunt44031102014-11-11 13:20:36 -0800820 } else {
Simon Huntca867ac2014-11-28 18:07:35 -0800821 // may have already removed host, if attached to removed device
822 console.warn('removeHost lookup fail. ID = "' + id + '"');
Simon Hunt44031102014-11-11 13:20:36 -0800823 }
824 }
825
Simon Huntca867ac2014-11-28 18:07:35 -0800826 // the following events are server responses to user actions
Thomas Vachuska47635c62014-11-22 01:21:36 -0800827 function showSummary(data) {
828 evTrace(data);
829 populateSummary(data.payload);
Simon Hunt06811b72014-11-25 18:54:48 -0800830 showSummaryPane();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800831 }
832
Simon Hunt61d04042014-11-11 17:27:16 -0800833 function showDetails(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800834 evTrace(data);
Simon Hunt27d322d2014-11-28 10:45:43 -0800835 haveDetails = true;
Simon Hunt61d04042014-11-11 17:27:16 -0800836 populateDetails(data.payload);
Simon Hunt27d322d2014-11-28 10:45:43 -0800837 if (useDetails) {
838 showDetailPane();
839 }
Simon Hunt61d04042014-11-11 17:27:16 -0800840 }
841
Simon Huntb53e0682014-11-12 13:32:01 -0800842 function showTraffic(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800843 evTrace(data);
Thomas Vachuska4731f122014-11-20 04:56:19 -0800844 var paths = data.payload.paths,
845 hasTraffic = false;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800846
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800847 // Revert any links hilighted previously.
Thomas Vachuska4731f122014-11-20 04:56:19 -0800848 link.style('stroke-width', null)
Thomas Vachuskaa3148a72014-11-19 21:38:35 -0800849 .classed('primary secondary animated optical', false);
Simon Hunte2575b62014-11-18 15:25:53 -0800850 // Remove all previous labels.
851 removeLinkLabels();
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800852
Simon Hunte2575b62014-11-18 15:25:53 -0800853 // Now hilight all links in the paths payload, and attach
854 // labels to them, if they are defined.
Simon Hunta255a2c2014-11-13 22:29:35 -0800855 paths.forEach(function (p) {
Simon Hunte2575b62014-11-18 15:25:53 -0800856 var n = p.links.length,
857 i,
858 ldata;
859
Thomas Vachuska4731f122014-11-20 04:56:19 -0800860 hasTraffic = hasTraffic || p.traffic;
Simon Hunte2575b62014-11-18 15:25:53 -0800861 for (i=0; i<n; i++) {
862 ldata = findLinkById(p.links[i]);
Thomas Vachuska4731f122014-11-20 04:56:19 -0800863 if (ldata && ldata.el) {
Simon Hunte2575b62014-11-18 15:25:53 -0800864 ldata.el.classed(p.class, true);
865 ldata.label = p.labels[i];
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800866 }
Simon Hunte2575b62014-11-18 15:25:53 -0800867 }
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800868 });
Thomas Vachuska4731f122014-11-20 04:56:19 -0800869
Simon Hunte2575b62014-11-18 15:25:53 -0800870 updateLinks();
Thomas Vachuska4731f122014-11-20 04:56:19 -0800871
872 if (hasTraffic && !antTimer) {
873 startAntTimer();
874 } else if (!hasTraffic && antTimer) {
875 stopAntTimer();
876 }
Simon Huntb53e0682014-11-12 13:32:01 -0800877 }
878
Simon Hunt56d51852014-11-09 13:03:35 -0800879 // ...............................
880
Simon Hunt99c13842014-11-06 18:23:12 -0800881 function unknownEvent(data) {
Simon Hunt434cf142014-11-24 11:10:28 -0800882 console.warn('Unknown event type: "' + data.event + '"', data);
Simon Hunt99c13842014-11-06 18:23:12 -0800883 }
884
885 function handleServerEvent(data) {
886 var fn = eventDispatch[data.event] || unknownEvent;
887 fn(data);
888 }
889
890 // ==============================
Simon Hunt61d04042014-11-11 17:27:16 -0800891 // Out-going messages...
892
Simon Huntb53e0682014-11-12 13:32:01 -0800893 function nSel() {
894 return selectOrder.length;
895 }
Simon Hunt61d04042014-11-11 17:27:16 -0800896 function getSel(idx) {
897 return selections[selectOrder[idx]];
898 }
Simon Huntb53e0682014-11-12 13:32:01 -0800899 function allSelectionsClass(cls) {
900 for (var i=0, n=nSel(); i<n; i++) {
901 if (getSel(i).obj.class !== cls) {
902 return false;
903 }
904 }
905 return true;
906 }
Simon Hunt61d04042014-11-11 17:27:16 -0800907
Thomas Vachuska47635c62014-11-22 01:21:36 -0800908 function toggleInstances() {
909 if (!oiBox.isVisible()) {
Simon Huntb0ecfa52014-11-23 21:05:12 -0800910 showInstances();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800911 } else {
Simon Huntb0ecfa52014-11-23 21:05:12 -0800912 hideInstances();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800913 }
914 }
915
Simon Huntb0ecfa52014-11-23 21:05:12 -0800916 function showInstances() {
917 oiBox.show();
918 colorAffinity = true;
919 updateDeviceColors();
920 }
921
922 function hideInstances() {
923 oiBox.hide();
924 colorAffinity = false;
925 cancelAffinity();
926 updateDeviceColors();
927 }
928
Thomas Vachuska47635c62014-11-22 01:21:36 -0800929 function toggleSummary() {
930 if (!summaryPane.isVisible()) {
931 requestSummary();
932 } else {
933 cancelSummary();
934 }
935 }
936
937 // request overall summary data
938 function requestSummary() {
939 sendMessage('requestSummary', {});
940 }
941
942 function cancelSummary() {
943 sendMessage('cancelSummary', {});
Simon Hunt06811b72014-11-25 18:54:48 -0800944 hideSummaryPane();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800945 }
946
Simon Hunt27d322d2014-11-28 10:45:43 -0800947 function toggleDetails() {
948 useDetails = !useDetails;
949 if (useDetails) {
950 flash('Enable details pane');
951 if (haveDetails) {
952 showDetailPane();
953 }
954 } else {
955 flash('Disable details pane');
956 hideDetailPane();
957 }
958 }
959
Simon Hunt06811b72014-11-25 18:54:48 -0800960 // encapsulate interaction between summary and details panes
961 function showSummaryPane() {
962 if (detailPane.isVisible()) {
963 detailPane.down(summaryPane.show);
964 } else {
965 summaryPane.show();
966 }
967 }
968
969 function hideSummaryPane() {
970 summaryPane.hide(function () {
971 if (detailPane.isVisible()) {
972 detailPane.up();
973 }
974 });
975 }
976
977 function showDetailPane() {
978 if (summaryPane.isVisible()) {
979 detailPane.down(detailPane.show);
980 } else {
981 detailPane.up(detailPane.show);
982 }
983 }
984
985 function hideDetailPane() {
986 detailPane.hide();
987 }
988
989
Simon Hunt61d04042014-11-11 17:27:16 -0800990 // request details for the selected element
Simon Huntd72bc702014-11-13 18:38:04 -0800991 // invoked from selection of a single node.
Simon Hunt61d04042014-11-11 17:27:16 -0800992 function requestDetails() {
993 var data = getSel(0).obj,
994 payload = {
995 id: data.id,
996 class: data.class
997 };
998 sendMessage('requestDetails', payload);
999 }
1000
Thomas Vachuska9edca302014-11-22 17:06:42 -08001001 function addHostIntentAction() {
Simon Huntd72bc702014-11-13 18:38:04 -08001002 sendMessage('addHostIntent', {
Thomas Vachuska9edca302014-11-22 17:06:42 -08001003 one: selectOrder[0],
1004 two: selectOrder[1],
1005 ids: selectOrder
Simon Huntd72bc702014-11-13 18:38:04 -08001006 });
Simon Hunt27d322d2014-11-28 10:45:43 -08001007 flash('Host-to-Host flow added');
Simon Huntd72bc702014-11-13 18:38:04 -08001008 }
1009
Thomas Vachuska9edca302014-11-22 17:06:42 -08001010 function addMultiSourceIntentAction() {
1011 sendMessage('addMultiSourceIntent', {
1012 src: selectOrder.slice(0, selectOrder.length - 1),
1013 dst: selectOrder[selectOrder.length - 1],
1014 ids: selectOrder
1015 });
Simon Hunt27d322d2014-11-28 10:45:43 -08001016 flash('Multi-Source flow added');
Thomas Vachuska29617e52014-11-20 03:17:46 -08001017 }
1018
Thomas Vachuska9edca302014-11-22 17:06:42 -08001019
Thomas Vachuska47635c62014-11-22 01:21:36 -08001020 function cancelTraffic() {
1021 sendMessage('cancelTraffic', {});
1022 }
1023
Thomas Vachuska9edca302014-11-22 17:06:42 -08001024 function requestTrafficForMode() {
1025 if (hoverMode === hoverModeAll) {
1026 requestAllTraffic();
1027 } else if (hoverMode === hoverModeFlows) {
1028 requestDeviceLinkFlows();
1029 } else if (hoverMode === hoverModeIntents) {
1030 requestSelectTraffic();
Simon Huntd72bc702014-11-13 18:38:04 -08001031 }
Thomas Vachuska9edca302014-11-22 17:06:42 -08001032 }
Simon Huntd72bc702014-11-13 18:38:04 -08001033
Thomas Vachuska9edca302014-11-22 17:06:42 -08001034 function showTrafficAction() {
1035 hoverMode = hoverModeIntents;
1036 requestSelectTraffic();
Simon Hunt27d322d2014-11-28 10:45:43 -08001037 flash('Related Traffic');
Thomas Vachuska9edca302014-11-22 17:06:42 -08001038 }
1039
1040 function requestSelectTraffic() {
1041 if (validateSelectionContext()) {
1042 var hoverId = (hoverMode === hoverModeIntents && hovered &&
1043 (hovered.class === 'host' || hovered.class === 'device'))
Simon Huntd72bc702014-11-13 18:38:04 -08001044 ? hovered.id : '';
Thomas Vachuska9edca302014-11-22 17:06:42 -08001045 sendMessage('requestTraffic', {
1046 ids: selectOrder,
1047 hover: hoverId
1048 });
1049 }
Simon Huntd72bc702014-11-13 18:38:04 -08001050 }
1051
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -08001052
Thomas Vachuska29617e52014-11-20 03:17:46 -08001053 function showDeviceLinkFlowsAction() {
Thomas Vachuska9edca302014-11-22 17:06:42 -08001054 hoverMode = hoverModeFlows;
1055 requestDeviceLinkFlows();
Simon Hunt27d322d2014-11-28 10:45:43 -08001056 flash('Device Flows');
Thomas Vachuska29617e52014-11-20 03:17:46 -08001057 }
1058
Thomas Vachuska9edca302014-11-22 17:06:42 -08001059 function requestDeviceLinkFlows() {
1060 if (validateSelectionContext()) {
1061 var hoverId = (hoverMode === hoverModeFlows && hovered &&
1062 (hovered.class === 'device')) ? hovered.id : '';
1063 sendMessage('requestDeviceLinkFlows', {
1064 ids: selectOrder,
1065 hover: hoverId
1066 });
1067 }
1068 }
1069
1070
1071 function showAllTrafficAction() {
1072 hoverMode = hoverModeAll;
1073 requestAllTraffic();
Simon Hunt27d322d2014-11-28 10:45:43 -08001074 flash('All Traffic');
Thomas Vachuska9edca302014-11-22 17:06:42 -08001075 }
1076
1077 function requestAllTraffic() {
1078 sendMessage('requestAllTraffic', {});
1079 }
1080
1081 function validateSelectionContext() {
Thomas Vachuska29617e52014-11-20 03:17:46 -08001082 if (!hovered && nSel() === 0) {
Thomas Vachuska47635c62014-11-22 01:21:36 -08001083 cancelTraffic();
Thomas Vachuska9edca302014-11-22 17:06:42 -08001084 return false;
Thomas Vachuska29617e52014-11-20 03:17:46 -08001085 }
Thomas Vachuska9edca302014-11-22 17:06:42 -08001086 return true;
Thomas Vachuska29617e52014-11-20 03:17:46 -08001087 }
Simon Huntd72bc702014-11-13 18:38:04 -08001088
Simon Hunta6a9fe72014-11-20 11:17:12 -08001089
Simon Hunt61d04042014-11-11 17:27:16 -08001090 // ==============================
Simon Hunta5e89142014-11-14 07:00:33 -08001091 // onos instance panel functions
Simon Huntb82f6902014-11-22 11:53:15 -08001092
Simon Hunt7b403bc2014-11-22 19:01:00 -08001093 var instCfg = {
1094 rectPad: 8,
1095 nodeOx: 9,
1096 nodeOy: 9,
1097 nodeDim: 40,
1098 birdOx: 19,
1099 birdOy: 21,
1100 birdDim: 21,
1101 uiDy: 45,
1102 titleDy: 30,
1103 textYOff: 20,
1104 textYSpc: 15
1105 };
1106
1107 function viewBox(dim) {
1108 return '0 0 ' + dim.w + ' ' + dim.h;
1109 }
1110
1111 function instRectAttr(dim) {
1112 var pad = instCfg.rectPad;
1113 return {
1114 x: pad,
1115 y: pad,
1116 width: dim.w - pad*2,
1117 height: dim.h - pad*2,
Simon Huntb0ecfa52014-11-23 21:05:12 -08001118 rx: 6
Simon Hunt7b403bc2014-11-22 19:01:00 -08001119 };
1120 }
1121
1122 function computeDim(self) {
1123 var css = window.getComputedStyle(self);
1124 return {
1125 w: stripPx(css.width),
1126 h: stripPx(css.height)
1127 };
Simon Huntb82f6902014-11-22 11:53:15 -08001128 }
Simon Hunta5e89142014-11-14 07:00:33 -08001129
1130 function updateInstances() {
1131 var onoses = oiBox.el.selectAll('.onosInst')
Simon Huntb82f6902014-11-22 11:53:15 -08001132 .data(onosOrder, function (d) { return d.id; }),
Simon Hunt7b403bc2014-11-22 19:01:00 -08001133 instDim = {w:0,h:0},
1134 c = instCfg;
Simon Hunta5e89142014-11-14 07:00:33 -08001135
Simon Hunt7b403bc2014-11-22 19:01:00 -08001136 function nSw(n) {
1137 return '# Switches: ' + n;
1138 }
Simon Hunta5e89142014-11-14 07:00:33 -08001139
Simon Huntb82f6902014-11-22 11:53:15 -08001140 // operate on existing onos instances if necessary
1141 onoses.each(function (d) {
1142 var el = d3.select(this),
1143 svg = el.select('svg');
Simon Hunt7b403bc2014-11-22 19:01:00 -08001144 instDim = computeDim(this);
Simon Huntb82f6902014-11-22 11:53:15 -08001145
1146 // update online state
1147 el.classed('online', d.online);
1148
1149 // update ui-attached state
1150 svg.select('use.uiBadge').remove();
1151 if (d.uiAttached) {
1152 attachUiBadge(svg);
1153 }
1154
Simon Hunt7b403bc2014-11-22 19:01:00 -08001155 function updAttr(id, value) {
1156 svg.select('text.instLabel.'+id).text(value);
1157 }
1158
1159 updAttr('ip', d.ip);
1160 updAttr('ns', nSw(d.switches));
Simon Huntb82f6902014-11-22 11:53:15 -08001161 });
1162
1163
1164 // operate on new onos instances
Simon Hunta5e89142014-11-14 07:00:33 -08001165 var entering = onoses.enter()
1166 .append('div')
1167 .attr('class', 'onosInst')
1168 .classed('online', function (d) { return d.online; })
Simon Hunt9c15eca2014-11-15 18:37:59 -08001169 .on('click', clickInst);
1170
Simon Huntb82f6902014-11-22 11:53:15 -08001171 entering.each(function (d) {
Simon Hunt9c15eca2014-11-15 18:37:59 -08001172 var el = d3.select(this),
Simon Hunt7b403bc2014-11-22 19:01:00 -08001173 rectAttr,
1174 svg;
1175 instDim = computeDim(this);
1176 rectAttr = instRectAttr(instDim);
Simon Hunt9c15eca2014-11-15 18:37:59 -08001177
Simon Hunt7b403bc2014-11-22 19:01:00 -08001178 svg = el.append('svg').attr({
1179 width: instDim.w,
1180 height: instDim.h,
1181 viewBox: viewBox(instDim)
Simon Hunt95908012014-11-20 10:20:26 -08001182 });
Simon Huntb82f6902014-11-22 11:53:15 -08001183
Simon Hunt7b403bc2014-11-22 19:01:00 -08001184 svg.append('rect').attr(rectAttr);
Simon Hunt9c15eca2014-11-15 18:37:59 -08001185
Thomas Vachuskae02e11c2014-11-24 16:13:52 -08001186 //appendGlyph(svg, c.nodeOx, c.nodeOy, c.nodeDim, '#node');
1187 appendBadge(svg, 14, 14, 28, '#bird');
Simon Huntb82f6902014-11-22 11:53:15 -08001188
1189 if (d.uiAttached) {
1190 attachUiBadge(svg);
1191 }
1192
Simon Hunt7b403bc2014-11-22 19:01:00 -08001193 var left = c.nodeOx + c.nodeDim,
1194 len = rectAttr.width - left,
1195 hlen = len / 2,
1196 midline = hlen + left;
Simon Hunt9c15eca2014-11-15 18:37:59 -08001197
Simon Hunt7b403bc2014-11-22 19:01:00 -08001198 // title
1199 svg.append('text')
1200 .attr({
1201 class: 'instTitle',
1202 x: midline,
1203 y: c.titleDy
1204 })
1205 .text(d.id);
1206
1207 // a couple of attributes
1208 var ty = c.titleDy + c.textYOff;
1209
1210 function addAttr(id, label) {
1211 svg.append('text').attr({
1212 class: 'instLabel ' + id,
1213 x: midline,
1214 y: ty
1215 }).text(label);
1216 ty += c.textYSpc;
1217 }
1218
1219 addAttr('ip', d.ip);
1220 addAttr('ns', nSw(d.switches));
Simon Hunt9c15eca2014-11-15 18:37:59 -08001221 });
Simon Hunta5e89142014-11-14 07:00:33 -08001222
1223 // operate on existing + new onoses here
Simon Hunt8f40cce2014-11-23 15:57:30 -08001224 // set the affinity colors...
1225 onoses.each(function (d) {
1226 var el = d3.select(this),
1227 rect = el.select('svg').select('rect'),
1228 col = instColor(d.id, d.online);
1229 rect.style('fill', col);
1230 });
Simon Hunta5e89142014-11-14 07:00:33 -08001231
Simon Hunt7b403bc2014-11-22 19:01:00 -08001232 // adjust the panel size appropriately...
1233 oiBox.width(instDim.w * onosOrder.length);
1234 oiBox.height(instDim.h);
1235
1236 // remove any outgoing instances
1237 onoses.exit().remove();
Simon Hunta5e89142014-11-14 07:00:33 -08001238 }
1239
Simon Hunt8f40cce2014-11-23 15:57:30 -08001240 function instColor(id, online) {
1241 return cat7.get(id, !online, network.view.getTheme());
1242 }
1243
Simon Hunt9462e8c2014-11-14 17:28:09 -08001244 function clickInst(d) {
1245 var el = d3.select(this),
1246 aff = el.classed('affinity');
1247 if (!aff) {
1248 setAffinity(el, d);
1249 } else {
1250 cancelAffinity();
1251 }
1252 }
1253
1254 function setAffinity(el, d) {
1255 d3.selectAll('.onosInst')
1256 .classed('mastership', true)
1257 .classed('affinity', false);
1258 el.classed('affinity', true);
1259
1260 suppressLayers(true);
1261 node.each(function (n) {
1262 if (n.master === d.id) {
1263 n.el.classed('suppressed', false);
1264 }
1265 });
1266 oiShowMaster = true;
1267 }
1268
1269 function cancelAffinity() {
1270 d3.selectAll('.onosInst')
1271 .classed('mastership affinity', false);
1272 restoreLayerState();
1273 oiShowMaster = false;
1274 }
1275
Simon Hunt7b403bc2014-11-22 19:01:00 -08001276 // TODO: these should be moved out to utility module.
1277 function stripPx(s) {
1278 return s.replace(/px$/,'');
1279 }
1280
1281 function appendUse(svg, ox, oy, dim, iid, cls) {
1282 var use = svg.append('use').attr({
1283 transform: translate(ox,oy),
1284 'xlink:href': iid,
1285 width: dim,
1286 height: dim
1287 });
1288 if (cls) {
1289 use.classed(cls, true);
1290 }
1291 return use;
1292 }
1293
1294 function appendGlyph(svg, ox, oy, dim, iid, cls) {
1295 appendUse(svg, ox, oy, dim, iid, cls).classed('glyphIcon', true);
1296 }
1297
1298 function appendBadge(svg, ox, oy, dim, iid, cls) {
1299 appendUse(svg, ox, oy, dim, iid,cls ).classed('badgeIcon', true);
1300 }
1301
1302 function attachUiBadge(svg) {
1303 appendBadge(svg, 12, instCfg.uiDy, 30, '#uiAttached', 'uiBadge');
1304 }
1305
Simon Hunt434cf142014-11-24 11:10:28 -08001306 function visVal(b) {
1307 return b ? 'visible' : 'hidden';
1308 }
1309
Simon Hunta5e89142014-11-14 07:00:33 -08001310 // ==============================
Simon Hunt99c13842014-11-06 18:23:12 -08001311 // force layout modification functions
1312
1313 function translate(x, y) {
1314 return 'translate(' + x + ',' + y + ')';
1315 }
1316
Simon Hunte2575b62014-11-18 15:25:53 -08001317 function rotate(deg) {
1318 return 'rotate(' + deg + ')';
1319 }
1320
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001321 function missMsg(what, id) {
1322 return '\n[' + what + '] "' + id + '" missing ';
1323 }
1324
1325 function linkEndPoints(srcId, dstId) {
1326 var srcNode = network.lookup[srcId],
1327 dstNode = network.lookup[dstId],
1328 sMiss = !srcNode ? missMsg('src', srcId) : '',
1329 dMiss = !dstNode ? missMsg('dst', dstId) : '';
1330
1331 if (sMiss || dMiss) {
1332 logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
1333 return null;
1334 }
1335 return {
1336 source: srcNode,
1337 target: dstNode,
1338 x1: srcNode.x,
1339 y1: srcNode.y,
1340 x2: dstNode.x,
1341 y2: dstNode.y
1342 };
1343 }
1344
Simon Hunt56d51852014-11-09 13:03:35 -08001345 function createHostLink(host) {
1346 var src = host.id,
1347 dst = host.cp.device,
Simon Hunt7cd48f32014-11-09 23:42:50 -08001348 id = host.ingress,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001349 lnk = linkEndPoints(src, dst);
Simon Hunt56d51852014-11-09 13:03:35 -08001350
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001351 if (!lnk) {
Simon Hunt56d51852014-11-09 13:03:35 -08001352 return null;
1353 }
1354
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001355 // Synthesize link ...
1356 $.extend(lnk, {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001357 key: id,
Simon Hunt56d51852014-11-09 13:03:35 -08001358 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -08001359
1360 type: function () { return 'hostLink'; },
Simon Hunt6d9bd032014-11-28 22:16:40 -08001361 online: function () {
1362 // hostlink target is edge switch
1363 return lnk.target.online;
1364 },
Simon Hunt8257f4c2014-11-16 19:34:54 -08001365 linkWidth: function () { return 1; }
Simon Hunt7cd48f32014-11-09 23:42:50 -08001366 });
Simon Hunt99c13842014-11-06 18:23:12 -08001367 return lnk;
1368 }
1369
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001370 function createLink(link) {
Simon Hunta6a9fe72014-11-20 11:17:12 -08001371 var lnk = linkEndPoints(link.src, link.dst);
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001372
1373 if (!lnk) {
1374 return null;
1375 }
1376
Simon Hunt8257f4c2014-11-16 19:34:54 -08001377 $.extend(lnk, {
1378 key: link.id,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001379 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -08001380 fromSource: link,
1381
1382 // functions to aggregate dual link state
1383 type: function () {
1384 var s = lnk.fromSource,
1385 t = lnk.fromTarget;
1386 return (s && s.type) || (t && t.type) || defaultLinkType;
1387 },
1388 online: function () {
1389 var s = lnk.fromSource,
Simon Hunt6d9bd032014-11-28 22:16:40 -08001390 t = lnk.fromTarget,
1391 both = lnk.source.online && lnk.target.online;
1392 return both && ((s && s.online) || (t && t.online));
Simon Hunt8257f4c2014-11-16 19:34:54 -08001393 },
1394 linkWidth: function () {
1395 var s = lnk.fromSource,
1396 t = lnk.fromTarget,
1397 ws = (s && s.linkWidth) || 0,
1398 wt = (t && t.linkWidth) || 0;
1399 return Math.max(ws, wt);
1400 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001401 });
1402 return lnk;
Simon Hunt1a9eff92014-11-07 11:06:34 -08001403 }
1404
Simon Hunte2575b62014-11-18 15:25:53 -08001405 function removeLinkLabels() {
1406 network.links.forEach(function (d) {
1407 d.label = '';
1408 });
1409 }
1410
Simon Hunt434cf142014-11-24 11:10:28 -08001411 function showHostVis(el) {
1412 el.style('visibility', visVal(showHosts));
1413 }
1414
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001415 var widthRatio = 1.4,
1416 linkScale = d3.scale.linear()
1417 .domain([1, 12])
1418 .range([widthRatio, 12 * widthRatio])
1419 .clamp(true);
1420
Simon Hunt99c13842014-11-06 18:23:12 -08001421 function updateLinks() {
1422 link = linkG.selectAll('.link')
Simon Hunt8257f4c2014-11-16 19:34:54 -08001423 .data(network.links, function (d) { return d.key; });
Simon Hunt99c13842014-11-06 18:23:12 -08001424
1425 // operate on existing links, if necessary
1426 // link .foo() .bar() ...
1427
1428 // operate on entering links:
1429 var entering = link.enter()
1430 .append('line')
1431 .attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001432 x1: function (d) { return d.x1; },
1433 y1: function (d) { return d.y1; },
1434 x2: function (d) { return d.x2; },
1435 y2: function (d) { return d.y2; },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001436 stroke: config.topo.linkInColor,
1437 'stroke-width': config.topo.linkInWidth
Simon Hunt99c13842014-11-06 18:23:12 -08001438 });
1439
1440 // augment links
Simon Hunt7cd48f32014-11-09 23:42:50 -08001441 entering.each(function (d) {
1442 var link = d3.select(this);
1443 // provide ref to element selection from backing data....
1444 d.el = link;
Simon Hunt8257f4c2014-11-16 19:34:54 -08001445 restyleLinkElement(d);
Simon Hunt434cf142014-11-24 11:10:28 -08001446 if (d.type() === 'hostLink') {
1447 showHostVis(link);
1448 }
Simon Hunt7cd48f32014-11-09 23:42:50 -08001449 });
Thomas Vachuska4830d392014-11-09 17:09:56 -08001450
1451 // operate on both existing and new links, if necessary
1452 //link .foo() .bar() ...
1453
Simon Hunte2575b62014-11-18 15:25:53 -08001454 // apply or remove labels
1455 var labelData = getLabelData();
1456 applyLinkLabels(labelData);
1457
Thomas Vachuska4830d392014-11-09 17:09:56 -08001458 // operate on exiting links:
Thomas Vachuska4830d392014-11-09 17:09:56 -08001459 link.exit()
Simon Hunt6d9bd032014-11-28 22:16:40 -08001460 .attr('stroke-dasharray', '3 3')
Simon Hunt13bf9c82014-11-18 07:26:44 -08001461 .style('opacity', 0.5)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001462 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -08001463 .duration(1500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001464 .attr({
Simon Hunt6d9bd032014-11-28 22:16:40 -08001465 'stroke-dasharray': '3 12',
Simon Hunt13bf9c82014-11-18 07:26:44 -08001466 stroke: config.topo.linkOutColor,
1467 'stroke-width': config.topo.linkOutWidth
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001468 })
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001469 .style('opacity', 0.0)
Thomas Vachuska4830d392014-11-09 17:09:56 -08001470 .remove();
Simon Hunte2575b62014-11-18 15:25:53 -08001471
1472 // NOTE: invoke a single tick to force the labels to position
1473 // onto their links.
1474 tick();
1475 }
1476
1477 function getLabelData() {
1478 // create the backing data for showing labels..
1479 var data = [];
1480 link.each(function (d) {
1481 if (d.label) {
1482 data.push({
1483 id: 'lab-' + d.key,
1484 key: d.key,
1485 label: d.label,
1486 ldata: d
1487 });
1488 }
1489 });
1490 return data;
1491 }
1492
1493 var linkLabelOffset = '0.3em';
1494
1495 function applyLinkLabels(data) {
1496 var entering;
1497
1498 linkLabel = linkLabelG.selectAll('.linkLabel')
1499 .data(data, function (d) { return d.id; });
1500
Simon Hunt56a2ea42014-11-19 12:39:31 -08001501 // for elements already existing, we need to update the text
1502 // and adjust the rectangle size to fit
1503 linkLabel.each(function (d) {
1504 var el = d3.select(this),
1505 rect = el.select('rect'),
1506 text = el.select('text');
1507 text.text(d.label);
1508 rect.attr(rectAroundText(el));
1509 });
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -08001510
Simon Hunte2575b62014-11-18 15:25:53 -08001511 entering = linkLabel.enter().append('g')
1512 .classed('linkLabel', true)
1513 .attr('id', function (d) { return d.id; });
1514
1515 entering.each(function (d) {
1516 var el = d3.select(this),
1517 rect,
1518 text,
1519 parms = {
1520 x1: d.ldata.x1,
1521 y1: d.ldata.y1,
1522 x2: d.ldata.x2,
1523 y2: d.ldata.y2
1524 };
1525
1526 d.el = el;
1527 rect = el.append('rect');
1528 text = el.append('text').text(d.label);
1529 rect.attr(rectAroundText(el));
1530 text.attr('dy', linkLabelOffset);
1531
1532 el.attr('transform', transformLabel(parms));
1533 });
1534
1535 // Remove any links that are no longer required.
1536 linkLabel.exit().remove();
1537 }
1538
1539 function rectAroundText(el) {
1540 var text = el.select('text'),
1541 box = text.node().getBBox();
1542
1543 // translate the bbox so that it is centered on [x,y]
1544 box.x = -box.width / 2;
1545 box.y = -box.height / 2;
1546
1547 // add padding
1548 box.x -= 1;
1549 box.width += 2;
1550 return box;
1551 }
1552
1553 function transformLabel(p) {
1554 var dx = p.x2 - p.x1,
1555 dy = p.y2 - p.y1,
1556 xMid = dx/2 + p.x1,
1557 yMid = dy/2 + p.y1;
Simon Hunte2575b62014-11-18 15:25:53 -08001558 return translate(xMid, yMid);
Simon Hunt99c13842014-11-06 18:23:12 -08001559 }
1560
1561 function createDeviceNode(device) {
1562 // start with the object as is
1563 var node = device,
Simon Huntbb282f52014-11-10 11:08:19 -08001564 type = device.type,
Simon Huntc72967b2014-11-20 09:21:42 -08001565 svgCls = type ? 'node device ' + type : 'node device',
1566 labels = device.labels || [];
1567
Simon Hunt99c13842014-11-06 18:23:12 -08001568 // Augment as needed...
1569 node.class = 'device';
Simon Huntbb282f52014-11-10 11:08:19 -08001570 node.svgClass = device.online ? svgCls + ' online' : svgCls;
Simon Hunt99c13842014-11-06 18:23:12 -08001571 positionNode(node);
Simon Hunt99c13842014-11-06 18:23:12 -08001572 return node;
1573 }
1574
Simon Hunt56d51852014-11-09 13:03:35 -08001575 function createHostNode(host) {
1576 // start with the object as is
1577 var node = host;
1578
1579 // Augment as needed...
1580 node.class = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001581 if (!node.type) {
Simon Hunt209155e2014-11-21 12:16:09 -08001582 node.type = 'endstation';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001583 }
Simon Hunt7fa116d2014-11-17 14:16:55 -08001584 node.svgClass = 'node host ' + node.type;
Simon Hunt56d51852014-11-09 13:03:35 -08001585 positionNode(node);
Simon Hunt56d51852014-11-09 13:03:35 -08001586 return node;
1587 }
1588
Simon Hunt62c47542014-11-22 22:16:32 -08001589 function positionNode(node, forUpdate) {
Simon Hunt99c13842014-11-06 18:23:12 -08001590 var meta = node.metaUi,
Simon Huntac9e24f2014-11-12 10:12:21 -08001591 x = meta && meta.x,
1592 y = meta && meta.y,
1593 xy;
Simon Hunt99c13842014-11-06 18:23:12 -08001594
Simon Huntac9e24f2014-11-12 10:12:21 -08001595 // If we have [x,y] already, use that...
Simon Hunt99c13842014-11-06 18:23:12 -08001596 if (x && y) {
1597 node.fixed = true;
Simon Hunt62c47542014-11-22 22:16:32 -08001598 node.px = node.x = x;
1599 node.py = node.y = y;
Simon Huntac9e24f2014-11-12 10:12:21 -08001600 return;
Simon Hunt99c13842014-11-06 18:23:12 -08001601 }
Simon Huntac9e24f2014-11-12 10:12:21 -08001602
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001603 var location = node.location;
1604 if (location && location.type === 'latlng') {
1605 var coord = geoMapProjection([location.lng, location.lat]);
1606 node.fixed = true;
Simon Hunt62c47542014-11-22 22:16:32 -08001607 node.px = node.x = coord[0];
1608 node.py = node.y = coord[1];
Simon Hunt62c47542014-11-22 22:16:32 -08001609 return true;
1610 }
1611
1612 // if this is a node update (not a node add).. skip randomizer
1613 if (forUpdate) {
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001614 return;
1615 }
1616
Simon Huntac9e24f2014-11-12 10:12:21 -08001617 // Note: Placing incoming unpinned nodes at exactly the same point
1618 // (center of the view) causes them to explode outwards when
1619 // the force layout kicks in. So, we spread them out a bit
1620 // initially, to provide a more serene layout convergence.
1621 // Additionally, if the node is a host, we place it near
1622 // the device it is connected to.
1623
1624 function spread(s) {
1625 return Math.floor((Math.random() * s) - s/2);
1626 }
1627
1628 function randDim(dim) {
1629 return dim / 2 + spread(dim * 0.7071);
1630 }
1631
1632 function rand() {
1633 return {
1634 x: randDim(network.view.width()),
1635 y: randDim(network.view.height())
1636 };
1637 }
1638
1639 function near(node) {
1640 var min = 12,
1641 dx = spread(12),
1642 dy = spread(12);
1643 return {
1644 x: node.x + min + dx,
1645 y: node.y + min + dy
1646 };
1647 }
1648
1649 function getDevice(cp) {
1650 var d = network.lookup[cp.device];
1651 return d || rand();
1652 }
1653
1654 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
1655 $.extend(node, xy);
Simon Hunt99c13842014-11-06 18:23:12 -08001656 }
1657
Simon Hunt99c13842014-11-06 18:23:12 -08001658
Simon Huntc72967b2014-11-20 09:21:42 -08001659 function iconGlyphUrl(d) {
1660 var which = d.type || 'unknown';
1661 return '#' + which;
1662 }
1663
Simon Hunt99c13842014-11-06 18:23:12 -08001664 // returns the newly computed bounding box of the rectangle
1665 function adjustRectToFitText(n) {
1666 var text = n.select('text'),
1667 box = text.node().getBBox(),
1668 lab = config.labels;
1669
1670 text.attr('text-anchor', 'middle')
1671 .attr('y', '-0.8em')
1672 .attr('x', lab.imgPad/2);
1673
1674 // translate the bbox so that it is centered on [x,y]
1675 box.x = -box.width / 2;
1676 box.y = -box.height / 2;
1677
1678 // add padding
1679 box.x -= (lab.padLR + lab.imgPad/2);
1680 box.width += lab.padLR * 2 + lab.imgPad;
1681 box.y -= lab.padTB;
1682 box.height += lab.padTB * 2;
1683
1684 return box;
1685 }
1686
Simon Hunt1a9eff92014-11-07 11:06:34 -08001687 function mkSvgClass(d) {
1688 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
1689 }
1690
Simon Hunt7cd48f32014-11-09 23:42:50 -08001691 function hostLabel(d) {
1692 var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0;
1693 return d.labels[idx];
1694 }
1695 function deviceLabel(d) {
1696 var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0;
1697 return d.labels[idx];
1698 }
Simon Huntc72967b2014-11-20 09:21:42 -08001699 function trimLabel(label) {
1700 return (label && label.trim()) || '';
1701 }
1702
1703 function emptyBox() {
1704 return {
1705 x: -2,
1706 y: -2,
1707 width: 4,
1708 height: 4
1709 };
Simon Hunt7cd48f32014-11-09 23:42:50 -08001710 }
1711
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001712 function updateDeviceLabel(d) {
Simon Huntc72967b2014-11-20 09:21:42 -08001713 var label = trimLabel(deviceLabel(d)),
1714 noLabel = !label,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001715 node = d.el,
Simon Huntc72967b2014-11-20 09:21:42 -08001716 box,
1717 dx,
Simon Hunt395a70c2014-11-22 23:17:40 -08001718 dy,
1719 cfg = config.icons.device;
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001720
1721 node.select('text')
1722 .text(label)
1723 .style('opacity', 0)
1724 .transition()
1725 .style('opacity', 1);
1726
Simon Huntc72967b2014-11-20 09:21:42 -08001727 if (noLabel) {
1728 box = emptyBox();
Simon Hunt395a70c2014-11-22 23:17:40 -08001729 dx = -cfg.dim/2;
1730 dy = -cfg.dim/2;
Simon Huntc72967b2014-11-20 09:21:42 -08001731 } else {
1732 box = adjustRectToFitText(node);
Simon Hunt395a70c2014-11-22 23:17:40 -08001733 dx = box.x + cfg.xoff;
1734 dy = box.y + cfg.yoff;
Simon Huntc72967b2014-11-20 09:21:42 -08001735 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001736
1737 node.select('rect')
1738 .transition()
1739 .attr(box);
1740
Simon Huntc72967b2014-11-20 09:21:42 -08001741 node.select('g.deviceIcon')
Thomas Vachuska89543292014-11-19 11:28:33 -08001742 .transition()
Simon Huntc72967b2014-11-20 09:21:42 -08001743 .attr('transform', translate(dx, dy));
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001744 }
1745
1746 function updateHostLabel(d) {
Simon Hunt6d9bd032014-11-28 22:16:40 -08001747 var label = trimLabel(hostLabel(d));
1748 d.el.select('text').text(label);
Simon Huntbb282f52014-11-10 11:08:19 -08001749 }
1750
Simon Hunt434cf142014-11-24 11:10:28 -08001751 function updateHostVisibility() {
1752 var v = visVal(showHosts);
1753 nodeG.selectAll('.host').style('visibility', v);
1754 linkG.selectAll('.hostLink').style('visibility', v);
1755 }
1756
Simon Hunt6d9bd032014-11-28 22:16:40 -08001757 function findOfflineNodes() {
1758 var a = [];
1759 network.nodes.forEach(function (d) {
1760 if (d.class === 'device' && !d.online) {
1761 a.push(d);
1762 }
1763 });
1764 return a;
1765 }
1766
1767 function updateOfflineVisibility(dev) {
1768 var so = showOffline,
1769 sh = showHosts,
1770 vb = 'visibility',
1771 v, off, al, ah, db, b;
1772
1773 function updAtt(show) {
1774 al.forEach(function (d) {
1775 b = show && ((d.type() !== 'hostLink') || sh);
1776 d.el.style(vb, visVal(b));
1777 });
1778 ah.forEach(function (d) {
1779 b = show && sh;
1780 d.el.style(vb, visVal(b));
1781 });
1782 }
1783
1784 if (dev) {
1785 // updating a specific device that just toggled off/on-line
1786 db = dev.online || so;
1787 dev.el.style(vb, visVal(db));
1788 al = findAttachedLinks(dev.id);
1789 ah = findAttachedHosts(dev.id);
1790 updAtt(db);
1791 } else {
1792 // updating all offline devices
1793 v = visVal(so);
1794 off = findOfflineNodes();
1795 off.forEach(function (d) {
1796 d.el.style(vb, v);
1797 al = findAttachedLinks(d.id);
1798 ah = findAttachedHosts(d.id);
1799 updAtt(so);
1800 });
1801 }
1802 }
1803
Simon Hunt6ac93f32014-11-13 12:17:27 -08001804 function nodeMouseOver(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001805 hovered = d;
Thomas Vachuska9edca302014-11-22 17:06:42 -08001806 requestTrafficForMode();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001807 }
1808
1809 function nodeMouseOut(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001810 hovered = null;
Thomas Vachuska9edca302014-11-22 17:06:42 -08001811 requestTrafficForMode();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001812 }
Simon Huntbb282f52014-11-10 11:08:19 -08001813
Simon Hunteb1514d2014-11-20 09:57:29 -08001814 function addHostIcon(node, radius, iid) {
Thomas Vachuska89543292014-11-19 11:28:33 -08001815 var dim = radius * 1.5,
1816 xlate = -dim / 2;
1817
Simon Hunteb1514d2014-11-20 09:57:29 -08001818 node.append('use').attr({
1819 class: 'glyphIcon hostIcon',
1820 transform: translate(xlate,xlate),
1821 'xlink:href': iid,
1822 width: dim,
1823 height: dim
1824 });
Thomas Vachuska89543292014-11-19 11:28:33 -08001825 }
1826
Simon Hunt99c13842014-11-06 18:23:12 -08001827 function updateNodes() {
1828 node = nodeG.selectAll('.node')
1829 .data(network.nodes, function (d) { return d.id; });
1830
Simon Hunt62c47542014-11-22 22:16:32 -08001831 // operate on existing nodes...
1832 node.filter('.device').each(function (d) {
Simon Hunt62c47542014-11-22 22:16:32 -08001833 var node = d.el;
1834 node.classed('online', d.online);
1835 updateDeviceLabel(d);
1836 positionNode(d, true);
1837 });
1838
1839 node.filter('.host').each(function (d) {
Simon Hunt6d9bd032014-11-28 22:16:40 -08001840 updateHostLabel(d);
1841 positionNode(d, true);
Simon Hunt62c47542014-11-22 22:16:32 -08001842 });
Simon Hunt99c13842014-11-06 18:23:12 -08001843
1844 // operate on entering nodes:
1845 var entering = node.enter()
1846 .append('g')
1847 .attr({
1848 id: function (d) { return safeId(d.id); },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001849 class: mkSvgClass,
Simon Hunt99c13842014-11-06 18:23:12 -08001850 transform: function (d) { return translate(d.x, d.y); },
1851 opacity: 0
1852 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08001853 .call(network.drag)
Simon Hunt6ac93f32014-11-13 12:17:27 -08001854 .on('mouseover', nodeMouseOver)
1855 .on('mouseout', nodeMouseOut)
Simon Hunt99c13842014-11-06 18:23:12 -08001856 .transition()
1857 .attr('opacity', 1);
1858
1859 // augment device nodes...
1860 entering.filter('.device').each(function (d) {
1861 var node = d3.select(this),
Simon Huntc72967b2014-11-20 09:21:42 -08001862 label = trimLabel(deviceLabel(d)),
1863 noLabel = !label,
Simon Hunt99c13842014-11-06 18:23:12 -08001864 box;
1865
Simon Hunt7cd48f32014-11-09 23:42:50 -08001866 // provide ref to element from backing data....
1867 d.el = node;
1868
Simon Hunt62c47542014-11-22 22:16:32 -08001869 node.append('rect').attr({ rx: 5, ry: 5 });
1870 node.append('text').text(label).attr('dy', '1.1em');
Simon Hunt99c13842014-11-06 18:23:12 -08001871 box = adjustRectToFitText(node);
Simon Hunta3dd9572014-11-20 15:22:41 -08001872 node.select('rect').attr(box);
Simon Huntc72967b2014-11-20 09:21:42 -08001873 addDeviceIcon(node, box, noLabel, iconGlyphUrl(d));
Simon Huntc7ee0662014-11-05 16:44:37 -08001874 });
Simon Hunt934c3ce2014-11-05 11:45:07 -08001875
Thomas Vachuska89543292014-11-19 11:28:33 -08001876
Simon Hunt56d51852014-11-09 13:03:35 -08001877 // augment host nodes...
1878 entering.filter('.host').each(function (d) {
1879 var node = d3.select(this),
Simon Hunt395a70c2014-11-22 23:17:40 -08001880 cfg = config.icons.host,
1881 r = cfg.radius[d.type] || cfg.defaultRadius,
Thomas Vachuska89543292014-11-19 11:28:33 -08001882 textDy = r + 10,
Simon Hunteb1514d2014-11-20 09:57:29 -08001883 iid = iconGlyphUrl(d);
Simon Hunt56d51852014-11-09 13:03:35 -08001884
Simon Hunt7cd48f32014-11-09 23:42:50 -08001885 // provide ref to element from backing data....
1886 d.el = node;
Simon Hunt434cf142014-11-24 11:10:28 -08001887 showHostVis(node);
Simon Hunt7cd48f32014-11-09 23:42:50 -08001888
Simon Hunt62c47542014-11-22 22:16:32 -08001889 node.append('circle').attr('r', r);
Simon Hunteb1514d2014-11-20 09:57:29 -08001890 if (iid) {
1891 addHostIcon(node, r, iid);
Simon Hunt7fa116d2014-11-17 14:16:55 -08001892 }
Simon Hunt56d51852014-11-09 13:03:35 -08001893 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001894 .text(hostLabel)
Thomas Vachuska89543292014-11-19 11:28:33 -08001895 .attr('dy', textDy)
Simon Hunt7cd48f32014-11-09 23:42:50 -08001896 .attr('text-anchor', 'middle');
Simon Hunt56d51852014-11-09 13:03:35 -08001897 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001898
Simon Hunt99c13842014-11-06 18:23:12 -08001899 // operate on both existing and new nodes, if necessary
Simon Huntb0ecfa52014-11-23 21:05:12 -08001900 updateDeviceColors();
Simon Huntc7ee0662014-11-05 16:44:37 -08001901
Simon Hunt99c13842014-11-06 18:23:12 -08001902 // operate on exiting nodes:
Simon Huntea80eb42014-11-11 13:46:57 -08001903 // Note that the node is removed after 2 seconds.
1904 // Sub element animations should be shorter than 2 seconds.
1905 var exiting = node.exit()
Simon Hunt44031102014-11-11 13:20:36 -08001906 .transition()
1907 .duration(2000)
Simon Huntea80eb42014-11-11 13:46:57 -08001908 .style('opacity', 0)
Simon Hunt99c13842014-11-06 18:23:12 -08001909 .remove();
Simon Huntea80eb42014-11-11 13:46:57 -08001910
1911 // host node exits....
1912 exiting.filter('.host').each(function (d) {
Simon Huntca867ac2014-11-28 18:07:35 -08001913 var node = d.el;
1914 node.select('use')
1915 .style('opacity', 0.5)
1916 .transition()
1917 .duration(800)
1918 .style('opacity', 0);
Simon Huntea80eb42014-11-11 13:46:57 -08001919
1920 node.select('text')
1921 .style('opacity', 0.5)
1922 .transition()
Simon Huntca867ac2014-11-28 18:07:35 -08001923 .duration(800)
Simon Huntea80eb42014-11-11 13:46:57 -08001924 .style('opacity', 0);
Simon Huntea80eb42014-11-11 13:46:57 -08001925
Thomas Vachuska89543292014-11-19 11:28:33 -08001926 node.select('circle')
1927 .style('stroke-fill', '#555')
1928 .style('fill', '#888')
Simon Huntea80eb42014-11-11 13:46:57 -08001929 .style('opacity', 0.5)
1930 .transition()
1931 .duration(1500)
1932 .attr('r', 0);
Simon Huntea80eb42014-11-11 13:46:57 -08001933 });
1934
Simon Huntca867ac2014-11-28 18:07:35 -08001935 // device node exits....
1936 exiting.filter('.device').each(function (d) {
1937 var node = d.el;
1938 node.select('use')
1939 .style('opacity', 0.5)
1940 .transition()
1941 .duration(800)
1942 .style('opacity', 0);
1943
1944 node.selectAll('rect')
1945 .style('stroke-fill', '#555')
1946 .style('fill', '#888')
1947 .style('opacity', 0.5);
1948 });
Simon Hunt62c47542014-11-22 22:16:32 -08001949
1950 network.force.resume();
Simon Huntc7ee0662014-11-05 16:44:37 -08001951 }
1952
Simon Hunt95dad922014-11-24 09:43:31 -08001953 var dCol = {
1954 black: '#000',
1955 paleblue: '#acf',
1956 offwhite: '#ddd',
1957 midgrey: '#888',
1958 lightgrey: '#bbb',
1959 orange: '#f90'
1960 };
1961
Simon Huntb0ecfa52014-11-23 21:05:12 -08001962 // note: these are the device icon colors without affinity
Simon Hunt95dad922014-11-24 09:43:31 -08001963 var dColTheme = {
Simon Huntb0ecfa52014-11-23 21:05:12 -08001964 light: {
1965 online: {
Simon Hunt95dad922014-11-24 09:43:31 -08001966 glyph: dCol.black,
1967 rect: dCol.paleblue
Simon Huntb0ecfa52014-11-23 21:05:12 -08001968 },
1969 offline: {
Simon Hunt95dad922014-11-24 09:43:31 -08001970 glyph: dCol.midgrey,
1971 rect: dCol.lightgrey
Simon Huntb0ecfa52014-11-23 21:05:12 -08001972 }
1973 },
Simon Hunt95dad922014-11-24 09:43:31 -08001974 // TODO: theme
Simon Huntb0ecfa52014-11-23 21:05:12 -08001975 dark: {
1976 online: {
Simon Hunt95dad922014-11-24 09:43:31 -08001977 glyph: dCol.black,
1978 rect: dCol.paleblue
Simon Huntb0ecfa52014-11-23 21:05:12 -08001979 },
1980 offline: {
Simon Hunt95dad922014-11-24 09:43:31 -08001981 glyph: dCol.midgrey,
1982 rect: dCol.lightgrey
Simon Huntb0ecfa52014-11-23 21:05:12 -08001983 }
1984 }
1985 };
1986
1987 function devBaseColor(d) {
1988 var t = network.view.getTheme(),
1989 o = d.online ? 'online' : 'offline';
Simon Hunt95dad922014-11-24 09:43:31 -08001990 return dColTheme[t][o];
Simon Huntb0ecfa52014-11-23 21:05:12 -08001991 }
1992
1993 function setDeviceColor(d) {
1994 var o = d.online,
1995 s = d.el.classed('selected'),
1996 c = devBaseColor(d),
1997 a = instColor(d.master, o),
1998 g, r,
1999 icon = d.el.select('g.deviceIcon');
2000
2001 if (s) {
2002 g = c.glyph;
Simon Hunt95dad922014-11-24 09:43:31 -08002003 r = dColTheme.sel;
Simon Huntb0ecfa52014-11-23 21:05:12 -08002004 } else if (colorAffinity) {
2005 g = o ? a : c.glyph;
Simon Hunt95dad922014-11-24 09:43:31 -08002006 r = o ? dCol.offwhite : a;
Simon Huntb0ecfa52014-11-23 21:05:12 -08002007 } else {
2008 g = c.glyph;
2009 r = c.rect;
2010 }
2011
2012 icon.select('use')
2013 .style('fill', g);
2014 icon.select('rect')
2015 .style('fill', r);
2016 }
2017
Simon Huntc72967b2014-11-20 09:21:42 -08002018 function addDeviceIcon(node, box, noLabel, iid) {
2019 var cfg = config.icons.device,
2020 dx,
2021 dy,
2022 g;
2023
2024 if (noLabel) {
2025 box = emptyBox();
2026 dx = -cfg.dim/2;
2027 dy = -cfg.dim/2;
2028 } else {
2029 box = adjustRectToFitText(node);
Simon Hunt395a70c2014-11-22 23:17:40 -08002030 dx = box.x + cfg.xoff;
2031 dy = box.y + cfg.yoff;
Simon Huntc72967b2014-11-20 09:21:42 -08002032 }
2033
Simon Hunteb1514d2014-11-20 09:57:29 -08002034 g = node.append('g')
2035 .attr('class', 'glyphIcon deviceIcon')
Simon Huntc72967b2014-11-20 09:21:42 -08002036 .attr('transform', translate(dx, dy));
2037
2038 g.append('rect').attr({
2039 x: 0,
2040 y: 0,
2041 rx: cfg.rx,
2042 width: cfg.dim,
2043 height: cfg.dim
2044 });
2045
2046 g.append('use').attr({
2047 'xlink:href': iid,
2048 width: cfg.dim,
2049 height: cfg.dim
2050 });
2051
Simon Huntc72967b2014-11-20 09:21:42 -08002052 }
2053
Simon Hunt7b403bc2014-11-22 19:01:00 -08002054 function find(key, array, tag) {
Simon Huntca867ac2014-11-28 18:07:35 -08002055 var _tag = tag || 'id',
Simon Hunt7b403bc2014-11-22 19:01:00 -08002056 idx, n, d;
2057 for (idx = 0, n = array.length; idx < n; idx++) {
2058 d = array[idx];
2059 if (d[_tag] === key) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08002060 return idx;
2061 }
2062 }
2063 return -1;
2064 }
2065
Simon Huntca867ac2014-11-28 18:07:35 -08002066 function removeLinkElement(d) {
2067 var idx = find(d.key, network.links, 'key'),
Simon Hunt8257f4c2014-11-16 19:34:54 -08002068 removed;
2069 if (idx >=0) {
2070 // remove from links array
2071 removed = network.links.splice(idx, 1);
2072 // remove from lookup cache
2073 delete network.lookup[removed[0].key];
2074 updateLinks();
2075 network.force.resume();
2076 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08002077 }
Simon Huntc7ee0662014-11-05 16:44:37 -08002078
Simon Huntca867ac2014-11-28 18:07:35 -08002079 function removeHostElement(d, upd) {
2080 var lu = network.lookup;
Simon Hunt44031102014-11-11 13:20:36 -08002081 // first, remove associated hostLink...
Simon Huntca867ac2014-11-28 18:07:35 -08002082 removeLinkElement(d.linkData);
2083
2084 // remove hostLink bindings
2085 delete lu[d.ingress];
2086 delete lu[d.egress];
Simon Hunt44031102014-11-11 13:20:36 -08002087
2088 // remove from lookup cache
Simon Huntca867ac2014-11-28 18:07:35 -08002089 delete lu[d.id];
Simon Hunt44031102014-11-11 13:20:36 -08002090 // remove from nodes array
Simon Huntca867ac2014-11-28 18:07:35 -08002091 var idx = find(d.id, network.nodes);
2092 network.nodes.splice(idx, 1);
2093 // remove from SVG
2094 // NOTE: upd is false if we were called from removeDeviceElement()
2095 if (upd) {
2096 updateNodes();
2097 network.force.resume();
2098 }
2099 }
2100
2101
2102 function removeDeviceElement(d) {
2103 var id = d.id;
2104 // first, remove associated hosts and links..
2105 findAttachedHosts(id).forEach(removeHostElement);
2106 findAttachedLinks(id).forEach(removeLinkElement);
2107
2108 // remove from lookup cache
2109 delete network.lookup[id];
2110 // remove from nodes array
2111 var idx = find(id, network.nodes);
Simon Hunt44031102014-11-11 13:20:36 -08002112 network.nodes.splice(idx, 1);
2113 // remove from SVG
2114 updateNodes();
2115 network.force.resume();
2116 }
2117
Simon Huntca867ac2014-11-28 18:07:35 -08002118 function findAttachedHosts(devId) {
2119 var hosts = [];
2120 network.nodes.forEach(function (d) {
2121 if (d.class === 'host' && d.cp.device === devId) {
2122 hosts.push(d);
2123 }
2124 });
2125 return hosts;
2126 }
2127
2128 function findAttachedLinks(devId) {
2129 var links = [];
2130 network.links.forEach(function (d) {
2131 if (d.source.id === devId || d.target.id === devId) {
2132 links.push(d);
2133 }
2134 });
2135 return links;
2136 }
Simon Hunt44031102014-11-11 13:20:36 -08002137
Simon Huntc7ee0662014-11-05 16:44:37 -08002138 function tick() {
2139 node.attr({
Simon Hunt99c13842014-11-06 18:23:12 -08002140 transform: function (d) { return translate(d.x, d.y); }
Simon Huntc7ee0662014-11-05 16:44:37 -08002141 });
2142
2143 link.attr({
2144 x1: function (d) { return d.source.x; },
2145 y1: function (d) { return d.source.y; },
2146 x2: function (d) { return d.target.x; },
2147 y2: function (d) { return d.target.y; }
2148 });
Simon Hunte2575b62014-11-18 15:25:53 -08002149
2150 linkLabel.each(function (d) {
2151 var el = d3.select(this);
Thomas Vachuska4731f122014-11-20 04:56:19 -08002152 var lnk = findLinkById(d.key);
2153
2154 if (lnk) {
2155 var parms = {
Simon Hunte2575b62014-11-18 15:25:53 -08002156 x1: lnk.source.x,
2157 y1: lnk.source.y,
2158 x2: lnk.target.x,
2159 y2: lnk.target.y
2160 };
Thomas Vachuska4731f122014-11-20 04:56:19 -08002161 el.attr('transform', transformLabel(parms));
2162 }
Simon Hunte2575b62014-11-18 15:25:53 -08002163 });
Simon Huntc7ee0662014-11-05 16:44:37 -08002164 }
Simon Hunt934c3ce2014-11-05 11:45:07 -08002165
2166 // ==============================
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002167 // Web-Socket for live data
2168
2169 function webSockUrl() {
2170 return document.location.toString()
2171 .replace(/\#.*/, '')
2172 .replace('http://', 'ws://')
2173 .replace('https://', 'wss://')
Simon Hunte5ab1382014-11-25 10:28:51 -08002174 .replace('index.html', config.webSockUrl);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002175 }
2176
2177 webSock = {
2178 ws : null,
2179
2180 connect : function() {
2181 webSock.ws = new WebSocket(webSockUrl());
2182
2183 webSock.ws.onopen = function() {
Simon Hunt0c6d4192014-11-12 12:07:10 -08002184 noWebSock(false);
Thomas Vachuska47635c62014-11-22 01:21:36 -08002185 requestSummary();
Simon Huntb0ecfa52014-11-23 21:05:12 -08002186 showInstances();
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002187 };
2188
2189 webSock.ws.onmessage = function(m) {
2190 if (m.data) {
Simon Huntbb282f52014-11-10 11:08:19 -08002191 wsTraceRx(m.data);
Thomas Vachuskad472c6e2014-11-07 19:11:05 -08002192 handleServerEvent(JSON.parse(m.data));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002193 }
2194 };
2195
2196 webSock.ws.onclose = function(m) {
2197 webSock.ws = null;
Simon Hunt0c6d4192014-11-12 12:07:10 -08002198 noWebSock(true);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002199 };
2200 },
2201
2202 send : function(text) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002203 if (text != null) {
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002204 webSock._send(text);
2205 }
2206 },
2207
2208 _send : function(message) {
2209 if (webSock.ws) {
2210 webSock.ws.send(message);
Simon Hunta255a2c2014-11-13 22:29:35 -08002211 } else if (config.useLiveData) {
Simon Hunt434cf142014-11-24 11:10:28 -08002212 console.warn('no web socket open', message);
Simon Hunta255a2c2014-11-13 22:29:35 -08002213 } else {
Simon Hunt434cf142014-11-24 11:10:28 -08002214 console.log('WS Send: ', message);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002215 }
2216 }
2217
2218 };
2219
Simon Hunt0c6d4192014-11-12 12:07:10 -08002220 function noWebSock(b) {
2221 mask.style('display',b ? 'block' : 'none');
2222 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002223
2224 function sendMessage(evType, payload) {
2225 var toSend = {
Simon Huntbb282f52014-11-10 11:08:19 -08002226 event: evType,
2227 sid: ++sid,
2228 payload: payload
2229 },
2230 asText = JSON.stringify(toSend);
2231 wsTraceTx(asText);
2232 webSock.send(asText);
Simon Huntc76ae892014-11-18 17:31:51 -08002233
2234 // Temporary measure for debugging UI behavior ...
2235 if (!config.useLiveData) {
2236 handleTestSend(toSend);
2237 }
Simon Huntbb282f52014-11-10 11:08:19 -08002238 }
2239
2240 function wsTraceTx(msg) {
2241 wsTrace('tx', msg);
2242 }
2243 function wsTraceRx(msg) {
2244 wsTrace('rx', msg);
2245 }
2246 function wsTrace(rxtx, msg) {
Simon Huntbb282f52014-11-10 11:08:19 -08002247 console.log('[' + rxtx + '] ' + msg);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002248 }
2249
Simon Huntc76ae892014-11-18 17:31:51 -08002250 // NOTE: Temporary hardcoded example for showing detail pane
2251 // while we fine-
2252 // Probably should not merge this change...
2253 function handleTestSend(msg) {
2254 if (msg.event === 'requestDetails') {
2255 showDetails({
2256 event: 'showDetails',
2257 sid: 1001,
2258 payload: {
2259 "id": "of:0000ffffffffff09",
2260 "type": "roadm",
2261 "propOrder": [
2262 "Name",
2263 "Vendor",
2264 "H/W Version",
2265 "S/W Version",
2266 "-",
2267 "Latitude",
2268 "Longitude",
2269 "Ports"
2270 ],
2271 "props": {
2272 "Name": null,
2273 "Vendor": "Linc",
2274 "H/W Version": "OE",
2275 "S/W Version": "?",
2276 "-": "",
2277 "Latitude": "40.8",
2278 "Longitude": "73.1",
2279 "Ports": "2"
2280 }
2281 }
2282 });
2283 }
2284 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002285
2286 // ==============================
2287 // Selection stuff
2288
2289 function selectObject(obj, el) {
2290 var n,
Simon Hunt01095ff2014-11-13 16:37:29 -08002291 srcEv = d3.event.sourceEvent,
2292 meta = srcEv.metaKey,
2293 shift = srcEv.shiftKey;
2294
Simon Huntdeab4322014-11-13 18:49:07 -08002295 if ((panZoom() && !meta) || (!panZoom() && meta)) {
Simon Hunt01095ff2014-11-13 16:37:29 -08002296 return;
2297 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002298
2299 if (el) {
2300 n = d3.select(el);
2301 } else {
2302 node.each(function(d) {
2303 if (d == obj) {
2304 n = d3.select(el = this);
2305 }
2306 });
2307 }
2308 if (!n) return;
2309
Simon Hunt01095ff2014-11-13 16:37:29 -08002310 if (shift && n.classed('selected')) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002311 deselectObject(obj.id);
Simon Hunt61d04042014-11-11 17:27:16 -08002312 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002313 return;
2314 }
2315
Simon Hunt01095ff2014-11-13 16:37:29 -08002316 if (!shift) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002317 deselectAll();
2318 }
2319
Simon Huntc31d5692014-11-12 13:27:18 -08002320 selections[obj.id] = { obj: obj, el: el };
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002321 selectOrder.push(obj.id);
2322
2323 n.classed('selected', true);
Simon Huntb0ecfa52014-11-23 21:05:12 -08002324 updateDeviceColors(obj);
Simon Hunt61d04042014-11-11 17:27:16 -08002325 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002326 }
2327
2328 function deselectObject(id) {
Simon Huntc31d5692014-11-12 13:27:18 -08002329 var obj = selections[id],
2330 idx;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002331 if (obj) {
2332 d3.select(obj.el).classed('selected', false);
Simon Hunt61d04042014-11-11 17:27:16 -08002333 delete selections[id];
Simon Huntc31d5692014-11-12 13:27:18 -08002334 idx = $.inArray(id, selectOrder);
2335 if (idx >= 0) {
2336 selectOrder.splice(idx, 1);
2337 }
Simon Huntb0ecfa52014-11-23 21:05:12 -08002338 updateDeviceColors(obj.obj);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002339 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002340 }
2341
2342 function deselectAll() {
2343 // deselect all nodes in the network...
2344 node.classed('selected', false);
2345 selections = {};
2346 selectOrder = [];
Simon Huntb0ecfa52014-11-23 21:05:12 -08002347 updateDeviceColors();
Simon Hunt61d04042014-11-11 17:27:16 -08002348 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002349 }
2350
Simon Huntb0ecfa52014-11-23 21:05:12 -08002351 function updateDeviceColors(d) {
2352 if (d) {
2353 setDeviceColor(d);
2354 } else {
2355 node.filter('.device').each(function (d) {
2356 setDeviceColor(d);
2357 });
2358 }
Thomas Vachuska47635c62014-11-22 01:21:36 -08002359 }
2360
Simon Hunt61d04042014-11-11 17:27:16 -08002361 // update the state of the detail pane, based on current selections
2362 function updateDetailPane() {
2363 var nSel = selectOrder.length;
2364 if (!nSel) {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002365 emptySelect();
Simon Hunt61d04042014-11-11 17:27:16 -08002366 } else if (nSel === 1) {
2367 singleSelect();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002368 requestTrafficForMode();
Simon Hunt61d04042014-11-11 17:27:16 -08002369 } else {
2370 multiSelect();
2371 }
2372 }
2373
Thomas Vachuska9edca302014-11-22 17:06:42 -08002374 function emptySelect() {
Simon Hunt27d322d2014-11-28 10:45:43 -08002375 haveDetails = false;
Simon Hunt06811b72014-11-25 18:54:48 -08002376 hideDetailPane();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002377 cancelTraffic();
2378 }
2379
Simon Hunt61d04042014-11-11 17:27:16 -08002380 function singleSelect() {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002381 // NOTE: detail is shown from showDetails event callback
Simon Hunt61d04042014-11-11 17:27:16 -08002382 requestDetails();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002383 requestTrafficForMode();
Simon Hunt61d04042014-11-11 17:27:16 -08002384 }
2385
2386 function multiSelect() {
Simon Hunt27d322d2014-11-28 10:45:43 -08002387 haveDetails = true;
Simon Huntb53e0682014-11-12 13:32:01 -08002388 populateMultiSelect();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002389 requestTrafficForMode();
Simon Huntb53e0682014-11-12 13:32:01 -08002390 }
2391
2392 function addSep(tbody) {
2393 var tr = tbody.append('tr');
2394 $('<hr>').appendTo(tr.append('td').attr('colspan', 2));
2395 }
2396
2397 function addProp(tbody, label, value) {
2398 var tr = tbody.append('tr');
2399
2400 tr.append('td')
2401 .attr('class', 'label')
2402 .text(label + ' :');
2403
2404 tr.append('td')
2405 .attr('class', 'value')
2406 .text(value);
2407 }
2408
2409 function populateMultiSelect() {
2410 detailPane.empty();
2411
Simon Hunta3dd9572014-11-20 15:22:41 -08002412 var title = detailPane.append('h3'),
2413 table = detailPane.append('table'),
2414 tbody = table.append('tbody');
Simon Huntb53e0682014-11-12 13:32:01 -08002415
Thomas Vachuska4731f122014-11-20 04:56:19 -08002416 title.text('Selected Nodes');
Simon Huntb53e0682014-11-12 13:32:01 -08002417
2418 selectOrder.forEach(function (d, i) {
2419 addProp(tbody, i+1, d);
2420 });
Simon Huntd72bc702014-11-13 18:38:04 -08002421
2422 addMultiSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08002423 }
2424
Thomas Vachuska47635c62014-11-22 01:21:36 -08002425 // TODO: refactor to consolidate with populateDetails
2426 function populateSummary(data) {
2427 summaryPane.empty();
2428
2429 var svg = summaryPane.append('svg'),
2430 iid = iconGlyphUrl(data);
2431
2432 var title = summaryPane.append('h2'),
2433 table = summaryPane.append('table'),
2434 tbody = table.append('tbody');
2435
2436 appendGlyph(svg, 0, 0, 40, iid);
2437
2438 svg.append('use')
2439 .attr({
2440 class: 'birdBadge',
2441 transform: translate(8,12),
2442 'xlink:href': '#bird',
2443 width: 24,
2444 height: 24,
2445 fill: '#fff'
2446 });
2447
2448 title.text('ONOS Summary');
2449
2450 data.propOrder.forEach(function(p) {
2451 if (p === '-') {
2452 addSep(tbody);
2453 } else {
2454 addProp(tbody, p, data.props[p]);
2455 }
2456 });
2457 }
2458
Simon Hunt61d04042014-11-11 17:27:16 -08002459 function populateDetails(data) {
2460 detailPane.empty();
2461
Simon Hunta6a9fe72014-11-20 11:17:12 -08002462 var svg = detailPane.append('svg'),
2463 iid = iconGlyphUrl(data);
2464
Simon Hunta3dd9572014-11-20 15:22:41 -08002465 var title = detailPane.append('h2'),
2466 table = detailPane.append('table'),
2467 tbody = table.append('tbody');
Simon Hunt61d04042014-11-11 17:27:16 -08002468
Simon Hunta6a9fe72014-11-20 11:17:12 -08002469 appendGlyph(svg, 0, 0, 40, iid);
2470 title.text(data.id);
Simon Hunt61d04042014-11-11 17:27:16 -08002471
2472 data.propOrder.forEach(function(p) {
2473 if (p === '-') {
2474 addSep(tbody);
2475 } else {
2476 addProp(tbody, p, data.props[p]);
2477 }
2478 });
Simon Huntd72bc702014-11-13 18:38:04 -08002479
Thomas Vachuska4731f122014-11-20 04:56:19 -08002480 addSingleSelectActions(data);
Simon Hunt61d04042014-11-11 17:27:16 -08002481 }
2482
Thomas Vachuska4731f122014-11-20 04:56:19 -08002483 function addSingleSelectActions(data) {
Simon Huntd72bc702014-11-13 18:38:04 -08002484 detailPane.append('hr');
2485 // always want to allow 'show traffic'
Thomas Vachuska4731f122014-11-20 04:56:19 -08002486 addAction(detailPane, 'Show Related Traffic', showTrafficAction);
2487
2488 if (data.type === 'switch') {
2489 addAction(detailPane, 'Show Device Flows', showDeviceLinkFlowsAction);
2490 }
Simon Huntd72bc702014-11-13 18:38:04 -08002491 }
2492
2493 function addMultiSelectActions() {
2494 detailPane.append('hr');
2495 // always want to allow 'show traffic'
Thomas Vachuska4731f122014-11-20 04:56:19 -08002496 addAction(detailPane, 'Show Related Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08002497 // if exactly two hosts are selected, also want 'add host intent'
2498 if (nSel() === 2 && allSelectionsClass('host')) {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002499 addAction(detailPane, 'Create Host-to-Host Flow', addHostIntentAction);
2500 } else if (nSel() >= 2 && allSelectionsClass('host')) {
2501 addAction(detailPane, 'Create Multi-Source Flow', addMultiSourceIntentAction);
Simon Huntd72bc702014-11-13 18:38:04 -08002502 }
2503 }
2504
Simon Hunta5e89142014-11-14 07:00:33 -08002505 function addAction(panel, text, cb) {
2506 panel.append('div')
Simon Huntd72bc702014-11-13 18:38:04 -08002507 .classed('actionBtn', true)
2508 .text(text)
2509 .on('click', cb);
2510 }
2511
2512
Paul Greysonfcba0e82014-11-13 10:21:16 -08002513 function zoomPan(scale, translate) {
2514 zoomPanContainer.attr("transform", "translate(" + translate + ")scale(" + scale + ")");
2515 // keep the map lines constant width while zooming
Thomas Vachuska89543292014-11-19 11:28:33 -08002516 bgImg.style("stroke-width", 2.0 / scale + "px");
Paul Greysonfcba0e82014-11-13 10:21:16 -08002517 }
2518
2519 function resetZoomPan() {
2520 zoomPan(1, [0,0]);
2521 zoom.scale(1).translate([0,0]);
2522 }
2523
2524 function setupZoomPan() {
2525 function zoomed() {
Simon Huntdeab4322014-11-13 18:49:07 -08002526 if (!panZoom() ^ !d3.event.sourceEvent.metaKey) {
Paul Greysonfcba0e82014-11-13 10:21:16 -08002527 zoomPan(d3.event.scale, d3.event.translate);
2528 }
2529 }
2530
2531 zoom = d3.behavior.zoom()
2532 .translate([0, 0])
2533 .scale(1)
2534 .scaleExtent([1, 8])
2535 .on("zoom", zoomed);
2536
2537 svg.call(zoom);
2538 }
2539
Simon Hunt61d04042014-11-11 17:27:16 -08002540 // ==============================
2541 // Test harness code
Simon Hunt56d51852014-11-09 13:03:35 -08002542
2543 function prepareScenario(view, ctx, dbg) {
2544 var sc = scenario,
2545 urlSc = sc.evDir + ctx + sc.evScenario;
2546
2547 if (!ctx) {
2548 view.alert("No scenario specified (null ctx)");
2549 return;
2550 }
2551
2552 sc.view = view;
2553 sc.ctx = ctx;
2554 sc.debug = dbg;
2555 sc.evNumber = 0;
2556
2557 d3.json(urlSc, function(err, data) {
Simon Huntbb282f52014-11-10 11:08:19 -08002558 var p = data && data.params || {},
2559 desc = data && data.description || null,
Simon Huntfc274c92014-11-11 11:05:46 -08002560 intro = data && data.title;
Simon Huntbb282f52014-11-10 11:08:19 -08002561
Simon Hunt56d51852014-11-09 13:03:35 -08002562 if (err) {
2563 view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err);
2564 } else {
2565 sc.params = p;
Simon Huntbb282f52014-11-10 11:08:19 -08002566 if (desc) {
2567 intro += '\n\n ' + desc.join('\n ');
2568 }
2569 view.alert(intro);
Simon Hunt56d51852014-11-09 13:03:35 -08002570 }
2571 });
2572
2573 }
2574
Simon Hunt01095ff2014-11-13 16:37:29 -08002575 // ==============================
2576 // Toggle Buttons in masthead
Simon Hunt0c6d4192014-11-12 12:07:10 -08002577
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002578 // TODO: toggle button (and other widgets in the masthead) should be provided
2579 // by the framework; not generated by the view.
2580
Thomas Vachuska47635c62014-11-22 01:21:36 -08002581 //var showInstances;
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002582
Simon Hunt87514342014-11-24 16:41:27 -08002583/*
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002584 function addButtonBar(view) {
2585 var bb = d3.select('#mast')
2586 .append('span').classed('right', true).attr('id', 'bb');
2587
Simon Hunta5e89142014-11-14 07:00:33 -08002588 function mkTogBtn(text, cb) {
2589 return bb.append('span')
2590 .classed('btn', true)
2591 .text(text)
2592 .on('click', cb);
2593 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002594
Thomas Vachuska47635c62014-11-22 01:21:36 -08002595 //showInstances = mkTogBtn('Show Instances', toggleInst);
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002596 }
Simon Hunt87514342014-11-24 16:41:27 -08002597*/
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002598
Simon Huntdeab4322014-11-13 18:49:07 -08002599 function panZoom() {
Simon Hunte5b71752014-11-18 20:06:07 -08002600 return false;
Simon Hunta5e89142014-11-14 07:00:33 -08002601 }
2602
Simon Hunt7fa116d2014-11-17 14:16:55 -08002603 function loadGlyphs(svg) {
2604 var defs = svg.append('defs');
2605 gly.defBird(defs);
Simon Huntc72967b2014-11-20 09:21:42 -08002606 gly.defGlyphs(defs);
Simon Huntb82f6902014-11-22 11:53:15 -08002607 gly.defBadges(defs);
Simon Hunt7fa116d2014-11-17 14:16:55 -08002608 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002609
Simon Hunt395a70c2014-11-22 23:17:40 -08002610 function sendUpdateMeta(d, store) {
2611 var metaUi = {},
2612 ll;
2613
2614 if (store) {
2615 ll = geoMapProjection.invert([d.x, d.y]);
Simon Hunt62c47542014-11-22 22:16:32 -08002616 metaUi = {
2617 x: d.x,
2618 y: d.y,
2619 lng: ll[0],
2620 lat: ll[1]
2621 };
Simon Hunt395a70c2014-11-22 23:17:40 -08002622 }
Simon Hunt62c47542014-11-22 22:16:32 -08002623 d.metaUi = metaUi;
2624 sendMessage('updateMeta', {
2625 id: d.id,
2626 'class': d.class,
2627 'memento': metaUi
2628 });
2629 }
2630
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002631 // ==============================
Simon Hunt142d0032014-11-04 20:13:09 -08002632 // View life-cycle callbacks
Simon Hunt195cb382014-11-03 17:50:51 -08002633
Simon Huntf67722a2014-11-10 09:32:06 -08002634 function preload(view, ctx, flags) {
Simon Hunt142d0032014-11-04 20:13:09 -08002635 var w = view.width(),
2636 h = view.height(),
Simon Huntc7ee0662014-11-05 16:44:37 -08002637 fcfg = config.force,
2638 fpad = fcfg.pad,
2639 forceDim = [w - 2*fpad, h - 2*fpad];
Simon Hunt195cb382014-11-03 17:50:51 -08002640
Simon Hunt142d0032014-11-04 20:13:09 -08002641 // NOTE: view.$div is a D3 selection of the view's div
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002642 var viewBox = '0 0 ' + config.logicalSize + ' ' + config.logicalSize;
2643 svg = view.$div.append('svg').attr('viewBox', viewBox);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002644 setSize(svg, view);
2645
Simon Hunt6e18fe32014-11-29 13:35:41 -08002646 // load glyphs and filters...
Simon Hunt7fa116d2014-11-17 14:16:55 -08002647 loadGlyphs(svg);
Simon Hunt6e18fe32014-11-29 13:35:41 -08002648 d3u.appendGlow(svg);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002649
Paul Greysonfcba0e82014-11-13 10:21:16 -08002650 zoomPanContainer = svg.append('g').attr('id', 'zoomPanContainer');
Paul Greysonfcba0e82014-11-13 10:21:16 -08002651 setupZoomPan();
2652
Simon Huntc7ee0662014-11-05 16:44:37 -08002653 // group for the topology
Paul Greysonfcba0e82014-11-13 10:21:16 -08002654 topoG = zoomPanContainer.append('g')
Simon Huntd3b7d512014-11-12 15:48:41 -08002655 .attr('id', 'topo-G')
Simon Huntc7ee0662014-11-05 16:44:37 -08002656 .attr('transform', fcfg.translate());
2657
Simon Hunte2575b62014-11-18 15:25:53 -08002658 // subgroups for links, link labels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002659 linkG = topoG.append('g').attr('id', 'links');
Simon Hunte2575b62014-11-18 15:25:53 -08002660 linkLabelG = topoG.append('g').attr('id', 'linkLabels');
Simon Huntc7ee0662014-11-05 16:44:37 -08002661 nodeG = topoG.append('g').attr('id', 'nodes');
2662
Simon Hunte2575b62014-11-18 15:25:53 -08002663 // selection of links, linkLabels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002664 link = linkG.selectAll('.link');
Simon Hunte2575b62014-11-18 15:25:53 -08002665 linkLabel = linkLabelG.selectAll('.linkLabel');
Simon Huntc7ee0662014-11-05 16:44:37 -08002666 node = nodeG.selectAll('.node');
2667
Simon Hunt7cd48f32014-11-09 23:42:50 -08002668 function chrg(d) {
2669 return fcfg.charge[d.class] || -12000;
2670 }
Simon Hunt99c13842014-11-06 18:23:12 -08002671 function ldist(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002672 return fcfg.linkDistance[d.type] || 50;
Simon Hunt99c13842014-11-06 18:23:12 -08002673 }
2674 function lstrg(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002675 // 0.0 - 1.0
2676 return fcfg.linkStrength[d.type] || 1.0;
Simon Hunt99c13842014-11-06 18:23:12 -08002677 }
2678
Simon Hunt1a9eff92014-11-07 11:06:34 -08002679 function selectCb(d, self) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002680 selectObject(d, self);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002681 }
2682
2683 function atDragEnd(d, self) {
Simon Hunt56d51852014-11-09 13:03:35 -08002684 // once we've finished moving, pin the node in position
2685 d.fixed = true;
2686 d3.select(self).classed('fixed', true);
2687 if (config.useLiveData) {
Simon Hunt395a70c2014-11-22 23:17:40 -08002688 sendUpdateMeta(d, true);
Simon Hunta255a2c2014-11-13 22:29:35 -08002689 } else {
2690 console.log('Moving node ' + d.id + ' to [' + d.x + ',' + d.y + ']');
Simon Hunt1a9eff92014-11-07 11:06:34 -08002691 }
2692 }
2693
Simon Hunt6e18fe32014-11-29 13:35:41 -08002694 // predicate that indicates when dragging is active
2695 function dragEnabled() {
2696 // meta key pressed means we are zooming/panning (so disable drag)
2697 return dragAllowed && !d3.event.sourceEvent.metaKey;
2698 // dragAllowed will be set false when we are in oblique view
2699 // or when we 'lock' node positions
2700 }
2701
Simon Huntc7ee0662014-11-05 16:44:37 -08002702 // set up the force layout
2703 network.force = d3.layout.force()
2704 .size(forceDim)
2705 .nodes(network.nodes)
2706 .links(network.links)
Simon Hunt7cd48f32014-11-09 23:42:50 -08002707 .gravity(0.4)
2708 .friction(0.7)
2709 .charge(chrg)
Simon Hunt99c13842014-11-06 18:23:12 -08002710 .linkDistance(ldist)
2711 .linkStrength(lstrg)
Simon Huntc7ee0662014-11-05 16:44:37 -08002712 .on('tick', tick);
Simon Hunt195cb382014-11-03 17:50:51 -08002713
Simon Hunt01095ff2014-11-13 16:37:29 -08002714 network.drag = d3u.createDragBehavior(network.force,
Simon Hunt6e18fe32014-11-29 13:35:41 -08002715 selectCb, atDragEnd, dragEnabled);
2716
Simon Hunt0c6d4192014-11-12 12:07:10 -08002717
2718 // create mask layer for when we lose connection to server.
Simon Hunta5e89142014-11-14 07:00:33 -08002719 // TODO: this should be part of the framework
Simon Hunt6e18fe32014-11-29 13:35:41 -08002720
2721 function para(sel, text) {
2722 sel.append('p').text(text);
2723 }
2724
Simon Hunt0c6d4192014-11-12 12:07:10 -08002725 mask = view.$div.append('div').attr('id','topo-mask');
2726 para(mask, 'Oops!');
2727 para(mask, 'Web-socket connection to server closed...');
2728 para(mask, 'Try refreshing the page.');
Simon Hunt12ce12e2014-11-15 21:13:19 -08002729
2730 mask.append('svg')
2731 .attr({
2732 id: 'mask-bird',
2733 width: w,
2734 height: h
2735 })
2736 .append('g')
2737 .attr('transform', birdTranslate(w, h))
2738 .style('opacity', 0.3)
2739 .append('use')
2740 .attr({
2741 'xlink:href': '#bird',
2742 width: config.birdDim,
2743 height: config.birdDim,
2744 fill: '#111'
Thomas Vachuska89543292014-11-19 11:28:33 -08002745 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08002746 }
Simon Hunt195cb382014-11-03 17:50:51 -08002747
Simon Hunt01095ff2014-11-13 16:37:29 -08002748
Simon Hunt56d51852014-11-09 13:03:35 -08002749 function load(view, ctx, flags) {
Simon Huntf67722a2014-11-10 09:32:06 -08002750 // resize, in case the window was resized while we were not loaded
2751 resize(view, ctx, flags);
2752
Simon Hunt99c13842014-11-06 18:23:12 -08002753 // cache the view token, so network topo functions can access it
2754 network.view = view;
Simon Hunt56d51852014-11-09 13:03:35 -08002755 config.useLiveData = !flags.local;
2756
2757 if (!config.useLiveData) {
2758 prepareScenario(view, ctx, flags.debug);
2759 }
Simon Hunt99c13842014-11-06 18:23:12 -08002760
2761 // set our radio buttons and key bindings
Simon Hunt9462e8c2014-11-14 17:28:09 -08002762 layerBtnSet = view.setRadio(layerButtons);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002763 view.setKeys(keyDispatch);
Simon Hunt87514342014-11-24 16:41:27 -08002764 view.setGestures(gestures);
Simon Hunt195cb382014-11-03 17:50:51 -08002765
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002766 // patch in our "button bar" for now
2767 // TODO: implement a more official frameworky way of doing this..
Simon Hunt87514342014-11-24 16:41:27 -08002768 //addButtonBar(view);
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002769
Simon Huntd3b7d512014-11-12 15:48:41 -08002770 // Load map data asynchronously; complete startup after that..
2771 loadGeoJsonData();
Simon Hunta255a2c2014-11-13 22:29:35 -08002772 }
2773
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002774 function startAntTimer() {
Thomas Vachuska4731f122014-11-20 04:56:19 -08002775 if (!antTimer) {
2776 var pulses = [5, 3, 1.2, 3],
2777 pulse = 0;
2778 antTimer = setInterval(function () {
2779 pulse = pulse + 1;
2780 pulse = pulse === pulses.length ? 0 : pulse;
2781 d3.selectAll('.animated').style('stroke-width', pulses[pulse]);
2782 }, 200);
2783 }
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002784 }
2785
2786 function stopAntTimer() {
Simon Hunta255a2c2014-11-13 22:29:35 -08002787 if (antTimer) {
2788 clearInterval(antTimer);
2789 antTimer = null;
2790 }
Simon Huntd3b7d512014-11-12 15:48:41 -08002791 }
2792
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002793 function unload(view, ctx, flags) {
2794 stopAntTimer();
2795 }
2796
Simon Huntd3b7d512014-11-12 15:48:41 -08002797 // TODO: move these to config/state portion of script
Simon Hunta6a9fe72014-11-20 11:17:12 -08002798 var geoJsonUrl = 'json/map/continental_us.json',
Simon Huntd3b7d512014-11-12 15:48:41 -08002799 geoJson;
2800
2801 function loadGeoJsonData() {
2802 d3.json(geoJsonUrl, function (err, data) {
2803 if (err) {
2804 // fall back to USA map background
2805 loadStaticMap();
2806 } else {
2807 geoJson = data;
2808 loadGeoMap();
2809 }
2810
2811 // finally, connect to the server...
2812 if (config.useLiveData) {
2813 webSock.connect();
2814 }
2815 });
2816 }
2817
2818 function showBg() {
Simon Hunt434cf142014-11-24 11:10:28 -08002819 return visVal(config.options.showBackground);
Simon Huntd3b7d512014-11-12 15:48:41 -08002820 }
2821
2822 function loadStaticMap() {
2823 fnTrace('loadStaticMap', config.backgroundUrl);
2824 var w = network.view.width(),
2825 h = network.view.height();
2826
2827 // load the background image
2828 bgImg = svg.insert('svg:image', '#topo-G')
2829 .attr({
2830 id: 'topo-bg',
2831 width: w,
2832 height: h,
2833 'xlink:href': config.backgroundUrl
2834 })
2835 .style({
2836 visibility: showBg()
2837 });
2838 }
2839
2840 function loadGeoMap() {
2841 fnTrace('loadGeoMap', geoJsonUrl);
Simon Huntd3b7d512014-11-12 15:48:41 -08002842
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002843 // extracts the topojson data into geocoordinate-based geometry
2844 var topoData = topojson.feature(geoJson, geoJson.objects.states);
Simon Huntd3b7d512014-11-12 15:48:41 -08002845
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002846 // see: http://bl.ocks.org/mbostock/4707858
2847 geoMapProjection = d3.geo.mercator();
2848 var path = d3.geo.path().projection(geoMapProjection);
Simon Huntd3b7d512014-11-12 15:48:41 -08002849
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002850 geoMapProjection
2851 .scale(1)
2852 .translate([0, 0]);
Simon Huntd3b7d512014-11-12 15:48:41 -08002853
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002854 // [[x1,y1],[x2,y2]]
2855 var b = path.bounds(topoData);
Paul Greysonfcba0e82014-11-13 10:21:16 -08002856 // size map to 95% of minimum dimension to fill space
2857 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 -08002858 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 -08002859
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002860 geoMapProjection
2861 .scale(s)
2862 .translate(t);
2863
Paul Greysonfcba0e82014-11-13 10:21:16 -08002864 bgImg = zoomPanContainer.insert("g", '#topo-G');
Thomas Vachuska89543292014-11-19 11:28:33 -08002865 bgImg.attr('id', 'map').selectAll('path')
2866 .data(topoData.features)
2867 .enter()
2868 .append('path')
2869 .attr('d', path);
Simon Hunt195cb382014-11-03 17:50:51 -08002870 }
2871
Simon Huntf67722a2014-11-10 09:32:06 -08002872 function resize(view, ctx, flags) {
Simon Hunt12ce12e2014-11-15 21:13:19 -08002873 var w = view.width(),
2874 h = view.height();
2875
Simon Hunt934c3ce2014-11-05 11:45:07 -08002876 setSize(svg, view);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002877
2878 d3.select('#mask-bird').attr({ width: w, height: h})
2879 .select('g').attr('transform', birdTranslate(w, h));
Simon Hunt142d0032014-11-04 20:13:09 -08002880 }
2881
Simon Hunt8f40cce2014-11-23 15:57:30 -08002882 function theme(view, ctx, flags) {
2883 updateInstances();
Simon Huntb0ecfa52014-11-23 21:05:12 -08002884 updateDeviceColors();
Simon Hunt8f40cce2014-11-23 15:57:30 -08002885 }
2886
Simon Hunt12ce12e2014-11-15 21:13:19 -08002887 function birdTranslate(w, h) {
2888 var bdim = config.birdDim;
2889 return 'translate('+((w-bdim)*.4)+','+((h-bdim)*.1)+')';
2890 }
Simon Hunt142d0032014-11-04 20:13:09 -08002891
Simon Hunt06811b72014-11-25 18:54:48 -08002892 function isF(f) { return $.isFunction(f) ? f : null; }
2893 function noop() {}
2894
2895 function augmentDetailPane() {
2896 var dp = detailPane;
2897 dp.ypos = { up: 64, down: 320, current: 320};
2898
2899 dp._move = function (y, cb) {
2900 var endCb = isF(cb) || noop,
2901 yp = dp.ypos;
2902 if (yp.current !== y) {
2903 yp.current = y;
2904 dp.el.transition().duration(300)
2905 .each('end', endCb)
2906 .style('top', yp.current + 'px');
2907 } else {
2908 endCb();
2909 }
2910 };
2911
2912 dp.down = function (cb) { dp._move(dp.ypos.down, cb); };
2913 dp.up = function (cb) { dp._move(dp.ypos.up, cb); };
2914 }
2915
Simon Hunt142d0032014-11-04 20:13:09 -08002916 // ==============================
2917 // View registration
Simon Hunt195cb382014-11-03 17:50:51 -08002918
Simon Hunt25248912014-11-04 11:25:48 -08002919 onos.ui.addView('topo', {
Simon Hunt142d0032014-11-04 20:13:09 -08002920 preload: preload,
2921 load: load,
Simon Hunta255a2c2014-11-13 22:29:35 -08002922 unload: unload,
Simon Hunt8f40cce2014-11-23 15:57:30 -08002923 resize: resize,
2924 theme: theme
Simon Hunt195cb382014-11-03 17:50:51 -08002925 });
2926
Thomas Vachuska47635c62014-11-22 01:21:36 -08002927 summaryPane = onos.ui.addFloatingPanel('topo-summary');
Simon Hunt61d04042014-11-11 17:27:16 -08002928 detailPane = onos.ui.addFloatingPanel('topo-detail');
Simon Hunt06811b72014-11-25 18:54:48 -08002929 augmentDetailPane();
Simon Hunta5e89142014-11-14 07:00:33 -08002930 oiBox = onos.ui.addFloatingPanel('topo-oibox', 'TL');
Simon Huntb82f6902014-11-22 11:53:15 -08002931 oiBox.width(20);
Simon Hunt61d04042014-11-11 17:27:16 -08002932
Simon Hunt195cb382014-11-03 17:50:51 -08002933}(ONOS));