Skip to content

Commit

Permalink
Revert PR: Add scope to field validator (sonic-net#3689)
Browse files Browse the repository at this point in the history
* Revert PR: Add scope to field validator

Reverts sonic-net#3675 since it would cause PR test failure:
  • Loading branch information
xwjiang-ms authored Dec 19, 2024
1 parent 5481d0e commit 2f8508f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 122 deletions.
28 changes: 11 additions & 17 deletions generic_config_updater/field_operation_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@
import jsonpointer
import subprocess
from sonic_py_common import device_info
from .gu_common import GenericConfigUpdaterError, HOST_NAMESPACE
from .gu_common import GenericConfigUpdaterError
from swsscommon import swsscommon
from utilities_common.constants import DEFAULT_SUPPORTED_FECS_LIST

STATE_DB_NAME = 'STATE_DB'
REDIS_TIMEOUT_MSECS = 0
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
GCU_TABLE_MOD_CONF_FILE = f"{SCRIPT_DIR}/gcu_field_operation_validators.conf.json"
GET_HWSKU_CMD = "sonic-cfggen -d -v DEVICE_METADATA.localhost.hwsku"


def get_asic_name(scope):
def get_asic_name():
asic = "unknown"

if os.path.exists(GCU_TABLE_MOD_CONF_FILE):
Expand All @@ -29,12 +28,7 @@ def get_asic_name(scope):
if asic_type == 'cisco-8000':
asic = "cisco-8000"
elif asic_type == 'mellanox' or asic_type == 'vs' or asic_type == 'broadcom':
get_hwsku_cmds = []
if scope == HOST_NAMESPACE:
get_hwsku_cmds = ["sonic-cfggen", "-d", "-v", "DEVICE_METADATA.localhost.hwsku"]
else:
get_hwsku_cmds = ["sonic-cfggen", "-d", "-n", scope, "-v", "DEVICE_METADATA.localhost.hwsku"]
proc = subprocess.Popen(get_hwsku_cmds, shell=False, universal_newlines=True, stdout=subprocess.PIPE)
proc = subprocess.Popen(GET_HWSKU_CMD, shell=True, universal_newlines=True, stdout=subprocess.PIPE)
output, err = proc.communicate()
hwsku = output.rstrip('\n')
if asic_type == 'mellanox' or asic_type == 'vs':
Expand Down Expand Up @@ -67,8 +61,8 @@ def get_asic_name(scope):
return asic


def rdma_config_update_validator(scope, patch_element):
asic = get_asic_name(scope)
def rdma_config_update_validator(patch_element):
asic = get_asic_name()
if asic == "unknown":
return False
version_info = device_info.get_sonic_version_info()
Expand Down Expand Up @@ -140,17 +134,17 @@ def _get_fields_in_patch():
return True


def read_statedb_entry(scope, table, key, field):
state_db = swsscommon.DBConnector(STATE_DB_NAME, REDIS_TIMEOUT_MSECS, True, scope)
def read_statedb_entry(table, key, field):
state_db = swsscommon.DBConnector("STATE_DB", 0)
tbl = swsscommon.Table(state_db, table)
return tbl.hget(key, field)[1]


def port_config_update_validator(scope, patch_element):
def port_config_update_validator(patch_element):

def _validate_field(field, port, value):
if field == "fec":
supported_fecs_str = read_statedb_entry(scope, "PORT_TABLE", port, "supported_fecs")
supported_fecs_str = read_statedb_entry("PORT_TABLE", port, "supported_fecs")
if supported_fecs_str:
if supported_fecs_str != 'N/A':
supported_fecs_list = [element.strip() for element in supported_fecs_str.split(',')]
Expand All @@ -162,7 +156,7 @@ def _validate_field(field, port, value):
return False
return True
if field == "speed":
supported_speeds_str = read_statedb_entry(scope, "PORT_TABLE", port, "supported_speeds") or ''
supported_speeds_str = read_statedb_entry("PORT_TABLE", port, "supported_speeds") or ''
try:
supported_speeds = [int(s) for s in supported_speeds_str.split(',') if s]
if supported_speeds and int(value) not in supported_speeds:
Expand Down
2 changes: 1 addition & 1 deletion generic_config_updater/gu_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def _invoke_validating_function(cmd, jsonpatch_element):
raise GenericConfigUpdaterError("Attempting to call invalid method {} in module {}. Module must be generic_config_updater.field_operation_validators, and method must be a defined validator".format(method_name, module_name))
module = importlib.import_module(module_name, package=None)
method_to_call = getattr(module, method_name)
return method_to_call(self.scope, jsonpatch_element)
return method_to_call(jsonpatch_element)

if os.path.exists(GCU_FIELD_OP_CONF_FILE):
with open(GCU_FIELD_OP_CONF_FILE, "r") as s:
Expand Down
Loading

0 comments on commit 2f8508f

Please sign in to comment.