02e153cf0317ff4273a6c765b19146a964f96f15
[dcaegen2/collectors/datafile.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.dcaegen2.collectors.datafile.tasks;
22
23 import com.google.gson.JsonElement;
24 import com.google.gson.JsonParser;
25 import java.io.File;
26 import java.io.IOException;
27 import java.net.MalformedURLException;
28 import java.net.URI;
29 import java.nio.file.Path;
30 import java.time.Duration;
31 import org.apache.http.HttpResponse;
32 import org.apache.http.client.methods.HttpPut;
33 import org.apache.http.entity.ContentType;
34 import org.apache.http.entity.FileEntity;
35 import org.onap.dcaegen2.collectors.datafile.configuration.AppConfig;
36 import org.onap.dcaegen2.collectors.datafile.configuration.PublisherConfiguration;
37 import org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException;
38 import org.onap.dcaegen2.collectors.datafile.model.Counters;
39 import org.onap.dcaegen2.collectors.datafile.model.FilePublishInformation;
40 import org.onap.dcaegen2.collectors.datafile.model.JsonSerializer;
41 import org.onap.dcaegen2.collectors.datafile.model.logging.MappedDiagnosticContext;
42 import org.onap.dcaegen2.collectors.datafile.service.HttpUtils;
43 import org.onap.dcaegen2.collectors.datafile.service.producer.DmaapProducerHttpClient;
44 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapPublisherConfiguration;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47 import org.slf4j.MDC;
48 import org.springframework.core.io.FileSystemResource;
49 import org.springframework.http.HttpHeaders;
50 import org.springframework.http.HttpStatus;
51 import org.springframework.web.util.DefaultUriBuilderFactory;
52 import reactor.core.publisher.Mono;
53
54 /**
55  * Publishes a file to the DataRouter.
56  *
57  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 4/13/18
58  * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
59  */
60 public class DataRouterPublisher {
61     private static final String X_DMAAP_DR_META = "X-DMAAP-DR-META";
62     private static final String CONTENT_TYPE = "application/octet-stream";
63
64     private static final Logger logger = LoggerFactory.getLogger(DataRouterPublisher.class);
65     private final AppConfig datafileAppConfig;
66     private final Counters counters;
67
68     public DataRouterPublisher(AppConfig datafileAppConfig, Counters counters) {
69         this.datafileAppConfig = datafileAppConfig;
70         this.counters = counters;
71     }
72
73     /**
74      * Publish one file.
75      *
76      * @param publishInfo information about the file to publish
77      * @param numRetries the maximal number of retries if the publishing fails
78      * @param firstBackoff the time to delay the first retry
79      * @return the (same) filePublishInformation
80      */
81     public Mono<FilePublishInformation> publishFile(FilePublishInformation publishInfo, long numRetries,
82             Duration firstBackoff) {
83         MDC.setContextMap(publishInfo.getContext());
84         return Mono.just(publishInfo) //
85                 .cache() //
86                 .flatMap(this::publishFile) //
87                 .flatMap(httpStatus -> handleHttpResponse(httpStatus, publishInfo)) //
88                 .retryBackoff(numRetries, firstBackoff);
89     }
90
91     private Mono<HttpStatus> publishFile(FilePublishInformation publishInfo) {
92         MDC.setContextMap(publishInfo.getContext());
93         logger.trace("Entering publishFile with {}", publishInfo);
94         try {
95             DmaapProducerHttpClient dmaapProducerHttpClient = resolveClient(publishInfo.getChangeIdentifier());
96             HttpPut put = new HttpPut();
97             prepareHead(publishInfo, put);
98             prepareBody(publishInfo, put);
99             dmaapProducerHttpClient.addUserCredentialsToHead(put);
100
101             HttpResponse response =
102                     dmaapProducerHttpClient.getDmaapProducerResponseWithRedirect(put, publishInfo.getContext());
103             logger.trace("{}", response);
104             counters.incTotalPublishedFiles();
105             return Mono.just(HttpStatus.valueOf(response.getStatusLine().getStatusCode()));
106         } catch (Exception e) {
107             counters.incNoOfFailedPublishAttempts();
108             logger.warn("Publishing file {} to DR unsuccessful.", publishInfo.getName(), e);
109             return Mono.error(e);
110         }
111     }
112
113     private void prepareHead(FilePublishInformation publishInfo, HttpPut put) throws DatafileTaskException {
114
115         put.addHeader(HttpHeaders.CONTENT_TYPE, CONTENT_TYPE);
116         JsonElement metaData = new JsonParser().parse(JsonSerializer.createJsonBodyForDataRouter(publishInfo));
117         put.addHeader(X_DMAAP_DR_META, metaData.toString());
118         URI uri = new DefaultUriBuilderFactory(
119                 datafileAppConfig.getPublisherConfiguration(publishInfo.getChangeIdentifier()).publishUrl()) //
120                         .builder() //
121                         .pathSegment(publishInfo.getName()) //
122                         .build();
123         put.setURI(uri);
124
125         MappedDiagnosticContext.appendTraceInfo(put);
126     }
127
128     private void prepareBody(FilePublishInformation publishInfo, HttpPut put) throws IOException {
129         File file = createInputFile(publishInfo.getInternalLocation());
130         FileEntity entity = new FileEntity(file, ContentType.DEFAULT_BINARY);
131         put.setEntity(entity);
132     }
133
134     private static Mono<FilePublishInformation> handleHttpResponse(HttpStatus response,
135             FilePublishInformation publishInfo) {
136         MDC.setContextMap(publishInfo.getContext());
137         if (HttpUtils.isSuccessfulResponseCode(response.value())) {
138             logger.trace("Publishing file {} to DR successful!", publishInfo.getName());
139             return Mono.just(publishInfo);
140         } else {
141             logger.warn("Publishing file {} to DR unsuccessful. Response code: {}", publishInfo.getName(), response);
142             return Mono.error(new Exception(
143                     "Publishing file " + publishInfo.getName() + " to DR unsuccessful. Response code: " + response));
144         }
145     }
146
147     File createInputFile(Path filePath) throws IOException {
148         FileSystemResource realResource = new FileSystemResource(filePath);
149         return realResource.getFile();
150     }
151
152     PublisherConfiguration resolveConfiguration(String changeIdentifer) throws DatafileTaskException {
153         return datafileAppConfig.getPublisherConfiguration(changeIdentifer);
154     }
155
156     DmaapProducerHttpClient resolveClient(String changeIdentifier) throws DatafileTaskException {
157         try {
158             DmaapPublisherConfiguration cfg = resolveConfiguration(changeIdentifier).toDmaap();
159             return new DmaapProducerHttpClient(cfg);
160         } catch (MalformedURLException e) {
161             throw new DatafileTaskException("Cannot resolve producer client", e);
162         }
163
164     }
165 }