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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.listener.impl;
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
30 import org.onap.appc.adapter.factory.DmaapMessageAdapterFactoryImpl;
31 import org.onap.appc.adapter.factory.MessageService;
32 import org.onap.appc.adapter.message.Consumer;
33 import org.onap.appc.adapter.message.MessageAdapterFactory;
34 import org.onap.appc.adapter.message.Producer;
35 import org.onap.appc.listener.EventHandler;
36 import org.onap.appc.listener.ListenerProperties;
37 import org.onap.appc.listener.util.Mapper;
38 import org.onap.appc.logging.LoggingConstants;
39 import org.osgi.framework.Bundle;
40 import org.osgi.framework.BundleContext;
41 import org.osgi.framework.FrameworkUtil;
42 import org.osgi.framework.ServiceReference;
45 import java.util.ArrayList;
46 import java.util.Collection;
47 import java.util.HashSet;
48 import java.util.List;
52 * This class is a wrapper for the DMaaP client provided in appc-dmaap-adapter. Its aim is to ensure
53 * that only well formed messages are sent and received on DMaaP.
55 public class EventHandlerImpl implements EventHandler {
57 private final EELFLogger LOG = EELFManager.getInstance().getLogger(EventHandlerImpl.class);
60 * The amount of time in seconds to keep a connection to a topic open while waiting for data
62 private int READ_TIMEOUT = 60;
65 * The pool of hosts to query against
67 private Collection<String> pool;
70 * The topic to read messages from
72 private String readTopic;
75 * The topic to write messages to
77 private Set<String> writeTopics;
80 * The client (group) name to use for reading messages
82 private String clientName;
85 * The id of the client (group) that is reading messages
87 private String clientId;
90 * The api public key to use for authentication
92 private String apiKey;
95 * The api secret key to use for authentication
97 private String apiSecret;
100 * A json object containing filter arguments.
102 private String filter_json;
104 private MessageService messageService;
106 private Consumer reader = null;
107 private Producer producer = null;
109 public EventHandlerImpl(ListenerProperties props) {
110 pool = new HashSet<>();
111 writeTopics = new HashSet<>();
114 readTopic = props.getProperty(ListenerProperties.KEYS.TOPIC_READ);
115 clientName = props.getProperty(ListenerProperties.KEYS.CLIENT_NAME, "APP-C");
116 clientId = props.getProperty(ListenerProperties.KEYS.CLIENT_ID, "0");
117 apiKey = props.getProperty(ListenerProperties.KEYS.AUTH_USER_KEY);
118 apiSecret = props.getProperty(ListenerProperties.KEYS.AUTH_SECRET_KEY);
120 filter_json = props.getProperty(ListenerProperties.KEYS.TOPIC_READ_FILTER);
122 READ_TIMEOUT = Integer
123 .valueOf(props.getProperty(ListenerProperties.KEYS.TOPIC_READ_TIMEOUT, String.valueOf(READ_TIMEOUT)));
125 String hostnames = props.getProperty(ListenerProperties.KEYS.HOSTS);
126 if (hostnames != null && !hostnames.isEmpty()) {
127 for (String name : hostnames.split(",")) {
132 String writeTopicStr = props.getProperty(ListenerProperties.KEYS.TOPIC_WRITE);
133 if (writeTopicStr != null) {
134 for (String topic : writeTopicStr.split(",")) {
135 writeTopics.add(topic);
139 messageService = MessageService.parse(props.getProperty(ListenerProperties.KEYS.MESSAGE_SERVICE));
141 LOG.info(String.format(
142 "Configured to use %s client on host pool [%s]. Reading from [%s] filtered by %s. Wriring to [%s]. Authenticated using %s",
143 messageService, hostnames, readTopic, filter_json, writeTopics, apiKey));
148 public List<String> getIncomingEvents() {
149 return getIncomingEvents(1000);
153 public List<String> getIncomingEvents(int limit) {
154 List<String> out = new ArrayList<>();
155 LOG.info(String.format("Getting up to %d incoming events", limit));
156 // reuse the consumer object instead of creating a new one every time
157 if (reader == null) {
158 LOG.info("Getting Consumer...");
159 reader = getConsumer();
161 if (reader != null) {
162 List<String> items = reader.fetch(READ_TIMEOUT * 1000, limit);
163 for (String item : items) {
167 LOG.info(String.format("Read %d messages from %s as %s/%s.", out.size(), readTopic, clientName, clientId));
172 public <T> List<T> getIncomingEvents(Class<T> cls) {
173 return getIncomingEvents(cls, 1000);
177 public <T> List<T> getIncomingEvents(Class<T> cls, int limit) {
178 List<String> incomingStrings = getIncomingEvents(limit);
179 return Mapper.mapList(incomingStrings, cls);
183 public void postStatus(String event) {
184 postStatus(null, event);
188 public void postStatus(String partition, String event) {
189 LOG.debug(String.format("Posting Message [%s]", event));
190 if (producer == null) {
191 LOG.info("Getting Producer...");
192 producer = getProducer();
194 producer.post(partition, event);
198 * Returns a consumer object for direct access to our Cambria consumer interface
200 * @return An instance of the consumer interface
202 protected Consumer getConsumer() {
203 LOG.debug(String.format("Getting Consumer: %s %s/%s/%s", pool, readTopic, clientName, clientId));
204 if (filter_json == null && writeTopics.contains(readTopic)) {
206 "*****We will be writing and reading to the same topic without a filter. This will cause an infinite loop.*****");
210 BundleContext ctx = null;
211 Bundle bundle = FrameworkUtil.getBundle(EventHandlerImpl.class);
213 ctx = bundle.getBundleContext();
217 ServiceReference svcRef = ctx.getServiceReference(MessageAdapterFactory.class.getName());
218 if (svcRef != null) {
220 out = ((MessageAdapterFactory) ctx.getService(svcRef))
221 .createConsumer(pool, readTopic, clientName, clientId, filter_json, apiKey, apiSecret);
222 } catch (Exception e) {
223 //TODO:create eelf message
224 LOG.error("EvenHandlerImp.getConsumer calling MessageAdapterFactor.createConsumer", e);
227 for (String url : pool) {
228 if (url.contains("3905") || url.contains("https")) {
240 * Returns a consumer object for direct access to our Cambria producer interface
242 * @return An instance of the producer interface
244 protected Producer getProducer() {
245 LOG.debug(String.format("Getting Producer: %s %s", pool, readTopic));
248 BundleContext ctx = FrameworkUtil.getBundle(EventHandlerImpl.class).getBundleContext();
250 ServiceReference svcRef = ctx.getServiceReference(MessageAdapterFactory.class.getName());
251 if (svcRef != null) {
252 out = ((MessageAdapterFactory) ctx.getService(svcRef))
253 .createProducer(pool, writeTopics, apiKey, apiSecret);
255 for (String url : pool) {
256 if (url.contains("3905") || url.contains("https")) {
268 public void closeClients() {
269 LOG.debug("Closing Consumer and Producer DMaaP clients");
270 if (reader != null) {
273 if (producer != null) {
279 public String getClientId() {
284 public void setClientId(String clientId) {
285 this.clientId = clientId;
289 public String getClientName() {
294 public void setClientName(String clientName) {
295 this.clientName = clientName;
296 MDC.put(LoggingConstants.MDCKeys.PARTNER_NAME, clientName);
300 public void addToPool(String hostname) {
305 public Collection<String> getPool() {
310 public void removeFromPool(String hostname) {
311 pool.remove(hostname);
315 public String getReadTopic() {
320 public void setReadTopic(String readTopic) {
321 this.readTopic = readTopic;
325 public Set<String> getWriteTopics() {
330 public void setWriteTopics(Set<String> writeTopics) {
331 this.writeTopics = writeTopics;
335 public void clearCredentials() {
341 public void setCredentials(String key, String secret) {