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