modify method extractVesAlarm to private
[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.List;
22 import javax.inject.Inject;
23 import javax.ws.rs.client.Client;
24 import javax.ws.rs.client.ClientBuilder;
25 import javax.ws.rs.client.WebTarget;
26 import javax.ws.rs.core.Response;
27 import lombok.Getter;
28 import lombok.Setter;
29 import org.glassfish.jersey.client.ClientConfig;
30 import org.onap.holmes.common.api.stat.VesAlarm;
31 import org.onap.holmes.common.exception.CorrelationException;
32
33 @Getter
34 @Setter
35 public class Subscriber {
36
37     @Inject
38     private DMaaPResponseUtil dMaaPResponseUtil;
39
40     /**
41      * The number of milliseconds to wait for messages if none are immediately available. This
42      * should normally be used, and set at 15000 or higher.
43      */
44     private int timeout = 15000;
45
46     /**
47      * The maximum number of messages to return
48      */
49     private int limit = 100;
50
51     /**
52      * The number of milliseconds to poll interval time. This should normally be used, and set at
53      * 15000 or higher.
54      */
55     private int period = 15000;
56
57     private boolean secure;
58     private String topic;
59     private String url;
60     private String consumerGroup = "g0";
61     private String consumer = "u1";
62     private String authInfo;
63     private String authExpDate;
64
65     public List<VesAlarm> subscribe() throws CorrelationException {
66         List<String> response = getDMaaPData();
67         try {
68             return extractVesAlarm(response);
69         } catch (Exception e) {
70             throw new CorrelationException("Failed to convert the response data to VES alarms.", e);
71         }
72     }
73
74     private List<String> getDMaaPData() {
75         Client client = ClientBuilder.newClient(new ClientConfig());
76         WebTarget webTarget = client.target(url);
77         Response response = webTarget.path(topic).path(consumerGroup).path(consumer).request().get();
78         return response.readEntity(List.class);
79     }
80
81     private List<VesAlarm> extractVesAlarm(List<String> responseEntity) throws IOException {
82         List<VesAlarm> vesAlarmList = new ArrayList<>();
83         for (String entity : responseEntity) {
84             vesAlarmList.add(dMaaPResponseUtil.convertJsonToVesAlarm(entity));
85         }
86         return vesAlarmList;
87     }
88 }