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