c3c0047fffa72816a01d2557e5aa83dae79e6b72
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / adapter / vnfm / tasks / VnfmAdapterCreateVnfTaskConfiguration.java
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.bpmn.infrastructure.adapter.vnfm.tasks;
22
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;
31 import javax.net.ssl.SSLContext;
32 import org.apache.http.client.HttpClient;
33 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
34 import org.apache.http.impl.client.HttpClients;
35 import org.apache.http.ssl.SSLContextBuilder;
36 import org.onap.so.configuration.rest.BasicHttpHeadersProvider;
37 import org.onap.so.configuration.rest.HttpHeadersProvider;
38 import org.onap.so.rest.service.HttpRestServiceProvider;
39 import org.onap.so.rest.service.HttpRestServiceProviderImpl;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.beans.factory.annotation.Qualifier;
44 import org.springframework.beans.factory.annotation.Value;
45 import org.springframework.context.annotation.Bean;
46 import org.springframework.context.annotation.Configuration;
47 import org.springframework.core.io.Resource;
48 import org.springframework.http.client.BufferingClientHttpRequestFactory;
49 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
50 import org.springframework.web.client.RestTemplate;
51
52 /**
53  * Provides {@link org.onap.so.rest.service.VnfmAdapterServiceProvider} configuration for
54  * {@link VnfmAdapterCreateVnfTask}
55  * 
56  * @author waqas.ikram@est.tech
57  */
58 @Configuration
59 public class VnfmAdapterCreateVnfTaskConfiguration {
60
61     private static final Logger logger = LoggerFactory.getLogger(VnfmAdapterCreateVnfTaskConfiguration.class);
62
63     @Value("${rest.http.client.configuration.ssl.trustStore:#{null}}")
64     private Resource trustStore;
65
66     @Value("${rest.http.client.configuration.ssl.trustStorePassword:#{null}}")
67     private String trustStorePassword;
68
69     @Value("${rest.http.client.configuration.ssl.keyStore:#{null}}")
70     private Resource keyStoreResource;
71
72     @Value("${rest.http.client.configuration.ssl.keyStorePassword:#{null}}")
73     private String keyStorePassword;
74
75     @Bean
76     public HttpRestServiceProvider databaseHttpRestServiceProvider(
77             @Qualifier(CONFIGURABLE_REST_TEMPLATE) @Autowired final RestTemplate restTemplate,
78             @Autowired final VnfmBasicHttpConfigProvider etsiVnfmAdapter) {
79         if (trustStore != null) {
80             setTrustStore(restTemplate);
81         }
82         return getHttpRestServiceProvider(restTemplate, new BasicHttpHeadersProvider(etsiVnfmAdapter.getAuth()));
83     }
84
85     private void setTrustStore(final RestTemplate restTemplate) {
86         SSLContext sslContext;
87         try {
88             if (keyStoreResource != null) {
89                 KeyStore keystore = KeyStore.getInstance("pkcs12");
90                 keystore.load(keyStoreResource.getInputStream(), keyStorePassword.toCharArray());
91                 sslContext =
92                         new SSLContextBuilder().loadTrustMaterial(trustStore.getURL(), trustStorePassword.toCharArray())
93                                 .loadKeyMaterial(keystore, keyStorePassword.toCharArray()).build();
94             } else {
95                 sslContext = new SSLContextBuilder()
96                         .loadTrustMaterial(trustStore.getURL(), trustStorePassword.toCharArray()).build();
97             }
98             logger.info("Setting truststore: {}", trustStore.getURL());
99             final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext);
100             final HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory).build();
101             final HttpComponentsClientHttpRequestFactory factory =
102                     new HttpComponentsClientHttpRequestFactory(httpClient);
103             restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(factory));
104         } catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException | CertificateException
105                 | IOException | UnrecoverableKeyException exception) {
106             logger.error("Error reading truststore, TLS connection to VNFM will fail.", exception);
107         }
108     }
109
110     private HttpRestServiceProvider getHttpRestServiceProvider(final RestTemplate restTemplate,
111             final HttpHeadersProvider httpHeadersProvider) {
112         return new HttpRestServiceProviderImpl(restTemplate, httpHeadersProvider);
113     }
114
115 }