diff options
author | jhh <jorge.hernandez-herrero@att.com> | 2019-04-08 11:13:14 -0500 |
---|---|---|
committer | jhh <jorge.hernandez-herrero@att.com> | 2019-04-08 19:46:52 -0500 |
commit | 3f90dba2636b06bcb90b8f1e158b30886af574d5 (patch) | |
tree | a4244aecdd658984afb46bb1d2720e363092eec5 /feature-lifecycle/src/main/java/org | |
parent | 420fb3114baf6dc468b3babf5b45304647fcb04d (diff) |
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 <jorge.hernandez-herrero@att.com>
Diffstat (limited to 'feature-lifecycle/src/main/java/org')
5 files changed, 176 insertions, 21 deletions
diff --git a/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleFeature.java b/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleFeature.java index 5e1ddaea..9f292946 100644 --- a/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleFeature.java +++ b/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleFeature.java @@ -23,6 +23,7 @@ package org.onap.policy.drools.lifecycle; import org.onap.policy.drools.features.DroolsControllerFeatureAPI; import org.onap.policy.drools.features.PolicyControllerFeatureAPI; import org.onap.policy.drools.features.PolicyEngineFeatureAPI; +import org.onap.policy.drools.system.PolicyController; import org.onap.policy.drools.system.PolicyEngine; /** @@ -40,30 +41,51 @@ public class LifecycleFeature return 10; } - /** - * The 'afterStart' hook on the Policy Engine tell us when the engine is functional. - */ @Override public boolean afterStart(PolicyEngine engine) { fsm.start(); return false; } - /** - * The 'afterStop' hook on the Policy Engine tell us when the engine is stopping. - */ @Override - public boolean afterStop(PolicyEngine engine) { + public boolean afterStart(PolicyController controller) { + fsm.start(controller); + return false; + } + + @Override + public boolean beforeStop(PolicyEngine engine) { fsm.stop(); return false; } - /** - * The 'beforeShutdown' hook on the Policy Engine tell us when the engine is going away. - */ + @Override + public boolean beforeStop(PolicyController controller) { + fsm.stop(controller); + return false; + } + @Override public boolean beforeShutdown(PolicyEngine engine) { fsm.shutdown(); return false; } + + @Override + public boolean beforeHalt(PolicyController controller) { + fsm.stop(controller); + return false; + } + + @Override + public boolean beforeLock(PolicyController controller) { + fsm.stop(controller); + return false; + } + + @Override + public boolean afterUnlock(PolicyController controller) { + fsm.start(controller); + return false; + } } diff --git a/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleFsm.java b/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleFsm.java index 74375688..b99953ed 100644 --- a/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleFsm.java +++ b/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleFsm.java @@ -21,7 +21,9 @@ package org.onap.policy.drools.lifecycle; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Objects; import java.util.Properties; import java.util.concurrent.ScheduledFuture; @@ -44,14 +46,17 @@ import org.onap.policy.common.utils.coder.StandardCoderObject; import org.onap.policy.common.utils.network.NetworkUtil; import org.onap.policy.drools.controller.DroolsController; import org.onap.policy.drools.persistence.SystemPersistence; +import org.onap.policy.drools.system.PolicyController; import org.onap.policy.models.pdp.concepts.PdpResponseDetails; import org.onap.policy.models.pdp.concepts.PdpStateChange; import org.onap.policy.models.pdp.concepts.PdpStatus; import org.onap.policy.models.pdp.concepts.PdpUpdate; -import org.onap.policy.models.pdp.concepts.ToscaPolicyTypeIdentifier; import org.onap.policy.models.pdp.enums.PdpHealthStatus; import org.onap.policy.models.pdp.enums.PdpMessageType; import org.onap.policy.models.pdp.enums.PdpState; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -103,6 +108,10 @@ public class LifecycleFsm implements Startable { @Getter protected String subgroup; + protected final Map<ToscaPolicyTypeIdentifier, PolicyController> policyTypesMap = new HashMap<>(); + + protected final Map<ToscaPolicyIdentifier, ToscaPolicy> policiesMap = new HashMap<>(); + /** * Constructor. */ @@ -130,19 +139,39 @@ public class LifecycleFsm implements Startable { @Override public synchronized boolean start() { - logger.info("lifecycle event: start"); + logger.info("lifecycle event: start engine"); return state.start(); } + /** + * Start a controller event. + */ + public synchronized void start(@NonNull PolicyController controller) { + logger.info("lifecycle event: start controller: {}" + controller.getName()); + for (ToscaPolicyTypeIdentifier id : controller.getPolicyTypes()) { + policyTypesMap.put(id, controller); + } + } + @Override public synchronized boolean stop() { - logger.info("lifecycle event: stop"); + logger.info("lifecycle event: stop engine"); return state.stop(); } + /** + * Stop a controller event. + */ + public synchronized void stop(@NonNull PolicyController controller) { + logger.info("lifecycle event: stop controller: {}" + controller.getName()); + for (ToscaPolicyTypeIdentifier id : controller.getPolicyTypes()) { + policyTypesMap.remove(id); + } + } + @Override public synchronized void shutdown() { - logger.info("lifecycle event: shutdown"); + logger.info("lifecycle event: shutdown engine"); state.shutdown(); } @@ -234,6 +263,26 @@ public class LifecycleFsm implements Startable { return stopTimers() && startTimers(); } + protected PolicyController getController(ToscaPolicyTypeIdentifier policyType) { + return policyTypesMap.get(policyType); + } + + protected List<ToscaPolicy> getDeployablePoliciesAction(@NonNull List<ToscaPolicy> policies) { + List<ToscaPolicy> deployPolicies = new ArrayList<>(policies); + deployPolicies.removeAll(policiesMap.values()); + return deployPolicies; + } + + protected List<ToscaPolicy> getUndeployablePoliciesAction(@NonNull List<ToscaPolicy> policies) { + List<ToscaPolicy> undeployPolicies = new ArrayList<>(policiesMap.values()); + undeployPolicies.removeAll(policies); + return undeployPolicies; + } + + protected void deployedPolicyAction(@NonNull ToscaPolicy policy) { + policiesMap.put(policy.getIdentifier(), policy); + } + /* ** Action Helpers ** */ private boolean startIo() { diff --git a/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleStateActive.java b/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleStateActive.java index 9ec68653..d481b8bd 100644 --- a/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleStateActive.java +++ b/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleStateActive.java @@ -20,16 +20,22 @@ package org.onap.policy.drools.lifecycle; +import lombok.NonNull; import lombok.ToString; +import org.onap.policy.drools.system.PolicyController; import org.onap.policy.models.pdp.concepts.PdpStateChange; import org.onap.policy.models.pdp.enums.PdpResponseStatus; import org.onap.policy.models.pdp.enums.PdpState; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Lifecycle Active State. */ @ToString public class LifecycleStateActive extends LifecycleStateRunning { + private static final Logger logger = LoggerFactory.getLogger(LifecycleStatePassive.class); protected LifecycleStateActive(LifecycleFsm manager) { super(manager); @@ -51,4 +57,26 @@ public class LifecycleStateActive extends LifecycleStateRunning { return fsm.statusAction(response(change.getRequestId(), PdpResponseStatus.SUCCESS, null)); } + @Override + protected boolean deployPolicy(@NonNull PolicyController controller, @NonNull ToscaPolicy policy) { + logger.info("{}: deploy {} into {}", this, policy.getIdentifier(), controller.getName()); + + // TODO: This is the latest version - retract policy with same id but different version + + if (!controller.offer(policy)) { + return false; + } + + fsm.deployedPolicyAction(policy); + return true; + } + + @Override + protected boolean undeployPolicy(@NonNull PolicyController controller, @NonNull ToscaPolicy policy) { + logger.info("{}: undeploy {} from {}", this, policy.getIdentifier(), controller.getName()); + + // TODO: retract policy. + + return true; + } } diff --git a/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleStatePassive.java b/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleStatePassive.java index 0720ec98..e9f4b9bf 100644 --- a/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleStatePassive.java +++ b/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleStatePassive.java @@ -20,16 +20,22 @@ package org.onap.policy.drools.lifecycle; +import lombok.NonNull; import lombok.ToString; +import org.onap.policy.drools.system.PolicyController; import org.onap.policy.models.pdp.concepts.PdpStateChange; import org.onap.policy.models.pdp.enums.PdpResponseStatus; import org.onap.policy.models.pdp.enums.PdpState; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Lifecycle Passive State. */ @ToString public class LifecycleStatePassive extends LifecycleStateRunning { + private static final Logger logger = LoggerFactory.getLogger(LifecycleStatePassive.class); protected LifecycleStatePassive(LifecycleFsm manager) { super(manager); @@ -41,13 +47,25 @@ public class LifecycleStatePassive extends LifecycleStateRunning { } @Override - protected boolean stateChangeToActive(PdpStateChange change) { + protected boolean stateChangeToActive(@NonNull PdpStateChange change) { fsm.transitionToAction(new LifecycleStateActive(fsm)); return fsm.statusAction(response(change.getRequestId(), PdpResponseStatus.SUCCESS,null)); } @Override - protected boolean stateChangeToPassive(PdpStateChange change) { + protected boolean stateChangeToPassive(@NonNull PdpStateChange change) { return fsm.statusAction(response(change.getRequestId(), PdpResponseStatus.SUCCESS,null)); } + + @Override + protected boolean deployPolicy(@NonNull PolicyController controller, @NonNull ToscaPolicy policy) { + logger.info("{}: deploy {} from {}", this, policy.getIdentifier(), controller.getName()); + return true; + } + + @Override + protected boolean undeployPolicy(@NonNull PolicyController controller, @NonNull ToscaPolicy policy) { + logger.info("{}: undeploy {} from {}", this, policy.getIdentifier(), controller.getName()); + return true; + } } diff --git a/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleStateRunning.java b/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleStateRunning.java index 916d155e..405dbeba 100644 --- a/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleStateRunning.java +++ b/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleStateRunning.java @@ -21,13 +21,16 @@ package org.onap.policy.drools.lifecycle; import java.util.List; +import java.util.function.BiFunction; import lombok.NonNull; +import org.onap.policy.drools.system.PolicyController; import org.onap.policy.models.pdp.concepts.PdpResponseDetails; import org.onap.policy.models.pdp.concepts.PdpStateChange; import org.onap.policy.models.pdp.concepts.PdpUpdate; import org.onap.policy.models.pdp.enums.PdpResponseStatus; import org.onap.policy.models.pdp.enums.PdpState; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -38,9 +41,13 @@ public abstract class LifecycleStateRunning extends LifecycleStateDefault { private static final Logger logger = LoggerFactory.getLogger(LifecycleState.class); - protected abstract boolean stateChangeToPassive(PdpStateChange change); + protected abstract boolean stateChangeToPassive(@NonNull PdpStateChange change); - protected abstract boolean stateChangeToActive(PdpStateChange change); + protected abstract boolean stateChangeToActive(@NonNull PdpStateChange change); + + protected abstract boolean deployPolicy(@NonNull PolicyController controller, @NonNull ToscaPolicy policy); + + protected abstract boolean undeployPolicy(@NonNull PolicyController controller, @NonNull ToscaPolicy policy); protected LifecycleStateRunning(LifecycleFsm manager) { super(manager); @@ -104,7 +111,8 @@ public abstract class LifecycleStateRunning extends LifecycleStateDefault { @Override public boolean update(@NonNull PdpUpdate update) { synchronized (fsm) { - if (!fsm.setStatusIntervalAction(update.getPdpHeartbeatIntervalMs() / 1000)) { + if (update.getPdpHeartbeatIntervalMs() != null + && !fsm.setStatusIntervalAction(update.getPdpHeartbeatIntervalMs() / 1000)) { fsm.statusAction(response(update.getRequestId(), PdpResponseStatus.FAIL, "invalid interval: " + update.getPdpHeartbeatIntervalMs() + " seconds")); return false; @@ -122,8 +130,38 @@ public abstract class LifecycleStateRunning extends LifecycleStateDefault { } protected boolean updatePolicies(List<ToscaPolicy> policies) { - // TODO - return true; + if (policies == null) { + return true; + } + + boolean success = deployPolicies(policies); + return undeployPolicies(policies) && success; + } + + protected boolean deployPolicies(List<ToscaPolicy> policies) { + return syncPolicies(fsm.getDeployablePoliciesAction(policies), this::deployPolicy); + } + + protected boolean undeployPolicies(List<ToscaPolicy> policies) { + return syncPolicies(fsm.getUndeployablePoliciesAction(policies), this::undeployPolicy); + } + + protected boolean syncPolicies(List<ToscaPolicy> policies, + BiFunction<PolicyController, ToscaPolicy, Boolean> sync) { + boolean success = true; + for (ToscaPolicy policy : policies) { + ToscaPolicyTypeIdentifier policyType = policy.getTypeIdentifier(); + PolicyController controller = fsm.getController(policyType); + if (controller == null) { + logger.warn("no controller found for {}", policyType); + success = false; + continue; + } + + success = sync.apply(controller, policy) && success; + } + + return success; } private void invalidStateChange(PdpStateChange change) { |