2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Modifications Copyright (C) 2019 IBM
10 * =============================================================================
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 * ============LICENSE_END=========================================================
26 package org.onap.appc.adapter.messaging.dmaap.impl;
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
30 import com.att.nsa.mr.client.MRBatchingPublisher;
31 import com.att.nsa.mr.client.MRClientFactory;
32 import java.io.IOException;
33 import java.util.Collection;
34 import java.util.Collections;
35 import java.util.HashSet;
36 import java.util.Properties;
38 import java.util.UUID;
39 import java.util.concurrent.TimeUnit;
40 import org.apache.commons.lang3.StringUtils;
41 import org.onap.appc.adapter.message.Producer;
42 import org.onap.appc.adapter.messaging.dmaap.utils.DmaapUtil;
43 import org.onap.appc.configuration.Configuration;
44 import org.onap.appc.configuration.ConfigurationFactory;
45 import org.onap.appc.metricservice.MetricRegistry;
46 import org.onap.appc.metricservice.MetricService;
47 import org.onap.appc.metricservice.metric.DmaapRequestCounterMetric;
48 import org.onap.appc.metricservice.metric.Metric;
49 import org.onap.appc.metricservice.metric.MetricType;
50 import org.onap.appc.metricservice.policy.PublishingPolicy;
51 import org.onap.appc.metricservice.publisher.LogPublisher;
52 import org.osgi.framework.BundleContext;
53 import org.osgi.framework.FrameworkUtil;
54 import org.osgi.framework.ServiceReference;
56 public class DmaapProducerImpl implements Producer {
58 private static final EELFLogger LOG = EELFManager.getInstance().getLogger(DmaapProducerImpl.class);
59 private static final Configuration configuration = ConfigurationFactory.getConfiguration();
61 private Set<String> topics;
63 private Properties props = null;
64 private MetricRegistry metricRegistry;
65 private boolean useHttps = false;
66 private boolean isMetricEnabled = false;
68 private Set<MRBatchingPublisher> clients;
70 public DmaapProducerImpl(Collection<String> urls, String topicName, String user, String password) {
71 this(urls, (Set<String>) null, user, password);
72 this.topics = new HashSet<>();
73 if (topicName != null) {
74 Collections.addAll(topics, topicName.split(","));
78 public DmaapProducerImpl(Collection<String> urls, Set<String> topicNames, String user, String password) {
81 throw new IllegalArgumentException("Mandaory argument is null: urls");
83 this.props = new Properties();
84 String urlsStr = StringUtils.join(urls, ',');
85 props.setProperty("host", urlsStr);
86 props.setProperty("id", UUID.randomUUID().toString());
87 if (user != null && password != null) {
88 props.setProperty("username", user);
89 props.setProperty("password", password);
91 props.setProperty("TransportType", "HTTPNOAUTH");
95 private void initMetric() {
96 LOG.debug("Metric getting initialized");
97 MetricService metricService = getMetricservice();
98 if (metricService != null) {
99 metricRegistry = metricService.createRegistry("APPC");
101 DmaapRequestCounterMetric dmaapKpiMetric = metricRegistry.metricBuilderFactory()
102 .dmaapRequestCounterBuilder().withName("DMAAP_KPI").withType(MetricType.COUNTER)
103 .withRecievedMessage(0).withPublishedMessage(0).build();
105 if (metricRegistry.register(dmaapKpiMetric)) {
106 Metric[] metrics = new Metric[] { dmaapKpiMetric };
107 LogPublisher logPublisher = new LogPublisher(metricRegistry, metrics);
108 LogPublisher[] logPublishers = new LogPublisher[1];
109 logPublishers[0] = logPublisher;
111 PublishingPolicy manuallyScheduledPublishingPolicy = metricRegistry.policyBuilderFactory()
112 .scheduledPolicyBuilder().withPublishers(logPublishers).withMetrics(metrics).build();
114 LOG.debug("Policy getting initialized");
115 manuallyScheduledPublishingPolicy.init();
116 LOG.debug("Metric initialized");
121 private Set<MRBatchingPublisher> getClients() {
122 Set<MRBatchingPublisher> out = new HashSet<>();
123 for (String topic : topics) {
125 String topicProducerPropFileName = DmaapUtil.createProducerPropFile(topic, props);
126 final MRBatchingPublisher client = MRClientFactory.createBatchingPublisher(topicProducerPropFileName);
128 } catch (Exception e) {
129 LOG.error(e.getMessage(), e);
136 public synchronized void updateCredentials(String key, String secret) {
137 LOG.info(String.format("Setting auth to %s for %s", key, this.toString()));
138 props.setProperty("username", String.valueOf(key));
139 props.setProperty("password", String.valueOf(secret));
144 public boolean post(String partition, String data) {
145 LOG.debug("In DmaapProducerImpl.post()");
146 boolean success = true;
147 Properties properties = configuration.getProperties();
148 if (properties != null && properties.getProperty("metric.enabled") != null) {
149 isMetricEnabled = Boolean.valueOf(properties.getProperty("metric.enabled"));
151 if (isMetricEnabled) {
155 // Create clients once and reuse them on subsequent posts. This is
156 // to support failover to other servers in the Dmaap cluster.
157 if ((clients == null) || (clients.isEmpty())) {
158 LOG.info("Getting CambriaBatchingPublisher Clients ...");
159 clients = getClients();
161 LOG.debug("In DmaapProducerImpl.post()::: before sending to clients");
162 for (MRBatchingPublisher client : clients) {
164 LOG.debug(String.format("Posting %s to %s", data, client));
165 client.send(partition, data);
166 } catch (IOException e) {
167 LOG.error(e.getMessage(), e);
171 incrementPublishedMessage();
175 private void incrementPublishedMessage() {
176 if (isMetricEnabled && metricRegistry != null) {
177 ((DmaapRequestCounterMetric) metricRegistry.metric("DMAAP_KPI")).incrementPublishedMessage();
182 * Close producer Dmaap client.
185 public void close() {
186 if ((clients == null) || (clients.isEmpty())) {
189 LOG.debug("Closing Dmaap producer clients....");
190 for (MRBatchingPublisher client : clients) {
192 client.close(1, TimeUnit.SECONDS);
193 } catch (InterruptedException e) {
194 LOG.warn(String.format("Failed to cleanly close Dmaap connection for [%s]", client), e);
195 Thread.currentThread().interrupt();
196 } catch (IOException e) {
197 LOG.warn(String.format("Failed to cleanly close Dmaap connection for [%s]", client), e);
203 public void useHttps(boolean yes) {
207 private MetricService getMetricservice() {
208 BundleContext bctx = FrameworkUtil.getBundle(MetricService.class).getBundleContext();
209 ServiceReference sref = bctx.getServiceReference(MetricService.class.getName());
211 LOG.info("Metric Service from bundlecontext");
212 return (MetricService) bctx.getService(sref);
214 LOG.info("Metric Service error from bundlecontext");
215 LOG.warn("Cannot find service reference for org.onap.appc.metricservice.MetricService");
220 public Properties getProperties() {
224 public boolean isHttps() {