09-07-2020 10:15 AM
Hello,
as per subject, I'd like to know what's the default value of a leaf as per YANG definition. In example:
leaf my-leaf { type my-cfg:my-leaf-type; default "this-is-the-default-value"; description "whatever"; }
I'd like to recover "this-is-the-default-value" string using the python APIs. I expected something like:
My-Model.my_cfg.my_leaf.get_default_value()
Thanks for the help.
Val
Solved! Go to Solution.
09-08-2020 07:09 AM
The leaf default values are available in the underlying Libyang library, but not exposed to YDK classes and API. New development is needed to add this feature. At this point I don't see any workaround and suggest you to open enhancement request in ydk-gen GitHub.
09-07-2020 04:08 PM
You can refer to YDK python API document.
09-07-2020 06:14 PM
module.container.list.leaf
09-08-2020 01:20 AM - edited 09-08-2020 01:23 AM
Hello thanks for the reply, but I can't find anything useful.
In the autogenerated code I can see something like:
.. attribute:: broadcast_ssid
broadcast SSID on a WLAN
**type**\: bool
**default value**\: true
But I need something to recover the default value programmatically.
09-08-2020 07:09 AM
The leaf default values are available in the underlying Libyang library, but not exposed to YDK classes and API. New development is needed to add this feature. At this point I don't see any workaround and suggest you to open enhancement request in ydk-gen GitHub.
09-08-2020 07:55 AM - edited 09-08-2020 08:01 AM
Hi Yan,
I'll create the request on GitHub. In the meantime I'm using the following workaround:
def get_yang_default_value(yang_def, name): doc = re.sub(r'^[\s\S]*?\.\. attribute::', '.. attribute::', yang_def.__doc__) if doc is not None: for part in doc.split(".. attribute:: "): lines = part.splitlines() if len(lines) > 0 and lines[0].replace(" (key)", "") == name: for line in lines: if "**default value**\: " in line: matches = re.match(r'[\s\S]*?\*\*default value\*\*\\: (.*)', line) if matches is not None and len(matches.groups()) == 1: return matches.group(1) return None raise ValueError(name) raise ValueError(yang_def)
default_value = get_yang_default_value(Cisco_IOS_XE_wireless_wlan_cfg.WlanCfgData.WlanCfgEntries.WlanCfgEntry, "wep_key_format")
Extra logic is needed to also cast to the right type.
09-09-2020 03:37 PM
This approach maybe good for your specific task, but it is not acceptable from the YDK as generic. The reason - the node name can appear multiple times in different branches of the model tree. Simple examples are nodes with name "state", "config", "data" etc. There is no need to "invent wheels". We just have to employ currently available and stable libyang API and expose it to YDK API.
09-10-2020 12:04 AM
Sure, I totally agree with you. I shared the code in case someone gets blocked because of this and needs a quick workaround until we get YDK supporting default values. I was thinking to look into YDK/pyang to propose a proper PR for this purpose, but I'm getting busy on other things so no promise.
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide