c6ac05bd321f6323c520faaa0b0993bfdfeac4c1
[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.slf4j.MDC;
32 import org.springframework.http.HttpHeaders;
33 import org.springframework.http.HttpStatus;
34 import org.springframework.web.reactive.function.client.WebClient;
35 import org.springframework.web.util.DefaultUriBuilderFactory;
36 import reactor.core.publisher.Mono;
37
38 /**
39  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 6/26/18
40  */
41 public class DMaaPConsumerReactiveHttpClient {
42     private final String dmaapHostName;
43     private final String dmaapProtocol;
44     private final Integer dmaapPortNumber;
45     private final String dmaapTopicName;
46     private final String consumerGroup;
47     private final String consumerId;
48     private final String contentType;
49     private final WebClient webClient;
50
51     /**
52      * Constructor of DMaaPConsumerReactiveHttpClient.
53      *
54      * @param consumerConfiguration - DMaaP consumer configuration object
55      */
56     public DMaaPConsumerReactiveHttpClient(DmaapConsumerConfiguration consumerConfiguration, WebClient webClient) {
57         this.dmaapHostName = consumerConfiguration.dmaapHostName();
58         this.dmaapProtocol = consumerConfiguration.dmaapProtocol();
59         this.dmaapPortNumber = consumerConfiguration.dmaapPortNumber();
60         this.dmaapTopicName = consumerConfiguration.dmaapTopicName();
61         this.consumerGroup = consumerConfiguration.consumerGroup();
62         this.consumerId = consumerConfiguration.consumerId();
63         this.contentType = consumerConfiguration.dmaapContentType();
64         this.webClient = webClient;
65     }
66
67     /**
68      * Function for calling DMaaP HTTP consumer - consuming messages from Kafka/DMaaP from topic.
69      *
70      * @return reactive response from DMaaP in string format
71      */
72     public Mono<String> getDMaaPConsumerResponse() {
73         return webClient
74             .get()
75             .uri(getUri())
76             .headers(getHeaders())
77             .retrieve()
78             .onStatus(HttpStatus::is4xxClientError, clientResponse ->
79                 Mono.error(new RuntimeException("DmaaPConsumer HTTP " + clientResponse.statusCode()))
80             )
81             .onStatus(HttpStatus::is5xxServerError, clientResponse ->
82                 Mono.error(new RuntimeException("DmaaPConsumer HTTP " + clientResponse.statusCode())))
83             .bodyToMono(String.class);
84     }
85
86     private Consumer<HttpHeaders> getHeaders() {
87         return httpHeaders -> {
88             httpHeaders.set(X_ONAP_REQUEST_ID, MDC.get(REQUEST_ID));
89             httpHeaders.set(X_INVOCATION_ID, UUID.randomUUID().toString());
90             httpHeaders.set(HttpHeaders.CONTENT_TYPE, contentType);
91         };
92     }
93
94     private String createRequestPath() {
95         return dmaapTopicName + "/" + consumerGroup + "/" + consumerId;
96     }
97
98
99     URI getUri() {
100         return new DefaultUriBuilderFactory().builder().scheme(dmaapProtocol).host(dmaapHostName).port(dmaapPortNumber)
101             .path(createRequestPath()).build();
102     }
103 }