2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.openecomp.appc.listener.impl;
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import org.openecomp.appc.adapter.factory.MessageService;
30 import org.openecomp.appc.adapter.message.Consumer;
31 import org.openecomp.appc.adapter.message.MessageAdapterFactory;
32 import org.openecomp.appc.adapter.message.Producer;
33 import org.openecomp.appc.listener.EventHandler;
34 import org.openecomp.appc.listener.ListenerProperties;
35 import org.openecomp.appc.listener.util.Mapper;
36 import org.openecomp.appc.logging.LoggingConstants;
37 import org.osgi.framework.BundleContext;
38 import org.osgi.framework.FrameworkUtil;
39 import org.osgi.framework.ServiceReference;
42 import java.util.ArrayList;
43 import java.util.Collection;
44 import java.util.HashSet;
45 import java.util.List;
49 * This class is a wrapper for the DMaaP client provided in appc-dmaap-adapter. Its aim is to ensure
50 * that only well formed messages are sent and received on DMaaP.
52 public class EventHandlerImpl implements EventHandler {
54 private final EELFLogger LOG = EELFManager.getInstance().getLogger(EventHandlerImpl.class);
57 * The amount of time in seconds to keep a connection to a topic open while waiting for data
59 private int READ_TIMEOUT = 60;
62 * The pool of hosts to query against
64 private Collection<String> pool;
67 * The topic to read messages from
69 private String readTopic;
72 * The topic to write messages to
74 private Set<String> writeTopics;
77 * The client (group) name to use for reading messages
79 private String clientName;
82 * The id of the client (group) that is reading messages
84 private String clientId;
87 * The api public key to use for authentication
89 private String apiKey;
92 * The api secret key to use for authentication
94 private String apiSecret;
97 * A json object containing filter arguments.
99 private String filter_json;
101 private MessageService messageService;
103 private Consumer reader = null;
104 private Producer producer = null;
106 public EventHandlerImpl(ListenerProperties props) {
107 pool = new HashSet<>();
108 writeTopics = new HashSet<>();
111 readTopic = props.getProperty(ListenerProperties.KEYS.TOPIC_READ);
112 clientName = props.getProperty(ListenerProperties.KEYS.CLIENT_NAME, "APP-C");
113 clientId = props.getProperty(ListenerProperties.KEYS.CLIENT_ID, "0");
114 apiKey = props.getProperty(ListenerProperties.KEYS.AUTH_USER_KEY);
115 apiSecret = props.getProperty(ListenerProperties.KEYS.AUTH_SECRET_KEY);
117 filter_json = props.getProperty(ListenerProperties.KEYS.TOPIC_READ_FILTER);
119 READ_TIMEOUT = Integer
120 .valueOf(props.getProperty(ListenerProperties.KEYS.TOPIC_READ_TIMEOUT, String.valueOf(READ_TIMEOUT)));
122 String hostnames = props.getProperty(ListenerProperties.KEYS.HOSTS);
123 if (hostnames != null && !hostnames.isEmpty()) {
124 for (String name : hostnames.split(",")) {
129 String writeTopicStr = props.getProperty(ListenerProperties.KEYS.TOPIC_WRITE);
130 if (writeTopicStr != null) {
131 for (String topic : writeTopicStr.split(",")) {
132 writeTopics.add(topic);
136 messageService = MessageService.parse(props.getProperty(ListenerProperties.KEYS.MESSAGE_SERVICE));
138 LOG.info(String.format(
139 "Configured to use %s client on host pool [%s]. Reading from [%s] filtered by %s. Wriring to [%s]. Authenticated using %s",
140 messageService, hostnames, readTopic, filter_json, writeTopics, apiKey));
145 public List<String> getIncomingEvents() {
146 return getIncomingEvents(1000);
150 public List<String> getIncomingEvents(int limit) {
151 List<String> out = new ArrayList<>();
152 LOG.info(String.format("Getting up to %d incoming events", limit));
153 // reuse the consumer object instead of creating a new one every time
154 if (reader == null) {
155 LOG.info("Getting Consumer...");
156 reader = getConsumer();
158 if (reader != null) {
159 List<String> items = reader.fetch(READ_TIMEOUT * 1000, limit);
160 for (String item : items) {
164 LOG.info(String.format("Read %d messages from %s as %s/%s.", out.size(), readTopic, clientName, clientId));
169 public <T> List<T> getIncomingEvents(Class<T> cls) {
170 return getIncomingEvents(cls, 1000);
174 public <T> List<T> getIncomingEvents(Class<T> cls, int limit) {
175 List<String> incomingStrings = getIncomingEvents(limit);
176 return Mapper.mapList(incomingStrings, cls);
180 public void postStatus(String event) {
181 postStatus(null, event);
185 public void postStatus(String partition, String event) {
186 LOG.debug(String.format("Posting Message [%s]", event));
187 if (producer == null) {
188 LOG.info("Getting Producer...");
189 producer = getProducer();
191 producer.post(partition, event);
195 * Returns a consumer object for direct access to our Cambria consumer interface
197 * @return An instance of the consumer interface
199 protected Consumer getConsumer() {
200 LOG.debug(String.format("Getting Consumer: %s %s/%s/%s", pool, readTopic, clientName, clientId));
201 if (filter_json == null && writeTopics.contains(readTopic)) {
203 "*****We will be writing and reading to the same topic without a filter. This will cause an infinite loop.*****");
207 BundleContext ctx = FrameworkUtil.getBundle(EventHandlerImpl.class).getBundleContext();
209 ServiceReference svcRef = ctx.getServiceReference(MessageAdapterFactory.class.getName());
210 if (svcRef != null) {
212 out = ((MessageAdapterFactory) ctx.getService(svcRef))
213 .createConsumer(pool, readTopic, clientName, clientId, filter_json, apiKey, apiSecret);
214 } catch (Exception e) {
215 //TODO:create eelf message
216 LOG.error("EvenHandlerImp.getConsumer calling MessageAdapterFactor.createConsumer", e);
219 for (String url : pool) {
220 if (url.contains("3905") || url.contains("https")) {
232 * Returns a consumer object for direct access to our Cambria producer interface
234 * @return An instance of the producer interface
236 protected Producer getProducer() {
237 LOG.debug(String.format("Getting Producer: %s %s", pool, readTopic));
240 BundleContext ctx = FrameworkUtil.getBundle(EventHandlerImpl.class).getBundleContext();
242 ServiceReference svcRef = ctx.getServiceReference(MessageAdapterFactory.class.getName());
243 if (svcRef != null) {
244 out = ((MessageAdapterFactory) ctx.getService(svcRef))
245 .createProducer(pool, writeTopics, apiKey, apiSecret);
247 for (String url : pool) {
248 if (url.contains("3905") || url.contains("https")) {
260 public void closeClients() {
261 LOG.debug("Closing Consumer and Producer DMaaP clients");
262 if (reader != null) {
265 if (producer != null) {
271 public String getClientId() {
276 public void setClientId(String clientId) {
277 this.clientId = clientId;
281 public String getClientName() {
286 public void setClientName(String clientName) {
287 this.clientName = clientName;
288 MDC.put(LoggingConstants.MDCKeys.PARTNER_NAME, clientName);
292 public void addToPool(String hostname) {
297 public Collection<String> getPool() {
302 public void removeFromPool(String hostname) {
303 pool.remove(hostname);
307 public String getReadTopic() {
312 public void setReadTopic(String readTopic) {
313 this.readTopic = readTopic;
317 public Set<String> getWriteTopics() {
322 public void setWriteTopics(Set<String> writeTopics) {
323 this.writeTopics = writeTopics;
327 public void clearCredentials() {
333 public void setCredentials(String key, String secret) {