Skip to content

Commit 1518ca9

Browse files
authored
Update load minigraph to load backend acl (sonic-net#2236)
Signed-off-by: Neetha John <[email protected]> What I did Load backend acl template as part of the load minigraph if the device type is a 'BackEndToRRouter' and the device is a storage device How to verify it Added unit tests to verify if the backend acl load commands are applied
1 parent c7389bd commit 1518ca9

File tree

2 files changed

+84
-2
lines changed

2 files changed

+84
-2
lines changed

config/main.py

+41-2
Original file line numberDiff line numberDiff line change
@@ -1129,6 +1129,41 @@ def validate_gre_type(ctx, _, value):
11291129
except ValueError:
11301130
raise click.UsageError("{} is not a valid GRE type".format(value))
11311131

1132+
def _is_storage_device(cfg_db):
1133+
"""
1134+
Check if the device is a storage device or not
1135+
"""
1136+
device_metadata = cfg_db.get_entry("DEVICE_METADATA", "localhost")
1137+
return device_metadata.get("storage_device", "Unknown") == "true"
1138+
1139+
def _is_acl_table_present(cfg_db, acl_table_name):
1140+
"""
1141+
Check if acl table exists
1142+
"""
1143+
return acl_table_name in cfg_db.get_keys("ACL_TABLE")
1144+
1145+
def load_backend_acl(cfg_db, device_type):
1146+
"""
1147+
Load acl on backend storage device
1148+
"""
1149+
1150+
BACKEND_ACL_TEMPLATE_FILE = os.path.join('/', "usr", "share", "sonic", "templates", "backend_acl.j2")
1151+
BACKEND_ACL_FILE = os.path.join('/', "etc", "sonic", "backend_acl.json")
1152+
1153+
if device_type and device_type == "BackEndToRRouter" and _is_storage_device(cfg_db) and _is_acl_table_present(cfg_db, "DATAACL"):
1154+
if os.path.isfile(BACKEND_ACL_TEMPLATE_FILE):
1155+
clicommon.run_command(
1156+
"{} -d -t {},{}".format(
1157+
SONIC_CFGGEN_PATH,
1158+
BACKEND_ACL_TEMPLATE_FILE,
1159+
BACKEND_ACL_FILE
1160+
),
1161+
display_cmd=True
1162+
)
1163+
if os.path.isfile(BACKEND_ACL_FILE):
1164+
clicommon.run_command("acl-loader update incremental {}".format(BACKEND_ACL_FILE), display_cmd=True)
1165+
1166+
11321167
# This is our main entrypoint - the main 'config' command
11331168
@click.group(cls=clicommon.AbbreviationGroup, context_settings=CONTEXT_SETTINGS)
11341169
@click.pass_context
@@ -1693,6 +1728,12 @@ def load_minigraph(db, no_service_restart):
16931728
if os.path.isfile('/etc/sonic/acl.json'):
16941729
clicommon.run_command("acl-loader update full /etc/sonic/acl.json", display_cmd=True)
16951730

1731+
# get the device type
1732+
device_type = _get_device_type()
1733+
1734+
# Load backend acl
1735+
load_backend_acl(db.cfgdb, device_type)
1736+
16961737
# Load port_config.json
16971738
try:
16981739
load_port_config(db.cfgdb, '/etc/sonic/port_config.json')
@@ -1702,8 +1743,6 @@ def load_minigraph(db, no_service_restart):
17021743
# generate QoS and Buffer configs
17031744
clicommon.run_command("config qos reload --no-dynamic-buffer", display_cmd=True)
17041745

1705-
# get the device type
1706-
device_type = _get_device_type()
17071746
if device_type != 'MgmtToRRouter' and device_type != 'MgmtTsToR' and device_type != 'BmcMgmtToRRouter' and device_type != 'EPMS':
17081747
clicommon.run_command("pfcwd start_default", display_cmd=True)
17091748

tests/config_test.py

+43
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,49 @@ def test_load_minigraph_with_port_config(self, get_cmd_module, setup_single_broa
357357
port_config = [{"PORT": {"Ethernet0": {"admin_status": "up"}}}]
358358
self.check_port_config(db, config, port_config, "config interface startup Ethernet0")
359359

360+
def test_load_backend_acl(self, get_cmd_module, setup_single_broadcom_asic):
361+
db = Db()
362+
db.cfgdb.set_entry("DEVICE_METADATA", "localhost", {"storage_device": "true"})
363+
self.check_backend_acl(get_cmd_module, db, device_type='BackEndToRRouter', condition=True)
364+
365+
def test_load_backend_acl_not_storage(self, get_cmd_module, setup_single_broadcom_asic):
366+
db = Db()
367+
self.check_backend_acl(get_cmd_module, db, device_type='BackEndToRRouter', condition=False)
368+
369+
def test_load_backend_acl_storage_leaf(self, get_cmd_module, setup_single_broadcom_asic):
370+
db = Db()
371+
db.cfgdb.set_entry("DEVICE_METADATA", "localhost", {"storage_device": "true"})
372+
self.check_backend_acl(get_cmd_module, db, device_type='BackEndLeafRouter', condition=False)
373+
374+
def test_load_backend_acl_storage_no_dataacl(self, get_cmd_module, setup_single_broadcom_asic):
375+
db = Db()
376+
db.cfgdb.set_entry("DEVICE_METADATA", "localhost", {"storage_device": "true"})
377+
db.cfgdb.set_entry("ACL_TABLE", "DATAACL", None)
378+
self.check_backend_acl(get_cmd_module, db, device_type='BackEndToRRouter', condition=False)
379+
380+
def check_backend_acl(self, get_cmd_module, db, device_type='BackEndToRRouter', condition=True):
381+
def is_file_side_effect(filename):
382+
return True if 'backend_acl' in filename else False
383+
with mock.patch('os.path.isfile', mock.MagicMock(side_effect=is_file_side_effect)):
384+
with mock.patch('config.main._get_device_type', mock.MagicMock(return_value=device_type)):
385+
with mock.patch(
386+
"utilities_common.cli.run_command",
387+
mock.MagicMock(side_effect=mock_run_command_side_effect)) as mock_run_command:
388+
(config, show) = get_cmd_module
389+
runner = CliRunner()
390+
result = runner.invoke(config.config.commands["load_minigraph"], ["-y"], obj=db)
391+
print(result.exit_code)
392+
expected_output = ['Running command: acl-loader update incremental /etc/sonic/backend_acl.json',
393+
'Running command: /usr/local/bin/sonic-cfggen -d -t /usr/share/sonic/templates/backend_acl.j2,/etc/sonic/backend_acl.json'
394+
]
395+
print(result.output)
396+
assert result.exit_code == 0
397+
output = result.output.split('\n')
398+
if condition:
399+
assert set(expected_output).issubset(set(output))
400+
else:
401+
assert not(set(expected_output).issubset(set(output)))
402+
360403
def check_port_config(self, db, config, port_config, expected_output):
361404
def read_json_file_side_effect(filename):
362405
return port_config

0 commit comments

Comments
 (0)