99f70209ca6d7e0e79a04632732e34a05ff0b1b8
[dcaegen2/services/sdk.git] /
1 /*
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
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.service.consumer;
22
23 import java.net.URI;
24 import java.util.HashMap;
25 import java.util.Map;
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;
34
35
36 /**
37  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 6/26/18
38  */
39 public class DMaaPConsumerReactiveHttpClient {
40
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;
51
52     /**
53      * Constructor of DMaaPConsumerReactiveHttpClient.
54      *
55      * @param consumerConfiguration - DMaaP consumer configuration object
56      */
57
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;
68     }
69
70     /**
71      * Function for calling DMaaP HTTP consumer - consuming messages from Kafka/DMaaP from topic.
72      *
73      * @return reactive response from DMaaP in string format
74      */
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);
80         }
81         RequestDiagnosticContext requestDiagnosticContext = ImmutableRequestDiagnosticContext.builder()
82             .invocationId(UUID.randomUUID()).requestId(UUID.randomUUID()).build();
83         return cloudHttpClient.get(getUri().toString(), requestDiagnosticContext, headers, String.class);
84     }
85
86     URI getUri() {
87         return URI.create(
88             new URIBuilder().scheme(dmaapProtocol).host(dmaapHostName).port(dmaapPortNumber).path(createRequestPath())
89                 .build().toString());
90     }
91
92     private String createRequestPath() {
93         return new StringBuilder().append(SLASH).append(dmaapTopicName).append(SLASH).append(consumerGroup)
94             .append(SLASH).append(consumerId).toString();
95     }
96 }