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";
45 private Consumer consumer;
46 private Producer producer;
47 private int readLimit;
50 @SuppressWarnings("Since15")
51 public void init(Properties props)
52 throws IOException, GeneralSecurityException, NoSuchFieldException, IllegalAccessException {
55 String readTopic = props.getProperty(UEBPropertiesKeys.TOPIC_READ);
56 String writeTopic = props.getProperty(UEBPropertiesKeys.TOPIC_WRITE);
57 String cType = props.getProperty(UEBPropertiesKeys.CONTROLLER_TYPE);
58 if (cType != null && cType.length()!= 0 && (!cType.equals("APPC")))
60 readTopic = cType + "-" + readTopic;
61 writeTopic = cType + "-" + writeTopic;
63 String apiKey = props.getProperty(UEBPropertiesKeys.AUTH_USER);
64 String apiSecret = props.getProperty(UEBPropertiesKeys.AUTH_SECRET);
65 String readTimeoutString = props.getProperty(UEBPropertiesKeys.TOPIC_READ_TIMEOUT, DEFAULT_READ_TIMEOUT_MS);
66 Integer readTimeout = Integer.parseInt(readTimeoutString);
67 String readLimitString = props.getProperty(UEBPropertiesKeys.READ_LIMIT, DEFAULT_READ_LIMIT);
68 readLimit = Integer.parseInt(readLimitString);
70 Collection<String> pool = new HashSet<>();
71 String hostNames = props.getProperty(UEBPropertiesKeys.HOSTS);
72 if (hostNames != null && !hostNames.isEmpty()) {
73 pool.addAll(Arrays.asList(hostNames.split(",")));
75 //generate consumer id and group - same value for both
76 String consumerName = UUID.randomUUID().toString();
78 //create consumer and producer
79 consumer = new ConsumerImpl(pool, readTopic, consumerName, consumerName, readTimeout, apiKey, apiSecret);
80 producer = new ProducerImpl(pool, writeTopic, apiKey, apiSecret);
82 //initial consumer registration
84 consumer.registerForRead();
85 } catch (Exception e) {
86 logger.error("Message consumer failed to register client " + consumerName, e);
92 public void send(String partition, String body) throws IOException {
93 producer.post(partition, body);
97 public List<String> fetch() throws IOException {
98 return consumer.fetch(readLimit);
102 public List<String> fetch(int limit) throws IOException {
103 return consumer.fetch(limit);
107 public void close() {