2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Modified Copyright (C) 2018 Samsung Electronics Co., Ltd.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.common.endpoints.event.comm.bus;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Properties;
30 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
31 import org.onap.policy.common.endpoints.event.comm.bus.internal.InlineUebTopicSink;
32 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * UEB Topic Sink Factory.
39 public interface UebTopicSinkFactory {
42 * Instantiates a new UEB Topic Writer.
44 * @param busTopicParams parameters object
45 * @return an UEB Topic Sink
47 UebTopicSink build(BusTopicParams busTopicParams);
50 * Creates an UEB Topic Writer based on properties files.
52 * @param properties Properties containing initialization values
54 * @return an UEB Topic Writer
55 * @throws IllegalArgumentException if invalid parameters are present
57 List<UebTopicSink> build(Properties properties);
60 * Instantiates a new UEB Topic Writer.
62 * @param servers list of servers
63 * @param topic topic name
65 * @return an UEB Topic Writer
66 * @throws IllegalArgumentException if invalid parameters are present
68 UebTopicSink build(List<String> servers, String topic);
71 * Destroys an UEB Topic Writer based on a topic.
73 * @param topic topic name
74 * @throws IllegalArgumentException if invalid parameters are present
76 void destroy(String topic);
79 * Destroys all UEB Topic Writers.
84 * gets an UEB Topic Writer based on topic name.
86 * @param topic the topic name
88 * @return an UEB Topic Writer with topic name
89 * @throws IllegalArgumentException if an invalid topic is provided
90 * @throws IllegalStateException if the UEB Topic Reader is an incorrect state
92 UebTopicSink get(String topic);
95 * Provides a snapshot of the UEB Topic Writers.
97 * @return a list of the UEB Topic Writers
99 List<UebTopicSink> inventory();
103 /* ------------- implementation ----------------- */
106 * Factory of UEB Reader Topics indexed by topic name.
108 class IndexedUebTopicSinkFactory implements UebTopicSinkFactory {
109 private static final String MISSING_TOPIC = "A topic must be provided";
114 private static Logger logger = LoggerFactory.getLogger(IndexedUebTopicSinkFactory.class);
117 * UEB Topic Name Index.
119 protected HashMap<String, UebTopicSink> uebTopicSinks = new HashMap<>();
122 public UebTopicSink build(BusTopicParams busTopicParams) {
124 if (busTopicParams.getServers() == null || busTopicParams.getServers().isEmpty()) {
125 throw new IllegalArgumentException("UEB Server(s) must be provided");
128 if (busTopicParams.getTopic() == null || busTopicParams.getTopic().isEmpty()) {
129 throw new IllegalArgumentException(MISSING_TOPIC);
132 synchronized (this) {
133 if (uebTopicSinks.containsKey(busTopicParams.getTopic())) {
134 return uebTopicSinks.get(busTopicParams.getTopic());
137 UebTopicSink uebTopicWriter = makeSink(busTopicParams);
139 if (busTopicParams.isManaged()) {
140 uebTopicSinks.put(busTopicParams.getTopic(), uebTopicWriter);
143 return uebTopicWriter;
149 public UebTopicSink build(List<String> servers, String topic) {
150 return this.build(BusTopicParams.builder()
155 .allowSelfSignedCerts(false)
161 public List<UebTopicSink> build(Properties properties) {
163 String writeTopics = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS);
164 if (writeTopics == null || writeTopics.isEmpty()) {
165 logger.info("{}: no topic for UEB Sink", this);
166 return new ArrayList<>();
169 List<String> writeTopicList = new ArrayList<>(Arrays.asList(writeTopics.split("\\s*,\\s*")));
170 List<UebTopicSink> newUebTopicSinks = new ArrayList<>();
171 synchronized (this) {
172 for (String topic : writeTopicList) {
173 if (this.uebTopicSinks.containsKey(topic)) {
174 newUebTopicSinks.add(this.uebTopicSinks.get(topic));
178 String servers = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS + "." + topic
179 + PolicyEndPointProperties.PROPERTY_TOPIC_SERVERS_SUFFIX);
180 if (servers == null || servers.isEmpty()) {
181 logger.error("{}: no UEB servers configured for sink {}", this, topic);
185 final List<String> serverList = new ArrayList<>(Arrays.asList(servers.split("\\s*,\\s*")));
187 final String apiKey = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS
188 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_API_KEY_SUFFIX);
189 final String apiSecret = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS
190 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_API_SECRET_SUFFIX);
191 final String partitionKey = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS
192 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SINK_PARTITION_KEY_SUFFIX);
194 String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS + "."
195 + topic + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX);
196 boolean managed = true;
197 if (managedString != null && !managedString.isEmpty()) {
198 managed = Boolean.parseBoolean(managedString);
201 String useHttpsString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS + "."
202 + topic + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX);
204 // default is to use HTTP if no https property exists
205 boolean useHttps = false;
206 if (useHttpsString != null && !useHttpsString.isEmpty()) {
207 useHttps = Boolean.parseBoolean(useHttpsString);
211 String allowSelfSignedCertsString =
212 properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS + "." + topic
213 + PolicyEndPointProperties.PROPERTY_ALLOW_SELF_SIGNED_CERTIFICATES_SUFFIX);
215 // default is to disallow self-signed certs
216 boolean allowSelfSignedCerts = false;
217 if (allowSelfSignedCertsString != null && !allowSelfSignedCertsString.isEmpty()) {
218 allowSelfSignedCerts = Boolean.parseBoolean(allowSelfSignedCertsString);
221 UebTopicSink uebTopicWriter = this.build(BusTopicParams.builder()
225 .apiSecret(apiSecret)
226 .partitionId(partitionKey)
229 .allowSelfSignedCerts(allowSelfSignedCerts)
231 newUebTopicSinks.add(uebTopicWriter);
233 return newUebTopicSinks;
238 public void destroy(String topic) {
240 if (topic == null || topic.isEmpty()) {
241 throw new IllegalArgumentException(MISSING_TOPIC);
244 UebTopicSink uebTopicWriter;
245 synchronized (this) {
246 if (!uebTopicSinks.containsKey(topic)) {
250 uebTopicWriter = uebTopicSinks.remove(topic);
253 uebTopicWriter.shutdown();
257 public void destroy() {
258 List<UebTopicSink> writers = this.inventory();
259 for (UebTopicSink writer : writers) {
263 synchronized (this) {
264 this.uebTopicSinks.clear();
269 public UebTopicSink get(String topic) {
271 if (topic == null || topic.isEmpty()) {
272 throw new IllegalArgumentException(MISSING_TOPIC);
275 synchronized (this) {
276 if (uebTopicSinks.containsKey(topic)) {
277 return uebTopicSinks.get(topic);
279 throw new IllegalStateException("UebTopicSink for " + topic + " not found");
285 public synchronized List<UebTopicSink> inventory() {
286 return new ArrayList<>(this.uebTopicSinks.values());
292 * @param busTopicParams parameters to use to configure the sink
295 protected UebTopicSink makeSink(BusTopicParams busTopicParams) {
296 return new InlineUebTopicSink(busTopicParams);
301 public String toString() {
302 StringBuilder builder = new StringBuilder();
303 builder.append("IndexedUebTopicSinkFactory []");
304 return builder.toString();