0567b1b4e9b76ef7e257a60b0ab017b6b1dfb37a
[dcaegen2/services/sdk.git] /
1 /*
2  * ============LICENSE_START====================================
3  * DCAEGEN2-SERVICES-SDK
4  * =========================================================
5  * Copyright (C) 2019-2021 Nokia. All rights reserved.
6  * =========================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *       http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=====================================
19  */
20
21 package org.onap.dcaegen2.services.sdk.rest.services.adapters.http;
22
23 import io.netty.handler.ssl.SslContext;
24 import io.vavr.control.Option;
25 import org.jetbrains.annotations.NotNull;
26 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.config.ConnectionPoolConfig;
27 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.config.RxHttpClientConfig;
28 import org.onap.dcaegen2.services.sdk.security.ssl.SecurityKeys;
29 import org.onap.dcaegen2.services.sdk.security.ssl.SslFactory;
30 import org.onap.dcaegen2.services.sdk.security.ssl.TrustStoreKeys;
31 import reactor.netty.http.client.HttpClient;
32 import reactor.netty.resources.ConnectionProvider;
33
34 /**
35  * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a>
36  * @since May 2019
37  */
38 public final class RxHttpClientFactory {
39
40     private static final SslFactory SSL_FACTORY = new SslFactory();
41
42     private RxHttpClientFactory() {
43     }
44
45     public static RxHttpClient create() {
46         return new RxHttpClient(HttpClient.create());
47     }
48
49     public static RxHttpClient create(RxHttpClientConfig config){
50         return Option.of(config.connectionPool())
51                 .map(RxHttpClientFactory::createConnectionProvider)
52                 .map(provider -> createWithConfig(HttpClient.create(provider), config))
53                 .getOrElse(createWithConfig(HttpClient.create(), config));
54     }
55
56     public static RxHttpClient create(SecurityKeys securityKeys) {
57         final SslContext context = SSL_FACTORY.createSecureClientContext(securityKeys);
58         return create(context);
59     }
60
61     public static RxHttpClient create(SecurityKeys securityKeys, RxHttpClientConfig config) {
62         final SslContext context = SSL_FACTORY.createSecureClientContext(securityKeys);
63         return create(context, config);
64     }
65
66     public static RxHttpClient create(TrustStoreKeys trustStoreKeys) {
67         final SslContext context = SSL_FACTORY.createSecureClientContext(trustStoreKeys);
68         return create(context);
69     }
70
71     public static RxHttpClient create(TrustStoreKeys trustStoreKeys, RxHttpClientConfig config) {
72         final SslContext context = SSL_FACTORY.createSecureClientContext(trustStoreKeys);
73         return create(context, config);
74     }
75
76     public static RxHttpClient createInsecure() {
77         final SslContext context = SSL_FACTORY.createInsecureClientContext();
78         return create(context);
79     }
80
81     public static RxHttpClient createInsecure(RxHttpClientConfig config) {
82         final SslContext context = SSL_FACTORY.createInsecureClientContext();
83         return create(context, config);
84     }
85
86     private static RxHttpClient create(@NotNull SslContext sslContext) {
87         HttpClient secure = HttpClient.create().secure(sslContextSpec -> sslContextSpec.sslContext(sslContext));
88         return new RxHttpClient(secure);
89     }
90
91     private static RxHttpClient create(@NotNull SslContext sslContext, RxHttpClientConfig config) {
92         HttpClient secure = HttpClient.create().secure(sslContextSpec -> sslContextSpec.sslContext(sslContext));
93         return createWithConfig(secure, config);
94     }
95
96     private static RxHttpClient createWithConfig(HttpClient httpClient, RxHttpClientConfig config) {
97         return Option.of(config.retryConfig())
98                 .map(retryConfig -> new RxHttpClient(httpClient, retryConfig))
99                 .getOrElse(() -> new RxHttpClient(httpClient));
100     }
101
102     @NotNull
103     private static ConnectionProvider createConnectionProvider(ConnectionPoolConfig connectionPoolConfig) {
104         return ConnectionProvider.builder("fixed")
105                 .maxConnections(connectionPoolConfig.connectionPool())
106                 .maxIdleTime(connectionPoolConfig.maxIdleTime())
107                 .maxLifeTime(connectionPoolConfig.maxLifeTime())
108                 .build();
109     }
110 }