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.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;
30 import reactor.core.publisher.Mono;
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>
36 public class DataRouterPublisher {
38 private static final Logger logger = LoggerFactory.getLogger(DataRouterPublisher.class);
39 private final AppConfig datafileAppConfig;
41 public DataRouterPublisher(AppConfig datafileAppConfig) {
42 this.datafileAppConfig = datafileAppConfig;
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
53 public Mono<ConsumerDmaapModel> execute(ConsumerDmaapModel model, long numRetries, Duration firstBackoff) {
54 logger.trace("Method called with arg {}", model);
55 DmaapProducerReactiveHttpClient dmaapProducerReactiveHttpClient = resolveClient();
58 return Mono.just(model)
60 .flatMap(dmaapProducerReactiveHttpClient::getDmaapProducerResponse)
61 .flatMap(httpStatus -> handleHttpResponse(httpStatus, model))
62 .retryBackoff(numRetries, firstBackoff);
66 private Mono<ConsumerDmaapModel> handleHttpResponse(HttpStatus response, ConsumerDmaapModel model) {
68 if (HttpUtils.isSuccessfulResponseCode(response.value())) {
69 logger.trace("Publish to DR successful!");
70 return Mono.just(model);
72 logger.warn("Publish to DR unsuccessful, response code: {}", response);
73 return Mono.error(new Exception("Publish to DR unsuccessful, response code: " + response));
78 DmaapPublisherConfiguration resolveConfiguration() {
79 return datafileAppConfig.getDmaapPublisherConfiguration();
82 DmaapProducerReactiveHttpClient resolveClient() {
83 return new DmaapProducerReactiveHttpClient(resolveConfiguration());