Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[sonic-utilities][sonic-py-common] Move logic to get port config file path to sonic-py-common and update sonic-utilities to comply #5264

Merged
merged 18 commits into from
Sep 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 3 additions & 29 deletions src/sonic-config-engine/portconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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 ({}, {}, {})

Expand Down Expand Up @@ -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)
jleveque marked this conversation as resolved.
Show resolved Hide resolved
if not port_config_file:
return None
if port_config_file.endswith('.json'):
Expand Down
14 changes: 7 additions & 7 deletions src/sonic-config-engine/sonic-cfggen
Original file line number Diff line number Diff line change
Expand Up @@ -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
jleveque marked this conversation as resolved.
Show resolved Hide resolved
from config_samples import generate_sample_config
from config_samples import get_available_config
from swsssdk import SonicV2Connector, ConfigDBConnector, SonicDBConfig, ConfigDBPipeConnector
Expand Down Expand Up @@ -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:
Expand All @@ -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)
jleveque marked this conversation as resolved.
Show resolved Hide resolved
(ports, _, _) = get_port_config(hwsku, platform, args.port_config, asic_id)
if not ports:
print('Failed to get port config', file=sys.stderr)
Expand Down Expand Up @@ -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,
Expand Down
100 changes: 80 additions & 20 deletions src/sonic-py-common/sonic_py_common/device_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,18 +154,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)
Expand All @@ -177,31 +174,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
jleveque marked this conversation as resolved.
Show resolved Hide resolved

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):
Expand Down
2 changes: 1 addition & 1 deletion src/sonic-utilities