blob: 64825dc27b48ea4241f0508235599aac5e3adadb [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
130 X: requestPath
Simon Hunt934c3ce2014-11-05 11:45:07 -0800131 };
Simon Hunt142d0032014-11-04 20:13:09 -0800132
Simon Hunt195cb382014-11-03 17:50:51 -0800133 // state variables
Simon Hunt99c13842014-11-06 18:23:12 -0800134 var network = {
Simon Hunt50128c02014-11-08 13:36:15 -0800135 view: null, // view token reference
Simon Hunt99c13842014-11-06 18:23:12 -0800136 nodes: [],
137 links: [],
138 lookup: {}
139 },
Simon Hunt56d51852014-11-09 13:03:35 -0800140 scenario = {
141 evDir: 'json/ev/',
142 evScenario: '/scenario.json',
143 evPrefix: '/ev_',
144 evOnos: '_onos.json',
145 evUi: '_ui.json',
146 ctx: null,
147 params: {},
148 evNumber: 0,
149 view: null,
150 debug: false
151 },
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800152 webSock,
Simon Hunt56d51852014-11-09 13:03:35 -0800153 deviceLabelIndex = 0,
154 hostLabelIndex = 0,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800155
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800156 selectOrder = [],
157 selections = {},
158
Simon Hunt195cb382014-11-03 17:50:51 -0800159 highlighted = null,
160 hovered = null,
161 viewMode = 'showAll',
162 portLabelsOn = false;
163
Simon Hunt934c3ce2014-11-05 11:45:07 -0800164 // D3 selections
165 var svg,
166 bgImg,
Simon Huntc7ee0662014-11-05 16:44:37 -0800167 topoG,
168 nodeG,
169 linkG,
170 node,
171 link;
Simon Hunt195cb382014-11-03 17:50:51 -0800172
Simon Hunt142d0032014-11-04 20:13:09 -0800173 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800174 // For Debugging / Development
Simon Hunt195cb382014-11-03 17:50:51 -0800175
Simon Hunt99c13842014-11-06 18:23:12 -0800176 function note(label, msg) {
177 console.log('NOTE: ' + label + ': ' + msg);
Simon Hunt195cb382014-11-03 17:50:51 -0800178 }
179
Simon Hunt99c13842014-11-06 18:23:12 -0800180 function debug(what) {
181 return config.debugOn && config.debug[what];
Simon Hunt934c3ce2014-11-05 11:45:07 -0800182 }
183
Simon Huntfc274c92014-11-11 11:05:46 -0800184 function fnTrace(msg, id) {
185 if (config.fnTrace) {
186 console.log('FN: ' + msg + ' [' + id + ']');
187 }
188 }
Simon Hunt99c13842014-11-06 18:23:12 -0800189
Simon Hunt934c3ce2014-11-05 11:45:07 -0800190 // ==============================
191 // Key Callbacks
192
Simon Hunt99c13842014-11-06 18:23:12 -0800193 function testMe(view) {
Simon Hunt50128c02014-11-08 13:36:15 -0800194 view.alert('test');
Simon Hunt99c13842014-11-06 18:23:12 -0800195 }
196
Simon Hunt56d51852014-11-09 13:03:35 -0800197 function abortIfLive() {
Simon Hunt50128c02014-11-08 13:36:15 -0800198 if (config.useLiveData) {
Simon Hunt56d51852014-11-09 13:03:35 -0800199 scenario.view.alert("Sorry, currently using live data..");
200 return true;
Simon Hunt50128c02014-11-08 13:36:15 -0800201 }
Simon Hunt56d51852014-11-09 13:03:35 -0800202 return false;
203 }
Simon Hunt50128c02014-11-08 13:36:15 -0800204
Simon Hunt56d51852014-11-09 13:03:35 -0800205 function testDebug(msg) {
206 if (scenario.debug) {
207 scenario.view.alert(msg);
208 }
209 }
Simon Hunt99c13842014-11-06 18:23:12 -0800210
Simon Hunt56d51852014-11-09 13:03:35 -0800211 function injectTestEvent(view) {
212 if (abortIfLive()) { return; }
213 var sc = scenario,
214 evn = ++sc.evNumber,
215 pfx = sc.evDir + sc.ctx + sc.evPrefix + evn,
216 onosUrl = pfx + sc.evOnos,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800217 uiUrl = pfx + sc.evUi,
218 stack = [
219 { url: onosUrl, cb: handleServerEvent },
220 { url: uiUrl, cb: handleUiEvent }
221 ];
222 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800223 }
224
Simon Hunt7cd48f32014-11-09 23:42:50 -0800225 function recurseFetchEvent(stack, evn) {
226 var v = scenario.view,
227 frame;
228 if (stack.length === 0) {
Simon Huntfc274c92014-11-11 11:05:46 -0800229 v.alert('Oops!\n\nNo event #' + evn + ' found.');
Simon Hunt7cd48f32014-11-09 23:42:50 -0800230 return;
231 }
232 frame = stack.shift();
233
234 d3.json(frame.url, function (err, data) {
Simon Hunt99c13842014-11-06 18:23:12 -0800235 if (err) {
Simon Hunt56d51852014-11-09 13:03:35 -0800236 if (err.status === 404) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800237 // if we didn't find the data, try the next stack frame
238 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800239 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800240 v.alert('non-404 error:\n\n' + frame.url + '\n\n' + err);
Simon Hunt56d51852014-11-09 13:03:35 -0800241 }
Simon Hunt99c13842014-11-06 18:23:12 -0800242 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800243 testDebug('loaded: ' + frame.url);
244 frame.cb(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800245 }
246 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800247
Simon Hunt56d51852014-11-09 13:03:35 -0800248 }
Simon Hunt50128c02014-11-08 13:36:15 -0800249
Simon Hunt56d51852014-11-09 13:03:35 -0800250 function handleUiEvent(data) {
Simon Huntbb282f52014-11-10 11:08:19 -0800251 scenario.view.alert('UI Tx: ' + data.event + '\n\n' +
252 JSON.stringify(data));
Simon Hunt56d51852014-11-09 13:03:35 -0800253 }
254
255 function injectStartupEvents(view) {
256 var last = scenario.params.lastAuto || 0;
257 if (abortIfLive()) { return; }
258
259 while (scenario.evNumber < last) {
Simon Hunt1a9eff92014-11-07 11:06:34 -0800260 injectTestEvent(view);
261 }
262 }
263
Simon Hunt934c3ce2014-11-05 11:45:07 -0800264 function toggleBg() {
265 var vis = bgImg.style('visibility');
266 bgImg.style('visibility', (vis === 'hidden') ? 'visible' : 'hidden');
267 }
268
Simon Hunt99c13842014-11-06 18:23:12 -0800269 function cycleLabels() {
Simon Huntbb282f52014-11-10 11:08:19 -0800270 deviceLabelIndex = (deviceLabelIndex === network.deviceLabelCount - 1)
271 ? 0 : deviceLabelIndex + 1;
Simon Hunt5f36d342014-11-08 21:33:14 -0800272
Simon Hunt99c13842014-11-06 18:23:12 -0800273 network.nodes.forEach(function (d) {
Simon Huntbb282f52014-11-10 11:08:19 -0800274 if (d.class === 'device') {
275 updateDeviceLabel(d);
276 }
Simon Hunt99c13842014-11-06 18:23:12 -0800277 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800278 }
279
280 function togglePorts(view) {
Simon Hunt50128c02014-11-08 13:36:15 -0800281 view.alert('togglePorts() callback')
Simon Hunt934c3ce2014-11-05 11:45:07 -0800282 }
283
284 function unpin(view) {
Simon Hunt50128c02014-11-08 13:36:15 -0800285 view.alert('unpin() callback')
Simon Hunt934c3ce2014-11-05 11:45:07 -0800286 }
287
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800288 function requestPath(view) {
289 var payload = {
290 one: selections[selectOrder[0]].obj.id,
291 two: selections[selectOrder[1]].obj.id
292 }
293 sendMessage('requestPath', payload);
294 }
295
Simon Hunt934c3ce2014-11-05 11:45:07 -0800296 // ==============================
297 // Radio Button Callbacks
298
Simon Hunt195cb382014-11-03 17:50:51 -0800299 function showAllLayers() {
Simon Hunt142d0032014-11-04 20:13:09 -0800300// network.node.classed('inactive', false);
301// network.link.classed('inactive', false);
302// d3.selectAll('svg .port').classed('inactive', false);
303// d3.selectAll('svg .portText').classed('inactive', false);
Simon Hunt934c3ce2014-11-05 11:45:07 -0800304 // TODO ...
Simon Hunt50128c02014-11-08 13:36:15 -0800305 network.view.alert('showAllLayers() callback');
Simon Hunt195cb382014-11-03 17:50:51 -0800306 }
307
308 function showPacketLayer() {
Simon Hunt934c3ce2014-11-05 11:45:07 -0800309 showAllLayers();
310 // TODO ...
Simon Hunt50128c02014-11-08 13:36:15 -0800311 network.view.alert('showPacketLayer() callback');
Simon Hunt195cb382014-11-03 17:50:51 -0800312 }
313
314 function showOpticalLayer() {
Simon Hunt934c3ce2014-11-05 11:45:07 -0800315 showAllLayers();
316 // TODO ...
Simon Hunt50128c02014-11-08 13:36:15 -0800317 network.view.alert('showOpticalLayer() callback');
Simon Hunt195cb382014-11-03 17:50:51 -0800318 }
319
Simon Hunt142d0032014-11-04 20:13:09 -0800320 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800321 // Private functions
322
Simon Hunt99c13842014-11-06 18:23:12 -0800323 function safeId(s) {
324 return s.replace(/[^a-z0-9]/gi, '-');
325 }
326
Simon Huntc7ee0662014-11-05 16:44:37 -0800327 // set the size of the given element to that of the view (reduced if padded)
328 function setSize(el, view, pad) {
329 var padding = pad ? pad * 2 : 0;
Simon Hunt934c3ce2014-11-05 11:45:07 -0800330 el.attr({
Simon Huntc7ee0662014-11-05 16:44:37 -0800331 width: view.width() - padding,
332 height: view.height() - padding
Simon Hunt934c3ce2014-11-05 11:45:07 -0800333 });
334 }
335
Simon Hunt934c3ce2014-11-05 11:45:07 -0800336
Simon Hunt99c13842014-11-06 18:23:12 -0800337 // ==============================
338 // Event handlers for server-pushed events
339
Simon Huntbb282f52014-11-10 11:08:19 -0800340 function logicError(msg) {
341 // TODO, report logic error to server, via websock, so it can be logged
342 network.view.alert('Logic Error:\n\n' + msg);
Simon Huntfc274c92014-11-11 11:05:46 -0800343 console.warn(msg);
Simon Huntbb282f52014-11-10 11:08:19 -0800344 }
345
Simon Hunt99c13842014-11-06 18:23:12 -0800346 var eventDispatch = {
347 addDevice: addDevice,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800348 addLink: addLink,
Simon Hunt56d51852014-11-09 13:03:35 -0800349 addHost: addHost,
Simon Huntbb282f52014-11-10 11:08:19 -0800350 updateDevice: updateDevice,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800351 updateLink: updateLink,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800352 updateHost: updateHost,
Simon Huntbb282f52014-11-10 11:08:19 -0800353 removeDevice: stillToImplement,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800354 removeLink: removeLink,
Simon Hunt44031102014-11-11 13:20:36 -0800355 removeHost: removeHost,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800356 showPath: showPath
Simon Hunt99c13842014-11-06 18:23:12 -0800357 };
358
359 function addDevice(data) {
Simon Huntfc274c92014-11-11 11:05:46 -0800360 fnTrace('addDevice', data.payload.id);
Simon Hunt99c13842014-11-06 18:23:12 -0800361 var device = data.payload,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800362 nodeData = createDeviceNode(device);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800363 network.nodes.push(nodeData);
364 network.lookup[nodeData.id] = nodeData;
Simon Hunt99c13842014-11-06 18:23:12 -0800365 updateNodes();
366 network.force.start();
367 }
368
Simon Hunt99c13842014-11-06 18:23:12 -0800369 function addLink(data) {
Simon Huntfc274c92014-11-11 11:05:46 -0800370 fnTrace('addLink', data.payload.id);
Simon Hunt99c13842014-11-06 18:23:12 -0800371 var link = data.payload,
372 lnk = createLink(link);
Simon Hunt99c13842014-11-06 18:23:12 -0800373 if (lnk) {
Simon Hunt99c13842014-11-06 18:23:12 -0800374 network.links.push(lnk);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800375 network.lookup[lnk.id] = lnk;
Simon Hunt99c13842014-11-06 18:23:12 -0800376 updateLinks();
377 network.force.start();
378 }
379 }
380
Simon Hunt56d51852014-11-09 13:03:35 -0800381 function addHost(data) {
Simon Huntfc274c92014-11-11 11:05:46 -0800382 fnTrace('addHost', data.payload.id);
Simon Hunt56d51852014-11-09 13:03:35 -0800383 var host = data.payload,
384 node = createHostNode(host),
385 lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800386 network.nodes.push(node);
387 network.lookup[host.id] = node;
388 updateNodes();
389
390 lnk = createHostLink(host);
391 if (lnk) {
Simon Hunt44031102014-11-11 13:20:36 -0800392 node.linkData = lnk; // cache ref on its host
Simon Hunt56d51852014-11-09 13:03:35 -0800393 network.links.push(lnk);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800394 network.lookup[host.ingress] = lnk;
395 network.lookup[host.egress] = lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800396 updateLinks();
397 }
398 network.force.start();
399 }
400
Simon Hunt44031102014-11-11 13:20:36 -0800401 // TODO: fold updateX(...) methods into one base method; remove duplication
Simon Huntbb282f52014-11-10 11:08:19 -0800402 function updateDevice(data) {
Simon Huntfc274c92014-11-11 11:05:46 -0800403 fnTrace('updateDevice', data.payload.id);
Simon Huntbb282f52014-11-10 11:08:19 -0800404 var device = data.payload,
405 id = device.id,
406 nodeData = network.lookup[id];
407 if (nodeData) {
408 $.extend(nodeData, device);
409 updateDeviceState(nodeData);
410 } else {
411 logicError('updateDevice lookup fail. ID = "' + id + '"');
412 }
413 }
414
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800415 function updateLink(data) {
Simon Huntfc274c92014-11-11 11:05:46 -0800416 fnTrace('updateLink', data.payload.id);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800417 var link = data.payload,
418 id = link.id,
419 linkData = network.lookup[id];
420 if (linkData) {
421 $.extend(linkData, link);
422 updateLinkState(linkData);
423 } else {
424 logicError('updateLink lookup fail. ID = "' + id + '"');
425 }
426 }
427
Simon Hunt7cd48f32014-11-09 23:42:50 -0800428 function updateHost(data) {
Simon Huntfc274c92014-11-11 11:05:46 -0800429 fnTrace('updateHost', data.payload.id);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800430 var host = data.payload,
Simon Huntbb282f52014-11-10 11:08:19 -0800431 id = host.id,
432 hostData = network.lookup[id];
433 if (hostData) {
434 $.extend(hostData, host);
435 updateHostState(hostData);
436 } else {
437 logicError('updateHost lookup fail. ID = "' + id + '"');
438 }
Simon Hunt7cd48f32014-11-09 23:42:50 -0800439 }
440
Simon Hunt44031102014-11-11 13:20:36 -0800441 // TODO: fold removeX(...) methods into base method - remove dup code
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800442 function removeLink(data) {
Simon Huntfc274c92014-11-11 11:05:46 -0800443 fnTrace('removeLink', data.payload.id);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800444 var link = data.payload,
445 id = link.id,
446 linkData = network.lookup[id];
447 if (linkData) {
448 removeLinkElement(linkData);
449 } else {
450 logicError('removeLink lookup fail. ID = "' + id + '"');
451 }
452 }
453
Simon Hunt44031102014-11-11 13:20:36 -0800454 function removeHost(data) {
455 fnTrace('removeHost', data.payload.id);
456 var host = data.payload,
457 id = host.id,
458 hostData = network.lookup[id];
459 if (hostData) {
460 removeHostElement(hostData);
461 } else {
462 logicError('removeHost lookup fail. ID = "' + id + '"');
463 }
464 }
465
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800466 function showPath(data) {
Simon Huntfc274c92014-11-11 11:05:46 -0800467 fnTrace('showPath', data.payload.id);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800468 var links = data.payload.links,
469 s = [ data.event + "\n" + links.length ];
470 links.forEach(function (d, i) {
471 s.push(d);
472 });
473 network.view.alert(s.join('\n'));
474
475 links.forEach(function (d, i) {
476 var link = network.lookup[d];
477 if (link) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800478 link.el.classed('showPath', true);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800479 }
480 });
481
482 // TODO: add selection-highlite lines to links
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800483 }
484
Simon Hunt56d51852014-11-09 13:03:35 -0800485 // ...............................
486
487 function stillToImplement(data) {
488 var p = data.payload;
489 note(data.event, p.id);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800490 network.view.alert('Not yet implemented: "' + data.event + '"');
Simon Hunt56d51852014-11-09 13:03:35 -0800491 }
Simon Hunt99c13842014-11-06 18:23:12 -0800492
493 function unknownEvent(data) {
Simon Hunt50128c02014-11-08 13:36:15 -0800494 network.view.alert('Unknown event type: "' + data.event + '"');
Simon Hunt99c13842014-11-06 18:23:12 -0800495 }
496
497 function handleServerEvent(data) {
498 var fn = eventDispatch[data.event] || unknownEvent;
499 fn(data);
500 }
501
502 // ==============================
503 // force layout modification functions
504
505 function translate(x, y) {
506 return 'translate(' + x + ',' + y + ')';
507 }
508
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800509 function missMsg(what, id) {
510 return '\n[' + what + '] "' + id + '" missing ';
511 }
512
513 function linkEndPoints(srcId, dstId) {
514 var srcNode = network.lookup[srcId],
515 dstNode = network.lookup[dstId],
516 sMiss = !srcNode ? missMsg('src', srcId) : '',
517 dMiss = !dstNode ? missMsg('dst', dstId) : '';
518
519 if (sMiss || dMiss) {
520 logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
521 return null;
522 }
523 return {
524 source: srcNode,
525 target: dstNode,
526 x1: srcNode.x,
527 y1: srcNode.y,
528 x2: dstNode.x,
529 y2: dstNode.y
530 };
531 }
532
Simon Hunt56d51852014-11-09 13:03:35 -0800533 function createHostLink(host) {
534 var src = host.id,
535 dst = host.cp.device,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800536 id = host.ingress,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800537 lnk = linkEndPoints(src, dst);
Simon Hunt56d51852014-11-09 13:03:35 -0800538
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800539 if (!lnk) {
Simon Hunt56d51852014-11-09 13:03:35 -0800540 return null;
541 }
542
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800543 // Synthesize link ...
544 $.extend(lnk, {
Thomas Vachuska4830d392014-11-09 17:09:56 -0800545 id: id,
Simon Hunt56d51852014-11-09 13:03:35 -0800546 class: 'link',
Simon Hunt7cd48f32014-11-09 23:42:50 -0800547 type: 'hostLink',
Simon Hunt56d51852014-11-09 13:03:35 -0800548 svgClass: 'link hostLink',
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800549 linkWidth: 1
Simon Hunt7cd48f32014-11-09 23:42:50 -0800550 });
Simon Hunt99c13842014-11-06 18:23:12 -0800551 return lnk;
552 }
553
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800554 function createLink(link) {
555 var lnk = linkEndPoints(link.src, link.dst),
556 type = link.type;
557
558 if (!lnk) {
559 return null;
560 }
561
562 // merge in remaining data
563 $.extend(lnk, link, {
564 class: 'link',
565 svgClass: type ? 'link ' + type : 'link'
566 });
567 return lnk;
Simon Hunt1a9eff92014-11-07 11:06:34 -0800568 }
569
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800570 var widthRatio = 1.4,
571 linkScale = d3.scale.linear()
572 .domain([1, 12])
573 .range([widthRatio, 12 * widthRatio])
574 .clamp(true);
575
576 function updateLinkWidth (d) {
577 // TODO: watch out for .showPath/.showTraffic classes
578 d.el.transition()
579 .duration(1000)
580 .attr('stroke-width', linkScale(d.linkWidth));
581 }
582
583
Simon Hunt99c13842014-11-06 18:23:12 -0800584 function updateLinks() {
585 link = linkG.selectAll('.link')
586 .data(network.links, function (d) { return d.id; });
587
588 // operate on existing links, if necessary
589 // link .foo() .bar() ...
590
591 // operate on entering links:
592 var entering = link.enter()
593 .append('line')
594 .attr({
Simon Hunt99c13842014-11-06 18:23:12 -0800595 class: function (d) { return d.svgClass; },
596 x1: function (d) { return d.x1; },
597 y1: function (d) { return d.y1; },
598 x2: function (d) { return d.x2; },
599 y2: function (d) { return d.y2; },
Simon Hunt1a9eff92014-11-07 11:06:34 -0800600 stroke: config.topo.linkInColor,
601 'stroke-width': config.topo.linkInWidth
Simon Hunt99c13842014-11-06 18:23:12 -0800602 })
603 .transition().duration(1000)
604 .attr({
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800605 'stroke-width': function (d) { return linkScale(d.linkWidth); },
Simon Hunt99c13842014-11-06 18:23:12 -0800606 stroke: '#666' // TODO: remove explicit stroke, rather...
607 });
608
609 // augment links
Simon Hunt7cd48f32014-11-09 23:42:50 -0800610 entering.each(function (d) {
611 var link = d3.select(this);
612 // provide ref to element selection from backing data....
613 d.el = link;
Simon Hunt99c13842014-11-06 18:23:12 -0800614
Simon Hunt7cd48f32014-11-09 23:42:50 -0800615 // TODO: add src/dst port labels etc.
616 });
Thomas Vachuska4830d392014-11-09 17:09:56 -0800617
618 // operate on both existing and new links, if necessary
619 //link .foo() .bar() ...
620
621 // operate on exiting links:
Thomas Vachuska4830d392014-11-09 17:09:56 -0800622 link.exit()
Thomas Vachuska4830d392014-11-09 17:09:56 -0800623 .attr({
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800624 'stroke-dasharray': '3, 3'
Thomas Vachuska4830d392014-11-09 17:09:56 -0800625 })
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800626 .style('opacity', 0.4)
627 .transition()
628 .duration(2000)
629 .attr({
630 'stroke-dasharray': '3, 12'
631 })
632 .transition()
633 .duration(1000)
634 .style('opacity', 0.0)
Thomas Vachuska4830d392014-11-09 17:09:56 -0800635 .remove();
Simon Hunt99c13842014-11-06 18:23:12 -0800636 }
637
638 function createDeviceNode(device) {
639 // start with the object as is
640 var node = device,
Simon Huntbb282f52014-11-10 11:08:19 -0800641 type = device.type,
642 svgCls = type ? 'node device ' + type : 'node device';
Simon Hunt99c13842014-11-06 18:23:12 -0800643
644 // Augment as needed...
645 node.class = 'device';
Simon Huntbb282f52014-11-10 11:08:19 -0800646 node.svgClass = device.online ? svgCls + ' online' : svgCls;
Simon Hunt99c13842014-11-06 18:23:12 -0800647 positionNode(node);
648
649 // cache label array length
650 network.deviceLabelCount = device.labels.length;
Simon Hunt99c13842014-11-06 18:23:12 -0800651 return node;
652 }
653
Simon Hunt56d51852014-11-09 13:03:35 -0800654 function createHostNode(host) {
655 // start with the object as is
656 var node = host;
657
658 // Augment as needed...
659 node.class = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -0800660 if (!node.type) {
661 // TODO: perhaps type would be: {phone, tablet, laptop, endstation} ?
662 node.type = 'endstation';
663 }
Simon Hunt56d51852014-11-09 13:03:35 -0800664 node.svgClass = 'node host';
665 // TODO: consider placing near its switch, if [x,y] not defined
666 positionNode(node);
667
668 // cache label array length
669 network.hostLabelCount = host.labels.length;
Simon Hunt56d51852014-11-09 13:03:35 -0800670 return node;
671 }
672
Simon Hunt99c13842014-11-06 18:23:12 -0800673 function positionNode(node) {
674 var meta = node.metaUi,
675 x = 0,
676 y = 0;
677
678 if (meta) {
679 x = meta.x;
680 y = meta.y;
681 }
682 if (x && y) {
683 node.fixed = true;
684 }
685 node.x = x || network.view.width() / 2;
686 node.y = y || network.view.height() / 2;
687 }
688
Simon Hunt99c13842014-11-06 18:23:12 -0800689 function iconUrl(d) {
690 return 'img/' + d.type + '.png';
691 }
692
693 // returns the newly computed bounding box of the rectangle
694 function adjustRectToFitText(n) {
695 var text = n.select('text'),
696 box = text.node().getBBox(),
697 lab = config.labels;
698
699 text.attr('text-anchor', 'middle')
700 .attr('y', '-0.8em')
701 .attr('x', lab.imgPad/2);
702
703 // translate the bbox so that it is centered on [x,y]
704 box.x = -box.width / 2;
705 box.y = -box.height / 2;
706
707 // add padding
708 box.x -= (lab.padLR + lab.imgPad/2);
709 box.width += lab.padLR * 2 + lab.imgPad;
710 box.y -= lab.padTB;
711 box.height += lab.padTB * 2;
712
713 return box;
714 }
715
Simon Hunt1a9eff92014-11-07 11:06:34 -0800716 function mkSvgClass(d) {
717 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
718 }
719
Simon Hunt7cd48f32014-11-09 23:42:50 -0800720 function hostLabel(d) {
721 var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0;
722 return d.labels[idx];
723 }
724 function deviceLabel(d) {
725 var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0;
726 return d.labels[idx];
727 }
728 function niceLabel(label) {
729 return (label && label.trim()) ? label : '.';
730 }
731
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800732 function updateDeviceLabel(d) {
733 var label = niceLabel(deviceLabel(d)),
734 node = d.el,
735 box;
736
737 node.select('text')
738 .text(label)
739 .style('opacity', 0)
740 .transition()
741 .style('opacity', 1);
742
743 box = adjustRectToFitText(node);
744
745 node.select('rect')
746 .transition()
747 .attr(box);
748
749 node.select('image')
750 .transition()
751 .attr('x', box.x + config.icons.xoff)
752 .attr('y', box.y + config.icons.yoff);
753 }
754
755 function updateHostLabel(d) {
756 var label = hostLabel(d),
757 host = d.el;
758
759 host.select('text').text(label);
760 }
761
Simon Huntbb282f52014-11-10 11:08:19 -0800762 function updateDeviceState(nodeData) {
763 nodeData.el.classed('online', nodeData.online);
764 updateDeviceLabel(nodeData);
765 // TODO: review what else might need to be updated
766 }
767
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800768 function updateLinkState(linkData) {
769 updateLinkWidth(linkData);
770 // TODO: review what else might need to be updated
771 // update label, if showing
772 }
773
Simon Huntbb282f52014-11-10 11:08:19 -0800774 function updateHostState(hostData) {
775 updateHostLabel(hostData);
776 // TODO: review what else might need to be updated
777 }
778
779
Simon Hunt99c13842014-11-06 18:23:12 -0800780 function updateNodes() {
781 node = nodeG.selectAll('.node')
782 .data(network.nodes, function (d) { return d.id; });
783
784 // operate on existing nodes, if necessary
Simon Hunt7cd48f32014-11-09 23:42:50 -0800785 // update host labels
Simon Hunt99c13842014-11-06 18:23:12 -0800786 //node .foo() .bar() ...
787
788 // operate on entering nodes:
789 var entering = node.enter()
790 .append('g')
791 .attr({
792 id: function (d) { return safeId(d.id); },
Simon Hunt1a9eff92014-11-07 11:06:34 -0800793 class: mkSvgClass,
Simon Hunt99c13842014-11-06 18:23:12 -0800794 transform: function (d) { return translate(d.x, d.y); },
795 opacity: 0
796 })
Simon Hunt1a9eff92014-11-07 11:06:34 -0800797 .call(network.drag)
Simon Hunt99c13842014-11-06 18:23:12 -0800798 //.on('mouseover', function (d) {})
799 //.on('mouseover', function (d) {})
800 .transition()
801 .attr('opacity', 1);
802
803 // augment device nodes...
804 entering.filter('.device').each(function (d) {
805 var node = d3.select(this),
806 icon = iconUrl(d),
Simon Hunt7cd48f32014-11-09 23:42:50 -0800807 label = niceLabel(deviceLabel(d)),
Simon Hunt99c13842014-11-06 18:23:12 -0800808 box;
809
Simon Hunt7cd48f32014-11-09 23:42:50 -0800810 // provide ref to element from backing data....
811 d.el = node;
812
Simon Hunt99c13842014-11-06 18:23:12 -0800813 node.append('rect')
814 .attr({
815 'rx': 5,
816 'ry': 5
817 });
818
819 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -0800820 .text(label)
Simon Hunt99c13842014-11-06 18:23:12 -0800821 .attr('dy', '1.1em');
822
823 box = adjustRectToFitText(node);
824
825 node.select('rect')
826 .attr(box);
827
828 if (icon) {
829 var cfg = config.icons;
830 node.append('svg:image')
831 .attr({
832 x: box.x + config.icons.xoff,
833 y: box.y + config.icons.yoff,
834 width: cfg.w,
835 height: cfg.h,
836 'xlink:href': icon
837 });
838 }
839
840 // debug function to show the modelled x,y coordinates of nodes...
841 if (debug('showNodeXY')) {
842 node.select('rect').attr('fill-opacity', 0.5);
843 node.append('circle')
844 .attr({
845 class: 'debug',
846 cx: 0,
847 cy: 0,
848 r: '3px'
849 });
Simon Huntc7ee0662014-11-05 16:44:37 -0800850 }
851 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800852
Simon Hunt56d51852014-11-09 13:03:35 -0800853 // augment host nodes...
854 entering.filter('.host').each(function (d) {
855 var node = d3.select(this),
Simon Hunt56d51852014-11-09 13:03:35 -0800856 box;
857
Simon Hunt7cd48f32014-11-09 23:42:50 -0800858 // provide ref to element from backing data....
859 d.el = node;
860
Simon Hunt56d51852014-11-09 13:03:35 -0800861 node.append('circle')
862 .attr('r', 8); // TODO: define host circle radius
863
864 // TODO: are we attaching labels to hosts?
865 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -0800866 .text(hostLabel)
867 .attr('dy', '1.3em')
868 .attr('text-anchor', 'middle');
Simon Hunt56d51852014-11-09 13:03:35 -0800869
870 // debug function to show the modelled x,y coordinates of nodes...
871 if (debug('showNodeXY')) {
872 node.select('circle').attr('fill-opacity', 0.5);
873 node.append('circle')
874 .attr({
875 class: 'debug',
876 cx: 0,
877 cy: 0,
878 r: '3px'
879 });
880 }
881 });
Simon Huntc7ee0662014-11-05 16:44:37 -0800882
Simon Hunt99c13842014-11-06 18:23:12 -0800883 // operate on both existing and new nodes, if necessary
884 //node .foo() .bar() ...
Simon Huntc7ee0662014-11-05 16:44:37 -0800885
Simon Hunt99c13842014-11-06 18:23:12 -0800886 // operate on exiting nodes:
887 // TODO: figure out how to remove the node 'g' AND its children
888 node.exit()
889 .transition()
Simon Hunt44031102014-11-11 13:20:36 -0800890 .style('opacity', 0.4)
891 .attr('fill', '#888')
892 .transition()
893 .duration(2000)
Simon Hunt99c13842014-11-06 18:23:12 -0800894 .attr({
895 opacity: 0,
Simon Hunt99c13842014-11-06 18:23:12 -0800896 r: 0
897 })
898 .remove();
Simon Huntc7ee0662014-11-05 16:44:37 -0800899 }
900
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800901 function find(id, array) {
902 for (var idx = 0, n = array.length; idx < n; idx++) {
903 if (array[idx].id === id) {
904 return idx;
905 }
906 }
907 return -1;
908 }
909
910 function removeLinkElement(linkData) {
911 // remove from lookup cache
912 delete network.lookup[linkData.id];
913 // remove from links array
914 var idx = find(linkData.id, network.links);
Simon Huntfc274c92014-11-11 11:05:46 -0800915 network.links.splice(idx, 1);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800916 // remove from SVG
917 updateLinks();
Simon Hunt44031102014-11-11 13:20:36 -0800918 network.force.resume();
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800919 }
Simon Huntc7ee0662014-11-05 16:44:37 -0800920
Simon Hunt44031102014-11-11 13:20:36 -0800921 function removeHostElement(hostData) {
922 // first, remove associated hostLink...
923 removeLinkElement(hostData.linkData);
924
925 // remove from lookup cache
926 delete network.lookup[hostData.id];
927 // remove from nodes array
928 var idx = find(hostData.id, network.nodes);
929 network.nodes.splice(idx, 1);
930 // remove from SVG
931 updateNodes();
932 network.force.resume();
933 }
934
935
Simon Huntc7ee0662014-11-05 16:44:37 -0800936 function tick() {
937 node.attr({
Simon Hunt99c13842014-11-06 18:23:12 -0800938 transform: function (d) { return translate(d.x, d.y); }
Simon Huntc7ee0662014-11-05 16:44:37 -0800939 });
940
941 link.attr({
942 x1: function (d) { return d.source.x; },
943 y1: function (d) { return d.source.y; },
944 x2: function (d) { return d.target.x; },
945 y2: function (d) { return d.target.y; }
946 });
947 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800948
949 // ==============================
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800950 // Web-Socket for live data
951
952 function webSockUrl() {
953 return document.location.toString()
954 .replace(/\#.*/, '')
955 .replace('http://', 'ws://')
956 .replace('https://', 'wss://')
957 .replace('index2.html', config.webSockUrl);
958 }
959
960 webSock = {
961 ws : null,
962
963 connect : function() {
964 webSock.ws = new WebSocket(webSockUrl());
965
966 webSock.ws.onopen = function() {
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800967 };
968
969 webSock.ws.onmessage = function(m) {
970 if (m.data) {
Simon Huntbb282f52014-11-10 11:08:19 -0800971 wsTraceRx(m.data);
Thomas Vachuskad472c6e2014-11-07 19:11:05 -0800972 handleServerEvent(JSON.parse(m.data));
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800973 }
974 };
975
976 webSock.ws.onclose = function(m) {
977 webSock.ws = null;
978 };
979 },
980
981 send : function(text) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800982 if (text != null) {
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800983 webSock._send(text);
984 }
985 },
986
987 _send : function(message) {
988 if (webSock.ws) {
989 webSock.ws.send(message);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800990 } else {
Simon Hunt56d51852014-11-09 13:03:35 -0800991 network.view.alert('no web socket open\n\n' + message);
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800992 }
993 }
994
995 };
996
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800997 var sid = 0;
998
999 function sendMessage(evType, payload) {
1000 var toSend = {
Simon Huntbb282f52014-11-10 11:08:19 -08001001 event: evType,
1002 sid: ++sid,
1003 payload: payload
1004 },
1005 asText = JSON.stringify(toSend);
1006 wsTraceTx(asText);
1007 webSock.send(asText);
1008 }
1009
1010 function wsTraceTx(msg) {
1011 wsTrace('tx', msg);
1012 }
1013 function wsTraceRx(msg) {
1014 wsTrace('rx', msg);
1015 }
1016 function wsTrace(rxtx, msg) {
1017
1018 console.log('[' + rxtx + '] ' + msg);
1019 // TODO: integrate with trace view
1020 //if (trace) {
1021 // trace.output(rxtx, msg);
1022 //}
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001023 }
1024
1025
1026 // ==============================
1027 // Selection stuff
1028
1029 function selectObject(obj, el) {
1030 var n,
1031 meta = d3.event.sourceEvent.metaKey;
1032
1033 if (el) {
1034 n = d3.select(el);
1035 } else {
1036 node.each(function(d) {
1037 if (d == obj) {
1038 n = d3.select(el = this);
1039 }
1040 });
1041 }
1042 if (!n) return;
1043
1044 if (meta && n.classed('selected')) {
1045 deselectObject(obj.id);
1046 //flyinPane(null);
1047 return;
1048 }
1049
1050 if (!meta) {
1051 deselectAll();
1052 }
1053
Simon Hunt5f36d342014-11-08 21:33:14 -08001054 selections[obj.id] = { obj: obj, el : el};
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001055 selectOrder.push(obj.id);
1056
1057 n.classed('selected', true);
1058 //flyinPane(obj);
1059 }
1060
1061 function deselectObject(id) {
1062 var obj = selections[id];
1063 if (obj) {
1064 d3.select(obj.el).classed('selected', false);
1065 selections[id] = null;
1066 // TODO: use splice to remove element
1067 }
1068 //flyinPane(null);
1069 }
1070
1071 function deselectAll() {
1072 // deselect all nodes in the network...
1073 node.classed('selected', false);
1074 selections = {};
1075 selectOrder = [];
1076 //flyinPane(null);
1077 }
1078
Simon Hunt56d51852014-11-09 13:03:35 -08001079 // TODO: this click handler does not get unloaded when the view does
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001080 $('#view').on('click', function(e) {
1081 if (!$(e.target).closest('.node').length) {
1082 if (!e.metaKey) {
1083 deselectAll();
1084 }
1085 }
1086 });
1087
Simon Hunt56d51852014-11-09 13:03:35 -08001088
1089 function prepareScenario(view, ctx, dbg) {
1090 var sc = scenario,
1091 urlSc = sc.evDir + ctx + sc.evScenario;
1092
1093 if (!ctx) {
1094 view.alert("No scenario specified (null ctx)");
1095 return;
1096 }
1097
1098 sc.view = view;
1099 sc.ctx = ctx;
1100 sc.debug = dbg;
1101 sc.evNumber = 0;
1102
1103 d3.json(urlSc, function(err, data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001104 var p = data && data.params || {},
1105 desc = data && data.description || null,
Simon Huntfc274c92014-11-11 11:05:46 -08001106 intro = data && data.title;
Simon Huntbb282f52014-11-10 11:08:19 -08001107
Simon Hunt56d51852014-11-09 13:03:35 -08001108 if (err) {
1109 view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err);
1110 } else {
1111 sc.params = p;
Simon Huntbb282f52014-11-10 11:08:19 -08001112 if (desc) {
1113 intro += '\n\n ' + desc.join('\n ');
1114 }
1115 view.alert(intro);
Simon Hunt56d51852014-11-09 13:03:35 -08001116 }
1117 });
1118
1119 }
1120
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001121 // ==============================
Simon Hunt142d0032014-11-04 20:13:09 -08001122 // View life-cycle callbacks
Simon Hunt195cb382014-11-03 17:50:51 -08001123
Simon Huntf67722a2014-11-10 09:32:06 -08001124 function preload(view, ctx, flags) {
Simon Hunt142d0032014-11-04 20:13:09 -08001125 var w = view.width(),
1126 h = view.height(),
1127 idBg = view.uid('bg'),
Simon Huntc7ee0662014-11-05 16:44:37 -08001128 showBg = config.options.showBackground ? 'visible' : 'hidden',
1129 fcfg = config.force,
1130 fpad = fcfg.pad,
1131 forceDim = [w - 2*fpad, h - 2*fpad];
Simon Hunt195cb382014-11-03 17:50:51 -08001132
Simon Huntbb282f52014-11-10 11:08:19 -08001133 // TODO: set trace api
1134 //trace = onos.exported.webSockTrace;
1135
Simon Hunt142d0032014-11-04 20:13:09 -08001136 // NOTE: view.$div is a D3 selection of the view's div
1137 svg = view.$div.append('svg');
Simon Hunt934c3ce2014-11-05 11:45:07 -08001138 setSize(svg, view);
1139
Simon Hunt1a9eff92014-11-07 11:06:34 -08001140 // add blue glow filter to svg layer
1141 d3u.appendGlow(svg);
1142
Simon Hunt142d0032014-11-04 20:13:09 -08001143 // load the background image
1144 bgImg = svg.append('svg:image')
Simon Hunt195cb382014-11-03 17:50:51 -08001145 .attr({
Simon Hunt142d0032014-11-04 20:13:09 -08001146 id: idBg,
1147 width: w,
1148 height: h,
Simon Hunt195cb382014-11-03 17:50:51 -08001149 'xlink:href': config.backgroundUrl
1150 })
Simon Hunt142d0032014-11-04 20:13:09 -08001151 .style({
1152 visibility: showBg
Simon Hunt195cb382014-11-03 17:50:51 -08001153 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001154
1155 // group for the topology
1156 topoG = svg.append('g')
1157 .attr('transform', fcfg.translate());
1158
1159 // subgroups for links and nodes
1160 linkG = topoG.append('g').attr('id', 'links');
1161 nodeG = topoG.append('g').attr('id', 'nodes');
1162
1163 // selection of nodes and links
1164 link = linkG.selectAll('.link');
1165 node = nodeG.selectAll('.node');
1166
Simon Hunt7cd48f32014-11-09 23:42:50 -08001167 function chrg(d) {
1168 return fcfg.charge[d.class] || -12000;
1169 }
Simon Hunt99c13842014-11-06 18:23:12 -08001170 function ldist(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08001171 return fcfg.linkDistance[d.type] || 50;
Simon Hunt99c13842014-11-06 18:23:12 -08001172 }
1173 function lstrg(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08001174 // 0.0 - 1.0
1175 return fcfg.linkStrength[d.type] || 1.0;
Simon Hunt99c13842014-11-06 18:23:12 -08001176 }
1177
Simon Hunt1a9eff92014-11-07 11:06:34 -08001178 function selectCb(d, self) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001179 selectObject(d, self);
Simon Hunt1a9eff92014-11-07 11:06:34 -08001180 }
1181
1182 function atDragEnd(d, self) {
Simon Hunt56d51852014-11-09 13:03:35 -08001183 // once we've finished moving, pin the node in position
1184 d.fixed = true;
1185 d3.select(self).classed('fixed', true);
1186 if (config.useLiveData) {
Simon Hunt902c9922014-11-11 11:59:31 -08001187 sendUpdateMeta(d);
Simon Hunt1a9eff92014-11-07 11:06:34 -08001188 }
1189 }
1190
Simon Hunt902c9922014-11-11 11:59:31 -08001191 function sendUpdateMeta(d) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001192 sendMessage('updateMeta', {
1193 id: d.id,
1194 'class': d.class,
Simon Hunt902c9922014-11-11 11:59:31 -08001195 'memento': {
1196 x: Math.floor(d.x),
1197 y: Math.floor(d.y)
1198 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001199 });
1200 }
1201
Simon Huntc7ee0662014-11-05 16:44:37 -08001202 // set up the force layout
1203 network.force = d3.layout.force()
1204 .size(forceDim)
1205 .nodes(network.nodes)
1206 .links(network.links)
Simon Hunt7cd48f32014-11-09 23:42:50 -08001207 .gravity(0.4)
1208 .friction(0.7)
1209 .charge(chrg)
Simon Hunt99c13842014-11-06 18:23:12 -08001210 .linkDistance(ldist)
1211 .linkStrength(lstrg)
Simon Huntc7ee0662014-11-05 16:44:37 -08001212 .on('tick', tick);
Simon Hunt195cb382014-11-03 17:50:51 -08001213
Simon Hunt1a9eff92014-11-07 11:06:34 -08001214 network.drag = d3u.createDragBehavior(network.force, selectCb, atDragEnd);
1215 }
Simon Hunt195cb382014-11-03 17:50:51 -08001216
Simon Hunt56d51852014-11-09 13:03:35 -08001217 function load(view, ctx, flags) {
Simon Huntf67722a2014-11-10 09:32:06 -08001218 // resize, in case the window was resized while we were not loaded
1219 resize(view, ctx, flags);
1220
Simon Hunt99c13842014-11-06 18:23:12 -08001221 // cache the view token, so network topo functions can access it
1222 network.view = view;
Simon Hunt56d51852014-11-09 13:03:35 -08001223 config.useLiveData = !flags.local;
1224
1225 if (!config.useLiveData) {
1226 prepareScenario(view, ctx, flags.debug);
1227 }
Simon Hunt99c13842014-11-06 18:23:12 -08001228
1229 // set our radio buttons and key bindings
Simon Hunt934c3ce2014-11-05 11:45:07 -08001230 view.setRadio(btnSet);
1231 view.setKeys(keyDispatch);
Simon Hunt195cb382014-11-03 17:50:51 -08001232
Simon Hunt50128c02014-11-08 13:36:15 -08001233 if (config.useLiveData) {
1234 webSock.connect();
1235 }
Simon Hunt195cb382014-11-03 17:50:51 -08001236 }
1237
Simon Huntf67722a2014-11-10 09:32:06 -08001238 function resize(view, ctx, flags) {
Simon Hunt934c3ce2014-11-05 11:45:07 -08001239 setSize(svg, view);
1240 setSize(bgImg, view);
Simon Hunt99c13842014-11-06 18:23:12 -08001241
1242 // TODO: hook to recompute layout, perhaps? work with zoom/pan code
1243 // adjust force layout size
Simon Hunt142d0032014-11-04 20:13:09 -08001244 }
1245
1246
1247 // ==============================
1248 // View registration
Simon Hunt195cb382014-11-03 17:50:51 -08001249
Simon Hunt25248912014-11-04 11:25:48 -08001250 onos.ui.addView('topo', {
Simon Hunt142d0032014-11-04 20:13:09 -08001251 preload: preload,
1252 load: load,
1253 resize: resize
Simon Hunt195cb382014-11-03 17:50:51 -08001254 });
1255
Simon Hunt195cb382014-11-03 17:50:51 -08001256}(ONOS));