From e3f1349915f425c2f0c09688a2cc6d8d246f4110 Mon Sep 17 00:00:00 2001 From: "a.sreekumar" Date: Fri, 19 Mar 2021 17:49:07 +0000 Subject: Fixing issues around deploying APEX policies with duplicate concepts Deploying invalid APEX policies with duplicate concepts resulting in corrupting all the policies in memory. This is fixed here. This entire functionality handling multiple policies will be revisited as part of https://jira.onap.org/browse/POLICY-3018 Change-Id: I751102d5dddcb92cdcede8222855e2f467bb724d Issue-ID: POLICY-3141 Signed-off-by: a.sreekumar --- .../services/onappf/handler/ApexEngineHandler.java | 60 +++++++++++++--------- 1 file changed, 35 insertions(+), 25 deletions(-) (limited to 'services/services-onappf') diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/ApexEngineHandler.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/ApexEngineHandler.java index 5a113a77d..9fe3c94ce 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/ApexEngineHandler.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/ApexEngineHandler.java @@ -24,6 +24,7 @@ package org.onap.policy.apex.services.onappf.handler; import java.io.File; import java.io.IOException; import java.util.ArrayList; +import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; @@ -133,12 +134,12 @@ public class ApexEngineHandler { List executorParamKeysToRetain = new ArrayList<>(); List schemaParamKeysToRetain = new ArrayList<>(); - List keyInfoKeystoRetain = new ArrayList<>(); - List schemaKeystoRetain = new ArrayList<>(); - List eventKeystoRetain = new ArrayList<>(); - List albumKeystoRetain = new ArrayList<>(); - List taskKeystoRetain = new ArrayList<>(); - List policyKeystoRetain = new ArrayList<>(); + Map keyInfoMapToRetain = new HashMap<>(); + Map schemaMapToRetain = new HashMap<>(); + Map eventMapToRetain = new HashMap<>(); + Map albumMapToRetain = new HashMap<>(); + Map taskMapToRetain = new HashMap<>(); + Map policyMapToRetain = new HashMap<>(); apexMainMap.values().forEach(main -> { inputParamKeysToRetain.addAll(main.getApexParameters().getEventInputParameters().keySet()); @@ -150,21 +151,21 @@ public class ApexEngineHandler { schemaParamKeysToRetain.addAll(main.getApexParameters().getEngineServiceParameters().getEngineParameters() .getContextParameters().getSchemaParameters().getSchemaHelperParameterMap().keySet()); - keyInfoKeystoRetain - .addAll(main.getActivator().getPolicyModel().getKeyInformation().getKeyInfoMap().keySet()); - schemaKeystoRetain.addAll(main.getActivator().getPolicyModel().getSchemas().getSchemasMap().keySet()); - eventKeystoRetain.addAll(main.getActivator().getPolicyModel().getEvents().getEventMap().keySet()); - albumKeystoRetain.addAll(main.getActivator().getPolicyModel().getAlbums().getAlbumsMap().keySet()); - taskKeystoRetain.addAll(main.getActivator().getPolicyModel().getTasks().getTaskMap().keySet()); - policyKeystoRetain.addAll(main.getActivator().getPolicyModel().getPolicies().getPolicyMap().keySet()); + AxPolicyModel policyModel = main.getActivator().getPolicyModel(); + keyInfoMapToRetain.putAll(policyModel.getKeyInformation().getKeyInfoMap()); + schemaMapToRetain.putAll(policyModel.getSchemas().getSchemasMap()); + eventMapToRetain.putAll(policyModel.getEvents().getEventMap()); + albumMapToRetain.putAll(policyModel.getAlbums().getAlbumsMap()); + taskMapToRetain.putAll(policyModel.getTasks().getTaskMap()); + policyMapToRetain.putAll(policyModel.getPolicies().getPolicyMap()); }); for (ApexMain main : undeployedPoliciesMainMap.values()) { handleParametersRemoval(inputParamKeysToRetain, outputParamKeysToRetain, taskParametersToRetain, executorParamKeysToRetain, schemaParamKeysToRetain, main); if (null != main.getActivator() && null != main.getActivator().getPolicyModel()) { - handleAxConceptsRemoval(keyInfoKeystoRetain, schemaKeystoRetain, eventKeystoRetain, albumKeystoRetain, - taskKeystoRetain, policyKeystoRetain, main); + handleAxConceptsRemoval(keyInfoMapToRetain, schemaMapToRetain, eventMapToRetain, albumMapToRetain, + taskMapToRetain, policyMapToRetain, main); } } } @@ -195,23 +196,23 @@ public class ApexEngineHandler { .getSchemaHelperParameterMap()::remove); } - private void handleAxConceptsRemoval(List keyInfoKeystoRetain, - List schemaKeystoRetain, List eventKeystoRetain, - List albumKeystoRetain, List taskKeystoRetain, - List policyKeystoRetain, ApexMain main) { + private void handleAxConceptsRemoval(Map keyInfoMapToRetain, + Map schemaMapToRetain, Map eventMapToRetain, + Map albumMapToRetain, Map taskMapToRetain, + Map policyMapToRetain, ApexMain main) { final AxPolicyModel policyModel = main.getActivator().getPolicyModel(); final List keyInfoKeystoRemove = policyModel.getKeyInformation().getKeyInfoMap().keySet() - .stream().filter(key -> !keyInfoKeystoRetain.contains(key)).collect(Collectors.toList()); + .stream().filter(key -> !keyInfoMapToRetain.containsKey(key)).collect(Collectors.toList()); final List schemaKeystoRemove = policyModel.getSchemas().getSchemasMap().keySet().stream() - .filter(key -> !schemaKeystoRetain.contains(key)).collect(Collectors.toList()); + .filter(key -> !schemaMapToRetain.containsKey(key)).collect(Collectors.toList()); final List eventKeystoRemove = policyModel.getEvents().getEventMap().keySet().stream() - .filter(key -> !eventKeystoRetain.contains(key)).collect(Collectors.toList()); + .filter(key -> !eventMapToRetain.containsKey(key)).collect(Collectors.toList()); final List albumKeystoRemove = policyModel.getAlbums().getAlbumsMap().keySet().stream() - .filter(key -> !albumKeystoRetain.contains(key)).collect(Collectors.toList()); + .filter(key -> !albumMapToRetain.containsKey(key)).collect(Collectors.toList()); final List taskKeystoRemove = policyModel.getTasks().getTaskMap().keySet().stream() - .filter(key -> !taskKeystoRetain.contains(key)).collect(Collectors.toList()); + .filter(key -> !taskMapToRetain.containsKey(key)).collect(Collectors.toList()); final List policyKeystoRemove = policyModel.getPolicies().getPolicyMap().keySet().stream() - .filter(key -> !policyKeystoRetain.contains(key)).collect(Collectors.toList()); + .filter(key -> !policyMapToRetain.containsKey(key)).collect(Collectors.toList()); final Map keyInfoMap = ModelService.getModel(AxKeyInformation.class).getKeyInfoMap(); final Map schemasMap = @@ -222,6 +223,15 @@ public class ApexEngineHandler { final Map taskMap = ModelService.getModel(AxTasks.class).getTaskMap(); final Map policyMap = ModelService.getModel(AxPolicies.class).getPolicyMap(); + // replace the ModelService with the right concept definition + // this can get corrupted in case of deploying policies with duplicate concept keys + keyInfoMap.putAll(keyInfoMapToRetain); + schemasMap.putAll(schemaMapToRetain); + eventMap.putAll(eventMapToRetain); + albumsMap.putAll(albumMapToRetain); + taskMap.putAll(taskMapToRetain); + policyMap.putAll(policyMapToRetain); + keyInfoKeystoRemove.forEach(keyInfoMap::remove); schemaKeystoRemove.forEach(schemasMap::remove); eventKeystoRemove.forEach(eventMap::remove); -- cgit 1.2.3-korg