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