Updated holmes-actions to 1.3.5
[holmes/engine-management.git] / engine-d / 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 lombok.Getter;
20 import lombok.Setter;
21 import lombok.extern.slf4j.Slf4j;
22 import org.onap.holmes.common.api.stat.VesAlarm;
23 import org.onap.holmes.common.dropwizard.ioc.utils.ServiceLocatorHolder;
24 import org.onap.holmes.common.exception.CorrelationException;
25 import org.onap.holmes.common.utils.JerseyClient;
26
27 import java.io.IOException;
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.UUID;
31
32 @Getter
33 @Setter
34 @Slf4j
35 public class Subscriber {
36
37     private DMaaPResponseUtil dMaaPResponseUtil = ServiceLocatorHolder.getLocator()
38             .getService(DMaaPResponseUtil.class);
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 = timeout;
56
57     private boolean secure;
58     private String topic;
59     private String url;
60     private String uuid = UUID.randomUUID() + "";
61     private String consumerGroup = "homlesGroup" + uuid;
62     private String consumer = "homles" + uuid;
63     private String authInfo;
64     private String authExpDate;
65
66     public List<VesAlarm> subscribe() throws CorrelationException {
67         List<String> response;
68         try {
69             response = getDMaaPData();
70         } catch (Exception e) {
71             throw new CorrelationException("Failed to get data from DMaaP.", e);
72         }
73         try {
74             return extractVesAlarm(response);
75         } catch (Exception e) {
76             throw new CorrelationException("Failed to convert the response data to VES alarms.", e);
77         }
78     }
79
80     private List<String> getDMaaPData() {
81         return JerseyClient.newInstance()
82                 .path(consumerGroup)
83                 .path(consumer)
84                 .queryParam("timeout", period)
85                 .get(url, List.class);
86     }
87
88     private List<VesAlarm> extractVesAlarm(List<String> responseEntity) {
89         List<VesAlarm> vesAlarmList = new ArrayList<>();
90         for (String entity : responseEntity) {
91             vesAlarmList.add(dMaaPResponseUtil.convertJsonToVesAlarm(entity));
92         }
93         return vesAlarmList;
94     }
95 }