Configure "portal app_password" in DB by environment variable
[vid.git] / vid-app-common / src / main / java / org / onap / vid / controller / WebConfig.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2018 - 2019 Nokia. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.vid.controller;
23
24 import static org.apache.commons.lang3.ObjectUtils.defaultIfNull;
25
26 import com.fasterxml.jackson.module.kotlin.KotlinModule;
27 import io.joshworks.restclient.http.mapper.ObjectMapper;
28 import java.io.File;
29 import java.util.concurrent.ExecutorService;
30 import java.util.concurrent.Executors;
31 import javax.annotation.PostConstruct;
32 import javax.servlet.ServletContext;
33 import org.apache.commons.lang3.StringUtils;
34 import org.onap.portalsdk.core.domain.App;
35 import org.onap.portalsdk.core.service.DataAccessService;
36 import org.onap.portalsdk.core.util.SystemProperties;
37 import org.onap.vid.aai.AaiClient;
38 import org.onap.vid.aai.AaiClientInterface;
39 import org.onap.vid.aai.AaiOverTLSClient;
40 import org.onap.vid.aai.AaiOverTLSClientInterface;
41 import org.onap.vid.aai.AaiOverTLSPropertySupplier;
42 import org.onap.vid.aai.AaiResponseTranslator;
43 import org.onap.vid.aai.PombaClientImpl;
44 import org.onap.vid.aai.PombaClientInterface;
45 import org.onap.vid.aai.PombaRestInterface;
46 import org.onap.vid.aai.model.PortDetailsTranslator;
47 import org.onap.vid.aai.util.AAIRestInterface;
48 import org.onap.vid.aai.util.CacheProvider;
49 import org.onap.vid.aai.util.HttpsAuthClient;
50 import org.onap.vid.aai.util.SSLContextProvider;
51 import org.onap.vid.aai.util.ServiceInstanceStandardQuery;
52 import org.onap.vid.aai.util.ServletRequestHelper;
53 import org.onap.vid.aai.util.SystemPropertyHelper;
54 import org.onap.vid.asdc.AsdcClient;
55 import org.onap.vid.asdc.parser.ToscaParserImpl2;
56 import org.onap.vid.asdc.parser.VidNotionsBuilder;
57 import org.onap.vid.asdc.rest.SdcRestClient;
58 import org.onap.vid.client.SyncRestClient;
59 import org.onap.vid.logging.VidLoggingInterceptor;
60 import org.onap.vid.properties.AsdcClientConfiguration;
61 import org.onap.vid.properties.Features;
62 import org.onap.vid.properties.VidProperties;
63 import org.onap.vid.scheduler.SchedulerService;
64 import org.onap.vid.scheduler.SchedulerServiceImpl;
65 import org.onap.vid.services.AAIServiceTree;
66 import org.onap.vid.services.AaiService;
67 import org.onap.vid.services.AaiServiceImpl;
68 import org.onap.vid.services.ChangeManagementService;
69 import org.onap.vid.services.PombaService;
70 import org.onap.vid.services.PombaServiceImpl;
71 import org.onap.vid.utils.JoshworksJacksonObjectMapper;
72 import org.onap.vid.utils.Logging;
73 import org.onap.vid.utils.SystemPropertiesWrapper;
74 import org.springframework.beans.factory.annotation.Autowired;
75 import org.springframework.beans.factory.annotation.Qualifier;
76 import org.springframework.context.annotation.Bean;
77 import org.springframework.context.annotation.Configuration;
78 import org.springframework.core.Ordered;
79 import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
80 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
81 import org.togglz.core.manager.FeatureManager;
82 import springfox.documentation.builders.PathSelectors;
83 import springfox.documentation.builders.RequestHandlerSelectors;
84 import springfox.documentation.spi.DocumentationType;
85 import springfox.documentation.spring.web.plugins.Docket;
86 import springfox.documentation.swagger2.annotations.EnableSwagger2;
87
88 @EnableSwagger2
89 @Configuration
90 public class WebConfig implements WebMvcConfigurer {
91
92     @Autowired DataAccessService dataAccessService;
93     @Autowired FeatureManager featureManager;
94     String portalAppPassword = System.getenv(VidProperties.PORTAL_APP_PASSWORD_ENVIRONMENT_VARIABLE_NAME);
95
96     @PostConstruct
97     public void persistPortalAppPassword() {
98         if (featureManager.isActive(Features.FLAG_GUILIN_CONFIG_PORTAL_APP_PASSWORD)) {
99             if (StringUtils.isEmpty(portalAppPassword)) {
100                 return;
101             }
102
103             // Read self app object (i.e. where id=1) from database as AppServiceImpl.getDefaultApp() does
104             App defaultApp = (App) dataAccessService.getDomainObject(App.class, 1L, null);
105
106             if (defaultApp == null || StringUtils.equals(defaultApp.getAppPassword(), portalAppPassword)) {
107                 return;
108             }
109
110             defaultApp.setAppPassword(portalAppPassword);
111             dataAccessService.saveDomainObject(defaultApp, null);
112         }
113     }
114
115
116     @Bean
117     public com.fasterxml.jackson.databind.ObjectMapper getObjectMapper() {
118         return new com.fasterxml.jackson.databind.ObjectMapper().registerModule(new KotlinModule());
119     }
120
121
122     @Bean
123     public SchedulerService schedulerService(ChangeManagementService changeManagementService) {
124         return new SchedulerServiceImpl(changeManagementService);
125     }
126
127     @Bean
128     public AaiService getAaiService(AaiClientInterface aaiClient, AaiResponseTranslator aaiResponseTranslator,
129         AAIServiceTree aaiServiceTree, Logging logging, ExecutorService executorService) {
130         return new AaiServiceImpl(aaiClient, aaiResponseTranslator, aaiServiceTree, executorService, logging);
131     }
132
133     @Bean
134     public AaiResponseTranslator aaiResponseTranslator() {
135         return new AaiResponseTranslator();
136     }
137
138     @Bean
139     public PortDetailsTranslator portDetailsTranslator() {
140         return new PortDetailsTranslator();
141     }
142
143     @Bean
144     public AaiClientInterface getAaiRestInterface(@Qualifier("aaiRestInterface") AAIRestInterface restController, PortDetailsTranslator portsDetailsTranslator, CacheProvider cacheProvider) {
145         return new AaiClient(restController, portsDetailsTranslator, cacheProvider);
146     }
147
148     @Bean(name = "aaiRestInterface")
149     public AAIRestInterface aaiRestInterface(HttpsAuthClient httpsAuthClientFactory,
150         ServletRequestHelper servletRequestHelper,
151         SystemPropertyHelper systemPropertyHelper,
152         Logging loggingService) {
153         return new AAIRestInterface(httpsAuthClientFactory, servletRequestHelper, systemPropertyHelper, loggingService);
154     }
155
156     @Bean
157     public PombaRestInterface getPombaRestInterface(HttpsAuthClient httpsAuthClientFactory,
158         ServletRequestHelper servletRequestHelper,
159         SystemPropertyHelper systemPropertyHelper,
160         Logging loggingService) {
161         return new PombaRestInterface(httpsAuthClientFactory, servletRequestHelper, systemPropertyHelper, loggingService);
162     }
163
164     @Bean
165     public SSLContextProvider sslContextProvider() {
166         return new SSLContextProvider();
167     }
168
169     @Bean
170     public SystemPropertyHelper systemPropertyHelper() {
171         return new SystemPropertyHelper();
172     }
173
174     @Bean
175     public ServletRequestHelper servletRequestHelper() {
176         return new ServletRequestHelper();
177     }
178
179     @Bean
180     public HttpsAuthClient httpsAuthClientFactory(ServletContext servletContext, SystemPropertyHelper systemPropertyHelper, SSLContextProvider sslContextProvider ,FeatureManager featureManager) {
181         final String certFilePath = new File(servletContext.getRealPath("/WEB-INF/cert/")).getAbsolutePath();
182         return new HttpsAuthClient(certFilePath, systemPropertyHelper, sslContextProvider, featureManager);
183     }
184
185     @Bean
186     public AsdcClient sdcClient(AsdcClientConfiguration asdcClientConfiguration, Logging loggingService) {
187         String auth = asdcClientConfiguration.getAsdcClientAuth();
188         String host = asdcClientConfiguration.getAsdcClientHost();
189         String protocol = asdcClientConfiguration.getAsdcClientProtocol();
190         int port = asdcClientConfiguration.getAsdcClientPort();
191
192         return new SdcRestClient(protocol + "://" + host + ":" + port + "/", auth,
193             new SyncRestClient( loggingService, true),
194             loggingService);
195     }
196
197     @Bean
198     public VidNotionsBuilder vidNotionsBuilder(FeatureManager featureManager) {
199         return new VidNotionsBuilder(featureManager);
200     }
201
202     @Bean
203     public ToscaParserImpl2 getToscaParser(VidNotionsBuilder vidNotionsBuilder) {
204         return new ToscaParserImpl2(vidNotionsBuilder);
205     }
206
207     @Bean
208     public PombaService getVerifyServiceInstanceService() {
209         return new PombaServiceImpl();
210     }
211
212     @Bean
213     public PombaClientInterface getVerifyServiceInstanceClientInterface() {
214         return new PombaClientImpl();
215     }
216
217     @Bean
218     public ServiceInstanceStandardQuery serviceInstanceStandardQuery(AaiClientInterface aaiClient) {
219         return new ServiceInstanceStandardQuery(aaiClient);
220     }
221
222     @Bean
223     public AaiOverTLSClientInterface aaiOverTLSClient(ObjectMapper unirestObjectMapper, SystemProperties systemProperties, Logging loggingService){
224         return new AaiOverTLSClient(
225             new SyncRestClient(unirestObjectMapper,  loggingService),
226             new AaiOverTLSPropertySupplier());
227     }
228
229     @Bean
230     public ObjectMapper unirestFasterxmlObjectMapper() {
231         return new JoshworksJacksonObjectMapper();
232     }
233
234     @Bean
235     public Docket api(){
236         return new Docket(DocumentationType.SWAGGER_2)
237                 .select()
238                 .apis(RequestHandlerSelectors.basePackage("org.onap.vid.controller.open"))
239                 .paths(PathSelectors.any())
240                 .build();
241     }
242
243     @Bean
244     public ExecutorService executorService() {
245         int threadsCount = defaultIfNull(Integer.parseInt(SystemProperties.getProperty(VidProperties.VID_THREAD_COUNT)), 1);
246         return Executors.newFixedThreadPool(threadsCount);
247     }
248
249     @Override
250     public void addInterceptors(InterceptorRegistry registry) {
251         registry.addInterceptor(
252                 new VidLoggingInterceptor(new ControllersUtils(new SystemPropertiesWrapper()))
253         ).order(Ordered.HIGHEST_PRECEDENCE);
254     }
255 }