6d41f7bba8941ea63aeff9f205bd4db98c49107f
[unimgr.git] / dlux / cpeui / cpeui-module / src / main / resources / cpeui / services / cpeui.services.js
1 define(['app/cpeui/cpeui.module'],function(cpeui) {
2
3     cpeui.factory('CpeuiSvc', function($http) {
4         var baseUrl = "/restconf/config/mef-global:mef-global/";
5         var svc = {};
6
7         svc.getTenantList = function(callback) {
8             var tenantList = [];
9             $http({
10                 method:'GET',
11                 url:baseUrl + "tenants-instances"
12             }).then(function successCallback(response) {
13                 tenantList = response.data["tenants-instances"]["tenant-list"];
14                 if (callback != undefined) {
15                     callback(tenantList);
16                 }
17             }, function errorCallback(response) {
18                 if (response.status == 404) {
19                   callback([]);
20                 } else {
21                   console.log(response);
22                 }
23             });
24         };
25
26         svc.addTenant = function(name, callback){
27             $http({
28                 method:'POST',
29                 url:baseUrl + "tenants-instances/",
30                 data: {"tenant-list":[{
31                       "name": name
32                     }]}
33             }).then(function successCallback(response) {
34                 if (callback != undefined) {
35                     callback();
36                 }
37             });
38         };
39         svc.deleteTenant = function(id, callback) {
40             $http({
41                 method:'DELETE',
42                 url:baseUrl + "tenants-instances/tenant-list/" + id + "/",
43             }).then(function successCallback(response) {
44                 if (callback != undefined) {
45                     callback();
46                 }
47             });
48         };
49
50         // Profiles
51         svc.getProfiles = function(callback) {
52           $http({
53               method:'GET',
54               url:"/restconf/config/mef-global:mef-global/profiles/ingress-bwp-flows/"
55           }).then(function successCallback(response) {
56               if (callback != undefined) {
57                   callback(response.data["ingress-bwp-flows"]["bwp-flow"]);
58               }
59           }, function errorCallback(response) {
60               if (response.status == 404) {
61                   callback([]);
62               }
63               console.log(response);
64           });
65       };
66
67       svc.addProfile = function(name, cir, cbs, callback){
68           $http({
69               method:'POST',
70               url:"/restconf/config/mef-global:mef-global/profiles/ingress-bwp-flows/",
71               data: {"bwp-flow":{
72                         "bw-profile" : name,
73                          "cir" : cir,
74                          "cbs" : cbs
75                     }}
76           }).then(function successCallback(response) {
77               if (callback != undefined) {
78                   callback();
79               }
80           });
81       };
82
83       svc.editProfile = function(name, cir, cbs, callback){
84           $http({
85               method:'PUT',
86               url:"/restconf/config/mef-global:mef-global/profiles/ingress-bwp-flows/bwp-flow/"+name,
87               data: {"bwp-flow":{
88                       "bw-profile": name,
89                       cir: cir,
90                       cbs:cbs
91                     }}
92           }).then(function successCallback(response) {
93               if (callback != undefined) {
94                   callback();
95               }
96           });
97       };
98       
99       svc.deleteProfile = function(name, callback) {
100           $http({
101               method:'DELETE',
102               url:"/restconf/config/mef-global:mef-global/profiles/ingress-bwp-flows/bwp-flow/"+name,
103           }).then(function successCallback(response) {
104               if (callback != undefined) {
105                   callback();
106               }
107           });
108       };
109
110         // CEs
111         svc.addCe = function(id, name, callback) {
112             $http({
113                 method:'POST',
114                 url:"/restconf/config/mef-topology:mef-topology/devices/",
115                 data:{
116                       "device": [
117                                  {
118                                    "dev-id": id,
119                                    "device-name":name,
120                                    "role": "ce",
121                                    "interfaces": {"interface": []}
122                                  }
123                                ]
124                              }
125
126             }).then(function successCallback(response) {
127                 if (callback != undefined) {
128                     callback();
129                 }
130             });
131         };
132
133         svc.addCeName = function(ce, new_name, callback) {
134           $http({
135             method:'POST',
136             url:"/restconf/config/mef-topology:mef-topology/devices/device/" + ce['dev-id'],
137             data: {"device-name": new_name}
138         }).then(function successCallback(response) {
139             if (callback != undefined) {
140                 callback();
141             }
142           }, function errorCallback(response) {
143             console.log(response);
144             $http({
145               method:'GET',
146               url:"/restconf/config/mef-topology:mef-topology/devices/device/" + ce['dev-id']
147             }).then(function successCallback(response) {
148               ce["device-name"] = response.data["device"][0]["device-name"];
149             });
150           });
151         };
152
153         svc.getCes = function(callback) {
154             var ces;
155             var operMap = {};
156
157             $http({
158                 method:'GET',
159                 url:"/restconf/operational/mef-topology:mef-topology/devices/"
160             }).then(function successCallback(response) {
161                 ces = response.data["devices"]["device"];
162                 ces.forEach(function(c) {
163                   c.displayName = c['dev-id'];
164                   operMap[c['dev-id']] = c;
165                 });
166             }).finally(function() {
167                 $http({
168                   method:'GET',
169                   url:"/restconf/config/mef-topology:mef-topology/devices/"
170                 }).then(function(response){
171                   var confCes = response.data["devices"]["device"];
172                   confCes.forEach(function(c) {
173                     c.displayName = c['device-name'] ? c['device-name'] : c['dev-id'];
174                     if (operMap[c['dev-id']]) {
175                       for (var attrname in c) {
176                         operMap[c['dev-id']][attrname] = c[attrname];
177                       }
178                     } else {
179                       operMap[c['dev-id']] = c;
180                     }
181                   });
182                 }).finally(function() {
183                   if (callback != undefined) {
184                     callback(Object.values(operMap));
185                   }
186                 });
187             });
188         };
189
190         svc.removeCe = function(ceid, callback) {
191              $http({
192                 method:'DELETE',
193                 url:"/restconf/config/mef-topology:mef-topology/devices/device/" + ceid + "/"
194             }).then(function successCallback(response) {
195                 if (callback != undefined) {
196                     callback();
197                 }
198             });
199         };
200
201         // UNIs
202
203         svc.addUni = function(id, tenantid, callback) {
204             $http({
205                 method:'POST',
206                 url:"/restconf/config/mef-interfaces:mef-interfaces/unis/",
207                 data:{"uni": [{
208                     "uni-id": id,
209                     "tenant-id":tenantid,
210                     "admin-state-enabled":true
211                     }]}
212             }).then(function successCallback(response) {
213                 if (callback != undefined) {
214                     callback();
215                 }
216             });
217         };
218
219
220         svc.updateUni = function(id, tenantid, callback) {
221             // TODO didn't find a better way to keep other uni fields, PATCH method is not supported :(
222             $http({
223                 method:'GET',
224                 url:"/restconf/operational/mef-interfaces:mef-interfaces/unis/uni/" + id + "/"
225             }).then(function successCallback(response) {
226                 uni = response.data;
227                 uni["uni"][0]["tenant-id"] = tenantid;
228                 if (!tenantid) {
229                   delete uni["uni"][0]["tenant-id"];
230                 }
231                 uni["uni"][0]["admin-state-enabled"] = true;
232                 $http({
233                   method:'PUT',
234                   url:"/restconf/config/mef-interfaces:mef-interfaces/unis/uni/" + id + "/",
235                     data:uni
236                 }).then(function successCallback(response) {
237                     if (callback != undefined) {
238                         callback();
239                     }
240                 });
241             });
242         };
243
244         svc.getUnis = function(callback) {
245             var unis;
246             $http({
247                 method:'GET',
248                 url:"/restconf/operational/mef-interfaces:mef-interfaces/unis/"
249             }).then(function successCallback(response) {
250                 unis = response.data["unis"]["uni"];
251                 if (unis != undefined){
252                     for (i=0; i < unis.length; i++) {
253                         if ((unis[i]["physical-layers"] != undefined) && (unis[i]["physical-layers"].links != undefined)){
254                             unis[i].device = unis[i]["physical-layers"].links.link[0].device;
255                         }
256                     }
257                 }
258                 var confMap = {}
259                 $http({
260                   method:'GET',
261                   url:"/restconf/config/mef-interfaces:mef-interfaces/unis/"
262                 }).then(function(response){
263                   var confUnis = response.data["unis"]["uni"];
264                   confUnis.forEach(function(u) {
265                     confMap[u['uni-id']] = u;
266                   });
267                 }).finally(function(){
268                   unis.forEach(function(u) {
269                     u.prettyID = u['uni-id'].split(":")[u['uni-id'].split(":").length - 1];
270                     // copy config fields like tenant-id
271                     if (confMap[u['uni-id']]){
272                       for (var attrname in confMap[u['uni-id']]) {
273                         u[attrname] = confMap[u['uni-id']][attrname]; 
274                       }
275                     }
276                   });
277                   if (callback != undefined) {
278                     callback(unis);
279                   }
280                 });
281             }, function errorCallback(response) {
282                 if (response.status == 404) {
283                   callback([]);
284                 }
285                 console.log(response);
286             });
287         };
288
289         svc.getTenantUnis = function(tenantid, callback) {
290             var unis;
291             svc.getUnis(function(unis){
292                 if (unis != undefined){
293                     unis = unis.filter(function(u){return u["tenant-id"] == tenantid;});
294                 }
295                 if (callback != undefined) {
296                     callback(unis);
297                 }
298             });
299         };
300
301         svc.removeUni = function(uniid, callback) {
302              $http({
303                 method:'DELETE',
304                 url:"/restconf/config/mef-interfaces:mef-interfaces/unis/uni/" + uniid + "/"
305             }).then(function successCallback(response) {
306                 if (callback != undefined) {
307                     callback();
308                 }
309             });
310         };
311
312         // IPVCs
313         svc.addIpvc = function(ipvc, tenant, callback) {
314           var data = {
315             "mef-service" :  {
316               "svc-id" : ipvc.id,
317               "svc-type" : 'eplan',
318               "tenant-id" : tenant,
319               "ipvc" : {
320                 "ipvc-id" : ipvc.id,
321                 "ipvc-type" : 'multipoint',
322               }
323             }
324           };
325           $http({
326               method:'POST',
327               url:"/restconf/config/mef-services:mef-services/",
328               data:data
329           }).then(function successCallback(response) {
330               if (callback != undefined) {
331                   callback();
332               }
333           });
334       };
335
336       svc.addIpUni = function(uniid, ipuni_id, ip_address, vlan, segmentation_id, callback) {
337         var data = {"ip-uni":{
338           "ip-uni-id": ipuni_id,
339           "ip-address": ip_address
340         }};
341         if (vlan){
342           data["ip-uni"].vlan = vlan;
343         }
344         if (segmentation_id) {
345           data["ip-uni"]["segmentation-id"] = segmentation_id;
346         }
347         $http({
348             method:'POST',
349             url:"/restconf/config/mef-interfaces:mef-interfaces/unis/uni/"+uniid+"/ip-unis/",
350             data:data
351         }).then(function successCallback(response) {
352             if (callback != undefined) {
353                 callback();
354             }
355         });
356     };
357     
358     svc.getAllIpUniSubnets = function(callback) {
359       $http({
360           method:'GET',
361           url : "/restconf/config/mef-interfaces:mef-interfaces/subnets/"
362       }).then(function successCallback(response) {
363           var raw_subnets = response.data["subnets"]["subnet"];
364           var subnets ={}
365           raw_subnets.forEach(function(sub){
366             if (subnets[sub["uni-id"]] == undefined) {
367               subnets[sub["uni-id"]] = {};
368             }
369             if (subnets[sub["uni-id"]][sub["ip-uni-id"]] == undefined) {
370               subnets[sub["uni-id"]][sub["ip-uni-id"]] = [];
371             }
372             subnets[sub["uni-id"]][sub["ip-uni-id"]].push(sub);
373           });
374           if (callback != undefined) {
375               callback(subnets);
376           }
377       }, function errorCallback(response) {
378           console.log(response);
379       });
380   };
381
382   svc.addIpUniSubnet = function(uniid, ipuniid, subnet, gateway, callback) {
383         var data = {
384             "subnet": 
385             {
386               "subnet": subnet,
387               "uni-id":uniid,
388               "ip-uni-id":ipuniid,
389               "gateway": gateway
390             }
391         };
392         $http(
393             {
394               method : 'POST',
395               url : "/restconf/config/mef-interfaces:mef-interfaces/subnets/",                                
396               data : data
397             }).then(function successCallback(response) {
398           if (callback != undefined) {
399             callback();
400           }
401         });
402       };
403     
404     svc.deleteIpUniSubnet = function(uniid, ipuni_id, subnet, callback) {
405         
406         $http({
407             method:'DELETE',
408             url:"/restconf/config/mef-interfaces:mef-interfaces/subnets/subnet/"+uniid+"/"+ipuni_id+"/"+subnet.replace("/","%2F")+"/"
409         }).then(function successCallback(response) {
410             if (callback != undefined) {
411                 callback();
412             }
413         });
414     };
415     svc.deleteIpUni = function(uniid, ipuni_id, callback) {
416         
417         $http({
418             method:'DELETE',
419             url:"/restconf/config/mef-interfaces:mef-interfaces/unis/uni/"+uniid+"/ip-unis/ip-uni/"+ipuni_id+"/"
420         }).then(function successCallback(response) {
421             if (callback != undefined) {
422                 callback();
423             }
424         });
425     };
426     
427     svc.getIpUniSubnets = function(uniid, ipuni_id, callback) {
428       $http({
429           method:'GET',
430           url:"/restconf/config/mef-interfaces:mef-interfaces/subnets/"
431             //subnet/"+uniid+"/ip-unis/ip-uni/"+ipuni_id+"/subnets"
432       }).then(function successCallback(response) {
433           subnets = response.data["subnets"]["subnet"];
434           subnets = subnets.filterByField('uni-id',uniid).filterByField('ip-uni-id',ipuni_id);
435           if (callback != undefined) {
436               callback(subnets);
437           }
438       });
439     };
440
441     
442     // EVCs
443     function getJsonUnis(unis) {
444             var uni_json = [];
445             if (unis == undefined) {
446                 unis = [];
447             }
448             unis.forEach(function(i){uni_json.push({"uni-id":i});});
449             return uni_json;
450         }
451     
452     svc.addEvc = function(evc, evc_type, tenant, callback) {
453             var uni_json = getJsonUnis(evc.unis);
454 //            preserved-vlan
455             var data = {
456               "mef-service" :  {
457                 "svc-id" : evc.id,
458                 "svc-type" : evc.svc_type,
459                 "tenant-id" : tenant,
460                 "evc" : {
461                   "evc-id" : evc.id,
462                   "evc-type" : evc_type,
463                   "preserve-ce-vlan-id" : evc.is_preserve_vlan,
464                   "max-svc-frame-size" : evc.mtu_size,
465                   "unicast-svc-frm-delivery" : evc.unicast,
466                   "multicast-svc-frm-delivery" : evc.multicast,
467                   "mac-timeout":evc.mac_timeout,
468                   "unis" : {
469                     "uni" : uni_json
470                   },
471                   "admin-state-enabled" : true
472                 }
473               }
474             };
475             if (evc.is_preserve_vlan) {
476               data["mef-service"]["evc"]["preserved-vlan"] = evc.preserved_vlan;
477             }
478             if (evc.subnet) {
479               data["mef-service"]["evc"].subnet = evc.subnet;
480             }
481             if (evc.segmentation_id) {
482               data["mef-service"]["evc"]["segmentation-id"] = evc.segmentation_id;
483             }
484             $http({
485                 method:'POST',
486                 url:"/restconf/config/mef-services:mef-services/",
487                 data:data
488             }).then(function successCallback(response) {
489                 if (callback != undefined) {
490                     callback();
491                 }
492             });
493         };
494         svc.getServices = function(tenantid, callback) {
495             var evcs;
496             $http({
497                 method:'GET',
498                 url:"/restconf/config/mef-services:mef-services/"
499             }).then(function successCallback(response) {
500                 evcs = response.data["mef-services"]["mef-service"]; // TODO try to filter on server side
501                 if (evcs != undefined) {
502                     evcs = evcs.filter(function(evc){return evc["tenant-id"] == tenantid;});
503                     for (i=0; i < evcs.length; i++) {
504                         if ((evcs[i].evc != undefined) && (evcs[i].evc.unis.uni != undefined)) {
505                             var unis = evcs[i].evc.unis.uni;
506                             for (j=0; j < unis.length; j++) {
507                                 if ((unis[j]['evc-uni-ce-vlans'] != undefined) && (unis[j]['evc-uni-ce-vlans']['evc-uni-ce-vlan'] != undefined)){
508                                     unis[j].vlans = unis[j]['evc-uni-ce-vlans']['evc-uni-ce-vlan'].map(function(u){return u.vid;}).sort();
509                                 } else {
510                                     unis[j].vlans = [];
511                                 }
512                             }
513                         }
514                     }
515                 }
516                 if (callback != undefined) {
517                     callback(evcs);
518                 }
519             }, function errorCallback(response) {
520                 console.log(response);
521             });
522             return evcs;
523         };
524
525         svc.getAllServices = function(callback) {
526           $http({
527               method:'GET',
528               url:"/restconf/config/mef-services:mef-services/"
529           }).then(function successCallback(response) {
530               if (callback != undefined) {
531                   callback(response.data["mef-services"]["mef-service"]);
532               }
533           }, function errorCallback(response) {
534               if (response.status == 404) {
535                 callback([]);
536               }
537               console.log(response);
538           });
539         };
540
541         svc.addTenantToService = function(svcId, tenantName, callbackSuccess, callbackFailure){
542           $http({
543             method:'POST',
544             url:"/restconf/config/mef-services:mef-services/mef-service/" + svcId,
545             data:{"tenant-id":tenantName}
546           }).then(function() {
547               if (callbackSuccess != undefined) {
548                 callbackSuccess();
549               }
550           }, function() {
551             if (callbackFailure != undefined) {
552               callbackFailure();
553             } else {
554               console.log(response);
555             }
556           });
557         };
558
559         svc.removeEvc = function(svcid, callback) {
560              $http({
561                 method:'DELETE',
562                 url:"/restconf/config/mef-services:mef-services/mef-service/" + svcid + "/"
563             }).then(function successCallback(response) {
564                 if (callback != undefined) {
565                     callback();
566                 }
567             });
568         };
569
570         svc.addIpvcUni = function(svcid, uni_id, ipuni_id, profile_name, callback) {
571           var data = {"uni":{
572                           "uni-id":uni_id,
573                           "ip-uni-id":ipuni_id
574                           }
575                       };
576           if (profile_name) {
577             data.uni["ingress-bw-profile"] = profile_name;
578           }
579            $http({
580               method:'PUT',
581               url:"/restconf/config/mef-services:mef-services/mef-service/" + svcid + "/ipvc/unis/uni/"+uni_id+"/"+ipuni_id,
582               data: data
583           }).then(function successCallback(response) {
584               if (callback != undefined) {
585                   callback();
586               }
587           }, function failureCallback(response) {
588               if (callback != undefined) {
589                   callback();
590               }
591           });
592       };
593       
594       svc.deleteIpvcUni = function(svcid, uni_id, ipuni_id, callback) {
595         $http({
596            method:'DELETE',
597            url:"/restconf/config/mef-services:mef-services/mef-service/" + svcid + "/ipvc/unis/uni/" + uni_id +"/"+ipuni_id + "/"
598        }).then(function successCallback(response) {
599            if (callback != undefined) {
600                callback();
601            }
602        });
603    };
604       
605       
606       
607         svc.addEvcUni = function(svcid, uni_id, role, vlans, profile_name, callback) {
608             var data = {"uni":{
609                             "uni-id":uni_id,
610                             "role":role,
611                             "admin-state-enabled":true
612                             }
613                         };
614             if (profile_name) {
615               data.uni["ingress-bw-profile"] = profile_name;
616             }
617             if (vlans != undefined) {
618                 data.uni['evc-uni-ce-vlans'] = {"evc-uni-ce-vlan":[]}
619                 for (var i=0; i< vlans.length; ++i) {
620                     data.uni['evc-uni-ce-vlans']["evc-uni-ce-vlan"].push({"vid":vlans[i]});
621                 }
622             }
623              $http({
624                 method:'POST',
625                 url:"/restconf/config/mef-services:mef-services/mef-service/" + svcid + "/evc/unis",
626                 data: data
627             }).then(function successCallback(response) {
628                 if (callback != undefined) {
629                     callback();
630                 }
631             }, function failureCallback(response) {
632                 if (callback != undefined) {
633                     callback();
634                 }
635             });
636         };
637
638         svc.addEvcUniVlan = function(svcid, uni_id, vlan, callback) {
639              $http({
640                 method:'POST',
641                 url:"/restconf/config/mef-services:mef-services/mef-service/" + svcid + "/evc/unis/uni/" + uni_id + "/evc-uni-ce-vlans/",
642                 data:{"evc-uni-ce-vlan":{"vid":vlan}}
643             }).then(function successCallback(response) {
644                 if (callback != undefined) {
645                     callback();
646                 }
647             });
648         };
649
650         svc.deleteEvcUni = function(svcid, uni_id, callback) {
651              $http({
652                 method:'DELETE',
653                 url:"/restconf/config/mef-services:mef-services/mef-service/" + svcid + "/evc/unis/uni/" + uni_id + "/"
654             }).then(function successCallback(response) {
655                 if (callback != undefined) {
656                     callback();
657                 }
658             });
659         };
660
661
662
663         svc.deleteVlan = function(svc_id, uni_id, vlan,callback) {
664             $http({
665                 method:'DELETE',
666                 url:"/restconf/config/mef-services:mef-services/mef-service/" + svc_id + "/evc/unis/uni/" + uni_id + "/evc-uni-ce-vlans/evc-uni-ce-vlan/"+ vlan+"/"
667             }).then(function successCallback(response) {
668                 if (callback != undefined) {
669                     callback();
670                 }
671             });
672         };
673
674         svc.addVlan = function(svc_id, uni_id, vlan, callback) {
675             $http({
676                 method:'PUT',
677                 url:"/restconf/config/mef-services:mef-services/mef-service/" + svc_id + "/evc/unis/uni/"+uni_id+"/evc-uni-ce-vlans/evc-uni-ce-vlan/"+vlan+"/",
678                 data:{"evc-uni-ce-vlan":{"vid":vlan}}
679             }).then(function successCallback(response) {
680                 if (callback != undefined) {
681                     callback();
682                 }
683             });
684         };
685
686         svc.getNetworkNames = function(callback){
687           $http({
688             method:'GET',
689             url:"/restconf/config/neutron:neutron/networks/"
690         }).then(function successCallback(response) {
691             if (callback != undefined) {
692                 callback(response.data.networks.network);
693             }
694         });
695         };
696
697         return svc;
698
699     });
700 });