f9411551250740560f0b21fe0d507ea5d497dc9d
[nonrtric/plt/ranpm.git] / datafilecollector / src / main / java / org / oran / datafile / http / DfcHttpClient.java
1 /*-
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2020-2021 Nokia. All rights reserved.
4  * Copyright (C) 2023 Nordix Foundation.
5  * ===============================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
7  * in compliance with the License. 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 distributed under the License
12  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13  * or implied. See the License for the specific language governing permissions and limitations under
14  * the License.
15  * ============LICENSE_END========================================================================
16  */
17 package org.oran.datafile.http;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.nio.file.Files;
22 import java.nio.file.Path;
23 import java.util.concurrent.CountDownLatch;
24 import java.util.concurrent.atomic.AtomicReference;
25 import java.util.function.Consumer;
26
27 import org.oran.datafile.commons.FileCollectClient;
28 import org.oran.datafile.exceptions.DatafileTaskException;
29 import org.oran.datafile.exceptions.NonRetryableDatafileTaskException;
30 import org.oran.datafile.model.FileServerData;
31 import org.oran.datafile.oauth2.SecurityContext;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 import reactor.core.Disposable;
36 import reactor.core.publisher.Flux;
37 import reactor.core.publisher.Mono;
38 import reactor.netty.http.client.HttpClient;
39 import reactor.netty.http.client.HttpClientResponse;
40 import reactor.netty.resources.ConnectionProvider;
41
42 /**
43  * Gets file from PNF with HTTP protocol.
44  *
45  */
46 public class DfcHttpClient implements FileCollectClient {
47
48     // Be aware to be less than ScheduledTasks.NUMBER_OF_WORKER_THREADS
49     private static final int MAX_NUMBER_OF_CONNECTIONS = 200;
50     private static final Logger logger = LoggerFactory.getLogger(DfcHttpClient.class);
51     private static final ConnectionProvider pool = ConnectionProvider.create("default", MAX_NUMBER_OF_CONNECTIONS);
52
53     private final FileServerData fileServerData;
54     private Disposable disposableClient;
55
56     protected HttpClient client;
57     private final SecurityContext securityContext;
58
59     public DfcHttpClient(SecurityContext securityContext, FileServerData fileServerData) {
60         this.fileServerData = fileServerData;
61         this.securityContext = securityContext;
62     }
63
64     @Override
65     public void open() throws DatafileTaskException {
66         logger.trace("Setting httpClient for file download.");
67
68         final String authorizationContent = this.securityContext.getBearerAuthToken();
69         this.client = HttpClient.create(pool).keepAlive(true);
70         if (!authorizationContent.isEmpty()) {
71             this.client = this.client.headers(h -> h.add("Authorization", "Bearer " + authorizationContent));
72             logger.trace("httpClient, auth header was set.");
73         } else if (!this.fileServerData.password.isEmpty()) {
74             String basicAuthContent =
75                 HttpUtils.basicAuthContent(this.fileServerData.userId, this.fileServerData.password);
76             this.client = this.client.headers(h -> h.add("Authorization", basicAuthContent));
77         }
78     }
79
80     @Override
81     public void collectFile(String remoteFile, Path localFile) throws DatafileTaskException {
82         logger.trace("Prepare to collectFile {}", localFile);
83         CountDownLatch latch = new CountDownLatch(1);
84         final AtomicReference<Exception> errorMessage = new AtomicReference<>();
85
86         Consumer<Throwable> onError = processFailedConnectionWithServer(latch, errorMessage);
87         Consumer<InputStream> onSuccess = processDataFromServer(localFile, latch, errorMessage);
88
89         Flux<InputStream> responseContent = getServerResponse(remoteFile);
90         disposableClient = responseContent.subscribe(onSuccess, onError);
91
92         try {
93             latch.await();
94         } catch (InterruptedException e) {
95             throw new DatafileTaskException("Interrupted exception after datafile download - ", e);
96         }
97
98         if (isDownloadFailed(errorMessage)) {
99             if (errorMessage.get() instanceof NonRetryableDatafileTaskException) {
100                 throw (NonRetryableDatafileTaskException) errorMessage.get();
101             }
102             throw (DatafileTaskException) errorMessage.get();
103         }
104
105         logger.trace("HTTP collectFile OK");
106     }
107
108     protected boolean isDownloadFailed(AtomicReference<Exception> errorMessage) {
109         return (errorMessage.get() != null);
110     }
111
112     protected Consumer<Throwable> processFailedConnectionWithServer(CountDownLatch latch,
113         AtomicReference<Exception> errorMessages) {
114         return (Throwable response) -> {
115             Exception e = new Exception("Error in connection has occurred during file download", response);
116             errorMessages.set(new DatafileTaskException(response.getMessage(), e));
117             if (response instanceof NonRetryableDatafileTaskException) {
118                 errorMessages.set(new NonRetryableDatafileTaskException(response.getMessage(), e));
119             }
120             latch.countDown();
121         };
122     }
123
124     protected Consumer<InputStream> processDataFromServer(Path localFile, CountDownLatch latch,
125         AtomicReference<Exception> errorMessages) {
126         return (InputStream response) -> {
127             logger.trace("Starting to process response.");
128             try {
129                 long numBytes = Files.copy(response, localFile);
130                 logger.trace("Transmission was successful - {} bytes downloaded.", numBytes);
131                 logger.trace("CollectFile fetched: {}", localFile);
132                 response.close();
133             } catch (IOException e) {
134                 errorMessages.set(new DatafileTaskException("Error fetching file with", e));
135             } finally {
136                 latch.countDown();
137             }
138         };
139     }
140
141     protected Flux<InputStream> getServerResponse(String remoteFile) {
142         return client.get().uri(HttpUtils.prepareHttpUri(fileServerData, remoteFile))
143             .response((responseReceiver, byteBufFlux) -> {
144                 logger.trace("HTTP response status - {}", responseReceiver.status());
145                 if (isResponseOk(responseReceiver)) {
146                     return byteBufFlux.aggregate().asInputStream();
147                 }
148                 if (isErrorInConnection(responseReceiver)) {
149                     return Mono.error(new NonRetryableDatafileTaskException(
150                         HttpUtils.nonRetryableResponse(getResponseCode(responseReceiver))));
151                 }
152                 return Mono
153                     .error(new DatafileTaskException(HttpUtils.retryableResponse(getResponseCode(responseReceiver))));
154             });
155     }
156
157     protected boolean isResponseOk(HttpClientResponse httpClientResponse) {
158         return getResponseCode(httpClientResponse) == 200;
159     }
160
161     private int getResponseCode(HttpClientResponse responseReceiver) {
162         return responseReceiver.status().code();
163     }
164
165     protected boolean isErrorInConnection(HttpClientResponse httpClientResponse) {
166         return getResponseCode(httpClientResponse) >= 400;
167     }
168
169     @Override
170     public void close() {
171         logger.trace("Starting http client disposal.");
172         disposableClient.dispose();
173         logger.trace("Http client disposed.");
174     }
175 }