Update vnfm simulator - subscribe and notify
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / vnfm-simulator / vnfm-service / src / main / java / org / onap / so / svnfm / simulator / config / SslBasedRestTemplateConfiguration.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 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 package org.onap.so.svnfm.simulator.config;
21
22 import com.google.gson.Gson;
23 import org.apache.http.client.HttpClient;
24 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
25 import org.apache.http.impl.client.HttpClientBuilder;
26 import org.apache.http.impl.client.HttpClients;
27 import org.apache.http.ssl.SSLContextBuilder;
28 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.JSON;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.beans.factory.annotation.Qualifier;
32 import org.springframework.beans.factory.annotation.Value;
33 import org.springframework.context.annotation.Bean;
34 import org.springframework.context.annotation.Configuration;
35 import org.springframework.core.io.Resource;
36 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
37 import org.springframework.http.converter.HttpMessageConverter;
38 import org.springframework.http.converter.json.GsonHttpMessageConverter;
39 import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
40 import org.springframework.web.client.RestTemplate;
41 import java.security.KeyStore;
42 import java.util.Iterator;
43
44 /**
45  * @author Waqas Ikram (waqas.ikram@est.tech)
46  * @author Andrew Lamb (andrew.a.lamb@est.tech)
47  */
48 @Configuration public class SslBasedRestTemplateConfiguration {
49
50     private static final Logger logger = LoggerFactory.getLogger(SslBasedRestTemplateConfiguration.class);
51
52     public static final String SSL_BASED_CONFIGURABLE_REST_TEMPLATE = "sslBasedConfigurableRestTemplate";
53
54     @Value("${http.client.ssl.trust-store:#{null}}")
55     private Resource trustStore;
56     @Value("${http.client.ssl.trust-store-password:#{null}}")
57     private String trustStorePassword;
58
59     @Value("${server.ssl.key-store:#{null}}")
60     private Resource keyStoreResource;
61     @Value("${server.ssl.key--store-password:#{null}}")
62     private String keyStorePassword;
63
64     @Bean
65     @Qualifier(SSL_BASED_CONFIGURABLE_REST_TEMPLATE)
66     public RestTemplate sslBasedrestTemplate() throws Exception {
67         logger.info("Configuring {} ...", this.getClass().getCanonicalName());
68         final RestTemplate restTemplate = new RestTemplate();
69         final HttpClientBuilder builder = HttpClients.custom();
70
71         if (keyStoreResource != null && trustStore != null) {
72             logger.info("Setting key-store: {}", keyStoreResource.getURL());
73             logger.info("Setting key-store-password: {}", keyStorePassword);
74             final KeyStore keystore = KeyStore.getInstance("pkcs12");
75             keystore.load(keyStoreResource.getInputStream(), keyStorePassword.toCharArray());
76
77             logger.info("Setting client trust-store: {}", trustStore.getURL());
78             logger.info("Setting client trust-store-password: {}", trustStorePassword);
79             final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
80                     new SSLContextBuilder().loadTrustMaterial(trustStore.getURL(), trustStorePassword.toCharArray())
81                             .loadKeyMaterial(keystore, keyStorePassword.toCharArray()).build());
82             builder.setSSLSocketFactory(socketFactory);
83         }
84
85         final HttpClient httpClient = builder.build();
86         final HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);
87         restTemplate.setRequestFactory(factory);
88         setGsonMessageConverter(restTemplate);
89         return restTemplate;
90     }
91
92     public void setGsonMessageConverter(final RestTemplate restTemplate) {
93         logger.info("Setting GsonMessageConverter ...");
94         final Iterator<HttpMessageConverter<?>> iterator = restTemplate.getMessageConverters().iterator();
95         while (iterator.hasNext()) {
96             if (iterator.next() instanceof MappingJackson2HttpMessageConverter) {
97                 iterator.remove();
98             }
99         }
100         final Gson gson = new JSON().getGson();
101         restTemplate.getMessageConverters().add(new GsonHttpMessageConverter(gson));
102         logger.info("Finished setting GsonMessageConverter ...");
103     }
104
105 }