X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=apps%2Fnxi_termination%2Foptimizers%2Fremote_opt_processor.py;fp=apps%2Fnxi_termination%2Foptimizers%2Fremote_opt_processor.py;h=fc3bc17fd30bcf66cb26e9cdf050fbfcd1650949;hb=e35d4a1b94bbcd844300282d1c3c8722ef58062d;hp=7878f5d23e81f149a593d22ebd41606af9aa0790;hpb=92373ddd2e28cc2000a2634d273836fea5d87a6e;p=optf%2Fosdf.git diff --git a/apps/nxi_termination/optimizers/remote_opt_processor.py b/apps/nxi_termination/optimizers/remote_opt_processor.py index 7878f5d..fc3bc17 100644 --- a/apps/nxi_termination/optimizers/remote_opt_processor.py +++ b/apps/nxi_termination/optimizers/remote_opt_processor.py @@ -21,6 +21,7 @@ from jinja2 import Template from apps.nxi_termination.optimizers.response_processor import get_nxi_termination_response from osdf.adapters.aai.fetch_aai_data import AAIException from osdf.adapters.aai.fetch_aai_data import execute_dsl_query +from osdf.adapters.aai.fetch_aai_data import get_aai_data from osdf.logging.osdf_logging import debug_log @@ -42,25 +43,38 @@ def process_nxi_termination_opt(request_json, osdf_config): } try: + if request_type == "NSSI": + templates = query_templates["nssi"] + for template in templates: + resource_count = get_resource_count(template, inputs, osdf_config) + if resource_count == -1: + continue + elif resource_count > 1 or (resource_count == 1 and not addtnl_args.get("serviceInstanceId")): + terminate_response = False + elif resource_count == 0: + terminate_response = True + elif resource_count == 1 and addtnl_args.get("serviceInstanceId"): + new_template = template + "('service-instance-id','{}')".format(addtnl_args["serviceInstanceId"]) + terminate_response = get_resource_count(new_template, inputs, osdf_config) == 1 + return set_response("success", "", request_info, terminate_response) + if request_type == "NSI": - query_type = "nsi" - if addtnl_args and "serviceProfileId" in addtnl_args: - inputs["profile_id"] = addtnl_args["serviceProfileId"] - query_type = "nsi_with_profile" - else: - query_type = "nssi" - if addtnl_args and "serviceInstanceId" in addtnl_args: - inputs["nsi_id"] = addtnl_args["serviceInstanceId"] - query_type = "nssi_with_nsi" - - debug_log.debug("query type: {}".format(query_type)) - - resource_count = get_resource_count(query_templates[query_type], inputs, osdf_config) - if query_type not in ["nsi", "nssi"]: - # if additional args is provided, it must have exactly one resource in its relationships - resource_count = resource_count - 1 - - return set_response("success", "", request_info, resource_count <= 0) + allotted_resources = get_allotted_resources(request_json, osdf_config) + resource_count = len(allotted_resources) + if resource_count == 1 and addtnl_args.get("serviceInstanceId"): + debug_log.debug("resource count {}".format(resource_count)) + terminate_response = False + properties = allotted_resources[0]["relationship-data"] + for property in properties: + if property["relationship-key"] == "service-instance.service-instance-id" \ + and property["relationship-value"] == addtnl_args.get("serviceInstanceId"): + terminate_response = True + elif resource_count > 1 or (resource_count == 1 and not addtnl_args.get("serviceInstanceId")): + terminate_response = False + elif resource_count == 0: + terminate_response = True + + return set_response("success", "", request_info, terminate_response) except AAIException as e: reason = str(e) return set_response("failure", reason, request_info) @@ -83,4 +97,11 @@ def get_resource_count(query_template, inputs, osdf_config): dsl_response = execute_dsl_query(query, "count", osdf_config) debug_log.debug("dsl_response {}".format(dsl_response)) # the dsl query with format "count" includes the original service-instance, hence reducing one from the result - return dsl_response["results"][0]["service-instance"] - 1 + count = dsl_response["results"][0] + return count.get("service-instance", 0) - 1 + + +def get_allotted_resources(request_json, osdf_config): + response = get_aai_data(request_json, osdf_config) + rel_list = response["relationship-list"]["relationship"] + return [rel for rel in rel_list if rel["related-to"] == "allotted-resource"]