9560996a9a28c97f065ad964e7a0b9f67030d66d
[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         "Content-Type": "application/json",
36         "Real-Time": "true"
37     }
38
39     def isCrossONAPLink(self, logical_link):
40         """
41         This method checks if cross link is cross onap
42         :param logical_link:
43         :return:
44         """
45         for relationship in logical_link["logical-links"]["relationsihp-list"]["relationship"]:
46             if relationship["related-to"] == "p-interface":
47                 if "external" in relationship["related-link"]:
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         # for the case of request for same domain, return the same node with destination update
62         if src_access_node_id == dst_access_node_id:
63             return {
64                 [
65                     {
66                         "access-topology-id": request["srcPort"]["src-access-topology-id"],
67                         "access-client-id": request["srcPort"]["access-client-id"],
68                         "access-provider-id": request["srcPort"]["access-provider-id"],
69                         "access-node-id": request["srcPort"]["access-node-id"],
70                         "src-access-ltp-id": request["srcPort"]["src-access-ltp-id"],
71                         "dst-access-ltp-id": request["dstPort"]["dst-access-ltp-id"]
72                     }
73                 ]
74             } 
75
76         ingress_p_interface = None
77         egress_p_interface = None
78
79         logical_links = self.get_logical_links()
80
81         # take the logical link where both the p-interface in same onap
82         if logical_links != None:
83             for logical_link in logical_links["results"]:
84                 if not self.isCrossONAPLink(logical_link):
85
86                     # link is in local ONAP
87                     for relationship in logical_link["logical-links"]["relationsihp-list"]["relationship"]:
88                         if relationship["related-to"] == "p-interface":
89                             if src_access_node_id in relationship["related-link"]:
90                                 ingress_p_interface = relationship["related-link"].split("/")[-1]
91                             if dst_access_node_id in relationship["related-link"]:
92                                 egress_p_interface = relationship["related-link"].split("/")[-1]
93
94             return {
95                 [
96                     {
97                         "access-topology-id": request["srcPort"]["src-access-topology-id"],
98                         "access-client-id": request["srcPort"]["access-client-id"],
99                         "access-provider-id": request["srcPort"]["access-provider-id"],
100                         "access-node-id": request["srcPort"]["access-node-id"],
101                         "src-access-ltp-id": request["srcPort"]["src-access-ltp-id"],
102                         "dst-access-ltp-id": ingress_p_interface
103                     },
104                     {
105                         "access-topology-id": request["dstPort"]["access-topology-id"],
106                         "access-client-id": request["dstPort"]["access-client-id"],
107                         "access-provider-id": request["dstPort"]["access-provider-id"],
108                         "access-node-id": request["dstPort"]["access-node-id"],
109                         "src-access-ltp-id": egress_p_interface,
110                         "dst-access-ltp-id": request["dstPort"]["dst-access-ltp-id"]
111                     }
112                 ]
113             }
114
115
116
117     def get_pinterface(self, url):
118         """
119         This method returns details for p interface
120         :return: details of p interface
121         """
122         aai_req_url = self.aai_host + url
123         response = requests.get(aai_req_url,
124                                 headers=self.aai_headers,
125                                 auth=HTTPBasicAuth("", ""))
126
127         if response.status_code == 200:
128             return response.json
129
130
131     def get_logical_links(self):
132         """
133         This method returns list of all cross ONAP links
134         from /aai/v14/network/logical-links?operation-status="Up"
135         :return: logical-links[]
136         """
137         logical_link_url = "/aai/v14/network/logical-links?operation-status=\"Up\""
138         aai_req_url = self.aai_host + logical_link_url
139
140         response = requests.get(aai_req_url,
141                      headers=self.aai_headers,
142                      auth=HTTPBasicAuth("", ""))
143
144         if response.status_code == 200:
145             return response.json