af54bf3b0ffd25cbd2df2c1888aa8a8a3ebd1d94
[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 java.net.URI;
24 import java.net.URISyntaxException;
25 import org.apache.http.client.utils.URIBuilder;
26 import org.onap.dcaegen2.services.prh.config.DmaapConsumerConfiguration;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.springframework.http.HttpStatus;
30 import org.springframework.web.reactive.function.client.WebClient;
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  */
36 public class DMaaPConsumerReactiveHttpClient {
37
38     private final Logger logger = LoggerFactory.getLogger(this.getClass());
39
40     private WebClient webClient;
41     private final String dmaapHostName;
42     private final String dmaapProtocol;
43     private final Integer dmaapPortNumber;
44     private final String dmaapTopicName;
45     private final String consumerGroup;
46     private final String consumerId;
47
48     public DMaaPConsumerReactiveHttpClient(DmaapConsumerConfiguration consumerConfiguration) {
49         this.dmaapHostName = consumerConfiguration.dmaapHostName();
50         this.dmaapProtocol = consumerConfiguration.dmaapProtocol();
51         this.dmaapPortNumber = consumerConfiguration.dmaapPortNumber();
52         this.dmaapTopicName = consumerConfiguration.dmaapTopicName();
53         this.consumerGroup = consumerConfiguration.consumerGroup();
54         this.consumerId = consumerConfiguration.consumerId();
55     }
56
57     public Mono<String> getDMaaPConsumerResponse() {
58         try {
59             return webClient
60                 .get()
61                 .uri(getUri())
62                 .retrieve()
63                 .onStatus(HttpStatus::is4xxClientError, clientResponse ->
64                     Mono.error(new Exception("HTTP 400"))
65                 )
66                 .onStatus(HttpStatus::is5xxServerError, clientResponse ->
67                     Mono.error(new Exception("HTTP 500")))
68                 .bodyToMono(String.class);
69         } catch (URISyntaxException e) {
70             logger.warn("Exception while evaluating URI ");
71             return Mono.error(e);
72         }
73     }
74
75     private String createRequestPath() {
76         return dmaapTopicName + "/" + consumerGroup + "/" + consumerId;
77     }
78
79     public DMaaPConsumerReactiveHttpClient createDMaaPWebClient(WebClient webClient) {
80         this.webClient = webClient;
81         return this;
82     }
83
84     URI getUri() throws URISyntaxException {
85         return new URIBuilder().setScheme(dmaapProtocol).setHost(dmaapHostName).setPort(dmaapPortNumber)
86             .setPath(createRequestPath()).build();
87     }
88 }