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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
14 * ============LICENSE_END========================================================================
17 package org.onap.dcaegen2.collectors.datafile.tasks;
19 import java.time.Duration;
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;
31 import reactor.core.publisher.Flux;
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>
37 public class DataRouterPublisher {
39 private static final Logger logger = LoggerFactory.getLogger(DataRouterPublisher.class);
40 private final Config datafileAppConfig;
42 public DataRouterPublisher(AppConfig datafileAppConfig) {
43 this.datafileAppConfig = datafileAppConfig;
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
54 public Flux<ConsumerDmaapModel> execute(ConsumerDmaapModel model, long numRetries, Duration firstBackoff) {
55 logger.trace("Method called with arg {}", model);
56 DmaapProducerReactiveHttpClient dmaapProducerReactiveHttpClient = resolveClient();
59 return Flux.just(model)
61 .flatMap(dmaapProducerReactiveHttpClient::getDmaapProducerResponse)
62 .flatMap(httpStatus -> handleHttpResponse(httpStatus, model))
63 .retryBackoff(numRetries, firstBackoff);
67 private Flux<ConsumerDmaapModel> handleHttpResponse(HttpStatus response, ConsumerDmaapModel model) {
69 if (HttpUtils.isSuccessfulResponseCode(response.value())) {
70 logger.trace("Publish to DR successful!");
71 return Flux.just(model);
73 logger.warn("Publish to DR unsuccessful, response code: " + response);
74 return Flux.error(new Exception("Publish to DR unsuccessful, response code: " + response));
79 DmaapPublisherConfiguration resolveConfiguration() {
80 return datafileAppConfig.getDmaapPublisherConfiguration();
83 DmaapProducerReactiveHttpClient resolveClient() {
84 return new DmaapProducerReactiveHttpClient(resolveConfiguration());