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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.ncmp.api.impl.config;
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;
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.
42 @RequiredArgsConstructor
43 public class DmiWebClientConfiguration {
45 private final HttpClientConfiguration httpClientConfiguration;
48 * Configures and create a WebClient bean for DMI data service.
50 * @return a WebClient instance for data services.
53 public WebClient dataServicesWebClient() {
54 final HttpClientConfiguration.DataServices httpClientConfiguration
55 = this.httpClientConfiguration.getDataServices();
57 final HttpClient httpClient = createHttpClient("dataConnectionPool",
58 httpClientConfiguration.getMaximumConnectionsTotal(),
59 httpClientConfiguration.getConnectionTimeoutInSeconds(),
60 httpClientConfiguration.getReadTimeoutInSeconds(),
61 httpClientConfiguration.getWriteTimeoutInSeconds());
62 return buildAndGetWebClient(httpClient, httpClientConfiguration.getMaximumInMemorySizeInMegabytes());
66 * Configures and creates a WebClient bean for DMI model service.
68 * @return a WebClient instance for model services.
71 public WebClient modelServicesWebClient() {
72 final HttpClientConfiguration.ModelServices httpClientConfiguration
73 = this.httpClientConfiguration.getModelServices();
75 final HttpClient httpClient = createHttpClient("modelConnectionPool",
76 httpClientConfiguration.getMaximumConnectionsTotal(),
77 httpClientConfiguration.getConnectionTimeoutInSeconds(),
78 httpClientConfiguration.getReadTimeoutInSeconds(),
79 httpClientConfiguration.getWriteTimeoutInSeconds());
80 return buildAndGetWebClient(httpClient, httpClientConfiguration.getMaximumInMemorySizeInMegabytes());
84 * Configures and creates a WebClient bean for DMI health service.
86 * @return a WebClient instance for health checks.
89 public WebClient healthChecksWebClient() {
90 final HttpClientConfiguration.HealthCheckServices httpClientConfiguration
91 = this.httpClientConfiguration.getHealthCheckServices();
93 final HttpClient httpClient = createHttpClient("healthConnectionPool",
94 httpClientConfiguration.getMaximumConnectionsTotal(),
95 httpClientConfiguration.getConnectionTimeoutInSeconds(),
96 httpClientConfiguration.getReadTimeoutInSeconds(),
97 httpClientConfiguration.getWriteTimeoutInSeconds());
98 return buildAndGetWebClient(httpClient, httpClientConfiguration.getMaximumInMemorySizeInMegabytes());
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);
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,
114 httpClient.warmup().block();
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();