summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPamela Dragosh <pdragosh@research.att.com>2020-03-16 11:09:36 -0400
committerPamela Dragosh <pdragosh@research.att.com>2020-03-16 11:24:19 -0400
commit6b4f9b69bc4acec28da8ef50b8f234d0182f0d78 (patch)
tree877cc134b10d08393f0868a7dae7d5f604e49a5b
parentec20e47d350e16e1bf52f0396b777063b23770c7 (diff)
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 <pdragosh@research.att.com>
-rw-r--r--applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/ToscaPolicyTranslator.java7
-rw-r--r--applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/XacmlPolicyUtils.java35
-rw-r--r--applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdBaseTranslator.java2
-rw-r--r--applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdCombinedPolicyResultsTranslator.java2
-rw-r--r--applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdMatchableTranslator.java3
-rw-r--r--applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdXacmlApplicationServiceProvider.java8
-rw-r--r--applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/XacmlPolicyUtilsTest.java27
-rw-r--r--applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/std/StdMatchableTranslatorTest.java4
-rw-r--r--applications/guard/src/main/java/org/onap/policy/xacml/pdp/application/guard/CoordinationGuardTranslator.java7
-rw-r--r--applications/guard/src/main/java/org/onap/policy/xacml/pdp/application/guard/GuardTranslator.java5
-rw-r--r--applications/guard/src/test/java/org/onap/policy/xacml/pdp/application/guard/GuardTranslatorTest.java2
-rw-r--r--applications/native/src/main/java/org/onap/policy/xacml/pdp/application/nativ/NativePdpApplicationTranslator.java17
-rw-r--r--applications/native/src/test/java/org/onap/policy/xacml/pdp/application/nativ/NativePdpApplicationTest.java65
-rw-r--r--applications/native/src/test/resources/policies/bad.native.policies.yaml28
-rw-r--r--applications/native/src/test/resources/policies/native.policy.yaml4
-rw-r--r--applications/optimization/src/main/java/org/onap/policy/xacml/pdp/application/optimization/OptimizationPdpApplicationTranslator.java7
16 files changed, 178 insertions, 45 deletions
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> 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<String, ToscaPolicy> 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);
diff --git a/applications/guard/src/main/java/org/onap/policy/xacml/pdp/application/guard/CoordinationGuardTranslator.java b/applications/guard/src/main/java/org/onap/policy/xacml/pdp/application/guard/CoordinationGuardTranslator.java
index f1006c6d..2c7ad587 100644
--- a/applications/guard/src/main/java/org/onap/policy/xacml/pdp/application/guard/CoordinationGuardTranslator.java
+++ b/applications/guard/src/main/java/org/onap/policy/xacml/pdp/application/guard/CoordinationGuardTranslator.java
@@ -39,9 +39,6 @@ import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;
-
-import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
-
import org.apache.commons.io.IOUtils;
import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardYamlCoder;
@@ -63,7 +60,7 @@ public class CoordinationGuardTranslator implements ToscaPolicyTranslator {
}
@Override
- public PolicyType convertPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException {
+ public Object convertPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException {
LOGGER.debug("Using CoordinationGuardTranslator.convertPolicy");
//
// Policy name should be at the root
@@ -88,7 +85,7 @@ public class CoordinationGuardTranslator implements ToscaPolicyTranslator {
// Scan the string and convert to PoilcyType
//
try (InputStream is = new ByteArrayInputStream(xacmlStr.getBytes(StandardCharsets.UTF_8))) {
- return (PolicyType) XACMLPolicyScanner.readPolicy(is);
+ return XACMLPolicyScanner.readPolicy(is);
} catch (IOException e) {
throw new ToscaPolicyConversionException("Failed to read policy", e);
}
diff --git a/applications/guard/src/main/java/org/onap/policy/xacml/pdp/application/guard/GuardTranslator.java b/applications/guard/src/main/java/org/onap/policy/xacml/pdp/application/guard/GuardTranslator.java
index fd46a988..1e4333ea 100644
--- a/applications/guard/src/main/java/org/onap/policy/xacml/pdp/application/guard/GuardTranslator.java
+++ b/applications/guard/src/main/java/org/onap/policy/xacml/pdp/application/guard/GuardTranslator.java
@@ -96,7 +96,8 @@ public class GuardTranslator implements ToscaPolicyTranslator {
/**
* Convert the policy.
*/
- public PolicyType convertPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException {
+ @Override
+ public Object convertPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException {
//
// Policy name should be at the root
//
@@ -140,6 +141,7 @@ public class GuardTranslator implements ToscaPolicyTranslator {
/**
* Convert Request.
*/
+ @Override
public Request convertRequest(DecisionRequest request) throws ToscaPolicyConversionException {
LOGGER.info("Converting Request {}", request);
try {
@@ -152,6 +154,7 @@ public class GuardTranslator implements ToscaPolicyTranslator {
/**
* Convert response.
*/
+ @Override
public DecisionResponse convertResponse(Response xacmlResponse) {
LOGGER.info("Converting Response {}", xacmlResponse);
DecisionResponse decisionResponse = new DecisionResponse();
diff --git a/applications/guard/src/test/java/org/onap/policy/xacml/pdp/application/guard/GuardTranslatorTest.java b/applications/guard/src/test/java/org/onap/policy/xacml/pdp/application/guard/GuardTranslatorTest.java
index 36b43aee..c785a50c 100644
--- a/applications/guard/src/test/java/org/onap/policy/xacml/pdp/application/guard/GuardTranslatorTest.java
+++ b/applications/guard/src/test/java/org/onap/policy/xacml/pdp/application/guard/GuardTranslatorTest.java
@@ -166,7 +166,7 @@ public class GuardTranslatorTest {
translator.convertPolicy(policy));
continue;
}
- PolicyType xacmlPolicy = translator.convertPolicy(policy);
+ PolicyType xacmlPolicy = (PolicyType) translator.convertPolicy(policy);
assertThat(xacmlPolicy).isNotNull();
//
// Let's dump it out
diff --git a/applications/native/src/main/java/org/onap/policy/xacml/pdp/application/nativ/NativePdpApplicationTranslator.java b/applications/native/src/main/java/org/onap/policy/xacml/pdp/application/nativ/NativePdpApplicationTranslator.java
index 546c29eb..5ce25fac 100644
--- a/applications/native/src/main/java/org/onap/policy/xacml/pdp/application/nativ/NativePdpApplicationTranslator.java
+++ b/applications/native/src/main/java/org/onap/policy/xacml/pdp/application/nativ/NativePdpApplicationTranslator.java
@@ -27,11 +27,9 @@ import com.att.research.xacml.api.Response;
import com.att.research.xacml.util.XACMLPolicyScanner;
import java.io.ByteArrayInputStream;
import java.io.IOException;
-import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Map;
-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;
@@ -56,7 +54,7 @@ public class NativePdpApplicationTranslator implements ToscaPolicyTranslator {
}
@Override
- public PolicyType convertPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException {
+ public Object convertPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException {
//
// Extract the Base64 encoded policy xml string and decode it
//
@@ -67,16 +65,19 @@ public class NativePdpApplicationTranslator implements ToscaPolicyTranslator {
} catch (IllegalArgumentException exc) {
throw new ToscaPolicyConversionException("error on Base64 decoding the native policy", exc);
}
- LOGGER.debug("Decoded xacml policy {}",decodedXacmlPolicy);
+ LOGGER.debug("Decoded xacml policy {}", decodedXacmlPolicy);
//
// Scan the string and convert to xacml PolicyType
//
- try (InputStream is = new ByteArrayInputStream(decodedXacmlPolicy.getBytes(StandardCharsets.UTF_8))) {
+ try (ByteArrayInputStream is = new ByteArrayInputStream(decodedXacmlPolicy.getBytes(StandardCharsets.UTF_8))) {
//
- // Here we assume it is PolicyType, not PolicySetType
- // PolicySetType will be addressed later
+ // Read the Policy In
//
- return (PolicyType) XACMLPolicyScanner.readPolicy(is);
+ Object policy = XACMLPolicyScanner.readPolicy(is);
+ if (policy == null) {
+ throw new ToscaPolicyConversionException("Invalid XACML Policy");
+ }
+ return policy;
} catch (IOException exc) {
throw new ToscaPolicyConversionException("Failed to read policy", exc);
}
diff --git a/applications/native/src/test/java/org/onap/policy/xacml/pdp/application/nativ/NativePdpApplicationTest.java b/applications/native/src/test/java/org/onap/policy/xacml/pdp/application/nativ/NativePdpApplicationTest.java
index b25c2a31..a11c1b14 100644
--- a/applications/native/src/test/java/org/onap/policy/xacml/pdp/application/nativ/NativePdpApplicationTest.java
+++ b/applications/native/src/test/java/org/onap/policy/xacml/pdp/application/nativ/NativePdpApplicationTest.java
@@ -23,6 +23,7 @@
package org.onap.policy.xacml.pdp.application.nativ;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import com.att.research.xacml.api.Decision;
import com.att.research.xacml.api.Request;
@@ -30,6 +31,7 @@ import com.att.research.xacml.api.Response;
import com.att.research.xacml.std.dom.DOMRequest;
import com.att.research.xacml.std.dom.DOMResponse;
import java.io.File;
+import java.util.Map;
import java.util.Properties;
import java.util.ServiceLoader;
import org.junit.BeforeClass;
@@ -37,7 +39,14 @@ import org.junit.ClassRule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.onap.policy.common.endpoints.parameters.RestServerParameters;
+import org.onap.policy.common.utils.coder.StandardYamlCoder;
+import org.onap.policy.common.utils.resources.ResourceUtils;
import org.onap.policy.common.utils.resources.TextFileUtils;
+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;
+import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
+import org.onap.policy.pdp.xacml.application.common.ToscaPolicyConversionException;
import org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider;
import org.onap.policy.pdp.xacml.application.common.XacmlPolicyUtils;
import org.onap.policy.pdp.xacml.xacmltest.TestUtils;
@@ -48,6 +57,7 @@ public class NativePdpApplicationTest {
private static final Logger LOGGER = LoggerFactory.getLogger(NativePdpApplicationTest.class);
private static final String PERMIT = "Permit";
+ private static final StandardYamlCoder yamlCoder = new StandardYamlCoder();
private static Properties properties = new Properties();
private static File propertiesFile;
private static RestServerParameters clientParams = new RestServerParameters();
@@ -111,6 +121,59 @@ public class NativePdpApplicationTest {
}
@Test
+ public void testUncommon() {
+ NativePdpApplicationTranslator translator = new NativePdpApplicationTranslator();
+ assertThatExceptionOfType(ToscaPolicyConversionException.class).isThrownBy(() ->
+ translator.convertRequest(null)
+ ).withMessageContaining("Do not call native convertRequest");
+
+ assertThat(translator.convertResponse(null)).isNull();
+
+ NativePdpApplication application = new NativePdpApplication();
+ assertThat(application.canSupportPolicyType(new ToscaPolicyTypeIdentifier(
+ "onap.policies.native.Xacml", "1.0.0"))).isTrue();
+ assertThat(application.canSupportPolicyType(new ToscaPolicyTypeIdentifier(
+ "onap.policies.native.SomethingElse", "1.0.0"))).isFalse();
+ assertThat(application.actionDecisionsSupported()).contains("native");
+ }
+
+ @Test
+ public void testBadPolicies() throws Exception {
+ NativePdpApplicationTranslator translator = new NativePdpApplicationTranslator();
+ String policyYaml = ResourceUtils.getResourceAsString("src/test/resources/policies/bad.native.policies.yaml");
+ //
+ // Serialize it into a class
+ //
+ ToscaServiceTemplate serviceTemplate = yamlCoder.decode(policyYaml, ToscaServiceTemplate.class);
+ //
+ // Make sure all the fields are setup properly
+ //
+ JpaToscaServiceTemplate jtst = new JpaToscaServiceTemplate();
+ jtst.fromAuthorative(serviceTemplate);
+ ToscaServiceTemplate completedJtst = jtst.toAuthorative();
+ //
+ // Get the policies
+ //
+ for (Map<String, ToscaPolicy> policies : completedJtst.getToscaTopologyTemplate().getPolicies()) {
+ for (ToscaPolicy policy : policies.values()) {
+ if ("bad.base64".equals(policy.getName())) {
+ assertThatExceptionOfType(ToscaPolicyConversionException.class).isThrownBy(() ->
+ translator.convertPolicy(policy)
+ ).withMessageContaining("error on Base64 decoding the native policy");
+ } else if ("bad.noproperties".equals(policy.getName())) {
+ assertThatExceptionOfType(ToscaPolicyConversionException.class).isThrownBy(() ->
+ translator.convertPolicy(policy)
+ ).withMessageContaining("no xacml native policy found in the tosca policy");
+ } else if ("bad.policy".equals(policy.getName())) {
+ assertThatExceptionOfType(ToscaPolicyConversionException.class).isThrownBy(() ->
+ translator.convertPolicy(policy)
+ ).withMessageContaining("Invalid XACML Policy");
+ }
+ }
+ }
+ }
+
+ @Test
public void testNativePolicy() throws Exception {
LOGGER.info("*********** Running native policy test *************");
@@ -152,7 +215,7 @@ public class NativePdpApplicationTest {
* @throws Exception on errors checking the decision
*
**/
- public void checkDecision(String expected, Response response) throws Exception {
+ private void checkDecision(String expected, Response response) throws Exception {
LOGGER.info("Looking for {} Decision", expected);
assertThat(response).isNotNull();
Decision decision = response.getResults().iterator().next().getDecision();
diff --git a/applications/native/src/test/resources/policies/bad.native.policies.yaml b/applications/native/src/test/resources/policies/bad.native.policies.yaml
new file mode 100644
index 00000000..318b3209
--- /dev/null
+++ b/applications/native/src/test/resources/policies/bad.native.policies.yaml
@@ -0,0 +1,28 @@
+tosca_definitions_version: tosca_simple_yaml_1_1_0
+topology_template:
+ policies:
+ -
+ bad.base64:
+ type: onap.policies.native.Xacml
+ version: 1.0.0
+ metadata:
+ policy-id: bad.base64
+ policy-version: 1.0.0
+ properties:
+ policy: "%%%%%%"
+ -
+ bad.noproperties:
+ type: onap.policies.native.Xacml
+ version: 1.0.0
+ metadata:
+ policy-id: bad.noproperties
+ policy-version: 1.0.0
+ -
+ bad.policy:
+ type: onap.policies.native.Xacml
+ version: 1.0.0
+ metadata:
+ policy-id: bad.policy
+ policy-version: 1.0.0
+ properties:
+ policy: ABC123 \ No newline at end of file
diff --git a/applications/native/src/test/resources/policies/native.policy.yaml b/applications/native/src/test/resources/policies/native.policy.yaml
index 00bc5db9..b9e8d60d 100644
--- a/applications/native/src/test/resources/policies/native.policy.yaml
+++ b/applications/native/src/test/resources/policies/native.policy.yaml
@@ -1,4 +1,4 @@
-tosca_definitions_version: tosca_simple_yaml_1_0_0
+tosca_definitions_version: tosca_simple_yaml_1_1_0
topology_template:
policies:
-
@@ -7,6 +7,6 @@ topology_template:
version: 1.0.0
metadata:
policy-id: native.access.control
- policy-version: 1
+ policy-version: 1.0.0
properties:
policy: PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+CjxQb2xpY3kgeG1sbnM9InVybjpvYXNpczpuYW1lczp0Yzp4YWNtbDozLjA6Y29yZTpzY2hlbWE6d2QtMTciIHhtbG5zOnhzaT0iaHR0cDovL3d3dy53My5vcmcvMjAwMS9YTUxTY2hlbWEtaW5zdGFuY2UiIFBvbGljeUlkPSJ1cm46b2FzaXM6bmFtZXM6dGM6eGFjbWw6Mi4wOmNvbmZvcm1hbmNlLXRlc3Q6SUlBMTpwb2xpY3kiIFJ1bGVDb21iaW5pbmdBbGdJZD0idXJuOm9hc2lzOm5hbWVzOnRjOnhhY21sOjMuMDpydWxlLWNvbWJpbmluZy1hbGdvcml0aG06ZGVueS1vdmVycmlkZXMiIFZlcnNpb249IjEuMCIgeHNpOnNjaGVtYUxvY2F0aW9uPSJ1cm46b2FzaXM6bmFtZXM6dGM6eGFjbWw6My4wOnBvbGljeTpzY2hlbWE6b3MgYWNjZXNzX2NvbnRyb2wteGFjbWwtMi4wLXBvbGljeS1zY2hlbWEtb3MueHNkIj4KICAgIDxEZXNjcmlwdGlvbj4KICAgICAgICBQb2xpY3kgZm9yIENvbmZvcm1hbmNlIFRlc3QgSUlBMDAxLgogICAgPC9EZXNjcmlwdGlvbj4KICAgIDxUYXJnZXQvPgogICAgPFJ1bGUgRWZmZWN0PSJQZXJtaXQiIFJ1bGVJZD0idXJuOm9hc2lzOm5hbWV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vUG9saWN5Pgo= \ No newline at end of file
diff --git a/applications/optimization/src/main/java/org/onap/policy/xacml/pdp/application/optimization/OptimizationPdpApplicationTranslator.java b/applications/optimization/src/main/java/org/onap/policy/xacml/pdp/application/optimization/OptimizationPdpApplicationTranslator.java
index d56b73ae..52f1ec0a 100644
--- a/applications/optimization/src/main/java/org/onap/policy/xacml/pdp/application/optimization/OptimizationPdpApplicationTranslator.java
+++ b/applications/optimization/src/main/java/org/onap/policy/xacml/pdp/application/optimization/OptimizationPdpApplicationTranslator.java
@@ -65,11 +65,12 @@ public class OptimizationPdpApplicationTranslator extends StdMatchableTranslator
@SuppressWarnings("unchecked")
@Override
- public PolicyType convertPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException {
+ public Object convertPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException {
//
- // Have our superclass do the work
+ // Have our superclass do the work - NOTE we are assuming
+ // that we are getting a PolicyType converted.
//
- PolicyType policy = super.convertPolicy(toscaPolicy);
+ PolicyType policy = (PolicyType) super.convertPolicy(toscaPolicy);
//
// Check if this is the subscriber policy
//