2 * ============LICENSE_START====================================
3 * DCAEGEN2-SERVICES-SDK
4 * =========================================================
5 * Copyright (C) 2019 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 static org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.impl.Commons.extractFailReason;
25 import com.google.gson.JsonArray;
26 import com.google.gson.JsonElement;
27 import io.vavr.collection.HashMap;
28 import io.vavr.collection.List;
29 import java.time.Duration;
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.dmaap.client.api.MessageRouterPublisher;
39 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.ImmutableMessageRouterPublishResponse;
40 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.MessageRouterPublishRequest;
41 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.MessageRouterPublishResponse;
42 import org.reactivestreams.Publisher;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import reactor.core.publisher.Flux;
46 import reactor.core.publisher.Mono;
49 * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a>
52 public class MessageRouterPublisherImpl implements MessageRouterPublisher {
53 private final RxHttpClient httpClient;
54 private final int maxBatchSize;
55 private final Duration maxBatchDuration;
56 private static final Logger LOGGER = LoggerFactory.getLogger(MessageRouterPublisherImpl.class);
58 public MessageRouterPublisherImpl(RxHttpClient httpClient, int maxBatchSize, Duration maxBatchDuration) {
59 this.httpClient = httpClient;
60 this.maxBatchSize = maxBatchSize;
61 this.maxBatchDuration = maxBatchDuration;
65 public Flux<MessageRouterPublishResponse> put(
66 MessageRouterPublishRequest request,
67 Flux<? extends JsonElement> items) {
68 return items.bufferTimeout(maxBatchSize, maxBatchDuration)
69 .flatMap(subItems -> subItems.isEmpty() ? Mono.empty() : pushBatchToMr(request, List.ofAll(subItems)));
72 private Publisher<? extends MessageRouterPublishResponse> pushBatchToMr(
73 MessageRouterPublishRequest request,
74 List<JsonElement> batch) {
75 LOGGER.debug("Sending a batch of {} items to DMaaP MR", batch.size());
76 LOGGER.trace("The items to be sent: {}", batch);
77 return httpClient.call(buildHttpRequest(request, asJsonBody(batch)))
78 .map(httpResponse -> buildResponse(httpResponse, batch));
81 private @NotNull RequestBody asJsonBody(List<? extends JsonElement> subItems) {
82 final JsonArray elements = new JsonArray(subItems.size());
83 subItems.forEach(elements::add);
84 return RequestBody.fromJson(elements);
87 private @NotNull HttpRequest buildHttpRequest(MessageRouterPublishRequest request, RequestBody body) {
88 return ImmutableHttpRequest.builder()
89 .method(HttpMethod.POST)
90 .url(request.sinkDefinition().topicUrl())
91 .diagnosticContext(request.diagnosticContext().withNewInvocationId())
92 .customHeaders(HashMap.of(HttpHeaders.CONTENT_TYPE, request.contentType()))
97 private MessageRouterPublishResponse buildResponse(
98 HttpResponse httpResponse, List<JsonElement> batch) {
99 final ImmutableMessageRouterPublishResponse.Builder builder =
100 ImmutableMessageRouterPublishResponse.builder();
101 return httpResponse.successful()
102 ? builder.items(batch).build()
103 : builder.failReason(extractFailReason(httpResponse)).build();