blob: ea0d731156285311ec8f5e2b0f54762a05cbd368 [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 Vachuska164fa5c2014-12-02 21:59:41 -0800148 V: [showRelatedIntentsAction, 'Show all related intents'],
149 N: [showNextIntentAction, 'Show next related intent'],
150 W: [showSelectedIntentTrafficAction, 'Monitor traffic of selected intent'],
151 A: [showAllTrafficAction, 'Monitor all traffic'],
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800152 F: [showDeviceLinkFlowsAction, 'Show device link flows'],
Simon Huntc2367d52014-11-29 19:30:23 -0800153 X: [toggleNodeLock, 'Lock / unlock node positions'],
Simon Huntd6f5a272014-11-29 23:45:50 -0800154 Z: [toggleOblique, 'Toggle oblique view (Experimental)'],
Simon Hunt9462e8c2014-11-14 17:28:09 -0800155 esc: handleEscape
Simon Hunt934c3ce2014-11-05 11:45:07 -0800156 };
Simon Hunt142d0032014-11-04 20:13:09 -0800157
Simon Hunt87514342014-11-24 16:41:27 -0800158 // mouse gestures
159 var gestures = [
160 ['click', 'Select the item and show details'],
161 ['shift-click', 'Toggle selection state'],
162 ['drag', 'Reposition (and pin) device / host'],
163 ['cmd-scroll', 'Zoom in / out'],
164 ['cmd-drag', 'Pan']
165 ];
166
Simon Hunt195cb382014-11-03 17:50:51 -0800167 // state variables
Simon Hunt99c13842014-11-06 18:23:12 -0800168 var network = {
Simon Hunt50128c02014-11-08 13:36:15 -0800169 view: null, // view token reference
Simon Hunt99c13842014-11-06 18:23:12 -0800170 nodes: [],
171 links: [],
Simon Hunt269670f2014-11-17 16:17:43 -0800172 lookup: {},
173 revLinkToKey: {}
Simon Hunt99c13842014-11-06 18:23:12 -0800174 },
Simon Hunt56d51852014-11-09 13:03:35 -0800175 scenario = {
176 evDir: 'json/ev/',
177 evScenario: '/scenario.json',
178 evPrefix: '/ev_',
179 evOnos: '_onos.json',
180 evUi: '_ui.json',
181 ctx: null,
182 params: {},
183 evNumber: 0,
Simon Hunt434cf142014-11-24 11:10:28 -0800184 view: null
Simon Hunt56d51852014-11-09 13:03:35 -0800185 },
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800186 webSock,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800187 sid = 0,
Thomas Vachuska60d72bf2014-11-21 13:02:00 -0800188 deviceLabelCount = 3,
Simon Hunt209155e2014-11-21 12:16:09 -0800189 hostLabelCount = 2,
Simon Hunt56d51852014-11-09 13:03:35 -0800190 deviceLabelIndex = 0,
191 hostLabelIndex = 0,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800192 selections = {},
Simon Hunta5e89142014-11-14 07:00:33 -0800193 selectOrder = [],
Simon Hunt6ac93f32014-11-13 12:17:27 -0800194 hovered = null,
Thomas Vachuska47635c62014-11-22 01:21:36 -0800195 summaryPane,
Simon Hunta5e89142014-11-14 07:00:33 -0800196 detailPane,
Simon Hunta255a2c2014-11-13 22:29:35 -0800197 antTimer = null,
Thomas Vachuska12dfdc32014-11-29 16:03:12 -0800198 guiSuccessor = null,
Simon Hunta5e89142014-11-14 07:00:33 -0800199 onosInstances = {},
200 onosOrder = [],
201 oiBox,
Simon Hunt9462e8c2014-11-14 17:28:09 -0800202 oiShowMaster = false,
Simon Hunt8f40cce2014-11-23 15:57:30 -0800203 portLabelsOn = false,
Simon Huntb0ecfa52014-11-23 21:05:12 -0800204 cat7 = d3u.cat7(),
Simon Hunt434cf142014-11-24 11:10:28 -0800205 colorAffinity = false,
Simon Hunt27d322d2014-11-28 10:45:43 -0800206 showHosts = false,
Simon Hunt6d9bd032014-11-28 22:16:40 -0800207 showOffline = true,
Simon Hunt27d322d2014-11-28 10:45:43 -0800208 useDetails = true,
Simon Hunt6e18fe32014-11-29 13:35:41 -0800209 haveDetails = false,
Simon Huntc2367d52014-11-29 19:30:23 -0800210 nodeLock = false,
211 oblique = false;
Simon Hunt195cb382014-11-03 17:50:51 -0800212
Simon Hunt434cf142014-11-24 11:10:28 -0800213 // constants
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800214 var hoverModeNone = 0,
215 hoverModeAll = 1,
Thomas Vachuska9edca302014-11-22 17:06:42 -0800216 hoverModeFlows = 2,
217 hoverModeIntents = 3,
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800218 hoverMode = hoverModeNone;
Thomas Vachuska9edca302014-11-22 17:06:42 -0800219
Simon Hunt934c3ce2014-11-05 11:45:07 -0800220 // D3 selections
221 var svg,
Simon Hunt3c5ca542014-11-29 14:11:43 -0800222 panZoomContainer,
Simon Hunt934c3ce2014-11-05 11:45:07 -0800223 bgImg,
Simon Huntc7ee0662014-11-05 16:44:37 -0800224 topoG,
225 nodeG,
226 linkG,
Simon Hunte2575b62014-11-18 15:25:53 -0800227 linkLabelG,
Simon Huntc7ee0662014-11-05 16:44:37 -0800228 node,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800229 link,
Simon Hunte2575b62014-11-18 15:25:53 -0800230 linkLabel,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800231 mask;
Simon Hunt195cb382014-11-03 17:50:51 -0800232
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800233 // the projection for the map background
Simon Hunt1b18aa52014-11-29 17:57:55 -0800234 var geoMapProj;
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800235
Paul Greysonfcba0e82014-11-13 10:21:16 -0800236 // the zoom function
237 var zoom;
238
Simon Hunt142d0032014-11-04 20:13:09 -0800239 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800240 // For Debugging / Development
Simon Hunt195cb382014-11-03 17:50:51 -0800241
Simon Hunt99c13842014-11-06 18:23:12 -0800242 function note(label, msg) {
243 console.log('NOTE: ' + label + ': ' + msg);
Simon Hunt195cb382014-11-03 17:50:51 -0800244 }
245
Simon Hunt99c13842014-11-06 18:23:12 -0800246 function debug(what) {
247 return config.debugOn && config.debug[what];
Simon Hunt934c3ce2014-11-05 11:45:07 -0800248 }
249
Simon Huntfc274c92014-11-11 11:05:46 -0800250 function fnTrace(msg, id) {
251 if (config.fnTrace) {
252 console.log('FN: ' + msg + ' [' + id + ']');
253 }
254 }
Simon Hunt99c13842014-11-06 18:23:12 -0800255
Simon Hunta5e89142014-11-14 07:00:33 -0800256 function evTrace(data) {
257 fnTrace(data.event, data.payload.id);
258 }
259
Simon Hunt934c3ce2014-11-05 11:45:07 -0800260 // ==============================
261 // Key Callbacks
262
Simon Hunt27d322d2014-11-28 10:45:43 -0800263 function flash(txt) {
264 network.view.flash(txt);
265 }
266
Simon Hunt99c13842014-11-06 18:23:12 -0800267 function testMe(view) {
Simon Hunt8f40cce2014-11-23 15:57:30 -0800268 //view.alert('Theme is ' + view.getTheme());
Simon Hunta3dd9572014-11-20 15:22:41 -0800269 //view.flash('This is some text');
Simon Hunt8f40cce2014-11-23 15:57:30 -0800270 cat7.testCard(svg);
Simon Hunt99c13842014-11-06 18:23:12 -0800271 }
272
Simon Hunt56d51852014-11-09 13:03:35 -0800273 function injectTestEvent(view) {
Simon Hunt434cf142014-11-24 11:10:28 -0800274 if (config.useLiveData) { return; }
275
Simon Hunt56d51852014-11-09 13:03:35 -0800276 var sc = scenario,
277 evn = ++sc.evNumber,
278 pfx = sc.evDir + sc.ctx + sc.evPrefix + evn,
279 onosUrl = pfx + sc.evOnos,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800280 uiUrl = pfx + sc.evUi,
281 stack = [
282 { url: onosUrl, cb: handleServerEvent },
283 { url: uiUrl, cb: handleUiEvent }
284 ];
285 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800286 }
287
Simon Hunt7cd48f32014-11-09 23:42:50 -0800288 function recurseFetchEvent(stack, evn) {
289 var v = scenario.view,
290 frame;
291 if (stack.length === 0) {
Simon Huntfc274c92014-11-11 11:05:46 -0800292 v.alert('Oops!\n\nNo event #' + evn + ' found.');
Simon Hunt7cd48f32014-11-09 23:42:50 -0800293 return;
294 }
295 frame = stack.shift();
296
297 d3.json(frame.url, function (err, data) {
Simon Hunt99c13842014-11-06 18:23:12 -0800298 if (err) {
Simon Hunt56d51852014-11-09 13:03:35 -0800299 if (err.status === 404) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800300 // if we didn't find the data, try the next stack frame
301 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800302 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800303 v.alert('non-404 error:\n\n' + frame.url + '\n\n' + err);
Simon Hunt56d51852014-11-09 13:03:35 -0800304 }
Simon Hunt99c13842014-11-06 18:23:12 -0800305 } else {
Simon Hunt1712ed82014-11-17 12:56:00 -0800306 wsTrace('test', JSON.stringify(data));
Simon Hunt7cd48f32014-11-09 23:42:50 -0800307 frame.cb(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800308 }
309 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800310
Simon Hunt56d51852014-11-09 13:03:35 -0800311 }
Simon Hunt50128c02014-11-08 13:36:15 -0800312
Simon Hunt56d51852014-11-09 13:03:35 -0800313 function handleUiEvent(data) {
Simon Huntbb282f52014-11-10 11:08:19 -0800314 scenario.view.alert('UI Tx: ' + data.event + '\n\n' +
315 JSON.stringify(data));
Simon Hunt56d51852014-11-09 13:03:35 -0800316 }
317
318 function injectStartupEvents(view) {
319 var last = scenario.params.lastAuto || 0;
Simon Hunt434cf142014-11-24 11:10:28 -0800320 if (config.useLiveData) { return; }
Simon Hunt56d51852014-11-09 13:03:35 -0800321
322 while (scenario.evNumber < last) {
Simon Hunt1a9eff92014-11-07 11:06:34 -0800323 injectTestEvent(view);
324 }
325 }
326
Simon Hunt934c3ce2014-11-05 11:45:07 -0800327 function toggleBg() {
328 var vis = bgImg.style('visibility');
Simon Hunt434cf142014-11-24 11:10:28 -0800329 bgImg.style('visibility', visVal(vis === 'hidden'));
330 }
331
Simon Huntd6f5a272014-11-29 23:45:50 -0800332 function opacifyBg(b) {
333 bgImg.transition()
334 .duration(1000)
335 .attr('opacity', b ? 1 : 0);
336 }
337
Simon Huntc2367d52014-11-29 19:30:23 -0800338 function toggleNodeLock() {
339 nodeLock = !nodeLock;
340 flash('Node positions ' + (nodeLock ? 'locked' : 'unlocked'))
341 }
342
343 function toggleOblique() {
344 oblique = !oblique;
Simon Huntd6f5a272014-11-29 23:45:50 -0800345 if (oblique) {
346 network.force.stop();
347 toObliqueView();
348 } else {
349 toNormalView();
350 }
Simon Huntc2367d52014-11-29 19:30:23 -0800351 }
352
Simon Hunt434cf142014-11-24 11:10:28 -0800353 function toggleHosts() {
354 showHosts = !showHosts;
355 updateHostVisibility();
Simon Hunt27d322d2014-11-28 10:45:43 -0800356 flash('Hosts ' + visVal(showHosts));
Simon Hunt934c3ce2014-11-05 11:45:07 -0800357 }
358
Simon Hunt6d9bd032014-11-28 22:16:40 -0800359 function toggleOffline() {
360 showOffline = !showOffline;
361 updateOfflineVisibility();
362 flash('Offline devices ' + visVal(showOffline));
363 }
364
Simon Hunt99c13842014-11-06 18:23:12 -0800365 function cycleLabels() {
Thomas Vachuska60d72bf2014-11-21 13:02:00 -0800366 deviceLabelIndex = (deviceLabelIndex === 2)
Simon Huntbb282f52014-11-10 11:08:19 -0800367 ? 0 : deviceLabelIndex + 1;
Simon Hunt5f36d342014-11-08 21:33:14 -0800368
Simon Hunt99c13842014-11-06 18:23:12 -0800369 network.nodes.forEach(function (d) {
Simon Huntbb282f52014-11-10 11:08:19 -0800370 if (d.class === 'device') {
371 updateDeviceLabel(d);
372 }
Simon Hunt99c13842014-11-06 18:23:12 -0800373 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800374 }
375
376 function togglePorts(view) {
Simon Hunt434cf142014-11-24 11:10:28 -0800377 //view.alert('togglePorts() callback')
Simon Hunt934c3ce2014-11-05 11:45:07 -0800378 }
379
Simon Hunt6ac93f32014-11-13 12:17:27 -0800380 function unpin() {
381 if (hovered) {
Simon Hunt395a70c2014-11-22 23:17:40 -0800382 sendUpdateMeta(hovered);
Simon Hunt6ac93f32014-11-13 12:17:27 -0800383 hovered.fixed = false;
384 hovered.el.classed('fixed', false);
Simon Huntd6f5a272014-11-29 23:45:50 -0800385 fResume();
Simon Hunt6ac93f32014-11-13 12:17:27 -0800386 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800387 }
388
Simon Hunt9462e8c2014-11-14 17:28:09 -0800389 function handleEscape(view) {
390 if (oiShowMaster) {
391 cancelAffinity();
Simon Hunt27d322d2014-11-28 10:45:43 -0800392 } else if (haveDetails) {
Simon Hunt9462e8c2014-11-14 17:28:09 -0800393 deselectAll();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800394 } else if (oiBox.isVisible()) {
Simon Huntb0ecfa52014-11-23 21:05:12 -0800395 hideInstances();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800396 } else if (summaryPane.isVisible()) {
397 cancelSummary();
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800398 stopAntTimer();
399 } else {
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800400 hoverMode = hoverModeNone;
Simon Hunt9462e8c2014-11-14 17:28:09 -0800401 }
402 }
403
Simon Hunt934c3ce2014-11-05 11:45:07 -0800404 // ==============================
Simon Huntd6f5a272014-11-29 23:45:50 -0800405 // Oblique view ...
406
407 var obview = {
408 tt: -.7, // x skew y factor
409 xsk: -35, // x skew angle
410 ysc: 0.5, // y scale
411 pad: 50,
412 time: 1500,
413 fill: {
414 pkt: 'rgba(130,130,170,0.3)',
415 opt: 'rgba(170,130,170,0.3)'
416 },
417 id: function (tag) {
418 return 'obview-' + tag + 'Plane';
419 },
420 yt: function (h, dir) {
421 return h * obview.ysc * dir * 1.1;
422 },
423 obXform: function (h, dir) {
424 var yt = obview.yt(h, dir);
425 return scale(1, obview.ysc) + translate(0, yt) + skewX(obview.xsk);
426 },
427 noXform: function () {
428 return skewX(0) + translate(0,0) + scale(1,1);
429 },
430 xffn: null,
431 plane: {}
432 };
433
434
435 function toObliqueView() {
436 var box = nodeG.node().getBBox(),
437 ox, oy;
438
439 padBox(box, obview.pad);
440
441 ox = box.x + box.width / 2;
442 oy = box.y + box.height / 2;
443
444 // remember node lock state, then lock the nodes down
445 obview.nodeLock = nodeLock;
446 nodeLock = true;
447 opacifyBg(false);
448
449 insertPlanes(ox, oy);
450
451 obview.xffn = function (xy, dir) {
452 var yt = obview.yt(box.height, dir),
453 ax = xy.x - ox,
454 ay = xy.y - oy,
455 x = ax + ay * obview.tt,
456 y = ay * obview.ysc + obview.ysc * yt;
457 return {x: ox + x, y: oy + y};
458 };
459
460 showPlane('pkt', box, -1);
461 showPlane('opt', box, 1);
462 obTransitionNodes();
463 }
464
465 function toNormalView() {
466 obview.xffn = null;
467
468 hidePlane('pkt');
469 hidePlane('opt');
470 obTransitionNodes();
471
472 removePlanes();
473
474 // restore node lock state
475 nodeLock = obview.nodeLock;
476 opacifyBg(true);
477 }
478
479 function obTransitionNodes() {
480 var xffn = obview.xffn;
481
482 // return the direction for the node
483 // -1 for pkt layer, 1 for optical layer
484 function dir(d) {
485 return inLayer(d, 'pkt') ? -1 : 1;
486 }
487
488 if (xffn) {
489 network.nodes.forEach(function (d) {
490 var oldxy = {x: d.x, y: d.y},
491 coords = xffn(oldxy, dir(d));
492 d.oldxy = oldxy;
493 d.px = d.x = coords.x;
494 d.py = d.y = coords.y;
495 });
496 } else {
497 network.nodes.forEach(function (d) {
498 var old = d.oldxy || {x: d.x, y: d.y};
499 d.px = d.x = old.x;
500 d.py = d.y = old.y;
501 delete d.oldxy;
502 });
503 }
504
505 node.transition()
506 .duration(obview.time)
507 .attr(tickStuff.nodeAttr);
508 link.transition()
509 .duration(obview.time)
510 .attr(tickStuff.linkAttr);
511 linkLabel.transition()
512 .duration(obview.time)
513 .attr(tickStuff.linkLabelAttr);
514 }
515
516 function showPlane(tag, box, dir) {
517 var g = obview.plane[tag];
518
519 // set box origin at center..
520 box.x = -box.width/2;
521 box.y = -box.height/2;
522
523 g.select('rect')
524 .attr(box)
525 .attr('opacity', 0)
526 .transition()
527 .duration(obview.time)
528 .attr('opacity', 1)
529 .attr('transform', obview.obXform(box.height, dir));
530 }
531
532 function hidePlane(tag) {
533 var g = obview.plane[tag];
534
535 g.select('rect')
536 .transition()
537 .duration(obview.time)
538 .attr('opacity', 0)
539 .attr('transform', obview.noXform());
540 }
541
542 function insertPlanes(ox, oy) {
543 function ins(tag) {
544 var id = obview.id(tag),
545 g = panZoomContainer.insert('g', '#topo-G')
546 .attr('id', id)
547 .attr('transform', translate(ox,oy));
548 g.append('rect')
549 .attr('fill', obview.fill[tag])
550 .attr('opacity', 0);
551 obview.plane[tag] = g;
552 }
553 ins('opt');
554 ins('pkt');
555 }
556
557 function removePlanes() {
558 function rem(tag) {
559 var id = obview.id(tag);
560 panZoomContainer.select('#'+id)
561 .transition()
562 .duration(obview.time + 50)
563 .remove();
564 delete obview.plane[tag];
565 }
566 rem('opt');
567 rem('pkt');
568 }
569
570 function padBox(box, p) {
571 box.x -= p;
572 box.y -= p;
573 box.width += p*2;
574 box.height += p*2;
575 }
576
577 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800578 // Radio Button Callbacks
579
Simon Hunta5e89142014-11-14 07:00:33 -0800580 var layerLookup = {
581 host: {
Simon Hunt209155e2014-11-21 12:16:09 -0800582 endstation: 'pkt', // default, if host event does not define type
Thomas Vachuska89543292014-11-19 11:28:33 -0800583 router: 'pkt',
Simon Hunta5e89142014-11-14 07:00:33 -0800584 bgpSpeaker: 'pkt'
585 },
586 device: {
587 switch: 'pkt',
588 roadm: 'opt'
589 },
590 link: {
591 hostLink: 'pkt',
592 direct: 'pkt',
Simon Hunt8257f4c2014-11-16 19:34:54 -0800593 indirect: '',
594 tunnel: '',
Simon Hunta5e89142014-11-14 07:00:33 -0800595 optical: 'opt'
596 }
597 };
598
599 function inLayer(d, layer) {
Simon Hunt8257f4c2014-11-16 19:34:54 -0800600 var type = d.class === 'link' ? d.type() : d.type,
601 look = layerLookup[d.class],
602 lyr = look && look[type];
Simon Hunta5e89142014-11-14 07:00:33 -0800603 return lyr === layer;
604 }
605
606 function unsuppressLayer(which) {
607 node.each(function (d) {
608 var node = d.el;
609 if (inLayer(d, which)) {
610 node.classed('suppressed', false);
611 }
612 });
613
614 link.each(function (d) {
615 var link = d.el;
616 if (inLayer(d, which)) {
617 link.classed('suppressed', false);
618 }
619 });
620 }
621
Simon Hunt9462e8c2014-11-14 17:28:09 -0800622 function suppressLayers(b) {
623 node.classed('suppressed', b);
624 link.classed('suppressed', b);
Simon Hunt142d0032014-11-04 20:13:09 -0800625// d3.selectAll('svg .port').classed('inactive', false);
626// d3.selectAll('svg .portText').classed('inactive', false);
Simon Hunt195cb382014-11-03 17:50:51 -0800627 }
628
Simon Hunt9462e8c2014-11-14 17:28:09 -0800629 function showAllLayers() {
630 suppressLayers(false);
631 }
632
Simon Hunt195cb382014-11-03 17:50:51 -0800633 function showPacketLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800634 node.classed('suppressed', true);
635 link.classed('suppressed', true);
636 unsuppressLayer('pkt');
Simon Hunt195cb382014-11-03 17:50:51 -0800637 }
638
639 function showOpticalLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800640 node.classed('suppressed', true);
641 link.classed('suppressed', true);
642 unsuppressLayer('opt');
Simon Hunt195cb382014-11-03 17:50:51 -0800643 }
644
Simon Hunt9462e8c2014-11-14 17:28:09 -0800645 function restoreLayerState() {
646 layerBtnDispatch[layerBtnSet.selected()]();
647 }
648
Simon Hunt142d0032014-11-04 20:13:09 -0800649 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800650 // Private functions
651
Simon Hunt99c13842014-11-06 18:23:12 -0800652 function safeId(s) {
653 return s.replace(/[^a-z0-9]/gi, '-');
654 }
655
Simon Huntc7ee0662014-11-05 16:44:37 -0800656 // set the size of the given element to that of the view (reduced if padded)
657 function setSize(el, view, pad) {
658 var padding = pad ? pad * 2 : 0;
Simon Hunt934c3ce2014-11-05 11:45:07 -0800659 el.attr({
Simon Huntc7ee0662014-11-05 16:44:37 -0800660 width: view.width() - padding,
661 height: view.height() - padding
Simon Hunt934c3ce2014-11-05 11:45:07 -0800662 });
663 }
664
Simon Hunt8257f4c2014-11-16 19:34:54 -0800665 function makeNodeKey(d, what) {
666 var port = what + 'Port';
667 return d[what] + '/' + d[port];
668 }
669
670 function makeLinkKey(d, flipped) {
671 var one = flipped ? makeNodeKey(d, 'dst') : makeNodeKey(d, 'src'),
672 two = flipped ? makeNodeKey(d, 'src') : makeNodeKey(d, 'dst');
673 return one + '-' + two;
674 }
675
Simon Hunt269670f2014-11-17 16:17:43 -0800676 function findLinkById(id) {
677 // check to see if this is a reverse lookup, else default to given id
678 var key = network.revLinkToKey[id] || id;
679 return key && network.lookup[key];
680 }
681
Simon Hunt8257f4c2014-11-16 19:34:54 -0800682 function findLink(linkData, op) {
683 var key = makeLinkKey(linkData),
684 keyrev = makeLinkKey(linkData, 1),
685 link = network.lookup[key],
686 linkRev = network.lookup[keyrev],
687 result = {},
688 ldata = link || linkRev,
689 rawLink;
690
691 if (op === 'add') {
692 if (link) {
693 // trying to add a link that we already know about
694 result.ldata = link;
695 result.badLogic = 'addLink: link already added';
696
697 } else if (linkRev) {
698 // we found the reverse of the link to be added
699 result.ldata = linkRev;
700 if (linkRev.fromTarget) {
701 result.badLogic = 'addLink: link already added';
702 }
703 }
704 } else if (op === 'update') {
705 if (!ldata) {
706 result.badLogic = 'updateLink: link not found';
707 } else {
708 rawLink = link ? ldata.fromSource : ldata.fromTarget;
709 result.updateWith = function (data) {
710 $.extend(rawLink, data);
711 restyleLinkElement(ldata);
712 }
713 }
714 } else if (op === 'remove') {
715 if (!ldata) {
716 result.badLogic = 'removeLink: link not found';
717 } else {
718 rawLink = link ? ldata.fromSource : ldata.fromTarget;
719
720 if (!rawLink) {
721 result.badLogic = 'removeLink: link not found';
722
723 } else {
724 result.removeRawLink = function () {
725 if (link) {
726 // remove fromSource
727 ldata.fromSource = null;
728 if (ldata.fromTarget) {
729 // promote target into source position
730 ldata.fromSource = ldata.fromTarget;
731 ldata.fromTarget = null;
732 ldata.key = keyrev;
733 delete network.lookup[key];
734 network.lookup[keyrev] = ldata;
Simon Hunt269670f2014-11-17 16:17:43 -0800735 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800736 }
737 } else {
738 // remove fromTarget
739 ldata.fromTarget = null;
Simon Hunt269670f2014-11-17 16:17:43 -0800740 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800741 }
742 if (ldata.fromSource) {
743 restyleLinkElement(ldata);
744 } else {
745 removeLinkElement(ldata);
746 }
747 }
748 }
749 }
750 }
751 return result;
752 }
753
754 function addLinkUpdate(ldata, link) {
755 // add link event, but we already have the reverse link installed
756 ldata.fromTarget = link;
Simon Hunt269670f2014-11-17 16:17:43 -0800757 network.revLinkToKey[link.id] = ldata.key;
Simon Hunt8257f4c2014-11-16 19:34:54 -0800758 restyleLinkElement(ldata);
759 }
760
761 var allLinkTypes = 'direct indirect optical tunnel',
762 defaultLinkType = 'direct';
763
764 function restyleLinkElement(ldata) {
765 // this fn's job is to look at raw links and decide what svg classes
766 // need to be applied to the line element in the DOM
767 var el = ldata.el,
768 type = ldata.type(),
769 lw = ldata.linkWidth(),
770 online = ldata.online();
771
772 el.classed('link', true);
773 el.classed('inactive', !online);
774 el.classed(allLinkTypes, false);
775 if (type) {
776 el.classed(type, true);
777 }
778 el.transition()
779 .duration(1000)
Thomas Vachuska89543292014-11-19 11:28:33 -0800780 .attr('stroke-width', linkScale(lw))
781 .attr('stroke', config.topo.linkBaseColor);
Simon Hunt8257f4c2014-11-16 19:34:54 -0800782 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800783
Simon Hunt99c13842014-11-06 18:23:12 -0800784 // ==============================
785 // Event handlers for server-pushed events
786
Simon Huntbb282f52014-11-10 11:08:19 -0800787 function logicError(msg) {
788 // TODO, report logic error to server, via websock, so it can be logged
Simon Huntfc274c92014-11-11 11:05:46 -0800789 console.warn(msg);
Simon Huntbb282f52014-11-10 11:08:19 -0800790 }
791
Simon Hunt99c13842014-11-06 18:23:12 -0800792 var eventDispatch = {
Simon Hunta5e89142014-11-14 07:00:33 -0800793 addInstance: addInstance,
Simon Hunt99c13842014-11-06 18:23:12 -0800794 addDevice: addDevice,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800795 addLink: addLink,
Simon Hunt56d51852014-11-09 13:03:35 -0800796 addHost: addHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800797
Simon Huntfcfb46c2014-11-19 12:53:38 -0800798 updateInstance: updateInstance,
Simon Huntbb282f52014-11-10 11:08:19 -0800799 updateDevice: updateDevice,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800800 updateLink: updateLink,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800801 updateHost: updateHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800802
Simon Hunt7b403bc2014-11-22 19:01:00 -0800803 removeInstance: removeInstance,
Simon Huntca867ac2014-11-28 18:07:35 -0800804 removeDevice: removeDevice,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800805 removeLink: removeLink,
Simon Hunt44031102014-11-11 13:20:36 -0800806 removeHost: removeHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800807
Simon Hunt61d04042014-11-11 17:27:16 -0800808 showDetails: showDetails,
Thomas Vachuska47635c62014-11-22 01:21:36 -0800809 showSummary: showSummary,
Simon Huntb53e0682014-11-12 13:32:01 -0800810 showTraffic: showTraffic
Simon Hunt99c13842014-11-06 18:23:12 -0800811 };
812
Simon Hunta5e89142014-11-14 07:00:33 -0800813 function addInstance(data) {
814 evTrace(data);
815 var inst = data.payload,
816 id = inst.id;
817 if (onosInstances[id]) {
Thomas Vachuska12dfdc32014-11-29 16:03:12 -0800818 updateInstance(data);
Simon Hunta5e89142014-11-14 07:00:33 -0800819 return;
820 }
821 onosInstances[id] = inst;
822 onosOrder.push(inst);
823 updateInstances();
824 }
825
Simon Hunt99c13842014-11-06 18:23:12 -0800826 function addDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800827 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800828 var device = data.payload,
Simon Huntca867ac2014-11-28 18:07:35 -0800829 id = device.id,
830 d;
831
832 if (network.lookup[id]) {
Thomas Vachuska12dfdc32014-11-29 16:03:12 -0800833 updateDevice(data);
Simon Huntca867ac2014-11-28 18:07:35 -0800834 return;
835 }
836
837 d = createDeviceNode(device);
838 network.nodes.push(d);
839 network.lookup[id] = d;
Simon Hunt99c13842014-11-06 18:23:12 -0800840 updateNodes();
Simon Huntd6f5a272014-11-29 23:45:50 -0800841 fStart();
Simon Hunt99c13842014-11-06 18:23:12 -0800842 }
843
Simon Hunt99c13842014-11-06 18:23:12 -0800844 function addLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800845 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800846 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800847 result = findLink(link, 'add'),
848 bad = result.badLogic,
Simon Huntca867ac2014-11-28 18:07:35 -0800849 d = result.ldata;
Simon Hunt8257f4c2014-11-16 19:34:54 -0800850
851 if (bad) {
852 logicError(bad + ': ' + link.id);
853 return;
854 }
855
Simon Huntca867ac2014-11-28 18:07:35 -0800856 if (d) {
Simon Hunt8257f4c2014-11-16 19:34:54 -0800857 // we already have a backing store link for src/dst nodes
Simon Huntca867ac2014-11-28 18:07:35 -0800858 addLinkUpdate(d, link);
Simon Hunt8257f4c2014-11-16 19:34:54 -0800859 return;
860 }
861
862 // no backing store link yet
Simon Huntca867ac2014-11-28 18:07:35 -0800863 d = createLink(link);
864 if (d) {
865 network.links.push(d);
866 network.lookup[d.key] = d;
Simon Hunt99c13842014-11-06 18:23:12 -0800867 updateLinks();
Simon Huntd6f5a272014-11-29 23:45:50 -0800868 fStart();
Simon Hunt99c13842014-11-06 18:23:12 -0800869 }
870 }
871
Simon Hunt56d51852014-11-09 13:03:35 -0800872 function addHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800873 evTrace(data);
Simon Hunt56d51852014-11-09 13:03:35 -0800874 var host = data.payload,
Simon Huntca867ac2014-11-28 18:07:35 -0800875 id = host.id,
876 d,
Simon Hunt56d51852014-11-09 13:03:35 -0800877 lnk;
Simon Huntca867ac2014-11-28 18:07:35 -0800878
879 if (network.lookup[id]) {
880 logicError('Host already added: ' + id);
881 return;
882 }
883
884 d = createHostNode(host);
885 network.nodes.push(d);
886 network.lookup[host.id] = d;
Simon Hunt56d51852014-11-09 13:03:35 -0800887 updateNodes();
888
889 lnk = createHostLink(host);
890 if (lnk) {
Simon Huntca867ac2014-11-28 18:07:35 -0800891 d.linkData = lnk; // cache ref on its host
Simon Hunt56d51852014-11-09 13:03:35 -0800892 network.links.push(lnk);
Simon Huntca867ac2014-11-28 18:07:35 -0800893 network.lookup[d.ingress] = lnk;
894 network.lookup[d.egress] = lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800895 updateLinks();
896 }
Simon Huntd6f5a272014-11-29 23:45:50 -0800897 fStart();
Simon Hunt56d51852014-11-09 13:03:35 -0800898 }
899
Simon Hunt44031102014-11-11 13:20:36 -0800900 // TODO: fold updateX(...) methods into one base method; remove duplication
Simon Hunt56a2ea42014-11-19 12:39:31 -0800901
902 function updateInstance(data) {
903 evTrace(data);
904 var inst = data.payload,
905 id = inst.id,
Simon Huntca867ac2014-11-28 18:07:35 -0800906 d = onosInstances[id];
907 if (d) {
908 $.extend(d, inst);
Simon Hunt56a2ea42014-11-19 12:39:31 -0800909 updateInstances();
Simon Hunt56a2ea42014-11-19 12:39:31 -0800910 } else {
911 logicError('updateInstance lookup fail. ID = "' + id + '"');
912 }
913 }
914
Simon Huntbb282f52014-11-10 11:08:19 -0800915 function updateDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800916 evTrace(data);
Simon Huntbb282f52014-11-10 11:08:19 -0800917 var device = data.payload,
918 id = device.id,
Simon Hunt6d9bd032014-11-28 22:16:40 -0800919 d = network.lookup[id],
920 wasOnline;
921
Simon Hunt62c47542014-11-22 22:16:32 -0800922 if (d) {
Simon Hunt6d9bd032014-11-28 22:16:40 -0800923 wasOnline = d.online;
Simon Hunt62c47542014-11-22 22:16:32 -0800924 $.extend(d, device);
925 if (positionNode(d, true)) {
Simon Hunt395a70c2014-11-22 23:17:40 -0800926 sendUpdateMeta(d, true);
Simon Hunt62c47542014-11-22 22:16:32 -0800927 }
928 updateNodes();
Simon Hunt6d9bd032014-11-28 22:16:40 -0800929 if (wasOnline !== d.online) {
930 findAttachedLinks(d.id).forEach(restyleLinkElement);
931 updateOfflineVisibility(d);
932 }
Simon Huntbb282f52014-11-10 11:08:19 -0800933 } else {
934 logicError('updateDevice lookup fail. ID = "' + id + '"');
935 }
936 }
937
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800938 function updateLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800939 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800940 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800941 result = findLink(link, 'update'),
942 bad = result.badLogic;
943 if (bad) {
944 logicError(bad + ': ' + link.id);
945 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800946 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800947 result.updateWith(link);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800948 }
949
Simon Hunt7cd48f32014-11-09 23:42:50 -0800950 function updateHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800951 evTrace(data);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800952 var host = data.payload,
Simon Huntbb282f52014-11-10 11:08:19 -0800953 id = host.id,
Simon Huntca867ac2014-11-28 18:07:35 -0800954 d = network.lookup[id];
955 if (d) {
956 $.extend(d, host);
Simon Hunt6d9bd032014-11-28 22:16:40 -0800957 if (positionNode(d, true)) {
958 sendUpdateMeta(d, true);
959 }
960 updateNodes(d);
Simon Huntbb282f52014-11-10 11:08:19 -0800961 } else {
962 logicError('updateHost lookup fail. ID = "' + id + '"');
963 }
Simon Hunt7cd48f32014-11-09 23:42:50 -0800964 }
965
Simon Hunt44031102014-11-11 13:20:36 -0800966 // TODO: fold removeX(...) methods into base method - remove dup code
Simon Hunt7b403bc2014-11-22 19:01:00 -0800967 function removeInstance(data) {
968 evTrace(data);
969 var inst = data.payload,
970 id = inst.id,
Simon Huntca867ac2014-11-28 18:07:35 -0800971 d = onosInstances[id];
972 if (d) {
973 var idx = find(id, onosOrder);
Simon Hunt7b403bc2014-11-22 19:01:00 -0800974 if (idx >= 0) {
975 onosOrder.splice(idx, 1);
976 }
977 delete onosInstances[id];
978 updateInstances();
979 } else {
980 logicError('updateInstance lookup fail. ID = "' + id + '"');
981 }
982 }
983
Simon Huntca867ac2014-11-28 18:07:35 -0800984 function removeDevice(data) {
985 evTrace(data);
986 var device = data.payload,
987 id = device.id,
988 d = network.lookup[id];
989 if (d) {
990 removeDeviceElement(d);
991 } else {
992 logicError('removeDevice lookup fail. ID = "' + id + '"');
993 }
994 }
995
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800996 function removeLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800997 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800998 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800999 result = findLink(link, 'remove'),
1000 bad = result.badLogic;
1001 if (bad) {
Simon Huntca867ac2014-11-28 18:07:35 -08001002 // may have already removed link, if attached to removed device
1003 console.warn(bad + ': ' + link.id);
Simon Hunt8257f4c2014-11-16 19:34:54 -08001004 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001005 }
Simon Hunt8257f4c2014-11-16 19:34:54 -08001006 result.removeRawLink();
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001007 }
1008
Simon Hunt44031102014-11-11 13:20:36 -08001009 function removeHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -08001010 evTrace(data);
Simon Hunt44031102014-11-11 13:20:36 -08001011 var host = data.payload,
1012 id = host.id,
Simon Huntca867ac2014-11-28 18:07:35 -08001013 d = network.lookup[id];
1014 if (d) {
1015 removeHostElement(d, true);
Simon Hunt44031102014-11-11 13:20:36 -08001016 } else {
Simon Huntca867ac2014-11-28 18:07:35 -08001017 // may have already removed host, if attached to removed device
1018 console.warn('removeHost lookup fail. ID = "' + id + '"');
Simon Hunt44031102014-11-11 13:20:36 -08001019 }
1020 }
1021
Simon Huntca867ac2014-11-28 18:07:35 -08001022 // the following events are server responses to user actions
Thomas Vachuska47635c62014-11-22 01:21:36 -08001023 function showSummary(data) {
1024 evTrace(data);
1025 populateSummary(data.payload);
Simon Hunt06811b72014-11-25 18:54:48 -08001026 showSummaryPane();
Thomas Vachuska47635c62014-11-22 01:21:36 -08001027 }
1028
Simon Hunt61d04042014-11-11 17:27:16 -08001029 function showDetails(data) {
Simon Hunta5e89142014-11-14 07:00:33 -08001030 evTrace(data);
Simon Hunt27d322d2014-11-28 10:45:43 -08001031 haveDetails = true;
Simon Hunt61d04042014-11-11 17:27:16 -08001032 populateDetails(data.payload);
Simon Hunt27d322d2014-11-28 10:45:43 -08001033 if (useDetails) {
1034 showDetailPane();
1035 }
Simon Hunt61d04042014-11-11 17:27:16 -08001036 }
1037
Simon Huntb53e0682014-11-12 13:32:01 -08001038 function showTraffic(data) {
Simon Hunta5e89142014-11-14 07:00:33 -08001039 evTrace(data);
Thomas Vachuska4731f122014-11-20 04:56:19 -08001040 var paths = data.payload.paths,
1041 hasTraffic = false;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -08001042
Thomas Vachuska3266abf2014-11-13 09:28:46 -08001043 // Revert any links hilighted previously.
Thomas Vachuska4731f122014-11-20 04:56:19 -08001044 link.style('stroke-width', null)
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08001045 .classed('primary secondary animated optical', false);
Simon Hunte2575b62014-11-18 15:25:53 -08001046 // Remove all previous labels.
1047 removeLinkLabels();
Thomas Vachuska3266abf2014-11-13 09:28:46 -08001048
Simon Hunte2575b62014-11-18 15:25:53 -08001049 // Now hilight all links in the paths payload, and attach
1050 // labels to them, if they are defined.
Simon Hunta255a2c2014-11-13 22:29:35 -08001051 paths.forEach(function (p) {
Simon Hunte2575b62014-11-18 15:25:53 -08001052 var n = p.links.length,
1053 i,
1054 ldata;
1055
Thomas Vachuska4731f122014-11-20 04:56:19 -08001056 hasTraffic = hasTraffic || p.traffic;
Simon Hunte2575b62014-11-18 15:25:53 -08001057 for (i=0; i<n; i++) {
1058 ldata = findLinkById(p.links[i]);
Thomas Vachuska4731f122014-11-20 04:56:19 -08001059 if (ldata && ldata.el) {
Simon Hunte2575b62014-11-18 15:25:53 -08001060 ldata.el.classed(p.class, true);
1061 ldata.label = p.labels[i];
Thomas Vachuskadea45ff2014-11-12 18:35:46 -08001062 }
Simon Hunte2575b62014-11-18 15:25:53 -08001063 }
Thomas Vachuskadea45ff2014-11-12 18:35:46 -08001064 });
Thomas Vachuska4731f122014-11-20 04:56:19 -08001065
Simon Hunte2575b62014-11-18 15:25:53 -08001066 updateLinks();
Thomas Vachuska4731f122014-11-20 04:56:19 -08001067
1068 if (hasTraffic && !antTimer) {
1069 startAntTimer();
1070 } else if (!hasTraffic && antTimer) {
1071 stopAntTimer();
1072 }
Simon Huntb53e0682014-11-12 13:32:01 -08001073 }
1074
Simon Hunt56d51852014-11-09 13:03:35 -08001075 // ...............................
1076
Simon Hunt99c13842014-11-06 18:23:12 -08001077 function unknownEvent(data) {
Simon Hunt434cf142014-11-24 11:10:28 -08001078 console.warn('Unknown event type: "' + data.event + '"', data);
Simon Hunt99c13842014-11-06 18:23:12 -08001079 }
1080
1081 function handleServerEvent(data) {
1082 var fn = eventDispatch[data.event] || unknownEvent;
1083 fn(data);
1084 }
1085
1086 // ==============================
Simon Hunt61d04042014-11-11 17:27:16 -08001087 // Out-going messages...
1088
Simon Huntb53e0682014-11-12 13:32:01 -08001089 function nSel() {
1090 return selectOrder.length;
1091 }
Simon Hunt61d04042014-11-11 17:27:16 -08001092 function getSel(idx) {
1093 return selections[selectOrder[idx]];
1094 }
Simon Huntb53e0682014-11-12 13:32:01 -08001095 function allSelectionsClass(cls) {
1096 for (var i=0, n=nSel(); i<n; i++) {
1097 if (getSel(i).obj.class !== cls) {
1098 return false;
1099 }
1100 }
1101 return true;
1102 }
Simon Hunt61d04042014-11-11 17:27:16 -08001103
Thomas Vachuska47635c62014-11-22 01:21:36 -08001104 function toggleInstances() {
1105 if (!oiBox.isVisible()) {
Simon Huntb0ecfa52014-11-23 21:05:12 -08001106 showInstances();
Thomas Vachuska47635c62014-11-22 01:21:36 -08001107 } else {
Simon Huntb0ecfa52014-11-23 21:05:12 -08001108 hideInstances();
Thomas Vachuska47635c62014-11-22 01:21:36 -08001109 }
1110 }
1111
Simon Huntb0ecfa52014-11-23 21:05:12 -08001112 function showInstances() {
1113 oiBox.show();
1114 colorAffinity = true;
1115 updateDeviceColors();
1116 }
1117
1118 function hideInstances() {
1119 oiBox.hide();
1120 colorAffinity = false;
1121 cancelAffinity();
1122 updateDeviceColors();
1123 }
1124
Thomas Vachuska1e68bdd2014-11-29 13:53:10 -08001125 function equalizeMasters() {
Thomas Vachuska1e68bdd2014-11-29 13:53:10 -08001126 sendMessage('equalizeMasters');
Simon Huntc1cc81c2014-11-29 14:59:01 -08001127 flash('Equalizing master roles');
Thomas Vachuska1e68bdd2014-11-29 13:53:10 -08001128 }
1129
Thomas Vachuska47635c62014-11-22 01:21:36 -08001130 function toggleSummary() {
1131 if (!summaryPane.isVisible()) {
1132 requestSummary();
1133 } else {
1134 cancelSummary();
1135 }
1136 }
1137
1138 // request overall summary data
1139 function requestSummary() {
Simon Huntc1cc81c2014-11-29 14:59:01 -08001140 sendMessage('requestSummary');
Thomas Vachuska47635c62014-11-22 01:21:36 -08001141 }
1142
1143 function cancelSummary() {
Simon Huntc1cc81c2014-11-29 14:59:01 -08001144 sendMessage('cancelSummary');
Simon Hunt06811b72014-11-25 18:54:48 -08001145 hideSummaryPane();
Thomas Vachuska47635c62014-11-22 01:21:36 -08001146 }
1147
Simon Hunt27d322d2014-11-28 10:45:43 -08001148 function toggleDetails() {
1149 useDetails = !useDetails;
1150 if (useDetails) {
1151 flash('Enable details pane');
1152 if (haveDetails) {
1153 showDetailPane();
1154 }
1155 } else {
1156 flash('Disable details pane');
1157 hideDetailPane();
1158 }
1159 }
1160
Simon Hunt06811b72014-11-25 18:54:48 -08001161 // encapsulate interaction between summary and details panes
1162 function showSummaryPane() {
1163 if (detailPane.isVisible()) {
1164 detailPane.down(summaryPane.show);
1165 } else {
1166 summaryPane.show();
1167 }
1168 }
1169
1170 function hideSummaryPane() {
1171 summaryPane.hide(function () {
1172 if (detailPane.isVisible()) {
1173 detailPane.up();
1174 }
1175 });
1176 }
1177
1178 function showDetailPane() {
1179 if (summaryPane.isVisible()) {
1180 detailPane.down(detailPane.show);
1181 } else {
1182 detailPane.up(detailPane.show);
1183 }
1184 }
1185
1186 function hideDetailPane() {
1187 detailPane.hide();
1188 }
1189
1190
Simon Hunt61d04042014-11-11 17:27:16 -08001191 // request details for the selected element
Simon Huntd72bc702014-11-13 18:38:04 -08001192 // invoked from selection of a single node.
Simon Hunt61d04042014-11-11 17:27:16 -08001193 function requestDetails() {
Simon Huntc1cc81c2014-11-29 14:59:01 -08001194 var data = getSel(0).obj;
1195 sendMessage('requestDetails', {
1196 id: data.id,
1197 class: data.class
1198 });
Simon Hunt61d04042014-11-11 17:27:16 -08001199 }
1200
Thomas Vachuska9edca302014-11-22 17:06:42 -08001201 function addHostIntentAction() {
Simon Huntd72bc702014-11-13 18:38:04 -08001202 sendMessage('addHostIntent', {
Thomas Vachuska9edca302014-11-22 17:06:42 -08001203 one: selectOrder[0],
1204 two: selectOrder[1],
1205 ids: selectOrder
Simon Huntd72bc702014-11-13 18:38:04 -08001206 });
Simon Hunt27d322d2014-11-28 10:45:43 -08001207 flash('Host-to-Host flow added');
Simon Huntd72bc702014-11-13 18:38:04 -08001208 }
1209
Thomas Vachuska9edca302014-11-22 17:06:42 -08001210 function addMultiSourceIntentAction() {
1211 sendMessage('addMultiSourceIntent', {
1212 src: selectOrder.slice(0, selectOrder.length - 1),
1213 dst: selectOrder[selectOrder.length - 1],
1214 ids: selectOrder
1215 });
Simon Hunt27d322d2014-11-28 10:45:43 -08001216 flash('Multi-Source flow added');
Thomas Vachuska29617e52014-11-20 03:17:46 -08001217 }
1218
Thomas Vachuska9edca302014-11-22 17:06:42 -08001219
Thomas Vachuska47635c62014-11-22 01:21:36 -08001220 function cancelTraffic() {
Simon Huntc1cc81c2014-11-29 14:59:01 -08001221 sendMessage('cancelTraffic');
Thomas Vachuska47635c62014-11-22 01:21:36 -08001222 }
1223
Thomas Vachuska9edca302014-11-22 17:06:42 -08001224 function requestTrafficForMode() {
Thomas Vachuska164fa5c2014-12-02 21:59:41 -08001225 if (hoverMode === hoverModeFlows) {
Thomas Vachuska9edca302014-11-22 17:06:42 -08001226 requestDeviceLinkFlows();
1227 } else if (hoverMode === hoverModeIntents) {
Thomas Vachuska164fa5c2014-12-02 21:59:41 -08001228 requestRelatedIntents();
Simon Huntd72bc702014-11-13 18:38:04 -08001229 }
Thomas Vachuska9edca302014-11-22 17:06:42 -08001230 }
Simon Huntd72bc702014-11-13 18:38:04 -08001231
Thomas Vachuska164fa5c2014-12-02 21:59:41 -08001232 function showRelatedIntentsAction() {
Thomas Vachuska9edca302014-11-22 17:06:42 -08001233 hoverMode = hoverModeIntents;
Thomas Vachuska164fa5c2014-12-02 21:59:41 -08001234 requestRelatedIntents();
1235 flash('Related intents');
Thomas Vachuska9edca302014-11-22 17:06:42 -08001236 }
1237
Thomas Vachuska164fa5c2014-12-02 21:59:41 -08001238 function requestRelatedIntents() {
Simon Huntc1cc81c2014-11-29 14:59:01 -08001239 function hoverValid() {
1240 return hoverMode === hoverModeIntents &&
1241 hovered &&
1242 (hovered.class === 'host' || hovered.class === 'device');
1243 }
1244
Thomas Vachuska9edca302014-11-22 17:06:42 -08001245 if (validateSelectionContext()) {
Thomas Vachuska164fa5c2014-12-02 21:59:41 -08001246 sendMessage('requestRelatedIntents', {
Thomas Vachuska9edca302014-11-22 17:06:42 -08001247 ids: selectOrder,
Simon Huntc1cc81c2014-11-29 14:59:01 -08001248 hover: hoverValid() ? hovered.id : ''
Thomas Vachuska9edca302014-11-22 17:06:42 -08001249 });
1250 }
Simon Huntd72bc702014-11-13 18:38:04 -08001251 }
1252
Thomas Vachuska164fa5c2014-12-02 21:59:41 -08001253 function showNextIntentAction() {
1254 hoverMode = hoverModeNone;
1255 sendMessage('requestNextRelatedIntent', {});
1256 flash('Next related intent');
1257 }
1258
1259 function showSelectedIntentTrafficAction() {
1260 hoverMode = hoverModeNone;
1261 sendMessage('requestSelectedIntentTraffic', {});
1262 flash('Monitoring selected intent');
1263 }
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -08001264
Thomas Vachuska29617e52014-11-20 03:17:46 -08001265 function showDeviceLinkFlowsAction() {
Thomas Vachuska9edca302014-11-22 17:06:42 -08001266 hoverMode = hoverModeFlows;
1267 requestDeviceLinkFlows();
Simon Hunt27d322d2014-11-28 10:45:43 -08001268 flash('Device Flows');
Thomas Vachuska29617e52014-11-20 03:17:46 -08001269 }
1270
Thomas Vachuska9edca302014-11-22 17:06:42 -08001271 function requestDeviceLinkFlows() {
Simon Huntc1cc81c2014-11-29 14:59:01 -08001272 function hoverValid() {
1273 return hoverMode === hoverModeFlows &&
1274 hovered && (hovered.class === 'device');
1275 }
1276
Thomas Vachuska9edca302014-11-22 17:06:42 -08001277 if (validateSelectionContext()) {
Thomas Vachuska9edca302014-11-22 17:06:42 -08001278 sendMessage('requestDeviceLinkFlows', {
1279 ids: selectOrder,
Simon Huntc1cc81c2014-11-29 14:59:01 -08001280 hover: hoverValid() ? hovered.id : ''
Thomas Vachuska9edca302014-11-22 17:06:42 -08001281 });
1282 }
1283 }
1284
1285
1286 function showAllTrafficAction() {
1287 hoverMode = hoverModeAll;
1288 requestAllTraffic();
Simon Hunt27d322d2014-11-28 10:45:43 -08001289 flash('All Traffic');
Thomas Vachuska9edca302014-11-22 17:06:42 -08001290 }
1291
1292 function requestAllTraffic() {
Simon Huntc1cc81c2014-11-29 14:59:01 -08001293 sendMessage('requestAllTraffic');
Thomas Vachuska9edca302014-11-22 17:06:42 -08001294 }
1295
1296 function validateSelectionContext() {
Thomas Vachuska29617e52014-11-20 03:17:46 -08001297 if (!hovered && nSel() === 0) {
Thomas Vachuska47635c62014-11-22 01:21:36 -08001298 cancelTraffic();
Thomas Vachuska9edca302014-11-22 17:06:42 -08001299 return false;
Thomas Vachuska29617e52014-11-20 03:17:46 -08001300 }
Thomas Vachuska9edca302014-11-22 17:06:42 -08001301 return true;
Thomas Vachuska29617e52014-11-20 03:17:46 -08001302 }
Simon Huntd72bc702014-11-13 18:38:04 -08001303
Simon Hunta6a9fe72014-11-20 11:17:12 -08001304
Simon Hunt61d04042014-11-11 17:27:16 -08001305 // ==============================
Simon Hunta5e89142014-11-14 07:00:33 -08001306 // onos instance panel functions
Simon Huntb82f6902014-11-22 11:53:15 -08001307
Simon Hunt7b403bc2014-11-22 19:01:00 -08001308 var instCfg = {
1309 rectPad: 8,
1310 nodeOx: 9,
1311 nodeOy: 9,
1312 nodeDim: 40,
1313 birdOx: 19,
1314 birdOy: 21,
1315 birdDim: 21,
1316 uiDy: 45,
1317 titleDy: 30,
1318 textYOff: 20,
1319 textYSpc: 15
1320 };
1321
1322 function viewBox(dim) {
1323 return '0 0 ' + dim.w + ' ' + dim.h;
1324 }
1325
1326 function instRectAttr(dim) {
1327 var pad = instCfg.rectPad;
1328 return {
1329 x: pad,
1330 y: pad,
1331 width: dim.w - pad*2,
1332 height: dim.h - pad*2,
Simon Huntb0ecfa52014-11-23 21:05:12 -08001333 rx: 6
Simon Hunt7b403bc2014-11-22 19:01:00 -08001334 };
1335 }
1336
1337 function computeDim(self) {
1338 var css = window.getComputedStyle(self);
1339 return {
1340 w: stripPx(css.width),
1341 h: stripPx(css.height)
1342 };
Simon Huntb82f6902014-11-22 11:53:15 -08001343 }
Simon Hunta5e89142014-11-14 07:00:33 -08001344
1345 function updateInstances() {
1346 var onoses = oiBox.el.selectAll('.onosInst')
Simon Huntb82f6902014-11-22 11:53:15 -08001347 .data(onosOrder, function (d) { return d.id; }),
Simon Hunt7b403bc2014-11-22 19:01:00 -08001348 instDim = {w:0,h:0},
1349 c = instCfg;
Simon Hunta5e89142014-11-14 07:00:33 -08001350
Simon Hunt7b403bc2014-11-22 19:01:00 -08001351 function nSw(n) {
1352 return '# Switches: ' + n;
1353 }
Simon Hunta5e89142014-11-14 07:00:33 -08001354
Simon Huntb82f6902014-11-22 11:53:15 -08001355 // operate on existing onos instances if necessary
1356 onoses.each(function (d) {
1357 var el = d3.select(this),
1358 svg = el.select('svg');
Simon Hunt7b403bc2014-11-22 19:01:00 -08001359 instDim = computeDim(this);
Simon Huntb82f6902014-11-22 11:53:15 -08001360
1361 // update online state
1362 el.classed('online', d.online);
1363
1364 // update ui-attached state
1365 svg.select('use.uiBadge').remove();
1366 if (d.uiAttached) {
1367 attachUiBadge(svg);
1368 }
1369
Simon Hunt7b403bc2014-11-22 19:01:00 -08001370 function updAttr(id, value) {
1371 svg.select('text.instLabel.'+id).text(value);
1372 }
1373
1374 updAttr('ip', d.ip);
1375 updAttr('ns', nSw(d.switches));
Simon Huntb82f6902014-11-22 11:53:15 -08001376 });
1377
1378
1379 // operate on new onos instances
Simon Hunta5e89142014-11-14 07:00:33 -08001380 var entering = onoses.enter()
1381 .append('div')
1382 .attr('class', 'onosInst')
1383 .classed('online', function (d) { return d.online; })
Simon Hunt9c15eca2014-11-15 18:37:59 -08001384 .on('click', clickInst);
1385
Simon Huntb82f6902014-11-22 11:53:15 -08001386 entering.each(function (d) {
Simon Hunt9c15eca2014-11-15 18:37:59 -08001387 var el = d3.select(this),
Simon Hunt7b403bc2014-11-22 19:01:00 -08001388 rectAttr,
1389 svg;
1390 instDim = computeDim(this);
1391 rectAttr = instRectAttr(instDim);
Simon Hunt9c15eca2014-11-15 18:37:59 -08001392
Simon Hunt7b403bc2014-11-22 19:01:00 -08001393 svg = el.append('svg').attr({
1394 width: instDim.w,
1395 height: instDim.h,
1396 viewBox: viewBox(instDim)
Simon Hunt95908012014-11-20 10:20:26 -08001397 });
Simon Huntb82f6902014-11-22 11:53:15 -08001398
Simon Hunt7b403bc2014-11-22 19:01:00 -08001399 svg.append('rect').attr(rectAttr);
Simon Hunt9c15eca2014-11-15 18:37:59 -08001400
Thomas Vachuskae02e11c2014-11-24 16:13:52 -08001401 //appendGlyph(svg, c.nodeOx, c.nodeOy, c.nodeDim, '#node');
1402 appendBadge(svg, 14, 14, 28, '#bird');
Simon Huntb82f6902014-11-22 11:53:15 -08001403
1404 if (d.uiAttached) {
1405 attachUiBadge(svg);
1406 }
1407
Simon Hunt7b403bc2014-11-22 19:01:00 -08001408 var left = c.nodeOx + c.nodeDim,
1409 len = rectAttr.width - left,
1410 hlen = len / 2,
1411 midline = hlen + left;
Simon Hunt9c15eca2014-11-15 18:37:59 -08001412
Simon Hunt7b403bc2014-11-22 19:01:00 -08001413 // title
1414 svg.append('text')
1415 .attr({
1416 class: 'instTitle',
1417 x: midline,
1418 y: c.titleDy
1419 })
1420 .text(d.id);
1421
1422 // a couple of attributes
1423 var ty = c.titleDy + c.textYOff;
1424
1425 function addAttr(id, label) {
1426 svg.append('text').attr({
1427 class: 'instLabel ' + id,
1428 x: midline,
1429 y: ty
1430 }).text(label);
1431 ty += c.textYSpc;
1432 }
1433
1434 addAttr('ip', d.ip);
1435 addAttr('ns', nSw(d.switches));
Simon Hunt9c15eca2014-11-15 18:37:59 -08001436 });
Simon Hunta5e89142014-11-14 07:00:33 -08001437
1438 // operate on existing + new onoses here
Simon Hunt8f40cce2014-11-23 15:57:30 -08001439 // set the affinity colors...
1440 onoses.each(function (d) {
1441 var el = d3.select(this),
1442 rect = el.select('svg').select('rect'),
1443 col = instColor(d.id, d.online);
1444 rect.style('fill', col);
1445 });
Simon Hunta5e89142014-11-14 07:00:33 -08001446
Simon Hunt7b403bc2014-11-22 19:01:00 -08001447 // adjust the panel size appropriately...
1448 oiBox.width(instDim.w * onosOrder.length);
1449 oiBox.height(instDim.h);
1450
1451 // remove any outgoing instances
1452 onoses.exit().remove();
Simon Hunta5e89142014-11-14 07:00:33 -08001453 }
1454
Simon Hunt8f40cce2014-11-23 15:57:30 -08001455 function instColor(id, online) {
1456 return cat7.get(id, !online, network.view.getTheme());
1457 }
1458
Simon Hunt9462e8c2014-11-14 17:28:09 -08001459 function clickInst(d) {
1460 var el = d3.select(this),
1461 aff = el.classed('affinity');
1462 if (!aff) {
1463 setAffinity(el, d);
1464 } else {
1465 cancelAffinity();
1466 }
1467 }
1468
1469 function setAffinity(el, d) {
1470 d3.selectAll('.onosInst')
1471 .classed('mastership', true)
1472 .classed('affinity', false);
1473 el.classed('affinity', true);
1474
1475 suppressLayers(true);
1476 node.each(function (n) {
1477 if (n.master === d.id) {
1478 n.el.classed('suppressed', false);
1479 }
1480 });
1481 oiShowMaster = true;
1482 }
1483
1484 function cancelAffinity() {
1485 d3.selectAll('.onosInst')
1486 .classed('mastership affinity', false);
1487 restoreLayerState();
1488 oiShowMaster = false;
1489 }
1490
Simon Hunt7b403bc2014-11-22 19:01:00 -08001491 // TODO: these should be moved out to utility module.
1492 function stripPx(s) {
1493 return s.replace(/px$/,'');
1494 }
1495
1496 function appendUse(svg, ox, oy, dim, iid, cls) {
1497 var use = svg.append('use').attr({
1498 transform: translate(ox,oy),
1499 'xlink:href': iid,
1500 width: dim,
1501 height: dim
1502 });
1503 if (cls) {
1504 use.classed(cls, true);
1505 }
1506 return use;
1507 }
1508
1509 function appendGlyph(svg, ox, oy, dim, iid, cls) {
1510 appendUse(svg, ox, oy, dim, iid, cls).classed('glyphIcon', true);
1511 }
1512
1513 function appendBadge(svg, ox, oy, dim, iid, cls) {
1514 appendUse(svg, ox, oy, dim, iid,cls ).classed('badgeIcon', true);
1515 }
1516
1517 function attachUiBadge(svg) {
1518 appendBadge(svg, 12, instCfg.uiDy, 30, '#uiAttached', 'uiBadge');
1519 }
1520
Simon Hunt434cf142014-11-24 11:10:28 -08001521 function visVal(b) {
1522 return b ? 'visible' : 'hidden';
1523 }
1524
Simon Hunta5e89142014-11-14 07:00:33 -08001525 // ==============================
Simon Hunt99c13842014-11-06 18:23:12 -08001526 // force layout modification functions
1527
1528 function translate(x, y) {
1529 return 'translate(' + x + ',' + y + ')';
1530 }
Simon Huntd6f5a272014-11-29 23:45:50 -08001531 function scale(x,y) {
1532 return 'scale(' + x + ',' + y + ')';
1533 }
1534 function skewX(x) {
1535 return 'skewX(' + x + ')';
1536 }
Simon Hunte2575b62014-11-18 15:25:53 -08001537 function rotate(deg) {
1538 return 'rotate(' + deg + ')';
1539 }
1540
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001541 function missMsg(what, id) {
1542 return '\n[' + what + '] "' + id + '" missing ';
1543 }
1544
1545 function linkEndPoints(srcId, dstId) {
1546 var srcNode = network.lookup[srcId],
1547 dstNode = network.lookup[dstId],
1548 sMiss = !srcNode ? missMsg('src', srcId) : '',
1549 dMiss = !dstNode ? missMsg('dst', dstId) : '';
1550
1551 if (sMiss || dMiss) {
1552 logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
1553 return null;
1554 }
1555 return {
1556 source: srcNode,
1557 target: dstNode,
1558 x1: srcNode.x,
1559 y1: srcNode.y,
1560 x2: dstNode.x,
1561 y2: dstNode.y
1562 };
1563 }
1564
Simon Hunt56d51852014-11-09 13:03:35 -08001565 function createHostLink(host) {
1566 var src = host.id,
1567 dst = host.cp.device,
Simon Hunt7cd48f32014-11-09 23:42:50 -08001568 id = host.ingress,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001569 lnk = linkEndPoints(src, dst);
Simon Hunt56d51852014-11-09 13:03:35 -08001570
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001571 if (!lnk) {
Simon Hunt56d51852014-11-09 13:03:35 -08001572 return null;
1573 }
1574
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001575 // Synthesize link ...
1576 $.extend(lnk, {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001577 key: id,
Simon Hunt56d51852014-11-09 13:03:35 -08001578 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -08001579
1580 type: function () { return 'hostLink'; },
Simon Hunt6d9bd032014-11-28 22:16:40 -08001581 online: function () {
1582 // hostlink target is edge switch
1583 return lnk.target.online;
1584 },
Simon Hunt8257f4c2014-11-16 19:34:54 -08001585 linkWidth: function () { return 1; }
Simon Hunt7cd48f32014-11-09 23:42:50 -08001586 });
Simon Hunt99c13842014-11-06 18:23:12 -08001587 return lnk;
1588 }
1589
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001590 function createLink(link) {
Simon Hunta6a9fe72014-11-20 11:17:12 -08001591 var lnk = linkEndPoints(link.src, link.dst);
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001592
1593 if (!lnk) {
1594 return null;
1595 }
1596
Simon Hunt8257f4c2014-11-16 19:34:54 -08001597 $.extend(lnk, {
1598 key: link.id,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001599 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -08001600 fromSource: link,
1601
1602 // functions to aggregate dual link state
1603 type: function () {
1604 var s = lnk.fromSource,
1605 t = lnk.fromTarget;
1606 return (s && s.type) || (t && t.type) || defaultLinkType;
1607 },
1608 online: function () {
1609 var s = lnk.fromSource,
Simon Hunt6d9bd032014-11-28 22:16:40 -08001610 t = lnk.fromTarget,
1611 both = lnk.source.online && lnk.target.online;
1612 return both && ((s && s.online) || (t && t.online));
Simon Hunt8257f4c2014-11-16 19:34:54 -08001613 },
1614 linkWidth: function () {
1615 var s = lnk.fromSource,
1616 t = lnk.fromTarget,
1617 ws = (s && s.linkWidth) || 0,
1618 wt = (t && t.linkWidth) || 0;
1619 return Math.max(ws, wt);
1620 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001621 });
1622 return lnk;
Simon Hunt1a9eff92014-11-07 11:06:34 -08001623 }
1624
Simon Hunte2575b62014-11-18 15:25:53 -08001625 function removeLinkLabels() {
1626 network.links.forEach(function (d) {
1627 d.label = '';
1628 });
1629 }
1630
Simon Hunt434cf142014-11-24 11:10:28 -08001631 function showHostVis(el) {
1632 el.style('visibility', visVal(showHosts));
1633 }
1634
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001635 var widthRatio = 1.4,
1636 linkScale = d3.scale.linear()
1637 .domain([1, 12])
1638 .range([widthRatio, 12 * widthRatio])
1639 .clamp(true);
1640
Simon Hunt99c13842014-11-06 18:23:12 -08001641 function updateLinks() {
1642 link = linkG.selectAll('.link')
Simon Hunt8257f4c2014-11-16 19:34:54 -08001643 .data(network.links, function (d) { return d.key; });
Simon Hunt99c13842014-11-06 18:23:12 -08001644
1645 // operate on existing links, if necessary
1646 // link .foo() .bar() ...
1647
1648 // operate on entering links:
1649 var entering = link.enter()
1650 .append('line')
1651 .attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001652 x1: function (d) { return d.x1; },
1653 y1: function (d) { return d.y1; },
1654 x2: function (d) { return d.x2; },
1655 y2: function (d) { return d.y2; },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001656 stroke: config.topo.linkInColor,
1657 'stroke-width': config.topo.linkInWidth
Simon Hunt99c13842014-11-06 18:23:12 -08001658 });
1659
1660 // augment links
Simon Hunt7cd48f32014-11-09 23:42:50 -08001661 entering.each(function (d) {
1662 var link = d3.select(this);
1663 // provide ref to element selection from backing data....
1664 d.el = link;
Simon Hunt8257f4c2014-11-16 19:34:54 -08001665 restyleLinkElement(d);
Simon Hunt434cf142014-11-24 11:10:28 -08001666 if (d.type() === 'hostLink') {
1667 showHostVis(link);
1668 }
Simon Hunt7cd48f32014-11-09 23:42:50 -08001669 });
Thomas Vachuska4830d392014-11-09 17:09:56 -08001670
1671 // operate on both existing and new links, if necessary
1672 //link .foo() .bar() ...
1673
Simon Hunte2575b62014-11-18 15:25:53 -08001674 // apply or remove labels
1675 var labelData = getLabelData();
1676 applyLinkLabels(labelData);
1677
Thomas Vachuska4830d392014-11-09 17:09:56 -08001678 // operate on exiting links:
Thomas Vachuska4830d392014-11-09 17:09:56 -08001679 link.exit()
Simon Hunt6d9bd032014-11-28 22:16:40 -08001680 .attr('stroke-dasharray', '3 3')
Simon Hunt13bf9c82014-11-18 07:26:44 -08001681 .style('opacity', 0.5)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001682 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -08001683 .duration(1500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001684 .attr({
Simon Hunt6d9bd032014-11-28 22:16:40 -08001685 'stroke-dasharray': '3 12',
Simon Hunt13bf9c82014-11-18 07:26:44 -08001686 stroke: config.topo.linkOutColor,
1687 'stroke-width': config.topo.linkOutWidth
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001688 })
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001689 .style('opacity', 0.0)
Thomas Vachuska4830d392014-11-09 17:09:56 -08001690 .remove();
Simon Hunte2575b62014-11-18 15:25:53 -08001691
1692 // NOTE: invoke a single tick to force the labels to position
1693 // onto their links.
1694 tick();
1695 }
1696
1697 function getLabelData() {
1698 // create the backing data for showing labels..
1699 var data = [];
1700 link.each(function (d) {
1701 if (d.label) {
1702 data.push({
1703 id: 'lab-' + d.key,
1704 key: d.key,
1705 label: d.label,
1706 ldata: d
1707 });
1708 }
1709 });
1710 return data;
1711 }
1712
1713 var linkLabelOffset = '0.3em';
1714
1715 function applyLinkLabels(data) {
1716 var entering;
1717
1718 linkLabel = linkLabelG.selectAll('.linkLabel')
1719 .data(data, function (d) { return d.id; });
1720
Simon Hunt56a2ea42014-11-19 12:39:31 -08001721 // for elements already existing, we need to update the text
1722 // and adjust the rectangle size to fit
1723 linkLabel.each(function (d) {
1724 var el = d3.select(this),
1725 rect = el.select('rect'),
1726 text = el.select('text');
1727 text.text(d.label);
1728 rect.attr(rectAroundText(el));
1729 });
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -08001730
Simon Hunte2575b62014-11-18 15:25:53 -08001731 entering = linkLabel.enter().append('g')
1732 .classed('linkLabel', true)
1733 .attr('id', function (d) { return d.id; });
1734
1735 entering.each(function (d) {
1736 var el = d3.select(this),
1737 rect,
1738 text,
1739 parms = {
1740 x1: d.ldata.x1,
1741 y1: d.ldata.y1,
1742 x2: d.ldata.x2,
1743 y2: d.ldata.y2
1744 };
1745
1746 d.el = el;
1747 rect = el.append('rect');
1748 text = el.append('text').text(d.label);
1749 rect.attr(rectAroundText(el));
1750 text.attr('dy', linkLabelOffset);
1751
1752 el.attr('transform', transformLabel(parms));
1753 });
1754
1755 // Remove any links that are no longer required.
1756 linkLabel.exit().remove();
1757 }
1758
1759 function rectAroundText(el) {
1760 var text = el.select('text'),
1761 box = text.node().getBBox();
1762
1763 // translate the bbox so that it is centered on [x,y]
1764 box.x = -box.width / 2;
1765 box.y = -box.height / 2;
1766
1767 // add padding
1768 box.x -= 1;
1769 box.width += 2;
1770 return box;
1771 }
1772
1773 function transformLabel(p) {
1774 var dx = p.x2 - p.x1,
1775 dy = p.y2 - p.y1,
1776 xMid = dx/2 + p.x1,
1777 yMid = dy/2 + p.y1;
Simon Hunte2575b62014-11-18 15:25:53 -08001778 return translate(xMid, yMid);
Simon Hunt99c13842014-11-06 18:23:12 -08001779 }
1780
1781 function createDeviceNode(device) {
1782 // start with the object as is
1783 var node = device,
Simon Huntbb282f52014-11-10 11:08:19 -08001784 type = device.type,
Simon Huntc72967b2014-11-20 09:21:42 -08001785 svgCls = type ? 'node device ' + type : 'node device',
1786 labels = device.labels || [];
1787
Simon Hunt99c13842014-11-06 18:23:12 -08001788 // Augment as needed...
1789 node.class = 'device';
Simon Huntbb282f52014-11-10 11:08:19 -08001790 node.svgClass = device.online ? svgCls + ' online' : svgCls;
Simon Hunt99c13842014-11-06 18:23:12 -08001791 positionNode(node);
Simon Hunt99c13842014-11-06 18:23:12 -08001792 return node;
1793 }
1794
Simon Hunt56d51852014-11-09 13:03:35 -08001795 function createHostNode(host) {
1796 // start with the object as is
1797 var node = host;
1798
1799 // Augment as needed...
1800 node.class = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001801 if (!node.type) {
Simon Hunt209155e2014-11-21 12:16:09 -08001802 node.type = 'endstation';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001803 }
Simon Hunt7fa116d2014-11-17 14:16:55 -08001804 node.svgClass = 'node host ' + node.type;
Simon Hunt56d51852014-11-09 13:03:35 -08001805 positionNode(node);
Simon Hunt56d51852014-11-09 13:03:35 -08001806 return node;
1807 }
1808
Simon Hunt62c47542014-11-22 22:16:32 -08001809 function positionNode(node, forUpdate) {
Simon Hunt99c13842014-11-06 18:23:12 -08001810 var meta = node.metaUi,
Simon Huntac9e24f2014-11-12 10:12:21 -08001811 x = meta && meta.x,
1812 y = meta && meta.y,
1813 xy;
Simon Hunt99c13842014-11-06 18:23:12 -08001814
Simon Huntac9e24f2014-11-12 10:12:21 -08001815 // If we have [x,y] already, use that...
Simon Hunt99c13842014-11-06 18:23:12 -08001816 if (x && y) {
1817 node.fixed = true;
Simon Hunt62c47542014-11-22 22:16:32 -08001818 node.px = node.x = x;
1819 node.py = node.y = y;
Simon Huntac9e24f2014-11-12 10:12:21 -08001820 return;
Simon Hunt99c13842014-11-06 18:23:12 -08001821 }
Simon Huntac9e24f2014-11-12 10:12:21 -08001822
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001823 var location = node.location;
1824 if (location && location.type === 'latlng') {
Simon Hunt1b18aa52014-11-29 17:57:55 -08001825 var coord = geoMapProj([location.lng, location.lat]);
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001826 node.fixed = true;
Simon Hunt62c47542014-11-22 22:16:32 -08001827 node.px = node.x = coord[0];
1828 node.py = node.y = coord[1];
Simon Hunt62c47542014-11-22 22:16:32 -08001829 return true;
1830 }
1831
1832 // if this is a node update (not a node add).. skip randomizer
1833 if (forUpdate) {
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001834 return;
1835 }
1836
Simon Huntac9e24f2014-11-12 10:12:21 -08001837 // Note: Placing incoming unpinned nodes at exactly the same point
1838 // (center of the view) causes them to explode outwards when
1839 // the force layout kicks in. So, we spread them out a bit
1840 // initially, to provide a more serene layout convergence.
1841 // Additionally, if the node is a host, we place it near
1842 // the device it is connected to.
1843
1844 function spread(s) {
1845 return Math.floor((Math.random() * s) - s/2);
1846 }
1847
1848 function randDim(dim) {
1849 return dim / 2 + spread(dim * 0.7071);
1850 }
1851
1852 function rand() {
1853 return {
1854 x: randDim(network.view.width()),
1855 y: randDim(network.view.height())
1856 };
1857 }
1858
1859 function near(node) {
1860 var min = 12,
1861 dx = spread(12),
1862 dy = spread(12);
1863 return {
1864 x: node.x + min + dx,
1865 y: node.y + min + dy
1866 };
1867 }
1868
1869 function getDevice(cp) {
1870 var d = network.lookup[cp.device];
1871 return d || rand();
1872 }
1873
1874 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
1875 $.extend(node, xy);
Simon Hunt99c13842014-11-06 18:23:12 -08001876 }
1877
Simon Hunt99c13842014-11-06 18:23:12 -08001878
Simon Huntc72967b2014-11-20 09:21:42 -08001879 function iconGlyphUrl(d) {
1880 var which = d.type || 'unknown';
1881 return '#' + which;
1882 }
1883
Simon Hunt99c13842014-11-06 18:23:12 -08001884 // returns the newly computed bounding box of the rectangle
1885 function adjustRectToFitText(n) {
1886 var text = n.select('text'),
1887 box = text.node().getBBox(),
1888 lab = config.labels;
1889
1890 text.attr('text-anchor', 'middle')
1891 .attr('y', '-0.8em')
1892 .attr('x', lab.imgPad/2);
1893
1894 // translate the bbox so that it is centered on [x,y]
1895 box.x = -box.width / 2;
1896 box.y = -box.height / 2;
1897
1898 // add padding
1899 box.x -= (lab.padLR + lab.imgPad/2);
1900 box.width += lab.padLR * 2 + lab.imgPad;
1901 box.y -= lab.padTB;
1902 box.height += lab.padTB * 2;
1903
1904 return box;
1905 }
1906
Simon Hunt1a9eff92014-11-07 11:06:34 -08001907 function mkSvgClass(d) {
1908 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
1909 }
1910
Simon Hunt7cd48f32014-11-09 23:42:50 -08001911 function hostLabel(d) {
1912 var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0;
1913 return d.labels[idx];
1914 }
1915 function deviceLabel(d) {
1916 var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0;
1917 return d.labels[idx];
1918 }
Simon Huntc72967b2014-11-20 09:21:42 -08001919 function trimLabel(label) {
1920 return (label && label.trim()) || '';
1921 }
1922
1923 function emptyBox() {
1924 return {
1925 x: -2,
1926 y: -2,
1927 width: 4,
1928 height: 4
1929 };
Simon Hunt7cd48f32014-11-09 23:42:50 -08001930 }
1931
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001932 function updateDeviceLabel(d) {
Simon Huntc72967b2014-11-20 09:21:42 -08001933 var label = trimLabel(deviceLabel(d)),
1934 noLabel = !label,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001935 node = d.el,
Simon Huntc72967b2014-11-20 09:21:42 -08001936 box,
1937 dx,
Simon Hunt395a70c2014-11-22 23:17:40 -08001938 dy,
1939 cfg = config.icons.device;
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001940
1941 node.select('text')
1942 .text(label)
1943 .style('opacity', 0)
1944 .transition()
1945 .style('opacity', 1);
1946
Simon Huntc72967b2014-11-20 09:21:42 -08001947 if (noLabel) {
1948 box = emptyBox();
Simon Hunt395a70c2014-11-22 23:17:40 -08001949 dx = -cfg.dim/2;
1950 dy = -cfg.dim/2;
Simon Huntc72967b2014-11-20 09:21:42 -08001951 } else {
1952 box = adjustRectToFitText(node);
Simon Hunt395a70c2014-11-22 23:17:40 -08001953 dx = box.x + cfg.xoff;
1954 dy = box.y + cfg.yoff;
Simon Huntc72967b2014-11-20 09:21:42 -08001955 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001956
1957 node.select('rect')
1958 .transition()
1959 .attr(box);
1960
Simon Huntc72967b2014-11-20 09:21:42 -08001961 node.select('g.deviceIcon')
Thomas Vachuska89543292014-11-19 11:28:33 -08001962 .transition()
Simon Huntc72967b2014-11-20 09:21:42 -08001963 .attr('transform', translate(dx, dy));
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001964 }
1965
1966 function updateHostLabel(d) {
Simon Hunt6d9bd032014-11-28 22:16:40 -08001967 var label = trimLabel(hostLabel(d));
1968 d.el.select('text').text(label);
Simon Huntbb282f52014-11-10 11:08:19 -08001969 }
1970
Simon Hunt434cf142014-11-24 11:10:28 -08001971 function updateHostVisibility() {
1972 var v = visVal(showHosts);
1973 nodeG.selectAll('.host').style('visibility', v);
1974 linkG.selectAll('.hostLink').style('visibility', v);
1975 }
1976
Simon Hunt6d9bd032014-11-28 22:16:40 -08001977 function findOfflineNodes() {
1978 var a = [];
1979 network.nodes.forEach(function (d) {
1980 if (d.class === 'device' && !d.online) {
1981 a.push(d);
1982 }
1983 });
1984 return a;
1985 }
1986
1987 function updateOfflineVisibility(dev) {
1988 var so = showOffline,
1989 sh = showHosts,
1990 vb = 'visibility',
1991 v, off, al, ah, db, b;
1992
1993 function updAtt(show) {
1994 al.forEach(function (d) {
1995 b = show && ((d.type() !== 'hostLink') || sh);
1996 d.el.style(vb, visVal(b));
1997 });
1998 ah.forEach(function (d) {
1999 b = show && sh;
2000 d.el.style(vb, visVal(b));
2001 });
2002 }
2003
2004 if (dev) {
2005 // updating a specific device that just toggled off/on-line
2006 db = dev.online || so;
2007 dev.el.style(vb, visVal(db));
2008 al = findAttachedLinks(dev.id);
2009 ah = findAttachedHosts(dev.id);
2010 updAtt(db);
2011 } else {
2012 // updating all offline devices
2013 v = visVal(so);
2014 off = findOfflineNodes();
2015 off.forEach(function (d) {
2016 d.el.style(vb, v);
2017 al = findAttachedLinks(d.id);
2018 ah = findAttachedHosts(d.id);
2019 updAtt(so);
2020 });
2021 }
2022 }
2023
Simon Hunt6ac93f32014-11-13 12:17:27 -08002024 function nodeMouseOver(d) {
Thomas Vachuska164fa5c2014-12-02 21:59:41 -08002025 if (hovered != d) {
2026 hovered = d;
2027 requestTrafficForMode();
2028 }
Simon Hunt6ac93f32014-11-13 12:17:27 -08002029 }
2030
2031 function nodeMouseOut(d) {
Thomas Vachuska164fa5c2014-12-02 21:59:41 -08002032 if (hovered != null) {
2033 hovered = null;
2034 requestTrafficForMode();
2035 }
Simon Hunt6ac93f32014-11-13 12:17:27 -08002036 }
Simon Huntbb282f52014-11-10 11:08:19 -08002037
Simon Hunteb1514d2014-11-20 09:57:29 -08002038 function addHostIcon(node, radius, iid) {
Thomas Vachuska89543292014-11-19 11:28:33 -08002039 var dim = radius * 1.5,
2040 xlate = -dim / 2;
2041
Simon Hunteb1514d2014-11-20 09:57:29 -08002042 node.append('use').attr({
2043 class: 'glyphIcon hostIcon',
2044 transform: translate(xlate,xlate),
2045 'xlink:href': iid,
2046 width: dim,
2047 height: dim
2048 });
Thomas Vachuska89543292014-11-19 11:28:33 -08002049 }
2050
Simon Hunt99c13842014-11-06 18:23:12 -08002051 function updateNodes() {
2052 node = nodeG.selectAll('.node')
2053 .data(network.nodes, function (d) { return d.id; });
2054
Simon Hunt62c47542014-11-22 22:16:32 -08002055 // operate on existing nodes...
2056 node.filter('.device').each(function (d) {
Simon Hunt62c47542014-11-22 22:16:32 -08002057 var node = d.el;
2058 node.classed('online', d.online);
2059 updateDeviceLabel(d);
2060 positionNode(d, true);
2061 });
2062
2063 node.filter('.host').each(function (d) {
Simon Hunt6d9bd032014-11-28 22:16:40 -08002064 updateHostLabel(d);
2065 positionNode(d, true);
Simon Hunt62c47542014-11-22 22:16:32 -08002066 });
Simon Hunt99c13842014-11-06 18:23:12 -08002067
2068 // operate on entering nodes:
2069 var entering = node.enter()
2070 .append('g')
2071 .attr({
2072 id: function (d) { return safeId(d.id); },
Simon Hunt1a9eff92014-11-07 11:06:34 -08002073 class: mkSvgClass,
Simon Hunt99c13842014-11-06 18:23:12 -08002074 transform: function (d) { return translate(d.x, d.y); },
2075 opacity: 0
2076 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08002077 .call(network.drag)
Simon Hunt6ac93f32014-11-13 12:17:27 -08002078 .on('mouseover', nodeMouseOver)
2079 .on('mouseout', nodeMouseOut)
Simon Hunt99c13842014-11-06 18:23:12 -08002080 .transition()
2081 .attr('opacity', 1);
2082
2083 // augment device nodes...
2084 entering.filter('.device').each(function (d) {
2085 var node = d3.select(this),
Simon Huntc72967b2014-11-20 09:21:42 -08002086 label = trimLabel(deviceLabel(d)),
2087 noLabel = !label,
Simon Hunt99c13842014-11-06 18:23:12 -08002088 box;
2089
Simon Hunt7cd48f32014-11-09 23:42:50 -08002090 // provide ref to element from backing data....
2091 d.el = node;
2092
Simon Hunt62c47542014-11-22 22:16:32 -08002093 node.append('rect').attr({ rx: 5, ry: 5 });
2094 node.append('text').text(label).attr('dy', '1.1em');
Simon Hunt99c13842014-11-06 18:23:12 -08002095 box = adjustRectToFitText(node);
Simon Hunta3dd9572014-11-20 15:22:41 -08002096 node.select('rect').attr(box);
Simon Huntc72967b2014-11-20 09:21:42 -08002097 addDeviceIcon(node, box, noLabel, iconGlyphUrl(d));
Simon Huntc7ee0662014-11-05 16:44:37 -08002098 });
Simon Hunt934c3ce2014-11-05 11:45:07 -08002099
Thomas Vachuska89543292014-11-19 11:28:33 -08002100
Simon Hunt56d51852014-11-09 13:03:35 -08002101 // augment host nodes...
2102 entering.filter('.host').each(function (d) {
2103 var node = d3.select(this),
Simon Hunt395a70c2014-11-22 23:17:40 -08002104 cfg = config.icons.host,
2105 r = cfg.radius[d.type] || cfg.defaultRadius,
Thomas Vachuska89543292014-11-19 11:28:33 -08002106 textDy = r + 10,
Simon Hunteb1514d2014-11-20 09:57:29 -08002107 iid = iconGlyphUrl(d);
Simon Hunt56d51852014-11-09 13:03:35 -08002108
Simon Hunt7cd48f32014-11-09 23:42:50 -08002109 // provide ref to element from backing data....
2110 d.el = node;
Simon Hunt434cf142014-11-24 11:10:28 -08002111 showHostVis(node);
Simon Hunt7cd48f32014-11-09 23:42:50 -08002112
Simon Hunt62c47542014-11-22 22:16:32 -08002113 node.append('circle').attr('r', r);
Simon Hunteb1514d2014-11-20 09:57:29 -08002114 if (iid) {
2115 addHostIcon(node, r, iid);
Simon Hunt7fa116d2014-11-17 14:16:55 -08002116 }
Simon Hunt56d51852014-11-09 13:03:35 -08002117 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08002118 .text(hostLabel)
Thomas Vachuska89543292014-11-19 11:28:33 -08002119 .attr('dy', textDy)
Simon Hunt7cd48f32014-11-09 23:42:50 -08002120 .attr('text-anchor', 'middle');
Simon Hunt56d51852014-11-09 13:03:35 -08002121 });
Simon Huntc7ee0662014-11-05 16:44:37 -08002122
Simon Hunt99c13842014-11-06 18:23:12 -08002123 // operate on both existing and new nodes, if necessary
Simon Huntb0ecfa52014-11-23 21:05:12 -08002124 updateDeviceColors();
Simon Huntc7ee0662014-11-05 16:44:37 -08002125
Simon Hunt99c13842014-11-06 18:23:12 -08002126 // operate on exiting nodes:
Simon Huntea80eb42014-11-11 13:46:57 -08002127 // Note that the node is removed after 2 seconds.
2128 // Sub element animations should be shorter than 2 seconds.
2129 var exiting = node.exit()
Simon Hunt44031102014-11-11 13:20:36 -08002130 .transition()
2131 .duration(2000)
Simon Huntea80eb42014-11-11 13:46:57 -08002132 .style('opacity', 0)
Simon Hunt99c13842014-11-06 18:23:12 -08002133 .remove();
Simon Huntea80eb42014-11-11 13:46:57 -08002134
2135 // host node exits....
2136 exiting.filter('.host').each(function (d) {
Simon Huntca867ac2014-11-28 18:07:35 -08002137 var node = d.el;
2138 node.select('use')
2139 .style('opacity', 0.5)
2140 .transition()
2141 .duration(800)
2142 .style('opacity', 0);
Simon Huntea80eb42014-11-11 13:46:57 -08002143
2144 node.select('text')
2145 .style('opacity', 0.5)
2146 .transition()
Simon Huntca867ac2014-11-28 18:07:35 -08002147 .duration(800)
Simon Huntea80eb42014-11-11 13:46:57 -08002148 .style('opacity', 0);
Simon Huntea80eb42014-11-11 13:46:57 -08002149
Thomas Vachuska89543292014-11-19 11:28:33 -08002150 node.select('circle')
2151 .style('stroke-fill', '#555')
2152 .style('fill', '#888')
Simon Huntea80eb42014-11-11 13:46:57 -08002153 .style('opacity', 0.5)
2154 .transition()
2155 .duration(1500)
2156 .attr('r', 0);
Simon Huntea80eb42014-11-11 13:46:57 -08002157 });
2158
Simon Huntca867ac2014-11-28 18:07:35 -08002159 // device node exits....
2160 exiting.filter('.device').each(function (d) {
2161 var node = d.el;
2162 node.select('use')
2163 .style('opacity', 0.5)
2164 .transition()
2165 .duration(800)
2166 .style('opacity', 0);
2167
2168 node.selectAll('rect')
2169 .style('stroke-fill', '#555')
2170 .style('fill', '#888')
2171 .style('opacity', 0.5);
2172 });
Simon Huntd6f5a272014-11-29 23:45:50 -08002173 fResume();
Simon Huntc7ee0662014-11-05 16:44:37 -08002174 }
2175
Simon Hunt95dad922014-11-24 09:43:31 -08002176 var dCol = {
2177 black: '#000',
2178 paleblue: '#acf',
2179 offwhite: '#ddd',
2180 midgrey: '#888',
2181 lightgrey: '#bbb',
2182 orange: '#f90'
2183 };
2184
Simon Huntb0ecfa52014-11-23 21:05:12 -08002185 // note: these are the device icon colors without affinity
Simon Hunt95dad922014-11-24 09:43:31 -08002186 var dColTheme = {
Simon Huntb0ecfa52014-11-23 21:05:12 -08002187 light: {
2188 online: {
Simon Hunt95dad922014-11-24 09:43:31 -08002189 glyph: dCol.black,
2190 rect: dCol.paleblue
Simon Huntb0ecfa52014-11-23 21:05:12 -08002191 },
2192 offline: {
Simon Hunt95dad922014-11-24 09:43:31 -08002193 glyph: dCol.midgrey,
2194 rect: dCol.lightgrey
Simon Huntb0ecfa52014-11-23 21:05:12 -08002195 }
2196 },
Simon Hunt95dad922014-11-24 09:43:31 -08002197 // TODO: theme
Simon Huntb0ecfa52014-11-23 21:05:12 -08002198 dark: {
2199 online: {
Simon Hunt95dad922014-11-24 09:43:31 -08002200 glyph: dCol.black,
2201 rect: dCol.paleblue
Simon Huntb0ecfa52014-11-23 21:05:12 -08002202 },
2203 offline: {
Simon Hunt95dad922014-11-24 09:43:31 -08002204 glyph: dCol.midgrey,
2205 rect: dCol.lightgrey
Simon Huntb0ecfa52014-11-23 21:05:12 -08002206 }
2207 }
2208 };
2209
2210 function devBaseColor(d) {
2211 var t = network.view.getTheme(),
2212 o = d.online ? 'online' : 'offline';
Simon Hunt95dad922014-11-24 09:43:31 -08002213 return dColTheme[t][o];
Simon Huntb0ecfa52014-11-23 21:05:12 -08002214 }
2215
2216 function setDeviceColor(d) {
2217 var o = d.online,
2218 s = d.el.classed('selected'),
2219 c = devBaseColor(d),
2220 a = instColor(d.master, o),
2221 g, r,
2222 icon = d.el.select('g.deviceIcon');
2223
2224 if (s) {
2225 g = c.glyph;
Simon Hunt95dad922014-11-24 09:43:31 -08002226 r = dColTheme.sel;
Simon Huntb0ecfa52014-11-23 21:05:12 -08002227 } else if (colorAffinity) {
2228 g = o ? a : c.glyph;
Simon Hunt95dad922014-11-24 09:43:31 -08002229 r = o ? dCol.offwhite : a;
Simon Huntb0ecfa52014-11-23 21:05:12 -08002230 } else {
2231 g = c.glyph;
2232 r = c.rect;
2233 }
2234
2235 icon.select('use')
2236 .style('fill', g);
2237 icon.select('rect')
2238 .style('fill', r);
2239 }
2240
Simon Huntc72967b2014-11-20 09:21:42 -08002241 function addDeviceIcon(node, box, noLabel, iid) {
2242 var cfg = config.icons.device,
2243 dx,
2244 dy,
2245 g;
2246
2247 if (noLabel) {
2248 box = emptyBox();
2249 dx = -cfg.dim/2;
2250 dy = -cfg.dim/2;
2251 } else {
2252 box = adjustRectToFitText(node);
Simon Hunt395a70c2014-11-22 23:17:40 -08002253 dx = box.x + cfg.xoff;
2254 dy = box.y + cfg.yoff;
Simon Huntc72967b2014-11-20 09:21:42 -08002255 }
2256
Simon Hunteb1514d2014-11-20 09:57:29 -08002257 g = node.append('g')
2258 .attr('class', 'glyphIcon deviceIcon')
Simon Huntc72967b2014-11-20 09:21:42 -08002259 .attr('transform', translate(dx, dy));
2260
2261 g.append('rect').attr({
2262 x: 0,
2263 y: 0,
2264 rx: cfg.rx,
2265 width: cfg.dim,
2266 height: cfg.dim
2267 });
2268
2269 g.append('use').attr({
2270 'xlink:href': iid,
2271 width: cfg.dim,
2272 height: cfg.dim
2273 });
2274
Simon Huntc72967b2014-11-20 09:21:42 -08002275 }
2276
Simon Hunt7b403bc2014-11-22 19:01:00 -08002277 function find(key, array, tag) {
Simon Huntca867ac2014-11-28 18:07:35 -08002278 var _tag = tag || 'id',
Simon Hunt7b403bc2014-11-22 19:01:00 -08002279 idx, n, d;
2280 for (idx = 0, n = array.length; idx < n; idx++) {
2281 d = array[idx];
2282 if (d[_tag] === key) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08002283 return idx;
2284 }
2285 }
2286 return -1;
2287 }
2288
Simon Huntca867ac2014-11-28 18:07:35 -08002289 function removeLinkElement(d) {
2290 var idx = find(d.key, network.links, 'key'),
Simon Hunt8257f4c2014-11-16 19:34:54 -08002291 removed;
2292 if (idx >=0) {
2293 // remove from links array
2294 removed = network.links.splice(idx, 1);
2295 // remove from lookup cache
2296 delete network.lookup[removed[0].key];
2297 updateLinks();
Simon Huntd6f5a272014-11-29 23:45:50 -08002298 fResume();
Simon Hunt8257f4c2014-11-16 19:34:54 -08002299 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08002300 }
Simon Huntc7ee0662014-11-05 16:44:37 -08002301
Simon Huntca867ac2014-11-28 18:07:35 -08002302 function removeHostElement(d, upd) {
2303 var lu = network.lookup;
Simon Hunt44031102014-11-11 13:20:36 -08002304 // first, remove associated hostLink...
Simon Huntca867ac2014-11-28 18:07:35 -08002305 removeLinkElement(d.linkData);
2306
2307 // remove hostLink bindings
2308 delete lu[d.ingress];
2309 delete lu[d.egress];
Simon Hunt44031102014-11-11 13:20:36 -08002310
2311 // remove from lookup cache
Simon Huntca867ac2014-11-28 18:07:35 -08002312 delete lu[d.id];
Simon Hunt44031102014-11-11 13:20:36 -08002313 // remove from nodes array
Simon Huntca867ac2014-11-28 18:07:35 -08002314 var idx = find(d.id, network.nodes);
2315 network.nodes.splice(idx, 1);
2316 // remove from SVG
2317 // NOTE: upd is false if we were called from removeDeviceElement()
2318 if (upd) {
2319 updateNodes();
Simon Huntd6f5a272014-11-29 23:45:50 -08002320 fResume();
Simon Huntca867ac2014-11-28 18:07:35 -08002321 }
2322 }
2323
2324
2325 function removeDeviceElement(d) {
2326 var id = d.id;
2327 // first, remove associated hosts and links..
2328 findAttachedHosts(id).forEach(removeHostElement);
2329 findAttachedLinks(id).forEach(removeLinkElement);
2330
2331 // remove from lookup cache
2332 delete network.lookup[id];
2333 // remove from nodes array
2334 var idx = find(id, network.nodes);
Simon Hunt44031102014-11-11 13:20:36 -08002335 network.nodes.splice(idx, 1);
2336 // remove from SVG
2337 updateNodes();
Simon Huntd6f5a272014-11-29 23:45:50 -08002338 fResume();
Simon Hunt44031102014-11-11 13:20:36 -08002339 }
2340
Simon Huntca867ac2014-11-28 18:07:35 -08002341 function findAttachedHosts(devId) {
2342 var hosts = [];
2343 network.nodes.forEach(function (d) {
2344 if (d.class === 'host' && d.cp.device === devId) {
2345 hosts.push(d);
2346 }
2347 });
2348 return hosts;
2349 }
2350
2351 function findAttachedLinks(devId) {
2352 var links = [];
2353 network.links.forEach(function (d) {
2354 if (d.source.id === devId || d.target.id === devId) {
2355 links.push(d);
2356 }
2357 });
2358 return links;
2359 }
Simon Hunt44031102014-11-11 13:20:36 -08002360
Simon Huntd6f5a272014-11-29 23:45:50 -08002361 function fResume() {
2362 if (!oblique) {
2363 network.force.resume();
2364 }
2365 }
Simon Huntc7ee0662014-11-05 16:44:37 -08002366
Simon Huntd6f5a272014-11-29 23:45:50 -08002367 function fStart() {
2368 if (!oblique) {
2369 network.force.start();
2370 }
2371 }
2372
2373 var tickStuff = {
2374 nodeAttr: {
2375 transform: function (d) { return translate(d.x, d.y); }
2376 },
2377 linkAttr: {
Simon Huntc7ee0662014-11-05 16:44:37 -08002378 x1: function (d) { return d.source.x; },
2379 y1: function (d) { return d.source.y; },
2380 x2: function (d) { return d.target.x; },
2381 y2: function (d) { return d.target.y; }
Simon Huntd6f5a272014-11-29 23:45:50 -08002382 },
2383 linkLabelAttr: {
2384 transform: function (d) {
2385 var lnk = findLinkById(d.key);
Simon Hunte2575b62014-11-18 15:25:53 -08002386
Simon Huntd6f5a272014-11-29 23:45:50 -08002387 if (lnk) {
2388 var parms = {
2389 x1: lnk.source.x,
2390 y1: lnk.source.y,
2391 x2: lnk.target.x,
2392 y2: lnk.target.y
2393 };
2394 return transformLabel(parms);
2395 }
Thomas Vachuska4731f122014-11-20 04:56:19 -08002396 }
Simon Huntd6f5a272014-11-29 23:45:50 -08002397 }
2398 };
2399
2400 function tick() {
2401 node.attr(tickStuff.nodeAttr);
2402 link.attr(tickStuff.linkAttr);
2403 linkLabel.attr(tickStuff.linkLabelAttr);
Simon Huntc7ee0662014-11-05 16:44:37 -08002404 }
Simon Hunt934c3ce2014-11-05 11:45:07 -08002405
2406 // ==============================
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002407 // Web-Socket for live data
2408
Thomas Vachuska12dfdc32014-11-29 16:03:12 -08002409 function findGuiSuccessor() {
2410 var idx = -1;
2411 onosOrder.forEach(function (d, i) {
2412 if (d.uiAttached) {
2413 idx = i;
2414 }
2415 });
2416
2417 for (var i = 0; i < onosOrder.length - 1; i++) {
2418 var ni = (idx + 1 + i) % onosOrder.length;
2419 if (onosOrder[ni].online) {
2420 return onosOrder[ni].ip;
2421 }
2422 }
2423 return null;
2424 }
2425
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002426 function webSockUrl() {
Thomas Vachuska12dfdc32014-11-29 16:03:12 -08002427 var url = document.location.toString()
2428 .replace(/\#.*/, '')
2429 .replace('http://', 'ws://')
2430 .replace('https://', 'wss://')
2431 .replace('index.html', config.webSockUrl);
2432 if (guiSuccessor) {
2433 url = url.replace(location.hostname, guiSuccessor);
2434 }
2435 return url;
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002436 }
2437
2438 webSock = {
2439 ws : null,
Thomas Vachuska12dfdc32014-11-29 16:03:12 -08002440 retries: 0,
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002441
2442 connect : function() {
2443 webSock.ws = new WebSocket(webSockUrl());
2444
2445 webSock.ws.onopen = function() {
Simon Hunt0c6d4192014-11-12 12:07:10 -08002446 noWebSock(false);
Thomas Vachuska47635c62014-11-22 01:21:36 -08002447 requestSummary();
Simon Huntb0ecfa52014-11-23 21:05:12 -08002448 showInstances();
Thomas Vachuska12dfdc32014-11-29 16:03:12 -08002449 webSock.retries = 0;
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002450 };
2451
2452 webSock.ws.onmessage = function(m) {
2453 if (m.data) {
Simon Huntbb282f52014-11-10 11:08:19 -08002454 wsTraceRx(m.data);
Thomas Vachuskad472c6e2014-11-07 19:11:05 -08002455 handleServerEvent(JSON.parse(m.data));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002456 }
2457 };
2458
2459 webSock.ws.onclose = function(m) {
2460 webSock.ws = null;
Thomas Vachuska12dfdc32014-11-29 16:03:12 -08002461 guiSuccessor = findGuiSuccessor();
2462 if (guiSuccessor && webSock.retries < onosOrder.length) {
2463 webSock.retries++;
2464 webSock.connect();
2465 } else {
2466 noWebSock(true);
2467 }
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002468 };
2469 },
2470
2471 send : function(text) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002472 if (text != null) {
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002473 webSock._send(text);
2474 }
2475 },
2476
2477 _send : function(message) {
2478 if (webSock.ws) {
2479 webSock.ws.send(message);
Simon Hunta255a2c2014-11-13 22:29:35 -08002480 } else if (config.useLiveData) {
Simon Hunt434cf142014-11-24 11:10:28 -08002481 console.warn('no web socket open', message);
Simon Hunta255a2c2014-11-13 22:29:35 -08002482 } else {
Simon Hunt434cf142014-11-24 11:10:28 -08002483 console.log('WS Send: ', message);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002484 }
2485 }
2486
2487 };
2488
Simon Hunt0c6d4192014-11-12 12:07:10 -08002489 function noWebSock(b) {
2490 mask.style('display',b ? 'block' : 'none');
2491 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002492
2493 function sendMessage(evType, payload) {
Simon Huntc1cc81c2014-11-29 14:59:01 -08002494 var p = payload || {},
2495 toSend = {
Simon Huntbb282f52014-11-10 11:08:19 -08002496 event: evType,
2497 sid: ++sid,
Simon Huntc1cc81c2014-11-29 14:59:01 -08002498 payload: p
Simon Huntbb282f52014-11-10 11:08:19 -08002499 },
2500 asText = JSON.stringify(toSend);
2501 wsTraceTx(asText);
2502 webSock.send(asText);
Simon Huntc76ae892014-11-18 17:31:51 -08002503
2504 // Temporary measure for debugging UI behavior ...
2505 if (!config.useLiveData) {
2506 handleTestSend(toSend);
2507 }
Simon Huntbb282f52014-11-10 11:08:19 -08002508 }
2509
2510 function wsTraceTx(msg) {
2511 wsTrace('tx', msg);
2512 }
2513 function wsTraceRx(msg) {
2514 wsTrace('rx', msg);
2515 }
2516 function wsTrace(rxtx, msg) {
Thomas Vachuska164fa5c2014-12-02 21:59:41 -08002517 // console.log('[' + rxtx + '] ' + msg);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002518 }
2519
Simon Huntc76ae892014-11-18 17:31:51 -08002520 // NOTE: Temporary hardcoded example for showing detail pane
2521 // while we fine-
2522 // Probably should not merge this change...
2523 function handleTestSend(msg) {
2524 if (msg.event === 'requestDetails') {
2525 showDetails({
2526 event: 'showDetails',
2527 sid: 1001,
2528 payload: {
2529 "id": "of:0000ffffffffff09",
2530 "type": "roadm",
2531 "propOrder": [
2532 "Name",
2533 "Vendor",
2534 "H/W Version",
2535 "S/W Version",
2536 "-",
2537 "Latitude",
2538 "Longitude",
2539 "Ports"
2540 ],
2541 "props": {
2542 "Name": null,
2543 "Vendor": "Linc",
2544 "H/W Version": "OE",
2545 "S/W Version": "?",
2546 "-": "",
2547 "Latitude": "40.8",
2548 "Longitude": "73.1",
2549 "Ports": "2"
2550 }
2551 }
2552 });
2553 }
2554 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002555
2556 // ==============================
2557 // Selection stuff
2558
2559 function selectObject(obj, el) {
2560 var n,
Simon Hunt01095ff2014-11-13 16:37:29 -08002561 srcEv = d3.event.sourceEvent,
2562 meta = srcEv.metaKey,
2563 shift = srcEv.shiftKey;
2564
Simon Hunt3c5ca542014-11-29 14:11:43 -08002565 // if the meta key is pressed, we are panning/zooming, so ignore
2566 if (meta) {
Simon Hunt01095ff2014-11-13 16:37:29 -08002567 return;
2568 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002569
2570 if (el) {
2571 n = d3.select(el);
2572 } else {
2573 node.each(function(d) {
2574 if (d == obj) {
2575 n = d3.select(el = this);
2576 }
2577 });
2578 }
2579 if (!n) return;
2580
Simon Hunt01095ff2014-11-13 16:37:29 -08002581 if (shift && n.classed('selected')) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002582 deselectObject(obj.id);
Simon Hunt61d04042014-11-11 17:27:16 -08002583 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002584 return;
2585 }
2586
Simon Hunt01095ff2014-11-13 16:37:29 -08002587 if (!shift) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002588 deselectAll();
2589 }
2590
Simon Huntc31d5692014-11-12 13:27:18 -08002591 selections[obj.id] = { obj: obj, el: el };
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002592 selectOrder.push(obj.id);
2593
2594 n.classed('selected', true);
Simon Huntb0ecfa52014-11-23 21:05:12 -08002595 updateDeviceColors(obj);
Simon Hunt61d04042014-11-11 17:27:16 -08002596 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002597 }
2598
2599 function deselectObject(id) {
Simon Huntc31d5692014-11-12 13:27:18 -08002600 var obj = selections[id],
2601 idx;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002602 if (obj) {
2603 d3.select(obj.el).classed('selected', false);
Simon Hunt61d04042014-11-11 17:27:16 -08002604 delete selections[id];
Simon Huntc31d5692014-11-12 13:27:18 -08002605 idx = $.inArray(id, selectOrder);
2606 if (idx >= 0) {
2607 selectOrder.splice(idx, 1);
2608 }
Simon Huntb0ecfa52014-11-23 21:05:12 -08002609 updateDeviceColors(obj.obj);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002610 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002611 }
2612
2613 function deselectAll() {
2614 // deselect all nodes in the network...
2615 node.classed('selected', false);
2616 selections = {};
2617 selectOrder = [];
Simon Huntb0ecfa52014-11-23 21:05:12 -08002618 updateDeviceColors();
Simon Hunt61d04042014-11-11 17:27:16 -08002619 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002620 }
2621
Simon Huntb0ecfa52014-11-23 21:05:12 -08002622 function updateDeviceColors(d) {
2623 if (d) {
2624 setDeviceColor(d);
2625 } else {
2626 node.filter('.device').each(function (d) {
2627 setDeviceColor(d);
2628 });
2629 }
Thomas Vachuska47635c62014-11-22 01:21:36 -08002630 }
2631
Simon Hunt61d04042014-11-11 17:27:16 -08002632 // update the state of the detail pane, based on current selections
2633 function updateDetailPane() {
2634 var nSel = selectOrder.length;
2635 if (!nSel) {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002636 emptySelect();
Simon Hunt61d04042014-11-11 17:27:16 -08002637 } else if (nSel === 1) {
2638 singleSelect();
2639 } else {
2640 multiSelect();
2641 }
2642 }
2643
Thomas Vachuska9edca302014-11-22 17:06:42 -08002644 function emptySelect() {
Simon Hunt27d322d2014-11-28 10:45:43 -08002645 haveDetails = false;
Simon Hunt06811b72014-11-25 18:54:48 -08002646 hideDetailPane();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002647 cancelTraffic();
2648 }
2649
Simon Hunt61d04042014-11-11 17:27:16 -08002650 function singleSelect() {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002651 // NOTE: detail is shown from showDetails event callback
Simon Hunt61d04042014-11-11 17:27:16 -08002652 requestDetails();
Thomas Vachuska164fa5c2014-12-02 21:59:41 -08002653 cancelTraffic();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002654 requestTrafficForMode();
Simon Hunt61d04042014-11-11 17:27:16 -08002655 }
2656
2657 function multiSelect() {
Simon Hunt27d322d2014-11-28 10:45:43 -08002658 haveDetails = true;
Simon Huntb53e0682014-11-12 13:32:01 -08002659 populateMultiSelect();
Thomas Vachuska164fa5c2014-12-02 21:59:41 -08002660 cancelTraffic();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002661 requestTrafficForMode();
Simon Huntb53e0682014-11-12 13:32:01 -08002662 }
2663
2664 function addSep(tbody) {
2665 var tr = tbody.append('tr');
2666 $('<hr>').appendTo(tr.append('td').attr('colspan', 2));
2667 }
2668
2669 function addProp(tbody, label, value) {
2670 var tr = tbody.append('tr');
2671
2672 tr.append('td')
2673 .attr('class', 'label')
2674 .text(label + ' :');
2675
2676 tr.append('td')
2677 .attr('class', 'value')
2678 .text(value);
2679 }
2680
2681 function populateMultiSelect() {
2682 detailPane.empty();
2683
Simon Hunta3dd9572014-11-20 15:22:41 -08002684 var title = detailPane.append('h3'),
2685 table = detailPane.append('table'),
2686 tbody = table.append('tbody');
Simon Huntb53e0682014-11-12 13:32:01 -08002687
Thomas Vachuska4731f122014-11-20 04:56:19 -08002688 title.text('Selected Nodes');
Simon Huntb53e0682014-11-12 13:32:01 -08002689
2690 selectOrder.forEach(function (d, i) {
2691 addProp(tbody, i+1, d);
2692 });
Simon Huntd72bc702014-11-13 18:38:04 -08002693
2694 addMultiSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08002695 }
2696
Thomas Vachuska47635c62014-11-22 01:21:36 -08002697 // TODO: refactor to consolidate with populateDetails
2698 function populateSummary(data) {
2699 summaryPane.empty();
2700
2701 var svg = summaryPane.append('svg'),
2702 iid = iconGlyphUrl(data);
2703
2704 var title = summaryPane.append('h2'),
2705 table = summaryPane.append('table'),
2706 tbody = table.append('tbody');
2707
2708 appendGlyph(svg, 0, 0, 40, iid);
2709
2710 svg.append('use')
2711 .attr({
2712 class: 'birdBadge',
2713 transform: translate(8,12),
2714 'xlink:href': '#bird',
2715 width: 24,
2716 height: 24,
2717 fill: '#fff'
2718 });
2719
2720 title.text('ONOS Summary');
2721
2722 data.propOrder.forEach(function(p) {
2723 if (p === '-') {
2724 addSep(tbody);
2725 } else {
2726 addProp(tbody, p, data.props[p]);
2727 }
2728 });
2729 }
2730
Simon Hunt61d04042014-11-11 17:27:16 -08002731 function populateDetails(data) {
2732 detailPane.empty();
2733
Simon Hunta6a9fe72014-11-20 11:17:12 -08002734 var svg = detailPane.append('svg'),
2735 iid = iconGlyphUrl(data);
2736
Simon Hunta3dd9572014-11-20 15:22:41 -08002737 var title = detailPane.append('h2'),
2738 table = detailPane.append('table'),
2739 tbody = table.append('tbody');
Simon Hunt61d04042014-11-11 17:27:16 -08002740
Simon Hunta6a9fe72014-11-20 11:17:12 -08002741 appendGlyph(svg, 0, 0, 40, iid);
2742 title.text(data.id);
Simon Hunt61d04042014-11-11 17:27:16 -08002743
2744 data.propOrder.forEach(function(p) {
2745 if (p === '-') {
2746 addSep(tbody);
2747 } else {
2748 addProp(tbody, p, data.props[p]);
2749 }
2750 });
Simon Huntd72bc702014-11-13 18:38:04 -08002751
Thomas Vachuska4731f122014-11-20 04:56:19 -08002752 addSingleSelectActions(data);
Simon Hunt61d04042014-11-11 17:27:16 -08002753 }
2754
Thomas Vachuska4731f122014-11-20 04:56:19 -08002755 function addSingleSelectActions(data) {
Simon Huntd72bc702014-11-13 18:38:04 -08002756 detailPane.append('hr');
2757 // always want to allow 'show traffic'
Thomas Vachuska164fa5c2014-12-02 21:59:41 -08002758 addAction(detailPane, 'Show Related Traffic', showRelatedIntentsAction);
Thomas Vachuska4731f122014-11-20 04:56:19 -08002759
2760 if (data.type === 'switch') {
2761 addAction(detailPane, 'Show Device Flows', showDeviceLinkFlowsAction);
2762 }
Simon Huntd72bc702014-11-13 18:38:04 -08002763 }
2764
2765 function addMultiSelectActions() {
2766 detailPane.append('hr');
2767 // always want to allow 'show traffic'
Thomas Vachuska164fa5c2014-12-02 21:59:41 -08002768 addAction(detailPane, 'Show Related Traffic', showRelatedIntentsAction);
Simon Huntd72bc702014-11-13 18:38:04 -08002769 // if exactly two hosts are selected, also want 'add host intent'
2770 if (nSel() === 2 && allSelectionsClass('host')) {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002771 addAction(detailPane, 'Create Host-to-Host Flow', addHostIntentAction);
2772 } else if (nSel() >= 2 && allSelectionsClass('host')) {
2773 addAction(detailPane, 'Create Multi-Source Flow', addMultiSourceIntentAction);
Simon Huntd72bc702014-11-13 18:38:04 -08002774 }
2775 }
2776
Simon Hunta5e89142014-11-14 07:00:33 -08002777 function addAction(panel, text, cb) {
2778 panel.append('div')
Simon Huntd72bc702014-11-13 18:38:04 -08002779 .classed('actionBtn', true)
2780 .text(text)
2781 .on('click', cb);
2782 }
2783
2784
Simon Hunt3c5ca542014-11-29 14:11:43 -08002785 // === Pan and Zoom behaviors...
2786
2787 function panZoom(translate, scale) {
2788 panZoomContainer.attr('transform',
2789 'translate(' + translate + ')scale(' + scale + ')');
Paul Greysonfcba0e82014-11-13 10:21:16 -08002790 // keep the map lines constant width while zooming
Simon Hunt3c5ca542014-11-29 14:11:43 -08002791 bgImg.style('stroke-width', 2.0 / scale + 'px');
Paul Greysonfcba0e82014-11-13 10:21:16 -08002792 }
2793
Simon Hunt3c5ca542014-11-29 14:11:43 -08002794 function resetPanZoom() {
2795 panZoom([0,0], 1);
2796 zoom.translate([0,0]).scale(1);
Paul Greysonfcba0e82014-11-13 10:21:16 -08002797 }
2798
Simon Hunt3c5ca542014-11-29 14:11:43 -08002799 function setupPanZoom() {
Paul Greysonfcba0e82014-11-13 10:21:16 -08002800 function zoomed() {
Simon Hunt3c5ca542014-11-29 14:11:43 -08002801 // pan zoom active when meta key is pressed...
2802 if (d3.event.sourceEvent.metaKey) {
2803 panZoom(d3.event.translate, d3.event.scale);
Paul Greysonfcba0e82014-11-13 10:21:16 -08002804 }
2805 }
2806
2807 zoom = d3.behavior.zoom()
2808 .translate([0, 0])
2809 .scale(1)
Simon Hunt1b18aa52014-11-29 17:57:55 -08002810 .scaleExtent([0.25, 10])
Paul Greysonfcba0e82014-11-13 10:21:16 -08002811 .on("zoom", zoomed);
2812
2813 svg.call(zoom);
2814 }
2815
Simon Hunt61d04042014-11-11 17:27:16 -08002816 // ==============================
2817 // Test harness code
Simon Hunt56d51852014-11-09 13:03:35 -08002818
2819 function prepareScenario(view, ctx, dbg) {
2820 var sc = scenario,
2821 urlSc = sc.evDir + ctx + sc.evScenario;
2822
2823 if (!ctx) {
2824 view.alert("No scenario specified (null ctx)");
2825 return;
2826 }
2827
2828 sc.view = view;
2829 sc.ctx = ctx;
2830 sc.debug = dbg;
2831 sc.evNumber = 0;
2832
2833 d3.json(urlSc, function(err, data) {
Simon Huntbb282f52014-11-10 11:08:19 -08002834 var p = data && data.params || {},
2835 desc = data && data.description || null,
Simon Huntfc274c92014-11-11 11:05:46 -08002836 intro = data && data.title;
Simon Huntbb282f52014-11-10 11:08:19 -08002837
Simon Hunt56d51852014-11-09 13:03:35 -08002838 if (err) {
2839 view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err);
2840 } else {
2841 sc.params = p;
Simon Huntbb282f52014-11-10 11:08:19 -08002842 if (desc) {
2843 intro += '\n\n ' + desc.join('\n ');
2844 }
2845 view.alert(intro);
Simon Hunt56d51852014-11-09 13:03:35 -08002846 }
2847 });
2848
2849 }
2850
Simon Hunt9f1bced2014-12-02 14:36:39 -08002851 function setupDefs(svg) {
Simon Hunt7fa116d2014-11-17 14:16:55 -08002852 var defs = svg.append('defs');
Simon Hunt9f1bced2014-12-02 14:36:39 -08002853 gly.loadDefs(defs);
2854 d3u.loadGlow(defs);
Simon Hunt7fa116d2014-11-17 14:16:55 -08002855 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002856
Simon Hunt395a70c2014-11-22 23:17:40 -08002857 function sendUpdateMeta(d, store) {
2858 var metaUi = {},
2859 ll;
2860
2861 if (store) {
Simon Hunt1b18aa52014-11-29 17:57:55 -08002862 ll = geoMapProj.invert([d.x, d.y]);
Simon Hunt62c47542014-11-22 22:16:32 -08002863 metaUi = {
2864 x: d.x,
2865 y: d.y,
2866 lng: ll[0],
2867 lat: ll[1]
2868 };
Simon Hunt395a70c2014-11-22 23:17:40 -08002869 }
Simon Hunt62c47542014-11-22 22:16:32 -08002870 d.metaUi = metaUi;
2871 sendMessage('updateMeta', {
2872 id: d.id,
2873 'class': d.class,
Simon Huntc1cc81c2014-11-29 14:59:01 -08002874 memento: metaUi
Simon Hunt62c47542014-11-22 22:16:32 -08002875 });
2876 }
2877
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002878 // ==============================
Simon Hunt142d0032014-11-04 20:13:09 -08002879 // View life-cycle callbacks
Simon Hunt195cb382014-11-03 17:50:51 -08002880
Simon Hunta2994cc2014-12-02 14:19:15 -08002881 function init(view, ctx, flags) {
Simon Hunt142d0032014-11-04 20:13:09 -08002882 var w = view.width(),
2883 h = view.height(),
Simon Hunt1b18aa52014-11-29 17:57:55 -08002884 fcfg = config.force;
Simon Hunt195cb382014-11-03 17:50:51 -08002885
Simon Hunt142d0032014-11-04 20:13:09 -08002886 // NOTE: view.$div is a D3 selection of the view's div
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002887 var viewBox = '0 0 ' + config.logicalSize + ' ' + config.logicalSize;
2888 svg = view.$div.append('svg').attr('viewBox', viewBox);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002889 setSize(svg, view);
2890
Simon Hunt9f1bced2014-12-02 14:36:39 -08002891 // load glyphs, filters, and other definitions...
2892 setupDefs(svg);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002893
Simon Hunt3c5ca542014-11-29 14:11:43 -08002894 panZoomContainer = svg.append('g').attr('id', 'panZoomContainer');
2895 setupPanZoom();
Paul Greysonfcba0e82014-11-13 10:21:16 -08002896
Simon Huntc7ee0662014-11-05 16:44:37 -08002897 // group for the topology
Simon Hunt3c5ca542014-11-29 14:11:43 -08002898 topoG = panZoomContainer.append('g')
Simon Hunt1b18aa52014-11-29 17:57:55 -08002899 .attr('id', 'topo-G');
Simon Huntc7ee0662014-11-05 16:44:37 -08002900
Simon Hunte2575b62014-11-18 15:25:53 -08002901 // subgroups for links, link labels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002902 linkG = topoG.append('g').attr('id', 'links');
Simon Hunte2575b62014-11-18 15:25:53 -08002903 linkLabelG = topoG.append('g').attr('id', 'linkLabels');
Simon Huntc7ee0662014-11-05 16:44:37 -08002904 nodeG = topoG.append('g').attr('id', 'nodes');
2905
Simon Hunte2575b62014-11-18 15:25:53 -08002906 // selection of links, linkLabels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002907 link = linkG.selectAll('.link');
Simon Hunte2575b62014-11-18 15:25:53 -08002908 linkLabel = linkLabelG.selectAll('.linkLabel');
Simon Huntc7ee0662014-11-05 16:44:37 -08002909 node = nodeG.selectAll('.node');
2910
Simon Hunt7cd48f32014-11-09 23:42:50 -08002911 function chrg(d) {
2912 return fcfg.charge[d.class] || -12000;
2913 }
Simon Hunt99c13842014-11-06 18:23:12 -08002914 function ldist(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002915 return fcfg.linkDistance[d.type] || 50;
Simon Hunt99c13842014-11-06 18:23:12 -08002916 }
2917 function lstrg(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002918 // 0.0 - 1.0
2919 return fcfg.linkStrength[d.type] || 1.0;
Simon Hunt99c13842014-11-06 18:23:12 -08002920 }
2921
Simon Hunt1a9eff92014-11-07 11:06:34 -08002922 function selectCb(d, self) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002923 selectObject(d, self);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002924 }
2925
2926 function atDragEnd(d, self) {
Simon Hunt56d51852014-11-09 13:03:35 -08002927 // once we've finished moving, pin the node in position
2928 d.fixed = true;
2929 d3.select(self).classed('fixed', true);
2930 if (config.useLiveData) {
Simon Hunt395a70c2014-11-22 23:17:40 -08002931 sendUpdateMeta(d, true);
Simon Hunta255a2c2014-11-13 22:29:35 -08002932 } else {
2933 console.log('Moving node ' + d.id + ' to [' + d.x + ',' + d.y + ']');
Simon Hunt1a9eff92014-11-07 11:06:34 -08002934 }
2935 }
2936
Simon Hunt6e18fe32014-11-29 13:35:41 -08002937 // predicate that indicates when dragging is active
2938 function dragEnabled() {
2939 // meta key pressed means we are zooming/panning (so disable drag)
Simon Huntc2367d52014-11-29 19:30:23 -08002940 return !nodeLock && !d3.event.sourceEvent.metaKey;
2941 }
2942
2943 // predicate that indicates when clicking is active
2944 function clickEnabled() {
2945 return true;
Simon Hunt6e18fe32014-11-29 13:35:41 -08002946 }
2947
Simon Huntc7ee0662014-11-05 16:44:37 -08002948 // set up the force layout
2949 network.force = d3.layout.force()
Simon Hunt1b18aa52014-11-29 17:57:55 -08002950 .size([w, h])
Simon Huntc7ee0662014-11-05 16:44:37 -08002951 .nodes(network.nodes)
2952 .links(network.links)
Simon Hunt7cd48f32014-11-09 23:42:50 -08002953 .gravity(0.4)
2954 .friction(0.7)
2955 .charge(chrg)
Simon Hunt99c13842014-11-06 18:23:12 -08002956 .linkDistance(ldist)
2957 .linkStrength(lstrg)
Simon Huntc7ee0662014-11-05 16:44:37 -08002958 .on('tick', tick);
Simon Hunt195cb382014-11-03 17:50:51 -08002959
Simon Hunt01095ff2014-11-13 16:37:29 -08002960 network.drag = d3u.createDragBehavior(network.force,
Simon Huntc2367d52014-11-29 19:30:23 -08002961 selectCb, atDragEnd, dragEnabled, clickEnabled);
Simon Hunt6e18fe32014-11-29 13:35:41 -08002962
Simon Hunt0c6d4192014-11-12 12:07:10 -08002963
2964 // create mask layer for when we lose connection to server.
Simon Hunta5e89142014-11-14 07:00:33 -08002965 // TODO: this should be part of the framework
Simon Hunt6e18fe32014-11-29 13:35:41 -08002966
2967 function para(sel, text) {
2968 sel.append('p').text(text);
2969 }
2970
Simon Hunt0c6d4192014-11-12 12:07:10 -08002971 mask = view.$div.append('div').attr('id','topo-mask');
2972 para(mask, 'Oops!');
2973 para(mask, 'Web-socket connection to server closed...');
2974 para(mask, 'Try refreshing the page.');
Simon Hunt12ce12e2014-11-15 21:13:19 -08002975
2976 mask.append('svg')
2977 .attr({
2978 id: 'mask-bird',
2979 width: w,
2980 height: h
2981 })
2982 .append('g')
2983 .attr('transform', birdTranslate(w, h))
2984 .style('opacity', 0.3)
2985 .append('use')
2986 .attr({
2987 'xlink:href': '#bird',
2988 width: config.birdDim,
2989 height: config.birdDim,
2990 fill: '#111'
Thomas Vachuska89543292014-11-19 11:28:33 -08002991 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08002992 }
Simon Hunt195cb382014-11-03 17:50:51 -08002993
Simon Hunt01095ff2014-11-13 16:37:29 -08002994
Simon Hunt56d51852014-11-09 13:03:35 -08002995 function load(view, ctx, flags) {
Simon Huntf67722a2014-11-10 09:32:06 -08002996 // resize, in case the window was resized while we were not loaded
2997 resize(view, ctx, flags);
2998
Simon Hunt99c13842014-11-06 18:23:12 -08002999 // cache the view token, so network topo functions can access it
3000 network.view = view;
Simon Hunt56d51852014-11-09 13:03:35 -08003001 config.useLiveData = !flags.local;
3002
3003 if (!config.useLiveData) {
3004 prepareScenario(view, ctx, flags.debug);
3005 }
Simon Hunt99c13842014-11-06 18:23:12 -08003006
3007 // set our radio buttons and key bindings
Simon Hunt9462e8c2014-11-14 17:28:09 -08003008 layerBtnSet = view.setRadio(layerButtons);
Simon Hunt934c3ce2014-11-05 11:45:07 -08003009 view.setKeys(keyDispatch);
Simon Hunt87514342014-11-24 16:41:27 -08003010 view.setGestures(gestures);
Simon Hunt195cb382014-11-03 17:50:51 -08003011
Simon Huntf8e5b4e02014-11-13 11:17:57 -08003012 // patch in our "button bar" for now
3013 // TODO: implement a more official frameworky way of doing this..
Simon Hunt87514342014-11-24 16:41:27 -08003014 //addButtonBar(view);
Simon Huntf8e5b4e02014-11-13 11:17:57 -08003015
Simon Huntd3b7d512014-11-12 15:48:41 -08003016 // Load map data asynchronously; complete startup after that..
3017 loadGeoJsonData();
Simon Hunta255a2c2014-11-13 22:29:35 -08003018 }
3019
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08003020 function startAntTimer() {
Thomas Vachuska4731f122014-11-20 04:56:19 -08003021 if (!antTimer) {
3022 var pulses = [5, 3, 1.2, 3],
3023 pulse = 0;
3024 antTimer = setInterval(function () {
3025 pulse = pulse + 1;
3026 pulse = pulse === pulses.length ? 0 : pulse;
3027 d3.selectAll('.animated').style('stroke-width', pulses[pulse]);
3028 }, 200);
3029 }
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08003030 }
3031
3032 function stopAntTimer() {
Simon Hunta255a2c2014-11-13 22:29:35 -08003033 if (antTimer) {
3034 clearInterval(antTimer);
3035 antTimer = null;
3036 }
Simon Huntd3b7d512014-11-12 15:48:41 -08003037 }
3038
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08003039 function unload(view, ctx, flags) {
3040 stopAntTimer();
3041 }
3042
Simon Huntd3b7d512014-11-12 15:48:41 -08003043 // TODO: move these to config/state portion of script
Simon Hunta6a9fe72014-11-20 11:17:12 -08003044 var geoJsonUrl = 'json/map/continental_us.json',
Simon Huntd3b7d512014-11-12 15:48:41 -08003045 geoJson;
3046
3047 function loadGeoJsonData() {
3048 d3.json(geoJsonUrl, function (err, data) {
3049 if (err) {
3050 // fall back to USA map background
3051 loadStaticMap();
3052 } else {
3053 geoJson = data;
3054 loadGeoMap();
3055 }
3056
3057 // finally, connect to the server...
3058 if (config.useLiveData) {
3059 webSock.connect();
3060 }
3061 });
3062 }
3063
3064 function showBg() {
Simon Hunt434cf142014-11-24 11:10:28 -08003065 return visVal(config.options.showBackground);
Simon Huntd3b7d512014-11-12 15:48:41 -08003066 }
3067
3068 function loadStaticMap() {
3069 fnTrace('loadStaticMap', config.backgroundUrl);
3070 var w = network.view.width(),
3071 h = network.view.height();
3072
3073 // load the background image
3074 bgImg = svg.insert('svg:image', '#topo-G')
3075 .attr({
3076 id: 'topo-bg',
3077 width: w,
3078 height: h,
3079 'xlink:href': config.backgroundUrl
3080 })
3081 .style({
3082 visibility: showBg()
3083 });
3084 }
3085
Simon Hunt1b18aa52014-11-29 17:57:55 -08003086 function setProjForView(path, topoData) {
3087 var dim = config.logicalSize;
3088
3089 // start with unit scale, no translation..
3090 geoMapProj.scale(1).translate([0, 0]);
3091
3092 // figure out dimensions of map data..
3093 var b = path.bounds(topoData),
3094 x1 = b[0][0],
3095 y1 = b[0][1],
3096 x2 = b[1][0],
3097 y2 = b[1][1],
3098 dx = x2 - x1,
3099 dy = y2 - y1,
3100 x = (x1 + x2) / 2,
3101 y = (y1 + y2) / 2;
3102
3103 // size map to 95% of minimum dimension to fill space..
3104 var s = .95 / Math.min(dx / dim, dy / dim);
3105 var t = [dim / 2 - s * x, dim / 2 - s * y];
3106
3107 // set new scale, translation on the projection..
3108 geoMapProj.scale(s).translate(t);
3109 }
3110
Simon Huntd3b7d512014-11-12 15:48:41 -08003111 function loadGeoMap() {
3112 fnTrace('loadGeoMap', geoJsonUrl);
Simon Huntd3b7d512014-11-12 15:48:41 -08003113
Paul Greyson6cb8ca02014-11-12 18:09:02 -08003114 // extracts the topojson data into geocoordinate-based geometry
3115 var topoData = topojson.feature(geoJson, geoJson.objects.states);
Simon Huntd3b7d512014-11-12 15:48:41 -08003116
Paul Greyson6cb8ca02014-11-12 18:09:02 -08003117 // see: http://bl.ocks.org/mbostock/4707858
Simon Hunt1b18aa52014-11-29 17:57:55 -08003118 geoMapProj = d3.geo.mercator();
3119 var path = d3.geo.path().projection(geoMapProj);
Simon Huntd3b7d512014-11-12 15:48:41 -08003120
Simon Hunt1b18aa52014-11-29 17:57:55 -08003121 setProjForView(path, topoData);
Paul Greyson6cb8ca02014-11-12 18:09:02 -08003122
Simon Hunt3c5ca542014-11-29 14:11:43 -08003123 bgImg = panZoomContainer.insert("g", '#topo-G');
Thomas Vachuska89543292014-11-19 11:28:33 -08003124 bgImg.attr('id', 'map').selectAll('path')
3125 .data(topoData.features)
3126 .enter()
3127 .append('path')
3128 .attr('d', path);
Simon Hunt195cb382014-11-03 17:50:51 -08003129 }
3130
Simon Huntf67722a2014-11-10 09:32:06 -08003131 function resize(view, ctx, flags) {
Simon Hunt12ce12e2014-11-15 21:13:19 -08003132 var w = view.width(),
3133 h = view.height();
3134
Simon Hunt934c3ce2014-11-05 11:45:07 -08003135 setSize(svg, view);
Simon Hunt12ce12e2014-11-15 21:13:19 -08003136
3137 d3.select('#mask-bird').attr({ width: w, height: h})
3138 .select('g').attr('transform', birdTranslate(w, h));
Simon Hunt142d0032014-11-04 20:13:09 -08003139 }
3140
Simon Hunt8f40cce2014-11-23 15:57:30 -08003141 function theme(view, ctx, flags) {
3142 updateInstances();
Simon Huntb0ecfa52014-11-23 21:05:12 -08003143 updateDeviceColors();
Simon Hunt8f40cce2014-11-23 15:57:30 -08003144 }
3145
Simon Hunt12ce12e2014-11-15 21:13:19 -08003146 function birdTranslate(w, h) {
3147 var bdim = config.birdDim;
3148 return 'translate('+((w-bdim)*.4)+','+((h-bdim)*.1)+')';
3149 }
Simon Hunt142d0032014-11-04 20:13:09 -08003150
Simon Hunt06811b72014-11-25 18:54:48 -08003151 function isF(f) { return $.isFunction(f) ? f : null; }
3152 function noop() {}
3153
3154 function augmentDetailPane() {
3155 var dp = detailPane;
3156 dp.ypos = { up: 64, down: 320, current: 320};
3157
3158 dp._move = function (y, cb) {
3159 var endCb = isF(cb) || noop,
3160 yp = dp.ypos;
3161 if (yp.current !== y) {
3162 yp.current = y;
3163 dp.el.transition().duration(300)
3164 .each('end', endCb)
3165 .style('top', yp.current + 'px');
3166 } else {
3167 endCb();
3168 }
3169 };
3170
3171 dp.down = function (cb) { dp._move(dp.ypos.down, cb); };
3172 dp.up = function (cb) { dp._move(dp.ypos.up, cb); };
3173 }
3174
Simon Hunt142d0032014-11-04 20:13:09 -08003175 // ==============================
3176 // View registration
Simon Hunt195cb382014-11-03 17:50:51 -08003177
Simon Hunt25248912014-11-04 11:25:48 -08003178 onos.ui.addView('topo', {
Simon Hunta2994cc2014-12-02 14:19:15 -08003179 init: init,
Simon Hunt142d0032014-11-04 20:13:09 -08003180 load: load,
Simon Hunta255a2c2014-11-13 22:29:35 -08003181 unload: unload,
Simon Hunt8f40cce2014-11-23 15:57:30 -08003182 resize: resize,
3183 theme: theme
Simon Hunt195cb382014-11-03 17:50:51 -08003184 });
3185
Thomas Vachuska47635c62014-11-22 01:21:36 -08003186 summaryPane = onos.ui.addFloatingPanel('topo-summary');
Simon Hunt61d04042014-11-11 17:27:16 -08003187 detailPane = onos.ui.addFloatingPanel('topo-detail');
Simon Hunt06811b72014-11-25 18:54:48 -08003188 augmentDetailPane();
Simon Hunta5e89142014-11-14 07:00:33 -08003189 oiBox = onos.ui.addFloatingPanel('topo-oibox', 'TL');
Simon Huntb82f6902014-11-22 11:53:15 -08003190 oiBox.width(20);
Simon Hunt61d04042014-11-11 17:27:16 -08003191
Simon Hunt195cb382014-11-03 17:50:51 -08003192}(ONOS));