Fixed a DMaaP Request Problem
[holmes/dsa.git] / dmaap-dsa / src / main / java / org / onap / holmes / dsa / dmaappolling / Subscriber.java
1 /*
2  * Copyright 2017 ZTE Corporation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.holmes.dsa.dmaappolling;
18
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.UUID;
24
25 import lombok.Getter;
26 import lombok.Setter;
27 import lombok.extern.slf4j.Slf4j;
28 import org.apache.http.HttpResponse;
29 import org.apache.http.client.methods.HttpGet;
30 import org.apache.http.impl.client.CloseableHttpClient;
31 import org.onap.holmes.common.api.stat.VesAlarm;
32 import org.onap.holmes.common.dropwizard.ioc.utils.ServiceLocatorHolder;
33 import org.onap.holmes.common.exception.CorrelationException;
34 import org.onap.holmes.common.utils.GsonUtil;
35 import org.onap.holmes.common.utils.HttpsUtils;
36
37 @Getter
38 @Setter
39 @Slf4j
40 public class Subscriber {
41
42     private DMaaPResponseUtil dMaaPResponseUtil = ServiceLocatorHolder.getLocator()
43             .getService(DMaaPResponseUtil.class);
44
45     /**
46      * The number of milliseconds to wait for messages if none are immediately available. This
47      * should normally be used, and set at 15000 or higher.
48      */
49     private int timeout = 15000;
50
51     /**
52      * The maximum number of messages to return
53      */
54     private int limit = 100;
55
56     /**
57      * The number of milliseconds to poll interval time. This should normally be used, and set at
58      * 15000 or higher.
59      */
60     private int period = timeout;
61
62     private boolean secure;
63     private String topic;
64     private String url;
65     private String uuid = UUID.randomUUID() + "";
66     private String consumerGroup = "homlesGroup" + uuid;
67     private String consumer = "homles" + uuid;
68     private String authInfo;
69     private String authExpDate;
70
71     public List<VesAlarm> subscribe() throws CorrelationException {
72         List<String> response;
73         try {
74             response = getDMaaPData();
75         } catch (Exception e) {
76             throw new CorrelationException("Failed to get data from DMaaP.", e);
77         }
78         try {
79             return extractVesAlarm(response);
80         } catch (Exception e) {
81             throw new CorrelationException("Failed to convert the response data to VES alarms.", e);
82         }
83     }
84
85     private List<String> getDMaaPData() throws Exception {
86         String response;
87         CloseableHttpClient closeableHttpClient = null;
88         HttpGet httpGet = new HttpGet(url + "/" + consumerGroup + "/" + consumer + "?timeout=" + period);
89         try {
90             closeableHttpClient = HttpsUtils.getHttpClient(timeout);
91             HttpResponse httpResponse = HttpsUtils
92                     .get(httpGet, new HashMap<>(), closeableHttpClient);
93             response = HttpsUtils.extractResponseEntity(httpResponse);
94         } catch (Exception e) {
95             throw e;
96         } finally {
97             httpGet.releaseConnection();
98             if (closeableHttpClient != null) {
99                 try {
100                     closeableHttpClient.close();
101                 } catch (IOException e) {
102                     log.warn("Failed to close http client!");
103                 }
104             }
105         }
106         return GsonUtil.jsonToBean(response, List.class);
107     }
108
109     private List<VesAlarm> extractVesAlarm(List<String> responseEntity) throws IOException {
110         List<VesAlarm> vesAlarmList = new ArrayList<>();
111         for (String entity : responseEntity) {
112             vesAlarmList.add(dMaaPResponseUtil.convertJsonToVesAlarm(entity));
113         }
114         return vesAlarmList;
115     }
116 }