Loading src/device/service/drivers/ietf_l2vpn/TfsApiClient.py +58 −5 Original line number Diff line number Diff line Loading @@ -12,11 +12,12 @@ # See the License for the specific language governing permissions and # limitations under the License. import logging, requests import json, logging, requests from typing import Dict, List, Optional from common.tools.rest_api.client.RestApiClient import RestApiClient from device.service.driver_api.ImportTopologyEnum import ImportTopologyEnum GET_CONTEXT_IDS_URL = '/tfs-api/context_ids' GET_DEVICES_URL = '/tfs-api/devices' GET_LINKS_URL = '/tfs-api/links' Loading Loading @@ -52,8 +53,10 @@ MAPPING_DRIVER = { 'DEVICEDRIVER_RESTCONF_OPENCONFIG' : 21, } LOGGER = logging.getLogger(__name__) class TfsApiClient(RestApiClient): def __init__( self, address : str, port : int, scheme : str = 'http', Loading @@ -65,9 +68,26 @@ class TfsApiClient(RestApiClient): timeout=timeout, verify_certs=False, allow_redirects=True, logger=LOGGER ) def check_credentials(self) -> None: def check_credentials(self, raise_if_fail : bool = True) -> None: try: LOGGER.info('Checking credentials...') self.get(GET_CONTEXT_IDS_URL, expected_status_codes={requests.codes['OK']}) LOGGER.info('Credentials checked') return True except requests.exceptions.Timeout as e: MSG = 'Timeout connecting {:s}' msg = MSG.format(GET_CONTEXT_IDS_URL) LOGGER.exception(msg) if raise_if_fail: raise Exception(msg) from e return False except Exception as e: MSG = 'Exception connecting credentials: {:s}' msg = MSG.format(GET_CONTEXT_IDS_URL) LOGGER.exception(msg) if raise_if_fail: raise Exception(msg) from e return False def get_devices_endpoints( self, import_topology : ImportTopologyEnum = ImportTopologyEnum.DEVICES Loading @@ -88,6 +108,10 @@ class TfsApiClient(RestApiClient): device_type : str = json_device['device_type'] #if not device_type.startswith('emu-'): device_type = 'emu-' + device_type device_status = json_device['device_operational_status'] ctrl_id : Dict[str, Dict] = json_device.get('controller_id', dict()) ctrl_uuid : Optional[str] = ctrl_id.get('device_uuid', dict()).get('uuid') device_url = '/devices/device[{:s}]'.format(device_uuid) device_data = { 'uuid': json_device['device_id']['device_uuid']['uuid'], Loading @@ -99,17 +123,46 @@ class TfsApiClient(RestApiClient): for driver in json_device['device_drivers'] ], } if ctrl_uuid is not None and len(ctrl_uuid) > 0: device_data['ctrl_uuid'] = ctrl_uuid result.append((device_url, device_data)) config_rule_list : List[Dict] = ( json_device .get('device_config', dict()) .get('config_rules', list()) ) config_rule_dict : Dict[str, Dict] = dict() for cr in config_rule_list: if cr['action'] != 'CONFIGACTION_SET': continue if 'custom' not in cr: continue cr_rk : str = cr['custom']['resource_key'] if not cr_rk.startswith('/endpoints/endpoint['): continue settings = json.loads(cr['custom']['resource_value']) ep_uuid = settings.get('uuid') if ep_uuid is not None: config_rule_dict[ep_uuid] = settings ep_name = settings.get('name') if ep_name is not None: config_rule_dict[ep_name] = settings for json_endpoint in json_device['device_endpoints']: endpoint_uuid = json_endpoint['endpoint_id']['endpoint_uuid']['uuid'] endpoint_name = json_endpoint['name'] endpoint_url = '/endpoints/endpoint[{:s}]'.format(endpoint_uuid) endpoint_data = { 'device_uuid': device_uuid, 'uuid': endpoint_uuid, 'name': json_endpoint['name'], 'name': endpoint_name, 'type': json_endpoint['endpoint_type'], } endpoint_settings = config_rule_dict.get(endpoint_uuid) if endpoint_settings is not None: endpoint_data['settings'] = endpoint_settings else: endpoint_settings = config_rule_dict.get(endpoint_name) if endpoint_settings is not None: endpoint_data['settings'] = endpoint_settings result.append((endpoint_url, endpoint_data)) if import_topology == ImportTopologyEnum.DEVICES: Loading Loading
src/device/service/drivers/ietf_l2vpn/TfsApiClient.py +58 −5 Original line number Diff line number Diff line Loading @@ -12,11 +12,12 @@ # See the License for the specific language governing permissions and # limitations under the License. import logging, requests import json, logging, requests from typing import Dict, List, Optional from common.tools.rest_api.client.RestApiClient import RestApiClient from device.service.driver_api.ImportTopologyEnum import ImportTopologyEnum GET_CONTEXT_IDS_URL = '/tfs-api/context_ids' GET_DEVICES_URL = '/tfs-api/devices' GET_LINKS_URL = '/tfs-api/links' Loading Loading @@ -52,8 +53,10 @@ MAPPING_DRIVER = { 'DEVICEDRIVER_RESTCONF_OPENCONFIG' : 21, } LOGGER = logging.getLogger(__name__) class TfsApiClient(RestApiClient): def __init__( self, address : str, port : int, scheme : str = 'http', Loading @@ -65,9 +68,26 @@ class TfsApiClient(RestApiClient): timeout=timeout, verify_certs=False, allow_redirects=True, logger=LOGGER ) def check_credentials(self) -> None: def check_credentials(self, raise_if_fail : bool = True) -> None: try: LOGGER.info('Checking credentials...') self.get(GET_CONTEXT_IDS_URL, expected_status_codes={requests.codes['OK']}) LOGGER.info('Credentials checked') return True except requests.exceptions.Timeout as e: MSG = 'Timeout connecting {:s}' msg = MSG.format(GET_CONTEXT_IDS_URL) LOGGER.exception(msg) if raise_if_fail: raise Exception(msg) from e return False except Exception as e: MSG = 'Exception connecting credentials: {:s}' msg = MSG.format(GET_CONTEXT_IDS_URL) LOGGER.exception(msg) if raise_if_fail: raise Exception(msg) from e return False def get_devices_endpoints( self, import_topology : ImportTopologyEnum = ImportTopologyEnum.DEVICES Loading @@ -88,6 +108,10 @@ class TfsApiClient(RestApiClient): device_type : str = json_device['device_type'] #if not device_type.startswith('emu-'): device_type = 'emu-' + device_type device_status = json_device['device_operational_status'] ctrl_id : Dict[str, Dict] = json_device.get('controller_id', dict()) ctrl_uuid : Optional[str] = ctrl_id.get('device_uuid', dict()).get('uuid') device_url = '/devices/device[{:s}]'.format(device_uuid) device_data = { 'uuid': json_device['device_id']['device_uuid']['uuid'], Loading @@ -99,17 +123,46 @@ class TfsApiClient(RestApiClient): for driver in json_device['device_drivers'] ], } if ctrl_uuid is not None and len(ctrl_uuid) > 0: device_data['ctrl_uuid'] = ctrl_uuid result.append((device_url, device_data)) config_rule_list : List[Dict] = ( json_device .get('device_config', dict()) .get('config_rules', list()) ) config_rule_dict : Dict[str, Dict] = dict() for cr in config_rule_list: if cr['action'] != 'CONFIGACTION_SET': continue if 'custom' not in cr: continue cr_rk : str = cr['custom']['resource_key'] if not cr_rk.startswith('/endpoints/endpoint['): continue settings = json.loads(cr['custom']['resource_value']) ep_uuid = settings.get('uuid') if ep_uuid is not None: config_rule_dict[ep_uuid] = settings ep_name = settings.get('name') if ep_name is not None: config_rule_dict[ep_name] = settings for json_endpoint in json_device['device_endpoints']: endpoint_uuid = json_endpoint['endpoint_id']['endpoint_uuid']['uuid'] endpoint_name = json_endpoint['name'] endpoint_url = '/endpoints/endpoint[{:s}]'.format(endpoint_uuid) endpoint_data = { 'device_uuid': device_uuid, 'uuid': endpoint_uuid, 'name': json_endpoint['name'], 'name': endpoint_name, 'type': json_endpoint['endpoint_type'], } endpoint_settings = config_rule_dict.get(endpoint_uuid) if endpoint_settings is not None: endpoint_data['settings'] = endpoint_settings else: endpoint_settings = config_rule_dict.get(endpoint_name) if endpoint_settings is not None: endpoint_data['settings'] = endpoint_settings result.append((endpoint_url, endpoint_data)) if import_topology == ImportTopologyEnum.DEVICES: Loading