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