blob: 2af25c718969e01303f2f4b89759988cd3f14c3d [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'],
Thomas Vachuskab7e40642014-12-03 11:16:06 -0800149 rightArrow: [showNextIntentAction, 'Show next related intent'],
150 leftArrow: [showPrevIntentAction, 'Show previous related intent'],
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800151 W: [showSelectedIntentTrafficAction, 'Monitor traffic of selected intent'],
152 A: [showAllTrafficAction, 'Monitor all traffic'],
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800153 F: [showDeviceLinkFlowsAction, 'Show device link flows'],
Simon Huntc2367d52014-11-29 19:30:23 -0800154 X: [toggleNodeLock, 'Lock / unlock node positions'],
Simon Huntd6f5a272014-11-29 23:45:50 -0800155 Z: [toggleOblique, 'Toggle oblique view (Experimental)'],
Simon Hunt9462e8c2014-11-14 17:28:09 -0800156 esc: handleEscape
Simon Hunt934c3ce2014-11-05 11:45:07 -0800157 };
Simon Hunt142d0032014-11-04 20:13:09 -0800158
Simon Hunt87514342014-11-24 16:41:27 -0800159 // mouse gestures
160 var gestures = [
161 ['click', 'Select the item and show details'],
162 ['shift-click', 'Toggle selection state'],
163 ['drag', 'Reposition (and pin) device / host'],
164 ['cmd-scroll', 'Zoom in / out'],
165 ['cmd-drag', 'Pan']
166 ];
167
Simon Hunt195cb382014-11-03 17:50:51 -0800168 // state variables
Simon Hunt99c13842014-11-06 18:23:12 -0800169 var network = {
Simon Hunt50128c02014-11-08 13:36:15 -0800170 view: null, // view token reference
Simon Hunt99c13842014-11-06 18:23:12 -0800171 nodes: [],
172 links: [],
Simon Hunt269670f2014-11-17 16:17:43 -0800173 lookup: {},
174 revLinkToKey: {}
Simon Hunt99c13842014-11-06 18:23:12 -0800175 },
Simon Hunt56d51852014-11-09 13:03:35 -0800176 scenario = {
177 evDir: 'json/ev/',
178 evScenario: '/scenario.json',
179 evPrefix: '/ev_',
180 evOnos: '_onos.json',
181 evUi: '_ui.json',
182 ctx: null,
183 params: {},
184 evNumber: 0,
Simon Hunt434cf142014-11-24 11:10:28 -0800185 view: null
Simon Hunt56d51852014-11-09 13:03:35 -0800186 },
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800187 webSock,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800188 sid = 0,
Thomas Vachuska60d72bf2014-11-21 13:02:00 -0800189 deviceLabelCount = 3,
Simon Hunt209155e2014-11-21 12:16:09 -0800190 hostLabelCount = 2,
Simon Hunt56d51852014-11-09 13:03:35 -0800191 deviceLabelIndex = 0,
192 hostLabelIndex = 0,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800193 selections = {},
Simon Hunta5e89142014-11-14 07:00:33 -0800194 selectOrder = [],
Simon Hunt6ac93f32014-11-13 12:17:27 -0800195 hovered = null,
Thomas Vachuska47635c62014-11-22 01:21:36 -0800196 summaryPane,
Simon Hunta5e89142014-11-14 07:00:33 -0800197 detailPane,
Simon Hunta255a2c2014-11-13 22:29:35 -0800198 antTimer = null,
Thomas Vachuska12dfdc32014-11-29 16:03:12 -0800199 guiSuccessor = null,
Simon Hunta5e89142014-11-14 07:00:33 -0800200 onosInstances = {},
201 onosOrder = [],
202 oiBox,
Simon Hunt9462e8c2014-11-14 17:28:09 -0800203 oiShowMaster = false,
Simon Hunt8f40cce2014-11-23 15:57:30 -0800204 portLabelsOn = false,
Simon Huntb0ecfa52014-11-23 21:05:12 -0800205 cat7 = d3u.cat7(),
Simon Hunt434cf142014-11-24 11:10:28 -0800206 colorAffinity = false,
Simon Hunt27d322d2014-11-28 10:45:43 -0800207 showHosts = false,
Simon Hunt6d9bd032014-11-28 22:16:40 -0800208 showOffline = true,
Simon Hunt27d322d2014-11-28 10:45:43 -0800209 useDetails = true,
Simon Hunt6e18fe32014-11-29 13:35:41 -0800210 haveDetails = false,
Simon Huntc2367d52014-11-29 19:30:23 -0800211 nodeLock = false,
212 oblique = false;
Simon Hunt195cb382014-11-03 17:50:51 -0800213
Simon Hunt434cf142014-11-24 11:10:28 -0800214 // constants
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800215 var hoverModeNone = 0,
216 hoverModeAll = 1,
Thomas Vachuska9edca302014-11-22 17:06:42 -0800217 hoverModeFlows = 2,
218 hoverModeIntents = 3,
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800219 hoverMode = hoverModeNone;
Thomas Vachuska9edca302014-11-22 17:06:42 -0800220
Simon Hunt934c3ce2014-11-05 11:45:07 -0800221 // D3 selections
222 var svg,
Simon Hunt3c5ca542014-11-29 14:11:43 -0800223 panZoomContainer,
Simon Hunt934c3ce2014-11-05 11:45:07 -0800224 bgImg,
Simon Huntc7ee0662014-11-05 16:44:37 -0800225 topoG,
226 nodeG,
227 linkG,
Simon Hunte2575b62014-11-18 15:25:53 -0800228 linkLabelG,
Simon Huntc7ee0662014-11-05 16:44:37 -0800229 node,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800230 link,
Simon Hunte2575b62014-11-18 15:25:53 -0800231 linkLabel,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800232 mask;
Simon Hunt195cb382014-11-03 17:50:51 -0800233
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800234 // the projection for the map background
Simon Hunt1b18aa52014-11-29 17:57:55 -0800235 var geoMapProj;
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800236
Paul Greysonfcba0e82014-11-13 10:21:16 -0800237 // the zoom function
238 var zoom;
239
Simon Hunt142d0032014-11-04 20:13:09 -0800240 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800241 // For Debugging / Development
Simon Hunt195cb382014-11-03 17:50:51 -0800242
Simon Hunt99c13842014-11-06 18:23:12 -0800243 function note(label, msg) {
244 console.log('NOTE: ' + label + ': ' + msg);
Simon Hunt195cb382014-11-03 17:50:51 -0800245 }
246
Simon Hunt99c13842014-11-06 18:23:12 -0800247 function debug(what) {
248 return config.debugOn && config.debug[what];
Simon Hunt934c3ce2014-11-05 11:45:07 -0800249 }
250
Simon Huntfc274c92014-11-11 11:05:46 -0800251 function fnTrace(msg, id) {
252 if (config.fnTrace) {
253 console.log('FN: ' + msg + ' [' + id + ']');
254 }
255 }
Simon Hunt99c13842014-11-06 18:23:12 -0800256
Simon Hunta5e89142014-11-14 07:00:33 -0800257 function evTrace(data) {
258 fnTrace(data.event, data.payload.id);
259 }
260
Simon Hunt934c3ce2014-11-05 11:45:07 -0800261 // ==============================
262 // Key Callbacks
263
Simon Hunt27d322d2014-11-28 10:45:43 -0800264 function flash(txt) {
265 network.view.flash(txt);
266 }
267
Simon Hunt99c13842014-11-06 18:23:12 -0800268 function testMe(view) {
Simon Hunt8f40cce2014-11-23 15:57:30 -0800269 //view.alert('Theme is ' + view.getTheme());
Simon Hunta3dd9572014-11-20 15:22:41 -0800270 //view.flash('This is some text');
Simon Hunt8f40cce2014-11-23 15:57:30 -0800271 cat7.testCard(svg);
Simon Hunt99c13842014-11-06 18:23:12 -0800272 }
273
Simon Hunt56d51852014-11-09 13:03:35 -0800274 function injectTestEvent(view) {
Simon Hunt434cf142014-11-24 11:10:28 -0800275 if (config.useLiveData) { return; }
276
Simon Hunt56d51852014-11-09 13:03:35 -0800277 var sc = scenario,
278 evn = ++sc.evNumber,
279 pfx = sc.evDir + sc.ctx + sc.evPrefix + evn,
280 onosUrl = pfx + sc.evOnos,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800281 uiUrl = pfx + sc.evUi,
282 stack = [
283 { url: onosUrl, cb: handleServerEvent },
284 { url: uiUrl, cb: handleUiEvent }
285 ];
286 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800287 }
288
Simon Hunt7cd48f32014-11-09 23:42:50 -0800289 function recurseFetchEvent(stack, evn) {
290 var v = scenario.view,
291 frame;
292 if (stack.length === 0) {
Simon Huntfc274c92014-11-11 11:05:46 -0800293 v.alert('Oops!\n\nNo event #' + evn + ' found.');
Simon Hunt7cd48f32014-11-09 23:42:50 -0800294 return;
295 }
296 frame = stack.shift();
297
298 d3.json(frame.url, function (err, data) {
Simon Hunt99c13842014-11-06 18:23:12 -0800299 if (err) {
Simon Hunt56d51852014-11-09 13:03:35 -0800300 if (err.status === 404) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800301 // if we didn't find the data, try the next stack frame
302 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800303 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800304 v.alert('non-404 error:\n\n' + frame.url + '\n\n' + err);
Simon Hunt56d51852014-11-09 13:03:35 -0800305 }
Simon Hunt99c13842014-11-06 18:23:12 -0800306 } else {
Simon Hunt1712ed82014-11-17 12:56:00 -0800307 wsTrace('test', JSON.stringify(data));
Simon Hunt7cd48f32014-11-09 23:42:50 -0800308 frame.cb(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800309 }
310 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800311
Simon Hunt56d51852014-11-09 13:03:35 -0800312 }
Simon Hunt50128c02014-11-08 13:36:15 -0800313
Simon Hunt56d51852014-11-09 13:03:35 -0800314 function handleUiEvent(data) {
Simon Huntbb282f52014-11-10 11:08:19 -0800315 scenario.view.alert('UI Tx: ' + data.event + '\n\n' +
316 JSON.stringify(data));
Simon Hunt56d51852014-11-09 13:03:35 -0800317 }
318
319 function injectStartupEvents(view) {
320 var last = scenario.params.lastAuto || 0;
Simon Hunt434cf142014-11-24 11:10:28 -0800321 if (config.useLiveData) { return; }
Simon Hunt56d51852014-11-09 13:03:35 -0800322
323 while (scenario.evNumber < last) {
Simon Hunt1a9eff92014-11-07 11:06:34 -0800324 injectTestEvent(view);
325 }
326 }
327
Simon Hunt934c3ce2014-11-05 11:45:07 -0800328 function toggleBg() {
329 var vis = bgImg.style('visibility');
Simon Hunt434cf142014-11-24 11:10:28 -0800330 bgImg.style('visibility', visVal(vis === 'hidden'));
331 }
332
Simon Huntd6f5a272014-11-29 23:45:50 -0800333 function opacifyBg(b) {
334 bgImg.transition()
335 .duration(1000)
336 .attr('opacity', b ? 1 : 0);
337 }
338
Simon Huntc2367d52014-11-29 19:30:23 -0800339 function toggleNodeLock() {
340 nodeLock = !nodeLock;
341 flash('Node positions ' + (nodeLock ? 'locked' : 'unlocked'))
342 }
343
344 function toggleOblique() {
345 oblique = !oblique;
Simon Huntd6f5a272014-11-29 23:45:50 -0800346 if (oblique) {
347 network.force.stop();
348 toObliqueView();
349 } else {
350 toNormalView();
351 }
Simon Huntc2367d52014-11-29 19:30:23 -0800352 }
353
Simon Hunt434cf142014-11-24 11:10:28 -0800354 function toggleHosts() {
355 showHosts = !showHosts;
356 updateHostVisibility();
Simon Hunt27d322d2014-11-28 10:45:43 -0800357 flash('Hosts ' + visVal(showHosts));
Simon Hunt934c3ce2014-11-05 11:45:07 -0800358 }
359
Simon Hunt6d9bd032014-11-28 22:16:40 -0800360 function toggleOffline() {
361 showOffline = !showOffline;
362 updateOfflineVisibility();
363 flash('Offline devices ' + visVal(showOffline));
364 }
365
Simon Hunt99c13842014-11-06 18:23:12 -0800366 function cycleLabels() {
Thomas Vachuska60d72bf2014-11-21 13:02:00 -0800367 deviceLabelIndex = (deviceLabelIndex === 2)
Simon Huntbb282f52014-11-10 11:08:19 -0800368 ? 0 : deviceLabelIndex + 1;
Simon Hunt5f36d342014-11-08 21:33:14 -0800369
Simon Hunt99c13842014-11-06 18:23:12 -0800370 network.nodes.forEach(function (d) {
Simon Huntbb282f52014-11-10 11:08:19 -0800371 if (d.class === 'device') {
372 updateDeviceLabel(d);
373 }
Simon Hunt99c13842014-11-06 18:23:12 -0800374 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800375 }
376
377 function togglePorts(view) {
Simon Hunt434cf142014-11-24 11:10:28 -0800378 //view.alert('togglePorts() callback')
Simon Hunt934c3ce2014-11-05 11:45:07 -0800379 }
380
Simon Hunt6ac93f32014-11-13 12:17:27 -0800381 function unpin() {
382 if (hovered) {
Simon Hunt395a70c2014-11-22 23:17:40 -0800383 sendUpdateMeta(hovered);
Simon Hunt6ac93f32014-11-13 12:17:27 -0800384 hovered.fixed = false;
385 hovered.el.classed('fixed', false);
Simon Huntd6f5a272014-11-29 23:45:50 -0800386 fResume();
Simon Hunt6ac93f32014-11-13 12:17:27 -0800387 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800388 }
389
Simon Hunt9462e8c2014-11-14 17:28:09 -0800390 function handleEscape(view) {
391 if (oiShowMaster) {
392 cancelAffinity();
Simon Hunt27d322d2014-11-28 10:45:43 -0800393 } else if (haveDetails) {
Simon Hunt9462e8c2014-11-14 17:28:09 -0800394 deselectAll();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800395 } else if (oiBox.isVisible()) {
Simon Huntb0ecfa52014-11-23 21:05:12 -0800396 hideInstances();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800397 } else if (summaryPane.isVisible()) {
398 cancelSummary();
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800399 stopAntTimer();
400 } else {
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800401 hoverMode = hoverModeNone;
Simon Hunt9462e8c2014-11-14 17:28:09 -0800402 }
403 }
404
Simon Hunt934c3ce2014-11-05 11:45:07 -0800405 // ==============================
Simon Huntd6f5a272014-11-29 23:45:50 -0800406 // Oblique view ...
407
408 var obview = {
409 tt: -.7, // x skew y factor
410 xsk: -35, // x skew angle
411 ysc: 0.5, // y scale
412 pad: 50,
413 time: 1500,
414 fill: {
415 pkt: 'rgba(130,130,170,0.3)',
416 opt: 'rgba(170,130,170,0.3)'
417 },
418 id: function (tag) {
419 return 'obview-' + tag + 'Plane';
420 },
421 yt: function (h, dir) {
422 return h * obview.ysc * dir * 1.1;
423 },
424 obXform: function (h, dir) {
425 var yt = obview.yt(h, dir);
426 return scale(1, obview.ysc) + translate(0, yt) + skewX(obview.xsk);
427 },
428 noXform: function () {
429 return skewX(0) + translate(0,0) + scale(1,1);
430 },
431 xffn: null,
432 plane: {}
433 };
434
435
436 function toObliqueView() {
437 var box = nodeG.node().getBBox(),
438 ox, oy;
439
440 padBox(box, obview.pad);
441
442 ox = box.x + box.width / 2;
443 oy = box.y + box.height / 2;
444
445 // remember node lock state, then lock the nodes down
446 obview.nodeLock = nodeLock;
447 nodeLock = true;
448 opacifyBg(false);
449
450 insertPlanes(ox, oy);
451
452 obview.xffn = function (xy, dir) {
453 var yt = obview.yt(box.height, dir),
454 ax = xy.x - ox,
455 ay = xy.y - oy,
456 x = ax + ay * obview.tt,
457 y = ay * obview.ysc + obview.ysc * yt;
458 return {x: ox + x, y: oy + y};
459 };
460
461 showPlane('pkt', box, -1);
462 showPlane('opt', box, 1);
463 obTransitionNodes();
464 }
465
466 function toNormalView() {
467 obview.xffn = null;
468
469 hidePlane('pkt');
470 hidePlane('opt');
471 obTransitionNodes();
472
473 removePlanes();
474
475 // restore node lock state
476 nodeLock = obview.nodeLock;
477 opacifyBg(true);
478 }
479
480 function obTransitionNodes() {
481 var xffn = obview.xffn;
482
483 // return the direction for the node
484 // -1 for pkt layer, 1 for optical layer
485 function dir(d) {
486 return inLayer(d, 'pkt') ? -1 : 1;
487 }
488
489 if (xffn) {
490 network.nodes.forEach(function (d) {
491 var oldxy = {x: d.x, y: d.y},
492 coords = xffn(oldxy, dir(d));
493 d.oldxy = oldxy;
494 d.px = d.x = coords.x;
495 d.py = d.y = coords.y;
496 });
497 } else {
498 network.nodes.forEach(function (d) {
499 var old = d.oldxy || {x: d.x, y: d.y};
500 d.px = d.x = old.x;
501 d.py = d.y = old.y;
502 delete d.oldxy;
503 });
504 }
505
506 node.transition()
507 .duration(obview.time)
508 .attr(tickStuff.nodeAttr);
509 link.transition()
510 .duration(obview.time)
511 .attr(tickStuff.linkAttr);
512 linkLabel.transition()
513 .duration(obview.time)
514 .attr(tickStuff.linkLabelAttr);
515 }
516
517 function showPlane(tag, box, dir) {
518 var g = obview.plane[tag];
519
520 // set box origin at center..
521 box.x = -box.width/2;
522 box.y = -box.height/2;
523
524 g.select('rect')
525 .attr(box)
526 .attr('opacity', 0)
527 .transition()
528 .duration(obview.time)
529 .attr('opacity', 1)
530 .attr('transform', obview.obXform(box.height, dir));
531 }
532
533 function hidePlane(tag) {
534 var g = obview.plane[tag];
535
536 g.select('rect')
537 .transition()
538 .duration(obview.time)
539 .attr('opacity', 0)
540 .attr('transform', obview.noXform());
541 }
542
543 function insertPlanes(ox, oy) {
544 function ins(tag) {
545 var id = obview.id(tag),
546 g = panZoomContainer.insert('g', '#topo-G')
547 .attr('id', id)
548 .attr('transform', translate(ox,oy));
549 g.append('rect')
550 .attr('fill', obview.fill[tag])
551 .attr('opacity', 0);
552 obview.plane[tag] = g;
553 }
554 ins('opt');
555 ins('pkt');
556 }
557
558 function removePlanes() {
559 function rem(tag) {
560 var id = obview.id(tag);
561 panZoomContainer.select('#'+id)
562 .transition()
563 .duration(obview.time + 50)
564 .remove();
565 delete obview.plane[tag];
566 }
567 rem('opt');
568 rem('pkt');
569 }
570
571 function padBox(box, p) {
572 box.x -= p;
573 box.y -= p;
574 box.width += p*2;
575 box.height += p*2;
576 }
577
578 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800579 // Radio Button Callbacks
580
Simon Hunta5e89142014-11-14 07:00:33 -0800581 var layerLookup = {
582 host: {
Simon Hunt209155e2014-11-21 12:16:09 -0800583 endstation: 'pkt', // default, if host event does not define type
Thomas Vachuska89543292014-11-19 11:28:33 -0800584 router: 'pkt',
Simon Hunta5e89142014-11-14 07:00:33 -0800585 bgpSpeaker: 'pkt'
586 },
587 device: {
588 switch: 'pkt',
589 roadm: 'opt'
590 },
591 link: {
592 hostLink: 'pkt',
593 direct: 'pkt',
Simon Hunt8257f4c2014-11-16 19:34:54 -0800594 indirect: '',
595 tunnel: '',
Simon Hunta5e89142014-11-14 07:00:33 -0800596 optical: 'opt'
597 }
598 };
599
600 function inLayer(d, layer) {
Simon Hunt8257f4c2014-11-16 19:34:54 -0800601 var type = d.class === 'link' ? d.type() : d.type,
602 look = layerLookup[d.class],
603 lyr = look && look[type];
Simon Hunta5e89142014-11-14 07:00:33 -0800604 return lyr === layer;
605 }
606
607 function unsuppressLayer(which) {
608 node.each(function (d) {
609 var node = d.el;
610 if (inLayer(d, which)) {
611 node.classed('suppressed', false);
612 }
613 });
614
615 link.each(function (d) {
616 var link = d.el;
617 if (inLayer(d, which)) {
618 link.classed('suppressed', false);
619 }
620 });
621 }
622
Simon Hunt9462e8c2014-11-14 17:28:09 -0800623 function suppressLayers(b) {
624 node.classed('suppressed', b);
625 link.classed('suppressed', b);
Simon Hunt142d0032014-11-04 20:13:09 -0800626// d3.selectAll('svg .port').classed('inactive', false);
627// d3.selectAll('svg .portText').classed('inactive', false);
Simon Hunt195cb382014-11-03 17:50:51 -0800628 }
629
Simon Hunt9462e8c2014-11-14 17:28:09 -0800630 function showAllLayers() {
631 suppressLayers(false);
632 }
633
Simon Hunt195cb382014-11-03 17:50:51 -0800634 function showPacketLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800635 node.classed('suppressed', true);
636 link.classed('suppressed', true);
637 unsuppressLayer('pkt');
Simon Hunt195cb382014-11-03 17:50:51 -0800638 }
639
640 function showOpticalLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800641 node.classed('suppressed', true);
642 link.classed('suppressed', true);
643 unsuppressLayer('opt');
Simon Hunt195cb382014-11-03 17:50:51 -0800644 }
645
Simon Hunt9462e8c2014-11-14 17:28:09 -0800646 function restoreLayerState() {
647 layerBtnDispatch[layerBtnSet.selected()]();
648 }
649
Simon Hunt142d0032014-11-04 20:13:09 -0800650 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800651 // Private functions
652
Simon Hunt99c13842014-11-06 18:23:12 -0800653 function safeId(s) {
654 return s.replace(/[^a-z0-9]/gi, '-');
655 }
656
Simon Huntc7ee0662014-11-05 16:44:37 -0800657 // set the size of the given element to that of the view (reduced if padded)
658 function setSize(el, view, pad) {
659 var padding = pad ? pad * 2 : 0;
Simon Hunt934c3ce2014-11-05 11:45:07 -0800660 el.attr({
Simon Huntc7ee0662014-11-05 16:44:37 -0800661 width: view.width() - padding,
662 height: view.height() - padding
Simon Hunt934c3ce2014-11-05 11:45:07 -0800663 });
664 }
665
Simon Hunt8257f4c2014-11-16 19:34:54 -0800666 function makeNodeKey(d, what) {
667 var port = what + 'Port';
668 return d[what] + '/' + d[port];
669 }
670
671 function makeLinkKey(d, flipped) {
672 var one = flipped ? makeNodeKey(d, 'dst') : makeNodeKey(d, 'src'),
673 two = flipped ? makeNodeKey(d, 'src') : makeNodeKey(d, 'dst');
674 return one + '-' + two;
675 }
676
Simon Hunt269670f2014-11-17 16:17:43 -0800677 function findLinkById(id) {
678 // check to see if this is a reverse lookup, else default to given id
679 var key = network.revLinkToKey[id] || id;
680 return key && network.lookup[key];
681 }
682
Simon Hunt8257f4c2014-11-16 19:34:54 -0800683 function findLink(linkData, op) {
684 var key = makeLinkKey(linkData),
685 keyrev = makeLinkKey(linkData, 1),
686 link = network.lookup[key],
687 linkRev = network.lookup[keyrev],
688 result = {},
689 ldata = link || linkRev,
690 rawLink;
691
692 if (op === 'add') {
693 if (link) {
694 // trying to add a link that we already know about
695 result.ldata = link;
696 result.badLogic = 'addLink: link already added';
697
698 } else if (linkRev) {
699 // we found the reverse of the link to be added
700 result.ldata = linkRev;
701 if (linkRev.fromTarget) {
702 result.badLogic = 'addLink: link already added';
703 }
704 }
705 } else if (op === 'update') {
706 if (!ldata) {
707 result.badLogic = 'updateLink: link not found';
708 } else {
709 rawLink = link ? ldata.fromSource : ldata.fromTarget;
710 result.updateWith = function (data) {
711 $.extend(rawLink, data);
712 restyleLinkElement(ldata);
713 }
714 }
715 } else if (op === 'remove') {
716 if (!ldata) {
717 result.badLogic = 'removeLink: link not found';
718 } else {
719 rawLink = link ? ldata.fromSource : ldata.fromTarget;
720
721 if (!rawLink) {
722 result.badLogic = 'removeLink: link not found';
723
724 } else {
725 result.removeRawLink = function () {
726 if (link) {
727 // remove fromSource
728 ldata.fromSource = null;
729 if (ldata.fromTarget) {
730 // promote target into source position
731 ldata.fromSource = ldata.fromTarget;
732 ldata.fromTarget = null;
733 ldata.key = keyrev;
734 delete network.lookup[key];
735 network.lookup[keyrev] = ldata;
Simon Hunt269670f2014-11-17 16:17:43 -0800736 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800737 }
738 } else {
739 // remove fromTarget
740 ldata.fromTarget = null;
Simon Hunt269670f2014-11-17 16:17:43 -0800741 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800742 }
743 if (ldata.fromSource) {
744 restyleLinkElement(ldata);
745 } else {
746 removeLinkElement(ldata);
747 }
748 }
749 }
750 }
751 }
752 return result;
753 }
754
755 function addLinkUpdate(ldata, link) {
756 // add link event, but we already have the reverse link installed
757 ldata.fromTarget = link;
Simon Hunt269670f2014-11-17 16:17:43 -0800758 network.revLinkToKey[link.id] = ldata.key;
Simon Hunt8257f4c2014-11-16 19:34:54 -0800759 restyleLinkElement(ldata);
760 }
761
762 var allLinkTypes = 'direct indirect optical tunnel',
763 defaultLinkType = 'direct';
764
765 function restyleLinkElement(ldata) {
766 // this fn's job is to look at raw links and decide what svg classes
767 // need to be applied to the line element in the DOM
768 var el = ldata.el,
769 type = ldata.type(),
770 lw = ldata.linkWidth(),
771 online = ldata.online();
772
773 el.classed('link', true);
774 el.classed('inactive', !online);
775 el.classed(allLinkTypes, false);
776 if (type) {
777 el.classed(type, true);
778 }
779 el.transition()
780 .duration(1000)
Thomas Vachuska89543292014-11-19 11:28:33 -0800781 .attr('stroke-width', linkScale(lw))
782 .attr('stroke', config.topo.linkBaseColor);
Simon Hunt8257f4c2014-11-16 19:34:54 -0800783 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800784
Simon Hunt99c13842014-11-06 18:23:12 -0800785 // ==============================
786 // Event handlers for server-pushed events
787
Simon Huntbb282f52014-11-10 11:08:19 -0800788 function logicError(msg) {
789 // TODO, report logic error to server, via websock, so it can be logged
Simon Huntfc274c92014-11-11 11:05:46 -0800790 console.warn(msg);
Simon Huntbb282f52014-11-10 11:08:19 -0800791 }
792
Simon Hunt99c13842014-11-06 18:23:12 -0800793 var eventDispatch = {
Simon Hunta5e89142014-11-14 07:00:33 -0800794 addInstance: addInstance,
Simon Hunt99c13842014-11-06 18:23:12 -0800795 addDevice: addDevice,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800796 addLink: addLink,
Simon Hunt56d51852014-11-09 13:03:35 -0800797 addHost: addHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800798
Simon Huntfcfb46c2014-11-19 12:53:38 -0800799 updateInstance: updateInstance,
Simon Huntbb282f52014-11-10 11:08:19 -0800800 updateDevice: updateDevice,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800801 updateLink: updateLink,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800802 updateHost: updateHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800803
Simon Hunt7b403bc2014-11-22 19:01:00 -0800804 removeInstance: removeInstance,
Simon Huntca867ac2014-11-28 18:07:35 -0800805 removeDevice: removeDevice,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800806 removeLink: removeLink,
Simon Hunt44031102014-11-11 13:20:36 -0800807 removeHost: removeHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800808
Simon Hunt61d04042014-11-11 17:27:16 -0800809 showDetails: showDetails,
Thomas Vachuska47635c62014-11-22 01:21:36 -0800810 showSummary: showSummary,
Simon Huntb53e0682014-11-12 13:32:01 -0800811 showTraffic: showTraffic
Simon Hunt99c13842014-11-06 18:23:12 -0800812 };
813
Simon Hunta5e89142014-11-14 07:00:33 -0800814 function addInstance(data) {
815 evTrace(data);
816 var inst = data.payload,
817 id = inst.id;
818 if (onosInstances[id]) {
Thomas Vachuska12dfdc32014-11-29 16:03:12 -0800819 updateInstance(data);
Simon Hunta5e89142014-11-14 07:00:33 -0800820 return;
821 }
822 onosInstances[id] = inst;
823 onosOrder.push(inst);
824 updateInstances();
825 }
826
Simon Hunt99c13842014-11-06 18:23:12 -0800827 function addDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800828 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800829 var device = data.payload,
Simon Huntca867ac2014-11-28 18:07:35 -0800830 id = device.id,
831 d;
832
833 if (network.lookup[id]) {
Thomas Vachuska12dfdc32014-11-29 16:03:12 -0800834 updateDevice(data);
Simon Huntca867ac2014-11-28 18:07:35 -0800835 return;
836 }
837
838 d = createDeviceNode(device);
839 network.nodes.push(d);
840 network.lookup[id] = d;
Simon Hunt99c13842014-11-06 18:23:12 -0800841 updateNodes();
Simon Huntd6f5a272014-11-29 23:45:50 -0800842 fStart();
Simon Hunt99c13842014-11-06 18:23:12 -0800843 }
844
Simon Hunt99c13842014-11-06 18:23:12 -0800845 function addLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800846 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800847 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800848 result = findLink(link, 'add'),
849 bad = result.badLogic,
Simon Huntca867ac2014-11-28 18:07:35 -0800850 d = result.ldata;
Simon Hunt8257f4c2014-11-16 19:34:54 -0800851
852 if (bad) {
853 logicError(bad + ': ' + link.id);
854 return;
855 }
856
Simon Huntca867ac2014-11-28 18:07:35 -0800857 if (d) {
Simon Hunt8257f4c2014-11-16 19:34:54 -0800858 // we already have a backing store link for src/dst nodes
Simon Huntca867ac2014-11-28 18:07:35 -0800859 addLinkUpdate(d, link);
Simon Hunt8257f4c2014-11-16 19:34:54 -0800860 return;
861 }
862
863 // no backing store link yet
Simon Huntca867ac2014-11-28 18:07:35 -0800864 d = createLink(link);
865 if (d) {
866 network.links.push(d);
867 network.lookup[d.key] = d;
Simon Hunt99c13842014-11-06 18:23:12 -0800868 updateLinks();
Simon Huntd6f5a272014-11-29 23:45:50 -0800869 fStart();
Simon Hunt99c13842014-11-06 18:23:12 -0800870 }
871 }
872
Simon Hunt56d51852014-11-09 13:03:35 -0800873 function addHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800874 evTrace(data);
Simon Hunt56d51852014-11-09 13:03:35 -0800875 var host = data.payload,
Simon Huntca867ac2014-11-28 18:07:35 -0800876 id = host.id,
877 d,
Simon Hunt56d51852014-11-09 13:03:35 -0800878 lnk;
Simon Huntca867ac2014-11-28 18:07:35 -0800879
880 if (network.lookup[id]) {
881 logicError('Host already added: ' + id);
882 return;
883 }
884
885 d = createHostNode(host);
886 network.nodes.push(d);
887 network.lookup[host.id] = d;
Simon Hunt56d51852014-11-09 13:03:35 -0800888 updateNodes();
889
890 lnk = createHostLink(host);
891 if (lnk) {
Simon Huntca867ac2014-11-28 18:07:35 -0800892 d.linkData = lnk; // cache ref on its host
Simon Hunt56d51852014-11-09 13:03:35 -0800893 network.links.push(lnk);
Simon Huntca867ac2014-11-28 18:07:35 -0800894 network.lookup[d.ingress] = lnk;
895 network.lookup[d.egress] = lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800896 updateLinks();
897 }
Simon Huntd6f5a272014-11-29 23:45:50 -0800898 fStart();
Simon Hunt56d51852014-11-09 13:03:35 -0800899 }
900
Simon Hunt44031102014-11-11 13:20:36 -0800901 // TODO: fold updateX(...) methods into one base method; remove duplication
Simon Hunt56a2ea42014-11-19 12:39:31 -0800902
903 function updateInstance(data) {
904 evTrace(data);
905 var inst = data.payload,
906 id = inst.id,
Simon Huntca867ac2014-11-28 18:07:35 -0800907 d = onosInstances[id];
908 if (d) {
909 $.extend(d, inst);
Simon Hunt56a2ea42014-11-19 12:39:31 -0800910 updateInstances();
Simon Hunt56a2ea42014-11-19 12:39:31 -0800911 } else {
912 logicError('updateInstance lookup fail. ID = "' + id + '"');
913 }
914 }
915
Simon Huntbb282f52014-11-10 11:08:19 -0800916 function updateDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800917 evTrace(data);
Simon Huntbb282f52014-11-10 11:08:19 -0800918 var device = data.payload,
919 id = device.id,
Simon Hunt6d9bd032014-11-28 22:16:40 -0800920 d = network.lookup[id],
921 wasOnline;
922
Simon Hunt62c47542014-11-22 22:16:32 -0800923 if (d) {
Simon Hunt6d9bd032014-11-28 22:16:40 -0800924 wasOnline = d.online;
Simon Hunt62c47542014-11-22 22:16:32 -0800925 $.extend(d, device);
926 if (positionNode(d, true)) {
Simon Hunt395a70c2014-11-22 23:17:40 -0800927 sendUpdateMeta(d, true);
Simon Hunt62c47542014-11-22 22:16:32 -0800928 }
929 updateNodes();
Simon Hunt6d9bd032014-11-28 22:16:40 -0800930 if (wasOnline !== d.online) {
931 findAttachedLinks(d.id).forEach(restyleLinkElement);
932 updateOfflineVisibility(d);
933 }
Simon Huntbb282f52014-11-10 11:08:19 -0800934 } else {
935 logicError('updateDevice lookup fail. ID = "' + id + '"');
936 }
937 }
938
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800939 function updateLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800940 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800941 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800942 result = findLink(link, 'update'),
943 bad = result.badLogic;
944 if (bad) {
945 logicError(bad + ': ' + link.id);
946 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800947 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800948 result.updateWith(link);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800949 }
950
Simon Hunt7cd48f32014-11-09 23:42:50 -0800951 function updateHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800952 evTrace(data);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800953 var host = data.payload,
Simon Huntbb282f52014-11-10 11:08:19 -0800954 id = host.id,
Simon Huntca867ac2014-11-28 18:07:35 -0800955 d = network.lookup[id];
956 if (d) {
957 $.extend(d, host);
Simon Hunt6d9bd032014-11-28 22:16:40 -0800958 if (positionNode(d, true)) {
959 sendUpdateMeta(d, true);
960 }
961 updateNodes(d);
Simon Huntbb282f52014-11-10 11:08:19 -0800962 } else {
963 logicError('updateHost lookup fail. ID = "' + id + '"');
964 }
Simon Hunt7cd48f32014-11-09 23:42:50 -0800965 }
966
Simon Hunt44031102014-11-11 13:20:36 -0800967 // TODO: fold removeX(...) methods into base method - remove dup code
Simon Hunt7b403bc2014-11-22 19:01:00 -0800968 function removeInstance(data) {
969 evTrace(data);
970 var inst = data.payload,
971 id = inst.id,
Simon Huntca867ac2014-11-28 18:07:35 -0800972 d = onosInstances[id];
973 if (d) {
974 var idx = find(id, onosOrder);
Simon Hunt7b403bc2014-11-22 19:01:00 -0800975 if (idx >= 0) {
976 onosOrder.splice(idx, 1);
977 }
978 delete onosInstances[id];
979 updateInstances();
980 } else {
981 logicError('updateInstance lookup fail. ID = "' + id + '"');
982 }
983 }
984
Simon Huntca867ac2014-11-28 18:07:35 -0800985 function removeDevice(data) {
986 evTrace(data);
987 var device = data.payload,
988 id = device.id,
989 d = network.lookup[id];
990 if (d) {
991 removeDeviceElement(d);
992 } else {
993 logicError('removeDevice lookup fail. ID = "' + id + '"');
994 }
995 }
996
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800997 function removeLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800998 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800999 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -08001000 result = findLink(link, 'remove'),
1001 bad = result.badLogic;
1002 if (bad) {
Simon Huntca867ac2014-11-28 18:07:35 -08001003 // may have already removed link, if attached to removed device
1004 console.warn(bad + ': ' + link.id);
Simon Hunt8257f4c2014-11-16 19:34:54 -08001005 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001006 }
Simon Hunt8257f4c2014-11-16 19:34:54 -08001007 result.removeRawLink();
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001008 }
1009
Simon Hunt44031102014-11-11 13:20:36 -08001010 function removeHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -08001011 evTrace(data);
Simon Hunt44031102014-11-11 13:20:36 -08001012 var host = data.payload,
1013 id = host.id,
Simon Huntca867ac2014-11-28 18:07:35 -08001014 d = network.lookup[id];
1015 if (d) {
1016 removeHostElement(d, true);
Simon Hunt44031102014-11-11 13:20:36 -08001017 } else {
Simon Huntca867ac2014-11-28 18:07:35 -08001018 // may have already removed host, if attached to removed device
1019 console.warn('removeHost lookup fail. ID = "' + id + '"');
Simon Hunt44031102014-11-11 13:20:36 -08001020 }
1021 }
1022
Simon Huntca867ac2014-11-28 18:07:35 -08001023 // the following events are server responses to user actions
Thomas Vachuska47635c62014-11-22 01:21:36 -08001024 function showSummary(data) {
1025 evTrace(data);
1026 populateSummary(data.payload);
Simon Hunt06811b72014-11-25 18:54:48 -08001027 showSummaryPane();
Thomas Vachuska47635c62014-11-22 01:21:36 -08001028 }
1029
Simon Hunt61d04042014-11-11 17:27:16 -08001030 function showDetails(data) {
Simon Hunta5e89142014-11-14 07:00:33 -08001031 evTrace(data);
Simon Hunt27d322d2014-11-28 10:45:43 -08001032 haveDetails = true;
Simon Hunt61d04042014-11-11 17:27:16 -08001033 populateDetails(data.payload);
Simon Hunt27d322d2014-11-28 10:45:43 -08001034 if (useDetails) {
1035 showDetailPane();
1036 }
Simon Hunt61d04042014-11-11 17:27:16 -08001037 }
1038
Simon Huntb53e0682014-11-12 13:32:01 -08001039 function showTraffic(data) {
Simon Hunta5e89142014-11-14 07:00:33 -08001040 evTrace(data);
Thomas Vachuska4731f122014-11-20 04:56:19 -08001041 var paths = data.payload.paths,
1042 hasTraffic = false;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -08001043
Thomas Vachuska3266abf2014-11-13 09:28:46 -08001044 // Revert any links hilighted previously.
Thomas Vachuska4731f122014-11-20 04:56:19 -08001045 link.style('stroke-width', null)
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08001046 .classed('primary secondary animated optical', false);
Simon Hunte2575b62014-11-18 15:25:53 -08001047 // Remove all previous labels.
1048 removeLinkLabels();
Thomas Vachuska3266abf2014-11-13 09:28:46 -08001049
Simon Hunte2575b62014-11-18 15:25:53 -08001050 // Now hilight all links in the paths payload, and attach
1051 // labels to them, if they are defined.
Simon Hunta255a2c2014-11-13 22:29:35 -08001052 paths.forEach(function (p) {
Simon Hunte2575b62014-11-18 15:25:53 -08001053 var n = p.links.length,
1054 i,
1055 ldata;
1056
Thomas Vachuska4731f122014-11-20 04:56:19 -08001057 hasTraffic = hasTraffic || p.traffic;
Simon Hunte2575b62014-11-18 15:25:53 -08001058 for (i=0; i<n; i++) {
1059 ldata = findLinkById(p.links[i]);
Thomas Vachuska4731f122014-11-20 04:56:19 -08001060 if (ldata && ldata.el) {
Simon Hunte2575b62014-11-18 15:25:53 -08001061 ldata.el.classed(p.class, true);
1062 ldata.label = p.labels[i];
Thomas Vachuskadea45ff2014-11-12 18:35:46 -08001063 }
Simon Hunte2575b62014-11-18 15:25:53 -08001064 }
Thomas Vachuskadea45ff2014-11-12 18:35:46 -08001065 });
Thomas Vachuska4731f122014-11-20 04:56:19 -08001066
Simon Hunte2575b62014-11-18 15:25:53 -08001067 updateLinks();
Thomas Vachuska4731f122014-11-20 04:56:19 -08001068
1069 if (hasTraffic && !antTimer) {
1070 startAntTimer();
1071 } else if (!hasTraffic && antTimer) {
1072 stopAntTimer();
1073 }
Simon Huntb53e0682014-11-12 13:32:01 -08001074 }
1075
Simon Hunt56d51852014-11-09 13:03:35 -08001076 // ...............................
1077
Simon Hunt99c13842014-11-06 18:23:12 -08001078 function unknownEvent(data) {
Simon Hunt434cf142014-11-24 11:10:28 -08001079 console.warn('Unknown event type: "' + data.event + '"', data);
Simon Hunt99c13842014-11-06 18:23:12 -08001080 }
1081
1082 function handleServerEvent(data) {
1083 var fn = eventDispatch[data.event] || unknownEvent;
1084 fn(data);
1085 }
1086
1087 // ==============================
Simon Hunt61d04042014-11-11 17:27:16 -08001088 // Out-going messages...
1089
Simon Huntb53e0682014-11-12 13:32:01 -08001090 function nSel() {
1091 return selectOrder.length;
1092 }
Simon Hunt61d04042014-11-11 17:27:16 -08001093 function getSel(idx) {
1094 return selections[selectOrder[idx]];
1095 }
Simon Huntb53e0682014-11-12 13:32:01 -08001096 function allSelectionsClass(cls) {
1097 for (var i=0, n=nSel(); i<n; i++) {
1098 if (getSel(i).obj.class !== cls) {
1099 return false;
1100 }
1101 }
1102 return true;
1103 }
Simon Hunt61d04042014-11-11 17:27:16 -08001104
Thomas Vachuska47635c62014-11-22 01:21:36 -08001105 function toggleInstances() {
1106 if (!oiBox.isVisible()) {
Simon Huntb0ecfa52014-11-23 21:05:12 -08001107 showInstances();
Thomas Vachuska47635c62014-11-22 01:21:36 -08001108 } else {
Simon Huntb0ecfa52014-11-23 21:05:12 -08001109 hideInstances();
Thomas Vachuska47635c62014-11-22 01:21:36 -08001110 }
1111 }
1112
Simon Huntb0ecfa52014-11-23 21:05:12 -08001113 function showInstances() {
1114 oiBox.show();
1115 colorAffinity = true;
1116 updateDeviceColors();
1117 }
1118
1119 function hideInstances() {
1120 oiBox.hide();
1121 colorAffinity = false;
1122 cancelAffinity();
1123 updateDeviceColors();
1124 }
1125
Thomas Vachuska1e68bdd2014-11-29 13:53:10 -08001126 function equalizeMasters() {
Thomas Vachuska1e68bdd2014-11-29 13:53:10 -08001127 sendMessage('equalizeMasters');
Simon Huntc1cc81c2014-11-29 14:59:01 -08001128 flash('Equalizing master roles');
Thomas Vachuska1e68bdd2014-11-29 13:53:10 -08001129 }
1130
Thomas Vachuska47635c62014-11-22 01:21:36 -08001131 function toggleSummary() {
1132 if (!summaryPane.isVisible()) {
1133 requestSummary();
1134 } else {
1135 cancelSummary();
1136 }
1137 }
1138
1139 // request overall summary data
1140 function requestSummary() {
Simon Huntc1cc81c2014-11-29 14:59:01 -08001141 sendMessage('requestSummary');
Thomas Vachuska47635c62014-11-22 01:21:36 -08001142 }
1143
1144 function cancelSummary() {
Simon Huntc1cc81c2014-11-29 14:59:01 -08001145 sendMessage('cancelSummary');
Simon Hunt06811b72014-11-25 18:54:48 -08001146 hideSummaryPane();
Thomas Vachuska47635c62014-11-22 01:21:36 -08001147 }
1148
Simon Hunt27d322d2014-11-28 10:45:43 -08001149 function toggleDetails() {
1150 useDetails = !useDetails;
1151 if (useDetails) {
1152 flash('Enable details pane');
1153 if (haveDetails) {
1154 showDetailPane();
1155 }
1156 } else {
1157 flash('Disable details pane');
1158 hideDetailPane();
1159 }
1160 }
1161
Simon Hunt06811b72014-11-25 18:54:48 -08001162 // encapsulate interaction between summary and details panes
1163 function showSummaryPane() {
1164 if (detailPane.isVisible()) {
1165 detailPane.down(summaryPane.show);
1166 } else {
1167 summaryPane.show();
1168 }
1169 }
1170
1171 function hideSummaryPane() {
1172 summaryPane.hide(function () {
1173 if (detailPane.isVisible()) {
1174 detailPane.up();
1175 }
1176 });
1177 }
1178
1179 function showDetailPane() {
1180 if (summaryPane.isVisible()) {
1181 detailPane.down(detailPane.show);
1182 } else {
1183 detailPane.up(detailPane.show);
1184 }
1185 }
1186
1187 function hideDetailPane() {
1188 detailPane.hide();
1189 }
1190
1191
Simon Hunt61d04042014-11-11 17:27:16 -08001192 // request details for the selected element
Simon Huntd72bc702014-11-13 18:38:04 -08001193 // invoked from selection of a single node.
Simon Hunt61d04042014-11-11 17:27:16 -08001194 function requestDetails() {
Simon Huntc1cc81c2014-11-29 14:59:01 -08001195 var data = getSel(0).obj;
1196 sendMessage('requestDetails', {
1197 id: data.id,
1198 class: data.class
1199 });
Simon Hunt61d04042014-11-11 17:27:16 -08001200 }
1201
Thomas Vachuska9edca302014-11-22 17:06:42 -08001202 function addHostIntentAction() {
Simon Huntd72bc702014-11-13 18:38:04 -08001203 sendMessage('addHostIntent', {
Thomas Vachuska9edca302014-11-22 17:06:42 -08001204 one: selectOrder[0],
1205 two: selectOrder[1],
1206 ids: selectOrder
Simon Huntd72bc702014-11-13 18:38:04 -08001207 });
Simon Hunt27d322d2014-11-28 10:45:43 -08001208 flash('Host-to-Host flow added');
Simon Huntd72bc702014-11-13 18:38:04 -08001209 }
1210
Thomas Vachuska9edca302014-11-22 17:06:42 -08001211 function addMultiSourceIntentAction() {
1212 sendMessage('addMultiSourceIntent', {
1213 src: selectOrder.slice(0, selectOrder.length - 1),
1214 dst: selectOrder[selectOrder.length - 1],
1215 ids: selectOrder
1216 });
Simon Hunt27d322d2014-11-28 10:45:43 -08001217 flash('Multi-Source flow added');
Thomas Vachuska29617e52014-11-20 03:17:46 -08001218 }
1219
Thomas Vachuska9edca302014-11-22 17:06:42 -08001220
Thomas Vachuska47635c62014-11-22 01:21:36 -08001221 function cancelTraffic() {
Simon Huntc1cc81c2014-11-29 14:59:01 -08001222 sendMessage('cancelTraffic');
Thomas Vachuska47635c62014-11-22 01:21:36 -08001223 }
1224
Thomas Vachuska9edca302014-11-22 17:06:42 -08001225 function requestTrafficForMode() {
Thomas Vachuska164fa5c2014-12-02 21:59:41 -08001226 if (hoverMode === hoverModeFlows) {
Thomas Vachuska9edca302014-11-22 17:06:42 -08001227 requestDeviceLinkFlows();
1228 } else if (hoverMode === hoverModeIntents) {
Thomas Vachuska164fa5c2014-12-02 21:59:41 -08001229 requestRelatedIntents();
Simon Huntd72bc702014-11-13 18:38:04 -08001230 }
Thomas Vachuska9edca302014-11-22 17:06:42 -08001231 }
Simon Huntd72bc702014-11-13 18:38:04 -08001232
Thomas Vachuska164fa5c2014-12-02 21:59:41 -08001233 function showRelatedIntentsAction() {
Thomas Vachuska9edca302014-11-22 17:06:42 -08001234 hoverMode = hoverModeIntents;
Thomas Vachuska164fa5c2014-12-02 21:59:41 -08001235 requestRelatedIntents();
Thomas Vachuskab7e40642014-12-03 11:16:06 -08001236 flash('Related Paths');
Thomas Vachuska9edca302014-11-22 17:06:42 -08001237 }
1238
Thomas Vachuska164fa5c2014-12-02 21:59:41 -08001239 function requestRelatedIntents() {
Simon Huntc1cc81c2014-11-29 14:59:01 -08001240 function hoverValid() {
1241 return hoverMode === hoverModeIntents &&
1242 hovered &&
1243 (hovered.class === 'host' || hovered.class === 'device');
1244 }
1245
Thomas Vachuska9edca302014-11-22 17:06:42 -08001246 if (validateSelectionContext()) {
Thomas Vachuska164fa5c2014-12-02 21:59:41 -08001247 sendMessage('requestRelatedIntents', {
Thomas Vachuska9edca302014-11-22 17:06:42 -08001248 ids: selectOrder,
Simon Huntc1cc81c2014-11-29 14:59:01 -08001249 hover: hoverValid() ? hovered.id : ''
Thomas Vachuska9edca302014-11-22 17:06:42 -08001250 });
1251 }
Simon Huntd72bc702014-11-13 18:38:04 -08001252 }
1253
Thomas Vachuska164fa5c2014-12-02 21:59:41 -08001254 function showNextIntentAction() {
1255 hoverMode = hoverModeNone;
Thomas Vachuskab7e40642014-12-03 11:16:06 -08001256 sendMessage('requestNextRelatedIntent');
1257 flash('>');
1258 }
1259
1260 function showPrevIntentAction() {
1261 hoverMode = hoverModeNone;
1262 sendMessage('requestPrevRelatedIntent');
1263 flash('<');
Thomas Vachuska164fa5c2014-12-02 21:59:41 -08001264 }
1265
1266 function showSelectedIntentTrafficAction() {
1267 hoverMode = hoverModeNone;
Thomas Vachuskab7e40642014-12-03 11:16:06 -08001268 sendMessage('requestSelectedIntentTraffic');
1269 flash('Traffic on Selected Path');
Thomas Vachuska164fa5c2014-12-02 21:59:41 -08001270 }
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -08001271
Thomas Vachuska29617e52014-11-20 03:17:46 -08001272 function showDeviceLinkFlowsAction() {
Thomas Vachuska9edca302014-11-22 17:06:42 -08001273 hoverMode = hoverModeFlows;
1274 requestDeviceLinkFlows();
Simon Hunt27d322d2014-11-28 10:45:43 -08001275 flash('Device Flows');
Thomas Vachuska29617e52014-11-20 03:17:46 -08001276 }
1277
Thomas Vachuska9edca302014-11-22 17:06:42 -08001278 function requestDeviceLinkFlows() {
Simon Huntc1cc81c2014-11-29 14:59:01 -08001279 function hoverValid() {
1280 return hoverMode === hoverModeFlows &&
1281 hovered && (hovered.class === 'device');
1282 }
1283
Thomas Vachuska9edca302014-11-22 17:06:42 -08001284 if (validateSelectionContext()) {
Thomas Vachuska9edca302014-11-22 17:06:42 -08001285 sendMessage('requestDeviceLinkFlows', {
1286 ids: selectOrder,
Simon Huntc1cc81c2014-11-29 14:59:01 -08001287 hover: hoverValid() ? hovered.id : ''
Thomas Vachuska9edca302014-11-22 17:06:42 -08001288 });
1289 }
1290 }
1291
1292
1293 function showAllTrafficAction() {
1294 hoverMode = hoverModeAll;
1295 requestAllTraffic();
Simon Hunt27d322d2014-11-28 10:45:43 -08001296 flash('All Traffic');
Thomas Vachuska9edca302014-11-22 17:06:42 -08001297 }
1298
1299 function requestAllTraffic() {
Simon Huntc1cc81c2014-11-29 14:59:01 -08001300 sendMessage('requestAllTraffic');
Thomas Vachuska9edca302014-11-22 17:06:42 -08001301 }
1302
1303 function validateSelectionContext() {
Thomas Vachuska29617e52014-11-20 03:17:46 -08001304 if (!hovered && nSel() === 0) {
Thomas Vachuska47635c62014-11-22 01:21:36 -08001305 cancelTraffic();
Thomas Vachuska9edca302014-11-22 17:06:42 -08001306 return false;
Thomas Vachuska29617e52014-11-20 03:17:46 -08001307 }
Thomas Vachuska9edca302014-11-22 17:06:42 -08001308 return true;
Thomas Vachuska29617e52014-11-20 03:17:46 -08001309 }
Simon Huntd72bc702014-11-13 18:38:04 -08001310
Simon Hunta6a9fe72014-11-20 11:17:12 -08001311
Simon Hunt61d04042014-11-11 17:27:16 -08001312 // ==============================
Simon Hunta5e89142014-11-14 07:00:33 -08001313 // onos instance panel functions
Simon Huntb82f6902014-11-22 11:53:15 -08001314
Simon Hunt7b403bc2014-11-22 19:01:00 -08001315 var instCfg = {
1316 rectPad: 8,
1317 nodeOx: 9,
1318 nodeOy: 9,
1319 nodeDim: 40,
1320 birdOx: 19,
1321 birdOy: 21,
1322 birdDim: 21,
1323 uiDy: 45,
1324 titleDy: 30,
1325 textYOff: 20,
1326 textYSpc: 15
1327 };
1328
1329 function viewBox(dim) {
1330 return '0 0 ' + dim.w + ' ' + dim.h;
1331 }
1332
1333 function instRectAttr(dim) {
1334 var pad = instCfg.rectPad;
1335 return {
1336 x: pad,
1337 y: pad,
1338 width: dim.w - pad*2,
1339 height: dim.h - pad*2,
Simon Huntb0ecfa52014-11-23 21:05:12 -08001340 rx: 6
Simon Hunt7b403bc2014-11-22 19:01:00 -08001341 };
1342 }
1343
1344 function computeDim(self) {
1345 var css = window.getComputedStyle(self);
1346 return {
1347 w: stripPx(css.width),
1348 h: stripPx(css.height)
1349 };
Simon Huntb82f6902014-11-22 11:53:15 -08001350 }
Simon Hunta5e89142014-11-14 07:00:33 -08001351
1352 function updateInstances() {
1353 var onoses = oiBox.el.selectAll('.onosInst')
Simon Huntb82f6902014-11-22 11:53:15 -08001354 .data(onosOrder, function (d) { return d.id; }),
Simon Hunt7b403bc2014-11-22 19:01:00 -08001355 instDim = {w:0,h:0},
1356 c = instCfg;
Simon Hunta5e89142014-11-14 07:00:33 -08001357
Simon Hunt7b403bc2014-11-22 19:01:00 -08001358 function nSw(n) {
1359 return '# Switches: ' + n;
1360 }
Simon Hunta5e89142014-11-14 07:00:33 -08001361
Simon Huntb82f6902014-11-22 11:53:15 -08001362 // operate on existing onos instances if necessary
1363 onoses.each(function (d) {
1364 var el = d3.select(this),
1365 svg = el.select('svg');
Simon Hunt7b403bc2014-11-22 19:01:00 -08001366 instDim = computeDim(this);
Simon Huntb82f6902014-11-22 11:53:15 -08001367
1368 // update online state
1369 el.classed('online', d.online);
1370
1371 // update ui-attached state
1372 svg.select('use.uiBadge').remove();
1373 if (d.uiAttached) {
1374 attachUiBadge(svg);
1375 }
1376
Simon Hunt7b403bc2014-11-22 19:01:00 -08001377 function updAttr(id, value) {
1378 svg.select('text.instLabel.'+id).text(value);
1379 }
1380
1381 updAttr('ip', d.ip);
1382 updAttr('ns', nSw(d.switches));
Simon Huntb82f6902014-11-22 11:53:15 -08001383 });
1384
1385
1386 // operate on new onos instances
Simon Hunta5e89142014-11-14 07:00:33 -08001387 var entering = onoses.enter()
1388 .append('div')
1389 .attr('class', 'onosInst')
1390 .classed('online', function (d) { return d.online; })
Simon Hunt9c15eca2014-11-15 18:37:59 -08001391 .on('click', clickInst);
1392
Simon Huntb82f6902014-11-22 11:53:15 -08001393 entering.each(function (d) {
Simon Hunt9c15eca2014-11-15 18:37:59 -08001394 var el = d3.select(this),
Simon Hunt7b403bc2014-11-22 19:01:00 -08001395 rectAttr,
1396 svg;
1397 instDim = computeDim(this);
1398 rectAttr = instRectAttr(instDim);
Simon Hunt9c15eca2014-11-15 18:37:59 -08001399
Simon Hunt7b403bc2014-11-22 19:01:00 -08001400 svg = el.append('svg').attr({
1401 width: instDim.w,
1402 height: instDim.h,
1403 viewBox: viewBox(instDim)
Simon Hunt95908012014-11-20 10:20:26 -08001404 });
Simon Huntb82f6902014-11-22 11:53:15 -08001405
Simon Hunt7b403bc2014-11-22 19:01:00 -08001406 svg.append('rect').attr(rectAttr);
Simon Hunt9c15eca2014-11-15 18:37:59 -08001407
Thomas Vachuskae02e11c2014-11-24 16:13:52 -08001408 //appendGlyph(svg, c.nodeOx, c.nodeOy, c.nodeDim, '#node');
1409 appendBadge(svg, 14, 14, 28, '#bird');
Simon Huntb82f6902014-11-22 11:53:15 -08001410
1411 if (d.uiAttached) {
1412 attachUiBadge(svg);
1413 }
1414
Simon Hunt7b403bc2014-11-22 19:01:00 -08001415 var left = c.nodeOx + c.nodeDim,
1416 len = rectAttr.width - left,
1417 hlen = len / 2,
1418 midline = hlen + left;
Simon Hunt9c15eca2014-11-15 18:37:59 -08001419
Simon Hunt7b403bc2014-11-22 19:01:00 -08001420 // title
1421 svg.append('text')
1422 .attr({
1423 class: 'instTitle',
1424 x: midline,
1425 y: c.titleDy
1426 })
1427 .text(d.id);
1428
1429 // a couple of attributes
1430 var ty = c.titleDy + c.textYOff;
1431
1432 function addAttr(id, label) {
1433 svg.append('text').attr({
1434 class: 'instLabel ' + id,
1435 x: midline,
1436 y: ty
1437 }).text(label);
1438 ty += c.textYSpc;
1439 }
1440
1441 addAttr('ip', d.ip);
1442 addAttr('ns', nSw(d.switches));
Simon Hunt9c15eca2014-11-15 18:37:59 -08001443 });
Simon Hunta5e89142014-11-14 07:00:33 -08001444
1445 // operate on existing + new onoses here
Simon Hunt8f40cce2014-11-23 15:57:30 -08001446 // set the affinity colors...
1447 onoses.each(function (d) {
1448 var el = d3.select(this),
1449 rect = el.select('svg').select('rect'),
1450 col = instColor(d.id, d.online);
1451 rect.style('fill', col);
1452 });
Simon Hunta5e89142014-11-14 07:00:33 -08001453
Simon Hunt7b403bc2014-11-22 19:01:00 -08001454 // adjust the panel size appropriately...
1455 oiBox.width(instDim.w * onosOrder.length);
1456 oiBox.height(instDim.h);
1457
1458 // remove any outgoing instances
1459 onoses.exit().remove();
Simon Hunta5e89142014-11-14 07:00:33 -08001460 }
1461
Simon Hunt8f40cce2014-11-23 15:57:30 -08001462 function instColor(id, online) {
1463 return cat7.get(id, !online, network.view.getTheme());
1464 }
1465
Simon Hunt9462e8c2014-11-14 17:28:09 -08001466 function clickInst(d) {
1467 var el = d3.select(this),
1468 aff = el.classed('affinity');
1469 if (!aff) {
1470 setAffinity(el, d);
1471 } else {
1472 cancelAffinity();
1473 }
1474 }
1475
1476 function setAffinity(el, d) {
1477 d3.selectAll('.onosInst')
1478 .classed('mastership', true)
1479 .classed('affinity', false);
1480 el.classed('affinity', true);
1481
1482 suppressLayers(true);
1483 node.each(function (n) {
1484 if (n.master === d.id) {
1485 n.el.classed('suppressed', false);
1486 }
1487 });
1488 oiShowMaster = true;
1489 }
1490
1491 function cancelAffinity() {
1492 d3.selectAll('.onosInst')
1493 .classed('mastership affinity', false);
1494 restoreLayerState();
1495 oiShowMaster = false;
1496 }
1497
Simon Hunt7b403bc2014-11-22 19:01:00 -08001498 // TODO: these should be moved out to utility module.
1499 function stripPx(s) {
1500 return s.replace(/px$/,'');
1501 }
1502
1503 function appendUse(svg, ox, oy, dim, iid, cls) {
1504 var use = svg.append('use').attr({
1505 transform: translate(ox,oy),
1506 'xlink:href': iid,
1507 width: dim,
1508 height: dim
1509 });
1510 if (cls) {
1511 use.classed(cls, true);
1512 }
1513 return use;
1514 }
1515
1516 function appendGlyph(svg, ox, oy, dim, iid, cls) {
1517 appendUse(svg, ox, oy, dim, iid, cls).classed('glyphIcon', true);
1518 }
1519
1520 function appendBadge(svg, ox, oy, dim, iid, cls) {
1521 appendUse(svg, ox, oy, dim, iid,cls ).classed('badgeIcon', true);
1522 }
1523
1524 function attachUiBadge(svg) {
1525 appendBadge(svg, 12, instCfg.uiDy, 30, '#uiAttached', 'uiBadge');
1526 }
1527
Simon Hunt434cf142014-11-24 11:10:28 -08001528 function visVal(b) {
1529 return b ? 'visible' : 'hidden';
1530 }
1531
Simon Hunta5e89142014-11-14 07:00:33 -08001532 // ==============================
Simon Hunt99c13842014-11-06 18:23:12 -08001533 // force layout modification functions
1534
1535 function translate(x, y) {
1536 return 'translate(' + x + ',' + y + ')';
1537 }
Simon Huntd6f5a272014-11-29 23:45:50 -08001538 function scale(x,y) {
1539 return 'scale(' + x + ',' + y + ')';
1540 }
1541 function skewX(x) {
1542 return 'skewX(' + x + ')';
1543 }
Simon Hunte2575b62014-11-18 15:25:53 -08001544 function rotate(deg) {
1545 return 'rotate(' + deg + ')';
1546 }
1547
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001548 function missMsg(what, id) {
1549 return '\n[' + what + '] "' + id + '" missing ';
1550 }
1551
1552 function linkEndPoints(srcId, dstId) {
1553 var srcNode = network.lookup[srcId],
1554 dstNode = network.lookup[dstId],
1555 sMiss = !srcNode ? missMsg('src', srcId) : '',
1556 dMiss = !dstNode ? missMsg('dst', dstId) : '';
1557
1558 if (sMiss || dMiss) {
1559 logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
1560 return null;
1561 }
1562 return {
1563 source: srcNode,
1564 target: dstNode,
1565 x1: srcNode.x,
1566 y1: srcNode.y,
1567 x2: dstNode.x,
1568 y2: dstNode.y
1569 };
1570 }
1571
Simon Hunt56d51852014-11-09 13:03:35 -08001572 function createHostLink(host) {
1573 var src = host.id,
1574 dst = host.cp.device,
Simon Hunt7cd48f32014-11-09 23:42:50 -08001575 id = host.ingress,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001576 lnk = linkEndPoints(src, dst);
Simon Hunt56d51852014-11-09 13:03:35 -08001577
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001578 if (!lnk) {
Simon Hunt56d51852014-11-09 13:03:35 -08001579 return null;
1580 }
1581
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001582 // Synthesize link ...
1583 $.extend(lnk, {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001584 key: id,
Simon Hunt56d51852014-11-09 13:03:35 -08001585 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -08001586
1587 type: function () { return 'hostLink'; },
Simon Hunt6d9bd032014-11-28 22:16:40 -08001588 online: function () {
1589 // hostlink target is edge switch
1590 return lnk.target.online;
1591 },
Simon Hunt8257f4c2014-11-16 19:34:54 -08001592 linkWidth: function () { return 1; }
Simon Hunt7cd48f32014-11-09 23:42:50 -08001593 });
Simon Hunt99c13842014-11-06 18:23:12 -08001594 return lnk;
1595 }
1596
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001597 function createLink(link) {
Simon Hunta6a9fe72014-11-20 11:17:12 -08001598 var lnk = linkEndPoints(link.src, link.dst);
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001599
1600 if (!lnk) {
1601 return null;
1602 }
1603
Simon Hunt8257f4c2014-11-16 19:34:54 -08001604 $.extend(lnk, {
1605 key: link.id,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001606 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -08001607 fromSource: link,
1608
1609 // functions to aggregate dual link state
1610 type: function () {
1611 var s = lnk.fromSource,
1612 t = lnk.fromTarget;
1613 return (s && s.type) || (t && t.type) || defaultLinkType;
1614 },
1615 online: function () {
1616 var s = lnk.fromSource,
Simon Hunt6d9bd032014-11-28 22:16:40 -08001617 t = lnk.fromTarget,
1618 both = lnk.source.online && lnk.target.online;
1619 return both && ((s && s.online) || (t && t.online));
Simon Hunt8257f4c2014-11-16 19:34:54 -08001620 },
1621 linkWidth: function () {
1622 var s = lnk.fromSource,
1623 t = lnk.fromTarget,
1624 ws = (s && s.linkWidth) || 0,
1625 wt = (t && t.linkWidth) || 0;
1626 return Math.max(ws, wt);
1627 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001628 });
1629 return lnk;
Simon Hunt1a9eff92014-11-07 11:06:34 -08001630 }
1631
Simon Hunte2575b62014-11-18 15:25:53 -08001632 function removeLinkLabels() {
1633 network.links.forEach(function (d) {
1634 d.label = '';
1635 });
1636 }
1637
Simon Hunt434cf142014-11-24 11:10:28 -08001638 function showHostVis(el) {
1639 el.style('visibility', visVal(showHosts));
1640 }
1641
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001642 var widthRatio = 1.4,
1643 linkScale = d3.scale.linear()
1644 .domain([1, 12])
1645 .range([widthRatio, 12 * widthRatio])
1646 .clamp(true);
1647
Simon Hunt99c13842014-11-06 18:23:12 -08001648 function updateLinks() {
1649 link = linkG.selectAll('.link')
Simon Hunt8257f4c2014-11-16 19:34:54 -08001650 .data(network.links, function (d) { return d.key; });
Simon Hunt99c13842014-11-06 18:23:12 -08001651
1652 // operate on existing links, if necessary
1653 // link .foo() .bar() ...
1654
1655 // operate on entering links:
1656 var entering = link.enter()
1657 .append('line')
1658 .attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001659 x1: function (d) { return d.x1; },
1660 y1: function (d) { return d.y1; },
1661 x2: function (d) { return d.x2; },
1662 y2: function (d) { return d.y2; },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001663 stroke: config.topo.linkInColor,
1664 'stroke-width': config.topo.linkInWidth
Simon Hunt99c13842014-11-06 18:23:12 -08001665 });
1666
1667 // augment links
Simon Hunt7cd48f32014-11-09 23:42:50 -08001668 entering.each(function (d) {
1669 var link = d3.select(this);
1670 // provide ref to element selection from backing data....
1671 d.el = link;
Simon Hunt8257f4c2014-11-16 19:34:54 -08001672 restyleLinkElement(d);
Simon Hunt434cf142014-11-24 11:10:28 -08001673 if (d.type() === 'hostLink') {
1674 showHostVis(link);
1675 }
Simon Hunt7cd48f32014-11-09 23:42:50 -08001676 });
Thomas Vachuska4830d392014-11-09 17:09:56 -08001677
1678 // operate on both existing and new links, if necessary
1679 //link .foo() .bar() ...
1680
Simon Hunte2575b62014-11-18 15:25:53 -08001681 // apply or remove labels
1682 var labelData = getLabelData();
1683 applyLinkLabels(labelData);
1684
Thomas Vachuska4830d392014-11-09 17:09:56 -08001685 // operate on exiting links:
Thomas Vachuska4830d392014-11-09 17:09:56 -08001686 link.exit()
Simon Hunt6d9bd032014-11-28 22:16:40 -08001687 .attr('stroke-dasharray', '3 3')
Simon Hunt13bf9c82014-11-18 07:26:44 -08001688 .style('opacity', 0.5)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001689 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -08001690 .duration(1500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001691 .attr({
Simon Hunt6d9bd032014-11-28 22:16:40 -08001692 'stroke-dasharray': '3 12',
Simon Hunt13bf9c82014-11-18 07:26:44 -08001693 stroke: config.topo.linkOutColor,
1694 'stroke-width': config.topo.linkOutWidth
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001695 })
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001696 .style('opacity', 0.0)
Thomas Vachuska4830d392014-11-09 17:09:56 -08001697 .remove();
Simon Hunte2575b62014-11-18 15:25:53 -08001698
1699 // NOTE: invoke a single tick to force the labels to position
1700 // onto their links.
1701 tick();
1702 }
1703
1704 function getLabelData() {
1705 // create the backing data for showing labels..
1706 var data = [];
1707 link.each(function (d) {
1708 if (d.label) {
1709 data.push({
1710 id: 'lab-' + d.key,
1711 key: d.key,
1712 label: d.label,
1713 ldata: d
1714 });
1715 }
1716 });
1717 return data;
1718 }
1719
1720 var linkLabelOffset = '0.3em';
1721
1722 function applyLinkLabels(data) {
1723 var entering;
1724
1725 linkLabel = linkLabelG.selectAll('.linkLabel')
1726 .data(data, function (d) { return d.id; });
1727
Simon Hunt56a2ea42014-11-19 12:39:31 -08001728 // for elements already existing, we need to update the text
1729 // and adjust the rectangle size to fit
1730 linkLabel.each(function (d) {
1731 var el = d3.select(this),
1732 rect = el.select('rect'),
1733 text = el.select('text');
1734 text.text(d.label);
1735 rect.attr(rectAroundText(el));
1736 });
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -08001737
Simon Hunte2575b62014-11-18 15:25:53 -08001738 entering = linkLabel.enter().append('g')
1739 .classed('linkLabel', true)
1740 .attr('id', function (d) { return d.id; });
1741
1742 entering.each(function (d) {
1743 var el = d3.select(this),
1744 rect,
1745 text,
1746 parms = {
1747 x1: d.ldata.x1,
1748 y1: d.ldata.y1,
1749 x2: d.ldata.x2,
1750 y2: d.ldata.y2
1751 };
1752
1753 d.el = el;
1754 rect = el.append('rect');
1755 text = el.append('text').text(d.label);
1756 rect.attr(rectAroundText(el));
1757 text.attr('dy', linkLabelOffset);
1758
1759 el.attr('transform', transformLabel(parms));
1760 });
1761
1762 // Remove any links that are no longer required.
1763 linkLabel.exit().remove();
1764 }
1765
1766 function rectAroundText(el) {
1767 var text = el.select('text'),
1768 box = text.node().getBBox();
1769
1770 // translate the bbox so that it is centered on [x,y]
1771 box.x = -box.width / 2;
1772 box.y = -box.height / 2;
1773
1774 // add padding
1775 box.x -= 1;
1776 box.width += 2;
1777 return box;
1778 }
1779
1780 function transformLabel(p) {
1781 var dx = p.x2 - p.x1,
1782 dy = p.y2 - p.y1,
1783 xMid = dx/2 + p.x1,
1784 yMid = dy/2 + p.y1;
Simon Hunte2575b62014-11-18 15:25:53 -08001785 return translate(xMid, yMid);
Simon Hunt99c13842014-11-06 18:23:12 -08001786 }
1787
1788 function createDeviceNode(device) {
1789 // start with the object as is
1790 var node = device,
Simon Huntbb282f52014-11-10 11:08:19 -08001791 type = device.type,
Simon Huntc72967b2014-11-20 09:21:42 -08001792 svgCls = type ? 'node device ' + type : 'node device',
1793 labels = device.labels || [];
1794
Simon Hunt99c13842014-11-06 18:23:12 -08001795 // Augment as needed...
1796 node.class = 'device';
Simon Huntbb282f52014-11-10 11:08:19 -08001797 node.svgClass = device.online ? svgCls + ' online' : svgCls;
Simon Hunt99c13842014-11-06 18:23:12 -08001798 positionNode(node);
Simon Hunt99c13842014-11-06 18:23:12 -08001799 return node;
1800 }
1801
Simon Hunt56d51852014-11-09 13:03:35 -08001802 function createHostNode(host) {
1803 // start with the object as is
1804 var node = host;
1805
1806 // Augment as needed...
1807 node.class = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001808 if (!node.type) {
Simon Hunt209155e2014-11-21 12:16:09 -08001809 node.type = 'endstation';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001810 }
Simon Hunt7fa116d2014-11-17 14:16:55 -08001811 node.svgClass = 'node host ' + node.type;
Simon Hunt56d51852014-11-09 13:03:35 -08001812 positionNode(node);
Simon Hunt56d51852014-11-09 13:03:35 -08001813 return node;
1814 }
1815
Simon Hunt62c47542014-11-22 22:16:32 -08001816 function positionNode(node, forUpdate) {
Simon Hunt99c13842014-11-06 18:23:12 -08001817 var meta = node.metaUi,
Simon Huntac9e24f2014-11-12 10:12:21 -08001818 x = meta && meta.x,
1819 y = meta && meta.y,
1820 xy;
Simon Hunt99c13842014-11-06 18:23:12 -08001821
Simon Huntac9e24f2014-11-12 10:12:21 -08001822 // If we have [x,y] already, use that...
Simon Hunt99c13842014-11-06 18:23:12 -08001823 if (x && y) {
1824 node.fixed = true;
Simon Hunt62c47542014-11-22 22:16:32 -08001825 node.px = node.x = x;
1826 node.py = node.y = y;
Simon Huntac9e24f2014-11-12 10:12:21 -08001827 return;
Simon Hunt99c13842014-11-06 18:23:12 -08001828 }
Simon Huntac9e24f2014-11-12 10:12:21 -08001829
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001830 var location = node.location;
1831 if (location && location.type === 'latlng') {
Simon Hunt1b18aa52014-11-29 17:57:55 -08001832 var coord = geoMapProj([location.lng, location.lat]);
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001833 node.fixed = true;
Simon Hunt62c47542014-11-22 22:16:32 -08001834 node.px = node.x = coord[0];
1835 node.py = node.y = coord[1];
Simon Hunt62c47542014-11-22 22:16:32 -08001836 return true;
1837 }
1838
1839 // if this is a node update (not a node add).. skip randomizer
1840 if (forUpdate) {
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001841 return;
1842 }
1843
Simon Huntac9e24f2014-11-12 10:12:21 -08001844 // Note: Placing incoming unpinned nodes at exactly the same point
1845 // (center of the view) causes them to explode outwards when
1846 // the force layout kicks in. So, we spread them out a bit
1847 // initially, to provide a more serene layout convergence.
1848 // Additionally, if the node is a host, we place it near
1849 // the device it is connected to.
1850
1851 function spread(s) {
1852 return Math.floor((Math.random() * s) - s/2);
1853 }
1854
1855 function randDim(dim) {
1856 return dim / 2 + spread(dim * 0.7071);
1857 }
1858
1859 function rand() {
1860 return {
1861 x: randDim(network.view.width()),
1862 y: randDim(network.view.height())
1863 };
1864 }
1865
1866 function near(node) {
1867 var min = 12,
1868 dx = spread(12),
1869 dy = spread(12);
1870 return {
1871 x: node.x + min + dx,
1872 y: node.y + min + dy
1873 };
1874 }
1875
1876 function getDevice(cp) {
1877 var d = network.lookup[cp.device];
1878 return d || rand();
1879 }
1880
1881 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
1882 $.extend(node, xy);
Simon Hunt99c13842014-11-06 18:23:12 -08001883 }
1884
Simon Hunt99c13842014-11-06 18:23:12 -08001885
Simon Huntc72967b2014-11-20 09:21:42 -08001886 function iconGlyphUrl(d) {
1887 var which = d.type || 'unknown';
1888 return '#' + which;
1889 }
1890
Simon Hunt99c13842014-11-06 18:23:12 -08001891 // returns the newly computed bounding box of the rectangle
1892 function adjustRectToFitText(n) {
1893 var text = n.select('text'),
1894 box = text.node().getBBox(),
1895 lab = config.labels;
1896
1897 text.attr('text-anchor', 'middle')
1898 .attr('y', '-0.8em')
1899 .attr('x', lab.imgPad/2);
1900
1901 // translate the bbox so that it is centered on [x,y]
1902 box.x = -box.width / 2;
1903 box.y = -box.height / 2;
1904
1905 // add padding
1906 box.x -= (lab.padLR + lab.imgPad/2);
1907 box.width += lab.padLR * 2 + lab.imgPad;
1908 box.y -= lab.padTB;
1909 box.height += lab.padTB * 2;
1910
1911 return box;
1912 }
1913
Simon Hunt1a9eff92014-11-07 11:06:34 -08001914 function mkSvgClass(d) {
1915 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
1916 }
1917
Simon Hunt7cd48f32014-11-09 23:42:50 -08001918 function hostLabel(d) {
1919 var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0;
1920 return d.labels[idx];
1921 }
1922 function deviceLabel(d) {
1923 var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0;
1924 return d.labels[idx];
1925 }
Simon Huntc72967b2014-11-20 09:21:42 -08001926 function trimLabel(label) {
1927 return (label && label.trim()) || '';
1928 }
1929
1930 function emptyBox() {
1931 return {
1932 x: -2,
1933 y: -2,
1934 width: 4,
1935 height: 4
1936 };
Simon Hunt7cd48f32014-11-09 23:42:50 -08001937 }
1938
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001939 function updateDeviceLabel(d) {
Simon Huntc72967b2014-11-20 09:21:42 -08001940 var label = trimLabel(deviceLabel(d)),
1941 noLabel = !label,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001942 node = d.el,
Simon Huntc72967b2014-11-20 09:21:42 -08001943 box,
1944 dx,
Simon Hunt395a70c2014-11-22 23:17:40 -08001945 dy,
1946 cfg = config.icons.device;
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001947
1948 node.select('text')
1949 .text(label)
1950 .style('opacity', 0)
1951 .transition()
1952 .style('opacity', 1);
1953
Simon Huntc72967b2014-11-20 09:21:42 -08001954 if (noLabel) {
1955 box = emptyBox();
Simon Hunt395a70c2014-11-22 23:17:40 -08001956 dx = -cfg.dim/2;
1957 dy = -cfg.dim/2;
Simon Huntc72967b2014-11-20 09:21:42 -08001958 } else {
1959 box = adjustRectToFitText(node);
Simon Hunt395a70c2014-11-22 23:17:40 -08001960 dx = box.x + cfg.xoff;
1961 dy = box.y + cfg.yoff;
Simon Huntc72967b2014-11-20 09:21:42 -08001962 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001963
1964 node.select('rect')
1965 .transition()
1966 .attr(box);
1967
Simon Huntc72967b2014-11-20 09:21:42 -08001968 node.select('g.deviceIcon')
Thomas Vachuska89543292014-11-19 11:28:33 -08001969 .transition()
Simon Huntc72967b2014-11-20 09:21:42 -08001970 .attr('transform', translate(dx, dy));
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001971 }
1972
1973 function updateHostLabel(d) {
Simon Hunt6d9bd032014-11-28 22:16:40 -08001974 var label = trimLabel(hostLabel(d));
1975 d.el.select('text').text(label);
Simon Huntbb282f52014-11-10 11:08:19 -08001976 }
1977
Simon Hunt434cf142014-11-24 11:10:28 -08001978 function updateHostVisibility() {
1979 var v = visVal(showHosts);
1980 nodeG.selectAll('.host').style('visibility', v);
1981 linkG.selectAll('.hostLink').style('visibility', v);
1982 }
1983
Simon Hunt6d9bd032014-11-28 22:16:40 -08001984 function findOfflineNodes() {
1985 var a = [];
1986 network.nodes.forEach(function (d) {
1987 if (d.class === 'device' && !d.online) {
1988 a.push(d);
1989 }
1990 });
1991 return a;
1992 }
1993
1994 function updateOfflineVisibility(dev) {
1995 var so = showOffline,
1996 sh = showHosts,
1997 vb = 'visibility',
1998 v, off, al, ah, db, b;
1999
2000 function updAtt(show) {
2001 al.forEach(function (d) {
2002 b = show && ((d.type() !== 'hostLink') || sh);
2003 d.el.style(vb, visVal(b));
2004 });
2005 ah.forEach(function (d) {
2006 b = show && sh;
2007 d.el.style(vb, visVal(b));
2008 });
2009 }
2010
2011 if (dev) {
2012 // updating a specific device that just toggled off/on-line
2013 db = dev.online || so;
2014 dev.el.style(vb, visVal(db));
2015 al = findAttachedLinks(dev.id);
2016 ah = findAttachedHosts(dev.id);
2017 updAtt(db);
2018 } else {
2019 // updating all offline devices
2020 v = visVal(so);
2021 off = findOfflineNodes();
2022 off.forEach(function (d) {
2023 d.el.style(vb, v);
2024 al = findAttachedLinks(d.id);
2025 ah = findAttachedHosts(d.id);
2026 updAtt(so);
2027 });
2028 }
2029 }
2030
Simon Hunt6ac93f32014-11-13 12:17:27 -08002031 function nodeMouseOver(d) {
Thomas Vachuska164fa5c2014-12-02 21:59:41 -08002032 if (hovered != d) {
2033 hovered = d;
2034 requestTrafficForMode();
2035 }
Simon Hunt6ac93f32014-11-13 12:17:27 -08002036 }
2037
2038 function nodeMouseOut(d) {
Thomas Vachuska164fa5c2014-12-02 21:59:41 -08002039 if (hovered != null) {
2040 hovered = null;
2041 requestTrafficForMode();
2042 }
Simon Hunt6ac93f32014-11-13 12:17:27 -08002043 }
Simon Huntbb282f52014-11-10 11:08:19 -08002044
Simon Hunteb1514d2014-11-20 09:57:29 -08002045 function addHostIcon(node, radius, iid) {
Thomas Vachuska89543292014-11-19 11:28:33 -08002046 var dim = radius * 1.5,
2047 xlate = -dim / 2;
2048
Simon Hunteb1514d2014-11-20 09:57:29 -08002049 node.append('use').attr({
2050 class: 'glyphIcon hostIcon',
2051 transform: translate(xlate,xlate),
2052 'xlink:href': iid,
2053 width: dim,
2054 height: dim
2055 });
Thomas Vachuska89543292014-11-19 11:28:33 -08002056 }
2057
Simon Hunt99c13842014-11-06 18:23:12 -08002058 function updateNodes() {
2059 node = nodeG.selectAll('.node')
2060 .data(network.nodes, function (d) { return d.id; });
2061
Simon Hunt62c47542014-11-22 22:16:32 -08002062 // operate on existing nodes...
2063 node.filter('.device').each(function (d) {
Simon Hunt62c47542014-11-22 22:16:32 -08002064 var node = d.el;
2065 node.classed('online', d.online);
2066 updateDeviceLabel(d);
2067 positionNode(d, true);
2068 });
2069
2070 node.filter('.host').each(function (d) {
Simon Hunt6d9bd032014-11-28 22:16:40 -08002071 updateHostLabel(d);
2072 positionNode(d, true);
Simon Hunt62c47542014-11-22 22:16:32 -08002073 });
Simon Hunt99c13842014-11-06 18:23:12 -08002074
2075 // operate on entering nodes:
2076 var entering = node.enter()
2077 .append('g')
2078 .attr({
2079 id: function (d) { return safeId(d.id); },
Simon Hunt1a9eff92014-11-07 11:06:34 -08002080 class: mkSvgClass,
Simon Hunt99c13842014-11-06 18:23:12 -08002081 transform: function (d) { return translate(d.x, d.y); },
2082 opacity: 0
2083 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08002084 .call(network.drag)
Simon Hunt6ac93f32014-11-13 12:17:27 -08002085 .on('mouseover', nodeMouseOver)
2086 .on('mouseout', nodeMouseOut)
Simon Hunt99c13842014-11-06 18:23:12 -08002087 .transition()
2088 .attr('opacity', 1);
2089
2090 // augment device nodes...
2091 entering.filter('.device').each(function (d) {
2092 var node = d3.select(this),
Simon Huntc72967b2014-11-20 09:21:42 -08002093 label = trimLabel(deviceLabel(d)),
2094 noLabel = !label,
Simon Hunt99c13842014-11-06 18:23:12 -08002095 box;
2096
Simon Hunt7cd48f32014-11-09 23:42:50 -08002097 // provide ref to element from backing data....
2098 d.el = node;
2099
Simon Hunt62c47542014-11-22 22:16:32 -08002100 node.append('rect').attr({ rx: 5, ry: 5 });
2101 node.append('text').text(label).attr('dy', '1.1em');
Simon Hunt99c13842014-11-06 18:23:12 -08002102 box = adjustRectToFitText(node);
Simon Hunta3dd9572014-11-20 15:22:41 -08002103 node.select('rect').attr(box);
Simon Huntc72967b2014-11-20 09:21:42 -08002104 addDeviceIcon(node, box, noLabel, iconGlyphUrl(d));
Simon Huntc7ee0662014-11-05 16:44:37 -08002105 });
Simon Hunt934c3ce2014-11-05 11:45:07 -08002106
Thomas Vachuska89543292014-11-19 11:28:33 -08002107
Simon Hunt56d51852014-11-09 13:03:35 -08002108 // augment host nodes...
2109 entering.filter('.host').each(function (d) {
2110 var node = d3.select(this),
Simon Hunt395a70c2014-11-22 23:17:40 -08002111 cfg = config.icons.host,
2112 r = cfg.radius[d.type] || cfg.defaultRadius,
Thomas Vachuska89543292014-11-19 11:28:33 -08002113 textDy = r + 10,
Simon Hunteb1514d2014-11-20 09:57:29 -08002114 iid = iconGlyphUrl(d);
Simon Hunt56d51852014-11-09 13:03:35 -08002115
Simon Hunt7cd48f32014-11-09 23:42:50 -08002116 // provide ref to element from backing data....
2117 d.el = node;
Simon Hunt434cf142014-11-24 11:10:28 -08002118 showHostVis(node);
Simon Hunt7cd48f32014-11-09 23:42:50 -08002119
Simon Hunt62c47542014-11-22 22:16:32 -08002120 node.append('circle').attr('r', r);
Simon Hunteb1514d2014-11-20 09:57:29 -08002121 if (iid) {
2122 addHostIcon(node, r, iid);
Simon Hunt7fa116d2014-11-17 14:16:55 -08002123 }
Simon Hunt56d51852014-11-09 13:03:35 -08002124 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08002125 .text(hostLabel)
Thomas Vachuska89543292014-11-19 11:28:33 -08002126 .attr('dy', textDy)
Simon Hunt7cd48f32014-11-09 23:42:50 -08002127 .attr('text-anchor', 'middle');
Simon Hunt56d51852014-11-09 13:03:35 -08002128 });
Simon Huntc7ee0662014-11-05 16:44:37 -08002129
Simon Hunt99c13842014-11-06 18:23:12 -08002130 // operate on both existing and new nodes, if necessary
Simon Huntb0ecfa52014-11-23 21:05:12 -08002131 updateDeviceColors();
Simon Huntc7ee0662014-11-05 16:44:37 -08002132
Simon Hunt99c13842014-11-06 18:23:12 -08002133 // operate on exiting nodes:
Simon Huntea80eb42014-11-11 13:46:57 -08002134 // Note that the node is removed after 2 seconds.
2135 // Sub element animations should be shorter than 2 seconds.
2136 var exiting = node.exit()
Simon Hunt44031102014-11-11 13:20:36 -08002137 .transition()
2138 .duration(2000)
Simon Huntea80eb42014-11-11 13:46:57 -08002139 .style('opacity', 0)
Simon Hunt99c13842014-11-06 18:23:12 -08002140 .remove();
Simon Huntea80eb42014-11-11 13:46:57 -08002141
2142 // host node exits....
2143 exiting.filter('.host').each(function (d) {
Simon Huntca867ac2014-11-28 18:07:35 -08002144 var node = d.el;
2145 node.select('use')
2146 .style('opacity', 0.5)
2147 .transition()
2148 .duration(800)
2149 .style('opacity', 0);
Simon Huntea80eb42014-11-11 13:46:57 -08002150
2151 node.select('text')
2152 .style('opacity', 0.5)
2153 .transition()
Simon Huntca867ac2014-11-28 18:07:35 -08002154 .duration(800)
Simon Huntea80eb42014-11-11 13:46:57 -08002155 .style('opacity', 0);
Simon Huntea80eb42014-11-11 13:46:57 -08002156
Thomas Vachuska89543292014-11-19 11:28:33 -08002157 node.select('circle')
2158 .style('stroke-fill', '#555')
2159 .style('fill', '#888')
Simon Huntea80eb42014-11-11 13:46:57 -08002160 .style('opacity', 0.5)
2161 .transition()
2162 .duration(1500)
2163 .attr('r', 0);
Simon Huntea80eb42014-11-11 13:46:57 -08002164 });
2165
Simon Huntca867ac2014-11-28 18:07:35 -08002166 // device node exits....
2167 exiting.filter('.device').each(function (d) {
2168 var node = d.el;
2169 node.select('use')
2170 .style('opacity', 0.5)
2171 .transition()
2172 .duration(800)
2173 .style('opacity', 0);
2174
2175 node.selectAll('rect')
2176 .style('stroke-fill', '#555')
2177 .style('fill', '#888')
2178 .style('opacity', 0.5);
2179 });
Simon Huntd6f5a272014-11-29 23:45:50 -08002180 fResume();
Simon Huntc7ee0662014-11-05 16:44:37 -08002181 }
2182
Simon Hunt95dad922014-11-24 09:43:31 -08002183 var dCol = {
2184 black: '#000',
2185 paleblue: '#acf',
2186 offwhite: '#ddd',
2187 midgrey: '#888',
2188 lightgrey: '#bbb',
2189 orange: '#f90'
2190 };
2191
Simon Huntb0ecfa52014-11-23 21:05:12 -08002192 // note: these are the device icon colors without affinity
Simon Hunt95dad922014-11-24 09:43:31 -08002193 var dColTheme = {
Simon Huntb0ecfa52014-11-23 21:05:12 -08002194 light: {
2195 online: {
Simon Hunt95dad922014-11-24 09:43:31 -08002196 glyph: dCol.black,
2197 rect: dCol.paleblue
Simon Huntb0ecfa52014-11-23 21:05:12 -08002198 },
2199 offline: {
Simon Hunt95dad922014-11-24 09:43:31 -08002200 glyph: dCol.midgrey,
2201 rect: dCol.lightgrey
Simon Huntb0ecfa52014-11-23 21:05:12 -08002202 }
2203 },
Simon Hunt95dad922014-11-24 09:43:31 -08002204 // TODO: theme
Simon Huntb0ecfa52014-11-23 21:05:12 -08002205 dark: {
2206 online: {
Simon Hunt95dad922014-11-24 09:43:31 -08002207 glyph: dCol.black,
2208 rect: dCol.paleblue
Simon Huntb0ecfa52014-11-23 21:05:12 -08002209 },
2210 offline: {
Simon Hunt95dad922014-11-24 09:43:31 -08002211 glyph: dCol.midgrey,
2212 rect: dCol.lightgrey
Simon Huntb0ecfa52014-11-23 21:05:12 -08002213 }
2214 }
2215 };
2216
2217 function devBaseColor(d) {
2218 var t = network.view.getTheme(),
2219 o = d.online ? 'online' : 'offline';
Simon Hunt95dad922014-11-24 09:43:31 -08002220 return dColTheme[t][o];
Simon Huntb0ecfa52014-11-23 21:05:12 -08002221 }
2222
2223 function setDeviceColor(d) {
2224 var o = d.online,
2225 s = d.el.classed('selected'),
2226 c = devBaseColor(d),
2227 a = instColor(d.master, o),
2228 g, r,
2229 icon = d.el.select('g.deviceIcon');
2230
2231 if (s) {
2232 g = c.glyph;
Simon Hunt95dad922014-11-24 09:43:31 -08002233 r = dColTheme.sel;
Simon Huntb0ecfa52014-11-23 21:05:12 -08002234 } else if (colorAffinity) {
2235 g = o ? a : c.glyph;
Simon Hunt95dad922014-11-24 09:43:31 -08002236 r = o ? dCol.offwhite : a;
Simon Huntb0ecfa52014-11-23 21:05:12 -08002237 } else {
2238 g = c.glyph;
2239 r = c.rect;
2240 }
2241
2242 icon.select('use')
2243 .style('fill', g);
2244 icon.select('rect')
2245 .style('fill', r);
2246 }
2247
Simon Huntc72967b2014-11-20 09:21:42 -08002248 function addDeviceIcon(node, box, noLabel, iid) {
2249 var cfg = config.icons.device,
2250 dx,
2251 dy,
2252 g;
2253
2254 if (noLabel) {
2255 box = emptyBox();
2256 dx = -cfg.dim/2;
2257 dy = -cfg.dim/2;
2258 } else {
2259 box = adjustRectToFitText(node);
Simon Hunt395a70c2014-11-22 23:17:40 -08002260 dx = box.x + cfg.xoff;
2261 dy = box.y + cfg.yoff;
Simon Huntc72967b2014-11-20 09:21:42 -08002262 }
2263
Simon Hunteb1514d2014-11-20 09:57:29 -08002264 g = node.append('g')
2265 .attr('class', 'glyphIcon deviceIcon')
Simon Huntc72967b2014-11-20 09:21:42 -08002266 .attr('transform', translate(dx, dy));
2267
2268 g.append('rect').attr({
2269 x: 0,
2270 y: 0,
2271 rx: cfg.rx,
2272 width: cfg.dim,
2273 height: cfg.dim
2274 });
2275
2276 g.append('use').attr({
2277 'xlink:href': iid,
2278 width: cfg.dim,
2279 height: cfg.dim
2280 });
2281
Simon Huntc72967b2014-11-20 09:21:42 -08002282 }
2283
Simon Hunt7b403bc2014-11-22 19:01:00 -08002284 function find(key, array, tag) {
Simon Huntca867ac2014-11-28 18:07:35 -08002285 var _tag = tag || 'id',
Simon Hunt7b403bc2014-11-22 19:01:00 -08002286 idx, n, d;
2287 for (idx = 0, n = array.length; idx < n; idx++) {
2288 d = array[idx];
2289 if (d[_tag] === key) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08002290 return idx;
2291 }
2292 }
2293 return -1;
2294 }
2295
Simon Huntca867ac2014-11-28 18:07:35 -08002296 function removeLinkElement(d) {
2297 var idx = find(d.key, network.links, 'key'),
Simon Hunt8257f4c2014-11-16 19:34:54 -08002298 removed;
2299 if (idx >=0) {
2300 // remove from links array
2301 removed = network.links.splice(idx, 1);
2302 // remove from lookup cache
2303 delete network.lookup[removed[0].key];
2304 updateLinks();
Simon Huntd6f5a272014-11-29 23:45:50 -08002305 fResume();
Simon Hunt8257f4c2014-11-16 19:34:54 -08002306 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08002307 }
Simon Huntc7ee0662014-11-05 16:44:37 -08002308
Simon Huntca867ac2014-11-28 18:07:35 -08002309 function removeHostElement(d, upd) {
2310 var lu = network.lookup;
Simon Hunt44031102014-11-11 13:20:36 -08002311 // first, remove associated hostLink...
Simon Huntca867ac2014-11-28 18:07:35 -08002312 removeLinkElement(d.linkData);
2313
2314 // remove hostLink bindings
2315 delete lu[d.ingress];
2316 delete lu[d.egress];
Simon Hunt44031102014-11-11 13:20:36 -08002317
2318 // remove from lookup cache
Simon Huntca867ac2014-11-28 18:07:35 -08002319 delete lu[d.id];
Simon Hunt44031102014-11-11 13:20:36 -08002320 // remove from nodes array
Simon Huntca867ac2014-11-28 18:07:35 -08002321 var idx = find(d.id, network.nodes);
2322 network.nodes.splice(idx, 1);
2323 // remove from SVG
2324 // NOTE: upd is false if we were called from removeDeviceElement()
2325 if (upd) {
2326 updateNodes();
Simon Huntd6f5a272014-11-29 23:45:50 -08002327 fResume();
Simon Huntca867ac2014-11-28 18:07:35 -08002328 }
2329 }
2330
2331
2332 function removeDeviceElement(d) {
2333 var id = d.id;
2334 // first, remove associated hosts and links..
2335 findAttachedHosts(id).forEach(removeHostElement);
2336 findAttachedLinks(id).forEach(removeLinkElement);
2337
2338 // remove from lookup cache
2339 delete network.lookup[id];
2340 // remove from nodes array
2341 var idx = find(id, network.nodes);
Simon Hunt44031102014-11-11 13:20:36 -08002342 network.nodes.splice(idx, 1);
2343 // remove from SVG
2344 updateNodes();
Simon Huntd6f5a272014-11-29 23:45:50 -08002345 fResume();
Simon Hunt44031102014-11-11 13:20:36 -08002346 }
2347
Simon Huntca867ac2014-11-28 18:07:35 -08002348 function findAttachedHosts(devId) {
2349 var hosts = [];
2350 network.nodes.forEach(function (d) {
2351 if (d.class === 'host' && d.cp.device === devId) {
2352 hosts.push(d);
2353 }
2354 });
2355 return hosts;
2356 }
2357
2358 function findAttachedLinks(devId) {
2359 var links = [];
2360 network.links.forEach(function (d) {
2361 if (d.source.id === devId || d.target.id === devId) {
2362 links.push(d);
2363 }
2364 });
2365 return links;
2366 }
Simon Hunt44031102014-11-11 13:20:36 -08002367
Simon Huntd6f5a272014-11-29 23:45:50 -08002368 function fResume() {
2369 if (!oblique) {
2370 network.force.resume();
2371 }
2372 }
Simon Huntc7ee0662014-11-05 16:44:37 -08002373
Simon Huntd6f5a272014-11-29 23:45:50 -08002374 function fStart() {
2375 if (!oblique) {
2376 network.force.start();
2377 }
2378 }
2379
2380 var tickStuff = {
2381 nodeAttr: {
2382 transform: function (d) { return translate(d.x, d.y); }
2383 },
2384 linkAttr: {
Simon Huntc7ee0662014-11-05 16:44:37 -08002385 x1: function (d) { return d.source.x; },
2386 y1: function (d) { return d.source.y; },
2387 x2: function (d) { return d.target.x; },
2388 y2: function (d) { return d.target.y; }
Simon Huntd6f5a272014-11-29 23:45:50 -08002389 },
2390 linkLabelAttr: {
2391 transform: function (d) {
2392 var lnk = findLinkById(d.key);
Simon Hunte2575b62014-11-18 15:25:53 -08002393
Simon Huntd6f5a272014-11-29 23:45:50 -08002394 if (lnk) {
2395 var parms = {
2396 x1: lnk.source.x,
2397 y1: lnk.source.y,
2398 x2: lnk.target.x,
2399 y2: lnk.target.y
2400 };
2401 return transformLabel(parms);
2402 }
Thomas Vachuska4731f122014-11-20 04:56:19 -08002403 }
Simon Huntd6f5a272014-11-29 23:45:50 -08002404 }
2405 };
2406
2407 function tick() {
2408 node.attr(tickStuff.nodeAttr);
2409 link.attr(tickStuff.linkAttr);
2410 linkLabel.attr(tickStuff.linkLabelAttr);
Simon Huntc7ee0662014-11-05 16:44:37 -08002411 }
Simon Hunt934c3ce2014-11-05 11:45:07 -08002412
2413 // ==============================
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002414 // Web-Socket for live data
2415
Thomas Vachuska12dfdc32014-11-29 16:03:12 -08002416 function findGuiSuccessor() {
2417 var idx = -1;
2418 onosOrder.forEach(function (d, i) {
2419 if (d.uiAttached) {
2420 idx = i;
2421 }
2422 });
2423
2424 for (var i = 0; i < onosOrder.length - 1; i++) {
2425 var ni = (idx + 1 + i) % onosOrder.length;
2426 if (onosOrder[ni].online) {
2427 return onosOrder[ni].ip;
2428 }
2429 }
2430 return null;
2431 }
2432
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002433 function webSockUrl() {
Thomas Vachuska12dfdc32014-11-29 16:03:12 -08002434 var url = document.location.toString()
2435 .replace(/\#.*/, '')
2436 .replace('http://', 'ws://')
2437 .replace('https://', 'wss://')
2438 .replace('index.html', config.webSockUrl);
2439 if (guiSuccessor) {
2440 url = url.replace(location.hostname, guiSuccessor);
2441 }
2442 return url;
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002443 }
2444
2445 webSock = {
2446 ws : null,
Thomas Vachuska12dfdc32014-11-29 16:03:12 -08002447 retries: 0,
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002448
2449 connect : function() {
2450 webSock.ws = new WebSocket(webSockUrl());
2451
2452 webSock.ws.onopen = function() {
Simon Hunt0c6d4192014-11-12 12:07:10 -08002453 noWebSock(false);
Thomas Vachuska47635c62014-11-22 01:21:36 -08002454 requestSummary();
Simon Huntb0ecfa52014-11-23 21:05:12 -08002455 showInstances();
Thomas Vachuska12dfdc32014-11-29 16:03:12 -08002456 webSock.retries = 0;
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002457 };
2458
2459 webSock.ws.onmessage = function(m) {
2460 if (m.data) {
Simon Huntbb282f52014-11-10 11:08:19 -08002461 wsTraceRx(m.data);
Thomas Vachuskad472c6e2014-11-07 19:11:05 -08002462 handleServerEvent(JSON.parse(m.data));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002463 }
2464 };
2465
2466 webSock.ws.onclose = function(m) {
2467 webSock.ws = null;
Thomas Vachuska12dfdc32014-11-29 16:03:12 -08002468 guiSuccessor = findGuiSuccessor();
2469 if (guiSuccessor && webSock.retries < onosOrder.length) {
2470 webSock.retries++;
2471 webSock.connect();
2472 } else {
2473 noWebSock(true);
2474 }
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002475 };
2476 },
2477
2478 send : function(text) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002479 if (text != null) {
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002480 webSock._send(text);
2481 }
2482 },
2483
2484 _send : function(message) {
2485 if (webSock.ws) {
2486 webSock.ws.send(message);
Simon Hunta255a2c2014-11-13 22:29:35 -08002487 } else if (config.useLiveData) {
Simon Hunt434cf142014-11-24 11:10:28 -08002488 console.warn('no web socket open', message);
Simon Hunta255a2c2014-11-13 22:29:35 -08002489 } else {
Simon Hunt434cf142014-11-24 11:10:28 -08002490 console.log('WS Send: ', message);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002491 }
2492 }
2493
2494 };
2495
Simon Hunt0c6d4192014-11-12 12:07:10 -08002496 function noWebSock(b) {
2497 mask.style('display',b ? 'block' : 'none');
2498 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002499
2500 function sendMessage(evType, payload) {
Simon Huntc1cc81c2014-11-29 14:59:01 -08002501 var p = payload || {},
2502 toSend = {
Simon Huntbb282f52014-11-10 11:08:19 -08002503 event: evType,
2504 sid: ++sid,
Simon Huntc1cc81c2014-11-29 14:59:01 -08002505 payload: p
Simon Huntbb282f52014-11-10 11:08:19 -08002506 },
2507 asText = JSON.stringify(toSend);
2508 wsTraceTx(asText);
2509 webSock.send(asText);
Simon Huntc76ae892014-11-18 17:31:51 -08002510
2511 // Temporary measure for debugging UI behavior ...
2512 if (!config.useLiveData) {
2513 handleTestSend(toSend);
2514 }
Simon Huntbb282f52014-11-10 11:08:19 -08002515 }
2516
2517 function wsTraceTx(msg) {
2518 wsTrace('tx', msg);
2519 }
2520 function wsTraceRx(msg) {
2521 wsTrace('rx', msg);
2522 }
2523 function wsTrace(rxtx, msg) {
Thomas Vachuska164fa5c2014-12-02 21:59:41 -08002524 // console.log('[' + rxtx + '] ' + msg);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002525 }
2526
Simon Huntc76ae892014-11-18 17:31:51 -08002527 // NOTE: Temporary hardcoded example for showing detail pane
2528 // while we fine-
2529 // Probably should not merge this change...
2530 function handleTestSend(msg) {
2531 if (msg.event === 'requestDetails') {
2532 showDetails({
2533 event: 'showDetails',
2534 sid: 1001,
2535 payload: {
2536 "id": "of:0000ffffffffff09",
2537 "type": "roadm",
2538 "propOrder": [
2539 "Name",
2540 "Vendor",
2541 "H/W Version",
2542 "S/W Version",
2543 "-",
2544 "Latitude",
2545 "Longitude",
2546 "Ports"
2547 ],
2548 "props": {
2549 "Name": null,
2550 "Vendor": "Linc",
2551 "H/W Version": "OE",
2552 "S/W Version": "?",
2553 "-": "",
2554 "Latitude": "40.8",
2555 "Longitude": "73.1",
2556 "Ports": "2"
2557 }
2558 }
2559 });
2560 }
2561 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002562
2563 // ==============================
2564 // Selection stuff
2565
2566 function selectObject(obj, el) {
2567 var n,
Simon Hunt01095ff2014-11-13 16:37:29 -08002568 srcEv = d3.event.sourceEvent,
2569 meta = srcEv.metaKey,
2570 shift = srcEv.shiftKey;
2571
Simon Hunt3c5ca542014-11-29 14:11:43 -08002572 // if the meta key is pressed, we are panning/zooming, so ignore
2573 if (meta) {
Simon Hunt01095ff2014-11-13 16:37:29 -08002574 return;
2575 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002576
2577 if (el) {
2578 n = d3.select(el);
2579 } else {
2580 node.each(function(d) {
2581 if (d == obj) {
2582 n = d3.select(el = this);
2583 }
2584 });
2585 }
2586 if (!n) return;
2587
Simon Hunt01095ff2014-11-13 16:37:29 -08002588 if (shift && n.classed('selected')) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002589 deselectObject(obj.id);
Simon Hunt61d04042014-11-11 17:27:16 -08002590 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002591 return;
2592 }
2593
Simon Hunt01095ff2014-11-13 16:37:29 -08002594 if (!shift) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002595 deselectAll();
2596 }
2597
Simon Huntc31d5692014-11-12 13:27:18 -08002598 selections[obj.id] = { obj: obj, el: el };
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002599 selectOrder.push(obj.id);
2600
2601 n.classed('selected', true);
Simon Huntb0ecfa52014-11-23 21:05:12 -08002602 updateDeviceColors(obj);
Simon Hunt61d04042014-11-11 17:27:16 -08002603 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002604 }
2605
2606 function deselectObject(id) {
Simon Huntc31d5692014-11-12 13:27:18 -08002607 var obj = selections[id],
2608 idx;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002609 if (obj) {
2610 d3.select(obj.el).classed('selected', false);
Simon Hunt61d04042014-11-11 17:27:16 -08002611 delete selections[id];
Simon Huntc31d5692014-11-12 13:27:18 -08002612 idx = $.inArray(id, selectOrder);
2613 if (idx >= 0) {
2614 selectOrder.splice(idx, 1);
2615 }
Simon Huntb0ecfa52014-11-23 21:05:12 -08002616 updateDeviceColors(obj.obj);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002617 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002618 }
2619
2620 function deselectAll() {
2621 // deselect all nodes in the network...
2622 node.classed('selected', false);
2623 selections = {};
2624 selectOrder = [];
Simon Huntb0ecfa52014-11-23 21:05:12 -08002625 updateDeviceColors();
Simon Hunt61d04042014-11-11 17:27:16 -08002626 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002627 }
2628
Simon Huntb0ecfa52014-11-23 21:05:12 -08002629 function updateDeviceColors(d) {
2630 if (d) {
2631 setDeviceColor(d);
2632 } else {
2633 node.filter('.device').each(function (d) {
2634 setDeviceColor(d);
2635 });
2636 }
Thomas Vachuska47635c62014-11-22 01:21:36 -08002637 }
2638
Simon Hunt61d04042014-11-11 17:27:16 -08002639 // update the state of the detail pane, based on current selections
2640 function updateDetailPane() {
2641 var nSel = selectOrder.length;
2642 if (!nSel) {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002643 emptySelect();
Simon Hunt61d04042014-11-11 17:27:16 -08002644 } else if (nSel === 1) {
2645 singleSelect();
2646 } else {
2647 multiSelect();
2648 }
2649 }
2650
Thomas Vachuska9edca302014-11-22 17:06:42 -08002651 function emptySelect() {
Simon Hunt27d322d2014-11-28 10:45:43 -08002652 haveDetails = false;
Simon Hunt06811b72014-11-25 18:54:48 -08002653 hideDetailPane();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002654 cancelTraffic();
2655 }
2656
Simon Hunt61d04042014-11-11 17:27:16 -08002657 function singleSelect() {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002658 // NOTE: detail is shown from showDetails event callback
Simon Hunt61d04042014-11-11 17:27:16 -08002659 requestDetails();
Thomas Vachuska164fa5c2014-12-02 21:59:41 -08002660 cancelTraffic();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002661 requestTrafficForMode();
Simon Hunt61d04042014-11-11 17:27:16 -08002662 }
2663
2664 function multiSelect() {
Simon Hunt27d322d2014-11-28 10:45:43 -08002665 haveDetails = true;
Simon Huntb53e0682014-11-12 13:32:01 -08002666 populateMultiSelect();
Thomas Vachuska164fa5c2014-12-02 21:59:41 -08002667 cancelTraffic();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002668 requestTrafficForMode();
Simon Huntb53e0682014-11-12 13:32:01 -08002669 }
2670
2671 function addSep(tbody) {
2672 var tr = tbody.append('tr');
2673 $('<hr>').appendTo(tr.append('td').attr('colspan', 2));
2674 }
2675
2676 function addProp(tbody, label, value) {
2677 var tr = tbody.append('tr');
2678
2679 tr.append('td')
2680 .attr('class', 'label')
2681 .text(label + ' :');
2682
2683 tr.append('td')
2684 .attr('class', 'value')
2685 .text(value);
2686 }
2687
2688 function populateMultiSelect() {
2689 detailPane.empty();
2690
Simon Hunta3dd9572014-11-20 15:22:41 -08002691 var title = detailPane.append('h3'),
2692 table = detailPane.append('table'),
2693 tbody = table.append('tbody');
Simon Huntb53e0682014-11-12 13:32:01 -08002694
Thomas Vachuska4731f122014-11-20 04:56:19 -08002695 title.text('Selected Nodes');
Simon Huntb53e0682014-11-12 13:32:01 -08002696
2697 selectOrder.forEach(function (d, i) {
2698 addProp(tbody, i+1, d);
2699 });
Simon Huntd72bc702014-11-13 18:38:04 -08002700
2701 addMultiSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08002702 }
2703
Thomas Vachuska47635c62014-11-22 01:21:36 -08002704 // TODO: refactor to consolidate with populateDetails
2705 function populateSummary(data) {
2706 summaryPane.empty();
2707
2708 var svg = summaryPane.append('svg'),
2709 iid = iconGlyphUrl(data);
2710
2711 var title = summaryPane.append('h2'),
2712 table = summaryPane.append('table'),
2713 tbody = table.append('tbody');
2714
2715 appendGlyph(svg, 0, 0, 40, iid);
2716
2717 svg.append('use')
2718 .attr({
2719 class: 'birdBadge',
2720 transform: translate(8,12),
2721 'xlink:href': '#bird',
2722 width: 24,
2723 height: 24,
2724 fill: '#fff'
2725 });
2726
2727 title.text('ONOS Summary');
2728
2729 data.propOrder.forEach(function(p) {
2730 if (p === '-') {
2731 addSep(tbody);
2732 } else {
2733 addProp(tbody, p, data.props[p]);
2734 }
2735 });
2736 }
2737
Simon Hunt61d04042014-11-11 17:27:16 -08002738 function populateDetails(data) {
2739 detailPane.empty();
2740
Simon Hunta6a9fe72014-11-20 11:17:12 -08002741 var svg = detailPane.append('svg'),
2742 iid = iconGlyphUrl(data);
2743
Simon Hunta3dd9572014-11-20 15:22:41 -08002744 var title = detailPane.append('h2'),
2745 table = detailPane.append('table'),
2746 tbody = table.append('tbody');
Simon Hunt61d04042014-11-11 17:27:16 -08002747
Simon Hunta6a9fe72014-11-20 11:17:12 -08002748 appendGlyph(svg, 0, 0, 40, iid);
2749 title.text(data.id);
Simon Hunt61d04042014-11-11 17:27:16 -08002750
2751 data.propOrder.forEach(function(p) {
2752 if (p === '-') {
2753 addSep(tbody);
2754 } else {
2755 addProp(tbody, p, data.props[p]);
2756 }
2757 });
Simon Huntd72bc702014-11-13 18:38:04 -08002758
Thomas Vachuska4731f122014-11-20 04:56:19 -08002759 addSingleSelectActions(data);
Simon Hunt61d04042014-11-11 17:27:16 -08002760 }
2761
Thomas Vachuska4731f122014-11-20 04:56:19 -08002762 function addSingleSelectActions(data) {
Simon Huntd72bc702014-11-13 18:38:04 -08002763 detailPane.append('hr');
2764 // always want to allow 'show traffic'
Thomas Vachuska164fa5c2014-12-02 21:59:41 -08002765 addAction(detailPane, 'Show Related Traffic', showRelatedIntentsAction);
Thomas Vachuska4731f122014-11-20 04:56:19 -08002766
2767 if (data.type === 'switch') {
2768 addAction(detailPane, 'Show Device Flows', showDeviceLinkFlowsAction);
2769 }
Simon Huntd72bc702014-11-13 18:38:04 -08002770 }
2771
2772 function addMultiSelectActions() {
2773 detailPane.append('hr');
2774 // always want to allow 'show traffic'
Thomas Vachuska164fa5c2014-12-02 21:59:41 -08002775 addAction(detailPane, 'Show Related Traffic', showRelatedIntentsAction);
Simon Huntd72bc702014-11-13 18:38:04 -08002776 // if exactly two hosts are selected, also want 'add host intent'
2777 if (nSel() === 2 && allSelectionsClass('host')) {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002778 addAction(detailPane, 'Create Host-to-Host Flow', addHostIntentAction);
2779 } else if (nSel() >= 2 && allSelectionsClass('host')) {
2780 addAction(detailPane, 'Create Multi-Source Flow', addMultiSourceIntentAction);
Simon Huntd72bc702014-11-13 18:38:04 -08002781 }
2782 }
2783
Simon Hunta5e89142014-11-14 07:00:33 -08002784 function addAction(panel, text, cb) {
2785 panel.append('div')
Simon Huntd72bc702014-11-13 18:38:04 -08002786 .classed('actionBtn', true)
2787 .text(text)
2788 .on('click', cb);
2789 }
2790
2791
Simon Hunt3c5ca542014-11-29 14:11:43 -08002792 // === Pan and Zoom behaviors...
2793
2794 function panZoom(translate, scale) {
2795 panZoomContainer.attr('transform',
2796 'translate(' + translate + ')scale(' + scale + ')');
Paul Greysonfcba0e82014-11-13 10:21:16 -08002797 // keep the map lines constant width while zooming
Simon Hunt3c5ca542014-11-29 14:11:43 -08002798 bgImg.style('stroke-width', 2.0 / scale + 'px');
Paul Greysonfcba0e82014-11-13 10:21:16 -08002799 }
2800
Simon Hunt3c5ca542014-11-29 14:11:43 -08002801 function resetPanZoom() {
2802 panZoom([0,0], 1);
2803 zoom.translate([0,0]).scale(1);
Paul Greysonfcba0e82014-11-13 10:21:16 -08002804 }
2805
Simon Hunt3c5ca542014-11-29 14:11:43 -08002806 function setupPanZoom() {
Paul Greysonfcba0e82014-11-13 10:21:16 -08002807 function zoomed() {
Simon Hunt3c5ca542014-11-29 14:11:43 -08002808 // pan zoom active when meta key is pressed...
2809 if (d3.event.sourceEvent.metaKey) {
2810 panZoom(d3.event.translate, d3.event.scale);
Paul Greysonfcba0e82014-11-13 10:21:16 -08002811 }
2812 }
2813
2814 zoom = d3.behavior.zoom()
2815 .translate([0, 0])
2816 .scale(1)
Simon Hunt1b18aa52014-11-29 17:57:55 -08002817 .scaleExtent([0.25, 10])
Paul Greysonfcba0e82014-11-13 10:21:16 -08002818 .on("zoom", zoomed);
2819
2820 svg.call(zoom);
2821 }
2822
Simon Hunt61d04042014-11-11 17:27:16 -08002823 // ==============================
2824 // Test harness code
Simon Hunt56d51852014-11-09 13:03:35 -08002825
2826 function prepareScenario(view, ctx, dbg) {
2827 var sc = scenario,
2828 urlSc = sc.evDir + ctx + sc.evScenario;
2829
2830 if (!ctx) {
2831 view.alert("No scenario specified (null ctx)");
2832 return;
2833 }
2834
2835 sc.view = view;
2836 sc.ctx = ctx;
2837 sc.debug = dbg;
2838 sc.evNumber = 0;
2839
2840 d3.json(urlSc, function(err, data) {
Simon Huntbb282f52014-11-10 11:08:19 -08002841 var p = data && data.params || {},
2842 desc = data && data.description || null,
Simon Huntfc274c92014-11-11 11:05:46 -08002843 intro = data && data.title;
Simon Huntbb282f52014-11-10 11:08:19 -08002844
Simon Hunt56d51852014-11-09 13:03:35 -08002845 if (err) {
2846 view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err);
2847 } else {
2848 sc.params = p;
Simon Huntbb282f52014-11-10 11:08:19 -08002849 if (desc) {
2850 intro += '\n\n ' + desc.join('\n ');
2851 }
2852 view.alert(intro);
Simon Hunt56d51852014-11-09 13:03:35 -08002853 }
2854 });
2855
2856 }
2857
Simon Hunt9f1bced2014-12-02 14:36:39 -08002858 function setupDefs(svg) {
Simon Hunt7fa116d2014-11-17 14:16:55 -08002859 var defs = svg.append('defs');
Simon Hunt9f1bced2014-12-02 14:36:39 -08002860 gly.loadDefs(defs);
2861 d3u.loadGlow(defs);
Simon Hunt7fa116d2014-11-17 14:16:55 -08002862 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002863
Simon Hunt395a70c2014-11-22 23:17:40 -08002864 function sendUpdateMeta(d, store) {
2865 var metaUi = {},
2866 ll;
2867
2868 if (store) {
Simon Hunt1b18aa52014-11-29 17:57:55 -08002869 ll = geoMapProj.invert([d.x, d.y]);
Simon Hunt62c47542014-11-22 22:16:32 -08002870 metaUi = {
2871 x: d.x,
2872 y: d.y,
2873 lng: ll[0],
2874 lat: ll[1]
2875 };
Simon Hunt395a70c2014-11-22 23:17:40 -08002876 }
Simon Hunt62c47542014-11-22 22:16:32 -08002877 d.metaUi = metaUi;
2878 sendMessage('updateMeta', {
2879 id: d.id,
2880 'class': d.class,
Simon Huntc1cc81c2014-11-29 14:59:01 -08002881 memento: metaUi
Simon Hunt62c47542014-11-22 22:16:32 -08002882 });
2883 }
2884
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002885 // ==============================
Simon Hunt142d0032014-11-04 20:13:09 -08002886 // View life-cycle callbacks
Simon Hunt195cb382014-11-03 17:50:51 -08002887
Simon Hunta2994cc2014-12-02 14:19:15 -08002888 function init(view, ctx, flags) {
Simon Hunt142d0032014-11-04 20:13:09 -08002889 var w = view.width(),
2890 h = view.height(),
Simon Hunt1b18aa52014-11-29 17:57:55 -08002891 fcfg = config.force;
Simon Hunt195cb382014-11-03 17:50:51 -08002892
Simon Hunt142d0032014-11-04 20:13:09 -08002893 // NOTE: view.$div is a D3 selection of the view's div
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002894 var viewBox = '0 0 ' + config.logicalSize + ' ' + config.logicalSize;
2895 svg = view.$div.append('svg').attr('viewBox', viewBox);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002896 setSize(svg, view);
2897
Simon Hunt9f1bced2014-12-02 14:36:39 -08002898 // load glyphs, filters, and other definitions...
2899 setupDefs(svg);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002900
Simon Hunt3c5ca542014-11-29 14:11:43 -08002901 panZoomContainer = svg.append('g').attr('id', 'panZoomContainer');
2902 setupPanZoom();
Paul Greysonfcba0e82014-11-13 10:21:16 -08002903
Simon Huntc7ee0662014-11-05 16:44:37 -08002904 // group for the topology
Simon Hunt3c5ca542014-11-29 14:11:43 -08002905 topoG = panZoomContainer.append('g')
Simon Hunt1b18aa52014-11-29 17:57:55 -08002906 .attr('id', 'topo-G');
Simon Huntc7ee0662014-11-05 16:44:37 -08002907
Simon Hunte2575b62014-11-18 15:25:53 -08002908 // subgroups for links, link labels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002909 linkG = topoG.append('g').attr('id', 'links');
Simon Hunte2575b62014-11-18 15:25:53 -08002910 linkLabelG = topoG.append('g').attr('id', 'linkLabels');
Simon Huntc7ee0662014-11-05 16:44:37 -08002911 nodeG = topoG.append('g').attr('id', 'nodes');
2912
Simon Hunte2575b62014-11-18 15:25:53 -08002913 // selection of links, linkLabels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002914 link = linkG.selectAll('.link');
Simon Hunte2575b62014-11-18 15:25:53 -08002915 linkLabel = linkLabelG.selectAll('.linkLabel');
Simon Huntc7ee0662014-11-05 16:44:37 -08002916 node = nodeG.selectAll('.node');
2917
Simon Hunt7cd48f32014-11-09 23:42:50 -08002918 function chrg(d) {
2919 return fcfg.charge[d.class] || -12000;
2920 }
Simon Hunt99c13842014-11-06 18:23:12 -08002921 function ldist(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002922 return fcfg.linkDistance[d.type] || 50;
Simon Hunt99c13842014-11-06 18:23:12 -08002923 }
2924 function lstrg(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002925 // 0.0 - 1.0
2926 return fcfg.linkStrength[d.type] || 1.0;
Simon Hunt99c13842014-11-06 18:23:12 -08002927 }
2928
Simon Hunt1a9eff92014-11-07 11:06:34 -08002929 function selectCb(d, self) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002930 selectObject(d, self);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002931 }
2932
2933 function atDragEnd(d, self) {
Simon Hunt56d51852014-11-09 13:03:35 -08002934 // once we've finished moving, pin the node in position
2935 d.fixed = true;
2936 d3.select(self).classed('fixed', true);
2937 if (config.useLiveData) {
Simon Hunt395a70c2014-11-22 23:17:40 -08002938 sendUpdateMeta(d, true);
Simon Hunta255a2c2014-11-13 22:29:35 -08002939 } else {
2940 console.log('Moving node ' + d.id + ' to [' + d.x + ',' + d.y + ']');
Simon Hunt1a9eff92014-11-07 11:06:34 -08002941 }
2942 }
2943
Simon Hunt6e18fe32014-11-29 13:35:41 -08002944 // predicate that indicates when dragging is active
2945 function dragEnabled() {
2946 // meta key pressed means we are zooming/panning (so disable drag)
Simon Huntc2367d52014-11-29 19:30:23 -08002947 return !nodeLock && !d3.event.sourceEvent.metaKey;
2948 }
2949
2950 // predicate that indicates when clicking is active
2951 function clickEnabled() {
2952 return true;
Simon Hunt6e18fe32014-11-29 13:35:41 -08002953 }
2954
Simon Huntc7ee0662014-11-05 16:44:37 -08002955 // set up the force layout
2956 network.force = d3.layout.force()
Simon Hunt1b18aa52014-11-29 17:57:55 -08002957 .size([w, h])
Simon Huntc7ee0662014-11-05 16:44:37 -08002958 .nodes(network.nodes)
2959 .links(network.links)
Simon Hunt7cd48f32014-11-09 23:42:50 -08002960 .gravity(0.4)
2961 .friction(0.7)
2962 .charge(chrg)
Simon Hunt99c13842014-11-06 18:23:12 -08002963 .linkDistance(ldist)
2964 .linkStrength(lstrg)
Simon Huntc7ee0662014-11-05 16:44:37 -08002965 .on('tick', tick);
Simon Hunt195cb382014-11-03 17:50:51 -08002966
Simon Hunt01095ff2014-11-13 16:37:29 -08002967 network.drag = d3u.createDragBehavior(network.force,
Simon Huntc2367d52014-11-29 19:30:23 -08002968 selectCb, atDragEnd, dragEnabled, clickEnabled);
Simon Hunt6e18fe32014-11-29 13:35:41 -08002969
Simon Hunt0c6d4192014-11-12 12:07:10 -08002970
2971 // create mask layer for when we lose connection to server.
Simon Hunta5e89142014-11-14 07:00:33 -08002972 // TODO: this should be part of the framework
Simon Hunt6e18fe32014-11-29 13:35:41 -08002973
2974 function para(sel, text) {
2975 sel.append('p').text(text);
2976 }
2977
Simon Hunt0c6d4192014-11-12 12:07:10 -08002978 mask = view.$div.append('div').attr('id','topo-mask');
2979 para(mask, 'Oops!');
2980 para(mask, 'Web-socket connection to server closed...');
2981 para(mask, 'Try refreshing the page.');
Simon Hunt12ce12e2014-11-15 21:13:19 -08002982
2983 mask.append('svg')
2984 .attr({
2985 id: 'mask-bird',
2986 width: w,
2987 height: h
2988 })
2989 .append('g')
2990 .attr('transform', birdTranslate(w, h))
2991 .style('opacity', 0.3)
2992 .append('use')
2993 .attr({
2994 'xlink:href': '#bird',
2995 width: config.birdDim,
2996 height: config.birdDim,
2997 fill: '#111'
Thomas Vachuska89543292014-11-19 11:28:33 -08002998 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08002999 }
Simon Hunt195cb382014-11-03 17:50:51 -08003000
Simon Hunt01095ff2014-11-13 16:37:29 -08003001
Simon Hunt56d51852014-11-09 13:03:35 -08003002 function load(view, ctx, flags) {
Simon Huntf67722a2014-11-10 09:32:06 -08003003 // resize, in case the window was resized while we were not loaded
3004 resize(view, ctx, flags);
3005
Simon Hunt99c13842014-11-06 18:23:12 -08003006 // cache the view token, so network topo functions can access it
3007 network.view = view;
Simon Hunt56d51852014-11-09 13:03:35 -08003008 config.useLiveData = !flags.local;
3009
3010 if (!config.useLiveData) {
3011 prepareScenario(view, ctx, flags.debug);
3012 }
Simon Hunt99c13842014-11-06 18:23:12 -08003013
3014 // set our radio buttons and key bindings
Simon Hunt9462e8c2014-11-14 17:28:09 -08003015 layerBtnSet = view.setRadio(layerButtons);
Simon Hunt934c3ce2014-11-05 11:45:07 -08003016 view.setKeys(keyDispatch);
Simon Hunt87514342014-11-24 16:41:27 -08003017 view.setGestures(gestures);
Simon Hunt195cb382014-11-03 17:50:51 -08003018
Simon Huntf8e5b4e02014-11-13 11:17:57 -08003019 // patch in our "button bar" for now
3020 // TODO: implement a more official frameworky way of doing this..
Simon Hunt87514342014-11-24 16:41:27 -08003021 //addButtonBar(view);
Simon Huntf8e5b4e02014-11-13 11:17:57 -08003022
Simon Huntd3b7d512014-11-12 15:48:41 -08003023 // Load map data asynchronously; complete startup after that..
3024 loadGeoJsonData();
Simon Hunta255a2c2014-11-13 22:29:35 -08003025 }
3026
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08003027 function startAntTimer() {
Thomas Vachuskab7e40642014-12-03 11:16:06 -08003028 // Note: disabled until traffic can be allotted to intents properly
3029 if (false && !antTimer) {
Thomas Vachuska4731f122014-11-20 04:56:19 -08003030 var pulses = [5, 3, 1.2, 3],
3031 pulse = 0;
3032 antTimer = setInterval(function () {
3033 pulse = pulse + 1;
3034 pulse = pulse === pulses.length ? 0 : pulse;
3035 d3.selectAll('.animated').style('stroke-width', pulses[pulse]);
3036 }, 200);
3037 }
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08003038 }
3039
3040 function stopAntTimer() {
Simon Hunta255a2c2014-11-13 22:29:35 -08003041 if (antTimer) {
3042 clearInterval(antTimer);
3043 antTimer = null;
3044 }
Simon Huntd3b7d512014-11-12 15:48:41 -08003045 }
3046
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08003047 function unload(view, ctx, flags) {
3048 stopAntTimer();
3049 }
3050
Simon Huntd3b7d512014-11-12 15:48:41 -08003051 // TODO: move these to config/state portion of script
Simon Hunta6a9fe72014-11-20 11:17:12 -08003052 var geoJsonUrl = 'json/map/continental_us.json',
Simon Huntd3b7d512014-11-12 15:48:41 -08003053 geoJson;
3054
3055 function loadGeoJsonData() {
3056 d3.json(geoJsonUrl, function (err, data) {
3057 if (err) {
3058 // fall back to USA map background
3059 loadStaticMap();
3060 } else {
3061 geoJson = data;
3062 loadGeoMap();
3063 }
3064
3065 // finally, connect to the server...
3066 if (config.useLiveData) {
3067 webSock.connect();
3068 }
3069 });
3070 }
3071
3072 function showBg() {
Simon Hunt434cf142014-11-24 11:10:28 -08003073 return visVal(config.options.showBackground);
Simon Huntd3b7d512014-11-12 15:48:41 -08003074 }
3075
3076 function loadStaticMap() {
3077 fnTrace('loadStaticMap', config.backgroundUrl);
3078 var w = network.view.width(),
3079 h = network.view.height();
3080
3081 // load the background image
3082 bgImg = svg.insert('svg:image', '#topo-G')
3083 .attr({
3084 id: 'topo-bg',
3085 width: w,
3086 height: h,
3087 'xlink:href': config.backgroundUrl
3088 })
3089 .style({
3090 visibility: showBg()
3091 });
3092 }
3093
Simon Hunt1b18aa52014-11-29 17:57:55 -08003094 function setProjForView(path, topoData) {
3095 var dim = config.logicalSize;
3096
3097 // start with unit scale, no translation..
3098 geoMapProj.scale(1).translate([0, 0]);
3099
3100 // figure out dimensions of map data..
3101 var b = path.bounds(topoData),
3102 x1 = b[0][0],
3103 y1 = b[0][1],
3104 x2 = b[1][0],
3105 y2 = b[1][1],
3106 dx = x2 - x1,
3107 dy = y2 - y1,
3108 x = (x1 + x2) / 2,
3109 y = (y1 + y2) / 2;
3110
3111 // size map to 95% of minimum dimension to fill space..
3112 var s = .95 / Math.min(dx / dim, dy / dim);
3113 var t = [dim / 2 - s * x, dim / 2 - s * y];
3114
3115 // set new scale, translation on the projection..
3116 geoMapProj.scale(s).translate(t);
3117 }
3118
Simon Huntd3b7d512014-11-12 15:48:41 -08003119 function loadGeoMap() {
3120 fnTrace('loadGeoMap', geoJsonUrl);
Simon Huntd3b7d512014-11-12 15:48:41 -08003121
Paul Greyson6cb8ca02014-11-12 18:09:02 -08003122 // extracts the topojson data into geocoordinate-based geometry
3123 var topoData = topojson.feature(geoJson, geoJson.objects.states);
Simon Huntd3b7d512014-11-12 15:48:41 -08003124
Paul Greyson6cb8ca02014-11-12 18:09:02 -08003125 // see: http://bl.ocks.org/mbostock/4707858
Simon Hunt1b18aa52014-11-29 17:57:55 -08003126 geoMapProj = d3.geo.mercator();
3127 var path = d3.geo.path().projection(geoMapProj);
Simon Huntd3b7d512014-11-12 15:48:41 -08003128
Simon Hunt1b18aa52014-11-29 17:57:55 -08003129 setProjForView(path, topoData);
Paul Greyson6cb8ca02014-11-12 18:09:02 -08003130
Simon Hunt3c5ca542014-11-29 14:11:43 -08003131 bgImg = panZoomContainer.insert("g", '#topo-G');
Thomas Vachuska89543292014-11-19 11:28:33 -08003132 bgImg.attr('id', 'map').selectAll('path')
3133 .data(topoData.features)
3134 .enter()
3135 .append('path')
3136 .attr('d', path);
Simon Hunt195cb382014-11-03 17:50:51 -08003137 }
3138
Simon Huntf67722a2014-11-10 09:32:06 -08003139 function resize(view, ctx, flags) {
Simon Hunt12ce12e2014-11-15 21:13:19 -08003140 var w = view.width(),
3141 h = view.height();
3142
Simon Hunt934c3ce2014-11-05 11:45:07 -08003143 setSize(svg, view);
Simon Hunt12ce12e2014-11-15 21:13:19 -08003144
3145 d3.select('#mask-bird').attr({ width: w, height: h})
3146 .select('g').attr('transform', birdTranslate(w, h));
Simon Hunt142d0032014-11-04 20:13:09 -08003147 }
3148
Simon Hunt8f40cce2014-11-23 15:57:30 -08003149 function theme(view, ctx, flags) {
3150 updateInstances();
Simon Huntb0ecfa52014-11-23 21:05:12 -08003151 updateDeviceColors();
Simon Hunt8f40cce2014-11-23 15:57:30 -08003152 }
3153
Simon Hunt12ce12e2014-11-15 21:13:19 -08003154 function birdTranslate(w, h) {
3155 var bdim = config.birdDim;
3156 return 'translate('+((w-bdim)*.4)+','+((h-bdim)*.1)+')';
3157 }
Simon Hunt142d0032014-11-04 20:13:09 -08003158
Simon Hunt06811b72014-11-25 18:54:48 -08003159 function isF(f) { return $.isFunction(f) ? f : null; }
3160 function noop() {}
3161
3162 function augmentDetailPane() {
3163 var dp = detailPane;
3164 dp.ypos = { up: 64, down: 320, current: 320};
3165
3166 dp._move = function (y, cb) {
3167 var endCb = isF(cb) || noop,
3168 yp = dp.ypos;
3169 if (yp.current !== y) {
3170 yp.current = y;
3171 dp.el.transition().duration(300)
3172 .each('end', endCb)
3173 .style('top', yp.current + 'px');
3174 } else {
3175 endCb();
3176 }
3177 };
3178
3179 dp.down = function (cb) { dp._move(dp.ypos.down, cb); };
3180 dp.up = function (cb) { dp._move(dp.ypos.up, cb); };
3181 }
3182
Simon Hunt142d0032014-11-04 20:13:09 -08003183 // ==============================
3184 // View registration
Simon Hunt195cb382014-11-03 17:50:51 -08003185
Simon Hunt25248912014-11-04 11:25:48 -08003186 onos.ui.addView('topo', {
Simon Hunta2994cc2014-12-02 14:19:15 -08003187 init: init,
Simon Hunt142d0032014-11-04 20:13:09 -08003188 load: load,
Simon Hunta255a2c2014-11-13 22:29:35 -08003189 unload: unload,
Simon Hunt8f40cce2014-11-23 15:57:30 -08003190 resize: resize,
3191 theme: theme
Simon Hunt195cb382014-11-03 17:50:51 -08003192 });
3193
Thomas Vachuska47635c62014-11-22 01:21:36 -08003194 summaryPane = onos.ui.addFloatingPanel('topo-summary');
Simon Hunt61d04042014-11-11 17:27:16 -08003195 detailPane = onos.ui.addFloatingPanel('topo-detail');
Simon Hunt06811b72014-11-25 18:54:48 -08003196 augmentDetailPane();
Simon Hunta5e89142014-11-14 07:00:33 -08003197 oiBox = onos.ui.addFloatingPanel('topo-oibox', 'TL');
Simon Huntb82f6902014-11-22 11:53:15 -08003198 oiBox.width(20);
Simon Hunt61d04042014-11-11 17:27:16 -08003199
Simon Hunt195cb382014-11-03 17:50:51 -08003200}(ONOS));