blob: a8f0570017dfeb5e43453bc9023b75521cc88489 [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
21(function (onos) {
22 'use strict';
23
Simon Hunt1a9eff92014-11-07 11:06:34 -080024 // shorter names for library APIs
Simon Huntbb282f52014-11-10 11:08:19 -080025 var d3u = onos.lib.d3util,
Simon Hunta6a9fe72014-11-20 11:17:12 -080026 gly = onos.lib.glyphs;
Simon Hunt1a9eff92014-11-07 11:06:34 -080027
Simon Hunt195cb382014-11-03 17:50:51 -080028 // configuration data
29 var config = {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -080030 useLiveData: true,
Simon Huntfc274c92014-11-11 11:05:46 -080031 fnTrace: true,
Simon Hunt195cb382014-11-03 17:50:51 -080032 debugOn: false,
Simon Hunt12ce12e2014-11-15 21:13:19 -080033 birdDim: 400,
Simon Hunt195cb382014-11-03 17:50:51 -080034 options: {
Simon Hunt142d0032014-11-04 20:13:09 -080035 showBackground: true
Simon Hunt195cb382014-11-03 17:50:51 -080036 },
Thomas Vachuska5f399652015-02-23 15:17:36 -080037 webSockUrl: '../ws/topology',
Simon Hunt195cb382014-11-03 17:50:51 -080038 data: {
39 live: {
Thomas Vachuska5f399652015-02-23 15:17:36 -080040 jsonUrl: '../rs/topology/graph',
41 detailPrefix: '../rs/topology/graph/',
Simon Hunt195cb382014-11-03 17:50:51 -080042 detailSuffix: ''
43 },
44 fake: {
45 jsonUrl: 'json/network2.json',
46 detailPrefix: 'json/',
47 detailSuffix: '.json'
48 }
49 },
Simon Hunt99c13842014-11-06 18:23:12 -080050 labels: {
51 imgPad: 16,
52 padLR: 4,
53 padTB: 3,
54 marginLR: 3,
55 marginTB: 2,
56 port: {
57 gap: 3,
58 width: 18,
59 height: 14
60 }
61 },
Simon Hunt1a9eff92014-11-07 11:06:34 -080062 topo: {
Thomas Vachuska89543292014-11-19 11:28:33 -080063 linkBaseColor: '#666',
Simon Hunt1a9eff92014-11-07 11:06:34 -080064 linkInColor: '#66f',
Thomas Vachuska9edca302014-11-22 17:06:42 -080065 linkInWidth: 12,
Thomas Vachuska89543292014-11-19 11:28:33 -080066 linkOutColor: '#f00',
Thomas Vachuska9edca302014-11-22 17:06:42 -080067 linkOutWidth: 10
Simon Hunt1a9eff92014-11-07 11:06:34 -080068 },
Paul Greyson29cd58f2014-11-18 13:14:57 -080069 icons: {
Simon Huntc72967b2014-11-20 09:21:42 -080070 device: {
Simon Hunt395a70c2014-11-22 23:17:40 -080071 dim: 36,
72 rx: 4,
73 xoff: -20,
74 yoff: -18
75 },
76 host: {
77 defaultRadius: 9,
78 radius: {
79 endstation: 14,
80 bgpSpeaker: 14,
81 router: 14
82 }
Simon Huntc72967b2014-11-20 09:21:42 -080083 }
Thomas Vachuska89543292014-11-19 11:28:33 -080084 },
Simon Hunt195cb382014-11-03 17:50:51 -080085 force: {
Simon Hunt7cd48f32014-11-09 23:42:50 -080086 note_for_links: 'link.type is used to differentiate',
Simon Huntc7ee0662014-11-05 16:44:37 -080087 linkDistance: {
Simon Hunt7cd48f32014-11-09 23:42:50 -080088 direct: 100,
89 optical: 120,
Thomas Vachuska3266abf2014-11-13 09:28:46 -080090 hostLink: 3
Simon Huntc7ee0662014-11-05 16:44:37 -080091 },
92 linkStrength: {
Simon Hunt7cd48f32014-11-09 23:42:50 -080093 direct: 1.0,
94 optical: 1.0,
95 hostLink: 1.0
Simon Huntc7ee0662014-11-05 16:44:37 -080096 },
Simon Hunt7cd48f32014-11-09 23:42:50 -080097 note_for_nodes: 'node.class is used to differentiate',
Simon Huntc7ee0662014-11-05 16:44:37 -080098 charge: {
Simon Hunt7cd48f32014-11-09 23:42:50 -080099 device: -8000,
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800100 host: -5000
Simon Hunt195cb382014-11-03 17:50:51 -0800101 }
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800102 },
103 // see below in creation of viewBox on main svg
104 logicalSize: 1000
Simon Hunt195cb382014-11-03 17:50:51 -0800105 };
106
Simon Hunt142d0032014-11-04 20:13:09 -0800107 // radio buttons
Simon Hunt9462e8c2014-11-14 17:28:09 -0800108 var layerButtons = [
109 { text: 'All Layers', id: 'all', cb: showAllLayers },
110 { text: 'Packet Only', id: 'pkt', cb: showPacketLayer },
111 { text: 'Optical Only', id: 'opt', cb: showOpticalLayer }
112 ],
113 layerBtnSet,
114 layerBtnDispatch = {
115 all: showAllLayers,
116 pkt: showPacketLayer,
117 opt: showOpticalLayer
118 };
Simon Hunt934c3ce2014-11-05 11:45:07 -0800119
120 // key bindings
121 var keyDispatch = {
Simon Hunt233747b2014-12-04 12:01:58 -0800122 // ==== "development mode" ====
123 //0: testMe,
124 //equals: injectStartupEvents,
125 //dash: injectTestEvent,
Simon Hunt99c13842014-11-06 18:23:12 -0800126
Thomas Vachuska47635c62014-11-22 01:21:36 -0800127 O: [toggleSummary, 'Toggle ONOS summary pane'],
128 I: [toggleInstances, 'Toggle ONOS instances pane'],
Simon Hunt27d322d2014-11-28 10:45:43 -0800129 D: [toggleDetails, 'Disable / enable details pane'],
Simon Hunta1162d82014-12-03 14:34:43 -0800130
Simon Hunt434cf142014-11-24 11:10:28 -0800131 H: [toggleHosts, 'Toggle host visibility'],
Simon Hunt6d9bd032014-11-28 22:16:40 -0800132 M: [toggleOffline, 'Toggle offline visibility'],
Simon Hunta1162d82014-12-03 14:34:43 -0800133 B: [toggleBg, 'Toggle background image'],
Simon Hunt934c3ce2014-11-05 11:45:07 -0800134 P: togglePorts,
Simon Hunta1162d82014-12-03 14:34:43 -0800135
136 X: [toggleNodeLock, 'Lock / unlock node positions'],
137 Z: [toggleOblique, 'Toggle oblique view (Experimental)'],
138 L: [cycleLabels, 'Cycle device labels'],
Simon Hunt87514342014-11-24 16:41:27 -0800139 U: [unpin, 'Unpin node (hover mouse over)'],
Simon Hunt3c5ca542014-11-29 14:11:43 -0800140 R: [resetPanZoom, 'Reset pan / zoom'],
Simon Hunta1162d82014-12-03 14:34:43 -0800141
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800142 V: [showRelatedIntentsAction, 'Show all related intents'],
Thomas Vachuskab7e40642014-12-03 11:16:06 -0800143 rightArrow: [showNextIntentAction, 'Show next related intent'],
144 leftArrow: [showPrevIntentAction, 'Show previous related intent'],
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800145 W: [showSelectedIntentTrafficAction, 'Monitor traffic of selected intent'],
146 A: [showAllTrafficAction, 'Monitor all traffic'],
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800147 F: [showDeviceLinkFlowsAction, 'Show device link flows'],
Simon Hunta1162d82014-12-03 14:34:43 -0800148
149 E: [equalizeMasters, 'Equalize mastership roles'],
150
151 esc: handleEscape,
152
153 _helpFormat: [
154 ['O', 'I', 'D', '-', 'H', 'M', 'B', 'P' ],
155 ['X', 'Z', 'L', 'U', 'R' ],
156 ['V', 'rightArrow', 'leftArrow', 'W', 'A', 'F', '-', 'E' ]
157 ]
Simon Hunt934c3ce2014-11-05 11:45:07 -0800158 };
Simon Hunt142d0032014-11-04 20:13:09 -0800159
Simon Hunt87514342014-11-24 16:41:27 -0800160 // mouse gestures
161 var gestures = [
162 ['click', 'Select the item and show details'],
163 ['shift-click', 'Toggle selection state'],
164 ['drag', 'Reposition (and pin) device / host'],
165 ['cmd-scroll', 'Zoom in / out'],
166 ['cmd-drag', 'Pan']
167 ];
168
Simon Hunt195cb382014-11-03 17:50:51 -0800169 // state variables
Simon Hunt99c13842014-11-06 18:23:12 -0800170 var network = {
Simon Hunt50128c02014-11-08 13:36:15 -0800171 view: null, // view token reference
Simon Hunt99c13842014-11-06 18:23:12 -0800172 nodes: [],
173 links: [],
Simon Hunt269670f2014-11-17 16:17:43 -0800174 lookup: {},
175 revLinkToKey: {}
Simon Hunt99c13842014-11-06 18:23:12 -0800176 },
Simon Hunt56d51852014-11-09 13:03:35 -0800177 scenario = {
178 evDir: 'json/ev/',
179 evScenario: '/scenario.json',
180 evPrefix: '/ev_',
181 evOnos: '_onos.json',
182 evUi: '_ui.json',
183 ctx: null,
184 params: {},
185 evNumber: 0,
Simon Hunt434cf142014-11-24 11:10:28 -0800186 view: null
Simon Hunt56d51852014-11-09 13:03:35 -0800187 },
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800188 webSock,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800189 sid = 0,
Thomas Vachuska60d72bf2014-11-21 13:02:00 -0800190 deviceLabelCount = 3,
Simon Hunt209155e2014-11-21 12:16:09 -0800191 hostLabelCount = 2,
Simon Hunt56d51852014-11-09 13:03:35 -0800192 deviceLabelIndex = 0,
193 hostLabelIndex = 0,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800194 selections = {},
Simon Hunta5e89142014-11-14 07:00:33 -0800195 selectOrder = [],
Simon Hunt6ac93f32014-11-13 12:17:27 -0800196 hovered = null,
Thomas Vachuska47635c62014-11-22 01:21:36 -0800197 summaryPane,
Simon Hunta5e89142014-11-14 07:00:33 -0800198 detailPane,
Simon Hunta255a2c2014-11-13 22:29:35 -0800199 antTimer = null,
Thomas Vachuska12dfdc32014-11-29 16:03:12 -0800200 guiSuccessor = null,
Simon Hunta5e89142014-11-14 07:00:33 -0800201 onosInstances = {},
202 onosOrder = [],
203 oiBox,
Simon Hunt9462e8c2014-11-14 17:28:09 -0800204 oiShowMaster = false,
Simon Hunt8f40cce2014-11-23 15:57:30 -0800205 portLabelsOn = false,
Simon Huntb0ecfa52014-11-23 21:05:12 -0800206 cat7 = d3u.cat7(),
Simon Hunt434cf142014-11-24 11:10:28 -0800207 colorAffinity = false,
Simon Hunt27d322d2014-11-28 10:45:43 -0800208 showHosts = false,
Simon Hunt6d9bd032014-11-28 22:16:40 -0800209 showOffline = true,
Simon Hunt27d322d2014-11-28 10:45:43 -0800210 useDetails = true,
Simon Hunt6e18fe32014-11-29 13:35:41 -0800211 haveDetails = false,
Simon Huntc2367d52014-11-29 19:30:23 -0800212 nodeLock = false,
213 oblique = false;
Simon Hunt195cb382014-11-03 17:50:51 -0800214
Simon Hunt434cf142014-11-24 11:10:28 -0800215 // constants
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800216 var hoverModeNone = 0,
217 hoverModeAll = 1,
Thomas Vachuska9edca302014-11-22 17:06:42 -0800218 hoverModeFlows = 2,
219 hoverModeIntents = 3,
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800220 hoverMode = hoverModeNone;
Thomas Vachuska9edca302014-11-22 17:06:42 -0800221
Simon Hunt934c3ce2014-11-05 11:45:07 -0800222 // D3 selections
223 var svg,
Simon Hunt3c5ca542014-11-29 14:11:43 -0800224 panZoomContainer,
Simon Hunt233747b2014-12-04 12:01:58 -0800225 noDevices,
Simon Hunt934c3ce2014-11-05 11:45:07 -0800226 bgImg,
Simon Huntc7ee0662014-11-05 16:44:37 -0800227 topoG,
228 nodeG,
229 linkG,
Simon Hunte2575b62014-11-18 15:25:53 -0800230 linkLabelG,
Simon Huntc7ee0662014-11-05 16:44:37 -0800231 node,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800232 link,
Simon Hunte2575b62014-11-18 15:25:53 -0800233 linkLabel,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800234 mask;
Simon Hunt195cb382014-11-03 17:50:51 -0800235
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800236 // the projection for the map background
Simon Hunt1b18aa52014-11-29 17:57:55 -0800237 var geoMapProj;
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800238
Paul Greysonfcba0e82014-11-13 10:21:16 -0800239 // the zoom function
240 var zoom;
241
Simon Hunt142d0032014-11-04 20:13:09 -0800242 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800243 // For Debugging / Development
Simon Hunt195cb382014-11-03 17:50:51 -0800244
Simon Huntfc274c92014-11-11 11:05:46 -0800245 function fnTrace(msg, id) {
246 if (config.fnTrace) {
247 console.log('FN: ' + msg + ' [' + id + ']');
248 }
249 }
Simon Hunt99c13842014-11-06 18:23:12 -0800250
Simon Hunta5e89142014-11-14 07:00:33 -0800251 function evTrace(data) {
252 fnTrace(data.event, data.payload.id);
253 }
254
Simon Hunt934c3ce2014-11-05 11:45:07 -0800255 // ==============================
256 // Key Callbacks
257
Simon Hunt27d322d2014-11-28 10:45:43 -0800258 function flash(txt) {
259 network.view.flash(txt);
260 }
261
Simon Hunt99c13842014-11-06 18:23:12 -0800262 function testMe(view) {
Simon Hunt8f40cce2014-11-23 15:57:30 -0800263 //view.alert('Theme is ' + view.getTheme());
Simon Hunta3dd9572014-11-20 15:22:41 -0800264 //view.flash('This is some text');
Simon Hunt8f40cce2014-11-23 15:57:30 -0800265 cat7.testCard(svg);
Simon Hunt99c13842014-11-06 18:23:12 -0800266 }
267
Simon Hunt56d51852014-11-09 13:03:35 -0800268 function injectTestEvent(view) {
Simon Hunt434cf142014-11-24 11:10:28 -0800269 if (config.useLiveData) { return; }
270
Simon Hunt56d51852014-11-09 13:03:35 -0800271 var sc = scenario,
272 evn = ++sc.evNumber,
273 pfx = sc.evDir + sc.ctx + sc.evPrefix + evn,
274 onosUrl = pfx + sc.evOnos,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800275 uiUrl = pfx + sc.evUi,
276 stack = [
277 { url: onosUrl, cb: handleServerEvent },
278 { url: uiUrl, cb: handleUiEvent }
279 ];
280 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800281 }
282
Simon Hunt7cd48f32014-11-09 23:42:50 -0800283 function recurseFetchEvent(stack, evn) {
284 var v = scenario.view,
285 frame;
286 if (stack.length === 0) {
Simon Huntfc274c92014-11-11 11:05:46 -0800287 v.alert('Oops!\n\nNo event #' + evn + ' found.');
Simon Hunt7cd48f32014-11-09 23:42:50 -0800288 return;
289 }
290 frame = stack.shift();
291
292 d3.json(frame.url, function (err, data) {
Simon Hunt99c13842014-11-06 18:23:12 -0800293 if (err) {
Simon Hunt56d51852014-11-09 13:03:35 -0800294 if (err.status === 404) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800295 // if we didn't find the data, try the next stack frame
296 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800297 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800298 v.alert('non-404 error:\n\n' + frame.url + '\n\n' + err);
Simon Hunt56d51852014-11-09 13:03:35 -0800299 }
Simon Hunt99c13842014-11-06 18:23:12 -0800300 } else {
Simon Hunt1712ed82014-11-17 12:56:00 -0800301 wsTrace('test', JSON.stringify(data));
Simon Hunt7cd48f32014-11-09 23:42:50 -0800302 frame.cb(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800303 }
304 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800305
Simon Hunt56d51852014-11-09 13:03:35 -0800306 }
Simon Hunt50128c02014-11-08 13:36:15 -0800307
Simon Hunt56d51852014-11-09 13:03:35 -0800308 function handleUiEvent(data) {
Simon Huntbb282f52014-11-10 11:08:19 -0800309 scenario.view.alert('UI Tx: ' + data.event + '\n\n' +
310 JSON.stringify(data));
Simon Hunt56d51852014-11-09 13:03:35 -0800311 }
312
313 function injectStartupEvents(view) {
314 var last = scenario.params.lastAuto || 0;
Simon Hunt434cf142014-11-24 11:10:28 -0800315 if (config.useLiveData) { return; }
Simon Hunt56d51852014-11-09 13:03:35 -0800316
317 while (scenario.evNumber < last) {
Simon Hunt1a9eff92014-11-07 11:06:34 -0800318 injectTestEvent(view);
319 }
320 }
321
Simon Hunt934c3ce2014-11-05 11:45:07 -0800322 function toggleBg() {
323 var vis = bgImg.style('visibility');
Simon Hunt434cf142014-11-24 11:10:28 -0800324 bgImg.style('visibility', visVal(vis === 'hidden'));
325 }
326
Simon Huntd6f5a272014-11-29 23:45:50 -0800327 function opacifyBg(b) {
328 bgImg.transition()
329 .duration(1000)
330 .attr('opacity', b ? 1 : 0);
331 }
332
Simon Huntc2367d52014-11-29 19:30:23 -0800333 function toggleNodeLock() {
334 nodeLock = !nodeLock;
335 flash('Node positions ' + (nodeLock ? 'locked' : 'unlocked'))
336 }
337
338 function toggleOblique() {
339 oblique = !oblique;
Simon Huntd6f5a272014-11-29 23:45:50 -0800340 if (oblique) {
341 network.force.stop();
342 toObliqueView();
343 } else {
344 toNormalView();
345 }
Simon Huntc2367d52014-11-29 19:30:23 -0800346 }
347
Simon Hunt434cf142014-11-24 11:10:28 -0800348 function toggleHosts() {
349 showHosts = !showHosts;
350 updateHostVisibility();
Simon Hunt27d322d2014-11-28 10:45:43 -0800351 flash('Hosts ' + visVal(showHosts));
Simon Hunt934c3ce2014-11-05 11:45:07 -0800352 }
353
Simon Hunt6d9bd032014-11-28 22:16:40 -0800354 function toggleOffline() {
355 showOffline = !showOffline;
356 updateOfflineVisibility();
357 flash('Offline devices ' + visVal(showOffline));
358 }
359
Simon Hunt99c13842014-11-06 18:23:12 -0800360 function cycleLabels() {
Thomas Vachuska60d72bf2014-11-21 13:02:00 -0800361 deviceLabelIndex = (deviceLabelIndex === 2)
Simon Huntbb282f52014-11-10 11:08:19 -0800362 ? 0 : deviceLabelIndex + 1;
Simon Hunt5f36d342014-11-08 21:33:14 -0800363
Simon Hunt99c13842014-11-06 18:23:12 -0800364 network.nodes.forEach(function (d) {
Simon Huntbb282f52014-11-10 11:08:19 -0800365 if (d.class === 'device') {
366 updateDeviceLabel(d);
367 }
Simon Hunt99c13842014-11-06 18:23:12 -0800368 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800369 }
370
371 function togglePorts(view) {
Simon Hunt434cf142014-11-24 11:10:28 -0800372 //view.alert('togglePorts() callback')
Simon Hunt934c3ce2014-11-05 11:45:07 -0800373 }
374
Simon Hunt6ac93f32014-11-13 12:17:27 -0800375 function unpin() {
376 if (hovered) {
Simon Hunt395a70c2014-11-22 23:17:40 -0800377 sendUpdateMeta(hovered);
Simon Hunt6ac93f32014-11-13 12:17:27 -0800378 hovered.fixed = false;
379 hovered.el.classed('fixed', false);
Simon Huntd6f5a272014-11-29 23:45:50 -0800380 fResume();
Simon Hunt6ac93f32014-11-13 12:17:27 -0800381 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800382 }
383
Simon Hunt9462e8c2014-11-14 17:28:09 -0800384 function handleEscape(view) {
385 if (oiShowMaster) {
386 cancelAffinity();
Simon Hunt27d322d2014-11-28 10:45:43 -0800387 } else if (haveDetails) {
Simon Hunt9462e8c2014-11-14 17:28:09 -0800388 deselectAll();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800389 } else if (oiBox.isVisible()) {
Simon Huntb0ecfa52014-11-23 21:05:12 -0800390 hideInstances();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800391 } else if (summaryPane.isVisible()) {
392 cancelSummary();
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800393 stopAntTimer();
394 } else {
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800395 hoverMode = hoverModeNone;
Simon Hunt9462e8c2014-11-14 17:28:09 -0800396 }
397 }
398
Simon Hunt233747b2014-12-04 12:01:58 -0800399 function showNoDevs(b) {
400 noDevices.style('visibility', visVal(b));
401 }
402
Simon Hunt934c3ce2014-11-05 11:45:07 -0800403 // ==============================
Simon Huntd6f5a272014-11-29 23:45:50 -0800404 // Oblique view ...
405
406 var obview = {
407 tt: -.7, // x skew y factor
408 xsk: -35, // x skew angle
409 ysc: 0.5, // y scale
410 pad: 50,
411 time: 1500,
412 fill: {
413 pkt: 'rgba(130,130,170,0.3)',
414 opt: 'rgba(170,130,170,0.3)'
415 },
416 id: function (tag) {
417 return 'obview-' + tag + 'Plane';
418 },
419 yt: function (h, dir) {
420 return h * obview.ysc * dir * 1.1;
421 },
422 obXform: function (h, dir) {
423 var yt = obview.yt(h, dir);
424 return scale(1, obview.ysc) + translate(0, yt) + skewX(obview.xsk);
425 },
426 noXform: function () {
427 return skewX(0) + translate(0,0) + scale(1,1);
428 },
429 xffn: null,
430 plane: {}
431 };
432
433
434 function toObliqueView() {
435 var box = nodeG.node().getBBox(),
436 ox, oy;
437
438 padBox(box, obview.pad);
439
440 ox = box.x + box.width / 2;
441 oy = box.y + box.height / 2;
442
443 // remember node lock state, then lock the nodes down
444 obview.nodeLock = nodeLock;
445 nodeLock = true;
446 opacifyBg(false);
447
448 insertPlanes(ox, oy);
449
450 obview.xffn = function (xy, dir) {
451 var yt = obview.yt(box.height, dir),
452 ax = xy.x - ox,
453 ay = xy.y - oy,
454 x = ax + ay * obview.tt,
455 y = ay * obview.ysc + obview.ysc * yt;
456 return {x: ox + x, y: oy + y};
457 };
458
459 showPlane('pkt', box, -1);
460 showPlane('opt', box, 1);
461 obTransitionNodes();
462 }
463
464 function toNormalView() {
465 obview.xffn = null;
466
467 hidePlane('pkt');
468 hidePlane('opt');
469 obTransitionNodes();
470
471 removePlanes();
472
473 // restore node lock state
474 nodeLock = obview.nodeLock;
475 opacifyBg(true);
476 }
477
478 function obTransitionNodes() {
479 var xffn = obview.xffn;
480
481 // return the direction for the node
482 // -1 for pkt layer, 1 for optical layer
483 function dir(d) {
484 return inLayer(d, 'pkt') ? -1 : 1;
485 }
486
487 if (xffn) {
488 network.nodes.forEach(function (d) {
489 var oldxy = {x: d.x, y: d.y},
490 coords = xffn(oldxy, dir(d));
491 d.oldxy = oldxy;
492 d.px = d.x = coords.x;
493 d.py = d.y = coords.y;
494 });
495 } else {
496 network.nodes.forEach(function (d) {
497 var old = d.oldxy || {x: d.x, y: d.y};
498 d.px = d.x = old.x;
499 d.py = d.y = old.y;
500 delete d.oldxy;
501 });
502 }
503
504 node.transition()
505 .duration(obview.time)
506 .attr(tickStuff.nodeAttr);
507 link.transition()
508 .duration(obview.time)
509 .attr(tickStuff.linkAttr);
510 linkLabel.transition()
511 .duration(obview.time)
512 .attr(tickStuff.linkLabelAttr);
513 }
514
515 function showPlane(tag, box, dir) {
516 var g = obview.plane[tag];
517
518 // set box origin at center..
519 box.x = -box.width/2;
520 box.y = -box.height/2;
521
522 g.select('rect')
523 .attr(box)
524 .attr('opacity', 0)
525 .transition()
526 .duration(obview.time)
527 .attr('opacity', 1)
528 .attr('transform', obview.obXform(box.height, dir));
529 }
530
531 function hidePlane(tag) {
532 var g = obview.plane[tag];
533
534 g.select('rect')
535 .transition()
536 .duration(obview.time)
537 .attr('opacity', 0)
538 .attr('transform', obview.noXform());
539 }
540
541 function insertPlanes(ox, oy) {
542 function ins(tag) {
543 var id = obview.id(tag),
544 g = panZoomContainer.insert('g', '#topo-G')
545 .attr('id', id)
546 .attr('transform', translate(ox,oy));
547 g.append('rect')
548 .attr('fill', obview.fill[tag])
549 .attr('opacity', 0);
550 obview.plane[tag] = g;
551 }
552 ins('opt');
553 ins('pkt');
554 }
555
556 function removePlanes() {
557 function rem(tag) {
558 var id = obview.id(tag);
559 panZoomContainer.select('#'+id)
560 .transition()
561 .duration(obview.time + 50)
562 .remove();
563 delete obview.plane[tag];
564 }
565 rem('opt');
566 rem('pkt');
567 }
568
569 function padBox(box, p) {
570 box.x -= p;
571 box.y -= p;
572 box.width += p*2;
573 box.height += p*2;
574 }
575
576 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800577 // Radio Button Callbacks
578
Simon Hunta5e89142014-11-14 07:00:33 -0800579 var layerLookup = {
580 host: {
Simon Hunt209155e2014-11-21 12:16:09 -0800581 endstation: 'pkt', // default, if host event does not define type
Thomas Vachuska89543292014-11-19 11:28:33 -0800582 router: 'pkt',
Simon Hunta5e89142014-11-14 07:00:33 -0800583 bgpSpeaker: 'pkt'
584 },
585 device: {
586 switch: 'pkt',
587 roadm: 'opt'
588 },
589 link: {
590 hostLink: 'pkt',
591 direct: 'pkt',
Simon Hunt8257f4c2014-11-16 19:34:54 -0800592 indirect: '',
593 tunnel: '',
Simon Hunta5e89142014-11-14 07:00:33 -0800594 optical: 'opt'
595 }
596 };
597
598 function inLayer(d, layer) {
Simon Hunt8257f4c2014-11-16 19:34:54 -0800599 var type = d.class === 'link' ? d.type() : d.type,
600 look = layerLookup[d.class],
601 lyr = look && look[type];
Simon Hunta5e89142014-11-14 07:00:33 -0800602 return lyr === layer;
603 }
604
605 function unsuppressLayer(which) {
606 node.each(function (d) {
607 var node = d.el;
608 if (inLayer(d, which)) {
609 node.classed('suppressed', false);
610 }
611 });
612
613 link.each(function (d) {
614 var link = d.el;
615 if (inLayer(d, which)) {
616 link.classed('suppressed', false);
617 }
618 });
619 }
620
Simon Hunt9462e8c2014-11-14 17:28:09 -0800621 function suppressLayers(b) {
622 node.classed('suppressed', b);
623 link.classed('suppressed', b);
Simon Hunt142d0032014-11-04 20:13:09 -0800624// d3.selectAll('svg .port').classed('inactive', false);
625// d3.selectAll('svg .portText').classed('inactive', false);
Simon Hunt195cb382014-11-03 17:50:51 -0800626 }
627
Simon Hunt9462e8c2014-11-14 17:28:09 -0800628 function showAllLayers() {
629 suppressLayers(false);
630 }
631
Simon Hunt195cb382014-11-03 17:50:51 -0800632 function showPacketLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800633 node.classed('suppressed', true);
634 link.classed('suppressed', true);
635 unsuppressLayer('pkt');
Simon Hunt195cb382014-11-03 17:50:51 -0800636 }
637
638 function showOpticalLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800639 node.classed('suppressed', true);
640 link.classed('suppressed', true);
641 unsuppressLayer('opt');
Simon Hunt195cb382014-11-03 17:50:51 -0800642 }
643
Simon Hunt9462e8c2014-11-14 17:28:09 -0800644 function restoreLayerState() {
645 layerBtnDispatch[layerBtnSet.selected()]();
646 }
647
Simon Hunt142d0032014-11-04 20:13:09 -0800648 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800649 // Private functions
650
Simon Hunt99c13842014-11-06 18:23:12 -0800651 function safeId(s) {
652 return s.replace(/[^a-z0-9]/gi, '-');
653 }
654
Simon Huntc7ee0662014-11-05 16:44:37 -0800655 // set the size of the given element to that of the view (reduced if padded)
656 function setSize(el, view, pad) {
657 var padding = pad ? pad * 2 : 0;
Simon Hunt934c3ce2014-11-05 11:45:07 -0800658 el.attr({
Simon Huntc7ee0662014-11-05 16:44:37 -0800659 width: view.width() - padding,
660 height: view.height() - padding
Simon Hunt934c3ce2014-11-05 11:45:07 -0800661 });
662 }
663
Simon Hunt8257f4c2014-11-16 19:34:54 -0800664 function makeNodeKey(d, what) {
665 var port = what + 'Port';
666 return d[what] + '/' + d[port];
667 }
668
669 function makeLinkKey(d, flipped) {
670 var one = flipped ? makeNodeKey(d, 'dst') : makeNodeKey(d, 'src'),
671 two = flipped ? makeNodeKey(d, 'src') : makeNodeKey(d, 'dst');
672 return one + '-' + two;
673 }
674
Simon Hunt269670f2014-11-17 16:17:43 -0800675 function findLinkById(id) {
676 // check to see if this is a reverse lookup, else default to given id
677 var key = network.revLinkToKey[id] || id;
678 return key && network.lookup[key];
679 }
680
Simon Hunt8257f4c2014-11-16 19:34:54 -0800681 function findLink(linkData, op) {
682 var key = makeLinkKey(linkData),
683 keyrev = makeLinkKey(linkData, 1),
684 link = network.lookup[key],
685 linkRev = network.lookup[keyrev],
686 result = {},
687 ldata = link || linkRev,
688 rawLink;
689
690 if (op === 'add') {
691 if (link) {
692 // trying to add a link that we already know about
693 result.ldata = link;
694 result.badLogic = 'addLink: link already added';
695
696 } else if (linkRev) {
697 // we found the reverse of the link to be added
698 result.ldata = linkRev;
699 if (linkRev.fromTarget) {
700 result.badLogic = 'addLink: link already added';
701 }
702 }
703 } else if (op === 'update') {
704 if (!ldata) {
705 result.badLogic = 'updateLink: link not found';
706 } else {
707 rawLink = link ? ldata.fromSource : ldata.fromTarget;
708 result.updateWith = function (data) {
709 $.extend(rawLink, data);
710 restyleLinkElement(ldata);
711 }
712 }
713 } else if (op === 'remove') {
714 if (!ldata) {
715 result.badLogic = 'removeLink: link not found';
716 } else {
717 rawLink = link ? ldata.fromSource : ldata.fromTarget;
718
719 if (!rawLink) {
720 result.badLogic = 'removeLink: link not found';
721
722 } else {
723 result.removeRawLink = function () {
724 if (link) {
725 // remove fromSource
726 ldata.fromSource = null;
727 if (ldata.fromTarget) {
728 // promote target into source position
729 ldata.fromSource = ldata.fromTarget;
730 ldata.fromTarget = null;
731 ldata.key = keyrev;
732 delete network.lookup[key];
733 network.lookup[keyrev] = ldata;
Simon Hunt269670f2014-11-17 16:17:43 -0800734 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800735 }
736 } else {
737 // remove fromTarget
738 ldata.fromTarget = null;
Simon Hunt269670f2014-11-17 16:17:43 -0800739 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800740 }
741 if (ldata.fromSource) {
742 restyleLinkElement(ldata);
743 } else {
744 removeLinkElement(ldata);
745 }
746 }
747 }
748 }
749 }
750 return result;
751 }
752
753 function addLinkUpdate(ldata, link) {
754 // add link event, but we already have the reverse link installed
755 ldata.fromTarget = link;
Simon Hunt269670f2014-11-17 16:17:43 -0800756 network.revLinkToKey[link.id] = ldata.key;
Simon Hunt8257f4c2014-11-16 19:34:54 -0800757 restyleLinkElement(ldata);
758 }
759
760 var allLinkTypes = 'direct indirect optical tunnel',
761 defaultLinkType = 'direct';
762
763 function restyleLinkElement(ldata) {
764 // this fn's job is to look at raw links and decide what svg classes
765 // need to be applied to the line element in the DOM
766 var el = ldata.el,
767 type = ldata.type(),
768 lw = ldata.linkWidth(),
769 online = ldata.online();
770
771 el.classed('link', true);
772 el.classed('inactive', !online);
773 el.classed(allLinkTypes, false);
774 if (type) {
775 el.classed(type, true);
776 }
777 el.transition()
778 .duration(1000)
Thomas Vachuska89543292014-11-19 11:28:33 -0800779 .attr('stroke-width', linkScale(lw))
780 .attr('stroke', config.topo.linkBaseColor);
Simon Hunt8257f4c2014-11-16 19:34:54 -0800781 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800782
Simon Hunt99c13842014-11-06 18:23:12 -0800783 // ==============================
784 // Event handlers for server-pushed events
785
Simon Huntbb282f52014-11-10 11:08:19 -0800786 function logicError(msg) {
787 // TODO, report logic error to server, via websock, so it can be logged
Simon Huntfc274c92014-11-11 11:05:46 -0800788 console.warn(msg);
Simon Huntbb282f52014-11-10 11:08:19 -0800789 }
790
Simon Hunt99c13842014-11-06 18:23:12 -0800791 var eventDispatch = {
Simon Hunta5e89142014-11-14 07:00:33 -0800792 addInstance: addInstance,
Simon Hunt99c13842014-11-06 18:23:12 -0800793 addDevice: addDevice,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800794 addLink: addLink,
Simon Hunt56d51852014-11-09 13:03:35 -0800795 addHost: addHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800796
Simon Huntfcfb46c2014-11-19 12:53:38 -0800797 updateInstance: updateInstance,
Simon Huntbb282f52014-11-10 11:08:19 -0800798 updateDevice: updateDevice,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800799 updateLink: updateLink,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800800 updateHost: updateHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800801
Simon Hunt7b403bc2014-11-22 19:01:00 -0800802 removeInstance: removeInstance,
Simon Huntca867ac2014-11-28 18:07:35 -0800803 removeDevice: removeDevice,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800804 removeLink: removeLink,
Simon Hunt44031102014-11-11 13:20:36 -0800805 removeHost: removeHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800806
Simon Hunt61d04042014-11-11 17:27:16 -0800807 showDetails: showDetails,
Thomas Vachuska47635c62014-11-22 01:21:36 -0800808 showSummary: showSummary,
Simon Huntb53e0682014-11-12 13:32:01 -0800809 showTraffic: showTraffic
Simon Hunt99c13842014-11-06 18:23:12 -0800810 };
811
Simon Hunta5e89142014-11-14 07:00:33 -0800812 function addInstance(data) {
813 evTrace(data);
814 var inst = data.payload,
815 id = inst.id;
816 if (onosInstances[id]) {
Thomas Vachuska12dfdc32014-11-29 16:03:12 -0800817 updateInstance(data);
Simon Hunta5e89142014-11-14 07:00:33 -0800818 return;
819 }
820 onosInstances[id] = inst;
821 onosOrder.push(inst);
822 updateInstances();
823 }
824
Simon Hunt99c13842014-11-06 18:23:12 -0800825 function addDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800826 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800827 var device = data.payload,
Simon Huntca867ac2014-11-28 18:07:35 -0800828 id = device.id,
829 d;
830
Simon Hunt233747b2014-12-04 12:01:58 -0800831 showNoDevs(false);
832
Simon Huntca867ac2014-11-28 18:07:35 -0800833 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 Hunt56a2ea42014-11-19 12:39:31 -0800901 function updateInstance(data) {
902 evTrace(data);
903 var inst = data.payload,
904 id = inst.id,
Simon Huntca867ac2014-11-28 18:07:35 -0800905 d = onosInstances[id];
906 if (d) {
907 $.extend(d, inst);
Simon Hunt56a2ea42014-11-19 12:39:31 -0800908 updateInstances();
Simon Hunt56a2ea42014-11-19 12:39:31 -0800909 } else {
910 logicError('updateInstance lookup fail. ID = "' + id + '"');
911 }
912 }
913
Simon Huntbb282f52014-11-10 11:08:19 -0800914 function updateDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800915 evTrace(data);
Simon Huntbb282f52014-11-10 11:08:19 -0800916 var device = data.payload,
917 id = device.id,
Simon Hunt6d9bd032014-11-28 22:16:40 -0800918 d = network.lookup[id],
919 wasOnline;
920
Simon Hunt62c47542014-11-22 22:16:32 -0800921 if (d) {
Simon Hunt6d9bd032014-11-28 22:16:40 -0800922 wasOnline = d.online;
Simon Hunt62c47542014-11-22 22:16:32 -0800923 $.extend(d, device);
924 if (positionNode(d, true)) {
Simon Hunt395a70c2014-11-22 23:17:40 -0800925 sendUpdateMeta(d, true);
Simon Hunt62c47542014-11-22 22:16:32 -0800926 }
927 updateNodes();
Simon Hunt6d9bd032014-11-28 22:16:40 -0800928 if (wasOnline !== d.online) {
929 findAttachedLinks(d.id).forEach(restyleLinkElement);
930 updateOfflineVisibility(d);
931 }
Simon Huntbb282f52014-11-10 11:08:19 -0800932 } else {
933 logicError('updateDevice lookup fail. ID = "' + id + '"');
934 }
935 }
936
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800937 function updateLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800938 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800939 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800940 result = findLink(link, 'update'),
941 bad = result.badLogic;
942 if (bad) {
943 logicError(bad + ': ' + link.id);
944 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800945 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800946 result.updateWith(link);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800947 }
948
Simon Hunt7cd48f32014-11-09 23:42:50 -0800949 function updateHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800950 evTrace(data);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800951 var host = data.payload,
Simon Huntbb282f52014-11-10 11:08:19 -0800952 id = host.id,
Simon Huntca867ac2014-11-28 18:07:35 -0800953 d = network.lookup[id];
954 if (d) {
955 $.extend(d, host);
Simon Hunt6d9bd032014-11-28 22:16:40 -0800956 if (positionNode(d, true)) {
957 sendUpdateMeta(d, true);
958 }
959 updateNodes(d);
Simon Huntbb282f52014-11-10 11:08:19 -0800960 } else {
961 logicError('updateHost lookup fail. ID = "' + id + '"');
962 }
Simon Hunt7cd48f32014-11-09 23:42:50 -0800963 }
964
Simon Hunt7b403bc2014-11-22 19:01:00 -0800965 function removeInstance(data) {
966 evTrace(data);
967 var inst = data.payload,
968 id = inst.id,
Simon Huntca867ac2014-11-28 18:07:35 -0800969 d = onosInstances[id];
970 if (d) {
971 var idx = find(id, onosOrder);
Simon Hunt7b403bc2014-11-22 19:01:00 -0800972 if (idx >= 0) {
973 onosOrder.splice(idx, 1);
974 }
975 delete onosInstances[id];
976 updateInstances();
977 } else {
978 logicError('updateInstance lookup fail. ID = "' + id + '"');
979 }
980 }
981
Simon Huntca867ac2014-11-28 18:07:35 -0800982 function removeDevice(data) {
983 evTrace(data);
984 var device = data.payload,
985 id = device.id,
986 d = network.lookup[id];
987 if (d) {
988 removeDeviceElement(d);
989 } else {
990 logicError('removeDevice lookup fail. ID = "' + id + '"');
991 }
992 }
993
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800994 function removeLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800995 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800996 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800997 result = findLink(link, 'remove'),
998 bad = result.badLogic;
999 if (bad) {
Simon Huntca867ac2014-11-28 18:07:35 -08001000 // may have already removed link, if attached to removed device
1001 console.warn(bad + ': ' + link.id);
Simon Hunt8257f4c2014-11-16 19:34:54 -08001002 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001003 }
Simon Hunt8257f4c2014-11-16 19:34:54 -08001004 result.removeRawLink();
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001005 }
1006
Simon Hunt44031102014-11-11 13:20:36 -08001007 function removeHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -08001008 evTrace(data);
Simon Hunt44031102014-11-11 13:20:36 -08001009 var host = data.payload,
1010 id = host.id,
Simon Huntca867ac2014-11-28 18:07:35 -08001011 d = network.lookup[id];
1012 if (d) {
1013 removeHostElement(d, true);
Simon Hunt44031102014-11-11 13:20:36 -08001014 } else {
Simon Huntca867ac2014-11-28 18:07:35 -08001015 // may have already removed host, if attached to removed device
1016 console.warn('removeHost lookup fail. ID = "' + id + '"');
Simon Hunt44031102014-11-11 13:20:36 -08001017 }
1018 }
1019
Simon Huntca867ac2014-11-28 18:07:35 -08001020 // the following events are server responses to user actions
Thomas Vachuska47635c62014-11-22 01:21:36 -08001021 function showSummary(data) {
1022 evTrace(data);
1023 populateSummary(data.payload);
Simon Hunt06811b72014-11-25 18:54:48 -08001024 showSummaryPane();
Thomas Vachuska47635c62014-11-22 01:21:36 -08001025 }
1026
Simon Hunt61d04042014-11-11 17:27:16 -08001027 function showDetails(data) {
Simon Hunta5e89142014-11-14 07:00:33 -08001028 evTrace(data);
Simon Hunt27d322d2014-11-28 10:45:43 -08001029 haveDetails = true;
Simon Hunt61d04042014-11-11 17:27:16 -08001030 populateDetails(data.payload);
Simon Hunt27d322d2014-11-28 10:45:43 -08001031 if (useDetails) {
1032 showDetailPane();
1033 }
Simon Hunt61d04042014-11-11 17:27:16 -08001034 }
1035
Simon Huntb53e0682014-11-12 13:32:01 -08001036 function showTraffic(data) {
Simon Hunta5e89142014-11-14 07:00:33 -08001037 evTrace(data);
Thomas Vachuska4731f122014-11-20 04:56:19 -08001038 var paths = data.payload.paths,
1039 hasTraffic = false;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -08001040
Thomas Vachuska3266abf2014-11-13 09:28:46 -08001041 // Revert any links hilighted previously.
Thomas Vachuska4731f122014-11-20 04:56:19 -08001042 link.style('stroke-width', null)
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08001043 .classed('primary secondary animated optical', false);
Simon Hunte2575b62014-11-18 15:25:53 -08001044 // Remove all previous labels.
1045 removeLinkLabels();
Thomas Vachuska3266abf2014-11-13 09:28:46 -08001046
Simon Hunte2575b62014-11-18 15:25:53 -08001047 // Now hilight all links in the paths payload, and attach
1048 // labels to them, if they are defined.
Simon Hunta255a2c2014-11-13 22:29:35 -08001049 paths.forEach(function (p) {
Simon Hunte2575b62014-11-18 15:25:53 -08001050 var n = p.links.length,
1051 i,
1052 ldata;
1053
Thomas Vachuska4731f122014-11-20 04:56:19 -08001054 hasTraffic = hasTraffic || p.traffic;
Simon Hunte2575b62014-11-18 15:25:53 -08001055 for (i=0; i<n; i++) {
1056 ldata = findLinkById(p.links[i]);
Thomas Vachuska4731f122014-11-20 04:56:19 -08001057 if (ldata && ldata.el) {
Simon Hunte2575b62014-11-18 15:25:53 -08001058 ldata.el.classed(p.class, true);
1059 ldata.label = p.labels[i];
Thomas Vachuskadea45ff2014-11-12 18:35:46 -08001060 }
Simon Hunte2575b62014-11-18 15:25:53 -08001061 }
Thomas Vachuskadea45ff2014-11-12 18:35:46 -08001062 });
Thomas Vachuska4731f122014-11-20 04:56:19 -08001063
Simon Hunte2575b62014-11-18 15:25:53 -08001064 updateLinks();
Thomas Vachuska4731f122014-11-20 04:56:19 -08001065
1066 if (hasTraffic && !antTimer) {
1067 startAntTimer();
1068 } else if (!hasTraffic && antTimer) {
1069 stopAntTimer();
1070 }
Simon Huntb53e0682014-11-12 13:32:01 -08001071 }
1072
Simon Hunt56d51852014-11-09 13:03:35 -08001073 // ...............................
1074
Simon Hunt99c13842014-11-06 18:23:12 -08001075 function unknownEvent(data) {
Simon Hunt434cf142014-11-24 11:10:28 -08001076 console.warn('Unknown event type: "' + data.event + '"', data);
Simon Hunt99c13842014-11-06 18:23:12 -08001077 }
1078
1079 function handleServerEvent(data) {
1080 var fn = eventDispatch[data.event] || unknownEvent;
1081 fn(data);
1082 }
1083
1084 // ==============================
Simon Hunt61d04042014-11-11 17:27:16 -08001085 // Out-going messages...
1086
Simon Huntb53e0682014-11-12 13:32:01 -08001087 function nSel() {
1088 return selectOrder.length;
1089 }
Simon Hunt61d04042014-11-11 17:27:16 -08001090 function getSel(idx) {
1091 return selections[selectOrder[idx]];
1092 }
Simon Huntb53e0682014-11-12 13:32:01 -08001093 function allSelectionsClass(cls) {
1094 for (var i=0, n=nSel(); i<n; i++) {
1095 if (getSel(i).obj.class !== cls) {
1096 return false;
1097 }
1098 }
1099 return true;
1100 }
Simon Hunt61d04042014-11-11 17:27:16 -08001101
Thomas Vachuska47635c62014-11-22 01:21:36 -08001102 function toggleInstances() {
1103 if (!oiBox.isVisible()) {
Simon Huntb0ecfa52014-11-23 21:05:12 -08001104 showInstances();
Thomas Vachuska47635c62014-11-22 01:21:36 -08001105 } else {
Simon Huntb0ecfa52014-11-23 21:05:12 -08001106 hideInstances();
Thomas Vachuska47635c62014-11-22 01:21:36 -08001107 }
1108 }
1109
Simon Huntb0ecfa52014-11-23 21:05:12 -08001110 function showInstances() {
1111 oiBox.show();
1112 colorAffinity = true;
1113 updateDeviceColors();
1114 }
1115
1116 function hideInstances() {
1117 oiBox.hide();
1118 colorAffinity = false;
1119 cancelAffinity();
1120 updateDeviceColors();
1121 }
1122
Thomas Vachuska1e68bdd2014-11-29 13:53:10 -08001123 function equalizeMasters() {
Thomas Vachuska1e68bdd2014-11-29 13:53:10 -08001124 sendMessage('equalizeMasters');
Simon Huntc1cc81c2014-11-29 14:59:01 -08001125 flash('Equalizing master roles');
Thomas Vachuska1e68bdd2014-11-29 13:53:10 -08001126 }
1127
Thomas Vachuska47635c62014-11-22 01:21:36 -08001128 function toggleSummary() {
1129 if (!summaryPane.isVisible()) {
1130 requestSummary();
1131 } else {
1132 cancelSummary();
1133 }
1134 }
1135
Thomas Vachuska47635c62014-11-22 01:21:36 -08001136 function requestSummary() {
Simon Huntc1cc81c2014-11-29 14:59:01 -08001137 sendMessage('requestSummary');
Thomas Vachuska47635c62014-11-22 01:21:36 -08001138 }
1139
1140 function cancelSummary() {
Simon Huntc1cc81c2014-11-29 14:59:01 -08001141 sendMessage('cancelSummary');
Simon Hunt06811b72014-11-25 18:54:48 -08001142 hideSummaryPane();
Thomas Vachuska47635c62014-11-22 01:21:36 -08001143 }
1144
Simon Hunt27d322d2014-11-28 10:45:43 -08001145 function toggleDetails() {
1146 useDetails = !useDetails;
1147 if (useDetails) {
1148 flash('Enable details pane');
1149 if (haveDetails) {
1150 showDetailPane();
1151 }
1152 } else {
1153 flash('Disable details pane');
1154 hideDetailPane();
1155 }
1156 }
1157
Simon Hunt06811b72014-11-25 18:54:48 -08001158 // encapsulate interaction between summary and details panes
1159 function showSummaryPane() {
1160 if (detailPane.isVisible()) {
1161 detailPane.down(summaryPane.show);
1162 } else {
1163 summaryPane.show();
1164 }
1165 }
1166
1167 function hideSummaryPane() {
1168 summaryPane.hide(function () {
1169 if (detailPane.isVisible()) {
1170 detailPane.up();
1171 }
1172 });
1173 }
1174
1175 function showDetailPane() {
1176 if (summaryPane.isVisible()) {
1177 detailPane.down(detailPane.show);
1178 } else {
1179 detailPane.up(detailPane.show);
1180 }
1181 }
1182
1183 function hideDetailPane() {
1184 detailPane.hide();
1185 }
1186
1187
Simon Hunt61d04042014-11-11 17:27:16 -08001188 // request details for the selected element
Simon Huntd72bc702014-11-13 18:38:04 -08001189 // invoked from selection of a single node.
Simon Hunt61d04042014-11-11 17:27:16 -08001190 function requestDetails() {
Simon Huntc1cc81c2014-11-29 14:59:01 -08001191 var data = getSel(0).obj;
1192 sendMessage('requestDetails', {
1193 id: data.id,
1194 class: data.class
1195 });
Simon Hunt61d04042014-11-11 17:27:16 -08001196 }
1197
Thomas Vachuska9edca302014-11-22 17:06:42 -08001198 function addHostIntentAction() {
Simon Huntd72bc702014-11-13 18:38:04 -08001199 sendMessage('addHostIntent', {
Thomas Vachuska9edca302014-11-22 17:06:42 -08001200 one: selectOrder[0],
1201 two: selectOrder[1],
1202 ids: selectOrder
Simon Huntd72bc702014-11-13 18:38:04 -08001203 });
Simon Hunt27d322d2014-11-28 10:45:43 -08001204 flash('Host-to-Host flow added');
Simon Huntd72bc702014-11-13 18:38:04 -08001205 }
1206
Thomas Vachuska9edca302014-11-22 17:06:42 -08001207 function addMultiSourceIntentAction() {
1208 sendMessage('addMultiSourceIntent', {
1209 src: selectOrder.slice(0, selectOrder.length - 1),
1210 dst: selectOrder[selectOrder.length - 1],
1211 ids: selectOrder
1212 });
Simon Hunt27d322d2014-11-28 10:45:43 -08001213 flash('Multi-Source flow added');
Thomas Vachuska29617e52014-11-20 03:17:46 -08001214 }
1215
Thomas Vachuska47635c62014-11-22 01:21:36 -08001216 function cancelTraffic() {
Simon Huntc1cc81c2014-11-29 14:59:01 -08001217 sendMessage('cancelTraffic');
Thomas Vachuska47635c62014-11-22 01:21:36 -08001218 }
1219
Thomas Vachuska9edca302014-11-22 17:06:42 -08001220 function requestTrafficForMode() {
Thomas Vachuska164fa5c2014-12-02 21:59:41 -08001221 if (hoverMode === hoverModeFlows) {
Thomas Vachuska9edca302014-11-22 17:06:42 -08001222 requestDeviceLinkFlows();
1223 } else if (hoverMode === hoverModeIntents) {
Thomas Vachuska164fa5c2014-12-02 21:59:41 -08001224 requestRelatedIntents();
Simon Huntd72bc702014-11-13 18:38:04 -08001225 }
Thomas Vachuska9edca302014-11-22 17:06:42 -08001226 }
Simon Huntd72bc702014-11-13 18:38:04 -08001227
Thomas Vachuska164fa5c2014-12-02 21:59:41 -08001228 function showRelatedIntentsAction() {
Thomas Vachuska9edca302014-11-22 17:06:42 -08001229 hoverMode = hoverModeIntents;
Thomas Vachuska164fa5c2014-12-02 21:59:41 -08001230 requestRelatedIntents();
Thomas Vachuskab7e40642014-12-03 11:16:06 -08001231 flash('Related Paths');
Thomas Vachuska9edca302014-11-22 17:06:42 -08001232 }
1233
Thomas Vachuska164fa5c2014-12-02 21:59:41 -08001234 function requestRelatedIntents() {
Simon Huntc1cc81c2014-11-29 14:59:01 -08001235 function hoverValid() {
1236 return hoverMode === hoverModeIntents &&
1237 hovered &&
1238 (hovered.class === 'host' || hovered.class === 'device');
1239 }
1240
Thomas Vachuska9edca302014-11-22 17:06:42 -08001241 if (validateSelectionContext()) {
Thomas Vachuska164fa5c2014-12-02 21:59:41 -08001242 sendMessage('requestRelatedIntents', {
Thomas Vachuska9edca302014-11-22 17:06:42 -08001243 ids: selectOrder,
Simon Huntc1cc81c2014-11-29 14:59:01 -08001244 hover: hoverValid() ? hovered.id : ''
Thomas Vachuska9edca302014-11-22 17:06:42 -08001245 });
1246 }
Simon Huntd72bc702014-11-13 18:38:04 -08001247 }
1248
Thomas Vachuska164fa5c2014-12-02 21:59:41 -08001249 function showNextIntentAction() {
1250 hoverMode = hoverModeNone;
Thomas Vachuskab7e40642014-12-03 11:16:06 -08001251 sendMessage('requestNextRelatedIntent');
1252 flash('>');
1253 }
1254
1255 function showPrevIntentAction() {
1256 hoverMode = hoverModeNone;
1257 sendMessage('requestPrevRelatedIntent');
1258 flash('<');
Thomas Vachuska164fa5c2014-12-02 21:59:41 -08001259 }
1260
1261 function showSelectedIntentTrafficAction() {
1262 hoverMode = hoverModeNone;
Thomas Vachuskab7e40642014-12-03 11:16:06 -08001263 sendMessage('requestSelectedIntentTraffic');
1264 flash('Traffic on Selected Path');
Thomas Vachuska164fa5c2014-12-02 21:59:41 -08001265 }
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -08001266
Thomas Vachuska29617e52014-11-20 03:17:46 -08001267 function showDeviceLinkFlowsAction() {
Thomas Vachuska9edca302014-11-22 17:06:42 -08001268 hoverMode = hoverModeFlows;
1269 requestDeviceLinkFlows();
Simon Hunt27d322d2014-11-28 10:45:43 -08001270 flash('Device Flows');
Thomas Vachuska29617e52014-11-20 03:17:46 -08001271 }
1272
Thomas Vachuska9edca302014-11-22 17:06:42 -08001273 function requestDeviceLinkFlows() {
Simon Huntc1cc81c2014-11-29 14:59:01 -08001274 function hoverValid() {
1275 return hoverMode === hoverModeFlows &&
1276 hovered && (hovered.class === 'device');
1277 }
1278
Thomas Vachuska9edca302014-11-22 17:06:42 -08001279 if (validateSelectionContext()) {
Thomas Vachuska9edca302014-11-22 17:06:42 -08001280 sendMessage('requestDeviceLinkFlows', {
1281 ids: selectOrder,
Simon Huntc1cc81c2014-11-29 14:59:01 -08001282 hover: hoverValid() ? hovered.id : ''
Thomas Vachuska9edca302014-11-22 17:06:42 -08001283 });
1284 }
1285 }
1286
Thomas Vachuska9edca302014-11-22 17:06:42 -08001287 function showAllTrafficAction() {
1288 hoverMode = hoverModeAll;
1289 requestAllTraffic();
Simon Hunt27d322d2014-11-28 10:45:43 -08001290 flash('All Traffic');
Thomas Vachuska9edca302014-11-22 17:06:42 -08001291 }
1292
1293 function requestAllTraffic() {
Simon Huntc1cc81c2014-11-29 14:59:01 -08001294 sendMessage('requestAllTraffic');
Thomas Vachuska9edca302014-11-22 17:06:42 -08001295 }
1296
1297 function validateSelectionContext() {
Thomas Vachuska29617e52014-11-20 03:17:46 -08001298 if (!hovered && nSel() === 0) {
Thomas Vachuska47635c62014-11-22 01:21:36 -08001299 cancelTraffic();
Thomas Vachuska9edca302014-11-22 17:06:42 -08001300 return false;
Thomas Vachuska29617e52014-11-20 03:17:46 -08001301 }
Thomas Vachuska9edca302014-11-22 17:06:42 -08001302 return true;
Thomas Vachuska29617e52014-11-20 03:17:46 -08001303 }
Simon Huntd72bc702014-11-13 18:38:04 -08001304
Simon Hunta6a9fe72014-11-20 11:17:12 -08001305
Simon Hunt61d04042014-11-11 17:27:16 -08001306 // ==============================
Simon Hunta5e89142014-11-14 07:00:33 -08001307 // onos instance panel functions
Simon Huntb82f6902014-11-22 11:53:15 -08001308
Simon Hunt7b403bc2014-11-22 19:01:00 -08001309 var instCfg = {
1310 rectPad: 8,
1311 nodeOx: 9,
1312 nodeOy: 9,
1313 nodeDim: 40,
1314 birdOx: 19,
1315 birdOy: 21,
1316 birdDim: 21,
1317 uiDy: 45,
1318 titleDy: 30,
1319 textYOff: 20,
1320 textYSpc: 15
1321 };
1322
1323 function viewBox(dim) {
1324 return '0 0 ' + dim.w + ' ' + dim.h;
1325 }
1326
1327 function instRectAttr(dim) {
1328 var pad = instCfg.rectPad;
1329 return {
1330 x: pad,
1331 y: pad,
1332 width: dim.w - pad*2,
1333 height: dim.h - pad*2,
Simon Huntb0ecfa52014-11-23 21:05:12 -08001334 rx: 6
Simon Hunt7b403bc2014-11-22 19:01:00 -08001335 };
1336 }
1337
1338 function computeDim(self) {
1339 var css = window.getComputedStyle(self);
1340 return {
1341 w: stripPx(css.width),
1342 h: stripPx(css.height)
1343 };
Simon Huntb82f6902014-11-22 11:53:15 -08001344 }
Simon Hunta5e89142014-11-14 07:00:33 -08001345
1346 function updateInstances() {
1347 var onoses = oiBox.el.selectAll('.onosInst')
Simon Huntb82f6902014-11-22 11:53:15 -08001348 .data(onosOrder, function (d) { return d.id; }),
Simon Hunt7b403bc2014-11-22 19:01:00 -08001349 instDim = {w:0,h:0},
1350 c = instCfg;
Simon Hunta5e89142014-11-14 07:00:33 -08001351
Simon Hunt7b403bc2014-11-22 19:01:00 -08001352 function nSw(n) {
1353 return '# Switches: ' + n;
1354 }
Simon Hunta5e89142014-11-14 07:00:33 -08001355
Simon Huntb82f6902014-11-22 11:53:15 -08001356 // operate on existing onos instances if necessary
1357 onoses.each(function (d) {
1358 var el = d3.select(this),
1359 svg = el.select('svg');
Simon Hunt7b403bc2014-11-22 19:01:00 -08001360 instDim = computeDim(this);
Simon Huntb82f6902014-11-22 11:53:15 -08001361
1362 // update online state
1363 el.classed('online', d.online);
1364
1365 // update ui-attached state
1366 svg.select('use.uiBadge').remove();
1367 if (d.uiAttached) {
1368 attachUiBadge(svg);
1369 }
1370
Simon Hunt7b403bc2014-11-22 19:01:00 -08001371 function updAttr(id, value) {
1372 svg.select('text.instLabel.'+id).text(value);
1373 }
1374
1375 updAttr('ip', d.ip);
1376 updAttr('ns', nSw(d.switches));
Simon Huntb82f6902014-11-22 11:53:15 -08001377 });
1378
1379
1380 // operate on new onos instances
Simon Hunta5e89142014-11-14 07:00:33 -08001381 var entering = onoses.enter()
1382 .append('div')
1383 .attr('class', 'onosInst')
1384 .classed('online', function (d) { return d.online; })
Simon Hunt9c15eca2014-11-15 18:37:59 -08001385 .on('click', clickInst);
1386
Simon Huntb82f6902014-11-22 11:53:15 -08001387 entering.each(function (d) {
Simon Hunt9c15eca2014-11-15 18:37:59 -08001388 var el = d3.select(this),
Simon Hunt7b403bc2014-11-22 19:01:00 -08001389 rectAttr,
1390 svg;
1391 instDim = computeDim(this);
1392 rectAttr = instRectAttr(instDim);
Simon Hunt9c15eca2014-11-15 18:37:59 -08001393
Simon Hunt7b403bc2014-11-22 19:01:00 -08001394 svg = el.append('svg').attr({
1395 width: instDim.w,
1396 height: instDim.h,
1397 viewBox: viewBox(instDim)
Simon Hunt95908012014-11-20 10:20:26 -08001398 });
Simon Huntb82f6902014-11-22 11:53:15 -08001399
Simon Hunt7b403bc2014-11-22 19:01:00 -08001400 svg.append('rect').attr(rectAttr);
Simon Hunt9c15eca2014-11-15 18:37:59 -08001401
Thomas Vachuskae02e11c2014-11-24 16:13:52 -08001402 appendBadge(svg, 14, 14, 28, '#bird');
Simon Huntb82f6902014-11-22 11:53:15 -08001403
1404 if (d.uiAttached) {
1405 attachUiBadge(svg);
1406 }
1407
Simon Hunt7b403bc2014-11-22 19:01:00 -08001408 var left = c.nodeOx + c.nodeDim,
1409 len = rectAttr.width - left,
1410 hlen = len / 2,
1411 midline = hlen + left;
Simon Hunt9c15eca2014-11-15 18:37:59 -08001412
Simon Hunt7b403bc2014-11-22 19:01:00 -08001413 // title
1414 svg.append('text')
1415 .attr({
1416 class: 'instTitle',
1417 x: midline,
1418 y: c.titleDy
1419 })
1420 .text(d.id);
1421
1422 // a couple of attributes
1423 var ty = c.titleDy + c.textYOff;
1424
1425 function addAttr(id, label) {
1426 svg.append('text').attr({
1427 class: 'instLabel ' + id,
1428 x: midline,
1429 y: ty
1430 }).text(label);
1431 ty += c.textYSpc;
1432 }
1433
1434 addAttr('ip', d.ip);
1435 addAttr('ns', nSw(d.switches));
Simon Hunt9c15eca2014-11-15 18:37:59 -08001436 });
Simon Hunta5e89142014-11-14 07:00:33 -08001437
1438 // operate on existing + new onoses here
Simon Hunt8f40cce2014-11-23 15:57:30 -08001439 // set the affinity colors...
1440 onoses.each(function (d) {
1441 var el = d3.select(this),
1442 rect = el.select('svg').select('rect'),
1443 col = instColor(d.id, d.online);
1444 rect.style('fill', col);
1445 });
Simon Hunta5e89142014-11-14 07:00:33 -08001446
Simon Hunt7b403bc2014-11-22 19:01:00 -08001447 // adjust the panel size appropriately...
1448 oiBox.width(instDim.w * onosOrder.length);
1449 oiBox.height(instDim.h);
1450
1451 // remove any outgoing instances
1452 onoses.exit().remove();
Simon Hunta5e89142014-11-14 07:00:33 -08001453 }
1454
Simon Hunt8f40cce2014-11-23 15:57:30 -08001455 function instColor(id, online) {
1456 return cat7.get(id, !online, network.view.getTheme());
1457 }
1458
Simon Hunt9462e8c2014-11-14 17:28:09 -08001459 function clickInst(d) {
1460 var el = d3.select(this),
1461 aff = el.classed('affinity');
1462 if (!aff) {
1463 setAffinity(el, d);
1464 } else {
1465 cancelAffinity();
1466 }
1467 }
1468
1469 function setAffinity(el, d) {
1470 d3.selectAll('.onosInst')
1471 .classed('mastership', true)
1472 .classed('affinity', false);
1473 el.classed('affinity', true);
1474
1475 suppressLayers(true);
1476 node.each(function (n) {
1477 if (n.master === d.id) {
1478 n.el.classed('suppressed', false);
1479 }
1480 });
1481 oiShowMaster = true;
1482 }
1483
1484 function cancelAffinity() {
1485 d3.selectAll('.onosInst')
1486 .classed('mastership affinity', false);
1487 restoreLayerState();
1488 oiShowMaster = false;
1489 }
1490
Simon Hunt7b403bc2014-11-22 19:01:00 -08001491 // TODO: these should be moved out to utility module.
1492 function stripPx(s) {
1493 return s.replace(/px$/,'');
1494 }
1495
1496 function appendUse(svg, ox, oy, dim, iid, cls) {
1497 var use = svg.append('use').attr({
1498 transform: translate(ox,oy),
1499 'xlink:href': iid,
1500 width: dim,
1501 height: dim
1502 });
1503 if (cls) {
1504 use.classed(cls, true);
1505 }
1506 return use;
1507 }
1508
1509 function appendGlyph(svg, ox, oy, dim, iid, cls) {
1510 appendUse(svg, ox, oy, dim, iid, cls).classed('glyphIcon', true);
1511 }
1512
1513 function appendBadge(svg, ox, oy, dim, iid, cls) {
Simon Hunt233747b2014-12-04 12:01:58 -08001514 appendUse(svg, ox, oy, dim, iid, cls).classed('badgeIcon', true);
Simon Hunt7b403bc2014-11-22 19:01:00 -08001515 }
1516
1517 function attachUiBadge(svg) {
1518 appendBadge(svg, 12, instCfg.uiDy, 30, '#uiAttached', 'uiBadge');
1519 }
1520
Simon Hunt434cf142014-11-24 11:10:28 -08001521 function visVal(b) {
1522 return b ? 'visible' : 'hidden';
1523 }
1524
Simon Hunta5e89142014-11-14 07:00:33 -08001525 // ==============================
Simon Hunt99c13842014-11-06 18:23:12 -08001526 // force layout modification functions
1527
1528 function translate(x, y) {
1529 return 'translate(' + x + ',' + y + ')';
1530 }
Simon Huntd6f5a272014-11-29 23:45:50 -08001531 function scale(x,y) {
1532 return 'scale(' + x + ',' + y + ')';
1533 }
1534 function skewX(x) {
1535 return 'skewX(' + x + ')';
1536 }
Simon Hunte2575b62014-11-18 15:25:53 -08001537 function rotate(deg) {
1538 return 'rotate(' + deg + ')';
1539 }
1540
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001541 function missMsg(what, id) {
1542 return '\n[' + what + '] "' + id + '" missing ';
1543 }
1544
1545 function linkEndPoints(srcId, dstId) {
1546 var srcNode = network.lookup[srcId],
1547 dstNode = network.lookup[dstId],
1548 sMiss = !srcNode ? missMsg('src', srcId) : '',
1549 dMiss = !dstNode ? missMsg('dst', dstId) : '';
1550
1551 if (sMiss || dMiss) {
1552 logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
1553 return null;
1554 }
1555 return {
1556 source: srcNode,
1557 target: dstNode,
1558 x1: srcNode.x,
1559 y1: srcNode.y,
1560 x2: dstNode.x,
1561 y2: dstNode.y
1562 };
1563 }
1564
Simon Hunt56d51852014-11-09 13:03:35 -08001565 function createHostLink(host) {
1566 var src = host.id,
1567 dst = host.cp.device,
Simon Hunt7cd48f32014-11-09 23:42:50 -08001568 id = host.ingress,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001569 lnk = linkEndPoints(src, dst);
Simon Hunt56d51852014-11-09 13:03:35 -08001570
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001571 if (!lnk) {
Simon Hunt56d51852014-11-09 13:03:35 -08001572 return null;
1573 }
1574
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001575 // Synthesize link ...
1576 $.extend(lnk, {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001577 key: id,
Simon Hunt56d51852014-11-09 13:03:35 -08001578 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -08001579
1580 type: function () { return 'hostLink'; },
Simon Hunt6d9bd032014-11-28 22:16:40 -08001581 online: function () {
1582 // hostlink target is edge switch
1583 return lnk.target.online;
1584 },
Simon Hunt8257f4c2014-11-16 19:34:54 -08001585 linkWidth: function () { return 1; }
Simon Hunt7cd48f32014-11-09 23:42:50 -08001586 });
Simon Hunt99c13842014-11-06 18:23:12 -08001587 return lnk;
1588 }
1589
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001590 function createLink(link) {
Simon Hunta6a9fe72014-11-20 11:17:12 -08001591 var lnk = linkEndPoints(link.src, link.dst);
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001592
1593 if (!lnk) {
1594 return null;
1595 }
1596
Simon Hunt8257f4c2014-11-16 19:34:54 -08001597 $.extend(lnk, {
1598 key: link.id,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001599 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -08001600 fromSource: link,
1601
1602 // functions to aggregate dual link state
1603 type: function () {
1604 var s = lnk.fromSource,
1605 t = lnk.fromTarget;
1606 return (s && s.type) || (t && t.type) || defaultLinkType;
1607 },
1608 online: function () {
1609 var s = lnk.fromSource,
Simon Hunt6d9bd032014-11-28 22:16:40 -08001610 t = lnk.fromTarget,
1611 both = lnk.source.online && lnk.target.online;
1612 return both && ((s && s.online) || (t && t.online));
Simon Hunt8257f4c2014-11-16 19:34:54 -08001613 },
1614 linkWidth: function () {
1615 var s = lnk.fromSource,
1616 t = lnk.fromTarget,
1617 ws = (s && s.linkWidth) || 0,
1618 wt = (t && t.linkWidth) || 0;
1619 return Math.max(ws, wt);
1620 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001621 });
1622 return lnk;
Simon Hunt1a9eff92014-11-07 11:06:34 -08001623 }
1624
Simon Hunte2575b62014-11-18 15:25:53 -08001625 function removeLinkLabels() {
1626 network.links.forEach(function (d) {
1627 d.label = '';
1628 });
1629 }
1630
Simon Hunt434cf142014-11-24 11:10:28 -08001631 function showHostVis(el) {
1632 el.style('visibility', visVal(showHosts));
1633 }
1634
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001635 var widthRatio = 1.4,
1636 linkScale = d3.scale.linear()
1637 .domain([1, 12])
1638 .range([widthRatio, 12 * widthRatio])
1639 .clamp(true);
1640
Simon Hunt99c13842014-11-06 18:23:12 -08001641 function updateLinks() {
1642 link = linkG.selectAll('.link')
Simon Hunt8257f4c2014-11-16 19:34:54 -08001643 .data(network.links, function (d) { return d.key; });
Simon Hunt99c13842014-11-06 18:23:12 -08001644
1645 // operate on existing links, if necessary
1646 // link .foo() .bar() ...
1647
1648 // operate on entering links:
1649 var entering = link.enter()
1650 .append('line')
1651 .attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001652 x1: function (d) { return d.x1; },
1653 y1: function (d) { return d.y1; },
1654 x2: function (d) { return d.x2; },
1655 y2: function (d) { return d.y2; },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001656 stroke: config.topo.linkInColor,
1657 'stroke-width': config.topo.linkInWidth
Simon Hunt99c13842014-11-06 18:23:12 -08001658 });
1659
1660 // augment links
Simon Hunt7cd48f32014-11-09 23:42:50 -08001661 entering.each(function (d) {
1662 var link = d3.select(this);
1663 // provide ref to element selection from backing data....
1664 d.el = link;
Simon Hunt8257f4c2014-11-16 19:34:54 -08001665 restyleLinkElement(d);
Simon Hunt434cf142014-11-24 11:10:28 -08001666 if (d.type() === 'hostLink') {
1667 showHostVis(link);
1668 }
Simon Hunt7cd48f32014-11-09 23:42:50 -08001669 });
Thomas Vachuska4830d392014-11-09 17:09:56 -08001670
1671 // operate on both existing and new links, if necessary
1672 //link .foo() .bar() ...
1673
Simon Hunte2575b62014-11-18 15:25:53 -08001674 // apply or remove labels
1675 var labelData = getLabelData();
1676 applyLinkLabels(labelData);
1677
Thomas Vachuska4830d392014-11-09 17:09:56 -08001678 // operate on exiting links:
Thomas Vachuska4830d392014-11-09 17:09:56 -08001679 link.exit()
Simon Hunt6d9bd032014-11-28 22:16:40 -08001680 .attr('stroke-dasharray', '3 3')
Simon Hunt13bf9c82014-11-18 07:26:44 -08001681 .style('opacity', 0.5)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001682 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -08001683 .duration(1500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001684 .attr({
Simon Hunt6d9bd032014-11-28 22:16:40 -08001685 'stroke-dasharray': '3 12',
Simon Hunt13bf9c82014-11-18 07:26:44 -08001686 stroke: config.topo.linkOutColor,
1687 'stroke-width': config.topo.linkOutWidth
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001688 })
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001689 .style('opacity', 0.0)
Thomas Vachuska4830d392014-11-09 17:09:56 -08001690 .remove();
Simon Hunte2575b62014-11-18 15:25:53 -08001691
1692 // NOTE: invoke a single tick to force the labels to position
1693 // onto their links.
1694 tick();
1695 }
1696
1697 function getLabelData() {
1698 // create the backing data for showing labels..
1699 var data = [];
1700 link.each(function (d) {
1701 if (d.label) {
1702 data.push({
1703 id: 'lab-' + d.key,
1704 key: d.key,
1705 label: d.label,
1706 ldata: d
1707 });
1708 }
1709 });
1710 return data;
1711 }
1712
1713 var linkLabelOffset = '0.3em';
1714
1715 function applyLinkLabels(data) {
1716 var entering;
1717
1718 linkLabel = linkLabelG.selectAll('.linkLabel')
1719 .data(data, function (d) { return d.id; });
1720
Simon Hunt56a2ea42014-11-19 12:39:31 -08001721 // for elements already existing, we need to update the text
1722 // and adjust the rectangle size to fit
1723 linkLabel.each(function (d) {
1724 var el = d3.select(this),
1725 rect = el.select('rect'),
1726 text = el.select('text');
1727 text.text(d.label);
1728 rect.attr(rectAroundText(el));
1729 });
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -08001730
Simon Hunte2575b62014-11-18 15:25:53 -08001731 entering = linkLabel.enter().append('g')
1732 .classed('linkLabel', true)
1733 .attr('id', function (d) { return d.id; });
1734
1735 entering.each(function (d) {
1736 var el = d3.select(this),
1737 rect,
1738 text,
1739 parms = {
1740 x1: d.ldata.x1,
1741 y1: d.ldata.y1,
1742 x2: d.ldata.x2,
1743 y2: d.ldata.y2
1744 };
1745
1746 d.el = el;
1747 rect = el.append('rect');
1748 text = el.append('text').text(d.label);
1749 rect.attr(rectAroundText(el));
1750 text.attr('dy', linkLabelOffset);
1751
1752 el.attr('transform', transformLabel(parms));
1753 });
1754
Simon Huntc2465f42014-12-04 10:28:07 -08001755 // Remove any labels that are no longer required.
Simon Hunte2575b62014-11-18 15:25:53 -08001756 linkLabel.exit().remove();
1757 }
1758
1759 function rectAroundText(el) {
1760 var text = el.select('text'),
1761 box = text.node().getBBox();
1762
1763 // translate the bbox so that it is centered on [x,y]
1764 box.x = -box.width / 2;
1765 box.y = -box.height / 2;
1766
1767 // add padding
1768 box.x -= 1;
1769 box.width += 2;
1770 return box;
1771 }
1772
1773 function transformLabel(p) {
1774 var dx = p.x2 - p.x1,
1775 dy = p.y2 - p.y1,
1776 xMid = dx/2 + p.x1,
1777 yMid = dy/2 + p.y1;
Simon Hunte2575b62014-11-18 15:25:53 -08001778 return translate(xMid, yMid);
Simon Hunt99c13842014-11-06 18:23:12 -08001779 }
1780
1781 function createDeviceNode(device) {
1782 // start with the object as is
1783 var node = device,
Simon Huntbb282f52014-11-10 11:08:19 -08001784 type = device.type,
Simon Huntc2465f42014-12-04 10:28:07 -08001785 svgCls = type ? 'node device ' + type : 'node device';
Simon Huntc72967b2014-11-20 09:21:42 -08001786
Simon Hunt99c13842014-11-06 18:23:12 -08001787 // Augment as needed...
1788 node.class = 'device';
Simon Huntbb282f52014-11-10 11:08:19 -08001789 node.svgClass = device.online ? svgCls + ' online' : svgCls;
Simon Hunt99c13842014-11-06 18:23:12 -08001790 positionNode(node);
Simon Hunt99c13842014-11-06 18:23:12 -08001791 return node;
1792 }
1793
Simon Hunt56d51852014-11-09 13:03:35 -08001794 function createHostNode(host) {
1795 // start with the object as is
1796 var node = host;
1797
1798 // Augment as needed...
1799 node.class = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001800 if (!node.type) {
Simon Hunt209155e2014-11-21 12:16:09 -08001801 node.type = 'endstation';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001802 }
Simon Hunt7fa116d2014-11-17 14:16:55 -08001803 node.svgClass = 'node host ' + node.type;
Simon Hunt56d51852014-11-09 13:03:35 -08001804 positionNode(node);
Simon Hunt56d51852014-11-09 13:03:35 -08001805 return node;
1806 }
1807
Simon Hunt62c47542014-11-22 22:16:32 -08001808 function positionNode(node, forUpdate) {
Simon Hunt99c13842014-11-06 18:23:12 -08001809 var meta = node.metaUi,
Simon Huntac9e24f2014-11-12 10:12:21 -08001810 x = meta && meta.x,
1811 y = meta && meta.y,
1812 xy;
Simon Hunt99c13842014-11-06 18:23:12 -08001813
Simon Huntac9e24f2014-11-12 10:12:21 -08001814 // If we have [x,y] already, use that...
Simon Hunt99c13842014-11-06 18:23:12 -08001815 if (x && y) {
1816 node.fixed = true;
Simon Hunt62c47542014-11-22 22:16:32 -08001817 node.px = node.x = x;
1818 node.py = node.y = y;
Simon Huntac9e24f2014-11-12 10:12:21 -08001819 return;
Simon Hunt99c13842014-11-06 18:23:12 -08001820 }
Simon Huntac9e24f2014-11-12 10:12:21 -08001821
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001822 var location = node.location;
1823 if (location && location.type === 'latlng') {
Simon Hunt1b18aa52014-11-29 17:57:55 -08001824 var coord = geoMapProj([location.lng, location.lat]);
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001825 node.fixed = true;
Simon Hunt62c47542014-11-22 22:16:32 -08001826 node.px = node.x = coord[0];
1827 node.py = node.y = coord[1];
Simon Hunt62c47542014-11-22 22:16:32 -08001828 return true;
1829 }
1830
1831 // if this is a node update (not a node add).. skip randomizer
1832 if (forUpdate) {
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001833 return;
1834 }
1835
Simon Huntac9e24f2014-11-12 10:12:21 -08001836 // Note: Placing incoming unpinned nodes at exactly the same point
1837 // (center of the view) causes them to explode outwards when
1838 // the force layout kicks in. So, we spread them out a bit
1839 // initially, to provide a more serene layout convergence.
1840 // Additionally, if the node is a host, we place it near
1841 // the device it is connected to.
1842
1843 function spread(s) {
1844 return Math.floor((Math.random() * s) - s/2);
1845 }
1846
1847 function randDim(dim) {
1848 return dim / 2 + spread(dim * 0.7071);
1849 }
1850
1851 function rand() {
1852 return {
1853 x: randDim(network.view.width()),
1854 y: randDim(network.view.height())
1855 };
1856 }
1857
1858 function near(node) {
1859 var min = 12,
1860 dx = spread(12),
1861 dy = spread(12);
1862 return {
1863 x: node.x + min + dx,
1864 y: node.y + min + dy
1865 };
1866 }
1867
1868 function getDevice(cp) {
1869 var d = network.lookup[cp.device];
1870 return d || rand();
1871 }
1872
1873 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
1874 $.extend(node, xy);
Simon Hunt99c13842014-11-06 18:23:12 -08001875 }
1876
Simon Hunt99c13842014-11-06 18:23:12 -08001877
Simon Huntc72967b2014-11-20 09:21:42 -08001878 function iconGlyphUrl(d) {
1879 var which = d.type || 'unknown';
1880 return '#' + which;
1881 }
1882
Simon Hunt99c13842014-11-06 18:23:12 -08001883 // returns the newly computed bounding box of the rectangle
1884 function adjustRectToFitText(n) {
1885 var text = n.select('text'),
1886 box = text.node().getBBox(),
1887 lab = config.labels;
1888
1889 text.attr('text-anchor', 'middle')
1890 .attr('y', '-0.8em')
1891 .attr('x', lab.imgPad/2);
1892
1893 // translate the bbox so that it is centered on [x,y]
1894 box.x = -box.width / 2;
1895 box.y = -box.height / 2;
1896
1897 // add padding
1898 box.x -= (lab.padLR + lab.imgPad/2);
1899 box.width += lab.padLR * 2 + lab.imgPad;
1900 box.y -= lab.padTB;
1901 box.height += lab.padTB * 2;
1902
1903 return box;
1904 }
1905
Simon Hunt1a9eff92014-11-07 11:06:34 -08001906 function mkSvgClass(d) {
1907 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
1908 }
1909
Simon Hunt7cd48f32014-11-09 23:42:50 -08001910 function hostLabel(d) {
1911 var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0;
1912 return d.labels[idx];
1913 }
1914 function deviceLabel(d) {
1915 var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0;
1916 return d.labels[idx];
1917 }
Simon Huntc72967b2014-11-20 09:21:42 -08001918 function trimLabel(label) {
1919 return (label && label.trim()) || '';
1920 }
1921
1922 function emptyBox() {
1923 return {
1924 x: -2,
1925 y: -2,
1926 width: 4,
1927 height: 4
1928 };
Simon Hunt7cd48f32014-11-09 23:42:50 -08001929 }
1930
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001931 function updateDeviceLabel(d) {
Simon Huntc72967b2014-11-20 09:21:42 -08001932 var label = trimLabel(deviceLabel(d)),
1933 noLabel = !label,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001934 node = d.el,
Simon Huntc72967b2014-11-20 09:21:42 -08001935 box,
1936 dx,
Simon Hunt395a70c2014-11-22 23:17:40 -08001937 dy,
1938 cfg = config.icons.device;
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001939
1940 node.select('text')
1941 .text(label)
1942 .style('opacity', 0)
1943 .transition()
1944 .style('opacity', 1);
1945
Simon Huntc72967b2014-11-20 09:21:42 -08001946 if (noLabel) {
1947 box = emptyBox();
Simon Hunt395a70c2014-11-22 23:17:40 -08001948 dx = -cfg.dim/2;
1949 dy = -cfg.dim/2;
Simon Huntc72967b2014-11-20 09:21:42 -08001950 } else {
1951 box = adjustRectToFitText(node);
Simon Hunt395a70c2014-11-22 23:17:40 -08001952 dx = box.x + cfg.xoff;
1953 dy = box.y + cfg.yoff;
Simon Huntc72967b2014-11-20 09:21:42 -08001954 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001955
1956 node.select('rect')
1957 .transition()
1958 .attr(box);
1959
Simon Huntc72967b2014-11-20 09:21:42 -08001960 node.select('g.deviceIcon')
Thomas Vachuska89543292014-11-19 11:28:33 -08001961 .transition()
Simon Huntc72967b2014-11-20 09:21:42 -08001962 .attr('transform', translate(dx, dy));
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001963 }
1964
1965 function updateHostLabel(d) {
Simon Hunt6d9bd032014-11-28 22:16:40 -08001966 var label = trimLabel(hostLabel(d));
1967 d.el.select('text').text(label);
Simon Huntbb282f52014-11-10 11:08:19 -08001968 }
1969
Simon Hunt434cf142014-11-24 11:10:28 -08001970 function updateHostVisibility() {
1971 var v = visVal(showHosts);
1972 nodeG.selectAll('.host').style('visibility', v);
1973 linkG.selectAll('.hostLink').style('visibility', v);
1974 }
1975
Simon Hunt6d9bd032014-11-28 22:16:40 -08001976 function findOfflineNodes() {
1977 var a = [];
1978 network.nodes.forEach(function (d) {
1979 if (d.class === 'device' && !d.online) {
1980 a.push(d);
1981 }
1982 });
1983 return a;
1984 }
1985
1986 function updateOfflineVisibility(dev) {
1987 var so = showOffline,
1988 sh = showHosts,
1989 vb = 'visibility',
1990 v, off, al, ah, db, b;
1991
1992 function updAtt(show) {
1993 al.forEach(function (d) {
1994 b = show && ((d.type() !== 'hostLink') || sh);
1995 d.el.style(vb, visVal(b));
1996 });
1997 ah.forEach(function (d) {
1998 b = show && sh;
1999 d.el.style(vb, visVal(b));
2000 });
2001 }
2002
2003 if (dev) {
2004 // updating a specific device that just toggled off/on-line
2005 db = dev.online || so;
2006 dev.el.style(vb, visVal(db));
2007 al = findAttachedLinks(dev.id);
2008 ah = findAttachedHosts(dev.id);
2009 updAtt(db);
2010 } else {
2011 // updating all offline devices
2012 v = visVal(so);
2013 off = findOfflineNodes();
2014 off.forEach(function (d) {
2015 d.el.style(vb, v);
2016 al = findAttachedLinks(d.id);
2017 ah = findAttachedHosts(d.id);
2018 updAtt(so);
2019 });
2020 }
2021 }
2022
Simon Hunt6ac93f32014-11-13 12:17:27 -08002023 function nodeMouseOver(d) {
Thomas Vachuska164fa5c2014-12-02 21:59:41 -08002024 if (hovered != d) {
2025 hovered = d;
2026 requestTrafficForMode();
2027 }
Simon Hunt6ac93f32014-11-13 12:17:27 -08002028 }
2029
2030 function nodeMouseOut(d) {
Thomas Vachuska164fa5c2014-12-02 21:59:41 -08002031 if (hovered != null) {
2032 hovered = null;
2033 requestTrafficForMode();
2034 }
Simon Hunt6ac93f32014-11-13 12:17:27 -08002035 }
Simon Huntbb282f52014-11-10 11:08:19 -08002036
Simon Hunteb1514d2014-11-20 09:57:29 -08002037 function addHostIcon(node, radius, iid) {
Thomas Vachuska89543292014-11-19 11:28:33 -08002038 var dim = radius * 1.5,
2039 xlate = -dim / 2;
2040
Simon Hunteb1514d2014-11-20 09:57:29 -08002041 node.append('use').attr({
2042 class: 'glyphIcon hostIcon',
2043 transform: translate(xlate,xlate),
2044 'xlink:href': iid,
2045 width: dim,
2046 height: dim
2047 });
Thomas Vachuska89543292014-11-19 11:28:33 -08002048 }
2049
Simon Hunt99c13842014-11-06 18:23:12 -08002050 function updateNodes() {
2051 node = nodeG.selectAll('.node')
2052 .data(network.nodes, function (d) { return d.id; });
2053
Simon Hunt62c47542014-11-22 22:16:32 -08002054 // operate on existing nodes...
2055 node.filter('.device').each(function (d) {
Simon Hunt62c47542014-11-22 22:16:32 -08002056 var node = d.el;
2057 node.classed('online', d.online);
2058 updateDeviceLabel(d);
2059 positionNode(d, true);
2060 });
2061
2062 node.filter('.host').each(function (d) {
Simon Hunt6d9bd032014-11-28 22:16:40 -08002063 updateHostLabel(d);
2064 positionNode(d, true);
Simon Hunt62c47542014-11-22 22:16:32 -08002065 });
Simon Hunt99c13842014-11-06 18:23:12 -08002066
2067 // operate on entering nodes:
2068 var entering = node.enter()
2069 .append('g')
2070 .attr({
2071 id: function (d) { return safeId(d.id); },
Simon Hunt1a9eff92014-11-07 11:06:34 -08002072 class: mkSvgClass,
Simon Hunt99c13842014-11-06 18:23:12 -08002073 transform: function (d) { return translate(d.x, d.y); },
2074 opacity: 0
2075 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08002076 .call(network.drag)
Simon Hunt6ac93f32014-11-13 12:17:27 -08002077 .on('mouseover', nodeMouseOver)
2078 .on('mouseout', nodeMouseOut)
Simon Hunt99c13842014-11-06 18:23:12 -08002079 .transition()
2080 .attr('opacity', 1);
2081
2082 // augment device nodes...
2083 entering.filter('.device').each(function (d) {
2084 var node = d3.select(this),
Simon Huntc72967b2014-11-20 09:21:42 -08002085 label = trimLabel(deviceLabel(d)),
2086 noLabel = !label,
Simon Hunt99c13842014-11-06 18:23:12 -08002087 box;
2088
Simon Hunt7cd48f32014-11-09 23:42:50 -08002089 // provide ref to element from backing data....
2090 d.el = node;
2091
Simon Hunt62c47542014-11-22 22:16:32 -08002092 node.append('rect').attr({ rx: 5, ry: 5 });
2093 node.append('text').text(label).attr('dy', '1.1em');
Simon Hunt99c13842014-11-06 18:23:12 -08002094 box = adjustRectToFitText(node);
Simon Hunta3dd9572014-11-20 15:22:41 -08002095 node.select('rect').attr(box);
Simon Huntc72967b2014-11-20 09:21:42 -08002096 addDeviceIcon(node, box, noLabel, iconGlyphUrl(d));
Simon Huntc7ee0662014-11-05 16:44:37 -08002097 });
Simon Hunt934c3ce2014-11-05 11:45:07 -08002098
Simon Hunt56d51852014-11-09 13:03:35 -08002099 // augment host nodes...
2100 entering.filter('.host').each(function (d) {
2101 var node = d3.select(this),
Simon Hunt395a70c2014-11-22 23:17:40 -08002102 cfg = config.icons.host,
2103 r = cfg.radius[d.type] || cfg.defaultRadius,
Thomas Vachuska89543292014-11-19 11:28:33 -08002104 textDy = r + 10,
Simon Hunteb1514d2014-11-20 09:57:29 -08002105 iid = iconGlyphUrl(d);
Simon Hunt56d51852014-11-09 13:03:35 -08002106
Simon Hunt7cd48f32014-11-09 23:42:50 -08002107 // provide ref to element from backing data....
2108 d.el = node;
Simon Hunt434cf142014-11-24 11:10:28 -08002109 showHostVis(node);
Simon Hunt7cd48f32014-11-09 23:42:50 -08002110
Simon Hunt62c47542014-11-22 22:16:32 -08002111 node.append('circle').attr('r', r);
Simon Hunteb1514d2014-11-20 09:57:29 -08002112 if (iid) {
2113 addHostIcon(node, r, iid);
Simon Hunt7fa116d2014-11-17 14:16:55 -08002114 }
Simon Hunt56d51852014-11-09 13:03:35 -08002115 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08002116 .text(hostLabel)
Thomas Vachuska89543292014-11-19 11:28:33 -08002117 .attr('dy', textDy)
Simon Hunt7cd48f32014-11-09 23:42:50 -08002118 .attr('text-anchor', 'middle');
Simon Hunt56d51852014-11-09 13:03:35 -08002119 });
Simon Huntc7ee0662014-11-05 16:44:37 -08002120
Simon Hunt99c13842014-11-06 18:23:12 -08002121 // operate on both existing and new nodes, if necessary
Simon Huntb0ecfa52014-11-23 21:05:12 -08002122 updateDeviceColors();
Simon Huntc7ee0662014-11-05 16:44:37 -08002123
Simon Hunt99c13842014-11-06 18:23:12 -08002124 // operate on exiting nodes:
Simon Huntea80eb42014-11-11 13:46:57 -08002125 // Note that the node is removed after 2 seconds.
2126 // Sub element animations should be shorter than 2 seconds.
2127 var exiting = node.exit()
Simon Hunt44031102014-11-11 13:20:36 -08002128 .transition()
2129 .duration(2000)
Simon Huntea80eb42014-11-11 13:46:57 -08002130 .style('opacity', 0)
Simon Hunt99c13842014-11-06 18:23:12 -08002131 .remove();
Simon Huntea80eb42014-11-11 13:46:57 -08002132
2133 // host node exits....
2134 exiting.filter('.host').each(function (d) {
Simon Huntca867ac2014-11-28 18:07:35 -08002135 var node = d.el;
2136 node.select('use')
2137 .style('opacity', 0.5)
2138 .transition()
2139 .duration(800)
2140 .style('opacity', 0);
Simon Huntea80eb42014-11-11 13:46:57 -08002141
2142 node.select('text')
2143 .style('opacity', 0.5)
2144 .transition()
Simon Huntca867ac2014-11-28 18:07:35 -08002145 .duration(800)
Simon Huntea80eb42014-11-11 13:46:57 -08002146 .style('opacity', 0);
Simon Huntea80eb42014-11-11 13:46:57 -08002147
Thomas Vachuska89543292014-11-19 11:28:33 -08002148 node.select('circle')
2149 .style('stroke-fill', '#555')
2150 .style('fill', '#888')
Simon Huntea80eb42014-11-11 13:46:57 -08002151 .style('opacity', 0.5)
2152 .transition()
2153 .duration(1500)
2154 .attr('r', 0);
Simon Huntea80eb42014-11-11 13:46:57 -08002155 });
2156
Simon Huntca867ac2014-11-28 18:07:35 -08002157 // device node exits....
2158 exiting.filter('.device').each(function (d) {
2159 var node = d.el;
2160 node.select('use')
2161 .style('opacity', 0.5)
2162 .transition()
2163 .duration(800)
2164 .style('opacity', 0);
2165
2166 node.selectAll('rect')
2167 .style('stroke-fill', '#555')
2168 .style('fill', '#888')
2169 .style('opacity', 0.5);
2170 });
Simon Huntd6f5a272014-11-29 23:45:50 -08002171 fResume();
Simon Huntc7ee0662014-11-05 16:44:37 -08002172 }
2173
Simon Hunt95dad922014-11-24 09:43:31 -08002174 var dCol = {
2175 black: '#000',
2176 paleblue: '#acf',
2177 offwhite: '#ddd',
2178 midgrey: '#888',
2179 lightgrey: '#bbb',
2180 orange: '#f90'
2181 };
2182
Simon Huntb0ecfa52014-11-23 21:05:12 -08002183 // note: these are the device icon colors without affinity
Simon Hunt95dad922014-11-24 09:43:31 -08002184 var dColTheme = {
Simon Huntb0ecfa52014-11-23 21:05:12 -08002185 light: {
2186 online: {
Simon Hunt95dad922014-11-24 09:43:31 -08002187 glyph: dCol.black,
2188 rect: dCol.paleblue
Simon Huntb0ecfa52014-11-23 21:05:12 -08002189 },
2190 offline: {
Simon Hunt95dad922014-11-24 09:43:31 -08002191 glyph: dCol.midgrey,
2192 rect: dCol.lightgrey
Simon Huntb0ecfa52014-11-23 21:05:12 -08002193 }
2194 },
Simon Hunt95dad922014-11-24 09:43:31 -08002195 // TODO: theme
Simon Huntb0ecfa52014-11-23 21:05:12 -08002196 dark: {
2197 online: {
Simon Hunt95dad922014-11-24 09:43:31 -08002198 glyph: dCol.black,
2199 rect: dCol.paleblue
Simon Huntb0ecfa52014-11-23 21:05:12 -08002200 },
2201 offline: {
Simon Hunt95dad922014-11-24 09:43:31 -08002202 glyph: dCol.midgrey,
2203 rect: dCol.lightgrey
Simon Huntb0ecfa52014-11-23 21:05:12 -08002204 }
2205 }
2206 };
2207
2208 function devBaseColor(d) {
2209 var t = network.view.getTheme(),
2210 o = d.online ? 'online' : 'offline';
Simon Hunt95dad922014-11-24 09:43:31 -08002211 return dColTheme[t][o];
Simon Huntb0ecfa52014-11-23 21:05:12 -08002212 }
2213
2214 function setDeviceColor(d) {
2215 var o = d.online,
2216 s = d.el.classed('selected'),
2217 c = devBaseColor(d),
2218 a = instColor(d.master, o),
2219 g, r,
2220 icon = d.el.select('g.deviceIcon');
2221
2222 if (s) {
2223 g = c.glyph;
Simon Hunt95dad922014-11-24 09:43:31 -08002224 r = dColTheme.sel;
Simon Huntb0ecfa52014-11-23 21:05:12 -08002225 } else if (colorAffinity) {
2226 g = o ? a : c.glyph;
Simon Hunt95dad922014-11-24 09:43:31 -08002227 r = o ? dCol.offwhite : a;
Simon Huntb0ecfa52014-11-23 21:05:12 -08002228 } else {
2229 g = c.glyph;
2230 r = c.rect;
2231 }
2232
2233 icon.select('use')
2234 .style('fill', g);
2235 icon.select('rect')
2236 .style('fill', r);
2237 }
2238
Simon Huntc72967b2014-11-20 09:21:42 -08002239 function addDeviceIcon(node, box, noLabel, iid) {
2240 var cfg = config.icons.device,
2241 dx,
2242 dy,
2243 g;
2244
2245 if (noLabel) {
Simon Huntc72967b2014-11-20 09:21:42 -08002246 dx = -cfg.dim/2;
2247 dy = -cfg.dim/2;
2248 } else {
2249 box = adjustRectToFitText(node);
Simon Hunt395a70c2014-11-22 23:17:40 -08002250 dx = box.x + cfg.xoff;
2251 dy = box.y + cfg.yoff;
Simon Huntc72967b2014-11-20 09:21:42 -08002252 }
2253
Simon Hunteb1514d2014-11-20 09:57:29 -08002254 g = node.append('g')
2255 .attr('class', 'glyphIcon deviceIcon')
Simon Huntc72967b2014-11-20 09:21:42 -08002256 .attr('transform', translate(dx, dy));
2257
2258 g.append('rect').attr({
2259 x: 0,
2260 y: 0,
2261 rx: cfg.rx,
2262 width: cfg.dim,
2263 height: cfg.dim
2264 });
2265
2266 g.append('use').attr({
2267 'xlink:href': iid,
2268 width: cfg.dim,
2269 height: cfg.dim
2270 });
2271
Simon Huntc72967b2014-11-20 09:21:42 -08002272 }
2273
Simon Hunt7b403bc2014-11-22 19:01:00 -08002274 function find(key, array, tag) {
Simon Huntca867ac2014-11-28 18:07:35 -08002275 var _tag = tag || 'id',
Simon Hunt7b403bc2014-11-22 19:01:00 -08002276 idx, n, d;
2277 for (idx = 0, n = array.length; idx < n; idx++) {
2278 d = array[idx];
2279 if (d[_tag] === key) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08002280 return idx;
2281 }
2282 }
2283 return -1;
2284 }
2285
Simon Huntca867ac2014-11-28 18:07:35 -08002286 function removeLinkElement(d) {
2287 var idx = find(d.key, network.links, 'key'),
Simon Hunt8257f4c2014-11-16 19:34:54 -08002288 removed;
2289 if (idx >=0) {
2290 // remove from links array
2291 removed = network.links.splice(idx, 1);
2292 // remove from lookup cache
2293 delete network.lookup[removed[0].key];
2294 updateLinks();
Simon Huntd6f5a272014-11-29 23:45:50 -08002295 fResume();
Simon Hunt8257f4c2014-11-16 19:34:54 -08002296 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08002297 }
Simon Huntc7ee0662014-11-05 16:44:37 -08002298
Simon Huntca867ac2014-11-28 18:07:35 -08002299 function removeHostElement(d, upd) {
2300 var lu = network.lookup;
Simon Hunt44031102014-11-11 13:20:36 -08002301 // first, remove associated hostLink...
Simon Huntca867ac2014-11-28 18:07:35 -08002302 removeLinkElement(d.linkData);
2303
2304 // remove hostLink bindings
2305 delete lu[d.ingress];
2306 delete lu[d.egress];
Simon Hunt44031102014-11-11 13:20:36 -08002307
2308 // remove from lookup cache
Simon Huntca867ac2014-11-28 18:07:35 -08002309 delete lu[d.id];
Simon Hunt44031102014-11-11 13:20:36 -08002310 // remove from nodes array
Simon Huntca867ac2014-11-28 18:07:35 -08002311 var idx = find(d.id, network.nodes);
2312 network.nodes.splice(idx, 1);
2313 // remove from SVG
2314 // NOTE: upd is false if we were called from removeDeviceElement()
2315 if (upd) {
2316 updateNodes();
Simon Huntd6f5a272014-11-29 23:45:50 -08002317 fResume();
Simon Huntca867ac2014-11-28 18:07:35 -08002318 }
2319 }
2320
2321
2322 function removeDeviceElement(d) {
2323 var id = d.id;
2324 // first, remove associated hosts and links..
2325 findAttachedHosts(id).forEach(removeHostElement);
2326 findAttachedLinks(id).forEach(removeLinkElement);
2327
2328 // remove from lookup cache
2329 delete network.lookup[id];
2330 // remove from nodes array
2331 var idx = find(id, network.nodes);
Simon Hunt44031102014-11-11 13:20:36 -08002332 network.nodes.splice(idx, 1);
Simon Hunt233747b2014-12-04 12:01:58 -08002333
2334 if (!network.nodes.length) {
2335 showNoDevs(true);
2336 }
2337
Simon Hunt44031102014-11-11 13:20:36 -08002338 // remove from SVG
2339 updateNodes();
Simon Huntd6f5a272014-11-29 23:45:50 -08002340 fResume();
Simon Hunt44031102014-11-11 13:20:36 -08002341 }
2342
Simon Huntca867ac2014-11-28 18:07:35 -08002343 function findAttachedHosts(devId) {
2344 var hosts = [];
2345 network.nodes.forEach(function (d) {
2346 if (d.class === 'host' && d.cp.device === devId) {
2347 hosts.push(d);
2348 }
2349 });
2350 return hosts;
2351 }
2352
2353 function findAttachedLinks(devId) {
2354 var links = [];
2355 network.links.forEach(function (d) {
2356 if (d.source.id === devId || d.target.id === devId) {
2357 links.push(d);
2358 }
2359 });
2360 return links;
2361 }
Simon Hunt44031102014-11-11 13:20:36 -08002362
Simon Huntd6f5a272014-11-29 23:45:50 -08002363 function fResume() {
2364 if (!oblique) {
2365 network.force.resume();
2366 }
2367 }
Simon Huntc7ee0662014-11-05 16:44:37 -08002368
Simon Huntd6f5a272014-11-29 23:45:50 -08002369 function fStart() {
2370 if (!oblique) {
2371 network.force.start();
2372 }
2373 }
2374
2375 var tickStuff = {
2376 nodeAttr: {
2377 transform: function (d) { return translate(d.x, d.y); }
2378 },
2379 linkAttr: {
Simon Huntc7ee0662014-11-05 16:44:37 -08002380 x1: function (d) { return d.source.x; },
2381 y1: function (d) { return d.source.y; },
2382 x2: function (d) { return d.target.x; },
2383 y2: function (d) { return d.target.y; }
Simon Huntd6f5a272014-11-29 23:45:50 -08002384 },
2385 linkLabelAttr: {
2386 transform: function (d) {
2387 var lnk = findLinkById(d.key);
Simon Hunte2575b62014-11-18 15:25:53 -08002388
Simon Huntd6f5a272014-11-29 23:45:50 -08002389 if (lnk) {
2390 var parms = {
2391 x1: lnk.source.x,
2392 y1: lnk.source.y,
2393 x2: lnk.target.x,
2394 y2: lnk.target.y
2395 };
2396 return transformLabel(parms);
2397 }
Thomas Vachuska4731f122014-11-20 04:56:19 -08002398 }
Simon Huntd6f5a272014-11-29 23:45:50 -08002399 }
2400 };
2401
2402 function tick() {
2403 node.attr(tickStuff.nodeAttr);
2404 link.attr(tickStuff.linkAttr);
2405 linkLabel.attr(tickStuff.linkLabelAttr);
Simon Huntc7ee0662014-11-05 16:44:37 -08002406 }
Simon Hunt934c3ce2014-11-05 11:45:07 -08002407
2408 // ==============================
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002409 // Web-Socket for live data
2410
Thomas Vachuska12dfdc32014-11-29 16:03:12 -08002411 function findGuiSuccessor() {
2412 var idx = -1;
2413 onosOrder.forEach(function (d, i) {
2414 if (d.uiAttached) {
2415 idx = i;
2416 }
2417 });
2418
2419 for (var i = 0; i < onosOrder.length - 1; i++) {
2420 var ni = (idx + 1 + i) % onosOrder.length;
2421 if (onosOrder[ni].online) {
2422 return onosOrder[ni].ip;
2423 }
2424 }
2425 return null;
2426 }
2427
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002428 function webSockUrl() {
Thomas Vachuska12dfdc32014-11-29 16:03:12 -08002429 var url = document.location.toString()
2430 .replace(/\#.*/, '')
2431 .replace('http://', 'ws://')
2432 .replace('https://', 'wss://')
2433 .replace('index.html', config.webSockUrl);
2434 if (guiSuccessor) {
2435 url = url.replace(location.hostname, guiSuccessor);
2436 }
2437 return url;
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002438 }
2439
2440 webSock = {
2441 ws : null,
Thomas Vachuska12dfdc32014-11-29 16:03:12 -08002442 retries: 0,
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002443
2444 connect : function() {
2445 webSock.ws = new WebSocket(webSockUrl());
2446
2447 webSock.ws.onopen = function() {
Simon Hunt0c6d4192014-11-12 12:07:10 -08002448 noWebSock(false);
Thomas Vachuska47635c62014-11-22 01:21:36 -08002449 requestSummary();
Simon Huntb0ecfa52014-11-23 21:05:12 -08002450 showInstances();
Thomas Vachuska12dfdc32014-11-29 16:03:12 -08002451 webSock.retries = 0;
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002452 };
2453
2454 webSock.ws.onmessage = function(m) {
2455 if (m.data) {
Simon Huntbb282f52014-11-10 11:08:19 -08002456 wsTraceRx(m.data);
Thomas Vachuskad472c6e2014-11-07 19:11:05 -08002457 handleServerEvent(JSON.parse(m.data));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002458 }
2459 };
2460
2461 webSock.ws.onclose = function(m) {
2462 webSock.ws = null;
Thomas Vachuska12dfdc32014-11-29 16:03:12 -08002463 guiSuccessor = findGuiSuccessor();
2464 if (guiSuccessor && webSock.retries < onosOrder.length) {
2465 webSock.retries++;
2466 webSock.connect();
2467 } else {
2468 noWebSock(true);
2469 }
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002470 };
2471 },
2472
2473 send : function(text) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002474 if (text != null) {
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002475 webSock._send(text);
2476 }
2477 },
2478
2479 _send : function(message) {
2480 if (webSock.ws) {
2481 webSock.ws.send(message);
Simon Hunta255a2c2014-11-13 22:29:35 -08002482 } else if (config.useLiveData) {
Simon Hunt434cf142014-11-24 11:10:28 -08002483 console.warn('no web socket open', message);
Simon Hunta255a2c2014-11-13 22:29:35 -08002484 } else {
Simon Hunt434cf142014-11-24 11:10:28 -08002485 console.log('WS Send: ', message);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002486 }
2487 }
2488
2489 };
2490
Simon Hunt0c6d4192014-11-12 12:07:10 -08002491 function noWebSock(b) {
2492 mask.style('display',b ? 'block' : 'none');
2493 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002494
2495 function sendMessage(evType, payload) {
Simon Huntc1cc81c2014-11-29 14:59:01 -08002496 var p = payload || {},
2497 toSend = {
Simon Huntbb282f52014-11-10 11:08:19 -08002498 event: evType,
2499 sid: ++sid,
Simon Huntc1cc81c2014-11-29 14:59:01 -08002500 payload: p
Simon Huntbb282f52014-11-10 11:08:19 -08002501 },
2502 asText = JSON.stringify(toSend);
2503 wsTraceTx(asText);
2504 webSock.send(asText);
Simon Huntc76ae892014-11-18 17:31:51 -08002505
2506 // Temporary measure for debugging UI behavior ...
2507 if (!config.useLiveData) {
2508 handleTestSend(toSend);
2509 }
Simon Huntbb282f52014-11-10 11:08:19 -08002510 }
2511
2512 function wsTraceTx(msg) {
2513 wsTrace('tx', msg);
2514 }
2515 function wsTraceRx(msg) {
2516 wsTrace('rx', msg);
2517 }
2518 function wsTrace(rxtx, msg) {
Thomas Vachuska164fa5c2014-12-02 21:59:41 -08002519 // console.log('[' + rxtx + '] ' + msg);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002520 }
2521
Simon Huntc2465f42014-12-04 10:28:07 -08002522 function handleTestSend(msg) { }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002523
2524 // ==============================
2525 // Selection stuff
2526
2527 function selectObject(obj, el) {
2528 var n,
Simon Hunta1162d82014-12-03 14:34:43 -08002529 ev = d3.event.sourceEvent;
Simon Hunt01095ff2014-11-13 16:37:29 -08002530
Simon Hunta1162d82014-12-03 14:34:43 -08002531 // if the meta or alt key is pressed, we are panning/zooming, so ignore
2532 if (ev.metaKey || ev.altKey) {
Simon Hunt01095ff2014-11-13 16:37:29 -08002533 return;
2534 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002535
2536 if (el) {
2537 n = d3.select(el);
2538 } else {
2539 node.each(function(d) {
2540 if (d == obj) {
2541 n = d3.select(el = this);
2542 }
2543 });
2544 }
2545 if (!n) return;
2546
Simon Hunta1162d82014-12-03 14:34:43 -08002547 if (ev.shiftKey && n.classed('selected')) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002548 deselectObject(obj.id);
Simon Hunt61d04042014-11-11 17:27:16 -08002549 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002550 return;
2551 }
2552
Simon Hunta1162d82014-12-03 14:34:43 -08002553 if (!ev.shiftKey) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002554 deselectAll();
2555 }
2556
Simon Huntc31d5692014-11-12 13:27:18 -08002557 selections[obj.id] = { obj: obj, el: el };
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002558 selectOrder.push(obj.id);
2559
2560 n.classed('selected', true);
Simon Huntb0ecfa52014-11-23 21:05:12 -08002561 updateDeviceColors(obj);
Simon Hunt61d04042014-11-11 17:27:16 -08002562 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002563 }
2564
2565 function deselectObject(id) {
Simon Huntc31d5692014-11-12 13:27:18 -08002566 var obj = selections[id],
2567 idx;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002568 if (obj) {
2569 d3.select(obj.el).classed('selected', false);
Simon Hunt61d04042014-11-11 17:27:16 -08002570 delete selections[id];
Simon Huntc31d5692014-11-12 13:27:18 -08002571 idx = $.inArray(id, selectOrder);
2572 if (idx >= 0) {
2573 selectOrder.splice(idx, 1);
2574 }
Simon Huntb0ecfa52014-11-23 21:05:12 -08002575 updateDeviceColors(obj.obj);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002576 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002577 }
2578
2579 function deselectAll() {
2580 // deselect all nodes in the network...
2581 node.classed('selected', false);
2582 selections = {};
2583 selectOrder = [];
Simon Huntb0ecfa52014-11-23 21:05:12 -08002584 updateDeviceColors();
Simon Hunt61d04042014-11-11 17:27:16 -08002585 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002586 }
2587
Simon Huntb0ecfa52014-11-23 21:05:12 -08002588 function updateDeviceColors(d) {
2589 if (d) {
2590 setDeviceColor(d);
2591 } else {
2592 node.filter('.device').each(function (d) {
2593 setDeviceColor(d);
2594 });
2595 }
Thomas Vachuska47635c62014-11-22 01:21:36 -08002596 }
2597
Simon Hunt61d04042014-11-11 17:27:16 -08002598 // update the state of the detail pane, based on current selections
2599 function updateDetailPane() {
2600 var nSel = selectOrder.length;
2601 if (!nSel) {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002602 emptySelect();
Simon Hunt61d04042014-11-11 17:27:16 -08002603 } else if (nSel === 1) {
2604 singleSelect();
2605 } else {
2606 multiSelect();
2607 }
2608 }
2609
Thomas Vachuska9edca302014-11-22 17:06:42 -08002610 function emptySelect() {
Simon Hunt27d322d2014-11-28 10:45:43 -08002611 haveDetails = false;
Simon Hunt06811b72014-11-25 18:54:48 -08002612 hideDetailPane();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002613 cancelTraffic();
2614 }
2615
Simon Hunt61d04042014-11-11 17:27:16 -08002616 function singleSelect() {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002617 // NOTE: detail is shown from showDetails event callback
Simon Hunt61d04042014-11-11 17:27:16 -08002618 requestDetails();
Thomas Vachuska164fa5c2014-12-02 21:59:41 -08002619 cancelTraffic();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002620 requestTrafficForMode();
Simon Hunt61d04042014-11-11 17:27:16 -08002621 }
2622
2623 function multiSelect() {
Simon Hunt27d322d2014-11-28 10:45:43 -08002624 haveDetails = true;
Simon Huntb53e0682014-11-12 13:32:01 -08002625 populateMultiSelect();
Thomas Vachuska164fa5c2014-12-02 21:59:41 -08002626 cancelTraffic();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002627 requestTrafficForMode();
Simon Huntb53e0682014-11-12 13:32:01 -08002628 }
2629
2630 function addSep(tbody) {
2631 var tr = tbody.append('tr');
2632 $('<hr>').appendTo(tr.append('td').attr('colspan', 2));
2633 }
2634
2635 function addProp(tbody, label, value) {
2636 var tr = tbody.append('tr');
2637
2638 tr.append('td')
2639 .attr('class', 'label')
2640 .text(label + ' :');
2641
2642 tr.append('td')
2643 .attr('class', 'value')
2644 .text(value);
2645 }
2646
2647 function populateMultiSelect() {
2648 detailPane.empty();
2649
Simon Hunta3dd9572014-11-20 15:22:41 -08002650 var title = detailPane.append('h3'),
2651 table = detailPane.append('table'),
2652 tbody = table.append('tbody');
Simon Huntb53e0682014-11-12 13:32:01 -08002653
Thomas Vachuska4731f122014-11-20 04:56:19 -08002654 title.text('Selected Nodes');
Simon Huntb53e0682014-11-12 13:32:01 -08002655
2656 selectOrder.forEach(function (d, i) {
2657 addProp(tbody, i+1, d);
2658 });
Simon Huntd72bc702014-11-13 18:38:04 -08002659
2660 addMultiSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08002661 }
2662
Thomas Vachuska47635c62014-11-22 01:21:36 -08002663 function populateSummary(data) {
2664 summaryPane.empty();
2665
2666 var svg = summaryPane.append('svg'),
2667 iid = iconGlyphUrl(data);
2668
2669 var title = summaryPane.append('h2'),
2670 table = summaryPane.append('table'),
2671 tbody = table.append('tbody');
2672
2673 appendGlyph(svg, 0, 0, 40, iid);
2674
2675 svg.append('use')
2676 .attr({
2677 class: 'birdBadge',
2678 transform: translate(8,12),
2679 'xlink:href': '#bird',
2680 width: 24,
2681 height: 24,
2682 fill: '#fff'
2683 });
2684
2685 title.text('ONOS Summary');
2686
2687 data.propOrder.forEach(function(p) {
2688 if (p === '-') {
2689 addSep(tbody);
2690 } else {
2691 addProp(tbody, p, data.props[p]);
2692 }
2693 });
2694 }
2695
Simon Hunt61d04042014-11-11 17:27:16 -08002696 function populateDetails(data) {
2697 detailPane.empty();
2698
Simon Hunta6a9fe72014-11-20 11:17:12 -08002699 var svg = detailPane.append('svg'),
2700 iid = iconGlyphUrl(data);
2701
Simon Hunta3dd9572014-11-20 15:22:41 -08002702 var title = detailPane.append('h2'),
2703 table = detailPane.append('table'),
2704 tbody = table.append('tbody');
Simon Hunt61d04042014-11-11 17:27:16 -08002705
Simon Hunta6a9fe72014-11-20 11:17:12 -08002706 appendGlyph(svg, 0, 0, 40, iid);
2707 title.text(data.id);
Simon Hunt61d04042014-11-11 17:27:16 -08002708
2709 data.propOrder.forEach(function(p) {
2710 if (p === '-') {
2711 addSep(tbody);
2712 } else {
2713 addProp(tbody, p, data.props[p]);
2714 }
2715 });
Simon Huntd72bc702014-11-13 18:38:04 -08002716
Thomas Vachuska4731f122014-11-20 04:56:19 -08002717 addSingleSelectActions(data);
Simon Hunt61d04042014-11-11 17:27:16 -08002718 }
2719
Thomas Vachuska4731f122014-11-20 04:56:19 -08002720 function addSingleSelectActions(data) {
Simon Huntd72bc702014-11-13 18:38:04 -08002721 detailPane.append('hr');
2722 // always want to allow 'show traffic'
Thomas Vachuska164fa5c2014-12-02 21:59:41 -08002723 addAction(detailPane, 'Show Related Traffic', showRelatedIntentsAction);
Thomas Vachuska4731f122014-11-20 04:56:19 -08002724
2725 if (data.type === 'switch') {
2726 addAction(detailPane, 'Show Device Flows', showDeviceLinkFlowsAction);
2727 }
Simon Huntd72bc702014-11-13 18:38:04 -08002728 }
2729
2730 function addMultiSelectActions() {
2731 detailPane.append('hr');
2732 // always want to allow 'show traffic'
Thomas Vachuska164fa5c2014-12-02 21:59:41 -08002733 addAction(detailPane, 'Show Related Traffic', showRelatedIntentsAction);
Simon Huntd72bc702014-11-13 18:38:04 -08002734 // if exactly two hosts are selected, also want 'add host intent'
2735 if (nSel() === 2 && allSelectionsClass('host')) {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002736 addAction(detailPane, 'Create Host-to-Host Flow', addHostIntentAction);
2737 } else if (nSel() >= 2 && allSelectionsClass('host')) {
2738 addAction(detailPane, 'Create Multi-Source Flow', addMultiSourceIntentAction);
Simon Huntd72bc702014-11-13 18:38:04 -08002739 }
2740 }
2741
Simon Hunta5e89142014-11-14 07:00:33 -08002742 function addAction(panel, text, cb) {
2743 panel.append('div')
Simon Huntd72bc702014-11-13 18:38:04 -08002744 .classed('actionBtn', true)
2745 .text(text)
2746 .on('click', cb);
2747 }
2748
2749
Simon Hunt3c5ca542014-11-29 14:11:43 -08002750 // === Pan and Zoom behaviors...
2751
2752 function panZoom(translate, scale) {
2753 panZoomContainer.attr('transform',
2754 'translate(' + translate + ')scale(' + scale + ')');
Paul Greysonfcba0e82014-11-13 10:21:16 -08002755 // keep the map lines constant width while zooming
Simon Hunt3c5ca542014-11-29 14:11:43 -08002756 bgImg.style('stroke-width', 2.0 / scale + 'px');
Paul Greysonfcba0e82014-11-13 10:21:16 -08002757 }
2758
Simon Hunt3c5ca542014-11-29 14:11:43 -08002759 function resetPanZoom() {
2760 panZoom([0,0], 1);
2761 zoom.translate([0,0]).scale(1);
Paul Greysonfcba0e82014-11-13 10:21:16 -08002762 }
2763
Simon Hunt3c5ca542014-11-29 14:11:43 -08002764 function setupPanZoom() {
Paul Greysonfcba0e82014-11-13 10:21:16 -08002765 function zoomed() {
Simon Hunta1162d82014-12-03 14:34:43 -08002766 var ev = d3.event.sourceEvent;
2767 // pan/zoom active when meta or alt key is pressed...
2768 if (ev.metaKey || ev.altKey) {
Simon Hunt3c5ca542014-11-29 14:11:43 -08002769 panZoom(d3.event.translate, d3.event.scale);
Paul Greysonfcba0e82014-11-13 10:21:16 -08002770 }
2771 }
2772
2773 zoom = d3.behavior.zoom()
2774 .translate([0, 0])
2775 .scale(1)
Simon Hunt1b18aa52014-11-29 17:57:55 -08002776 .scaleExtent([0.25, 10])
Paul Greysonfcba0e82014-11-13 10:21:16 -08002777 .on("zoom", zoomed);
2778
2779 svg.call(zoom);
2780 }
2781
Simon Hunt233747b2014-12-04 12:01:58 -08002782
2783 function setupNoDevices() {
2784 var g = noDevices.append('g');
2785 appendBadge(g, 0, 0, 100, '#bird', 'noDevsBird');
2786 var text = g.append('text')
2787 .text('No devices are connected')
2788 .attr({ x: 120, y: 80});
2789 }
2790
2791 function repositionNoDevices() {
2792 var g = noDevices.select('g');
2793 var box = g.node().getBBox();
2794 box.x -= box.width/2;
2795 box.y -= box.height/2;
2796 g.attr('transform', translate(box.x, box.y));
2797 }
2798
2799
Simon Hunt61d04042014-11-11 17:27:16 -08002800 // ==============================
2801 // Test harness code
Simon Hunt56d51852014-11-09 13:03:35 -08002802
2803 function prepareScenario(view, ctx, dbg) {
2804 var sc = scenario,
2805 urlSc = sc.evDir + ctx + sc.evScenario;
2806
2807 if (!ctx) {
2808 view.alert("No scenario specified (null ctx)");
2809 return;
2810 }
2811
2812 sc.view = view;
2813 sc.ctx = ctx;
2814 sc.debug = dbg;
2815 sc.evNumber = 0;
2816
2817 d3.json(urlSc, function(err, data) {
Simon Huntbb282f52014-11-10 11:08:19 -08002818 var p = data && data.params || {},
2819 desc = data && data.description || null,
Simon Huntfc274c92014-11-11 11:05:46 -08002820 intro = data && data.title;
Simon Huntbb282f52014-11-10 11:08:19 -08002821
Simon Hunt56d51852014-11-09 13:03:35 -08002822 if (err) {
2823 view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err);
2824 } else {
2825 sc.params = p;
Simon Huntbb282f52014-11-10 11:08:19 -08002826 if (desc) {
2827 intro += '\n\n ' + desc.join('\n ');
2828 }
2829 view.alert(intro);
Simon Hunt56d51852014-11-09 13:03:35 -08002830 }
2831 });
2832
2833 }
2834
Simon Hunt9f1bced2014-12-02 14:36:39 -08002835 function setupDefs(svg) {
Simon Hunt7fa116d2014-11-17 14:16:55 -08002836 var defs = svg.append('defs');
Simon Hunt9f1bced2014-12-02 14:36:39 -08002837 gly.loadDefs(defs);
2838 d3u.loadGlow(defs);
Simon Hunt7fa116d2014-11-17 14:16:55 -08002839 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002840
Simon Hunt395a70c2014-11-22 23:17:40 -08002841 function sendUpdateMeta(d, store) {
2842 var metaUi = {},
2843 ll;
2844
2845 if (store) {
Simon Hunt1b18aa52014-11-29 17:57:55 -08002846 ll = geoMapProj.invert([d.x, d.y]);
Simon Hunt62c47542014-11-22 22:16:32 -08002847 metaUi = {
2848 x: d.x,
2849 y: d.y,
2850 lng: ll[0],
2851 lat: ll[1]
2852 };
Simon Hunt395a70c2014-11-22 23:17:40 -08002853 }
Simon Hunt62c47542014-11-22 22:16:32 -08002854 d.metaUi = metaUi;
2855 sendMessage('updateMeta', {
2856 id: d.id,
2857 'class': d.class,
Simon Huntc1cc81c2014-11-29 14:59:01 -08002858 memento: metaUi
Simon Hunt62c47542014-11-22 22:16:32 -08002859 });
2860 }
2861
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002862 // ==============================
Simon Hunt142d0032014-11-04 20:13:09 -08002863 // View life-cycle callbacks
Simon Hunt195cb382014-11-03 17:50:51 -08002864
Simon Hunta2994cc2014-12-02 14:19:15 -08002865 function init(view, ctx, flags) {
Simon Hunt142d0032014-11-04 20:13:09 -08002866 var w = view.width(),
2867 h = view.height(),
Simon Hunt233747b2014-12-04 12:01:58 -08002868 logSize = config.logicalSize,
Simon Hunt1b18aa52014-11-29 17:57:55 -08002869 fcfg = config.force;
Simon Hunt195cb382014-11-03 17:50:51 -08002870
Simon Hunt142d0032014-11-04 20:13:09 -08002871 // NOTE: view.$div is a D3 selection of the view's div
Simon Hunt233747b2014-12-04 12:01:58 -08002872 var viewBox = '0 0 ' + logSize + ' ' + logSize;
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002873 svg = view.$div.append('svg').attr('viewBox', viewBox);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002874 setSize(svg, view);
2875
Simon Hunt9f1bced2014-12-02 14:36:39 -08002876 // load glyphs, filters, and other definitions...
2877 setupDefs(svg);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002878
Simon Hunt3c5ca542014-11-29 14:11:43 -08002879 panZoomContainer = svg.append('g').attr('id', 'panZoomContainer');
2880 setupPanZoom();
Paul Greysonfcba0e82014-11-13 10:21:16 -08002881
Simon Hunt233747b2014-12-04 12:01:58 -08002882 noDevices = svg.append('g')
2883 .attr('class', 'noDevsLayer')
2884 .attr('transform', translate(logSize/2, logSize/2));
2885 setupNoDevices();
2886
Simon Huntc7ee0662014-11-05 16:44:37 -08002887 // group for the topology
Simon Hunt3c5ca542014-11-29 14:11:43 -08002888 topoG = panZoomContainer.append('g')
Simon Hunt1b18aa52014-11-29 17:57:55 -08002889 .attr('id', 'topo-G');
Simon Huntc7ee0662014-11-05 16:44:37 -08002890
Simon Hunte2575b62014-11-18 15:25:53 -08002891 // subgroups for links, link labels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002892 linkG = topoG.append('g').attr('id', 'links');
Simon Hunte2575b62014-11-18 15:25:53 -08002893 linkLabelG = topoG.append('g').attr('id', 'linkLabels');
Simon Huntc7ee0662014-11-05 16:44:37 -08002894 nodeG = topoG.append('g').attr('id', 'nodes');
2895
Simon Hunte2575b62014-11-18 15:25:53 -08002896 // selection of links, linkLabels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002897 link = linkG.selectAll('.link');
Simon Hunte2575b62014-11-18 15:25:53 -08002898 linkLabel = linkLabelG.selectAll('.linkLabel');
Simon Huntc7ee0662014-11-05 16:44:37 -08002899 node = nodeG.selectAll('.node');
2900
Simon Hunt7cd48f32014-11-09 23:42:50 -08002901 function chrg(d) {
2902 return fcfg.charge[d.class] || -12000;
2903 }
Simon Hunt99c13842014-11-06 18:23:12 -08002904 function ldist(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002905 return fcfg.linkDistance[d.type] || 50;
Simon Hunt99c13842014-11-06 18:23:12 -08002906 }
2907 function lstrg(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002908 // 0.0 - 1.0
2909 return fcfg.linkStrength[d.type] || 1.0;
Simon Hunt99c13842014-11-06 18:23:12 -08002910 }
2911
Simon Hunt1a9eff92014-11-07 11:06:34 -08002912 function selectCb(d, self) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002913 selectObject(d, self);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002914 }
2915
2916 function atDragEnd(d, self) {
Simon Hunt56d51852014-11-09 13:03:35 -08002917 // once we've finished moving, pin the node in position
2918 d.fixed = true;
2919 d3.select(self).classed('fixed', true);
2920 if (config.useLiveData) {
Simon Hunt395a70c2014-11-22 23:17:40 -08002921 sendUpdateMeta(d, true);
Simon Hunta255a2c2014-11-13 22:29:35 -08002922 } else {
2923 console.log('Moving node ' + d.id + ' to [' + d.x + ',' + d.y + ']');
Simon Hunt1a9eff92014-11-07 11:06:34 -08002924 }
2925 }
2926
Simon Hunt6e18fe32014-11-29 13:35:41 -08002927 // predicate that indicates when dragging is active
2928 function dragEnabled() {
Simon Hunta1162d82014-12-03 14:34:43 -08002929 var ev = d3.event.sourceEvent;
2930 // nodeLock means we aren't allowing nodes to be dragged...
2931 // meta or alt key pressed means we are zooming/panning...
2932 return !nodeLock && !(ev.metaKey || ev.altKey);
Simon Huntc2367d52014-11-29 19:30:23 -08002933 }
2934
2935 // predicate that indicates when clicking is active
2936 function clickEnabled() {
2937 return true;
Simon Hunt6e18fe32014-11-29 13:35:41 -08002938 }
2939
Simon Huntc7ee0662014-11-05 16:44:37 -08002940 // set up the force layout
2941 network.force = d3.layout.force()
Simon Hunt1b18aa52014-11-29 17:57:55 -08002942 .size([w, h])
Simon Huntc7ee0662014-11-05 16:44:37 -08002943 .nodes(network.nodes)
2944 .links(network.links)
Simon Hunt7cd48f32014-11-09 23:42:50 -08002945 .gravity(0.4)
2946 .friction(0.7)
2947 .charge(chrg)
Simon Hunt99c13842014-11-06 18:23:12 -08002948 .linkDistance(ldist)
2949 .linkStrength(lstrg)
Simon Huntc7ee0662014-11-05 16:44:37 -08002950 .on('tick', tick);
Simon Hunt195cb382014-11-03 17:50:51 -08002951
Simon Hunt01095ff2014-11-13 16:37:29 -08002952 network.drag = d3u.createDragBehavior(network.force,
Simon Huntc2367d52014-11-29 19:30:23 -08002953 selectCb, atDragEnd, dragEnabled, clickEnabled);
Simon Hunt6e18fe32014-11-29 13:35:41 -08002954
Simon Hunt0c6d4192014-11-12 12:07:10 -08002955
2956 // create mask layer for when we lose connection to server.
Simon Hunta5e89142014-11-14 07:00:33 -08002957 // TODO: this should be part of the framework
Simon Hunt6e18fe32014-11-29 13:35:41 -08002958
2959 function para(sel, text) {
2960 sel.append('p').text(text);
2961 }
2962
Simon Hunt0c6d4192014-11-12 12:07:10 -08002963 mask = view.$div.append('div').attr('id','topo-mask');
2964 para(mask, 'Oops!');
2965 para(mask, 'Web-socket connection to server closed...');
2966 para(mask, 'Try refreshing the page.');
Simon Hunt12ce12e2014-11-15 21:13:19 -08002967
2968 mask.append('svg')
2969 .attr({
2970 id: 'mask-bird',
2971 width: w,
2972 height: h
2973 })
2974 .append('g')
2975 .attr('transform', birdTranslate(w, h))
2976 .style('opacity', 0.3)
2977 .append('use')
2978 .attr({
2979 'xlink:href': '#bird',
2980 width: config.birdDim,
2981 height: config.birdDim,
2982 fill: '#111'
Thomas Vachuska89543292014-11-19 11:28:33 -08002983 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08002984 }
Simon Hunt195cb382014-11-03 17:50:51 -08002985
Simon Hunt01095ff2014-11-13 16:37:29 -08002986
Simon Hunt56d51852014-11-09 13:03:35 -08002987 function load(view, ctx, flags) {
Simon Huntf67722a2014-11-10 09:32:06 -08002988 // resize, in case the window was resized while we were not loaded
2989 resize(view, ctx, flags);
2990
Simon Hunt99c13842014-11-06 18:23:12 -08002991 // cache the view token, so network topo functions can access it
2992 network.view = view;
Simon Hunt56d51852014-11-09 13:03:35 -08002993 config.useLiveData = !flags.local;
2994
2995 if (!config.useLiveData) {
2996 prepareScenario(view, ctx, flags.debug);
2997 }
Simon Hunt99c13842014-11-06 18:23:12 -08002998
2999 // set our radio buttons and key bindings
Simon Hunt9462e8c2014-11-14 17:28:09 -08003000 layerBtnSet = view.setRadio(layerButtons);
Simon Hunt934c3ce2014-11-05 11:45:07 -08003001 view.setKeys(keyDispatch);
Simon Hunt87514342014-11-24 16:41:27 -08003002 view.setGestures(gestures);
Simon Hunt195cb382014-11-03 17:50:51 -08003003
Simon Huntd3b7d512014-11-12 15:48:41 -08003004 // Load map data asynchronously; complete startup after that..
3005 loadGeoJsonData();
Simon Hunta255a2c2014-11-13 22:29:35 -08003006 }
3007
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08003008 function startAntTimer() {
Thomas Vachuskab7e40642014-12-03 11:16:06 -08003009 // Note: disabled until traffic can be allotted to intents properly
3010 if (false && !antTimer) {
Thomas Vachuska4731f122014-11-20 04:56:19 -08003011 var pulses = [5, 3, 1.2, 3],
3012 pulse = 0;
3013 antTimer = setInterval(function () {
3014 pulse = pulse + 1;
3015 pulse = pulse === pulses.length ? 0 : pulse;
3016 d3.selectAll('.animated').style('stroke-width', pulses[pulse]);
3017 }, 200);
3018 }
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08003019 }
3020
3021 function stopAntTimer() {
Simon Hunta255a2c2014-11-13 22:29:35 -08003022 if (antTimer) {
3023 clearInterval(antTimer);
3024 antTimer = null;
3025 }
Simon Huntd3b7d512014-11-12 15:48:41 -08003026 }
3027
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08003028 function unload(view, ctx, flags) {
3029 stopAntTimer();
3030 }
3031
Thomas Vachuska5f399652015-02-23 15:17:36 -08003032 var geoJsonUrl = 'continental_us.json',
Simon Huntd3b7d512014-11-12 15:48:41 -08003033 geoJson;
3034
3035 function loadGeoJsonData() {
3036 d3.json(geoJsonUrl, function (err, data) {
3037 if (err) {
Simon Huntc2465f42014-12-04 10:28:07 -08003038 console.error('failed to load Map data', err);
Simon Huntd3b7d512014-11-12 15:48:41 -08003039 } else {
3040 geoJson = data;
3041 loadGeoMap();
3042 }
3043
Simon Hunt233747b2014-12-04 12:01:58 -08003044 repositionNoDevices();
3045 showNoDevs(true);
3046
Simon Huntd3b7d512014-11-12 15:48:41 -08003047 // finally, connect to the server...
3048 if (config.useLiveData) {
3049 webSock.connect();
3050 }
3051 });
3052 }
3053
Simon Hunt1b18aa52014-11-29 17:57:55 -08003054 function setProjForView(path, topoData) {
3055 var dim = config.logicalSize;
3056
3057 // start with unit scale, no translation..
3058 geoMapProj.scale(1).translate([0, 0]);
3059
3060 // figure out dimensions of map data..
3061 var b = path.bounds(topoData),
3062 x1 = b[0][0],
3063 y1 = b[0][1],
3064 x2 = b[1][0],
3065 y2 = b[1][1],
3066 dx = x2 - x1,
3067 dy = y2 - y1,
3068 x = (x1 + x2) / 2,
3069 y = (y1 + y2) / 2;
3070
3071 // size map to 95% of minimum dimension to fill space..
3072 var s = .95 / Math.min(dx / dim, dy / dim);
3073 var t = [dim / 2 - s * x, dim / 2 - s * y];
3074
3075 // set new scale, translation on the projection..
3076 geoMapProj.scale(s).translate(t);
3077 }
3078
Simon Huntd3b7d512014-11-12 15:48:41 -08003079 function loadGeoMap() {
3080 fnTrace('loadGeoMap', geoJsonUrl);
Simon Huntd3b7d512014-11-12 15:48:41 -08003081
Paul Greyson6cb8ca02014-11-12 18:09:02 -08003082 // extracts the topojson data into geocoordinate-based geometry
3083 var topoData = topojson.feature(geoJson, geoJson.objects.states);
Simon Huntd3b7d512014-11-12 15:48:41 -08003084
Paul Greyson6cb8ca02014-11-12 18:09:02 -08003085 // see: http://bl.ocks.org/mbostock/4707858
Simon Hunt1b18aa52014-11-29 17:57:55 -08003086 geoMapProj = d3.geo.mercator();
3087 var path = d3.geo.path().projection(geoMapProj);
Simon Huntd3b7d512014-11-12 15:48:41 -08003088
Simon Hunt1b18aa52014-11-29 17:57:55 -08003089 setProjForView(path, topoData);
Paul Greyson6cb8ca02014-11-12 18:09:02 -08003090
Simon Hunt3c5ca542014-11-29 14:11:43 -08003091 bgImg = panZoomContainer.insert("g", '#topo-G');
Thomas Vachuska89543292014-11-19 11:28:33 -08003092 bgImg.attr('id', 'map').selectAll('path')
3093 .data(topoData.features)
3094 .enter()
3095 .append('path')
3096 .attr('d', path);
Simon Hunt195cb382014-11-03 17:50:51 -08003097 }
3098
Simon Huntf67722a2014-11-10 09:32:06 -08003099 function resize(view, ctx, flags) {
Simon Hunt12ce12e2014-11-15 21:13:19 -08003100 var w = view.width(),
3101 h = view.height();
3102
Simon Hunt934c3ce2014-11-05 11:45:07 -08003103 setSize(svg, view);
Simon Hunt12ce12e2014-11-15 21:13:19 -08003104
3105 d3.select('#mask-bird').attr({ width: w, height: h})
3106 .select('g').attr('transform', birdTranslate(w, h));
Simon Hunt142d0032014-11-04 20:13:09 -08003107 }
3108
Simon Hunt8f40cce2014-11-23 15:57:30 -08003109 function theme(view, ctx, flags) {
3110 updateInstances();
Simon Huntb0ecfa52014-11-23 21:05:12 -08003111 updateDeviceColors();
Simon Hunt8f40cce2014-11-23 15:57:30 -08003112 }
3113
Simon Hunt12ce12e2014-11-15 21:13:19 -08003114 function birdTranslate(w, h) {
3115 var bdim = config.birdDim;
3116 return 'translate('+((w-bdim)*.4)+','+((h-bdim)*.1)+')';
3117 }
Simon Hunt142d0032014-11-04 20:13:09 -08003118
Simon Hunt06811b72014-11-25 18:54:48 -08003119 function isF(f) { return $.isFunction(f) ? f : null; }
3120 function noop() {}
3121
3122 function augmentDetailPane() {
3123 var dp = detailPane;
3124 dp.ypos = { up: 64, down: 320, current: 320};
3125
3126 dp._move = function (y, cb) {
3127 var endCb = isF(cb) || noop,
3128 yp = dp.ypos;
3129 if (yp.current !== y) {
3130 yp.current = y;
3131 dp.el.transition().duration(300)
3132 .each('end', endCb)
3133 .style('top', yp.current + 'px');
3134 } else {
3135 endCb();
3136 }
3137 };
3138
3139 dp.down = function (cb) { dp._move(dp.ypos.down, cb); };
3140 dp.up = function (cb) { dp._move(dp.ypos.up, cb); };
3141 }
3142
Simon Hunt142d0032014-11-04 20:13:09 -08003143 // ==============================
3144 // View registration
Simon Hunt195cb382014-11-03 17:50:51 -08003145
Simon Hunt25248912014-11-04 11:25:48 -08003146 onos.ui.addView('topo', {
Simon Hunta2994cc2014-12-02 14:19:15 -08003147 init: init,
Simon Hunt142d0032014-11-04 20:13:09 -08003148 load: load,
Simon Hunta255a2c2014-11-13 22:29:35 -08003149 unload: unload,
Simon Hunt8f40cce2014-11-23 15:57:30 -08003150 resize: resize,
3151 theme: theme
Simon Hunt195cb382014-11-03 17:50:51 -08003152 });
3153
Thomas Vachuska47635c62014-11-22 01:21:36 -08003154 summaryPane = onos.ui.addFloatingPanel('topo-summary');
Simon Hunt61d04042014-11-11 17:27:16 -08003155 detailPane = onos.ui.addFloatingPanel('topo-detail');
Simon Hunt06811b72014-11-25 18:54:48 -08003156 augmentDetailPane();
Simon Hunta5e89142014-11-14 07:00:33 -08003157 oiBox = onos.ui.addFloatingPanel('topo-oibox', 'TL');
Simon Huntb82f6902014-11-22 11:53:15 -08003158 oiBox.width(20);
Simon Hunt61d04042014-11-11 17:27:16 -08003159
Simon Hunt195cb382014-11-03 17:50:51 -08003160}(ONOS));