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