Fixes for dlux
[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
6     $scope.unisTables = {};
7     $scope.unis = [];
8     $scope.ces = [];
9     $scope.ipvcs = []
10     $scope.cesDisplayNames = {};
11     $scope.unisMap = {};
12     $scope.networkNames = {};
13
14
15     function init(){
16       $scope.updateUnis(function(unis){
17         CpeuiSvc.getCes(function(ces) {
18           $scope.ces = ces.filter(function(item) {
19             
20             var filteredUnis = unis.filterByField('device', item["dev-id"]); 
21             filteredUnis = filteredUnis.filterByField('prettyID', 'br-int', true);
22             filteredUnis = filteredUnis.filter(function(i){return !(i.prettyID && i.prettyID.startsWith('tun'));});
23
24             return (filteredUnis.length);
25           });
26           ces.forEach(function(ce){
27             $scope.cesDisplayNames[ce['dev-id']] = ce['device-name'] ? ce['device-name'] : ce['dev-id'];
28           });
29           $scope.updateEvcView();
30         });
31       });
32       
33       CpeuiSvc.getNetworkNames(function(networks){        
34         networks.forEach(function(net){
35           $scope.networkNames[net.uuid] = net.name;
36         });
37       });
38     }
39
40     $scope.updateUnis = function(callback) {
41       CpeuiSvc.getUnis(function(unis) {
42         unis.forEach(function(u) {
43           u.prettyID = u['uni-id'].split(":")[u['uni-id'].split(":").length - 1];
44           $scope.unisMap[u['uni-id']] = u;
45         });
46         $scope.unis = unis.filter(function(u){return u["tenant-id"] == $scope.curTenant;});
47
48         if (callback) {
49           callback($scope.unis);
50         }
51       });
52     };
53
54     $scope.updateEvcView = function() {
55       CpeuiSvc.getServices($scope.curTenant, function(services) {
56         
57         $scope.evcs = services.filter(function(svc){ return svc.evc != undefined;});
58         $scope.ipvcs = services.filter(function(svc){ return svc.ipvc != undefined;});
59
60         $scope.updateUnis();
61
62         $scope.ipvcs.forEach(function(e){
63           e.isTree = (e.ipvc['ipvc-type'] == 'rooted-multipoint');
64         });
65         
66         $scope.evcs.forEach(function(e){
67           e.isTree = (e.evc['evc-type'] == 'rooted-multipoint');
68           e.device2unis = {};
69           if (e.evc.unis.uni != undefined){
70             e.evc.unis.uni.forEach(function(u){
71               if (e.device2unis[$scope.unisMap[u['uni-id']].device] == undefined){
72                 e.device2unis[$scope.unisMap[u['uni-id']].device] = [];
73               }
74               u.prettyID = u['uni-id'].split(":")[u['uni-id'].split(":").length - 1];              
75               e.device2unis[$scope.unisMap[u['uni-id']].device].push(u);
76             });
77           }
78         });
79       });
80     };
81
82     $scope.title = function(str) {
83       if (!str) {
84         return str;
85         }
86       return str.split('-').map(function(s) {
87         return s[0].toUpperCase() + s.slice(1);
88       }).join(' ');
89     }
90
91     $scope.svcTypes = [ 'epl', 'evpl', 'eplan', 'evplan', 'eptree', 'evptree' ]
92     var evcTypes = {
93       'epl' : 'point-to-point',
94       'evpl' : 'point-to-point',
95       'eplan' : 'multipoint-to-multipoint',
96       'evplan' : 'multipoint-to-multipoint',
97       'eptree' : 'rooted-multipoint',
98       'evptree' : 'rooted-multipoint'
99     }
100
101     var addEvcController = function($scope, $mdDialog) {
102       $scope.validate = function(form) {
103         form.svc_type.$setTouched(); // patch because angular bug http://stackoverflow.com/questions/36138442/error-not-showing-for-angular-material-md-select
104         console.log($scope);
105         return form.$valid;
106       };
107     };
108
109     $scope.evcDialog = new CpeuiDialogs.Dialog('AddEvc', {}, function(obj) {
110       CpeuiSvc.addEvc(obj, evcTypes[obj.svc_type], $scope.curTenant, function() {
111             $scope.updateEvcView();
112           });
113     }, addEvcController);
114
115     $scope.deleteEvc = function(svcid) {
116       CpeuiDialogs.confirm(function() {
117         CpeuiSvc.removeEvc(svcid, function() {
118           $scope.updateEvcView();
119         });
120       });
121     };
122
123     $scope.deleteEvcUni = function(svc_id, uni_id) {
124       CpeuiDialogs.confirm(function() {
125         CpeuiSvc.deleteEvcUni(svc_id, uni_id, function() {
126           $scope.updateEvcView();
127         });
128       });
129     };
130
131     var linkEvcUniController = function($scope, $mdDialog, params) {
132       $scope.params = params;
133       $scope.obj = {
134         vlans : []
135       };
136       $scope.deleteVlan = function(vlan) {
137         $scope.obj.vlans.splice($scope.obj.vlans.indexOf(vlan), 1);
138       };
139       $scope.addVlan = function(vlan) {
140         if ($scope.obj.vlans.indexOf(vlan) == -1) {
141           $scope.obj.vlans.push(vlan);
142           console.log(vlan);
143         }
144         $('#vlan_input').val(undefined);
145       };
146       
147       $scope.filterUsedUnis = function(evc){
148         return function(u) {          
149           if (u.prettyID == 'br-int') {
150             return false;
151           }
152           if (u.prettyID && u.prettyID.startsWith('tun')) {
153             return false;
154           }
155           if (evc.evc.unis.uni == undefined){
156             evc.evc.unis.uni = [];     
157           }
158           return evc.evc.unis.uni.filterByField('uni-id',u['uni-id']).length == 0;
159         };
160       };
161     };
162
163     $scope.linkEvcUniDialog = new CpeuiDialogs.Dialog('LinkEvcUni', {},
164         function(obj) {
165           if (!obj.role) {
166             obj.role = "root";
167           }
168           CpeuiSvc.addEvcUni(obj.svc_id, obj.uni_id, obj.role, obj.vlans,
169               function() {
170                 $scope.updateEvcView();
171               });
172         }, linkEvcUniController);
173
174     var editVlanController = function($scope, $mdDialog, params) {
175       $scope.params = params;
176
177       $scope.deleteVlan = function(svc_id, uni_id, vlan, allvlans) {
178         CpeuiSvc.deleteVlan(svc_id, uni_id, vlan, function() {
179           allvlans.splice(allvlans.indexOf(vlan), 1)
180         });
181       };
182       $scope.addVlan = function(svc_id, uni_id, vlan, allvlans) {
183         if (allvlans == undefined) {
184           allvlans = [];
185         }
186         if (allvlans.indexOf(vlan) == -1) {
187           CpeuiSvc.addVlan(svc_id, uni_id, vlan, function() {
188             allvlans.push(vlan);
189           });
190         }
191         $('#vlan_input').val(undefined);
192       };
193     };
194
195     $scope.editVlanDialog = new CpeuiDialogs.Dialog('EditVlans', {}, undefined, editVlanController);
196
197     $scope.sortEvc = function(evc) {
198       return evc.evc['evc-id'];
199     };
200     $scope.sortUni = function(uni) {
201       return uni['uni-id'];
202     };
203     $scope.sortIpvc = function(ipvc) {
204       return ipvc['ipvc-id'];
205     };    
206
207     init();
208   });
209 });