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