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.onap.appc.adapter.messaging.dmaap.impl;
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import com.att.nsa.mr.client.MRBatchingPublisher;
30 import com.att.nsa.mr.client.MRClientFactory;
31 import java.io.IOException;
32 import java.util.Collection;
33 import java.util.Collections;
34 import java.util.HashSet;
35 import java.util.Properties;
37 import java.util.UUID;
38 import java.util.concurrent.TimeUnit;
39 import org.apache.commons.lang3.StringUtils;
40 import org.onap.appc.adapter.message.Producer;
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;
69 public DmaapProducerImpl(Collection<String> urls, String topicName, String user, String password) {
70 this(urls, (Set<String>)null, user, password);
71 this.topics = new HashSet<>();
72 if (topicName != null) {
73 Collections.addAll(topics, topicName.split(","));
77 public DmaapProducerImpl(Collection<String> urls, Set<String> topicNames, String user, String password) {
79 if (urls == null || user == null || password == null) {
80 throw new IllegalArgumentException("one of these mandaory argument is null: urls, user, password");
82 this.props = new Properties();
83 String urlsStr = StringUtils.join(urls, ',');
84 props.setProperty("host",urlsStr);
85 props.setProperty("id", UUID.randomUUID().toString());
86 props.setProperty("username",user);
87 props.setProperty("password",password);
90 private void initMetric() {
91 LOG.debug("Metric getting initialized");
92 MetricService metricService = getMetricservice();
93 if (metricService != null) {
94 metricRegistry = metricService.createRegistry("APPC");
96 DmaapRequestCounterMetric dmaapKpiMetric = metricRegistry.metricBuilderFactory()
97 .dmaapRequestCounterBuilder()
98 .withName("DMAAP_KPI").withType(MetricType.COUNTER)
99 .withRecievedMessage(0)
100 .withPublishedMessage(0)
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()
111 .withPublishers(logPublishers)
112 .withMetrics(metrics)
115 LOG.debug("Policy getting initialized");
116 manuallyScheduledPublishingPolicy.init();
117 LOG.debug("Metric initialized");
122 private Set<MRBatchingPublisher> getClients() {
123 Set<MRBatchingPublisher> out = new HashSet<>();
124 for (String topic : topics) {
126 String topicProducerPropFileName = DmaapUtil.createProducerPropFile(topic,props);
127 final MRBatchingPublisher client = MRClientFactory.createBatchingPublisher(topicProducerPropFileName);
129 } catch (Exception e) {
130 LOG.error(e.getMessage(), e);
137 public synchronized void updateCredentials(String key, String secret) {
138 LOG.info(String.format("Setting auth to %s for %s", key, this.toString()));
139 props.setProperty("user", String.valueOf(key));
140 props.setProperty("password", String.valueOf(secret));
145 public boolean post(String partition, String data) {
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();
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 (IOException | InterruptedException e) {
194 LOG.warn(String.format("Failed to cleanly close Dmaap connection for [%s]", client), e);
200 public void useHttps(boolean yes) {
204 private MetricService getMetricservice() {
205 BundleContext bctx = FrameworkUtil.getBundle(MetricService.class).getBundleContext();
206 ServiceReference sref = bctx.getServiceReference(MetricService.class.getName());
208 LOG.info("Metric Service from bundlecontext");
209 return (MetricService) bctx.getService(sref);
211 LOG.info("Metric Service error from bundlecontext");
212 LOG.warn("Cannot find service reference for org.onap.appc.metricservice.MetricService");