blob: e14f0207bdd1b67ce651b4d5cb56b1d2000fa298 [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
21 */
22
23(function (onos) {
24 'use strict';
25
Simon Hunt1a9eff92014-11-07 11:06:34 -080026 // shorter names for library APIs
Simon Huntbb282f52014-11-10 11:08:19 -080027 var d3u = onos.lib.d3util,
28 trace;
Simon Hunt1a9eff92014-11-07 11:06:34 -080029
Simon Hunt195cb382014-11-03 17:50:51 -080030 // configuration data
31 var config = {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -080032 useLiveData: true,
Simon Huntfc274c92014-11-11 11:05:46 -080033 fnTrace: true,
Simon Hunt195cb382014-11-03 17:50:51 -080034 debugOn: false,
35 debug: {
Simon Hunt99c13842014-11-06 18:23:12 -080036 showNodeXY: true,
37 showKeyHandler: false
Simon Hunt195cb382014-11-03 17:50:51 -080038 },
39 options: {
40 layering: true,
41 collisionPrevention: true,
Simon Hunt142d0032014-11-04 20:13:09 -080042 showBackground: true
Simon Hunt195cb382014-11-03 17:50:51 -080043 },
44 backgroundUrl: 'img/us-map.png',
Thomas Vachuska7d638d32014-11-07 10:24:43 -080045 webSockUrl: 'ws/topology',
Simon Hunt195cb382014-11-03 17:50:51 -080046 data: {
47 live: {
48 jsonUrl: 'rs/topology/graph',
49 detailPrefix: 'rs/topology/graph/',
50 detailSuffix: ''
51 },
52 fake: {
53 jsonUrl: 'json/network2.json',
54 detailPrefix: 'json/',
55 detailSuffix: '.json'
56 }
57 },
Simon Hunt99c13842014-11-06 18:23:12 -080058 labels: {
59 imgPad: 16,
60 padLR: 4,
61 padTB: 3,
62 marginLR: 3,
63 marginTB: 2,
64 port: {
65 gap: 3,
66 width: 18,
67 height: 14
68 }
69 },
Simon Hunt1a9eff92014-11-07 11:06:34 -080070 topo: {
71 linkInColor: '#66f',
72 linkInWidth: 14
73 },
Simon Hunt99c13842014-11-06 18:23:12 -080074 icons: {
75 w: 28,
76 h: 28,
77 xoff: -12,
78 yoff: -8
79 },
Simon Hunt195cb382014-11-03 17:50:51 -080080 iconUrl: {
81 device: 'img/device.png',
82 host: 'img/host.png',
83 pkt: 'img/pkt.png',
84 opt: 'img/opt.png'
85 },
Simon Hunt195cb382014-11-03 17:50:51 -080086 force: {
Simon Hunt7cd48f32014-11-09 23:42:50 -080087 note_for_links: 'link.type is used to differentiate',
Simon Huntc7ee0662014-11-05 16:44:37 -080088 linkDistance: {
Simon Hunt7cd48f32014-11-09 23:42:50 -080089 direct: 100,
90 optical: 120,
Thomas Vachuska3266abf2014-11-13 09:28:46 -080091 hostLink: 3
Simon Huntc7ee0662014-11-05 16:44:37 -080092 },
93 linkStrength: {
Simon Hunt7cd48f32014-11-09 23:42:50 -080094 direct: 1.0,
95 optical: 1.0,
96 hostLink: 1.0
Simon Huntc7ee0662014-11-05 16:44:37 -080097 },
Simon Hunt7cd48f32014-11-09 23:42:50 -080098 note_for_nodes: 'node.class is used to differentiate',
Simon Huntc7ee0662014-11-05 16:44:37 -080099 charge: {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800100 device: -8000,
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800101 host: -5000
Simon Huntc7ee0662014-11-05 16:44:37 -0800102 },
103 pad: 20,
Simon Hunt195cb382014-11-03 17:50:51 -0800104 translate: function() {
105 return 'translate(' +
Simon Huntc7ee0662014-11-05 16:44:37 -0800106 config.force.pad + ',' +
107 config.force.pad + ')';
Simon Hunt195cb382014-11-03 17:50:51 -0800108 }
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800109 },
110 // see below in creation of viewBox on main svg
111 logicalSize: 1000
Simon Hunt195cb382014-11-03 17:50:51 -0800112 };
113
Simon Hunt142d0032014-11-04 20:13:09 -0800114 // radio buttons
115 var btnSet = [
Simon Hunt934c3ce2014-11-05 11:45:07 -0800116 { text: 'All Layers', cb: showAllLayers },
117 { text: 'Packet Only', cb: showPacketLayer },
118 { text: 'Optical Only', cb: showOpticalLayer }
119 ];
120
121 // key bindings
122 var keyDispatch = {
Simon Hunta255a2c2014-11-13 22:29:35 -0800123 M: testMe, // TODO: remove (testing only)
124 S: injectStartupEvents, // TODO: remove (testing only)
125 space: injectTestEvent, // TODO: remove (testing only)
Simon Hunt99c13842014-11-06 18:23:12 -0800126
Simon Hunt01095ff2014-11-13 16:37:29 -0800127 B: toggleBg,
Simon Hunt934c3ce2014-11-05 11:45:07 -0800128 L: cycleLabels,
129 P: togglePorts,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800130 U: unpin,
Paul Greysonfcba0e82014-11-13 10:21:16 -0800131 R: resetZoomPan,
Simon Huntd72bc702014-11-13 18:38:04 -0800132 esc: deselectAll
Simon Hunt934c3ce2014-11-05 11:45:07 -0800133 };
Simon Hunt142d0032014-11-04 20:13:09 -0800134
Simon Hunt195cb382014-11-03 17:50:51 -0800135 // state variables
Simon Hunt99c13842014-11-06 18:23:12 -0800136 var network = {
Simon Hunt50128c02014-11-08 13:36:15 -0800137 view: null, // view token reference
Simon Hunt99c13842014-11-06 18:23:12 -0800138 nodes: [],
139 links: [],
140 lookup: {}
141 },
Simon Hunt56d51852014-11-09 13:03:35 -0800142 scenario = {
143 evDir: 'json/ev/',
144 evScenario: '/scenario.json',
145 evPrefix: '/ev_',
146 evOnos: '_onos.json',
147 evUi: '_ui.json',
148 ctx: null,
149 params: {},
150 evNumber: 0,
151 view: null,
152 debug: false
153 },
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800154 webSock,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800155 sid = 0,
Simon Hunt56d51852014-11-09 13:03:35 -0800156 deviceLabelIndex = 0,
157 hostLabelIndex = 0,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800158 selections = {},
Simon Hunta5e89142014-11-14 07:00:33 -0800159 selectOrder = [],
Simon Hunt6ac93f32014-11-13 12:17:27 -0800160 hovered = null,
Simon Hunta5e89142014-11-14 07:00:33 -0800161 detailPane,
Simon Hunta255a2c2014-11-13 22:29:35 -0800162 antTimer = null,
Simon Hunta5e89142014-11-14 07:00:33 -0800163 onosInstances = {},
164 onosOrder = [],
165 oiBox,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800166
Simon Hunt195cb382014-11-03 17:50:51 -0800167 viewMode = 'showAll',
168 portLabelsOn = false;
169
Simon Hunt934c3ce2014-11-05 11:45:07 -0800170 // D3 selections
171 var svg,
Paul Greysonfcba0e82014-11-13 10:21:16 -0800172 zoomPanContainer,
Simon Hunt934c3ce2014-11-05 11:45:07 -0800173 bgImg,
Simon Huntc7ee0662014-11-05 16:44:37 -0800174 topoG,
175 nodeG,
176 linkG,
177 node,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800178 link,
179 mask;
Simon Hunt195cb382014-11-03 17:50:51 -0800180
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800181 // the projection for the map background
182 var geoMapProjection;
183
Paul Greysonfcba0e82014-11-13 10:21:16 -0800184 // the zoom function
185 var zoom;
186
Simon Hunt142d0032014-11-04 20:13:09 -0800187 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800188 // For Debugging / Development
Simon Hunt195cb382014-11-03 17:50:51 -0800189
Simon Hunt99c13842014-11-06 18:23:12 -0800190 function note(label, msg) {
191 console.log('NOTE: ' + label + ': ' + msg);
Simon Hunt195cb382014-11-03 17:50:51 -0800192 }
193
Simon Hunt99c13842014-11-06 18:23:12 -0800194 function debug(what) {
195 return config.debugOn && config.debug[what];
Simon Hunt934c3ce2014-11-05 11:45:07 -0800196 }
197
Simon Huntfc274c92014-11-11 11:05:46 -0800198 function fnTrace(msg, id) {
199 if (config.fnTrace) {
200 console.log('FN: ' + msg + ' [' + id + ']');
201 }
202 }
Simon Hunt99c13842014-11-06 18:23:12 -0800203
Simon Hunta5e89142014-11-14 07:00:33 -0800204 function evTrace(data) {
205 fnTrace(data.event, data.payload.id);
206 }
207
Simon Hunt934c3ce2014-11-05 11:45:07 -0800208 // ==============================
209 // Key Callbacks
210
Simon Hunt99c13842014-11-06 18:23:12 -0800211 function testMe(view) {
Simon Hunta5e89142014-11-14 07:00:33 -0800212 //view.alert('test');
213 detailPane.show();
214 setTimeout(detailPane.hide, 2000);
215 oiBox.show();
216 setTimeout(oiBox.hide, 2000);
Simon Hunt99c13842014-11-06 18:23:12 -0800217 }
218
Simon Hunt56d51852014-11-09 13:03:35 -0800219 function abortIfLive() {
Simon Hunt50128c02014-11-08 13:36:15 -0800220 if (config.useLiveData) {
Simon Huntb53e0682014-11-12 13:32:01 -0800221 network.view.alert("Sorry, currently using live data..");
Simon Hunt56d51852014-11-09 13:03:35 -0800222 return true;
Simon Hunt50128c02014-11-08 13:36:15 -0800223 }
Simon Hunt56d51852014-11-09 13:03:35 -0800224 return false;
225 }
Simon Hunt50128c02014-11-08 13:36:15 -0800226
Simon Hunt56d51852014-11-09 13:03:35 -0800227 function testDebug(msg) {
228 if (scenario.debug) {
229 scenario.view.alert(msg);
230 }
231 }
Simon Hunt99c13842014-11-06 18:23:12 -0800232
Simon Hunt56d51852014-11-09 13:03:35 -0800233 function injectTestEvent(view) {
234 if (abortIfLive()) { return; }
235 var sc = scenario,
236 evn = ++sc.evNumber,
237 pfx = sc.evDir + sc.ctx + sc.evPrefix + evn,
238 onosUrl = pfx + sc.evOnos,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800239 uiUrl = pfx + sc.evUi,
240 stack = [
241 { url: onosUrl, cb: handleServerEvent },
242 { url: uiUrl, cb: handleUiEvent }
243 ];
244 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800245 }
246
Simon Hunt7cd48f32014-11-09 23:42:50 -0800247 function recurseFetchEvent(stack, evn) {
248 var v = scenario.view,
249 frame;
250 if (stack.length === 0) {
Simon Huntfc274c92014-11-11 11:05:46 -0800251 v.alert('Oops!\n\nNo event #' + evn + ' found.');
Simon Hunt7cd48f32014-11-09 23:42:50 -0800252 return;
253 }
254 frame = stack.shift();
255
256 d3.json(frame.url, function (err, data) {
Simon Hunt99c13842014-11-06 18:23:12 -0800257 if (err) {
Simon Hunt56d51852014-11-09 13:03:35 -0800258 if (err.status === 404) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800259 // if we didn't find the data, try the next stack frame
260 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800261 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800262 v.alert('non-404 error:\n\n' + frame.url + '\n\n' + err);
Simon Hunt56d51852014-11-09 13:03:35 -0800263 }
Simon Hunt99c13842014-11-06 18:23:12 -0800264 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800265 testDebug('loaded: ' + frame.url);
266 frame.cb(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800267 }
268 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800269
Simon Hunt56d51852014-11-09 13:03:35 -0800270 }
Simon Hunt50128c02014-11-08 13:36:15 -0800271
Simon Hunt56d51852014-11-09 13:03:35 -0800272 function handleUiEvent(data) {
Simon Huntbb282f52014-11-10 11:08:19 -0800273 scenario.view.alert('UI Tx: ' + data.event + '\n\n' +
274 JSON.stringify(data));
Simon Hunt56d51852014-11-09 13:03:35 -0800275 }
276
277 function injectStartupEvents(view) {
278 var last = scenario.params.lastAuto || 0;
279 if (abortIfLive()) { return; }
280
281 while (scenario.evNumber < last) {
Simon Hunt1a9eff92014-11-07 11:06:34 -0800282 injectTestEvent(view);
283 }
284 }
285
Simon Hunt934c3ce2014-11-05 11:45:07 -0800286 function toggleBg() {
287 var vis = bgImg.style('visibility');
288 bgImg.style('visibility', (vis === 'hidden') ? 'visible' : 'hidden');
289 }
290
Simon Hunt99c13842014-11-06 18:23:12 -0800291 function cycleLabels() {
Simon Huntbb282f52014-11-10 11:08:19 -0800292 deviceLabelIndex = (deviceLabelIndex === network.deviceLabelCount - 1)
293 ? 0 : deviceLabelIndex + 1;
Simon Hunt5f36d342014-11-08 21:33:14 -0800294
Simon Hunt99c13842014-11-06 18:23:12 -0800295 network.nodes.forEach(function (d) {
Simon Huntbb282f52014-11-10 11:08:19 -0800296 if (d.class === 'device') {
297 updateDeviceLabel(d);
298 }
Simon Hunt99c13842014-11-06 18:23:12 -0800299 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800300 }
301
302 function togglePorts(view) {
Simon Hunt50128c02014-11-08 13:36:15 -0800303 view.alert('togglePorts() callback')
Simon Hunt934c3ce2014-11-05 11:45:07 -0800304 }
305
Simon Hunt6ac93f32014-11-13 12:17:27 -0800306 function unpin() {
307 if (hovered) {
308 hovered.fixed = false;
309 hovered.el.classed('fixed', false);
310 network.force.resume();
311 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800312 }
313
314 // ==============================
315 // Radio Button Callbacks
316
Simon Hunta5e89142014-11-14 07:00:33 -0800317 var layerLookup = {
318 host: {
319 endstation: 'pkt', // default, if host event does not define type
320 bgpSpeaker: 'pkt'
321 },
322 device: {
323 switch: 'pkt',
324 roadm: 'opt'
325 },
326 link: {
327 hostLink: 'pkt',
328 direct: 'pkt',
329 optical: 'opt'
330 }
331 };
332
333 function inLayer(d, layer) {
334 var look = layerLookup[d.class],
335 lyr = look && look[d.type];
336 return lyr === layer;
337 }
338
339 function unsuppressLayer(which) {
340 node.each(function (d) {
341 var node = d.el;
342 if (inLayer(d, which)) {
343 node.classed('suppressed', false);
344 }
345 });
346
347 link.each(function (d) {
348 var link = d.el;
349 if (inLayer(d, which)) {
350 link.classed('suppressed', false);
351 }
352 });
353 }
354
Simon Hunt195cb382014-11-03 17:50:51 -0800355 function showAllLayers() {
Simon Hunta5e89142014-11-14 07:00:33 -0800356 node.classed('suppressed', false);
357 link.classed('suppressed', false);
Simon Hunt142d0032014-11-04 20:13:09 -0800358// d3.selectAll('svg .port').classed('inactive', false);
359// d3.selectAll('svg .portText').classed('inactive', false);
Simon Hunt195cb382014-11-03 17:50:51 -0800360 }
361
362 function showPacketLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800363 node.classed('suppressed', true);
364 link.classed('suppressed', true);
365 unsuppressLayer('pkt');
Simon Hunt195cb382014-11-03 17:50:51 -0800366 }
367
368 function showOpticalLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800369 node.classed('suppressed', true);
370 link.classed('suppressed', true);
371 unsuppressLayer('opt');
Simon Hunt195cb382014-11-03 17:50:51 -0800372 }
373
Simon Hunt142d0032014-11-04 20:13:09 -0800374 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800375 // Private functions
376
Simon Hunt99c13842014-11-06 18:23:12 -0800377 function safeId(s) {
378 return s.replace(/[^a-z0-9]/gi, '-');
379 }
380
Simon Huntc7ee0662014-11-05 16:44:37 -0800381 // set the size of the given element to that of the view (reduced if padded)
382 function setSize(el, view, pad) {
383 var padding = pad ? pad * 2 : 0;
Simon Hunt934c3ce2014-11-05 11:45:07 -0800384 el.attr({
Simon Huntc7ee0662014-11-05 16:44:37 -0800385 width: view.width() - padding,
386 height: view.height() - padding
Simon Hunt934c3ce2014-11-05 11:45:07 -0800387 });
388 }
389
Simon Hunt934c3ce2014-11-05 11:45:07 -0800390
Simon Hunt99c13842014-11-06 18:23:12 -0800391 // ==============================
392 // Event handlers for server-pushed events
393
Simon Huntbb282f52014-11-10 11:08:19 -0800394 function logicError(msg) {
395 // TODO, report logic error to server, via websock, so it can be logged
396 network.view.alert('Logic Error:\n\n' + msg);
Simon Huntfc274c92014-11-11 11:05:46 -0800397 console.warn(msg);
Simon Huntbb282f52014-11-10 11:08:19 -0800398 }
399
Simon Hunt99c13842014-11-06 18:23:12 -0800400 var eventDispatch = {
Simon Hunta5e89142014-11-14 07:00:33 -0800401 addInstance: addInstance,
Simon Hunt99c13842014-11-06 18:23:12 -0800402 addDevice: addDevice,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800403 addLink: addLink,
Simon Hunt56d51852014-11-09 13:03:35 -0800404 addHost: addHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800405
Simon Huntd72bc702014-11-13 18:38:04 -0800406 updateInstance: stillToImplement,
Simon Huntbb282f52014-11-10 11:08:19 -0800407 updateDevice: updateDevice,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800408 updateLink: updateLink,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800409 updateHost: updateHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800410
Simon Huntd72bc702014-11-13 18:38:04 -0800411 removeInstance: stillToImplement,
Simon Huntbb282f52014-11-10 11:08:19 -0800412 removeDevice: stillToImplement,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800413 removeLink: removeLink,
Simon Hunt44031102014-11-11 13:20:36 -0800414 removeHost: removeHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800415
Simon Hunt61d04042014-11-11 17:27:16 -0800416 showDetails: showDetails,
Simon Huntb53e0682014-11-12 13:32:01 -0800417 showPath: showPath,
418 showTraffic: showTraffic
Simon Hunt99c13842014-11-06 18:23:12 -0800419 };
420
Simon Hunta5e89142014-11-14 07:00:33 -0800421 function addInstance(data) {
422 evTrace(data);
423 var inst = data.payload,
424 id = inst.id;
425 if (onosInstances[id]) {
426 logicError('ONOS instance already added: ' + id);
427 return;
428 }
429 onosInstances[id] = inst;
430 onosOrder.push(inst);
431 updateInstances();
432 }
433
Simon Hunt99c13842014-11-06 18:23:12 -0800434 function addDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800435 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800436 var device = data.payload,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800437 nodeData = createDeviceNode(device);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800438 network.nodes.push(nodeData);
439 network.lookup[nodeData.id] = nodeData;
Simon Hunt99c13842014-11-06 18:23:12 -0800440 updateNodes();
441 network.force.start();
442 }
443
Simon Hunt99c13842014-11-06 18:23:12 -0800444 function addLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800445 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800446 var link = data.payload,
447 lnk = createLink(link);
Simon Hunt99c13842014-11-06 18:23:12 -0800448 if (lnk) {
Simon Hunt99c13842014-11-06 18:23:12 -0800449 network.links.push(lnk);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800450 network.lookup[lnk.id] = lnk;
Simon Hunt99c13842014-11-06 18:23:12 -0800451 updateLinks();
452 network.force.start();
453 }
454 }
455
Simon Hunt56d51852014-11-09 13:03:35 -0800456 function addHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800457 evTrace(data);
Simon Hunt56d51852014-11-09 13:03:35 -0800458 var host = data.payload,
459 node = createHostNode(host),
460 lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800461 network.nodes.push(node);
462 network.lookup[host.id] = node;
463 updateNodes();
464
465 lnk = createHostLink(host);
466 if (lnk) {
Simon Hunt44031102014-11-11 13:20:36 -0800467 node.linkData = lnk; // cache ref on its host
Simon Hunt56d51852014-11-09 13:03:35 -0800468 network.links.push(lnk);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800469 network.lookup[host.ingress] = lnk;
470 network.lookup[host.egress] = lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800471 updateLinks();
472 }
473 network.force.start();
474 }
475
Simon Hunt44031102014-11-11 13:20:36 -0800476 // TODO: fold updateX(...) methods into one base method; remove duplication
Simon Huntbb282f52014-11-10 11:08:19 -0800477 function updateDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800478 evTrace(data);
Simon Huntbb282f52014-11-10 11:08:19 -0800479 var device = data.payload,
480 id = device.id,
481 nodeData = network.lookup[id];
482 if (nodeData) {
483 $.extend(nodeData, device);
484 updateDeviceState(nodeData);
485 } else {
486 logicError('updateDevice lookup fail. ID = "' + id + '"');
487 }
488 }
489
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800490 function updateLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800491 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800492 var link = data.payload,
493 id = link.id,
494 linkData = network.lookup[id];
495 if (linkData) {
496 $.extend(linkData, link);
497 updateLinkState(linkData);
498 } else {
499 logicError('updateLink lookup fail. ID = "' + id + '"');
500 }
501 }
502
Simon Hunt7cd48f32014-11-09 23:42:50 -0800503 function updateHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800504 evTrace(data);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800505 var host = data.payload,
Simon Huntbb282f52014-11-10 11:08:19 -0800506 id = host.id,
507 hostData = network.lookup[id];
508 if (hostData) {
509 $.extend(hostData, host);
510 updateHostState(hostData);
511 } else {
512 logicError('updateHost lookup fail. ID = "' + id + '"');
513 }
Simon Hunt7cd48f32014-11-09 23:42:50 -0800514 }
515
Simon Hunt44031102014-11-11 13:20:36 -0800516 // TODO: fold removeX(...) methods into base method - remove dup code
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800517 function removeLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800518 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800519 var link = data.payload,
520 id = link.id,
521 linkData = network.lookup[id];
522 if (linkData) {
523 removeLinkElement(linkData);
524 } else {
525 logicError('removeLink lookup fail. ID = "' + id + '"');
526 }
527 }
528
Simon Hunt44031102014-11-11 13:20:36 -0800529 function removeHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800530 evTrace(data);
Simon Hunt44031102014-11-11 13:20:36 -0800531 var host = data.payload,
532 id = host.id,
533 hostData = network.lookup[id];
534 if (hostData) {
535 removeHostElement(hostData);
536 } else {
537 logicError('removeHost lookup fail. ID = "' + id + '"');
538 }
539 }
540
Simon Hunt61d04042014-11-11 17:27:16 -0800541 function showDetails(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800542 evTrace(data);
Simon Hunt61d04042014-11-11 17:27:16 -0800543 populateDetails(data.payload);
544 detailPane.show();
545 }
546
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800547 function showPath(data) {
Simon Huntd72bc702014-11-13 18:38:04 -0800548 // TODO: review - making sure we are handling the payload correctly.
Simon Hunta5e89142014-11-14 07:00:33 -0800549 evTrace(data);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800550 var links = data.payload.links,
551 s = [ data.event + "\n" + links.length ];
552 links.forEach(function (d, i) {
553 s.push(d);
554 });
555 network.view.alert(s.join('\n'));
556
557 links.forEach(function (d, i) {
558 var link = network.lookup[d];
559 if (link) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800560 link.el.classed('showPath', true);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800561 }
562 });
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800563 }
564
Simon Huntb53e0682014-11-12 13:32:01 -0800565 function showTraffic(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800566 evTrace(data);
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800567 var paths = data.payload.paths;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800568
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800569 // Revert any links hilighted previously.
Simon Hunta255a2c2014-11-13 22:29:35 -0800570 link.classed('primary secondary animated optical', false);
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800571
572 // Now hilight all links in the paths payload.
Simon Hunta255a2c2014-11-13 22:29:35 -0800573 paths.forEach(function (p) {
574 var cls = p.class;
575 p.links.forEach(function (id) {
576 var lnk = network.lookup[id];
577 if (lnk) {
578 lnk.el.classed(cls, true);
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800579 }
580 });
581 });
Simon Huntb53e0682014-11-12 13:32:01 -0800582 }
583
Simon Hunt56d51852014-11-09 13:03:35 -0800584 // ...............................
585
586 function stillToImplement(data) {
587 var p = data.payload;
588 note(data.event, p.id);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800589 network.view.alert('Not yet implemented: "' + data.event + '"');
Simon Hunt56d51852014-11-09 13:03:35 -0800590 }
Simon Hunt99c13842014-11-06 18:23:12 -0800591
592 function unknownEvent(data) {
Simon Hunt50128c02014-11-08 13:36:15 -0800593 network.view.alert('Unknown event type: "' + data.event + '"');
Simon Hunt99c13842014-11-06 18:23:12 -0800594 }
595
596 function handleServerEvent(data) {
597 var fn = eventDispatch[data.event] || unknownEvent;
598 fn(data);
599 }
600
601 // ==============================
Simon Hunt61d04042014-11-11 17:27:16 -0800602 // Out-going messages...
603
Simon Huntb53e0682014-11-12 13:32:01 -0800604 function userFeedback(msg) {
605 // for now, use the alert pane as is. Maybe different alert style in
606 // the future (centered on view; dismiss button?)
607 network.view.alert(msg);
608 }
609
610 function nSel() {
611 return selectOrder.length;
612 }
Simon Hunt61d04042014-11-11 17:27:16 -0800613 function getSel(idx) {
614 return selections[selectOrder[idx]];
615 }
Simon Huntb53e0682014-11-12 13:32:01 -0800616 function getSelId(idx) {
617 return getSel(idx).obj.id;
618 }
619 function allSelectionsClass(cls) {
620 for (var i=0, n=nSel(); i<n; i++) {
621 if (getSel(i).obj.class !== cls) {
622 return false;
623 }
624 }
625 return true;
626 }
Simon Hunt61d04042014-11-11 17:27:16 -0800627
Simon Hunt61d04042014-11-11 17:27:16 -0800628 // request details for the selected element
Simon Huntd72bc702014-11-13 18:38:04 -0800629 // invoked from selection of a single node.
Simon Hunt61d04042014-11-11 17:27:16 -0800630 function requestDetails() {
631 var data = getSel(0).obj,
632 payload = {
633 id: data.id,
634 class: data.class
635 };
636 sendMessage('requestDetails', payload);
637 }
638
Simon Huntd72bc702014-11-13 18:38:04 -0800639 function addIntentAction() {
640 sendMessage('addHostIntent', {
641 one: getSelId(0),
642 two: getSelId(1)
643 });
644 }
645
646 function showTrafficAction() {
647 // if nothing is hovered over, and nothing selected, send cancel request
648 if (!hovered && nSel() === 0) {
649 sendMessage('cancelTraffic', {});
650 return;
651 }
652
653 // NOTE: hover is only populated if "show traffic on hover" is
654 // toggled on, and the item hovered is a host...
655 var hoverId = (trafficHover() && hovered && hovered.class === 'host')
656 ? hovered.id : '';
657 sendMessage('requestTraffic', {
658 ids: selectOrder,
659 hover: hoverId
660 });
661 }
662
663
Simon Hunt61d04042014-11-11 17:27:16 -0800664 // ==============================
Simon Hunta5e89142014-11-14 07:00:33 -0800665 // onos instance panel functions
666
667 function updateInstances() {
668 var onoses = oiBox.el.selectAll('.onosInst')
669 .data(onosOrder, function (d) { return d.id; });
670
671 // operate on existing onoses if necessary
672
673 var entering = onoses.enter()
674 .append('div')
675 .attr('class', 'onosInst')
676 .classed('online', function (d) { return d.online; })
677 .text(function (d) { return d.id; });
678
679 // operate on existing + new onoses here
680
681 // the departed...
682 var exiting = onoses.exit()
683 .transition()
684 .style('opacity', 0)
685 .remove();
686 }
687
688 // ==============================
Simon Hunt99c13842014-11-06 18:23:12 -0800689 // force layout modification functions
690
691 function translate(x, y) {
692 return 'translate(' + x + ',' + y + ')';
693 }
694
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800695 function missMsg(what, id) {
696 return '\n[' + what + '] "' + id + '" missing ';
697 }
698
699 function linkEndPoints(srcId, dstId) {
700 var srcNode = network.lookup[srcId],
701 dstNode = network.lookup[dstId],
702 sMiss = !srcNode ? missMsg('src', srcId) : '',
703 dMiss = !dstNode ? missMsg('dst', dstId) : '';
704
705 if (sMiss || dMiss) {
706 logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
707 return null;
708 }
709 return {
710 source: srcNode,
711 target: dstNode,
712 x1: srcNode.x,
713 y1: srcNode.y,
714 x2: dstNode.x,
715 y2: dstNode.y
716 };
717 }
718
Simon Hunt56d51852014-11-09 13:03:35 -0800719 function createHostLink(host) {
720 var src = host.id,
721 dst = host.cp.device,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800722 id = host.ingress,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800723 lnk = linkEndPoints(src, dst);
Simon Hunt56d51852014-11-09 13:03:35 -0800724
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800725 if (!lnk) {
Simon Hunt56d51852014-11-09 13:03:35 -0800726 return null;
727 }
728
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800729 // Synthesize link ...
730 $.extend(lnk, {
Thomas Vachuska4830d392014-11-09 17:09:56 -0800731 id: id,
Simon Hunt56d51852014-11-09 13:03:35 -0800732 class: 'link',
Simon Hunt7cd48f32014-11-09 23:42:50 -0800733 type: 'hostLink',
Simon Hunt56d51852014-11-09 13:03:35 -0800734 svgClass: 'link hostLink',
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800735 linkWidth: 1
Simon Hunt7cd48f32014-11-09 23:42:50 -0800736 });
Simon Hunt99c13842014-11-06 18:23:12 -0800737 return lnk;
738 }
739
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800740 function createLink(link) {
741 var lnk = linkEndPoints(link.src, link.dst),
742 type = link.type;
743
744 if (!lnk) {
745 return null;
746 }
747
748 // merge in remaining data
749 $.extend(lnk, link, {
750 class: 'link',
751 svgClass: type ? 'link ' + type : 'link'
752 });
753 return lnk;
Simon Hunt1a9eff92014-11-07 11:06:34 -0800754 }
755
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800756 var widthRatio = 1.4,
757 linkScale = d3.scale.linear()
758 .domain([1, 12])
759 .range([widthRatio, 12 * widthRatio])
760 .clamp(true);
761
762 function updateLinkWidth (d) {
763 // TODO: watch out for .showPath/.showTraffic classes
764 d.el.transition()
765 .duration(1000)
766 .attr('stroke-width', linkScale(d.linkWidth));
767 }
768
769
Simon Hunt99c13842014-11-06 18:23:12 -0800770 function updateLinks() {
771 link = linkG.selectAll('.link')
772 .data(network.links, function (d) { return d.id; });
773
774 // operate on existing links, if necessary
775 // link .foo() .bar() ...
776
777 // operate on entering links:
778 var entering = link.enter()
779 .append('line')
780 .attr({
Simon Hunt99c13842014-11-06 18:23:12 -0800781 class: function (d) { return d.svgClass; },
782 x1: function (d) { return d.x1; },
783 y1: function (d) { return d.y1; },
784 x2: function (d) { return d.x2; },
785 y2: function (d) { return d.y2; },
Simon Hunt1a9eff92014-11-07 11:06:34 -0800786 stroke: config.topo.linkInColor,
787 'stroke-width': config.topo.linkInWidth
Simon Hunt99c13842014-11-06 18:23:12 -0800788 })
789 .transition().duration(1000)
790 .attr({
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800791 'stroke-width': function (d) { return linkScale(d.linkWidth); },
Simon Hunt99c13842014-11-06 18:23:12 -0800792 stroke: '#666' // TODO: remove explicit stroke, rather...
793 });
794
795 // augment links
Simon Hunt7cd48f32014-11-09 23:42:50 -0800796 entering.each(function (d) {
797 var link = d3.select(this);
798 // provide ref to element selection from backing data....
799 d.el = link;
Simon Hunt99c13842014-11-06 18:23:12 -0800800
Simon Hunt7cd48f32014-11-09 23:42:50 -0800801 // TODO: add src/dst port labels etc.
802 });
Thomas Vachuska4830d392014-11-09 17:09:56 -0800803
804 // operate on both existing and new links, if necessary
805 //link .foo() .bar() ...
806
807 // operate on exiting links:
Thomas Vachuska4830d392014-11-09 17:09:56 -0800808 link.exit()
Thomas Vachuska4830d392014-11-09 17:09:56 -0800809 .attr({
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800810 'stroke-dasharray': '3, 3'
Thomas Vachuska4830d392014-11-09 17:09:56 -0800811 })
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800812 .style('opacity', 0.4)
813 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -0800814 .duration(1500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800815 .attr({
816 'stroke-dasharray': '3, 12'
817 })
818 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -0800819 .duration(500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800820 .style('opacity', 0.0)
Thomas Vachuska4830d392014-11-09 17:09:56 -0800821 .remove();
Simon Hunt99c13842014-11-06 18:23:12 -0800822 }
823
824 function createDeviceNode(device) {
825 // start with the object as is
826 var node = device,
Simon Huntbb282f52014-11-10 11:08:19 -0800827 type = device.type,
828 svgCls = type ? 'node device ' + type : 'node device';
Simon Hunt99c13842014-11-06 18:23:12 -0800829
830 // Augment as needed...
831 node.class = 'device';
Simon Huntbb282f52014-11-10 11:08:19 -0800832 node.svgClass = device.online ? svgCls + ' online' : svgCls;
Simon Hunt99c13842014-11-06 18:23:12 -0800833 positionNode(node);
834
835 // cache label array length
836 network.deviceLabelCount = device.labels.length;
Simon Hunt99c13842014-11-06 18:23:12 -0800837 return node;
838 }
839
Simon Hunt56d51852014-11-09 13:03:35 -0800840 function createHostNode(host) {
841 // start with the object as is
842 var node = host;
843
844 // Augment as needed...
845 node.class = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -0800846 if (!node.type) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800847 node.type = 'endstation';
848 }
Simon Hunt56d51852014-11-09 13:03:35 -0800849 node.svgClass = 'node host';
Simon Hunt56d51852014-11-09 13:03:35 -0800850 positionNode(node);
851
852 // cache label array length
853 network.hostLabelCount = host.labels.length;
Simon Hunt56d51852014-11-09 13:03:35 -0800854 return node;
855 }
856
Simon Hunt99c13842014-11-06 18:23:12 -0800857 function positionNode(node) {
858 var meta = node.metaUi,
Simon Huntac9e24f2014-11-12 10:12:21 -0800859 x = meta && meta.x,
860 y = meta && meta.y,
861 xy;
Simon Hunt99c13842014-11-06 18:23:12 -0800862
Simon Huntac9e24f2014-11-12 10:12:21 -0800863 // If we have [x,y] already, use that...
Simon Hunt99c13842014-11-06 18:23:12 -0800864 if (x && y) {
865 node.fixed = true;
Simon Huntac9e24f2014-11-12 10:12:21 -0800866 node.x = x;
867 node.y = y;
868 return;
Simon Hunt99c13842014-11-06 18:23:12 -0800869 }
Simon Huntac9e24f2014-11-12 10:12:21 -0800870
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800871 var location = node.location;
872 if (location && location.type === 'latlng') {
873 var coord = geoMapProjection([location.lng, location.lat]);
874 node.fixed = true;
875 node.x = coord[0];
876 node.y = coord[1];
877 return;
878 }
879
Simon Huntac9e24f2014-11-12 10:12:21 -0800880 // Note: Placing incoming unpinned nodes at exactly the same point
881 // (center of the view) causes them to explode outwards when
882 // the force layout kicks in. So, we spread them out a bit
883 // initially, to provide a more serene layout convergence.
884 // Additionally, if the node is a host, we place it near
885 // the device it is connected to.
886
887 function spread(s) {
888 return Math.floor((Math.random() * s) - s/2);
889 }
890
891 function randDim(dim) {
892 return dim / 2 + spread(dim * 0.7071);
893 }
894
895 function rand() {
896 return {
897 x: randDim(network.view.width()),
898 y: randDim(network.view.height())
899 };
900 }
901
902 function near(node) {
903 var min = 12,
904 dx = spread(12),
905 dy = spread(12);
906 return {
907 x: node.x + min + dx,
908 y: node.y + min + dy
909 };
910 }
911
912 function getDevice(cp) {
913 var d = network.lookup[cp.device];
914 return d || rand();
915 }
916
917 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
918 $.extend(node, xy);
Simon Hunt99c13842014-11-06 18:23:12 -0800919 }
920
Simon Hunt99c13842014-11-06 18:23:12 -0800921 function iconUrl(d) {
922 return 'img/' + d.type + '.png';
923 }
924
925 // returns the newly computed bounding box of the rectangle
926 function adjustRectToFitText(n) {
927 var text = n.select('text'),
928 box = text.node().getBBox(),
929 lab = config.labels;
930
931 text.attr('text-anchor', 'middle')
932 .attr('y', '-0.8em')
933 .attr('x', lab.imgPad/2);
934
935 // translate the bbox so that it is centered on [x,y]
936 box.x = -box.width / 2;
937 box.y = -box.height / 2;
938
939 // add padding
940 box.x -= (lab.padLR + lab.imgPad/2);
941 box.width += lab.padLR * 2 + lab.imgPad;
942 box.y -= lab.padTB;
943 box.height += lab.padTB * 2;
944
945 return box;
946 }
947
Simon Hunt1a9eff92014-11-07 11:06:34 -0800948 function mkSvgClass(d) {
949 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
950 }
951
Simon Hunt7cd48f32014-11-09 23:42:50 -0800952 function hostLabel(d) {
953 var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0;
954 return d.labels[idx];
955 }
956 function deviceLabel(d) {
957 var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0;
958 return d.labels[idx];
959 }
960 function niceLabel(label) {
961 return (label && label.trim()) ? label : '.';
962 }
963
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800964 function updateDeviceLabel(d) {
965 var label = niceLabel(deviceLabel(d)),
966 node = d.el,
967 box;
968
969 node.select('text')
970 .text(label)
971 .style('opacity', 0)
972 .transition()
973 .style('opacity', 1);
974
975 box = adjustRectToFitText(node);
976
977 node.select('rect')
978 .transition()
979 .attr(box);
980
981 node.select('image')
982 .transition()
983 .attr('x', box.x + config.icons.xoff)
984 .attr('y', box.y + config.icons.yoff);
985 }
986
987 function updateHostLabel(d) {
988 var label = hostLabel(d),
989 host = d.el;
990
991 host.select('text').text(label);
992 }
993
Simon Huntbb282f52014-11-10 11:08:19 -0800994 function updateDeviceState(nodeData) {
995 nodeData.el.classed('online', nodeData.online);
996 updateDeviceLabel(nodeData);
997 // TODO: review what else might need to be updated
998 }
999
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001000 function updateLinkState(linkData) {
1001 updateLinkWidth(linkData);
1002 // TODO: review what else might need to be updated
1003 // update label, if showing
1004 }
1005
Simon Huntbb282f52014-11-10 11:08:19 -08001006 function updateHostState(hostData) {
1007 updateHostLabel(hostData);
1008 // TODO: review what else might need to be updated
1009 }
1010
Simon Hunt6ac93f32014-11-13 12:17:27 -08001011 function nodeMouseOver(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001012 hovered = d;
Simon Huntd72bc702014-11-13 18:38:04 -08001013 if (trafficHover() && d.class === 'host') {
1014 showTrafficAction();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001015 }
1016 }
1017
1018 function nodeMouseOut(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001019 hovered = null;
Simon Huntd72bc702014-11-13 18:38:04 -08001020 if (trafficHover() && d.class === 'host') {
1021 showTrafficAction();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001022 }
1023 }
Simon Huntbb282f52014-11-10 11:08:19 -08001024
Simon Hunt99c13842014-11-06 18:23:12 -08001025 function updateNodes() {
1026 node = nodeG.selectAll('.node')
1027 .data(network.nodes, function (d) { return d.id; });
1028
1029 // operate on existing nodes, if necessary
Simon Hunt7cd48f32014-11-09 23:42:50 -08001030 // update host labels
Simon Hunt99c13842014-11-06 18:23:12 -08001031 //node .foo() .bar() ...
1032
1033 // operate on entering nodes:
1034 var entering = node.enter()
1035 .append('g')
1036 .attr({
1037 id: function (d) { return safeId(d.id); },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001038 class: mkSvgClass,
Simon Hunt99c13842014-11-06 18:23:12 -08001039 transform: function (d) { return translate(d.x, d.y); },
1040 opacity: 0
1041 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08001042 .call(network.drag)
Simon Hunt6ac93f32014-11-13 12:17:27 -08001043 .on('mouseover', nodeMouseOver)
1044 .on('mouseout', nodeMouseOut)
Simon Hunt99c13842014-11-06 18:23:12 -08001045 .transition()
1046 .attr('opacity', 1);
1047
1048 // augment device nodes...
1049 entering.filter('.device').each(function (d) {
1050 var node = d3.select(this),
1051 icon = iconUrl(d),
Simon Hunt7cd48f32014-11-09 23:42:50 -08001052 label = niceLabel(deviceLabel(d)),
Simon Hunt99c13842014-11-06 18:23:12 -08001053 box;
1054
Simon Hunt7cd48f32014-11-09 23:42:50 -08001055 // provide ref to element from backing data....
1056 d.el = node;
1057
Simon Hunt99c13842014-11-06 18:23:12 -08001058 node.append('rect')
1059 .attr({
1060 'rx': 5,
1061 'ry': 5
1062 });
1063
1064 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001065 .text(label)
Simon Hunt99c13842014-11-06 18:23:12 -08001066 .attr('dy', '1.1em');
1067
1068 box = adjustRectToFitText(node);
1069
1070 node.select('rect')
1071 .attr(box);
1072
1073 if (icon) {
1074 var cfg = config.icons;
1075 node.append('svg:image')
1076 .attr({
1077 x: box.x + config.icons.xoff,
1078 y: box.y + config.icons.yoff,
1079 width: cfg.w,
1080 height: cfg.h,
1081 'xlink:href': icon
1082 });
1083 }
1084
1085 // debug function to show the modelled x,y coordinates of nodes...
1086 if (debug('showNodeXY')) {
1087 node.select('rect').attr('fill-opacity', 0.5);
1088 node.append('circle')
1089 .attr({
1090 class: 'debug',
1091 cx: 0,
1092 cy: 0,
1093 r: '3px'
1094 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001095 }
1096 });
Simon Hunt934c3ce2014-11-05 11:45:07 -08001097
Simon Hunt56d51852014-11-09 13:03:35 -08001098 // augment host nodes...
1099 entering.filter('.host').each(function (d) {
1100 var node = d3.select(this),
Simon Hunt56d51852014-11-09 13:03:35 -08001101 box;
1102
Simon Hunt7cd48f32014-11-09 23:42:50 -08001103 // provide ref to element from backing data....
1104 d.el = node;
1105
Simon Hunt56d51852014-11-09 13:03:35 -08001106 node.append('circle')
1107 .attr('r', 8); // TODO: define host circle radius
1108
Simon Hunt56d51852014-11-09 13:03:35 -08001109 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001110 .text(hostLabel)
1111 .attr('dy', '1.3em')
1112 .attr('text-anchor', 'middle');
Simon Hunt56d51852014-11-09 13:03:35 -08001113
1114 // debug function to show the modelled x,y coordinates of nodes...
1115 if (debug('showNodeXY')) {
1116 node.select('circle').attr('fill-opacity', 0.5);
1117 node.append('circle')
1118 .attr({
1119 class: 'debug',
1120 cx: 0,
1121 cy: 0,
1122 r: '3px'
1123 });
1124 }
1125 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001126
Simon Hunt99c13842014-11-06 18:23:12 -08001127 // operate on both existing and new nodes, if necessary
1128 //node .foo() .bar() ...
Simon Huntc7ee0662014-11-05 16:44:37 -08001129
Simon Hunt99c13842014-11-06 18:23:12 -08001130 // operate on exiting nodes:
Simon Huntea80eb42014-11-11 13:46:57 -08001131 // Note that the node is removed after 2 seconds.
1132 // Sub element animations should be shorter than 2 seconds.
1133 var exiting = node.exit()
Simon Hunt44031102014-11-11 13:20:36 -08001134 .transition()
1135 .duration(2000)
Simon Huntea80eb42014-11-11 13:46:57 -08001136 .style('opacity', 0)
Simon Hunt99c13842014-11-06 18:23:12 -08001137 .remove();
Simon Huntea80eb42014-11-11 13:46:57 -08001138
1139 // host node exits....
1140 exiting.filter('.host').each(function (d) {
1141 var node = d3.select(this);
1142
1143 node.select('text')
1144 .style('opacity', 0.5)
1145 .transition()
1146 .duration(1000)
1147 .style('opacity', 0);
1148 // note, leave <g>.remove to remove this element
1149
1150 node.select('circle')
1151 .style('stroke-fill', '#555')
1152 .style('fill', '#888')
1153 .style('opacity', 0.5)
1154 .transition()
1155 .duration(1500)
1156 .attr('r', 0);
1157 // note, leave <g>.remove to remove this element
1158
1159 });
1160
1161 // TODO: device node exits
Simon Huntc7ee0662014-11-05 16:44:37 -08001162 }
1163
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001164 function find(id, array) {
1165 for (var idx = 0, n = array.length; idx < n; idx++) {
1166 if (array[idx].id === id) {
1167 return idx;
1168 }
1169 }
1170 return -1;
1171 }
1172
1173 function removeLinkElement(linkData) {
1174 // remove from lookup cache
1175 delete network.lookup[linkData.id];
1176 // remove from links array
1177 var idx = find(linkData.id, network.links);
Simon Huntfc274c92014-11-11 11:05:46 -08001178 network.links.splice(idx, 1);
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001179 // remove from SVG
1180 updateLinks();
Simon Hunt44031102014-11-11 13:20:36 -08001181 network.force.resume();
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001182 }
Simon Huntc7ee0662014-11-05 16:44:37 -08001183
Simon Hunt44031102014-11-11 13:20:36 -08001184 function removeHostElement(hostData) {
1185 // first, remove associated hostLink...
1186 removeLinkElement(hostData.linkData);
1187
1188 // remove from lookup cache
1189 delete network.lookup[hostData.id];
1190 // remove from nodes array
1191 var idx = find(hostData.id, network.nodes);
1192 network.nodes.splice(idx, 1);
1193 // remove from SVG
1194 updateNodes();
1195 network.force.resume();
1196 }
1197
1198
Simon Huntc7ee0662014-11-05 16:44:37 -08001199 function tick() {
1200 node.attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001201 transform: function (d) { return translate(d.x, d.y); }
Simon Huntc7ee0662014-11-05 16:44:37 -08001202 });
1203
1204 link.attr({
1205 x1: function (d) { return d.source.x; },
1206 y1: function (d) { return d.source.y; },
1207 x2: function (d) { return d.target.x; },
1208 y2: function (d) { return d.target.y; }
1209 });
1210 }
Simon Hunt934c3ce2014-11-05 11:45:07 -08001211
1212 // ==============================
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001213 // Web-Socket for live data
1214
1215 function webSockUrl() {
1216 return document.location.toString()
1217 .replace(/\#.*/, '')
1218 .replace('http://', 'ws://')
1219 .replace('https://', 'wss://')
1220 .replace('index2.html', config.webSockUrl);
1221 }
1222
1223 webSock = {
1224 ws : null,
1225
1226 connect : function() {
1227 webSock.ws = new WebSocket(webSockUrl());
1228
1229 webSock.ws.onopen = function() {
Simon Hunt0c6d4192014-11-12 12:07:10 -08001230 noWebSock(false);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001231 };
1232
1233 webSock.ws.onmessage = function(m) {
1234 if (m.data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001235 wsTraceRx(m.data);
Thomas Vachuskad472c6e2014-11-07 19:11:05 -08001236 handleServerEvent(JSON.parse(m.data));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001237 }
1238 };
1239
1240 webSock.ws.onclose = function(m) {
1241 webSock.ws = null;
Simon Hunt0c6d4192014-11-12 12:07:10 -08001242 noWebSock(true);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001243 };
1244 },
1245
1246 send : function(text) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001247 if (text != null) {
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001248 webSock._send(text);
1249 }
1250 },
1251
1252 _send : function(message) {
1253 if (webSock.ws) {
1254 webSock.ws.send(message);
Simon Hunta255a2c2014-11-13 22:29:35 -08001255 } else if (config.useLiveData) {
Simon Hunt56d51852014-11-09 13:03:35 -08001256 network.view.alert('no web socket open\n\n' + message);
Simon Hunta255a2c2014-11-13 22:29:35 -08001257 } else {
1258 console.log('WS Send: ' + JSON.stringify(message));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001259 }
1260 }
1261
1262 };
1263
Simon Hunt0c6d4192014-11-12 12:07:10 -08001264 function noWebSock(b) {
1265 mask.style('display',b ? 'block' : 'none');
1266 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001267
Simon Hunt61d04042014-11-11 17:27:16 -08001268 // TODO: use cache of pending messages (key = sid) to reconcile responses
1269
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001270 function sendMessage(evType, payload) {
1271 var toSend = {
Simon Huntbb282f52014-11-10 11:08:19 -08001272 event: evType,
1273 sid: ++sid,
1274 payload: payload
1275 },
1276 asText = JSON.stringify(toSend);
1277 wsTraceTx(asText);
1278 webSock.send(asText);
1279 }
1280
1281 function wsTraceTx(msg) {
1282 wsTrace('tx', msg);
1283 }
1284 function wsTraceRx(msg) {
1285 wsTrace('rx', msg);
1286 }
1287 function wsTrace(rxtx, msg) {
Simon Huntbb282f52014-11-10 11:08:19 -08001288 console.log('[' + rxtx + '] ' + msg);
1289 // TODO: integrate with trace view
1290 //if (trace) {
1291 // trace.output(rxtx, msg);
1292 //}
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001293 }
1294
1295
1296 // ==============================
1297 // Selection stuff
1298
1299 function selectObject(obj, el) {
1300 var n,
Simon Hunt01095ff2014-11-13 16:37:29 -08001301 srcEv = d3.event.sourceEvent,
1302 meta = srcEv.metaKey,
1303 shift = srcEv.shiftKey;
1304
Simon Huntdeab4322014-11-13 18:49:07 -08001305 if ((panZoom() && !meta) || (!panZoom() && meta)) {
Simon Hunt01095ff2014-11-13 16:37:29 -08001306 return;
1307 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001308
1309 if (el) {
1310 n = d3.select(el);
1311 } else {
1312 node.each(function(d) {
1313 if (d == obj) {
1314 n = d3.select(el = this);
1315 }
1316 });
1317 }
1318 if (!n) return;
1319
Simon Hunt01095ff2014-11-13 16:37:29 -08001320 if (shift && n.classed('selected')) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001321 deselectObject(obj.id);
Simon Hunt61d04042014-11-11 17:27:16 -08001322 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001323 return;
1324 }
1325
Simon Hunt01095ff2014-11-13 16:37:29 -08001326 if (!shift) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001327 deselectAll();
1328 }
1329
Simon Huntc31d5692014-11-12 13:27:18 -08001330 selections[obj.id] = { obj: obj, el: el };
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001331 selectOrder.push(obj.id);
1332
1333 n.classed('selected', true);
Simon Hunt61d04042014-11-11 17:27:16 -08001334 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001335 }
1336
1337 function deselectObject(id) {
Simon Huntc31d5692014-11-12 13:27:18 -08001338 var obj = selections[id],
1339 idx;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001340 if (obj) {
1341 d3.select(obj.el).classed('selected', false);
Simon Hunt61d04042014-11-11 17:27:16 -08001342 delete selections[id];
Simon Huntc31d5692014-11-12 13:27:18 -08001343 idx = $.inArray(id, selectOrder);
1344 if (idx >= 0) {
1345 selectOrder.splice(idx, 1);
1346 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001347 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001348 }
1349
1350 function deselectAll() {
1351 // deselect all nodes in the network...
1352 node.classed('selected', false);
1353 selections = {};
1354 selectOrder = [];
Simon Hunt61d04042014-11-11 17:27:16 -08001355 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001356 }
1357
Simon Hunt61d04042014-11-11 17:27:16 -08001358 // update the state of the detail pane, based on current selections
1359 function updateDetailPane() {
1360 var nSel = selectOrder.length;
1361 if (!nSel) {
1362 detailPane.hide();
Simon Huntd72bc702014-11-13 18:38:04 -08001363 showTrafficAction(); // sends cancelTraffic event
Simon Hunt61d04042014-11-11 17:27:16 -08001364 } else if (nSel === 1) {
1365 singleSelect();
1366 } else {
1367 multiSelect();
1368 }
1369 }
1370
1371 function singleSelect() {
1372 requestDetails();
Simon Huntb53e0682014-11-12 13:32:01 -08001373 // NOTE: detail pane will be shown from showDetails event callback
Simon Hunt61d04042014-11-11 17:27:16 -08001374 }
1375
1376 function multiSelect() {
Simon Huntb53e0682014-11-12 13:32:01 -08001377 populateMultiSelect();
Simon Huntb53e0682014-11-12 13:32:01 -08001378 }
1379
1380 function addSep(tbody) {
1381 var tr = tbody.append('tr');
1382 $('<hr>').appendTo(tr.append('td').attr('colspan', 2));
1383 }
1384
1385 function addProp(tbody, label, value) {
1386 var tr = tbody.append('tr');
1387
1388 tr.append('td')
1389 .attr('class', 'label')
1390 .text(label + ' :');
1391
1392 tr.append('td')
1393 .attr('class', 'value')
1394 .text(value);
1395 }
1396
1397 function populateMultiSelect() {
1398 detailPane.empty();
1399
1400 var title = detailPane.append("h2"),
1401 table = detailPane.append("table"),
1402 tbody = table.append("tbody");
1403
1404 title.text('Multi-Select...');
1405
1406 selectOrder.forEach(function (d, i) {
1407 addProp(tbody, i+1, d);
1408 });
Simon Huntd72bc702014-11-13 18:38:04 -08001409
1410 addMultiSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08001411 }
1412
1413 function populateDetails(data) {
1414 detailPane.empty();
1415
1416 var title = detailPane.append("h2"),
1417 table = detailPane.append("table"),
1418 tbody = table.append("tbody");
1419
1420 $('<img src="img/' + data.type + '.png">').appendTo(title);
1421 $('<span>').attr('class', 'icon').text(data.id).appendTo(title);
1422
1423 data.propOrder.forEach(function(p) {
1424 if (p === '-') {
1425 addSep(tbody);
1426 } else {
1427 addProp(tbody, p, data.props[p]);
1428 }
1429 });
Simon Huntd72bc702014-11-13 18:38:04 -08001430
1431 addSingleSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08001432 }
1433
Simon Huntd72bc702014-11-13 18:38:04 -08001434 function addSingleSelectActions() {
1435 detailPane.append('hr');
1436 // always want to allow 'show traffic'
Simon Hunta5e89142014-11-14 07:00:33 -08001437 addAction(detailPane, 'Show Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001438 }
1439
1440 function addMultiSelectActions() {
1441 detailPane.append('hr');
1442 // always want to allow 'show traffic'
Simon Hunta5e89142014-11-14 07:00:33 -08001443 addAction(detailPane, 'Show Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001444 // if exactly two hosts are selected, also want 'add host intent'
1445 if (nSel() === 2 && allSelectionsClass('host')) {
Simon Hunta5e89142014-11-14 07:00:33 -08001446 addAction(detailPane, 'Add Host Intent', addIntentAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001447 }
1448 }
1449
Simon Hunta5e89142014-11-14 07:00:33 -08001450 function addAction(panel, text, cb) {
1451 panel.append('div')
Simon Huntd72bc702014-11-13 18:38:04 -08001452 .classed('actionBtn', true)
1453 .text(text)
1454 .on('click', cb);
1455 }
1456
1457
Paul Greysonfcba0e82014-11-13 10:21:16 -08001458 function zoomPan(scale, translate) {
1459 zoomPanContainer.attr("transform", "translate(" + translate + ")scale(" + scale + ")");
1460 // keep the map lines constant width while zooming
1461 bgImg.style("stroke-width", 2.0 / scale + "px");
1462 }
1463
1464 function resetZoomPan() {
1465 zoomPan(1, [0,0]);
1466 zoom.scale(1).translate([0,0]);
1467 }
1468
1469 function setupZoomPan() {
1470 function zoomed() {
Simon Huntdeab4322014-11-13 18:49:07 -08001471 if (!panZoom() ^ !d3.event.sourceEvent.metaKey) {
Paul Greysonfcba0e82014-11-13 10:21:16 -08001472 zoomPan(d3.event.scale, d3.event.translate);
1473 }
1474 }
1475
1476 zoom = d3.behavior.zoom()
1477 .translate([0, 0])
1478 .scale(1)
1479 .scaleExtent([1, 8])
1480 .on("zoom", zoomed);
1481
1482 svg.call(zoom);
1483 }
1484
Simon Hunt61d04042014-11-11 17:27:16 -08001485 // ==============================
1486 // Test harness code
Simon Hunt56d51852014-11-09 13:03:35 -08001487
1488 function prepareScenario(view, ctx, dbg) {
1489 var sc = scenario,
1490 urlSc = sc.evDir + ctx + sc.evScenario;
1491
1492 if (!ctx) {
1493 view.alert("No scenario specified (null ctx)");
1494 return;
1495 }
1496
1497 sc.view = view;
1498 sc.ctx = ctx;
1499 sc.debug = dbg;
1500 sc.evNumber = 0;
1501
1502 d3.json(urlSc, function(err, data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001503 var p = data && data.params || {},
1504 desc = data && data.description || null,
Simon Huntfc274c92014-11-11 11:05:46 -08001505 intro = data && data.title;
Simon Huntbb282f52014-11-10 11:08:19 -08001506
Simon Hunt56d51852014-11-09 13:03:35 -08001507 if (err) {
1508 view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err);
1509 } else {
1510 sc.params = p;
Simon Huntbb282f52014-11-10 11:08:19 -08001511 if (desc) {
1512 intro += '\n\n ' + desc.join('\n ');
1513 }
1514 view.alert(intro);
Simon Hunt56d51852014-11-09 13:03:35 -08001515 }
1516 });
1517
1518 }
1519
Simon Hunt01095ff2014-11-13 16:37:29 -08001520 // ==============================
1521 // Toggle Buttons in masthead
Simon Hunt0c6d4192014-11-12 12:07:10 -08001522
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001523 // TODO: toggle button (and other widgets in the masthead) should be provided
1524 // by the framework; not generated by the view.
1525
Simon Hunta5e89142014-11-14 07:00:33 -08001526 var showInstances,
1527 doPanZoom,
1528 showTrafficOnHover;
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001529
1530 function addButtonBar(view) {
1531 var bb = d3.select('#mast')
1532 .append('span').classed('right', true).attr('id', 'bb');
1533
Simon Hunta5e89142014-11-14 07:00:33 -08001534 function mkTogBtn(text, cb) {
1535 return bb.append('span')
1536 .classed('btn', true)
1537 .text(text)
1538 .on('click', cb);
1539 }
Simon Hunt01095ff2014-11-13 16:37:29 -08001540
Simon Hunta5e89142014-11-14 07:00:33 -08001541 showInstances = mkTogBtn('Show Instances', toggleInst);
1542 doPanZoom = mkTogBtn('Pan/Zoom', togglePanZoom);
1543 showTrafficOnHover = mkTogBtn('Show traffic on hover', toggleTrafficHover);
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001544 }
1545
Simon Hunta5e89142014-11-14 07:00:33 -08001546 function instShown() {
1547 return showInstances.classed('active');
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001548 }
Simon Hunta5e89142014-11-14 07:00:33 -08001549 function toggleInst() {
1550 showInstances.classed('active', !instShown());
1551 if (instShown()) {
1552 oiBox.show();
1553 } else {
1554 oiBox.hide();
1555 }
Simon Hunt01095ff2014-11-13 16:37:29 -08001556 }
1557
Simon Huntdeab4322014-11-13 18:49:07 -08001558 function panZoom() {
1559 return doPanZoom.classed('active');
Simon Hunt01095ff2014-11-13 16:37:29 -08001560 }
Simon Hunta5e89142014-11-14 07:00:33 -08001561 function togglePanZoom() {
1562 doPanZoom.classed('active', !panZoom());
1563 }
1564
1565 function trafficHover() {
1566 return showTrafficOnHover.classed('active');
1567 }
1568 function toggleTrafficHover() {
1569 showTrafficOnHover.classed('active', !trafficHover());
1570 }
1571
Simon Hunt01095ff2014-11-13 16:37:29 -08001572
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001573 // ==============================
Simon Hunt142d0032014-11-04 20:13:09 -08001574 // View life-cycle callbacks
Simon Hunt195cb382014-11-03 17:50:51 -08001575
Simon Huntf67722a2014-11-10 09:32:06 -08001576 function preload(view, ctx, flags) {
Simon Hunt142d0032014-11-04 20:13:09 -08001577 var w = view.width(),
1578 h = view.height(),
Simon Huntc7ee0662014-11-05 16:44:37 -08001579 fcfg = config.force,
1580 fpad = fcfg.pad,
1581 forceDim = [w - 2*fpad, h - 2*fpad];
Simon Hunt195cb382014-11-03 17:50:51 -08001582
Simon Hunt142d0032014-11-04 20:13:09 -08001583 // NOTE: view.$div is a D3 selection of the view's div
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001584 var viewBox = '0 0 ' + config.logicalSize + ' ' + config.logicalSize;
1585 svg = view.$div.append('svg').attr('viewBox', viewBox);
Simon Hunt934c3ce2014-11-05 11:45:07 -08001586 setSize(svg, view);
1587
Paul Greysonfcba0e82014-11-13 10:21:16 -08001588 zoomPanContainer = svg.append('g').attr('id', 'zoomPanContainer');
Paul Greysonfcba0e82014-11-13 10:21:16 -08001589 setupZoomPan();
1590
Simon Hunt1a9eff92014-11-07 11:06:34 -08001591 // add blue glow filter to svg layer
Paul Greysonfcba0e82014-11-13 10:21:16 -08001592 d3u.appendGlow(zoomPanContainer);
Simon Hunt1a9eff92014-11-07 11:06:34 -08001593
Simon Huntc7ee0662014-11-05 16:44:37 -08001594 // group for the topology
Paul Greysonfcba0e82014-11-13 10:21:16 -08001595 topoG = zoomPanContainer.append('g')
Simon Huntd3b7d512014-11-12 15:48:41 -08001596 .attr('id', 'topo-G')
Simon Huntc7ee0662014-11-05 16:44:37 -08001597 .attr('transform', fcfg.translate());
1598
1599 // subgroups for links and nodes
1600 linkG = topoG.append('g').attr('id', 'links');
1601 nodeG = topoG.append('g').attr('id', 'nodes');
1602
1603 // selection of nodes and links
1604 link = linkG.selectAll('.link');
1605 node = nodeG.selectAll('.node');
1606
Simon Hunt7cd48f32014-11-09 23:42:50 -08001607 function chrg(d) {
1608 return fcfg.charge[d.class] || -12000;
1609 }
Simon Hunt99c13842014-11-06 18:23:12 -08001610 function ldist(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08001611 return fcfg.linkDistance[d.type] || 50;
Simon Hunt99c13842014-11-06 18:23:12 -08001612 }
1613 function lstrg(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08001614 // 0.0 - 1.0
1615 return fcfg.linkStrength[d.type] || 1.0;
Simon Hunt99c13842014-11-06 18:23:12 -08001616 }
1617
Simon Hunt1a9eff92014-11-07 11:06:34 -08001618 function selectCb(d, self) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001619 selectObject(d, self);
Simon Hunt1a9eff92014-11-07 11:06:34 -08001620 }
1621
1622 function atDragEnd(d, self) {
Simon Hunt56d51852014-11-09 13:03:35 -08001623 // once we've finished moving, pin the node in position
1624 d.fixed = true;
1625 d3.select(self).classed('fixed', true);
1626 if (config.useLiveData) {
Simon Hunt902c9922014-11-11 11:59:31 -08001627 sendUpdateMeta(d);
Simon Hunta255a2c2014-11-13 22:29:35 -08001628 } else {
1629 console.log('Moving node ' + d.id + ' to [' + d.x + ',' + d.y + ']');
Simon Hunt1a9eff92014-11-07 11:06:34 -08001630 }
1631 }
1632
Simon Hunt902c9922014-11-11 11:59:31 -08001633 function sendUpdateMeta(d) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001634 sendMessage('updateMeta', {
1635 id: d.id,
1636 'class': d.class,
Simon Hunt902c9922014-11-11 11:59:31 -08001637 'memento': {
Simon Hunt01095ff2014-11-13 16:37:29 -08001638 x: d.x,
1639 y: d.y
Simon Hunt902c9922014-11-11 11:59:31 -08001640 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001641 });
1642 }
1643
Simon Huntc7ee0662014-11-05 16:44:37 -08001644 // set up the force layout
1645 network.force = d3.layout.force()
1646 .size(forceDim)
1647 .nodes(network.nodes)
1648 .links(network.links)
Simon Hunt7cd48f32014-11-09 23:42:50 -08001649 .gravity(0.4)
1650 .friction(0.7)
1651 .charge(chrg)
Simon Hunt99c13842014-11-06 18:23:12 -08001652 .linkDistance(ldist)
1653 .linkStrength(lstrg)
Simon Huntc7ee0662014-11-05 16:44:37 -08001654 .on('tick', tick);
Simon Hunt195cb382014-11-03 17:50:51 -08001655
Simon Hunt01095ff2014-11-13 16:37:29 -08001656 network.drag = d3u.createDragBehavior(network.force,
Simon Huntdeab4322014-11-13 18:49:07 -08001657 selectCb, atDragEnd, panZoom);
Simon Hunt0c6d4192014-11-12 12:07:10 -08001658
1659 // create mask layer for when we lose connection to server.
Simon Hunta5e89142014-11-14 07:00:33 -08001660 // TODO: this should be part of the framework
Simon Hunt0c6d4192014-11-12 12:07:10 -08001661 mask = view.$div.append('div').attr('id','topo-mask');
1662 para(mask, 'Oops!');
1663 para(mask, 'Web-socket connection to server closed...');
1664 para(mask, 'Try refreshing the page.');
Simon Hunt1a9eff92014-11-07 11:06:34 -08001665 }
Simon Hunt195cb382014-11-03 17:50:51 -08001666
Simon Hunt01095ff2014-11-13 16:37:29 -08001667 function para(sel, text) {
1668 sel.append('p').text(text);
1669 }
1670
1671
Simon Hunt56d51852014-11-09 13:03:35 -08001672 function load(view, ctx, flags) {
Simon Huntf67722a2014-11-10 09:32:06 -08001673 // resize, in case the window was resized while we were not loaded
1674 resize(view, ctx, flags);
1675
Simon Hunt99c13842014-11-06 18:23:12 -08001676 // cache the view token, so network topo functions can access it
1677 network.view = view;
Simon Hunt56d51852014-11-09 13:03:35 -08001678 config.useLiveData = !flags.local;
1679
1680 if (!config.useLiveData) {
1681 prepareScenario(view, ctx, flags.debug);
1682 }
Simon Hunt99c13842014-11-06 18:23:12 -08001683
1684 // set our radio buttons and key bindings
Simon Hunt934c3ce2014-11-05 11:45:07 -08001685 view.setRadio(btnSet);
1686 view.setKeys(keyDispatch);
Simon Hunt195cb382014-11-03 17:50:51 -08001687
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001688 // patch in our "button bar" for now
1689 // TODO: implement a more official frameworky way of doing this..
1690 addButtonBar(view);
1691
Simon Huntd3b7d512014-11-12 15:48:41 -08001692 // Load map data asynchronously; complete startup after that..
1693 loadGeoJsonData();
Simon Hunta255a2c2014-11-13 22:29:35 -08001694
1695 // start the and timer
1696 var dashIdx = 0;
1697 antTimer = setInterval(function () {
1698 // TODO: figure out how to choose Src-->Dst and Dst-->Src, per link
1699 dashIdx = dashIdx === 0 ? 14 : dashIdx - 2;
1700 d3.selectAll('.animated').style('stroke-dashoffset', dashIdx);
1701 }, 35);
1702 }
1703
1704 function unload(view, ctx, flags) {
1705 if (antTimer) {
1706 clearInterval(antTimer);
1707 antTimer = null;
1708 }
Simon Huntd3b7d512014-11-12 15:48:41 -08001709 }
1710
1711 // TODO: move these to config/state portion of script
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001712 var geoJsonUrl = 'json/map/continental_us.json', // TODO: Paul
Simon Huntd3b7d512014-11-12 15:48:41 -08001713 geoJson;
1714
1715 function loadGeoJsonData() {
1716 d3.json(geoJsonUrl, function (err, data) {
1717 if (err) {
1718 // fall back to USA map background
1719 loadStaticMap();
1720 } else {
1721 geoJson = data;
1722 loadGeoMap();
1723 }
1724
1725 // finally, connect to the server...
1726 if (config.useLiveData) {
1727 webSock.connect();
1728 }
1729 });
1730 }
1731
1732 function showBg() {
1733 return config.options.showBackground ? 'visible' : 'hidden';
1734 }
1735
1736 function loadStaticMap() {
1737 fnTrace('loadStaticMap', config.backgroundUrl);
1738 var w = network.view.width(),
1739 h = network.view.height();
1740
1741 // load the background image
1742 bgImg = svg.insert('svg:image', '#topo-G')
1743 .attr({
1744 id: 'topo-bg',
1745 width: w,
1746 height: h,
1747 'xlink:href': config.backgroundUrl
1748 })
1749 .style({
1750 visibility: showBg()
1751 });
1752 }
1753
1754 function loadGeoMap() {
1755 fnTrace('loadGeoMap', geoJsonUrl);
Simon Huntd3b7d512014-11-12 15:48:41 -08001756
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001757 // extracts the topojson data into geocoordinate-based geometry
1758 var topoData = topojson.feature(geoJson, geoJson.objects.states);
Simon Huntd3b7d512014-11-12 15:48:41 -08001759
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001760 // see: http://bl.ocks.org/mbostock/4707858
1761 geoMapProjection = d3.geo.mercator();
1762 var path = d3.geo.path().projection(geoMapProjection);
Simon Huntd3b7d512014-11-12 15:48:41 -08001763
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001764 geoMapProjection
1765 .scale(1)
1766 .translate([0, 0]);
Simon Huntd3b7d512014-11-12 15:48:41 -08001767
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001768 // [[x1,y1],[x2,y2]]
1769 var b = path.bounds(topoData);
Paul Greysonfcba0e82014-11-13 10:21:16 -08001770 // size map to 95% of minimum dimension to fill space
1771 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 -08001772 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 -08001773
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001774 geoMapProjection
1775 .scale(s)
1776 .translate(t);
1777
Paul Greysonfcba0e82014-11-13 10:21:16 -08001778 bgImg = zoomPanContainer.insert("g", '#topo-G');
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001779 bgImg.attr('id', 'map').selectAll('path')
1780 .data(topoData.features)
1781 .enter()
1782 .append('path')
1783 .attr('d', path);
Simon Hunt195cb382014-11-03 17:50:51 -08001784 }
1785
Simon Huntf67722a2014-11-10 09:32:06 -08001786 function resize(view, ctx, flags) {
Simon Hunt934c3ce2014-11-05 11:45:07 -08001787 setSize(svg, view);
Simon Hunt142d0032014-11-04 20:13:09 -08001788 }
1789
1790
1791 // ==============================
1792 // View registration
Simon Hunt195cb382014-11-03 17:50:51 -08001793
Simon Hunt25248912014-11-04 11:25:48 -08001794 onos.ui.addView('topo', {
Simon Hunt142d0032014-11-04 20:13:09 -08001795 preload: preload,
1796 load: load,
Simon Hunta255a2c2014-11-13 22:29:35 -08001797 unload: unload,
Simon Hunt142d0032014-11-04 20:13:09 -08001798 resize: resize
Simon Hunt195cb382014-11-03 17:50:51 -08001799 });
1800
Simon Hunt61d04042014-11-11 17:27:16 -08001801 detailPane = onos.ui.addFloatingPanel('topo-detail');
Simon Hunta5e89142014-11-14 07:00:33 -08001802 oiBox = onos.ui.addFloatingPanel('topo-oibox', 'TL');
Simon Hunt61d04042014-11-11 17:27:16 -08001803
Simon Hunt195cb382014-11-03 17:50:51 -08001804}(ONOS));