8ac2e3fc1cf191cdb7d97d18dbc9dfce6dfb17bb
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / config / DmiWebClientConfiguration.java
1
2 /*
3  *  ============LICENSE_START=======================================================
4  *  Copyright (C) 2024 Nordix Foundation
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.ncmp.api.impl.config;
23
24 import io.netty.channel.ChannelOption;
25 import io.netty.handler.timeout.ReadTimeoutHandler;
26 import io.netty.handler.timeout.WriteTimeoutHandler;
27 import java.util.concurrent.TimeUnit;
28 import lombok.Getter;
29 import lombok.RequiredArgsConstructor;
30 import lombok.extern.slf4j.Slf4j;
31 import org.springframework.beans.factory.annotation.Value;
32 import org.springframework.context.annotation.Bean;
33 import org.springframework.context.annotation.Configuration;
34 import org.springframework.http.HttpHeaders;
35 import org.springframework.http.MediaType;
36 import org.springframework.http.client.reactive.ReactorClientHttpConnector;
37 import org.springframework.stereotype.Component;
38 import org.springframework.web.reactive.function.client.WebClient;
39 import reactor.netty.http.client.HttpClient;
40
41 @Slf4j
42 @Configuration
43 @RequiredArgsConstructor
44 public class DmiWebClientConfiguration {
45
46     @Value("${ncmp.dmi.httpclient.connectionTimeoutInSeconds:20000}")
47     private Integer connectionTimeoutInSeconds;
48
49     @Value("${ncmp.dmi.httpclient.maximumInMemorySizeInMegabytes:1}")
50     private Integer maximumInMemorySizeInMegabytes;
51
52     @Getter
53     @Component
54     public static class DmiProperties {
55         @Value("${ncmp.dmi.auth.username}")
56         private String authUsername;
57         @Value("${ncmp.dmi.auth.password}")
58         private String authPassword;
59         @Value("${ncmp.dmi.api.base-path}")
60         private String dmiBasePath;
61         @Value("${ncmp.dmi.auth.enabled}")
62         private boolean dmiBasicAuthEnabled;
63     }
64
65     /**
66      * Configures and create a WebClient bean.
67      *
68      * @return a WebClient instance.
69      */
70     @Bean
71     public WebClient webClient() {
72         final var httpClient = HttpClient.create()
73                 .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectionTimeoutInSeconds * 1000)
74                 .doOnConnected(connection ->
75                         connection
76                                 .addHandlerLast(new ReadTimeoutHandler(connectionTimeoutInSeconds, TimeUnit.SECONDS))
77                                 .addHandlerLast(new WriteTimeoutHandler(connectionTimeoutInSeconds, TimeUnit.SECONDS)));
78
79         return WebClient.builder()
80                 .defaultHeaders(header -> header.set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE))
81                 .defaultHeaders(header -> header.set(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE))
82                 .clientConnector(new ReactorClientHttpConnector(httpClient))
83                 .codecs(configurer -> configurer
84                         .defaultCodecs()
85                         .maxInMemorySize(maximumInMemorySizeInMegabytes * 1024 * 1024))
86                 .build();
87     }
88 }