blob: ce1fceb8803d7eabb638724ce0207e9057d433da [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
Simon Hunta6a9fe72014-11-20 11:17:12 -080021 @author Thomas Vachuska
Simon Hunt195cb382014-11-03 17:50:51 -080022 */
23
24(function (onos) {
25 'use strict';
26
Simon Hunt1a9eff92014-11-07 11:06:34 -080027 // shorter names for library APIs
Simon Huntbb282f52014-11-10 11:08:19 -080028 var d3u = onos.lib.d3util,
Simon Hunta6a9fe72014-11-20 11:17:12 -080029 gly = onos.lib.glyphs;
Simon Hunt1a9eff92014-11-07 11:06:34 -080030
Simon Hunt195cb382014-11-03 17:50:51 -080031 // configuration data
32 var config = {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -080033 useLiveData: true,
Simon Huntfc274c92014-11-11 11:05:46 -080034 fnTrace: true,
Simon Hunt195cb382014-11-03 17:50:51 -080035 debugOn: false,
36 debug: {
Simon Hunt99c13842014-11-06 18:23:12 -080037 showNodeXY: true,
38 showKeyHandler: false
Simon Hunt195cb382014-11-03 17:50:51 -080039 },
Simon Hunt12ce12e2014-11-15 21:13:19 -080040 birdDim: 400,
Simon Hunt195cb382014-11-03 17:50:51 -080041 options: {
42 layering: true,
43 collisionPrevention: true,
Simon Hunt142d0032014-11-04 20:13:09 -080044 showBackground: true
Simon Hunt195cb382014-11-03 17:50:51 -080045 },
46 backgroundUrl: 'img/us-map.png',
Thomas Vachuska7d638d32014-11-07 10:24:43 -080047 webSockUrl: 'ws/topology',
Simon Hunt195cb382014-11-03 17:50:51 -080048 data: {
49 live: {
50 jsonUrl: 'rs/topology/graph',
51 detailPrefix: 'rs/topology/graph/',
52 detailSuffix: ''
53 },
54 fake: {
55 jsonUrl: 'json/network2.json',
56 detailPrefix: 'json/',
57 detailSuffix: '.json'
58 }
59 },
Simon Hunt99c13842014-11-06 18:23:12 -080060 labels: {
61 imgPad: 16,
62 padLR: 4,
63 padTB: 3,
64 marginLR: 3,
65 marginTB: 2,
66 port: {
67 gap: 3,
68 width: 18,
69 height: 14
70 }
71 },
Simon Hunt1a9eff92014-11-07 11:06:34 -080072 topo: {
Thomas Vachuska89543292014-11-19 11:28:33 -080073 linkBaseColor: '#666',
Simon Hunt1a9eff92014-11-07 11:06:34 -080074 linkInColor: '#66f',
Thomas Vachuska9edca302014-11-22 17:06:42 -080075 linkInWidth: 12,
Thomas Vachuska89543292014-11-19 11:28:33 -080076 linkOutColor: '#f00',
Thomas Vachuska9edca302014-11-22 17:06:42 -080077 linkOutWidth: 10
Simon Hunt1a9eff92014-11-07 11:06:34 -080078 },
Paul Greyson29cd58f2014-11-18 13:14:57 -080079 icons: {
Simon Huntc72967b2014-11-20 09:21:42 -080080 device: {
Simon Hunt395a70c2014-11-22 23:17:40 -080081 dim: 36,
82 rx: 4,
83 xoff: -20,
84 yoff: -18
85 },
86 host: {
87 defaultRadius: 9,
88 radius: {
89 endstation: 14,
90 bgpSpeaker: 14,
91 router: 14
92 }
Simon Huntc72967b2014-11-20 09:21:42 -080093 }
Thomas Vachuska89543292014-11-19 11:28:33 -080094 },
Simon Hunt195cb382014-11-03 17:50:51 -080095 force: {
Simon Hunt7cd48f32014-11-09 23:42:50 -080096 note_for_links: 'link.type is used to differentiate',
Simon Huntc7ee0662014-11-05 16:44:37 -080097 linkDistance: {
Simon Hunt7cd48f32014-11-09 23:42:50 -080098 direct: 100,
99 optical: 120,
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800100 hostLink: 3
Simon Huntc7ee0662014-11-05 16:44:37 -0800101 },
102 linkStrength: {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800103 direct: 1.0,
104 optical: 1.0,
105 hostLink: 1.0
Simon Huntc7ee0662014-11-05 16:44:37 -0800106 },
Simon Hunt7cd48f32014-11-09 23:42:50 -0800107 note_for_nodes: 'node.class is used to differentiate',
Simon Huntc7ee0662014-11-05 16:44:37 -0800108 charge: {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800109 device: -8000,
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800110 host: -5000
Simon Hunt195cb382014-11-03 17:50:51 -0800111 }
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800112 },
113 // see below in creation of viewBox on main svg
114 logicalSize: 1000
Simon Hunt195cb382014-11-03 17:50:51 -0800115 };
116
Simon Hunt142d0032014-11-04 20:13:09 -0800117 // radio buttons
Simon Hunt9462e8c2014-11-14 17:28:09 -0800118 var layerButtons = [
119 { text: 'All Layers', id: 'all', cb: showAllLayers },
120 { text: 'Packet Only', id: 'pkt', cb: showPacketLayer },
121 { text: 'Optical Only', id: 'opt', cb: showOpticalLayer }
122 ],
123 layerBtnSet,
124 layerBtnDispatch = {
125 all: showAllLayers,
126 pkt: showPacketLayer,
127 opt: showOpticalLayer
128 };
Simon Hunt934c3ce2014-11-05 11:45:07 -0800129
130 // key bindings
131 var keyDispatch = {
Simon Hunt988c6fc2014-11-20 17:43:03 -0800132 // TODO: remove these "development only" bindings
Simon Hunt8f40cce2014-11-23 15:57:30 -0800133 0: testMe,
134 equals: injectStartupEvents,
135 dash: injectTestEvent,
Simon Hunt99c13842014-11-06 18:23:12 -0800136
Thomas Vachuska1e68bdd2014-11-29 13:53:10 -0800137 E: [equalizeMasters, 'Equalize mastership roles'],
Thomas Vachuska47635c62014-11-22 01:21:36 -0800138 O: [toggleSummary, 'Toggle ONOS summary pane'],
139 I: [toggleInstances, 'Toggle ONOS instances pane'],
Simon Hunt27d322d2014-11-28 10:45:43 -0800140 D: [toggleDetails, 'Disable / enable details pane'],
Simon Hunt988c6fc2014-11-20 17:43:03 -0800141 B: [toggleBg, 'Toggle background image'],
Simon Hunt434cf142014-11-24 11:10:28 -0800142 H: [toggleHosts, 'Toggle host visibility'],
Simon Hunt6d9bd032014-11-28 22:16:40 -0800143 M: [toggleOffline, 'Toggle offline visibility'],
Simon Hunt27d322d2014-11-28 10:45:43 -0800144 L: [cycleLabels, 'Cycle device labels'],
Simon Hunt934c3ce2014-11-05 11:45:07 -0800145 P: togglePorts,
Simon Hunt87514342014-11-24 16:41:27 -0800146 U: [unpin, 'Unpin node (hover mouse over)'],
Simon Hunt3c5ca542014-11-29 14:11:43 -0800147 R: [resetPanZoom, 'Reset pan / zoom'],
Thomas Vachuska9edca302014-11-22 17:06:42 -0800148 V: [showTrafficAction, 'Show related traffic'],
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800149 A: [showAllTrafficAction, 'Show all traffic'],
150 F: [showDeviceLinkFlowsAction, 'Show device link flows'],
Simon Huntc2367d52014-11-29 19:30:23 -0800151 X: [toggleNodeLock, 'Lock / unlock node positions'],
Simon Huntd6f5a272014-11-29 23:45:50 -0800152 Z: [toggleOblique, 'Toggle oblique view (Experimental)'],
Simon Hunt9462e8c2014-11-14 17:28:09 -0800153 esc: handleEscape
Simon Hunt934c3ce2014-11-05 11:45:07 -0800154 };
Simon Hunt142d0032014-11-04 20:13:09 -0800155
Simon Hunt87514342014-11-24 16:41:27 -0800156 // mouse gestures
157 var gestures = [
158 ['click', 'Select the item and show details'],
159 ['shift-click', 'Toggle selection state'],
160 ['drag', 'Reposition (and pin) device / host'],
161 ['cmd-scroll', 'Zoom in / out'],
162 ['cmd-drag', 'Pan']
163 ];
164
Simon Hunt195cb382014-11-03 17:50:51 -0800165 // state variables
Simon Hunt99c13842014-11-06 18:23:12 -0800166 var network = {
Simon Hunt50128c02014-11-08 13:36:15 -0800167 view: null, // view token reference
Simon Hunt99c13842014-11-06 18:23:12 -0800168 nodes: [],
169 links: [],
Simon Hunt269670f2014-11-17 16:17:43 -0800170 lookup: {},
171 revLinkToKey: {}
Simon Hunt99c13842014-11-06 18:23:12 -0800172 },
Simon Hunt56d51852014-11-09 13:03:35 -0800173 scenario = {
174 evDir: 'json/ev/',
175 evScenario: '/scenario.json',
176 evPrefix: '/ev_',
177 evOnos: '_onos.json',
178 evUi: '_ui.json',
179 ctx: null,
180 params: {},
181 evNumber: 0,
Simon Hunt434cf142014-11-24 11:10:28 -0800182 view: null
Simon Hunt56d51852014-11-09 13:03:35 -0800183 },
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800184 webSock,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800185 sid = 0,
Thomas Vachuska60d72bf2014-11-21 13:02:00 -0800186 deviceLabelCount = 3,
Simon Hunt209155e2014-11-21 12:16:09 -0800187 hostLabelCount = 2,
Simon Hunt56d51852014-11-09 13:03:35 -0800188 deviceLabelIndex = 0,
189 hostLabelIndex = 0,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800190 selections = {},
Simon Hunta5e89142014-11-14 07:00:33 -0800191 selectOrder = [],
Simon Hunt6ac93f32014-11-13 12:17:27 -0800192 hovered = null,
Thomas Vachuska47635c62014-11-22 01:21:36 -0800193 summaryPane,
Simon Hunta5e89142014-11-14 07:00:33 -0800194 detailPane,
Simon Hunta255a2c2014-11-13 22:29:35 -0800195 antTimer = null,
Thomas Vachuska12dfdc32014-11-29 16:03:12 -0800196 guiSuccessor = null,
Simon Hunta5e89142014-11-14 07:00:33 -0800197 onosInstances = {},
198 onosOrder = [],
199 oiBox,
Simon Hunt9462e8c2014-11-14 17:28:09 -0800200 oiShowMaster = false,
Simon Hunt8f40cce2014-11-23 15:57:30 -0800201 portLabelsOn = false,
Simon Huntb0ecfa52014-11-23 21:05:12 -0800202 cat7 = d3u.cat7(),
Simon Hunt434cf142014-11-24 11:10:28 -0800203 colorAffinity = false,
Simon Hunt27d322d2014-11-28 10:45:43 -0800204 showHosts = false,
Simon Hunt6d9bd032014-11-28 22:16:40 -0800205 showOffline = true,
Simon Hunt27d322d2014-11-28 10:45:43 -0800206 useDetails = true,
Simon Hunt6e18fe32014-11-29 13:35:41 -0800207 haveDetails = false,
Simon Huntc2367d52014-11-29 19:30:23 -0800208 nodeLock = false,
209 oblique = false;
Simon Hunt195cb382014-11-03 17:50:51 -0800210
Simon Hunt434cf142014-11-24 11:10:28 -0800211 // constants
Thomas Vachuska9edca302014-11-22 17:06:42 -0800212 var hoverModeAll = 1,
213 hoverModeFlows = 2,
214 hoverModeIntents = 3,
215 hoverMode = hoverModeFlows;
216
Simon Hunt934c3ce2014-11-05 11:45:07 -0800217 // D3 selections
218 var svg,
Simon Hunt3c5ca542014-11-29 14:11:43 -0800219 panZoomContainer,
Simon Hunt934c3ce2014-11-05 11:45:07 -0800220 bgImg,
Simon Huntc7ee0662014-11-05 16:44:37 -0800221 topoG,
222 nodeG,
223 linkG,
Simon Hunte2575b62014-11-18 15:25:53 -0800224 linkLabelG,
Simon Huntc7ee0662014-11-05 16:44:37 -0800225 node,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800226 link,
Simon Hunte2575b62014-11-18 15:25:53 -0800227 linkLabel,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800228 mask;
Simon Hunt195cb382014-11-03 17:50:51 -0800229
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800230 // the projection for the map background
Simon Hunt1b18aa52014-11-29 17:57:55 -0800231 var geoMapProj;
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800232
Paul Greysonfcba0e82014-11-13 10:21:16 -0800233 // the zoom function
234 var zoom;
235
Simon Hunt142d0032014-11-04 20:13:09 -0800236 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800237 // For Debugging / Development
Simon Hunt195cb382014-11-03 17:50:51 -0800238
Simon Hunt99c13842014-11-06 18:23:12 -0800239 function note(label, msg) {
240 console.log('NOTE: ' + label + ': ' + msg);
Simon Hunt195cb382014-11-03 17:50:51 -0800241 }
242
Simon Hunt99c13842014-11-06 18:23:12 -0800243 function debug(what) {
244 return config.debugOn && config.debug[what];
Simon Hunt934c3ce2014-11-05 11:45:07 -0800245 }
246
Simon Huntfc274c92014-11-11 11:05:46 -0800247 function fnTrace(msg, id) {
248 if (config.fnTrace) {
249 console.log('FN: ' + msg + ' [' + id + ']');
250 }
251 }
Simon Hunt99c13842014-11-06 18:23:12 -0800252
Simon Hunta5e89142014-11-14 07:00:33 -0800253 function evTrace(data) {
254 fnTrace(data.event, data.payload.id);
255 }
256
Simon Hunt934c3ce2014-11-05 11:45:07 -0800257 // ==============================
258 // Key Callbacks
259
Simon Hunt27d322d2014-11-28 10:45:43 -0800260 function flash(txt) {
261 network.view.flash(txt);
262 }
263
Simon Hunt99c13842014-11-06 18:23:12 -0800264 function testMe(view) {
Simon Hunt8f40cce2014-11-23 15:57:30 -0800265 //view.alert('Theme is ' + view.getTheme());
Simon Hunta3dd9572014-11-20 15:22:41 -0800266 //view.flash('This is some text');
Simon Hunt8f40cce2014-11-23 15:57:30 -0800267 cat7.testCard(svg);
Simon Hunt99c13842014-11-06 18:23:12 -0800268 }
269
Simon Hunt56d51852014-11-09 13:03:35 -0800270 function injectTestEvent(view) {
Simon Hunt434cf142014-11-24 11:10:28 -0800271 if (config.useLiveData) { return; }
272
Simon Hunt56d51852014-11-09 13:03:35 -0800273 var sc = scenario,
274 evn = ++sc.evNumber,
275 pfx = sc.evDir + sc.ctx + sc.evPrefix + evn,
276 onosUrl = pfx + sc.evOnos,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800277 uiUrl = pfx + sc.evUi,
278 stack = [
279 { url: onosUrl, cb: handleServerEvent },
280 { url: uiUrl, cb: handleUiEvent }
281 ];
282 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800283 }
284
Simon Hunt7cd48f32014-11-09 23:42:50 -0800285 function recurseFetchEvent(stack, evn) {
286 var v = scenario.view,
287 frame;
288 if (stack.length === 0) {
Simon Huntfc274c92014-11-11 11:05:46 -0800289 v.alert('Oops!\n\nNo event #' + evn + ' found.');
Simon Hunt7cd48f32014-11-09 23:42:50 -0800290 return;
291 }
292 frame = stack.shift();
293
294 d3.json(frame.url, function (err, data) {
Simon Hunt99c13842014-11-06 18:23:12 -0800295 if (err) {
Simon Hunt56d51852014-11-09 13:03:35 -0800296 if (err.status === 404) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800297 // if we didn't find the data, try the next stack frame
298 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800299 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800300 v.alert('non-404 error:\n\n' + frame.url + '\n\n' + err);
Simon Hunt56d51852014-11-09 13:03:35 -0800301 }
Simon Hunt99c13842014-11-06 18:23:12 -0800302 } else {
Simon Hunt1712ed82014-11-17 12:56:00 -0800303 wsTrace('test', JSON.stringify(data));
Simon Hunt7cd48f32014-11-09 23:42:50 -0800304 frame.cb(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800305 }
306 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800307
Simon Hunt56d51852014-11-09 13:03:35 -0800308 }
Simon Hunt50128c02014-11-08 13:36:15 -0800309
Simon Hunt56d51852014-11-09 13:03:35 -0800310 function handleUiEvent(data) {
Simon Huntbb282f52014-11-10 11:08:19 -0800311 scenario.view.alert('UI Tx: ' + data.event + '\n\n' +
312 JSON.stringify(data));
Simon Hunt56d51852014-11-09 13:03:35 -0800313 }
314
315 function injectStartupEvents(view) {
316 var last = scenario.params.lastAuto || 0;
Simon Hunt434cf142014-11-24 11:10:28 -0800317 if (config.useLiveData) { return; }
Simon Hunt56d51852014-11-09 13:03:35 -0800318
319 while (scenario.evNumber < last) {
Simon Hunt1a9eff92014-11-07 11:06:34 -0800320 injectTestEvent(view);
321 }
322 }
323
Simon Hunt934c3ce2014-11-05 11:45:07 -0800324 function toggleBg() {
325 var vis = bgImg.style('visibility');
Simon Hunt434cf142014-11-24 11:10:28 -0800326 bgImg.style('visibility', visVal(vis === 'hidden'));
327 }
328
Simon Huntd6f5a272014-11-29 23:45:50 -0800329 function opacifyBg(b) {
330 bgImg.transition()
331 .duration(1000)
332 .attr('opacity', b ? 1 : 0);
333 }
334
Simon Huntc2367d52014-11-29 19:30:23 -0800335 function toggleNodeLock() {
336 nodeLock = !nodeLock;
337 flash('Node positions ' + (nodeLock ? 'locked' : 'unlocked'))
338 }
339
340 function toggleOblique() {
341 oblique = !oblique;
Simon Huntd6f5a272014-11-29 23:45:50 -0800342 if (oblique) {
343 network.force.stop();
344 toObliqueView();
345 } else {
346 toNormalView();
347 }
Simon Huntc2367d52014-11-29 19:30:23 -0800348 }
349
Simon Hunt434cf142014-11-24 11:10:28 -0800350 function toggleHosts() {
351 showHosts = !showHosts;
352 updateHostVisibility();
Simon Hunt27d322d2014-11-28 10:45:43 -0800353 flash('Hosts ' + visVal(showHosts));
Simon Hunt934c3ce2014-11-05 11:45:07 -0800354 }
355
Simon Hunt6d9bd032014-11-28 22:16:40 -0800356 function toggleOffline() {
357 showOffline = !showOffline;
358 updateOfflineVisibility();
359 flash('Offline devices ' + visVal(showOffline));
360 }
361
Simon Hunt99c13842014-11-06 18:23:12 -0800362 function cycleLabels() {
Thomas Vachuska60d72bf2014-11-21 13:02:00 -0800363 deviceLabelIndex = (deviceLabelIndex === 2)
Simon Huntbb282f52014-11-10 11:08:19 -0800364 ? 0 : deviceLabelIndex + 1;
Simon Hunt5f36d342014-11-08 21:33:14 -0800365
Simon Hunt99c13842014-11-06 18:23:12 -0800366 network.nodes.forEach(function (d) {
Simon Huntbb282f52014-11-10 11:08:19 -0800367 if (d.class === 'device') {
368 updateDeviceLabel(d);
369 }
Simon Hunt99c13842014-11-06 18:23:12 -0800370 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800371 }
372
373 function togglePorts(view) {
Simon Hunt434cf142014-11-24 11:10:28 -0800374 //view.alert('togglePorts() callback')
Simon Hunt934c3ce2014-11-05 11:45:07 -0800375 }
376
Simon Hunt6ac93f32014-11-13 12:17:27 -0800377 function unpin() {
378 if (hovered) {
Simon Hunt395a70c2014-11-22 23:17:40 -0800379 sendUpdateMeta(hovered);
Simon Hunt6ac93f32014-11-13 12:17:27 -0800380 hovered.fixed = false;
381 hovered.el.classed('fixed', false);
Simon Huntd6f5a272014-11-29 23:45:50 -0800382 fResume();
Simon Hunt6ac93f32014-11-13 12:17:27 -0800383 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800384 }
385
Simon Hunt9462e8c2014-11-14 17:28:09 -0800386 function handleEscape(view) {
387 if (oiShowMaster) {
388 cancelAffinity();
Simon Hunt27d322d2014-11-28 10:45:43 -0800389 } else if (haveDetails) {
Simon Hunt9462e8c2014-11-14 17:28:09 -0800390 deselectAll();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800391 } else if (oiBox.isVisible()) {
Simon Huntb0ecfa52014-11-23 21:05:12 -0800392 hideInstances();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800393 } else if (summaryPane.isVisible()) {
394 cancelSummary();
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800395 stopAntTimer();
396 } else {
397 hoverMode = hoverModeFlows;
Simon Hunt9462e8c2014-11-14 17:28:09 -0800398 }
399 }
400
Simon Hunt934c3ce2014-11-05 11:45:07 -0800401 // ==============================
Simon Huntd6f5a272014-11-29 23:45:50 -0800402 // Oblique view ...
403
404 var obview = {
405 tt: -.7, // x skew y factor
406 xsk: -35, // x skew angle
407 ysc: 0.5, // y scale
408 pad: 50,
409 time: 1500,
410 fill: {
411 pkt: 'rgba(130,130,170,0.3)',
412 opt: 'rgba(170,130,170,0.3)'
413 },
414 id: function (tag) {
415 return 'obview-' + tag + 'Plane';
416 },
417 yt: function (h, dir) {
418 return h * obview.ysc * dir * 1.1;
419 },
420 obXform: function (h, dir) {
421 var yt = obview.yt(h, dir);
422 return scale(1, obview.ysc) + translate(0, yt) + skewX(obview.xsk);
423 },
424 noXform: function () {
425 return skewX(0) + translate(0,0) + scale(1,1);
426 },
427 xffn: null,
428 plane: {}
429 };
430
431
432 function toObliqueView() {
433 var box = nodeG.node().getBBox(),
434 ox, oy;
435
436 padBox(box, obview.pad);
437
438 ox = box.x + box.width / 2;
439 oy = box.y + box.height / 2;
440
441 // remember node lock state, then lock the nodes down
442 obview.nodeLock = nodeLock;
443 nodeLock = true;
444 opacifyBg(false);
445
446 insertPlanes(ox, oy);
447
448 obview.xffn = function (xy, dir) {
449 var yt = obview.yt(box.height, dir),
450 ax = xy.x - ox,
451 ay = xy.y - oy,
452 x = ax + ay * obview.tt,
453 y = ay * obview.ysc + obview.ysc * yt;
454 return {x: ox + x, y: oy + y};
455 };
456
457 showPlane('pkt', box, -1);
458 showPlane('opt', box, 1);
459 obTransitionNodes();
460 }
461
462 function toNormalView() {
463 obview.xffn = null;
464
465 hidePlane('pkt');
466 hidePlane('opt');
467 obTransitionNodes();
468
469 removePlanes();
470
471 // restore node lock state
472 nodeLock = obview.nodeLock;
473 opacifyBg(true);
474 }
475
476 function obTransitionNodes() {
477 var xffn = obview.xffn;
478
479 // return the direction for the node
480 // -1 for pkt layer, 1 for optical layer
481 function dir(d) {
482 return inLayer(d, 'pkt') ? -1 : 1;
483 }
484
485 if (xffn) {
486 network.nodes.forEach(function (d) {
487 var oldxy = {x: d.x, y: d.y},
488 coords = xffn(oldxy, dir(d));
489 d.oldxy = oldxy;
490 d.px = d.x = coords.x;
491 d.py = d.y = coords.y;
492 });
493 } else {
494 network.nodes.forEach(function (d) {
495 var old = d.oldxy || {x: d.x, y: d.y};
496 d.px = d.x = old.x;
497 d.py = d.y = old.y;
498 delete d.oldxy;
499 });
500 }
501
502 node.transition()
503 .duration(obview.time)
504 .attr(tickStuff.nodeAttr);
505 link.transition()
506 .duration(obview.time)
507 .attr(tickStuff.linkAttr);
508 linkLabel.transition()
509 .duration(obview.time)
510 .attr(tickStuff.linkLabelAttr);
511 }
512
513 function showPlane(tag, box, dir) {
514 var g = obview.plane[tag];
515
516 // set box origin at center..
517 box.x = -box.width/2;
518 box.y = -box.height/2;
519
520 g.select('rect')
521 .attr(box)
522 .attr('opacity', 0)
523 .transition()
524 .duration(obview.time)
525 .attr('opacity', 1)
526 .attr('transform', obview.obXform(box.height, dir));
527 }
528
529 function hidePlane(tag) {
530 var g = obview.plane[tag];
531
532 g.select('rect')
533 .transition()
534 .duration(obview.time)
535 .attr('opacity', 0)
536 .attr('transform', obview.noXform());
537 }
538
539 function insertPlanes(ox, oy) {
540 function ins(tag) {
541 var id = obview.id(tag),
542 g = panZoomContainer.insert('g', '#topo-G')
543 .attr('id', id)
544 .attr('transform', translate(ox,oy));
545 g.append('rect')
546 .attr('fill', obview.fill[tag])
547 .attr('opacity', 0);
548 obview.plane[tag] = g;
549 }
550 ins('opt');
551 ins('pkt');
552 }
553
554 function removePlanes() {
555 function rem(tag) {
556 var id = obview.id(tag);
557 panZoomContainer.select('#'+id)
558 .transition()
559 .duration(obview.time + 50)
560 .remove();
561 delete obview.plane[tag];
562 }
563 rem('opt');
564 rem('pkt');
565 }
566
567 function padBox(box, p) {
568 box.x -= p;
569 box.y -= p;
570 box.width += p*2;
571 box.height += p*2;
572 }
573
574 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800575 // Radio Button Callbacks
576
Simon Hunta5e89142014-11-14 07:00:33 -0800577 var layerLookup = {
578 host: {
Simon Hunt209155e2014-11-21 12:16:09 -0800579 endstation: 'pkt', // default, if host event does not define type
Thomas Vachuska89543292014-11-19 11:28:33 -0800580 router: 'pkt',
Simon Hunta5e89142014-11-14 07:00:33 -0800581 bgpSpeaker: 'pkt'
582 },
583 device: {
584 switch: 'pkt',
585 roadm: 'opt'
586 },
587 link: {
588 hostLink: 'pkt',
589 direct: 'pkt',
Simon Hunt8257f4c2014-11-16 19:34:54 -0800590 indirect: '',
591 tunnel: '',
Simon Hunta5e89142014-11-14 07:00:33 -0800592 optical: 'opt'
593 }
594 };
595
596 function inLayer(d, layer) {
Simon Hunt8257f4c2014-11-16 19:34:54 -0800597 var type = d.class === 'link' ? d.type() : d.type,
598 look = layerLookup[d.class],
599 lyr = look && look[type];
Simon Hunta5e89142014-11-14 07:00:33 -0800600 return lyr === layer;
601 }
602
603 function unsuppressLayer(which) {
604 node.each(function (d) {
605 var node = d.el;
606 if (inLayer(d, which)) {
607 node.classed('suppressed', false);
608 }
609 });
610
611 link.each(function (d) {
612 var link = d.el;
613 if (inLayer(d, which)) {
614 link.classed('suppressed', false);
615 }
616 });
617 }
618
Simon Hunt9462e8c2014-11-14 17:28:09 -0800619 function suppressLayers(b) {
620 node.classed('suppressed', b);
621 link.classed('suppressed', b);
Simon Hunt142d0032014-11-04 20:13:09 -0800622// d3.selectAll('svg .port').classed('inactive', false);
623// d3.selectAll('svg .portText').classed('inactive', false);
Simon Hunt195cb382014-11-03 17:50:51 -0800624 }
625
Simon Hunt9462e8c2014-11-14 17:28:09 -0800626 function showAllLayers() {
627 suppressLayers(false);
628 }
629
Simon Hunt195cb382014-11-03 17:50:51 -0800630 function showPacketLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800631 node.classed('suppressed', true);
632 link.classed('suppressed', true);
633 unsuppressLayer('pkt');
Simon Hunt195cb382014-11-03 17:50:51 -0800634 }
635
636 function showOpticalLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800637 node.classed('suppressed', true);
638 link.classed('suppressed', true);
639 unsuppressLayer('opt');
Simon Hunt195cb382014-11-03 17:50:51 -0800640 }
641
Simon Hunt9462e8c2014-11-14 17:28:09 -0800642 function restoreLayerState() {
643 layerBtnDispatch[layerBtnSet.selected()]();
644 }
645
Simon Hunt142d0032014-11-04 20:13:09 -0800646 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800647 // Private functions
648
Simon Hunt99c13842014-11-06 18:23:12 -0800649 function safeId(s) {
650 return s.replace(/[^a-z0-9]/gi, '-');
651 }
652
Simon Huntc7ee0662014-11-05 16:44:37 -0800653 // set the size of the given element to that of the view (reduced if padded)
654 function setSize(el, view, pad) {
655 var padding = pad ? pad * 2 : 0;
Simon Hunt934c3ce2014-11-05 11:45:07 -0800656 el.attr({
Simon Huntc7ee0662014-11-05 16:44:37 -0800657 width: view.width() - padding,
658 height: view.height() - padding
Simon Hunt934c3ce2014-11-05 11:45:07 -0800659 });
660 }
661
Simon Hunt8257f4c2014-11-16 19:34:54 -0800662 function makeNodeKey(d, what) {
663 var port = what + 'Port';
664 return d[what] + '/' + d[port];
665 }
666
667 function makeLinkKey(d, flipped) {
668 var one = flipped ? makeNodeKey(d, 'dst') : makeNodeKey(d, 'src'),
669 two = flipped ? makeNodeKey(d, 'src') : makeNodeKey(d, 'dst');
670 return one + '-' + two;
671 }
672
Simon Hunt269670f2014-11-17 16:17:43 -0800673 function findLinkById(id) {
674 // check to see if this is a reverse lookup, else default to given id
675 var key = network.revLinkToKey[id] || id;
676 return key && network.lookup[key];
677 }
678
Simon Hunt8257f4c2014-11-16 19:34:54 -0800679 function findLink(linkData, op) {
680 var key = makeLinkKey(linkData),
681 keyrev = makeLinkKey(linkData, 1),
682 link = network.lookup[key],
683 linkRev = network.lookup[keyrev],
684 result = {},
685 ldata = link || linkRev,
686 rawLink;
687
688 if (op === 'add') {
689 if (link) {
690 // trying to add a link that we already know about
691 result.ldata = link;
692 result.badLogic = 'addLink: link already added';
693
694 } else if (linkRev) {
695 // we found the reverse of the link to be added
696 result.ldata = linkRev;
697 if (linkRev.fromTarget) {
698 result.badLogic = 'addLink: link already added';
699 }
700 }
701 } else if (op === 'update') {
702 if (!ldata) {
703 result.badLogic = 'updateLink: link not found';
704 } else {
705 rawLink = link ? ldata.fromSource : ldata.fromTarget;
706 result.updateWith = function (data) {
707 $.extend(rawLink, data);
708 restyleLinkElement(ldata);
709 }
710 }
711 } else if (op === 'remove') {
712 if (!ldata) {
713 result.badLogic = 'removeLink: link not found';
714 } else {
715 rawLink = link ? ldata.fromSource : ldata.fromTarget;
716
717 if (!rawLink) {
718 result.badLogic = 'removeLink: link not found';
719
720 } else {
721 result.removeRawLink = function () {
722 if (link) {
723 // remove fromSource
724 ldata.fromSource = null;
725 if (ldata.fromTarget) {
726 // promote target into source position
727 ldata.fromSource = ldata.fromTarget;
728 ldata.fromTarget = null;
729 ldata.key = keyrev;
730 delete network.lookup[key];
731 network.lookup[keyrev] = ldata;
Simon Hunt269670f2014-11-17 16:17:43 -0800732 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800733 }
734 } else {
735 // remove fromTarget
736 ldata.fromTarget = null;
Simon Hunt269670f2014-11-17 16:17:43 -0800737 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800738 }
739 if (ldata.fromSource) {
740 restyleLinkElement(ldata);
741 } else {
742 removeLinkElement(ldata);
743 }
744 }
745 }
746 }
747 }
748 return result;
749 }
750
751 function addLinkUpdate(ldata, link) {
752 // add link event, but we already have the reverse link installed
753 ldata.fromTarget = link;
Simon Hunt269670f2014-11-17 16:17:43 -0800754 network.revLinkToKey[link.id] = ldata.key;
Simon Hunt8257f4c2014-11-16 19:34:54 -0800755 restyleLinkElement(ldata);
756 }
757
758 var allLinkTypes = 'direct indirect optical tunnel',
759 defaultLinkType = 'direct';
760
761 function restyleLinkElement(ldata) {
762 // this fn's job is to look at raw links and decide what svg classes
763 // need to be applied to the line element in the DOM
764 var el = ldata.el,
765 type = ldata.type(),
766 lw = ldata.linkWidth(),
767 online = ldata.online();
768
769 el.classed('link', true);
770 el.classed('inactive', !online);
771 el.classed(allLinkTypes, false);
772 if (type) {
773 el.classed(type, true);
774 }
775 el.transition()
776 .duration(1000)
Thomas Vachuska89543292014-11-19 11:28:33 -0800777 .attr('stroke-width', linkScale(lw))
778 .attr('stroke', config.topo.linkBaseColor);
Simon Hunt8257f4c2014-11-16 19:34:54 -0800779 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800780
Simon Hunt99c13842014-11-06 18:23:12 -0800781 // ==============================
782 // Event handlers for server-pushed events
783
Simon Huntbb282f52014-11-10 11:08:19 -0800784 function logicError(msg) {
785 // TODO, report logic error to server, via websock, so it can be logged
Simon Huntfc274c92014-11-11 11:05:46 -0800786 console.warn(msg);
Simon Huntbb282f52014-11-10 11:08:19 -0800787 }
788
Simon Hunt99c13842014-11-06 18:23:12 -0800789 var eventDispatch = {
Simon Hunta5e89142014-11-14 07:00:33 -0800790 addInstance: addInstance,
Simon Hunt99c13842014-11-06 18:23:12 -0800791 addDevice: addDevice,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800792 addLink: addLink,
Simon Hunt56d51852014-11-09 13:03:35 -0800793 addHost: addHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800794
Simon Huntfcfb46c2014-11-19 12:53:38 -0800795 updateInstance: updateInstance,
Simon Huntbb282f52014-11-10 11:08:19 -0800796 updateDevice: updateDevice,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800797 updateLink: updateLink,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800798 updateHost: updateHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800799
Simon Hunt7b403bc2014-11-22 19:01:00 -0800800 removeInstance: removeInstance,
Simon Huntca867ac2014-11-28 18:07:35 -0800801 removeDevice: removeDevice,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800802 removeLink: removeLink,
Simon Hunt44031102014-11-11 13:20:36 -0800803 removeHost: removeHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800804
Simon Hunt61d04042014-11-11 17:27:16 -0800805 showDetails: showDetails,
Thomas Vachuska47635c62014-11-22 01:21:36 -0800806 showSummary: showSummary,
Simon Huntb53e0682014-11-12 13:32:01 -0800807 showTraffic: showTraffic
Simon Hunt99c13842014-11-06 18:23:12 -0800808 };
809
Simon Hunta5e89142014-11-14 07:00:33 -0800810 function addInstance(data) {
811 evTrace(data);
812 var inst = data.payload,
813 id = inst.id;
814 if (onosInstances[id]) {
Thomas Vachuska12dfdc32014-11-29 16:03:12 -0800815 updateInstance(data);
Simon Hunta5e89142014-11-14 07:00:33 -0800816 return;
817 }
818 onosInstances[id] = inst;
819 onosOrder.push(inst);
820 updateInstances();
821 }
822
Simon Hunt99c13842014-11-06 18:23:12 -0800823 function addDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800824 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800825 var device = data.payload,
Simon Huntca867ac2014-11-28 18:07:35 -0800826 id = device.id,
827 d;
828
829 if (network.lookup[id]) {
Thomas Vachuska12dfdc32014-11-29 16:03:12 -0800830 updateDevice(data);
Simon Huntca867ac2014-11-28 18:07:35 -0800831 return;
832 }
833
834 d = createDeviceNode(device);
835 network.nodes.push(d);
836 network.lookup[id] = d;
Simon Hunt99c13842014-11-06 18:23:12 -0800837 updateNodes();
Simon Huntd6f5a272014-11-29 23:45:50 -0800838 fStart();
Simon Hunt99c13842014-11-06 18:23:12 -0800839 }
840
Simon Hunt99c13842014-11-06 18:23:12 -0800841 function addLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800842 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800843 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800844 result = findLink(link, 'add'),
845 bad = result.badLogic,
Simon Huntca867ac2014-11-28 18:07:35 -0800846 d = result.ldata;
Simon Hunt8257f4c2014-11-16 19:34:54 -0800847
848 if (bad) {
849 logicError(bad + ': ' + link.id);
850 return;
851 }
852
Simon Huntca867ac2014-11-28 18:07:35 -0800853 if (d) {
Simon Hunt8257f4c2014-11-16 19:34:54 -0800854 // we already have a backing store link for src/dst nodes
Simon Huntca867ac2014-11-28 18:07:35 -0800855 addLinkUpdate(d, link);
Simon Hunt8257f4c2014-11-16 19:34:54 -0800856 return;
857 }
858
859 // no backing store link yet
Simon Huntca867ac2014-11-28 18:07:35 -0800860 d = createLink(link);
861 if (d) {
862 network.links.push(d);
863 network.lookup[d.key] = d;
Simon Hunt99c13842014-11-06 18:23:12 -0800864 updateLinks();
Simon Huntd6f5a272014-11-29 23:45:50 -0800865 fStart();
Simon Hunt99c13842014-11-06 18:23:12 -0800866 }
867 }
868
Simon Hunt56d51852014-11-09 13:03:35 -0800869 function addHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800870 evTrace(data);
Simon Hunt56d51852014-11-09 13:03:35 -0800871 var host = data.payload,
Simon Huntca867ac2014-11-28 18:07:35 -0800872 id = host.id,
873 d,
Simon Hunt56d51852014-11-09 13:03:35 -0800874 lnk;
Simon Huntca867ac2014-11-28 18:07:35 -0800875
876 if (network.lookup[id]) {
877 logicError('Host already added: ' + id);
878 return;
879 }
880
881 d = createHostNode(host);
882 network.nodes.push(d);
883 network.lookup[host.id] = d;
Simon Hunt56d51852014-11-09 13:03:35 -0800884 updateNodes();
885
886 lnk = createHostLink(host);
887 if (lnk) {
Simon Huntca867ac2014-11-28 18:07:35 -0800888 d.linkData = lnk; // cache ref on its host
Simon Hunt56d51852014-11-09 13:03:35 -0800889 network.links.push(lnk);
Simon Huntca867ac2014-11-28 18:07:35 -0800890 network.lookup[d.ingress] = lnk;
891 network.lookup[d.egress] = lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800892 updateLinks();
893 }
Simon Huntd6f5a272014-11-29 23:45:50 -0800894 fStart();
Simon Hunt56d51852014-11-09 13:03:35 -0800895 }
896
Simon Hunt44031102014-11-11 13:20:36 -0800897 // TODO: fold updateX(...) methods into one base method; remove duplication
Simon Hunt56a2ea42014-11-19 12:39:31 -0800898
899 function updateInstance(data) {
900 evTrace(data);
901 var inst = data.payload,
902 id = inst.id,
Simon Huntca867ac2014-11-28 18:07:35 -0800903 d = onosInstances[id];
904 if (d) {
905 $.extend(d, inst);
Simon Hunt56a2ea42014-11-19 12:39:31 -0800906 updateInstances();
Simon Hunt56a2ea42014-11-19 12:39:31 -0800907 } else {
908 logicError('updateInstance lookup fail. ID = "' + id + '"');
909 }
910 }
911
Simon Huntbb282f52014-11-10 11:08:19 -0800912 function updateDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800913 evTrace(data);
Simon Huntbb282f52014-11-10 11:08:19 -0800914 var device = data.payload,
915 id = device.id,
Simon Hunt6d9bd032014-11-28 22:16:40 -0800916 d = network.lookup[id],
917 wasOnline;
918
Simon Hunt62c47542014-11-22 22:16:32 -0800919 if (d) {
Simon Hunt6d9bd032014-11-28 22:16:40 -0800920 wasOnline = d.online;
Simon Hunt62c47542014-11-22 22:16:32 -0800921 $.extend(d, device);
922 if (positionNode(d, true)) {
Simon Hunt395a70c2014-11-22 23:17:40 -0800923 sendUpdateMeta(d, true);
Simon Hunt62c47542014-11-22 22:16:32 -0800924 }
925 updateNodes();
Simon Hunt6d9bd032014-11-28 22:16:40 -0800926 if (wasOnline !== d.online) {
927 findAttachedLinks(d.id).forEach(restyleLinkElement);
928 updateOfflineVisibility(d);
929 }
Simon Huntbb282f52014-11-10 11:08:19 -0800930 } else {
931 logicError('updateDevice lookup fail. ID = "' + id + '"');
932 }
933 }
934
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800935 function updateLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800936 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800937 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800938 result = findLink(link, 'update'),
939 bad = result.badLogic;
940 if (bad) {
941 logicError(bad + ': ' + link.id);
942 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800943 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800944 result.updateWith(link);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800945 }
946
Simon Hunt7cd48f32014-11-09 23:42:50 -0800947 function updateHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800948 evTrace(data);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800949 var host = data.payload,
Simon Huntbb282f52014-11-10 11:08:19 -0800950 id = host.id,
Simon Huntca867ac2014-11-28 18:07:35 -0800951 d = network.lookup[id];
952 if (d) {
953 $.extend(d, host);
Simon Hunt6d9bd032014-11-28 22:16:40 -0800954 if (positionNode(d, true)) {
955 sendUpdateMeta(d, true);
956 }
957 updateNodes(d);
Simon Huntbb282f52014-11-10 11:08:19 -0800958 } else {
959 logicError('updateHost lookup fail. ID = "' + id + '"');
960 }
Simon Hunt7cd48f32014-11-09 23:42:50 -0800961 }
962
Simon Hunt44031102014-11-11 13:20:36 -0800963 // TODO: fold removeX(...) methods into base method - remove dup code
Simon Hunt7b403bc2014-11-22 19:01:00 -0800964 function removeInstance(data) {
965 evTrace(data);
966 var inst = data.payload,
967 id = inst.id,
Simon Huntca867ac2014-11-28 18:07:35 -0800968 d = onosInstances[id];
969 if (d) {
970 var idx = find(id, onosOrder);
Simon Hunt7b403bc2014-11-22 19:01:00 -0800971 if (idx >= 0) {
972 onosOrder.splice(idx, 1);
973 }
974 delete onosInstances[id];
975 updateInstances();
976 } else {
977 logicError('updateInstance lookup fail. ID = "' + id + '"');
978 }
979 }
980
Simon Huntca867ac2014-11-28 18:07:35 -0800981 function removeDevice(data) {
982 evTrace(data);
983 var device = data.payload,
984 id = device.id,
985 d = network.lookup[id];
986 if (d) {
987 removeDeviceElement(d);
988 } else {
989 logicError('removeDevice lookup fail. ID = "' + id + '"');
990 }
991 }
992
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800993 function removeLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800994 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800995 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800996 result = findLink(link, 'remove'),
997 bad = result.badLogic;
998 if (bad) {
Simon Huntca867ac2014-11-28 18:07:35 -0800999 // may have already removed link, if attached to removed device
1000 console.warn(bad + ': ' + link.id);
Simon Hunt8257f4c2014-11-16 19:34:54 -08001001 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001002 }
Simon Hunt8257f4c2014-11-16 19:34:54 -08001003 result.removeRawLink();
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001004 }
1005
Simon Hunt44031102014-11-11 13:20:36 -08001006 function removeHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -08001007 evTrace(data);
Simon Hunt44031102014-11-11 13:20:36 -08001008 var host = data.payload,
1009 id = host.id,
Simon Huntca867ac2014-11-28 18:07:35 -08001010 d = network.lookup[id];
1011 if (d) {
1012 removeHostElement(d, true);
Simon Hunt44031102014-11-11 13:20:36 -08001013 } else {
Simon Huntca867ac2014-11-28 18:07:35 -08001014 // may have already removed host, if attached to removed device
1015 console.warn('removeHost lookup fail. ID = "' + id + '"');
Simon Hunt44031102014-11-11 13:20:36 -08001016 }
1017 }
1018
Simon Huntca867ac2014-11-28 18:07:35 -08001019 // the following events are server responses to user actions
Thomas Vachuska47635c62014-11-22 01:21:36 -08001020 function showSummary(data) {
1021 evTrace(data);
1022 populateSummary(data.payload);
Simon Hunt06811b72014-11-25 18:54:48 -08001023 showSummaryPane();
Thomas Vachuska47635c62014-11-22 01:21:36 -08001024 }
1025
Simon Hunt61d04042014-11-11 17:27:16 -08001026 function showDetails(data) {
Simon Hunta5e89142014-11-14 07:00:33 -08001027 evTrace(data);
Simon Hunt27d322d2014-11-28 10:45:43 -08001028 haveDetails = true;
Simon Hunt61d04042014-11-11 17:27:16 -08001029 populateDetails(data.payload);
Simon Hunt27d322d2014-11-28 10:45:43 -08001030 if (useDetails) {
1031 showDetailPane();
1032 }
Simon Hunt61d04042014-11-11 17:27:16 -08001033 }
1034
Simon Huntb53e0682014-11-12 13:32:01 -08001035 function showTraffic(data) {
Simon Hunta5e89142014-11-14 07:00:33 -08001036 evTrace(data);
Thomas Vachuska4731f122014-11-20 04:56:19 -08001037 var paths = data.payload.paths,
1038 hasTraffic = false;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -08001039
Thomas Vachuska3266abf2014-11-13 09:28:46 -08001040 // Revert any links hilighted previously.
Thomas Vachuska4731f122014-11-20 04:56:19 -08001041 link.style('stroke-width', null)
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08001042 .classed('primary secondary animated optical', false);
Simon Hunte2575b62014-11-18 15:25:53 -08001043 // Remove all previous labels.
1044 removeLinkLabels();
Thomas Vachuska3266abf2014-11-13 09:28:46 -08001045
Simon Hunte2575b62014-11-18 15:25:53 -08001046 // Now hilight all links in the paths payload, and attach
1047 // labels to them, if they are defined.
Simon Hunta255a2c2014-11-13 22:29:35 -08001048 paths.forEach(function (p) {
Simon Hunte2575b62014-11-18 15:25:53 -08001049 var n = p.links.length,
1050 i,
1051 ldata;
1052
Thomas Vachuska4731f122014-11-20 04:56:19 -08001053 hasTraffic = hasTraffic || p.traffic;
Simon Hunte2575b62014-11-18 15:25:53 -08001054 for (i=0; i<n; i++) {
1055 ldata = findLinkById(p.links[i]);
Thomas Vachuska4731f122014-11-20 04:56:19 -08001056 if (ldata && ldata.el) {
Simon Hunte2575b62014-11-18 15:25:53 -08001057 ldata.el.classed(p.class, true);
1058 ldata.label = p.labels[i];
Thomas Vachuskadea45ff2014-11-12 18:35:46 -08001059 }
Simon Hunte2575b62014-11-18 15:25:53 -08001060 }
Thomas Vachuskadea45ff2014-11-12 18:35:46 -08001061 });
Thomas Vachuska4731f122014-11-20 04:56:19 -08001062
Simon Hunte2575b62014-11-18 15:25:53 -08001063 updateLinks();
Thomas Vachuska4731f122014-11-20 04:56:19 -08001064
1065 if (hasTraffic && !antTimer) {
1066 startAntTimer();
1067 } else if (!hasTraffic && antTimer) {
1068 stopAntTimer();
1069 }
Simon Huntb53e0682014-11-12 13:32:01 -08001070 }
1071
Simon Hunt56d51852014-11-09 13:03:35 -08001072 // ...............................
1073
Simon Hunt99c13842014-11-06 18:23:12 -08001074 function unknownEvent(data) {
Simon Hunt434cf142014-11-24 11:10:28 -08001075 console.warn('Unknown event type: "' + data.event + '"', data);
Simon Hunt99c13842014-11-06 18:23:12 -08001076 }
1077
1078 function handleServerEvent(data) {
1079 var fn = eventDispatch[data.event] || unknownEvent;
1080 fn(data);
1081 }
1082
1083 // ==============================
Simon Hunt61d04042014-11-11 17:27:16 -08001084 // Out-going messages...
1085
Simon Huntb53e0682014-11-12 13:32:01 -08001086 function nSel() {
1087 return selectOrder.length;
1088 }
Simon Hunt61d04042014-11-11 17:27:16 -08001089 function getSel(idx) {
1090 return selections[selectOrder[idx]];
1091 }
Simon Huntb53e0682014-11-12 13:32:01 -08001092 function allSelectionsClass(cls) {
1093 for (var i=0, n=nSel(); i<n; i++) {
1094 if (getSel(i).obj.class !== cls) {
1095 return false;
1096 }
1097 }
1098 return true;
1099 }
Simon Hunt61d04042014-11-11 17:27:16 -08001100
Thomas Vachuska47635c62014-11-22 01:21:36 -08001101 function toggleInstances() {
1102 if (!oiBox.isVisible()) {
Simon Huntb0ecfa52014-11-23 21:05:12 -08001103 showInstances();
Thomas Vachuska47635c62014-11-22 01:21:36 -08001104 } else {
Simon Huntb0ecfa52014-11-23 21:05:12 -08001105 hideInstances();
Thomas Vachuska47635c62014-11-22 01:21:36 -08001106 }
1107 }
1108
Simon Huntb0ecfa52014-11-23 21:05:12 -08001109 function showInstances() {
1110 oiBox.show();
1111 colorAffinity = true;
1112 updateDeviceColors();
1113 }
1114
1115 function hideInstances() {
1116 oiBox.hide();
1117 colorAffinity = false;
1118 cancelAffinity();
1119 updateDeviceColors();
1120 }
1121
Thomas Vachuska1e68bdd2014-11-29 13:53:10 -08001122 function equalizeMasters() {
Thomas Vachuska1e68bdd2014-11-29 13:53:10 -08001123 sendMessage('equalizeMasters');
Simon Huntc1cc81c2014-11-29 14:59:01 -08001124 flash('Equalizing master roles');
Thomas Vachuska1e68bdd2014-11-29 13:53:10 -08001125 }
1126
Thomas Vachuska47635c62014-11-22 01:21:36 -08001127 function toggleSummary() {
1128 if (!summaryPane.isVisible()) {
1129 requestSummary();
1130 } else {
1131 cancelSummary();
1132 }
1133 }
1134
1135 // request overall summary data
1136 function requestSummary() {
Simon Huntc1cc81c2014-11-29 14:59:01 -08001137 sendMessage('requestSummary');
Thomas Vachuska47635c62014-11-22 01:21:36 -08001138 }
1139
1140 function cancelSummary() {
Simon Huntc1cc81c2014-11-29 14:59:01 -08001141 sendMessage('cancelSummary');
Simon Hunt06811b72014-11-25 18:54:48 -08001142 hideSummaryPane();
Thomas Vachuska47635c62014-11-22 01:21:36 -08001143 }
1144
Simon Hunt27d322d2014-11-28 10:45:43 -08001145 function toggleDetails() {
1146 useDetails = !useDetails;
1147 if (useDetails) {
1148 flash('Enable details pane');
1149 if (haveDetails) {
1150 showDetailPane();
1151 }
1152 } else {
1153 flash('Disable details pane');
1154 hideDetailPane();
1155 }
1156 }
1157
Simon Hunt06811b72014-11-25 18:54:48 -08001158 // encapsulate interaction between summary and details panes
1159 function showSummaryPane() {
1160 if (detailPane.isVisible()) {
1161 detailPane.down(summaryPane.show);
1162 } else {
1163 summaryPane.show();
1164 }
1165 }
1166
1167 function hideSummaryPane() {
1168 summaryPane.hide(function () {
1169 if (detailPane.isVisible()) {
1170 detailPane.up();
1171 }
1172 });
1173 }
1174
1175 function showDetailPane() {
1176 if (summaryPane.isVisible()) {
1177 detailPane.down(detailPane.show);
1178 } else {
1179 detailPane.up(detailPane.show);
1180 }
1181 }
1182
1183 function hideDetailPane() {
1184 detailPane.hide();
1185 }
1186
1187
Simon Hunt61d04042014-11-11 17:27:16 -08001188 // request details for the selected element
Simon Huntd72bc702014-11-13 18:38:04 -08001189 // invoked from selection of a single node.
Simon Hunt61d04042014-11-11 17:27:16 -08001190 function requestDetails() {
Simon Huntc1cc81c2014-11-29 14:59:01 -08001191 var data = getSel(0).obj;
1192 sendMessage('requestDetails', {
1193 id: data.id,
1194 class: data.class
1195 });
Simon Hunt61d04042014-11-11 17:27:16 -08001196 }
1197
Thomas Vachuska9edca302014-11-22 17:06:42 -08001198 function addHostIntentAction() {
Simon Huntd72bc702014-11-13 18:38:04 -08001199 sendMessage('addHostIntent', {
Thomas Vachuska9edca302014-11-22 17:06:42 -08001200 one: selectOrder[0],
1201 two: selectOrder[1],
1202 ids: selectOrder
Simon Huntd72bc702014-11-13 18:38:04 -08001203 });
Simon Hunt27d322d2014-11-28 10:45:43 -08001204 flash('Host-to-Host flow added');
Simon Huntd72bc702014-11-13 18:38:04 -08001205 }
1206
Thomas Vachuska9edca302014-11-22 17:06:42 -08001207 function addMultiSourceIntentAction() {
1208 sendMessage('addMultiSourceIntent', {
1209 src: selectOrder.slice(0, selectOrder.length - 1),
1210 dst: selectOrder[selectOrder.length - 1],
1211 ids: selectOrder
1212 });
Simon Hunt27d322d2014-11-28 10:45:43 -08001213 flash('Multi-Source flow added');
Thomas Vachuska29617e52014-11-20 03:17:46 -08001214 }
1215
Thomas Vachuska9edca302014-11-22 17:06:42 -08001216
Thomas Vachuska47635c62014-11-22 01:21:36 -08001217 function cancelTraffic() {
Simon Huntc1cc81c2014-11-29 14:59:01 -08001218 sendMessage('cancelTraffic');
Thomas Vachuska47635c62014-11-22 01:21:36 -08001219 }
1220
Thomas Vachuska9edca302014-11-22 17:06:42 -08001221 function requestTrafficForMode() {
1222 if (hoverMode === hoverModeAll) {
1223 requestAllTraffic();
1224 } else if (hoverMode === hoverModeFlows) {
1225 requestDeviceLinkFlows();
1226 } else if (hoverMode === hoverModeIntents) {
1227 requestSelectTraffic();
Simon Huntd72bc702014-11-13 18:38:04 -08001228 }
Thomas Vachuska9edca302014-11-22 17:06:42 -08001229 }
Simon Huntd72bc702014-11-13 18:38:04 -08001230
Thomas Vachuska9edca302014-11-22 17:06:42 -08001231 function showTrafficAction() {
1232 hoverMode = hoverModeIntents;
1233 requestSelectTraffic();
Simon Hunt27d322d2014-11-28 10:45:43 -08001234 flash('Related Traffic');
Thomas Vachuska9edca302014-11-22 17:06:42 -08001235 }
1236
1237 function requestSelectTraffic() {
Simon Huntc1cc81c2014-11-29 14:59:01 -08001238 function hoverValid() {
1239 return hoverMode === hoverModeIntents &&
1240 hovered &&
1241 (hovered.class === 'host' || hovered.class === 'device');
1242 }
1243
Thomas Vachuska9edca302014-11-22 17:06:42 -08001244 if (validateSelectionContext()) {
Thomas Vachuska9edca302014-11-22 17:06:42 -08001245 sendMessage('requestTraffic', {
1246 ids: selectOrder,
Simon Huntc1cc81c2014-11-29 14:59:01 -08001247 hover: hoverValid() ? hovered.id : ''
Thomas Vachuska9edca302014-11-22 17:06:42 -08001248 });
1249 }
Simon Huntd72bc702014-11-13 18:38:04 -08001250 }
1251
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -08001252
Thomas Vachuska29617e52014-11-20 03:17:46 -08001253 function showDeviceLinkFlowsAction() {
Thomas Vachuska9edca302014-11-22 17:06:42 -08001254 hoverMode = hoverModeFlows;
1255 requestDeviceLinkFlows();
Simon Hunt27d322d2014-11-28 10:45:43 -08001256 flash('Device Flows');
Thomas Vachuska29617e52014-11-20 03:17:46 -08001257 }
1258
Thomas Vachuska9edca302014-11-22 17:06:42 -08001259 function requestDeviceLinkFlows() {
Simon Huntc1cc81c2014-11-29 14:59:01 -08001260 function hoverValid() {
1261 return hoverMode === hoverModeFlows &&
1262 hovered && (hovered.class === 'device');
1263 }
1264
Thomas Vachuska9edca302014-11-22 17:06:42 -08001265 if (validateSelectionContext()) {
Thomas Vachuska9edca302014-11-22 17:06:42 -08001266 sendMessage('requestDeviceLinkFlows', {
1267 ids: selectOrder,
Simon Huntc1cc81c2014-11-29 14:59:01 -08001268 hover: hoverValid() ? hovered.id : ''
Thomas Vachuska9edca302014-11-22 17:06:42 -08001269 });
1270 }
1271 }
1272
1273
1274 function showAllTrafficAction() {
1275 hoverMode = hoverModeAll;
1276 requestAllTraffic();
Simon Hunt27d322d2014-11-28 10:45:43 -08001277 flash('All Traffic');
Thomas Vachuska9edca302014-11-22 17:06:42 -08001278 }
1279
1280 function requestAllTraffic() {
Simon Huntc1cc81c2014-11-29 14:59:01 -08001281 sendMessage('requestAllTraffic');
Thomas Vachuska9edca302014-11-22 17:06:42 -08001282 }
1283
1284 function validateSelectionContext() {
Thomas Vachuska29617e52014-11-20 03:17:46 -08001285 if (!hovered && nSel() === 0) {
Thomas Vachuska47635c62014-11-22 01:21:36 -08001286 cancelTraffic();
Thomas Vachuska9edca302014-11-22 17:06:42 -08001287 return false;
Thomas Vachuska29617e52014-11-20 03:17:46 -08001288 }
Thomas Vachuska9edca302014-11-22 17:06:42 -08001289 return true;
Thomas Vachuska29617e52014-11-20 03:17:46 -08001290 }
Simon Huntd72bc702014-11-13 18:38:04 -08001291
Simon Hunta6a9fe72014-11-20 11:17:12 -08001292
Simon Hunt61d04042014-11-11 17:27:16 -08001293 // ==============================
Simon Hunta5e89142014-11-14 07:00:33 -08001294 // onos instance panel functions
Simon Huntb82f6902014-11-22 11:53:15 -08001295
Simon Hunt7b403bc2014-11-22 19:01:00 -08001296 var instCfg = {
1297 rectPad: 8,
1298 nodeOx: 9,
1299 nodeOy: 9,
1300 nodeDim: 40,
1301 birdOx: 19,
1302 birdOy: 21,
1303 birdDim: 21,
1304 uiDy: 45,
1305 titleDy: 30,
1306 textYOff: 20,
1307 textYSpc: 15
1308 };
1309
1310 function viewBox(dim) {
1311 return '0 0 ' + dim.w + ' ' + dim.h;
1312 }
1313
1314 function instRectAttr(dim) {
1315 var pad = instCfg.rectPad;
1316 return {
1317 x: pad,
1318 y: pad,
1319 width: dim.w - pad*2,
1320 height: dim.h - pad*2,
Simon Huntb0ecfa52014-11-23 21:05:12 -08001321 rx: 6
Simon Hunt7b403bc2014-11-22 19:01:00 -08001322 };
1323 }
1324
1325 function computeDim(self) {
1326 var css = window.getComputedStyle(self);
1327 return {
1328 w: stripPx(css.width),
1329 h: stripPx(css.height)
1330 };
Simon Huntb82f6902014-11-22 11:53:15 -08001331 }
Simon Hunta5e89142014-11-14 07:00:33 -08001332
1333 function updateInstances() {
1334 var onoses = oiBox.el.selectAll('.onosInst')
Simon Huntb82f6902014-11-22 11:53:15 -08001335 .data(onosOrder, function (d) { return d.id; }),
Simon Hunt7b403bc2014-11-22 19:01:00 -08001336 instDim = {w:0,h:0},
1337 c = instCfg;
Simon Hunta5e89142014-11-14 07:00:33 -08001338
Simon Hunt7b403bc2014-11-22 19:01:00 -08001339 function nSw(n) {
1340 return '# Switches: ' + n;
1341 }
Simon Hunta5e89142014-11-14 07:00:33 -08001342
Simon Huntb82f6902014-11-22 11:53:15 -08001343 // operate on existing onos instances if necessary
1344 onoses.each(function (d) {
1345 var el = d3.select(this),
1346 svg = el.select('svg');
Simon Hunt7b403bc2014-11-22 19:01:00 -08001347 instDim = computeDim(this);
Simon Huntb82f6902014-11-22 11:53:15 -08001348
1349 // update online state
1350 el.classed('online', d.online);
1351
1352 // update ui-attached state
1353 svg.select('use.uiBadge').remove();
1354 if (d.uiAttached) {
1355 attachUiBadge(svg);
1356 }
1357
Simon Hunt7b403bc2014-11-22 19:01:00 -08001358 function updAttr(id, value) {
1359 svg.select('text.instLabel.'+id).text(value);
1360 }
1361
1362 updAttr('ip', d.ip);
1363 updAttr('ns', nSw(d.switches));
Simon Huntb82f6902014-11-22 11:53:15 -08001364 });
1365
1366
1367 // operate on new onos instances
Simon Hunta5e89142014-11-14 07:00:33 -08001368 var entering = onoses.enter()
1369 .append('div')
1370 .attr('class', 'onosInst')
1371 .classed('online', function (d) { return d.online; })
Simon Hunt9c15eca2014-11-15 18:37:59 -08001372 .on('click', clickInst);
1373
Simon Huntb82f6902014-11-22 11:53:15 -08001374 entering.each(function (d) {
Simon Hunt9c15eca2014-11-15 18:37:59 -08001375 var el = d3.select(this),
Simon Hunt7b403bc2014-11-22 19:01:00 -08001376 rectAttr,
1377 svg;
1378 instDim = computeDim(this);
1379 rectAttr = instRectAttr(instDim);
Simon Hunt9c15eca2014-11-15 18:37:59 -08001380
Simon Hunt7b403bc2014-11-22 19:01:00 -08001381 svg = el.append('svg').attr({
1382 width: instDim.w,
1383 height: instDim.h,
1384 viewBox: viewBox(instDim)
Simon Hunt95908012014-11-20 10:20:26 -08001385 });
Simon Huntb82f6902014-11-22 11:53:15 -08001386
Simon Hunt7b403bc2014-11-22 19:01:00 -08001387 svg.append('rect').attr(rectAttr);
Simon Hunt9c15eca2014-11-15 18:37:59 -08001388
Thomas Vachuskae02e11c2014-11-24 16:13:52 -08001389 //appendGlyph(svg, c.nodeOx, c.nodeOy, c.nodeDim, '#node');
1390 appendBadge(svg, 14, 14, 28, '#bird');
Simon Huntb82f6902014-11-22 11:53:15 -08001391
1392 if (d.uiAttached) {
1393 attachUiBadge(svg);
1394 }
1395
Simon Hunt7b403bc2014-11-22 19:01:00 -08001396 var left = c.nodeOx + c.nodeDim,
1397 len = rectAttr.width - left,
1398 hlen = len / 2,
1399 midline = hlen + left;
Simon Hunt9c15eca2014-11-15 18:37:59 -08001400
Simon Hunt7b403bc2014-11-22 19:01:00 -08001401 // title
1402 svg.append('text')
1403 .attr({
1404 class: 'instTitle',
1405 x: midline,
1406 y: c.titleDy
1407 })
1408 .text(d.id);
1409
1410 // a couple of attributes
1411 var ty = c.titleDy + c.textYOff;
1412
1413 function addAttr(id, label) {
1414 svg.append('text').attr({
1415 class: 'instLabel ' + id,
1416 x: midline,
1417 y: ty
1418 }).text(label);
1419 ty += c.textYSpc;
1420 }
1421
1422 addAttr('ip', d.ip);
1423 addAttr('ns', nSw(d.switches));
Simon Hunt9c15eca2014-11-15 18:37:59 -08001424 });
Simon Hunta5e89142014-11-14 07:00:33 -08001425
1426 // operate on existing + new onoses here
Simon Hunt8f40cce2014-11-23 15:57:30 -08001427 // set the affinity colors...
1428 onoses.each(function (d) {
1429 var el = d3.select(this),
1430 rect = el.select('svg').select('rect'),
1431 col = instColor(d.id, d.online);
1432 rect.style('fill', col);
1433 });
Simon Hunta5e89142014-11-14 07:00:33 -08001434
Simon Hunt7b403bc2014-11-22 19:01:00 -08001435 // adjust the panel size appropriately...
1436 oiBox.width(instDim.w * onosOrder.length);
1437 oiBox.height(instDim.h);
1438
1439 // remove any outgoing instances
1440 onoses.exit().remove();
Simon Hunta5e89142014-11-14 07:00:33 -08001441 }
1442
Simon Hunt8f40cce2014-11-23 15:57:30 -08001443 function instColor(id, online) {
1444 return cat7.get(id, !online, network.view.getTheme());
1445 }
1446
Simon Hunt9462e8c2014-11-14 17:28:09 -08001447 function clickInst(d) {
1448 var el = d3.select(this),
1449 aff = el.classed('affinity');
1450 if (!aff) {
1451 setAffinity(el, d);
1452 } else {
1453 cancelAffinity();
1454 }
1455 }
1456
1457 function setAffinity(el, d) {
1458 d3.selectAll('.onosInst')
1459 .classed('mastership', true)
1460 .classed('affinity', false);
1461 el.classed('affinity', true);
1462
1463 suppressLayers(true);
1464 node.each(function (n) {
1465 if (n.master === d.id) {
1466 n.el.classed('suppressed', false);
1467 }
1468 });
1469 oiShowMaster = true;
1470 }
1471
1472 function cancelAffinity() {
1473 d3.selectAll('.onosInst')
1474 .classed('mastership affinity', false);
1475 restoreLayerState();
1476 oiShowMaster = false;
1477 }
1478
Simon Hunt7b403bc2014-11-22 19:01:00 -08001479 // TODO: these should be moved out to utility module.
1480 function stripPx(s) {
1481 return s.replace(/px$/,'');
1482 }
1483
1484 function appendUse(svg, ox, oy, dim, iid, cls) {
1485 var use = svg.append('use').attr({
1486 transform: translate(ox,oy),
1487 'xlink:href': iid,
1488 width: dim,
1489 height: dim
1490 });
1491 if (cls) {
1492 use.classed(cls, true);
1493 }
1494 return use;
1495 }
1496
1497 function appendGlyph(svg, ox, oy, dim, iid, cls) {
1498 appendUse(svg, ox, oy, dim, iid, cls).classed('glyphIcon', true);
1499 }
1500
1501 function appendBadge(svg, ox, oy, dim, iid, cls) {
1502 appendUse(svg, ox, oy, dim, iid,cls ).classed('badgeIcon', true);
1503 }
1504
1505 function attachUiBadge(svg) {
1506 appendBadge(svg, 12, instCfg.uiDy, 30, '#uiAttached', 'uiBadge');
1507 }
1508
Simon Hunt434cf142014-11-24 11:10:28 -08001509 function visVal(b) {
1510 return b ? 'visible' : 'hidden';
1511 }
1512
Simon Hunta5e89142014-11-14 07:00:33 -08001513 // ==============================
Simon Hunt99c13842014-11-06 18:23:12 -08001514 // force layout modification functions
1515
1516 function translate(x, y) {
1517 return 'translate(' + x + ',' + y + ')';
1518 }
Simon Huntd6f5a272014-11-29 23:45:50 -08001519 function scale(x,y) {
1520 return 'scale(' + x + ',' + y + ')';
1521 }
1522 function skewX(x) {
1523 return 'skewX(' + x + ')';
1524 }
Simon Hunte2575b62014-11-18 15:25:53 -08001525 function rotate(deg) {
1526 return 'rotate(' + deg + ')';
1527 }
1528
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001529 function missMsg(what, id) {
1530 return '\n[' + what + '] "' + id + '" missing ';
1531 }
1532
1533 function linkEndPoints(srcId, dstId) {
1534 var srcNode = network.lookup[srcId],
1535 dstNode = network.lookup[dstId],
1536 sMiss = !srcNode ? missMsg('src', srcId) : '',
1537 dMiss = !dstNode ? missMsg('dst', dstId) : '';
1538
1539 if (sMiss || dMiss) {
1540 logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
1541 return null;
1542 }
1543 return {
1544 source: srcNode,
1545 target: dstNode,
1546 x1: srcNode.x,
1547 y1: srcNode.y,
1548 x2: dstNode.x,
1549 y2: dstNode.y
1550 };
1551 }
1552
Simon Hunt56d51852014-11-09 13:03:35 -08001553 function createHostLink(host) {
1554 var src = host.id,
1555 dst = host.cp.device,
Simon Hunt7cd48f32014-11-09 23:42:50 -08001556 id = host.ingress,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001557 lnk = linkEndPoints(src, dst);
Simon Hunt56d51852014-11-09 13:03:35 -08001558
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001559 if (!lnk) {
Simon Hunt56d51852014-11-09 13:03:35 -08001560 return null;
1561 }
1562
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001563 // Synthesize link ...
1564 $.extend(lnk, {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001565 key: id,
Simon Hunt56d51852014-11-09 13:03:35 -08001566 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -08001567
1568 type: function () { return 'hostLink'; },
Simon Hunt6d9bd032014-11-28 22:16:40 -08001569 online: function () {
1570 // hostlink target is edge switch
1571 return lnk.target.online;
1572 },
Simon Hunt8257f4c2014-11-16 19:34:54 -08001573 linkWidth: function () { return 1; }
Simon Hunt7cd48f32014-11-09 23:42:50 -08001574 });
Simon Hunt99c13842014-11-06 18:23:12 -08001575 return lnk;
1576 }
1577
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001578 function createLink(link) {
Simon Hunta6a9fe72014-11-20 11:17:12 -08001579 var lnk = linkEndPoints(link.src, link.dst);
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001580
1581 if (!lnk) {
1582 return null;
1583 }
1584
Simon Hunt8257f4c2014-11-16 19:34:54 -08001585 $.extend(lnk, {
1586 key: link.id,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001587 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -08001588 fromSource: link,
1589
1590 // functions to aggregate dual link state
1591 type: function () {
1592 var s = lnk.fromSource,
1593 t = lnk.fromTarget;
1594 return (s && s.type) || (t && t.type) || defaultLinkType;
1595 },
1596 online: function () {
1597 var s = lnk.fromSource,
Simon Hunt6d9bd032014-11-28 22:16:40 -08001598 t = lnk.fromTarget,
1599 both = lnk.source.online && lnk.target.online;
1600 return both && ((s && s.online) || (t && t.online));
Simon Hunt8257f4c2014-11-16 19:34:54 -08001601 },
1602 linkWidth: function () {
1603 var s = lnk.fromSource,
1604 t = lnk.fromTarget,
1605 ws = (s && s.linkWidth) || 0,
1606 wt = (t && t.linkWidth) || 0;
1607 return Math.max(ws, wt);
1608 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001609 });
1610 return lnk;
Simon Hunt1a9eff92014-11-07 11:06:34 -08001611 }
1612
Simon Hunte2575b62014-11-18 15:25:53 -08001613 function removeLinkLabels() {
1614 network.links.forEach(function (d) {
1615 d.label = '';
1616 });
1617 }
1618
Simon Hunt434cf142014-11-24 11:10:28 -08001619 function showHostVis(el) {
1620 el.style('visibility', visVal(showHosts));
1621 }
1622
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001623 var widthRatio = 1.4,
1624 linkScale = d3.scale.linear()
1625 .domain([1, 12])
1626 .range([widthRatio, 12 * widthRatio])
1627 .clamp(true);
1628
Simon Hunt99c13842014-11-06 18:23:12 -08001629 function updateLinks() {
1630 link = linkG.selectAll('.link')
Simon Hunt8257f4c2014-11-16 19:34:54 -08001631 .data(network.links, function (d) { return d.key; });
Simon Hunt99c13842014-11-06 18:23:12 -08001632
1633 // operate on existing links, if necessary
1634 // link .foo() .bar() ...
1635
1636 // operate on entering links:
1637 var entering = link.enter()
1638 .append('line')
1639 .attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001640 x1: function (d) { return d.x1; },
1641 y1: function (d) { return d.y1; },
1642 x2: function (d) { return d.x2; },
1643 y2: function (d) { return d.y2; },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001644 stroke: config.topo.linkInColor,
1645 'stroke-width': config.topo.linkInWidth
Simon Hunt99c13842014-11-06 18:23:12 -08001646 });
1647
1648 // augment links
Simon Hunt7cd48f32014-11-09 23:42:50 -08001649 entering.each(function (d) {
1650 var link = d3.select(this);
1651 // provide ref to element selection from backing data....
1652 d.el = link;
Simon Hunt8257f4c2014-11-16 19:34:54 -08001653 restyleLinkElement(d);
Simon Hunt434cf142014-11-24 11:10:28 -08001654 if (d.type() === 'hostLink') {
1655 showHostVis(link);
1656 }
Simon Hunt7cd48f32014-11-09 23:42:50 -08001657 });
Thomas Vachuska4830d392014-11-09 17:09:56 -08001658
1659 // operate on both existing and new links, if necessary
1660 //link .foo() .bar() ...
1661
Simon Hunte2575b62014-11-18 15:25:53 -08001662 // apply or remove labels
1663 var labelData = getLabelData();
1664 applyLinkLabels(labelData);
1665
Thomas Vachuska4830d392014-11-09 17:09:56 -08001666 // operate on exiting links:
Thomas Vachuska4830d392014-11-09 17:09:56 -08001667 link.exit()
Simon Hunt6d9bd032014-11-28 22:16:40 -08001668 .attr('stroke-dasharray', '3 3')
Simon Hunt13bf9c82014-11-18 07:26:44 -08001669 .style('opacity', 0.5)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001670 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -08001671 .duration(1500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001672 .attr({
Simon Hunt6d9bd032014-11-28 22:16:40 -08001673 'stroke-dasharray': '3 12',
Simon Hunt13bf9c82014-11-18 07:26:44 -08001674 stroke: config.topo.linkOutColor,
1675 'stroke-width': config.topo.linkOutWidth
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001676 })
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001677 .style('opacity', 0.0)
Thomas Vachuska4830d392014-11-09 17:09:56 -08001678 .remove();
Simon Hunte2575b62014-11-18 15:25:53 -08001679
1680 // NOTE: invoke a single tick to force the labels to position
1681 // onto their links.
1682 tick();
1683 }
1684
1685 function getLabelData() {
1686 // create the backing data for showing labels..
1687 var data = [];
1688 link.each(function (d) {
1689 if (d.label) {
1690 data.push({
1691 id: 'lab-' + d.key,
1692 key: d.key,
1693 label: d.label,
1694 ldata: d
1695 });
1696 }
1697 });
1698 return data;
1699 }
1700
1701 var linkLabelOffset = '0.3em';
1702
1703 function applyLinkLabels(data) {
1704 var entering;
1705
1706 linkLabel = linkLabelG.selectAll('.linkLabel')
1707 .data(data, function (d) { return d.id; });
1708
Simon Hunt56a2ea42014-11-19 12:39:31 -08001709 // for elements already existing, we need to update the text
1710 // and adjust the rectangle size to fit
1711 linkLabel.each(function (d) {
1712 var el = d3.select(this),
1713 rect = el.select('rect'),
1714 text = el.select('text');
1715 text.text(d.label);
1716 rect.attr(rectAroundText(el));
1717 });
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -08001718
Simon Hunte2575b62014-11-18 15:25:53 -08001719 entering = linkLabel.enter().append('g')
1720 .classed('linkLabel', true)
1721 .attr('id', function (d) { return d.id; });
1722
1723 entering.each(function (d) {
1724 var el = d3.select(this),
1725 rect,
1726 text,
1727 parms = {
1728 x1: d.ldata.x1,
1729 y1: d.ldata.y1,
1730 x2: d.ldata.x2,
1731 y2: d.ldata.y2
1732 };
1733
1734 d.el = el;
1735 rect = el.append('rect');
1736 text = el.append('text').text(d.label);
1737 rect.attr(rectAroundText(el));
1738 text.attr('dy', linkLabelOffset);
1739
1740 el.attr('transform', transformLabel(parms));
1741 });
1742
1743 // Remove any links that are no longer required.
1744 linkLabel.exit().remove();
1745 }
1746
1747 function rectAroundText(el) {
1748 var text = el.select('text'),
1749 box = text.node().getBBox();
1750
1751 // translate the bbox so that it is centered on [x,y]
1752 box.x = -box.width / 2;
1753 box.y = -box.height / 2;
1754
1755 // add padding
1756 box.x -= 1;
1757 box.width += 2;
1758 return box;
1759 }
1760
1761 function transformLabel(p) {
1762 var dx = p.x2 - p.x1,
1763 dy = p.y2 - p.y1,
1764 xMid = dx/2 + p.x1,
1765 yMid = dy/2 + p.y1;
Simon Hunte2575b62014-11-18 15:25:53 -08001766 return translate(xMid, yMid);
Simon Hunt99c13842014-11-06 18:23:12 -08001767 }
1768
1769 function createDeviceNode(device) {
1770 // start with the object as is
1771 var node = device,
Simon Huntbb282f52014-11-10 11:08:19 -08001772 type = device.type,
Simon Huntc72967b2014-11-20 09:21:42 -08001773 svgCls = type ? 'node device ' + type : 'node device',
1774 labels = device.labels || [];
1775
Simon Hunt99c13842014-11-06 18:23:12 -08001776 // Augment as needed...
1777 node.class = 'device';
Simon Huntbb282f52014-11-10 11:08:19 -08001778 node.svgClass = device.online ? svgCls + ' online' : svgCls;
Simon Hunt99c13842014-11-06 18:23:12 -08001779 positionNode(node);
Simon Hunt99c13842014-11-06 18:23:12 -08001780 return node;
1781 }
1782
Simon Hunt56d51852014-11-09 13:03:35 -08001783 function createHostNode(host) {
1784 // start with the object as is
1785 var node = host;
1786
1787 // Augment as needed...
1788 node.class = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001789 if (!node.type) {
Simon Hunt209155e2014-11-21 12:16:09 -08001790 node.type = 'endstation';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001791 }
Simon Hunt7fa116d2014-11-17 14:16:55 -08001792 node.svgClass = 'node host ' + node.type;
Simon Hunt56d51852014-11-09 13:03:35 -08001793 positionNode(node);
Simon Hunt56d51852014-11-09 13:03:35 -08001794 return node;
1795 }
1796
Simon Hunt62c47542014-11-22 22:16:32 -08001797 function positionNode(node, forUpdate) {
Simon Hunt99c13842014-11-06 18:23:12 -08001798 var meta = node.metaUi,
Simon Huntac9e24f2014-11-12 10:12:21 -08001799 x = meta && meta.x,
1800 y = meta && meta.y,
1801 xy;
Simon Hunt99c13842014-11-06 18:23:12 -08001802
Simon Huntac9e24f2014-11-12 10:12:21 -08001803 // If we have [x,y] already, use that...
Simon Hunt99c13842014-11-06 18:23:12 -08001804 if (x && y) {
1805 node.fixed = true;
Simon Hunt62c47542014-11-22 22:16:32 -08001806 node.px = node.x = x;
1807 node.py = node.y = y;
Simon Huntac9e24f2014-11-12 10:12:21 -08001808 return;
Simon Hunt99c13842014-11-06 18:23:12 -08001809 }
Simon Huntac9e24f2014-11-12 10:12:21 -08001810
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001811 var location = node.location;
1812 if (location && location.type === 'latlng') {
Simon Hunt1b18aa52014-11-29 17:57:55 -08001813 var coord = geoMapProj([location.lng, location.lat]);
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001814 node.fixed = true;
Simon Hunt62c47542014-11-22 22:16:32 -08001815 node.px = node.x = coord[0];
1816 node.py = node.y = coord[1];
Simon Hunt62c47542014-11-22 22:16:32 -08001817 return true;
1818 }
1819
1820 // if this is a node update (not a node add).. skip randomizer
1821 if (forUpdate) {
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001822 return;
1823 }
1824
Simon Huntac9e24f2014-11-12 10:12:21 -08001825 // Note: Placing incoming unpinned nodes at exactly the same point
1826 // (center of the view) causes them to explode outwards when
1827 // the force layout kicks in. So, we spread them out a bit
1828 // initially, to provide a more serene layout convergence.
1829 // Additionally, if the node is a host, we place it near
1830 // the device it is connected to.
1831
1832 function spread(s) {
1833 return Math.floor((Math.random() * s) - s/2);
1834 }
1835
1836 function randDim(dim) {
1837 return dim / 2 + spread(dim * 0.7071);
1838 }
1839
1840 function rand() {
1841 return {
1842 x: randDim(network.view.width()),
1843 y: randDim(network.view.height())
1844 };
1845 }
1846
1847 function near(node) {
1848 var min = 12,
1849 dx = spread(12),
1850 dy = spread(12);
1851 return {
1852 x: node.x + min + dx,
1853 y: node.y + min + dy
1854 };
1855 }
1856
1857 function getDevice(cp) {
1858 var d = network.lookup[cp.device];
1859 return d || rand();
1860 }
1861
1862 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
1863 $.extend(node, xy);
Simon Hunt99c13842014-11-06 18:23:12 -08001864 }
1865
Simon Hunt99c13842014-11-06 18:23:12 -08001866
Simon Huntc72967b2014-11-20 09:21:42 -08001867 function iconGlyphUrl(d) {
1868 var which = d.type || 'unknown';
1869 return '#' + which;
1870 }
1871
Simon Hunt99c13842014-11-06 18:23:12 -08001872 // returns the newly computed bounding box of the rectangle
1873 function adjustRectToFitText(n) {
1874 var text = n.select('text'),
1875 box = text.node().getBBox(),
1876 lab = config.labels;
1877
1878 text.attr('text-anchor', 'middle')
1879 .attr('y', '-0.8em')
1880 .attr('x', lab.imgPad/2);
1881
1882 // translate the bbox so that it is centered on [x,y]
1883 box.x = -box.width / 2;
1884 box.y = -box.height / 2;
1885
1886 // add padding
1887 box.x -= (lab.padLR + lab.imgPad/2);
1888 box.width += lab.padLR * 2 + lab.imgPad;
1889 box.y -= lab.padTB;
1890 box.height += lab.padTB * 2;
1891
1892 return box;
1893 }
1894
Simon Hunt1a9eff92014-11-07 11:06:34 -08001895 function mkSvgClass(d) {
1896 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
1897 }
1898
Simon Hunt7cd48f32014-11-09 23:42:50 -08001899 function hostLabel(d) {
1900 var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0;
1901 return d.labels[idx];
1902 }
1903 function deviceLabel(d) {
1904 var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0;
1905 return d.labels[idx];
1906 }
Simon Huntc72967b2014-11-20 09:21:42 -08001907 function trimLabel(label) {
1908 return (label && label.trim()) || '';
1909 }
1910
1911 function emptyBox() {
1912 return {
1913 x: -2,
1914 y: -2,
1915 width: 4,
1916 height: 4
1917 };
Simon Hunt7cd48f32014-11-09 23:42:50 -08001918 }
1919
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001920 function updateDeviceLabel(d) {
Simon Huntc72967b2014-11-20 09:21:42 -08001921 var label = trimLabel(deviceLabel(d)),
1922 noLabel = !label,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001923 node = d.el,
Simon Huntc72967b2014-11-20 09:21:42 -08001924 box,
1925 dx,
Simon Hunt395a70c2014-11-22 23:17:40 -08001926 dy,
1927 cfg = config.icons.device;
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001928
1929 node.select('text')
1930 .text(label)
1931 .style('opacity', 0)
1932 .transition()
1933 .style('opacity', 1);
1934
Simon Huntc72967b2014-11-20 09:21:42 -08001935 if (noLabel) {
1936 box = emptyBox();
Simon Hunt395a70c2014-11-22 23:17:40 -08001937 dx = -cfg.dim/2;
1938 dy = -cfg.dim/2;
Simon Huntc72967b2014-11-20 09:21:42 -08001939 } else {
1940 box = adjustRectToFitText(node);
Simon Hunt395a70c2014-11-22 23:17:40 -08001941 dx = box.x + cfg.xoff;
1942 dy = box.y + cfg.yoff;
Simon Huntc72967b2014-11-20 09:21:42 -08001943 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001944
1945 node.select('rect')
1946 .transition()
1947 .attr(box);
1948
Simon Huntc72967b2014-11-20 09:21:42 -08001949 node.select('g.deviceIcon')
Thomas Vachuska89543292014-11-19 11:28:33 -08001950 .transition()
Simon Huntc72967b2014-11-20 09:21:42 -08001951 .attr('transform', translate(dx, dy));
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001952 }
1953
1954 function updateHostLabel(d) {
Simon Hunt6d9bd032014-11-28 22:16:40 -08001955 var label = trimLabel(hostLabel(d));
1956 d.el.select('text').text(label);
Simon Huntbb282f52014-11-10 11:08:19 -08001957 }
1958
Simon Hunt434cf142014-11-24 11:10:28 -08001959 function updateHostVisibility() {
1960 var v = visVal(showHosts);
1961 nodeG.selectAll('.host').style('visibility', v);
1962 linkG.selectAll('.hostLink').style('visibility', v);
1963 }
1964
Simon Hunt6d9bd032014-11-28 22:16:40 -08001965 function findOfflineNodes() {
1966 var a = [];
1967 network.nodes.forEach(function (d) {
1968 if (d.class === 'device' && !d.online) {
1969 a.push(d);
1970 }
1971 });
1972 return a;
1973 }
1974
1975 function updateOfflineVisibility(dev) {
1976 var so = showOffline,
1977 sh = showHosts,
1978 vb = 'visibility',
1979 v, off, al, ah, db, b;
1980
1981 function updAtt(show) {
1982 al.forEach(function (d) {
1983 b = show && ((d.type() !== 'hostLink') || sh);
1984 d.el.style(vb, visVal(b));
1985 });
1986 ah.forEach(function (d) {
1987 b = show && sh;
1988 d.el.style(vb, visVal(b));
1989 });
1990 }
1991
1992 if (dev) {
1993 // updating a specific device that just toggled off/on-line
1994 db = dev.online || so;
1995 dev.el.style(vb, visVal(db));
1996 al = findAttachedLinks(dev.id);
1997 ah = findAttachedHosts(dev.id);
1998 updAtt(db);
1999 } else {
2000 // updating all offline devices
2001 v = visVal(so);
2002 off = findOfflineNodes();
2003 off.forEach(function (d) {
2004 d.el.style(vb, v);
2005 al = findAttachedLinks(d.id);
2006 ah = findAttachedHosts(d.id);
2007 updAtt(so);
2008 });
2009 }
2010 }
2011
Simon Hunt6ac93f32014-11-13 12:17:27 -08002012 function nodeMouseOver(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08002013 hovered = d;
Thomas Vachuska9edca302014-11-22 17:06:42 -08002014 requestTrafficForMode();
Simon Hunt6ac93f32014-11-13 12:17:27 -08002015 }
2016
2017 function nodeMouseOut(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08002018 hovered = null;
Thomas Vachuska9edca302014-11-22 17:06:42 -08002019 requestTrafficForMode();
Simon Hunt6ac93f32014-11-13 12:17:27 -08002020 }
Simon Huntbb282f52014-11-10 11:08:19 -08002021
Simon Hunteb1514d2014-11-20 09:57:29 -08002022 function addHostIcon(node, radius, iid) {
Thomas Vachuska89543292014-11-19 11:28:33 -08002023 var dim = radius * 1.5,
2024 xlate = -dim / 2;
2025
Simon Hunteb1514d2014-11-20 09:57:29 -08002026 node.append('use').attr({
2027 class: 'glyphIcon hostIcon',
2028 transform: translate(xlate,xlate),
2029 'xlink:href': iid,
2030 width: dim,
2031 height: dim
2032 });
Thomas Vachuska89543292014-11-19 11:28:33 -08002033 }
2034
Simon Hunt99c13842014-11-06 18:23:12 -08002035 function updateNodes() {
2036 node = nodeG.selectAll('.node')
2037 .data(network.nodes, function (d) { return d.id; });
2038
Simon Hunt62c47542014-11-22 22:16:32 -08002039 // operate on existing nodes...
2040 node.filter('.device').each(function (d) {
Simon Hunt62c47542014-11-22 22:16:32 -08002041 var node = d.el;
2042 node.classed('online', d.online);
2043 updateDeviceLabel(d);
2044 positionNode(d, true);
2045 });
2046
2047 node.filter('.host').each(function (d) {
Simon Hunt6d9bd032014-11-28 22:16:40 -08002048 updateHostLabel(d);
2049 positionNode(d, true);
Simon Hunt62c47542014-11-22 22:16:32 -08002050 });
Simon Hunt99c13842014-11-06 18:23:12 -08002051
2052 // operate on entering nodes:
2053 var entering = node.enter()
2054 .append('g')
2055 .attr({
2056 id: function (d) { return safeId(d.id); },
Simon Hunt1a9eff92014-11-07 11:06:34 -08002057 class: mkSvgClass,
Simon Hunt99c13842014-11-06 18:23:12 -08002058 transform: function (d) { return translate(d.x, d.y); },
2059 opacity: 0
2060 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08002061 .call(network.drag)
Simon Hunt6ac93f32014-11-13 12:17:27 -08002062 .on('mouseover', nodeMouseOver)
2063 .on('mouseout', nodeMouseOut)
Simon Hunt99c13842014-11-06 18:23:12 -08002064 .transition()
2065 .attr('opacity', 1);
2066
2067 // augment device nodes...
2068 entering.filter('.device').each(function (d) {
2069 var node = d3.select(this),
Simon Huntc72967b2014-11-20 09:21:42 -08002070 label = trimLabel(deviceLabel(d)),
2071 noLabel = !label,
Simon Hunt99c13842014-11-06 18:23:12 -08002072 box;
2073
Simon Hunt7cd48f32014-11-09 23:42:50 -08002074 // provide ref to element from backing data....
2075 d.el = node;
2076
Simon Hunt62c47542014-11-22 22:16:32 -08002077 node.append('rect').attr({ rx: 5, ry: 5 });
2078 node.append('text').text(label).attr('dy', '1.1em');
Simon Hunt99c13842014-11-06 18:23:12 -08002079 box = adjustRectToFitText(node);
Simon Hunta3dd9572014-11-20 15:22:41 -08002080 node.select('rect').attr(box);
Simon Huntc72967b2014-11-20 09:21:42 -08002081 addDeviceIcon(node, box, noLabel, iconGlyphUrl(d));
Simon Huntc7ee0662014-11-05 16:44:37 -08002082 });
Simon Hunt934c3ce2014-11-05 11:45:07 -08002083
Thomas Vachuska89543292014-11-19 11:28:33 -08002084
Simon Hunt56d51852014-11-09 13:03:35 -08002085 // augment host nodes...
2086 entering.filter('.host').each(function (d) {
2087 var node = d3.select(this),
Simon Hunt395a70c2014-11-22 23:17:40 -08002088 cfg = config.icons.host,
2089 r = cfg.radius[d.type] || cfg.defaultRadius,
Thomas Vachuska89543292014-11-19 11:28:33 -08002090 textDy = r + 10,
Simon Hunteb1514d2014-11-20 09:57:29 -08002091 iid = iconGlyphUrl(d);
Simon Hunt56d51852014-11-09 13:03:35 -08002092
Simon Hunt7cd48f32014-11-09 23:42:50 -08002093 // provide ref to element from backing data....
2094 d.el = node;
Simon Hunt434cf142014-11-24 11:10:28 -08002095 showHostVis(node);
Simon Hunt7cd48f32014-11-09 23:42:50 -08002096
Simon Hunt62c47542014-11-22 22:16:32 -08002097 node.append('circle').attr('r', r);
Simon Hunteb1514d2014-11-20 09:57:29 -08002098 if (iid) {
2099 addHostIcon(node, r, iid);
Simon Hunt7fa116d2014-11-17 14:16:55 -08002100 }
Simon Hunt56d51852014-11-09 13:03:35 -08002101 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08002102 .text(hostLabel)
Thomas Vachuska89543292014-11-19 11:28:33 -08002103 .attr('dy', textDy)
Simon Hunt7cd48f32014-11-09 23:42:50 -08002104 .attr('text-anchor', 'middle');
Simon Hunt56d51852014-11-09 13:03:35 -08002105 });
Simon Huntc7ee0662014-11-05 16:44:37 -08002106
Simon Hunt99c13842014-11-06 18:23:12 -08002107 // operate on both existing and new nodes, if necessary
Simon Huntb0ecfa52014-11-23 21:05:12 -08002108 updateDeviceColors();
Simon Huntc7ee0662014-11-05 16:44:37 -08002109
Simon Hunt99c13842014-11-06 18:23:12 -08002110 // operate on exiting nodes:
Simon Huntea80eb42014-11-11 13:46:57 -08002111 // Note that the node is removed after 2 seconds.
2112 // Sub element animations should be shorter than 2 seconds.
2113 var exiting = node.exit()
Simon Hunt44031102014-11-11 13:20:36 -08002114 .transition()
2115 .duration(2000)
Simon Huntea80eb42014-11-11 13:46:57 -08002116 .style('opacity', 0)
Simon Hunt99c13842014-11-06 18:23:12 -08002117 .remove();
Simon Huntea80eb42014-11-11 13:46:57 -08002118
2119 // host node exits....
2120 exiting.filter('.host').each(function (d) {
Simon Huntca867ac2014-11-28 18:07:35 -08002121 var node = d.el;
2122 node.select('use')
2123 .style('opacity', 0.5)
2124 .transition()
2125 .duration(800)
2126 .style('opacity', 0);
Simon Huntea80eb42014-11-11 13:46:57 -08002127
2128 node.select('text')
2129 .style('opacity', 0.5)
2130 .transition()
Simon Huntca867ac2014-11-28 18:07:35 -08002131 .duration(800)
Simon Huntea80eb42014-11-11 13:46:57 -08002132 .style('opacity', 0);
Simon Huntea80eb42014-11-11 13:46:57 -08002133
Thomas Vachuska89543292014-11-19 11:28:33 -08002134 node.select('circle')
2135 .style('stroke-fill', '#555')
2136 .style('fill', '#888')
Simon Huntea80eb42014-11-11 13:46:57 -08002137 .style('opacity', 0.5)
2138 .transition()
2139 .duration(1500)
2140 .attr('r', 0);
Simon Huntea80eb42014-11-11 13:46:57 -08002141 });
2142
Simon Huntca867ac2014-11-28 18:07:35 -08002143 // device node exits....
2144 exiting.filter('.device').each(function (d) {
2145 var node = d.el;
2146 node.select('use')
2147 .style('opacity', 0.5)
2148 .transition()
2149 .duration(800)
2150 .style('opacity', 0);
2151
2152 node.selectAll('rect')
2153 .style('stroke-fill', '#555')
2154 .style('fill', '#888')
2155 .style('opacity', 0.5);
2156 });
Simon Huntd6f5a272014-11-29 23:45:50 -08002157 fResume();
Simon Huntc7ee0662014-11-05 16:44:37 -08002158 }
2159
Simon Hunt95dad922014-11-24 09:43:31 -08002160 var dCol = {
2161 black: '#000',
2162 paleblue: '#acf',
2163 offwhite: '#ddd',
2164 midgrey: '#888',
2165 lightgrey: '#bbb',
2166 orange: '#f90'
2167 };
2168
Simon Huntb0ecfa52014-11-23 21:05:12 -08002169 // note: these are the device icon colors without affinity
Simon Hunt95dad922014-11-24 09:43:31 -08002170 var dColTheme = {
Simon Huntb0ecfa52014-11-23 21:05:12 -08002171 light: {
2172 online: {
Simon Hunt95dad922014-11-24 09:43:31 -08002173 glyph: dCol.black,
2174 rect: dCol.paleblue
Simon Huntb0ecfa52014-11-23 21:05:12 -08002175 },
2176 offline: {
Simon Hunt95dad922014-11-24 09:43:31 -08002177 glyph: dCol.midgrey,
2178 rect: dCol.lightgrey
Simon Huntb0ecfa52014-11-23 21:05:12 -08002179 }
2180 },
Simon Hunt95dad922014-11-24 09:43:31 -08002181 // TODO: theme
Simon Huntb0ecfa52014-11-23 21:05:12 -08002182 dark: {
2183 online: {
Simon Hunt95dad922014-11-24 09:43:31 -08002184 glyph: dCol.black,
2185 rect: dCol.paleblue
Simon Huntb0ecfa52014-11-23 21:05:12 -08002186 },
2187 offline: {
Simon Hunt95dad922014-11-24 09:43:31 -08002188 glyph: dCol.midgrey,
2189 rect: dCol.lightgrey
Simon Huntb0ecfa52014-11-23 21:05:12 -08002190 }
2191 }
2192 };
2193
2194 function devBaseColor(d) {
2195 var t = network.view.getTheme(),
2196 o = d.online ? 'online' : 'offline';
Simon Hunt95dad922014-11-24 09:43:31 -08002197 return dColTheme[t][o];
Simon Huntb0ecfa52014-11-23 21:05:12 -08002198 }
2199
2200 function setDeviceColor(d) {
2201 var o = d.online,
2202 s = d.el.classed('selected'),
2203 c = devBaseColor(d),
2204 a = instColor(d.master, o),
2205 g, r,
2206 icon = d.el.select('g.deviceIcon');
2207
2208 if (s) {
2209 g = c.glyph;
Simon Hunt95dad922014-11-24 09:43:31 -08002210 r = dColTheme.sel;
Simon Huntb0ecfa52014-11-23 21:05:12 -08002211 } else if (colorAffinity) {
2212 g = o ? a : c.glyph;
Simon Hunt95dad922014-11-24 09:43:31 -08002213 r = o ? dCol.offwhite : a;
Simon Huntb0ecfa52014-11-23 21:05:12 -08002214 } else {
2215 g = c.glyph;
2216 r = c.rect;
2217 }
2218
2219 icon.select('use')
2220 .style('fill', g);
2221 icon.select('rect')
2222 .style('fill', r);
2223 }
2224
Simon Huntc72967b2014-11-20 09:21:42 -08002225 function addDeviceIcon(node, box, noLabel, iid) {
2226 var cfg = config.icons.device,
2227 dx,
2228 dy,
2229 g;
2230
2231 if (noLabel) {
2232 box = emptyBox();
2233 dx = -cfg.dim/2;
2234 dy = -cfg.dim/2;
2235 } else {
2236 box = adjustRectToFitText(node);
Simon Hunt395a70c2014-11-22 23:17:40 -08002237 dx = box.x + cfg.xoff;
2238 dy = box.y + cfg.yoff;
Simon Huntc72967b2014-11-20 09:21:42 -08002239 }
2240
Simon Hunteb1514d2014-11-20 09:57:29 -08002241 g = node.append('g')
2242 .attr('class', 'glyphIcon deviceIcon')
Simon Huntc72967b2014-11-20 09:21:42 -08002243 .attr('transform', translate(dx, dy));
2244
2245 g.append('rect').attr({
2246 x: 0,
2247 y: 0,
2248 rx: cfg.rx,
2249 width: cfg.dim,
2250 height: cfg.dim
2251 });
2252
2253 g.append('use').attr({
2254 'xlink:href': iid,
2255 width: cfg.dim,
2256 height: cfg.dim
2257 });
2258
Simon Huntc72967b2014-11-20 09:21:42 -08002259 }
2260
Simon Hunt7b403bc2014-11-22 19:01:00 -08002261 function find(key, array, tag) {
Simon Huntca867ac2014-11-28 18:07:35 -08002262 var _tag = tag || 'id',
Simon Hunt7b403bc2014-11-22 19:01:00 -08002263 idx, n, d;
2264 for (idx = 0, n = array.length; idx < n; idx++) {
2265 d = array[idx];
2266 if (d[_tag] === key) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08002267 return idx;
2268 }
2269 }
2270 return -1;
2271 }
2272
Simon Huntca867ac2014-11-28 18:07:35 -08002273 function removeLinkElement(d) {
2274 var idx = find(d.key, network.links, 'key'),
Simon Hunt8257f4c2014-11-16 19:34:54 -08002275 removed;
2276 if (idx >=0) {
2277 // remove from links array
2278 removed = network.links.splice(idx, 1);
2279 // remove from lookup cache
2280 delete network.lookup[removed[0].key];
2281 updateLinks();
Simon Huntd6f5a272014-11-29 23:45:50 -08002282 fResume();
Simon Hunt8257f4c2014-11-16 19:34:54 -08002283 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08002284 }
Simon Huntc7ee0662014-11-05 16:44:37 -08002285
Simon Huntca867ac2014-11-28 18:07:35 -08002286 function removeHostElement(d, upd) {
2287 var lu = network.lookup;
Simon Hunt44031102014-11-11 13:20:36 -08002288 // first, remove associated hostLink...
Simon Huntca867ac2014-11-28 18:07:35 -08002289 removeLinkElement(d.linkData);
2290
2291 // remove hostLink bindings
2292 delete lu[d.ingress];
2293 delete lu[d.egress];
Simon Hunt44031102014-11-11 13:20:36 -08002294
2295 // remove from lookup cache
Simon Huntca867ac2014-11-28 18:07:35 -08002296 delete lu[d.id];
Simon Hunt44031102014-11-11 13:20:36 -08002297 // remove from nodes array
Simon Huntca867ac2014-11-28 18:07:35 -08002298 var idx = find(d.id, network.nodes);
2299 network.nodes.splice(idx, 1);
2300 // remove from SVG
2301 // NOTE: upd is false if we were called from removeDeviceElement()
2302 if (upd) {
2303 updateNodes();
Simon Huntd6f5a272014-11-29 23:45:50 -08002304 fResume();
Simon Huntca867ac2014-11-28 18:07:35 -08002305 }
2306 }
2307
2308
2309 function removeDeviceElement(d) {
2310 var id = d.id;
2311 // first, remove associated hosts and links..
2312 findAttachedHosts(id).forEach(removeHostElement);
2313 findAttachedLinks(id).forEach(removeLinkElement);
2314
2315 // remove from lookup cache
2316 delete network.lookup[id];
2317 // remove from nodes array
2318 var idx = find(id, network.nodes);
Simon Hunt44031102014-11-11 13:20:36 -08002319 network.nodes.splice(idx, 1);
2320 // remove from SVG
2321 updateNodes();
Simon Huntd6f5a272014-11-29 23:45:50 -08002322 fResume();
Simon Hunt44031102014-11-11 13:20:36 -08002323 }
2324
Simon Huntca867ac2014-11-28 18:07:35 -08002325 function findAttachedHosts(devId) {
2326 var hosts = [];
2327 network.nodes.forEach(function (d) {
2328 if (d.class === 'host' && d.cp.device === devId) {
2329 hosts.push(d);
2330 }
2331 });
2332 return hosts;
2333 }
2334
2335 function findAttachedLinks(devId) {
2336 var links = [];
2337 network.links.forEach(function (d) {
2338 if (d.source.id === devId || d.target.id === devId) {
2339 links.push(d);
2340 }
2341 });
2342 return links;
2343 }
Simon Hunt44031102014-11-11 13:20:36 -08002344
Simon Huntd6f5a272014-11-29 23:45:50 -08002345 function fResume() {
2346 if (!oblique) {
2347 network.force.resume();
2348 }
2349 }
Simon Huntc7ee0662014-11-05 16:44:37 -08002350
Simon Huntd6f5a272014-11-29 23:45:50 -08002351 function fStart() {
2352 if (!oblique) {
2353 network.force.start();
2354 }
2355 }
2356
2357 var tickStuff = {
2358 nodeAttr: {
2359 transform: function (d) { return translate(d.x, d.y); }
2360 },
2361 linkAttr: {
Simon Huntc7ee0662014-11-05 16:44:37 -08002362 x1: function (d) { return d.source.x; },
2363 y1: function (d) { return d.source.y; },
2364 x2: function (d) { return d.target.x; },
2365 y2: function (d) { return d.target.y; }
Simon Huntd6f5a272014-11-29 23:45:50 -08002366 },
2367 linkLabelAttr: {
2368 transform: function (d) {
2369 var lnk = findLinkById(d.key);
Simon Hunte2575b62014-11-18 15:25:53 -08002370
Simon Huntd6f5a272014-11-29 23:45:50 -08002371 if (lnk) {
2372 var parms = {
2373 x1: lnk.source.x,
2374 y1: lnk.source.y,
2375 x2: lnk.target.x,
2376 y2: lnk.target.y
2377 };
2378 return transformLabel(parms);
2379 }
Thomas Vachuska4731f122014-11-20 04:56:19 -08002380 }
Simon Huntd6f5a272014-11-29 23:45:50 -08002381 }
2382 };
2383
2384 function tick() {
2385 node.attr(tickStuff.nodeAttr);
2386 link.attr(tickStuff.linkAttr);
2387 linkLabel.attr(tickStuff.linkLabelAttr);
Simon Huntc7ee0662014-11-05 16:44:37 -08002388 }
Simon Hunt934c3ce2014-11-05 11:45:07 -08002389
2390 // ==============================
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002391 // Web-Socket for live data
2392
Thomas Vachuska12dfdc32014-11-29 16:03:12 -08002393 function findGuiSuccessor() {
2394 var idx = -1;
2395 onosOrder.forEach(function (d, i) {
2396 if (d.uiAttached) {
2397 idx = i;
2398 }
2399 });
2400
2401 for (var i = 0; i < onosOrder.length - 1; i++) {
2402 var ni = (idx + 1 + i) % onosOrder.length;
2403 if (onosOrder[ni].online) {
2404 return onosOrder[ni].ip;
2405 }
2406 }
2407 return null;
2408 }
2409
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002410 function webSockUrl() {
Thomas Vachuska12dfdc32014-11-29 16:03:12 -08002411 var url = document.location.toString()
2412 .replace(/\#.*/, '')
2413 .replace('http://', 'ws://')
2414 .replace('https://', 'wss://')
2415 .replace('index.html', config.webSockUrl);
2416 if (guiSuccessor) {
2417 url = url.replace(location.hostname, guiSuccessor);
2418 }
2419 return url;
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002420 }
2421
2422 webSock = {
2423 ws : null,
Thomas Vachuska12dfdc32014-11-29 16:03:12 -08002424 retries: 0,
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002425
2426 connect : function() {
2427 webSock.ws = new WebSocket(webSockUrl());
2428
2429 webSock.ws.onopen = function() {
Simon Hunt0c6d4192014-11-12 12:07:10 -08002430 noWebSock(false);
Thomas Vachuska47635c62014-11-22 01:21:36 -08002431 requestSummary();
Simon Huntb0ecfa52014-11-23 21:05:12 -08002432 showInstances();
Thomas Vachuska12dfdc32014-11-29 16:03:12 -08002433 webSock.retries = 0;
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002434 };
2435
2436 webSock.ws.onmessage = function(m) {
2437 if (m.data) {
Simon Huntbb282f52014-11-10 11:08:19 -08002438 wsTraceRx(m.data);
Thomas Vachuskad472c6e2014-11-07 19:11:05 -08002439 handleServerEvent(JSON.parse(m.data));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002440 }
2441 };
2442
2443 webSock.ws.onclose = function(m) {
2444 webSock.ws = null;
Thomas Vachuska12dfdc32014-11-29 16:03:12 -08002445 guiSuccessor = findGuiSuccessor();
2446 if (guiSuccessor && webSock.retries < onosOrder.length) {
2447 webSock.retries++;
2448 webSock.connect();
2449 } else {
2450 noWebSock(true);
2451 }
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002452 };
2453 },
2454
2455 send : function(text) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002456 if (text != null) {
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002457 webSock._send(text);
2458 }
2459 },
2460
2461 _send : function(message) {
2462 if (webSock.ws) {
2463 webSock.ws.send(message);
Simon Hunta255a2c2014-11-13 22:29:35 -08002464 } else if (config.useLiveData) {
Simon Hunt434cf142014-11-24 11:10:28 -08002465 console.warn('no web socket open', message);
Simon Hunta255a2c2014-11-13 22:29:35 -08002466 } else {
Simon Hunt434cf142014-11-24 11:10:28 -08002467 console.log('WS Send: ', message);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002468 }
2469 }
2470
2471 };
2472
Simon Hunt0c6d4192014-11-12 12:07:10 -08002473 function noWebSock(b) {
2474 mask.style('display',b ? 'block' : 'none');
2475 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002476
2477 function sendMessage(evType, payload) {
Simon Huntc1cc81c2014-11-29 14:59:01 -08002478 var p = payload || {},
2479 toSend = {
Simon Huntbb282f52014-11-10 11:08:19 -08002480 event: evType,
2481 sid: ++sid,
Simon Huntc1cc81c2014-11-29 14:59:01 -08002482 payload: p
Simon Huntbb282f52014-11-10 11:08:19 -08002483 },
2484 asText = JSON.stringify(toSend);
2485 wsTraceTx(asText);
2486 webSock.send(asText);
Simon Huntc76ae892014-11-18 17:31:51 -08002487
2488 // Temporary measure for debugging UI behavior ...
2489 if (!config.useLiveData) {
2490 handleTestSend(toSend);
2491 }
Simon Huntbb282f52014-11-10 11:08:19 -08002492 }
2493
2494 function wsTraceTx(msg) {
2495 wsTrace('tx', msg);
2496 }
2497 function wsTraceRx(msg) {
2498 wsTrace('rx', msg);
2499 }
2500 function wsTrace(rxtx, msg) {
Simon Huntbb282f52014-11-10 11:08:19 -08002501 console.log('[' + rxtx + '] ' + msg);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002502 }
2503
Simon Huntc76ae892014-11-18 17:31:51 -08002504 // NOTE: Temporary hardcoded example for showing detail pane
2505 // while we fine-
2506 // Probably should not merge this change...
2507 function handleTestSend(msg) {
2508 if (msg.event === 'requestDetails') {
2509 showDetails({
2510 event: 'showDetails',
2511 sid: 1001,
2512 payload: {
2513 "id": "of:0000ffffffffff09",
2514 "type": "roadm",
2515 "propOrder": [
2516 "Name",
2517 "Vendor",
2518 "H/W Version",
2519 "S/W Version",
2520 "-",
2521 "Latitude",
2522 "Longitude",
2523 "Ports"
2524 ],
2525 "props": {
2526 "Name": null,
2527 "Vendor": "Linc",
2528 "H/W Version": "OE",
2529 "S/W Version": "?",
2530 "-": "",
2531 "Latitude": "40.8",
2532 "Longitude": "73.1",
2533 "Ports": "2"
2534 }
2535 }
2536 });
2537 }
2538 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002539
2540 // ==============================
2541 // Selection stuff
2542
2543 function selectObject(obj, el) {
2544 var n,
Simon Hunt01095ff2014-11-13 16:37:29 -08002545 srcEv = d3.event.sourceEvent,
2546 meta = srcEv.metaKey,
2547 shift = srcEv.shiftKey;
2548
Simon Hunt3c5ca542014-11-29 14:11:43 -08002549 // if the meta key is pressed, we are panning/zooming, so ignore
2550 if (meta) {
Simon Hunt01095ff2014-11-13 16:37:29 -08002551 return;
2552 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002553
2554 if (el) {
2555 n = d3.select(el);
2556 } else {
2557 node.each(function(d) {
2558 if (d == obj) {
2559 n = d3.select(el = this);
2560 }
2561 });
2562 }
2563 if (!n) return;
2564
Simon Hunt01095ff2014-11-13 16:37:29 -08002565 if (shift && n.classed('selected')) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002566 deselectObject(obj.id);
Simon Hunt61d04042014-11-11 17:27:16 -08002567 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002568 return;
2569 }
2570
Simon Hunt01095ff2014-11-13 16:37:29 -08002571 if (!shift) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002572 deselectAll();
2573 }
2574
Simon Huntc31d5692014-11-12 13:27:18 -08002575 selections[obj.id] = { obj: obj, el: el };
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002576 selectOrder.push(obj.id);
2577
2578 n.classed('selected', true);
Simon Huntb0ecfa52014-11-23 21:05:12 -08002579 updateDeviceColors(obj);
Simon Hunt61d04042014-11-11 17:27:16 -08002580 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002581 }
2582
2583 function deselectObject(id) {
Simon Huntc31d5692014-11-12 13:27:18 -08002584 var obj = selections[id],
2585 idx;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002586 if (obj) {
2587 d3.select(obj.el).classed('selected', false);
Simon Hunt61d04042014-11-11 17:27:16 -08002588 delete selections[id];
Simon Huntc31d5692014-11-12 13:27:18 -08002589 idx = $.inArray(id, selectOrder);
2590 if (idx >= 0) {
2591 selectOrder.splice(idx, 1);
2592 }
Simon Huntb0ecfa52014-11-23 21:05:12 -08002593 updateDeviceColors(obj.obj);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002594 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002595 }
2596
2597 function deselectAll() {
2598 // deselect all nodes in the network...
2599 node.classed('selected', false);
2600 selections = {};
2601 selectOrder = [];
Simon Huntb0ecfa52014-11-23 21:05:12 -08002602 updateDeviceColors();
Simon Hunt61d04042014-11-11 17:27:16 -08002603 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002604 }
2605
Simon Huntb0ecfa52014-11-23 21:05:12 -08002606 function updateDeviceColors(d) {
2607 if (d) {
2608 setDeviceColor(d);
2609 } else {
2610 node.filter('.device').each(function (d) {
2611 setDeviceColor(d);
2612 });
2613 }
Thomas Vachuska47635c62014-11-22 01:21:36 -08002614 }
2615
Simon Hunt61d04042014-11-11 17:27:16 -08002616 // update the state of the detail pane, based on current selections
2617 function updateDetailPane() {
2618 var nSel = selectOrder.length;
2619 if (!nSel) {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002620 emptySelect();
Simon Hunt61d04042014-11-11 17:27:16 -08002621 } else if (nSel === 1) {
2622 singleSelect();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002623 requestTrafficForMode();
Simon Hunt61d04042014-11-11 17:27:16 -08002624 } else {
2625 multiSelect();
2626 }
2627 }
2628
Thomas Vachuska9edca302014-11-22 17:06:42 -08002629 function emptySelect() {
Simon Hunt27d322d2014-11-28 10:45:43 -08002630 haveDetails = false;
Simon Hunt06811b72014-11-25 18:54:48 -08002631 hideDetailPane();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002632 cancelTraffic();
2633 }
2634
Simon Hunt61d04042014-11-11 17:27:16 -08002635 function singleSelect() {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002636 // NOTE: detail is shown from showDetails event callback
Simon Hunt61d04042014-11-11 17:27:16 -08002637 requestDetails();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002638 requestTrafficForMode();
Simon Hunt61d04042014-11-11 17:27:16 -08002639 }
2640
2641 function multiSelect() {
Simon Hunt27d322d2014-11-28 10:45:43 -08002642 haveDetails = true;
Simon Huntb53e0682014-11-12 13:32:01 -08002643 populateMultiSelect();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002644 requestTrafficForMode();
Simon Huntb53e0682014-11-12 13:32:01 -08002645 }
2646
2647 function addSep(tbody) {
2648 var tr = tbody.append('tr');
2649 $('<hr>').appendTo(tr.append('td').attr('colspan', 2));
2650 }
2651
2652 function addProp(tbody, label, value) {
2653 var tr = tbody.append('tr');
2654
2655 tr.append('td')
2656 .attr('class', 'label')
2657 .text(label + ' :');
2658
2659 tr.append('td')
2660 .attr('class', 'value')
2661 .text(value);
2662 }
2663
2664 function populateMultiSelect() {
2665 detailPane.empty();
2666
Simon Hunta3dd9572014-11-20 15:22:41 -08002667 var title = detailPane.append('h3'),
2668 table = detailPane.append('table'),
2669 tbody = table.append('tbody');
Simon Huntb53e0682014-11-12 13:32:01 -08002670
Thomas Vachuska4731f122014-11-20 04:56:19 -08002671 title.text('Selected Nodes');
Simon Huntb53e0682014-11-12 13:32:01 -08002672
2673 selectOrder.forEach(function (d, i) {
2674 addProp(tbody, i+1, d);
2675 });
Simon Huntd72bc702014-11-13 18:38:04 -08002676
2677 addMultiSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08002678 }
2679
Thomas Vachuska47635c62014-11-22 01:21:36 -08002680 // TODO: refactor to consolidate with populateDetails
2681 function populateSummary(data) {
2682 summaryPane.empty();
2683
2684 var svg = summaryPane.append('svg'),
2685 iid = iconGlyphUrl(data);
2686
2687 var title = summaryPane.append('h2'),
2688 table = summaryPane.append('table'),
2689 tbody = table.append('tbody');
2690
2691 appendGlyph(svg, 0, 0, 40, iid);
2692
2693 svg.append('use')
2694 .attr({
2695 class: 'birdBadge',
2696 transform: translate(8,12),
2697 'xlink:href': '#bird',
2698 width: 24,
2699 height: 24,
2700 fill: '#fff'
2701 });
2702
2703 title.text('ONOS Summary');
2704
2705 data.propOrder.forEach(function(p) {
2706 if (p === '-') {
2707 addSep(tbody);
2708 } else {
2709 addProp(tbody, p, data.props[p]);
2710 }
2711 });
2712 }
2713
Simon Hunt61d04042014-11-11 17:27:16 -08002714 function populateDetails(data) {
2715 detailPane.empty();
2716
Simon Hunta6a9fe72014-11-20 11:17:12 -08002717 var svg = detailPane.append('svg'),
2718 iid = iconGlyphUrl(data);
2719
Simon Hunta3dd9572014-11-20 15:22:41 -08002720 var title = detailPane.append('h2'),
2721 table = detailPane.append('table'),
2722 tbody = table.append('tbody');
Simon Hunt61d04042014-11-11 17:27:16 -08002723
Simon Hunta6a9fe72014-11-20 11:17:12 -08002724 appendGlyph(svg, 0, 0, 40, iid);
2725 title.text(data.id);
Simon Hunt61d04042014-11-11 17:27:16 -08002726
2727 data.propOrder.forEach(function(p) {
2728 if (p === '-') {
2729 addSep(tbody);
2730 } else {
2731 addProp(tbody, p, data.props[p]);
2732 }
2733 });
Simon Huntd72bc702014-11-13 18:38:04 -08002734
Thomas Vachuska4731f122014-11-20 04:56:19 -08002735 addSingleSelectActions(data);
Simon Hunt61d04042014-11-11 17:27:16 -08002736 }
2737
Thomas Vachuska4731f122014-11-20 04:56:19 -08002738 function addSingleSelectActions(data) {
Simon Huntd72bc702014-11-13 18:38:04 -08002739 detailPane.append('hr');
2740 // always want to allow 'show traffic'
Thomas Vachuska4731f122014-11-20 04:56:19 -08002741 addAction(detailPane, 'Show Related Traffic', showTrafficAction);
2742
2743 if (data.type === 'switch') {
2744 addAction(detailPane, 'Show Device Flows', showDeviceLinkFlowsAction);
2745 }
Simon Huntd72bc702014-11-13 18:38:04 -08002746 }
2747
2748 function addMultiSelectActions() {
2749 detailPane.append('hr');
2750 // always want to allow 'show traffic'
Thomas Vachuska4731f122014-11-20 04:56:19 -08002751 addAction(detailPane, 'Show Related Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08002752 // if exactly two hosts are selected, also want 'add host intent'
2753 if (nSel() === 2 && allSelectionsClass('host')) {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002754 addAction(detailPane, 'Create Host-to-Host Flow', addHostIntentAction);
2755 } else if (nSel() >= 2 && allSelectionsClass('host')) {
2756 addAction(detailPane, 'Create Multi-Source Flow', addMultiSourceIntentAction);
Simon Huntd72bc702014-11-13 18:38:04 -08002757 }
2758 }
2759
Simon Hunta5e89142014-11-14 07:00:33 -08002760 function addAction(panel, text, cb) {
2761 panel.append('div')
Simon Huntd72bc702014-11-13 18:38:04 -08002762 .classed('actionBtn', true)
2763 .text(text)
2764 .on('click', cb);
2765 }
2766
2767
Simon Hunt3c5ca542014-11-29 14:11:43 -08002768 // === Pan and Zoom behaviors...
2769
2770 function panZoom(translate, scale) {
2771 panZoomContainer.attr('transform',
2772 'translate(' + translate + ')scale(' + scale + ')');
Paul Greysonfcba0e82014-11-13 10:21:16 -08002773 // keep the map lines constant width while zooming
Simon Hunt3c5ca542014-11-29 14:11:43 -08002774 bgImg.style('stroke-width', 2.0 / scale + 'px');
Paul Greysonfcba0e82014-11-13 10:21:16 -08002775 }
2776
Simon Hunt3c5ca542014-11-29 14:11:43 -08002777 function resetPanZoom() {
2778 panZoom([0,0], 1);
2779 zoom.translate([0,0]).scale(1);
Paul Greysonfcba0e82014-11-13 10:21:16 -08002780 }
2781
Simon Hunt3c5ca542014-11-29 14:11:43 -08002782 function setupPanZoom() {
Paul Greysonfcba0e82014-11-13 10:21:16 -08002783 function zoomed() {
Simon Hunt3c5ca542014-11-29 14:11:43 -08002784 // pan zoom active when meta key is pressed...
2785 if (d3.event.sourceEvent.metaKey) {
2786 panZoom(d3.event.translate, d3.event.scale);
Paul Greysonfcba0e82014-11-13 10:21:16 -08002787 }
2788 }
2789
2790 zoom = d3.behavior.zoom()
2791 .translate([0, 0])
2792 .scale(1)
Simon Hunt1b18aa52014-11-29 17:57:55 -08002793 .scaleExtent([0.25, 10])
Paul Greysonfcba0e82014-11-13 10:21:16 -08002794 .on("zoom", zoomed);
2795
2796 svg.call(zoom);
2797 }
2798
Simon Hunt61d04042014-11-11 17:27:16 -08002799 // ==============================
2800 // Test harness code
Simon Hunt56d51852014-11-09 13:03:35 -08002801
2802 function prepareScenario(view, ctx, dbg) {
2803 var sc = scenario,
2804 urlSc = sc.evDir + ctx + sc.evScenario;
2805
2806 if (!ctx) {
2807 view.alert("No scenario specified (null ctx)");
2808 return;
2809 }
2810
2811 sc.view = view;
2812 sc.ctx = ctx;
2813 sc.debug = dbg;
2814 sc.evNumber = 0;
2815
2816 d3.json(urlSc, function(err, data) {
Simon Huntbb282f52014-11-10 11:08:19 -08002817 var p = data && data.params || {},
2818 desc = data && data.description || null,
Simon Huntfc274c92014-11-11 11:05:46 -08002819 intro = data && data.title;
Simon Huntbb282f52014-11-10 11:08:19 -08002820
Simon Hunt56d51852014-11-09 13:03:35 -08002821 if (err) {
2822 view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err);
2823 } else {
2824 sc.params = p;
Simon Huntbb282f52014-11-10 11:08:19 -08002825 if (desc) {
2826 intro += '\n\n ' + desc.join('\n ');
2827 }
2828 view.alert(intro);
Simon Hunt56d51852014-11-09 13:03:35 -08002829 }
2830 });
2831
2832 }
2833
Simon Hunt7fa116d2014-11-17 14:16:55 -08002834 function loadGlyphs(svg) {
2835 var defs = svg.append('defs');
2836 gly.defBird(defs);
Simon Huntc72967b2014-11-20 09:21:42 -08002837 gly.defGlyphs(defs);
Simon Huntb82f6902014-11-22 11:53:15 -08002838 gly.defBadges(defs);
Simon Hunt7fa116d2014-11-17 14:16:55 -08002839 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002840
Simon Hunt395a70c2014-11-22 23:17:40 -08002841 function sendUpdateMeta(d, store) {
2842 var metaUi = {},
2843 ll;
2844
2845 if (store) {
Simon Hunt1b18aa52014-11-29 17:57:55 -08002846 ll = geoMapProj.invert([d.x, d.y]);
Simon Hunt62c47542014-11-22 22:16:32 -08002847 metaUi = {
2848 x: d.x,
2849 y: d.y,
2850 lng: ll[0],
2851 lat: ll[1]
2852 };
Simon Hunt395a70c2014-11-22 23:17:40 -08002853 }
Simon Hunt62c47542014-11-22 22:16:32 -08002854 d.metaUi = metaUi;
2855 sendMessage('updateMeta', {
2856 id: d.id,
2857 'class': d.class,
Simon Huntc1cc81c2014-11-29 14:59:01 -08002858 memento: metaUi
Simon Hunt62c47542014-11-22 22:16:32 -08002859 });
2860 }
2861
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002862 // ==============================
Simon Hunt142d0032014-11-04 20:13:09 -08002863 // View life-cycle callbacks
Simon Hunt195cb382014-11-03 17:50:51 -08002864
Simon Hunta2994cc2014-12-02 14:19:15 -08002865 function init(view, ctx, flags) {
Simon Hunt142d0032014-11-04 20:13:09 -08002866 var w = view.width(),
2867 h = view.height(),
Simon Hunt1b18aa52014-11-29 17:57:55 -08002868 fcfg = config.force;
Simon Hunt195cb382014-11-03 17:50:51 -08002869
Simon Hunt142d0032014-11-04 20:13:09 -08002870 // NOTE: view.$div is a D3 selection of the view's div
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002871 var viewBox = '0 0 ' + config.logicalSize + ' ' + config.logicalSize;
2872 svg = view.$div.append('svg').attr('viewBox', viewBox);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002873 setSize(svg, view);
2874
Simon Hunt6e18fe32014-11-29 13:35:41 -08002875 // load glyphs and filters...
Simon Hunt7fa116d2014-11-17 14:16:55 -08002876 loadGlyphs(svg);
Simon Hunt6e18fe32014-11-29 13:35:41 -08002877 d3u.appendGlow(svg);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002878
Simon Hunt3c5ca542014-11-29 14:11:43 -08002879 panZoomContainer = svg.append('g').attr('id', 'panZoomContainer');
2880 setupPanZoom();
Paul Greysonfcba0e82014-11-13 10:21:16 -08002881
Simon Huntc7ee0662014-11-05 16:44:37 -08002882 // group for the topology
Simon Hunt3c5ca542014-11-29 14:11:43 -08002883 topoG = panZoomContainer.append('g')
Simon Hunt1b18aa52014-11-29 17:57:55 -08002884 .attr('id', 'topo-G');
Simon Huntc7ee0662014-11-05 16:44:37 -08002885
Simon Hunte2575b62014-11-18 15:25:53 -08002886 // subgroups for links, link labels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002887 linkG = topoG.append('g').attr('id', 'links');
Simon Hunte2575b62014-11-18 15:25:53 -08002888 linkLabelG = topoG.append('g').attr('id', 'linkLabels');
Simon Huntc7ee0662014-11-05 16:44:37 -08002889 nodeG = topoG.append('g').attr('id', 'nodes');
2890
Simon Hunte2575b62014-11-18 15:25:53 -08002891 // selection of links, linkLabels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002892 link = linkG.selectAll('.link');
Simon Hunte2575b62014-11-18 15:25:53 -08002893 linkLabel = linkLabelG.selectAll('.linkLabel');
Simon Huntc7ee0662014-11-05 16:44:37 -08002894 node = nodeG.selectAll('.node');
2895
Simon Hunt7cd48f32014-11-09 23:42:50 -08002896 function chrg(d) {
2897 return fcfg.charge[d.class] || -12000;
2898 }
Simon Hunt99c13842014-11-06 18:23:12 -08002899 function ldist(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002900 return fcfg.linkDistance[d.type] || 50;
Simon Hunt99c13842014-11-06 18:23:12 -08002901 }
2902 function lstrg(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002903 // 0.0 - 1.0
2904 return fcfg.linkStrength[d.type] || 1.0;
Simon Hunt99c13842014-11-06 18:23:12 -08002905 }
2906
Simon Hunt1a9eff92014-11-07 11:06:34 -08002907 function selectCb(d, self) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002908 selectObject(d, self);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002909 }
2910
2911 function atDragEnd(d, self) {
Simon Hunt56d51852014-11-09 13:03:35 -08002912 // once we've finished moving, pin the node in position
2913 d.fixed = true;
2914 d3.select(self).classed('fixed', true);
2915 if (config.useLiveData) {
Simon Hunt395a70c2014-11-22 23:17:40 -08002916 sendUpdateMeta(d, true);
Simon Hunta255a2c2014-11-13 22:29:35 -08002917 } else {
2918 console.log('Moving node ' + d.id + ' to [' + d.x + ',' + d.y + ']');
Simon Hunt1a9eff92014-11-07 11:06:34 -08002919 }
2920 }
2921
Simon Hunt6e18fe32014-11-29 13:35:41 -08002922 // predicate that indicates when dragging is active
2923 function dragEnabled() {
2924 // meta key pressed means we are zooming/panning (so disable drag)
Simon Huntc2367d52014-11-29 19:30:23 -08002925 return !nodeLock && !d3.event.sourceEvent.metaKey;
2926 }
2927
2928 // predicate that indicates when clicking is active
2929 function clickEnabled() {
2930 return true;
Simon Hunt6e18fe32014-11-29 13:35:41 -08002931 }
2932
Simon Huntc7ee0662014-11-05 16:44:37 -08002933 // set up the force layout
2934 network.force = d3.layout.force()
Simon Hunt1b18aa52014-11-29 17:57:55 -08002935 .size([w, h])
Simon Huntc7ee0662014-11-05 16:44:37 -08002936 .nodes(network.nodes)
2937 .links(network.links)
Simon Hunt7cd48f32014-11-09 23:42:50 -08002938 .gravity(0.4)
2939 .friction(0.7)
2940 .charge(chrg)
Simon Hunt99c13842014-11-06 18:23:12 -08002941 .linkDistance(ldist)
2942 .linkStrength(lstrg)
Simon Huntc7ee0662014-11-05 16:44:37 -08002943 .on('tick', tick);
Simon Hunt195cb382014-11-03 17:50:51 -08002944
Simon Hunt01095ff2014-11-13 16:37:29 -08002945 network.drag = d3u.createDragBehavior(network.force,
Simon Huntc2367d52014-11-29 19:30:23 -08002946 selectCb, atDragEnd, dragEnabled, clickEnabled);
Simon Hunt6e18fe32014-11-29 13:35:41 -08002947
Simon Hunt0c6d4192014-11-12 12:07:10 -08002948
2949 // create mask layer for when we lose connection to server.
Simon Hunta5e89142014-11-14 07:00:33 -08002950 // TODO: this should be part of the framework
Simon Hunt6e18fe32014-11-29 13:35:41 -08002951
2952 function para(sel, text) {
2953 sel.append('p').text(text);
2954 }
2955
Simon Hunt0c6d4192014-11-12 12:07:10 -08002956 mask = view.$div.append('div').attr('id','topo-mask');
2957 para(mask, 'Oops!');
2958 para(mask, 'Web-socket connection to server closed...');
2959 para(mask, 'Try refreshing the page.');
Simon Hunt12ce12e2014-11-15 21:13:19 -08002960
2961 mask.append('svg')
2962 .attr({
2963 id: 'mask-bird',
2964 width: w,
2965 height: h
2966 })
2967 .append('g')
2968 .attr('transform', birdTranslate(w, h))
2969 .style('opacity', 0.3)
2970 .append('use')
2971 .attr({
2972 'xlink:href': '#bird',
2973 width: config.birdDim,
2974 height: config.birdDim,
2975 fill: '#111'
Thomas Vachuska89543292014-11-19 11:28:33 -08002976 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08002977 }
Simon Hunt195cb382014-11-03 17:50:51 -08002978
Simon Hunt01095ff2014-11-13 16:37:29 -08002979
Simon Hunt56d51852014-11-09 13:03:35 -08002980 function load(view, ctx, flags) {
Simon Huntf67722a2014-11-10 09:32:06 -08002981 // resize, in case the window was resized while we were not loaded
2982 resize(view, ctx, flags);
2983
Simon Hunt99c13842014-11-06 18:23:12 -08002984 // cache the view token, so network topo functions can access it
2985 network.view = view;
Simon Hunt56d51852014-11-09 13:03:35 -08002986 config.useLiveData = !flags.local;
2987
2988 if (!config.useLiveData) {
2989 prepareScenario(view, ctx, flags.debug);
2990 }
Simon Hunt99c13842014-11-06 18:23:12 -08002991
2992 // set our radio buttons and key bindings
Simon Hunt9462e8c2014-11-14 17:28:09 -08002993 layerBtnSet = view.setRadio(layerButtons);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002994 view.setKeys(keyDispatch);
Simon Hunt87514342014-11-24 16:41:27 -08002995 view.setGestures(gestures);
Simon Hunt195cb382014-11-03 17:50:51 -08002996
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002997 // patch in our "button bar" for now
2998 // TODO: implement a more official frameworky way of doing this..
Simon Hunt87514342014-11-24 16:41:27 -08002999 //addButtonBar(view);
Simon Huntf8e5b4e02014-11-13 11:17:57 -08003000
Simon Huntd3b7d512014-11-12 15:48:41 -08003001 // Load map data asynchronously; complete startup after that..
3002 loadGeoJsonData();
Simon Hunta255a2c2014-11-13 22:29:35 -08003003 }
3004
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08003005 function startAntTimer() {
Thomas Vachuska4731f122014-11-20 04:56:19 -08003006 if (!antTimer) {
3007 var pulses = [5, 3, 1.2, 3],
3008 pulse = 0;
3009 antTimer = setInterval(function () {
3010 pulse = pulse + 1;
3011 pulse = pulse === pulses.length ? 0 : pulse;
3012 d3.selectAll('.animated').style('stroke-width', pulses[pulse]);
3013 }, 200);
3014 }
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08003015 }
3016
3017 function stopAntTimer() {
Simon Hunta255a2c2014-11-13 22:29:35 -08003018 if (antTimer) {
3019 clearInterval(antTimer);
3020 antTimer = null;
3021 }
Simon Huntd3b7d512014-11-12 15:48:41 -08003022 }
3023
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08003024 function unload(view, ctx, flags) {
3025 stopAntTimer();
3026 }
3027
Simon Huntd3b7d512014-11-12 15:48:41 -08003028 // TODO: move these to config/state portion of script
Simon Hunta6a9fe72014-11-20 11:17:12 -08003029 var geoJsonUrl = 'json/map/continental_us.json',
Simon Huntd3b7d512014-11-12 15:48:41 -08003030 geoJson;
3031
3032 function loadGeoJsonData() {
3033 d3.json(geoJsonUrl, function (err, data) {
3034 if (err) {
3035 // fall back to USA map background
3036 loadStaticMap();
3037 } else {
3038 geoJson = data;
3039 loadGeoMap();
3040 }
3041
3042 // finally, connect to the server...
3043 if (config.useLiveData) {
3044 webSock.connect();
3045 }
3046 });
3047 }
3048
3049 function showBg() {
Simon Hunt434cf142014-11-24 11:10:28 -08003050 return visVal(config.options.showBackground);
Simon Huntd3b7d512014-11-12 15:48:41 -08003051 }
3052
3053 function loadStaticMap() {
3054 fnTrace('loadStaticMap', config.backgroundUrl);
3055 var w = network.view.width(),
3056 h = network.view.height();
3057
3058 // load the background image
3059 bgImg = svg.insert('svg:image', '#topo-G')
3060 .attr({
3061 id: 'topo-bg',
3062 width: w,
3063 height: h,
3064 'xlink:href': config.backgroundUrl
3065 })
3066 .style({
3067 visibility: showBg()
3068 });
3069 }
3070
Simon Hunt1b18aa52014-11-29 17:57:55 -08003071 function setProjForView(path, topoData) {
3072 var dim = config.logicalSize;
3073
3074 // start with unit scale, no translation..
3075 geoMapProj.scale(1).translate([0, 0]);
3076
3077 // figure out dimensions of map data..
3078 var b = path.bounds(topoData),
3079 x1 = b[0][0],
3080 y1 = b[0][1],
3081 x2 = b[1][0],
3082 y2 = b[1][1],
3083 dx = x2 - x1,
3084 dy = y2 - y1,
3085 x = (x1 + x2) / 2,
3086 y = (y1 + y2) / 2;
3087
3088 // size map to 95% of minimum dimension to fill space..
3089 var s = .95 / Math.min(dx / dim, dy / dim);
3090 var t = [dim / 2 - s * x, dim / 2 - s * y];
3091
3092 // set new scale, translation on the projection..
3093 geoMapProj.scale(s).translate(t);
3094 }
3095
Simon Huntd3b7d512014-11-12 15:48:41 -08003096 function loadGeoMap() {
3097 fnTrace('loadGeoMap', geoJsonUrl);
Simon Huntd3b7d512014-11-12 15:48:41 -08003098
Paul Greyson6cb8ca02014-11-12 18:09:02 -08003099 // extracts the topojson data into geocoordinate-based geometry
3100 var topoData = topojson.feature(geoJson, geoJson.objects.states);
Simon Huntd3b7d512014-11-12 15:48:41 -08003101
Paul Greyson6cb8ca02014-11-12 18:09:02 -08003102 // see: http://bl.ocks.org/mbostock/4707858
Simon Hunt1b18aa52014-11-29 17:57:55 -08003103 geoMapProj = d3.geo.mercator();
3104 var path = d3.geo.path().projection(geoMapProj);
Simon Huntd3b7d512014-11-12 15:48:41 -08003105
Simon Hunt1b18aa52014-11-29 17:57:55 -08003106 setProjForView(path, topoData);
Paul Greyson6cb8ca02014-11-12 18:09:02 -08003107
Simon Hunt3c5ca542014-11-29 14:11:43 -08003108 bgImg = panZoomContainer.insert("g", '#topo-G');
Thomas Vachuska89543292014-11-19 11:28:33 -08003109 bgImg.attr('id', 'map').selectAll('path')
3110 .data(topoData.features)
3111 .enter()
3112 .append('path')
3113 .attr('d', path);
Simon Hunt195cb382014-11-03 17:50:51 -08003114 }
3115
Simon Huntf67722a2014-11-10 09:32:06 -08003116 function resize(view, ctx, flags) {
Simon Hunt12ce12e2014-11-15 21:13:19 -08003117 var w = view.width(),
3118 h = view.height();
3119
Simon Hunt934c3ce2014-11-05 11:45:07 -08003120 setSize(svg, view);
Simon Hunt12ce12e2014-11-15 21:13:19 -08003121
3122 d3.select('#mask-bird').attr({ width: w, height: h})
3123 .select('g').attr('transform', birdTranslate(w, h));
Simon Hunt142d0032014-11-04 20:13:09 -08003124 }
3125
Simon Hunt8f40cce2014-11-23 15:57:30 -08003126 function theme(view, ctx, flags) {
3127 updateInstances();
Simon Huntb0ecfa52014-11-23 21:05:12 -08003128 updateDeviceColors();
Simon Hunt8f40cce2014-11-23 15:57:30 -08003129 }
3130
Simon Hunt12ce12e2014-11-15 21:13:19 -08003131 function birdTranslate(w, h) {
3132 var bdim = config.birdDim;
3133 return 'translate('+((w-bdim)*.4)+','+((h-bdim)*.1)+')';
3134 }
Simon Hunt142d0032014-11-04 20:13:09 -08003135
Simon Hunt06811b72014-11-25 18:54:48 -08003136 function isF(f) { return $.isFunction(f) ? f : null; }
3137 function noop() {}
3138
3139 function augmentDetailPane() {
3140 var dp = detailPane;
3141 dp.ypos = { up: 64, down: 320, current: 320};
3142
3143 dp._move = function (y, cb) {
3144 var endCb = isF(cb) || noop,
3145 yp = dp.ypos;
3146 if (yp.current !== y) {
3147 yp.current = y;
3148 dp.el.transition().duration(300)
3149 .each('end', endCb)
3150 .style('top', yp.current + 'px');
3151 } else {
3152 endCb();
3153 }
3154 };
3155
3156 dp.down = function (cb) { dp._move(dp.ypos.down, cb); };
3157 dp.up = function (cb) { dp._move(dp.ypos.up, cb); };
3158 }
3159
Simon Hunt142d0032014-11-04 20:13:09 -08003160 // ==============================
3161 // View registration
Simon Hunt195cb382014-11-03 17:50:51 -08003162
Simon Hunt25248912014-11-04 11:25:48 -08003163 onos.ui.addView('topo', {
Simon Hunta2994cc2014-12-02 14:19:15 -08003164 init: init,
Simon Hunt142d0032014-11-04 20:13:09 -08003165 load: load,
Simon Hunta255a2c2014-11-13 22:29:35 -08003166 unload: unload,
Simon Hunt8f40cce2014-11-23 15:57:30 -08003167 resize: resize,
3168 theme: theme
Simon Hunt195cb382014-11-03 17:50:51 -08003169 });
3170
Thomas Vachuska47635c62014-11-22 01:21:36 -08003171 summaryPane = onos.ui.addFloatingPanel('topo-summary');
Simon Hunt61d04042014-11-11 17:27:16 -08003172 detailPane = onos.ui.addFloatingPanel('topo-detail');
Simon Hunt06811b72014-11-25 18:54:48 -08003173 augmentDetailPane();
Simon Hunta5e89142014-11-14 07:00:33 -08003174 oiBox = onos.ui.addFloatingPanel('topo-oibox', 'TL');
Simon Huntb82f6902014-11-22 11:53:15 -08003175 oiBox.width(20);
Simon Hunt61d04042014-11-11 17:27:16 -08003176
Simon Hunt195cb382014-11-03 17:50:51 -08003177}(ONOS));