Replace RestTemplate with WebClient in synchronous DMI calls
[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     @Getter
50     @Component
51     public static class DmiProperties {
52         @Value("${ncmp.dmi.auth.username}")
53         private String authUsername;
54         @Value("${ncmp.dmi.auth.password}")
55         private String authPassword;
56         @Value("${ncmp.dmi.api.base-path}")
57         private String dmiBasePath;
58         @Value("${ncmp.dmi.auth.enabled}")
59         private boolean dmiBasicAuthEnabled;
60     }
61
62     /**
63      * Configures and create a WebClient bean.
64      *
65      * @return a WebClient instance.
66      */
67     @Bean
68     public WebClient webClient() {
69         final var httpClient = HttpClient.create()
70                 .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectionTimeoutInSeconds * 1000)
71                 .doOnConnected(connection ->
72                         connection
73                                 .addHandlerLast(new ReadTimeoutHandler(connectionTimeoutInSeconds, TimeUnit.SECONDS))
74                                 .addHandlerLast(new WriteTimeoutHandler(connectionTimeoutInSeconds, TimeUnit.SECONDS)));
75
76         return WebClient.builder()
77                 .defaultHeaders(header -> header.set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE))
78                 .defaultHeaders(header -> header.set(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE))
79                 .clientConnector(new ReactorClientHttpConnector(httpClient))
80                 .build();
81     }
82 }