11d5a4877a5af6d9f32472b0fc66eddf89304d74
[dcaegen2/services/prh.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * PNF-REGISTRATION-HANDLER
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.service.producer;
22
23 import org.apache.http.HttpEntity;
24 import org.apache.http.client.ResponseHandler;
25 import org.apache.http.client.methods.HttpPost;
26 import org.apache.http.client.methods.HttpRequestBase;
27 import org.apache.http.client.utils.URIBuilder;
28 import org.apache.http.entity.StringEntity;
29 import org.apache.http.impl.client.CloseableHttpClient;
30 import org.apache.http.util.EntityUtils;
31 import org.onap.dcaegen2.services.config.DmaapPublisherConfiguration;
32 import org.onap.dcaegen2.services.prh.model.CommonFunctions;
33 import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel;
34 import org.onap.dcaegen2.services.service.DmaapHttpClientImpl;
35 import org.onap.dcaegen2.services.service.HttpUtils;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 import java.io.IOException;
40 import java.io.UnsupportedEncodingException;
41 import java.net.URI;
42 import java.net.URISyntaxException;
43 import java.util.Optional;
44
45 public class ExtendedDmaapProducerHttpClientImpl {
46
47     private static Logger logger = LoggerFactory.getLogger(ExtendedDmaapProducerHttpClientImpl.class);
48
49     private final CloseableHttpClient closeableHttpClient;
50     private final String dmaapHostName;
51     private final String dmaapProtocol;
52     private final Integer dmaapPortNumber;
53     private final String dmaapTopicName;
54     private final String dmaapContentType;
55
56
57     public ExtendedDmaapProducerHttpClientImpl(DmaapPublisherConfiguration configuration) {
58         this.closeableHttpClient = new DmaapHttpClientImpl(configuration).getHttpClient();
59         this.dmaapHostName = configuration.dmaapHostName();
60         this.dmaapProtocol = configuration.dmaapProtocol();
61         this.dmaapPortNumber = configuration.dmaapPortNumber();
62         this.dmaapTopicName = configuration.dmaapTopicName();
63         this.dmaapContentType = configuration.dmaapContentType();
64     }
65
66     public Optional<String> getHttpProducerResponse(ConsumerDmaapModel consumerDmaapModel) {
67         Optional<String> extendedDetails = Optional.empty();
68         Optional<HttpRequestBase> request = createRequest(consumerDmaapModel);
69         try {
70             extendedDetails = closeableHttpClient.execute(request.get(), dmaapProducerResponseHandler());
71         } catch (IOException | NullPointerException e) {
72             logger.warn("Exception while executing HTTP request: ", e);
73         }
74         return extendedDetails;
75     }
76     
77     private Optional<StringEntity> createStringEntity(Optional<String> jsonBody) {
78         return Optional.of(parseJson(jsonBody).get());
79     }
80
81     private Optional<StringEntity> parseJson(Optional<String> jsonBody) {
82         Optional<StringEntity> stringEntity = Optional.empty();
83         try {
84             stringEntity = Optional.of(new StringEntity(jsonBody.get()));
85         } catch (UnsupportedEncodingException e) {
86             logger.warn("Exception while parsing JSON: ", e);
87         }
88         return stringEntity;
89     }
90
91     private Optional<HttpRequestBase> createRequest(ConsumerDmaapModel consumerDmaapModel) {
92         Optional<HttpRequestBase> request = Optional.empty();
93         final URI extendedURI = createDmaapPublisherExtendedURI();
94
95         if ("application/json".equals(dmaapContentType)) {
96             request = Optional.ofNullable(createRequest(extendedURI, consumerDmaapModel));
97             request.get().addHeader("Content-type", dmaapContentType);
98         }
99
100         return request;
101     }
102
103     private URI createDmaapPublisherExtendedURI() {
104         URI extendedURI = null;
105         final URIBuilder uriBuilder = new URIBuilder()
106             .setScheme(dmaapProtocol)
107             .setHost(dmaapHostName)
108             .setPort(dmaapPortNumber)
109             .setPath(dmaapTopicName);
110         try {
111             extendedURI = uriBuilder.build();
112             logger.trace("Building extended URI: {}", extendedURI);
113         } catch (URISyntaxException e) {
114             logger.warn("Exception while building extended URI: ", e);
115         }
116         return extendedURI;
117     }
118
119     private HttpRequestBase createRequest(URI extendedURI, ConsumerDmaapModel consumerDmaapModel) {
120         if (extendedURI != null) {
121             return createHttpPost(extendedURI, Optional.ofNullable(CommonFunctions.createJsonBody(consumerDmaapModel)));
122         } else {
123             return null;
124         }
125     }
126
127     private HttpPost createHttpPost(URI extendedURI, Optional<String> jsonBody) {
128         HttpPost post = new HttpPost(extendedURI);
129         Optional<StringEntity> stringEntity = createStringEntity(jsonBody);
130         post.setEntity(stringEntity.get());
131         return post;
132     }
133
134     private ResponseHandler<Optional<String>> dmaapProducerResponseHandler() {
135         return httpResponse -> {
136             final int responseCode = httpResponse.getStatusLine().getStatusCode();
137             final HttpEntity responseEntity = httpResponse.getEntity();
138
139             if (HttpUtils.isSuccessfulResponseCode(responseCode)) {
140                 logger.trace("HTTP response successful.");
141                 return Optional.of("" + responseCode);
142             } else {
143                 String response = responseEntity != null ? EntityUtils.toString(responseEntity) : "";
144                 logger.warn("HTTP response not successful : {}", response);
145                 return Optional.of("" + responseCode);
146             }
147         };
148     }
149 }