blob: 9c3edf0d92e196750c87325e2c5e2f6d535bc032 [file] [log] [blame]
Simon Hunt195cb382014-11-03 17:50:51 -08001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
Simon Hunt142d0032014-11-04 20:13:09 -080018 ONOS network topology viewer - version 1.1
Simon Hunt195cb382014-11-03 17:50:51 -080019
20 @author Simon Hunt
21 */
22
23(function (onos) {
24 'use strict';
25
Simon Hunt1a9eff92014-11-07 11:06:34 -080026 // shorter names for library APIs
Simon Huntbb282f52014-11-10 11:08:19 -080027 var d3u = onos.lib.d3util,
28 trace;
Simon Hunt1a9eff92014-11-07 11:06:34 -080029
Simon Hunt195cb382014-11-03 17:50:51 -080030 // configuration data
31 var config = {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -080032 useLiveData: true,
Simon Huntfc274c92014-11-11 11:05:46 -080033 fnTrace: true,
Simon Hunt195cb382014-11-03 17:50:51 -080034 debugOn: false,
35 debug: {
Simon Hunt99c13842014-11-06 18:23:12 -080036 showNodeXY: true,
37 showKeyHandler: false
Simon Hunt195cb382014-11-03 17:50:51 -080038 },
39 options: {
40 layering: true,
41 collisionPrevention: true,
Simon Hunt142d0032014-11-04 20:13:09 -080042 showBackground: true
Simon Hunt195cb382014-11-03 17:50:51 -080043 },
44 backgroundUrl: 'img/us-map.png',
Thomas Vachuska7d638d32014-11-07 10:24:43 -080045 webSockUrl: 'ws/topology',
Simon Hunt195cb382014-11-03 17:50:51 -080046 data: {
47 live: {
48 jsonUrl: 'rs/topology/graph',
49 detailPrefix: 'rs/topology/graph/',
50 detailSuffix: ''
51 },
52 fake: {
53 jsonUrl: 'json/network2.json',
54 detailPrefix: 'json/',
55 detailSuffix: '.json'
56 }
57 },
Simon Hunt99c13842014-11-06 18:23:12 -080058 labels: {
59 imgPad: 16,
60 padLR: 4,
61 padTB: 3,
62 marginLR: 3,
63 marginTB: 2,
64 port: {
65 gap: 3,
66 width: 18,
67 height: 14
68 }
69 },
Simon Hunt1a9eff92014-11-07 11:06:34 -080070 topo: {
71 linkInColor: '#66f',
72 linkInWidth: 14
73 },
Simon Hunt99c13842014-11-06 18:23:12 -080074 icons: {
75 w: 28,
76 h: 28,
77 xoff: -12,
78 yoff: -8
79 },
Simon Hunt195cb382014-11-03 17:50:51 -080080 iconUrl: {
81 device: 'img/device.png',
82 host: 'img/host.png',
83 pkt: 'img/pkt.png',
84 opt: 'img/opt.png'
85 },
Simon Hunt195cb382014-11-03 17:50:51 -080086 force: {
Simon Hunt7cd48f32014-11-09 23:42:50 -080087 note_for_links: 'link.type is used to differentiate',
Simon Huntc7ee0662014-11-05 16:44:37 -080088 linkDistance: {
Simon Hunt7cd48f32014-11-09 23:42:50 -080089 direct: 100,
90 optical: 120,
Thomas Vachuska3266abf2014-11-13 09:28:46 -080091 hostLink: 3
Simon Huntc7ee0662014-11-05 16:44:37 -080092 },
93 linkStrength: {
Simon Hunt7cd48f32014-11-09 23:42:50 -080094 direct: 1.0,
95 optical: 1.0,
96 hostLink: 1.0
Simon Huntc7ee0662014-11-05 16:44:37 -080097 },
Simon Hunt7cd48f32014-11-09 23:42:50 -080098 note_for_nodes: 'node.class is used to differentiate',
Simon Huntc7ee0662014-11-05 16:44:37 -080099 charge: {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800100 device: -8000,
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800101 host: -5000
Simon Huntc7ee0662014-11-05 16:44:37 -0800102 },
103 pad: 20,
Simon Hunt195cb382014-11-03 17:50:51 -0800104 translate: function() {
105 return 'translate(' +
Simon Huntc7ee0662014-11-05 16:44:37 -0800106 config.force.pad + ',' +
107 config.force.pad + ')';
Simon Hunt195cb382014-11-03 17:50:51 -0800108 }
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800109 },
110 // see below in creation of viewBox on main svg
111 logicalSize: 1000
Simon Hunt195cb382014-11-03 17:50:51 -0800112 };
113
Simon Hunt142d0032014-11-04 20:13:09 -0800114 // radio buttons
115 var btnSet = [
Simon Hunt934c3ce2014-11-05 11:45:07 -0800116 { text: 'All Layers', cb: showAllLayers },
117 { text: 'Packet Only', cb: showPacketLayer },
118 { text: 'Optical Only', cb: showOpticalLayer }
119 ];
120
121 // key bindings
122 var keyDispatch = {
Simon Hunt01095ff2014-11-13 16:37:29 -0800123 //M: testMe, // TODO: remove (testing only)
124 //S: injectStartupEvents, // TODO: remove (testing only)
125 //space: injectTestEvent, // TODO: remove (testing only)
Simon Hunt99c13842014-11-06 18:23:12 -0800126
Simon Hunt01095ff2014-11-13 16:37:29 -0800127 B: toggleBg,
Simon Hunt934c3ce2014-11-05 11:45:07 -0800128 L: cycleLabels,
129 P: togglePorts,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800130 U: unpin,
Paul Greysonfcba0e82014-11-13 10:21:16 -0800131 R: resetZoomPan,
Simon Huntd72bc702014-11-13 18:38:04 -0800132 esc: deselectAll
Simon Hunt934c3ce2014-11-05 11:45:07 -0800133 };
Simon Hunt142d0032014-11-04 20:13:09 -0800134
Simon Hunt195cb382014-11-03 17:50:51 -0800135 // state variables
Simon Hunt99c13842014-11-06 18:23:12 -0800136 var network = {
Simon Hunt50128c02014-11-08 13:36:15 -0800137 view: null, // view token reference
Simon Hunt99c13842014-11-06 18:23:12 -0800138 nodes: [],
139 links: [],
140 lookup: {}
141 },
Simon Hunt56d51852014-11-09 13:03:35 -0800142 scenario = {
143 evDir: 'json/ev/',
144 evScenario: '/scenario.json',
145 evPrefix: '/ev_',
146 evOnos: '_onos.json',
147 evUi: '_ui.json',
148 ctx: null,
149 params: {},
150 evNumber: 0,
151 view: null,
152 debug: false
153 },
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800154 webSock,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800155 sid = 0,
Simon Hunt56d51852014-11-09 13:03:35 -0800156 deviceLabelIndex = 0,
157 hostLabelIndex = 0,
Simon Hunt61d04042014-11-11 17:27:16 -0800158 detailPane,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800159 selectOrder = [],
160 selections = {},
Simon Hunt6ac93f32014-11-13 12:17:27 -0800161 hovered = null,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800162
Simon Hunt195cb382014-11-03 17:50:51 -0800163 highlighted = null,
Simon Hunt195cb382014-11-03 17:50:51 -0800164 viewMode = 'showAll',
165 portLabelsOn = false;
166
Simon Hunt934c3ce2014-11-05 11:45:07 -0800167 // D3 selections
168 var svg,
Paul Greysonfcba0e82014-11-13 10:21:16 -0800169 zoomPanContainer,
Simon Hunt934c3ce2014-11-05 11:45:07 -0800170 bgImg,
Simon Huntc7ee0662014-11-05 16:44:37 -0800171 topoG,
172 nodeG,
173 linkG,
174 node,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800175 link,
176 mask;
Simon Hunt195cb382014-11-03 17:50:51 -0800177
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800178 // the projection for the map background
179 var geoMapProjection;
180
Paul Greysonfcba0e82014-11-13 10:21:16 -0800181 // the zoom function
182 var zoom;
183
Simon Hunt142d0032014-11-04 20:13:09 -0800184 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800185 // For Debugging / Development
Simon Hunt195cb382014-11-03 17:50:51 -0800186
Simon Hunt99c13842014-11-06 18:23:12 -0800187 function note(label, msg) {
188 console.log('NOTE: ' + label + ': ' + msg);
Simon Hunt195cb382014-11-03 17:50:51 -0800189 }
190
Simon Hunt99c13842014-11-06 18:23:12 -0800191 function debug(what) {
192 return config.debugOn && config.debug[what];
Simon Hunt934c3ce2014-11-05 11:45:07 -0800193 }
194
Simon Huntfc274c92014-11-11 11:05:46 -0800195 function fnTrace(msg, id) {
196 if (config.fnTrace) {
197 console.log('FN: ' + msg + ' [' + id + ']');
198 }
199 }
Simon Hunt99c13842014-11-06 18:23:12 -0800200
Simon Hunt934c3ce2014-11-05 11:45:07 -0800201 // ==============================
202 // Key Callbacks
203
Simon Hunt99c13842014-11-06 18:23:12 -0800204 function testMe(view) {
Simon Hunt50128c02014-11-08 13:36:15 -0800205 view.alert('test');
Simon Hunt99c13842014-11-06 18:23:12 -0800206 }
207
Simon Hunt56d51852014-11-09 13:03:35 -0800208 function abortIfLive() {
Simon Hunt50128c02014-11-08 13:36:15 -0800209 if (config.useLiveData) {
Simon Huntb53e0682014-11-12 13:32:01 -0800210 network.view.alert("Sorry, currently using live data..");
Simon Hunt56d51852014-11-09 13:03:35 -0800211 return true;
Simon Hunt50128c02014-11-08 13:36:15 -0800212 }
Simon Hunt56d51852014-11-09 13:03:35 -0800213 return false;
214 }
Simon Hunt50128c02014-11-08 13:36:15 -0800215
Simon Hunt56d51852014-11-09 13:03:35 -0800216 function testDebug(msg) {
217 if (scenario.debug) {
218 scenario.view.alert(msg);
219 }
220 }
Simon Hunt99c13842014-11-06 18:23:12 -0800221
Simon Hunt56d51852014-11-09 13:03:35 -0800222 function injectTestEvent(view) {
223 if (abortIfLive()) { return; }
224 var sc = scenario,
225 evn = ++sc.evNumber,
226 pfx = sc.evDir + sc.ctx + sc.evPrefix + evn,
227 onosUrl = pfx + sc.evOnos,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800228 uiUrl = pfx + sc.evUi,
229 stack = [
230 { url: onosUrl, cb: handleServerEvent },
231 { url: uiUrl, cb: handleUiEvent }
232 ];
233 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800234 }
235
Simon Hunt7cd48f32014-11-09 23:42:50 -0800236 function recurseFetchEvent(stack, evn) {
237 var v = scenario.view,
238 frame;
239 if (stack.length === 0) {
Simon Huntfc274c92014-11-11 11:05:46 -0800240 v.alert('Oops!\n\nNo event #' + evn + ' found.');
Simon Hunt7cd48f32014-11-09 23:42:50 -0800241 return;
242 }
243 frame = stack.shift();
244
245 d3.json(frame.url, function (err, data) {
Simon Hunt99c13842014-11-06 18:23:12 -0800246 if (err) {
Simon Hunt56d51852014-11-09 13:03:35 -0800247 if (err.status === 404) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800248 // if we didn't find the data, try the next stack frame
249 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800250 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800251 v.alert('non-404 error:\n\n' + frame.url + '\n\n' + err);
Simon Hunt56d51852014-11-09 13:03:35 -0800252 }
Simon Hunt99c13842014-11-06 18:23:12 -0800253 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800254 testDebug('loaded: ' + frame.url);
255 frame.cb(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800256 }
257 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800258
Simon Hunt56d51852014-11-09 13:03:35 -0800259 }
Simon Hunt50128c02014-11-08 13:36:15 -0800260
Simon Hunt56d51852014-11-09 13:03:35 -0800261 function handleUiEvent(data) {
Simon Huntbb282f52014-11-10 11:08:19 -0800262 scenario.view.alert('UI Tx: ' + data.event + '\n\n' +
263 JSON.stringify(data));
Simon Hunt56d51852014-11-09 13:03:35 -0800264 }
265
266 function injectStartupEvents(view) {
267 var last = scenario.params.lastAuto || 0;
268 if (abortIfLive()) { return; }
269
270 while (scenario.evNumber < last) {
Simon Hunt1a9eff92014-11-07 11:06:34 -0800271 injectTestEvent(view);
272 }
273 }
274
Simon Hunt934c3ce2014-11-05 11:45:07 -0800275 function toggleBg() {
276 var vis = bgImg.style('visibility');
277 bgImg.style('visibility', (vis === 'hidden') ? 'visible' : 'hidden');
278 }
279
Simon Hunt99c13842014-11-06 18:23:12 -0800280 function cycleLabels() {
Simon Huntbb282f52014-11-10 11:08:19 -0800281 deviceLabelIndex = (deviceLabelIndex === network.deviceLabelCount - 1)
282 ? 0 : deviceLabelIndex + 1;
Simon Hunt5f36d342014-11-08 21:33:14 -0800283
Simon Hunt99c13842014-11-06 18:23:12 -0800284 network.nodes.forEach(function (d) {
Simon Huntbb282f52014-11-10 11:08:19 -0800285 if (d.class === 'device') {
286 updateDeviceLabel(d);
287 }
Simon Hunt99c13842014-11-06 18:23:12 -0800288 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800289 }
290
291 function togglePorts(view) {
Simon Hunt50128c02014-11-08 13:36:15 -0800292 view.alert('togglePorts() callback')
Simon Hunt934c3ce2014-11-05 11:45:07 -0800293 }
294
Simon Hunt6ac93f32014-11-13 12:17:27 -0800295 function unpin() {
296 if (hovered) {
297 hovered.fixed = false;
298 hovered.el.classed('fixed', false);
299 network.force.resume();
300 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800301 }
302
303 // ==============================
304 // Radio Button Callbacks
305
Simon Hunt195cb382014-11-03 17:50:51 -0800306 function showAllLayers() {
Simon Hunt142d0032014-11-04 20:13:09 -0800307// network.node.classed('inactive', false);
308// network.link.classed('inactive', false);
309// d3.selectAll('svg .port').classed('inactive', false);
310// d3.selectAll('svg .portText').classed('inactive', false);
Simon Hunt934c3ce2014-11-05 11:45:07 -0800311 // TODO ...
Simon Hunt50128c02014-11-08 13:36:15 -0800312 network.view.alert('showAllLayers() callback');
Simon Hunt195cb382014-11-03 17:50:51 -0800313 }
314
315 function showPacketLayer() {
Simon Hunt934c3ce2014-11-05 11:45:07 -0800316 showAllLayers();
317 // TODO ...
Simon Hunt50128c02014-11-08 13:36:15 -0800318 network.view.alert('showPacketLayer() callback');
Simon Hunt195cb382014-11-03 17:50:51 -0800319 }
320
321 function showOpticalLayer() {
Simon Hunt934c3ce2014-11-05 11:45:07 -0800322 showAllLayers();
323 // TODO ...
Simon Hunt50128c02014-11-08 13:36:15 -0800324 network.view.alert('showOpticalLayer() callback');
Simon Hunt195cb382014-11-03 17:50:51 -0800325 }
326
Simon Hunt142d0032014-11-04 20:13:09 -0800327 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800328 // Private functions
329
Simon Hunt99c13842014-11-06 18:23:12 -0800330 function safeId(s) {
331 return s.replace(/[^a-z0-9]/gi, '-');
332 }
333
Simon Huntc7ee0662014-11-05 16:44:37 -0800334 // set the size of the given element to that of the view (reduced if padded)
335 function setSize(el, view, pad) {
336 var padding = pad ? pad * 2 : 0;
Simon Hunt934c3ce2014-11-05 11:45:07 -0800337 el.attr({
Simon Huntc7ee0662014-11-05 16:44:37 -0800338 width: view.width() - padding,
339 height: view.height() - padding
Simon Hunt934c3ce2014-11-05 11:45:07 -0800340 });
341 }
342
Simon Hunt934c3ce2014-11-05 11:45:07 -0800343
Simon Hunt99c13842014-11-06 18:23:12 -0800344 // ==============================
345 // Event handlers for server-pushed events
346
Simon Huntbb282f52014-11-10 11:08:19 -0800347 function logicError(msg) {
348 // TODO, report logic error to server, via websock, so it can be logged
349 network.view.alert('Logic Error:\n\n' + msg);
Simon Huntfc274c92014-11-11 11:05:46 -0800350 console.warn(msg);
Simon Huntbb282f52014-11-10 11:08:19 -0800351 }
352
Simon Hunt99c13842014-11-06 18:23:12 -0800353 var eventDispatch = {
Simon Huntd72bc702014-11-13 18:38:04 -0800354 addInstance: stillToImplement,
Simon Hunt99c13842014-11-06 18:23:12 -0800355 addDevice: addDevice,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800356 addLink: addLink,
Simon Hunt56d51852014-11-09 13:03:35 -0800357 addHost: addHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800358
Simon Huntd72bc702014-11-13 18:38:04 -0800359 updateInstance: stillToImplement,
Simon Huntbb282f52014-11-10 11:08:19 -0800360 updateDevice: updateDevice,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800361 updateLink: updateLink,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800362 updateHost: updateHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800363
Simon Huntd72bc702014-11-13 18:38:04 -0800364 removeInstance: stillToImplement,
Simon Huntbb282f52014-11-10 11:08:19 -0800365 removeDevice: stillToImplement,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800366 removeLink: removeLink,
Simon Hunt44031102014-11-11 13:20:36 -0800367 removeHost: removeHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800368
Simon Hunt61d04042014-11-11 17:27:16 -0800369 showDetails: showDetails,
Simon Huntb53e0682014-11-12 13:32:01 -0800370 showPath: showPath,
371 showTraffic: showTraffic
Simon Hunt99c13842014-11-06 18:23:12 -0800372 };
373
374 function addDevice(data) {
Simon Huntfc274c92014-11-11 11:05:46 -0800375 fnTrace('addDevice', data.payload.id);
Simon Hunt99c13842014-11-06 18:23:12 -0800376 var device = data.payload,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800377 nodeData = createDeviceNode(device);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800378 network.nodes.push(nodeData);
379 network.lookup[nodeData.id] = nodeData;
Simon Hunt99c13842014-11-06 18:23:12 -0800380 updateNodes();
381 network.force.start();
382 }
383
Simon Hunt99c13842014-11-06 18:23:12 -0800384 function addLink(data) {
Simon Huntfc274c92014-11-11 11:05:46 -0800385 fnTrace('addLink', data.payload.id);
Simon Hunt99c13842014-11-06 18:23:12 -0800386 var link = data.payload,
387 lnk = createLink(link);
Simon Hunt99c13842014-11-06 18:23:12 -0800388 if (lnk) {
Simon Hunt99c13842014-11-06 18:23:12 -0800389 network.links.push(lnk);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800390 network.lookup[lnk.id] = lnk;
Simon Hunt99c13842014-11-06 18:23:12 -0800391 updateLinks();
392 network.force.start();
393 }
394 }
395
Simon Hunt56d51852014-11-09 13:03:35 -0800396 function addHost(data) {
Simon Huntfc274c92014-11-11 11:05:46 -0800397 fnTrace('addHost', data.payload.id);
Simon Hunt56d51852014-11-09 13:03:35 -0800398 var host = data.payload,
399 node = createHostNode(host),
400 lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800401 network.nodes.push(node);
402 network.lookup[host.id] = node;
403 updateNodes();
404
405 lnk = createHostLink(host);
406 if (lnk) {
Simon Hunt44031102014-11-11 13:20:36 -0800407 node.linkData = lnk; // cache ref on its host
Simon Hunt56d51852014-11-09 13:03:35 -0800408 network.links.push(lnk);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800409 network.lookup[host.ingress] = lnk;
410 network.lookup[host.egress] = lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800411 updateLinks();
412 }
413 network.force.start();
414 }
415
Simon Hunt44031102014-11-11 13:20:36 -0800416 // TODO: fold updateX(...) methods into one base method; remove duplication
Simon Huntbb282f52014-11-10 11:08:19 -0800417 function updateDevice(data) {
Simon Huntfc274c92014-11-11 11:05:46 -0800418 fnTrace('updateDevice', data.payload.id);
Simon Huntbb282f52014-11-10 11:08:19 -0800419 var device = data.payload,
420 id = device.id,
421 nodeData = network.lookup[id];
422 if (nodeData) {
423 $.extend(nodeData, device);
424 updateDeviceState(nodeData);
425 } else {
426 logicError('updateDevice lookup fail. ID = "' + id + '"');
427 }
428 }
429
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800430 function updateLink(data) {
Simon Huntfc274c92014-11-11 11:05:46 -0800431 fnTrace('updateLink', data.payload.id);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800432 var link = data.payload,
433 id = link.id,
434 linkData = network.lookup[id];
435 if (linkData) {
436 $.extend(linkData, link);
437 updateLinkState(linkData);
438 } else {
439 logicError('updateLink lookup fail. ID = "' + id + '"');
440 }
441 }
442
Simon Hunt7cd48f32014-11-09 23:42:50 -0800443 function updateHost(data) {
Simon Huntfc274c92014-11-11 11:05:46 -0800444 fnTrace('updateHost', data.payload.id);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800445 var host = data.payload,
Simon Huntbb282f52014-11-10 11:08:19 -0800446 id = host.id,
447 hostData = network.lookup[id];
448 if (hostData) {
449 $.extend(hostData, host);
450 updateHostState(hostData);
451 } else {
452 logicError('updateHost lookup fail. ID = "' + id + '"');
453 }
Simon Hunt7cd48f32014-11-09 23:42:50 -0800454 }
455
Simon Hunt44031102014-11-11 13:20:36 -0800456 // TODO: fold removeX(...) methods into base method - remove dup code
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800457 function removeLink(data) {
Simon Huntfc274c92014-11-11 11:05:46 -0800458 fnTrace('removeLink', data.payload.id);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800459 var link = data.payload,
460 id = link.id,
461 linkData = network.lookup[id];
462 if (linkData) {
463 removeLinkElement(linkData);
464 } else {
465 logicError('removeLink lookup fail. ID = "' + id + '"');
466 }
467 }
468
Simon Hunt44031102014-11-11 13:20:36 -0800469 function removeHost(data) {
470 fnTrace('removeHost', data.payload.id);
471 var host = data.payload,
472 id = host.id,
473 hostData = network.lookup[id];
474 if (hostData) {
475 removeHostElement(hostData);
476 } else {
477 logicError('removeHost lookup fail. ID = "' + id + '"');
478 }
479 }
480
Simon Hunt61d04042014-11-11 17:27:16 -0800481 function showDetails(data) {
482 fnTrace('showDetails', data.payload.id);
483 populateDetails(data.payload);
484 detailPane.show();
485 }
486
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800487 function showPath(data) {
Simon Huntd72bc702014-11-13 18:38:04 -0800488 // TODO: review - making sure we are handling the payload correctly.
Simon Huntfc274c92014-11-11 11:05:46 -0800489 fnTrace('showPath', data.payload.id);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800490 var links = data.payload.links,
491 s = [ data.event + "\n" + links.length ];
492 links.forEach(function (d, i) {
493 s.push(d);
494 });
495 network.view.alert(s.join('\n'));
496
497 links.forEach(function (d, i) {
498 var link = network.lookup[d];
499 if (link) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800500 link.el.classed('showPath', true);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800501 }
502 });
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800503 }
504
Simon Huntb53e0682014-11-12 13:32:01 -0800505 function showTraffic(data) {
Simon Huntd72bc702014-11-13 18:38:04 -0800506 // TODO: review - making sure we are handling the payload correctly.
507 // TODO: handle 'class' of link: primary, secondary, animated...
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800508 fnTrace('showTraffic', data.payload.id);
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800509 var paths = data.payload.paths;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800510
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800511 // Revert any links hilighted previously.
512 network.links.forEach(function (d) {
513 d.el.classed('showPath', false);
514 });
515
516 // Now hilight all links in the paths payload.
517 paths.forEach(function (d) {
518 var links = d.links;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800519 links.forEach(function (d, i) {
520 var link = network.lookup[d];
521 if (link) {
522 link.el.classed('showPath', true);
523 }
524 });
525 });
Simon Huntb53e0682014-11-12 13:32:01 -0800526 }
527
Simon Hunt56d51852014-11-09 13:03:35 -0800528 // ...............................
529
530 function stillToImplement(data) {
531 var p = data.payload;
532 note(data.event, p.id);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800533 network.view.alert('Not yet implemented: "' + data.event + '"');
Simon Hunt56d51852014-11-09 13:03:35 -0800534 }
Simon Hunt99c13842014-11-06 18:23:12 -0800535
536 function unknownEvent(data) {
Simon Hunt50128c02014-11-08 13:36:15 -0800537 network.view.alert('Unknown event type: "' + data.event + '"');
Simon Hunt99c13842014-11-06 18:23:12 -0800538 }
539
540 function handleServerEvent(data) {
541 var fn = eventDispatch[data.event] || unknownEvent;
542 fn(data);
543 }
544
545 // ==============================
Simon Hunt61d04042014-11-11 17:27:16 -0800546 // Out-going messages...
547
Simon Huntb53e0682014-11-12 13:32:01 -0800548 function userFeedback(msg) {
549 // for now, use the alert pane as is. Maybe different alert style in
550 // the future (centered on view; dismiss button?)
551 network.view.alert(msg);
552 }
553
554 function nSel() {
555 return selectOrder.length;
556 }
Simon Hunt61d04042014-11-11 17:27:16 -0800557 function getSel(idx) {
558 return selections[selectOrder[idx]];
559 }
Simon Huntb53e0682014-11-12 13:32:01 -0800560 function getSelId(idx) {
561 return getSel(idx).obj.id;
562 }
563 function allSelectionsClass(cls) {
564 for (var i=0, n=nSel(); i<n; i++) {
565 if (getSel(i).obj.class !== cls) {
566 return false;
567 }
568 }
569 return true;
570 }
Simon Hunt61d04042014-11-11 17:27:16 -0800571
Simon Hunt61d04042014-11-11 17:27:16 -0800572 // request details for the selected element
Simon Huntd72bc702014-11-13 18:38:04 -0800573 // invoked from selection of a single node.
Simon Hunt61d04042014-11-11 17:27:16 -0800574 function requestDetails() {
575 var data = getSel(0).obj,
576 payload = {
577 id: data.id,
578 class: data.class
579 };
580 sendMessage('requestDetails', payload);
581 }
582
Simon Huntd72bc702014-11-13 18:38:04 -0800583 function addIntentAction() {
584 sendMessage('addHostIntent', {
585 one: getSelId(0),
586 two: getSelId(1)
587 });
588 }
589
590 function showTrafficAction() {
591 // if nothing is hovered over, and nothing selected, send cancel request
592 if (!hovered && nSel() === 0) {
593 sendMessage('cancelTraffic', {});
594 return;
595 }
596
597 // NOTE: hover is only populated if "show traffic on hover" is
598 // toggled on, and the item hovered is a host...
599 var hoverId = (trafficHover() && hovered && hovered.class === 'host')
600 ? hovered.id : '';
601 sendMessage('requestTraffic', {
602 ids: selectOrder,
603 hover: hoverId
604 });
605 }
606
607
Simon Hunt61d04042014-11-11 17:27:16 -0800608 // ==============================
Simon Hunt99c13842014-11-06 18:23:12 -0800609 // force layout modification functions
610
611 function translate(x, y) {
612 return 'translate(' + x + ',' + y + ')';
613 }
614
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800615 function missMsg(what, id) {
616 return '\n[' + what + '] "' + id + '" missing ';
617 }
618
619 function linkEndPoints(srcId, dstId) {
620 var srcNode = network.lookup[srcId],
621 dstNode = network.lookup[dstId],
622 sMiss = !srcNode ? missMsg('src', srcId) : '',
623 dMiss = !dstNode ? missMsg('dst', dstId) : '';
624
625 if (sMiss || dMiss) {
626 logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
627 return null;
628 }
629 return {
630 source: srcNode,
631 target: dstNode,
632 x1: srcNode.x,
633 y1: srcNode.y,
634 x2: dstNode.x,
635 y2: dstNode.y
636 };
637 }
638
Simon Hunt56d51852014-11-09 13:03:35 -0800639 function createHostLink(host) {
640 var src = host.id,
641 dst = host.cp.device,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800642 id = host.ingress,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800643 lnk = linkEndPoints(src, dst);
Simon Hunt56d51852014-11-09 13:03:35 -0800644
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800645 if (!lnk) {
Simon Hunt56d51852014-11-09 13:03:35 -0800646 return null;
647 }
648
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800649 // Synthesize link ...
650 $.extend(lnk, {
Thomas Vachuska4830d392014-11-09 17:09:56 -0800651 id: id,
Simon Hunt56d51852014-11-09 13:03:35 -0800652 class: 'link',
Simon Hunt7cd48f32014-11-09 23:42:50 -0800653 type: 'hostLink',
Simon Hunt56d51852014-11-09 13:03:35 -0800654 svgClass: 'link hostLink',
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800655 linkWidth: 1
Simon Hunt7cd48f32014-11-09 23:42:50 -0800656 });
Simon Hunt99c13842014-11-06 18:23:12 -0800657 return lnk;
658 }
659
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800660 function createLink(link) {
661 var lnk = linkEndPoints(link.src, link.dst),
662 type = link.type;
663
664 if (!lnk) {
665 return null;
666 }
667
668 // merge in remaining data
669 $.extend(lnk, link, {
670 class: 'link',
671 svgClass: type ? 'link ' + type : 'link'
672 });
673 return lnk;
Simon Hunt1a9eff92014-11-07 11:06:34 -0800674 }
675
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800676 var widthRatio = 1.4,
677 linkScale = d3.scale.linear()
678 .domain([1, 12])
679 .range([widthRatio, 12 * widthRatio])
680 .clamp(true);
681
682 function updateLinkWidth (d) {
683 // TODO: watch out for .showPath/.showTraffic classes
684 d.el.transition()
685 .duration(1000)
686 .attr('stroke-width', linkScale(d.linkWidth));
687 }
688
689
Simon Hunt99c13842014-11-06 18:23:12 -0800690 function updateLinks() {
691 link = linkG.selectAll('.link')
692 .data(network.links, function (d) { return d.id; });
693
694 // operate on existing links, if necessary
695 // link .foo() .bar() ...
696
697 // operate on entering links:
698 var entering = link.enter()
699 .append('line')
700 .attr({
Simon Hunt99c13842014-11-06 18:23:12 -0800701 class: function (d) { return d.svgClass; },
702 x1: function (d) { return d.x1; },
703 y1: function (d) { return d.y1; },
704 x2: function (d) { return d.x2; },
705 y2: function (d) { return d.y2; },
Simon Hunt1a9eff92014-11-07 11:06:34 -0800706 stroke: config.topo.linkInColor,
707 'stroke-width': config.topo.linkInWidth
Simon Hunt99c13842014-11-06 18:23:12 -0800708 })
709 .transition().duration(1000)
710 .attr({
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800711 'stroke-width': function (d) { return linkScale(d.linkWidth); },
Simon Hunt99c13842014-11-06 18:23:12 -0800712 stroke: '#666' // TODO: remove explicit stroke, rather...
713 });
714
715 // augment links
Simon Hunt7cd48f32014-11-09 23:42:50 -0800716 entering.each(function (d) {
717 var link = d3.select(this);
718 // provide ref to element selection from backing data....
719 d.el = link;
Simon Hunt99c13842014-11-06 18:23:12 -0800720
Simon Hunt7cd48f32014-11-09 23:42:50 -0800721 // TODO: add src/dst port labels etc.
722 });
Thomas Vachuska4830d392014-11-09 17:09:56 -0800723
724 // operate on both existing and new links, if necessary
725 //link .foo() .bar() ...
726
727 // operate on exiting links:
Thomas Vachuska4830d392014-11-09 17:09:56 -0800728 link.exit()
Thomas Vachuska4830d392014-11-09 17:09:56 -0800729 .attr({
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800730 'stroke-dasharray': '3, 3'
Thomas Vachuska4830d392014-11-09 17:09:56 -0800731 })
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800732 .style('opacity', 0.4)
733 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -0800734 .duration(1500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800735 .attr({
736 'stroke-dasharray': '3, 12'
737 })
738 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -0800739 .duration(500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800740 .style('opacity', 0.0)
Thomas Vachuska4830d392014-11-09 17:09:56 -0800741 .remove();
Simon Hunt99c13842014-11-06 18:23:12 -0800742 }
743
744 function createDeviceNode(device) {
745 // start with the object as is
746 var node = device,
Simon Huntbb282f52014-11-10 11:08:19 -0800747 type = device.type,
748 svgCls = type ? 'node device ' + type : 'node device';
Simon Hunt99c13842014-11-06 18:23:12 -0800749
750 // Augment as needed...
751 node.class = 'device';
Simon Huntbb282f52014-11-10 11:08:19 -0800752 node.svgClass = device.online ? svgCls + ' online' : svgCls;
Simon Hunt99c13842014-11-06 18:23:12 -0800753 positionNode(node);
754
755 // cache label array length
756 network.deviceLabelCount = device.labels.length;
Simon Hunt99c13842014-11-06 18:23:12 -0800757 return node;
758 }
759
Simon Hunt56d51852014-11-09 13:03:35 -0800760 function createHostNode(host) {
761 // start with the object as is
762 var node = host;
763
764 // Augment as needed...
765 node.class = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -0800766 if (!node.type) {
767 // TODO: perhaps type would be: {phone, tablet, laptop, endstation} ?
768 node.type = 'endstation';
769 }
Simon Hunt56d51852014-11-09 13:03:35 -0800770 node.svgClass = 'node host';
771 // TODO: consider placing near its switch, if [x,y] not defined
772 positionNode(node);
773
774 // cache label array length
775 network.hostLabelCount = host.labels.length;
Simon Hunt56d51852014-11-09 13:03:35 -0800776 return node;
777 }
778
Simon Hunt99c13842014-11-06 18:23:12 -0800779 function positionNode(node) {
780 var meta = node.metaUi,
Simon Huntac9e24f2014-11-12 10:12:21 -0800781 x = meta && meta.x,
782 y = meta && meta.y,
783 xy;
Simon Hunt99c13842014-11-06 18:23:12 -0800784
Simon Huntac9e24f2014-11-12 10:12:21 -0800785 // If we have [x,y] already, use that...
Simon Hunt99c13842014-11-06 18:23:12 -0800786 if (x && y) {
787 node.fixed = true;
Simon Huntac9e24f2014-11-12 10:12:21 -0800788 node.x = x;
789 node.y = y;
790 return;
Simon Hunt99c13842014-11-06 18:23:12 -0800791 }
Simon Huntac9e24f2014-11-12 10:12:21 -0800792
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800793 var location = node.location;
794 if (location && location.type === 'latlng') {
795 var coord = geoMapProjection([location.lng, location.lat]);
796 node.fixed = true;
797 node.x = coord[0];
798 node.y = coord[1];
799 return;
800 }
801
Simon Huntac9e24f2014-11-12 10:12:21 -0800802 // Note: Placing incoming unpinned nodes at exactly the same point
803 // (center of the view) causes them to explode outwards when
804 // the force layout kicks in. So, we spread them out a bit
805 // initially, to provide a more serene layout convergence.
806 // Additionally, if the node is a host, we place it near
807 // the device it is connected to.
808
809 function spread(s) {
810 return Math.floor((Math.random() * s) - s/2);
811 }
812
813 function randDim(dim) {
814 return dim / 2 + spread(dim * 0.7071);
815 }
816
817 function rand() {
818 return {
819 x: randDim(network.view.width()),
820 y: randDim(network.view.height())
821 };
822 }
823
824 function near(node) {
825 var min = 12,
826 dx = spread(12),
827 dy = spread(12);
828 return {
829 x: node.x + min + dx,
830 y: node.y + min + dy
831 };
832 }
833
834 function getDevice(cp) {
835 var d = network.lookup[cp.device];
836 return d || rand();
837 }
838
839 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
840 $.extend(node, xy);
Simon Hunt99c13842014-11-06 18:23:12 -0800841 }
842
Simon Hunt99c13842014-11-06 18:23:12 -0800843 function iconUrl(d) {
844 return 'img/' + d.type + '.png';
845 }
846
847 // returns the newly computed bounding box of the rectangle
848 function adjustRectToFitText(n) {
849 var text = n.select('text'),
850 box = text.node().getBBox(),
851 lab = config.labels;
852
853 text.attr('text-anchor', 'middle')
854 .attr('y', '-0.8em')
855 .attr('x', lab.imgPad/2);
856
857 // translate the bbox so that it is centered on [x,y]
858 box.x = -box.width / 2;
859 box.y = -box.height / 2;
860
861 // add padding
862 box.x -= (lab.padLR + lab.imgPad/2);
863 box.width += lab.padLR * 2 + lab.imgPad;
864 box.y -= lab.padTB;
865 box.height += lab.padTB * 2;
866
867 return box;
868 }
869
Simon Hunt1a9eff92014-11-07 11:06:34 -0800870 function mkSvgClass(d) {
871 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
872 }
873
Simon Hunt7cd48f32014-11-09 23:42:50 -0800874 function hostLabel(d) {
875 var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0;
876 return d.labels[idx];
877 }
878 function deviceLabel(d) {
879 var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0;
880 return d.labels[idx];
881 }
882 function niceLabel(label) {
883 return (label && label.trim()) ? label : '.';
884 }
885
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800886 function updateDeviceLabel(d) {
887 var label = niceLabel(deviceLabel(d)),
888 node = d.el,
889 box;
890
891 node.select('text')
892 .text(label)
893 .style('opacity', 0)
894 .transition()
895 .style('opacity', 1);
896
897 box = adjustRectToFitText(node);
898
899 node.select('rect')
900 .transition()
901 .attr(box);
902
903 node.select('image')
904 .transition()
905 .attr('x', box.x + config.icons.xoff)
906 .attr('y', box.y + config.icons.yoff);
907 }
908
909 function updateHostLabel(d) {
910 var label = hostLabel(d),
911 host = d.el;
912
913 host.select('text').text(label);
914 }
915
Simon Huntbb282f52014-11-10 11:08:19 -0800916 function updateDeviceState(nodeData) {
917 nodeData.el.classed('online', nodeData.online);
918 updateDeviceLabel(nodeData);
919 // TODO: review what else might need to be updated
920 }
921
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800922 function updateLinkState(linkData) {
923 updateLinkWidth(linkData);
924 // TODO: review what else might need to be updated
925 // update label, if showing
926 }
927
Simon Huntbb282f52014-11-10 11:08:19 -0800928 function updateHostState(hostData) {
929 updateHostLabel(hostData);
930 // TODO: review what else might need to be updated
931 }
932
Simon Hunt6ac93f32014-11-13 12:17:27 -0800933 function nodeMouseOver(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -0800934 hovered = d;
Simon Huntd72bc702014-11-13 18:38:04 -0800935 if (trafficHover() && d.class === 'host') {
936 showTrafficAction();
Simon Hunt6ac93f32014-11-13 12:17:27 -0800937 }
938 }
939
940 function nodeMouseOut(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -0800941 hovered = null;
Simon Huntd72bc702014-11-13 18:38:04 -0800942 if (trafficHover() && d.class === 'host') {
943 showTrafficAction();
Simon Hunt6ac93f32014-11-13 12:17:27 -0800944 }
945 }
Simon Huntbb282f52014-11-10 11:08:19 -0800946
Simon Hunt99c13842014-11-06 18:23:12 -0800947 function updateNodes() {
948 node = nodeG.selectAll('.node')
949 .data(network.nodes, function (d) { return d.id; });
950
951 // operate on existing nodes, if necessary
Simon Hunt7cd48f32014-11-09 23:42:50 -0800952 // update host labels
Simon Hunt99c13842014-11-06 18:23:12 -0800953 //node .foo() .bar() ...
954
955 // operate on entering nodes:
956 var entering = node.enter()
957 .append('g')
958 .attr({
959 id: function (d) { return safeId(d.id); },
Simon Hunt1a9eff92014-11-07 11:06:34 -0800960 class: mkSvgClass,
Simon Hunt99c13842014-11-06 18:23:12 -0800961 transform: function (d) { return translate(d.x, d.y); },
962 opacity: 0
963 })
Simon Hunt1a9eff92014-11-07 11:06:34 -0800964 .call(network.drag)
Simon Hunt6ac93f32014-11-13 12:17:27 -0800965 .on('mouseover', nodeMouseOver)
966 .on('mouseout', nodeMouseOut)
Simon Hunt99c13842014-11-06 18:23:12 -0800967 .transition()
968 .attr('opacity', 1);
969
970 // augment device nodes...
971 entering.filter('.device').each(function (d) {
972 var node = d3.select(this),
973 icon = iconUrl(d),
Simon Hunt7cd48f32014-11-09 23:42:50 -0800974 label = niceLabel(deviceLabel(d)),
Simon Hunt99c13842014-11-06 18:23:12 -0800975 box;
976
Simon Hunt7cd48f32014-11-09 23:42:50 -0800977 // provide ref to element from backing data....
978 d.el = node;
979
Simon Hunt99c13842014-11-06 18:23:12 -0800980 node.append('rect')
981 .attr({
982 'rx': 5,
983 'ry': 5
984 });
985
986 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -0800987 .text(label)
Simon Hunt99c13842014-11-06 18:23:12 -0800988 .attr('dy', '1.1em');
989
990 box = adjustRectToFitText(node);
991
992 node.select('rect')
993 .attr(box);
994
995 if (icon) {
996 var cfg = config.icons;
997 node.append('svg:image')
998 .attr({
999 x: box.x + config.icons.xoff,
1000 y: box.y + config.icons.yoff,
1001 width: cfg.w,
1002 height: cfg.h,
1003 'xlink:href': icon
1004 });
1005 }
1006
1007 // debug function to show the modelled x,y coordinates of nodes...
1008 if (debug('showNodeXY')) {
1009 node.select('rect').attr('fill-opacity', 0.5);
1010 node.append('circle')
1011 .attr({
1012 class: 'debug',
1013 cx: 0,
1014 cy: 0,
1015 r: '3px'
1016 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001017 }
1018 });
Simon Hunt934c3ce2014-11-05 11:45:07 -08001019
Simon Hunt56d51852014-11-09 13:03:35 -08001020 // augment host nodes...
1021 entering.filter('.host').each(function (d) {
1022 var node = d3.select(this),
Simon Hunt56d51852014-11-09 13:03:35 -08001023 box;
1024
Simon Hunt7cd48f32014-11-09 23:42:50 -08001025 // provide ref to element from backing data....
1026 d.el = node;
1027
Simon Hunt56d51852014-11-09 13:03:35 -08001028 node.append('circle')
1029 .attr('r', 8); // TODO: define host circle radius
1030
Simon Hunt56d51852014-11-09 13:03:35 -08001031 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001032 .text(hostLabel)
1033 .attr('dy', '1.3em')
1034 .attr('text-anchor', 'middle');
Simon Hunt56d51852014-11-09 13:03:35 -08001035
1036 // debug function to show the modelled x,y coordinates of nodes...
1037 if (debug('showNodeXY')) {
1038 node.select('circle').attr('fill-opacity', 0.5);
1039 node.append('circle')
1040 .attr({
1041 class: 'debug',
1042 cx: 0,
1043 cy: 0,
1044 r: '3px'
1045 });
1046 }
1047 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001048
Simon Hunt99c13842014-11-06 18:23:12 -08001049 // operate on both existing and new nodes, if necessary
1050 //node .foo() .bar() ...
Simon Huntc7ee0662014-11-05 16:44:37 -08001051
Simon Hunt99c13842014-11-06 18:23:12 -08001052 // operate on exiting nodes:
Simon Huntea80eb42014-11-11 13:46:57 -08001053 // Note that the node is removed after 2 seconds.
1054 // Sub element animations should be shorter than 2 seconds.
1055 var exiting = node.exit()
Simon Hunt44031102014-11-11 13:20:36 -08001056 .transition()
1057 .duration(2000)
Simon Huntea80eb42014-11-11 13:46:57 -08001058 .style('opacity', 0)
Simon Hunt99c13842014-11-06 18:23:12 -08001059 .remove();
Simon Huntea80eb42014-11-11 13:46:57 -08001060
1061 // host node exits....
1062 exiting.filter('.host').each(function (d) {
1063 var node = d3.select(this);
1064
1065 node.select('text')
1066 .style('opacity', 0.5)
1067 .transition()
1068 .duration(1000)
1069 .style('opacity', 0);
1070 // note, leave <g>.remove to remove this element
1071
1072 node.select('circle')
1073 .style('stroke-fill', '#555')
1074 .style('fill', '#888')
1075 .style('opacity', 0.5)
1076 .transition()
1077 .duration(1500)
1078 .attr('r', 0);
1079 // note, leave <g>.remove to remove this element
1080
1081 });
1082
1083 // TODO: device node exits
Simon Huntc7ee0662014-11-05 16:44:37 -08001084 }
1085
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001086 function find(id, array) {
1087 for (var idx = 0, n = array.length; idx < n; idx++) {
1088 if (array[idx].id === id) {
1089 return idx;
1090 }
1091 }
1092 return -1;
1093 }
1094
1095 function removeLinkElement(linkData) {
1096 // remove from lookup cache
1097 delete network.lookup[linkData.id];
1098 // remove from links array
1099 var idx = find(linkData.id, network.links);
Simon Huntfc274c92014-11-11 11:05:46 -08001100 network.links.splice(idx, 1);
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001101 // remove from SVG
1102 updateLinks();
Simon Hunt44031102014-11-11 13:20:36 -08001103 network.force.resume();
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001104 }
Simon Huntc7ee0662014-11-05 16:44:37 -08001105
Simon Hunt44031102014-11-11 13:20:36 -08001106 function removeHostElement(hostData) {
1107 // first, remove associated hostLink...
1108 removeLinkElement(hostData.linkData);
1109
1110 // remove from lookup cache
1111 delete network.lookup[hostData.id];
1112 // remove from nodes array
1113 var idx = find(hostData.id, network.nodes);
1114 network.nodes.splice(idx, 1);
1115 // remove from SVG
1116 updateNodes();
1117 network.force.resume();
1118 }
1119
1120
Simon Huntc7ee0662014-11-05 16:44:37 -08001121 function tick() {
1122 node.attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001123 transform: function (d) { return translate(d.x, d.y); }
Simon Huntc7ee0662014-11-05 16:44:37 -08001124 });
1125
1126 link.attr({
1127 x1: function (d) { return d.source.x; },
1128 y1: function (d) { return d.source.y; },
1129 x2: function (d) { return d.target.x; },
1130 y2: function (d) { return d.target.y; }
1131 });
1132 }
Simon Hunt934c3ce2014-11-05 11:45:07 -08001133
1134 // ==============================
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001135 // Web-Socket for live data
1136
1137 function webSockUrl() {
1138 return document.location.toString()
1139 .replace(/\#.*/, '')
1140 .replace('http://', 'ws://')
1141 .replace('https://', 'wss://')
1142 .replace('index2.html', config.webSockUrl);
1143 }
1144
1145 webSock = {
1146 ws : null,
1147
1148 connect : function() {
1149 webSock.ws = new WebSocket(webSockUrl());
1150
1151 webSock.ws.onopen = function() {
Simon Hunt0c6d4192014-11-12 12:07:10 -08001152 noWebSock(false);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001153 };
1154
1155 webSock.ws.onmessage = function(m) {
1156 if (m.data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001157 wsTraceRx(m.data);
Thomas Vachuskad472c6e2014-11-07 19:11:05 -08001158 handleServerEvent(JSON.parse(m.data));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001159 }
1160 };
1161
1162 webSock.ws.onclose = function(m) {
1163 webSock.ws = null;
Simon Hunt0c6d4192014-11-12 12:07:10 -08001164 noWebSock(true);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001165 };
1166 },
1167
1168 send : function(text) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001169 if (text != null) {
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001170 webSock._send(text);
1171 }
1172 },
1173
1174 _send : function(message) {
1175 if (webSock.ws) {
1176 webSock.ws.send(message);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001177 } else {
Simon Hunt56d51852014-11-09 13:03:35 -08001178 network.view.alert('no web socket open\n\n' + message);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001179 }
1180 }
1181
1182 };
1183
Simon Hunt0c6d4192014-11-12 12:07:10 -08001184 function noWebSock(b) {
1185 mask.style('display',b ? 'block' : 'none');
1186 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001187
Simon Hunt61d04042014-11-11 17:27:16 -08001188 // TODO: use cache of pending messages (key = sid) to reconcile responses
1189
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001190 function sendMessage(evType, payload) {
1191 var toSend = {
Simon Huntbb282f52014-11-10 11:08:19 -08001192 event: evType,
1193 sid: ++sid,
1194 payload: payload
1195 },
1196 asText = JSON.stringify(toSend);
1197 wsTraceTx(asText);
1198 webSock.send(asText);
1199 }
1200
1201 function wsTraceTx(msg) {
1202 wsTrace('tx', msg);
1203 }
1204 function wsTraceRx(msg) {
1205 wsTrace('rx', msg);
1206 }
1207 function wsTrace(rxtx, msg) {
Simon Huntbb282f52014-11-10 11:08:19 -08001208 console.log('[' + rxtx + '] ' + msg);
1209 // TODO: integrate with trace view
1210 //if (trace) {
1211 // trace.output(rxtx, msg);
1212 //}
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001213 }
1214
1215
1216 // ==============================
1217 // Selection stuff
1218
1219 function selectObject(obj, el) {
1220 var n,
Simon Hunt01095ff2014-11-13 16:37:29 -08001221 srcEv = d3.event.sourceEvent,
1222 meta = srcEv.metaKey,
1223 shift = srcEv.shiftKey;
1224
1225 if ((metaSelect() && !meta) || (!metaSelect() && meta)) {
1226 return;
1227 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001228
1229 if (el) {
1230 n = d3.select(el);
1231 } else {
1232 node.each(function(d) {
1233 if (d == obj) {
1234 n = d3.select(el = this);
1235 }
1236 });
1237 }
1238 if (!n) return;
1239
Simon Hunt01095ff2014-11-13 16:37:29 -08001240 if (shift && n.classed('selected')) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001241 deselectObject(obj.id);
Simon Hunt61d04042014-11-11 17:27:16 -08001242 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001243 return;
1244 }
1245
Simon Hunt01095ff2014-11-13 16:37:29 -08001246 if (!shift) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001247 deselectAll();
1248 }
1249
Simon Huntc31d5692014-11-12 13:27:18 -08001250 selections[obj.id] = { obj: obj, el: el };
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001251 selectOrder.push(obj.id);
1252
1253 n.classed('selected', true);
Simon Hunt61d04042014-11-11 17:27:16 -08001254 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001255 }
1256
1257 function deselectObject(id) {
Simon Huntc31d5692014-11-12 13:27:18 -08001258 var obj = selections[id],
1259 idx;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001260 if (obj) {
1261 d3.select(obj.el).classed('selected', false);
Simon Hunt61d04042014-11-11 17:27:16 -08001262 delete selections[id];
Simon Huntc31d5692014-11-12 13:27:18 -08001263 idx = $.inArray(id, selectOrder);
1264 if (idx >= 0) {
1265 selectOrder.splice(idx, 1);
1266 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001267 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001268 }
1269
1270 function deselectAll() {
1271 // deselect all nodes in the network...
1272 node.classed('selected', false);
1273 selections = {};
1274 selectOrder = [];
Simon Hunt61d04042014-11-11 17:27:16 -08001275 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001276 }
1277
Simon Hunt61d04042014-11-11 17:27:16 -08001278 // update the state of the detail pane, based on current selections
1279 function updateDetailPane() {
1280 var nSel = selectOrder.length;
1281 if (!nSel) {
1282 detailPane.hide();
Simon Huntd72bc702014-11-13 18:38:04 -08001283 showTrafficAction(); // sends cancelTraffic event
Simon Hunt61d04042014-11-11 17:27:16 -08001284 } else if (nSel === 1) {
1285 singleSelect();
1286 } else {
1287 multiSelect();
1288 }
1289 }
1290
1291 function singleSelect() {
1292 requestDetails();
Simon Huntb53e0682014-11-12 13:32:01 -08001293 // NOTE: detail pane will be shown from showDetails event callback
Simon Hunt61d04042014-11-11 17:27:16 -08001294 }
1295
1296 function multiSelect() {
Simon Huntb53e0682014-11-12 13:32:01 -08001297 populateMultiSelect();
Simon Huntb53e0682014-11-12 13:32:01 -08001298 }
1299
1300 function addSep(tbody) {
1301 var tr = tbody.append('tr');
1302 $('<hr>').appendTo(tr.append('td').attr('colspan', 2));
1303 }
1304
1305 function addProp(tbody, label, value) {
1306 var tr = tbody.append('tr');
1307
1308 tr.append('td')
1309 .attr('class', 'label')
1310 .text(label + ' :');
1311
1312 tr.append('td')
1313 .attr('class', 'value')
1314 .text(value);
1315 }
1316
1317 function populateMultiSelect() {
1318 detailPane.empty();
1319
1320 var title = detailPane.append("h2"),
1321 table = detailPane.append("table"),
1322 tbody = table.append("tbody");
1323
1324 title.text('Multi-Select...');
1325
1326 selectOrder.forEach(function (d, i) {
1327 addProp(tbody, i+1, d);
1328 });
Simon Huntd72bc702014-11-13 18:38:04 -08001329
1330 addMultiSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08001331 }
1332
1333 function populateDetails(data) {
1334 detailPane.empty();
1335
1336 var title = detailPane.append("h2"),
1337 table = detailPane.append("table"),
1338 tbody = table.append("tbody");
1339
1340 $('<img src="img/' + data.type + '.png">').appendTo(title);
1341 $('<span>').attr('class', 'icon').text(data.id).appendTo(title);
1342
1343 data.propOrder.forEach(function(p) {
1344 if (p === '-') {
1345 addSep(tbody);
1346 } else {
1347 addProp(tbody, p, data.props[p]);
1348 }
1349 });
Simon Huntd72bc702014-11-13 18:38:04 -08001350
1351 addSingleSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08001352 }
1353
Simon Huntd72bc702014-11-13 18:38:04 -08001354 function addSingleSelectActions() {
1355 detailPane.append('hr');
1356 // always want to allow 'show traffic'
1357 addAction('Show Traffic', showTrafficAction);
1358 }
1359
1360 function addMultiSelectActions() {
1361 detailPane.append('hr');
1362 // always want to allow 'show traffic'
1363 addAction('Show Traffic', showTrafficAction);
1364 // if exactly two hosts are selected, also want 'add host intent'
1365 if (nSel() === 2 && allSelectionsClass('host')) {
1366 addAction('Add Host Intent', addIntentAction);
1367 }
1368 }
1369
1370 function addAction(text, cb) {
1371 detailPane.append('div')
1372 .classed('actionBtn', true)
1373 .text(text)
1374 .on('click', cb);
1375 }
1376
1377
Paul Greysonfcba0e82014-11-13 10:21:16 -08001378 function zoomPan(scale, translate) {
1379 zoomPanContainer.attr("transform", "translate(" + translate + ")scale(" + scale + ")");
1380 // keep the map lines constant width while zooming
1381 bgImg.style("stroke-width", 2.0 / scale + "px");
1382 }
1383
1384 function resetZoomPan() {
1385 zoomPan(1, [0,0]);
1386 zoom.scale(1).translate([0,0]);
1387 }
1388
1389 function setupZoomPan() {
1390 function zoomed() {
Simon Hunt01095ff2014-11-13 16:37:29 -08001391 if (!metaSelect() ^ !d3.event.sourceEvent.metaKey) {
Paul Greysonfcba0e82014-11-13 10:21:16 -08001392 zoomPan(d3.event.scale, d3.event.translate);
1393 }
1394 }
1395
1396 zoom = d3.behavior.zoom()
1397 .translate([0, 0])
1398 .scale(1)
1399 .scaleExtent([1, 8])
1400 .on("zoom", zoomed);
1401
1402 svg.call(zoom);
1403 }
1404
Simon Hunt61d04042014-11-11 17:27:16 -08001405 // ==============================
1406 // Test harness code
Simon Hunt56d51852014-11-09 13:03:35 -08001407
1408 function prepareScenario(view, ctx, dbg) {
1409 var sc = scenario,
1410 urlSc = sc.evDir + ctx + sc.evScenario;
1411
1412 if (!ctx) {
1413 view.alert("No scenario specified (null ctx)");
1414 return;
1415 }
1416
1417 sc.view = view;
1418 sc.ctx = ctx;
1419 sc.debug = dbg;
1420 sc.evNumber = 0;
1421
1422 d3.json(urlSc, function(err, data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001423 var p = data && data.params || {},
1424 desc = data && data.description || null,
Simon Huntfc274c92014-11-11 11:05:46 -08001425 intro = data && data.title;
Simon Huntbb282f52014-11-10 11:08:19 -08001426
Simon Hunt56d51852014-11-09 13:03:35 -08001427 if (err) {
1428 view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err);
1429 } else {
1430 sc.params = p;
Simon Huntbb282f52014-11-10 11:08:19 -08001431 if (desc) {
1432 intro += '\n\n ' + desc.join('\n ');
1433 }
1434 view.alert(intro);
Simon Hunt56d51852014-11-09 13:03:35 -08001435 }
1436 });
1437
1438 }
1439
Simon Hunt01095ff2014-11-13 16:37:29 -08001440 // ==============================
1441 // Toggle Buttons in masthead
Simon Hunt0c6d4192014-11-12 12:07:10 -08001442
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001443 // TODO: toggle button (and other widgets in the masthead) should be provided
1444 // by the framework; not generated by the view.
1445
Simon Hunt01095ff2014-11-13 16:37:29 -08001446 var showTrafficOnHover,
1447 metaToSelect;
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001448
1449 function addButtonBar(view) {
1450 var bb = d3.select('#mast')
1451 .append('span').classed('right', true).attr('id', 'bb');
1452
Simon Hunt01095ff2014-11-13 16:37:29 -08001453 metaToSelect = bb.append('span')
1454 .classed('btn', true)
1455 .text('Meta to select')
1456 .on('click', toggleMetaSelect);
1457
1458 showTrafficOnHover = bb.append('span')
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001459 .classed('btn', true)
1460 .text('Show traffic on hover')
Simon Hunt01095ff2014-11-13 16:37:29 -08001461 .on('click', toggleTrafficHover);
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001462 }
1463
Simon Hunt01095ff2014-11-13 16:37:29 -08001464 function toggleTrafficHover() {
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001465 showTrafficOnHover.classed('active', !trafficHover());
1466 }
1467
1468 function trafficHover() {
1469 return showTrafficOnHover.classed('active');
1470 }
1471
Simon Hunt01095ff2014-11-13 16:37:29 -08001472 function toggleMetaSelect() {
1473 metaToSelect.classed('active', !metaSelect());
1474 }
1475
1476 function metaSelect() {
1477 return metaToSelect.classed('active');
1478 }
1479
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001480 // ==============================
Simon Hunt142d0032014-11-04 20:13:09 -08001481 // View life-cycle callbacks
Simon Hunt195cb382014-11-03 17:50:51 -08001482
Simon Huntf67722a2014-11-10 09:32:06 -08001483 function preload(view, ctx, flags) {
Simon Hunt142d0032014-11-04 20:13:09 -08001484 var w = view.width(),
1485 h = view.height(),
Simon Huntc7ee0662014-11-05 16:44:37 -08001486 fcfg = config.force,
1487 fpad = fcfg.pad,
1488 forceDim = [w - 2*fpad, h - 2*fpad];
Simon Hunt195cb382014-11-03 17:50:51 -08001489
Simon Huntbb282f52014-11-10 11:08:19 -08001490 // TODO: set trace api
1491 //trace = onos.exported.webSockTrace;
1492
Simon Hunt142d0032014-11-04 20:13:09 -08001493 // NOTE: view.$div is a D3 selection of the view's div
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001494 var viewBox = '0 0 ' + config.logicalSize + ' ' + config.logicalSize;
1495 svg = view.$div.append('svg').attr('viewBox', viewBox);
Simon Hunt934c3ce2014-11-05 11:45:07 -08001496 setSize(svg, view);
1497
Paul Greysonfcba0e82014-11-13 10:21:16 -08001498 zoomPanContainer = svg.append('g').attr('id', 'zoomPanContainer');
1499
1500 setupZoomPan();
1501
Simon Hunt1a9eff92014-11-07 11:06:34 -08001502 // add blue glow filter to svg layer
Paul Greysonfcba0e82014-11-13 10:21:16 -08001503 d3u.appendGlow(zoomPanContainer);
Simon Hunt1a9eff92014-11-07 11:06:34 -08001504
Simon Huntc7ee0662014-11-05 16:44:37 -08001505 // group for the topology
Paul Greysonfcba0e82014-11-13 10:21:16 -08001506 topoG = zoomPanContainer.append('g')
Simon Huntd3b7d512014-11-12 15:48:41 -08001507 .attr('id', 'topo-G')
Simon Huntc7ee0662014-11-05 16:44:37 -08001508 .attr('transform', fcfg.translate());
1509
1510 // subgroups for links and nodes
1511 linkG = topoG.append('g').attr('id', 'links');
1512 nodeG = topoG.append('g').attr('id', 'nodes');
1513
1514 // selection of nodes and links
1515 link = linkG.selectAll('.link');
1516 node = nodeG.selectAll('.node');
1517
Simon Hunt7cd48f32014-11-09 23:42:50 -08001518 function chrg(d) {
1519 return fcfg.charge[d.class] || -12000;
1520 }
Simon Hunt99c13842014-11-06 18:23:12 -08001521 function ldist(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08001522 return fcfg.linkDistance[d.type] || 50;
Simon Hunt99c13842014-11-06 18:23:12 -08001523 }
1524 function lstrg(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08001525 // 0.0 - 1.0
1526 return fcfg.linkStrength[d.type] || 1.0;
Simon Hunt99c13842014-11-06 18:23:12 -08001527 }
1528
Simon Hunt1a9eff92014-11-07 11:06:34 -08001529 function selectCb(d, self) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001530 selectObject(d, self);
Simon Hunt1a9eff92014-11-07 11:06:34 -08001531 }
1532
1533 function atDragEnd(d, self) {
Simon Hunt56d51852014-11-09 13:03:35 -08001534 // once we've finished moving, pin the node in position
1535 d.fixed = true;
1536 d3.select(self).classed('fixed', true);
1537 if (config.useLiveData) {
Simon Hunt902c9922014-11-11 11:59:31 -08001538 sendUpdateMeta(d);
Simon Hunt1a9eff92014-11-07 11:06:34 -08001539 }
1540 }
1541
Simon Hunt902c9922014-11-11 11:59:31 -08001542 function sendUpdateMeta(d) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001543 sendMessage('updateMeta', {
1544 id: d.id,
1545 'class': d.class,
Simon Hunt902c9922014-11-11 11:59:31 -08001546 'memento': {
Simon Hunt01095ff2014-11-13 16:37:29 -08001547 x: d.x,
1548 y: d.y
Simon Hunt902c9922014-11-11 11:59:31 -08001549 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001550 });
1551 }
1552
Simon Huntc7ee0662014-11-05 16:44:37 -08001553 // set up the force layout
1554 network.force = d3.layout.force()
1555 .size(forceDim)
1556 .nodes(network.nodes)
1557 .links(network.links)
Simon Hunt7cd48f32014-11-09 23:42:50 -08001558 .gravity(0.4)
1559 .friction(0.7)
1560 .charge(chrg)
Simon Hunt99c13842014-11-06 18:23:12 -08001561 .linkDistance(ldist)
1562 .linkStrength(lstrg)
Simon Huntc7ee0662014-11-05 16:44:37 -08001563 .on('tick', tick);
Simon Hunt195cb382014-11-03 17:50:51 -08001564
Simon Hunt01095ff2014-11-13 16:37:29 -08001565 network.drag = d3u.createDragBehavior(network.force,
1566 selectCb, atDragEnd, metaSelect);
Simon Hunt0c6d4192014-11-12 12:07:10 -08001567
1568 // create mask layer for when we lose connection to server.
1569 mask = view.$div.append('div').attr('id','topo-mask');
1570 para(mask, 'Oops!');
1571 para(mask, 'Web-socket connection to server closed...');
1572 para(mask, 'Try refreshing the page.');
Simon Hunt1a9eff92014-11-07 11:06:34 -08001573 }
Simon Hunt195cb382014-11-03 17:50:51 -08001574
Simon Hunt01095ff2014-11-13 16:37:29 -08001575 function para(sel, text) {
1576 sel.append('p').text(text);
1577 }
1578
1579
Simon Hunt56d51852014-11-09 13:03:35 -08001580 function load(view, ctx, flags) {
Simon Huntf67722a2014-11-10 09:32:06 -08001581 // resize, in case the window was resized while we were not loaded
1582 resize(view, ctx, flags);
1583
Simon Hunt99c13842014-11-06 18:23:12 -08001584 // cache the view token, so network topo functions can access it
1585 network.view = view;
Simon Hunt56d51852014-11-09 13:03:35 -08001586 config.useLiveData = !flags.local;
1587
1588 if (!config.useLiveData) {
1589 prepareScenario(view, ctx, flags.debug);
1590 }
Simon Hunt99c13842014-11-06 18:23:12 -08001591
1592 // set our radio buttons and key bindings
Simon Hunt934c3ce2014-11-05 11:45:07 -08001593 view.setRadio(btnSet);
1594 view.setKeys(keyDispatch);
Simon Hunt195cb382014-11-03 17:50:51 -08001595
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001596 // patch in our "button bar" for now
1597 // TODO: implement a more official frameworky way of doing this..
1598 addButtonBar(view);
1599
Simon Huntd3b7d512014-11-12 15:48:41 -08001600 // Load map data asynchronously; complete startup after that..
1601 loadGeoJsonData();
1602 }
1603
1604 // TODO: move these to config/state portion of script
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001605 var geoJsonUrl = 'json/map/continental_us.json', // TODO: Paul
Simon Huntd3b7d512014-11-12 15:48:41 -08001606 geoJson;
1607
1608 function loadGeoJsonData() {
1609 d3.json(geoJsonUrl, function (err, data) {
1610 if (err) {
1611 // fall back to USA map background
1612 loadStaticMap();
1613 } else {
1614 geoJson = data;
1615 loadGeoMap();
1616 }
1617
1618 // finally, connect to the server...
1619 if (config.useLiveData) {
1620 webSock.connect();
1621 }
1622 });
1623 }
1624
1625 function showBg() {
1626 return config.options.showBackground ? 'visible' : 'hidden';
1627 }
1628
1629 function loadStaticMap() {
1630 fnTrace('loadStaticMap', config.backgroundUrl);
1631 var w = network.view.width(),
1632 h = network.view.height();
1633
1634 // load the background image
1635 bgImg = svg.insert('svg:image', '#topo-G')
1636 .attr({
1637 id: 'topo-bg',
1638 width: w,
1639 height: h,
1640 'xlink:href': config.backgroundUrl
1641 })
1642 .style({
1643 visibility: showBg()
1644 });
1645 }
1646
1647 function loadGeoMap() {
1648 fnTrace('loadGeoMap', geoJsonUrl);
Simon Huntd3b7d512014-11-12 15:48:41 -08001649
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001650 // extracts the topojson data into geocoordinate-based geometry
1651 var topoData = topojson.feature(geoJson, geoJson.objects.states);
Simon Huntd3b7d512014-11-12 15:48:41 -08001652
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001653 // see: http://bl.ocks.org/mbostock/4707858
1654 geoMapProjection = d3.geo.mercator();
1655 var path = d3.geo.path().projection(geoMapProjection);
Simon Huntd3b7d512014-11-12 15:48:41 -08001656
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001657 geoMapProjection
1658 .scale(1)
1659 .translate([0, 0]);
Simon Huntd3b7d512014-11-12 15:48:41 -08001660
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001661 // [[x1,y1],[x2,y2]]
1662 var b = path.bounds(topoData);
Paul Greysonfcba0e82014-11-13 10:21:16 -08001663 // size map to 95% of minimum dimension to fill space
1664 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 -08001665 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 -08001666
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001667 geoMapProjection
1668 .scale(s)
1669 .translate(t);
1670
Paul Greysonfcba0e82014-11-13 10:21:16 -08001671 bgImg = zoomPanContainer.insert("g", '#topo-G');
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001672 bgImg.attr('id', 'map').selectAll('path')
1673 .data(topoData.features)
1674 .enter()
1675 .append('path')
1676 .attr('d', path);
Simon Hunt195cb382014-11-03 17:50:51 -08001677 }
1678
Simon Huntf67722a2014-11-10 09:32:06 -08001679 function resize(view, ctx, flags) {
Simon Hunt934c3ce2014-11-05 11:45:07 -08001680 setSize(svg, view);
Simon Hunt142d0032014-11-04 20:13:09 -08001681 }
1682
1683
1684 // ==============================
1685 // View registration
Simon Hunt195cb382014-11-03 17:50:51 -08001686
Simon Hunt25248912014-11-04 11:25:48 -08001687 onos.ui.addView('topo', {
Simon Hunt142d0032014-11-04 20:13:09 -08001688 preload: preload,
1689 load: load,
1690 resize: resize
Simon Hunt195cb382014-11-03 17:50:51 -08001691 });
1692
Simon Hunt61d04042014-11-11 17:27:16 -08001693 detailPane = onos.ui.addFloatingPanel('topo-detail');
1694
Simon Hunt195cb382014-11-03 17:50:51 -08001695}(ONOS));