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