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.SingleThreadedDmaapTopicSource;
30 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
31 import org.onap.policy.common.endpoints.utils.DmaapPropertyUtils;
32 import org.onap.policy.common.endpoints.utils.PropertyUtils;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * Factory of DMAAP Source Topics indexed by topic name.
40 class IndexedDmaapTopicSourceFactory implements DmaapTopicSourceFactory {
41 private static final String MISSING_TOPIC = "A topic must be provided";
46 private static Logger logger = LoggerFactory.getLogger(IndexedDmaapTopicSourceFactory.class);
49 * DMaaP Topic Name Index.
51 protected HashMap<String, DmaapTopicSource> dmaapTopicSources = new HashMap<>();
54 public DmaapTopicSource build(BusTopicParams busTopicParams) {
56 if (busTopicParams.getTopic() == null || busTopicParams.getTopic().isEmpty()) {
57 throw new IllegalArgumentException(MISSING_TOPIC);
61 if (dmaapTopicSources.containsKey(busTopicParams.getTopic())) {
62 return dmaapTopicSources.get(busTopicParams.getTopic());
65 DmaapTopicSource dmaapTopicSource = makeSource(busTopicParams);
67 if (busTopicParams.isManaged()) {
68 dmaapTopicSources.put(busTopicParams.getTopic(), dmaapTopicSource);
70 return dmaapTopicSource;
75 public List<DmaapTopicSource> build(Properties properties) {
77 String readTopics = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS);
78 if (StringUtils.isBlank(readTopics)) {
79 logger.info("{}: no topic for DMaaP Source", this);
80 return new ArrayList<>();
83 List<DmaapTopicSource> dmaapTopicSourceLst = new ArrayList<>();
85 for (String topic : readTopics.split("\\s*,\\s*")) {
86 addTopic(dmaapTopicSourceLst, properties, topic);
89 return dmaapTopicSourceLst;
93 public DmaapTopicSource build(List<String> servers, String topic, String apiKey, String apiSecret) {
94 return this.build(BusTopicParams.builder()
99 .fetchTimeout(DmaapTopicSource.DEFAULT_TIMEOUT_MS_FETCH)
100 .fetchLimit(DmaapTopicSource.DEFAULT_LIMIT_FETCH)
103 .allowSelfSignedCerts(false)
108 public DmaapTopicSource build(List<String> servers, String topic) {
109 return this.build(servers, topic, null, null);
112 private void addTopic(List<DmaapTopicSource> dmaapTopicSourceLst, Properties properties, String topic) {
113 if (this.dmaapTopicSources.containsKey(topic)) {
114 dmaapTopicSourceLst.add(this.dmaapTopicSources.get(topic));
118 String topicPrefix = PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "." + topic;
120 PropertyUtils props = new PropertyUtils(properties, topicPrefix,
121 (name, value, ex) -> logger.warn("{}: {} {} is in invalid format for topic {} ", this, name, value, topic));
123 String servers = properties.getProperty(topicPrefix + PolicyEndPointProperties.PROPERTY_TOPIC_SERVERS_SUFFIX);
124 if (StringUtils.isBlank(servers)) {
125 logger.error("{}: no DMaaP servers or DME2 ServiceName provided", this);
129 DmaapTopicSource uebTopicSource = this.build(DmaapPropertyUtils.makeBuilder(props, topic, servers)
130 .consumerGroup(props.getString(
131 PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_CONSUMER_GROUP_SUFFIX, null))
132 .consumerInstance(props.getString(
133 PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_CONSUMER_INSTANCE_SUFFIX, null))
134 .fetchTimeout(props.getInteger(
135 PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_FETCH_TIMEOUT_SUFFIX,
136 DmaapTopicSource.DEFAULT_TIMEOUT_MS_FETCH))
137 .fetchLimit(props.getInteger(PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_FETCH_LIMIT_SUFFIX,
138 DmaapTopicSource.DEFAULT_LIMIT_FETCH))
141 dmaapTopicSourceLst.add(uebTopicSource);
145 * Makes a new source.
147 * @param busTopicParams parameters to use to configure the source
148 * @return a new source
150 protected DmaapTopicSource makeSource(BusTopicParams busTopicParams) {
151 return new SingleThreadedDmaapTopicSource(busTopicParams);
155 public void destroy(String topic) {
157 if (topic == null || topic.isEmpty()) {
158 throw new IllegalArgumentException(MISSING_TOPIC);
161 DmaapTopicSource uebTopicSource;
163 synchronized (this) {
164 if (!dmaapTopicSources.containsKey(topic)) {
168 uebTopicSource = dmaapTopicSources.remove(topic);
171 uebTopicSource.shutdown();
175 public void destroy() {
176 List<DmaapTopicSource> readers = this.inventory();
177 for (DmaapTopicSource reader : readers) {
181 synchronized (this) {
182 this.dmaapTopicSources.clear();
187 public DmaapTopicSource get(String topic) {
189 if (topic == null || topic.isEmpty()) {
190 throw new IllegalArgumentException(MISSING_TOPIC);
193 synchronized (this) {
194 if (dmaapTopicSources.containsKey(topic)) {
195 return dmaapTopicSources.get(topic);
197 throw new IllegalStateException("DmaapTopiceSource for " + topic + " not found");
203 public synchronized List<DmaapTopicSource> inventory() {
204 return new ArrayList<>(this.dmaapTopicSources.values());
208 public String toString() {
209 return "IndexedDmaapTopicSourceFactory []";