Skip to content

Add scope to field validator. #3675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions generic_config_updater/field_operation_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@
import jsonpointer
import subprocess
from sonic_py_common import device_info
from .gu_common import GenericConfigUpdaterError
from .gu_common import GenericConfigUpdaterError, HOST_NAMESPACE
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():

def get_asic_name(scope):
asic = "unknown"

if os.path.exists(GCU_TABLE_MOD_CONF_FILE):
Expand All @@ -27,7 +29,12 @@ def get_asic_name():
if asic_type == 'cisco-8000':
asic = "cisco-8000"
elif asic_type == 'mellanox' or asic_type == 'vs' or asic_type == 'broadcom':
proc = subprocess.Popen(GET_HWSKU_CMD, shell=True, universal_newlines=True, stdout=subprocess.PIPE)
get_hwsku_cmds = []
if scope == HOST_NAMESPACE:
get_hwsku_cmds = ["sonic-cfggen", "-d", "-v", "DEVICE_METADATA.localhost.hwsku"]
else:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may not be really required as we have the hwsku in the hostname space and asic namespace. But it should be ok as we have the same in all namespaces.

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)
output, err = proc.communicate()
hwsku = output.rstrip('\n')
if asic_type == 'mellanox' or asic_type == 'vs':
Expand Down Expand Up @@ -60,8 +67,8 @@ def get_asic_name():
return asic


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


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


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

def _validate_field(field, port, value):
if field == "fec":
supported_fecs_str = read_statedb_entry("PORT_TABLE", port, "supported_fecs")
supported_fecs_str = read_statedb_entry(scope, "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 @@ -155,7 +162,7 @@ def _validate_field(field, port, value):
return False
return True
if field == "speed":
supported_speeds_str = read_statedb_entry("PORT_TABLE", port, "supported_speeds") or ''
supported_speeds_str = read_statedb_entry(scope, "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 @@ -189,7 +189,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(jsonpatch_element)
return method_to_call(self.scope, jsonpatch_element)

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