Skip to content

Commit

Permalink
confd keypath is converted from xpath via maapi
Browse files Browse the repository at this point in the history
Signed-off-by: Michal Novak <[email protected]>
  • Loading branch information
micnovak committed Sep 21, 2023
1 parent fd942dd commit 626a416
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions src/confd_gnmi_api_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 626a416

Please sign in to comment.