Alex Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame] | 1 | # ================================================================================ |
Lusheng Ji | 967cc9a | 2018-02-12 10:45:42 -0500 | [diff] [blame] | 2 | # Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. |
Alex Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame] | 3 | # ================================================================================ |
| 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 |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 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. |
| 15 | # ============LICENSE_END========================================================= |
| 16 | # |
| 17 | # ECOMP is a trademark and service mark of AT&T Intellectual Property. |
| 18 | |
Alex Shatov | f53e5e7 | 2018-01-11 11:15:56 -0500 | [diff] [blame] | 19 | """policy-client communicates with policy-engine thru REST API""" |
| 20 | |
Alex Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame] | 21 | import json |
Alex Shatov | a2b2629 | 2018-03-13 17:50:51 -0400 | [diff] [blame^] | 22 | import logging |
Alex Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame] | 23 | import re |
| 24 | |
Alex Shatov | a2b2629 | 2018-03-13 17:50:51 -0400 | [diff] [blame^] | 25 | from .policy_consts import (POLICY_BODY, POLICY_CONFIG, POLICY_ID, POLICY_NAME, |
| 26 | POLICY_VERSION) |
Alex Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame] | 27 | |
| 28 | class PolicyUtils(object): |
| 29 | """policy-client utils""" |
| 30 | _logger = logging.getLogger("policy_handler.policy_utils") |
| 31 | _policy_name_ext = re.compile('[.][0-9]+[.][a-zA-Z]+$') |
| 32 | |
| 33 | @staticmethod |
Alex Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame] | 34 | def extract_policy_id(policy_name): |
| 35 | """ policy_name = policy_id + "." + <version> + "." + <extension> |
| 36 | For instance, |
| 37 | policy_name = DCAE_alex.Config_alex_policy_number_1.3.xml |
| 38 | policy_id = DCAE_alex.Config_alex_policy_number_1 |
| 39 | policy_scope = DCAE_alex |
| 40 | policy_class = Config |
| 41 | policy_version = 3 |
| 42 | type = extension = xml |
| 43 | delimiter = "." |
| 44 | policy_class_delimiter = "_" |
| 45 | policy_name in PAP = DCAE_alex.alex_policy_number_1 |
| 46 | """ |
| 47 | if not policy_name: |
| 48 | return |
| 49 | return PolicyUtils._policy_name_ext.sub('', policy_name) |
| 50 | |
| 51 | @staticmethod |
| 52 | def parse_policy_config(policy): |
| 53 | """try parsing the config in policy.""" |
| 54 | if not policy: |
| 55 | return policy |
| 56 | config = policy.get(POLICY_BODY, {}).get(POLICY_CONFIG) |
| 57 | if config: |
Alex Shatov | a2b2629 | 2018-03-13 17:50:51 -0400 | [diff] [blame^] | 58 | policy[POLICY_BODY][POLICY_CONFIG] = Utils.safe_json_parse(config) |
Alex Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame] | 59 | return policy |
| 60 | |
| 61 | @staticmethod |
| 62 | def convert_to_policy(policy_config): |
| 63 | """wrap policy_config received from policy-engine with policy_id.""" |
| 64 | if not policy_config: |
| 65 | return |
| 66 | policy_name = policy_config.get(POLICY_NAME) |
| 67 | policy_version = policy_config.get(POLICY_VERSION) |
| 68 | if not policy_name or not policy_version: |
| 69 | return |
| 70 | policy_id = PolicyUtils.extract_policy_id(policy_name) |
| 71 | if not policy_id: |
| 72 | return |
| 73 | return {POLICY_ID:policy_id, POLICY_BODY:policy_config} |
| 74 | |
| 75 | @staticmethod |
| 76 | def select_latest_policy(policy_configs, min_version_expected=None, ignore_policy_names=None): |
| 77 | """For some reason, the policy-engine returns all version of the policy_configs. |
| 78 | DCAE-Controller is only interested in the latest version |
| 79 | """ |
| 80 | if not policy_configs: |
| 81 | return |
| 82 | latest_policy_config = {} |
| 83 | for policy_config in policy_configs: |
| 84 | policy_name = policy_config.get(POLICY_NAME) |
| 85 | policy_version = policy_config.get(POLICY_VERSION) |
| 86 | if not policy_name or not policy_version or not policy_version.isdigit(): |
| 87 | continue |
| 88 | policy_version = int(policy_version) |
| 89 | if min_version_expected and policy_version < min_version_expected: |
| 90 | continue |
| 91 | if ignore_policy_names and policy_name in ignore_policy_names: |
| 92 | continue |
| 93 | |
| 94 | if not latest_policy_config \ |
| 95 | or int(latest_policy_config[POLICY_VERSION]) < policy_version: |
| 96 | latest_policy_config = policy_config |
| 97 | |
| 98 | return PolicyUtils.parse_policy_config(PolicyUtils.convert_to_policy(latest_policy_config)) |
| 99 | |
| 100 | @staticmethod |
| 101 | def select_latest_policies(policy_configs): |
| 102 | """For some reason, the policy-engine returns all version of the policy_configs. |
| 103 | DCAE-Controller is only interested in the latest versions |
| 104 | """ |
| 105 | if not policy_configs: |
| 106 | return {} |
| 107 | policies = {} |
| 108 | for policy_config in policy_configs: |
| 109 | policy = PolicyUtils.convert_to_policy(policy_config) |
| 110 | if not policy: |
| 111 | continue |
| 112 | policy_id = policy.get(POLICY_ID) |
| 113 | policy_version = policy.get(POLICY_BODY, {}).get(POLICY_VERSION) |
| 114 | if not policy_id or not policy_version or not policy_version.isdigit(): |
| 115 | continue |
| 116 | if policy_id not in policies \ |
| 117 | or int(policy_version) > int(policies[policy_id][POLICY_BODY][POLICY_VERSION]): |
| 118 | policies[policy_id] = policy |
| 119 | |
| 120 | for policy_id in policies: |
| 121 | policies[policy_id] = PolicyUtils.parse_policy_config(policies[policy_id]) |
| 122 | |
| 123 | return policies |
Alex Shatov | a2b2629 | 2018-03-13 17:50:51 -0400 | [diff] [blame^] | 124 | |
| 125 | class Utils(object): |
| 126 | """general purpose utils""" |
| 127 | _logger = logging.getLogger("policy_handler.utils") |
| 128 | |
| 129 | @staticmethod |
| 130 | def safe_json_parse(json_str): |
| 131 | """try parsing json without exception - returns the json_str back if fails""" |
| 132 | if not json_str: |
| 133 | return json_str |
| 134 | try: |
| 135 | return json.loads(json_str) |
| 136 | except (ValueError, TypeError) as err: |
| 137 | Utils._logger.warn("unexpected json %s: %s", str(json_str), str(err)) |
| 138 | return json_str |
| 139 | |
| 140 | @staticmethod |
| 141 | def are_the_same(body_1, body_2): |
| 142 | """check whether both objects are the same""" |
| 143 | if (body_1 and not body_2) or (not body_1 and body_2): |
| 144 | Utils._logger.debug("only one is empty %s != %s", body_1, body_2) |
| 145 | return False |
| 146 | |
| 147 | if body_1 is None and body_2 is None: |
| 148 | return True |
| 149 | |
| 150 | if isinstance(body_1, list) and isinstance(body_2, list): |
| 151 | if len(body_1) != len(body_2): |
| 152 | Utils._logger.debug("len %s != %s", json.dumps(body_1), json.dumps(body_2)) |
| 153 | return False |
| 154 | |
| 155 | for val_1, val_2 in zip(body_1, body_2): |
| 156 | if not Utils.are_the_same(val_1, val_2): |
| 157 | return False |
| 158 | return True |
| 159 | |
| 160 | if isinstance(body_1, dict) and isinstance(body_2, dict): |
| 161 | if body_1.viewkeys() ^ body_2.viewkeys(): |
| 162 | Utils._logger.debug("keys %s != %s", json.dumps(body_1), json.dumps(body_2)) |
| 163 | return False |
| 164 | |
| 165 | for key, val_1 in body_1.iteritems(): |
| 166 | if not Utils.are_the_same(val_1, body_2[key]): |
| 167 | return False |
| 168 | return True |
| 169 | |
| 170 | # ... here when primitive values or mismatched types ... |
| 171 | the_same_values = (body_1 == body_2) |
| 172 | if not the_same_values: |
| 173 | Utils._logger.debug("values %s != %s", body_1, body_2) |
| 174 | return the_same_values |