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