summaryrefslogtreecommitdiffstats
path: root/apps/nxi_termination
diff options
context:
space:
mode:
authorkrishnaa96 <krishna.moorthy6@wipro.com>2021-04-08 14:14:48 +0530
committerkrishnaa96 <krishna.moorthy6@wipro.com>2021-04-08 14:14:48 +0530
commit7ef613de6d9cd794fd10ae43d98ffa9312418e68 (patch)
tree7fb698b06166d0227b6dc8c04347df4c5a11ed18 /apps/nxi_termination
parentaafb48a95fe5ace431ebe607cab660fddde8c2cc (diff)
Update AAI query to DSL query - Slice termination
Issue-ID: OPTFRA-936 Signed-off-by: krishnaa96 <krishna.moorthy6@wipro.com> Change-Id: I7a2bba7d23645c8bff9f503361a93fd3ac3ebc3a
Diffstat (limited to 'apps/nxi_termination')
-rw-r--r--apps/nxi_termination/optimizers/remote_opt_processor.py115
-rw-r--r--apps/nxi_termination/optimizers/response_processor.py15
2 files changed, 40 insertions, 90 deletions
diff --git a/apps/nxi_termination/optimizers/remote_opt_processor.py b/apps/nxi_termination/optimizers/remote_opt_processor.py
index 8867c97..7878f5d 100644
--- a/apps/nxi_termination/optimizers/remote_opt_processor.py
+++ b/apps/nxi_termination/optimizers/remote_opt_processor.py
@@ -15,109 +15,72 @@
#
# -------------------------------------------------------------------------
#
-from apps.nxi_termination.optimizers.response_processor import get_nxi_termination_failure_response
+
+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 get_aai_data
+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
+ :param request_json: api request
+ :param osdf_config: configuration specific to OSDF app
+ :return: response as a success,failure
"""
- type = request_json["type"]
+ request_type = request_json["type"]
request_info = request_json.get("requestInfo", {})
addtnl_args = request_info.get("addtnlArgs", {})
- if type == "NSI":
- arg_val = addtnl_args.get("serviceProfileId", "")
- return check_nxi_termination(request_json, osdf_config, addtnl_args, arg_val,
- get_service_profiles, get_service_profile_id)
+ query_templates = osdf_config.core["nxi_termination"]["query_templates"]
- else:
- arg_val = addtnl_args.get("serviceInstanceId", "")
- return check_nxi_termination(request_json, osdf_config, addtnl_args, arg_val, get_relationshiplist, get_nsi_id)
-
-
-def check_nxi_termination(request_json, osdf_config, addtnl_args, arg_val, get_response_object, get_response_id):
- request_info = request_json.get("requestInfo", {})
+ inputs = {
+ "instance_id": request_json["NxIId"]
+ }
try:
- response_object = get_response_object(request_json, osdf_config)
- if addtnl_args and arg_val and len(response_object) == 1:
- response_id = get_response_id(response_object)
- if arg_val == response_id:
- reason = ''
- return set_success_response(reason, request_info, terminate_response=True)
-
- else:
- reason = "{} is not available in AAI".format(arg_val)
- return set_success_response(reason, request_info, terminate_response=False)
+ 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"
- elif len(response_object) == 0:
- reason = ''
- return set_success_response(reason, request_info, terminate_response=True)
+ debug_log.debug("query type: {}".format(query_type))
- else:
- reason = "Associated to more than one"
- return set_success_response(reason, request_info, terminate_response=False)
+ 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_failure_response(reason, request_info)
+ return set_response("failure", reason, request_info)
except Exception as e:
reason = "{} Exception Occurred while processing".format(str(e))
- return set_failure_response(reason, request_info)
+ return set_response("failure", reason, request_info)
-def get_service_profiles(request_json, osdf_config):
- try:
- json_response = get_aai_data(request_json, osdf_config)
- service_profiles = json_response.get("service-profiles", {})
- service_profile = service_profiles.get("service-profile", [])
- return service_profile
- except AAIException as e:
- raise AAIException(e)
-
-
-def get_relationshiplist(request_json, osdf_config):
- try:
- json_response = get_aai_data(request_json, osdf_config)
- rel_list = json_response.get("relationship-list", {})
- relationship = rel_list.get("relationship", [])
- return relationship
- except AAIException as e:
- raise AAIException(e)
-
-
-def get_service_profile_id(service_profile):
- profile_obj = service_profile[0]
- return profile_obj.get("profile-id", "")
-
-
-def get_nsi_id(relationship):
- rel_obj = relationship[0]
- rel_data = rel_obj.get("relationship-data", [])
- for data in rel_data:
- if data["relationship-key"] == "service-instance.service-instance-id":
- return data["relationship-value"]
-
-
-def set_success_response(reason, request_info, terminate_response):
+def set_response(status, reason, request_info, terminate_response=None):
res = dict()
- res["requestStatus"] = "success"
+ res["requestStatus"] = status
res["terminateResponse"] = terminate_response
res["reason"] = reason
return get_nxi_termination_response(request_info, res)
-def set_failure_response(reason, request_info,):
- res = dict()
- res["requestStatus"] = "failure"
- res["reason"] = reason
- return get_nxi_termination_failure_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
diff --git a/apps/nxi_termination/optimizers/response_processor.py b/apps/nxi_termination/optimizers/response_processor.py
index e7f1041..3ea35c0 100644
--- a/apps/nxi_termination/optimizers/response_processor.py
+++ b/apps/nxi_termination/optimizers/response_processor.py
@@ -16,6 +16,7 @@
# -------------------------------------------------------------------------
#
+
def get_nxi_termination_response(request_info, response):
"""Get NXI termination response from final solution
@@ -29,17 +30,3 @@ def get_nxi_termination_response(request_info, response):
'requestStatus': response["requestStatus"],
'terminateResponse': response["terminateResponse"],
'reason': response['reason']}
-
-
-def get_nxi_termination_failure_response(request_info, response):
-
- """Get NXI termination response from final solution
-
- :param request_info: request info
- :param response: response to be send
- :return: NxI Termination response to send back as dictionary
- """
- return {'requestId': request_info['requestId'],
- 'transactionId': request_info['transactionId'],
- 'requestStatus': response["requestStatus"],
- 'reason': response['reason']}