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