From 1c4c6626f2abb24bcb35af581c6e57130f0ccae2 Mon Sep 17 00:00:00 2001 From: Alex Shatov Date: Fri, 10 Aug 2018 11:03:02 -0400 Subject: 4.0.1 fix for removing the non-matching policy - fixed removing the policy that no longer match the filter = sending an empty policy_filter_matches for the policy_id - cleaner workaround on getting the updated policy when policy-engine sent policy-update notification before finishing the update on there side = using the collection of expected_versions instead of min_version_expected - some minor refactoring on policy_matcher and audit Change-Id: Ica3cb810378e61d6991c616f88265ff170d32a64 Signed-off-by: Alex Shatov Issue-ID: DCAEGEN2-492 --- policyhandler/policy_matcher.py | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) (limited to 'policyhandler/policy_matcher.py') diff --git a/policyhandler/policy_matcher.py b/policyhandler/policy_matcher.py index 8406f14..71b5ce8 100644 --- a/policyhandler/policy_matcher.py +++ b/policyhandler/policy_matcher.py @@ -70,6 +70,13 @@ class PolicyMatcher(object): latest_policies = pdp_response.get(LATEST_POLICIES, {}) errored_policies = pdp_response.get(ERRORED_POLICIES, {}) + latest_policies, changed_policies, policy_filter_matches = PolicyMatcher._match_policies( + audit, latest_policies, deployed_policies, deployed_policy_filters) + + errored_policies = dict((policy_id, policy) + for (policy_id, policy) in errored_policies.items() + if deployed_policies.get(policy_id, {}).get(POLICY_VERSIONS)) + removed_policies = dict( (policy_id, True) for (policy_id, deployed_policy) in deployed_policies.items() @@ -78,11 +85,6 @@ class PolicyMatcher(object): and policy_id not in errored_policies ) - latest_policies, changed_policies, policy_filter_matches = PolicyMatcher.match_policies( - audit, latest_policies, deployed_policies, deployed_policy_filters) - errored_policies, _, _ = PolicyMatcher.match_policies( - audit, errored_policies, deployed_policies, deployed_policy_filters) - return ({LATEST_POLICIES: latest_policies, ERRORED_POLICIES: errored_policies}, PolicyUpdateMessage(changed_policies, removed_policies, @@ -110,20 +112,21 @@ class PolicyMatcher(object): @staticmethod def match_to_deployed_policies(audit, policies_updated, policies_removed): """match the policies_updated, policies_removed versus deployed policies""" - deployed_policies, deployed_policy_filters = DeployHandler.get_deployed_policies( - audit) + deployed_policies, deployed_policy_filters = DeployHandler.get_deployed_policies(audit) if not audit.is_success(): return {}, {}, {} - _, changed_policies, policy_filter_matches = PolicyMatcher.match_policies( + _, changed_policies, policy_filter_matches = PolicyMatcher._match_policies( audit, policies_updated, deployed_policies, deployed_policy_filters) - policies_removed, _, _ = PolicyMatcher.match_policies( - audit, policies_removed, deployed_policies, deployed_policy_filters) + + policies_removed = dict((policy_id, policy) + for (policy_id, policy) in policies_removed.items() + if deployed_policies.get(policy_id, {}).get(POLICY_VERSIONS)) return changed_policies, policies_removed, policy_filter_matches @staticmethod - def match_policies(audit, policies, deployed_policies, deployed_policy_filters): + def _match_policies(audit, policies, deployed_policies, deployed_policy_filters): """ Match policies to deployed policies either by policy_id or the policy-filters. @@ -150,10 +153,11 @@ class PolicyMatcher(object): deployed_policy.get(POLICY_VERSIONS, {}).keys())) if policy_changed: changed_policies[policy_id] = policy + policy_filter_matches[policy_id] = {} in_filters = False for (policy_filter_id, policy_filter) in deployed_policy_filters.items(): - if not PolicyMatcher.match_policy_to_filter( + if not PolicyMatcher._match_policy_to_filter( audit, policy_id, policy, policy_filter_id, policy_filter.get(POLICY_FILTER)): continue @@ -171,7 +175,7 @@ class PolicyMatcher(object): return matching_policies, changed_policies, policy_filter_matches @staticmethod - def match_policy_to_filter(audit, policy_id, policy, policy_filter_id, policy_filter): + def _match_policy_to_filter(audit, policy_id, policy, policy_filter_id, policy_filter): """Match the policy to the policy-filter""" if not policy_id or not policy or not policy_filter or not policy_filter_id: return False @@ -188,7 +192,7 @@ class PolicyMatcher(object): if not policy_name: return False - log_line = "policy {} to filter id {}: {}".format(json.dumps(policy_body), + log_line = "policy {} to filter id {}: {}".format(json.dumps(policy), policy_filter_id, json.dumps(policy_filter)) # PolicyMatcher._logger.debug(audit.debug("matching {}...".format(log_line))) @@ -214,7 +218,7 @@ class PolicyMatcher(object): filter_config_name = policy_filter.get("configName") policy_config_name = matching_conditions.get("ConfigName") - if filter_onap_name and filter_onap_name != policy_onap_name: + if filter_onap_name and filter_config_name != policy_config_name: PolicyMatcher._logger.debug( audit.debug("not match by configName: {} != {}: {}" .format(policy_config_name, filter_config_name, log_line))) -- cgit 1.2.3-korg