2 * Copyright 2017-2022 ZTE Corporation.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.holmes.dsa.dmaappolling;
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;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.UUID;
34 public class Subscriber {
36 private DMaaPResponseUtil dMaaPResponseUtil = SpringContextUtil.getBean(DMaaPResponseUtil.class);
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.
42 private int timeout = 15000;
45 * The maximum number of messages to return
47 private int limit = 100;
50 * The number of milliseconds to poll interval time. This should normally be used, and set at
53 private int period = timeout;
55 private boolean secure;
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;
64 public List<VesAlarm> subscribe() throws CorrelationException {
65 List<String> response;
67 response = getDMaaPData();
68 } catch (Exception e) {
69 throw new CorrelationException("Failed to get data from DMaaP.", e);
72 return extractVesAlarm(response);
73 } catch (Exception e) {
74 throw new CorrelationException("Failed to convert the response data to VES alarms.", e);
78 private List<String> getDMaaPData() {
79 return JerseyClient.newInstance()
82 .queryParam("timeout", period)
83 .get(url, List.class);
86 private List<VesAlarm> extractVesAlarm(List<String> responseEntity) {
87 List<VesAlarm> vesAlarmList = new ArrayList<>();
88 for (String entity : responseEntity) {
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