ab631837dbd527d0030a7ecbfc338ad168cf595d
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 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.adapters.vnfmadapter.extclients.vnfm;
22
23 import static org.onap.so.client.RestTemplateConfig.CONFIGURABLE_REST_TEMPLATE;
24 import com.google.gson.Gson;
25 import java.io.IOException;
26 import java.security.KeyManagementException;
27 import java.security.KeyStoreException;
28 import java.security.NoSuchAlgorithmException;
29 import java.security.cert.CertificateException;
30 import java.util.Iterator;
31 import java.util.ListIterator;
32 import javax.net.ssl.SSLContext;
33 import org.apache.http.client.HttpClient;
34 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
35 import org.apache.http.impl.client.HttpClients;
36 import org.apache.http.ssl.SSLContextBuilder;
37 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.JSON;
38 import org.onap.so.configuration.rest.BasicHttpHeadersProvider;
39 import org.onap.so.configuration.rest.HttpHeadersProvider;
40 import org.onap.so.logging.jaxrs.filter.SpringClientFilter;
41 import org.onap.so.rest.service.HttpRestServiceProvider;
42 import org.onap.so.rest.service.HttpRestServiceProviderImpl;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.beans.factory.annotation.Qualifier;
47 import org.springframework.beans.factory.annotation.Value;
48 import org.springframework.context.annotation.Bean;
49 import org.springframework.context.annotation.Configuration;
50 import org.springframework.core.io.Resource;
51 import org.springframework.http.client.ClientHttpRequestInterceptor;
52 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
53 import org.springframework.http.converter.HttpMessageConverter;
54 import org.springframework.http.converter.json.GsonHttpMessageConverter;
55 import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
56 import org.springframework.web.client.RestTemplate;
57
58 /**
59  * Configures the HttpRestServiceProvider for REST call to a VNFM.
60  */
61 @Configuration
62 public class VnfmServiceProviderConfiguration {
63
64     private static final Logger logger = LoggerFactory.getLogger(VnfmServiceProviderConfiguration.class);
65
66     @Value("${http.client.ssl.trust-store:#{null}}")
67     private Resource keyStore;
68     @Value("${http.client.ssl.trust-store-password:#{null}}")
69     private String keyStorePassword;
70
71     @Bean(name = "vnfmServiceProvider")
72     public HttpRestServiceProvider httpRestServiceProvider(
73             @Qualifier(CONFIGURABLE_REST_TEMPLATE) @Autowired final RestTemplate restTemplate) {
74         return getHttpRestServiceProvider(restTemplate, new BasicHttpHeadersProvider());
75     }
76
77     private HttpRestServiceProvider getHttpRestServiceProvider(final RestTemplate restTemplate,
78             final HttpHeadersProvider httpHeadersProvider) {
79         setGsonMessageConverter(restTemplate);
80         if (keyStore != null) {
81             setTrustStore(restTemplate);
82         }
83         removeSpringClientFilter(restTemplate);
84         return new HttpRestServiceProviderImpl(restTemplate, httpHeadersProvider);
85     }
86
87     private void setGsonMessageConverter(final RestTemplate restTemplate) {
88         final Iterator<HttpMessageConverter<?>> iterator = restTemplate.getMessageConverters().iterator();
89         while (iterator.hasNext()) {
90             if (iterator.next() instanceof MappingJackson2HttpMessageConverter) {
91                 iterator.remove();
92             }
93         }
94         final Gson gson = new JSON().getGson();
95         restTemplate.getMessageConverters().add(new GsonHttpMessageConverter(gson));
96     }
97
98     private void setTrustStore(final RestTemplate restTemplate) {
99         SSLContext sslContext;
100         try {
101             sslContext = new SSLContextBuilder().loadTrustMaterial(keyStore.getURL(), keyStorePassword.toCharArray())
102                     .build();
103             logger.info("Setting truststore: {}", keyStore.getURL());
104             final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext);
105             final HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory).build();
106             final HttpComponentsClientHttpRequestFactory factory =
107                     new HttpComponentsClientHttpRequestFactory(httpClient);
108             restTemplate.setRequestFactory(factory);
109         } catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException | CertificateException
110                 | IOException exception) {
111             logger.error("Error reading truststore, TLS connection to VNFM will fail.", exception);
112         }
113     }
114
115     private void removeSpringClientFilter(final RestTemplate restTemplate) {
116         ListIterator<ClientHttpRequestInterceptor> interceptorIterator = restTemplate.getInterceptors().listIterator();
117         while (interceptorIterator.hasNext()) {
118             if (interceptorIterator.next() instanceof SpringClientFilter) {
119                 interceptorIterator.remove();
120             }
121         }
122     }
123
124 }