blob: 41fcd6be8b6666f673164311d9cc8dcfbc09e5ea [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 Hunt1a9eff92014-11-07 11:06:34 -0800123 M: testMe, // TODO: remove (testing only)
Simon Hunt50128c02014-11-08 13:36:15 -0800124 S: injectStartupEvents, // TODO: remove (testing only)
125 space: injectTestEvent, // TODO: remove (testing only)
Simon Hunt99c13842014-11-06 18:23:12 -0800126
Thomas Vachuska65368e32014-11-08 16:10:20 -0800127 B: toggleBg, // TODO: do we really need this?
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,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800132
Simon Huntb53e0682014-11-12 13:32:01 -0800133 W: requestTraffic, // bag of selections
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800134 X: cancelTraffic,
135 Z: requestPath // host-to-host intent (and monitor)
Simon Hunt934c3ce2014-11-05 11:45:07 -0800136 };
Simon Hunt142d0032014-11-04 20:13:09 -0800137
Simon Hunt195cb382014-11-03 17:50:51 -0800138 // state variables
Simon Hunt99c13842014-11-06 18:23:12 -0800139 var network = {
Simon Hunt50128c02014-11-08 13:36:15 -0800140 view: null, // view token reference
Simon Hunt99c13842014-11-06 18:23:12 -0800141 nodes: [],
142 links: [],
143 lookup: {}
144 },
Simon Hunt56d51852014-11-09 13:03:35 -0800145 scenario = {
146 evDir: 'json/ev/',
147 evScenario: '/scenario.json',
148 evPrefix: '/ev_',
149 evOnos: '_onos.json',
150 evUi: '_ui.json',
151 ctx: null,
152 params: {},
153 evNumber: 0,
154 view: null,
155 debug: false
156 },
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800157 webSock,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800158 sid = 0,
Simon Hunt56d51852014-11-09 13:03:35 -0800159 deviceLabelIndex = 0,
160 hostLabelIndex = 0,
Simon Hunt61d04042014-11-11 17:27:16 -0800161 detailPane,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800162 selectOrder = [],
163 selections = {},
Simon Hunt6ac93f32014-11-13 12:17:27 -0800164 hovered = null,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800165
Simon Hunt195cb382014-11-03 17:50:51 -0800166 highlighted = null,
Simon Hunt195cb382014-11-03 17:50:51 -0800167 viewMode = 'showAll',
168 portLabelsOn = false;
169
Simon Hunt934c3ce2014-11-05 11:45:07 -0800170 // D3 selections
171 var svg,
Paul Greysonfcba0e82014-11-13 10:21:16 -0800172 zoomPanContainer,
Simon Hunt934c3ce2014-11-05 11:45:07 -0800173 bgImg,
Simon Huntc7ee0662014-11-05 16:44:37 -0800174 topoG,
175 nodeG,
176 linkG,
177 node,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800178 link,
179 mask;
Simon Hunt195cb382014-11-03 17:50:51 -0800180
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800181 // the projection for the map background
182 var geoMapProjection;
183
Paul Greysonfcba0e82014-11-13 10:21:16 -0800184 // the zoom function
185 var zoom;
186
Simon Hunt142d0032014-11-04 20:13:09 -0800187 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800188 // For Debugging / Development
Simon Hunt195cb382014-11-03 17:50:51 -0800189
Simon Hunt99c13842014-11-06 18:23:12 -0800190 function note(label, msg) {
191 console.log('NOTE: ' + label + ': ' + msg);
Simon Hunt195cb382014-11-03 17:50:51 -0800192 }
193
Simon Hunt99c13842014-11-06 18:23:12 -0800194 function debug(what) {
195 return config.debugOn && config.debug[what];
Simon Hunt934c3ce2014-11-05 11:45:07 -0800196 }
197
Simon Huntfc274c92014-11-11 11:05:46 -0800198 function fnTrace(msg, id) {
199 if (config.fnTrace) {
200 console.log('FN: ' + msg + ' [' + id + ']');
201 }
202 }
Simon Hunt99c13842014-11-06 18:23:12 -0800203
Simon Hunt934c3ce2014-11-05 11:45:07 -0800204 // ==============================
205 // Key Callbacks
206
Simon Hunt99c13842014-11-06 18:23:12 -0800207 function testMe(view) {
Simon Hunt50128c02014-11-08 13:36:15 -0800208 view.alert('test');
Simon Hunt99c13842014-11-06 18:23:12 -0800209 }
210
Simon Hunt56d51852014-11-09 13:03:35 -0800211 function abortIfLive() {
Simon Hunt50128c02014-11-08 13:36:15 -0800212 if (config.useLiveData) {
Simon Huntb53e0682014-11-12 13:32:01 -0800213 network.view.alert("Sorry, currently using live data..");
Simon Hunt56d51852014-11-09 13:03:35 -0800214 return true;
Simon Hunt50128c02014-11-08 13:36:15 -0800215 }
Simon Hunt56d51852014-11-09 13:03:35 -0800216 return false;
217 }
Simon Hunt50128c02014-11-08 13:36:15 -0800218
Simon Hunt56d51852014-11-09 13:03:35 -0800219 function testDebug(msg) {
220 if (scenario.debug) {
221 scenario.view.alert(msg);
222 }
223 }
Simon Hunt99c13842014-11-06 18:23:12 -0800224
Simon Hunt56d51852014-11-09 13:03:35 -0800225 function injectTestEvent(view) {
226 if (abortIfLive()) { return; }
227 var sc = scenario,
228 evn = ++sc.evNumber,
229 pfx = sc.evDir + sc.ctx + sc.evPrefix + evn,
230 onosUrl = pfx + sc.evOnos,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800231 uiUrl = pfx + sc.evUi,
232 stack = [
233 { url: onosUrl, cb: handleServerEvent },
234 { url: uiUrl, cb: handleUiEvent }
235 ];
236 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800237 }
238
Simon Hunt7cd48f32014-11-09 23:42:50 -0800239 function recurseFetchEvent(stack, evn) {
240 var v = scenario.view,
241 frame;
242 if (stack.length === 0) {
Simon Huntfc274c92014-11-11 11:05:46 -0800243 v.alert('Oops!\n\nNo event #' + evn + ' found.');
Simon Hunt7cd48f32014-11-09 23:42:50 -0800244 return;
245 }
246 frame = stack.shift();
247
248 d3.json(frame.url, function (err, data) {
Simon Hunt99c13842014-11-06 18:23:12 -0800249 if (err) {
Simon Hunt56d51852014-11-09 13:03:35 -0800250 if (err.status === 404) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800251 // if we didn't find the data, try the next stack frame
252 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800253 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800254 v.alert('non-404 error:\n\n' + frame.url + '\n\n' + err);
Simon Hunt56d51852014-11-09 13:03:35 -0800255 }
Simon Hunt99c13842014-11-06 18:23:12 -0800256 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800257 testDebug('loaded: ' + frame.url);
258 frame.cb(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800259 }
260 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800261
Simon Hunt56d51852014-11-09 13:03:35 -0800262 }
Simon Hunt50128c02014-11-08 13:36:15 -0800263
Simon Hunt56d51852014-11-09 13:03:35 -0800264 function handleUiEvent(data) {
Simon Huntbb282f52014-11-10 11:08:19 -0800265 scenario.view.alert('UI Tx: ' + data.event + '\n\n' +
266 JSON.stringify(data));
Simon Hunt56d51852014-11-09 13:03:35 -0800267 }
268
269 function injectStartupEvents(view) {
270 var last = scenario.params.lastAuto || 0;
271 if (abortIfLive()) { return; }
272
273 while (scenario.evNumber < last) {
Simon Hunt1a9eff92014-11-07 11:06:34 -0800274 injectTestEvent(view);
275 }
276 }
277
Simon Hunt934c3ce2014-11-05 11:45:07 -0800278 function toggleBg() {
279 var vis = bgImg.style('visibility');
280 bgImg.style('visibility', (vis === 'hidden') ? 'visible' : 'hidden');
281 }
282
Simon Hunt99c13842014-11-06 18:23:12 -0800283 function cycleLabels() {
Simon Huntbb282f52014-11-10 11:08:19 -0800284 deviceLabelIndex = (deviceLabelIndex === network.deviceLabelCount - 1)
285 ? 0 : deviceLabelIndex + 1;
Simon Hunt5f36d342014-11-08 21:33:14 -0800286
Simon Hunt99c13842014-11-06 18:23:12 -0800287 network.nodes.forEach(function (d) {
Simon Huntbb282f52014-11-10 11:08:19 -0800288 if (d.class === 'device') {
289 updateDeviceLabel(d);
290 }
Simon Hunt99c13842014-11-06 18:23:12 -0800291 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800292 }
293
294 function togglePorts(view) {
Simon Hunt50128c02014-11-08 13:36:15 -0800295 view.alert('togglePorts() callback')
Simon Hunt934c3ce2014-11-05 11:45:07 -0800296 }
297
Simon Hunt6ac93f32014-11-13 12:17:27 -0800298 function unpin() {
299 if (hovered) {
300 hovered.fixed = false;
301 hovered.el.classed('fixed', false);
302 network.force.resume();
303 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800304 }
305
306 // ==============================
307 // Radio Button Callbacks
308
Simon Hunt195cb382014-11-03 17:50:51 -0800309 function showAllLayers() {
Simon Hunt142d0032014-11-04 20:13:09 -0800310// network.node.classed('inactive', false);
311// network.link.classed('inactive', false);
312// d3.selectAll('svg .port').classed('inactive', false);
313// d3.selectAll('svg .portText').classed('inactive', false);
Simon Hunt934c3ce2014-11-05 11:45:07 -0800314 // TODO ...
Simon Hunt50128c02014-11-08 13:36:15 -0800315 network.view.alert('showAllLayers() callback');
Simon Hunt195cb382014-11-03 17:50:51 -0800316 }
317
318 function showPacketLayer() {
Simon Hunt934c3ce2014-11-05 11:45:07 -0800319 showAllLayers();
320 // TODO ...
Simon Hunt50128c02014-11-08 13:36:15 -0800321 network.view.alert('showPacketLayer() callback');
Simon Hunt195cb382014-11-03 17:50:51 -0800322 }
323
324 function showOpticalLayer() {
Simon Hunt934c3ce2014-11-05 11:45:07 -0800325 showAllLayers();
326 // TODO ...
Simon Hunt50128c02014-11-08 13:36:15 -0800327 network.view.alert('showOpticalLayer() callback');
Simon Hunt195cb382014-11-03 17:50:51 -0800328 }
329
Simon Hunt142d0032014-11-04 20:13:09 -0800330 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800331 // Private functions
332
Simon Hunt99c13842014-11-06 18:23:12 -0800333 function safeId(s) {
334 return s.replace(/[^a-z0-9]/gi, '-');
335 }
336
Simon Huntc7ee0662014-11-05 16:44:37 -0800337 // set the size of the given element to that of the view (reduced if padded)
338 function setSize(el, view, pad) {
339 var padding = pad ? pad * 2 : 0;
Simon Hunt934c3ce2014-11-05 11:45:07 -0800340 el.attr({
Simon Huntc7ee0662014-11-05 16:44:37 -0800341 width: view.width() - padding,
342 height: view.height() - padding
Simon Hunt934c3ce2014-11-05 11:45:07 -0800343 });
344 }
345
Simon Hunt934c3ce2014-11-05 11:45:07 -0800346
Simon Hunt99c13842014-11-06 18:23:12 -0800347 // ==============================
348 // Event handlers for server-pushed events
349
Simon Huntbb282f52014-11-10 11:08:19 -0800350 function logicError(msg) {
351 // TODO, report logic error to server, via websock, so it can be logged
352 network.view.alert('Logic Error:\n\n' + msg);
Simon Huntfc274c92014-11-11 11:05:46 -0800353 console.warn(msg);
Simon Huntbb282f52014-11-10 11:08:19 -0800354 }
355
Simon Hunt99c13842014-11-06 18:23:12 -0800356 var eventDispatch = {
357 addDevice: addDevice,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800358 addLink: addLink,
Simon Hunt56d51852014-11-09 13:03:35 -0800359 addHost: addHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800360
Simon Huntbb282f52014-11-10 11:08:19 -0800361 updateDevice: updateDevice,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800362 updateLink: updateLink,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800363 updateHost: updateHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800364
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);
Simon Huntb53e0682014-11-12 13:32:01 -0800484 // TODO: Add single-select actions ...
Simon Hunt61d04042014-11-11 17:27:16 -0800485 detailPane.show();
486 }
487
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800488 function showPath(data) {
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 });
503
504 // TODO: add selection-highlite lines to links
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800505 }
506
Simon Huntb53e0682014-11-12 13:32:01 -0800507 function showTraffic(data) {
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 });
526 //network.view.alert("showTraffic() -- TODO")
Simon Huntb53e0682014-11-12 13:32:01 -0800527 }
528
Simon Hunt56d51852014-11-09 13:03:35 -0800529 // ...............................
530
531 function stillToImplement(data) {
532 var p = data.payload;
533 note(data.event, p.id);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800534 network.view.alert('Not yet implemented: "' + data.event + '"');
Simon Hunt56d51852014-11-09 13:03:35 -0800535 }
Simon Hunt99c13842014-11-06 18:23:12 -0800536
537 function unknownEvent(data) {
Simon Hunt50128c02014-11-08 13:36:15 -0800538 network.view.alert('Unknown event type: "' + data.event + '"');
Simon Hunt99c13842014-11-06 18:23:12 -0800539 }
540
541 function handleServerEvent(data) {
542 var fn = eventDispatch[data.event] || unknownEvent;
543 fn(data);
544 }
545
546 // ==============================
Simon Hunt61d04042014-11-11 17:27:16 -0800547 // Out-going messages...
548
Simon Huntb53e0682014-11-12 13:32:01 -0800549 function userFeedback(msg) {
550 // for now, use the alert pane as is. Maybe different alert style in
551 // the future (centered on view; dismiss button?)
552 network.view.alert(msg);
553 }
554
555 function nSel() {
556 return selectOrder.length;
557 }
Simon Hunt61d04042014-11-11 17:27:16 -0800558 function getSel(idx) {
559 return selections[selectOrder[idx]];
560 }
Simon Huntb53e0682014-11-12 13:32:01 -0800561 function getSelId(idx) {
562 return getSel(idx).obj.id;
563 }
564 function allSelectionsClass(cls) {
565 for (var i=0, n=nSel(); i<n; i++) {
566 if (getSel(i).obj.class !== cls) {
567 return false;
568 }
569 }
570 return true;
571 }
Simon Hunt61d04042014-11-11 17:27:16 -0800572
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800573 function requestTraffic(hoverNode) {
574 if (nSel() > 0 || hoverNode) {
575 var nodes = hoverNode ? selectOrder.concat(hoverNode.id) : selectOrder;
Simon Huntb53e0682014-11-12 13:32:01 -0800576 sendMessage('requestTraffic', {
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800577 ids: nodes
Simon Huntb53e0682014-11-12 13:32:01 -0800578 });
579 } else {
580 userFeedback('Request-Traffic requires one or\n' +
581 'more items to be selected.');
582 }
583 }
584
Simon Hunt61d04042014-11-11 17:27:16 -0800585 function requestPath() {
Simon Huntb53e0682014-11-12 13:32:01 -0800586 if (nSel() === 2 && allSelectionsClass('host')) {
587 sendMessage('requestPath', {
588 one: getSelId(0),
589 two: getSelId(1)
590 });
591 } else {
592 userFeedback('Request-Path requires two\n' +
593 'hosts to be selected.');
594 }
Simon Hunt61d04042014-11-11 17:27:16 -0800595 }
596
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800597 function cancelTraffic(hoverNode) {
598 if (hoverNode && selectOrder.length) {
599 requestTraffic();
600 } else {
601 // FIXME: from where do we get the intent id(s) to send to the server?
602 sendMessage('cancelTraffic', {
603 ids: ["need_the_intent_id"]
604 });
605 }
Simon Huntac9e24f2014-11-12 10:12:21 -0800606 }
607
Simon Hunt61d04042014-11-11 17:27:16 -0800608 // request details for the selected element
609 function requestDetails() {
610 var data = getSel(0).obj,
611 payload = {
612 id: data.id,
613 class: data.class
614 };
615 sendMessage('requestDetails', payload);
616 }
617
618 // ==============================
Simon Hunt99c13842014-11-06 18:23:12 -0800619 // force layout modification functions
620
621 function translate(x, y) {
622 return 'translate(' + x + ',' + y + ')';
623 }
624
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800625 function missMsg(what, id) {
626 return '\n[' + what + '] "' + id + '" missing ';
627 }
628
629 function linkEndPoints(srcId, dstId) {
630 var srcNode = network.lookup[srcId],
631 dstNode = network.lookup[dstId],
632 sMiss = !srcNode ? missMsg('src', srcId) : '',
633 dMiss = !dstNode ? missMsg('dst', dstId) : '';
634
635 if (sMiss || dMiss) {
636 logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
637 return null;
638 }
639 return {
640 source: srcNode,
641 target: dstNode,
642 x1: srcNode.x,
643 y1: srcNode.y,
644 x2: dstNode.x,
645 y2: dstNode.y
646 };
647 }
648
Simon Hunt56d51852014-11-09 13:03:35 -0800649 function createHostLink(host) {
650 var src = host.id,
651 dst = host.cp.device,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800652 id = host.ingress,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800653 lnk = linkEndPoints(src, dst);
Simon Hunt56d51852014-11-09 13:03:35 -0800654
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800655 if (!lnk) {
Simon Hunt56d51852014-11-09 13:03:35 -0800656 return null;
657 }
658
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800659 // Synthesize link ...
660 $.extend(lnk, {
Thomas Vachuska4830d392014-11-09 17:09:56 -0800661 id: id,
Simon Hunt56d51852014-11-09 13:03:35 -0800662 class: 'link',
Simon Hunt7cd48f32014-11-09 23:42:50 -0800663 type: 'hostLink',
Simon Hunt56d51852014-11-09 13:03:35 -0800664 svgClass: 'link hostLink',
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800665 linkWidth: 1
Simon Hunt7cd48f32014-11-09 23:42:50 -0800666 });
Simon Hunt99c13842014-11-06 18:23:12 -0800667 return lnk;
668 }
669
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800670 function createLink(link) {
671 var lnk = linkEndPoints(link.src, link.dst),
672 type = link.type;
673
674 if (!lnk) {
675 return null;
676 }
677
678 // merge in remaining data
679 $.extend(lnk, link, {
680 class: 'link',
681 svgClass: type ? 'link ' + type : 'link'
682 });
683 return lnk;
Simon Hunt1a9eff92014-11-07 11:06:34 -0800684 }
685
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800686 var widthRatio = 1.4,
687 linkScale = d3.scale.linear()
688 .domain([1, 12])
689 .range([widthRatio, 12 * widthRatio])
690 .clamp(true);
691
692 function updateLinkWidth (d) {
693 // TODO: watch out for .showPath/.showTraffic classes
694 d.el.transition()
695 .duration(1000)
696 .attr('stroke-width', linkScale(d.linkWidth));
697 }
698
699
Simon Hunt99c13842014-11-06 18:23:12 -0800700 function updateLinks() {
701 link = linkG.selectAll('.link')
702 .data(network.links, function (d) { return d.id; });
703
704 // operate on existing links, if necessary
705 // link .foo() .bar() ...
706
707 // operate on entering links:
708 var entering = link.enter()
709 .append('line')
710 .attr({
Simon Hunt99c13842014-11-06 18:23:12 -0800711 class: function (d) { return d.svgClass; },
712 x1: function (d) { return d.x1; },
713 y1: function (d) { return d.y1; },
714 x2: function (d) { return d.x2; },
715 y2: function (d) { return d.y2; },
Simon Hunt1a9eff92014-11-07 11:06:34 -0800716 stroke: config.topo.linkInColor,
717 'stroke-width': config.topo.linkInWidth
Simon Hunt99c13842014-11-06 18:23:12 -0800718 })
719 .transition().duration(1000)
720 .attr({
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800721 'stroke-width': function (d) { return linkScale(d.linkWidth); },
Simon Hunt99c13842014-11-06 18:23:12 -0800722 stroke: '#666' // TODO: remove explicit stroke, rather...
723 });
724
725 // augment links
Simon Hunt7cd48f32014-11-09 23:42:50 -0800726 entering.each(function (d) {
727 var link = d3.select(this);
728 // provide ref to element selection from backing data....
729 d.el = link;
Simon Hunt99c13842014-11-06 18:23:12 -0800730
Simon Hunt7cd48f32014-11-09 23:42:50 -0800731 // TODO: add src/dst port labels etc.
732 });
Thomas Vachuska4830d392014-11-09 17:09:56 -0800733
734 // operate on both existing and new links, if necessary
735 //link .foo() .bar() ...
736
737 // operate on exiting links:
Thomas Vachuska4830d392014-11-09 17:09:56 -0800738 link.exit()
Thomas Vachuska4830d392014-11-09 17:09:56 -0800739 .attr({
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800740 'stroke-dasharray': '3, 3'
Thomas Vachuska4830d392014-11-09 17:09:56 -0800741 })
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800742 .style('opacity', 0.4)
743 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -0800744 .duration(1500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800745 .attr({
746 'stroke-dasharray': '3, 12'
747 })
748 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -0800749 .duration(500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800750 .style('opacity', 0.0)
Thomas Vachuska4830d392014-11-09 17:09:56 -0800751 .remove();
Simon Hunt99c13842014-11-06 18:23:12 -0800752 }
753
754 function createDeviceNode(device) {
755 // start with the object as is
756 var node = device,
Simon Huntbb282f52014-11-10 11:08:19 -0800757 type = device.type,
758 svgCls = type ? 'node device ' + type : 'node device';
Simon Hunt99c13842014-11-06 18:23:12 -0800759
760 // Augment as needed...
761 node.class = 'device';
Simon Huntbb282f52014-11-10 11:08:19 -0800762 node.svgClass = device.online ? svgCls + ' online' : svgCls;
Simon Hunt99c13842014-11-06 18:23:12 -0800763 positionNode(node);
764
765 // cache label array length
766 network.deviceLabelCount = device.labels.length;
Simon Hunt99c13842014-11-06 18:23:12 -0800767 return node;
768 }
769
Simon Hunt56d51852014-11-09 13:03:35 -0800770 function createHostNode(host) {
771 // start with the object as is
772 var node = host;
773
774 // Augment as needed...
775 node.class = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -0800776 if (!node.type) {
777 // TODO: perhaps type would be: {phone, tablet, laptop, endstation} ?
778 node.type = 'endstation';
779 }
Simon Hunt56d51852014-11-09 13:03:35 -0800780 node.svgClass = 'node host';
781 // TODO: consider placing near its switch, if [x,y] not defined
782 positionNode(node);
783
784 // cache label array length
785 network.hostLabelCount = host.labels.length;
Simon Hunt56d51852014-11-09 13:03:35 -0800786 return node;
787 }
788
Simon Hunt99c13842014-11-06 18:23:12 -0800789 function positionNode(node) {
790 var meta = node.metaUi,
Simon Huntac9e24f2014-11-12 10:12:21 -0800791 x = meta && meta.x,
792 y = meta && meta.y,
793 xy;
Simon Hunt99c13842014-11-06 18:23:12 -0800794
Simon Huntac9e24f2014-11-12 10:12:21 -0800795 // If we have [x,y] already, use that...
Simon Hunt99c13842014-11-06 18:23:12 -0800796 if (x && y) {
797 node.fixed = true;
Simon Huntac9e24f2014-11-12 10:12:21 -0800798 node.x = x;
799 node.y = y;
800 return;
Simon Hunt99c13842014-11-06 18:23:12 -0800801 }
Simon Huntac9e24f2014-11-12 10:12:21 -0800802
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800803 var location = node.location;
804 if (location && location.type === 'latlng') {
805 var coord = geoMapProjection([location.lng, location.lat]);
806 node.fixed = true;
807 node.x = coord[0];
808 node.y = coord[1];
809 return;
810 }
811
Simon Huntac9e24f2014-11-12 10:12:21 -0800812 // Note: Placing incoming unpinned nodes at exactly the same point
813 // (center of the view) causes them to explode outwards when
814 // the force layout kicks in. So, we spread them out a bit
815 // initially, to provide a more serene layout convergence.
816 // Additionally, if the node is a host, we place it near
817 // the device it is connected to.
818
819 function spread(s) {
820 return Math.floor((Math.random() * s) - s/2);
821 }
822
823 function randDim(dim) {
824 return dim / 2 + spread(dim * 0.7071);
825 }
826
827 function rand() {
828 return {
829 x: randDim(network.view.width()),
830 y: randDim(network.view.height())
831 };
832 }
833
834 function near(node) {
835 var min = 12,
836 dx = spread(12),
837 dy = spread(12);
838 return {
839 x: node.x + min + dx,
840 y: node.y + min + dy
841 };
842 }
843
844 function getDevice(cp) {
845 var d = network.lookup[cp.device];
846 return d || rand();
847 }
848
849 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
850 $.extend(node, xy);
Simon Hunt99c13842014-11-06 18:23:12 -0800851 }
852
Simon Hunt99c13842014-11-06 18:23:12 -0800853 function iconUrl(d) {
854 return 'img/' + d.type + '.png';
855 }
856
857 // returns the newly computed bounding box of the rectangle
858 function adjustRectToFitText(n) {
859 var text = n.select('text'),
860 box = text.node().getBBox(),
861 lab = config.labels;
862
863 text.attr('text-anchor', 'middle')
864 .attr('y', '-0.8em')
865 .attr('x', lab.imgPad/2);
866
867 // translate the bbox so that it is centered on [x,y]
868 box.x = -box.width / 2;
869 box.y = -box.height / 2;
870
871 // add padding
872 box.x -= (lab.padLR + lab.imgPad/2);
873 box.width += lab.padLR * 2 + lab.imgPad;
874 box.y -= lab.padTB;
875 box.height += lab.padTB * 2;
876
877 return box;
878 }
879
Simon Hunt1a9eff92014-11-07 11:06:34 -0800880 function mkSvgClass(d) {
881 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
882 }
883
Simon Hunt7cd48f32014-11-09 23:42:50 -0800884 function hostLabel(d) {
885 var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0;
886 return d.labels[idx];
887 }
888 function deviceLabel(d) {
889 var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0;
890 return d.labels[idx];
891 }
892 function niceLabel(label) {
893 return (label && label.trim()) ? label : '.';
894 }
895
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800896 function updateDeviceLabel(d) {
897 var label = niceLabel(deviceLabel(d)),
898 node = d.el,
899 box;
900
901 node.select('text')
902 .text(label)
903 .style('opacity', 0)
904 .transition()
905 .style('opacity', 1);
906
907 box = adjustRectToFitText(node);
908
909 node.select('rect')
910 .transition()
911 .attr(box);
912
913 node.select('image')
914 .transition()
915 .attr('x', box.x + config.icons.xoff)
916 .attr('y', box.y + config.icons.yoff);
917 }
918
919 function updateHostLabel(d) {
920 var label = hostLabel(d),
921 host = d.el;
922
923 host.select('text').text(label);
924 }
925
Simon Huntbb282f52014-11-10 11:08:19 -0800926 function updateDeviceState(nodeData) {
927 nodeData.el.classed('online', nodeData.online);
928 updateDeviceLabel(nodeData);
929 // TODO: review what else might need to be updated
930 }
931
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800932 function updateLinkState(linkData) {
933 updateLinkWidth(linkData);
934 // TODO: review what else might need to be updated
935 // update label, if showing
936 }
937
Simon Huntbb282f52014-11-10 11:08:19 -0800938 function updateHostState(hostData) {
939 updateHostLabel(hostData);
940 // TODO: review what else might need to be updated
941 }
942
Simon Hunt6ac93f32014-11-13 12:17:27 -0800943 function nodeMouseOver(d) {
944 console.log("Hover:", d);
945 hovered = d;
946 if (d.class === 'host') {
947 //requestTraffic(d);
948 }
949 }
950
951 function nodeMouseOut(d) {
952 console.log("Unhover:", d);
953 hovered = null;
954 if (d.class === 'host') {
955 //cancelTraffic(d);
956 }
957 }
Simon Huntbb282f52014-11-10 11:08:19 -0800958
Simon Hunt99c13842014-11-06 18:23:12 -0800959 function updateNodes() {
960 node = nodeG.selectAll('.node')
961 .data(network.nodes, function (d) { return d.id; });
962
963 // operate on existing nodes, if necessary
Simon Hunt7cd48f32014-11-09 23:42:50 -0800964 // update host labels
Simon Hunt99c13842014-11-06 18:23:12 -0800965 //node .foo() .bar() ...
966
967 // operate on entering nodes:
968 var entering = node.enter()
969 .append('g')
970 .attr({
971 id: function (d) { return safeId(d.id); },
Simon Hunt1a9eff92014-11-07 11:06:34 -0800972 class: mkSvgClass,
Simon Hunt99c13842014-11-06 18:23:12 -0800973 transform: function (d) { return translate(d.x, d.y); },
974 opacity: 0
975 })
Simon Hunt1a9eff92014-11-07 11:06:34 -0800976 .call(network.drag)
Simon Hunt6ac93f32014-11-13 12:17:27 -0800977 .on('mouseover', nodeMouseOver)
978 .on('mouseout', nodeMouseOut)
Simon Hunt99c13842014-11-06 18:23:12 -0800979 .transition()
980 .attr('opacity', 1);
981
982 // augment device nodes...
983 entering.filter('.device').each(function (d) {
984 var node = d3.select(this),
985 icon = iconUrl(d),
Simon Hunt7cd48f32014-11-09 23:42:50 -0800986 label = niceLabel(deviceLabel(d)),
Simon Hunt99c13842014-11-06 18:23:12 -0800987 box;
988
Simon Hunt7cd48f32014-11-09 23:42:50 -0800989 // provide ref to element from backing data....
990 d.el = node;
991
Simon Hunt99c13842014-11-06 18:23:12 -0800992 node.append('rect')
993 .attr({
994 'rx': 5,
995 'ry': 5
996 });
997
998 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -0800999 .text(label)
Simon Hunt99c13842014-11-06 18:23:12 -08001000 .attr('dy', '1.1em');
1001
1002 box = adjustRectToFitText(node);
1003
1004 node.select('rect')
1005 .attr(box);
1006
1007 if (icon) {
1008 var cfg = config.icons;
1009 node.append('svg:image')
1010 .attr({
1011 x: box.x + config.icons.xoff,
1012 y: box.y + config.icons.yoff,
1013 width: cfg.w,
1014 height: cfg.h,
1015 'xlink:href': icon
1016 });
1017 }
1018
1019 // debug function to show the modelled x,y coordinates of nodes...
1020 if (debug('showNodeXY')) {
1021 node.select('rect').attr('fill-opacity', 0.5);
1022 node.append('circle')
1023 .attr({
1024 class: 'debug',
1025 cx: 0,
1026 cy: 0,
1027 r: '3px'
1028 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001029 }
1030 });
Simon Hunt934c3ce2014-11-05 11:45:07 -08001031
Simon Hunt56d51852014-11-09 13:03:35 -08001032 // augment host nodes...
1033 entering.filter('.host').each(function (d) {
1034 var node = d3.select(this),
Simon Hunt56d51852014-11-09 13:03:35 -08001035 box;
1036
Simon Hunt7cd48f32014-11-09 23:42:50 -08001037 // provide ref to element from backing data....
1038 d.el = node;
1039
Simon Hunt56d51852014-11-09 13:03:35 -08001040 node.append('circle')
1041 .attr('r', 8); // TODO: define host circle radius
1042
1043 // TODO: are we attaching labels to hosts?
1044 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001045 .text(hostLabel)
1046 .attr('dy', '1.3em')
1047 .attr('text-anchor', 'middle');
Simon Hunt56d51852014-11-09 13:03:35 -08001048
1049 // debug function to show the modelled x,y coordinates of nodes...
1050 if (debug('showNodeXY')) {
1051 node.select('circle').attr('fill-opacity', 0.5);
1052 node.append('circle')
1053 .attr({
1054 class: 'debug',
1055 cx: 0,
1056 cy: 0,
1057 r: '3px'
1058 });
1059 }
1060 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001061
Simon Hunt99c13842014-11-06 18:23:12 -08001062 // operate on both existing and new nodes, if necessary
1063 //node .foo() .bar() ...
Simon Huntc7ee0662014-11-05 16:44:37 -08001064
Simon Hunt99c13842014-11-06 18:23:12 -08001065 // operate on exiting nodes:
Simon Huntea80eb42014-11-11 13:46:57 -08001066 // Note that the node is removed after 2 seconds.
1067 // Sub element animations should be shorter than 2 seconds.
1068 var exiting = node.exit()
Simon Hunt44031102014-11-11 13:20:36 -08001069 .transition()
1070 .duration(2000)
Simon Huntea80eb42014-11-11 13:46:57 -08001071 .style('opacity', 0)
Simon Hunt99c13842014-11-06 18:23:12 -08001072 .remove();
Simon Huntea80eb42014-11-11 13:46:57 -08001073
1074 // host node exits....
1075 exiting.filter('.host').each(function (d) {
1076 var node = d3.select(this);
1077
1078 node.select('text')
1079 .style('opacity', 0.5)
1080 .transition()
1081 .duration(1000)
1082 .style('opacity', 0);
1083 // note, leave <g>.remove to remove this element
1084
1085 node.select('circle')
1086 .style('stroke-fill', '#555')
1087 .style('fill', '#888')
1088 .style('opacity', 0.5)
1089 .transition()
1090 .duration(1500)
1091 .attr('r', 0);
1092 // note, leave <g>.remove to remove this element
1093
1094 });
1095
1096 // TODO: device node exits
Simon Huntc7ee0662014-11-05 16:44:37 -08001097 }
1098
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001099 function find(id, array) {
1100 for (var idx = 0, n = array.length; idx < n; idx++) {
1101 if (array[idx].id === id) {
1102 return idx;
1103 }
1104 }
1105 return -1;
1106 }
1107
1108 function removeLinkElement(linkData) {
1109 // remove from lookup cache
1110 delete network.lookup[linkData.id];
1111 // remove from links array
1112 var idx = find(linkData.id, network.links);
Simon Huntfc274c92014-11-11 11:05:46 -08001113 network.links.splice(idx, 1);
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001114 // remove from SVG
1115 updateLinks();
Simon Hunt44031102014-11-11 13:20:36 -08001116 network.force.resume();
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001117 }
Simon Huntc7ee0662014-11-05 16:44:37 -08001118
Simon Hunt44031102014-11-11 13:20:36 -08001119 function removeHostElement(hostData) {
1120 // first, remove associated hostLink...
1121 removeLinkElement(hostData.linkData);
1122
1123 // remove from lookup cache
1124 delete network.lookup[hostData.id];
1125 // remove from nodes array
1126 var idx = find(hostData.id, network.nodes);
1127 network.nodes.splice(idx, 1);
1128 // remove from SVG
1129 updateNodes();
1130 network.force.resume();
1131 }
1132
1133
Simon Huntc7ee0662014-11-05 16:44:37 -08001134 function tick() {
1135 node.attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001136 transform: function (d) { return translate(d.x, d.y); }
Simon Huntc7ee0662014-11-05 16:44:37 -08001137 });
1138
1139 link.attr({
1140 x1: function (d) { return d.source.x; },
1141 y1: function (d) { return d.source.y; },
1142 x2: function (d) { return d.target.x; },
1143 y2: function (d) { return d.target.y; }
1144 });
1145 }
Simon Hunt934c3ce2014-11-05 11:45:07 -08001146
1147 // ==============================
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001148 // Web-Socket for live data
1149
1150 function webSockUrl() {
1151 return document.location.toString()
1152 .replace(/\#.*/, '')
1153 .replace('http://', 'ws://')
1154 .replace('https://', 'wss://')
1155 .replace('index2.html', config.webSockUrl);
1156 }
1157
1158 webSock = {
1159 ws : null,
1160
1161 connect : function() {
1162 webSock.ws = new WebSocket(webSockUrl());
1163
1164 webSock.ws.onopen = function() {
Simon Hunt0c6d4192014-11-12 12:07:10 -08001165 noWebSock(false);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001166 };
1167
1168 webSock.ws.onmessage = function(m) {
1169 if (m.data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001170 wsTraceRx(m.data);
Thomas Vachuskad472c6e2014-11-07 19:11:05 -08001171 handleServerEvent(JSON.parse(m.data));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001172 }
1173 };
1174
1175 webSock.ws.onclose = function(m) {
1176 webSock.ws = null;
Simon Hunt0c6d4192014-11-12 12:07:10 -08001177 noWebSock(true);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001178 };
1179 },
1180
1181 send : function(text) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001182 if (text != null) {
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001183 webSock._send(text);
1184 }
1185 },
1186
1187 _send : function(message) {
1188 if (webSock.ws) {
1189 webSock.ws.send(message);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001190 } else {
Simon Hunt56d51852014-11-09 13:03:35 -08001191 network.view.alert('no web socket open\n\n' + message);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001192 }
1193 }
1194
1195 };
1196
Simon Hunt0c6d4192014-11-12 12:07:10 -08001197 function noWebSock(b) {
1198 mask.style('display',b ? 'block' : 'none');
1199 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001200
Simon Hunt61d04042014-11-11 17:27:16 -08001201 // TODO: use cache of pending messages (key = sid) to reconcile responses
1202
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001203 function sendMessage(evType, payload) {
1204 var toSend = {
Simon Huntbb282f52014-11-10 11:08:19 -08001205 event: evType,
1206 sid: ++sid,
1207 payload: payload
1208 },
1209 asText = JSON.stringify(toSend);
1210 wsTraceTx(asText);
1211 webSock.send(asText);
1212 }
1213
1214 function wsTraceTx(msg) {
1215 wsTrace('tx', msg);
1216 }
1217 function wsTraceRx(msg) {
1218 wsTrace('rx', msg);
1219 }
1220 function wsTrace(rxtx, msg) {
Simon Huntbb282f52014-11-10 11:08:19 -08001221 console.log('[' + rxtx + '] ' + msg);
1222 // TODO: integrate with trace view
1223 //if (trace) {
1224 // trace.output(rxtx, msg);
1225 //}
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001226 }
1227
1228
1229 // ==============================
1230 // Selection stuff
1231
1232 function selectObject(obj, el) {
1233 var n,
1234 meta = d3.event.sourceEvent.metaKey;
1235
1236 if (el) {
1237 n = d3.select(el);
1238 } else {
1239 node.each(function(d) {
1240 if (d == obj) {
1241 n = d3.select(el = this);
1242 }
1243 });
1244 }
1245 if (!n) return;
1246
1247 if (meta && n.classed('selected')) {
1248 deselectObject(obj.id);
Simon Hunt61d04042014-11-11 17:27:16 -08001249 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001250 return;
1251 }
1252
1253 if (!meta) {
1254 deselectAll();
1255 }
1256
Simon Huntc31d5692014-11-12 13:27:18 -08001257 selections[obj.id] = { obj: obj, el: el };
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001258 selectOrder.push(obj.id);
1259
1260 n.classed('selected', true);
Simon Hunt61d04042014-11-11 17:27:16 -08001261 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001262 }
1263
1264 function deselectObject(id) {
Simon Huntc31d5692014-11-12 13:27:18 -08001265 var obj = selections[id],
1266 idx;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001267 if (obj) {
1268 d3.select(obj.el).classed('selected', false);
Simon Hunt61d04042014-11-11 17:27:16 -08001269 delete selections[id];
Simon Huntc31d5692014-11-12 13:27:18 -08001270 idx = $.inArray(id, selectOrder);
1271 if (idx >= 0) {
1272 selectOrder.splice(idx, 1);
1273 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001274 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001275 }
1276
1277 function deselectAll() {
1278 // deselect all nodes in the network...
1279 node.classed('selected', false);
1280 selections = {};
1281 selectOrder = [];
Simon Hunt61d04042014-11-11 17:27:16 -08001282 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001283 }
1284
Simon Hunt61d04042014-11-11 17:27:16 -08001285 // FIXME: this click handler does not get unloaded when the view does
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001286 $('#view').on('click', function(e) {
1287 if (!$(e.target).closest('.node').length) {
1288 if (!e.metaKey) {
1289 deselectAll();
1290 }
1291 }
1292 });
1293
Simon Hunt61d04042014-11-11 17:27:16 -08001294 // update the state of the detail pane, based on current selections
1295 function updateDetailPane() {
1296 var nSel = selectOrder.length;
1297 if (!nSel) {
1298 detailPane.hide();
1299 } else if (nSel === 1) {
1300 singleSelect();
1301 } else {
1302 multiSelect();
1303 }
1304 }
1305
1306 function singleSelect() {
1307 requestDetails();
Simon Huntb53e0682014-11-12 13:32:01 -08001308 // NOTE: detail pane will be shown from showDetails event callback
Simon Hunt61d04042014-11-11 17:27:16 -08001309 }
1310
1311 function multiSelect() {
Simon Huntb53e0682014-11-12 13:32:01 -08001312 populateMultiSelect();
1313 // TODO: Add multi-select actions ...
1314 }
1315
1316 function addSep(tbody) {
1317 var tr = tbody.append('tr');
1318 $('<hr>').appendTo(tr.append('td').attr('colspan', 2));
1319 }
1320
1321 function addProp(tbody, label, value) {
1322 var tr = tbody.append('tr');
1323
1324 tr.append('td')
1325 .attr('class', 'label')
1326 .text(label + ' :');
1327
1328 tr.append('td')
1329 .attr('class', 'value')
1330 .text(value);
1331 }
1332
1333 function populateMultiSelect() {
1334 detailPane.empty();
1335
1336 var title = detailPane.append("h2"),
1337 table = detailPane.append("table"),
1338 tbody = table.append("tbody");
1339
1340 title.text('Multi-Select...');
1341
1342 selectOrder.forEach(function (d, i) {
1343 addProp(tbody, i+1, d);
1344 });
Simon Hunt61d04042014-11-11 17:27:16 -08001345 }
1346
1347 function populateDetails(data) {
1348 detailPane.empty();
1349
1350 var title = detailPane.append("h2"),
1351 table = detailPane.append("table"),
1352 tbody = table.append("tbody");
1353
1354 $('<img src="img/' + data.type + '.png">').appendTo(title);
1355 $('<span>').attr('class', 'icon').text(data.id).appendTo(title);
1356
1357 data.propOrder.forEach(function(p) {
1358 if (p === '-') {
1359 addSep(tbody);
1360 } else {
1361 addProp(tbody, p, data.props[p]);
1362 }
1363 });
Simon Hunt61d04042014-11-11 17:27:16 -08001364 }
1365
Paul Greysonfcba0e82014-11-13 10:21:16 -08001366 function zoomPan(scale, translate) {
1367 zoomPanContainer.attr("transform", "translate(" + translate + ")scale(" + scale + ")");
1368 // keep the map lines constant width while zooming
1369 bgImg.style("stroke-width", 2.0 / scale + "px");
1370 }
1371
1372 function resetZoomPan() {
1373 zoomPan(1, [0,0]);
1374 zoom.scale(1).translate([0,0]);
1375 }
1376
1377 function setupZoomPan() {
1378 function zoomed() {
1379 if (!d3.event.sourceEvent.metaKey) {
1380 zoomPan(d3.event.scale, d3.event.translate);
1381 }
1382 }
1383
1384 zoom = d3.behavior.zoom()
1385 .translate([0, 0])
1386 .scale(1)
1387 .scaleExtent([1, 8])
1388 .on("zoom", zoomed);
1389
1390 svg.call(zoom);
1391 }
1392
Simon Hunt61d04042014-11-11 17:27:16 -08001393 // ==============================
1394 // Test harness code
Simon Hunt56d51852014-11-09 13:03:35 -08001395
1396 function prepareScenario(view, ctx, dbg) {
1397 var sc = scenario,
1398 urlSc = sc.evDir + ctx + sc.evScenario;
1399
1400 if (!ctx) {
1401 view.alert("No scenario specified (null ctx)");
1402 return;
1403 }
1404
1405 sc.view = view;
1406 sc.ctx = ctx;
1407 sc.debug = dbg;
1408 sc.evNumber = 0;
1409
1410 d3.json(urlSc, function(err, data) {
Simon Huntbb282f52014-11-10 11:08:19 -08001411 var p = data && data.params || {},
1412 desc = data && data.description || null,
Simon Huntfc274c92014-11-11 11:05:46 -08001413 intro = data && data.title;
Simon Huntbb282f52014-11-10 11:08:19 -08001414
Simon Hunt56d51852014-11-09 13:03:35 -08001415 if (err) {
1416 view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err);
1417 } else {
1418 sc.params = p;
Simon Huntbb282f52014-11-10 11:08:19 -08001419 if (desc) {
1420 intro += '\n\n ' + desc.join('\n ');
1421 }
1422 view.alert(intro);
Simon Hunt56d51852014-11-09 13:03:35 -08001423 }
1424 });
1425
1426 }
1427
Simon Hunt0c6d4192014-11-12 12:07:10 -08001428 function para(sel, text) {
1429 sel.append('p').text(text);
1430 }
1431
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001432 // TODO: toggle button (and other widgets in the masthead) should be provided
1433 // by the framework; not generated by the view.
1434
1435 var showTrafficOnHover;
1436
1437 function addButtonBar(view) {
1438 var bb = d3.select('#mast')
1439 .append('span').classed('right', true).attr('id', 'bb');
1440
1441 showTrafficOnHover = bb.append('div')
1442 .classed('btn', true)
1443 .text('Show traffic on hover')
1444 .on('click', toggleShowTraffic);
1445 }
1446
1447 function toggleShowTraffic() {
1448 showTrafficOnHover.classed('active', !trafficHover());
1449 }
1450
1451 function trafficHover() {
1452 return showTrafficOnHover.classed('active');
1453 }
1454
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001455 // ==============================
Simon Hunt142d0032014-11-04 20:13:09 -08001456 // View life-cycle callbacks
Simon Hunt195cb382014-11-03 17:50:51 -08001457
Simon Huntf67722a2014-11-10 09:32:06 -08001458 function preload(view, ctx, flags) {
Simon Hunt142d0032014-11-04 20:13:09 -08001459 var w = view.width(),
1460 h = view.height(),
Simon Huntc7ee0662014-11-05 16:44:37 -08001461 fcfg = config.force,
1462 fpad = fcfg.pad,
1463 forceDim = [w - 2*fpad, h - 2*fpad];
Simon Hunt195cb382014-11-03 17:50:51 -08001464
Simon Huntbb282f52014-11-10 11:08:19 -08001465 // TODO: set trace api
1466 //trace = onos.exported.webSockTrace;
1467
Simon Hunt142d0032014-11-04 20:13:09 -08001468 // NOTE: view.$div is a D3 selection of the view's div
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001469 var viewBox = '0 0 ' + config.logicalSize + ' ' + config.logicalSize;
1470 svg = view.$div.append('svg').attr('viewBox', viewBox);
Simon Hunt934c3ce2014-11-05 11:45:07 -08001471 setSize(svg, view);
1472
Paul Greysonfcba0e82014-11-13 10:21:16 -08001473 zoomPanContainer = svg.append('g').attr('id', 'zoomPanContainer');
1474
1475 setupZoomPan();
1476
Simon Hunt1a9eff92014-11-07 11:06:34 -08001477 // add blue glow filter to svg layer
Paul Greysonfcba0e82014-11-13 10:21:16 -08001478 d3u.appendGlow(zoomPanContainer);
Simon Hunt1a9eff92014-11-07 11:06:34 -08001479
Simon Huntc7ee0662014-11-05 16:44:37 -08001480 // group for the topology
Paul Greysonfcba0e82014-11-13 10:21:16 -08001481 topoG = zoomPanContainer.append('g')
Simon Huntd3b7d512014-11-12 15:48:41 -08001482 .attr('id', 'topo-G')
Simon Huntc7ee0662014-11-05 16:44:37 -08001483 .attr('transform', fcfg.translate());
1484
1485 // subgroups for links and nodes
1486 linkG = topoG.append('g').attr('id', 'links');
1487 nodeG = topoG.append('g').attr('id', 'nodes');
1488
1489 // selection of nodes and links
1490 link = linkG.selectAll('.link');
1491 node = nodeG.selectAll('.node');
1492
Simon Hunt7cd48f32014-11-09 23:42:50 -08001493 function chrg(d) {
1494 return fcfg.charge[d.class] || -12000;
1495 }
Simon Hunt99c13842014-11-06 18:23:12 -08001496 function ldist(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08001497 return fcfg.linkDistance[d.type] || 50;
Simon Hunt99c13842014-11-06 18:23:12 -08001498 }
1499 function lstrg(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08001500 // 0.0 - 1.0
1501 return fcfg.linkStrength[d.type] || 1.0;
Simon Hunt99c13842014-11-06 18:23:12 -08001502 }
1503
Simon Hunt1a9eff92014-11-07 11:06:34 -08001504 function selectCb(d, self) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001505 selectObject(d, self);
Simon Hunt1a9eff92014-11-07 11:06:34 -08001506 }
1507
1508 function atDragEnd(d, self) {
Simon Hunt56d51852014-11-09 13:03:35 -08001509 // once we've finished moving, pin the node in position
1510 d.fixed = true;
1511 d3.select(self).classed('fixed', true);
1512 if (config.useLiveData) {
Simon Hunt902c9922014-11-11 11:59:31 -08001513 sendUpdateMeta(d);
Simon Hunt1a9eff92014-11-07 11:06:34 -08001514 }
1515 }
1516
Simon Hunt902c9922014-11-11 11:59:31 -08001517 function sendUpdateMeta(d) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001518 sendMessage('updateMeta', {
1519 id: d.id,
1520 'class': d.class,
Simon Hunt902c9922014-11-11 11:59:31 -08001521 'memento': {
1522 x: Math.floor(d.x),
1523 y: Math.floor(d.y)
1524 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08001525 });
1526 }
1527
Simon Huntc7ee0662014-11-05 16:44:37 -08001528 // set up the force layout
1529 network.force = d3.layout.force()
1530 .size(forceDim)
1531 .nodes(network.nodes)
1532 .links(network.links)
Simon Hunt7cd48f32014-11-09 23:42:50 -08001533 .gravity(0.4)
1534 .friction(0.7)
1535 .charge(chrg)
Simon Hunt99c13842014-11-06 18:23:12 -08001536 .linkDistance(ldist)
1537 .linkStrength(lstrg)
Simon Huntc7ee0662014-11-05 16:44:37 -08001538 .on('tick', tick);
Simon Hunt195cb382014-11-03 17:50:51 -08001539
Paul Greysonfcba0e82014-11-13 10:21:16 -08001540 network.drag = d3u.createDragBehavior(network.force, selectCb, atDragEnd, true); // true=require meta
Simon Hunt0c6d4192014-11-12 12:07:10 -08001541
1542 // create mask layer for when we lose connection to server.
1543 mask = view.$div.append('div').attr('id','topo-mask');
1544 para(mask, 'Oops!');
1545 para(mask, 'Web-socket connection to server closed...');
1546 para(mask, 'Try refreshing the page.');
Simon Hunt1a9eff92014-11-07 11:06:34 -08001547 }
Simon Hunt195cb382014-11-03 17:50:51 -08001548
Simon Hunt56d51852014-11-09 13:03:35 -08001549 function load(view, ctx, flags) {
Simon Huntf67722a2014-11-10 09:32:06 -08001550 // resize, in case the window was resized while we were not loaded
1551 resize(view, ctx, flags);
1552
Simon Hunt99c13842014-11-06 18:23:12 -08001553 // cache the view token, so network topo functions can access it
1554 network.view = view;
Simon Hunt56d51852014-11-09 13:03:35 -08001555 config.useLiveData = !flags.local;
1556
1557 if (!config.useLiveData) {
1558 prepareScenario(view, ctx, flags.debug);
1559 }
Simon Hunt99c13842014-11-06 18:23:12 -08001560
1561 // set our radio buttons and key bindings
Simon Hunt934c3ce2014-11-05 11:45:07 -08001562 view.setRadio(btnSet);
1563 view.setKeys(keyDispatch);
Simon Hunt195cb382014-11-03 17:50:51 -08001564
Simon Huntf8e5b4e02014-11-13 11:17:57 -08001565 // patch in our "button bar" for now
1566 // TODO: implement a more official frameworky way of doing this..
1567 addButtonBar(view);
1568
Simon Huntd3b7d512014-11-12 15:48:41 -08001569 // Load map data asynchronously; complete startup after that..
1570 loadGeoJsonData();
1571 }
1572
1573 // TODO: move these to config/state portion of script
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001574 var geoJsonUrl = 'json/map/continental_us.json', // TODO: Paul
Simon Huntd3b7d512014-11-12 15:48:41 -08001575 geoJson;
1576
1577 function loadGeoJsonData() {
1578 d3.json(geoJsonUrl, function (err, data) {
1579 if (err) {
1580 // fall back to USA map background
1581 loadStaticMap();
1582 } else {
1583 geoJson = data;
1584 loadGeoMap();
1585 }
1586
1587 // finally, connect to the server...
1588 if (config.useLiveData) {
1589 webSock.connect();
1590 }
1591 });
1592 }
1593
1594 function showBg() {
1595 return config.options.showBackground ? 'visible' : 'hidden';
1596 }
1597
1598 function loadStaticMap() {
1599 fnTrace('loadStaticMap', config.backgroundUrl);
1600 var w = network.view.width(),
1601 h = network.view.height();
1602
1603 // load the background image
1604 bgImg = svg.insert('svg:image', '#topo-G')
1605 .attr({
1606 id: 'topo-bg',
1607 width: w,
1608 height: h,
1609 'xlink:href': config.backgroundUrl
1610 })
1611 .style({
1612 visibility: showBg()
1613 });
1614 }
1615
1616 function loadGeoMap() {
1617 fnTrace('loadGeoMap', geoJsonUrl);
Simon Huntd3b7d512014-11-12 15:48:41 -08001618
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001619 // extracts the topojson data into geocoordinate-based geometry
1620 var topoData = topojson.feature(geoJson, geoJson.objects.states);
Simon Huntd3b7d512014-11-12 15:48:41 -08001621
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001622 // see: http://bl.ocks.org/mbostock/4707858
1623 geoMapProjection = d3.geo.mercator();
1624 var path = d3.geo.path().projection(geoMapProjection);
Simon Huntd3b7d512014-11-12 15:48:41 -08001625
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001626 geoMapProjection
1627 .scale(1)
1628 .translate([0, 0]);
Simon Huntd3b7d512014-11-12 15:48:41 -08001629
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001630 // [[x1,y1],[x2,y2]]
1631 var b = path.bounds(topoData);
Paul Greysonfcba0e82014-11-13 10:21:16 -08001632 // size map to 95% of minimum dimension to fill space
1633 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 -08001634 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 -08001635
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001636 geoMapProjection
1637 .scale(s)
1638 .translate(t);
1639
Paul Greysonfcba0e82014-11-13 10:21:16 -08001640 bgImg = zoomPanContainer.insert("g", '#topo-G');
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001641 bgImg.attr('id', 'map').selectAll('path')
1642 .data(topoData.features)
1643 .enter()
1644 .append('path')
1645 .attr('d', path);
Simon Hunt195cb382014-11-03 17:50:51 -08001646 }
1647
Simon Huntf67722a2014-11-10 09:32:06 -08001648 function resize(view, ctx, flags) {
Simon Hunt934c3ce2014-11-05 11:45:07 -08001649 setSize(svg, view);
Simon Hunt99c13842014-11-06 18:23:12 -08001650
1651 // TODO: hook to recompute layout, perhaps? work with zoom/pan code
1652 // adjust force layout size
Simon Hunt142d0032014-11-04 20:13:09 -08001653 }
1654
1655
1656 // ==============================
1657 // View registration
Simon Hunt195cb382014-11-03 17:50:51 -08001658
Simon Hunt25248912014-11-04 11:25:48 -08001659 onos.ui.addView('topo', {
Simon Hunt142d0032014-11-04 20:13:09 -08001660 preload: preload,
1661 load: load,
1662 resize: resize
Simon Hunt195cb382014-11-03 17:50:51 -08001663 });
1664
Simon Hunt61d04042014-11-11 17:27:16 -08001665 detailPane = onos.ui.addFloatingPanel('topo-detail');
1666
Simon Hunt195cb382014-11-03 17:50:51 -08001667}(ONOS));