62437823f12806b103e93cca914db5946e192de0
[policy/common.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2017-2019 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;
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.internal.BusTopicParams;
30 import org.onap.policy.common.endpoints.event.comm.bus.internal.InlineUebTopicSink;
31 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * Factory of UEB Reader Topics indexed by topic name.
37  */
38 class IndexedUebTopicSinkFactory implements UebTopicSinkFactory {
39     private static final String MISSING_TOPIC = "A topic must be provided";
40
41     /**
42      * Logger.
43      */
44     private static Logger logger = LoggerFactory.getLogger(IndexedUebTopicSinkFactory.class);
45
46     /**
47      * UEB Topic Name Index.
48      */
49     protected HashMap<String, UebTopicSink> uebTopicSinks = new HashMap<>();
50
51     @Override
52     public UebTopicSink build(BusTopicParams busTopicParams) {
53
54         if (busTopicParams.getServers() == null || busTopicParams.getServers().isEmpty()) {
55             throw new IllegalArgumentException("UEB Server(s) must be provided");
56         }
57
58         if (busTopicParams.getTopic() == null || busTopicParams.getTopic().isEmpty()) {
59             throw new IllegalArgumentException(MISSING_TOPIC);
60         }
61
62         synchronized (this) {
63             if (uebTopicSinks.containsKey(busTopicParams.getTopic())) {
64                 return uebTopicSinks.get(busTopicParams.getTopic());
65             }
66
67             UebTopicSink uebTopicWriter = makeSink(busTopicParams);
68
69             if (busTopicParams.isManaged()) {
70                 uebTopicSinks.put(busTopicParams.getTopic(), uebTopicWriter);
71             }
72
73             return uebTopicWriter;
74         }
75     }
76
77
78     @Override
79     public UebTopicSink build(List<String> servers, String topic) {
80         return this.build(BusTopicParams.builder()
81                 .servers(servers)
82                 .topic(topic)
83                 .managed(true)
84                 .useHttps(false)
85                 .allowSelfSignedCerts(false)
86                 .build());
87     }
88
89
90     @Override
91     public List<UebTopicSink> build(Properties properties) {
92
93         String writeTopics = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS);
94         if (writeTopics == null || writeTopics.isEmpty()) {
95             logger.info("{}: no topic for UEB Sink", this);
96             return new ArrayList<>();
97         }
98
99         List<String> writeTopicList = new ArrayList<>(Arrays.asList(writeTopics.split("\\s*,\\s*")));
100         List<UebTopicSink> newUebTopicSinks = new ArrayList<>();
101         synchronized (this) {
102             for (String topic : writeTopicList) {
103                 if (this.uebTopicSinks.containsKey(topic)) {
104                     newUebTopicSinks.add(this.uebTopicSinks.get(topic));
105                     continue;
106                 }
107
108                 String servers = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS + "." + topic
109                         + PolicyEndPointProperties.PROPERTY_TOPIC_SERVERS_SUFFIX);
110                 if (servers == null || servers.isEmpty()) {
111                     logger.error("{}: no UEB servers configured for sink {}", this, topic);
112                     continue;
113                 }
114
115                 final List<String> serverList = new ArrayList<>(Arrays.asList(servers.split("\\s*,\\s*")));
116
117                 final String effectiveTopic = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS
118                                 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_EFFECTIVE_TOPIC_SUFFIX, topic);
119                 final String apiKey = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS
120                                 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_API_KEY_SUFFIX);
121                 final String apiSecret = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS 
122                                 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_API_SECRET_SUFFIX);
123                 final String partitionKey = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS 
124                                 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SINK_PARTITION_KEY_SUFFIX);
125
126                 String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS + "."
127                         + topic + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX);
128                 boolean managed = true;
129                 if (managedString != null && !managedString.isEmpty()) {
130                     managed = Boolean.parseBoolean(managedString);
131                 }
132
133                 String useHttpsString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS + "."
134                         + topic + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX);
135
136                 // default is to use HTTP if no https property exists
137                 boolean useHttps = false;
138                 if (useHttpsString != null && !useHttpsString.isEmpty()) {
139                     useHttps = Boolean.parseBoolean(useHttpsString);
140                 }
141
142
143                 String allowSelfSignedCertsString =
144                         properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS + "." + topic
145                                 + PolicyEndPointProperties.PROPERTY_ALLOW_SELF_SIGNED_CERTIFICATES_SUFFIX);
146
147                 // default is to disallow self-signed certs
148                 boolean allowSelfSignedCerts = false;
149                 if (allowSelfSignedCertsString != null && !allowSelfSignedCertsString.isEmpty()) {
150                     allowSelfSignedCerts = Boolean.parseBoolean(allowSelfSignedCertsString);
151                 }
152
153                 UebTopicSink uebTopicWriter = this.build(BusTopicParams.builder()
154                         .servers(serverList)
155                         .topic(topic)
156                         .effectiveTopic(effectiveTopic)
157                         .apiKey(apiKey)
158                         .apiSecret(apiSecret)
159                         .partitionId(partitionKey)
160                         .managed(managed)
161                         .useHttps(useHttps)
162                         .allowSelfSignedCerts(allowSelfSignedCerts)
163                         .build());
164                 newUebTopicSinks.add(uebTopicWriter);
165             }
166             return newUebTopicSinks;
167         }
168     }
169
170     @Override
171     public void destroy(String topic) {
172
173         if (topic == null || topic.isEmpty()) {
174             throw new IllegalArgumentException(MISSING_TOPIC);
175         }
176
177         UebTopicSink uebTopicWriter;
178         synchronized (this) {
179             if (!uebTopicSinks.containsKey(topic)) {
180                 return;
181             }
182
183             uebTopicWriter = uebTopicSinks.remove(topic);
184         }
185
186         uebTopicWriter.shutdown();
187     }
188
189     @Override
190     public void destroy() {
191         List<UebTopicSink> writers = this.inventory();
192         for (UebTopicSink writer : writers) {
193             writer.shutdown();
194         }
195
196         synchronized (this) {
197             this.uebTopicSinks.clear();
198         }
199     }
200
201     @Override
202     public UebTopicSink get(String topic) {
203
204         if (topic == null || topic.isEmpty()) {
205             throw new IllegalArgumentException(MISSING_TOPIC);
206         }
207
208         synchronized (this) {
209             if (uebTopicSinks.containsKey(topic)) {
210                 return uebTopicSinks.get(topic);
211             } else {
212                 throw new IllegalStateException("UebTopicSink for " + topic + " not found");
213             }
214         }
215     }
216
217     @Override
218     public synchronized List<UebTopicSink> inventory() {
219         return new ArrayList<>(this.uebTopicSinks.values());
220     }
221
222     /**
223      * Makes a new sink.
224      * 
225      * @param busTopicParams parameters to use to configure the sink
226      * @return a new sink
227      */
228     protected UebTopicSink makeSink(BusTopicParams busTopicParams) {
229         return new InlineUebTopicSink(busTopicParams);
230     }
231
232
233     @Override
234     public String toString() {
235         StringBuilder builder = new StringBuilder();
236         builder.append("IndexedUebTopicSinkFactory []");
237         return builder.toString();
238     }
239
240 }