aristanetworks / openmgmt

Documentation and examples for using open network management tools such as OpenConfig
https://aristanetworks.github.io/openmgmt/
Apache License 2.0
70 stars 32 forks source link

update pyangbind example to use the new OC BGP structure (i.e under network-instance) #142

Closed ksator closed 1 year ago

ksator commented 1 year ago

we need to update: https://github.com/aristanetworks/openmgmt/blob/main/src/pyangbind/pyangbind_demo.py https://github.com/aristanetworks/openmgmt/tree/main/docs/examples/pyangbind

Here's an example, inspired by https://github.com/arista-netdevops-community/openconfig-peering/blob/main/oc-peering.py#L126

pip install pyang
pip install pyangbind
git clone https://github.com/openconfig/public.git
mkdir yang_modules
cp public/release/models/*.yang yang_modules/.
cp -R public/release/models/*/*.yang yang_modules/.
cp public/third_party/ietf/*.yang yang_modules/.
cd yang_modules
pyang --plugindir /usr/local/lib/python3.9/site-packages/pyangbind/plugin/ -f pybind -o ni.py openconfig-network-instance.yang 
name="qwerty.net"
asn="12345"
ipaddr4 = "192.168.1.2"
from ni import openconfig_network_instance
import pyangbind.lib.pybindJSON as pybindJSON
oc = openconfig_network_instance()
oc.network_instances.network_instance.add('default')
oc.network_instances.network_instance['default'].protocols.protocol.add(identifier='BGP', name='BGP')
oc.network_instances.network_instance['default'].protocols.protocol['BGP' + ' BGP'].bgp.neighbors.neighbor.add(ipaddr4)
oc.network_instances.network_instance['default'].protocols.protocol['BGP' + ' BGP'].bgp.neighbors.neighbor[ipaddr4].config.neighbor_address=ipaddr4
oc.network_instances.network_instance['default'].protocols.protocol['BGP' + ' BGP'].bgp.neighbors.neighbor[ipaddr4].config.enabled=True
oc.network_instances.network_instance['default'].protocols.protocol['BGP' + ' BGP'].bgp.neighbors.neighbor[ipaddr4].config.peer_as=asn
oc.network_instances.network_instance['default'].protocols.protocol['BGP' + ' BGP'].bgp.neighbors.neighbor[ipaddr4].config.description=name
oc.network_instances.network_instance['default'].protocols.protocol['BGP' + ' BGP'].bgp.neighbors.neighbor[ipaddr4].afi_safis.afi_safi.add('IPV4_UNICAST')
oc.network_instances.network_instance['default'].protocols.protocol['BGP' + ' BGP'].bgp.neighbors.neighbor[ipaddr4].afi_safis.afi_safi['IPV4_UNICAST'].config.afi_safi_name='IPV4_UNICAST'
oc.network_instances.network_instance['default'].protocols.protocol['BGP' + ' BGP'].bgp.neighbors.neighbor[ipaddr4].afi_safis.afi_safi['IPV4_UNICAST'].config.enabled=True
with open("bgp.json", "w") as f:
    f.write(pybindJSON.dumps(oc.network_instances, mode="ietf"))
more bgp.json                                                                    
{
    "openconfig-network-instance:network-instance": [
        {
            "name": "default",
            "protocols": {
                "protocol": [
                    {
                        "identifier": "BGP",
                        "name": "BGP",
                        "bgp": {
                            "neighbors": {
                                "neighbor": [
                                    {
                                        "neighbor-address": "192.168.1.2",
                                        "config": {
                                            "neighbor-address": "192.168.1.2",
                                            "peer-as": 12345,
                                            "description": "qwerty.net"
                                        },
                                        "afi-safis": {
                                            "afi-safi": [
                                                {
                                                    "afi-safi-name": "IPV4_UNICAST",
                                                    "config": {
                                                        "afi-safi-name": "openconfig-bgp-types:IPV4_UNICAST",
                                                        "enabled": true
                                                    }
                                                }
                                            ]
                                        }
                                    }
                                ]
                            }
                        }
                    }
                ]
            }
        }
    ]
}
lyricjixh commented 1 year ago

Tried a quick run to generate the gnmi config json, but keep getting the following typeerror for reraise() function call. pretty much having followed the exact guidance, Any idea how to fix and proceed? it's for preparing a quick gnmi demo.

"TypeError: reraise() missing 2 required positional arguments: 'tp' and 'value'"

xji@vmxji:~/devops/yang/consolidated$ pip3 list | grep pyang pyang 2.5.3 pyangbind 0.8.1 xji@vmxji:~/devops/yang/consolidated$ python3 -V Python 3.10.6 ... File "/home/xji/.local/lib/python3.10/site-packages/pyangbind/lib/yangtypes.py", line 1160, in YANGDynClass return YANGBaseClass(*args, **kwargs) File "/home/xji/.local/lib/python3.10/site-packages/pyangbind/lib/yangtypes.py", line 1037, in init six.reraise() TypeError: reraise() missing 2 required positional arguments: 'tp' and 'value'

prep_yang.sh script

!/bin/bash

rm -Rf yang mkdir -p yang/consolidated git clone https://github.com/openconfig/public.git yang/openconfig git clone https://github.com/aristanetworks/yang.git yang/arista cp yang/openconfig/release/models/.yang yang/consolidated/ cp -R yang/openconfig/release/models//.yang yang/consolidated/ cp yang/openconfig/third_party/ietf/.yang yang/consolidated/ for i in find yang/arista/EOS-4.28.2F/ -name 'arista*.yang'; do cp $i yang/consolidated; done

export PYBINDPLUGIN=/usr/bin/env python -c \ 'import pyangbind; import os; print ("{}/plugin".format(os.path.dirname(pyangbind.__file__)))' echo $PYBINDPLUGIN

end of script

cd yang/consolidated pyang --plugindir $PYBINDPLUGIN -f pybind -o ./ni.py openconfig-network-instance.yang

github-actions[bot] commented 1 year ago

This issue has been marked stale given 180 days of inactivity. It will be automatically closed in 14 days if there are no updates.

github-actions[bot] commented 1 year ago

This issue has been closed due to prolonged inactivity.