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.impl;
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.UebTopicSource;
30 import org.onap.policy.common.endpoints.event.comm.bus.UebTopicSourceFactory;
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 * Factory of UEB Source Topics indexed by topic name
39 public class IndexedUebTopicSourceFactory implements UebTopicSourceFactory {
40 private static final String MISSING_TOPIC = "A topic must be provided";
42 private static final IndexedUebTopicSourceFactory instance = new IndexedUebTopicSourceFactory();
47 private static Logger logger = LoggerFactory.getLogger(IndexedUebTopicSourceFactory.class);
50 * UEB Topic Name Index
52 protected HashMap<String, UebTopicSource> uebTopicSources = new HashMap<>();
55 * Get the singleton instance.
57 * @return the instance
59 public static IndexedUebTopicSourceFactory getInstance() {
63 private IndexedUebTopicSourceFactory() {}
69 public UebTopicSource build(List<String> servers, String topic, String apiKey, String apiSecret,
70 String consumerGroup, String consumerInstance, int fetchTimeout, int fetchLimit, boolean managed,
71 boolean useHttps, boolean allowSelfSignedCerts) {
72 if (servers == null || servers.isEmpty()) {
73 throw new IllegalArgumentException("UEB Server(s) must be provided");
76 if (topic == null || topic.isEmpty()) {
77 throw new IllegalArgumentException(MISSING_TOPIC);
81 if (uebTopicSources.containsKey(topic)) {
82 return uebTopicSources.get(topic);
85 UebTopicSource uebTopicSource = new SingleThreadedUebTopicSource(servers, topic, apiKey, apiSecret,
86 consumerGroup, consumerInstance, fetchTimeout, fetchLimit, useHttps, allowSelfSignedCerts);
89 uebTopicSources.put(topic, uebTopicSource);
92 return uebTopicSource;
100 public List<UebTopicSource> build(Properties properties) {
102 String readTopics = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS);
103 if (readTopics == null || readTopics.isEmpty()) {
104 logger.info("{}: no topic for UEB Source", this);
105 return new ArrayList<>();
107 List<String> readTopicList = new ArrayList<>(Arrays.asList(readTopics.split("\\s*,\\s*")));
109 List<UebTopicSource> newUebTopicSources = new ArrayList<>();
110 synchronized (this) {
111 for (String topic : readTopicList) {
112 if (this.uebTopicSources.containsKey(topic)) {
113 newUebTopicSources.add(this.uebTopicSources.get(topic));
117 String servers = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "." + topic
118 + PolicyEndPointProperties.PROPERTY_TOPIC_SERVERS_SUFFIX);
120 if (servers == null || servers.isEmpty()) {
121 logger.error("{}: no UEB servers configured for sink {}", this, topic);
125 List<String> serverList = new ArrayList<>(Arrays.asList(servers.split("\\s*,\\s*")));
127 String apiKey = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "." + topic
128 + PolicyEndPointProperties.PROPERTY_TOPIC_API_KEY_SUFFIX);
130 String apiSecret = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "." + topic
131 + PolicyEndPointProperties.PROPERTY_TOPIC_API_SECRET_SUFFIX);
133 String consumerGroup = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "." + topic
134 + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_CONSUMER_GROUP_SUFFIX);
136 String consumerInstance = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "."
137 + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_CONSUMER_INSTANCE_SUFFIX);
139 String fetchTimeoutString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "."
140 + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_FETCH_TIMEOUT_SUFFIX);
141 int fetchTimeout = UebTopicSource.DEFAULT_TIMEOUT_MS_FETCH;
142 if (fetchTimeoutString != null && !fetchTimeoutString.isEmpty()) {
144 fetchTimeout = Integer.parseInt(fetchTimeoutString);
145 } catch (NumberFormatException nfe) {
146 logger.warn("{}: fetch timeout {} is in invalid format for topic {} ", this, fetchTimeoutString,
151 String fetchLimitString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "."
152 + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_FETCH_LIMIT_SUFFIX);
153 int fetchLimit = UebTopicSource.DEFAULT_LIMIT_FETCH;
154 if (fetchLimitString != null && !fetchLimitString.isEmpty()) {
156 fetchLimit = Integer.parseInt(fetchLimitString);
157 } catch (NumberFormatException nfe) {
158 logger.warn("{}: fetch limit {} is in invalid format for topic {} ", this, fetchLimitString,
163 String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "." + topic
164 + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX);
165 boolean managed = true;
166 if (managedString != null && !managedString.isEmpty()) {
167 managed = Boolean.parseBoolean(managedString);
170 String useHttpsString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "." + topic
171 + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX);
173 // default is to use HTTP if no https property exists
174 boolean useHttps = false;
175 if (useHttpsString != null && !useHttpsString.isEmpty()) {
176 useHttps = Boolean.parseBoolean(useHttpsString);
179 String allowSelfSignedCertsString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS
180 + "." + topic + PolicyEndPointProperties.PROPERTY_ALLOW_SELF_SIGNED_CERTIFICATES_SUFFIX);
182 // default is to disallow self-signed certs
183 boolean allowSelfSignedCerts = false;
184 if (allowSelfSignedCertsString != null && !allowSelfSignedCertsString.isEmpty()) {
185 allowSelfSignedCerts = Boolean.parseBoolean(allowSelfSignedCertsString);
188 UebTopicSource uebTopicSource = this.build(serverList, topic, apiKey, apiSecret, consumerGroup,
189 consumerInstance, fetchTimeout, fetchLimit, managed, useHttps, allowSelfSignedCerts);
190 newUebTopicSources.add(uebTopicSource);
193 return newUebTopicSources;
200 public UebTopicSource build(List<String> servers, String topic, String apiKey, String apiSecret) {
202 return this.build(servers, topic, apiKey, apiSecret, null, null, UebTopicSource.DEFAULT_TIMEOUT_MS_FETCH,
203 UebTopicSource.DEFAULT_LIMIT_FETCH, true, false, true);
210 public UebTopicSource build(List<String> servers, String topic) {
211 return this.build(servers, topic, null, null);
218 public void destroy(String topic) {
220 if (topic == null || topic.isEmpty()) {
221 throw new IllegalArgumentException(MISSING_TOPIC);
224 UebTopicSource uebTopicSource;
226 synchronized (this) {
227 if (!uebTopicSources.containsKey(topic)) {
231 uebTopicSource = uebTopicSources.remove(topic);
234 uebTopicSource.shutdown();
241 public UebTopicSource get(String topic) {
243 if (topic == null || topic.isEmpty()) {
244 throw new IllegalArgumentException(MISSING_TOPIC);
247 synchronized (this) {
248 if (uebTopicSources.containsKey(topic)) {
249 return uebTopicSources.get(topic);
251 throw new IllegalStateException("UebTopiceSource for " + topic + " not found");
257 public synchronized List<UebTopicSource> inventory() {
258 return new ArrayList<>(this.uebTopicSources.values());
262 public void destroy() {
263 List<UebTopicSource> readers = this.inventory();
264 for (UebTopicSource reader : readers) {
268 synchronized (this) {
269 this.uebTopicSources.clear();
274 public String toString() {
275 StringBuilder builder = new StringBuilder();
276 builder.append("IndexedUebTopicSourceFactory []");
277 return builder.toString();