fe710ec00b33e6313d04dabc8149b4cb57cb4f37
[so.git] / so-etsi-nfvo / so-etsi-nfvo-ns-lcm / so-etsi-nfvo-ns-lcm-bpmn-flows / src / main / java / org / onap / so / etsi / nfvo / ns / lcm / bpmn / flows / extclients / vnfm / Sol003AdapterConfiguration.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.extclients.vnfm;
22
23 import java.io.IOException;
24 import java.security.KeyManagementException;
25 import java.security.KeyStoreException;
26 import java.security.NoSuchAlgorithmException;
27 import java.security.cert.CertificateException;
28 import java.util.Iterator;
29 import javax.net.ssl.SSLContext;
30 import org.apache.http.client.HttpClient;
31 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
32 import org.apache.http.impl.client.HttpClients;
33 import org.apache.http.ssl.SSLContextBuilder;
34 import org.onap.logging.filter.spring.SpringClientPayloadFilter;
35 import org.onap.so.configuration.rest.BasicHttpHeadersProvider;
36 import org.onap.so.configuration.rest.HttpComponentsClientConfiguration;
37 import org.onap.so.configuration.rest.HttpHeadersProvider;
38 import org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.GsonProvider;
39 import org.onap.so.logging.jaxrs.filter.SOSpringClientFilter;
40 import org.onap.so.rest.service.HttpRestServiceProvider;
41 import org.onap.so.rest.service.HttpRestServiceProviderImpl;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.beans.factory.annotation.Qualifier;
46 import org.springframework.beans.factory.annotation.Value;
47 import org.springframework.context.annotation.Bean;
48 import org.springframework.context.annotation.Configuration;
49 import org.springframework.core.io.Resource;
50 import org.springframework.http.client.BufferingClientHttpRequestFactory;
51 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
52 import org.springframework.http.converter.HttpMessageConverter;
53 import org.springframework.http.converter.json.GsonHttpMessageConverter;
54 import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
55 import org.springframework.web.client.RestTemplate;
56
57 /**
58  * @author Waqas Ikram (waqas.ikram@est.tech)
59  *
60  */
61 @Configuration
62 public class Sol003AdapterConfiguration {
63
64     private static final Logger logger = LoggerFactory.getLogger(Sol003AdapterConfiguration.class);
65
66     public static final String SOL003_ADAPTER_REST_TEMPLATE_BEAN = "Sol003AdapterRestTemplateBean";
67     public static final String SOL003_ADAPTER_HTTP_REST_SERVICE_PROVIDER_BEAN =
68             "Sol003AdapterHttpRestServiceProviderBean";
69
70     @Value("${rest.http.client.configuration.ssl.trustStore:#{null}}")
71     private Resource trustStore;
72
73     @Value("${rest.http.client.configuration.ssl.trustStorePassword:#{null}}")
74     private String trustStorePassword;
75
76     @Value("so.adapters.sol003-adapter.auth:Basic dm5mbTpwYXNzd29yZDEk")
77     private String sol003AdapterBasicAuth;
78
79     @Autowired
80     private GsonProvider gsonProvider;
81
82     @Autowired
83     private HttpComponentsClientConfiguration httpComponentsClientConfiguration;
84
85     @Bean
86     @Qualifier(SOL003_ADAPTER_REST_TEMPLATE_BEAN)
87     public RestTemplate sol003AdapterRestTemplate() {
88         final HttpComponentsClientHttpRequestFactory clientHttpRequestFactory =
89                 httpComponentsClientConfiguration.httpComponentsClientHttpRequestFactory();
90         final RestTemplate restTemplate =
91                 new RestTemplate(new BufferingClientHttpRequestFactory(clientHttpRequestFactory));
92         restTemplate.getInterceptors().add(new SOSpringClientFilter());
93         restTemplate.getInterceptors().add((new SpringClientPayloadFilter()));
94         return restTemplate;
95
96     }
97
98     @Bean
99     @Qualifier(SOL003_ADAPTER_HTTP_REST_SERVICE_PROVIDER_BEAN)
100     public HttpRestServiceProvider sol003AdapaterHttpRestServiceProvider(
101             @Qualifier(SOL003_ADAPTER_REST_TEMPLATE_BEAN) @Autowired final RestTemplate restTemplate) {
102
103         if (trustStore != null) {
104             setTrustStore(restTemplate);
105         }
106         setGsonMessageConverter(restTemplate);
107         return getHttpRestServiceProvider(restTemplate, new BasicHttpHeadersProvider(sol003AdapterBasicAuth));
108     }
109
110     private void setTrustStore(final RestTemplate restTemplate) {
111         try {
112             final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(getSSLContext());
113             final HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory).build();
114             final HttpComponentsClientHttpRequestFactory factory =
115                     new HttpComponentsClientHttpRequestFactory(httpClient);
116             restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(factory));
117         } catch (Exception exception) {
118             logger.error("Error reading truststore, TLS connection to VNFM will fail.", exception);
119         }
120     }
121
122     private SSLContext getSSLContext() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException,
123             CertificateException, IOException {
124         if (trustStore != null) {
125             logger.info("Setting truststore: {}", trustStore.getURL());
126             return new SSLContextBuilder().loadTrustMaterial(trustStore.getURL(), trustStorePassword.toCharArray())
127                     .build();
128         }
129         logger.info("Setting Default SSL ...");
130         return SSLContext.getDefault();
131
132     }
133
134     private HttpRestServiceProvider getHttpRestServiceProvider(final RestTemplate restTemplate,
135             final HttpHeadersProvider httpHeadersProvider) {
136         return new HttpRestServiceProviderImpl(restTemplate, httpHeadersProvider.getHttpHeaders());
137     }
138
139     private void setGsonMessageConverter(final RestTemplate restTemplate) {
140         final Iterator<HttpMessageConverter<?>> iterator = restTemplate.getMessageConverters().iterator();
141         while (iterator.hasNext()) {
142             if (iterator.next() instanceof MappingJackson2HttpMessageConverter) {
143                 iterator.remove();
144             }
145         }
146         restTemplate.getMessageConverters().add(new GsonHttpMessageConverter(gsonProvider.getGson()));
147     }
148 }