Skip to content
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

[GCU] Supressing YANG errors from libyang while sorting #1991

Merged
merged 2 commits into from
Apr 29, 2022
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
14 changes: 9 additions & 5 deletions generic_config_updater/gu_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ def validate_sonic_yang_config(self, sonic_yang_as_json):
sy.loadData(config_db_as_json)

sy.validate_data_tree()
return True
return True, None
except sonic_yang.SonicYangException as ex:
return False
return False, ex

def validate_config_db_config(self, config_db_as_json):
sy = self.create_sonic_yang_with_loaded_models()
Expand All @@ -118,9 +118,9 @@ def validate_config_db_config(self, config_db_as_json):
sy.loadData(tmp_config_db_as_json)

sy.validate_data_tree()
return True
return True, None
except sonic_yang.SonicYangException as ex:
return False
return False, ex

def crop_tables_without_yang(self, config_db_as_json):
sy = self.create_sonic_yang_with_loaded_models()
Expand Down Expand Up @@ -151,7 +151,8 @@ def remove_empty_tables(self, config):
def create_sonic_yang_with_loaded_models(self):
# sonic_yang_with_loaded_models will only be initialized once the first time this method is called
if self.sonic_yang_with_loaded_models is None:
loaded_models_sy = sonic_yang.SonicYang(self.yang_dir)
sonic_yang_print_log_enabled = genericUpdaterLogging.get_verbose()
loaded_models_sy = sonic_yang.SonicYang(self.yang_dir, print_log_enabled=sonic_yang_print_log_enabled)
loaded_models_sy.loadYangModel() # This call takes a long time (100s of ms) because it reads files from disk
self.sonic_yang_with_loaded_models = loaded_models_sy

Expand Down Expand Up @@ -829,6 +830,9 @@ def __init__(self):
def set_verbose(self, verbose):
self._verbose = verbose

def get_verbose(self):
return self._verbose

def get_logger(self, title, print_all_to_console=False):
return TitledLogger(SYSLOG_IDENTIFIER, title, self._verbose, print_all_to_console)

Expand Down
13 changes: 8 additions & 5 deletions generic_config_updater/patch_sorter.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,8 @@ def __init__(self, config_wrapper):

def validate(self, move, diff):
simulated_config = move.apply(diff.current_config)
return self.config_wrapper.validate_config_db_config(simulated_config)
is_valid, error = self.config_wrapper.validate_config_db_config(simulated_config)
Copy link
Contributor

Choose a reason for hiding this comment

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

error

Not a big issue, you can use _ if you intend not to use it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will fix it in a later PR as this is blocking #2145

return is_valid

# TODO: Add this validation to YANG models instead
class UniqueLanesMoveValidator:
Expand Down Expand Up @@ -1543,8 +1544,9 @@ def sort(self, patch, algorithm=Algorithm.DFS):

# Validate target config
self.logger.log_info("Validating target config according to YANG models.")
if not(self.config_wrapper.validate_config_db_config(target_config)):
raise ValueError(f"Given patch is not valid because it will result in an invalid config")
is_valid, error = self.config_wrapper.validate_config_db_config(target_config)
if not is_valid:
raise ValueError(f"Given patch will produce invalid config. Error: {error}")

# Generate list of changes to apply
self.logger.log_info("Sorting patch updates.")
Expand Down Expand Up @@ -1731,8 +1733,9 @@ def sort(self, patch, algorithm=Algorithm.DFS):

# Validate YANG covered target config
self.logger.log_info("Validating YANG covered target config according to YANG models.")
if not(self.config_wrapper.validate_config_db_config(target_config_yang)):
raise ValueError(f"Given patch is not valid because it will result in an invalid config")
is_valid, error = self.config_wrapper.validate_config_db_config(target_config_yang)
if not is_valid:
raise ValueError(f"Given patch will produce invalid config. Error: {error}")

# Generating changes associated with non-YANG covered configs
self.logger.log_info("Sorting non-YANG covered configs patch updates.")
Expand Down
12 changes: 8 additions & 4 deletions tests/generic_config_updater/gu_common_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,43 +144,47 @@ def test_validate_sonic_yang_config__valid_config__returns_true(self):
expected = True

# Act
actual = config_wrapper.validate_sonic_yang_config(Files.SONIC_YANG_AS_JSON)
actual, error = config_wrapper.validate_sonic_yang_config(Files.SONIC_YANG_AS_JSON)

# Assert
self.assertEqual(expected, actual)
self.assertIsNone(error)

def test_validate_sonic_yang_config__invvalid_config__returns_false(self):
# Arrange
config_wrapper = gu_common.ConfigWrapper()
expected = False

# Act
actual = config_wrapper.validate_sonic_yang_config(Files.SONIC_YANG_AS_JSON_INVALID)
actual, error = config_wrapper.validate_sonic_yang_config(Files.SONIC_YANG_AS_JSON_INVALID)

# Assert
self.assertEqual(expected, actual)
self.assertIsNotNone(error)

def test_validate_config_db_config__valid_config__returns_true(self):
# Arrange
config_wrapper = gu_common.ConfigWrapper()
expected = True

# Act
actual = config_wrapper.validate_config_db_config(Files.CONFIG_DB_AS_JSON)
actual, error = config_wrapper.validate_config_db_config(Files.CONFIG_DB_AS_JSON)

# Assert
self.assertEqual(expected, actual)
self.assertIsNone(error)

def test_validate_config_db_config__invalid_config__returns_false(self):
# Arrange
config_wrapper = gu_common.ConfigWrapper()
expected = False

# Act
actual = config_wrapper.validate_config_db_config(Files.CONFIG_DB_AS_JSON_INVALID)
actual, error = config_wrapper.validate_config_db_config(Files.CONFIG_DB_AS_JSON_INVALID)

# Assert
self.assertEqual(expected, actual)
self.assertIsNotNone(error)

def test_crop_tables_without_yang__returns_cropped_config_db_as_json(self):
# Arrange
Expand Down
11 changes: 6 additions & 5 deletions tests/generic_config_updater/patch_sorter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,7 @@ def test_validate__invalid_config_db_after_applying_move__failure(self):
# Arrange
config_wrapper = Mock()
config_wrapper.validate_config_db_config.side_effect = \
create_side_effect_dict({(str(self.any_simulated_config),): False})
create_side_effect_dict({(str(self.any_simulated_config),): (False, None)})
validator = ps.FullConfigMoveValidator(config_wrapper)

# Act and assert
Expand All @@ -935,7 +935,7 @@ def test_validate__valid_config_db_after_applying_move__success(self):
# Arrange
config_wrapper = Mock()
config_wrapper.validate_config_db_config.side_effect = \
create_side_effect_dict({(str(self.any_simulated_config),): True})
create_side_effect_dict({(str(self.any_simulated_config),): (True, None)})
validator = ps.FullConfigMoveValidator(config_wrapper)

# Act and assert
Expand Down Expand Up @@ -3102,7 +3102,8 @@ def run_single_success_case(self, data, skip_exact_change_list_match):
simulated_config = current_config
for change in actual_changes:
simulated_config = change.apply(simulated_config)
self.assertTrue(self.config_wrapper.validate_config_db_config(simulated_config))
is_valid, error = self.config_wrapper.validate_config_db_config(simulated_config)
self.assertTrue(is_valid, f"Change will produce invalid config. Error: {error}")
self.assertEqual(target_config, simulated_config)

def test_patch_sorter_failure(self):
Expand Down Expand Up @@ -3426,7 +3427,7 @@ def __create_patch_sorter(self,
(str(any_target_config),): (any_target_config_yang, any_target_config_non_yang)})

config_wrapper.validate_config_db_config.side_effect = \
create_side_effect_dict({(str(any_target_config_yang),): valid_yang_covered_config})
create_side_effect_dict({(str(any_target_config_yang),): (valid_yang_covered_config, None)})

patch_wrapper.generate_patch.side_effect = \
create_side_effect_dict(
Expand Down Expand Up @@ -3519,7 +3520,7 @@ def __create_patch_sorter(self,

config_wrapper.validate_config_db_config.side_effect = \
create_side_effect_dict(
{(str(any_target_config),): valid_config_db})
{(str(any_target_config),): (valid_config_db, None)})


inner_patch_sorter.sort.side_effect = \
Expand Down