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.apache.http.HttpResponse;
23 import org.apache.http.client.methods.HttpGet;
24 import org.apache.http.impl.client.CloseableHttpClient;
25 import org.onap.holmes.common.api.stat.VesAlarm;
26 import org.onap.holmes.common.dropwizard.ioc.utils.ServiceLocatorHolder;
27 import org.onap.holmes.common.exception.CorrelationException;
28 import org.onap.holmes.common.utils.GsonUtil;
29 import org.onap.holmes.common.utils.HttpsUtils;
31 import java.io.IOException;
32 import java.util.ArrayList;
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.UUID;
40 public class Subscriber {
42 private DMaaPResponseUtil dMaaPResponseUtil = ServiceLocatorHolder.getLocator()
43 .getService(DMaaPResponseUtil.class);
46 * The number of milliseconds to wait for messages if none are immediately available. This
47 * should normally be used, and set at 15000 or higher.
49 private int timeout = 15000;
52 * The maximum number of messages to return
54 private int limit = 100;
57 * The number of milliseconds to poll interval time. This should normally be used, and set at
60 private int period = timeout;
62 private boolean secure;
65 private String uuid = UUID.randomUUID() + "";
66 private String consumerGroup = "homlesGroup" + uuid;
67 private String consumer = "homles" + uuid;
68 private String authInfo;
69 private String authExpDate;
71 public List<VesAlarm> subscribe() throws CorrelationException {
72 List<String> response;
74 response = getDMaaPData();
75 } catch (Exception e) {
76 throw new CorrelationException("Failed to get data from DMaaP.", e);
79 return extractVesAlarm(response);
80 } catch (Exception e) {
81 throw new CorrelationException("Failed to convert the response data to VES alarms.", e);
85 private List<String> getDMaaPData() throws Exception {
87 CloseableHttpClient closeableHttpClient = null;
88 HttpGet httpGet = new HttpGet(url + "/" + consumerGroup + "/" + consumer + "?timeout=" + period);
90 closeableHttpClient = HttpsUtils.getConditionalHttpsClient(timeout);
91 HttpResponse httpResponse = HttpsUtils
92 .get(httpGet, new HashMap<>(), closeableHttpClient);
93 response = HttpsUtils.extractResponseEntity(httpResponse);
94 } catch (Exception e) {
97 httpGet.releaseConnection();
98 if (closeableHttpClient != null) {
100 closeableHttpClient.close();
101 } catch (IOException e) {
102 log.warn("Failed to close http client!");
106 return GsonUtil.jsonToBean(response, List.class);
109 private List<VesAlarm> extractVesAlarm(List<String> responseEntity) throws IOException {
110 List<VesAlarm> vesAlarmList = new ArrayList<>();
111 for (String entity : responseEntity) {
112 vesAlarmList.add(dMaaPResponseUtil.convertJsonToVesAlarm(entity));