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
9 * http://www.apache.org/licenses/LICENSE-2.0
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========================================================================
19 package org.onap.dcaegen2.collectors.datafile.service.consumer;
22 import java.net.URISyntaxException;
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;
31 import reactor.core.publisher.Mono;
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>
37 public class DmaapConsumerReactiveHttpClient {
39 private final Logger logger = LoggerFactory.getLogger(this.getClass());
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;
50 * Constructor of DmaapConsumerReactiveHttpClient.
52 * @param consumerConfiguration - DMaaP consumer configuration object
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();
64 * Function for calling DMaaP HTTP consumer - consuming messages from Kafka/DMaaP from topic.
66 * @return reactive response from DMaaP in string format
68 public Mono<String> getDmaapConsumerResponse() {
74 .onStatus(HttpStatus::is4xxClientError, clientResponse ->
75 Mono.error(new Exception("HTTP 400"))
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);
86 private String createRequestPath() {
87 return dmaapTopicName + "/" + consumerGroup + "/" + consumerId;
90 public DmaapConsumerReactiveHttpClient createDmaapWebClient(WebClient webClient) {
91 this.webClient = webClient;
95 URI getUri() throws URISyntaxException {
96 return new URIBuilder().setScheme(dmaapProtocol).setHost(dmaapHostName).setPort(dmaapPortNumber)
97 .setPath(createRequestPath()).build();