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