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.Arrays;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Properties;
29 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
30 import org.onap.policy.common.endpoints.event.comm.bus.internal.InlineUebTopicSink;
31 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * Factory of UEB Reader Topics indexed by topic name.
38 class IndexedUebTopicSinkFactory implements UebTopicSinkFactory {
39 private static final String MISSING_TOPIC = "A topic must be provided";
44 private static Logger logger = LoggerFactory.getLogger(IndexedUebTopicSinkFactory.class);
47 * UEB Topic Name Index.
49 protected HashMap<String, UebTopicSink> uebTopicSinks = new HashMap<>();
52 public UebTopicSink build(BusTopicParams busTopicParams) {
54 if (busTopicParams.getServers() == null || busTopicParams.getServers().isEmpty()) {
55 throw new IllegalArgumentException("UEB Server(s) must be provided");
58 if (busTopicParams.getTopic() == null || busTopicParams.getTopic().isEmpty()) {
59 throw new IllegalArgumentException(MISSING_TOPIC);
63 if (uebTopicSinks.containsKey(busTopicParams.getTopic())) {
64 return uebTopicSinks.get(busTopicParams.getTopic());
67 UebTopicSink uebTopicWriter = makeSink(busTopicParams);
69 if (busTopicParams.isManaged()) {
70 uebTopicSinks.put(busTopicParams.getTopic(), uebTopicWriter);
73 return uebTopicWriter;
79 public UebTopicSink build(List<String> servers, String topic) {
80 return this.build(BusTopicParams.builder()
85 .allowSelfSignedCerts(false)
91 public List<UebTopicSink> build(Properties properties) {
93 String writeTopics = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS);
94 if (writeTopics == null || writeTopics.isEmpty()) {
95 logger.info("{}: no topic for UEB Sink", this);
96 return new ArrayList<>();
99 List<String> writeTopicList = new ArrayList<>(Arrays.asList(writeTopics.split("\\s*,\\s*")));
100 List<UebTopicSink> newUebTopicSinks = new ArrayList<>();
101 synchronized (this) {
102 for (String topic : writeTopicList) {
103 if (this.uebTopicSinks.containsKey(topic)) {
104 newUebTopicSinks.add(this.uebTopicSinks.get(topic));
108 String servers = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS + "." + topic
109 + PolicyEndPointProperties.PROPERTY_TOPIC_SERVERS_SUFFIX);
110 if (servers == null || servers.isEmpty()) {
111 logger.error("{}: no UEB servers configured for sink {}", this, topic);
115 final List<String> serverList = new ArrayList<>(Arrays.asList(servers.split("\\s*,\\s*")));
117 final String effectiveTopic = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS
118 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_EFFECTIVE_TOPIC_SUFFIX, topic);
119 final String apiKey = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS
120 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_API_KEY_SUFFIX);
121 final String apiSecret = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS
122 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_API_SECRET_SUFFIX);
123 final String partitionKey = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS
124 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SINK_PARTITION_KEY_SUFFIX);
126 String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS + "."
127 + topic + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX);
128 boolean managed = true;
129 if (managedString != null && !managedString.isEmpty()) {
130 managed = Boolean.parseBoolean(managedString);
133 String useHttpsString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS + "."
134 + topic + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX);
136 // default is to use HTTP if no https property exists
137 boolean useHttps = false;
138 if (useHttpsString != null && !useHttpsString.isEmpty()) {
139 useHttps = Boolean.parseBoolean(useHttpsString);
143 String allowSelfSignedCertsString =
144 properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS + "." + topic
145 + PolicyEndPointProperties.PROPERTY_ALLOW_SELF_SIGNED_CERTIFICATES_SUFFIX);
147 // default is to disallow self-signed certs
148 boolean allowSelfSignedCerts = false;
149 if (allowSelfSignedCertsString != null && !allowSelfSignedCertsString.isEmpty()) {
150 allowSelfSignedCerts = Boolean.parseBoolean(allowSelfSignedCertsString);
153 UebTopicSink uebTopicWriter = this.build(BusTopicParams.builder()
156 .effectiveTopic(effectiveTopic)
158 .apiSecret(apiSecret)
159 .partitionId(partitionKey)
162 .allowSelfSignedCerts(allowSelfSignedCerts)
164 newUebTopicSinks.add(uebTopicWriter);
166 return newUebTopicSinks;
171 public void destroy(String topic) {
173 if (topic == null || topic.isEmpty()) {
174 throw new IllegalArgumentException(MISSING_TOPIC);
177 UebTopicSink uebTopicWriter;
178 synchronized (this) {
179 if (!uebTopicSinks.containsKey(topic)) {
183 uebTopicWriter = uebTopicSinks.remove(topic);
186 uebTopicWriter.shutdown();
190 public void destroy() {
191 List<UebTopicSink> writers = this.inventory();
192 for (UebTopicSink writer : writers) {
196 synchronized (this) {
197 this.uebTopicSinks.clear();
202 public UebTopicSink get(String topic) {
204 if (topic == null || topic.isEmpty()) {
205 throw new IllegalArgumentException(MISSING_TOPIC);
208 synchronized (this) {
209 if (uebTopicSinks.containsKey(topic)) {
210 return uebTopicSinks.get(topic);
212 throw new IllegalStateException("UebTopicSink for " + topic + " not found");
218 public synchronized List<UebTopicSink> inventory() {
219 return new ArrayList<>(this.uebTopicSinks.values());
225 * @param busTopicParams parameters to use to configure the sink
228 protected UebTopicSink makeSink(BusTopicParams busTopicParams) {
229 return new InlineUebTopicSink(busTopicParams);
234 public String toString() {
235 StringBuilder builder = new StringBuilder();
236 builder.append("IndexedUebTopicSinkFactory []");
237 return builder.toString();