From 8382101e6c43578b6c78dfc10bacbd165e413c69 Mon Sep 17 00:00:00 2001 From: "Straubs, Ralph (rs8887)" Date: Mon, 13 Jul 2020 10:26:50 -0400 Subject: Support multiple Policy/Controller types using 'controller.type' property It provides a feature base to allow for custom Policy and Drools Controllers. Issue-ID: POLICY-2415 Change-Id: Ibe3f11e3ecd925537ffd03d2420bb3b8214029c9 Signed-off-by: Straubs, Ralph (rs8887) Signed-off-by: jhh --- .../drools/controller/DroolsControllerFactory.java | 7 +-- .../controller/IndexedDroolsControllerFactory.java | 51 +++++++++++++++++++--- .../features/DroolsControllerFeatureApi.java | 37 +++++++++++++++- .../features/PolicyControllerFeatureApi.java | 26 +++++++++++ .../system/IndexedPolicyControllerFactory.java | 35 +++++++++++++-- .../internal/AggregatedPolicyController.java | 8 ++-- 6 files changed, 147 insertions(+), 17 deletions(-) (limited to 'policy-management/src/main/java/org/onap') diff --git a/policy-management/src/main/java/org/onap/policy/drools/controller/DroolsControllerFactory.java b/policy-management/src/main/java/org/onap/policy/drools/controller/DroolsControllerFactory.java index 7652a671..a355f6f7 100644 --- a/policy-management/src/main/java/org/onap/policy/drools/controller/DroolsControllerFactory.java +++ b/policy-management/src/main/java/org/onap/policy/drools/controller/DroolsControllerFactory.java @@ -1,8 +1,8 @@ /* * ============LICENSE_START======================================================= - * policy-management + * ONAP * ================================================================================ - * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -49,6 +49,7 @@ public interface DroolsControllerFactory { /** * Explicit construction of a Drools Controller. * + * @param properties properties containing initialization parameters * @param groupId maven group id of drools artifact * @param artifactId maven artifact id of drools artifact * @param version maven version id of drools artifact @@ -59,7 +60,7 @@ public interface DroolsControllerFactory { * @throws IllegalArgumentException with invalid parameters * @throws LinkageError Failure to link rules and models in Drools Libraries */ - DroolsController build(String groupId, String artifactId, String version, + DroolsController build(Properties properties, String groupId, String artifactId, String version, List decoderConfigurations, List encoderConfigurations) throws LinkageError; diff --git a/policy-management/src/main/java/org/onap/policy/drools/controller/IndexedDroolsControllerFactory.java b/policy-management/src/main/java/org/onap/policy/drools/controller/IndexedDroolsControllerFactory.java index 6d42ba53..e8234a46 100644 --- a/policy-management/src/main/java/org/onap/policy/drools/controller/IndexedDroolsControllerFactory.java +++ b/policy-management/src/main/java/org/onap/policy/drools/controller/IndexedDroolsControllerFactory.java @@ -24,6 +24,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Properties; import org.onap.policy.common.endpoints.event.comm.Topic; import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; @@ -32,6 +33,8 @@ import org.onap.policy.common.endpoints.event.comm.TopicSource; import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties; 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; @@ -48,12 +51,12 @@ class IndexedDroolsControllerFactory implements DroolsControllerFactory { /** * logger. */ - private static Logger logger = LoggerFactory.getLogger(IndexedDroolsControllerFactory.class); + private static final Logger logger = LoggerFactory.getLogger(IndexedDroolsControllerFactory.class); /** * Policy Controller Name Index. */ - protected HashMap droolsControllers = new HashMap<>(); + protected Map droolsControllers = new HashMap<>(); /** * Null Drools Controller. @@ -97,11 +100,12 @@ class IndexedDroolsControllerFactory implements DroolsControllerFactory { List topics2DecodedClasses2Filters = codersAndFilters(properties, eventSources); List topics2EncodedClasses2Filters = codersAndFilters(properties, eventSinks); - return this.build(groupId, artifactId, version, topics2DecodedClasses2Filters, topics2EncodedClasses2Filters); + return this.build(properties, groupId, artifactId, version, + topics2DecodedClasses2Filters, topics2EncodedClasses2Filters); } @Override - public DroolsController build(String newGroupId, String newArtifactId, String newVersion, + public DroolsController build(Properties properties, String newGroupId, String newArtifactId, String newVersion, List decoderConfigurations, List encoderConfigurations) throws LinkageError { @@ -148,16 +152,51 @@ class IndexedDroolsControllerFactory implements DroolsControllerFactory { /* new drools controller */ - DroolsController controller = new MavenDroolsController(newGroupId, newArtifactId, newVersion, - decoderConfigurations, 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); + } + } + + if (controller == null) { + controller = new MavenDroolsController(newGroupId, newArtifactId, newVersion, decoderConfigurations, + encoderConfigurations); + } synchronized (this) { droolsControllers.put(controllerId, controller); } + for (DroolsControllerFeatureApi feature: getProviders()) { + try { + feature.afterInstance(controller, properties); + } catch (RuntimeException r) { + logger.error("feature {} ({}) afterInstance() of drools controller {}:{}:{} failed", + feature.getName(), feature.getSequenceNumber(), + newGroupId, newArtifactId, newVersion, r); + } + } + return controller; } + protected List getProviders() { + return DroolsControllerFeatureApiConstants.getProviders().getList(); + } + /** * find out decoder classes and filters. * diff --git a/policy-management/src/main/java/org/onap/policy/drools/features/DroolsControllerFeatureApi.java b/policy-management/src/main/java/org/onap/policy/drools/features/DroolsControllerFeatureApi.java index abf524e4..bb11bb64 100644 --- a/policy-management/src/main/java/org/onap/policy/drools/features/DroolsControllerFeatureApi.java +++ b/policy-management/src/main/java/org/onap/policy/drools/features/DroolsControllerFeatureApi.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2018-2020 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,15 +20,50 @@ package org.onap.policy.drools.features; +import java.util.List; +import java.util.Properties; import org.onap.policy.common.endpoints.event.comm.TopicSink; import org.onap.policy.common.utils.services.OrderedService; import org.onap.policy.drools.controller.DroolsController; +import org.onap.policy.drools.protocol.coders.TopicCoderFilterConfiguration; /** * Drools Controller Feature API. Hooks into the Drools Controller operations. */ public interface DroolsControllerFeatureApi extends OrderedService { + /** + * intercepts the instantiation of a DroolsController. + * + * @param properties controller properties + * @param groupId group id coordinate + * @param artifactId artifact id coordinate + * @param version version coordinate + * @param decoderConfigurations decoder configurations + * @param encoderConfigurations encoder configurations + * + * @return a Drools Controller or 'null' for no intercept + */ + default DroolsController beforeInstance(Properties properties, + String groupId, String artifactId, String version, + List decoderConfigurations, + List encoderConfigurations) { + return null; + } + + /** + * called after a DroolsController is instantiated. + * + * @param droolsController drools controller + * @param properties controller properties + * + * @return True if this feature intercepts and takes ownership of the operation + * preventing the invocation of lower priority features. False, otherwise + */ + default boolean afterInstance(DroolsController droolsController, Properties properties) { + return false; + } + /** * intercepts before the Drools Controller gives the Policy Container a fact to * insert into its Policy Sessions. diff --git a/policy-management/src/main/java/org/onap/policy/drools/features/PolicyControllerFeatureApi.java b/policy-management/src/main/java/org/onap/policy/drools/features/PolicyControllerFeatureApi.java index d11863f0..f022bf1d 100644 --- a/policy-management/src/main/java/org/onap/policy/drools/features/PolicyControllerFeatureApi.java +++ b/policy-management/src/main/java/org/onap/policy/drools/features/PolicyControllerFeatureApi.java @@ -57,6 +57,32 @@ public interface PolicyControllerFeatureApi extends OrderedService { return false; } + /** + * called before the actual instantiation of a Policy Controller. + * + * @param name name of the controller + * @param properties configuration parameters in the form of properties + * + * @return a Policy Controller or 'null for no intercept. + */ + default PolicyController beforeInstance(String name, Properties properties) { + return null; + } + + /** + * called after the Policy Controller is instantiated. + * + * @param controller policy controller + * @param properties configuration parameters in the form of properties + * + * @return true if this feature intercepts and takes ownership + * of the operation preventing the invocation of + * lower priority features. False, otherwise. + */ + default boolean afterInstance(PolicyController controller, Properties properties) { + return false; + } + /** * intercept before the Policy Controller is started. * diff --git a/policy-management/src/main/java/org/onap/policy/drools/system/IndexedPolicyControllerFactory.java b/policy-management/src/main/java/org/onap/policy/drools/system/IndexedPolicyControllerFactory.java index 6956a45e..c03535b1 100644 --- a/policy-management/src/main/java/org/onap/policy/drools/system/IndexedPolicyControllerFactory.java +++ b/policy-management/src/main/java/org/onap/policy/drools/system/IndexedPolicyControllerFactory.java @@ -24,6 +24,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore; import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Properties; import org.onap.policy.common.gson.annotation.GsonJsonIgnore; import org.onap.policy.drools.controller.DroolsController; @@ -44,13 +45,13 @@ class IndexedPolicyControllerFactory implements PolicyControllerFactory { /** * Policy Controller Name Index. */ - private final HashMap policyControllers = + private final Map policyControllers = new HashMap<>(); /** * Group/Artifact Ids Index. */ - private final HashMap coordinates2Controller = + private final Map coordinates2Controller = new HashMap<>(); /** @@ -380,7 +381,35 @@ class IndexedPolicyControllerFactory implements PolicyControllerFactory { // these methods can be overridden by junit tests protected PolicyController newPolicyController(String name, Properties properties) { - return new AggregatedPolicyController(name, properties); + PolicyController controller = null; + for (PolicyControllerFeatureApi feature: getProviders()) { + try { + controller = feature.beforeInstance(name, properties); + if (controller != null) { + logger.info("feature {} ({}) beforeInstance() has intercepted controller {}", + feature.getName(), feature.getSequenceNumber(), name); + break; + } + } catch (RuntimeException r) { + logger.error("feature {} ({}) beforeInstance() of controller {} failed", + feature.getName(), feature.getSequenceNumber(), name, r); + } + } + + if (controller == null) { + controller = new AggregatedPolicyController(name, properties); + } + + for (PolicyControllerFeatureApi feature: getProviders()) { + try { + feature.afterInstance(controller, properties); + } catch (RuntimeException r) { + logger.error("feature {} ({}) afterInstance() of controller {} failed ", + feature.getName(), feature.getSequenceNumber(), name, r); + } + } + + return controller; } protected List getProviders() { diff --git a/policy-management/src/main/java/org/onap/policy/drools/system/internal/AggregatedPolicyController.java b/policy-management/src/main/java/org/onap/policy/drools/system/internal/AggregatedPolicyController.java index 8eb2f85b..4e23dab1 100644 --- a/policy-management/src/main/java/org/onap/policy/drools/system/internal/AggregatedPolicyController.java +++ b/policy-management/src/main/java/org/onap/policy/drools/system/internal/AggregatedPolicyController.java @@ -73,12 +73,12 @@ public class AggregatedPolicyController implements PolicyController, TopicListen /** * Abstracted Event Sources List regardless communication technology. */ - private final List sources; + protected final List sources; /** * Abstracted Event Sinks List regardless communication technology. */ - private final List sinks; + protected final List sinks; /** * Mapping topics to sinks. @@ -102,7 +102,7 @@ public class AggregatedPolicyController implements PolicyController, TopicListen /** * Policy Drools Controller. */ - private final AtomicReference droolsController = new AtomicReference<>(); + protected final AtomicReference droolsController = new AtomicReference<>(); /** * Properties used to initialize controller. @@ -190,7 +190,7 @@ public class AggregatedPolicyController implements PolicyController, TopicListen * * @throws IllegalArgumentException if invalid parameters are passed in */ - private void initDrools(Properties properties) { + protected void initDrools(Properties properties) { try { // Register with drools infrastructure this.droolsController.set(getDroolsFactory().build(properties, sources, sinks)); -- cgit 1.2.3-korg