2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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.onap.policy.common.endpoints.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.onap.policy.common.endpoints.event.comm.bus.internal.SingleThreadedUebTopicSource;
30 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
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);
50 * Instantiates a new UEB Topic Source
52 * @param servers list of servers
53 * @param topic topic name
54 * @param apiKey API Key
55 * @param apiSecret API Secret
56 * @param consumerGroup Consumer Group
57 * @param consumerInstance Consumer Instance
58 * @param fetchTimeout Read Fetch Timeout
59 * @param fetchLimit Fetch Limit
60 * @param managed is this source endpoint managed?
62 * @return an UEB Topic Source
63 * @throws IllegalArgumentException if invalid parameters are present
65 public UebTopicSource build(List<String> servers, String topic, String apiKey, String apiSecret,
66 String consumerGroup, String consumerInstance, int fetchTimeout, int fetchLimit, boolean managed,
67 boolean useHttps, boolean allowSelfSignedCerts);
70 * Instantiates a new UEB Topic Source
72 * @param servers list of servers
73 * @param topic topic name
74 * @param apiKey API Key
75 * @param apiSecret API Secret
77 * @return an UEB Topic Source
78 * @throws IllegalArgumentException if invalid parameters are present
80 public UebTopicSource build(List<String> servers, String topic, String apiKey, String apiSecret);
83 * Instantiates a new UEB Topic Source
85 * @param servers list of servers
86 * @param topic topic name
88 * @return an UEB Topic Source
89 * @throws IllegalArgumentException if invalid parameters are present
91 public UebTopicSource build(List<String> servers, String topic);
94 * Destroys an UEB Topic Source based on a topic
96 * @param topic topic name
97 * @throws IllegalArgumentException if invalid parameters are present
99 public void destroy(String topic);
102 * Destroys all UEB Topic Sources
104 public void destroy();
107 * gets an UEB Topic Source based on topic name
109 * @param topic the topic name
110 * @return an UEB Topic Source with topic name
111 * @throws IllegalArgumentException if an invalid topic is provided
112 * @throws IllegalStateException if the UEB Topic Source is an incorrect state
114 public UebTopicSource get(String topic);
117 * Provides a snapshot of the UEB Topic Sources
119 * @return a list of the UEB Topic Sources
121 public List<UebTopicSource> inventory();
125 /* ------------- implementation ----------------- */
128 * Factory of UEB Source Topics indexed by topic name
130 class IndexedUebTopicSourceFactory implements UebTopicSourceFactory {
131 private static final String MISSING_TOPIC = "A topic must be provided";
136 private static Logger logger = LoggerFactory.getLogger(IndexedUebTopicSourceFactory.class);
139 * UEB Topic Name Index
141 protected HashMap<String, UebTopicSource> uebTopicSources = new HashMap<>();
147 public UebTopicSource build(List<String> servers, String topic, String apiKey, String apiSecret,
148 String consumerGroup, String consumerInstance, int fetchTimeout, int fetchLimit, boolean managed,
149 boolean useHttps, boolean allowSelfSignedCerts) {
150 if (servers == null || servers.isEmpty()) {
151 throw new IllegalArgumentException("UEB Server(s) must be provided");
154 if (topic == null || topic.isEmpty()) {
155 throw new IllegalArgumentException(MISSING_TOPIC);
158 synchronized (this) {
159 if (uebTopicSources.containsKey(topic)) {
160 return uebTopicSources.get(topic);
163 UebTopicSource uebTopicSource = new SingleThreadedUebTopicSource(servers, topic, apiKey, apiSecret,
164 consumerGroup, consumerInstance, fetchTimeout, fetchLimit, useHttps, allowSelfSignedCerts);
167 uebTopicSources.put(topic, uebTopicSource);
170 return uebTopicSource;
178 public List<UebTopicSource> build(Properties properties) {
180 String readTopics = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS);
181 if (readTopics == null || readTopics.isEmpty()) {
182 logger.info("{}: no topic for UEB Source", this);
183 return new ArrayList<>();
185 List<String> readTopicList = new ArrayList<>(Arrays.asList(readTopics.split("\\s*,\\s*")));
187 List<UebTopicSource> newUebTopicSources = new ArrayList<>();
188 synchronized (this) {
189 for (String topic : readTopicList) {
190 if (this.uebTopicSources.containsKey(topic)) {
191 newUebTopicSources.add(this.uebTopicSources.get(topic));
195 String servers = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "."
196 + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SERVERS_SUFFIX);
198 if (servers == null || servers.isEmpty()) {
199 logger.error("{}: no UEB servers configured for sink {}", this, topic);
203 List<String> serverList = new ArrayList<>(Arrays.asList(servers.split("\\s*,\\s*")));
205 String apiKey = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "." + topic
206 + PolicyEndPointProperties.PROPERTY_TOPIC_API_KEY_SUFFIX);
208 String apiSecret = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "."
209 + topic + PolicyEndPointProperties.PROPERTY_TOPIC_API_SECRET_SUFFIX);
211 String consumerGroup = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "."
212 + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_CONSUMER_GROUP_SUFFIX);
214 String consumerInstance = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS
215 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_CONSUMER_INSTANCE_SUFFIX);
217 String fetchTimeoutString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS
218 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_FETCH_TIMEOUT_SUFFIX);
219 int fetchTimeout = UebTopicSource.DEFAULT_TIMEOUT_MS_FETCH;
220 if (fetchTimeoutString != null && !fetchTimeoutString.isEmpty()) {
222 fetchTimeout = Integer.parseInt(fetchTimeoutString);
223 } catch (NumberFormatException nfe) {
224 logger.warn("{}: fetch timeout {} is in invalid format for topic {} ", this, fetchTimeoutString,
229 String fetchLimitString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS
230 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_FETCH_LIMIT_SUFFIX);
231 int fetchLimit = UebTopicSource.DEFAULT_LIMIT_FETCH;
232 if (fetchLimitString != null && !fetchLimitString.isEmpty()) {
234 fetchLimit = Integer.parseInt(fetchLimitString);
235 } catch (NumberFormatException nfe) {
236 logger.warn("{}: fetch limit {} is in invalid format for topic {} ", this, fetchLimitString,
241 String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "."
242 + topic + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX);
243 boolean managed = true;
244 if (managedString != null && !managedString.isEmpty()) {
245 managed = Boolean.parseBoolean(managedString);
248 String useHttpsString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "."
249 + topic + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX);
251 // default is to use HTTP if no https property exists
252 boolean useHttps = false;
253 if (useHttpsString != null && !useHttpsString.isEmpty()) {
254 useHttps = Boolean.parseBoolean(useHttpsString);
257 String allowSelfSignedCertsString =
258 properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "." + topic
259 + PolicyEndPointProperties.PROPERTY_ALLOW_SELF_SIGNED_CERTIFICATES_SUFFIX);
261 // default is to disallow self-signed certs
262 boolean allowSelfSignedCerts = false;
263 if (allowSelfSignedCertsString != null && !allowSelfSignedCertsString.isEmpty()) {
264 allowSelfSignedCerts = Boolean.parseBoolean(allowSelfSignedCertsString);
267 UebTopicSource uebTopicSource = this.build(serverList, topic, apiKey, apiSecret, consumerGroup,
268 consumerInstance, fetchTimeout, fetchLimit, managed, useHttps, allowSelfSignedCerts);
269 newUebTopicSources.add(uebTopicSource);
272 return newUebTopicSources;
279 public UebTopicSource build(List<String> servers, String topic, String apiKey, String apiSecret) {
281 return this.build(servers, topic, apiKey, apiSecret, null, null, UebTopicSource.DEFAULT_TIMEOUT_MS_FETCH,
282 UebTopicSource.DEFAULT_LIMIT_FETCH, true, false, true);
289 public UebTopicSource build(List<String> servers, String topic) {
290 return this.build(servers, topic, null, null);
297 public void destroy(String topic) {
299 if (topic == null || topic.isEmpty()) {
300 throw new IllegalArgumentException(MISSING_TOPIC);
303 UebTopicSource uebTopicSource;
305 synchronized (this) {
306 if (!uebTopicSources.containsKey(topic)) {
310 uebTopicSource = uebTopicSources.remove(topic);
313 uebTopicSource.shutdown();
320 public UebTopicSource get(String topic) {
322 if (topic == null || topic.isEmpty()) {
323 throw new IllegalArgumentException(MISSING_TOPIC);
326 synchronized (this) {
327 if (uebTopicSources.containsKey(topic)) {
328 return uebTopicSources.get(topic);
330 throw new IllegalStateException("UebTopiceSource for " + topic + " not found");
336 public synchronized List<UebTopicSource> inventory() {
337 return new ArrayList<>(this.uebTopicSources.values());
341 public void destroy() {
342 List<UebTopicSource> readers = this.inventory();
343 for (UebTopicSource reader : readers) {
347 synchronized (this) {
348 this.uebTopicSources.clear();
353 public String toString() {
354 StringBuilder builder = new StringBuilder();
355 builder.append("IndexedUebTopicSourceFactory []");
356 return builder.toString();