from .vpp_papi import FuncWrapper, VPP, VppApiDynamicMethodHolder # noqa: F401
-from .vpp_papi import VppEnum, VppEnumType # noqa: F401
+from .vpp_papi import VppEnum, VppEnumType, VppEnumFlag # noqa: F401
from .vpp_papi import VPPIOError, VPPRuntimeError, VPPValueError # noqa: F401
from .vpp_papi import VPPApiClient # noqa: F401
-from .vpp_papi import VPPApiJSONFiles # noqa: F401
+from .vpp_papi import VPPApiJSONFiles # noqa: F401
from . macaddress import MACAddress, mac_pton, mac_ntop # noqa: F401
# sorted lexicographically
class TestVppPapiVPPApiClient(unittest.TestCase):
-
def test_getcontext(self):
vpp_papi.VPPApiClient.apidir = '.'
c = vpp_papi.VPPApiClient(testmode=True, use_socket=True)
class TestVppTypes(unittest.TestCase):
-
def test_enum_from_json(self):
json_api = """\
{
self.assertTrue(str(t).startswith("VPPEnumType"))
self.assertEqual(t.name, type_name)
+ def test_enumflagmixed_from_json(self):
+ json_api = """\
+{
+ "enums": [
-class TestVppPapiLogging(unittest.TestCase):
+ [
+ "address_family",
+ [
+ "ADDRESS_IP4",
+ 0
+ ],
+ [
+ "ADDRESS_IP6",
+ 1
+ ],
+ {
+ "enumtype": "u8"
+ }
+ ]
+ ],
+ "enumflags": [
+
+ [
+ "if_type",
+ [
+ "IF_API_TYPE_HARDWARE",
+ 0
+ ],
+ [
+ "IF_API_TYPE_SUB",
+ 1
+ ],
+ [
+ "IF_API_TYPE_P2P",
+ 2
+ ],
+ [
+ "IF_API_TYPE_PIPE",
+ 3
+ ],
+ {
+ "enumtype": "u32"
+ }
+ ]
+ ]
+}
+"""
+
+ processor = vpp_papi.VPPApiJSONFiles()
+
+ # add the types to vpp_serializer
+ processor.process_json_str(json_api)
+
+ vpp_transport_shmem.VppTransport = mock.MagicMock()
+ ac = vpp_papi.VPPApiClient(apifiles=[], testmode=True)
+ print(ac)
+ type_name = "vl_api_if_type_t"
+ t = ac.get_type(type_name)
+ print(t)
+ self.assertTrue(str(t).startswith("VPPEnumType"))
+ self.assertEqual(t.name, type_name)
+
+ def test_enumflag_from_json(self):
+ json_api = """\
+{
+ "enumflags": [
+ [
+ "address_family",
+ [
+ "ADDRESS_IP4",
+ 0
+ ],
+ [
+ "ADDRESS_IP6",
+ 1
+ ],
+ {
+ "enumtype": "u8"
+ }
+ ],
+ [
+ "if_type",
+ [
+ "IF_API_TYPE_HARDWARE",
+ 0
+ ],
+ [
+ "IF_API_TYPE_SUB",
+ 1
+ ],
+ [
+ "IF_API_TYPE_P2P",
+ 2
+ ],
+ [
+ "IF_API_TYPE_PIPE",
+ 3
+ ],
+ {
+ "enumtype": "u32"
+ }
+ ]
+ ]
+}
+"""
+ processor = vpp_papi.VPPApiJSONFiles()
+
+ # add the types to vpp_serializer
+ processor.process_json_str(json_api)
+
+ vpp_transport_shmem.VppTransport = mock.MagicMock()
+ ac = vpp_papi.VPPApiClient(apifiles=[], testmode=True)
+ type_name = "vl_api_if_type_t"
+ t = ac.get_type(type_name)
+ self.assertTrue(str(t).startswith("VPPEnumType"))
+ self.assertEqual(t.name, type_name)
+
+
+class TestVppPapiLogging(unittest.TestCase):
def test_logger(self):
class Transport:
connected = True
#!/usr/bin/env python3
import unittest
-from vpp_papi.vpp_serializer import VPPType, VPPEnumType
+from vpp_papi.vpp_serializer import VPPType, VPPEnumType, VPPEnumFlagType
from vpp_papi.vpp_serializer import VPPUnionType, VPPMessage
from vpp_papi.vpp_serializer import VPPTypeAlias, VPPSerializerValueError
from vpp_papi import MACAddress
af = VPPEnumType('vl_api_address_family_t', [["ADDRESS_IP4", 0],
["ADDRESS_IP6", 1],
{"enumtype": "u32"}])
+ aff = VPPEnumFlagType('vl_api_address_family_flag_t', [["ADDRESS_IP4", 0],
+ ["ADDRESS_IP6", 1],
+ {"enumtype": "u32"}])
ip4 = VPPTypeAlias('vl_api_ip4_address_t', {'type': 'u8',
'length': 4})
ip6 = VPPTypeAlias('vl_api_ip6_address_t', {'type': 'u8',
import atexit
import time
from . vpp_format import verify_enum_hint
-from . vpp_serializer import VPPType, VPPEnumType, VPPUnionType
+from . vpp_serializer import VPPType, VPPEnumType, VPPEnumFlagType, VPPUnionType
from . vpp_serializer import VPPMessage, vpp_get_type, VPPTypeAlias
try:
logger.addHandler(logging.NullHandler())
__all__ = ('FuncWrapper', 'VPP', 'VppApiDynamicMethodHolder',
- 'VppEnum', 'VppEnumType',
+ 'VppEnum', 'VppEnumType', 'VppEnumFlag',
'VPPIOError', 'VPPRuntimeError', 'VPPValueError',
'VPPApiClient', )
pass
+@metaclass(VppEnumType)
+class VppEnumFlag:
+ pass
+
+
def vpp_atexit(vpp_weakref):
"""Clean up VPP connection on shutdown."""
vpp_instance = vpp_weakref()
vpp_instance.disconnect()
-
-
def add_convenience_methods():
# provide convenience methods to IP[46]Address.vapi_af
def _vapi_af(self):
types[t[0]] = {'type': 'enum', 'data': t}
except KeyError:
pass
-
+ try:
+ for t in api['enumflags']:
+ t[0] = 'vl_api_' + t[0] + '_t'
+ types[t[0]] = {'type': 'enum', 'data': t}
+ except KeyError:
+ pass
try:
for t in api['unions']:
t[0] = 'vl_api_' + t[0] + '_t'
VPPEnumType(t[0], t[1:])
except ValueError:
unresolved[k] = v
+ if not vpp_get_type(k):
+ if v['type'] == 'enumflag':
+ try:
+ VPPEnumFlagType(t[0], t[1:])
+ except ValueError:
+ unresolved[k] = v
elif v['type'] == 'union':
try:
VPPUnionType(t[0], t[1:])
)
+# Will change to IntEnum after 21.04 release
class VPPEnumType(Packer):
+ output_class = IntFlag
+
def __init__(self, name, msgdef, options=None):
self.size = types['u32'].size
self.name = name
continue
ename, evalue = f
e_hash[ename] = evalue
- self.enum = IntFlag(name, e_hash)
+ self.enum = self.output_class(name, e_hash)
types[name] = self
- class_types[name] = VPPEnumType
+ class_types[name] = self.__class__
self.options = options
def __getattr__(self, name):
def __bool__(self):
return True
-
def pack(self, data, kwargs=None):
if data is None: # Default to zero if not specified
if self.options and 'default' in self.options:
x, size = types[self.enumtype].unpack(data, offset)
return self.enum(x), size
- @staticmethod
- def _get_packer_with_options(f_type, options):
- return VPPEnumType(f_type, types[f_type].msgdef, options=options)
+ @classmethod
+ def _get_packer_with_options(cls, f_type, options):
+ return cls(f_type, types[f_type].msgdef, options=options)
def __repr__(self):
- return "VPPEnumType(name=%s, msgdef=%s, options=%s)" % (
- self.name, self.msgdef, self.options
+ return "%s(name=%s, msgdef=%s, options=%s)" % (
+ self.__class__.__name__, self.name, self.msgdef, self.options
)
+class VPPEnumFlagType(VPPEnumType):
+ output_class = IntFlag
+
+ def __init__(self, name, msgdef, options=None):
+ super(VPPEnumFlagType, self).__init__(name, msgdef, options)
+
+
class VPPUnionType(Packer):
def __init__(self, name, msgdef):
self.name = name