Sort CEs in L2 unis table
[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         L2:{},L3:{}
19     };
20
21     var tabIndexs = {
22         "inventory" : 0,
23         "L2" : 1,
24         "L3" : 2,
25       }
26     if ($stateParams.tenantTabName in tabIndexs) {
27       $scope.tab.tenantData = tabIndexs[$stateParams.tenantTabName];
28     } else {
29         $scope.tab.tenantData = tabIndexs.inventory;
30     }
31
32     CpeuiSvc.getTenantList(function(tenant_list) {
33         if (tenant_list.filter(t => t.name == $scope.curTenant).length == 0) {
34             window.location = "#/cpeui/admin/tenants";
35         }
36     });
37
38     function init(){
39       $scope.updateUnis(function(unis){
40         CpeuiSvc.getCes(function(ces) {
41           $scope.ces = ces.filter(function(item) {
42
43             var filteredUnis = unis.filterByField('device', item["dev-id"]);
44             filteredUnis = filteredUnis.filterByField('prettyID', 'br-int', true);
45             filteredUnis = filteredUnis.filter(function(i){return !(i.prettyID && i.prettyID.startsWith('tun'));});
46
47             return (filteredUnis.length);
48           });
49           ces.forEach(function(ce){
50             $scope.cesDisplayNames[ce['dev-id']] = ce['device-name'] ? ce['device-name'] : ce['dev-id'];
51           });
52           $scope.updateEvcView();
53         });
54       });
55
56       CpeuiSvc.getNetworkNames(function(networks){
57         networks.forEach(function(net){
58           $scope.networkNames[net.uuid] = net.name;
59         });
60       });
61
62       CpeuiSvc.getProfiles(function(profiles) {
63         $scope.profiles = profiles;
64       });
65     }
66
67     $scope.updateUnis = function(callback) {
68       CpeuiSvc.getUnis(function(unis) {
69         unis.forEach(function(u) {
70           u.prettyID = u['uni-id'].split(":")[u['uni-id'].split(":").length - 1];
71           $scope.unisMap[u['uni-id']] = u;
72         });
73         $scope.unis = unis.filter(function(u){return u["tenant-id"] == $scope.curTenant;});
74
75         if (callback) {
76           callback($scope.unis);
77         }
78       });
79     };
80
81     $scope.updateEvcView = function() {
82       CpeuiSvc.getServices($scope.curTenant, function(services) {
83
84         $scope.evcs = services.filter(function(svc){ return svc.evc != undefined;});
85         $scope.ipvcs = services.filter(function(svc){ return svc.ipvc != undefined;});
86         $scope.updateUnis();
87         function mapUniToService(uni, service) {
88             var uniObj = $scope.unis.filterByField('uni-id',uni['uni-id'])[0];
89             if (uniObj === undefined) {
90                 return;
91             }
92             if (!uniObj.vlanToService) {
93                 uniObj.vlanToService = [];
94             }
95             uniObj.hasService = true;
96             if (uni['evc-uni-ce-vlans'] && uni['evc-uni-ce-vlans']['evc-uni-ce-vlan']){
97                 uni['evc-uni-ce-vlans']['evc-uni-ce-vlan'].forEach(function(v){
98                     uniObj.vlanToService.push({"vlan":v.vid, "svc":service});
99                 });
100             } else {
101                 if (uni["ip-uni-id"]) {
102                     var ipuni = $scope.unis.filterByField('uni-id',uni['uni-id'])[0];
103                     if (ipuni && ipuni["ip-unis"] && ipuni["ip-unis"]["ip-uni"]) {
104                         ipuni["ip-unis"]["ip-uni"].forEach(function(ipu){
105                             if (ipu['ip-uni-id'] == uni["ip-uni-id"]){
106                                 var vlan = ipu.vlan ? ipu.vlan : 0;
107                                 uniObj.vlanToService.push({"vlan":vlan, "svc":service});
108                             }
109                         });
110                     }
111                 } else {
112                     uniObj.vlanToService.push({"vlan":0, "svc":service});
113                 }
114             }
115         }
116
117         $scope.ipvcs.forEach(function(e){
118           if (e.ipvc.unis != undefined && e.ipvc.unis.uni != undefined){
119               e.ipvc.unis.uni.forEach(function(u){
120                 u.device = u['uni-id'].split(":")[u['uni-id'].split(":").length-2];
121                 u.prettyID = u['uni-id'].split(":")[u['uni-id'].split(":").length-1];
122                 mapUniToService(u,e);
123             });
124           }
125         });
126         $scope.evcs.forEach(function(e){
127           e.isTree = (e.evc['evc-type'] == 'rooted-multipoint');
128           e.device2unis = {};
129           if (e.evc.unis != undefined && e.evc.unis.uni != undefined){
130             e.evc.unis.uni.forEach(function(u){
131               if (e.device2unis[$scope.unisMap[u['uni-id']].device] == undefined){
132                 e.device2unis[$scope.unisMap[u['uni-id']].device] = [];
133               }
134               u.prettyID = u['uni-id'].split(":")[u['uni-id'].split(":").length - 1];
135               e.device2unis[$scope.unisMap[u['uni-id']].device].push(u);
136               mapUniToService(u,e);
137             });
138           }
139         });
140       });
141       CpeuiSvc.getAllIpUniSubnets(function(raw_subnets){
142           var subnets ={}
143           if (raw_subnets) {
144               raw_subnets.forEach(function(sub) {
145                 if (subnets[sub["uni-id"]] == undefined) {
146                   subnets[sub["uni-id"]] = {};
147                 }
148                 if (subnets[sub["uni-id"]][sub["ip-uni-id"]] == undefined) {
149                   subnets[sub["uni-id"]][sub["ip-uni-id"]] = [];
150                 }
151                 subnets[sub["uni-id"]][sub["ip-uni-id"]].push(sub);
152               });
153           }
154         $scope.subnets = subnets;
155       });
156     };
157
158     $scope.doesAllUniHasService = function(ceUnis) {
159         for (var i=0 ; i< ceUnis.length; ++i) {
160             if (!ceUnis[i].hasService) {
161                 return false;
162             }
163         }
164         return true;
165     }
166
167     $scope.title = function(str) {
168       if (!str) {
169         return str;
170         }
171       return str.split('-').map(function(s) {
172         return s[0].toUpperCase() + s.slice(1);
173       }).join(' ');
174     }
175
176     $scope.svcTypes = [ 'epl', 'evpl', 'eplan', 'evplan', 'eptree', 'evptree' ]
177     var evcTypes = {
178       'epl' : 'point-to-point',
179       'evpl' : 'point-to-point',
180       'eplan' : 'multipoint-to-multipoint',
181       'evplan' : 'multipoint-to-multipoint',
182       'eptree' : 'rooted-multipoint',
183       'evptree' : 'rooted-multipoint'
184     }
185
186     var addEvcController = function($scope, $mdDialog) {
187       $scope.initObj = function(svc) {
188           $scope.obj = angular.merge($scope.obj, svc);
189           if (!$scope.obj.evc) {
190               $scope.obj.evc = {};
191           }
192           if (!$scope.obj.evc['max-svc-frame-size']){
193               $scope.obj.evc['max-svc-frame-size'] = 1522;
194           }
195           if (!$scope.obj.evc['mac-timeout']){
196               $scope.obj.evc['mac-timeout'] = 300;
197           }
198       }
199       $scope.validate = function(form) {
200         form.svc_type.$setTouched(); // patch because angular bug http://stackoverflow.com/questions/36138442/error-not-showing-for-angular-material-md-select
201         console.log($scope);
202         return form.$valid;
203       };
204     };
205
206     $scope.editEvc = function($event, svc) {
207         new CpeuiDialogs.Dialog('AddEvc', {}, function(obj) {
208             obj['svc-id'] = svc['svc-id'];
209             CpeuiSvc.addEvc(obj, evcTypes[obj.svc_type], $scope.curTenant, function() {
210                   $scope.updateEvcView();
211                 });
212           }, addEvcController).show($event, {'svcTypes':$scope.svcTypes, svc:svc});
213     }
214
215     $scope.openMenu = function($mdOpenMenu, ev) {
216         originatorEv = ev;
217         $mdOpenMenu(ev);
218       };
219
220     $scope.evcDialog = new CpeuiDialogs.Dialog('AddEvc', {}, function(obj) {
221       CpeuiSvc.addEvc(obj, evcTypes[obj.svc_type], $scope.curTenant, function() {
222             $scope.updateEvcView();
223           });
224     }, addEvcController);
225
226     $scope.ipvcDialog = new CpeuiDialogs.Dialog('AddIpvc', {}, function(obj) {
227       CpeuiSvc.addIpvc(obj, $scope.curTenant, function() {
228             $scope.updateEvcView();
229           });
230     });
231
232     $scope.linkIpvcUniDialog = new CpeuiDialogs.Dialog('LinkIpvcUni', {},
233         function(obj) {
234           CpeuiSvc.addIpvcUni(obj.svc_id, obj.uni['uni-id'], obj.ip_uni, obj.profile_name,
235               function() {
236                 $scope.updateEvcView();
237               });
238         });
239
240     var ipUniDialogController = function($scope, $mdDialog) {
241       $scope.hasVlans = false;
242       if ($scope.params.uni['ip-unis'] && $scope.params.uni['ip-unis']['ip-uni']) {
243         var ipunis = $scope.params.uni['ip-unis']['ip-uni'];
244         for (i = 0; i < ipunis.length; i++) {
245           if (ipunis[i].vlan){
246             $scope.hasVlans = true;
247           }
248         }
249       }
250     };
251
252     $scope.ipUniDialog = new CpeuiDialogs.Dialog('AddIpUni', {}, function(obj) {
253       CpeuiSvc.addIpUni(obj['uni-id'], obj['ip-address'], obj.vlan, obj['segmentation-id'], function() {
254           $scope.updateUnis();
255         });
256       }, ipUniDialogController);
257
258     $scope.openIpUniDialog = function(event,uni){
259       if (uni['ip-unis'] && (uni['ip-unis']['ip-uni'] != undefined)){
260         var ipunis = uni['ip-unis']['ip-uni'];
261         for (i = 0; i < ipunis.length; i++) {
262           if (!ipunis[i].vlan){
263             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.")
264             return;
265           }
266         }
267       }
268       $scope.ipUniDialog.show(event,{'uniid':uni['uni-id'], uni:uni})
269     }
270
271
272     var staticRoutingController = function($scope, $mdDialog, params) {
273         $scope.add = function(obj){
274             if ($scope.projectForm.$valid) {
275                 if (!obj.selectedUni.subnets) {
276                     obj.selectedUni.subnets = [];
277                 }
278                 obj.selectedUni.subnets.push({"subnet":obj.network,"gateway":obj.gateway});
279
280                 if (!$scope.toAdd) {
281                     $scope.toAdd = [];
282                 }
283                 $scope.toAdd.push(obj);
284
285                 // reset form
286                 $scope.obj={};
287                 $scope.projectForm.$setPristine();
288                 $scope.projectForm.$setUntouched();
289
290             }
291         };
292
293         $scope.removeSubnet = function(u,subnet) {
294             if (!$scope.toRemove) {
295                 $scope.toRemove = [];
296             }
297             u.subnets = u.subnets.filter(s=>s!=subnet);
298             $scope.toRemove.push({"uni":u, "subnet":subnet});
299         }
300
301         $scope.done = function() {
302 //            if (!angular.equals({},$scope.obj)){
303 //                $scope.projectForm.$setSubmitted();
304 //                return;
305 //            }
306             if (!$scope.toRemove && !$scope.toAdd) {
307                 $mdDialog.hide();
308                 return;
309             }
310             CpeuiSvc.getAllIpUniSubnets(function(subnets){
311                 if ($scope.toRemove) {
312                     $scope.toRemove.forEach(function(u){
313                         subnets = subnets.filter(function(s) {
314                             if (s['uni-id'] != u.uni['uni-id']) {
315                                 return true;
316                             } else if (s['ip-uni-id'] != u.uni['ip-uni-id']) {
317                                 return true;
318                             } else if (s.subnet != u.subnet.subnet) {
319                                 return true;
320                             }
321                             return false;
322                         });
323                     });
324                 }
325                 if ($scope.toAdd) {
326                     $scope.toAdd.forEach(function(added){
327                         var u = added.selectedUni;
328                         subnets.push({
329                             "ip-uni-id":u['ip-uni-id'],
330                             "subnet":added.network,
331                             "gateway":added.gateway,
332                             "uni-id":u['uni-id'],
333                         });
334                     });
335                 }
336                 CpeuiSvc.setAllSubnets(subnets, $scope.callback);
337             });
338           $mdDialog.hide();
339         };
340
341       };
342
343     $scope.openRoutingDialog = function(ipvc) {
344         if (ipvc.ipvc.unis && ipvc.ipvc.unis.uni) {
345             ipvc.ipvc.unis.uni.forEach(function(u){
346                 var mefUni = $scope.getMefInterfaceIpvc(u['uni-id'],u['ip-uni-id']);
347                 u.ipAddress = mefUni['ip-address'];
348                 u.deviceName = $scope.cesDisplayNames[u.device];
349                 if ($scope.subnets[u['uni-id']]) {
350                     u.subnets = $scope.subnets[u['uni-id']][u['ip-uni-id']];
351                 }
352                 if (mefUni.vlan) { u.vlan = mefUni.vlan };
353             });
354         }
355         new CpeuiDialogs.Dialog('StaticRouting', {}, function() {
356             $scope.updateEvcView();
357         }, staticRoutingController).show(undefined, {"ipvc":ipvc, "subnets":angular.copy($scope.subnets)});
358     }
359
360     $scope.ipUniSubnetDialog = new CpeuiDialogs.Dialog('AddIpUniSubnet', {}, function(obj) {
361       CpeuiSvc.addIpUniSubnet(obj.uniid, obj.ipuniid, obj.subnet, obj.gateway, function() {
362         if ($scope.subnets[obj.uniid] == undefined){
363           $scope.subnets[obj.uniid] = {};
364         }
365         if ($scope.subnets[obj.uniid][obj.ipuniid] == undefined) {
366           $scope.subnets[obj.uniid][obj.ipuniid] = [];
367         }
368         $scope.subnets[obj.uniid][obj.ipuniid].push({
369           "uni-id": obj.uniid,
370           "ip-uni-id": obj.ipuniid,
371           "subnet": obj.subnet,
372           "gateway": obj.gateway
373         });
374       });
375     });
376
377     $scope.deleteIpUni = function(uniid, ipuni_id) {
378         CpeuiDialogs.confirm(function() {
379           CpeuiSvc.deleteIpUni(uniid, ipuni_id, function() {
380             $scope.updateEvcView(); // TODO update unis only
381           });
382         });
383       };
384       $scope.deleteIpvcUni = function(svc_id, uni_id, ipuni_id) {
385         CpeuiDialogs.confirm(function() {
386           CpeuiSvc.deleteIpvcUni(svc_id, uni_id, ipuni_id, function() {
387             $scope.updateEvcView();
388           });
389         });
390       };
391       $scope.getMefInterfaceIpvc = function(uni_id,ipuni_id){
392         var uni = $scope.unis.filterByField('uni-id',uni_id)[0];
393         if ((uni == undefined) || (uni['ip-unis'] == undefined) || (uni['ip-unis']['ip-uni'] == undefined)) {
394           return undefined;
395         }
396         return uni['ip-unis']['ip-uni'].filterByField('ip-uni-id',ipuni_id)[0];
397       }
398
399       $scope.deleteIpUniSubnet = function(uniid, ipuni_id, subnet) {
400           CpeuiDialogs.confirm(function() {
401             CpeuiSvc.deleteIpUniSubnet(uniid, ipuni_id, subnet, function() {
402               $scope.updateEvcView(); // TODO update unis only
403             });
404           });
405     };
406
407     $scope.deleteEvc = function(svcid) {
408       CpeuiDialogs.confirm(function() {
409         CpeuiSvc.removeEvc(svcid, function() {
410           $scope.updateEvcView();
411         });
412       });
413     };
414
415     $scope.deleteEvcUni = function(svc_id, uni_id) {
416       CpeuiDialogs.confirm(function() {
417         CpeuiSvc.deleteEvcUni(svc_id, uni_id, function() {
418           $scope.updateEvcView();
419         });
420       });
421     };
422
423     var linkEvcUniController = function($scope, $mdDialog, params) {
424       $scope.params = params;
425       $scope.obj = {
426         vlans : []
427       };
428       $scope.deleteVlan = function(vlan) {
429         $scope.obj.vlans.splice($scope.obj.vlans.indexOf(vlan), 1);
430       };
431       $scope.addVlan = function(vlan) {
432         if ($scope.obj.vlans.indexOf(vlan) == -1) {
433           $scope.obj.vlans.push(vlan);
434           console.log(vlan);
435         }
436         $('#vlan_input').val(undefined);
437       };
438
439       $scope.filterUsedUnis = function(evc){
440         return function(u) {
441           if (u.prettyID == 'br-int') {
442             return false;
443           }
444           if (u.prettyID && u.prettyID.startsWith('tun')) {
445             return false;
446           }
447           if (evc.evc == undefined || evc.evc.unis.uni == undefined){
448             return true;
449           }
450           return evc.evc.unis.uni.filterByField('uni-id',u['uni-id']).length == 0;
451         };
452       };
453     };
454
455     $scope.linkEvcUniDialog = new CpeuiDialogs.Dialog('LinkEvcUni', {},
456         function(obj) {
457           if (!obj.role) {
458             obj.role = "root";
459           }
460           CpeuiSvc.addEvcUni(obj.svc_id, obj.uni_id, obj.role, obj.vlans, obj.profile_name,
461               function() {
462                 $scope.updateEvcView();
463               });
464         }, linkEvcUniController);
465
466     var editVlanController = function($scope, $mdDialog, params) {
467       $scope.params = params;
468
469       $scope.deleteVlan = function(svc_id, uni_id, vlan, allvlans) {
470         CpeuiSvc.deleteVlan(svc_id, uni_id, vlan, function() {
471           allvlans.splice(allvlans.indexOf(vlan), 1)
472         });
473       };
474       $scope.addVlan = function(svc_id, uni_id, vlan, allvlans) {
475         if (allvlans == undefined) {
476           allvlans = [];
477         }
478         if (allvlans.indexOf(vlan) == -1) {
479           CpeuiSvc.addVlan(svc_id, uni_id, vlan, function() {
480             allvlans.push(vlan);
481           });
482         }
483         $('#vlan_input').val(undefined);
484       };
485     };
486
487     $scope.editVlanDialog = new CpeuiDialogs.Dialog('EditVlans', {}, undefined, editVlanController);
488
489     $scope.sortUni = function(uni) {
490       return uni['uni-id'];
491     };
492
493     $scope.getKeys = function(obj){
494         var keys = [];
495         for(var keyName in obj){
496             keys.push(keyName)
497         }
498         return keys;
499     }
500     $scope.sortCeFromId = function(ce) {
501         return $scope.cesDisplayNames[ce];
502     };
503
504     $scope.isEmpty = function(obj){
505         return angular.equals({}, obj);
506     }
507
508     init();
509   });
510 });