393e7ccbbfda183eb8ce0811bb32e187e2fa52dc
[portal.git] / ecomp-portal-FE-common / client / app / services / microservice / microservice.service.js
1 /*-
2  * ================================================================================
3  * ECOMP Portal
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ================================================================================
19  */
20 'use strict';
21
22 (function () {
23     class MicroserviceService {
24         constructor($q, $log, $http, conf,uuid, utilsService) {
25             this.$q = $q;
26             this.$log = $log;
27             this.$http = $http;
28             this.conf = conf;
29             this.uuid = uuid;
30             this.utilsService = utilsService;   
31         }
32         
33         createService(newService) {
34                 let deferred = this.$q.defer();
35                 this.$http({
36                 method: "POST",
37                 url: this.conf.api.widgetCommon,
38                 data: newService,
39                 cache: false,
40                 headers:{
41                         'X-ECOMP-RequestID':this.uuid.generate()
42                 }
43             }).then(res => {
44                 if (res == null || res.data == null) {
45                                 this.$log.error('MicroserviceService::createService Failed: Result or result.data is null');
46                      deferred.reject("MicroserviceService::createService Failed: Result or result.data is null");
47                 } else if (!this.utilsService.isValidJSON(res.data)) {
48                         this.$log.error('MicroserviceService::createService Failed: Invalid JSON format');
49                     deferred.reject("MicroserviceService::createService Failed: Invalid JSON format");
50                 } else {
51                     deferred.resolve(res.data);
52                 }
53             })
54             .catch(errRes => {
55                 deferred.reject(errRes);
56             });
57             return deferred.promise;
58         }
59         
60         
61         updateService(serviceId, newService) {
62                 let deferred = this.$q.defer();
63                 this.$http({
64                 method: "PUT",
65                 url: this.conf.api.widgetCommon + "/" + serviceId,
66                 data: newService,
67                 cache: false,
68                 headers:{
69                         'X-ECOMP-RequestID':this.uuid.generate()
70                 }
71             }).then(res => {
72                 if (res == null || res.data == null) {
73                                 this.$log.error('MicroserviceService::updateService Failed: Result or result.data is null');
74                      deferred.reject("MicroserviceService::updateService Failed: Result or result.data is null");
75                 } else if (!this.utilsService.isValidJSON(res.data)) {
76                         this.$log.error('MicroserviceService::updateService Failed: Invalid JSON format');
77                     deferred.reject("MicroserviceService::updateService Failed: Invalid JSON format");
78                 } else {
79                     deferred.resolve(res.data);
80                 }
81             })
82             .catch(errRes => {
83                 deferred.reject(errRes);
84             });
85             return deferred.promise;
86         }
87         
88         deleteService(serviceId) {
89                 let deferred = this.$q.defer();
90                 this.$http({
91                 method: "DELETE",
92                 url: this.conf.api.widgetCommon + "/" + serviceId,
93                 cache: false,
94                 headers:{
95                         'X-ECOMP-RequestID':this.uuid.generate()
96                 }
97             }).then(res => {
98                 if (res == null || res.data == null) {
99                                 this.$log.error('MicroserviceService::deleteService Failed: Result or result.data is null');
100                      deferred.reject("MicroserviceService::deleteService Failed: Result or result.data is null");
101                 } else if (!this.utilsService.isValidJSON(res.data)) {
102                         this.$log.error('MicroserviceService::deleteService Failed: Invalid JSON format');
103                     deferred.reject("MicroserviceService::deleteService Failed: Invalid JSON format");
104                 } else {
105                     deferred.resolve(res.data);
106                 }
107             })
108             .catch(errRes => {
109                 deferred.reject(errRes);
110             });
111             return deferred.promise;
112         }
113         
114         getServiceList() {
115                 let deferred = this.$q.defer();
116                 this.$http({
117                 method: "GET",
118                 url: this.conf.api.widgetCommon,
119                 cache: false,
120                 headers:{
121                         'X-ECOMP-RequestID':this.uuid.generate()
122                 }
123             }).then(res => {
124                 if (res == null || res.data == null) {
125                                 this.$log.error('MicroserviceService::getServiceList Failed: Result or result.data is null');
126                      deferred.reject("MicroserviceService::getServiceList Failed: Result or result.data is null");
127                 } else if (!this.utilsService.isValidJSON(res.data)) {
128                         this.$log.error('MicroserviceService::getServiceList Failed: Invalid JSON format');
129                     deferred.reject("MicroserviceService::getServiceList Failed: Invalid JSON format");
130                 }  else {
131                     deferred.resolve(res.data);
132                 }
133             })
134             .catch(errRes => {
135                 deferred.reject(errRes);
136             });
137             return deferred.promise;
138         }
139         
140         getWidgetListByService(serviceId) {
141                 let deferred = this.$q.defer();
142                 this.$http({
143                 method: "GET",
144                 url: this.conf.api.widgetCommon + '/' + serviceId,
145                 cache: false,
146                 headers:{
147                         'X-ECOMP-RequestID':this.uuid.generate()
148                 }
149             }).then(res => {
150                 if (res == null || res.data == null) {
151                                 this.$log.error('MicroserviceService::getWidgetListByService Failed: Result or result.data is null');
152                      deferred.reject("MicroserviceService::getWidgetListByService Failed: Result or result.data is null");
153                 } else if (!this.utilsService.isValidJSON(res.data)) {
154                         this.$log.error('MicroserviceService::getWidgetListByService Failed: Invalid JSON format');
155                     deferred.reject("MicroserviceService::getWidgetListByService Failed: Invalid JSON format");
156                 } else {
157                     deferred.resolve(res.data);
158                 }
159             })
160             .catch(errRes => {
161                 deferred.reject(errRes);
162             });
163             return deferred.promise;
164         }
165         
166         getUserParameterById(paramId){
167                 let deferred = this.$q.defer();
168                 this.$http({
169                 method: "GET",
170                 url: this.conf.api.widgetCommon + '/services/' +  paramId,
171                 cache: false,
172                 headers:{
173                         'X-ECOMP-RequestID':this.uuid.generate()
174                 }
175             }).then(res => {
176                 if (res == null || res.data == null) {
177                                 this.$log.error('MicroserviceService::getUserParameterById Failed: Result or result.data is null');
178                      deferred.reject("MicroserviceService::getUserParameterById Failed: Result or result.data is null");
179                 } else if (!this.utilsService.isValidJSON(res.data)) {
180                         this.$log.error('MicroserviceService::getUserParameterById Failed: Invalid JSON format');
181                     deferred.reject("MicroserviceService::getUserParameterById Failed: Invalid JSON format");
182                 } else {
183                     deferred.resolve(res.data);
184                 }
185             })
186             .catch(errRes => {
187                 deferred.reject(errRes);
188             });
189             return deferred.promise;
190         }
191         
192         deleteUserParameterById(paramId){
193                 let deferred = this.$q.defer();
194                 this.$http({
195                 method: "DELETE",
196                 url: this.conf.api.widgetCommon + '/services/' +  paramId,
197                 cache: false,
198                 headers:{
199                         'X-ECOMP-RequestID':this.uuid.generate()
200                 }
201             }).then(res => {
202                 if (res == null || res.data == null) {
203                                 this.$log.error('MicroserviceService::deleteUserParameterById Failed: Result or result.data is null');
204                      deferred.reject("MicroserviceService::deleteUserParameterById Failed: Result or result.data is null");
205                 } else {
206                     deferred.resolve(res.data);
207                 }
208             })
209             .catch(errRes => {
210                 deferred.reject(errRes);
211             });
212             return deferred.promise;
213         }
214     }
215     
216     MicroserviceService.$inject = ['$q', '$log', '$http', 'conf','uuid4', 'utilsService'];
217     angular.module('ecompApp').service('microserviceService', MicroserviceService)
218 })();