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