diff options
author | Michael Mokry <michael.mokry@att.com> | 2019-04-11 12:19:50 -0500 |
---|---|---|
committer | Michael Mokry <michael.mokry@att.com> | 2019-04-11 17:15:07 -0500 |
commit | 432cec9fa6f143dad324cd11f62fb052c7da32b7 (patch) | |
tree | 3d7d5bb4f1e7ada0e8ca3255e03149800a1b4b00 /applications/common/src | |
parent | 4013653daa38a7fe1b9ffcae02e27d0bc411ac8f (diff) |
Changes to handle PDPX deploy/undeploy
Added changes to handle incoming udpate for deploying or undeploying the
policies by comparing both sets. STill need to add support for removing
the policies and getting the deployed ToscaPolicyIdentifiers for
PdpStatus response.
* PLD - added functionality to unload policies in the applications and
* PLD - added map of loaded policies
* Mike - made change to address Jim's and Joshua's comments
* Mike - Made change to use mapLoadedPolicies to get deployed policies
from the applications
* Mike - made change to assembly.xml to add support for apps directory.
Change-Id: If7d119197171b68b905b7fd0f2b5be6cf6f609e7
Issue-ID: POLICY-1451
Signed-off-by: Michael Mokry <michael.mokry@att.com>
Signed-off-by: Pamela Dragosh <pdragosh@research.att.com>
Diffstat (limited to 'applications/common/src')
5 files changed, 166 insertions, 10 deletions
diff --git a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/TestUtils.java b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/TestUtils.java index fa32516d..50eb50bf 100644 --- a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/TestUtils.java +++ b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/TestUtils.java @@ -22,6 +22,8 @@ package org.onap.policy.pdp.xacml.application.common; +import java.util.ArrayList; +import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -30,9 +32,12 @@ import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.common.utils.resources.ResourceUtils; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.yaml.snakeyaml.Yaml; public class TestUtils { + private static final Logger LOGGER = LoggerFactory.getLogger(TestUtils.class); private static final StandardCoder standardCoder = new StandardCoder(); private TestUtils() { @@ -47,8 +52,15 @@ public class TestUtils { * @throws CoderException exception if it cannot be decoded * @throws XacmlApplicationException If the application cannot load the policy */ - public static void loadPolicies(String resourceFile, XacmlApplicationServiceProvider service) + public static List<ToscaPolicy> loadPolicies(String resourceFile, XacmlApplicationServiceProvider service) throws CoderException, XacmlApplicationException { + // + // Our return object + // + List<ToscaPolicy> loadedPolicies = new ArrayList<>(); + // + // Decode it + // String policyYaml = ResourceUtils.getResourceAsString(resourceFile); Yaml yaml = new Yaml(); Object yamlObject = yaml.load(policyYaml); @@ -58,11 +70,15 @@ public class TestUtils { // Get the policies // for (Map<String, ToscaPolicy> policies : serviceTemplate.getToscaTopologyTemplate().getPolicies()) { - for (Entry<String, ToscaPolicy> entrySet : policies.entrySet()) { - service.loadPolicy(entrySet.getValue()); + for (ToscaPolicy policy : policies.values()) { + if (service.loadPolicy(policy)) { + loadedPolicies.add(policy); + } else { + LOGGER.error("Application failed to load policy"); + } } } - + return loadedPolicies; } } diff --git a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/XacmlApplicationServiceProvider.java b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/XacmlApplicationServiceProvider.java index c1682fb7..d4cce5c1 100644 --- a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/XacmlApplicationServiceProvider.java +++ b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/XacmlApplicationServiceProvider.java @@ -83,7 +83,14 @@ public interface XacmlApplicationServiceProvider { * * @param toscaPolicy object */ - void loadPolicy(ToscaPolicy toscaPolicy) throws XacmlApplicationException; + boolean loadPolicy(ToscaPolicy toscaPolicy) throws XacmlApplicationException; + + /** + * unloadPolicy a Tosca Policy. + * + * @param toscaPolicy object + */ + boolean unloadPolicy(ToscaPolicy toscaPolicy) throws XacmlApplicationException; /** * Makes a decision given the incoming request and returns a response. 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 364b6519..2a5f21f0 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 @@ -215,6 +215,50 @@ public class XacmlPolicyUtils { } /** + * Removes a root policy from the Properties object. Both in the line + * that identifies the policy and the .file property that points to the path. + * + * @param properties Input Properties object to remove + * @param rootPolicyPath The policy file path + * @return Properties object + */ + public static Properties removeRootPolicy(Properties properties, Path rootPolicyPath) { + // + // Get the current set of referenced policy ids + // + StringJoiner join = new StringJoiner(","); + boolean found = false; + Set<String> rootPolicies = XACMLProperties.getRootPolicyIDs(properties); + for (String refPolicy : rootPolicies) { + String refPolicyFile = refPolicy + DOT_FILE_SUFFIX; + // + // If the key and value match, then it will return true + // + if (properties.remove(refPolicyFile, rootPolicyPath.toString())) { + // + // Record that we actually removed it + // + found = true; + } else { + // + // Retain it + // + join.add(refPolicy); + } + } + // + // Did we remove it? + // + if (found) { + // + // Now update the list of referenced properties + // + properties.setProperty(XACMLProperties.PROP_ROOTPOLICIES, join.toString()); + } + return properties; + } + + /** * Removes a referenced policy from the Properties object. Both in the line * that identifies the policy and the .file property that points to the path. * 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 7f85d2f0..2eddc2e8 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 @@ -37,7 +37,9 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Properties; import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType; @@ -60,6 +62,7 @@ public abstract class StdXacmlApplicationServiceProvider implements XacmlApplica private Path pathForData = null; private Properties pdpProperties = null; private PDPEngine pdpEngine = null; + private Map<ToscaPolicy, Path> mapLoadedPolicies = new HashMap<>(); public StdXacmlApplicationServiceProvider() { super(); @@ -115,7 +118,7 @@ public abstract class StdXacmlApplicationServiceProvider implements XacmlApplica @Override public List<ToscaPolicyTypeIdentifier> supportedPolicyTypes() { - return Collections.emptyList(); + throw new UnsupportedOperationException("Please override and implement supportedPolicyTypes"); } @Override @@ -124,7 +127,7 @@ public abstract class StdXacmlApplicationServiceProvider implements XacmlApplica } @Override - public synchronized void loadPolicy(ToscaPolicy toscaPolicy) { + public synchronized boolean loadPolicy(ToscaPolicy toscaPolicy) { try { // // Convert the policies first @@ -166,13 +169,77 @@ public abstract class StdXacmlApplicationServiceProvider implements XacmlApplica // Save the properties // this.pdpProperties = newProperties; + // + // Save in our map + // + this.mapLoadedPolicies.put(toscaPolicy, refPath); } catch (IOException | ToscaPolicyConversionException e) { LOGGER.error("Failed to loadPolicies {}", e); + return false; + } + return true; + } + + @Override + public synchronized boolean unloadPolicy(ToscaPolicy toscaPolicy) throws XacmlApplicationException { + // + // Find it in our map + // + Path refPolicy = this.mapLoadedPolicies.get(toscaPolicy); + if (refPolicy == null) { + LOGGER.error("Failed to find ToscaPolicy {} in our map size {}", toscaPolicy.getMetadata(), + this.mapLoadedPolicies.size()); + return false; + } + // + // Create a copy of the properties object + // + Properties newProperties = this.getProperties(); + // + // Remove it from the properties + // + XacmlPolicyUtils.removeRootPolicy(newProperties, refPolicy); + // + // We can delete the file + // + try { + Files.delete(refPolicy); + } catch (IOException e) { + LOGGER.error("Failed to delete policy {} from disk {}", toscaPolicy.getMetadata(), + refPolicy.toAbsolutePath().toString(), e); } + // + // Write the properties to disk + // + try { + XacmlPolicyUtils.storeXacmlProperties(newProperties, + XacmlPolicyUtils.getPropertiesPath(this.getDataPath())); + } catch (IOException e) { + LOGGER.error("Failed to save the properties to disk {}", newProperties); + } + // + // Reload the engine + // + this.createEngine(newProperties); + // + // Save the properties + // + this.pdpProperties = newProperties; + // + // Save in our map + // + if (this.mapLoadedPolicies.remove(toscaPolicy) == null) { + LOGGER.error("Failed to remove toscaPolicy {} from internal map size {}", toscaPolicy.getMetadata(), + this.mapLoadedPolicies.size()); + } + // + // Not sure if any of the errors above warrant returning false + // + return true; } @Override - public synchronized DecisionResponse makeDecision(DecisionRequest request) { + public DecisionResponse makeDecision(DecisionRequest request) { // // Convert to a XacmlRequest // @@ -251,7 +318,6 @@ public abstract class StdXacmlApplicationServiceProvider implements XacmlApplica PDPEngine engine = factory.newEngine(properties); if (engine != null) { this.pdpEngine = engine; -// this.pdpProperties = new Properties(properties); } } catch (FactoryException e) { LOGGER.error("Failed to create XACML PDP Engine {}", e); 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 fe0f675d..ed63bb9c 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 @@ -225,7 +225,7 @@ public class XacmlPolicyUtilsTest { } @Test - public void testRemovingProperties() { + public void testRemovingReferencedProperties() { // // Dump what we are starting with // @@ -261,4 +261,27 @@ public class XacmlPolicyUtilsTest { XacmlPolicyUtils.debugDumpPolicyProperties(properties, LOGGER); assertThat(properties.getProperty("refstart4.file")).isNullOrEmpty(); } + + @Test + public void testRemovingRootProperties() { + // + // Dump what we are starting with + // + XacmlPolicyUtils.debugDumpPolicyProperties(properties, LOGGER); + // + // Remove root policies + // + Path ref = Paths.get("src/test/resources/root.xml"); + XacmlPolicyUtils.removeRootPolicy(properties, ref); + XacmlPolicyUtils.debugDumpPolicyProperties(properties, LOGGER); + assertThat(properties.getProperty("root.file")).isNullOrEmpty(); + + // + // Test one that isn't in there + // + ref = Paths.get("src/test/resources/NotThere.xml"); + XacmlPolicyUtils.removeRootPolicy(properties, ref); + XacmlPolicyUtils.debugDumpPolicyProperties(properties, LOGGER); + assertThat(properties.getProperty("refstart3.file")).isNotBlank(); + } } |