2 * ============LICENSE_START=======================================================
3 * ONAP Policy Engine - Common Modules
4 * ================================================================================
5 * Copyright (C) 2017-2019 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;
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.internal.BusTopicParams;
31 import org.onap.policy.common.endpoints.event.comm.bus.internal.InlineDmaapTopicSink;
32 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * Factory of DMAAP Reader Topics indexed by topic name.
39 class IndexedDmaapTopicSinkFactory implements DmaapTopicSinkFactory {
41 private static final String MISSING_TOPIC = "A topic must be provided";
46 private static Logger logger = LoggerFactory.getLogger(IndexedDmaapTopicSinkFactory.class);
49 * DMAAP Topic Name Index.
51 protected HashMap<String, DmaapTopicSink> dmaapTopicWriters = new HashMap<>();
54 public DmaapTopicSink build(BusTopicParams busTopicParams) {
56 if (busTopicParams.getTopic() == null || busTopicParams.getTopic().isEmpty()) {
57 throw new IllegalArgumentException(MISSING_TOPIC);
61 if (dmaapTopicWriters.containsKey(busTopicParams.getTopic())) {
62 return dmaapTopicWriters.get(busTopicParams.getTopic());
65 DmaapTopicSink dmaapTopicSink = makeSink(busTopicParams);
67 if (busTopicParams.isManaged()) {
68 dmaapTopicWriters.put(busTopicParams.getTopic(), dmaapTopicSink);
70 return dmaapTopicSink;
75 public DmaapTopicSink build(List<String> servers, String topic) {
76 return this.build(BusTopicParams.builder()
81 .allowSelfSignedCerts(false)
86 public List<DmaapTopicSink> build(Properties properties) {
88 String writeTopics = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS);
89 if (writeTopics == null || writeTopics.isEmpty()) {
90 logger.info("{}: no topic for DMaaP Sink", this);
91 return new ArrayList<>();
94 List<String> writeTopicList = new ArrayList<>(Arrays.asList(writeTopics.split("\\s*,\\s*")));
95 List<DmaapTopicSink> newDmaapTopicSinks = new ArrayList<>();
97 for (String topic : writeTopicList) {
98 if (this.dmaapTopicWriters.containsKey(topic)) {
99 newDmaapTopicSinks.add(this.dmaapTopicWriters.get(topic));
102 String servers = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "."
103 + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SERVERS_SUFFIX);
105 List<String> serverList;
106 if (servers != null && !servers.isEmpty()) {
107 serverList = new ArrayList<>(Arrays.asList(servers.split("\\s*,\\s*")));
109 serverList = new ArrayList<>();
112 final String apiKey = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS
113 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_API_KEY_SUFFIX);
114 final String apiSecret = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS
115 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_API_SECRET_SUFFIX);
117 final String aafMechId = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS
118 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_AAF_MECHID_SUFFIX);
119 final String aafPassword = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS
120 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_AAF_PASSWORD_SUFFIX);
122 final String partitionKey = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS
123 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SINK_PARTITION_KEY_SUFFIX);
125 final String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS
126 + "." + topic + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX);
128 /* DME2 Properties */
130 final String dme2Environment = properties.getProperty(
131 PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS
132 + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_ENVIRONMENT_SUFFIX);
134 final String dme2AftEnvironment = properties.getProperty(
135 PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS
136 + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_AFT_ENVIRONMENT_SUFFIX);
138 final String dme2Partner = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS
139 + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_PARTNER_SUFFIX);
141 final String dme2RouteOffer = properties.getProperty(
142 PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS
143 + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_ROUTE_OFFER_SUFFIX);
145 final String dme2Latitude = properties.getProperty(
146 PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "."
147 + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_LATITUDE_SUFFIX);
149 final String dme2Longitude = properties.getProperty(
150 PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS
151 + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_LONGITUDE_SUFFIX);
153 final String dme2EpReadTimeoutMs = properties.getProperty(
154 PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS
155 + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_EP_READ_TIMEOUT_MS_SUFFIX);
157 final String dme2EpConnTimeout = properties.getProperty(
158 PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS
159 + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_EP_CONN_TIMEOUT_SUFFIX);
161 final String dme2RoundtripTimeoutMs =
162 properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS
164 + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_ROUNDTRIP_TIMEOUT_MS_SUFFIX);
166 final String dme2Version = properties.getProperty(
167 PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "."
168 + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_VERSION_SUFFIX);
170 final String dme2SubContextPath = properties.getProperty(
171 PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS
172 + "." + topic + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_SUB_CONTEXT_PATH_SUFFIX);
174 final String dme2SessionStickinessRequired =
175 properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS
177 + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_SESSION_STICKINESS_REQUIRED_SUFFIX);
179 Map<String, String> dme2AdditionalProps = new HashMap<>();
181 if (dme2EpReadTimeoutMs != null && !dme2EpReadTimeoutMs.isEmpty()) {
182 dme2AdditionalProps.put(DME2_READ_TIMEOUT_PROPERTY, dme2EpReadTimeoutMs);
184 if (dme2EpConnTimeout != null && !dme2EpConnTimeout.isEmpty()) {
185 dme2AdditionalProps.put(DME2_EP_CONN_TIMEOUT_PROPERTY, dme2EpConnTimeout);
187 if (dme2RoundtripTimeoutMs != null && !dme2RoundtripTimeoutMs.isEmpty()) {
188 dme2AdditionalProps.put(DME2_ROUNDTRIP_TIMEOUT_PROPERTY, dme2RoundtripTimeoutMs);
190 if (dme2Version != null && !dme2Version.isEmpty()) {
191 dme2AdditionalProps.put(DME2_VERSION_PROPERTY, dme2Version);
193 if (dme2RouteOffer != null && !dme2RouteOffer.isEmpty()) {
194 dme2AdditionalProps.put(DME2_ROUTE_OFFER_PROPERTY, dme2RouteOffer);
196 if (dme2SubContextPath != null && !dme2SubContextPath.isEmpty()) {
197 dme2AdditionalProps.put(DME2_SUBCONTEXT_PATH_PROPERTY, dme2SubContextPath);
199 if (dme2SessionStickinessRequired != null && !dme2SessionStickinessRequired.isEmpty()) {
200 dme2AdditionalProps.put(DME2_SESSION_STICKINESS_REQUIRED_PROPERTY, dme2SessionStickinessRequired);
203 if (servers == null || servers.isEmpty()) {
204 logger.error("{}: no DMaaP servers or DME2 ServiceName provided", this);
208 boolean managed = true;
209 if (managedString != null && !managedString.isEmpty()) {
210 managed = Boolean.parseBoolean(managedString);
213 String useHttpsString = properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "."
214 + topic + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX);
216 // default is to use HTTP if no https property exists
217 boolean useHttps = false;
218 if (useHttpsString != null && !useHttpsString.isEmpty()) {
219 useHttps = Boolean.parseBoolean(useHttpsString);
222 String allowSelfSignedCertsString =
223 properties.getProperty(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "." + topic
224 + PolicyEndPointProperties.PROPERTY_ALLOW_SELF_SIGNED_CERTIFICATES_SUFFIX);
226 // default is to disallow self-signed certs
227 boolean allowSelfSignedCerts = false;
228 if (allowSelfSignedCertsString != null && !allowSelfSignedCertsString.isEmpty()) {
229 allowSelfSignedCerts = Boolean.parseBoolean(allowSelfSignedCertsString);
232 DmaapTopicSink dmaapTopicSink = this.build(BusTopicParams.builder()
236 .apiSecret(apiSecret)
238 .password(aafPassword)
239 .partitionId(partitionKey)
240 .environment(dme2Environment)
241 .aftEnvironment(dme2AftEnvironment)
242 .partner(dme2Partner)
243 .latitude(dme2Latitude)
244 .longitude(dme2Longitude)
245 .additionalProps(dme2AdditionalProps)
248 .allowSelfSignedCerts(allowSelfSignedCerts)
251 newDmaapTopicSinks.add(dmaapTopicSink);
253 return newDmaapTopicSinks;
260 * @param busTopicParams parameters to use to configure the sink
263 protected DmaapTopicSink makeSink(BusTopicParams busTopicParams) {
264 return new InlineDmaapTopicSink(busTopicParams);
268 public void destroy(String topic) {
270 if (topic == null || topic.isEmpty()) {
271 throw new IllegalArgumentException(MISSING_TOPIC);
274 DmaapTopicSink dmaapTopicWriter;
275 synchronized (this) {
276 if (!dmaapTopicWriters.containsKey(topic)) {
280 dmaapTopicWriter = dmaapTopicWriters.remove(topic);
283 dmaapTopicWriter.shutdown();
287 public void destroy() {
288 List<DmaapTopicSink> writers = this.inventory();
289 for (DmaapTopicSink writer : writers) {
293 synchronized (this) {
294 this.dmaapTopicWriters.clear();
299 public DmaapTopicSink get(String topic) {
301 if (topic == null || topic.isEmpty()) {
302 throw new IllegalArgumentException(MISSING_TOPIC);
305 synchronized (this) {
306 if (dmaapTopicWriters.containsKey(topic)) {
307 return dmaapTopicWriters.get(topic);
309 throw new IllegalStateException("DmaapTopicSink for " + topic + " not found");
315 public synchronized List<DmaapTopicSink> inventory() {
316 return new ArrayList<>(this.dmaapTopicWriters.values());
320 public String toString() {
321 return "IndexedDmaapTopicSinkFactory []";