8f003c2ea0083678703ce8c75b4cb6669097d547
[optf/osdf.git] / osdf / optimizers / pciopt / configdb.py
1 # -------------------------------------------------------------------------
2 #   Copyright (c) 2018 AT&T 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 from datetime import datetime as dt
20
21 from osdf.logging.osdf_logging import debug_log
22 from osdf.utils.interfaces import RestClient
23
24
25 def request(req_object, osdf_config, flat_policies):
26     """
27     Process a configdb request from a Client (build Conductor API call, make the call, return result)
28     :param req_object: Request parameters from the client
29     :param osdf_config: Configuration specific to OSDF application (core + deployment)
30     :param flat_policies: policies related to PCI Opt (fetched based on request)
31     :return: response from ConfigDB (accounting for redirects from Conductor service
32     """
33     cell_list_response = {}
34     config = osdf_config.deployment
35     local_config = osdf_config.core
36     uid, passwd = config['configDbUserName'], config['configDbPassword']
37     req_id = req_object['requestInfo']['requestId']
38     transaction_id = req_object['requestInfo']['transactionId']
39     headers = dict(transaction_id=transaction_id)
40
41     network_id = req_object['cellInfo']['networkId']
42
43     cell_list_response['network_id'] = network_id
44
45     ts = dt.strftime(dt.now(), '%Y-%m-%d %H:%M:%S%z')
46
47     rc = RestClient(userid=uid, passwd=passwd, method="GET", log_func=debug_log.debug, headers=headers)
48
49     cell_list_url = '{}/{}/{}/{}'.format(config['configDbUrl'], config['configDbGetCellListUrl'], network_id, ts)
50
51     cell_list_resp = rc.request(raw_response=True, url=cell_list_url)
52     cell_resp = cell_list_resp.json()
53
54     cell_list = []
55     count = 0
56     for cell_id in cell_resp:
57         cell_info = {'cell_id': cell_id, 'id': count}
58         nbr_list_url = '{}/{}/{}/{}'.format(config['configDbUrl'], config['configDbGetNbrListUrl'], cell_id, ts)
59         nbr_list_raw = rc.request(url=nbr_list_url, raw_response=True)
60         cell_info['nbr_list'] = get_neighbor_list(nbr_list_raw.json())
61         cell_list.append(cell_info)
62         count += 1
63
64     cell_list_response['cell_list'] = cell_list
65     return cell_resp, cell_list_response
66
67
68 def get_neighbor_list(nbr_list_response):
69     return nbr_list_response.get('nbrList', [])