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