From 7f1a0e6e142513087aa9385786212455a1ea0f22 Mon Sep 17 00:00:00 2001 From: jhh Date: Thu, 27 Feb 2020 19:20:39 -0600 Subject: Undeploy support for native rules PDP-D policies. Issue-ID: POLICY-2388 Signed-off-by: jhh Change-Id: Ic829bda9dbbb54d8883df6c642c1895cd7936823 --- .../lifecycle/PolicyTypeRulesController.java | 44 +++++----- .../onap.policies.native.Drools-1.0.0.schema.json | 21 ++--- .../lifecycle/PolicyTypeRulesControllerTest.java | 94 +++++++++++++--------- 3 files changed, 86 insertions(+), 73 deletions(-) diff --git a/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/PolicyTypeRulesController.java b/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/PolicyTypeRulesController.java index 878907b0..79e2ddb8 100644 --- a/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/PolicyTypeRulesController.java +++ b/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/PolicyTypeRulesController.java @@ -26,7 +26,6 @@ import org.onap.policy.common.gson.annotation.GsonJsonIgnore; import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.drools.controller.DroolsControllerConstants; import org.onap.policy.drools.domain.models.nativ.rules.NativeDroolsPolicy; -import org.onap.policy.drools.domain.models.nativ.rules.NativeDroolsRulesArtifact; import org.onap.policy.drools.protocol.configuration.DroolsConfiguration; import org.onap.policy.drools.system.PolicyController; import org.onap.policy.drools.system.PolicyControllerConstants; @@ -52,16 +51,34 @@ public class PolicyTypeRulesController implements PolicyTypeController { @Override public boolean deploy(ToscaPolicy policy) { - // TODO - return fsm.getDomainMaker().isConformant(policy); + NativeDroolsPolicy nativePolicy; + PolicyController controller; + try { + nativePolicy = fsm.getDomainMaker().convertTo(policy, NativeDroolsPolicy.class); + controller = + PolicyControllerConstants.getFactory().get(nativePolicy.getProperties().getController().getName()); + } catch (CoderException e) { + logger.warn("Invalid Policy: {}", policy); + return false; + } + + DroolsConfiguration newConfig = + new DroolsConfiguration( + nativePolicy.getProperties().getRulesArtifact().getArtifactId(), + nativePolicy.getProperties().getRulesArtifact().getGroupId(), + nativePolicy.getProperties().getRulesArtifact().getVersion()); + + PolicyControllerConstants.getFactory().patch(controller, newConfig); + return true; } @Override public boolean undeploy(ToscaPolicy policy) { PolicyController controller; try { - controller = getPolicyController( - fsm.getDomainMaker().convertTo(policy, NativeDroolsPolicy.class)); + NativeDroolsPolicy nativePolicy = fsm.getDomainMaker().convertTo(policy, NativeDroolsPolicy.class); + controller = + PolicyControllerConstants.getFactory().get(nativePolicy.getProperties().getController().getName()); } catch (RuntimeException | CoderException e) { logger.warn("Invalid Policy: {}", policy); return false; @@ -76,21 +93,4 @@ public class PolicyTypeRulesController implements PolicyTypeController { PolicyControllerConstants.getFactory().patch(controller, noConfig); return true; } - - private PolicyController getPolicyController(NativeDroolsPolicy domainPolicy) { - /* - * If the controller is present, it must have a name (schema validated) and as such, the - * controller must exist (via previously deployed controller policy). - */ - if (domainPolicy.getProperties().getController() != null) { - return PolicyControllerConstants.getFactory().get(domainPolicy.getProperties().getController().getName()); - } - - /* - * Attempt to get the controller from the rules coordinates (excluding the version). - * The rules coordinates are mandatory (per schema validation). - */ - NativeDroolsRulesArtifact rules = domainPolicy.getProperties().getRulesArtifact(); - return PolicyControllerConstants.getFactory().get(rules.getGroupId(), rules.getArtifactId()); - } } diff --git a/feature-lifecycle/src/main/resources/schemas/onap.policies.native.Drools-1.0.0.schema.json b/feature-lifecycle/src/main/resources/schemas/onap.policies.native.Drools-1.0.0.schema.json index 1dd766a2..8742768b 100644 --- a/feature-lifecycle/src/main/resources/schemas/onap.policies.native.Drools-1.0.0.schema.json +++ b/feature-lifecycle/src/main/resources/schemas/onap.policies.native.Drools-1.0.0.schema.json @@ -73,7 +73,8 @@ "type": "object", "title": "Properties", "required": [ - "rulesArtifact" + "rulesArtifact", + "controller" ], "properties": { "rulesArtifact": { @@ -93,7 +94,7 @@ "examples": [ "org.onap.policy.controlloop" ], - "pattern": "^(.*)$" + "pattern": "^(.+)$" }, "artifactId": { "$id": "#/properties/properties/properties/rulesArtifact/properties/artifactId", @@ -102,7 +103,7 @@ "examples": [ "example" ], - "pattern": "^(.*)$" + "pattern": "^(.+)$" }, "version": { "$id": "#/properties/properties/properties/rulesArtifact/properties/version", @@ -111,7 +112,7 @@ "examples": [ "1.0.0" ], - "pattern": "^(.*)$" + "pattern": "^(.+)$" } } }, @@ -120,8 +121,7 @@ "type": "object", "title": "Controller", "required": [ - "name", - "version" + "name" ], "properties": { "name": { @@ -132,15 +132,6 @@ "example" ], "pattern": "^(.+)$" - }, - "version": { - "$id": "#/properties/properties/properties/controller/properties/version", - "type": "string", - "title": "Version", - "examples": [ - "1.0.0" - ], - "pattern": "^(.*)$" } } } diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/PolicyTypeRulesControllerTest.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/PolicyTypeRulesControllerTest.java index 17a4ebed..5754f06e 100644 --- a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/PolicyTypeRulesControllerTest.java +++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/PolicyTypeRulesControllerTest.java @@ -34,6 +34,7 @@ import org.junit.Test; import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.drools.controller.DroolsControllerConstants; +import org.onap.policy.drools.controller.internal.MavenDroolsController; import org.onap.policy.drools.controller.internal.NullDroolsController; import org.onap.policy.drools.domain.models.nativ.rules.NativeDroolsPolicy; import org.onap.policy.drools.system.PolicyControllerConstants; @@ -52,40 +53,28 @@ public class PolicyTypeRulesControllerTest extends LifecycleStateRunningTest { private static final String EXAMPLE_NATIVE_DROOLS_POLICY_JSON = "src/test/resources/example.policy.native.drools.tosca.json"; + private ToscaPolicy policy; + private NativeDroolsPolicy nativePolicy; + private PolicyTypeRulesController controller; + /** * Test Set initialization. */ @Before - public void init() { + public void init() throws IOException, CoderException { fsm = makeFsmWithPseudoTime(); - } - - @Test - public void testDeploy() { - // TODO - } + policy = getPolicyFromFile(EXAMPLE_NATIVE_DROOLS_POLICY_JSON, EXAMPLE_NATIVE_DROOLS_POLICY_NAME); + nativePolicy = fsm.getDomainMaker().convertTo(policy, NativeDroolsPolicy.class); + controller = + new PolicyTypeRulesController(fsm, + new ToscaPolicyTypeIdentifier("onap.policies.native.Drools", "1.0.0")); - @Test - public void testUndeploy() throws IOException, CoderException { assertTrue(controllerSupport.getController().getDrools().isBrained()); assertFalse(controllerSupport.getController().isAlive()); assertFalse(controllerSupport.getController().getDrools().isAlive()); assertSame(controllerSupport.getController(), PolicyControllerConstants.getFactory().get("lifecycle")); - ToscaPolicy policy = getPolicyFromFile(EXAMPLE_NATIVE_DROOLS_POLICY_JSON, EXAMPLE_NATIVE_DROOLS_POLICY_NAME); - NativeDroolsPolicy nativePolicy = fsm.getDomainMaker().convertTo(policy, NativeDroolsPolicy.class); - - assertSame(controllerSupport.getController(), - PolicyControllerConstants.getFactory().get( - nativePolicy.getProperties().getRulesArtifact().getGroupId(), - nativePolicy.getProperties().getRulesArtifact().getArtifactId())); - assertEquals(controllerSupport.getController().getDrools().getGroupId(), - nativePolicy.getProperties().getRulesArtifact().getGroupId()); - assertEquals(controllerSupport.getController().getDrools().getArtifactId(), - nativePolicy.getProperties().getRulesArtifact().getArtifactId()); - assertEquals(controllerSupport.getController().getDrools().getVersion(), - nativePolicy.getProperties().getRulesArtifact().getVersion()); - + /* start controller */ assertTrue(controllerSupport.getController().start()); assertTrue(controllerSupport.getController().isAlive()); @@ -102,33 +91,66 @@ public class PolicyTypeRulesControllerTest extends LifecycleStateRunningTest { nativePolicy.getProperties().getRulesArtifact().getArtifactId()); assertEquals(controllerSupport.getController().getDrools().getVersion(), nativePolicy.getProperties().getRulesArtifact().getVersion()); + } - PolicyTypeRulesController controller = - new PolicyTypeRulesController(makeFsmWithPseudoTime(), - new ToscaPolicyTypeIdentifier("onap.policies.native.Drools", "1.0.0")); + @Test + public void testUndeployDeploy() { + undeploy(); + deploy(); + + PolicyControllerConstants.getFactory().destroy("lifecycle"); + assertThatIllegalArgumentException().isThrownBy(() -> PolicyControllerConstants.getFactory().get("lifecycle")); + } + + private void undeploy() { + assertTrue(controller.undeploy(policy)); + assertUndeployed(); + + /* idempotence */ assertTrue(controller.undeploy(policy)); + assertUndeployed(); + } + + + private void deploy() { + assertTrue(controller.deploy(policy)); + assertDeployed(); + /* idempotence */ + assertTrue(controller.deploy(policy)); + assertDeployed(); + + // TODO: test a point version upgrade + } + + private void assertUndeployed() { assertFalse(controllerSupport.getController().getDrools().isBrained()); assertFalse(controllerSupport.getController().getDrools().isAlive()); assertTrue(controllerSupport.getController().isAlive()); assertSame(controllerSupport.getController(), PolicyControllerConstants.getFactory().get("lifecycle")); - assertThatIllegalArgumentException().isThrownBy(() -> PolicyControllerConstants.getFactory().get( - nativePolicy.getProperties().getRulesArtifact().getGroupId(), - nativePolicy.getProperties().getRulesArtifact().getArtifactId())); + assertThatIllegalArgumentException().isThrownBy(() -> PolicyControllerConstants.getFactory() + .get(nativePolicy.getProperties() + .getRulesArtifact().getGroupId(), + nativePolicy.getProperties() + .getRulesArtifact() + .getArtifactId())); assertTrue(controllerSupport.getController().getDrools() instanceof NullDroolsController); assertEquals(DroolsControllerConstants.NO_GROUP_ID, controllerSupport.getController().getDrools().getGroupId()); assertEquals(DroolsControllerConstants.NO_ARTIFACT_ID, controllerSupport.getController().getDrools().getArtifactId()); assertEquals(DroolsControllerConstants.NO_VERSION, controllerSupport.getController().getDrools().getVersion()); - - assertTrue(controller.undeploy(policy)); - PolicyControllerConstants.getFactory().destroy("lifecycle"); - assertThatIllegalArgumentException().isThrownBy(() -> PolicyControllerConstants.getFactory().get("lifecycle")); } - @Test - public void testGetPolicyType() { - // TODO + private void assertDeployed() { + assertTrue(controllerSupport.getController().getDrools().isBrained()); + assertTrue(controllerSupport.getController().getDrools().isAlive()); + assertTrue(controllerSupport.getController().isAlive()); + assertSame(controllerSupport.getController(), PolicyControllerConstants.getFactory().get("lifecycle")); + assertSame(controllerSupport.getController(), + PolicyControllerConstants.getFactory().get( + nativePolicy.getProperties().getRulesArtifact().getGroupId(), + nativePolicy.getProperties().getRulesArtifact().getArtifactId())); + assertTrue(controllerSupport.getController().getDrools() instanceof MavenDroolsController); } private ToscaPolicy getPolicyFromFile(String filePath, String policyName) throws CoderException, IOException { -- cgit 1.2.3-korg