blob: 8a3bc5d3dc9cc7a85b817d08780e085542117650 [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 Huntb53e0682014-11-12 13:32:01 -0800130 W: requestTraffic, // bag of selections
131 Z: requestPath, // host-to-host intent (and monitor)
Simon Huntac9e24f2014-11-12 10:12:21 -0800132 X: cancelMonitor
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,
Simon Hunt61d04042014-11-11 17:27:16 -0800158 detailPane,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800159 selectOrder = [],
160 selections = {},
161
Simon Hunt195cb382014-11-03 17:50:51 -0800162 highlighted = null,
163 hovered = null,
164 viewMode = 'showAll',
165 portLabelsOn = false;
166
Simon Hunt934c3ce2014-11-05 11:45:07 -0800167 // D3 selections
168 var svg,
169 bgImg,
Simon Huntc7ee0662014-11-05 16:44:37 -0800170 topoG,
171 nodeG,
172 linkG,
173 node,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800174 link,
175 mask;
Simon Hunt195cb382014-11-03 17:50:51 -0800176
Simon Hunt142d0032014-11-04 20:13:09 -0800177 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800178 // For Debugging / Development
Simon Hunt195cb382014-11-03 17:50:51 -0800179
Simon Hunt99c13842014-11-06 18:23:12 -0800180 function note(label, msg) {
181 console.log('NOTE: ' + label + ': ' + msg);
Simon Hunt195cb382014-11-03 17:50:51 -0800182 }
183
Simon Hunt99c13842014-11-06 18:23:12 -0800184 function debug(what) {
185 return config.debugOn && config.debug[what];
Simon Hunt934c3ce2014-11-05 11:45:07 -0800186 }
187
Simon Huntfc274c92014-11-11 11:05:46 -0800188 function fnTrace(msg, id) {
189 if (config.fnTrace) {
190 console.log('FN: ' + msg + ' [' + id + ']');
191 }
192 }
Simon Hunt99c13842014-11-06 18:23:12 -0800193
Simon Hunt934c3ce2014-11-05 11:45:07 -0800194 // ==============================
195 // Key Callbacks
196
Simon Hunt99c13842014-11-06 18:23:12 -0800197 function testMe(view) {
Simon Hunt50128c02014-11-08 13:36:15 -0800198 view.alert('test');
Simon Hunt99c13842014-11-06 18:23:12 -0800199 }
200
Simon Hunt56d51852014-11-09 13:03:35 -0800201 function abortIfLive() {
Simon Hunt50128c02014-11-08 13:36:15 -0800202 if (config.useLiveData) {
Simon Huntb53e0682014-11-12 13:32:01 -0800203 network.view.alert("Sorry, currently using live data..");
Simon Hunt56d51852014-11-09 13:03:35 -0800204 return true;
Simon Hunt50128c02014-11-08 13:36:15 -0800205 }
Simon Hunt56d51852014-11-09 13:03:35 -0800206 return false;
207 }
Simon Hunt50128c02014-11-08 13:36:15 -0800208
Simon Hunt56d51852014-11-09 13:03:35 -0800209 function testDebug(msg) {
210 if (scenario.debug) {
211 scenario.view.alert(msg);
212 }
213 }
Simon Hunt99c13842014-11-06 18:23:12 -0800214
Simon Hunt56d51852014-11-09 13:03:35 -0800215 function injectTestEvent(view) {
216 if (abortIfLive()) { return; }
217 var sc = scenario,
218 evn = ++sc.evNumber,
219 pfx = sc.evDir + sc.ctx + sc.evPrefix + evn,
220 onosUrl = pfx + sc.evOnos,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800221 uiUrl = pfx + sc.evUi,
222 stack = [
223 { url: onosUrl, cb: handleServerEvent },
224 { url: uiUrl, cb: handleUiEvent }
225 ];
226 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800227 }
228
Simon Hunt7cd48f32014-11-09 23:42:50 -0800229 function recurseFetchEvent(stack, evn) {
230 var v = scenario.view,
231 frame;
232 if (stack.length === 0) {
Simon Huntfc274c92014-11-11 11:05:46 -0800233 v.alert('Oops!\n\nNo event #' + evn + ' found.');
Simon Hunt7cd48f32014-11-09 23:42:50 -0800234 return;
235 }
236 frame = stack.shift();
237
238 d3.json(frame.url, function (err, data) {
Simon Hunt99c13842014-11-06 18:23:12 -0800239 if (err) {
Simon Hunt56d51852014-11-09 13:03:35 -0800240 if (err.status === 404) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800241 // if we didn't find the data, try the next stack frame
242 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800243 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800244 v.alert('non-404 error:\n\n' + frame.url + '\n\n' + err);
Simon Hunt56d51852014-11-09 13:03:35 -0800245 }
Simon Hunt99c13842014-11-06 18:23:12 -0800246 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800247 testDebug('loaded: ' + frame.url);
248 frame.cb(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800249 }
250 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800251
Simon Hunt56d51852014-11-09 13:03:35 -0800252 }
Simon Hunt50128c02014-11-08 13:36:15 -0800253
Simon Hunt56d51852014-11-09 13:03:35 -0800254 function handleUiEvent(data) {
Simon Huntbb282f52014-11-10 11:08:19 -0800255 scenario.view.alert('UI Tx: ' + data.event + '\n\n' +
256 JSON.stringify(data));
Simon Hunt56d51852014-11-09 13:03:35 -0800257 }
258
259 function injectStartupEvents(view) {
260 var last = scenario.params.lastAuto || 0;
261 if (abortIfLive()) { return; }
262
263 while (scenario.evNumber < last) {
Simon Hunt1a9eff92014-11-07 11:06:34 -0800264 injectTestEvent(view);
265 }
266 }
267
Simon Hunt934c3ce2014-11-05 11:45:07 -0800268 function toggleBg() {
269 var vis = bgImg.style('visibility');
270 bgImg.style('visibility', (vis === 'hidden') ? 'visible' : 'hidden');
271 }
272
Simon Hunt99c13842014-11-06 18:23:12 -0800273 function cycleLabels() {
Simon Huntbb282f52014-11-10 11:08:19 -0800274 deviceLabelIndex = (deviceLabelIndex === network.deviceLabelCount - 1)
275 ? 0 : deviceLabelIndex + 1;
Simon Hunt5f36d342014-11-08 21:33:14 -0800276
Simon Hunt99c13842014-11-06 18:23:12 -0800277 network.nodes.forEach(function (d) {
Simon Huntbb282f52014-11-10 11:08:19 -0800278 if (d.class === 'device') {
279 updateDeviceLabel(d);
280 }
Simon Hunt99c13842014-11-06 18:23:12 -0800281 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800282 }
283
284 function togglePorts(view) {
Simon Hunt50128c02014-11-08 13:36:15 -0800285 view.alert('togglePorts() callback')
Simon Hunt934c3ce2014-11-05 11:45:07 -0800286 }
287
288 function unpin(view) {
Simon Hunt50128c02014-11-08 13:36:15 -0800289 view.alert('unpin() callback')
Simon Hunt934c3ce2014-11-05 11:45:07 -0800290 }
291
292 // ==============================
293 // Radio Button Callbacks
294
Simon Hunt195cb382014-11-03 17:50:51 -0800295 function showAllLayers() {
Simon Hunt142d0032014-11-04 20:13:09 -0800296// network.node.classed('inactive', false);
297// network.link.classed('inactive', false);
298// d3.selectAll('svg .port').classed('inactive', false);
299// d3.selectAll('svg .portText').classed('inactive', false);
Simon Hunt934c3ce2014-11-05 11:45:07 -0800300 // TODO ...
Simon Hunt50128c02014-11-08 13:36:15 -0800301 network.view.alert('showAllLayers() callback');
Simon Hunt195cb382014-11-03 17:50:51 -0800302 }
303
304 function showPacketLayer() {
Simon Hunt934c3ce2014-11-05 11:45:07 -0800305 showAllLayers();
306 // TODO ...
Simon Hunt50128c02014-11-08 13:36:15 -0800307 network.view.alert('showPacketLayer() callback');
Simon Hunt195cb382014-11-03 17:50:51 -0800308 }
309
310 function showOpticalLayer() {
Simon Hunt934c3ce2014-11-05 11:45:07 -0800311 showAllLayers();
312 // TODO ...
Simon Hunt50128c02014-11-08 13:36:15 -0800313 network.view.alert('showOpticalLayer() callback');
Simon Hunt195cb382014-11-03 17:50:51 -0800314 }
315
Simon Hunt142d0032014-11-04 20:13:09 -0800316 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800317 // Private functions
318
Simon Hunt99c13842014-11-06 18:23:12 -0800319 function safeId(s) {
320 return s.replace(/[^a-z0-9]/gi, '-');
321 }
322
Simon Huntc7ee0662014-11-05 16:44:37 -0800323 // set the size of the given element to that of the view (reduced if padded)
324 function setSize(el, view, pad) {
325 var padding = pad ? pad * 2 : 0;
Simon Hunt934c3ce2014-11-05 11:45:07 -0800326 el.attr({
Simon Huntc7ee0662014-11-05 16:44:37 -0800327 width: view.width() - padding,
328 height: view.height() - padding
Simon Hunt934c3ce2014-11-05 11:45:07 -0800329 });
330 }
331
Simon Hunt934c3ce2014-11-05 11:45:07 -0800332
Simon Hunt99c13842014-11-06 18:23:12 -0800333 // ==============================
334 // Event handlers for server-pushed events
335
Simon Huntbb282f52014-11-10 11:08:19 -0800336 function logicError(msg) {
337 // TODO, report logic error to server, via websock, so it can be logged
338 network.view.alert('Logic Error:\n\n' + msg);
Simon Huntfc274c92014-11-11 11:05:46 -0800339 console.warn(msg);
Simon Huntbb282f52014-11-10 11:08:19 -0800340 }
341
Simon Hunt99c13842014-11-06 18:23:12 -0800342 var eventDispatch = {
343 addDevice: addDevice,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800344 addLink: addLink,
Simon Hunt56d51852014-11-09 13:03:35 -0800345 addHost: addHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800346
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 Huntb53e0682014-11-12 13:32:01 -0800350
Simon Huntbb282f52014-11-10 11:08:19 -0800351 removeDevice: stillToImplement,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800352 removeLink: removeLink,
Simon Hunt44031102014-11-11 13:20:36 -0800353 removeHost: removeHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800354
Simon Hunt61d04042014-11-11 17:27:16 -0800355 showDetails: showDetails,
Simon Huntb53e0682014-11-12 13:32:01 -0800356 showPath: showPath,
357 showTraffic: showTraffic
Simon Hunt99c13842014-11-06 18:23:12 -0800358 };
359
360 function addDevice(data) {
Simon Huntfc274c92014-11-11 11:05:46 -0800361 fnTrace('addDevice', data.payload.id);
Simon Hunt99c13842014-11-06 18:23:12 -0800362 var device = data.payload,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800363 nodeData = createDeviceNode(device);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800364 network.nodes.push(nodeData);
365 network.lookup[nodeData.id] = nodeData;
Simon Hunt99c13842014-11-06 18:23:12 -0800366 updateNodes();
367 network.force.start();
368 }
369
Simon Hunt99c13842014-11-06 18:23:12 -0800370 function addLink(data) {
Simon Huntfc274c92014-11-11 11:05:46 -0800371 fnTrace('addLink', data.payload.id);
Simon Hunt99c13842014-11-06 18:23:12 -0800372 var link = data.payload,
373 lnk = createLink(link);
Simon Hunt99c13842014-11-06 18:23:12 -0800374 if (lnk) {
Simon Hunt99c13842014-11-06 18:23:12 -0800375 network.links.push(lnk);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800376 network.lookup[lnk.id] = lnk;
Simon Hunt99c13842014-11-06 18:23:12 -0800377 updateLinks();
378 network.force.start();
379 }
380 }
381
Simon Hunt56d51852014-11-09 13:03:35 -0800382 function addHost(data) {
Simon Huntfc274c92014-11-11 11:05:46 -0800383 fnTrace('addHost', data.payload.id);
Simon Hunt56d51852014-11-09 13:03:35 -0800384 var host = data.payload,
385 node = createHostNode(host),
386 lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800387 network.nodes.push(node);
388 network.lookup[host.id] = node;
389 updateNodes();
390
391 lnk = createHostLink(host);
392 if (lnk) {
Simon Hunt44031102014-11-11 13:20:36 -0800393 node.linkData = lnk; // cache ref on its host
Simon Hunt56d51852014-11-09 13:03:35 -0800394 network.links.push(lnk);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800395 network.lookup[host.ingress] = lnk;
396 network.lookup[host.egress] = lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800397 updateLinks();
398 }
399 network.force.start();
400 }
401
Simon Hunt44031102014-11-11 13:20:36 -0800402 // TODO: fold updateX(...) methods into one base method; remove duplication
Simon Huntbb282f52014-11-10 11:08:19 -0800403 function updateDevice(data) {
Simon Huntfc274c92014-11-11 11:05:46 -0800404 fnTrace('updateDevice', data.payload.id);
Simon Huntbb282f52014-11-10 11:08:19 -0800405 var device = data.payload,
406 id = device.id,
407 nodeData = network.lookup[id];
408 if (nodeData) {
409 $.extend(nodeData, device);
410 updateDeviceState(nodeData);
411 } else {
412 logicError('updateDevice lookup fail. ID = "' + id + '"');
413 }
414 }
415
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800416 function updateLink(data) {
Simon Huntfc274c92014-11-11 11:05:46 -0800417 fnTrace('updateLink', data.payload.id);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800418 var link = data.payload,
419 id = link.id,
420 linkData = network.lookup[id];
421 if (linkData) {
422 $.extend(linkData, link);
423 updateLinkState(linkData);
424 } else {
425 logicError('updateLink lookup fail. ID = "' + id + '"');
426 }
427 }
428
Simon Hunt7cd48f32014-11-09 23:42:50 -0800429 function updateHost(data) {
Simon Huntfc274c92014-11-11 11:05:46 -0800430 fnTrace('updateHost', data.payload.id);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800431 var host = data.payload,
Simon Huntbb282f52014-11-10 11:08:19 -0800432 id = host.id,
433 hostData = network.lookup[id];
434 if (hostData) {
435 $.extend(hostData, host);
436 updateHostState(hostData);
437 } else {
438 logicError('updateHost lookup fail. ID = "' + id + '"');
439 }
Simon Hunt7cd48f32014-11-09 23:42:50 -0800440 }
441
Simon Hunt44031102014-11-11 13:20:36 -0800442 // TODO: fold removeX(...) methods into base method - remove dup code
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800443 function removeLink(data) {
Simon Huntfc274c92014-11-11 11:05:46 -0800444 fnTrace('removeLink', data.payload.id);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800445 var link = data.payload,
446 id = link.id,
447 linkData = network.lookup[id];
448 if (linkData) {
449 removeLinkElement(linkData);
450 } else {
451 logicError('removeLink lookup fail. ID = "' + id + '"');
452 }
453 }
454
Simon Hunt44031102014-11-11 13:20:36 -0800455 function removeHost(data) {
456 fnTrace('removeHost', data.payload.id);
457 var host = data.payload,
458 id = host.id,
459 hostData = network.lookup[id];
460 if (hostData) {
461 removeHostElement(hostData);
462 } else {
463 logicError('removeHost lookup fail. ID = "' + id + '"');
464 }
465 }
466
Simon Hunt61d04042014-11-11 17:27:16 -0800467 function showDetails(data) {
468 fnTrace('showDetails', data.payload.id);
469 populateDetails(data.payload);
Simon Huntb53e0682014-11-12 13:32:01 -0800470 // TODO: Add single-select actions ...
Simon Hunt61d04042014-11-11 17:27:16 -0800471 detailPane.show();
472 }
473
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800474 function showPath(data) {
Simon Huntfc274c92014-11-11 11:05:46 -0800475 fnTrace('showPath', data.payload.id);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800476 var links = data.payload.links,
477 s = [ data.event + "\n" + links.length ];
478 links.forEach(function (d, i) {
479 s.push(d);
480 });
481 network.view.alert(s.join('\n'));
482
483 links.forEach(function (d, i) {
484 var link = network.lookup[d];
485 if (link) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800486 link.el.classed('showPath', true);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800487 }
488 });
489
490 // TODO: add selection-highlite lines to links
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800491 }
492
Simon Huntb53e0682014-11-12 13:32:01 -0800493 function showTraffic(data) {
494 network.view.alert("showTraffic() -- TODO")
495 }
496
Simon Hunt56d51852014-11-09 13:03:35 -0800497 // ...............................
498
499 function stillToImplement(data) {
500 var p = data.payload;
501 note(data.event, p.id);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800502 network.view.alert('Not yet implemented: "' + data.event + '"');
Simon Hunt56d51852014-11-09 13:03:35 -0800503 }
Simon Hunt99c13842014-11-06 18:23:12 -0800504
505 function unknownEvent(data) {
Simon Hunt50128c02014-11-08 13:36:15 -0800506 network.view.alert('Unknown event type: "' + data.event + '"');
Simon Hunt99c13842014-11-06 18:23:12 -0800507 }
508
509 function handleServerEvent(data) {
510 var fn = eventDispatch[data.event] || unknownEvent;
511 fn(data);
512 }
513
514 // ==============================
Simon Hunt61d04042014-11-11 17:27:16 -0800515 // Out-going messages...
516
Simon Huntb53e0682014-11-12 13:32:01 -0800517 function userFeedback(msg) {
518 // for now, use the alert pane as is. Maybe different alert style in
519 // the future (centered on view; dismiss button?)
520 network.view.alert(msg);
521 }
522
523 function nSel() {
524 return selectOrder.length;
525 }
Simon Hunt61d04042014-11-11 17:27:16 -0800526 function getSel(idx) {
527 return selections[selectOrder[idx]];
528 }
Simon Huntb53e0682014-11-12 13:32:01 -0800529 function getSelId(idx) {
530 return getSel(idx).obj.id;
531 }
532 function allSelectionsClass(cls) {
533 for (var i=0, n=nSel(); i<n; i++) {
534 if (getSel(i).obj.class !== cls) {
535 return false;
536 }
537 }
538 return true;
539 }
Simon Hunt61d04042014-11-11 17:27:16 -0800540
Simon Huntb53e0682014-11-12 13:32:01 -0800541 function requestTraffic() {
542 if (nSel() > 0) {
543 sendMessage('requestTraffic', {
544 ids: selectOrder
545 });
546 } else {
547 userFeedback('Request-Traffic requires one or\n' +
548 'more items to be selected.');
549 }
550 }
551
Simon Hunt61d04042014-11-11 17:27:16 -0800552 function requestPath() {
Simon Huntb53e0682014-11-12 13:32:01 -0800553 if (nSel() === 2 && allSelectionsClass('host')) {
554 sendMessage('requestPath', {
555 one: getSelId(0),
556 two: getSelId(1)
557 });
558 } else {
559 userFeedback('Request-Path requires two\n' +
560 'hosts to be selected.');
561 }
Simon Hunt61d04042014-11-11 17:27:16 -0800562 }
563
Simon Huntac9e24f2014-11-12 10:12:21 -0800564 function cancelMonitor() {
Simon Huntb53e0682014-11-12 13:32:01 -0800565 // FIXME: from where do we get the intent id(s) to send to the server?
566 sendMessage('cancelMonitor', {
567 ids: ["need_the_intent_id"]
568 });
Simon Huntac9e24f2014-11-12 10:12:21 -0800569 }
570
Simon Hunt61d04042014-11-11 17:27:16 -0800571 // request details for the selected element
572 function requestDetails() {
573 var data = getSel(0).obj,
574 payload = {
575 id: data.id,
576 class: data.class
577 };
578 sendMessage('requestDetails', payload);
579 }
580
581 // ==============================
Simon Hunt99c13842014-11-06 18:23:12 -0800582 // force layout modification functions
583
584 function translate(x, y) {
585 return 'translate(' + x + ',' + y + ')';
586 }
587
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800588 function missMsg(what, id) {
589 return '\n[' + what + '] "' + id + '" missing ';
590 }
591
592 function linkEndPoints(srcId, dstId) {
593 var srcNode = network.lookup[srcId],
594 dstNode = network.lookup[dstId],
595 sMiss = !srcNode ? missMsg('src', srcId) : '',
596 dMiss = !dstNode ? missMsg('dst', dstId) : '';
597
598 if (sMiss || dMiss) {
599 logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
600 return null;
601 }
602 return {
603 source: srcNode,
604 target: dstNode,
605 x1: srcNode.x,
606 y1: srcNode.y,
607 x2: dstNode.x,
608 y2: dstNode.y
609 };
610 }
611
Simon Hunt56d51852014-11-09 13:03:35 -0800612 function createHostLink(host) {
613 var src = host.id,
614 dst = host.cp.device,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800615 id = host.ingress,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800616 lnk = linkEndPoints(src, dst);
Simon Hunt56d51852014-11-09 13:03:35 -0800617
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800618 if (!lnk) {
Simon Hunt56d51852014-11-09 13:03:35 -0800619 return null;
620 }
621
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800622 // Synthesize link ...
623 $.extend(lnk, {
Thomas Vachuska4830d392014-11-09 17:09:56 -0800624 id: id,
Simon Hunt56d51852014-11-09 13:03:35 -0800625 class: 'link',
Simon Hunt7cd48f32014-11-09 23:42:50 -0800626 type: 'hostLink',
Simon Hunt56d51852014-11-09 13:03:35 -0800627 svgClass: 'link hostLink',
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800628 linkWidth: 1
Simon Hunt7cd48f32014-11-09 23:42:50 -0800629 });
Simon Hunt99c13842014-11-06 18:23:12 -0800630 return lnk;
631 }
632
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800633 function createLink(link) {
634 var lnk = linkEndPoints(link.src, link.dst),
635 type = link.type;
636
637 if (!lnk) {
638 return null;
639 }
640
641 // merge in remaining data
642 $.extend(lnk, link, {
643 class: 'link',
644 svgClass: type ? 'link ' + type : 'link'
645 });
646 return lnk;
Simon Hunt1a9eff92014-11-07 11:06:34 -0800647 }
648
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800649 var widthRatio = 1.4,
650 linkScale = d3.scale.linear()
651 .domain([1, 12])
652 .range([widthRatio, 12 * widthRatio])
653 .clamp(true);
654
655 function updateLinkWidth (d) {
656 // TODO: watch out for .showPath/.showTraffic classes
657 d.el.transition()
658 .duration(1000)
659 .attr('stroke-width', linkScale(d.linkWidth));
660 }
661
662
Simon Hunt99c13842014-11-06 18:23:12 -0800663 function updateLinks() {
664 link = linkG.selectAll('.link')
665 .data(network.links, function (d) { return d.id; });
666
667 // operate on existing links, if necessary
668 // link .foo() .bar() ...
669
670 // operate on entering links:
671 var entering = link.enter()
672 .append('line')
673 .attr({
Simon Hunt99c13842014-11-06 18:23:12 -0800674 class: function (d) { return d.svgClass; },
675 x1: function (d) { return d.x1; },
676 y1: function (d) { return d.y1; },
677 x2: function (d) { return d.x2; },
678 y2: function (d) { return d.y2; },
Simon Hunt1a9eff92014-11-07 11:06:34 -0800679 stroke: config.topo.linkInColor,
680 'stroke-width': config.topo.linkInWidth
Simon Hunt99c13842014-11-06 18:23:12 -0800681 })
682 .transition().duration(1000)
683 .attr({
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800684 'stroke-width': function (d) { return linkScale(d.linkWidth); },
Simon Hunt99c13842014-11-06 18:23:12 -0800685 stroke: '#666' // TODO: remove explicit stroke, rather...
686 });
687
688 // augment links
Simon Hunt7cd48f32014-11-09 23:42:50 -0800689 entering.each(function (d) {
690 var link = d3.select(this);
691 // provide ref to element selection from backing data....
692 d.el = link;
Simon Hunt99c13842014-11-06 18:23:12 -0800693
Simon Hunt7cd48f32014-11-09 23:42:50 -0800694 // TODO: add src/dst port labels etc.
695 });
Thomas Vachuska4830d392014-11-09 17:09:56 -0800696
697 // operate on both existing and new links, if necessary
698 //link .foo() .bar() ...
699
700 // operate on exiting links:
Thomas Vachuska4830d392014-11-09 17:09:56 -0800701 link.exit()
Thomas Vachuska4830d392014-11-09 17:09:56 -0800702 .attr({
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800703 'stroke-dasharray': '3, 3'
Thomas Vachuska4830d392014-11-09 17:09:56 -0800704 })
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800705 .style('opacity', 0.4)
706 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -0800707 .duration(1500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800708 .attr({
709 'stroke-dasharray': '3, 12'
710 })
711 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -0800712 .duration(500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800713 .style('opacity', 0.0)
Thomas Vachuska4830d392014-11-09 17:09:56 -0800714 .remove();
Simon Hunt99c13842014-11-06 18:23:12 -0800715 }
716
717 function createDeviceNode(device) {
718 // start with the object as is
719 var node = device,
Simon Huntbb282f52014-11-10 11:08:19 -0800720 type = device.type,
721 svgCls = type ? 'node device ' + type : 'node device';
Simon Hunt99c13842014-11-06 18:23:12 -0800722
723 // Augment as needed...
724 node.class = 'device';
Simon Huntbb282f52014-11-10 11:08:19 -0800725 node.svgClass = device.online ? svgCls + ' online' : svgCls;
Simon Hunt99c13842014-11-06 18:23:12 -0800726 positionNode(node);
727
728 // cache label array length
729 network.deviceLabelCount = device.labels.length;
Simon Hunt99c13842014-11-06 18:23:12 -0800730 return node;
731 }
732
Simon Hunt56d51852014-11-09 13:03:35 -0800733 function createHostNode(host) {
734 // start with the object as is
735 var node = host;
736
737 // Augment as needed...
738 node.class = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -0800739 if (!node.type) {
740 // TODO: perhaps type would be: {phone, tablet, laptop, endstation} ?
741 node.type = 'endstation';
742 }
Simon Hunt56d51852014-11-09 13:03:35 -0800743 node.svgClass = 'node host';
744 // TODO: consider placing near its switch, if [x,y] not defined
745 positionNode(node);
746
747 // cache label array length
748 network.hostLabelCount = host.labels.length;
Simon Hunt56d51852014-11-09 13:03:35 -0800749 return node;
750 }
751
Simon Hunt99c13842014-11-06 18:23:12 -0800752 function positionNode(node) {
753 var meta = node.metaUi,
Simon Huntac9e24f2014-11-12 10:12:21 -0800754 x = meta && meta.x,
755 y = meta && meta.y,
756 xy;
Simon Hunt99c13842014-11-06 18:23:12 -0800757
Simon Huntac9e24f2014-11-12 10:12:21 -0800758 // If we have [x,y] already, use that...
Simon Hunt99c13842014-11-06 18:23:12 -0800759 if (x && y) {
760 node.fixed = true;
Simon Huntac9e24f2014-11-12 10:12:21 -0800761 node.x = x;
762 node.y = y;
763 return;
Simon Hunt99c13842014-11-06 18:23:12 -0800764 }
Simon Huntac9e24f2014-11-12 10:12:21 -0800765
766 // Note: Placing incoming unpinned nodes at exactly the same point
767 // (center of the view) causes them to explode outwards when
768 // the force layout kicks in. So, we spread them out a bit
769 // initially, to provide a more serene layout convergence.
770 // Additionally, if the node is a host, we place it near
771 // the device it is connected to.
772
773 function spread(s) {
774 return Math.floor((Math.random() * s) - s/2);
775 }
776
777 function randDim(dim) {
778 return dim / 2 + spread(dim * 0.7071);
779 }
780
781 function rand() {
782 return {
783 x: randDim(network.view.width()),
784 y: randDim(network.view.height())
785 };
786 }
787
788 function near(node) {
789 var min = 12,
790 dx = spread(12),
791 dy = spread(12);
792 return {
793 x: node.x + min + dx,
794 y: node.y + min + dy
795 };
796 }
797
798 function getDevice(cp) {
799 var d = network.lookup[cp.device];
800 return d || rand();
801 }
802
803 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
804 $.extend(node, xy);
Simon Hunt99c13842014-11-06 18:23:12 -0800805 }
806
Simon Hunt99c13842014-11-06 18:23:12 -0800807 function iconUrl(d) {
808 return 'img/' + d.type + '.png';
809 }
810
811 // returns the newly computed bounding box of the rectangle
812 function adjustRectToFitText(n) {
813 var text = n.select('text'),
814 box = text.node().getBBox(),
815 lab = config.labels;
816
817 text.attr('text-anchor', 'middle')
818 .attr('y', '-0.8em')
819 .attr('x', lab.imgPad/2);
820
821 // translate the bbox so that it is centered on [x,y]
822 box.x = -box.width / 2;
823 box.y = -box.height / 2;
824
825 // add padding
826 box.x -= (lab.padLR + lab.imgPad/2);
827 box.width += lab.padLR * 2 + lab.imgPad;
828 box.y -= lab.padTB;
829 box.height += lab.padTB * 2;
830
831 return box;
832 }
833
Simon Hunt1a9eff92014-11-07 11:06:34 -0800834 function mkSvgClass(d) {
835 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
836 }
837
Simon Hunt7cd48f32014-11-09 23:42:50 -0800838 function hostLabel(d) {
839 var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0;
840 return d.labels[idx];
841 }
842 function deviceLabel(d) {
843 var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0;
844 return d.labels[idx];
845 }
846 function niceLabel(label) {
847 return (label && label.trim()) ? label : '.';
848 }
849
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800850 function updateDeviceLabel(d) {
851 var label = niceLabel(deviceLabel(d)),
852 node = d.el,
853 box;
854
855 node.select('text')
856 .text(label)
857 .style('opacity', 0)
858 .transition()
859 .style('opacity', 1);
860
861 box = adjustRectToFitText(node);
862
863 node.select('rect')
864 .transition()
865 .attr(box);
866
867 node.select('image')
868 .transition()
869 .attr('x', box.x + config.icons.xoff)
870 .attr('y', box.y + config.icons.yoff);
871 }
872
873 function updateHostLabel(d) {
874 var label = hostLabel(d),
875 host = d.el;
876
877 host.select('text').text(label);
878 }
879
Simon Huntbb282f52014-11-10 11:08:19 -0800880 function updateDeviceState(nodeData) {
881 nodeData.el.classed('online', nodeData.online);
882 updateDeviceLabel(nodeData);
883 // TODO: review what else might need to be updated
884 }
885
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800886 function updateLinkState(linkData) {
887 updateLinkWidth(linkData);
888 // TODO: review what else might need to be updated
889 // update label, if showing
890 }
891
Simon Huntbb282f52014-11-10 11:08:19 -0800892 function updateHostState(hostData) {
893 updateHostLabel(hostData);
894 // TODO: review what else might need to be updated
895 }
896
897
Simon Hunt99c13842014-11-06 18:23:12 -0800898 function updateNodes() {
899 node = nodeG.selectAll('.node')
900 .data(network.nodes, function (d) { return d.id; });
901
902 // operate on existing nodes, if necessary
Simon Hunt7cd48f32014-11-09 23:42:50 -0800903 // update host labels
Simon Hunt99c13842014-11-06 18:23:12 -0800904 //node .foo() .bar() ...
905
906 // operate on entering nodes:
907 var entering = node.enter()
908 .append('g')
909 .attr({
910 id: function (d) { return safeId(d.id); },
Simon Hunt1a9eff92014-11-07 11:06:34 -0800911 class: mkSvgClass,
Simon Hunt99c13842014-11-06 18:23:12 -0800912 transform: function (d) { return translate(d.x, d.y); },
913 opacity: 0
914 })
Simon Hunt1a9eff92014-11-07 11:06:34 -0800915 .call(network.drag)
Simon Hunt99c13842014-11-06 18:23:12 -0800916 //.on('mouseover', function (d) {})
917 //.on('mouseover', function (d) {})
918 .transition()
919 .attr('opacity', 1);
920
921 // augment device nodes...
922 entering.filter('.device').each(function (d) {
923 var node = d3.select(this),
924 icon = iconUrl(d),
Simon Hunt7cd48f32014-11-09 23:42:50 -0800925 label = niceLabel(deviceLabel(d)),
Simon Hunt99c13842014-11-06 18:23:12 -0800926 box;
927
Simon Hunt7cd48f32014-11-09 23:42:50 -0800928 // provide ref to element from backing data....
929 d.el = node;
930
Simon Hunt99c13842014-11-06 18:23:12 -0800931 node.append('rect')
932 .attr({
933 'rx': 5,
934 'ry': 5
935 });
936
937 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -0800938 .text(label)
Simon Hunt99c13842014-11-06 18:23:12 -0800939 .attr('dy', '1.1em');
940
941 box = adjustRectToFitText(node);
942
943 node.select('rect')
944 .attr(box);
945
946 if (icon) {
947 var cfg = config.icons;
948 node.append('svg:image')
949 .attr({
950 x: box.x + config.icons.xoff,
951 y: box.y + config.icons.yoff,
952 width: cfg.w,
953 height: cfg.h,
954 'xlink:href': icon
955 });
956 }
957
958 // debug function to show the modelled x,y coordinates of nodes...
959 if (debug('showNodeXY')) {
960 node.select('rect').attr('fill-opacity', 0.5);
961 node.append('circle')
962 .attr({
963 class: 'debug',
964 cx: 0,
965 cy: 0,
966 r: '3px'
967 });
Simon Huntc7ee0662014-11-05 16:44:37 -0800968 }
969 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800970
Simon Hunt56d51852014-11-09 13:03:35 -0800971 // augment host nodes...
972 entering.filter('.host').each(function (d) {
973 var node = d3.select(this),
Simon Hunt56d51852014-11-09 13:03:35 -0800974 box;
975
Simon Hunt7cd48f32014-11-09 23:42:50 -0800976 // provide ref to element from backing data....
977 d.el = node;
978
Simon Hunt56d51852014-11-09 13:03:35 -0800979 node.append('circle')
980 .attr('r', 8); // TODO: define host circle radius
981
982 // TODO: are we attaching labels to hosts?
983 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -0800984 .text(hostLabel)
985 .attr('dy', '1.3em')
986 .attr('text-anchor', 'middle');
Simon Hunt56d51852014-11-09 13:03:35 -0800987
988 // debug function to show the modelled x,y coordinates of nodes...
989 if (debug('showNodeXY')) {
990 node.select('circle').attr('fill-opacity', 0.5);
991 node.append('circle')
992 .attr({
993 class: 'debug',
994 cx: 0,
995 cy: 0,
996 r: '3px'
997 });
998 }
999 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001000
Simon Hunt99c13842014-11-06 18:23:12 -08001001 // operate on both existing and new nodes, if necessary
1002 //node .foo() .bar() ...
Simon Huntc7ee0662014-11-05 16:44:37 -08001003
Simon Hunt99c13842014-11-06 18:23:12 -08001004 // operate on exiting nodes:
Simon Huntea80eb42014-11-11 13:46:57 -08001005 // Note that the node is removed after 2 seconds.
1006 // Sub element animations should be shorter than 2 seconds.
1007 var exiting = node.exit()
Simon Hunt44031102014-11-11 13:20:36 -08001008 .transition()
1009 .duration(2000)
Simon Huntea80eb42014-11-11 13:46:57 -08001010 .style('opacity', 0)
Simon Hunt99c13842014-11-06 18:23:12 -08001011 .remove();
Simon Huntea80eb42014-11-11 13:46:57 -08001012
1013 // host node exits....
1014 exiting.filter('.host').each(function (d) {
1015 var node = d3.select(this);
1016
1017 node.select('text')
1018 .style('opacity', 0.5)
1019 .transition()
1020 .duration(1000)
1021 .style('opacity', 0);
1022 // note, leave <g>.remove to remove this element
1023
1024 node.select('circle')
1025 .style('stroke-fill', '#555')
1026 .style('fill', '#888')
1027 .style('opacity', 0.5)
1028 .transition()
1029 .duration(1500)
1030 .attr('r', 0);
1031 // note, leave <g>.remove to remove this element
1032
1033 });
1034
1035 // TODO: device node exits
Simon Huntc7ee0662014-11-05 16:44:37 -08001036 }
1037
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001038 function find(id, array) {
1039 for (var idx = 0, n = array.length; idx < n; idx++) {
1040 if (array[idx].id === id) {
1041 return idx;
1042 }
1043 }
1044 return -1;
1045 }
1046
1047 function removeLinkElement(linkData) {
1048 // remove from lookup cache
1049 delete network.lookup[linkData.id];
1050 // remove from links array
1051 var idx = find(linkData.id, network.links);
Simon Huntfc274c92014-11-11 11:05:46 -08001052 network.links.splice(idx, 1);
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001053 // remove from SVG
1054 updateLinks();
Simon Hunt44031102014-11-11 13:20:36 -08001055 network.force.resume();
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001056 }
Simon Huntc7ee0662014-11-05 16:44:37 -08001057
Simon Hunt44031102014-11-11 13:20:36 -08001058 function removeHostElement(hostData) {
1059 // first, remove associated hostLink...
1060 removeLinkElement(hostData.linkData);
1061
1062 // remove from lookup cache
1063 delete network.lookup[hostData.id];
1064 // remove from nodes array
1065 var idx = find(hostData.id, network.nodes);
1066 network.nodes.splice(idx, 1);
1067 // remove from SVG
1068 updateNodes();
1069 network.force.resume();
1070 }
1071
1072
Simon Huntc7ee0662014-11-05 16:44:37 -08001073 function tick() {
1074 node.attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001075 transform: function (d) { return translate(d.x, d.y); }
Simon Huntc7ee0662014-11-05 16:44:37 -08001076 });
1077
1078 link.attr({
1079 x1: function (d) { return d.source.x; },
1080 y1: function (d) { return d.source.y; },
1081 x2: function (d) { return d.target.x; },
1082 y2: function (d) { return d.target.y; }
1083 });
1084 }
Simon Hunt934c3ce2014-11-05 11:45:07 -08001085
1086 // ==============================
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001087 // Web-Socket for live data
1088
1089 function webSockUrl() {
1090 return document.location.toString()
1091 .replace(/\#.*/, '')
1092 .replace('http://', 'ws://')
1093 .replace('https://', 'wss://')
1094 .replace('index2.html', config.webSockUrl);
1095 }
1096
1097 webSock = {
1098 ws : null,
1099
1100 connect : function() {
1101 webSock.ws = new WebSocket(webSockUrl());
1102
1103 webSock.ws.onopen = function() {
Simon Hunt0c6d4192014-11-12 12:07:10 -08001104 noWebSock(false);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001105 };
1106
1107 webSock.ws.onmessage = function(m) {
1108 if (m.data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001109 wsTraceRx(m.data);
Thomas Vachuskad472c6e2014-11-07 19:11:05 -08001110 handleServerEvent(JSON.parse(m.data));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001111 }
1112 };
1113
1114 webSock.ws.onclose = function(m) {
1115 webSock.ws = null;
Simon Hunt0c6d4192014-11-12 12:07:10 -08001116 noWebSock(true);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001117 };
1118 },
1119
1120 send : function(text) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001121 if (text != null) {
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001122 webSock._send(text);
1123 }
1124 },
1125
1126 _send : function(message) {
1127 if (webSock.ws) {
1128 webSock.ws.send(message);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001129 } else {
Simon Hunt56d51852014-11-09 13:03:35 -08001130 network.view.alert('no web socket open\n\n' + message);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001131 }
1132 }
1133
1134 };
1135
Simon Hunt0c6d4192014-11-12 12:07:10 -08001136 function noWebSock(b) {
1137 mask.style('display',b ? 'block' : 'none');
1138 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001139
Simon Hunt61d04042014-11-11 17:27:16 -08001140 // TODO: use cache of pending messages (key = sid) to reconcile responses
1141
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001142 function sendMessage(evType, payload) {
1143 var toSend = {
Simon Huntbb282f52014-11-10 11:08:19 -08001144 event: evType,
1145 sid: ++sid,
1146 payload: payload
1147 },
1148 asText = JSON.stringify(toSend);
1149 wsTraceTx(asText);
1150 webSock.send(asText);
1151 }
1152
1153 function wsTraceTx(msg) {
1154 wsTrace('tx', msg);
1155 }
1156 function wsTraceRx(msg) {
1157 wsTrace('rx', msg);
1158 }
1159 function wsTrace(rxtx, msg) {
Simon Huntbb282f52014-11-10 11:08:19 -08001160 console.log('[' + rxtx + '] ' + msg);
1161 // TODO: integrate with trace view
1162 //if (trace) {
1163 // trace.output(rxtx, msg);
1164 //}
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001165 }
1166
1167
1168 // ==============================
1169 // Selection stuff
1170
1171 function selectObject(obj, el) {
1172 var n,
1173 meta = d3.event.sourceEvent.metaKey;
1174
1175 if (el) {
1176 n = d3.select(el);
1177 } else {
1178 node.each(function(d) {
1179 if (d == obj) {
1180 n = d3.select(el = this);
1181 }
1182 });
1183 }
1184 if (!n) return;
1185
1186 if (meta && n.classed('selected')) {
1187 deselectObject(obj.id);
Simon Hunt61d04042014-11-11 17:27:16 -08001188 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001189 return;
1190 }
1191
1192 if (!meta) {
1193 deselectAll();
1194 }
1195
Simon Huntc31d5692014-11-12 13:27:18 -08001196 selections[obj.id] = { obj: obj, el: el };
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001197 selectOrder.push(obj.id);
1198
1199 n.classed('selected', true);
Simon Hunt61d04042014-11-11 17:27:16 -08001200 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001201 }
1202
1203 function deselectObject(id) {
Simon Huntc31d5692014-11-12 13:27:18 -08001204 var obj = selections[id],
1205 idx;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001206 if (obj) {
1207 d3.select(obj.el).classed('selected', false);
Simon Hunt61d04042014-11-11 17:27:16 -08001208 delete selections[id];
Simon Huntc31d5692014-11-12 13:27:18 -08001209 idx = $.inArray(id, selectOrder);
1210 if (idx >= 0) {
1211 selectOrder.splice(idx, 1);
1212 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001213 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001214 }
1215
1216 function deselectAll() {
1217 // deselect all nodes in the network...
1218 node.classed('selected', false);
1219 selections = {};
1220 selectOrder = [];
Simon Hunt61d04042014-11-11 17:27:16 -08001221 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001222 }
1223
Simon Hunt61d04042014-11-11 17:27:16 -08001224 // FIXME: this click handler does not get unloaded when the view does
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001225 $('#view').on('click', function(e) {
1226 if (!$(e.target).closest('.node').length) {
1227 if (!e.metaKey) {
1228 deselectAll();
1229 }
1230 }
1231 });
1232
Simon Hunt61d04042014-11-11 17:27:16 -08001233 // update the state of the detail pane, based on current selections
1234 function updateDetailPane() {
1235 var nSel = selectOrder.length;
1236 if (!nSel) {
1237 detailPane.hide();
1238 } else if (nSel === 1) {
1239 singleSelect();
1240 } else {
1241 multiSelect();
1242 }
1243 }
1244
1245 function singleSelect() {
1246 requestDetails();
Simon Huntb53e0682014-11-12 13:32:01 -08001247 // NOTE: detail pane will be shown from showDetails event callback
Simon Hunt61d04042014-11-11 17:27:16 -08001248 }
1249
1250 function multiSelect() {
Simon Huntb53e0682014-11-12 13:32:01 -08001251 populateMultiSelect();
1252 // TODO: Add multi-select actions ...
1253 }
1254
1255 function addSep(tbody) {
1256 var tr = tbody.append('tr');
1257 $('<hr>').appendTo(tr.append('td').attr('colspan', 2));
1258 }
1259
1260 function addProp(tbody, label, value) {
1261 var tr = tbody.append('tr');
1262
1263 tr.append('td')
1264 .attr('class', 'label')
1265 .text(label + ' :');
1266
1267 tr.append('td')
1268 .attr('class', 'value')
1269 .text(value);
1270 }
1271
1272 function populateMultiSelect() {
1273 detailPane.empty();
1274
1275 var title = detailPane.append("h2"),
1276 table = detailPane.append("table"),
1277 tbody = table.append("tbody");
1278
1279 title.text('Multi-Select...');
1280
1281 selectOrder.forEach(function (d, i) {
1282 addProp(tbody, i+1, d);
1283 });
Simon Hunt61d04042014-11-11 17:27:16 -08001284 }
1285
1286 function populateDetails(data) {
1287 detailPane.empty();
1288
1289 var title = detailPane.append("h2"),
1290 table = detailPane.append("table"),
1291 tbody = table.append("tbody");
1292
1293 $('<img src="img/' + data.type + '.png">').appendTo(title);
1294 $('<span>').attr('class', 'icon').text(data.id).appendTo(title);
1295
1296 data.propOrder.forEach(function(p) {
1297 if (p === '-') {
1298 addSep(tbody);
1299 } else {
1300 addProp(tbody, p, data.props[p]);
1301 }
1302 });
Simon Hunt61d04042014-11-11 17:27:16 -08001303 }
1304
1305 // ==============================
1306 // Test harness code
Simon Hunt56d51852014-11-09 13:03:35 -08001307
1308 function prepareScenario(view, ctx, dbg) {
1309 var sc = scenario,
1310 urlSc = sc.evDir + ctx + sc.evScenario;
1311
1312 if (!ctx) {
1313 view.alert("No scenario specified (null ctx)");
1314 return;
1315 }
1316
1317 sc.view = view;
1318 sc.ctx = ctx;
1319 sc.debug = dbg;
1320 sc.evNumber = 0;
1321
1322 d3.json(urlSc, function(err, data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001323 var p = data && data.params || {},
1324 desc = data && data.description || null,
Simon Huntfc274c92014-11-11 11:05:46 -08001325 intro = data && data.title;
Simon Huntbb282f52014-11-10 11:08:19 -08001326
Simon Hunt56d51852014-11-09 13:03:35 -08001327 if (err) {
1328 view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err);
1329 } else {
1330 sc.params = p;
Simon Huntbb282f52014-11-10 11:08:19 -08001331 if (desc) {
1332 intro += '\n\n ' + desc.join('\n ');
1333 }
1334 view.alert(intro);
Simon Hunt56d51852014-11-09 13:03:35 -08001335 }
1336 });
1337
1338 }
1339
Simon Hunt0c6d4192014-11-12 12:07:10 -08001340
1341 function para(sel, text) {
1342 sel.append('p').text(text);
1343 }
1344
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001345 // ==============================
Simon Hunt142d0032014-11-04 20:13:09 -08001346 // View life-cycle callbacks
Simon Hunt195cb382014-11-03 17:50:51 -08001347
Simon Huntf67722a2014-11-10 09:32:06 -08001348 function preload(view, ctx, flags) {
Simon Hunt142d0032014-11-04 20:13:09 -08001349 var w = view.width(),
1350 h = view.height(),
1351 idBg = view.uid('bg'),
Simon Huntc7ee0662014-11-05 16:44:37 -08001352 showBg = config.options.showBackground ? 'visible' : 'hidden',
1353 fcfg = config.force,
1354 fpad = fcfg.pad,
1355 forceDim = [w - 2*fpad, h - 2*fpad];
Simon Hunt195cb382014-11-03 17:50:51 -08001356
Simon Huntbb282f52014-11-10 11:08:19 -08001357 // TODO: set trace api
1358 //trace = onos.exported.webSockTrace;
1359
Simon Hunt142d0032014-11-04 20:13:09 -08001360 // NOTE: view.$div is a D3 selection of the view's div
1361 svg = view.$div.append('svg');
Simon Hunt934c3ce2014-11-05 11:45:07 -08001362 setSize(svg, view);
1363
Simon Hunt1a9eff92014-11-07 11:06:34 -08001364 // add blue glow filter to svg layer
1365 d3u.appendGlow(svg);
1366
Simon Hunt142d0032014-11-04 20:13:09 -08001367 // load the background image
1368 bgImg = svg.append('svg:image')
Simon Hunt195cb382014-11-03 17:50:51 -08001369 .attr({
Simon Hunt142d0032014-11-04 20:13:09 -08001370 id: idBg,
1371 width: w,
1372 height: h,
Simon Hunt195cb382014-11-03 17:50:51 -08001373 'xlink:href': config.backgroundUrl
1374 })
Simon Hunt142d0032014-11-04 20:13:09 -08001375 .style({
1376 visibility: showBg
Simon Hunt195cb382014-11-03 17:50:51 -08001377 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001378
1379 // group for the topology
1380 topoG = svg.append('g')
1381 .attr('transform', fcfg.translate());
1382
1383 // subgroups for links and nodes
1384 linkG = topoG.append('g').attr('id', 'links');
1385 nodeG = topoG.append('g').attr('id', 'nodes');
1386
1387 // selection of nodes and links
1388 link = linkG.selectAll('.link');
1389 node = nodeG.selectAll('.node');
1390
Simon Hunt7cd48f32014-11-09 23:42:50 -08001391 function chrg(d) {
1392 return fcfg.charge[d.class] || -12000;
1393 }
Simon Hunt99c13842014-11-06 18:23:12 -08001394 function ldist(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08001395 return fcfg.linkDistance[d.type] || 50;
Simon Hunt99c13842014-11-06 18:23:12 -08001396 }
1397 function lstrg(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08001398 // 0.0 - 1.0
1399 return fcfg.linkStrength[d.type] || 1.0;
Simon Hunt99c13842014-11-06 18:23:12 -08001400 }
1401
Simon Hunt1a9eff92014-11-07 11:06:34 -08001402 function selectCb(d, self) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001403 selectObject(d, self);
Simon Hunt1a9eff92014-11-07 11:06:34 -08001404 }
1405
1406 function atDragEnd(d, self) {
Simon Hunt56d51852014-11-09 13:03:35 -08001407 // once we've finished moving, pin the node in position
1408 d.fixed = true;
1409 d3.select(self).classed('fixed', true);
1410 if (config.useLiveData) {
Simon Hunt902c9922014-11-11 11:59:31 -08001411 sendUpdateMeta(d);
Simon Hunt1a9eff92014-11-07 11:06:34 -08001412 }
1413 }
1414
Simon Hunt902c9922014-11-11 11:59:31 -08001415 function sendUpdateMeta(d) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001416 sendMessage('updateMeta', {
1417 id: d.id,
1418 'class': d.class,
Simon Hunt902c9922014-11-11 11:59:31 -08001419 'memento': {
1420 x: Math.floor(d.x),
1421 y: Math.floor(d.y)
1422 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001423 });
1424 }
1425
Simon Huntc7ee0662014-11-05 16:44:37 -08001426 // set up the force layout
1427 network.force = d3.layout.force()
1428 .size(forceDim)
1429 .nodes(network.nodes)
1430 .links(network.links)
Simon Hunt7cd48f32014-11-09 23:42:50 -08001431 .gravity(0.4)
1432 .friction(0.7)
1433 .charge(chrg)
Simon Hunt99c13842014-11-06 18:23:12 -08001434 .linkDistance(ldist)
1435 .linkStrength(lstrg)
Simon Huntc7ee0662014-11-05 16:44:37 -08001436 .on('tick', tick);
Simon Hunt195cb382014-11-03 17:50:51 -08001437
Simon Hunt1a9eff92014-11-07 11:06:34 -08001438 network.drag = d3u.createDragBehavior(network.force, selectCb, atDragEnd);
Simon Hunt0c6d4192014-11-12 12:07:10 -08001439
1440 // create mask layer for when we lose connection to server.
1441 mask = view.$div.append('div').attr('id','topo-mask');
1442 para(mask, 'Oops!');
1443 para(mask, 'Web-socket connection to server closed...');
1444 para(mask, 'Try refreshing the page.');
Simon Hunt1a9eff92014-11-07 11:06:34 -08001445 }
Simon Hunt195cb382014-11-03 17:50:51 -08001446
Simon Hunt56d51852014-11-09 13:03:35 -08001447 function load(view, ctx, flags) {
Simon Huntf67722a2014-11-10 09:32:06 -08001448 // resize, in case the window was resized while we were not loaded
1449 resize(view, ctx, flags);
1450
Simon Hunt99c13842014-11-06 18:23:12 -08001451 // cache the view token, so network topo functions can access it
1452 network.view = view;
Simon Hunt56d51852014-11-09 13:03:35 -08001453 config.useLiveData = !flags.local;
1454
1455 if (!config.useLiveData) {
1456 prepareScenario(view, ctx, flags.debug);
1457 }
Simon Hunt99c13842014-11-06 18:23:12 -08001458
1459 // set our radio buttons and key bindings
Simon Hunt934c3ce2014-11-05 11:45:07 -08001460 view.setRadio(btnSet);
1461 view.setKeys(keyDispatch);
Simon Hunt195cb382014-11-03 17:50:51 -08001462
Simon Hunt50128c02014-11-08 13:36:15 -08001463 if (config.useLiveData) {
1464 webSock.connect();
1465 }
Simon Hunt195cb382014-11-03 17:50:51 -08001466 }
1467
Simon Huntf67722a2014-11-10 09:32:06 -08001468 function resize(view, ctx, flags) {
Simon Hunt934c3ce2014-11-05 11:45:07 -08001469 setSize(svg, view);
1470 setSize(bgImg, view);
Simon Hunt99c13842014-11-06 18:23:12 -08001471
1472 // TODO: hook to recompute layout, perhaps? work with zoom/pan code
1473 // adjust force layout size
Simon Hunt142d0032014-11-04 20:13:09 -08001474 }
1475
1476
1477 // ==============================
1478 // View registration
Simon Hunt195cb382014-11-03 17:50:51 -08001479
Simon Hunt25248912014-11-04 11:25:48 -08001480 onos.ui.addView('topo', {
Simon Hunt142d0032014-11-04 20:13:09 -08001481 preload: preload,
1482 load: load,
1483 resize: resize
Simon Hunt195cb382014-11-03 17:50:51 -08001484 });
1485
Simon Hunt61d04042014-11-11 17:27:16 -08001486 detailPane = onos.ui.addFloatingPanel('topo-detail');
1487
Simon Hunt195cb382014-11-03 17:50:51 -08001488}(ONOS));