f3f3e1565ca65a20226673410749f7a8317bd7bb
[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     @Override
134     public UebTopicSource build(BusTopicParams busTopicParams) {
135         if (busTopicParams.getServers() == null || busTopicParams.getServers().isEmpty()) {
136             throw new IllegalArgumentException("UEB Server(s) must be provided");
137         }
138
139         if (busTopicParams.getTopic() == null || busTopicParams.getTopic().isEmpty()) {
140             throw new IllegalArgumentException(MISSING_TOPIC);
141         }
142
143         synchronized (this) {
144             if (uebTopicSources.containsKey(busTopicParams.getTopic())) {
145                 return uebTopicSources.get(busTopicParams.getTopic());
146             }
147
148             UebTopicSource uebTopicSource = new SingleThreadedUebTopicSource(busTopicParams);
149
150             if (busTopicParams.isManaged()) {
151                 uebTopicSources.put(busTopicParams.getTopic(), uebTopicSource);
152             }
153
154             return uebTopicSource;
155         }
156     }
157
158     @Override
159     public List<UebTopicSource> build(Properties properties) {
160
161         String readTopics = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS);
162         if (readTopics == null || readTopics.isEmpty()) {
163             logger.info("{}: no topic for UEB Source", this);
164             return new ArrayList<>();
165         }
166         List<String> readTopicList = new ArrayList<>(Arrays.asList(readTopics.split("\\s*,\\s*")));
167
168         List<UebTopicSource> newUebTopicSources = new ArrayList<>();
169         synchronized (this) {
170             for (String topic : readTopicList) {
171                 if (this.uebTopicSources.containsKey(topic)) {
172                     newUebTopicSources.add(this.uebTopicSources.get(topic));
173                     continue;
174                 }
175
176                 String servers = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "."
177                         + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SERVERS_SUFFIX);
178
179                 if (servers == null || servers.isEmpty()) {
180                     logger.error("{}: no UEB servers configured for sink {}", this, topic);
181                     continue;
182                 }
183
184                 final List<String> serverList = new ArrayList<>(Arrays.asList(servers.split("\\s*,\\s*")));
185
186                 final String apiKey = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS 
187                         + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_API_KEY_SUFFIX);
188
189                 final String apiSecret = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS 
190                         + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_API_SECRET_SUFFIX);
191
192                 final String consumerGroup = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS 
193                         + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_CONSUMER_GROUP_SUFFIX);
194
195                 final String consumerInstance = properties.getProperty(
196                         PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS
197                         + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_CONSUMER_INSTANCE_SUFFIX);
198
199                 String fetchTimeoutString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS
200                         + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_FETCH_TIMEOUT_SUFFIX);
201                 int fetchTimeout = UebTopicSource.DEFAULT_TIMEOUT_MS_FETCH;
202                 if (fetchTimeoutString != null && !fetchTimeoutString.isEmpty()) {
203                     try {
204                         fetchTimeout = Integer.parseInt(fetchTimeoutString);
205                     } catch (NumberFormatException nfe) {
206                         logger.warn("{}: fetch timeout {} is in invalid format for topic {} ", this, fetchTimeoutString,
207                                 topic);
208                     }
209                 }
210
211                 String fetchLimitString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS
212                         + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_FETCH_LIMIT_SUFFIX);
213                 int fetchLimit = UebTopicSource.DEFAULT_LIMIT_FETCH;
214                 if (fetchLimitString != null && !fetchLimitString.isEmpty()) {
215                     try {
216                         fetchLimit = Integer.parseInt(fetchLimitString);
217                     } catch (NumberFormatException nfe) {
218                         logger.warn("{}: fetch limit {} is in invalid format for topic {} ", this, fetchLimitString,
219                                 topic);
220                     }
221                 }
222
223                 String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "."
224                         + topic + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX);
225                 boolean managed = true;
226                 if (managedString != null && !managedString.isEmpty()) {
227                     managed = Boolean.parseBoolean(managedString);
228                 }
229
230                 String useHttpsString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "."
231                         + topic + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX);
232
233                 // default is to use HTTP if no https property exists
234                 boolean useHttps = false;
235                 if (useHttpsString != null && !useHttpsString.isEmpty()) {
236                     useHttps = Boolean.parseBoolean(useHttpsString);
237                 }
238
239                 String allowSelfSignedCertsString =
240                         properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + "." + topic
241                                 + PolicyEndPointProperties.PROPERTY_ALLOW_SELF_SIGNED_CERTIFICATES_SUFFIX);
242
243                 // default is to disallow self-signed certs
244                 boolean allowSelfSignedCerts = false;
245                 if (allowSelfSignedCertsString != null && !allowSelfSignedCertsString.isEmpty()) {
246                     allowSelfSignedCerts = Boolean.parseBoolean(allowSelfSignedCertsString);
247                 }
248
249                 UebTopicSource uebTopicSource = this.build(BusTopicParams.builder()
250                         .servers(serverList)
251                         .topic(topic)
252                         .apiKey(apiKey)
253                         .apiSecret(apiSecret)
254                         .consumerGroup(consumerGroup)
255                         .consumerInstance(consumerInstance)
256                         .fetchTimeout(fetchTimeout)
257                         .fetchLimit(fetchLimit)
258                         .managed(managed)
259                         .useHttps(useHttps)
260                         .allowSelfSignedCerts(allowSelfSignedCerts).build());
261                 newUebTopicSources.add(uebTopicSource);
262             }
263         }
264         return newUebTopicSources;
265     }
266
267     @Override
268     public UebTopicSource build(List<String> servers, String topic, String apiKey, String apiSecret) {
269
270         return this.build(BusTopicParams.builder()
271                 .servers(servers)
272                 .topic(topic)
273                 .apiKey(apiKey)
274                 .apiSecret(apiSecret)
275                 .fetchTimeout(UebTopicSource.DEFAULT_TIMEOUT_MS_FETCH)
276                 .fetchLimit(UebTopicSource.DEFAULT_LIMIT_FETCH)
277                 .managed(true)
278                 .useHttps(false)
279                 .allowSelfSignedCerts(true).build());
280     }
281
282     @Override
283     public UebTopicSource build(List<String> servers, String topic) {
284         return this.build(servers, topic, null, null);
285     }
286
287     @Override
288     public void destroy(String topic) {
289
290         if (topic == null || topic.isEmpty()) {
291             throw new IllegalArgumentException(MISSING_TOPIC);
292         }
293
294         UebTopicSource uebTopicSource;
295
296         synchronized (this) {
297             if (!uebTopicSources.containsKey(topic)) {
298                 return;
299             }
300
301             uebTopicSource = uebTopicSources.remove(topic);
302         }
303
304         uebTopicSource.shutdown();
305     }
306
307     @Override
308     public void destroy() {
309         List<UebTopicSource> readers = this.inventory();
310         for (UebTopicSource reader : readers) {
311             reader.shutdown();
312         }
313
314         synchronized (this) {
315             this.uebTopicSources.clear();
316         }
317     }
318
319     @Override
320     public UebTopicSource get(String topic) {
321
322         if (topic == null || topic.isEmpty()) {
323             throw new IllegalArgumentException(MISSING_TOPIC);
324         }
325
326         synchronized (this) {
327             if (uebTopicSources.containsKey(topic)) {
328                 return uebTopicSources.get(topic);
329             } else {
330                 throw new IllegalStateException("UebTopiceSource for " + topic + " not found");
331             }
332         }
333     }
334
335     @Override
336     public synchronized List<UebTopicSource> inventory() {
337         return new ArrayList<>(this.uebTopicSources.values());
338     }
339
340     @Override
341     public String toString() {
342         StringBuilder builder = new StringBuilder();
343         builder.append("IndexedUebTopicSourceFactory []");
344         return builder.toString();
345     }
346 }