From f95233f63d7e1fe910973d3582a680a5166ef045 Mon Sep 17 00:00:00 2001 From: Prem Prakash Date: Tue, 18 Jun 2019 22:49:40 -0700 Subject: [PATCH] View acls and ports or specifc port configs on a sonic switch (in Json format) Using --var-json to display the acl and ports/specific port configs Editing the test(var_json_test) assertion due to change in output format of command Adding test for the interface argument added to Sonic config engine Test for the interface argument added --- src/sonic-config-engine/sonic-cfggen | 13 +++++++++++++ src/sonic-config-engine/tests/test_cfggen.py | 7 ++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/sonic-config-engine/sonic-cfggen b/src/sonic-config-engine/sonic-cfggen index b07172b17e75..12a1ed677f6a 100755 --- a/src/sonic-config-engine/sonic-cfggen +++ b/src/sonic-config-engine/sonic-cfggen @@ -169,6 +169,14 @@ def sort_data(data): data[table] = OrderedDict(natsorted(data[table].items())) return data +def get_config(data, keyword): + config = {} + for key, value in data.iteritems(): + if key == keyword: + config[keyword] = value + break + return config + def main(): parser=argparse.ArgumentParser(description="Render configuration file from minigraph data and jinja2 template.") @@ -183,6 +191,7 @@ def main(): parser.add_argument("-d", "--from-db", help="read config from configdb", action='store_true') parser.add_argument("-H", "--platform-info", help="read platform and hardware info", action='store_true') parser.add_argument("-s", "--redis-unix-sock-file", help="unix sock file for redis connection") + parser.add_argument("-i", "--interface", help="print specific port details") group = parser.add_mutually_exclusive_group() group.add_argument("-t", "--template", help="render the data with the template file") group.add_argument("-v", "--var", help="print the value of a variable, support jinja2 expression") @@ -276,6 +285,10 @@ def main(): print(json.dumps(FormatConverter.to_serialized(data[args.var_json], args.key), indent=4, cls=minigraph_encoder)) else: print(json.dumps(FormatConverter.to_serialized(data[args.var_json]), indent=4, cls=minigraph_encoder)) + config = get_config(data, args.var_json) + if args.var_json == "PORT" and args.interface: + config = get_config(config[args.var_json], args.interface) + print(json.dumps(FormatConverter.to_serialized(config), indent=4, cls=minigraph_encoder)) if args.write_to_db: configdb = ConfigDBConnector(**db_kwargs) diff --git a/src/sonic-config-engine/tests/test_cfggen.py b/src/sonic-config-engine/tests/test_cfggen.py index a28e1263c475..78ad7762d769 100644 --- a/src/sonic-config-engine/tests/test_cfggen.py +++ b/src/sonic-config-engine/tests/test_cfggen.py @@ -78,7 +78,12 @@ def test_additional_json_data_level2_key(self): def test_var_json_data(self): argument = '-m "' + self.sample_graph_simple + '" -p "' + self.port_config + '" --var-json VLAN_MEMBER' output = self.run_script(argument) - self.assertEqual(output.strip(), '{\n "Vlan1000|Ethernet8": {\n "tagging_mode": "untagged"\n }\n}') + self.assertEqual(output.strip(), '{\n "VLAN_MEMBER": {\n "Vlan1000|Ethernet8": {\n "tagging_mode": "untagged"\n }\n }\n}') + + def test_var_json_with_interface(self): + argument = '-m "' + self.sample_graph_simple + '" -p "' + self.port_config + '" --var-json PORT -i Ethernet124' + output = self.run_script(argument) + self.assertEqual(output.strip(), '{\n "Ethernet124": {\n "alias": "fortyGigE0/124", \n "description": "fortyGigE0/124", \n "lanes": "101,102,103,104", \n "mtu": "9100", \n "pfc_asym": "off"\n }\n}') def test_read_yaml(self): argument = '-v yml_item -y ' + os.path.join(self.test_dir, 'test.yml')