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