3bae6989a3df798c41367ea474265c6c732bc18d
[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.consumer;
22
23 import java.io.IOException;
24 import java.net.URI;
25 import java.net.URISyntaxException;
26 import java.util.Optional;
27 import org.apache.http.HttpEntity;
28 import org.apache.http.client.ResponseHandler;
29 import org.apache.http.client.methods.HttpGet;
30 import org.apache.http.client.methods.HttpRequestBase;
31 import org.apache.http.client.utils.URIBuilder;
32 import org.apache.http.impl.client.CloseableHttpClient;
33 import org.apache.http.util.EntityUtils;
34 import org.onap.dcaegen2.services.prh.config.DmaapConsumerConfiguration;
35 import org.onap.dcaegen2.services.prh.service.DmaapHttpClientImpl;
36 import org.onap.dcaegen2.services.prh.service.HttpUtils;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40
41 public class ExtendedDmaapConsumerHttpClientImpl {
42
43     private static Logger logger = LoggerFactory.getLogger(ExtendedDmaapConsumerHttpClientImpl.class);
44
45     private final CloseableHttpClient closeableHttpClient;
46     private final String dmaapHostName;
47     private final String dmaapProtocol;
48     private final Integer dmaapPortNumber;
49     private final String dmaapTopicName;
50     private final String consumerGroup;
51     private final String consumerId;
52     private final String dmaapContentType;
53
54
55     public ExtendedDmaapConsumerHttpClientImpl(DmaapConsumerConfiguration configuration) {
56         this.closeableHttpClient = new DmaapHttpClientImpl(configuration).getHttpClient();
57         this.dmaapHostName = configuration.dmaapHostName();
58         this.dmaapProtocol = configuration.dmaapProtocol();
59         this.dmaapPortNumber = configuration.dmaapPortNumber();
60         this.dmaapTopicName = configuration.dmaapTopicName();
61         this.consumerGroup = configuration.consumerGroup();
62         this.consumerId = configuration.consumerId();
63         this.dmaapContentType = configuration.dmaapContentType();
64     }
65
66     public Optional<String> getHttpConsumerResponse() {
67
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, getDmaapConsumerResponseHandler());
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             ? createDmaapConsumerExtendedURI().map(this::createHttpRequest)
89             : Optional.empty();
90     }
91
92     private HttpRequestBase createHttpRequest(URI extendedURI) {
93         HttpRequestBase httpRequestBase = new HttpGet(extendedURI);
94         httpRequestBase.addHeader("Content-type", dmaapContentType);
95         return httpRequestBase;
96     }
97
98
99     private String createRequestPath() {
100         return dmaapTopicName + "/" + consumerGroup + "/" + consumerId;
101     }
102
103     private Optional<URI> createDmaapConsumerExtendedURI() throws URISyntaxException {
104         return Optional.ofNullable(new URIBuilder()
105             .setScheme(dmaapProtocol)
106             .setHost(dmaapHostName)
107             .setPort(dmaapPortNumber)
108             .setPath(createRequestPath()).build());
109     }
110
111     private ResponseHandler<Optional<String>> getDmaapConsumerResponseHandler() {
112         return httpResponse -> {
113             final int responseCode = httpResponse.getStatusLine().getStatusCode();
114             logger.info("Status code of operation: {}", responseCode);
115             final HttpEntity responseEntity = httpResponse.getEntity();
116
117             if (HttpUtils.isSuccessfulResponseCode(responseCode)) {
118                 logger.info("HTTP response successful.");
119                 final String dmaapResponse = EntityUtils.toString(responseEntity);
120                 return Optional.of(dmaapResponse);
121             } else {
122                 String dmaapResponse = responseEntity != null ? EntityUtils.toString(responseEntity) : "";
123                 logger.warn("HTTP response not successful : {}", dmaapResponse);
124                 return Optional.of(String.valueOf(responseCode));
125             }
126         };
127     }
128 }
129
130