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.control.Option;
29 import org.jetbrains.annotations.NotNull;
30 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpHeaders;
31 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpMethod;
32 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpRequest;
33 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpResponse;
34 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.ImmutableHttpRequest;
35 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.RequestBody;
36 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.RxHttpClient;
37 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.ContentType;
38 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.api.MessageRouterPublisher;
39 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.error.ClientErrorReason;
40 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.error.ClientErrorReasonPresenter;
41 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.error.ClientErrorReasons;
42 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.ImmutableMessageRouterPublishResponse;
43 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.MessageRouterPublishRequest;
44 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.MessageRouterPublishResponse;
45 import org.reactivestreams.Publisher;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48 import reactor.core.publisher.Flux;
49 import reactor.core.publisher.Mono;
51 import java.time.Duration;
52 import java.util.stream.Collectors;
54 import static org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.impl.Commons.extractFailReason;
57 * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a>
60 public class MessageRouterPublisherImpl implements MessageRouterPublisher {
61 private final RxHttpClient httpClient;
62 private final int maxBatchSize;
63 private final Duration maxBatchDuration;
64 private final ClientErrorReasonPresenter clientErrorReasonPresenter;
66 private static final Logger LOGGER = LoggerFactory.getLogger(MessageRouterPublisherImpl.class);
68 public MessageRouterPublisherImpl(RxHttpClient httpClient, int maxBatchSize, Duration maxBatchDuration, ClientErrorReasonPresenter clientErrorReasonPresenter) {
69 this.httpClient = httpClient;
70 this.maxBatchSize = maxBatchSize;
71 this.maxBatchDuration = maxBatchDuration;
72 this.clientErrorReasonPresenter = clientErrorReasonPresenter;
76 public Flux<MessageRouterPublishResponse> put(
77 MessageRouterPublishRequest request,
78 Flux<? extends JsonElement> items) {
79 return items.bufferTimeout(maxBatchSize, maxBatchDuration)
80 .flatMap(subItems -> subItems.isEmpty() ? Mono.empty() : pushBatchToMr(request, List.ofAll(subItems)));
83 private Publisher<? extends MessageRouterPublishResponse> pushBatchToMr(
84 MessageRouterPublishRequest request,
85 List<JsonElement> batch) {
86 LOGGER.debug("Sending a batch of {} items to DMaaP MR", batch.size());
87 LOGGER.trace("The items to be sent: {}", batch);
88 return httpClient.call(buildHttpRequest(request, createBody(batch, request.contentType())))
89 .map(httpResponse -> buildResponse(httpResponse, batch))
90 .doOnError(ReadTimeoutException.class, e -> LOGGER.error("Timeout exception occurred when sending items to DMaaP MR", e))
91 .onErrorResume(ReadTimeoutException.class, e -> createErrorResponse(ClientErrorReasons.TIMEOUT));
94 private @NotNull RequestBody createBody(List<? extends JsonElement> subItems, ContentType contentType) {
95 if (contentType == ContentType.APPLICATION_JSON) {
96 final JsonArray elements = new JsonArray(subItems.size());
97 subItems.forEach(elements::add);
98 return RequestBody.fromJson(elements);
99 } else if (contentType == ContentType.TEXT_PLAIN) {
100 String messages = subItems.map(JsonElement::toString)
101 .collect(Collectors.joining("\n"));
102 return RequestBody.fromString(messages);
103 } else throw new IllegalArgumentException("Unsupported content type: " + contentType);
106 private @NotNull HttpRequest buildHttpRequest(MessageRouterPublishRequest request, RequestBody body) {
107 ImmutableHttpRequest.Builder requestBuilder = ImmutableHttpRequest.builder()
108 .method(HttpMethod.POST)
109 .url(request.sinkDefinition().topicUrl())
110 .diagnosticContext(request.diagnosticContext().withNewInvocationId())
111 .customHeaders(HashMap.of(HttpHeaders.CONTENT_TYPE, request.contentType().toString()))
114 return Option.of(request.timeoutConfig())
115 .map(timeoutConfig -> requestBuilder.timeout(timeoutConfig.getTimeout()).build())
116 .getOrElse(requestBuilder::build);
119 private MessageRouterPublishResponse buildResponse(
120 HttpResponse httpResponse, List<JsonElement> batch) {
121 final ImmutableMessageRouterPublishResponse.Builder builder =
122 ImmutableMessageRouterPublishResponse.builder();
124 return httpResponse.successful()
125 ? builder.items(batch).build()
126 : builder.failReason(extractFailReason(httpResponse)).build();
129 private Mono<MessageRouterPublishResponse> createErrorResponse(ClientErrorReason clientErrorReason) {
130 String failReason = clientErrorReasonPresenter.present(clientErrorReason);
131 return Mono.just(ImmutableMessageRouterPublishResponse.builder()
132 .failReason(failReason)