robshakir / pyangbind

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

When we generate python code from Yang module, missing attributes in class #334

Closed Baiyu-cmd closed 6 months ago

Baiyu-cmd commented 6 months ago

I follow the steps of the project completely. When I run the "docs/example/oc-local-routing/generate_bindings.sh" command, there is no corresponding attribute in the binding file's class.The following is the binding document content produced by me.

# -*- coding: utf-8 -*-

from operator import attrgetter
from pyangbind.lib.yangtypes import RestrictedPrecisionDecimalType
from pyangbind.lib.yangtypes import RestrictedClassType
from pyangbind.lib.yangtypes import TypedListType
from pyangbind.lib.yangtypes import YANGBool
from pyangbind.lib.yangtypes import YANGListType
from pyangbind.lib.yangtypes import YANGDynClass
from pyangbind.lib.yangtypes import ReferenceType
from pyangbind.lib.yangtypes import YANGBinary
from pyangbind.lib.yangtypes import YANGBitsType
from pyangbind.lib.base import PybindBase
from collections import OrderedDict
from decimal import Decimal
import six

# PY3 support of some PY2 keywords (needs improved)
if six.PY3:
  import builtins as __builtin__
  long = int
elif six.PY2:
  import __builtin__

class openconfig_local_routing(PybindBase):
  """
  This class was auto-generated by the PythonClass plugin for PYANG
  from YANG module openconfig-local-routing - based on the path /openconfig-local-routing. Each member element of
  the container is represented as a class variable - with a specific
  YANG type.

  YANG Description: This module describes configuration and operational state data
for routes that are locally generated, i.e., not created by
dynamic routing protocols.  These include static routes, locally
created aggregate routes for reducing the number of constituent
routes that must be advertised, summary routes for IGPs, etc.

This model expresses locally generated routes as generically as
possible, avoiding configuration of protocol-specific attributes
at the time of route creation.  This is primarily to avoid
assumptions about how underlying router implementations handle
route attributes in various routing table data structures they
maintain.  Hence, the definition of locally generated routes
essentially creates 'bare' routes that do not have any protocol-
specific attributes.

When protocol-specific attributes must be attached to a route
(e.g., communities on a locally defined route meant to be
advertised via BGP), the attributes should be attached via a
protocol-specific policy after importing the route into the
protocol for distribution (again via routing policy).
  """
  _pyangbind_elements = {}

I assumed that it was legal to continue the operation and found that subsequent steps showed that the file was missing corresponding attributes at that location. I don't know if there is anything wrong with my previous steps, if you need to post it.

xavier-contreras commented 6 months ago

I just verified I am running into this as well, to get to the point of generating a binding file I also had to add some commands to the shell script in case someone can take a look:

curl https://raw.githubusercontent.com/openconfig/public/master/release/models/bfd/openconfig-bfd.yang -o $SDIR/yang/openconfig-bfd.yang
curl https://raw.githubusercontent.com/openconfig/public/master/release/models/optical-transport/openconfig-transport-types.yang -o $SDIR/yang/openconfig-transport-types.yang
curl https://raw.githubusercontent.com/openconfig/public/master/release/models/platform/openconfig-platform-types.yang -o $SDIR/yang/openconfig-platform-types.yang

I tried to generate other bindings for different models (interfaces & wifi/access-points) and those worked fine.

robshakir commented 6 months ago

I think this was because local-routing in OpenConfig is now within network instances.

r.

On Thu, 21 Dec 2023 at 14:52, xavier-contreras @.***> wrote:

I just verified I am running into this as well, to get to the point of generating a binding file I also had to add some commands to the shell script in case someone can take a look:

curl https://raw.githubusercontent.com/openconfig/public/master/release/models/bfd/openconfig-bfd.yang -o $SDIR/yang/openconfig-bfd.yang curl https://raw.githubusercontent.com/openconfig/public/master/release/models/optical-transport/openconfig-transport-types.yang -o $SDIR/yang/openconfig-transport-types.yang curl https://raw.githubusercontent.com/openconfig/public/master/release/models/platform/openconfig-platform-types.yang -o $SDIR/yang/openconfig-platform-types.yang

I tried to generate other bindings for different models (interfaces & wifi/access-points) and those worked fine.

— Reply to this email directly, view it on GitHub https://github.com/robshakir/pyangbind/issues/334#issuecomment-1866975657, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABE43VJN3MCLLTRH3OCMJK3YKSVRBAVCNFSM6AAAAABA55DCGGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQNRWHE3TKNRVG4 . You are receiving this because you are subscribed to this thread.Message ID: @.***>

Baiyu-cmd commented 6 months ago

I tried to generate other bindings for different models (interfaces & wifi/access-points) and those worked fine.

Glad to receive your reply. Also sorry, I found the three files missing while running the instance, so I found the source address and added the three files myself, still not generating the correct binding. I also tried using tests/base-test.yang (which is the file used in the previous example steps) to generate binding.py. It is also used to generate xml files, but the generated xml file content is only declarations.

Baiyu-cmd commented 6 months ago

In order to better solve the problem, I have posted my operations on the oc_local_routing.yang and base_test.yang files in two attachments, I wish I could use pyangding in ncclient. base_test.txt oc_local_routing.txt

xavier-contreras commented 6 months ago

Thanks @robshakir for the pointer , that does seem to be the case here.

@Baiyu-cmd , the README will need quite a bit of updating since models moved around, but to unblock you, you should be able to do something like this.

$ git clone https://github.com/openconfig/public
$ export PYBINDPLUGIN=`/usr/bin/env python3 -c \
> 'import pyangbind; import os; print ("{}/plugin".format(os.path.dirname(pyangbind.__file__)))'`
$ pyang --plugindir $PYBINDPLUGIN -f pybind -o routingbinding.py public/release/models/network-instance/openconfig-network-instance.yang -p ./public/release/models/
$ python3
>>> from routingbinding import openconfig_network_instance
>>> oclr = openconfig_network_instance()
>>> oclr.network_instances.network_instance.add('a')
>>> oclr.network_instances.network_instance['a'].protocols
>>> oclr.network_instances.network_instance['a'].protocols.protocol.add(identifier='STATIC', name='DEFAULT')
>>> rt = oclr.network_instances.network_instance['a'].protocols.protocol['STATIC DEFAULT'].static_routes.static.add("192.0.2.1/32")
>>> rt.config.set_tag = 42
>>> print(rt.config.set_tag)
42

local-routes was removed a while back, so keep that in mind when reading the documentation for now: https://github.com/openconfig/public/blob/master/release/models/local-routing/openconfig-local-routing.yang#L56C36-L56C42

Baiyu-cmd commented 6 months ago

Thanks @robshakir for the pointer , that does seem to be the case here.

@Baiyu-cmd , the README will need quite a bit of updating since models moved around, but to unblock you, you should be able to do something like this.

$ git clone https://github.com/openconfig/public
$ export PYBINDPLUGIN=`/usr/bin/env python3 -c \
> 'import pyangbind; import os; print ("{}/plugin".format(os.path.dirname(pyangbind.__file__)))'`
$ pyang --plugindir $PYBINDPLUGIN -f pybind -o routingbinding.py public/release/models/network-instance/openconfig-network-instance.yang -p ./public/release/models/
$ python3
>>> from routingbinding import openconfig_network_instance
>>> oclr = openconfig_network_instance()
>>> oclr.network_instances.network_instance.add('a')
>>> oclr.network_instances.network_instance['a'].protocols
>>> oclr.network_instances.network_instance['a'].protocols.protocol.add(identifier='STATIC', name='DEFAULT')
>>> rt = oclr.network_instances.network_instance['a'].protocols.protocol['STATIC DEFAULT'].static_routes.static.add("192.0.2.1/32")
>>> rt.config.set_tag = 42
>>> print(rt.config.set_tag)
42

local-routes was removed a while back, so keep that in mind when reading the documentation for now: https://github.com/openconfig/public/blob/master/release/models/local-routing/openconfig-local-routing.yang#L56C36-L56C42

@xavier-contreras Thanks again for your reply, I am very happy to see the implementation of the function, although I have tested some other yang files and there is still a problem of stably generating usable.py files, but this makes me see some possibilities and feel happy for this wonderful code. Am I correct in understanding that this 'local-routes' refers to the instance now used in the README?