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