aboutsummaryrefslogtreecommitdiffstats
path: root/feature-lifecycle/src/main
diff options
context:
space:
mode:
authorjhh <jorge.hernandez-herrero@att.com>2021-01-11 07:39:17 -0600
committerjhh <jorge.hernandez-herrero@att.com>2021-01-11 07:39:17 -0600
commitb73ceb906a6b8feabe88383ed35121dffbc20201 (patch)
tree1ad749ff10e6d9abadc8a74b6280443ab88b3230 /feature-lifecycle/src/main
parenta4e03e4efd11858f4ea69ea97c4c273e0792daa4 (diff)
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 <jorge.hernandez-herrero@att.com> Change-Id: Icb15dcec87fd5d9917ee152ab15ca7277e13590a
Diffstat (limited to 'feature-lifecycle/src/main')
-rw-r--r--feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleFsm.java66
1 files changed, 63 insertions, 3 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 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<ToscaPolicy> getDeployablePoliciesAction(@NonNull List<ToscaPolicy> policies) {
List<ToscaPolicy> 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<String, List<ToscaPolicy>> policyTypeGroups = groupPoliciesByPolicyType(deployPolicies);
+
+ // place native controller policies at the start of the list
+ List<ToscaPolicy> 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<ToscaPolicy> getUndeployablePoliciesAction(@NonNull List<ToscaPolicy> policies) {
List<ToscaPolicy> 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<String, List<ToscaPolicy>> policyTypeGroups = groupPoliciesByPolicyType(undeployPolicies);
+
+ // place controller only (non-native policies) at the start of the list of the undeployment list
+ List<ToscaPolicy> 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<String, List<ToscaPolicy>> groupPoliciesByPolicyType(List<ToscaPolicy> deployPolicies) {
+ return deployPolicies.stream().collect(Collectors.groupingBy(policy -> policy.getTypeIdentifier().getName()));
+ }
+
+ protected List<ToscaPolicy> getNonNativePolicies(@NonNull Map<String, List<ToscaPolicy>> 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?.
*/