UI: add UNI & IPVC tabs (work in-progress)
[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]["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         // IPVCs
210         svc.addIpvc = function(ipvc, tenant, callback) {
211 //          var uni_json = getJsonUnis(evc.unis);
212 //          preserved-vlan
213           var data = {
214             "mef-service" :  {
215               "svc-id" : ipvc.id,
216               "svc-type" : 'eplan',
217               "tenant-id" : tenant,
218               "ipvc" : {
219                 "ipvc-id" : ipvc.id,
220                 "ipvc-type" : 'multipoint',
221 //                "unis" : {
222 //                  "uni" : uni_json
223 //                },
224               }
225             }
226           };
227           $http({
228               method:'POST',
229               url:"/restconf/config/mef-services:mef-services/",
230               data:data
231           }).then(function successCallback(response) {
232               if (callback != undefined) {
233                   callback();
234               }
235           });
236       };
237
238       svc.addIpUni = function(uniid, ipuni_id, ip_address, vlan, subnets, callback) {
239         var data = {"ip-uni":{
240           "ip-uni-id": ipuni_id,
241           "ip-address": ip_address,          
242           "subnets":{
243                 "subnet":subnets
244           }
245         }};
246         if (vlan){
247           data["ip-uni"].vlan = vlan;
248         }
249         $http({
250             method:'POST',
251             url:"/restconf/config/mef-interfaces:mef-interfaces/unis/uni/"+uniid+"/ip-unis/",
252             data:data
253         }).then(function successCallback(response) {
254             if (callback != undefined) {
255                 callback();
256             }
257         });
258     };
259     
260       svc.addIpUniSubnet = function(uniid, ipuniid, subnet, gateway,
261           callback) {
262         var data = {
263             "subnet": 
264             {
265               "subnet": subnet,
266               "gateway": gateway
267             }
268           
269         };
270         $http(
271             {
272               method : 'POST',
273               url : "/restconf/config/mef-interfaces:mef-interfaces/unis/uni/"+uniid+"/ip-unis/ip-uni/"+ipuniid+"/subnets/",                                
274               data : data
275             }).then(function successCallback(response) {
276           if (callback != undefined) {
277             callback();
278           }
279         });
280       };
281     
282     svc.deleteIpUniSubnet = function(uniid, ipuni_id, subnet, callback) {
283         
284         $http({
285             method:'DELETE',
286             url:"/restconf/config/mef-interfaces:mef-interfaces/unis/uni/"+uniid+"/ip-unis/ip-uni/"+ipuni_id+"/subnets/subnet/"+subnet.replace("/","%2F")+"/"
287         }).then(function successCallback(response) {
288             if (callback != undefined) {
289                 callback();
290             }
291         });
292     };
293     svc.deleteIpUni = function(uniid, ipuni_id, callback) {
294         
295         $http({
296             method:'DELETE',
297             url:"/restconf/config/mef-interfaces:mef-interfaces/unis/uni/"+uniid+"/ip-unis/ip-uni/"+ipuni_id+"/"
298         }).then(function successCallback(response) {
299             if (callback != undefined) {
300                 callback();
301             }
302         });
303     };
304     
305     svc.getIpUniSubnets = function(uniid, ipuni_id, callback) {
306       $http({
307           method:'GET',
308           url:"/restconf/config/mef-interfaces:mef-interfaces/unis/uni/"+uniid+"/ip-unis/ip-uni/"+ipuni_id+"/subnets"
309       }).then(function successCallback(response) {
310           subnets = response.data["subnets"]["subnet"];
311           if (callback != undefined) {
312               callback(subnets);
313           }
314       });
315     };
316
317     
318     // EVCs
319     function getJsonUnis(unis) {
320             var uni_json = [];
321             if (unis == undefined) {
322                 unis = [];
323             }
324             unis.forEach(function(i){uni_json.push({"uni-id":i});});
325             return uni_json;
326         }
327         svc.addEvc = function(evc, evc_type, tenant, callback) {
328             var uni_json = getJsonUnis(evc.unis);
329 //            preserved-vlan
330             var data = {
331               "mef-service" :  {
332                 "svc-id" : evc.id,
333                 "svc-type" : evc.svc_type,
334                 "tenant-id" : tenant,
335                 "evc" : {
336                   "evc-id" : evc.id,
337                   "evc-type" : evc_type,
338                   "preserve-ce-vlan-id" : evc.is_preserve_vlan,
339                   "max-svc-frame-size" : evc.mtu_size,
340                   "unicast-svc-frm-delivery" : evc.unicast,
341                   "multicast-svc-frm-delivery" : evc.multicast,
342                   "unis" : {
343                     "uni" : uni_json
344                   },
345                   "admin-state-enabled" : true
346                 }
347               }
348             };
349             if (evc.is_preserve_vlan) {
350               data["mef-service"]["evc"]["preserved-vlan"] = evc.preserved_vlan;
351             }
352             $http({
353                 method:'POST',
354                 url:"/restconf/config/mef-services:mef-services/",
355                 data:data
356             }).then(function successCallback(response) {
357                 if (callback != undefined) {
358                     callback();
359                 }
360             });
361         };
362         svc.getServices = function(tenantid, callback) {
363             var evcs;
364             $http({
365                 method:'GET',
366                 url:"/restconf/config/mef-services:mef-services/"
367             }).then(function successCallback(response) {
368                 evcs = response.data["mef-services"]["mef-service"]; // TODO try to filter on server side
369                 if (evcs != undefined) {
370                     evcs = evcs.filter(function(evc){return evc["tenant-id"] == tenantid;});
371                     for (i=0; i < evcs.length; i++) {
372                         if ((evcs[i].evc != undefined) && (evcs[i].evc.unis.uni != undefined)) {
373                             var unis = evcs[i].evc.unis.uni;
374                             for (j=0; j < unis.length; j++) {
375                                 if ((unis[j]['evc-uni-ce-vlans'] != undefined) && (unis[j]['evc-uni-ce-vlans']['evc-uni-ce-vlan'] != undefined)){
376                                     unis[j].vlans = unis[j]['evc-uni-ce-vlans']['evc-uni-ce-vlan'].map(function(u){return u.vid;}).sort();
377                                 } else {
378                                     unis[j].vlans = [];
379                                 }
380                             }
381                         }
382                     }
383                 }
384                 if (callback != undefined) {
385                     callback(evcs);
386                 }
387             }, function errorCallback(response) {
388                 console.log(response);
389             });
390             return evcs;
391         };
392
393         svc.getAllServices = function(callback) {
394           $http({
395               method:'GET',
396               url:"/restconf/config/mef-services:mef-services/"
397           }).then(function successCallback(response) {
398               if (callback != undefined) {
399                   callback(response.data["mef-services"]["mef-service"]);
400               }
401           }, function errorCallback(response) {
402               console.log(response);
403           });
404         };
405
406         svc.addTenantToService = function(svcId, tenantName, callbackSuccess, callbackFailure){
407           $http({
408             method:'POST',
409             url:"/restconf/config/mef-services:mef-services/mef-service/" + svcId,
410             data:{"tenant-id":tenantName}
411           }).then(function() {
412               if (callbackSuccess != undefined) {
413                 callbackSuccess();
414               }
415           }, function() {
416             if (callbackFailure != undefined) {
417               callbackFailure();
418             } else {
419               console.log(response);
420             }
421           });
422         };
423
424         svc.removeEvc = function(svcid, callback) {
425              $http({
426                 method:'DELETE',
427                 url:"/restconf/config/mef-services:mef-services/mef-service/" + svcid + "/"
428             }).then(function successCallback(response) {
429                 if (callback != undefined) {
430                     callback();
431                 }
432             });
433         };
434
435         svc.addIpvcUni = function(svcid, uni_id, ipuni_id, callback) {
436           var data = {"uni":{
437                           "uni-id":uni_id,
438                           "ip-uni-id":ipuni_id
439                           }
440                       };          
441            $http({
442               method:'PUT',
443               url:"/restconf/config/mef-services:mef-services/mef-service/" + svcid + "/ipvc/unis/uni/"+uni_id,
444               data: data
445           }).then(function successCallback(response) {
446               if (callback != undefined) {
447                   callback();
448               }
449           }, function failureCallback(response) {
450               if (callback != undefined) {
451                   callback();
452               }
453           });
454       };
455       
456       svc.deleteIpvcUni = function(svcid, uni_id, callback) {
457         $http({
458            method:'DELETE',
459            url:"/restconf/config/mef-services:mef-services/mef-service/" + svcid + "/ipvc/unis/uni/" + uni_id + "/"
460        }).then(function successCallback(response) {
461            if (callback != undefined) {
462                callback();
463            }
464        });
465    };
466       
467       
468       
469         svc.addEvcUni = function(svcid, uni_id, role, vlans, callback) {
470             var data = {"uni":{
471                             "uni-id":uni_id,
472                             "role":role,
473                             "admin-state-enabled":true
474                             }
475                         };
476             if (vlans != undefined) {
477                 data.uni['evc-uni-ce-vlans'] = {"evc-uni-ce-vlan":[]}
478                 for (var i=0; i< vlans.length; ++i) {
479                     data.uni['evc-uni-ce-vlans']["evc-uni-ce-vlan"].push({"vid":vlans[i]});
480                 }
481             }
482              $http({
483                 method:'POST',
484                 url:"/restconf/config/mef-services:mef-services/mef-service/" + svcid + "/evc/unis",
485                 data: data
486             }).then(function successCallback(response) {
487                 if (callback != undefined) {
488                     callback();
489                 }
490             }, function failureCallback(response) {
491                 if (callback != undefined) {
492                     callback();
493                 }
494             });
495         };
496
497         svc.addEvcUniVlan = function(svcid, uni_id, vlan, callback) {
498              $http({
499                 method:'POST',
500                 url:"/restconf/config/mef-services:mef-services/mef-service/" + svcid + "/evc/unis/uni/" + uni_id + "/evc-uni-ce-vlans/",
501                 data:{"evc-uni-ce-vlan":{"vid":vlan}}
502             }).then(function successCallback(response) {
503                 if (callback != undefined) {
504                     callback();
505                 }
506             });
507         };
508
509         svc.deleteEvcUni = function(svcid, uni_id, callback) {
510              $http({
511                 method:'DELETE',
512                 url:"/restconf/config/mef-services:mef-services/mef-service/" + svcid + "/evc/unis/uni/" + uni_id + "/"
513             }).then(function successCallback(response) {
514                 if (callback != undefined) {
515                     callback();
516                 }
517             });
518         };
519
520
521
522         svc.deleteVlan = function(svc_id, uni_id, vlan,callback) {
523             $http({
524                 method:'DELETE',
525                 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+"/"
526             }).then(function successCallback(response) {
527                 if (callback != undefined) {
528                     callback();
529                 }
530             });
531         };
532
533         svc.addVlan = function(svc_id, uni_id, vlan, callback) {
534             $http({
535                 method:'PUT',
536                 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+"/",
537                 data:{"evc-uni-ce-vlan":{"vid":vlan}}
538             }).then(function successCallback(response) {
539                 if (callback != undefined) {
540                     callback();
541                 }
542             });
543         };
544
545         svc.getNetworkNames = function(callback){
546           $http({
547             method:'GET',
548             url:"/restconf/config/neutron:neutron/networks/"
549         }).then(function successCallback(response) {
550             if (callback != undefined) {
551                 callback(response.data.networks.network);
552             }
553         });
554         };
555
556         return svc;
557
558     });
559 });