pyangbind icon indicating copy to clipboard operation
pyangbind copied to clipboard

Error while adding a TypedList to a parent container: ValueError: Cannot add ['TCP', 'TLS'] to TypedList

Open sravanjosh opened this issue 6 years ago • 1 comments

Hi,

I am relatively new to YANG and want to desperately need some python implementation to generate yang to python.

I am getting the following exception when I try to use a valid YANG model after generating the code with pyangbind.

Pasting my YANG definition and my client code at the end.

Exception seen:

Traceback (most recent call last):
  File "/anaconda3/lib/python3.6/site-packages/pyangbind/lib/yangtypes.py", line 1033, in __init__
    super(YANGBaseClass, self).__init__(*args, **kwargs)
  File "/anaconda3/lib/python3.6/site-packages/pyangbind/lib/yangtypes.py", line 391, in __init__
    tmp = self.check(args[0])
  File "/anaconda3/lib/python3.6/site-packages/pyangbind/lib/yangtypes.py", line 441, in check
    (v, self._allowed_type))
ValueError: Cannot add ['TCP', 'TLS'] to TypedList (accepts only [<class 'pyangbind.lib.yangtypes.RestrictedClassType.<locals>.RestrictedClass'>])

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/joshisk/PycharmProjects/tapi-pyang/lib/practice.py", line 94, in _set_supported_protocols
    t = YANGDynClass(v,unique=True, base=TypedListType(allowed_type=RestrictedClassType(base_type=six.text_type,                                     restriction_type="dict_key",                                     restriction_arg={'TCP': {}, 'TLS': {}, 'UDP': {}},)), is_leaf=False, yang_name="supported-protocols", parent=self, path_helper=self._path_helper, extmethods=self._extmethods, register_paths=True, namespace='http://example.com/practice', defining_module='practice', yang_type='protocol-name', is_config=False)
  File "/anaconda3/lib/python3.6/site-packages/pyangbind/lib/yangtypes.py", line 1160, in YANGDynClass
    return YANGBaseClass(*args, **kwargs)
  File "/anaconda3/lib/python3.6/site-packages/pyangbind/lib/yangtypes.py", line 1037, in __init__
    six.reraise()
TypeError: reraise() missing 2 required positional arguments: 'tp' and 'value'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/anaconda3/lib/python3.6/site-packages/pyangbind/lib/yangtypes.py", line 1033, in __init__
    super(YANGBaseClass, self).__init__(*args, **kwargs)
  File "/Users/joshisk/PycharmProjects/tapi-pyang/lib/practice.py", line 65, in __init__
    setmethod(getattr(args[0], e), load=load)
  File "/Users/joshisk/PycharmProjects/tapi-pyang/lib/practice.py", line 99, in _set_supported_protocols
    'generated-type': """YANGDynClass(unique=True, base=TypedListType(allowed_type=RestrictedClassType(base_type=six.text_type,                                     restriction_type="dict_key",                                     restriction_arg={'TCP': {}, 'TLS': {}, 'UDP': {}},)), is_leaf=False, yang_name="supported-protocols", parent=self, path_helper=self._path_helper, extmethods=self._extmethods, register_paths=True, namespace='http://example.com/practice', defining_module='practice', yang_type='protocol-name', is_config=False)""",
ValueError: {'error-string': 'supported_protocols must be of a type compatible with protocol-name', 'defined-type': 'practice:protocol-name', 'generated-type': 'YANGDynClass(unique=True, base=TypedListType(allowed_type=RestrictedClassType(base_type=six.text_type,                                     restriction_type="dict_key",                                     restriction_arg={\'TCP\': {}, \'TLS\': {}, \'UDP\': {}},)), is_leaf=False, yang_name="supported-protocols", parent=self, path_helper=self._path_helper, extmethods=self._extmethods, register_paths=True, namespace=\'http://example.com/practice\', defining_module=\'practice\', yang_type=\'protocol-name\', is_config=False)'}

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/joshisk/PycharmProjects/tapi-pyang/src/main.py", line 14, in <module>
    _tmp = computer.application.append(application)
  File "/anaconda3/lib/python3.6/site-packages/pyangbind/lib/yangtypes.py", line 1099, in append
    super(YANGBaseClass, self).append(*args, **kwargs)
  File "/anaconda3/lib/python3.6/site-packages/pyangbind/lib/yangtypes.py", line 741, in append
    self.__set(_k=self._extract_key(obj), _v=obj)
  File "/anaconda3/lib/python3.6/site-packages/pyangbind/lib/yangtypes.py", line 665, in __set
    extensions=extensions)
  File "/anaconda3/lib/python3.6/site-packages/pyangbind/lib/yangtypes.py", line 1160, in YANGDynClass
    return YANGBaseClass(*args, **kwargs)
  File "/anaconda3/lib/python3.6/site-packages/pyangbind/lib/yangtypes.py", line 1037, in __init__
    six.reraise()
TypeError: reraise() missing 2 required positional arguments: 'tp' and 'value'

YANG:

module practice {
    yang-version "1";
    namespace "http://example.com/practice";
    prefix "foo";

    description
        "A Practice Yang Definition";

    typedef protocol-name {
        description "Transport layer protocols";
        type enumeration {
            enum TCP {
                description "TRANSMISSION CONTROL PROTOCOL";
            }
            enum TLS {
                description "TRANSPORT LAYER SECURITY";
            }
            enum UDP {
                description "USER DATAGRAM PROTOCOL";
            }
        }
    }

    grouping application {
                description "Application";

                leaf-list supported-protocols {
                    description "Supported transport protocols of this application";
                    type protocol-name;
                    config false;
                    min-elements 1;
                }
    }

    container computer {
        list application {
            key 'uuid';
            config false;
            uses application;
            leaf uuid {
                type string;
                description 'UUID';
            }
            description 'none';
        }
    }
}

My Code:

from lib import practice
from pyangbind.lib import pybindJSON

application = practice.yc_application_practice__computer_application()
application._set_uuid('UUID1')
application.supported_protocols.append('TCP')
application.supported_protocols.append('TLS')

print(pybindJSON.dumps(application, mode='ietf'))

computer = practice.yc_computer_practice__computer()
_tmp = computer.application.append(application)

print(pybindJSON.dumps(computer, mode='ietf'))

sravanjosh avatar Aug 29 '18 08:08 sravanjosh

You need to create an instance of practice.practice() and then grab a reference to a new entry on a list from inside that.

something like:

pr = practice.practice()
...
application = pr.computer.application.add("UUID1")
application.supported_protocols.append("TCP")

chrisy avatar Aug 29 '18 13:08 chrisy

Closing issues without recent activity

JoseIgnacioTamayo avatar Feb 22 '24 21:02 JoseIgnacioTamayo