1 # -------------------------------------------------------------------------
2 # Copyright (c) 2015-2017 AT&T Intellectual Property
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 # -------------------------------------------------------------------------
21 from collections import defaultdict
23 from osdf.utils.programming_utils import dot_notation, list_flatten
26 def group_policies_gen(flat_policies, config):
27 """Filter policies using the following steps:
28 1. Apply prioritization among the policies that are sharing the same policy type and resource type
29 2. Remove redundant policies that may applicable across different types of resource
30 3. Filter policies based on type and return
31 :param flat_policies: list of flat policies
32 :return: Filtered policies
34 filtered_policies = defaultdict(list)
36 policies = [x for x in flat_policies if x[list(x.keys())[0]]["type"]] # drop ones without 'type'
37 priority = config.get('policy_info', {}).get('prioritization_attributes', {})
38 aggregated_policies = dict()
40 attrs = [dot_notation(plc[list(plc.keys())[0]], dot_path) for key in priority.keys() for dot_path in priority[key]]
41 attrs_list = [x if isinstance(x, list) else [x] for x in attrs]
42 attributes = [list_flatten(x) if isinstance(x, list) else x for x in attrs_list]
43 for y in itertools.product(*attributes):
44 aggregated_policies.setdefault(y, [])
45 aggregated_policies[y].append(plc)
47 for key in aggregated_policies.keys():
48 #aggregated_policies[key].sort(key=lambda x: x['priority'], reverse=True)
49 prioritized_policy = aggregated_policies[key][0]
50 if list(prioritized_policy.keys())[0] not in policy_name:
51 # TODO: Check logic here... should policy appear only once across all groups?
52 filtered_policies[prioritized_policy[list(prioritized_policy.keys())[0]]['type']].append(prioritized_policy)
53 policy_name.append(list(prioritized_policy.keys())[0])
55 return filtered_policies
58 def policy_name_as_regex(policy_name):
59 """Get the correct policy name as a regex
60 (e.g. OOF_HAS_vCPE.cloudAttributePolicy ends up in policy as OOF_HAS_vCPE.Config_MS_cloudAttributePolicy.1.xml
61 So, for now, we query it as OOF_HAS_vCPE..*aicAttributePolicy.*)
62 :param policy_name: Example: OOF_HAS_vCPE.aicAttributePolicy
63 :return: regexp for policy: Example: OOF_HAS_vCPE..*aicAttributePolicy.*
65 p = policy_name.partition('.')
66 return p[0] + p[1] + ".*" + p[2] + ".*"
69 def retrieve_node(req_json, reference):
71 Get the child node(s) from the dot-notation [reference] and parent [req_json].
72 For placement and other requests, there are encoded JSONs inside the request or policy,
73 so we need to expand it and then do a search over the parent plus expanded JSON.
75 req_json_copy = copy.deepcopy(req_json)
76 info = dot_notation(req_json_copy, reference)
77 return list_flatten(info) if isinstance(info, list) else info