From b73ceb906a6b8feabe88383ed35121dffbc20201 Mon Sep 17 00:00:00 2001 From: jhh Date: Mon, 11 Jan 2021 07:39:17 -0600 Subject: sort deploy and undeploy policy lists An order is imposed in the deployment and undeployment actions when the list of active policies is retrieved from PAP. This is to ensure that the operations are applied in a sane way, for example to try to prevent to undeploy policies before deleting a controller, etc .. The deployment order is 1) native controller, 2) native rule, and 3) non-native policies. The undeployment order is 1) non-native, 2) native rule, and 3) native controller policies. Issue-ID: POLICY-2762 Signed-off-by: jhh Change-Id: Icb15dcec87fd5d9917ee152ab15ca7277e13590a --- .../onap/policy/drools/lifecycle/LifecycleFsm.java | 66 +++++++++++++++++++++- 1 file changed, 63 insertions(+), 3 deletions(-) (limited to 'feature-lifecycle/src/main/java/org/onap') 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 ef47e1d6..5478136f 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 @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2021 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -22,6 +22,7 @@ package org.onap.policy.drools.lifecycle; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -347,13 +348,61 @@ public class LifecycleFsm implements Startable { protected List getDeployablePoliciesAction(@NonNull List policies) { List deployPolicies = new ArrayList<>(policies); deployPolicies.removeAll(policiesMap.values()); - return deployPolicies; + + // Ensure that the sequence of policy deployments is sane to minimize potential errors, + // First policies to deploy are the controller related ones, those that affect the lifecycle of + // controllers, starting with the ones that affect the existence of the controller (native controller), + // second the ones that "brain" the controller with application logic (native artifacts). + // Lastly the application specific ones such as operational policies. + + // group policies by policy types + Map> policyTypeGroups = groupPoliciesByPolicyType(deployPolicies); + + // place native controller policies at the start of the list + List orderedDeployableList = + new ArrayList<>(policyTypeGroups.getOrDefault(POLICY_TYPE_DROOLS_NATIVE_CONTROLLER.getName(), + Collections.EMPTY_LIST)); + + // add to the working list the native controller policies + orderedDeployableList.addAll( + policyTypeGroups.getOrDefault(POLICY_TYPE_DROOLS_NATIVE_RULES.getName(), Collections.EMPTY_LIST)); + + // place non-native policies to place at the end of the list + orderedDeployableList.addAll(getNonNativePolicies(policyTypeGroups)); + + return orderedDeployableList; } protected List getUndeployablePoliciesAction(@NonNull List policies) { List undeployPolicies = new ArrayList<>(policiesMap.values()); undeployPolicies.removeAll(policies); - return undeployPolicies; + if (undeployPolicies.isEmpty()) { + return undeployPolicies; + } + + // Ensure that the sequence of policy undeployments is sane to minimize potential errors, + // as it is assumed not smart ordering from the policies sent by the PAP. + // First policies to undeploy are those that are only of relevance within a drools container, + // such as the operational policies. The next set of policies to undeploy are those that + // affect the overall PDP-D application support, firstly the ones that supports the + // application software wiring (native rules policies), and second those that relate + // to the PDP-D controllers lifecycle. + + // group policies by policy types + Map> policyTypeGroups = groupPoliciesByPolicyType(undeployPolicies); + + // place controller only (non-native policies) at the start of the list of the undeployment list + List orderedUndeployableList = getNonNativePolicies(policyTypeGroups); + + // add to the working list the native rules policies if any + orderedUndeployableList.addAll( + policyTypeGroups.getOrDefault(POLICY_TYPE_DROOLS_NATIVE_RULES.getName(), Collections.EMPTY_LIST)); + + // finally add to the working list native controller policies if any + orderedUndeployableList.addAll( + policyTypeGroups.getOrDefault(POLICY_TYPE_DROOLS_NATIVE_CONTROLLER.getName(), Collections.EMPTY_LIST)); + + return orderedUndeployableList; } protected void deployedPolicyAction(@NonNull ToscaPolicy policy) { @@ -378,6 +427,17 @@ public class LifecycleFsm implements Startable { return policyTypesMap.get(policyType); } + protected Map> groupPoliciesByPolicyType(List deployPolicies) { + return deployPolicies.stream().collect(Collectors.groupingBy(policy -> policy.getTypeIdentifier().getName())); + } + + protected List getNonNativePolicies(@NonNull Map> policyTypeGroups) { + return policyTypeGroups.entrySet().stream() + .filter(entry -> !entry.getKey().equals(POLICY_TYPE_DROOLS_NATIVE_RULES.getName()) + && !entry.getKey().equals(POLICY_TYPE_DROOLS_NATIVE_CONTROLLER.getName())) + .flatMap(entry -> entry.getValue().stream()).collect(Collectors.toList()); + } + /** * Do I support the mandatory policy types?. */ -- cgit