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 /main | |
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 'main')
3 files changed, 77 insertions, 11 deletions
diff --git a/main/src/main/java/org/onap/policy/pdpx/main/comm/XacmlPdpMessage.java b/main/src/main/java/org/onap/policy/pdpx/main/comm/XacmlPdpMessage.java index 809e43d0..1253ff28 100644 --- a/main/src/main/java/org/onap/policy/pdpx/main/comm/XacmlPdpMessage.java +++ b/main/src/main/java/org/onap/policy/pdpx/main/comm/XacmlPdpMessage.java @@ -22,7 +22,6 @@ package org.onap.policy.pdpx.main.comm; -import java.net.UnknownHostException; import org.onap.policy.common.utils.network.NetworkUtil; import org.onap.policy.models.pdp.concepts.PdpStateChange; import org.onap.policy.models.pdp.concepts.PdpStatus; @@ -111,7 +110,7 @@ public class XacmlPdpMessage { status.setPdpGroup(message.getPdpGroup()); status.setPdpSubgroup(message.getPdpSubgroup()); status.setSupportedPolicyTypes(XacmlPdpApplicationManager.getToscaPolicyTypeIdents()); - status.setPolicies(XacmlPdpApplicationManager.getToscaPolicies()); + status.setPolicies(XacmlPdpApplicationManager.getToscaPolicyIdentifiers()); return status; } diff --git a/main/src/main/java/org/onap/policy/pdpx/main/comm/XacmlPdpUpdatePublisher.java b/main/src/main/java/org/onap/policy/pdpx/main/comm/XacmlPdpUpdatePublisher.java index 716421c7..4c9d0c21 100644 --- a/main/src/main/java/org/onap/policy/pdpx/main/comm/XacmlPdpUpdatePublisher.java +++ b/main/src/main/java/org/onap/policy/pdpx/main/comm/XacmlPdpUpdatePublisher.java @@ -20,6 +20,8 @@ package org.onap.policy.pdpx.main.comm; +import java.util.HashSet; +import java.util.Set; import org.onap.policy.common.endpoints.event.comm.client.TopicSinkClient; import org.onap.policy.models.pdp.concepts.PdpStatus; import org.onap.policy.models.pdp.concepts.PdpUpdate; @@ -45,9 +47,26 @@ public class XacmlPdpUpdatePublisher { public static void handlePdpUpdate(PdpUpdate message, TopicSinkClient client) { if (!message.getPolicies().isEmpty() || message.getPolicies() != null) { - // Load the policies on PDP applications - for (ToscaPolicy toscaPolicy : message.getPolicies()) { - XacmlPdpApplicationManager.loadDeployedPolicy(toscaPolicy); + + Set<ToscaPolicy> incomingPolicies = + new HashSet<>(message.getPolicies()); + Set<ToscaPolicy> deployedPolicies = + new HashSet<>(XacmlPdpApplicationManager.getToscaPolicies().keySet()); + + // Undeploy a policy + // if incoming policies do not contain the deployed policy then remove it from PDP + for (ToscaPolicy policy : deployedPolicies) { + if (!incomingPolicies.contains(policy)) { + XacmlPdpApplicationManager.removeUndeployedPolicy(policy); + } + } + + // Deploy a policy + // if deployed policies do not contain the incoming policy load it + for (ToscaPolicy policy : incomingPolicies) { + if (!deployedPolicies.contains(policy)) { + XacmlPdpApplicationManager.loadDeployedPolicy(policy); + } } } 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 09805593..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,6 +29,7 @@ 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; @@ -45,7 +46,10 @@ public class XacmlPdpApplicationManager { 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<>(); + private static List<ToscaPolicyIdentifier> toscaPolicyIdents = new ArrayList<>(); + private static List<ToscaPolicy> toscaPolicies = new ArrayList<>(); + private static Map<ToscaPolicy, XacmlApplicationServiceProvider> mapLoadedPolicies = new HashMap<>(); + private XacmlPdpApplicationManager() { super(); @@ -115,11 +119,55 @@ public class XacmlPdpApplicationManager { return providerActionMap.get(request.getAction()); } + /** + * getToscaPolicies. + * + * @return the map containing ToscaPolicies + */ + public static Map<ToscaPolicy, XacmlApplicationServiceProvider> getToscaPolicies() { + return mapLoadedPolicies; + } + + /** + * getToscaPolicyIdentifiers. + * + * @return list of ToscaPolicyIdentifier + */ + public static List<ToscaPolicyIdentifier> getToscaPolicyIdentifiers() { + // + // converting map to return List of ToscaPolicyIdentiers + // + return mapLoadedPolicies.keySet().stream().map(ToscaPolicy::getIdentifier).collect(Collectors.toList()); + } + public static List<ToscaPolicyTypeIdentifier> 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 @@ -134,7 +182,11 @@ public class XacmlPdpApplicationManager { // just use the first one found. // if (application.canSupportPolicyType(policy.getTypeIdentifier())) { - application.loadPolicy(policy); + if (application.loadPolicy(policy)) { + LOGGER.info("Loaded ToscaPolicy {} into application {}", policy.getMetadata(), + application.applicationName()); + mapLoadedPolicies.put(policy, application); + } return; } } catch (XacmlApplicationException e) { @@ -143,10 +195,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 |