6cd54846e81b22c320e21f53e227bab985fa3d0f
[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.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;
27
28 import java.net.URI;
29 import java.util.UUID;
30 import org.onap.dcaegen2.services.prh.config.DmaapPublisherConfiguration;
31 import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel;
32 import org.slf4j.MDC;
33 import org.springframework.http.HttpEntity;
34 import org.springframework.http.HttpHeaders;
35 import org.springframework.http.HttpMethod;
36 import org.springframework.http.ResponseEntity;
37 import org.springframework.web.client.RestTemplate;
38 import org.springframework.web.util.DefaultUriBuilderFactory;
39 import reactor.core.publisher.Mono;
40
41
42
43 /**
44  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/4/18
45  */
46 public class DMaaPProducerReactiveHttpClient {
47
48     private final String dmaapHostName;
49     private final Integer dmaapPortNumber;
50     private final String dmaapProtocol;
51     private final String dmaapTopicName;
52     private final String dmaapContentType;
53
54     private RestTemplate restTemplate;
55
56     /**
57      * Constructor DMaaPProducerReactiveHttpClient.
58      *
59      * @param dmaapPublisherConfiguration - DMaaP producer configuration object
60      */
61     public DMaaPProducerReactiveHttpClient(DmaapPublisherConfiguration dmaapPublisherConfiguration) {
62         this.dmaapHostName = dmaapPublisherConfiguration.dmaapHostName();
63         this.dmaapProtocol = dmaapPublisherConfiguration.dmaapProtocol();
64         this.dmaapPortNumber = dmaapPublisherConfiguration.dmaapPortNumber();
65         this.dmaapTopicName = dmaapPublisherConfiguration.dmaapTopicName();
66         this.dmaapContentType = dmaapPublisherConfiguration.dmaapContentType();
67     }
68
69     /**
70      * Function for calling DMaaP HTTP producer - post request to DMaaP.
71      *
72      * @param consumerDmaapModelMono - object which will be sent to DMaaP
73      * @return status code of operation
74      */
75
76     public Mono<ResponseEntity<String>> getDMaaPProducerResponse(ConsumerDmaapModel consumerDmaapModelMono) {
77         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
81         });
82     }
83
84     private HttpHeaders getAllHeaders() {
85         HttpHeaders headers = new HttpHeaders();
86         headers.set(X_ONAP_REQUEST_ID, MDC.get(REQUEST_ID));
87         headers.set(X_INVOCATION_ID, UUID.randomUUID().toString());
88         headers.set(HttpHeaders.CONTENT_TYPE, dmaapContentType);
89         return headers;
90
91     }
92
93     public DMaaPProducerReactiveHttpClient createDMaaPWebClient(RestTemplate restTemplate) {
94         this.restTemplate = restTemplate;
95         return this;
96     }
97
98     URI getUri() {
99         return new DefaultUriBuilderFactory().builder().scheme(dmaapProtocol).host(dmaapHostName).port(dmaapPortNumber)
100             .path(dmaapTopicName).build();
101     }
102
103 }