7878f5d23e81f149a593d22ebd41606af9aa0790
[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
19 from jinja2 import Template
20
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
25
26
27 def process_nxi_termination_opt(request_json, osdf_config):
28     """Process the nxi Termination request from API layer
29
30            :param request_json: api request
31            :param osdf_config: configuration specific to OSDF app
32            :return: response as a success,failure
33     """
34
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"]
39
40     inputs = {
41         "instance_id": request_json["NxIId"]
42     }
43
44     try:
45         if request_type == "NSI":
46             query_type = "nsi"
47             if addtnl_args and "serviceProfileId" in addtnl_args:
48                 inputs["profile_id"] = addtnl_args["serviceProfileId"]
49                 query_type = "nsi_with_profile"
50         else:
51             query_type = "nssi"
52             if addtnl_args and "serviceInstanceId" in addtnl_args:
53                 inputs["nsi_id"] = addtnl_args["serviceInstanceId"]
54                 query_type = "nssi_with_nsi"
55
56         debug_log.debug("query type: {}".format(query_type))
57
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
62
63         return set_response("success", "", request_info, resource_count <= 0)
64     except AAIException as e:
65         reason = str(e)
66         return set_response("failure", reason, request_info)
67
68     except Exception as e:
69         reason = "{} Exception Occurred while processing".format(str(e))
70         return set_response("failure", reason, request_info)
71
72
73 def set_response(status, reason, request_info, terminate_response=None):
74     res = dict()
75     res["requestStatus"] = status
76     res["terminateResponse"] = terminate_response
77     res["reason"] = reason
78     return get_nxi_termination_response(request_info, res)
79
80
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