# ------------------------------------------------------------------------- # Copyright (C) 2020 Wipro Limited. # # 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 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.logging.osdf_logging import debug_log def process_nxi_termination_opt(request_json, osdf_config): """Process the nxi Termination request from API layer :param request_json: api request :param osdf_config: configuration specific to OSDF app :return: response as a success,failure """ request_type = request_json["type"] request_info = request_json.get("requestInfo", {}) addtnl_args = request_info.get("addtnlArgs", {}) query_templates = osdf_config.core["nxi_termination"]["query_templates"] inputs = { "instance_id": request_json["NxIId"] } try: 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) except AAIException as e: reason = str(e) return set_response("failure", reason, request_info) except Exception as e: reason = "{} Exception Occurred while processing".format(str(e)) return set_response("failure", reason, request_info) def set_response(status, reason, request_info, terminate_response=None): res = dict() res["requestStatus"] = status res["terminateResponse"] = terminate_response res["reason"] = reason return get_nxi_termination_response(request_info, res) def get_resource_count(query_template, inputs, osdf_config): query = Template(query_template).render(inputs) 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