Add cps client to PCI app
[optf/osdf.git] / apps / pci / optimizers / config / configdb.py
1 # -------------------------------------------------------------------------
2 #   Copyright (C) 2021 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 from datetime import datetime as dt
20 import uuid
21
22 from apps.pci.optimizers.config.config_client import ConfigClient
23 from osdf.config.base import osdf_config
24 from osdf.logging.osdf_logging import debug_log
25 from osdf.utils.interfaces import RestClient
26
27
28 @ConfigClient.register_subclass('configdb')
29 class ConfigDb(ConfigClient):
30
31     def __init__(self):
32         self.config = osdf_config.deployment
33         uid, passwd = self.config['configDbUserName'], self.config['configDbPassword']
34         headers = dict(transaction_id=str(uuid.uuid4()))
35         self.rc = RestClient(userid=uid, passwd=passwd, method="GET", log_func=debug_log.debug, headers=headers)
36
37     def get_cell_list(self, network_id):
38         ts = dt.strftime(dt.now(), '%Y-%m-%d %H:%M:%S%z')
39         cell_list_url = '{}/{}/{}/{}'.format(self.config['configDbUrl'],
40                                              self.config['configDbGetCellListUrl'], network_id, ts)
41         return self.rc.request(raw_response=True, url=cell_list_url).json()
42
43     def get_nbr_list(self, network_id, cell_id):
44         ts = dt.strftime(dt.now(), '%Y-%m-%d %H:%M:%S%z')
45         nbr_list_url = '{}/{}/{}/{}'.format(self.config['configDbUrl'],
46                                             self.config['configDbGetNbrListUrl'], cell_id, ts)
47         response = self.rc.request(url=nbr_list_url, raw_response=True).json()
48
49         debug_log.debug("cell_id {} nbr_list {}".format(cell_id, response.get('nbrList')))
50
51         return response.get('nbrList', [])