Merge "Logging improvements"
[vid.git] / vid-app-common / src / main / java / org / onap / vid / controllers / WebConfig.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2018 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.controllers;
23
24 import com.fasterxml.jackson.core.JsonProcessingException;
25 import io.joshworks.restclient.http.mapper.ObjectMapper;
26 import java.io.IOException;
27 import org.onap.vid.aai.AaiClient;
28 import org.onap.vid.aai.AaiClientInterface;
29 import org.onap.vid.aai.AaiOverTLSClient;
30 import org.onap.vid.aai.AaiOverTLSClientInterface;
31 import org.onap.vid.aai.AaiOverTLSPropertySupplier;
32 import org.onap.vid.aai.AaiResponseTranslator;
33 import org.onap.vid.aai.PombaClientImpl;
34 import org.onap.vid.aai.PombaClientInterface;
35 import org.onap.vid.aai.PombaRestInterface;
36 import org.onap.vid.aai.model.PortDetailsTranslator;
37 import org.onap.vid.aai.util.AAIRestInterface;
38 import org.onap.vid.aai.util.HttpsAuthClient;
39 import org.onap.vid.aai.util.SSLContextProvider;
40 import org.onap.vid.aai.util.ServletRequestHelper;
41 import org.onap.vid.aai.util.SystemPropertyHelper;
42 import org.onap.vid.asdc.AsdcClient;
43 import org.onap.vid.asdc.parser.ToscaParserImpl2;
44 import org.onap.vid.asdc.rest.SdcRestClient;
45 import org.onap.vid.client.SyncRestClient;
46 import org.onap.vid.client.SyncRestClientInterface;
47 import org.onap.vid.properties.AsdcClientConfiguration;
48 import org.onap.vid.services.AaiService;
49 import org.onap.vid.services.AaiServiceImpl;
50 import org.onap.vid.services.PombaService;
51 import org.onap.vid.services.PombaServiceImpl;
52 import org.onap.vid.services.VidService;
53 import org.onap.vid.services.VidServiceImpl;
54 import org.onap.vid.scheduler.SchedulerRestInterface;
55 import org.onap.vid.scheduler.SchedulerRestInterfaceIfc;
56 import org.springframework.beans.factory.annotation.Qualifier;
57 import org.springframework.context.annotation.Bean;
58 import org.springframework.context.annotation.Configuration;
59 import org.togglz.core.manager.FeatureManager;
60
61 import javax.servlet.ServletContext;
62 import java.io.File;
63
64 @Configuration
65 public class WebConfig {
66
67     /**
68      * Gets the object mapper.
69      *
70      * @return the object mapper
71      */
72     @Bean
73     public com.fasterxml.jackson.databind.ObjectMapper getObjectMapper() {
74         return new com.fasterxml.jackson.databind.ObjectMapper();
75     }
76
77
78     @Bean
79     public VidService vidService(AsdcClient asdcClient, FeatureManager featureManager) {
80         return new VidServiceImpl(asdcClient, featureManager);
81     }
82
83     @Bean
84     public AaiService getAaiService() {
85         return new AaiServiceImpl();
86     }
87
88     @Bean
89     public AaiResponseTranslator aaiResponseTranslator() {
90         return new AaiResponseTranslator();
91     }
92
93     @Bean
94     public PortDetailsTranslator portDetailsTranslator() {
95         return new PortDetailsTranslator();
96     }
97
98     @Bean
99     public AaiClientInterface getAaiRestInterface(@Qualifier("aaiRestInterface") AAIRestInterface restController, PortDetailsTranslator portsDetailsTranslator) {
100         return new AaiClient(restController, portsDetailsTranslator);
101     }
102
103     @Bean(name = "aaiRestInterface")
104     public AAIRestInterface aaiRestInterface(HttpsAuthClient httpsAuthClientFactory, ServletRequestHelper servletRequestHelper, SystemPropertyHelper systemPropertyHelper) {
105         return new AAIRestInterface(httpsAuthClientFactory, servletRequestHelper, systemPropertyHelper);
106     }
107
108     @Bean
109     public PombaRestInterface getPombaRestInterface(HttpsAuthClient httpsAuthClientFactory, ServletRequestHelper servletRequestHelper, SystemPropertyHelper systemPropertyHelper) {
110         return new PombaRestInterface(httpsAuthClientFactory, servletRequestHelper, systemPropertyHelper);
111     }
112
113     @Bean
114     public SSLContextProvider sslContextProvider() {
115         return new SSLContextProvider();
116     }
117
118     @Bean
119     public SystemPropertyHelper systemPropertyHelper() {
120         return new SystemPropertyHelper();
121     }
122
123     @Bean
124     public ServletRequestHelper servletRequestHelper() {
125         return new ServletRequestHelper();
126     }
127
128     @Bean
129     public HttpsAuthClient httpsAuthClientFactory(ServletContext servletContext, SystemPropertyHelper systemPropertyHelper, SSLContextProvider sslContextProvider) {
130         final String certFilePath = new File(servletContext.getRealPath("/WEB-INF/cert/")).getAbsolutePath();
131         return new HttpsAuthClient(certFilePath, systemPropertyHelper, sslContextProvider);
132     }
133
134     @Bean
135     public AsdcClient sdcClient(AsdcClientConfiguration asdcClientConfiguration, SyncRestClientInterface syncRestClient) {
136         String auth = asdcClientConfiguration.getAsdcClientAuth();
137         String host = asdcClientConfiguration.getAsdcClientHost();
138         String protocol = asdcClientConfiguration.getAsdcClientProtocol();
139         int port = asdcClientConfiguration.getAsdcClientPort();
140
141         return new SdcRestClient(protocol + "://" + host + ":" + port + "/", auth, syncRestClient);
142     }
143
144     @Bean
145     public SyncRestClientInterface syncRestClient() {
146         return new SyncRestClient();
147     }
148
149     @Bean
150     public ToscaParserImpl2 getToscaParser() {
151         return new ToscaParserImpl2();
152     }
153
154     @Bean
155     public PombaService getVerifyServiceInstanceService() {
156         return new PombaServiceImpl();
157     }
158
159     @Bean
160     public PombaClientInterface getVerifyServiceInstanceClientInterface() {
161         return new PombaClientImpl();
162     }
163
164     @Bean
165     public SchedulerRestInterfaceIfc getSchedulerRestInterface(){
166         return new SchedulerRestInterface();
167     }
168
169     @Bean(name = "aaiClientForFasterXmlMapping")
170     public AaiOverTLSClientInterface getAaiClientForFasterXmlMapping(){
171          ObjectMapper objectMapper = new ObjectMapper() {
172
173             com.fasterxml.jackson.databind.ObjectMapper om = new com.fasterxml.jackson.databind.ObjectMapper();
174
175             @Override
176             public <T> T readValue(String s, Class<T> aClass) {
177                 try {
178                     return om.readValue(s, aClass);
179                 } catch (IOException e) {
180                     throw new RuntimeException(e);
181                 }
182             }
183
184             @Override
185             public String writeValue(Object o) {
186                 try {
187                     return om.writeValueAsString(o);
188                 } catch (JsonProcessingException e) {
189                     throw new RuntimeException(e);
190                 }
191             }
192         };
193
194         return new AaiOverTLSClient(new SyncRestClient(objectMapper), new AaiOverTLSPropertySupplier());
195     }
196
197
198     @Bean(name = "aaiClientForCodehausMapping")
199     public AaiOverTLSClientInterface getAaiClientForCodehausMapping() {
200
201        ObjectMapper objectMapper = new ObjectMapper() {
202
203             org.codehaus.jackson.map.ObjectMapper om = new org.codehaus.jackson.map.ObjectMapper();
204
205             @Override
206             public <T> T readValue(String s, Class<T> aClass) {
207                 try {
208                     return om.readValue(s, aClass);
209                 } catch (IOException e) {
210                     throw new RuntimeException(e);
211                 }
212             }
213
214             @Override
215             public String writeValue(Object o) {
216                 try {
217                     return om.writeValueAsString(o);
218                 } catch (IOException e) {
219                     throw new RuntimeException(e);
220                 }
221             }
222         };
223
224         return new AaiOverTLSClient(new SyncRestClient(objectMapper), new AaiOverTLSPropertySupplier());
225     }
226 }