diff options
author | Sastry Isukapalli <sastry@research.att.com> | 2018-03-17 02:40:15 -0400 |
---|---|---|
committer | Sastry Isukapalli <sastry@research.att.com> | 2018-03-17 07:07:32 +0000 |
commit | 1a9638f5d5fc78f7e8be700e71b506fed3cc9d2d (patch) | |
tree | a5c38fbddd08beeead83c943ab2c744d448944f8 /osdf/adapters | |
parent | 7c70d5ba1469b9ea3220bc61be1d1973e5e3e98a (diff) |
New policies and required code changes
Issue-ID: OPTFRA-146
Change-Id: I2261ae69f52b184cd7dcb7b86d5905538666a411
Signed-off-by: Sastry Isukapalli <sastry@research.att.com>
Diffstat (limited to 'osdf/adapters')
-rw-r--r-- | osdf/adapters/local_data/local_policies.py | 11 | ||||
-rw-r--r-- | osdf/adapters/policy/interface.py | 20 | ||||
-rw-r--r-- | osdf/adapters/policy/utils.py | 8 | ||||
-rw-r--r-- | osdf/adapters/sdc/constraint_handler.py | 81 | ||||
-rwxr-xr-x | osdf/adapters/sdc/sdc.py | 40 |
5 files changed, 25 insertions, 135 deletions
diff --git a/osdf/adapters/local_data/local_policies.py b/osdf/adapters/local_data/local_policies.py index c63ae5a..e722fe2 100644 --- a/osdf/adapters/local_data/local_policies.py +++ b/osdf/adapters/local_data/local_policies.py @@ -18,6 +18,7 @@ import json import os +import re def get_local_policies(local_policy_folder, local_policy_list, policy_id_list=None): @@ -38,3 +39,13 @@ def get_local_policies(local_policy_folder, local_policy_list, policy_id_list=No with open(os.path.join(local_policy_folder, fname)) as fid: policies.append(json.load(fid)) return policies + + +def get_policy_names_from_file(fname_for_list_of_files): + """Get policy names; take care of comments, empty lines, etc""" + with open(fname_for_list_of_files) as fid: + return [ + re.sub(r'#.*$', '', x).strip() # remove inline comments and strip spaces + for x in fid + if not re.search(r'^#|^$', x.strip()) # remove blank or comments-only lines + ] diff --git a/osdf/adapters/policy/interface.py b/osdf/adapters/policy/interface.py index 4ddee15..288571f 100644 --- a/osdf/adapters/policy/interface.py +++ b/osdf/adapters/policy/interface.py @@ -110,9 +110,7 @@ def get_by_scope(rest_client, req, config_local, type_service): model_name = retrieve_node(req, pscope['service_name']) service_name = model_name - # service_name = data_mapping.get_request_service_type(req) - # if service_name is None: - # service_name = data_mapping.get_service_type(model_name) + scope = pscope['scope_{}'.format(service_name.lower())] subscriber_role, prov_status = get_subscriber_role(rest_client, req, pmain, service_name, scope) policy_type_list = pmain['policy_type_{}'.format(service_name.lower())] @@ -134,9 +132,7 @@ def remote_api(req_json, osdf_config, service_type="placement"): :param service_type: the type of service to call: "placement", "scheduling" :return: all related policies and provStatus retrieved from Subscriber policy """ -# if not req_json[service_type + "Info"]['policyId']: -# return [] - + prov_status = None config = osdf_config.deployment uid, passwd = config['policyPlatformUsername'], config['policyPlatformPassword'] pcuid, pcpasswd = config['policyClientUsername'], config['policyClientPassword'] @@ -168,16 +164,18 @@ def local_policies_location(req_json, osdf_config, service_type): :param service_type: placement supported for now, but can be any other service :return: a tuple (folder, file_list) or None """ - lp = osdf_config.core.get('osdf_hacks', {}).get('local_policies', {}) + lp = osdf_config.core.get('osdf_temp', {}).get('local_policies', {}) if lp.get('global_disabled'): return None # short-circuit to disable all local policies if lp.get('local_{}_policies_enabled'.format(service_type)): if service_type == "scheduling": return lp.get('{}_policy_dir'.format(service_type)), lp.get('{}_policy_files'.format(service_type)) else: - model_name = retrieve_node(req_json, osdf_config.core['policy_info'][service_type]['policy_scope']['service_name']) - service_name = data_mapping.get_service_type(model_name) - return lp.get('{}_policy_dir_{}'.format(service_type, service_name.lower())), lp.get('{}_policy_files_{}'.format(service_type, service_name.lower())) + required_node = osdf_config.core['policy_info'][service_type]['policy_scope']['service_name'] + model_name = retrieve_node(req_json, required_node) + service_name = model_name # TODO: data_mapping.get_service_type(model_name) + return lp.get('{}_policy_dir_{}'.format(service_type, service_name.lower())), \ + lp.get('{}_policy_files_{}'.format(service_type, service_name.lower())) return None @@ -199,6 +197,6 @@ def get_policies(request_json, service_type): to_filter = request_json[service_type + "Info"]['policyId'] policies = get_local_policies(local_info[0], local_info[1], to_filter) else: - policies, prov_status= remote_api(request_json, osdf_config, service_type) + policies, prov_status = remote_api(request_json, osdf_config, service_type) return policies, prov_status diff --git a/osdf/adapters/policy/utils.py b/osdf/adapters/policy/utils.py index a006f12..27885a5 100644 --- a/osdf/adapters/policy/utils.py +++ b/osdf/adapters/policy/utils.py @@ -31,10 +31,12 @@ def group_policies(flat_policies): filter_policies = defaultdict(list) policy_name = [] for policy in flat_policies: - policy_type = policy['content']['type'] + policy_type = policy['content'].get('policyType') + if not policy_type: + continue if policy_type not in aggregated_policies: aggregated_policies[policy_type] = defaultdict(list) - for resource in policy['content']['policyScope']['resourceInstanceType']: + for resource in policy['content'].get('resourceInstanceType', []): aggregated_policies[policy_type][resource].append(policy) for policy_type in aggregated_policies: for resource in aggregated_policies[policy_type]: @@ -42,7 +44,7 @@ def group_policies(flat_policies): aggregated_policies[policy_type][resource].sort(key=lambda x: x['priority'], reverse=True) policy = aggregated_policies[policy_type][resource][0] if policy['policyName'] not in policy_name: - filter_policies[policy['content']['type']].append(policy) + filter_policies[policy['content']['policyType']].append(policy) policy_name.append(policy['policyName']) return filter_policies diff --git a/osdf/adapters/sdc/constraint_handler.py b/osdf/adapters/sdc/constraint_handler.py deleted file mode 100644 index 2aae9a0..0000000 --- a/osdf/adapters/sdc/constraint_handler.py +++ /dev/null @@ -1,81 +0,0 @@ -# -------------------------------------------------------------------------
-# Copyright (c) 2015-2017 AT&T Intellectual Property
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-# -------------------------------------------------------------------------
-#
-
-from osdf.config.base import osdf_config
-from osdf.utils.programming_utils import dot_notation
-
-ns = {'p': 'http://xmlns.onap.org/sdc/license-model/1.0'}
-config_local = osdf_config.core
-
-
-def choose_license(license_artifacts, order_info, service_type):
- entitlement_pool_uuids = []
- license_key_group_uuids = []
-
- for license_artifact in license_artifacts:
- for feature in license_artifact.findall('./p:feature-group-list/', ns):
- for entitlement in feature.findall('./p:entitlement-pool-list/', ns):
- if is_valid(entitlement, order_info, service_type):
- entitlement_pool_uuid = entitlement.find('p:entitlement-pool-uuid', ns).text
- entitlement_pool_uuids.append(entitlement_pool_uuid)
- for license_key_group in feature.findall('./p:license-key-group-list/', ns):
- if is_valid(license_key_group, order_info, service_type):
- license_key_group_uuid = license_key_group.find('p:license-key-group-uuid', ns).text
- license_key_group_uuids.append(license_key_group_uuid)
- return entitlement_pool_uuids, license_key_group_uuids
-
-
-# element is expected to be a license-key-group or entitlement-pool
-# if these elements diverge at a later date this method should be refactored
-def is_valid(element, order_info, service_type):
- for limit in element.findall('./p:sp-limits/p:limit', ns):
- # description = limit.find('p:description', ns).text
- metric_value = limit.find('p:values', ns).text
- metric = limit.find('p:metric', ns).text
- try:
- order_value = dot_notation(order_info, config_local['service_info'][service_type][metric])
- # print("The order has the value %s for the metric %s and the limit specifies the value %s. The limit has the description %s." % (order_value, metric, metric_value, description))
- if isinstance(order_value, list): # it is possible a list is returned, for example a list of vnfs for vCPE
- for arr_value in order_value:
- if str(metric_value) != str(arr_value):
- return False
- else:
- if str(metric_value) != str(order_value):
- return False
- except KeyError:
- return False
- # vendor limits
- for limit in element.findall('./p:vendor-limits/p:limit', ns):
- # description = limit.find('p:description', ns).text
- metric_value = limit.find('p:values', ns).text
- metric = limit.find('p:metric', ns).text
- try:
- order_value = dot_notation(order_info, config_local['service_info'][service_type][metric])
- if isinstance(order_value, list): # it is possible a list is returned, for example a list of vnfs for vCPE
- for arr_value in order_value:
- if str(metric_value) != str(arr_value):
- return False
- else:
- if str(metric_value) != str(order_value):
- return False
- # print("The order has the value %s for the metric %s and the limit specifies the value %s. The limit has the description %s." % (order_value, metric, metric_value, description))
-
- except KeyError:
- return False
- return True
-
diff --git a/osdf/adapters/sdc/sdc.py b/osdf/adapters/sdc/sdc.py deleted file mode 100755 index 43932ba..0000000 --- a/osdf/adapters/sdc/sdc.py +++ /dev/null @@ -1,40 +0,0 @@ -# ------------------------------------------------------------------------- -# Copyright (c) 2015-2017 AT&T Intellectual Property -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -# ------------------------------------------------------------------------- -# - -from osdf.utils.interfaces import RestClient -import xml.etree.ElementTree as ET - -def request(model_version_id, request_id, config): - """Get all of the license artifacts from SDC using service_resource_id and model_version_id - :param model_version_id: model_version_id - :param request_id: request_id - :return: license artifacts from SDC - """ - base_url = config['sdcUrl'] - uid, passwd = config['sdcUsername'], config['sdcPassword'] - headers = {"CSP_UID": config['sdcMechId'], "X-ONAP-InstanceID": "osdf"} - rc = RestClient(userid=uid, passwd=passwd, headers=headers, method="GET", req_id=request_id) - resource_data = rc.request(base_url + '/resources/{}/metadata'.format(model_version_id)) - - artifact_ids = [x['artifactURL'].split("/resources/")[-1] # get the part after /resources/ - for x in resource_data.get('artifacts', []) if x.get('artifactType') == "VF_LICENSE"] - artifact_urls = [base_url + '/resources/' + str(artifact_id) for artifact_id in artifact_ids] - licenses = [] - for x in artifact_urls: - licenses.append(ET.fromstring(rc.request(x, asjson=False))) - return licenses |