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