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