d696deefedcd55e73da77ef5eacf6e9723cb2f5c
[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.impl.client.CloseableHttpClient;
30 import org.onap.holmes.common.api.stat.VesAlarm;
31 import org.onap.holmes.common.dropwizard.ioc.utils.ServiceLocatorHolder;
32 import org.onap.holmes.common.exception.CorrelationException;
33 import org.onap.holmes.common.utils.GsonUtil;
34 import org.onap.holmes.common.utils.HttpsUtils;
35
36 @Getter
37 @Setter
38 @Slf4j
39 public class Subscriber {
40
41     private DMaaPResponseUtil dMaaPResponseUtil = ServiceLocatorHolder.getLocator()
42             .getService(DMaaPResponseUtil.class);
43
44     /**
45      * The number of milliseconds to wait for messages if none are immediately available. This
46      * should normally be used, and set at 15000 or higher.
47      */
48     private int timeout = 15000;
49
50     /**
51      * The maximum number of messages to return
52      */
53     private int limit = 100;
54
55     /**
56      * The number of milliseconds to poll interval time. This should normally be used, and set at
57      * 15000 or higher.
58      */
59     private int period = 15000;
60
61     private boolean secure;
62     private String topic;
63     private String url;
64     private String uuid = UUID.randomUUID() + "";
65     private String consumerGroup = "homlesGroup" + uuid;
66     private String consumer = "homles" + uuid;
67     private String authInfo;
68     private String authExpDate;
69
70     public List<VesAlarm> subscribe() throws CorrelationException {
71         List<String> response;
72         try {
73             response = getDMaaPData();
74         } catch (Exception e) {
75             throw new CorrelationException("Failed to get data from DMaaP.", e);
76         }
77         try {
78             return extractVesAlarm(response);
79         } catch (Exception e) {
80             throw new CorrelationException("Failed to convert the response data to VES alarms.", e);
81         }
82     }
83
84     private List<String> getDMaaPData() throws Exception {
85         String response;
86         CloseableHttpClient closeableHttpClient = null;
87         try {
88             closeableHttpClient = HttpsUtils.getHttpClient(timeout);
89             HttpResponse httpResponse = HttpsUtils
90                     .get(url + "/" + consumerGroup + "/" + consumer, new HashMap<>(), closeableHttpClient);
91             response = HttpsUtils.extractResponseEntity(httpResponse);
92         } catch (Exception e) {
93             throw e;
94         } finally {
95             if (closeableHttpClient != null) {
96                 try {
97                     closeableHttpClient.close();
98                 } catch (IOException e) {
99                     log.warn("Failed to close http client!");
100                 }
101             }
102         }
103         return GsonUtil.jsonToBean(response, List.class);
104     }
105
106     private List<VesAlarm> extractVesAlarm(List<String> responseEntity) throws IOException {
107         List<VesAlarm> vesAlarmList = new ArrayList<>();
108         for (String entity : responseEntity) {
109             vesAlarmList.add(dMaaPResponseUtil.convertJsonToVesAlarm(entity));
110         }
111         return vesAlarmList;
112     }
113 }