2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 * https://creativecommons.org/licenses/by/4.0/
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.
34 * ============LICENSE_END============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
38 package org.openecomp.portalapp.widget.service.impl;
41 import java.io.FileOutputStream;
42 import java.io.InputStream;
43 import java.io.OutputStream;
44 import java.util.HashSet;
46 import javax.transaction.Transactional;
48 import org.openecomp.portalapp.widget.domain.MicroserviceData;
49 import org.openecomp.portalapp.widget.domain.MicroserviceParameter;
50 import org.openecomp.portalapp.widget.domain.RoleApp;
51 import org.openecomp.portalapp.widget.domain.WidgetCatalog;
52 import org.openecomp.portalapp.widget.service.InitializationService;
53 import org.openecomp.portalapp.widget.service.MicroserviceService;
54 import org.openecomp.portalapp.widget.service.StorageService;
55 import org.openecomp.portalapp.widget.service.WidgetCatalogService;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
58 import org.springframework.beans.factory.annotation.Autowired;
59 import org.springframework.beans.factory.annotation.Value;
60 import org.springframework.context.annotation.EnableAspectJAutoProxy;
61 import org.springframework.stereotype.Service;
64 * Uploads widget zip archives to Portal.
66 @Service("initService")
68 @org.springframework.context.annotation.Configuration
69 @EnableAspectJAutoProxy
70 public class InitializationServiceImpl implements InitializationService {
72 private static final String BASIC_AUTH = "Basic Authentication";
73 private static final String PARAMETER_KEY = "resourceType";
74 private static final Logger logger = LoggerFactory.getLogger(InitializationServiceImpl.class);
77 WidgetCatalogService widgetCatalogService;
80 StorageService storageService;
83 MicroserviceService microserviceService;
85 @Value("${account.user.name}")
88 @Value("${account.user.password}")
89 String account_password;
91 @Value("${initialization.widgetData.url}")
92 String widgetData_url;
95 public void initialize() {
96 initCommonWidget("News");
97 initCommonWidget("Events");
98 initCommonWidget("Resources");
101 private void initCommonWidget(String name) {
103 final String newServiceName = name + " Microservice";
105 Long serviceId = microserviceService.getMicroserviceIdByName(newServiceName);
107 if (serviceId == null) {
108 MicroserviceData newService = new MicroserviceData();
109 newService.setName(newServiceName);
110 newService.setDesc(name);
111 newService.setAppId(1);
112 newService.setUrl(widgetData_url);
113 newService.setSecurityType(BASIC_AUTH);
114 newService.setUsername(account_user);
115 newService.setPassword(account_password);
116 newService.setActive("Y");
117 serviceId = microserviceService.saveMicroserivce(newService);
119 MicroserviceParameter parameter = new MicroserviceParameter();
120 parameter.setServiceId(serviceId);
121 parameter.setPara_key(PARAMETER_KEY);
122 String parameter_value = null;
123 switch (name.toLowerCase()) {
125 parameter_value = "NEWS";
128 parameter_value = "EVENTS";
131 parameter_value = "IMPORTANTRESOURCES";
134 parameter.setPara_value(parameter_value);
135 microserviceService.saveMicroserviceParameter(parameter);
138 if (!widgetCatalogService.getWidgetIdByName(name)) {
139 WidgetCatalog newWidget = new WidgetCatalog();
140 newWidget.setName(name);
141 newWidget.setDesc(name);
142 newWidget.setAllowAllUser("1");
143 String fileLocation = name.toLowerCase() + "-widget.zip";
144 newWidget.setFileLocation(fileLocation);
145 newWidget.setServiceId(serviceId);
146 newWidget.setWidgetRoles(new HashSet<RoleApp>());
147 long widgetId = widgetCatalogService.saveWidgetCatalog(newWidget);
149 File tmpZipFile = new File("/tmp/" + fileLocation);
150 InputStream fileInputStream = null;
151 OutputStream outputStream = null;
153 fileInputStream = this.getClass().getClassLoader().getResourceAsStream(fileLocation);
154 outputStream = new FileOutputStream(tmpZipFile);
156 byte[] bytes = new byte[4096];
157 while ((read = fileInputStream.read(bytes)) != -1) {
158 outputStream.write(bytes, 0, read);
160 outputStream.close();
161 fileInputStream.close();
162 } catch (Exception e) {
164 "Exception occurred while performing InitializationServiceImpl.initCommonWidget in widget microservices. Details:", e);
166 storageService.initSave(tmpZipFile, newWidget, widgetId);