Skip to content

Commit

Permalink
Add common functions applicable to single/multi asic platforms (#5224)
Browse files Browse the repository at this point in the history
* Add common functions applicable to single/multi asic platforms
* Raise exception if invalid namespace is given as input.
  • Loading branch information
judyjoseph authored and abdosi committed Sep 4, 2020
1 parent a518fe2 commit f8c4b83
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion src/sonic-py-common/sonic_py_common/multi_asic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

# Get the ASIC id from the asic.conf file.
def get_asic_device_id(asic_id):
Expand Down Expand Up @@ -336,3 +342,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

0 comments on commit f8c4b83

Please sign in to comment.