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.etsisol003adapter.lcm.extclients.vnfm;
23 import static org.onap.so.client.RestTemplateConfig.CONFIGURABLE_REST_TEMPLATE;
24 import java.io.IOException;
25 import java.security.KeyManagementException;
26 import java.security.KeyStore;
27 import java.security.KeyStoreException;
28 import java.security.NoSuchAlgorithmException;
29 import java.security.UnrecoverableKeyException;
30 import java.security.cert.CertificateException;
32 import java.util.UUID;
33 import java.util.concurrent.ConcurrentHashMap;
34 import javax.net.ssl.SSLContext;
35 import org.apache.commons.lang3.StringUtils;
36 import org.apache.http.client.HttpClient;
37 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
38 import org.apache.http.impl.client.HttpClients;
39 import org.apache.http.ssl.SSLContextBuilder;
40 import org.onap.aai.domain.yang.EsrSystemInfo;
41 import org.onap.aai.domain.yang.EsrVnfm;
42 import org.onap.so.adapters.etsisol003adapter.lcm.extclients.AbstractServiceProviderConfiguration;
43 import org.onap.so.configuration.rest.BasicHttpHeadersProvider;
44 import org.onap.so.rest.service.HttpRestServiceProvider;
45 import org.onap.so.rest.service.HttpRestServiceProviderImpl;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48 import org.springframework.beans.factory.annotation.Autowired;
49 import org.springframework.beans.factory.annotation.Qualifier;
50 import org.springframework.beans.factory.annotation.Value;
51 import org.springframework.context.annotation.Configuration;
52 import org.springframework.core.io.Resource;
53 import org.springframework.http.client.BufferingClientHttpRequestFactory;
54 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
55 import org.springframework.security.oauth2.client.OAuth2RestTemplate;
56 import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails;
57 import org.springframework.web.client.RestTemplate;
60 * Configures the HttpRestServiceProvider for REST call to a VNFM.
63 public class VnfmServiceProviderConfiguration extends AbstractServiceProviderConfiguration {
65 private static final Logger logger = LoggerFactory.getLogger(VnfmServiceProviderConfiguration.class);
66 private final Map<String, HttpRestServiceProvider> mapOfVnfmIdToHttpRestServiceProvider = new ConcurrentHashMap<>();
68 @Value("${http.client.ssl.trust-store:#{null}}")
69 private Resource trustStore;
70 @Value("${http.client.ssl.trust-store-password:#{null}}")
71 private String trustStorePassword;
73 @Value("${server.ssl.key-store:#{null}}")
74 private Resource keyStoreResource;
75 @Value("${server.ssl.key--store-password:#{null}}")
76 private String keyStorePassword;
79 * This property is only intended to be temporary until the AAI schema is updated to support setting the endpoint
81 @Value("${vnfmadapter.temp.vnfm.oauth.endpoint:#{null}}")
82 private String oauthEndpoint;
84 @Qualifier(CONFIGURABLE_REST_TEMPLATE)
86 private RestTemplate defaultRestTemplate;
88 public HttpRestServiceProvider getHttpRestServiceProvider(final EsrVnfm vnfm) {
89 if (!mapOfVnfmIdToHttpRestServiceProvider.containsKey(vnfm.getVnfmId())) {
90 mapOfVnfmIdToHttpRestServiceProvider.put(vnfm.getVnfmId(), createHttpRestServiceProvider(vnfm));
92 return mapOfVnfmIdToHttpRestServiceProvider.get(vnfm.getVnfmId());
95 private HttpRestServiceProvider createHttpRestServiceProvider(final EsrVnfm vnfm) {
96 final RestTemplate restTemplate = createRestTemplate(vnfm);
97 setGsonMessageConverter(restTemplate);
98 if (trustStore != null) {
99 setTrustStore(restTemplate);
101 return new HttpRestServiceProviderImpl(restTemplate, new BasicHttpHeadersProvider().getHttpHeaders());
104 private RestTemplate createRestTemplate(final EsrVnfm vnfm) {
106 for (final EsrSystemInfo esrSystemInfo : vnfm.getEsrSystemInfoList().getEsrSystemInfo()) {
107 if (!StringUtils.isEmpty(esrSystemInfo.getUserName())
108 && !StringUtils.isEmpty(esrSystemInfo.getPassword())) {
109 return createOAuth2RestTemplate(esrSystemInfo);
113 return defaultRestTemplate;
116 private OAuth2RestTemplate createOAuth2RestTemplate(final EsrSystemInfo esrSystemInfo) {
117 logger.debug("Getting OAuth2RestTemplate ...");
118 final ClientCredentialsResourceDetails resourceDetails = new ClientCredentialsResourceDetails();
119 resourceDetails.setId(UUID.randomUUID().toString());
120 resourceDetails.setClientId(esrSystemInfo.getUserName());
121 resourceDetails.setClientSecret(esrSystemInfo.getPassword());
122 resourceDetails.setAccessTokenUri(
123 oauthEndpoint == null ? esrSystemInfo.getServiceUrl().replace("vnflcm/v1", "oauth/token")
125 resourceDetails.setGrantType("client_credentials");
126 return new OAuth2RestTemplate(resourceDetails);
129 private void setTrustStore(final RestTemplate restTemplate) {
130 SSLContext sslContext;
132 if (keyStoreResource != null) {
133 final KeyStore keystore = KeyStore.getInstance("pkcs12");
134 keystore.load(keyStoreResource.getInputStream(), keyStorePassword.toCharArray());
136 new SSLContextBuilder().loadTrustMaterial(trustStore.getURL(), trustStorePassword.toCharArray())
137 .loadKeyMaterial(keystore, keyStorePassword.toCharArray()).build();
139 sslContext = new SSLContextBuilder()
140 .loadTrustMaterial(trustStore.getURL(), trustStorePassword.toCharArray()).build();
142 logger.info("Setting truststore: {}", trustStore.getURL());
143 final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext);
144 final HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory).build();
145 final HttpComponentsClientHttpRequestFactory factory =
146 new HttpComponentsClientHttpRequestFactory(httpClient);
147 restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(factory));
148 } catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException | CertificateException
149 | IOException | UnrecoverableKeyException exception) {
150 logger.error("Error reading truststore, TLS connection to VNFM will fail.", exception);