b2a372491745eb194a713aa43b1d0200bd7eedb8
[dcaegen2/services/sdk.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * DCAEGEN2-SERVICES-SDK
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.sdk.rest.services.dmaap.client.service.producer;
22
23
24 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapPublisherConfiguration;
25 import org.onap.dcaegen2.services.sdk.rest.services.model.DmaapModel;
26 import org.onap.dcaegen2.services.sdk.rest.services.model.JsonBodyBuilder;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.slf4j.MDC;
30 import org.springframework.http.HttpEntity;
31 import org.springframework.http.HttpHeaders;
32 import org.springframework.http.HttpMethod;
33 import org.springframework.http.ResponseEntity;
34 import org.springframework.web.client.RestTemplate;
35 import org.springframework.web.util.DefaultUriBuilderFactory;
36 import reactor.core.publisher.Mono;
37
38 import java.net.URI;
39 import java.util.UUID;
40
41 import static org.onap.dcaegen2.services.sdk.rest.services.model.logging.MdcVariables.REQUEST_ID;
42 import static org.onap.dcaegen2.services.sdk.rest.services.model.logging.MdcVariables.X_INVOCATION_ID;
43 import static org.onap.dcaegen2.services.sdk.rest.services.model.logging.MdcVariables.X_ONAP_REQUEST_ID;
44
45
46 /**
47  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/4/18
48  */
49 public class DMaaPPublisherReactiveHttpClient {
50
51     private final Logger logger = LoggerFactory.getLogger(DMaaPPublisherReactiveHttpClient.class);
52     private final String dmaapHostName;
53     private final Integer dmaapPortNumber;
54     private final String dmaapProtocol;
55     private final String dmaapTopicName;
56     private final String dmaapContentType;
57     private final Mono<RestTemplate> restTemplateMono;
58     private final JsonBodyBuilder jsonBodyBuilder;
59
60     /**
61      * Constructor DMaaPPublisherReactiveHttpClient.
62      *
63      * @param dmaapPublisherConfiguration - DMaaP producer configuration object
64      */
65     DMaaPPublisherReactiveHttpClient(DmaapPublisherConfiguration dmaapPublisherConfiguration,
66                                      Mono<RestTemplate> restTemplateMono, JsonBodyBuilder jsonBodyBuilder) {
67         this.dmaapHostName = dmaapPublisherConfiguration.dmaapHostName();
68         this.dmaapProtocol = dmaapPublisherConfiguration.dmaapProtocol();
69         this.dmaapPortNumber = dmaapPublisherConfiguration.dmaapPortNumber();
70         this.dmaapTopicName = dmaapPublisherConfiguration.dmaapTopicName();
71         this.dmaapContentType = dmaapPublisherConfiguration.dmaapContentType();
72         this.restTemplateMono = restTemplateMono;
73         this.jsonBodyBuilder = jsonBodyBuilder;
74     }
75
76     /**
77      * Function for calling DMaaP HTTP producer - post request to DMaaP.
78      *
79      * @param dmaapModel - object which will be sent to DMaaP
80      * @return status code of operation
81      */
82
83     public Mono<ResponseEntity<String>> getDMaaPProducerResponse(DmaapModel dmaapModel) {
84         return Mono.defer(() -> {
85             HttpEntity<String> request = new HttpEntity<>(jsonBodyBuilder.createJsonBody(dmaapModel), getAllHeaders());
86             logger.info("Request: {} {}", getUri(), request);
87             return restTemplateMono.map(
88                 restTemplate -> restTemplate.exchange(getUri(), HttpMethod.POST, request, String.class));
89         });
90     }
91
92     private HttpHeaders getAllHeaders() {
93         HttpHeaders headers = new HttpHeaders();
94         headers.set(X_ONAP_REQUEST_ID, MDC.get(REQUEST_ID));
95         headers.set(X_INVOCATION_ID, UUID.randomUUID().toString());
96         headers.set(HttpHeaders.CONTENT_TYPE, dmaapContentType);
97         return headers;
98
99     }
100
101     URI getUri() {
102         return new DefaultUriBuilderFactory().builder().scheme(dmaapProtocol).host(dmaapHostName).port(dmaapPortNumber)
103                 .path(dmaapTopicName).build();
104     }
105
106 }