d92aef9c94913fb861929397da1287374de80538
[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 static org.onap.dcaegen2.services.sdk.rest.services.model.logging.MdcVariables.REQUEST_ID;
24 import static org.onap.dcaegen2.services.sdk.rest.services.model.logging.MdcVariables.X_INVOCATION_ID;
25 import static org.onap.dcaegen2.services.sdk.rest.services.model.logging.MdcVariables.X_ONAP_REQUEST_ID;
26
27 import java.net.URI;
28 import java.util.UUID;
29 import java.util.function.Consumer;
30 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapConsumerConfiguration;
31 import org.onap.dcaegen2.services.sdk.rest.services.uri.URI.URIBuilder;
32 import org.slf4j.MDC;
33 import org.springframework.http.HttpHeaders;
34 import org.springframework.http.HttpStatus;
35 import org.springframework.web.reactive.function.client.WebClient;
36 import reactor.core.publisher.Mono;
37
38
39 /**
40  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 6/26/18
41  */
42 public class DMaaPConsumerReactiveHttpClient {
43
44     private final String dmaapHostName;
45     private final String dmaapProtocol;
46     private final Integer dmaapPortNumber;
47     private final String dmaapTopicName;
48     private final String consumerGroup;
49     private final String consumerId;
50     private final String contentType;
51     private final WebClient webClient;
52     private final static String SLASH="/";
53
54     /**
55      * Constructor of DMaaPConsumerReactiveHttpClient.
56      *
57      * @param consumerConfiguration - DMaaP consumer configuration object
58      */
59     public DMaaPConsumerReactiveHttpClient(DmaapConsumerConfiguration consumerConfiguration, WebClient webClient) {
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.webClient = webClient;
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() {
76         return webClient
77             .get()
78             .uri(getUri())
79             .headers(getHeaders())
80             .retrieve()
81             .onStatus(HttpStatus::is4xxClientError, clientResponse ->
82                 Mono.error(new RuntimeException("DmaaPConsumer HTTP " + clientResponse.statusCode()))
83             )
84             .onStatus(HttpStatus::is5xxServerError, clientResponse ->
85                 Mono.error(new RuntimeException("DmaaPConsumer HTTP " + clientResponse.statusCode())))
86             .bodyToMono(String.class);
87     }
88
89     private Consumer<HttpHeaders> getHeaders() {
90         return httpHeaders -> {
91             httpHeaders.set(X_ONAP_REQUEST_ID, MDC.get(REQUEST_ID));
92             httpHeaders.set(X_INVOCATION_ID, UUID.randomUUID().toString());
93             httpHeaders.set(HttpHeaders.CONTENT_TYPE, contentType);
94         };
95     }
96
97     private String createRequestPath() {
98         return  new StringBuilder().append(SLASH).append(dmaapTopicName).append(SLASH).append(consumerGroup).append(SLASH).append(consumerId).toString();
99     }
100
101     URI getUri() {
102         return URI.create(
103             new URIBuilder().scheme(dmaapProtocol).host(dmaapHostName).port(dmaapPortNumber).path(createRequestPath())
104                 .build().toString());
105     }
106 }