2 * Copyright 2017 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.dropwizard.ioc.utils.ServiceLocatorHolder;
24 import org.onap.holmes.common.exception.CorrelationException;
25 import org.onap.holmes.common.utils.JerseyClient;
27 import java.io.IOException;
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.UUID;
35 public class Subscriber {
37 private DMaaPResponseUtil dMaaPResponseUtil = ServiceLocatorHolder.getLocator()
38 .getService(DMaaPResponseUtil.class);
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.
44 private int timeout = 15000;
47 * The maximum number of messages to return
49 private int limit = 100;
52 * The number of milliseconds to poll interval time. This should normally be used, and set at
55 private int period = timeout;
57 private boolean secure;
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;
66 public List<VesAlarm> subscribe() throws CorrelationException {
67 List<String> response;
69 response = getDMaaPData();
70 } catch (Exception e) {
71 throw new CorrelationException("Failed to get data from DMaaP.", e);
74 return extractVesAlarm(response);
75 } catch (Exception e) {
76 throw new CorrelationException("Failed to convert the response data to VES alarms.", e);
80 private List<String> getDMaaPData() {
81 return JerseyClient.newInstance()
84 .queryParam("timeout", period)
85 .get(url, List.class);
88 private List<VesAlarm> extractVesAlarm(List<String> responseEntity) {
89 List<VesAlarm> vesAlarmList = new ArrayList<>();
90 for (String entity : responseEntity) {
92 vesAlarmList.add(dMaaPResponseUtil.convertJsonToVesAlarm(entity));
93 } catch (Exception e) {
94 log.error("Failed to convert the response data to VES alarm ", e);
95 //Continue with other events