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;
28 import lombok.RequiredArgsConstructor;
29 import org.springframework.beans.factory.annotation.Value;
30 import org.springframework.context.annotation.Bean;
31 import org.springframework.context.annotation.Configuration;
32 import org.springframework.http.HttpHeaders;
33 import org.springframework.http.MediaType;
34 import org.springframework.http.client.reactive.ReactorClientHttpConnector;
35 import org.springframework.stereotype.Component;
36 import org.springframework.web.reactive.function.client.WebClient;
37 import reactor.netty.http.client.HttpClient;
38 import reactor.netty.resources.ConnectionProvider;
41 @RequiredArgsConstructor
42 public class DmiWebClientConfiguration {
44 @Value("${ncmp.dmi.httpclient.connectionTimeoutInSeconds:20000}")
45 private Integer connectionTimeoutInSeconds;
47 @Value("${ncmp.dmi.httpclient.maximumInMemorySizeInMegabytes:1}")
48 private Integer maximumInMemorySizeInMegabytes;
50 @Value("${ncmp.dmi.httpclient.maximumConnectionsTotal:100}")
51 private Integer maximumConnectionsTotal;
55 public static class DmiProperties {
56 @Value("${ncmp.dmi.auth.username}")
57 private String authUsername;
58 @Value("${ncmp.dmi.auth.password}")
59 private String authPassword;
60 @Value("${ncmp.dmi.api.base-path}")
61 private String dmiBasePath;
62 @Value("${ncmp.dmi.auth.enabled}")
63 private boolean dmiBasicAuthEnabled;
67 * Configures and create a WebClient bean.
69 * @return a WebClient instance.
72 public WebClient webClient() {
74 final ConnectionProvider dmiWebClientConnectionProvider
75 = ConnectionProvider.create("dmiWebClientConnectionPool", maximumConnectionsTotal);
77 final HttpClient httpClient = HttpClient.create(dmiWebClientConnectionProvider)
78 .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectionTimeoutInSeconds * 1000)
79 .doOnConnected(connection ->
81 .addHandlerLast(new ReadTimeoutHandler(connectionTimeoutInSeconds, TimeUnit.SECONDS))
82 .addHandlerLast(new WriteTimeoutHandler(connectionTimeoutInSeconds, TimeUnit.SECONDS)));
84 return WebClient.builder()
85 .defaultHeaders(header -> header.set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE))
86 .defaultHeaders(header -> header.set(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE))
87 .clientConnector(new ReactorClientHttpConnector(httpClient))
88 .codecs(configurer -> configurer
90 .maxInMemorySize(maximumInMemorySizeInMegabytes * 1024 * 1024))