diff options
Diffstat (limited to 'python')
| -rw-r--r-- | python/vyos/configtree.py | 46 | ||||
| -rw-r--r-- | python/vyos/defaults.py | 2 | ||||
| -rw-r--r-- | python/vyos/referencetree.py | 81 |
3 files changed, 120 insertions, 9 deletions
diff --git a/python/vyos/configtree.py b/python/vyos/configtree.py index e57cd5205..afe1d7fe4 100644 --- a/python/vyos/configtree.py +++ b/python/vyos/configtree.py @@ -188,6 +188,17 @@ class ConfigTree(object): self.__equal.argtypes = [c_void_p, c_void_p] self.__equal.restype = c_bool + self.__config_dict = self.__lib.config_dict + self.__config_dict.argtypes = [ + c_void_p, + c_void_p, + c_void_p, + c_char_p, + c_bool, + c_bool, + ] + self.__config_dict.restype = c_char_p + if address is not None: self.__config = address self.__version = '' @@ -224,20 +235,20 @@ class ConfigTree(object): def __eq__(self, other): if isinstance(other, ConfigTree): - return self.__equal(self._get_config(), other._get_config()) + return self.__equal(self.get_tree(), other.get_tree()) return False def __str__(self): return self.to_string() - def _get_config(self): + def get_tree(self): return self.__config def get_version_string(self): return self.__version def write_cache(self, file_name): - self.__write_internal(self._get_config(), file_name.encode()) + self.__write_internal(self.get_tree(), file_name.encode()) def to_string(self, ordered_values=False, no_version=False): config_string = self.__to_string(self.__config, ordered_values).decode() @@ -472,6 +483,23 @@ class ConfigTree(object): subt = ConfigTree(address=res) return subt + def config_dict( + self, ref_tree, path, mask, get_first_key=False, with_defaults=False + ): + check_path(path) + path_str = ' '.join(map(str, path)).encode() + + res_json = self.__config_dict( + self.__config, + ref_tree.get_tree(), + mask.get_tree(), + path_str, + get_first_key, + with_defaults, + ).decode() + res = json.loads(res_json) + return res + def diff_compare(left, right, path=[], commands=False, libpath=LIBPATH): if left is None: @@ -495,7 +523,7 @@ def diff_compare(left, right, path=[], commands=False, libpath=LIBPATH): __get_error.argtypes = [] __get_error.restype = c_char_p - res = __diff_compare(commands, path_str, left._get_config(), right._get_config()) + res = __diff_compare(commands, path_str, left.get_tree(), right.get_tree()) res = res.decode() if res == '#1@': msg = __get_error().decode() @@ -521,7 +549,7 @@ def union(left, right, libpath=LIBPATH): __get_error.argtypes = [] __get_error.restype = c_char_p - res = __tree_union(left._get_config(), right._get_config()) + res = __tree_union(left.get_tree(), right.get_tree()) tree = ConfigTree(address=res) return tree @@ -543,7 +571,7 @@ def merge(left, right, destructive=False, libpath=LIBPATH): __get_error.argtypes = [] __get_error.restype = c_char_p - res = __tree_merge(destructive, left._get_config(), right._get_config()) + res = __tree_merge(destructive, left.get_tree(), right.get_tree()) tree = ConfigTree(address=res) return tree @@ -562,7 +590,7 @@ def mask_inclusive(left, right, libpath=LIBPATH): __get_error.argtypes = [] __get_error.restype = c_char_p - res = __mask_tree(left._get_config(), right._get_config()) + res = __mask_tree(left.get_tree(), right.get_tree()) except Exception as e: raise ConfigTreeError(e) if not res: @@ -657,7 +685,7 @@ def validate_tree_filter( __get_error.argtypes = [] __get_error.restype = c_char_p res = __validate_tree_filter( - config_tree._get_config(), cache_path.encode(), validator_dir.encode() + config_tree.get_tree(), cache_path.encode(), validator_dir.encode() ) except Exception as e: raise ConfigTreeError(e) @@ -706,7 +734,7 @@ class DiffTree: check_path(path) path_str = ' '.join(map(str, path)).encode() - res = self.__diff_tree(path_str, left._get_config(), right._get_config()) + res = self.__diff_tree(path_str, left.get_tree(), right.get_tree()) # full diff config_tree and python dict representation self.full = ConfigTree(address=res) diff --git a/python/vyos/defaults.py b/python/vyos/defaults.py index 833ac1fbf..fbaf18919 100644 --- a/python/vyos/defaults.py +++ b/python/vyos/defaults.py @@ -99,3 +99,5 @@ SSH_DSA_DEPRECATION_WARNING: str = \ 'Support for SSH-DSA keys is deprecated and will be removed in VyOS 1.6. ' \ 'Please update affected keys to a supported algorithm (e.g., RSA, ECDSA or ' \ 'ED25519) to avoid authentication failures after the upgrade.' + +reference_tree_cache = '/usr/share/vyos/reftree.cache' diff --git a/python/vyos/referencetree.py b/python/vyos/referencetree.py new file mode 100644 index 000000000..87ed310b4 --- /dev/null +++ b/python/vyos/referencetree.py @@ -0,0 +1,81 @@ +# Copyright VyOS maintainers and contributors <maintainers@vyos.io> +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this library. If not, see <http://www.gnu.org/licenses/>. + +from ctypes import cdll, c_char_p, c_void_p, c_bool + +from vyos.defaults import reference_tree_cache + +LIBPATH = '/usr/lib/libvyosconfig.so.0' + + +class ReferenceTreeError(Exception): + pass + + +class ReferenceTree: + # pylint: disable=too-many-instance-attributes + def __init__(self, cache_file=reference_tree_cache, libpath=LIBPATH): + self.__pointer = None + self.__lib = cdll.LoadLibrary(libpath) + + # Import functions + self.__get_error = self.__lib.get_error + self.__get_error.argtypes = [] + self.__get_error.restype = c_char_p + + self.__read_internal = self.__lib.read_internal_reference_tree + self.__read_internal.argtypes = [c_char_p] + self.__read_internal.restype = c_void_p + + self.__write_internal = self.__lib.write_internal_reference_tree + self.__write_internal.argtypes = [c_void_p, c_char_p] + + self.__to_json = self.__lib.to_json_reference_tree + self.__to_json.argtypes = [c_void_p] + self.__to_json.restype = c_char_p + + self.__destroy = self.__lib.destroy + self.__destroy.argtypes = [c_void_p] + + self.__equal = self.__lib.equal + self.__equal.argtypes = [c_void_p, c_void_p] + self.__equal.restype = c_bool + + pointer = self.__read_internal(cache_file.encode()) + if pointer is None: + msg = self.__get_error().decode() + raise ValueError(f'Failed to read internal rep: {msg}') + self.__pointer = pointer + + def __del__(self): + if self.__pointer is not None: + self.__destroy(self.__pointer) + + def __eq__(self, other): + if isinstance(other, ReferenceTree): + return self.__equal(self.get_tree(), other.get_tree()) + return False + + def __str__(self): + return self.to_json() + + def get_tree(self): + return self.__pointer + + def write_cache(self, file_name): + self.__write_internal(self.get_tree(), file_name.encode()) + + def to_json(self): + return self.__to_json(self.__pointer).decode() |
