23cdc98d3d8b91bfae11bb0d6a4e08cbf59bab19
[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 package org.onap.dcaegen2.services.prh.service.consumer;
21
22 import java.net.URI;
23 import java.net.URISyntaxException;
24 import org.apache.http.client.utils.URIBuilder;
25 import org.onap.dcaegen2.services.prh.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 import reactor.core.publisher.Mono;
31
32 /**
33  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 6/26/18
34  */
35 public class DMaaPConsumerReactiveHttpClient {
36
37     private final Logger logger = LoggerFactory.getLogger(this.getClass());
38
39     private WebClient webClient;
40     private final String dmaapHostName;
41     private final String dmaapProtocol;
42     private final Integer dmaapPortNumber;
43     private final String dmaapTopicName;
44     private final String consumerGroup;
45     private final String consumerId;
46
47     public DMaaPConsumerReactiveHttpClient(DmaapConsumerConfiguration consumerConfiguration) {
48         this.dmaapHostName = consumerConfiguration.dmaapHostName();
49         this.dmaapProtocol = consumerConfiguration.dmaapProtocol();
50         this.dmaapPortNumber = consumerConfiguration.dmaapPortNumber();
51         this.dmaapTopicName = consumerConfiguration.dmaapTopicName();
52         this.consumerGroup = consumerConfiguration.consumerGroup();
53         this.consumerId = consumerConfiguration.consumerId();
54     }
55
56     public Mono<String> getDMaaPConsumerResponse() {
57         try {
58             return webClient
59                 .get()
60                 .uri(getUri())
61                 .retrieve()
62                 .onStatus(HttpStatus::is4xxClientError, clientResponse ->
63                     Mono.error(new Exception("HTTP 400"))
64                 )
65                 .onStatus(HttpStatus::is5xxServerError, clientResponse ->
66                     Mono.error(new Exception("HTTP 500")))
67                 .bodyToMono(String.class);
68         } catch (URISyntaxException e) {
69             logger.warn("Exception while evaluating URI ");
70             return Mono.error(e);
71         }
72     }
73
74     private String createRequestPath() {
75         return dmaapTopicName + "/" + consumerGroup + "/" + consumerId;
76     }
77
78     public DMaaPConsumerReactiveHttpClient createDMaaPWebClient(WebClient webClient) {
79         this.webClient = webClient;
80         return this;
81     }
82
83     URI getUri() throws URISyntaxException {
84         return new URIBuilder().setScheme(dmaapProtocol).setHost(dmaapHostName).setPort(dmaapPortNumber)
85             .setPath(createRequestPath()).build();
86     }
87 }