1 # -------------------------------------------------------------------------
2 # Copyright (C) 2020 Wipro Limited.
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
16 # -------------------------------------------------------------------------
19 from jinja2 import Template
21 from apps.nxi_termination.optimizers.response_processor import get_nxi_termination_response
22 from osdf.adapters.aai.fetch_aai_data import AAIException
23 from osdf.adapters.aai.fetch_aai_data import execute_dsl_query
24 from osdf.logging.osdf_logging import debug_log
27 def process_nxi_termination_opt(request_json, osdf_config):
28 """Process the nxi Termination request from API layer
30 :param request_json: api request
31 :param osdf_config: configuration specific to OSDF app
32 :return: response as a success,failure
35 request_type = request_json["type"]
36 request_info = request_json.get("requestInfo", {})
37 addtnl_args = request_info.get("addtnlArgs", {})
38 query_templates = osdf_config.core["nxi_termination"]["query_templates"]
41 "instance_id": request_json["NxIId"]
45 if request_type == "NSI":
47 if addtnl_args and "serviceProfileId" in addtnl_args:
48 inputs["profile_id"] = addtnl_args["serviceProfileId"]
49 query_type = "nsi_with_profile"
52 if addtnl_args and "serviceInstanceId" in addtnl_args:
53 inputs["nsi_id"] = addtnl_args["serviceInstanceId"]
54 query_type = "nssi_with_nsi"
56 debug_log.debug("query type: {}".format(query_type))
58 resource_count = get_resource_count(query_templates[query_type], inputs, osdf_config)
59 if query_type not in ["nsi", "nssi"]:
60 # if additional args is provided, it must have exactly one resource in its relationships
61 resource_count = resource_count - 1
63 return set_response("success", "", request_info, resource_count <= 0)
64 except AAIException as e:
66 return set_response("failure", reason, request_info)
68 except Exception as e:
69 reason = "{} Exception Occurred while processing".format(str(e))
70 return set_response("failure", reason, request_info)
73 def set_response(status, reason, request_info, terminate_response=None):
75 res["requestStatus"] = status
76 res["terminateResponse"] = terminate_response
77 res["reason"] = reason
78 return get_nxi_termination_response(request_info, res)
81 def get_resource_count(query_template, inputs, osdf_config):
82 query = Template(query_template).render(inputs)
83 dsl_response = execute_dsl_query(query, "count", osdf_config)
84 debug_log.debug("dsl_response {}".format(dsl_response))
85 # the dsl query with format "count" includes the original service-instance, hence reducing one from the result
86 return dsl_response["results"][0]["service-instance"] - 1