1fcebeacf20cfe646361e61f4927952f37ab06ef
[dcaegen2/collectors/datafile.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * Datafile Collector Service
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.collectors.datafile.service.consumer;
22
23 import java.net.URI;
24 import java.net.URISyntaxException;
25 import org.apache.http.client.utils.URIBuilder;
26 import org.onap.dcaegen2.collectors.datafile.config.DmaapConsumerConfiguration;
27 import org.onap.dcaegen2.collectors.datafile.service.consumer.DMaaPConsumerReactiveHttpClient;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.http.HttpStatus;
31 import org.springframework.web.reactive.function.client.WebClient;
32 import reactor.core.publisher.Mono;
33
34 /**
35  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 6/26/18
36  */
37 public class DMaaPConsumerReactiveHttpClient {
38
39     private final Logger logger = LoggerFactory.getLogger(this.getClass());
40
41     private WebClient webClient;
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
49     /**
50      * Constructor of DMaaPConsumerReactiveHttpClient.
51      *
52      * @param consumerConfiguration - DMaaP consumer configuration object
53      */
54     public DMaaPConsumerReactiveHttpClient(DmaapConsumerConfiguration consumerConfiguration) {
55         this.dmaapHostName = consumerConfiguration.dmaapHostName();
56         this.dmaapProtocol = consumerConfiguration.dmaapProtocol();
57         this.dmaapPortNumber = consumerConfiguration.dmaapPortNumber();
58         this.dmaapTopicName = consumerConfiguration.dmaapTopicName();
59         this.consumerGroup = consumerConfiguration.consumerGroup();
60         this.consumerId = consumerConfiguration.consumerId();
61     }
62
63     /**
64      * Function for calling DMaaP HTTP consumer - consuming messages from Kafka/DMaaP from topic.
65      *
66      * @return reactive response from DMaaP in string format
67      */
68     public Mono<String> getDMaaPConsumerResponse() {
69         try {
70             return webClient
71                 .get()
72                 .uri(getUri())
73                 .retrieve()
74                 .onStatus(HttpStatus::is4xxClientError, clientResponse ->
75                     Mono.error(new Exception("HTTP 400"))
76                 )
77                 .onStatus(HttpStatus::is5xxServerError, clientResponse ->
78                     Mono.error(new Exception("HTTP 500")))
79                 .bodyToMono(String.class);
80         } catch (URISyntaxException e) {
81             logger.warn("Exception while evaluating URI ");
82             return Mono.error(e);
83         }
84     }
85
86     private String createRequestPath() {
87         return dmaapTopicName + "/" + consumerGroup + "/" + consumerId;
88     }
89
90     public DMaaPConsumerReactiveHttpClient createDMaaPWebClient(WebClient webClient) {
91         this.webClient = webClient;
92         return this;
93     }
94
95     URI getUri() throws URISyntaxException {
96         return new URIBuilder().setScheme(dmaapProtocol).setHost(dmaapHostName).setPort(dmaapPortNumber)
97             .setPath(createRequestPath()).build();
98     }
99 }