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.onap.appc.client.impl.protocol;
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import java.io.IOException;
30 import java.security.GeneralSecurityException;
31 import java.util.Arrays;
32 import java.util.Collection;
33 import java.util.HashSet;
34 import java.util.List;
35 import java.util.Properties;
36 import java.util.UUID;
38 class UEBMessagingService implements MessagingService {
40 private final EELFLogger logger = EELFManager.getInstance().getLogger(UEBMessagingService.class);
42 private static final String DEFAULT_READ_TIMEOUT_MS = "60000";
43 private static final String DEFAULT_READ_LIMIT = "1000";
44 private static final String DEFAULT_READ_TOPIC = "client-read";
45 private static final String DEFAULT_WRITE_TOPIC = "client-write";
47 private Consumer consumer;
48 private Producer producer;
49 private int readLimit;
52 @SuppressWarnings("Since15")
53 public void init(Properties props)
54 throws IOException, GeneralSecurityException, NoSuchFieldException, IllegalAccessException {
57 String readTopic = null;
58 String writeTopic = null;
59 String cType = props.getProperty(UEBPropertiesKeys.CONTROLLER_TYPE); //CONTROLLER_TYPE = "controllerType"
61 if (cType != null && cType.length()!= 0 && (!cType.equals("APPC")))
63 logger.debug("Using controller type " + cType + " for topic properties");
65 readTopic = props.getProperty(cType + "-" + UEBPropertiesKeys.TOPIC_READ);
66 if(readTopic == null) {
67 logger.error("Error reading property '"+ cType + "-" + UEBPropertiesKeys.TOPIC_READ + "' defaulting to " + DEFAULT_READ_TOPIC);
68 readTopic = DEFAULT_READ_TOPIC;
70 writeTopic = props.getProperty(cType + "-" + UEBPropertiesKeys.TOPIC_WRITE);
71 if(writeTopic == null) {
72 logger.error("Error reading property '"+ cType + "-" + UEBPropertiesKeys.TOPIC_READ + "' defaulting to " + DEFAULT_WRITE_TOPIC);
73 writeTopic = DEFAULT_WRITE_TOPIC;
77 readTopic = props.getProperty(UEBPropertiesKeys.TOPIC_READ); //TOPIC_READ = "topic.read"
78 if(readTopic == null) {
79 logger.error("Error reading property '"+ UEBPropertiesKeys.TOPIC_READ + "' defaulting to " + DEFAULT_READ_TOPIC);
80 readTopic = DEFAULT_READ_TOPIC;
82 writeTopic = props.getProperty(UEBPropertiesKeys.TOPIC_WRITE); //TOPIC_WRITE = "topic.write"
83 if(writeTopic == null) {
84 logger.error("Error reading property '" + UEBPropertiesKeys.TOPIC_READ + "' defaulting to " + DEFAULT_WRITE_TOPIC);
85 writeTopic = DEFAULT_WRITE_TOPIC;
89 logger.debug("Using topics: Read = '" + readTopic + "' Write = '" + writeTopic + "'");
91 String apiKey = props.getProperty(UEBPropertiesKeys.AUTH_USER);
92 String apiSecret = props.getProperty(UEBPropertiesKeys.AUTH_SECRET);
93 String readTimeoutString = props.getProperty(UEBPropertiesKeys.TOPIC_READ_TIMEOUT, DEFAULT_READ_TIMEOUT_MS);
94 Integer readTimeout = Integer.parseInt(readTimeoutString);
95 String readLimitString = props.getProperty(UEBPropertiesKeys.READ_LIMIT, DEFAULT_READ_LIMIT);
96 readLimit = Integer.parseInt(readLimitString);
98 Collection<String> pool = new HashSet<>();
99 String hostNames = props.getProperty(UEBPropertiesKeys.HOSTS);
100 if (hostNames != null && !hostNames.isEmpty()) {
101 pool.addAll(Arrays.asList(hostNames.split(",")));
103 //generate consumer id and group - same value for both
104 String consumerName = UUID.randomUUID().toString();
106 //create consumer and producer
107 consumer = new ConsumerImpl(pool, readTopic, consumerName, consumerName, readTimeout, apiKey, apiSecret);
108 producer = new ProducerImpl(pool, writeTopic, apiKey, apiSecret);
110 //initial consumer registration
112 consumer.registerForRead();
113 } catch (Exception e) {
114 logger.error("Message consumer failed to register client " + consumerName, e);
120 public void send(String partition, String body) throws IOException {
121 producer.post(partition, body);
125 public List<String> fetch() throws IOException {
126 return consumer.fetch(readLimit);
130 public List<String> fetch(int limit) throws IOException {
131 return consumer.fetch(limit);
135 public void close() {