blob: f2afe5faa4bfa6df3e4c30aee12c11239f06b0a6 [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,
Simon Hunt12ce12e2014-11-15 21:13:19 -080028 gly = onos.lib.glyphs,
Simon Huntbb282f52014-11-10 11:08:19 -080029 trace;
Simon Hunt1a9eff92014-11-07 11:06:34 -080030
Simon Hunt195cb382014-11-03 17:50:51 -080031 // configuration data
32 var config = {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -080033 useLiveData: true,
Simon Huntfc274c92014-11-11 11:05:46 -080034 fnTrace: true,
Simon Hunt195cb382014-11-03 17:50:51 -080035 debugOn: false,
36 debug: {
Simon Hunt99c13842014-11-06 18:23:12 -080037 showNodeXY: true,
38 showKeyHandler: false
Simon Hunt195cb382014-11-03 17:50:51 -080039 },
Simon Hunt12ce12e2014-11-15 21:13:19 -080040 birdDim: 400,
Simon Hunt195cb382014-11-03 17:50:51 -080041 options: {
42 layering: true,
43 collisionPrevention: true,
Simon Hunt142d0032014-11-04 20:13:09 -080044 showBackground: true
Simon Hunt195cb382014-11-03 17:50:51 -080045 },
46 backgroundUrl: 'img/us-map.png',
Thomas Vachuska7d638d32014-11-07 10:24:43 -080047 webSockUrl: 'ws/topology',
Simon Hunt195cb382014-11-03 17:50:51 -080048 data: {
49 live: {
50 jsonUrl: 'rs/topology/graph',
51 detailPrefix: 'rs/topology/graph/',
52 detailSuffix: ''
53 },
54 fake: {
55 jsonUrl: 'json/network2.json',
56 detailPrefix: 'json/',
57 detailSuffix: '.json'
58 }
59 },
Simon Hunt99c13842014-11-06 18:23:12 -080060 labels: {
61 imgPad: 16,
62 padLR: 4,
63 padTB: 3,
64 marginLR: 3,
65 marginTB: 2,
66 port: {
67 gap: 3,
68 width: 18,
69 height: 14
70 }
71 },
Simon Hunt1a9eff92014-11-07 11:06:34 -080072 topo: {
73 linkInColor: '#66f',
74 linkInWidth: 14
75 },
Simon Hunt99c13842014-11-06 18:23:12 -080076 icons: {
77 w: 28,
78 h: 28,
79 xoff: -12,
80 yoff: -8
81 },
Simon Hunt195cb382014-11-03 17:50:51 -080082 iconUrl: {
83 device: 'img/device.png',
84 host: 'img/host.png',
85 pkt: 'img/pkt.png',
86 opt: 'img/opt.png'
87 },
Simon Hunt195cb382014-11-03 17:50:51 -080088 force: {
Simon Hunt7cd48f32014-11-09 23:42:50 -080089 note_for_links: 'link.type is used to differentiate',
Simon Huntc7ee0662014-11-05 16:44:37 -080090 linkDistance: {
Simon Hunt7cd48f32014-11-09 23:42:50 -080091 direct: 100,
92 optical: 120,
Thomas Vachuska3266abf2014-11-13 09:28:46 -080093 hostLink: 3
Simon Huntc7ee0662014-11-05 16:44:37 -080094 },
95 linkStrength: {
Simon Hunt7cd48f32014-11-09 23:42:50 -080096 direct: 1.0,
97 optical: 1.0,
98 hostLink: 1.0
Simon Huntc7ee0662014-11-05 16:44:37 -080099 },
Simon Hunt7cd48f32014-11-09 23:42:50 -0800100 note_for_nodes: 'node.class is used to differentiate',
Simon Huntc7ee0662014-11-05 16:44:37 -0800101 charge: {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800102 device: -8000,
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800103 host: -5000
Simon Huntc7ee0662014-11-05 16:44:37 -0800104 },
105 pad: 20,
Simon Hunt195cb382014-11-03 17:50:51 -0800106 translate: function() {
107 return 'translate(' +
Simon Huntc7ee0662014-11-05 16:44:37 -0800108 config.force.pad + ',' +
109 config.force.pad + ')';
Simon Hunt195cb382014-11-03 17:50:51 -0800110 }
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800111 },
112 // see below in creation of viewBox on main svg
113 logicalSize: 1000
Simon Hunt195cb382014-11-03 17:50:51 -0800114 };
115
Simon Hunt142d0032014-11-04 20:13:09 -0800116 // radio buttons
Simon Hunt9462e8c2014-11-14 17:28:09 -0800117 var layerButtons = [
118 { text: 'All Layers', id: 'all', cb: showAllLayers },
119 { text: 'Packet Only', id: 'pkt', cb: showPacketLayer },
120 { text: 'Optical Only', id: 'opt', cb: showOpticalLayer }
121 ],
122 layerBtnSet,
123 layerBtnDispatch = {
124 all: showAllLayers,
125 pkt: showPacketLayer,
126 opt: showOpticalLayer
127 };
Simon Hunt934c3ce2014-11-05 11:45:07 -0800128
129 // key bindings
130 var keyDispatch = {
Simon Hunta255a2c2014-11-13 22:29:35 -0800131 M: testMe, // TODO: remove (testing only)
132 S: injectStartupEvents, // TODO: remove (testing only)
133 space: injectTestEvent, // TODO: remove (testing only)
Simon Hunt99c13842014-11-06 18:23:12 -0800134
Simon Hunt01095ff2014-11-13 16:37:29 -0800135 B: toggleBg,
Simon Hunt934c3ce2014-11-05 11:45:07 -0800136 L: cycleLabels,
137 P: togglePorts,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800138 U: unpin,
Paul Greysonfcba0e82014-11-13 10:21:16 -0800139 R: resetZoomPan,
Simon Hunt9462e8c2014-11-14 17:28:09 -0800140 esc: handleEscape
Simon Hunt934c3ce2014-11-05 11:45:07 -0800141 };
Simon Hunt142d0032014-11-04 20:13:09 -0800142
Simon Hunt195cb382014-11-03 17:50:51 -0800143 // state variables
Simon Hunt99c13842014-11-06 18:23:12 -0800144 var network = {
Simon Hunt50128c02014-11-08 13:36:15 -0800145 view: null, // view token reference
Simon Hunt99c13842014-11-06 18:23:12 -0800146 nodes: [],
147 links: [],
148 lookup: {}
149 },
Simon Hunt56d51852014-11-09 13:03:35 -0800150 scenario = {
151 evDir: 'json/ev/',
152 evScenario: '/scenario.json',
153 evPrefix: '/ev_',
154 evOnos: '_onos.json',
155 evUi: '_ui.json',
156 ctx: null,
157 params: {},
158 evNumber: 0,
159 view: null,
160 debug: false
161 },
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800162 webSock,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800163 sid = 0,
Simon Hunt56d51852014-11-09 13:03:35 -0800164 deviceLabelIndex = 0,
165 hostLabelIndex = 0,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800166 selections = {},
Simon Hunta5e89142014-11-14 07:00:33 -0800167 selectOrder = [],
Simon Hunt6ac93f32014-11-13 12:17:27 -0800168 hovered = null,
Simon Hunta5e89142014-11-14 07:00:33 -0800169 detailPane,
Simon Hunta255a2c2014-11-13 22:29:35 -0800170 antTimer = null,
Simon Hunta5e89142014-11-14 07:00:33 -0800171 onosInstances = {},
172 onosOrder = [],
173 oiBox,
Simon Hunt9462e8c2014-11-14 17:28:09 -0800174 oiShowMaster = false,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800175
Simon Hunt195cb382014-11-03 17:50:51 -0800176 portLabelsOn = false;
177
Simon Hunt934c3ce2014-11-05 11:45:07 -0800178 // D3 selections
179 var svg,
Paul Greysonfcba0e82014-11-13 10:21:16 -0800180 zoomPanContainer,
Simon Hunt934c3ce2014-11-05 11:45:07 -0800181 bgImg,
Simon Huntc7ee0662014-11-05 16:44:37 -0800182 topoG,
183 nodeG,
184 linkG,
185 node,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800186 link,
187 mask;
Simon Hunt195cb382014-11-03 17:50:51 -0800188
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800189 // the projection for the map background
190 var geoMapProjection;
191
Paul Greysonfcba0e82014-11-13 10:21:16 -0800192 // the zoom function
193 var zoom;
194
Simon Hunt142d0032014-11-04 20:13:09 -0800195 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800196 // For Debugging / Development
Simon Hunt195cb382014-11-03 17:50:51 -0800197
Simon Hunt99c13842014-11-06 18:23:12 -0800198 function note(label, msg) {
199 console.log('NOTE: ' + label + ': ' + msg);
Simon Hunt195cb382014-11-03 17:50:51 -0800200 }
201
Simon Hunt99c13842014-11-06 18:23:12 -0800202 function debug(what) {
203 return config.debugOn && config.debug[what];
Simon Hunt934c3ce2014-11-05 11:45:07 -0800204 }
205
Simon Huntfc274c92014-11-11 11:05:46 -0800206 function fnTrace(msg, id) {
207 if (config.fnTrace) {
208 console.log('FN: ' + msg + ' [' + id + ']');
209 }
210 }
Simon Hunt99c13842014-11-06 18:23:12 -0800211
Simon Hunta5e89142014-11-14 07:00:33 -0800212 function evTrace(data) {
213 fnTrace(data.event, data.payload.id);
214 }
215
Simon Hunt934c3ce2014-11-05 11:45:07 -0800216 // ==============================
217 // Key Callbacks
218
Simon Hunt99c13842014-11-06 18:23:12 -0800219 function testMe(view) {
Simon Hunta5e89142014-11-14 07:00:33 -0800220 //view.alert('test');
Simon Hunt12ce12e2014-11-15 21:13:19 -0800221 noWebSock(true);
Simon Hunt99c13842014-11-06 18:23:12 -0800222 }
223
Simon Hunt56d51852014-11-09 13:03:35 -0800224 function abortIfLive() {
Simon Hunt50128c02014-11-08 13:36:15 -0800225 if (config.useLiveData) {
Simon Huntb53e0682014-11-12 13:32:01 -0800226 network.view.alert("Sorry, currently using live data..");
Simon Hunt56d51852014-11-09 13:03:35 -0800227 return true;
Simon Hunt50128c02014-11-08 13:36:15 -0800228 }
Simon Hunt56d51852014-11-09 13:03:35 -0800229 return false;
230 }
Simon Hunt50128c02014-11-08 13:36:15 -0800231
Simon Hunt56d51852014-11-09 13:03:35 -0800232 function testDebug(msg) {
233 if (scenario.debug) {
234 scenario.view.alert(msg);
235 }
236 }
Simon Hunt99c13842014-11-06 18:23:12 -0800237
Simon Hunt56d51852014-11-09 13:03:35 -0800238 function injectTestEvent(view) {
239 if (abortIfLive()) { return; }
240 var sc = scenario,
241 evn = ++sc.evNumber,
242 pfx = sc.evDir + sc.ctx + sc.evPrefix + evn,
243 onosUrl = pfx + sc.evOnos,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800244 uiUrl = pfx + sc.evUi,
245 stack = [
246 { url: onosUrl, cb: handleServerEvent },
247 { url: uiUrl, cb: handleUiEvent }
248 ];
249 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800250 }
251
Simon Hunt7cd48f32014-11-09 23:42:50 -0800252 function recurseFetchEvent(stack, evn) {
253 var v = scenario.view,
254 frame;
255 if (stack.length === 0) {
Simon Huntfc274c92014-11-11 11:05:46 -0800256 v.alert('Oops!\n\nNo event #' + evn + ' found.');
Simon Hunt7cd48f32014-11-09 23:42:50 -0800257 return;
258 }
259 frame = stack.shift();
260
261 d3.json(frame.url, function (err, data) {
Simon Hunt99c13842014-11-06 18:23:12 -0800262 if (err) {
Simon Hunt56d51852014-11-09 13:03:35 -0800263 if (err.status === 404) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800264 // if we didn't find the data, try the next stack frame
265 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800266 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800267 v.alert('non-404 error:\n\n' + frame.url + '\n\n' + err);
Simon Hunt56d51852014-11-09 13:03:35 -0800268 }
Simon Hunt99c13842014-11-06 18:23:12 -0800269 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800270 testDebug('loaded: ' + frame.url);
271 frame.cb(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800272 }
273 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800274
Simon Hunt56d51852014-11-09 13:03:35 -0800275 }
Simon Hunt50128c02014-11-08 13:36:15 -0800276
Simon Hunt56d51852014-11-09 13:03:35 -0800277 function handleUiEvent(data) {
Simon Huntbb282f52014-11-10 11:08:19 -0800278 scenario.view.alert('UI Tx: ' + data.event + '\n\n' +
279 JSON.stringify(data));
Simon Hunt56d51852014-11-09 13:03:35 -0800280 }
281
282 function injectStartupEvents(view) {
283 var last = scenario.params.lastAuto || 0;
284 if (abortIfLive()) { return; }
285
286 while (scenario.evNumber < last) {
Simon Hunt1a9eff92014-11-07 11:06:34 -0800287 injectTestEvent(view);
288 }
289 }
290
Simon Hunt934c3ce2014-11-05 11:45:07 -0800291 function toggleBg() {
292 var vis = bgImg.style('visibility');
293 bgImg.style('visibility', (vis === 'hidden') ? 'visible' : 'hidden');
294 }
295
Simon Hunt99c13842014-11-06 18:23:12 -0800296 function cycleLabels() {
Simon Huntbb282f52014-11-10 11:08:19 -0800297 deviceLabelIndex = (deviceLabelIndex === network.deviceLabelCount - 1)
298 ? 0 : deviceLabelIndex + 1;
Simon Hunt5f36d342014-11-08 21:33:14 -0800299
Simon Hunt99c13842014-11-06 18:23:12 -0800300 network.nodes.forEach(function (d) {
Simon Huntbb282f52014-11-10 11:08:19 -0800301 if (d.class === 'device') {
302 updateDeviceLabel(d);
303 }
Simon Hunt99c13842014-11-06 18:23:12 -0800304 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800305 }
306
307 function togglePorts(view) {
Simon Hunt50128c02014-11-08 13:36:15 -0800308 view.alert('togglePorts() callback')
Simon Hunt934c3ce2014-11-05 11:45:07 -0800309 }
310
Simon Hunt6ac93f32014-11-13 12:17:27 -0800311 function unpin() {
312 if (hovered) {
313 hovered.fixed = false;
314 hovered.el.classed('fixed', false);
315 network.force.resume();
316 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800317 }
318
Simon Hunt9462e8c2014-11-14 17:28:09 -0800319 function handleEscape(view) {
320 if (oiShowMaster) {
321 cancelAffinity();
322 } else {
323 deselectAll();
324 }
325 }
326
Simon Hunt934c3ce2014-11-05 11:45:07 -0800327 // ==============================
328 // Radio Button Callbacks
329
Simon Hunta5e89142014-11-14 07:00:33 -0800330 var layerLookup = {
331 host: {
332 endstation: 'pkt', // default, if host event does not define type
333 bgpSpeaker: 'pkt'
334 },
335 device: {
336 switch: 'pkt',
337 roadm: 'opt'
338 },
339 link: {
340 hostLink: 'pkt',
341 direct: 'pkt',
342 optical: 'opt'
343 }
344 };
345
346 function inLayer(d, layer) {
347 var look = layerLookup[d.class],
348 lyr = look && look[d.type];
349 return lyr === layer;
350 }
351
352 function unsuppressLayer(which) {
353 node.each(function (d) {
354 var node = d.el;
355 if (inLayer(d, which)) {
356 node.classed('suppressed', false);
357 }
358 });
359
360 link.each(function (d) {
361 var link = d.el;
362 if (inLayer(d, which)) {
363 link.classed('suppressed', false);
364 }
365 });
366 }
367
Simon Hunt9462e8c2014-11-14 17:28:09 -0800368 function suppressLayers(b) {
369 node.classed('suppressed', b);
370 link.classed('suppressed', b);
Simon Hunt142d0032014-11-04 20:13:09 -0800371// d3.selectAll('svg .port').classed('inactive', false);
372// d3.selectAll('svg .portText').classed('inactive', false);
Simon Hunt195cb382014-11-03 17:50:51 -0800373 }
374
Simon Hunt9462e8c2014-11-14 17:28:09 -0800375 function showAllLayers() {
376 suppressLayers(false);
377 }
378
Simon Hunt195cb382014-11-03 17:50:51 -0800379 function showPacketLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800380 node.classed('suppressed', true);
381 link.classed('suppressed', true);
382 unsuppressLayer('pkt');
Simon Hunt195cb382014-11-03 17:50:51 -0800383 }
384
385 function showOpticalLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800386 node.classed('suppressed', true);
387 link.classed('suppressed', true);
388 unsuppressLayer('opt');
Simon Hunt195cb382014-11-03 17:50:51 -0800389 }
390
Simon Hunt9462e8c2014-11-14 17:28:09 -0800391 function restoreLayerState() {
392 layerBtnDispatch[layerBtnSet.selected()]();
393 }
394
Simon Hunt142d0032014-11-04 20:13:09 -0800395 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800396 // Private functions
397
Simon Hunt99c13842014-11-06 18:23:12 -0800398 function safeId(s) {
399 return s.replace(/[^a-z0-9]/gi, '-');
400 }
401
Simon Huntc7ee0662014-11-05 16:44:37 -0800402 // set the size of the given element to that of the view (reduced if padded)
403 function setSize(el, view, pad) {
404 var padding = pad ? pad * 2 : 0;
Simon Hunt934c3ce2014-11-05 11:45:07 -0800405 el.attr({
Simon Huntc7ee0662014-11-05 16:44:37 -0800406 width: view.width() - padding,
407 height: view.height() - padding
Simon Hunt934c3ce2014-11-05 11:45:07 -0800408 });
409 }
410
Simon Hunt934c3ce2014-11-05 11:45:07 -0800411
Simon Hunt99c13842014-11-06 18:23:12 -0800412 // ==============================
413 // Event handlers for server-pushed events
414
Simon Huntbb282f52014-11-10 11:08:19 -0800415 function logicError(msg) {
416 // TODO, report logic error to server, via websock, so it can be logged
417 network.view.alert('Logic Error:\n\n' + msg);
Simon Huntfc274c92014-11-11 11:05:46 -0800418 console.warn(msg);
Simon Huntbb282f52014-11-10 11:08:19 -0800419 }
420
Simon Hunt99c13842014-11-06 18:23:12 -0800421 var eventDispatch = {
Simon Hunta5e89142014-11-14 07:00:33 -0800422 addInstance: addInstance,
Simon Hunt99c13842014-11-06 18:23:12 -0800423 addDevice: addDevice,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800424 addLink: addLink,
Simon Hunt56d51852014-11-09 13:03:35 -0800425 addHost: addHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800426
Simon Huntd72bc702014-11-13 18:38:04 -0800427 updateInstance: stillToImplement,
Simon Huntbb282f52014-11-10 11:08:19 -0800428 updateDevice: updateDevice,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800429 updateLink: updateLink,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800430 updateHost: updateHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800431
Simon Huntd72bc702014-11-13 18:38:04 -0800432 removeInstance: stillToImplement,
Simon Huntbb282f52014-11-10 11:08:19 -0800433 removeDevice: stillToImplement,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800434 removeLink: removeLink,
Simon Hunt44031102014-11-11 13:20:36 -0800435 removeHost: removeHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800436
Simon Hunt61d04042014-11-11 17:27:16 -0800437 showDetails: showDetails,
Simon Huntb53e0682014-11-12 13:32:01 -0800438 showPath: showPath,
439 showTraffic: showTraffic
Simon Hunt99c13842014-11-06 18:23:12 -0800440 };
441
Simon Hunta5e89142014-11-14 07:00:33 -0800442 function addInstance(data) {
443 evTrace(data);
444 var inst = data.payload,
445 id = inst.id;
446 if (onosInstances[id]) {
447 logicError('ONOS instance already added: ' + id);
448 return;
449 }
450 onosInstances[id] = inst;
451 onosOrder.push(inst);
452 updateInstances();
453 }
454
Simon Hunt99c13842014-11-06 18:23:12 -0800455 function addDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800456 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800457 var device = data.payload,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800458 nodeData = createDeviceNode(device);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800459 network.nodes.push(nodeData);
460 network.lookup[nodeData.id] = nodeData;
Simon Hunt99c13842014-11-06 18:23:12 -0800461 updateNodes();
462 network.force.start();
463 }
464
Simon Hunt99c13842014-11-06 18:23:12 -0800465 function addLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800466 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800467 var link = data.payload,
468 lnk = createLink(link);
Simon Hunt99c13842014-11-06 18:23:12 -0800469 if (lnk) {
Simon Hunt99c13842014-11-06 18:23:12 -0800470 network.links.push(lnk);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800471 network.lookup[lnk.id] = lnk;
Simon Hunt99c13842014-11-06 18:23:12 -0800472 updateLinks();
473 network.force.start();
474 }
475 }
476
Simon Hunt56d51852014-11-09 13:03:35 -0800477 function addHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800478 evTrace(data);
Simon Hunt56d51852014-11-09 13:03:35 -0800479 var host = data.payload,
480 node = createHostNode(host),
481 lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800482 network.nodes.push(node);
483 network.lookup[host.id] = node;
484 updateNodes();
485
486 lnk = createHostLink(host);
487 if (lnk) {
Simon Hunt44031102014-11-11 13:20:36 -0800488 node.linkData = lnk; // cache ref on its host
Simon Hunt56d51852014-11-09 13:03:35 -0800489 network.links.push(lnk);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800490 network.lookup[host.ingress] = lnk;
491 network.lookup[host.egress] = lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800492 updateLinks();
493 }
494 network.force.start();
495 }
496
Simon Hunt44031102014-11-11 13:20:36 -0800497 // TODO: fold updateX(...) methods into one base method; remove duplication
Simon Huntbb282f52014-11-10 11:08:19 -0800498 function updateDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800499 evTrace(data);
Simon Huntbb282f52014-11-10 11:08:19 -0800500 var device = data.payload,
501 id = device.id,
502 nodeData = network.lookup[id];
503 if (nodeData) {
504 $.extend(nodeData, device);
505 updateDeviceState(nodeData);
506 } else {
507 logicError('updateDevice lookup fail. ID = "' + id + '"');
508 }
509 }
510
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800511 function updateLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800512 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800513 var link = data.payload,
514 id = link.id,
515 linkData = network.lookup[id];
516 if (linkData) {
517 $.extend(linkData, link);
518 updateLinkState(linkData);
519 } else {
520 logicError('updateLink lookup fail. ID = "' + id + '"');
521 }
522 }
523
Simon Hunt7cd48f32014-11-09 23:42:50 -0800524 function updateHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800525 evTrace(data);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800526 var host = data.payload,
Simon Huntbb282f52014-11-10 11:08:19 -0800527 id = host.id,
528 hostData = network.lookup[id];
529 if (hostData) {
530 $.extend(hostData, host);
531 updateHostState(hostData);
532 } else {
533 logicError('updateHost lookup fail. ID = "' + id + '"');
534 }
Simon Hunt7cd48f32014-11-09 23:42:50 -0800535 }
536
Simon Hunt44031102014-11-11 13:20:36 -0800537 // TODO: fold removeX(...) methods into base method - remove dup code
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800538 function removeLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800539 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800540 var link = data.payload,
541 id = link.id,
542 linkData = network.lookup[id];
543 if (linkData) {
544 removeLinkElement(linkData);
545 } else {
546 logicError('removeLink lookup fail. ID = "' + id + '"');
547 }
548 }
549
Simon Hunt44031102014-11-11 13:20:36 -0800550 function removeHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800551 evTrace(data);
Simon Hunt44031102014-11-11 13:20:36 -0800552 var host = data.payload,
553 id = host.id,
554 hostData = network.lookup[id];
555 if (hostData) {
556 removeHostElement(hostData);
557 } else {
558 logicError('removeHost lookup fail. ID = "' + id + '"');
559 }
560 }
561
Simon Hunt61d04042014-11-11 17:27:16 -0800562 function showDetails(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800563 evTrace(data);
Simon Hunt61d04042014-11-11 17:27:16 -0800564 populateDetails(data.payload);
565 detailPane.show();
566 }
567
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800568 function showPath(data) {
Simon Huntd72bc702014-11-13 18:38:04 -0800569 // TODO: review - making sure we are handling the payload correctly.
Simon Hunta5e89142014-11-14 07:00:33 -0800570 evTrace(data);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800571 var links = data.payload.links,
572 s = [ data.event + "\n" + links.length ];
573 links.forEach(function (d, i) {
574 s.push(d);
575 });
576 network.view.alert(s.join('\n'));
577
578 links.forEach(function (d, i) {
579 var link = network.lookup[d];
580 if (link) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800581 link.el.classed('showPath', true);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800582 }
583 });
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800584 }
585
Simon Huntb53e0682014-11-12 13:32:01 -0800586 function showTraffic(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800587 evTrace(data);
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800588 var paths = data.payload.paths;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800589
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800590 // Revert any links hilighted previously.
Simon Hunta255a2c2014-11-13 22:29:35 -0800591 link.classed('primary secondary animated optical', false);
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800592
593 // Now hilight all links in the paths payload.
Simon Hunta255a2c2014-11-13 22:29:35 -0800594 paths.forEach(function (p) {
595 var cls = p.class;
596 p.links.forEach(function (id) {
597 var lnk = network.lookup[id];
598 if (lnk) {
599 lnk.el.classed(cls, true);
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800600 }
601 });
602 });
Simon Huntb53e0682014-11-12 13:32:01 -0800603 }
604
Simon Hunt56d51852014-11-09 13:03:35 -0800605 // ...............................
606
607 function stillToImplement(data) {
608 var p = data.payload;
609 note(data.event, p.id);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800610 network.view.alert('Not yet implemented: "' + data.event + '"');
Simon Hunt56d51852014-11-09 13:03:35 -0800611 }
Simon Hunt99c13842014-11-06 18:23:12 -0800612
613 function unknownEvent(data) {
Simon Hunt50128c02014-11-08 13:36:15 -0800614 network.view.alert('Unknown event type: "' + data.event + '"');
Simon Hunt99c13842014-11-06 18:23:12 -0800615 }
616
617 function handleServerEvent(data) {
618 var fn = eventDispatch[data.event] || unknownEvent;
619 fn(data);
620 }
621
622 // ==============================
Simon Hunt61d04042014-11-11 17:27:16 -0800623 // Out-going messages...
624
Simon Huntb53e0682014-11-12 13:32:01 -0800625 function userFeedback(msg) {
626 // for now, use the alert pane as is. Maybe different alert style in
627 // the future (centered on view; dismiss button?)
628 network.view.alert(msg);
629 }
630
631 function nSel() {
632 return selectOrder.length;
633 }
Simon Hunt61d04042014-11-11 17:27:16 -0800634 function getSel(idx) {
635 return selections[selectOrder[idx]];
636 }
Simon Huntb53e0682014-11-12 13:32:01 -0800637 function getSelId(idx) {
638 return getSel(idx).obj.id;
639 }
640 function allSelectionsClass(cls) {
641 for (var i=0, n=nSel(); i<n; i++) {
642 if (getSel(i).obj.class !== cls) {
643 return false;
644 }
645 }
646 return true;
647 }
Simon Hunt61d04042014-11-11 17:27:16 -0800648
Simon Hunt61d04042014-11-11 17:27:16 -0800649 // request details for the selected element
Simon Huntd72bc702014-11-13 18:38:04 -0800650 // invoked from selection of a single node.
Simon Hunt61d04042014-11-11 17:27:16 -0800651 function requestDetails() {
652 var data = getSel(0).obj,
653 payload = {
654 id: data.id,
655 class: data.class
656 };
657 sendMessage('requestDetails', payload);
658 }
659
Simon Huntd72bc702014-11-13 18:38:04 -0800660 function addIntentAction() {
661 sendMessage('addHostIntent', {
662 one: getSelId(0),
663 two: getSelId(1)
664 });
665 }
666
667 function showTrafficAction() {
668 // if nothing is hovered over, and nothing selected, send cancel request
669 if (!hovered && nSel() === 0) {
670 sendMessage('cancelTraffic', {});
671 return;
672 }
673
674 // NOTE: hover is only populated if "show traffic on hover" is
675 // toggled on, and the item hovered is a host...
676 var hoverId = (trafficHover() && hovered && hovered.class === 'host')
677 ? hovered.id : '';
678 sendMessage('requestTraffic', {
679 ids: selectOrder,
680 hover: hoverId
681 });
682 }
683
684
Simon Hunt61d04042014-11-11 17:27:16 -0800685 // ==============================
Simon Hunta5e89142014-11-14 07:00:33 -0800686 // onos instance panel functions
687
688 function updateInstances() {
689 var onoses = oiBox.el.selectAll('.onosInst')
690 .data(onosOrder, function (d) { return d.id; });
691
692 // operate on existing onoses if necessary
693
694 var entering = onoses.enter()
695 .append('div')
696 .attr('class', 'onosInst')
697 .classed('online', function (d) { return d.online; })
Simon Hunt9c15eca2014-11-15 18:37:59 -0800698 .on('click', clickInst);
699
700 entering.each(function (d, i) {
701 var el = d3.select(this),
702 img;
703
704 $('<img src="img/host.png">').appendTo(el);
705 img = el.select('img')
706 .attr({
707 width: 40,
708 top: -10,
709 left: -10
710 })
711 .style({
712 });
713
714 $('<div>').attr('class', 'onosTitle').text(d.id).appendTo(el);
715
716 // is the UI attached to this instance?
717 // TODO: need uiAttached boolean in instance data
718 //if (d.uiAttached) {
719 if (i === 0) {
720 $('<img src="img/ui.png">').attr('class','ui').appendTo(el);
721 }
722 });
Simon Hunta5e89142014-11-14 07:00:33 -0800723
724 // operate on existing + new onoses here
725
726 // the departed...
727 var exiting = onoses.exit()
728 .transition()
729 .style('opacity', 0)
730 .remove();
731 }
732
Simon Hunt9462e8c2014-11-14 17:28:09 -0800733 function clickInst(d) {
734 var el = d3.select(this),
735 aff = el.classed('affinity');
736 if (!aff) {
737 setAffinity(el, d);
738 } else {
739 cancelAffinity();
740 }
741 }
742
743 function setAffinity(el, d) {
744 d3.selectAll('.onosInst')
745 .classed('mastership', true)
746 .classed('affinity', false);
747 el.classed('affinity', true);
748
749 suppressLayers(true);
750 node.each(function (n) {
751 if (n.master === d.id) {
752 n.el.classed('suppressed', false);
753 }
754 });
755 oiShowMaster = true;
756 }
757
758 function cancelAffinity() {
759 d3.selectAll('.onosInst')
760 .classed('mastership affinity', false);
761 restoreLayerState();
762 oiShowMaster = false;
763 }
764
Simon Hunta5e89142014-11-14 07:00:33 -0800765 // ==============================
Simon Hunt99c13842014-11-06 18:23:12 -0800766 // force layout modification functions
767
768 function translate(x, y) {
769 return 'translate(' + x + ',' + y + ')';
770 }
771
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800772 function missMsg(what, id) {
773 return '\n[' + what + '] "' + id + '" missing ';
774 }
775
776 function linkEndPoints(srcId, dstId) {
777 var srcNode = network.lookup[srcId],
778 dstNode = network.lookup[dstId],
779 sMiss = !srcNode ? missMsg('src', srcId) : '',
780 dMiss = !dstNode ? missMsg('dst', dstId) : '';
781
782 if (sMiss || dMiss) {
783 logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
784 return null;
785 }
786 return {
787 source: srcNode,
788 target: dstNode,
789 x1: srcNode.x,
790 y1: srcNode.y,
791 x2: dstNode.x,
792 y2: dstNode.y
793 };
794 }
795
Simon Hunt56d51852014-11-09 13:03:35 -0800796 function createHostLink(host) {
797 var src = host.id,
798 dst = host.cp.device,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800799 id = host.ingress,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800800 lnk = linkEndPoints(src, dst);
Simon Hunt56d51852014-11-09 13:03:35 -0800801
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800802 if (!lnk) {
Simon Hunt56d51852014-11-09 13:03:35 -0800803 return null;
804 }
805
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800806 // Synthesize link ...
807 $.extend(lnk, {
Thomas Vachuska4830d392014-11-09 17:09:56 -0800808 id: id,
Simon Hunt56d51852014-11-09 13:03:35 -0800809 class: 'link',
Simon Hunt7cd48f32014-11-09 23:42:50 -0800810 type: 'hostLink',
Simon Hunt56d51852014-11-09 13:03:35 -0800811 svgClass: 'link hostLink',
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800812 linkWidth: 1
Simon Hunt7cd48f32014-11-09 23:42:50 -0800813 });
Simon Hunt99c13842014-11-06 18:23:12 -0800814 return lnk;
815 }
816
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800817 function createLink(link) {
818 var lnk = linkEndPoints(link.src, link.dst),
819 type = link.type;
820
821 if (!lnk) {
822 return null;
823 }
824
825 // merge in remaining data
826 $.extend(lnk, link, {
827 class: 'link',
Thomas Vachuskae4cebaf2014-11-15 18:49:34 -0800828 svgClass: (type ? 'link ' + type : 'link') + ' ' + (link.online ? 'active' : 'inactive')
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800829 });
830 return lnk;
Simon Hunt1a9eff92014-11-07 11:06:34 -0800831 }
832
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800833 var widthRatio = 1.4,
834 linkScale = d3.scale.linear()
835 .domain([1, 12])
836 .range([widthRatio, 12 * widthRatio])
837 .clamp(true);
838
839 function updateLinkWidth (d) {
840 // TODO: watch out for .showPath/.showTraffic classes
841 d.el.transition()
842 .duration(1000)
843 .attr('stroke-width', linkScale(d.linkWidth));
844 }
845
846
Simon Hunt99c13842014-11-06 18:23:12 -0800847 function updateLinks() {
848 link = linkG.selectAll('.link')
849 .data(network.links, function (d) { return d.id; });
850
851 // operate on existing links, if necessary
852 // link .foo() .bar() ...
853
854 // operate on entering links:
855 var entering = link.enter()
856 .append('line')
857 .attr({
Simon Hunt99c13842014-11-06 18:23:12 -0800858 class: function (d) { return d.svgClass; },
859 x1: function (d) { return d.x1; },
860 y1: function (d) { return d.y1; },
861 x2: function (d) { return d.x2; },
862 y2: function (d) { return d.y2; },
Simon Hunt1a9eff92014-11-07 11:06:34 -0800863 stroke: config.topo.linkInColor,
864 'stroke-width': config.topo.linkInWidth
Simon Hunt99c13842014-11-06 18:23:12 -0800865 })
866 .transition().duration(1000)
867 .attr({
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800868 'stroke-width': function (d) { return linkScale(d.linkWidth); },
Simon Hunt99c13842014-11-06 18:23:12 -0800869 stroke: '#666' // TODO: remove explicit stroke, rather...
870 });
871
872 // augment links
Simon Hunt7cd48f32014-11-09 23:42:50 -0800873 entering.each(function (d) {
874 var link = d3.select(this);
875 // provide ref to element selection from backing data....
876 d.el = link;
Simon Hunt99c13842014-11-06 18:23:12 -0800877
Simon Hunt7cd48f32014-11-09 23:42:50 -0800878 // TODO: add src/dst port labels etc.
879 });
Thomas Vachuska4830d392014-11-09 17:09:56 -0800880
881 // operate on both existing and new links, if necessary
882 //link .foo() .bar() ...
883
884 // operate on exiting links:
Thomas Vachuska4830d392014-11-09 17:09:56 -0800885 link.exit()
Thomas Vachuska4830d392014-11-09 17:09:56 -0800886 .attr({
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800887 'stroke-dasharray': '3, 3'
Thomas Vachuska4830d392014-11-09 17:09:56 -0800888 })
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800889 .style('opacity', 0.4)
890 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -0800891 .duration(1500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800892 .attr({
893 'stroke-dasharray': '3, 12'
894 })
895 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -0800896 .duration(500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800897 .style('opacity', 0.0)
Thomas Vachuska4830d392014-11-09 17:09:56 -0800898 .remove();
Simon Hunt99c13842014-11-06 18:23:12 -0800899 }
900
901 function createDeviceNode(device) {
902 // start with the object as is
903 var node = device,
Simon Huntbb282f52014-11-10 11:08:19 -0800904 type = device.type,
905 svgCls = type ? 'node device ' + type : 'node device';
Simon Hunt99c13842014-11-06 18:23:12 -0800906
907 // Augment as needed...
908 node.class = 'device';
Simon Huntbb282f52014-11-10 11:08:19 -0800909 node.svgClass = device.online ? svgCls + ' online' : svgCls;
Simon Hunt99c13842014-11-06 18:23:12 -0800910 positionNode(node);
911
912 // cache label array length
913 network.deviceLabelCount = device.labels.length;
Simon Hunt99c13842014-11-06 18:23:12 -0800914 return node;
915 }
916
Simon Hunt56d51852014-11-09 13:03:35 -0800917 function createHostNode(host) {
918 // start with the object as is
919 var node = host;
920
921 // Augment as needed...
922 node.class = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -0800923 if (!node.type) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800924 node.type = 'endstation';
925 }
Simon Hunt56d51852014-11-09 13:03:35 -0800926 node.svgClass = 'node host';
Simon Hunt56d51852014-11-09 13:03:35 -0800927 positionNode(node);
928
929 // cache label array length
930 network.hostLabelCount = host.labels.length;
Simon Hunt56d51852014-11-09 13:03:35 -0800931 return node;
932 }
933
Simon Hunt99c13842014-11-06 18:23:12 -0800934 function positionNode(node) {
935 var meta = node.metaUi,
Simon Huntac9e24f2014-11-12 10:12:21 -0800936 x = meta && meta.x,
937 y = meta && meta.y,
938 xy;
Simon Hunt99c13842014-11-06 18:23:12 -0800939
Simon Huntac9e24f2014-11-12 10:12:21 -0800940 // If we have [x,y] already, use that...
Simon Hunt99c13842014-11-06 18:23:12 -0800941 if (x && y) {
942 node.fixed = true;
Simon Huntac9e24f2014-11-12 10:12:21 -0800943 node.x = x;
944 node.y = y;
945 return;
Simon Hunt99c13842014-11-06 18:23:12 -0800946 }
Simon Huntac9e24f2014-11-12 10:12:21 -0800947
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800948 var location = node.location;
949 if (location && location.type === 'latlng') {
950 var coord = geoMapProjection([location.lng, location.lat]);
951 node.fixed = true;
952 node.x = coord[0];
953 node.y = coord[1];
954 return;
955 }
956
Simon Huntac9e24f2014-11-12 10:12:21 -0800957 // Note: Placing incoming unpinned nodes at exactly the same point
958 // (center of the view) causes them to explode outwards when
959 // the force layout kicks in. So, we spread them out a bit
960 // initially, to provide a more serene layout convergence.
961 // Additionally, if the node is a host, we place it near
962 // the device it is connected to.
963
964 function spread(s) {
965 return Math.floor((Math.random() * s) - s/2);
966 }
967
968 function randDim(dim) {
969 return dim / 2 + spread(dim * 0.7071);
970 }
971
972 function rand() {
973 return {
974 x: randDim(network.view.width()),
975 y: randDim(network.view.height())
976 };
977 }
978
979 function near(node) {
980 var min = 12,
981 dx = spread(12),
982 dy = spread(12);
983 return {
984 x: node.x + min + dx,
985 y: node.y + min + dy
986 };
987 }
988
989 function getDevice(cp) {
990 var d = network.lookup[cp.device];
991 return d || rand();
992 }
993
994 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
995 $.extend(node, xy);
Simon Hunt99c13842014-11-06 18:23:12 -0800996 }
997
Simon Hunt99c13842014-11-06 18:23:12 -0800998 function iconUrl(d) {
999 return 'img/' + d.type + '.png';
1000 }
1001
1002 // returns the newly computed bounding box of the rectangle
1003 function adjustRectToFitText(n) {
1004 var text = n.select('text'),
1005 box = text.node().getBBox(),
1006 lab = config.labels;
1007
1008 text.attr('text-anchor', 'middle')
1009 .attr('y', '-0.8em')
1010 .attr('x', lab.imgPad/2);
1011
1012 // translate the bbox so that it is centered on [x,y]
1013 box.x = -box.width / 2;
1014 box.y = -box.height / 2;
1015
1016 // add padding
1017 box.x -= (lab.padLR + lab.imgPad/2);
1018 box.width += lab.padLR * 2 + lab.imgPad;
1019 box.y -= lab.padTB;
1020 box.height += lab.padTB * 2;
1021
1022 return box;
1023 }
1024
Simon Hunt1a9eff92014-11-07 11:06:34 -08001025 function mkSvgClass(d) {
1026 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
1027 }
1028
Simon Hunt7cd48f32014-11-09 23:42:50 -08001029 function hostLabel(d) {
1030 var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0;
1031 return d.labels[idx];
1032 }
1033 function deviceLabel(d) {
1034 var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0;
1035 return d.labels[idx];
1036 }
1037 function niceLabel(label) {
1038 return (label && label.trim()) ? label : '.';
1039 }
1040
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001041 function updateDeviceLabel(d) {
1042 var label = niceLabel(deviceLabel(d)),
1043 node = d.el,
1044 box;
1045
1046 node.select('text')
1047 .text(label)
1048 .style('opacity', 0)
1049 .transition()
1050 .style('opacity', 1);
1051
1052 box = adjustRectToFitText(node);
1053
1054 node.select('rect')
1055 .transition()
1056 .attr(box);
1057
1058 node.select('image')
1059 .transition()
1060 .attr('x', box.x + config.icons.xoff)
1061 .attr('y', box.y + config.icons.yoff);
1062 }
1063
1064 function updateHostLabel(d) {
1065 var label = hostLabel(d),
1066 host = d.el;
1067
1068 host.select('text').text(label);
1069 }
1070
Simon Huntbb282f52014-11-10 11:08:19 -08001071 function updateDeviceState(nodeData) {
1072 nodeData.el.classed('online', nodeData.online);
1073 updateDeviceLabel(nodeData);
1074 // TODO: review what else might need to be updated
1075 }
1076
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001077 function updateLinkState(linkData) {
1078 updateLinkWidth(linkData);
1079 // TODO: review what else might need to be updated
1080 // update label, if showing
1081 }
1082
Simon Huntbb282f52014-11-10 11:08:19 -08001083 function updateHostState(hostData) {
1084 updateHostLabel(hostData);
1085 // TODO: review what else might need to be updated
1086 }
1087
Simon Hunt6ac93f32014-11-13 12:17:27 -08001088 function nodeMouseOver(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001089 hovered = d;
Simon Huntd72bc702014-11-13 18:38:04 -08001090 if (trafficHover() && d.class === 'host') {
1091 showTrafficAction();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001092 }
1093 }
1094
1095 function nodeMouseOut(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001096 hovered = null;
Simon Huntd72bc702014-11-13 18:38:04 -08001097 if (trafficHover() && d.class === 'host') {
1098 showTrafficAction();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001099 }
1100 }
Simon Huntbb282f52014-11-10 11:08:19 -08001101
Simon Hunt99c13842014-11-06 18:23:12 -08001102 function updateNodes() {
1103 node = nodeG.selectAll('.node')
1104 .data(network.nodes, function (d) { return d.id; });
1105
1106 // operate on existing nodes, if necessary
Simon Hunt7cd48f32014-11-09 23:42:50 -08001107 // update host labels
Simon Hunt99c13842014-11-06 18:23:12 -08001108 //node .foo() .bar() ...
1109
1110 // operate on entering nodes:
1111 var entering = node.enter()
1112 .append('g')
1113 .attr({
1114 id: function (d) { return safeId(d.id); },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001115 class: mkSvgClass,
Simon Hunt99c13842014-11-06 18:23:12 -08001116 transform: function (d) { return translate(d.x, d.y); },
1117 opacity: 0
1118 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08001119 .call(network.drag)
Simon Hunt6ac93f32014-11-13 12:17:27 -08001120 .on('mouseover', nodeMouseOver)
1121 .on('mouseout', nodeMouseOut)
Simon Hunt99c13842014-11-06 18:23:12 -08001122 .transition()
1123 .attr('opacity', 1);
1124
1125 // augment device nodes...
1126 entering.filter('.device').each(function (d) {
1127 var node = d3.select(this),
1128 icon = iconUrl(d),
Simon Hunt7cd48f32014-11-09 23:42:50 -08001129 label = niceLabel(deviceLabel(d)),
Simon Hunt99c13842014-11-06 18:23:12 -08001130 box;
1131
Simon Hunt7cd48f32014-11-09 23:42:50 -08001132 // provide ref to element from backing data....
1133 d.el = node;
1134
Simon Hunt99c13842014-11-06 18:23:12 -08001135 node.append('rect')
1136 .attr({
1137 'rx': 5,
1138 'ry': 5
1139 });
1140
1141 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001142 .text(label)
Simon Hunt99c13842014-11-06 18:23:12 -08001143 .attr('dy', '1.1em');
1144
1145 box = adjustRectToFitText(node);
1146
1147 node.select('rect')
1148 .attr(box);
1149
1150 if (icon) {
1151 var cfg = config.icons;
1152 node.append('svg:image')
1153 .attr({
1154 x: box.x + config.icons.xoff,
1155 y: box.y + config.icons.yoff,
1156 width: cfg.w,
1157 height: cfg.h,
1158 'xlink:href': icon
1159 });
1160 }
1161
1162 // debug function to show the modelled x,y coordinates of nodes...
1163 if (debug('showNodeXY')) {
1164 node.select('rect').attr('fill-opacity', 0.5);
1165 node.append('circle')
1166 .attr({
1167 class: 'debug',
1168 cx: 0,
1169 cy: 0,
1170 r: '3px'
1171 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001172 }
1173 });
Simon Hunt934c3ce2014-11-05 11:45:07 -08001174
Simon Hunt56d51852014-11-09 13:03:35 -08001175 // augment host nodes...
1176 entering.filter('.host').each(function (d) {
1177 var node = d3.select(this),
Simon Hunt56d51852014-11-09 13:03:35 -08001178 box;
1179
Simon Hunt7cd48f32014-11-09 23:42:50 -08001180 // provide ref to element from backing data....
1181 d.el = node;
1182
Simon Hunt56d51852014-11-09 13:03:35 -08001183 node.append('circle')
1184 .attr('r', 8); // TODO: define host circle radius
1185
Simon Hunt56d51852014-11-09 13:03:35 -08001186 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001187 .text(hostLabel)
1188 .attr('dy', '1.3em')
1189 .attr('text-anchor', 'middle');
Simon Hunt56d51852014-11-09 13:03:35 -08001190
1191 // debug function to show the modelled x,y coordinates of nodes...
1192 if (debug('showNodeXY')) {
1193 node.select('circle').attr('fill-opacity', 0.5);
1194 node.append('circle')
1195 .attr({
1196 class: 'debug',
1197 cx: 0,
1198 cy: 0,
1199 r: '3px'
1200 });
1201 }
1202 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001203
Simon Hunt99c13842014-11-06 18:23:12 -08001204 // operate on both existing and new nodes, if necessary
1205 //node .foo() .bar() ...
Simon Huntc7ee0662014-11-05 16:44:37 -08001206
Simon Hunt99c13842014-11-06 18:23:12 -08001207 // operate on exiting nodes:
Simon Huntea80eb42014-11-11 13:46:57 -08001208 // Note that the node is removed after 2 seconds.
1209 // Sub element animations should be shorter than 2 seconds.
1210 var exiting = node.exit()
Simon Hunt44031102014-11-11 13:20:36 -08001211 .transition()
1212 .duration(2000)
Simon Huntea80eb42014-11-11 13:46:57 -08001213 .style('opacity', 0)
Simon Hunt99c13842014-11-06 18:23:12 -08001214 .remove();
Simon Huntea80eb42014-11-11 13:46:57 -08001215
1216 // host node exits....
1217 exiting.filter('.host').each(function (d) {
1218 var node = d3.select(this);
1219
1220 node.select('text')
1221 .style('opacity', 0.5)
1222 .transition()
1223 .duration(1000)
1224 .style('opacity', 0);
1225 // note, leave <g>.remove to remove this element
1226
1227 node.select('circle')
1228 .style('stroke-fill', '#555')
1229 .style('fill', '#888')
1230 .style('opacity', 0.5)
1231 .transition()
1232 .duration(1500)
1233 .attr('r', 0);
1234 // note, leave <g>.remove to remove this element
1235
1236 });
1237
1238 // TODO: device node exits
Simon Huntc7ee0662014-11-05 16:44:37 -08001239 }
1240
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001241 function find(id, array) {
1242 for (var idx = 0, n = array.length; idx < n; idx++) {
1243 if (array[idx].id === id) {
1244 return idx;
1245 }
1246 }
1247 return -1;
1248 }
1249
1250 function removeLinkElement(linkData) {
1251 // remove from lookup cache
1252 delete network.lookup[linkData.id];
1253 // remove from links array
1254 var idx = find(linkData.id, network.links);
Simon Huntfc274c92014-11-11 11:05:46 -08001255 network.links.splice(idx, 1);
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001256 // remove from SVG
1257 updateLinks();
Simon Hunt44031102014-11-11 13:20:36 -08001258 network.force.resume();
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001259 }
Simon Huntc7ee0662014-11-05 16:44:37 -08001260
Simon Hunt44031102014-11-11 13:20:36 -08001261 function removeHostElement(hostData) {
1262 // first, remove associated hostLink...
1263 removeLinkElement(hostData.linkData);
1264
1265 // remove from lookup cache
1266 delete network.lookup[hostData.id];
1267 // remove from nodes array
1268 var idx = find(hostData.id, network.nodes);
1269 network.nodes.splice(idx, 1);
1270 // remove from SVG
1271 updateNodes();
1272 network.force.resume();
1273 }
1274
1275
Simon Huntc7ee0662014-11-05 16:44:37 -08001276 function tick() {
1277 node.attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001278 transform: function (d) { return translate(d.x, d.y); }
Simon Huntc7ee0662014-11-05 16:44:37 -08001279 });
1280
1281 link.attr({
1282 x1: function (d) { return d.source.x; },
1283 y1: function (d) { return d.source.y; },
1284 x2: function (d) { return d.target.x; },
1285 y2: function (d) { return d.target.y; }
1286 });
1287 }
Simon Hunt934c3ce2014-11-05 11:45:07 -08001288
1289 // ==============================
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001290 // Web-Socket for live data
1291
1292 function webSockUrl() {
1293 return document.location.toString()
1294 .replace(/\#.*/, '')
1295 .replace('http://', 'ws://')
1296 .replace('https://', 'wss://')
1297 .replace('index2.html', config.webSockUrl);
1298 }
1299
1300 webSock = {
1301 ws : null,
1302
1303 connect : function() {
1304 webSock.ws = new WebSocket(webSockUrl());
1305
1306 webSock.ws.onopen = function() {
Simon Hunt0c6d4192014-11-12 12:07:10 -08001307 noWebSock(false);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001308 };
1309
1310 webSock.ws.onmessage = function(m) {
1311 if (m.data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001312 wsTraceRx(m.data);
Thomas Vachuskad472c6e2014-11-07 19:11:05 -08001313 handleServerEvent(JSON.parse(m.data));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001314 }
1315 };
1316
1317 webSock.ws.onclose = function(m) {
1318 webSock.ws = null;
Simon Hunt0c6d4192014-11-12 12:07:10 -08001319 noWebSock(true);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001320 };
1321 },
1322
1323 send : function(text) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001324 if (text != null) {
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001325 webSock._send(text);
1326 }
1327 },
1328
1329 _send : function(message) {
1330 if (webSock.ws) {
1331 webSock.ws.send(message);
Simon Hunta255a2c2014-11-13 22:29:35 -08001332 } else if (config.useLiveData) {
Simon Hunt56d51852014-11-09 13:03:35 -08001333 network.view.alert('no web socket open\n\n' + message);
Simon Hunta255a2c2014-11-13 22:29:35 -08001334 } else {
1335 console.log('WS Send: ' + JSON.stringify(message));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001336 }
1337 }
1338
1339 };
1340
Simon Hunt0c6d4192014-11-12 12:07:10 -08001341 function noWebSock(b) {
1342 mask.style('display',b ? 'block' : 'none');
1343 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001344
Simon Hunt61d04042014-11-11 17:27:16 -08001345 // TODO: use cache of pending messages (key = sid) to reconcile responses
1346
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001347 function sendMessage(evType, payload) {
1348 var toSend = {
Simon Huntbb282f52014-11-10 11:08:19 -08001349 event: evType,
1350 sid: ++sid,
1351 payload: payload
1352 },
1353 asText = JSON.stringify(toSend);
1354 wsTraceTx(asText);
1355 webSock.send(asText);
1356 }
1357
1358 function wsTraceTx(msg) {
1359 wsTrace('tx', msg);
1360 }
1361 function wsTraceRx(msg) {
1362 wsTrace('rx', msg);
1363 }
1364 function wsTrace(rxtx, msg) {
Simon Huntbb282f52014-11-10 11:08:19 -08001365 console.log('[' + rxtx + '] ' + msg);
1366 // TODO: integrate with trace view
1367 //if (trace) {
1368 // trace.output(rxtx, msg);
1369 //}
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001370 }
1371
1372
1373 // ==============================
1374 // Selection stuff
1375
1376 function selectObject(obj, el) {
1377 var n,
Simon Hunt01095ff2014-11-13 16:37:29 -08001378 srcEv = d3.event.sourceEvent,
1379 meta = srcEv.metaKey,
1380 shift = srcEv.shiftKey;
1381
Simon Huntdeab4322014-11-13 18:49:07 -08001382 if ((panZoom() && !meta) || (!panZoom() && meta)) {
Simon Hunt01095ff2014-11-13 16:37:29 -08001383 return;
1384 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001385
1386 if (el) {
1387 n = d3.select(el);
1388 } else {
1389 node.each(function(d) {
1390 if (d == obj) {
1391 n = d3.select(el = this);
1392 }
1393 });
1394 }
1395 if (!n) return;
1396
Simon Hunt01095ff2014-11-13 16:37:29 -08001397 if (shift && n.classed('selected')) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001398 deselectObject(obj.id);
Simon Hunt61d04042014-11-11 17:27:16 -08001399 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001400 return;
1401 }
1402
Simon Hunt01095ff2014-11-13 16:37:29 -08001403 if (!shift) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001404 deselectAll();
1405 }
1406
Simon Huntc31d5692014-11-12 13:27:18 -08001407 selections[obj.id] = { obj: obj, el: el };
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001408 selectOrder.push(obj.id);
1409
1410 n.classed('selected', true);
Simon Hunt61d04042014-11-11 17:27:16 -08001411 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001412 }
1413
1414 function deselectObject(id) {
Simon Huntc31d5692014-11-12 13:27:18 -08001415 var obj = selections[id],
1416 idx;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001417 if (obj) {
1418 d3.select(obj.el).classed('selected', false);
Simon Hunt61d04042014-11-11 17:27:16 -08001419 delete selections[id];
Simon Huntc31d5692014-11-12 13:27:18 -08001420 idx = $.inArray(id, selectOrder);
1421 if (idx >= 0) {
1422 selectOrder.splice(idx, 1);
1423 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001424 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001425 }
1426
1427 function deselectAll() {
1428 // deselect all nodes in the network...
1429 node.classed('selected', false);
1430 selections = {};
1431 selectOrder = [];
Simon Hunt61d04042014-11-11 17:27:16 -08001432 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001433 }
1434
Simon Hunt61d04042014-11-11 17:27:16 -08001435 // update the state of the detail pane, based on current selections
1436 function updateDetailPane() {
1437 var nSel = selectOrder.length;
1438 if (!nSel) {
1439 detailPane.hide();
Simon Huntd72bc702014-11-13 18:38:04 -08001440 showTrafficAction(); // sends cancelTraffic event
Simon Hunt61d04042014-11-11 17:27:16 -08001441 } else if (nSel === 1) {
1442 singleSelect();
1443 } else {
1444 multiSelect();
1445 }
1446 }
1447
1448 function singleSelect() {
1449 requestDetails();
Simon Huntb53e0682014-11-12 13:32:01 -08001450 // NOTE: detail pane will be shown from showDetails event callback
Simon Hunt61d04042014-11-11 17:27:16 -08001451 }
1452
1453 function multiSelect() {
Simon Huntb53e0682014-11-12 13:32:01 -08001454 populateMultiSelect();
Simon Huntb53e0682014-11-12 13:32:01 -08001455 }
1456
1457 function addSep(tbody) {
1458 var tr = tbody.append('tr');
1459 $('<hr>').appendTo(tr.append('td').attr('colspan', 2));
1460 }
1461
1462 function addProp(tbody, label, value) {
1463 var tr = tbody.append('tr');
1464
1465 tr.append('td')
1466 .attr('class', 'label')
1467 .text(label + ' :');
1468
1469 tr.append('td')
1470 .attr('class', 'value')
1471 .text(value);
1472 }
1473
1474 function populateMultiSelect() {
1475 detailPane.empty();
1476
1477 var title = detailPane.append("h2"),
1478 table = detailPane.append("table"),
1479 tbody = table.append("tbody");
1480
1481 title.text('Multi-Select...');
1482
1483 selectOrder.forEach(function (d, i) {
1484 addProp(tbody, i+1, d);
1485 });
Simon Huntd72bc702014-11-13 18:38:04 -08001486
1487 addMultiSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08001488 }
1489
1490 function populateDetails(data) {
1491 detailPane.empty();
1492
1493 var title = detailPane.append("h2"),
1494 table = detailPane.append("table"),
1495 tbody = table.append("tbody");
1496
1497 $('<img src="img/' + data.type + '.png">').appendTo(title);
1498 $('<span>').attr('class', 'icon').text(data.id).appendTo(title);
1499
1500 data.propOrder.forEach(function(p) {
1501 if (p === '-') {
1502 addSep(tbody);
1503 } else {
1504 addProp(tbody, p, data.props[p]);
1505 }
1506 });
Simon Huntd72bc702014-11-13 18:38:04 -08001507
1508 addSingleSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08001509 }
1510
Simon Huntd72bc702014-11-13 18:38:04 -08001511 function addSingleSelectActions() {
1512 detailPane.append('hr');
1513 // always want to allow 'show traffic'
Simon Hunta5e89142014-11-14 07:00:33 -08001514 addAction(detailPane, 'Show Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001515 }
1516
1517 function addMultiSelectActions() {
1518 detailPane.append('hr');
1519 // always want to allow 'show traffic'
Simon Hunta5e89142014-11-14 07:00:33 -08001520 addAction(detailPane, 'Show Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001521 // if exactly two hosts are selected, also want 'add host intent'
1522 if (nSel() === 2 && allSelectionsClass('host')) {
Simon Hunta5e89142014-11-14 07:00:33 -08001523 addAction(detailPane, 'Add Host Intent', addIntentAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001524 }
1525 }
1526
Simon Hunta5e89142014-11-14 07:00:33 -08001527 function addAction(panel, text, cb) {
1528 panel.append('div')
Simon Huntd72bc702014-11-13 18:38:04 -08001529 .classed('actionBtn', true)
1530 .text(text)
1531 .on('click', cb);
1532 }
1533
1534
Paul Greysonfcba0e82014-11-13 10:21:16 -08001535 function zoomPan(scale, translate) {
1536 zoomPanContainer.attr("transform", "translate(" + translate + ")scale(" + scale + ")");
1537 // keep the map lines constant width while zooming
1538 bgImg.style("stroke-width", 2.0 / scale + "px");
1539 }
1540
1541 function resetZoomPan() {
1542 zoomPan(1, [0,0]);
1543 zoom.scale(1).translate([0,0]);
1544 }
1545
1546 function setupZoomPan() {
1547 function zoomed() {
Simon Huntdeab4322014-11-13 18:49:07 -08001548 if (!panZoom() ^ !d3.event.sourceEvent.metaKey) {
Paul Greysonfcba0e82014-11-13 10:21:16 -08001549 zoomPan(d3.event.scale, d3.event.translate);
1550 }
1551 }
1552
1553 zoom = d3.behavior.zoom()
1554 .translate([0, 0])
1555 .scale(1)
1556 .scaleExtent([1, 8])
1557 .on("zoom", zoomed);
1558
1559 svg.call(zoom);
1560 }
1561
Simon Hunt61d04042014-11-11 17:27:16 -08001562 // ==============================
1563 // Test harness code
Simon Hunt56d51852014-11-09 13:03:35 -08001564
1565 function prepareScenario(view, ctx, dbg) {
1566 var sc = scenario,
1567 urlSc = sc.evDir + ctx + sc.evScenario;
1568
1569 if (!ctx) {
1570 view.alert("No scenario specified (null ctx)");
1571 return;
1572 }
1573
1574 sc.view = view;
1575 sc.ctx = ctx;
1576 sc.debug = dbg;
1577 sc.evNumber = 0;
1578
1579 d3.json(urlSc, function(err, data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001580 var p = data && data.params || {},
1581 desc = data && data.description || null,
Simon Huntfc274c92014-11-11 11:05:46 -08001582 intro = data && data.title;
Simon Huntbb282f52014-11-10 11:08:19 -08001583
Simon Hunt56d51852014-11-09 13:03:35 -08001584 if (err) {
1585 view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err);
1586 } else {
1587 sc.params = p;
Simon Huntbb282f52014-11-10 11:08:19 -08001588 if (desc) {
1589 intro += '\n\n ' + desc.join('\n ');
1590 }
1591 view.alert(intro);
Simon Hunt56d51852014-11-09 13:03:35 -08001592 }
1593 });
1594
1595 }
1596
Simon Hunt01095ff2014-11-13 16:37:29 -08001597 // ==============================
1598 // Toggle Buttons in masthead
Simon Hunt0c6d4192014-11-12 12:07:10 -08001599
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001600 // TODO: toggle button (and other widgets in the masthead) should be provided
1601 // by the framework; not generated by the view.
1602
Simon Hunta5e89142014-11-14 07:00:33 -08001603 var showInstances,
1604 doPanZoom,
1605 showTrafficOnHover;
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001606
1607 function addButtonBar(view) {
1608 var bb = d3.select('#mast')
1609 .append('span').classed('right', true).attr('id', 'bb');
1610
Simon Hunta5e89142014-11-14 07:00:33 -08001611 function mkTogBtn(text, cb) {
1612 return bb.append('span')
1613 .classed('btn', true)
1614 .text(text)
1615 .on('click', cb);
1616 }
Simon Hunt01095ff2014-11-13 16:37:29 -08001617
Simon Hunta5e89142014-11-14 07:00:33 -08001618 showInstances = mkTogBtn('Show Instances', toggleInst);
1619 doPanZoom = mkTogBtn('Pan/Zoom', togglePanZoom);
1620 showTrafficOnHover = mkTogBtn('Show traffic on hover', toggleTrafficHover);
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001621 }
1622
Simon Hunta5e89142014-11-14 07:00:33 -08001623 function instShown() {
1624 return showInstances.classed('active');
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001625 }
Simon Hunta5e89142014-11-14 07:00:33 -08001626 function toggleInst() {
1627 showInstances.classed('active', !instShown());
1628 if (instShown()) {
1629 oiBox.show();
1630 } else {
1631 oiBox.hide();
1632 }
Simon Hunt01095ff2014-11-13 16:37:29 -08001633 }
1634
Simon Huntdeab4322014-11-13 18:49:07 -08001635 function panZoom() {
1636 return doPanZoom.classed('active');
Simon Hunt01095ff2014-11-13 16:37:29 -08001637 }
Simon Hunta5e89142014-11-14 07:00:33 -08001638 function togglePanZoom() {
1639 doPanZoom.classed('active', !panZoom());
1640 }
1641
1642 function trafficHover() {
1643 return showTrafficOnHover.classed('active');
1644 }
1645 function toggleTrafficHover() {
1646 showTrafficOnHover.classed('active', !trafficHover());
1647 }
1648
Simon Hunt01095ff2014-11-13 16:37:29 -08001649
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001650 // ==============================
Simon Hunt142d0032014-11-04 20:13:09 -08001651 // View life-cycle callbacks
Simon Hunt195cb382014-11-03 17:50:51 -08001652
Simon Huntf67722a2014-11-10 09:32:06 -08001653 function preload(view, ctx, flags) {
Simon Hunt142d0032014-11-04 20:13:09 -08001654 var w = view.width(),
1655 h = view.height(),
Simon Huntc7ee0662014-11-05 16:44:37 -08001656 fcfg = config.force,
1657 fpad = fcfg.pad,
1658 forceDim = [w - 2*fpad, h - 2*fpad];
Simon Hunt195cb382014-11-03 17:50:51 -08001659
Simon Hunt142d0032014-11-04 20:13:09 -08001660 // NOTE: view.$div is a D3 selection of the view's div
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001661 var viewBox = '0 0 ' + config.logicalSize + ' ' + config.logicalSize;
1662 svg = view.$div.append('svg').attr('viewBox', viewBox);
Simon Hunt934c3ce2014-11-05 11:45:07 -08001663 setSize(svg, view);
1664
Simon Hunt12ce12e2014-11-15 21:13:19 -08001665 var defs = svg.append('defs');
1666 gly.defBird(defs);
1667
Paul Greysonfcba0e82014-11-13 10:21:16 -08001668 zoomPanContainer = svg.append('g').attr('id', 'zoomPanContainer');
Paul Greysonfcba0e82014-11-13 10:21:16 -08001669 setupZoomPan();
1670
Simon Hunt1a9eff92014-11-07 11:06:34 -08001671 // add blue glow filter to svg layer
Paul Greysonfcba0e82014-11-13 10:21:16 -08001672 d3u.appendGlow(zoomPanContainer);
Simon Hunt1a9eff92014-11-07 11:06:34 -08001673
Simon Huntc7ee0662014-11-05 16:44:37 -08001674 // group for the topology
Paul Greysonfcba0e82014-11-13 10:21:16 -08001675 topoG = zoomPanContainer.append('g')
Simon Huntd3b7d512014-11-12 15:48:41 -08001676 .attr('id', 'topo-G')
Simon Huntc7ee0662014-11-05 16:44:37 -08001677 .attr('transform', fcfg.translate());
1678
1679 // subgroups for links and nodes
1680 linkG = topoG.append('g').attr('id', 'links');
1681 nodeG = topoG.append('g').attr('id', 'nodes');
1682
1683 // selection of nodes and links
1684 link = linkG.selectAll('.link');
1685 node = nodeG.selectAll('.node');
1686
Simon Hunt7cd48f32014-11-09 23:42:50 -08001687 function chrg(d) {
1688 return fcfg.charge[d.class] || -12000;
1689 }
Simon Hunt99c13842014-11-06 18:23:12 -08001690 function ldist(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08001691 return fcfg.linkDistance[d.type] || 50;
Simon Hunt99c13842014-11-06 18:23:12 -08001692 }
1693 function lstrg(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08001694 // 0.0 - 1.0
1695 return fcfg.linkStrength[d.type] || 1.0;
Simon Hunt99c13842014-11-06 18:23:12 -08001696 }
1697
Simon Hunt1a9eff92014-11-07 11:06:34 -08001698 function selectCb(d, self) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001699 selectObject(d, self);
Simon Hunt1a9eff92014-11-07 11:06:34 -08001700 }
1701
1702 function atDragEnd(d, self) {
Simon Hunt56d51852014-11-09 13:03:35 -08001703 // once we've finished moving, pin the node in position
1704 d.fixed = true;
1705 d3.select(self).classed('fixed', true);
1706 if (config.useLiveData) {
Simon Hunt902c9922014-11-11 11:59:31 -08001707 sendUpdateMeta(d);
Simon Hunta255a2c2014-11-13 22:29:35 -08001708 } else {
1709 console.log('Moving node ' + d.id + ' to [' + d.x + ',' + d.y + ']');
Simon Hunt1a9eff92014-11-07 11:06:34 -08001710 }
1711 }
1712
Simon Hunt902c9922014-11-11 11:59:31 -08001713 function sendUpdateMeta(d) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001714 sendMessage('updateMeta', {
1715 id: d.id,
1716 'class': d.class,
Simon Hunt902c9922014-11-11 11:59:31 -08001717 'memento': {
Simon Hunt01095ff2014-11-13 16:37:29 -08001718 x: d.x,
1719 y: d.y
Simon Hunt902c9922014-11-11 11:59:31 -08001720 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001721 });
1722 }
1723
Simon Huntc7ee0662014-11-05 16:44:37 -08001724 // set up the force layout
1725 network.force = d3.layout.force()
1726 .size(forceDim)
1727 .nodes(network.nodes)
1728 .links(network.links)
Simon Hunt7cd48f32014-11-09 23:42:50 -08001729 .gravity(0.4)
1730 .friction(0.7)
1731 .charge(chrg)
Simon Hunt99c13842014-11-06 18:23:12 -08001732 .linkDistance(ldist)
1733 .linkStrength(lstrg)
Simon Huntc7ee0662014-11-05 16:44:37 -08001734 .on('tick', tick);
Simon Hunt195cb382014-11-03 17:50:51 -08001735
Simon Hunt01095ff2014-11-13 16:37:29 -08001736 network.drag = d3u.createDragBehavior(network.force,
Simon Huntdeab4322014-11-13 18:49:07 -08001737 selectCb, atDragEnd, panZoom);
Simon Hunt0c6d4192014-11-12 12:07:10 -08001738
1739 // create mask layer for when we lose connection to server.
Simon Hunta5e89142014-11-14 07:00:33 -08001740 // TODO: this should be part of the framework
Simon Hunt0c6d4192014-11-12 12:07:10 -08001741 mask = view.$div.append('div').attr('id','topo-mask');
1742 para(mask, 'Oops!');
1743 para(mask, 'Web-socket connection to server closed...');
1744 para(mask, 'Try refreshing the page.');
Simon Hunt12ce12e2014-11-15 21:13:19 -08001745
1746 mask.append('svg')
1747 .attr({
1748 id: 'mask-bird',
1749 width: w,
1750 height: h
1751 })
1752 .append('g')
1753 .attr('transform', birdTranslate(w, h))
1754 .style('opacity', 0.3)
1755 .append('use')
1756 .attr({
1757 'xlink:href': '#bird',
1758 width: config.birdDim,
1759 height: config.birdDim,
1760 fill: '#111'
1761 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08001762 }
Simon Hunt195cb382014-11-03 17:50:51 -08001763
Simon Hunt01095ff2014-11-13 16:37:29 -08001764 function para(sel, text) {
1765 sel.append('p').text(text);
1766 }
1767
1768
Simon Hunt56d51852014-11-09 13:03:35 -08001769 function load(view, ctx, flags) {
Simon Huntf67722a2014-11-10 09:32:06 -08001770 // resize, in case the window was resized while we were not loaded
1771 resize(view, ctx, flags);
1772
Simon Hunt99c13842014-11-06 18:23:12 -08001773 // cache the view token, so network topo functions can access it
1774 network.view = view;
Simon Hunt56d51852014-11-09 13:03:35 -08001775 config.useLiveData = !flags.local;
1776
1777 if (!config.useLiveData) {
1778 prepareScenario(view, ctx, flags.debug);
1779 }
Simon Hunt99c13842014-11-06 18:23:12 -08001780
1781 // set our radio buttons and key bindings
Simon Hunt9462e8c2014-11-14 17:28:09 -08001782 layerBtnSet = view.setRadio(layerButtons);
Simon Hunt934c3ce2014-11-05 11:45:07 -08001783 view.setKeys(keyDispatch);
Simon Hunt195cb382014-11-03 17:50:51 -08001784
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001785 // patch in our "button bar" for now
1786 // TODO: implement a more official frameworky way of doing this..
1787 addButtonBar(view);
1788
Simon Huntd3b7d512014-11-12 15:48:41 -08001789 // Load map data asynchronously; complete startup after that..
1790 loadGeoJsonData();
Simon Hunta255a2c2014-11-13 22:29:35 -08001791
1792 // start the and timer
1793 var dashIdx = 0;
1794 antTimer = setInterval(function () {
1795 // TODO: figure out how to choose Src-->Dst and Dst-->Src, per link
1796 dashIdx = dashIdx === 0 ? 14 : dashIdx - 2;
1797 d3.selectAll('.animated').style('stroke-dashoffset', dashIdx);
1798 }, 35);
1799 }
1800
1801 function unload(view, ctx, flags) {
1802 if (antTimer) {
1803 clearInterval(antTimer);
1804 antTimer = null;
1805 }
Simon Huntd3b7d512014-11-12 15:48:41 -08001806 }
1807
1808 // TODO: move these to config/state portion of script
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001809 var geoJsonUrl = 'json/map/continental_us.json', // TODO: Paul
Simon Huntd3b7d512014-11-12 15:48:41 -08001810 geoJson;
1811
1812 function loadGeoJsonData() {
1813 d3.json(geoJsonUrl, function (err, data) {
1814 if (err) {
1815 // fall back to USA map background
1816 loadStaticMap();
1817 } else {
1818 geoJson = data;
1819 loadGeoMap();
1820 }
1821
1822 // finally, connect to the server...
1823 if (config.useLiveData) {
1824 webSock.connect();
1825 }
1826 });
1827 }
1828
1829 function showBg() {
1830 return config.options.showBackground ? 'visible' : 'hidden';
1831 }
1832
1833 function loadStaticMap() {
1834 fnTrace('loadStaticMap', config.backgroundUrl);
1835 var w = network.view.width(),
1836 h = network.view.height();
1837
1838 // load the background image
1839 bgImg = svg.insert('svg:image', '#topo-G')
1840 .attr({
1841 id: 'topo-bg',
1842 width: w,
1843 height: h,
1844 'xlink:href': config.backgroundUrl
1845 })
1846 .style({
1847 visibility: showBg()
1848 });
1849 }
1850
1851 function loadGeoMap() {
1852 fnTrace('loadGeoMap', geoJsonUrl);
Simon Huntd3b7d512014-11-12 15:48:41 -08001853
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001854 // extracts the topojson data into geocoordinate-based geometry
1855 var topoData = topojson.feature(geoJson, geoJson.objects.states);
Simon Huntd3b7d512014-11-12 15:48:41 -08001856
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001857 // see: http://bl.ocks.org/mbostock/4707858
1858 geoMapProjection = d3.geo.mercator();
1859 var path = d3.geo.path().projection(geoMapProjection);
Simon Huntd3b7d512014-11-12 15:48:41 -08001860
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001861 geoMapProjection
1862 .scale(1)
1863 .translate([0, 0]);
Simon Huntd3b7d512014-11-12 15:48:41 -08001864
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001865 // [[x1,y1],[x2,y2]]
1866 var b = path.bounds(topoData);
Paul Greysonfcba0e82014-11-13 10:21:16 -08001867 // size map to 95% of minimum dimension to fill space
1868 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 -08001869 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 -08001870
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001871 geoMapProjection
1872 .scale(s)
1873 .translate(t);
1874
Paul Greysonfcba0e82014-11-13 10:21:16 -08001875 bgImg = zoomPanContainer.insert("g", '#topo-G');
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001876 bgImg.attr('id', 'map').selectAll('path')
1877 .data(topoData.features)
1878 .enter()
1879 .append('path')
1880 .attr('d', path);
Simon Hunt195cb382014-11-03 17:50:51 -08001881 }
1882
Simon Huntf67722a2014-11-10 09:32:06 -08001883 function resize(view, ctx, flags) {
Simon Hunt12ce12e2014-11-15 21:13:19 -08001884 var w = view.width(),
1885 h = view.height();
1886
Simon Hunt934c3ce2014-11-05 11:45:07 -08001887 setSize(svg, view);
Simon Hunt12ce12e2014-11-15 21:13:19 -08001888
1889 d3.select('#mask-bird').attr({ width: w, height: h})
1890 .select('g').attr('transform', birdTranslate(w, h));
Simon Hunt142d0032014-11-04 20:13:09 -08001891 }
1892
Simon Hunt12ce12e2014-11-15 21:13:19 -08001893 function birdTranslate(w, h) {
1894 var bdim = config.birdDim;
1895 return 'translate('+((w-bdim)*.4)+','+((h-bdim)*.1)+')';
1896 }
Simon Hunt142d0032014-11-04 20:13:09 -08001897
1898 // ==============================
1899 // View registration
Simon Hunt195cb382014-11-03 17:50:51 -08001900
Simon Hunt25248912014-11-04 11:25:48 -08001901 onos.ui.addView('topo', {
Simon Hunt142d0032014-11-04 20:13:09 -08001902 preload: preload,
1903 load: load,
Simon Hunta255a2c2014-11-13 22:29:35 -08001904 unload: unload,
Simon Hunt142d0032014-11-04 20:13:09 -08001905 resize: resize
Simon Hunt195cb382014-11-03 17:50:51 -08001906 });
1907
Simon Hunt61d04042014-11-11 17:27:16 -08001908 detailPane = onos.ui.addFloatingPanel('topo-detail');
Simon Hunta5e89142014-11-14 07:00:33 -08001909 oiBox = onos.ui.addFloatingPanel('topo-oibox', 'TL');
Simon Hunt61d04042014-11-11 17:27:16 -08001910
Simon Hunt195cb382014-11-03 17:50:51 -08001911}(ONOS));