-
Notifications
You must be signed in to change notification settings - Fork 819
Generic_upater: Apply JSON change #1856
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
Changes from 4 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7863b59
ChangeApplier and unit test code.
renukamanavalan 84f493b
minor update
renukamanavalan dd337eb
fix unused import
renukamanavalan 6e0bba0
Added service validation
renukamanavalan cabcf23
Take off added code for logging
renukamanavalan a075606
Merge remote-tracking branch 'upstream/master' into updater
renukamanavalan bd7781c
fix merge
renukamanavalan f839e53
change applier & test updated for service validation
renukamanavalan beb1ea7
removed unused import
renukamanavalan fe25d2e
Added two service validators
renukamanavalan fec8f8f
move global to class; No logical code changes
renukamanavalan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| import copy | ||
| import json | ||
| import importlib | ||
| import os | ||
| import syslog | ||
| import tempfile | ||
| from collections import defaultdict | ||
| from swsscommon.swsscommon import ConfigDBConnector | ||
| from .gu_common import log_error, log_debug, log_info | ||
|
|
||
|
|
||
| def get_config_db(): | ||
| config_db = ConfigDBConnector() | ||
| config_db.connect() | ||
| return config_db | ||
|
|
||
|
|
||
| def set_config(config_db, tbl, key, data): | ||
| config_db.set_entry(tbl, key, data) | ||
|
|
||
|
|
||
| UPDATER_CONF_FILE = "/etc/sonic/generic_config_updater.conf" | ||
| updater_data = None | ||
|
|
||
| class ChangeApplier: | ||
| def __init__(self): | ||
| global updater_data, log_level | ||
|
|
||
| self.config_db = get_config_db() | ||
| if updater_data == None: | ||
renukamanavalan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| with open(UPDATER_CONF_FILE, "r") as s: | ||
| updater_data = json.load(s) | ||
|
|
||
|
|
||
| def _invoke_cmd(cmd, old_cfg, upd_cfg, keys): | ||
renukamanavalan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| method_name = cmd.split(".")[-1] | ||
| module_name = ".".join(cmd.split(".")[0:-1]) | ||
|
|
||
| module = importlib.import_module(module_name, package=None) | ||
| method_to_call = getattr(module, method_name) | ||
|
|
||
| return method_to_call(old_cfg, upd_cfg, keys) | ||
|
|
||
|
|
||
| def _services_validate(old_cfg, upd_cfg, keys): | ||
| lst_svcs = set() | ||
| lst_cmds = set() | ||
| if not keys: | ||
| keys[""] = {} | ||
renukamanavalan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| for tbl in keys: | ||
| lst_svcs.update(updater_data.get(tbl, {}).get("services_to_validate", [])) | ||
| for svc in lst_svcs: | ||
| lst_cmds.update(updater_data.get(svc, {}).get("validate_commands", [])) | ||
|
|
||
| for cmd in lst_cmds: | ||
| ret = _invoke_cmd(cmd, old_cfg, upd_cfg, keys) | ||
| if ret: | ||
| return ret | ||
| return 0 | ||
|
|
||
|
|
||
| def _upd_data(self, tbl, run_tbl, upd_tbl, upd_keys): | ||
| for key in set(run_tbl.keys()).union(set(upd_tbl.keys())): | ||
| run_data = run_tbl.get(key, None) | ||
| upd_data = upd_tbl.get(key, None) | ||
|
|
||
| if run_data != upd_data: | ||
| set_config(self.config_db, tbl, key, upd_data) | ||
| upd_keys[tbl][key] = {} | ||
|
|
||
|
|
||
| def apply(self, change): | ||
| run_data = self._get_running_config() | ||
| upd_data = change.apply(copy.deepcopy(run_data)) | ||
| upd_keys = defaultdict(dict) | ||
|
|
||
| for tbl in set(run_data.keys()).union(set(upd_data.keys())): | ||
| self._upd_data(tbl, run_data.get(tbl, {}), | ||
| upd_data.get(tbl, {}), upd_keys) | ||
|
|
||
| ret = _services_validate(run_data, upd_data, upd_keys) | ||
renukamanavalan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if not ret: | ||
| run_data = self._get_running_config() | ||
| if upd_data != run_data: | ||
| report_mismatch(run_data, upd_data) | ||
| ret = -1 | ||
| return ret | ||
|
|
||
|
|
||
| def _get_running_config(self): | ||
| (_, fname) = tempfile.mkstemp(suffix="_changeApplier") | ||
| os.system("sonic-cfggen -d --print-data > {}".format(fname)) | ||
| run_data = {} | ||
| with open(fname, "r") as s: | ||
| run_data = json.load(s) | ||
| if os.path.isfile(fname): | ||
| os.remove(fname) | ||
| return run_data | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
renukamanavalan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| { | ||
| "tables": { | ||
| "": { | ||
| "services_to_validate": [ "system_health" ] | ||
| }, | ||
| "PORT": { | ||
| "services_to_validate": [ "port_service" ] | ||
| } | ||
| }, | ||
| "README": [ | ||
renukamanavalan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 'Validate_commands provides, module & method name as ', | ||
| ' <module name>.<method name>', | ||
| 'NOTE: module name could have "."', | ||
| ' ', | ||
| 'The last element separated by "." is considered as ', | ||
| 'method name', | ||
| '', | ||
| 'e.g. "show.acl.test_acl"', | ||
| '', | ||
| 'Here we load "show.acl" and call "test_acl" method on it.', | ||
| '', | ||
| 'called as:', | ||
| ' <module>.<method>>(<config before change>, ', | ||
| ' <config after change>, <affected keys>)', | ||
| ' config is in JSON format as in config_db.json', | ||
| ' affected_keys in same format, but w/o value', | ||
| ' { "ACL_TABLE": { "SNMP_ACL": {} ... }, ...}', | ||
| ' The affected keys has "added", "updated" & "deleted"', | ||
| '', | ||
| 'Multiple validate commands may be provided.',', | ||
| '', | ||
| 'Note: The commands may be called in any order', | ||
| '' | ||
| ], | ||
| "services": { | ||
| "system_health": { | ||
| "validate_commands": [ ] | ||
| }, | ||
| "port_service": { | ||
| "validate_commands": [ ] | ||
| } | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.