Improve OJSI-185 documentation
[appc.git] / appc-adapters / appc-dmaap-adapter / appc-dmaap-adapter-bundle / src / main / java / org / onap / appc / adapter / messaging / dmaap / impl / DmaapProducerImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
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.
22  *
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.adapter.messaging.dmaap.impl;
27
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;
37 import java.util.Set;
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;
55
56 public class DmaapProducerImpl implements Producer {
57
58     private static final EELFLogger    LOG             = EELFManager.getInstance().getLogger(DmaapProducerImpl.class);
59     private static final Configuration configuration   = ConfigurationFactory.getConfiguration();
60
61     private Set<String>                topics;
62
63     private Properties                 props           = null;
64     private MetricRegistry             metricRegistry;
65     private boolean                    useHttps        = false;
66     private boolean                    isMetricEnabled = false;
67
68     private Set<MRBatchingPublisher>   clients;
69
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(","));
75         }
76     }
77
78     public DmaapProducerImpl(Collection<String> urls, Set<String> topicNames, String user, String password) {
79         topics = topicNames;
80         if (urls == null) {
81             throw new IllegalArgumentException("Mandaory argument is null: urls");
82         }
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);
90         } else {
91             props.setProperty("TransportType", "HTTPNOAUTH");
92         }
93     }
94
95     private void initMetric() {
96         LOG.debug("Metric getting initialized");
97         MetricService metricService = getMetricservice();
98         if (metricService != null) {
99             metricRegistry = metricService.createRegistry("APPC");
100
101             DmaapRequestCounterMetric dmaapKpiMetric = metricRegistry.metricBuilderFactory()
102                     .dmaapRequestCounterBuilder().withName("DMAAP_KPI").withType(MetricType.COUNTER)
103                     .withRecievedMessage(0).withPublishedMessage(0).build();
104
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;
110
111                 PublishingPolicy manuallyScheduledPublishingPolicy = metricRegistry.policyBuilderFactory()
112                         .scheduledPolicyBuilder().withPublishers(logPublishers).withMetrics(metrics).build();
113
114                 LOG.debug("Policy getting initialized");
115                 manuallyScheduledPublishingPolicy.init();
116                 LOG.debug("Metric initialized");
117             }
118         }
119     }
120
121     private Set<MRBatchingPublisher> getClients() {
122         Set<MRBatchingPublisher> out = new HashSet<>();
123         for (String topic : topics) {
124             try {
125                 String topicProducerPropFileName = DmaapUtil.createProducerPropFile(topic, props);
126                 final MRBatchingPublisher client = MRClientFactory.createBatchingPublisher(topicProducerPropFileName);
127                 out.add(client);
128             } catch (Exception e) {
129                 LOG.error(e.getMessage(), e);
130             }
131         }
132         return out;
133     }
134
135     @Override
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));
140         clients = null;
141     }
142
143     @Override
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"));
150         }
151         if (isMetricEnabled) {
152             initMetric();
153         }
154
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();
160         }
161         LOG.debug("In DmaapProducerImpl.post()::: before sending to clients");
162         for (MRBatchingPublisher client : clients) {
163             try {
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);
168                 success = false;
169             }
170         }
171         incrementPublishedMessage();
172         return success;
173     }
174
175     private void incrementPublishedMessage() {
176         if (isMetricEnabled && metricRegistry != null) {
177             ((DmaapRequestCounterMetric) metricRegistry.metric("DMAAP_KPI")).incrementPublishedMessage();
178         }
179     }
180
181     /**
182      * Close producer Dmaap client.
183      */
184     @Override
185     public void close() {
186         if ((clients == null) || (clients.isEmpty())) {
187             return;
188         }
189         LOG.debug("Closing Dmaap producer clients....");
190         for (MRBatchingPublisher client : clients) {
191           try {
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);
198           }
199         }
200     }
201
202     @Override
203     public void useHttps(boolean yes) {
204         useHttps = yes;
205     }
206
207     private MetricService getMetricservice() {
208         BundleContext bctx = FrameworkUtil.getBundle(MetricService.class).getBundleContext();
209         ServiceReference sref = bctx.getServiceReference(MetricService.class.getName());
210         if (sref != null) {
211             LOG.info("Metric Service from bundlecontext");
212             return (MetricService) bctx.getService(sref);
213         } else {
214             LOG.info("Metric Service error from bundlecontext");
215             LOG.warn("Cannot find service reference for org.onap.appc.metricservice.MetricService");
216             return null;
217         }
218     }
219
220     public Properties getProperties() {
221         return props;
222     }
223
224     public boolean isHttps() {
225         return useHttps;
226     }
227
228 }