blob: f34f247b711f35ee93fabe4fadd5c8f159509f69 [file] [log] [blame]
Simon Hunt195cb382014-11-03 17:50:51 -08001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
Simon Hunt142d0032014-11-04 20:13:09 -080018 ONOS network topology viewer - version 1.1
Simon Hunt195cb382014-11-03 17:50:51 -080019
20 @author Simon Hunt
21 */
22
23(function (onos) {
24 'use strict';
25
Simon Hunt1a9eff92014-11-07 11:06:34 -080026 // shorter names for library APIs
Simon Huntbb282f52014-11-10 11:08:19 -080027 var d3u = onos.lib.d3util,
28 trace;
Simon Hunt1a9eff92014-11-07 11:06:34 -080029
Simon Hunt195cb382014-11-03 17:50:51 -080030 // configuration data
31 var config = {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -080032 useLiveData: true,
Simon Huntfc274c92014-11-11 11:05:46 -080033 fnTrace: true,
Simon Hunt195cb382014-11-03 17:50:51 -080034 debugOn: false,
35 debug: {
Simon Hunt99c13842014-11-06 18:23:12 -080036 showNodeXY: true,
37 showKeyHandler: false
Simon Hunt195cb382014-11-03 17:50:51 -080038 },
39 options: {
40 layering: true,
41 collisionPrevention: true,
Simon Hunt142d0032014-11-04 20:13:09 -080042 showBackground: true
Simon Hunt195cb382014-11-03 17:50:51 -080043 },
44 backgroundUrl: 'img/us-map.png',
Thomas Vachuska7d638d32014-11-07 10:24:43 -080045 webSockUrl: 'ws/topology',
Simon Hunt195cb382014-11-03 17:50:51 -080046 data: {
47 live: {
48 jsonUrl: 'rs/topology/graph',
49 detailPrefix: 'rs/topology/graph/',
50 detailSuffix: ''
51 },
52 fake: {
53 jsonUrl: 'json/network2.json',
54 detailPrefix: 'json/',
55 detailSuffix: '.json'
56 }
57 },
Simon Hunt99c13842014-11-06 18:23:12 -080058 labels: {
59 imgPad: 16,
60 padLR: 4,
61 padTB: 3,
62 marginLR: 3,
63 marginTB: 2,
64 port: {
65 gap: 3,
66 width: 18,
67 height: 14
68 }
69 },
Simon Hunt1a9eff92014-11-07 11:06:34 -080070 topo: {
71 linkInColor: '#66f',
72 linkInWidth: 14
73 },
Simon Hunt99c13842014-11-06 18:23:12 -080074 icons: {
75 w: 28,
76 h: 28,
77 xoff: -12,
78 yoff: -8
79 },
Simon Hunt195cb382014-11-03 17:50:51 -080080 iconUrl: {
81 device: 'img/device.png',
82 host: 'img/host.png',
83 pkt: 'img/pkt.png',
84 opt: 'img/opt.png'
85 },
Simon Hunt195cb382014-11-03 17:50:51 -080086 force: {
Simon Hunt7cd48f32014-11-09 23:42:50 -080087 note_for_links: 'link.type is used to differentiate',
Simon Huntc7ee0662014-11-05 16:44:37 -080088 linkDistance: {
Simon Hunt7cd48f32014-11-09 23:42:50 -080089 direct: 100,
90 optical: 120,
Thomas Vachuska3266abf2014-11-13 09:28:46 -080091 hostLink: 3
Simon Huntc7ee0662014-11-05 16:44:37 -080092 },
93 linkStrength: {
Simon Hunt7cd48f32014-11-09 23:42:50 -080094 direct: 1.0,
95 optical: 1.0,
96 hostLink: 1.0
Simon Huntc7ee0662014-11-05 16:44:37 -080097 },
Simon Hunt7cd48f32014-11-09 23:42:50 -080098 note_for_nodes: 'node.class is used to differentiate',
Simon Huntc7ee0662014-11-05 16:44:37 -080099 charge: {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800100 device: -8000,
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800101 host: -5000
Simon Huntc7ee0662014-11-05 16:44:37 -0800102 },
103 pad: 20,
Simon Hunt195cb382014-11-03 17:50:51 -0800104 translate: function() {
105 return 'translate(' +
Simon Huntc7ee0662014-11-05 16:44:37 -0800106 config.force.pad + ',' +
107 config.force.pad + ')';
Simon Hunt195cb382014-11-03 17:50:51 -0800108 }
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800109 },
110 // see below in creation of viewBox on main svg
111 logicalSize: 1000
Simon Hunt195cb382014-11-03 17:50:51 -0800112 };
113
Simon Hunt142d0032014-11-04 20:13:09 -0800114 // radio buttons
Simon Hunt9462e8c2014-11-14 17:28:09 -0800115 var layerButtons = [
116 { text: 'All Layers', id: 'all', cb: showAllLayers },
117 { text: 'Packet Only', id: 'pkt', cb: showPacketLayer },
118 { text: 'Optical Only', id: 'opt', cb: showOpticalLayer }
119 ],
120 layerBtnSet,
121 layerBtnDispatch = {
122 all: showAllLayers,
123 pkt: showPacketLayer,
124 opt: showOpticalLayer
125 };
Simon Hunt934c3ce2014-11-05 11:45:07 -0800126
127 // key bindings
128 var keyDispatch = {
Simon Hunta255a2c2014-11-13 22:29:35 -0800129 M: testMe, // TODO: remove (testing only)
130 S: injectStartupEvents, // TODO: remove (testing only)
131 space: injectTestEvent, // TODO: remove (testing only)
Simon Hunt99c13842014-11-06 18:23:12 -0800132
Simon Hunt01095ff2014-11-13 16:37:29 -0800133 B: toggleBg,
Simon Hunt934c3ce2014-11-05 11:45:07 -0800134 L: cycleLabels,
135 P: togglePorts,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800136 U: unpin,
Paul Greysonfcba0e82014-11-13 10:21:16 -0800137 R: resetZoomPan,
Simon Hunt9462e8c2014-11-14 17:28:09 -0800138 esc: handleEscape
Simon Hunt934c3ce2014-11-05 11:45:07 -0800139 };
Simon Hunt142d0032014-11-04 20:13:09 -0800140
Simon Hunt195cb382014-11-03 17:50:51 -0800141 // state variables
Simon Hunt99c13842014-11-06 18:23:12 -0800142 var network = {
Simon Hunt50128c02014-11-08 13:36:15 -0800143 view: null, // view token reference
Simon Hunt99c13842014-11-06 18:23:12 -0800144 nodes: [],
145 links: [],
146 lookup: {}
147 },
Simon Hunt56d51852014-11-09 13:03:35 -0800148 scenario = {
149 evDir: 'json/ev/',
150 evScenario: '/scenario.json',
151 evPrefix: '/ev_',
152 evOnos: '_onos.json',
153 evUi: '_ui.json',
154 ctx: null,
155 params: {},
156 evNumber: 0,
157 view: null,
158 debug: false
159 },
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800160 webSock,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800161 sid = 0,
Simon Hunt56d51852014-11-09 13:03:35 -0800162 deviceLabelIndex = 0,
163 hostLabelIndex = 0,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800164 selections = {},
Simon Hunta5e89142014-11-14 07:00:33 -0800165 selectOrder = [],
Simon Hunt6ac93f32014-11-13 12:17:27 -0800166 hovered = null,
Simon Hunta5e89142014-11-14 07:00:33 -0800167 detailPane,
Simon Hunta255a2c2014-11-13 22:29:35 -0800168 antTimer = null,
Simon Hunta5e89142014-11-14 07:00:33 -0800169 onosInstances = {},
170 onosOrder = [],
171 oiBox,
Simon Hunt9462e8c2014-11-14 17:28:09 -0800172 oiShowMaster = false,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800173
Simon Hunt195cb382014-11-03 17:50:51 -0800174 portLabelsOn = false;
175
Simon Hunt934c3ce2014-11-05 11:45:07 -0800176 // D3 selections
177 var svg,
Paul Greysonfcba0e82014-11-13 10:21:16 -0800178 zoomPanContainer,
Simon Hunt934c3ce2014-11-05 11:45:07 -0800179 bgImg,
Simon Huntc7ee0662014-11-05 16:44:37 -0800180 topoG,
181 nodeG,
182 linkG,
183 node,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800184 link,
185 mask;
Simon Hunt195cb382014-11-03 17:50:51 -0800186
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800187 // the projection for the map background
188 var geoMapProjection;
189
Paul Greysonfcba0e82014-11-13 10:21:16 -0800190 // the zoom function
191 var zoom;
192
Simon Hunt142d0032014-11-04 20:13:09 -0800193 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800194 // For Debugging / Development
Simon Hunt195cb382014-11-03 17:50:51 -0800195
Simon Hunt99c13842014-11-06 18:23:12 -0800196 function note(label, msg) {
197 console.log('NOTE: ' + label + ': ' + msg);
Simon Hunt195cb382014-11-03 17:50:51 -0800198 }
199
Simon Hunt99c13842014-11-06 18:23:12 -0800200 function debug(what) {
201 return config.debugOn && config.debug[what];
Simon Hunt934c3ce2014-11-05 11:45:07 -0800202 }
203
Simon Huntfc274c92014-11-11 11:05:46 -0800204 function fnTrace(msg, id) {
205 if (config.fnTrace) {
206 console.log('FN: ' + msg + ' [' + id + ']');
207 }
208 }
Simon Hunt99c13842014-11-06 18:23:12 -0800209
Simon Hunta5e89142014-11-14 07:00:33 -0800210 function evTrace(data) {
211 fnTrace(data.event, data.payload.id);
212 }
213
Simon Hunt934c3ce2014-11-05 11:45:07 -0800214 // ==============================
215 // Key Callbacks
216
Simon Hunt99c13842014-11-06 18:23:12 -0800217 function testMe(view) {
Simon Hunta5e89142014-11-14 07:00:33 -0800218 //view.alert('test');
219 detailPane.show();
220 setTimeout(detailPane.hide, 2000);
221 oiBox.show();
222 setTimeout(oiBox.hide, 2000);
Simon Hunt99c13842014-11-06 18:23:12 -0800223 }
224
Simon Hunt56d51852014-11-09 13:03:35 -0800225 function abortIfLive() {
Simon Hunt50128c02014-11-08 13:36:15 -0800226 if (config.useLiveData) {
Simon Huntb53e0682014-11-12 13:32:01 -0800227 network.view.alert("Sorry, currently using live data..");
Simon Hunt56d51852014-11-09 13:03:35 -0800228 return true;
Simon Hunt50128c02014-11-08 13:36:15 -0800229 }
Simon Hunt56d51852014-11-09 13:03:35 -0800230 return false;
231 }
Simon Hunt50128c02014-11-08 13:36:15 -0800232
Simon Hunt56d51852014-11-09 13:03:35 -0800233 function testDebug(msg) {
234 if (scenario.debug) {
235 scenario.view.alert(msg);
236 }
237 }
Simon Hunt99c13842014-11-06 18:23:12 -0800238
Simon Hunt56d51852014-11-09 13:03:35 -0800239 function injectTestEvent(view) {
240 if (abortIfLive()) { return; }
241 var sc = scenario,
242 evn = ++sc.evNumber,
243 pfx = sc.evDir + sc.ctx + sc.evPrefix + evn,
244 onosUrl = pfx + sc.evOnos,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800245 uiUrl = pfx + sc.evUi,
246 stack = [
247 { url: onosUrl, cb: handleServerEvent },
248 { url: uiUrl, cb: handleUiEvent }
249 ];
250 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800251 }
252
Simon Hunt7cd48f32014-11-09 23:42:50 -0800253 function recurseFetchEvent(stack, evn) {
254 var v = scenario.view,
255 frame;
256 if (stack.length === 0) {
Simon Huntfc274c92014-11-11 11:05:46 -0800257 v.alert('Oops!\n\nNo event #' + evn + ' found.');
Simon Hunt7cd48f32014-11-09 23:42:50 -0800258 return;
259 }
260 frame = stack.shift();
261
262 d3.json(frame.url, function (err, data) {
Simon Hunt99c13842014-11-06 18:23:12 -0800263 if (err) {
Simon Hunt56d51852014-11-09 13:03:35 -0800264 if (err.status === 404) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800265 // if we didn't find the data, try the next stack frame
266 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800267 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800268 v.alert('non-404 error:\n\n' + frame.url + '\n\n' + err);
Simon Hunt56d51852014-11-09 13:03:35 -0800269 }
Simon Hunt99c13842014-11-06 18:23:12 -0800270 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800271 testDebug('loaded: ' + frame.url);
272 frame.cb(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800273 }
274 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800275
Simon Hunt56d51852014-11-09 13:03:35 -0800276 }
Simon Hunt50128c02014-11-08 13:36:15 -0800277
Simon Hunt56d51852014-11-09 13:03:35 -0800278 function handleUiEvent(data) {
Simon Huntbb282f52014-11-10 11:08:19 -0800279 scenario.view.alert('UI Tx: ' + data.event + '\n\n' +
280 JSON.stringify(data));
Simon Hunt56d51852014-11-09 13:03:35 -0800281 }
282
283 function injectStartupEvents(view) {
284 var last = scenario.params.lastAuto || 0;
285 if (abortIfLive()) { return; }
286
287 while (scenario.evNumber < last) {
Simon Hunt1a9eff92014-11-07 11:06:34 -0800288 injectTestEvent(view);
289 }
290 }
291
Simon Hunt934c3ce2014-11-05 11:45:07 -0800292 function toggleBg() {
293 var vis = bgImg.style('visibility');
294 bgImg.style('visibility', (vis === 'hidden') ? 'visible' : 'hidden');
295 }
296
Simon Hunt99c13842014-11-06 18:23:12 -0800297 function cycleLabels() {
Simon Huntbb282f52014-11-10 11:08:19 -0800298 deviceLabelIndex = (deviceLabelIndex === network.deviceLabelCount - 1)
299 ? 0 : deviceLabelIndex + 1;
Simon Hunt5f36d342014-11-08 21:33:14 -0800300
Simon Hunt99c13842014-11-06 18:23:12 -0800301 network.nodes.forEach(function (d) {
Simon Huntbb282f52014-11-10 11:08:19 -0800302 if (d.class === 'device') {
303 updateDeviceLabel(d);
304 }
Simon Hunt99c13842014-11-06 18:23:12 -0800305 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800306 }
307
308 function togglePorts(view) {
Simon Hunt50128c02014-11-08 13:36:15 -0800309 view.alert('togglePorts() callback')
Simon Hunt934c3ce2014-11-05 11:45:07 -0800310 }
311
Simon Hunt6ac93f32014-11-13 12:17:27 -0800312 function unpin() {
313 if (hovered) {
314 hovered.fixed = false;
315 hovered.el.classed('fixed', false);
316 network.force.resume();
317 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800318 }
319
Simon Hunt9462e8c2014-11-14 17:28:09 -0800320 function handleEscape(view) {
321 if (oiShowMaster) {
322 cancelAffinity();
323 } else {
324 deselectAll();
325 }
326 }
327
Simon Hunt934c3ce2014-11-05 11:45:07 -0800328 // ==============================
329 // Radio Button Callbacks
330
Simon Hunta5e89142014-11-14 07:00:33 -0800331 var layerLookup = {
332 host: {
333 endstation: 'pkt', // default, if host event does not define type
334 bgpSpeaker: 'pkt'
335 },
336 device: {
337 switch: 'pkt',
338 roadm: 'opt'
339 },
340 link: {
341 hostLink: 'pkt',
342 direct: 'pkt',
343 optical: 'opt'
344 }
345 };
346
347 function inLayer(d, layer) {
348 var look = layerLookup[d.class],
349 lyr = look && look[d.type];
350 return lyr === layer;
351 }
352
353 function unsuppressLayer(which) {
354 node.each(function (d) {
355 var node = d.el;
356 if (inLayer(d, which)) {
357 node.classed('suppressed', false);
358 }
359 });
360
361 link.each(function (d) {
362 var link = d.el;
363 if (inLayer(d, which)) {
364 link.classed('suppressed', false);
365 }
366 });
367 }
368
Simon Hunt9462e8c2014-11-14 17:28:09 -0800369 function suppressLayers(b) {
370 node.classed('suppressed', b);
371 link.classed('suppressed', b);
Simon Hunt142d0032014-11-04 20:13:09 -0800372// d3.selectAll('svg .port').classed('inactive', false);
373// d3.selectAll('svg .portText').classed('inactive', false);
Simon Hunt195cb382014-11-03 17:50:51 -0800374 }
375
Simon Hunt9462e8c2014-11-14 17:28:09 -0800376 function showAllLayers() {
377 suppressLayers(false);
378 }
379
Simon Hunt195cb382014-11-03 17:50:51 -0800380 function showPacketLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800381 node.classed('suppressed', true);
382 link.classed('suppressed', true);
383 unsuppressLayer('pkt');
Simon Hunt195cb382014-11-03 17:50:51 -0800384 }
385
386 function showOpticalLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800387 node.classed('suppressed', true);
388 link.classed('suppressed', true);
389 unsuppressLayer('opt');
Simon Hunt195cb382014-11-03 17:50:51 -0800390 }
391
Simon Hunt9462e8c2014-11-14 17:28:09 -0800392 function restoreLayerState() {
393 layerBtnDispatch[layerBtnSet.selected()]();
394 }
395
Simon Hunt142d0032014-11-04 20:13:09 -0800396 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800397 // Private functions
398
Simon Hunt99c13842014-11-06 18:23:12 -0800399 function safeId(s) {
400 return s.replace(/[^a-z0-9]/gi, '-');
401 }
402
Simon Huntc7ee0662014-11-05 16:44:37 -0800403 // set the size of the given element to that of the view (reduced if padded)
404 function setSize(el, view, pad) {
405 var padding = pad ? pad * 2 : 0;
Simon Hunt934c3ce2014-11-05 11:45:07 -0800406 el.attr({
Simon Huntc7ee0662014-11-05 16:44:37 -0800407 width: view.width() - padding,
408 height: view.height() - padding
Simon Hunt934c3ce2014-11-05 11:45:07 -0800409 });
410 }
411
Simon Hunt934c3ce2014-11-05 11:45:07 -0800412
Simon Hunt99c13842014-11-06 18:23:12 -0800413 // ==============================
414 // Event handlers for server-pushed events
415
Simon Huntbb282f52014-11-10 11:08:19 -0800416 function logicError(msg) {
417 // TODO, report logic error to server, via websock, so it can be logged
418 network.view.alert('Logic Error:\n\n' + msg);
Simon Huntfc274c92014-11-11 11:05:46 -0800419 console.warn(msg);
Simon Huntbb282f52014-11-10 11:08:19 -0800420 }
421
Simon Hunt99c13842014-11-06 18:23:12 -0800422 var eventDispatch = {
Simon Hunta5e89142014-11-14 07:00:33 -0800423 addInstance: addInstance,
Simon Hunt99c13842014-11-06 18:23:12 -0800424 addDevice: addDevice,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800425 addLink: addLink,
Simon Hunt56d51852014-11-09 13:03:35 -0800426 addHost: addHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800427
Simon Huntd72bc702014-11-13 18:38:04 -0800428 updateInstance: stillToImplement,
Simon Huntbb282f52014-11-10 11:08:19 -0800429 updateDevice: updateDevice,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800430 updateLink: updateLink,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800431 updateHost: updateHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800432
Simon Huntd72bc702014-11-13 18:38:04 -0800433 removeInstance: stillToImplement,
Simon Huntbb282f52014-11-10 11:08:19 -0800434 removeDevice: stillToImplement,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800435 removeLink: removeLink,
Simon Hunt44031102014-11-11 13:20:36 -0800436 removeHost: removeHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800437
Simon Hunt61d04042014-11-11 17:27:16 -0800438 showDetails: showDetails,
Simon Huntb53e0682014-11-12 13:32:01 -0800439 showPath: showPath,
440 showTraffic: showTraffic
Simon Hunt99c13842014-11-06 18:23:12 -0800441 };
442
Simon Hunta5e89142014-11-14 07:00:33 -0800443 function addInstance(data) {
444 evTrace(data);
445 var inst = data.payload,
446 id = inst.id;
447 if (onosInstances[id]) {
448 logicError('ONOS instance already added: ' + id);
449 return;
450 }
451 onosInstances[id] = inst;
452 onosOrder.push(inst);
453 updateInstances();
454 }
455
Simon Hunt99c13842014-11-06 18:23:12 -0800456 function addDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800457 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800458 var device = data.payload,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800459 nodeData = createDeviceNode(device);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800460 network.nodes.push(nodeData);
461 network.lookup[nodeData.id] = nodeData;
Simon Hunt99c13842014-11-06 18:23:12 -0800462 updateNodes();
463 network.force.start();
464 }
465
Simon Hunt99c13842014-11-06 18:23:12 -0800466 function addLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800467 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800468 var link = data.payload,
469 lnk = createLink(link);
Simon Hunt99c13842014-11-06 18:23:12 -0800470 if (lnk) {
Simon Hunt99c13842014-11-06 18:23:12 -0800471 network.links.push(lnk);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800472 network.lookup[lnk.id] = lnk;
Simon Hunt99c13842014-11-06 18:23:12 -0800473 updateLinks();
474 network.force.start();
475 }
476 }
477
Simon Hunt56d51852014-11-09 13:03:35 -0800478 function addHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800479 evTrace(data);
Simon Hunt56d51852014-11-09 13:03:35 -0800480 var host = data.payload,
481 node = createHostNode(host),
482 lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800483 network.nodes.push(node);
484 network.lookup[host.id] = node;
485 updateNodes();
486
487 lnk = createHostLink(host);
488 if (lnk) {
Simon Hunt44031102014-11-11 13:20:36 -0800489 node.linkData = lnk; // cache ref on its host
Simon Hunt56d51852014-11-09 13:03:35 -0800490 network.links.push(lnk);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800491 network.lookup[host.ingress] = lnk;
492 network.lookup[host.egress] = lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800493 updateLinks();
494 }
495 network.force.start();
496 }
497
Simon Hunt44031102014-11-11 13:20:36 -0800498 // TODO: fold updateX(...) methods into one base method; remove duplication
Simon Huntbb282f52014-11-10 11:08:19 -0800499 function updateDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800500 evTrace(data);
Simon Huntbb282f52014-11-10 11:08:19 -0800501 var device = data.payload,
502 id = device.id,
503 nodeData = network.lookup[id];
504 if (nodeData) {
505 $.extend(nodeData, device);
506 updateDeviceState(nodeData);
507 } else {
508 logicError('updateDevice lookup fail. ID = "' + id + '"');
509 }
510 }
511
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800512 function updateLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800513 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800514 var link = data.payload,
515 id = link.id,
516 linkData = network.lookup[id];
517 if (linkData) {
518 $.extend(linkData, link);
519 updateLinkState(linkData);
520 } else {
521 logicError('updateLink lookup fail. ID = "' + id + '"');
522 }
523 }
524
Simon Hunt7cd48f32014-11-09 23:42:50 -0800525 function updateHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800526 evTrace(data);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800527 var host = data.payload,
Simon Huntbb282f52014-11-10 11:08:19 -0800528 id = host.id,
529 hostData = network.lookup[id];
530 if (hostData) {
531 $.extend(hostData, host);
532 updateHostState(hostData);
533 } else {
534 logicError('updateHost lookup fail. ID = "' + id + '"');
535 }
Simon Hunt7cd48f32014-11-09 23:42:50 -0800536 }
537
Simon Hunt44031102014-11-11 13:20:36 -0800538 // TODO: fold removeX(...) methods into base method - remove dup code
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800539 function removeLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800540 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800541 var link = data.payload,
542 id = link.id,
543 linkData = network.lookup[id];
544 if (linkData) {
545 removeLinkElement(linkData);
546 } else {
547 logicError('removeLink lookup fail. ID = "' + id + '"');
548 }
549 }
550
Simon Hunt44031102014-11-11 13:20:36 -0800551 function removeHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800552 evTrace(data);
Simon Hunt44031102014-11-11 13:20:36 -0800553 var host = data.payload,
554 id = host.id,
555 hostData = network.lookup[id];
556 if (hostData) {
557 removeHostElement(hostData);
558 } else {
559 logicError('removeHost lookup fail. ID = "' + id + '"');
560 }
561 }
562
Simon Hunt61d04042014-11-11 17:27:16 -0800563 function showDetails(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800564 evTrace(data);
Simon Hunt61d04042014-11-11 17:27:16 -0800565 populateDetails(data.payload);
566 detailPane.show();
567 }
568
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800569 function showPath(data) {
Simon Huntd72bc702014-11-13 18:38:04 -0800570 // TODO: review - making sure we are handling the payload correctly.
Simon Hunta5e89142014-11-14 07:00:33 -0800571 evTrace(data);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800572 var links = data.payload.links,
573 s = [ data.event + "\n" + links.length ];
574 links.forEach(function (d, i) {
575 s.push(d);
576 });
577 network.view.alert(s.join('\n'));
578
579 links.forEach(function (d, i) {
580 var link = network.lookup[d];
581 if (link) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800582 link.el.classed('showPath', true);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800583 }
584 });
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800585 }
586
Simon Huntb53e0682014-11-12 13:32:01 -0800587 function showTraffic(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800588 evTrace(data);
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800589 var paths = data.payload.paths;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800590
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800591 // Revert any links hilighted previously.
Simon Hunta255a2c2014-11-13 22:29:35 -0800592 link.classed('primary secondary animated optical', false);
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800593
594 // Now hilight all links in the paths payload.
Simon Hunta255a2c2014-11-13 22:29:35 -0800595 paths.forEach(function (p) {
596 var cls = p.class;
597 p.links.forEach(function (id) {
598 var lnk = network.lookup[id];
599 if (lnk) {
600 lnk.el.classed(cls, true);
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800601 }
602 });
603 });
Simon Huntb53e0682014-11-12 13:32:01 -0800604 }
605
Simon Hunt56d51852014-11-09 13:03:35 -0800606 // ...............................
607
608 function stillToImplement(data) {
609 var p = data.payload;
610 note(data.event, p.id);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800611 network.view.alert('Not yet implemented: "' + data.event + '"');
Simon Hunt56d51852014-11-09 13:03:35 -0800612 }
Simon Hunt99c13842014-11-06 18:23:12 -0800613
614 function unknownEvent(data) {
Simon Hunt50128c02014-11-08 13:36:15 -0800615 network.view.alert('Unknown event type: "' + data.event + '"');
Simon Hunt99c13842014-11-06 18:23:12 -0800616 }
617
618 function handleServerEvent(data) {
619 var fn = eventDispatch[data.event] || unknownEvent;
620 fn(data);
621 }
622
623 // ==============================
Simon Hunt61d04042014-11-11 17:27:16 -0800624 // Out-going messages...
625
Simon Huntb53e0682014-11-12 13:32:01 -0800626 function userFeedback(msg) {
627 // for now, use the alert pane as is. Maybe different alert style in
628 // the future (centered on view; dismiss button?)
629 network.view.alert(msg);
630 }
631
632 function nSel() {
633 return selectOrder.length;
634 }
Simon Hunt61d04042014-11-11 17:27:16 -0800635 function getSel(idx) {
636 return selections[selectOrder[idx]];
637 }
Simon Huntb53e0682014-11-12 13:32:01 -0800638 function getSelId(idx) {
639 return getSel(idx).obj.id;
640 }
641 function allSelectionsClass(cls) {
642 for (var i=0, n=nSel(); i<n; i++) {
643 if (getSel(i).obj.class !== cls) {
644 return false;
645 }
646 }
647 return true;
648 }
Simon Hunt61d04042014-11-11 17:27:16 -0800649
Simon Hunt61d04042014-11-11 17:27:16 -0800650 // request details for the selected element
Simon Huntd72bc702014-11-13 18:38:04 -0800651 // invoked from selection of a single node.
Simon Hunt61d04042014-11-11 17:27:16 -0800652 function requestDetails() {
653 var data = getSel(0).obj,
654 payload = {
655 id: data.id,
656 class: data.class
657 };
658 sendMessage('requestDetails', payload);
659 }
660
Simon Huntd72bc702014-11-13 18:38:04 -0800661 function addIntentAction() {
662 sendMessage('addHostIntent', {
663 one: getSelId(0),
664 two: getSelId(1)
665 });
666 }
667
668 function showTrafficAction() {
669 // if nothing is hovered over, and nothing selected, send cancel request
670 if (!hovered && nSel() === 0) {
671 sendMessage('cancelTraffic', {});
672 return;
673 }
674
675 // NOTE: hover is only populated if "show traffic on hover" is
676 // toggled on, and the item hovered is a host...
677 var hoverId = (trafficHover() && hovered && hovered.class === 'host')
678 ? hovered.id : '';
679 sendMessage('requestTraffic', {
680 ids: selectOrder,
681 hover: hoverId
682 });
683 }
684
685
Simon Hunt61d04042014-11-11 17:27:16 -0800686 // ==============================
Simon Hunta5e89142014-11-14 07:00:33 -0800687 // onos instance panel functions
688
689 function updateInstances() {
690 var onoses = oiBox.el.selectAll('.onosInst')
691 .data(onosOrder, function (d) { return d.id; });
692
693 // operate on existing onoses if necessary
694
695 var entering = onoses.enter()
696 .append('div')
697 .attr('class', 'onosInst')
698 .classed('online', function (d) { return d.online; })
Simon Hunt9c15eca2014-11-15 18:37:59 -0800699 .on('click', clickInst);
700
701 entering.each(function (d, i) {
702 var el = d3.select(this),
703 img;
704
705 $('<img src="img/host.png">').appendTo(el);
706 img = el.select('img')
707 .attr({
708 width: 40,
709 top: -10,
710 left: -10
711 })
712 .style({
713 });
714
715 $('<div>').attr('class', 'onosTitle').text(d.id).appendTo(el);
716
717 // is the UI attached to this instance?
718 // TODO: need uiAttached boolean in instance data
719 //if (d.uiAttached) {
720 if (i === 0) {
721 $('<img src="img/ui.png">').attr('class','ui').appendTo(el);
722 }
723 });
Simon Hunta5e89142014-11-14 07:00:33 -0800724
725 // operate on existing + new onoses here
726
727 // the departed...
728 var exiting = onoses.exit()
729 .transition()
730 .style('opacity', 0)
731 .remove();
732 }
733
Simon Hunt9462e8c2014-11-14 17:28:09 -0800734 function clickInst(d) {
735 var el = d3.select(this),
736 aff = el.classed('affinity');
737 if (!aff) {
738 setAffinity(el, d);
739 } else {
740 cancelAffinity();
741 }
742 }
743
744 function setAffinity(el, d) {
745 d3.selectAll('.onosInst')
746 .classed('mastership', true)
747 .classed('affinity', false);
748 el.classed('affinity', true);
749
750 suppressLayers(true);
751 node.each(function (n) {
752 if (n.master === d.id) {
753 n.el.classed('suppressed', false);
754 }
755 });
756 oiShowMaster = true;
757 }
758
759 function cancelAffinity() {
760 d3.selectAll('.onosInst')
761 .classed('mastership affinity', false);
762 restoreLayerState();
763 oiShowMaster = false;
764 }
765
Simon Hunta5e89142014-11-14 07:00:33 -0800766 // ==============================
Simon Hunt99c13842014-11-06 18:23:12 -0800767 // force layout modification functions
768
769 function translate(x, y) {
770 return 'translate(' + x + ',' + y + ')';
771 }
772
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800773 function missMsg(what, id) {
774 return '\n[' + what + '] "' + id + '" missing ';
775 }
776
777 function linkEndPoints(srcId, dstId) {
778 var srcNode = network.lookup[srcId],
779 dstNode = network.lookup[dstId],
780 sMiss = !srcNode ? missMsg('src', srcId) : '',
781 dMiss = !dstNode ? missMsg('dst', dstId) : '';
782
783 if (sMiss || dMiss) {
784 logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
785 return null;
786 }
787 return {
788 source: srcNode,
789 target: dstNode,
790 x1: srcNode.x,
791 y1: srcNode.y,
792 x2: dstNode.x,
793 y2: dstNode.y
794 };
795 }
796
Simon Hunt56d51852014-11-09 13:03:35 -0800797 function createHostLink(host) {
798 var src = host.id,
799 dst = host.cp.device,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800800 id = host.ingress,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800801 lnk = linkEndPoints(src, dst);
Simon Hunt56d51852014-11-09 13:03:35 -0800802
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800803 if (!lnk) {
Simon Hunt56d51852014-11-09 13:03:35 -0800804 return null;
805 }
806
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800807 // Synthesize link ...
808 $.extend(lnk, {
Thomas Vachuska4830d392014-11-09 17:09:56 -0800809 id: id,
Simon Hunt56d51852014-11-09 13:03:35 -0800810 class: 'link',
Simon Hunt7cd48f32014-11-09 23:42:50 -0800811 type: 'hostLink',
Simon Hunt56d51852014-11-09 13:03:35 -0800812 svgClass: 'link hostLink',
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800813 linkWidth: 1
Simon Hunt7cd48f32014-11-09 23:42:50 -0800814 });
Simon Hunt99c13842014-11-06 18:23:12 -0800815 return lnk;
816 }
817
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800818 function createLink(link) {
819 var lnk = linkEndPoints(link.src, link.dst),
820 type = link.type;
821
822 if (!lnk) {
823 return null;
824 }
825
826 // merge in remaining data
827 $.extend(lnk, link, {
828 class: 'link',
829 svgClass: type ? 'link ' + type : 'link'
830 });
831 return lnk;
Simon Hunt1a9eff92014-11-07 11:06:34 -0800832 }
833
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800834 var widthRatio = 1.4,
835 linkScale = d3.scale.linear()
836 .domain([1, 12])
837 .range([widthRatio, 12 * widthRatio])
838 .clamp(true);
839
840 function updateLinkWidth (d) {
841 // TODO: watch out for .showPath/.showTraffic classes
842 d.el.transition()
843 .duration(1000)
844 .attr('stroke-width', linkScale(d.linkWidth));
845 }
846
847
Simon Hunt99c13842014-11-06 18:23:12 -0800848 function updateLinks() {
849 link = linkG.selectAll('.link')
850 .data(network.links, function (d) { return d.id; });
851
852 // operate on existing links, if necessary
853 // link .foo() .bar() ...
854
855 // operate on entering links:
856 var entering = link.enter()
857 .append('line')
858 .attr({
Simon Hunt99c13842014-11-06 18:23:12 -0800859 class: function (d) { return d.svgClass; },
860 x1: function (d) { return d.x1; },
861 y1: function (d) { return d.y1; },
862 x2: function (d) { return d.x2; },
863 y2: function (d) { return d.y2; },
Simon Hunt1a9eff92014-11-07 11:06:34 -0800864 stroke: config.topo.linkInColor,
865 'stroke-width': config.topo.linkInWidth
Simon Hunt99c13842014-11-06 18:23:12 -0800866 })
867 .transition().duration(1000)
868 .attr({
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800869 'stroke-width': function (d) { return linkScale(d.linkWidth); },
Simon Hunt99c13842014-11-06 18:23:12 -0800870 stroke: '#666' // TODO: remove explicit stroke, rather...
871 });
872
873 // augment links
Simon Hunt7cd48f32014-11-09 23:42:50 -0800874 entering.each(function (d) {
875 var link = d3.select(this);
876 // provide ref to element selection from backing data....
877 d.el = link;
Simon Hunt99c13842014-11-06 18:23:12 -0800878
Simon Hunt7cd48f32014-11-09 23:42:50 -0800879 // TODO: add src/dst port labels etc.
880 });
Thomas Vachuska4830d392014-11-09 17:09:56 -0800881
882 // operate on both existing and new links, if necessary
883 //link .foo() .bar() ...
884
885 // operate on exiting links:
Thomas Vachuska4830d392014-11-09 17:09:56 -0800886 link.exit()
Thomas Vachuska4830d392014-11-09 17:09:56 -0800887 .attr({
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800888 'stroke-dasharray': '3, 3'
Thomas Vachuska4830d392014-11-09 17:09:56 -0800889 })
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800890 .style('opacity', 0.4)
891 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -0800892 .duration(1500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800893 .attr({
894 'stroke-dasharray': '3, 12'
895 })
896 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -0800897 .duration(500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800898 .style('opacity', 0.0)
Thomas Vachuska4830d392014-11-09 17:09:56 -0800899 .remove();
Simon Hunt99c13842014-11-06 18:23:12 -0800900 }
901
902 function createDeviceNode(device) {
903 // start with the object as is
904 var node = device,
Simon Huntbb282f52014-11-10 11:08:19 -0800905 type = device.type,
906 svgCls = type ? 'node device ' + type : 'node device';
Simon Hunt99c13842014-11-06 18:23:12 -0800907
908 // Augment as needed...
909 node.class = 'device';
Simon Huntbb282f52014-11-10 11:08:19 -0800910 node.svgClass = device.online ? svgCls + ' online' : svgCls;
Simon Hunt99c13842014-11-06 18:23:12 -0800911 positionNode(node);
912
913 // cache label array length
914 network.deviceLabelCount = device.labels.length;
Simon Hunt99c13842014-11-06 18:23:12 -0800915 return node;
916 }
917
Simon Hunt56d51852014-11-09 13:03:35 -0800918 function createHostNode(host) {
919 // start with the object as is
920 var node = host;
921
922 // Augment as needed...
923 node.class = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -0800924 if (!node.type) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800925 node.type = 'endstation';
926 }
Simon Hunt56d51852014-11-09 13:03:35 -0800927 node.svgClass = 'node host';
Simon Hunt56d51852014-11-09 13:03:35 -0800928 positionNode(node);
929
930 // cache label array length
931 network.hostLabelCount = host.labels.length;
Simon Hunt56d51852014-11-09 13:03:35 -0800932 return node;
933 }
934
Simon Hunt99c13842014-11-06 18:23:12 -0800935 function positionNode(node) {
936 var meta = node.metaUi,
Simon Huntac9e24f2014-11-12 10:12:21 -0800937 x = meta && meta.x,
938 y = meta && meta.y,
939 xy;
Simon Hunt99c13842014-11-06 18:23:12 -0800940
Simon Huntac9e24f2014-11-12 10:12:21 -0800941 // If we have [x,y] already, use that...
Simon Hunt99c13842014-11-06 18:23:12 -0800942 if (x && y) {
943 node.fixed = true;
Simon Huntac9e24f2014-11-12 10:12:21 -0800944 node.x = x;
945 node.y = y;
946 return;
Simon Hunt99c13842014-11-06 18:23:12 -0800947 }
Simon Huntac9e24f2014-11-12 10:12:21 -0800948
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800949 var location = node.location;
950 if (location && location.type === 'latlng') {
951 var coord = geoMapProjection([location.lng, location.lat]);
952 node.fixed = true;
953 node.x = coord[0];
954 node.y = coord[1];
955 return;
956 }
957
Simon Huntac9e24f2014-11-12 10:12:21 -0800958 // Note: Placing incoming unpinned nodes at exactly the same point
959 // (center of the view) causes them to explode outwards when
960 // the force layout kicks in. So, we spread them out a bit
961 // initially, to provide a more serene layout convergence.
962 // Additionally, if the node is a host, we place it near
963 // the device it is connected to.
964
965 function spread(s) {
966 return Math.floor((Math.random() * s) - s/2);
967 }
968
969 function randDim(dim) {
970 return dim / 2 + spread(dim * 0.7071);
971 }
972
973 function rand() {
974 return {
975 x: randDim(network.view.width()),
976 y: randDim(network.view.height())
977 };
978 }
979
980 function near(node) {
981 var min = 12,
982 dx = spread(12),
983 dy = spread(12);
984 return {
985 x: node.x + min + dx,
986 y: node.y + min + dy
987 };
988 }
989
990 function getDevice(cp) {
991 var d = network.lookup[cp.device];
992 return d || rand();
993 }
994
995 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
996 $.extend(node, xy);
Simon Hunt99c13842014-11-06 18:23:12 -0800997 }
998
Simon Hunt99c13842014-11-06 18:23:12 -0800999 function iconUrl(d) {
1000 return 'img/' + d.type + '.png';
1001 }
1002
1003 // returns the newly computed bounding box of the rectangle
1004 function adjustRectToFitText(n) {
1005 var text = n.select('text'),
1006 box = text.node().getBBox(),
1007 lab = config.labels;
1008
1009 text.attr('text-anchor', 'middle')
1010 .attr('y', '-0.8em')
1011 .attr('x', lab.imgPad/2);
1012
1013 // translate the bbox so that it is centered on [x,y]
1014 box.x = -box.width / 2;
1015 box.y = -box.height / 2;
1016
1017 // add padding
1018 box.x -= (lab.padLR + lab.imgPad/2);
1019 box.width += lab.padLR * 2 + lab.imgPad;
1020 box.y -= lab.padTB;
1021 box.height += lab.padTB * 2;
1022
1023 return box;
1024 }
1025
Simon Hunt1a9eff92014-11-07 11:06:34 -08001026 function mkSvgClass(d) {
1027 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
1028 }
1029
Simon Hunt7cd48f32014-11-09 23:42:50 -08001030 function hostLabel(d) {
1031 var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0;
1032 return d.labels[idx];
1033 }
1034 function deviceLabel(d) {
1035 var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0;
1036 return d.labels[idx];
1037 }
1038 function niceLabel(label) {
1039 return (label && label.trim()) ? label : '.';
1040 }
1041
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001042 function updateDeviceLabel(d) {
1043 var label = niceLabel(deviceLabel(d)),
1044 node = d.el,
1045 box;
1046
1047 node.select('text')
1048 .text(label)
1049 .style('opacity', 0)
1050 .transition()
1051 .style('opacity', 1);
1052
1053 box = adjustRectToFitText(node);
1054
1055 node.select('rect')
1056 .transition()
1057 .attr(box);
1058
1059 node.select('image')
1060 .transition()
1061 .attr('x', box.x + config.icons.xoff)
1062 .attr('y', box.y + config.icons.yoff);
1063 }
1064
1065 function updateHostLabel(d) {
1066 var label = hostLabel(d),
1067 host = d.el;
1068
1069 host.select('text').text(label);
1070 }
1071
Simon Huntbb282f52014-11-10 11:08:19 -08001072 function updateDeviceState(nodeData) {
1073 nodeData.el.classed('online', nodeData.online);
1074 updateDeviceLabel(nodeData);
1075 // TODO: review what else might need to be updated
1076 }
1077
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001078 function updateLinkState(linkData) {
1079 updateLinkWidth(linkData);
1080 // TODO: review what else might need to be updated
1081 // update label, if showing
1082 }
1083
Simon Huntbb282f52014-11-10 11:08:19 -08001084 function updateHostState(hostData) {
1085 updateHostLabel(hostData);
1086 // TODO: review what else might need to be updated
1087 }
1088
Simon Hunt6ac93f32014-11-13 12:17:27 -08001089 function nodeMouseOver(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001090 hovered = d;
Simon Huntd72bc702014-11-13 18:38:04 -08001091 if (trafficHover() && d.class === 'host') {
1092 showTrafficAction();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001093 }
1094 }
1095
1096 function nodeMouseOut(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001097 hovered = null;
Simon Huntd72bc702014-11-13 18:38:04 -08001098 if (trafficHover() && d.class === 'host') {
1099 showTrafficAction();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001100 }
1101 }
Simon Huntbb282f52014-11-10 11:08:19 -08001102
Simon Hunt99c13842014-11-06 18:23:12 -08001103 function updateNodes() {
1104 node = nodeG.selectAll('.node')
1105 .data(network.nodes, function (d) { return d.id; });
1106
1107 // operate on existing nodes, if necessary
Simon Hunt7cd48f32014-11-09 23:42:50 -08001108 // update host labels
Simon Hunt99c13842014-11-06 18:23:12 -08001109 //node .foo() .bar() ...
1110
1111 // operate on entering nodes:
1112 var entering = node.enter()
1113 .append('g')
1114 .attr({
1115 id: function (d) { return safeId(d.id); },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001116 class: mkSvgClass,
Simon Hunt99c13842014-11-06 18:23:12 -08001117 transform: function (d) { return translate(d.x, d.y); },
1118 opacity: 0
1119 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08001120 .call(network.drag)
Simon Hunt6ac93f32014-11-13 12:17:27 -08001121 .on('mouseover', nodeMouseOver)
1122 .on('mouseout', nodeMouseOut)
Simon Hunt99c13842014-11-06 18:23:12 -08001123 .transition()
1124 .attr('opacity', 1);
1125
1126 // augment device nodes...
1127 entering.filter('.device').each(function (d) {
1128 var node = d3.select(this),
1129 icon = iconUrl(d),
Simon Hunt7cd48f32014-11-09 23:42:50 -08001130 label = niceLabel(deviceLabel(d)),
Simon Hunt99c13842014-11-06 18:23:12 -08001131 box;
1132
Simon Hunt7cd48f32014-11-09 23:42:50 -08001133 // provide ref to element from backing data....
1134 d.el = node;
1135
Simon Hunt99c13842014-11-06 18:23:12 -08001136 node.append('rect')
1137 .attr({
1138 'rx': 5,
1139 'ry': 5
1140 });
1141
1142 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001143 .text(label)
Simon Hunt99c13842014-11-06 18:23:12 -08001144 .attr('dy', '1.1em');
1145
1146 box = adjustRectToFitText(node);
1147
1148 node.select('rect')
1149 .attr(box);
1150
1151 if (icon) {
1152 var cfg = config.icons;
1153 node.append('svg:image')
1154 .attr({
1155 x: box.x + config.icons.xoff,
1156 y: box.y + config.icons.yoff,
1157 width: cfg.w,
1158 height: cfg.h,
1159 'xlink:href': icon
1160 });
1161 }
1162
1163 // debug function to show the modelled x,y coordinates of nodes...
1164 if (debug('showNodeXY')) {
1165 node.select('rect').attr('fill-opacity', 0.5);
1166 node.append('circle')
1167 .attr({
1168 class: 'debug',
1169 cx: 0,
1170 cy: 0,
1171 r: '3px'
1172 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001173 }
1174 });
Simon Hunt934c3ce2014-11-05 11:45:07 -08001175
Simon Hunt56d51852014-11-09 13:03:35 -08001176 // augment host nodes...
1177 entering.filter('.host').each(function (d) {
1178 var node = d3.select(this),
Simon Hunt56d51852014-11-09 13:03:35 -08001179 box;
1180
Simon Hunt7cd48f32014-11-09 23:42:50 -08001181 // provide ref to element from backing data....
1182 d.el = node;
1183
Simon Hunt56d51852014-11-09 13:03:35 -08001184 node.append('circle')
1185 .attr('r', 8); // TODO: define host circle radius
1186
Simon Hunt56d51852014-11-09 13:03:35 -08001187 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001188 .text(hostLabel)
1189 .attr('dy', '1.3em')
1190 .attr('text-anchor', 'middle');
Simon Hunt56d51852014-11-09 13:03:35 -08001191
1192 // debug function to show the modelled x,y coordinates of nodes...
1193 if (debug('showNodeXY')) {
1194 node.select('circle').attr('fill-opacity', 0.5);
1195 node.append('circle')
1196 .attr({
1197 class: 'debug',
1198 cx: 0,
1199 cy: 0,
1200 r: '3px'
1201 });
1202 }
1203 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001204
Simon Hunt99c13842014-11-06 18:23:12 -08001205 // operate on both existing and new nodes, if necessary
1206 //node .foo() .bar() ...
Simon Huntc7ee0662014-11-05 16:44:37 -08001207
Simon Hunt99c13842014-11-06 18:23:12 -08001208 // operate on exiting nodes:
Simon Huntea80eb42014-11-11 13:46:57 -08001209 // Note that the node is removed after 2 seconds.
1210 // Sub element animations should be shorter than 2 seconds.
1211 var exiting = node.exit()
Simon Hunt44031102014-11-11 13:20:36 -08001212 .transition()
1213 .duration(2000)
Simon Huntea80eb42014-11-11 13:46:57 -08001214 .style('opacity', 0)
Simon Hunt99c13842014-11-06 18:23:12 -08001215 .remove();
Simon Huntea80eb42014-11-11 13:46:57 -08001216
1217 // host node exits....
1218 exiting.filter('.host').each(function (d) {
1219 var node = d3.select(this);
1220
1221 node.select('text')
1222 .style('opacity', 0.5)
1223 .transition()
1224 .duration(1000)
1225 .style('opacity', 0);
1226 // note, leave <g>.remove to remove this element
1227
1228 node.select('circle')
1229 .style('stroke-fill', '#555')
1230 .style('fill', '#888')
1231 .style('opacity', 0.5)
1232 .transition()
1233 .duration(1500)
1234 .attr('r', 0);
1235 // note, leave <g>.remove to remove this element
1236
1237 });
1238
1239 // TODO: device node exits
Simon Huntc7ee0662014-11-05 16:44:37 -08001240 }
1241
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001242 function find(id, array) {
1243 for (var idx = 0, n = array.length; idx < n; idx++) {
1244 if (array[idx].id === id) {
1245 return idx;
1246 }
1247 }
1248 return -1;
1249 }
1250
1251 function removeLinkElement(linkData) {
1252 // remove from lookup cache
1253 delete network.lookup[linkData.id];
1254 // remove from links array
1255 var idx = find(linkData.id, network.links);
Simon Huntfc274c92014-11-11 11:05:46 -08001256 network.links.splice(idx, 1);
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001257 // remove from SVG
1258 updateLinks();
Simon Hunt44031102014-11-11 13:20:36 -08001259 network.force.resume();
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001260 }
Simon Huntc7ee0662014-11-05 16:44:37 -08001261
Simon Hunt44031102014-11-11 13:20:36 -08001262 function removeHostElement(hostData) {
1263 // first, remove associated hostLink...
1264 removeLinkElement(hostData.linkData);
1265
1266 // remove from lookup cache
1267 delete network.lookup[hostData.id];
1268 // remove from nodes array
1269 var idx = find(hostData.id, network.nodes);
1270 network.nodes.splice(idx, 1);
1271 // remove from SVG
1272 updateNodes();
1273 network.force.resume();
1274 }
1275
1276
Simon Huntc7ee0662014-11-05 16:44:37 -08001277 function tick() {
1278 node.attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001279 transform: function (d) { return translate(d.x, d.y); }
Simon Huntc7ee0662014-11-05 16:44:37 -08001280 });
1281
1282 link.attr({
1283 x1: function (d) { return d.source.x; },
1284 y1: function (d) { return d.source.y; },
1285 x2: function (d) { return d.target.x; },
1286 y2: function (d) { return d.target.y; }
1287 });
1288 }
Simon Hunt934c3ce2014-11-05 11:45:07 -08001289
1290 // ==============================
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001291 // Web-Socket for live data
1292
1293 function webSockUrl() {
1294 return document.location.toString()
1295 .replace(/\#.*/, '')
1296 .replace('http://', 'ws://')
1297 .replace('https://', 'wss://')
1298 .replace('index2.html', config.webSockUrl);
1299 }
1300
1301 webSock = {
1302 ws : null,
1303
1304 connect : function() {
1305 webSock.ws = new WebSocket(webSockUrl());
1306
1307 webSock.ws.onopen = function() {
Simon Hunt0c6d4192014-11-12 12:07:10 -08001308 noWebSock(false);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001309 };
1310
1311 webSock.ws.onmessage = function(m) {
1312 if (m.data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001313 wsTraceRx(m.data);
Thomas Vachuskad472c6e2014-11-07 19:11:05 -08001314 handleServerEvent(JSON.parse(m.data));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001315 }
1316 };
1317
1318 webSock.ws.onclose = function(m) {
1319 webSock.ws = null;
Simon Hunt0c6d4192014-11-12 12:07:10 -08001320 noWebSock(true);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001321 };
1322 },
1323
1324 send : function(text) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001325 if (text != null) {
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001326 webSock._send(text);
1327 }
1328 },
1329
1330 _send : function(message) {
1331 if (webSock.ws) {
1332 webSock.ws.send(message);
Simon Hunta255a2c2014-11-13 22:29:35 -08001333 } else if (config.useLiveData) {
Simon Hunt56d51852014-11-09 13:03:35 -08001334 network.view.alert('no web socket open\n\n' + message);
Simon Hunta255a2c2014-11-13 22:29:35 -08001335 } else {
1336 console.log('WS Send: ' + JSON.stringify(message));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001337 }
1338 }
1339
1340 };
1341
Simon Hunt0c6d4192014-11-12 12:07:10 -08001342 function noWebSock(b) {
1343 mask.style('display',b ? 'block' : 'none');
1344 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001345
Simon Hunt61d04042014-11-11 17:27:16 -08001346 // TODO: use cache of pending messages (key = sid) to reconcile responses
1347
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001348 function sendMessage(evType, payload) {
1349 var toSend = {
Simon Huntbb282f52014-11-10 11:08:19 -08001350 event: evType,
1351 sid: ++sid,
1352 payload: payload
1353 },
1354 asText = JSON.stringify(toSend);
1355 wsTraceTx(asText);
1356 webSock.send(asText);
1357 }
1358
1359 function wsTraceTx(msg) {
1360 wsTrace('tx', msg);
1361 }
1362 function wsTraceRx(msg) {
1363 wsTrace('rx', msg);
1364 }
1365 function wsTrace(rxtx, msg) {
Simon Huntbb282f52014-11-10 11:08:19 -08001366 console.log('[' + rxtx + '] ' + msg);
1367 // TODO: integrate with trace view
1368 //if (trace) {
1369 // trace.output(rxtx, msg);
1370 //}
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001371 }
1372
1373
1374 // ==============================
1375 // Selection stuff
1376
1377 function selectObject(obj, el) {
1378 var n,
Simon Hunt01095ff2014-11-13 16:37:29 -08001379 srcEv = d3.event.sourceEvent,
1380 meta = srcEv.metaKey,
1381 shift = srcEv.shiftKey;
1382
Simon Huntdeab4322014-11-13 18:49:07 -08001383 if ((panZoom() && !meta) || (!panZoom() && meta)) {
Simon Hunt01095ff2014-11-13 16:37:29 -08001384 return;
1385 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001386
1387 if (el) {
1388 n = d3.select(el);
1389 } else {
1390 node.each(function(d) {
1391 if (d == obj) {
1392 n = d3.select(el = this);
1393 }
1394 });
1395 }
1396 if (!n) return;
1397
Simon Hunt01095ff2014-11-13 16:37:29 -08001398 if (shift && n.classed('selected')) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001399 deselectObject(obj.id);
Simon Hunt61d04042014-11-11 17:27:16 -08001400 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001401 return;
1402 }
1403
Simon Hunt01095ff2014-11-13 16:37:29 -08001404 if (!shift) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001405 deselectAll();
1406 }
1407
Simon Huntc31d5692014-11-12 13:27:18 -08001408 selections[obj.id] = { obj: obj, el: el };
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001409 selectOrder.push(obj.id);
1410
1411 n.classed('selected', true);
Simon Hunt61d04042014-11-11 17:27:16 -08001412 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001413 }
1414
1415 function deselectObject(id) {
Simon Huntc31d5692014-11-12 13:27:18 -08001416 var obj = selections[id],
1417 idx;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001418 if (obj) {
1419 d3.select(obj.el).classed('selected', false);
Simon Hunt61d04042014-11-11 17:27:16 -08001420 delete selections[id];
Simon Huntc31d5692014-11-12 13:27:18 -08001421 idx = $.inArray(id, selectOrder);
1422 if (idx >= 0) {
1423 selectOrder.splice(idx, 1);
1424 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001425 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001426 }
1427
1428 function deselectAll() {
1429 // deselect all nodes in the network...
1430 node.classed('selected', false);
1431 selections = {};
1432 selectOrder = [];
Simon Hunt61d04042014-11-11 17:27:16 -08001433 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001434 }
1435
Simon Hunt61d04042014-11-11 17:27:16 -08001436 // update the state of the detail pane, based on current selections
1437 function updateDetailPane() {
1438 var nSel = selectOrder.length;
1439 if (!nSel) {
1440 detailPane.hide();
Simon Huntd72bc702014-11-13 18:38:04 -08001441 showTrafficAction(); // sends cancelTraffic event
Simon Hunt61d04042014-11-11 17:27:16 -08001442 } else if (nSel === 1) {
1443 singleSelect();
1444 } else {
1445 multiSelect();
1446 }
1447 }
1448
1449 function singleSelect() {
1450 requestDetails();
Simon Huntb53e0682014-11-12 13:32:01 -08001451 // NOTE: detail pane will be shown from showDetails event callback
Simon Hunt61d04042014-11-11 17:27:16 -08001452 }
1453
1454 function multiSelect() {
Simon Huntb53e0682014-11-12 13:32:01 -08001455 populateMultiSelect();
Simon Huntb53e0682014-11-12 13:32:01 -08001456 }
1457
1458 function addSep(tbody) {
1459 var tr = tbody.append('tr');
1460 $('<hr>').appendTo(tr.append('td').attr('colspan', 2));
1461 }
1462
1463 function addProp(tbody, label, value) {
1464 var tr = tbody.append('tr');
1465
1466 tr.append('td')
1467 .attr('class', 'label')
1468 .text(label + ' :');
1469
1470 tr.append('td')
1471 .attr('class', 'value')
1472 .text(value);
1473 }
1474
1475 function populateMultiSelect() {
1476 detailPane.empty();
1477
1478 var title = detailPane.append("h2"),
1479 table = detailPane.append("table"),
1480 tbody = table.append("tbody");
1481
1482 title.text('Multi-Select...');
1483
1484 selectOrder.forEach(function (d, i) {
1485 addProp(tbody, i+1, d);
1486 });
Simon Huntd72bc702014-11-13 18:38:04 -08001487
1488 addMultiSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08001489 }
1490
1491 function populateDetails(data) {
1492 detailPane.empty();
1493
1494 var title = detailPane.append("h2"),
1495 table = detailPane.append("table"),
1496 tbody = table.append("tbody");
1497
1498 $('<img src="img/' + data.type + '.png">').appendTo(title);
1499 $('<span>').attr('class', 'icon').text(data.id).appendTo(title);
1500
1501 data.propOrder.forEach(function(p) {
1502 if (p === '-') {
1503 addSep(tbody);
1504 } else {
1505 addProp(tbody, p, data.props[p]);
1506 }
1507 });
Simon Huntd72bc702014-11-13 18:38:04 -08001508
1509 addSingleSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08001510 }
1511
Simon Huntd72bc702014-11-13 18:38:04 -08001512 function addSingleSelectActions() {
1513 detailPane.append('hr');
1514 // always want to allow 'show traffic'
Simon Hunta5e89142014-11-14 07:00:33 -08001515 addAction(detailPane, 'Show Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001516 }
1517
1518 function addMultiSelectActions() {
1519 detailPane.append('hr');
1520 // always want to allow 'show traffic'
Simon Hunta5e89142014-11-14 07:00:33 -08001521 addAction(detailPane, 'Show Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001522 // if exactly two hosts are selected, also want 'add host intent'
1523 if (nSel() === 2 && allSelectionsClass('host')) {
Simon Hunta5e89142014-11-14 07:00:33 -08001524 addAction(detailPane, 'Add Host Intent', addIntentAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001525 }
1526 }
1527
Simon Hunta5e89142014-11-14 07:00:33 -08001528 function addAction(panel, text, cb) {
1529 panel.append('div')
Simon Huntd72bc702014-11-13 18:38:04 -08001530 .classed('actionBtn', true)
1531 .text(text)
1532 .on('click', cb);
1533 }
1534
1535
Paul Greysonfcba0e82014-11-13 10:21:16 -08001536 function zoomPan(scale, translate) {
1537 zoomPanContainer.attr("transform", "translate(" + translate + ")scale(" + scale + ")");
1538 // keep the map lines constant width while zooming
1539 bgImg.style("stroke-width", 2.0 / scale + "px");
1540 }
1541
1542 function resetZoomPan() {
1543 zoomPan(1, [0,0]);
1544 zoom.scale(1).translate([0,0]);
1545 }
1546
1547 function setupZoomPan() {
1548 function zoomed() {
Simon Huntdeab4322014-11-13 18:49:07 -08001549 if (!panZoom() ^ !d3.event.sourceEvent.metaKey) {
Paul Greysonfcba0e82014-11-13 10:21:16 -08001550 zoomPan(d3.event.scale, d3.event.translate);
1551 }
1552 }
1553
1554 zoom = d3.behavior.zoom()
1555 .translate([0, 0])
1556 .scale(1)
1557 .scaleExtent([1, 8])
1558 .on("zoom", zoomed);
1559
1560 svg.call(zoom);
1561 }
1562
Simon Hunt61d04042014-11-11 17:27:16 -08001563 // ==============================
1564 // Test harness code
Simon Hunt56d51852014-11-09 13:03:35 -08001565
1566 function prepareScenario(view, ctx, dbg) {
1567 var sc = scenario,
1568 urlSc = sc.evDir + ctx + sc.evScenario;
1569
1570 if (!ctx) {
1571 view.alert("No scenario specified (null ctx)");
1572 return;
1573 }
1574
1575 sc.view = view;
1576 sc.ctx = ctx;
1577 sc.debug = dbg;
1578 sc.evNumber = 0;
1579
1580 d3.json(urlSc, function(err, data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001581 var p = data && data.params || {},
1582 desc = data && data.description || null,
Simon Huntfc274c92014-11-11 11:05:46 -08001583 intro = data && data.title;
Simon Huntbb282f52014-11-10 11:08:19 -08001584
Simon Hunt56d51852014-11-09 13:03:35 -08001585 if (err) {
1586 view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err);
1587 } else {
1588 sc.params = p;
Simon Huntbb282f52014-11-10 11:08:19 -08001589 if (desc) {
1590 intro += '\n\n ' + desc.join('\n ');
1591 }
1592 view.alert(intro);
Simon Hunt56d51852014-11-09 13:03:35 -08001593 }
1594 });
1595
1596 }
1597
Simon Hunt01095ff2014-11-13 16:37:29 -08001598 // ==============================
1599 // Toggle Buttons in masthead
Simon Hunt0c6d4192014-11-12 12:07:10 -08001600
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001601 // TODO: toggle button (and other widgets in the masthead) should be provided
1602 // by the framework; not generated by the view.
1603
Simon Hunta5e89142014-11-14 07:00:33 -08001604 var showInstances,
1605 doPanZoom,
1606 showTrafficOnHover;
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001607
1608 function addButtonBar(view) {
1609 var bb = d3.select('#mast')
1610 .append('span').classed('right', true).attr('id', 'bb');
1611
Simon Hunta5e89142014-11-14 07:00:33 -08001612 function mkTogBtn(text, cb) {
1613 return bb.append('span')
1614 .classed('btn', true)
1615 .text(text)
1616 .on('click', cb);
1617 }
Simon Hunt01095ff2014-11-13 16:37:29 -08001618
Simon Hunta5e89142014-11-14 07:00:33 -08001619 showInstances = mkTogBtn('Show Instances', toggleInst);
1620 doPanZoom = mkTogBtn('Pan/Zoom', togglePanZoom);
1621 showTrafficOnHover = mkTogBtn('Show traffic on hover', toggleTrafficHover);
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001622 }
1623
Simon Hunta5e89142014-11-14 07:00:33 -08001624 function instShown() {
1625 return showInstances.classed('active');
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001626 }
Simon Hunta5e89142014-11-14 07:00:33 -08001627 function toggleInst() {
1628 showInstances.classed('active', !instShown());
1629 if (instShown()) {
1630 oiBox.show();
1631 } else {
1632 oiBox.hide();
1633 }
Simon Hunt01095ff2014-11-13 16:37:29 -08001634 }
1635
Simon Huntdeab4322014-11-13 18:49:07 -08001636 function panZoom() {
1637 return doPanZoom.classed('active');
Simon Hunt01095ff2014-11-13 16:37:29 -08001638 }
Simon Hunta5e89142014-11-14 07:00:33 -08001639 function togglePanZoom() {
1640 doPanZoom.classed('active', !panZoom());
1641 }
1642
1643 function trafficHover() {
1644 return showTrafficOnHover.classed('active');
1645 }
1646 function toggleTrafficHover() {
1647 showTrafficOnHover.classed('active', !trafficHover());
1648 }
1649
Simon Hunt01095ff2014-11-13 16:37:29 -08001650
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001651 // ==============================
Simon Hunt142d0032014-11-04 20:13:09 -08001652 // View life-cycle callbacks
Simon Hunt195cb382014-11-03 17:50:51 -08001653
Simon Huntf67722a2014-11-10 09:32:06 -08001654 function preload(view, ctx, flags) {
Simon Hunt142d0032014-11-04 20:13:09 -08001655 var w = view.width(),
1656 h = view.height(),
Simon Huntc7ee0662014-11-05 16:44:37 -08001657 fcfg = config.force,
1658 fpad = fcfg.pad,
1659 forceDim = [w - 2*fpad, h - 2*fpad];
Simon Hunt195cb382014-11-03 17:50:51 -08001660
Simon Hunt142d0032014-11-04 20:13:09 -08001661 // NOTE: view.$div is a D3 selection of the view's div
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001662 var viewBox = '0 0 ' + config.logicalSize + ' ' + config.logicalSize;
1663 svg = view.$div.append('svg').attr('viewBox', viewBox);
Simon Hunt934c3ce2014-11-05 11:45:07 -08001664 setSize(svg, view);
1665
Paul Greysonfcba0e82014-11-13 10:21:16 -08001666 zoomPanContainer = svg.append('g').attr('id', 'zoomPanContainer');
Paul Greysonfcba0e82014-11-13 10:21:16 -08001667 setupZoomPan();
1668
Simon Hunt1a9eff92014-11-07 11:06:34 -08001669 // add blue glow filter to svg layer
Paul Greysonfcba0e82014-11-13 10:21:16 -08001670 d3u.appendGlow(zoomPanContainer);
Simon Hunt1a9eff92014-11-07 11:06:34 -08001671
Simon Huntc7ee0662014-11-05 16:44:37 -08001672 // group for the topology
Paul Greysonfcba0e82014-11-13 10:21:16 -08001673 topoG = zoomPanContainer.append('g')
Simon Huntd3b7d512014-11-12 15:48:41 -08001674 .attr('id', 'topo-G')
Simon Huntc7ee0662014-11-05 16:44:37 -08001675 .attr('transform', fcfg.translate());
1676
1677 // subgroups for links and nodes
1678 linkG = topoG.append('g').attr('id', 'links');
1679 nodeG = topoG.append('g').attr('id', 'nodes');
1680
1681 // selection of nodes and links
1682 link = linkG.selectAll('.link');
1683 node = nodeG.selectAll('.node');
1684
Simon Hunt7cd48f32014-11-09 23:42:50 -08001685 function chrg(d) {
1686 return fcfg.charge[d.class] || -12000;
1687 }
Simon Hunt99c13842014-11-06 18:23:12 -08001688 function ldist(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08001689 return fcfg.linkDistance[d.type] || 50;
Simon Hunt99c13842014-11-06 18:23:12 -08001690 }
1691 function lstrg(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08001692 // 0.0 - 1.0
1693 return fcfg.linkStrength[d.type] || 1.0;
Simon Hunt99c13842014-11-06 18:23:12 -08001694 }
1695
Simon Hunt1a9eff92014-11-07 11:06:34 -08001696 function selectCb(d, self) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001697 selectObject(d, self);
Simon Hunt1a9eff92014-11-07 11:06:34 -08001698 }
1699
1700 function atDragEnd(d, self) {
Simon Hunt56d51852014-11-09 13:03:35 -08001701 // once we've finished moving, pin the node in position
1702 d.fixed = true;
1703 d3.select(self).classed('fixed', true);
1704 if (config.useLiveData) {
Simon Hunt902c9922014-11-11 11:59:31 -08001705 sendUpdateMeta(d);
Simon Hunta255a2c2014-11-13 22:29:35 -08001706 } else {
1707 console.log('Moving node ' + d.id + ' to [' + d.x + ',' + d.y + ']');
Simon Hunt1a9eff92014-11-07 11:06:34 -08001708 }
1709 }
1710
Simon Hunt902c9922014-11-11 11:59:31 -08001711 function sendUpdateMeta(d) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001712 sendMessage('updateMeta', {
1713 id: d.id,
1714 'class': d.class,
Simon Hunt902c9922014-11-11 11:59:31 -08001715 'memento': {
Simon Hunt01095ff2014-11-13 16:37:29 -08001716 x: d.x,
1717 y: d.y
Simon Hunt902c9922014-11-11 11:59:31 -08001718 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001719 });
1720 }
1721
Simon Huntc7ee0662014-11-05 16:44:37 -08001722 // set up the force layout
1723 network.force = d3.layout.force()
1724 .size(forceDim)
1725 .nodes(network.nodes)
1726 .links(network.links)
Simon Hunt7cd48f32014-11-09 23:42:50 -08001727 .gravity(0.4)
1728 .friction(0.7)
1729 .charge(chrg)
Simon Hunt99c13842014-11-06 18:23:12 -08001730 .linkDistance(ldist)
1731 .linkStrength(lstrg)
Simon Huntc7ee0662014-11-05 16:44:37 -08001732 .on('tick', tick);
Simon Hunt195cb382014-11-03 17:50:51 -08001733
Simon Hunt01095ff2014-11-13 16:37:29 -08001734 network.drag = d3u.createDragBehavior(network.force,
Simon Huntdeab4322014-11-13 18:49:07 -08001735 selectCb, atDragEnd, panZoom);
Simon Hunt0c6d4192014-11-12 12:07:10 -08001736
1737 // create mask layer for when we lose connection to server.
Simon Hunta5e89142014-11-14 07:00:33 -08001738 // TODO: this should be part of the framework
Simon Hunt0c6d4192014-11-12 12:07:10 -08001739 mask = view.$div.append('div').attr('id','topo-mask');
1740 para(mask, 'Oops!');
1741 para(mask, 'Web-socket connection to server closed...');
1742 para(mask, 'Try refreshing the page.');
Simon Hunt1a9eff92014-11-07 11:06:34 -08001743 }
Simon Hunt195cb382014-11-03 17:50:51 -08001744
Simon Hunt01095ff2014-11-13 16:37:29 -08001745 function para(sel, text) {
1746 sel.append('p').text(text);
1747 }
1748
1749
Simon Hunt56d51852014-11-09 13:03:35 -08001750 function load(view, ctx, flags) {
Simon Huntf67722a2014-11-10 09:32:06 -08001751 // resize, in case the window was resized while we were not loaded
1752 resize(view, ctx, flags);
1753
Simon Hunt99c13842014-11-06 18:23:12 -08001754 // cache the view token, so network topo functions can access it
1755 network.view = view;
Simon Hunt56d51852014-11-09 13:03:35 -08001756 config.useLiveData = !flags.local;
1757
1758 if (!config.useLiveData) {
1759 prepareScenario(view, ctx, flags.debug);
1760 }
Simon Hunt99c13842014-11-06 18:23:12 -08001761
1762 // set our radio buttons and key bindings
Simon Hunt9462e8c2014-11-14 17:28:09 -08001763 layerBtnSet = view.setRadio(layerButtons);
Simon Hunt934c3ce2014-11-05 11:45:07 -08001764 view.setKeys(keyDispatch);
Simon Hunt195cb382014-11-03 17:50:51 -08001765
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001766 // patch in our "button bar" for now
1767 // TODO: implement a more official frameworky way of doing this..
1768 addButtonBar(view);
1769
Simon Huntd3b7d512014-11-12 15:48:41 -08001770 // Load map data asynchronously; complete startup after that..
1771 loadGeoJsonData();
Simon Hunta255a2c2014-11-13 22:29:35 -08001772
1773 // start the and timer
1774 var dashIdx = 0;
1775 antTimer = setInterval(function () {
1776 // TODO: figure out how to choose Src-->Dst and Dst-->Src, per link
1777 dashIdx = dashIdx === 0 ? 14 : dashIdx - 2;
1778 d3.selectAll('.animated').style('stroke-dashoffset', dashIdx);
1779 }, 35);
1780 }
1781
1782 function unload(view, ctx, flags) {
1783 if (antTimer) {
1784 clearInterval(antTimer);
1785 antTimer = null;
1786 }
Simon Huntd3b7d512014-11-12 15:48:41 -08001787 }
1788
1789 // TODO: move these to config/state portion of script
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001790 var geoJsonUrl = 'json/map/continental_us.json', // TODO: Paul
Simon Huntd3b7d512014-11-12 15:48:41 -08001791 geoJson;
1792
1793 function loadGeoJsonData() {
1794 d3.json(geoJsonUrl, function (err, data) {
1795 if (err) {
1796 // fall back to USA map background
1797 loadStaticMap();
1798 } else {
1799 geoJson = data;
1800 loadGeoMap();
1801 }
1802
1803 // finally, connect to the server...
1804 if (config.useLiveData) {
1805 webSock.connect();
1806 }
1807 });
1808 }
1809
1810 function showBg() {
1811 return config.options.showBackground ? 'visible' : 'hidden';
1812 }
1813
1814 function loadStaticMap() {
1815 fnTrace('loadStaticMap', config.backgroundUrl);
1816 var w = network.view.width(),
1817 h = network.view.height();
1818
1819 // load the background image
1820 bgImg = svg.insert('svg:image', '#topo-G')
1821 .attr({
1822 id: 'topo-bg',
1823 width: w,
1824 height: h,
1825 'xlink:href': config.backgroundUrl
1826 })
1827 .style({
1828 visibility: showBg()
1829 });
1830 }
1831
1832 function loadGeoMap() {
1833 fnTrace('loadGeoMap', geoJsonUrl);
Simon Huntd3b7d512014-11-12 15:48:41 -08001834
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001835 // extracts the topojson data into geocoordinate-based geometry
1836 var topoData = topojson.feature(geoJson, geoJson.objects.states);
Simon Huntd3b7d512014-11-12 15:48:41 -08001837
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001838 // see: http://bl.ocks.org/mbostock/4707858
1839 geoMapProjection = d3.geo.mercator();
1840 var path = d3.geo.path().projection(geoMapProjection);
Simon Huntd3b7d512014-11-12 15:48:41 -08001841
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001842 geoMapProjection
1843 .scale(1)
1844 .translate([0, 0]);
Simon Huntd3b7d512014-11-12 15:48:41 -08001845
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001846 // [[x1,y1],[x2,y2]]
1847 var b = path.bounds(topoData);
Paul Greysonfcba0e82014-11-13 10:21:16 -08001848 // size map to 95% of minimum dimension to fill space
1849 var s = .95 / Math.min((b[1][0] - b[0][0]) / config.logicalSize, (b[1][1] - b[0][1]) / config.logicalSize);
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001850 var t = [(config.logicalSize - s * (b[1][0] + b[0][0])) / 2, (config.logicalSize - s * (b[1][1] + b[0][1])) / 2];
Simon Huntd3b7d512014-11-12 15:48:41 -08001851
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001852 geoMapProjection
1853 .scale(s)
1854 .translate(t);
1855
Paul Greysonfcba0e82014-11-13 10:21:16 -08001856 bgImg = zoomPanContainer.insert("g", '#topo-G');
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001857 bgImg.attr('id', 'map').selectAll('path')
1858 .data(topoData.features)
1859 .enter()
1860 .append('path')
1861 .attr('d', path);
Simon Hunt195cb382014-11-03 17:50:51 -08001862 }
1863
Simon Huntf67722a2014-11-10 09:32:06 -08001864 function resize(view, ctx, flags) {
Simon Hunt934c3ce2014-11-05 11:45:07 -08001865 setSize(svg, view);
Simon Hunt142d0032014-11-04 20:13:09 -08001866 }
1867
1868
1869 // ==============================
1870 // View registration
Simon Hunt195cb382014-11-03 17:50:51 -08001871
Simon Hunt25248912014-11-04 11:25:48 -08001872 onos.ui.addView('topo', {
Simon Hunt142d0032014-11-04 20:13:09 -08001873 preload: preload,
1874 load: load,
Simon Hunta255a2c2014-11-13 22:29:35 -08001875 unload: unload,
Simon Hunt142d0032014-11-04 20:13:09 -08001876 resize: resize
Simon Hunt195cb382014-11-03 17:50:51 -08001877 });
1878
Simon Hunt61d04042014-11-11 17:27:16 -08001879 detailPane = onos.ui.addFloatingPanel('topo-detail');
Simon Hunta5e89142014-11-14 07:00:33 -08001880 oiBox = onos.ui.addFloatingPanel('topo-oibox', 'TL');
Simon Hunt61d04042014-11-11 17:27:16 -08001881
Simon Hunt195cb382014-11-03 17:50:51 -08001882}(ONOS));