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