fad2512087e96bb94e6a12e853e0bed8179f9899
[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 json
20 import requests
21 from requests.auth import HTTPBasicAuth
22 from requests import RequestException
23
24 from osdf.logging.osdf_logging import debug_log
25
26 AAI_HEADERS = {
27     "X-TransactionId": "9999",
28     "X-FromAppId": "OOF",
29     "Accept": "application/json",
30     "Content-Type": "application/json",
31 }
32
33 AUTH = HTTPBasicAuth("AAI", "AAI")
34
35
36 class AAIException(Exception):
37     pass
38
39
40 def get_aai_data(request_json, osdf_config):
41
42     """Get the response from AAI
43
44        :param request_json: requestjson
45        :param osdf_config: configuration specific to OSDF app
46        :return:response body from AAI
47     """
48
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"
53
54     try:
55         response = requests.get(aai_req_url, headers=AAI_HEADERS, auth=AUTH, verify=False)
56     except RequestException as e:
57         raise AAIException("Request exception was encountered {}".format(e))
58
59     if response.status_code == 200:
60         return response.json()
61     else:
62         raise AAIException("Error response recieved from AAI for the request {}".format(aai_req_url))
63
64
65 def execute_dsl_query(query, format, osdf_config):
66     """Get the response from AAI
67
68            :param query: dsl query to be executed
69            :param format format of the output
70            :param osdf_config: configuration specific to OSDF app
71            :return:response body from AAI
72     """
73     config = osdf_config.deployment
74     dsl_url = config["aaiUrl"] + config["dslQueryPath"] + format
75
76     data = json.dumps({'dsl': query})
77     debug_log.debug("aai dsl request: {}".format(data))
78     try:
79         response = requests.put(dsl_url, data=data, headers=AAI_HEADERS, auth=AUTH, verify=False)
80     except RequestException as ex:
81         raise AAIException("Request exception was encountered {}".format(ex))
82
83     if response.status_code == 200:
84         return response.json()
85     else:
86         raise AAIException("Response code other than 200 from AAI: {} {}".format(response.status_code,
87                                                                                  response.content))