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