bea36201dd432562a76d5f70a257bdb8c2c036ab
[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.prh.service.producer;
22
23 import java.io.IOException;
24 import java.io.UnsupportedEncodingException;
25 import java.net.URI;
26 import java.net.URISyntaxException;
27 import java.util.Optional;
28 import org.apache.http.HttpEntity;
29 import org.apache.http.HttpResponse;
30 import org.apache.http.client.methods.HttpPost;
31 import org.apache.http.client.methods.HttpRequestBase;
32 import org.apache.http.client.utils.URIBuilder;
33 import org.apache.http.entity.StringEntity;
34 import org.apache.http.impl.client.CloseableHttpClient;
35 import org.apache.http.util.EntityUtils;
36 import org.onap.dcaegen2.services.prh.config.DmaapPublisherConfiguration;
37 import org.onap.dcaegen2.services.prh.model.CommonFunctions;
38 import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel;
39 import org.onap.dcaegen2.services.prh.service.DmaapHttpClientImpl;
40 import org.onap.dcaegen2.services.prh.service.HttpUtils;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 public class ExtendedDmaapProducerHttpClientImpl {
45
46     private static Logger logger = LoggerFactory.getLogger(ExtendedDmaapProducerHttpClientImpl.class);
47
48     private final CloseableHttpClient closeableHttpClient;
49     private final String dmaapHostName;
50     private final String dmaapProtocol;
51     private final Integer dmaapPortNumber;
52     private final String dmaapTopicName;
53     private final String dmaapContentType;
54     private ConsumerDmaapModel consumerDmaapModel;
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         this.consumerDmaapModel = consumerDmaapModel;
68         try {
69             return createRequest()
70                 .flatMap(this::executeHttpClient);
71         } catch (NullPointerException | URISyntaxException e) {
72             logger.warn("Exception while executing HTTP request: ", e);
73         }
74         return Optional.empty();
75     }
76
77     private Optional<String> executeHttpClient(HttpRequestBase httpRequestBase) {
78         try {
79             return closeableHttpClient.execute(httpRequestBase, this::getDmaapProducerResponseHandler);
80         } catch (IOException e) {
81             logger.warn("Exception while executing HTTP request: ", e);
82         }
83         return Optional.empty();
84     }
85
86     private Optional<HttpRequestBase> createRequest() throws URISyntaxException {
87         return "application/json".equals(dmaapContentType)
88             ? createDmaapPublisherExtendedURI().map(this::createHttpPostRequest)
89             : Optional.empty();
90     }
91
92     private Optional<URI> createDmaapPublisherExtendedURI() throws URISyntaxException {
93         return Optional.ofNullable(new URIBuilder()
94             .setScheme(dmaapProtocol)
95             .setHost(dmaapHostName)
96             .setPort(dmaapPortNumber)
97             .setPath(dmaapTopicName).build());
98     }
99
100     private HttpPost createHttpPostRequest(URI extendedURI) {
101         HttpPost post = new HttpPost(extendedURI);
102         post.addHeader("Content-type", dmaapContentType);
103         createStringEntity().ifPresent(post::setEntity);
104         return post;
105     }
106
107     private Optional<StringEntity> createStringEntity() {
108         try {
109             return Optional.of(new StringEntity(CommonFunctions.createJsonBody(consumerDmaapModel)));
110         } catch (UnsupportedEncodingException | IllegalArgumentException e) {
111             logger.warn("Exception while parsing JSON: ", e);
112         }
113         return Optional.empty();
114     }
115
116     private Optional<String> getDmaapProducerResponseHandler(HttpResponse httpResponse) throws IOException {
117             final int responseCode = httpResponse.getStatusLine().getStatusCode();
118             final HttpEntity responseEntity = httpResponse.getEntity();
119
120             if (HttpUtils.isSuccessfulResponseCode(responseCode)) {
121                 logger.trace("HTTP response successful.");
122                 return Optional.of("" + responseCode);
123             } else {
124                 String response = responseEntity != null ? EntityUtils.toString(responseEntity) : "";
125                 logger.warn("HTTP response not successful : {}", response);
126                 return Optional.of("" + responseCode);
127             }
128     }
129 }