blob: 37e098ebc44b5c83e4e90087854bd366dd0d2f91 [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 Vachuskabadb93f2014-11-15 23:51:17 -0800828 svgClass: (type ? 'link ' + type : 'link')
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 })
Thomas Vachuskabadb93f2014-11-15 23:51:17 -0800866 .classed('inactive', function(d) { return !d.online; })
Simon Hunt99c13842014-11-06 18:23:12 -0800867 .transition().duration(1000)
868 .attr({
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800869 'stroke-width': function (d) { return linkScale(d.linkWidth); },
Simon Hunt99c13842014-11-06 18:23:12 -0800870 stroke: '#666' // TODO: remove explicit stroke, rather...
871 });
872
873 // augment links
Simon Hunt7cd48f32014-11-09 23:42:50 -0800874 entering.each(function (d) {
875 var link = d3.select(this);
876 // provide ref to element selection from backing data....
877 d.el = link;
Simon Hunt99c13842014-11-06 18:23:12 -0800878
Simon Hunt7cd48f32014-11-09 23:42:50 -0800879 // TODO: add src/dst port labels etc.
880 });
Thomas Vachuska4830d392014-11-09 17:09:56 -0800881
882 // operate on both existing and new links, if necessary
883 //link .foo() .bar() ...
884
885 // operate on exiting links:
Thomas Vachuska4830d392014-11-09 17:09:56 -0800886 link.exit()
Thomas Vachuska4830d392014-11-09 17:09:56 -0800887 .attr({
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800888 'stroke-dasharray': '3, 3'
Thomas Vachuska4830d392014-11-09 17:09:56 -0800889 })
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800890 .style('opacity', 0.4)
891 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -0800892 .duration(1500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800893 .attr({
894 'stroke-dasharray': '3, 12'
895 })
896 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -0800897 .duration(500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800898 .style('opacity', 0.0)
Thomas Vachuska4830d392014-11-09 17:09:56 -0800899 .remove();
Simon Hunt99c13842014-11-06 18:23:12 -0800900 }
901
902 function createDeviceNode(device) {
903 // start with the object as is
904 var node = device,
Simon Huntbb282f52014-11-10 11:08:19 -0800905 type = device.type,
906 svgCls = type ? 'node device ' + type : 'node device';
Simon Hunt99c13842014-11-06 18:23:12 -0800907
908 // Augment as needed...
909 node.class = 'device';
Simon Huntbb282f52014-11-10 11:08:19 -0800910 node.svgClass = device.online ? svgCls + ' online' : svgCls;
Simon Hunt99c13842014-11-06 18:23:12 -0800911 positionNode(node);
912
913 // cache label array length
914 network.deviceLabelCount = device.labels.length;
Simon Hunt99c13842014-11-06 18:23:12 -0800915 return node;
916 }
917
Simon Hunt56d51852014-11-09 13:03:35 -0800918 function createHostNode(host) {
919 // start with the object as is
920 var node = host;
921
922 // Augment as needed...
923 node.class = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -0800924 if (!node.type) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800925 node.type = 'endstation';
926 }
Simon Hunt56d51852014-11-09 13:03:35 -0800927 node.svgClass = 'node host';
Simon Hunt56d51852014-11-09 13:03:35 -0800928 positionNode(node);
929
930 // cache label array length
931 network.hostLabelCount = host.labels.length;
Simon Hunt56d51852014-11-09 13:03:35 -0800932 return node;
933 }
934
Simon Hunt99c13842014-11-06 18:23:12 -0800935 function positionNode(node) {
936 var meta = node.metaUi,
Simon Huntac9e24f2014-11-12 10:12:21 -0800937 x = meta && meta.x,
938 y = meta && meta.y,
939 xy;
Simon Hunt99c13842014-11-06 18:23:12 -0800940
Simon Huntac9e24f2014-11-12 10:12:21 -0800941 // If we have [x,y] already, use that...
Simon Hunt99c13842014-11-06 18:23:12 -0800942 if (x && y) {
943 node.fixed = true;
Simon Huntac9e24f2014-11-12 10:12:21 -0800944 node.x = x;
945 node.y = y;
946 return;
Simon Hunt99c13842014-11-06 18:23:12 -0800947 }
Simon Huntac9e24f2014-11-12 10:12:21 -0800948
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800949 var location = node.location;
950 if (location && location.type === 'latlng') {
951 var coord = geoMapProjection([location.lng, location.lat]);
952 node.fixed = true;
953 node.x = coord[0];
954 node.y = coord[1];
955 return;
956 }
957
Simon Huntac9e24f2014-11-12 10:12:21 -0800958 // Note: Placing incoming unpinned nodes at exactly the same point
959 // (center of the view) causes them to explode outwards when
960 // the force layout kicks in. So, we spread them out a bit
961 // initially, to provide a more serene layout convergence.
962 // Additionally, if the node is a host, we place it near
963 // the device it is connected to.
964
965 function spread(s) {
966 return Math.floor((Math.random() * s) - s/2);
967 }
968
969 function randDim(dim) {
970 return dim / 2 + spread(dim * 0.7071);
971 }
972
973 function rand() {
974 return {
975 x: randDim(network.view.width()),
976 y: randDim(network.view.height())
977 };
978 }
979
980 function near(node) {
981 var min = 12,
982 dx = spread(12),
983 dy = spread(12);
984 return {
985 x: node.x + min + dx,
986 y: node.y + min + dy
987 };
988 }
989
990 function getDevice(cp) {
991 var d = network.lookup[cp.device];
992 return d || rand();
993 }
994
995 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
996 $.extend(node, xy);
Simon Hunt99c13842014-11-06 18:23:12 -0800997 }
998
Simon Hunt99c13842014-11-06 18:23:12 -0800999 function iconUrl(d) {
1000 return 'img/' + d.type + '.png';
1001 }
1002
1003 // returns the newly computed bounding box of the rectangle
1004 function adjustRectToFitText(n) {
1005 var text = n.select('text'),
1006 box = text.node().getBBox(),
1007 lab = config.labels;
1008
1009 text.attr('text-anchor', 'middle')
1010 .attr('y', '-0.8em')
1011 .attr('x', lab.imgPad/2);
1012
1013 // translate the bbox so that it is centered on [x,y]
1014 box.x = -box.width / 2;
1015 box.y = -box.height / 2;
1016
1017 // add padding
1018 box.x -= (lab.padLR + lab.imgPad/2);
1019 box.width += lab.padLR * 2 + lab.imgPad;
1020 box.y -= lab.padTB;
1021 box.height += lab.padTB * 2;
1022
1023 return box;
1024 }
1025
Simon Hunt1a9eff92014-11-07 11:06:34 -08001026 function mkSvgClass(d) {
1027 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
1028 }
1029
Simon Hunt7cd48f32014-11-09 23:42:50 -08001030 function hostLabel(d) {
1031 var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0;
1032 return d.labels[idx];
1033 }
1034 function deviceLabel(d) {
1035 var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0;
1036 return d.labels[idx];
1037 }
1038 function niceLabel(label) {
1039 return (label && label.trim()) ? label : '.';
1040 }
1041
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001042 function updateDeviceLabel(d) {
1043 var label = niceLabel(deviceLabel(d)),
1044 node = d.el,
1045 box;
1046
1047 node.select('text')
1048 .text(label)
1049 .style('opacity', 0)
1050 .transition()
1051 .style('opacity', 1);
1052
1053 box = adjustRectToFitText(node);
1054
1055 node.select('rect')
1056 .transition()
1057 .attr(box);
1058
1059 node.select('image')
1060 .transition()
1061 .attr('x', box.x + config.icons.xoff)
1062 .attr('y', box.y + config.icons.yoff);
1063 }
1064
1065 function updateHostLabel(d) {
1066 var label = hostLabel(d),
1067 host = d.el;
1068
1069 host.select('text').text(label);
1070 }
1071
Simon Huntbb282f52014-11-10 11:08:19 -08001072 function updateDeviceState(nodeData) {
1073 nodeData.el.classed('online', nodeData.online);
1074 updateDeviceLabel(nodeData);
1075 // TODO: review what else might need to be updated
1076 }
1077
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001078 function updateLinkState(linkData) {
1079 updateLinkWidth(linkData);
Thomas Vachuskabadb93f2014-11-15 23:51:17 -08001080 linkData.el.classed('inactive', !linkData.online);
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001081 // TODO: review what else might need to be updated
1082 // update label, if showing
1083 }
1084
Simon Huntbb282f52014-11-10 11:08:19 -08001085 function updateHostState(hostData) {
1086 updateHostLabel(hostData);
1087 // TODO: review what else might need to be updated
1088 }
1089
Simon Hunt6ac93f32014-11-13 12:17:27 -08001090 function nodeMouseOver(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001091 hovered = d;
Simon Huntd72bc702014-11-13 18:38:04 -08001092 if (trafficHover() && d.class === 'host') {
1093 showTrafficAction();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001094 }
1095 }
1096
1097 function nodeMouseOut(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001098 hovered = null;
Simon Huntd72bc702014-11-13 18:38:04 -08001099 if (trafficHover() && d.class === 'host') {
1100 showTrafficAction();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001101 }
1102 }
Simon Huntbb282f52014-11-10 11:08:19 -08001103
Simon Hunt99c13842014-11-06 18:23:12 -08001104 function updateNodes() {
1105 node = nodeG.selectAll('.node')
1106 .data(network.nodes, function (d) { return d.id; });
1107
1108 // operate on existing nodes, if necessary
Simon Hunt7cd48f32014-11-09 23:42:50 -08001109 // update host labels
Simon Hunt99c13842014-11-06 18:23:12 -08001110 //node .foo() .bar() ...
1111
1112 // operate on entering nodes:
1113 var entering = node.enter()
1114 .append('g')
1115 .attr({
1116 id: function (d) { return safeId(d.id); },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001117 class: mkSvgClass,
Simon Hunt99c13842014-11-06 18:23:12 -08001118 transform: function (d) { return translate(d.x, d.y); },
1119 opacity: 0
1120 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08001121 .call(network.drag)
Simon Hunt6ac93f32014-11-13 12:17:27 -08001122 .on('mouseover', nodeMouseOver)
1123 .on('mouseout', nodeMouseOut)
Simon Hunt99c13842014-11-06 18:23:12 -08001124 .transition()
1125 .attr('opacity', 1);
1126
1127 // augment device nodes...
1128 entering.filter('.device').each(function (d) {
1129 var node = d3.select(this),
1130 icon = iconUrl(d),
Simon Hunt7cd48f32014-11-09 23:42:50 -08001131 label = niceLabel(deviceLabel(d)),
Simon Hunt99c13842014-11-06 18:23:12 -08001132 box;
1133
Simon Hunt7cd48f32014-11-09 23:42:50 -08001134 // provide ref to element from backing data....
1135 d.el = node;
1136
Simon Hunt99c13842014-11-06 18:23:12 -08001137 node.append('rect')
1138 .attr({
1139 'rx': 5,
1140 'ry': 5
1141 });
1142
1143 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001144 .text(label)
Simon Hunt99c13842014-11-06 18:23:12 -08001145 .attr('dy', '1.1em');
1146
1147 box = adjustRectToFitText(node);
1148
1149 node.select('rect')
1150 .attr(box);
1151
1152 if (icon) {
1153 var cfg = config.icons;
1154 node.append('svg:image')
1155 .attr({
1156 x: box.x + config.icons.xoff,
1157 y: box.y + config.icons.yoff,
1158 width: cfg.w,
1159 height: cfg.h,
1160 'xlink:href': icon
1161 });
1162 }
1163
1164 // debug function to show the modelled x,y coordinates of nodes...
1165 if (debug('showNodeXY')) {
1166 node.select('rect').attr('fill-opacity', 0.5);
1167 node.append('circle')
1168 .attr({
1169 class: 'debug',
1170 cx: 0,
1171 cy: 0,
1172 r: '3px'
1173 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001174 }
1175 });
Simon Hunt934c3ce2014-11-05 11:45:07 -08001176
Simon Hunt56d51852014-11-09 13:03:35 -08001177 // augment host nodes...
1178 entering.filter('.host').each(function (d) {
1179 var node = d3.select(this),
Simon Hunt56d51852014-11-09 13:03:35 -08001180 box;
1181
Simon Hunt7cd48f32014-11-09 23:42:50 -08001182 // provide ref to element from backing data....
1183 d.el = node;
1184
Simon Hunt56d51852014-11-09 13:03:35 -08001185 node.append('circle')
1186 .attr('r', 8); // TODO: define host circle radius
1187
Simon Hunt56d51852014-11-09 13:03:35 -08001188 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001189 .text(hostLabel)
1190 .attr('dy', '1.3em')
1191 .attr('text-anchor', 'middle');
Simon Hunt56d51852014-11-09 13:03:35 -08001192
1193 // debug function to show the modelled x,y coordinates of nodes...
1194 if (debug('showNodeXY')) {
1195 node.select('circle').attr('fill-opacity', 0.5);
1196 node.append('circle')
1197 .attr({
1198 class: 'debug',
1199 cx: 0,
1200 cy: 0,
1201 r: '3px'
1202 });
1203 }
1204 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001205
Simon Hunt99c13842014-11-06 18:23:12 -08001206 // operate on both existing and new nodes, if necessary
1207 //node .foo() .bar() ...
Simon Huntc7ee0662014-11-05 16:44:37 -08001208
Simon Hunt99c13842014-11-06 18:23:12 -08001209 // operate on exiting nodes:
Simon Huntea80eb42014-11-11 13:46:57 -08001210 // Note that the node is removed after 2 seconds.
1211 // Sub element animations should be shorter than 2 seconds.
1212 var exiting = node.exit()
Simon Hunt44031102014-11-11 13:20:36 -08001213 .transition()
1214 .duration(2000)
Simon Huntea80eb42014-11-11 13:46:57 -08001215 .style('opacity', 0)
Simon Hunt99c13842014-11-06 18:23:12 -08001216 .remove();
Simon Huntea80eb42014-11-11 13:46:57 -08001217
1218 // host node exits....
1219 exiting.filter('.host').each(function (d) {
1220 var node = d3.select(this);
1221
1222 node.select('text')
1223 .style('opacity', 0.5)
1224 .transition()
1225 .duration(1000)
1226 .style('opacity', 0);
1227 // note, leave <g>.remove to remove this element
1228
1229 node.select('circle')
1230 .style('stroke-fill', '#555')
1231 .style('fill', '#888')
1232 .style('opacity', 0.5)
1233 .transition()
1234 .duration(1500)
1235 .attr('r', 0);
1236 // note, leave <g>.remove to remove this element
1237
1238 });
1239
1240 // TODO: device node exits
Simon Huntc7ee0662014-11-05 16:44:37 -08001241 }
1242
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001243 function find(id, array) {
1244 for (var idx = 0, n = array.length; idx < n; idx++) {
1245 if (array[idx].id === id) {
1246 return idx;
1247 }
1248 }
1249 return -1;
1250 }
1251
1252 function removeLinkElement(linkData) {
1253 // remove from lookup cache
1254 delete network.lookup[linkData.id];
1255 // remove from links array
1256 var idx = find(linkData.id, network.links);
Simon Huntfc274c92014-11-11 11:05:46 -08001257 network.links.splice(idx, 1);
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001258 // remove from SVG
1259 updateLinks();
Simon Hunt44031102014-11-11 13:20:36 -08001260 network.force.resume();
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001261 }
Simon Huntc7ee0662014-11-05 16:44:37 -08001262
Simon Hunt44031102014-11-11 13:20:36 -08001263 function removeHostElement(hostData) {
1264 // first, remove associated hostLink...
1265 removeLinkElement(hostData.linkData);
1266
1267 // remove from lookup cache
1268 delete network.lookup[hostData.id];
1269 // remove from nodes array
1270 var idx = find(hostData.id, network.nodes);
1271 network.nodes.splice(idx, 1);
1272 // remove from SVG
1273 updateNodes();
1274 network.force.resume();
1275 }
1276
1277
Simon Huntc7ee0662014-11-05 16:44:37 -08001278 function tick() {
1279 node.attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001280 transform: function (d) { return translate(d.x, d.y); }
Simon Huntc7ee0662014-11-05 16:44:37 -08001281 });
1282
1283 link.attr({
1284 x1: function (d) { return d.source.x; },
1285 y1: function (d) { return d.source.y; },
1286 x2: function (d) { return d.target.x; },
1287 y2: function (d) { return d.target.y; }
1288 });
1289 }
Simon Hunt934c3ce2014-11-05 11:45:07 -08001290
1291 // ==============================
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001292 // Web-Socket for live data
1293
1294 function webSockUrl() {
1295 return document.location.toString()
1296 .replace(/\#.*/, '')
1297 .replace('http://', 'ws://')
1298 .replace('https://', 'wss://')
1299 .replace('index2.html', config.webSockUrl);
1300 }
1301
1302 webSock = {
1303 ws : null,
1304
1305 connect : function() {
1306 webSock.ws = new WebSocket(webSockUrl());
1307
1308 webSock.ws.onopen = function() {
Simon Hunt0c6d4192014-11-12 12:07:10 -08001309 noWebSock(false);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001310 };
1311
1312 webSock.ws.onmessage = function(m) {
1313 if (m.data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001314 wsTraceRx(m.data);
Thomas Vachuskad472c6e2014-11-07 19:11:05 -08001315 handleServerEvent(JSON.parse(m.data));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001316 }
1317 };
1318
1319 webSock.ws.onclose = function(m) {
1320 webSock.ws = null;
Simon Hunt0c6d4192014-11-12 12:07:10 -08001321 noWebSock(true);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001322 };
1323 },
1324
1325 send : function(text) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001326 if (text != null) {
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001327 webSock._send(text);
1328 }
1329 },
1330
1331 _send : function(message) {
1332 if (webSock.ws) {
1333 webSock.ws.send(message);
Simon Hunta255a2c2014-11-13 22:29:35 -08001334 } else if (config.useLiveData) {
Simon Hunt56d51852014-11-09 13:03:35 -08001335 network.view.alert('no web socket open\n\n' + message);
Simon Hunta255a2c2014-11-13 22:29:35 -08001336 } else {
1337 console.log('WS Send: ' + JSON.stringify(message));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001338 }
1339 }
1340
1341 };
1342
Simon Hunt0c6d4192014-11-12 12:07:10 -08001343 function noWebSock(b) {
1344 mask.style('display',b ? 'block' : 'none');
1345 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001346
Simon Hunt61d04042014-11-11 17:27:16 -08001347 // TODO: use cache of pending messages (key = sid) to reconcile responses
1348
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001349 function sendMessage(evType, payload) {
1350 var toSend = {
Simon Huntbb282f52014-11-10 11:08:19 -08001351 event: evType,
1352 sid: ++sid,
1353 payload: payload
1354 },
1355 asText = JSON.stringify(toSend);
1356 wsTraceTx(asText);
1357 webSock.send(asText);
1358 }
1359
1360 function wsTraceTx(msg) {
1361 wsTrace('tx', msg);
1362 }
1363 function wsTraceRx(msg) {
1364 wsTrace('rx', msg);
1365 }
1366 function wsTrace(rxtx, msg) {
Simon Huntbb282f52014-11-10 11:08:19 -08001367 console.log('[' + rxtx + '] ' + msg);
1368 // TODO: integrate with trace view
1369 //if (trace) {
1370 // trace.output(rxtx, msg);
1371 //}
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001372 }
1373
1374
1375 // ==============================
1376 // Selection stuff
1377
1378 function selectObject(obj, el) {
1379 var n,
Simon Hunt01095ff2014-11-13 16:37:29 -08001380 srcEv = d3.event.sourceEvent,
1381 meta = srcEv.metaKey,
1382 shift = srcEv.shiftKey;
1383
Simon Huntdeab4322014-11-13 18:49:07 -08001384 if ((panZoom() && !meta) || (!panZoom() && meta)) {
Simon Hunt01095ff2014-11-13 16:37:29 -08001385 return;
1386 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001387
1388 if (el) {
1389 n = d3.select(el);
1390 } else {
1391 node.each(function(d) {
1392 if (d == obj) {
1393 n = d3.select(el = this);
1394 }
1395 });
1396 }
1397 if (!n) return;
1398
Simon Hunt01095ff2014-11-13 16:37:29 -08001399 if (shift && n.classed('selected')) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001400 deselectObject(obj.id);
Simon Hunt61d04042014-11-11 17:27:16 -08001401 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001402 return;
1403 }
1404
Simon Hunt01095ff2014-11-13 16:37:29 -08001405 if (!shift) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001406 deselectAll();
1407 }
1408
Simon Huntc31d5692014-11-12 13:27:18 -08001409 selections[obj.id] = { obj: obj, el: el };
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001410 selectOrder.push(obj.id);
1411
1412 n.classed('selected', true);
Simon Hunt61d04042014-11-11 17:27:16 -08001413 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001414 }
1415
1416 function deselectObject(id) {
Simon Huntc31d5692014-11-12 13:27:18 -08001417 var obj = selections[id],
1418 idx;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001419 if (obj) {
1420 d3.select(obj.el).classed('selected', false);
Simon Hunt61d04042014-11-11 17:27:16 -08001421 delete selections[id];
Simon Huntc31d5692014-11-12 13:27:18 -08001422 idx = $.inArray(id, selectOrder);
1423 if (idx >= 0) {
1424 selectOrder.splice(idx, 1);
1425 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001426 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001427 }
1428
1429 function deselectAll() {
1430 // deselect all nodes in the network...
1431 node.classed('selected', false);
1432 selections = {};
1433 selectOrder = [];
Simon Hunt61d04042014-11-11 17:27:16 -08001434 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001435 }
1436
Simon Hunt61d04042014-11-11 17:27:16 -08001437 // update the state of the detail pane, based on current selections
1438 function updateDetailPane() {
1439 var nSel = selectOrder.length;
1440 if (!nSel) {
1441 detailPane.hide();
Simon Huntd72bc702014-11-13 18:38:04 -08001442 showTrafficAction(); // sends cancelTraffic event
Simon Hunt61d04042014-11-11 17:27:16 -08001443 } else if (nSel === 1) {
1444 singleSelect();
1445 } else {
1446 multiSelect();
1447 }
1448 }
1449
1450 function singleSelect() {
1451 requestDetails();
Simon Huntb53e0682014-11-12 13:32:01 -08001452 // NOTE: detail pane will be shown from showDetails event callback
Simon Hunt61d04042014-11-11 17:27:16 -08001453 }
1454
1455 function multiSelect() {
Simon Huntb53e0682014-11-12 13:32:01 -08001456 populateMultiSelect();
Simon Huntb53e0682014-11-12 13:32:01 -08001457 }
1458
1459 function addSep(tbody) {
1460 var tr = tbody.append('tr');
1461 $('<hr>').appendTo(tr.append('td').attr('colspan', 2));
1462 }
1463
1464 function addProp(tbody, label, value) {
1465 var tr = tbody.append('tr');
1466
1467 tr.append('td')
1468 .attr('class', 'label')
1469 .text(label + ' :');
1470
1471 tr.append('td')
1472 .attr('class', 'value')
1473 .text(value);
1474 }
1475
1476 function populateMultiSelect() {
1477 detailPane.empty();
1478
1479 var title = detailPane.append("h2"),
1480 table = detailPane.append("table"),
1481 tbody = table.append("tbody");
1482
1483 title.text('Multi-Select...');
1484
1485 selectOrder.forEach(function (d, i) {
1486 addProp(tbody, i+1, d);
1487 });
Simon Huntd72bc702014-11-13 18:38:04 -08001488
1489 addMultiSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08001490 }
1491
1492 function populateDetails(data) {
1493 detailPane.empty();
1494
1495 var title = detailPane.append("h2"),
1496 table = detailPane.append("table"),
1497 tbody = table.append("tbody");
1498
1499 $('<img src="img/' + data.type + '.png">').appendTo(title);
1500 $('<span>').attr('class', 'icon').text(data.id).appendTo(title);
1501
1502 data.propOrder.forEach(function(p) {
1503 if (p === '-') {
1504 addSep(tbody);
1505 } else {
1506 addProp(tbody, p, data.props[p]);
1507 }
1508 });
Simon Huntd72bc702014-11-13 18:38:04 -08001509
1510 addSingleSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08001511 }
1512
Simon Huntd72bc702014-11-13 18:38:04 -08001513 function addSingleSelectActions() {
1514 detailPane.append('hr');
1515 // always want to allow 'show traffic'
Simon Hunta5e89142014-11-14 07:00:33 -08001516 addAction(detailPane, 'Show Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001517 }
1518
1519 function addMultiSelectActions() {
1520 detailPane.append('hr');
1521 // always want to allow 'show traffic'
Simon Hunta5e89142014-11-14 07:00:33 -08001522 addAction(detailPane, 'Show Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001523 // if exactly two hosts are selected, also want 'add host intent'
1524 if (nSel() === 2 && allSelectionsClass('host')) {
Simon Hunta5e89142014-11-14 07:00:33 -08001525 addAction(detailPane, 'Add Host Intent', addIntentAction);
Simon Huntd72bc702014-11-13 18:38:04 -08001526 }
1527 }
1528
Simon Hunta5e89142014-11-14 07:00:33 -08001529 function addAction(panel, text, cb) {
1530 panel.append('div')
Simon Huntd72bc702014-11-13 18:38:04 -08001531 .classed('actionBtn', true)
1532 .text(text)
1533 .on('click', cb);
1534 }
1535
1536
Paul Greysonfcba0e82014-11-13 10:21:16 -08001537 function zoomPan(scale, translate) {
1538 zoomPanContainer.attr("transform", "translate(" + translate + ")scale(" + scale + ")");
1539 // keep the map lines constant width while zooming
1540 bgImg.style("stroke-width", 2.0 / scale + "px");
1541 }
1542
1543 function resetZoomPan() {
1544 zoomPan(1, [0,0]);
1545 zoom.scale(1).translate([0,0]);
1546 }
1547
1548 function setupZoomPan() {
1549 function zoomed() {
Simon Huntdeab4322014-11-13 18:49:07 -08001550 if (!panZoom() ^ !d3.event.sourceEvent.metaKey) {
Paul Greysonfcba0e82014-11-13 10:21:16 -08001551 zoomPan(d3.event.scale, d3.event.translate);
1552 }
1553 }
1554
1555 zoom = d3.behavior.zoom()
1556 .translate([0, 0])
1557 .scale(1)
1558 .scaleExtent([1, 8])
1559 .on("zoom", zoomed);
1560
1561 svg.call(zoom);
1562 }
1563
Simon Hunt61d04042014-11-11 17:27:16 -08001564 // ==============================
1565 // Test harness code
Simon Hunt56d51852014-11-09 13:03:35 -08001566
1567 function prepareScenario(view, ctx, dbg) {
1568 var sc = scenario,
1569 urlSc = sc.evDir + ctx + sc.evScenario;
1570
1571 if (!ctx) {
1572 view.alert("No scenario specified (null ctx)");
1573 return;
1574 }
1575
1576 sc.view = view;
1577 sc.ctx = ctx;
1578 sc.debug = dbg;
1579 sc.evNumber = 0;
1580
1581 d3.json(urlSc, function(err, data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001582 var p = data && data.params || {},
1583 desc = data && data.description || null,
Simon Huntfc274c92014-11-11 11:05:46 -08001584 intro = data && data.title;
Simon Huntbb282f52014-11-10 11:08:19 -08001585
Simon Hunt56d51852014-11-09 13:03:35 -08001586 if (err) {
1587 view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err);
1588 } else {
1589 sc.params = p;
Simon Huntbb282f52014-11-10 11:08:19 -08001590 if (desc) {
1591 intro += '\n\n ' + desc.join('\n ');
1592 }
1593 view.alert(intro);
Simon Hunt56d51852014-11-09 13:03:35 -08001594 }
1595 });
1596
1597 }
1598
Simon Hunt01095ff2014-11-13 16:37:29 -08001599 // ==============================
1600 // Toggle Buttons in masthead
Simon Hunt0c6d4192014-11-12 12:07:10 -08001601
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001602 // TODO: toggle button (and other widgets in the masthead) should be provided
1603 // by the framework; not generated by the view.
1604
Simon Hunta5e89142014-11-14 07:00:33 -08001605 var showInstances,
1606 doPanZoom,
1607 showTrafficOnHover;
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001608
1609 function addButtonBar(view) {
1610 var bb = d3.select('#mast')
1611 .append('span').classed('right', true).attr('id', 'bb');
1612
Simon Hunta5e89142014-11-14 07:00:33 -08001613 function mkTogBtn(text, cb) {
1614 return bb.append('span')
1615 .classed('btn', true)
1616 .text(text)
1617 .on('click', cb);
1618 }
Simon Hunt01095ff2014-11-13 16:37:29 -08001619
Simon Hunta5e89142014-11-14 07:00:33 -08001620 showInstances = mkTogBtn('Show Instances', toggleInst);
1621 doPanZoom = mkTogBtn('Pan/Zoom', togglePanZoom);
1622 showTrafficOnHover = mkTogBtn('Show traffic on hover', toggleTrafficHover);
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001623 }
1624
Simon Hunta5e89142014-11-14 07:00:33 -08001625 function instShown() {
1626 return showInstances.classed('active');
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001627 }
Simon Hunta5e89142014-11-14 07:00:33 -08001628 function toggleInst() {
1629 showInstances.classed('active', !instShown());
1630 if (instShown()) {
1631 oiBox.show();
1632 } else {
1633 oiBox.hide();
1634 }
Simon Hunt01095ff2014-11-13 16:37:29 -08001635 }
1636
Simon Huntdeab4322014-11-13 18:49:07 -08001637 function panZoom() {
1638 return doPanZoom.classed('active');
Simon Hunt01095ff2014-11-13 16:37:29 -08001639 }
Simon Hunta5e89142014-11-14 07:00:33 -08001640 function togglePanZoom() {
1641 doPanZoom.classed('active', !panZoom());
1642 }
1643
1644 function trafficHover() {
1645 return showTrafficOnHover.classed('active');
1646 }
1647 function toggleTrafficHover() {
1648 showTrafficOnHover.classed('active', !trafficHover());
1649 }
1650
Simon Hunt01095ff2014-11-13 16:37:29 -08001651
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001652 // ==============================
Simon Hunt142d0032014-11-04 20:13:09 -08001653 // View life-cycle callbacks
Simon Hunt195cb382014-11-03 17:50:51 -08001654
Simon Huntf67722a2014-11-10 09:32:06 -08001655 function preload(view, ctx, flags) {
Simon Hunt142d0032014-11-04 20:13:09 -08001656 var w = view.width(),
1657 h = view.height(),
Simon Huntc7ee0662014-11-05 16:44:37 -08001658 fcfg = config.force,
1659 fpad = fcfg.pad,
1660 forceDim = [w - 2*fpad, h - 2*fpad];
Simon Hunt195cb382014-11-03 17:50:51 -08001661
Simon Hunt142d0032014-11-04 20:13:09 -08001662 // NOTE: view.$div is a D3 selection of the view's div
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001663 var viewBox = '0 0 ' + config.logicalSize + ' ' + config.logicalSize;
1664 svg = view.$div.append('svg').attr('viewBox', viewBox);
Simon Hunt934c3ce2014-11-05 11:45:07 -08001665 setSize(svg, view);
1666
Simon Hunt12ce12e2014-11-15 21:13:19 -08001667 var defs = svg.append('defs');
1668 gly.defBird(defs);
1669
Paul Greysonfcba0e82014-11-13 10:21:16 -08001670 zoomPanContainer = svg.append('g').attr('id', 'zoomPanContainer');
Paul Greysonfcba0e82014-11-13 10:21:16 -08001671 setupZoomPan();
1672
Simon Hunt1a9eff92014-11-07 11:06:34 -08001673 // add blue glow filter to svg layer
Paul Greysonfcba0e82014-11-13 10:21:16 -08001674 d3u.appendGlow(zoomPanContainer);
Simon Hunt1a9eff92014-11-07 11:06:34 -08001675
Simon Huntc7ee0662014-11-05 16:44:37 -08001676 // group for the topology
Paul Greysonfcba0e82014-11-13 10:21:16 -08001677 topoG = zoomPanContainer.append('g')
Simon Huntd3b7d512014-11-12 15:48:41 -08001678 .attr('id', 'topo-G')
Simon Huntc7ee0662014-11-05 16:44:37 -08001679 .attr('transform', fcfg.translate());
1680
1681 // subgroups for links and nodes
1682 linkG = topoG.append('g').attr('id', 'links');
1683 nodeG = topoG.append('g').attr('id', 'nodes');
1684
1685 // selection of nodes and links
1686 link = linkG.selectAll('.link');
1687 node = nodeG.selectAll('.node');
1688
Simon Hunt7cd48f32014-11-09 23:42:50 -08001689 function chrg(d) {
1690 return fcfg.charge[d.class] || -12000;
1691 }
Simon Hunt99c13842014-11-06 18:23:12 -08001692 function ldist(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08001693 return fcfg.linkDistance[d.type] || 50;
Simon Hunt99c13842014-11-06 18:23:12 -08001694 }
1695 function lstrg(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08001696 // 0.0 - 1.0
1697 return fcfg.linkStrength[d.type] || 1.0;
Simon Hunt99c13842014-11-06 18:23:12 -08001698 }
1699
Simon Hunt1a9eff92014-11-07 11:06:34 -08001700 function selectCb(d, self) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001701 selectObject(d, self);
Simon Hunt1a9eff92014-11-07 11:06:34 -08001702 }
1703
1704 function atDragEnd(d, self) {
Simon Hunt56d51852014-11-09 13:03:35 -08001705 // once we've finished moving, pin the node in position
1706 d.fixed = true;
1707 d3.select(self).classed('fixed', true);
1708 if (config.useLiveData) {
Simon Hunt902c9922014-11-11 11:59:31 -08001709 sendUpdateMeta(d);
Simon Hunta255a2c2014-11-13 22:29:35 -08001710 } else {
1711 console.log('Moving node ' + d.id + ' to [' + d.x + ',' + d.y + ']');
Simon Hunt1a9eff92014-11-07 11:06:34 -08001712 }
1713 }
1714
Simon Hunt902c9922014-11-11 11:59:31 -08001715 function sendUpdateMeta(d) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001716 sendMessage('updateMeta', {
1717 id: d.id,
1718 'class': d.class,
Simon Hunt902c9922014-11-11 11:59:31 -08001719 'memento': {
Simon Hunt01095ff2014-11-13 16:37:29 -08001720 x: d.x,
1721 y: d.y
Simon Hunt902c9922014-11-11 11:59:31 -08001722 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001723 });
1724 }
1725
Simon Huntc7ee0662014-11-05 16:44:37 -08001726 // set up the force layout
1727 network.force = d3.layout.force()
1728 .size(forceDim)
1729 .nodes(network.nodes)
1730 .links(network.links)
Simon Hunt7cd48f32014-11-09 23:42:50 -08001731 .gravity(0.4)
1732 .friction(0.7)
1733 .charge(chrg)
Simon Hunt99c13842014-11-06 18:23:12 -08001734 .linkDistance(ldist)
1735 .linkStrength(lstrg)
Simon Huntc7ee0662014-11-05 16:44:37 -08001736 .on('tick', tick);
Simon Hunt195cb382014-11-03 17:50:51 -08001737
Simon Hunt01095ff2014-11-13 16:37:29 -08001738 network.drag = d3u.createDragBehavior(network.force,
Simon Huntdeab4322014-11-13 18:49:07 -08001739 selectCb, atDragEnd, panZoom);
Simon Hunt0c6d4192014-11-12 12:07:10 -08001740
1741 // create mask layer for when we lose connection to server.
Simon Hunta5e89142014-11-14 07:00:33 -08001742 // TODO: this should be part of the framework
Simon Hunt0c6d4192014-11-12 12:07:10 -08001743 mask = view.$div.append('div').attr('id','topo-mask');
1744 para(mask, 'Oops!');
1745 para(mask, 'Web-socket connection to server closed...');
1746 para(mask, 'Try refreshing the page.');
Simon Hunt12ce12e2014-11-15 21:13:19 -08001747
1748 mask.append('svg')
1749 .attr({
1750 id: 'mask-bird',
1751 width: w,
1752 height: h
1753 })
1754 .append('g')
1755 .attr('transform', birdTranslate(w, h))
1756 .style('opacity', 0.3)
1757 .append('use')
1758 .attr({
1759 'xlink:href': '#bird',
1760 width: config.birdDim,
1761 height: config.birdDim,
1762 fill: '#111'
1763 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08001764 }
Simon Hunt195cb382014-11-03 17:50:51 -08001765
Simon Hunt01095ff2014-11-13 16:37:29 -08001766 function para(sel, text) {
1767 sel.append('p').text(text);
1768 }
1769
1770
Simon Hunt56d51852014-11-09 13:03:35 -08001771 function load(view, ctx, flags) {
Simon Huntf67722a2014-11-10 09:32:06 -08001772 // resize, in case the window was resized while we were not loaded
1773 resize(view, ctx, flags);
1774
Simon Hunt99c13842014-11-06 18:23:12 -08001775 // cache the view token, so network topo functions can access it
1776 network.view = view;
Simon Hunt56d51852014-11-09 13:03:35 -08001777 config.useLiveData = !flags.local;
1778
1779 if (!config.useLiveData) {
1780 prepareScenario(view, ctx, flags.debug);
1781 }
Simon Hunt99c13842014-11-06 18:23:12 -08001782
1783 // set our radio buttons and key bindings
Simon Hunt9462e8c2014-11-14 17:28:09 -08001784 layerBtnSet = view.setRadio(layerButtons);
Simon Hunt934c3ce2014-11-05 11:45:07 -08001785 view.setKeys(keyDispatch);
Simon Hunt195cb382014-11-03 17:50:51 -08001786
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001787 // patch in our "button bar" for now
1788 // TODO: implement a more official frameworky way of doing this..
1789 addButtonBar(view);
1790
Simon Huntd3b7d512014-11-12 15:48:41 -08001791 // Load map data asynchronously; complete startup after that..
1792 loadGeoJsonData();
Simon Hunta255a2c2014-11-13 22:29:35 -08001793
1794 // start the and timer
1795 var dashIdx = 0;
1796 antTimer = setInterval(function () {
1797 // TODO: figure out how to choose Src-->Dst and Dst-->Src, per link
1798 dashIdx = dashIdx === 0 ? 14 : dashIdx - 2;
1799 d3.selectAll('.animated').style('stroke-dashoffset', dashIdx);
1800 }, 35);
1801 }
1802
1803 function unload(view, ctx, flags) {
1804 if (antTimer) {
1805 clearInterval(antTimer);
1806 antTimer = null;
1807 }
Simon Huntd3b7d512014-11-12 15:48:41 -08001808 }
1809
1810 // TODO: move these to config/state portion of script
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001811 var geoJsonUrl = 'json/map/continental_us.json', // TODO: Paul
Simon Huntd3b7d512014-11-12 15:48:41 -08001812 geoJson;
1813
1814 function loadGeoJsonData() {
1815 d3.json(geoJsonUrl, function (err, data) {
1816 if (err) {
1817 // fall back to USA map background
1818 loadStaticMap();
1819 } else {
1820 geoJson = data;
1821 loadGeoMap();
1822 }
1823
1824 // finally, connect to the server...
1825 if (config.useLiveData) {
1826 webSock.connect();
1827 }
1828 });
1829 }
1830
1831 function showBg() {
1832 return config.options.showBackground ? 'visible' : 'hidden';
1833 }
1834
1835 function loadStaticMap() {
1836 fnTrace('loadStaticMap', config.backgroundUrl);
1837 var w = network.view.width(),
1838 h = network.view.height();
1839
1840 // load the background image
1841 bgImg = svg.insert('svg:image', '#topo-G')
1842 .attr({
1843 id: 'topo-bg',
1844 width: w,
1845 height: h,
1846 'xlink:href': config.backgroundUrl
1847 })
1848 .style({
1849 visibility: showBg()
1850 });
1851 }
1852
1853 function loadGeoMap() {
1854 fnTrace('loadGeoMap', geoJsonUrl);
Simon Huntd3b7d512014-11-12 15:48:41 -08001855
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001856 // extracts the topojson data into geocoordinate-based geometry
1857 var topoData = topojson.feature(geoJson, geoJson.objects.states);
Simon Huntd3b7d512014-11-12 15:48:41 -08001858
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001859 // see: http://bl.ocks.org/mbostock/4707858
1860 geoMapProjection = d3.geo.mercator();
1861 var path = d3.geo.path().projection(geoMapProjection);
Simon Huntd3b7d512014-11-12 15:48:41 -08001862
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001863 geoMapProjection
1864 .scale(1)
1865 .translate([0, 0]);
Simon Huntd3b7d512014-11-12 15:48:41 -08001866
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001867 // [[x1,y1],[x2,y2]]
1868 var b = path.bounds(topoData);
Paul Greysonfcba0e82014-11-13 10:21:16 -08001869 // size map to 95% of minimum dimension to fill space
1870 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 -08001871 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 -08001872
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001873 geoMapProjection
1874 .scale(s)
1875 .translate(t);
1876
Paul Greysonfcba0e82014-11-13 10:21:16 -08001877 bgImg = zoomPanContainer.insert("g", '#topo-G');
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001878 bgImg.attr('id', 'map').selectAll('path')
1879 .data(topoData.features)
1880 .enter()
1881 .append('path')
1882 .attr('d', path);
Simon Hunt195cb382014-11-03 17:50:51 -08001883 }
1884
Simon Huntf67722a2014-11-10 09:32:06 -08001885 function resize(view, ctx, flags) {
Simon Hunt12ce12e2014-11-15 21:13:19 -08001886 var w = view.width(),
1887 h = view.height();
1888
Simon Hunt934c3ce2014-11-05 11:45:07 -08001889 setSize(svg, view);
Simon Hunt12ce12e2014-11-15 21:13:19 -08001890
1891 d3.select('#mask-bird').attr({ width: w, height: h})
1892 .select('g').attr('transform', birdTranslate(w, h));
Simon Hunt142d0032014-11-04 20:13:09 -08001893 }
1894
Simon Hunt12ce12e2014-11-15 21:13:19 -08001895 function birdTranslate(w, h) {
1896 var bdim = config.birdDim;
1897 return 'translate('+((w-bdim)*.4)+','+((h-bdim)*.1)+')';
1898 }
Simon Hunt142d0032014-11-04 20:13:09 -08001899
1900 // ==============================
1901 // View registration
Simon Hunt195cb382014-11-03 17:50:51 -08001902
Simon Hunt25248912014-11-04 11:25:48 -08001903 onos.ui.addView('topo', {
Simon Hunt142d0032014-11-04 20:13:09 -08001904 preload: preload,
1905 load: load,
Simon Hunta255a2c2014-11-13 22:29:35 -08001906 unload: unload,
Simon Hunt142d0032014-11-04 20:13:09 -08001907 resize: resize
Simon Hunt195cb382014-11-03 17:50:51 -08001908 });
1909
Simon Hunt61d04042014-11-11 17:27:16 -08001910 detailPane = onos.ui.addFloatingPanel('topo-detail');
Simon Hunta5e89142014-11-14 07:00:33 -08001911 oiBox = onos.ui.addFloatingPanel('topo-oibox', 'TL');
Simon Hunt61d04042014-11-11 17:27:16 -08001912
Simon Hunt195cb382014-11-03 17:50:51 -08001913}(ONOS));