blob: 640743815ea2ca6b05302d916835cdc465a88dba [file] [log] [blame]
Simon Hunt195cb382014-11-03 17:50:51 -08001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
Simon Hunt142d0032014-11-04 20:13:09 -080018 ONOS network topology viewer - version 1.1
Simon Hunt195cb382014-11-03 17:50:51 -080019
20 @author Simon Hunt
Simon Hunta6a9fe72014-11-20 11:17:12 -080021 @author Thomas Vachuska
Simon Hunt195cb382014-11-03 17:50:51 -080022 */
23
24(function (onos) {
25 'use strict';
26
Simon Hunt1a9eff92014-11-07 11:06:34 -080027 // shorter names for library APIs
Simon Huntbb282f52014-11-10 11:08:19 -080028 var d3u = onos.lib.d3util,
Simon Hunta6a9fe72014-11-20 11:17:12 -080029 gly = onos.lib.glyphs;
Simon Hunt1a9eff92014-11-07 11:06:34 -080030
Simon Hunt195cb382014-11-03 17:50:51 -080031 // configuration data
32 var config = {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -080033 useLiveData: true,
Simon Huntfc274c92014-11-11 11:05:46 -080034 fnTrace: true,
Simon Hunt195cb382014-11-03 17:50:51 -080035 debugOn: false,
36 debug: {
Simon Hunt99c13842014-11-06 18:23:12 -080037 showNodeXY: true,
38 showKeyHandler: false
Simon Hunt195cb382014-11-03 17:50:51 -080039 },
Simon Hunt12ce12e2014-11-15 21:13:19 -080040 birdDim: 400,
Simon Hunt195cb382014-11-03 17:50:51 -080041 options: {
42 layering: true,
43 collisionPrevention: true,
Simon Hunt142d0032014-11-04 20:13:09 -080044 showBackground: true
Simon Hunt195cb382014-11-03 17:50:51 -080045 },
46 backgroundUrl: 'img/us-map.png',
Thomas Vachuska7d638d32014-11-07 10:24:43 -080047 webSockUrl: 'ws/topology',
Simon Hunt195cb382014-11-03 17:50:51 -080048 data: {
49 live: {
50 jsonUrl: 'rs/topology/graph',
51 detailPrefix: 'rs/topology/graph/',
52 detailSuffix: ''
53 },
54 fake: {
55 jsonUrl: 'json/network2.json',
56 detailPrefix: 'json/',
57 detailSuffix: '.json'
58 }
59 },
Simon Hunt99c13842014-11-06 18:23:12 -080060 labels: {
61 imgPad: 16,
62 padLR: 4,
63 padTB: 3,
64 marginLR: 3,
65 marginTB: 2,
66 port: {
67 gap: 3,
68 width: 18,
69 height: 14
70 }
71 },
Simon Hunt1a9eff92014-11-07 11:06:34 -080072 topo: {
Thomas Vachuska89543292014-11-19 11:28:33 -080073 linkBaseColor: '#666',
Simon Hunt1a9eff92014-11-07 11:06:34 -080074 linkInColor: '#66f',
Thomas Vachuska9edca302014-11-22 17:06:42 -080075 linkInWidth: 12,
Thomas Vachuska89543292014-11-19 11:28:33 -080076 linkOutColor: '#f00',
Thomas Vachuska9edca302014-11-22 17:06:42 -080077 linkOutWidth: 10
Simon Hunt1a9eff92014-11-07 11:06:34 -080078 },
Paul Greyson29cd58f2014-11-18 13:14:57 -080079 icons: {
Simon Huntc72967b2014-11-20 09:21:42 -080080 device: {
Simon Hunt395a70c2014-11-22 23:17:40 -080081 dim: 36,
82 rx: 4,
83 xoff: -20,
84 yoff: -18
85 },
86 host: {
87 defaultRadius: 9,
88 radius: {
89 endstation: 14,
90 bgpSpeaker: 14,
91 router: 14
92 }
Simon Huntc72967b2014-11-20 09:21:42 -080093 }
Thomas Vachuska89543292014-11-19 11:28:33 -080094 },
Simon Hunt195cb382014-11-03 17:50:51 -080095 force: {
Simon Hunt7cd48f32014-11-09 23:42:50 -080096 note_for_links: 'link.type is used to differentiate',
Simon Huntc7ee0662014-11-05 16:44:37 -080097 linkDistance: {
Simon Hunt7cd48f32014-11-09 23:42:50 -080098 direct: 100,
99 optical: 120,
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800100 hostLink: 3
Simon Huntc7ee0662014-11-05 16:44:37 -0800101 },
102 linkStrength: {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800103 direct: 1.0,
104 optical: 1.0,
105 hostLink: 1.0
Simon Huntc7ee0662014-11-05 16:44:37 -0800106 },
Simon Hunt7cd48f32014-11-09 23:42:50 -0800107 note_for_nodes: 'node.class is used to differentiate',
Simon Huntc7ee0662014-11-05 16:44:37 -0800108 charge: {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800109 device: -8000,
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800110 host: -5000
Simon Huntc7ee0662014-11-05 16:44:37 -0800111 },
112 pad: 20,
Simon Hunt195cb382014-11-03 17:50:51 -0800113 translate: function() {
114 return 'translate(' +
Simon Huntc7ee0662014-11-05 16:44:37 -0800115 config.force.pad + ',' +
116 config.force.pad + ')';
Simon Hunt195cb382014-11-03 17:50:51 -0800117 }
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800118 },
119 // see below in creation of viewBox on main svg
120 logicalSize: 1000
Simon Hunt195cb382014-11-03 17:50:51 -0800121 };
122
Simon Hunt142d0032014-11-04 20:13:09 -0800123 // radio buttons
Simon Hunt9462e8c2014-11-14 17:28:09 -0800124 var layerButtons = [
125 { text: 'All Layers', id: 'all', cb: showAllLayers },
126 { text: 'Packet Only', id: 'pkt', cb: showPacketLayer },
127 { text: 'Optical Only', id: 'opt', cb: showOpticalLayer }
128 ],
129 layerBtnSet,
130 layerBtnDispatch = {
131 all: showAllLayers,
132 pkt: showPacketLayer,
133 opt: showOpticalLayer
134 };
Simon Hunt934c3ce2014-11-05 11:45:07 -0800135
136 // key bindings
137 var keyDispatch = {
Simon Hunt988c6fc2014-11-20 17:43:03 -0800138 // TODO: remove these "development only" bindings
Simon Hunt8f40cce2014-11-23 15:57:30 -0800139 0: testMe,
140 equals: injectStartupEvents,
141 dash: injectTestEvent,
Simon Hunt99c13842014-11-06 18:23:12 -0800142
Thomas Vachuska47635c62014-11-22 01:21:36 -0800143 O: [toggleSummary, 'Toggle ONOS summary pane'],
144 I: [toggleInstances, 'Toggle ONOS instances pane'],
Simon Hunt988c6fc2014-11-20 17:43:03 -0800145 B: [toggleBg, 'Toggle background image'],
146 L: [cycleLabels, 'Cycle Device labels'],
Simon Hunt934c3ce2014-11-05 11:45:07 -0800147 P: togglePorts,
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800148 U: [unpin, 'Unpin node'],
149 R: [resetZoomPan, 'Reset zoom/pan'],
Thomas Vachuska9edca302014-11-22 17:06:42 -0800150 V: [showTrafficAction, 'Show related traffic'],
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800151 A: [showAllTrafficAction, 'Show all traffic'],
152 F: [showDeviceLinkFlowsAction, 'Show device link flows'],
Simon Hunt9462e8c2014-11-14 17:28:09 -0800153 esc: handleEscape
Simon Hunt934c3ce2014-11-05 11:45:07 -0800154 };
Simon Hunt142d0032014-11-04 20:13:09 -0800155
Simon Hunt195cb382014-11-03 17:50:51 -0800156 // state variables
Simon Hunt99c13842014-11-06 18:23:12 -0800157 var network = {
Simon Hunt50128c02014-11-08 13:36:15 -0800158 view: null, // view token reference
Simon Hunt99c13842014-11-06 18:23:12 -0800159 nodes: [],
160 links: [],
Simon Hunt269670f2014-11-17 16:17:43 -0800161 lookup: {},
162 revLinkToKey: {}
Simon Hunt99c13842014-11-06 18:23:12 -0800163 },
Simon Hunt56d51852014-11-09 13:03:35 -0800164 scenario = {
165 evDir: 'json/ev/',
166 evScenario: '/scenario.json',
167 evPrefix: '/ev_',
168 evOnos: '_onos.json',
169 evUi: '_ui.json',
170 ctx: null,
171 params: {},
172 evNumber: 0,
173 view: null,
174 debug: false
175 },
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800176 webSock,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800177 sid = 0,
Thomas Vachuska60d72bf2014-11-21 13:02:00 -0800178 deviceLabelCount = 3,
Simon Hunt209155e2014-11-21 12:16:09 -0800179 hostLabelCount = 2,
Simon Hunt56d51852014-11-09 13:03:35 -0800180 deviceLabelIndex = 0,
181 hostLabelIndex = 0,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800182 selections = {},
Simon Hunta5e89142014-11-14 07:00:33 -0800183 selectOrder = [],
Simon Hunt6ac93f32014-11-13 12:17:27 -0800184 hovered = null,
Thomas Vachuska47635c62014-11-22 01:21:36 -0800185 summaryPane,
Simon Hunta5e89142014-11-14 07:00:33 -0800186 detailPane,
Simon Hunta255a2c2014-11-13 22:29:35 -0800187 antTimer = null,
Simon Hunta5e89142014-11-14 07:00:33 -0800188 onosInstances = {},
189 onosOrder = [],
190 oiBox,
Simon Hunt9462e8c2014-11-14 17:28:09 -0800191 oiShowMaster = false,
Simon Hunt8f40cce2014-11-23 15:57:30 -0800192 portLabelsOn = false,
193 cat7 = d3u.cat7();
Simon Hunt195cb382014-11-03 17:50:51 -0800194
Thomas Vachuska9edca302014-11-22 17:06:42 -0800195 var hoverModeAll = 1,
196 hoverModeFlows = 2,
197 hoverModeIntents = 3,
198 hoverMode = hoverModeFlows;
199
Simon Hunt934c3ce2014-11-05 11:45:07 -0800200 // D3 selections
201 var svg,
Paul Greysonfcba0e82014-11-13 10:21:16 -0800202 zoomPanContainer,
Simon Hunt934c3ce2014-11-05 11:45:07 -0800203 bgImg,
Simon Huntc7ee0662014-11-05 16:44:37 -0800204 topoG,
205 nodeG,
206 linkG,
Simon Hunte2575b62014-11-18 15:25:53 -0800207 linkLabelG,
Simon Huntc7ee0662014-11-05 16:44:37 -0800208 node,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800209 link,
Simon Hunte2575b62014-11-18 15:25:53 -0800210 linkLabel,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800211 mask;
Simon Hunt195cb382014-11-03 17:50:51 -0800212
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800213 // the projection for the map background
214 var geoMapProjection;
215
Paul Greysonfcba0e82014-11-13 10:21:16 -0800216 // the zoom function
217 var zoom;
218
Simon Hunt142d0032014-11-04 20:13:09 -0800219 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800220 // For Debugging / Development
Simon Hunt195cb382014-11-03 17:50:51 -0800221
Simon Hunt99c13842014-11-06 18:23:12 -0800222 function note(label, msg) {
223 console.log('NOTE: ' + label + ': ' + msg);
Simon Hunt195cb382014-11-03 17:50:51 -0800224 }
225
Simon Hunt99c13842014-11-06 18:23:12 -0800226 function debug(what) {
227 return config.debugOn && config.debug[what];
Simon Hunt934c3ce2014-11-05 11:45:07 -0800228 }
229
Simon Huntfc274c92014-11-11 11:05:46 -0800230 function fnTrace(msg, id) {
231 if (config.fnTrace) {
232 console.log('FN: ' + msg + ' [' + id + ']');
233 }
234 }
Simon Hunt99c13842014-11-06 18:23:12 -0800235
Simon Hunta5e89142014-11-14 07:00:33 -0800236 function evTrace(data) {
237 fnTrace(data.event, data.payload.id);
238 }
239
Simon Hunt934c3ce2014-11-05 11:45:07 -0800240 // ==============================
241 // Key Callbacks
242
Simon Hunt99c13842014-11-06 18:23:12 -0800243 function testMe(view) {
Simon Hunt8f40cce2014-11-23 15:57:30 -0800244 //view.alert('Theme is ' + view.getTheme());
Simon Hunta3dd9572014-11-20 15:22:41 -0800245 //view.flash('This is some text');
Simon Hunt8f40cce2014-11-23 15:57:30 -0800246 cat7.testCard(svg);
Simon Hunt99c13842014-11-06 18:23:12 -0800247 }
248
Simon Hunt56d51852014-11-09 13:03:35 -0800249 function abortIfLive() {
Simon Hunt50128c02014-11-08 13:36:15 -0800250 if (config.useLiveData) {
Simon Huntb53e0682014-11-12 13:32:01 -0800251 network.view.alert("Sorry, currently using live data..");
Simon Hunt56d51852014-11-09 13:03:35 -0800252 return true;
Simon Hunt50128c02014-11-08 13:36:15 -0800253 }
Simon Hunt56d51852014-11-09 13:03:35 -0800254 return false;
255 }
Simon Hunt50128c02014-11-08 13:36:15 -0800256
Simon Hunt56d51852014-11-09 13:03:35 -0800257 function testDebug(msg) {
258 if (scenario.debug) {
259 scenario.view.alert(msg);
260 }
261 }
Simon Hunt99c13842014-11-06 18:23:12 -0800262
Simon Hunt56d51852014-11-09 13:03:35 -0800263 function injectTestEvent(view) {
264 if (abortIfLive()) { return; }
265 var sc = scenario,
266 evn = ++sc.evNumber,
267 pfx = sc.evDir + sc.ctx + sc.evPrefix + evn,
268 onosUrl = pfx + sc.evOnos,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800269 uiUrl = pfx + sc.evUi,
270 stack = [
271 { url: onosUrl, cb: handleServerEvent },
272 { url: uiUrl, cb: handleUiEvent }
273 ];
274 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800275 }
276
Simon Hunt7cd48f32014-11-09 23:42:50 -0800277 function recurseFetchEvent(stack, evn) {
278 var v = scenario.view,
279 frame;
280 if (stack.length === 0) {
Simon Huntfc274c92014-11-11 11:05:46 -0800281 v.alert('Oops!\n\nNo event #' + evn + ' found.');
Simon Hunt7cd48f32014-11-09 23:42:50 -0800282 return;
283 }
284 frame = stack.shift();
285
286 d3.json(frame.url, function (err, data) {
Simon Hunt99c13842014-11-06 18:23:12 -0800287 if (err) {
Simon Hunt56d51852014-11-09 13:03:35 -0800288 if (err.status === 404) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800289 // if we didn't find the data, try the next stack frame
290 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800291 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800292 v.alert('non-404 error:\n\n' + frame.url + '\n\n' + err);
Simon Hunt56d51852014-11-09 13:03:35 -0800293 }
Simon Hunt99c13842014-11-06 18:23:12 -0800294 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800295 testDebug('loaded: ' + frame.url);
Simon Hunt1712ed82014-11-17 12:56:00 -0800296 wsTrace('test', JSON.stringify(data));
Simon Hunt7cd48f32014-11-09 23:42:50 -0800297 frame.cb(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800298 }
299 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800300
Simon Hunt56d51852014-11-09 13:03:35 -0800301 }
Simon Hunt50128c02014-11-08 13:36:15 -0800302
Simon Hunt56d51852014-11-09 13:03:35 -0800303 function handleUiEvent(data) {
Simon Huntbb282f52014-11-10 11:08:19 -0800304 scenario.view.alert('UI Tx: ' + data.event + '\n\n' +
305 JSON.stringify(data));
Simon Hunt56d51852014-11-09 13:03:35 -0800306 }
307
308 function injectStartupEvents(view) {
309 var last = scenario.params.lastAuto || 0;
310 if (abortIfLive()) { return; }
311
312 while (scenario.evNumber < last) {
Simon Hunt1a9eff92014-11-07 11:06:34 -0800313 injectTestEvent(view);
314 }
315 }
316
Simon Hunt934c3ce2014-11-05 11:45:07 -0800317 function toggleBg() {
318 var vis = bgImg.style('visibility');
319 bgImg.style('visibility', (vis === 'hidden') ? 'visible' : 'hidden');
320 }
321
Simon Hunt99c13842014-11-06 18:23:12 -0800322 function cycleLabels() {
Thomas Vachuska60d72bf2014-11-21 13:02:00 -0800323 deviceLabelIndex = (deviceLabelIndex === 2)
Simon Huntbb282f52014-11-10 11:08:19 -0800324 ? 0 : deviceLabelIndex + 1;
Simon Hunt5f36d342014-11-08 21:33:14 -0800325
Simon Hunt99c13842014-11-06 18:23:12 -0800326 network.nodes.forEach(function (d) {
Simon Huntbb282f52014-11-10 11:08:19 -0800327 if (d.class === 'device') {
328 updateDeviceLabel(d);
329 }
Simon Hunt99c13842014-11-06 18:23:12 -0800330 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800331 }
332
333 function togglePorts(view) {
Simon Hunt50128c02014-11-08 13:36:15 -0800334 view.alert('togglePorts() callback')
Simon Hunt934c3ce2014-11-05 11:45:07 -0800335 }
336
Simon Hunt6ac93f32014-11-13 12:17:27 -0800337 function unpin() {
338 if (hovered) {
Simon Hunt395a70c2014-11-22 23:17:40 -0800339 sendUpdateMeta(hovered);
Simon Hunt6ac93f32014-11-13 12:17:27 -0800340 hovered.fixed = false;
341 hovered.el.classed('fixed', false);
342 network.force.resume();
343 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800344 }
345
Simon Hunt9462e8c2014-11-14 17:28:09 -0800346 function handleEscape(view) {
347 if (oiShowMaster) {
348 cancelAffinity();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800349 } else if (detailPane.isVisible()) {
Simon Hunt9462e8c2014-11-14 17:28:09 -0800350 deselectAll();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800351 } else if (oiBox.isVisible()) {
352 oiBox.hide();
353 } else if (summaryPane.isVisible()) {
354 cancelSummary();
Simon Hunt9462e8c2014-11-14 17:28:09 -0800355 }
356 }
357
Simon Hunt934c3ce2014-11-05 11:45:07 -0800358 // ==============================
359 // Radio Button Callbacks
360
Simon Hunta5e89142014-11-14 07:00:33 -0800361 var layerLookup = {
362 host: {
Simon Hunt209155e2014-11-21 12:16:09 -0800363 endstation: 'pkt', // default, if host event does not define type
Thomas Vachuska89543292014-11-19 11:28:33 -0800364 router: 'pkt',
Simon Hunta5e89142014-11-14 07:00:33 -0800365 bgpSpeaker: 'pkt'
366 },
367 device: {
368 switch: 'pkt',
369 roadm: 'opt'
370 },
371 link: {
372 hostLink: 'pkt',
373 direct: 'pkt',
Simon Hunt8257f4c2014-11-16 19:34:54 -0800374 indirect: '',
375 tunnel: '',
Simon Hunta5e89142014-11-14 07:00:33 -0800376 optical: 'opt'
377 }
378 };
379
380 function inLayer(d, layer) {
Simon Hunt8257f4c2014-11-16 19:34:54 -0800381 var type = d.class === 'link' ? d.type() : d.type,
382 look = layerLookup[d.class],
383 lyr = look && look[type];
Simon Hunta5e89142014-11-14 07:00:33 -0800384 return lyr === layer;
385 }
386
387 function unsuppressLayer(which) {
388 node.each(function (d) {
389 var node = d.el;
390 if (inLayer(d, which)) {
391 node.classed('suppressed', false);
392 }
393 });
394
395 link.each(function (d) {
396 var link = d.el;
397 if (inLayer(d, which)) {
398 link.classed('suppressed', false);
399 }
400 });
401 }
402
Simon Hunt9462e8c2014-11-14 17:28:09 -0800403 function suppressLayers(b) {
404 node.classed('suppressed', b);
405 link.classed('suppressed', b);
Simon Hunt142d0032014-11-04 20:13:09 -0800406// d3.selectAll('svg .port').classed('inactive', false);
407// d3.selectAll('svg .portText').classed('inactive', false);
Simon Hunt195cb382014-11-03 17:50:51 -0800408 }
409
Simon Hunt9462e8c2014-11-14 17:28:09 -0800410 function showAllLayers() {
411 suppressLayers(false);
412 }
413
Simon Hunt195cb382014-11-03 17:50:51 -0800414 function showPacketLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800415 node.classed('suppressed', true);
416 link.classed('suppressed', true);
417 unsuppressLayer('pkt');
Simon Hunt195cb382014-11-03 17:50:51 -0800418 }
419
420 function showOpticalLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800421 node.classed('suppressed', true);
422 link.classed('suppressed', true);
423 unsuppressLayer('opt');
Simon Hunt195cb382014-11-03 17:50:51 -0800424 }
425
Simon Hunt9462e8c2014-11-14 17:28:09 -0800426 function restoreLayerState() {
427 layerBtnDispatch[layerBtnSet.selected()]();
428 }
429
Simon Hunt142d0032014-11-04 20:13:09 -0800430 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800431 // Private functions
432
Simon Hunt99c13842014-11-06 18:23:12 -0800433 function safeId(s) {
434 return s.replace(/[^a-z0-9]/gi, '-');
435 }
436
Simon Huntc7ee0662014-11-05 16:44:37 -0800437 // set the size of the given element to that of the view (reduced if padded)
438 function setSize(el, view, pad) {
439 var padding = pad ? pad * 2 : 0;
Simon Hunt934c3ce2014-11-05 11:45:07 -0800440 el.attr({
Simon Huntc7ee0662014-11-05 16:44:37 -0800441 width: view.width() - padding,
442 height: view.height() - padding
Simon Hunt934c3ce2014-11-05 11:45:07 -0800443 });
444 }
445
Simon Hunt8257f4c2014-11-16 19:34:54 -0800446 function makeNodeKey(d, what) {
447 var port = what + 'Port';
448 return d[what] + '/' + d[port];
449 }
450
451 function makeLinkKey(d, flipped) {
452 var one = flipped ? makeNodeKey(d, 'dst') : makeNodeKey(d, 'src'),
453 two = flipped ? makeNodeKey(d, 'src') : makeNodeKey(d, 'dst');
454 return one + '-' + two;
455 }
456
Simon Hunt269670f2014-11-17 16:17:43 -0800457 function findLinkById(id) {
458 // check to see if this is a reverse lookup, else default to given id
459 var key = network.revLinkToKey[id] || id;
460 return key && network.lookup[key];
461 }
462
Simon Hunt8257f4c2014-11-16 19:34:54 -0800463 function findLink(linkData, op) {
464 var key = makeLinkKey(linkData),
465 keyrev = makeLinkKey(linkData, 1),
466 link = network.lookup[key],
467 linkRev = network.lookup[keyrev],
468 result = {},
469 ldata = link || linkRev,
470 rawLink;
471
472 if (op === 'add') {
473 if (link) {
474 // trying to add a link that we already know about
475 result.ldata = link;
476 result.badLogic = 'addLink: link already added';
477
478 } else if (linkRev) {
479 // we found the reverse of the link to be added
480 result.ldata = linkRev;
481 if (linkRev.fromTarget) {
482 result.badLogic = 'addLink: link already added';
483 }
484 }
485 } else if (op === 'update') {
486 if (!ldata) {
487 result.badLogic = 'updateLink: link not found';
488 } else {
489 rawLink = link ? ldata.fromSource : ldata.fromTarget;
490 result.updateWith = function (data) {
491 $.extend(rawLink, data);
492 restyleLinkElement(ldata);
493 }
494 }
495 } else if (op === 'remove') {
496 if (!ldata) {
497 result.badLogic = 'removeLink: link not found';
498 } else {
499 rawLink = link ? ldata.fromSource : ldata.fromTarget;
500
501 if (!rawLink) {
502 result.badLogic = 'removeLink: link not found';
503
504 } else {
505 result.removeRawLink = function () {
506 if (link) {
507 // remove fromSource
508 ldata.fromSource = null;
509 if (ldata.fromTarget) {
510 // promote target into source position
511 ldata.fromSource = ldata.fromTarget;
512 ldata.fromTarget = null;
513 ldata.key = keyrev;
514 delete network.lookup[key];
515 network.lookup[keyrev] = ldata;
Simon Hunt269670f2014-11-17 16:17:43 -0800516 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800517 }
518 } else {
519 // remove fromTarget
520 ldata.fromTarget = null;
Simon Hunt269670f2014-11-17 16:17:43 -0800521 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800522 }
523 if (ldata.fromSource) {
524 restyleLinkElement(ldata);
525 } else {
526 removeLinkElement(ldata);
527 }
528 }
529 }
530 }
531 }
532 return result;
533 }
534
535 function addLinkUpdate(ldata, link) {
536 // add link event, but we already have the reverse link installed
537 ldata.fromTarget = link;
Simon Hunt269670f2014-11-17 16:17:43 -0800538 network.revLinkToKey[link.id] = ldata.key;
Simon Hunt8257f4c2014-11-16 19:34:54 -0800539 restyleLinkElement(ldata);
540 }
541
542 var allLinkTypes = 'direct indirect optical tunnel',
543 defaultLinkType = 'direct';
544
545 function restyleLinkElement(ldata) {
546 // this fn's job is to look at raw links and decide what svg classes
547 // need to be applied to the line element in the DOM
548 var el = ldata.el,
549 type = ldata.type(),
550 lw = ldata.linkWidth(),
551 online = ldata.online();
552
553 el.classed('link', true);
554 el.classed('inactive', !online);
555 el.classed(allLinkTypes, false);
556 if (type) {
557 el.classed(type, true);
558 }
559 el.transition()
560 .duration(1000)
Thomas Vachuska89543292014-11-19 11:28:33 -0800561 .attr('stroke-width', linkScale(lw))
562 .attr('stroke', config.topo.linkBaseColor);
Simon Hunt8257f4c2014-11-16 19:34:54 -0800563 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800564
Simon Hunt99c13842014-11-06 18:23:12 -0800565 // ==============================
566 // Event handlers for server-pushed events
567
Simon Huntbb282f52014-11-10 11:08:19 -0800568 function logicError(msg) {
569 // TODO, report logic error to server, via websock, so it can be logged
Simon Huntcb56cff2014-11-17 11:42:26 -0800570 //network.view.alert('Logic Error:\n\n' + msg);
Simon Huntfc274c92014-11-11 11:05:46 -0800571 console.warn(msg);
Simon Huntbb282f52014-11-10 11:08:19 -0800572 }
573
Simon Hunt99c13842014-11-06 18:23:12 -0800574 var eventDispatch = {
Simon Hunta5e89142014-11-14 07:00:33 -0800575 addInstance: addInstance,
Simon Hunt99c13842014-11-06 18:23:12 -0800576 addDevice: addDevice,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800577 addLink: addLink,
Simon Hunt56d51852014-11-09 13:03:35 -0800578 addHost: addHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800579
Simon Huntfcfb46c2014-11-19 12:53:38 -0800580 updateInstance: updateInstance,
Simon Huntbb282f52014-11-10 11:08:19 -0800581 updateDevice: updateDevice,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800582 updateLink: updateLink,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800583 updateHost: updateHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800584
Simon Hunt7b403bc2014-11-22 19:01:00 -0800585 removeInstance: removeInstance,
Simon Huntbb282f52014-11-10 11:08:19 -0800586 removeDevice: stillToImplement,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800587 removeLink: removeLink,
Simon Hunt44031102014-11-11 13:20:36 -0800588 removeHost: removeHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800589
Simon Hunt61d04042014-11-11 17:27:16 -0800590 showDetails: showDetails,
Thomas Vachuska47635c62014-11-22 01:21:36 -0800591 showSummary: showSummary,
Simon Huntb53e0682014-11-12 13:32:01 -0800592 showTraffic: showTraffic
Simon Hunt99c13842014-11-06 18:23:12 -0800593 };
594
Simon Hunta5e89142014-11-14 07:00:33 -0800595 function addInstance(data) {
596 evTrace(data);
597 var inst = data.payload,
598 id = inst.id;
599 if (onosInstances[id]) {
600 logicError('ONOS instance already added: ' + id);
601 return;
602 }
603 onosInstances[id] = inst;
604 onosOrder.push(inst);
605 updateInstances();
606 }
607
Simon Hunt99c13842014-11-06 18:23:12 -0800608 function addDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800609 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800610 var device = data.payload,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800611 nodeData = createDeviceNode(device);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800612 network.nodes.push(nodeData);
613 network.lookup[nodeData.id] = nodeData;
Simon Hunt99c13842014-11-06 18:23:12 -0800614 updateNodes();
615 network.force.start();
616 }
617
Simon Hunt99c13842014-11-06 18:23:12 -0800618 function addLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800619 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800620 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800621 result = findLink(link, 'add'),
622 bad = result.badLogic,
623 ldata = result.ldata;
624
625 if (bad) {
626 logicError(bad + ': ' + link.id);
627 return;
628 }
629
630 if (ldata) {
631 // we already have a backing store link for src/dst nodes
632 addLinkUpdate(ldata, link);
633 return;
634 }
635
636 // no backing store link yet
637 ldata = createLink(link);
638 if (ldata) {
639 network.links.push(ldata);
640 network.lookup[ldata.key] = ldata;
Simon Hunt99c13842014-11-06 18:23:12 -0800641 updateLinks();
642 network.force.start();
643 }
644 }
645
Simon Hunt56d51852014-11-09 13:03:35 -0800646 function addHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800647 evTrace(data);
Simon Hunt56d51852014-11-09 13:03:35 -0800648 var host = data.payload,
649 node = createHostNode(host),
650 lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800651 network.nodes.push(node);
652 network.lookup[host.id] = node;
653 updateNodes();
654
655 lnk = createHostLink(host);
656 if (lnk) {
Simon Hunt44031102014-11-11 13:20:36 -0800657 node.linkData = lnk; // cache ref on its host
Simon Hunt56d51852014-11-09 13:03:35 -0800658 network.links.push(lnk);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800659 network.lookup[host.ingress] = lnk;
660 network.lookup[host.egress] = lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800661 updateLinks();
662 }
663 network.force.start();
664 }
665
Simon Hunt44031102014-11-11 13:20:36 -0800666 // TODO: fold updateX(...) methods into one base method; remove duplication
Simon Hunt56a2ea42014-11-19 12:39:31 -0800667
668 function updateInstance(data) {
669 evTrace(data);
670 var inst = data.payload,
671 id = inst.id,
672 instData = onosInstances[id];
673 if (instData) {
674 $.extend(instData, inst);
675 updateInstances();
Simon Hunt56a2ea42014-11-19 12:39:31 -0800676 } else {
677 logicError('updateInstance lookup fail. ID = "' + id + '"');
678 }
679 }
680
Simon Huntbb282f52014-11-10 11:08:19 -0800681 function updateDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800682 evTrace(data);
Simon Huntbb282f52014-11-10 11:08:19 -0800683 var device = data.payload,
684 id = device.id,
Simon Hunt62c47542014-11-22 22:16:32 -0800685 d = network.lookup[id];
686 if (d) {
687 $.extend(d, device);
688 if (positionNode(d, true)) {
Simon Hunt395a70c2014-11-22 23:17:40 -0800689 sendUpdateMeta(d, true);
Simon Hunt62c47542014-11-22 22:16:32 -0800690 }
691 updateNodes();
Simon Huntbb282f52014-11-10 11:08:19 -0800692 } else {
693 logicError('updateDevice lookup fail. ID = "' + id + '"');
694 }
695 }
696
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800697 function updateLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800698 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800699 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800700 result = findLink(link, 'update'),
701 bad = result.badLogic;
702 if (bad) {
703 logicError(bad + ': ' + link.id);
704 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800705 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800706 result.updateWith(link);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800707 }
708
Simon Hunt7cd48f32014-11-09 23:42:50 -0800709 function updateHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800710 evTrace(data);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800711 var host = data.payload,
Simon Huntbb282f52014-11-10 11:08:19 -0800712 id = host.id,
713 hostData = network.lookup[id];
714 if (hostData) {
715 $.extend(hostData, host);
716 updateHostState(hostData);
717 } else {
718 logicError('updateHost lookup fail. ID = "' + id + '"');
719 }
Simon Hunt7cd48f32014-11-09 23:42:50 -0800720 }
721
Simon Hunt44031102014-11-11 13:20:36 -0800722 // TODO: fold removeX(...) methods into base method - remove dup code
Simon Hunt7b403bc2014-11-22 19:01:00 -0800723 function removeInstance(data) {
724 evTrace(data);
725 var inst = data.payload,
726 id = inst.id,
727 instData = onosInstances[id];
728 if (instData) {
729 var idx = find(id, onosOrder, 'id');
730 if (idx >= 0) {
731 onosOrder.splice(idx, 1);
732 }
733 delete onosInstances[id];
734 updateInstances();
735 } else {
736 logicError('updateInstance lookup fail. ID = "' + id + '"');
737 }
738 }
739
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800740 function removeLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800741 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800742 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800743 result = findLink(link, 'remove'),
744 bad = result.badLogic;
745 if (bad) {
746 logicError(bad + ': ' + link.id);
747 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800748 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800749 result.removeRawLink();
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800750 }
751
Simon Hunt44031102014-11-11 13:20:36 -0800752 function removeHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800753 evTrace(data);
Simon Hunt44031102014-11-11 13:20:36 -0800754 var host = data.payload,
755 id = host.id,
756 hostData = network.lookup[id];
757 if (hostData) {
758 removeHostElement(hostData);
759 } else {
760 logicError('removeHost lookup fail. ID = "' + id + '"');
761 }
762 }
763
Thomas Vachuska47635c62014-11-22 01:21:36 -0800764 function showSummary(data) {
765 evTrace(data);
766 populateSummary(data.payload);
767 summaryPane.show();
768 }
769
Simon Hunt61d04042014-11-11 17:27:16 -0800770 function showDetails(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800771 evTrace(data);
Simon Hunt61d04042014-11-11 17:27:16 -0800772 populateDetails(data.payload);
773 detailPane.show();
774 }
775
Simon Huntb53e0682014-11-12 13:32:01 -0800776 function showTraffic(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800777 evTrace(data);
Thomas Vachuska4731f122014-11-20 04:56:19 -0800778 var paths = data.payload.paths,
779 hasTraffic = false;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800780
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800781 // Revert any links hilighted previously.
Thomas Vachuska4731f122014-11-20 04:56:19 -0800782 link.style('stroke-width', null)
Thomas Vachuskaa3148a72014-11-19 21:38:35 -0800783 .classed('primary secondary animated optical', false);
Simon Hunte2575b62014-11-18 15:25:53 -0800784 // Remove all previous labels.
785 removeLinkLabels();
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800786
Simon Hunte2575b62014-11-18 15:25:53 -0800787 // Now hilight all links in the paths payload, and attach
788 // labels to them, if they are defined.
Simon Hunta255a2c2014-11-13 22:29:35 -0800789 paths.forEach(function (p) {
Simon Hunte2575b62014-11-18 15:25:53 -0800790 var n = p.links.length,
791 i,
792 ldata;
793
Thomas Vachuska4731f122014-11-20 04:56:19 -0800794 hasTraffic = hasTraffic || p.traffic;
Simon Hunte2575b62014-11-18 15:25:53 -0800795 for (i=0; i<n; i++) {
796 ldata = findLinkById(p.links[i]);
Thomas Vachuska4731f122014-11-20 04:56:19 -0800797 if (ldata && ldata.el) {
Simon Hunte2575b62014-11-18 15:25:53 -0800798 ldata.el.classed(p.class, true);
799 ldata.label = p.labels[i];
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800800 }
Simon Hunte2575b62014-11-18 15:25:53 -0800801 }
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800802 });
Thomas Vachuska4731f122014-11-20 04:56:19 -0800803
Simon Hunte2575b62014-11-18 15:25:53 -0800804 updateLinks();
Thomas Vachuska4731f122014-11-20 04:56:19 -0800805
806 if (hasTraffic && !antTimer) {
807 startAntTimer();
808 } else if (!hasTraffic && antTimer) {
809 stopAntTimer();
810 }
Simon Huntb53e0682014-11-12 13:32:01 -0800811 }
812
Simon Hunt56d51852014-11-09 13:03:35 -0800813 // ...............................
814
815 function stillToImplement(data) {
816 var p = data.payload;
817 note(data.event, p.id);
Simon Hunt7b403bc2014-11-22 19:01:00 -0800818 if (!config.useLiveData) {
819 network.view.alert('Not yet implemented: "' + data.event + '"');
820 }
Simon Hunt56d51852014-11-09 13:03:35 -0800821 }
Simon Hunt99c13842014-11-06 18:23:12 -0800822
823 function unknownEvent(data) {
Simon Hunt50128c02014-11-08 13:36:15 -0800824 network.view.alert('Unknown event type: "' + data.event + '"');
Simon Hunt99c13842014-11-06 18:23:12 -0800825 }
826
827 function handleServerEvent(data) {
828 var fn = eventDispatch[data.event] || unknownEvent;
829 fn(data);
830 }
831
832 // ==============================
Simon Hunt61d04042014-11-11 17:27:16 -0800833 // Out-going messages...
834
Simon Huntb53e0682014-11-12 13:32:01 -0800835 function userFeedback(msg) {
836 // for now, use the alert pane as is. Maybe different alert style in
837 // the future (centered on view; dismiss button?)
838 network.view.alert(msg);
839 }
840
841 function nSel() {
842 return selectOrder.length;
843 }
Simon Hunt61d04042014-11-11 17:27:16 -0800844 function getSel(idx) {
845 return selections[selectOrder[idx]];
846 }
Simon Huntb53e0682014-11-12 13:32:01 -0800847 function getSelId(idx) {
848 return getSel(idx).obj.id;
849 }
Thomas Vachuska9edca302014-11-22 17:06:42 -0800850 function getSelIds(start, endOffset) {
851 var end = selectOrder.length - endOffset;
852 var ids = [];
853 selectOrder.slice(start, end).forEach(function (d) {
854 ids.push(getSelId(d));
855 });
856 return ids;
857 }
Simon Huntb53e0682014-11-12 13:32:01 -0800858 function allSelectionsClass(cls) {
859 for (var i=0, n=nSel(); i<n; i++) {
860 if (getSel(i).obj.class !== cls) {
861 return false;
862 }
863 }
864 return true;
865 }
Simon Hunt61d04042014-11-11 17:27:16 -0800866
Simon Hunt8f40cce2014-11-23 15:57:30 -0800867 function colorAffinity(on) {
868 // FIXME: need to code this portion up.
869 }
Thomas Vachuska47635c62014-11-22 01:21:36 -0800870
871 function toggleInstances() {
872 if (!oiBox.isVisible()) {
873 oiBox.show();
Simon Hunt8f40cce2014-11-23 15:57:30 -0800874 colorAffinity(true);
Thomas Vachuska47635c62014-11-22 01:21:36 -0800875 } else {
876 oiBox.hide();
Simon Hunt8f40cce2014-11-23 15:57:30 -0800877 colorAffinity(false);
Thomas Vachuska47635c62014-11-22 01:21:36 -0800878 }
879 }
880
881 function toggleSummary() {
882 if (!summaryPane.isVisible()) {
883 requestSummary();
884 } else {
885 cancelSummary();
886 }
887 }
888
889 // request overall summary data
890 function requestSummary() {
891 sendMessage('requestSummary', {});
892 }
893
894 function cancelSummary() {
895 sendMessage('cancelSummary', {});
896 summaryPane.hide();
897 }
898
Simon Hunt61d04042014-11-11 17:27:16 -0800899 // request details for the selected element
Simon Huntd72bc702014-11-13 18:38:04 -0800900 // invoked from selection of a single node.
Simon Hunt61d04042014-11-11 17:27:16 -0800901 function requestDetails() {
902 var data = getSel(0).obj,
903 payload = {
904 id: data.id,
905 class: data.class
906 };
907 sendMessage('requestDetails', payload);
908 }
909
Thomas Vachuska9edca302014-11-22 17:06:42 -0800910 function addHostIntentAction() {
Simon Huntd72bc702014-11-13 18:38:04 -0800911 sendMessage('addHostIntent', {
Thomas Vachuska9edca302014-11-22 17:06:42 -0800912 one: selectOrder[0],
913 two: selectOrder[1],
914 ids: selectOrder
Simon Huntd72bc702014-11-13 18:38:04 -0800915 });
Thomas Vachuska9edca302014-11-22 17:06:42 -0800916 network.view.flash('Host-to-Host flow added');
Simon Huntd72bc702014-11-13 18:38:04 -0800917 }
918
Thomas Vachuska9edca302014-11-22 17:06:42 -0800919 function addMultiSourceIntentAction() {
920 sendMessage('addMultiSourceIntent', {
921 src: selectOrder.slice(0, selectOrder.length - 1),
922 dst: selectOrder[selectOrder.length - 1],
923 ids: selectOrder
924 });
925 network.view.flash('Multi-Source flow added');
Thomas Vachuska29617e52014-11-20 03:17:46 -0800926 }
927
Thomas Vachuska9edca302014-11-22 17:06:42 -0800928
Thomas Vachuska47635c62014-11-22 01:21:36 -0800929 function cancelTraffic() {
930 sendMessage('cancelTraffic', {});
931 }
932
Thomas Vachuska9edca302014-11-22 17:06:42 -0800933 function requestTrafficForMode() {
934 if (hoverMode === hoverModeAll) {
935 requestAllTraffic();
936 } else if (hoverMode === hoverModeFlows) {
937 requestDeviceLinkFlows();
938 } else if (hoverMode === hoverModeIntents) {
939 requestSelectTraffic();
Simon Huntd72bc702014-11-13 18:38:04 -0800940 }
Thomas Vachuska9edca302014-11-22 17:06:42 -0800941 }
Simon Huntd72bc702014-11-13 18:38:04 -0800942
Thomas Vachuska9edca302014-11-22 17:06:42 -0800943 function showTrafficAction() {
944 hoverMode = hoverModeIntents;
945 requestSelectTraffic();
946 network.view.flash('Related Traffic');
947 }
948
949 function requestSelectTraffic() {
950 if (validateSelectionContext()) {
951 var hoverId = (hoverMode === hoverModeIntents && hovered &&
952 (hovered.class === 'host' || hovered.class === 'device'))
Simon Huntd72bc702014-11-13 18:38:04 -0800953 ? hovered.id : '';
Thomas Vachuska9edca302014-11-22 17:06:42 -0800954 sendMessage('requestTraffic', {
955 ids: selectOrder,
956 hover: hoverId
957 });
958 }
Simon Huntd72bc702014-11-13 18:38:04 -0800959 }
960
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800961
Thomas Vachuska29617e52014-11-20 03:17:46 -0800962 function showDeviceLinkFlowsAction() {
Thomas Vachuska9edca302014-11-22 17:06:42 -0800963 hoverMode = hoverModeFlows;
964 requestDeviceLinkFlows();
Thomas Vachuskac59658c2014-11-21 13:13:36 -0800965 network.view.flash('Device Flows');
Thomas Vachuska29617e52014-11-20 03:17:46 -0800966 }
967
Thomas Vachuska9edca302014-11-22 17:06:42 -0800968 function requestDeviceLinkFlows() {
969 if (validateSelectionContext()) {
970 var hoverId = (hoverMode === hoverModeFlows && hovered &&
971 (hovered.class === 'device')) ? hovered.id : '';
972 sendMessage('requestDeviceLinkFlows', {
973 ids: selectOrder,
974 hover: hoverId
975 });
976 }
977 }
978
979
980 function showAllTrafficAction() {
981 hoverMode = hoverModeAll;
982 requestAllTraffic();
983 network.view.flash('All Traffic');
984 }
985
986 function requestAllTraffic() {
987 sendMessage('requestAllTraffic', {});
988 }
989
990 function validateSelectionContext() {
Thomas Vachuska29617e52014-11-20 03:17:46 -0800991 if (!hovered && nSel() === 0) {
Thomas Vachuska47635c62014-11-22 01:21:36 -0800992 cancelTraffic();
Thomas Vachuska9edca302014-11-22 17:06:42 -0800993 return false;
Thomas Vachuska29617e52014-11-20 03:17:46 -0800994 }
Thomas Vachuska9edca302014-11-22 17:06:42 -0800995 return true;
Thomas Vachuska29617e52014-11-20 03:17:46 -0800996 }
Simon Huntd72bc702014-11-13 18:38:04 -0800997
Simon Hunta6a9fe72014-11-20 11:17:12 -0800998
Simon Hunt61d04042014-11-11 17:27:16 -0800999 // ==============================
Simon Hunta5e89142014-11-14 07:00:33 -08001000 // onos instance panel functions
Simon Huntb82f6902014-11-22 11:53:15 -08001001
Simon Hunt7b403bc2014-11-22 19:01:00 -08001002 var instCfg = {
1003 rectPad: 8,
1004 nodeOx: 9,
1005 nodeOy: 9,
1006 nodeDim: 40,
1007 birdOx: 19,
1008 birdOy: 21,
1009 birdDim: 21,
1010 uiDy: 45,
1011 titleDy: 30,
1012 textYOff: 20,
1013 textYSpc: 15
1014 };
1015
1016 function viewBox(dim) {
1017 return '0 0 ' + dim.w + ' ' + dim.h;
1018 }
1019
1020 function instRectAttr(dim) {
1021 var pad = instCfg.rectPad;
1022 return {
1023 x: pad,
1024 y: pad,
1025 width: dim.w - pad*2,
1026 height: dim.h - pad*2,
1027 rx: 12
1028 };
1029 }
1030
1031 function computeDim(self) {
1032 var css = window.getComputedStyle(self);
1033 return {
1034 w: stripPx(css.width),
1035 h: stripPx(css.height)
1036 };
Simon Huntb82f6902014-11-22 11:53:15 -08001037 }
Simon Hunta5e89142014-11-14 07:00:33 -08001038
1039 function updateInstances() {
1040 var onoses = oiBox.el.selectAll('.onosInst')
Simon Huntb82f6902014-11-22 11:53:15 -08001041 .data(onosOrder, function (d) { return d.id; }),
Simon Hunt7b403bc2014-11-22 19:01:00 -08001042 instDim = {w:0,h:0},
1043 c = instCfg;
Simon Hunta5e89142014-11-14 07:00:33 -08001044
Simon Hunt7b403bc2014-11-22 19:01:00 -08001045 function nSw(n) {
1046 return '# Switches: ' + n;
1047 }
Simon Hunta5e89142014-11-14 07:00:33 -08001048
Simon Huntb82f6902014-11-22 11:53:15 -08001049 // operate on existing onos instances if necessary
1050 onoses.each(function (d) {
1051 var el = d3.select(this),
1052 svg = el.select('svg');
Simon Hunt7b403bc2014-11-22 19:01:00 -08001053 instDim = computeDim(this);
Simon Huntb82f6902014-11-22 11:53:15 -08001054
1055 // update online state
1056 el.classed('online', d.online);
1057
1058 // update ui-attached state
1059 svg.select('use.uiBadge').remove();
1060 if (d.uiAttached) {
1061 attachUiBadge(svg);
1062 }
1063
Simon Hunt7b403bc2014-11-22 19:01:00 -08001064 function updAttr(id, value) {
1065 svg.select('text.instLabel.'+id).text(value);
1066 }
1067
1068 updAttr('ip', d.ip);
1069 updAttr('ns', nSw(d.switches));
Simon Huntb82f6902014-11-22 11:53:15 -08001070 });
1071
1072
1073 // operate on new onos instances
Simon Hunta5e89142014-11-14 07:00:33 -08001074 var entering = onoses.enter()
1075 .append('div')
1076 .attr('class', 'onosInst')
1077 .classed('online', function (d) { return d.online; })
Simon Hunt9c15eca2014-11-15 18:37:59 -08001078 .on('click', clickInst);
1079
Simon Huntb82f6902014-11-22 11:53:15 -08001080 entering.each(function (d) {
Simon Hunt9c15eca2014-11-15 18:37:59 -08001081 var el = d3.select(this),
Simon Hunt7b403bc2014-11-22 19:01:00 -08001082 rectAttr,
1083 svg;
1084 instDim = computeDim(this);
1085 rectAttr = instRectAttr(instDim);
Simon Hunt9c15eca2014-11-15 18:37:59 -08001086
Simon Hunt7b403bc2014-11-22 19:01:00 -08001087 svg = el.append('svg').attr({
1088 width: instDim.w,
1089 height: instDim.h,
1090 viewBox: viewBox(instDim)
Simon Hunt95908012014-11-20 10:20:26 -08001091 });
Simon Huntb82f6902014-11-22 11:53:15 -08001092
Simon Hunt7b403bc2014-11-22 19:01:00 -08001093 svg.append('rect').attr(rectAttr);
Simon Hunt9c15eca2014-11-15 18:37:59 -08001094
Simon Hunt7b403bc2014-11-22 19:01:00 -08001095 appendGlyph(svg, c.nodeOx, c.nodeOy, c.nodeDim, '#node');
1096 appendBadge(svg, c.birdOx, c.birdOy, c.birdDim, '#bird');
Simon Huntb82f6902014-11-22 11:53:15 -08001097
1098 if (d.uiAttached) {
1099 attachUiBadge(svg);
1100 }
1101
Simon Hunt7b403bc2014-11-22 19:01:00 -08001102 var left = c.nodeOx + c.nodeDim,
1103 len = rectAttr.width - left,
1104 hlen = len / 2,
1105 midline = hlen + left;
Simon Hunt9c15eca2014-11-15 18:37:59 -08001106
Simon Hunt7b403bc2014-11-22 19:01:00 -08001107 // title
1108 svg.append('text')
1109 .attr({
1110 class: 'instTitle',
1111 x: midline,
1112 y: c.titleDy
1113 })
1114 .text(d.id);
1115
1116 // a couple of attributes
1117 var ty = c.titleDy + c.textYOff;
1118
1119 function addAttr(id, label) {
1120 svg.append('text').attr({
1121 class: 'instLabel ' + id,
1122 x: midline,
1123 y: ty
1124 }).text(label);
1125 ty += c.textYSpc;
1126 }
1127
1128 addAttr('ip', d.ip);
1129 addAttr('ns', nSw(d.switches));
Simon Hunt9c15eca2014-11-15 18:37:59 -08001130 });
Simon Hunta5e89142014-11-14 07:00:33 -08001131
1132 // operate on existing + new onoses here
Simon Hunt8f40cce2014-11-23 15:57:30 -08001133 // set the affinity colors...
1134 onoses.each(function (d) {
1135 var el = d3.select(this),
1136 rect = el.select('svg').select('rect'),
1137 col = instColor(d.id, d.online);
1138 rect.style('fill', col);
1139 });
Simon Hunta5e89142014-11-14 07:00:33 -08001140
Simon Hunt7b403bc2014-11-22 19:01:00 -08001141 // adjust the panel size appropriately...
1142 oiBox.width(instDim.w * onosOrder.length);
1143 oiBox.height(instDim.h);
1144
1145 // remove any outgoing instances
1146 onoses.exit().remove();
Simon Hunta5e89142014-11-14 07:00:33 -08001147 }
1148
Simon Hunt8f40cce2014-11-23 15:57:30 -08001149 function instColor(id, online) {
1150 return cat7.get(id, !online, network.view.getTheme());
1151 }
1152
Simon Hunt9462e8c2014-11-14 17:28:09 -08001153 function clickInst(d) {
1154 var el = d3.select(this),
1155 aff = el.classed('affinity');
1156 if (!aff) {
1157 setAffinity(el, d);
1158 } else {
1159 cancelAffinity();
1160 }
1161 }
1162
1163 function setAffinity(el, d) {
1164 d3.selectAll('.onosInst')
1165 .classed('mastership', true)
1166 .classed('affinity', false);
1167 el.classed('affinity', true);
1168
1169 suppressLayers(true);
1170 node.each(function (n) {
1171 if (n.master === d.id) {
1172 n.el.classed('suppressed', false);
1173 }
1174 });
1175 oiShowMaster = true;
1176 }
1177
1178 function cancelAffinity() {
1179 d3.selectAll('.onosInst')
1180 .classed('mastership affinity', false);
1181 restoreLayerState();
1182 oiShowMaster = false;
1183 }
1184
Simon Hunt7b403bc2014-11-22 19:01:00 -08001185 // TODO: these should be moved out to utility module.
1186 function stripPx(s) {
1187 return s.replace(/px$/,'');
1188 }
1189
1190 function appendUse(svg, ox, oy, dim, iid, cls) {
1191 var use = svg.append('use').attr({
1192 transform: translate(ox,oy),
1193 'xlink:href': iid,
1194 width: dim,
1195 height: dim
1196 });
1197 if (cls) {
1198 use.classed(cls, true);
1199 }
1200 return use;
1201 }
1202
1203 function appendGlyph(svg, ox, oy, dim, iid, cls) {
1204 appendUse(svg, ox, oy, dim, iid, cls).classed('glyphIcon', true);
1205 }
1206
1207 function appendBadge(svg, ox, oy, dim, iid, cls) {
1208 appendUse(svg, ox, oy, dim, iid,cls ).classed('badgeIcon', true);
1209 }
1210
1211 function attachUiBadge(svg) {
1212 appendBadge(svg, 12, instCfg.uiDy, 30, '#uiAttached', 'uiBadge');
1213 }
1214
Simon Hunta5e89142014-11-14 07:00:33 -08001215 // ==============================
Simon Hunt99c13842014-11-06 18:23:12 -08001216 // force layout modification functions
1217
1218 function translate(x, y) {
1219 return 'translate(' + x + ',' + y + ')';
1220 }
1221
Simon Hunte2575b62014-11-18 15:25:53 -08001222 function rotate(deg) {
1223 return 'rotate(' + deg + ')';
1224 }
1225
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001226 function missMsg(what, id) {
1227 return '\n[' + what + '] "' + id + '" missing ';
1228 }
1229
1230 function linkEndPoints(srcId, dstId) {
1231 var srcNode = network.lookup[srcId],
1232 dstNode = network.lookup[dstId],
1233 sMiss = !srcNode ? missMsg('src', srcId) : '',
1234 dMiss = !dstNode ? missMsg('dst', dstId) : '';
1235
1236 if (sMiss || dMiss) {
1237 logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
1238 return null;
1239 }
1240 return {
1241 source: srcNode,
1242 target: dstNode,
1243 x1: srcNode.x,
1244 y1: srcNode.y,
1245 x2: dstNode.x,
1246 y2: dstNode.y
1247 };
1248 }
1249
Simon Hunt56d51852014-11-09 13:03:35 -08001250 function createHostLink(host) {
1251 var src = host.id,
1252 dst = host.cp.device,
Simon Hunt7cd48f32014-11-09 23:42:50 -08001253 id = host.ingress,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001254 lnk = linkEndPoints(src, dst);
Simon Hunt56d51852014-11-09 13:03:35 -08001255
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001256 if (!lnk) {
Simon Hunt56d51852014-11-09 13:03:35 -08001257 return null;
1258 }
1259
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001260 // Synthesize link ...
1261 $.extend(lnk, {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001262 key: id,
Simon Hunt56d51852014-11-09 13:03:35 -08001263 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -08001264
1265 type: function () { return 'hostLink'; },
1266 // TODO: ideally, we should see if our edge switch is online...
1267 online: function () { return true; },
1268 linkWidth: function () { return 1; }
Simon Hunt7cd48f32014-11-09 23:42:50 -08001269 });
Simon Hunt99c13842014-11-06 18:23:12 -08001270 return lnk;
1271 }
1272
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001273 function createLink(link) {
Simon Hunta6a9fe72014-11-20 11:17:12 -08001274 var lnk = linkEndPoints(link.src, link.dst);
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001275
1276 if (!lnk) {
1277 return null;
1278 }
1279
Simon Hunt8257f4c2014-11-16 19:34:54 -08001280 $.extend(lnk, {
1281 key: link.id,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001282 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -08001283 fromSource: link,
1284
1285 // functions to aggregate dual link state
1286 type: function () {
1287 var s = lnk.fromSource,
1288 t = lnk.fromTarget;
1289 return (s && s.type) || (t && t.type) || defaultLinkType;
1290 },
1291 online: function () {
1292 var s = lnk.fromSource,
1293 t = lnk.fromTarget;
1294 return (s && s.online) || (t && t.online);
1295 },
1296 linkWidth: function () {
1297 var s = lnk.fromSource,
1298 t = lnk.fromTarget,
1299 ws = (s && s.linkWidth) || 0,
1300 wt = (t && t.linkWidth) || 0;
1301 return Math.max(ws, wt);
1302 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001303 });
1304 return lnk;
Simon Hunt1a9eff92014-11-07 11:06:34 -08001305 }
1306
Simon Hunte2575b62014-11-18 15:25:53 -08001307 function removeLinkLabels() {
1308 network.links.forEach(function (d) {
1309 d.label = '';
1310 });
1311 }
1312
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001313 var widthRatio = 1.4,
1314 linkScale = d3.scale.linear()
1315 .domain([1, 12])
1316 .range([widthRatio, 12 * widthRatio])
1317 .clamp(true);
1318
Simon Hunt99c13842014-11-06 18:23:12 -08001319 function updateLinks() {
1320 link = linkG.selectAll('.link')
Simon Hunt8257f4c2014-11-16 19:34:54 -08001321 .data(network.links, function (d) { return d.key; });
Simon Hunt99c13842014-11-06 18:23:12 -08001322
1323 // operate on existing links, if necessary
1324 // link .foo() .bar() ...
1325
1326 // operate on entering links:
1327 var entering = link.enter()
1328 .append('line')
1329 .attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001330 x1: function (d) { return d.x1; },
1331 y1: function (d) { return d.y1; },
1332 x2: function (d) { return d.x2; },
1333 y2: function (d) { return d.y2; },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001334 stroke: config.topo.linkInColor,
1335 'stroke-width': config.topo.linkInWidth
Simon Hunt99c13842014-11-06 18:23:12 -08001336 });
1337
1338 // augment links
Simon Hunt7cd48f32014-11-09 23:42:50 -08001339 entering.each(function (d) {
1340 var link = d3.select(this);
1341 // provide ref to element selection from backing data....
1342 d.el = link;
Simon Hunt8257f4c2014-11-16 19:34:54 -08001343 restyleLinkElement(d);
Simon Hunt7cd48f32014-11-09 23:42:50 -08001344 });
Thomas Vachuska4830d392014-11-09 17:09:56 -08001345
1346 // operate on both existing and new links, if necessary
1347 //link .foo() .bar() ...
1348
Simon Hunte2575b62014-11-18 15:25:53 -08001349 // apply or remove labels
1350 var labelData = getLabelData();
1351 applyLinkLabels(labelData);
1352
Thomas Vachuska4830d392014-11-09 17:09:56 -08001353 // operate on exiting links:
Thomas Vachuska4830d392014-11-09 17:09:56 -08001354 link.exit()
Simon Hunt13bf9c82014-11-18 07:26:44 -08001355 .attr('stroke-dasharray', '3, 3')
1356 .style('opacity', 0.5)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001357 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -08001358 .duration(1500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001359 .attr({
Simon Hunt13bf9c82014-11-18 07:26:44 -08001360 'stroke-dasharray': '3, 12',
1361 stroke: config.topo.linkOutColor,
1362 'stroke-width': config.topo.linkOutWidth
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001363 })
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001364 .style('opacity', 0.0)
Thomas Vachuska4830d392014-11-09 17:09:56 -08001365 .remove();
Simon Hunte2575b62014-11-18 15:25:53 -08001366
1367 // NOTE: invoke a single tick to force the labels to position
1368 // onto their links.
1369 tick();
1370 }
1371
1372 function getLabelData() {
1373 // create the backing data for showing labels..
1374 var data = [];
1375 link.each(function (d) {
1376 if (d.label) {
1377 data.push({
1378 id: 'lab-' + d.key,
1379 key: d.key,
1380 label: d.label,
1381 ldata: d
1382 });
1383 }
1384 });
1385 return data;
1386 }
1387
1388 var linkLabelOffset = '0.3em';
1389
1390 function applyLinkLabels(data) {
1391 var entering;
1392
1393 linkLabel = linkLabelG.selectAll('.linkLabel')
1394 .data(data, function (d) { return d.id; });
1395
Simon Hunt56a2ea42014-11-19 12:39:31 -08001396 // for elements already existing, we need to update the text
1397 // and adjust the rectangle size to fit
1398 linkLabel.each(function (d) {
1399 var el = d3.select(this),
1400 rect = el.select('rect'),
1401 text = el.select('text');
1402 text.text(d.label);
1403 rect.attr(rectAroundText(el));
1404 });
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -08001405
Simon Hunte2575b62014-11-18 15:25:53 -08001406 entering = linkLabel.enter().append('g')
1407 .classed('linkLabel', true)
1408 .attr('id', function (d) { return d.id; });
1409
1410 entering.each(function (d) {
1411 var el = d3.select(this),
1412 rect,
1413 text,
1414 parms = {
1415 x1: d.ldata.x1,
1416 y1: d.ldata.y1,
1417 x2: d.ldata.x2,
1418 y2: d.ldata.y2
1419 };
1420
1421 d.el = el;
1422 rect = el.append('rect');
1423 text = el.append('text').text(d.label);
1424 rect.attr(rectAroundText(el));
1425 text.attr('dy', linkLabelOffset);
1426
1427 el.attr('transform', transformLabel(parms));
1428 });
1429
1430 // Remove any links that are no longer required.
1431 linkLabel.exit().remove();
1432 }
1433
1434 function rectAroundText(el) {
1435 var text = el.select('text'),
1436 box = text.node().getBBox();
1437
1438 // translate the bbox so that it is centered on [x,y]
1439 box.x = -box.width / 2;
1440 box.y = -box.height / 2;
1441
1442 // add padding
1443 box.x -= 1;
1444 box.width += 2;
1445 return box;
1446 }
1447
1448 function transformLabel(p) {
1449 var dx = p.x2 - p.x1,
1450 dy = p.y2 - p.y1,
1451 xMid = dx/2 + p.x1,
1452 yMid = dy/2 + p.y1;
Simon Hunte2575b62014-11-18 15:25:53 -08001453 return translate(xMid, yMid);
Simon Hunt99c13842014-11-06 18:23:12 -08001454 }
1455
1456 function createDeviceNode(device) {
1457 // start with the object as is
1458 var node = device,
Simon Huntbb282f52014-11-10 11:08:19 -08001459 type = device.type,
Simon Huntc72967b2014-11-20 09:21:42 -08001460 svgCls = type ? 'node device ' + type : 'node device',
1461 labels = device.labels || [];
1462
Simon Hunt99c13842014-11-06 18:23:12 -08001463 // Augment as needed...
1464 node.class = 'device';
Simon Huntbb282f52014-11-10 11:08:19 -08001465 node.svgClass = device.online ? svgCls + ' online' : svgCls;
Simon Hunt99c13842014-11-06 18:23:12 -08001466 positionNode(node);
Simon Hunt99c13842014-11-06 18:23:12 -08001467 return node;
1468 }
1469
Simon Hunt56d51852014-11-09 13:03:35 -08001470 function createHostNode(host) {
1471 // start with the object as is
1472 var node = host;
1473
1474 // Augment as needed...
1475 node.class = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001476 if (!node.type) {
Simon Hunt209155e2014-11-21 12:16:09 -08001477 node.type = 'endstation';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001478 }
Simon Hunt7fa116d2014-11-17 14:16:55 -08001479 node.svgClass = 'node host ' + node.type;
Simon Hunt56d51852014-11-09 13:03:35 -08001480 positionNode(node);
Simon Hunt56d51852014-11-09 13:03:35 -08001481 return node;
1482 }
1483
Simon Hunt62c47542014-11-22 22:16:32 -08001484 function positionNode(node, forUpdate) {
Simon Hunt99c13842014-11-06 18:23:12 -08001485 var meta = node.metaUi,
Simon Huntac9e24f2014-11-12 10:12:21 -08001486 x = meta && meta.x,
1487 y = meta && meta.y,
1488 xy;
Simon Hunt99c13842014-11-06 18:23:12 -08001489
Simon Huntac9e24f2014-11-12 10:12:21 -08001490 // If we have [x,y] already, use that...
Simon Hunt99c13842014-11-06 18:23:12 -08001491 if (x && y) {
1492 node.fixed = true;
Simon Hunt62c47542014-11-22 22:16:32 -08001493 node.px = node.x = x;
1494 node.py = node.y = y;
1495 //node.px = x;
1496 //node.py = y;
Simon Huntac9e24f2014-11-12 10:12:21 -08001497 return;
Simon Hunt99c13842014-11-06 18:23:12 -08001498 }
Simon Huntac9e24f2014-11-12 10:12:21 -08001499
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001500 var location = node.location;
1501 if (location && location.type === 'latlng') {
1502 var coord = geoMapProjection([location.lng, location.lat]);
1503 node.fixed = true;
Simon Hunt62c47542014-11-22 22:16:32 -08001504 node.px = node.x = coord[0];
1505 node.py = node.y = coord[1];
1506 //node.x = coord[0];
1507 //node.y = coord[1];
1508 return true;
1509 }
1510
1511 // if this is a node update (not a node add).. skip randomizer
1512 if (forUpdate) {
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001513 return;
1514 }
1515
Simon Huntac9e24f2014-11-12 10:12:21 -08001516 // Note: Placing incoming unpinned nodes at exactly the same point
1517 // (center of the view) causes them to explode outwards when
1518 // the force layout kicks in. So, we spread them out a bit
1519 // initially, to provide a more serene layout convergence.
1520 // Additionally, if the node is a host, we place it near
1521 // the device it is connected to.
1522
1523 function spread(s) {
1524 return Math.floor((Math.random() * s) - s/2);
1525 }
1526
1527 function randDim(dim) {
1528 return dim / 2 + spread(dim * 0.7071);
1529 }
1530
1531 function rand() {
1532 return {
1533 x: randDim(network.view.width()),
1534 y: randDim(network.view.height())
1535 };
1536 }
1537
1538 function near(node) {
1539 var min = 12,
1540 dx = spread(12),
1541 dy = spread(12);
1542 return {
1543 x: node.x + min + dx,
1544 y: node.y + min + dy
1545 };
1546 }
1547
1548 function getDevice(cp) {
1549 var d = network.lookup[cp.device];
1550 return d || rand();
1551 }
1552
1553 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
1554 $.extend(node, xy);
Simon Hunt99c13842014-11-06 18:23:12 -08001555 }
1556
Simon Hunt99c13842014-11-06 18:23:12 -08001557
Simon Huntc72967b2014-11-20 09:21:42 -08001558 function iconGlyphUrl(d) {
1559 var which = d.type || 'unknown';
1560 return '#' + which;
1561 }
1562
Simon Hunt99c13842014-11-06 18:23:12 -08001563 // returns the newly computed bounding box of the rectangle
1564 function adjustRectToFitText(n) {
1565 var text = n.select('text'),
1566 box = text.node().getBBox(),
1567 lab = config.labels;
1568
1569 text.attr('text-anchor', 'middle')
1570 .attr('y', '-0.8em')
1571 .attr('x', lab.imgPad/2);
1572
1573 // translate the bbox so that it is centered on [x,y]
1574 box.x = -box.width / 2;
1575 box.y = -box.height / 2;
1576
1577 // add padding
1578 box.x -= (lab.padLR + lab.imgPad/2);
1579 box.width += lab.padLR * 2 + lab.imgPad;
1580 box.y -= lab.padTB;
1581 box.height += lab.padTB * 2;
1582
1583 return box;
1584 }
1585
Simon Hunt1a9eff92014-11-07 11:06:34 -08001586 function mkSvgClass(d) {
1587 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
1588 }
1589
Simon Hunt7cd48f32014-11-09 23:42:50 -08001590 function hostLabel(d) {
1591 var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0;
1592 return d.labels[idx];
1593 }
1594 function deviceLabel(d) {
1595 var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0;
1596 return d.labels[idx];
1597 }
Simon Huntc72967b2014-11-20 09:21:42 -08001598 function trimLabel(label) {
1599 return (label && label.trim()) || '';
1600 }
1601
1602 function emptyBox() {
1603 return {
1604 x: -2,
1605 y: -2,
1606 width: 4,
1607 height: 4
1608 };
Simon Hunt7cd48f32014-11-09 23:42:50 -08001609 }
1610
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001611 function updateDeviceLabel(d) {
Simon Huntc72967b2014-11-20 09:21:42 -08001612 var label = trimLabel(deviceLabel(d)),
1613 noLabel = !label,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001614 node = d.el,
Simon Huntc72967b2014-11-20 09:21:42 -08001615 box,
1616 dx,
Simon Hunt395a70c2014-11-22 23:17:40 -08001617 dy,
1618 cfg = config.icons.device;
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001619
1620 node.select('text')
1621 .text(label)
1622 .style('opacity', 0)
1623 .transition()
1624 .style('opacity', 1);
1625
Simon Huntc72967b2014-11-20 09:21:42 -08001626 if (noLabel) {
1627 box = emptyBox();
Simon Hunt395a70c2014-11-22 23:17:40 -08001628 dx = -cfg.dim/2;
1629 dy = -cfg.dim/2;
Simon Huntc72967b2014-11-20 09:21:42 -08001630 } else {
1631 box = adjustRectToFitText(node);
Simon Hunt395a70c2014-11-22 23:17:40 -08001632 dx = box.x + cfg.xoff;
1633 dy = box.y + cfg.yoff;
Simon Huntc72967b2014-11-20 09:21:42 -08001634 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001635
1636 node.select('rect')
1637 .transition()
1638 .attr(box);
1639
Simon Huntc72967b2014-11-20 09:21:42 -08001640 node.select('g.deviceIcon')
Thomas Vachuska89543292014-11-19 11:28:33 -08001641 .transition()
Simon Huntc72967b2014-11-20 09:21:42 -08001642 .attr('transform', translate(dx, dy));
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001643 }
1644
1645 function updateHostLabel(d) {
1646 var label = hostLabel(d),
1647 host = d.el;
1648
1649 host.select('text').text(label);
1650 }
1651
Simon Hunt62c47542014-11-22 22:16:32 -08001652 // FIXME : fold this into updateNodes.
Simon Huntbb282f52014-11-10 11:08:19 -08001653 function updateHostState(hostData) {
1654 updateHostLabel(hostData);
Simon Huntbb282f52014-11-10 11:08:19 -08001655 }
1656
Simon Hunt6ac93f32014-11-13 12:17:27 -08001657 function nodeMouseOver(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001658 hovered = d;
Thomas Vachuska9edca302014-11-22 17:06:42 -08001659 requestTrafficForMode();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001660 }
1661
1662 function nodeMouseOut(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001663 hovered = null;
Thomas Vachuska9edca302014-11-22 17:06:42 -08001664 requestTrafficForMode();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001665 }
Simon Huntbb282f52014-11-10 11:08:19 -08001666
Simon Hunteb1514d2014-11-20 09:57:29 -08001667 function addHostIcon(node, radius, iid) {
Thomas Vachuska89543292014-11-19 11:28:33 -08001668 var dim = radius * 1.5,
1669 xlate = -dim / 2;
1670
Simon Hunteb1514d2014-11-20 09:57:29 -08001671 node.append('use').attr({
1672 class: 'glyphIcon hostIcon',
1673 transform: translate(xlate,xlate),
1674 'xlink:href': iid,
1675 width: dim,
1676 height: dim
1677 });
Thomas Vachuska89543292014-11-19 11:28:33 -08001678 }
1679
Simon Hunt99c13842014-11-06 18:23:12 -08001680 function updateNodes() {
1681 node = nodeG.selectAll('.node')
1682 .data(network.nodes, function (d) { return d.id; });
1683
Simon Hunt62c47542014-11-22 22:16:32 -08001684 // operate on existing nodes...
1685 node.filter('.device').each(function (d) {
1686 //var node = d3.select(this);
1687 var node = d.el;
1688 node.classed('online', d.online);
1689 updateDeviceLabel(d);
1690 positionNode(d, true);
1691 });
1692
1693 node.filter('.host').each(function (d) {
1694 //var node = d3.select(this);
1695 var node = d.el;
1696 // TODO: appropriate update of host visuals
1697 });
Simon Hunt99c13842014-11-06 18:23:12 -08001698
1699 // operate on entering nodes:
1700 var entering = node.enter()
1701 .append('g')
1702 .attr({
1703 id: function (d) { return safeId(d.id); },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001704 class: mkSvgClass,
Simon Hunt99c13842014-11-06 18:23:12 -08001705 transform: function (d) { return translate(d.x, d.y); },
1706 opacity: 0
1707 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08001708 .call(network.drag)
Simon Hunt6ac93f32014-11-13 12:17:27 -08001709 .on('mouseover', nodeMouseOver)
1710 .on('mouseout', nodeMouseOut)
Simon Hunt99c13842014-11-06 18:23:12 -08001711 .transition()
1712 .attr('opacity', 1);
1713
1714 // augment device nodes...
1715 entering.filter('.device').each(function (d) {
1716 var node = d3.select(this),
Simon Huntc72967b2014-11-20 09:21:42 -08001717 label = trimLabel(deviceLabel(d)),
1718 noLabel = !label,
Simon Hunt99c13842014-11-06 18:23:12 -08001719 box;
1720
Simon Hunt7cd48f32014-11-09 23:42:50 -08001721 // provide ref to element from backing data....
1722 d.el = node;
1723
Simon Hunt62c47542014-11-22 22:16:32 -08001724 node.append('rect').attr({ rx: 5, ry: 5 });
1725 node.append('text').text(label).attr('dy', '1.1em');
Simon Hunt99c13842014-11-06 18:23:12 -08001726 box = adjustRectToFitText(node);
Simon Hunta3dd9572014-11-20 15:22:41 -08001727 node.select('rect').attr(box);
Simon Huntc72967b2014-11-20 09:21:42 -08001728 addDeviceIcon(node, box, noLabel, iconGlyphUrl(d));
Simon Huntc7ee0662014-11-05 16:44:37 -08001729 });
Simon Hunt934c3ce2014-11-05 11:45:07 -08001730
Thomas Vachuska89543292014-11-19 11:28:33 -08001731
Simon Hunt56d51852014-11-09 13:03:35 -08001732 // augment host nodes...
1733 entering.filter('.host').each(function (d) {
1734 var node = d3.select(this),
Simon Hunt395a70c2014-11-22 23:17:40 -08001735 cfg = config.icons.host,
1736 r = cfg.radius[d.type] || cfg.defaultRadius,
Thomas Vachuska89543292014-11-19 11:28:33 -08001737 textDy = r + 10,
Simon Hunteb1514d2014-11-20 09:57:29 -08001738 iid = iconGlyphUrl(d);
Simon Hunt56d51852014-11-09 13:03:35 -08001739
Simon Hunt7cd48f32014-11-09 23:42:50 -08001740 // provide ref to element from backing data....
1741 d.el = node;
1742
Simon Hunt62c47542014-11-22 22:16:32 -08001743 node.append('circle').attr('r', r);
Simon Hunt7fa116d2014-11-17 14:16:55 -08001744
Simon Hunteb1514d2014-11-20 09:57:29 -08001745 if (iid) {
1746 addHostIcon(node, r, iid);
Simon Hunt7fa116d2014-11-17 14:16:55 -08001747 }
Simon Hunt56d51852014-11-09 13:03:35 -08001748
Simon Hunt56d51852014-11-09 13:03:35 -08001749 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001750 .text(hostLabel)
Thomas Vachuska89543292014-11-19 11:28:33 -08001751 .attr('dy', textDy)
Simon Hunt7cd48f32014-11-09 23:42:50 -08001752 .attr('text-anchor', 'middle');
Simon Hunt56d51852014-11-09 13:03:35 -08001753 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001754
Simon Hunt99c13842014-11-06 18:23:12 -08001755 // operate on both existing and new nodes, if necessary
1756 //node .foo() .bar() ...
Simon Huntc7ee0662014-11-05 16:44:37 -08001757
Simon Hunt99c13842014-11-06 18:23:12 -08001758 // operate on exiting nodes:
Simon Huntea80eb42014-11-11 13:46:57 -08001759 // Note that the node is removed after 2 seconds.
1760 // Sub element animations should be shorter than 2 seconds.
1761 var exiting = node.exit()
Simon Hunt44031102014-11-11 13:20:36 -08001762 .transition()
1763 .duration(2000)
Simon Huntea80eb42014-11-11 13:46:57 -08001764 .style('opacity', 0)
Simon Hunt99c13842014-11-06 18:23:12 -08001765 .remove();
Simon Huntea80eb42014-11-11 13:46:57 -08001766
1767 // host node exits....
1768 exiting.filter('.host').each(function (d) {
1769 var node = d3.select(this);
1770
1771 node.select('text')
1772 .style('opacity', 0.5)
1773 .transition()
1774 .duration(1000)
1775 .style('opacity', 0);
1776 // note, leave <g>.remove to remove this element
1777
Thomas Vachuska89543292014-11-19 11:28:33 -08001778 node.select('circle')
1779 .style('stroke-fill', '#555')
1780 .style('fill', '#888')
Simon Huntea80eb42014-11-11 13:46:57 -08001781 .style('opacity', 0.5)
1782 .transition()
1783 .duration(1500)
1784 .attr('r', 0);
1785 // note, leave <g>.remove to remove this element
1786
1787 });
1788
Simon Hunt62c47542014-11-22 22:16:32 -08001789 // TODO: device node exit animation
1790
1791 network.force.resume();
Simon Huntc7ee0662014-11-05 16:44:37 -08001792 }
1793
Simon Huntc72967b2014-11-20 09:21:42 -08001794 function addDeviceIcon(node, box, noLabel, iid) {
1795 var cfg = config.icons.device,
1796 dx,
1797 dy,
1798 g;
1799
1800 if (noLabel) {
1801 box = emptyBox();
1802 dx = -cfg.dim/2;
1803 dy = -cfg.dim/2;
1804 } else {
1805 box = adjustRectToFitText(node);
Simon Hunt395a70c2014-11-22 23:17:40 -08001806 dx = box.x + cfg.xoff;
1807 dy = box.y + cfg.yoff;
Simon Huntc72967b2014-11-20 09:21:42 -08001808 }
1809
Simon Hunteb1514d2014-11-20 09:57:29 -08001810 g = node.append('g')
1811 .attr('class', 'glyphIcon deviceIcon')
Simon Huntc72967b2014-11-20 09:21:42 -08001812 .attr('transform', translate(dx, dy));
1813
1814 g.append('rect').attr({
1815 x: 0,
1816 y: 0,
1817 rx: cfg.rx,
1818 width: cfg.dim,
1819 height: cfg.dim
1820 });
1821
1822 g.append('use').attr({
1823 'xlink:href': iid,
1824 width: cfg.dim,
1825 height: cfg.dim
1826 });
1827
Simon Huntc72967b2014-11-20 09:21:42 -08001828 }
1829
Simon Hunt7b403bc2014-11-22 19:01:00 -08001830 function find(key, array, tag) {
1831 var _tag = tag || 'key',
1832 idx, n, d;
1833 for (idx = 0, n = array.length; idx < n; idx++) {
1834 d = array[idx];
1835 if (d[_tag] === key) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001836 return idx;
1837 }
1838 }
1839 return -1;
1840 }
1841
1842 function removeLinkElement(linkData) {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001843 var idx = find(linkData.key, network.links),
1844 removed;
1845 if (idx >=0) {
1846 // remove from links array
1847 removed = network.links.splice(idx, 1);
1848 // remove from lookup cache
1849 delete network.lookup[removed[0].key];
1850 updateLinks();
1851 network.force.resume();
1852 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001853 }
Simon Huntc7ee0662014-11-05 16:44:37 -08001854
Simon Hunt44031102014-11-11 13:20:36 -08001855 function removeHostElement(hostData) {
1856 // first, remove associated hostLink...
1857 removeLinkElement(hostData.linkData);
1858
1859 // remove from lookup cache
1860 delete network.lookup[hostData.id];
1861 // remove from nodes array
1862 var idx = find(hostData.id, network.nodes);
1863 network.nodes.splice(idx, 1);
1864 // remove from SVG
1865 updateNodes();
1866 network.force.resume();
1867 }
1868
1869
Simon Huntc7ee0662014-11-05 16:44:37 -08001870 function tick() {
1871 node.attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001872 transform: function (d) { return translate(d.x, d.y); }
Simon Huntc7ee0662014-11-05 16:44:37 -08001873 });
1874
1875 link.attr({
1876 x1: function (d) { return d.source.x; },
1877 y1: function (d) { return d.source.y; },
1878 x2: function (d) { return d.target.x; },
1879 y2: function (d) { return d.target.y; }
1880 });
Simon Hunte2575b62014-11-18 15:25:53 -08001881
1882 linkLabel.each(function (d) {
1883 var el = d3.select(this);
Thomas Vachuska4731f122014-11-20 04:56:19 -08001884 var lnk = findLinkById(d.key);
1885
1886 if (lnk) {
1887 var parms = {
Simon Hunte2575b62014-11-18 15:25:53 -08001888 x1: lnk.source.x,
1889 y1: lnk.source.y,
1890 x2: lnk.target.x,
1891 y2: lnk.target.y
1892 };
Thomas Vachuska4731f122014-11-20 04:56:19 -08001893 el.attr('transform', transformLabel(parms));
1894 }
Simon Hunte2575b62014-11-18 15:25:53 -08001895 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001896 }
Simon Hunt934c3ce2014-11-05 11:45:07 -08001897
1898 // ==============================
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001899 // Web-Socket for live data
1900
1901 function webSockUrl() {
1902 return document.location.toString()
1903 .replace(/\#.*/, '')
1904 .replace('http://', 'ws://')
1905 .replace('https://', 'wss://')
1906 .replace('index2.html', config.webSockUrl);
1907 }
1908
1909 webSock = {
1910 ws : null,
1911
1912 connect : function() {
1913 webSock.ws = new WebSocket(webSockUrl());
1914
1915 webSock.ws.onopen = function() {
Simon Hunt0c6d4192014-11-12 12:07:10 -08001916 noWebSock(false);
Thomas Vachuska47635c62014-11-22 01:21:36 -08001917 requestSummary();
1918 oiBox.show();
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001919 };
1920
1921 webSock.ws.onmessage = function(m) {
1922 if (m.data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001923 wsTraceRx(m.data);
Thomas Vachuskad472c6e2014-11-07 19:11:05 -08001924 handleServerEvent(JSON.parse(m.data));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001925 }
1926 };
1927
1928 webSock.ws.onclose = function(m) {
1929 webSock.ws = null;
Simon Hunt0c6d4192014-11-12 12:07:10 -08001930 noWebSock(true);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001931 };
1932 },
1933
1934 send : function(text) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001935 if (text != null) {
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001936 webSock._send(text);
1937 }
1938 },
1939
1940 _send : function(message) {
1941 if (webSock.ws) {
1942 webSock.ws.send(message);
Simon Hunta255a2c2014-11-13 22:29:35 -08001943 } else if (config.useLiveData) {
Simon Hunt56d51852014-11-09 13:03:35 -08001944 network.view.alert('no web socket open\n\n' + message);
Simon Hunta255a2c2014-11-13 22:29:35 -08001945 } else {
1946 console.log('WS Send: ' + JSON.stringify(message));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001947 }
1948 }
1949
1950 };
1951
Simon Hunt0c6d4192014-11-12 12:07:10 -08001952 function noWebSock(b) {
1953 mask.style('display',b ? 'block' : 'none');
1954 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001955
1956 function sendMessage(evType, payload) {
1957 var toSend = {
Simon Huntbb282f52014-11-10 11:08:19 -08001958 event: evType,
1959 sid: ++sid,
1960 payload: payload
1961 },
1962 asText = JSON.stringify(toSend);
1963 wsTraceTx(asText);
1964 webSock.send(asText);
Simon Huntc76ae892014-11-18 17:31:51 -08001965
1966 // Temporary measure for debugging UI behavior ...
1967 if (!config.useLiveData) {
1968 handleTestSend(toSend);
1969 }
Simon Huntbb282f52014-11-10 11:08:19 -08001970 }
1971
1972 function wsTraceTx(msg) {
1973 wsTrace('tx', msg);
1974 }
1975 function wsTraceRx(msg) {
1976 wsTrace('rx', msg);
1977 }
1978 function wsTrace(rxtx, msg) {
Simon Huntbb282f52014-11-10 11:08:19 -08001979 console.log('[' + rxtx + '] ' + msg);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001980 }
1981
Simon Huntc76ae892014-11-18 17:31:51 -08001982 // NOTE: Temporary hardcoded example for showing detail pane
1983 // while we fine-
1984 // Probably should not merge this change...
1985 function handleTestSend(msg) {
1986 if (msg.event === 'requestDetails') {
1987 showDetails({
1988 event: 'showDetails',
1989 sid: 1001,
1990 payload: {
1991 "id": "of:0000ffffffffff09",
1992 "type": "roadm",
1993 "propOrder": [
1994 "Name",
1995 "Vendor",
1996 "H/W Version",
1997 "S/W Version",
1998 "-",
1999 "Latitude",
2000 "Longitude",
2001 "Ports"
2002 ],
2003 "props": {
2004 "Name": null,
2005 "Vendor": "Linc",
2006 "H/W Version": "OE",
2007 "S/W Version": "?",
2008 "-": "",
2009 "Latitude": "40.8",
2010 "Longitude": "73.1",
2011 "Ports": "2"
2012 }
2013 }
2014 });
2015 }
2016 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002017
2018 // ==============================
2019 // Selection stuff
2020
2021 function selectObject(obj, el) {
2022 var n,
Simon Hunt01095ff2014-11-13 16:37:29 -08002023 srcEv = d3.event.sourceEvent,
2024 meta = srcEv.metaKey,
2025 shift = srcEv.shiftKey;
2026
Simon Huntdeab4322014-11-13 18:49:07 -08002027 if ((panZoom() && !meta) || (!panZoom() && meta)) {
Simon Hunt01095ff2014-11-13 16:37:29 -08002028 return;
2029 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002030
2031 if (el) {
2032 n = d3.select(el);
2033 } else {
2034 node.each(function(d) {
2035 if (d == obj) {
2036 n = d3.select(el = this);
2037 }
2038 });
2039 }
2040 if (!n) return;
2041
Simon Hunt01095ff2014-11-13 16:37:29 -08002042 if (shift && n.classed('selected')) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002043 deselectObject(obj.id);
Simon Hunt61d04042014-11-11 17:27:16 -08002044 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002045 return;
2046 }
2047
Simon Hunt01095ff2014-11-13 16:37:29 -08002048 if (!shift) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002049 deselectAll();
2050 }
2051
Simon Huntc31d5692014-11-12 13:27:18 -08002052 selections[obj.id] = { obj: obj, el: el };
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002053 selectOrder.push(obj.id);
2054
2055 n.classed('selected', true);
Simon Hunt61d04042014-11-11 17:27:16 -08002056 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002057 }
2058
2059 function deselectObject(id) {
Simon Huntc31d5692014-11-12 13:27:18 -08002060 var obj = selections[id],
2061 idx;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002062 if (obj) {
2063 d3.select(obj.el).classed('selected', false);
Simon Hunt61d04042014-11-11 17:27:16 -08002064 delete selections[id];
Simon Huntc31d5692014-11-12 13:27:18 -08002065 idx = $.inArray(id, selectOrder);
2066 if (idx >= 0) {
2067 selectOrder.splice(idx, 1);
2068 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002069 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002070 }
2071
2072 function deselectAll() {
2073 // deselect all nodes in the network...
2074 node.classed('selected', false);
2075 selections = {};
2076 selectOrder = [];
Simon Hunt61d04042014-11-11 17:27:16 -08002077 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002078 }
2079
Thomas Vachuska47635c62014-11-22 01:21:36 -08002080 // update the state of the sumary pane
2081 function updateSummaryPane() {
2082
2083 }
2084
Simon Hunt61d04042014-11-11 17:27:16 -08002085 // update the state of the detail pane, based on current selections
2086 function updateDetailPane() {
2087 var nSel = selectOrder.length;
2088 if (!nSel) {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002089 emptySelect();
Simon Hunt61d04042014-11-11 17:27:16 -08002090 } else if (nSel === 1) {
2091 singleSelect();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002092 requestTrafficForMode();
Simon Hunt61d04042014-11-11 17:27:16 -08002093 } else {
2094 multiSelect();
2095 }
2096 }
2097
Thomas Vachuska9edca302014-11-22 17:06:42 -08002098 function emptySelect() {
2099 detailPane.hide();
2100 cancelTraffic();
2101 }
2102
Simon Hunt61d04042014-11-11 17:27:16 -08002103 function singleSelect() {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002104 // NOTE: detail is shown from showDetails event callback
Simon Hunt61d04042014-11-11 17:27:16 -08002105 requestDetails();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002106 requestTrafficForMode();
Simon Hunt61d04042014-11-11 17:27:16 -08002107 }
2108
2109 function multiSelect() {
Simon Huntb53e0682014-11-12 13:32:01 -08002110 populateMultiSelect();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002111 requestTrafficForMode();
Simon Huntb53e0682014-11-12 13:32:01 -08002112 }
2113
2114 function addSep(tbody) {
2115 var tr = tbody.append('tr');
2116 $('<hr>').appendTo(tr.append('td').attr('colspan', 2));
2117 }
2118
2119 function addProp(tbody, label, value) {
2120 var tr = tbody.append('tr');
2121
2122 tr.append('td')
2123 .attr('class', 'label')
2124 .text(label + ' :');
2125
2126 tr.append('td')
2127 .attr('class', 'value')
2128 .text(value);
2129 }
2130
2131 function populateMultiSelect() {
2132 detailPane.empty();
2133
Simon Hunta3dd9572014-11-20 15:22:41 -08002134 var title = detailPane.append('h3'),
2135 table = detailPane.append('table'),
2136 tbody = table.append('tbody');
Simon Huntb53e0682014-11-12 13:32:01 -08002137
Thomas Vachuska4731f122014-11-20 04:56:19 -08002138 title.text('Selected Nodes');
Simon Huntb53e0682014-11-12 13:32:01 -08002139
2140 selectOrder.forEach(function (d, i) {
2141 addProp(tbody, i+1, d);
2142 });
Simon Huntd72bc702014-11-13 18:38:04 -08002143
2144 addMultiSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08002145 }
2146
Thomas Vachuska47635c62014-11-22 01:21:36 -08002147 // TODO: refactor to consolidate with populateDetails
2148 function populateSummary(data) {
2149 summaryPane.empty();
2150
2151 var svg = summaryPane.append('svg'),
2152 iid = iconGlyphUrl(data);
2153
2154 var title = summaryPane.append('h2'),
2155 table = summaryPane.append('table'),
2156 tbody = table.append('tbody');
2157
2158 appendGlyph(svg, 0, 0, 40, iid);
2159
2160 svg.append('use')
2161 .attr({
2162 class: 'birdBadge',
2163 transform: translate(8,12),
2164 'xlink:href': '#bird',
2165 width: 24,
2166 height: 24,
2167 fill: '#fff'
2168 });
2169
2170 title.text('ONOS Summary');
2171
2172 data.propOrder.forEach(function(p) {
2173 if (p === '-') {
2174 addSep(tbody);
2175 } else {
2176 addProp(tbody, p, data.props[p]);
2177 }
2178 });
2179 }
2180
Simon Hunt61d04042014-11-11 17:27:16 -08002181 function populateDetails(data) {
2182 detailPane.empty();
2183
Simon Hunta6a9fe72014-11-20 11:17:12 -08002184 var svg = detailPane.append('svg'),
2185 iid = iconGlyphUrl(data);
2186
Simon Hunta3dd9572014-11-20 15:22:41 -08002187 var title = detailPane.append('h2'),
2188 table = detailPane.append('table'),
2189 tbody = table.append('tbody');
Simon Hunt61d04042014-11-11 17:27:16 -08002190
Simon Hunta6a9fe72014-11-20 11:17:12 -08002191 appendGlyph(svg, 0, 0, 40, iid);
2192 title.text(data.id);
Simon Hunt61d04042014-11-11 17:27:16 -08002193
2194 data.propOrder.forEach(function(p) {
2195 if (p === '-') {
2196 addSep(tbody);
2197 } else {
2198 addProp(tbody, p, data.props[p]);
2199 }
2200 });
Simon Huntd72bc702014-11-13 18:38:04 -08002201
Thomas Vachuska4731f122014-11-20 04:56:19 -08002202 addSingleSelectActions(data);
Simon Hunt61d04042014-11-11 17:27:16 -08002203 }
2204
Thomas Vachuska4731f122014-11-20 04:56:19 -08002205 function addSingleSelectActions(data) {
Simon Huntd72bc702014-11-13 18:38:04 -08002206 detailPane.append('hr');
2207 // always want to allow 'show traffic'
Thomas Vachuska4731f122014-11-20 04:56:19 -08002208 addAction(detailPane, 'Show Related Traffic', showTrafficAction);
2209
2210 if (data.type === 'switch') {
2211 addAction(detailPane, 'Show Device Flows', showDeviceLinkFlowsAction);
2212 }
Simon Huntd72bc702014-11-13 18:38:04 -08002213 }
2214
2215 function addMultiSelectActions() {
2216 detailPane.append('hr');
2217 // always want to allow 'show traffic'
Thomas Vachuska4731f122014-11-20 04:56:19 -08002218 addAction(detailPane, 'Show Related Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08002219 // if exactly two hosts are selected, also want 'add host intent'
2220 if (nSel() === 2 && allSelectionsClass('host')) {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002221 addAction(detailPane, 'Create Host-to-Host Flow', addHostIntentAction);
2222 } else if (nSel() >= 2 && allSelectionsClass('host')) {
2223 addAction(detailPane, 'Create Multi-Source Flow', addMultiSourceIntentAction);
Simon Huntd72bc702014-11-13 18:38:04 -08002224 }
2225 }
2226
Simon Hunta5e89142014-11-14 07:00:33 -08002227 function addAction(panel, text, cb) {
2228 panel.append('div')
Simon Huntd72bc702014-11-13 18:38:04 -08002229 .classed('actionBtn', true)
2230 .text(text)
2231 .on('click', cb);
2232 }
2233
2234
Paul Greysonfcba0e82014-11-13 10:21:16 -08002235 function zoomPan(scale, translate) {
2236 zoomPanContainer.attr("transform", "translate(" + translate + ")scale(" + scale + ")");
2237 // keep the map lines constant width while zooming
Thomas Vachuska89543292014-11-19 11:28:33 -08002238 bgImg.style("stroke-width", 2.0 / scale + "px");
Paul Greysonfcba0e82014-11-13 10:21:16 -08002239 }
2240
2241 function resetZoomPan() {
2242 zoomPan(1, [0,0]);
2243 zoom.scale(1).translate([0,0]);
2244 }
2245
2246 function setupZoomPan() {
2247 function zoomed() {
Simon Huntdeab4322014-11-13 18:49:07 -08002248 if (!panZoom() ^ !d3.event.sourceEvent.metaKey) {
Paul Greysonfcba0e82014-11-13 10:21:16 -08002249 zoomPan(d3.event.scale, d3.event.translate);
2250 }
2251 }
2252
2253 zoom = d3.behavior.zoom()
2254 .translate([0, 0])
2255 .scale(1)
2256 .scaleExtent([1, 8])
2257 .on("zoom", zoomed);
2258
2259 svg.call(zoom);
2260 }
2261
Simon Hunt61d04042014-11-11 17:27:16 -08002262 // ==============================
2263 // Test harness code
Simon Hunt56d51852014-11-09 13:03:35 -08002264
2265 function prepareScenario(view, ctx, dbg) {
2266 var sc = scenario,
2267 urlSc = sc.evDir + ctx + sc.evScenario;
2268
2269 if (!ctx) {
2270 view.alert("No scenario specified (null ctx)");
2271 return;
2272 }
2273
2274 sc.view = view;
2275 sc.ctx = ctx;
2276 sc.debug = dbg;
2277 sc.evNumber = 0;
2278
2279 d3.json(urlSc, function(err, data) {
Simon Huntbb282f52014-11-10 11:08:19 -08002280 var p = data && data.params || {},
2281 desc = data && data.description || null,
Simon Huntfc274c92014-11-11 11:05:46 -08002282 intro = data && data.title;
Simon Huntbb282f52014-11-10 11:08:19 -08002283
Simon Hunt56d51852014-11-09 13:03:35 -08002284 if (err) {
2285 view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err);
2286 } else {
2287 sc.params = p;
Simon Huntbb282f52014-11-10 11:08:19 -08002288 if (desc) {
2289 intro += '\n\n ' + desc.join('\n ');
2290 }
2291 view.alert(intro);
Simon Hunt56d51852014-11-09 13:03:35 -08002292 }
2293 });
2294
2295 }
2296
Simon Hunt01095ff2014-11-13 16:37:29 -08002297 // ==============================
2298 // Toggle Buttons in masthead
Simon Hunt0c6d4192014-11-12 12:07:10 -08002299
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002300 // TODO: toggle button (and other widgets in the masthead) should be provided
2301 // by the framework; not generated by the view.
2302
Thomas Vachuska47635c62014-11-22 01:21:36 -08002303 //var showInstances;
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002304
2305 function addButtonBar(view) {
2306 var bb = d3.select('#mast')
2307 .append('span').classed('right', true).attr('id', 'bb');
2308
Simon Hunta5e89142014-11-14 07:00:33 -08002309 function mkTogBtn(text, cb) {
2310 return bb.append('span')
2311 .classed('btn', true)
2312 .text(text)
2313 .on('click', cb);
2314 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002315
Thomas Vachuska47635c62014-11-22 01:21:36 -08002316 //showInstances = mkTogBtn('Show Instances', toggleInst);
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002317 }
2318
Simon Huntdeab4322014-11-13 18:49:07 -08002319 function panZoom() {
Simon Hunte5b71752014-11-18 20:06:07 -08002320 return false;
Simon Hunta5e89142014-11-14 07:00:33 -08002321 }
2322
Simon Hunt7fa116d2014-11-17 14:16:55 -08002323 function loadGlyphs(svg) {
2324 var defs = svg.append('defs');
2325 gly.defBird(defs);
Simon Huntc72967b2014-11-20 09:21:42 -08002326 gly.defGlyphs(defs);
Simon Huntb82f6902014-11-22 11:53:15 -08002327 gly.defBadges(defs);
Simon Hunt7fa116d2014-11-17 14:16:55 -08002328 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002329
Simon Hunt395a70c2014-11-22 23:17:40 -08002330 function sendUpdateMeta(d, store) {
2331 var metaUi = {},
2332 ll;
2333
2334 if (store) {
2335 ll = geoMapProjection.invert([d.x, d.y]);
Simon Hunt62c47542014-11-22 22:16:32 -08002336 metaUi = {
2337 x: d.x,
2338 y: d.y,
2339 lng: ll[0],
2340 lat: ll[1]
2341 };
Simon Hunt395a70c2014-11-22 23:17:40 -08002342 }
Simon Hunt62c47542014-11-22 22:16:32 -08002343 d.metaUi = metaUi;
2344 sendMessage('updateMeta', {
2345 id: d.id,
2346 'class': d.class,
2347 'memento': metaUi
2348 });
2349 }
2350
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002351 // ==============================
Simon Hunt142d0032014-11-04 20:13:09 -08002352 // View life-cycle callbacks
Simon Hunt195cb382014-11-03 17:50:51 -08002353
Simon Huntf67722a2014-11-10 09:32:06 -08002354 function preload(view, ctx, flags) {
Simon Hunt142d0032014-11-04 20:13:09 -08002355 var w = view.width(),
2356 h = view.height(),
Simon Huntc7ee0662014-11-05 16:44:37 -08002357 fcfg = config.force,
2358 fpad = fcfg.pad,
2359 forceDim = [w - 2*fpad, h - 2*fpad];
Simon Hunt195cb382014-11-03 17:50:51 -08002360
Simon Hunt142d0032014-11-04 20:13:09 -08002361 // NOTE: view.$div is a D3 selection of the view's div
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002362 var viewBox = '0 0 ' + config.logicalSize + ' ' + config.logicalSize;
2363 svg = view.$div.append('svg').attr('viewBox', viewBox);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002364 setSize(svg, view);
2365
Simon Hunt7fa116d2014-11-17 14:16:55 -08002366 loadGlyphs(svg);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002367
Paul Greysonfcba0e82014-11-13 10:21:16 -08002368 zoomPanContainer = svg.append('g').attr('id', 'zoomPanContainer');
Paul Greysonfcba0e82014-11-13 10:21:16 -08002369 setupZoomPan();
2370
Simon Hunt1a9eff92014-11-07 11:06:34 -08002371 // add blue glow filter to svg layer
Paul Greysonfcba0e82014-11-13 10:21:16 -08002372 d3u.appendGlow(zoomPanContainer);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002373
Simon Huntc7ee0662014-11-05 16:44:37 -08002374 // group for the topology
Paul Greysonfcba0e82014-11-13 10:21:16 -08002375 topoG = zoomPanContainer.append('g')
Simon Huntd3b7d512014-11-12 15:48:41 -08002376 .attr('id', 'topo-G')
Simon Huntc7ee0662014-11-05 16:44:37 -08002377 .attr('transform', fcfg.translate());
2378
Simon Hunte2575b62014-11-18 15:25:53 -08002379 // subgroups for links, link labels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002380 linkG = topoG.append('g').attr('id', 'links');
Simon Hunte2575b62014-11-18 15:25:53 -08002381 linkLabelG = topoG.append('g').attr('id', 'linkLabels');
Simon Huntc7ee0662014-11-05 16:44:37 -08002382 nodeG = topoG.append('g').attr('id', 'nodes');
2383
Simon Hunte2575b62014-11-18 15:25:53 -08002384 // selection of links, linkLabels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002385 link = linkG.selectAll('.link');
Simon Hunte2575b62014-11-18 15:25:53 -08002386 linkLabel = linkLabelG.selectAll('.linkLabel');
Simon Huntc7ee0662014-11-05 16:44:37 -08002387 node = nodeG.selectAll('.node');
2388
Simon Hunt7cd48f32014-11-09 23:42:50 -08002389 function chrg(d) {
2390 return fcfg.charge[d.class] || -12000;
2391 }
Simon Hunt99c13842014-11-06 18:23:12 -08002392 function ldist(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002393 return fcfg.linkDistance[d.type] || 50;
Simon Hunt99c13842014-11-06 18:23:12 -08002394 }
2395 function lstrg(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002396 // 0.0 - 1.0
2397 return fcfg.linkStrength[d.type] || 1.0;
Simon Hunt99c13842014-11-06 18:23:12 -08002398 }
2399
Simon Hunt1a9eff92014-11-07 11:06:34 -08002400 function selectCb(d, self) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002401 selectObject(d, self);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002402 }
2403
2404 function atDragEnd(d, self) {
Simon Hunt56d51852014-11-09 13:03:35 -08002405 // once we've finished moving, pin the node in position
2406 d.fixed = true;
2407 d3.select(self).classed('fixed', true);
2408 if (config.useLiveData) {
Simon Hunt395a70c2014-11-22 23:17:40 -08002409 sendUpdateMeta(d, true);
Simon Hunta255a2c2014-11-13 22:29:35 -08002410 } else {
2411 console.log('Moving node ' + d.id + ' to [' + d.x + ',' + d.y + ']');
Simon Hunt1a9eff92014-11-07 11:06:34 -08002412 }
2413 }
2414
Simon Huntc7ee0662014-11-05 16:44:37 -08002415 // set up the force layout
2416 network.force = d3.layout.force()
2417 .size(forceDim)
2418 .nodes(network.nodes)
2419 .links(network.links)
Simon Hunt7cd48f32014-11-09 23:42:50 -08002420 .gravity(0.4)
2421 .friction(0.7)
2422 .charge(chrg)
Simon Hunt99c13842014-11-06 18:23:12 -08002423 .linkDistance(ldist)
2424 .linkStrength(lstrg)
Simon Huntc7ee0662014-11-05 16:44:37 -08002425 .on('tick', tick);
Simon Hunt195cb382014-11-03 17:50:51 -08002426
Simon Hunt01095ff2014-11-13 16:37:29 -08002427 network.drag = d3u.createDragBehavior(network.force,
Simon Huntdeab4322014-11-13 18:49:07 -08002428 selectCb, atDragEnd, panZoom);
Simon Hunt0c6d4192014-11-12 12:07:10 -08002429
2430 // create mask layer for when we lose connection to server.
Simon Hunta5e89142014-11-14 07:00:33 -08002431 // TODO: this should be part of the framework
Simon Hunt0c6d4192014-11-12 12:07:10 -08002432 mask = view.$div.append('div').attr('id','topo-mask');
2433 para(mask, 'Oops!');
2434 para(mask, 'Web-socket connection to server closed...');
2435 para(mask, 'Try refreshing the page.');
Simon Hunt12ce12e2014-11-15 21:13:19 -08002436
2437 mask.append('svg')
2438 .attr({
2439 id: 'mask-bird',
2440 width: w,
2441 height: h
2442 })
2443 .append('g')
2444 .attr('transform', birdTranslate(w, h))
2445 .style('opacity', 0.3)
2446 .append('use')
2447 .attr({
2448 'xlink:href': '#bird',
2449 width: config.birdDim,
2450 height: config.birdDim,
2451 fill: '#111'
Thomas Vachuska89543292014-11-19 11:28:33 -08002452 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08002453 }
Simon Hunt195cb382014-11-03 17:50:51 -08002454
Simon Hunt01095ff2014-11-13 16:37:29 -08002455 function para(sel, text) {
2456 sel.append('p').text(text);
2457 }
2458
2459
Simon Hunt56d51852014-11-09 13:03:35 -08002460 function load(view, ctx, flags) {
Simon Huntf67722a2014-11-10 09:32:06 -08002461 // resize, in case the window was resized while we were not loaded
2462 resize(view, ctx, flags);
2463
Simon Hunt99c13842014-11-06 18:23:12 -08002464 // cache the view token, so network topo functions can access it
2465 network.view = view;
Simon Hunt56d51852014-11-09 13:03:35 -08002466 config.useLiveData = !flags.local;
2467
2468 if (!config.useLiveData) {
2469 prepareScenario(view, ctx, flags.debug);
2470 }
Simon Hunt99c13842014-11-06 18:23:12 -08002471
2472 // set our radio buttons and key bindings
Simon Hunt9462e8c2014-11-14 17:28:09 -08002473 layerBtnSet = view.setRadio(layerButtons);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002474 view.setKeys(keyDispatch);
Simon Hunt195cb382014-11-03 17:50:51 -08002475
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002476 // patch in our "button bar" for now
2477 // TODO: implement a more official frameworky way of doing this..
2478 addButtonBar(view);
2479
Simon Huntd3b7d512014-11-12 15:48:41 -08002480 // Load map data asynchronously; complete startup after that..
2481 loadGeoJsonData();
Simon Hunta255a2c2014-11-13 22:29:35 -08002482 }
2483
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002484 function startAntTimer() {
Thomas Vachuska4731f122014-11-20 04:56:19 -08002485 if (!antTimer) {
2486 var pulses = [5, 3, 1.2, 3],
2487 pulse = 0;
2488 antTimer = setInterval(function () {
2489 pulse = pulse + 1;
2490 pulse = pulse === pulses.length ? 0 : pulse;
2491 d3.selectAll('.animated').style('stroke-width', pulses[pulse]);
2492 }, 200);
2493 }
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002494 }
2495
2496 function stopAntTimer() {
Simon Hunta255a2c2014-11-13 22:29:35 -08002497 if (antTimer) {
2498 clearInterval(antTimer);
2499 antTimer = null;
2500 }
Simon Huntd3b7d512014-11-12 15:48:41 -08002501 }
2502
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002503 function unload(view, ctx, flags) {
2504 stopAntTimer();
2505 }
2506
Simon Huntd3b7d512014-11-12 15:48:41 -08002507 // TODO: move these to config/state portion of script
Simon Hunta6a9fe72014-11-20 11:17:12 -08002508 var geoJsonUrl = 'json/map/continental_us.json',
Simon Huntd3b7d512014-11-12 15:48:41 -08002509 geoJson;
2510
2511 function loadGeoJsonData() {
2512 d3.json(geoJsonUrl, function (err, data) {
2513 if (err) {
2514 // fall back to USA map background
2515 loadStaticMap();
2516 } else {
2517 geoJson = data;
2518 loadGeoMap();
2519 }
2520
2521 // finally, connect to the server...
2522 if (config.useLiveData) {
2523 webSock.connect();
2524 }
2525 });
2526 }
2527
2528 function showBg() {
2529 return config.options.showBackground ? 'visible' : 'hidden';
2530 }
2531
2532 function loadStaticMap() {
2533 fnTrace('loadStaticMap', config.backgroundUrl);
2534 var w = network.view.width(),
2535 h = network.view.height();
2536
2537 // load the background image
2538 bgImg = svg.insert('svg:image', '#topo-G')
2539 .attr({
2540 id: 'topo-bg',
2541 width: w,
2542 height: h,
2543 'xlink:href': config.backgroundUrl
2544 })
2545 .style({
2546 visibility: showBg()
2547 });
2548 }
2549
2550 function loadGeoMap() {
2551 fnTrace('loadGeoMap', geoJsonUrl);
Simon Huntd3b7d512014-11-12 15:48:41 -08002552
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002553 // extracts the topojson data into geocoordinate-based geometry
2554 var topoData = topojson.feature(geoJson, geoJson.objects.states);
Simon Huntd3b7d512014-11-12 15:48:41 -08002555
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002556 // see: http://bl.ocks.org/mbostock/4707858
2557 geoMapProjection = d3.geo.mercator();
2558 var path = d3.geo.path().projection(geoMapProjection);
Simon Huntd3b7d512014-11-12 15:48:41 -08002559
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002560 geoMapProjection
2561 .scale(1)
2562 .translate([0, 0]);
Simon Huntd3b7d512014-11-12 15:48:41 -08002563
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002564 // [[x1,y1],[x2,y2]]
2565 var b = path.bounds(topoData);
Paul Greysonfcba0e82014-11-13 10:21:16 -08002566 // size map to 95% of minimum dimension to fill space
2567 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 -08002568 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 -08002569
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002570 geoMapProjection
2571 .scale(s)
2572 .translate(t);
2573
Paul Greysonfcba0e82014-11-13 10:21:16 -08002574 bgImg = zoomPanContainer.insert("g", '#topo-G');
Thomas Vachuska89543292014-11-19 11:28:33 -08002575 bgImg.attr('id', 'map').selectAll('path')
2576 .data(topoData.features)
2577 .enter()
2578 .append('path')
2579 .attr('d', path);
Simon Hunt195cb382014-11-03 17:50:51 -08002580 }
2581
Simon Huntf67722a2014-11-10 09:32:06 -08002582 function resize(view, ctx, flags) {
Simon Hunt12ce12e2014-11-15 21:13:19 -08002583 var w = view.width(),
2584 h = view.height();
2585
Simon Hunt934c3ce2014-11-05 11:45:07 -08002586 setSize(svg, view);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002587
2588 d3.select('#mask-bird').attr({ width: w, height: h})
2589 .select('g').attr('transform', birdTranslate(w, h));
Simon Hunt142d0032014-11-04 20:13:09 -08002590 }
2591
Simon Hunt8f40cce2014-11-23 15:57:30 -08002592 function theme(view, ctx, flags) {
2593 updateInstances();
2594 // TODO: update other theme-affected elements
2595
2596 }
2597
Simon Hunt12ce12e2014-11-15 21:13:19 -08002598 function birdTranslate(w, h) {
2599 var bdim = config.birdDim;
2600 return 'translate('+((w-bdim)*.4)+','+((h-bdim)*.1)+')';
2601 }
Simon Hunt142d0032014-11-04 20:13:09 -08002602
2603 // ==============================
2604 // View registration
Simon Hunt195cb382014-11-03 17:50:51 -08002605
Simon Hunt25248912014-11-04 11:25:48 -08002606 onos.ui.addView('topo', {
Simon Hunt142d0032014-11-04 20:13:09 -08002607 preload: preload,
2608 load: load,
Simon Hunta255a2c2014-11-13 22:29:35 -08002609 unload: unload,
Simon Hunt8f40cce2014-11-23 15:57:30 -08002610 resize: resize,
2611 theme: theme
Simon Hunt195cb382014-11-03 17:50:51 -08002612 });
2613
Thomas Vachuska47635c62014-11-22 01:21:36 -08002614 summaryPane = onos.ui.addFloatingPanel('topo-summary');
Simon Hunt61d04042014-11-11 17:27:16 -08002615 detailPane = onos.ui.addFloatingPanel('topo-detail');
Simon Hunta5e89142014-11-14 07:00:33 -08002616 oiBox = onos.ui.addFloatingPanel('topo-oibox', 'TL');
Simon Huntb82f6902014-11-22 11:53:15 -08002617 oiBox.width(20);
Simon Hunt61d04042014-11-11 17:27:16 -08002618
Simon Hunt195cb382014-11-03 17:50:51 -08002619}(ONOS));