2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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.properties.PolicyEndPointProperties;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * Noop Topic Sink Factory
36 public interface NoopTopicSinkFactory {
39 * Creates noop topic sinks based on properties files
41 * @param properties Properties containing initialization values
43 * @return a noop topic sink
44 * @throws IllegalArgumentException if invalid parameters are present
46 public List<NoopTopicSink> build(Properties properties);
51 * @param servers list of servers
52 * @param topic topic name
53 * @param managed is this sink endpoint managed?
54 * @return a noop topic sink
55 * @throws IllegalArgumentException if invalid parameters are present
57 public NoopTopicSink build(List<String> servers, String topic, boolean managed);
60 * Destroys a sink based on the topic
62 * @param topic topic name
63 * @throws IllegalArgumentException if invalid parameters are present
65 public void destroy(String topic);
68 * gets a sink based on topic name
70 * @param topic the topic name
72 * @return a sink with topic name
73 * @throws IllegalArgumentException if an invalid topic is provided
74 * @throws IllegalStateException if the sink is in an incorrect state
76 public NoopTopicSink get(String topic);
79 * Provides a snapshot of the UEB Topic Writers
81 * @return a list of the UEB Topic Writers
83 public List<NoopTopicSink> inventory();
88 public void destroy();
92 /* ------------- implementation ----------------- */
95 * Factory of noop sinks
97 class IndexedNoopTopicSinkFactory implements NoopTopicSinkFactory {
98 private static final String MISSING_TOPIC = "A topic must be provided";
103 private static Logger logger = LoggerFactory.getLogger(IndexedUebTopicSinkFactory.class);
106 * noop topic sinks map
108 protected HashMap<String, NoopTopicSink> noopTopicSinks = new HashMap<>();
111 public List<NoopTopicSink> build(Properties properties) {
113 final String sinkTopics = properties.getProperty(PolicyEndPointProperties.PROPERTY_NOOP_SINK_TOPICS);
114 if (sinkTopics == null || sinkTopics.isEmpty()) {
115 logger.info("{}: no topic for noop sink", this);
116 return new ArrayList<>();
119 final List<String> sinkTopicList = new ArrayList<>(Arrays.asList(sinkTopics.split("\\s*,\\s*")));
120 final List<NoopTopicSink> newSinks = new ArrayList<>();
121 synchronized (this) {
122 for (final String topic : sinkTopicList) {
123 if (this.noopTopicSinks.containsKey(topic)) {
124 newSinks.add(this.noopTopicSinks.get(topic));
128 String servers = properties.getProperty(PolicyEndPointProperties.PROPERTY_NOOP_SINK_TOPICS + "." + topic
129 + PolicyEndPointProperties.PROPERTY_TOPIC_SERVERS_SUFFIX);
131 if (servers == null || servers.isEmpty()) {
135 final List<String> serverList = new ArrayList<>(Arrays.asList(servers.split("\\s*,\\s*")));
137 final String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_NOOP_SINK_TOPICS
138 + "." + topic + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX);
139 boolean managed = true;
140 if (managedString != null && !managedString.isEmpty()) {
141 managed = Boolean.parseBoolean(managedString);
144 final NoopTopicSink noopSink = this.build(serverList, topic, managed);
145 newSinks.add(noopSink);
152 public NoopTopicSink build(List<String> servers, String topic, boolean managed) {
154 List<String> noopSinkServers = servers;
155 if (noopSinkServers == null) {
156 noopSinkServers = new ArrayList<>();
159 if (noopSinkServers.isEmpty()) {
160 noopSinkServers.add("noop");
163 if (topic == null || topic.isEmpty()) {
164 throw new IllegalArgumentException(MISSING_TOPIC);
167 synchronized (this) {
168 if (this.noopTopicSinks.containsKey(topic)) {
169 return this.noopTopicSinks.get(topic);
172 final NoopTopicSink sink = new NoopTopicSink(noopSinkServers, topic);
175 this.noopTopicSinks.put(topic, sink);
183 public void destroy(String topic) {
184 if (topic == null || topic.isEmpty()) {
185 throw new IllegalArgumentException(MISSING_TOPIC);
188 NoopTopicSink noopSink;
189 synchronized (this) {
190 if (!this.noopTopicSinks.containsKey(topic)) {
194 noopSink = this.noopTopicSinks.remove(topic);
201 public void destroy() {
202 final List<NoopTopicSink> sinks = this.inventory();
203 for (final NoopTopicSink sink : sinks) {
207 synchronized (this) {
208 this.noopTopicSinks.clear();
213 public NoopTopicSink get(String topic) {
214 if (topic == null || topic.isEmpty()) {
215 throw new IllegalArgumentException(MISSING_TOPIC);
218 synchronized (this) {
219 if (this.noopTopicSinks.containsKey(topic)) {
220 return this.noopTopicSinks.get(topic);
222 throw new IllegalStateException("DmaapTopicSink for " + topic + " not found");
228 public List<NoopTopicSink> inventory() {
229 return new ArrayList<>(this.noopTopicSinks.values());