Simon Hunt | 1002cd8 | 2015-04-23 14:44:03 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Simon Hunt | 1002cd8 | 2015-04-23 14:44:03 -0700 | [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 -- Flow View Module |
| 19 | */ |
| 20 | |
| 21 | (function () { |
| 22 | 'use strict'; |
| 23 | |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 24 | // injected references |
Viswanath KSP | 7bdc917 | 2016-10-19 20:19:17 +0530 | [diff] [blame] | 25 | var $log, $scope, $location, fs, tbs, ns, mast, ps, wss, is, ks; |
| 26 | |
| 27 | // internal state |
| 28 | var detailsPanel, |
| 29 | pStartY, |
| 30 | pHeight, |
| 31 | top, |
Viswanath KSP | 70e0d14 | 2016-10-28 21:58:32 +0530 | [diff] [blame] | 32 | topTable, |
Viswanath KSP | ed55ecf | 2016-12-22 18:15:45 +0530 | [diff] [blame] | 33 | trmtDiv, |
| 34 | selDiv, |
| 35 | topSelTable, |
| 36 | topTrmtTable, |
Viswanath KSP | 7bdc917 | 2016-10-19 20:19:17 +0530 | [diff] [blame] | 37 | bottom, |
| 38 | iconDiv, |
| 39 | nameDiv, |
| 40 | wSize; |
| 41 | |
| 42 | |
| 43 | // constants |
| 44 | var topPdg = 28, |
| 45 | ctnrPdg = 24, |
| 46 | scrollSize = 17, |
| 47 | portsTblPdg = 50, |
| 48 | htPdg = 479, |
| 49 | wtPdg = 532, |
| 50 | |
| 51 | pName = 'flow-details-panel', |
| 52 | detailsReq = 'flowDetailsRequest', |
Viswanath KSP | 70e0d14 | 2016-10-28 21:58:32 +0530 | [diff] [blame] | 53 | detailsResp = 'flowDetailsResponse', |
| 54 | |
| 55 | propOrder = [ |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame] | 56 | 'flowId', |
| 57 | 'state', |
| 58 | |
| 59 | 'bytes', |
| 60 | 'packets', |
| 61 | 'duration', |
| 62 | |
| 63 | 'priority', |
| 64 | 'tableId', |
| 65 | 'appId', |
| 66 | |
| 67 | 'groupId', |
| 68 | 'timeout', |
| 69 | 'permanent' |
Viswanath KSP | 70e0d14 | 2016-10-28 21:58:32 +0530 | [diff] [blame] | 70 | ], |
| 71 | friendlyProps = [ |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame] | 72 | 'Flow ID', |
| 73 | 'State', |
| 74 | |
| 75 | 'Bytes', |
| 76 | 'Packets', |
| 77 | 'Duration', |
| 78 | |
| 79 | 'Flow Priority', |
| 80 | 'Table ID', |
| 81 | 'App ID', |
| 82 | |
| 83 | 'Group ID', |
| 84 | 'Timeout', |
| 85 | 'Permanent' |
Viswanath KSP | 70e0d14 | 2016-10-28 21:58:32 +0530 | [diff] [blame] | 86 | ]; |
Viswanath KSP | 7bdc917 | 2016-10-19 20:19:17 +0530 | [diff] [blame] | 87 | |
| 88 | function closePanel() { |
| 89 | if (detailsPanel.isVisible()) { |
| 90 | $scope.selId = null; |
| 91 | detailsPanel.hide(); |
| 92 | return true; |
| 93 | } |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | function addCloseBtn(div) { |
| 98 | is.loadEmbeddedIcon(div, 'close', 20); |
| 99 | div.on('click', closePanel); |
| 100 | } |
| 101 | |
Viswanath KSP | 70e0d14 | 2016-10-28 21:58:32 +0530 | [diff] [blame] | 102 | function handleEscape() { |
| 103 | return closePanel(); |
| 104 | } |
| 105 | |
Viswanath KSP | 7bdc917 | 2016-10-19 20:19:17 +0530 | [diff] [blame] | 106 | function setUpPanel() { |
| 107 | var container, closeBtn, tblDiv; |
| 108 | detailsPanel.empty(); |
| 109 | |
| 110 | container = detailsPanel.append('div').classed('container', true); |
Viswanath KSP | 7bdc917 | 2016-10-19 20:19:17 +0530 | [diff] [blame] | 111 | top = container.append('div').classed('top', true); |
Viswanath KSP | ed55ecf | 2016-12-22 18:15:45 +0530 | [diff] [blame] | 112 | selDiv = container.append('div').classed('top', true); |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame] | 113 | trmtDiv = container.append('div').classed('top', true); |
Viswanath KSP | 7bdc917 | 2016-10-19 20:19:17 +0530 | [diff] [blame] | 114 | closeBtn = top.append('div').classed('close-btn', true); |
| 115 | addCloseBtn(closeBtn); |
| 116 | iconDiv = top.append('div').classed('dev-icon', true); |
| 117 | top.append('h2'); |
Viswanath KSP | 70e0d14 | 2016-10-28 21:58:32 +0530 | [diff] [blame] | 118 | topTable = top.append('div').classed('top-content', true) |
| 119 | .append('table'); |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame] | 120 | |
Viswanath KSP | 7bdc917 | 2016-10-19 20:19:17 +0530 | [diff] [blame] | 121 | top.append('hr'); |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame] | 122 | |
Simon Hunt | 239f09e | 2017-05-18 13:10:09 -0700 | [diff] [blame] | 123 | selDiv.append('h2').text('Selector'); |
Viswanath KSP | ed55ecf | 2016-12-22 18:15:45 +0530 | [diff] [blame] | 124 | topSelTable = selDiv.append('div').classed('top-content', true) |
| 125 | .append('table'); |
| 126 | |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame] | 127 | selDiv.append('hr'); |
Viswanath KSP | 7bdc917 | 2016-10-19 20:19:17 +0530 | [diff] [blame] | 128 | |
Simon Hunt | 9bff595 | 2017-07-13 14:05:09 -0700 | [diff] [blame] | 129 | trmtDiv.append('h2').text('Treatment'); |
| 130 | topTrmtTable = trmtDiv.append('div').classed('top-content', true) |
| 131 | .append('table'); |
Viswanath KSP | 7bdc917 | 2016-10-19 20:19:17 +0530 | [diff] [blame] | 132 | } |
| 133 | |
Viswanath KSP | 70e0d14 | 2016-10-28 21:58:32 +0530 | [diff] [blame] | 134 | function addProp(tbody, index, value) { |
| 135 | var tr = tbody.append('tr'); |
Viswanath KSP | 7bdc917 | 2016-10-19 20:19:17 +0530 | [diff] [blame] | 136 | |
Viswanath KSP | 70e0d14 | 2016-10-28 21:58:32 +0530 | [diff] [blame] | 137 | function addCell(cls, txt) { |
Simon Hunt | 239f09e | 2017-05-18 13:10:09 -0700 | [diff] [blame] | 138 | tr.append('td').attr('class', cls).text(txt); |
Viswanath KSP | 70e0d14 | 2016-10-28 21:58:32 +0530 | [diff] [blame] | 139 | } |
| 140 | addCell('label', friendlyProps[index] + ' :'); |
| 141 | addCell('value', value); |
| 142 | } |
| 143 | |
Viswanath KSP | ed55ecf | 2016-12-22 18:15:45 +0530 | [diff] [blame] | 144 | function populateTable(tbody, label, value) { |
| 145 | var tr = tbody.append('tr'); |
| 146 | |
| 147 | function addCell(cls, txt) { |
Simon Hunt | 239f09e | 2017-05-18 13:10:09 -0700 | [diff] [blame] | 148 | tr.append('td').attr('class', cls).text(txt); |
Viswanath KSP | ed55ecf | 2016-12-22 18:15:45 +0530 | [diff] [blame] | 149 | } |
| 150 | addCell('label', label + ' :'); |
| 151 | addCell('value', value); |
| 152 | } |
| 153 | |
Viswanath KSP | 70e0d14 | 2016-10-28 21:58:32 +0530 | [diff] [blame] | 154 | function populateTop(details) { |
| 155 | is.loadEmbeddedIcon(iconDiv, 'flowTable', 40); |
Simon Hunt | 239f09e | 2017-05-18 13:10:09 -0700 | [diff] [blame] | 156 | top.select('h2').text(details.flowId); |
Viswanath KSP | 70e0d14 | 2016-10-28 21:58:32 +0530 | [diff] [blame] | 157 | |
| 158 | var tbody = topTable.append('tbody'); |
| 159 | |
Viswanath KSP | ed55ecf | 2016-12-22 18:15:45 +0530 | [diff] [blame] | 160 | var topSelTablebody = topSelTable.append('tbody'); |
| 161 | var selectorString = details['selector']; |
| 162 | var selectors = selectorString.split(','); |
| 163 | |
| 164 | var topTrmtTablebody = topTrmtTable.append('tbody'); |
| 165 | var treatmentString = details['treatment']; |
| 166 | var treatment = treatmentString.split(','); |
| 167 | |
Viswanath KSP | 70e0d14 | 2016-10-28 21:58:32 +0530 | [diff] [blame] | 168 | propOrder.forEach(function (prop, i) { |
| 169 | addProp(tbody, i, details[prop]); |
| 170 | }); |
Viswanath KSP | ed55ecf | 2016-12-22 18:15:45 +0530 | [diff] [blame] | 171 | |
| 172 | selectors.forEach(function (sel) { |
Jian Li | 70c460a | 2017-05-11 02:26:07 +0900 | [diff] [blame] | 173 | var selArray = sel.match(/^([^:]*):([\s\S]*)/); |
| 174 | populateTable(topSelTablebody, selArray[1], selArray[2]); |
Viswanath KSP | ed55ecf | 2016-12-22 18:15:45 +0530 | [diff] [blame] | 175 | }); |
| 176 | |
| 177 | treatment.forEach(function (sel) { |
Jian Li | 70c460a | 2017-05-11 02:26:07 +0900 | [diff] [blame] | 178 | var selArray = sel.match(/^([^:]*):([\s\S]*)/); |
| 179 | populateTable(topTrmtTable, selArray[1], selArray[2]); |
Viswanath KSP | ed55ecf | 2016-12-22 18:15:45 +0530 | [diff] [blame] | 180 | }); |
| 181 | |
Viswanath KSP | 7bdc917 | 2016-10-19 20:19:17 +0530 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | function createDetailsPane() { |
| 185 | detailsPanel = ps.createPanel(pName, { |
| 186 | width: wSize.width, |
| 187 | margin: 0, |
| 188 | hideMargin: 0 |
| 189 | }); |
| 190 | detailsPanel.el().style({ |
| 191 | position: 'absolute', |
| 192 | top: pStartY + 'px' |
| 193 | }); |
| 194 | $scope.hidePanel = function () { detailsPanel.hide(); }; |
| 195 | detailsPanel.hide(); |
| 196 | } |
| 197 | |
| 198 | function populateDetails(details) { |
Viswanath KSP | 7bdc917 | 2016-10-19 20:19:17 +0530 | [diff] [blame] | 199 | setUpPanel(); |
| 200 | populateTop(details); |
| 201 | |
| 202 | //ToDo add more details |
| 203 | detailsPanel.height(pHeight); |
| 204 | detailsPanel.width(wtPdg); |
Viswanath KSP | 7bdc917 | 2016-10-19 20:19:17 +0530 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | function respDetailsCb(data) { |
Viswanath KSP | 70e0d14 | 2016-10-28 21:58:32 +0530 | [diff] [blame] | 208 | $log.debug("Got response from server :", data); |
| 209 | $scope.panelData = data.details; |
| 210 | $scope.$apply(); |
Viswanath KSP | 7bdc917 | 2016-10-19 20:19:17 +0530 | [diff] [blame] | 211 | } |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 212 | |
Simon Hunt | 1002cd8 | 2015-04-23 14:44:03 -0700 | [diff] [blame] | 213 | angular.module('ovFlow', []) |
| 214 | .controller('OvFlowCtrl', |
Bri Prebilic Cole | 3d4d01c | 2015-04-30 13:48:36 -0700 | [diff] [blame] | 215 | ['$log', '$scope', '$location', |
Bri Prebilic Cole | 9b1fb9a | 2015-07-01 13:57:11 -0700 | [diff] [blame] | 216 | 'FnService', 'TableBuilderService', 'NavService', |
Viswanath KSP | 7bdc917 | 2016-10-19 20:19:17 +0530 | [diff] [blame] | 217 | 'MastService', 'PanelService', 'KeyService', 'IconService', |
| 218 | 'WebSocketService', |
Simon Hunt | 1002cd8 | 2015-04-23 14:44:03 -0700 | [diff] [blame] | 219 | |
Viswanath KSP | 7bdc917 | 2016-10-19 20:19:17 +0530 | [diff] [blame] | 220 | function (_$log_, _$scope_, _$location_, _fs_, _tbs_, _ns_, |
| 221 | _mast_, _ps_, _ks_, _is_, _wss_) { |
| 222 | var params, |
| 223 | handlers = {}; |
| 224 | |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 225 | $log = _$log_; |
| 226 | $scope = _$scope_; |
| 227 | $location = _$location_; |
| 228 | fs = _fs_; |
| 229 | tbs = _tbs_; |
Bri Prebilic Cole | 9b1fb9a | 2015-07-01 13:57:11 -0700 | [diff] [blame] | 230 | ns = _ns_; |
Viswanath KSP | 7bdc917 | 2016-10-19 20:19:17 +0530 | [diff] [blame] | 231 | is = _is_; |
| 232 | wss = _wss_; |
| 233 | mast = _mast_; |
| 234 | ps = _ps_; |
Simon Hunt | 20856ec | 2015-11-16 15:58:14 -0800 | [diff] [blame] | 235 | $scope.deviceTip = 'Show device table'; |
Bri Prebilic Cole | eef67ae | 2015-07-01 16:26:59 -0700 | [diff] [blame] | 236 | $scope.portTip = 'Show port view for this device'; |
| 237 | $scope.groupTip = 'Show group view for this device'; |
Jian Li | 1f54473 | 2015-12-30 23:36:37 -0800 | [diff] [blame] | 238 | $scope.meterTip = 'Show meter view for selected device'; |
Kavitha Alagesan | eaf614c | 2016-08-22 23:46:05 -0700 | [diff] [blame] | 239 | $scope.briefTip = 'Switch to brief view'; |
| 240 | $scope.detailTip = 'Switch to detailed view'; |
| 241 | $scope.brief = true; |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 242 | params = $location.search(); |
| 243 | if (params.hasOwnProperty('devId')) { |
Bri Prebilic Cole | e568ead | 2015-05-01 09:51:28 -0700 | [diff] [blame] | 244 | $scope.devId = params['devId']; |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | tbs.buildTable({ |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 248 | scope: $scope, |
| 249 | tag: 'flow', |
Viswanath KSP | 7bdc917 | 2016-10-19 20:19:17 +0530 | [diff] [blame] | 250 | selCb: selCb, |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 251 | query: params |
| 252 | }); |
Bri Prebilic Cole | 3d4d01c | 2015-04-30 13:48:36 -0700 | [diff] [blame] | 253 | |
Bri Prebilic Cole | 9b1fb9a | 2015-07-01 13:57:11 -0700 | [diff] [blame] | 254 | $scope.nav = function (path) { |
| 255 | if ($scope.devId) { |
| 256 | ns.navTo(path, { devId: $scope.devId }); |
| 257 | } |
| 258 | }; |
| 259 | |
Viswanath KSP | 7bdc917 | 2016-10-19 20:19:17 +0530 | [diff] [blame] | 260 | // details panel handlers |
| 261 | handlers[detailsResp] = respDetailsCb; |
| 262 | wss.bindHandlers(handlers); |
| 263 | |
| 264 | function selCb($event, row) { |
| 265 | if ($scope.selId) { |
| 266 | wss.sendEvent(detailsReq, {flowId: row.id, appId: row.appId}); |
Viswanath KSP | 7bdc917 | 2016-10-19 20:19:17 +0530 | [diff] [blame] | 267 | } else { |
| 268 | $scope.hidePanel(); |
| 269 | } |
| 270 | $log.debug('Got a click on:', row); |
| 271 | } |
| 272 | |
Viswanath KSP | 70e0d14 | 2016-10-28 21:58:32 +0530 | [diff] [blame] | 273 | $scope.$on('$destroy', function () { |
| 274 | wss.unbindHandlers(handlers); |
| 275 | }); |
| 276 | |
Kavitha Alagesan | eaf614c | 2016-08-22 23:46:05 -0700 | [diff] [blame] | 277 | $scope.briefToggle = function () { |
| 278 | $scope.brief = !$scope.brief; |
| 279 | }; |
| 280 | |
sisubram | 9ada8d7 | 2016-09-02 13:54:40 +0530 | [diff] [blame] | 281 | Object.defineProperty($scope, "queryFilter", { |
| 282 | get: function() { |
Simon Hunt | c8f06d9 | 2016-09-28 17:28:26 -0700 | [diff] [blame] | 283 | var out = {}; |
| 284 | out[$scope.queryBy || "$"] = $scope.queryTxt; |
| 285 | return out; |
sisubram | 9ada8d7 | 2016-09-02 13:54:40 +0530 | [diff] [blame] | 286 | } |
| 287 | }); |
| 288 | |
Simon Hunt | 1002cd8 | 2015-04-23 14:44:03 -0700 | [diff] [blame] | 289 | $log.log('OvFlowCtrl has been created'); |
Viswanath KSP | 7bdc917 | 2016-10-19 20:19:17 +0530 | [diff] [blame] | 290 | }]) |
| 291 | |
| 292 | .directive('flowDetailsPanel', |
| 293 | ['$rootScope', '$window', '$timeout', 'KeyService', |
| 294 | function ($rootScope, $window, $timeout, ks) { |
| 295 | return function (scope) { |
| 296 | var unbindWatch; |
| 297 | |
| 298 | function heightCalc() { |
| 299 | pStartY = fs.noPxStyle(d3.select('.tabular-header'), 'height') |
| 300 | + mast.mastHeight() + topPdg; |
| 301 | wSize = fs.windowSize(pStartY); |
| 302 | pHeight = wSize.height; |
| 303 | } |
| 304 | |
| 305 | function initPanel() { |
| 306 | heightCalc(); |
| 307 | createDetailsPane(); |
| 308 | } |
| 309 | |
| 310 | // Safari has a bug where it renders the fixed-layout table wrong |
| 311 | // if you ask for the window's size too early |
| 312 | if (scope.onos.browser === 'safari') { |
| 313 | $timeout(initPanel); |
| 314 | } else { |
| 315 | initPanel(); |
| 316 | } |
Viswanath KSP | 70e0d14 | 2016-10-28 21:58:32 +0530 | [diff] [blame] | 317 | // create key bindings to handle panel |
| 318 | ks.keyBindings({ |
| 319 | esc: [handleEscape, 'Close the details panel'], |
| 320 | _helpFormat: ['esc'] |
| 321 | }); |
| 322 | ks.gestureNotes([ |
| 323 | ['click', 'Select a row to show cluster node details'], |
| 324 | ['scroll down', 'See available cluster nodes'] |
| 325 | ]); |
| 326 | // if the panelData changes |
| 327 | scope.$watch('panelData', function () { |
| 328 | if (!fs.isEmptyObject(scope.panelData)) { |
| 329 | populateDetails(scope.panelData); |
| 330 | detailsPanel.show(); |
| 331 | } |
| 332 | }); |
Viswanath KSP | 7bdc917 | 2016-10-19 20:19:17 +0530 | [diff] [blame] | 333 | // if the window size changes |
| 334 | unbindWatch = $rootScope.$watchCollection( |
| 335 | function () { |
| 336 | return { |
| 337 | h: $window.innerHeight, |
| 338 | w: $window.innerWidth |
| 339 | }; |
| 340 | }, function () { |
| 341 | if (!fs.isEmptyObject(scope.panelData)) { |
| 342 | heightCalc(); |
| 343 | populateDetails(scope.panelData); |
| 344 | } |
| 345 | } |
| 346 | ); |
| 347 | |
| 348 | scope.$on('$destroy', function () { |
| 349 | unbindWatch(); |
| 350 | ps.destroyPanel(pName); |
| 351 | }); |
| 352 | }; |
| 353 | }]); |
| 354 | |
Simon Hunt | 1002cd8 | 2015-04-23 14:44:03 -0700 | [diff] [blame] | 355 | }()); |