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