summaryrefslogtreecommitdiffstats
path: root/ecomp-portal-widget-ms/widget-ms/src/main/java/org/onap/portalapp/widget/service/impl/InitializationServiceImpl.java
blob: cbc0c9d02323c6b17048ba357ec85eb2de36ef8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package org.onap.portalapp.widget.service.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashSet;

import javax.transaction.Transactional;

import org.onap.portalapp.widget.domain.MicroserviceData;
import org.onap.portalapp.widget.domain.MicroserviceParameter;
import org.onap.portalapp.widget.domain.RoleApp;
import org.onap.portalapp.widget.domain.WidgetCatalog;
import org.onap.portalapp.widget.service.InitializationService;
import org.onap.portalapp.widget.service.MicroserviceService;
import org.onap.portalapp.widget.service.StorageService;
import org.onap.portalapp.widget.service.WidgetCatalogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Service;

/**
 * Uploads widget zip archives to Portal.
 */
@Service("initService")
@Transactional
@org.springframework.context.annotation.Configuration
@EnableAspectJAutoProxy
public class InitializationServiceImpl implements InitializationService {

	private static final String BASIC_AUTH = "Basic Authentication";
	private static final String PARAMETER_KEY = "resourceType";
	private static final Logger logger = LoggerFactory.getLogger(InitializationServiceImpl.class);

	@Autowired
	WidgetCatalogService widgetCatalogService;

	@Autowired
	StorageService storageService;

	@Autowired
	MicroserviceService microserviceService;

	@Value("${account.user.name}")
	String account_user;

	@Value("${account.user.password}")
	String account_password;

	@Value("${initialization.widgetData.url}")
	String widgetData_url;

	@Override
	public void initialize() {
		initCommonWidget("News");
		initCommonWidget("Events");
		initCommonWidget("Resources");
		initCommonWidget("Portal-Common-Scheduler");
	}

	private void initCommonWidget(String name) {

		final String newServiceName = name + " Microservice";

		Long serviceId = microserviceService.getMicroserviceIdByName(newServiceName);

		if (serviceId == null) {
			MicroserviceData newService = new MicroserviceData();
			newService.setName(newServiceName);
			newService.setDesc(name);
			newService.setAppId(1);
			newService.setUrl(widgetData_url);
			newService.setSecurityType(BASIC_AUTH);
			newService.setUsername(account_user);
			newService.setPassword(account_password);
			newService.setActive("Y");
			serviceId = microserviceService.saveMicroserivce(newService);

			MicroserviceParameter parameter = new MicroserviceParameter();
			parameter.setServiceId(serviceId);
			parameter.setPara_key(PARAMETER_KEY);
			String parameter_value = null;
			switch (name.toLowerCase()) {
			case "Portal-Common-Scheduler":
				parameter_value = "NEWS";
				break;
			case "news":
				parameter_value = "NEWS";
				break;
			case "events":
				parameter_value = "EVENTS";
				break;
			case "resources":
				parameter_value = "IMPORTANTRESOURCES";
				break;
			}
			parameter.setPara_value(parameter_value);
			microserviceService.saveMicroserviceParameter(parameter);
		}

		if (!widgetCatalogService.getWidgetIdByName(name)) {
			WidgetCatalog newWidget = new WidgetCatalog();
			newWidget.setName(name);
			newWidget.setDesc(name);
			newWidget.setAllowAllUser("1");
			String fileLocation = name.toLowerCase() + "-widget.zip";
			newWidget.setFileLocation(fileLocation);
			newWidget.setServiceId(serviceId);
			newWidget.setWidgetRoles(new HashSet<RoleApp>());
			long widgetId = widgetCatalogService.saveWidgetCatalog(newWidget);

			File tmpZipFile = new File("/tmp/" + fileLocation);
			InputStream fileInputStream = null;
			OutputStream outputStream = null;
			try {
				fileInputStream = this.getClass().getClassLoader().getResourceAsStream(fileLocation);
				outputStream = new FileOutputStream(tmpZipFile);
				int read = 0;
				byte[] bytes = new byte[4096];
				while ((read = fileInputStream.read(bytes)) != -1) {
					outputStream.write(bytes, 0, read);
				}
				outputStream.close();
				fileInputStream.close();
			} catch (Exception e) {
				logger.error(
						"Exception occurred while performing InitializationServiceImpl.initCommonWidget in widget microservices. Details:", e);
			}
			storageService.initSave(tmpZipFile, newWidget, widgetId);
			tmpZipFile.delete();
		}
	}
}