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 * @param busTopicParams parameters object
55 * @return an UEB Topic Source
57 UebTopicSource build(BusTopicParams busTopicParams);
60 * Instantiates a new UEB Topic Source.
62 * @param servers list of servers
63 * @param topic topic name
64 * @param apiKey API Key
65 * @param apiSecret API Secret
67 * @return an UEB Topic Source
68 * @throws IllegalArgumentException if invalid parameters are present
70 UebTopicSource build(List<String> servers, String topic, String apiKey, String apiSecret);
73 * Instantiates a new UEB Topic Source.
75 * @param servers list of servers
76 * @param topic topic name
78 * @return an UEB Topic Source
79 * @throws IllegalArgumentException if invalid parameters are present
81 UebTopicSource build(List<String> servers, String topic);
84 * Destroys an UEB Topic Source based on a topic.
86 * @param topic topic name
87 * @throws IllegalArgumentException if invalid parameters are present
89 void destroy(String topic);
92 * Destroys all UEB Topic Sources.
97 * Gets an UEB Topic Source based on topic name.
99 * @param topic the topic name
100 * @return an UEB Topic Source with topic name
101 * @throws IllegalArgumentException if an invalid topic is provided
102 * @throws IllegalStateException if the UEB Topic Source is an incorrect state
104 UebTopicSource get(String topic);
107 * Provides a snapshot of the UEB Topic Sources.
109 * @return a list of the UEB Topic Sources
111 List<UebTopicSource> inventory();
115 /* ------------- implementation ----------------- */
118 * Factory of UEB Source Topics indexed by topic name.
120 class IndexedUebTopicSourceFactory implements UebTopicSourceFactory {
121 private static final String MISSING_TOPIC = "A topic must be provided";
126 private static Logger logger = LoggerFactory.getLogger(IndexedUebTopicSourceFactory.class);
129 * UEB Topic Name Index.
131 protected HashMap<String, UebTopicSource> uebTopicSources = new HashMap<>();
134 public UebTopicSource build(BusTopicParams busTopicParams) {
135 if (busTopicParams.getServers() == null || busTopicParams.getServers().isEmpty()) {
136 throw new IllegalArgumentException("UEB Server(s) must be provided");
139 if (busTopicParams.getTopic() == null || busTopicParams.getTopic().isEmpty()) {
140 throw new IllegalArgumentException(MISSING_TOPIC);
143 synchronized (this) {
144 if (uebTopicSources.containsKey(busTopicParams.getTopic())) {
145 return uebTopicSources.get(busTopicParams.getTopic());
148 UebTopicSource uebTopicSource = makeSource(busTopicParams);
150 if (busTopicParams.isManaged()) {
151 uebTopicSources.put(busTopicParams.getTopic(), uebTopicSource);
154 return uebTopicSource;
159 public List<UebTopicSource> build(Properties properties) {
161 String readTopics = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS);
162 if (readTopics == null || readTopics.isEmpty()) {
163 logger.info("{}: no topic for UEB Source", this);
164 return new ArrayList<>();
166 List<String> readTopicList = new ArrayList<>(Arrays.asList(readTopics.split("\\s*,\\s*")));
168 List<UebTopicSource> newUebTopicSources = new ArrayList<>();
169 synchronized (this) {
170 for (String topic : readTopicList) {
171 if (this.uebTopicSources.containsKey(topic)) {
172 newUebTopicSources.add(this.uebTopicSources.get(topic));
176 String servers = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "."
177 + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SERVERS_SUFFIX);
179 if (servers == null || servers.isEmpty()) {
180 logger.error("{}: no UEB servers configured for sink {}", this, topic);
184 final List<String> serverList = new ArrayList<>(Arrays.asList(servers.split("\\s*,\\s*")));
186 final String apiKey = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS
187 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_API_KEY_SUFFIX);
189 final String apiSecret = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS
190 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_API_SECRET_SUFFIX);
192 final String consumerGroup = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS
193 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_CONSUMER_GROUP_SUFFIX);
195 final String consumerInstance = properties.getProperty(
196 PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS
197 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_CONSUMER_INSTANCE_SUFFIX);
199 String fetchTimeoutString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS
200 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_FETCH_TIMEOUT_SUFFIX);
201 int fetchTimeout = UebTopicSource.DEFAULT_TIMEOUT_MS_FETCH;
202 if (fetchTimeoutString != null && !fetchTimeoutString.isEmpty()) {
204 fetchTimeout = Integer.parseInt(fetchTimeoutString);
205 } catch (NumberFormatException nfe) {
206 logger.warn("{}: fetch timeout {} is in invalid format for topic {} ", this, fetchTimeoutString,
211 String fetchLimitString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS
212 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_FETCH_LIMIT_SUFFIX);
213 int fetchLimit = UebTopicSource.DEFAULT_LIMIT_FETCH;
214 if (fetchLimitString != null && !fetchLimitString.isEmpty()) {
216 fetchLimit = Integer.parseInt(fetchLimitString);
217 } catch (NumberFormatException nfe) {
218 logger.warn("{}: fetch limit {} is in invalid format for topic {} ", this, fetchLimitString,
223 String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "."
224 + topic + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX);
225 boolean managed = true;
226 if (managedString != null && !managedString.isEmpty()) {
227 managed = Boolean.parseBoolean(managedString);
230 String useHttpsString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "."
231 + topic + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX);
233 // default is to use HTTP if no https property exists
234 boolean useHttps = false;
235 if (useHttpsString != null && !useHttpsString.isEmpty()) {
236 useHttps = Boolean.parseBoolean(useHttpsString);
239 String allowSelfSignedCertsString =
240 properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "." + topic
241 + PolicyEndPointProperties.PROPERTY_ALLOW_SELF_SIGNED_CERTIFICATES_SUFFIX);
243 // default is to disallow self-signed certs
244 boolean allowSelfSignedCerts = false;
245 if (allowSelfSignedCertsString != null && !allowSelfSignedCertsString.isEmpty()) {
246 allowSelfSignedCerts = Boolean.parseBoolean(allowSelfSignedCertsString);
249 UebTopicSource uebTopicSource = this.build(BusTopicParams.builder()
253 .apiSecret(apiSecret)
254 .consumerGroup(consumerGroup)
255 .consumerInstance(consumerInstance)
256 .fetchTimeout(fetchTimeout)
257 .fetchLimit(fetchLimit)
260 .allowSelfSignedCerts(allowSelfSignedCerts).build());
261 newUebTopicSources.add(uebTopicSource);
264 return newUebTopicSources;
268 public UebTopicSource build(List<String> servers, String topic, String apiKey, String apiSecret) {
270 return this.build(BusTopicParams.builder()
274 .apiSecret(apiSecret)
275 .fetchTimeout(UebTopicSource.DEFAULT_TIMEOUT_MS_FETCH)
276 .fetchLimit(UebTopicSource.DEFAULT_LIMIT_FETCH)
279 .allowSelfSignedCerts(true).build());
283 public UebTopicSource build(List<String> servers, String topic) {
284 return this.build(servers, topic, null, null);
288 * Makes a new source.
290 * @param busTopicParams parameters to use to configure the source
291 * @return a new source
293 protected UebTopicSource makeSource(BusTopicParams busTopicParams) {
294 return new SingleThreadedUebTopicSource(busTopicParams);
298 public void destroy(String topic) {
300 if (topic == null || topic.isEmpty()) {
301 throw new IllegalArgumentException(MISSING_TOPIC);
304 UebTopicSource uebTopicSource;
306 synchronized (this) {
307 if (!uebTopicSources.containsKey(topic)) {
311 uebTopicSource = uebTopicSources.remove(topic);
314 uebTopicSource.shutdown();
318 public void destroy() {
319 List<UebTopicSource> readers = this.inventory();
320 for (UebTopicSource reader : readers) {
324 synchronized (this) {
325 this.uebTopicSources.clear();
330 public UebTopicSource get(String topic) {
332 if (topic == null || topic.isEmpty()) {
333 throw new IllegalArgumentException(MISSING_TOPIC);
336 synchronized (this) {
337 if (uebTopicSources.containsKey(topic)) {
338 return uebTopicSources.get(topic);
340 throw new IllegalStateException("UebTopiceSource for " + topic + " not found");
346 public synchronized List<UebTopicSource> inventory() {
347 return new ArrayList<>(this.uebTopicSources.values());
351 public String toString() {
352 StringBuilder builder = new StringBuilder();
353 builder.append("IndexedUebTopicSourceFactory []");
354 return builder.toString();