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