|
| 1 | +import json |
1 | 2 | import logging |
2 | 3 | import ptf.packet as scapy |
3 | 4 | import pytest |
@@ -41,14 +42,61 @@ class Dhcpv6PktRecvBase: |
41 | 42 | @pytest.fixture(scope="class") |
42 | 43 | def setup_teardown(self, rand_selected_dut, tbinfo): |
43 | 44 | duthost = rand_selected_dut |
| 45 | + |
| 46 | + # Remove DATAACL table to free TCAM resources |
| 47 | + TABLE_NAME = "DATAACL" |
| 48 | + data_acl_table = None |
| 49 | + data_acl_rules = None |
| 50 | + |
| 51 | + # Save both table and rules |
| 52 | + output = duthost.shell("sonic-cfggen -d --var-json \"ACL_TABLE\"")['stdout'] |
| 53 | + try: |
| 54 | + acl_tables = json.loads(output) |
| 55 | + if TABLE_NAME in acl_tables: |
| 56 | + data_acl_table = {TABLE_NAME: acl_tables[TABLE_NAME]} |
| 57 | + except ValueError: |
| 58 | + pass |
| 59 | + |
| 60 | + output = duthost.shell("sonic-cfggen -d --var-json \"ACL_RULE\"")['stdout'] |
| 61 | + try: |
| 62 | + acl_rules = json.loads(output) |
| 63 | + data_acl_rules = {k: v for k, v in acl_rules.items() if k.startswith(TABLE_NAME + "|")} |
| 64 | + except ValueError: |
| 65 | + pass |
| 66 | + |
| 67 | + if data_acl_table is not None: |
| 68 | + logging.info("Removing ACL table {} to free TCAM resources".format(TABLE_NAME)) |
| 69 | + duthost.shell(cmd="config acl remove table {}".format(TABLE_NAME)) |
| 70 | + |
44 | 71 | dut_index = str(tbinfo['duts_map'][duthost.hostname]) |
45 | 72 | disabled_host_interfaces = tbinfo['topo']['properties']['topology'].get('disabled_host_interfaces', []) |
46 | 73 | host_interfaces = [intf for intf in tbinfo['topo']['properties']['topology'].get('host_interfaces', []) |
47 | 74 | if intf not in disabled_host_interfaces] |
48 | 75 | ptf_indices = self.parse_ptf_indices(host_interfaces, dut_index) |
49 | 76 | dut_intf_ptf_index = duthost.get_extended_minigraph_facts(tbinfo)['minigraph_ptf_indices'] |
| 77 | + |
50 | 78 | yield ptf_indices, dut_intf_ptf_index |
51 | 79 |
|
| 80 | + # Restore DATAACL table and rules if they were removed |
| 81 | + if data_acl_table is not None: |
| 82 | + # Restore table |
| 83 | + data_acl = {'ACL_TABLE': data_acl_table} |
| 84 | + if data_acl_rules: |
| 85 | + data_acl['ACL_RULE'] = data_acl_rules |
| 86 | + cmd = 'sonic-cfggen -a \'{}\' -w'.format(json.dumps(data_acl)) |
| 87 | + logging.info("Restoring ACL table {} with {} rules".format(TABLE_NAME, |
| 88 | + len(data_acl_rules) if data_acl_rules |
| 89 | + else 0)) |
| 90 | + duthost.shell(cmd) |
| 91 | + # Verify restoration worked by checking for a specific rule |
| 92 | + if data_acl_rules: |
| 93 | + verify_cmd = "sonic-cfggen -d --var-json \"ACL_RULE\" | grep -q 'DATAACL'" |
| 94 | + result = duthost.shell(verify_cmd, module_ignore_errors=True) |
| 95 | + if result['rc'] == 0: |
| 96 | + logging.info("ACL rules successfully restored") |
| 97 | + else: |
| 98 | + logging.warning("ACL rule restoration may have failed") |
| 99 | + |
52 | 100 | def parse_ptf_indices(self, host_interfaces, dut_index): |
53 | 101 | indices = list() |
54 | 102 | for _ports in host_interfaces: |
|
0 commit comments