Skip to content

Commit f8c4b83

Browse files
judyjosephabdosi
authored andcommitted
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.
1 parent a518fe2 commit f8c4b83

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

src/sonic-py-common/sonic_py_common/multi_asic.py

+37-1
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,17 @@ def is_multi_asic():
123123

124124

125125
def get_asic_id_from_name(asic_name):
126+
"""
127+
Get the asic id from the asic name for multi-asic platforms
128+
In single ASIC platforms, it would fail and throw an exception.
126129
130+
Returns:
131+
asic id.
132+
"""
127133
if asic_name.startswith(ASIC_NAME_PREFIX):
128134
return asic_name[len(ASIC_NAME_PREFIX):]
129135
else:
130-
return None
136+
raise ValueError('Unknown asic namespace name {}'.format(asic_name))
131137

132138
# Get the ASIC id from the asic.conf file.
133139
def get_asic_device_id(asic_id):
@@ -336,3 +342,33 @@ def is_bgp_session_internal(bgp_neigh_ip, namespace=None):
336342
else:
337343
return False
338344
return False
345+
346+
def get_front_end_namespaces():
347+
"""
348+
Get the namespaces in the platform. For multi-asic devices we get the namespaces
349+
mapped to asic which have front-panel interfaces. For single ASIC device it is the
350+
DEFAULT_NAMESPACE which maps to the linux host.
351+
352+
Returns:
353+
a list of namespaces
354+
"""
355+
namespaces = [DEFAULT_NAMESPACE]
356+
if is_multi_asic():
357+
ns_list = get_all_namespaces()
358+
namespaces = ns_list['front_ns']
359+
360+
return namespaces
361+
362+
363+
def get_asic_index_from_namespace(namespace):
364+
"""
365+
Get asic index from the namespace name.
366+
With single ASIC platform, return asic_index 0, which is mapped to the only asic present.
367+
368+
Returns:
369+
asic_index as an integer.
370+
"""
371+
if is_multi_asic():
372+
return int(get_asic_id_from_name(namespace))
373+
374+
return 0

0 commit comments

Comments
 (0)