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 List<UebTopicSource> build(Properties properties);
52 * Instantiates a new UEB Topic Source.
54 * servers list of servers
57 * apiSecret API Secret
58 * consumerGroup Consumer Group
59 * consumerInstance Consumer Instance
60 * fetchTimeout Read Fetch Timeout
61 * fetchLimit Fetch Limit
62 * managed is this source endpoint managed?
63 * @param busTopicParams parameters object
64 * @return an UEB Topic Source
65 * @throws IllegalArgumentException if invalid parameters are present
67 UebTopicSource build(BusTopicParams busTopicParams);
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 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 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 void destroy(String topic);
102 * Destroys all UEB Topic Sources.
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 UebTopicSource get(String topic);
117 * Provides a snapshot of the UEB Topic Sources.
119 * @return a list of the UEB Topic Sources
121 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(BusTopicParams busTopicParams) {
148 if (busTopicParams.getServers() == null || busTopicParams.getServers().isEmpty()) {
149 throw new IllegalArgumentException("UEB Server(s) must be provided");
152 if (busTopicParams.getTopic() == null || busTopicParams.getTopic().isEmpty()) {
153 throw new IllegalArgumentException(MISSING_TOPIC);
156 synchronized (this) {
157 if (uebTopicSources.containsKey(busTopicParams.getTopic())) {
158 return uebTopicSources.get(busTopicParams.getTopic());
161 UebTopicSource uebTopicSource = new SingleThreadedUebTopicSource(busTopicParams);
163 if (busTopicParams.isManaged()) {
164 uebTopicSources.put(busTopicParams.getTopic(), uebTopicSource);
167 return uebTopicSource;
175 public List<UebTopicSource> build(Properties properties) {
177 String readTopics = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS);
178 if (readTopics == null || readTopics.isEmpty()) {
179 logger.info("{}: no topic for UEB Source", this);
180 return new ArrayList<>();
182 List<String> readTopicList = new ArrayList<>(Arrays.asList(readTopics.split("\\s*,\\s*")));
184 List<UebTopicSource> newUebTopicSources = new ArrayList<>();
185 synchronized (this) {
186 for (String topic : readTopicList) {
187 if (this.uebTopicSources.containsKey(topic)) {
188 newUebTopicSources.add(this.uebTopicSources.get(topic));
192 String servers = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "."
193 + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SERVERS_SUFFIX);
195 if (servers == null || servers.isEmpty()) {
196 logger.error("{}: no UEB servers configured for sink {}", this, topic);
200 List<String> serverList = new ArrayList<>(Arrays.asList(servers.split("\\s*,\\s*")));
202 String apiKey = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "." + topic
203 + PolicyEndPointProperties.PROPERTY_TOPIC_API_KEY_SUFFIX);
205 String apiSecret = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "."
206 + topic + PolicyEndPointProperties.PROPERTY_TOPIC_API_SECRET_SUFFIX);
208 String consumerGroup = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "."
209 + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_CONSUMER_GROUP_SUFFIX);
211 String consumerInstance = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS
212 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_CONSUMER_INSTANCE_SUFFIX);
214 String fetchTimeoutString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS
215 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_FETCH_TIMEOUT_SUFFIX);
216 int fetchTimeout = UebTopicSource.DEFAULT_TIMEOUT_MS_FETCH;
217 if (fetchTimeoutString != null && !fetchTimeoutString.isEmpty()) {
219 fetchTimeout = Integer.parseInt(fetchTimeoutString);
220 } catch (NumberFormatException nfe) {
221 logger.warn("{}: fetch timeout {} is in invalid format for topic {} ", this, fetchTimeoutString,
226 String fetchLimitString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS
227 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_FETCH_LIMIT_SUFFIX);
228 int fetchLimit = UebTopicSource.DEFAULT_LIMIT_FETCH;
229 if (fetchLimitString != null && !fetchLimitString.isEmpty()) {
231 fetchLimit = Integer.parseInt(fetchLimitString);
232 } catch (NumberFormatException nfe) {
233 logger.warn("{}: fetch limit {} is in invalid format for topic {} ", this, fetchLimitString,
238 String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "."
239 + topic + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX);
240 boolean managed = true;
241 if (managedString != null && !managedString.isEmpty()) {
242 managed = Boolean.parseBoolean(managedString);
245 String useHttpsString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "."
246 + topic + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX);
248 // default is to use HTTP if no https property exists
249 boolean useHttps = false;
250 if (useHttpsString != null && !useHttpsString.isEmpty()) {
251 useHttps = Boolean.parseBoolean(useHttpsString);
254 String allowSelfSignedCertsString =
255 properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "." + topic
256 + PolicyEndPointProperties.PROPERTY_ALLOW_SELF_SIGNED_CERTIFICATES_SUFFIX);
258 // default is to disallow self-signed certs
259 boolean allowSelfSignedCerts = false;
260 if (allowSelfSignedCertsString != null && !allowSelfSignedCertsString.isEmpty()) {
261 allowSelfSignedCerts = Boolean.parseBoolean(allowSelfSignedCertsString);
264 UebTopicSource uebTopicSource = this.build(BusTopicParams.builder()
268 .apiSecret(apiSecret)
269 .consumerGroup(consumerGroup)
270 .consumerInstance(consumerInstance)
271 .fetchTimeout(fetchTimeout)
272 .fetchLimit(fetchLimit)
275 .allowSelfSignedCerts(allowSelfSignedCerts).build());
276 newUebTopicSources.add(uebTopicSource);
279 return newUebTopicSources;
286 public UebTopicSource build(List<String> servers, String topic, String apiKey, String apiSecret) {
288 return this.build(BusTopicParams.builder()
292 .apiSecret(apiSecret)
293 .fetchTimeout(UebTopicSource.DEFAULT_TIMEOUT_MS_FETCH)
294 .fetchLimit(UebTopicSource.DEFAULT_LIMIT_FETCH)
297 .allowSelfSignedCerts(true).build());
304 public UebTopicSource build(List<String> servers, String topic) {
305 return this.build(servers, topic, null, null);
312 public void destroy(String topic) {
314 if (topic == null || topic.isEmpty()) {
315 throw new IllegalArgumentException(MISSING_TOPIC);
318 UebTopicSource uebTopicSource;
320 synchronized (this) {
321 if (!uebTopicSources.containsKey(topic)) {
325 uebTopicSource = uebTopicSources.remove(topic);
328 uebTopicSource.shutdown();
332 public void destroy() {
333 List<UebTopicSource> readers = this.inventory();
334 for (UebTopicSource reader : readers) {
338 synchronized (this) {
339 this.uebTopicSources.clear();
347 public UebTopicSource get(String topic) {
349 if (topic == null || topic.isEmpty()) {
350 throw new IllegalArgumentException(MISSING_TOPIC);
353 synchronized (this) {
354 if (uebTopicSources.containsKey(topic)) {
355 return uebTopicSources.get(topic);
357 throw new IllegalStateException("UebTopiceSource for " + topic + " not found");
363 public synchronized List<UebTopicSource> inventory() {
364 return new ArrayList<>(this.uebTopicSources.values());
368 public String toString() {
369 StringBuilder builder = new StringBuilder();
370 builder.append("IndexedUebTopicSourceFactory []");
371 return builder.toString();