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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.dcaegen2.services.prh.service.producer;
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;
36 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/4/18
38 public class DMaaPProducerReactiveHttpClient {
40 private final Logger logger = LoggerFactory.getLogger(this.getClass());
42 private WebClient webClient;
43 private final String dmaapHostName;
44 private final Integer dmaapPortNumber;
45 private final String dmaapProtocol;
46 private final String dmaapTopicName;
49 * Constructor DMaaPProducerReactiveHttpClient.
51 * @param dmaapPublisherConfiguration - DMaaP producer configuration object
53 public DMaaPProducerReactiveHttpClient(DmaapPublisherConfiguration dmaapPublisherConfiguration) {
54 this.dmaapHostName = dmaapPublisherConfiguration.dmaapHostName();
55 this.dmaapProtocol = dmaapPublisherConfiguration.dmaapProtocol();
56 this.dmaapPortNumber = dmaapPublisherConfiguration.dmaapPortNumber();
57 this.dmaapTopicName = dmaapPublisherConfiguration.dmaapTopicName();
61 * Function for calling DMaaP HTTP producer - post request to DMaaP.
63 * @param consumerDmaapModelMono - object which will be sent to DMaaP
64 * @return status code of operation
66 public Mono<String> getDMaaPProducerResponse(Mono<ConsumerDmaapModel> consumerDmaapModelMono) {
71 .body(BodyInserters.fromObject(consumerDmaapModelMono))
73 .onStatus(HttpStatus::is4xxClientError, clientResponse ->
74 Mono.error(new Exception("HTTP 400"))
76 .onStatus(HttpStatus::is5xxServerError, clientResponse ->
77 Mono.error(new Exception("HTTP 500")))
78 .bodyToMono(String.class);
79 } catch (URISyntaxException e) {
80 logger.warn("Exception while evaluating URI");
85 public DMaaPProducerReactiveHttpClient createDMaaPWebClient(WebClient webClient) {
86 this.webClient = webClient;
90 URI getUri() throws URISyntaxException {
91 return new URIBuilder().setScheme(dmaapProtocol).setHost(dmaapHostName).setPort(dmaapPortNumber)
92 .setPath(dmaapTopicName).build();