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.KeyStore;
28 import java.security.KeyStoreException;
29 import java.security.NoSuchAlgorithmException;
30 import java.security.UnrecoverableKeyException;
31 import java.security.cert.CertificateException;
32 import java.util.Iterator;
33 import java.util.ListIterator;
35 import java.util.UUID;
36 import java.util.concurrent.ConcurrentHashMap;
37 import javax.net.ssl.SSLContext;
38 import org.apache.commons.lang3.StringUtils;
39 import org.apache.http.client.HttpClient;
40 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
41 import org.apache.http.impl.client.HttpClients;
42 import org.apache.http.ssl.SSLContextBuilder;
43 import org.onap.aai.domain.yang.EsrSystemInfo;
44 import org.onap.aai.domain.yang.EsrVnfm;
45 import org.onap.logging.filter.spring.SpringClientPayloadFilter;
46 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.JSON;
47 import org.onap.so.configuration.rest.BasicHttpHeadersProvider;
48 import org.onap.so.logging.jaxrs.filter.SOSpringClientFilter;
49 import org.onap.so.rest.service.HttpRestServiceProvider;
50 import org.onap.so.rest.service.HttpRestServiceProviderImpl;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53 import org.springframework.beans.factory.annotation.Autowired;
54 import org.springframework.beans.factory.annotation.Qualifier;
55 import org.springframework.beans.factory.annotation.Value;
56 import org.springframework.context.annotation.Configuration;
57 import org.springframework.core.io.Resource;
58 import org.springframework.http.client.BufferingClientHttpRequestFactory;
59 import org.springframework.http.client.ClientHttpRequestInterceptor;
60 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
61 import org.springframework.http.converter.HttpMessageConverter;
62 import org.springframework.http.converter.json.GsonHttpMessageConverter;
63 import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
64 import org.springframework.security.oauth2.client.OAuth2RestTemplate;
65 import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails;
66 import org.springframework.web.client.RestTemplate;
69 * Configures the HttpRestServiceProvider for REST call to a VNFM.
72 public class VnfmServiceProviderConfiguration {
74 private static final Logger logger = LoggerFactory.getLogger(VnfmServiceProviderConfiguration.class);
75 private Map<String, HttpRestServiceProvider> mapOfVnfmIdToHttpRestServiceProvider = new ConcurrentHashMap<>();
77 @Value("${http.client.ssl.trust-store:#{null}}")
78 private Resource trustStore;
79 @Value("${http.client.ssl.trust-store-password:#{null}}")
80 private String trustStorePassword;
82 @Value("${server.ssl.key-store:#{null}}")
83 private Resource keyStoreResource;
84 @Value("${server.ssl.key--store-password:#{null}}")
85 private String keyStorePassword;
88 * This property is only intended to be temporary until the AAI schema is updated to support setting the endpoint
90 @Value("${vnfmadapter.temp.vnfm.oauth.endpoint:#{null}}")
91 private String oauthEndpoint;
93 @Qualifier(CONFIGURABLE_REST_TEMPLATE)
95 private RestTemplate defaultRestTemplate;
97 public HttpRestServiceProvider getHttpRestServiceProvider(final EsrVnfm vnfm) {
98 if (!mapOfVnfmIdToHttpRestServiceProvider.containsKey(vnfm.getVnfmId())) {
99 mapOfVnfmIdToHttpRestServiceProvider.put(vnfm.getVnfmId(), createHttpRestServiceProvider(vnfm));
101 return mapOfVnfmIdToHttpRestServiceProvider.get(vnfm.getVnfmId());
104 private HttpRestServiceProvider createHttpRestServiceProvider(final EsrVnfm vnfm) {
105 final RestTemplate restTemplate = createRestTemplate(vnfm);
106 setGsonMessageConverter(restTemplate);
107 if (trustStore != null) {
108 setTrustStore(restTemplate);
110 return new HttpRestServiceProviderImpl(restTemplate, new BasicHttpHeadersProvider());
113 private RestTemplate createRestTemplate(final EsrVnfm vnfm) {
115 for (final EsrSystemInfo esrSystemInfo : vnfm.getEsrSystemInfoList().getEsrSystemInfo()) {
116 if (!StringUtils.isEmpty(esrSystemInfo.getUserName())
117 && !StringUtils.isEmpty(esrSystemInfo.getPassword())) {
118 return createOAuth2RestTemplate(esrSystemInfo);
122 return defaultRestTemplate;
125 private OAuth2RestTemplate createOAuth2RestTemplate(final EsrSystemInfo esrSystemInfo) {
126 logger.debug("Getting OAuth2RestTemplate ...");
127 final ClientCredentialsResourceDetails resourceDetails = new ClientCredentialsResourceDetails();
128 resourceDetails.setId(UUID.randomUUID().toString());
129 resourceDetails.setClientId(esrSystemInfo.getUserName());
130 resourceDetails.setClientSecret(esrSystemInfo.getPassword());
131 resourceDetails.setAccessTokenUri(
132 oauthEndpoint == null ? esrSystemInfo.getServiceUrl().replace("vnflcm/v1", "oauth/token")
134 resourceDetails.setGrantType("client_credentials");
135 return new OAuth2RestTemplate(resourceDetails);
138 private void setGsonMessageConverter(final RestTemplate restTemplate) {
139 final Iterator<HttpMessageConverter<?>> iterator = restTemplate.getMessageConverters().iterator();
140 while (iterator.hasNext()) {
141 if (iterator.next() instanceof MappingJackson2HttpMessageConverter) {
145 final Gson gson = new JSON().getGson();
146 restTemplate.getMessageConverters().add(new GsonHttpMessageConverter(gson));
149 private void setTrustStore(final RestTemplate restTemplate) {
150 SSLContext sslContext;
152 if (keyStoreResource != null) {
153 KeyStore keystore = KeyStore.getInstance("pkcs12");
154 keystore.load(keyStoreResource.getInputStream(), keyStorePassword.toCharArray());
156 new SSLContextBuilder().loadTrustMaterial(trustStore.getURL(), trustStorePassword.toCharArray())
157 .loadKeyMaterial(keystore, keyStorePassword.toCharArray()).build();
159 sslContext = new SSLContextBuilder()
160 .loadTrustMaterial(trustStore.getURL(), trustStorePassword.toCharArray()).build();
162 logger.info("Setting truststore: {}", trustStore.getURL());
163 final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext);
164 final HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory).build();
165 final HttpComponentsClientHttpRequestFactory factory =
166 new HttpComponentsClientHttpRequestFactory(httpClient);
167 restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(factory));
168 } catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException | CertificateException
169 | IOException | UnrecoverableKeyException exception) {
170 logger.error("Error reading truststore, TLS connection to VNFM will fail.", exception);
174 private void removeSpringClientFilter(final RestTemplate restTemplate) {
175 ListIterator<ClientHttpRequestInterceptor> interceptorIterator = restTemplate.getInterceptors().listIterator();
176 while (interceptorIterator.hasNext()) {
177 ClientHttpRequestInterceptor interceptor = interceptorIterator.next();
178 if (interceptor instanceof SOSpringClientFilter || interceptor instanceof SpringClientPayloadFilter) {
179 interceptorIterator.remove();