From 0095ee39734b4f58ec8fc72fc09d7e3bc167d274 Mon Sep 17 00:00:00 2001 From: "Schmalzried, Terry (ts862m)" Date: Mon, 25 Nov 2019 18:09:53 -0500 Subject: policy lib 3.x support Issue-ID: DCAEGEN2-1548 Change-Id: Id1c10f08f1375554aa744332bdfe37e1d6afc9d8 Signed-off-by: Schmalzried, Terry (ts862m) --- onap-dcae-dcaepolicy-lib/README.md | 2 +- .../onap_dcae_dcaepolicy_lib/dcae_policy.py | 20 +++++++------- .../onap_dcae_dcaepolicy_lib/policies_output.py | 13 +++++---- onap-dcae-dcaepolicy-lib/pom.xml | 4 +-- onap-dcae-dcaepolicy-lib/setup.py | 4 +-- onap-dcae-dcaepolicy-lib/tests/test_dcae_policy.py | 32 +++++++++++----------- 6 files changed, 39 insertions(+), 36 deletions(-) (limited to 'onap-dcae-dcaepolicy-lib') diff --git a/onap-dcae-dcaepolicy-lib/README.md b/onap-dcae-dcaepolicy-lib/README.md index 0273b7b..b279a23 100644 --- a/onap-dcae-dcaepolicy-lib/README.md +++ b/onap-dcae-dcaepolicy-lib/README.md @@ -15,7 +15,7 @@ python setup.py sdist upload ### **requirements.txt** ```python -onap-dcae-dcaepolicy-lib==2.4.1 +onap-dcae-dcaepolicy-lib==2.5.0 ``` ### **tasks.py** diff --git a/onap-dcae-dcaepolicy-lib/onap_dcae_dcaepolicy_lib/dcae_policy.py b/onap-dcae-dcaepolicy-lib/onap_dcae_dcaepolicy_lib/dcae_policy.py index a8d8d70..68a9858 100644 --- a/onap-dcae-dcaepolicy-lib/onap_dcae_dcaepolicy_lib/dcae_policy.py +++ b/onap-dcae-dcaepolicy-lib/onap_dcae_dcaepolicy_lib/dcae_policy.py @@ -1,5 +1,5 @@ # ================================================================================ -# Copyright (c) 2017-2018 AT&T Intellectual Property. All rights reserved. +# Copyright (c) 2017-2019 AT&T Intellectual Property. All rights reserved. # ================================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -126,7 +126,7 @@ class Policies(object): property_policy_filter = target.node.properties.get(POLICY_FILTER) if property_policy_filter: policy_filter = deepcopy(dict( - (k, v) for (k, v) in dict(property_policy_filter).iteritems() + (k, v) for (k, v) in dict(property_policy_filter).items() if v or isinstance(v, (int, float)) )) Policies._fix_policy_filter(policy_filter) @@ -140,7 +140,7 @@ class Policies(object): filtered_policies = target.instance.runtime_properties.get(POLICIES_FILTERED) if not filtered_policies or not isinstance(filtered_policies, dict): return True - for (policy_id, policy) in filtered_policies.iteritems(): + for (policy_id, policy) in filtered_policies.items(): Policies._add_policy(policy_id, policy, False, policies) return True @@ -150,9 +150,8 @@ class Policies(object): if not policies: return {} - return dict((policy_id, policy.get(POLICY_BODY)) - for policy_id, policy in policies.iteritems() if policy.get(POLICY_BODY) - ) + return {policy_id: policy.get(POLICY_BODY) + for policy_id, policy in policies.items() if policy.get(POLICY_BODY)} @staticmethod def gather_policies_to_node(): @@ -238,11 +237,12 @@ class Policies(object): new_policies = dict((policy_id, policy) for policy_filter_id in policy_filters for (policy_id, policy) in added_policies.get(policy_filter_id, {}) - .get(POLICIES, {}).iteritems()) + .get(POLICIES, {}).items()) ctx.logger.info("new_policies: {0}".format(json.dumps(new_policies))) - for (policy_id, policy) in new_policies.iteritems(): + for policy_id in new_policies: + policy = new_policies.get(policy_id) deployed_policy = policies.get(policy_id) if not deployed_policy: policies[policy_id] = policy @@ -363,8 +363,8 @@ class Policies(object): """returns the list of policy_body objects if policy_body exists""" if isinstance(selected_policies, dict): return deepcopy([policy.get(POLICY_BODY) - for policy in selected_policies.values() if policy.get(POLICY_BODY)]) + for policy in selected_policies.itervalues() if policy.get(POLICY_BODY)]) policies = ctx.instance.runtime_properties.get(POLICIES, {}) return deepcopy([policy.get(POLICY_BODY) - for policy in policies.values() if policy.get(POLICY_BODY)]) + for policy in policies.itervalues() if policy.get(POLICY_BODY)]) diff --git a/onap-dcae-dcaepolicy-lib/onap_dcae_dcaepolicy_lib/policies_output.py b/onap-dcae-dcaepolicy-lib/onap_dcae_dcaepolicy_lib/policies_output.py index d8125c0..0034b9c 100644 --- a/onap-dcae-dcaepolicy-lib/onap_dcae_dcaepolicy_lib/policies_output.py +++ b/onap-dcae-dcaepolicy-lib/onap_dcae_dcaepolicy_lib/policies_output.py @@ -1,5 +1,5 @@ # ================================================================================ -# Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. +# Copyright (c) 2019 AT&T Intellectual Property. All rights reserved. # ================================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -20,7 +20,10 @@ import base64 import json -import urllib +try: + from urllib.parse import quote +except ImportError: + from urllib import quote import uuid from datetime import datetime @@ -51,7 +54,7 @@ class PoliciesOutput(object): def _gen_txn_operation(verb, service_component_name, key=None, value=None): """returns the properly formatted operation to be used inside transaction""" key = PoliciesOutput.POLICIES_FOLDER_MASK.format( - service_component_name, urllib.quote(key or "") + service_component_name, quote(key or "") ) if value: return {"KV": {"Verb": verb, "Key": key, "Value": base64.b64encode(value)}} @@ -108,7 +111,7 @@ class PoliciesOutput(object): store_policies = [ PoliciesOutput._gen_txn_operation(PoliciesOutput.OPERATION_SET, service_component_name, "items/" + policy_id, json.dumps(policy_body)) - for policy_id, policy_body in policy_bodies.iteritems() + for policy_id, policy_body in policy_bodies.items() ] txn = [ PoliciesOutput._gen_txn_operation( @@ -117,7 +120,7 @@ class PoliciesOutput(object): PoliciesOutput.OPERATION_SET, service_component_name, "event", json.dumps(event)) ] idx_step = PoliciesOutput.MAX_OPS_PER_TXN - len(txn) - for idx in xrange(0, len(store_policies), idx_step): + for idx in range(0, len(store_policies), idx_step): txn += store_policies[idx : idx + idx_step] if not PoliciesOutput._run_transaction("store_policies", txn): return False diff --git a/onap-dcae-dcaepolicy-lib/pom.xml b/onap-dcae-dcaepolicy-lib/pom.xml index ecbbea7..3bc3719 100644 --- a/onap-dcae-dcaepolicy-lib/pom.xml +++ b/onap-dcae-dcaepolicy-lib/pom.xml @@ -1,7 +1,7 @@