Merge "Drools-apps CSIT randomly fails deploying policies"
[integration/csit.git] / plans / usecases-pnf-sw-upgrade / pnf-sw-upgrade / sorch / simulator / aai-simulator / src / main / java / org / onap / so / aaisimulator / configration / ApplicationConfigration.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 package org.onap.so.aaisimulator.configration;
21
22 import java.util.ArrayList;
23 import java.util.List;
24 import javax.net.ssl.SSLContext;
25 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
26 import org.apache.http.impl.client.CloseableHttpClient;
27 import org.apache.http.impl.client.HttpClients;
28 import org.apache.http.ssl.SSLContextBuilder;
29 import org.onap.so.aaisimulator.utils.CacheName;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.beans.factory.annotation.Value;
33 import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
34 import org.springframework.cache.Cache;
35 import org.springframework.cache.CacheManager;
36 import org.springframework.cache.concurrent.ConcurrentMapCache;
37 import org.springframework.cache.support.SimpleCacheManager;
38 import org.springframework.context.annotation.Bean;
39 import org.springframework.context.annotation.Configuration;
40 import org.springframework.context.annotation.Profile;
41 import org.springframework.core.io.Resource;
42 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
43 import org.springframework.web.client.RestTemplate;
44 import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;
45
46 /**
47  * @author waqas.ikram@ericsson.com
48  *
49  */
50 @Configuration
51 public class ApplicationConfigration {
52     private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationConfigration.class);
53
54
55     @Bean
56     public Jackson2ObjectMapperBuilderCustomizer jacksonCustomizer() {
57         return (mapperBuilder) -> mapperBuilder.modulesToInstall(new JaxbAnnotationModule());
58     }
59
60     @Bean
61     public CacheManager cacheManager() {
62         final SimpleCacheManager manager = new SimpleCacheManager();
63
64         final List<Cache> caches = new ArrayList<>();
65         for (final CacheName cacheName : CacheName.values()) {
66             caches.add(getCache(cacheName.getName()));
67         }
68         manager.setCaches(caches);
69         return manager;
70     }
71
72     private Cache getCache(final String name) {
73         LOGGER.info("Creating cache with name: {}", name);
74         return new ConcurrentMapCache(name);
75     }
76
77     @Profile("!test")
78     @Bean
79     public RestTemplate restTemplate(@Value("${http.client.ssl.trust-store:#{null}}") final Resource trustStore,
80             @Value("${http.client.ssl.trust-store-password:#{null}}") final String trustStorePassword)
81             throws Exception {
82         LOGGER.info("Setting up RestTemplate .... ");
83         final RestTemplate restTemplate = new RestTemplate();
84
85         final HttpComponentsClientHttpRequestFactory factory =
86                 new HttpComponentsClientHttpRequestFactory(httpClient(trustStore, trustStorePassword));
87
88         restTemplate.setRequestFactory(factory);
89         return restTemplate;
90     }
91
92     private CloseableHttpClient httpClient(final Resource trustStore, final String trustStorePassword)
93             throws Exception {
94         LOGGER.info("Creating SSLConnectionSocketFactory with custom SSLContext and HostnameVerifier ... ");
95         return HttpClients.custom().setSSLSocketFactory(getSSLConnectionSocketFactory(trustStore, trustStorePassword))
96                 .build();
97     }
98
99     private SSLConnectionSocketFactory getSSLConnectionSocketFactory(final Resource trustStore,
100             final String trustStorePassword) throws Exception {
101         return new SSLConnectionSocketFactory(getSslContext(trustStore, trustStorePassword));
102     }
103
104     private SSLContext getSslContext(final Resource trustStore, final String trustStorePassword)
105             throws Exception, Exception {
106         return new SSLContextBuilder().loadTrustMaterial(trustStore.getURL(), trustStorePassword.toCharArray()).build();
107     }
108
109 }