summaryrefslogtreecommitdiffstats
path: root/apps/nxi_termination/optimizers
diff options
context:
space:
mode:
Diffstat (limited to 'apps/nxi_termination/optimizers')
-rw-r--r--apps/nxi_termination/optimizers/__init__.py0
-rw-r--r--apps/nxi_termination/optimizers/remote_opt_processor.py123
-rw-r--r--apps/nxi_termination/optimizers/response_processor.py45
3 files changed, 168 insertions, 0 deletions
diff --git a/apps/nxi_termination/optimizers/__init__.py b/apps/nxi_termination/optimizers/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/apps/nxi_termination/optimizers/__init__.py
diff --git a/apps/nxi_termination/optimizers/remote_opt_processor.py b/apps/nxi_termination/optimizers/remote_opt_processor.py
new file mode 100644
index 0000000..8867c97
--- /dev/null
+++ b/apps/nxi_termination/optimizers/remote_opt_processor.py
@@ -0,0 +1,123 @@
+# -------------------------------------------------------------------------
+# 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 apps.nxi_termination.optimizers.response_processor import get_nxi_termination_failure_response
+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
+
+
+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
+ """
+
+ 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)
+
+ 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", {})
+
+ 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)
+
+ elif len(response_object) == 0:
+ reason = ''
+ return set_success_response(reason, request_info, terminate_response=True)
+
+ else:
+ reason = "Associated to more than one"
+ return set_success_response(reason, request_info, terminate_response=False)
+
+ except AAIException as e:
+ reason = str(e)
+ return set_failure_response(reason, request_info)
+
+ except Exception as e:
+ reason = "{} Exception Occurred while processing".format(str(e))
+ return set_failure_response(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):
+ res = dict()
+ res["requestStatus"] = "success"
+ 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)
diff --git a/apps/nxi_termination/optimizers/response_processor.py b/apps/nxi_termination/optimizers/response_processor.py
new file mode 100644
index 0000000..e7f1041
--- /dev/null
+++ b/apps/nxi_termination/optimizers/response_processor.py
@@ -0,0 +1,45 @@
+# -------------------------------------------------------------------------
+# 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.
+#
+# -------------------------------------------------------------------------
+#
+
+def get_nxi_termination_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"],
+ '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']}