2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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=========================================================
20 package org.onap.policy.drools.event.comm.bus;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Properties;
28 import org.onap.policy.drools.properties.PolicyProperties;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * Noop Topic Sink Factory
35 public interface NoopTopicSinkFactory {
38 * Creates noop topic sinks based on properties files
40 * @param properties Properties containing initialization values
42 * @return a noop topic sink
43 * @throws IllegalArgumentException if invalid parameters are present
45 public List<NoopTopicSink> build(Properties properties);
50 * @param servers list of servers
51 * @param topic topic name
52 * @param managed is this sink endpoint managed?
53 * @return a noop topic sink
54 * @throws IllegalArgumentException if invalid parameters are present
56 public NoopTopicSink build(List<String> servers, String topic, boolean managed);
59 * Destroys a sink based on the topic
61 * @param topic topic name
62 * @throws IllegalArgumentException if invalid parameters are present
64 public void destroy(String topic);
67 * gets a sink based on topic name
69 * @param topic the topic name
71 * @return a sink with topic name
72 * @throws IllegalArgumentException if an invalid topic is provided
73 * @throws IllegalStateException if the sink is in an incorrect state
75 public NoopTopicSink get(String topic);
78 * Provides a snapshot of the UEB Topic Writers
80 * @return a list of the UEB Topic Writers
82 public List<NoopTopicSink> inventory();
87 public void destroy();
91 /* ------------- implementation ----------------- */
94 * Factory of noop sinks
96 class IndexedNoopTopicSinkFactory implements NoopTopicSinkFactory {
100 private static Logger logger = LoggerFactory.getLogger(IndexedUebTopicSinkFactory.class);
103 * noop topic sinks map
105 protected HashMap<String, NoopTopicSink> noopTopicSinks = new HashMap<>();
108 public List<NoopTopicSink> build(Properties properties) {
110 final String sinkTopics = properties.getProperty(PolicyProperties.PROPERTY_NOOP_SINK_TOPICS);
111 if (sinkTopics == null || sinkTopics.isEmpty()) {
112 logger.info("{}: no topic for noop sink", this);
113 return new ArrayList<>();
116 final List<String> sinkTopicList =
117 new ArrayList<>(Arrays.asList(sinkTopics.split("\\s*,\\s*")));
118 final List<NoopTopicSink> newSinks = new ArrayList<NoopTopicSink>();
119 synchronized (this) {
120 for (final String topic : sinkTopicList) {
121 if (this.noopTopicSinks.containsKey(topic)) {
122 newSinks.add(this.noopTopicSinks.get(topic));
126 String servers = properties.getProperty(PolicyProperties.PROPERTY_NOOP_SINK_TOPICS + "."
127 + topic + PolicyProperties.PROPERTY_TOPIC_SERVERS_SUFFIX);
129 if (servers == null || servers.isEmpty())
132 final List<String> serverList = new ArrayList<>(Arrays.asList(servers.split("\\s*,\\s*")));
134 final String managedString =
135 properties.getProperty(PolicyProperties.PROPERTY_NOOP_SINK_TOPICS + "." + topic
136 + PolicyProperties.PROPERTY_MANAGED_SUFFIX);
137 boolean managed = true;
138 if (managedString != null && !managedString.isEmpty()) {
139 managed = Boolean.parseBoolean(managedString);
142 final NoopTopicSink noopSink = this.build(serverList, topic, managed);
143 newSinks.add(noopSink);
150 public NoopTopicSink build(List<String> servers, String topic, boolean managed) {
151 if (servers == null) {
152 servers = new ArrayList<>();
155 if (servers.isEmpty()) {
159 if (topic == null || topic.isEmpty()) {
160 throw new IllegalArgumentException("A topic must be provided");
163 synchronized (this) {
164 if (this.noopTopicSinks.containsKey(topic)) {
165 return this.noopTopicSinks.get(topic);
168 final NoopTopicSink sink = new NoopTopicSink(servers, topic);
171 this.noopTopicSinks.put(topic, sink);
178 public void destroy(String topic) {
179 if (topic == null || topic.isEmpty()) {
180 throw new IllegalArgumentException("A topic must be provided");
183 NoopTopicSink noopSink;
184 synchronized (this) {
185 if (!this.noopTopicSinks.containsKey(topic)) {
189 noopSink = this.noopTopicSinks.remove(topic);
196 public void destroy() {
197 final List<NoopTopicSink> sinks = this.inventory();
198 for (final NoopTopicSink sink : sinks) {
202 synchronized (this) {
203 this.noopTopicSinks.clear();
208 public NoopTopicSink get(String topic) {
209 if (topic == null || topic.isEmpty()) {
210 throw new IllegalArgumentException("A topic must be provided");
213 synchronized (this) {
214 if (this.noopTopicSinks.containsKey(topic)) {
215 return this.noopTopicSinks.get(topic);
217 throw new IllegalStateException("DmaapTopicSink for " + topic + " not found");
223 public List<NoopTopicSink> inventory() {
224 return new ArrayList<>(this.noopTopicSinks.values());