5704cd11c533848b608161d8184795a8fcf8b75e
[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 org.apache.http.HttpEntity;
24 import org.apache.http.HttpResponse;
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.prh.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.prh.model.utils.HttpUtils;
35 import org.onap.dcaegen2.services.prh.service.DmaapHttpClientImpl;
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     private ConsumerDmaapModel consumerDmaapModel;
56
57
58     public ExtendedDmaapProducerHttpClientImpl(DmaapPublisherConfiguration configuration) {
59         this.closeableHttpClient = new DmaapHttpClientImpl(configuration).getHttpClient();
60         this.dmaapHostName = configuration.dmaapHostName();
61         this.dmaapProtocol = configuration.dmaapProtocol();
62         this.dmaapPortNumber = configuration.dmaapPortNumber();
63         this.dmaapTopicName = configuration.dmaapTopicName();
64         this.dmaapContentType = configuration.dmaapContentType();
65     }
66
67     public Optional<Integer> getHttpProducerResponse(ConsumerDmaapModel consumerDmaapModel) {
68         this.consumerDmaapModel = consumerDmaapModel;
69         try {
70             return createRequest()
71                 .flatMap(this::executeHttpClient);
72         } catch (URISyntaxException e) {
73             logger.warn("Exception while executing HTTP request: ", e);
74         }
75         return Optional.empty();
76     }
77
78     private Optional<Integer> executeHttpClient(HttpRequestBase httpRequestBase) {
79         try {
80             return closeableHttpClient.execute(httpRequestBase, this::handleResponse);
81         } catch (IOException e) {
82             logger.warn("Exception while executing HTTP request: ", e);
83         }
84         return Optional.empty();
85     }
86
87     private Optional<HttpRequestBase> createRequest() throws URISyntaxException {
88         return "application/json".equals(dmaapContentType)
89             ? createDmaapPublisherExtendedURI().map(this::createHttpPostRequest)
90             : Optional.empty();
91     }
92
93     private Optional<URI> createDmaapPublisherExtendedURI() throws URISyntaxException {
94         return Optional.ofNullable(new URIBuilder()
95             .setScheme(dmaapProtocol)
96             .setHost(dmaapHostName)
97             .setPort(dmaapPortNumber)
98             .setPath(dmaapTopicName).build());
99     }
100
101     private HttpPost createHttpPostRequest(URI extendedURI) {
102         HttpPost post = new HttpPost(extendedURI);
103         post.addHeader("Content-type", dmaapContentType);
104         createStringEntity().ifPresent(post::setEntity);
105         return post;
106     }
107
108     private Optional<StringEntity> createStringEntity() {
109         try {
110             return Optional.of(new StringEntity(CommonFunctions.createJsonBody(consumerDmaapModel)));
111         } catch (UnsupportedEncodingException | IllegalArgumentException e) {
112             logger.warn("Exception while parsing JSON: ", e);
113         }
114         return Optional.empty();
115     }
116
117     Optional<Integer> handleResponse(HttpResponse response) throws IOException {
118
119         final Integer responseCode = response.getStatusLine().getStatusCode();
120         logger.info("Status code of operation: {}", responseCode);
121         final HttpEntity responseEntity = response.getEntity();
122
123         if (HttpUtils.isSuccessfulResponseCode(responseCode)) {
124             logger.trace("HTTP response successful.");
125             return Optional.of(responseCode);
126         } else {
127             String aaiResponse = responseEntity != null ? EntityUtils.toString(responseEntity) : "";
128             logger.warn("HTTP response not successful : {}", aaiResponse);
129             return Optional.of(responseCode);
130         }
131     }
132 }