cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1503
Views
0
Helpful
7
Replies

How do I get default value for a leaf?

vadigreg
Cisco Employee
Cisco Employee

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

1 Accepted Solution

Accepted Solutions

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.

Yan Gorelik
YDK Solutions

View solution in original post

7 Replies 7

You can refer to YDK python API document.

Jacky Zhang
Global Telecom

module.container.list.leaf

Jacky Zhang
Global Telecom

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.

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.

Yan Gorelik
YDK Solutions

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)
this you can call like:
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. 

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.

Yan Gorelik
YDK Solutions

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.

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: