From 6b4f9b69bc4acec28da8ef50b8f234d0182f0d78 Mon Sep 17 00:00:00 2001 From: Pamela Dragosh Date: Mon, 16 Mar 2020 11:09:36 -0400 Subject: Missing support for PolicySetType Adds support for PolicySetType specifically for Native policies. When/If the other applications change to support they can easily do so. Adding some more code coverage for Native application and translator. Issue-ID: POLICY-2433 Change-Id: I463ca9f04928d759624a2176598b463057d386bd Signed-off-by: Pamela Dragosh --- .../application/common/ToscaPolicyTranslator.java | 7 ++--- .../xacml/application/common/XacmlPolicyUtils.java | 35 ++++++++++++++++++++-- .../application/common/std/StdBaseTranslator.java | 2 +- .../std/StdCombinedPolicyResultsTranslator.java | 2 +- .../common/std/StdMatchableTranslator.java | 3 +- .../std/StdXacmlApplicationServiceProvider.java | 8 ++--- .../application/common/XacmlPolicyUtilsTest.java | 27 ++++++++++++----- .../common/std/StdMatchableTranslatorTest.java | 4 +-- 8 files changed, 64 insertions(+), 24 deletions(-) (limited to 'applications/common') diff --git a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/ToscaPolicyTranslator.java b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/ToscaPolicyTranslator.java index 32b950b4..61ec0bed 100644 --- a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/ToscaPolicyTranslator.java +++ b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/ToscaPolicyTranslator.java @@ -24,9 +24,6 @@ package org.onap.policy.pdp.xacml.application.common; import com.att.research.xacml.api.Request; import com.att.research.xacml.api.Response; - -import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType; - import org.onap.policy.models.decisions.concepts.DecisionRequest; import org.onap.policy.models.decisions.concepts.DecisionResponse; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; @@ -37,10 +34,10 @@ public interface ToscaPolicyTranslator { * Implement this method to translate policies. * * @param toscaPolicy Incoming Tosca Policy object - * @return Xacml PolicyType object + * @return Xacml PolicyType or PolicySetType object * @throws ToscaPolicyConversionException Exception */ - PolicyType convertPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException; + Object convertPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException; /** * Implement this method to convert an ONAP DecisionRequest into diff --git a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/XacmlPolicyUtils.java b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/XacmlPolicyUtils.java index 0ca71980..c8457091 100644 --- a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/XacmlPolicyUtils.java +++ b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/XacmlPolicyUtils.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * 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. @@ -23,6 +23,7 @@ package org.onap.policy.pdp.xacml.application.common; import com.att.research.xacml.api.Identifier; +import com.att.research.xacml.util.XACMLPolicyWriter; import com.att.research.xacml.util.XACMLProperties; import java.io.File; @@ -352,13 +353,24 @@ public class XacmlPolicyUtils { * @param path Path for policy * @return Path unique file path for the Policy */ - public static Path constructUniquePolicyFilename(PolicyType policy, Path path) { + public static Path constructUniquePolicyFilename(Object policy, Path path) { + String id; + String version; + if (policy instanceof PolicyType) { + id = ((PolicyType) policy).getPolicyId(); + version = ((PolicyType) policy).getVersion(); + } else if (policy instanceof PolicySetType) { + id = ((PolicySetType) policy).getPolicySetId(); + version = ((PolicySetType) policy).getVersion(); + } else { + throw new IllegalArgumentException("Must pass a PolicyType or PolicySetType"); + } // // // Can it be possible to produce an invalid filename? // Should we insert a UUID // - String filename = policy.getPolicyId() + "_" + policy.getVersion() + ".xml"; + String filename = id + "_" + version + ".xml"; // // Construct the Path // @@ -495,4 +507,21 @@ public class XacmlPolicyUtils { return propertiesFile; } } + + /** + * Wraps the call to XACMLPolicyWriter. + * + * @param path Path to file to be written to. + * @param policy PolicyType or PolicySetType + * @return Path - the same path passed in most likely from XACMLPolicyWriter. Or NULL if an error occurs. + */ + public static Path writePolicyFile(Path path, Object policy) { + if (policy instanceof PolicyType) { + return XACMLPolicyWriter.writePolicyFile(path, (PolicyType) policy); + } else if (policy instanceof PolicySetType) { + return XACMLPolicyWriter.writePolicyFile(path, (PolicySetType) policy); + } else { + throw new IllegalArgumentException("Expecting PolicyType or PolicySetType"); + } + } } diff --git a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdBaseTranslator.java b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdBaseTranslator.java index 3ac57b7b..a6c9977e 100644 --- a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdBaseTranslator.java +++ b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdBaseTranslator.java @@ -64,7 +64,7 @@ public abstract class StdBaseTranslator implements ToscaPolicyTranslator { public static final String POLICY_VERSION = "policy-version"; @Override - public PolicyType convertPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException { + public Object convertPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException { throw new ToscaPolicyConversionException("Please override convertPolicy"); } diff --git a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdCombinedPolicyResultsTranslator.java b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdCombinedPolicyResultsTranslator.java index d9661829..8db9948d 100644 --- a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdCombinedPolicyResultsTranslator.java +++ b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdCombinedPolicyResultsTranslator.java @@ -59,7 +59,7 @@ public class StdCombinedPolicyResultsTranslator extends StdBaseTranslator { } @Override - public PolicyType convertPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException { + public Object convertPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException { // // Sanity checks // diff --git a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdMatchableTranslator.java b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdMatchableTranslator.java index 7ca995e0..690b710f 100644 --- a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdMatchableTranslator.java +++ b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdMatchableTranslator.java @@ -156,6 +156,7 @@ public class StdMatchableTranslator extends StdBaseTranslator { ); } + @Override protected void scanAdvice(Collection advice, DecisionResponse decisionResponse) { LOGGER.warn("scanAdvice not supported by {}", this.getClass()); } @@ -235,7 +236,7 @@ public class StdMatchableTranslator extends StdBaseTranslator { } @Override - public PolicyType convertPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException { + public Object convertPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException { // // Get the TOSCA Policy Type for this policy // diff --git a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdXacmlApplicationServiceProvider.java b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdXacmlApplicationServiceProvider.java index 0fdd3a96..9a8b63fb 100644 --- a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdXacmlApplicationServiceProvider.java +++ b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdXacmlApplicationServiceProvider.java @@ -28,7 +28,6 @@ import com.att.research.xacml.api.pdp.PDPEngine; import com.att.research.xacml.api.pdp.PDPEngineFactory; import com.att.research.xacml.api.pdp.PDPException; import com.att.research.xacml.util.FactoryException; -import com.att.research.xacml.util.XACMLPolicyWriter; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -39,7 +38,6 @@ import java.util.List; import java.util.Map; import java.util.Properties; import lombok.Getter; -import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType; import org.apache.commons.lang3.tuple.Pair; import org.onap.policy.common.endpoints.parameters.RestServerParameters; import org.onap.policy.models.decisions.concepts.DecisionRequest; @@ -121,7 +119,7 @@ public abstract class StdXacmlApplicationServiceProvider implements XacmlApplica // // Convert the policies first // - PolicyType xacmlPolicy = this.getTranslator(toscaPolicy.getType()).convertPolicy(toscaPolicy); + Object xacmlPolicy = this.getTranslator(toscaPolicy.getType()).convertPolicy(toscaPolicy); if (xacmlPolicy == null) { throw new ToscaPolicyConversionException("Failed to convert policy"); } @@ -137,7 +135,9 @@ public abstract class StdXacmlApplicationServiceProvider implements XacmlApplica // Write the policy to disk // Maybe check for an error // - XACMLPolicyWriter.writePolicyFile(refPath, xacmlPolicy); + if (XacmlPolicyUtils.writePolicyFile(refPath, xacmlPolicy) == null) { + throw new ToscaPolicyConversionException("Unable to writePolicyFile"); + } if (LOGGER.isInfoEnabled()) { LOGGER.info("Xacml Policy is {}{}", XacmlPolicyUtils.LINE_SEPARATOR, new String(Files.readAllBytes(refPath), StandardCharsets.UTF_8)); diff --git a/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/XacmlPolicyUtilsTest.java b/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/XacmlPolicyUtilsTest.java index 46ad83b5..05d796ea 100644 --- a/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/XacmlPolicyUtilsTest.java +++ b/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/XacmlPolicyUtilsTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * 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. @@ -24,6 +24,7 @@ package org.onap.policy.pdp.xacml.application.common; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatCode; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import com.att.research.xacml.api.XACML3; import com.att.research.xacml.util.XACMLPolicyWriter; @@ -121,9 +122,9 @@ public class XacmlPolicyUtilsTest { // // Save root policy // - File rootFile = policyFolder.newFile("root.xml"); - LOGGER.info("Creating Root Policy {}", rootFile.getAbsolutePath()); - rootPath = XACMLPolicyWriter.writePolicyFile(rootFile.toPath(), rootPolicy); + Path rootFile = XacmlPolicyUtils.constructUniquePolicyFilename(rootPolicy, policyFolder.getRoot().toPath()); + LOGGER.info("Creating Root Policy {}", rootFile.toAbsolutePath()); + rootPath = XacmlPolicyUtils.writePolicyFile(rootFile, rootPolicy); // // Create policies - Policies 1 and 2 will become references in the // root policy. While Policies 3 and 4 will become references in the @@ -200,9 +201,21 @@ public class XacmlPolicyUtilsTest { // // Save it to disk // - File file = policyFolder.newFile(policy.getPolicyId() + ".xml"); - LOGGER.info("Creating Policy {}", file.getAbsolutePath()); - return XACMLPolicyWriter.writePolicyFile(file.toPath(), policy); + Path policyFile = XacmlPolicyUtils.constructUniquePolicyFilename(policy, policyFolder.getRoot().toPath()); + LOGGER.info("Creating Policy {}", policyFile.toAbsolutePath()); + return XacmlPolicyUtils.writePolicyFile(policyFile, policy); + } + + @Test + public void testUncommonConditions() throws IOException { + File fileTemp = policyFolder.newFile(); + assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> + XacmlPolicyUtils.writePolicyFile(fileTemp.toPath(), new String("not a policy")) + ); + assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> + XacmlPolicyUtils.constructUniquePolicyFilename(new String("not a policy"), + policyFolder.getRoot().toPath()) + ); } @Test diff --git a/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/std/StdMatchableTranslatorTest.java b/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/std/StdMatchableTranslatorTest.java index e191a08a..584390cd 100644 --- a/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/std/StdMatchableTranslatorTest.java +++ b/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/std/StdMatchableTranslatorTest.java @@ -192,9 +192,9 @@ public class StdMatchableTranslatorTest { for (Map policies : completedJtst.getToscaTopologyTemplate().getPolicies()) { for (ToscaPolicy policy : policies.values()) { // - // Test that we can convert the policy + // Test that we can convert the policy - assuming PolicyType // - PolicyType translatedPolicy = translator.convertPolicy(policy); + PolicyType translatedPolicy = (PolicyType) translator.convertPolicy(policy); assertNotNull(translatedPolicy); assertThat(translatedPolicy.getObligationExpressions().getObligationExpression()).hasSize(1); logger.info("Translated policy {} {}", XacmlPolicyUtils.LINE_SEPARATOR, translatedPolicy); -- cgit 1.2.3-korg