robshakir / pyangbind

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

question: adding attributes to edit-config - eg. operation="replace" #349

Open hyberdk opened 2 weeks ago

hyberdk commented 2 weeks ago

Im trying out pyangbind, but there are something that I cannot wrap my head around.

I would like to define the attribute operation="replace" as defined in https://datatracker.ietf.org/doc/html/rfc6241#section-7.2

what I want is xml that looks something like this:

<native xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native">
    <ip>
        <access-list>
            <extended xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-acl" operation="replace" >
                <name>TEST_TEST</name>
                <access-list-seq-rule>
                    <sequence>10</sequence>
                    <ace-rule>
                        <action>permit</action>
                        <protocol>tcp</protocol>
                        <any/>
                        <dst-any/>
                    </ace-rule>
                </access-list-seq-rule>
                <access-list-seq-rule>
                    <sequence>20</sequence>
                    <ace-rule>
                        <action>permit</action>
                        <protocol>udp</protocol>
                        <any/>
                        <dst-any/>
                    </ace-rule>
                </access-list-seq-rule>
                <access-list-seq-rule>
                    <sequence>30</sequence>
                    <ace-rule>
                        <action>deny</action>
                        <protocol>ospf</protocol>
                        <any/>
                        <dst-any/>
                    </ace-rule>
                </access-list-seq-rule>
            </extended>
        </access-list>
    </ip>
</native>

but I cannot for the heck of it, figure out how to do it.. I have been playing around with YDK-GEN and there you can set a "YFilter" property that is reflected in the xml output

See this: https://ygorelik.github.io/ydk-gen/api/filters.html#ydk.filters.YFilter

is there a similar thing I can set in pyangbind?

Please RFTM me, I cannot find it ;-)

JoseIgnacioTamayo commented 2 weeks ago

A few clarification questions:

I do not have extensive experience with NETCONF or XML, but I will try my best to help.

hyberdk commented 2 weeks ago

Hey @JoseIgnacioTamayo,

thanks a lot for your reply. Yes that is the model I am using (although a slightly newer version). For the access-lists, this model augments the native model with acls: https://github.com/YangModels/yang/blob/main/vendor/cisco/xe/1671/Cisco-IOS-XE-acl.yang

Once you get and use your Python classes, you want to use the generated classes to create a Python object that, when serialized in XML, produces the output you desire, is this correct?

correct

What is the Python code you have tried?

I haven't gone that far yet. I was trying to read up on the docs, and I am pretty sure I can do the python code and generate both the XML and JSON representation of the model. which is fine.. essentially when I send it to the device, I need to wrap it in netconf edit-config tags..

something like

<rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="101">
  <edit-config>
    <target>
      <running/>
    </target>
    <config>
      <!-- yang data here. -->
    </config>
  </edit-config>
</rpc>

I dont think pyangbind can do the entire request for me, however it would be nice if it could. do you know?

The problem is that the "default" edit-config is a "merge", where it merges the new config into the existing.. this is where the operation="replace" attribute comes in, as it instructs the device in not merging the new config, but replace it entirely (removing any additional config)..

ydk-gen can do the entire process start-to-end, including setting the operation attribute, but to be honest ydk-gen is poorly maintained, so I was hoping that pyandbind would be able to do something similar for me..

I hope it makes sense..

Esben

JoseIgnacioTamayo commented 1 week ago

Upon reading a bit, I think pyangbind cannot help you here.

The Operation setting, within a block, is part of the NETCONF definition.

pyangbind works with the Cisco YANG Model, in which there is no notion of 'operation'. The Cisco-IOS-XE-acl.yang says that there is a container 'extended' keyed by 'name'.

What you could do is to use the pyangbind-generated Python classes to serialize the XML payload you need, and use something like python xml.etree to inject the attribute 'operation=replace' at the right part of the XML tree, and wrap it all in a NetConf XML operation.

Hope that helps.