diff --git a/pyangbind/lib/yangtypes.py b/pyangbind/lib/yangtypes.py index 7e67e27..4966ec7 100644 --- a/pyangbind/lib/yangtypes.py +++ b/pyangbind/lib/yangtypes.py @@ -1445,3 +1445,71 @@ def __str__(self, encoding="ascii", errors="replace"): return " ".join(sorted(self, key=sort_key)) return YANGBits +def YANGInstanceIdentifier(*args, **kwargs): + + path_helper = kwargs.pop("path_helper", None) + caller = kwargs.pop("caller", False) + require_instance = kwargs.pop("require_instance", False) + + class YANGInstanceIdentifier: + + __slots__ = ( + "_path_helper", + "_caller", + "_referenced_object", + "_ptr", + "_require_instance", + "_type", + "_utype", + ) + + _pybind_generated_by = "YANGInstanceIdentifier" + + def __init__(self, *args, **kwargs): + self._referenced_path = ref_path + self._path_helper = path_helper + self._caller = caller + self._require_instance = require_instance + + if len(args): + value = args[0] + if hasattr(self, "_set"): + self._set() + else: + value = None + + if self._path_helper and value is not None: + path_chk = self._path_helper.get(value, caller=self._caller) + + # THis lookup must return an instance object. If it does not, then we should atleast + # check for the leaf object. If this is a leaf object then allow setting the value else don't. + if self._require_instance: + + if len(path_chk) < 1: + + raise ValueError("If require-instance is set to true, the xpath needs to exist. In other words + it must exist.") + + self.set(value=value) + + + def _get_ptr(self): + if self._ptr: + ptr = self._path_helper.get(self.value, caller=self._caller) + if len(ptr) == 1: + return ptr[0] + raise ValueError("Invalid pointer specified") + + + def _get(self): + return self.value + + def __str__(self): + return str(self.value) + + + def set(self, value): + self.value = value + + + return type(YANGInstanceIdentifier(*args, **kwargs)) diff --git a/pyangbind/plugin/pybind.py b/pyangbind/plugin/pybind.py index f4606e7..de3217d 100644 --- a/pyangbind/plugin/pybind.py +++ b/pyangbind/plugin/pybind.py @@ -35,6 +35,8 @@ import pyangbind.helpers.misc as misc_help from pyangbind.helpers.identity import IdentityStore from pyangbind.lib.yangtypes import RestrictedClassType, YANGBool, safe_name, YANGBinary, YANGBitsType +from pyangbind.lib.yangtypes import (RestrictedClassType, YANGBinary, YANGBitsType, YANGBool, YANGInstanceIdentifier, + safe_name) long = int @@ -177,6 +179,12 @@ base_type=long, restriction_dict={"range": ["-9223372036854775808..9223372036854775807"]}, int_size=64 ), }, + "instance-identifier": { + "native_type": "YANGInstanceIdentifier", + "base_type": True, + "quote_arg": True, + "pytype": YANGInstanceIdentifier + } } # We have a set of types which support "range" statements in RFC6020. This @@ -320,6 +328,7 @@ def build_pybind(ctx, modules, fd): "ReferenceType", "YANGBinary", "YANGBitsType", + "YANGInstanceIdentifier" ] for library in yangtypes_imports: ctx.pybind_common_hdr += "from pyangbind.lib.yangtypes import {}\n".format(library)