2 * ============LICENSE_START=======================================================
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.HashMap;
25 import java.util.List;
26 import java.util.Properties;
27 import org.apache.commons.lang3.StringUtils;
28 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
29 import org.onap.policy.common.endpoints.event.comm.bus.internal.SingleThreadedUebTopicSource;
30 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
31 import org.onap.policy.common.endpoints.utils.PropertyUtils;
32 import org.onap.policy.common.endpoints.utils.UebPropertyUtils;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * Factory of UEB Source Topics indexed by topic name.
39 class IndexedUebTopicSourceFactory implements UebTopicSourceFactory {
40 private static final String MISSING_TOPIC = "A topic must be provided";
45 private static Logger logger = LoggerFactory.getLogger(IndexedUebTopicSourceFactory.class);
48 * UEB Topic Name Index.
50 protected HashMap<String, UebTopicSource> uebTopicSources = new HashMap<>();
53 public UebTopicSource build(BusTopicParams busTopicParams) {
54 if (busTopicParams.getServers() == null || busTopicParams.getServers().isEmpty()) {
55 throw new IllegalArgumentException("UEB Server(s) must be provided");
58 if (busTopicParams.getTopic() == null || busTopicParams.getTopic().isEmpty()) {
59 throw new IllegalArgumentException(MISSING_TOPIC);
63 if (uebTopicSources.containsKey(busTopicParams.getTopic())) {
64 return uebTopicSources.get(busTopicParams.getTopic());
67 UebTopicSource uebTopicSource = makeSource(busTopicParams);
69 if (busTopicParams.isManaged()) {
70 uebTopicSources.put(busTopicParams.getTopic(), uebTopicSource);
73 return uebTopicSource;
78 public List<UebTopicSource> build(Properties properties) {
80 String readTopics = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS);
81 if (StringUtils.isBlank(readTopics)) {
82 logger.info("{}: no topic for UEB Source", this);
83 return new ArrayList<>();
86 List<UebTopicSource> newUebTopicSources = new ArrayList<>();
88 for (String topic : readTopics.split("\\s*,\\s*")) {
89 addTopic(newUebTopicSources, topic, properties);
92 return newUebTopicSources;
96 public UebTopicSource build(List<String> servers, String topic, String apiKey, String apiSecret) {
98 return this.build(BusTopicParams.builder()
102 .apiSecret(apiSecret)
103 .fetchTimeout(PolicyEndPointProperties.DEFAULT_TIMEOUT_MS_FETCH)
104 .fetchLimit(PolicyEndPointProperties.DEFAULT_LIMIT_FETCH)
107 .allowSelfSignedCerts(true).build());
111 public UebTopicSource build(List<String> servers, String topic) {
112 return this.build(servers, topic, null, null);
115 private void addTopic(List<UebTopicSource> newUebTopicSources, String topic, Properties properties) {
116 if (this.uebTopicSources.containsKey(topic)) {
117 newUebTopicSources.add(this.uebTopicSources.get(topic));
121 String topicPrefix = PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "." + topic;
123 PropertyUtils props = new PropertyUtils(properties, topicPrefix,
124 (name, value, ex) -> logger.warn("{}: {} {} is in invalid format for topic {} ", this, name, value, topic));
126 String servers = properties.getProperty(topicPrefix + PolicyEndPointProperties.PROPERTY_TOPIC_SERVERS_SUFFIX);
127 if (StringUtils.isBlank(servers)) {
128 logger.error("{}: no UEB servers configured for sink {}", this, topic);
132 UebTopicSource uebTopicSource = this.build(UebPropertyUtils.makeBuilder(props, topic, servers)
133 .consumerGroup(props.getString(
134 PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_CONSUMER_GROUP_SUFFIX, null))
135 .consumerInstance(props.getString(
136 PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_CONSUMER_INSTANCE_SUFFIX, null))
137 .fetchTimeout(props.getInteger(
138 PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_FETCH_TIMEOUT_SUFFIX,
139 PolicyEndPointProperties.DEFAULT_TIMEOUT_MS_FETCH))
140 .fetchLimit(props.getInteger(PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_FETCH_LIMIT_SUFFIX,
141 PolicyEndPointProperties.DEFAULT_LIMIT_FETCH))
144 newUebTopicSources.add(uebTopicSource);
148 * Makes a new source.
150 * @param busTopicParams parameters to use to configure the source
151 * @return a new source
153 protected UebTopicSource makeSource(BusTopicParams busTopicParams) {
154 return new SingleThreadedUebTopicSource(busTopicParams);
158 public void destroy(String topic) {
160 if (topic == null || topic.isEmpty()) {
161 throw new IllegalArgumentException(MISSING_TOPIC);
164 UebTopicSource uebTopicSource;
166 synchronized (this) {
167 if (!uebTopicSources.containsKey(topic)) {
171 uebTopicSource = uebTopicSources.remove(topic);
174 uebTopicSource.shutdown();
178 public void destroy() {
179 List<UebTopicSource> readers = this.inventory();
180 for (UebTopicSource reader : readers) {
184 synchronized (this) {
185 this.uebTopicSources.clear();
190 public UebTopicSource get(String topic) {
192 if (topic == null || topic.isEmpty()) {
193 throw new IllegalArgumentException(MISSING_TOPIC);
196 synchronized (this) {
197 if (uebTopicSources.containsKey(topic)) {
198 return uebTopicSources.get(topic);
200 throw new IllegalStateException("UebTopiceSource for " + topic + " not found");
206 public synchronized List<UebTopicSource> inventory() {
207 return new ArrayList<>(this.uebTopicSources.values());
211 public String toString() {
212 StringBuilder builder = new StringBuilder();
213 builder.append("IndexedUebTopicSourceFactory []");
214 return builder.toString();