Skip to content

Commit

Permalink
View acls and ports or specifc port configs on a sonic switch (in Jso…
Browse files Browse the repository at this point in the history
…n 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
  • Loading branch information
prprakas committed Aug 7, 2019
1 parent 2b28d55 commit f95233f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/sonic-config-engine/sonic-cfggen
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand All @@ -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")
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion src/sonic-config-engine/tests/test_cfggen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit f95233f

Please sign in to comment.