blob: 86aca727173479b0f1969c90035f6818d7bbf558 [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 Hunt56d51852014-11-09 13:03:35 -0800355 removeHost: stillToImplement,
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) {
392 network.links.push(lnk);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800393 network.lookup[host.ingress] = lnk;
394 network.lookup[host.egress] = lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800395 updateLinks();
396 }
397 network.force.start();
398 }
399
Simon Huntbb282f52014-11-10 11:08:19 -0800400 function updateDevice(data) {
Simon Huntfc274c92014-11-11 11:05:46 -0800401 fnTrace('updateDevice', data.payload.id);
Simon Huntbb282f52014-11-10 11:08:19 -0800402 var device = data.payload,
403 id = device.id,
404 nodeData = network.lookup[id];
405 if (nodeData) {
406 $.extend(nodeData, device);
407 updateDeviceState(nodeData);
408 } else {
409 logicError('updateDevice lookup fail. ID = "' + id + '"');
410 }
411 }
412
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800413 function updateLink(data) {
Simon Huntfc274c92014-11-11 11:05:46 -0800414 fnTrace('updateLink', data.payload.id);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800415 var link = data.payload,
416 id = link.id,
417 linkData = network.lookup[id];
418 if (linkData) {
419 $.extend(linkData, link);
420 updateLinkState(linkData);
421 } else {
422 logicError('updateLink lookup fail. ID = "' + id + '"');
423 }
424 }
425
Simon Hunt7cd48f32014-11-09 23:42:50 -0800426 function updateHost(data) {
Simon Huntfc274c92014-11-11 11:05:46 -0800427 fnTrace('updateHost', data.payload.id);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800428 var host = data.payload,
Simon Huntbb282f52014-11-10 11:08:19 -0800429 id = host.id,
430 hostData = network.lookup[id];
431 if (hostData) {
432 $.extend(hostData, host);
433 updateHostState(hostData);
434 } else {
435 logicError('updateHost lookup fail. ID = "' + id + '"');
436 }
Simon Hunt7cd48f32014-11-09 23:42:50 -0800437 }
438
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800439 function removeLink(data) {
Simon Huntfc274c92014-11-11 11:05:46 -0800440 fnTrace('removeLink', data.payload.id);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800441 var link = data.payload,
442 id = link.id,
443 linkData = network.lookup[id];
444 if (linkData) {
445 removeLinkElement(linkData);
446 } else {
447 logicError('removeLink lookup fail. ID = "' + id + '"');
448 }
449 }
450
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800451 function showPath(data) {
Simon Huntfc274c92014-11-11 11:05:46 -0800452 fnTrace('showPath', data.payload.id);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800453 var links = data.payload.links,
454 s = [ data.event + "\n" + links.length ];
455 links.forEach(function (d, i) {
456 s.push(d);
457 });
458 network.view.alert(s.join('\n'));
459
460 links.forEach(function (d, i) {
461 var link = network.lookup[d];
462 if (link) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800463 link.el.classed('showPath', true);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800464 }
465 });
466
467 // TODO: add selection-highlite lines to links
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800468 }
469
Simon Hunt56d51852014-11-09 13:03:35 -0800470 // ...............................
471
472 function stillToImplement(data) {
473 var p = data.payload;
474 note(data.event, p.id);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800475 network.view.alert('Not yet implemented: "' + data.event + '"');
Simon Hunt56d51852014-11-09 13:03:35 -0800476 }
Simon Hunt99c13842014-11-06 18:23:12 -0800477
478 function unknownEvent(data) {
Simon Hunt50128c02014-11-08 13:36:15 -0800479 network.view.alert('Unknown event type: "' + data.event + '"');
Simon Hunt99c13842014-11-06 18:23:12 -0800480 }
481
482 function handleServerEvent(data) {
483 var fn = eventDispatch[data.event] || unknownEvent;
484 fn(data);
485 }
486
487 // ==============================
488 // force layout modification functions
489
490 function translate(x, y) {
491 return 'translate(' + x + ',' + y + ')';
492 }
493
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800494 function missMsg(what, id) {
495 return '\n[' + what + '] "' + id + '" missing ';
496 }
497
498 function linkEndPoints(srcId, dstId) {
499 var srcNode = network.lookup[srcId],
500 dstNode = network.lookup[dstId],
501 sMiss = !srcNode ? missMsg('src', srcId) : '',
502 dMiss = !dstNode ? missMsg('dst', dstId) : '';
503
504 if (sMiss || dMiss) {
505 logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
506 return null;
507 }
508 return {
509 source: srcNode,
510 target: dstNode,
511 x1: srcNode.x,
512 y1: srcNode.y,
513 x2: dstNode.x,
514 y2: dstNode.y
515 };
516 }
517
Simon Hunt56d51852014-11-09 13:03:35 -0800518 function createHostLink(host) {
519 var src = host.id,
520 dst = host.cp.device,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800521 id = host.ingress,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800522 lnk = linkEndPoints(src, dst);
Simon Hunt56d51852014-11-09 13:03:35 -0800523
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800524 if (!lnk) {
Simon Hunt56d51852014-11-09 13:03:35 -0800525 return null;
526 }
527
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800528 // Synthesize link ...
529 $.extend(lnk, {
Thomas Vachuska4830d392014-11-09 17:09:56 -0800530 id: id,
Simon Hunt56d51852014-11-09 13:03:35 -0800531 class: 'link',
Simon Hunt7cd48f32014-11-09 23:42:50 -0800532 type: 'hostLink',
Simon Hunt56d51852014-11-09 13:03:35 -0800533 svgClass: 'link hostLink',
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800534 linkWidth: 1
Simon Hunt7cd48f32014-11-09 23:42:50 -0800535 });
Simon Hunt99c13842014-11-06 18:23:12 -0800536 return lnk;
537 }
538
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800539 function createLink(link) {
540 var lnk = linkEndPoints(link.src, link.dst),
541 type = link.type;
542
543 if (!lnk) {
544 return null;
545 }
546
547 // merge in remaining data
548 $.extend(lnk, link, {
549 class: 'link',
550 svgClass: type ? 'link ' + type : 'link'
551 });
552 return lnk;
Simon Hunt1a9eff92014-11-07 11:06:34 -0800553 }
554
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800555 var widthRatio = 1.4,
556 linkScale = d3.scale.linear()
557 .domain([1, 12])
558 .range([widthRatio, 12 * widthRatio])
559 .clamp(true);
560
561 function updateLinkWidth (d) {
562 // TODO: watch out for .showPath/.showTraffic classes
563 d.el.transition()
564 .duration(1000)
565 .attr('stroke-width', linkScale(d.linkWidth));
566 }
567
568
Simon Hunt99c13842014-11-06 18:23:12 -0800569 function updateLinks() {
570 link = linkG.selectAll('.link')
571 .data(network.links, function (d) { return d.id; });
572
573 // operate on existing links, if necessary
574 // link .foo() .bar() ...
575
576 // operate on entering links:
577 var entering = link.enter()
578 .append('line')
579 .attr({
Simon Hunt99c13842014-11-06 18:23:12 -0800580 class: function (d) { return d.svgClass; },
581 x1: function (d) { return d.x1; },
582 y1: function (d) { return d.y1; },
583 x2: function (d) { return d.x2; },
584 y2: function (d) { return d.y2; },
Simon Hunt1a9eff92014-11-07 11:06:34 -0800585 stroke: config.topo.linkInColor,
586 'stroke-width': config.topo.linkInWidth
Simon Hunt99c13842014-11-06 18:23:12 -0800587 })
588 .transition().duration(1000)
589 .attr({
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800590 'stroke-width': function (d) { return linkScale(d.linkWidth); },
Simon Hunt99c13842014-11-06 18:23:12 -0800591 stroke: '#666' // TODO: remove explicit stroke, rather...
592 });
593
594 // augment links
Simon Hunt7cd48f32014-11-09 23:42:50 -0800595 entering.each(function (d) {
596 var link = d3.select(this);
597 // provide ref to element selection from backing data....
598 d.el = link;
Simon Hunt99c13842014-11-06 18:23:12 -0800599
Simon Hunt7cd48f32014-11-09 23:42:50 -0800600 // TODO: add src/dst port labels etc.
601 });
Thomas Vachuska4830d392014-11-09 17:09:56 -0800602
603 // operate on both existing and new links, if necessary
604 //link .foo() .bar() ...
605
606 // operate on exiting links:
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800607 // TODO: better transition (longer as a dashed, grey line)
Thomas Vachuska4830d392014-11-09 17:09:56 -0800608 link.exit()
Thomas Vachuska4830d392014-11-09 17:09:56 -0800609 .attr({
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800610 'stroke-dasharray': '3, 3'
Thomas Vachuska4830d392014-11-09 17:09:56 -0800611 })
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800612 .style('opacity', 0.4)
613 .transition()
614 .duration(2000)
615 .attr({
616 'stroke-dasharray': '3, 12'
617 })
618 .transition()
619 .duration(1000)
620 .style('opacity', 0.0)
Thomas Vachuska4830d392014-11-09 17:09:56 -0800621 .remove();
Simon Hunt99c13842014-11-06 18:23:12 -0800622 }
623
624 function createDeviceNode(device) {
625 // start with the object as is
626 var node = device,
Simon Huntbb282f52014-11-10 11:08:19 -0800627 type = device.type,
628 svgCls = type ? 'node device ' + type : 'node device';
Simon Hunt99c13842014-11-06 18:23:12 -0800629
630 // Augment as needed...
631 node.class = 'device';
Simon Huntbb282f52014-11-10 11:08:19 -0800632 node.svgClass = device.online ? svgCls + ' online' : svgCls;
Simon Hunt99c13842014-11-06 18:23:12 -0800633 positionNode(node);
634
635 // cache label array length
636 network.deviceLabelCount = device.labels.length;
Simon Hunt99c13842014-11-06 18:23:12 -0800637 return node;
638 }
639
Simon Hunt56d51852014-11-09 13:03:35 -0800640 function createHostNode(host) {
641 // start with the object as is
642 var node = host;
643
644 // Augment as needed...
645 node.class = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -0800646 if (!node.type) {
647 // TODO: perhaps type would be: {phone, tablet, laptop, endstation} ?
648 node.type = 'endstation';
649 }
Simon Hunt56d51852014-11-09 13:03:35 -0800650 node.svgClass = 'node host';
651 // TODO: consider placing near its switch, if [x,y] not defined
652 positionNode(node);
653
654 // cache label array length
655 network.hostLabelCount = host.labels.length;
Simon Hunt56d51852014-11-09 13:03:35 -0800656 return node;
657 }
658
Simon Hunt99c13842014-11-06 18:23:12 -0800659 function positionNode(node) {
660 var meta = node.metaUi,
661 x = 0,
662 y = 0;
663
664 if (meta) {
665 x = meta.x;
666 y = meta.y;
667 }
668 if (x && y) {
669 node.fixed = true;
670 }
671 node.x = x || network.view.width() / 2;
672 node.y = y || network.view.height() / 2;
673 }
674
Simon Hunt99c13842014-11-06 18:23:12 -0800675 function iconUrl(d) {
676 return 'img/' + d.type + '.png';
677 }
678
679 // returns the newly computed bounding box of the rectangle
680 function adjustRectToFitText(n) {
681 var text = n.select('text'),
682 box = text.node().getBBox(),
683 lab = config.labels;
684
685 text.attr('text-anchor', 'middle')
686 .attr('y', '-0.8em')
687 .attr('x', lab.imgPad/2);
688
689 // translate the bbox so that it is centered on [x,y]
690 box.x = -box.width / 2;
691 box.y = -box.height / 2;
692
693 // add padding
694 box.x -= (lab.padLR + lab.imgPad/2);
695 box.width += lab.padLR * 2 + lab.imgPad;
696 box.y -= lab.padTB;
697 box.height += lab.padTB * 2;
698
699 return box;
700 }
701
Simon Hunt1a9eff92014-11-07 11:06:34 -0800702 function mkSvgClass(d) {
703 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
704 }
705
Simon Hunt7cd48f32014-11-09 23:42:50 -0800706 function hostLabel(d) {
707 var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0;
708 return d.labels[idx];
709 }
710 function deviceLabel(d) {
711 var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0;
712 return d.labels[idx];
713 }
714 function niceLabel(label) {
715 return (label && label.trim()) ? label : '.';
716 }
717
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800718 function updateDeviceLabel(d) {
719 var label = niceLabel(deviceLabel(d)),
720 node = d.el,
721 box;
722
723 node.select('text')
724 .text(label)
725 .style('opacity', 0)
726 .transition()
727 .style('opacity', 1);
728
729 box = adjustRectToFitText(node);
730
731 node.select('rect')
732 .transition()
733 .attr(box);
734
735 node.select('image')
736 .transition()
737 .attr('x', box.x + config.icons.xoff)
738 .attr('y', box.y + config.icons.yoff);
739 }
740
741 function updateHostLabel(d) {
742 var label = hostLabel(d),
743 host = d.el;
744
745 host.select('text').text(label);
746 }
747
Simon Huntbb282f52014-11-10 11:08:19 -0800748 function updateDeviceState(nodeData) {
749 nodeData.el.classed('online', nodeData.online);
750 updateDeviceLabel(nodeData);
751 // TODO: review what else might need to be updated
752 }
753
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800754 function updateLinkState(linkData) {
755 updateLinkWidth(linkData);
756 // TODO: review what else might need to be updated
757 // update label, if showing
758 }
759
Simon Huntbb282f52014-11-10 11:08:19 -0800760 function updateHostState(hostData) {
761 updateHostLabel(hostData);
762 // TODO: review what else might need to be updated
763 }
764
765
Simon Hunt99c13842014-11-06 18:23:12 -0800766 function updateNodes() {
767 node = nodeG.selectAll('.node')
768 .data(network.nodes, function (d) { return d.id; });
769
770 // operate on existing nodes, if necessary
Simon Hunt7cd48f32014-11-09 23:42:50 -0800771 // update host labels
Simon Hunt99c13842014-11-06 18:23:12 -0800772 //node .foo() .bar() ...
773
774 // operate on entering nodes:
775 var entering = node.enter()
776 .append('g')
777 .attr({
778 id: function (d) { return safeId(d.id); },
Simon Hunt1a9eff92014-11-07 11:06:34 -0800779 class: mkSvgClass,
Simon Hunt99c13842014-11-06 18:23:12 -0800780 transform: function (d) { return translate(d.x, d.y); },
781 opacity: 0
782 })
Simon Hunt1a9eff92014-11-07 11:06:34 -0800783 .call(network.drag)
Simon Hunt99c13842014-11-06 18:23:12 -0800784 //.on('mouseover', function (d) {})
785 //.on('mouseover', function (d) {})
786 .transition()
787 .attr('opacity', 1);
788
789 // augment device nodes...
790 entering.filter('.device').each(function (d) {
791 var node = d3.select(this),
792 icon = iconUrl(d),
Simon Hunt7cd48f32014-11-09 23:42:50 -0800793 label = niceLabel(deviceLabel(d)),
Simon Hunt99c13842014-11-06 18:23:12 -0800794 box;
795
Simon Hunt7cd48f32014-11-09 23:42:50 -0800796 // provide ref to element from backing data....
797 d.el = node;
798
Simon Hunt99c13842014-11-06 18:23:12 -0800799 node.append('rect')
800 .attr({
801 'rx': 5,
802 'ry': 5
803 });
804
805 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -0800806 .text(label)
Simon Hunt99c13842014-11-06 18:23:12 -0800807 .attr('dy', '1.1em');
808
809 box = adjustRectToFitText(node);
810
811 node.select('rect')
812 .attr(box);
813
814 if (icon) {
815 var cfg = config.icons;
816 node.append('svg:image')
817 .attr({
818 x: box.x + config.icons.xoff,
819 y: box.y + config.icons.yoff,
820 width: cfg.w,
821 height: cfg.h,
822 'xlink:href': icon
823 });
824 }
825
826 // debug function to show the modelled x,y coordinates of nodes...
827 if (debug('showNodeXY')) {
828 node.select('rect').attr('fill-opacity', 0.5);
829 node.append('circle')
830 .attr({
831 class: 'debug',
832 cx: 0,
833 cy: 0,
834 r: '3px'
835 });
Simon Huntc7ee0662014-11-05 16:44:37 -0800836 }
837 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800838
Simon Hunt56d51852014-11-09 13:03:35 -0800839 // augment host nodes...
840 entering.filter('.host').each(function (d) {
841 var node = d3.select(this),
Simon Hunt56d51852014-11-09 13:03:35 -0800842 box;
843
Simon Hunt7cd48f32014-11-09 23:42:50 -0800844 // provide ref to element from backing data....
845 d.el = node;
846
Simon Hunt56d51852014-11-09 13:03:35 -0800847 node.append('circle')
848 .attr('r', 8); // TODO: define host circle radius
849
850 // TODO: are we attaching labels to hosts?
851 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -0800852 .text(hostLabel)
853 .attr('dy', '1.3em')
854 .attr('text-anchor', 'middle');
Simon Hunt56d51852014-11-09 13:03:35 -0800855
856 // debug function to show the modelled x,y coordinates of nodes...
857 if (debug('showNodeXY')) {
858 node.select('circle').attr('fill-opacity', 0.5);
859 node.append('circle')
860 .attr({
861 class: 'debug',
862 cx: 0,
863 cy: 0,
864 r: '3px'
865 });
866 }
867 });
Simon Huntc7ee0662014-11-05 16:44:37 -0800868
Simon Hunt99c13842014-11-06 18:23:12 -0800869 // operate on both existing and new nodes, if necessary
870 //node .foo() .bar() ...
Simon Huntc7ee0662014-11-05 16:44:37 -0800871
Simon Hunt99c13842014-11-06 18:23:12 -0800872 // operate on exiting nodes:
873 // TODO: figure out how to remove the node 'g' AND its children
874 node.exit()
875 .transition()
876 .duration(750)
877 .attr({
878 opacity: 0,
879 cx: 0,
880 cy: 0,
881 r: 0
882 })
883 .remove();
Simon Huntc7ee0662014-11-05 16:44:37 -0800884 }
885
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800886 function find(id, array) {
887 for (var idx = 0, n = array.length; idx < n; idx++) {
888 if (array[idx].id === id) {
889 return idx;
890 }
891 }
892 return -1;
893 }
894
895 function removeLinkElement(linkData) {
896 // remove from lookup cache
897 delete network.lookup[linkData.id];
898 // remove from links array
899 var idx = find(linkData.id, network.links);
900
Simon Huntfc274c92014-11-11 11:05:46 -0800901 network.links.splice(idx, 1);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800902 // remove from SVG
903 updateLinks();
904 }
Simon Huntc7ee0662014-11-05 16:44:37 -0800905
906 function tick() {
907 node.attr({
Simon Hunt99c13842014-11-06 18:23:12 -0800908 transform: function (d) { return translate(d.x, d.y); }
Simon Huntc7ee0662014-11-05 16:44:37 -0800909 });
910
911 link.attr({
912 x1: function (d) { return d.source.x; },
913 y1: function (d) { return d.source.y; },
914 x2: function (d) { return d.target.x; },
915 y2: function (d) { return d.target.y; }
916 });
917 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800918
919 // ==============================
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800920 // Web-Socket for live data
921
922 function webSockUrl() {
923 return document.location.toString()
924 .replace(/\#.*/, '')
925 .replace('http://', 'ws://')
926 .replace('https://', 'wss://')
927 .replace('index2.html', config.webSockUrl);
928 }
929
930 webSock = {
931 ws : null,
932
933 connect : function() {
934 webSock.ws = new WebSocket(webSockUrl());
935
936 webSock.ws.onopen = function() {
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800937 };
938
939 webSock.ws.onmessage = function(m) {
940 if (m.data) {
Simon Huntbb282f52014-11-10 11:08:19 -0800941 wsTraceRx(m.data);
Thomas Vachuskad472c6e2014-11-07 19:11:05 -0800942 handleServerEvent(JSON.parse(m.data));
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800943 }
944 };
945
946 webSock.ws.onclose = function(m) {
947 webSock.ws = null;
948 };
949 },
950
951 send : function(text) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800952 if (text != null) {
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800953 webSock._send(text);
954 }
955 },
956
957 _send : function(message) {
958 if (webSock.ws) {
959 webSock.ws.send(message);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800960 } else {
Simon Hunt56d51852014-11-09 13:03:35 -0800961 network.view.alert('no web socket open\n\n' + message);
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800962 }
963 }
964
965 };
966
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800967 var sid = 0;
968
969 function sendMessage(evType, payload) {
970 var toSend = {
Simon Huntbb282f52014-11-10 11:08:19 -0800971 event: evType,
972 sid: ++sid,
973 payload: payload
974 },
975 asText = JSON.stringify(toSend);
976 wsTraceTx(asText);
977 webSock.send(asText);
978 }
979
980 function wsTraceTx(msg) {
981 wsTrace('tx', msg);
982 }
983 function wsTraceRx(msg) {
984 wsTrace('rx', msg);
985 }
986 function wsTrace(rxtx, msg) {
987
988 console.log('[' + rxtx + '] ' + msg);
989 // TODO: integrate with trace view
990 //if (trace) {
991 // trace.output(rxtx, msg);
992 //}
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800993 }
994
995
996 // ==============================
997 // Selection stuff
998
999 function selectObject(obj, el) {
1000 var n,
1001 meta = d3.event.sourceEvent.metaKey;
1002
1003 if (el) {
1004 n = d3.select(el);
1005 } else {
1006 node.each(function(d) {
1007 if (d == obj) {
1008 n = d3.select(el = this);
1009 }
1010 });
1011 }
1012 if (!n) return;
1013
1014 if (meta && n.classed('selected')) {
1015 deselectObject(obj.id);
1016 //flyinPane(null);
1017 return;
1018 }
1019
1020 if (!meta) {
1021 deselectAll();
1022 }
1023
Simon Hunt5f36d342014-11-08 21:33:14 -08001024 selections[obj.id] = { obj: obj, el : el};
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001025 selectOrder.push(obj.id);
1026
1027 n.classed('selected', true);
1028 //flyinPane(obj);
1029 }
1030
1031 function deselectObject(id) {
1032 var obj = selections[id];
1033 if (obj) {
1034 d3.select(obj.el).classed('selected', false);
1035 selections[id] = null;
1036 // TODO: use splice to remove element
1037 }
1038 //flyinPane(null);
1039 }
1040
1041 function deselectAll() {
1042 // deselect all nodes in the network...
1043 node.classed('selected', false);
1044 selections = {};
1045 selectOrder = [];
1046 //flyinPane(null);
1047 }
1048
Simon Hunt56d51852014-11-09 13:03:35 -08001049 // TODO: this click handler does not get unloaded when the view does
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001050 $('#view').on('click', function(e) {
1051 if (!$(e.target).closest('.node').length) {
1052 if (!e.metaKey) {
1053 deselectAll();
1054 }
1055 }
1056 });
1057
Simon Hunt56d51852014-11-09 13:03:35 -08001058
1059 function prepareScenario(view, ctx, dbg) {
1060 var sc = scenario,
1061 urlSc = sc.evDir + ctx + sc.evScenario;
1062
1063 if (!ctx) {
1064 view.alert("No scenario specified (null ctx)");
1065 return;
1066 }
1067
1068 sc.view = view;
1069 sc.ctx = ctx;
1070 sc.debug = dbg;
1071 sc.evNumber = 0;
1072
1073 d3.json(urlSc, function(err, data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001074 var p = data && data.params || {},
1075 desc = data && data.description || null,
Simon Huntfc274c92014-11-11 11:05:46 -08001076 intro = data && data.title;
Simon Huntbb282f52014-11-10 11:08:19 -08001077
Simon Hunt56d51852014-11-09 13:03:35 -08001078 if (err) {
1079 view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err);
1080 } else {
1081 sc.params = p;
Simon Huntbb282f52014-11-10 11:08:19 -08001082 if (desc) {
1083 intro += '\n\n ' + desc.join('\n ');
1084 }
1085 view.alert(intro);
Simon Hunt56d51852014-11-09 13:03:35 -08001086 }
1087 });
1088
1089 }
1090
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001091 // ==============================
Simon Hunt142d0032014-11-04 20:13:09 -08001092 // View life-cycle callbacks
Simon Hunt195cb382014-11-03 17:50:51 -08001093
Simon Huntf67722a2014-11-10 09:32:06 -08001094 function preload(view, ctx, flags) {
Simon Hunt142d0032014-11-04 20:13:09 -08001095 var w = view.width(),
1096 h = view.height(),
1097 idBg = view.uid('bg'),
Simon Huntc7ee0662014-11-05 16:44:37 -08001098 showBg = config.options.showBackground ? 'visible' : 'hidden',
1099 fcfg = config.force,
1100 fpad = fcfg.pad,
1101 forceDim = [w - 2*fpad, h - 2*fpad];
Simon Hunt195cb382014-11-03 17:50:51 -08001102
Simon Huntbb282f52014-11-10 11:08:19 -08001103 // TODO: set trace api
1104 //trace = onos.exported.webSockTrace;
1105
Simon Hunt142d0032014-11-04 20:13:09 -08001106 // NOTE: view.$div is a D3 selection of the view's div
1107 svg = view.$div.append('svg');
Simon Hunt934c3ce2014-11-05 11:45:07 -08001108 setSize(svg, view);
1109
Simon Hunt1a9eff92014-11-07 11:06:34 -08001110 // add blue glow filter to svg layer
1111 d3u.appendGlow(svg);
1112
Simon Hunt142d0032014-11-04 20:13:09 -08001113 // load the background image
1114 bgImg = svg.append('svg:image')
Simon Hunt195cb382014-11-03 17:50:51 -08001115 .attr({
Simon Hunt142d0032014-11-04 20:13:09 -08001116 id: idBg,
1117 width: w,
1118 height: h,
Simon Hunt195cb382014-11-03 17:50:51 -08001119 'xlink:href': config.backgroundUrl
1120 })
Simon Hunt142d0032014-11-04 20:13:09 -08001121 .style({
1122 visibility: showBg
Simon Hunt195cb382014-11-03 17:50:51 -08001123 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001124
1125 // group for the topology
1126 topoG = svg.append('g')
1127 .attr('transform', fcfg.translate());
1128
1129 // subgroups for links and nodes
1130 linkG = topoG.append('g').attr('id', 'links');
1131 nodeG = topoG.append('g').attr('id', 'nodes');
1132
1133 // selection of nodes and links
1134 link = linkG.selectAll('.link');
1135 node = nodeG.selectAll('.node');
1136
Simon Hunt7cd48f32014-11-09 23:42:50 -08001137 function chrg(d) {
1138 return fcfg.charge[d.class] || -12000;
1139 }
Simon Hunt99c13842014-11-06 18:23:12 -08001140 function ldist(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08001141 return fcfg.linkDistance[d.type] || 50;
Simon Hunt99c13842014-11-06 18:23:12 -08001142 }
1143 function lstrg(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08001144 // 0.0 - 1.0
1145 return fcfg.linkStrength[d.type] || 1.0;
Simon Hunt99c13842014-11-06 18:23:12 -08001146 }
1147
Simon Hunt1a9eff92014-11-07 11:06:34 -08001148 function selectCb(d, self) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001149 selectObject(d, self);
Simon Hunt1a9eff92014-11-07 11:06:34 -08001150 }
1151
1152 function atDragEnd(d, self) {
Simon Hunt56d51852014-11-09 13:03:35 -08001153 // once we've finished moving, pin the node in position
1154 d.fixed = true;
1155 d3.select(self).classed('fixed', true);
1156 if (config.useLiveData) {
Simon Hunt902c9922014-11-11 11:59:31 -08001157 sendUpdateMeta(d);
Simon Hunt1a9eff92014-11-07 11:06:34 -08001158 }
1159 }
1160
Simon Hunt902c9922014-11-11 11:59:31 -08001161 function sendUpdateMeta(d) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001162 sendMessage('updateMeta', {
1163 id: d.id,
1164 'class': d.class,
Simon Hunt902c9922014-11-11 11:59:31 -08001165 'memento': {
1166 x: Math.floor(d.x),
1167 y: Math.floor(d.y)
1168 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001169 });
1170 }
1171
Simon Huntc7ee0662014-11-05 16:44:37 -08001172 // set up the force layout
1173 network.force = d3.layout.force()
1174 .size(forceDim)
1175 .nodes(network.nodes)
1176 .links(network.links)
Simon Hunt7cd48f32014-11-09 23:42:50 -08001177 .gravity(0.4)
1178 .friction(0.7)
1179 .charge(chrg)
Simon Hunt99c13842014-11-06 18:23:12 -08001180 .linkDistance(ldist)
1181 .linkStrength(lstrg)
Simon Huntc7ee0662014-11-05 16:44:37 -08001182 .on('tick', tick);
Simon Hunt195cb382014-11-03 17:50:51 -08001183
Simon Hunt1a9eff92014-11-07 11:06:34 -08001184 network.drag = d3u.createDragBehavior(network.force, selectCb, atDragEnd);
1185 }
Simon Hunt195cb382014-11-03 17:50:51 -08001186
Simon Hunt56d51852014-11-09 13:03:35 -08001187 function load(view, ctx, flags) {
Simon Huntf67722a2014-11-10 09:32:06 -08001188 // resize, in case the window was resized while we were not loaded
1189 resize(view, ctx, flags);
1190
Simon Hunt99c13842014-11-06 18:23:12 -08001191 // cache the view token, so network topo functions can access it
1192 network.view = view;
Simon Hunt56d51852014-11-09 13:03:35 -08001193 config.useLiveData = !flags.local;
1194
1195 if (!config.useLiveData) {
1196 prepareScenario(view, ctx, flags.debug);
1197 }
Simon Hunt99c13842014-11-06 18:23:12 -08001198
1199 // set our radio buttons and key bindings
Simon Hunt934c3ce2014-11-05 11:45:07 -08001200 view.setRadio(btnSet);
1201 view.setKeys(keyDispatch);
Simon Hunt195cb382014-11-03 17:50:51 -08001202
Simon Hunt50128c02014-11-08 13:36:15 -08001203 if (config.useLiveData) {
1204 webSock.connect();
1205 }
Simon Hunt195cb382014-11-03 17:50:51 -08001206 }
1207
Simon Huntf67722a2014-11-10 09:32:06 -08001208 function resize(view, ctx, flags) {
Simon Hunt934c3ce2014-11-05 11:45:07 -08001209 setSize(svg, view);
1210 setSize(bgImg, view);
Simon Hunt99c13842014-11-06 18:23:12 -08001211
1212 // TODO: hook to recompute layout, perhaps? work with zoom/pan code
1213 // adjust force layout size
Simon Hunt142d0032014-11-04 20:13:09 -08001214 }
1215
1216
1217 // ==============================
1218 // View registration
Simon Hunt195cb382014-11-03 17:50:51 -08001219
Simon Hunt25248912014-11-04 11:25:48 -08001220 onos.ui.addView('topo', {
Simon Hunt142d0032014-11-04 20:13:09 -08001221 preload: preload,
1222 load: load,
1223 resize: resize
Simon Hunt195cb382014-11-03 17:50:51 -08001224 });
1225
Simon Hunt195cb382014-11-03 17:50:51 -08001226}(ONOS));