Use re2j pattern for sonar vulnerabilities
[policy/drools-pdp.git] / policy-management / src / main / java / org / onap / policy / drools / controller / IndexedDroolsControllerFactory.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2019-2021 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.drools.controller;
22
23 import com.google.re2j.Pattern;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Properties;
29 import lombok.NonNull;
30 import org.onap.policy.common.endpoints.event.comm.Topic;
31 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
32 import org.onap.policy.common.endpoints.event.comm.TopicSink;
33 import org.onap.policy.common.endpoints.event.comm.TopicSource;
34 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
35 import org.onap.policy.common.utils.services.FeatureApiUtils;
36 import org.onap.policy.drools.controller.internal.MavenDroolsController;
37 import org.onap.policy.drools.controller.internal.NullDroolsController;
38 import org.onap.policy.drools.features.DroolsControllerFeatureApi;
39 import org.onap.policy.drools.features.DroolsControllerFeatureApiConstants;
40 import org.onap.policy.drools.properties.DroolsPropertyConstants;
41 import org.onap.policy.drools.protocol.coders.JsonProtocolFilter;
42 import org.onap.policy.drools.protocol.coders.TopicCoderFilterConfiguration;
43 import org.onap.policy.drools.protocol.coders.TopicCoderFilterConfiguration.CustomGsonCoder;
44 import org.onap.policy.drools.protocol.coders.TopicCoderFilterConfiguration.PotentialCoderFilter;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 /**
49  * Factory of Drools Controllers indexed by the Maven coordinates.
50  */
51 class IndexedDroolsControllerFactory implements DroolsControllerFactory {
52
53     private static final Logger logger = LoggerFactory.getLogger(IndexedDroolsControllerFactory.class);
54     private static final Pattern COMMA_SPACE_PAT = Pattern.compile("\\s*,\\s*");
55
56     /**
57      * Policy Controller Name Index.
58      */
59     protected Map<String, DroolsController> droolsControllers = new HashMap<>();
60
61     /**
62      * Null Drools Controller.
63      */
64     protected NullDroolsController nullDroolsController = new NullDroolsController();
65
66     /**
67      * Constructs the object.
68      */
69     public IndexedDroolsControllerFactory() {
70
71         /* Add a NULL controller which will always be present in the hash */
72
73         DroolsController controller = new NullDroolsController();
74         String controllerId = controller.getGroupId() + ":" + controller.getArtifactId();
75
76         synchronized (this) {
77             droolsControllers.put(controllerId, controller);
78         }
79     }
80
81     @Override
82     public DroolsController build(Properties properties, List<? extends TopicSource> eventSources,
83             List<? extends TopicSink> eventSinks) throws LinkageError {
84
85         String groupId = properties.getProperty(DroolsPropertyConstants.RULES_GROUPID);
86         if (groupId == null || groupId.isEmpty()) {
87             groupId = DroolsControllerConstants.NO_GROUP_ID;
88         }
89
90         String artifactId = properties.getProperty(DroolsPropertyConstants.RULES_ARTIFACTID);
91         if (artifactId == null || artifactId.isEmpty()) {
92             artifactId = DroolsControllerConstants.NO_ARTIFACT_ID;
93         }
94
95         String version = properties.getProperty(DroolsPropertyConstants.RULES_VERSION);
96         if (version == null || version.isEmpty()) {
97             version = DroolsControllerConstants.NO_VERSION;
98         }
99
100         List<TopicCoderFilterConfiguration> topics2DecodedClasses2Filters = codersAndFilters(properties, eventSources);
101         List<TopicCoderFilterConfiguration> topics2EncodedClasses2Filters = codersAndFilters(properties, eventSinks);
102
103         return this.build(properties, groupId, artifactId, version,
104                 topics2DecodedClasses2Filters, topics2EncodedClasses2Filters);
105     }
106
107     @Override
108     public DroolsController build(Properties properties, String newGroupId, String newArtifactId, String newVersion,
109             List<TopicCoderFilterConfiguration> decoderConfigurations,
110             List<TopicCoderFilterConfiguration> encoderConfigurations) throws LinkageError {
111
112         if (newGroupId == null || newGroupId.isEmpty()) {
113             throw new IllegalArgumentException("Missing maven group-id coordinate");
114         }
115
116         if (newArtifactId == null || newArtifactId.isEmpty()) {
117             throw new IllegalArgumentException("Missing maven artifact-id coordinate");
118         }
119
120         if (newVersion == null || newVersion.isEmpty()) {
121             throw new IllegalArgumentException("Missing maven version coordinate");
122         }
123
124         String controllerId = newGroupId + ":" + newArtifactId;
125         DroolsController controllerCopy = null;
126         synchronized (this) {
127             /*
128              * The Null Drools Controller for no maven coordinates is always here so when no
129              * coordinates present, this is the return point
130              *
131              * assert (controllerCopy instanceof NullDroolsController)
132              */
133             if (droolsControllers.containsKey(controllerId)) {
134                 controllerCopy = droolsControllers.get(controllerId);
135                 if (controllerCopy.getVersion().equalsIgnoreCase(newVersion)) {
136                     return controllerCopy;
137                 }
138             }
139         }
140
141         if (controllerCopy != null) {
142             /*
143              * a controller keyed by group id + artifact id exists but with different version =>
144              * version upgrade/downgrade
145              */
146
147             controllerCopy.updateToVersion(newGroupId, newArtifactId, newVersion, decoderConfigurations,
148                     encoderConfigurations);
149
150             return controllerCopy;
151         }
152
153         /* new drools controller */
154
155         DroolsController controller = applyBeforeInstance(properties, newGroupId, newArtifactId, newVersion,
156                         decoderConfigurations, encoderConfigurations);
157
158         if (controller == null) {
159             controller = new MavenDroolsController(newGroupId, newArtifactId, newVersion, decoderConfigurations,
160                     encoderConfigurations);
161         }
162
163         synchronized (this) {
164             droolsControllers.put(controllerId, controller);
165         }
166
167         final DroolsController controllerFinal = controller;
168
169         FeatureApiUtils.apply(getProviders(),
170             feature -> feature.afterInstance(controllerFinal, properties),
171             (feature, ex) -> logger.error("feature {} ({}) afterInstance() of drools controller {}:{}:{} failed",
172                             feature.getName(), feature.getSequenceNumber(),
173                             newGroupId, newArtifactId, newVersion, ex));
174
175         return controller;
176     }
177
178     private DroolsController applyBeforeInstance(Properties properties, String newGroupId, String newArtifactId,
179                     String newVersion, List<TopicCoderFilterConfiguration> decoderConfigurations,
180                     List<TopicCoderFilterConfiguration> encoderConfigurations) {
181         DroolsController controller = null;
182         for (DroolsControllerFeatureApi feature: getProviders()) {
183             try {
184                 controller = feature.beforeInstance(properties,
185                         newGroupId, newArtifactId, newVersion,
186                         decoderConfigurations, encoderConfigurations);
187                 if (controller != null) {
188                     logger.info("feature {} ({}) beforeInstance() has intercepted drools controller {}:{}:{}",
189                             feature.getName(), feature.getSequenceNumber(),
190                             newGroupId, newArtifactId, newVersion);
191                     break;
192                 }
193             } catch (RuntimeException r) {
194                 logger.error("feature {} ({}) beforeInstance() of drools controller {}:{}:{} failed",
195                         feature.getName(), feature.getSequenceNumber(),
196                         newGroupId, newArtifactId, newVersion, r);
197             }
198         }
199         return controller;
200     }
201
202     protected List<DroolsControllerFeatureApi> getProviders() {
203         return DroolsControllerFeatureApiConstants.getProviders().getList();
204     }
205
206     /**
207      * find out decoder classes and filters.
208      *
209      * @param properties properties with information about decoders
210      * @param topicEntities topic sources
211      * @return list of topics, each with associated decoder classes, each with a list of associated
212      *         filters
213      * @throws IllegalArgumentException invalid input data
214      */
215     protected List<TopicCoderFilterConfiguration> codersAndFilters(Properties properties,
216             List<? extends Topic> topicEntities) {
217
218         List<TopicCoderFilterConfiguration> topics2DecodedClasses2Filters = new ArrayList<>();
219
220         if (topicEntities == null || topicEntities.isEmpty()) {
221             return topics2DecodedClasses2Filters;
222         }
223
224         for (Topic topic : topicEntities) {
225
226             // 1. first the topic
227
228             String firstTopic = topic.getTopic();
229
230             String propertyTopicEntityPrefix = getPropertyTopicPrefix(topic) + firstTopic;
231
232             // 2. check if there is a custom decoder for this topic that the user prefers to use
233             // instead of the ones provided in the platform
234
235             CustomGsonCoder customGsonCoder = getCustomCoder(properties, propertyTopicEntityPrefix);
236
237             // 3. second the list of classes associated with each topic
238
239             String eventClasses = properties
240                     .getProperty(propertyTopicEntityPrefix + PolicyEndPointProperties.PROPERTY_TOPIC_EVENTS_SUFFIX);
241
242             if (eventClasses == null || eventClasses.isEmpty()) {
243                 logger.warn("There are no event classes for topic {}", firstTopic);
244                 continue;
245             }
246
247             List<PotentialCoderFilter> classes2Filters =
248                             getFilterExpressions(properties, propertyTopicEntityPrefix, eventClasses);
249
250             TopicCoderFilterConfiguration topic2Classes2Filters =
251                     new TopicCoderFilterConfiguration(firstTopic, classes2Filters, customGsonCoder);
252             topics2DecodedClasses2Filters.add(topic2Classes2Filters);
253         }
254
255         return topics2DecodedClasses2Filters;
256     }
257
258     private String getPropertyTopicPrefix(Topic topic) {
259         boolean isSource = topic instanceof TopicSource;
260         CommInfrastructure commInfra = topic.getTopicCommInfrastructure();
261         if (commInfra == CommInfrastructure.UEB) {
262             if (isSource) {
263                 return PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS + ".";
264             } else {
265                 return PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS + ".";
266             }
267         } else if (commInfra == CommInfrastructure.DMAAP) {
268             if (isSource) {
269                 return PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + ".";
270             } else {
271                 return PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + ".";
272             }
273         } else if (commInfra == CommInfrastructure.NOOP) {
274             if (isSource) {
275                 return PolicyEndPointProperties.PROPERTY_NOOP_SOURCE_TOPICS + ".";
276             } else {
277                 return PolicyEndPointProperties.PROPERTY_NOOP_SINK_TOPICS + ".";
278             }
279         } else {
280             throw new IllegalArgumentException("Invalid Communication Infrastructure: " + commInfra);
281         }
282     }
283
284     private CustomGsonCoder getCustomCoder(Properties properties, String propertyPrefix) {
285         String customGson = properties.getProperty(propertyPrefix
286                 + PolicyEndPointProperties.PROPERTY_TOPIC_EVENTS_CUSTOM_MODEL_CODER_GSON_SUFFIX);
287
288         CustomGsonCoder customGsonCoder = null;
289         if (customGson != null && !customGson.isEmpty()) {
290             try {
291                 customGsonCoder = new CustomGsonCoder(customGson);
292             } catch (IllegalArgumentException e) {
293                 logger.warn("{}: cannot create custom-gson-coder {} because of {}", this, customGson,
294                         e.getMessage(), e);
295             }
296         }
297         return customGsonCoder;
298     }
299
300     private List<PotentialCoderFilter> getFilterExpressions(Properties properties, String propertyPrefix,
301                     @NonNull String eventClasses) {
302
303         List<PotentialCoderFilter> classes2Filters = new ArrayList<>();
304         for (String theClass : COMMA_SPACE_PAT.split(eventClasses)) {
305
306             // 4. for each coder class, get the filter expression
307
308             String filter = properties
309                     .getProperty(propertyPrefix
310                             + PolicyEndPointProperties.PROPERTY_TOPIC_EVENTS_SUFFIX
311                             + "." + theClass + PolicyEndPointProperties.PROPERTY_TOPIC_EVENTS_FILTER_SUFFIX);
312
313             JsonProtocolFilter protocolFilter = new JsonProtocolFilter(filter);
314             PotentialCoderFilter class2Filters = new PotentialCoderFilter(theClass, protocolFilter);
315             classes2Filters.add(class2Filters);
316         }
317
318         return classes2Filters;
319     }
320
321     @Override
322     public void destroy(DroolsController controller) {
323         unmanage(controller);
324         controller.halt();
325     }
326
327     @Override
328     public void destroy() {
329         List<DroolsController> controllers = this.inventory();
330         for (DroolsController controller : controllers) {
331             controller.halt();
332         }
333
334         synchronized (this) {
335             this.droolsControllers.clear();
336         }
337     }
338
339     /**
340      * unmanage the drools controller.
341      *
342      * @param controller the controller
343      */
344     protected void unmanage(DroolsController controller) {
345         if (controller == null) {
346             throw new IllegalArgumentException("No controller provided");
347         }
348
349         if (!controller.isBrained()) {
350             logger.info("Drools Controller is NOT OPERATIONAL - nothing to destroy");
351             return;
352         }
353
354         String controllerId = controller.getGroupId() + ":" + controller.getArtifactId();
355         synchronized (this) {
356             if (!this.droolsControllers.containsKey(controllerId)) {
357                 return;
358             }
359
360             droolsControllers.remove(controllerId);
361         }
362     }
363
364     @Override
365     public void shutdown(DroolsController controller) {
366         this.unmanage(controller);
367         controller.shutdown();
368     }
369
370     @Override
371     public void shutdown() {
372         List<DroolsController> controllers = this.inventory();
373         for (DroolsController controller : controllers) {
374             controller.shutdown();
375         }
376
377         synchronized (this) {
378             this.droolsControllers.clear();
379         }
380     }
381
382     @Override
383     public DroolsController get(String groupId, String artifactId, String version) {
384
385         if (groupId == null || artifactId == null || groupId.isEmpty() || artifactId.isEmpty()) {
386             throw new IllegalArgumentException("Missing maven coordinates: " + groupId + ":" + artifactId);
387         }
388
389         String controllerId = groupId + ":" + artifactId;
390
391         synchronized (this) {
392             if (this.droolsControllers.containsKey(controllerId)) {
393                 return droolsControllers.get(controllerId);
394             } else {
395                 throw new IllegalStateException("DroolController for " + controllerId + " not found");
396             }
397         }
398     }
399
400     @Override
401     public List<DroolsController> inventory() {
402         return new ArrayList<>(this.droolsControllers.values());
403     }
404
405     @Override
406     public String toString() {
407         StringBuilder builder = new StringBuilder();
408         builder.append("IndexedDroolsControllerFactory [#droolsControllers=").append(droolsControllers.size())
409                 .append("]");
410         return builder.toString();
411     }
412
413 }