blob: dcde928b922d7b64d45f16f9fa4fa4834c1c11db [file] [log] [blame]
Simon Hunt195cb382014-11-03 17:50:51 -08001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
Simon Hunt142d0032014-11-04 20:13:09 -080018 ONOS network topology viewer - version 1.1
Simon Hunt195cb382014-11-03 17:50:51 -080019
20 @author Simon Hunt
21 */
22
23(function (onos) {
24 'use strict';
25
Simon Hunt1a9eff92014-11-07 11:06:34 -080026 // shorter names for library APIs
Simon Huntbb282f52014-11-10 11:08:19 -080027 var d3u = onos.lib.d3util,
28 trace;
Simon Hunt1a9eff92014-11-07 11:06:34 -080029
Simon Hunt195cb382014-11-03 17:50:51 -080030 // configuration data
31 var config = {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -080032 useLiveData: true,
Simon Huntfc274c92014-11-11 11:05:46 -080033 fnTrace: true,
Simon Hunt195cb382014-11-03 17:50:51 -080034 debugOn: false,
35 debug: {
Simon Hunt99c13842014-11-06 18:23:12 -080036 showNodeXY: true,
37 showKeyHandler: false
Simon Hunt195cb382014-11-03 17:50:51 -080038 },
39 options: {
40 layering: true,
41 collisionPrevention: true,
Simon Hunt142d0032014-11-04 20:13:09 -080042 showBackground: true
Simon Hunt195cb382014-11-03 17:50:51 -080043 },
44 backgroundUrl: 'img/us-map.png',
Thomas Vachuska7d638d32014-11-07 10:24:43 -080045 webSockUrl: 'ws/topology',
Simon Hunt195cb382014-11-03 17:50:51 -080046 data: {
47 live: {
48 jsonUrl: 'rs/topology/graph',
49 detailPrefix: 'rs/topology/graph/',
50 detailSuffix: ''
51 },
52 fake: {
53 jsonUrl: 'json/network2.json',
54 detailPrefix: 'json/',
55 detailSuffix: '.json'
56 }
57 },
Simon Hunt99c13842014-11-06 18:23:12 -080058 labels: {
59 imgPad: 16,
60 padLR: 4,
61 padTB: 3,
62 marginLR: 3,
63 marginTB: 2,
64 port: {
65 gap: 3,
66 width: 18,
67 height: 14
68 }
69 },
Simon Hunt1a9eff92014-11-07 11:06:34 -080070 topo: {
71 linkInColor: '#66f',
72 linkInWidth: 14
73 },
Simon Hunt99c13842014-11-06 18:23:12 -080074 icons: {
75 w: 28,
76 h: 28,
77 xoff: -12,
78 yoff: -8
79 },
Simon Hunt195cb382014-11-03 17:50:51 -080080 iconUrl: {
81 device: 'img/device.png',
82 host: 'img/host.png',
83 pkt: 'img/pkt.png',
84 opt: 'img/opt.png'
85 },
Simon Hunt195cb382014-11-03 17:50:51 -080086 force: {
Simon Hunt7cd48f32014-11-09 23:42:50 -080087 note_for_links: 'link.type is used to differentiate',
Simon Huntc7ee0662014-11-05 16:44:37 -080088 linkDistance: {
Simon Hunt7cd48f32014-11-09 23:42:50 -080089 direct: 100,
90 optical: 120,
Thomas Vachuska3266abf2014-11-13 09:28:46 -080091 hostLink: 3
Simon Huntc7ee0662014-11-05 16:44:37 -080092 },
93 linkStrength: {
Simon Hunt7cd48f32014-11-09 23:42:50 -080094 direct: 1.0,
95 optical: 1.0,
96 hostLink: 1.0
Simon Huntc7ee0662014-11-05 16:44:37 -080097 },
Simon Hunt7cd48f32014-11-09 23:42:50 -080098 note_for_nodes: 'node.class is used to differentiate',
Simon Huntc7ee0662014-11-05 16:44:37 -080099 charge: {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800100 device: -8000,
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800101 host: -5000
Simon Huntc7ee0662014-11-05 16:44:37 -0800102 },
103 pad: 20,
Simon Hunt195cb382014-11-03 17:50:51 -0800104 translate: function() {
105 return 'translate(' +
Simon Huntc7ee0662014-11-05 16:44:37 -0800106 config.force.pad + ',' +
107 config.force.pad + ')';
Simon Hunt195cb382014-11-03 17:50:51 -0800108 }
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800109 },
110 // see below in creation of viewBox on main svg
111 logicalSize: 1000
Simon Hunt195cb382014-11-03 17:50:51 -0800112 };
113
Simon Hunt142d0032014-11-04 20:13:09 -0800114 // radio buttons
115 var btnSet = [
Simon Hunt934c3ce2014-11-05 11:45:07 -0800116 { text: 'All Layers', cb: showAllLayers },
117 { text: 'Packet Only', cb: showPacketLayer },
118 { text: 'Optical Only', cb: showOpticalLayer }
119 ];
120
121 // key bindings
122 var keyDispatch = {
Simon Hunt1a9eff92014-11-07 11:06:34 -0800123 M: testMe, // TODO: remove (testing only)
Simon Hunt50128c02014-11-08 13:36:15 -0800124 S: injectStartupEvents, // TODO: remove (testing only)
125 space: injectTestEvent, // TODO: remove (testing only)
Simon Hunt99c13842014-11-06 18:23:12 -0800126
Thomas Vachuska65368e32014-11-08 16:10:20 -0800127 B: toggleBg, // TODO: do we really need this?
Simon Hunt934c3ce2014-11-05 11:45:07 -0800128 L: cycleLabels,
129 P: togglePorts,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800130 U: unpin,
131
Simon Huntb53e0682014-11-12 13:32:01 -0800132 W: requestTraffic, // bag of selections
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800133 X: cancelTraffic,
134 Z: requestPath // host-to-host intent (and monitor)
Simon Hunt934c3ce2014-11-05 11:45:07 -0800135 };
Simon Hunt142d0032014-11-04 20:13:09 -0800136
Simon Hunt195cb382014-11-03 17:50:51 -0800137 // state variables
Simon Hunt99c13842014-11-06 18:23:12 -0800138 var network = {
Simon Hunt50128c02014-11-08 13:36:15 -0800139 view: null, // view token reference
Simon Hunt99c13842014-11-06 18:23:12 -0800140 nodes: [],
141 links: [],
142 lookup: {}
143 },
Simon Hunt56d51852014-11-09 13:03:35 -0800144 scenario = {
145 evDir: 'json/ev/',
146 evScenario: '/scenario.json',
147 evPrefix: '/ev_',
148 evOnos: '_onos.json',
149 evUi: '_ui.json',
150 ctx: null,
151 params: {},
152 evNumber: 0,
153 view: null,
154 debug: false
155 },
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800156 webSock,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800157 sid = 0,
Simon Hunt56d51852014-11-09 13:03:35 -0800158 deviceLabelIndex = 0,
159 hostLabelIndex = 0,
Simon Hunt61d04042014-11-11 17:27:16 -0800160 detailPane,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800161 selectOrder = [],
162 selections = {},
163
Simon Hunt195cb382014-11-03 17:50:51 -0800164 highlighted = null,
165 hovered = null,
166 viewMode = 'showAll',
167 portLabelsOn = false;
168
Simon Hunt934c3ce2014-11-05 11:45:07 -0800169 // D3 selections
170 var svg,
171 bgImg,
Simon Huntc7ee0662014-11-05 16:44:37 -0800172 topoG,
173 nodeG,
174 linkG,
175 node,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800176 link,
177 mask;
Simon Hunt195cb382014-11-03 17:50:51 -0800178
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800179 // the projection for the map background
180 var geoMapProjection;
181
Simon Hunt142d0032014-11-04 20:13:09 -0800182 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800183 // For Debugging / Development
Simon Hunt195cb382014-11-03 17:50:51 -0800184
Simon Hunt99c13842014-11-06 18:23:12 -0800185 function note(label, msg) {
186 console.log('NOTE: ' + label + ': ' + msg);
Simon Hunt195cb382014-11-03 17:50:51 -0800187 }
188
Simon Hunt99c13842014-11-06 18:23:12 -0800189 function debug(what) {
190 return config.debugOn && config.debug[what];
Simon Hunt934c3ce2014-11-05 11:45:07 -0800191 }
192
Simon Huntfc274c92014-11-11 11:05:46 -0800193 function fnTrace(msg, id) {
194 if (config.fnTrace) {
195 console.log('FN: ' + msg + ' [' + id + ']');
196 }
197 }
Simon Hunt99c13842014-11-06 18:23:12 -0800198
Simon Hunt934c3ce2014-11-05 11:45:07 -0800199 // ==============================
200 // Key Callbacks
201
Simon Hunt99c13842014-11-06 18:23:12 -0800202 function testMe(view) {
Simon Hunt50128c02014-11-08 13:36:15 -0800203 view.alert('test');
Simon Hunt99c13842014-11-06 18:23:12 -0800204 }
205
Simon Hunt56d51852014-11-09 13:03:35 -0800206 function abortIfLive() {
Simon Hunt50128c02014-11-08 13:36:15 -0800207 if (config.useLiveData) {
Simon Huntb53e0682014-11-12 13:32:01 -0800208 network.view.alert("Sorry, currently using live data..");
Simon Hunt56d51852014-11-09 13:03:35 -0800209 return true;
Simon Hunt50128c02014-11-08 13:36:15 -0800210 }
Simon Hunt56d51852014-11-09 13:03:35 -0800211 return false;
212 }
Simon Hunt50128c02014-11-08 13:36:15 -0800213
Simon Hunt56d51852014-11-09 13:03:35 -0800214 function testDebug(msg) {
215 if (scenario.debug) {
216 scenario.view.alert(msg);
217 }
218 }
Simon Hunt99c13842014-11-06 18:23:12 -0800219
Simon Hunt56d51852014-11-09 13:03:35 -0800220 function injectTestEvent(view) {
221 if (abortIfLive()) { return; }
222 var sc = scenario,
223 evn = ++sc.evNumber,
224 pfx = sc.evDir + sc.ctx + sc.evPrefix + evn,
225 onosUrl = pfx + sc.evOnos,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800226 uiUrl = pfx + sc.evUi,
227 stack = [
228 { url: onosUrl, cb: handleServerEvent },
229 { url: uiUrl, cb: handleUiEvent }
230 ];
231 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800232 }
233
Simon Hunt7cd48f32014-11-09 23:42:50 -0800234 function recurseFetchEvent(stack, evn) {
235 var v = scenario.view,
236 frame;
237 if (stack.length === 0) {
Simon Huntfc274c92014-11-11 11:05:46 -0800238 v.alert('Oops!\n\nNo event #' + evn + ' found.');
Simon Hunt7cd48f32014-11-09 23:42:50 -0800239 return;
240 }
241 frame = stack.shift();
242
243 d3.json(frame.url, function (err, data) {
Simon Hunt99c13842014-11-06 18:23:12 -0800244 if (err) {
Simon Hunt56d51852014-11-09 13:03:35 -0800245 if (err.status === 404) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800246 // if we didn't find the data, try the next stack frame
247 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800248 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800249 v.alert('non-404 error:\n\n' + frame.url + '\n\n' + err);
Simon Hunt56d51852014-11-09 13:03:35 -0800250 }
Simon Hunt99c13842014-11-06 18:23:12 -0800251 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800252 testDebug('loaded: ' + frame.url);
253 frame.cb(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800254 }
255 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800256
Simon Hunt56d51852014-11-09 13:03:35 -0800257 }
Simon Hunt50128c02014-11-08 13:36:15 -0800258
Simon Hunt56d51852014-11-09 13:03:35 -0800259 function handleUiEvent(data) {
Simon Huntbb282f52014-11-10 11:08:19 -0800260 scenario.view.alert('UI Tx: ' + data.event + '\n\n' +
261 JSON.stringify(data));
Simon Hunt56d51852014-11-09 13:03:35 -0800262 }
263
264 function injectStartupEvents(view) {
265 var last = scenario.params.lastAuto || 0;
266 if (abortIfLive()) { return; }
267
268 while (scenario.evNumber < last) {
Simon Hunt1a9eff92014-11-07 11:06:34 -0800269 injectTestEvent(view);
270 }
271 }
272
Simon Hunt934c3ce2014-11-05 11:45:07 -0800273 function toggleBg() {
274 var vis = bgImg.style('visibility');
275 bgImg.style('visibility', (vis === 'hidden') ? 'visible' : 'hidden');
276 }
277
Simon Hunt99c13842014-11-06 18:23:12 -0800278 function cycleLabels() {
Simon Huntbb282f52014-11-10 11:08:19 -0800279 deviceLabelIndex = (deviceLabelIndex === network.deviceLabelCount - 1)
280 ? 0 : deviceLabelIndex + 1;
Simon Hunt5f36d342014-11-08 21:33:14 -0800281
Simon Hunt99c13842014-11-06 18:23:12 -0800282 network.nodes.forEach(function (d) {
Simon Huntbb282f52014-11-10 11:08:19 -0800283 if (d.class === 'device') {
284 updateDeviceLabel(d);
285 }
Simon Hunt99c13842014-11-06 18:23:12 -0800286 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800287 }
288
289 function togglePorts(view) {
Simon Hunt50128c02014-11-08 13:36:15 -0800290 view.alert('togglePorts() callback')
Simon Hunt934c3ce2014-11-05 11:45:07 -0800291 }
292
293 function unpin(view) {
Simon Hunt50128c02014-11-08 13:36:15 -0800294 view.alert('unpin() callback')
Simon Hunt934c3ce2014-11-05 11:45:07 -0800295 }
296
297 // ==============================
298 // Radio Button Callbacks
299
Simon Hunt195cb382014-11-03 17:50:51 -0800300 function showAllLayers() {
Simon Hunt142d0032014-11-04 20:13:09 -0800301// network.node.classed('inactive', false);
302// network.link.classed('inactive', false);
303// d3.selectAll('svg .port').classed('inactive', false);
304// d3.selectAll('svg .portText').classed('inactive', false);
Simon Hunt934c3ce2014-11-05 11:45:07 -0800305 // TODO ...
Simon Hunt50128c02014-11-08 13:36:15 -0800306 network.view.alert('showAllLayers() callback');
Simon Hunt195cb382014-11-03 17:50:51 -0800307 }
308
309 function showPacketLayer() {
Simon Hunt934c3ce2014-11-05 11:45:07 -0800310 showAllLayers();
311 // TODO ...
Simon Hunt50128c02014-11-08 13:36:15 -0800312 network.view.alert('showPacketLayer() callback');
Simon Hunt195cb382014-11-03 17:50:51 -0800313 }
314
315 function showOpticalLayer() {
Simon Hunt934c3ce2014-11-05 11:45:07 -0800316 showAllLayers();
317 // TODO ...
Simon Hunt50128c02014-11-08 13:36:15 -0800318 network.view.alert('showOpticalLayer() callback');
Simon Hunt195cb382014-11-03 17:50:51 -0800319 }
320
Simon Hunt142d0032014-11-04 20:13:09 -0800321 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800322 // Private functions
323
Simon Hunt99c13842014-11-06 18:23:12 -0800324 function safeId(s) {
325 return s.replace(/[^a-z0-9]/gi, '-');
326 }
327
Simon Huntc7ee0662014-11-05 16:44:37 -0800328 // set the size of the given element to that of the view (reduced if padded)
329 function setSize(el, view, pad) {
330 var padding = pad ? pad * 2 : 0;
Simon Hunt934c3ce2014-11-05 11:45:07 -0800331 el.attr({
Simon Huntc7ee0662014-11-05 16:44:37 -0800332 width: view.width() - padding,
333 height: view.height() - padding
Simon Hunt934c3ce2014-11-05 11:45:07 -0800334 });
335 }
336
Simon Hunt934c3ce2014-11-05 11:45:07 -0800337
Simon Hunt99c13842014-11-06 18:23:12 -0800338 // ==============================
339 // Event handlers for server-pushed events
340
Simon Huntbb282f52014-11-10 11:08:19 -0800341 function logicError(msg) {
342 // TODO, report logic error to server, via websock, so it can be logged
343 network.view.alert('Logic Error:\n\n' + msg);
Simon Huntfc274c92014-11-11 11:05:46 -0800344 console.warn(msg);
Simon Huntbb282f52014-11-10 11:08:19 -0800345 }
346
Simon Hunt99c13842014-11-06 18:23:12 -0800347 var eventDispatch = {
348 addDevice: addDevice,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800349 addLink: addLink,
Simon Hunt56d51852014-11-09 13:03:35 -0800350 addHost: addHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800351
Simon Huntbb282f52014-11-10 11:08:19 -0800352 updateDevice: updateDevice,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800353 updateLink: updateLink,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800354 updateHost: updateHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800355
Simon Huntbb282f52014-11-10 11:08:19 -0800356 removeDevice: stillToImplement,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800357 removeLink: removeLink,
Simon Hunt44031102014-11-11 13:20:36 -0800358 removeHost: removeHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800359
Simon Hunt61d04042014-11-11 17:27:16 -0800360 showDetails: showDetails,
Simon Huntb53e0682014-11-12 13:32:01 -0800361 showPath: showPath,
362 showTraffic: showTraffic
Simon Hunt99c13842014-11-06 18:23:12 -0800363 };
364
365 function addDevice(data) {
Simon Huntfc274c92014-11-11 11:05:46 -0800366 fnTrace('addDevice', data.payload.id);
Simon Hunt99c13842014-11-06 18:23:12 -0800367 var device = data.payload,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800368 nodeData = createDeviceNode(device);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800369 network.nodes.push(nodeData);
370 network.lookup[nodeData.id] = nodeData;
Simon Hunt99c13842014-11-06 18:23:12 -0800371 updateNodes();
372 network.force.start();
373 }
374
Simon Hunt99c13842014-11-06 18:23:12 -0800375 function addLink(data) {
Simon Huntfc274c92014-11-11 11:05:46 -0800376 fnTrace('addLink', data.payload.id);
Simon Hunt99c13842014-11-06 18:23:12 -0800377 var link = data.payload,
378 lnk = createLink(link);
Simon Hunt99c13842014-11-06 18:23:12 -0800379 if (lnk) {
Simon Hunt99c13842014-11-06 18:23:12 -0800380 network.links.push(lnk);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800381 network.lookup[lnk.id] = lnk;
Simon Hunt99c13842014-11-06 18:23:12 -0800382 updateLinks();
383 network.force.start();
384 }
385 }
386
Simon Hunt56d51852014-11-09 13:03:35 -0800387 function addHost(data) {
Simon Huntfc274c92014-11-11 11:05:46 -0800388 fnTrace('addHost', data.payload.id);
Simon Hunt56d51852014-11-09 13:03:35 -0800389 var host = data.payload,
390 node = createHostNode(host),
391 lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800392 network.nodes.push(node);
393 network.lookup[host.id] = node;
394 updateNodes();
395
396 lnk = createHostLink(host);
397 if (lnk) {
Simon Hunt44031102014-11-11 13:20:36 -0800398 node.linkData = lnk; // cache ref on its host
Simon Hunt56d51852014-11-09 13:03:35 -0800399 network.links.push(lnk);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800400 network.lookup[host.ingress] = lnk;
401 network.lookup[host.egress] = lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800402 updateLinks();
403 }
404 network.force.start();
405 }
406
Simon Hunt44031102014-11-11 13:20:36 -0800407 // TODO: fold updateX(...) methods into one base method; remove duplication
Simon Huntbb282f52014-11-10 11:08:19 -0800408 function updateDevice(data) {
Simon Huntfc274c92014-11-11 11:05:46 -0800409 fnTrace('updateDevice', data.payload.id);
Simon Huntbb282f52014-11-10 11:08:19 -0800410 var device = data.payload,
411 id = device.id,
412 nodeData = network.lookup[id];
413 if (nodeData) {
414 $.extend(nodeData, device);
415 updateDeviceState(nodeData);
416 } else {
417 logicError('updateDevice lookup fail. ID = "' + id + '"');
418 }
419 }
420
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800421 function updateLink(data) {
Simon Huntfc274c92014-11-11 11:05:46 -0800422 fnTrace('updateLink', data.payload.id);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800423 var link = data.payload,
424 id = link.id,
425 linkData = network.lookup[id];
426 if (linkData) {
427 $.extend(linkData, link);
428 updateLinkState(linkData);
429 } else {
430 logicError('updateLink lookup fail. ID = "' + id + '"');
431 }
432 }
433
Simon Hunt7cd48f32014-11-09 23:42:50 -0800434 function updateHost(data) {
Simon Huntfc274c92014-11-11 11:05:46 -0800435 fnTrace('updateHost', data.payload.id);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800436 var host = data.payload,
Simon Huntbb282f52014-11-10 11:08:19 -0800437 id = host.id,
438 hostData = network.lookup[id];
439 if (hostData) {
440 $.extend(hostData, host);
441 updateHostState(hostData);
442 } else {
443 logicError('updateHost lookup fail. ID = "' + id + '"');
444 }
Simon Hunt7cd48f32014-11-09 23:42:50 -0800445 }
446
Simon Hunt44031102014-11-11 13:20:36 -0800447 // TODO: fold removeX(...) methods into base method - remove dup code
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800448 function removeLink(data) {
Simon Huntfc274c92014-11-11 11:05:46 -0800449 fnTrace('removeLink', data.payload.id);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800450 var link = data.payload,
451 id = link.id,
452 linkData = network.lookup[id];
453 if (linkData) {
454 removeLinkElement(linkData);
455 } else {
456 logicError('removeLink lookup fail. ID = "' + id + '"');
457 }
458 }
459
Simon Hunt44031102014-11-11 13:20:36 -0800460 function removeHost(data) {
461 fnTrace('removeHost', data.payload.id);
462 var host = data.payload,
463 id = host.id,
464 hostData = network.lookup[id];
465 if (hostData) {
466 removeHostElement(hostData);
467 } else {
468 logicError('removeHost lookup fail. ID = "' + id + '"');
469 }
470 }
471
Simon Hunt61d04042014-11-11 17:27:16 -0800472 function showDetails(data) {
473 fnTrace('showDetails', data.payload.id);
474 populateDetails(data.payload);
Simon Huntb53e0682014-11-12 13:32:01 -0800475 // TODO: Add single-select actions ...
Simon Hunt61d04042014-11-11 17:27:16 -0800476 detailPane.show();
477 }
478
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800479 function showPath(data) {
Simon Huntfc274c92014-11-11 11:05:46 -0800480 fnTrace('showPath', data.payload.id);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800481 var links = data.payload.links,
482 s = [ data.event + "\n" + links.length ];
483 links.forEach(function (d, i) {
484 s.push(d);
485 });
486 network.view.alert(s.join('\n'));
487
488 links.forEach(function (d, i) {
489 var link = network.lookup[d];
490 if (link) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800491 link.el.classed('showPath', true);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800492 }
493 });
494
495 // TODO: add selection-highlite lines to links
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800496 }
497
Simon Huntb53e0682014-11-12 13:32:01 -0800498 function showTraffic(data) {
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800499 fnTrace('showTraffic', data.payload.id);
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800500 var paths = data.payload.paths;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800501
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800502 // Revert any links hilighted previously.
503 network.links.forEach(function (d) {
504 d.el.classed('showPath', false);
505 });
506
507 // Now hilight all links in the paths payload.
508 paths.forEach(function (d) {
509 var links = d.links;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800510 links.forEach(function (d, i) {
511 var link = network.lookup[d];
512 if (link) {
513 link.el.classed('showPath', true);
514 }
515 });
516 });
517 //network.view.alert("showTraffic() -- TODO")
Simon Huntb53e0682014-11-12 13:32:01 -0800518 }
519
Simon Hunt56d51852014-11-09 13:03:35 -0800520 // ...............................
521
522 function stillToImplement(data) {
523 var p = data.payload;
524 note(data.event, p.id);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800525 network.view.alert('Not yet implemented: "' + data.event + '"');
Simon Hunt56d51852014-11-09 13:03:35 -0800526 }
Simon Hunt99c13842014-11-06 18:23:12 -0800527
528 function unknownEvent(data) {
Simon Hunt50128c02014-11-08 13:36:15 -0800529 network.view.alert('Unknown event type: "' + data.event + '"');
Simon Hunt99c13842014-11-06 18:23:12 -0800530 }
531
532 function handleServerEvent(data) {
533 var fn = eventDispatch[data.event] || unknownEvent;
534 fn(data);
535 }
536
537 // ==============================
Simon Hunt61d04042014-11-11 17:27:16 -0800538 // Out-going messages...
539
Simon Huntb53e0682014-11-12 13:32:01 -0800540 function userFeedback(msg) {
541 // for now, use the alert pane as is. Maybe different alert style in
542 // the future (centered on view; dismiss button?)
543 network.view.alert(msg);
544 }
545
546 function nSel() {
547 return selectOrder.length;
548 }
Simon Hunt61d04042014-11-11 17:27:16 -0800549 function getSel(idx) {
550 return selections[selectOrder[idx]];
551 }
Simon Huntb53e0682014-11-12 13:32:01 -0800552 function getSelId(idx) {
553 return getSel(idx).obj.id;
554 }
555 function allSelectionsClass(cls) {
556 for (var i=0, n=nSel(); i<n; i++) {
557 if (getSel(i).obj.class !== cls) {
558 return false;
559 }
560 }
561 return true;
562 }
Simon Hunt61d04042014-11-11 17:27:16 -0800563
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800564 function requestTraffic(hoverNode) {
565 if (nSel() > 0 || hoverNode) {
566 var nodes = hoverNode ? selectOrder.concat(hoverNode.id) : selectOrder;
Simon Huntb53e0682014-11-12 13:32:01 -0800567 sendMessage('requestTraffic', {
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800568 ids: nodes
Simon Huntb53e0682014-11-12 13:32:01 -0800569 });
570 } else {
571 userFeedback('Request-Traffic requires one or\n' +
572 'more items to be selected.');
573 }
574 }
575
Simon Hunt61d04042014-11-11 17:27:16 -0800576 function requestPath() {
Simon Huntb53e0682014-11-12 13:32:01 -0800577 if (nSel() === 2 && allSelectionsClass('host')) {
578 sendMessage('requestPath', {
579 one: getSelId(0),
580 two: getSelId(1)
581 });
582 } else {
583 userFeedback('Request-Path requires two\n' +
584 'hosts to be selected.');
585 }
Simon Hunt61d04042014-11-11 17:27:16 -0800586 }
587
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800588 function cancelTraffic(hoverNode) {
589 if (hoverNode && selectOrder.length) {
590 requestTraffic();
591 } else {
592 // FIXME: from where do we get the intent id(s) to send to the server?
593 sendMessage('cancelTraffic', {
594 ids: ["need_the_intent_id"]
595 });
596 }
Simon Huntac9e24f2014-11-12 10:12:21 -0800597 }
598
Simon Hunt61d04042014-11-11 17:27:16 -0800599 // request details for the selected element
600 function requestDetails() {
601 var data = getSel(0).obj,
602 payload = {
603 id: data.id,
604 class: data.class
605 };
606 sendMessage('requestDetails', payload);
607 }
608
609 // ==============================
Simon Hunt99c13842014-11-06 18:23:12 -0800610 // force layout modification functions
611
612 function translate(x, y) {
613 return 'translate(' + x + ',' + y + ')';
614 }
615
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800616 function missMsg(what, id) {
617 return '\n[' + what + '] "' + id + '" missing ';
618 }
619
620 function linkEndPoints(srcId, dstId) {
621 var srcNode = network.lookup[srcId],
622 dstNode = network.lookup[dstId],
623 sMiss = !srcNode ? missMsg('src', srcId) : '',
624 dMiss = !dstNode ? missMsg('dst', dstId) : '';
625
626 if (sMiss || dMiss) {
627 logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
628 return null;
629 }
630 return {
631 source: srcNode,
632 target: dstNode,
633 x1: srcNode.x,
634 y1: srcNode.y,
635 x2: dstNode.x,
636 y2: dstNode.y
637 };
638 }
639
Simon Hunt56d51852014-11-09 13:03:35 -0800640 function createHostLink(host) {
641 var src = host.id,
642 dst = host.cp.device,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800643 id = host.ingress,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800644 lnk = linkEndPoints(src, dst);
Simon Hunt56d51852014-11-09 13:03:35 -0800645
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800646 if (!lnk) {
Simon Hunt56d51852014-11-09 13:03:35 -0800647 return null;
648 }
649
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800650 // Synthesize link ...
651 $.extend(lnk, {
Thomas Vachuska4830d392014-11-09 17:09:56 -0800652 id: id,
Simon Hunt56d51852014-11-09 13:03:35 -0800653 class: 'link',
Simon Hunt7cd48f32014-11-09 23:42:50 -0800654 type: 'hostLink',
Simon Hunt56d51852014-11-09 13:03:35 -0800655 svgClass: 'link hostLink',
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800656 linkWidth: 1
Simon Hunt7cd48f32014-11-09 23:42:50 -0800657 });
Simon Hunt99c13842014-11-06 18:23:12 -0800658 return lnk;
659 }
660
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800661 function createLink(link) {
662 var lnk = linkEndPoints(link.src, link.dst),
663 type = link.type;
664
665 if (!lnk) {
666 return null;
667 }
668
669 // merge in remaining data
670 $.extend(lnk, link, {
671 class: 'link',
672 svgClass: type ? 'link ' + type : 'link'
673 });
674 return lnk;
Simon Hunt1a9eff92014-11-07 11:06:34 -0800675 }
676
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800677 var widthRatio = 1.4,
678 linkScale = d3.scale.linear()
679 .domain([1, 12])
680 .range([widthRatio, 12 * widthRatio])
681 .clamp(true);
682
683 function updateLinkWidth (d) {
684 // TODO: watch out for .showPath/.showTraffic classes
685 d.el.transition()
686 .duration(1000)
687 .attr('stroke-width', linkScale(d.linkWidth));
688 }
689
690
Simon Hunt99c13842014-11-06 18:23:12 -0800691 function updateLinks() {
692 link = linkG.selectAll('.link')
693 .data(network.links, function (d) { return d.id; });
694
695 // operate on existing links, if necessary
696 // link .foo() .bar() ...
697
698 // operate on entering links:
699 var entering = link.enter()
700 .append('line')
701 .attr({
Simon Hunt99c13842014-11-06 18:23:12 -0800702 class: function (d) { return d.svgClass; },
703 x1: function (d) { return d.x1; },
704 y1: function (d) { return d.y1; },
705 x2: function (d) { return d.x2; },
706 y2: function (d) { return d.y2; },
Simon Hunt1a9eff92014-11-07 11:06:34 -0800707 stroke: config.topo.linkInColor,
708 'stroke-width': config.topo.linkInWidth
Simon Hunt99c13842014-11-06 18:23:12 -0800709 })
710 .transition().duration(1000)
711 .attr({
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800712 'stroke-width': function (d) { return linkScale(d.linkWidth); },
Simon Hunt99c13842014-11-06 18:23:12 -0800713 stroke: '#666' // TODO: remove explicit stroke, rather...
714 });
715
716 // augment links
Simon Hunt7cd48f32014-11-09 23:42:50 -0800717 entering.each(function (d) {
718 var link = d3.select(this);
719 // provide ref to element selection from backing data....
720 d.el = link;
Simon Hunt99c13842014-11-06 18:23:12 -0800721
Simon Hunt7cd48f32014-11-09 23:42:50 -0800722 // TODO: add src/dst port labels etc.
723 });
Thomas Vachuska4830d392014-11-09 17:09:56 -0800724
725 // operate on both existing and new links, if necessary
726 //link .foo() .bar() ...
727
728 // operate on exiting links:
Thomas Vachuska4830d392014-11-09 17:09:56 -0800729 link.exit()
Thomas Vachuska4830d392014-11-09 17:09:56 -0800730 .attr({
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800731 'stroke-dasharray': '3, 3'
Thomas Vachuska4830d392014-11-09 17:09:56 -0800732 })
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800733 .style('opacity', 0.4)
734 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -0800735 .duration(1500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800736 .attr({
737 'stroke-dasharray': '3, 12'
738 })
739 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -0800740 .duration(500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800741 .style('opacity', 0.0)
Thomas Vachuska4830d392014-11-09 17:09:56 -0800742 .remove();
Simon Hunt99c13842014-11-06 18:23:12 -0800743 }
744
745 function createDeviceNode(device) {
746 // start with the object as is
747 var node = device,
Simon Huntbb282f52014-11-10 11:08:19 -0800748 type = device.type,
749 svgCls = type ? 'node device ' + type : 'node device';
Simon Hunt99c13842014-11-06 18:23:12 -0800750
751 // Augment as needed...
752 node.class = 'device';
Simon Huntbb282f52014-11-10 11:08:19 -0800753 node.svgClass = device.online ? svgCls + ' online' : svgCls;
Simon Hunt99c13842014-11-06 18:23:12 -0800754 positionNode(node);
755
756 // cache label array length
757 network.deviceLabelCount = device.labels.length;
Simon Hunt99c13842014-11-06 18:23:12 -0800758 return node;
759 }
760
Simon Hunt56d51852014-11-09 13:03:35 -0800761 function createHostNode(host) {
762 // start with the object as is
763 var node = host;
764
765 // Augment as needed...
766 node.class = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -0800767 if (!node.type) {
768 // TODO: perhaps type would be: {phone, tablet, laptop, endstation} ?
769 node.type = 'endstation';
770 }
Simon Hunt56d51852014-11-09 13:03:35 -0800771 node.svgClass = 'node host';
772 // TODO: consider placing near its switch, if [x,y] not defined
773 positionNode(node);
774
775 // cache label array length
776 network.hostLabelCount = host.labels.length;
Simon Hunt56d51852014-11-09 13:03:35 -0800777 return node;
778 }
779
Simon Hunt99c13842014-11-06 18:23:12 -0800780 function positionNode(node) {
781 var meta = node.metaUi,
Simon Huntac9e24f2014-11-12 10:12:21 -0800782 x = meta && meta.x,
783 y = meta && meta.y,
784 xy;
Simon Hunt99c13842014-11-06 18:23:12 -0800785
Simon Huntac9e24f2014-11-12 10:12:21 -0800786 // If we have [x,y] already, use that...
Simon Hunt99c13842014-11-06 18:23:12 -0800787 if (x && y) {
788 node.fixed = true;
Simon Huntac9e24f2014-11-12 10:12:21 -0800789 node.x = x;
790 node.y = y;
791 return;
Simon Hunt99c13842014-11-06 18:23:12 -0800792 }
Simon Huntac9e24f2014-11-12 10:12:21 -0800793
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800794 var location = node.location;
795 if (location && location.type === 'latlng') {
796 var coord = geoMapProjection([location.lng, location.lat]);
797 node.fixed = true;
798 node.x = coord[0];
799 node.y = coord[1];
800 return;
801 }
802
Simon Huntac9e24f2014-11-12 10:12:21 -0800803 // Note: Placing incoming unpinned nodes at exactly the same point
804 // (center of the view) causes them to explode outwards when
805 // the force layout kicks in. So, we spread them out a bit
806 // initially, to provide a more serene layout convergence.
807 // Additionally, if the node is a host, we place it near
808 // the device it is connected to.
809
810 function spread(s) {
811 return Math.floor((Math.random() * s) - s/2);
812 }
813
814 function randDim(dim) {
815 return dim / 2 + spread(dim * 0.7071);
816 }
817
818 function rand() {
819 return {
820 x: randDim(network.view.width()),
821 y: randDim(network.view.height())
822 };
823 }
824
825 function near(node) {
826 var min = 12,
827 dx = spread(12),
828 dy = spread(12);
829 return {
830 x: node.x + min + dx,
831 y: node.y + min + dy
832 };
833 }
834
835 function getDevice(cp) {
836 var d = network.lookup[cp.device];
837 return d || rand();
838 }
839
840 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
841 $.extend(node, xy);
Simon Hunt99c13842014-11-06 18:23:12 -0800842 }
843
Simon Hunt99c13842014-11-06 18:23:12 -0800844 function iconUrl(d) {
845 return 'img/' + d.type + '.png';
846 }
847
848 // returns the newly computed bounding box of the rectangle
849 function adjustRectToFitText(n) {
850 var text = n.select('text'),
851 box = text.node().getBBox(),
852 lab = config.labels;
853
854 text.attr('text-anchor', 'middle')
855 .attr('y', '-0.8em')
856 .attr('x', lab.imgPad/2);
857
858 // translate the bbox so that it is centered on [x,y]
859 box.x = -box.width / 2;
860 box.y = -box.height / 2;
861
862 // add padding
863 box.x -= (lab.padLR + lab.imgPad/2);
864 box.width += lab.padLR * 2 + lab.imgPad;
865 box.y -= lab.padTB;
866 box.height += lab.padTB * 2;
867
868 return box;
869 }
870
Simon Hunt1a9eff92014-11-07 11:06:34 -0800871 function mkSvgClass(d) {
872 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
873 }
874
Simon Hunt7cd48f32014-11-09 23:42:50 -0800875 function hostLabel(d) {
876 var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0;
877 return d.labels[idx];
878 }
879 function deviceLabel(d) {
880 var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0;
881 return d.labels[idx];
882 }
883 function niceLabel(label) {
884 return (label && label.trim()) ? label : '.';
885 }
886
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800887 function updateDeviceLabel(d) {
888 var label = niceLabel(deviceLabel(d)),
889 node = d.el,
890 box;
891
892 node.select('text')
893 .text(label)
894 .style('opacity', 0)
895 .transition()
896 .style('opacity', 1);
897
898 box = adjustRectToFitText(node);
899
900 node.select('rect')
901 .transition()
902 .attr(box);
903
904 node.select('image')
905 .transition()
906 .attr('x', box.x + config.icons.xoff)
907 .attr('y', box.y + config.icons.yoff);
908 }
909
910 function updateHostLabel(d) {
911 var label = hostLabel(d),
912 host = d.el;
913
914 host.select('text').text(label);
915 }
916
Simon Huntbb282f52014-11-10 11:08:19 -0800917 function updateDeviceState(nodeData) {
918 nodeData.el.classed('online', nodeData.online);
919 updateDeviceLabel(nodeData);
920 // TODO: review what else might need to be updated
921 }
922
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800923 function updateLinkState(linkData) {
924 updateLinkWidth(linkData);
925 // TODO: review what else might need to be updated
926 // update label, if showing
927 }
928
Simon Huntbb282f52014-11-10 11:08:19 -0800929 function updateHostState(hostData) {
930 updateHostLabel(hostData);
931 // TODO: review what else might need to be updated
932 }
933
934
Simon Hunt99c13842014-11-06 18:23:12 -0800935 function updateNodes() {
936 node = nodeG.selectAll('.node')
937 .data(network.nodes, function (d) { return d.id; });
938
939 // operate on existing nodes, if necessary
Simon Hunt7cd48f32014-11-09 23:42:50 -0800940 // update host labels
Simon Hunt99c13842014-11-06 18:23:12 -0800941 //node .foo() .bar() ...
942
943 // operate on entering nodes:
944 var entering = node.enter()
945 .append('g')
946 .attr({
947 id: function (d) { return safeId(d.id); },
Simon Hunt1a9eff92014-11-07 11:06:34 -0800948 class: mkSvgClass,
Simon Hunt99c13842014-11-06 18:23:12 -0800949 transform: function (d) { return translate(d.x, d.y); },
950 opacity: 0
951 })
Simon Hunt1a9eff92014-11-07 11:06:34 -0800952 .call(network.drag)
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800953 .on('mouseover', function (d) {
954 console.log(d);
955 if (d.class === 'host') {
956 requestTraffic(d);
957 }
958 })
959 .on('mouseout', function (d) {
960 console.log(d);
961 if (d.class === 'host') {
962 cancelTraffic(d);
963 }
964 })
Simon Hunt99c13842014-11-06 18:23:12 -0800965 //.on('mouseover', function (d) {})
966 .transition()
967 .attr('opacity', 1);
968
969 // augment device nodes...
970 entering.filter('.device').each(function (d) {
971 var node = d3.select(this),
972 icon = iconUrl(d),
Simon Hunt7cd48f32014-11-09 23:42:50 -0800973 label = niceLabel(deviceLabel(d)),
Simon Hunt99c13842014-11-06 18:23:12 -0800974 box;
975
Simon Hunt7cd48f32014-11-09 23:42:50 -0800976 // provide ref to element from backing data....
977 d.el = node;
978
Simon Hunt99c13842014-11-06 18:23:12 -0800979 node.append('rect')
980 .attr({
981 'rx': 5,
982 'ry': 5
983 });
984
985 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -0800986 .text(label)
Simon Hunt99c13842014-11-06 18:23:12 -0800987 .attr('dy', '1.1em');
988
989 box = adjustRectToFitText(node);
990
991 node.select('rect')
992 .attr(box);
993
994 if (icon) {
995 var cfg = config.icons;
996 node.append('svg:image')
997 .attr({
998 x: box.x + config.icons.xoff,
999 y: box.y + config.icons.yoff,
1000 width: cfg.w,
1001 height: cfg.h,
1002 'xlink:href': icon
1003 });
1004 }
1005
1006 // debug function to show the modelled x,y coordinates of nodes...
1007 if (debug('showNodeXY')) {
1008 node.select('rect').attr('fill-opacity', 0.5);
1009 node.append('circle')
1010 .attr({
1011 class: 'debug',
1012 cx: 0,
1013 cy: 0,
1014 r: '3px'
1015 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001016 }
1017 });
Simon Hunt934c3ce2014-11-05 11:45:07 -08001018
Simon Hunt56d51852014-11-09 13:03:35 -08001019 // augment host nodes...
1020 entering.filter('.host').each(function (d) {
1021 var node = d3.select(this),
Simon Hunt56d51852014-11-09 13:03:35 -08001022 box;
1023
Simon Hunt7cd48f32014-11-09 23:42:50 -08001024 // provide ref to element from backing data....
1025 d.el = node;
1026
Simon Hunt56d51852014-11-09 13:03:35 -08001027 node.append('circle')
1028 .attr('r', 8); // TODO: define host circle radius
1029
1030 // TODO: are we attaching labels to hosts?
1031 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001032 .text(hostLabel)
1033 .attr('dy', '1.3em')
1034 .attr('text-anchor', 'middle');
Simon Hunt56d51852014-11-09 13:03:35 -08001035
1036 // debug function to show the modelled x,y coordinates of nodes...
1037 if (debug('showNodeXY')) {
1038 node.select('circle').attr('fill-opacity', 0.5);
1039 node.append('circle')
1040 .attr({
1041 class: 'debug',
1042 cx: 0,
1043 cy: 0,
1044 r: '3px'
1045 });
1046 }
1047 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001048
Simon Hunt99c13842014-11-06 18:23:12 -08001049 // operate on both existing and new nodes, if necessary
1050 //node .foo() .bar() ...
Simon Huntc7ee0662014-11-05 16:44:37 -08001051
Simon Hunt99c13842014-11-06 18:23:12 -08001052 // operate on exiting nodes:
Simon Huntea80eb42014-11-11 13:46:57 -08001053 // Note that the node is removed after 2 seconds.
1054 // Sub element animations should be shorter than 2 seconds.
1055 var exiting = node.exit()
Simon Hunt44031102014-11-11 13:20:36 -08001056 .transition()
1057 .duration(2000)
Simon Huntea80eb42014-11-11 13:46:57 -08001058 .style('opacity', 0)
Simon Hunt99c13842014-11-06 18:23:12 -08001059 .remove();
Simon Huntea80eb42014-11-11 13:46:57 -08001060
1061 // host node exits....
1062 exiting.filter('.host').each(function (d) {
1063 var node = d3.select(this);
1064
1065 node.select('text')
1066 .style('opacity', 0.5)
1067 .transition()
1068 .duration(1000)
1069 .style('opacity', 0);
1070 // note, leave <g>.remove to remove this element
1071
1072 node.select('circle')
1073 .style('stroke-fill', '#555')
1074 .style('fill', '#888')
1075 .style('opacity', 0.5)
1076 .transition()
1077 .duration(1500)
1078 .attr('r', 0);
1079 // note, leave <g>.remove to remove this element
1080
1081 });
1082
1083 // TODO: device node exits
Simon Huntc7ee0662014-11-05 16:44:37 -08001084 }
1085
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001086 function find(id, array) {
1087 for (var idx = 0, n = array.length; idx < n; idx++) {
1088 if (array[idx].id === id) {
1089 return idx;
1090 }
1091 }
1092 return -1;
1093 }
1094
1095 function removeLinkElement(linkData) {
1096 // remove from lookup cache
1097 delete network.lookup[linkData.id];
1098 // remove from links array
1099 var idx = find(linkData.id, network.links);
Simon Huntfc274c92014-11-11 11:05:46 -08001100 network.links.splice(idx, 1);
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001101 // remove from SVG
1102 updateLinks();
Simon Hunt44031102014-11-11 13:20:36 -08001103 network.force.resume();
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001104 }
Simon Huntc7ee0662014-11-05 16:44:37 -08001105
Simon Hunt44031102014-11-11 13:20:36 -08001106 function removeHostElement(hostData) {
1107 // first, remove associated hostLink...
1108 removeLinkElement(hostData.linkData);
1109
1110 // remove from lookup cache
1111 delete network.lookup[hostData.id];
1112 // remove from nodes array
1113 var idx = find(hostData.id, network.nodes);
1114 network.nodes.splice(idx, 1);
1115 // remove from SVG
1116 updateNodes();
1117 network.force.resume();
1118 }
1119
1120
Simon Huntc7ee0662014-11-05 16:44:37 -08001121 function tick() {
1122 node.attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001123 transform: function (d) { return translate(d.x, d.y); }
Simon Huntc7ee0662014-11-05 16:44:37 -08001124 });
1125
1126 link.attr({
1127 x1: function (d) { return d.source.x; },
1128 y1: function (d) { return d.source.y; },
1129 x2: function (d) { return d.target.x; },
1130 y2: function (d) { return d.target.y; }
1131 });
1132 }
Simon Hunt934c3ce2014-11-05 11:45:07 -08001133
1134 // ==============================
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001135 // Web-Socket for live data
1136
1137 function webSockUrl() {
1138 return document.location.toString()
1139 .replace(/\#.*/, '')
1140 .replace('http://', 'ws://')
1141 .replace('https://', 'wss://')
1142 .replace('index2.html', config.webSockUrl);
1143 }
1144
1145 webSock = {
1146 ws : null,
1147
1148 connect : function() {
1149 webSock.ws = new WebSocket(webSockUrl());
1150
1151 webSock.ws.onopen = function() {
Simon Hunt0c6d4192014-11-12 12:07:10 -08001152 noWebSock(false);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001153 };
1154
1155 webSock.ws.onmessage = function(m) {
1156 if (m.data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001157 wsTraceRx(m.data);
Thomas Vachuskad472c6e2014-11-07 19:11:05 -08001158 handleServerEvent(JSON.parse(m.data));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001159 }
1160 };
1161
1162 webSock.ws.onclose = function(m) {
1163 webSock.ws = null;
Simon Hunt0c6d4192014-11-12 12:07:10 -08001164 noWebSock(true);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001165 };
1166 },
1167
1168 send : function(text) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001169 if (text != null) {
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001170 webSock._send(text);
1171 }
1172 },
1173
1174 _send : function(message) {
1175 if (webSock.ws) {
1176 webSock.ws.send(message);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001177 } else {
Simon Hunt56d51852014-11-09 13:03:35 -08001178 network.view.alert('no web socket open\n\n' + message);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001179 }
1180 }
1181
1182 };
1183
Simon Hunt0c6d4192014-11-12 12:07:10 -08001184 function noWebSock(b) {
1185 mask.style('display',b ? 'block' : 'none');
1186 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001187
Simon Hunt61d04042014-11-11 17:27:16 -08001188 // TODO: use cache of pending messages (key = sid) to reconcile responses
1189
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001190 function sendMessage(evType, payload) {
1191 var toSend = {
Simon Huntbb282f52014-11-10 11:08:19 -08001192 event: evType,
1193 sid: ++sid,
1194 payload: payload
1195 },
1196 asText = JSON.stringify(toSend);
1197 wsTraceTx(asText);
1198 webSock.send(asText);
1199 }
1200
1201 function wsTraceTx(msg) {
1202 wsTrace('tx', msg);
1203 }
1204 function wsTraceRx(msg) {
1205 wsTrace('rx', msg);
1206 }
1207 function wsTrace(rxtx, msg) {
Simon Huntbb282f52014-11-10 11:08:19 -08001208 console.log('[' + rxtx + '] ' + msg);
1209 // TODO: integrate with trace view
1210 //if (trace) {
1211 // trace.output(rxtx, msg);
1212 //}
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001213 }
1214
1215
1216 // ==============================
1217 // Selection stuff
1218
1219 function selectObject(obj, el) {
1220 var n,
1221 meta = d3.event.sourceEvent.metaKey;
1222
1223 if (el) {
1224 n = d3.select(el);
1225 } else {
1226 node.each(function(d) {
1227 if (d == obj) {
1228 n = d3.select(el = this);
1229 }
1230 });
1231 }
1232 if (!n) return;
1233
1234 if (meta && n.classed('selected')) {
1235 deselectObject(obj.id);
Simon Hunt61d04042014-11-11 17:27:16 -08001236 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001237 return;
1238 }
1239
1240 if (!meta) {
1241 deselectAll();
1242 }
1243
Simon Huntc31d5692014-11-12 13:27:18 -08001244 selections[obj.id] = { obj: obj, el: el };
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001245 selectOrder.push(obj.id);
1246
1247 n.classed('selected', true);
Simon Hunt61d04042014-11-11 17:27:16 -08001248 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001249 }
1250
1251 function deselectObject(id) {
Simon Huntc31d5692014-11-12 13:27:18 -08001252 var obj = selections[id],
1253 idx;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001254 if (obj) {
1255 d3.select(obj.el).classed('selected', false);
Simon Hunt61d04042014-11-11 17:27:16 -08001256 delete selections[id];
Simon Huntc31d5692014-11-12 13:27:18 -08001257 idx = $.inArray(id, selectOrder);
1258 if (idx >= 0) {
1259 selectOrder.splice(idx, 1);
1260 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001261 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001262 }
1263
1264 function deselectAll() {
1265 // deselect all nodes in the network...
1266 node.classed('selected', false);
1267 selections = {};
1268 selectOrder = [];
Simon Hunt61d04042014-11-11 17:27:16 -08001269 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001270 }
1271
Simon Hunt61d04042014-11-11 17:27:16 -08001272 // FIXME: this click handler does not get unloaded when the view does
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001273 $('#view').on('click', function(e) {
1274 if (!$(e.target).closest('.node').length) {
1275 if (!e.metaKey) {
1276 deselectAll();
1277 }
1278 }
1279 });
1280
Simon Hunt61d04042014-11-11 17:27:16 -08001281 // update the state of the detail pane, based on current selections
1282 function updateDetailPane() {
1283 var nSel = selectOrder.length;
1284 if (!nSel) {
1285 detailPane.hide();
1286 } else if (nSel === 1) {
1287 singleSelect();
1288 } else {
1289 multiSelect();
1290 }
1291 }
1292
1293 function singleSelect() {
1294 requestDetails();
Simon Huntb53e0682014-11-12 13:32:01 -08001295 // NOTE: detail pane will be shown from showDetails event callback
Simon Hunt61d04042014-11-11 17:27:16 -08001296 }
1297
1298 function multiSelect() {
Simon Huntb53e0682014-11-12 13:32:01 -08001299 populateMultiSelect();
1300 // TODO: Add multi-select actions ...
1301 }
1302
1303 function addSep(tbody) {
1304 var tr = tbody.append('tr');
1305 $('<hr>').appendTo(tr.append('td').attr('colspan', 2));
1306 }
1307
1308 function addProp(tbody, label, value) {
1309 var tr = tbody.append('tr');
1310
1311 tr.append('td')
1312 .attr('class', 'label')
1313 .text(label + ' :');
1314
1315 tr.append('td')
1316 .attr('class', 'value')
1317 .text(value);
1318 }
1319
1320 function populateMultiSelect() {
1321 detailPane.empty();
1322
1323 var title = detailPane.append("h2"),
1324 table = detailPane.append("table"),
1325 tbody = table.append("tbody");
1326
1327 title.text('Multi-Select...');
1328
1329 selectOrder.forEach(function (d, i) {
1330 addProp(tbody, i+1, d);
1331 });
Simon Hunt61d04042014-11-11 17:27:16 -08001332 }
1333
1334 function populateDetails(data) {
1335 detailPane.empty();
1336
1337 var title = detailPane.append("h2"),
1338 table = detailPane.append("table"),
1339 tbody = table.append("tbody");
1340
1341 $('<img src="img/' + data.type + '.png">').appendTo(title);
1342 $('<span>').attr('class', 'icon').text(data.id).appendTo(title);
1343
1344 data.propOrder.forEach(function(p) {
1345 if (p === '-') {
1346 addSep(tbody);
1347 } else {
1348 addProp(tbody, p, data.props[p]);
1349 }
1350 });
Simon Hunt61d04042014-11-11 17:27:16 -08001351 }
1352
1353 // ==============================
1354 // Test harness code
Simon Hunt56d51852014-11-09 13:03:35 -08001355
1356 function prepareScenario(view, ctx, dbg) {
1357 var sc = scenario,
1358 urlSc = sc.evDir + ctx + sc.evScenario;
1359
1360 if (!ctx) {
1361 view.alert("No scenario specified (null ctx)");
1362 return;
1363 }
1364
1365 sc.view = view;
1366 sc.ctx = ctx;
1367 sc.debug = dbg;
1368 sc.evNumber = 0;
1369
1370 d3.json(urlSc, function(err, data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001371 var p = data && data.params || {},
1372 desc = data && data.description || null,
Simon Huntfc274c92014-11-11 11:05:46 -08001373 intro = data && data.title;
Simon Huntbb282f52014-11-10 11:08:19 -08001374
Simon Hunt56d51852014-11-09 13:03:35 -08001375 if (err) {
1376 view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err);
1377 } else {
1378 sc.params = p;
Simon Huntbb282f52014-11-10 11:08:19 -08001379 if (desc) {
1380 intro += '\n\n ' + desc.join('\n ');
1381 }
1382 view.alert(intro);
Simon Hunt56d51852014-11-09 13:03:35 -08001383 }
1384 });
1385
1386 }
1387
Simon Hunt0c6d4192014-11-12 12:07:10 -08001388
1389 function para(sel, text) {
1390 sel.append('p').text(text);
1391 }
1392
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001393 // ==============================
Simon Hunt142d0032014-11-04 20:13:09 -08001394 // View life-cycle callbacks
Simon Hunt195cb382014-11-03 17:50:51 -08001395
Simon Huntf67722a2014-11-10 09:32:06 -08001396 function preload(view, ctx, flags) {
Simon Hunt142d0032014-11-04 20:13:09 -08001397 var w = view.width(),
1398 h = view.height(),
Simon Huntc7ee0662014-11-05 16:44:37 -08001399 fcfg = config.force,
1400 fpad = fcfg.pad,
1401 forceDim = [w - 2*fpad, h - 2*fpad];
Simon Hunt195cb382014-11-03 17:50:51 -08001402
Simon Huntbb282f52014-11-10 11:08:19 -08001403 // TODO: set trace api
1404 //trace = onos.exported.webSockTrace;
1405
Simon Hunt142d0032014-11-04 20:13:09 -08001406 // NOTE: view.$div is a D3 selection of the view's div
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001407 var viewBox = '0 0 ' + config.logicalSize + ' ' + config.logicalSize;
1408 svg = view.$div.append('svg').attr('viewBox', viewBox);
Simon Hunt934c3ce2014-11-05 11:45:07 -08001409 setSize(svg, view);
1410
Simon Hunt1a9eff92014-11-07 11:06:34 -08001411 // add blue glow filter to svg layer
1412 d3u.appendGlow(svg);
1413
Simon Huntc7ee0662014-11-05 16:44:37 -08001414 // group for the topology
1415 topoG = svg.append('g')
Simon Huntd3b7d512014-11-12 15:48:41 -08001416 .attr('id', 'topo-G')
Simon Huntc7ee0662014-11-05 16:44:37 -08001417 .attr('transform', fcfg.translate());
1418
1419 // subgroups for links and nodes
1420 linkG = topoG.append('g').attr('id', 'links');
1421 nodeG = topoG.append('g').attr('id', 'nodes');
1422
1423 // selection of nodes and links
1424 link = linkG.selectAll('.link');
1425 node = nodeG.selectAll('.node');
1426
Simon Hunt7cd48f32014-11-09 23:42:50 -08001427 function chrg(d) {
1428 return fcfg.charge[d.class] || -12000;
1429 }
Simon Hunt99c13842014-11-06 18:23:12 -08001430 function ldist(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08001431 return fcfg.linkDistance[d.type] || 50;
Simon Hunt99c13842014-11-06 18:23:12 -08001432 }
1433 function lstrg(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08001434 // 0.0 - 1.0
1435 return fcfg.linkStrength[d.type] || 1.0;
Simon Hunt99c13842014-11-06 18:23:12 -08001436 }
1437
Simon Hunt1a9eff92014-11-07 11:06:34 -08001438 function selectCb(d, self) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001439 selectObject(d, self);
Simon Hunt1a9eff92014-11-07 11:06:34 -08001440 }
1441
1442 function atDragEnd(d, self) {
Simon Hunt56d51852014-11-09 13:03:35 -08001443 // once we've finished moving, pin the node in position
1444 d.fixed = true;
1445 d3.select(self).classed('fixed', true);
1446 if (config.useLiveData) {
Simon Hunt902c9922014-11-11 11:59:31 -08001447 sendUpdateMeta(d);
Simon Hunt1a9eff92014-11-07 11:06:34 -08001448 }
1449 }
1450
Simon Hunt902c9922014-11-11 11:59:31 -08001451 function sendUpdateMeta(d) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001452 sendMessage('updateMeta', {
1453 id: d.id,
1454 'class': d.class,
Simon Hunt902c9922014-11-11 11:59:31 -08001455 'memento': {
1456 x: Math.floor(d.x),
1457 y: Math.floor(d.y)
1458 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001459 });
1460 }
1461
Simon Huntc7ee0662014-11-05 16:44:37 -08001462 // set up the force layout
1463 network.force = d3.layout.force()
1464 .size(forceDim)
1465 .nodes(network.nodes)
1466 .links(network.links)
Simon Hunt7cd48f32014-11-09 23:42:50 -08001467 .gravity(0.4)
1468 .friction(0.7)
1469 .charge(chrg)
Simon Hunt99c13842014-11-06 18:23:12 -08001470 .linkDistance(ldist)
1471 .linkStrength(lstrg)
Simon Huntc7ee0662014-11-05 16:44:37 -08001472 .on('tick', tick);
Simon Hunt195cb382014-11-03 17:50:51 -08001473
Simon Hunt1a9eff92014-11-07 11:06:34 -08001474 network.drag = d3u.createDragBehavior(network.force, selectCb, atDragEnd);
Simon Hunt0c6d4192014-11-12 12:07:10 -08001475
1476 // create mask layer for when we lose connection to server.
1477 mask = view.$div.append('div').attr('id','topo-mask');
1478 para(mask, 'Oops!');
1479 para(mask, 'Web-socket connection to server closed...');
1480 para(mask, 'Try refreshing the page.');
Simon Hunt1a9eff92014-11-07 11:06:34 -08001481 }
Simon Hunt195cb382014-11-03 17:50:51 -08001482
Simon Hunt56d51852014-11-09 13:03:35 -08001483 function load(view, ctx, flags) {
Simon Huntf67722a2014-11-10 09:32:06 -08001484 // resize, in case the window was resized while we were not loaded
1485 resize(view, ctx, flags);
1486
Simon Hunt99c13842014-11-06 18:23:12 -08001487 // cache the view token, so network topo functions can access it
1488 network.view = view;
Simon Hunt56d51852014-11-09 13:03:35 -08001489 config.useLiveData = !flags.local;
1490
1491 if (!config.useLiveData) {
1492 prepareScenario(view, ctx, flags.debug);
1493 }
Simon Hunt99c13842014-11-06 18:23:12 -08001494
1495 // set our radio buttons and key bindings
Simon Hunt934c3ce2014-11-05 11:45:07 -08001496 view.setRadio(btnSet);
1497 view.setKeys(keyDispatch);
Simon Hunt195cb382014-11-03 17:50:51 -08001498
Simon Huntd3b7d512014-11-12 15:48:41 -08001499 // Load map data asynchronously; complete startup after that..
1500 loadGeoJsonData();
1501 }
1502
1503 // TODO: move these to config/state portion of script
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001504 var geoJsonUrl = 'json/map/continental_us.json', // TODO: Paul
Simon Huntd3b7d512014-11-12 15:48:41 -08001505 geoJson;
1506
1507 function loadGeoJsonData() {
1508 d3.json(geoJsonUrl, function (err, data) {
1509 if (err) {
1510 // fall back to USA map background
1511 loadStaticMap();
1512 } else {
1513 geoJson = data;
1514 loadGeoMap();
1515 }
1516
1517 // finally, connect to the server...
1518 if (config.useLiveData) {
1519 webSock.connect();
1520 }
1521 });
1522 }
1523
1524 function showBg() {
1525 return config.options.showBackground ? 'visible' : 'hidden';
1526 }
1527
1528 function loadStaticMap() {
1529 fnTrace('loadStaticMap', config.backgroundUrl);
1530 var w = network.view.width(),
1531 h = network.view.height();
1532
1533 // load the background image
1534 bgImg = svg.insert('svg:image', '#topo-G')
1535 .attr({
1536 id: 'topo-bg',
1537 width: w,
1538 height: h,
1539 'xlink:href': config.backgroundUrl
1540 })
1541 .style({
1542 visibility: showBg()
1543 });
1544 }
1545
1546 function loadGeoMap() {
1547 fnTrace('loadGeoMap', geoJsonUrl);
Simon Huntd3b7d512014-11-12 15:48:41 -08001548
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001549 // extracts the topojson data into geocoordinate-based geometry
1550 var topoData = topojson.feature(geoJson, geoJson.objects.states);
Simon Huntd3b7d512014-11-12 15:48:41 -08001551
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001552 // see: http://bl.ocks.org/mbostock/4707858
1553 geoMapProjection = d3.geo.mercator();
1554 var path = d3.geo.path().projection(geoMapProjection);
Simon Huntd3b7d512014-11-12 15:48:41 -08001555
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001556 geoMapProjection
1557 .scale(1)
1558 .translate([0, 0]);
Simon Huntd3b7d512014-11-12 15:48:41 -08001559
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001560 // [[x1,y1],[x2,y2]]
1561 var b = path.bounds(topoData);
1562 // TODO: why 1.75?
1563 var s = 1.75 / Math.max((b[1][0] - b[0][0]) / config.logicalSize, (b[1][1] - b[0][1]) / config.logicalSize);
1564 var t = [(config.logicalSize - s * (b[1][0] + b[0][0])) / 2, (config.logicalSize - s * (b[1][1] + b[0][1])) / 2];
Simon Huntd3b7d512014-11-12 15:48:41 -08001565
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001566 geoMapProjection
1567 .scale(s)
1568 .translate(t);
1569
1570 bgImg = svg.insert("g", '#topo-G');
1571 bgImg.attr('id', 'map').selectAll('path')
1572 .data(topoData.features)
1573 .enter()
1574 .append('path')
1575 .attr('d', path);
Simon Hunt195cb382014-11-03 17:50:51 -08001576 }
1577
Simon Huntf67722a2014-11-10 09:32:06 -08001578 function resize(view, ctx, flags) {
Simon Hunt934c3ce2014-11-05 11:45:07 -08001579 setSize(svg, view);
Simon Hunt99c13842014-11-06 18:23:12 -08001580
1581 // TODO: hook to recompute layout, perhaps? work with zoom/pan code
1582 // adjust force layout size
Simon Hunt142d0032014-11-04 20:13:09 -08001583 }
1584
1585
1586 // ==============================
1587 // View registration
Simon Hunt195cb382014-11-03 17:50:51 -08001588
Simon Hunt25248912014-11-04 11:25:48 -08001589 onos.ui.addView('topo', {
Simon Hunt142d0032014-11-04 20:13:09 -08001590 preload: preload,
1591 load: load,
1592 resize: resize
Simon Hunt195cb382014-11-03 17:50:51 -08001593 });
1594
Simon Hunt61d04042014-11-11 17:27:16 -08001595 detailPane = onos.ui.addFloatingPanel('topo-detail');
1596
Simon Hunt195cb382014-11-03 17:50:51 -08001597}(ONOS));