From cbb46e426b5e88d34a6a38b2132b222bd3b896a4 Mon Sep 17 00:00:00 2001 From: judyjoseph <53951155+judyjoseph@users.noreply.github.com> Date: Sat, 22 Aug 2020 16:24:44 -0700 Subject: [PATCH] Add common functions applicable to single/multi asic platforms (#5224) * Add common functions applicable to single/multi asic platforms * Raise exception if invalid namespace is given as input. --- .../sonic_py_common/multi_asic.py | 38 ++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/src/sonic-py-common/sonic_py_common/multi_asic.py b/src/sonic-py-common/sonic_py_common/multi_asic.py index 2083e3935b..78bc6d72ba 100644 --- a/src/sonic-py-common/sonic_py_common/multi_asic.py +++ b/src/sonic-py-common/sonic_py_common/multi_asic.py @@ -123,11 +123,17 @@ def is_multi_asic(): def get_asic_id_from_name(asic_name): + """ + Get the asic id from the asic name for multi-asic platforms + In single ASIC platforms, it would fail and throw an exception. + Returns: + asic id. + """ if asic_name.startswith(ASIC_NAME_PREFIX): return asic_name[len(ASIC_NAME_PREFIX):] else: - return None + raise ValueError('Unknown asic namespace name {}'.format(asic_name)) def get_namespaces_from_linux(): @@ -308,3 +314,33 @@ def is_bgp_session_internal(bgp_neigh_ip, namespace=None): else: return False return False + +def get_front_end_namespaces(): + """ + Get the namespaces in the platform. For multi-asic devices we get the namespaces + mapped to asic which have front-panel interfaces. For single ASIC device it is the + DEFAULT_NAMESPACE which maps to the linux host. + + Returns: + a list of namespaces + """ + namespaces = [DEFAULT_NAMESPACE] + if is_multi_asic(): + ns_list = get_all_namespaces() + namespaces = ns_list['front_ns'] + + return namespaces + + +def get_asic_index_from_namespace(namespace): + """ + Get asic index from the namespace name. + With single ASIC platform, return asic_index 0, which is mapped to the only asic present. + + Returns: + asic_index as an integer. + """ + if is_multi_asic(): + return int(get_asic_id_from_name(namespace)) + + return 0