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.model.logging.MdcVariables;
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 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 Map<String, String> contextMap) {
55 MdcVariables.setMdcContextMap(contextMap);
56 logger.trace("Method called with arg {}", model);
57 DmaapProducerReactiveHttpClient dmaapProducerReactiveHttpClient = resolveClient();
60 return Mono.just(model)
62 .flatMap(m -> dmaapProducerReactiveHttpClient.getDmaapProducerResponse(m, contextMap))
63 .flatMap(httpStatus -> handleHttpResponse(httpStatus, model, contextMap))
64 .retryBackoff(numRetries, firstBackoff);
68 private Mono<ConsumerDmaapModel> handleHttpResponse(HttpStatus response, ConsumerDmaapModel model,
69 Map<String, String> contextMap) {
70 MdcVariables.setMdcContextMap(contextMap);
71 if (HttpUtils.isSuccessfulResponseCode(response.value())) {
72 logger.trace("Publish to DR successful!");
73 return Mono.just(model);
75 logger.warn("Publish to DR unsuccessful, response code: {}", response);
76 return Mono.error(new Exception("Publish to DR unsuccessful, response code: " + response));
81 DmaapPublisherConfiguration resolveConfiguration() {
82 return datafileAppConfig.getDmaapPublisherConfiguration();
85 DmaapProducerReactiveHttpClient resolveClient() {
86 return new DmaapProducerReactiveHttpClient(resolveConfiguration());