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