5363a30df83dbc7f44a46e42622ca09db4fb91a4
[policy/common.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * policy-endpoints
4  * ================================================================================
5  * Copyright (C) 2017-2018 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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.policy.common.endpoints.event.comm.bus.impl;
22
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Properties;
28
29 import org.onap.policy.common.endpoints.event.comm.bus.UebTopicSource;
30 import org.onap.policy.common.endpoints.event.comm.bus.UebTopicSourceFactory;
31 import org.onap.policy.common.endpoints.event.comm.bus.internal.SingleThreadedUebTopicSource;
32 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * Factory of UEB Source Topics indexed by topic name
38  */
39 public class IndexedUebTopicSourceFactory implements UebTopicSourceFactory {
40     private static final String MISSING_TOPIC = "A topic must be provided";
41
42     private static final IndexedUebTopicSourceFactory instance = new IndexedUebTopicSourceFactory();
43
44     /**
45      * Logger
46      */
47     private static Logger logger = LoggerFactory.getLogger(IndexedUebTopicSourceFactory.class);
48
49     /**
50      * UEB Topic Name Index
51      */
52     protected HashMap<String, UebTopicSource> uebTopicSources = new HashMap<>();
53
54     /**
55      * Get the singleton instance.
56      * 
57      * @return the instance
58      */
59     public static IndexedUebTopicSourceFactory getInstance() {
60         return instance;
61     }
62
63     private IndexedUebTopicSourceFactory() {}
64
65     /**
66      * {@inheritDoc}
67      */
68     @Override
69     public UebTopicSource build(List<String> servers, String topic, String apiKey, String apiSecret,
70             String consumerGroup, String consumerInstance, int fetchTimeout, int fetchLimit, boolean managed,
71             boolean useHttps, boolean allowSelfSignedCerts) {
72         if (servers == null || servers.isEmpty()) {
73             throw new IllegalArgumentException("UEB Server(s) must be provided");
74         }
75
76         if (topic == null || topic.isEmpty()) {
77             throw new IllegalArgumentException(MISSING_TOPIC);
78         }
79
80         synchronized (this) {
81             if (uebTopicSources.containsKey(topic)) {
82                 return uebTopicSources.get(topic);
83             }
84
85             UebTopicSource uebTopicSource = new SingleThreadedUebTopicSource(servers, topic, apiKey, apiSecret,
86                     consumerGroup, consumerInstance, fetchTimeout, fetchLimit, useHttps, allowSelfSignedCerts);
87
88             if (managed) {
89                 uebTopicSources.put(topic, uebTopicSource);
90             }
91
92             return uebTopicSource;
93         }
94     }
95
96     /**
97      * {@inheritDoc}
98      */
99     @Override
100     public List<UebTopicSource> build(Properties properties) {
101
102         String readTopics = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS);
103         if (readTopics == null || readTopics.isEmpty()) {
104             logger.info("{}: no topic for UEB Source", this);
105             return new ArrayList<>();
106         }
107         List<String> readTopicList = new ArrayList<>(Arrays.asList(readTopics.split("\\s*,\\s*")));
108
109         List<UebTopicSource> newUebTopicSources = new ArrayList<>();
110         synchronized (this) {
111             for (String topic : readTopicList) {
112                 if (this.uebTopicSources.containsKey(topic)) {
113                     newUebTopicSources.add(this.uebTopicSources.get(topic));
114                     continue;
115                 }
116
117                 String servers = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "." + topic
118                         + PolicyEndPointProperties.PROPERTY_TOPIC_SERVERS_SUFFIX);
119
120                 if (servers == null || servers.isEmpty()) {
121                     logger.error("{}: no UEB servers configured for sink {}", this, topic);
122                     continue;
123                 }
124
125                 List<String> serverList = new ArrayList<>(Arrays.asList(servers.split("\\s*,\\s*")));
126
127                 String apiKey = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "." + topic
128                         + PolicyEndPointProperties.PROPERTY_TOPIC_API_KEY_SUFFIX);
129
130                 String apiSecret = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "." + topic
131                         + PolicyEndPointProperties.PROPERTY_TOPIC_API_SECRET_SUFFIX);
132
133                 String consumerGroup = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "." + topic
134                         + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_CONSUMER_GROUP_SUFFIX);
135
136                 String consumerInstance = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "."
137                         + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_CONSUMER_INSTANCE_SUFFIX);
138
139                 String fetchTimeoutString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "."
140                         + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_FETCH_TIMEOUT_SUFFIX);
141                 int fetchTimeout = UebTopicSource.DEFAULT_TIMEOUT_MS_FETCH;
142                 if (fetchTimeoutString != null && !fetchTimeoutString.isEmpty()) {
143                     try {
144                         fetchTimeout = Integer.parseInt(fetchTimeoutString);
145                     } catch (NumberFormatException nfe) {
146                         logger.warn("{}: fetch timeout {} is in invalid format for topic {} ", this, fetchTimeoutString,
147                                 topic);
148                     }
149                 }
150
151                 String fetchLimitString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "."
152                         + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_FETCH_LIMIT_SUFFIX);
153                 int fetchLimit = UebTopicSource.DEFAULT_LIMIT_FETCH;
154                 if (fetchLimitString != null && !fetchLimitString.isEmpty()) {
155                     try {
156                         fetchLimit = Integer.parseInt(fetchLimitString);
157                     } catch (NumberFormatException nfe) {
158                         logger.warn("{}: fetch limit {} is in invalid format for topic {} ", this, fetchLimitString,
159                                 topic);
160                     }
161                 }
162
163                 String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "." + topic
164                         + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX);
165                 boolean managed = true;
166                 if (managedString != null && !managedString.isEmpty()) {
167                     managed = Boolean.parseBoolean(managedString);
168                 }
169
170                 String useHttpsString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "." + topic
171                         + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX);
172
173                 // default is to use HTTP if no https property exists
174                 boolean useHttps = false;
175                 if (useHttpsString != null && !useHttpsString.isEmpty()) {
176                     useHttps = Boolean.parseBoolean(useHttpsString);
177                 }
178
179                 String allowSelfSignedCertsString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS
180                         + "." + topic + PolicyEndPointProperties.PROPERTY_ALLOW_SELF_SIGNED_CERTIFICATES_SUFFIX);
181
182                 // default is to disallow self-signed certs
183                 boolean allowSelfSignedCerts = false;
184                 if (allowSelfSignedCertsString != null && !allowSelfSignedCertsString.isEmpty()) {
185                     allowSelfSignedCerts = Boolean.parseBoolean(allowSelfSignedCertsString);
186                 }
187
188                 UebTopicSource uebTopicSource = this.build(serverList, topic, apiKey, apiSecret, consumerGroup,
189                         consumerInstance, fetchTimeout, fetchLimit, managed, useHttps, allowSelfSignedCerts);
190                 newUebTopicSources.add(uebTopicSource);
191             }
192         }
193         return newUebTopicSources;
194     }
195
196     /**
197      * {@inheritDoc}
198      */
199     @Override
200     public UebTopicSource build(List<String> servers, String topic, String apiKey, String apiSecret) {
201
202         return this.build(servers, topic, apiKey, apiSecret, null, null, UebTopicSource.DEFAULT_TIMEOUT_MS_FETCH,
203                 UebTopicSource.DEFAULT_LIMIT_FETCH, true, false, true);
204     }
205
206     /**
207      * {@inheritDoc}
208      */
209     @Override
210     public UebTopicSource build(List<String> servers, String topic) {
211         return this.build(servers, topic, null, null);
212     }
213
214     /**
215      * {@inheritDoc}
216      */
217     @Override
218     public void destroy(String topic) {
219
220         if (topic == null || topic.isEmpty()) {
221             throw new IllegalArgumentException(MISSING_TOPIC);
222         }
223
224         UebTopicSource uebTopicSource;
225
226         synchronized (this) {
227             if (!uebTopicSources.containsKey(topic)) {
228                 return;
229             }
230
231             uebTopicSource = uebTopicSources.remove(topic);
232         }
233
234         uebTopicSource.shutdown();
235     }
236
237     /**
238      * {@inheritDoc}
239      */
240     @Override
241     public UebTopicSource get(String topic) {
242
243         if (topic == null || topic.isEmpty()) {
244             throw new IllegalArgumentException(MISSING_TOPIC);
245         }
246
247         synchronized (this) {
248             if (uebTopicSources.containsKey(topic)) {
249                 return uebTopicSources.get(topic);
250             } else {
251                 throw new IllegalStateException("UebTopiceSource for " + topic + " not found");
252             }
253         }
254     }
255
256     @Override
257     public synchronized List<UebTopicSource> inventory() {
258         return new ArrayList<>(this.uebTopicSources.values());
259     }
260
261     @Override
262     public void destroy() {
263         List<UebTopicSource> readers = this.inventory();
264         for (UebTopicSource reader : readers) {
265             reader.shutdown();
266         }
267
268         synchronized (this) {
269             this.uebTopicSources.clear();
270         }
271     }
272
273     @Override
274     public String toString() {
275         StringBuilder builder = new StringBuilder();
276         builder.append("IndexedUebTopicSourceFactory []");
277         return builder.toString();
278     }
279
280 }