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