2 * ============LICENSE_START=======================================================
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 Ericsson
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
15 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 * ============LICENSE_END=========================================================
26 package org.onap.appc.listener.impl;
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
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;
106 * Blacklist time for a server with response problem in seconds
108 private String responseProblemBlacklistTime;
111 * Blacklist time for a server with server problem in seconds
113 private String serverProblemBlacklistTime;
116 * Blacklist time for a server with DNS problem in seconds
118 private String dnsIssueBlacklistTime;
121 * Blacklist time for a server with IO Exception problem in seconds
123 private String ioExceptionBlacklistTime;
125 private MessageService messageService;
127 private Consumer reader = null;
128 private Producer producer = null;
130 public EventHandlerImpl(ListenerProperties props) {
131 pool = new HashSet<>();
132 writeTopics = new HashSet<>();
135 readTopic = props.getProperty(ListenerProperties.KEYS.TOPIC_READ);
136 clientName = props.getProperty(ListenerProperties.KEYS.CLIENT_NAME, "APP-C");
137 clientId = props.getProperty(ListenerProperties.KEYS.CLIENT_ID, "0");
138 apiKey = props.getProperty(ListenerProperties.KEYS.AUTH_USER_KEY);
139 apiSecret = props.getProperty(ListenerProperties.KEYS.AUTH_SECRET_KEY);
140 responseProblemBlacklistTime = props.getProperty(ListenerProperties.KEYS.PROBLEM_WITH_RESPONSE_BLACKLIST_TIME);
141 serverProblemBlacklistTime = props.getProperty(ListenerProperties.KEYS.PROBLEM_SERVERSIDE_ERROR_BLACKLIST_TIME);
142 dnsIssueBlacklistTime = props.getProperty(ListenerProperties.KEYS.PROBLEM_DNS_BLACKLIST_TIME);
143 ioExceptionBlacklistTime = props.getProperty(ListenerProperties.KEYS.PROBLEM_IO_EXCEPTION_BLACKLIST_TIME);
145 filter_json = props.getProperty(ListenerProperties.KEYS.TOPIC_READ_FILTER);
147 READ_TIMEOUT = Integer
148 .valueOf(props.getProperty(ListenerProperties.KEYS.TOPIC_READ_TIMEOUT, String.valueOf(READ_TIMEOUT)));
150 String hostnames = props.getProperty(ListenerProperties.KEYS.HOSTS);
151 if (hostnames != null && !hostnames.isEmpty()) {
152 for (String name : hostnames.split(",")) {
157 String writeTopicStr = props.getProperty(ListenerProperties.KEYS.TOPIC_WRITE);
158 if (writeTopicStr != null) {
159 for (String topic : writeTopicStr.split(",")) {
160 writeTopics.add(topic);
164 messageService = MessageService.parse(props.getProperty(ListenerProperties.KEYS.MESSAGE_SERVICE));
166 LOG.info(String.format(
167 "Configured to use %s client on host pool [%s]. Reading from [%s] filtered by %s. Writing to [%s]. Authenticated using %s",
168 messageService, hostnames, readTopic, filter_json, writeTopics, apiKey));
173 public List<String> getIncomingEvents() {
174 return getIncomingEvents(1000);
178 public List<String> getIncomingEvents(int limit) {
179 List<String> out = new ArrayList<>();
180 LOG.info(String.format("Getting up to %d incoming events", limit));
181 // reuse the consumer object instead of creating a new one every time
182 if (reader == null) {
183 LOG.info("Getting Consumer...");
184 reader = getConsumer();
186 if (reader != null) {
187 List<String> items = reader.fetch(READ_TIMEOUT * 1000, limit);
188 for (String item : items) {
192 LOG.info(String.format("Read %d messages from %s as %s/%s.", out.size(), readTopic, clientName, clientId));
197 public <T> List<T> getIncomingEvents(Class<T> cls) {
198 return getIncomingEvents(cls, 1000);
202 public <T> List<T> getIncomingEvents(Class<T> cls, int limit) {
203 List<String> incomingStrings = getIncomingEvents(limit);
204 return Mapper.mapList(incomingStrings, cls);
208 public void postStatus(String event) {
209 postStatus(null, event);
213 public void postStatus(String partition, String event) {
214 LOG.debug(String.format("Posting Message [%s]", event));
215 if (producer == null) {
216 LOG.info("Getting Producer...");
217 producer = getProducer();
219 producer.post(partition, event);
223 * Returns a consumer object for direct access to our Cambria consumer interface
225 * @return An instance of the consumer interface
227 protected Consumer getConsumer() {
228 LOG.debug(String.format("Getting Consumer: %s %s/%s/%s", pool, readTopic, clientName, clientId));
229 if (filter_json == null && writeTopics.contains(readTopic)) {
231 "*****We will be writing and reading to the same topic without a filter. This will cause an infinite loop.*****");
235 BundleContext ctx = null;
236 Bundle bundle = FrameworkUtil.getBundle(EventHandlerImpl.class);
238 ctx = bundle.getBundleContext();
242 ServiceReference svcRef = ctx.getServiceReference(MessageAdapterFactory.class.getName());
243 if (svcRef != null) {
245 out = ((MessageAdapterFactory) ctx.getService(svcRef))
246 .createConsumer(pool, readTopic, clientName, clientId, filter_json, apiKey, apiSecret);
248 if (out != null && responseProblemBlacklistTime != null && responseProblemBlacklistTime.length() > 0)
250 out.setResponseProblemBlacklistTime(responseProblemBlacklistTime);
253 if (out != null && serverProblemBlacklistTime != null && serverProblemBlacklistTime.length() > 0)
255 out.setServerProblemBlacklistTime(serverProblemBlacklistTime);
258 if (out != null && dnsIssueBlacklistTime != null && dnsIssueBlacklistTime.length() > 0)
260 out.setDnsIssueBlacklistTime(dnsIssueBlacklistTime);
263 if (out != null && ioExceptionBlacklistTime != null && ioExceptionBlacklistTime.length() > 0)
265 out.setIOExceptionBlacklistTime(ioExceptionBlacklistTime);
267 } catch (Exception e) {
268 //TODO:create eelf message
269 LOG.error("EvenHandlerImp.getConsumer calling MessageAdapterFactor.createConsumer", e);
272 for (String url : pool) {
273 if (url.contains("3905") || url.contains("https")) {
285 * Returns a consumer object for direct access to our Cambria producer interface
287 * @return An instance of the producer interface
289 protected Producer getProducer() {
290 LOG.debug(String.format("Getting Producer: %s %s", pool, readTopic));
293 BundleContext ctx = FrameworkUtil.getBundle(EventHandlerImpl.class).getBundleContext();
295 ServiceReference svcRef = ctx.getServiceReference(MessageAdapterFactory.class.getName());
296 if (svcRef != null) {
297 out = ((MessageAdapterFactory) ctx.getService(svcRef))
298 .createProducer(pool, writeTopics, apiKey, apiSecret);
299 if (out != null && responseProblemBlacklistTime != null && responseProblemBlacklistTime.length() > 0)
301 out.setResponseProblemBlacklistTime(responseProblemBlacklistTime);
304 if (out != null && serverProblemBlacklistTime != null && serverProblemBlacklistTime.length() > 0)
306 out.setServerProblemBlacklistTime(serverProblemBlacklistTime);
309 if (out != null && dnsIssueBlacklistTime != null && dnsIssueBlacklistTime.length() > 0)
311 out.setDnsIssueBlacklistTime(dnsIssueBlacklistTime);
314 if (out != null && ioExceptionBlacklistTime != null && ioExceptionBlacklistTime.length() > 0)
316 out.setIOExceptionBlacklistTime(ioExceptionBlacklistTime);
319 for (String url : pool) {
320 if (url.contains("3905") || url.contains("https")) {
332 public void closeClients() {
333 LOG.debug("Closing Consumer and Producer DMaaP clients");
334 if (reader != null) {
337 if (producer != null) {
343 public String getClientId() {
348 public void setClientId(String clientId) {
349 this.clientId = clientId;
353 public String getClientName() {
358 public void setClientName(String clientName) {
359 this.clientName = clientName;
360 MDC.put(LoggingConstants.MDCKeys.PARTNER_NAME, clientName);
364 public void addToPool(String hostname) {
369 public Collection<String> getPool() {
374 public void removeFromPool(String hostname) {
375 pool.remove(hostname);
379 public String getReadTopic() {
384 public void setReadTopic(String readTopic) {
385 this.readTopic = readTopic;
389 public Set<String> getWriteTopics() {
394 public void setWriteTopics(Set<String> writeTopics) {
395 this.writeTopics = writeTopics;
399 public void setResponseProblemBlacklistTime(String duration){
400 this.responseProblemBlacklistTime = duration;
404 public void setServerProblemBlacklistTime(String duration){
405 this.serverProblemBlacklistTime = duration;
409 public void setDnsIssueBlacklistTime(String duration){
410 this.dnsIssueBlacklistTime = duration;
414 public void setIOExceptionBlacklistTime(String duration){
415 this.ioExceptionBlacklistTime = duration;
419 public void clearCredentials() {
425 public void setCredentials(String key, String secret) {