diff options
Diffstat (limited to 'rest-services/dmaap-client')
6 files changed, 191 insertions, 9 deletions
diff --git a/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/api/DmaapClientFactory.java b/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/api/DmaapClientFactory.java index 48e6f5d1..7eb72f7c 100644 --- a/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/api/DmaapClientFactory.java +++ b/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/api/DmaapClientFactory.java @@ -20,23 +20,34 @@ package org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.api; +import com.google.gson.Gson; +import io.vavr.Lazy; +import org.jetbrains.annotations.NotNull; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.RxHttpClient; import org.onap.dcaegen2.services.sdk.rest.services.annotations.ExperimentalApi; +import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.impl.MessageRouterClientImpl; /** + * <b>WARNING</b>: This is a proof-of-concept. It is untested. API may change or be removed. Use at your own risk. + * You've been warned. + * * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a> * @since 1.1.4 */ @ExperimentalApi public final class DmaapClientFactory { + private static final Lazy<MessageRouterClientImpl> THE_CLIENT = Lazy.of(() -> + new MessageRouterClientImpl(RxHttpClient.create(), new Gson())); + private DmaapClientFactory() { } - public static MessageRouterPublisher createMessageRouterPublisher() { - throw new UnsupportedOperationException("not implemented yet"); + public static @NotNull MessageRouterPublisher createMessageRouterPublisher() { + return THE_CLIENT.get(); } - public static MessageRouterSubscriber createMessageRouterSubscriber() { - throw new UnsupportedOperationException("not implemented yet"); + public static @NotNull MessageRouterSubscriber createMessageRouterSubscriber() { + return THE_CLIENT.get(); } } diff --git a/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/impl/MessageRouterClientImpl.java b/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/impl/MessageRouterClientImpl.java new file mode 100644 index 00000000..0ef06201 --- /dev/null +++ b/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/impl/MessageRouterClientImpl.java @@ -0,0 +1,126 @@ +/* + * ============LICENSE_START==================================== + * DCAEGEN2-SERVICES-SDK + * ========================================================= + * Copyright (C) 2019 Nokia. All rights reserved. + * ========================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END===================================== + */ + +package org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.impl; + +import com.google.gson.Gson; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import io.netty.buffer.ByteBuf; +import io.vavr.collection.HashMap; +import java.nio.charset.StandardCharsets; +import java.time.Duration; +import org.jetbrains.annotations.NotNull; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpHeaders; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpMethod; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpRequest; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpResponse; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.ImmutableHttpRequest; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.RequestBody; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.RxHttpClient; +import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.api.MessageRouterPublisher; +import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.api.MessageRouterSubscriber; +import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.ImmutableMessageRouterPublishResponse; +import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.ImmutableMessageRouterSubscribeResponse; +import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.MessageRouterPublishRequest; +import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.MessageRouterPublishResponse; +import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.MessageRouterSubscribeRequest; +import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.MessageRouterSubscribeResponse; +import org.reactivestreams.Publisher; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +/** + * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a> + * @since March 2019 + */ +// TODO: This is a PoC. It's untested. +public class MessageRouterClientImpl implements MessageRouterPublisher, MessageRouterSubscriber { + + private static final Duration WINDOW_MAX_TIME = Duration.ofSeconds(1); + private static final int WINDOW_MAX_SIZE = 512; + private final RxHttpClient httpClient; + private final Gson gson; + + public MessageRouterClientImpl(RxHttpClient httpClient, Gson gson) { + this.httpClient = httpClient; + this.gson = gson; + } + + @Override + public Flux<MessageRouterPublishResponse> put( + MessageRouterPublishRequest request, + Flux<? extends JsonElement> items) { + return items.windowTimeout(WINDOW_MAX_SIZE, WINDOW_MAX_TIME).flatMap(subItems -> + subItems.collect(JsonArray::new, JsonArray::add) + .filter(arr -> arr.size() > 0) + .map(RequestBody::fromJson) + .flatMap(body -> httpClient.call(buildPostHttpRequest(request, body))) + .map(this::buildPutResponse)); + } + + @Override + public Mono<MessageRouterSubscribeResponse> get(MessageRouterSubscribeRequest request) { + return httpClient.call(buildGetHttpRequest(request)).map(this::buildGetResponse); + } + + private @NotNull MessageRouterPublishResponse buildPutResponse(HttpResponse httpResponse) { + final ImmutableMessageRouterPublishResponse.Builder builder = + ImmutableMessageRouterPublishResponse.builder(); + return httpResponse.successful() + ? builder.build() + : builder.failReason(extractFailReason(httpResponse)).build(); + } + + private @NotNull MessageRouterSubscribeResponse buildGetResponse(HttpResponse httpResponse) { + final ImmutableMessageRouterSubscribeResponse.Builder builder = + ImmutableMessageRouterSubscribeResponse.builder(); + return httpResponse.successful() + ? builder.items(httpResponse.bodyAsJson(StandardCharsets.UTF_8, gson, JsonArray.class)).build() + : builder.failReason(extractFailReason(httpResponse)).build(); + } + + private String extractFailReason(HttpResponse httpResponse) { + return String.format("%d %s%n%s", httpResponse.statusCode(), httpResponse.statusReason(), + httpResponse.bodyAsString()); + } + + private @NotNull HttpRequest buildPostHttpRequest(MessageRouterPublishRequest request, RequestBody body) { + return ImmutableHttpRequest.builder() + .method(HttpMethod.POST) + .url(request.sinkDefinition().topicUrl()) + .diagnosticContext(request.diagnosticContext()) + .customHeaders(HashMap.of(HttpHeaders.CONTENT_TYPE, request.contentType())) + .body(body) + .build(); + } + + private @NotNull HttpRequest buildGetHttpRequest(MessageRouterSubscribeRequest request) { + return ImmutableHttpRequest.builder() + .method(HttpMethod.GET) + .url(buildSubscribeUrl(request)) + .diagnosticContext(request.diagnosticContext()) + .build(); + } + + private String buildSubscribeUrl(MessageRouterSubscribeRequest request) { + return String.format("%s/%s/%s", request.sourceDefinition().topicUrl(), request.consumerGroup(), request.consumerId()); + } +} diff --git a/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/model/Constants.java b/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/model/Constants.java new file mode 100644 index 00000000..1e1ce955 --- /dev/null +++ b/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/model/Constants.java @@ -0,0 +1,35 @@ +/* + * ============LICENSE_START==================================== + * DCAEGEN2-SERVICES-SDK + * ========================================================= + * Copyright (C) 2019 Nokia. All rights reserved. + * ========================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END===================================== + */ + +package org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model; + +import java.util.UUID; + +/** + * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a> + * @since March 2019 + */ +final class Constants { + + private Constants() { + } + + static final String CLASS_LOADER_SCOPED_UNIQUE_ID = UUID.randomUUID().toString(); +} diff --git a/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/model/DmaapRequest.java b/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/model/DmaapRequest.java index f2a7193e..2bed4c9f 100644 --- a/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/model/DmaapRequest.java +++ b/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/model/DmaapRequest.java @@ -34,5 +34,4 @@ public interface DmaapRequest { default RequestDiagnosticContext diagnosticContext() { return RequestDiagnosticContext.create(); } - } diff --git a/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/model/MessageRouterPublishRequest.java b/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/model/MessageRouterPublishRequest.java index 0d507865..4c816f31 100644 --- a/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/model/MessageRouterPublishRequest.java +++ b/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/model/MessageRouterPublishRequest.java @@ -23,7 +23,6 @@ package org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model; import org.immutables.value.Value; import org.onap.dcaegen2.services.sdk.model.streams.dmaap.MessageRouterSink; import org.onap.dcaegen2.services.sdk.rest.services.annotations.ExperimentalApi; -import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext; /** * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a> @@ -34,4 +33,9 @@ import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnos public interface MessageRouterPublishRequest extends DmaapRequest { MessageRouterSink sinkDefinition(); + + @Value.Default + default String contentType() { + return "application/json"; + } } diff --git a/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/model/MessageRouterSubscribeRequest.java b/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/model/MessageRouterSubscribeRequest.java index 49dca603..f32fd0ee 100644 --- a/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/model/MessageRouterSubscribeRequest.java +++ b/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/model/MessageRouterSubscribeRequest.java @@ -17,14 +17,13 @@ * limitations under the License. * ============LICENSE_END===================================== */ - package org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model; +import java.time.Duration; import org.immutables.value.Value; +import org.jetbrains.annotations.Nullable; import org.onap.dcaegen2.services.sdk.model.streams.dmaap.MessageRouterSource; import org.onap.dcaegen2.services.sdk.rest.services.annotations.ExperimentalApi; -import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext; - /** * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a> @@ -35,4 +34,12 @@ import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnos public interface MessageRouterSubscribeRequest extends DmaapRequest { MessageRouterSource sourceDefinition(); + + String consumerGroup(); + + @Nullable Duration timeout(); + + default String consumerId() { + return Constants.CLASS_LOADER_SCOPED_UNIQUE_ID; + } } |