UI Changes according to Ofir requirements
[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     };
19
20     var tabIndexs = {
21         "inventory" : 0,
22         "L2" : 1,
23         "L3" : 2,
24         "unis" : 6,
25       }
26     if ($stateParams.tenantTabName in tabIndexs) {
27       $scope.tab.tenantData = tabIndexs[$stateParams.tenantTabName];
28     }
29
30     function init(){
31       $scope.updateUnis(function(unis){
32         CpeuiSvc.getCes(function(ces) {
33           $scope.ces = ces.filter(function(item) {
34
35             var filteredUnis = unis.filterByField('device', item["dev-id"]);
36             filteredUnis = filteredUnis.filterByField('prettyID', 'br-int', true);
37             filteredUnis = filteredUnis.filter(function(i){return !(i.prettyID && i.prettyID.startsWith('tun'));});
38
39             return (filteredUnis.length);
40           });
41           ces.forEach(function(ce){
42             $scope.cesDisplayNames[ce['dev-id']] = ce['device-name'] ? ce['device-name'] : ce['dev-id'];
43           });
44           $scope.updateEvcView();
45         });
46       });
47
48       CpeuiSvc.getNetworkNames(function(networks){
49         networks.forEach(function(net){
50           $scope.networkNames[net.uuid] = net.name;
51         });
52       });
53
54       CpeuiSvc.getProfiles(function(profiles) {
55         $scope.profiles = profiles;
56       });
57     }
58
59     $scope.updateUnis = function(callback) {
60       CpeuiSvc.getUnis(function(unis) {
61         unis.forEach(function(u) {
62           u.prettyID = u['uni-id'].split(":")[u['uni-id'].split(":").length - 1];
63           $scope.unisMap[u['uni-id']] = u;
64         });
65         $scope.unis = unis.filter(function(u){return u["tenant-id"] == $scope.curTenant;});
66
67         if (callback) {
68           callback($scope.unis);
69         }
70       });
71     };
72
73     $scope.updateEvcView = function() {
74       CpeuiSvc.getServices($scope.curTenant, function(services) {
75
76         $scope.evcs = services.filter(function(svc){ return svc.evc != undefined;});
77         $scope.ipvcs = services.filter(function(svc){ return svc.ipvc != undefined;});
78         $scope.updateUnis();
79         function mapUniToService(uni, service) {
80             var uniObj = $scope.unis.filterByField('uni-id',uni['uni-id'])[0];
81             if (!uniObj.vlanToService) {
82                 uniObj.vlanToService = [];
83             }
84             uniObj.hasService = true;
85             if (uni['evc-uni-ce-vlans'] && uni['evc-uni-ce-vlans']['evc-uni-ce-vlan']){
86                 uni['evc-uni-ce-vlans']['evc-uni-ce-vlan'].forEach(function(v){
87                     uniObj.vlanToService.push({"vlan":v.vid, "svc":service});
88                 });
89             } else {
90                 if (uni["ip-uni-id"]) {
91                     var ipuni = $scope.unis.filterByField('uni-id',uni['uni-id'])[0];
92                     ipuni["ip-unis"]["ip-uni"].forEach(function(ipu){
93                         if (ipu['ip-uni-id'] == uni["ip-uni-id"]){
94                             var vlan = ipu.vlan ? ipu.vlan : 0;
95                             uniObj.vlanToService.push({"vlan":vlan, "svc":service});
96                         }
97                     });
98                 } else {
99                     uniObj.vlanToService.push({"vlan":0, "svc":service});
100                 }
101             }
102         }
103
104         $scope.ipvcs.forEach(function(e){
105           if (e.ipvc.unis != undefined && e.ipvc.unis.uni != undefined){
106               e.ipvc.unis.uni.forEach(function(u){
107                 u.device = u['uni-id'].split(":")[u['uni-id'].split(":").length-2];
108                 u.prettyID = u['uni-id'].split(":")[u['uni-id'].split(":").length-1];
109                 mapUniToService(u,e);
110             });
111           }
112         });
113         $scope.evcs.forEach(function(e){
114           e.isTree = (e.evc['evc-type'] == 'rooted-multipoint');
115           e.device2unis = {};
116           if (e.evc.unis != undefined && e.evc.unis.uni != undefined){
117             e.evc.unis.uni.forEach(function(u){
118               if (e.device2unis[$scope.unisMap[u['uni-id']].device] == undefined){
119                 e.device2unis[$scope.unisMap[u['uni-id']].device] = [];
120               }
121               u.prettyID = u['uni-id'].split(":")[u['uni-id'].split(":").length - 1];
122               e.device2unis[$scope.unisMap[u['uni-id']].device].push(u);
123               mapUniToService(u,e);
124             });
125           }
126         });
127       });
128       CpeuiSvc.getAllIpUniSubnets(function(subnets){
129         $scope.subnets = subnets;
130       });
131     };
132
133     $scope.doesAllUniHasService = function(ceUnis) {
134         for (var i=0 ; i< ceUnis.length; ++i) {
135             if (!ceUnis[i].hasService) {
136                 return false;
137             }
138         }
139         return true;
140     }
141
142     $scope.title = function(str) {
143       if (!str) {
144         return str;
145         }
146       return str.split('-').map(function(s) {
147         return s[0].toUpperCase() + s.slice(1);
148       }).join(' ');
149     }
150
151     $scope.svcTypes = [ 'epl', 'evpl', 'eplan', 'evplan', 'eptree', 'evptree' ]
152     var evcTypes = {
153       'epl' : 'point-to-point',
154       'evpl' : 'point-to-point',
155       'eplan' : 'multipoint-to-multipoint',
156       'evplan' : 'multipoint-to-multipoint',
157       'eptree' : 'rooted-multipoint',
158       'evptree' : 'rooted-multipoint'
159     }
160
161     var addEvcController = function($scope, $mdDialog) {
162       $scope.validate = function(form) {
163         form.svc_type.$setTouched(); // patch because angular bug http://stackoverflow.com/questions/36138442/error-not-showing-for-angular-material-md-select
164         console.log($scope);
165         return form.$valid;
166       };
167     };
168
169     $scope.evcDialog = new CpeuiDialogs.Dialog('AddEvc', {}, function(obj) {
170       CpeuiSvc.addEvc(obj, evcTypes[obj.svc_type], $scope.curTenant, function() {
171             $scope.updateEvcView();
172           });
173     }, addEvcController);
174
175     $scope.ipvcDialog = new CpeuiDialogs.Dialog('AddIpvc', {}, function(obj) {
176       CpeuiSvc.addIpvc(obj, $scope.curTenant, function() {
177             $scope.updateEvcView();
178           });
179     });
180
181     $scope.linkIpvcUniDialog = new CpeuiDialogs.Dialog('LinkIpvcUni', {},
182         function(obj) {
183           CpeuiSvc.addIpvcUni(obj.svc_id, obj.uni['uni-id'], obj.ip_uni, obj.profile_name,
184               function() {
185                 $scope.updateEvcView();
186               });
187         });
188
189     var ipUniDialogController = function($scope, $mdDialog) {
190       $scope.hasVlans = false;
191       if ($scope.params.uni['ip-unis'] && $scope.params.uni['ip-unis']['ip-uni']) {
192         var ipunis = $scope.params.uni['ip-unis']['ip-uni'];
193         for (i = 0; i < ipunis.length; i++) {
194           if (ipunis[i].vlan){
195             $scope.hasVlans = true;
196           }
197         }
198       }
199     };
200
201     $scope.ipUniDialog = new CpeuiDialogs.Dialog('AddIpUni', {}, function(obj) {
202       CpeuiSvc.addIpUni(obj['uni-id'], obj['ip-address'], obj.vlan, obj['segmentation-id'], function() {
203         var uni = $scope.unis.filterByField('uni-id',obj['uni-id'])[0];
204         if (uni['ip-unis'] == undefined || uni['ip-unis']['ip-uni'] == undefined){
205           uni['ip-unis'] = {'ip-uni':[]};
206           }
207         uni['ip-unis']['ip-uni'].push(obj);
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 });