blob: 5b46e3d6d85419ede82b755065f9a3849140c50e [file] [log] [blame]
Simon Hunt195cb382014-11-03 17:50:51 -08001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
Simon Hunt142d0032014-11-04 20:13:09 -080018 ONOS network topology viewer - version 1.1
Simon Hunt195cb382014-11-03 17:50:51 -080019
20 @author Simon Hunt
Simon Hunta6a9fe72014-11-20 11:17:12 -080021 @author Thomas Vachuska
Simon Hunt195cb382014-11-03 17:50:51 -080022 */
23
24(function (onos) {
25 'use strict';
26
Simon Hunt1a9eff92014-11-07 11:06:34 -080027 // shorter names for library APIs
Simon Huntbb282f52014-11-10 11:08:19 -080028 var d3u = onos.lib.d3util,
Simon Hunta6a9fe72014-11-20 11:17:12 -080029 gly = onos.lib.glyphs;
Simon Hunt1a9eff92014-11-07 11:06:34 -080030
Simon Hunt195cb382014-11-03 17:50:51 -080031 // configuration data
32 var config = {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -080033 useLiveData: true,
Simon Huntfc274c92014-11-11 11:05:46 -080034 fnTrace: true,
Simon Hunt195cb382014-11-03 17:50:51 -080035 debugOn: false,
36 debug: {
Simon Hunt99c13842014-11-06 18:23:12 -080037 showNodeXY: true,
38 showKeyHandler: false
Simon Hunt195cb382014-11-03 17:50:51 -080039 },
Simon Hunt12ce12e2014-11-15 21:13:19 -080040 birdDim: 400,
Simon Hunt195cb382014-11-03 17:50:51 -080041 options: {
42 layering: true,
43 collisionPrevention: true,
Simon Hunt142d0032014-11-04 20:13:09 -080044 showBackground: true
Simon Hunt195cb382014-11-03 17:50:51 -080045 },
46 backgroundUrl: 'img/us-map.png',
Thomas Vachuska7d638d32014-11-07 10:24:43 -080047 webSockUrl: 'ws/topology',
Simon Hunt195cb382014-11-03 17:50:51 -080048 data: {
49 live: {
50 jsonUrl: 'rs/topology/graph',
51 detailPrefix: 'rs/topology/graph/',
52 detailSuffix: ''
53 },
54 fake: {
55 jsonUrl: 'json/network2.json',
56 detailPrefix: 'json/',
57 detailSuffix: '.json'
58 }
59 },
Simon Hunt99c13842014-11-06 18:23:12 -080060 labels: {
61 imgPad: 16,
62 padLR: 4,
63 padTB: 3,
64 marginLR: 3,
65 marginTB: 2,
66 port: {
67 gap: 3,
68 width: 18,
69 height: 14
70 }
71 },
Simon Hunt1a9eff92014-11-07 11:06:34 -080072 topo: {
Thomas Vachuska89543292014-11-19 11:28:33 -080073 linkBaseColor: '#666',
Simon Hunt1a9eff92014-11-07 11:06:34 -080074 linkInColor: '#66f',
Thomas Vachuska9edca302014-11-22 17:06:42 -080075 linkInWidth: 12,
Thomas Vachuska89543292014-11-19 11:28:33 -080076 linkOutColor: '#f00',
Thomas Vachuska9edca302014-11-22 17:06:42 -080077 linkOutWidth: 10
Simon Hunt1a9eff92014-11-07 11:06:34 -080078 },
Paul Greyson29cd58f2014-11-18 13:14:57 -080079 icons: {
Simon Huntc72967b2014-11-20 09:21:42 -080080 device: {
Simon Hunt395a70c2014-11-22 23:17:40 -080081 dim: 36,
82 rx: 4,
83 xoff: -20,
84 yoff: -18
85 },
86 host: {
87 defaultRadius: 9,
88 radius: {
89 endstation: 14,
90 bgpSpeaker: 14,
91 router: 14
92 }
Simon Huntc72967b2014-11-20 09:21:42 -080093 }
Thomas Vachuska89543292014-11-19 11:28:33 -080094 },
Simon Hunt195cb382014-11-03 17:50:51 -080095 force: {
Simon Hunt7cd48f32014-11-09 23:42:50 -080096 note_for_links: 'link.type is used to differentiate',
Simon Huntc7ee0662014-11-05 16:44:37 -080097 linkDistance: {
Simon Hunt7cd48f32014-11-09 23:42:50 -080098 direct: 100,
99 optical: 120,
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800100 hostLink: 3
Simon Huntc7ee0662014-11-05 16:44:37 -0800101 },
102 linkStrength: {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800103 direct: 1.0,
104 optical: 1.0,
105 hostLink: 1.0
Simon Huntc7ee0662014-11-05 16:44:37 -0800106 },
Simon Hunt7cd48f32014-11-09 23:42:50 -0800107 note_for_nodes: 'node.class is used to differentiate',
Simon Huntc7ee0662014-11-05 16:44:37 -0800108 charge: {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800109 device: -8000,
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800110 host: -5000
Simon Huntc7ee0662014-11-05 16:44:37 -0800111 },
112 pad: 20,
Simon Hunt195cb382014-11-03 17:50:51 -0800113 translate: function() {
114 return 'translate(' +
Simon Huntc7ee0662014-11-05 16:44:37 -0800115 config.force.pad + ',' +
116 config.force.pad + ')';
Simon Hunt195cb382014-11-03 17:50:51 -0800117 }
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800118 },
119 // see below in creation of viewBox on main svg
120 logicalSize: 1000
Simon Hunt195cb382014-11-03 17:50:51 -0800121 };
122
Simon Hunt142d0032014-11-04 20:13:09 -0800123 // radio buttons
Simon Hunt9462e8c2014-11-14 17:28:09 -0800124 var layerButtons = [
125 { text: 'All Layers', id: 'all', cb: showAllLayers },
126 { text: 'Packet Only', id: 'pkt', cb: showPacketLayer },
127 { text: 'Optical Only', id: 'opt', cb: showOpticalLayer }
128 ],
129 layerBtnSet,
130 layerBtnDispatch = {
131 all: showAllLayers,
132 pkt: showPacketLayer,
133 opt: showOpticalLayer
134 };
Simon Hunt934c3ce2014-11-05 11:45:07 -0800135
136 // key bindings
137 var keyDispatch = {
Simon Hunt988c6fc2014-11-20 17:43:03 -0800138 // TODO: remove these "development only" bindings
Simon Hunt8f40cce2014-11-23 15:57:30 -0800139 0: testMe,
140 equals: injectStartupEvents,
141 dash: injectTestEvent,
Simon Hunt99c13842014-11-06 18:23:12 -0800142
Thomas Vachuska47635c62014-11-22 01:21:36 -0800143 O: [toggleSummary, 'Toggle ONOS summary pane'],
144 I: [toggleInstances, 'Toggle ONOS instances pane'],
Simon Hunt27d322d2014-11-28 10:45:43 -0800145 D: [toggleDetails, 'Disable / enable details pane'],
Simon Hunt988c6fc2014-11-20 17:43:03 -0800146 B: [toggleBg, 'Toggle background image'],
Simon Hunt434cf142014-11-24 11:10:28 -0800147 H: [toggleHosts, 'Toggle host visibility'],
Simon Hunt27d322d2014-11-28 10:45:43 -0800148 L: [cycleLabels, 'Cycle device labels'],
Simon Hunt934c3ce2014-11-05 11:45:07 -0800149 P: togglePorts,
Simon Hunt87514342014-11-24 16:41:27 -0800150 U: [unpin, 'Unpin node (hover mouse over)'],
151 R: [resetZoomPan, 'Reset zoom / pan'],
Thomas Vachuska9edca302014-11-22 17:06:42 -0800152 V: [showTrafficAction, 'Show related traffic'],
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800153 A: [showAllTrafficAction, 'Show all traffic'],
154 F: [showDeviceLinkFlowsAction, 'Show device link flows'],
Simon Hunt9462e8c2014-11-14 17:28:09 -0800155 esc: handleEscape
Simon Hunt934c3ce2014-11-05 11:45:07 -0800156 };
Simon Hunt142d0032014-11-04 20:13:09 -0800157
Simon Hunt87514342014-11-24 16:41:27 -0800158 // mouse gestures
159 var gestures = [
160 ['click', 'Select the item and show details'],
161 ['shift-click', 'Toggle selection state'],
162 ['drag', 'Reposition (and pin) device / host'],
163 ['cmd-scroll', 'Zoom in / out'],
164 ['cmd-drag', 'Pan']
165 ];
166
Simon Hunt195cb382014-11-03 17:50:51 -0800167 // state variables
Simon Hunt99c13842014-11-06 18:23:12 -0800168 var network = {
Simon Hunt50128c02014-11-08 13:36:15 -0800169 view: null, // view token reference
Simon Hunt99c13842014-11-06 18:23:12 -0800170 nodes: [],
171 links: [],
Simon Hunt269670f2014-11-17 16:17:43 -0800172 lookup: {},
173 revLinkToKey: {}
Simon Hunt99c13842014-11-06 18:23:12 -0800174 },
Simon Hunt56d51852014-11-09 13:03:35 -0800175 scenario = {
176 evDir: 'json/ev/',
177 evScenario: '/scenario.json',
178 evPrefix: '/ev_',
179 evOnos: '_onos.json',
180 evUi: '_ui.json',
181 ctx: null,
182 params: {},
183 evNumber: 0,
Simon Hunt434cf142014-11-24 11:10:28 -0800184 view: null
Simon Hunt56d51852014-11-09 13:03:35 -0800185 },
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800186 webSock,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800187 sid = 0,
Thomas Vachuska60d72bf2014-11-21 13:02:00 -0800188 deviceLabelCount = 3,
Simon Hunt209155e2014-11-21 12:16:09 -0800189 hostLabelCount = 2,
Simon Hunt56d51852014-11-09 13:03:35 -0800190 deviceLabelIndex = 0,
191 hostLabelIndex = 0,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800192 selections = {},
Simon Hunta5e89142014-11-14 07:00:33 -0800193 selectOrder = [],
Simon Hunt6ac93f32014-11-13 12:17:27 -0800194 hovered = null,
Thomas Vachuska47635c62014-11-22 01:21:36 -0800195 summaryPane,
Simon Hunta5e89142014-11-14 07:00:33 -0800196 detailPane,
Simon Hunta255a2c2014-11-13 22:29:35 -0800197 antTimer = null,
Simon Hunta5e89142014-11-14 07:00:33 -0800198 onosInstances = {},
199 onosOrder = [],
200 oiBox,
Simon Hunt9462e8c2014-11-14 17:28:09 -0800201 oiShowMaster = false,
Simon Hunt8f40cce2014-11-23 15:57:30 -0800202 portLabelsOn = false,
Simon Huntb0ecfa52014-11-23 21:05:12 -0800203 cat7 = d3u.cat7(),
Simon Hunt434cf142014-11-24 11:10:28 -0800204 colorAffinity = false,
Simon Hunt27d322d2014-11-28 10:45:43 -0800205 showHosts = false,
206 useDetails = true,
207 haveDetails = false;
Simon Hunt195cb382014-11-03 17:50:51 -0800208
Simon Hunt434cf142014-11-24 11:10:28 -0800209 // constants
Thomas Vachuska9edca302014-11-22 17:06:42 -0800210 var hoverModeAll = 1,
211 hoverModeFlows = 2,
212 hoverModeIntents = 3,
213 hoverMode = hoverModeFlows;
214
Simon Hunt934c3ce2014-11-05 11:45:07 -0800215 // D3 selections
216 var svg,
Paul Greysonfcba0e82014-11-13 10:21:16 -0800217 zoomPanContainer,
Simon Hunt934c3ce2014-11-05 11:45:07 -0800218 bgImg,
Simon Huntc7ee0662014-11-05 16:44:37 -0800219 topoG,
220 nodeG,
221 linkG,
Simon Hunte2575b62014-11-18 15:25:53 -0800222 linkLabelG,
Simon Huntc7ee0662014-11-05 16:44:37 -0800223 node,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800224 link,
Simon Hunte2575b62014-11-18 15:25:53 -0800225 linkLabel,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800226 mask;
Simon Hunt195cb382014-11-03 17:50:51 -0800227
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800228 // the projection for the map background
229 var geoMapProjection;
230
Paul Greysonfcba0e82014-11-13 10:21:16 -0800231 // the zoom function
232 var zoom;
233
Simon Hunt142d0032014-11-04 20:13:09 -0800234 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800235 // For Debugging / Development
Simon Hunt195cb382014-11-03 17:50:51 -0800236
Simon Hunt99c13842014-11-06 18:23:12 -0800237 function note(label, msg) {
238 console.log('NOTE: ' + label + ': ' + msg);
Simon Hunt195cb382014-11-03 17:50:51 -0800239 }
240
Simon Hunt99c13842014-11-06 18:23:12 -0800241 function debug(what) {
242 return config.debugOn && config.debug[what];
Simon Hunt934c3ce2014-11-05 11:45:07 -0800243 }
244
Simon Huntfc274c92014-11-11 11:05:46 -0800245 function fnTrace(msg, id) {
246 if (config.fnTrace) {
247 console.log('FN: ' + msg + ' [' + id + ']');
248 }
249 }
Simon Hunt99c13842014-11-06 18:23:12 -0800250
Simon Hunta5e89142014-11-14 07:00:33 -0800251 function evTrace(data) {
252 fnTrace(data.event, data.payload.id);
253 }
254
Simon Hunt934c3ce2014-11-05 11:45:07 -0800255 // ==============================
256 // Key Callbacks
257
Simon Hunt27d322d2014-11-28 10:45:43 -0800258 function flash(txt) {
259 network.view.flash(txt);
260 }
261
Simon Hunt99c13842014-11-06 18:23:12 -0800262 function testMe(view) {
Simon Hunt8f40cce2014-11-23 15:57:30 -0800263 //view.alert('Theme is ' + view.getTheme());
Simon Hunta3dd9572014-11-20 15:22:41 -0800264 //view.flash('This is some text');
Simon Hunt8f40cce2014-11-23 15:57:30 -0800265 cat7.testCard(svg);
Simon Hunt99c13842014-11-06 18:23:12 -0800266 }
267
Simon Hunt56d51852014-11-09 13:03:35 -0800268 function injectTestEvent(view) {
Simon Hunt434cf142014-11-24 11:10:28 -0800269 if (config.useLiveData) { return; }
270
Simon Hunt56d51852014-11-09 13:03:35 -0800271 var sc = scenario,
272 evn = ++sc.evNumber,
273 pfx = sc.evDir + sc.ctx + sc.evPrefix + evn,
274 onosUrl = pfx + sc.evOnos,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800275 uiUrl = pfx + sc.evUi,
276 stack = [
277 { url: onosUrl, cb: handleServerEvent },
278 { url: uiUrl, cb: handleUiEvent }
279 ];
280 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800281 }
282
Simon Hunt7cd48f32014-11-09 23:42:50 -0800283 function recurseFetchEvent(stack, evn) {
284 var v = scenario.view,
285 frame;
286 if (stack.length === 0) {
Simon Huntfc274c92014-11-11 11:05:46 -0800287 v.alert('Oops!\n\nNo event #' + evn + ' found.');
Simon Hunt7cd48f32014-11-09 23:42:50 -0800288 return;
289 }
290 frame = stack.shift();
291
292 d3.json(frame.url, function (err, data) {
Simon Hunt99c13842014-11-06 18:23:12 -0800293 if (err) {
Simon Hunt56d51852014-11-09 13:03:35 -0800294 if (err.status === 404) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800295 // if we didn't find the data, try the next stack frame
296 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800297 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800298 v.alert('non-404 error:\n\n' + frame.url + '\n\n' + err);
Simon Hunt56d51852014-11-09 13:03:35 -0800299 }
Simon Hunt99c13842014-11-06 18:23:12 -0800300 } else {
Simon Hunt1712ed82014-11-17 12:56:00 -0800301 wsTrace('test', JSON.stringify(data));
Simon Hunt7cd48f32014-11-09 23:42:50 -0800302 frame.cb(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800303 }
304 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800305
Simon Hunt56d51852014-11-09 13:03:35 -0800306 }
Simon Hunt50128c02014-11-08 13:36:15 -0800307
Simon Hunt56d51852014-11-09 13:03:35 -0800308 function handleUiEvent(data) {
Simon Huntbb282f52014-11-10 11:08:19 -0800309 scenario.view.alert('UI Tx: ' + data.event + '\n\n' +
310 JSON.stringify(data));
Simon Hunt56d51852014-11-09 13:03:35 -0800311 }
312
313 function injectStartupEvents(view) {
314 var last = scenario.params.lastAuto || 0;
Simon Hunt434cf142014-11-24 11:10:28 -0800315 if (config.useLiveData) { return; }
Simon Hunt56d51852014-11-09 13:03:35 -0800316
317 while (scenario.evNumber < last) {
Simon Hunt1a9eff92014-11-07 11:06:34 -0800318 injectTestEvent(view);
319 }
320 }
321
Simon Hunt934c3ce2014-11-05 11:45:07 -0800322 function toggleBg() {
323 var vis = bgImg.style('visibility');
Simon Hunt434cf142014-11-24 11:10:28 -0800324 bgImg.style('visibility', visVal(vis === 'hidden'));
325 }
326
327 function toggleHosts() {
328 showHosts = !showHosts;
329 updateHostVisibility();
Simon Hunt27d322d2014-11-28 10:45:43 -0800330 flash('Hosts ' + visVal(showHosts));
Simon Hunt934c3ce2014-11-05 11:45:07 -0800331 }
332
Simon Hunt99c13842014-11-06 18:23:12 -0800333 function cycleLabels() {
Thomas Vachuska60d72bf2014-11-21 13:02:00 -0800334 deviceLabelIndex = (deviceLabelIndex === 2)
Simon Huntbb282f52014-11-10 11:08:19 -0800335 ? 0 : deviceLabelIndex + 1;
Simon Hunt5f36d342014-11-08 21:33:14 -0800336
Simon Hunt99c13842014-11-06 18:23:12 -0800337 network.nodes.forEach(function (d) {
Simon Huntbb282f52014-11-10 11:08:19 -0800338 if (d.class === 'device') {
339 updateDeviceLabel(d);
340 }
Simon Hunt99c13842014-11-06 18:23:12 -0800341 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800342 }
343
344 function togglePorts(view) {
Simon Hunt434cf142014-11-24 11:10:28 -0800345 //view.alert('togglePorts() callback')
Simon Hunt934c3ce2014-11-05 11:45:07 -0800346 }
347
Simon Hunt6ac93f32014-11-13 12:17:27 -0800348 function unpin() {
349 if (hovered) {
Simon Hunt395a70c2014-11-22 23:17:40 -0800350 sendUpdateMeta(hovered);
Simon Hunt6ac93f32014-11-13 12:17:27 -0800351 hovered.fixed = false;
352 hovered.el.classed('fixed', false);
353 network.force.resume();
354 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800355 }
356
Simon Hunt9462e8c2014-11-14 17:28:09 -0800357 function handleEscape(view) {
358 if (oiShowMaster) {
359 cancelAffinity();
Simon Hunt27d322d2014-11-28 10:45:43 -0800360 } else if (haveDetails) {
Simon Hunt9462e8c2014-11-14 17:28:09 -0800361 deselectAll();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800362 } else if (oiBox.isVisible()) {
Simon Huntb0ecfa52014-11-23 21:05:12 -0800363 hideInstances();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800364 } else if (summaryPane.isVisible()) {
365 cancelSummary();
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800366 stopAntTimer();
367 } else {
368 hoverMode = hoverModeFlows;
Simon Hunt9462e8c2014-11-14 17:28:09 -0800369 }
370 }
371
Simon Hunt934c3ce2014-11-05 11:45:07 -0800372 // ==============================
373 // Radio Button Callbacks
374
Simon Hunta5e89142014-11-14 07:00:33 -0800375 var layerLookup = {
376 host: {
Simon Hunt209155e2014-11-21 12:16:09 -0800377 endstation: 'pkt', // default, if host event does not define type
Thomas Vachuska89543292014-11-19 11:28:33 -0800378 router: 'pkt',
Simon Hunta5e89142014-11-14 07:00:33 -0800379 bgpSpeaker: 'pkt'
380 },
381 device: {
382 switch: 'pkt',
383 roadm: 'opt'
384 },
385 link: {
386 hostLink: 'pkt',
387 direct: 'pkt',
Simon Hunt8257f4c2014-11-16 19:34:54 -0800388 indirect: '',
389 tunnel: '',
Simon Hunta5e89142014-11-14 07:00:33 -0800390 optical: 'opt'
391 }
392 };
393
394 function inLayer(d, layer) {
Simon Hunt8257f4c2014-11-16 19:34:54 -0800395 var type = d.class === 'link' ? d.type() : d.type,
396 look = layerLookup[d.class],
397 lyr = look && look[type];
Simon Hunta5e89142014-11-14 07:00:33 -0800398 return lyr === layer;
399 }
400
401 function unsuppressLayer(which) {
402 node.each(function (d) {
403 var node = d.el;
404 if (inLayer(d, which)) {
405 node.classed('suppressed', false);
406 }
407 });
408
409 link.each(function (d) {
410 var link = d.el;
411 if (inLayer(d, which)) {
412 link.classed('suppressed', false);
413 }
414 });
415 }
416
Simon Hunt9462e8c2014-11-14 17:28:09 -0800417 function suppressLayers(b) {
418 node.classed('suppressed', b);
419 link.classed('suppressed', b);
Simon Hunt142d0032014-11-04 20:13:09 -0800420// d3.selectAll('svg .port').classed('inactive', false);
421// d3.selectAll('svg .portText').classed('inactive', false);
Simon Hunt195cb382014-11-03 17:50:51 -0800422 }
423
Simon Hunt9462e8c2014-11-14 17:28:09 -0800424 function showAllLayers() {
425 suppressLayers(false);
426 }
427
Simon Hunt195cb382014-11-03 17:50:51 -0800428 function showPacketLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800429 node.classed('suppressed', true);
430 link.classed('suppressed', true);
431 unsuppressLayer('pkt');
Simon Hunt195cb382014-11-03 17:50:51 -0800432 }
433
434 function showOpticalLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800435 node.classed('suppressed', true);
436 link.classed('suppressed', true);
437 unsuppressLayer('opt');
Simon Hunt195cb382014-11-03 17:50:51 -0800438 }
439
Simon Hunt9462e8c2014-11-14 17:28:09 -0800440 function restoreLayerState() {
441 layerBtnDispatch[layerBtnSet.selected()]();
442 }
443
Simon Hunt142d0032014-11-04 20:13:09 -0800444 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800445 // Private functions
446
Simon Hunt99c13842014-11-06 18:23:12 -0800447 function safeId(s) {
448 return s.replace(/[^a-z0-9]/gi, '-');
449 }
450
Simon Huntc7ee0662014-11-05 16:44:37 -0800451 // set the size of the given element to that of the view (reduced if padded)
452 function setSize(el, view, pad) {
453 var padding = pad ? pad * 2 : 0;
Simon Hunt934c3ce2014-11-05 11:45:07 -0800454 el.attr({
Simon Huntc7ee0662014-11-05 16:44:37 -0800455 width: view.width() - padding,
456 height: view.height() - padding
Simon Hunt934c3ce2014-11-05 11:45:07 -0800457 });
458 }
459
Simon Hunt8257f4c2014-11-16 19:34:54 -0800460 function makeNodeKey(d, what) {
461 var port = what + 'Port';
462 return d[what] + '/' + d[port];
463 }
464
465 function makeLinkKey(d, flipped) {
466 var one = flipped ? makeNodeKey(d, 'dst') : makeNodeKey(d, 'src'),
467 two = flipped ? makeNodeKey(d, 'src') : makeNodeKey(d, 'dst');
468 return one + '-' + two;
469 }
470
Simon Hunt269670f2014-11-17 16:17:43 -0800471 function findLinkById(id) {
472 // check to see if this is a reverse lookup, else default to given id
473 var key = network.revLinkToKey[id] || id;
474 return key && network.lookup[key];
475 }
476
Simon Hunt8257f4c2014-11-16 19:34:54 -0800477 function findLink(linkData, op) {
478 var key = makeLinkKey(linkData),
479 keyrev = makeLinkKey(linkData, 1),
480 link = network.lookup[key],
481 linkRev = network.lookup[keyrev],
482 result = {},
483 ldata = link || linkRev,
484 rawLink;
485
486 if (op === 'add') {
487 if (link) {
488 // trying to add a link that we already know about
489 result.ldata = link;
490 result.badLogic = 'addLink: link already added';
491
492 } else if (linkRev) {
493 // we found the reverse of the link to be added
494 result.ldata = linkRev;
495 if (linkRev.fromTarget) {
496 result.badLogic = 'addLink: link already added';
497 }
498 }
499 } else if (op === 'update') {
500 if (!ldata) {
501 result.badLogic = 'updateLink: link not found';
502 } else {
503 rawLink = link ? ldata.fromSource : ldata.fromTarget;
504 result.updateWith = function (data) {
505 $.extend(rawLink, data);
506 restyleLinkElement(ldata);
507 }
508 }
509 } else if (op === 'remove') {
510 if (!ldata) {
511 result.badLogic = 'removeLink: link not found';
512 } else {
513 rawLink = link ? ldata.fromSource : ldata.fromTarget;
514
515 if (!rawLink) {
516 result.badLogic = 'removeLink: link not found';
517
518 } else {
519 result.removeRawLink = function () {
520 if (link) {
521 // remove fromSource
522 ldata.fromSource = null;
523 if (ldata.fromTarget) {
524 // promote target into source position
525 ldata.fromSource = ldata.fromTarget;
526 ldata.fromTarget = null;
527 ldata.key = keyrev;
528 delete network.lookup[key];
529 network.lookup[keyrev] = ldata;
Simon Hunt269670f2014-11-17 16:17:43 -0800530 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800531 }
532 } else {
533 // remove fromTarget
534 ldata.fromTarget = null;
Simon Hunt269670f2014-11-17 16:17:43 -0800535 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800536 }
537 if (ldata.fromSource) {
538 restyleLinkElement(ldata);
539 } else {
540 removeLinkElement(ldata);
541 }
542 }
543 }
544 }
545 }
546 return result;
547 }
548
549 function addLinkUpdate(ldata, link) {
550 // add link event, but we already have the reverse link installed
551 ldata.fromTarget = link;
Simon Hunt269670f2014-11-17 16:17:43 -0800552 network.revLinkToKey[link.id] = ldata.key;
Simon Hunt8257f4c2014-11-16 19:34:54 -0800553 restyleLinkElement(ldata);
554 }
555
556 var allLinkTypes = 'direct indirect optical tunnel',
557 defaultLinkType = 'direct';
558
559 function restyleLinkElement(ldata) {
560 // this fn's job is to look at raw links and decide what svg classes
561 // need to be applied to the line element in the DOM
562 var el = ldata.el,
563 type = ldata.type(),
564 lw = ldata.linkWidth(),
565 online = ldata.online();
566
567 el.classed('link', true);
568 el.classed('inactive', !online);
569 el.classed(allLinkTypes, false);
570 if (type) {
571 el.classed(type, true);
572 }
573 el.transition()
574 .duration(1000)
Thomas Vachuska89543292014-11-19 11:28:33 -0800575 .attr('stroke-width', linkScale(lw))
576 .attr('stroke', config.topo.linkBaseColor);
Simon Hunt8257f4c2014-11-16 19:34:54 -0800577 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800578
Simon Hunt99c13842014-11-06 18:23:12 -0800579 // ==============================
580 // Event handlers for server-pushed events
581
Simon Huntbb282f52014-11-10 11:08:19 -0800582 function logicError(msg) {
583 // TODO, report logic error to server, via websock, so it can be logged
Simon Huntfc274c92014-11-11 11:05:46 -0800584 console.warn(msg);
Simon Huntbb282f52014-11-10 11:08:19 -0800585 }
586
Simon Hunt99c13842014-11-06 18:23:12 -0800587 var eventDispatch = {
Simon Hunta5e89142014-11-14 07:00:33 -0800588 addInstance: addInstance,
Simon Hunt99c13842014-11-06 18:23:12 -0800589 addDevice: addDevice,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800590 addLink: addLink,
Simon Hunt56d51852014-11-09 13:03:35 -0800591 addHost: addHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800592
Simon Huntfcfb46c2014-11-19 12:53:38 -0800593 updateInstance: updateInstance,
Simon Huntbb282f52014-11-10 11:08:19 -0800594 updateDevice: updateDevice,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800595 updateLink: updateLink,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800596 updateHost: updateHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800597
Simon Hunt7b403bc2014-11-22 19:01:00 -0800598 removeInstance: removeInstance,
Simon Huntca867ac2014-11-28 18:07:35 -0800599 removeDevice: removeDevice,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800600 removeLink: removeLink,
Simon Hunt44031102014-11-11 13:20:36 -0800601 removeHost: removeHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800602
Simon Hunt61d04042014-11-11 17:27:16 -0800603 showDetails: showDetails,
Thomas Vachuska47635c62014-11-22 01:21:36 -0800604 showSummary: showSummary,
Simon Huntb53e0682014-11-12 13:32:01 -0800605 showTraffic: showTraffic
Simon Hunt99c13842014-11-06 18:23:12 -0800606 };
607
Simon Hunta5e89142014-11-14 07:00:33 -0800608 function addInstance(data) {
609 evTrace(data);
610 var inst = data.payload,
611 id = inst.id;
612 if (onosInstances[id]) {
613 logicError('ONOS instance already added: ' + id);
614 return;
615 }
616 onosInstances[id] = inst;
617 onosOrder.push(inst);
618 updateInstances();
619 }
620
Simon Hunt99c13842014-11-06 18:23:12 -0800621 function addDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800622 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800623 var device = data.payload,
Simon Huntca867ac2014-11-28 18:07:35 -0800624 id = device.id,
625 d;
626
627 if (network.lookup[id]) {
628 logicError('Device already added: ' + id);
629 return;
630 }
631
632 d = createDeviceNode(device);
633 network.nodes.push(d);
634 network.lookup[id] = d;
Simon Hunt99c13842014-11-06 18:23:12 -0800635 updateNodes();
636 network.force.start();
637 }
638
Simon Hunt99c13842014-11-06 18:23:12 -0800639 function addLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800640 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800641 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800642 result = findLink(link, 'add'),
643 bad = result.badLogic,
Simon Huntca867ac2014-11-28 18:07:35 -0800644 d = result.ldata;
Simon Hunt8257f4c2014-11-16 19:34:54 -0800645
646 if (bad) {
647 logicError(bad + ': ' + link.id);
648 return;
649 }
650
Simon Huntca867ac2014-11-28 18:07:35 -0800651 if (d) {
Simon Hunt8257f4c2014-11-16 19:34:54 -0800652 // we already have a backing store link for src/dst nodes
Simon Huntca867ac2014-11-28 18:07:35 -0800653 addLinkUpdate(d, link);
Simon Hunt8257f4c2014-11-16 19:34:54 -0800654 return;
655 }
656
657 // no backing store link yet
Simon Huntca867ac2014-11-28 18:07:35 -0800658 d = createLink(link);
659 if (d) {
660 network.links.push(d);
661 network.lookup[d.key] = d;
Simon Hunt99c13842014-11-06 18:23:12 -0800662 updateLinks();
663 network.force.start();
664 }
665 }
666
Simon Hunt56d51852014-11-09 13:03:35 -0800667 function addHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800668 evTrace(data);
Simon Hunt56d51852014-11-09 13:03:35 -0800669 var host = data.payload,
Simon Huntca867ac2014-11-28 18:07:35 -0800670 id = host.id,
671 d,
Simon Hunt56d51852014-11-09 13:03:35 -0800672 lnk;
Simon Huntca867ac2014-11-28 18:07:35 -0800673
674 if (network.lookup[id]) {
675 logicError('Host already added: ' + id);
676 return;
677 }
678
679 d = createHostNode(host);
680 network.nodes.push(d);
681 network.lookup[host.id] = d;
Simon Hunt56d51852014-11-09 13:03:35 -0800682 updateNodes();
683
684 lnk = createHostLink(host);
685 if (lnk) {
Simon Huntca867ac2014-11-28 18:07:35 -0800686 d.linkData = lnk; // cache ref on its host
Simon Hunt56d51852014-11-09 13:03:35 -0800687 network.links.push(lnk);
Simon Huntca867ac2014-11-28 18:07:35 -0800688 network.lookup[d.ingress] = lnk;
689 network.lookup[d.egress] = lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800690 updateLinks();
691 }
692 network.force.start();
693 }
694
Simon Hunt44031102014-11-11 13:20:36 -0800695 // TODO: fold updateX(...) methods into one base method; remove duplication
Simon Hunt56a2ea42014-11-19 12:39:31 -0800696
697 function updateInstance(data) {
698 evTrace(data);
699 var inst = data.payload,
700 id = inst.id,
Simon Huntca867ac2014-11-28 18:07:35 -0800701 d = onosInstances[id];
702 if (d) {
703 $.extend(d, inst);
Simon Hunt56a2ea42014-11-19 12:39:31 -0800704 updateInstances();
Simon Hunt56a2ea42014-11-19 12:39:31 -0800705 } else {
706 logicError('updateInstance lookup fail. ID = "' + id + '"');
707 }
708 }
709
Simon Huntbb282f52014-11-10 11:08:19 -0800710 function updateDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800711 evTrace(data);
Simon Huntbb282f52014-11-10 11:08:19 -0800712 var device = data.payload,
713 id = device.id,
Simon Hunt62c47542014-11-22 22:16:32 -0800714 d = network.lookup[id];
715 if (d) {
716 $.extend(d, device);
717 if (positionNode(d, true)) {
Simon Hunt395a70c2014-11-22 23:17:40 -0800718 sendUpdateMeta(d, true);
Simon Hunt62c47542014-11-22 22:16:32 -0800719 }
720 updateNodes();
Simon Huntbb282f52014-11-10 11:08:19 -0800721 } else {
722 logicError('updateDevice lookup fail. ID = "' + id + '"');
723 }
724 }
725
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800726 function updateLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800727 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800728 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800729 result = findLink(link, 'update'),
730 bad = result.badLogic;
731 if (bad) {
732 logicError(bad + ': ' + link.id);
733 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800734 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800735 result.updateWith(link);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800736 }
737
Simon Hunt7cd48f32014-11-09 23:42:50 -0800738 function updateHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800739 evTrace(data);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800740 var host = data.payload,
Simon Huntbb282f52014-11-10 11:08:19 -0800741 id = host.id,
Simon Huntca867ac2014-11-28 18:07:35 -0800742 d = network.lookup[id];
743 if (d) {
744 $.extend(d, host);
745 updateHostState(d);
Simon Huntbb282f52014-11-10 11:08:19 -0800746 } else {
747 logicError('updateHost lookup fail. ID = "' + id + '"');
748 }
Simon Hunt7cd48f32014-11-09 23:42:50 -0800749 }
750
Simon Hunt44031102014-11-11 13:20:36 -0800751 // TODO: fold removeX(...) methods into base method - remove dup code
Simon Hunt7b403bc2014-11-22 19:01:00 -0800752 function removeInstance(data) {
753 evTrace(data);
754 var inst = data.payload,
755 id = inst.id,
Simon Huntca867ac2014-11-28 18:07:35 -0800756 d = onosInstances[id];
757 if (d) {
758 var idx = find(id, onosOrder);
Simon Hunt7b403bc2014-11-22 19:01:00 -0800759 if (idx >= 0) {
760 onosOrder.splice(idx, 1);
761 }
762 delete onosInstances[id];
763 updateInstances();
764 } else {
765 logicError('updateInstance lookup fail. ID = "' + id + '"');
766 }
767 }
768
Simon Huntca867ac2014-11-28 18:07:35 -0800769 function removeDevice(data) {
770 evTrace(data);
771 var device = data.payload,
772 id = device.id,
773 d = network.lookup[id];
774 if (d) {
775 removeDeviceElement(d);
776 } else {
777 logicError('removeDevice lookup fail. ID = "' + id + '"');
778 }
779 }
780
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800781 function removeLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800782 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800783 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800784 result = findLink(link, 'remove'),
785 bad = result.badLogic;
786 if (bad) {
Simon Huntca867ac2014-11-28 18:07:35 -0800787 // may have already removed link, if attached to removed device
788 console.warn(bad + ': ' + link.id);
Simon Hunt8257f4c2014-11-16 19:34:54 -0800789 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800790 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800791 result.removeRawLink();
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800792 }
793
Simon Hunt44031102014-11-11 13:20:36 -0800794 function removeHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800795 evTrace(data);
Simon Hunt44031102014-11-11 13:20:36 -0800796 var host = data.payload,
797 id = host.id,
Simon Huntca867ac2014-11-28 18:07:35 -0800798 d = network.lookup[id];
799 if (d) {
800 removeHostElement(d, true);
Simon Hunt44031102014-11-11 13:20:36 -0800801 } else {
Simon Huntca867ac2014-11-28 18:07:35 -0800802 // may have already removed host, if attached to removed device
803 console.warn('removeHost lookup fail. ID = "' + id + '"');
Simon Hunt44031102014-11-11 13:20:36 -0800804 }
805 }
806
Simon Huntca867ac2014-11-28 18:07:35 -0800807 // the following events are server responses to user actions
Thomas Vachuska47635c62014-11-22 01:21:36 -0800808 function showSummary(data) {
809 evTrace(data);
810 populateSummary(data.payload);
Simon Hunt06811b72014-11-25 18:54:48 -0800811 showSummaryPane();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800812 }
813
Simon Hunt61d04042014-11-11 17:27:16 -0800814 function showDetails(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800815 evTrace(data);
Simon Hunt27d322d2014-11-28 10:45:43 -0800816 haveDetails = true;
Simon Hunt61d04042014-11-11 17:27:16 -0800817 populateDetails(data.payload);
Simon Hunt27d322d2014-11-28 10:45:43 -0800818 if (useDetails) {
819 showDetailPane();
820 }
Simon Hunt61d04042014-11-11 17:27:16 -0800821 }
822
Simon Huntb53e0682014-11-12 13:32:01 -0800823 function showTraffic(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800824 evTrace(data);
Thomas Vachuska4731f122014-11-20 04:56:19 -0800825 var paths = data.payload.paths,
826 hasTraffic = false;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800827
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800828 // Revert any links hilighted previously.
Thomas Vachuska4731f122014-11-20 04:56:19 -0800829 link.style('stroke-width', null)
Thomas Vachuskaa3148a72014-11-19 21:38:35 -0800830 .classed('primary secondary animated optical', false);
Simon Hunte2575b62014-11-18 15:25:53 -0800831 // Remove all previous labels.
832 removeLinkLabels();
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800833
Simon Hunte2575b62014-11-18 15:25:53 -0800834 // Now hilight all links in the paths payload, and attach
835 // labels to them, if they are defined.
Simon Hunta255a2c2014-11-13 22:29:35 -0800836 paths.forEach(function (p) {
Simon Hunte2575b62014-11-18 15:25:53 -0800837 var n = p.links.length,
838 i,
839 ldata;
840
Thomas Vachuska4731f122014-11-20 04:56:19 -0800841 hasTraffic = hasTraffic || p.traffic;
Simon Hunte2575b62014-11-18 15:25:53 -0800842 for (i=0; i<n; i++) {
843 ldata = findLinkById(p.links[i]);
Thomas Vachuska4731f122014-11-20 04:56:19 -0800844 if (ldata && ldata.el) {
Simon Hunte2575b62014-11-18 15:25:53 -0800845 ldata.el.classed(p.class, true);
846 ldata.label = p.labels[i];
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800847 }
Simon Hunte2575b62014-11-18 15:25:53 -0800848 }
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800849 });
Thomas Vachuska4731f122014-11-20 04:56:19 -0800850
Simon Hunte2575b62014-11-18 15:25:53 -0800851 updateLinks();
Thomas Vachuska4731f122014-11-20 04:56:19 -0800852
853 if (hasTraffic && !antTimer) {
854 startAntTimer();
855 } else if (!hasTraffic && antTimer) {
856 stopAntTimer();
857 }
Simon Huntb53e0682014-11-12 13:32:01 -0800858 }
859
Simon Hunt56d51852014-11-09 13:03:35 -0800860 // ...............................
861
Simon Hunt99c13842014-11-06 18:23:12 -0800862 function unknownEvent(data) {
Simon Hunt434cf142014-11-24 11:10:28 -0800863 console.warn('Unknown event type: "' + data.event + '"', data);
Simon Hunt99c13842014-11-06 18:23:12 -0800864 }
865
866 function handleServerEvent(data) {
867 var fn = eventDispatch[data.event] || unknownEvent;
868 fn(data);
869 }
870
871 // ==============================
Simon Hunt61d04042014-11-11 17:27:16 -0800872 // Out-going messages...
873
Simon Huntb53e0682014-11-12 13:32:01 -0800874 function nSel() {
875 return selectOrder.length;
876 }
Simon Hunt61d04042014-11-11 17:27:16 -0800877 function getSel(idx) {
878 return selections[selectOrder[idx]];
879 }
Simon Huntb53e0682014-11-12 13:32:01 -0800880 function allSelectionsClass(cls) {
881 for (var i=0, n=nSel(); i<n; i++) {
882 if (getSel(i).obj.class !== cls) {
883 return false;
884 }
885 }
886 return true;
887 }
Simon Hunt61d04042014-11-11 17:27:16 -0800888
Thomas Vachuska47635c62014-11-22 01:21:36 -0800889 function toggleInstances() {
890 if (!oiBox.isVisible()) {
Simon Huntb0ecfa52014-11-23 21:05:12 -0800891 showInstances();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800892 } else {
Simon Huntb0ecfa52014-11-23 21:05:12 -0800893 hideInstances();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800894 }
895 }
896
Simon Huntb0ecfa52014-11-23 21:05:12 -0800897 function showInstances() {
898 oiBox.show();
899 colorAffinity = true;
900 updateDeviceColors();
901 }
902
903 function hideInstances() {
904 oiBox.hide();
905 colorAffinity = false;
906 cancelAffinity();
907 updateDeviceColors();
908 }
909
Thomas Vachuska47635c62014-11-22 01:21:36 -0800910 function toggleSummary() {
911 if (!summaryPane.isVisible()) {
912 requestSummary();
913 } else {
914 cancelSummary();
915 }
916 }
917
918 // request overall summary data
919 function requestSummary() {
920 sendMessage('requestSummary', {});
921 }
922
923 function cancelSummary() {
924 sendMessage('cancelSummary', {});
Simon Hunt06811b72014-11-25 18:54:48 -0800925 hideSummaryPane();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800926 }
927
Simon Hunt27d322d2014-11-28 10:45:43 -0800928 function toggleDetails() {
929 useDetails = !useDetails;
930 if (useDetails) {
931 flash('Enable details pane');
932 if (haveDetails) {
933 showDetailPane();
934 }
935 } else {
936 flash('Disable details pane');
937 hideDetailPane();
938 }
939 }
940
Simon Hunt06811b72014-11-25 18:54:48 -0800941 // encapsulate interaction between summary and details panes
942 function showSummaryPane() {
943 if (detailPane.isVisible()) {
944 detailPane.down(summaryPane.show);
945 } else {
946 summaryPane.show();
947 }
948 }
949
950 function hideSummaryPane() {
951 summaryPane.hide(function () {
952 if (detailPane.isVisible()) {
953 detailPane.up();
954 }
955 });
956 }
957
958 function showDetailPane() {
959 if (summaryPane.isVisible()) {
960 detailPane.down(detailPane.show);
961 } else {
962 detailPane.up(detailPane.show);
963 }
964 }
965
966 function hideDetailPane() {
967 detailPane.hide();
968 }
969
970
Simon Hunt61d04042014-11-11 17:27:16 -0800971 // request details for the selected element
Simon Huntd72bc702014-11-13 18:38:04 -0800972 // invoked from selection of a single node.
Simon Hunt61d04042014-11-11 17:27:16 -0800973 function requestDetails() {
974 var data = getSel(0).obj,
975 payload = {
976 id: data.id,
977 class: data.class
978 };
979 sendMessage('requestDetails', payload);
980 }
981
Thomas Vachuska9edca302014-11-22 17:06:42 -0800982 function addHostIntentAction() {
Simon Huntd72bc702014-11-13 18:38:04 -0800983 sendMessage('addHostIntent', {
Thomas Vachuska9edca302014-11-22 17:06:42 -0800984 one: selectOrder[0],
985 two: selectOrder[1],
986 ids: selectOrder
Simon Huntd72bc702014-11-13 18:38:04 -0800987 });
Simon Hunt27d322d2014-11-28 10:45:43 -0800988 flash('Host-to-Host flow added');
Simon Huntd72bc702014-11-13 18:38:04 -0800989 }
990
Thomas Vachuska9edca302014-11-22 17:06:42 -0800991 function addMultiSourceIntentAction() {
992 sendMessage('addMultiSourceIntent', {
993 src: selectOrder.slice(0, selectOrder.length - 1),
994 dst: selectOrder[selectOrder.length - 1],
995 ids: selectOrder
996 });
Simon Hunt27d322d2014-11-28 10:45:43 -0800997 flash('Multi-Source flow added');
Thomas Vachuska29617e52014-11-20 03:17:46 -0800998 }
999
Thomas Vachuska9edca302014-11-22 17:06:42 -08001000
Thomas Vachuska47635c62014-11-22 01:21:36 -08001001 function cancelTraffic() {
1002 sendMessage('cancelTraffic', {});
1003 }
1004
Thomas Vachuska9edca302014-11-22 17:06:42 -08001005 function requestTrafficForMode() {
1006 if (hoverMode === hoverModeAll) {
1007 requestAllTraffic();
1008 } else if (hoverMode === hoverModeFlows) {
1009 requestDeviceLinkFlows();
1010 } else if (hoverMode === hoverModeIntents) {
1011 requestSelectTraffic();
Simon Huntd72bc702014-11-13 18:38:04 -08001012 }
Thomas Vachuska9edca302014-11-22 17:06:42 -08001013 }
Simon Huntd72bc702014-11-13 18:38:04 -08001014
Thomas Vachuska9edca302014-11-22 17:06:42 -08001015 function showTrafficAction() {
1016 hoverMode = hoverModeIntents;
1017 requestSelectTraffic();
Simon Hunt27d322d2014-11-28 10:45:43 -08001018 flash('Related Traffic');
Thomas Vachuska9edca302014-11-22 17:06:42 -08001019 }
1020
1021 function requestSelectTraffic() {
1022 if (validateSelectionContext()) {
1023 var hoverId = (hoverMode === hoverModeIntents && hovered &&
1024 (hovered.class === 'host' || hovered.class === 'device'))
Simon Huntd72bc702014-11-13 18:38:04 -08001025 ? hovered.id : '';
Thomas Vachuska9edca302014-11-22 17:06:42 -08001026 sendMessage('requestTraffic', {
1027 ids: selectOrder,
1028 hover: hoverId
1029 });
1030 }
Simon Huntd72bc702014-11-13 18:38:04 -08001031 }
1032
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -08001033
Thomas Vachuska29617e52014-11-20 03:17:46 -08001034 function showDeviceLinkFlowsAction() {
Thomas Vachuska9edca302014-11-22 17:06:42 -08001035 hoverMode = hoverModeFlows;
1036 requestDeviceLinkFlows();
Simon Hunt27d322d2014-11-28 10:45:43 -08001037 flash('Device Flows');
Thomas Vachuska29617e52014-11-20 03:17:46 -08001038 }
1039
Thomas Vachuska9edca302014-11-22 17:06:42 -08001040 function requestDeviceLinkFlows() {
1041 if (validateSelectionContext()) {
1042 var hoverId = (hoverMode === hoverModeFlows && hovered &&
1043 (hovered.class === 'device')) ? hovered.id : '';
1044 sendMessage('requestDeviceLinkFlows', {
1045 ids: selectOrder,
1046 hover: hoverId
1047 });
1048 }
1049 }
1050
1051
1052 function showAllTrafficAction() {
1053 hoverMode = hoverModeAll;
1054 requestAllTraffic();
Simon Hunt27d322d2014-11-28 10:45:43 -08001055 flash('All Traffic');
Thomas Vachuska9edca302014-11-22 17:06:42 -08001056 }
1057
1058 function requestAllTraffic() {
1059 sendMessage('requestAllTraffic', {});
1060 }
1061
1062 function validateSelectionContext() {
Thomas Vachuska29617e52014-11-20 03:17:46 -08001063 if (!hovered && nSel() === 0) {
Thomas Vachuska47635c62014-11-22 01:21:36 -08001064 cancelTraffic();
Thomas Vachuska9edca302014-11-22 17:06:42 -08001065 return false;
Thomas Vachuska29617e52014-11-20 03:17:46 -08001066 }
Thomas Vachuska9edca302014-11-22 17:06:42 -08001067 return true;
Thomas Vachuska29617e52014-11-20 03:17:46 -08001068 }
Simon Huntd72bc702014-11-13 18:38:04 -08001069
Simon Hunta6a9fe72014-11-20 11:17:12 -08001070
Simon Hunt61d04042014-11-11 17:27:16 -08001071 // ==============================
Simon Hunta5e89142014-11-14 07:00:33 -08001072 // onos instance panel functions
Simon Huntb82f6902014-11-22 11:53:15 -08001073
Simon Hunt7b403bc2014-11-22 19:01:00 -08001074 var instCfg = {
1075 rectPad: 8,
1076 nodeOx: 9,
1077 nodeOy: 9,
1078 nodeDim: 40,
1079 birdOx: 19,
1080 birdOy: 21,
1081 birdDim: 21,
1082 uiDy: 45,
1083 titleDy: 30,
1084 textYOff: 20,
1085 textYSpc: 15
1086 };
1087
1088 function viewBox(dim) {
1089 return '0 0 ' + dim.w + ' ' + dim.h;
1090 }
1091
1092 function instRectAttr(dim) {
1093 var pad = instCfg.rectPad;
1094 return {
1095 x: pad,
1096 y: pad,
1097 width: dim.w - pad*2,
1098 height: dim.h - pad*2,
Simon Huntb0ecfa52014-11-23 21:05:12 -08001099 rx: 6
Simon Hunt7b403bc2014-11-22 19:01:00 -08001100 };
1101 }
1102
1103 function computeDim(self) {
1104 var css = window.getComputedStyle(self);
1105 return {
1106 w: stripPx(css.width),
1107 h: stripPx(css.height)
1108 };
Simon Huntb82f6902014-11-22 11:53:15 -08001109 }
Simon Hunta5e89142014-11-14 07:00:33 -08001110
1111 function updateInstances() {
1112 var onoses = oiBox.el.selectAll('.onosInst')
Simon Huntb82f6902014-11-22 11:53:15 -08001113 .data(onosOrder, function (d) { return d.id; }),
Simon Hunt7b403bc2014-11-22 19:01:00 -08001114 instDim = {w:0,h:0},
1115 c = instCfg;
Simon Hunta5e89142014-11-14 07:00:33 -08001116
Simon Hunt7b403bc2014-11-22 19:01:00 -08001117 function nSw(n) {
1118 return '# Switches: ' + n;
1119 }
Simon Hunta5e89142014-11-14 07:00:33 -08001120
Simon Huntb82f6902014-11-22 11:53:15 -08001121 // operate on existing onos instances if necessary
1122 onoses.each(function (d) {
1123 var el = d3.select(this),
1124 svg = el.select('svg');
Simon Hunt7b403bc2014-11-22 19:01:00 -08001125 instDim = computeDim(this);
Simon Huntb82f6902014-11-22 11:53:15 -08001126
1127 // update online state
1128 el.classed('online', d.online);
1129
1130 // update ui-attached state
1131 svg.select('use.uiBadge').remove();
1132 if (d.uiAttached) {
1133 attachUiBadge(svg);
1134 }
1135
Simon Hunt7b403bc2014-11-22 19:01:00 -08001136 function updAttr(id, value) {
1137 svg.select('text.instLabel.'+id).text(value);
1138 }
1139
1140 updAttr('ip', d.ip);
1141 updAttr('ns', nSw(d.switches));
Simon Huntb82f6902014-11-22 11:53:15 -08001142 });
1143
1144
1145 // operate on new onos instances
Simon Hunta5e89142014-11-14 07:00:33 -08001146 var entering = onoses.enter()
1147 .append('div')
1148 .attr('class', 'onosInst')
1149 .classed('online', function (d) { return d.online; })
Simon Hunt9c15eca2014-11-15 18:37:59 -08001150 .on('click', clickInst);
1151
Simon Huntb82f6902014-11-22 11:53:15 -08001152 entering.each(function (d) {
Simon Hunt9c15eca2014-11-15 18:37:59 -08001153 var el = d3.select(this),
Simon Hunt7b403bc2014-11-22 19:01:00 -08001154 rectAttr,
1155 svg;
1156 instDim = computeDim(this);
1157 rectAttr = instRectAttr(instDim);
Simon Hunt9c15eca2014-11-15 18:37:59 -08001158
Simon Hunt7b403bc2014-11-22 19:01:00 -08001159 svg = el.append('svg').attr({
1160 width: instDim.w,
1161 height: instDim.h,
1162 viewBox: viewBox(instDim)
Simon Hunt95908012014-11-20 10:20:26 -08001163 });
Simon Huntb82f6902014-11-22 11:53:15 -08001164
Simon Hunt7b403bc2014-11-22 19:01:00 -08001165 svg.append('rect').attr(rectAttr);
Simon Hunt9c15eca2014-11-15 18:37:59 -08001166
Thomas Vachuskae02e11c2014-11-24 16:13:52 -08001167 //appendGlyph(svg, c.nodeOx, c.nodeOy, c.nodeDim, '#node');
1168 appendBadge(svg, 14, 14, 28, '#bird');
Simon Huntb82f6902014-11-22 11:53:15 -08001169
1170 if (d.uiAttached) {
1171 attachUiBadge(svg);
1172 }
1173
Simon Hunt7b403bc2014-11-22 19:01:00 -08001174 var left = c.nodeOx + c.nodeDim,
1175 len = rectAttr.width - left,
1176 hlen = len / 2,
1177 midline = hlen + left;
Simon Hunt9c15eca2014-11-15 18:37:59 -08001178
Simon Hunt7b403bc2014-11-22 19:01:00 -08001179 // title
1180 svg.append('text')
1181 .attr({
1182 class: 'instTitle',
1183 x: midline,
1184 y: c.titleDy
1185 })
1186 .text(d.id);
1187
1188 // a couple of attributes
1189 var ty = c.titleDy + c.textYOff;
1190
1191 function addAttr(id, label) {
1192 svg.append('text').attr({
1193 class: 'instLabel ' + id,
1194 x: midline,
1195 y: ty
1196 }).text(label);
1197 ty += c.textYSpc;
1198 }
1199
1200 addAttr('ip', d.ip);
1201 addAttr('ns', nSw(d.switches));
Simon Hunt9c15eca2014-11-15 18:37:59 -08001202 });
Simon Hunta5e89142014-11-14 07:00:33 -08001203
1204 // operate on existing + new onoses here
Simon Hunt8f40cce2014-11-23 15:57:30 -08001205 // set the affinity colors...
1206 onoses.each(function (d) {
1207 var el = d3.select(this),
1208 rect = el.select('svg').select('rect'),
1209 col = instColor(d.id, d.online);
1210 rect.style('fill', col);
1211 });
Simon Hunta5e89142014-11-14 07:00:33 -08001212
Simon Hunt7b403bc2014-11-22 19:01:00 -08001213 // adjust the panel size appropriately...
1214 oiBox.width(instDim.w * onosOrder.length);
1215 oiBox.height(instDim.h);
1216
1217 // remove any outgoing instances
1218 onoses.exit().remove();
Simon Hunta5e89142014-11-14 07:00:33 -08001219 }
1220
Simon Hunt8f40cce2014-11-23 15:57:30 -08001221 function instColor(id, online) {
1222 return cat7.get(id, !online, network.view.getTheme());
1223 }
1224
Simon Hunt9462e8c2014-11-14 17:28:09 -08001225 function clickInst(d) {
1226 var el = d3.select(this),
1227 aff = el.classed('affinity');
1228 if (!aff) {
1229 setAffinity(el, d);
1230 } else {
1231 cancelAffinity();
1232 }
1233 }
1234
1235 function setAffinity(el, d) {
1236 d3.selectAll('.onosInst')
1237 .classed('mastership', true)
1238 .classed('affinity', false);
1239 el.classed('affinity', true);
1240
1241 suppressLayers(true);
1242 node.each(function (n) {
1243 if (n.master === d.id) {
1244 n.el.classed('suppressed', false);
1245 }
1246 });
1247 oiShowMaster = true;
1248 }
1249
1250 function cancelAffinity() {
1251 d3.selectAll('.onosInst')
1252 .classed('mastership affinity', false);
1253 restoreLayerState();
1254 oiShowMaster = false;
1255 }
1256
Simon Hunt7b403bc2014-11-22 19:01:00 -08001257 // TODO: these should be moved out to utility module.
1258 function stripPx(s) {
1259 return s.replace(/px$/,'');
1260 }
1261
1262 function appendUse(svg, ox, oy, dim, iid, cls) {
1263 var use = svg.append('use').attr({
1264 transform: translate(ox,oy),
1265 'xlink:href': iid,
1266 width: dim,
1267 height: dim
1268 });
1269 if (cls) {
1270 use.classed(cls, true);
1271 }
1272 return use;
1273 }
1274
1275 function appendGlyph(svg, ox, oy, dim, iid, cls) {
1276 appendUse(svg, ox, oy, dim, iid, cls).classed('glyphIcon', true);
1277 }
1278
1279 function appendBadge(svg, ox, oy, dim, iid, cls) {
1280 appendUse(svg, ox, oy, dim, iid,cls ).classed('badgeIcon', true);
1281 }
1282
1283 function attachUiBadge(svg) {
1284 appendBadge(svg, 12, instCfg.uiDy, 30, '#uiAttached', 'uiBadge');
1285 }
1286
Simon Hunt434cf142014-11-24 11:10:28 -08001287 function visVal(b) {
1288 return b ? 'visible' : 'hidden';
1289 }
1290
Simon Hunta5e89142014-11-14 07:00:33 -08001291 // ==============================
Simon Hunt99c13842014-11-06 18:23:12 -08001292 // force layout modification functions
1293
1294 function translate(x, y) {
1295 return 'translate(' + x + ',' + y + ')';
1296 }
1297
Simon Hunte2575b62014-11-18 15:25:53 -08001298 function rotate(deg) {
1299 return 'rotate(' + deg + ')';
1300 }
1301
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001302 function missMsg(what, id) {
1303 return '\n[' + what + '] "' + id + '" missing ';
1304 }
1305
1306 function linkEndPoints(srcId, dstId) {
1307 var srcNode = network.lookup[srcId],
1308 dstNode = network.lookup[dstId],
1309 sMiss = !srcNode ? missMsg('src', srcId) : '',
1310 dMiss = !dstNode ? missMsg('dst', dstId) : '';
1311
1312 if (sMiss || dMiss) {
1313 logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
1314 return null;
1315 }
1316 return {
1317 source: srcNode,
1318 target: dstNode,
1319 x1: srcNode.x,
1320 y1: srcNode.y,
1321 x2: dstNode.x,
1322 y2: dstNode.y
1323 };
1324 }
1325
Simon Hunt56d51852014-11-09 13:03:35 -08001326 function createHostLink(host) {
1327 var src = host.id,
1328 dst = host.cp.device,
Simon Hunt7cd48f32014-11-09 23:42:50 -08001329 id = host.ingress,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001330 lnk = linkEndPoints(src, dst);
Simon Hunt56d51852014-11-09 13:03:35 -08001331
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001332 if (!lnk) {
Simon Hunt56d51852014-11-09 13:03:35 -08001333 return null;
1334 }
1335
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001336 // Synthesize link ...
1337 $.extend(lnk, {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001338 key: id,
Simon Hunt56d51852014-11-09 13:03:35 -08001339 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -08001340
1341 type: function () { return 'hostLink'; },
1342 // TODO: ideally, we should see if our edge switch is online...
1343 online: function () { return true; },
1344 linkWidth: function () { return 1; }
Simon Hunt7cd48f32014-11-09 23:42:50 -08001345 });
Simon Hunt99c13842014-11-06 18:23:12 -08001346 return lnk;
1347 }
1348
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001349 function createLink(link) {
Simon Hunta6a9fe72014-11-20 11:17:12 -08001350 var lnk = linkEndPoints(link.src, link.dst);
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001351
1352 if (!lnk) {
1353 return null;
1354 }
1355
Simon Hunt8257f4c2014-11-16 19:34:54 -08001356 $.extend(lnk, {
1357 key: link.id,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001358 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -08001359 fromSource: link,
1360
1361 // functions to aggregate dual link state
1362 type: function () {
1363 var s = lnk.fromSource,
1364 t = lnk.fromTarget;
1365 return (s && s.type) || (t && t.type) || defaultLinkType;
1366 },
1367 online: function () {
1368 var s = lnk.fromSource,
1369 t = lnk.fromTarget;
1370 return (s && s.online) || (t && t.online);
1371 },
1372 linkWidth: function () {
1373 var s = lnk.fromSource,
1374 t = lnk.fromTarget,
1375 ws = (s && s.linkWidth) || 0,
1376 wt = (t && t.linkWidth) || 0;
1377 return Math.max(ws, wt);
1378 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001379 });
1380 return lnk;
Simon Hunt1a9eff92014-11-07 11:06:34 -08001381 }
1382
Simon Hunte2575b62014-11-18 15:25:53 -08001383 function removeLinkLabels() {
1384 network.links.forEach(function (d) {
1385 d.label = '';
1386 });
1387 }
1388
Simon Hunt434cf142014-11-24 11:10:28 -08001389 function showHostVis(el) {
1390 el.style('visibility', visVal(showHosts));
1391 }
1392
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001393 var widthRatio = 1.4,
1394 linkScale = d3.scale.linear()
1395 .domain([1, 12])
1396 .range([widthRatio, 12 * widthRatio])
1397 .clamp(true);
1398
Simon Hunt99c13842014-11-06 18:23:12 -08001399 function updateLinks() {
1400 link = linkG.selectAll('.link')
Simon Hunt8257f4c2014-11-16 19:34:54 -08001401 .data(network.links, function (d) { return d.key; });
Simon Hunt99c13842014-11-06 18:23:12 -08001402
1403 // operate on existing links, if necessary
1404 // link .foo() .bar() ...
1405
1406 // operate on entering links:
1407 var entering = link.enter()
1408 .append('line')
1409 .attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001410 x1: function (d) { return d.x1; },
1411 y1: function (d) { return d.y1; },
1412 x2: function (d) { return d.x2; },
1413 y2: function (d) { return d.y2; },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001414 stroke: config.topo.linkInColor,
1415 'stroke-width': config.topo.linkInWidth
Simon Hunt99c13842014-11-06 18:23:12 -08001416 });
1417
1418 // augment links
Simon Hunt7cd48f32014-11-09 23:42:50 -08001419 entering.each(function (d) {
1420 var link = d3.select(this);
1421 // provide ref to element selection from backing data....
1422 d.el = link;
Simon Hunt8257f4c2014-11-16 19:34:54 -08001423 restyleLinkElement(d);
Simon Hunt434cf142014-11-24 11:10:28 -08001424 if (d.type() === 'hostLink') {
1425 showHostVis(link);
1426 }
Simon Hunt7cd48f32014-11-09 23:42:50 -08001427 });
Thomas Vachuska4830d392014-11-09 17:09:56 -08001428
1429 // operate on both existing and new links, if necessary
1430 //link .foo() .bar() ...
1431
Simon Hunte2575b62014-11-18 15:25:53 -08001432 // apply or remove labels
1433 var labelData = getLabelData();
1434 applyLinkLabels(labelData);
1435
Thomas Vachuska4830d392014-11-09 17:09:56 -08001436 // operate on exiting links:
Thomas Vachuska4830d392014-11-09 17:09:56 -08001437 link.exit()
Simon Hunt13bf9c82014-11-18 07:26:44 -08001438 .attr('stroke-dasharray', '3, 3')
1439 .style('opacity', 0.5)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001440 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -08001441 .duration(1500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001442 .attr({
Simon Hunt13bf9c82014-11-18 07:26:44 -08001443 'stroke-dasharray': '3, 12',
1444 stroke: config.topo.linkOutColor,
1445 'stroke-width': config.topo.linkOutWidth
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001446 })
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001447 .style('opacity', 0.0)
Thomas Vachuska4830d392014-11-09 17:09:56 -08001448 .remove();
Simon Hunte2575b62014-11-18 15:25:53 -08001449
1450 // NOTE: invoke a single tick to force the labels to position
1451 // onto their links.
1452 tick();
1453 }
1454
1455 function getLabelData() {
1456 // create the backing data for showing labels..
1457 var data = [];
1458 link.each(function (d) {
1459 if (d.label) {
1460 data.push({
1461 id: 'lab-' + d.key,
1462 key: d.key,
1463 label: d.label,
1464 ldata: d
1465 });
1466 }
1467 });
1468 return data;
1469 }
1470
1471 var linkLabelOffset = '0.3em';
1472
1473 function applyLinkLabels(data) {
1474 var entering;
1475
1476 linkLabel = linkLabelG.selectAll('.linkLabel')
1477 .data(data, function (d) { return d.id; });
1478
Simon Hunt56a2ea42014-11-19 12:39:31 -08001479 // for elements already existing, we need to update the text
1480 // and adjust the rectangle size to fit
1481 linkLabel.each(function (d) {
1482 var el = d3.select(this),
1483 rect = el.select('rect'),
1484 text = el.select('text');
1485 text.text(d.label);
1486 rect.attr(rectAroundText(el));
1487 });
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -08001488
Simon Hunte2575b62014-11-18 15:25:53 -08001489 entering = linkLabel.enter().append('g')
1490 .classed('linkLabel', true)
1491 .attr('id', function (d) { return d.id; });
1492
1493 entering.each(function (d) {
1494 var el = d3.select(this),
1495 rect,
1496 text,
1497 parms = {
1498 x1: d.ldata.x1,
1499 y1: d.ldata.y1,
1500 x2: d.ldata.x2,
1501 y2: d.ldata.y2
1502 };
1503
1504 d.el = el;
1505 rect = el.append('rect');
1506 text = el.append('text').text(d.label);
1507 rect.attr(rectAroundText(el));
1508 text.attr('dy', linkLabelOffset);
1509
1510 el.attr('transform', transformLabel(parms));
1511 });
1512
1513 // Remove any links that are no longer required.
1514 linkLabel.exit().remove();
1515 }
1516
1517 function rectAroundText(el) {
1518 var text = el.select('text'),
1519 box = text.node().getBBox();
1520
1521 // translate the bbox so that it is centered on [x,y]
1522 box.x = -box.width / 2;
1523 box.y = -box.height / 2;
1524
1525 // add padding
1526 box.x -= 1;
1527 box.width += 2;
1528 return box;
1529 }
1530
1531 function transformLabel(p) {
1532 var dx = p.x2 - p.x1,
1533 dy = p.y2 - p.y1,
1534 xMid = dx/2 + p.x1,
1535 yMid = dy/2 + p.y1;
Simon Hunte2575b62014-11-18 15:25:53 -08001536 return translate(xMid, yMid);
Simon Hunt99c13842014-11-06 18:23:12 -08001537 }
1538
1539 function createDeviceNode(device) {
1540 // start with the object as is
1541 var node = device,
Simon Huntbb282f52014-11-10 11:08:19 -08001542 type = device.type,
Simon Huntc72967b2014-11-20 09:21:42 -08001543 svgCls = type ? 'node device ' + type : 'node device',
1544 labels = device.labels || [];
1545
Simon Hunt99c13842014-11-06 18:23:12 -08001546 // Augment as needed...
1547 node.class = 'device';
Simon Huntbb282f52014-11-10 11:08:19 -08001548 node.svgClass = device.online ? svgCls + ' online' : svgCls;
Simon Hunt99c13842014-11-06 18:23:12 -08001549 positionNode(node);
Simon Hunt99c13842014-11-06 18:23:12 -08001550 return node;
1551 }
1552
Simon Hunt56d51852014-11-09 13:03:35 -08001553 function createHostNode(host) {
1554 // start with the object as is
1555 var node = host;
1556
1557 // Augment as needed...
1558 node.class = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001559 if (!node.type) {
Simon Hunt209155e2014-11-21 12:16:09 -08001560 node.type = 'endstation';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001561 }
Simon Hunt7fa116d2014-11-17 14:16:55 -08001562 node.svgClass = 'node host ' + node.type;
Simon Hunt56d51852014-11-09 13:03:35 -08001563 positionNode(node);
Simon Hunt56d51852014-11-09 13:03:35 -08001564 return node;
1565 }
1566
Simon Hunt62c47542014-11-22 22:16:32 -08001567 function positionNode(node, forUpdate) {
Simon Hunt99c13842014-11-06 18:23:12 -08001568 var meta = node.metaUi,
Simon Huntac9e24f2014-11-12 10:12:21 -08001569 x = meta && meta.x,
1570 y = meta && meta.y,
1571 xy;
Simon Hunt99c13842014-11-06 18:23:12 -08001572
Simon Huntac9e24f2014-11-12 10:12:21 -08001573 // If we have [x,y] already, use that...
Simon Hunt99c13842014-11-06 18:23:12 -08001574 if (x && y) {
1575 node.fixed = true;
Simon Hunt62c47542014-11-22 22:16:32 -08001576 node.px = node.x = x;
1577 node.py = node.y = y;
1578 //node.px = x;
1579 //node.py = y;
Simon Huntac9e24f2014-11-12 10:12:21 -08001580 return;
Simon Hunt99c13842014-11-06 18:23:12 -08001581 }
Simon Huntac9e24f2014-11-12 10:12:21 -08001582
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001583 var location = node.location;
1584 if (location && location.type === 'latlng') {
1585 var coord = geoMapProjection([location.lng, location.lat]);
1586 node.fixed = true;
Simon Hunt62c47542014-11-22 22:16:32 -08001587 node.px = node.x = coord[0];
1588 node.py = node.y = coord[1];
1589 //node.x = coord[0];
1590 //node.y = coord[1];
1591 return true;
1592 }
1593
1594 // if this is a node update (not a node add).. skip randomizer
1595 if (forUpdate) {
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001596 return;
1597 }
1598
Simon Huntac9e24f2014-11-12 10:12:21 -08001599 // Note: Placing incoming unpinned nodes at exactly the same point
1600 // (center of the view) causes them to explode outwards when
1601 // the force layout kicks in. So, we spread them out a bit
1602 // initially, to provide a more serene layout convergence.
1603 // Additionally, if the node is a host, we place it near
1604 // the device it is connected to.
1605
1606 function spread(s) {
1607 return Math.floor((Math.random() * s) - s/2);
1608 }
1609
1610 function randDim(dim) {
1611 return dim / 2 + spread(dim * 0.7071);
1612 }
1613
1614 function rand() {
1615 return {
1616 x: randDim(network.view.width()),
1617 y: randDim(network.view.height())
1618 };
1619 }
1620
1621 function near(node) {
1622 var min = 12,
1623 dx = spread(12),
1624 dy = spread(12);
1625 return {
1626 x: node.x + min + dx,
1627 y: node.y + min + dy
1628 };
1629 }
1630
1631 function getDevice(cp) {
1632 var d = network.lookup[cp.device];
1633 return d || rand();
1634 }
1635
1636 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
1637 $.extend(node, xy);
Simon Hunt99c13842014-11-06 18:23:12 -08001638 }
1639
Simon Hunt99c13842014-11-06 18:23:12 -08001640
Simon Huntc72967b2014-11-20 09:21:42 -08001641 function iconGlyphUrl(d) {
1642 var which = d.type || 'unknown';
1643 return '#' + which;
1644 }
1645
Simon Hunt99c13842014-11-06 18:23:12 -08001646 // returns the newly computed bounding box of the rectangle
1647 function adjustRectToFitText(n) {
1648 var text = n.select('text'),
1649 box = text.node().getBBox(),
1650 lab = config.labels;
1651
1652 text.attr('text-anchor', 'middle')
1653 .attr('y', '-0.8em')
1654 .attr('x', lab.imgPad/2);
1655
1656 // translate the bbox so that it is centered on [x,y]
1657 box.x = -box.width / 2;
1658 box.y = -box.height / 2;
1659
1660 // add padding
1661 box.x -= (lab.padLR + lab.imgPad/2);
1662 box.width += lab.padLR * 2 + lab.imgPad;
1663 box.y -= lab.padTB;
1664 box.height += lab.padTB * 2;
1665
1666 return box;
1667 }
1668
Simon Hunt1a9eff92014-11-07 11:06:34 -08001669 function mkSvgClass(d) {
1670 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
1671 }
1672
Simon Hunt7cd48f32014-11-09 23:42:50 -08001673 function hostLabel(d) {
1674 var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0;
1675 return d.labels[idx];
1676 }
1677 function deviceLabel(d) {
1678 var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0;
1679 return d.labels[idx];
1680 }
Simon Huntc72967b2014-11-20 09:21:42 -08001681 function trimLabel(label) {
1682 return (label && label.trim()) || '';
1683 }
1684
1685 function emptyBox() {
1686 return {
1687 x: -2,
1688 y: -2,
1689 width: 4,
1690 height: 4
1691 };
Simon Hunt7cd48f32014-11-09 23:42:50 -08001692 }
1693
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001694 function updateDeviceLabel(d) {
Simon Huntc72967b2014-11-20 09:21:42 -08001695 var label = trimLabel(deviceLabel(d)),
1696 noLabel = !label,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001697 node = d.el,
Simon Huntc72967b2014-11-20 09:21:42 -08001698 box,
1699 dx,
Simon Hunt395a70c2014-11-22 23:17:40 -08001700 dy,
1701 cfg = config.icons.device;
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001702
1703 node.select('text')
1704 .text(label)
1705 .style('opacity', 0)
1706 .transition()
1707 .style('opacity', 1);
1708
Simon Huntc72967b2014-11-20 09:21:42 -08001709 if (noLabel) {
1710 box = emptyBox();
Simon Hunt395a70c2014-11-22 23:17:40 -08001711 dx = -cfg.dim/2;
1712 dy = -cfg.dim/2;
Simon Huntc72967b2014-11-20 09:21:42 -08001713 } else {
1714 box = adjustRectToFitText(node);
Simon Hunt395a70c2014-11-22 23:17:40 -08001715 dx = box.x + cfg.xoff;
1716 dy = box.y + cfg.yoff;
Simon Huntc72967b2014-11-20 09:21:42 -08001717 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001718
1719 node.select('rect')
1720 .transition()
1721 .attr(box);
1722
Simon Huntc72967b2014-11-20 09:21:42 -08001723 node.select('g.deviceIcon')
Thomas Vachuska89543292014-11-19 11:28:33 -08001724 .transition()
Simon Huntc72967b2014-11-20 09:21:42 -08001725 .attr('transform', translate(dx, dy));
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001726 }
1727
1728 function updateHostLabel(d) {
1729 var label = hostLabel(d),
1730 host = d.el;
1731
1732 host.select('text').text(label);
1733 }
1734
Simon Hunt62c47542014-11-22 22:16:32 -08001735 // FIXME : fold this into updateNodes.
Simon Huntbb282f52014-11-10 11:08:19 -08001736 function updateHostState(hostData) {
1737 updateHostLabel(hostData);
Simon Huntbb282f52014-11-10 11:08:19 -08001738 }
1739
Simon Hunt434cf142014-11-24 11:10:28 -08001740 function updateHostVisibility() {
1741 var v = visVal(showHosts);
1742 nodeG.selectAll('.host').style('visibility', v);
1743 linkG.selectAll('.hostLink').style('visibility', v);
1744 }
1745
Simon Hunt6ac93f32014-11-13 12:17:27 -08001746 function nodeMouseOver(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001747 hovered = d;
Thomas Vachuska9edca302014-11-22 17:06:42 -08001748 requestTrafficForMode();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001749 }
1750
1751 function nodeMouseOut(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001752 hovered = null;
Thomas Vachuska9edca302014-11-22 17:06:42 -08001753 requestTrafficForMode();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001754 }
Simon Huntbb282f52014-11-10 11:08:19 -08001755
Simon Hunteb1514d2014-11-20 09:57:29 -08001756 function addHostIcon(node, radius, iid) {
Thomas Vachuska89543292014-11-19 11:28:33 -08001757 var dim = radius * 1.5,
1758 xlate = -dim / 2;
1759
Simon Hunteb1514d2014-11-20 09:57:29 -08001760 node.append('use').attr({
1761 class: 'glyphIcon hostIcon',
1762 transform: translate(xlate,xlate),
1763 'xlink:href': iid,
1764 width: dim,
1765 height: dim
1766 });
Thomas Vachuska89543292014-11-19 11:28:33 -08001767 }
1768
Simon Hunt99c13842014-11-06 18:23:12 -08001769 function updateNodes() {
1770 node = nodeG.selectAll('.node')
1771 .data(network.nodes, function (d) { return d.id; });
1772
Simon Hunt62c47542014-11-22 22:16:32 -08001773 // operate on existing nodes...
1774 node.filter('.device').each(function (d) {
Simon Hunt62c47542014-11-22 22:16:32 -08001775 var node = d.el;
1776 node.classed('online', d.online);
1777 updateDeviceLabel(d);
1778 positionNode(d, true);
1779 });
1780
1781 node.filter('.host').each(function (d) {
Simon Hunt62c47542014-11-22 22:16:32 -08001782 var node = d.el;
1783 // TODO: appropriate update of host visuals
1784 });
Simon Hunt99c13842014-11-06 18:23:12 -08001785
1786 // operate on entering nodes:
1787 var entering = node.enter()
1788 .append('g')
1789 .attr({
1790 id: function (d) { return safeId(d.id); },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001791 class: mkSvgClass,
Simon Hunt99c13842014-11-06 18:23:12 -08001792 transform: function (d) { return translate(d.x, d.y); },
1793 opacity: 0
1794 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08001795 .call(network.drag)
Simon Hunt6ac93f32014-11-13 12:17:27 -08001796 .on('mouseover', nodeMouseOver)
1797 .on('mouseout', nodeMouseOut)
Simon Hunt99c13842014-11-06 18:23:12 -08001798 .transition()
1799 .attr('opacity', 1);
1800
1801 // augment device nodes...
1802 entering.filter('.device').each(function (d) {
1803 var node = d3.select(this),
Simon Huntc72967b2014-11-20 09:21:42 -08001804 label = trimLabel(deviceLabel(d)),
1805 noLabel = !label,
Simon Hunt99c13842014-11-06 18:23:12 -08001806 box;
1807
Simon Hunt7cd48f32014-11-09 23:42:50 -08001808 // provide ref to element from backing data....
1809 d.el = node;
1810
Simon Hunt62c47542014-11-22 22:16:32 -08001811 node.append('rect').attr({ rx: 5, ry: 5 });
1812 node.append('text').text(label).attr('dy', '1.1em');
Simon Hunt99c13842014-11-06 18:23:12 -08001813 box = adjustRectToFitText(node);
Simon Hunta3dd9572014-11-20 15:22:41 -08001814 node.select('rect').attr(box);
Simon Huntc72967b2014-11-20 09:21:42 -08001815 addDeviceIcon(node, box, noLabel, iconGlyphUrl(d));
Simon Huntc7ee0662014-11-05 16:44:37 -08001816 });
Simon Hunt934c3ce2014-11-05 11:45:07 -08001817
Thomas Vachuska89543292014-11-19 11:28:33 -08001818
Simon Hunt56d51852014-11-09 13:03:35 -08001819 // augment host nodes...
1820 entering.filter('.host').each(function (d) {
1821 var node = d3.select(this),
Simon Hunt395a70c2014-11-22 23:17:40 -08001822 cfg = config.icons.host,
1823 r = cfg.radius[d.type] || cfg.defaultRadius,
Thomas Vachuska89543292014-11-19 11:28:33 -08001824 textDy = r + 10,
Simon Hunteb1514d2014-11-20 09:57:29 -08001825 iid = iconGlyphUrl(d);
Simon Hunt56d51852014-11-09 13:03:35 -08001826
Simon Hunt7cd48f32014-11-09 23:42:50 -08001827 // provide ref to element from backing data....
1828 d.el = node;
Simon Hunt434cf142014-11-24 11:10:28 -08001829 showHostVis(node);
Simon Hunt7cd48f32014-11-09 23:42:50 -08001830
Simon Hunt62c47542014-11-22 22:16:32 -08001831 node.append('circle').attr('r', r);
Simon Hunteb1514d2014-11-20 09:57:29 -08001832 if (iid) {
1833 addHostIcon(node, r, iid);
Simon Hunt7fa116d2014-11-17 14:16:55 -08001834 }
Simon Hunt56d51852014-11-09 13:03:35 -08001835 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001836 .text(hostLabel)
Thomas Vachuska89543292014-11-19 11:28:33 -08001837 .attr('dy', textDy)
Simon Hunt7cd48f32014-11-09 23:42:50 -08001838 .attr('text-anchor', 'middle');
Simon Hunt56d51852014-11-09 13:03:35 -08001839 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001840
Simon Hunt99c13842014-11-06 18:23:12 -08001841 // operate on both existing and new nodes, if necessary
Simon Huntb0ecfa52014-11-23 21:05:12 -08001842 updateDeviceColors();
Simon Huntc7ee0662014-11-05 16:44:37 -08001843
Simon Hunt99c13842014-11-06 18:23:12 -08001844 // operate on exiting nodes:
Simon Huntea80eb42014-11-11 13:46:57 -08001845 // Note that the node is removed after 2 seconds.
1846 // Sub element animations should be shorter than 2 seconds.
1847 var exiting = node.exit()
Simon Hunt44031102014-11-11 13:20:36 -08001848 .transition()
1849 .duration(2000)
Simon Huntea80eb42014-11-11 13:46:57 -08001850 .style('opacity', 0)
Simon Hunt99c13842014-11-06 18:23:12 -08001851 .remove();
Simon Huntea80eb42014-11-11 13:46:57 -08001852
1853 // host node exits....
1854 exiting.filter('.host').each(function (d) {
Simon Huntca867ac2014-11-28 18:07:35 -08001855 var node = d.el;
1856 node.select('use')
1857 .style('opacity', 0.5)
1858 .transition()
1859 .duration(800)
1860 .style('opacity', 0);
Simon Huntea80eb42014-11-11 13:46:57 -08001861
1862 node.select('text')
1863 .style('opacity', 0.5)
1864 .transition()
Simon Huntca867ac2014-11-28 18:07:35 -08001865 .duration(800)
Simon Huntea80eb42014-11-11 13:46:57 -08001866 .style('opacity', 0);
Simon Huntea80eb42014-11-11 13:46:57 -08001867
Thomas Vachuska89543292014-11-19 11:28:33 -08001868 node.select('circle')
1869 .style('stroke-fill', '#555')
1870 .style('fill', '#888')
Simon Huntea80eb42014-11-11 13:46:57 -08001871 .style('opacity', 0.5)
1872 .transition()
1873 .duration(1500)
1874 .attr('r', 0);
Simon Huntea80eb42014-11-11 13:46:57 -08001875 });
1876
Simon Huntca867ac2014-11-28 18:07:35 -08001877 // device node exits....
1878 exiting.filter('.device').each(function (d) {
1879 var node = d.el;
1880 node.select('use')
1881 .style('opacity', 0.5)
1882 .transition()
1883 .duration(800)
1884 .style('opacity', 0);
1885
1886 node.selectAll('rect')
1887 .style('stroke-fill', '#555')
1888 .style('fill', '#888')
1889 .style('opacity', 0.5);
1890 });
Simon Hunt62c47542014-11-22 22:16:32 -08001891
1892 network.force.resume();
Simon Huntc7ee0662014-11-05 16:44:37 -08001893 }
1894
Simon Hunt95dad922014-11-24 09:43:31 -08001895 var dCol = {
1896 black: '#000',
1897 paleblue: '#acf',
1898 offwhite: '#ddd',
1899 midgrey: '#888',
1900 lightgrey: '#bbb',
1901 orange: '#f90'
1902 };
1903
Simon Huntb0ecfa52014-11-23 21:05:12 -08001904 // note: these are the device icon colors without affinity
Simon Hunt95dad922014-11-24 09:43:31 -08001905 var dColTheme = {
Simon Huntb0ecfa52014-11-23 21:05:12 -08001906 light: {
1907 online: {
Simon Hunt95dad922014-11-24 09:43:31 -08001908 glyph: dCol.black,
1909 rect: dCol.paleblue
Simon Huntb0ecfa52014-11-23 21:05:12 -08001910 },
1911 offline: {
Simon Hunt95dad922014-11-24 09:43:31 -08001912 glyph: dCol.midgrey,
1913 rect: dCol.lightgrey
Simon Huntb0ecfa52014-11-23 21:05:12 -08001914 }
1915 },
Simon Hunt95dad922014-11-24 09:43:31 -08001916 // TODO: theme
Simon Huntb0ecfa52014-11-23 21:05:12 -08001917 dark: {
1918 online: {
Simon Hunt95dad922014-11-24 09:43:31 -08001919 glyph: dCol.black,
1920 rect: dCol.paleblue
Simon Huntb0ecfa52014-11-23 21:05:12 -08001921 },
1922 offline: {
Simon Hunt95dad922014-11-24 09:43:31 -08001923 glyph: dCol.midgrey,
1924 rect: dCol.lightgrey
Simon Huntb0ecfa52014-11-23 21:05:12 -08001925 }
1926 }
1927 };
1928
1929 function devBaseColor(d) {
1930 var t = network.view.getTheme(),
1931 o = d.online ? 'online' : 'offline';
Simon Hunt95dad922014-11-24 09:43:31 -08001932 return dColTheme[t][o];
Simon Huntb0ecfa52014-11-23 21:05:12 -08001933 }
1934
1935 function setDeviceColor(d) {
1936 var o = d.online,
1937 s = d.el.classed('selected'),
1938 c = devBaseColor(d),
1939 a = instColor(d.master, o),
1940 g, r,
1941 icon = d.el.select('g.deviceIcon');
1942
1943 if (s) {
1944 g = c.glyph;
Simon Hunt95dad922014-11-24 09:43:31 -08001945 r = dColTheme.sel;
Simon Huntb0ecfa52014-11-23 21:05:12 -08001946 } else if (colorAffinity) {
1947 g = o ? a : c.glyph;
Simon Hunt95dad922014-11-24 09:43:31 -08001948 r = o ? dCol.offwhite : a;
Simon Huntb0ecfa52014-11-23 21:05:12 -08001949 } else {
1950 g = c.glyph;
1951 r = c.rect;
1952 }
1953
1954 icon.select('use')
1955 .style('fill', g);
1956 icon.select('rect')
1957 .style('fill', r);
1958 }
1959
Simon Huntc72967b2014-11-20 09:21:42 -08001960 function addDeviceIcon(node, box, noLabel, iid) {
1961 var cfg = config.icons.device,
1962 dx,
1963 dy,
1964 g;
1965
1966 if (noLabel) {
1967 box = emptyBox();
1968 dx = -cfg.dim/2;
1969 dy = -cfg.dim/2;
1970 } else {
1971 box = adjustRectToFitText(node);
Simon Hunt395a70c2014-11-22 23:17:40 -08001972 dx = box.x + cfg.xoff;
1973 dy = box.y + cfg.yoff;
Simon Huntc72967b2014-11-20 09:21:42 -08001974 }
1975
Simon Hunteb1514d2014-11-20 09:57:29 -08001976 g = node.append('g')
1977 .attr('class', 'glyphIcon deviceIcon')
Simon Huntc72967b2014-11-20 09:21:42 -08001978 .attr('transform', translate(dx, dy));
1979
1980 g.append('rect').attr({
1981 x: 0,
1982 y: 0,
1983 rx: cfg.rx,
1984 width: cfg.dim,
1985 height: cfg.dim
1986 });
1987
1988 g.append('use').attr({
1989 'xlink:href': iid,
1990 width: cfg.dim,
1991 height: cfg.dim
1992 });
1993
Simon Huntc72967b2014-11-20 09:21:42 -08001994 }
1995
Simon Hunt7b403bc2014-11-22 19:01:00 -08001996 function find(key, array, tag) {
Simon Huntca867ac2014-11-28 18:07:35 -08001997 var _tag = tag || 'id',
Simon Hunt7b403bc2014-11-22 19:01:00 -08001998 idx, n, d;
1999 for (idx = 0, n = array.length; idx < n; idx++) {
2000 d = array[idx];
2001 if (d[_tag] === key) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08002002 return idx;
2003 }
2004 }
2005 return -1;
2006 }
2007
Simon Huntca867ac2014-11-28 18:07:35 -08002008 function removeLinkElement(d) {
2009 var idx = find(d.key, network.links, 'key'),
Simon Hunt8257f4c2014-11-16 19:34:54 -08002010 removed;
2011 if (idx >=0) {
2012 // remove from links array
2013 removed = network.links.splice(idx, 1);
2014 // remove from lookup cache
2015 delete network.lookup[removed[0].key];
2016 updateLinks();
2017 network.force.resume();
2018 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08002019 }
Simon Huntc7ee0662014-11-05 16:44:37 -08002020
Simon Huntca867ac2014-11-28 18:07:35 -08002021 function removeHostElement(d, upd) {
2022 var lu = network.lookup;
Simon Hunt44031102014-11-11 13:20:36 -08002023 // first, remove associated hostLink...
Simon Huntca867ac2014-11-28 18:07:35 -08002024 removeLinkElement(d.linkData);
2025
2026 // remove hostLink bindings
2027 delete lu[d.ingress];
2028 delete lu[d.egress];
Simon Hunt44031102014-11-11 13:20:36 -08002029
2030 // remove from lookup cache
Simon Huntca867ac2014-11-28 18:07:35 -08002031 delete lu[d.id];
Simon Hunt44031102014-11-11 13:20:36 -08002032 // remove from nodes array
Simon Huntca867ac2014-11-28 18:07:35 -08002033 var idx = find(d.id, network.nodes);
2034 network.nodes.splice(idx, 1);
2035 // remove from SVG
2036 // NOTE: upd is false if we were called from removeDeviceElement()
2037 if (upd) {
2038 updateNodes();
2039 network.force.resume();
2040 }
2041 }
2042
2043
2044 function removeDeviceElement(d) {
2045 var id = d.id;
2046 // first, remove associated hosts and links..
2047 findAttachedHosts(id).forEach(removeHostElement);
2048 findAttachedLinks(id).forEach(removeLinkElement);
2049
2050 // remove from lookup cache
2051 delete network.lookup[id];
2052 // remove from nodes array
2053 var idx = find(id, network.nodes);
Simon Hunt44031102014-11-11 13:20:36 -08002054 network.nodes.splice(idx, 1);
2055 // remove from SVG
2056 updateNodes();
2057 network.force.resume();
2058 }
2059
Simon Huntca867ac2014-11-28 18:07:35 -08002060 function findAttachedHosts(devId) {
2061 var hosts = [];
2062 network.nodes.forEach(function (d) {
2063 if (d.class === 'host' && d.cp.device === devId) {
2064 hosts.push(d);
2065 }
2066 });
2067 return hosts;
2068 }
2069
2070 function findAttachedLinks(devId) {
2071 var links = [];
2072 network.links.forEach(function (d) {
2073 if (d.source.id === devId || d.target.id === devId) {
2074 links.push(d);
2075 }
2076 });
2077 return links;
2078 }
Simon Hunt44031102014-11-11 13:20:36 -08002079
Simon Huntc7ee0662014-11-05 16:44:37 -08002080 function tick() {
2081 node.attr({
Simon Hunt99c13842014-11-06 18:23:12 -08002082 transform: function (d) { return translate(d.x, d.y); }
Simon Huntc7ee0662014-11-05 16:44:37 -08002083 });
2084
2085 link.attr({
2086 x1: function (d) { return d.source.x; },
2087 y1: function (d) { return d.source.y; },
2088 x2: function (d) { return d.target.x; },
2089 y2: function (d) { return d.target.y; }
2090 });
Simon Hunte2575b62014-11-18 15:25:53 -08002091
2092 linkLabel.each(function (d) {
2093 var el = d3.select(this);
Thomas Vachuska4731f122014-11-20 04:56:19 -08002094 var lnk = findLinkById(d.key);
2095
2096 if (lnk) {
2097 var parms = {
Simon Hunte2575b62014-11-18 15:25:53 -08002098 x1: lnk.source.x,
2099 y1: lnk.source.y,
2100 x2: lnk.target.x,
2101 y2: lnk.target.y
2102 };
Thomas Vachuska4731f122014-11-20 04:56:19 -08002103 el.attr('transform', transformLabel(parms));
2104 }
Simon Hunte2575b62014-11-18 15:25:53 -08002105 });
Simon Huntc7ee0662014-11-05 16:44:37 -08002106 }
Simon Hunt934c3ce2014-11-05 11:45:07 -08002107
2108 // ==============================
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002109 // Web-Socket for live data
2110
2111 function webSockUrl() {
2112 return document.location.toString()
2113 .replace(/\#.*/, '')
2114 .replace('http://', 'ws://')
2115 .replace('https://', 'wss://')
Simon Hunte5ab1382014-11-25 10:28:51 -08002116 .replace('index.html', config.webSockUrl);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002117 }
2118
2119 webSock = {
2120 ws : null,
2121
2122 connect : function() {
2123 webSock.ws = new WebSocket(webSockUrl());
2124
2125 webSock.ws.onopen = function() {
Simon Hunt0c6d4192014-11-12 12:07:10 -08002126 noWebSock(false);
Thomas Vachuska47635c62014-11-22 01:21:36 -08002127 requestSummary();
Simon Huntb0ecfa52014-11-23 21:05:12 -08002128 showInstances();
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002129 };
2130
2131 webSock.ws.onmessage = function(m) {
2132 if (m.data) {
Simon Huntbb282f52014-11-10 11:08:19 -08002133 wsTraceRx(m.data);
Thomas Vachuskad472c6e2014-11-07 19:11:05 -08002134 handleServerEvent(JSON.parse(m.data));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002135 }
2136 };
2137
2138 webSock.ws.onclose = function(m) {
2139 webSock.ws = null;
Simon Hunt0c6d4192014-11-12 12:07:10 -08002140 noWebSock(true);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002141 };
2142 },
2143
2144 send : function(text) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002145 if (text != null) {
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002146 webSock._send(text);
2147 }
2148 },
2149
2150 _send : function(message) {
2151 if (webSock.ws) {
2152 webSock.ws.send(message);
Simon Hunta255a2c2014-11-13 22:29:35 -08002153 } else if (config.useLiveData) {
Simon Hunt434cf142014-11-24 11:10:28 -08002154 console.warn('no web socket open', message);
Simon Hunta255a2c2014-11-13 22:29:35 -08002155 } else {
Simon Hunt434cf142014-11-24 11:10:28 -08002156 console.log('WS Send: ', message);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002157 }
2158 }
2159
2160 };
2161
Simon Hunt0c6d4192014-11-12 12:07:10 -08002162 function noWebSock(b) {
2163 mask.style('display',b ? 'block' : 'none');
2164 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002165
2166 function sendMessage(evType, payload) {
2167 var toSend = {
Simon Huntbb282f52014-11-10 11:08:19 -08002168 event: evType,
2169 sid: ++sid,
2170 payload: payload
2171 },
2172 asText = JSON.stringify(toSend);
2173 wsTraceTx(asText);
2174 webSock.send(asText);
Simon Huntc76ae892014-11-18 17:31:51 -08002175
2176 // Temporary measure for debugging UI behavior ...
2177 if (!config.useLiveData) {
2178 handleTestSend(toSend);
2179 }
Simon Huntbb282f52014-11-10 11:08:19 -08002180 }
2181
2182 function wsTraceTx(msg) {
2183 wsTrace('tx', msg);
2184 }
2185 function wsTraceRx(msg) {
2186 wsTrace('rx', msg);
2187 }
2188 function wsTrace(rxtx, msg) {
Simon Huntbb282f52014-11-10 11:08:19 -08002189 console.log('[' + rxtx + '] ' + msg);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002190 }
2191
Simon Huntc76ae892014-11-18 17:31:51 -08002192 // NOTE: Temporary hardcoded example for showing detail pane
2193 // while we fine-
2194 // Probably should not merge this change...
2195 function handleTestSend(msg) {
2196 if (msg.event === 'requestDetails') {
2197 showDetails({
2198 event: 'showDetails',
2199 sid: 1001,
2200 payload: {
2201 "id": "of:0000ffffffffff09",
2202 "type": "roadm",
2203 "propOrder": [
2204 "Name",
2205 "Vendor",
2206 "H/W Version",
2207 "S/W Version",
2208 "-",
2209 "Latitude",
2210 "Longitude",
2211 "Ports"
2212 ],
2213 "props": {
2214 "Name": null,
2215 "Vendor": "Linc",
2216 "H/W Version": "OE",
2217 "S/W Version": "?",
2218 "-": "",
2219 "Latitude": "40.8",
2220 "Longitude": "73.1",
2221 "Ports": "2"
2222 }
2223 }
2224 });
2225 }
2226 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002227
2228 // ==============================
2229 // Selection stuff
2230
2231 function selectObject(obj, el) {
2232 var n,
Simon Hunt01095ff2014-11-13 16:37:29 -08002233 srcEv = d3.event.sourceEvent,
2234 meta = srcEv.metaKey,
2235 shift = srcEv.shiftKey;
2236
Simon Huntdeab4322014-11-13 18:49:07 -08002237 if ((panZoom() && !meta) || (!panZoom() && meta)) {
Simon Hunt01095ff2014-11-13 16:37:29 -08002238 return;
2239 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002240
2241 if (el) {
2242 n = d3.select(el);
2243 } else {
2244 node.each(function(d) {
2245 if (d == obj) {
2246 n = d3.select(el = this);
2247 }
2248 });
2249 }
2250 if (!n) return;
2251
Simon Hunt01095ff2014-11-13 16:37:29 -08002252 if (shift && n.classed('selected')) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002253 deselectObject(obj.id);
Simon Hunt61d04042014-11-11 17:27:16 -08002254 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002255 return;
2256 }
2257
Simon Hunt01095ff2014-11-13 16:37:29 -08002258 if (!shift) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002259 deselectAll();
2260 }
2261
Simon Huntc31d5692014-11-12 13:27:18 -08002262 selections[obj.id] = { obj: obj, el: el };
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002263 selectOrder.push(obj.id);
2264
2265 n.classed('selected', true);
Simon Huntb0ecfa52014-11-23 21:05:12 -08002266 updateDeviceColors(obj);
Simon Hunt61d04042014-11-11 17:27:16 -08002267 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002268 }
2269
2270 function deselectObject(id) {
Simon Huntc31d5692014-11-12 13:27:18 -08002271 var obj = selections[id],
2272 idx;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002273 if (obj) {
2274 d3.select(obj.el).classed('selected', false);
Simon Hunt61d04042014-11-11 17:27:16 -08002275 delete selections[id];
Simon Huntc31d5692014-11-12 13:27:18 -08002276 idx = $.inArray(id, selectOrder);
2277 if (idx >= 0) {
2278 selectOrder.splice(idx, 1);
2279 }
Simon Huntb0ecfa52014-11-23 21:05:12 -08002280 updateDeviceColors(obj.obj);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002281 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002282 }
2283
2284 function deselectAll() {
2285 // deselect all nodes in the network...
2286 node.classed('selected', false);
2287 selections = {};
2288 selectOrder = [];
Simon Huntb0ecfa52014-11-23 21:05:12 -08002289 updateDeviceColors();
Simon Hunt61d04042014-11-11 17:27:16 -08002290 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002291 }
2292
Simon Huntb0ecfa52014-11-23 21:05:12 -08002293 function updateDeviceColors(d) {
2294 if (d) {
2295 setDeviceColor(d);
2296 } else {
2297 node.filter('.device').each(function (d) {
2298 setDeviceColor(d);
2299 });
2300 }
Thomas Vachuska47635c62014-11-22 01:21:36 -08002301 }
2302
Simon Hunt61d04042014-11-11 17:27:16 -08002303 // update the state of the detail pane, based on current selections
2304 function updateDetailPane() {
2305 var nSel = selectOrder.length;
2306 if (!nSel) {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002307 emptySelect();
Simon Hunt61d04042014-11-11 17:27:16 -08002308 } else if (nSel === 1) {
2309 singleSelect();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002310 requestTrafficForMode();
Simon Hunt61d04042014-11-11 17:27:16 -08002311 } else {
2312 multiSelect();
2313 }
2314 }
2315
Thomas Vachuska9edca302014-11-22 17:06:42 -08002316 function emptySelect() {
Simon Hunt27d322d2014-11-28 10:45:43 -08002317 haveDetails = false;
Simon Hunt06811b72014-11-25 18:54:48 -08002318 hideDetailPane();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002319 cancelTraffic();
2320 }
2321
Simon Hunt61d04042014-11-11 17:27:16 -08002322 function singleSelect() {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002323 // NOTE: detail is shown from showDetails event callback
Simon Hunt61d04042014-11-11 17:27:16 -08002324 requestDetails();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002325 requestTrafficForMode();
Simon Hunt61d04042014-11-11 17:27:16 -08002326 }
2327
2328 function multiSelect() {
Simon Hunt27d322d2014-11-28 10:45:43 -08002329 haveDetails = true;
Simon Huntb53e0682014-11-12 13:32:01 -08002330 populateMultiSelect();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002331 requestTrafficForMode();
Simon Huntb53e0682014-11-12 13:32:01 -08002332 }
2333
2334 function addSep(tbody) {
2335 var tr = tbody.append('tr');
2336 $('<hr>').appendTo(tr.append('td').attr('colspan', 2));
2337 }
2338
2339 function addProp(tbody, label, value) {
2340 var tr = tbody.append('tr');
2341
2342 tr.append('td')
2343 .attr('class', 'label')
2344 .text(label + ' :');
2345
2346 tr.append('td')
2347 .attr('class', 'value')
2348 .text(value);
2349 }
2350
2351 function populateMultiSelect() {
2352 detailPane.empty();
2353
Simon Hunta3dd9572014-11-20 15:22:41 -08002354 var title = detailPane.append('h3'),
2355 table = detailPane.append('table'),
2356 tbody = table.append('tbody');
Simon Huntb53e0682014-11-12 13:32:01 -08002357
Thomas Vachuska4731f122014-11-20 04:56:19 -08002358 title.text('Selected Nodes');
Simon Huntb53e0682014-11-12 13:32:01 -08002359
2360 selectOrder.forEach(function (d, i) {
2361 addProp(tbody, i+1, d);
2362 });
Simon Huntd72bc702014-11-13 18:38:04 -08002363
2364 addMultiSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08002365 }
2366
Thomas Vachuska47635c62014-11-22 01:21:36 -08002367 // TODO: refactor to consolidate with populateDetails
2368 function populateSummary(data) {
2369 summaryPane.empty();
2370
2371 var svg = summaryPane.append('svg'),
2372 iid = iconGlyphUrl(data);
2373
2374 var title = summaryPane.append('h2'),
2375 table = summaryPane.append('table'),
2376 tbody = table.append('tbody');
2377
2378 appendGlyph(svg, 0, 0, 40, iid);
2379
2380 svg.append('use')
2381 .attr({
2382 class: 'birdBadge',
2383 transform: translate(8,12),
2384 'xlink:href': '#bird',
2385 width: 24,
2386 height: 24,
2387 fill: '#fff'
2388 });
2389
2390 title.text('ONOS Summary');
2391
2392 data.propOrder.forEach(function(p) {
2393 if (p === '-') {
2394 addSep(tbody);
2395 } else {
2396 addProp(tbody, p, data.props[p]);
2397 }
2398 });
2399 }
2400
Simon Hunt61d04042014-11-11 17:27:16 -08002401 function populateDetails(data) {
2402 detailPane.empty();
2403
Simon Hunta6a9fe72014-11-20 11:17:12 -08002404 var svg = detailPane.append('svg'),
2405 iid = iconGlyphUrl(data);
2406
Simon Hunta3dd9572014-11-20 15:22:41 -08002407 var title = detailPane.append('h2'),
2408 table = detailPane.append('table'),
2409 tbody = table.append('tbody');
Simon Hunt61d04042014-11-11 17:27:16 -08002410
Simon Hunta6a9fe72014-11-20 11:17:12 -08002411 appendGlyph(svg, 0, 0, 40, iid);
2412 title.text(data.id);
Simon Hunt61d04042014-11-11 17:27:16 -08002413
2414 data.propOrder.forEach(function(p) {
2415 if (p === '-') {
2416 addSep(tbody);
2417 } else {
2418 addProp(tbody, p, data.props[p]);
2419 }
2420 });
Simon Huntd72bc702014-11-13 18:38:04 -08002421
Thomas Vachuska4731f122014-11-20 04:56:19 -08002422 addSingleSelectActions(data);
Simon Hunt61d04042014-11-11 17:27:16 -08002423 }
2424
Thomas Vachuska4731f122014-11-20 04:56:19 -08002425 function addSingleSelectActions(data) {
Simon Huntd72bc702014-11-13 18:38:04 -08002426 detailPane.append('hr');
2427 // always want to allow 'show traffic'
Thomas Vachuska4731f122014-11-20 04:56:19 -08002428 addAction(detailPane, 'Show Related Traffic', showTrafficAction);
2429
2430 if (data.type === 'switch') {
2431 addAction(detailPane, 'Show Device Flows', showDeviceLinkFlowsAction);
2432 }
Simon Huntd72bc702014-11-13 18:38:04 -08002433 }
2434
2435 function addMultiSelectActions() {
2436 detailPane.append('hr');
2437 // always want to allow 'show traffic'
Thomas Vachuska4731f122014-11-20 04:56:19 -08002438 addAction(detailPane, 'Show Related Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08002439 // if exactly two hosts are selected, also want 'add host intent'
2440 if (nSel() === 2 && allSelectionsClass('host')) {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002441 addAction(detailPane, 'Create Host-to-Host Flow', addHostIntentAction);
2442 } else if (nSel() >= 2 && allSelectionsClass('host')) {
2443 addAction(detailPane, 'Create Multi-Source Flow', addMultiSourceIntentAction);
Simon Huntd72bc702014-11-13 18:38:04 -08002444 }
2445 }
2446
Simon Hunta5e89142014-11-14 07:00:33 -08002447 function addAction(panel, text, cb) {
2448 panel.append('div')
Simon Huntd72bc702014-11-13 18:38:04 -08002449 .classed('actionBtn', true)
2450 .text(text)
2451 .on('click', cb);
2452 }
2453
2454
Paul Greysonfcba0e82014-11-13 10:21:16 -08002455 function zoomPan(scale, translate) {
2456 zoomPanContainer.attr("transform", "translate(" + translate + ")scale(" + scale + ")");
2457 // keep the map lines constant width while zooming
Thomas Vachuska89543292014-11-19 11:28:33 -08002458 bgImg.style("stroke-width", 2.0 / scale + "px");
Paul Greysonfcba0e82014-11-13 10:21:16 -08002459 }
2460
2461 function resetZoomPan() {
2462 zoomPan(1, [0,0]);
2463 zoom.scale(1).translate([0,0]);
2464 }
2465
2466 function setupZoomPan() {
2467 function zoomed() {
Simon Huntdeab4322014-11-13 18:49:07 -08002468 if (!panZoom() ^ !d3.event.sourceEvent.metaKey) {
Paul Greysonfcba0e82014-11-13 10:21:16 -08002469 zoomPan(d3.event.scale, d3.event.translate);
2470 }
2471 }
2472
2473 zoom = d3.behavior.zoom()
2474 .translate([0, 0])
2475 .scale(1)
2476 .scaleExtent([1, 8])
2477 .on("zoom", zoomed);
2478
2479 svg.call(zoom);
2480 }
2481
Simon Hunt61d04042014-11-11 17:27:16 -08002482 // ==============================
2483 // Test harness code
Simon Hunt56d51852014-11-09 13:03:35 -08002484
2485 function prepareScenario(view, ctx, dbg) {
2486 var sc = scenario,
2487 urlSc = sc.evDir + ctx + sc.evScenario;
2488
2489 if (!ctx) {
2490 view.alert("No scenario specified (null ctx)");
2491 return;
2492 }
2493
2494 sc.view = view;
2495 sc.ctx = ctx;
2496 sc.debug = dbg;
2497 sc.evNumber = 0;
2498
2499 d3.json(urlSc, function(err, data) {
Simon Huntbb282f52014-11-10 11:08:19 -08002500 var p = data && data.params || {},
2501 desc = data && data.description || null,
Simon Huntfc274c92014-11-11 11:05:46 -08002502 intro = data && data.title;
Simon Huntbb282f52014-11-10 11:08:19 -08002503
Simon Hunt56d51852014-11-09 13:03:35 -08002504 if (err) {
2505 view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err);
2506 } else {
2507 sc.params = p;
Simon Huntbb282f52014-11-10 11:08:19 -08002508 if (desc) {
2509 intro += '\n\n ' + desc.join('\n ');
2510 }
2511 view.alert(intro);
Simon Hunt56d51852014-11-09 13:03:35 -08002512 }
2513 });
2514
2515 }
2516
Simon Hunt01095ff2014-11-13 16:37:29 -08002517 // ==============================
2518 // Toggle Buttons in masthead
Simon Hunt0c6d4192014-11-12 12:07:10 -08002519
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002520 // TODO: toggle button (and other widgets in the masthead) should be provided
2521 // by the framework; not generated by the view.
2522
Thomas Vachuska47635c62014-11-22 01:21:36 -08002523 //var showInstances;
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002524
Simon Hunt87514342014-11-24 16:41:27 -08002525/*
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002526 function addButtonBar(view) {
2527 var bb = d3.select('#mast')
2528 .append('span').classed('right', true).attr('id', 'bb');
2529
Simon Hunta5e89142014-11-14 07:00:33 -08002530 function mkTogBtn(text, cb) {
2531 return bb.append('span')
2532 .classed('btn', true)
2533 .text(text)
2534 .on('click', cb);
2535 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002536
Thomas Vachuska47635c62014-11-22 01:21:36 -08002537 //showInstances = mkTogBtn('Show Instances', toggleInst);
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002538 }
Simon Hunt87514342014-11-24 16:41:27 -08002539*/
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002540
Simon Huntdeab4322014-11-13 18:49:07 -08002541 function panZoom() {
Simon Hunte5b71752014-11-18 20:06:07 -08002542 return false;
Simon Hunta5e89142014-11-14 07:00:33 -08002543 }
2544
Simon Hunt7fa116d2014-11-17 14:16:55 -08002545 function loadGlyphs(svg) {
2546 var defs = svg.append('defs');
2547 gly.defBird(defs);
Simon Huntc72967b2014-11-20 09:21:42 -08002548 gly.defGlyphs(defs);
Simon Huntb82f6902014-11-22 11:53:15 -08002549 gly.defBadges(defs);
Simon Hunt7fa116d2014-11-17 14:16:55 -08002550 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002551
Simon Hunt395a70c2014-11-22 23:17:40 -08002552 function sendUpdateMeta(d, store) {
2553 var metaUi = {},
2554 ll;
2555
2556 if (store) {
2557 ll = geoMapProjection.invert([d.x, d.y]);
Simon Hunt62c47542014-11-22 22:16:32 -08002558 metaUi = {
2559 x: d.x,
2560 y: d.y,
2561 lng: ll[0],
2562 lat: ll[1]
2563 };
Simon Hunt395a70c2014-11-22 23:17:40 -08002564 }
Simon Hunt62c47542014-11-22 22:16:32 -08002565 d.metaUi = metaUi;
2566 sendMessage('updateMeta', {
2567 id: d.id,
2568 'class': d.class,
2569 'memento': metaUi
2570 });
2571 }
2572
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002573 // ==============================
Simon Hunt142d0032014-11-04 20:13:09 -08002574 // View life-cycle callbacks
Simon Hunt195cb382014-11-03 17:50:51 -08002575
Simon Huntf67722a2014-11-10 09:32:06 -08002576 function preload(view, ctx, flags) {
Simon Hunt142d0032014-11-04 20:13:09 -08002577 var w = view.width(),
2578 h = view.height(),
Simon Huntc7ee0662014-11-05 16:44:37 -08002579 fcfg = config.force,
2580 fpad = fcfg.pad,
2581 forceDim = [w - 2*fpad, h - 2*fpad];
Simon Hunt195cb382014-11-03 17:50:51 -08002582
Simon Hunt142d0032014-11-04 20:13:09 -08002583 // NOTE: view.$div is a D3 selection of the view's div
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002584 var viewBox = '0 0 ' + config.logicalSize + ' ' + config.logicalSize;
2585 svg = view.$div.append('svg').attr('viewBox', viewBox);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002586 setSize(svg, view);
2587
Simon Hunt7fa116d2014-11-17 14:16:55 -08002588 loadGlyphs(svg);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002589
Paul Greysonfcba0e82014-11-13 10:21:16 -08002590 zoomPanContainer = svg.append('g').attr('id', 'zoomPanContainer');
Paul Greysonfcba0e82014-11-13 10:21:16 -08002591 setupZoomPan();
2592
Simon Hunt1a9eff92014-11-07 11:06:34 -08002593 // add blue glow filter to svg layer
Paul Greysonfcba0e82014-11-13 10:21:16 -08002594 d3u.appendGlow(zoomPanContainer);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002595
Simon Huntc7ee0662014-11-05 16:44:37 -08002596 // group for the topology
Paul Greysonfcba0e82014-11-13 10:21:16 -08002597 topoG = zoomPanContainer.append('g')
Simon Huntd3b7d512014-11-12 15:48:41 -08002598 .attr('id', 'topo-G')
Simon Huntc7ee0662014-11-05 16:44:37 -08002599 .attr('transform', fcfg.translate());
2600
Simon Hunte2575b62014-11-18 15:25:53 -08002601 // subgroups for links, link labels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002602 linkG = topoG.append('g').attr('id', 'links');
Simon Hunte2575b62014-11-18 15:25:53 -08002603 linkLabelG = topoG.append('g').attr('id', 'linkLabels');
Simon Huntc7ee0662014-11-05 16:44:37 -08002604 nodeG = topoG.append('g').attr('id', 'nodes');
2605
Simon Hunte2575b62014-11-18 15:25:53 -08002606 // selection of links, linkLabels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002607 link = linkG.selectAll('.link');
Simon Hunte2575b62014-11-18 15:25:53 -08002608 linkLabel = linkLabelG.selectAll('.linkLabel');
Simon Huntc7ee0662014-11-05 16:44:37 -08002609 node = nodeG.selectAll('.node');
2610
Simon Hunt7cd48f32014-11-09 23:42:50 -08002611 function chrg(d) {
2612 return fcfg.charge[d.class] || -12000;
2613 }
Simon Hunt99c13842014-11-06 18:23:12 -08002614 function ldist(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002615 return fcfg.linkDistance[d.type] || 50;
Simon Hunt99c13842014-11-06 18:23:12 -08002616 }
2617 function lstrg(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002618 // 0.0 - 1.0
2619 return fcfg.linkStrength[d.type] || 1.0;
Simon Hunt99c13842014-11-06 18:23:12 -08002620 }
2621
Simon Hunt1a9eff92014-11-07 11:06:34 -08002622 function selectCb(d, self) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002623 selectObject(d, self);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002624 }
2625
2626 function atDragEnd(d, self) {
Simon Hunt56d51852014-11-09 13:03:35 -08002627 // once we've finished moving, pin the node in position
2628 d.fixed = true;
2629 d3.select(self).classed('fixed', true);
2630 if (config.useLiveData) {
Simon Hunt395a70c2014-11-22 23:17:40 -08002631 sendUpdateMeta(d, true);
Simon Hunta255a2c2014-11-13 22:29:35 -08002632 } else {
2633 console.log('Moving node ' + d.id + ' to [' + d.x + ',' + d.y + ']');
Simon Hunt1a9eff92014-11-07 11:06:34 -08002634 }
2635 }
2636
Simon Huntc7ee0662014-11-05 16:44:37 -08002637 // set up the force layout
2638 network.force = d3.layout.force()
2639 .size(forceDim)
2640 .nodes(network.nodes)
2641 .links(network.links)
Simon Hunt7cd48f32014-11-09 23:42:50 -08002642 .gravity(0.4)
2643 .friction(0.7)
2644 .charge(chrg)
Simon Hunt99c13842014-11-06 18:23:12 -08002645 .linkDistance(ldist)
2646 .linkStrength(lstrg)
Simon Huntc7ee0662014-11-05 16:44:37 -08002647 .on('tick', tick);
Simon Hunt195cb382014-11-03 17:50:51 -08002648
Simon Hunt01095ff2014-11-13 16:37:29 -08002649 network.drag = d3u.createDragBehavior(network.force,
Simon Huntdeab4322014-11-13 18:49:07 -08002650 selectCb, atDragEnd, panZoom);
Simon Hunt0c6d4192014-11-12 12:07:10 -08002651
2652 // create mask layer for when we lose connection to server.
Simon Hunta5e89142014-11-14 07:00:33 -08002653 // TODO: this should be part of the framework
Simon Hunt0c6d4192014-11-12 12:07:10 -08002654 mask = view.$div.append('div').attr('id','topo-mask');
2655 para(mask, 'Oops!');
2656 para(mask, 'Web-socket connection to server closed...');
2657 para(mask, 'Try refreshing the page.');
Simon Hunt12ce12e2014-11-15 21:13:19 -08002658
2659 mask.append('svg')
2660 .attr({
2661 id: 'mask-bird',
2662 width: w,
2663 height: h
2664 })
2665 .append('g')
2666 .attr('transform', birdTranslate(w, h))
2667 .style('opacity', 0.3)
2668 .append('use')
2669 .attr({
2670 'xlink:href': '#bird',
2671 width: config.birdDim,
2672 height: config.birdDim,
2673 fill: '#111'
Thomas Vachuska89543292014-11-19 11:28:33 -08002674 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08002675 }
Simon Hunt195cb382014-11-03 17:50:51 -08002676
Simon Hunt01095ff2014-11-13 16:37:29 -08002677 function para(sel, text) {
2678 sel.append('p').text(text);
2679 }
2680
2681
Simon Hunt56d51852014-11-09 13:03:35 -08002682 function load(view, ctx, flags) {
Simon Huntf67722a2014-11-10 09:32:06 -08002683 // resize, in case the window was resized while we were not loaded
2684 resize(view, ctx, flags);
2685
Simon Hunt99c13842014-11-06 18:23:12 -08002686 // cache the view token, so network topo functions can access it
2687 network.view = view;
Simon Hunt56d51852014-11-09 13:03:35 -08002688 config.useLiveData = !flags.local;
2689
2690 if (!config.useLiveData) {
2691 prepareScenario(view, ctx, flags.debug);
2692 }
Simon Hunt99c13842014-11-06 18:23:12 -08002693
2694 // set our radio buttons and key bindings
Simon Hunt9462e8c2014-11-14 17:28:09 -08002695 layerBtnSet = view.setRadio(layerButtons);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002696 view.setKeys(keyDispatch);
Simon Hunt87514342014-11-24 16:41:27 -08002697 view.setGestures(gestures);
Simon Hunt195cb382014-11-03 17:50:51 -08002698
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002699 // patch in our "button bar" for now
2700 // TODO: implement a more official frameworky way of doing this..
Simon Hunt87514342014-11-24 16:41:27 -08002701 //addButtonBar(view);
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002702
Simon Huntd3b7d512014-11-12 15:48:41 -08002703 // Load map data asynchronously; complete startup after that..
2704 loadGeoJsonData();
Simon Hunta255a2c2014-11-13 22:29:35 -08002705 }
2706
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002707 function startAntTimer() {
Thomas Vachuska4731f122014-11-20 04:56:19 -08002708 if (!antTimer) {
2709 var pulses = [5, 3, 1.2, 3],
2710 pulse = 0;
2711 antTimer = setInterval(function () {
2712 pulse = pulse + 1;
2713 pulse = pulse === pulses.length ? 0 : pulse;
2714 d3.selectAll('.animated').style('stroke-width', pulses[pulse]);
2715 }, 200);
2716 }
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002717 }
2718
2719 function stopAntTimer() {
Simon Hunta255a2c2014-11-13 22:29:35 -08002720 if (antTimer) {
2721 clearInterval(antTimer);
2722 antTimer = null;
2723 }
Simon Huntd3b7d512014-11-12 15:48:41 -08002724 }
2725
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002726 function unload(view, ctx, flags) {
2727 stopAntTimer();
2728 }
2729
Simon Huntd3b7d512014-11-12 15:48:41 -08002730 // TODO: move these to config/state portion of script
Simon Hunta6a9fe72014-11-20 11:17:12 -08002731 var geoJsonUrl = 'json/map/continental_us.json',
Simon Huntd3b7d512014-11-12 15:48:41 -08002732 geoJson;
2733
2734 function loadGeoJsonData() {
2735 d3.json(geoJsonUrl, function (err, data) {
2736 if (err) {
2737 // fall back to USA map background
2738 loadStaticMap();
2739 } else {
2740 geoJson = data;
2741 loadGeoMap();
2742 }
2743
2744 // finally, connect to the server...
2745 if (config.useLiveData) {
2746 webSock.connect();
2747 }
2748 });
2749 }
2750
2751 function showBg() {
Simon Hunt434cf142014-11-24 11:10:28 -08002752 return visVal(config.options.showBackground);
Simon Huntd3b7d512014-11-12 15:48:41 -08002753 }
2754
2755 function loadStaticMap() {
2756 fnTrace('loadStaticMap', config.backgroundUrl);
2757 var w = network.view.width(),
2758 h = network.view.height();
2759
2760 // load the background image
2761 bgImg = svg.insert('svg:image', '#topo-G')
2762 .attr({
2763 id: 'topo-bg',
2764 width: w,
2765 height: h,
2766 'xlink:href': config.backgroundUrl
2767 })
2768 .style({
2769 visibility: showBg()
2770 });
2771 }
2772
2773 function loadGeoMap() {
2774 fnTrace('loadGeoMap', geoJsonUrl);
Simon Huntd3b7d512014-11-12 15:48:41 -08002775
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002776 // extracts the topojson data into geocoordinate-based geometry
2777 var topoData = topojson.feature(geoJson, geoJson.objects.states);
Simon Huntd3b7d512014-11-12 15:48:41 -08002778
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002779 // see: http://bl.ocks.org/mbostock/4707858
2780 geoMapProjection = d3.geo.mercator();
2781 var path = d3.geo.path().projection(geoMapProjection);
Simon Huntd3b7d512014-11-12 15:48:41 -08002782
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002783 geoMapProjection
2784 .scale(1)
2785 .translate([0, 0]);
Simon Huntd3b7d512014-11-12 15:48:41 -08002786
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002787 // [[x1,y1],[x2,y2]]
2788 var b = path.bounds(topoData);
Paul Greysonfcba0e82014-11-13 10:21:16 -08002789 // size map to 95% of minimum dimension to fill space
2790 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 -08002791 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 -08002792
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002793 geoMapProjection
2794 .scale(s)
2795 .translate(t);
2796
Paul Greysonfcba0e82014-11-13 10:21:16 -08002797 bgImg = zoomPanContainer.insert("g", '#topo-G');
Thomas Vachuska89543292014-11-19 11:28:33 -08002798 bgImg.attr('id', 'map').selectAll('path')
2799 .data(topoData.features)
2800 .enter()
2801 .append('path')
2802 .attr('d', path);
Simon Hunt195cb382014-11-03 17:50:51 -08002803 }
2804
Simon Huntf67722a2014-11-10 09:32:06 -08002805 function resize(view, ctx, flags) {
Simon Hunt12ce12e2014-11-15 21:13:19 -08002806 var w = view.width(),
2807 h = view.height();
2808
Simon Hunt934c3ce2014-11-05 11:45:07 -08002809 setSize(svg, view);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002810
2811 d3.select('#mask-bird').attr({ width: w, height: h})
2812 .select('g').attr('transform', birdTranslate(w, h));
Simon Hunt142d0032014-11-04 20:13:09 -08002813 }
2814
Simon Hunt8f40cce2014-11-23 15:57:30 -08002815 function theme(view, ctx, flags) {
2816 updateInstances();
Simon Huntb0ecfa52014-11-23 21:05:12 -08002817 updateDeviceColors();
Simon Hunt8f40cce2014-11-23 15:57:30 -08002818 }
2819
Simon Hunt12ce12e2014-11-15 21:13:19 -08002820 function birdTranslate(w, h) {
2821 var bdim = config.birdDim;
2822 return 'translate('+((w-bdim)*.4)+','+((h-bdim)*.1)+')';
2823 }
Simon Hunt142d0032014-11-04 20:13:09 -08002824
Simon Hunt06811b72014-11-25 18:54:48 -08002825 function isF(f) { return $.isFunction(f) ? f : null; }
2826 function noop() {}
2827
2828 function augmentDetailPane() {
2829 var dp = detailPane;
2830 dp.ypos = { up: 64, down: 320, current: 320};
2831
2832 dp._move = function (y, cb) {
2833 var endCb = isF(cb) || noop,
2834 yp = dp.ypos;
2835 if (yp.current !== y) {
2836 yp.current = y;
2837 dp.el.transition().duration(300)
2838 .each('end', endCb)
2839 .style('top', yp.current + 'px');
2840 } else {
2841 endCb();
2842 }
2843 };
2844
2845 dp.down = function (cb) { dp._move(dp.ypos.down, cb); };
2846 dp.up = function (cb) { dp._move(dp.ypos.up, cb); };
2847 }
2848
Simon Hunt142d0032014-11-04 20:13:09 -08002849 // ==============================
2850 // View registration
Simon Hunt195cb382014-11-03 17:50:51 -08002851
Simon Hunt25248912014-11-04 11:25:48 -08002852 onos.ui.addView('topo', {
Simon Hunt142d0032014-11-04 20:13:09 -08002853 preload: preload,
2854 load: load,
Simon Hunta255a2c2014-11-13 22:29:35 -08002855 unload: unload,
Simon Hunt8f40cce2014-11-23 15:57:30 -08002856 resize: resize,
2857 theme: theme
Simon Hunt195cb382014-11-03 17:50:51 -08002858 });
2859
Thomas Vachuska47635c62014-11-22 01:21:36 -08002860 summaryPane = onos.ui.addFloatingPanel('topo-summary');
Simon Hunt61d04042014-11-11 17:27:16 -08002861 detailPane = onos.ui.addFloatingPanel('topo-detail');
Simon Hunt06811b72014-11-25 18:54:48 -08002862 augmentDetailPane();
Simon Hunta5e89142014-11-14 07:00:33 -08002863 oiBox = onos.ui.addFloatingPanel('topo-oibox', 'TL');
Simon Huntb82f6902014-11-22 11:53:15 -08002864 oiBox.width(20);
Simon Hunt61d04042014-11-11 17:27:16 -08002865
Simon Hunt195cb382014-11-03 17:50:51 -08002866}(ONOS));