2 * ============LICENSE_START====================================
3 * DCAEGEN2-SERVICES-SDK
4 * =========================================================
5 * Copyright (C) 2019-2021 Nokia. All rights reserved.
6 * =========================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=====================================
21 package org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.impl;
23 import com.google.gson.JsonArray;
24 import com.google.gson.JsonElement;
25 import io.netty.handler.timeout.ReadTimeoutException;
26 import io.vavr.collection.HashMap;
27 import io.vavr.collection.List;
28 import io.vavr.collection.Map;
29 import io.vavr.control.Option;
30 import org.jetbrains.annotations.NotNull;
31 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpHeaders;
32 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpMethod;
33 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpRequest;
34 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpResponse;
35 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.ImmutableHttpRequest;
36 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.RequestBody;
37 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.RxHttpClient;
38 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.exceptions.RetryableException;
39 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.ContentType;
40 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.api.MessageRouterPublisher;
41 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.error.ClientErrorReason;
42 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.error.ClientErrorReasonPresenter;
43 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.error.ClientErrorReasons;
44 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.ImmutableMessageRouterPublishResponse;
45 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.MessageRouterPublishRequest;
46 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.MessageRouterPublishResponse;
47 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.config.DmaapTimeoutConfig;
48 import org.reactivestreams.Publisher;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51 import reactor.core.publisher.Flux;
52 import reactor.core.publisher.Mono;
54 import java.net.ConnectException;
55 import java.time.Duration;
56 import java.util.stream.Collectors;
58 import static org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.impl.Commons.extractFailReason;
61 * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a>
64 public class MessageRouterPublisherImpl implements MessageRouterPublisher {
65 private final RxHttpClient httpClient;
66 private final int maxBatchSize;
67 private final Duration maxBatchDuration;
68 private final ClientErrorReasonPresenter clientErrorReasonPresenter;
70 private static final Logger LOGGER = LoggerFactory.getLogger(MessageRouterPublisherImpl.class);
72 public MessageRouterPublisherImpl(RxHttpClient httpClient, int maxBatchSize, Duration maxBatchDuration, ClientErrorReasonPresenter clientErrorReasonPresenter) {
73 this.httpClient = httpClient;
74 this.maxBatchSize = maxBatchSize;
75 this.maxBatchDuration = maxBatchDuration;
76 this.clientErrorReasonPresenter = clientErrorReasonPresenter;
80 public Flux<MessageRouterPublishResponse> put(
81 MessageRouterPublishRequest request,
82 Flux<? extends JsonElement> items) {
83 return items.bufferTimeout(maxBatchSize, maxBatchDuration)
84 .flatMap(subItems -> subItems.isEmpty() ? Mono.empty() : pushBatchToMr(request, List.ofAll(subItems)));
87 private Publisher<? extends MessageRouterPublishResponse> pushBatchToMr(
88 MessageRouterPublishRequest request,
89 List<JsonElement> batch) {
90 LOGGER.debug("Sending a batch of {} items to DMaaP MR", batch.size());
91 LOGGER.trace("The items to be sent: {}", batch);
92 return httpClient.call(buildHttpRequest(request, createBody(batch, request.contentType())))
93 .map(httpResponse -> buildResponse(httpResponse, batch))
94 .doOnError(ReadTimeoutException.class,
95 e -> LOGGER.error("Timeout exception occurred when sending items to DMaaP MR", e))
96 .onErrorResume(ReadTimeoutException.class, e -> buildErrorResponse(ClientErrorReasons.TIMEOUT))
97 .doOnError(ConnectException.class, e -> LOGGER.error("DMaaP MR is unavailable, {}", e.getMessage()))
98 .onErrorResume(ConnectException.class, e -> buildErrorResponse(ClientErrorReasons.SERVICE_UNAVAILABLE))
99 .onErrorResume(RetryableException.class, e -> Mono.just(buildResponse(e.getResponse(), batch)));
102 private @NotNull RequestBody createBody(List<? extends JsonElement> subItems, ContentType contentType) {
103 if (contentType == ContentType.APPLICATION_JSON) {
104 final JsonArray elements = new JsonArray(subItems.size());
105 subItems.forEach(elements::add);
106 return RequestBody.fromJson(elements);
107 } else if (contentType == ContentType.TEXT_PLAIN) {
108 String messages = subItems.map(JsonElement::toString)
109 .collect(Collectors.joining("\n"));
110 return RequestBody.fromString(messages);
111 } else throw new IllegalArgumentException("Unsupported content type: " + contentType);
114 private @NotNull HttpRequest buildHttpRequest(MessageRouterPublishRequest request, RequestBody body) {
115 return ImmutableHttpRequest.builder()
116 .method(HttpMethod.POST)
117 .url(request.sinkDefinition().topicUrl())
118 .diagnosticContext(request.diagnosticContext().withNewInvocationId())
119 .customHeaders(headers(request))
121 .timeout(timeout(request).getOrNull())
125 private MessageRouterPublishResponse buildResponse(
126 HttpResponse httpResponse, List<JsonElement> batch) {
127 final ImmutableMessageRouterPublishResponse.Builder builder =
128 ImmutableMessageRouterPublishResponse.builder();
130 return httpResponse.successful()
131 ? builder.items(batch).build()
132 : builder.failReason(extractFailReason(httpResponse)).build();
135 private Mono<MessageRouterPublishResponse> buildErrorResponse(ClientErrorReason clientErrorReason) {
136 String failReason = clientErrorReasonPresenter.present(clientErrorReason);
137 return Mono.just(ImmutableMessageRouterPublishResponse.builder()
138 .failReason(failReason)
142 private Option<Duration> timeout(MessageRouterPublishRequest request) {
143 return Option.of(request.timeoutConfig())
144 .map(DmaapTimeoutConfig::getTimeout);
147 private Map<String, String> headers(MessageRouterPublishRequest request) {
148 Map<String, String> headers = Option.of(request.sinkDefinition().aafCredentials())
149 .map(Commons::basicAuthHeader)
151 .getOrElse(HashMap.empty());
152 return headers.put(HttpHeaders.CONTENT_TYPE, request.contentType().toString());