blob: ea6dffaa6c82420162247679502b7032bdfc69ac [file] [log] [blame]
Jonghwan Hyun13a430d2018-07-22 17:02:51 +09001(function() {
2 'use strict';
3
4 // injected refs
Yi Tseng930b0cd2020-10-04 22:33:22 -07005 let $log, $scope, $interval, $timeout, fs, wss, ks, ls;
Jonghwan Hyun13a430d2018-07-22 17:02:51 +09006
7 // constants
Yi Tseng930b0cd2020-10-04 22:33:22 -07008 let intIntentAddReq = 'intIntentAddRequest';
9 let intIntentDelReq = 'intIntentDelRequest';
Jonghwan Hyun13a430d2018-07-22 17:02:51 +090010
Yi Tseng930b0cd2020-10-04 22:33:22 -070011 let refreshInterval = 1000;
Jonghwan Hyun13a430d2018-07-22 17:02:51 +090012
Yi Tseng930b0cd2020-10-04 22:33:22 -070013 let propOrder = ['id', 'srcAddr', 'dstAddr', 'srcPort', 'dstPort', 'insMask'];
14 let friendlyProps = ['IntIntent ID', 'Src Address', 'Dst Address', 'Src Port', 'Dst Port', 'Ins Mask'];
15
16 function checkArgAndShowMsg() {
17 // Need to match at least one field
18 if ($scope.ip4SrcPrefix === "" &&
19 $scope.ip4DstPrefix === "" &&
20 $scope.l4SrcPort === "" &&
21 $scope.l4DstPort === "" ) {
22 $scope.intAddMsg = "Nothing installed since there is no matching spec.";
23 return false;
24 }
25 // IP address validation
26 let ipv4Pattern = /^([0-9]{1,3}\.){3}[0-9]{1,3}(\/[0-9]{1,2})?$/;
27 if ($scope.ip4SrcPrefix !== "" && !ipv4Pattern.test($scope.ip4SrcPrefix)) {
28 $scope.intAddMsg = "Invalid source IP.";
29 return false;
30 }
31 if ($scope.ip4DstPrefix !== "" && !ipv4Pattern.test($scope.ip4DstPrefix)) {
32 $scope.intAddMsg = "Invalid destination IP.";
33 return false;
34 }
35 // L4 port validation
36 if ($scope.l4SrcPort !== "") {
37 let l4SrcPort = parseInt($scope.l4SrcPort);
38 if (isNaN(l4SrcPort)) {
39 $scope.intAddMsg = "Invalid source port number.";
40 return false;
41 }
42 if (l4SrcPort <= 0 || l4SrcPort > 65535) {
43 $scope.intAddMsg = "Invalid source port number.";
44 return false;
45 }
46 if ($scope.protocol === "") {
47 $scope.intAddMsg = "protocol cannot be empty.";
48 return false;
49 }
50 }
51 if ($scope.l4DstPort !== "") {
52 let l4DstPort = parseInt($scope.l4DstPort);
53 if (isNaN(l4DstPort)) {
54 $scope.intAddMsg = "Invalid destination port number.";
55 return false;
56 }
57 if (l4DstPort <= 0 || l4DstPort > 65535) {
58 $scope.intAddMsg = "Invalid destination port number.";
59 return false;
60 }
61 if ($scope.protocol === "") {
62 $scope.intAddMsg = "protocol cannot be empty.";
63 return false;
64 }
65 }
66 $scope.intAddMsg = "";
67 return true;
68 }
Jonghwan Hyun13a430d2018-07-22 17:02:51 +090069
Jonghwan Hyun13a430d2018-07-22 17:02:51 +090070 function sendIntIntentString() {
Yi Tseng930b0cd2020-10-04 22:33:22 -070071 let inst = [];
Jonghwan Hyun13a430d2018-07-22 17:02:51 +090072 if ($scope.metaSwId) inst.push("SWITCH_ID");
73 if ($scope.metaPortId) inst.push("PORT_ID");
74 if ($scope.metaHopLatency) inst.push("HOP_LATENCY");
75 if ($scope.metaQOccupancy) inst.push("QUEUE_OCCUPANCY");
76 if ($scope.metaIngressTstamp) inst.push("INGRESS_TIMESTAMP");
77 if ($scope.metaEgressTstamp) inst.push("EGRESS_TIMESTAMP");
Jonghwan Hyun13a430d2018-07-22 17:02:51 +090078 if ($scope.metaEgressTx) inst.push("EGRESS_TX_UTIL");
79
Yi Tseng930b0cd2020-10-04 22:33:22 -070080 let intentObjectNode = {
Jonghwan Hyun13a430d2018-07-22 17:02:51 +090081 "ip4SrcPrefix": $scope.ip4SrcPrefix,
82 "ip4DstPrefix": $scope.ip4DstPrefix,
83 "l4SrcPort": $scope.l4SrcPort,
84 "l4DstPort": $scope.l4DstPort,
85 "protocol": $scope.protocol,
Yi Tseng4027cac2020-10-13 19:15:12 -070086 "metadata": inst,
87 "telemetryMode": $scope.telemetryMode
Jonghwan Hyun13a430d2018-07-22 17:02:51 +090088 };
Yi Tseng930b0cd2020-10-04 22:33:22 -070089 if (checkArgAndShowMsg()) {
90 wss.sendEvent(intIntentAddReq, intentObjectNode);
91 }
Jonghwan Hyun13a430d2018-07-22 17:02:51 +090092 }
93
94 function delIntIntent() {
95 if ($scope.selId) {
96 wss.sendEvent(intIntentDelReq, {
97 "intentId": $scope.selId
98 });
99 }
100 }
101
102 function intIntentBuildTable(o) {
Yi Tseng930b0cd2020-10-04 22:33:22 -0700103 let handlers = {},
Davide Scanob5ade982020-06-03 21:47:13 +0200104 root = o.tag + 's',
Jonghwan Hyun13a430d2018-07-22 17:02:51 +0900105 req = o.tag + 'DataRequest',
106 resp = o.tag + 'DataResponse',
107 onSel = fs.isF(o.selCb),
108 onResp = fs.isF(o.respCb),
109 idKey = o.idKey || 'id',
110 oldTableData = [],
111 refreshPromise;
112
113 o.scope.tableData = [];
114 o.scope.changedData = [];
115 o.scope.sortParams = o.sortParams || {};
116 o.scope.autoRefresh = true;
117 o.scope.autoRefreshTip = 'Toggle auto refresh';
118
119 // === websocket functions --------------------
120 // response
121 function respCb(data) {
122 ls.stop();
123 o.scope.tableData = data[root];
124 o.scope.annots = data.annots;
125 onResp && onResp();
126
127 // checks if data changed for row flashing
128 if (!angular.equals(o.scope.tableData, oldTableData)) {
129 o.scope.changedData = [];
130 // only flash the row if the data already exists
131 if (oldTableData.length) {
132 angular.forEach(o.scope.tableData, function (item) {
133 if (!fs.containsObj(oldTableData, item)) {
134 o.scope.changedData.push(item);
135 }
136 });
137 }
138 angular.copy(o.scope.tableData, oldTableData);
139 }
140 }
141 handlers[resp] = respCb;
142 wss.bindHandlers(handlers);
143
144 // request
145 function sortCb(params) {
Yi Tseng930b0cd2020-10-04 22:33:22 -0700146 let p = angular.extend({}, params, o.query);
Jonghwan Hyun13a430d2018-07-22 17:02:51 +0900147 if (wss.isConnected()) {
148 wss.sendEvent(req, p);
149 ls.start();
150 }
151 }
152 o.scope.sortCallback = sortCb;
153
154 // === selecting a row functions ----------------
155 function selCb($event, selRow) {
Yi Tseng930b0cd2020-10-04 22:33:22 -0700156 let selId = selRow[idKey];
Jonghwan Hyun13a430d2018-07-22 17:02:51 +0900157 o.scope.selId = (o.scope.selId === selId) ? null : selId;
158 onSel && onSel($event, selRow);
159 }
160 o.scope.selectCallback = selCb;
161
162 // === autoRefresh functions ------------------
163 function fetchDataIfNotWaiting() {
164 if (!ls.waiting()) {
165 if (fs.debugOn('widget')) {
166 $log.debug('Refreshing ' + root + ' page');
167 }
168 sortCb(o.scope.sortParams);
169 }
170 }
171
172 function startRefresh() {
173 refreshPromise = $interval(fetchDataIfNotWaiting, refreshInterval);
174 }
175
176 function stopRefresh() {
177 if (refreshPromise) {
178 $interval.cancel(refreshPromise);
179 refreshPromise = null;
180 }
181 }
182
183 function toggleRefresh() {
184 o.scope.autoRefresh = !o.scope.autoRefresh;
185 o.scope.autoRefresh ? startRefresh() : stopRefresh();
186 }
187 o.scope.toggleRefresh = toggleRefresh;
188
189 // === Cleanup on destroyed scope -----------------
190 o.scope.$on('$destroy', function () {
191 wss.unbindHandlers(handlers);
192 stopRefresh();
193 ls.stop();
194 });
195
196 sortCb(o.scope.sortParams);
197 startRefresh();
198 }
199
Yi Tseng930b0cd2020-10-04 22:33:22 -0700200 let app1 = angular.module('ovIntApp', []);
Jonghwan Hyun13a430d2018-07-22 17:02:51 +0900201 app1.controller('OvIntAppCtrl',
202 ['$log', '$scope', '$interval', '$timeout', 'TableBuilderService',
203 'FnService', 'WebSocketService', 'KeyService', 'LoadingService',
204
205 function(_$log_, _$scope_, _$interval_, _$timeout_, tbs, _fs_, _wss_, _ks_, _ls_) {
206 $log = _$log_;
207 $scope = _$scope_;
208 $interval = _$interval_;
209 $timeout = _$timeout_;
210 fs = _fs_;
211 wss = _wss_;
212 ks = _ks_;
213 ls = _ls_;
214
215 // custom selection callback
216 function selCb($event, row) {
217 }
218 intIntentBuildTable({
219 scope: $scope,
220 tag: 'intAppIntIntent'
221 // selCb: selCb
222 });
223
224 $scope.sendIntIntentString = sendIntIntentString;
225 $scope.delIntIntent = delIntIntent;
Yi Tseng930b0cd2020-10-04 22:33:22 -0700226 $scope.intAddMsg = "";
227 $scope.ip4SrcPrefix = "";
228 $scope.ip4DstPrefix = "";
229 $scope.l4SrcPort = "";
230 $scope.l4DstPort = "";
231 $scope.protocol = "";
Yi Tseng4027cac2020-10-13 19:15:12 -0700232 $scope.telemetryMode = "POSTCARD";
Jonghwan Hyun13a430d2018-07-22 17:02:51 +0900233
234 // get data the first time...
235 // getData();
236
237 // cleanup
238 $scope.$on('$destroy', function() {
239 // wss.unbindHandlers(handlers);
240 /*ks.unbindKeys();*/
241 $log.log('OvIntAppCtrl has been destroyed');
242 });
243
244 $log.log('OvIntAppCtrl has been created');
245 }
246 ]);
247}());