diff options
author | jhh <jorge.hernandez-herrero@att.com> | 2021-04-28 16:24:44 -0500 |
---|---|---|
committer | jhh <jorge.hernandez-herrero@att.com> | 2021-05-03 15:46:00 -0500 |
commit | c2e25b77402244fdc9f443628ec127e03606c9f6 (patch) | |
tree | 8135ed26db0b38214bc3f3429462a465fe6350e9 /feature-lifecycle/src/main/java | |
parent | 20a3082d4423a7b432187cd02b172b696e8f5f61 (diff) |
support for PAP delta updates
Issue-ID: POLICY-3187
Signed-off-by: jhh <jorge.hernandez-herrero@att.com>
Change-Id: I91b343feb28fdff6b2391387e51defbdcfbe7b0f
Signed-off-by: jhh <jorge.hernandez-herrero@att.com>
Diffstat (limited to 'feature-lifecycle/src/main/java')
3 files changed, 59 insertions, 22 deletions
diff --git a/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleFsm.java b/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleFsm.java index ea5e49f4..c4f2f873 100644 --- a/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleFsm.java +++ b/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleFsm.java @@ -268,7 +268,7 @@ public class LifecycleFsm implements Startable { List<PolicyTypeDroolsController> opControllers = policyTypesMap.values().stream() - .filter(typeController -> typeController instanceof PolicyTypeDroolsController) + .filter(PolicyTypeDroolsController.class::isInstance) .map(PolicyTypeDroolsController.class::cast) .filter(opController -> opController.getControllers().containsKey(controller.getName())) .collect(Collectors.toList()); @@ -522,13 +522,43 @@ public class LifecycleFsm implements Startable { .flatMap(entry -> entry.getValue().stream()).collect(Collectors.toList()); } - protected String getPolicyIdsMessage(List<ToscaPolicy> policies) { + /** + * Get the policy identifiers. + */ + public List<ToscaConceptIdentifier> getPolicyIds(List<ToscaPolicy> policies) { return policies.stream() + .map(ToscaPolicy::getIdentifier) .distinct() - .map(ToscaPolicy::getIdentifier).collect(Collectors.toList()) - .toString(); + .collect(Collectors.toList()); + } + + protected String getPolicyIdsMessage(List<ToscaPolicy> policies) { + return getPolicyIds(policies).toString(); + } + + protected List<ToscaPolicy> removeByPolicyId(@NonNull List<ToscaPolicy> policies, + @NonNull List<ToscaConceptIdentifier> toRemoveList) { + policies.removeIf(policy -> toRemoveList.contains(policy.getIdentifier())); + return policies; } + protected List<ToscaPolicy> removeByPolicyId(@NonNull List<ToscaConceptIdentifier> toRemoveList) { + return removeByPolicyId(getActivePolicies(), toRemoveList); + } + + protected List<ToscaPolicy> mergePolicies(@NonNull List<ToscaPolicy> addPolicies, + @NonNull List<ToscaConceptIdentifier> removePolicies) { + + if (addPolicies.isEmpty() && removePolicies.isEmpty()) { + return getActivePolicies(); + } + + List<ToscaPolicy> policies = getActivePolicies(); + policies.addAll(addPolicies); + return removeByPolicyId(new ArrayList<>(new HashSet<>(policies)), removePolicies); + } + + /** * Do I support the mandatory policy types?. */ diff --git a/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleStateRunning.java b/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleStateRunning.java index dacb6058..4c92b67b 100644 --- a/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleStateRunning.java +++ b/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleStateRunning.java @@ -122,9 +122,6 @@ public abstract class LifecycleStateRunning extends LifecycleStateDefault { @Override public boolean update(@NonNull PdpUpdate update) { - // UPDATE messages contain the complete universe of active policies in this PDP-D - // regardless if they were already functioning in the PDP-D or new. - synchronized (fsm) { if (update.getPdpHeartbeatIntervalMs() != null && !fsm.setStatusIntervalAction(update.getPdpHeartbeatIntervalMs() / 1000)) { @@ -137,6 +134,15 @@ public abstract class LifecycleStateRunning extends LifecycleStateDefault { fsm.setSubGroup(update.getPdpSubgroup()); + // Compute the desired final policy set after processing this update. + // Delta policies allows for the PAP to send us just the policies to deploy and undeploy + // Note that in this mode of operation, there may be dependent policies in the + // active inventory. For example a request to remove a controller policy in a + // delta request, may affect operational or artifact policies in use. + + List<ToscaPolicy> desiredPolicyInventory = + fsm.mergePolicies(update.getPoliciesToBeDeployed(), update.getPoliciesToBeUndeployed()); + // snapshot the active policies previous to apply the new set of active // policies as given by the PAP in the update message @@ -144,9 +150,7 @@ public abstract class LifecycleStateRunning extends LifecycleStateDefault { Map<String, List<ToscaPolicy>> activePoliciesPreUpdateMap = fsm.groupPoliciesByPolicyType(activePoliciesPreUpdate); - // update policies with the current set of active policies - - Pair<List<ToscaPolicy>, List<ToscaPolicy>> results = updatePoliciesWithResults(update.getPolicies()); + Pair<List<ToscaPolicy>, List<ToscaPolicy>> results = updatePoliciesWithResults(desiredPolicyInventory); // summary message to return in the update response to the PAP diff --git a/feature-lifecycle/src/main/java/org/onap/policy/drools/server/restful/RestLifecycleManager.java b/feature-lifecycle/src/main/java/org/onap/policy/drools/server/restful/RestLifecycleManager.java index 9af78ae6..381fd600 100644 --- a/feature-lifecycle/src/main/java/org/onap/policy/drools/server/restful/RestLifecycleManager.java +++ b/feature-lifecycle/src/main/java/org/onap/policy/drools/server/restful/RestLifecycleManager.java @@ -26,7 +26,6 @@ import io.swagger.annotations.ApiParam; import java.util.Collections; import java.util.List; import java.util.Properties; -import java.util.stream.Collectors; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; import javax.ws.rs.GET; @@ -284,11 +283,7 @@ public class RestLifecycleManager { return Response.status(Response.Status.NOT_FOUND).build(); } - List<ToscaPolicy> policies = - LifecycleFeature.getFsm().getPoliciesMap().values().stream().collect(Collectors.toList()); - policies.add(toscaPolicy); - - boolean updateResult = LifecycleFeature.getFsm().update(getPolicyUpdate(policies)); + boolean updateResult = LifecycleFeature.getFsm().update(getDeployPolicyUpdate(List.of(toscaPolicy))); return Response.status((updateResult ? Response.Status.OK : Response.Status.NOT_ACCEPTABLE)) .entity(updateResult) .build(); @@ -345,11 +340,8 @@ public class RestLifecycleManager { return Response.status(Response.Status.NOT_FOUND).build(); } - List<ToscaPolicy> policies = - LifecycleFeature.getFsm().getPoliciesMap().values().stream().collect(Collectors.toList()); - policies.removeIf(otherPolicy -> policy.getIdentifier().equals(otherPolicy.getIdentifier())); return Response.status(Response.Status.OK) - .entity(LifecycleFeature.getFsm().update(getPolicyUpdate(policies))) + .entity(LifecycleFeature.getFsm().update(getUndeployPolicyUpdate(List.of(policy)))) .build(); } @@ -448,12 +440,23 @@ public class RestLifecycleManager { return LifecycleFeature.getFsm().getPolicyTypesMap().get(policy.getTypeIdentifier()); } - private PdpUpdate getPolicyUpdate(List<ToscaPolicy> policies) { + private PdpUpdate getPolicyUpdate() { PdpUpdate update = new PdpUpdate(); update.setName(LifecycleFeature.getFsm().getName()); update.setPdpGroup(LifecycleFeature.getFsm().getGroup()); update.setPdpSubgroup(LifecycleFeature.getFsm().getSubGroup()); - update.setPolicies(policies); + return update; + } + + private PdpUpdate getDeployPolicyUpdate(List<ToscaPolicy> policies) { + PdpUpdate update = getPolicyUpdate(); + update.setPoliciesToBeDeployed(policies); + return update; + } + + private PdpUpdate getUndeployPolicyUpdate(List<ToscaPolicy> policies) { + PdpUpdate update = getPolicyUpdate(); + update.setPoliciesToBeUndeployed(LifecycleFeature.fsm.getPolicyIds(policies)); return update; } |