2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.openecomp.appc.adapter.messaging.dmaap.impl;
27 import java.io.IOException;
30 import com.att.eelf.configuration.EELFLogger;
31 import com.att.eelf.configuration.EELFManager;
32 //import com.att.nsa.cambria.client.CambriaClientBuilders;
33 //import com.att.nsa.cambria.client.CambriaClientBuilders.ConsumerBuilder;
34 //import com.att.nsa.cambria.client.CambriaConsumer;
36 import com.att.nsa.mr.client.MRClientFactory;
37 import com.att.nsa.mr.client.MRConsumer;
38 import org.apache.commons.lang3.StringUtils;
39 import org.openecomp.appc.adapter.message.Consumer;
40 import org.openecomp.appc.configuration.Configuration;
41 import org.openecomp.appc.configuration.ConfigurationFactory;
42 import org.openecomp.appc.metricservice.MetricRegistry;
43 import org.openecomp.appc.metricservice.MetricService;
44 import org.openecomp.appc.metricservice.impl.MetricServiceImpl;
45 import org.openecomp.appc.metricservice.metric.Metric;
46 import org.openecomp.appc.metricservice.metric.MetricType;
47 import org.openecomp.appc.metricservice.metric.DmaapRequestCounterMetric;
48 import org.openecomp.appc.metricservice.policy.PublishingPolicy;
49 import org.openecomp.appc.metricservice.publisher.LogPublisher;
50 import org.osgi.framework.BundleContext;
51 import org.osgi.framework.FrameworkUtil;
52 import org.osgi.framework.ServiceReference;
54 public class DmaapConsumerImpl implements Consumer {
56 private static final EELFLogger LOG = EELFManager.getInstance().getLogger(DmaapConsumerImpl.class);
57 private static final Configuration configuration = ConfigurationFactory.getConfiguration();
59 private static final int DEFAULT_TIMEOUT_MS = 60000;
60 private static final int DEFAULT_LIMIT = 1000;
61 private static MetricRegistry metricRegistry;
63 private DmaapRequestCounterMetric dmaapKpiMetric;
64 private boolean isMetricEnabled=false;
65 private boolean useHttps = false;
66 private MRConsumer client = null;
67 private Properties props = null;
70 public DmaapConsumerImpl(Collection<String> urls, String topicName, String consumerGroupName, String consumerId,String user, String password) {
71 this(urls, topicName, consumerGroupName, consumerId,user, password,null);
75 public DmaapConsumerImpl(Collection<String> urls, String topicName, String consumerGroupName, String consumerId,String user, String password,String filter) {
76 this.topic = topicName;
77 this.props = new Properties();
78 String urlsStr = StringUtils.join(urls, ',');
79 props.setProperty("host",urlsStr);
80 props.setProperty("group",consumerGroupName);
81 props.setProperty("id",consumerId);
82 props.setProperty("username",user);
83 props.setProperty("password",password);
85 props.setProperty("filter", filter);
90 private void initMetric() {
91 LOG.debug("Metric getting initialized");
92 MetricService metricService = getMetricservice();
93 metricRegistry = metricService.createRegistry("APPC");
94 dmaapKpiMetric = metricRegistry.metricBuilderFactory().
95 dmaapRequestCounterBuilder().
96 withName("DMAAP_KPI").withType(MetricType.COUNTER).
97 withRecievedMessage(0)
98 .withPublishedMessage(0)
100 if (metricRegistry.register(dmaapKpiMetric)) {
101 Metric[] metrics = new Metric[]{dmaapKpiMetric};
102 LogPublisher logPublisher = new LogPublisher(metricRegistry, metrics);
103 LogPublisher[] logPublishers = new LogPublisher[1];
104 logPublishers[0] = logPublisher;
105 PublishingPolicy manuallyScheduledPublishingPolicy = metricRegistry.policyBuilderFactory().
106 scheduledPolicyBuilder().withPublishers(logPublishers).
107 withMetrics(metrics).
109 LOG.debug("Policy getting initialized");
110 manuallyScheduledPublishingPolicy.init();
111 LOG.debug("Metric initialized");
114 private MRConsumer getClient() {
115 return getClient(DEFAULT_TIMEOUT_MS, DEFAULT_LIMIT);
119 * @return An instance of MRConsumer created from our class variables
121 private synchronized MRConsumer getClient(int waitMs, int limit) {
123 props.setProperty("timeout",String.valueOf(waitMs));
124 props.setProperty("limit",String.valueOf(limit));
125 String topicProducerPropFileName = DmaapUtil.createConsumerPropFile(topic,props);
126 return MRClientFactory.createConsumer ( topicProducerPropFileName);
127 } catch (IOException e1) {
128 LOG.error("failed to createConsumer",e1);
134 public synchronized void updateCredentials(String key, String secret) {
135 LOG.info(String.format("Setting auth to %s for %s", key, this.toString()));
137 String password = secret;
138 props.setProperty("user",String.valueOf(user));
139 props.setProperty("password",String.valueOf(password));
144 public List<String> fetch(int waitMs, int limit) {
145 Properties properties=configuration.getProperties();
146 if(properties!=null && properties.getProperty("metric.enabled")!=null ){
147 isMetricEnabled=Boolean.valueOf(properties.getProperty("metric.enabled"));
152 LOG.debug(String.format("Fetching up to %d records with %dms wait on %s", limit, waitMs, this.toString()));
153 List<String> out = new ArrayList<String>();
155 // Create client once and reuse it on subsequent fetches. This is
156 // to support failover to other servers in the DMaaP cluster.
157 if (client == null) {
158 LOG.info("Getting DMaaP Client ...");
159 client = getClient(waitMs, limit);
162 for (String s : client.fetch(waitMs, limit)) {
165 ((DmaapRequestCounterMetric)metricRegistry.metric("DMAAP_KPI")).incrementRecievedMessage();
168 LOG.debug(String.format("Got %d records from %s", out.size(), this.toString()));
169 } catch (Exception e) {
170 // Connection exception
171 LOG.error(String.format("Dmaap Connection Issue Detected. %s", e.getMessage()));
174 LOG.warn(String.format("Sleeping for %dms to compensate for connection failure", waitMs));
175 Thread.sleep(waitMs);
176 } catch (InterruptedException e2) {
177 LOG.warn(String.format("Failed to wait for %dms after bad fetch", waitMs));
186 * Close consumer Dmaap client
189 public void close() {
190 LOG.debug("Closing Dmaap consumer client....");
191 if (client != null) {
197 public List<String> fetch() {
198 return fetch(DEFAULT_TIMEOUT_MS, DEFAULT_LIMIT);
202 public String toString() {
203 String hostStr = (props == null || props.getProperty("host") == null? "N/A" : props.getProperty("host"));
204 String group = (props == null || props.getProperty("group") == null? "N/A" : props.getProperty("group"));
205 String id = (props == null || props.getProperty("id") == null? "N/A" : props.getProperty("id"));
206 return String.format("Consumer %s/%s listening to %s on [%s]", group, id, topic, hostStr);
210 public void useHttps(boolean yes) {
215 private MetricService getMetricservice() {
216 BundleContext bctx = FrameworkUtil.getBundle(MetricService.class).getBundleContext();
217 // Get AAIadapter reference
218 ServiceReference sref = bctx.getServiceReference(MetricService.class.getName());
220 LOG.info("Metric Service from bundlecontext");
221 return (MetricServiceImpl) bctx.getService(sref);
224 LOG.info("Metric Service error from bundlecontext");
225 LOG.warn("Cannot find service reference for org.openecomp.appc.metricservice.MetricService");
231 public Metric getMetric(String name){
232 return metricRegistry.metric(name);