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