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