f09c5397d6b2f3a8d2959762c3ec9901a2ff8455
[dcaegen2/services/sdk.git] /
1 /*
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
10  *
11  *       http://www.apache.org/licenses/LICENSE-2.0
12  *
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=====================================
19  */
20
21 package org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.impl;
22
23 import static org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.impl.Commons.extractFailReason;
24
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 reactor.core.publisher.Flux;
44 import reactor.core.publisher.Mono;
45
46 /**
47  * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a>
48  * @since March 2019
49  */
50 // TODO: This is a PoC. It's untested.
51 public class MessageRouterPublisherImpl implements MessageRouterPublisher {
52     private final RxHttpClient httpClient;
53     private final int maxBatchSize;
54     private final Duration maxBatchDuration;
55
56     public MessageRouterPublisherImpl(RxHttpClient httpClient, int maxBatchSize, Duration maxBatchDuration) {
57         this.httpClient = httpClient;
58         this.maxBatchSize = maxBatchSize;
59         this.maxBatchDuration = maxBatchDuration;
60     }
61
62     @Override
63     public Flux<MessageRouterPublishResponse> put(
64             MessageRouterPublishRequest request,
65             Flux<? extends JsonElement> items) {
66         return items.bufferTimeout(maxBatchSize, maxBatchDuration)
67                 .flatMap(subItems -> subItems.isEmpty() ? Mono.empty() : pushBatchToMr(request, List.ofAll(subItems)));
68     }
69
70     private Publisher<? extends MessageRouterPublishResponse> pushBatchToMr(
71             MessageRouterPublishRequest request,
72             List<JsonElement> batch) {
73         return httpClient.call(buildHttpRequest(request, asJsonBody(batch)))
74                 .map(httpResponse -> buildResponse(httpResponse, batch));
75     }
76
77     private @NotNull RequestBody asJsonBody(List<? extends JsonElement> subItems) {
78         final JsonArray elements = new JsonArray(subItems.size());
79         subItems.forEach(elements::add);
80         return RequestBody.fromJson(elements);
81     }
82
83     private @NotNull HttpRequest buildHttpRequest(MessageRouterPublishRequest request, RequestBody body) {
84         return ImmutableHttpRequest.builder()
85                 .method(HttpMethod.POST)
86                 .url(request.sinkDefinition().topicUrl())
87                 .diagnosticContext(request.diagnosticContext())
88                 .customHeaders(HashMap.of(HttpHeaders.CONTENT_TYPE, request.contentType()))
89                 .body(body)
90                 .build();
91     }
92
93     private MessageRouterPublishResponse buildResponse(
94             HttpResponse httpResponse, List<JsonElement> batch) {
95         final ImmutableMessageRouterPublishResponse.Builder builder =
96                 ImmutableMessageRouterPublishResponse.builder();
97         return httpResponse.successful()
98                 ? builder.items(batch).build()
99                 : builder.failReason(extractFailReason(httpResponse)).build();
100     }
101 }