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 # -------------------------------------------------------------------------
18 from apps.nxi_termination.optimizers.response_processor import get_nxi_termination_failure_response
19 from apps.nxi_termination.optimizers.response_processor import get_nxi_termination_response
20 from osdf.adapters.aai.fetch_aai_data import AAIException
21 from osdf.adapters.aai.fetch_aai_data import get_aai_data
24 def process_nxi_termination_opt(request_json, osdf_config):
26 """Process the nxi Termination request from API layer
28 :param request_json: api request
29 :param osdf_config: configuration specific to OSDF app
30 :return: response as a success,failure
33 type = request_json["type"]
34 request_info = request_json.get("requestInfo", {})
35 addtnl_args = request_info.get("addtnlArgs", {})
37 arg_val = addtnl_args.get("serviceProfileId", "")
38 return check_nxi_termination(request_json, osdf_config, addtnl_args, arg_val,
39 get_service_profiles, get_service_profile_id)
42 arg_val = addtnl_args.get("serviceInstanceId", "")
43 return check_nxi_termination(request_json, osdf_config, addtnl_args, arg_val, get_relationshiplist, get_nsi_id)
46 def check_nxi_termination(request_json, osdf_config, addtnl_args, arg_val, get_response_object, get_response_id):
47 request_info = request_json.get("requestInfo", {})
50 response_object = get_response_object(request_json, osdf_config)
51 if addtnl_args and arg_val and len(response_object) == 1:
52 response_id = get_response_id(response_object)
53 if arg_val == response_id:
55 return set_success_response(reason, request_info, terminate_response=True)
58 reason = "{} is not available in AAI".format(arg_val)
59 return set_success_response(reason, request_info, terminate_response=False)
61 elif len(response_object) == 0:
63 return set_success_response(reason, request_info, terminate_response=True)
66 reason = "Associated to more than one"
67 return set_success_response(reason, request_info, terminate_response=False)
69 except AAIException as e:
71 return set_failure_response(reason, request_info)
73 except Exception as e:
74 reason = "{} Exception Occurred while processing".format(str(e))
75 return set_failure_response(reason, request_info)
78 def get_service_profiles(request_json, osdf_config):
80 json_response = get_aai_data(request_json, osdf_config)
81 service_profiles = json_response.get("service-profiles", {})
82 service_profile = service_profiles.get("service-profile", [])
83 return service_profile
84 except AAIException as e:
88 def get_relationshiplist(request_json, osdf_config):
90 json_response = get_aai_data(request_json, osdf_config)
91 rel_list = json_response.get("relationship-list", {})
92 relationship = rel_list.get("relationship", [])
94 except AAIException as e:
98 def get_service_profile_id(service_profile):
99 profile_obj = service_profile[0]
100 return profile_obj.get("profile-id", "")
103 def get_nsi_id(relationship):
104 rel_obj = relationship[0]
105 rel_data = rel_obj.get("relationship-data", [])
106 for data in rel_data:
107 if data["relationship-key"] == "service-instance.service-instance-id":
108 return data["relationship-value"]
111 def set_success_response(reason, request_info, terminate_response):
113 res["requestStatus"] = "success"
114 res["terminateResponse"] = terminate_response
115 res["reason"] = reason
116 return get_nxi_termination_response(request_info, res)
119 def set_failure_response(reason, request_info,):
121 res["requestStatus"] = "failure"
122 res["reason"] = reason
123 return get_nxi_termination_failure_response(request_info, res)