aboutsummaryrefslogtreecommitdiffstats
path: root/apps/route/optimizers/simple_route_opt.py
blob: b00180dcaaa2e3eb4c21ae3b4aeb86e4ec033df0 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# -------------------------------------------------------------------------
#   Copyright (c) 2018 Huawei Intellectual Property
#
#   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.
#
# -------------------------------------------------------------------------
#

import requests
from requests.auth import HTTPBasicAuth

from osdf.utils.mdc_utils import mdc_from_json


class RouteOpt:

    """
    This values will need to deleted.. 
    only added for the debug purpose 
    """
    # DNS server and standard port of AAI.. 
    # TODO: read the port from the configuration and add to DNS
    aai_host = "https://aai.api.simpledemo.onap.org:8443"
    aai_headers = {
        "X-TransactionId": "9999",
        "X-FromAppId": "OOF",
        "Accept": "application/json",
        "Content-Type": "application/json",
        "Real-Time": "true"
    }

    def isCrossONAPLink(self, logical_link):
        """
        This method checks if cross link is cross onap
        :param logical_link:
        :return:
        """
        for relationship in logical_link["relationship-list"]["relationship"]:
            if relationship["related-to"] == "ext-aai-network":
                return True
        return False

    def getRoute(self, request):
        """
        This method checks 
        :param logical_link:
        :return:
        """
        mdc_from_json(request)

        src_access_node_id = request["srcPort"]["src-access-node-id"]
        dst_access_node_id = request["dstPort"]["dst-access-node-id"]
        

        ingress_p_interface = None
        egress_p_interface = None

        # for the case of request_json for same domain, return the same node with destination update
        if src_access_node_id == dst_access_node_id:
            data = '{'\
                '"vpns":['\
                    '{'\
                        '"access-topology-id": "' + request["srcPort"]["src-access-topology-id"] + '",'\
                        '"access-client-id": "' + request["srcPort"]["src-access-client-id"] + '",'\
                        '"access-provider-id": "' + request["srcPort"]["src-access-provider-id"]+ '",'\
                        '"access-node-id": "' + request["srcPort"]["src-access-node-id"]+ '",'\
                        '"src-access-ltp-id": "' + request["srcPort"]["src-access-ltp-id"]+ '",'\
                        '"dst-access-ltp-id": "' + request["dstPort"]["dst-access-ltp-id"]  +'"'\
                    '}'\
                ']'\
            '}'
            return data
        else:
            logical_links = self.get_logical_links()

            # take the logical link where both the p-interface in same onap
            if logical_links != None:
                for logical_link in logical_links.get("logical-link"):
                    if not self.isCrossONAPLink(logical_link):
                        # link is in local ONAP
                        for relationship in logical_link["relationship-list"]["relationship"]:
                            if relationship["related-to"] == "p-interface":
                                if src_access_node_id in relationship["related-link"]:
                                    i_interface = relationship["related-link"].split("/")[-1]
                                    ingress_p_interface = i_interface.split("-")[-1]
                                if dst_access_node_id in relationship["related-link"]:
                                    e_interface = relationship["related-link"].split("/")[-1]
                                    egress_p_interface = e_interface.split("-")[-1]
                        data = '{'\
                                '"vpns":['\
                                        '{'\
                                        '"access-topology-id": "' + request["srcPort"]["src-access-topology-id"] + '",'\
                                        '"access-client-id": "' + request["srcPort"]["src-access-client-id"] + '",'\
                                        '"access-provider-id": "' + request["srcPort"]["src-access-provider-id"]+ '",'\
                                        '"access-node-id": "' + request["srcPort"]["src-access-node-id"]+ '",'\
                                        '"src-access-ltp-id": "' + request["srcPort"]["src-access-ltp-id"]+ '",'\
                                        '"dst-access-ltp-id": "' + ingress_p_interface +'"'\
                                '},'\
                                '{' \
                                        '"access-topology-id": "' + request["dstPort"]["dst-access-topology-id"] + '",' \
                                        '"access-topology-id": "' + request["dstPort"]["dst-access-topology-id"]+ '",' \
                                        '"access-provider-id": "' + request["dstPort"]["dst-access-provider-id"]+ '",' \
                                        '"access-node-id": "' + request["dstPort"]["dst-access-node-id"]+ '",' \
                                        '"src-access-ltp-id": "' + egress_p_interface + '",' \
                                        '"dst-access-ltp-id": "' + request["dstPort"]["dst-access-ltp-id"] + '"' \
                                '}'\
                            ']'\
                        '}'
                        return data


    def get_pinterface(self, url):
        """
        This method returns details for p interface
        :return: details of p interface
        """
        aai_req_url = self.aai_host + url
        response = requests.get(aai_req_url,
                                headers=self.aai_headers,
                                auth=HTTPBasicAuth("AAI", "AAI"),
                                verify=False)

        if response.status_code == 200:
            return response.json()


    def get_logical_links(self):
        """
        This method returns list of all cross ONAP links
        from /aai/v14/network/logical-links?operation-status="Up"
        :return: logical-links[]
        """
        logical_link_url = "/aai/v13/network/logical-links?operational-status=up"
        aai_req_url = self.aai_host + logical_link_url

        response = requests.get(aai_req_url,
                     headers=self.aai_headers,
                     auth=HTTPBasicAuth("AAI", "AAI"),
                     verify=False)

        logical_links =  None
        if response.status_code == 200:
            return response.json()