338c8323c0acea05bfd69f072ace4ce67a04069e
[dcaegen2/collectors/datafile.git] /
1 /*
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2019 Nordix Foundation. All rights reserved.
4  * ===============================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
6  * in compliance with the License. You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software distributed under the License
11  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12  * or implied. See the License for the specific language governing permissions and limitations under
13  * the License.
14  * ============LICENSE_END========================================================================
15  */
16
17 package org.onap.dcaegen2.collectors.datafile.tasks;
18
19 import java.time.Duration;
20
21 import org.onap.dcaegen2.collectors.datafile.configuration.AppConfig;
22 import org.onap.dcaegen2.collectors.datafile.model.ConsumerDmaapModel;
23 import org.onap.dcaegen2.collectors.datafile.service.HttpUtils;
24 import org.onap.dcaegen2.collectors.datafile.service.producer.DmaapProducerReactiveHttpClient;
25 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapPublisherConfiguration;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.springframework.http.HttpStatus;
29
30 import reactor.core.publisher.Flux;
31
32 /**
33  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 4/13/18
34  * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
35  */
36 public class DataRouterPublisher {
37
38     private static final Logger logger = LoggerFactory.getLogger(DataRouterPublisher.class);
39     private final AppConfig datafileAppConfig;
40
41     public DataRouterPublisher(AppConfig datafileAppConfig) {
42         this.datafileAppConfig = datafileAppConfig;
43     }
44
45
46     /**
47      * Publish one file
48      * @param consumerDmaapModel information about the file to publish
49      * @param maxNumberOfRetries the maximal number of retries if the publishing fails
50      * @param firstBackoffTimeout the time to delay the first retry
51      * @return the HTTP response status as a string
52      */
53     public Flux<ConsumerDmaapModel> execute(ConsumerDmaapModel model, long numRetries, Duration firstBackoff) {
54         logger.trace("Method called with arg {}", model);
55         DmaapProducerReactiveHttpClient dmaapProducerReactiveHttpClient = resolveClient();
56
57         //@formatter:off
58         return Flux.just(model)
59                 .cache(1)
60                 .flatMap(dmaapProducerReactiveHttpClient::getDmaapProducerResponse)
61                 .flatMap(httpStatus -> handleHttpResponse(httpStatus, model))
62                 .retryBackoff(numRetries, firstBackoff);
63         //@formatter:on
64     }
65
66     private Flux<ConsumerDmaapModel> handleHttpResponse(HttpStatus response, ConsumerDmaapModel model) {
67
68         if (HttpUtils.isSuccessfulResponseCode(response.value())) {
69             logger.trace("Publish to DR successful!");
70             return Flux.just(model);
71         } else {
72             logger.warn("Publish to DR unsuccessful, response code: " + response);
73             return Flux.error(new Exception("Publish to DR unsuccessful, response code: " + response));
74         }
75     }
76
77
78     DmaapPublisherConfiguration resolveConfiguration() {
79         return datafileAppConfig.getDmaapPublisherConfiguration();
80     }
81
82     DmaapProducerReactiveHttpClient resolveClient() {
83         return new DmaapProducerReactiveHttpClient(resolveConfiguration());
84     }
85 }