From 626a4161cf9bea45aad7e895c8f74eb4882bffae Mon Sep 17 00:00:00 2001 From: Michal Novak Date: Thu, 21 Sep 2023 13:28:15 +0200 Subject: [PATCH] confd keypath is converted from xpath via maapi Signed-off-by: Michal Novak --- src/confd_gnmi_api_adapter.py | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/src/confd_gnmi_api_adapter.py b/src/confd_gnmi_api_adapter.py index c0ec11e..368b2f6 100644 --- a/src/confd_gnmi_api_adapter.py +++ b/src/confd_gnmi_api_adapter.py @@ -137,10 +137,8 @@ def get_sample(self, path, prefix, allow_aggregation=False, start_change_processing=False): log.debug("==>") pathstr = make_xpath_path(path, prefix, quote_val=True) - confd_path = make_formatted_path(path, prefix, quote_val=True) datatype = gnmi_pb2.GetRequest.DataType.ALL updates = self.adapter.get_updates_with_maapi(pathstr, datatype, - confd_path=confd_path, allow_aggregation=allow_aggregation) sample = [gnmi_pb2.Update(path=remove_path_prefix(u.path, prefix), val=u.val) @@ -499,12 +497,12 @@ def make_gnmi_json_value(self, value, csnode): gnmi_value = gnmi_pb2.TypedValue(json_ietf_val=json.dumps(json_value).encode()) return gnmi_value - def append_update(self, tr, path_str, csnode=None): + def append_update(self, tr, keypath, csnode=None): if csnode is None: - csnode = _tm.cs_node_cd(None, path_str) + csnode = _tm.cs_node_cd(None, keypath) def append_update_inner(): - if tr.exists(path_str): + if tr.exists(keypath): gnmi_value = None if csnode.is_empty_leaf(): # See https://datatracker.ietf.org/doc/html/rfc7951#section-6.9 @@ -514,10 +512,10 @@ def append_update_inner(): gnmi_value = gnmi_pb2.TypedValue( json_ietf_val=json.dumps({}).encode()) else: - elem = tr.get_elem(path_str) + elem = tr.get_elem(keypath) gnmi_value = self.make_gnmi_json_value(elem, csnode) if gnmi_value is not None: - tr.pushd(path_str) + tr.pushd(keypath) kp = tr.getcwd_kpath() tr.popd() gnmi_path = self.make_gnmi_keypath(kp, csnode) @@ -547,15 +545,15 @@ def make_updates_with_maagic_rec(self, tr, node): for n in children: yield from self.make_updates_with_maagic_rec(tr, n) - def make_updates_with_maagic(self, tr, path_str): - node = maagic.get_node(tr, path_str) + def make_updates_with_maagic(self, tr, keypath): + node = maagic.get_node(tr, keypath) if not isinstance(node, maagic.Node): - yield from self.append_update(tr, path_str) + yield from self.append_update(tr, keypath) else: yield from self.make_updates_with_maagic_rec(tr, node) - def get_updates(self, trans, path_str, confd_path, save_flags, allow_aggregation=False): + def get_updates(self, trans, path_str, save_flags, allow_aggregation=False): log.debug("==> path_str=%s", path_str) tagpath = '/' + '/'.join(tag for tag, _ in parse_instance_path(path_str)) log.debug("tagpath=%s", tagpath) @@ -593,7 +591,8 @@ def add_update_json(keypath, _value): else: trans.xpath_eval(path_str, add_update_json, None, '/') else: - updates = list(self.make_updates_with_maagic(trans, confd_path)) + keypath = str(trans.maapi.xpath2kpath(path_str)) + updates = list(self.make_updates_with_maagic(trans, keypath)) log.debug("<== save_str=%s", updates) return updates @@ -604,12 +603,10 @@ def module_to_prefix(match): return self.module_to_pfx.get(name, name) + ':' return re.sub(r'([^/:]+):', module_to_prefix, path) - def get_updates_with_maapi(self, path, data_type, confd_path=None, allow_aggregation=False): + def get_updates_with_maapi(self, path, data_type, allow_aggregation=False): log.debug("==> path=%s data_type=%s", path, data_type) pfx_path = self.fix_path_prefixes(path) - if confd_path is not None: - confd_path = self.fix_path_prefixes(confd_path) save_flags = _tm.maapi.CONFIG_JSON | _tm.maapi.CONFIG_NO_PARENTS \ | _tm.maapi.CONFIG_WITH_DEFAULTS db = _tm.OPERATIONAL @@ -629,7 +626,7 @@ def get_updates_with_maapi(self, path, data_type, confd_path=None, allow_aggrega try: with maapi.single_read_trans(self.username, context, groups, db=db, src_ip=self.addr, src_port=self.port) as t: - updates = self.get_updates(t, pfx_path, confd_path, save_flags, + updates = self.get_updates(t, pfx_path, save_flags, allow_aggregation=allow_aggregation) except ValueError: pass