6c5595ca3925113dbfc483b00c5c292de32b987d
[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.servlet.ServletContext;
32 import org.onap.logging.filter.spring.LoggingInterceptor;
33 import org.onap.portalsdk.core.util.SystemProperties;
34 import org.onap.vid.aai.AaiClient;
35 import org.onap.vid.aai.AaiClientInterface;
36 import org.onap.vid.aai.AaiOverTLSClient;
37 import org.onap.vid.aai.AaiOverTLSClientInterface;
38 import org.onap.vid.aai.AaiOverTLSPropertySupplier;
39 import org.onap.vid.aai.AaiResponseTranslator;
40 import org.onap.vid.aai.PombaClientImpl;
41 import org.onap.vid.aai.PombaClientInterface;
42 import org.onap.vid.aai.PombaRestInterface;
43 import org.onap.vid.aai.model.PortDetailsTranslator;
44 import org.onap.vid.aai.util.AAIRestInterface;
45 import org.onap.vid.aai.util.CacheProvider;
46 import org.onap.vid.aai.util.HttpsAuthClient;
47 import org.onap.vid.aai.util.SSLContextProvider;
48 import org.onap.vid.aai.util.ServiceInstanceStandardQuery;
49 import org.onap.vid.aai.util.ServletRequestHelper;
50 import org.onap.vid.aai.util.SystemPropertyHelper;
51 import org.onap.vid.asdc.AsdcClient;
52 import org.onap.vid.asdc.parser.ToscaParserImpl2;
53 import org.onap.vid.asdc.parser.VidNotionsBuilder;
54 import org.onap.vid.asdc.rest.SdcRestClient;
55 import org.onap.vid.client.SyncRestClient;
56 import org.onap.vid.properties.AsdcClientConfiguration;
57 import org.onap.vid.properties.VidProperties;
58 import org.onap.vid.scheduler.SchedulerService;
59 import org.onap.vid.scheduler.SchedulerServiceImpl;
60 import org.onap.vid.services.AAIServiceTree;
61 import org.onap.vid.services.AAITreeNodeBuilder;
62 import org.onap.vid.services.AaiService;
63 import org.onap.vid.services.AaiServiceImpl;
64 import org.onap.vid.services.ChangeManagementService;
65 import org.onap.vid.services.PombaService;
66 import org.onap.vid.services.PombaServiceImpl;
67 import org.onap.vid.utils.JoshworksJacksonObjectMapper;
68 import org.onap.vid.utils.Logging;
69 import org.springframework.beans.factory.annotation.Qualifier;
70 import org.springframework.context.annotation.Bean;
71 import org.springframework.context.annotation.Configuration;
72 import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
73 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
74 import org.togglz.core.manager.FeatureManager;
75 import springfox.documentation.builders.PathSelectors;
76 import springfox.documentation.builders.RequestHandlerSelectors;
77 import springfox.documentation.spi.DocumentationType;
78 import springfox.documentation.spring.web.plugins.Docket;
79 import springfox.documentation.swagger2.annotations.EnableSwagger2;
80
81 @EnableSwagger2
82 @Configuration
83 public class WebConfig implements WebMvcConfigurer {
84
85     /**
86      * Gets the object mapper.
87      *
88      * @return the object mapper
89      */
90     @Bean
91     public com.fasterxml.jackson.databind.ObjectMapper getObjectMapper() {
92         return new com.fasterxml.jackson.databind.ObjectMapper().registerModule(new KotlinModule());
93     }
94
95
96     @Bean
97     public SchedulerService schedulerService(ChangeManagementService changeManagementService) {
98         return new SchedulerServiceImpl(changeManagementService);
99     }
100
101     @Bean
102     public AaiService getAaiService(AaiClientInterface aaiClient, AaiResponseTranslator aaiResponseTranslator,
103         AAITreeNodeBuilder aaiTreeNode, AAIServiceTree aaiServiceTree, ExecutorService executorService) {
104         return new AaiServiceImpl(aaiClient, aaiResponseTranslator, aaiServiceTree, executorService);
105     }
106
107     @Bean
108     public AaiResponseTranslator aaiResponseTranslator() {
109         return new AaiResponseTranslator();
110     }
111
112     @Bean
113     public PortDetailsTranslator portDetailsTranslator() {
114         return new PortDetailsTranslator();
115     }
116
117     @Bean
118     public AaiClientInterface getAaiRestInterface(@Qualifier("aaiRestInterface") AAIRestInterface restController, PortDetailsTranslator portsDetailsTranslator, CacheProvider cacheProvider) {
119         return new AaiClient(restController, portsDetailsTranslator, cacheProvider);
120     }
121
122     @Bean(name = "aaiRestInterface")
123     public AAIRestInterface aaiRestInterface(HttpsAuthClient httpsAuthClientFactory,
124         ServletRequestHelper servletRequestHelper,
125         SystemPropertyHelper systemPropertyHelper,
126         Logging loggingService) {
127         return new AAIRestInterface(httpsAuthClientFactory, servletRequestHelper, systemPropertyHelper, loggingService);
128     }
129
130     @Bean
131     public PombaRestInterface getPombaRestInterface(HttpsAuthClient httpsAuthClientFactory,
132         ServletRequestHelper servletRequestHelper,
133         SystemPropertyHelper systemPropertyHelper,
134         Logging loggingService) {
135         return new PombaRestInterface(httpsAuthClientFactory, servletRequestHelper, systemPropertyHelper, loggingService);
136     }
137
138     @Bean
139     public SSLContextProvider sslContextProvider() {
140         return new SSLContextProvider();
141     }
142
143     @Bean
144     public SystemPropertyHelper systemPropertyHelper() {
145         return new SystemPropertyHelper();
146     }
147
148     @Bean
149     public ServletRequestHelper servletRequestHelper() {
150         return new ServletRequestHelper();
151     }
152
153     @Bean
154     public HttpsAuthClient httpsAuthClientFactory(ServletContext servletContext, SystemPropertyHelper systemPropertyHelper, SSLContextProvider sslContextProvider ,FeatureManager featureManager) {
155         final String certFilePath = new File(servletContext.getRealPath("/WEB-INF/cert/")).getAbsolutePath();
156         return new HttpsAuthClient(certFilePath, systemPropertyHelper, sslContextProvider, featureManager);
157     }
158
159     @Bean
160     public AsdcClient sdcClient(AsdcClientConfiguration asdcClientConfiguration, Logging loggingService) {
161         String auth = asdcClientConfiguration.getAsdcClientAuth();
162         String host = asdcClientConfiguration.getAsdcClientHost();
163         String protocol = asdcClientConfiguration.getAsdcClientProtocol();
164         int port = asdcClientConfiguration.getAsdcClientPort();
165
166         return new SdcRestClient(protocol + "://" + host + ":" + port + "/", auth,
167             new SyncRestClient( loggingService, true),
168             loggingService);
169     }
170
171     @Bean
172     public VidNotionsBuilder vidNotionsBuilder(FeatureManager featureManager) {
173         return new VidNotionsBuilder(featureManager);
174     }
175
176     @Bean
177     public ToscaParserImpl2 getToscaParser(VidNotionsBuilder vidNotionsBuilder) {
178         return new ToscaParserImpl2(vidNotionsBuilder);
179     }
180
181     @Bean
182     public PombaService getVerifyServiceInstanceService() {
183         return new PombaServiceImpl();
184     }
185
186     @Bean
187     public PombaClientInterface getVerifyServiceInstanceClientInterface() {
188         return new PombaClientImpl();
189     }
190
191     @Bean
192     public ServiceInstanceStandardQuery serviceInstanceStandardQuery(AaiClientInterface aaiClient) {
193         return new ServiceInstanceStandardQuery(aaiClient);
194     }
195
196     @Bean
197     public AaiOverTLSClientInterface aaiOverTLSClient(ObjectMapper unirestObjectMapper, SystemProperties systemProperties, Logging loggingService){
198         return new AaiOverTLSClient(
199             new SyncRestClient(unirestObjectMapper,  loggingService),
200             new AaiOverTLSPropertySupplier());
201     }
202
203     @Bean
204     public ObjectMapper unirestFasterxmlObjectMapper() {
205         return new JoshworksJacksonObjectMapper();
206     }
207
208     @Bean
209     public Docket api(){
210         return new Docket(DocumentationType.SWAGGER_2)
211                 .select()
212                 .apis(RequestHandlerSelectors.basePackage("org.onap.vid.controller.open"))
213                 .paths(PathSelectors.any())
214                 .build();
215     }
216
217     @Bean
218     public ExecutorService executorService() {
219         int threadsCount = defaultIfNull(Integer.parseInt(SystemProperties.getProperty(VidProperties.VID_THREAD_COUNT)), 1);
220         return Executors.newFixedThreadPool(threadsCount);
221     }
222
223     @Override
224     public void addInterceptors(InterceptorRegistry registry) {
225         registry.addInterceptor(new LoggingInterceptor());
226     }
227 }