2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.openecomp.policy.drools.event.comm.bus;
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.openecomp.policy.common.logging.flexlogger.FlexLogger;
31 import org.openecomp.policy.common.logging.flexlogger.Logger;
32 import org.openecomp.policy.drools.event.comm.bus.internal.InlineDmaapTopicSink;
33 import org.openecomp.policy.drools.properties.PolicyProperties;
36 * DMAAP Topic Sink Factory
38 public interface DmaapTopicSinkFactory {
39 public final String DME2_READ_TIMEOUT_PROPERTY = "AFT_DME2_EP_READ_TIMEOUT_MS";
40 public final String DME2_EP_CONN_TIMEOUT_PROPERTY = "AFT_DME2_EP_CONN_TIMEOUT";
41 public final String DME2_ROUNDTRIP_TIMEOUT_PROPERTY = "AFT_DME2_ROUNDTRIP_TIMEOUT_MS";
42 public final String DME2_VERSION_PROPERTY = "Version";
43 public final String DME2_ROUTE_OFFER_PROPERTY = "routeOffer";
44 public final String DME2_SERVICE_NAME_PROPERTY = "ServiceName";
45 public final String DME2_SUBCONTEXT_PATH_PROPERTY = "SubContextPath";
46 public final String DME2_SESSION_STICKINESS_REQUIRED_PROPERTY = "sessionstickinessrequired";
49 * Instantiates a new DMAAP Topic Sink
51 * @param servers list of servers
52 * @param topic topic name
53 * @param apiKey API Key
54 * @param apiSecret API Secret
55 * @param userName AAF user name
56 * @param password AAF password
57 * @param partitionKey Consumer Group
58 * @param environment DME2 environment
59 * @param aftEnvironment DME2 AFT environment
60 * @param partner DME2 Partner
61 * @param latitude DME2 latitude
62 * @param longitude DME2 longitude
63 * @param additionalProps additional properties to pass to DME2
64 * @param managed is this sink endpoint managed?
66 * @return an DMAAP Topic Sink
67 * @throws IllegalArgumentException if invalid parameters are present
69 public DmaapTopicSink build(List<String> servers,
77 String aftEnvironment,
81 Map<String,String> additionalProps,
84 boolean allowSelfSignedCerts) ;
87 * Instantiates a new DMAAP Topic Sink
89 * @param servers list of servers
90 * @param topic topic name
91 * @param apiKey API Key
92 * @param apiSecret API Secret
93 * @param userName AAF user name
94 * @param password AAF password
95 * @param partitionKey Consumer Group
96 * @param managed is this sink endpoint managed?
98 * @return an DMAAP Topic Sink
99 * @throws IllegalArgumentException if invalid parameters are present
101 public DmaapTopicSink build(List<String> servers,
110 boolean allowSelfSignedCerts)
111 throws IllegalArgumentException;
114 * Creates an DMAAP Topic Sink based on properties files
116 * @param properties Properties containing initialization values
118 * @return an DMAAP Topic Sink
119 * @throws IllegalArgumentException if invalid parameters are present
121 public List<DmaapTopicSink> build(Properties properties)
122 throws IllegalArgumentException;
125 * Instantiates a new DMAAP Topic Sink
127 * @param servers list of servers
128 * @param topic topic name
130 * @return an DMAAP Topic Sink
131 * @throws IllegalArgumentException if invalid parameters are present
133 public DmaapTopicSink build(List<String> servers, String topic)
134 throws IllegalArgumentException;
137 * Destroys an DMAAP Topic Sink based on a topic
139 * @param topic topic name
140 * @throws IllegalArgumentException if invalid parameters are present
142 public void destroy(String topic);
145 * gets an DMAAP Topic Sink based on topic name
146 * @param topic the topic name
148 * @return an DMAAP Topic Sink with topic name
149 * @throws IllegalArgumentException if an invalid topic is provided
150 * @throws IllegalStateException if the DMAAP Topic Reader is
153 public DmaapTopicSink get(String topic)
154 throws IllegalArgumentException, IllegalStateException;
157 * Provides a snapshot of the DMAAP Topic Sinks
158 * @return a list of the DMAAP Topic Sinks
160 public List<DmaapTopicSink> inventory();
163 * Destroys all DMAAP Topic Sinks
165 public void destroy();
168 /* ------------- implementation ----------------- */
171 * Factory of DMAAP Reader Topics indexed by topic name
173 class IndexedDmaapTopicSinkFactory implements DmaapTopicSinkFactory {
174 // get an instance of logger
175 private static Logger logger = FlexLogger.getLogger(IndexedDmaapTopicSinkFactory.class);
178 * DMAAP Topic Name Index
180 protected HashMap<String, DmaapTopicSink> dmaapTopicWriters =
181 new HashMap<String, DmaapTopicSink>();
187 public DmaapTopicSink build(List<String> servers,
195 String aftEnvironment,
199 Map<String,String> additionalProps,
202 boolean allowSelfSignedCerts)
203 throws IllegalArgumentException {
205 if (topic == null || topic.isEmpty()) {
206 throw new IllegalArgumentException("A topic must be provided");
209 synchronized (this) {
210 if (dmaapTopicWriters.containsKey(topic)) {
211 return dmaapTopicWriters.get(topic);
214 DmaapTopicSink dmaapTopicSink =
215 new InlineDmaapTopicSink(servers, topic,
219 environment, aftEnvironment,
220 partner, latitude, longitude, additionalProps, useHttps, allowSelfSignedCerts);
223 dmaapTopicWriters.put(topic, dmaapTopicSink);
224 return dmaapTopicSink;
232 public DmaapTopicSink build(List<String> servers,
240 boolean useHttps, boolean allowSelfSignedCerts)
241 throws IllegalArgumentException {
243 if (topic == null || topic.isEmpty()) {
244 throw new IllegalArgumentException("A topic must be provided");
247 synchronized (this) {
248 if (dmaapTopicWriters.containsKey(topic)) {
249 return dmaapTopicWriters.get(topic);
252 DmaapTopicSink dmaapTopicSink =
253 new InlineDmaapTopicSink(servers, topic,
256 partitionKey, useHttps, allowSelfSignedCerts);
259 dmaapTopicWriters.put(topic, dmaapTopicSink);
260 return dmaapTopicSink;
269 public DmaapTopicSink build(List<String> servers, String topic) throws IllegalArgumentException {
270 return this.build(servers, topic, null, null, null, null, null, true, false, false);
278 public List<DmaapTopicSink> build(Properties properties) throws IllegalArgumentException {
280 String writeTopics = properties.getProperty(PolicyProperties.PROPERTY_DMAAP_SINK_TOPICS);
281 if (writeTopics == null || writeTopics.isEmpty()) {
282 logger.warn("No topic for DMAAP Sink " + properties);
283 return new ArrayList<DmaapTopicSink>();
286 List<String> writeTopicList = new ArrayList<String>(Arrays.asList(writeTopics.split("\\s*,\\s*")));
287 List<DmaapTopicSink> newDmaapTopicSinks = new ArrayList<DmaapTopicSink>();
289 for (String topic: writeTopicList) {
290 if (this.dmaapTopicWriters.containsKey(topic)) {
291 newDmaapTopicSinks.add(this.dmaapTopicWriters.get(topic));
294 String servers = properties.getProperty(PolicyProperties.PROPERTY_DMAAP_SINK_TOPICS + "." +
296 PolicyProperties.PROPERTY_TOPIC_SERVERS_SUFFIX);
298 List<String> serverList;
299 if (servers != null && !servers.isEmpty()) serverList = new ArrayList<String>(Arrays.asList(servers.split("\\s*,\\s*")));
300 else serverList = new ArrayList<>();
302 String apiKey = properties.getProperty(PolicyProperties.PROPERTY_DMAAP_SINK_TOPICS +
304 PolicyProperties.PROPERTY_TOPIC_API_KEY_SUFFIX);
305 String apiSecret = properties.getProperty(PolicyProperties.PROPERTY_DMAAP_SINK_TOPICS +
307 PolicyProperties.PROPERTY_TOPIC_API_SECRET_SUFFIX);
309 String aafMechId = properties.getProperty(PolicyProperties.PROPERTY_DMAAP_SINK_TOPICS +
311 PolicyProperties.PROPERTY_TOPIC_AAF_MECHID_SUFFIX);
312 String aafPassword = properties.getProperty(PolicyProperties.PROPERTY_DMAAP_SINK_TOPICS +
314 PolicyProperties.PROPERTY_TOPIC_AAF_PASSWORD_SUFFIX);
316 String partitionKey = properties.getProperty(PolicyProperties.PROPERTY_DMAAP_SINK_TOPICS +
318 PolicyProperties.PROPERTY_TOPIC_SINK_PARTITION_KEY_SUFFIX);
320 String managedString = properties.getProperty(PolicyProperties.PROPERTY_UEB_SINK_TOPICS + "." + topic +
321 PolicyProperties.PROPERTY_MANAGED_SUFFIX);
323 /* DME2 Properties */
325 String dme2Environment = properties.getProperty(PolicyProperties.PROPERTY_DMAAP_SINK_TOPICS + "."
326 + topic + PolicyProperties.PROPERTY_DMAAP_DME2_ENVIRONMENT_SUFFIX);
328 String dme2AftEnvironment = properties.getProperty(PolicyProperties.PROPERTY_DMAAP_SINK_TOPICS + "."
329 + topic + PolicyProperties.PROPERTY_DMAAP_DME2_AFT_ENVIRONMENT_SUFFIX);
331 String dme2Partner = properties.getProperty(PolicyProperties.PROPERTY_DMAAP_SINK_TOPICS + "." + topic
332 + PolicyProperties.PROPERTY_DMAAP_DME2_PARTNER_SUFFIX);
334 String dme2RouteOffer = properties.getProperty(PolicyProperties.PROPERTY_DMAAP_SINK_TOPICS + "." + topic
335 + PolicyProperties.PROPERTY_DMAAP_DME2_ROUTE_OFFER_SUFFIX);
337 String dme2Latitude = properties.getProperty(PolicyProperties.PROPERTY_DMAAP_SINK_TOPICS + "." + topic
338 + PolicyProperties.PROPERTY_DMAAP_DME2_LATITUDE_SUFFIX);
340 String dme2Longitude = properties.getProperty(PolicyProperties.PROPERTY_DMAAP_SINK_TOPICS + "."
341 + topic + PolicyProperties.PROPERTY_DMAAP_DME2_LONGITUDE_SUFFIX);
343 String dme2EpReadTimeoutMs = properties.getProperty(PolicyProperties.PROPERTY_DMAAP_SINK_TOPICS + "."
344 + topic + PolicyProperties.PROPERTY_DMAAP_DME2_EP_READ_TIMEOUT_MS_SUFFIX);
346 String dme2EpConnTimeout = properties.getProperty(PolicyProperties.PROPERTY_DMAAP_SINK_TOPICS + "."
347 + topic + PolicyProperties.PROPERTY_DMAAP_DME2_EP_CONN_TIMEOUT_SUFFIX);
349 String dme2RoundtripTimeoutMs = properties.getProperty(PolicyProperties.PROPERTY_DMAAP_SINK_TOPICS
350 + "." + topic + PolicyProperties.PROPERTY_DMAAP_DME2_ROUNDTRIP_TIMEOUT_MS_SUFFIX);
352 String dme2Version = properties.getProperty(PolicyProperties.PROPERTY_DMAAP_SINK_TOPICS + "." + topic
353 + PolicyProperties.PROPERTY_DMAAP_DME2_VERSION_SUFFIX);
355 String dme2SubContextPath = properties.getProperty(PolicyProperties.PROPERTY_DMAAP_SINK_TOPICS + "."
356 + topic + PolicyProperties.PROPERTY_DMAAP_DME2_SUB_CONTEXT_PATH_SUFFIX);
358 String dme2SessionStickinessRequired = properties
359 .getProperty(PolicyProperties.PROPERTY_DMAAP_SINK_TOPICS + "." + topic
360 + PolicyProperties.PROPERTY_DMAAP_DME2_SESSION_STICKINESS_REQUIRED_SUFFIX);
362 Map<String,String> dme2AdditionalProps = new HashMap<>();
364 if (dme2EpReadTimeoutMs != null && !dme2EpReadTimeoutMs.isEmpty())
365 dme2AdditionalProps.put(DME2_READ_TIMEOUT_PROPERTY, dme2EpReadTimeoutMs);
366 if (dme2EpConnTimeout != null && !dme2EpConnTimeout.isEmpty())
367 dme2AdditionalProps.put(DME2_EP_CONN_TIMEOUT_PROPERTY, dme2EpConnTimeout);
368 if (dme2RoundtripTimeoutMs != null && !dme2RoundtripTimeoutMs.isEmpty())
369 dme2AdditionalProps.put(DME2_ROUNDTRIP_TIMEOUT_PROPERTY, dme2RoundtripTimeoutMs);
370 if (dme2Version != null && !dme2Version.isEmpty())
371 dme2AdditionalProps.put(DME2_VERSION_PROPERTY, dme2Version);
372 if (dme2RouteOffer != null && !dme2RouteOffer.isEmpty())
373 dme2AdditionalProps.put(DME2_ROUTE_OFFER_PROPERTY, dme2RouteOffer);
374 if (dme2SubContextPath != null && !dme2SubContextPath.isEmpty())
375 dme2AdditionalProps.put(DME2_SUBCONTEXT_PATH_PROPERTY, dme2SubContextPath);
376 if (dme2SessionStickinessRequired != null && !dme2SessionStickinessRequired.isEmpty())
377 dme2AdditionalProps.put(DME2_SESSION_STICKINESS_REQUIRED_PROPERTY, dme2SessionStickinessRequired);
379 if (servers == null || servers.isEmpty()) {
380 logger.error("No DMaaP servers or DME2 ServiceName provided");
384 boolean managed = true;
385 if (managedString != null && !managedString.isEmpty()) {
386 managed = Boolean.parseBoolean(managedString);
389 String useHttpsString = properties.getProperty(PolicyProperties.PROPERTY_DMAAP_SINK_TOPICS + "." + topic +
390 PolicyProperties.PROPERTY_HTTP_HTTPS_SUFFIX);
392 //default is to use HTTP if no https property exists
393 boolean useHttps = false;
394 if (useHttpsString != null && !useHttpsString.isEmpty()){
395 useHttps = Boolean.parseBoolean(useHttpsString);
399 String allowSelfSignedCertsString = properties.getProperty(PolicyProperties.PROPERTY_DMAAP_SINK_TOPICS + "." + topic +
400 PolicyProperties.PROPERTY_ALLOW_SELF_SIGNED_CERTIFICATES_SUFFIX);
402 //default is to disallow self-signed certs
403 boolean allowSelfSignedCerts = false;
404 if (allowSelfSignedCertsString != null && !allowSelfSignedCertsString.isEmpty()){
405 allowSelfSignedCerts = Boolean.parseBoolean(allowSelfSignedCertsString);
408 DmaapTopicSink dmaapTopicSink = this.build(serverList, topic,
410 aafMechId, aafPassword,
412 dme2Environment, dme2AftEnvironment,
413 dme2Partner, dme2Latitude, dme2Longitude,
414 dme2AdditionalProps, managed, useHttps, allowSelfSignedCerts);
416 newDmaapTopicSinks.add(dmaapTopicSink);
418 return newDmaapTopicSinks;
426 public void destroy(String topic)
427 throws IllegalArgumentException {
429 if (topic == null || topic.isEmpty()) {
430 throw new IllegalArgumentException("A topic must be provided");
433 DmaapTopicSink dmaapTopicWriter;
435 if (!dmaapTopicWriters.containsKey(topic)) {
439 dmaapTopicWriter = dmaapTopicWriters.remove(topic);
442 dmaapTopicWriter.shutdown();
449 public void destroy() {
450 List<DmaapTopicSink> writers = this.inventory();
451 for (DmaapTopicSink writer: writers) {
456 this.dmaapTopicWriters.clear();
464 public DmaapTopicSink get(String topic)
465 throws IllegalArgumentException, IllegalStateException {
467 if (topic == null || topic.isEmpty()) {
468 throw new IllegalArgumentException("A topic must be provided");
472 if (dmaapTopicWriters.containsKey(topic)) {
473 return dmaapTopicWriters.get(topic);
475 throw new IllegalStateException("DmaapTopicSink for " + topic + " not found");
484 public synchronized List<DmaapTopicSink> inventory() {
485 List<DmaapTopicSink> writers =
486 new ArrayList<DmaapTopicSink>(this.dmaapTopicWriters.values());