blob: efcb3b433dcf91187f695060ac6021f1d308f2a2 [file] [log] [blame]
Jonghwan Hyun13a430d2018-07-22 17:02:51 +09001(function() {
2 'use strict';
3
4 // injected refs
5 var $log, $scope, $interval, $timeout, fs, wss, ks, ls;
6
7 // constants
8 var intIntentAddReq = 'intIntentAddRequest';
9 var intIntentDelReq = 'intIntentDelRequest';
Jonghwan Hyun13a430d2018-07-22 17:02:51 +090010
11 var refreshInterval = 1000;
12
13 var propOrder = ['id', 'srcAddr', 'dstAddr', 'srcPort', 'dstPort', 'insMask'];
14 var friendlyProps = ['IntIntent ID', 'Src Address', 'Dst Address', 'Src Port', 'Dst Port', 'Ins Mask'];
15
Jonghwan Hyun13a430d2018-07-22 17:02:51 +090016 function sendIntIntentString() {
17 var inst = [];
18 if ($scope.metaSwId) inst.push("SWITCH_ID");
19 if ($scope.metaPortId) inst.push("PORT_ID");
20 if ($scope.metaHopLatency) inst.push("HOP_LATENCY");
21 if ($scope.metaQOccupancy) inst.push("QUEUE_OCCUPANCY");
22 if ($scope.metaIngressTstamp) inst.push("INGRESS_TIMESTAMP");
23 if ($scope.metaEgressTstamp) inst.push("EGRESS_TIMESTAMP");
Jonghwan Hyun13a430d2018-07-22 17:02:51 +090024 if ($scope.metaEgressTx) inst.push("EGRESS_TX_UTIL");
25
26 var intentObjectNode = {
27 "ip4SrcPrefix": $scope.ip4SrcPrefix,
28 "ip4DstPrefix": $scope.ip4DstPrefix,
29 "l4SrcPort": $scope.l4SrcPort,
30 "l4DstPort": $scope.l4DstPort,
31 "protocol": $scope.protocol,
32 "metadata": inst
33 };
34 wss.sendEvent(intIntentAddReq, intentObjectNode);
35 }
36
37 function delIntIntent() {
38 if ($scope.selId) {
39 wss.sendEvent(intIntentDelReq, {
40 "intentId": $scope.selId
41 });
42 }
43 }
44
45 function intIntentBuildTable(o) {
46 var handlers = {},
Davide Scanob5ade982020-06-03 21:47:13 +020047 root = o.tag + 's',
Jonghwan Hyun13a430d2018-07-22 17:02:51 +090048 req = o.tag + 'DataRequest',
49 resp = o.tag + 'DataResponse',
50 onSel = fs.isF(o.selCb),
51 onResp = fs.isF(o.respCb),
52 idKey = o.idKey || 'id',
53 oldTableData = [],
54 refreshPromise;
55
56 o.scope.tableData = [];
57 o.scope.changedData = [];
58 o.scope.sortParams = o.sortParams || {};
59 o.scope.autoRefresh = true;
60 o.scope.autoRefreshTip = 'Toggle auto refresh';
61
62 // === websocket functions --------------------
63 // response
64 function respCb(data) {
65 ls.stop();
66 o.scope.tableData = data[root];
67 o.scope.annots = data.annots;
68 onResp && onResp();
69
70 // checks if data changed for row flashing
71 if (!angular.equals(o.scope.tableData, oldTableData)) {
72 o.scope.changedData = [];
73 // only flash the row if the data already exists
74 if (oldTableData.length) {
75 angular.forEach(o.scope.tableData, function (item) {
76 if (!fs.containsObj(oldTableData, item)) {
77 o.scope.changedData.push(item);
78 }
79 });
80 }
81 angular.copy(o.scope.tableData, oldTableData);
82 }
83 }
84 handlers[resp] = respCb;
85 wss.bindHandlers(handlers);
86
87 // request
88 function sortCb(params) {
89 var p = angular.extend({}, params, o.query);
90 if (wss.isConnected()) {
91 wss.sendEvent(req, p);
92 ls.start();
93 }
94 }
95 o.scope.sortCallback = sortCb;
96
97 // === selecting a row functions ----------------
98 function selCb($event, selRow) {
99 var selId = selRow[idKey];
100 o.scope.selId = (o.scope.selId === selId) ? null : selId;
101 onSel && onSel($event, selRow);
102 }
103 o.scope.selectCallback = selCb;
104
105 // === autoRefresh functions ------------------
106 function fetchDataIfNotWaiting() {
107 if (!ls.waiting()) {
108 if (fs.debugOn('widget')) {
109 $log.debug('Refreshing ' + root + ' page');
110 }
111 sortCb(o.scope.sortParams);
112 }
113 }
114
115 function startRefresh() {
116 refreshPromise = $interval(fetchDataIfNotWaiting, refreshInterval);
117 }
118
119 function stopRefresh() {
120 if (refreshPromise) {
121 $interval.cancel(refreshPromise);
122 refreshPromise = null;
123 }
124 }
125
126 function toggleRefresh() {
127 o.scope.autoRefresh = !o.scope.autoRefresh;
128 o.scope.autoRefresh ? startRefresh() : stopRefresh();
129 }
130 o.scope.toggleRefresh = toggleRefresh;
131
132 // === Cleanup on destroyed scope -----------------
133 o.scope.$on('$destroy', function () {
134 wss.unbindHandlers(handlers);
135 stopRefresh();
136 ls.stop();
137 });
138
139 sortCb(o.scope.sortParams);
140 startRefresh();
141 }
142
143 var app1 = angular.module('ovIntApp', []);
144 app1.controller('OvIntAppCtrl',
145 ['$log', '$scope', '$interval', '$timeout', 'TableBuilderService',
146 'FnService', 'WebSocketService', 'KeyService', 'LoadingService',
147
148 function(_$log_, _$scope_, _$interval_, _$timeout_, tbs, _fs_, _wss_, _ks_, _ls_) {
149 $log = _$log_;
150 $scope = _$scope_;
151 $interval = _$interval_;
152 $timeout = _$timeout_;
153 fs = _fs_;
154 wss = _wss_;
155 ks = _ks_;
156 ls = _ls_;
157
158 // custom selection callback
159 function selCb($event, row) {
160 }
161 intIntentBuildTable({
162 scope: $scope,
163 tag: 'intAppIntIntent'
164 // selCb: selCb
165 });
166
167 $scope.sendIntIntentString = sendIntIntentString;
168 $scope.delIntIntent = delIntIntent;
169 $scope.sendIntConfigString = sendIntConfigString;
170
171 // get data the first time...
172 // getData();
173
174 // cleanup
175 $scope.$on('$destroy', function() {
176 // wss.unbindHandlers(handlers);
177 /*ks.unbindKeys();*/
178 $log.log('OvIntAppCtrl has been destroyed');
179 });
180
181 $log.log('OvIntAppCtrl has been created');
182 }
183 ]);
184}());