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 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 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 void destroy(String topic);
73 * gets a sink based on topic name.
75 * @param topic the topic name
77 * @return a sink with topic name
78 * @throws IllegalArgumentException if an invalid topic is provided
79 * @throws IllegalStateException if the sink is in an incorrect state
81 NoopTopicSink get(String topic);
84 * Provides a snapshot of the UEB Topic Writers.
86 * @return a list of the UEB Topic Writers
88 List<NoopTopicSink> inventory();
93 /* ------------- implementation ----------------- */
96 * Factory of noop sinks.
98 class IndexedNoopTopicSinkFactory implements NoopTopicSinkFactory {
99 private static final String MISSING_TOPIC = "A topic must be provided";
104 private static Logger logger = LoggerFactory.getLogger(IndexedNoopTopicSinkFactory.class);
107 * noop topic sinks map.
109 protected HashMap<String, NoopTopicSink> noopTopicSinks = new HashMap<>();
112 public List<NoopTopicSink> build(Properties properties) {
114 final String sinkTopics = properties.getProperty(PolicyEndPointProperties.PROPERTY_NOOP_SINK_TOPICS);
115 if (sinkTopics == null || sinkTopics.isEmpty()) {
116 logger.info("{}: no topic for noop sink", this);
117 return new ArrayList<>();
120 final List<String> sinkTopicList = new ArrayList<>(Arrays.asList(sinkTopics.split("\\s*,\\s*")));
121 final List<NoopTopicSink> newSinks = new ArrayList<>();
122 synchronized (this) {
123 for (final String topic : sinkTopicList) {
124 if (this.noopTopicSinks.containsKey(topic)) {
125 newSinks.add(this.noopTopicSinks.get(topic));
129 String servers = properties.getProperty(PolicyEndPointProperties.PROPERTY_NOOP_SINK_TOPICS + "." + topic
130 + PolicyEndPointProperties.PROPERTY_TOPIC_SERVERS_SUFFIX);
132 if (servers == null || servers.isEmpty()) {
136 final List<String> serverList = new ArrayList<>(Arrays.asList(servers.split("\\s*,\\s*")));
138 final String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_NOOP_SINK_TOPICS
139 + "." + topic + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX);
140 boolean managed = true;
141 if (managedString != null && !managedString.isEmpty()) {
142 managed = Boolean.parseBoolean(managedString);
145 final NoopTopicSink noopSink = this.build(serverList, topic, managed);
146 newSinks.add(noopSink);
153 public NoopTopicSink build(List<String> servers, String topic, boolean managed) {
155 List<String> noopSinkServers = servers;
156 if (noopSinkServers == null || noopSinkServers.isEmpty()) {
157 noopSinkServers = Arrays.asList("noop");
160 if (topic == null || topic.isEmpty()) {
161 throw new IllegalArgumentException(MISSING_TOPIC);
164 synchronized (this) {
165 if (this.noopTopicSinks.containsKey(topic)) {
166 return this.noopTopicSinks.get(topic);
169 final NoopTopicSink sink = new NoopTopicSink(noopSinkServers, topic);
172 this.noopTopicSinks.put(topic, sink);
180 public void destroy(String topic) {
181 if (topic == null || topic.isEmpty()) {
182 throw new IllegalArgumentException(MISSING_TOPIC);
185 NoopTopicSink noopSink;
186 synchronized (this) {
187 if (!this.noopTopicSinks.containsKey(topic)) {
191 noopSink = this.noopTopicSinks.remove(topic);
198 public void destroy() {
199 final List<NoopTopicSink> sinks = this.inventory();
200 for (final NoopTopicSink sink : sinks) {
204 synchronized (this) {
205 this.noopTopicSinks.clear();
210 public NoopTopicSink get(String topic) {
211 if (topic == null || topic.isEmpty()) {
212 throw new IllegalArgumentException(MISSING_TOPIC);
215 synchronized (this) {
216 if (this.noopTopicSinks.containsKey(topic)) {
217 return this.noopTopicSinks.get(topic);
219 throw new IllegalStateException("DmaapTopicSink for " + topic + " not found");
225 public List<NoopTopicSink> inventory() {
226 return new ArrayList<>(this.noopTopicSinks.values());