Bulk upload changes and music health check apis
[portal.git] / ecomp-portal-FE-common / client / app / services / widgets-catalog / widgets-catalog.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  * 
37  */
38 'use strict';
39
40 (function () {
41         class WidgetsCatalogService {
42         constructor($q, $log, $http, conf,uuid,base64Service, beReaderService, utilsService) {
43             this.$q = $q;
44             this.$log = $log;
45             this.$http = $http;
46             this.conf = conf;
47             this.uuid = uuid;
48             this.base64Service = base64Service;
49             this.beReaderService = beReaderService;
50             this.utilsService = utilsService;
51         }
52         
53         getUserWidgets(loginName) {
54                  let deferred = this.$q.defer();
55                  this.$http({
56                          method: "GET",
57                      url: this.conf.api.widgetCommon + '/widgetCatalog' + '/' + loginName,
58                      cache: false,
59                      headers: {
60                          'X-Widgets-Type': 'all',
61                          'X-ECOMP-RequestID':this.uuid.generate()
62                      }
63              }).then(res => {
64                         if (res == null || res.data == null) {
65                      deferred.reject("WidgetsCatalogService::getUserWidgets Failed");
66                  } else {
67                      deferred.resolve(res.data);
68                  }
69              })
70              .catch(status => {
71                  deferred.reject(status);
72                  });
73              return deferred.promise;
74         }
75
76         getManagedWidgets() {           
77             let deferred = this.$q.defer();
78             let url = this.conf.api.widgetCommon + '/widgetCatalog';
79             this.$http({
80                 method: "GET",
81                 url: url,
82                 cache: false,
83                 headers: {
84                         'X-Widgets-Type': 'all',
85                         'X-ECOMP-RequestID':this.uuid.generate()
86                 }
87             }).then(res => {
88                         if (res == null || res.data == null) {
89                      deferred.reject("WidgetsCatalogService::getManagedWidgets Failed");
90                  } else {
91                      deferred.resolve(res.data);
92                  }
93              })
94              .catch(status => {
95                  deferred.reject(status);
96              });  
97             return deferred.promise;
98         }
99         getUploadFlag() {
100             let deferred = this.$q.defer();
101             let url = this.conf.api.widgetCommon + '/uploadFlag';
102             this.$http({
103                 method: "GET",
104                 url: url,
105                 cache: false,
106                 headers: {
107                         'X-Widgets-Type': 'all',
108                         'X-ECOMP-RequestID':this.uuid.generate()
109                 }
110             }).then(res => {
111                         if (res == null || res.data == null) {
112                      deferred.reject("WidgetsCatalogService::getUploadFlag Failed");
113                  } else {
114                      deferred.resolve(res.data);
115                  }
116              })
117              .catch(status => {
118                  deferred.reject(status);
119              });  
120             return deferred.promise;
121         }
122         
123         createWidget(newWidget, file) {
124                 console.log(newWidget);
125                 let deferred = this.$q.defer();
126                 var formData = new FormData();
127                 formData.append('file', file);
128                 this.$http({
129                 method: "POST",
130                 url: this.conf.api.widgetCommon + '/widgetCatalog',
131                 transformRequest: angular.identity,  
132                 data: formData,
133                 headers:{  
134                         'Content-Type': undefined,
135                         'X-Widgets-Type': 'all',
136                         'X-ECOMP-RequestID':this.uuid.generate()
137                 },
138                 params:{
139                         'newWidget': newWidget
140                 }
141             }).then(res => {
142                 if (res == null || res.data == null) {
143                     deferred.reject("WidgetsCatalogService::getManagedWidgets Failed");
144                 } else {
145                     deferred.resolve(res.data);
146                 }
147             })
148             .catch(errRes => {
149                 deferred.reject(errRes);
150             });
151             return deferred.promise;
152         }
153         
154         updateWidgetWithFile(file, widgetId, newWidget){
155                 console.log(widgetId);
156                 let deferred = this.$q.defer();
157             var formData = new FormData();
158                 formData.append('file', file);
159                 let url = this.conf.api.widgetCommon + '/widgetCatalog/' + widgetId;
160                 this.$http({
161                 method: 'POST',
162                 url: url, 
163                 transformRequest: angular.identity,  
164                 data: formData,
165                 headers: {        
166                         'Content-Type': undefined,
167                         'X-Widgets-Type': 'all',
168                         'X-ECOMP-RequestID':this.uuid.generate()
169                 },
170                         params:{
171                                 'newWidget': newWidget
172                         }
173             })
174             .then(res => {
175                if (res == null || res.data == null)
176                    deferred.reject("WidgetsCatalogService::saveWidgetFile Failed");
177                else 
178                    deferred.resolve(res.data);
179             })
180             .catch(status => {
181                  deferred.reject(status);
182              });  
183                 return deferred.promise;
184         }
185         
186         updateWidget(widgetId, widgetData) {
187             let deferred = this.$q.defer();
188             let url = this.conf.api.widgetCommon  + '/widgetCatalog' + '/' + widgetId;
189             this.$http({
190                 method: 'PUT',
191                 url: url,
192                 cache: false,
193                 data: widgetData,
194                 headers: {
195                         'X-Widgets-Type': 'all',
196                         'X-ECOMP-RequestID':this.uuid.generate()
197                 }
198             })
199             .then(res => {
200                 deferred.resolve(res.data);
201             })
202             .catch(errRes => {
203                 deferred.reject(errRes);
204             });
205            
206             return deferred.promise;
207         }
208         
209         
210         deleteWidgetFile(widgetName) {
211                 let deferred = this.$q.defer();
212             this.$log.info('WidgetsCatalogService::deleteWidgetCatalog');
213             let url = this.conf.api.widgetCommon + '/doUpload' + '/' + widgetName;
214             this.$http({
215                 method: "DELETE",
216                 url: url,
217                 cache: false,
218                 headers:{  
219                         'X-Widgets-Type': 'all',
220                         'X-ECOMP-RequestID':this.uuid.generate()
221                 }
222             }).then(res => {
223                     deferred.resolve(res.data); 
224                 })
225                 .catch(status => {
226                         deferred.reject(status);
227                 });
228            
229             return deferred.promise;
230         }
231         
232         deleteWidget(widgetId) {
233                 let deferred = this.$q.defer();
234             this.$log.info('WidgetsCatalogService::deleteWidgetCatalog');
235             let url = this.conf.api.widgetCommon + '/widgetCatalog'  + '/' + widgetId;
236             this.$http({
237                 method: "DELETE",
238                 url: url,
239                 cache: false,
240                 headers:{  
241                         'X-Widgets-Type': 'all',
242                         'X-ECOMP-RequestID':this.uuid.generate()
243                 }
244             }).then(res => {
245                     deferred.resolve(res.data); 
246                 })
247                 .catch(status => {
248                         deferred.reject(status);
249                 });
250            
251             return deferred.promise;
252         }
253         
254         downloadWidgetFile(widgetId) {
255                 let deferred = this.$q.defer();
256             this.$log.info('WidgetsCatalogService::downloadWidgetFile');
257             let url = this.conf.api.widgetCommon + '/download/' + widgetId;
258             console.log(url);
259             this.$http({
260                 method: "GET",
261                 url: url,
262                 cache: false,
263                 responseType: "arraybuffer",
264                 headers:{  
265                         'X-Widgets-Type': 'all',
266                         'X-ECOMP-RequestID':this.uuid.generate()
267                 }
268             }).then(res => {
269                     deferred.resolve(res.data); 
270                 })
271                 .catch(status => {
272                         deferred.reject(status);
273                 });
274            
275             return deferred.promise;
276         }
277         
278         updateWidgetCatalog(appData){
279                 let deferred = this.$q.defer();
280             // Validate the request, maybe this is overkill
281             if (appData == null || appData.widgetId == null || appData.select == null) {
282                 var msg = 'WidgetCatalogService::updateAppCatalog: field appId and/or select not found';
283                 this.$log.error(msg);
284                 return deferred.reject(msg);
285             }
286             this.$http({
287                 method: "PUT",
288                 url: this.conf.api.widgetCatalogSelection,
289                 data: appData,
290                 headers: {
291                          'X-Widgets-Type': 'all',
292                          'X-ECOMP-RequestID':this.uuid.generate()
293                 }
294             }).then( res => {
295                     // Detect non-JSON
296                     if (res == null || res.data == null) {
297                         deferred.reject("WidgetCatalogService::updateAppCatalog Failed");
298                     } else {
299                         deferred.resolve(res.data);
300                     }
301                 })
302                 .catch( status => {
303                         this.$log.error('WidgetCatalogService:updateAppCatalog failed: ' + status);
304                     deferred.reject(status);
305                 });
306             return deferred.promise;
307         }
308         
309         getServiceJSON(serviceId){
310                 let deferred = this.$q.defer();
311             this.$log.info('WidgetsCatalogService::getServiceJSON');
312             let url = this.conf.api.microserviceProxy + "/" + serviceId;
313             console.log(url);
314             this.$http({
315                 method: "GET",
316                 url: url,
317                 cache: false,
318                 headers:{  
319                         'X-Widgets-Type': 'all',
320                         'X-ECOMP-RequestID':this.uuid.generate()
321                 }
322             }).then(res => {
323                         if (res == null || res == null) {
324                                         this.$log.error('WidgetCatalogService::getServiceJSON Failed: Result or result.data is null');
325                             deferred.reject("WidgetCatalogService::getServiceJSON Failed: Result or result.data is null");
326                         } else{
327                                 deferred.resolve(res.data);
328                         }
329                         
330                 })
331                 .catch(status => {
332                         deferred.reject(status);
333                 });
334            
335             return deferred.promise;
336         }
337         
338         getWidgetCatalogParameters(widgetId){
339                 let deferred = this.$q.defer();
340             this.$log.info('WidgetsCatalogService::getWidgetCatalogParameters');
341             let url = this.conf.api.widgetCommon + "/parameters/" + widgetId;
342             console.log(url);
343             this.$http({
344                 method: "GET",
345                 url: url,
346                 cache: false,
347                 headers:{  
348                         'X-Widgets-Type': 'all',
349                         'X-ECOMP-RequestID':this.uuid.generate()
350                 }
351             }).then(res => {
352                         if (res == null || res.data == null) {
353                                         this.$log.error('WidgetCatalogService::getWidgetCatalogParameters Failed: Result or result.data is null');
354                             deferred.reject("WidgetCatalogService::getWidgetCatalogParameters Failed: Result or result.data is null");
355                         } else {
356                             deferred.resolve(res.data);
357                         }
358                 })
359                 .catch(status => {
360                         deferred.reject(status);
361                 });
362            
363             return deferred.promise;
364         }
365         
366         
367         saveWidgetParameter(widgetParamObject){
368                 let deferred = this.$q.defer();
369             this.$log.info('WidgetsCatalogService::saveWidgetParameter');
370             let url = this.conf.api.widgetCommon + "/parameters";
371             this.$http({
372                 method: "POST",
373                 url: url,
374                 cache: false,
375                 data: widgetParamObject,
376                 headers:{  
377                         'X-Widgets-Type': 'all',
378                         'X-ECOMP-RequestID':this.uuid.generate()
379                 }
380             }).then(res => {
381                         if (res == null || res.data == null) {
382                                         this.$log.error('WidgetCatalogService::getWidgetCatalogParameters Failed: Result or result.data is null');
383                             deferred.reject("WidgetCatalogService::getWidgetCatalogParameters Failed: Result or result.data is null");
384                         } else {
385                             deferred.resolve(res.data);
386                         }
387                 })
388                 .catch(status => {
389                         deferred.reject(status);
390                 });
391            
392             return deferred.promise;
393         }
394         
395     }
396     
397     WidgetsCatalogService.$inject = ['$q', '$log', '$http', 'conf','uuid4','base64Service', 'beReaderService', 'utilsService'];
398     angular.module('ecompApp').service('widgetsCatalogService', WidgetsCatalogService)
399 })();