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.SingleThreadedDmaapTopicSource;
33 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * DMAAP Topic Source Factory.
40 public interface DmaapTopicSourceFactory {
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 * Creates an DMAAP Topic Source based on properties files.
53 * @param properties Properties containing initialization values
55 * @return an DMAAP Topic Source
56 * @throws IllegalArgumentException if invalid parameters are present
58 List<DmaapTopicSource> build(Properties properties);
61 * Instantiates a new DMAAP Topic Source.
63 * @param busTopicParams parameters object
64 * @return a DMAAP Topic Source
66 DmaapTopicSource build(BusTopicParams busTopicParams);
69 * Instantiates a new DMAAP Topic Source.
71 * @param servers list of servers
72 * @param topic topic name
73 * @param apiKey API Key
74 * @param apiSecret API Secret
76 * @return an DMAAP Topic Source
77 * @throws IllegalArgumentException if invalid parameters are present
79 DmaapTopicSource build(List<String> servers, String topic, String apiKey, String apiSecret);
82 * Instantiates a new DMAAP Topic Source.
84 * @param servers list of servers
85 * @param topic topic name
87 * @return an DMAAP Topic Source
88 * @throws IllegalArgumentException if invalid parameters are present
90 DmaapTopicSource build(List<String> servers, String topic);
93 * Destroys an DMAAP Topic Source based on a topic.
95 * @param topic topic name
96 * @throws IllegalArgumentException if invalid parameters are present
98 void destroy(String topic);
101 * Destroys all DMAAP Topic Sources.
106 * Gets an DMAAP Topic Source based on topic name.
108 * @param topic the topic name
109 * @return an DMAAP Topic Source with topic name
110 * @throws IllegalArgumentException if an invalid topic is provided
111 * @throws IllegalStateException if the DMAAP Topic Source is an incorrect state
113 DmaapTopicSource get(String topic);
116 * Provides a snapshot of the DMAAP Topic Sources.
118 * @return a list of the DMAAP Topic Sources
120 List<DmaapTopicSource> inventory();
124 /* ------------- implementation ----------------- */
127 * Factory of DMAAP Source Topics indexed by topic name.
130 class IndexedDmaapTopicSourceFactory implements DmaapTopicSourceFactory {
131 private static final String MISSING_TOPIC = "A topic must be provided";
136 private static Logger logger = LoggerFactory.getLogger(IndexedDmaapTopicSourceFactory.class);
139 * DMaaP Topic Name Index.
141 protected HashMap<String, DmaapTopicSource> dmaapTopicSources = new HashMap<>();
144 public DmaapTopicSource build(BusTopicParams busTopicParams) {
146 if (busTopicParams.getTopic() == null || busTopicParams.getTopic().isEmpty()) {
147 throw new IllegalArgumentException(MISSING_TOPIC);
150 synchronized (this) {
151 if (dmaapTopicSources.containsKey(busTopicParams.getTopic())) {
152 return dmaapTopicSources.get(busTopicParams.getTopic());
155 DmaapTopicSource dmaapTopicSource = makeSource(busTopicParams);
157 if (busTopicParams.isManaged()) {
158 dmaapTopicSources.put(busTopicParams.getTopic(), dmaapTopicSource);
160 return dmaapTopicSource;
165 public List<DmaapTopicSource> build(Properties properties) {
167 String readTopics = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS);
168 if (readTopics == null || readTopics.isEmpty()) {
169 logger.info("{}: no topic for DMaaP Source", this);
170 return new ArrayList<>();
172 List<String> readTopicList = new ArrayList<>(Arrays.asList(readTopics.split("\\s*,\\s*")));
174 List<DmaapTopicSource> dmaapTopicSourceLst = new ArrayList<>();
175 synchronized (this) {
176 for (String topic : readTopicList) {
177 if (this.dmaapTopicSources.containsKey(topic)) {
178 dmaapTopicSourceLst.add(this.dmaapTopicSources.get(topic));
182 String servers = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "."
183 + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SERVERS_SUFFIX);
185 List<String> serverList;
186 if (servers != null && !servers.isEmpty()) {
187 serverList = new ArrayList<>(Arrays.asList(servers.split("\\s*,\\s*")));
189 serverList = new ArrayList<>();
192 final String apiKey = properties.getProperty(
193 PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "."
194 + topic + PolicyEndPointProperties.PROPERTY_TOPIC_API_KEY_SUFFIX);
196 final String apiSecret = properties.getProperty(
197 PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "."
198 + topic + PolicyEndPointProperties.PROPERTY_TOPIC_API_SECRET_SUFFIX);
200 final String aafMechId = properties.getProperty(
201 PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "."
202 + topic + PolicyEndPointProperties.PROPERTY_TOPIC_AAF_MECHID_SUFFIX);
204 final String aafPassword = properties.getProperty(
205 PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "."
206 + topic + PolicyEndPointProperties.PROPERTY_TOPIC_AAF_PASSWORD_SUFFIX);
208 final String consumerGroup = properties.getProperty(
209 PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS
210 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_CONSUMER_GROUP_SUFFIX);
212 final String consumerInstance = properties.getProperty(
213 PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS
214 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_CONSUMER_INSTANCE_SUFFIX);
216 final String fetchTimeoutString = properties.getProperty(
217 PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS
218 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_FETCH_TIMEOUT_SUFFIX);
220 /* DME2 Properties */
222 final String dme2Environment = properties.getProperty(
223 PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS
224 + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_ENVIRONMENT_SUFFIX);
226 final String dme2AftEnvironment = properties.getProperty(
227 PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS
228 + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_AFT_ENVIRONMENT_SUFFIX);
230 final String dme2Partner = properties.getProperty(
231 PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "."
232 + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_PARTNER_SUFFIX);
234 final String dme2RouteOffer = properties.getProperty(
235 PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS
236 + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_ROUTE_OFFER_SUFFIX);
238 final String dme2Latitude = properties.getProperty(
239 PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "."
240 + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_LATITUDE_SUFFIX);
242 final String dme2Longitude = properties.getProperty(
243 PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS
244 + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_LONGITUDE_SUFFIX);
246 final String dme2EpReadTimeoutMs =
247 properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "." + topic
248 + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_EP_READ_TIMEOUT_MS_SUFFIX);
250 final String dme2EpConnTimeout = properties.getProperty(
251 PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS
252 + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_EP_CONN_TIMEOUT_SUFFIX);
254 final String dme2RoundtripTimeoutMs =
255 properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "." + topic
256 + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_ROUNDTRIP_TIMEOUT_MS_SUFFIX);
258 final String dme2Version = properties.getProperty(
259 PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "."
260 + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_VERSION_SUFFIX);
262 final String dme2SubContextPath = properties.getProperty(
263 PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS
264 + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_SUB_CONTEXT_PATH_SUFFIX);
266 final String dme2SessionStickinessRequired =
267 properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "." + topic
268 + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_SESSION_STICKINESS_REQUIRED_SUFFIX);
270 Map<String, String> dme2AdditionalProps = new HashMap<>();
272 if (dme2EpReadTimeoutMs != null && !dme2EpReadTimeoutMs.isEmpty()) {
273 dme2AdditionalProps.put(DME2_READ_TIMEOUT_PROPERTY, dme2EpReadTimeoutMs);
275 if (dme2EpConnTimeout != null && !dme2EpConnTimeout.isEmpty()) {
276 dme2AdditionalProps.put(DME2_EP_CONN_TIMEOUT_PROPERTY, dme2EpConnTimeout);
278 if (dme2RoundtripTimeoutMs != null && !dme2RoundtripTimeoutMs.isEmpty()) {
279 dme2AdditionalProps.put(DME2_ROUNDTRIP_TIMEOUT_PROPERTY, dme2RoundtripTimeoutMs);
281 if (dme2Version != null && !dme2Version.isEmpty()) {
282 dme2AdditionalProps.put(DME2_VERSION_PROPERTY, dme2Version);
284 if (dme2RouteOffer != null && !dme2RouteOffer.isEmpty()) {
285 dme2AdditionalProps.put(DME2_ROUTE_OFFER_PROPERTY, dme2RouteOffer);
287 if (dme2SubContextPath != null && !dme2SubContextPath.isEmpty()) {
288 dme2AdditionalProps.put(DME2_SUBCONTEXT_PATH_PROPERTY, dme2SubContextPath);
290 if (dme2SessionStickinessRequired != null && !dme2SessionStickinessRequired.isEmpty()) {
291 dme2AdditionalProps.put(DME2_SESSION_STICKINESS_REQUIRED_PROPERTY, dme2SessionStickinessRequired);
295 if (servers == null || servers.isEmpty()) {
297 logger.error("{}: no DMaaP servers or DME2 ServiceName provided", this);
301 int fetchTimeout = DmaapTopicSource.DEFAULT_TIMEOUT_MS_FETCH;
302 if (fetchTimeoutString != null && !fetchTimeoutString.isEmpty()) {
304 fetchTimeout = Integer.parseInt(fetchTimeoutString);
305 } catch (NumberFormatException nfe) {
306 logger.warn("{}: fetch timeout {} is in invalid format for topic {} ", this, fetchTimeoutString,
311 String fetchLimitString = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS
312 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_FETCH_LIMIT_SUFFIX);
313 int fetchLimit = DmaapTopicSource.DEFAULT_LIMIT_FETCH;
314 if (fetchLimitString != null && !fetchLimitString.isEmpty()) {
316 fetchLimit = Integer.parseInt(fetchLimitString);
317 } catch (NumberFormatException nfe) {
318 logger.warn("{}: fetch limit {} is in invalid format for topic {} ", this, fetchLimitString,
323 String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS
324 + "." + topic + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX);
325 boolean managed = true;
326 if (managedString != null && !managedString.isEmpty()) {
327 managed = Boolean.parseBoolean(managedString);
330 String useHttpsString = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS
331 + "." + topic + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX);
333 // default is to use HTTP if no https property exists
334 boolean useHttps = false;
335 if (useHttpsString != null && !useHttpsString.isEmpty()) {
336 useHttps = Boolean.parseBoolean(useHttpsString);
339 String allowSelfSignedCertsString =
340 properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "." + topic
341 + PolicyEndPointProperties.PROPERTY_ALLOW_SELF_SIGNED_CERTIFICATES_SUFFIX);
343 // default is to disallow self-signed certs
344 boolean allowSelfSignedCerts = false;
345 if (allowSelfSignedCertsString != null && !allowSelfSignedCertsString.isEmpty()) {
346 allowSelfSignedCerts = Boolean.parseBoolean(allowSelfSignedCertsString);
350 DmaapTopicSource uebTopicSource = this.build(BusTopicParams.builder()
354 .apiSecret(apiSecret)
356 .password(aafPassword)
357 .consumerGroup(consumerGroup)
358 .consumerInstance(consumerInstance)
359 .fetchTimeout(fetchTimeout)
360 .fetchLimit(fetchLimit)
361 .environment(dme2Environment)
362 .aftEnvironment(dme2AftEnvironment)
363 .partner(dme2Partner)
364 .latitude(dme2Latitude)
365 .longitude(dme2Longitude)
366 .additionalProps(dme2AdditionalProps)
369 .allowSelfSignedCerts(allowSelfSignedCerts)
372 dmaapTopicSourceLst.add(uebTopicSource);
375 return dmaapTopicSourceLst;
379 public DmaapTopicSource build(List<String> servers, String topic, String apiKey, String apiSecret) {
380 return this.build(BusTopicParams.builder()
384 .apiSecret(apiSecret)
385 .fetchTimeout(DmaapTopicSource.DEFAULT_TIMEOUT_MS_FETCH)
386 .fetchLimit(DmaapTopicSource.DEFAULT_LIMIT_FETCH)
389 .allowSelfSignedCerts(false)
394 public DmaapTopicSource build(List<String> servers, String topic) {
395 return this.build(servers, topic, null, null);
399 * Makes a new source.
401 * @param busTopicParams parameters to use to configure the source
402 * @return a new source
404 protected DmaapTopicSource makeSource(BusTopicParams busTopicParams) {
405 return new SingleThreadedDmaapTopicSource(busTopicParams);
409 public void destroy(String topic) {
411 if (topic == null || topic.isEmpty()) {
412 throw new IllegalArgumentException(MISSING_TOPIC);
415 DmaapTopicSource uebTopicSource;
417 synchronized (this) {
418 if (!dmaapTopicSources.containsKey(topic)) {
422 uebTopicSource = dmaapTopicSources.remove(topic);
425 uebTopicSource.shutdown();
429 public void destroy() {
430 List<DmaapTopicSource> readers = this.inventory();
431 for (DmaapTopicSource reader : readers) {
435 synchronized (this) {
436 this.dmaapTopicSources.clear();
441 public DmaapTopicSource get(String topic) {
443 if (topic == null || topic.isEmpty()) {
444 throw new IllegalArgumentException(MISSING_TOPIC);
447 synchronized (this) {
448 if (dmaapTopicSources.containsKey(topic)) {
449 return dmaapTopicSources.get(topic);
451 throw new IllegalArgumentException("DmaapTopiceSource for " + topic + " not found");
457 public synchronized List<DmaapTopicSource> inventory() {
458 return new ArrayList<>(this.dmaapTopicSources.values());
462 public String toString() {
463 return "IndexedDmaapTopicSourceFactory []";