|
| 1 | +from dump.helper import create_template_dict |
| 2 | +from dump.match_infra import MatchRequest |
| 3 | +from swsscommon.swsscommon import SonicDBConfig |
| 4 | + |
| 5 | +from dump.match_helper import fetch_acl_counter_oid |
| 6 | +from .executor import Executor |
| 7 | + |
| 8 | + |
| 9 | +CFG_DB_SEPARATOR = SonicDBConfig.getSeparator("CONFIG_DB") |
| 10 | +ASIC_DB_SEPARATOR = SonicDBConfig.getSeparator("ASIC_DB") |
| 11 | + |
| 12 | + |
| 13 | +class Acl_Table(Executor): |
| 14 | + """ |
| 15 | + Debug Dump Plugin for ACL Table Module |
| 16 | + """ |
| 17 | + ARG_NAME = "acl_table_name" |
| 18 | + |
| 19 | + def __init__(self, match_engine=None): |
| 20 | + super().__init__(match_engine) |
| 21 | + |
| 22 | + def get_all_args(self, ns=""): |
| 23 | + req = MatchRequest(db="CONFIG_DB", table="ACL_TABLE", key_pattern="*", ns=ns) |
| 24 | + ret = self.match_engine.fetch(req) |
| 25 | + acl_tables = ret["keys"] |
| 26 | + return [key.split(CFG_DB_SEPARATOR)[-1] for key in acl_tables] |
| 27 | + |
| 28 | + def execute(self, params): |
| 29 | + self.ret_temp = create_template_dict(dbs=["CONFIG_DB", "ASIC_DB"]) |
| 30 | + acl_table_name = params[self.ARG_NAME] |
| 31 | + self.ns = params["namespace"] |
| 32 | + self.init_acl_table_config_info(acl_table_name) |
| 33 | + self.init_acl_table_asic_info(acl_table_name) |
| 34 | + return self.ret_temp |
| 35 | + |
| 36 | + def init_acl_table_config_info(self, acl_table_name): |
| 37 | + req = MatchRequest(db="CONFIG_DB", table="ACL_TABLE", key_pattern=acl_table_name, return_fields=["type"], ns=self.ns) |
| 38 | + ret = self.match_engine.fetch(req) |
| 39 | + self.add_to_ret_template(req.table, req.db, ret["keys"], ret["error"]) |
| 40 | + |
| 41 | + # Find corresponding ACL table type in CONFIG DB |
| 42 | + return_values = ret["return_values"] |
| 43 | + acl_table_type_name = return_values.get(CFG_DB_SEPARATOR.join(["ACL_TABLE", acl_table_name]), {}).get("type") |
| 44 | + req = MatchRequest(db="CONFIG_DB", table="ACL_TABLE_TYPE", key_pattern=acl_table_type_name, ns=self.ns) |
| 45 | + ret = self.match_engine.fetch(req) |
| 46 | + # If not found don't add it to the table, it might be a default table type |
| 47 | + if ret["keys"]: |
| 48 | + self.add_to_ret_template(req.table, req.db, ret["keys"], ret["error"]) |
| 49 | + |
| 50 | + def init_acl_table_asic_info(self, acl_table_name): |
| 51 | + req = MatchRequest(db="CONFIG_DB", table="ACL_RULE", key_pattern=CFG_DB_SEPARATOR.join([acl_table_name, "*"]), ns=self.ns) |
| 52 | + ret = self.match_engine.fetch(req) |
| 53 | + acl_rules = ret["keys"] |
| 54 | + if not acl_rules: |
| 55 | + return |
| 56 | + acl_rule_name = acl_rules[0].split(CFG_DB_SEPARATOR)[-1] |
| 57 | + |
| 58 | + counter_oid = fetch_acl_counter_oid(self.match_engine, acl_table_name, acl_rule_name, self.ns) |
| 59 | + if not counter_oid: |
| 60 | + return |
| 61 | + |
| 62 | + req = MatchRequest(db="ASIC_DB", table=ASIC_DB_SEPARATOR.join(["ASIC_STATE", "SAI_OBJECT_TYPE_ACL_COUNTER"]), |
| 63 | + key_pattern=counter_oid, return_fields=["SAI_ACL_COUNTER_ATTR_TABLE_ID"], ns=self.ns) |
| 64 | + ret = self.match_engine.fetch(req) |
| 65 | + |
| 66 | + return_values = ret["return_values"] |
| 67 | + counter_object = return_values.get(ASIC_DB_SEPARATOR.join(["ASIC_STATE", "SAI_OBJECT_TYPE_ACL_COUNTER", counter_oid]), {}) |
| 68 | + table_oid = counter_object.get("SAI_ACL_COUNTER_ATTR_TABLE_ID") |
| 69 | + if not table_oid: |
| 70 | + raise Exception("Invalid counter object without table OID in ASIC_DB") |
| 71 | + |
| 72 | + req = MatchRequest(db="ASIC_DB", table=ASIC_DB_SEPARATOR.join(["ASIC_STATE", "SAI_OBJECT_TYPE_ACL_TABLE"]), |
| 73 | + key_pattern=table_oid, ns=self.ns) |
| 74 | + ret = self.match_engine.fetch(req) |
| 75 | + self.add_to_ret_template(req.table, req.db, ret["keys"], ret["error"]) |
| 76 | + |
| 77 | + req = MatchRequest(db="ASIC_DB", table=ASIC_DB_SEPARATOR.join(["ASIC_STATE", "SAI_OBJECT_TYPE_ACL_TABLE_GROUP_MEMBER"]), |
| 78 | + key_pattern="*", field="SAI_ACL_TABLE_GROUP_MEMBER_ATTR_ACL_TABLE_ID", |
| 79 | + value=table_oid, return_fields=["SAI_ACL_TABLE_GROUP_MEMBER_ATTR_ACL_TABLE_GROUP_ID"], ns=self.ns) |
| 80 | + ret = self.match_engine.fetch(req) |
| 81 | + self.add_to_ret_template(req.table, req.db, ret["keys"], ret["error"]) |
| 82 | + |
| 83 | + group_oids = set() |
| 84 | + for _, entry in ret["return_values"].items(): |
| 85 | + group_oids.add(entry.get("SAI_ACL_TABLE_GROUP_MEMBER_ATTR_ACL_TABLE_GROUP_ID")) |
| 86 | + |
| 87 | + for group_oid in group_oids: |
| 88 | + req = MatchRequest(db="ASIC_DB", table=ASIC_DB_SEPARATOR.join(["ASIC_STATE", "SAI_OBJECT_TYPE_ACL_TABLE_GROUP"]), |
| 89 | + key_pattern=group_oid, ns=self.ns) |
| 90 | + ret = self.match_engine.fetch(req) |
| 91 | + self.add_to_ret_template(req.table, req.db, ret["keys"], ret["error"]) |
0 commit comments