faa8e342fd89f7d623f7a936a676a6910dcdc3b5
[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.InlineUebTopicSink;
32 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * UEB Topic Sink Factory.
38  */
39 public interface UebTopicSinkFactory {
40
41     /**
42      * Instantiates a new UEB Topic Writer.
43      * 
44      * servers list of servers
45      * topic topic name
46      * apiKey API Key
47      * apiSecret API Secret
48      * partitionKey Consumer Group
49      * managed is this sink endpoint managed?
50      * @param busTopicParams parameter object
51      * @return an UEB Topic Sink
52      * @throws IllegalArgumentException if invalid parameters are present
53      */
54     UebTopicSink build(BusTopicParams busTopicParams);
55
56     /**
57      * Creates an UEB Topic Writer based on properties files.
58      * 
59      * @param properties Properties containing initialization values
60      * 
61      * @return an UEB Topic Writer
62      * @throws IllegalArgumentException if invalid parameters are present
63      */
64     List<UebTopicSink> build(Properties properties);
65
66     /**
67      * Instantiates a new UEB Topic Writer.
68      * 
69      * @param servers list of servers
70      * @param topic topic name
71      * 
72      * @return an UEB Topic Writer
73      * @throws IllegalArgumentException if invalid parameters are present
74      */
75     UebTopicSink build(List<String> servers, String topic);
76
77     /**
78      * Destroys an UEB Topic Writer based on a topic.
79      * 
80      * @param topic topic name
81      * @throws IllegalArgumentException if invalid parameters are present
82      */
83     void destroy(String topic);
84
85     /**
86      * Destroys all UEB Topic Writers.
87      */
88     void destroy();
89
90     /**
91      * gets an UEB Topic Writer based on topic name.
92      * 
93      * @param topic the topic name
94      * 
95      * @return an UEB Topic Writer with topic name
96      * @throws IllegalArgumentException if an invalid topic is provided
97      * @throws IllegalStateException if the UEB Topic Reader is an incorrect state
98      */
99     UebTopicSink get(String topic);
100
101     /**
102      * Provides a snapshot of the UEB Topic Writers.
103      * 
104      * @return a list of the UEB Topic Writers
105      */
106     List<UebTopicSink> inventory();
107 }
108
109
110 /* ------------- implementation ----------------- */
111
112 /**
113  * Factory of UEB Reader Topics indexed by topic name.
114  */
115 class IndexedUebTopicSinkFactory implements UebTopicSinkFactory {
116     private static final String MISSING_TOPIC = "A topic must be provided";
117
118     /**
119      * Logger.
120      */
121     private static Logger logger = LoggerFactory.getLogger(IndexedUebTopicSinkFactory.class);
122
123     /**
124      * UEB Topic Name Index.
125      */
126     protected HashMap<String, UebTopicSink> uebTopicSinks = new HashMap<>();
127
128     @Override
129     public UebTopicSink build(BusTopicParams busTopicParams) {
130
131         if (busTopicParams.getServers() == null || busTopicParams.getServers().isEmpty()) {
132             throw new IllegalArgumentException("UEB Server(s) must be provided");
133         }
134
135         if (busTopicParams.getTopic() == null || busTopicParams.getTopic().isEmpty()) {
136             throw new IllegalArgumentException(MISSING_TOPIC);
137         }
138
139         synchronized (this) {
140             if (uebTopicSinks.containsKey(busTopicParams.getTopic())) {
141                 return uebTopicSinks.get(busTopicParams.getTopic());
142             }
143
144             UebTopicSink uebTopicWriter = new InlineUebTopicSink(busTopicParams);
145
146             if (busTopicParams.isManaged()) {
147                 uebTopicSinks.put(busTopicParams.getTopic(), uebTopicWriter);
148             }
149
150             return uebTopicWriter;
151         }
152     }
153
154
155     @Override
156     public UebTopicSink build(List<String> servers, String topic) {
157         return this.build(BusTopicParams.builder()
158                 .servers(servers)
159                 .topic(topic)
160                 .managed(true)
161                 .useHttps(false)
162                 .allowSelfSignedCerts(false)
163                 .build());
164     }
165
166
167     @Override
168     public List<UebTopicSink> build(Properties properties) {
169
170         String writeTopics = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS);
171         if (writeTopics == null || writeTopics.isEmpty()) {
172             logger.info("{}: no topic for UEB Sink", this);
173             return new ArrayList<>();
174         }
175
176         List<String> writeTopicList = new ArrayList<>(Arrays.asList(writeTopics.split("\\s*,\\s*")));
177         List<UebTopicSink> newUebTopicSinks = new ArrayList<>();
178         synchronized (this) {
179             for (String topic : writeTopicList) {
180                 if (this.uebTopicSinks.containsKey(topic)) {
181                     newUebTopicSinks.add(this.uebTopicSinks.get(topic));
182                     continue;
183                 }
184
185                 String servers = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS + "." + topic
186                         + PolicyEndPointProperties.PROPERTY_TOPIC_SERVERS_SUFFIX);
187                 if (servers == null || servers.isEmpty()) {
188                     logger.error("{}: no UEB servers configured for sink {}", this, topic);
189                     continue;
190                 }
191
192                 List<String> serverList = new ArrayList<>(Arrays.asList(servers.split("\\s*,\\s*")));
193
194                 String apiKey = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS + "." + topic
195                         + PolicyEndPointProperties.PROPERTY_TOPIC_API_KEY_SUFFIX);
196                 String apiSecret = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS + "."
197                         + topic + PolicyEndPointProperties.PROPERTY_TOPIC_API_SECRET_SUFFIX);
198                 String partitionKey = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS + "."
199                         + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SINK_PARTITION_KEY_SUFFIX);
200
201                 String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS + "."
202                         + topic + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX);
203                 boolean managed = true;
204                 if (managedString != null && !managedString.isEmpty()) {
205                     managed = Boolean.parseBoolean(managedString);
206                 }
207
208                 String useHttpsString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS + "."
209                         + topic + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX);
210
211                 // default is to use HTTP if no https property exists
212                 boolean useHttps = false;
213                 if (useHttpsString != null && !useHttpsString.isEmpty()) {
214                     useHttps = Boolean.parseBoolean(useHttpsString);
215                 }
216
217
218                 String allowSelfSignedCertsString =
219                         properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS + "." + topic
220                                 + PolicyEndPointProperties.PROPERTY_ALLOW_SELF_SIGNED_CERTIFICATES_SUFFIX);
221
222                 // default is to disallow self-signed certs
223                 boolean allowSelfSignedCerts = false;
224                 if (allowSelfSignedCertsString != null && !allowSelfSignedCertsString.isEmpty()) {
225                     allowSelfSignedCerts = Boolean.parseBoolean(allowSelfSignedCertsString);
226                 }
227
228                 UebTopicSink uebTopicWriter = this.build(BusTopicParams.builder()
229                         .servers(serverList)
230                         .topic(topic)
231                         .apiKey(apiKey)
232                         .apiSecret(apiSecret)
233                         .partitionId(partitionKey)
234                         .managed(managed)
235                         .useHttps(useHttps)
236                         .allowSelfSignedCerts(allowSelfSignedCerts)
237                         .build());
238                 newUebTopicSinks.add(uebTopicWriter);
239             }
240             return newUebTopicSinks;
241         }
242     }
243
244     @Override
245     public void destroy(String topic) {
246
247         if (topic == null || topic.isEmpty()) {
248             throw new IllegalArgumentException(MISSING_TOPIC);
249         }
250
251         UebTopicSink uebTopicWriter;
252         synchronized (this) {
253             if (!uebTopicSinks.containsKey(topic)) {
254                 return;
255             }
256
257             uebTopicWriter = uebTopicSinks.remove(topic);
258         }
259
260         uebTopicWriter.shutdown();
261     }
262
263     @Override
264     public void destroy() {
265         List<UebTopicSink> writers = this.inventory();
266         for (UebTopicSink writer : writers) {
267             writer.shutdown();
268         }
269
270         synchronized (this) {
271             this.uebTopicSinks.clear();
272         }
273     }
274
275     @Override
276     public UebTopicSink get(String topic) {
277
278         if (topic == null || topic.isEmpty()) {
279             throw new IllegalArgumentException(MISSING_TOPIC);
280         }
281
282         synchronized (this) {
283             if (uebTopicSinks.containsKey(topic)) {
284                 return uebTopicSinks.get(topic);
285             } else {
286                 throw new IllegalStateException("UebTopicSink for " + topic + " not found");
287             }
288         }
289     }
290
291     @Override
292     public synchronized List<UebTopicSink> inventory() {
293         return new ArrayList<>(this.uebTopicSinks.values());
294     }
295
296
297     @Override
298     public String toString() {
299         StringBuilder builder = new StringBuilder();
300         builder.append("IndexedUebTopicSinkFactory []");
301         return builder.toString();
302     }
303
304 }