From 53b503a4986cc5733e10d4ac05238fe3c20aa73d Mon Sep 17 00:00:00 2001 From: jhh Date: Mon, 2 Mar 2020 15:26:22 -0600 Subject: Undeploy support for drools policies. Issue-ID: POLICY-2388 Signed-off-by: jhh Change-Id: I4c5ed6752a32c9c2391cf03dd56cb2f1abc32dfd Signed-off-by: jhh --- .../lifecycle/PolicyTypeNativeController.java | 16 ++++- .../domain/models/DomainPolicyTypesTest.java | 2 +- .../lifecycle/LifecycleStateRunningTest.java | 10 +++ .../lifecycle/PolicyTypeNativeControllerTest.java | 82 ++++++++++++++++++++++ .../lifecycle/PolicyTypeRulesControllerTest.java | 12 ---- .../example.policy.drools.controller.tosca.json | 2 +- 6 files changed, 108 insertions(+), 16 deletions(-) create mode 100644 feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/PolicyTypeNativeControllerTest.java (limited to 'feature-lifecycle') 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 2d9ca514..1b5e7c47 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 @@ package org.onap.policy.drools.lifecycle; 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 @@ public class PolicyTypeNativeController implements PolicyTypeController { @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 17bb9d49..dd70553d 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 @@ public class DomainPolicyTypesTest { 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 b80a98c7..1d03124a 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.ScheduledExecutorService; 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.drools.utils.logging.LoggerUtil; 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 @@ public abstract class LifecycleStateRunningTest { protected Callable 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 00000000..528fa7ef --- /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 5754f06e..369284dc 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.assertSame; 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.domain.models.nativ.rules.NativeDroolsPolicy; 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 @@ public class PolicyTypeRulesControllerTest extends LifecycleStateRunningTest { 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 b4dd50da..f5a9151f 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", -- cgit 1.2.3-korg