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;
29 import java.util.Properties;
31 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
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 * DMAAP Topic Sink Factory.
40 public interface DmaapTopicSinkFactory {
41 String DME2_READ_TIMEOUT_PROPERTY = "AFT_DME2_EP_READ_TIMEOUT_MS";
42 String DME2_EP_CONN_TIMEOUT_PROPERTY = "AFT_DME2_EP_CONN_TIMEOUT";
43 String DME2_ROUNDTRIP_TIMEOUT_PROPERTY = "AFT_DME2_ROUNDTRIP_TIMEOUT_MS";
44 String DME2_VERSION_PROPERTY = "Version";
45 String DME2_ROUTE_OFFER_PROPERTY = "routeOffer";
46 String DME2_SERVICE_NAME_PROPERTY = "ServiceName";
47 String DME2_SUBCONTEXT_PATH_PROPERTY = "SubContextPath";
48 String DME2_SESSION_STICKINESS_REQUIRED_PROPERTY = "sessionstickinessrequired";
51 * Instantiate a new DMAAP Topic Sink, with following params.
52 * servers list of servers
55 * apiSecret API Secret
56 * userName AAF user name
57 * password AAF password
58 * partitionKey Consumer Group
59 * environment DME2 environment
60 * aftEnvironment DME2 AFT environment
61 * partner DME2 Partner
62 * latitude DME2 latitude
63 * longitude DME2 longitude
64 * additionalProps additional properties to pass to DME2
65 * managed is this sink endpoint managed?
66 * @param busTopicParams parameter object
67 * @return DmaapTopicSink object
68 * @throws IllegalArgumentException if invalid parameters are present
70 DmaapTopicSink build(BusTopicParams busTopicParams);
73 * Creates an DMAAP Topic Sink based on properties files.
75 * @param properties Properties containing initialization values
76 * @return an DMAAP Topic Sink
77 * @throws IllegalArgumentException if invalid parameters are present
79 List<DmaapTopicSink> build(Properties properties);
82 * Instantiates a new DMAAP Topic Sink.
84 * @param servers list of servers
85 * @param topic topic name
86 * @return an DMAAP Topic Sink
87 * @throws IllegalArgumentException if invalid parameters are present
89 DmaapTopicSink build(List<String> servers, String topic);
92 * Destroys an DMAAP Topic Sink based on a topic.
94 * @param topic topic name
95 * @throws IllegalArgumentException if invalid parameters are present
97 void destroy(String topic);
100 * Destroys all DMAAP Topic Sinks.
105 * Gets an DMAAP Topic Sink based on topic name.
107 * @param topic the topic name
108 * @return an DMAAP Topic Sink with topic name
109 * @throws IllegalArgumentException if an invalid topic is provided
110 * @throws IllegalStateException if the DMAAP Topic Reader is an incorrect state
112 DmaapTopicSink get(String topic);
115 * Provides a snapshot of the DMAAP Topic Sinks.
117 * @return a list of the DMAAP Topic Sinks
119 List<DmaapTopicSink> inventory();
123 /* ------------- implementation ----------------- */
126 * Factory of DMAAP Reader Topics indexed by topic name.
128 class IndexedDmaapTopicSinkFactory implements DmaapTopicSinkFactory {
129 private static final String MISSING_TOPIC = "A topic must be provided";
134 private static Logger logger = LoggerFactory.getLogger(IndexedDmaapTopicSinkFactory.class);
137 * DMAAP Topic Name Index.
139 protected HashMap<String, DmaapTopicSink> dmaapTopicWriters = new HashMap<>();
142 public DmaapTopicSink build(BusTopicParams busTopicParams) {
144 if (busTopicParams.getTopic() == null || busTopicParams.getTopic().isEmpty()) {
145 throw new IllegalArgumentException(MISSING_TOPIC);
148 synchronized (this) {
149 if (dmaapTopicWriters.containsKey(busTopicParams.getTopic())) {
150 return dmaapTopicWriters.get(busTopicParams.getTopic());
153 DmaapTopicSink dmaapTopicSink = makeSink(busTopicParams);
155 if (busTopicParams.isManaged()) {
156 dmaapTopicWriters.put(busTopicParams.getTopic(), dmaapTopicSink);
158 return dmaapTopicSink;
163 public DmaapTopicSink build(List<String> servers, String topic) {
164 return this.build(BusTopicParams.builder()
169 .allowSelfSignedCerts(false)
174 public List<DmaapTopicSink> build(Properties properties) {
176 String writeTopics = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS);
177 if (writeTopics == null || writeTopics.isEmpty()) {
178 logger.info("{}: no topic for DMaaP Sink", this);
179 return new ArrayList<>();
182 List<String> writeTopicList = new ArrayList<>(Arrays.asList(writeTopics.split("\\s*,\\s*")));
183 List<DmaapTopicSink> newDmaapTopicSinks = new ArrayList<>();
184 synchronized (this) {
185 for (String topic : writeTopicList) {
186 if (this.dmaapTopicWriters.containsKey(topic)) {
187 newDmaapTopicSinks.add(this.dmaapTopicWriters.get(topic));
190 String servers = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "."
191 + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SERVERS_SUFFIX);
193 List<String> serverList;
194 if (servers != null && !servers.isEmpty()) {
195 serverList = new ArrayList<>(Arrays.asList(servers.split("\\s*,\\s*")));
197 serverList = new ArrayList<>();
200 final String apiKey = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS
201 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_API_KEY_SUFFIX);
202 final String apiSecret = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS
203 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_API_SECRET_SUFFIX);
205 final String aafMechId = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS
206 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_AAF_MECHID_SUFFIX);
207 final String aafPassword = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS
208 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_AAF_PASSWORD_SUFFIX);
210 final String partitionKey = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS
211 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SINK_PARTITION_KEY_SUFFIX);
213 final String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS
214 + "." + topic + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX);
216 /* DME2 Properties */
218 final String dme2Environment = properties.getProperty(
219 PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS
220 + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_ENVIRONMENT_SUFFIX);
222 final String dme2AftEnvironment = properties.getProperty(
223 PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS
224 + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_AFT_ENVIRONMENT_SUFFIX);
226 final String dme2Partner = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS
227 + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_PARTNER_SUFFIX);
229 final String dme2RouteOffer = properties.getProperty(
230 PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS
231 + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_ROUTE_OFFER_SUFFIX);
233 final String dme2Latitude = properties.getProperty(
234 PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "."
235 + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_LATITUDE_SUFFIX);
237 final String dme2Longitude = properties.getProperty(
238 PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS
239 + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_LONGITUDE_SUFFIX);
241 final String dme2EpReadTimeoutMs = properties.getProperty(
242 PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS
243 + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_EP_READ_TIMEOUT_MS_SUFFIX);
245 final String dme2EpConnTimeout = properties.getProperty(
246 PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS
247 + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_EP_CONN_TIMEOUT_SUFFIX);
249 final String dme2RoundtripTimeoutMs =
250 properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS
252 + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_ROUNDTRIP_TIMEOUT_MS_SUFFIX);
254 final String dme2Version = properties.getProperty(
255 PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "."
256 + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_VERSION_SUFFIX);
258 final String dme2SubContextPath = properties.getProperty(
259 PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS
260 + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_SUB_CONTEXT_PATH_SUFFIX);
262 final String dme2SessionStickinessRequired =
263 properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS
265 + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_SESSION_STICKINESS_REQUIRED_SUFFIX);
267 Map<String, String> dme2AdditionalProps = new HashMap<>();
269 if (dme2EpReadTimeoutMs != null && !dme2EpReadTimeoutMs.isEmpty()) {
270 dme2AdditionalProps.put(DME2_READ_TIMEOUT_PROPERTY, dme2EpReadTimeoutMs);
272 if (dme2EpConnTimeout != null && !dme2EpConnTimeout.isEmpty()) {
273 dme2AdditionalProps.put(DME2_EP_CONN_TIMEOUT_PROPERTY, dme2EpConnTimeout);
275 if (dme2RoundtripTimeoutMs != null && !dme2RoundtripTimeoutMs.isEmpty()) {
276 dme2AdditionalProps.put(DME2_ROUNDTRIP_TIMEOUT_PROPERTY, dme2RoundtripTimeoutMs);
278 if (dme2Version != null && !dme2Version.isEmpty()) {
279 dme2AdditionalProps.put(DME2_VERSION_PROPERTY, dme2Version);
281 if (dme2RouteOffer != null && !dme2RouteOffer.isEmpty()) {
282 dme2AdditionalProps.put(DME2_ROUTE_OFFER_PROPERTY, dme2RouteOffer);
284 if (dme2SubContextPath != null && !dme2SubContextPath.isEmpty()) {
285 dme2AdditionalProps.put(DME2_SUBCONTEXT_PATH_PROPERTY, dme2SubContextPath);
287 if (dme2SessionStickinessRequired != null && !dme2SessionStickinessRequired.isEmpty()) {
288 dme2AdditionalProps.put(DME2_SESSION_STICKINESS_REQUIRED_PROPERTY, dme2SessionStickinessRequired);
291 if (servers == null || servers.isEmpty()) {
292 logger.error("{}: no DMaaP servers or DME2 ServiceName provided", this);
296 boolean managed = true;
297 if (managedString != null && !managedString.isEmpty()) {
298 managed = Boolean.parseBoolean(managedString);
301 String useHttpsString = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "."
302 + topic + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX);
304 // default is to use HTTP if no https property exists
305 boolean useHttps = false;
306 if (useHttpsString != null && !useHttpsString.isEmpty()) {
307 useHttps = Boolean.parseBoolean(useHttpsString);
311 String allowSelfSignedCertsString =
312 properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "." + topic
313 + PolicyEndPointProperties.PROPERTY_ALLOW_SELF_SIGNED_CERTIFICATES_SUFFIX);
315 // default is to disallow self-signed certs
316 boolean allowSelfSignedCerts = false;
317 if (allowSelfSignedCertsString != null && !allowSelfSignedCertsString.isEmpty()) {
318 allowSelfSignedCerts = Boolean.parseBoolean(allowSelfSignedCertsString);
321 DmaapTopicSink dmaapTopicSink = this.build(BusTopicParams.builder()
325 .apiSecret(apiSecret)
327 .password(aafPassword)
328 .partitionId(partitionKey)
329 .environment(dme2Environment)
330 .aftEnvironment(dme2AftEnvironment)
331 .partner(dme2Partner)
332 .latitude(dme2Latitude)
333 .longitude(dme2Longitude)
334 .additionalProps(dme2AdditionalProps)
337 .allowSelfSignedCerts(allowSelfSignedCerts)
340 newDmaapTopicSinks.add(dmaapTopicSink);
342 return newDmaapTopicSinks;
349 * @param busTopicParams parameters to use to configure the sink
352 protected DmaapTopicSink makeSink(BusTopicParams busTopicParams) {
353 return new InlineDmaapTopicSink(busTopicParams);
357 public void destroy(String topic) {
359 if (topic == null || topic.isEmpty()) {
360 throw new IllegalArgumentException(MISSING_TOPIC);
363 DmaapTopicSink dmaapTopicWriter;
364 synchronized (this) {
365 if (!dmaapTopicWriters.containsKey(topic)) {
369 dmaapTopicWriter = dmaapTopicWriters.remove(topic);
372 dmaapTopicWriter.shutdown();
376 public void destroy() {
377 List<DmaapTopicSink> writers = this.inventory();
378 for (DmaapTopicSink writer : writers) {
382 synchronized (this) {
383 this.dmaapTopicWriters.clear();
388 public DmaapTopicSink get(String topic) {
390 if (topic == null || topic.isEmpty()) {
391 throw new IllegalArgumentException(MISSING_TOPIC);
394 synchronized (this) {
395 if (dmaapTopicWriters.containsKey(topic)) {
396 return dmaapTopicWriters.get(topic);
398 throw new IllegalArgumentException("DmaapTopicSink for " + topic + " not found");
404 public synchronized List<DmaapTopicSink> inventory() {
405 return new ArrayList<>(this.dmaapTopicWriters.values());
409 public String toString() {
410 return "IndexedDmaapTopicSinkFactory []";