1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ============LICENSE_END=========================================================
19 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21 *******************************************************************************/
22 package org.onap.dmaap.dmf.mr.metrics.publisher.impl;
24 import com.att.nsa.apiClient.http.HttpException;
25 import com.att.nsa.apiClient.http.HttpObjectNotFoundException;
26 import jline.internal.Log;
27 import org.json.JSONArray;
28 import org.json.JSONException;
29 import org.json.JSONObject;
30 import org.onap.dmaap.dmf.mr.metrics.publisher.CambriaPublisherUtility;
32 import java.io.IOException;
33 import java.io.UnsupportedEncodingException;
34 import java.net.MalformedURLException;
35 import java.net.URLEncoder;
36 import java.util.Collection;
37 import java.util.LinkedList;
38 import java.util.List;
42 * @author anowarul.islam
45 public class DMaaPCambriaConsumerImpl extends CambriaBaseClient
46 implements org.onap.dmaap.dmf.mr.metrics.publisher.CambriaConsumer {
47 private final String fTopic;
48 private final String fGroup;
49 private final String fId;
50 private final int fTimeoutMs;
51 private final int fLimit;
52 private final String fFilter;
58 * @param consumerGroup
66 public DMaaPCambriaConsumerImpl(Collection<String> hostPart, final String topic, final String consumerGroup,
67 final String consumerId, int timeoutMs, int limit, String filter, String apiKey, String apiSecret) throws MalformedURLException {
68 super(hostPart, topic + "::" + consumerGroup + "::" + consumerId);
71 fGroup = consumerGroup;
73 fTimeoutMs = timeoutMs;
77 setApiCredentials(apiKey, apiSecret);
81 * method converts String to list
86 public static List<String> stringToList(String str) {
87 final LinkedList<String> set = new LinkedList<String>();
89 final String[] parts = str.trim().split(",");
90 for (String part : parts) {
91 final String trimmed = part.trim();
92 if (trimmed.length() > 0) {
101 public Iterable<String> fetch() throws IOException {
102 // fetch with the timeout and limit set in constructor
103 return fetch(fTimeoutMs, fLimit);
107 public Iterable<String> fetch(int timeoutMs, int limit) throws IOException {
108 final LinkedList<String> msgs = new LinkedList<String>();
110 final String urlPath = createUrlPath(timeoutMs, limit);
112 getLog().info("UEB GET " + urlPath);
114 final JSONObject o = get(urlPath);
117 final JSONArray a = o.getJSONArray("result");
119 for (int i = 0; i < a.length(); i++) {
120 msgs.add(a.getString(i));
124 } catch (HttpObjectNotFoundException e) {
125 // this can happen if the topic is not yet created. ignore.
126 Log.error("Failed due to topic is not yet created" + e);
127 } catch (JSONException e) {
128 // unexpected response
129 reportProblemWithResponse();
130 Log.error("Failed due to jsonException", e);
131 } catch (HttpException e) {
132 throw new IOException(e);
138 public String createUrlPath(int timeoutMs, int limit) {
139 final StringBuilder url = new StringBuilder(CambriaPublisherUtility.makeConsumerUrl(fTopic, fGroup, fId));
140 final StringBuilder adds = new StringBuilder();
141 if (timeoutMs > -1) {
142 adds.append("timeout=").append(timeoutMs);
146 if (adds.length() > 0) {
149 adds.append("limit=").append(limit);
151 if (fFilter != null && fFilter.length() > 0) {
153 if (adds.length() > 0) {
156 adds.append("filter=").append(URLEncoder.encode(fFilter, "UTF-8"));
157 } catch (UnsupportedEncodingException e) {
158 Log.error("Failed due to UnsupportedEncodingException" + e);
161 if (adds.length() > 0) {
162 url.append("?").append(adds.toString());
164 return url.toString();