0bc78d439f32207926c81f027bca412920378bc0
[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.producer;
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.DmaapPublisherConfiguration;
27 import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.http.HttpStatus;
31 import org.springframework.web.reactive.function.BodyInserters;
32 import org.springframework.web.reactive.function.client.WebClient;
33 import reactor.core.publisher.Mono;
34
35 /**
36  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/4/18
37  */
38 public class DMaaPProducerReactiveHttpClient {
39
40     private final Logger logger = LoggerFactory.getLogger(this.getClass());
41
42     private WebClient webClient;
43     private final String dmaapHostName;
44     private final Integer dmaapPortNumber;
45     private final String dmaapProtocol;
46     private final String dmaapTopicName;
47
48     public DMaaPProducerReactiveHttpClient(DmaapPublisherConfiguration dmaapPublisherConfiguration) {
49         this.dmaapHostName = dmaapPublisherConfiguration.dmaapHostName();
50         this.dmaapProtocol = dmaapPublisherConfiguration.dmaapProtocol();
51         this.dmaapPortNumber = dmaapPublisherConfiguration.dmaapPortNumber();
52         this.dmaapTopicName = dmaapPublisherConfiguration.dmaapTopicName();
53     }
54
55     public Mono<String> getDMaaPProducerResponse(Mono<ConsumerDmaapModel> consumerDmaapModelMono) {
56         try {
57             return webClient
58                 .post()
59                 .uri(getUri())
60                 .body(BodyInserters.fromObject(consumerDmaapModelMono))
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     public DMaaPProducerReactiveHttpClient createDMaaPWebClient(WebClient webClient) {
75         this.webClient = webClient;
76         return this;
77     }
78
79     URI getUri() throws URISyntaxException {
80         return new URIBuilder().setScheme(dmaapProtocol).setHost(dmaapHostName).setPort(dmaapPortNumber)
81             .setPath(dmaapTopicName).build();
82     }
83
84 }