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

config_facts enhancements for multi-asic and support in SonicAsic #2435

Merged
merged 1 commit into from
Nov 2, 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
27 changes: 17 additions & 10 deletions ansible/library/config_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
- Set to "running" for running config, or "persistent" for persistent config from /etc/sonic/config_db.json
'''

PERSISTENT_CONFIG_PATH = "/etc/sonic/config_db.json"
PERSISTENT_CONFIG_PATH = "/etc/sonic/config_db{}.json"
TABLE_NAME_SEPARATOR = '|'

def format_config(json_data):
Expand Down Expand Up @@ -63,18 +63,21 @@ def format_config(json_data):

def create_maps(config):
""" Create a map of SONiC port name to physical port index """
port_index_map = {}
port_name_to_alias_map = {}
port_alias_to_name_map = {}

port_name_list = config["PORT"].keys()
port_name_list_sorted = natsorted(port_name_list)
if 'PORT' in config:
port_name_list = config["PORT"].keys()
port_name_list_sorted = natsorted(port_name_list)

port_index_map = {}
for idx, val in enumerate(port_name_list_sorted):
port_index_map[val] = idx
for idx, val in enumerate(port_name_list_sorted):
port_index_map[val] = idx

port_name_to_alias_map = { name : v['alias'] for name, v in config["PORT"].iteritems()}
port_name_to_alias_map = { name : v['alias'] for name, v in config["PORT"].iteritems()}

# Create inverse mapping between port name and alias
port_alias_to_name_map = {v: k for k, v in port_name_to_alias_map.iteritems()}
# Create inverse mapping between port name and alias
port_alias_to_name_map = {v: k for k, v in port_name_to_alias_map.iteritems()}

return {
'port_name_to_alias_map' : port_name_to_alias_map,
Expand Down Expand Up @@ -126,7 +129,11 @@ def main():
if 'filename' in m_args and m_args['filename'] is not None:
cfg_file_path = "%s" % m_args['filename']
else:
cfg_file_path = PERSISTENT_CONFIG_PATH
if namespace is not None:
asic_index = namespace.split("asic")[1]
cfg_file_path = PERSISTENT_CONFIG_PATH.format(asic_index)
else:
cfg_file_path = PERSISTENT_CONFIG_PATH.format("")
with open(cfg_file_path, "r") as f:
config = json.load(f)
elif m_args["source"] == "running":
Expand Down
18 changes: 18 additions & 0 deletions tests/common/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -1334,6 +1334,24 @@ def bgp_facts(self, *module_args, **complex_args):
complex_args['instance_id'] = self.asic_index
return self.sonichost.bgp_facts(*module_args, **complex_args)

def config_facts(self, *module_args, **complex_args):
""" Wrapper method for config_facts ansible module.
If number of asics in SonicHost are more than 1, then add 'namespace' param for this Asic
If 'host' is not specified in complex_args, add it - as it is a mandatory param for the config_facts module

Args:
module_args: other ansible module args passed from the caller
complex_args: other ansible keyword args

Returns:
if SonicHost has only 1 asic, then return the config_facts for the global namespace, else config_facts for namespace for my asic_index.
"""
if 'host' not in complex_args:
complex_args['host'] = self.sonichost.hostname
if self.sonichost.facts['num_asic'] != 1:
complex_args['namespace'] = 'asic{}'.format(self.asic_index)
return self.sonichost.config_facts(*module_args, **complex_args)


class MultiAsicSonicHost(object):
""" This class represents a Multi-asic SonicHost It has two attributes:
Expand Down