X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=main%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fpdpx%2Fmain%2Frest%2FXacmlPdpApplicationManager.java;h=785d7591290406dec2ceaf8196bd61a43003611d;hb=432cec9fa6f143dad324cd11f62fb052c7da32b7;hp=a5e1d030edd28a4c365e883b883274c80e229e62;hpb=e034d785a227815f66138d1f83f49624c402aa97;p=policy%2Fxacml-pdp.git diff --git a/main/src/main/java/org/onap/policy/pdpx/main/rest/XacmlPdpApplicationManager.java b/main/src/main/java/org/onap/policy/pdpx/main/rest/XacmlPdpApplicationManager.java index a5e1d030..785d7591 100644 --- a/main/src/main/java/org/onap/policy/pdpx/main/rest/XacmlPdpApplicationManager.java +++ b/main/src/main/java/org/onap/policy/pdpx/main/rest/XacmlPdpApplicationManager.java @@ -29,7 +29,10 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.ServiceLoader; +import java.util.stream.Collectors; 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; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier; import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException; import org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider; @@ -43,6 +46,10 @@ public class XacmlPdpApplicationManager { private static ServiceLoader applicationLoader; private static Map providerActionMap = new HashMap<>(); private static List toscaPolicyTypeIdents = new ArrayList<>(); + private static List toscaPolicyIdents = new ArrayList<>(); + private static List toscaPolicies = new ArrayList<>(); + private static Map mapLoadedPolicies = new HashMap<>(); + private XacmlPdpApplicationManager() { super(); @@ -112,10 +119,82 @@ public class XacmlPdpApplicationManager { return providerActionMap.get(request.getAction()); } + /** + * getToscaPolicies. + * + * @return the map containing ToscaPolicies + */ + public static Map getToscaPolicies() { + return mapLoadedPolicies; + } + + /** + * getToscaPolicyIdentifiers. + * + * @return list of ToscaPolicyIdentifier + */ + public static List getToscaPolicyIdentifiers() { + // + // converting map to return List of ToscaPolicyIdentiers + // + return mapLoadedPolicies.keySet().stream().map(ToscaPolicy::getIdentifier).collect(Collectors.toList()); + } + public static List getToscaPolicyTypeIdents() { return toscaPolicyTypeIdents; } + /** + * Finds the appropriate application and removes the policy. + * + * @param policy Incoming policy + */ + public static void removeUndeployedPolicy(ToscaPolicy policy) { + + for (XacmlApplicationServiceProvider application : applicationLoader) { + try { + if (application.unloadPolicy(policy)) { + 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) { + + for (XacmlApplicationServiceProvider application : applicationLoader) { + try { + // + // There should be only one application per policytype. We can + // put more logic surrounding enforcement of that later. For now, + // just use the first one found. + // + if (application.canSupportPolicyType(policy.getTypeIdentifier())) { + if (application.loadPolicy(policy)) { + LOGGER.info("Loaded ToscaPolicy {} into application {}", policy.getMetadata(), + application.applicationName()); + mapLoadedPolicies.put(policy, application); + } + return; + } + } catch (XacmlApplicationException e) { + LOGGER.error("Failed to load the Tosca Policy", e); + } + } + } + /** * 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