2daea7ba52699c730ccfdc48aba77345300fa13c
[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/config/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/config/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].device = unis[i]["physical-layers"].links != undefined)){
171                             unis[i].device = unis[i]["physical-layers"].links.link[0].device;
172                         }
173                     }
174                 }
175                 unis.forEach(function(u) {
176                   u.prettyID = u['uni-id'].split(":")[u['uni-id'].split(":").length - 1];
177                 });
178                 if (callback != undefined) {
179                     callback(unis);
180                 }
181             }, function errorCallback(response) {
182                 console.log(response);
183             });
184         };
185
186         svc.getTenantUnis = function(tenantid, callback) {
187             var unis;
188             svc.getUnis(function(unis){
189                 if (unis != undefined){
190                     unis = unis.filter(function(u){return u["tenant-id"] == tenantid;});
191                 }
192                 if (callback != undefined) {
193                     callback(unis);
194                 }
195             });
196         };
197
198         svc.removeUni = function(uniid, callback) {
199              $http({
200                 method:'DELETE',
201                 url:"/restconf/config/mef-interfaces:mef-interfaces/unis/uni/" + uniid + "/"
202             }).then(function successCallback(response) {
203                 if (callback != undefined) {
204                     callback();
205                 }
206             });
207         };
208
209         // EVCs
210
211         function getJsonUnis(unis) {
212             var uni_json = [];
213             if (unis == undefined) {
214                 unis = [];
215             }
216             unis.forEach(function(i){uni_json.push({"uni-id":i});});
217             return uni_json;
218         }
219
220         svc.addEvc = function(evc, evc_type, tenant, callback) {
221             var uni_json = getJsonUnis(evc.unis);
222 //            preserved-vlan
223             var data = {
224               "mef-service" :  {
225                 "svc-id" : evc.id,
226                 "svc-type" : evc.svc_type,
227                 "tenant-id" : tenant,
228                 "evc" : {
229                   "evc-id" : evc.id,
230                   "evc-type" : evc_type,
231                   "preserve-ce-vlan-id" : evc.is_preserve_vlan,
232                   "max-svc-frame-size" : evc.mtu_size,
233                   "unicast-svc-frm-delivery" : evc.unicast,
234                   "multicast-svc-frm-delivery" : evc.multicast,
235                   "unis" : {
236                     "uni" : uni_json
237                   },
238                   "admin-state-enabled" : true
239                 }
240               } 
241             };
242             if (evc.is_preserve_vlan) {
243               data["mef-service"]["evc"]["preserved-vlan"] = evc.preserved_vlan;
244             }
245             $http({
246                 method:'POST',
247                 url:"/restconf/config/mef-services:mef-services/",
248                 data:data
249             }).then(function successCallback(response) {
250                 if (callback != undefined) {
251                     callback();
252                 }
253             });
254         };
255
256         svc.getServices = function(tenantid, callback) {
257             var evcs;
258             $http({
259                 method:'GET',
260                 url:"/restconf/config/mef-services:mef-services/"
261             }).then(function successCallback(response) {
262                 evcs = response.data["mef-services"]["mef-service"]; // TODO try to filter on server side
263
264                 if (evcs != undefined) {
265                     evcs = evcs.filter(function(evc){return evc["tenant-id"] == tenantid;});
266                     for (i=0; i < evcs.length; i++) {
267                         if ((evcs[i].evc != undefined) && (evcs[i].evc.unis.uni != undefined)) {
268                             var unis = evcs[i].evc.unis.uni;
269                             for (j=0; j < unis.length; j++) {
270                                 if ((unis[j]['evc-uni-ce-vlans'] != undefined) && (unis[j]['evc-uni-ce-vlans']['evc-uni-ce-vlan'] != undefined)){
271                                     unis[j].vlans = unis[j]['evc-uni-ce-vlans']['evc-uni-ce-vlan'].map(function(u){return u.vid;}).sort();
272                                 } else {
273                                     unis[j].vlans = [];
274                                 }
275                             }
276                         }
277                     }
278                 }
279                 if (callback != undefined) {
280                     callback(evcs);
281                 }
282             }, function errorCallback(response) {
283                 console.log(response);
284             });
285
286             return evcs;
287         };
288
289         svc.getAllServices = function(callback) {          
290           $http({
291               method:'GET',
292               url:"/restconf/config/mef-services:mef-services/"
293           }).then(function successCallback(response) {              
294               if (callback != undefined) {
295                   callback(response.data["mef-services"]["mef-service"]);
296               }
297           }, function errorCallback(response) {
298               console.log(response);
299           });
300         };
301         
302         svc.addTenantToService = function(svcId, tenantName, callbackSuccess, callbackFailure){
303           $http({
304             method:'POST',
305             url:"/restconf/config/mef-services:mef-services/mef-service/" + svcId,
306             data:{"tenant-id":tenantName}
307           }).then(function() {
308               if (callbackSuccess != undefined) {
309                 callbackSuccess();
310               }
311           }, function() {
312             if (callbackFailure != undefined) {
313               callbackFailure();
314             } else {
315               console.log(response);
316             }
317           });
318         };
319
320         svc.removeEvc = function(svcid, callback) {
321              $http({
322                 method:'DELETE',
323                 url:"/restconf/config/mef-services:mef-services/mef-service/" + svcid + "/"
324             }).then(function successCallback(response) {
325                 if (callback != undefined) {
326                     callback();
327                 }
328             });
329         };
330
331         svc.addEvcUni = function(svcid, uni_id, role, vlans, callback) {
332             var data = {"uni":{
333                             "uni-id":uni_id,
334                             "role":role,
335                             "admin-state-enabled":true
336                             }
337                         };
338             if (vlans != undefined) {
339                 data.uni['evc-uni-ce-vlans'] = {"evc-uni-ce-vlan":[]}
340                 for (var i=0; i< vlans.length; ++i) {
341                     data.uni['evc-uni-ce-vlans']["evc-uni-ce-vlan"].push({"vid":vlans[i]});
342                 }
343             }
344              $http({
345                 method:'POST',
346                 url:"/restconf/config/mef-services:mef-services/mef-service/" + svcid + "/evc/unis",
347                 data: data
348             }).then(function successCallback(response) {
349                 if (callback != undefined) {
350                     callback();
351                 }
352             }, function failureCallback(response) {
353                 if (callback != undefined) {
354                     callback();
355                 }
356             });
357         };
358
359         svc.addEvcUniVlan = function(svcid, uni_id, vlan, callback) {
360              $http({
361                 method:'POST',
362                 url:"/restconf/config/mef-services:mef-services/mef-service/" + svcid + "/evc/unis/uni/" + uni_id + "/evc-uni-ce-vlans/",
363                 data:{"evc-uni-ce-vlan":{"vid":vlan}}
364             }).then(function successCallback(response) {
365                 if (callback != undefined) {
366                     callback();
367                 }
368             });
369         };
370
371         svc.deleteEvcUni = function(svcid, uni_id, callback) {
372              $http({
373                 method:'DELETE',
374                 url:"/restconf/config/mef-services:mef-services/mef-service/" + svcid + "/evc/unis/uni/" + uni_id + "/"
375             }).then(function successCallback(response) {
376                 if (callback != undefined) {
377                     callback();
378                 }
379             });
380         };
381
382
383
384         svc.deleteVlan = function(svc_id, uni_id, vlan,callback) {
385             $http({
386                 method:'DELETE',
387                 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+"/"
388             }).then(function successCallback(response) {
389                 if (callback != undefined) {
390                     callback();
391                 }
392             });
393         };
394
395         svc.addVlan = function(svc_id, uni_id, vlan, callback) {
396             $http({
397                 method:'PUT',
398                 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+"/",
399                 data:{"evc-uni-ce-vlan":{"vid":vlan}}
400             }).then(function successCallback(response) {
401                 if (callback != undefined) {
402                     callback();
403                 }
404             });
405         };
406
407         svc.getNetworkNames = function(callback){
408           $http({
409             method:'GET',
410             url:"/restconf/config/neutron:neutron/networks/"            
411         }).then(function successCallback(response) {
412             if (callback != undefined) {                
413                 callback(response.data.networks.network);
414             }
415         });
416         };
417         
418         return svc;
419
420     });
421 });