b00180dcaaa2e3eb4c21ae3b4aeb86e4ec033df0
[optf/osdf.git] / apps / route / optimizers / simple_route_opt.py
1 # -------------------------------------------------------------------------
2 #   Copyright (c) 2018 Huawei Intellectual Property
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 import requests
20 from requests.auth import HTTPBasicAuth
21
22 from osdf.utils.mdc_utils import mdc_from_json
23
24
25 class RouteOpt:
26
27     """
28     This values will need to deleted.. 
29     only added for the debug purpose 
30     """
31     # DNS server and standard port of AAI.. 
32     # TODO: read the port from the configuration and add to DNS
33     aai_host = "https://aai.api.simpledemo.onap.org:8443"
34     aai_headers = {
35         "X-TransactionId": "9999",
36         "X-FromAppId": "OOF",
37         "Accept": "application/json",
38         "Content-Type": "application/json",
39         "Real-Time": "true"
40     }
41
42     def isCrossONAPLink(self, logical_link):
43         """
44         This method checks if cross link is cross onap
45         :param logical_link:
46         :return:
47         """
48         for relationship in logical_link["relationship-list"]["relationship"]:
49             if relationship["related-to"] == "ext-aai-network":
50                 return True
51         return False
52
53     def getRoute(self, request):
54         """
55         This method checks 
56         :param logical_link:
57         :return:
58         """
59         mdc_from_json(request)
60
61         src_access_node_id = request["srcPort"]["src-access-node-id"]
62         dst_access_node_id = request["dstPort"]["dst-access-node-id"]
63         
64
65         ingress_p_interface = None
66         egress_p_interface = None
67
68         # for the case of request_json for same domain, return the same node with destination update
69         if src_access_node_id == dst_access_node_id:
70             data = '{'\
71                 '"vpns":['\
72                     '{'\
73                         '"access-topology-id": "' + request["srcPort"]["src-access-topology-id"] + '",'\
74                         '"access-client-id": "' + request["srcPort"]["src-access-client-id"] + '",'\
75                         '"access-provider-id": "' + request["srcPort"]["src-access-provider-id"]+ '",'\
76                         '"access-node-id": "' + request["srcPort"]["src-access-node-id"]+ '",'\
77                         '"src-access-ltp-id": "' + request["srcPort"]["src-access-ltp-id"]+ '",'\
78                         '"dst-access-ltp-id": "' + request["dstPort"]["dst-access-ltp-id"]  +'"'\
79                     '}'\
80                 ']'\
81             '}'
82             return data
83         else:
84             logical_links = self.get_logical_links()
85
86             # take the logical link where both the p-interface in same onap
87             if logical_links != None:
88                 for logical_link in logical_links.get("logical-link"):
89                     if not self.isCrossONAPLink(logical_link):
90                         # link is in local ONAP
91                         for relationship in logical_link["relationship-list"]["relationship"]:
92                             if relationship["related-to"] == "p-interface":
93                                 if src_access_node_id in relationship["related-link"]:
94                                     i_interface = relationship["related-link"].split("/")[-1]
95                                     ingress_p_interface = i_interface.split("-")[-1]
96                                 if dst_access_node_id in relationship["related-link"]:
97                                     e_interface = relationship["related-link"].split("/")[-1]
98                                     egress_p_interface = e_interface.split("-")[-1]
99                         data = '{'\
100                                 '"vpns":['\
101                                         '{'\
102                                         '"access-topology-id": "' + request["srcPort"]["src-access-topology-id"] + '",'\
103                                         '"access-client-id": "' + request["srcPort"]["src-access-client-id"] + '",'\
104                                         '"access-provider-id": "' + request["srcPort"]["src-access-provider-id"]+ '",'\
105                                         '"access-node-id": "' + request["srcPort"]["src-access-node-id"]+ '",'\
106                                         '"src-access-ltp-id": "' + request["srcPort"]["src-access-ltp-id"]+ '",'\
107                                         '"dst-access-ltp-id": "' + ingress_p_interface +'"'\
108                                 '},'\
109                                 '{' \
110                                         '"access-topology-id": "' + request["dstPort"]["dst-access-topology-id"] + '",' \
111                                         '"access-topology-id": "' + request["dstPort"]["dst-access-topology-id"]+ '",' \
112                                         '"access-provider-id": "' + request["dstPort"]["dst-access-provider-id"]+ '",' \
113                                         '"access-node-id": "' + request["dstPort"]["dst-access-node-id"]+ '",' \
114                                         '"src-access-ltp-id": "' + egress_p_interface + '",' \
115                                         '"dst-access-ltp-id": "' + request["dstPort"]["dst-access-ltp-id"] + '"' \
116                                 '}'\
117                             ']'\
118                         '}'
119                         return data
120
121
122     def get_pinterface(self, url):
123         """
124         This method returns details for p interface
125         :return: details of p interface
126         """
127         aai_req_url = self.aai_host + url
128         response = requests.get(aai_req_url,
129                                 headers=self.aai_headers,
130                                 auth=HTTPBasicAuth("AAI", "AAI"),
131                                 verify=False)
132
133         if response.status_code == 200:
134             return response.json()
135
136
137     def get_logical_links(self):
138         """
139         This method returns list of all cross ONAP links
140         from /aai/v14/network/logical-links?operation-status="Up"
141         :return: logical-links[]
142         """
143         logical_link_url = "/aai/v13/network/logical-links?operational-status=up"
144         aai_req_url = self.aai_host + logical_link_url
145
146         response = requests.get(aai_req_url,
147                      headers=self.aai_headers,
148                      auth=HTTPBasicAuth("AAI", "AAI"),
149                      verify=False)
150
151         logical_links =  None
152         if response.status_code == 200:
153             return response.json()