Refactor xacml-pdp to remove various statics
[policy/xacml-pdp.git] / main / src / main / java / org / onap / policy / pdpx / main / rest / XacmlPdpApplicationManager.java
index 0980559..6a5555b 100644 (file)
@@ -29,6 +29,9 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.ServiceLoader;
+import java.util.stream.Collectors;
+import lombok.Getter;
+import lombok.Setter;
 import org.onap.policy.models.decisions.concepts.DecisionRequest;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
@@ -41,26 +44,22 @@ import org.slf4j.LoggerFactory;
 public class XacmlPdpApplicationManager {
     private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpApplicationManager.class);
 
-    private static boolean needsInit = true;
-    private static ServiceLoader<XacmlApplicationServiceProvider> applicationLoader;
-    private static Map<String, XacmlApplicationServiceProvider> providerActionMap = new HashMap<>();
-    private static List<ToscaPolicyTypeIdentifier> toscaPolicyTypeIdents = new ArrayList<>();
-    private static List<ToscaPolicyIdentifier> toscaPolicies = new ArrayList<>();
+    @Getter
+    @Setter
+    private static XacmlPdpApplicationManager current;
+
+    private ServiceLoader<XacmlApplicationServiceProvider> applicationLoader;
+    private Map<String, XacmlApplicationServiceProvider> providerActionMap = new HashMap<>();
+    private List<ToscaPolicyTypeIdentifier> toscaPolicyTypeIdents = new ArrayList<>();
+    private Map<ToscaPolicy, XacmlApplicationServiceProvider> mapLoadedPolicies = new HashMap<>();
 
-    private XacmlPdpApplicationManager() {
-        super();
-    }
 
     /**
      * One time to initialize the applications upon startup.
      */
-    public static void initializeApplications(Path applicationPath) {
-        //
-        // If we have already done this
-        //
-        if (! needsInit) {
-            LOGGER.error("Already initialized the applications");
-            return;
+    public XacmlPdpApplicationManager(Path applicationPath) {
+        if (LOGGER.isInfoEnabled()) {
+            LOGGER.info("Initialization applications {}", applicationPath.toAbsolutePath());
         }
         //
         // Load service
@@ -70,8 +69,10 @@ public class XacmlPdpApplicationManager {
         // Iterate through the applications for actions and supported policy types
         //
         for (XacmlApplicationServiceProvider application : applicationLoader) {
-            LOGGER.info("Application {} supports {}", application.applicationName(),
+            if (LOGGER.isInfoEnabled()) {
+                LOGGER.info("Application {} supports {}", application.applicationName(),
                     application.supportedPolicyTypes());
+            }
             //
             // We are not going to make this available unless the application can
             // install correctly.
@@ -108,23 +109,70 @@ public class XacmlPdpApplicationManager {
         //
         // we have initialized
         //
-        needsInit = false;
+        LOGGER.info("Finished applications initialization {}", providerActionMap);
+
     }
 
-    public static XacmlApplicationServiceProvider findApplication(DecisionRequest request) {
+    public XacmlApplicationServiceProvider findApplication(DecisionRequest request) {
         return providerActionMap.get(request.getAction());
     }
 
-    public static List<ToscaPolicyTypeIdentifier> getToscaPolicyTypeIdents() {
+    /**
+     * getToscaPolicies.
+     *
+     * @return the map containing ToscaPolicies
+     */
+    public Map<ToscaPolicy, XacmlApplicationServiceProvider> getToscaPolicies() {
+        return mapLoadedPolicies;
+    }
+
+    /**
+     * getToscaPolicyIdentifiers.
+     *
+     * @return list of ToscaPolicyIdentifier
+     */
+    public List<ToscaPolicyIdentifier> getToscaPolicyIdentifiers() {
+        //
+        // converting map to return List of ToscaPolicyIdentiers
+        //
+        return mapLoadedPolicies.keySet().stream().map(ToscaPolicy::getIdentifier).collect(Collectors.toList());
+    }
+
+    public List<ToscaPolicyTypeIdentifier> getToscaPolicyTypeIdents() {
         return toscaPolicyTypeIdents;
     }
 
+    /**
+     * Finds the appropriate application and removes the policy.
+     *
+     * @param policy Incoming policy
+     */
+    public void removeUndeployedPolicy(ToscaPolicy policy) {
+
+        for (XacmlApplicationServiceProvider application : applicationLoader) {
+            try {
+                if (application.unloadPolicy(policy)) {
+                    if (LOGGER.isInfoEnabled()) {
+                        LOGGER.info("Unloaded ToscaPolicy {} from application {}", policy.getMetadata(),
+                            application.applicationName());
+                    }
+                    if (mapLoadedPolicies.remove(policy) == null) {
+                        LOGGER.error("Failed to remove unloaded policy {} from map size {}", policy.getMetadata(),
+                                mapLoadedPolicies.size());
+                    }
+                }
+            } catch (XacmlApplicationException e) {
+                LOGGER.error("Failed to undeploy the Tosca Policy", e);
+            }
+        }
+    }
+
     /**
      * Finds the appropriate application and loads the policy.
      *
      * @param policy Incoming policy
      */
-    public static void loadDeployedPolicy(ToscaPolicy policy) {
+    public void loadDeployedPolicy(ToscaPolicy policy) {
 
         for (XacmlApplicationServiceProvider application : applicationLoader) {
             try {
@@ -134,7 +182,13 @@ public class XacmlPdpApplicationManager {
                 // just use the first one found.
                 //
                 if (application.canSupportPolicyType(policy.getTypeIdentifier())) {
-                    application.loadPolicy(policy);
+                    if (application.loadPolicy(policy)) {
+                        if (LOGGER.isInfoEnabled()) {
+                            LOGGER.info("Loaded ToscaPolicy {} into application {}", policy.getMetadata(),
+                                application.applicationName());
+                        }
+                        mapLoadedPolicies.put(policy, application);
+                    }
                     return;
                 }
             } catch (XacmlApplicationException e) {
@@ -143,10 +197,6 @@ public class XacmlPdpApplicationManager {
         }
     }
 
-    public static List<ToscaPolicyIdentifier> getToscaPolicies() {
-        return toscaPolicies;
-    }
-
     /**
      * Returns the current count of policy types supported. This could be misleading a bit
      * as some applications can support wildcard of policy types. Eg. onap.Monitoring.* as
@@ -154,7 +204,7 @@ public class XacmlPdpApplicationManager {
      *
      * @return Total count added from all applications
      */
-    public static long getPolicyTypeCount() {
+    public long getPolicyTypeCount() {
         long types = 0;
         for (XacmlApplicationServiceProvider application : applicationLoader) {
             types += application.supportedPolicyTypes().size();
@@ -162,14 +212,16 @@ public class XacmlPdpApplicationManager {
         return types;
     }
 
-    private static void initializeApplicationPath(Path basePath, XacmlApplicationServiceProvider application)
+    private void initializeApplicationPath(Path basePath, XacmlApplicationServiceProvider application)
             throws XacmlApplicationException {
         //
         // Making an assumption that all application names are unique, and
         // they can result in a valid directory being created.
         //
         Path path = Paths.get(basePath.toAbsolutePath().toString(), application.applicationName());
-        LOGGER.info("initializeApplicationPath {} at this path {}", application.applicationName(), path);
+        if (LOGGER.isInfoEnabled()) {
+            LOGGER.info("initializeApplicationPath {} at this path {}", application.applicationName(), path);
+        }
         //
         // Create that the directory if it does not exist. Ideally
         // this is only for testing, but could be used for production
@@ -183,7 +235,7 @@ public class XacmlPdpApplicationManager {
                 //
                 Files.createDirectory(path);
             } catch (IOException e) {
-                LOGGER.error("Failed to create application directory", e);
+                LOGGER.error("Failed to create application directory {}", path.toAbsolutePath().toString(), e);
             }
         }
         //
@@ -191,5 +243,4 @@ public class XacmlPdpApplicationManager {
         //
         application.initialize(path);
     }
-
 }