aboutsummaryrefslogtreecommitdiffstats
path: root/apps/nxi_termination/optimizers/remote_opt_processor.py
blob: 8867c9757b602e825ac5566a5091a1dd827fe6fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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)