Skip to content

Commit

Permalink
Revert "[macsec]: Add MACsec clear CLI support (sonic-net#11731)"
Browse files Browse the repository at this point in the history
This reverts commit 3b128ec.
  • Loading branch information
yxieca committed Sep 29, 2022
1 parent 09615a5 commit 05d482e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 94 deletions.
36 changes: 0 additions & 36 deletions dockers/docker-macsec/cli/clear/plugins/clear_macsec_counter.py

This file was deleted.

72 changes: 15 additions & 57 deletions dockers/docker-macsec/cli/show/plugins/show_macsec.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
import typing
from natsort import natsorted
import datetime
import pickle
import os
import copy

import click
from tabulate import tabulate

import utilities_common.multi_asic as multi_asic_util
from swsscommon.swsscommon import CounterTable, MacsecCounter
from utilities_common.cli import UserCache

CACHE_MANAGER = UserCache(app_name="macsec")
CACHE_FILE = os.path.join(CACHE_MANAGER.get_directory(), "macsecstats{}")

DB_CONNECTOR = None
COUNTER_TABLE = None
Expand All @@ -22,12 +15,12 @@
class MACsecAppMeta(object):
def __init__(self, *args) -> None:
SEPARATOR = DB_CONNECTOR.get_db_separator(DB_CONNECTOR.APPL_DB)
self.key = self.__class__.get_appl_table_name() + SEPARATOR + \
key = self.__class__.get_appl_table_name() + SEPARATOR + \
SEPARATOR.join(args)
self.meta = DB_CONNECTOR.get_all(
DB_CONNECTOR.APPL_DB, self.key)
DB_CONNECTOR.APPL_DB, key)
if len(self.meta) == 0:
raise ValueError("No such MACsecAppMeta: {}".format(self.key))
raise ValueError("No such MACsecAppMeta: {}".format(key))
for k, v in self.meta.items():
setattr(self, k, v)

Expand All @@ -46,15 +39,10 @@ def __init__(self, port_name: str, sci: str, an: str) -> None:
MACsecAppMeta.__init__(self, port_name, sci, an)
MACsecCounters.__init__(self, port_name, sci, an)

def dump_str(self, cache = None) -> str:
def dump_str(self) -> str:
buffer = self.get_header()
meta = sorted(self.meta.items(), key=lambda x: x[0])
counters = copy.deepcopy(self.counters)
if cache:
for k, v in counters.items():
if k in cache.counters:
counters[k] = int(counters[k]) - int(cache.counters[k])
counters = sorted(counters.items(), key=lambda x: x[0])
counters = sorted(self.counters.items(), key=lambda x: x[0])
buffer += tabulate(meta + counters)
buffer = "\n".join(["\t\t" + line for line in buffer.splitlines()])
return buffer
Expand Down Expand Up @@ -99,7 +87,7 @@ def __init__(self, port_name: str, sci: str) -> None:
def get_appl_table_name(cls) -> str:
return "MACSEC_INGRESS_SC_TABLE"

def dump_str(self, cache = None) -> str:
def dump_str(self) -> str:
buffer = self.get_header()
buffer = "\n".join(["\t" + line for line in buffer.splitlines()])
return buffer
Expand All @@ -116,7 +104,7 @@ def __init__(self, port_name: str, sci: str) -> None:
def get_appl_table_name(cls) -> str:
return "MACSEC_EGRESS_SC_TABLE"

def dump_str(self, cache = None) -> str:
def dump_str(self) -> str:
buffer = self.get_header()
buffer += tabulate(sorted(self.meta.items(), key=lambda x: x[0]))
buffer = "\n".join(["\t" + line for line in buffer.splitlines()])
Expand All @@ -135,7 +123,7 @@ def __init__(self, port_name: str) -> None:
def get_appl_table_name(cls) -> str:
return "MACSEC_PORT_TABLE"

def dump_str(self, cache = None) -> str:
def dump_str(self) -> str:
buffer = self.get_header()
buffer += tabulate(sorted(self.meta.items(), key=lambda x: x[0]))
return buffer
Expand All @@ -161,7 +149,6 @@ def create_macsec_obj(key: str) -> MACsecAppMeta:
except ValueError as e:
return None


def create_macsec_objs(interface_name: str) -> typing.List[MACsecAppMeta]:
objs = []
objs.append(create_macsec_obj(MACsecPort.get_appl_table_name() + ":" + interface_name))
Expand Down Expand Up @@ -192,25 +179,12 @@ def create_macsec_objs(interface_name: str) -> typing.List[MACsecAppMeta]:
return objs


def cache_find(cache: dict, target: MACsecAppMeta) -> MACsecAppMeta:
if not cache or not cache["objs"]:
return None
for obj in cache["objs"]:
if type(obj) == type(target) and obj.key == target.key:
# MACsec SA may be refreshed by a cycle that use the same key
# So, use the SA as the identifier
if isinstance(obj, MACsecSA) and obj.sak != target.sak:
continue
return obj
return None


@click.command()
@click.argument('interface_name', required=False)
@click.option('--dump-file', is_flag=True, required=False, default=False)
@multi_asic_util.multi_asic_click_options
def macsec(interface_name, dump_file, namespace, display):
MacsecContext(namespace, display).show(interface_name, dump_file)
def macsec(interface_name, namespace, display):
MacsecContext(namespace, display).show(interface_name)


class MacsecContext(object):

Expand All @@ -220,7 +194,7 @@ def __init__(self, namespace_option, display_option):
display_option, namespace_option)

@multi_asic_util.run_on_multi_asic
def show(self, interface_name, dump_file):
def show(self, interface_name):
global DB_CONNECTOR
global COUNTER_TABLE
DB_CONNECTOR = self.db
Expand All @@ -231,29 +205,13 @@ def show(self, interface_name, dump_file):
if interface_name not in interface_names:
return
interface_names = [interface_name]
objs = []

objs = []
for interface_name in natsorted(interface_names):
objs += create_macsec_objs(interface_name)
for obj in objs:
print(obj.dump_str())

cache = {}
if os.path.isfile(CACHE_FILE.format(self.multi_asic.current_namespace)):
cache = pickle.load(open(CACHE_FILE.format(self.multi_asic.current_namespace), "rb"))

if not dump_file:
if cache and cache["time"] and objs:
print("Last cached time was {}".format(cache["time"]))
for obj in objs:
cache_obj = cache_find(cache, obj)
print(obj.dump_str(cache_obj))
else:
dump_obj = {
"time": datetime.datetime.now(),
"objs": objs
}
with open(CACHE_FILE.format(self.multi_asic.current_namespace), 'wb') as dump_file:
pickle.dump(dump_obj, dump_file)
dump_file.flush()

def register(cli):
cli.add_command(macsec)
Expand Down
1 change: 0 additions & 1 deletion rules/docker-macsec.mk
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,5 @@ $(DOCKER_MACSEC)_RUN_OPT += -v /host/warmboot:/var/warmboot

$(DOCKER_MACSEC)_CLI_CONFIG_PLUGIN = /cli/config/plugins/macsec.py
$(DOCKER_MACSEC)_CLI_SHOW_PLUGIN = /cli/show/plugins/show_macsec.py
$(DOCKER_MACSEC)_CLI_CLEAR_PLUGIN = /cli/clear/plugins/clear_macsec_counter.py

$(DOCKER_MACSEC)_FILES += $(SUPERVISOR_PROC_EXIT_LISTENER_SCRIPT)

0 comments on commit 05d482e

Please sign in to comment.