blob: 24053d8387ab41655df29c40c81444e7ffe86432 [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()
Simon Huntea80eb42014-11-11 13:46:57 -0800628 .duration(1500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800629 .attr({
630 'stroke-dasharray': '3, 12'
631 })
632 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -0800633 .duration(500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800634 .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:
Simon Huntea80eb42014-11-11 13:46:57 -0800887 // Note that the node is removed after 2 seconds.
888 // Sub element animations should be shorter than 2 seconds.
889 var exiting = node.exit()
Simon Hunt44031102014-11-11 13:20:36 -0800890 .transition()
891 .duration(2000)
Simon Huntea80eb42014-11-11 13:46:57 -0800892 .style('opacity', 0)
Simon Hunt99c13842014-11-06 18:23:12 -0800893 .remove();
Simon Huntea80eb42014-11-11 13:46:57 -0800894
895 // host node exits....
896 exiting.filter('.host').each(function (d) {
897 var node = d3.select(this);
898
899 node.select('text')
900 .style('opacity', 0.5)
901 .transition()
902 .duration(1000)
903 .style('opacity', 0);
904 // note, leave <g>.remove to remove this element
905
906 node.select('circle')
907 .style('stroke-fill', '#555')
908 .style('fill', '#888')
909 .style('opacity', 0.5)
910 .transition()
911 .duration(1500)
912 .attr('r', 0);
913 // note, leave <g>.remove to remove this element
914
915 });
916
917 // TODO: device node exits
Simon Huntc7ee0662014-11-05 16:44:37 -0800918 }
919
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800920 function find(id, array) {
921 for (var idx = 0, n = array.length; idx < n; idx++) {
922 if (array[idx].id === id) {
923 return idx;
924 }
925 }
926 return -1;
927 }
928
929 function removeLinkElement(linkData) {
930 // remove from lookup cache
931 delete network.lookup[linkData.id];
932 // remove from links array
933 var idx = find(linkData.id, network.links);
Simon Huntfc274c92014-11-11 11:05:46 -0800934 network.links.splice(idx, 1);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800935 // remove from SVG
936 updateLinks();
Simon Hunt44031102014-11-11 13:20:36 -0800937 network.force.resume();
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800938 }
Simon Huntc7ee0662014-11-05 16:44:37 -0800939
Simon Hunt44031102014-11-11 13:20:36 -0800940 function removeHostElement(hostData) {
941 // first, remove associated hostLink...
942 removeLinkElement(hostData.linkData);
943
944 // remove from lookup cache
945 delete network.lookup[hostData.id];
946 // remove from nodes array
947 var idx = find(hostData.id, network.nodes);
948 network.nodes.splice(idx, 1);
949 // remove from SVG
950 updateNodes();
951 network.force.resume();
952 }
953
954
Simon Huntc7ee0662014-11-05 16:44:37 -0800955 function tick() {
956 node.attr({
Simon Hunt99c13842014-11-06 18:23:12 -0800957 transform: function (d) { return translate(d.x, d.y); }
Simon Huntc7ee0662014-11-05 16:44:37 -0800958 });
959
960 link.attr({
961 x1: function (d) { return d.source.x; },
962 y1: function (d) { return d.source.y; },
963 x2: function (d) { return d.target.x; },
964 y2: function (d) { return d.target.y; }
965 });
966 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800967
968 // ==============================
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800969 // Web-Socket for live data
970
971 function webSockUrl() {
972 return document.location.toString()
973 .replace(/\#.*/, '')
974 .replace('http://', 'ws://')
975 .replace('https://', 'wss://')
976 .replace('index2.html', config.webSockUrl);
977 }
978
979 webSock = {
980 ws : null,
981
982 connect : function() {
983 webSock.ws = new WebSocket(webSockUrl());
984
985 webSock.ws.onopen = function() {
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800986 };
987
988 webSock.ws.onmessage = function(m) {
989 if (m.data) {
Simon Huntbb282f52014-11-10 11:08:19 -0800990 wsTraceRx(m.data);
Thomas Vachuskad472c6e2014-11-07 19:11:05 -0800991 handleServerEvent(JSON.parse(m.data));
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800992 }
993 };
994
995 webSock.ws.onclose = function(m) {
996 webSock.ws = null;
997 };
998 },
999
1000 send : function(text) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001001 if (text != null) {
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001002 webSock._send(text);
1003 }
1004 },
1005
1006 _send : function(message) {
1007 if (webSock.ws) {
1008 webSock.ws.send(message);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001009 } else {
Simon Hunt56d51852014-11-09 13:03:35 -08001010 network.view.alert('no web socket open\n\n' + message);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001011 }
1012 }
1013
1014 };
1015
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001016 var sid = 0;
1017
1018 function sendMessage(evType, payload) {
1019 var toSend = {
Simon Huntbb282f52014-11-10 11:08:19 -08001020 event: evType,
1021 sid: ++sid,
1022 payload: payload
1023 },
1024 asText = JSON.stringify(toSend);
1025 wsTraceTx(asText);
1026 webSock.send(asText);
1027 }
1028
1029 function wsTraceTx(msg) {
1030 wsTrace('tx', msg);
1031 }
1032 function wsTraceRx(msg) {
1033 wsTrace('rx', msg);
1034 }
1035 function wsTrace(rxtx, msg) {
1036
1037 console.log('[' + rxtx + '] ' + msg);
1038 // TODO: integrate with trace view
1039 //if (trace) {
1040 // trace.output(rxtx, msg);
1041 //}
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001042 }
1043
1044
1045 // ==============================
1046 // Selection stuff
1047
1048 function selectObject(obj, el) {
1049 var n,
1050 meta = d3.event.sourceEvent.metaKey;
1051
1052 if (el) {
1053 n = d3.select(el);
1054 } else {
1055 node.each(function(d) {
1056 if (d == obj) {
1057 n = d3.select(el = this);
1058 }
1059 });
1060 }
1061 if (!n) return;
1062
1063 if (meta && n.classed('selected')) {
1064 deselectObject(obj.id);
1065 //flyinPane(null);
1066 return;
1067 }
1068
1069 if (!meta) {
1070 deselectAll();
1071 }
1072
Simon Hunt5f36d342014-11-08 21:33:14 -08001073 selections[obj.id] = { obj: obj, el : el};
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001074 selectOrder.push(obj.id);
1075
1076 n.classed('selected', true);
1077 //flyinPane(obj);
1078 }
1079
1080 function deselectObject(id) {
1081 var obj = selections[id];
1082 if (obj) {
1083 d3.select(obj.el).classed('selected', false);
1084 selections[id] = null;
1085 // TODO: use splice to remove element
1086 }
1087 //flyinPane(null);
1088 }
1089
1090 function deselectAll() {
1091 // deselect all nodes in the network...
1092 node.classed('selected', false);
1093 selections = {};
1094 selectOrder = [];
1095 //flyinPane(null);
1096 }
1097
Simon Hunt56d51852014-11-09 13:03:35 -08001098 // TODO: this click handler does not get unloaded when the view does
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001099 $('#view').on('click', function(e) {
1100 if (!$(e.target).closest('.node').length) {
1101 if (!e.metaKey) {
1102 deselectAll();
1103 }
1104 }
1105 });
1106
Simon Hunt56d51852014-11-09 13:03:35 -08001107
1108 function prepareScenario(view, ctx, dbg) {
1109 var sc = scenario,
1110 urlSc = sc.evDir + ctx + sc.evScenario;
1111
1112 if (!ctx) {
1113 view.alert("No scenario specified (null ctx)");
1114 return;
1115 }
1116
1117 sc.view = view;
1118 sc.ctx = ctx;
1119 sc.debug = dbg;
1120 sc.evNumber = 0;
1121
1122 d3.json(urlSc, function(err, data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001123 var p = data && data.params || {},
1124 desc = data && data.description || null,
Simon Huntfc274c92014-11-11 11:05:46 -08001125 intro = data && data.title;
Simon Huntbb282f52014-11-10 11:08:19 -08001126
Simon Hunt56d51852014-11-09 13:03:35 -08001127 if (err) {
1128 view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err);
1129 } else {
1130 sc.params = p;
Simon Huntbb282f52014-11-10 11:08:19 -08001131 if (desc) {
1132 intro += '\n\n ' + desc.join('\n ');
1133 }
1134 view.alert(intro);
Simon Hunt56d51852014-11-09 13:03:35 -08001135 }
1136 });
1137
1138 }
1139
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001140 // ==============================
Simon Hunt142d0032014-11-04 20:13:09 -08001141 // View life-cycle callbacks
Simon Hunt195cb382014-11-03 17:50:51 -08001142
Simon Huntf67722a2014-11-10 09:32:06 -08001143 function preload(view, ctx, flags) {
Simon Hunt142d0032014-11-04 20:13:09 -08001144 var w = view.width(),
1145 h = view.height(),
1146 idBg = view.uid('bg'),
Simon Huntc7ee0662014-11-05 16:44:37 -08001147 showBg = config.options.showBackground ? 'visible' : 'hidden',
1148 fcfg = config.force,
1149 fpad = fcfg.pad,
1150 forceDim = [w - 2*fpad, h - 2*fpad];
Simon Hunt195cb382014-11-03 17:50:51 -08001151
Simon Huntbb282f52014-11-10 11:08:19 -08001152 // TODO: set trace api
1153 //trace = onos.exported.webSockTrace;
1154
Simon Hunt142d0032014-11-04 20:13:09 -08001155 // NOTE: view.$div is a D3 selection of the view's div
1156 svg = view.$div.append('svg');
Simon Hunt934c3ce2014-11-05 11:45:07 -08001157 setSize(svg, view);
1158
Simon Hunt1a9eff92014-11-07 11:06:34 -08001159 // add blue glow filter to svg layer
1160 d3u.appendGlow(svg);
1161
Simon Hunt142d0032014-11-04 20:13:09 -08001162 // load the background image
1163 bgImg = svg.append('svg:image')
Simon Hunt195cb382014-11-03 17:50:51 -08001164 .attr({
Simon Hunt142d0032014-11-04 20:13:09 -08001165 id: idBg,
1166 width: w,
1167 height: h,
Simon Hunt195cb382014-11-03 17:50:51 -08001168 'xlink:href': config.backgroundUrl
1169 })
Simon Hunt142d0032014-11-04 20:13:09 -08001170 .style({
1171 visibility: showBg
Simon Hunt195cb382014-11-03 17:50:51 -08001172 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001173
1174 // group for the topology
1175 topoG = svg.append('g')
1176 .attr('transform', fcfg.translate());
1177
1178 // subgroups for links and nodes
1179 linkG = topoG.append('g').attr('id', 'links');
1180 nodeG = topoG.append('g').attr('id', 'nodes');
1181
1182 // selection of nodes and links
1183 link = linkG.selectAll('.link');
1184 node = nodeG.selectAll('.node');
1185
Simon Hunt7cd48f32014-11-09 23:42:50 -08001186 function chrg(d) {
1187 return fcfg.charge[d.class] || -12000;
1188 }
Simon Hunt99c13842014-11-06 18:23:12 -08001189 function ldist(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08001190 return fcfg.linkDistance[d.type] || 50;
Simon Hunt99c13842014-11-06 18:23:12 -08001191 }
1192 function lstrg(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08001193 // 0.0 - 1.0
1194 return fcfg.linkStrength[d.type] || 1.0;
Simon Hunt99c13842014-11-06 18:23:12 -08001195 }
1196
Simon Hunt1a9eff92014-11-07 11:06:34 -08001197 function selectCb(d, self) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001198 selectObject(d, self);
Simon Hunt1a9eff92014-11-07 11:06:34 -08001199 }
1200
1201 function atDragEnd(d, self) {
Simon Hunt56d51852014-11-09 13:03:35 -08001202 // once we've finished moving, pin the node in position
1203 d.fixed = true;
1204 d3.select(self).classed('fixed', true);
1205 if (config.useLiveData) {
Simon Hunt902c9922014-11-11 11:59:31 -08001206 sendUpdateMeta(d);
Simon Hunt1a9eff92014-11-07 11:06:34 -08001207 }
1208 }
1209
Simon Hunt902c9922014-11-11 11:59:31 -08001210 function sendUpdateMeta(d) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001211 sendMessage('updateMeta', {
1212 id: d.id,
1213 'class': d.class,
Simon Hunt902c9922014-11-11 11:59:31 -08001214 'memento': {
1215 x: Math.floor(d.x),
1216 y: Math.floor(d.y)
1217 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001218 });
1219 }
1220
Simon Huntc7ee0662014-11-05 16:44:37 -08001221 // set up the force layout
1222 network.force = d3.layout.force()
1223 .size(forceDim)
1224 .nodes(network.nodes)
1225 .links(network.links)
Simon Hunt7cd48f32014-11-09 23:42:50 -08001226 .gravity(0.4)
1227 .friction(0.7)
1228 .charge(chrg)
Simon Hunt99c13842014-11-06 18:23:12 -08001229 .linkDistance(ldist)
1230 .linkStrength(lstrg)
Simon Huntc7ee0662014-11-05 16:44:37 -08001231 .on('tick', tick);
Simon Hunt195cb382014-11-03 17:50:51 -08001232
Simon Hunt1a9eff92014-11-07 11:06:34 -08001233 network.drag = d3u.createDragBehavior(network.force, selectCb, atDragEnd);
1234 }
Simon Hunt195cb382014-11-03 17:50:51 -08001235
Simon Hunt56d51852014-11-09 13:03:35 -08001236 function load(view, ctx, flags) {
Simon Huntf67722a2014-11-10 09:32:06 -08001237 // resize, in case the window was resized while we were not loaded
1238 resize(view, ctx, flags);
1239
Simon Hunt99c13842014-11-06 18:23:12 -08001240 // cache the view token, so network topo functions can access it
1241 network.view = view;
Simon Hunt56d51852014-11-09 13:03:35 -08001242 config.useLiveData = !flags.local;
1243
1244 if (!config.useLiveData) {
1245 prepareScenario(view, ctx, flags.debug);
1246 }
Simon Hunt99c13842014-11-06 18:23:12 -08001247
1248 // set our radio buttons and key bindings
Simon Hunt934c3ce2014-11-05 11:45:07 -08001249 view.setRadio(btnSet);
1250 view.setKeys(keyDispatch);
Simon Hunt195cb382014-11-03 17:50:51 -08001251
Simon Hunt50128c02014-11-08 13:36:15 -08001252 if (config.useLiveData) {
1253 webSock.connect();
1254 }
Simon Hunt195cb382014-11-03 17:50:51 -08001255 }
1256
Simon Huntf67722a2014-11-10 09:32:06 -08001257 function resize(view, ctx, flags) {
Simon Hunt934c3ce2014-11-05 11:45:07 -08001258 setSize(svg, view);
1259 setSize(bgImg, view);
Simon Hunt99c13842014-11-06 18:23:12 -08001260
1261 // TODO: hook to recompute layout, perhaps? work with zoom/pan code
1262 // adjust force layout size
Simon Hunt142d0032014-11-04 20:13:09 -08001263 }
1264
1265
1266 // ==============================
1267 // View registration
Simon Hunt195cb382014-11-03 17:50:51 -08001268
Simon Hunt25248912014-11-04 11:25:48 -08001269 onos.ui.addView('topo', {
Simon Hunt142d0032014-11-04 20:13:09 -08001270 preload: preload,
1271 load: load,
1272 resize: resize
Simon Hunt195cb382014-11-03 17:50:51 -08001273 });
1274
Simon Hunt195cb382014-11-03 17:50:51 -08001275}(ONOS));