2 * ============LICENSE_START=======================================================
3 * DCAEGEN2-SERVICES-SDK
4 * ================================================================================
5 * Copyright (C) 2018 NOKIA Intellectual Property. 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.service.consumer;
24 import java.util.HashMap;
26 import java.util.Optional;
27 import java.util.UUID;
28 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.CloudHttpClient;
29 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapConsumerConfiguration;
30 import org.onap.dcaegen2.services.sdk.rest.services.model.logging.ImmutableRequestDiagnosticContext;
31 import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext;
32 import org.onap.dcaegen2.services.sdk.rest.services.uri.URI.URIBuilder;
33 import reactor.core.publisher.Mono;
37 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 6/26/18
39 public class DMaaPConsumerReactiveHttpClient {
41 private final static String SLASH = "/";
42 private final static String CONTENT_TYPE = "Content-Type";
43 private final String dmaapHostName;
44 private final String dmaapProtocol;
45 private final Integer dmaapPortNumber;
46 private final String dmaapTopicName;
47 private final String consumerGroup;
48 private final String consumerId;
49 private final String contentType;
50 private final CloudHttpClient cloudHttpClient;
53 * Constructor of DMaaPConsumerReactiveHttpClient.
55 * @param consumerConfiguration - DMaaP consumer configuration object
58 public DMaaPConsumerReactiveHttpClient(DmaapConsumerConfiguration consumerConfiguration,
59 CloudHttpClient cloudHttpClient) {
60 this.dmaapHostName = consumerConfiguration.dmaapHostName();
61 this.dmaapProtocol = consumerConfiguration.dmaapProtocol();
62 this.dmaapPortNumber = consumerConfiguration.dmaapPortNumber();
63 this.dmaapTopicName = consumerConfiguration.dmaapTopicName();
64 this.consumerGroup = consumerConfiguration.consumerGroup();
65 this.consumerId = consumerConfiguration.consumerId();
66 this.contentType = consumerConfiguration.dmaapContentType();
67 this.cloudHttpClient = cloudHttpClient;
71 * Function for calling DMaaP HTTP consumer - consuming messages from Kafka/DMaaP from topic.
73 * @return reactive response from DMaaP in string format
75 public Mono<String> getDMaaPConsumerResponse(Optional<RequestDiagnosticContext> requestDiagnosticContextOptional) {
76 Map<String,String> headers = new HashMap<>();
77 headers.put(CONTENT_TYPE,contentType);
78 if (requestDiagnosticContextOptional.isPresent()) {
79 return cloudHttpClient.get(getUri().toString(), requestDiagnosticContextOptional.get(),headers, String.class);
81 RequestDiagnosticContext requestDiagnosticContext = ImmutableRequestDiagnosticContext.builder()
82 .invocationId(UUID.randomUUID()).requestId(UUID.randomUUID()).build();
83 return cloudHttpClient.get(getUri().toString(), requestDiagnosticContext, headers, String.class);
88 new URIBuilder().scheme(dmaapProtocol).host(dmaapHostName).port(dmaapPortNumber).path(createRequestPath())
92 private String createRequestPath() {
93 return new StringBuilder().append(SLASH).append(dmaapTopicName).append(SLASH).append(consumerGroup)
94 .append(SLASH).append(consumerId).toString();