Update license; improve coverage; add docs dir
[portal.git] / ecomp-portal-widget-ms / widget-ms / src / main / java / org / openecomp / portalapp / widget / service / impl / InitializationServiceImpl.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
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
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
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
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
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.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.openecomp.portalapp.widget.service.impl;
39
40 import java.io.File;
41 import java.io.FileOutputStream;
42 import java.io.InputStream;
43 import java.io.OutputStream;
44 import java.util.HashSet;
45
46 import javax.transaction.Transactional;
47
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;
62
63 /**
64  * Uploads widget zip archives to Portal.
65  */
66 @Service("initService")
67 @Transactional
68 @org.springframework.context.annotation.Configuration
69 @EnableAspectJAutoProxy
70 public class InitializationServiceImpl implements InitializationService {
71
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);
75
76         @Autowired
77         WidgetCatalogService widgetCatalogService;
78
79         @Autowired
80         StorageService storageService;
81
82         @Autowired
83         MicroserviceService microserviceService;
84
85         @Value("${account.user.name}")
86         String account_user;
87
88         @Value("${account.user.password}")
89         String account_password;
90
91         @Value("${initialization.widgetData.url}")
92         String widgetData_url;
93
94         @Override
95         public void initialize() {
96                 initCommonWidget("News");
97                 initCommonWidget("Events");
98                 initCommonWidget("Resources");
99         }
100
101         private void initCommonWidget(String name) {
102
103                 final String newServiceName = name + " Microservice";
104
105                 Long serviceId = microserviceService.getMicroserviceIdByName(newServiceName);
106
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);
118
119                         MicroserviceParameter parameter = new MicroserviceParameter();
120                         parameter.setServiceId(serviceId);
121                         parameter.setPara_key(PARAMETER_KEY);
122                         String parameter_value = null;
123                         switch (name.toLowerCase()) {
124                         case "news":
125                                 parameter_value = "NEWS";
126                                 break;
127                         case "events":
128                                 parameter_value = "EVENTS";
129                                 break;
130                         case "resources":
131                                 parameter_value = "IMPORTANTRESOURCES";
132                                 break;
133                         }
134                         parameter.setPara_value(parameter_value);
135                         microserviceService.saveMicroserviceParameter(parameter);
136                 }
137
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);
148
149                         File tmpZipFile = new File("/tmp/" + fileLocation);
150                         InputStream fileInputStream = null;
151                         OutputStream outputStream = null;
152                         try {
153                                 fileInputStream = this.getClass().getClassLoader().getResourceAsStream(fileLocation);
154                                 outputStream = new FileOutputStream(tmpZipFile);
155                                 int read = 0;
156                                 byte[] bytes = new byte[4096];
157                                 while ((read = fileInputStream.read(bytes)) != -1) {
158                                         outputStream.write(bytes, 0, read);
159                                 }
160                                 outputStream.close();
161                                 fileInputStream.close();
162                         } catch (Exception e) {
163                                 logger.error(
164                                                 "Exception occurred while performing InitializationServiceImpl.initCommonWidget in widget microservices. Details:", e);
165                         }
166                         storageService.initSave(tmpZipFile, newWidget, widgetId);
167                         tmpZipFile.delete();
168                 }
169         }
170 }