From 6dfab64262ea5816c761042192c84e498a3177ab Mon Sep 17 00:00:00 2001 From: jhh Date: Wed, 12 Feb 2020 18:20:06 -0600 Subject: tosca compliant op policy support + vcpe test Tosca Compliant and Toscal Legacy Operational policies are both supported for backwards compatibility. vCPE usecase junits to support 2 equivalent policies, one tosca compliant and the other one legacy. Issue-ID: POLICY-2360 Signed-off-by: jhh Change-Id: Iafbfc92bbec42e6a3fe4ceb4a9a17c85e636ea14 Signed-off-by: jhh --- controlloop/common/eventmanager/pom.xml | 402 +++++++++++---------- .../controlloop/drl/legacy/ControlLoopParams.java | 3 +- .../eventmanager/ControlLoopEventManager.java | 46 ++- .../processor/ControlLoopProcessor.java | 109 +++++- .../policy/controlloop/utils/ControlLoopUtils.java | 32 +- .../drl/legacy/ControlLoopParamsTest.java | 26 +- .../eventmanager/ControlLoopEventManagerTest.java | 120 +++--- .../processor/ControlLoopProcessorTest.java | 34 +- .../controlloop/utils/ControlLoopUtilsTest.java | 20 +- .../resources/tosca-policy-compliant-vcpe.json | 37 ++ .../test/resources/tosca-policy-legacy-vcpe.json | 9 + .../tosca-policy-operational-restart.json | 9 - 12 files changed, 492 insertions(+), 355 deletions(-) create mode 100644 controlloop/common/eventmanager/src/test/resources/tosca-policy-compliant-vcpe.json create mode 100644 controlloop/common/eventmanager/src/test/resources/tosca-policy-legacy-vcpe.json delete mode 100644 controlloop/common/eventmanager/src/test/resources/tosca-policy-operational-restart.json (limited to 'controlloop/common/eventmanager') diff --git a/controlloop/common/eventmanager/pom.xml b/controlloop/common/eventmanager/pom.xml index 7b3985551..23ac4a227 100644 --- a/controlloop/common/eventmanager/pom.xml +++ b/controlloop/common/eventmanager/pom.xml @@ -20,207 +20,213 @@ ============LICENSE_END========================================================= --> - - 4.0.0 + + 4.0.0 - - org.onap.policy.drools-applications.controlloop.common - drools-applications-common - 1.6.0-SNAPSHOT - + + org.onap.policy.drools-applications.controlloop.common + drools-applications-common + 1.6.0-SNAPSHOT + - eventmanager + eventmanager - - - org.eclipse.persistence - org.eclipse.persistence.jpa - provided - - - commons-io - commons-io - 2.5 - provided - - - org.onap.policy.models.policy-models-interactions.model-actors - actorServiceProvider - ${policy.models.version} - provided - - - org.onap.policy.models.policy-models-interactions.model-actors - actor.sdnr - ${policy.models.version} - provided - - - org.onap.policy.models.policy-models-interactions.model-actors - actor.appc - ${policy.models.version} - provided - - - org.onap.policy.models.policy-models-interactions.model-actors - actor.so - ${policy.models.version} - provided - - - org.onap.policy.models.policy-models-interactions.model-actors - actor.vfc - ${policy.models.version} - provided - - - org.onap.policy.models.policy-models-interactions.model-actors - actor.sdnc - ${policy.models.version} - provided - - - org.onap.policy.models.policy-models-interactions.model-actors - actor.appclcm - ${policy.models.version} - provided - - - org.onap.policy.models.policy-models-interactions.model-actors - actor.cds - ${policy.models.version} - provided - - - org.onap.policy.drools-applications.controlloop.common - database - ${project.version} - - - org.onap.policy.drools-applications.controlloop.common - guard - ${project.version} - provided - - - org.onap.policy.models.policy-models-interactions.model-impl - events - ${policy.models.version} - provided - - - org.onap.policy.models.policy-models-interactions.model-impl - sdnr - ${policy.models.version} - provided - - - org.onap.policy.models.policy-models-interactions.model-impl - appc - ${policy.models.version} - provided - - - org.onap.policy.models.policy-models-interactions.model-impl - vfc - ${policy.models.version} - provided - - - org.onap.policy.models.policy-models-interactions.model-impl - sdnc - ${policy.models.version} - provided - - - org.onap.policy.models.policy-models-interactions.model-impl - appclcm - ${policy.models.version} - provided - - - org.onap.policy.models.policy-models-interactions.model-impl - cds - ${policy.models.version} - provided - - - org.onap.ccsdk.cds.components - proto-definition - - - junit - junit - test - - - org.powermock - powermock-api-mockito2 - test - - - org.assertj - assertj-core - test - - - com.google.code.gson - gson - provided - - - org.onap.policy.models.policy-models-interactions.model-impl - sdc - ${policy.models.version} - provided - - - org.onap.policy.models.policy-models-interactions.model-impl - aai - ${policy.models.version} - provided - - - org.onap.policy.models.policy-models-interactions.model-impl - so - ${policy.models.version} - provided - - - org.apache.httpcomponents - httpclient - provided - - - org.onap.policy.common - policy-endpoints - ${version.policy.common} - provided - - - org.onap.policy.drools-pdp - policy-management - ${version.policy.drools-pdp} - provided - - - org.onap.policy.common - utils-test - ${version.policy.common} - test - - - org.onap.policy.models.policy-models-interactions - simulators - ${policy.models.version} - test - - - com.h2database - h2 - test - - + + + org.eclipse.persistence + org.eclipse.persistence.jpa + provided + + + commons-io + commons-io + 2.5 + provided + + + org.onap.policy.models.policy-models-interactions.model-actors + actorServiceProvider + ${policy.models.version} + provided + + + org.onap.policy.models.policy-models-interactions.model-actors + actor.sdnr + ${policy.models.version} + provided + + + org.onap.policy.models.policy-models-interactions.model-actors + actor.appc + ${policy.models.version} + provided + + + org.onap.policy.models.policy-models-interactions.model-actors + actor.so + ${policy.models.version} + provided + + + org.onap.policy.models.policy-models-interactions.model-actors + actor.vfc + ${policy.models.version} + provided + + + org.onap.policy.models.policy-models-interactions.model-actors + actor.sdnc + ${policy.models.version} + provided + + + org.onap.policy.models.policy-models-interactions.model-actors + actor.appclcm + ${policy.models.version} + provided + + + org.onap.policy.models.policy-models-interactions.model-actors + actor.cds + ${policy.models.version} + provided + + + org.onap.policy.drools-applications.controlloop.common + database + ${project.version} + + + org.onap.policy.drools-applications.controlloop.common + guard + ${project.version} + provided + + + org.onap.policy.models.policy-models-interactions.model-impl + events + ${policy.models.version} + provided + + + org.onap.policy.models.policy-models-interactions.model-impl + sdnr + ${policy.models.version} + provided + + + org.onap.policy.models.policy-models-interactions.model-impl + appc + ${policy.models.version} + provided + + + org.onap.policy.models.policy-models-interactions.model-impl + vfc + ${policy.models.version} + provided + + + org.onap.policy.models.policy-models-interactions.model-impl + sdnc + ${policy.models.version} + provided + + + org.onap.policy.models.policy-models-interactions.model-impl + appclcm + ${policy.models.version} + provided + + + org.onap.policy.models.policy-models-interactions.model-impl + cds + ${policy.models.version} + provided + + + org.onap.ccsdk.cds.components + proto-definition + + + junit + junit + test + + + org.powermock + powermock-api-mockito2 + test + + + org.assertj + assertj-core + test + + + com.google.code.gson + gson + provided + + + org.onap.policy.models.policy-models-interactions.model-impl + sdc + ${policy.models.version} + provided + + + org.onap.policy.models.policy-models-interactions.model-impl + aai + ${policy.models.version} + provided + + + org.onap.policy.models.policy-models-interactions.model-impl + so + ${policy.models.version} + provided + + + org.apache.httpcomponents + httpclient + provided + + + org.onap.policy.common + policy-endpoints + ${version.policy.common} + provided + + + org.onap.policy.drools-pdp + policy-management + ${version.policy.drools-pdp} + provided + + + org.onap.policy.common + utils-test + ${version.policy.common} + test + + + org.onap.policy.models.policy-models-interactions + simulators + ${policy.models.version} + test + + + com.h2database + h2 + test + + + org.onap.policy.drools-applications.controlloop.common + domains + 1.6.0-SNAPSHOT + + diff --git a/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/drl/legacy/ControlLoopParams.java b/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/drl/legacy/ControlLoopParams.java index 6e24e26bf..769a6d971 100644 --- a/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/drl/legacy/ControlLoopParams.java +++ b/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/drl/legacy/ControlLoopParams.java @@ -23,6 +23,7 @@ package org.onap.policy.controlloop.drl.legacy; import java.io.Serializable; import lombok.Data; import lombok.NoArgsConstructor; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; @Data @NoArgsConstructor @@ -31,8 +32,8 @@ public class ControlLoopParams implements Serializable { private static final long serialVersionUID = 970755684770982776L; private String closedLoopControlName; - private String controlLoopYaml; private String policyName; private String policyScope; private String policyVersion; + private ToscaPolicy toscaPolicy; } diff --git a/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ControlLoopEventManager.java b/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ControlLoopEventManager.java index f297a8f6b..6e88ce982 100644 --- a/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ControlLoopEventManager.java +++ b/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ControlLoopEventManager.java @@ -55,6 +55,7 @@ import org.onap.policy.drools.core.lock.LockImpl; import org.onap.policy.drools.core.lock.LockState; import org.onap.policy.drools.system.PolicyEngineConstants; import org.onap.policy.drools.utils.Pair; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; import org.onap.policy.rest.RestManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -234,19 +235,7 @@ public class ControlLoopEventManager implements Serializable { return notification; } - // - // At this point we are good to go with this event - // - this.onset = event; - this.numOnsets = 1; - // - // - // Set ourselves as active - // - this.isActivated = true; - - notification.setNotification(ControlLoopNotificationType.ACTIVE); - return notification; + return postActivate(event, notification); } /** @@ -287,6 +276,34 @@ public class ControlLoopEventManager implements Serializable { return rejectNotification(event, e.getMessage()); } + return postActivate(event, notification); + } + + /** + * Activate a control loop event. + * + * @param toscaPolicy the tosca policy + * @param event the event + * @return the VirtualControlLoopNotification + */ + public VirtualControlLoopNotification activate(ToscaPolicy toscaPolicy, VirtualControlLoopEvent event) { + VirtualControlLoopNotification notification = preActivationChecks(event); + if (notification.getNotification() == ControlLoopNotificationType.REJECTED) { + return notification; + } + + try { + this.processor = new ControlLoopProcessor(toscaPolicy); + } catch (ControlLoopException e) { + logger.error("{}: activate from Tosca Policy threw: ", this, e); + return rejectNotification(event, e.getMessage()); + } + + return postActivate(event, notification); + } + + private VirtualControlLoopNotification postActivate( + VirtualControlLoopEvent event, VirtualControlLoopNotification notification) { // // At this point we are good to go with this event // @@ -298,9 +315,6 @@ public class ControlLoopEventManager implements Serializable { // this.isActivated = true; - // - // - // notification.setNotification(ControlLoopNotificationType.ACTIVE); return notification; } diff --git a/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/processor/ControlLoopProcessor.java b/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/processor/ControlLoopProcessor.java index ac684fcda..4cff616a0 100644 --- a/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/processor/ControlLoopProcessor.java +++ b/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/processor/ControlLoopProcessor.java @@ -1,8 +1,8 @@ /*- * ============LICENSE_START======================================================= - * controlloop processor + * ONAP * ================================================================================ - * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-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. @@ -21,22 +21,41 @@ package org.onap.policy.controlloop.processor; import java.io.Serializable; +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; +import java.util.stream.Collectors; +import lombok.Getter; +import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.controlloop.ControlLoopException; +import org.onap.policy.controlloop.drl.legacy.ControlLoopParams; import org.onap.policy.controlloop.policy.ControlLoop; import org.onap.policy.controlloop.policy.ControlLoopPolicy; import org.onap.policy.controlloop.policy.FinalResult; import org.onap.policy.controlloop.policy.Policy; +import org.onap.policy.controlloop.policy.PolicyParam; import org.onap.policy.controlloop.policy.PolicyResult; +import org.onap.policy.controlloop.policy.Target; +import org.onap.policy.controlloop.policy.TargetType; +import org.onap.policy.drools.domain.models.DroolsPolicy; +import org.onap.policy.drools.models.domain.legacy.LegacyPolicy; +import org.onap.policy.drools.models.domain.operational.OperationalPolicy; +import org.onap.policy.drools.system.PolicyEngineConstants; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.constructor.CustomClassLoaderConstructor; public class ControlLoopProcessor implements Serializable { private static final long serialVersionUID = 1L; - - private final String yaml; + private final ControlLoopPolicy policy; private String currentNestedPolicyId = null; + @Getter + private ToscaPolicy toscaOpPolicy; + + @Getter + private DroolsPolicy domainOpPolicy; + /** * Construct an instance from yaml. * @@ -44,11 +63,10 @@ public class ControlLoopProcessor implements Serializable { * @throws ControlLoopException if an error occurs */ public ControlLoopProcessor(String yaml) throws ControlLoopException { - this.yaml = yaml; try { final Yaml y = new Yaml(new CustomClassLoaderConstructor(ControlLoopPolicy.class, ControlLoopPolicy.class.getClassLoader())); - final Object obj = y.load(this.yaml); + final Object obj = y.load(yaml); this.policy = (ControlLoopPolicy) obj; this.currentNestedPolicyId = this.policy.getControlLoop().getTrigger_policy(); @@ -60,6 +78,85 @@ public class ControlLoopProcessor implements Serializable { } } + /** + * Create an instance from a Tosca Policy. + */ + public ControlLoopProcessor(ToscaPolicy toscaPolicy) throws ControlLoopException { + try { + // TODO: automate policy type to models mapping + this.policy = + ("onap.policies.controlloop.Operational".equals(toscaPolicy.getType())) + ? buildPolicyFromToscaLegacy(toscaPolicy) + : buildPolicyFromToscaCompliant(toscaPolicy); + + this.currentNestedPolicyId = this.policy.getControlLoop().getTrigger_policy(); + this.toscaOpPolicy = toscaPolicy; + } catch (RuntimeException | CoderException | UnsupportedEncodingException e) { + throw new ControlLoopException(e); + } + } + + protected ControlLoopPolicy buildPolicyFromToscaLegacy(ToscaPolicy policy) + throws UnsupportedEncodingException, CoderException { + LegacyPolicy legacyPolicy = + PolicyEngineConstants.getManager().getDomainMaker().convertTo(policy, LegacyPolicy.class); + this.domainOpPolicy = legacyPolicy; + String decodedPolicy = URLDecoder.decode(legacyPolicy.getProperties().getContent(), "UTF-8"); + return new Yaml( + new CustomClassLoaderConstructor( + ControlLoopPolicy.class, ControlLoopPolicy.class.getClassLoader())).load(decodedPolicy); + } + + protected ControlLoopPolicy buildPolicyFromToscaCompliant(ToscaPolicy policy) throws CoderException { + OperationalPolicy domainPolicy = + PolicyEngineConstants.getManager().getDomainMaker().convertTo(policy, OperationalPolicy.class); + + ControlLoopPolicy backwardsCompatiblePolicy = new ControlLoopPolicy(); + + // @formatter:off + backwardsCompatiblePolicy.setPolicies( + domainPolicy.getProperties().getOperations().stream().map(operation -> new Policy( + PolicyParam.builder() + .id(operation.getId()) + .name(operation.getActorOperation().getOperation()) + .description(operation.getDescription()) + .actor(operation.getActorOperation().getActor()) + .payload(operation.getActorOperation().getPayload()) + .recipe(operation.getActorOperation().getOperation()) + .retries(operation.getRetries()) + .timeout(operation.getTimeout()) + .target(new Target(TargetType.valueOf(operation.getActorOperation().getTarget().getType()), + operation.getActorOperation().getTarget().getResourceId())).build())) + .collect(Collectors.toList())); + // @formatter:on + + ControlLoop controlLoop = new ControlLoop(); + controlLoop.setAbatement(domainPolicy.getProperties().isAbatement()); + controlLoop.setControlLoopName(domainPolicy.getProperties().getId()); + controlLoop.setTimeout(domainPolicy.getProperties().getTimeout()); + controlLoop.setTrigger_policy(domainPolicy.getProperties().getTrigger()); + controlLoop.setVersion(domainPolicy.getVersion()); + + backwardsCompatiblePolicy.setControlLoop(controlLoop); + this.domainOpPolicy = domainPolicy; + return backwardsCompatiblePolicy; + } + + /** + * Get ControlLoopParams. + */ + public ControlLoopParams getControlLoopParams() { + ControlLoopParams controlLoopParams = new ControlLoopParams(); + + controlLoopParams.setClosedLoopControlName(this.getControlLoop().getControlLoopName()); + controlLoopParams.setPolicyScope(domainOpPolicy.getType() + ":" + domainOpPolicy.getTypeVersion()); + controlLoopParams.setPolicyName(domainOpPolicy.getName()); + controlLoopParams.setPolicyVersion(domainOpPolicy.getVersion()); + controlLoopParams.setToscaPolicy(toscaOpPolicy); + + return controlLoopParams; + } + public ControlLoop getControlLoop() { return this.policy.getControlLoop(); } diff --git a/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/utils/ControlLoopUtils.java b/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/utils/ControlLoopUtils.java index cfa4b3d3b..b5d95fed7 100644 --- a/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/utils/ControlLoopUtils.java +++ b/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/utils/ControlLoopUtils.java @@ -18,9 +18,6 @@ package org.onap.policy.controlloop.utils; -import java.io.UnsupportedEncodingException; -import java.net.URLDecoder; -import org.apache.commons.lang3.StringUtils; import org.onap.policy.controlloop.ControlLoopException; import org.onap.policy.controlloop.drl.legacy.ControlLoopParams; import org.onap.policy.controlloop.processor.ControlLoopProcessor; @@ -34,7 +31,6 @@ import org.slf4j.LoggerFactory; public class ControlLoopUtils { public static final Logger logger = LoggerFactory.getLogger(ControlLoopUtils.class); - public static final String TOSCA_POLICY_PROPERTY_CONTENT = "content"; private ControlLoopUtils() { super(); @@ -45,35 +41,11 @@ public class ControlLoopUtils { */ public static ControlLoopParams toControlLoopParams(ToscaPolicy policy) { - // TODO: ControlLoopParams class should be moved to this repo and take Tosca Policy in a constructor. - /* No exceptions are thrown to keep the DRL simpler */ try { - if (policy == null || policy.getProperties() == null - || policy.getProperties().get(TOSCA_POLICY_PROPERTY_CONTENT) == null) { - logger.error("Invalid Policy: {}", policy); - return null; - } - - String encodedPolicy = policy.getProperties().get(TOSCA_POLICY_PROPERTY_CONTENT).toString(); - String decodedPolicy = URLDecoder.decode(encodedPolicy, "UTF-8"); - - ControlLoopProcessor controlLoopProcessor = new ControlLoopProcessor(decodedPolicy); - if (controlLoopProcessor.getControlLoop() == null - || StringUtils.isEmpty(controlLoopProcessor.getControlLoop().getControlLoopName())) { - return null; - } - - ControlLoopParams controlLoopParams = new ControlLoopParams(); - controlLoopParams.setClosedLoopControlName(controlLoopProcessor.getControlLoop().getControlLoopName()); - controlLoopParams.setControlLoopYaml(encodedPolicy); - controlLoopParams.setPolicyScope(policy.getType() + ":" + policy.getTypeVersion()); - controlLoopParams.setPolicyName(policy.getName()); - controlLoopParams.setPolicyVersion(policy.getVersion()); - - return controlLoopParams; - } catch (ControlLoopException | RuntimeException | UnsupportedEncodingException e) { + return new ControlLoopProcessor(policy).getControlLoopParams(); + } catch (ControlLoopException | RuntimeException e) { logger.error("Invalid Policy because of {}: {}", e.getMessage(), policy, e); return null; } diff --git a/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/drl/legacy/ControlLoopParamsTest.java b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/drl/legacy/ControlLoopParamsTest.java index 7bb2c5c96..f636e08f6 100644 --- a/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/drl/legacy/ControlLoopParamsTest.java +++ b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/drl/legacy/ControlLoopParamsTest.java @@ -22,6 +22,12 @@ package org.onap.policy.controlloop.drl.legacy; import static org.junit.Assert.assertEquals; +import com.openpojo.reflection.PojoClass; +import com.openpojo.reflection.impl.PojoClassFactory; +import com.openpojo.validation.Validator; +import com.openpojo.validation.ValidatorBuilder; +import com.openpojo.validation.test.impl.GetterTester; +import com.openpojo.validation.test.impl.SetterTester; import org.junit.Before; import org.junit.Test; @@ -30,7 +36,6 @@ public class ControlLoopParamsTest { private static final String POLICY_NAME = "m"; private static final String POLICY_SCOPE = "s"; private static final String POLICY_VERSION = "v"; - private static final String CONTROL_LOOP_YAML = "y"; private ControlLoopParams clp = new ControlLoopParams(); @@ -43,17 +48,19 @@ public class ControlLoopParamsTest { clp.setPolicyName(POLICY_NAME); clp.setPolicyScope(POLICY_SCOPE); clp.setPolicyVersion(POLICY_VERSION); - clp.setControlLoopYaml(CONTROL_LOOP_YAML); } @Test - public void getClosedLoopControlName() { - assertEquals(CONTROL_LOOP_NAME, clp.getClosedLoopControlName()); + public void testPojo() { + PojoClass controlLoopParams = PojoClassFactory.getPojoClass(ControlLoopParams.class); + Validator validator = ValidatorBuilder.create() + .with(new SetterTester(), new GetterTester()).build(); + validator.validate(controlLoopParams); } @Test - public void getControlLoopYaml() { - assertEquals(CONTROL_LOOP_YAML, clp.getControlLoopYaml()); + public void getClosedLoopControlName() { + assertEquals(CONTROL_LOOP_NAME, clp.getClosedLoopControlName()); } @Test @@ -77,12 +84,6 @@ public class ControlLoopParamsTest { assertEquals(CONTROL_LOOP_NAME.toUpperCase(), clp.getClosedLoopControlName()); } - @Test - public void setControlLoopYaml() { - clp.setControlLoopYaml(CONTROL_LOOP_YAML.toUpperCase()); - assertEquals(CONTROL_LOOP_YAML.toUpperCase(), clp.getControlLoopYaml()); - } - @Test public void setPolicyName() { clp.setPolicyName(POLICY_NAME.toUpperCase()); @@ -108,7 +109,6 @@ public class ControlLoopParamsTest { other.setPolicyName(POLICY_NAME); other.setPolicyScope(POLICY_SCOPE); other.setPolicyVersion(POLICY_VERSION); - other.setControlLoopYaml(CONTROL_LOOP_YAML); assertEquals(clp, other); assertEquals(clp.hashCode(), other.hashCode()); diff --git a/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/ControlLoopEventManagerTest.java b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/ControlLoopEventManagerTest.java index 21b082c82..266ad1ac9 100644 --- a/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/ControlLoopEventManagerTest.java +++ b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/ControlLoopEventManagerTest.java @@ -1,8 +1,8 @@ /*- * ============LICENSE_START======================================================= - * unit test + * ONAP * ================================================================================ - * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-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. @@ -39,11 +39,14 @@ import java.io.IOException; import java.io.InputStream; import java.io.Serializable; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; import java.time.Instant; import java.util.HashMap; import java.util.Map; import java.util.UUID; import org.apache.commons.io.IOUtils; +import org.jetbrains.annotations.NotNull; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; @@ -51,6 +54,8 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.common.utils.io.Serializer; import org.onap.policy.controlloop.ControlLoopEventStatus; import org.onap.policy.controlloop.ControlLoopException; @@ -64,6 +69,7 @@ import org.onap.policy.drools.core.lock.Lock; import org.onap.policy.drools.core.lock.LockCallback; import org.onap.policy.drools.system.PolicyEngineConstants; import org.onap.policy.drools.utils.Pair; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; import org.powermock.reflect.Whitebox; public class ControlLoopEventManagerTest { @@ -160,16 +166,7 @@ public class ControlLoopEventManagerTest { @Test public void testAlreadyActivated() { - UUID requestId = UUID.randomUUID(); - VirtualControlLoopEvent event = new VirtualControlLoopEvent(); - event.setClosedLoopControlName(TWO_ONSET_TEST); - event.setRequestId(requestId); - event.setTarget(VNF_ID); - event.setClosedLoopAlarmStart(Instant.now()); - event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET); - event.setAai(new HashMap<>()); - event.getAai().put(VNF_NAME, ONSET_ONE); - event.setTargetType(ControlLoopTargetType.VNF); + VirtualControlLoopEvent event = getOnsetEvent(); ControlLoopEventManager manager = makeManager(event); manager.setActivated(true); @@ -178,23 +175,13 @@ public class ControlLoopEventManagerTest { } @Test - public void testActivationYaml() throws IOException { - - UUID requestId = UUID.randomUUID(); - VirtualControlLoopEvent event = new VirtualControlLoopEvent(); - event.setClosedLoopControlName(TWO_ONSET_TEST); - event.setRequestId(requestId); - event.setTarget(VNF_ID); - event.setClosedLoopAlarmStart(Instant.now()); - event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET); - event.setAai(new HashMap<>()); - event.getAai().put(VNF_NAME, ONSET_ONE); - event.setTargetType(ControlLoopTargetType.VNF); + public void testActivationYaml() throws IOException, CoderException { + VirtualControlLoopEvent event = getOnsetEvent(); ControlLoopEventManager manager = makeManager(event); // Null YAML should fail - VirtualControlLoopNotification notificationNull = manager.activate(null, event); + VirtualControlLoopNotification notificationNull = manager.activate((String) null, event); assertNotNull(notificationNull); assertEquals(ControlLoopNotificationType.REJECTED, notificationNull.getNotification()); @@ -225,18 +212,33 @@ public class ControlLoopEventManagerTest { assertEquals(ControlLoopNotificationType.REJECTED, notificationActive.getNotification()); } + @Test + public void testActivateToscaLegacy() throws IOException, CoderException { + String policy = + new String(Files.readAllBytes(Paths.get("src/test/resources/tosca-policy-legacy-vcpe.json"))); + ToscaPolicy toscaPolicy = new StandardCoder().decode(policy, ToscaPolicy.class); + + VirtualControlLoopEvent event = getOnsetEvent(); + ControlLoopEventManager manager = makeManager(event); + + // trigger a reject by passing the wrong policy type + toscaPolicy.setType("onap.policies.controlloop.operational.common.Drools"); + VirtualControlLoopNotification notification = manager.activate(toscaPolicy, event); + assertEquals(ControlLoopNotificationType.REJECTED, notification.getNotification()); + + // place back correct policy type + toscaPolicy.setType("onap.policies.controlloop.Operational"); + notification = manager.activate(toscaPolicy, event); + assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification()); + + // another activate should fail + notification = manager.activate(toscaPolicy, event); + assertEquals(ControlLoopNotificationType.REJECTED, notification.getNotification()); + } + @Test public void testControlLoopFinal() throws Exception { - UUID requestId = UUID.randomUUID(); - VirtualControlLoopEvent event = new VirtualControlLoopEvent(); - event.setClosedLoopControlName(TWO_ONSET_TEST); - event.setRequestId(requestId); - event.setTarget(VNF_ID); - event.setClosedLoopAlarmStart(Instant.now()); - event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET); - event.setAai(new HashMap<>()); - event.getAai().put(VNF_NAME, ONSET_ONE); - event.setTargetType(ControlLoopTargetType.VNF); + VirtualControlLoopEvent event = getOnsetEvent(); ControlLoopEventManager manager = makeManager(event); ControlLoopEventManager manager2 = manager; @@ -293,6 +295,21 @@ public class ControlLoopEventManagerTest { assertEquals(ControlLoopNotificationType.FINAL_FAILURE, clfNotification.getNotification()); } + @NotNull + private VirtualControlLoopEvent getOnsetEvent() { + UUID requestId = UUID.randomUUID(); + VirtualControlLoopEvent event = new VirtualControlLoopEvent(); + event.setClosedLoopControlName(TWO_ONSET_TEST); + event.setRequestId(requestId); + event.setTarget(VNF_ID); + event.setClosedLoopAlarmStart(Instant.now()); + event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET); + event.setAai(new HashMap<>()); + event.getAai().put(VNF_NAME, ONSET_ONE); + event.setTargetType(ControlLoopTargetType.VNF); + return event; + } + @Test public void testProcessControlLoop() throws Exception { UUID requestId = UUID.randomUUID(); @@ -633,16 +650,7 @@ public class ControlLoopEventManagerTest { @Test public void testControlLoopTimeout() throws IOException { - UUID requestId = UUID.randomUUID(); - VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent(); - onsetEvent.setClosedLoopControlName(TWO_ONSET_TEST); - onsetEvent.setRequestId(requestId); - onsetEvent.setTarget(VNF_ID); - onsetEvent.setClosedLoopAlarmStart(Instant.now()); - onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET); - onsetEvent.setAai(new HashMap<>()); - onsetEvent.getAai().put(VNF_NAME, ONSET_ONE); - onsetEvent.setTargetType(ControlLoopTargetType.VNF); + VirtualControlLoopEvent onsetEvent = getOnsetEvent(); ControlLoopEventManager manager = makeManager(onsetEvent); assertTrue(0 == manager.getControlLoopTimeout(null)); @@ -660,16 +668,7 @@ public class ControlLoopEventManagerTest { @Test public void testControlLoopTimeout_ZeroTimeout() throws IOException { - UUID requestId = UUID.randomUUID(); - VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent(); - onsetEvent.setClosedLoopControlName(TWO_ONSET_TEST); - onsetEvent.setRequestId(requestId); - onsetEvent.setTarget(VNF_ID); - onsetEvent.setClosedLoopAlarmStart(Instant.now()); - onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET); - onsetEvent.setAai(new HashMap<>()); - onsetEvent.getAai().put(VNF_NAME, ONSET_ONE); - onsetEvent.setTargetType(ControlLoopTargetType.VNF); + VirtualControlLoopEvent onsetEvent = getOnsetEvent(); ControlLoopEventManager manager = makeManager(onsetEvent); @@ -686,16 +685,7 @@ public class ControlLoopEventManagerTest { @Test public void testControlLoopTimeout_NullTimeout() throws IOException { - UUID requestId = UUID.randomUUID(); - VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent(); - onsetEvent.setClosedLoopControlName(TWO_ONSET_TEST); - onsetEvent.setRequestId(requestId); - onsetEvent.setTarget(VNF_ID); - onsetEvent.setClosedLoopAlarmStart(Instant.now()); - onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET); - onsetEvent.setAai(new HashMap<>()); - onsetEvent.getAai().put(VNF_NAME, ONSET_ONE); - onsetEvent.setTargetType(ControlLoopTargetType.VNF); + VirtualControlLoopEvent onsetEvent = getOnsetEvent(); ControlLoopEventManager manager = makeManager(onsetEvent); diff --git a/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/processor/ControlLoopProcessorTest.java b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/processor/ControlLoopProcessorTest.java index 84fe44914..f76c0060c 100644 --- a/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/processor/ControlLoopProcessorTest.java +++ b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/processor/ControlLoopProcessorTest.java @@ -1,8 +1,8 @@ /*- * ============LICENSE_START======================================================= - * unit test + * ONAP * ================================================================================ - * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-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. @@ -29,12 +29,17 @@ import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; import org.apache.commons.io.IOUtils; import org.junit.Test; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.controlloop.ControlLoopException; import org.onap.policy.controlloop.policy.FinalResult; import org.onap.policy.controlloop.policy.Policy; import org.onap.policy.controlloop.policy.PolicyResult; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -49,6 +54,31 @@ public class ControlLoopProcessorTest { this.testFailure(yamlString); } + @Test + public void testControlLoopFromToscaLegacy() throws IOException, CoderException, ControlLoopException { + String policy = + new String(Files.readAllBytes(Paths.get("src/test/resources/tosca-policy-legacy-vcpe.json"))); + assertNotNull( + new ControlLoopProcessor(new StandardCoder().decode(policy, ToscaPolicy.class)).getCurrentPolicy()); + } + + @Test + public void testControlLoopFromToscaCompliant() throws IOException, CoderException, ControlLoopException { + String policy = + new String(Files.readAllBytes(Paths.get("src/test/resources/tosca-policy-compliant-vcpe.json"))); + assertNotNull( + new ControlLoopProcessor(new StandardCoder().decode(policy, ToscaPolicy.class)).getCurrentPolicy()); + } + + @Test + public void testControlLoopFromToscaCompliantBad() throws IOException, CoderException, ControlLoopException { + String policy = + new String(Files.readAllBytes(Paths.get("src/test/resources/tosca-policy-compliant-vcpe.json"))); + ToscaPolicy toscaPolicy = new StandardCoder().decode(policy, ToscaPolicy.class); + toscaPolicy.setType("onap.policies.controlloop.Operational"); + assertThatThrownBy(() -> new ControlLoopProcessor(toscaPolicy)).hasCauseInstanceOf(CoderException.class); + } + @Test public void testControlLoopProcessorBadYaml() throws IOException { InputStream is = new FileInputStream(new File("src/test/resources/string.yaml")); diff --git a/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/utils/ControlLoopUtilsTest.java b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/utils/ControlLoopUtilsTest.java index 2c26517cd..2e4811475 100644 --- a/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/utils/ControlLoopUtilsTest.java +++ b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/utils/ControlLoopUtilsTest.java @@ -19,12 +19,10 @@ package org.onap.policy.controlloop.utils; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; import java.nio.file.Files; import java.nio.file.Paths; -import java.util.Map; import org.junit.Test; import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.controlloop.drl.legacy.ControlLoopParams; @@ -35,22 +33,14 @@ public class ControlLoopUtilsTest { @Test public void testToControlLoopParams() throws Exception { String policy = - new String(Files.readAllBytes(Paths.get("src/test/resources/tosca-policy-operational-restart.json"))); - + new String(Files.readAllBytes(Paths.get("src/test/resources/tosca-policy-legacy-vcpe.json"))); ToscaPolicy toscaPolicy = new StandardCoder().decode(policy, ToscaPolicy.class); + ControlLoopParams params = ControlLoopUtils.toControlLoopParams(toscaPolicy); - assertNotNull(params); - assertNotNull(params.getClosedLoopControlName()); - assertEquals(toscaPolicy.getProperties().get("content"), params.getControlLoopYaml()); + assertEquals("ControlLoop-vCPE-48f0c2c3-a172-4192-9ae3-052274181b6e", params.getClosedLoopControlName()); assertEquals(toscaPolicy.getName(), params.getPolicyName()); assertEquals(toscaPolicy.getVersion(), params.getPolicyVersion()); assertEquals(toscaPolicy.getType() + ":" + toscaPolicy.getVersion(), params.getPolicyScope()); - - assertNull(ControlLoopUtils.toControlLoopParams(null)); - - Map properties = toscaPolicy.getProperties(); - toscaPolicy.setProperties(null); - assertNull(ControlLoopUtils.toControlLoopParams(toscaPolicy)); - toscaPolicy.setProperties(properties); + assertSame(toscaPolicy, params.getToscaPolicy()); } } \ No newline at end of file diff --git a/controlloop/common/eventmanager/src/test/resources/tosca-policy-compliant-vcpe.json b/controlloop/common/eventmanager/src/test/resources/tosca-policy-compliant-vcpe.json new file mode 100644 index 000000000..c01f6898c --- /dev/null +++ b/controlloop/common/eventmanager/src/test/resources/tosca-policy-compliant-vcpe.json @@ -0,0 +1,37 @@ +{ + "type": "onap.policies.controlloop.operational.common.Drools", + "type_version": "1.0.0", + "version": "1.0.0", + "name": "operational.restart", + "metadata": { + "policy-id": "operational.restart" + }, + "properties": { + "id": "ControlLoop-vCPE-48f0c2c3-a172-4192-9ae3-052274181b6e", + "timeout": 3600, + "abatement": false, + "trigger": "unique-policy-id-1-restart", + "operations": [ + { + "id": "unique-policy-id-1-restart", + "description": "Restart the VM", + "operation": { + "actor": "APPC", + "operation": "Restart", + "target": { + "type": "VM" + } + }, + "timeout": 1200, + "retries": 3, + "success": "final_success", + "failure": "final_failure", + "failure_timeout": "final_failure_timeout", + "failure_retries": "final_failure_retries", + "failure_exception": "final_failure_exception", + "failure_guard": "final_failure_guard" + } + ], + "controllerName": "usecases" + } +} \ No newline at end of file diff --git a/controlloop/common/eventmanager/src/test/resources/tosca-policy-legacy-vcpe.json b/controlloop/common/eventmanager/src/test/resources/tosca-policy-legacy-vcpe.json new file mode 100644 index 000000000..fd7c718db --- /dev/null +++ b/controlloop/common/eventmanager/src/test/resources/tosca-policy-legacy-vcpe.json @@ -0,0 +1,9 @@ +{ + "type": "onap.policies.controlloop.Operational", + "type_version": "1.0.0", + "properties": { + "content": "controlLoop%3A%0D%0A++version%3A+2.0.0%0D%0A++controlLoopName%3A+ControlLoop-vCPE-48f0c2c3-a172-4192-9ae3-052274181b6e%0D%0A++trigger_policy%3A+unique-policy-id-1-restart%0D%0A++timeout%3A+3600%0D%0A++abatement%3A+false%0D%0A+%0D%0Apolicies%3A%0D%0A++-+id%3A+unique-policy-id-1-restart%0D%0A++++name%3A+Restart+the+VM%0D%0A++++description%3A%0D%0A++++actor%3A+APPC%0D%0A++++recipe%3A+Restart%0D%0A++++target%3A%0D%0A++++++type%3A+VM%0D%0A++++retry%3A+3%0D%0A++++timeout%3A+1200%0D%0A++++success%3A+final_success%0D%0A++++failure%3A+final_failure%0D%0A++++failure_timeout%3A+final_failure_timeout%0D%0A++++failure_retries%3A+final_failure_retries%0D%0A++++failure_exception%3A+final_failure_exception%0D%0A++++failure_guard%3A+final_failure_guard" + }, + "name": "operational.restart", + "version": "1.0.0" +} diff --git a/controlloop/common/eventmanager/src/test/resources/tosca-policy-operational-restart.json b/controlloop/common/eventmanager/src/test/resources/tosca-policy-operational-restart.json deleted file mode 100644 index fd7c718db..000000000 --- a/controlloop/common/eventmanager/src/test/resources/tosca-policy-operational-restart.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "type": "onap.policies.controlloop.Operational", - "type_version": "1.0.0", - "properties": { - "content": "controlLoop%3A%0D%0A++version%3A+2.0.0%0D%0A++controlLoopName%3A+ControlLoop-vCPE-48f0c2c3-a172-4192-9ae3-052274181b6e%0D%0A++trigger_policy%3A+unique-policy-id-1-restart%0D%0A++timeout%3A+3600%0D%0A++abatement%3A+false%0D%0A+%0D%0Apolicies%3A%0D%0A++-+id%3A+unique-policy-id-1-restart%0D%0A++++name%3A+Restart+the+VM%0D%0A++++description%3A%0D%0A++++actor%3A+APPC%0D%0A++++recipe%3A+Restart%0D%0A++++target%3A%0D%0A++++++type%3A+VM%0D%0A++++retry%3A+3%0D%0A++++timeout%3A+1200%0D%0A++++success%3A+final_success%0D%0A++++failure%3A+final_failure%0D%0A++++failure_timeout%3A+final_failure_timeout%0D%0A++++failure_retries%3A+final_failure_retries%0D%0A++++failure_exception%3A+final_failure_exception%0D%0A++++failure_guard%3A+final_failure_guard" - }, - "name": "operational.restart", - "version": "1.0.0" -} -- cgit 1.2.3-korg