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

[Sonic-config-engine]: View acls and ports or specifc port configs on sonic switch (Json) #3036

Closed
wants to merge 1 commit into from
Closed
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
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")
prprakas marked this conversation as resolved.
Show resolved Hide resolved
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}')
lguohan marked this conversation as resolved.
Show resolved Hide resolved

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