diff options
author | Liam Fallon <liam.fallon@est.tech> | 2020-02-18 16:12:02 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2020-02-18 16:12:02 +0000 |
commit | 5cdc0da265e1488321f88c38d9e0fb9d3626f54d (patch) | |
tree | 90b49e58970bf408b33eaa4b6464238f2c533a91 /controlloop/common/eventmanager/src | |
parent | 9c2cabe28c17bb243a29f218f21aafee7e9a70e4 (diff) | |
parent | 6dfab64262ea5816c761042192c84e498a3177ab (diff) |
Merge "tosca compliant op policy support + vcpe test"
Diffstat (limited to 'controlloop/common/eventmanager/src')
10 files changed, 279 insertions, 148 deletions
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 @@ -78,12 +85,6 @@ public class ControlLoopParamsTest { } @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()); assertEquals(POLICY_NAME.toUpperCase(), clp.getPolicyName()); @@ -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()); @@ -226,17 +213,32 @@ public class ControlLoopEventManagerTest { } @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; @@ -50,6 +55,31 @@ public class ControlLoopProcessorTest { } @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")); String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8); 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<String, Object> 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-operational-restart.json b/controlloop/common/eventmanager/src/test/resources/tosca-policy-legacy-vcpe.json index fd7c718db..fd7c718db 100644 --- a/controlloop/common/eventmanager/src/test/resources/tosca-policy-operational-restart.json +++ b/controlloop/common/eventmanager/src/test/resources/tosca-policy-legacy-vcpe.json |