8867c9757b602e825ac5566a5091a1dd827fe6fb
[optf/osdf.git] / apps / nxi_termination / optimizers / remote_opt_processor.py
1 # -------------------------------------------------------------------------
2 #   Copyright (C) 2020 Wipro Limited.
3 #
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
7 #
8 #       http://www.apache.org/licenses/LICENSE-2.0
9 #
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.
15 #
16 # -------------------------------------------------------------------------
17 #
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
22
23
24 def process_nxi_termination_opt(request_json, osdf_config):
25
26     """Process the nxi Termination request from API layer
27
28        :param request_json: api request
29        :param osdf_config: configuration specific to OSDF app
30        :return: response as a success,failure
31     """
32
33     type = request_json["type"]
34     request_info = request_json.get("requestInfo", {})
35     addtnl_args = request_info.get("addtnlArgs", {})
36     if type == "NSI":
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)
40
41     else:
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)
44
45
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", {})
48
49     try:
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:
54                 reason = ''
55                 return set_success_response(reason, request_info, terminate_response=True)
56
57             else:
58                 reason = "{} is not available in AAI".format(arg_val)
59                 return set_success_response(reason, request_info, terminate_response=False)
60
61         elif len(response_object) == 0:
62             reason = ''
63             return set_success_response(reason, request_info, terminate_response=True)
64
65         else:
66             reason = "Associated to more than one"
67             return set_success_response(reason, request_info, terminate_response=False)
68
69     except AAIException as e:
70         reason = str(e)
71         return set_failure_response(reason, request_info)
72
73     except Exception as e:
74         reason = "{} Exception Occurred while processing".format(str(e))
75         return set_failure_response(reason, request_info)
76
77
78 def get_service_profiles(request_json, osdf_config):
79     try:
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:
85         raise AAIException(e)
86
87
88 def get_relationshiplist(request_json, osdf_config):
89     try:
90         json_response = get_aai_data(request_json, osdf_config)
91         rel_list = json_response.get("relationship-list", {})
92         relationship = rel_list.get("relationship", [])
93         return relationship
94     except AAIException as e:
95         raise AAIException(e)
96
97
98 def get_service_profile_id(service_profile):
99     profile_obj = service_profile[0]
100     return profile_obj.get("profile-id", "")
101
102
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"]
109
110
111 def set_success_response(reason, request_info, terminate_response):
112     res = dict()
113     res["requestStatus"] = "success"
114     res["terminateResponse"] = terminate_response
115     res["reason"] = reason
116     return get_nxi_termination_response(request_info, res)
117
118
119 def set_failure_response(reason, request_info,):
120     res = dict()
121     res["requestStatus"] = "failure"
122     res["reason"] = reason
123     return get_nxi_termination_failure_response(request_info, res)