2 * ============LICENSE_START=======================================================
3 * ONAP Policy Engine - Common Modules
4 * ================================================================================
5 * Copyright (C) 2017-2019 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;
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.internal.BusTopicParams;
30 import org.onap.policy.common.endpoints.event.comm.bus.internal.SingleThreadedUebTopicSource;
31 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * Factory of UEB Source Topics indexed by topic name.
38 class IndexedUebTopicSourceFactory implements UebTopicSourceFactory {
39 private static final String MISSING_TOPIC = "A topic must be provided";
44 private static Logger logger = LoggerFactory.getLogger(IndexedUebTopicSourceFactory.class);
47 * UEB Topic Name Index.
49 protected HashMap<String, UebTopicSource> uebTopicSources = new HashMap<>();
52 public UebTopicSource build(BusTopicParams busTopicParams) {
53 if (busTopicParams.getServers() == null || busTopicParams.getServers().isEmpty()) {
54 throw new IllegalArgumentException("UEB Server(s) must be provided");
57 if (busTopicParams.getTopic() == null || busTopicParams.getTopic().isEmpty()) {
58 throw new IllegalArgumentException(MISSING_TOPIC);
62 if (uebTopicSources.containsKey(busTopicParams.getTopic())) {
63 return uebTopicSources.get(busTopicParams.getTopic());
66 UebTopicSource uebTopicSource = makeSource(busTopicParams);
68 if (busTopicParams.isManaged()) {
69 uebTopicSources.put(busTopicParams.getTopic(), uebTopicSource);
72 return uebTopicSource;
77 public List<UebTopicSource> build(Properties properties) {
79 String readTopics = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS);
80 if (readTopics == null || readTopics.isEmpty()) {
81 logger.info("{}: no topic for UEB Source", this);
82 return new ArrayList<>();
84 List<String> readTopicList = new ArrayList<>(Arrays.asList(readTopics.split("\\s*,\\s*")));
86 List<UebTopicSource> newUebTopicSources = new ArrayList<>();
88 for (String topic : readTopicList) {
89 if (this.uebTopicSources.containsKey(topic)) {
90 newUebTopicSources.add(this.uebTopicSources.get(topic));
94 String servers = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "."
95 + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SERVERS_SUFFIX);
97 if (servers == null || servers.isEmpty()) {
98 logger.error("{}: no UEB servers configured for sink {}", this, topic);
102 final List<String> serverList = new ArrayList<>(Arrays.asList(servers.split("\\s*,\\s*")));
104 final String apiKey = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS
105 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_API_KEY_SUFFIX);
107 final String apiSecret = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS
108 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_API_SECRET_SUFFIX);
110 final String consumerGroup = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS
111 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_CONSUMER_GROUP_SUFFIX);
113 final String consumerInstance = properties.getProperty(
114 PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS
115 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_CONSUMER_INSTANCE_SUFFIX);
117 String fetchTimeoutString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS
118 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_FETCH_TIMEOUT_SUFFIX);
119 int fetchTimeout = UebTopicSource.DEFAULT_TIMEOUT_MS_FETCH;
120 if (fetchTimeoutString != null && !fetchTimeoutString.isEmpty()) {
122 fetchTimeout = Integer.parseInt(fetchTimeoutString);
123 } catch (NumberFormatException nfe) {
124 logger.warn("{}: fetch timeout {} is in invalid format for topic {} ", this, fetchTimeoutString,
129 String fetchLimitString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS
130 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_FETCH_LIMIT_SUFFIX);
131 int fetchLimit = UebTopicSource.DEFAULT_LIMIT_FETCH;
132 if (fetchLimitString != null && !fetchLimitString.isEmpty()) {
134 fetchLimit = Integer.parseInt(fetchLimitString);
135 } catch (NumberFormatException nfe) {
136 logger.warn("{}: fetch limit {} is in invalid format for topic {} ", this, fetchLimitString,
141 String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "."
142 + topic + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX);
143 boolean managed = true;
144 if (managedString != null && !managedString.isEmpty()) {
145 managed = Boolean.parseBoolean(managedString);
148 String useHttpsString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "."
149 + topic + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX);
151 // default is to use HTTP if no https property exists
152 boolean useHttps = false;
153 if (useHttpsString != null && !useHttpsString.isEmpty()) {
154 useHttps = Boolean.parseBoolean(useHttpsString);
157 String allowSelfSignedCertsString =
158 properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "." + topic
159 + PolicyEndPointProperties.PROPERTY_ALLOW_SELF_SIGNED_CERTIFICATES_SUFFIX);
161 // default is to disallow self-signed certs
162 boolean allowSelfSignedCerts = false;
163 if (allowSelfSignedCertsString != null && !allowSelfSignedCertsString.isEmpty()) {
164 allowSelfSignedCerts = Boolean.parseBoolean(allowSelfSignedCertsString);
167 UebTopicSource uebTopicSource = this.build(BusTopicParams.builder()
171 .apiSecret(apiSecret)
172 .consumerGroup(consumerGroup)
173 .consumerInstance(consumerInstance)
174 .fetchTimeout(fetchTimeout)
175 .fetchLimit(fetchLimit)
178 .allowSelfSignedCerts(allowSelfSignedCerts).build());
179 newUebTopicSources.add(uebTopicSource);
182 return newUebTopicSources;
186 public UebTopicSource build(List<String> servers, String topic, String apiKey, String apiSecret) {
188 return this.build(BusTopicParams.builder()
192 .apiSecret(apiSecret)
193 .fetchTimeout(UebTopicSource.DEFAULT_TIMEOUT_MS_FETCH)
194 .fetchLimit(UebTopicSource.DEFAULT_LIMIT_FETCH)
197 .allowSelfSignedCerts(true).build());
201 public UebTopicSource build(List<String> servers, String topic) {
202 return this.build(servers, topic, null, null);
206 * Makes a new source.
208 * @param busTopicParams parameters to use to configure the source
209 * @return a new source
211 protected UebTopicSource makeSource(BusTopicParams busTopicParams) {
212 return new SingleThreadedUebTopicSource(busTopicParams);
216 public void destroy(String topic) {
218 if (topic == null || topic.isEmpty()) {
219 throw new IllegalArgumentException(MISSING_TOPIC);
222 UebTopicSource uebTopicSource;
224 synchronized (this) {
225 if (!uebTopicSources.containsKey(topic)) {
229 uebTopicSource = uebTopicSources.remove(topic);
232 uebTopicSource.shutdown();
236 public void destroy() {
237 List<UebTopicSource> readers = this.inventory();
238 for (UebTopicSource reader : readers) {
242 synchronized (this) {
243 this.uebTopicSources.clear();
248 public UebTopicSource get(String topic) {
250 if (topic == null || topic.isEmpty()) {
251 throw new IllegalArgumentException(MISSING_TOPIC);
254 synchronized (this) {
255 if (uebTopicSources.containsKey(topic)) {
256 return uebTopicSources.get(topic);
258 throw new IllegalStateException("UebTopiceSource for " + topic + " not found");
264 public synchronized List<UebTopicSource> inventory() {
265 return new ArrayList<>(this.uebTopicSources.values());
269 public String toString() {
270 StringBuilder builder = new StringBuilder();
271 builder.append("IndexedUebTopicSourceFactory []");
272 return builder.toString();