0b61f9c24d37849c1f717134d16a4a0d3d9b1390
[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.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;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.slf4j.MDC;
37 import org.springframework.http.HttpEntity;
38 import org.springframework.http.HttpHeaders;
39 import org.springframework.http.HttpMethod;
40 import org.springframework.http.ResponseEntity;
41 import org.springframework.web.client.RestTemplate;
42 import reactor.core.publisher.Mono;
43
44 /**
45  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/4/18
46  */
47 public class DMaaPProducerReactiveHttpClient {
48
49     private final Logger logger = LoggerFactory.getLogger(this.getClass());
50
51     private final String dmaapHostName;
52     private final Integer dmaapPortNumber;
53     private final String dmaapProtocol;
54     private final String dmaapTopicName;
55     private final String dmaapContentType;
56     private RestTemplate restTemplate;
57
58     /**
59      * Constructor DMaaPProducerReactiveHttpClient.
60      *
61      * @param dmaapPublisherConfiguration - DMaaP producer configuration object
62      */
63     public DMaaPProducerReactiveHttpClient(DmaapPublisherConfiguration dmaapPublisherConfiguration) {
64         this.dmaapHostName = dmaapPublisherConfiguration.dmaapHostName();
65         this.dmaapProtocol = dmaapPublisherConfiguration.dmaapProtocol();
66         this.dmaapPortNumber = dmaapPublisherConfiguration.dmaapPortNumber();
67         this.dmaapTopicName = dmaapPublisherConfiguration.dmaapTopicName();
68         this.dmaapContentType = dmaapPublisherConfiguration.dmaapContentType();
69     }
70
71     /**
72      * Function for calling DMaaP HTTP producer - post request to DMaaP.
73      *
74      * @param consumerDmaapModelMono - object which will be sent to DMaaP
75      * @return status code of operation
76      */
77
78     public Mono<ResponseEntity<String>> getDMaaPProducerResponse(ConsumerDmaapModel consumerDmaapModelMono) {
79         return Mono.defer(() -> {
80             try {
81                 HttpEntity<String> request = new HttpEntity<>(createJsonBody(consumerDmaapModelMono), getAllHeaders());
82                 return Mono.just(restTemplate.exchange(getUri(), HttpMethod.POST, request, String.class));
83             } catch (URISyntaxException e) {
84                 logger.warn("Exception while evaluating URI");
85                 return Mono.error(e);
86             }
87         });
88     }
89
90     private HttpHeaders getAllHeaders() {
91         HttpHeaders headers = new HttpHeaders();
92         headers.set(X_ONAP_REQUEST_ID, MDC.get(REQUEST_ID));
93         headers.set(X_INVOCATION_ID, UUID.randomUUID().toString());
94         headers.set(HttpHeaders.CONTENT_TYPE, dmaapContentType);
95         return headers;
96
97     }
98
99     public DMaaPProducerReactiveHttpClient createDMaaPWebClient(RestTemplate restTemplate) {
100         this.restTemplate = restTemplate;
101         return this;
102     }
103
104     URI getUri() throws URISyntaxException {
105         return new URIBuilder().setScheme(dmaapProtocol).setHost(dmaapHostName).setPort(dmaapPortNumber)
106             .setPath(dmaapTopicName).build();
107     }
108
109 }