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>();
285 List<String> writeTopicList = new ArrayList<String>(Arrays.asList(writeTopics.split("\\s*,\\s*")));
288 List<DmaapTopicSink> dmaapTopicWriters = new ArrayList<DmaapTopicSink>();
289 for (String topic: writeTopicList) {
291 String servers = properties.getProperty(PolicyProperties.PROPERTY_DMAAP_SINK_TOPICS + "." +
293 PolicyProperties.PROPERTY_TOPIC_SERVERS_SUFFIX);
295 List<String> serverList;
296 if (servers != null && !servers.isEmpty()) serverList = new ArrayList<String>(Arrays.asList(servers.split("\\s*,\\s*")));
297 else serverList = new ArrayList<>();
299 String apiKey = properties.getProperty(PolicyProperties.PROPERTY_DMAAP_SINK_TOPICS +
301 PolicyProperties.PROPERTY_TOPIC_API_KEY_SUFFIX);
302 String apiSecret = properties.getProperty(PolicyProperties.PROPERTY_DMAAP_SINK_TOPICS +
304 PolicyProperties.PROPERTY_TOPIC_API_SECRET_SUFFIX);
306 String aafMechId = properties.getProperty(PolicyProperties.PROPERTY_DMAAP_SINK_TOPICS +
308 PolicyProperties.PROPERTY_TOPIC_AAF_MECHID_SUFFIX);
309 String aafPassword = properties.getProperty(PolicyProperties.PROPERTY_DMAAP_SINK_TOPICS +
311 PolicyProperties.PROPERTY_TOPIC_AAF_PASSWORD_SUFFIX);
313 String partitionKey = properties.getProperty(PolicyProperties.PROPERTY_DMAAP_SINK_TOPICS +
315 PolicyProperties.PROPERTY_TOPIC_SINK_PARTITION_KEY_SUFFIX);
317 String managedString = properties.getProperty(PolicyProperties.PROPERTY_UEB_SINK_TOPICS + "." + topic +
318 PolicyProperties.PROPERTY_MANAGED_SUFFIX);
320 /* DME2 Properties */
322 String dme2Environment = properties.getProperty(PolicyProperties.PROPERTY_DMAAP_SINK_TOPICS + "."
323 + topic + PolicyProperties.PROPERTY_DMAAP_DME2_ENVIRONMENT_SUFFIX);
325 String dme2AftEnvironment = properties.getProperty(PolicyProperties.PROPERTY_DMAAP_SINK_TOPICS + "."
326 + topic + PolicyProperties.PROPERTY_DMAAP_DME2_AFT_ENVIRONMENT_SUFFIX);
328 String dme2Partner = properties.getProperty(PolicyProperties.PROPERTY_DMAAP_SINK_TOPICS + "." + topic
329 + PolicyProperties.PROPERTY_DMAAP_DME2_PARTNER_SUFFIX);
331 String dme2RouteOffer = properties.getProperty(PolicyProperties.PROPERTY_DMAAP_SINK_TOPICS + "." + topic
332 + PolicyProperties.PROPERTY_DMAAP_DME2_ROUTE_OFFER_SUFFIX);
334 String dme2Latitude = properties.getProperty(PolicyProperties.PROPERTY_DMAAP_SINK_TOPICS + "." + topic
335 + PolicyProperties.PROPERTY_DMAAP_DME2_LATITUDE_SUFFIX);
337 String dme2Longitude = properties.getProperty(PolicyProperties.PROPERTY_DMAAP_SINK_TOPICS + "."
338 + topic + PolicyProperties.PROPERTY_DMAAP_DME2_LONGITUDE_SUFFIX);
340 String dme2EpReadTimeoutMs = properties.getProperty(PolicyProperties.PROPERTY_DMAAP_SINK_TOPICS + "."
341 + topic + PolicyProperties.PROPERTY_DMAAP_DME2_EP_READ_TIMEOUT_MS_SUFFIX);
343 String dme2EpConnTimeout = properties.getProperty(PolicyProperties.PROPERTY_DMAAP_SINK_TOPICS + "."
344 + topic + PolicyProperties.PROPERTY_DMAAP_DME2_EP_CONN_TIMEOUT_SUFFIX);
346 String dme2RoundtripTimeoutMs = properties.getProperty(PolicyProperties.PROPERTY_DMAAP_SINK_TOPICS
347 + "." + topic + PolicyProperties.PROPERTY_DMAAP_DME2_ROUNDTRIP_TIMEOUT_MS_SUFFIX);
349 String dme2Version = properties.getProperty(PolicyProperties.PROPERTY_DMAAP_SINK_TOPICS + "." + topic
350 + PolicyProperties.PROPERTY_DMAAP_DME2_VERSION_SUFFIX);
352 String dme2SubContextPath = properties.getProperty(PolicyProperties.PROPERTY_DMAAP_SINK_TOPICS + "."
353 + topic + PolicyProperties.PROPERTY_DMAAP_DME2_SUB_CONTEXT_PATH_SUFFIX);
355 String dme2SessionStickinessRequired = properties
356 .getProperty(PolicyProperties.PROPERTY_DMAAP_SINK_TOPICS + "." + topic
357 + PolicyProperties.PROPERTY_DMAAP_DME2_SESSION_STICKINESS_REQUIRED_SUFFIX);
359 Map<String,String> dme2AdditionalProps = new HashMap<>();
361 if (dme2EpReadTimeoutMs != null && !dme2EpReadTimeoutMs.isEmpty())
362 dme2AdditionalProps.put(DME2_READ_TIMEOUT_PROPERTY, dme2EpReadTimeoutMs);
363 if (dme2EpConnTimeout != null && !dme2EpConnTimeout.isEmpty())
364 dme2AdditionalProps.put(DME2_EP_CONN_TIMEOUT_PROPERTY, dme2EpConnTimeout);
365 if (dme2RoundtripTimeoutMs != null && !dme2RoundtripTimeoutMs.isEmpty())
366 dme2AdditionalProps.put(DME2_ROUNDTRIP_TIMEOUT_PROPERTY, dme2RoundtripTimeoutMs);
367 if (dme2Version != null && !dme2Version.isEmpty())
368 dme2AdditionalProps.put(DME2_VERSION_PROPERTY, dme2Version);
369 if (dme2RouteOffer != null && !dme2RouteOffer.isEmpty())
370 dme2AdditionalProps.put(DME2_ROUTE_OFFER_PROPERTY, dme2RouteOffer);
371 if (dme2SubContextPath != null && !dme2SubContextPath.isEmpty())
372 dme2AdditionalProps.put(DME2_SUBCONTEXT_PATH_PROPERTY, dme2SubContextPath);
373 if (dme2SessionStickinessRequired != null && !dme2SessionStickinessRequired.isEmpty())
374 dme2AdditionalProps.put(DME2_SESSION_STICKINESS_REQUIRED_PROPERTY, dme2SessionStickinessRequired);
376 if (servers == null || servers.isEmpty()) {
377 logger.error("No DMaaP servers or DME2 ServiceName provided");
381 boolean managed = true;
382 if (managedString != null && !managedString.isEmpty()) {
383 managed = Boolean.parseBoolean(managedString);
386 String useHttpsString = properties.getProperty(PolicyProperties.PROPERTY_DMAAP_SINK_TOPICS + "." + topic +
387 PolicyProperties.PROPERTY_HTTP_HTTPS_SUFFIX);
389 //default is to use HTTP if no https property exists
390 boolean useHttps = false;
391 if (useHttpsString != null && !useHttpsString.isEmpty()){
392 useHttps = Boolean.parseBoolean(useHttpsString);
396 String allowSelfSignedCertsString = properties.getProperty(PolicyProperties.PROPERTY_DMAAP_SINK_TOPICS + "." + topic +
397 PolicyProperties.PROPERTY_ALLOW_SELF_SIGNED_CERTIFICATES_SUFFIX);
399 //default is to disallow self-signed certs
400 boolean allowSelfSignedCerts = false;
401 if (allowSelfSignedCertsString != null && !allowSelfSignedCertsString.isEmpty()){
402 allowSelfSignedCerts = Boolean.parseBoolean(allowSelfSignedCertsString);
405 DmaapTopicSink dmaapTopicSink = this.build(serverList, topic,
407 aafMechId, aafPassword,
409 dme2Environment, dme2AftEnvironment,
410 dme2Partner, dme2Latitude, dme2Longitude,
411 dme2AdditionalProps, managed, useHttps, allowSelfSignedCerts);
413 dmaapTopicWriters.add(dmaapTopicSink);
415 return dmaapTopicWriters;
423 public void destroy(String topic)
424 throws IllegalArgumentException {
426 if (topic == null || topic.isEmpty()) {
427 throw new IllegalArgumentException("A topic must be provided");
430 DmaapTopicSink dmaapTopicWriter;
432 if (!dmaapTopicWriters.containsKey(topic)) {
436 dmaapTopicWriter = dmaapTopicWriters.remove(topic);
439 dmaapTopicWriter.shutdown();
446 public void destroy() {
447 List<DmaapTopicSink> writers = this.inventory();
448 for (DmaapTopicSink writer: writers) {
453 this.dmaapTopicWriters.clear();
461 public DmaapTopicSink get(String topic)
462 throws IllegalArgumentException, IllegalStateException {
464 if (topic == null || topic.isEmpty()) {
465 throw new IllegalArgumentException("A topic must be provided");
469 if (dmaapTopicWriters.containsKey(topic)) {
470 return dmaapTopicWriters.get(topic);
472 throw new IllegalStateException("DmaapTopicSink for " + topic + " not found");
481 public synchronized List<DmaapTopicSink> inventory() {
482 List<DmaapTopicSink> writers =
483 new ArrayList<DmaapTopicSink>(this.dmaapTopicWriters.values());