Jian Li | 1f54473 | 2015-12-30 23:36:37 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Jian Li | 1f54473 | 2015-12-30 23:36:37 -0800 | [diff] [blame] | 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 | /* |
| 18 | ONOS GUI -- Meter View Module |
| 19 | */ |
| 20 | (function () { |
| 21 | 'use strict'; |
| 22 | |
| 23 | // injected references |
Steven Burrows | 7ed6036 | 2017-10-20 03:26:05 +0100 | [diff] [blame] | 24 | var $log, $scope, $location, fs, tbs, ns, prefs, |
| 25 | fs, mast, wss, ns, dps, is, ps; |
| 26 | |
| 27 | var detailsPanel, |
| 28 | pStartY, |
| 29 | pHeight, |
| 30 | wSize, |
| 31 | meter; |
| 32 | |
| 33 | // constants |
| 34 | var topPdg = 28, |
| 35 | dPanelWidth = 480, |
| 36 | |
| 37 | pName = 'meter-details-panel', |
| 38 | detailsReq = 'meterDetailsRequest', |
| 39 | detailsResp = 'meterDetailsResponse'; |
| 40 | |
| 41 | |
| 42 | var keyBindings = { |
| 43 | esc: [closePanel, 'Close the details panel'], |
| 44 | _helpFormat: ['esc'], |
| 45 | }; |
| 46 | |
| 47 | function closePanel() { |
| 48 | if (detailsPanel.isVisible()) { |
| 49 | $scope.selId = null; |
| 50 | detailsPanel.hide(); |
| 51 | return true; |
| 52 | } |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | function createDetailsPanel() { |
| 57 | detailsPanel = dps.create(pName, { |
| 58 | width: wSize.width, |
| 59 | margin: 0, |
| 60 | hideMargin: 0, |
| 61 | scope: $scope, |
| 62 | keyBindings: keyBindings, |
| 63 | }); |
| 64 | |
| 65 | dps.setResponse(detailsResp, respDetailsCb); |
| 66 | |
| 67 | $scope.hidePanel = function () { detailsPanel.hide(); }; |
| 68 | } |
| 69 | |
| 70 | function setUpPanel() { |
| 71 | dps.empty(); |
| 72 | dps.addContainers(); |
| 73 | dps.addCloseButton(closePanel); |
| 74 | |
| 75 | var top = dps.top(); |
| 76 | |
| 77 | dps.addHeading('port-icon'); |
| 78 | top.append('div').classed('top-content', true); |
| 79 | |
| 80 | top.append('hr'); |
| 81 | } |
| 82 | |
| 83 | function friendlyPropsList(details) { |
| 84 | $log.debug(details); |
| 85 | return { |
| 86 | 'ID': details['id'], |
| 87 | 'Device': details['devId'], |
| 88 | 'App Id': details['app_id'], |
| 89 | 'Bytes': details['bytes'], |
| 90 | 'Burst': details['isBurst'], |
| 91 | 'Packets': details['packets'], |
| 92 | 'State': details['state'], |
| 93 | }; |
| 94 | } |
| 95 | |
| 96 | |
| 97 | function populateTop(tblDiv, details) { |
| 98 | is.loadEmbeddedIcon(dps.select('.iconDiv'), details._iconid_type, 40); |
| 99 | dps.top().select('h2').text(details.devId + ' Meter ' + details.id); |
| 100 | dps.addPropsList(tblDiv, friendlyPropsList(details)); |
| 101 | } |
| 102 | |
| 103 | function populateDetails(details) { |
| 104 | setUpPanel(); |
| 105 | populateTop(dps.select('.top-content'), details); |
| 106 | detailsPanel.height(pHeight); |
| 107 | detailsPanel.width(dPanelWidth); |
| 108 | |
| 109 | } |
| 110 | |
| 111 | function respDetailsCb(data) { |
| 112 | $scope.panelData = data.details; |
| 113 | meter = data.meter; |
| 114 | $scope.$apply(); |
| 115 | } |
Jian Li | 1f54473 | 2015-12-30 23:36:37 -0800 | [diff] [blame] | 116 | |
| 117 | angular.module('ovMeter', []) |
| 118 | .controller('OvMeterCtrl', |
| 119 | ['$log', '$scope', '$location', '$sce', |
Steven Burrows | 7ed6036 | 2017-10-20 03:26:05 +0100 | [diff] [blame] | 120 | 'FnService', 'TableBuilderService', 'NavService', 'PrefsService', |
| 121 | 'MastService', 'WebSocketService', 'DetailsPanelService', 'IconService', |
| 122 | 'PanelService', |
Jian Li | 1f54473 | 2015-12-30 23:36:37 -0800 | [diff] [blame] | 123 | |
Steven Burrows | 7ed6036 | 2017-10-20 03:26:05 +0100 | [diff] [blame] | 124 | function (_$log_, _$scope_, _$location_, $sce, |
| 125 | _fs_, _tbs_, _ns_, _prefs_, |
| 126 | _mast_, _wss_, _dps_, _is_, _ps_) { |
Jian Li | 1f54473 | 2015-12-30 23:36:37 -0800 | [diff] [blame] | 127 | var params; |
| 128 | $log = _$log_; |
| 129 | $scope = _$scope_; |
| 130 | $location = _$location_; |
| 131 | fs = _fs_; |
| 132 | tbs = _tbs_; |
| 133 | ns = _ns_; |
Steven Burrows | 7ed6036 | 2017-10-20 03:26:05 +0100 | [diff] [blame] | 134 | fs = _fs_; |
| 135 | mast = _mast_; |
| 136 | wss = _wss_; |
| 137 | prefs = _prefs_; |
| 138 | dps = _dps_; |
| 139 | is = _is_; |
| 140 | |
Jian Li | 1f54473 | 2015-12-30 23:36:37 -0800 | [diff] [blame] | 141 | $scope.deviceTip = 'Show device table'; |
| 142 | $scope.flowTip = 'Show flow view for this device'; |
| 143 | $scope.portTip = 'Show port view for this device'; |
| 144 | $scope.groupTip = 'Show group view for this device'; |
Yi Tseng | a87b40c | 2017-09-10 00:59:03 -0700 | [diff] [blame] | 145 | $scope.pipeconfTip = 'Show pipeconf view for selected device'; |
Jian Li | 1f54473 | 2015-12-30 23:36:37 -0800 | [diff] [blame] | 146 | |
| 147 | params = $location.search(); |
| 148 | if (params.hasOwnProperty('devId')) { |
| 149 | $scope.devId = params['devId']; |
| 150 | } |
| 151 | |
Steven Burrows | 7ed6036 | 2017-10-20 03:26:05 +0100 | [diff] [blame] | 152 | function selCb($event, row) { |
| 153 | if ($scope.selId) { |
| 154 | wss.sendEvent(detailsReq, { |
| 155 | id: row.id, |
| 156 | devId: $scope.devId, |
| 157 | }); |
| 158 | } else { |
| 159 | $scope.hidePanel(); |
| 160 | } |
| 161 | $log.debug('Got a click on:', row); |
| 162 | } |
| 163 | |
Jian Li | 1f54473 | 2015-12-30 23:36:37 -0800 | [diff] [blame] | 164 | tbs.buildTable({ |
| 165 | scope: $scope, |
| 166 | tag: 'meter', |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 167 | query: params, |
Steven Burrows | 7ed6036 | 2017-10-20 03:26:05 +0100 | [diff] [blame] | 168 | selCb: selCb, |
Jian Li | 1f54473 | 2015-12-30 23:36:37 -0800 | [diff] [blame] | 169 | }); |
| 170 | |
| 171 | $scope.$watch('tableData', function () { |
| 172 | if (!fs.isEmptyObject($scope.tableData)) { |
| 173 | $scope.tableData.forEach(function (meter) { |
| 174 | meter.bands = $sce.trustAsHtml(meter.bands); |
| 175 | }); |
| 176 | } |
| 177 | }); |
| 178 | |
| 179 | $scope.nav = function (path) { |
| 180 | if ($scope.devId) { |
| 181 | ns.navTo(path, { devId: $scope.devId }); |
| 182 | } |
| 183 | }; |
| 184 | |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 185 | Object.defineProperty($scope, 'queryFilter', { |
| 186 | get: function () { |
kalagesa | 1101dbb | 2016-12-20 23:34:28 +0530 | [diff] [blame] | 187 | var out = {}; |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 188 | out[$scope.queryBy || '$'] = $scope.query; |
kalagesa | 1101dbb | 2016-12-20 23:34:28 +0530 | [diff] [blame] | 189 | return out; |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 190 | }, |
kalagesa | 1101dbb | 2016-12-20 23:34:28 +0530 | [diff] [blame] | 191 | }); |
| 192 | |
Steven Burrows | 7ed6036 | 2017-10-20 03:26:05 +0100 | [diff] [blame] | 193 | $scope.$on('$destroy', function () { |
| 194 | dps.destroy(); |
| 195 | }); |
kalagesa | 1101dbb | 2016-12-20 23:34:28 +0530 | [diff] [blame] | 196 | |
Jian Li | 1f54473 | 2015-12-30 23:36:37 -0800 | [diff] [blame] | 197 | $log.log('OvMeterCtrl has been created'); |
Steven Burrows | 7ed6036 | 2017-10-20 03:26:05 +0100 | [diff] [blame] | 198 | }]) |
| 199 | .directive('meterDetailsPanel', |
| 200 | ['$rootScope', '$window', '$timeout', 'KeyService', |
| 201 | function ($rootScope, $window, $timeout, ks) { |
| 202 | return function (scope) { |
| 203 | var unbindWatch; |
| 204 | |
| 205 | function heightCalc() { |
| 206 | pStartY = fs.noPxStyle(d3.select('.tabular-header'), 'height') |
| 207 | + mast.mastHeight() + topPdg; |
| 208 | wSize = fs.windowSize(pStartY); |
| 209 | pHeight = wSize.height; |
| 210 | } |
| 211 | |
| 212 | function initPanel() { |
| 213 | heightCalc(); |
| 214 | createDetailsPanel(); |
| 215 | } |
| 216 | |
| 217 | // Safari has a bug where it renders the fixed-layout table wrong |
| 218 | // if you ask for the window's size too early |
| 219 | if (scope.onos.browser === 'safari') { |
| 220 | $timeout(initPanel); |
| 221 | } else { |
| 222 | initPanel(); |
| 223 | } |
| 224 | // create key bindings to handle panel |
| 225 | ks.keyBindings(keyBindings); |
| 226 | |
| 227 | ks.gestureNotes([ |
| 228 | ['click', 'Select a row to show port details'], |
| 229 | ['scroll down', 'See more ports'], |
| 230 | ]); |
| 231 | |
| 232 | // if the panelData changes |
| 233 | scope.$watch('panelData', function () { |
| 234 | if (!fs.isEmptyObject(scope.panelData)) { |
| 235 | populateDetails(scope.panelData); |
| 236 | detailsPanel.show(); |
| 237 | } |
| 238 | }); |
| 239 | |
| 240 | // if the window size changes |
| 241 | unbindWatch = $rootScope.$watchCollection( |
| 242 | function () { |
| 243 | return { |
| 244 | h: $window.innerHeight, |
| 245 | w: $window.innerWidth, |
| 246 | }; |
| 247 | }, function () { |
| 248 | if (!fs.isEmptyObject(scope.panelData)) { |
| 249 | heightCalc(); |
| 250 | populateDetails(scope.panelData); |
| 251 | } |
| 252 | } |
| 253 | ); |
| 254 | |
| 255 | scope.$on('$destroy', function () { |
| 256 | unbindWatch(); |
| 257 | ks.unbindKeys(); |
| 258 | ps.destroyPanel(pName); |
| 259 | }); |
| 260 | }; |
| 261 | }]); |
kalagesa | 1101dbb | 2016-12-20 23:34:28 +0530 | [diff] [blame] | 262 | }()); |