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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.so.adapters.vnfmadapter.extclients.vnfm;
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;
59 * Configures the HttpRestServiceProvider for REST call to a VNFM.
62 public class VnfmServiceProviderConfiguration {
64 private static final Logger logger = LoggerFactory.getLogger(VnfmServiceProviderConfiguration.class);
66 @Value("${http.client.ssl.trust-store}")
67 private Resource keyStore;
68 @Value("${http.client.ssl.trust-store-password}")
69 private String keyStorePassword;
71 @Bean(name = "vnfmServiceProvider")
72 public HttpRestServiceProvider httpRestServiceProvider(
73 @Qualifier(CONFIGURABLE_REST_TEMPLATE) @Autowired final RestTemplate restTemplate) {
74 return getHttpRestServiceProvider(restTemplate, new BasicHttpHeadersProvider());
77 private HttpRestServiceProvider getHttpRestServiceProvider(final RestTemplate restTemplate,
78 final HttpHeadersProvider httpHeadersProvider) {
79 setGsonMessageConverter(restTemplate);
80 setTrustStore(restTemplate);
81 removeSpringClientFilter(restTemplate);
82 return new HttpRestServiceProviderImpl(restTemplate, httpHeadersProvider);
85 private void setGsonMessageConverter(final RestTemplate restTemplate) {
86 final Iterator<HttpMessageConverter<?>> iterator = restTemplate.getMessageConverters().iterator();
87 while (iterator.hasNext()) {
88 if (iterator.next() instanceof MappingJackson2HttpMessageConverter) {
92 final Gson gson = new JSON().getGson();
93 restTemplate.getMessageConverters().add(new GsonHttpMessageConverter(gson));
96 private void setTrustStore(final RestTemplate restTemplate) {
97 SSLContext sslContext;
99 sslContext = new SSLContextBuilder().loadTrustMaterial(keyStore.getURL(), keyStorePassword.toCharArray())
101 logger.info("Setting truststore: {}", keyStore.getURL());
102 final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext);
103 final HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory).build();
104 final HttpComponentsClientHttpRequestFactory factory =
105 new HttpComponentsClientHttpRequestFactory(httpClient);
106 restTemplate.setRequestFactory(factory);
107 } catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException | CertificateException
108 | IOException exception) {
109 logger.error("Error reading truststore, TLS connection to VNFM will fail.", exception);
113 private void removeSpringClientFilter(final RestTemplate restTemplate) {
114 ListIterator<ClientHttpRequestInterceptor> interceptorIterator = restTemplate.getInterceptors().listIterator();
115 while (interceptorIterator.hasNext()) {
116 if (interceptorIterator.next() instanceof SpringClientFilter) {
117 interceptorIterator.remove();