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.listener.impl;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
29 import org.onap.appc.adapter.factory.DmaapMessageAdapterFactoryImpl;
30 import org.onap.appc.adapter.factory.MessageService;
31 import org.onap.appc.adapter.message.Consumer;
32 import org.onap.appc.adapter.message.MessageAdapterFactory;
33 import org.onap.appc.adapter.message.Producer;
34 import org.onap.appc.listener.EventHandler;
35 import org.onap.appc.listener.ListenerProperties;
36 import org.onap.appc.listener.util.Mapper;
37 import org.onap.appc.logging.LoggingConstants;
38 import org.osgi.framework.Bundle;
39 import org.osgi.framework.BundleContext;
40 import org.osgi.framework.FrameworkUtil;
41 import org.osgi.framework.ServiceReference;
44 import java.util.ArrayList;
45 import java.util.Collection;
46 import java.util.HashSet;
47 import java.util.List;
51 * This class is a wrapper for the DMaaP client provided in appc-dmaap-adapter. Its aim is to ensure
52 * that only well formed messages are sent and received on DMaaP.
54 public class EventHandlerImpl implements EventHandler {
56 private final EELFLogger LOG = EELFManager.getInstance().getLogger(EventHandlerImpl.class);
59 * The amount of time in seconds to keep a connection to a topic open while waiting for data
61 private int READ_TIMEOUT = 60;
64 * The pool of hosts to query against
66 private Collection<String> pool;
69 * The topic to read messages from
71 private String readTopic;
74 * The topic to write messages to
76 private Set<String> writeTopics;
79 * The client (group) name to use for reading messages
81 private String clientName;
84 * The id of the client (group) that is reading messages
86 private String clientId;
89 * The api public key to use for authentication
91 private String apiKey;
94 * The api secret key to use for authentication
96 private String apiSecret;
99 * A json object containing filter arguments.
101 private String filter_json;
103 private MessageService messageService;
105 private Consumer reader = null;
106 private Producer producer = null;
108 public EventHandlerImpl(ListenerProperties props) {
109 pool = new HashSet<>();
110 writeTopics = new HashSet<>();
113 readTopic = props.getProperty(ListenerProperties.KEYS.TOPIC_READ);
114 clientName = props.getProperty(ListenerProperties.KEYS.CLIENT_NAME, "APP-C");
115 clientId = props.getProperty(ListenerProperties.KEYS.CLIENT_ID, "0");
116 apiKey = props.getProperty(ListenerProperties.KEYS.AUTH_USER_KEY);
117 apiSecret = props.getProperty(ListenerProperties.KEYS.AUTH_SECRET_KEY);
119 filter_json = props.getProperty(ListenerProperties.KEYS.TOPIC_READ_FILTER);
121 READ_TIMEOUT = Integer
122 .valueOf(props.getProperty(ListenerProperties.KEYS.TOPIC_READ_TIMEOUT, String.valueOf(READ_TIMEOUT)));
124 String hostnames = props.getProperty(ListenerProperties.KEYS.HOSTS);
125 if (hostnames != null && !hostnames.isEmpty()) {
126 for (String name : hostnames.split(",")) {
131 String writeTopicStr = props.getProperty(ListenerProperties.KEYS.TOPIC_WRITE);
132 if (writeTopicStr != null) {
133 for (String topic : writeTopicStr.split(",")) {
134 writeTopics.add(topic);
138 messageService = MessageService.parse(props.getProperty(ListenerProperties.KEYS.MESSAGE_SERVICE));
140 LOG.info(String.format(
141 "Configured to use %s client on host pool [%s]. Reading from [%s] filtered by %s. Writing to [%s]. Authenticated using %s",
142 messageService, hostnames, readTopic, filter_json, writeTopics, apiKey));
147 public List<String> getIncomingEvents() {
148 return getIncomingEvents(1000);
152 public List<String> getIncomingEvents(int limit) {
153 List<String> out = new ArrayList<>();
154 LOG.info(String.format("Getting up to %d incoming events", limit));
155 // reuse the consumer object instead of creating a new one every time
156 if (reader == null) {
157 LOG.info("Getting Consumer...");
158 reader = getConsumer();
160 if (reader != null) {
161 List<String> items = reader.fetch(READ_TIMEOUT * 1000, limit);
162 for (String item : items) {
166 LOG.info(String.format("Read %d messages from %s as %s/%s.", out.size(), readTopic, clientName, clientId));
171 public <T> List<T> getIncomingEvents(Class<T> cls) {
172 return getIncomingEvents(cls, 1000);
176 public <T> List<T> getIncomingEvents(Class<T> cls, int limit) {
177 List<String> incomingStrings = getIncomingEvents(limit);
178 return Mapper.mapList(incomingStrings, cls);
182 public void postStatus(String event) {
183 postStatus(null, event);
187 public void postStatus(String partition, String event) {
188 LOG.debug(String.format("Posting Message [%s]", event));
189 if (producer == null) {
190 LOG.info("Getting Producer...");
191 producer = getProducer();
193 producer.post(partition, event);
197 * Returns a consumer object for direct access to our Cambria consumer interface
199 * @return An instance of the consumer interface
201 protected Consumer getConsumer() {
202 LOG.debug(String.format("Getting Consumer: %s %s/%s/%s", pool, readTopic, clientName, clientId));
203 if (filter_json == null && writeTopics.contains(readTopic)) {
205 "*****We will be writing and reading to the same topic without a filter. This will cause an infinite loop.*****");
209 BundleContext ctx = null;
210 Bundle bundle = FrameworkUtil.getBundle(EventHandlerImpl.class);
212 ctx = bundle.getBundleContext();
216 ServiceReference svcRef = ctx.getServiceReference(MessageAdapterFactory.class.getName());
217 if (svcRef != null) {
219 out = ((MessageAdapterFactory) ctx.getService(svcRef))
220 .createConsumer(pool, readTopic, clientName, clientId, filter_json, apiKey, apiSecret);
221 } catch (Exception e) {
222 //TODO:create eelf message
223 LOG.error("EvenHandlerImp.getConsumer calling MessageAdapterFactor.createConsumer", e);
226 for (String url : pool) {
227 if (url.contains("3905") || url.contains("https")) {
239 * Returns a consumer object for direct access to our Cambria producer interface
241 * @return An instance of the producer interface
243 protected Producer getProducer() {
244 LOG.debug(String.format("Getting Producer: %s %s", pool, readTopic));
247 BundleContext ctx = FrameworkUtil.getBundle(EventHandlerImpl.class).getBundleContext();
249 ServiceReference svcRef = ctx.getServiceReference(MessageAdapterFactory.class.getName());
250 if (svcRef != null) {
251 out = ((MessageAdapterFactory) ctx.getService(svcRef))
252 .createProducer(pool, writeTopics, apiKey, apiSecret);
254 for (String url : pool) {
255 if (url.contains("3905") || url.contains("https")) {
267 public void closeClients() {
268 LOG.debug("Closing Consumer and Producer DMaaP clients");
269 if (reader != null) {
272 if (producer != null) {
278 public String getClientId() {
283 public void setClientId(String clientId) {
284 this.clientId = clientId;
288 public String getClientName() {
293 public void setClientName(String clientName) {
294 this.clientName = clientName;
295 MDC.put(LoggingConstants.MDCKeys.PARTNER_NAME, clientName);
299 public void addToPool(String hostname) {
304 public Collection<String> getPool() {
309 public void removeFromPool(String hostname) {
310 pool.remove(hostname);
314 public String getReadTopic() {
319 public void setReadTopic(String readTopic) {
320 this.readTopic = readTopic;
324 public Set<String> getWriteTopics() {
329 public void setWriteTopics(Set<String> writeTopics) {
330 this.writeTopics = writeTopics;
334 public void clearCredentials() {
340 public void setCredentials(String key, String secret) {