170d5e5eb80d4ce2a17c46188e27c2eb1a66ceda
[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         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))
60
61     if response.status_code == 200:
62         return response.json()
63     else:
64         raise AAIException("Error response recieved from AAI for the request {}".format(aai_req_url))
65
66
67 def execute_dsl_query(query, format, osdf_config):
68     """Get the response from AAI
69
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
74     """
75     config = osdf_config.deployment
76     dsl_url = config["aaiUrl"] + config["dslQueryPath"] + format
77
78     data = json.dumps({'dsl': query})
79     debug_log.debug("aai dsl request: {}".format(data))
80     try:
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))
85
86     if response.status_code == 200:
87         return response.json()
88     else:
89         raise AAIException("Response code other than 200 from AAI: {} {}".format(response.status_code,
90                                                                                  response.content))