2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.openecomp.appc.adapter.messaging.dmaap.impl;
26 import java.util.concurrent.TimeUnit;
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
30 //import com.att.nsa.cambria.client.CambriaBatchingPublisher;
31 //import com.att.nsa.cambria.client.CambriaClientBuilders;
32 //import com.att.nsa.cambria.client.CambriaClientBuilders.PublisherBuilder;
34 import com.att.nsa.mr.client.MRBatchingPublisher;
35 import com.att.nsa.mr.client.MRClientFactory;
36 import org.apache.commons.lang3.StringUtils;
37 import org.openecomp.appc.adapter.message.Producer;
38 import org.openecomp.appc.adapter.messaging.dmaap.impl.DmaapUtil;
39 import org.openecomp.appc.configuration.Configuration;
40 import org.openecomp.appc.configuration.ConfigurationFactory;
41 import org.openecomp.appc.metricservice.MetricRegistry;
42 import org.openecomp.appc.metricservice.MetricService;
43 import org.openecomp.appc.metricservice.metric.Metric;
44 import org.openecomp.appc.metricservice.metric.MetricType;
45 import org.openecomp.appc.metricservice.metric.DmaapRequestCounterMetric;
46 import org.openecomp.appc.metricservice.policy.PublishingPolicy;
47 import org.openecomp.appc.metricservice.publisher.LogPublisher;
48 import org.osgi.framework.BundleContext;
49 import org.osgi.framework.FrameworkUtil;
50 import org.osgi.framework.ServiceReference;
52 public class DmaapProducerImpl implements Producer {
54 private static final EELFLogger LOG = EELFManager.getInstance().getLogger(DmaapProducerImpl.class);
55 private static final Configuration configuration = ConfigurationFactory.getConfiguration();
57 private Set<String> topics = new HashSet<String>();
59 private Properties props = null;
60 private static MetricRegistry metricRegistry;
61 private boolean useHttps = false;
62 private DmaapRequestCounterMetric dmaapKpiMetric;
63 private boolean isMetricEnabled=false;
65 private Set<MRBatchingPublisher> clients;
68 public DmaapProducerImpl(Collection<String> urls, String topicName, String user, String password) {
69 this(urls, (Set<String>)null, user, password);
70 this.topics = new HashSet<>();
71 if (topicName != null) {
72 for (String topic : topicName.split(",")) {
78 public DmaapProducerImpl(Collection<String> urls, Set<String> topicNames, String user, String password) {
80 if(urls == null || user == null || password == null){
81 throw new IllegalArgumentException("one of these mandaory argument is null: urls, user, password" );
83 this.props = new Properties();
84 String urlsStr = StringUtils.join(urls, ',');
85 props.setProperty("host",urlsStr);
86 props.setProperty("id", UUID.randomUUID().toString());
87 props.setProperty("username",user);
88 props.setProperty("password",password);
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");
115 private Set<MRBatchingPublisher> getClients() {
116 Set<MRBatchingPublisher> out = new HashSet<MRBatchingPublisher>();
117 for (String topic : topics) {
119 String topicProducerPropFileName = DmaapUtil.createProducerPropFile(topic,props);
120 final MRBatchingPublisher client = MRClientFactory.createBatchingPublisher (topicProducerPropFileName);
122 } catch (Exception e) {
131 public synchronized void updateCredentials(String key, String secret) {
132 LOG.info(String.format("Setting auth to %s for %s", key, this.toString()));
134 String password = secret;
135 props.setProperty("user",String.valueOf(user));
136 props.setProperty("password",String.valueOf(password));
141 public boolean post(String partition, String data) {
142 boolean success = true;
143 Properties properties=configuration.getProperties();
144 if(properties!=null && properties.getProperty("metric.enabled")!=null ){
145 isMetricEnabled=Boolean.valueOf(properties.getProperty("metric.enabled"));
151 // Create clients once and reuse them on subsequent posts. This is
152 // to support failover to other servers in the Dmaap cluster.
153 if ((clients == null) || (clients.isEmpty())) {
154 LOG.info("Getting CambriaBatchingPublisher Clients ...");
155 clients = getClients();
158 for (MRBatchingPublisher client : clients) {
160 LOG.debug(String.format("Posting %s to %s", data, client));
161 client.send(partition, data);
162 } catch (IOException e) {
168 ( (DmaapRequestCounterMetric) metricRegistry.metric("DMAAP_KPI")).incrementPublishedMessage();
174 * Close producer Dmaap client
177 public void close() {
178 if ((clients == null) || (clients.isEmpty())) {
182 LOG.debug("Closing Dmaap producer clients....");
183 for (MRBatchingPublisher client : clients) {
185 client.close(1, TimeUnit.SECONDS);
186 } catch (IOException | InterruptedException e) {
187 LOG.warn(String.format("Failed to cleanly close Dmaap connection for [%s]", client));
194 public void useHttps(boolean yes) {
198 private MetricService getMetricservice() {
200 return AppcDmaapAdapterActivator.getMetricService();
203 BundleContext bctx = FrameworkUtil.getBundle(MetricService.class).getBundleContext();
204 ServiceReference sref = bctx.getServiceReference(MetricService.class.getName());
206 LOG.info("Metric Service from bundlecontext");
207 return (MetricService) bctx.getService(sref);
210 LOG.info("Metric Service error from bundlecontext");
211 LOG.warn("Cannot find service reference for org.openecomp.appc.metricservice.MetricService");
217 public Metric getMetric(String name){
218 return metricRegistry.metric(name);