Switched from Dropwizard to Springboot
[holmes/engine-management.git] / engine-d / src / main / java / org / onap / holmes / dsa / dmaappolling / Subscriber.java
1 /*
2  * Copyright 2017-2022 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.exception.CorrelationException;
24 import org.onap.holmes.common.utils.JerseyClient;
25 import org.onap.holmes.common.utils.SpringContextUtil;
26
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.UUID;
30
31 @Getter
32 @Setter
33 @Slf4j
34 public class Subscriber {
35
36     private DMaaPResponseUtil dMaaPResponseUtil = SpringContextUtil.getBean(DMaaPResponseUtil.class);
37
38     /**
39      * The number of milliseconds to wait for messages if none are immediately available. This
40      * should normally be used, and set at 15000 or higher.
41      */
42     private int timeout = 15000;
43
44     /**
45      * The maximum number of messages to return
46      */
47     private int limit = 100;
48
49     /**
50      * The number of milliseconds to poll interval time. This should normally be used, and set at
51      * 15000 or higher.
52      */
53     private int period = timeout;
54
55     private boolean secure;
56     private String topic;
57     private String url;
58     private String uuid = UUID.randomUUID() + "";
59     private String consumerGroup = "homlesGroup" + uuid;
60     private String consumer = "homles" + uuid;
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() {
79         return JerseyClient.newInstance()
80                 .path(consumerGroup)
81                 .path(consumer)
82                 .queryParam("timeout", period)
83                 .get(url, List.class);
84     }
85
86     private List<VesAlarm> extractVesAlarm(List<String> responseEntity) {
87         List<VesAlarm> vesAlarmList = new ArrayList<>();
88         for (String entity : responseEntity) {
89             try {
90                 vesAlarmList.add(dMaaPResponseUtil.convertJsonToVesAlarm(entity));
91             } catch (Exception e) {
92                 log.error("Failed to convert the response data to VES alarm ", e);
93                 //Continue with other events
94             }
95         }
96         return vesAlarmList;
97     }
98 }