1 package org.onap.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.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;
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");
62 initCommonWidget("Portal-Common-Scheduler");
65 private void initCommonWidget(String name) {
67 final String newServiceName = name + " Microservice";
69 Long serviceId = microserviceService.getMicroserviceIdByName(newServiceName);
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);
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";
92 parameter_value = "NEWS";
95 parameter_value = "EVENTS";
98 parameter_value = "IMPORTANTRESOURCES";
101 parameter.setPara_value(parameter_value);
102 microserviceService.saveMicroserviceParameter(parameter);
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);
116 File tmpZipFile = new File("/tmp/" + fileLocation);
117 InputStream fileInputStream = null;
118 OutputStream outputStream = null;
120 fileInputStream = this.getClass().getClassLoader().getResourceAsStream(fileLocation);
121 outputStream = new FileOutputStream(tmpZipFile);
123 byte[] bytes = new byte[4096];
124 while ((read = fileInputStream.read(bytes)) != -1) {
125 outputStream.write(bytes, 0, read);
127 outputStream.close();
128 fileInputStream.close();
129 } catch (Exception e) {
131 "Exception occurred while performing InitializationServiceImpl.initCommonWidget in widget microservices. Details:", e);
133 storageService.initSave(tmpZipFile, newWidget, widgetId);