2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.policy.drools.event.comm.bus;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Properties;
29 import org.openecomp.policy.drools.event.comm.bus.internal.SingleThreadedUebTopicSource;
30 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
31 import org.openecomp.policy.common.logging.flexlogger.Logger;
32 import org.openecomp.policy.drools.properties.PolicyProperties;
35 * UEB Topic Source Factory
37 public interface UebTopicSourceFactory {
40 * Creates an UEB Topic Source based on properties files
42 * @param properties Properties containing initialization values
44 * @return an UEB Topic Source
45 * @throws IllegalArgumentException if invalid parameters are present
47 public List<UebTopicSource> build(Properties properties)
48 throws IllegalArgumentException;
51 * Instantiates a new UEB Topic Source
53 * @param servers list of servers
54 * @param topic topic name
55 * @param apiKey API Key
56 * @param apiSecret API Secret
57 * @param consumerGroup Consumer Group
58 * @param consumerInstance Consumer Instance
59 * @param fetchTimeout Read Fetch Timeout
60 * @param fetchLimit Fetch Limit
61 * @param managed is this source endpoint managed?
63 * @return an UEB Topic Source
64 * @throws IllegalArgumentException if invalid parameters are present
66 public UebTopicSource build(List<String> servers,
71 String consumerInstance,
75 throws IllegalArgumentException;
78 * Instantiates a new UEB Topic Source
80 * @param servers list of servers
81 * @param topic topic name
82 * @param apiKey API Key
83 * @param apiSecret API Secret
85 * @return an UEB Topic Source
86 * @throws IllegalArgumentException if invalid parameters are present
88 public UebTopicSource build(List<String> servers,
92 throws IllegalArgumentException;
95 * Instantiates a new UEB Topic Source
97 * @param uebTopicSourceType Implementation type
98 * @param servers list of servers
99 * @param topic topic name
101 * @return an UEB Topic Source
102 * @throws IllegalArgumentException if invalid parameters are present
104 public UebTopicSource build(List<String> servers,
106 throws IllegalArgumentException;
109 * Destroys an UEB Topic Source based on a topic
111 * @param topic topic name
112 * @throws IllegalArgumentException if invalid parameters are present
114 public void destroy(String topic);
117 * Destroys all UEB Topic Sources
119 public void destroy();
122 * gets an UEB Topic Source based on topic name
123 * @param topic the topic name
124 * @return an UEB Topic Source with topic name
125 * @throws IllegalArgumentException if an invalid topic is provided
126 * @throws IllegalStateException if the UEB Topic Source is
129 public UebTopicSource get(String topic)
130 throws IllegalArgumentException, IllegalStateException;
133 * Provides a snapshot of the UEB Topic Sources
134 * @return a list of the UEB Topic Sources
136 public List<UebTopicSource> inventory();
139 /* ------------- implementation ----------------- */
142 * Factory of UEB Source Topics indexed by topic name
144 class IndexedUebTopicSourceFactory implements UebTopicSourceFactory {
145 // get an instance of logger
146 private static Logger logger = FlexLogger.getLogger(IndexedUebTopicSourceFactory.class);
148 * UEB Topic Name Index
150 protected HashMap<String, UebTopicSource> uebTopicSources =
151 new HashMap<String, UebTopicSource>();
157 public UebTopicSource build(List<String> servers,
161 String consumerGroup,
162 String consumerInstance,
166 throws IllegalArgumentException {
168 if (topic == null || topic.isEmpty()) {
169 throw new IllegalArgumentException("A topic must be provided");
173 if (uebTopicSources.containsKey(topic)) {
174 return uebTopicSources.get(topic);
177 UebTopicSource uebTopicSource =
178 new SingleThreadedUebTopicSource(servers, topic,
180 consumerGroup, consumerInstance,
181 fetchTimeout, fetchLimit);
184 uebTopicSources.put(topic, uebTopicSource);
186 return uebTopicSource;
194 public List<UebTopicSource> build(Properties properties)
195 throws IllegalArgumentException {
197 String readTopics = properties.getProperty(PolicyProperties.PROPERTY_UEB_SOURCE_TOPICS);
198 if (readTopics == null || readTopics.isEmpty()) {
199 logger.warn("No topic for UEB Source " + properties);
200 return new ArrayList<UebTopicSource>();
202 List<String> readTopicList = new ArrayList<String>(Arrays.asList(readTopics.split("\\s*,\\s*")));
204 List<UebTopicSource> uebTopicSources = new ArrayList<UebTopicSource>();
206 for (String topic: readTopicList) {
208 String servers = properties.getProperty(PolicyProperties.PROPERTY_UEB_SOURCE_TOPICS + "." +
210 PolicyProperties.PROPERTY_TOPIC_SERVERS_SUFFIX);
212 if (servers == null || servers.isEmpty()) {
213 logger.error("No UEB servers provided in " + properties);
217 List<String> serverList = new ArrayList<String>(Arrays.asList(servers.split("\\s*,\\s*")));
219 String apiKey = properties.getProperty(PolicyProperties.PROPERTY_UEB_SOURCE_TOPICS +
221 PolicyProperties.PROPERTY_TOPIC_API_KEY_SUFFIX);
223 String apiSecret = properties.getProperty(PolicyProperties.PROPERTY_UEB_SOURCE_TOPICS +
225 PolicyProperties.PROPERTY_TOPIC_API_SECRET_SUFFIX);
227 String consumerGroup = properties.getProperty(PolicyProperties.PROPERTY_UEB_SOURCE_TOPICS +
229 PolicyProperties.PROPERTY_TOPIC_SOURCE_CONSUMER_GROUP_SUFFIX);
231 String consumerInstance = properties.getProperty(PolicyProperties.PROPERTY_UEB_SOURCE_TOPICS +
233 PolicyProperties.PROPERTY_TOPIC_SOURCE_CONSUMER_INSTANCE_SUFFIX);
235 String fetchTimeoutString = properties.getProperty(PolicyProperties.PROPERTY_UEB_SOURCE_TOPICS +
237 PolicyProperties.PROPERTY_TOPIC_SOURCE_FETCH_TIMEOUT_SUFFIX);
238 int fetchTimeout = UebTopicSource.DEFAULT_TIMEOUT_MS_FETCH;
239 if (fetchTimeoutString != null && !fetchTimeoutString.isEmpty()) {
241 fetchTimeout = Integer.parseInt(fetchTimeoutString);
242 } catch (NumberFormatException nfe) {
243 logger.warn("Fetch Timeout in invalid format for topic " + topic + ": " + fetchTimeoutString);
247 String fetchLimitString = properties.getProperty(PolicyProperties.PROPERTY_UEB_SOURCE_TOPICS +
249 PolicyProperties.PROPERTY_TOPIC_SOURCE_FETCH_TIMEOUT_SUFFIX);
250 int fetchLimit = UebTopicSource.DEFAULT_LIMIT_FETCH;
251 if (fetchLimitString != null && !fetchLimitString.isEmpty()) {
253 fetchLimit = Integer.parseInt(fetchLimitString);
254 } catch (NumberFormatException nfe) {
255 logger.warn("Fetch Limit in invalid format for topic " + topic + ": " + fetchLimitString);
259 String managedString = properties.getProperty(PolicyProperties.PROPERTY_UEB_SOURCE_TOPICS + "." +
260 topic + PolicyProperties.PROPERTY_MANAGED_SUFFIX);
261 boolean managed = true;
262 if (managedString != null && !managedString.isEmpty()) {
263 managed = Boolean.parseBoolean(managedString);
266 UebTopicSource uebTopicSource = this.build(serverList, topic,
268 consumerGroup, consumerInstance,
269 fetchTimeout, fetchLimit, managed);
270 uebTopicSources.add(uebTopicSource);
273 return uebTopicSources;
280 public UebTopicSource build(List<String> servers,
284 return this.build(servers, topic,
287 UebTopicSource.DEFAULT_TIMEOUT_MS_FETCH,
288 UebTopicSource.DEFAULT_LIMIT_FETCH, true);
295 public UebTopicSource build(List<String> servers, String topic) {
296 return this.build(servers, topic, null, null);
303 public void destroy(String topic)
304 throws IllegalArgumentException {
306 if (topic == null || topic.isEmpty()) {
307 throw new IllegalArgumentException("A topic must be provided");
310 UebTopicSource uebTopicSource;
313 if (!uebTopicSources.containsKey(topic)) {
317 uebTopicSource = uebTopicSources.remove(topic);
320 uebTopicSource.shutdown();
327 public UebTopicSource get(String topic)
328 throws IllegalArgumentException, IllegalStateException {
330 if (topic == null || topic.isEmpty()) {
331 throw new IllegalArgumentException("A topic must be provided");
335 if (uebTopicSources.containsKey(topic)) {
336 return uebTopicSources.get(topic);
338 throw new IllegalStateException("UebTopiceSource for " + topic + " not found");
344 public synchronized List<UebTopicSource> inventory() {
345 List<UebTopicSource> readers =
346 new ArrayList<UebTopicSource>(this.uebTopicSources.values());
351 public void destroy() {
352 List<UebTopicSource> readers = this.inventory();
353 for (UebTopicSource reader: readers) {
358 this.uebTopicSources.clear();