blob: 681562ebd972697cd6e9f0e867c0a6fda9703c32 [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,
Simon Huntf67722a2014-11-10 09:32:06 -080091 hostLink: 5
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,
101 host: -300
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 }
Simon Hunt142d0032014-11-04 20:13:09 -0800109 }
Simon Hunt195cb382014-11-03 17:50:51 -0800110 };
111
Simon Hunt142d0032014-11-04 20:13:09 -0800112 // radio buttons
113 var btnSet = [
Simon Hunt934c3ce2014-11-05 11:45:07 -0800114 { text: 'All Layers', cb: showAllLayers },
115 { text: 'Packet Only', cb: showPacketLayer },
116 { text: 'Optical Only', cb: showOpticalLayer }
117 ];
118
119 // key bindings
120 var keyDispatch = {
Simon Hunt1a9eff92014-11-07 11:06:34 -0800121 M: testMe, // TODO: remove (testing only)
Simon Hunt50128c02014-11-08 13:36:15 -0800122 S: injectStartupEvents, // TODO: remove (testing only)
123 space: injectTestEvent, // TODO: remove (testing only)
Simon Hunt99c13842014-11-06 18:23:12 -0800124
Thomas Vachuska65368e32014-11-08 16:10:20 -0800125 B: toggleBg, // TODO: do we really need this?
Simon Hunt934c3ce2014-11-05 11:45:07 -0800126 L: cycleLabels,
127 P: togglePorts,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800128 U: unpin,
129
Simon Huntac9e24f2014-11-12 10:12:21 -0800130 Z: requestPath,
131 X: cancelMonitor
Simon Hunt934c3ce2014-11-05 11:45:07 -0800132 };
Simon Hunt142d0032014-11-04 20:13:09 -0800133
Simon Hunt195cb382014-11-03 17:50:51 -0800134 // state variables
Simon Hunt99c13842014-11-06 18:23:12 -0800135 var network = {
Simon Hunt50128c02014-11-08 13:36:15 -0800136 view: null, // view token reference
Simon Hunt99c13842014-11-06 18:23:12 -0800137 nodes: [],
138 links: [],
139 lookup: {}
140 },
Simon Hunt56d51852014-11-09 13:03:35 -0800141 scenario = {
142 evDir: 'json/ev/',
143 evScenario: '/scenario.json',
144 evPrefix: '/ev_',
145 evOnos: '_onos.json',
146 evUi: '_ui.json',
147 ctx: null,
148 params: {},
149 evNumber: 0,
150 view: null,
151 debug: false
152 },
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800153 webSock,
Simon Hunt56d51852014-11-09 13:03:35 -0800154 deviceLabelIndex = 0,
155 hostLabelIndex = 0,
Simon Hunt61d04042014-11-11 17:27:16 -0800156 detailPane,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800157 selectOrder = [],
158 selections = {},
159
Simon Hunt195cb382014-11-03 17:50:51 -0800160 highlighted = null,
161 hovered = null,
162 viewMode = 'showAll',
163 portLabelsOn = false;
164
Simon Hunt934c3ce2014-11-05 11:45:07 -0800165 // D3 selections
166 var svg,
167 bgImg,
Simon Huntc7ee0662014-11-05 16:44:37 -0800168 topoG,
169 nodeG,
170 linkG,
171 node,
172 link;
Simon Hunt195cb382014-11-03 17:50:51 -0800173
Simon Hunt142d0032014-11-04 20:13:09 -0800174 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800175 // For Debugging / Development
Simon Hunt195cb382014-11-03 17:50:51 -0800176
Simon Hunt99c13842014-11-06 18:23:12 -0800177 function note(label, msg) {
178 console.log('NOTE: ' + label + ': ' + msg);
Simon Hunt195cb382014-11-03 17:50:51 -0800179 }
180
Simon Hunt99c13842014-11-06 18:23:12 -0800181 function debug(what) {
182 return config.debugOn && config.debug[what];
Simon Hunt934c3ce2014-11-05 11:45:07 -0800183 }
184
Simon Huntfc274c92014-11-11 11:05:46 -0800185 function fnTrace(msg, id) {
186 if (config.fnTrace) {
187 console.log('FN: ' + msg + ' [' + id + ']');
188 }
189 }
Simon Hunt99c13842014-11-06 18:23:12 -0800190
Simon Hunt934c3ce2014-11-05 11:45:07 -0800191 // ==============================
192 // Key Callbacks
193
Simon Hunt99c13842014-11-06 18:23:12 -0800194 function testMe(view) {
Simon Hunt50128c02014-11-08 13:36:15 -0800195 view.alert('test');
Simon Hunt61d04042014-11-11 17:27:16 -0800196 detailPane.show();
197 setTimeout(function () {
198 detailPane.hide();
199 }, 3000);
Simon Hunt99c13842014-11-06 18:23:12 -0800200 }
201
Simon Hunt56d51852014-11-09 13:03:35 -0800202 function abortIfLive() {
Simon Hunt50128c02014-11-08 13:36:15 -0800203 if (config.useLiveData) {
Simon Hunt56d51852014-11-09 13:03:35 -0800204 scenario.view.alert("Sorry, currently using live data..");
205 return true;
Simon Hunt50128c02014-11-08 13:36:15 -0800206 }
Simon Hunt56d51852014-11-09 13:03:35 -0800207 return false;
208 }
Simon Hunt50128c02014-11-08 13:36:15 -0800209
Simon Hunt56d51852014-11-09 13:03:35 -0800210 function testDebug(msg) {
211 if (scenario.debug) {
212 scenario.view.alert(msg);
213 }
214 }
Simon Hunt99c13842014-11-06 18:23:12 -0800215
Simon Hunt56d51852014-11-09 13:03:35 -0800216 function injectTestEvent(view) {
217 if (abortIfLive()) { return; }
218 var sc = scenario,
219 evn = ++sc.evNumber,
220 pfx = sc.evDir + sc.ctx + sc.evPrefix + evn,
221 onosUrl = pfx + sc.evOnos,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800222 uiUrl = pfx + sc.evUi,
223 stack = [
224 { url: onosUrl, cb: handleServerEvent },
225 { url: uiUrl, cb: handleUiEvent }
226 ];
227 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800228 }
229
Simon Hunt7cd48f32014-11-09 23:42:50 -0800230 function recurseFetchEvent(stack, evn) {
231 var v = scenario.view,
232 frame;
233 if (stack.length === 0) {
Simon Huntfc274c92014-11-11 11:05:46 -0800234 v.alert('Oops!\n\nNo event #' + evn + ' found.');
Simon Hunt7cd48f32014-11-09 23:42:50 -0800235 return;
236 }
237 frame = stack.shift();
238
239 d3.json(frame.url, function (err, data) {
Simon Hunt99c13842014-11-06 18:23:12 -0800240 if (err) {
Simon Hunt56d51852014-11-09 13:03:35 -0800241 if (err.status === 404) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800242 // if we didn't find the data, try the next stack frame
243 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800244 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800245 v.alert('non-404 error:\n\n' + frame.url + '\n\n' + err);
Simon Hunt56d51852014-11-09 13:03:35 -0800246 }
Simon Hunt99c13842014-11-06 18:23:12 -0800247 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800248 testDebug('loaded: ' + frame.url);
249 frame.cb(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800250 }
251 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800252
Simon Hunt56d51852014-11-09 13:03:35 -0800253 }
Simon Hunt50128c02014-11-08 13:36:15 -0800254
Simon Hunt56d51852014-11-09 13:03:35 -0800255 function handleUiEvent(data) {
Simon Huntbb282f52014-11-10 11:08:19 -0800256 scenario.view.alert('UI Tx: ' + data.event + '\n\n' +
257 JSON.stringify(data));
Simon Hunt56d51852014-11-09 13:03:35 -0800258 }
259
260 function injectStartupEvents(view) {
261 var last = scenario.params.lastAuto || 0;
262 if (abortIfLive()) { return; }
263
264 while (scenario.evNumber < last) {
Simon Hunt1a9eff92014-11-07 11:06:34 -0800265 injectTestEvent(view);
266 }
267 }
268
Simon Hunt934c3ce2014-11-05 11:45:07 -0800269 function toggleBg() {
270 var vis = bgImg.style('visibility');
271 bgImg.style('visibility', (vis === 'hidden') ? 'visible' : 'hidden');
272 }
273
Simon Hunt99c13842014-11-06 18:23:12 -0800274 function cycleLabels() {
Simon Huntbb282f52014-11-10 11:08:19 -0800275 deviceLabelIndex = (deviceLabelIndex === network.deviceLabelCount - 1)
276 ? 0 : deviceLabelIndex + 1;
Simon Hunt5f36d342014-11-08 21:33:14 -0800277
Simon Hunt99c13842014-11-06 18:23:12 -0800278 network.nodes.forEach(function (d) {
Simon Huntbb282f52014-11-10 11:08:19 -0800279 if (d.class === 'device') {
280 updateDeviceLabel(d);
281 }
Simon Hunt99c13842014-11-06 18:23:12 -0800282 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800283 }
284
285 function togglePorts(view) {
Simon Hunt50128c02014-11-08 13:36:15 -0800286 view.alert('togglePorts() callback')
Simon Hunt934c3ce2014-11-05 11:45:07 -0800287 }
288
289 function unpin(view) {
Simon Hunt50128c02014-11-08 13:36:15 -0800290 view.alert('unpin() callback')
Simon Hunt934c3ce2014-11-05 11:45:07 -0800291 }
292
293 // ==============================
294 // Radio Button Callbacks
295
Simon Hunt195cb382014-11-03 17:50:51 -0800296 function showAllLayers() {
Simon Hunt142d0032014-11-04 20:13:09 -0800297// network.node.classed('inactive', false);
298// network.link.classed('inactive', false);
299// d3.selectAll('svg .port').classed('inactive', false);
300// d3.selectAll('svg .portText').classed('inactive', false);
Simon Hunt934c3ce2014-11-05 11:45:07 -0800301 // TODO ...
Simon Hunt50128c02014-11-08 13:36:15 -0800302 network.view.alert('showAllLayers() callback');
Simon Hunt195cb382014-11-03 17:50:51 -0800303 }
304
305 function showPacketLayer() {
Simon Hunt934c3ce2014-11-05 11:45:07 -0800306 showAllLayers();
307 // TODO ...
Simon Hunt50128c02014-11-08 13:36:15 -0800308 network.view.alert('showPacketLayer() callback');
Simon Hunt195cb382014-11-03 17:50:51 -0800309 }
310
311 function showOpticalLayer() {
Simon Hunt934c3ce2014-11-05 11:45:07 -0800312 showAllLayers();
313 // TODO ...
Simon Hunt50128c02014-11-08 13:36:15 -0800314 network.view.alert('showOpticalLayer() callback');
Simon Hunt195cb382014-11-03 17:50:51 -0800315 }
316
Simon Hunt142d0032014-11-04 20:13:09 -0800317 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800318 // Private functions
319
Simon Hunt99c13842014-11-06 18:23:12 -0800320 function safeId(s) {
321 return s.replace(/[^a-z0-9]/gi, '-');
322 }
323
Simon Huntc7ee0662014-11-05 16:44:37 -0800324 // set the size of the given element to that of the view (reduced if padded)
325 function setSize(el, view, pad) {
326 var padding = pad ? pad * 2 : 0;
Simon Hunt934c3ce2014-11-05 11:45:07 -0800327 el.attr({
Simon Huntc7ee0662014-11-05 16:44:37 -0800328 width: view.width() - padding,
329 height: view.height() - padding
Simon Hunt934c3ce2014-11-05 11:45:07 -0800330 });
331 }
332
Simon Hunt934c3ce2014-11-05 11:45:07 -0800333
Simon Hunt99c13842014-11-06 18:23:12 -0800334 // ==============================
335 // Event handlers for server-pushed events
336
Simon Huntbb282f52014-11-10 11:08:19 -0800337 function logicError(msg) {
338 // TODO, report logic error to server, via websock, so it can be logged
339 network.view.alert('Logic Error:\n\n' + msg);
Simon Huntfc274c92014-11-11 11:05:46 -0800340 console.warn(msg);
Simon Huntbb282f52014-11-10 11:08:19 -0800341 }
342
Simon Hunt99c13842014-11-06 18:23:12 -0800343 var eventDispatch = {
344 addDevice: addDevice,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800345 addLink: addLink,
Simon Hunt56d51852014-11-09 13:03:35 -0800346 addHost: addHost,
Simon Huntbb282f52014-11-10 11:08:19 -0800347 updateDevice: updateDevice,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800348 updateLink: updateLink,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800349 updateHost: updateHost,
Simon Huntbb282f52014-11-10 11:08:19 -0800350 removeDevice: stillToImplement,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800351 removeLink: removeLink,
Simon Hunt44031102014-11-11 13:20:36 -0800352 removeHost: removeHost,
Simon Hunt61d04042014-11-11 17:27:16 -0800353 showDetails: showDetails,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800354 showPath: showPath
Simon Hunt99c13842014-11-06 18:23:12 -0800355 };
356
357 function addDevice(data) {
Simon Huntfc274c92014-11-11 11:05:46 -0800358 fnTrace('addDevice', data.payload.id);
Simon Hunt99c13842014-11-06 18:23:12 -0800359 var device = data.payload,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800360 nodeData = createDeviceNode(device);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800361 network.nodes.push(nodeData);
362 network.lookup[nodeData.id] = nodeData;
Simon Hunt99c13842014-11-06 18:23:12 -0800363 updateNodes();
364 network.force.start();
365 }
366
Simon Hunt99c13842014-11-06 18:23:12 -0800367 function addLink(data) {
Simon Huntfc274c92014-11-11 11:05:46 -0800368 fnTrace('addLink', data.payload.id);
Simon Hunt99c13842014-11-06 18:23:12 -0800369 var link = data.payload,
370 lnk = createLink(link);
Simon Hunt99c13842014-11-06 18:23:12 -0800371 if (lnk) {
Simon Hunt99c13842014-11-06 18:23:12 -0800372 network.links.push(lnk);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800373 network.lookup[lnk.id] = lnk;
Simon Hunt99c13842014-11-06 18:23:12 -0800374 updateLinks();
375 network.force.start();
376 }
377 }
378
Simon Hunt56d51852014-11-09 13:03:35 -0800379 function addHost(data) {
Simon Huntfc274c92014-11-11 11:05:46 -0800380 fnTrace('addHost', data.payload.id);
Simon Hunt56d51852014-11-09 13:03:35 -0800381 var host = data.payload,
382 node = createHostNode(host),
383 lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800384 network.nodes.push(node);
385 network.lookup[host.id] = node;
386 updateNodes();
387
388 lnk = createHostLink(host);
389 if (lnk) {
Simon Hunt44031102014-11-11 13:20:36 -0800390 node.linkData = lnk; // cache ref on its host
Simon Hunt56d51852014-11-09 13:03:35 -0800391 network.links.push(lnk);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800392 network.lookup[host.ingress] = lnk;
393 network.lookup[host.egress] = lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800394 updateLinks();
395 }
396 network.force.start();
397 }
398
Simon Hunt44031102014-11-11 13:20:36 -0800399 // TODO: fold updateX(...) methods into one base method; remove duplication
Simon Huntbb282f52014-11-10 11:08:19 -0800400 function updateDevice(data) {
Simon Huntfc274c92014-11-11 11:05:46 -0800401 fnTrace('updateDevice', data.payload.id);
Simon Huntbb282f52014-11-10 11:08:19 -0800402 var device = data.payload,
403 id = device.id,
404 nodeData = network.lookup[id];
405 if (nodeData) {
406 $.extend(nodeData, device);
407 updateDeviceState(nodeData);
408 } else {
409 logicError('updateDevice lookup fail. ID = "' + id + '"');
410 }
411 }
412
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800413 function updateLink(data) {
Simon Huntfc274c92014-11-11 11:05:46 -0800414 fnTrace('updateLink', data.payload.id);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800415 var link = data.payload,
416 id = link.id,
417 linkData = network.lookup[id];
418 if (linkData) {
419 $.extend(linkData, link);
420 updateLinkState(linkData);
421 } else {
422 logicError('updateLink lookup fail. ID = "' + id + '"');
423 }
424 }
425
Simon Hunt7cd48f32014-11-09 23:42:50 -0800426 function updateHost(data) {
Simon Huntfc274c92014-11-11 11:05:46 -0800427 fnTrace('updateHost', data.payload.id);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800428 var host = data.payload,
Simon Huntbb282f52014-11-10 11:08:19 -0800429 id = host.id,
430 hostData = network.lookup[id];
431 if (hostData) {
432 $.extend(hostData, host);
433 updateHostState(hostData);
434 } else {
435 logicError('updateHost lookup fail. ID = "' + id + '"');
436 }
Simon Hunt7cd48f32014-11-09 23:42:50 -0800437 }
438
Simon Hunt44031102014-11-11 13:20:36 -0800439 // TODO: fold removeX(...) methods into base method - remove dup code
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800440 function removeLink(data) {
Simon Huntfc274c92014-11-11 11:05:46 -0800441 fnTrace('removeLink', data.payload.id);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800442 var link = data.payload,
443 id = link.id,
444 linkData = network.lookup[id];
445 if (linkData) {
446 removeLinkElement(linkData);
447 } else {
448 logicError('removeLink lookup fail. ID = "' + id + '"');
449 }
450 }
451
Simon Hunt44031102014-11-11 13:20:36 -0800452 function removeHost(data) {
453 fnTrace('removeHost', data.payload.id);
454 var host = data.payload,
455 id = host.id,
456 hostData = network.lookup[id];
457 if (hostData) {
458 removeHostElement(hostData);
459 } else {
460 logicError('removeHost lookup fail. ID = "' + id + '"');
461 }
462 }
463
Simon Hunt61d04042014-11-11 17:27:16 -0800464 function showDetails(data) {
465 fnTrace('showDetails', data.payload.id);
466 populateDetails(data.payload);
467 detailPane.show();
468 }
469
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800470 function showPath(data) {
Simon Huntfc274c92014-11-11 11:05:46 -0800471 fnTrace('showPath', data.payload.id);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800472 var links = data.payload.links,
473 s = [ data.event + "\n" + links.length ];
474 links.forEach(function (d, i) {
475 s.push(d);
476 });
477 network.view.alert(s.join('\n'));
478
479 links.forEach(function (d, i) {
480 var link = network.lookup[d];
481 if (link) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800482 link.el.classed('showPath', true);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800483 }
484 });
485
486 // TODO: add selection-highlite lines to links
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800487 }
488
Simon Hunt56d51852014-11-09 13:03:35 -0800489 // ...............................
490
491 function stillToImplement(data) {
492 var p = data.payload;
493 note(data.event, p.id);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800494 network.view.alert('Not yet implemented: "' + data.event + '"');
Simon Hunt56d51852014-11-09 13:03:35 -0800495 }
Simon Hunt99c13842014-11-06 18:23:12 -0800496
497 function unknownEvent(data) {
Simon Hunt50128c02014-11-08 13:36:15 -0800498 network.view.alert('Unknown event type: "' + data.event + '"');
Simon Hunt99c13842014-11-06 18:23:12 -0800499 }
500
501 function handleServerEvent(data) {
502 var fn = eventDispatch[data.event] || unknownEvent;
503 fn(data);
504 }
505
506 // ==============================
Simon Hunt61d04042014-11-11 17:27:16 -0800507 // Out-going messages...
508
509 function getSel(idx) {
510 return selections[selectOrder[idx]];
511 }
512
513 // for now, just a host-to-host intent, (and implicit start-monitoring)
514 function requestPath() {
515 var payload = {
516 one: getSel(0).obj.id,
517 two: getSel(1).obj.id
518 };
519 sendMessage('requestPath', payload);
520 }
521
Simon Huntac9e24f2014-11-12 10:12:21 -0800522 function cancelMonitor() {
523 var payload = {
524 id: "need_the_intent_id" // FIXME: where are we storing this?
525 };
526 sendMessage('cancelMonitor', payload);
527 }
528
Simon Hunt61d04042014-11-11 17:27:16 -0800529 // request details for the selected element
530 function requestDetails() {
531 var data = getSel(0).obj,
532 payload = {
533 id: data.id,
534 class: data.class
535 };
536 sendMessage('requestDetails', payload);
537 }
538
539 // ==============================
Simon Hunt99c13842014-11-06 18:23:12 -0800540 // force layout modification functions
541
542 function translate(x, y) {
543 return 'translate(' + x + ',' + y + ')';
544 }
545
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800546 function missMsg(what, id) {
547 return '\n[' + what + '] "' + id + '" missing ';
548 }
549
550 function linkEndPoints(srcId, dstId) {
551 var srcNode = network.lookup[srcId],
552 dstNode = network.lookup[dstId],
553 sMiss = !srcNode ? missMsg('src', srcId) : '',
554 dMiss = !dstNode ? missMsg('dst', dstId) : '';
555
556 if (sMiss || dMiss) {
557 logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
558 return null;
559 }
560 return {
561 source: srcNode,
562 target: dstNode,
563 x1: srcNode.x,
564 y1: srcNode.y,
565 x2: dstNode.x,
566 y2: dstNode.y
567 };
568 }
569
Simon Hunt56d51852014-11-09 13:03:35 -0800570 function createHostLink(host) {
571 var src = host.id,
572 dst = host.cp.device,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800573 id = host.ingress,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800574 lnk = linkEndPoints(src, dst);
Simon Hunt56d51852014-11-09 13:03:35 -0800575
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800576 if (!lnk) {
Simon Hunt56d51852014-11-09 13:03:35 -0800577 return null;
578 }
579
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800580 // Synthesize link ...
581 $.extend(lnk, {
Thomas Vachuska4830d392014-11-09 17:09:56 -0800582 id: id,
Simon Hunt56d51852014-11-09 13:03:35 -0800583 class: 'link',
Simon Hunt7cd48f32014-11-09 23:42:50 -0800584 type: 'hostLink',
Simon Hunt56d51852014-11-09 13:03:35 -0800585 svgClass: 'link hostLink',
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800586 linkWidth: 1
Simon Hunt7cd48f32014-11-09 23:42:50 -0800587 });
Simon Hunt99c13842014-11-06 18:23:12 -0800588 return lnk;
589 }
590
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800591 function createLink(link) {
592 var lnk = linkEndPoints(link.src, link.dst),
593 type = link.type;
594
595 if (!lnk) {
596 return null;
597 }
598
599 // merge in remaining data
600 $.extend(lnk, link, {
601 class: 'link',
602 svgClass: type ? 'link ' + type : 'link'
603 });
604 return lnk;
Simon Hunt1a9eff92014-11-07 11:06:34 -0800605 }
606
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800607 var widthRatio = 1.4,
608 linkScale = d3.scale.linear()
609 .domain([1, 12])
610 .range([widthRatio, 12 * widthRatio])
611 .clamp(true);
612
613 function updateLinkWidth (d) {
614 // TODO: watch out for .showPath/.showTraffic classes
615 d.el.transition()
616 .duration(1000)
617 .attr('stroke-width', linkScale(d.linkWidth));
618 }
619
620
Simon Hunt99c13842014-11-06 18:23:12 -0800621 function updateLinks() {
622 link = linkG.selectAll('.link')
623 .data(network.links, function (d) { return d.id; });
624
625 // operate on existing links, if necessary
626 // link .foo() .bar() ...
627
628 // operate on entering links:
629 var entering = link.enter()
630 .append('line')
631 .attr({
Simon Hunt99c13842014-11-06 18:23:12 -0800632 class: function (d) { return d.svgClass; },
633 x1: function (d) { return d.x1; },
634 y1: function (d) { return d.y1; },
635 x2: function (d) { return d.x2; },
636 y2: function (d) { return d.y2; },
Simon Hunt1a9eff92014-11-07 11:06:34 -0800637 stroke: config.topo.linkInColor,
638 'stroke-width': config.topo.linkInWidth
Simon Hunt99c13842014-11-06 18:23:12 -0800639 })
640 .transition().duration(1000)
641 .attr({
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800642 'stroke-width': function (d) { return linkScale(d.linkWidth); },
Simon Hunt99c13842014-11-06 18:23:12 -0800643 stroke: '#666' // TODO: remove explicit stroke, rather...
644 });
645
646 // augment links
Simon Hunt7cd48f32014-11-09 23:42:50 -0800647 entering.each(function (d) {
648 var link = d3.select(this);
649 // provide ref to element selection from backing data....
650 d.el = link;
Simon Hunt99c13842014-11-06 18:23:12 -0800651
Simon Hunt7cd48f32014-11-09 23:42:50 -0800652 // TODO: add src/dst port labels etc.
653 });
Thomas Vachuska4830d392014-11-09 17:09:56 -0800654
655 // operate on both existing and new links, if necessary
656 //link .foo() .bar() ...
657
658 // operate on exiting links:
Thomas Vachuska4830d392014-11-09 17:09:56 -0800659 link.exit()
Thomas Vachuska4830d392014-11-09 17:09:56 -0800660 .attr({
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800661 'stroke-dasharray': '3, 3'
Thomas Vachuska4830d392014-11-09 17:09:56 -0800662 })
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800663 .style('opacity', 0.4)
664 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -0800665 .duration(1500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800666 .attr({
667 'stroke-dasharray': '3, 12'
668 })
669 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -0800670 .duration(500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800671 .style('opacity', 0.0)
Thomas Vachuska4830d392014-11-09 17:09:56 -0800672 .remove();
Simon Hunt99c13842014-11-06 18:23:12 -0800673 }
674
675 function createDeviceNode(device) {
676 // start with the object as is
677 var node = device,
Simon Huntbb282f52014-11-10 11:08:19 -0800678 type = device.type,
679 svgCls = type ? 'node device ' + type : 'node device';
Simon Hunt99c13842014-11-06 18:23:12 -0800680
681 // Augment as needed...
682 node.class = 'device';
Simon Huntbb282f52014-11-10 11:08:19 -0800683 node.svgClass = device.online ? svgCls + ' online' : svgCls;
Simon Hunt99c13842014-11-06 18:23:12 -0800684 positionNode(node);
685
686 // cache label array length
687 network.deviceLabelCount = device.labels.length;
Simon Hunt99c13842014-11-06 18:23:12 -0800688 return node;
689 }
690
Simon Hunt56d51852014-11-09 13:03:35 -0800691 function createHostNode(host) {
692 // start with the object as is
693 var node = host;
694
695 // Augment as needed...
696 node.class = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -0800697 if (!node.type) {
698 // TODO: perhaps type would be: {phone, tablet, laptop, endstation} ?
699 node.type = 'endstation';
700 }
Simon Hunt56d51852014-11-09 13:03:35 -0800701 node.svgClass = 'node host';
702 // TODO: consider placing near its switch, if [x,y] not defined
703 positionNode(node);
704
705 // cache label array length
706 network.hostLabelCount = host.labels.length;
Simon Hunt56d51852014-11-09 13:03:35 -0800707 return node;
708 }
709
Simon Hunt99c13842014-11-06 18:23:12 -0800710 function positionNode(node) {
711 var meta = node.metaUi,
Simon Huntac9e24f2014-11-12 10:12:21 -0800712 x = meta && meta.x,
713 y = meta && meta.y,
714 xy;
Simon Hunt99c13842014-11-06 18:23:12 -0800715
Simon Huntac9e24f2014-11-12 10:12:21 -0800716 // If we have [x,y] already, use that...
Simon Hunt99c13842014-11-06 18:23:12 -0800717 if (x && y) {
718 node.fixed = true;
Simon Huntac9e24f2014-11-12 10:12:21 -0800719 node.x = x;
720 node.y = y;
721 return;
Simon Hunt99c13842014-11-06 18:23:12 -0800722 }
Simon Huntac9e24f2014-11-12 10:12:21 -0800723
724 // Note: Placing incoming unpinned nodes at exactly the same point
725 // (center of the view) causes them to explode outwards when
726 // the force layout kicks in. So, we spread them out a bit
727 // initially, to provide a more serene layout convergence.
728 // Additionally, if the node is a host, we place it near
729 // the device it is connected to.
730
731 function spread(s) {
732 return Math.floor((Math.random() * s) - s/2);
733 }
734
735 function randDim(dim) {
736 return dim / 2 + spread(dim * 0.7071);
737 }
738
739 function rand() {
740 return {
741 x: randDim(network.view.width()),
742 y: randDim(network.view.height())
743 };
744 }
745
746 function near(node) {
747 var min = 12,
748 dx = spread(12),
749 dy = spread(12);
750 return {
751 x: node.x + min + dx,
752 y: node.y + min + dy
753 };
754 }
755
756 function getDevice(cp) {
757 var d = network.lookup[cp.device];
758 return d || rand();
759 }
760
761 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
762 $.extend(node, xy);
Simon Hunt99c13842014-11-06 18:23:12 -0800763 }
764
Simon Hunt99c13842014-11-06 18:23:12 -0800765 function iconUrl(d) {
766 return 'img/' + d.type + '.png';
767 }
768
769 // returns the newly computed bounding box of the rectangle
770 function adjustRectToFitText(n) {
771 var text = n.select('text'),
772 box = text.node().getBBox(),
773 lab = config.labels;
774
775 text.attr('text-anchor', 'middle')
776 .attr('y', '-0.8em')
777 .attr('x', lab.imgPad/2);
778
779 // translate the bbox so that it is centered on [x,y]
780 box.x = -box.width / 2;
781 box.y = -box.height / 2;
782
783 // add padding
784 box.x -= (lab.padLR + lab.imgPad/2);
785 box.width += lab.padLR * 2 + lab.imgPad;
786 box.y -= lab.padTB;
787 box.height += lab.padTB * 2;
788
789 return box;
790 }
791
Simon Hunt1a9eff92014-11-07 11:06:34 -0800792 function mkSvgClass(d) {
793 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
794 }
795
Simon Hunt7cd48f32014-11-09 23:42:50 -0800796 function hostLabel(d) {
797 var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0;
798 return d.labels[idx];
799 }
800 function deviceLabel(d) {
801 var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0;
802 return d.labels[idx];
803 }
804 function niceLabel(label) {
805 return (label && label.trim()) ? label : '.';
806 }
807
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800808 function updateDeviceLabel(d) {
809 var label = niceLabel(deviceLabel(d)),
810 node = d.el,
811 box;
812
813 node.select('text')
814 .text(label)
815 .style('opacity', 0)
816 .transition()
817 .style('opacity', 1);
818
819 box = adjustRectToFitText(node);
820
821 node.select('rect')
822 .transition()
823 .attr(box);
824
825 node.select('image')
826 .transition()
827 .attr('x', box.x + config.icons.xoff)
828 .attr('y', box.y + config.icons.yoff);
829 }
830
831 function updateHostLabel(d) {
832 var label = hostLabel(d),
833 host = d.el;
834
835 host.select('text').text(label);
836 }
837
Simon Huntbb282f52014-11-10 11:08:19 -0800838 function updateDeviceState(nodeData) {
839 nodeData.el.classed('online', nodeData.online);
840 updateDeviceLabel(nodeData);
841 // TODO: review what else might need to be updated
842 }
843
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800844 function updateLinkState(linkData) {
845 updateLinkWidth(linkData);
846 // TODO: review what else might need to be updated
847 // update label, if showing
848 }
849
Simon Huntbb282f52014-11-10 11:08:19 -0800850 function updateHostState(hostData) {
851 updateHostLabel(hostData);
852 // TODO: review what else might need to be updated
853 }
854
855
Simon Hunt99c13842014-11-06 18:23:12 -0800856 function updateNodes() {
857 node = nodeG.selectAll('.node')
858 .data(network.nodes, function (d) { return d.id; });
859
860 // operate on existing nodes, if necessary
Simon Hunt7cd48f32014-11-09 23:42:50 -0800861 // update host labels
Simon Hunt99c13842014-11-06 18:23:12 -0800862 //node .foo() .bar() ...
863
864 // operate on entering nodes:
865 var entering = node.enter()
866 .append('g')
867 .attr({
868 id: function (d) { return safeId(d.id); },
Simon Hunt1a9eff92014-11-07 11:06:34 -0800869 class: mkSvgClass,
Simon Hunt99c13842014-11-06 18:23:12 -0800870 transform: function (d) { return translate(d.x, d.y); },
871 opacity: 0
872 })
Simon Hunt1a9eff92014-11-07 11:06:34 -0800873 .call(network.drag)
Simon Hunt99c13842014-11-06 18:23:12 -0800874 //.on('mouseover', function (d) {})
875 //.on('mouseover', function (d) {})
876 .transition()
877 .attr('opacity', 1);
878
879 // augment device nodes...
880 entering.filter('.device').each(function (d) {
881 var node = d3.select(this),
882 icon = iconUrl(d),
Simon Hunt7cd48f32014-11-09 23:42:50 -0800883 label = niceLabel(deviceLabel(d)),
Simon Hunt99c13842014-11-06 18:23:12 -0800884 box;
885
Simon Hunt7cd48f32014-11-09 23:42:50 -0800886 // provide ref to element from backing data....
887 d.el = node;
888
Simon Hunt99c13842014-11-06 18:23:12 -0800889 node.append('rect')
890 .attr({
891 'rx': 5,
892 'ry': 5
893 });
894
895 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -0800896 .text(label)
Simon Hunt99c13842014-11-06 18:23:12 -0800897 .attr('dy', '1.1em');
898
899 box = adjustRectToFitText(node);
900
901 node.select('rect')
902 .attr(box);
903
904 if (icon) {
905 var cfg = config.icons;
906 node.append('svg:image')
907 .attr({
908 x: box.x + config.icons.xoff,
909 y: box.y + config.icons.yoff,
910 width: cfg.w,
911 height: cfg.h,
912 'xlink:href': icon
913 });
914 }
915
916 // debug function to show the modelled x,y coordinates of nodes...
917 if (debug('showNodeXY')) {
918 node.select('rect').attr('fill-opacity', 0.5);
919 node.append('circle')
920 .attr({
921 class: 'debug',
922 cx: 0,
923 cy: 0,
924 r: '3px'
925 });
Simon Huntc7ee0662014-11-05 16:44:37 -0800926 }
927 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800928
Simon Hunt56d51852014-11-09 13:03:35 -0800929 // augment host nodes...
930 entering.filter('.host').each(function (d) {
931 var node = d3.select(this),
Simon Hunt56d51852014-11-09 13:03:35 -0800932 box;
933
Simon Hunt7cd48f32014-11-09 23:42:50 -0800934 // provide ref to element from backing data....
935 d.el = node;
936
Simon Hunt56d51852014-11-09 13:03:35 -0800937 node.append('circle')
938 .attr('r', 8); // TODO: define host circle radius
939
940 // TODO: are we attaching labels to hosts?
941 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -0800942 .text(hostLabel)
943 .attr('dy', '1.3em')
944 .attr('text-anchor', 'middle');
Simon Hunt56d51852014-11-09 13:03:35 -0800945
946 // debug function to show the modelled x,y coordinates of nodes...
947 if (debug('showNodeXY')) {
948 node.select('circle').attr('fill-opacity', 0.5);
949 node.append('circle')
950 .attr({
951 class: 'debug',
952 cx: 0,
953 cy: 0,
954 r: '3px'
955 });
956 }
957 });
Simon Huntc7ee0662014-11-05 16:44:37 -0800958
Simon Hunt99c13842014-11-06 18:23:12 -0800959 // operate on both existing and new nodes, if necessary
960 //node .foo() .bar() ...
Simon Huntc7ee0662014-11-05 16:44:37 -0800961
Simon Hunt99c13842014-11-06 18:23:12 -0800962 // operate on exiting nodes:
Simon Huntea80eb42014-11-11 13:46:57 -0800963 // Note that the node is removed after 2 seconds.
964 // Sub element animations should be shorter than 2 seconds.
965 var exiting = node.exit()
Simon Hunt44031102014-11-11 13:20:36 -0800966 .transition()
967 .duration(2000)
Simon Huntea80eb42014-11-11 13:46:57 -0800968 .style('opacity', 0)
Simon Hunt99c13842014-11-06 18:23:12 -0800969 .remove();
Simon Huntea80eb42014-11-11 13:46:57 -0800970
971 // host node exits....
972 exiting.filter('.host').each(function (d) {
973 var node = d3.select(this);
974
975 node.select('text')
976 .style('opacity', 0.5)
977 .transition()
978 .duration(1000)
979 .style('opacity', 0);
980 // note, leave <g>.remove to remove this element
981
982 node.select('circle')
983 .style('stroke-fill', '#555')
984 .style('fill', '#888')
985 .style('opacity', 0.5)
986 .transition()
987 .duration(1500)
988 .attr('r', 0);
989 // note, leave <g>.remove to remove this element
990
991 });
992
993 // TODO: device node exits
Simon Huntc7ee0662014-11-05 16:44:37 -0800994 }
995
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800996 function find(id, array) {
997 for (var idx = 0, n = array.length; idx < n; idx++) {
998 if (array[idx].id === id) {
999 return idx;
1000 }
1001 }
1002 return -1;
1003 }
1004
1005 function removeLinkElement(linkData) {
1006 // remove from lookup cache
1007 delete network.lookup[linkData.id];
1008 // remove from links array
1009 var idx = find(linkData.id, network.links);
Simon Huntfc274c92014-11-11 11:05:46 -08001010 network.links.splice(idx, 1);
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001011 // remove from SVG
1012 updateLinks();
Simon Hunt44031102014-11-11 13:20:36 -08001013 network.force.resume();
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001014 }
Simon Huntc7ee0662014-11-05 16:44:37 -08001015
Simon Hunt44031102014-11-11 13:20:36 -08001016 function removeHostElement(hostData) {
1017 // first, remove associated hostLink...
1018 removeLinkElement(hostData.linkData);
1019
1020 // remove from lookup cache
1021 delete network.lookup[hostData.id];
1022 // remove from nodes array
1023 var idx = find(hostData.id, network.nodes);
1024 network.nodes.splice(idx, 1);
1025 // remove from SVG
1026 updateNodes();
1027 network.force.resume();
1028 }
1029
1030
Simon Huntc7ee0662014-11-05 16:44:37 -08001031 function tick() {
1032 node.attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001033 transform: function (d) { return translate(d.x, d.y); }
Simon Huntc7ee0662014-11-05 16:44:37 -08001034 });
1035
1036 link.attr({
1037 x1: function (d) { return d.source.x; },
1038 y1: function (d) { return d.source.y; },
1039 x2: function (d) { return d.target.x; },
1040 y2: function (d) { return d.target.y; }
1041 });
1042 }
Simon Hunt934c3ce2014-11-05 11:45:07 -08001043
1044 // ==============================
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001045 // Web-Socket for live data
1046
1047 function webSockUrl() {
1048 return document.location.toString()
1049 .replace(/\#.*/, '')
1050 .replace('http://', 'ws://')
1051 .replace('https://', 'wss://')
1052 .replace('index2.html', config.webSockUrl);
1053 }
1054
1055 webSock = {
1056 ws : null,
1057
1058 connect : function() {
1059 webSock.ws = new WebSocket(webSockUrl());
1060
1061 webSock.ws.onopen = function() {
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001062 };
1063
1064 webSock.ws.onmessage = function(m) {
1065 if (m.data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001066 wsTraceRx(m.data);
Thomas Vachuskad472c6e2014-11-07 19:11:05 -08001067 handleServerEvent(JSON.parse(m.data));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001068 }
1069 };
1070
1071 webSock.ws.onclose = function(m) {
1072 webSock.ws = null;
1073 };
1074 },
1075
1076 send : function(text) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001077 if (text != null) {
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001078 webSock._send(text);
1079 }
1080 },
1081
1082 _send : function(message) {
1083 if (webSock.ws) {
1084 webSock.ws.send(message);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001085 } else {
Simon Hunt56d51852014-11-09 13:03:35 -08001086 network.view.alert('no web socket open\n\n' + message);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001087 }
1088 }
1089
1090 };
1091
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001092 var sid = 0;
1093
Simon Hunt61d04042014-11-11 17:27:16 -08001094 // TODO: use cache of pending messages (key = sid) to reconcile responses
1095
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001096 function sendMessage(evType, payload) {
1097 var toSend = {
Simon Huntbb282f52014-11-10 11:08:19 -08001098 event: evType,
1099 sid: ++sid,
1100 payload: payload
1101 },
1102 asText = JSON.stringify(toSend);
1103 wsTraceTx(asText);
1104 webSock.send(asText);
1105 }
1106
1107 function wsTraceTx(msg) {
1108 wsTrace('tx', msg);
1109 }
1110 function wsTraceRx(msg) {
1111 wsTrace('rx', msg);
1112 }
1113 function wsTrace(rxtx, msg) {
Simon Huntbb282f52014-11-10 11:08:19 -08001114 console.log('[' + rxtx + '] ' + msg);
1115 // TODO: integrate with trace view
1116 //if (trace) {
1117 // trace.output(rxtx, msg);
1118 //}
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001119 }
1120
1121
1122 // ==============================
1123 // Selection stuff
1124
1125 function selectObject(obj, el) {
1126 var n,
1127 meta = d3.event.sourceEvent.metaKey;
1128
1129 if (el) {
1130 n = d3.select(el);
1131 } else {
1132 node.each(function(d) {
1133 if (d == obj) {
1134 n = d3.select(el = this);
1135 }
1136 });
1137 }
1138 if (!n) return;
1139
1140 if (meta && n.classed('selected')) {
1141 deselectObject(obj.id);
Simon Hunt61d04042014-11-11 17:27:16 -08001142 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001143 return;
1144 }
1145
1146 if (!meta) {
1147 deselectAll();
1148 }
1149
Simon Hunt5f36d342014-11-08 21:33:14 -08001150 selections[obj.id] = { obj: obj, el : el};
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001151 selectOrder.push(obj.id);
1152
1153 n.classed('selected', true);
Simon Hunt61d04042014-11-11 17:27:16 -08001154 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001155 }
1156
1157 function deselectObject(id) {
1158 var obj = selections[id];
1159 if (obj) {
1160 d3.select(obj.el).classed('selected', false);
Simon Hunt61d04042014-11-11 17:27:16 -08001161 delete selections[id];
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001162 }
Simon Hunt61d04042014-11-11 17:27:16 -08001163 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001164 }
1165
1166 function deselectAll() {
1167 // deselect all nodes in the network...
1168 node.classed('selected', false);
1169 selections = {};
1170 selectOrder = [];
Simon Hunt61d04042014-11-11 17:27:16 -08001171 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001172 }
1173
Simon Hunt61d04042014-11-11 17:27:16 -08001174 // FIXME: this click handler does not get unloaded when the view does
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001175 $('#view').on('click', function(e) {
1176 if (!$(e.target).closest('.node').length) {
1177 if (!e.metaKey) {
1178 deselectAll();
1179 }
1180 }
1181 });
1182
Simon Hunt61d04042014-11-11 17:27:16 -08001183 // update the state of the detail pane, based on current selections
1184 function updateDetailPane() {
1185 var nSel = selectOrder.length;
1186 if (!nSel) {
1187 detailPane.hide();
1188 } else if (nSel === 1) {
1189 singleSelect();
1190 } else {
1191 multiSelect();
1192 }
1193 }
1194
1195 function singleSelect() {
1196 requestDetails();
1197 // NOTE: detail pane will be shown from showDetails event.
1198 }
1199
1200 function multiSelect() {
1201 // TODO: use detail pane for multi-select view.
1202 //detailPane.show();
1203 }
1204
1205 function populateDetails(data) {
1206 detailPane.empty();
1207
1208 var title = detailPane.append("h2"),
1209 table = detailPane.append("table"),
1210 tbody = table.append("tbody");
1211
1212 $('<img src="img/' + data.type + '.png">').appendTo(title);
1213 $('<span>').attr('class', 'icon').text(data.id).appendTo(title);
1214
1215 data.propOrder.forEach(function(p) {
1216 if (p === '-') {
1217 addSep(tbody);
1218 } else {
1219 addProp(tbody, p, data.props[p]);
1220 }
1221 });
1222
1223 function addSep(tbody) {
1224 var tr = tbody.append('tr');
1225 $('<hr>').appendTo(tr.append('td').attr('colspan', 2));
1226 }
1227
1228 function addProp(tbody, label, value) {
1229 var tr = tbody.append('tr');
1230
1231 tr.append('td')
1232 .attr('class', 'label')
1233 .text(label + ' :');
1234
1235 tr.append('td')
1236 .attr('class', 'value')
1237 .text(value);
1238 }
1239 }
1240
1241 // ==============================
1242 // Test harness code
Simon Hunt56d51852014-11-09 13:03:35 -08001243
1244 function prepareScenario(view, ctx, dbg) {
1245 var sc = scenario,
1246 urlSc = sc.evDir + ctx + sc.evScenario;
1247
1248 if (!ctx) {
1249 view.alert("No scenario specified (null ctx)");
1250 return;
1251 }
1252
1253 sc.view = view;
1254 sc.ctx = ctx;
1255 sc.debug = dbg;
1256 sc.evNumber = 0;
1257
1258 d3.json(urlSc, function(err, data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001259 var p = data && data.params || {},
1260 desc = data && data.description || null,
Simon Huntfc274c92014-11-11 11:05:46 -08001261 intro = data && data.title;
Simon Huntbb282f52014-11-10 11:08:19 -08001262
Simon Hunt56d51852014-11-09 13:03:35 -08001263 if (err) {
1264 view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err);
1265 } else {
1266 sc.params = p;
Simon Huntbb282f52014-11-10 11:08:19 -08001267 if (desc) {
1268 intro += '\n\n ' + desc.join('\n ');
1269 }
1270 view.alert(intro);
Simon Hunt56d51852014-11-09 13:03:35 -08001271 }
1272 });
1273
1274 }
1275
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001276 // ==============================
Simon Hunt142d0032014-11-04 20:13:09 -08001277 // View life-cycle callbacks
Simon Hunt195cb382014-11-03 17:50:51 -08001278
Simon Huntf67722a2014-11-10 09:32:06 -08001279 function preload(view, ctx, flags) {
Simon Hunt142d0032014-11-04 20:13:09 -08001280 var w = view.width(),
1281 h = view.height(),
1282 idBg = view.uid('bg'),
Simon Huntc7ee0662014-11-05 16:44:37 -08001283 showBg = config.options.showBackground ? 'visible' : 'hidden',
1284 fcfg = config.force,
1285 fpad = fcfg.pad,
1286 forceDim = [w - 2*fpad, h - 2*fpad];
Simon Hunt195cb382014-11-03 17:50:51 -08001287
Simon Huntbb282f52014-11-10 11:08:19 -08001288 // TODO: set trace api
1289 //trace = onos.exported.webSockTrace;
1290
Simon Hunt142d0032014-11-04 20:13:09 -08001291 // NOTE: view.$div is a D3 selection of the view's div
1292 svg = view.$div.append('svg');
Simon Hunt934c3ce2014-11-05 11:45:07 -08001293 setSize(svg, view);
1294
Simon Hunt1a9eff92014-11-07 11:06:34 -08001295 // add blue glow filter to svg layer
1296 d3u.appendGlow(svg);
1297
Simon Hunt142d0032014-11-04 20:13:09 -08001298 // load the background image
1299 bgImg = svg.append('svg:image')
Simon Hunt195cb382014-11-03 17:50:51 -08001300 .attr({
Simon Hunt142d0032014-11-04 20:13:09 -08001301 id: idBg,
1302 width: w,
1303 height: h,
Simon Hunt195cb382014-11-03 17:50:51 -08001304 'xlink:href': config.backgroundUrl
1305 })
Simon Hunt142d0032014-11-04 20:13:09 -08001306 .style({
1307 visibility: showBg
Simon Hunt195cb382014-11-03 17:50:51 -08001308 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001309
1310 // group for the topology
1311 topoG = svg.append('g')
1312 .attr('transform', fcfg.translate());
1313
1314 // subgroups for links and nodes
1315 linkG = topoG.append('g').attr('id', 'links');
1316 nodeG = topoG.append('g').attr('id', 'nodes');
1317
1318 // selection of nodes and links
1319 link = linkG.selectAll('.link');
1320 node = nodeG.selectAll('.node');
1321
Simon Hunt7cd48f32014-11-09 23:42:50 -08001322 function chrg(d) {
1323 return fcfg.charge[d.class] || -12000;
1324 }
Simon Hunt99c13842014-11-06 18:23:12 -08001325 function ldist(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08001326 return fcfg.linkDistance[d.type] || 50;
Simon Hunt99c13842014-11-06 18:23:12 -08001327 }
1328 function lstrg(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08001329 // 0.0 - 1.0
1330 return fcfg.linkStrength[d.type] || 1.0;
Simon Hunt99c13842014-11-06 18:23:12 -08001331 }
1332
Simon Hunt1a9eff92014-11-07 11:06:34 -08001333 function selectCb(d, self) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001334 selectObject(d, self);
Simon Hunt1a9eff92014-11-07 11:06:34 -08001335 }
1336
1337 function atDragEnd(d, self) {
Simon Hunt56d51852014-11-09 13:03:35 -08001338 // once we've finished moving, pin the node in position
1339 d.fixed = true;
1340 d3.select(self).classed('fixed', true);
1341 if (config.useLiveData) {
Simon Hunt902c9922014-11-11 11:59:31 -08001342 sendUpdateMeta(d);
Simon Hunt1a9eff92014-11-07 11:06:34 -08001343 }
1344 }
1345
Simon Hunt902c9922014-11-11 11:59:31 -08001346 function sendUpdateMeta(d) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001347 sendMessage('updateMeta', {
1348 id: d.id,
1349 'class': d.class,
Simon Hunt902c9922014-11-11 11:59:31 -08001350 'memento': {
1351 x: Math.floor(d.x),
1352 y: Math.floor(d.y)
1353 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001354 });
1355 }
1356
Simon Huntc7ee0662014-11-05 16:44:37 -08001357 // set up the force layout
1358 network.force = d3.layout.force()
1359 .size(forceDim)
1360 .nodes(network.nodes)
1361 .links(network.links)
Simon Hunt7cd48f32014-11-09 23:42:50 -08001362 .gravity(0.4)
1363 .friction(0.7)
1364 .charge(chrg)
Simon Hunt99c13842014-11-06 18:23:12 -08001365 .linkDistance(ldist)
1366 .linkStrength(lstrg)
Simon Huntc7ee0662014-11-05 16:44:37 -08001367 .on('tick', tick);
Simon Hunt195cb382014-11-03 17:50:51 -08001368
Simon Hunt1a9eff92014-11-07 11:06:34 -08001369 network.drag = d3u.createDragBehavior(network.force, selectCb, atDragEnd);
1370 }
Simon Hunt195cb382014-11-03 17:50:51 -08001371
Simon Hunt56d51852014-11-09 13:03:35 -08001372 function load(view, ctx, flags) {
Simon Huntf67722a2014-11-10 09:32:06 -08001373 // resize, in case the window was resized while we were not loaded
1374 resize(view, ctx, flags);
1375
Simon Hunt99c13842014-11-06 18:23:12 -08001376 // cache the view token, so network topo functions can access it
1377 network.view = view;
Simon Hunt56d51852014-11-09 13:03:35 -08001378 config.useLiveData = !flags.local;
1379
1380 if (!config.useLiveData) {
1381 prepareScenario(view, ctx, flags.debug);
1382 }
Simon Hunt99c13842014-11-06 18:23:12 -08001383
1384 // set our radio buttons and key bindings
Simon Hunt934c3ce2014-11-05 11:45:07 -08001385 view.setRadio(btnSet);
1386 view.setKeys(keyDispatch);
Simon Hunt195cb382014-11-03 17:50:51 -08001387
Simon Hunt50128c02014-11-08 13:36:15 -08001388 if (config.useLiveData) {
1389 webSock.connect();
1390 }
Simon Hunt195cb382014-11-03 17:50:51 -08001391 }
1392
Simon Huntf67722a2014-11-10 09:32:06 -08001393 function resize(view, ctx, flags) {
Simon Hunt934c3ce2014-11-05 11:45:07 -08001394 setSize(svg, view);
1395 setSize(bgImg, view);
Simon Hunt99c13842014-11-06 18:23:12 -08001396
1397 // TODO: hook to recompute layout, perhaps? work with zoom/pan code
1398 // adjust force layout size
Simon Hunt142d0032014-11-04 20:13:09 -08001399 }
1400
1401
1402 // ==============================
1403 // View registration
Simon Hunt195cb382014-11-03 17:50:51 -08001404
Simon Hunt25248912014-11-04 11:25:48 -08001405 onos.ui.addView('topo', {
Simon Hunt142d0032014-11-04 20:13:09 -08001406 preload: preload,
1407 load: load,
1408 resize: resize
Simon Hunt195cb382014-11-03 17:50:51 -08001409 });
1410
Simon Hunt61d04042014-11-11 17:27:16 -08001411 detailPane = onos.ui.addFloatingPanel('topo-detail');
1412
Simon Hunt195cb382014-11-03 17:50:51 -08001413}(ONOS));