robshakir / pyangbind

A plugin for pyang that creates Python bindings for a YANG model.
Other
203 stars 121 forks source link

How to filter by leaf? #332

Closed Dulin0411 closed 9 months ago

Dulin0411 commented 9 months ago

Hi,

I want to user pyangbind to get leaf node, but I don't know how to realize it?

use the fllowing code can realize filter by contrainer: interfaces = ietf_interfaces().interfaces filter = pybindIETFXMLEncoder.serialise(interfaces)

But same way don't take effect on leaf node.

Thanks

JoseIgnacioTamayo commented 9 months ago

I suggest you check the documentation and examples. The Issues are intended to report bugs and missing features. How to use the library can be found in the documentation and examples, and also online searching for code that uses pyangbind.

interfaces, like next_hops in https://github.com/robshakir/pyangbind/blob/master/README.md, is an ordered dict of elements. You need to add an element, which later can be retrieved by index.

interfaces = binding.ietf_interfaces().interfaces
type(interfaces)
         <class 'pyangbind.lib.yangtypes.YANGDynClass.<locals>.YANGBaseClass'>
help(interfaces)
 ...
interfaces.interface.add(name="lo0")
interfaces.interface.add(name="lo1")

print(interfaces.interface["lo0"])
         OrderedDict([('name', <property object at 0x7c0303ced2c0>), ('description', <property object at 0x7c0303ced270>), ('type', <property object at 0x7c030384d270>), ('enabled', <property object at 0x7c030384d180>), ('link_up_down_trap_enable', <property object at 0x7c030384d2c0>)])

pybindIETFXMLEncoder.serialise(interfaces.interface["lo1"])
         '<interface xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces">\n  <name>lo1</name>\n</interface>\n'
Dulin0411 commented 9 months ago

Sorry, may be I didn't describe clearly. There is difference between filter by leaf node and filter by leaf value(as you writed).

There is an example for filter by leaf node, Can get all interfaces name: `

`

JoseIgnacioTamayo commented 9 months ago

Is your intention to write the NETCONF Filter expression, in XML, using pyangbind?

https://www.juniper.net/documentation/us/en/software/junos/netconf/topics/task/netconf-requesting-configuration-information-object-single.html

I wonder if your question has to do with NETCONF, and not with pyanbind.

I would suggest to write the filter you need in XML and attempt to reproduce it with pyangbind.... but maybe it is also possible to create the XML filter using https://docs.python.org/3/library/xml.etree.elementtree.html#module-xml.etree.ElementTree. You can define the XML structure as per the filter you need, and dump it in a string.

https://docs.python.org/3/library/xml.etree.elementtree.html#building-xml-documents