Skip to content

Commit 2ef5b31

Browse files
authored
[GCU] Add PFC_WD RDMA validator (#2619)
1 parent c7aa841 commit 2ef5b31

File tree

7 files changed

+102
-10
lines changed

7 files changed

+102
-10
lines changed

generic_config_updater/change_applier.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from .gu_common import genericUpdaterLogging
1010

1111
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
12-
UPDATER_CONF_FILE = f"{SCRIPT_DIR}/generic_config_updater.conf.json"
12+
UPDATER_CONF_FILE = f"{SCRIPT_DIR}/gcu_services_validator.conf.json"
1313
logger = genericUpdaterLogging.get_logger(title="Change Applier")
1414

1515
print_to_console = False
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from sonic_py_common import device_info
2+
import re
3+
4+
def rdma_config_update_validator():
5+
version_info = device_info.get_sonic_version_info()
6+
build_version = version_info.get('build_version')
7+
asic_type = version_info.get('asic_type')
8+
9+
if (asic_type != 'mellanox' and asic_type != 'broadcom' and asic_type != 'cisco-8000'):
10+
return False
11+
12+
version_substrings = build_version.split('.')
13+
branch_version = None
14+
15+
for substring in version_substrings:
16+
if substring.isdigit() and re.match(r'^\d{8}$', substring):
17+
branch_version = substring
18+
break
19+
20+
if branch_version is None:
21+
return False
22+
23+
if asic_type == 'cisco-8000':
24+
return branch_version >= "20201200"
25+
else:
26+
return branch_version >= "20181100"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"README": [
3+
"field_operation_validators provides, module & method name as ",
4+
" <module name>.<method name>",
5+
"NOTE: module name could have '.'",
6+
" ",
7+
"The last element separated by '.' is considered as ",
8+
"method name",
9+
"",
10+
"e.g. 'show.acl.test_acl'",
11+
"",
12+
"field_operation_validators for a given table defines a list of validators that all must pass for modification to the specified field and table to be allowed",
13+
""
14+
],
15+
"tables": {
16+
"PFC_WD": {
17+
"field_operation_validators": [ "generic_config_updater.field_operation_validators.rdma_config_update_validator" ]
18+
}
19+
}
20+
}

generic_config_updater/gu_common.py

+36
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
import json
22
import jsonpatch
3+
import importlib
34
from jsonpointer import JsonPointer
45
import sonic_yang
56
import sonic_yang_ext
67
import subprocess
78
import yang as ly
89
import copy
910
import re
11+
import os
1012
from sonic_py_common import logger
1113
from enum import Enum
1214

1315
YANG_DIR = "/usr/local/yang-models"
1416
SYSLOG_IDENTIFIER = "GenericConfigUpdater"
17+
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
18+
GCU_FIELD_OP_CONF_FILE = f"{SCRIPT_DIR}/gcu_field_operation_validators.conf.json"
1519

1620
class GenericConfigUpdaterError(Exception):
1721
pass
@@ -162,6 +166,38 @@ def validate_field_operation(self, old_config, target_config):
162166
if any(op['op'] == operation and field == op['path'] for op in patch):
163167
raise IllegalPatchOperationError("Given patch operation is invalid. Operation: {} is illegal on field: {}".format(operation, field))
164168

169+
def _invoke_validating_function(cmd):
170+
# cmd is in the format as <package/module name>.<method name>
171+
method_name = cmd.split(".")[-1]
172+
module_name = ".".join(cmd.split(".")[0:-1])
173+
if module_name != "generic_config_updater.field_operation_validators" or "validator" not in method_name:
174+
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))
175+
module = importlib.import_module(module_name, package=None)
176+
method_to_call = getattr(module, method_name)
177+
return method_to_call()
178+
179+
if os.path.exists(GCU_FIELD_OP_CONF_FILE):
180+
with open(GCU_FIELD_OP_CONF_FILE, "r") as s:
181+
gcu_field_operation_conf = json.load(s)
182+
else:
183+
raise GenericConfigUpdaterError("GCU field operation validators config file not found")
184+
185+
for element in patch:
186+
path = element["path"]
187+
match = re.search(r'\/([^\/]+)(\/|$)', path) # This matches the table name in the path, eg if path if /PFC_WD/GLOBAL, the match would be PFC_WD
188+
if match is not None:
189+
table = match.group(1)
190+
else:
191+
raise GenericConfigUpdaterError("Invalid jsonpatch path: {}".format(path))
192+
validating_functions= set()
193+
tables = gcu_field_operation_conf["tables"]
194+
validating_functions.update(tables.get(table, {}).get("field_operation_validators", []))
195+
196+
for function in validating_functions:
197+
if not _invoke_validating_function(function):
198+
raise IllegalPatchOperationError("Modification of {} table is illegal- validating function {} returned False".format(table, function))
199+
200+
165201
def validate_lanes(self, config_db):
166202
if "PORT" not in config_db:
167203
return True, None

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
'sonic_cli_gen',
6565
],
6666
package_data={
67-
'generic_config_updater': ['generic_config_updater.conf.json'],
67+
'generic_config_updater': ['gcu_services_validator.conf.json', 'gcu_field_operation_validators.conf.json'],
6868
'show': ['aliases.ini'],
6969
'sonic_installer': ['aliases.ini'],
7070
'tests': ['acl_input/*',

tests/generic_config_updater/gu_common_test.py

+18-8
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import jsonpatch
44
import sonic_yang
55
import unittest
6-
from unittest.mock import MagicMock, Mock, patch
6+
import mock
77

8+
from unittest.mock import MagicMock, Mock
9+
from mock import patch
810
from .gutest_helpers import create_side_effect_dict, Files
911
import generic_config_updater.gu_common as gu_common
1012

@@ -69,11 +71,25 @@ def setUp(self):
6971
self.config_wrapper_mock = gu_common.ConfigWrapper()
7072
self.config_wrapper_mock.get_config_db_as_json=MagicMock(return_value=Files.CONFIG_DB_AS_JSON)
7173

74+
@patch("sonic_py_common.device_info.get_sonic_version_info", mock.Mock(return_value={"asic_type": "mellanox", "build_version": "SONiC.20181131"}))
7275
def test_validate_field_operation_legal__pfcwd(self):
7376
old_config = {"PFC_WD": {"GLOBAL": {"POLL_INTERVAL": "60"}}}
7477
target_config = {"PFC_WD": {"GLOBAL": {"POLL_INTERVAL": "40"}}}
7578
config_wrapper = gu_common.ConfigWrapper()
7679
config_wrapper.validate_field_operation(old_config, target_config)
80+
81+
def test_validate_field_operation_illegal__pfcwd(self):
82+
old_config = {"PFC_WD": {"GLOBAL": {"POLL_INTERVAL": "60"}}}
83+
target_config = {"PFC_WD": {"GLOBAL": {}}}
84+
config_wrapper = gu_common.ConfigWrapper()
85+
self.assertRaises(gu_common.IllegalPatchOperationError, config_wrapper.validate_field_operation, old_config, target_config)
86+
87+
@patch("sonic_py_common.device_info.get_sonic_version_info", mock.Mock(return_value={"asic_type": "invalid-asic", "build_version": "SONiC.20181131"}))
88+
def test_validate_field_modification_illegal__pfcwd(self):
89+
old_config = {"PFC_WD": {"GLOBAL": {"POLL_INTERVAL": "60"}}}
90+
target_config = {"PFC_WD": {"GLOBAL": {"POLL_INTERVAL": "80"}}}
91+
config_wrapper = gu_common.ConfigWrapper()
92+
self.assertRaises(gu_common.IllegalPatchOperationError, config_wrapper.validate_field_operation, old_config, target_config)
7793

7894
def test_validate_field_operation_legal__rm_loopback1(self):
7995
old_config = {
@@ -92,13 +108,7 @@ def test_validate_field_operation_legal__rm_loopback1(self):
92108
}
93109
config_wrapper = gu_common.ConfigWrapper()
94110
config_wrapper.validate_field_operation(old_config, target_config)
95-
96-
def test_validate_field_operation_illegal__pfcwd(self):
97-
old_config = {"PFC_WD": {"GLOBAL": {"POLL_INTERVAL": 60}}}
98-
target_config = {"PFC_WD": {"GLOBAL": {}}}
99-
config_wrapper = gu_common.ConfigWrapper()
100-
self.assertRaises(gu_common.IllegalPatchOperationError, config_wrapper.validate_field_operation, old_config, target_config)
101-
111+
102112
def test_validate_field_operation_illegal__rm_loopback0(self):
103113
old_config = {
104114
"LOOPBACK_INTERFACE": {

0 commit comments

Comments
 (0)