2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2017 Amdocs
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=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.openecomp.appc.adapter.messaging.dmaap.impl;
27 import java.util.concurrent.TimeUnit;
29 import com.att.eelf.configuration.EELFLogger;
30 import com.att.eelf.configuration.EELFManager;
31 //import com.att.nsa.cambria.client.CambriaBatchingPublisher;
32 //import com.att.nsa.cambria.client.CambriaClientBuilders;
33 //import com.att.nsa.cambria.client.CambriaClientBuilders.PublisherBuilder;
35 import com.att.nsa.mr.client.MRBatchingPublisher;
36 import com.att.nsa.mr.client.MRClientFactory;
37 import org.apache.commons.lang3.StringUtils;
38 import org.openecomp.appc.adapter.message.Producer;
39 import org.openecomp.appc.adapter.messaging.dmaap.impl.DmaapUtil;
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.metric.Metric;
45 import org.openecomp.appc.metricservice.metric.MetricType;
46 import org.openecomp.appc.metricservice.metric.DmaapRequestCounterMetric;
47 import org.openecomp.appc.metricservice.policy.PublishingPolicy;
48 import org.openecomp.appc.metricservice.publisher.LogPublisher;
49 import org.osgi.framework.BundleContext;
50 import org.osgi.framework.FrameworkUtil;
51 import org.osgi.framework.ServiceReference;
53 public class DmaapProducerImpl implements Producer {
55 private static final EELFLogger LOG = EELFManager.getInstance().getLogger(DmaapProducerImpl.class);
56 private static final Configuration configuration = ConfigurationFactory.getConfiguration();
58 private Set<String> topics = new HashSet<String>();
60 private Properties props = null;
61 private static MetricRegistry metricRegistry;
62 private boolean useHttps = false;
63 private DmaapRequestCounterMetric dmaapKpiMetric;
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 for (String topic : topicName.split(",")) {
79 public DmaapProducerImpl(Collection<String> urls, Set<String> topicNames, String user, String password) {
81 if(urls == null || user == null || password == null){
82 throw new IllegalArgumentException("one of these mandaory argument is null: urls, user, password" );
84 this.props = new Properties();
85 String urlsStr = StringUtils.join(urls, ',');
86 props.setProperty("host",urlsStr);
87 props.setProperty("id", UUID.randomUUID().toString());
88 props.setProperty("username",user);
89 props.setProperty("password",password);
91 private void initMetric() {
92 LOG.debug("Metric getting initialized");
93 MetricService metricService = getMetricservice();
94 metricRegistry=metricService.createRegistry("APPC");
95 dmaapKpiMetric = metricRegistry.metricBuilderFactory().
96 dmaapRequestCounterBuilder().
97 withName("DMAAP_KPI").withType(MetricType.COUNTER).
98 withRecievedMessage(0)
99 .withPublishedMessage(0)
101 if(metricRegistry.register(dmaapKpiMetric)) {
102 Metric[] metrics = new Metric[]{dmaapKpiMetric};
103 LogPublisher logPublisher = new LogPublisher(metricRegistry, metrics);
104 LogPublisher[] logPublishers = new LogPublisher[1];
105 logPublishers[0] = logPublisher;
106 PublishingPolicy manuallyScheduledPublishingPolicy = metricRegistry.policyBuilderFactory().
107 scheduledPolicyBuilder().withPublishers(logPublishers).
108 withMetrics(metrics).
110 LOG.debug("Policy getting initialized");
111 manuallyScheduledPublishingPolicy.init();
112 LOG.debug("Metric initialized");
116 private Set<MRBatchingPublisher> getClients() {
117 Set<MRBatchingPublisher> out = new HashSet<MRBatchingPublisher>();
118 for (String topic : topics) {
120 String topicProducerPropFileName = DmaapUtil.createProducerPropFile(topic,props);
121 final MRBatchingPublisher client = MRClientFactory.createBatchingPublisher (topicProducerPropFileName);
123 } catch (Exception e) {
132 public synchronized void updateCredentials(String key, String secret) {
133 LOG.info(String.format("Setting auth to %s for %s", key, this.toString()));
135 String password = secret;
136 props.setProperty("user",String.valueOf(user));
137 props.setProperty("password",String.valueOf(password));
142 public boolean post(String partition, String data) {
143 boolean success = true;
144 Properties properties=configuration.getProperties();
145 if(properties!=null && properties.getProperty("metric.enabled")!=null ){
146 isMetricEnabled=Boolean.valueOf(properties.getProperty("metric.enabled"));
152 // Create clients once and reuse them on subsequent posts. This is
153 // to support failover to other servers in the Dmaap cluster.
154 if ((clients == null) || (clients.isEmpty())) {
155 LOG.info("Getting CambriaBatchingPublisher Clients ...");
156 clients = getClients();
159 for (MRBatchingPublisher client : clients) {
161 LOG.debug(String.format("Posting %s to %s", data, client));
162 client.send(partition, data);
163 } catch (IOException e) {
169 ( (DmaapRequestCounterMetric) metricRegistry.metric("DMAAP_KPI")).incrementPublishedMessage();
175 * Close producer Dmaap client
178 public void close() {
179 if ((clients == null) || (clients.isEmpty())) {
183 LOG.debug("Closing Dmaap producer clients....");
184 for (MRBatchingPublisher client : clients) {
186 client.close(1, TimeUnit.SECONDS);
187 } catch (IOException | InterruptedException e) {
188 LOG.warn(String.format("Failed to cleanly close Dmaap connection for [%s]", client));
195 public void useHttps(boolean yes) {
199 private MetricService getMetricservice() {
201 return AppcDmaapAdapterActivator.getMetricService();
204 BundleContext bctx = FrameworkUtil.getBundle(MetricService.class).getBundleContext();
205 ServiceReference sref = bctx.getServiceReference(MetricService.class.getName());
207 LOG.info("Metric Service from bundlecontext");
208 return (MetricService) bctx.getService(sref);
211 LOG.info("Metric Service error from bundlecontext");
212 LOG.warn("Cannot find service reference for org.openecomp.appc.metricservice.MetricService");
218 public Metric getMetric(String name){
219 return metricRegistry.metric(name);