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<>();
137 public UebTopicSource build(BusTopicParams busTopicParams) {
138 if (busTopicParams.getServers() == null || busTopicParams.getServers().isEmpty()) {
139 throw new IllegalArgumentException("UEB Server(s) must be provided");
142 if (busTopicParams.getTopic() == null || busTopicParams.getTopic().isEmpty()) {
143 throw new IllegalArgumentException(MISSING_TOPIC);
146 synchronized (this) {
147 if (uebTopicSources.containsKey(busTopicParams.getTopic())) {
148 return uebTopicSources.get(busTopicParams.getTopic());
151 UebTopicSource uebTopicSource = new SingleThreadedUebTopicSource(busTopicParams);
153 if (busTopicParams.isManaged()) {
154 uebTopicSources.put(busTopicParams.getTopic(), uebTopicSource);
157 return uebTopicSource;
165 public List<UebTopicSource> build(Properties properties) {
167 String readTopics = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS);
168 if (readTopics == null || readTopics.isEmpty()) {
169 logger.info("{}: no topic for UEB Source", this);
170 return new ArrayList<>();
172 List<String> readTopicList = new ArrayList<>(Arrays.asList(readTopics.split("\\s*,\\s*")));
174 List<UebTopicSource> newUebTopicSources = new ArrayList<>();
175 synchronized (this) {
176 for (String topic : readTopicList) {
177 if (this.uebTopicSources.containsKey(topic)) {
178 newUebTopicSources.add(this.uebTopicSources.get(topic));
182 String servers = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "."
183 + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SERVERS_SUFFIX);
185 if (servers == null || servers.isEmpty()) {
186 logger.error("{}: no UEB servers configured for sink {}", this, topic);
190 List<String> serverList = new ArrayList<>(Arrays.asList(servers.split("\\s*,\\s*")));
192 String apiKey = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "." + topic
193 + PolicyEndPointProperties.PROPERTY_TOPIC_API_KEY_SUFFIX);
195 String apiSecret = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "."
196 + topic + PolicyEndPointProperties.PROPERTY_TOPIC_API_SECRET_SUFFIX);
198 String consumerGroup = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "."
199 + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_CONSUMER_GROUP_SUFFIX);
201 String consumerInstance = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS
202 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_CONSUMER_INSTANCE_SUFFIX);
204 String fetchTimeoutString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS
205 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_FETCH_TIMEOUT_SUFFIX);
206 int fetchTimeout = UebTopicSource.DEFAULT_TIMEOUT_MS_FETCH;
207 if (fetchTimeoutString != null && !fetchTimeoutString.isEmpty()) {
209 fetchTimeout = Integer.parseInt(fetchTimeoutString);
210 } catch (NumberFormatException nfe) {
211 logger.warn("{}: fetch timeout {} is in invalid format for topic {} ", this, fetchTimeoutString,
216 String fetchLimitString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS
217 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_FETCH_LIMIT_SUFFIX);
218 int fetchLimit = UebTopicSource.DEFAULT_LIMIT_FETCH;
219 if (fetchLimitString != null && !fetchLimitString.isEmpty()) {
221 fetchLimit = Integer.parseInt(fetchLimitString);
222 } catch (NumberFormatException nfe) {
223 logger.warn("{}: fetch limit {} is in invalid format for topic {} ", this, fetchLimitString,
228 String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "."
229 + topic + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX);
230 boolean managed = true;
231 if (managedString != null && !managedString.isEmpty()) {
232 managed = Boolean.parseBoolean(managedString);
235 String useHttpsString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "."
236 + topic + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX);
238 // default is to use HTTP if no https property exists
239 boolean useHttps = false;
240 if (useHttpsString != null && !useHttpsString.isEmpty()) {
241 useHttps = Boolean.parseBoolean(useHttpsString);
244 String allowSelfSignedCertsString =
245 properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "." + topic
246 + PolicyEndPointProperties.PROPERTY_ALLOW_SELF_SIGNED_CERTIFICATES_SUFFIX);
248 // default is to disallow self-signed certs
249 boolean allowSelfSignedCerts = false;
250 if (allowSelfSignedCertsString != null && !allowSelfSignedCertsString.isEmpty()) {
251 allowSelfSignedCerts = Boolean.parseBoolean(allowSelfSignedCertsString);
254 UebTopicSource uebTopicSource = this.build(BusTopicParams.builder()
258 .apiSecret(apiSecret)
259 .consumerGroup(consumerGroup)
260 .consumerInstance(consumerInstance)
261 .fetchTimeout(fetchTimeout)
262 .fetchLimit(fetchLimit)
265 .allowSelfSignedCerts(allowSelfSignedCerts).build());
266 newUebTopicSources.add(uebTopicSource);
269 return newUebTopicSources;
276 public UebTopicSource build(List<String> servers, String topic, String apiKey, String apiSecret) {
278 return this.build(BusTopicParams.builder()
282 .apiSecret(apiSecret)
283 .fetchTimeout(UebTopicSource.DEFAULT_TIMEOUT_MS_FETCH)
284 .fetchLimit(UebTopicSource.DEFAULT_LIMIT_FETCH)
287 .allowSelfSignedCerts(true).build());
294 public UebTopicSource build(List<String> servers, String topic) {
295 return this.build(servers, topic, null, null);
302 public void destroy(String topic) {
304 if (topic == null || topic.isEmpty()) {
305 throw new IllegalArgumentException(MISSING_TOPIC);
308 UebTopicSource uebTopicSource;
310 synchronized (this) {
311 if (!uebTopicSources.containsKey(topic)) {
315 uebTopicSource = uebTopicSources.remove(topic);
318 uebTopicSource.shutdown();
322 public void destroy() {
323 List<UebTopicSource> readers = this.inventory();
324 for (UebTopicSource reader : readers) {
328 synchronized (this) {
329 this.uebTopicSources.clear();
337 public UebTopicSource get(String topic) {
339 if (topic == null || topic.isEmpty()) {
340 throw new IllegalArgumentException(MISSING_TOPIC);
343 synchronized (this) {
344 if (uebTopicSources.containsKey(topic)) {
345 return uebTopicSources.get(topic);
347 throw new IllegalStateException("UebTopiceSource for " + topic + " not found");
353 public synchronized List<UebTopicSource> inventory() {
354 return new ArrayList<>(this.uebTopicSources.values());
358 public String toString() {
359 StringBuilder builder = new StringBuilder();
360 builder.append("IndexedUebTopicSourceFactory []");
361 return builder.toString();