UI: add UNI & IPVC tabs (work in-progress)
[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           $scope.unis.filterByField('uni-id',obj['uni-id'])[0]['ip-unis']['ip-uni'].push(obj);
158           });
159     }, ipUniDialogController);
160
161     $scope.openIpUniDialog = function(event,uni){
162       if (uni['ip-unis'] && (uni['ip-unis']['ip-uni'] != undefined)){
163         var ipunis = uni['ip-unis']['ip-uni'];
164         for (i = 0; i < ipunis.length; i++) {
165           if (!ipunis[i].vlan){
166             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.")
167             return;
168           }
169         }
170       }
171       $scope.ipUniDialog.show(event,{'uniid':uni['uni-id'], uni:uni})
172     }
173
174     $scope.ipUniSubnetDialog = new CpeuiDialogs.Dialog('AddIpUniSubnet', {}, function(obj) {
175       CpeuiSvc.addIpUniSubnet(obj.uniid, obj.ipuniid, obj.subnet, obj.gateway, function() {
176           CpeuiSvc.getIpUniSubnets(obj.uniid, obj.ipuniid, function(subnets) {
177             $scope.unis.filterByField('uni-id',obj.uniid)[0]['ip-unis']['ip-uni'].filterByField('ip-uni-id',obj.ipuniid)[0].subnets = {subnet:subnets};
178           });
179       });
180     });
181
182     $scope.deleteIpUni = function(uniid, ipuni_id) {
183         CpeuiDialogs.confirm(function() {
184           CpeuiSvc.deleteIpUni(uniid, ipuni_id, function() {
185             $scope.updateEvcView(); // TODO update unis only
186           });
187         });
188       };
189       $scope.deleteIpvcUni = function(svc_id, uni_id) {
190         CpeuiDialogs.confirm(function() {
191           CpeuiSvc.deleteIpvcUni(svc_id, uni_id, function() {
192             $scope.updateEvcView();
193           });
194         });
195       };
196       $scope.getMefInterfaceIpvc = function(uni_id,ipuni_id){
197         var uni = $scope.unis.filterByField('uni-id',uni_id)[0];
198         if ((uni == undefined) || (uni['ip-unis'] == undefined) || (uni['ip-unis']['ip-uni'] == undefined)) {
199           return undefined;
200         }
201         return uni['ip-unis']['ip-uni'].filterByField('ip-uni-id',ipuni_id)[0];
202       }
203
204       $scope.deleteIpUniSubnet = function(uniid, ipuni_id, subnet) {
205           CpeuiDialogs.confirm(function() {
206             CpeuiSvc.deleteIpUniSubnet(uniid, ipuni_id, subnet, function() {
207               $scope.updateEvcView(); // TODO update unis only
208             });
209           });
210     };
211
212     $scope.deleteEvc = function(svcid) {
213       CpeuiDialogs.confirm(function() {
214         CpeuiSvc.removeEvc(svcid, function() {
215           $scope.updateEvcView();
216         });
217       });
218     };
219
220     $scope.deleteEvcUni = function(svc_id, uni_id) {
221       CpeuiDialogs.confirm(function() {
222         CpeuiSvc.deleteEvcUni(svc_id, uni_id, function() {
223           $scope.updateEvcView();
224         });
225       });
226     };
227
228     var linkEvcUniController = function($scope, $mdDialog, params) {
229       $scope.params = params;
230       $scope.obj = {
231         vlans : []
232       };
233       $scope.deleteVlan = function(vlan) {
234         $scope.obj.vlans.splice($scope.obj.vlans.indexOf(vlan), 1);
235       };
236       $scope.addVlan = function(vlan) {
237         if ($scope.obj.vlans.indexOf(vlan) == -1) {
238           $scope.obj.vlans.push(vlan);
239           console.log(vlan);
240         }
241         $('#vlan_input').val(undefined);
242       };
243
244       $scope.filterUsedUnis = function(evc){
245         return function(u) {
246           if (u.prettyID == 'br-int') {
247             return false;
248           }
249           if (u.prettyID && u.prettyID.startsWith('tun')) {
250             return false;
251           }
252           if (evc.evc == undefined || evc.evc.unis.uni == undefined){
253             return true;
254           }
255           return evc.evc.unis.uni.filterByField('uni-id',u['uni-id']).length == 0;
256         };
257       };
258     };
259
260     $scope.linkEvcUniDialog = new CpeuiDialogs.Dialog('LinkEvcUni', {},
261         function(obj) {
262           if (!obj.role) {
263             obj.role = "root";
264           }
265           CpeuiSvc.addEvcUni(obj.svc_id, obj.uni_id, obj.role, obj.vlans,
266               function() {
267                 $scope.updateEvcView();
268               });
269         }, linkEvcUniController);
270
271     var editVlanController = function($scope, $mdDialog, params) {
272       $scope.params = params;
273
274       $scope.deleteVlan = function(svc_id, uni_id, vlan, allvlans) {
275         CpeuiSvc.deleteVlan(svc_id, uni_id, vlan, function() {
276           allvlans.splice(allvlans.indexOf(vlan), 1)
277         });
278       };
279       $scope.addVlan = function(svc_id, uni_id, vlan, allvlans) {
280         if (allvlans == undefined) {
281           allvlans = [];
282         }
283         if (allvlans.indexOf(vlan) == -1) {
284           CpeuiSvc.addVlan(svc_id, uni_id, vlan, function() {
285             allvlans.push(vlan);
286           });
287         }
288         $('#vlan_input').val(undefined);
289       };
290     };
291
292     $scope.editVlanDialog = new CpeuiDialogs.Dialog('EditVlans', {}, undefined, editVlanController);
293
294     $scope.sortEvc = function(evc) {
295       return evc.evc['evc-id'];
296     };
297     $scope.sortUni = function(uni) {
298       return uni['uni-id'];
299     };
300     $scope.sortIpvc = function(ipvc) {
301       return ipvc['ipvc-id'];
302     };
303
304     init();
305   });
306 });