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;
53 import reactor.netty.internal.shaded.reactor.pool.PoolAcquirePendingLimitException;
55 import java.net.ConnectException;
56 import java.time.Duration;
57 import java.util.stream.Collectors;
59 import static org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.impl.Commons.extractFailReason;
62 * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a>
65 public class MessageRouterPublisherImpl implements MessageRouterPublisher {
66 private final RxHttpClient httpClient;
67 private final int maxBatchSize;
68 private final Duration maxBatchDuration;
69 private final ClientErrorReasonPresenter clientErrorReasonPresenter;
71 private static final Logger LOGGER = LoggerFactory.getLogger(MessageRouterPublisherImpl.class);
73 public MessageRouterPublisherImpl(RxHttpClient httpClient, int maxBatchSize, Duration maxBatchDuration, ClientErrorReasonPresenter clientErrorReasonPresenter) {
74 this.httpClient = httpClient;
75 this.maxBatchSize = maxBatchSize;
76 this.maxBatchDuration = maxBatchDuration;
77 this.clientErrorReasonPresenter = clientErrorReasonPresenter;
81 public Flux<MessageRouterPublishResponse> put(
82 MessageRouterPublishRequest request,
83 Flux<? extends JsonElement> items) {
84 return items.bufferTimeout(maxBatchSize, maxBatchDuration)
85 .flatMap(subItems -> subItems.isEmpty() ? Mono.empty() : pushBatchToMr(request, List.ofAll(subItems)));
88 private Publisher<? extends MessageRouterPublishResponse> pushBatchToMr(
89 MessageRouterPublishRequest request,
90 List<JsonElement> batch) {
91 LOGGER.debug("Sending a batch of {} items to DMaaP MR", batch.size());
92 LOGGER.trace("The items to be sent: {}", batch);
93 return httpClient.call(buildHttpRequest(request, createBody(batch, request.contentType())))
94 .map(httpResponse -> buildResponse(httpResponse, batch))
95 .doOnError(ReadTimeoutException.class,
96 e -> LOGGER.error("Timeout exception occurred when sending items to DMaaP MR", e))
97 .onErrorResume(ReadTimeoutException.class, e -> buildErrorResponse(ClientErrorReasons.TIMEOUT))
98 .doOnError(ConnectException.class, e -> LOGGER.error("DMaaP MR is unavailable, {}", e.getMessage()))
99 .onErrorResume(PoolAcquirePendingLimitException.class, e -> buildErrorResponse(ClientErrorReasons.CONNECTION_POLL_LIMIT))
100 .onErrorResume(ConnectException.class, e -> buildErrorResponse(ClientErrorReasons.SERVICE_UNAVAILABLE))
101 .onErrorResume(RetryableException.class, e -> Mono.just(buildResponse(e.getResponse(), batch)));
104 private @NotNull RequestBody createBody(List<? extends JsonElement> subItems, ContentType contentType) {
105 if (contentType == ContentType.APPLICATION_JSON) {
106 final JsonArray elements = new JsonArray(subItems.size());
107 subItems.forEach(elements::add);
108 return RequestBody.fromJson(elements);
109 } else if (contentType == ContentType.TEXT_PLAIN) {
110 String messages = subItems.map(JsonElement::toString)
111 .collect(Collectors.joining("\n"));
112 return RequestBody.fromString(messages);
113 } else throw new IllegalArgumentException("Unsupported content type: " + contentType);
116 private @NotNull HttpRequest buildHttpRequest(MessageRouterPublishRequest request, RequestBody body) {
117 return ImmutableHttpRequest.builder()
118 .method(HttpMethod.POST)
119 .url(request.sinkDefinition().topicUrl())
120 .diagnosticContext(request.diagnosticContext().withNewInvocationId())
121 .customHeaders(headers(request))
123 .timeout(timeout(request).getOrNull())
127 private MessageRouterPublishResponse buildResponse(
128 HttpResponse httpResponse, List<JsonElement> batch) {
129 final ImmutableMessageRouterPublishResponse.Builder builder =
130 ImmutableMessageRouterPublishResponse.builder();
132 return httpResponse.successful()
133 ? builder.items(batch).build()
134 : builder.failReason(extractFailReason(httpResponse)).build();
137 private Mono<MessageRouterPublishResponse> buildErrorResponse(ClientErrorReason clientErrorReason) {
138 String failReason = clientErrorReasonPresenter.present(clientErrorReason);
139 return Mono.just(ImmutableMessageRouterPublishResponse.builder()
140 .failReason(failReason)
144 private Option<Duration> timeout(MessageRouterPublishRequest request) {
145 return Option.of(request.timeoutConfig())
146 .map(DmaapTimeoutConfig::getTimeout);
149 private Map<String, String> headers(MessageRouterPublishRequest request) {
150 Map<String, String> headers = Option.of(request.sinkDefinition().aafCredentials())
151 .map(Commons::basicAuthHeader)
153 .getOrElse(HashMap.empty());
154 return headers.put(HttpHeaders.CONTENT_TYPE, request.contentType().toString());