From 3f90dba2636b06bcb90b8f1e158b30886af574d5 Mon Sep 17 00:00:00 2001 From: jhh Date: Mon, 8 Apr 2019 11:13:14 -0500 Subject: Initial support for deploy This is a first pass, some functionality is missing (undeploy). Compile error fix from changes in policy/models repo. Change-Id: If448492ab665c135bace99d4d684d403e2a6be03 Issue-ID: POLICY-1624 Signed-off-by: jhh --- .../policy/drools/controller/DroolsController.java | 65 ++++++++------ .../controller/internal/MavenDroolsController.java | 45 +++++----- .../controller/internal/NullDroolsController.java | 5 ++ .../features/PolicyControllerFeatureAPI.java | 91 +++++++++++-------- .../policy/drools/system/PolicyController.java | 16 +++- .../internal/AggregatedPolicyController.java | 100 ++++++++++++++++++--- 6 files changed, 221 insertions(+), 101 deletions(-) (limited to 'policy-management/src/main/java') diff --git a/policy-management/src/main/java/org/onap/policy/drools/controller/DroolsController.java b/policy-management/src/main/java/org/onap/policy/drools/controller/DroolsController.java index 1e7b8c79..344725ff 100644 --- a/policy-management/src/main/java/org/onap/policy/drools/controller/DroolsController.java +++ b/policy-management/src/main/java/org/onap/policy/drools/controller/DroolsController.java @@ -37,74 +37,83 @@ public interface DroolsController extends Startable, Lockable { /** * No Group ID identifier. */ - public static final String NO_GROUP_ID = "NO-GROUP-ID"; + String NO_GROUP_ID = "NO-GROUP-ID"; /** * No Artifact ID identifier. */ - public static final String NO_ARTIFACT_ID = "NO-ARTIFACT-ID"; + String NO_ARTIFACT_ID = "NO-ARTIFACT-ID"; /** * No version identifier. */ - public static final String NO_VERSION = "NO-VERSION"; + String NO_VERSION = "NO-VERSION"; /** * Factory to track and manage drools controllers. */ - public static final DroolsControllerFactory factory = new IndexedDroolsControllerFactory(); + DroolsControllerFactory factory = new IndexedDroolsControllerFactory(); /** * get group id. * * @return group id */ - public String getGroupId(); + String getGroupId(); /** * get artifact id. * * @return artifact id */ - public String getArtifactId(); + String getArtifactId(); /** * get version. * * @return version */ - public String getVersion(); + String getVersion(); /** * return the policy session names. * * @return policy session */ - public List getSessionNames(); + List getSessionNames(); /** * return the policy full session names. * * @return policy session */ - public List getCanonicalSessionNames(); + List getCanonicalSessionNames(); /** * get base domains. * * @return list of base domains. */ - public List getBaseDomainNames(); + List getBaseDomainNames(); /** - * offers an event to this controller for processing. + * offers a raw event to this controller for processing. * * @param topic topic associated with the event * @param event the event * * @return true if the operation was successful */ - public boolean offer(String topic, String event); + boolean offer(String topic, String event); + + /** + * offers a T event to this controller for processing. + * + * @param event the event + * + * @return true if the operation was successful + */ + boolean offer(T event); /** * delivers "event" to "sink". @@ -118,28 +127,28 @@ public interface DroolsController extends Startable, Lockable { * @throws UnsupportedOperationException when the engine cannot deliver due to the functionality * missing (ie. communication infrastructure not supported. */ - public boolean deliver(TopicSink sink, Object event); + boolean deliver(TopicSink sink, Object event); /** * Get recent source events. * * @return the most recent received events. */ - public Object[] getRecentSourceEvents(); + Object[] getRecentSourceEvents(); /** * Get recent sink events. * * @return the most recent delivered events */ - public String[] getRecentSinkEvents(); + String[] getRecentSinkEvents(); /** * Get container. * * @return the underlying policy container */ - public PolicyContainer getContainer(); + PolicyContainer getContainer(); /** * Does it owns the coder. @@ -148,7 +157,7 @@ public interface DroolsController extends Startable, Lockable { * @param modelHash the hash for the model * @return true it owns it */ - public boolean ownsCoder(Class coderClass, int modelHash); + boolean ownsCoder(Class coderClass, int modelHash); /** * fetches a class from the model. @@ -156,12 +165,12 @@ public interface DroolsController extends Startable, Lockable { * @param className the class to fetch * @return the actual class object, or null if not found */ - public Class fetchModelClass(String className); + Class fetchModelClass(String className); /** * is this controller Smart. */ - public boolean isBrained(); + boolean isBrained(); /** * update the new version of the maven jar rules file. @@ -175,9 +184,9 @@ public interface DroolsController extends Startable, Lockable { * @throws Exception from within drools libraries * @throws LinkageError from within drools libraries */ - public void updateToVersion(String newGroupId, String newArtifactId, String newVersion, - List decoderConfigurations, - List encoderConfigurations) throws LinkageError; + void updateToVersion(String newGroupId, String newArtifactId, String newVersion, + List decoderConfigurations, + List encoderConfigurations) throws LinkageError; /** * gets the classnames of facts as well as the current count. @@ -185,7 +194,7 @@ public interface DroolsController extends Startable, Lockable { * @param sessionName the session name * @return map of class to count */ - public Map factClassNames(String sessionName); + Map factClassNames(String sessionName); /** * gets the count of facts for a given session. @@ -193,7 +202,7 @@ public interface DroolsController extends Startable, Lockable { * @param sessionName the session name * @return the fact count */ - public long factCount(String sessionName); + long factCount(String sessionName); /** * gets all the facts of a given class for a given session. @@ -203,7 +212,7 @@ public interface DroolsController extends Startable, Lockable { * @param delete retract from drools the results of the query? * @return the list of facts returned by the query */ - public List facts(String sessionName, String className, boolean delete); + List facts(String sessionName, String className, boolean delete); /** * gets the facts associated with a query for a give session for a given queried entity. @@ -215,12 +224,12 @@ public interface DroolsController extends Startable, Lockable { * @param queryParams query parameters * @return list of facts returned by the query */ - public List factQuery(String sessionName, String queryName, String queriedEntity, boolean delete, - Object... queryParams); + List factQuery(String sessionName, String queryName, String queriedEntity, boolean delete, + Object... queryParams); /** * halts and permanently releases all resources. * */ - public void halt(); + void halt(); } diff --git a/policy-management/src/main/java/org/onap/policy/drools/controller/internal/MavenDroolsController.java b/policy-management/src/main/java/org/onap/policy/drools/controller/internal/MavenDroolsController.java index eb401eba..95b053fb 100644 --- a/policy-management/src/main/java/org/onap/policy/drools/controller/internal/MavenDroolsController.java +++ b/policy-management/src/main/java/org/onap/policy/drools/controller/internal/MavenDroolsController.java @@ -480,19 +480,9 @@ public class MavenDroolsController implements DroolsController { @Override public boolean offer(String topic, String event) { - logger.debug("{}: OFFER: {} <- {}", this, topic, event); + logger.debug("{}: OFFER raw event from {}", this, topic); - if (this.locked) { - return true; - } - if (!this.alive) { - return true; - } - - // 0. Check if the policy container has any sessions - - if (this.policyContainer.getPolicySessions().isEmpty()) { - // no sessions + if (this.locked || !this.alive || this.policyContainer.getPolicySessions().isEmpty()) { return true; } @@ -525,48 +515,55 @@ public class MavenDroolsController implements DroolsController { return true; } + return offer(anEvent); + + } + + @Override + public boolean offer(T event) { + logger.debug("{}: OFFER event", this); + + if (this.locked || !this.alive || this.policyContainer.getPolicySessions().isEmpty()) { + return true; + } + synchronized (this.recentSourceEvents) { - this.recentSourceEvents.add(anEvent); + this.recentSourceEvents.add(event); } - // increment event count for Nagios monitoring PdpJmx.getInstance().updateOccured(); // Broadcast - if (logger.isInfoEnabled()) { - logger.info("{} BROADCAST-INJECT of {} FROM {} INTO {}", - this, event, topic, this.policyContainer.getName()); - } - for (DroolsControllerFeatureAPI feature : DroolsControllerFeatureAPI.providers.getList()) { try { - if (feature.beforeInsert(this, anEvent)) { + if (feature.beforeInsert(this, event)) { return true; } } catch (Exception e) { logger.error("{}: feature {} before-insert failure because of {}", - this, feature.getClass().getName(), e.getMessage(), e); + this, feature.getClass().getName(), e.getMessage(), e); } } - boolean successInject = this.policyContainer.insertAll(anEvent); + boolean successInject = this.policyContainer.insertAll(event); if (!successInject) { logger.warn(this + "Failed to inject into PolicyContainer {}", this.getSessionNames()); } for (DroolsControllerFeatureAPI feature : DroolsControllerFeatureAPI.providers.getList()) { try { - if (feature.afterInsert(this, anEvent, successInject)) { + if (feature.afterInsert(this, event, successInject)) { return true; } } catch (Exception e) { logger.error("{}: feature {} after-insert failure because of {}", - this, feature.getClass().getName(), e.getMessage(), e); + this, feature.getClass().getName(), e.getMessage(), e); } } return true; + } @Override diff --git a/policy-management/src/main/java/org/onap/policy/drools/controller/internal/NullDroolsController.java b/policy-management/src/main/java/org/onap/policy/drools/controller/internal/NullDroolsController.java index 45568678..815aaabb 100644 --- a/policy-management/src/main/java/org/onap/policy/drools/controller/internal/NullDroolsController.java +++ b/policy-management/src/main/java/org/onap/policy/drools/controller/internal/NullDroolsController.java @@ -110,6 +110,11 @@ public class NullDroolsController implements DroolsController { return false; } + @Override + public boolean offer(T event) { + return false; + } + @Override public boolean deliver(TopicSink sink, Object event) { throw new IllegalStateException(makeInvokeMsg()); 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 40958427..914b073f 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 @@ -21,7 +21,6 @@ package org.onap.policy.drools.features; import java.util.Properties; - import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; import org.onap.policy.common.utils.services.OrderedService; import org.onap.policy.common.utils.services.OrderedServiceImpl; @@ -32,7 +31,7 @@ public interface PolicyControllerFeatureAPI extends OrderedService { /** * Feature providers implementing this interface. */ - public static final OrderedServiceImpl providers = + OrderedServiceImpl providers = new OrderedServiceImpl<>(PolicyControllerFeatureAPI.class); /** @@ -47,7 +46,7 @@ public interface PolicyControllerFeatureAPI extends OrderedService { * 'null' indicates that no take over has taken place, and processing should * continue to the next feature provider. */ - public default PolicyController beforeCreate(String name, Properties properties) { + default PolicyController beforeCreate(String name, Properties properties) { return null; } @@ -60,7 +59,7 @@ public interface PolicyControllerFeatureAPI extends OrderedService { * of the operation preventing the invocation of * lower priority features. False, otherwise. */ - public default boolean afterCreate(PolicyController controller) { + default boolean afterCreate(PolicyController controller) { return false; } @@ -71,7 +70,7 @@ public interface PolicyControllerFeatureAPI extends OrderedService { * of the operation preventing the invocation of * lower priority features. False, otherwise. */ - public default boolean beforeStart(PolicyController controller) { + default boolean beforeStart(PolicyController controller) { return false; } @@ -82,7 +81,7 @@ public interface PolicyControllerFeatureAPI extends OrderedService { * of the operation preventing the invocation of * lower priority features. False, otherwise. */ - public default boolean afterStart(PolicyController controller) { + default boolean afterStart(PolicyController controller) { return false; } @@ -93,7 +92,7 @@ public interface PolicyControllerFeatureAPI extends OrderedService { * of the operation preventing the invocation of * lower priority features. False, otherwise.. */ - public default boolean beforeStop(PolicyController controller) { + default boolean beforeStop(PolicyController controller) { return false; } @@ -104,7 +103,7 @@ public interface PolicyControllerFeatureAPI extends OrderedService { * of the operation preventing the invocation of * lower priority features. False, otherwise.d. */ - public default boolean afterStop(PolicyController controller) { + default boolean afterStop(PolicyController controller) { return false; } @@ -115,7 +114,7 @@ public interface PolicyControllerFeatureAPI extends OrderedService { * of the operation preventing the invocation of * lower priority features. False, otherwise. */ - public default boolean beforeLock(PolicyController controller) { + default boolean beforeLock(PolicyController controller) { return false; } @@ -126,7 +125,7 @@ public interface PolicyControllerFeatureAPI extends OrderedService { * of the operation preventing the invocation of * lower priority features. False, otherwise.. */ - public default boolean afterLock(PolicyController controller) { + default boolean afterLock(PolicyController controller) { return false; } @@ -137,7 +136,7 @@ public interface PolicyControllerFeatureAPI extends OrderedService { * of the operation preventing the invocation of * lower priority features. False, otherwise. */ - public default boolean beforeUnlock(PolicyController controller) { + default boolean beforeUnlock(PolicyController controller) { return false; } @@ -148,7 +147,7 @@ public interface PolicyControllerFeatureAPI extends OrderedService { * of the operation preventing the invocation of * lower priority features. False, otherwise. */ - public default boolean afterUnlock(PolicyController controller) { + default boolean afterUnlock(PolicyController controller) { return false; } @@ -159,7 +158,7 @@ public interface PolicyControllerFeatureAPI extends OrderedService { * of the operation preventing the invocation of * lower priority features. False, otherwise.. */ - public default boolean beforeShutdown(PolicyController controller) { + default boolean beforeShutdown(PolicyController controller) { return false; } @@ -170,7 +169,7 @@ public interface PolicyControllerFeatureAPI extends OrderedService { * of the operation preventing the invocation of * lower priority features. False, otherwise. */ - public default boolean afterShutdown(PolicyController controller) { + default boolean afterShutdown(PolicyController controller) { return false; } @@ -181,7 +180,7 @@ public interface PolicyControllerFeatureAPI extends OrderedService { * of the operation preventing the invocation of * lower priority features. False, otherwise.. */ - public default boolean beforeHalt(PolicyController controller) { + default boolean beforeHalt(PolicyController controller) { return false; } @@ -192,22 +191,33 @@ public interface PolicyControllerFeatureAPI extends OrderedService { * of the operation preventing the invocation of * lower priority features. False, otherwise. */ - public default boolean afterHalt(PolicyController controller) { + default boolean afterHalt(PolicyController controller) { return false; } - /** + /* * intercept before the Policy Controller is offered an event. * * @return true if this feature intercepts and takes ownership. * of the operation preventing the invocation of * lower priority features. False, otherwise. */ - public default boolean beforeOffer(PolicyController controller, - CommInfrastructure protocol, - String topic, - String event) { + default boolean beforeOffer(PolicyController controller, + CommInfrastructure protocol, + String topic, + String event) { + return false; + } + + /** + * intercept before the Policy Controller delivers (posts) an event. + * + * @return true if this feature intercepts and takes ownership + * of the operation preventing the invocation of + * lower priority features. False, otherwise. + */ + default boolean beforeOffer(PolicyController controller, T event) { return false; } @@ -218,11 +228,22 @@ public interface PolicyControllerFeatureAPI extends OrderedService { * of the operation preventing the invocation of * lower priority features. False, otherwise. */ - public default boolean afterOffer(PolicyController controller, - CommInfrastructure protocol, - String topic, - String event, - boolean success) { + default boolean afterOffer(PolicyController controller, + CommInfrastructure protocol, + String topic, + String event, + boolean success) { + return false; + } + + /** + * called after the Policy Controller delivers (posts) an event. + * + * @return true if this feature intercepts and takes ownership + * of the operation preventing the invocation of + * lower priority features. False, otherwise. + */ + default boolean afterOffer(PolicyController controller, T event, boolean success) { return false; } @@ -233,10 +254,10 @@ public interface PolicyControllerFeatureAPI extends OrderedService { * of the operation preventing the invocation of * lower priority features. False, otherwise. */ - public default boolean beforeDeliver(PolicyController controller, - CommInfrastructure protocol, - String topic, - Object event) { + default boolean beforeDeliver(PolicyController controller, + CommInfrastructure protocol, + String topic, + Object event) { return false; } @@ -247,11 +268,11 @@ public interface PolicyControllerFeatureAPI extends OrderedService { * of the operation preventing the invocation of * lower priority features. False, otherwise. */ - public default boolean afterDeliver(PolicyController controller, - CommInfrastructure protocol, - String topic, - Object event, - boolean success) { + default boolean afterDeliver(PolicyController controller, + CommInfrastructure protocol, + String topic, + Object event, + boolean success) { return false; } } diff --git a/policy-management/src/main/java/org/onap/policy/drools/system/PolicyController.java b/policy-management/src/main/java/org/onap/policy/drools/system/PolicyController.java index 514dc364..9bc75b39 100644 --- a/policy-management/src/main/java/org/onap/policy/drools/system/PolicyController.java +++ b/policy-management/src/main/java/org/onap/policy/drools/system/PolicyController.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * policy-management * ================================================================================ - * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2019 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. @@ -22,7 +22,6 @@ package org.onap.policy.drools.system; import java.util.List; import java.util.Properties; - import org.onap.policy.common.capabilities.Lockable; import org.onap.policy.common.capabilities.Startable; import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; @@ -30,6 +29,7 @@ import org.onap.policy.common.endpoints.event.comm.TopicSink; import org.onap.policy.common.endpoints.event.comm.TopicSource; import org.onap.policy.drools.controller.DroolsController; import org.onap.policy.drools.protocol.configuration.DroolsConfiguration; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier; /** * A Policy Controller is the higher level unit of control. It corresponds to the ncomp equivalent @@ -66,7 +66,12 @@ public interface PolicyController extends Startable, Lockable { DroolsController getDrools(); /** - * update maven configuration. + * Get Policy Types supported by this controller. + */ + List getPolicyTypes(); + + /** + * Update maven configuration. * * @param newDroolsConfiguration new drools configuration * @return true if the update was successful, false otherwise @@ -78,6 +83,11 @@ public interface PolicyController extends Startable, Lockable { */ Properties getProperties(); + /** + * Offer an event of type T. + */ + boolean offer(T event); + /** * Attempts delivering of an String over communication infrastructure "busType". * 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 5bfde9a7..6fd05fb3 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 @@ -21,11 +21,12 @@ package org.onap.policy.drools.system.internal; import com.fasterxml.jackson.annotation.JsonIgnore; - +import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Properties; - +import java.util.stream.Collectors; import org.onap.policy.common.endpoints.event.comm.Topic; import org.onap.policy.common.endpoints.event.comm.TopicEndpoint; import org.onap.policy.common.endpoints.event.comm.TopicListener; @@ -39,6 +40,7 @@ import org.onap.policy.drools.persistence.SystemPersistence; import org.onap.policy.drools.properties.DroolsProperties; import org.onap.policy.drools.protocol.configuration.DroolsConfiguration; import org.onap.policy.drools.system.PolicyController; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -97,6 +99,11 @@ public class AggregatedPolicyController implements PolicyController, TopicListen */ private final Properties properties; + /** + * Policy Types. + */ + private List policyTypes; + /** * Constructor version mainly used for bootstrapping at initialization time a policy engine * controller. @@ -127,6 +134,43 @@ public class AggregatedPolicyController implements PolicyController, TopicListen /* persist new properties */ getPersistenceManager().storeController(name, properties); this.properties = properties; + + this.policyTypes = getPolicyTypesFromProperties(); + } + + @Override + public List getPolicyTypes() { + if (!policyTypes.isEmpty()) { + return policyTypes; + } + + return droolsController + .getBaseDomainNames() + .stream() + .map(d -> new ToscaPolicyTypeIdentifier(d, DroolsProperties.DEFAULT_CONTROLLER_POLICY_TYPE_VERSION)) + .collect(Collectors.toList()); + } + + protected List getPolicyTypesFromProperties() { + List policyTypeIds = new ArrayList<>(); + + String ptiPropValue = properties.getProperty(DroolsProperties.PROPERTY_CONTROLLER_POLICY_TYPES); + if (ptiPropValue == null) { + return policyTypeIds; + } + + List ptiPropList = new ArrayList<>(Arrays.asList(ptiPropValue.split("\\s*,\\s*"))); + for (String pti : ptiPropList) { + String[] ptv = pti.split(":"); + if (ptv.length == 1) { + policyTypeIds.add(new ToscaPolicyTypeIdentifier(ptv[0], + DroolsProperties.DEFAULT_CONTROLLER_POLICY_TYPE_VERSION)); + } else if (ptv.length == 2) { + policyTypeIds.add(new ToscaPolicyTypeIdentifier(ptv[0], ptv[1])); + } + } + + return policyTypeIds; } /** @@ -399,8 +443,11 @@ public class AggregatedPolicyController implements PolicyController, TopicListen */ @Override public void onTopicEvent(Topic.CommInfrastructure commType, String topic, String event) { + logger.debug("{}: raw event offered from {}:{}: {}", this, commType, topic, event); - logger.debug("{}: event offered from {}:{}: {}", this, commType, topic, event); + if (skipOffer()) { + return; + } for (PolicyControllerFeatureAPI feature : getProviders()) { try { @@ -413,14 +460,6 @@ public class AggregatedPolicyController implements PolicyController, TopicListen } } - if (this.locked) { - return; - } - - if (!this.alive) { - return; - } - boolean success = this.droolsController.offer(topic, event); for (PolicyControllerFeatureAPI feature : getProviders()) { @@ -435,6 +474,45 @@ public class AggregatedPolicyController implements PolicyController, TopicListen } } + @Override + public boolean offer(T event) { + logger.debug("{}: event offered: {}", this, event); + + if (skipOffer()) { + return true; + } + + for (PolicyControllerFeatureAPI feature : getProviders()) { + try { + if (feature.beforeOffer(this, event)) { + return true; + } + } catch (Exception e) { + logger.error("{}: feature {} before-offer failure because of {}", this, feature.getClass().getName(), + e.getMessage(), e); + } + } + + boolean success = this.droolsController.offer(event); + + for (PolicyControllerFeatureAPI feature : getProviders()) { + try { + if (feature.afterOffer(this, event, success)) { + return success; + } + } catch (Exception e) { + logger.error("{}: feature {} after-offer failure because of {}", this, feature.getClass().getName(), + e.getMessage(), e); + } + } + + return success; + } + + private boolean skipOffer() { + return isLocked() || !isAlive(); + } + /** * {@inheritDoc}. */ -- cgit 1.2.3-korg