242214d283a6918df2d2000a2702572736cf009a
[dcaegen2/services/prh.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * PNF-REGISTRATION-HANDLER
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.prh.service.consumer;
22
23 import static org.onap.dcaegen2.services.prh.model.logging.MdcVariables.REQUEST_ID;
24 import static org.onap.dcaegen2.services.prh.model.logging.MdcVariables.X_INVOCATION_ID;
25 import static org.onap.dcaegen2.services.prh.model.logging.MdcVariables.X_ONAP_REQUEST_ID;
26
27 import java.net.URI;
28 import java.net.URISyntaxException;
29 import java.util.UUID;
30 import java.util.function.Consumer;
31 import org.apache.http.client.utils.URIBuilder;
32 import org.onap.dcaegen2.services.prh.config.DmaapConsumerConfiguration;
33 import org.slf4j.MDC;
34 import org.springframework.http.HttpHeaders;
35 import org.springframework.http.HttpStatus;
36 import org.springframework.web.reactive.function.client.WebClient;
37 import reactor.core.publisher.Mono;
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 WebClient webClient;
52
53     /**
54      * Constructor of DMaaPConsumerReactiveHttpClient.
55      *
56      * @param consumerConfiguration - DMaaP consumer configuration object
57      */
58     public DMaaPConsumerReactiveHttpClient(DmaapConsumerConfiguration consumerConfiguration) {
59         this.dmaapHostName = consumerConfiguration.dmaapHostName();
60         this.dmaapProtocol = consumerConfiguration.dmaapProtocol();
61         this.dmaapPortNumber = consumerConfiguration.dmaapPortNumber();
62         this.dmaapTopicName = consumerConfiguration.dmaapTopicName();
63         this.consumerGroup = consumerConfiguration.consumerGroup();
64         this.consumerId = consumerConfiguration.consumerId();
65         this.contentType = consumerConfiguration.dmaapContentType();
66     }
67
68     /**
69      * Function for calling DMaaP HTTP consumer - consuming messages from Kafka/DMaaP from topic.
70      *
71      * @return reactive response from DMaaP in string format
72      */
73     public Mono<String> getDMaaPConsumerResponse() {
74         try {
75             return webClient
76                 .get()
77                 .uri(getUri())
78                 .headers(getHeaders())
79                 .retrieve()
80                 .onStatus(HttpStatus::is4xxClientError, clientResponse ->
81                     Mono.error(new RuntimeException("DmaaPConsumer HTTP " + clientResponse.statusCode()))
82                 )
83                 .onStatus(HttpStatus::is5xxServerError, clientResponse ->
84                     Mono.error(new RuntimeException("DmaaPConsumer HTTP " + clientResponse.statusCode())))
85                 .bodyToMono(String.class);
86         } catch (URISyntaxException e) {
87             return Mono.error(e);
88         }
89     }
90
91     private Consumer<HttpHeaders> getHeaders() {
92         return httpHeaders -> {
93             httpHeaders.set(X_ONAP_REQUEST_ID, MDC.get(REQUEST_ID));
94             httpHeaders.set(X_INVOCATION_ID, UUID.randomUUID().toString());
95             httpHeaders.set(HttpHeaders.CONTENT_TYPE, contentType);
96         };
97     }
98
99     private String createRequestPath() {
100         return dmaapTopicName + "/" + consumerGroup + "/" + consumerId;
101     }
102
103     public DMaaPConsumerReactiveHttpClient createDMaaPWebClient(WebClient webClient) {
104         this.webClient = webClient;
105         return this;
106     }
107
108     URI getUri() throws URISyntaxException {
109         return new URIBuilder().setScheme(dmaapProtocol).setHost(dmaapHostName).setPort(dmaapPortNumber)
110             .setPath(createRequestPath()).build();
111     }
112 }