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