Fix AAI request url for NxI Termination
[optf/osdf.git] / osdf / adapters / aai / fetch_aai_data.py
1 # -------------------------------------------------------------------------
2 #   Copyright (C) 2020 Wipro Limited.
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 from requests import RequestException
22
23
24 class AAIException(Exception):
25     pass
26
27
28 def get_aai_data(request_json, osdf_config):
29
30     """Get the response from AAI
31
32        :param request_json: requestjson
33        :param osdf_config: configuration specific to OSDF app
34        :return:response body from AAI
35     """
36     aai_headers = {
37         "X-TransactionId": "9999",
38         "X-FromAppId": "OOF",
39         "Accept": "application/json",
40         "Content-Type": "application/json",
41     }
42     nxi_id = request_json["NxIId"]
43     config = osdf_config.deployment
44     aai_url = config["aaiUrl"]
45     aai_req_url = aai_url + config["aaiServiceInstanceUrl"] + nxi_id + "?depth=2"
46
47     try:
48         response = requests.get(aai_req_url, headers=aai_headers, auth=HTTPBasicAuth("AAI", "AAI"), verify=False)
49     except RequestException as e:
50         raise AAIException("Request exception was encountered {}".format(e))
51
52     if response.status_code == 200:
53         return response.json()
54     else:
55         raise AAIException("Error response recieved from AAI for the request {}".format(aai_req_url))