aboutsummaryrefslogtreecommitdiffstats
path: root/feature-lifecycle
diff options
context:
space:
mode:
authorjhh <jorge.hernandez-herrero@att.com>2019-04-08 11:13:14 -0500
committerjhh <jorge.hernandez-herrero@att.com>2019-04-08 19:46:52 -0500
commit3f90dba2636b06bcb90b8f1e158b30886af574d5 (patch)
treea4244aecdd658984afb46bb1d2720e363092eec5 /feature-lifecycle
parent420fb3114baf6dc468b3babf5b45304647fcb04d (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')
-rw-r--r--feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleFeature.java42
-rw-r--r--feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleFsm.java57
-rw-r--r--feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleStateActive.java28
-rw-r--r--feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleStatePassive.java22
-rw-r--r--feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleStateRunning.java48
-rw-r--r--feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/ControllerSupport.java112
-rw-r--r--feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateActiveTest.java42
-rw-r--r--feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStatePassiveTest.java67
-rw-r--r--feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateRunningTest.java63
-rw-r--r--feature-lifecycle/src/test/resources/lifecycle.drl (renamed from feature-lifecycle/src/test/resources/echo.drl)23
-rw-r--r--feature-lifecycle/src/test/resources/lifecycle.kmodule (renamed from feature-lifecycle/src/test/resources/echo.kmodule)4
-rw-r--r--feature-lifecycle/src/test/resources/lifecycle.pom (renamed from feature-lifecycle/src/test/resources/echo.pom)2
-rw-r--r--feature-lifecycle/src/test/resources/tosca-policy.json9
13 files changed, 447 insertions, 72 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) {
diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/ControllerSupport.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/ControllerSupport.java
new file mode 100644
index 00000000..1beee552
--- /dev/null
+++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/ControllerSupport.java
@@ -0,0 +1,112 @@
+/*
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 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.
+ * 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.lifecycle;
+
+import java.io.IOException;
+import java.nio.file.Paths;
+import java.util.List;
+import java.util.Properties;
+import java.util.stream.Collectors;
+import lombok.Getter;
+import lombok.NonNull;
+import org.kie.api.builder.ReleaseId;
+import org.onap.policy.drools.properties.DroolsProperties;
+import org.onap.policy.drools.system.PolicyController;
+import org.onap.policy.drools.util.KieUtils;
+
+/**
+ * Controller Test Support.
+ */
+public class ControllerSupport {
+
+ protected static final String JUNIT_KMODULE_DRL_PATH = "src/test/resources/lifecycle.drl";
+ protected static final String JUNIT_KMODULE_POM_PATH = "src/test/resources/lifecycle.pom";
+ protected static final String JUNIT_KMODULE_PATH = "src/test/resources/lifecycle.kmodule";
+ protected static final String JUNIT_KJAR_DRL_PATH =
+ "src/main/resources/kbLifecycle/org/onap/policy/drools/test/lifecycle.drl";
+
+ protected static final String POLICY_TYPE = "onap.policies.controlloop.Operational";
+ protected static final String POLICY_TYPE_VERSION = "1.0.0";
+
+ protected static final String SESSION_NAME = "junits";
+
+ @Getter
+ private final String name;
+
+ public ControllerSupport(@NonNull String name) {
+ this.name = name;
+ }
+
+ /**
+ * Create controller.
+ */
+ public PolicyController createController() throws IOException {
+ ReleaseId coordinates =
+ KieUtils.installArtifact(Paths.get(JUNIT_KMODULE_PATH).toFile(),
+ Paths.get(JUNIT_KMODULE_POM_PATH).toFile(),
+ JUNIT_KJAR_DRL_PATH,
+ Paths.get(JUNIT_KMODULE_DRL_PATH).toFile());
+
+
+ Properties controllerProps = new Properties();
+ controllerProps.put(DroolsProperties.PROPERTY_CONTROLLER_NAME, name);
+ controllerProps.put(DroolsProperties.PROPERTY_CONTROLLER_POLICY_TYPES, getPolicyType());
+ controllerProps.put(DroolsProperties.RULES_GROUPID, coordinates.getGroupId());
+ controllerProps.put(DroolsProperties.RULES_ARTIFACTID, coordinates.getArtifactId());
+ controllerProps.put(DroolsProperties.RULES_VERSION, coordinates.getVersion());
+
+ return PolicyController.factory.build(name, controllerProps);
+ }
+
+ /**
+ * Destroy the echo controller.
+ */
+ public void destroyController() {
+ PolicyController.factory.destroy(name);
+ }
+
+ /**
+ * Get controller.
+ */
+ public PolicyController getController() {
+ return PolicyController.factory.get(name);
+ }
+
+ /**
+ * Get Policy Type.
+ */
+ public static String getPolicyType() {
+ return POLICY_TYPE + ":" + POLICY_TYPE_VERSION;
+ }
+
+ /**
+ * Get facts.
+ */
+ public <T> List<T> getFacts(Class<T> clazz) {
+ return PolicyController.factory.get(name)
+ .getDrools()
+ .facts(SESSION_NAME, clazz.getCanonicalName(), false)
+ .stream()
+ .filter(clazz::isInstance)
+ .map(clazz::cast)
+ .collect(Collectors.toList());
+ }
+}
diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateActiveTest.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateActiveTest.java
index c4d47d83..32006425 100644
--- a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateActiveTest.java
+++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateActiveTest.java
@@ -27,43 +27,30 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
-import org.junit.AfterClass;
import org.junit.Before;
-import org.junit.BeforeClass;
import org.junit.Test;
import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardCoder;
import org.onap.policy.common.utils.network.NetworkUtil;
-import org.onap.policy.drools.persistence.SystemPersistence;
-import org.onap.policy.drools.utils.logging.LoggerUtil;
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.enums.PdpMessageType;
import org.onap.policy.models.pdp.enums.PdpState;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
/**
* Lifecycle State Active Test.
*/
-public class LifecycleStateActiveTest {
-
- private LifecycleFsm fsm;
-
- @BeforeClass
- public static void setUp() {
- SystemPersistence.manager.setConfigurationDir("src/test/resources");
- LoggerUtil.setLevel("org.onap.policy.common.endpoints", "WARN");
- }
-
- @AfterClass
- public static void tearDown() {
- SystemPersistence.manager.setConfigurationDir(null);
- }
+public class LifecycleStateActiveTest extends LifecycleStateRunningTest {
/**
* Start tests in the Active state.
@@ -199,7 +186,7 @@ public class LifecycleStateActiveTest {
}
@Test
- public void update() {
+ public void update() throws IOException, CoderException {
PdpUpdate update = new PdpUpdate();
update.setName(NetworkUtil.getHostname());
update.setPdpGroup("Z");
@@ -210,6 +197,9 @@ public class LifecycleStateActiveTest {
long interval = 2 * originalInterval;
update.setPdpHeartbeatIntervalMs(interval * 1000L);
+ controllerSupport.getController().start();
+ fsm.start(controllerSupport.getController());
+
assertTrue(fsm.update(update));
assertEquals(PdpState.ACTIVE, fsm.state());
@@ -217,6 +207,20 @@ public class LifecycleStateActiveTest {
assertEquals("Z", fsm.getGroup());
assertEquals("z", fsm.getSubgroup());
+ String rawPolicy =
+ new String(Files.readAllBytes(Paths.get("src/test/resources/tosca-policy.json")));
+ ToscaPolicy toscaPolicy = new StandardCoder().decode(rawPolicy, ToscaPolicy.class);
+ update.setPolicies(Arrays.asList(toscaPolicy));
+
+ assertTrue(fsm.update(update));
+ assertEquals(1, fsm.policyTypesMap.size());
+
+ List<ToscaPolicy> factPolicies = controllerSupport.getFacts(ToscaPolicy.class);
+ assertEquals(1, factPolicies.size());
+ assertEquals(toscaPolicy, factPolicies.get(0));
+ assertEquals(1, fsm.policiesMap.size());
+
+ controllerSupport.getController().stop();
fsm.shutdown();
}
}
diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStatePassiveTest.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStatePassiveTest.java
index 376eb3a7..100bcef5 100644
--- a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStatePassiveTest.java
+++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStatePassiveTest.java
@@ -27,45 +27,35 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.Arrays;
import java.util.Collections;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import org.awaitility.core.ConditionTimeoutException;
-import org.junit.AfterClass;
import org.junit.Before;
-import org.junit.BeforeClass;
import org.junit.Test;
import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardCoder;
import org.onap.policy.common.utils.network.NetworkUtil;
-import org.onap.policy.drools.persistence.SystemPersistence;
-import org.onap.policy.drools.utils.logging.LoggerUtil;
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.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.ToscaPolicyTypeIdentifier;
/**
* Lifecycle State Passive Tests.
*/
-public class LifecycleStatePassiveTest {
-
- private LifecycleFsm fsm;
-
- @BeforeClass
- public static void setUp() {
- SystemPersistence.manager.setConfigurationDir("src/test/resources");
- LoggerUtil.setLevel("org.onap.policy.common.endpoints", "WARN");
- }
-
- @AfterClass
- public static void tearDown() {
- SystemPersistence.manager.setConfigurationDir(null);
- }
+public class LifecycleStatePassiveTest extends LifecycleStateRunningTest {
/**
* Start tests in the Passive state.
@@ -87,6 +77,21 @@ public class LifecycleStatePassiveTest {
}
@Test
+ public void controller() {
+ fsm.start(controllerSupport.getController());
+ assertSame(controllerSupport.getController(),
+ fsm.getController(new ToscaPolicyTypeIdentifier(ControllerSupport.POLICY_TYPE,
+ ControllerSupport.POLICY_TYPE_VERSION)));
+
+ fsm.stop(controllerSupport.getController());
+ assertNull(fsm.getController(
+ new ToscaPolicyTypeIdentifier(ControllerSupport.POLICY_TYPE,
+ ControllerSupport.POLICY_TYPE_VERSION)));
+
+ fsm.shutdown();
+ }
+
+ @Test
public void start() {
assertEquals(0, fsm.client.getSink().getRecentEvents().length);
assertFalse(fsm.start());
@@ -163,7 +168,7 @@ public class LifecycleStatePassiveTest {
}
@Test
- public void update() {
+ public void update() throws IOException, CoderException {
PdpUpdate update = new PdpUpdate();
update.setName(NetworkUtil.getHostname());
update.setPdpGroup("Z");
@@ -181,6 +186,30 @@ public class LifecycleStatePassiveTest {
assertEquals("z", fsm.getSubgroup());
assertBasicPassive();
+ String rawPolicy =
+ new String(Files.readAllBytes(Paths.get("src/test/resources/tosca-policy.json")));
+ ToscaPolicy toscaPolicy = new StandardCoder().decode(rawPolicy, ToscaPolicy.class);
+ update.setPolicies(Arrays.asList(toscaPolicy));
+
+ assertFalse(fsm.update(update));
+
+ assertEquals(PdpState.PASSIVE, fsm.state());
+ assertEquals(interval, fsm.getStatusTimerSeconds());
+ assertEquals("Z", fsm.getGroup());
+ assertEquals("z", fsm.getSubgroup());
+ assertBasicPassive();
+
+ assertTrue(fsm.policyTypesMap.isEmpty());
+ assertTrue(fsm.policiesMap.isEmpty());
+
+ fsm.start(controllerSupport.getController());
+ assertEquals(1, fsm.policyTypesMap.size());
+ assertTrue(fsm.policiesMap.isEmpty());
+
+ assertTrue(fsm.update(update));
+ assertEquals(1, fsm.policyTypesMap.size());
+ assertTrue(fsm.policiesMap.isEmpty());
+
fsm.shutdown();
}
diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateRunningTest.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateRunningTest.java
new file mode 100644
index 00000000..d7bb6d75
--- /dev/null
+++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateRunningTest.java
@@ -0,0 +1,63 @@
+/*
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 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.
+ * 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * =============LICENSE_END========================================================
+ */
+
+package org.onap.policy.drools.lifecycle;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.onap.policy.drools.persistence.SystemPersistence;
+import org.onap.policy.drools.utils.logging.LoggerUtil;
+
+public abstract class LifecycleStateRunningTest {
+
+ private static final String CONTROLLER_NAME = "lifecycle";
+ protected static ControllerSupport controllerSupport = new ControllerSupport(CONTROLLER_NAME);
+ protected LifecycleFsm fsm;
+
+ /**
+ * Set up.
+ */
+ @BeforeClass
+ public static void setUp() throws IOException {
+ LoggerUtil.setLevel(LoggerUtil.ROOT_LOGGER, "INFO");
+ LoggerUtil.setLevel("org.onap.policy.common.endpoints", "WARN");
+ LoggerUtil.setLevel("org.onap.policy.drools", "WARN");
+ SystemPersistence.manager.setConfigurationDir("src/test/resources");
+ controllerSupport.createController();
+ }
+
+ /**
+ * Tear Down.
+ */
+ @AfterClass
+ public static void tearDown() {
+ controllerSupport.destroyController();
+ try {
+ Files.deleteIfExists(Paths.get(SystemPersistence.manager.getConfigurationPath().toString(),
+ CONTROLLER_NAME + "-controller.properties.bak"));
+ } catch (IOException e) {
+ ;
+ }
+ SystemPersistence.manager.setConfigurationDir(null);
+ }
+}
diff --git a/feature-lifecycle/src/test/resources/echo.drl b/feature-lifecycle/src/test/resources/lifecycle.drl
index c044f2cb..597661cf 100644
--- a/feature-lifecycle/src/test/resources/echo.drl
+++ b/feature-lifecycle/src/test/resources/lifecycle.drl
@@ -20,17 +20,20 @@
package org.onap.policy.drools.test;
-rule "INIT"
-lock-on-active
-when
-then
- insert("hello, I am up!");
-end
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
-rule "ECHO"
+rule "INSERT.TOSCA.POLICY"
when
- $o : Object();
+ $policy : ToscaPolicy();
then
- System.out.println("ECHO: " + $o);
- retract($o);
+ System.out.println("");
+ System.out.println("");
+ System.out.println("************************************************************************");
+ System.out.println(drools.getRule().getName() + ":");
+ System.out.println("");
+ System.out.println("Tosca Policy Type: " + $policy.getType() + " " + $policy.getTypeVersion());
+ System.out.println("Tosca Policy: " + $policy.getName() + " " + $policy.getVersion());
+ System.out.println("************************************************************************");
+ System.out.println("");
+ System.out.println("");
end
diff --git a/feature-lifecycle/src/test/resources/echo.kmodule b/feature-lifecycle/src/test/resources/lifecycle.kmodule
index 1019bd3d..8bf1ed5a 100644
--- a/feature-lifecycle/src/test/resources/echo.kmodule
+++ b/feature-lifecycle/src/test/resources/lifecycle.kmodule
@@ -20,7 +20,7 @@
-->
<kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule">
- <kbase name="controller-logs">
- <ksession name="test" />
+ <kbase name="onap.policies.type1.type2">
+ <ksession name="junits" />
</kbase>
</kmodule>
diff --git a/feature-lifecycle/src/test/resources/echo.pom b/feature-lifecycle/src/test/resources/lifecycle.pom
index 7e654793..87eafc10 100644
--- a/feature-lifecycle/src/test/resources/echo.pom
+++ b/feature-lifecycle/src/test/resources/lifecycle.pom
@@ -23,7 +23,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.onap.policy.drools.test</groupId>
- <artifactId>echo</artifactId>
+ <artifactId>lifecycle</artifactId>
<version>1.4.0-SNAPSHOT</version>
</project>
diff --git a/feature-lifecycle/src/test/resources/tosca-policy.json b/feature-lifecycle/src/test/resources/tosca-policy.json
new file mode 100644
index 00000000..5258ca1c
--- /dev/null
+++ b/feature-lifecycle/src/test/resources/tosca-policy.json
@@ -0,0 +1,9 @@
+{
+ "type": "onap.policies.controlloop.Operational",
+ "typeVersion": "1.0.0",
+ "properties": {
+ "content": "controlLoop%3A%0A%20%20version%3A%202.0.0%0A%20%20controlLoopName%3A%20ControlLoop-vCPE-48f0c2c3-a172-4192-9ae3-052274181b6e%0A%20%20trigger_policy%3A%20unique-policy-id-1-restart%0A%20%20timeout%3A%203600%0A%20%20abatement%3A%20true%0A%20%0Apolicies%3A%0A%20%20-%20id%3A%20unique-policy-id-1-restart%0A%20%20%20%20name%3A%20Restart%20the%20VM%0A%20%20%20%20description%3A%0A%20%20%20%20actor%3A%20APPC%0A%20%20%20%20recipe%3A%20Restart%0A%20%20%20%20target%3A%0A%20%20%20%20%20%20type%3A%20VM%0A%20%20%20%20retry%3A%203%0A%20%20%20%20timeout%3A%201200%0A%20%20%20%20success%3A%20final_success%0A%20%20%20%20failure%3A%20final_failure%0A%20%20%20%20failure_timeout%3A%20final_failure_timeout%0A%20%20%20%20failure_retries%3A%20final_failure_retries%0A%20%20%20%20failure_exception%3A%20final_failure_exception%0A%20%20%20%20failure_guard%3A%20final_failure_guard"
+ },
+ "name": "operational.restart",
+ "version": "1.0.0"
+}