5b3fc66970c033523eabe77f1945304ec3ad6272
[policy/common.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine - Common Modules
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 apiKey = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS 
118                                 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_API_KEY_SUFFIX);
119                 final String apiSecret = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS 
120                                 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_API_SECRET_SUFFIX);
121                 final String partitionKey = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS 
122                                 + "." + topic + PolicyEndPointProperties.PROPERTY_TOPIC_SINK_PARTITION_KEY_SUFFIX);
123
124                 String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS + "."
125                         + topic + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX);
126                 boolean managed = true;
127                 if (managedString != null && !managedString.isEmpty()) {
128                     managed = Boolean.parseBoolean(managedString);
129                 }
130
131                 String useHttpsString = properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS + "."
132                         + topic + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX);
133
134                 // default is to use HTTP if no https property exists
135                 boolean useHttps = false;
136                 if (useHttpsString != null && !useHttpsString.isEmpty()) {
137                     useHttps = Boolean.parseBoolean(useHttpsString);
138                 }
139
140
141                 String allowSelfSignedCertsString =
142                         properties.getProperty(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS + "." + topic
143                                 + PolicyEndPointProperties.PROPERTY_ALLOW_SELF_SIGNED_CERTIFICATES_SUFFIX);
144
145                 // default is to disallow self-signed certs
146                 boolean allowSelfSignedCerts = false;
147                 if (allowSelfSignedCertsString != null && !allowSelfSignedCertsString.isEmpty()) {
148                     allowSelfSignedCerts = Boolean.parseBoolean(allowSelfSignedCertsString);
149                 }
150
151                 UebTopicSink uebTopicWriter = this.build(BusTopicParams.builder()
152                         .servers(serverList)
153                         .topic(topic)
154                         .apiKey(apiKey)
155                         .apiSecret(apiSecret)
156                         .partitionId(partitionKey)
157                         .managed(managed)
158                         .useHttps(useHttps)
159                         .allowSelfSignedCerts(allowSelfSignedCerts)
160                         .build());
161                 newUebTopicSinks.add(uebTopicWriter);
162             }
163             return newUebTopicSinks;
164         }
165     }
166
167     @Override
168     public void destroy(String topic) {
169
170         if (topic == null || topic.isEmpty()) {
171             throw new IllegalArgumentException(MISSING_TOPIC);
172         }
173
174         UebTopicSink uebTopicWriter;
175         synchronized (this) {
176             if (!uebTopicSinks.containsKey(topic)) {
177                 return;
178             }
179
180             uebTopicWriter = uebTopicSinks.remove(topic);
181         }
182
183         uebTopicWriter.shutdown();
184     }
185
186     @Override
187     public void destroy() {
188         List<UebTopicSink> writers = this.inventory();
189         for (UebTopicSink writer : writers) {
190             writer.shutdown();
191         }
192
193         synchronized (this) {
194             this.uebTopicSinks.clear();
195         }
196     }
197
198     @Override
199     public UebTopicSink get(String topic) {
200
201         if (topic == null || topic.isEmpty()) {
202             throw new IllegalArgumentException(MISSING_TOPIC);
203         }
204
205         synchronized (this) {
206             if (uebTopicSinks.containsKey(topic)) {
207                 return uebTopicSinks.get(topic);
208             } else {
209                 throw new IllegalStateException("UebTopicSink for " + topic + " not found");
210             }
211         }
212     }
213
214     @Override
215     public synchronized List<UebTopicSink> inventory() {
216         return new ArrayList<>(this.uebTopicSinks.values());
217     }
218
219     /**
220      * Makes a new sink.
221      * 
222      * @param busTopicParams parameters to use to configure the sink
223      * @return a new sink
224      */
225     protected UebTopicSink makeSink(BusTopicParams busTopicParams) {
226         return new InlineUebTopicSink(busTopicParams);
227     }
228
229
230     @Override
231     public String toString() {
232         StringBuilder builder = new StringBuilder();
233         builder.append("IndexedUebTopicSinkFactory []");
234         return builder.toString();
235     }
236
237 }