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 16 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
90 changes: 78 additions & 12 deletions src/sonic-py-common/sonic_py_common/device_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,52 @@ def get_asic_conf_file_path():
return None


def get_path_to_platform_dir():
"""
Retreives the paths to the device's platform directory

Returns:
A string containing the path to the platform directory of the device
"""
# 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)

if os.path.isdir(CONTAINER_PLATFORM_PATH):
platform_path = CONTAINER_PLATFORM_PATH
elif os.path.isdir(platform_path_host):
platform_path = platform_path_host
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 and hwsku
(platform, hwsku) = get_platform_and_hwsku()

# Determine whether we're running in a container or on the host
platform_path_host = os.path.join(HOST_DEVICE_PATH, platform)
jleveque marked this conversation as resolved.
Show resolved Hide resolved

if os.path.isdir(CONTAINER_PLATFORM_PATH):
platform_path = CONTAINER_PLATFORM_PATH
elif os.path.isdir(platform_path_host):
platform_path = platform_path_host
else:
raise OSError("Failed to locate platform directory")

hwsku_path = os.path.join(platform_path, hwsku)

return hwsku_path
jleveque marked this conversation as resolved.
Show resolved Hide resolved

def get_paths_to_platform_and_hwsku_dirs():
"""
Retreives the paths to the device's platform and hardware SKU data
Expand Down Expand Up @@ -181,27 +227,47 @@ def get_paths_to_platform_and_hwsku_dirs():

return (platform_path, hwsku_path)


def get_path_to_port_config_file():
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

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()

port_config_candidates = []

# Check for 'platform.json' file presence first
port_config_candidates.append(os.path.join(platform_path, PLATFORM_JSON_FILE))

return port_config_file_path
# 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