Skip to content

Commit ecca18f

Browse files
authored
[202012] Update load minigraph to load backend acl (#2235)
Signed-off-by: Neetha John <[email protected]> Backport #2236 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 c061a18 commit ecca18f

File tree

2 files changed

+83
-2
lines changed

2 files changed

+83
-2
lines changed

config/main.py

+40-2
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,40 @@ def validate_ipv4_address(ctx, param, ip_addr):
970970
except ValueError as e:
971971
raise click.UsageError(str(e))
972972

973+
def _is_storage_device(cfg_db):
974+
"""
975+
Check if the device is a storage device or not
976+
"""
977+
device_metadata = cfg_db.get_entry("DEVICE_METADATA", "localhost")
978+
return device_metadata.get("storage_device", "Unknown") == "true"
979+
980+
def _is_acl_table_present(cfg_db, acl_table_name):
981+
"""
982+
Check if acl table exists
983+
"""
984+
return acl_table_name in cfg_db.get_keys("ACL_TABLE")
985+
986+
def load_backend_acl(cfg_db, device_type):
987+
"""
988+
Load acl on backend storage device
989+
"""
990+
991+
BACKEND_ACL_TEMPLATE_FILE = os.path.join('/', "usr", "share", "sonic", "templates", "backend_acl.j2")
992+
BACKEND_ACL_FILE = os.path.join('/', "etc", "sonic", "backend_acl.json")
993+
994+
if device_type and device_type == "BackEndToRRouter" and _is_storage_device(cfg_db) and _is_acl_table_present(cfg_db, "DATAACL"):
995+
if os.path.isfile(BACKEND_ACL_TEMPLATE_FILE):
996+
clicommon.run_command(
997+
"{} -d -t {},{}".format(
998+
SONIC_CFGGEN_PATH,
999+
BACKEND_ACL_TEMPLATE_FILE,
1000+
BACKEND_ACL_FILE
1001+
),
1002+
display_cmd=True
1003+
)
1004+
if os.path.isfile(BACKEND_ACL_FILE):
1005+
clicommon.run_command("acl-loader update incremental {}".format(BACKEND_ACL_FILE), display_cmd=True)
1006+
9731007

9741008
# This is our main entrypoint - the main 'config' command
9751009
@click.group(cls=clicommon.AbbreviationGroup, context_settings=CONTEXT_SETTINGS)
@@ -1351,6 +1385,12 @@ def load_minigraph(db, no_service_restart):
13511385
if os.path.isfile('/etc/sonic/acl.json'):
13521386
clicommon.run_command("acl-loader update full /etc/sonic/acl.json", display_cmd=True)
13531387

1388+
# get the device type
1389+
device_type = _get_device_type()
1390+
1391+
# Load backend acl
1392+
load_backend_acl(db.cfgdb, device_type)
1393+
13541394
# Load port_config.json
13551395
try:
13561396
load_port_config(db.cfgdb, '/etc/sonic/port_config.json')
@@ -1360,8 +1400,6 @@ def load_minigraph(db, no_service_restart):
13601400
# generate QoS and Buffer configs
13611401
clicommon.run_command("config qos reload --no-dynamic-buffer", display_cmd=True)
13621402

1363-
# get the device type
1364-
device_type = _get_device_type()
13651403
if device_type != 'MgmtToRRouter' and device_type != 'MgmtTsToR' and device_type != 'BmcMgmtToRRouter' and device_type != 'EPMS':
13661404
clicommon.run_command("pfcwd start_default", display_cmd=True)
13671405

tests/config_test.py

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

144+
def test_load_backend_acl(self, get_cmd_module, setup_single_broadcom_asic):
145+
db = Db()
146+
db.cfgdb.set_entry("DEVICE_METADATA", "localhost", {"storage_device": "true"})
147+
self.check_backend_acl(get_cmd_module, db, device_type='BackEndToRRouter', condition=True)
148+
149+
def test_load_backend_acl_not_storage(self, get_cmd_module, setup_single_broadcom_asic):
150+
db = Db()
151+
self.check_backend_acl(get_cmd_module, db, device_type='BackEndToRRouter', condition=False)
152+
153+
def test_load_backend_acl_storage_leaf(self, get_cmd_module, setup_single_broadcom_asic):
154+
db = Db()
155+
db.cfgdb.set_entry("DEVICE_METADATA", "localhost", {"storage_device": "true"})
156+
self.check_backend_acl(get_cmd_module, db, device_type='BackEndLeafRouter', condition=False)
157+
158+
def test_load_backend_acl_storage_no_dataacl(self, get_cmd_module, setup_single_broadcom_asic):
159+
db = Db()
160+
db.cfgdb.set_entry("DEVICE_METADATA", "localhost", {"storage_device": "true"})
161+
db.cfgdb.set_entry("ACL_TABLE", "DATAACL", None)
162+
self.check_backend_acl(get_cmd_module, db, device_type='BackEndToRRouter', condition=False)
163+
164+
def check_backend_acl(self, get_cmd_module, db, device_type='BackEndToRRouter', condition=True):
165+
def is_file_side_effect(filename):
166+
return True if 'backend_acl' in filename else False
167+
with mock.patch('os.path.isfile', mock.MagicMock(side_effect=is_file_side_effect)):
168+
with mock.patch('config.main._get_device_type', mock.MagicMock(return_value=device_type)):
169+
with mock.patch(
170+
"utilities_common.cli.run_command",
171+
mock.MagicMock(side_effect=mock_run_command_side_effect)) as mock_run_command:
172+
(config, show) = get_cmd_module
173+
runner = CliRunner()
174+
result = runner.invoke(config.config.commands["load_minigraph"], ["-y"], obj=db)
175+
print(result.exit_code)
176+
expected_output = ['Running command: acl-loader update incremental /etc/sonic/backend_acl.json',
177+
'Running command: /usr/local/bin/sonic-cfggen -d -t /usr/share/sonic/templates/backend_acl.j2,/etc/sonic/backend_acl.json'
178+
]
179+
print(result.output)
180+
assert result.exit_code == 0
181+
output = result.output.split('\n')
182+
if condition:
183+
assert set(expected_output).issubset(set(output))
184+
else:
185+
assert not(set(expected_output).issubset(set(output)))
186+
144187
def check_port_config(self, db, config, port_config, expected_output):
145188
def read_json_file_side_effect(filename):
146189
return port_config

0 commit comments

Comments
 (0)