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;
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.List;
22 import javax.ws.rs.client.Client;
23 import javax.ws.rs.client.ClientBuilder;
24 import javax.ws.rs.client.WebTarget;
25 import javax.ws.rs.core.Response;
28 import org.glassfish.jersey.client.ClientConfig;
29 import org.onap.holmes.common.api.stat.VesAlarm;
30 import org.onap.holmes.common.dropwizard.ioc.utils.ServiceLocatorHolder;
31 import org.onap.holmes.common.exception.CorrelationException;
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 = 15000;
57 private boolean secure;
60 private String consumerGroup = "homlesGroup1";
61 private String consumer = "homles1";
62 private String authInfo;
63 private String authExpDate;
65 public List<VesAlarm> subscribe() throws CorrelationException {
66 List<String> response;
68 response = getDMaaPData();
69 } catch (Exception e) {
70 throw new CorrelationException("Failed to get data from DMaaP.", e);
73 return extractVesAlarm(response);
74 } catch (Exception e) {
75 throw new CorrelationException("Failed to convert the response data to VES alarms.", e);
79 private List<String> getDMaaPData() {
80 Client client = ClientBuilder.newClient(new ClientConfig());
81 WebTarget webTarget = client.target(url + "/" + consumerGroup + "/" + consumer);
82 Response response = webTarget.queryParam("timeout", timeout).request().get();
83 return response.readEntity(List.class);
86 private List<VesAlarm> extractVesAlarm(List<String> responseEntity) throws IOException {
87 List<VesAlarm> vesAlarmList = new ArrayList<>();
88 for (String entity : responseEntity) {
89 vesAlarmList.add(dMaaPResponseUtil.convertJsonToVesAlarm(entity));