blob: 3a5baccba9e8f032a438321a63083698f60edf40 [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 Hunt9462e8c2014-11-14 17:28:09 -0800699 .on('click', clickInst)
Simon Hunta5e89142014-11-14 07:00:33 -0800700 .text(function (d) { return d.id; });
701
702 // operate on existing + new onoses here
703
704 // the departed...
705 var exiting = onoses.exit()
706 .transition()
707 .style('opacity', 0)
708 .remove();
709 }
710
Simon Hunt9462e8c2014-11-14 17:28:09 -0800711 function clickInst(d) {
712 var el = d3.select(this),
713 aff = el.classed('affinity');
714 if (!aff) {
715 setAffinity(el, d);
716 } else {
717 cancelAffinity();
718 }
719 }
720
721 function setAffinity(el, d) {
722 d3.selectAll('.onosInst')
723 .classed('mastership', true)
724 .classed('affinity', false);
725 el.classed('affinity', true);
726
727 suppressLayers(true);
728 node.each(function (n) {
729 if (n.master === d.id) {
730 n.el.classed('suppressed', false);
731 }
732 });
733 oiShowMaster = true;
734 }
735
736 function cancelAffinity() {
737 d3.selectAll('.onosInst')
738 .classed('mastership affinity', false);
739 restoreLayerState();
740 oiShowMaster = false;
741 }
742
Simon Hunta5e89142014-11-14 07:00:33 -0800743 // ==============================
Simon Hunt99c13842014-11-06 18:23:12 -0800744 // force layout modification functions
745
746 function translate(x, y) {
747 return 'translate(' + x + ',' + y + ')';
748 }
749
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800750 function missMsg(what, id) {
751 return '\n[' + what + '] "' + id + '" missing ';
752 }
753
754 function linkEndPoints(srcId, dstId) {
755 var srcNode = network.lookup[srcId],
756 dstNode = network.lookup[dstId],
757 sMiss = !srcNode ? missMsg('src', srcId) : '',
758 dMiss = !dstNode ? missMsg('dst', dstId) : '';
759
760 if (sMiss || dMiss) {
761 logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
762 return null;
763 }
764 return {
765 source: srcNode,
766 target: dstNode,
767 x1: srcNode.x,
768 y1: srcNode.y,
769 x2: dstNode.x,
770 y2: dstNode.y
771 };
772 }
773
Simon Hunt56d51852014-11-09 13:03:35 -0800774 function createHostLink(host) {
775 var src = host.id,
776 dst = host.cp.device,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800777 id = host.ingress,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800778 lnk = linkEndPoints(src, dst);
Simon Hunt56d51852014-11-09 13:03:35 -0800779
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800780 if (!lnk) {
Simon Hunt56d51852014-11-09 13:03:35 -0800781 return null;
782 }
783
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800784 // Synthesize link ...
785 $.extend(lnk, {
Thomas Vachuska4830d392014-11-09 17:09:56 -0800786 id: id,
Simon Hunt56d51852014-11-09 13:03:35 -0800787 class: 'link',
Simon Hunt7cd48f32014-11-09 23:42:50 -0800788 type: 'hostLink',
Simon Hunt56d51852014-11-09 13:03:35 -0800789 svgClass: 'link hostLink',
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800790 linkWidth: 1
Simon Hunt7cd48f32014-11-09 23:42:50 -0800791 });
Simon Hunt99c13842014-11-06 18:23:12 -0800792 return lnk;
793 }
794
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800795 function createLink(link) {
796 var lnk = linkEndPoints(link.src, link.dst),
797 type = link.type;
798
799 if (!lnk) {
800 return null;
801 }
802
803 // merge in remaining data
804 $.extend(lnk, link, {
805 class: 'link',
806 svgClass: type ? 'link ' + type : 'link'
807 });
808 return lnk;
Simon Hunt1a9eff92014-11-07 11:06:34 -0800809 }
810
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800811 var widthRatio = 1.4,
812 linkScale = d3.scale.linear()
813 .domain([1, 12])
814 .range([widthRatio, 12 * widthRatio])
815 .clamp(true);
816
817 function updateLinkWidth (d) {
818 // TODO: watch out for .showPath/.showTraffic classes
819 d.el.transition()
820 .duration(1000)
821 .attr('stroke-width', linkScale(d.linkWidth));
822 }
823
824
Simon Hunt99c13842014-11-06 18:23:12 -0800825 function updateLinks() {
826 link = linkG.selectAll('.link')
827 .data(network.links, function (d) { return d.id; });
828
829 // operate on existing links, if necessary
830 // link .foo() .bar() ...
831
832 // operate on entering links:
833 var entering = link.enter()
834 .append('line')
835 .attr({
Simon Hunt99c13842014-11-06 18:23:12 -0800836 class: function (d) { return d.svgClass; },
837 x1: function (d) { return d.x1; },
838 y1: function (d) { return d.y1; },
839 x2: function (d) { return d.x2; },
840 y2: function (d) { return d.y2; },
Simon Hunt1a9eff92014-11-07 11:06:34 -0800841 stroke: config.topo.linkInColor,
842 'stroke-width': config.topo.linkInWidth
Simon Hunt99c13842014-11-06 18:23:12 -0800843 })
844 .transition().duration(1000)
845 .attr({
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800846 'stroke-width': function (d) { return linkScale(d.linkWidth); },
Simon Hunt99c13842014-11-06 18:23:12 -0800847 stroke: '#666' // TODO: remove explicit stroke, rather...
848 });
849
850 // augment links
Simon Hunt7cd48f32014-11-09 23:42:50 -0800851 entering.each(function (d) {
852 var link = d3.select(this);
853 // provide ref to element selection from backing data....
854 d.el = link;
Simon Hunt99c13842014-11-06 18:23:12 -0800855
Simon Hunt7cd48f32014-11-09 23:42:50 -0800856 // TODO: add src/dst port labels etc.
857 });
Thomas Vachuska4830d392014-11-09 17:09:56 -0800858
859 // operate on both existing and new links, if necessary
860 //link .foo() .bar() ...
861
862 // operate on exiting links:
Thomas Vachuska4830d392014-11-09 17:09:56 -0800863 link.exit()
Thomas Vachuska4830d392014-11-09 17:09:56 -0800864 .attr({
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800865 'stroke-dasharray': '3, 3'
Thomas Vachuska4830d392014-11-09 17:09:56 -0800866 })
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800867 .style('opacity', 0.4)
868 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -0800869 .duration(1500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800870 .attr({
871 'stroke-dasharray': '3, 12'
872 })
873 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -0800874 .duration(500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800875 .style('opacity', 0.0)
Thomas Vachuska4830d392014-11-09 17:09:56 -0800876 .remove();
Simon Hunt99c13842014-11-06 18:23:12 -0800877 }
878
879 function createDeviceNode(device) {
880 // start with the object as is
881 var node = device,
Simon Huntbb282f52014-11-10 11:08:19 -0800882 type = device.type,
883 svgCls = type ? 'node device ' + type : 'node device';
Simon Hunt99c13842014-11-06 18:23:12 -0800884
885 // Augment as needed...
886 node.class = 'device';
Simon Huntbb282f52014-11-10 11:08:19 -0800887 node.svgClass = device.online ? svgCls + ' online' : svgCls;
Simon Hunt99c13842014-11-06 18:23:12 -0800888 positionNode(node);
889
890 // cache label array length
891 network.deviceLabelCount = device.labels.length;
Simon Hunt99c13842014-11-06 18:23:12 -0800892 return node;
893 }
894
Simon Hunt56d51852014-11-09 13:03:35 -0800895 function createHostNode(host) {
896 // start with the object as is
897 var node = host;
898
899 // Augment as needed...
900 node.class = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -0800901 if (!node.type) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800902 node.type = 'endstation';
903 }
Simon Hunt56d51852014-11-09 13:03:35 -0800904 node.svgClass = 'node host';
Simon Hunt56d51852014-11-09 13:03:35 -0800905 positionNode(node);
906
907 // cache label array length
908 network.hostLabelCount = host.labels.length;
Simon Hunt56d51852014-11-09 13:03:35 -0800909 return node;
910 }
911
Simon Hunt99c13842014-11-06 18:23:12 -0800912 function positionNode(node) {
913 var meta = node.metaUi,
Simon Huntac9e24f2014-11-12 10:12:21 -0800914 x = meta && meta.x,
915 y = meta && meta.y,
916 xy;
Simon Hunt99c13842014-11-06 18:23:12 -0800917
Simon Huntac9e24f2014-11-12 10:12:21 -0800918 // If we have [x,y] already, use that...
Simon Hunt99c13842014-11-06 18:23:12 -0800919 if (x && y) {
920 node.fixed = true;
Simon Huntac9e24f2014-11-12 10:12:21 -0800921 node.x = x;
922 node.y = y;
923 return;
Simon Hunt99c13842014-11-06 18:23:12 -0800924 }
Simon Huntac9e24f2014-11-12 10:12:21 -0800925
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800926 var location = node.location;
927 if (location && location.type === 'latlng') {
928 var coord = geoMapProjection([location.lng, location.lat]);
929 node.fixed = true;
930 node.x = coord[0];
931 node.y = coord[1];
932 return;
933 }
934
Simon Huntac9e24f2014-11-12 10:12:21 -0800935 // Note: Placing incoming unpinned nodes at exactly the same point
936 // (center of the view) causes them to explode outwards when
937 // the force layout kicks in. So, we spread them out a bit
938 // initially, to provide a more serene layout convergence.
939 // Additionally, if the node is a host, we place it near
940 // the device it is connected to.
941
942 function spread(s) {
943 return Math.floor((Math.random() * s) - s/2);
944 }
945
946 function randDim(dim) {
947 return dim / 2 + spread(dim * 0.7071);
948 }
949
950 function rand() {
951 return {
952 x: randDim(network.view.width()),
953 y: randDim(network.view.height())
954 };
955 }
956
957 function near(node) {
958 var min = 12,
959 dx = spread(12),
960 dy = spread(12);
961 return {
962 x: node.x + min + dx,
963 y: node.y + min + dy
964 };
965 }
966
967 function getDevice(cp) {
968 var d = network.lookup[cp.device];
969 return d || rand();
970 }
971
972 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
973 $.extend(node, xy);
Simon Hunt99c13842014-11-06 18:23:12 -0800974 }
975
Simon Hunt99c13842014-11-06 18:23:12 -0800976 function iconUrl(d) {
977 return 'img/' + d.type + '.png';
978 }
979
980 // returns the newly computed bounding box of the rectangle
981 function adjustRectToFitText(n) {
982 var text = n.select('text'),
983 box = text.node().getBBox(),
984 lab = config.labels;
985
986 text.attr('text-anchor', 'middle')
987 .attr('y', '-0.8em')
988 .attr('x', lab.imgPad/2);
989
990 // translate the bbox so that it is centered on [x,y]
991 box.x = -box.width / 2;
992 box.y = -box.height / 2;
993
994 // add padding
995 box.x -= (lab.padLR + lab.imgPad/2);
996 box.width += lab.padLR * 2 + lab.imgPad;
997 box.y -= lab.padTB;
998 box.height += lab.padTB * 2;
999
1000 return box;
1001 }
1002
Simon Hunt1a9eff92014-11-07 11:06:34 -08001003 function mkSvgClass(d) {
1004 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
1005 }
1006
Simon Hunt7cd48f32014-11-09 23:42:50 -08001007 function hostLabel(d) {
1008 var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0;
1009 return d.labels[idx];
1010 }
1011 function deviceLabel(d) {
1012 var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0;
1013 return d.labels[idx];
1014 }
1015 function niceLabel(label) {
1016 return (label && label.trim()) ? label : '.';
1017 }
1018
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001019 function updateDeviceLabel(d) {
1020 var label = niceLabel(deviceLabel(d)),
1021 node = d.el,
1022 box;
1023
1024 node.select('text')
1025 .text(label)
1026 .style('opacity', 0)
1027 .transition()
1028 .style('opacity', 1);
1029
1030 box = adjustRectToFitText(node);
1031
1032 node.select('rect')
1033 .transition()
1034 .attr(box);
1035
1036 node.select('image')
1037 .transition()
1038 .attr('x', box.x + config.icons.xoff)
1039 .attr('y', box.y + config.icons.yoff);
1040 }
1041
1042 function updateHostLabel(d) {
1043 var label = hostLabel(d),
1044 host = d.el;
1045
1046 host.select('text').text(label);
1047 }
1048
Simon Huntbb282f52014-11-10 11:08:19 -08001049 function updateDeviceState(nodeData) {
1050 nodeData.el.classed('online', nodeData.online);
1051 updateDeviceLabel(nodeData);
1052 // TODO: review what else might need to be updated
1053 }
1054
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001055 function updateLinkState(linkData) {
1056 updateLinkWidth(linkData);
1057 // TODO: review what else might need to be updated
1058 // update label, if showing
1059 }
1060
Simon Huntbb282f52014-11-10 11:08:19 -08001061 function updateHostState(hostData) {
1062 updateHostLabel(hostData);
1063 // TODO: review what else might need to be updated
1064 }
1065
Simon Hunt6ac93f32014-11-13 12:17:27 -08001066 function nodeMouseOver(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001067 hovered = d;
Simon Huntd72bc702014-11-13 18:38:04 -08001068 if (trafficHover() && d.class === 'host') {
1069 showTrafficAction();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001070 }
1071 }
1072
1073 function nodeMouseOut(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001074 hovered = null;
Simon Huntd72bc702014-11-13 18:38:04 -08001075 if (trafficHover() && d.class === 'host') {
1076 showTrafficAction();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001077 }
1078 }
Simon Huntbb282f52014-11-10 11:08:19 -08001079
Simon Hunt99c13842014-11-06 18:23:12 -08001080 function updateNodes() {
1081 node = nodeG.selectAll('.node')
1082 .data(network.nodes, function (d) { return d.id; });
1083
1084 // operate on existing nodes, if necessary
Simon Hunt7cd48f32014-11-09 23:42:50 -08001085 // update host labels
Simon Hunt99c13842014-11-06 18:23:12 -08001086 //node .foo() .bar() ...
1087
1088 // operate on entering nodes:
1089 var entering = node.enter()
1090 .append('g')
1091 .attr({
1092 id: function (d) { return safeId(d.id); },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001093 class: mkSvgClass,
Simon Hunt99c13842014-11-06 18:23:12 -08001094 transform: function (d) { return translate(d.x, d.y); },
1095 opacity: 0
1096 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08001097 .call(network.drag)
Simon Hunt6ac93f32014-11-13 12:17:27 -08001098 .on('mouseover', nodeMouseOver)
1099 .on('mouseout', nodeMouseOut)
Simon Hunt99c13842014-11-06 18:23:12 -08001100 .transition()
1101 .attr('opacity', 1);
1102
1103 // augment device nodes...
1104 entering.filter('.device').each(function (d) {
1105 var node = d3.select(this),
1106 icon = iconUrl(d),
Simon Hunt7cd48f32014-11-09 23:42:50 -08001107 label = niceLabel(deviceLabel(d)),
Simon Hunt99c13842014-11-06 18:23:12 -08001108 box;
1109
Simon Hunt7cd48f32014-11-09 23:42:50 -08001110 // provide ref to element from backing data....
1111 d.el = node;
1112
Simon Hunt99c13842014-11-06 18:23:12 -08001113 node.append('rect')
1114 .attr({
1115 'rx': 5,
1116 'ry': 5
1117 });
1118
1119 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001120 .text(label)
Simon Hunt99c13842014-11-06 18:23:12 -08001121 .attr('dy', '1.1em');
1122
1123 box = adjustRectToFitText(node);
1124
1125 node.select('rect')
1126 .attr(box);
1127
1128 if (icon) {
1129 var cfg = config.icons;
1130 node.append('svg:image')
1131 .attr({
1132 x: box.x + config.icons.xoff,
1133 y: box.y + config.icons.yoff,
1134 width: cfg.w,
1135 height: cfg.h,
1136 'xlink:href': icon
1137 });
1138 }
1139
1140 // debug function to show the modelled x,y coordinates of nodes...
1141 if (debug('showNodeXY')) {
1142 node.select('rect').attr('fill-opacity', 0.5);
1143 node.append('circle')
1144 .attr({
1145 class: 'debug',
1146 cx: 0,
1147 cy: 0,
1148 r: '3px'
1149 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001150 }
1151 });
Simon Hunt934c3ce2014-11-05 11:45:07 -08001152
Simon Hunt56d51852014-11-09 13:03:35 -08001153 // augment host nodes...
1154 entering.filter('.host').each(function (d) {
1155 var node = d3.select(this),
Simon Hunt56d51852014-11-09 13:03:35 -08001156 box;
1157
Simon Hunt7cd48f32014-11-09 23:42:50 -08001158 // provide ref to element from backing data....
1159 d.el = node;
1160
Simon Hunt56d51852014-11-09 13:03:35 -08001161 node.append('circle')
1162 .attr('r', 8); // TODO: define host circle radius
1163
Simon Hunt56d51852014-11-09 13:03:35 -08001164 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001165 .text(hostLabel)
1166 .attr('dy', '1.3em')
1167 .attr('text-anchor', 'middle');
Simon Hunt56d51852014-11-09 13:03:35 -08001168
1169 // debug function to show the modelled x,y coordinates of nodes...
1170 if (debug('showNodeXY')) {
1171 node.select('circle').attr('fill-opacity', 0.5);
1172 node.append('circle')
1173 .attr({
1174 class: 'debug',
1175 cx: 0,
1176 cy: 0,
1177 r: '3px'
1178 });
1179 }
1180 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001181
Simon Hunt99c13842014-11-06 18:23:12 -08001182 // operate on both existing and new nodes, if necessary
1183 //node .foo() .bar() ...
Simon Huntc7ee0662014-11-05 16:44:37 -08001184
Simon Hunt99c13842014-11-06 18:23:12 -08001185 // operate on exiting nodes:
Simon Huntea80eb42014-11-11 13:46:57 -08001186 // Note that the node is removed after 2 seconds.
1187 // Sub element animations should be shorter than 2 seconds.
1188 var exiting = node.exit()
Simon Hunt44031102014-11-11 13:20:36 -08001189 .transition()
1190 .duration(2000)
Simon Huntea80eb42014-11-11 13:46:57 -08001191 .style('opacity', 0)
Simon Hunt99c13842014-11-06 18:23:12 -08001192 .remove();
Simon Huntea80eb42014-11-11 13:46:57 -08001193
1194 // host node exits....
1195 exiting.filter('.host').each(function (d) {
1196 var node = d3.select(this);
1197
1198 node.select('text')
1199 .style('opacity', 0.5)
1200 .transition()
1201 .duration(1000)
1202 .style('opacity', 0);
1203 // note, leave <g>.remove to remove this element
1204
1205 node.select('circle')
1206 .style('stroke-fill', '#555')
1207 .style('fill', '#888')
1208 .style('opacity', 0.5)
1209 .transition()
1210 .duration(1500)
1211 .attr('r', 0);
1212 // note, leave <g>.remove to remove this element
1213
1214 });
1215
1216 // TODO: device node exits
Simon Huntc7ee0662014-11-05 16:44:37 -08001217 }
1218
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001219 function find(id, array) {
1220 for (var idx = 0, n = array.length; idx < n; idx++) {
1221 if (array[idx].id === id) {
1222 return idx;
1223 }
1224 }
1225 return -1;
1226 }
1227
1228 function removeLinkElement(linkData) {
1229 // remove from lookup cache
1230 delete network.lookup[linkData.id];
1231 // remove from links array
1232 var idx = find(linkData.id, network.links);
Simon Huntfc274c92014-11-11 11:05:46 -08001233 network.links.splice(idx, 1);
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001234 // remove from SVG
1235 updateLinks();
Simon Hunt44031102014-11-11 13:20:36 -08001236 network.force.resume();
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001237 }
Simon Huntc7ee0662014-11-05 16:44:37 -08001238
Simon Hunt44031102014-11-11 13:20:36 -08001239 function removeHostElement(hostData) {
1240 // first, remove associated hostLink...
1241 removeLinkElement(hostData.linkData);
1242
1243 // remove from lookup cache
1244 delete network.lookup[hostData.id];
1245 // remove from nodes array
1246 var idx = find(hostData.id, network.nodes);
1247 network.nodes.splice(idx, 1);
1248 // remove from SVG
1249 updateNodes();
1250 network.force.resume();
1251 }
1252
1253
Simon Huntc7ee0662014-11-05 16:44:37 -08001254 function tick() {
1255 node.attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001256 transform: function (d) { return translate(d.x, d.y); }
Simon Huntc7ee0662014-11-05 16:44:37 -08001257 });
1258
1259 link.attr({
1260 x1: function (d) { return d.source.x; },
1261 y1: function (d) { return d.source.y; },
1262 x2: function (d) { return d.target.x; },
1263 y2: function (d) { return d.target.y; }
1264 });
1265 }
Simon Hunt934c3ce2014-11-05 11:45:07 -08001266
1267 // ==============================
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001268 // Web-Socket for live data
1269
1270 function webSockUrl() {
1271 return document.location.toString()
1272 .replace(/\#.*/, '')
1273 .replace('http://', 'ws://')
1274 .replace('https://', 'wss://')
1275 .replace('index2.html', config.webSockUrl);
1276 }
1277
1278 webSock = {
1279 ws : null,
1280
1281 connect : function() {
1282 webSock.ws = new WebSocket(webSockUrl());
1283
1284 webSock.ws.onopen = function() {
Simon Hunt0c6d4192014-11-12 12:07:10 -08001285 noWebSock(false);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001286 };
1287
1288 webSock.ws.onmessage = function(m) {
1289 if (m.data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001290 wsTraceRx(m.data);
Thomas Vachuskad472c6e2014-11-07 19:11:05 -08001291 handleServerEvent(JSON.parse(m.data));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001292 }
1293 };
1294
1295 webSock.ws.onclose = function(m) {
1296 webSock.ws = null;
Simon Hunt0c6d4192014-11-12 12:07:10 -08001297 noWebSock(true);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001298 };
1299 },
1300
1301 send : function(text) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001302 if (text != null) {
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001303 webSock._send(text);
1304 }
1305 },
1306
1307 _send : function(message) {
1308 if (webSock.ws) {
1309 webSock.ws.send(message);
Simon Hunta255a2c2014-11-13 22:29:35 -08001310 } else if (config.useLiveData) {
Simon Hunt56d51852014-11-09 13:03:35 -08001311 network.view.alert('no web socket open\n\n' + message);
Simon Hunta255a2c2014-11-13 22:29:35 -08001312 } else {
1313 console.log('WS Send: ' + JSON.stringify(message));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001314 }
1315 }
1316
1317 };
1318
Simon Hunt0c6d4192014-11-12 12:07:10 -08001319 function noWebSock(b) {
1320 mask.style('display',b ? 'block' : 'none');
1321 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001322
Simon Hunt61d04042014-11-11 17:27:16 -08001323 // TODO: use cache of pending messages (key = sid) to reconcile responses
1324
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001325 function sendMessage(evType, payload) {
1326 var toSend = {
Simon Huntbb282f52014-11-10 11:08:19 -08001327 event: evType,
1328 sid: ++sid,
1329 payload: payload
1330 },
1331 asText = JSON.stringify(toSend);
1332 wsTraceTx(asText);
1333 webSock.send(asText);
1334 }
1335
1336 function wsTraceTx(msg) {
1337 wsTrace('tx', msg);
1338 }
1339 function wsTraceRx(msg) {
1340 wsTrace('rx', msg);
1341 }
1342 function wsTrace(rxtx, msg) {
Simon Huntbb282f52014-11-10 11:08:19 -08001343 console.log('[' + rxtx + '] ' + msg);
1344 // TODO: integrate with trace view
1345 //if (trace) {
1346 // trace.output(rxtx, msg);
1347 //}
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001348 }
1349
1350
1351 // ==============================
1352 // Selection stuff
1353
1354 function selectObject(obj, el) {
1355 var n,
Simon Hunt01095ff2014-11-13 16:37:29 -08001356 srcEv = d3.event.sourceEvent,
1357 meta = srcEv.metaKey,
1358 shift = srcEv.shiftKey;
1359
Simon Huntdeab4322014-11-13 18:49:07 -08001360 if ((panZoom() && !meta) || (!panZoom() && meta)) {
Simon Hunt01095ff2014-11-13 16:37:29 -08001361 return;
1362 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001363
1364 if (el) {
1365 n = d3.select(el);
1366 } else {
1367 node.each(function(d) {
1368 if (d == obj) {
1369 n = d3.select(el = this);
1370 }
1371 });
1372 }
1373 if (!n) return;
1374
Simon Hunt01095ff2014-11-13 16:37:29 -08001375 if (shift && n.classed('selected')) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001376 deselectObject(obj.id);
Simon Hunt61d04042014-11-11 17:27:16 -08001377 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001378 return;
1379 }
1380
Simon Hunt01095ff2014-11-13 16:37:29 -08001381 if (!shift) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001382 deselectAll();
1383 }
1384
Simon Huntc31d5692014-11-12 13:27:18 -08001385 selections[obj.id] = { obj: obj, el: el };
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001386 selectOrder.push(obj.id);
1387
1388 n.classed('selected', true);
Simon Hunt61d04042014-11-11 17:27:16 -08001389 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001390 }
1391
1392 function deselectObject(id) {
Simon Huntc31d5692014-11-12 13:27:18 -08001393 var obj = selections[id],
1394 idx;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001395 if (obj) {
1396 d3.select(obj.el).classed('selected', false);
Simon Hunt61d04042014-11-11 17:27:16 -08001397 delete selections[id];
Simon Huntc31d5692014-11-12 13:27:18 -08001398 idx = $.inArray(id, selectOrder);
1399 if (idx >= 0) {
1400 selectOrder.splice(idx, 1);
1401 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001402 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001403 }
1404
1405 function deselectAll() {
1406 // deselect all nodes in the network...
1407 node.classed('selected', false);
1408 selections = {};
1409 selectOrder = [];
Simon Hunt61d04042014-11-11 17:27:16 -08001410 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001411 }
1412
Simon Hunt61d04042014-11-11 17:27:16 -08001413 // update the state of the detail pane, based on current selections
1414 function updateDetailPane() {
1415 var nSel = selectOrder.length;
1416 if (!nSel) {
1417 detailPane.hide();
Simon Huntd72bc702014-11-13 18:38:04 -08001418 showTrafficAction(); // sends cancelTraffic event
Simon Hunt61d04042014-11-11 17:27:16 -08001419 } else if (nSel === 1) {
1420 singleSelect();
1421 } else {
1422 multiSelect();
1423 }
1424 }
1425
1426 function singleSelect() {
1427 requestDetails();
Simon Huntb53e0682014-11-12 13:32:01 -08001428 // NOTE: detail pane will be shown from showDetails event callback
Simon Hunt61d04042014-11-11 17:27:16 -08001429 }
1430
1431 function multiSelect() {
Simon Huntb53e0682014-11-12 13:32:01 -08001432 populateMultiSelect();
Simon Huntb53e0682014-11-12 13:32:01 -08001433 }
1434
1435 function addSep(tbody) {
1436 var tr = tbody.append('tr');
1437 $('<hr>').appendTo(tr.append('td').attr('colspan', 2));
1438 }
1439
1440 function addProp(tbody, label, value) {
1441 var tr = tbody.append('tr');
1442
1443 tr.append('td')
1444 .attr('class', 'label')
1445 .text(label + ' :');
1446
1447 tr.append('td')
1448 .attr('class', 'value')
1449 .text(value);
1450 }
1451
1452 function populateMultiSelect() {
1453 detailPane.empty();
1454
1455 var title = detailPane.append("h2"),
1456 table = detailPane.append("table"),
1457 tbody = table.append("tbody");
1458
1459 title.text('Multi-Select...');
1460
1461 selectOrder.forEach(function (d, i) {
1462 addProp(tbody, i+1, d);
1463 });
Simon Huntd72bc702014-11-13 18:38:04 -08001464
1465 addMultiSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08001466 }
1467
1468 function populateDetails(data) {
1469 detailPane.empty();
1470
1471 var title = detailPane.append("h2"),
1472 table = detailPane.append("table"),
1473 tbody = table.append("tbody");
1474
1475 $('<img src="img/' + data.type + '.png">').appendTo(title);
1476 $('<span>').attr('class', 'icon').text(data.id).appendTo(title);
1477
1478 data.propOrder.forEach(function(p) {
1479 if (p === '-') {
1480 addSep(tbody);
1481 } else {
1482 addProp(tbody, p, data.props[p]);
1483 }
1484 });
Simon Huntd72bc702014-11-13 18:38:04 -08001485
1486 addSingleSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08001487 }
1488
Simon Huntd72bc702014-11-13 18:38:04 -08001489 function addSingleSelectActions() {
1490 detailPane.append('hr');
1491 // always want to allow 'show traffic'
Simon Hunta5e89142014-11-14 07:00:33 -08001492 addAction(detailPane, 'Show Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001493 }
1494
1495 function addMultiSelectActions() {
1496 detailPane.append('hr');
1497 // always want to allow 'show traffic'
Simon Hunta5e89142014-11-14 07:00:33 -08001498 addAction(detailPane, 'Show Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001499 // if exactly two hosts are selected, also want 'add host intent'
1500 if (nSel() === 2 && allSelectionsClass('host')) {
Simon Hunta5e89142014-11-14 07:00:33 -08001501 addAction(detailPane, 'Add Host Intent', addIntentAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001502 }
1503 }
1504
Simon Hunta5e89142014-11-14 07:00:33 -08001505 function addAction(panel, text, cb) {
1506 panel.append('div')
Simon Huntd72bc702014-11-13 18:38:04 -08001507 .classed('actionBtn', true)
1508 .text(text)
1509 .on('click', cb);
1510 }
1511
1512
Paul Greysonfcba0e82014-11-13 10:21:16 -08001513 function zoomPan(scale, translate) {
1514 zoomPanContainer.attr("transform", "translate(" + translate + ")scale(" + scale + ")");
1515 // keep the map lines constant width while zooming
1516 bgImg.style("stroke-width", 2.0 / scale + "px");
1517 }
1518
1519 function resetZoomPan() {
1520 zoomPan(1, [0,0]);
1521 zoom.scale(1).translate([0,0]);
1522 }
1523
1524 function setupZoomPan() {
1525 function zoomed() {
Simon Huntdeab4322014-11-13 18:49:07 -08001526 if (!panZoom() ^ !d3.event.sourceEvent.metaKey) {
Paul Greysonfcba0e82014-11-13 10:21:16 -08001527 zoomPan(d3.event.scale, d3.event.translate);
1528 }
1529 }
1530
1531 zoom = d3.behavior.zoom()
1532 .translate([0, 0])
1533 .scale(1)
1534 .scaleExtent([1, 8])
1535 .on("zoom", zoomed);
1536
1537 svg.call(zoom);
1538 }
1539
Simon Hunt61d04042014-11-11 17:27:16 -08001540 // ==============================
1541 // Test harness code
Simon Hunt56d51852014-11-09 13:03:35 -08001542
1543 function prepareScenario(view, ctx, dbg) {
1544 var sc = scenario,
1545 urlSc = sc.evDir + ctx + sc.evScenario;
1546
1547 if (!ctx) {
1548 view.alert("No scenario specified (null ctx)");
1549 return;
1550 }
1551
1552 sc.view = view;
1553 sc.ctx = ctx;
1554 sc.debug = dbg;
1555 sc.evNumber = 0;
1556
1557 d3.json(urlSc, function(err, data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001558 var p = data && data.params || {},
1559 desc = data && data.description || null,
Simon Huntfc274c92014-11-11 11:05:46 -08001560 intro = data && data.title;
Simon Huntbb282f52014-11-10 11:08:19 -08001561
Simon Hunt56d51852014-11-09 13:03:35 -08001562 if (err) {
1563 view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err);
1564 } else {
1565 sc.params = p;
Simon Huntbb282f52014-11-10 11:08:19 -08001566 if (desc) {
1567 intro += '\n\n ' + desc.join('\n ');
1568 }
1569 view.alert(intro);
Simon Hunt56d51852014-11-09 13:03:35 -08001570 }
1571 });
1572
1573 }
1574
Simon Hunt01095ff2014-11-13 16:37:29 -08001575 // ==============================
1576 // Toggle Buttons in masthead
Simon Hunt0c6d4192014-11-12 12:07:10 -08001577
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001578 // TODO: toggle button (and other widgets in the masthead) should be provided
1579 // by the framework; not generated by the view.
1580
Simon Hunta5e89142014-11-14 07:00:33 -08001581 var showInstances,
1582 doPanZoom,
1583 showTrafficOnHover;
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001584
1585 function addButtonBar(view) {
1586 var bb = d3.select('#mast')
1587 .append('span').classed('right', true).attr('id', 'bb');
1588
Simon Hunta5e89142014-11-14 07:00:33 -08001589 function mkTogBtn(text, cb) {
1590 return bb.append('span')
1591 .classed('btn', true)
1592 .text(text)
1593 .on('click', cb);
1594 }
Simon Hunt01095ff2014-11-13 16:37:29 -08001595
Simon Hunta5e89142014-11-14 07:00:33 -08001596 showInstances = mkTogBtn('Show Instances', toggleInst);
1597 doPanZoom = mkTogBtn('Pan/Zoom', togglePanZoom);
1598 showTrafficOnHover = mkTogBtn('Show traffic on hover', toggleTrafficHover);
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001599 }
1600
Simon Hunta5e89142014-11-14 07:00:33 -08001601 function instShown() {
1602 return showInstances.classed('active');
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001603 }
Simon Hunta5e89142014-11-14 07:00:33 -08001604 function toggleInst() {
1605 showInstances.classed('active', !instShown());
1606 if (instShown()) {
1607 oiBox.show();
1608 } else {
1609 oiBox.hide();
1610 }
Simon Hunt01095ff2014-11-13 16:37:29 -08001611 }
1612
Simon Huntdeab4322014-11-13 18:49:07 -08001613 function panZoom() {
1614 return doPanZoom.classed('active');
Simon Hunt01095ff2014-11-13 16:37:29 -08001615 }
Simon Hunta5e89142014-11-14 07:00:33 -08001616 function togglePanZoom() {
1617 doPanZoom.classed('active', !panZoom());
1618 }
1619
1620 function trafficHover() {
1621 return showTrafficOnHover.classed('active');
1622 }
1623 function toggleTrafficHover() {
1624 showTrafficOnHover.classed('active', !trafficHover());
1625 }
1626
Simon Hunt01095ff2014-11-13 16:37:29 -08001627
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001628 // ==============================
Simon Hunt142d0032014-11-04 20:13:09 -08001629 // View life-cycle callbacks
Simon Hunt195cb382014-11-03 17:50:51 -08001630
Simon Huntf67722a2014-11-10 09:32:06 -08001631 function preload(view, ctx, flags) {
Simon Hunt142d0032014-11-04 20:13:09 -08001632 var w = view.width(),
1633 h = view.height(),
Simon Huntc7ee0662014-11-05 16:44:37 -08001634 fcfg = config.force,
1635 fpad = fcfg.pad,
1636 forceDim = [w - 2*fpad, h - 2*fpad];
Simon Hunt195cb382014-11-03 17:50:51 -08001637
Simon Hunt142d0032014-11-04 20:13:09 -08001638 // NOTE: view.$div is a D3 selection of the view's div
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001639 var viewBox = '0 0 ' + config.logicalSize + ' ' + config.logicalSize;
1640 svg = view.$div.append('svg').attr('viewBox', viewBox);
Simon Hunt934c3ce2014-11-05 11:45:07 -08001641 setSize(svg, view);
1642
Paul Greysonfcba0e82014-11-13 10:21:16 -08001643 zoomPanContainer = svg.append('g').attr('id', 'zoomPanContainer');
Paul Greysonfcba0e82014-11-13 10:21:16 -08001644 setupZoomPan();
1645
Simon Hunt1a9eff92014-11-07 11:06:34 -08001646 // add blue glow filter to svg layer
Paul Greysonfcba0e82014-11-13 10:21:16 -08001647 d3u.appendGlow(zoomPanContainer);
Simon Hunt1a9eff92014-11-07 11:06:34 -08001648
Simon Huntc7ee0662014-11-05 16:44:37 -08001649 // group for the topology
Paul Greysonfcba0e82014-11-13 10:21:16 -08001650 topoG = zoomPanContainer.append('g')
Simon Huntd3b7d512014-11-12 15:48:41 -08001651 .attr('id', 'topo-G')
Simon Huntc7ee0662014-11-05 16:44:37 -08001652 .attr('transform', fcfg.translate());
1653
1654 // subgroups for links and nodes
1655 linkG = topoG.append('g').attr('id', 'links');
1656 nodeG = topoG.append('g').attr('id', 'nodes');
1657
1658 // selection of nodes and links
1659 link = linkG.selectAll('.link');
1660 node = nodeG.selectAll('.node');
1661
Simon Hunt7cd48f32014-11-09 23:42:50 -08001662 function chrg(d) {
1663 return fcfg.charge[d.class] || -12000;
1664 }
Simon Hunt99c13842014-11-06 18:23:12 -08001665 function ldist(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08001666 return fcfg.linkDistance[d.type] || 50;
Simon Hunt99c13842014-11-06 18:23:12 -08001667 }
1668 function lstrg(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08001669 // 0.0 - 1.0
1670 return fcfg.linkStrength[d.type] || 1.0;
Simon Hunt99c13842014-11-06 18:23:12 -08001671 }
1672
Simon Hunt1a9eff92014-11-07 11:06:34 -08001673 function selectCb(d, self) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001674 selectObject(d, self);
Simon Hunt1a9eff92014-11-07 11:06:34 -08001675 }
1676
1677 function atDragEnd(d, self) {
Simon Hunt56d51852014-11-09 13:03:35 -08001678 // once we've finished moving, pin the node in position
1679 d.fixed = true;
1680 d3.select(self).classed('fixed', true);
1681 if (config.useLiveData) {
Simon Hunt902c9922014-11-11 11:59:31 -08001682 sendUpdateMeta(d);
Simon Hunta255a2c2014-11-13 22:29:35 -08001683 } else {
1684 console.log('Moving node ' + d.id + ' to [' + d.x + ',' + d.y + ']');
Simon Hunt1a9eff92014-11-07 11:06:34 -08001685 }
1686 }
1687
Simon Hunt902c9922014-11-11 11:59:31 -08001688 function sendUpdateMeta(d) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001689 sendMessage('updateMeta', {
1690 id: d.id,
1691 'class': d.class,
Simon Hunt902c9922014-11-11 11:59:31 -08001692 'memento': {
Simon Hunt01095ff2014-11-13 16:37:29 -08001693 x: d.x,
1694 y: d.y
Simon Hunt902c9922014-11-11 11:59:31 -08001695 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001696 });
1697 }
1698
Simon Huntc7ee0662014-11-05 16:44:37 -08001699 // set up the force layout
1700 network.force = d3.layout.force()
1701 .size(forceDim)
1702 .nodes(network.nodes)
1703 .links(network.links)
Simon Hunt7cd48f32014-11-09 23:42:50 -08001704 .gravity(0.4)
1705 .friction(0.7)
1706 .charge(chrg)
Simon Hunt99c13842014-11-06 18:23:12 -08001707 .linkDistance(ldist)
1708 .linkStrength(lstrg)
Simon Huntc7ee0662014-11-05 16:44:37 -08001709 .on('tick', tick);
Simon Hunt195cb382014-11-03 17:50:51 -08001710
Simon Hunt01095ff2014-11-13 16:37:29 -08001711 network.drag = d3u.createDragBehavior(network.force,
Simon Huntdeab4322014-11-13 18:49:07 -08001712 selectCb, atDragEnd, panZoom);
Simon Hunt0c6d4192014-11-12 12:07:10 -08001713
1714 // create mask layer for when we lose connection to server.
Simon Hunta5e89142014-11-14 07:00:33 -08001715 // TODO: this should be part of the framework
Simon Hunt0c6d4192014-11-12 12:07:10 -08001716 mask = view.$div.append('div').attr('id','topo-mask');
1717 para(mask, 'Oops!');
1718 para(mask, 'Web-socket connection to server closed...');
1719 para(mask, 'Try refreshing the page.');
Simon Hunt1a9eff92014-11-07 11:06:34 -08001720 }
Simon Hunt195cb382014-11-03 17:50:51 -08001721
Simon Hunt01095ff2014-11-13 16:37:29 -08001722 function para(sel, text) {
1723 sel.append('p').text(text);
1724 }
1725
1726
Simon Hunt56d51852014-11-09 13:03:35 -08001727 function load(view, ctx, flags) {
Simon Huntf67722a2014-11-10 09:32:06 -08001728 // resize, in case the window was resized while we were not loaded
1729 resize(view, ctx, flags);
1730
Simon Hunt99c13842014-11-06 18:23:12 -08001731 // cache the view token, so network topo functions can access it
1732 network.view = view;
Simon Hunt56d51852014-11-09 13:03:35 -08001733 config.useLiveData = !flags.local;
1734
1735 if (!config.useLiveData) {
1736 prepareScenario(view, ctx, flags.debug);
1737 }
Simon Hunt99c13842014-11-06 18:23:12 -08001738
1739 // set our radio buttons and key bindings
Simon Hunt9462e8c2014-11-14 17:28:09 -08001740 layerBtnSet = view.setRadio(layerButtons);
Simon Hunt934c3ce2014-11-05 11:45:07 -08001741 view.setKeys(keyDispatch);
Simon Hunt195cb382014-11-03 17:50:51 -08001742
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001743 // patch in our "button bar" for now
1744 // TODO: implement a more official frameworky way of doing this..
1745 addButtonBar(view);
1746
Simon Huntd3b7d512014-11-12 15:48:41 -08001747 // Load map data asynchronously; complete startup after that..
1748 loadGeoJsonData();
Simon Hunta255a2c2014-11-13 22:29:35 -08001749
1750 // start the and timer
1751 var dashIdx = 0;
1752 antTimer = setInterval(function () {
1753 // TODO: figure out how to choose Src-->Dst and Dst-->Src, per link
1754 dashIdx = dashIdx === 0 ? 14 : dashIdx - 2;
1755 d3.selectAll('.animated').style('stroke-dashoffset', dashIdx);
1756 }, 35);
1757 }
1758
1759 function unload(view, ctx, flags) {
1760 if (antTimer) {
1761 clearInterval(antTimer);
1762 antTimer = null;
1763 }
Simon Huntd3b7d512014-11-12 15:48:41 -08001764 }
1765
1766 // TODO: move these to config/state portion of script
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001767 var geoJsonUrl = 'json/map/continental_us.json', // TODO: Paul
Simon Huntd3b7d512014-11-12 15:48:41 -08001768 geoJson;
1769
1770 function loadGeoJsonData() {
1771 d3.json(geoJsonUrl, function (err, data) {
1772 if (err) {
1773 // fall back to USA map background
1774 loadStaticMap();
1775 } else {
1776 geoJson = data;
1777 loadGeoMap();
1778 }
1779
1780 // finally, connect to the server...
1781 if (config.useLiveData) {
1782 webSock.connect();
1783 }
1784 });
1785 }
1786
1787 function showBg() {
1788 return config.options.showBackground ? 'visible' : 'hidden';
1789 }
1790
1791 function loadStaticMap() {
1792 fnTrace('loadStaticMap', config.backgroundUrl);
1793 var w = network.view.width(),
1794 h = network.view.height();
1795
1796 // load the background image
1797 bgImg = svg.insert('svg:image', '#topo-G')
1798 .attr({
1799 id: 'topo-bg',
1800 width: w,
1801 height: h,
1802 'xlink:href': config.backgroundUrl
1803 })
1804 .style({
1805 visibility: showBg()
1806 });
1807 }
1808
1809 function loadGeoMap() {
1810 fnTrace('loadGeoMap', geoJsonUrl);
Simon Huntd3b7d512014-11-12 15:48:41 -08001811
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001812 // extracts the topojson data into geocoordinate-based geometry
1813 var topoData = topojson.feature(geoJson, geoJson.objects.states);
Simon Huntd3b7d512014-11-12 15:48:41 -08001814
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001815 // see: http://bl.ocks.org/mbostock/4707858
1816 geoMapProjection = d3.geo.mercator();
1817 var path = d3.geo.path().projection(geoMapProjection);
Simon Huntd3b7d512014-11-12 15:48:41 -08001818
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001819 geoMapProjection
1820 .scale(1)
1821 .translate([0, 0]);
Simon Huntd3b7d512014-11-12 15:48:41 -08001822
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001823 // [[x1,y1],[x2,y2]]
1824 var b = path.bounds(topoData);
Paul Greysonfcba0e82014-11-13 10:21:16 -08001825 // size map to 95% of minimum dimension to fill space
1826 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 -08001827 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 -08001828
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001829 geoMapProjection
1830 .scale(s)
1831 .translate(t);
1832
Paul Greysonfcba0e82014-11-13 10:21:16 -08001833 bgImg = zoomPanContainer.insert("g", '#topo-G');
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001834 bgImg.attr('id', 'map').selectAll('path')
1835 .data(topoData.features)
1836 .enter()
1837 .append('path')
1838 .attr('d', path);
Simon Hunt195cb382014-11-03 17:50:51 -08001839 }
1840
Simon Huntf67722a2014-11-10 09:32:06 -08001841 function resize(view, ctx, flags) {
Simon Hunt934c3ce2014-11-05 11:45:07 -08001842 setSize(svg, view);
Simon Hunt142d0032014-11-04 20:13:09 -08001843 }
1844
1845
1846 // ==============================
1847 // View registration
Simon Hunt195cb382014-11-03 17:50:51 -08001848
Simon Hunt25248912014-11-04 11:25:48 -08001849 onos.ui.addView('topo', {
Simon Hunt142d0032014-11-04 20:13:09 -08001850 preload: preload,
1851 load: load,
Simon Hunta255a2c2014-11-13 22:29:35 -08001852 unload: unload,
Simon Hunt142d0032014-11-04 20:13:09 -08001853 resize: resize
Simon Hunt195cb382014-11-03 17:50:51 -08001854 });
1855
Simon Hunt61d04042014-11-11 17:27:16 -08001856 detailPane = onos.ui.addFloatingPanel('topo-detail');
Simon Hunta5e89142014-11-14 07:00:33 -08001857 oiBox = onos.ui.addFloatingPanel('topo-oibox', 'TL');
Simon Hunt61d04042014-11-11 17:27:16 -08001858
Simon Hunt195cb382014-11-03 17:50:51 -08001859}(ONOS));