Merge "[k6] Refactor k6 tests for CM handle searches"
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / config / DmiWebClientConfiguration.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2024 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.cps.ncmp.api.impl.config;
22
23 import io.netty.channel.ChannelOption;
24 import io.netty.handler.timeout.ReadTimeoutHandler;
25 import io.netty.handler.timeout.WriteTimeoutHandler;
26 import java.util.concurrent.TimeUnit;
27 import lombok.RequiredArgsConstructor;
28 import org.springframework.context.annotation.Bean;
29 import org.springframework.context.annotation.Configuration;
30 import org.springframework.http.HttpHeaders;
31 import org.springframework.http.MediaType;
32 import org.springframework.http.client.reactive.ReactorClientHttpConnector;
33 import org.springframework.web.reactive.function.client.WebClient;
34 import reactor.netty.http.client.HttpClient;
35 import reactor.netty.resources.ConnectionProvider;
36
37 /**
38  * Configures and creates a WebClient bean that triggers an initialization (warmup) of the host name resolver and
39  * loads the necessary native libraries to avoid the extra time needed to load resources for first request.
40  */
41 @Configuration
42 @RequiredArgsConstructor
43 public class DmiWebClientConfiguration {
44
45     private final HttpClientConfiguration httpClientConfiguration;
46
47     /**
48      * Configures and create a WebClient bean for DMI data service.
49      *
50      * @return a WebClient instance for data services.
51      */
52     @Bean
53     public WebClient dataServicesWebClient() {
54         final HttpClientConfiguration.DataServices httpClientConfiguration
55                 = this.httpClientConfiguration.getDataServices();
56
57         final HttpClient httpClient = createHttpClient("dataConnectionPool",
58                 httpClientConfiguration.getMaximumConnectionsTotal(),
59                 httpClientConfiguration.getConnectionTimeoutInSeconds(),
60                 httpClientConfiguration.getReadTimeoutInSeconds(),
61                 httpClientConfiguration.getWriteTimeoutInSeconds());
62         return buildAndGetWebClient(httpClient, httpClientConfiguration.getMaximumInMemorySizeInMegabytes());
63     }
64
65     /**
66      * Configures and creates a WebClient bean for DMI model service.
67      *
68      * @return a WebClient instance for model services.
69      */
70     @Bean
71     public WebClient modelServicesWebClient() {
72         final HttpClientConfiguration.ModelServices httpClientConfiguration
73                 = this.httpClientConfiguration.getModelServices();
74
75         final HttpClient httpClient = createHttpClient("modelConnectionPool",
76                 httpClientConfiguration.getMaximumConnectionsTotal(),
77                 httpClientConfiguration.getConnectionTimeoutInSeconds(),
78                 httpClientConfiguration.getReadTimeoutInSeconds(),
79                 httpClientConfiguration.getWriteTimeoutInSeconds());
80         return buildAndGetWebClient(httpClient, httpClientConfiguration.getMaximumInMemorySizeInMegabytes());
81     }
82
83     /**
84      * Configures and creates a WebClient bean for DMI health service.
85      *
86      * @return a WebClient instance for health checks.
87      */
88     @Bean
89     public WebClient healthChecksWebClient() {
90         final HttpClientConfiguration.HealthCheckServices httpClientConfiguration
91                 = this.httpClientConfiguration.getHealthCheckServices();
92
93         final HttpClient httpClient = createHttpClient("healthConnectionPool",
94                 httpClientConfiguration.getMaximumConnectionsTotal(),
95                 httpClientConfiguration.getConnectionTimeoutInSeconds(),
96                 httpClientConfiguration.getReadTimeoutInSeconds(),
97                 httpClientConfiguration.getWriteTimeoutInSeconds());
98         return buildAndGetWebClient(httpClient, httpClientConfiguration.getMaximumInMemorySizeInMegabytes());
99     }
100
101     private static HttpClient createHttpClient(final String connectionProviderName,
102                                                final Integer maximumConnectionsTotal,
103                                                final Integer connectionTimeoutInSeconds,
104                                                final Integer readTimeoutInSeconds,
105                                                final Integer writeTimeoutInSeconds) {
106         final ConnectionProvider dmiWebClientConnectionProvider = ConnectionProvider.create(connectionProviderName,
107                 maximumConnectionsTotal);
108
109         final HttpClient httpClient = HttpClient.create(dmiWebClientConnectionProvider)
110                 .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectionTimeoutInSeconds * 1000)
111                 .doOnConnected(connection -> connection.addHandlerLast(new ReadTimeoutHandler(readTimeoutInSeconds,
112                         TimeUnit.SECONDS)).addHandlerLast(new WriteTimeoutHandler(writeTimeoutInSeconds,
113                         TimeUnit.SECONDS)));
114         httpClient.warmup().block();
115         return httpClient;
116     }
117
118     private static WebClient buildAndGetWebClient(final HttpClient httpClient,
119                                                   final Integer maximumInMemorySizeInMegabytes) {
120         return WebClient.builder()
121                 .defaultHeaders(header -> header.set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE))
122                 .defaultHeaders(header -> header.set(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE))
123                 .clientConnector(new ReactorClientHttpConnector(httpClient))
124                 .codecs(configurer -> configurer.defaultCodecs().maxInMemorySize(
125                         maximumInMemorySizeInMegabytes * 1024 * 1024)).build();
126     }
127 }