3a54fbe642eace5519870f695fb3621cc936359b
[appc.git] / appc-adapters / appc-dmaap-adapter / appc-dmaap-adapter-bundle / src / main / java / org / openecomp / appc / adapter / messaging / dmaap / impl / DmaapProducerImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * APPC
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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.
21  */
22
23 package org.openecomp.appc.adapter.messaging.dmaap.impl;
24
25 import java.io.*;
26 import java.util.*;
27 import java.util.concurrent.TimeUnit;
28
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;
34
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;
52
53 public class DmaapProducerImpl implements Producer {
54
55     private static final EELFLogger LOG = EELFManager.getInstance().getLogger(DmaapProducerImpl.class);
56     private static final Configuration configuration = ConfigurationFactory.getConfiguration();
57
58     private Set<String> topics = new HashSet<String>();
59
60     private Properties props = null;
61     private static MetricRegistry metricRegistry;
62     private boolean useHttps = false;
63     private DmaapRequestCounterMetric dmaapKpiMetric;
64     private boolean isMetricEnabled=false;
65     
66     private Set<MRBatchingPublisher> clients;
67
68     
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(",")) {
74                 topics.add(topic);
75             }
76         }
77     }
78
79     public DmaapProducerImpl(Collection<String> urls, Set<String> topicNames, String user, String password) {
80         topics = topicNames;
81         if(urls == null || user == null || password == null){
82             throw new IllegalArgumentException("one of these mandaory argument is null: urls, user, password" );
83         }
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);
90     }
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)
100                 .build();
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).
109                     build();
110             LOG.debug("Policy getting initialized");
111             manuallyScheduledPublishingPolicy.init();
112             LOG.debug("Metric initialized");
113         }
114
115     }
116     private Set<MRBatchingPublisher> getClients() {
117         Set<MRBatchingPublisher> out = new HashSet<MRBatchingPublisher>();
118         for (String topic : topics) {
119             try {
120                 String topicProducerPropFileName = DmaapUtil.createProducerPropFile(topic,props);
121                 final MRBatchingPublisher client = MRClientFactory.createBatchingPublisher (topicProducerPropFileName);
122                 out.add(client);
123             } catch (Exception e) {
124                 e.printStackTrace();
125             }
126         }
127
128         return out;
129     }
130
131     @Override
132     public synchronized void updateCredentials(String key, String secret) {
133         LOG.info(String.format("Setting auth to %s for %s", key, this.toString()));
134         String user = key;
135         String password = secret;
136         props.setProperty("user",String.valueOf(user));
137         props.setProperty("password",String.valueOf(password));
138         clients = null;
139     }
140
141     @Override
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"));
147         }
148         if(isMetricEnabled){
149             initMetric();
150         }
151         
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();
157         }
158         
159         for (MRBatchingPublisher client : clients) {
160             try {
161                 LOG.debug(String.format("Posting %s to %s", data, client));
162                 client.send(partition, data);
163             } catch (IOException e) {
164                 e.printStackTrace();
165                 success = false;
166             }
167         }
168         if(isMetricEnabled){
169             ( (DmaapRequestCounterMetric) metricRegistry.metric("DMAAP_KPI")).incrementPublishedMessage();
170         }
171         return success;
172     }
173
174     /**
175      * Close producer Dmaap client
176      */
177     @Override
178     public void close() {
179         if ((clients == null) || (clients.isEmpty())) {
180                 return;
181         }
182
183         LOG.debug("Closing Dmaap producer clients....");
184         for (MRBatchingPublisher client : clients) {
185             try {
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));
189                 e.printStackTrace();
190             }
191         }
192     }
193
194     @Override
195     public void useHttps(boolean yes) {
196         useHttps = yes;
197     }
198
199     private MetricService getMetricservice() {
200 /*
201         return AppcDmaapAdapterActivator.getMetricService();
202 */
203
204         BundleContext bctx = FrameworkUtil.getBundle(MetricService.class).getBundleContext();
205         ServiceReference sref = bctx.getServiceReference(MetricService.class.getName());
206         if (sref != null) {
207             LOG.info("Metric Service from bundlecontext");
208             return (MetricService) bctx.getService(sref);
209
210         } else {
211             LOG.info("Metric Service error from bundlecontext");
212             LOG.warn("Cannot find service reference for org.openecomp.appc.metricservice.MetricService");
213             return null;
214
215         }
216     }
217
218     public  Metric getMetric(String name){
219         return metricRegistry.metric(name);
220     }
221 }