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