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