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;
23 import static org.onap.dcaegen2.services.prh.model.CommonFunctions.createJsonBody;
24 import static org.onap.dcaegen2.services.prh.model.logging.MdcVariables.REQUEST_ID;
25 import static org.onap.dcaegen2.services.prh.model.logging.MdcVariables.X_INVOCATION_ID;
26 import static org.onap.dcaegen2.services.prh.model.logging.MdcVariables.X_ONAP_REQUEST_ID;
29 import java.net.URISyntaxException;
30 import java.util.UUID;
31 import org.apache.http.client.utils.URIBuilder;
32 import org.onap.dcaegen2.services.prh.config.DmaapPublisherConfiguration;
33 import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel;
35 import org.springframework.http.HttpEntity;
36 import org.springframework.http.HttpHeaders;
37 import org.springframework.http.HttpMethod;
38 import org.springframework.http.ResponseEntity;
39 import org.springframework.web.client.RestTemplate;
40 import reactor.core.publisher.Mono;
43 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/4/18
45 public class DMaaPProducerReactiveHttpClient {
47 private final String dmaapHostName;
48 private final Integer dmaapPortNumber;
49 private final String dmaapProtocol;
50 private final String dmaapTopicName;
51 private final String dmaapContentType;
53 private RestTemplate restTemplate;
56 * Constructor DMaaPProducerReactiveHttpClient.
58 * @param dmaapPublisherConfiguration - DMaaP producer configuration object
60 public DMaaPProducerReactiveHttpClient(DmaapPublisherConfiguration dmaapPublisherConfiguration) {
61 this.dmaapHostName = dmaapPublisherConfiguration.dmaapHostName();
62 this.dmaapProtocol = dmaapPublisherConfiguration.dmaapProtocol();
63 this.dmaapPortNumber = dmaapPublisherConfiguration.dmaapPortNumber();
64 this.dmaapTopicName = dmaapPublisherConfiguration.dmaapTopicName();
65 this.dmaapContentType = dmaapPublisherConfiguration.dmaapContentType();
69 * Function for calling DMaaP HTTP producer - post request to DMaaP.
71 * @param consumerDmaapModelMono - object which will be sent to DMaaP
72 * @return status code of operation
75 public Mono<ResponseEntity<String>> getDMaaPProducerResponse(ConsumerDmaapModel consumerDmaapModelMono) {
76 return Mono.defer(() -> {
78 HttpEntity<String> request = new HttpEntity<>(createJsonBody(consumerDmaapModelMono), getAllHeaders());
79 return Mono.just(restTemplate.exchange(getUri(), HttpMethod.POST, request, String.class));
80 } catch (URISyntaxException e) {
86 private HttpHeaders getAllHeaders() {
87 HttpHeaders headers = new HttpHeaders();
88 headers.set(X_ONAP_REQUEST_ID, MDC.get(REQUEST_ID));
89 headers.set(X_INVOCATION_ID, UUID.randomUUID().toString());
90 headers.set(HttpHeaders.CONTENT_TYPE, dmaapContentType);
95 public DMaaPProducerReactiveHttpClient createDMaaPWebClient(RestTemplate restTemplate) {
96 this.restTemplate = restTemplate;
100 URI getUri() throws URISyntaxException {
101 return new URIBuilder().setScheme(dmaapProtocol).setHost(dmaapHostName).setPort(dmaapPortNumber)
102 .setPath(dmaapTopicName).build();