1245127a0102c82af28457dc5f632c965f624b9a
[policy/common.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * policy-endpoints
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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=========================================================
20  */
21
22 package org.onap.policy.common.endpoints.event.comm.bus;
23
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Properties;
29
30 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
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  * UEB Topic Source Factory.
38  */
39 public interface UebTopicSourceFactory {
40
41     /**
42      * Creates an UEB Topic Source based on properties files.
43      * 
44      * @param properties Properties containing initialization values
45      * 
46      * @return an UEB Topic Source
47      * @throws IllegalArgumentException if invalid parameters are present
48      */
49     List<UebTopicSource> build(Properties properties);
50
51     /**
52      * Instantiates a new UEB Topic Source.
53      * 
54      * servers list of servers
55      * topic topic name
56      * apiKey API Key
57      * apiSecret API Secret
58      * consumerGroup Consumer Group
59      * consumerInstance Consumer Instance
60      * fetchTimeout Read Fetch Timeout
61      * fetchLimit Fetch Limit
62      * managed is this source endpoint managed?
63      * @param busTopicParams parameters object
64      * @return an UEB Topic Source
65      * @throws IllegalArgumentException if invalid parameters are present
66      */
67     UebTopicSource build(BusTopicParams busTopicParams);
68
69     /**
70      * Instantiates a new UEB Topic Source.
71      * 
72      * @param servers list of servers
73      * @param topic topic name
74      * @param apiKey API Key
75      * @param apiSecret API Secret
76      * 
77      * @return an UEB Topic Source
78      * @throws IllegalArgumentException if invalid parameters are present
79      */
80     UebTopicSource build(List<String> servers, String topic, String apiKey, String apiSecret);
81
82     /**
83      * Instantiates a new UEB Topic Source.
84      * 
85      * @param servers list of servers
86      * @param topic topic name
87      * 
88      * @return an UEB Topic Source
89      * @throws IllegalArgumentException if invalid parameters are present
90      */
91     UebTopicSource build(List<String> servers, String topic);
92
93     /**
94      * Destroys an UEB Topic Source based on a topic.
95      * 
96      * @param topic topic name
97      * @throws IllegalArgumentException if invalid parameters are present
98      */
99     void destroy(String topic);
100
101     /**
102      * Destroys all UEB Topic Sources.
103      */
104     void destroy();
105
106     /**
107      * Gets an UEB Topic Source based on topic name.
108      * 
109      * @param topic the topic name
110      * @return an UEB Topic Source with topic name
111      * @throws IllegalArgumentException if an invalid topic is provided
112      * @throws IllegalStateException if the UEB Topic Source is an incorrect state
113      */
114     UebTopicSource get(String topic);
115
116     /**
117      * Provides a snapshot of the UEB Topic Sources.
118      * 
119      * @return a list of the UEB Topic Sources
120      */
121     List<UebTopicSource> inventory();
122 }
123
124
125 /* ------------- implementation ----------------- */
126
127 /**
128  * Factory of UEB Source Topics indexed by topic name.
129  */
130 class IndexedUebTopicSourceFactory implements UebTopicSourceFactory {
131     private static final String MISSING_TOPIC = "A topic must be provided";
132
133     /**
134      * Logger.
135      */
136     private static Logger logger = LoggerFactory.getLogger(IndexedUebTopicSourceFactory.class);
137
138     /**
139      * UEB Topic Name Index.
140      */
141     protected HashMap<String, UebTopicSource> uebTopicSources = new HashMap<>();
142
143     /**
144      * {@inheritDoc}
145      */
146     @Override
147     public UebTopicSource build(BusTopicParams busTopicParams) {
148         if (busTopicParams.getServers() == null || busTopicParams.getServers().isEmpty()) {
149             throw new IllegalArgumentException("UEB Server(s) must be provided");
150         }
151
152         if (busTopicParams.getTopic() == null || busTopicParams.getTopic().isEmpty()) {
153             throw new IllegalArgumentException(MISSING_TOPIC);
154         }
155
156         synchronized (this) {
157             if (uebTopicSources.containsKey(busTopicParams.getTopic())) {
158                 return uebTopicSources.get(busTopicParams.getTopic());
159             }
160
161             UebTopicSource uebTopicSource = new SingleThreadedUebTopicSource(busTopicParams);
162
163             if (busTopicParams.isManaged()) {
164                 uebTopicSources.put(busTopicParams.getTopic(), uebTopicSource);
165             }
166
167             return uebTopicSource;
168         }
169     }
170
171     /**
172      * {@inheritDoc}
173      */
174     @Override
175     public List<UebTopicSource> build(Properties properties) {
176
177         String readTopics = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS);
178         if (readTopics == null || readTopics.isEmpty()) {
179             logger.info("{}: no topic for UEB Source", this);
180             return new ArrayList<>();
181         }
182         List<String> readTopicList = new ArrayList<>(Arrays.asList(readTopics.split("\\s*,\\s*")));
183
184         List<UebTopicSource> newUebTopicSources = new ArrayList<>();
185         synchronized (this) {
186             for (String topic : readTopicList) {
187                 if (this.uebTopicSources.containsKey(topic)) {
188                     newUebTopicSources.add(this.uebTopicSources.get(topic));
189                     continue;
190                 }
191
192                 String servers = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "."
193                         + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SERVERS_SUFFIX);
194
195                 if (servers == null || servers.isEmpty()) {
196                     logger.error("{}: no UEB servers configured for sink {}", this, topic);
197                     continue;
198                 }
199
200                 List<String> serverList = new ArrayList<>(Arrays.asList(servers.split("\\s*,\\s*")));
201
202                 String apiKey = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "." + topic
203                         + PolicyEndPointProperties.PROPERTY_TOPIC_API_KEY_SUFFIX);
204
205                 String apiSecret = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "."
206                         + topic + PolicyEndPointProperties.PROPERTY_TOPIC_API_SECRET_SUFFIX);
207
208                 String consumerGroup = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "."
209                         + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_CONSUMER_GROUP_SUFFIX);
210
211                 String consumerInstance = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS
212                         + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_CONSUMER_INSTANCE_SUFFIX);
213
214                 String fetchTimeoutString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS
215                         + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_FETCH_TIMEOUT_SUFFIX);
216                 int fetchTimeout = UebTopicSource.DEFAULT_TIMEOUT_MS_FETCH;
217                 if (fetchTimeoutString != null && !fetchTimeoutString.isEmpty()) {
218                     try {
219                         fetchTimeout = Integer.parseInt(fetchTimeoutString);
220                     } catch (NumberFormatException nfe) {
221                         logger.warn("{}: fetch timeout {} is in invalid format for topic {} ", this, fetchTimeoutString,
222                                 topic);
223                     }
224                 }
225
226                 String fetchLimitString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS
227                         + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_FETCH_LIMIT_SUFFIX);
228                 int fetchLimit = UebTopicSource.DEFAULT_LIMIT_FETCH;
229                 if (fetchLimitString != null && !fetchLimitString.isEmpty()) {
230                     try {
231                         fetchLimit = Integer.parseInt(fetchLimitString);
232                     } catch (NumberFormatException nfe) {
233                         logger.warn("{}: fetch limit {} is in invalid format for topic {} ", this, fetchLimitString,
234                                 topic);
235                     }
236                 }
237
238                 String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "."
239                         + topic + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX);
240                 boolean managed = true;
241                 if (managedString != null && !managedString.isEmpty()) {
242                     managed = Boolean.parseBoolean(managedString);
243                 }
244
245                 String useHttpsString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "."
246                         + topic + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX);
247
248                 // default is to use HTTP if no https property exists
249                 boolean useHttps = false;
250                 if (useHttpsString != null && !useHttpsString.isEmpty()) {
251                     useHttps = Boolean.parseBoolean(useHttpsString);
252                 }
253
254                 String allowSelfSignedCertsString =
255                         properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "." + topic
256                                 + PolicyEndPointProperties.PROPERTY_ALLOW_SELF_SIGNED_CERTIFICATES_SUFFIX);
257
258                 // default is to disallow self-signed certs
259                 boolean allowSelfSignedCerts = false;
260                 if (allowSelfSignedCertsString != null && !allowSelfSignedCertsString.isEmpty()) {
261                     allowSelfSignedCerts = Boolean.parseBoolean(allowSelfSignedCertsString);
262                 }
263
264                 UebTopicSource uebTopicSource = this.build(BusTopicParams.builder()
265                         .servers(serverList)
266                         .topic(topic)
267                         .apiKey(apiKey)
268                         .apiSecret(apiSecret)
269                         .consumerGroup(consumerGroup)
270                         .consumerInstance(consumerInstance)
271                         .fetchTimeout(fetchTimeout)
272                         .fetchLimit(fetchLimit)
273                         .managed(managed)
274                         .useHttps(useHttps)
275                         .allowSelfSignedCerts(allowSelfSignedCerts).build());
276                 newUebTopicSources.add(uebTopicSource);
277             }
278         }
279         return newUebTopicSources;
280     }
281
282     /**
283      * {@inheritDoc}
284      */
285     @Override
286     public UebTopicSource build(List<String> servers, String topic, String apiKey, String apiSecret) {
287
288         return this.build(BusTopicParams.builder()
289                 .servers(servers)
290                 .topic(topic)
291                 .apiKey(apiKey)
292                 .apiSecret(apiSecret)
293                 .fetchTimeout(UebTopicSource.DEFAULT_TIMEOUT_MS_FETCH)
294                 .fetchLimit(UebTopicSource.DEFAULT_LIMIT_FETCH)
295                 .managed(true)
296                 .useHttps(false)
297                 .allowSelfSignedCerts(true).build());
298     }
299
300     /**
301      * {@inheritDoc}
302      */
303     @Override
304     public UebTopicSource build(List<String> servers, String topic) {
305         return this.build(servers, topic, null, null);
306     }
307
308     /**
309      * {@inheritDoc}
310      */
311     @Override
312     public void destroy(String topic) {
313
314         if (topic == null || topic.isEmpty()) {
315             throw new IllegalArgumentException(MISSING_TOPIC);
316         }
317
318         UebTopicSource uebTopicSource;
319
320         synchronized (this) {
321             if (!uebTopicSources.containsKey(topic)) {
322                 return;
323             }
324
325             uebTopicSource = uebTopicSources.remove(topic);
326         }
327
328         uebTopicSource.shutdown();
329     }
330
331     @Override
332     public void destroy() {
333         List<UebTopicSource> readers = this.inventory();
334         for (UebTopicSource reader : readers) {
335             reader.shutdown();
336         }
337
338         synchronized (this) {
339             this.uebTopicSources.clear();
340         }
341     }
342
343     /**
344      * {@inheritDoc}
345      */
346     @Override
347     public UebTopicSource get(String topic) {
348
349         if (topic == null || topic.isEmpty()) {
350             throw new IllegalArgumentException(MISSING_TOPIC);
351         }
352
353         synchronized (this) {
354             if (uebTopicSources.containsKey(topic)) {
355                 return uebTopicSources.get(topic);
356             } else {
357                 throw new IllegalStateException("UebTopiceSource for " + topic + " not found");
358             }
359         }
360     }
361
362     @Override
363     public synchronized List<UebTopicSource> inventory() {
364         return new ArrayList<>(this.uebTopicSources.values());
365     }
366
367     @Override
368     public String toString() {
369         StringBuilder builder = new StringBuilder();
370         builder.append("IndexedUebTopicSourceFactory []");
371         return builder.toString();
372     }
373 }