bump poms to 2.1.4-SNAPSHOT
[policy/xacml-pdp.git] / applications / common / src / main / java / org / onap / policy / pdp / xacml / application / common / XacmlPolicyUtils.java
index 46742af..6f2a9f0 100644 (file)
@@ -34,6 +34,7 @@ import java.io.OutputStream;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
+import java.util.Map.Entry;
 import java.util.Properties;
 import java.util.Set;
 import java.util.StringJoiner;
@@ -51,6 +52,9 @@ import org.slf4j.LoggerFactory;
 public class XacmlPolicyUtils {
 
     private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPolicyUtils.class);
+
+    public static final String XACML_PROPERTY_FILE = "xacml.properties";
+
     private static final String DOT_FILE_SUFFIX = ".file";
     private static final String NOT_FOUND_MESSAGE = "NOT FOUND";
 
@@ -162,7 +166,7 @@ public class XacmlPolicyUtils {
         //
         int id = 1;
         while (true) {
-            String refId = "ref" + id;
+            String refId = "root" + id;
             if (rootPolicies.contains(refId)) {
                 id++;
             } else {
@@ -213,6 +217,50 @@ public class XacmlPolicyUtils {
         return properties;
     }
 
+    /**
+     * 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.
@@ -322,10 +370,16 @@ public class XacmlPolicyUtils {
      * @throws IOException If unable to read file
      */
     public static Properties loadXacmlProperties(Path propertyPath) throws IOException {
-        LOGGER.debug("Loading xacml properties {}", propertyPath);
+        LOGGER.info("Loading xacml properties {}", propertyPath);
         try (InputStream is = Files.newInputStream(propertyPath)) {
             Properties properties = new Properties();
             properties.load(is);
+            if (LOGGER.isInfoEnabled()) {
+                LOGGER.info("Loaded xacml properties {} {}", System.lineSeparator(), properties);
+                for (Entry<Object, Object> entrySet : properties.entrySet()) {
+                    LOGGER.info("{} -> {}", entrySet.getKey(), entrySet.getValue());
+                }
+            }
             return properties;
         }
     }
@@ -336,6 +390,9 @@ public class XacmlPolicyUtils {
      * @throws IOException If unable to store the file.
      */
     public static void storeXacmlProperties(Properties properties, Path propertyPath) throws IOException {
+        if (LOGGER.isInfoEnabled()) {
+            LOGGER.info("Storing xacml properties {} {} {}", properties, System.lineSeparator(), propertyPath);
+        }
         try (OutputStream os = Files.newOutputStream(propertyPath)) {
             String strComments = "#";
             properties.store(os, strComments);
@@ -349,9 +406,10 @@ public class XacmlPolicyUtils {
      * @return Path to rootPath/xacml.properties file
      */
     public static Path getPropertiesPath(Path rootPath) {
-        return Paths.get(rootPath.toAbsolutePath().toString(), "xacml.properties");
+        return Paths.get(rootPath.toAbsolutePath().toString(), XACML_PROPERTY_FILE);
     }
 
+    @FunctionalInterface
     public interface FileCreator {
         public File createAFile(String filename) throws IOException;
 
@@ -379,7 +437,7 @@ public class XacmlPolicyUtils {
             //
             // Now we create a new xacml.properties in the temporary folder location
             //
-            File propertiesFile = creator.createAFile("xacml.properties");
+            File propertiesFile = creator.createAFile(XACML_PROPERTY_FILE);
             //
             // Iterate through any root policies defined
             //
@@ -388,7 +446,7 @@ public class XacmlPolicyUtils {
                 // Get a file
                 //
                 Path rootPath = Paths.get(properties.getProperty(root + DOT_FILE_SUFFIX));
-                LOGGER.debug("Root file {} {}", rootPath, rootPath.getFileName());
+                LOGGER.info("Root file {} {}", rootPath, rootPath.getFileName());
                 //
                 // Construct new path for the root policy
                 //
@@ -411,7 +469,7 @@ public class XacmlPolicyUtils {
                 // Get a file
                 //
                 Path refPath = Paths.get(properties.getProperty(referenced + DOT_FILE_SUFFIX));
-                LOGGER.debug("Referenced file {} {}", refPath, refPath.getFileName());
+                LOGGER.info("Referenced file {} {}", refPath, refPath.getFileName());
                 //
                 // Construct new path for the root policy
                 //