[SO] Create changes for SO-API and BPMN-INFRA to support CNF's through ASD
[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     public static final String VNFM_HTTP_REST_SERVICE_PROVIDER_BEAN = "vnfmHttpRestServiceProvider";
62
63     private static final Logger logger = LoggerFactory.getLogger(VnfmAdapterCreateVnfTaskConfiguration.class);
64
65     @Value("${rest.http.client.configuration.ssl.trustStore:#{null}}")
66     private Resource trustStore;
67
68     @Value("${rest.http.client.configuration.ssl.trustStorePassword:#{null}}")
69     private String trustStorePassword;
70
71     @Value("${rest.http.client.configuration.ssl.keyStore:#{null}}")
72     private Resource keyStoreResource;
73
74     @Value("${rest.http.client.configuration.ssl.keyStorePassword:#{null}}")
75     private String keyStorePassword;
76
77     @Bean
78     @Qualifier(VNFM_HTTP_REST_SERVICE_PROVIDER_BEAN)
79     public HttpRestServiceProvider vnfmHttpRestServiceProvider(
80             @Qualifier(CONFIGURABLE_REST_TEMPLATE) @Autowired final RestTemplate restTemplate,
81             @Autowired final VnfmBasicHttpConfigProvider etsiVnfmAdapter) {
82         if (trustStore != null) {
83             setTrustStore(restTemplate);
84         }
85         return getHttpRestServiceProvider(restTemplate, new BasicHttpHeadersProvider(etsiVnfmAdapter.getAuth()));
86     }
87
88     private void setTrustStore(final RestTemplate restTemplate) {
89         SSLContext sslContext;
90         try {
91             if (keyStoreResource != null) {
92                 KeyStore keystore = KeyStore.getInstance("pkcs12");
93                 keystore.load(keyStoreResource.getInputStream(), keyStorePassword.toCharArray());
94                 sslContext =
95                         new SSLContextBuilder().loadTrustMaterial(trustStore.getURL(), trustStorePassword.toCharArray())
96                                 .loadKeyMaterial(keystore, keyStorePassword.toCharArray()).build();
97             } else {
98                 sslContext = new SSLContextBuilder()
99                         .loadTrustMaterial(trustStore.getURL(), trustStorePassword.toCharArray()).build();
100             }
101             logger.info("Setting truststore: {}", trustStore.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(new BufferingClientHttpRequestFactory(factory));
107         } catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException | CertificateException
108                 | IOException | UnrecoverableKeyException exception) {
109             logger.error("Error reading truststore, TLS connection to VNFM will fail.", exception);
110         }
111     }
112
113     private HttpRestServiceProvider getHttpRestServiceProvider(final RestTemplate restTemplate,
114             final HttpHeadersProvider httpHeadersProvider) {
115         return new HttpRestServiceProviderImpl(restTemplate, httpHeadersProvider);
116     }
117
118 }