0d453e4770ef5bc7ea621b4fad43bd5c770e3bfa
[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.adapters.http.CloudHttpClient;
25 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpResponse;
26 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapPublisherConfiguration;
27 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.DMaaPAbstractReactiveHttpClient;
28 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.DMaaPClientServiceUtils;
29 import org.onap.dcaegen2.services.sdk.rest.services.model.DmaapModel;
30 import org.onap.dcaegen2.services.sdk.rest.services.model.JsonBodyBuilder;
31 import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext;
32 import org.onap.dcaegen2.services.sdk.rest.services.uri.URI.URIBuilder;
33 import reactor.core.publisher.Mono;
34
35 import java.net.URI;
36 import java.util.Map;
37 import java.util.Optional;
38
39
40 /**
41  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/4/18
42  */
43 public class DMaaPPublisherReactiveHttpClient extends DMaaPAbstractReactiveHttpClient {
44
45     private final DmaapPublisherConfiguration dmaapPublisherConfiguration;
46     private final JsonBodyBuilder jsonBodyBuilder;
47     private final CloudHttpClient cloudHttpClient;
48
49     /**
50      * Constructor DMaaPPublisherReactiveHttpClient.
51      *
52      * @param dmaapPublisherConfiguration - DMaaP producer configuration object
53      * @param cloudHttpClient             - cloudHttpClient sending http requests
54      */
55     DMaaPPublisherReactiveHttpClient(DmaapPublisherConfiguration dmaapPublisherConfiguration,
56                                      CloudHttpClient cloudHttpClient, JsonBodyBuilder jsonBodyBuilder) {
57         this.dmaapPublisherConfiguration = dmaapPublisherConfiguration;
58         this.cloudHttpClient = cloudHttpClient;
59         this.jsonBodyBuilder = jsonBodyBuilder;
60     }
61
62     /**
63      * Function for calling DMaaP HTTP producer - post request to DMaaP.
64      *
65      * @param dmaapModel - object which will be sent to DMaaP
66      * @return status code of operation
67      */
68
69     public Mono<HttpResponse> getDMaaPProducerResponse(
70             DmaapModel dmaapModel,
71             Optional<RequestDiagnosticContext> requestDiagnosticContextOptional) {
72         return Mono.defer(() -> {
73             Map<String, String> headers = DMaaPClientServiceUtils.getHeaders(dmaapPublisherConfiguration.dmaapContentType());
74             if (requestDiagnosticContextOptional.isPresent()) {
75                 cloudHttpClient
76                         .post(getUri().toString(), requestDiagnosticContextOptional.get(), headers, jsonBodyBuilder,
77                                 dmaapModel);
78             }
79             return cloudHttpClient
80                     .post(getUri().toString(), getRequestDiagnosticContext(), headers, jsonBodyBuilder, dmaapModel);
81         });
82     }
83
84
85     URI getUri() {
86         return URI.create(
87                 new URIBuilder().scheme(dmaapPublisherConfiguration.dmaapProtocol())
88                         .host(dmaapPublisherConfiguration.dmaapHostName()).port(dmaapPublisherConfiguration.dmaapPortNumber())
89                         .path(createRequestPath())
90                         .build().toString());
91     }
92
93     private String createRequestPath() {
94         return new StringBuilder().append(SLASH).append(dmaapPublisherConfiguration.dmaapTopicName()).toString();
95     }
96
97 }