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