ad9e6fe7ecda654b11acf4e49af5cb8a028105d4
[dcaegen2/collectors/datafile.git] /
1 /*
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2018 NOKIA Intellectual Property, 2018 Nordix Foundation. All rights reserved.
4  * ===============================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ============LICENSE_END========================================================================
17  */
18
19 package org.onap.dcaegen2.collectors.datafile.service.consumer;
20
21 import java.net.URI;
22 import java.net.URISyntaxException;
23
24 import org.apache.http.client.utils.URIBuilder;
25 import org.onap.dcaegen2.collectors.datafile.config.DmaapConsumerConfiguration;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.springframework.http.HttpStatus;
29 import org.springframework.web.reactive.function.client.WebClient;
30
31 import reactor.core.publisher.Mono;
32
33 /**
34  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 6/26/18
35  * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
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.error("Unable to parse URI in message from xNF.", e);
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 }