2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 package org.openecomp.appc.listener.impl;
24 import com.att.eelf.configuration.EELFLogger;
25 import com.att.eelf.configuration.EELFManager;
27 import org.openecomp.appc.adapter.factory.DmaapMessageAdapterFactoryImpl;
28 import org.openecomp.appc.adapter.factory.MessageService;
29 import org.openecomp.appc.adapter.message.Consumer;
30 import org.openecomp.appc.adapter.message.MessageAdapterFactory;
31 import org.openecomp.appc.adapter.message.Producer;
32 import org.openecomp.appc.listener.EventHandler;
33 import org.openecomp.appc.listener.ListenerProperties;
34 import org.openecomp.appc.listener.util.Mapper;
35 import org.openecomp.appc.logging.LoggingConstants;
36 import org.osgi.framework.BundleContext;
37 import org.osgi.framework.FrameworkUtil;
38 import org.osgi.framework.ServiceReference;
44 * This class is a wrapper for the DMaaP client provided in appc-dmaap-adapter. Its aim is to ensure that only well formed
45 * messages are sent and received on DMaaP.
48 public class EventHandlerImpl implements EventHandler {
50 private final EELFLogger LOG = EELFManager.getInstance().getLogger(EventHandlerImpl.class);
53 * The amount of time in seconds to keep a connection to a topic open while waiting for data
55 private int READ_TIMEOUT = 60;
58 * The pool of hosts to query against
60 private Collection<String> pool;
63 * The topic to read messages from
65 private String readTopic;
68 * The topic to write messages to
70 private Set<String> writeTopics;
73 * The client (group) name to use for reading messages
75 private String clientName;
78 * The id of the client (group) that is reading messages
80 private String clientId;
83 * The api public key to use for authentication
85 private String apiKey;
88 * The api secret key to use for authentication
90 private String apiSecret;
93 * A json object containing filter arguments.
95 private String filter_json;
97 private MessageService messageService;
99 private Consumer reader = null;
100 private Producer producer = null;
102 public EventHandlerImpl(ListenerProperties props) {
103 pool = new HashSet<String>();
104 writeTopics = new HashSet<String>();
107 readTopic = props.getProperty(ListenerProperties.KEYS.TOPIC_READ);
108 clientName = props.getProperty(ListenerProperties.KEYS.CLIENT_NAME, "APP-C");
109 clientId = props.getProperty(ListenerProperties.KEYS.CLIENT_ID, "0");
110 apiKey = props.getProperty(ListenerProperties.KEYS.AUTH_USER_KEY);
111 apiSecret = props.getProperty(ListenerProperties.KEYS.AUTH_SECRET_KEY);
113 filter_json = props.getProperty(ListenerProperties.KEYS.TOPIC_READ_FILTER);
115 READ_TIMEOUT = Integer
116 .valueOf(props.getProperty(ListenerProperties.KEYS.TOPIC_READ_TIMEOUT, String.valueOf(READ_TIMEOUT)));
118 String hostnames = props.getProperty(ListenerProperties.KEYS.HOSTS);
119 if (hostnames != null && !hostnames.isEmpty()) {
120 for (String name : hostnames.split(",")) {
125 String writeTopicStr = props.getProperty(ListenerProperties.KEYS.TOPIC_WRITE);
126 if (writeTopicStr != null) {
127 for (String topic : writeTopicStr.split(",")) {
128 writeTopics.add(topic);
132 messageService = MessageService.parse(props.getProperty(ListenerProperties.KEYS.MESSAGE_SERVICE));
134 LOG.info(String.format(
135 "Configured to use %s client on host pool [%s]. Reading from [%s] filtered by %s. Wriring to [%s]. Authenticated using %s",
136 messageService, hostnames, readTopic, filter_json, writeTopics, apiKey));
141 public List<String> getIncomingEvents() {
142 return getIncomingEvents(1000);
146 public List<String> getIncomingEvents(int limit) {
147 List<String> out = new ArrayList<String>();
148 LOG.info(String.format("Getting up to %d incoming events", limit));
149 // reuse the consumer object instead of creating a new one every time
150 if (reader == null) {
151 LOG.info("Getting Consumer...");
152 reader = getConsumer();
155 List<String> items = null;
157 items = reader.fetch(READ_TIMEOUT * 1000, limit);
159 LOG.error("EvenHandlerImpl.getIncomingEvents",r);
163 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 = FrameworkUtil.getBundle(EventHandlerImpl.class).getBundleContext();
211 ServiceReference svcRef = ctx.getServiceReference(MessageAdapterFactory.class.getName());
212 if (svcRef != null) {
214 out = ((MessageAdapterFactory) ctx.getService(svcRef)).createConsumer(pool, readTopic, clientName, clientId, filter_json, apiKey, apiSecret);
216 //TODO:create eelf message
217 LOG.error("EvenHandlerImp.getConsumer calling MessageAdapterFactor.createConsumer",e);
219 for (String url : pool) {
220 if (url.contains("3905") || url.contains("https")) {
231 * Returns a consumer object for direct access to our Cambria producer interface
233 * @return An instance of the producer interface
235 protected Producer getProducer() {
236 LOG.debug(String.format("Getting Producer: %s %s", pool, readTopic));
239 BundleContext ctx = FrameworkUtil.getBundle(EventHandlerImpl.class).getBundleContext();
241 ServiceReference svcRef = ctx.getServiceReference(MessageAdapterFactory.class.getName());
242 if (svcRef != null) {
243 out = ((MessageAdapterFactory) ctx.getService(svcRef)).createProducer(pool, writeTopics,apiKey, apiSecret);
244 for (String url : pool) {
245 if (url.contains("3905") || url.contains("https")) {
256 public void closeClients() {
257 LOG.debug("Closing Consumer and Producer DMaaP clients");
258 if (reader != null) {
261 if (producer != null) {
267 public String getClientId() {
272 public void setClientId(String clientId) {
273 this.clientId = clientId;
277 public String getClientName() {
282 public void setClientName(String clientName) {
283 this.clientName = clientName;
284 MDC.put(LoggingConstants.MDCKeys.PARTNER_NAME, clientName);
288 public void addToPool(String hostname) {
293 public Collection<String> getPool() {
298 public void removeFromPool(String hostname) {
299 pool.remove(hostname);
303 public String getReadTopic() {
308 public void setReadTopic(String readTopic) {
309 this.readTopic = readTopic;
313 public Set<String> getWriteTopics() {
318 public void setWriteTopics(Set<String> writeTopics) {
319 this.writeTopics = writeTopics;
323 public void clearCredentials() {
329 public void setCredentials(String key, String secret) {