blob: c1a64f4758f2710b996e0c9b65e97f1bd5909185 [file] [log] [blame]
Bri Prebilic Cole7c92a3d2015-01-09 16:50:03 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Bri Prebilic Cole7c92a3d2015-01-09 16:50:03 -08003 *
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/*
Bri Prebilic Coleaa0f0882015-02-04 15:27:55 -080018 ONOS GUI -- Device View Module
Bri Prebilic Cole7c92a3d2015-01-09 16:50:03 -080019 */
20
21(function () {
22 'use strict';
23
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070024 // injected refs
Simon Huntd5579252015-10-06 15:09:14 -070025 var $log, $scope, $loc, fs, mast, ps, wss, is, ns, ks;
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070026
27 // internal state
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070028 var detailsPanel,
Simon Huntd766c312015-10-06 11:46:32 -070029 pStartY,
30 pHeight,
31 top,
32 bottom,
33 iconDiv,
34 wSize,
35 editingName = false;
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070036
37 // constants
Simon Hunt986b92f2016-06-03 15:46:59 -070038 var topPdg = 28,
Bri Prebilic Cole45069382015-04-14 15:21:38 -070039 ctnrPdg = 24,
Bri Prebilic Cole9cf1a8d2015-04-21 13:15:29 -070040 scrollSize = 17,
Bri Prebilic Cole45069382015-04-14 15:21:38 -070041 portsTblPdg = 50,
42
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070043 pName = 'device-details-panel',
44 detailsReq = 'deviceDetailsRequest',
45 detailsResp = 'deviceDetailsResponse',
Simon Huntd5579252015-10-06 15:09:14 -070046 nameChangeReq = 'deviceNameChangeRequest',
47 nameChangeResp = 'deviceNameChangeResponse',
Bri Prebilic Cole45069382015-04-14 15:21:38 -070048
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070049 propOrder = [
Simon Huntd5579252015-10-06 15:09:14 -070050 'id', 'type', 'masterid', 'chassisid',
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070051 'mfr', 'hw', 'sw', 'protocol', 'serial'
52 ],
53 friendlyProps = [
Simon Huntd5579252015-10-06 15:09:14 -070054 'URI', 'Type', 'Master ID', 'Chassis ID',
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070055 'Vendor', 'H/W Version', 'S/W Version', 'Protocol', 'Serial #'
56 ],
57 portCols = [
Thomas Vachuskab52a0142015-04-21 17:48:15 -070058 'enabled', 'id', 'speed', 'type', 'elinks_dest', 'name'
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070059 ],
60 friendlyPortCols = [
Thomas Vachuskab52a0142015-04-21 17:48:15 -070061 'Enabled', 'ID', 'Speed', 'Type', 'Egress Links', 'Name'
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070062 ];
63
Bri Prebilic Cole9dcaea52015-07-21 14:39:48 -070064 function closePanel() {
65 if (detailsPanel.isVisible()) {
66 $scope.selId = null;
67 detailsPanel.hide();
Simon Huntd766c312015-10-06 11:46:32 -070068 return true;
Bri Prebilic Cole9dcaea52015-07-21 14:39:48 -070069 }
Simon Huntd766c312015-10-06 11:46:32 -070070 return false;
Bri Prebilic Cole9dcaea52015-07-21 14:39:48 -070071 }
72
Bri Prebilic Coleb699a162015-04-13 12:01:39 -070073 function addCloseBtn(div) {
Simon Hunt986b92f2016-06-03 15:46:59 -070074 is.loadEmbeddedIcon(div, 'close', 20);
Bri Prebilic Cole9dcaea52015-07-21 14:39:48 -070075 div.on('click', closePanel);
Bri Prebilic Coleb699a162015-04-13 12:01:39 -070076 }
77
Simon Huntd5579252015-10-06 15:09:14 -070078 function exitEditMode(nameH2, name) {
79 nameH2.html(name);
Simon Hunt986b92f2016-06-03 15:46:59 -070080 nameH2.classed('editable clickable', true);
Simon Huntd5579252015-10-06 15:09:14 -070081 editingName = false;
82 ks.enableGlobalKeys(true);
Simon Huntd766c312015-10-06 11:46:32 -070083 }
84
85 function editNameSave() {
Simon Huntd5579252015-10-06 15:09:14 -070086 var nameH2 = top.select('h2'),
87 id = $scope.panelData.id,
88 val,
Simon Huntd766c312015-10-06 11:46:32 -070089 newVal;
Simon Huntd5579252015-10-06 15:09:14 -070090
Simon Huntd766c312015-10-06 11:46:32 -070091 if (editingName) {
Simon Huntd5579252015-10-06 15:09:14 -070092 val = nameH2.select('input').property('value').trim();
93 newVal = val || id;
Simon Huntd766c312015-10-06 11:46:32 -070094
Simon Huntd5579252015-10-06 15:09:14 -070095 exitEditMode(nameH2, newVal);
Simon Huntd766c312015-10-06 11:46:32 -070096 $scope.panelData.name = newVal;
Simon Huntd5579252015-10-06 15:09:14 -070097 wss.sendEvent(nameChangeReq, { id: id, name: val });
Simon Huntd766c312015-10-06 11:46:32 -070098 }
99 }
100
101 function editNameCancel() {
Simon Huntd766c312015-10-06 11:46:32 -0700102 if (editingName) {
Simon Huntd5579252015-10-06 15:09:14 -0700103 exitEditMode(top.select('h2'), $scope.panelData.name);
Simon Huntd766c312015-10-06 11:46:32 -0700104 return true;
105 }
106 return false;
107 }
108
109 function editName() {
Simon Huntd5579252015-10-06 15:09:14 -0700110 var nameH2 = top.select('h2'),
111 tf, el;
112
Simon Huntd766c312015-10-06 11:46:32 -0700113 if (!editingName) {
Simon Hunt986b92f2016-06-03 15:46:59 -0700114 nameH2.classed('editable clickable', false);
Simon Huntd5579252015-10-06 15:09:14 -0700115 nameH2.html('');
116 tf = nameH2.append('input').classed('name-input', true)
Simon Huntd766c312015-10-06 11:46:32 -0700117 .attr('type', 'text')
118 .attr('value', $scope.panelData.name);
Simon Huntd5579252015-10-06 15:09:14 -0700119 el = tf[0][0];
120 el.focus();
121 el.select();
122 editingName = true;
123 ks.enableGlobalKeys(false);
Simon Huntd766c312015-10-06 11:46:32 -0700124 }
125 }
126
127 function handleEscape() {
128 return editNameCancel() || closePanel();
129 }
130
Bri Prebilic Coleb699a162015-04-13 12:01:39 -0700131 function setUpPanel() {
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700132 var container, closeBtn, tblDiv;
Bri Prebilic Coleb699a162015-04-13 12:01:39 -0700133 detailsPanel.empty();
134
135 container = detailsPanel.append('div').classed('container', true);
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700136
137 top = container.append('div').classed('top', true);
138 closeBtn = top.append('div').classed('close-btn', true);
139 addCloseBtn(closeBtn);
Bri Prebilic Coleb699a162015-04-13 12:01:39 -0700140 iconDiv = top.append('div').classed('dev-icon', true);
Simon Hunt986b92f2016-06-03 15:46:59 -0700141 top.append('h2').classed('editable clickable', true).on('click', editName);
Simon Huntd766c312015-10-06 11:46:32 -0700142
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700143 tblDiv = top.append('div').classed('top-tables', true);
144 tblDiv.append('div').classed('left', true).append('table');
145 tblDiv.append('div').classed('right', true).append('table');
146
Bri Prebilic Cole45069382015-04-14 15:21:38 -0700147 top.append('hr');
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700148
149 bottom = container.append('div').classed('bottom', true);
Bri Prebilic Cole45069382015-04-14 15:21:38 -0700150 bottom.append('h2').classed('ports-title', true).html('Ports');
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700151 bottom.append('table');
152 }
153
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700154 function addProp(tbody, index, value) {
155 var tr = tbody.append('tr');
156
157 function addCell(cls, txt) {
158 tr.append('td').attr('class', cls).html(txt);
159 }
160 addCell('label', friendlyProps[index] + ' :');
161 addCell('value', value);
162 }
163
Bri Prebilic Cole9b1fb9a2015-07-01 13:57:11 -0700164 function populateTop(tblDiv, details) {
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700165 var leftTbl = tblDiv.select('.left')
166 .select('table')
167 .append('tbody'),
168 rightTbl = tblDiv.select('.right')
169 .select('table')
170 .append('tbody');
171
Bri Prebilic Coleb699a162015-04-13 12:01:39 -0700172 is.loadEmbeddedIcon(iconDiv, details._iconid_type, 40);
Simon Huntd5579252015-10-06 15:09:14 -0700173 top.select('h2').html(details.name);
Bri Prebilic Coleb699a162015-04-13 12:01:39 -0700174
175 propOrder.forEach(function (prop, i) {
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700176 // properties are split into two tables
Simon Huntd5579252015-10-06 15:09:14 -0700177 addProp(i < 4 ? leftTbl : rightTbl, i, details[prop]);
Bri Prebilic Coleb699a162015-04-13 12:01:39 -0700178 });
179 }
180
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700181 function addPortRow(tbody, port) {
182 var tr = tbody.append('tr');
183
184 portCols.forEach(function (col) {
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700185 tr.append('td').html(port[col]);
186 });
187 }
188
Bri Prebilic Coleb699a162015-04-13 12:01:39 -0700189 function populateBottom(table, ports) {
190 var theader = table.append('thead').append('tr'),
191 tbody = table.append('tbody'),
Bri Prebilic Cole45069382015-04-14 15:21:38 -0700192 tbWidth, tbHeight;
Bri Prebilic Coleb699a162015-04-13 12:01:39 -0700193
194 friendlyPortCols.forEach(function (col) {
195 theader.append('th').html(col);
196 });
197 ports.forEach(function (port) {
198 addPortRow(tbody, port);
199 });
200
201 tbWidth = fs.noPxStyle(tbody, 'width') + scrollSize;
Bri Prebilic Cole45069382015-04-14 15:21:38 -0700202 tbHeight = pHeight
203 - (fs.noPxStyle(detailsPanel.el()
204 .select('.top'), 'height')
205 + fs.noPxStyle(detailsPanel.el()
206 .select('.ports-title'), 'height')
207 + portsTblPdg);
Bri Prebilic Coleb699a162015-04-13 12:01:39 -0700208
209 table.style({
210 height: tbHeight + 'px',
211 width: tbWidth + 'px',
212 overflow: 'auto',
213 display: 'block'
214 });
215
Bri Prebilic Cole45069382015-04-14 15:21:38 -0700216 detailsPanel.width(tbWidth + ctnrPdg);
Bri Prebilic Coleb699a162015-04-13 12:01:39 -0700217 }
218
Simon Huntd766c312015-10-06 11:46:32 -0700219 function populateName(div, name) {
220 var lab = div.select('.label'),
221 val = div.select('.value');
222 lab.html('Friendly Name:');
223 val.html(name);
224 }
225
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700226 function populateDetails(details) {
Simon Huntd766c312015-10-06 11:46:32 -0700227 var nameDiv, topTbs, btmTbl, ports;
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700228 setUpPanel();
229
Simon Huntd766c312015-10-06 11:46:32 -0700230 nameDiv = top.select('.name-div');
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700231 topTbs = top.select('.top-tables');
Bri Prebilic Cole45069382015-04-14 15:21:38 -0700232 btmTbl = bottom.select('table');
233 ports = details.ports;
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700234
Simon Huntd766c312015-10-06 11:46:32 -0700235 populateName(nameDiv, details.name);
Bri Prebilic Cole9b1fb9a2015-07-01 13:57:11 -0700236 populateTop(topTbs, details);
Bri Prebilic Coleb699a162015-04-13 12:01:39 -0700237 populateBottom(btmTbl, ports);
Bri Prebilic Cole45069382015-04-14 15:21:38 -0700238
239 detailsPanel.height(pHeight);
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700240 }
241
242 function respDetailsCb(data) {
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700243 $scope.panelData = data.details;
Bri Prebilic Cole54bbfb92015-05-28 16:02:28 -0700244 $scope.$apply();
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700245 }
246
Simon Huntd5579252015-10-06 15:09:14 -0700247 function respNameCb(data) {
248 if (data.warn) {
249 $log.warn(data.warn, data.id);
250 top.select('h2').html(data.id);
251 }
252 }
253
Bri Prebilic Cole45069382015-04-14 15:21:38 -0700254 function createDetailsPane() {
255 detailsPanel = ps.createPanel(pName, {
256 width: wSize.width,
257 margin: 0,
258 hideMargin: 0
259 });
260 detailsPanel.el().style({
261 position: 'absolute',
262 top: pStartY + 'px'
263 });
Bri Prebilic Cole68844ba2015-07-22 15:41:37 -0700264 $scope.hidePanel = function () { detailsPanel.hide(); };
Bri Prebilic Cole45069382015-04-14 15:21:38 -0700265 detailsPanel.hide();
266 }
267
Bri Prebilic Cole7c92a3d2015-01-09 16:50:03 -0800268 angular.module('ovDevice', [])
Simon Hunta89f0f92015-02-26 16:47:12 -0800269 .controller('OvDeviceCtrl',
Bri Prebilic Cole17c6d0a2015-07-16 14:56:40 -0700270 ['$log', '$scope', '$location', 'TableBuilderService', 'FnService',
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700271 'MastService', 'PanelService', 'WebSocketService', 'IconService',
Simon Huntd5579252015-10-06 15:09:14 -0700272 'NavService', 'KeyService',
Simon Hunta89f0f92015-02-26 16:47:12 -0800273
Bri Prebilic Cole17c6d0a2015-07-16 14:56:40 -0700274 function (_$log_, _$scope_, _$location_,
Simon Huntd5579252015-10-06 15:09:14 -0700275 tbs, _fs_, _mast_, _ps_, _wss_, _is_, _ns_, _ks_) {
276 var params,
277 handlers = {};
278
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700279 $log = _$log_;
280 $scope = _$scope_;
Simon Huntd5579252015-10-06 15:09:14 -0700281 $loc = _$location_;
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700282 fs = _fs_;
283 mast = _mast_;
284 ps = _ps_;
285 wss = _wss_;
286 is = _is_;
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700287 ns = _ns_;
Simon Huntd5579252015-10-06 15:09:14 -0700288 ks = _ks_;
289
290 params = $loc.search();
291
Bri Prebilic Cole68844ba2015-07-22 15:41:37 -0700292 $scope.panelData = {};
Bri Prebilic Coleeef67ae2015-07-01 16:26:59 -0700293 $scope.flowTip = 'Show flow view for selected device';
294 $scope.portTip = 'Show port view for selected device';
295 $scope.groupTip = 'Show group view for selected device';
Jian Li1f544732015-12-30 23:36:37 -0800296 $scope.meterTip = 'Show meter view for selected device';
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700297
Bri Prebilic Cole17c6d0a2015-07-16 14:56:40 -0700298 // details panel handlers
299 handlers[detailsResp] = respDetailsCb;
Simon Huntd5579252015-10-06 15:09:14 -0700300 handlers[nameChangeResp] = respNameCb;
Bri Prebilic Cole17c6d0a2015-07-16 14:56:40 -0700301 wss.bindHandlers(handlers);
302
303 // query for if a certain device needs to be highlighted
304 if (params.hasOwnProperty('devId')) {
305 $scope.selId = params['devId'];
306 wss.sendEvent(detailsReq, { id: $scope.selId });
307 }
308
Bri Prebilic Coleb699a162015-04-13 12:01:39 -0700309 function selCb($event, row) {
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -0700310 if ($scope.selId) {
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700311 wss.sendEvent(detailsReq, { id: row.id });
312 } else {
Bri Prebilic Cole68844ba2015-07-22 15:41:37 -0700313 $scope.hidePanel();
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700314 }
315 $log.debug('Got a click on:', row);
316 }
317
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700318 tbs.buildTable({
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700319 scope: $scope,
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700320 tag: 'device',
321 selCb: selCb
322 });
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700323
Bri Prebilic Cole9b1fb9a2015-07-01 13:57:11 -0700324 $scope.nav = function (path) {
325 if ($scope.selId) {
326 ns.navTo(path, { devId: $scope.selId });
327 }
328 };
329
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700330 $scope.$on('$destroy', function () {
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700331 wss.unbindHandlers(handlers);
Simon Hunta89f0f92015-02-26 16:47:12 -0800332 });
333
Bri Prebilic Cole72eb6db2015-03-30 16:58:53 -0700334 $log.log('OvDeviceCtrl has been created');
Bri Prebilic Cole54bbfb92015-05-28 16:02:28 -0700335 }])
336
Bri Prebilic Cole55ee09b2015-08-04 14:34:07 -0700337 .directive('deviceDetailsPanel',
338 ['$rootScope', '$window', '$timeout', 'KeyService',
339 function ($rootScope, $window, $timeout, ks) {
Bri Prebilic Cole17c6d0a2015-07-16 14:56:40 -0700340 return function (scope) {
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -0700341 var unbindWatch;
Bri Prebilic Cole54bbfb92015-05-28 16:02:28 -0700342
Bri Prebilic Cole17c6d0a2015-07-16 14:56:40 -0700343 function heightCalc() {
344 pStartY = fs.noPxStyle(d3.select('.tabular-header'), 'height')
345 + mast.mastHeight() + topPdg;
346 wSize = fs.windowSize(pStartY);
347 pHeight = wSize.height;
348 }
Bri Prebilic Cole17c6d0a2015-07-16 14:56:40 -0700349
Bri Prebilic Cole55ee09b2015-08-04 14:34:07 -0700350 function initPanel() {
351 heightCalc();
352 createDetailsPane();
353 }
Bri Prebilic Cole17c6d0a2015-07-16 14:56:40 -0700354
Bri Prebilic Cole55ee09b2015-08-04 14:34:07 -0700355 // Safari has a bug where it renders the fixed-layout table wrong
356 // if you ask for the window's size too early
357 if (scope.onos.browser === 'safari') {
358 $timeout(initPanel);
359 } else {
360 initPanel();
361 }
Bri Prebilic Cole9dcaea52015-07-21 14:39:48 -0700362 // create key bindings to handle panel
363 ks.keyBindings({
Simon Huntd766c312015-10-06 11:46:32 -0700364 enter: editNameSave,
365 esc: [handleEscape, 'Close the details panel'],
Bri Prebilic Cole9dcaea52015-07-21 14:39:48 -0700366 _helpFormat: ['esc']
367 });
368 ks.gestureNotes([
369 ['click', 'Select a row to show device details'],
370 ['scroll down', 'See more devices']
371 ]);
372
Bri Prebilic Cole55ee09b2015-08-04 14:34:07 -0700373 // if the panelData changes
Bri Prebilic Cole17c6d0a2015-07-16 14:56:40 -0700374 scope.$watch('panelData', function () {
375 if (!fs.isEmptyObject(scope.panelData)) {
376 populateDetails(scope.panelData);
377 detailsPanel.show();
Bri Prebilic Cole54bbfb92015-05-28 16:02:28 -0700378 }
Bri Prebilic Cole17c6d0a2015-07-16 14:56:40 -0700379 });
Bri Prebilic Cole54bbfb92015-05-28 16:02:28 -0700380
Bri Prebilic Cole55ee09b2015-08-04 14:34:07 -0700381 // if the window size changes
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -0700382 unbindWatch = $rootScope.$watchCollection(
Bri Prebilic Cole17c6d0a2015-07-16 14:56:40 -0700383 function () {
384 return {
385 h: $window.innerHeight,
386 w: $window.innerWidth
387 };
388 }, function () {
Bri Prebilic Cole54bbfb92015-05-28 16:02:28 -0700389 if (!fs.isEmptyObject(scope.panelData)) {
Bri Prebilic Cole17c6d0a2015-07-16 14:56:40 -0700390 heightCalc();
Bri Prebilic Cole54bbfb92015-05-28 16:02:28 -0700391 populateDetails(scope.panelData);
Bri Prebilic Cole54bbfb92015-05-28 16:02:28 -0700392 }
Bri Prebilic Cole17c6d0a2015-07-16 14:56:40 -0700393 }
394 );
Bri Prebilic Cole54bbfb92015-05-28 16:02:28 -0700395
Bri Prebilic Cole17c6d0a2015-07-16 14:56:40 -0700396 scope.$on('$destroy', function () {
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -0700397 unbindWatch();
Bri Prebilic Cole9dcaea52015-07-21 14:39:48 -0700398 ks.unbindKeys();
Bri Prebilic Cole17c6d0a2015-07-16 14:56:40 -0700399 ps.destroyPanel(pName);
400 });
401 };
402 }]);
Bri Prebilic Cole7c92a3d2015-01-09 16:50:03 -0800403}());