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