Rate limits on uni port
[unimgr.git] / dlux / cpeui / cpeui-module / src / main / resources / cpeui / tenant.controller.js
1 define([ 'app/cpeui/cpeui.module' ], function(cpeui) {
2   cpeui.register.controller('OpenTenantCtrl', function($scope, CpeuiSvc, CpeuiDialogs, $stateParams) {
3
4     $scope.curTenant = $stateParams.tenantid;
5     $scope.unisTables = {};
6     $scope.unis = [];
7     $scope.ces = [];
8     $scope.ipvcs = [];
9     $scope.subnets = {};
10     $scope.profiles =[];
11     $scope.cesDisplayNames = {};
12     $scope.unisMap = {};
13     $scope.networkNames = {};
14     $scope.expandFlags = {
15         ipuni:{},
16         tuni:{}
17     };
18
19     var tabIndexs = {
20         "L2" : 1,
21         "L3" : 2,
22         "unis" : 3,
23       }
24     if (tabIndexs[$stateParams.tenantTabName]) {
25       $scope.tab.tenantData = tabIndexs[$stateParams.tenantTabName];
26     }
27
28     function init(){
29       $scope.updateUnis(function(unis){
30         CpeuiSvc.getCes(function(ces) {
31           $scope.ces = ces.filter(function(item) {
32
33             var filteredUnis = unis.filterByField('device', item["dev-id"]);
34             filteredUnis = filteredUnis.filterByField('prettyID', 'br-int', true);
35             filteredUnis = filteredUnis.filter(function(i){return !(i.prettyID && i.prettyID.startsWith('tun'));});
36
37             return (filteredUnis.length);
38           });
39           ces.forEach(function(ce){
40             $scope.cesDisplayNames[ce['dev-id']] = ce['device-name'] ? ce['device-name'] : ce['dev-id'];
41           });
42           $scope.updateEvcView();
43         });
44       });
45
46       CpeuiSvc.getNetworkNames(function(networks){
47         networks.forEach(function(net){
48           $scope.networkNames[net.uuid] = net.name;
49         });
50       });
51
52       CpeuiSvc.getProfiles(function(profiles) {
53         $scope.profiles = profiles;
54       });
55     }
56
57     $scope.updateUnis = function(callback) {
58       CpeuiSvc.getUnis(function(unis) {
59         unis.forEach(function(u) {
60           u.prettyID = u['uni-id'].split(":")[u['uni-id'].split(":").length - 1];
61           $scope.unisMap[u['uni-id']] = u;
62         });
63         $scope.unis = unis.filter(function(u){return u["tenant-id"] == $scope.curTenant;});
64
65         if (callback) {
66           callback($scope.unis);
67         }
68       });
69     };
70
71     $scope.updateEvcView = function() {
72       CpeuiSvc.getServices($scope.curTenant, function(services) {
73
74         $scope.evcs = services.filter(function(svc){ return svc.evc != undefined;});
75         $scope.ipvcs = services.filter(function(svc){ return svc.ipvc != undefined;});
76         $scope.updateUnis();
77         console.log($scope.ipvcs);
78         $scope.ipvcs.forEach(function(e){
79           if (e.ipvc.unis != undefined && e.ipvc.unis.uni != undefined){
80               e.ipvc.unis.uni.forEach(function(u){
81                 u.device = u['uni-id'].split(":")[u['uni-id'].split(":").length-2];
82                 u.prettyID = u['uni-id'].split(":")[u['uni-id'].split(":").length-1];
83             });
84           }
85         });
86         $scope.evcs.forEach(function(e){
87           e.isTree = (e.evc['evc-type'] == 'rooted-multipoint');
88           e.device2unis = {};
89           if (e.evc.unis != undefined && e.evc.unis.uni != undefined){
90             e.evc.unis.uni.forEach(function(u){
91               if (e.device2unis[$scope.unisMap[u['uni-id']].device] == undefined){
92                 e.device2unis[$scope.unisMap[u['uni-id']].device] = [];
93               }
94               u.prettyID = u['uni-id'].split(":")[u['uni-id'].split(":").length - 1];
95               e.device2unis[$scope.unisMap[u['uni-id']].device].push(u);
96             });
97           }
98         });
99       });
100       CpeuiSvc.getAllIpUniSubnets(function(subnets){
101         $scope.subnets = subnets;
102       });
103     };
104
105     $scope.title = function(str) {
106       if (!str) {
107         return str;
108         }
109       return str.split('-').map(function(s) {
110         return s[0].toUpperCase() + s.slice(1);
111       }).join(' ');
112     }
113
114     $scope.svcTypes = [ 'epl', 'evpl', 'eplan', 'evplan', 'eptree', 'evptree' ]
115     var evcTypes = {
116       'epl' : 'point-to-point',
117       'evpl' : 'point-to-point',
118       'eplan' : 'multipoint-to-multipoint',
119       'evplan' : 'multipoint-to-multipoint',
120       'eptree' : 'rooted-multipoint',
121       'evptree' : 'rooted-multipoint'
122     }
123
124     var addEvcController = function($scope, $mdDialog) {
125       $scope.validate = function(form) {
126         form.svc_type.$setTouched(); // patch because angular bug http://stackoverflow.com/questions/36138442/error-not-showing-for-angular-material-md-select
127         console.log($scope);
128         return form.$valid;
129       };
130     };
131
132     $scope.evcDialog = new CpeuiDialogs.Dialog('AddEvc', {}, function(obj) {
133       CpeuiSvc.addEvc(obj, evcTypes[obj.svc_type], $scope.curTenant, function() {
134             $scope.updateEvcView();
135           });
136     }, addEvcController);
137
138     $scope.ipvcDialog = new CpeuiDialogs.Dialog('AddIpvc', {}, function(obj) {
139       CpeuiSvc.addIpvc(obj, $scope.curTenant, function() {
140             $scope.updateEvcView();
141           });
142     });
143
144     $scope.linkIpvcUniDialog = new CpeuiDialogs.Dialog('LinkIpvcUni', {},
145         function(obj) {
146           CpeuiSvc.addIpvcUni(obj.svc_id, obj.uni['uni-id'], obj.ip_uni, obj.profile_name,
147               function() {
148                 $scope.updateEvcView();
149               });
150         });
151
152     var ipUniDialogController = function($scope, $mdDialog) {
153       $scope.hasVlans = false;
154       if ($scope.params.uni['ip-unis'] && $scope.params.uni['ip-unis']['ip-uni']) {
155         var ipunis = $scope.params.uni['ip-unis']['ip-uni'];
156         for (i = 0; i < ipunis.length; i++) {
157           if (ipunis[i].vlan){
158             $scope.hasVlans = true;
159           }
160         }
161       }
162     };
163
164     $scope.ipUniDialog = new CpeuiDialogs.Dialog('AddIpUni', {}, function(obj) {
165       CpeuiSvc.addIpUni(obj['uni-id'], obj['ip-uni-id'], obj['ip-address'], obj.vlan, function() {
166         var uni = $scope.unis.filterByField('uni-id',obj['uni-id'])[0];
167         if (uni['ip-unis'] == undefined || uni['ip-unis']['ip-uni'] == undefined){
168           uni['ip-unis'] = {'ip-uni':[]};
169           }
170         uni['ip-unis']['ip-uni'].push(obj);
171         });
172       }, ipUniDialogController);
173
174     $scope.openIpUniDialog = function(event,uni){
175       if (uni['ip-unis'] && (uni['ip-unis']['ip-uni'] != undefined)){
176         var ipunis = uni['ip-unis']['ip-uni'];
177         for (i = 0; i < ipunis.length; i++) {
178           if (!ipunis[i].vlan){
179             CpeuiDialogs.alert("Error","You Can't have more then one ip-uni with no vlan. please remove the non-vlan ip-uni before adding new.")
180             return;
181           }
182         }
183       }
184       $scope.ipUniDialog.show(event,{'uniid':uni['uni-id'], uni:uni})
185     }
186
187     $scope.ipUniSubnetDialog = new CpeuiDialogs.Dialog('AddIpUniSubnet', {}, function(obj) {
188       CpeuiSvc.addIpUniSubnet(obj.uniid, obj.ipuniid, obj.subnet, obj.gateway, function() {
189         if ($scope.subnets[obj.uniid] == undefined){
190           $scope.subnets[obj.uniid] = {};
191         }
192         if ($scope.subnets[obj.uniid][obj.ipuniid] == undefined){
193           $scope.subnets[obj.uniid][obj.ipuniid] = [];
194         }
195         $scope.subnets[obj.uniid][obj.ipuniid].push({
196           "uni-id": obj.uniid,
197           "ip-uni-id": obj.ipuniid,
198           "subnet": obj.subnet,
199           "gateway": obj.gateway
200         });
201       });
202     });
203
204     $scope.deleteIpUni = function(uniid, ipuni_id) {
205         CpeuiDialogs.confirm(function() {
206           CpeuiSvc.deleteIpUni(uniid, ipuni_id, function() {
207             $scope.updateEvcView(); // TODO update unis only
208           });
209         });
210       };
211       $scope.deleteIpvcUni = function(svc_id, uni_id, ipuni_id) {
212         CpeuiDialogs.confirm(function() {
213           CpeuiSvc.deleteIpvcUni(svc_id, uni_id, ipuni_id, function() {
214             $scope.updateEvcView();
215           });
216         });
217       };
218       $scope.getMefInterfaceIpvc = function(uni_id,ipuni_id){
219         var uni = $scope.unis.filterByField('uni-id',uni_id)[0];
220         if ((uni == undefined) || (uni['ip-unis'] == undefined) || (uni['ip-unis']['ip-uni'] == undefined)) {
221           return undefined;
222         }
223         return uni['ip-unis']['ip-uni'].filterByField('ip-uni-id',ipuni_id)[0];
224       }
225
226       $scope.deleteIpUniSubnet = function(uniid, ipuni_id, subnet) {
227           CpeuiDialogs.confirm(function() {
228             CpeuiSvc.deleteIpUniSubnet(uniid, ipuni_id, subnet, function() {
229               $scope.updateEvcView(); // TODO update unis only
230             });
231           });
232     };
233
234     $scope.deleteEvc = function(svcid) {
235       CpeuiDialogs.confirm(function() {
236         CpeuiSvc.removeEvc(svcid, function() {
237           $scope.updateEvcView();
238         });
239       });
240     };
241
242     $scope.deleteEvcUni = function(svc_id, uni_id) {
243       CpeuiDialogs.confirm(function() {
244         CpeuiSvc.deleteEvcUni(svc_id, uni_id, function() {
245           $scope.updateEvcView();
246         });
247       });
248     };
249
250     var linkEvcUniController = function($scope, $mdDialog, params) {
251       $scope.params = params;
252       $scope.obj = {
253         vlans : []
254       };
255       $scope.deleteVlan = function(vlan) {
256         $scope.obj.vlans.splice($scope.obj.vlans.indexOf(vlan), 1);
257       };
258       $scope.addVlan = function(vlan) {
259         if ($scope.obj.vlans.indexOf(vlan) == -1) {
260           $scope.obj.vlans.push(vlan);
261           console.log(vlan);
262         }
263         $('#vlan_input').val(undefined);
264       };
265
266       $scope.filterUsedUnis = function(evc){
267         return function(u) {
268           if (u.prettyID == 'br-int') {
269             return false;
270           }
271           if (u.prettyID && u.prettyID.startsWith('tun')) {
272             return false;
273           }
274           if (evc.evc == undefined || evc.evc.unis.uni == undefined){
275             return true;
276           }
277           return evc.evc.unis.uni.filterByField('uni-id',u['uni-id']).length == 0;
278         };
279       };
280     };
281
282     $scope.linkEvcUniDialog = new CpeuiDialogs.Dialog('LinkEvcUni', {},
283         function(obj) {
284           if (!obj.role) {
285             obj.role = "root";
286           }
287           CpeuiSvc.addEvcUni(obj.svc_id, obj.uni_id, obj.role, obj.vlans, obj.profile_name,
288               function() {
289                 $scope.updateEvcView();
290               });
291         }, linkEvcUniController);
292
293     var editVlanController = function($scope, $mdDialog, params) {
294       $scope.params = params;
295
296       $scope.deleteVlan = function(svc_id, uni_id, vlan, allvlans) {
297         CpeuiSvc.deleteVlan(svc_id, uni_id, vlan, function() {
298           allvlans.splice(allvlans.indexOf(vlan), 1)
299         });
300       };
301       $scope.addVlan = function(svc_id, uni_id, vlan, allvlans) {
302         if (allvlans == undefined) {
303           allvlans = [];
304         }
305         if (allvlans.indexOf(vlan) == -1) {
306           CpeuiSvc.addVlan(svc_id, uni_id, vlan, function() {
307             allvlans.push(vlan);
308           });
309         }
310         $('#vlan_input').val(undefined);
311       };
312     };
313
314     $scope.editVlanDialog = new CpeuiDialogs.Dialog('EditVlans', {}, undefined, editVlanController);
315
316     $scope.sortEvc = function(evc) {
317       return evc.evc['evc-id'];
318     };
319     $scope.sortUni = function(uni) {
320       return uni['uni-id'];
321     };
322     $scope.sortIpvc = function(ipvc) {
323       return ipvc['ipvc-id'];
324     };
325
326     init();
327   });
328 });