7173624df83704bcd11cdac0e5a64c0f882967c7
[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  * @deprecated Use new API {@link org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.api.DmaapClientFactory}
43  */
44 @Deprecated
45 public class DMaaPPublisherReactiveHttpClient extends DMaaPAbstractReactiveHttpClient {
46
47     private final DmaapPublisherConfiguration dmaapPublisherConfiguration;
48     private final JsonBodyBuilder jsonBodyBuilder;
49     private final CloudHttpClient cloudHttpClient;
50
51     /**
52      * Constructor DMaaPPublisherReactiveHttpClient.
53      *
54      * @param dmaapPublisherConfiguration - DMaaP producer configuration object
55      * @param cloudHttpClient             - cloudHttpClient sending http requests
56      */
57     DMaaPPublisherReactiveHttpClient(DmaapPublisherConfiguration dmaapPublisherConfiguration,
58                                      CloudHttpClient cloudHttpClient, JsonBodyBuilder jsonBodyBuilder) {
59         this.dmaapPublisherConfiguration = dmaapPublisherConfiguration;
60         this.cloudHttpClient = cloudHttpClient;
61         this.jsonBodyBuilder = jsonBodyBuilder;
62     }
63
64     /**
65      * Function for calling DMaaP HTTP producer - post request to DMaaP.
66      *
67      * @param dmaapModel - object which will be sent to DMaaP
68      * @return status code of operation
69      */
70
71     public Mono<HttpResponse> getDMaaPProducerResponse(
72             DmaapModel dmaapModel,
73             Optional<RequestDiagnosticContext> requestDiagnosticContextOptional) {
74         return Mono.defer(() -> {
75             Map<String, String> headers = DMaaPClientServiceUtils.getHeaders(dmaapPublisherConfiguration.dmaapContentType());
76             if (requestDiagnosticContextOptional.isPresent()) {
77                 cloudHttpClient
78                         .post(getUri().toString(), requestDiagnosticContextOptional.get(), headers, jsonBodyBuilder,
79                                 dmaapModel);
80             }
81             return cloudHttpClient
82                     .post(getUri().toString(), getRequestDiagnosticContext(), headers, jsonBodyBuilder, dmaapModel);
83         });
84     }
85
86
87     URI getUri() {
88         return URI.create(
89                 new URIBuilder().scheme(dmaapPublisherConfiguration.dmaapProtocol())
90                         .host(dmaapPublisherConfiguration.dmaapHostName()).port(dmaapPublisherConfiguration.dmaapPortNumber())
91                         .path(createRequestPath())
92                         .build().toString());
93     }
94
95     private String createRequestPath() {
96         return new StringBuilder().append(SLASH).append(dmaapPublisherConfiguration.dmaapTopicName()).toString();
97     }
98
99 }