blob: fbe5dbba5fc1ccad03fcb9f226c8c94a61f40f23 [file] [log] [blame]
Bri Prebilic Cole7c92a3d2015-01-09 16:50:03 -08001/*
2 * Copyright 2015 Open Networking Laboratory
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/*
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
25 var $log, $scope, fs, mast, ps, wss, is;
26
27 // internal state
28 var self,
29 detailsPane,
30 container, top, bottom, closeBtn;
31
32 // constants
33 // TODO: consider having a set y height that all tables start at
34 var h2Pdg = 40,
35 mastPdg = 8,
36 tbodyPdg = 5,
37 cntrPdg = 24,
38 pName = 'device-details-panel',
39 detailsReq = 'deviceDetailsRequest',
40 detailsResp = 'deviceDetailsResponse',
41 propOrder = [
42 'type', 'masterid', 'chassisid',
43 'mfr', 'hw', 'sw', 'protocol', 'serial'
44 ],
45 friendlyProps = [
46 'Type', 'Master ID', 'Chassis ID',
47 'Vendor', 'H/W Version', 'S/W Version', 'Protocol', 'Serial #'
48 ],
49 portCols = [
50 'enabled', 'id', 'speed', 'type', 'elinks_dest'
51 ],
52 friendlyPortCols = [
53 'Enabled', 'ID', 'Speed', 'Type', 'Egress Links'
54 ];
55
56 function setUpPanel() {
57 detailsPane.empty();
58
59 container = detailsPane.append('div').classed('container', true);
60
61 top = container.append('div').classed('top', true);
62 closeBtn = top.append('div').classed('close-btn', true);
63 addCloseBtn(closeBtn);
64 top.append('h2');
65 top.append('table');
66
67 container.append('hr');
68
69 bottom = container.append('div').classed('bottom', true);
70 bottom.append('h2').text('Ports');
71 bottom.append('table');
72 }
73
74 function createDetailsPane() {
75 var headerHeight = h2Pdg + fs.noPxStyle(d3.select('h2'), 'height'),
76 panelTop = headerHeight + tbodyPdg + mast.mastHeight() + mastPdg,
77 wSize = fs.windowSize(panelTop);
78
79 detailsPane = ps.createPanel(pName, {
80 height: wSize.height,
81 width: wSize.width / 2,
82 margin: 0,
83 hideMargin: 0
84 });
85
86 detailsPane.el().style({
87 position: 'absolute',
88 top: panelTop + 'px'
89 });
90
91 setUpPanel();
92
93 detailsPane.hide();
94 }
95
96 function addCloseBtn(div) {
97 is.loadEmbeddedIcon(div, 'appPlus', 30);
98 div.select('g').attr('transform', 'translate(25, 0) rotate(45)');
99 div.on('click', function () {
100 detailsPane.hide();
101 // TODO: deselect the table row when button is clicked
102 //$scope.sel = null;
103 });
104 }
105
106 function populateTopHalf(tbody, details) {
107 top.select('h2').text(details['id']);
108
109 propOrder.forEach(function (prop, i) {
110 addProp(tbody, i, details[prop]);
111 });
112 }
113
114 function populateBottomHalf(table, ports) {
115 var theader = table.append('thead').append('tr'),
116 tbody = table.append('tbody'),
117 tbWidth, tbHeight,
118 scrollSize = 20,
119 btmPdg = 50;
120
121 friendlyPortCols.forEach(function (header) {
122 theader.append('th').html(header);
123 });
124 ports.forEach(function (port) {
125 addPortRow(tbody, port);
126 });
127
128 tbWidth = fs.noPxStyle(tbody, 'width') + scrollSize;
129 tbHeight = detailsPane.height()
130 - (fs.noPxStyle(detailsPane.el().select('.top'), 'height')
131 + fs.noPxStyle(detailsPane.el().select('hr'), 'height')
132 + fs.noPxStyle(detailsPane.el().select('h2'), 'height')
133 + btmPdg);
134
135 table.style({
136 height: tbHeight + 'px',
137 width: tbWidth + 'px',
138 overflow: 'auto',
139 display: 'block'
140 });
141
142 detailsPane.width(tbWidth + cntrPdg);
143 }
144
145 function addProp(tbody, index, value) {
146 var tr = tbody.append('tr');
147
148 function addCell(cls, txt) {
149 tr.append('td').attr('class', cls).html(txt);
150 }
151 addCell('label', friendlyProps[index] + ' :');
152 addCell('value', value);
153 }
154
155 function addPortRow(tbody, port) {
156 var tr = tbody.append('tr');
157
158 portCols.forEach(function (col) {
159 if (col === 'type' || col === 'id') {
160 port[col] = fs.cap(port[col]);
161 }
162 tr.append('td').html(port[col]);
163 });
164 }
165
166 function populateDetails(details) {
167 setUpPanel();
168
169 var toptbody = top.select('table').append('tbody'),
170 btmTable = bottom.select('table'),
171 ports = details.ports;
172
173 populateTopHalf(toptbody, details);
174 populateBottomHalf(btmTable, ports);
175 }
176
177 function respDetailsCb(data) {
178 self.panelData = data['details'];
179 populateDetails(self.panelData);
180 detailsPane.show();
181 }
182
Bri Prebilic Cole7c92a3d2015-01-09 16:50:03 -0800183 angular.module('ovDevice', [])
Simon Hunta89f0f92015-02-26 16:47:12 -0800184 .controller('OvDeviceCtrl',
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700185 ['$log', '$scope', 'TableBuilderService', 'FnService',
186 'MastService', 'PanelService', 'WebSocketService', 'IconService',
Simon Hunta89f0f92015-02-26 16:47:12 -0800187
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700188 function (_$log_, _$scope_, tbs, _fs_, _mast_, _ps_, _wss_, _is_) {
189 $log = _$log_;
190 $scope = _$scope_;
191 fs = _fs_;
192 mast = _mast_;
193 ps = _ps_;
194 wss = _wss_;
195 is = _is_;
196 self = this;
197 var handlers = {};
198 self.panelData = [];
199
200 function selCb(row) {
201 // request the server for more information
202 // get the id from the row to request details with
203 if ($scope.sel) {
204 wss.sendEvent(detailsReq, { id: row.id });
205 } else {
206 detailsPane.hide();
207 }
208 $log.debug('Got a click on:', row);
209 }
210
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700211 tbs.buildTable({
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700212 self: self,
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700213 scope: $scope,
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700214 tag: 'device',
215 selCb: selCb
216 });
217
218 createDetailsPane();
219
220 // bind websocket handlers
221 handlers[detailsResp] = respDetailsCb;
222 wss.bindHandlers(handlers);
223
224 $scope.$on('$destroy', function () {
225 ps.destroyPanel(pName);
226 wss.unbindHandlers(handlers);
Simon Hunta89f0f92015-02-26 16:47:12 -0800227 });
228
Bri Prebilic Cole72eb6db2015-03-30 16:58:53 -0700229 $log.log('OvDeviceCtrl has been created');
Bri Prebilic Cole7c92a3d2015-01-09 16:50:03 -0800230 }]);
231}());