Fix sonar issues
[portal.git] / ecomp-portal-widget-ms / widget-ms / src / main / java / org / onap / portalapp / widget / service / impl / InitializationServiceImpl.java
1 package org.onap.portalapp.widget.service.impl;
2
3 import java.io.File;
4 import java.io.FileOutputStream;
5 import java.io.InputStream;
6 import java.io.OutputStream;
7 import java.util.HashSet;
8
9 import javax.transaction.Transactional;
10
11 import org.onap.portalapp.widget.domain.MicroserviceData;
12 import org.onap.portalapp.widget.domain.MicroserviceParameter;
13 import org.onap.portalapp.widget.domain.RoleApp;
14 import org.onap.portalapp.widget.domain.WidgetCatalog;
15 import org.onap.portalapp.widget.service.InitializationService;
16 import org.onap.portalapp.widget.service.MicroserviceService;
17 import org.onap.portalapp.widget.service.StorageService;
18 import org.onap.portalapp.widget.service.WidgetCatalogService;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21 import org.springframework.beans.factory.annotation.Autowired;
22 import org.springframework.beans.factory.annotation.Value;
23 import org.springframework.context.annotation.EnableAspectJAutoProxy;
24 import org.springframework.stereotype.Service;
25
26 /**
27  * Uploads widget zip archives to Portal.
28  */
29 @Service("initService")
30 @Transactional
31 @org.springframework.context.annotation.Configuration
32 @EnableAspectJAutoProxy
33 public class InitializationServiceImpl implements InitializationService {
34
35         private static final String BASIC_AUTH = "Basic Authentication";
36         private static final String PARAMETER_KEY = "resourceType";
37         private static final Logger logger = LoggerFactory.getLogger(InitializationServiceImpl.class);
38
39         @Autowired
40         WidgetCatalogService widgetCatalogService;
41
42         @Autowired
43         StorageService storageService;
44
45         @Autowired
46         MicroserviceService microserviceService;
47
48         @Value("${account.user.name}")
49         String account_user;
50
51         @Value("${account.user.password}")
52         String account_password;
53
54         @Value("${initialization.widgetData.url}")
55         String widgetData_url;
56
57         @Override
58         public void initialize() {
59                 initCommonWidget("News");
60                 initCommonWidget("Events");
61                 initCommonWidget("Resources");
62                 initCommonWidget("Portal-Common-Scheduler");
63         }
64
65         private void initCommonWidget(String name) {
66
67                 final String newServiceName = name + " Microservice";
68
69                 Long serviceId = microserviceService.getMicroserviceIdByName(newServiceName);
70
71                 if (serviceId == null) {
72                         MicroserviceData newService = new MicroserviceData();
73                         newService.setName(newServiceName);
74                         newService.setDesc(name);
75                         newService.setAppId(1);
76                         newService.setUrl(widgetData_url);
77                         newService.setSecurityType(BASIC_AUTH);
78                         newService.setUsername(account_user);
79                         newService.setPassword(account_password);
80                         newService.setActive("Y");
81                         serviceId = microserviceService.saveMicroserivce(newService);
82
83                         MicroserviceParameter parameter = new MicroserviceParameter();
84                         parameter.setServiceId(serviceId);
85                         parameter.setPara_key(PARAMETER_KEY);
86                         String parameter_value = null;
87                         switch (name.toLowerCase()) {
88                         case "Portal-Common-Scheduler":
89                                 parameter_value = "NEWS";
90                                 break;
91                         case "news":
92                                 parameter_value = "NEWS";
93                                 break;
94                         case "events":
95                                 parameter_value = "EVENTS";
96                                 break;
97                         case "resources":
98                                 parameter_value = "IMPORTANTRESOURCES";
99                                 break;
100                         }
101                         parameter.setPara_value(parameter_value);
102                         microserviceService.saveMicroserviceParameter(parameter);
103                 }
104
105                 if (!widgetCatalogService.getWidgetIdByName(name)) {
106                         WidgetCatalog newWidget = new WidgetCatalog();
107                         newWidget.setName(name);
108                         newWidget.setDesc(name);
109                         newWidget.setAllowAllUser("1");
110                         String fileLocation = name.toLowerCase() + "-widget.zip";
111                         newWidget.setFileLocation(fileLocation);
112                         newWidget.setServiceId(serviceId);
113                         newWidget.setWidgetRoles(new HashSet<RoleApp>());
114                         long widgetId = widgetCatalogService.saveWidgetCatalog(newWidget);
115
116                         File tmpZipFile = new File("/tmp/" + fileLocation);
117                         try(OutputStream outputStream = new FileOutputStream(tmpZipFile);
118                                 InputStream fileInputStream = this.getClass().getClassLoader().getResourceAsStream(fileLocation)) {
119                                 int read = 0;
120                                 byte[] bytes = new byte[4096];
121                                 while ((read = fileInputStream.read(bytes)) != -1) {
122                                         outputStream.write(bytes, 0, read);
123                                 }
124                         } catch (Exception e) {
125                                 logger.error(
126                                                 "Exception occurred while performing InitializationServiceImpl.initCommonWidget in widget microservices. Details:", e);
127                         }
128                         storageService.initSave(tmpZipFile, newWidget, widgetId);
129                         tmpZipFile.delete();
130                 }
131         }
132 }