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.impl;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.HashMap;
26 import java.util.List;
28 import java.util.Properties;
30 import org.onap.policy.common.endpoints.event.comm.bus.DmaapTopicSink;
31 import org.onap.policy.common.endpoints.event.comm.bus.DmaapTopicSinkFactory;
32 import org.onap.policy.common.endpoints.event.comm.bus.internal.InlineDmaapTopicSink;
33 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * Factory of DMAAP Reader Topics indexed by topic name
40 public class IndexedDmaapTopicSinkFactory implements DmaapTopicSinkFactory {
41 private static final String MISSING_TOPIC = "A topic must be provided";
43 private static final IndexedDmaapTopicSinkFactory instance = new IndexedDmaapTopicSinkFactory();
47 private static Logger logger = LoggerFactory.getLogger(IndexedDmaapTopicSinkFactory.class);
50 * DMAAP Topic Name Index
52 protected HashMap<String, DmaapTopicSink> dmaapTopicWriters = new HashMap<>();
55 * Get the singleton instance.
57 * @return the instance
59 public static IndexedDmaapTopicSinkFactory getInstance() {
63 private IndexedDmaapTopicSinkFactory() {}
66 public DmaapTopicSink build(List<String> servers, String topic, String apiKey, String apiSecret, String userName,
67 String password, String partitionKey, String environment, String aftEnvironment, String partner,
68 String latitude, String longitude, Map<String, String> additionalProps, boolean managed, boolean useHttps,
69 boolean allowSelfSignedCerts) {
71 if (topic == null || topic.isEmpty()) {
72 throw new IllegalArgumentException(MISSING_TOPIC);
76 if (dmaapTopicWriters.containsKey(topic)) {
77 return dmaapTopicWriters.get(topic);
80 DmaapTopicSink dmaapTopicSink = new InlineDmaapTopicSink(servers, topic, apiKey, apiSecret, userName,
81 password, partitionKey, environment, aftEnvironment, partner, latitude, longitude, additionalProps,
82 useHttps, allowSelfSignedCerts);
85 dmaapTopicWriters.put(topic, dmaapTopicSink);
87 return dmaapTopicSink;
92 public DmaapTopicSink build(List<String> servers, String topic, String apiKey, String apiSecret, String userName,
93 String password, String partitionKey, boolean managed, boolean useHttps, boolean allowSelfSignedCerts) {
95 if (topic == null || topic.isEmpty()) {
96 throw new IllegalArgumentException(MISSING_TOPIC);
100 if (dmaapTopicWriters.containsKey(topic)) {
101 return dmaapTopicWriters.get(topic);
104 DmaapTopicSink dmaapTopicSink = new InlineDmaapTopicSink(servers, topic, apiKey, apiSecret, userName,
105 password, partitionKey, useHttps, allowSelfSignedCerts);
108 dmaapTopicWriters.put(topic, dmaapTopicSink);
110 return dmaapTopicSink;
115 public DmaapTopicSink build(List<String> servers, String topic) {
116 return this.build(servers, topic, null, null, null, null, null, true, false, false);
120 public List<DmaapTopicSink> build(Properties properties) {
122 String writeTopics = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS);
123 if (writeTopics == null || writeTopics.isEmpty()) {
124 logger.info("{}: no topic for DMaaP Sink", this);
125 return new ArrayList<>();
128 List<String> writeTopicList = new ArrayList<>(Arrays.asList(writeTopics.split("\\s*,\\s*")));
129 List<DmaapTopicSink> newDmaapTopicSinks = new ArrayList<>();
130 synchronized (this) {
131 for (String topic : writeTopicList) {
132 if (this.dmaapTopicWriters.containsKey(topic)) {
133 newDmaapTopicSinks.add(this.dmaapTopicWriters.get(topic));
136 String servers = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "."
137 + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SERVERS_SUFFIX);
139 List<String> serverList;
140 if (servers != null && !servers.isEmpty()) {
141 serverList = new ArrayList<>(Arrays.asList(servers.split("\\s*,\\s*")));
143 serverList = new ArrayList<>();
146 String apiKey = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "." + topic
147 + PolicyEndPointProperties.PROPERTY_TOPIC_API_KEY_SUFFIX);
148 String apiSecret = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "."
149 + topic + PolicyEndPointProperties.PROPERTY_TOPIC_API_SECRET_SUFFIX);
151 String aafMechId = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "."
152 + topic + PolicyEndPointProperties.PROPERTY_TOPIC_AAF_MECHID_SUFFIX);
153 String aafPassword = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "."
154 + topic + PolicyEndPointProperties.PROPERTY_TOPIC_AAF_PASSWORD_SUFFIX);
156 String partitionKey = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "."
157 + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SINK_PARTITION_KEY_SUFFIX);
159 String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS + "."
160 + topic + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX);
162 /* DME2 Properties */
164 String dme2Environment = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS
165 + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_ENVIRONMENT_SUFFIX);
167 String dme2AftEnvironment = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS
168 + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_AFT_ENVIRONMENT_SUFFIX);
170 String dme2Partner = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "."
171 + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_PARTNER_SUFFIX);
173 String dme2RouteOffer = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "."
174 + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_ROUTE_OFFER_SUFFIX);
176 String dme2Latitude = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "."
177 + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_LATITUDE_SUFFIX);
179 String dme2Longitude = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "."
180 + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_LONGITUDE_SUFFIX);
182 String dme2EpReadTimeoutMs = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS
183 + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_EP_READ_TIMEOUT_MS_SUFFIX);
185 String dme2EpConnTimeout = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS
186 + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_EP_CONN_TIMEOUT_SUFFIX);
188 String dme2RoundtripTimeoutMs =
189 properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "." + topic
190 + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_ROUNDTRIP_TIMEOUT_MS_SUFFIX);
192 String dme2Version = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "."
193 + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_VERSION_SUFFIX);
195 String dme2SubContextPath = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS
196 + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_SUB_CONTEXT_PATH_SUFFIX);
198 String dme2SessionStickinessRequired =
199 properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "." + topic
200 + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_SESSION_STICKINESS_REQUIRED_SUFFIX);
202 Map<String, String> dme2AdditionalProps = new HashMap<>();
204 if (dme2EpReadTimeoutMs != null && !dme2EpReadTimeoutMs.isEmpty()) {
205 dme2AdditionalProps.put(DME2_READ_TIMEOUT_PROPERTY, dme2EpReadTimeoutMs);
207 if (dme2EpConnTimeout != null && !dme2EpConnTimeout.isEmpty()) {
208 dme2AdditionalProps.put(DME2_EP_CONN_TIMEOUT_PROPERTY, dme2EpConnTimeout);
210 if (dme2RoundtripTimeoutMs != null && !dme2RoundtripTimeoutMs.isEmpty()) {
211 dme2AdditionalProps.put(DME2_ROUNDTRIP_TIMEOUT_PROPERTY, dme2RoundtripTimeoutMs);
213 if (dme2Version != null && !dme2Version.isEmpty()) {
214 dme2AdditionalProps.put(DME2_VERSION_PROPERTY, dme2Version);
216 if (dme2RouteOffer != null && !dme2RouteOffer.isEmpty()) {
217 dme2AdditionalProps.put(DME2_ROUTE_OFFER_PROPERTY, dme2RouteOffer);
219 if (dme2SubContextPath != null && !dme2SubContextPath.isEmpty()) {
220 dme2AdditionalProps.put(DME2_SUBCONTEXT_PATH_PROPERTY, dme2SubContextPath);
222 if (dme2SessionStickinessRequired != null && !dme2SessionStickinessRequired.isEmpty()) {
223 dme2AdditionalProps.put(DME2_SESSION_STICKINESS_REQUIRED_PROPERTY, dme2SessionStickinessRequired);
226 if (servers == null || servers.isEmpty()) {
227 logger.error("{}: no DMaaP servers or DME2 ServiceName provided", this);
231 boolean managed = true;
232 if (managedString != null && !managedString.isEmpty()) {
233 managed = Boolean.parseBoolean(managedString);
236 String useHttpsString = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "."
237 + topic + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX);
239 // default is to use HTTP if no https property exists
240 boolean useHttps = false;
241 if (useHttpsString != null && !useHttpsString.isEmpty()) {
242 useHttps = Boolean.parseBoolean(useHttpsString);
246 String allowSelfSignedCertsString =
247 properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "." + topic
248 + PolicyEndPointProperties.PROPERTY_ALLOW_SELF_SIGNED_CERTIFICATES_SUFFIX);
250 // default is to disallow self-signed certs
251 boolean allowSelfSignedCerts = false;
252 if (allowSelfSignedCertsString != null && !allowSelfSignedCertsString.isEmpty()) {
253 allowSelfSignedCerts = Boolean.parseBoolean(allowSelfSignedCertsString);
256 DmaapTopicSink dmaapTopicSink = this.build(serverList, topic, apiKey, apiSecret, aafMechId, aafPassword,
257 partitionKey, dme2Environment, dme2AftEnvironment, dme2Partner, dme2Latitude, dme2Longitude,
258 dme2AdditionalProps, managed, useHttps, allowSelfSignedCerts);
260 newDmaapTopicSinks.add(dmaapTopicSink);
262 return newDmaapTopicSinks;
267 public void destroy(String topic) {
269 if (topic == null || topic.isEmpty()) {
270 throw new IllegalArgumentException(MISSING_TOPIC);
273 DmaapTopicSink dmaapTopicWriter;
274 synchronized (this) {
275 if (!dmaapTopicWriters.containsKey(topic)) {
279 dmaapTopicWriter = dmaapTopicWriters.remove(topic);
282 dmaapTopicWriter.shutdown();
286 public void destroy() {
287 List<DmaapTopicSink> writers = this.inventory();
288 for (DmaapTopicSink writer : writers) {
292 synchronized (this) {
293 this.dmaapTopicWriters.clear();
298 public DmaapTopicSink get(String topic) {
300 if (topic == null || topic.isEmpty()) {
301 throw new IllegalArgumentException(MISSING_TOPIC);
304 synchronized (this) {
305 if (dmaapTopicWriters.containsKey(topic)) {
306 return dmaapTopicWriters.get(topic);
308 throw new IllegalStateException("DmaapTopicSink for " + topic + " not found");
314 public synchronized List<DmaapTopicSink> inventory() {
315 return new ArrayList<>(this.dmaapTopicWriters.values());
319 public String toString() {
320 StringBuilder builder = new StringBuilder();
321 builder.append("IndexedDmaapTopicSinkFactory []");
322 return builder.toString();