blob: c85f5ec7f7d2694afac4897bf1842a4c93bdc8c7 [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
Simon Hunta9301bb2016-10-20 11:38:10 -070049 propSplit = 4,
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070050 propOrder = [
Simon Huntd5579252015-10-06 15:09:14 -070051 'id', 'type', 'masterid', 'chassisid',
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070052 'mfr', 'hw', 'sw', 'protocol', 'serial'
53 ],
54 friendlyProps = [
Simon Huntd5579252015-10-06 15:09:14 -070055 'URI', 'Type', 'Master ID', 'Chassis ID',
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070056 'Vendor', 'H/W Version', 'S/W Version', 'Protocol', 'Serial #'
57 ],
58 portCols = [
Thomas Vachuskab52a0142015-04-21 17:48:15 -070059 'enabled', 'id', 'speed', 'type', 'elinks_dest', 'name'
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070060 ],
61 friendlyPortCols = [
Thomas Vachuskab52a0142015-04-21 17:48:15 -070062 'Enabled', 'ID', 'Speed', 'Type', 'Egress Links', 'Name'
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070063 ];
64
Bri Prebilic Cole9dcaea52015-07-21 14:39:48 -070065 function closePanel() {
66 if (detailsPanel.isVisible()) {
67 $scope.selId = null;
68 detailsPanel.hide();
Simon Huntd766c312015-10-06 11:46:32 -070069 return true;
Bri Prebilic Cole9dcaea52015-07-21 14:39:48 -070070 }
Simon Huntd766c312015-10-06 11:46:32 -070071 return false;
Bri Prebilic Cole9dcaea52015-07-21 14:39:48 -070072 }
73
Bri Prebilic Coleb699a162015-04-13 12:01:39 -070074 function addCloseBtn(div) {
Simon Hunt986b92f2016-06-03 15:46:59 -070075 is.loadEmbeddedIcon(div, 'close', 20);
Bri Prebilic Cole9dcaea52015-07-21 14:39:48 -070076 div.on('click', closePanel);
Bri Prebilic Coleb699a162015-04-13 12:01:39 -070077 }
78
Simon Huntd5579252015-10-06 15:09:14 -070079 function exitEditMode(nameH2, name) {
80 nameH2.html(name);
Simon Hunt986b92f2016-06-03 15:46:59 -070081 nameH2.classed('editable clickable', true);
Simon Huntd5579252015-10-06 15:09:14 -070082 editingName = false;
83 ks.enableGlobalKeys(true);
Simon Huntd766c312015-10-06 11:46:32 -070084 }
85
86 function editNameSave() {
Simon Huntd5579252015-10-06 15:09:14 -070087 var nameH2 = top.select('h2'),
88 id = $scope.panelData.id,
89 val,
Simon Huntd766c312015-10-06 11:46:32 -070090 newVal;
Simon Huntd5579252015-10-06 15:09:14 -070091
Simon Huntd766c312015-10-06 11:46:32 -070092 if (editingName) {
Simon Huntd5579252015-10-06 15:09:14 -070093 val = nameH2.select('input').property('value').trim();
94 newVal = val || id;
Simon Huntd766c312015-10-06 11:46:32 -070095
Simon Huntd5579252015-10-06 15:09:14 -070096 exitEditMode(nameH2, newVal);
Simon Huntd766c312015-10-06 11:46:32 -070097 $scope.panelData.name = newVal;
Simon Huntd5579252015-10-06 15:09:14 -070098 wss.sendEvent(nameChangeReq, { id: id, name: val });
Simon Huntd766c312015-10-06 11:46:32 -070099 }
100 }
101
102 function editNameCancel() {
Simon Huntd766c312015-10-06 11:46:32 -0700103 if (editingName) {
Simon Huntd5579252015-10-06 15:09:14 -0700104 exitEditMode(top.select('h2'), $scope.panelData.name);
Simon Huntd766c312015-10-06 11:46:32 -0700105 return true;
106 }
107 return false;
108 }
109
110 function editName() {
Simon Huntd5579252015-10-06 15:09:14 -0700111 var nameH2 = top.select('h2'),
112 tf, el;
113
Simon Huntd766c312015-10-06 11:46:32 -0700114 if (!editingName) {
Simon Hunt986b92f2016-06-03 15:46:59 -0700115 nameH2.classed('editable clickable', false);
Simon Huntd5579252015-10-06 15:09:14 -0700116 nameH2.html('');
117 tf = nameH2.append('input').classed('name-input', true)
Simon Huntd766c312015-10-06 11:46:32 -0700118 .attr('type', 'text')
119 .attr('value', $scope.panelData.name);
Simon Huntd5579252015-10-06 15:09:14 -0700120 el = tf[0][0];
121 el.focus();
122 el.select();
123 editingName = true;
124 ks.enableGlobalKeys(false);
Simon Huntd766c312015-10-06 11:46:32 -0700125 }
126 }
127
128 function handleEscape() {
129 return editNameCancel() || closePanel();
130 }
131
Bri Prebilic Coleb699a162015-04-13 12:01:39 -0700132 function setUpPanel() {
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700133 var container, closeBtn, tblDiv;
Bri Prebilic Coleb699a162015-04-13 12:01:39 -0700134 detailsPanel.empty();
135
136 container = detailsPanel.append('div').classed('container', true);
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700137
138 top = container.append('div').classed('top', true);
139 closeBtn = top.append('div').classed('close-btn', true);
140 addCloseBtn(closeBtn);
Bri Prebilic Coleb699a162015-04-13 12:01:39 -0700141 iconDiv = top.append('div').classed('dev-icon', true);
Simon Hunt986b92f2016-06-03 15:46:59 -0700142 top.append('h2').classed('editable clickable', true).on('click', editName);
Simon Huntd766c312015-10-06 11:46:32 -0700143
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700144 tblDiv = top.append('div').classed('top-tables', true);
145 tblDiv.append('div').classed('left', true).append('table');
146 tblDiv.append('div').classed('right', true).append('table');
147
Bri Prebilic Cole45069382015-04-14 15:21:38 -0700148 top.append('hr');
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700149
150 bottom = container.append('div').classed('bottom', true);
Bri Prebilic Cole45069382015-04-14 15:21:38 -0700151 bottom.append('h2').classed('ports-title', true).html('Ports');
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700152 bottom.append('table');
153 }
154
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700155 function addProp(tbody, index, value) {
156 var tr = tbody.append('tr');
157
158 function addCell(cls, txt) {
159 tr.append('td').attr('class', cls).html(txt);
160 }
161 addCell('label', friendlyProps[index] + ' :');
162 addCell('value', value);
163 }
164
Bri Prebilic Cole9b1fb9a2015-07-01 13:57:11 -0700165 function populateTop(tblDiv, details) {
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700166 var leftTbl = tblDiv.select('.left')
167 .select('table')
168 .append('tbody'),
169 rightTbl = tblDiv.select('.right')
170 .select('table')
171 .append('tbody');
172
Bri Prebilic Coleb699a162015-04-13 12:01:39 -0700173 is.loadEmbeddedIcon(iconDiv, details._iconid_type, 40);
Simon Huntd5579252015-10-06 15:09:14 -0700174 top.select('h2').html(details.name);
Bri Prebilic Coleb699a162015-04-13 12:01:39 -0700175
176 propOrder.forEach(function (prop, i) {
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700177 // properties are split into two tables
Simon Hunta9301bb2016-10-20 11:38:10 -0700178 addProp(i < propSplit ? leftTbl : rightTbl, i, details[prop]);
Bri Prebilic Coleb699a162015-04-13 12:01:39 -0700179 });
180 }
181
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700182 function addPortRow(tbody, port) {
183 var tr = tbody.append('tr');
184
185 portCols.forEach(function (col) {
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700186 tr.append('td').html(port[col]);
187 });
188 }
189
Bri Prebilic Coleb699a162015-04-13 12:01:39 -0700190 function populateBottom(table, ports) {
191 var theader = table.append('thead').append('tr'),
192 tbody = table.append('tbody'),
Bri Prebilic Cole45069382015-04-14 15:21:38 -0700193 tbWidth, tbHeight;
Bri Prebilic Coleb699a162015-04-13 12:01:39 -0700194
195 friendlyPortCols.forEach(function (col) {
196 theader.append('th').html(col);
197 });
198 ports.forEach(function (port) {
199 addPortRow(tbody, port);
200 });
201
202 tbWidth = fs.noPxStyle(tbody, 'width') + scrollSize;
Bri Prebilic Cole45069382015-04-14 15:21:38 -0700203 tbHeight = pHeight
204 - (fs.noPxStyle(detailsPanel.el()
205 .select('.top'), 'height')
206 + fs.noPxStyle(detailsPanel.el()
207 .select('.ports-title'), 'height')
208 + portsTblPdg);
Bri Prebilic Coleb699a162015-04-13 12:01:39 -0700209
210 table.style({
211 height: tbHeight + 'px',
212 width: tbWidth + 'px',
213 overflow: 'auto',
214 display: 'block'
215 });
216
Bri Prebilic Cole45069382015-04-14 15:21:38 -0700217 detailsPanel.width(tbWidth + ctnrPdg);
Bri Prebilic Coleb699a162015-04-13 12:01:39 -0700218 }
219
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700220 function populateDetails(details) {
Simon Hunta9301bb2016-10-20 11:38:10 -0700221 var topTbs, btmTbl, ports;
222
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700223 setUpPanel();
224
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700225 topTbs = top.select('.top-tables');
Bri Prebilic Cole45069382015-04-14 15:21:38 -0700226 btmTbl = bottom.select('table');
227 ports = details.ports;
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700228
Bri Prebilic Cole9b1fb9a2015-07-01 13:57:11 -0700229 populateTop(topTbs, details);
Bri Prebilic Coleb699a162015-04-13 12:01:39 -0700230 populateBottom(btmTbl, ports);
Bri Prebilic Cole45069382015-04-14 15:21:38 -0700231
232 detailsPanel.height(pHeight);
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700233 }
234
235 function respDetailsCb(data) {
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700236 $scope.panelData = data.details;
Bri Prebilic Cole54bbfb92015-05-28 16:02:28 -0700237 $scope.$apply();
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700238 }
239
Simon Huntd5579252015-10-06 15:09:14 -0700240 function respNameCb(data) {
241 if (data.warn) {
242 $log.warn(data.warn, data.id);
243 top.select('h2').html(data.id);
244 }
245 }
246
Bri Prebilic Cole45069382015-04-14 15:21:38 -0700247 function createDetailsPane() {
248 detailsPanel = ps.createPanel(pName, {
249 width: wSize.width,
250 margin: 0,
251 hideMargin: 0
252 });
253 detailsPanel.el().style({
254 position: 'absolute',
255 top: pStartY + 'px'
256 });
Bri Prebilic Cole68844ba2015-07-22 15:41:37 -0700257 $scope.hidePanel = function () { detailsPanel.hide(); };
Bri Prebilic Cole45069382015-04-14 15:21:38 -0700258 detailsPanel.hide();
259 }
260
Bri Prebilic Cole7c92a3d2015-01-09 16:50:03 -0800261 angular.module('ovDevice', [])
Simon Hunta89f0f92015-02-26 16:47:12 -0800262 .controller('OvDeviceCtrl',
Bri Prebilic Cole17c6d0a2015-07-16 14:56:40 -0700263 ['$log', '$scope', '$location', 'TableBuilderService', 'FnService',
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700264 'MastService', 'PanelService', 'WebSocketService', 'IconService',
Simon Huntd5579252015-10-06 15:09:14 -0700265 'NavService', 'KeyService',
Simon Hunta89f0f92015-02-26 16:47:12 -0800266
Bri Prebilic Cole17c6d0a2015-07-16 14:56:40 -0700267 function (_$log_, _$scope_, _$location_,
Simon Huntd5579252015-10-06 15:09:14 -0700268 tbs, _fs_, _mast_, _ps_, _wss_, _is_, _ns_, _ks_) {
269 var params,
270 handlers = {};
271
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700272 $log = _$log_;
273 $scope = _$scope_;
Simon Huntd5579252015-10-06 15:09:14 -0700274 $loc = _$location_;
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700275 fs = _fs_;
276 mast = _mast_;
277 ps = _ps_;
278 wss = _wss_;
279 is = _is_;
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700280 ns = _ns_;
Simon Huntd5579252015-10-06 15:09:14 -0700281 ks = _ks_;
282
283 params = $loc.search();
284
Bri Prebilic Cole68844ba2015-07-22 15:41:37 -0700285 $scope.panelData = {};
Bri Prebilic Coleeef67ae2015-07-01 16:26:59 -0700286 $scope.flowTip = 'Show flow view for selected device';
287 $scope.portTip = 'Show port view for selected device';
288 $scope.groupTip = 'Show group view for selected device';
Jian Li1f544732015-12-30 23:36:37 -0800289 $scope.meterTip = 'Show meter view for selected device';
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700290
Bri Prebilic Cole17c6d0a2015-07-16 14:56:40 -0700291 // details panel handlers
292 handlers[detailsResp] = respDetailsCb;
Simon Huntd5579252015-10-06 15:09:14 -0700293 handlers[nameChangeResp] = respNameCb;
Bri Prebilic Cole17c6d0a2015-07-16 14:56:40 -0700294 wss.bindHandlers(handlers);
295
296 // query for if a certain device needs to be highlighted
297 if (params.hasOwnProperty('devId')) {
298 $scope.selId = params['devId'];
299 wss.sendEvent(detailsReq, { id: $scope.selId });
300 }
301
Bri Prebilic Coleb699a162015-04-13 12:01:39 -0700302 function selCb($event, row) {
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -0700303 if ($scope.selId) {
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700304 wss.sendEvent(detailsReq, { id: row.id });
305 } else {
Bri Prebilic Cole68844ba2015-07-22 15:41:37 -0700306 $scope.hidePanel();
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700307 }
308 $log.debug('Got a click on:', row);
309 }
310
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700311 tbs.buildTable({
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700312 scope: $scope,
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700313 tag: 'device',
314 selCb: selCb
315 });
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700316
Bri Prebilic Cole9b1fb9a2015-07-01 13:57:11 -0700317 $scope.nav = function (path) {
318 if ($scope.selId) {
319 ns.navTo(path, { devId: $scope.selId });
320 }
321 };
322
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700323 $scope.$on('$destroy', function () {
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700324 wss.unbindHandlers(handlers);
Simon Hunta89f0f92015-02-26 16:47:12 -0800325 });
326
Bri Prebilic Cole72eb6db2015-03-30 16:58:53 -0700327 $log.log('OvDeviceCtrl has been created');
Bri Prebilic Cole54bbfb92015-05-28 16:02:28 -0700328 }])
329
Bri Prebilic Cole55ee09b2015-08-04 14:34:07 -0700330 .directive('deviceDetailsPanel',
331 ['$rootScope', '$window', '$timeout', 'KeyService',
332 function ($rootScope, $window, $timeout, ks) {
Bri Prebilic Cole17c6d0a2015-07-16 14:56:40 -0700333 return function (scope) {
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -0700334 var unbindWatch;
Bri Prebilic Cole54bbfb92015-05-28 16:02:28 -0700335
Bri Prebilic Cole17c6d0a2015-07-16 14:56:40 -0700336 function heightCalc() {
337 pStartY = fs.noPxStyle(d3.select('.tabular-header'), 'height')
338 + mast.mastHeight() + topPdg;
339 wSize = fs.windowSize(pStartY);
340 pHeight = wSize.height;
341 }
Bri Prebilic Cole17c6d0a2015-07-16 14:56:40 -0700342
Bri Prebilic Cole55ee09b2015-08-04 14:34:07 -0700343 function initPanel() {
344 heightCalc();
345 createDetailsPane();
346 }
Bri Prebilic Cole17c6d0a2015-07-16 14:56:40 -0700347
Bri Prebilic Cole55ee09b2015-08-04 14:34:07 -0700348 // Safari has a bug where it renders the fixed-layout table wrong
349 // if you ask for the window's size too early
350 if (scope.onos.browser === 'safari') {
351 $timeout(initPanel);
352 } else {
353 initPanel();
354 }
Bri Prebilic Cole9dcaea52015-07-21 14:39:48 -0700355 // create key bindings to handle panel
356 ks.keyBindings({
Simon Huntd766c312015-10-06 11:46:32 -0700357 enter: editNameSave,
358 esc: [handleEscape, 'Close the details panel'],
Bri Prebilic Cole9dcaea52015-07-21 14:39:48 -0700359 _helpFormat: ['esc']
360 });
361 ks.gestureNotes([
362 ['click', 'Select a row to show device details'],
363 ['scroll down', 'See more devices']
364 ]);
365
Bri Prebilic Cole55ee09b2015-08-04 14:34:07 -0700366 // if the panelData changes
Bri Prebilic Cole17c6d0a2015-07-16 14:56:40 -0700367 scope.$watch('panelData', function () {
368 if (!fs.isEmptyObject(scope.panelData)) {
369 populateDetails(scope.panelData);
370 detailsPanel.show();
Bri Prebilic Cole54bbfb92015-05-28 16:02:28 -0700371 }
Bri Prebilic Cole17c6d0a2015-07-16 14:56:40 -0700372 });
Bri Prebilic Cole54bbfb92015-05-28 16:02:28 -0700373
Bri Prebilic Cole55ee09b2015-08-04 14:34:07 -0700374 // if the window size changes
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -0700375 unbindWatch = $rootScope.$watchCollection(
Bri Prebilic Cole17c6d0a2015-07-16 14:56:40 -0700376 function () {
377 return {
378 h: $window.innerHeight,
379 w: $window.innerWidth
380 };
381 }, function () {
Bri Prebilic Cole54bbfb92015-05-28 16:02:28 -0700382 if (!fs.isEmptyObject(scope.panelData)) {
Bri Prebilic Cole17c6d0a2015-07-16 14:56:40 -0700383 heightCalc();
Bri Prebilic Cole54bbfb92015-05-28 16:02:28 -0700384 populateDetails(scope.panelData);
Bri Prebilic Cole54bbfb92015-05-28 16:02:28 -0700385 }
Bri Prebilic Cole17c6d0a2015-07-16 14:56:40 -0700386 }
387 );
Bri Prebilic Cole54bbfb92015-05-28 16:02:28 -0700388
Bri Prebilic Cole17c6d0a2015-07-16 14:56:40 -0700389 scope.$on('$destroy', function () {
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -0700390 unbindWatch();
Bri Prebilic Cole9dcaea52015-07-21 14:39:48 -0700391 ks.unbindKeys();
Bri Prebilic Cole17c6d0a2015-07-16 14:56:40 -0700392 ps.destroyPanel(pName);
393 });
394 };
395 }]);
Bri Prebilic Cole7c92a3d2015-01-09 16:50:03 -0800396}());