1 # -------------------------------------------------------------------------
2 # Copyright (C) 2020 Wipro Limited.
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
16 # -------------------------------------------------------------------------
21 from requests.auth import HTTPBasicAuth
22 from requests import RequestException
24 from osdf.logging.osdf_logging import debug_log
27 "X-TransactionId": "9999",
29 "Accept": "application/json",
30 "Content-Type": "application/json",
33 AUTH = HTTPBasicAuth("AAI", "AAI")
36 class AAIException(Exception):
40 def get_aai_data(request_json, osdf_config):
42 """Get the response from AAI
44 :param request_json: requestjson
45 :param osdf_config: configuration specific to OSDF app
46 :return:response body from AAI
49 nxi_id = request_json["NxIId"]
50 config = osdf_config.deployment
51 aai_url = config["aaiUrl"]
52 aai_req_url = aai_url + config["aaiServiceInstanceUrl"] + nxi_id + "?depth=2"
55 debug_log.debug("aai request: {}".format(aai_req_url))
56 response = requests.get(aai_req_url, headers=AAI_HEADERS, auth=AUTH, verify=False)
57 debug_log.debug("aai response: {}".format(response.json()))
58 except RequestException as e:
59 raise AAIException("Request exception was encountered {}".format(e))
61 if response.status_code == 200:
62 return response.json()
64 raise AAIException("Error response recieved from AAI for the request {}".format(aai_req_url))
67 def execute_dsl_query(query, format, osdf_config):
68 """Get the response from AAI
70 :param query: dsl query to be executed
71 :param format format of the output
72 :param osdf_config: configuration specific to OSDF app
73 :return:response body from AAI
75 config = osdf_config.deployment
76 dsl_url = config["aaiUrl"] + config["dslQueryPath"] + format
78 data = json.dumps({'dsl': query})
79 debug_log.debug("aai dsl request: {}".format(data))
81 response = requests.put(dsl_url, data=data, headers=AAI_HEADERS, auth=AUTH, verify=False)
82 debug_log.debug("aai dsl response: {}".format(response))
83 except RequestException as ex:
84 raise AAIException("Request exception was encountered {}".format(ex))
86 if response.status_code == 200:
87 return response.json()
89 raise AAIException("Response code other than 200 from AAI: {} {}".format(response.status_code,