b65ddd63e220cb70f439908de3989a941f41ad4b
[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.configuration.Config;
23 import org.onap.dcaegen2.collectors.datafile.model.ConsumerDmaapModel;
24 import org.onap.dcaegen2.collectors.datafile.service.HttpUtils;
25 import org.onap.dcaegen2.collectors.datafile.service.producer.DmaapProducerReactiveHttpClient;
26 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapPublisherConfiguration;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.springframework.http.HttpStatus;
30
31 import reactor.core.publisher.Flux;
32
33 /**
34  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 4/13/18
35  * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
36  */
37 public class DataRouterPublisher {
38
39     private static final Logger logger = LoggerFactory.getLogger(DataRouterPublisher.class);
40     private final Config datafileAppConfig;
41
42     public DataRouterPublisher(AppConfig datafileAppConfig) {
43         this.datafileAppConfig = datafileAppConfig;
44     }
45
46
47     /**
48      * Publish one file
49      * @param consumerDmaapModel information about the file to publish
50      * @param maxNumberOfRetries the maximal number of retries if the publishing fails
51      * @param firstBackoffTimeout the time to delay the first retry
52      * @return the HTTP response status as a string
53      */
54     public Flux<ConsumerDmaapModel> execute(ConsumerDmaapModel model, long numRetries, Duration firstBackoff) {
55         logger.trace("Method called with arg {}", model);
56         DmaapProducerReactiveHttpClient dmaapProducerReactiveHttpClient = resolveClient();
57
58         //@formatter:off
59         return Flux.just(model)
60                 .cache(1)
61                 .flatMap(dmaapProducerReactiveHttpClient::getDmaapProducerResponse)
62                 .flatMap(httpStatus -> handleHttpResponse(httpStatus, model))
63                 .retryBackoff(numRetries, firstBackoff);
64         //@formatter:on
65     }
66
67     private Flux<ConsumerDmaapModel> handleHttpResponse(HttpStatus response, ConsumerDmaapModel model) {
68
69         if (HttpUtils.isSuccessfulResponseCode(response.value())) {
70             logger.trace("Publish to DR successful!");
71             return Flux.just(model);
72         } else {
73             logger.warn("Publish to DR unsuccessful, response code: " + response);
74             return Flux.error(new Exception("Publish to DR unsuccessful, response code: " + response));
75         }
76     }
77
78
79     DmaapPublisherConfiguration resolveConfiguration() {
80         return datafileAppConfig.getDmaapPublisherConfiguration();
81     }
82
83     DmaapProducerReactiveHttpClient resolveClient() {
84         return new DmaapProducerReactiveHttpClient(resolveConfiguration());
85     }
86 }