add auto generated id for mef elements
[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, Utils) {
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 ipvcId = Utils.randomId();
315           var data = {
316             "mef-service" :  {
317               "svc-id" : ipvcId,
318               "name" : ipvc.svc_name,
319               "svc-type" : 'eplan',
320               "tenant-id" : tenant,
321               "ipvc" : {
322                 "ipvc-id" : ipvcId,
323                 "ipvc-type" : 'multipoint',
324               }
325             }
326           };
327           $http({
328               method:'POST',
329               url:"/restconf/config/mef-services:mef-services/",
330               data:data
331           }).then(function successCallback(response) {
332               if (callback != undefined) {
333                   callback();
334               }
335           });
336       };
337
338       svc.addIpUni = function(uniid, ip_address, vlan, segmentation_id, callback) {
339         var ipUniId = Utils.randomId();
340         var data = {"ip-uni":{
341           "ip-uni-id": ipUniId,
342           "ip-address": ip_address
343         }};
344         if (vlan){
345           data["ip-uni"].vlan = vlan;
346         }
347         if (segmentation_id) {
348           data["ip-uni"]["segmentation-id"] = segmentation_id;
349         }
350         $http({
351             method:'POST',
352             url:"/restconf/config/mef-interfaces:mef-interfaces/unis/uni/"+uniid+"/ip-unis/",
353             data:data
354         }).then(function successCallback(response) {
355             if (callback != undefined) {
356                 callback();
357             }
358         });
359     };
360
361     svc.getAllIpUniSubnets = function(callback) {
362       $http({
363           method:'GET',
364           url : "/restconf/config/mef-interfaces:mef-interfaces/subnets/"
365       }).then(function successCallback(response) {
366           var raw_subnets = response.data["subnets"]["subnet"];
367           var subnets ={}
368           raw_subnets.forEach(function(sub){
369             if (subnets[sub["uni-id"]] == undefined) {
370               subnets[sub["uni-id"]] = {};
371             }
372             if (subnets[sub["uni-id"]][sub["ip-uni-id"]] == undefined) {
373               subnets[sub["uni-id"]][sub["ip-uni-id"]] = [];
374             }
375             subnets[sub["uni-id"]][sub["ip-uni-id"]].push(sub);
376           });
377           if (callback != undefined) {
378               callback(subnets);
379           }
380       }, function errorCallback(response) {
381           console.log(response);
382       });
383   };
384
385   svc.addIpUniSubnet = function(uniid, ipuniid, subnet, gateway, callback) {
386         var data = {
387             "subnet":
388             {
389               "subnet": subnet,
390               "uni-id":uniid,
391               "ip-uni-id":ipuniid,
392               "gateway": gateway
393             }
394         };
395         $http(
396             {
397               method : 'POST',
398               url : "/restconf/config/mef-interfaces:mef-interfaces/subnets/",
399               data : data
400             }).then(function successCallback(response) {
401           if (callback != undefined) {
402             callback();
403           }
404         });
405       };
406
407     svc.deleteIpUniSubnet = function(uniid, ipuni_id, subnet, callback) {
408
409         $http({
410             method:'DELETE',
411             url:"/restconf/config/mef-interfaces:mef-interfaces/subnets/subnet/"+uniid+"/"+ipuni_id+"/"+subnet.replace("/","%2F")+"/"
412         }).then(function successCallback(response) {
413             if (callback != undefined) {
414                 callback();
415             }
416         });
417     };
418     svc.deleteIpUni = function(uniid, ipuni_id, callback) {
419
420         $http({
421             method:'DELETE',
422             url:"/restconf/config/mef-interfaces:mef-interfaces/unis/uni/"+uniid+"/ip-unis/ip-uni/"+ipuni_id+"/"
423         }).then(function successCallback(response) {
424             if (callback != undefined) {
425                 callback();
426             }
427         });
428     };
429
430     svc.getIpUniSubnets = function(uniid, ipuni_id, callback) {
431       $http({
432           method:'GET',
433           url:"/restconf/config/mef-interfaces:mef-interfaces/subnets/"
434             //subnet/"+uniid+"/ip-unis/ip-uni/"+ipuni_id+"/subnets"
435       }).then(function successCallback(response) {
436           subnets = response.data["subnets"]["subnet"];
437           subnets = subnets.filterByField('uni-id',uniid).filterByField('ip-uni-id',ipuni_id);
438           if (callback != undefined) {
439               callback(subnets);
440           }
441       });
442     };
443
444
445     // EVCs
446     function getJsonUnis(unis) {
447             var uni_json = [];
448             if (unis == undefined) {
449                 unis = [];
450             }
451             unis.forEach(function(i){uni_json.push({"uni-id":i});});
452             return uni_json;
453         }
454
455     svc.addEvc = function(evc, evc_type, tenant, callback) {
456             var uni_json = getJsonUnis(evc.unis);
457 //            preserved-vlan
458             var evcId = Utils.randomId();
459             var data = {
460               "mef-service" :  {
461                 "svc-id" : evcId,
462                 "name" : evc.svc_name,
463                 "svc-type" : evc.svc_type,
464                 "tenant-id" : tenant,
465                 "evc" : {
466                   "evc-id" : evcId,
467                   "evc-type" : evc_type,
468                   "preserve-ce-vlan-id" : evc.is_preserve_vlan,
469                   "max-svc-frame-size" : evc.mtu_size,
470                   "unicast-svc-frm-delivery" : evc.unicast,
471                   "multicast-svc-frm-delivery" : evc.multicast,
472                   "mac-timeout":evc.mac_timeout,
473                   "unis" : {
474                     "uni" : uni_json
475                   },
476                   "admin-state-enabled" : true
477                 }
478               }
479             };
480             if (evc.is_preserve_vlan) {
481               data["mef-service"]["evc"]["preserved-vlan"] = evc.preserved_vlan;
482             }
483             if (evc.subnet) {
484               data["mef-service"]["evc"].subnet = evc.subnet;
485             }
486             if (evc.segmentation_id) {
487               data["mef-service"]["evc"]["segmentation-id"] = evc.segmentation_id;
488             }
489             $http({
490                 method:'POST',
491                 url:"/restconf/config/mef-services:mef-services/",
492                 data:data
493             }).then(function successCallback(response) {
494                 if (callback != undefined) {
495                     callback();
496                 }
497             });
498         };
499         svc.getServices = function(tenantid, callback) {
500             var evcs;
501             $http({
502                 method:'GET',
503                 url:"/restconf/config/mef-services:mef-services/"
504             }).then(function successCallback(response) {
505                 evcs = response.data["mef-services"]["mef-service"]; // TODO try to filter on server side
506                 if (evcs != undefined) {
507                     evcs = evcs.filter(function(evc){return evc["tenant-id"] == tenantid;});
508                     for (i=0; i < evcs.length; i++) {
509                         if ((evcs[i].evc != undefined) && (evcs[i].evc.unis.uni != undefined)) {
510                             var unis = evcs[i].evc.unis.uni;
511                             for (j=0; j < unis.length; j++) {
512                                 if ((unis[j]['evc-uni-ce-vlans'] != undefined) && (unis[j]['evc-uni-ce-vlans']['evc-uni-ce-vlan'] != undefined)){
513                                     unis[j].vlans = unis[j]['evc-uni-ce-vlans']['evc-uni-ce-vlan'].map(function(u){return u.vid;}).sort();
514                                 } else {
515                                     unis[j].vlans = [];
516                                 }
517                             }
518                         }
519                     }
520                 }
521                 if (callback != undefined) {
522                     callback(evcs);
523                 }
524             }, function errorCallback(response) {
525                 console.log(response);
526             });
527             return evcs;
528         };
529
530         svc.getAllServices = function(callback) {
531           $http({
532               method:'GET',
533               url:"/restconf/config/mef-services:mef-services/"
534           }).then(function successCallback(response) {
535               if (callback != undefined) {
536                   callback(response.data["mef-services"]["mef-service"]);
537               }
538           }, function errorCallback(response) {
539               if (response.status == 404) {
540                 callback([]);
541               }
542               console.log(response);
543           });
544         };
545
546         svc.addTenantToService = function(svcId, tenantName, callbackSuccess, callbackFailure){
547           $http({
548             method:'POST',
549             url:"/restconf/config/mef-services:mef-services/mef-service/" + svcId,
550             data:{"tenant-id":tenantName}
551           }).then(function() {
552               if (callbackSuccess != undefined) {
553                 callbackSuccess();
554               }
555           }, function() {
556             if (callbackFailure != undefined) {
557               callbackFailure();
558             } else {
559               console.log(response);
560             }
561           });
562         };
563
564         svc.removeEvc = function(svcid, callback) {
565              $http({
566                 method:'DELETE',
567                 url:"/restconf/config/mef-services:mef-services/mef-service/" + svcid + "/"
568             }).then(function successCallback(response) {
569                 if (callback != undefined) {
570                     callback();
571                 }
572             });
573         };
574
575         svc.addIpvcUni = function(svcid, uni_id, ipuni_id, profile_name, callback) {
576           var data = {"uni":{
577                           "uni-id":uni_id,
578                           "ip-uni-id":ipuni_id
579                           }
580                       };
581           if (profile_name) {
582             data.uni["ingress-bw-profile"] = profile_name;
583           }
584            $http({
585               method:'PUT',
586               url:"/restconf/config/mef-services:mef-services/mef-service/" + svcid + "/ipvc/unis/uni/"+uni_id+"/"+ipuni_id,
587               data: data
588           }).then(function successCallback(response) {
589               if (callback != undefined) {
590                   callback();
591               }
592           }, function failureCallback(response) {
593               if (callback != undefined) {
594                   callback();
595               }
596           });
597       };
598
599       svc.deleteIpvcUni = function(svcid, uni_id, ipuni_id, callback) {
600         $http({
601            method:'DELETE',
602            url:"/restconf/config/mef-services:mef-services/mef-service/" + svcid + "/ipvc/unis/uni/" + uni_id +"/"+ipuni_id + "/"
603        }).then(function successCallback(response) {
604            if (callback != undefined) {
605                callback();
606            }
607        });
608    };
609
610
611
612         svc.addEvcUni = function(svcid, uni_id, role, vlans, profile_name, callback) {
613             var data = {"uni":{
614                             "uni-id":uni_id,
615                             "role":role,
616                             "admin-state-enabled":true
617                             }
618                         };
619             if (profile_name) {
620               data.uni["ingress-bw-profile"] = profile_name;
621             }
622             if (vlans != undefined) {
623                 data.uni['evc-uni-ce-vlans'] = {"evc-uni-ce-vlan":[]}
624                 for (var i=0; i< vlans.length; ++i) {
625                     data.uni['evc-uni-ce-vlans']["evc-uni-ce-vlan"].push({"vid":vlans[i]});
626                 }
627             }
628              $http({
629                 method:'POST',
630                 url:"/restconf/config/mef-services:mef-services/mef-service/" + svcid + "/evc/unis",
631                 data: data
632             }).then(function successCallback(response) {
633                 if (callback != undefined) {
634                     callback();
635                 }
636             }, function failureCallback(response) {
637                 if (callback != undefined) {
638                     callback();
639                 }
640             });
641         };
642
643         svc.addEvcUniVlan = function(svcid, uni_id, vlan, callback) {
644              $http({
645                 method:'POST',
646                 url:"/restconf/config/mef-services:mef-services/mef-service/" + svcid + "/evc/unis/uni/" + uni_id + "/evc-uni-ce-vlans/",
647                 data:{"evc-uni-ce-vlan":{"vid":vlan}}
648             }).then(function successCallback(response) {
649                 if (callback != undefined) {
650                     callback();
651                 }
652             });
653         };
654
655         svc.deleteEvcUni = function(svcid, uni_id, callback) {
656              $http({
657                 method:'DELETE',
658                 url:"/restconf/config/mef-services:mef-services/mef-service/" + svcid + "/evc/unis/uni/" + uni_id + "/"
659             }).then(function successCallback(response) {
660                 if (callback != undefined) {
661                     callback();
662                 }
663             });
664         };
665
666
667
668         svc.deleteVlan = function(svc_id, uni_id, vlan,callback) {
669             $http({
670                 method:'DELETE',
671                 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+"/"
672             }).then(function successCallback(response) {
673                 if (callback != undefined) {
674                     callback();
675                 }
676             });
677         };
678
679         svc.addVlan = function(svc_id, uni_id, vlan, callback) {
680             $http({
681                 method:'PUT',
682                 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+"/",
683                 data:{"evc-uni-ce-vlan":{"vid":vlan}}
684             }).then(function successCallback(response) {
685                 if (callback != undefined) {
686                     callback();
687                 }
688             });
689         };
690
691         svc.getNetworkNames = function(callback){
692           $http({
693             method:'GET',
694             url:"/restconf/config/neutron:neutron/networks/"
695         }).then(function successCallback(response) {
696             if (callback != undefined) {
697                 callback(response.data.networks.network);
698             }
699         });
700         };
701
702         return svc;
703
704     });
705 });