1 package org.openecomp.portalapp.widget.service.impl;
4 import java.io.FileOutputStream;
5 import java.io.InputStream;
6 import java.io.OutputStream;
7 import java.util.HashSet;
9 import javax.transaction.Transactional;
11 import org.openecomp.portalapp.widget.domain.MicroserviceData;
12 import org.openecomp.portalapp.widget.domain.MicroserviceParameter;
13 import org.openecomp.portalapp.widget.domain.RoleApp;
14 import org.openecomp.portalapp.widget.domain.WidgetCatalog;
15 import org.openecomp.portalapp.widget.service.InitializationService;
16 import org.openecomp.portalapp.widget.service.MicroserviceService;
17 import org.openecomp.portalapp.widget.service.StorageService;
18 import org.openecomp.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;
27 * Uploads widget zip archives to Portal.
29 @Service("initService")
31 @org.springframework.context.annotation.Configuration
32 @EnableAspectJAutoProxy
33 public class InitializationServiceImpl implements InitializationService {
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);
40 WidgetCatalogService widgetCatalogService;
43 StorageService storageService;
46 MicroserviceService microserviceService;
48 @Value("${account.user.name}")
51 @Value("${account.user.password}")
52 String account_password;
54 @Value("${initialization.widgetData.url}")
55 String widgetData_url;
58 public void initialize() {
59 initCommonWidget("News");
60 initCommonWidget("Events");
61 initCommonWidget("Resources");
64 private void initCommonWidget(String name) {
66 final String newServiceName = name + " Microservice";
68 Long serviceId = microserviceService.getMicroserviceIdByName(newServiceName);
70 if (serviceId == null) {
71 MicroserviceData newService = new MicroserviceData();
72 newService.setName(newServiceName);
73 newService.setDesc(name);
74 newService.setAppId(1);
75 newService.setUrl(widgetData_url);
76 newService.setSecurityType(BASIC_AUTH);
77 newService.setUsername(account_user);
78 newService.setPassword(account_password);
79 newService.setActive("Y");
80 serviceId = microserviceService.saveMicroserivce(newService);
82 MicroserviceParameter parameter = new MicroserviceParameter();
83 parameter.setServiceId(serviceId);
84 parameter.setPara_key(PARAMETER_KEY);
85 String parameter_value = null;
86 switch (name.toLowerCase()) {
88 parameter_value = "NEWS";
91 parameter_value = "EVENTS";
94 parameter_value = "IMPORTANTRESOURCES";
97 parameter.setPara_value(parameter_value);
98 microserviceService.saveMicroserviceParameter(parameter);
101 if (!widgetCatalogService.getWidgetIdByName(name)) {
102 WidgetCatalog newWidget = new WidgetCatalog();
103 newWidget.setName(name);
104 newWidget.setDesc(name);
105 newWidget.setAllowAllUser("1");
106 String fileLocation = name.toLowerCase() + "-widget.zip";
107 newWidget.setFileLocation(fileLocation);
108 newWidget.setServiceId(serviceId);
109 newWidget.setWidgetRoles(new HashSet<RoleApp>());
110 long widgetId = widgetCatalogService.saveWidgetCatalog(newWidget);
112 File tmpZipFile = new File("/tmp/" + fileLocation);
113 InputStream fileInputStream = null;
114 OutputStream outputStream = null;
116 fileInputStream = this.getClass().getClassLoader().getResourceAsStream(fileLocation);
117 outputStream = new FileOutputStream(tmpZipFile);
119 byte[] bytes = new byte[4096];
120 while ((read = fileInputStream.read(bytes)) != -1) {
121 outputStream.write(bytes, 0, read);
123 outputStream.close();
124 fileInputStream.close();
125 } catch (Exception e) {
127 "Exception occurred while performing InitializationServiceImpl.initCommonWidget in widget microservices. Details:", e);
129 storageService.initSave(tmpZipFile, newWidget, widgetId);