From f1e95cd95c05be145c3fde9f08404724937edf49 Mon Sep 17 00:00:00 2001 From: Ankitkumar Patel Date: Tue, 27 Mar 2018 11:54:53 -0400 Subject: Updates to address new HPA policies Issue-ID: OPTFRA-100 Change-Id: Iaff7b50dc5394546cc46591bef9b054cd6cde792 Signed-off-by: Ankitkumar Patel --- osdf/adapters/policy/utils.py | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) (limited to 'osdf/adapters') diff --git a/osdf/adapters/policy/utils.py b/osdf/adapters/policy/utils.py index 9acfa2a..95d03af 100644 --- a/osdf/adapters/policy/utils.py +++ b/osdf/adapters/policy/utils.py @@ -19,7 +19,7 @@ import copy import json from collections import defaultdict - +import itertools from osdf.utils.programming_utils import dot_notation, list_flatten @@ -33,13 +33,13 @@ def group_policies(flat_policies): """ filtered_policies = defaultdict(list) policy_name = [] - policies = [x for x in flat_policies if x['content'].get('policy_type')] # drop ones without 'policy_type' + policies = [x for x in flat_policies if x['content'].get('policyType')] # drop ones without 'policy_type' policy_types = set([x['content'].get('policyType') for x in policies]) aggregated_policies = dict((x, defaultdict(list)) for x in policy_types) for policy in policies: policy_type = policy['content'].get('policyType') - for resource in policy['content'].get('resourceInstanceType', []): + for resource in policy['content'].get('resources', []): aggregated_policies[policy_type][resource].append(policy) for policy_type in aggregated_policies: @@ -54,6 +54,38 @@ def group_policies(flat_policies): return filtered_policies +def group_policies_gen(flat_policies, config): + """Filter policies using the following steps: + 1. Apply prioritization among the policies that are sharing the same policy type and resource type + 2. Remove redundant policies that may applicable across different types of resource + 3. Filter policies based on type and return + :param flat_policies: list of flat policies + :return: Filtered policies + """ + filtered_policies = defaultdict(list) + policy_name = [] + policies = [x for x in flat_policies if x['content'].get('policyType')] # drop ones without 'policy_type' + priority = config.get('policy_info', {}).get('prioritization_attributes', {}) + aggregated_policies = dict() + for plc in policies: + attrs = [dot_notation(plc, dot_path) for key in priority.keys() for dot_path in priority[key]] + attrs_list = [x if isinstance(x, list) else [x] for x in attrs] + attributes = [list_flatten(x) if isinstance(x, list) else x for x in attrs_list] + for y in itertools.product(*attributes): + aggregated_policies.setdefault(y, []) + aggregated_policies[y].append(plc) + + for key in aggregated_policies.keys(): + aggregated_policies[key].sort(key=lambda x: x['priority'], reverse=True) + prioritized_policy = aggregated_policies[key][0] + if prioritized_policy['policyName'] not in policy_name: + # TODO: Check logic here... should policy appear only once across all groups? + filtered_policies[prioritized_policy['content']['policyType']].append(prioritized_policy) + policy_name.append(prioritized_policy['policyName']) + + return filtered_policies + + def policy_name_as_regex(policy_name): """Get the correct policy name as a regex (e.g. OOF_HAS_vCPE.cloudAttributePolicy ends up in policy as OOF_HAS_vCPE.Config_MS_cloudAttributePolicy.1.xml -- cgit 1.2.3-korg