2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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 * ============LICENSE_END=========================================================
24 package org.onap.appc.adapter.messaging.dmaap.impl;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import com.att.nsa.mr.client.MRBatchingPublisher;
29 import com.att.nsa.mr.client.MRClientFactory;
30 import java.io.IOException;
31 import java.util.Collection;
32 import java.util.Collections;
33 import java.util.HashSet;
34 import java.util.Properties;
36 import java.util.UUID;
37 import java.util.concurrent.TimeUnit;
38 import org.apache.commons.lang3.StringUtils;
39 import org.onap.appc.adapter.message.Producer;
40 import org.onap.appc.adapter.messaging.dmaap.utils.DmaapUtil;
41 import org.onap.appc.configuration.Configuration;
42 import org.onap.appc.configuration.ConfigurationFactory;
43 import org.onap.appc.metricservice.MetricRegistry;
44 import org.onap.appc.metricservice.MetricService;
45 import org.onap.appc.metricservice.metric.DmaapRequestCounterMetric;
46 import org.onap.appc.metricservice.metric.Metric;
47 import org.onap.appc.metricservice.metric.MetricType;
48 import org.onap.appc.metricservice.policy.PublishingPolicy;
49 import org.onap.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 DmaapProducerImpl implements Producer {
56 private static final EELFLogger LOG = EELFManager.getInstance().getLogger(DmaapProducerImpl.class);
57 private static final Configuration configuration = ConfigurationFactory.getConfiguration();
59 private Set<String> topics;
61 private Properties props = null;
62 private MetricRegistry metricRegistry;
63 private boolean useHttps = false;
64 private boolean isMetricEnabled = false;
66 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 Collections.addAll(topics, topicName.split(","));
76 public DmaapProducerImpl(Collection<String> urls, Set<String> topicNames, String user, String password) {
79 throw new IllegalArgumentException("Mandaory argument is null: urls");
81 this.props = new Properties();
82 String urlsStr = StringUtils.join(urls, ',');
83 props.setProperty("host", urlsStr);
84 props.setProperty("id", UUID.randomUUID().toString());
85 if (user != null && password != null) {
86 props.setProperty("username", user);
87 props.setProperty("password", password);
89 props.setProperty("TransportType", "HTTPNOAUTH");
93 private void initMetric() {
94 LOG.debug("Metric getting initialized");
95 MetricService metricService = getMetricservice();
96 if (metricService != null) {
97 metricRegistry = metricService.createRegistry("APPC");
99 DmaapRequestCounterMetric dmaapKpiMetric = metricRegistry.metricBuilderFactory()
100 .dmaapRequestCounterBuilder().withName("DMAAP_KPI").withType(MetricType.COUNTER)
101 .withRecievedMessage(0).withPublishedMessage(0).build();
103 if (metricRegistry.register(dmaapKpiMetric)) {
104 Metric[] metrics = new Metric[] { dmaapKpiMetric };
105 LogPublisher logPublisher = new LogPublisher(metricRegistry, metrics);
106 LogPublisher[] logPublishers = new LogPublisher[1];
107 logPublishers[0] = logPublisher;
109 PublishingPolicy manuallyScheduledPublishingPolicy = metricRegistry.policyBuilderFactory()
110 .scheduledPolicyBuilder().withPublishers(logPublishers).withMetrics(metrics).build();
112 LOG.debug("Policy getting initialized");
113 manuallyScheduledPublishingPolicy.init();
114 LOG.debug("Metric initialized");
119 private Set<MRBatchingPublisher> getClients() {
120 Set<MRBatchingPublisher> out = new HashSet<>();
121 for (String topic : topics) {
123 String topicProducerPropFileName = DmaapUtil.createProducerPropFile(topic, props);
124 final MRBatchingPublisher client = MRClientFactory.createBatchingPublisher(topicProducerPropFileName);
126 } catch (Exception e) {
127 LOG.error(e.getMessage(), e);
134 public synchronized void updateCredentials(String key, String secret) {
135 LOG.info(String.format("Setting auth to %s for %s", key, this.toString()));
136 props.setProperty("username", String.valueOf(key));
137 props.setProperty("password", String.valueOf(secret));
142 public boolean post(String partition, String data) {
143 LOG.debug("In DmaapProducerImpl.post()");
144 boolean success = true;
145 Properties properties = configuration.getProperties();
146 if (properties != null && properties.getProperty("metric.enabled") != null) {
147 isMetricEnabled = Boolean.valueOf(properties.getProperty("metric.enabled"));
149 if (isMetricEnabled) {
153 // Create clients once and reuse them on subsequent posts. This is
154 // to support failover to other servers in the Dmaap cluster.
155 if ((clients == null) || (clients.isEmpty())) {
156 LOG.info("Getting CambriaBatchingPublisher Clients ...");
157 clients = getClients();
159 LOG.debug("In DmaapProducerImpl.post()::: before sending to clients");
160 for (MRBatchingPublisher client : clients) {
162 LOG.debug(String.format("Posting %s to %s", data, client));
163 client.send(partition, data);
164 } catch (IOException e) {
165 LOG.error(e.getMessage(), e);
169 incrementPublishedMessage();
173 private void incrementPublishedMessage() {
174 if (isMetricEnabled && metricRegistry != null) {
175 ((DmaapRequestCounterMetric) metricRegistry.metric("DMAAP_KPI")).incrementPublishedMessage();
180 * Close producer Dmaap client.
183 public void close() {
184 if ((clients == null) || (clients.isEmpty())) {
187 LOG.debug("Closing Dmaap producer clients....");
188 for (MRBatchingPublisher client : clients) {
190 client.close(1, TimeUnit.SECONDS);
191 } catch (IOException | InterruptedException e) {
192 LOG.warn(String.format("Failed to cleanly close Dmaap connection for [%s]", client), e);
198 public void useHttps(boolean yes) {
202 private MetricService 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);
209 LOG.info("Metric Service error from bundlecontext");
210 LOG.warn("Cannot find service reference for org.onap.appc.metricservice.MetricService");
215 public Properties getProperties() {
219 public boolean isHttps() {