Merge "Replicate demo/boot/portal_vm_init.sh"
[portal.git] / ecomp-portal-widget-ms / widget-ms / src / main / java / org / openecomp / portalapp / widget / service / impl / InitializationServiceImpl.java
1 package org.openecomp.portalapp.widget.service.impl;
2
3 import java.io.File;
4 import java.io.FileOutputStream;
5 import java.io.InputStream;
6 import java.io.OutputStream;
7 import java.util.HashSet;
8
9 import javax.transaction.Transactional;
10
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;
25
26 /**
27  * Uploads widget zip archives to Portal.
28  */
29 @Service("initService")
30 @Transactional
31 @org.springframework.context.annotation.Configuration
32 @EnableAspectJAutoProxy
33 public class InitializationServiceImpl implements InitializationService {
34
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);
38
39         @Autowired
40         WidgetCatalogService widgetCatalogService;
41
42         @Autowired
43         StorageService storageService;
44
45         @Autowired
46         MicroserviceService microserviceService;
47
48         @Value("${account.user.name}")
49         String account_user;
50
51         @Value("${account.user.password}")
52         String account_password;
53
54         @Value("${initialization.widgetData.url}")
55         String widgetData_url;
56
57         @Override
58         public void initialize() {
59                 initCommonWidget("News");
60                 initCommonWidget("Events");
61                 initCommonWidget("Resources");
62         }
63
64         private void initCommonWidget(String name) {
65
66                 final String newServiceName = name + " Microservice";
67
68                 Long serviceId = microserviceService.getMicroserviceIdByName(newServiceName);
69
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);
81
82                         MicroserviceParameter parameter = new MicroserviceParameter();
83                         parameter.setServiceId(serviceId);
84                         parameter.setPara_key(PARAMETER_KEY);
85                         String parameter_value = null;
86                         switch (name.toLowerCase()) {
87                         case "news":
88                                 parameter_value = "NEWS";
89                                 break;
90                         case "events":
91                                 parameter_value = "EVENTS";
92                                 break;
93                         case "resources":
94                                 parameter_value = "IMPORTANTRESOURCES";
95                                 break;
96                         }
97                         parameter.setPara_value(parameter_value);
98                         microserviceService.saveMicroserviceParameter(parameter);
99                 }
100
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);
111
112                         File tmpZipFile = new File("/tmp/" + fileLocation);
113                         InputStream fileInputStream = null;
114                         OutputStream outputStream = null;
115                         try {
116                                 fileInputStream = this.getClass().getClassLoader().getResourceAsStream(fileLocation);
117                                 outputStream = new FileOutputStream(tmpZipFile);
118                                 int read = 0;
119                                 byte[] bytes = new byte[4096];
120                                 while ((read = fileInputStream.read(bytes)) != -1) {
121                                         outputStream.write(bytes, 0, read);
122                                 }
123                                 outputStream.close();
124                                 fileInputStream.close();
125                         } catch (Exception e) {
126                                 logger.error(
127                                                 "Exception occurred while performing InitializationServiceImpl.initCommonWidget in widget microservices. Details:", e);
128                         }
129                         storageService.initSave(tmpZipFile, newWidget, widgetId);
130                         tmpZipFile.delete();
131                 }
132         }
133 }