2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Modified Copyright (C) 2018 Samsung Electronics Co., Ltd.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.common.endpoints.event.comm.bus;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Properties;
30 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
31 import org.onap.policy.common.endpoints.event.comm.bus.internal.SingleThreadedUebTopicSource;
32 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * UEB Topic Source Factory.
39 public interface UebTopicSourceFactory {
42 * Creates an UEB Topic Source based on properties files.
44 * @param properties Properties containing initialization values
46 * @return an UEB Topic Source
47 * @throws IllegalArgumentException if invalid parameters are present
49 public List<UebTopicSource> build(Properties properties);
52 * Instantiates a new UEB Topic Source.
54 * @param servers list of servers
55 * @param topic topic name
56 * @param apiKey API Key
57 * @param apiSecret API Secret
58 * @param consumerGroup Consumer Group
59 * @param consumerInstance Consumer Instance
60 * @param fetchTimeout Read Fetch Timeout
61 * @param fetchLimit Fetch Limit
62 * @param managed is this source endpoint managed?
64 * @return an UEB Topic Source
65 * @throws IllegalArgumentException if invalid parameters are present
67 public UebTopicSource build(List<String> servers, String topic, String apiKey, String apiSecret,
68 String consumerGroup, String consumerInstance,
69 int fetchTimeout, int fetchLimit, boolean managed,
70 boolean useHttps, boolean allowSelfSignedCerts);
73 * Instantiates a new UEB Topic Source.
75 * @param servers list of servers
76 * @param topic topic name
77 * @param apiKey API Key
78 * @param apiSecret API Secret
80 * @return an UEB Topic Source
81 * @throws IllegalArgumentException if invalid parameters are present
83 public UebTopicSource build(List<String> servers, String topic, String apiKey, String apiSecret);
86 * Instantiates a new UEB Topic Source.
88 * @param servers list of servers
89 * @param topic topic name
91 * @return an UEB Topic Source
92 * @throws IllegalArgumentException if invalid parameters are present
94 public UebTopicSource build(List<String> servers, String topic);
97 * Destroys an UEB Topic Source based on a topic.
99 * @param topic topic name
100 * @throws IllegalArgumentException if invalid parameters are present
102 public void destroy(String topic);
105 * Destroys all UEB Topic Sources.
107 public void destroy();
110 * Gets an UEB Topic Source based on topic name.
112 * @param topic the topic name
113 * @return an UEB Topic Source with topic name
114 * @throws IllegalArgumentException if an invalid topic is provided
115 * @throws IllegalStateException if the UEB Topic Source is an incorrect state
117 public UebTopicSource get(String topic);
120 * Provides a snapshot of the UEB Topic Sources.
122 * @return a list of the UEB Topic Sources
124 public List<UebTopicSource> inventory();
128 /* ------------- implementation ----------------- */
131 * Factory of UEB Source Topics indexed by topic name.
133 class IndexedUebTopicSourceFactory implements UebTopicSourceFactory {
134 private static final String MISSING_TOPIC = "A topic must be provided";
139 private static Logger logger = LoggerFactory.getLogger(IndexedUebTopicSourceFactory.class);
142 * UEB Topic Name Index.
144 protected HashMap<String, UebTopicSource> uebTopicSources = new HashMap<>();
150 public UebTopicSource build(List<String> servers, String topic, String apiKey, String apiSecret,
151 String consumerGroup, String consumerInstance, int fetchTimeout, int fetchLimit, boolean managed,
152 boolean useHttps, boolean allowSelfSignedCerts) {
153 if (servers == null || servers.isEmpty()) {
154 throw new IllegalArgumentException("UEB Server(s) must be provided");
157 if (topic == null || topic.isEmpty()) {
158 throw new IllegalArgumentException(MISSING_TOPIC);
161 synchronized (this) {
162 if (uebTopicSources.containsKey(topic)) {
163 return uebTopicSources.get(topic);
166 UebTopicSource uebTopicSource = new SingleThreadedUebTopicSource(BusTopicParams.builder()
170 .apiSecret(apiSecret)
171 .consumerGroup(consumerGroup)
172 .consumerInstance(consumerInstance)
173 .fetchTimeout(fetchTimeout)
174 .fetchLimit(fetchLimit)
176 .allowSelfSignedCerts(allowSelfSignedCerts)
180 uebTopicSources.put(topic, uebTopicSource);
183 return uebTopicSource;
191 public List<UebTopicSource> build(Properties properties) {
193 String readTopics = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS);
194 if (readTopics == null || readTopics.isEmpty()) {
195 logger.info("{}: no topic for UEB Source", this);
196 return new ArrayList<>();
198 List<String> readTopicList = new ArrayList<>(Arrays.asList(readTopics.split("\\s*,\\s*")));
200 List<UebTopicSource> newUebTopicSources = new ArrayList<>();
201 synchronized (this) {
202 for (String topic : readTopicList) {
203 if (this.uebTopicSources.containsKey(topic)) {
204 newUebTopicSources.add(this.uebTopicSources.get(topic));
208 String servers = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "."
209 + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SERVERS_SUFFIX);
211 if (servers == null || servers.isEmpty()) {
212 logger.error("{}: no UEB servers configured for sink {}", this, topic);
216 List<String> serverList = new ArrayList<>(Arrays.asList(servers.split("\\s*,\\s*")));
218 String apiKey = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "." + topic
219 + PolicyEndPointProperties.PROPERTY_TOPIC_API_KEY_SUFFIX);
221 String apiSecret = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "."
222 + topic + PolicyEndPointProperties.PROPERTY_TOPIC_API_SECRET_SUFFIX);
224 String consumerGroup = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "."
225 + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_CONSUMER_GROUP_SUFFIX);
227 String consumerInstance = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS
228 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_CONSUMER_INSTANCE_SUFFIX);
230 String fetchTimeoutString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS
231 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_FETCH_TIMEOUT_SUFFIX);
232 int fetchTimeout = UebTopicSource.DEFAULT_TIMEOUT_MS_FETCH;
233 if (fetchTimeoutString != null && !fetchTimeoutString.isEmpty()) {
235 fetchTimeout = Integer.parseInt(fetchTimeoutString);
236 } catch (NumberFormatException nfe) {
237 logger.warn("{}: fetch timeout {} is in invalid format for topic {} ", this, fetchTimeoutString,
242 String fetchLimitString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS
243 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_FETCH_LIMIT_SUFFIX);
244 int fetchLimit = UebTopicSource.DEFAULT_LIMIT_FETCH;
245 if (fetchLimitString != null && !fetchLimitString.isEmpty()) {
247 fetchLimit = Integer.parseInt(fetchLimitString);
248 } catch (NumberFormatException nfe) {
249 logger.warn("{}: fetch limit {} is in invalid format for topic {} ", this, fetchLimitString,
254 String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "."
255 + topic + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX);
256 boolean managed = true;
257 if (managedString != null && !managedString.isEmpty()) {
258 managed = Boolean.parseBoolean(managedString);
261 String useHttpsString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "."
262 + topic + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX);
264 // default is to use HTTP if no https property exists
265 boolean useHttps = false;
266 if (useHttpsString != null && !useHttpsString.isEmpty()) {
267 useHttps = Boolean.parseBoolean(useHttpsString);
270 String allowSelfSignedCertsString =
271 properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "." + topic
272 + PolicyEndPointProperties.PROPERTY_ALLOW_SELF_SIGNED_CERTIFICATES_SUFFIX);
274 // default is to disallow self-signed certs
275 boolean allowSelfSignedCerts = false;
276 if (allowSelfSignedCertsString != null && !allowSelfSignedCertsString.isEmpty()) {
277 allowSelfSignedCerts = Boolean.parseBoolean(allowSelfSignedCertsString);
280 UebTopicSource uebTopicSource = this.build(serverList, topic, apiKey, apiSecret, consumerGroup,
281 consumerInstance, fetchTimeout, fetchLimit, managed, useHttps, allowSelfSignedCerts);
282 newUebTopicSources.add(uebTopicSource);
285 return newUebTopicSources;
292 public UebTopicSource build(List<String> servers, String topic, String apiKey, String apiSecret) {
294 return this.build(servers, topic, apiKey, apiSecret, null, null, UebTopicSource.DEFAULT_TIMEOUT_MS_FETCH,
295 UebTopicSource.DEFAULT_LIMIT_FETCH, true, false, true);
302 public UebTopicSource build(List<String> servers, String topic) {
303 return this.build(servers, topic, null, null);
310 public void destroy(String topic) {
312 if (topic == null || topic.isEmpty()) {
313 throw new IllegalArgumentException(MISSING_TOPIC);
316 UebTopicSource uebTopicSource;
318 synchronized (this) {
319 if (!uebTopicSources.containsKey(topic)) {
323 uebTopicSource = uebTopicSources.remove(topic);
326 uebTopicSource.shutdown();
330 public void destroy() {
331 List<UebTopicSource> readers = this.inventory();
332 for (UebTopicSource reader : readers) {
336 synchronized (this) {
337 this.uebTopicSources.clear();
345 public UebTopicSource get(String topic) {
347 if (topic == null || topic.isEmpty()) {
348 throw new IllegalArgumentException(MISSING_TOPIC);
351 synchronized (this) {
352 if (uebTopicSources.containsKey(topic)) {
353 return uebTopicSources.get(topic);
355 throw new IllegalStateException("UebTopiceSource for " + topic + " not found");
361 public synchronized List<UebTopicSource> inventory() {
362 return new ArrayList<>(this.uebTopicSources.values());
366 public String toString() {
367 StringBuilder builder = new StringBuilder();
368 builder.append("IndexedUebTopicSourceFactory []");
369 return builder.toString();