From 04ea32b0c2c15c569fc2efc802f72b2a1cd27236 Mon Sep 17 00:00:00 2001 From: Junhua Zhai Date: Sat, 18 Jun 2022 19:17:05 +0800 Subject: [PATCH] [macsec] CLI Supports display of gearbox macsec counter (#11113) Why I did it To support gearbox macsec counter display, following Azure/sonic-swss-common#622. How I did it Use swsscommon CounterTable API --- .../cli-plugin-tests/mock_tables.py | 15 +++++++++++++++ .../cli/show/plugins/show_macsec.py | 18 ++++-------------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/dockers/docker-macsec/cli-plugin-tests/mock_tables.py b/dockers/docker-macsec/cli-plugin-tests/mock_tables.py index 2e88297597..3708644bfb 100644 --- a/dockers/docker-macsec/cli-plugin-tests/mock_tables.py +++ b/dockers/docker-macsec/cli-plugin-tests/mock_tables.py @@ -115,6 +115,19 @@ class SwssSyncClient(mockredis.MockRedis): return [key for key in self.redis if regex.match(key)] +class MacsecCounter: + pass + + +class CounterTable: + def __init__(self, db): + self.db = db + + def get(self, macsec, name): + key = self.db.hget("COUNTERS_MACSEC_NAME_MAP", name) + return self.db.get("COUNTERS:" + key) + + swsssdk.interface.DBInterface._subscribe_keyspace_notification = _subscribe_keyspace_notification mockredis.MockRedis.config_set = config_set redis.StrictRedis = SwssSyncClient @@ -122,3 +135,5 @@ SonicV2Connector.connect = connect_SonicV2Connector swsscommon.SonicV2Connector = SonicV2Connector swsscommon.ConfigDBConnector = ConfigDBConnector swsscommon.ConfigDBPipeConnector = ConfigDBPipeConnector +swsscommon.CounterTable = CounterTable +swsscommon.MacsecCounter = MacsecCounter diff --git a/dockers/docker-macsec/cli/show/plugins/show_macsec.py b/dockers/docker-macsec/cli/show/plugins/show_macsec.py index 538b8947f9..0d32f7e962 100644 --- a/dockers/docker-macsec/cli/show/plugins/show_macsec.py +++ b/dockers/docker-macsec/cli/show/plugins/show_macsec.py @@ -5,11 +5,13 @@ import click from tabulate import tabulate from swsscommon.swsscommon import SonicV2Connector +from swsscommon.swsscommon import CounterTable, MacsecCounter DB_CONNECTOR = SonicV2Connector(use_unix_socket_path=False) DB_CONNECTOR.connect(DB_CONNECTOR.APPL_DB) DB_CONNECTOR.connect(DB_CONNECTOR.COUNTERS_DB) +COUNTER_TABLE = CounterTable(DB_CONNECTOR.get_redis_client(DB_CONNECTOR.COUNTERS_DB)) class MACsecAppMeta(object): @@ -28,12 +30,8 @@ class MACsecAppMeta(object): class MACsecCounters(object): def __init__(self, *args) -> None: - key = ":".join(args) - counters_id = DB_CONNECTOR.get( - DB_CONNECTOR.COUNTERS_DB, self.__class__.get_counter_table_name(), key) - counter_key = "COUNTERS:" + counters_id - self.counters = DB_CONNECTOR.get_all( - DB_CONNECTOR.COUNTERS_DB, counter_key) + _, fvs = COUNTER_TABLE.get(MacsecCounter(), ":".join(args)) + self.counters = dict(fvs) class MACsecSA(MACsecAppMeta, MACsecCounters): @@ -61,10 +59,6 @@ class MACsecIngressSA(MACsecSA): def get_appl_table_name(cls) -> str: return "MACSEC_INGRESS_SA_TABLE" - @classmethod - def get_counter_table_name(cls) -> str: - return "COUNTERS_MACSEC_SA_RX_NAME_MAP" - def get_header(self): return "MACsec Ingress SA ({})\n".format(self.an) @@ -77,10 +71,6 @@ class MACsecEgressSA(MACsecSA): def get_appl_table_name(cls) -> str: return "MACSEC_EGRESS_SA_TABLE" - @classmethod - def get_counter_table_name(cls) -> str: - return "COUNTERS_MACSEC_SA_TX_NAME_MAP" - def get_header(self): return "MACsec Egress SA ({})\n".format(self.an)