aboutsummaryrefslogtreecommitdiffstats
path: root/policy-management/src/main/java/org/onap/policy/drools/controller/IndexedDroolsControllerFactory.java
blob: 7f28f9bbd41824121122438e2f08f2fb74825245 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
 * ============LICENSE_START=======================================================
 * ONAP
 * ================================================================================
 * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
 * Modifications Copyright (C) 2023-2024 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.drools.controller;

import com.google.re2j.Pattern;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import lombok.NonNull;
import org.onap.policy.common.endpoints.event.comm.Topic;
import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
import org.onap.policy.common.endpoints.event.comm.TopicSink;
import org.onap.policy.common.endpoints.event.comm.TopicSource;
import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
import org.onap.policy.common.utils.services.FeatureApiUtils;
import org.onap.policy.drools.controller.internal.MavenDroolsController;
import org.onap.policy.drools.controller.internal.NullDroolsController;
import org.onap.policy.drools.features.DroolsControllerFeatureApi;
import org.onap.policy.drools.features.DroolsControllerFeatureApiConstants;
import org.onap.policy.drools.properties.DroolsPropertyConstants;
import org.onap.policy.drools.protocol.coders.JsonProtocolFilter;
import org.onap.policy.drools.protocol.coders.TopicCoderFilterConfiguration;
import org.onap.policy.drools.protocol.coders.TopicCoderFilterConfiguration.CustomGsonCoder;
import org.onap.policy.drools.protocol.coders.TopicCoderFilterConfiguration.PotentialCoderFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Factory of Drools Controllers indexed by the Maven coordinates.
 */
class IndexedDroolsControllerFactory implements DroolsControllerFactory {

    private static final Logger logger = LoggerFactory.getLogger(IndexedDroolsControllerFactory.class);
    private static final Pattern COMMA_SPACE_PAT = Pattern.compile("\\s*,\\s*");

    /**
     * Policy Controller Name Index.
     */
    protected Map<String, DroolsController> droolsControllers = new HashMap<>();

    /**
     * Null Drools Controller.
     */
    protected NullDroolsController nullDroolsController = new NullDroolsController();

    /**
     * Constructs the object.
     */
    public IndexedDroolsControllerFactory() {

        /* Add a NULL controller which will always be present in the hash */

        DroolsController controller = new NullDroolsController();
        String controllerId = controller.getGroupId() + ":" + controller.getArtifactId();

        synchronized (this) {
            droolsControllers.put(controllerId, controller);
        }
    }

    @Override
    public DroolsController build(Properties properties, List<? extends TopicSource> eventSources,
            List<? extends TopicSink> eventSinks) throws LinkageError {

        String groupId = properties.getProperty(DroolsPropertyConstants.RULES_GROUPID);
        if (groupId == null || groupId.isEmpty()) {
            groupId = DroolsControllerConstants.NO_GROUP_ID;
        }

        String artifactId = properties.getProperty(DroolsPropertyConstants.RULES_ARTIFACTID);
        if (artifactId == null || artifactId.isEmpty()) {
            artifactId = DroolsControllerConstants.NO_ARTIFACT_ID;
        }

        String version = properties.getProperty(DroolsPropertyConstants.RULES_VERSION);
        if (version == null || version.isEmpty()) {
            version = DroolsControllerConstants.NO_VERSION;
        }

        List<TopicCoderFilterConfiguration> topics2DecodedClasses2Filters = codersAndFilters(properties, eventSources);
        List<TopicCoderFilterConfiguration> topics2EncodedClasses2Filters = codersAndFilters(properties, eventSinks);

        return this.build(properties, groupId, artifactId, version,
                topics2DecodedClasses2Filters, topics2EncodedClasses2Filters);
    }

    @Override
    public DroolsController build(Properties properties, String newGroupId, String newArtifactId, String newVersion,
            List<TopicCoderFilterConfiguration> decoderConfigurations,
            List<TopicCoderFilterConfiguration> encoderConfigurations) throws LinkageError {

        if (newGroupId == null || newGroupId.isEmpty()) {
            throw new IllegalArgumentException("Missing maven group-id coordinate");
        }

        if (newArtifactId == null || newArtifactId.isEmpty()) {
            throw new IllegalArgumentException("Missing maven artifact-id coordinate");
        }

        if (newVersion == null || newVersion.isEmpty()) {
            throw new IllegalArgumentException("Missing maven version coordinate");
        }

        String controllerId = newGroupId + ":" + newArtifactId;
        DroolsController controllerCopy = null;
        synchronized (this) {
            /*
             * The Null Drools Controller for no maven coordinates is always here so when no
             * coordinates present, this is the return point
             *
             * assert (controllerCopy instanceof NullDroolsController)
             */
            if (droolsControllers.containsKey(controllerId)) {
                controllerCopy = droolsControllers.get(controllerId);
                if (controllerCopy.getVersion().equalsIgnoreCase(newVersion)) {
                    return controllerCopy;
                }
            }
        }

        if (controllerCopy != null) {
            /*
             * a controller keyed by group id + artifact id exists but with different version =>
             * version upgrade/downgrade
             */

            controllerCopy.updateToVersion(newGroupId, newArtifactId, newVersion, decoderConfigurations,
                    encoderConfigurations);

            return controllerCopy;
        }

        /* new drools controller */

        DroolsController controller = applyBeforeInstance(properties, newGroupId, newArtifactId, newVersion,
                        decoderConfigurations, encoderConfigurations);

        if (controller == null) {
            controller = new MavenDroolsController(newGroupId, newArtifactId, newVersion, decoderConfigurations,
                    encoderConfigurations);
        }

        synchronized (this) {
            droolsControllers.put(controllerId, controller);
        }

        final DroolsController controllerFinal = controller;

        FeatureApiUtils.apply(getProviders(),
            feature -> feature.afterInstance(controllerFinal, properties),
            (feature, ex) -> logger.error("feature {} ({}) afterInstance() of drools controller {}:{}:{} failed",
                            feature.getName(), feature.getSequenceNumber(),
                            newGroupId, newArtifactId, newVersion, ex));

        return controller;
    }

    private DroolsController applyBeforeInstance(Properties properties, String newGroupId, String newArtifactId,
                    String newVersion, List<TopicCoderFilterConfiguration> decoderConfigurations,
                    List<TopicCoderFilterConfiguration> encoderConfigurations) {
        DroolsController controller = null;
        for (DroolsControllerFeatureApi feature: getProviders()) {
            try {
                controller = feature.beforeInstance(properties,
                        newGroupId, newArtifactId, newVersion,
                        decoderConfigurations, encoderConfigurations);
                if (controller != null) {
                    logger.info("feature {} ({}) beforeInstance() has intercepted drools controller {}:{}:{}",
                            feature.getName(), feature.getSequenceNumber(),
                            newGroupId, newArtifactId, newVersion);
                    break;
                }
            } catch (RuntimeException r) {
                logger.error("feature {} ({}) beforeInstance() of drools controller {}:{}:{} failed",
                        feature.getName(), feature.getSequenceNumber(),
                        newGroupId, newArtifactId, newVersion, r);
            }
        }
        return controller;
    }

    protected List<DroolsControllerFeatureApi> getProviders() {
        return DroolsControllerFeatureApiConstants.getProviders().getList();
    }

    /**
     * find out decoder classes and filters.
     *
     * @param properties properties with information about decoders
     * @param topicEntities topic sources
     * @return list of topics, each with associated decoder classes, each with a list of associated
     *         filters
     * @throws IllegalArgumentException invalid input data
     */
    protected List<TopicCoderFilterConfiguration> codersAndFilters(Properties properties,
            List<? extends Topic> topicEntities) {

        List<TopicCoderFilterConfiguration> topics2DecodedClasses2Filters = new ArrayList<>();

        if (topicEntities == null || topicEntities.isEmpty()) {
            return topics2DecodedClasses2Filters;
        }

        for (Topic topic : topicEntities) {

            // 1. first the topic

            String firstTopic = topic.getTopic();

            String propertyTopicEntityPrefix = getPropertyTopicPrefix(topic) + firstTopic;

            // 2. check if there is a custom decoder for this topic that the user prefers to use
            // instead of the ones provided in the platform

            var customGsonCoder = getCustomCoder(properties, propertyTopicEntityPrefix);

            // 3. second the list of classes associated with each topic

            String eventClasses = properties
                    .getProperty(propertyTopicEntityPrefix + PolicyEndPointProperties.PROPERTY_TOPIC_EVENTS_SUFFIX);

            if (eventClasses == null || eventClasses.isEmpty()) {
                logger.warn("There are no event classes for topic {}", firstTopic);
                continue;
            }

            List<PotentialCoderFilter> classes2Filters =
                            getFilterExpressions(properties, propertyTopicEntityPrefix, eventClasses);

            topics2DecodedClasses2Filters
                    .add(new TopicCoderFilterConfiguration(firstTopic, classes2Filters, customGsonCoder));
        }

        return topics2DecodedClasses2Filters;
    }

    private String getPropertyTopicPrefix(Topic topic) {
        boolean isSource = topic instanceof TopicSource;
        var commInfra = topic.getTopicCommInfrastructure();
        if (commInfra == CommInfrastructure.NOOP) {
            if (isSource) {
                return PolicyEndPointProperties.PROPERTY_NOOP_SOURCE_TOPICS + ".";
            } else {
                return PolicyEndPointProperties.PROPERTY_NOOP_SINK_TOPICS + ".";
            }
        } else if (commInfra == CommInfrastructure.KAFKA) {
            if (isSource) {
                return PolicyEndPointProperties.PROPERTY_KAFKA_SOURCE_TOPICS + ".";
            } else {
                return PolicyEndPointProperties.PROPERTY_KAFKA_SINK_TOPICS + ".";
            }
        } else {
            throw new IllegalArgumentException("Invalid Communication Infrastructure: " + commInfra);
        }
    }

    private CustomGsonCoder getCustomCoder(Properties properties, String propertyPrefix) {
        String customGson = properties.getProperty(propertyPrefix
                + PolicyEndPointProperties.PROPERTY_TOPIC_EVENTS_CUSTOM_MODEL_CODER_GSON_SUFFIX);

        CustomGsonCoder customGsonCoder = null;
        if (customGson != null && !customGson.isEmpty()) {
            try {
                customGsonCoder = new CustomGsonCoder(customGson);
            } catch (IllegalArgumentException e) {
                logger.warn("{}: cannot create custom-gson-coder {} because of {}", this, customGson,
                        e.getMessage(), e);
            }
        }
        return customGsonCoder;
    }

    private List<PotentialCoderFilter> getFilterExpressions(Properties properties, String propertyPrefix,
                    @NonNull String eventClasses) {

        List<PotentialCoderFilter> classes2Filters = new ArrayList<>();
        for (String theClass : COMMA_SPACE_PAT.split(eventClasses)) {

            // 4. for each coder class, get the filter expression

            String filter = properties
                    .getProperty(propertyPrefix
                            + PolicyEndPointProperties.PROPERTY_TOPIC_EVENTS_SUFFIX
                            + "." + theClass + PolicyEndPointProperties.PROPERTY_TOPIC_EVENTS_FILTER_SUFFIX);

            var class2Filters = new PotentialCoderFilter(theClass, new JsonProtocolFilter(filter));
            classes2Filters.add(class2Filters);
        }

        return classes2Filters;
    }

    @Override
    public void destroy(DroolsController controller) {
        unmanage(controller);
        controller.halt();
    }

    @Override
    public void destroy() {
        List<DroolsController> controllers = this.inventory();
        for (DroolsController controller : controllers) {
            controller.halt();
        }

        synchronized (this) {
            this.droolsControllers.clear();
        }
    }

    /**
     * unmanage the drools controller.
     *
     * @param controller the controller
     */
    protected void unmanage(DroolsController controller) {
        if (controller == null) {
            throw new IllegalArgumentException("No controller provided");
        }

        if (!controller.isBrained()) {
            logger.info("Drools Controller is NOT OPERATIONAL - nothing to destroy");
            return;
        }

        String controllerId = controller.getGroupId() + ":" + controller.getArtifactId();
        synchronized (this) {
            if (!this.droolsControllers.containsKey(controllerId)) {
                return;
            }

            droolsControllers.remove(controllerId);
        }
    }

    @Override
    public void shutdown(DroolsController controller) {
        this.unmanage(controller);
        controller.shutdown();
    }

    @Override
    public void shutdown() {
        List<DroolsController> controllers = this.inventory();
        for (DroolsController controller : controllers) {
            controller.shutdown();
        }

        synchronized (this) {
            this.droolsControllers.clear();
        }
    }

    @Override
    public DroolsController get(String groupId, String artifactId, String version) {

        if (groupId == null || artifactId == null || groupId.isEmpty() || artifactId.isEmpty()) {
            throw new IllegalArgumentException("Missing maven coordinates: " + groupId + ":" + artifactId);
        }

        String controllerId = groupId + ":" + artifactId;

        synchronized (this) {
            if (this.droolsControllers.containsKey(controllerId)) {
                return droolsControllers.get(controllerId);
            } else {
                throw new IllegalStateException("DroolController for " + controllerId + " not found");
            }
        }
    }

    @Override
    public List<DroolsController> inventory() {
        return new ArrayList<>(this.droolsControllers.values());
    }

    @Override
    public String toString() {
        return "IndexedDroolsControllerFactory [#droolsControllers=" + droolsControllers.size() + "]";
    }

}