Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -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 -- Cluster View Module |
| 19 | */ |
| 20 | |
| 21 | (function () { |
| 22 | 'use strict'; |
| 23 | |
Simon Hunt | 6b5a941 | 2016-10-26 19:32:04 -0700 | [diff] [blame] | 24 | // injected references |
| 25 | var $log, $scope, $location, fs, tbs, ns, mast, ps, wss, is, ks; |
Viswanath KSP | 9fcf970 | 2016-10-23 17:13:29 +0530 | [diff] [blame] | 26 | |
Simon Hunt | 6b5a941 | 2016-10-26 19:32:04 -0700 | [diff] [blame] | 27 | // internal state |
| 28 | var detailsPanel, |
| 29 | pStartY, |
| 30 | pHeight, |
| 31 | top, |
| 32 | topTable, |
| 33 | bottom, |
| 34 | iconDiv, |
| 35 | nameDiv, |
| 36 | wSize; |
Viswanath KSP | 9fcf970 | 2016-10-23 17:13:29 +0530 | [diff] [blame] | 37 | |
| 38 | |
Simon Hunt | 6b5a941 | 2016-10-26 19:32:04 -0700 | [diff] [blame] | 39 | // constants |
| 40 | var topPdg = 28, |
| 41 | ctnrPdg = 24, |
| 42 | scrollSize = 17, |
| 43 | portsTblPdg = 50, |
| 44 | htPdg = 479, |
| 45 | wtPdg = 532, |
Viswanath KSP | 9fcf970 | 2016-10-23 17:13:29 +0530 | [diff] [blame] | 46 | |
Simon Hunt | 6b5a941 | 2016-10-26 19:32:04 -0700 | [diff] [blame] | 47 | pName = 'cluster-details-panel', |
| 48 | detailsReq = 'clusterDetailsRequest', |
| 49 | detailsResp = 'clusterDetailsResponse', |
Viswanath KSP | 9fcf970 | 2016-10-23 17:13:29 +0530 | [diff] [blame] | 50 | |
Simon Hunt | 6b5a941 | 2016-10-26 19:32:04 -0700 | [diff] [blame] | 51 | propOrder = [ |
| 52 | 'id', 'ip' |
| 53 | ], |
| 54 | friendlyProps = [ |
| 55 | 'Node ID', 'IP Address' |
Viswanath KSP | 116f0c1 | 2016-11-21 00:02:51 +0530 | [diff] [blame] | 56 | ], |
| 57 | deviceCols = [ |
| 58 | 'id', 'type', 'chassisid', 'mfr', |
| 59 | 'hw', 'sw', 'protocol', 'serial' |
| 60 | ], |
| 61 | friendlyDeviceCols = [ |
| 62 | 'URI', 'Type', 'Chassis ID', 'Vendor', |
| 63 | 'H/W Version', 'S/W Version', 'Protocol', |
| 64 | 'Serial #' |
Simon Hunt | 6b5a941 | 2016-10-26 19:32:04 -0700 | [diff] [blame] | 65 | ]; |
Viswanath KSP | 9fcf970 | 2016-10-23 17:13:29 +0530 | [diff] [blame] | 66 | |
Simon Hunt | 6b5a941 | 2016-10-26 19:32:04 -0700 | [diff] [blame] | 67 | function closePanel() { |
| 68 | if (detailsPanel.isVisible()) { |
| 69 | $scope.selId = null; |
Viswanath KSP | 9fcf970 | 2016-10-23 17:13:29 +0530 | [diff] [blame] | 70 | detailsPanel.hide(); |
Simon Hunt | 6b5a941 | 2016-10-26 19:32:04 -0700 | [diff] [blame] | 71 | return true; |
Viswanath KSP | 9fcf970 | 2016-10-23 17:13:29 +0530 | [diff] [blame] | 72 | } |
Simon Hunt | 6b5a941 | 2016-10-26 19:32:04 -0700 | [diff] [blame] | 73 | return false; |
| 74 | } |
Viswanath KSP | 9fcf970 | 2016-10-23 17:13:29 +0530 | [diff] [blame] | 75 | |
Simon Hunt | 6b5a941 | 2016-10-26 19:32:04 -0700 | [diff] [blame] | 76 | function addCloseBtn(div) { |
| 77 | is.loadEmbeddedIcon(div, 'close', 20); |
| 78 | div.on('click', closePanel); |
| 79 | } |
Viswanath KSP | 9fcf970 | 2016-10-23 17:13:29 +0530 | [diff] [blame] | 80 | |
Viswanath KSP | 8601627 | 2016-10-27 09:49:46 +0530 | [diff] [blame] | 81 | function handleEscape() { |
| 82 | return closePanel(); |
| 83 | } |
| 84 | |
Simon Hunt | 6b5a941 | 2016-10-26 19:32:04 -0700 | [diff] [blame] | 85 | function setUpPanel() { |
| 86 | var container, closeBtn, tblDiv; |
| 87 | detailsPanel.empty(); |
Viswanath KSP | 9fcf970 | 2016-10-23 17:13:29 +0530 | [diff] [blame] | 88 | |
Simon Hunt | 6b5a941 | 2016-10-26 19:32:04 -0700 | [diff] [blame] | 89 | container = detailsPanel.append('div').classed('container', true); |
Viswanath KSP | 9fcf970 | 2016-10-23 17:13:29 +0530 | [diff] [blame] | 90 | |
Simon Hunt | 6b5a941 | 2016-10-26 19:32:04 -0700 | [diff] [blame] | 91 | top = container.append('div').classed('top', true); |
| 92 | closeBtn = top.append('div').classed('close-btn', true); |
| 93 | addCloseBtn(closeBtn); |
| 94 | iconDiv = top.append('div').classed('dev-icon', true); |
| 95 | top.append('h2'); |
| 96 | topTable = top.append('div').classed('top-content', true) |
| 97 | .append('table'); |
| 98 | top.append('hr'); |
| 99 | |
Viswanath KSP | 116f0c1 | 2016-11-21 00:02:51 +0530 | [diff] [blame] | 100 | bottom = container.append('div').classed('bottom', true); |
| 101 | bottom.append('h2').classed('devices-title', true).html('Devices'); |
| 102 | bottom.append('table'); |
Simon Hunt | 6b5a941 | 2016-10-26 19:32:04 -0700 | [diff] [blame] | 103 | //ToDo add more details |
| 104 | } |
| 105 | |
| 106 | function addProp(tbody, index, value) { |
| 107 | var tr = tbody.append('tr'); |
| 108 | |
| 109 | function addCell(cls, txt) { |
| 110 | tr.append('td').attr('class', cls).html(txt); |
Viswanath KSP | 9fcf970 | 2016-10-23 17:13:29 +0530 | [diff] [blame] | 111 | } |
Simon Hunt | 6b5a941 | 2016-10-26 19:32:04 -0700 | [diff] [blame] | 112 | addCell('label', friendlyProps[index] + ' :'); |
| 113 | addCell('value', value); |
| 114 | } |
Viswanath KSP | 9fcf970 | 2016-10-23 17:13:29 +0530 | [diff] [blame] | 115 | |
Simon Hunt | 6b5a941 | 2016-10-26 19:32:04 -0700 | [diff] [blame] | 116 | function populateTop(details) { |
| 117 | is.loadEmbeddedIcon(iconDiv, 'node', 40); |
| 118 | top.select('h2').html(details.id); |
| 119 | |
| 120 | var tbody = topTable.append('tbody'); |
| 121 | |
| 122 | propOrder.forEach(function (prop, i) { |
| 123 | addProp(tbody, i, details[prop]); |
| 124 | }); |
| 125 | } |
| 126 | |
Viswanath KSP | 116f0c1 | 2016-11-21 00:02:51 +0530 | [diff] [blame] | 127 | function addDeviceRow(tbody, device) { |
| 128 | var tr = tbody.append('tr'); |
| 129 | |
| 130 | deviceCols.forEach(function (col) { |
| 131 | tr.append('td').html(device[col]); |
| 132 | }); |
| 133 | } |
| 134 | |
| 135 | function populateBottom(devices) { |
| 136 | var table = bottom.select('table'), |
| 137 | theader = table.append('thead').append('tr'), |
| 138 | tbody = table.append('tbody'), |
| 139 | tbWidth, tbHeight; |
| 140 | |
| 141 | friendlyDeviceCols.forEach(function (col) { |
| 142 | theader.append('th').html(col); |
| 143 | }); |
| 144 | devices.forEach(function (device) { |
| 145 | addDeviceRow(tbody, device); |
| 146 | }); |
| 147 | |
| 148 | tbWidth = fs.noPxStyle(tbody, 'width') + scrollSize; |
| 149 | tbHeight = pHeight |
| 150 | - (fs.noPxStyle(detailsPanel.el() |
| 151 | .select('.top'), 'height') |
| 152 | + fs.noPxStyle(detailsPanel.el() |
| 153 | .select('.devices-title'), 'height') |
| 154 | + portsTblPdg); |
| 155 | |
| 156 | table.style({ |
| 157 | height: tbHeight + 'px', |
| 158 | width: tbWidth + 'px', |
| 159 | overflow: 'auto', |
| 160 | display: 'block' |
| 161 | }); |
| 162 | |
| 163 | detailsPanel.width(tbWidth + ctnrPdg); |
| 164 | } |
| 165 | |
Simon Hunt | 6b5a941 | 2016-10-26 19:32:04 -0700 | [diff] [blame] | 166 | function createDetailsPane() { |
| 167 | detailsPanel = ps.createPanel(pName, { |
| 168 | width: wSize.width, |
| 169 | margin: 0, |
| 170 | hideMargin: 0 |
| 171 | }); |
| 172 | detailsPanel.el().style({ |
| 173 | position: 'absolute', |
| 174 | top: pStartY + 'px' |
| 175 | }); |
| 176 | $scope.hidePanel = function () { detailsPanel.hide(); }; |
| 177 | detailsPanel.hide(); |
| 178 | } |
| 179 | |
| 180 | function populateDetails(details) { |
| 181 | setUpPanel(); |
Viswanath KSP | 116f0c1 | 2016-11-21 00:02:51 +0530 | [diff] [blame] | 182 | |
Simon Hunt | 6b5a941 | 2016-10-26 19:32:04 -0700 | [diff] [blame] | 183 | populateTop(details); |
Viswanath KSP | 116f0c1 | 2016-11-21 00:02:51 +0530 | [diff] [blame] | 184 | populateBottom(details.devices); |
Simon Hunt | 6b5a941 | 2016-10-26 19:32:04 -0700 | [diff] [blame] | 185 | |
| 186 | //ToDo add more details |
| 187 | detailsPanel.height(pHeight); |
Viswanath KSP | 116f0c1 | 2016-11-21 00:02:51 +0530 | [diff] [blame] | 188 | //detailsPanel.width(wtPdg); ToDO Use this when needed! |
Simon Hunt | 6b5a941 | 2016-10-26 19:32:04 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | function respDetailsCb(data) { |
| 192 | $scope.panelData = data.details; |
| 193 | $scope.$apply(); |
| 194 | } |
Viswanath KSP | 9fcf970 | 2016-10-23 17:13:29 +0530 | [diff] [blame] | 195 | |
| 196 | |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 197 | angular.module('ovCluster', []) |
| 198 | .controller('OvClusterCtrl', |
Viswanath KSP | 9fcf970 | 2016-10-23 17:13:29 +0530 | [diff] [blame] | 199 | ['$log', '$scope', 'TableBuilderService', 'NavService', 'MastService', |
| 200 | 'PanelService', 'KeyService', 'IconService','WebSocketService', |
| 201 | 'FnService', |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 202 | |
Simon Hunt | 6b5a941 | 2016-10-26 19:32:04 -0700 | [diff] [blame] | 203 | function (_$log_, _$scope_, tbs, _ns_, _mast_, _ps_, _ks_, _is_, |
| 204 | _wss_, _fs_) { |
| 205 | var handlers = {}; |
Viswanath KSP | 9fcf970 | 2016-10-23 17:13:29 +0530 | [diff] [blame] | 206 | |
Simon Hunt | 6b5a941 | 2016-10-26 19:32:04 -0700 | [diff] [blame] | 207 | $log = _$log_; |
| 208 | $scope = _$scope_; |
| 209 | fs = _fs_; |
| 210 | ns = _ns_; |
| 211 | is = _is_; |
| 212 | wss = _wss_; |
| 213 | mast = _mast_; |
| 214 | ps = _ps_; |
Viswanath KSP | 9fcf970 | 2016-10-23 17:13:29 +0530 | [diff] [blame] | 215 | |
Simon Hunt | 6b5a941 | 2016-10-26 19:32:04 -0700 | [diff] [blame] | 216 | $scope.panelData = {}; |
Viswanath KSP | 9fcf970 | 2016-10-23 17:13:29 +0530 | [diff] [blame] | 217 | |
Simon Hunt | 6b5a941 | 2016-10-26 19:32:04 -0700 | [diff] [blame] | 218 | tbs.buildTable({ |
| 219 | scope: $scope, |
| 220 | selCb: selCb, |
| 221 | tag: 'cluster' |
Viswanath KSP | 9fcf970 | 2016-10-23 17:13:29 +0530 | [diff] [blame] | 222 | }); |
| 223 | |
Simon Hunt | 6b5a941 | 2016-10-26 19:32:04 -0700 | [diff] [blame] | 224 | // details panel handlers |
| 225 | handlers[detailsResp] = respDetailsCb; |
| 226 | wss.bindHandlers(handlers); |
Viswanath KSP | 9fcf970 | 2016-10-23 17:13:29 +0530 | [diff] [blame] | 227 | |
Simon Hunt | 6b5a941 | 2016-10-26 19:32:04 -0700 | [diff] [blame] | 228 | function selCb($event, row) { |
| 229 | if ($scope.selId) { |
| 230 | wss.sendEvent(detailsReq, {id: row.id}); |
| 231 | } else { |
| 232 | $scope.hidePanel(); |
| 233 | } |
| 234 | $log.debug('Got a click on:', row); |
| 235 | } |
| 236 | |
| 237 | $scope.$on('$destroy', function () { |
| 238 | wss.unbindHandlers(handlers); |
| 239 | }); |
| 240 | |
| 241 | $log.log('OvClusterCtrl has been created'); |
| 242 | }]) |
Viswanath KSP | 9fcf970 | 2016-10-23 17:13:29 +0530 | [diff] [blame] | 243 | |
Simon Hunt | a271e0a | 2016-10-26 10:59:14 -0700 | [diff] [blame] | 244 | .directive('clusterDetailsPanel', |
Viswanath KSP | 9fcf970 | 2016-10-23 17:13:29 +0530 | [diff] [blame] | 245 | ['$rootScope', '$window', '$timeout', 'KeyService', |
| 246 | function ($rootScope, $window, $timeout, ks) { |
| 247 | return function (scope) { |
| 248 | var unbindWatch; |
| 249 | |
| 250 | function heightCalc() { |
| 251 | pStartY = fs.noPxStyle(d3.select('.tabular-header'), 'height') |
| 252 | + mast.mastHeight() + topPdg; |
| 253 | wSize = fs.windowSize(pStartY); |
| 254 | pHeight = wSize.height; |
| 255 | } |
| 256 | |
| 257 | function initPanel() { |
| 258 | heightCalc(); |
| 259 | createDetailsPane(); |
| 260 | } |
| 261 | |
| 262 | // Safari has a bug where it renders the fixed-layout table wrong |
| 263 | // if you ask for the window's size too early |
| 264 | if (scope.onos.browser === 'safari') { |
| 265 | $timeout(initPanel); |
| 266 | } else { |
| 267 | initPanel(); |
| 268 | } |
Viswanath KSP | 8601627 | 2016-10-27 09:49:46 +0530 | [diff] [blame] | 269 | // create key bindings to handle panel |
| 270 | ks.keyBindings({ |
| 271 | esc: [handleEscape, 'Close the details panel'], |
| 272 | _helpFormat: ['esc'] |
| 273 | }); |
| 274 | ks.gestureNotes([ |
| 275 | ['click', 'Select a row to show cluster node details'], |
| 276 | ['scroll down', 'See available cluster nodes'] |
| 277 | ]); |
Simon Hunt | a271e0a | 2016-10-26 10:59:14 -0700 | [diff] [blame] | 278 | // if the panelData changes |
| 279 | scope.$watch('panelData', function () { |
| 280 | if (!fs.isEmptyObject(scope.panelData)) { |
| 281 | populateDetails(scope.panelData); |
| 282 | detailsPanel.show(); |
| 283 | } |
| 284 | }); |
| 285 | |
Viswanath KSP | 9fcf970 | 2016-10-23 17:13:29 +0530 | [diff] [blame] | 286 | // if the window size changes |
| 287 | unbindWatch = $rootScope.$watchCollection( |
| 288 | function () { |
| 289 | return { |
| 290 | h: $window.innerHeight, |
| 291 | w: $window.innerWidth |
| 292 | }; |
| 293 | }, function () { |
| 294 | if (!fs.isEmptyObject(scope.panelData)) { |
| 295 | heightCalc(); |
| 296 | populateDetails(scope.panelData); |
| 297 | } |
| 298 | } |
| 299 | ); |
| 300 | |
| 301 | scope.$on('$destroy', function () { |
| 302 | unbindWatch(); |
| 303 | ps.destroyPanel(pName); |
| 304 | }); |
| 305 | }; |
| 306 | }]); |
Bri Prebilic Cole | 384e8dc | 2015-04-13 15:51:14 -0700 | [diff] [blame] | 307 | }()); |