d049d38023b9d194cc71c6130ad9fa928667b52b
[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 static org.onap.dcaegen2.services.prh.model.logging.MDCVariables.REQUEST_ID;
24 import static org.onap.dcaegen2.services.prh.model.logging.MDCVariables.X_INVOCATION_ID;
25 import static org.onap.dcaegen2.services.prh.model.logging.MDCVariables.X_ONAP_REQUEST_ID;
26
27 import java.net.URI;
28 import java.net.URISyntaxException;
29 import java.util.UUID;
30 import org.apache.http.client.utils.URIBuilder;
31 import org.onap.dcaegen2.services.prh.config.DmaapPublisherConfiguration;
32 import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.slf4j.MDC;
36 import org.springframework.http.HttpStatus;
37 import org.springframework.web.reactive.function.BodyInserters;
38 import org.springframework.web.reactive.function.client.WebClient;
39 import reactor.core.publisher.Mono;
40
41 /**
42  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/4/18
43  */
44 public class DMaaPProducerReactiveHttpClient {
45
46     private final Logger logger = LoggerFactory.getLogger(this.getClass());
47     private final String dmaapHostName;
48     private final Integer dmaapPortNumber;
49     private final String dmaapProtocol;
50     private final String dmaapTopicName;
51     private WebClient webClient;
52
53     /**
54      * Constructor DMaaPProducerReactiveHttpClient.
55      *
56      * @param dmaapPublisherConfiguration - DMaaP producer configuration object
57      */
58     public DMaaPProducerReactiveHttpClient(DmaapPublisherConfiguration dmaapPublisherConfiguration) {
59         this.dmaapHostName = dmaapPublisherConfiguration.dmaapHostName();
60         this.dmaapProtocol = dmaapPublisherConfiguration.dmaapProtocol();
61         this.dmaapPortNumber = dmaapPublisherConfiguration.dmaapPortNumber();
62         this.dmaapTopicName = dmaapPublisherConfiguration.dmaapTopicName();
63     }
64
65     /**
66      * Function for calling DMaaP HTTP producer - post request to DMaaP.
67      *
68      * @param consumerDmaapModelMono - object which will be sent to DMaaP
69      * @return status code of operation
70      */
71     public Mono<String> getDMaaPProducerResponse(ConsumerDmaapModel consumerDmaapModelMono) {
72         try {
73             return webClient
74                 .post()
75                 .uri(getUri())
76                 .header(X_ONAP_REQUEST_ID, MDC.get(REQUEST_ID))
77                 .header(X_INVOCATION_ID, UUID.randomUUID().toString())
78                 .body(BodyInserters.fromObject(consumerDmaapModelMono))
79                 .retrieve()
80                 .onStatus(HttpStatus::is4xxClientError, clientResponse ->
81                     Mono.error(new Exception("DmaapProducer HTTP" + clientResponse.statusCode()))
82                 )
83                 .onStatus(HttpStatus::is5xxServerError, clientResponse ->
84                     Mono.error(new Exception("DmaapProducer HTTP " + clientResponse.statusCode())))
85                 .bodyToMono(String.class);
86         } catch (URISyntaxException e) {
87             logger.warn("Exception while evaluating URI");
88             return Mono.error(e);
89         }
90     }
91
92     public DMaaPProducerReactiveHttpClient createDMaaPWebClient(WebClient webClient) {
93         this.webClient = webClient;
94         return this;
95     }
96
97     URI getUri() throws URISyntaxException {
98         return new URIBuilder().setScheme(dmaapProtocol).setHost(dmaapHostName).setPort(dmaapPortNumber)
99             .setPath(dmaapTopicName).build();
100     }
101
102 }