Undeploy support for native rules PDP-D policies. 30/102530/1
authorjhh <jorge.hernandez-herrero@att.com>
Fri, 28 Feb 2020 01:20:39 +0000 (19:20 -0600)
committerjhh <jorge.hernandez-herrero@att.com>
Fri, 28 Feb 2020 01:20:39 +0000 (19:20 -0600)
Issue-ID: POLICY-2388
Signed-off-by: jhh <jorge.hernandez-herrero@att.com>
Change-Id: Ic829bda9dbbb54d8883df6c642c1895cd7936823

feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/PolicyTypeRulesController.java
feature-lifecycle/src/main/resources/schemas/onap.policies.native.Drools-1.0.0.schema.json
feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/PolicyTypeRulesControllerTest.java

index 878907b..79e2ddb 100644 (file)
@@ -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());
-    }
 }
index 1dd766a..8742768 100644 (file)
@@ -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",
                             "examples": [
                                 "example"
                             ],
-                            "pattern": "^(.*)$"
+                            "pattern": "^(.+)$"
                         },
                         "version": {
                             "$id": "#/properties/properties/properties/rulesArtifact/properties/version",
                             "examples": [
                                 "1.0.0"
                             ],
-                            "pattern": "^(.*)$"
+                            "pattern": "^(.+)$"
                         }
                     }
                 },
                     "type": "object",
                     "title": "Controller",
                     "required": [
-                        "name",
-                        "version"
+                        "name"
                     ],
                     "properties": {
                         "name": {
                                 "example"
                             ],
                             "pattern": "^(.+)$"
-                        },
-                        "version": {
-                            "$id": "#/properties/properties/properties/controller/properties/version",
-                            "type": "string",
-                            "title": "Version",
-                            "examples": [
-                                "1.0.0"
-                            ],
-                            "pattern": "^(.*)$"
                         }
                     }
                 }
index 17a4ebe..5754f06 100644 (file)
@@ -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 {