From 679d8df36f93af397dbe03fb6ddb2b9f24eef898 Mon Sep 17 00:00:00 2001 From: vdahiya12 <67608553+vdahiya12@users.noreply.github.com> Date: Fri, 4 Sep 2020 10:19:12 -0700 Subject: [PATCH] [sonic-utilities][sonic-py-common] Move logic to get port config file path to sonic-py-common and update sonic-utilities to comply (#5264) * [sonic-utilities]update submodule with fix This PR addresses fixes in sonic-py-common to imitate the behavior inside sonic-cfggen. Essentially this is a fix for accessing the port-config file. First check if there is a platform.json file for config generation and then for legacy port_config.ini. Also updating the sub-module sonic-utilities. Fix pfcwd stats crash with invalid queue name (#1077) [show][bgp]Display the Total number of neighbors in the show ip bgp(v6) summary. (#1079) [config] Update SONiC Environment Vars When Loading Minigraph (#1073) Multi asic platform changes for interface, portchannel commands (#878) Update Command-Reference.md (#1075) [filter-fdb] Fix Filter FDB With IPv6 Present in Config DB (#1059) [config] Remove _get_breakout_cfg_file_name helper function (#1069) [SHOW][BGP] support show ip(v6) bgp summary for multi asic platform (#1064) [fanshow] Display other fan status, such as Updating (#1014) Add ip_prefix len based on proxy_arp status (#1046) Enable the platform specific ssd firmware upgrade during reboot (#954) [show][cli[show interface portchannel support for Multi ASIC (#1005) support show interface commands for multi ASIC platforms (#1006) Signed-off-by: vaibhav-dahiya --- src/sonic-config-engine/portconfig.py | 32 +----- src/sonic-config-engine/sonic-cfggen | 14 +-- .../sonic_py_common/device_info.py | 100 ++++++++++++++---- src/sonic-utilities | 2 +- 4 files changed, 91 insertions(+), 57 deletions(-) diff --git a/src/sonic-config-engine/portconfig.py b/src/sonic-config-engine/portconfig.py index 714ddd37f5f9..df1decece763 100644 --- a/src/sonic-config-engine/portconfig.py +++ b/src/sonic-config-engine/portconfig.py @@ -7,6 +7,7 @@ import re from collections import OrderedDict from swsssdk import ConfigDBConnector + from sonic_py_common import device_info except ImportError as e: raise ImportError("%s - required module not found" % str(e)) @@ -64,33 +65,6 @@ def db_connect_configdb(): config_db = None return config_db -def get_port_config_file_name(hwsku=None, platform=None, asic=None): - - # check 'platform.json' file presence - port_config_candidates_Json = [] - port_config_candidates_Json.append(os.path.join(PLATFORM_ROOT_PATH_DOCKER, PLATFORM_JSON)) - if platform: - port_config_candidates_Json.append(os.path.join(PLATFORM_ROOT_PATH, platform, PLATFORM_JSON)) - - # check 'portconfig.ini' file presence - port_config_candidates = [] - port_config_candidates.append(os.path.join(HWSKU_ROOT_PATH, PORT_CONFIG_INI)) - if hwsku: - if platform: - if asic: - port_config_candidates.append(os.path.join(PLATFORM_ROOT_PATH, platform, hwsku, asic, PORT_CONFIG_INI)) - port_config_candidates.append(os.path.join(PLATFORM_ROOT_PATH, platform, hwsku, PORT_CONFIG_INI)) - port_config_candidates.append(os.path.join(PLATFORM_ROOT_PATH_DOCKER, hwsku, PORT_CONFIG_INI)) - port_config_candidates.append(os.path.join(SONIC_ROOT_PATH, hwsku, PORT_CONFIG_INI)) - - elif platform and not hwsku: - port_config_candidates.append(os.path.join(PLATFORM_ROOT_PATH, platform, PORT_CONFIG_INI)) - - for candidate in port_config_candidates_Json + port_config_candidates: - if os.path.isfile(candidate): - return candidate - return None - def get_hwsku_file_name(hwsku=None, platform=None): hwsku_candidates_Json = [] hwsku_candidates_Json.append(os.path.join(HWSKU_ROOT_PATH, HWSKU_JSON)) @@ -119,7 +93,7 @@ def get_port_config(hwsku=None, platform=None, port_config_file=None, hwsku_conf return (ports, port_alias_map, port_alias_asic_map) if not port_config_file: - port_config_file = get_port_config_file_name(hwsku, platform, asic) + port_config_file = device_info.get_path_to_port_config_file(hwsku, asic) if not port_config_file: return ({}, {}, {}) @@ -289,7 +263,7 @@ def parse_platform_json_file(hwsku_json_file, platform_json_file): def get_breakout_mode(hwsku=None, platform=None, port_config_file=None): if not port_config_file: - port_config_file = get_port_config_file_name(hwsku, platform) + port_config_file = device_info.get_path_to_port_config_file(hwsku) if not port_config_file: return None if port_config_file.endswith('.json'): diff --git a/src/sonic-config-engine/sonic-cfggen b/src/sonic-config-engine/sonic-cfggen index 6897e383d8c6..3e4e661f6adc 100755 --- a/src/sonic-config-engine/sonic-cfggen +++ b/src/sonic-config-engine/sonic-cfggen @@ -41,9 +41,9 @@ from minigraph import minigraph_encoder from minigraph import parse_xml from minigraph import parse_device_desc_xml from minigraph import parse_asic_sub_role -from portconfig import get_port_config, get_port_config_file_name, get_breakout_mode -from sonic_py_common.device_info import get_platform, get_system_mac +from portconfig import get_port_config, get_breakout_mode from sonic_py_common.multi_asic import get_asic_id_from_name, is_multi_asic +from sonic_py_common import device_info from config_samples import generate_sample_config from config_samples import get_available_config from swsssdk import SonicV2Connector, ConfigDBConnector, SonicDBConfig, ConfigDBPipeConnector @@ -277,7 +277,7 @@ def main(): group.add_argument("-K", "--key", help="Lookup for a specific key") args = parser.parse_args() - platform = get_platform() + platform = device_info.get_platform() db_kwargs = {} if args.redis_unix_sock_file != None: @@ -301,7 +301,7 @@ def main(): }}} deep_update(data, hardware_data) if args.port_config is None: - args.port_config = get_port_config_file_name(hwsku, platform) + args.port_config = device_info.get_path_to_port_config_file(hwsku) (ports, _, _) = get_port_config(hwsku, platform, args.port_config, asic_id) if not ports: print('Failed to get port config', file=sys.stderr) @@ -356,11 +356,11 @@ def main(): asic_role = parse_asic_sub_role(args.minigraph, asic_name) if asic_role is not None and asic_role.lower() == "backend": - mac = get_system_mac(namespace=asic_name) + mac = device_info.get_system_mac(namespace=asic_name) else: - mac = get_system_mac() + mac = device_info.get_system_mac() else: - mac = get_system_mac() + mac = device_info.get_system_mac() hardware_data = {'DEVICE_METADATA': {'localhost': { 'platform': platform, diff --git a/src/sonic-py-common/sonic_py_common/device_info.py b/src/sonic-py-common/sonic_py_common/device_info.py index 6e30cb8b94a7..f6b25de55ab5 100644 --- a/src/sonic-py-common/sonic_py_common/device_info.py +++ b/src/sonic-py-common/sonic_py_common/device_info.py @@ -153,18 +153,15 @@ def get_asic_conf_file_path(): return None -def get_paths_to_platform_and_hwsku_dirs(): +def get_path_to_platform_dir(): """ - Retreives the paths to the device's platform and hardware SKU data - directories + Retreives the paths to the device's platform directory Returns: - A tuple of two strings, the first containing the path to the platform - directory of the device, the second containing the path to the hardware - SKU directory of the device + A string containing the path to the platform directory of the device """ - # Get platform and hwsku - (platform, hwsku) = get_platform_and_hwsku() + # Get platform + platform = get_platform() # Determine whether we're running in a container or on the host platform_path_host = os.path.join(HOST_DEVICE_PATH, platform) @@ -176,31 +173,94 @@ def get_paths_to_platform_and_hwsku_dirs(): else: raise OSError("Failed to locate platform directory") + return platform_path + +def get_path_to_hwsku_dir(): + """ + Retreives the path to the device's hardware SKU data directory + + Returns: + A string, containing the path to the hardware SKU directory of the device + """ + + # Get Platform path first + platform_path = get_path_to_platform_dir() + + # Get hwsku + hwsku = get_hwsku() + hwsku_path = os.path.join(platform_path, hwsku) - return (platform_path, hwsku_path) + return hwsku_path +def get_paths_to_platform_and_hwsku_dirs(): + """ + Retreives the paths to the device's platform and hardware SKU data + directories -def get_path_to_port_config_file(): + Returns: + A tuple of two strings, the first containing the path to the platform + directory of the device, the second containing the path to the hardware + SKU directory of the device + """ + + # Get Platform path first + platform_path = get_path_to_platform_dir() + + # Get hwsku + hwsku = get_hwsku() + + hwsku_path = os.path.join(platform_path, hwsku) + + return (platform_path, hwsku_path) + +def get_path_to_port_config_file(hwsku=None, asic=None): """ Retrieves the path to the device's port configuration file + Args: + hwsku: a string, it is allowed to be passed in args because when loading the + initial configuration on the device, the HwSKU is not yet present in ConfigDB. + asic: a string , asic argument should be passed on multi-ASIC devices only, + it should be omitted on single-ASIC platforms. + Returns: A string containing the path the the device's port configuration file """ - # Get platform and hwsku path - (platform_path, hwsku_path) = get_paths_to_platform_and_hwsku_dirs() - # First check for the presence of the new 'platform.json' file - port_config_file_path = os.path.join(platform_path, PLATFORM_JSON_FILE) - if not os.path.isfile(port_config_file_path): - # platform.json doesn't exist. Try loading the legacy 'port_config.ini' file - port_config_file_path = os.path.join(hwsku_path, PORT_CONFIG_FILE) - if not os.path.isfile(port_config_file_path): - raise OSError("Failed to detect port config file: {}".format(port_config_file_path)) + """ + This platform check is performed to make sure we return a None + in case of unit-tests within sonic-cfggen where platform is not expected to be + present because tests are not run on actual Hardware/Container. + TODO: refactor sonic-cfggen such that we can remove this check + """ + + platform = get_platform() + if not platform: + return None + + if hwsku: + platform_path = get_path_to_platform_dir() + hwsku_path = os.path.join(platform_path, hwsku) + else: + (platform_path, hwsku_path) = get_paths_to_platform_and_hwsku_dirs() - return port_config_file_path + port_config_candidates = [] + # Check for 'platform.json' file presence first + port_config_candidates.append(os.path.join(platform_path, PLATFORM_JSON_FILE)) + + # Check for 'port_config.ini' file presence in a few locations + if asic: + port_config_candidates.append(os.path.join(hwsku_path, asic, PORT_CONFIG_FILE)) + else: + port_config_candidates.append(os.path.join(hwsku_path, PORT_CONFIG_FILE)) + + for candidate in port_config_candidates: + if os.path.isfile(candidate): + return candidate + + return None def get_sonic_version_info(): if not os.path.isfile(SONIC_VERSION_YAML_PATH): diff --git a/src/sonic-utilities b/src/sonic-utilities index 17fb3781b2cb..58c29616f510 160000 --- a/src/sonic-utilities +++ b/src/sonic-utilities @@ -1 +1 @@ -Subproject commit 17fb3781b2cb7dfb845faa9f16bc17ccd0069649 +Subproject commit 58c29616f510ebd94122db4490523174e1256943