Undeploy support for drools policies.

Issue-ID: POLICY-2388
Signed-off-by: jhh <jorge.hernandez-herrero@att.com>
Change-Id: I4c5ed6752a32c9c2391cf03dd56cb2f1abc32dfd
Signed-off-by: jhh <jorge.hernandez-herrero@att.com>
diff --git a/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/PolicyTypeNativeController.java b/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/PolicyTypeNativeController.java
index 2d9ca51..1b5e7c4 100644
--- a/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/PolicyTypeNativeController.java
+++ b/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/PolicyTypeNativeController.java
@@ -23,10 +23,16 @@
 import com.fasterxml.jackson.annotation.JsonIgnore;
 import lombok.Getter;
 import org.onap.policy.common.gson.annotation.GsonJsonIgnore;
+import org.onap.policy.common.utils.coder.CoderException;
+import org.onap.policy.drools.domain.models.controller.ControllerPolicy;
+import org.onap.policy.drools.system.PolicyControllerConstants;
 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;
 
 public class PolicyTypeNativeController implements PolicyTypeController {
+    private static final Logger logger = LoggerFactory.getLogger(PolicyTypeNativeController.class);
 
     @Getter
     protected final ToscaPolicyTypeIdentifier policyType;
@@ -48,7 +54,13 @@
 
     @Override
     public boolean undeploy(ToscaPolicy policy) {
-        // TODO
-        return true;
+        try {
+            ControllerPolicy nativePolicy = fsm.getDomainMaker().convertTo(policy, ControllerPolicy.class);
+            PolicyControllerConstants.getFactory().destroy(nativePolicy.getProperties().getControllerName());
+            return true;
+        } catch (RuntimeException | CoderException e) {
+            logger.warn("failed undeploy of policy: {}", policy);
+            return false;
+        }
     }
 }
diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/domain/models/DomainPolicyTypesTest.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/domain/models/DomainPolicyTypesTest.java
index 17bb9d4..dd70553 100644
--- a/feature-lifecycle/src/test/java/org/onap/policy/drools/domain/models/DomainPolicyTypesTest.java
+++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/domain/models/DomainPolicyTypesTest.java
@@ -188,7 +188,7 @@
         assertEquals("onap.policies.drools.Controller", controllerPolicy.getType());
         assertEquals("1.0.0", controllerPolicy.getTypeVersion());
         assertEquals("example", controllerPolicy.getMetadata().getPolicyId());
-        assertEquals("example", controllerPolicy.getProperties().getControllerName());
+        assertEquals("lifecycle", controllerPolicy.getProperties().getControllerName());
         assertEquals("DCAE_TOPIC", controllerPolicy.getProperties().getSourceTopics().get(0).getTopicName());
         assertEquals("org.onap.policy.controlloop.CanonicalOnset",
             controllerPolicy.getProperties().getSourceTopics().get(0).getSerialization().get(0).getEventClass());
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
index b80a98c..1d03124 100644
--- 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
@@ -28,6 +28,7 @@
 import java.util.concurrent.TimeUnit;
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
+import org.onap.policy.common.utils.coder.CoderException;
 import org.onap.policy.common.utils.coder.StandardCoder;
 import org.onap.policy.common.utils.time.PseudoScheduledExecutorService;
 import org.onap.policy.common.utils.time.TestTimeMulti;
@@ -36,8 +37,11 @@
 import org.onap.policy.models.pdp.concepts.PdpStatus;
 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.ToscaServiceTemplate;
 
 public abstract class LifecycleStateRunningTest {
+    private static final StandardCoder coder = new StandardCoder();
 
     private static final String CONTROLLER_NAME = "lifecycle";
     protected static ControllerSupport controllerSupport = new ControllerSupport(CONTROLLER_NAME);
@@ -106,4 +110,10 @@
     protected Callable<Boolean> isStatus(PdpState state) {
         return isStatus(state, fsm.client.getSink().getRecentEvents().length);
     }
+
+    protected ToscaPolicy getPolicyFromFile(String filePath, String policyName) throws CoderException, IOException {
+        String policyJson = Files.readString(Paths.get(filePath));
+        ToscaServiceTemplate serviceTemplate = coder.decode(policyJson, ToscaServiceTemplate.class);
+        return serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyName);
+    }
 }
diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/PolicyTypeNativeControllerTest.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/PolicyTypeNativeControllerTest.java
new file mode 100644
index 0000000..528fa7e
--- /dev/null
+++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/PolicyTypeNativeControllerTest.java
@@ -0,0 +1,82 @@
+/*
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2019-2020 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 static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+
+import java.io.IOException;
+import org.junit.Before;
+import org.junit.Test;
+import org.onap.policy.common.utils.coder.CoderException;
+import org.onap.policy.drools.domain.models.controller.ControllerPolicy;
+import org.onap.policy.drools.system.PolicyControllerConstants;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
+
+/**
+ * Native Controller Policy Test.
+ */
+public class PolicyTypeNativeControllerTest extends LifecycleStateRunningTest {
+    // Native Drools Policy
+    private static final String EXAMPLE_NATIVE_DROOLS_POLICY_NAME = "example";
+    private static final String EXAMPLE_NATIVE_DROOLS_POLICY_JSON =
+            "src/test/resources/example.policy.drools.controller.tosca.json";
+
+    private ToscaPolicy policy;
+    private ControllerPolicy controllerPolicy;
+    private PolicyTypeNativeController controller;
+
+    /**
+     * Test initialization.
+     */
+    @Before
+    public void init() throws IOException, CoderException {
+        fsm = makeFsmWithPseudoTime();
+        policy = getPolicyFromFile(EXAMPLE_NATIVE_DROOLS_POLICY_JSON, EXAMPLE_NATIVE_DROOLS_POLICY_NAME);
+        controllerPolicy = fsm.getDomainMaker().convertTo(policy, ControllerPolicy.class);
+        controller =
+                new PolicyTypeNativeController(fsm,
+                        new ToscaPolicyTypeIdentifier("onap.policies.drools.Controller", "1.0.0"));
+
+        assertTrue(controllerSupport.getController().getDrools().isBrained());
+        assertFalse(controllerSupport.getController().isAlive());
+        assertFalse(controllerSupport.getController().getDrools().isAlive());
+        assertSame(controllerSupport.getController(), PolicyControllerConstants.getFactory().get("lifecycle"));
+
+        /* start controller */
+        assertTrue(controllerSupport.getController().start());
+
+        assertTrue(controllerSupport.getController().isAlive());
+        assertTrue(controllerSupport.getController().getDrools().isAlive());
+        assertTrue(controllerSupport.getController().getDrools().isBrained());
+        assertSame(controllerSupport.getController(), PolicyControllerConstants.getFactory().get("lifecycle"));
+    }
+
+    @Test
+    public void testUndeploy() {
+        assertTrue(controller.undeploy(policy));
+        assertThatIllegalArgumentException().isThrownBy(
+            () -> PolicyControllerConstants.getFactory().get(controllerPolicy.getName()));
+    }
+}
\ No newline at end of file
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 5754f06..369284d 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
@@ -27,12 +27,9 @@
 import static org.junit.Assert.assertTrue;
 
 import java.io.IOException;
-import java.nio.file.Files;
-import java.nio.file.Paths;
 import org.junit.Before;
 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;
@@ -40,14 +37,11 @@
 import org.onap.policy.drools.system.PolicyControllerConstants;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
 
 /**
  * Rules Controller Test.
  */
 public class PolicyTypeRulesControllerTest extends LifecycleStateRunningTest {
-    private static final StandardCoder coder = new StandardCoder();
-
     // Native Drools Policy
     private static final String EXAMPLE_NATIVE_DROOLS_POLICY_NAME = "example";
     private static final String EXAMPLE_NATIVE_DROOLS_POLICY_JSON =
@@ -153,10 +147,4 @@
         assertTrue(controllerSupport.getController().getDrools() instanceof MavenDroolsController);
     }
 
-    private ToscaPolicy getPolicyFromFile(String filePath, String policyName) throws CoderException, IOException {
-        String policyJson = new String(Files.readAllBytes(Paths.get(filePath)));
-        ToscaServiceTemplate serviceTemplate = coder.decode(policyJson, ToscaServiceTemplate.class);
-        return serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyName);
-    }
-
 }
\ No newline at end of file
diff --git a/feature-lifecycle/src/test/resources/example.policy.drools.controller.tosca.json b/feature-lifecycle/src/test/resources/example.policy.drools.controller.tosca.json
index b4dd50d..f5a9151 100644
--- a/feature-lifecycle/src/test/resources/example.policy.drools.controller.tosca.json
+++ b/feature-lifecycle/src/test/resources/example.policy.drools.controller.tosca.json
@@ -12,7 +12,7 @@
                         "policy-id": "example"
                     },
                     "properties": {
-                        "controllerName": "example",
+                        "controllerName": "lifecycle",
                         "sourceTopics": [
                             {
                                 "topicName": "DCAE_TOPIC",