summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOle Troan <ot@cisco.com>2018-10-16 14:42:50 +0200
committerNeale Ranns <nranns@cisco.com>2018-10-18 10:16:49 +0000
commit0685da4a6db8195d1a86f3466fcd3395b0ccb83e (patch)
treec77bf5b08aade685beadc01972f601e795685ba8
parent5fcca2a124ac828b01633a3ad237a6435e8ff603 (diff)
PAPI: Expose API enums to tests / applications
e.g: from vpp_papi import VppEnum VppEnum.vl_api_address_family_t.ADDRESS_IP4 Change-Id: I10c22d57234a1a06e98a889cf80b19085b468ed3 Signed-off-by: Ole Troan <ot@cisco.com>
-rw-r--r--src/vpp-api/python/vpp_papi/vpp_papi.py49
-rw-r--r--src/vpp-api/python/vpp_papi/vpp_serializer.py10
-rw-r--r--test/test_svs.py95
-rw-r--r--test/vpp_ip.py20
4 files changed, 106 insertions, 68 deletions
diff --git a/src/vpp-api/python/vpp_papi/vpp_papi.py b/src/vpp-api/python/vpp_papi/vpp_papi.py
index e1a7059f317..5e98f92cecd 100644
--- a/src/vpp-api/python/vpp_papi/vpp_papi.py
+++ b/src/vpp-api/python/vpp_papi/vpp_papi.py
@@ -27,7 +27,7 @@ import fnmatch
import weakref
import atexit
from . vpp_serializer import VPPType, VPPEnumType, VPPUnionType, BaseTypes
-from . vpp_serializer import VPPMessage
+from . vpp_serializer import VPPMessage, vpp_get_type
if sys.version[0] == '2':
import Queue as queue
@@ -35,6 +35,19 @@ else:
import queue as queue
+class VppEnumType(type):
+ def __getattr__(cls, name):
+ t = vpp_get_type(name)
+ return t.enum
+
+
+# Python3
+# class VppEnum(metaclass=VppEnumType):
+# pass
+class VppEnum:
+ __metaclass__ = VppEnumType
+
+
def vpp_atexit(vpp_weakref):
"""Clean up VPP connection on shutdown."""
vpp_instance = vpp_weakref()
@@ -94,21 +107,22 @@ class VPP():
unresolved = {}
for k, v in types.items():
t = v['data']
- if v['type'] == 'enum':
- try:
- VPPEnumType(t[0], t[1:])
- except ValueError:
- unresolved[k] = v
- elif v['type'] == 'union':
- try:
- VPPUnionType(t[0], t[1:])
- except ValueError:
- unresolved[k] = v
- elif v['type'] == 'type':
- try:
- VPPType(t[0], t[1:])
- except ValueError:
- unresolved[k] = v
+ if not vpp_get_type(t[0]):
+ if v['type'] == 'enum':
+ try:
+ VPPEnumType(t[0], t[1:])
+ except ValueError:
+ unresolved[k] = v
+ elif v['type'] == 'union':
+ try:
+ VPPUnionType(t[0], t[1:])
+ except ValueError:
+ unresolved[k] = v
+ elif v['type'] == 'type':
+ try:
+ VPPType(t[0], t[1:])
+ except ValueError:
+ unresolved[k] = v
if len(unresolved) == 0:
break
if i > 3:
@@ -200,6 +214,9 @@ class VPP():
return self.context
get_context = ContextId()
+ def get_type(self, name):
+ return vpp_get_type(name)
+
@classmethod
def find_api_dir(cls):
"""Attempt to find the best directory in which API definition
diff --git a/src/vpp-api/python/vpp_papi/vpp_serializer.py b/src/vpp-api/python/vpp_papi/vpp_serializer.py
index cac6dd61791..240912d96ba 100644
--- a/src/vpp-api/python/vpp_papi/vpp_serializer.py
+++ b/src/vpp-api/python/vpp_papi/vpp_serializer.py
@@ -62,6 +62,13 @@ types['f64'] = BaseTypes('f64')
types['bool'] = BaseTypes('bool')
+def vpp_get_type(name):
+ try:
+ return types[name]
+ except KeyError:
+ return None
+
+
class FixedList_u8():
def __init__(self, name, field_type, num):
self.name = name
@@ -202,6 +209,9 @@ class VPPEnumType():
def __getattr__(self, name):
return self.enum[name]
+ def __nonzero__(self):
+ return True
+
def pack(self, data, kwargs=None):
return types['u32'].pack(data, kwargs)
diff --git a/test/test_svs.py b/test/test_svs.py
index 550f35a489f..8429f437ae1 100644
--- a/test/test_svs.py
+++ b/test/test_svs.py
@@ -1,7 +1,7 @@
#!/usr/bin/env python
from framework import VppTestCase, VppTestRunner
-from vpp_ip import IpAddressFamily, VppIpPrefix
+from vpp_ip import VppIpPrefix
from vpp_ip_route import VppIpTable
@@ -10,6 +10,8 @@ from scapy.layers.l2 import Ether
from scapy.layers.inet import IP, UDP, ICMP
from scapy.layers.inet6 import IPv6
+from vpp_papi import VppEnum
+
class TestSVS(VppTestCase):
""" SVS Test Case """
@@ -94,7 +96,8 @@ class TestSVS(VppTestCase):
table_ids = [101, 102]
for table_id in table_ids:
- self.vapi.svs_table_add_del(IpAddressFamily.ADDRESS_IP4, table_id)
+ self.vapi.svs_table_add_del(
+ VppEnum.vl_api_address_family_t.ADDRESS_IP4, table_id)
#
# map X.0.0.0/8 to each SVS table for lookup in table X
@@ -108,12 +111,12 @@ class TestSVS(VppTestCase):
#
# Enable SVS on pg0/pg1 using table 1001/1002
#
- self.vapi.svs_enable_disable(IpAddressFamily.ADDRESS_IP4,
- table_ids[0],
- self.pg0.sw_if_index)
- self.vapi.svs_enable_disable(IpAddressFamily.ADDRESS_IP4,
- table_ids[1],
- self.pg1.sw_if_index)
+ self.vapi.svs_enable_disable(
+ VppEnum.vl_api_address_family_t.ADDRESS_IP4, table_ids[0],
+ self.pg0.sw_if_index)
+ self.vapi.svs_enable_disable(
+ VppEnum.vl_api_address_family_t.ADDRESS_IP4, table_ids[1],
+ self.pg1.sw_if_index)
#
# now all the packets should be delivered out the respective interface
@@ -148,22 +151,24 @@ class TestSVS(VppTestCase):
self.assertEqual(ss[0].table_id, table_ids[0])
self.assertEqual(ss[0].sw_if_index, self.pg0.sw_if_index)
- self.assertEqual(ss[0].af, IpAddressFamily.ADDRESS_IP4)
+ self.assertEqual(ss[0].af, VppEnum.vl_api_address_family_t.ADDRESS_IP4)
self.assertEqual(ss[1].table_id, table_ids[1])
self.assertEqual(ss[1].sw_if_index, self.pg1.sw_if_index)
- self.assertEqual(ss[1].af, IpAddressFamily.ADDRESS_IP4)
+ self.assertEqual(ss[1].af, VppEnum.vl_api_address_family_t.ADDRESS_IP4)
#
# cleanup
#
- self.vapi.svs_enable_disable(IpAddressFamily.ADDRESS_IP4,
- table_ids[0],
- self.pg0.sw_if_index,
- is_enable=0)
- self.vapi.svs_enable_disable(IpAddressFamily.ADDRESS_IP4,
- table_ids[1],
- self.pg1.sw_if_index,
- is_enable=0)
+ self.vapi.svs_enable_disable(
+ VppEnum.vl_api_address_family_t.ADDRESS_IP4,
+ table_ids[0],
+ self.pg0.sw_if_index,
+ is_enable=0)
+ self.vapi.svs_enable_disable(
+ VppEnum.vl_api_address_family_t.ADDRESS_IP4,
+ table_ids[1],
+ self.pg1.sw_if_index,
+ is_enable=0)
for table_id in table_ids:
for i in range(1, 4):
@@ -171,9 +176,10 @@ class TestSVS(VppTestCase):
table_id,
VppIpPrefix("%d.0.0.0" % i, 8).encode(),
0, is_add=0)
- self.vapi.svs_table_add_del(IpAddressFamily.ADDRESS_IP4,
- table_id,
- is_add=0)
+ self.vapi.svs_table_add_del(
+ VppEnum.vl_api_address_family_t.ADDRESS_IP4,
+ table_id,
+ is_add=0)
def test_svs6(self):
""" Source VRF Select IP6 """
@@ -220,7 +226,8 @@ class TestSVS(VppTestCase):
table_ids = [101, 102]
for table_id in table_ids:
- self.vapi.svs_table_add_del(IpAddressFamily.ADDRESS_IP6, table_id)
+ self.vapi.svs_table_add_del(
+ VppEnum.vl_api_address_family_t.ADDRESS_IP6, table_id)
#
# map X.0.0.0/8 to each SVS table for lookup in table X
@@ -234,12 +241,14 @@ class TestSVS(VppTestCase):
#
# Enable SVS on pg0/pg1 using table 1001/1002
#
- self.vapi.svs_enable_disable(IpAddressFamily.ADDRESS_IP6,
- table_ids[0],
- self.pg0.sw_if_index)
- self.vapi.svs_enable_disable(IpAddressFamily.ADDRESS_IP6,
- table_ids[1],
- self.pg1.sw_if_index)
+ self.vapi.svs_enable_disable(
+ VppEnum.vl_api_address_family_t.ADDRESS_IP6,
+ table_ids[0],
+ self.pg0.sw_if_index)
+ self.vapi.svs_enable_disable(
+ VppEnum.vl_api_address_family_t.ADDRESS_IP6,
+ table_ids[1],
+ self.pg1.sw_if_index)
#
# now all the packets should be delivered out the respective interface
@@ -274,31 +283,35 @@ class TestSVS(VppTestCase):
self.assertEqual(ss[0].table_id, table_ids[0])
self.assertEqual(ss[0].sw_if_index, self.pg0.sw_if_index)
- self.assertEqual(ss[0].af, IpAddressFamily.ADDRESS_IP6)
+ self.assertEqual(ss[0].af, VppEnum.vl_api_address_family_t.ADDRESS_IP6)
self.assertEqual(ss[1].table_id, table_ids[1])
self.assertEqual(ss[1].sw_if_index, self.pg1.sw_if_index)
- self.assertEqual(ss[1].af, IpAddressFamily.ADDRESS_IP6)
+ self.assertEqual(ss[1].af, VppEnum.vl_api_address_family_t.ADDRESS_IP6)
#
# cleanup
#
- self.vapi.svs_enable_disable(IpAddressFamily.ADDRESS_IP6,
- table_ids[0],
- self.pg0.sw_if_index,
- is_enable=0)
- self.vapi.svs_enable_disable(IpAddressFamily.ADDRESS_IP6,
- table_ids[1],
- self.pg1.sw_if_index,
- is_enable=0)
+ self.vapi.svs_enable_disable(
+ VppEnum.vl_api_address_family_t.ADDRESS_IP6,
+ table_ids[0],
+ self.pg0.sw_if_index,
+ is_enable=0)
+ self.vapi.svs_enable_disable(
+ VppEnum.vl_api_address_family_t.ADDRESS_IP6,
+ table_ids[1],
+ self.pg1.sw_if_index,
+ is_enable=0)
for table_id in table_ids:
for i in range(1, 4):
self.vapi.svs_route_add_del(
table_id,
VppIpPrefix("2001:%d::" % i, 32).encode(),
0, is_add=0)
- self.vapi.svs_table_add_del(IpAddressFamily.ADDRESS_IP6,
- table_id,
- is_add=0)
+ self.vapi.svs_table_add_del(
+ VppEnum.vl_api_address_family_t.ADDRESS_IP6,
+ table_id,
+ is_add=0)
+
if __name__ == '__main__':
unittest.main(testRunner=VppTestRunner)
diff --git a/test/vpp_ip.py b/test/vpp_ip.py
index e44e6b56e30..e92c91943db 100644
--- a/test/vpp_ip.py
+++ b/test/vpp_ip.py
@@ -5,11 +5,7 @@
from ipaddress import ip_address
from socket import AF_INET, AF_INET6
-
-
-class IpAddressFamily:
- ADDRESS_IP4 = 0
- ADDRESS_IP6 = 1
+from vpp_papi import VppEnum
class DpoProto:
@@ -86,12 +82,12 @@ class VppIpAddress():
def encode(self):
if self.addr.version is 6:
return {
- 'af': IpAddressFamily.ADDRESS_IP6,
+ 'af': VppEnum.vl_api_address_family_t.ADDRESS_IP6,
'un': self.addr.encode()
}
else:
return {
- 'af': IpAddressFamily.ADDRESS_IP4,
+ 'af': VppEnum.vl_api_address_family_t.ADDRESS_IP4,
'un': self.addr.encode()
}
@@ -101,10 +97,12 @@ class VppIpAddress():
elif hasattr(other, "af") and hasattr(other, "un"):
# a vp_api_address_t
if 4 is self.version:
- return other.af == IpAddressFamily.ADDRESS_IP4 and \
+ return other.af == \
+ VppEnum.vl_api_address_family_t.ADDRESS_IP4 and \
other.un == self.addr
else:
- return other.af == IpAddressFamily.ADDRESS_IP6 and \
+ return other.af == \
+ VppEnum.vl_api_address_family_t.ADDRESS_IP6 and \
other.un == self.addr
else:
raise Exception("Comparing VppIpAddress:%s with unknown type: %s" %
@@ -202,7 +200,7 @@ class VppIpMPrefix():
if 6 is self.ip_saddr.version:
prefix = {
- 'af': IpAddressFamily.ADDRESS_IP6,
+ 'af': VppEnum.vl_api_address_family_t.ADDRESS_IP6,
'grp_address': {
'ip6': {
'address': self.ip_gaddr.packed
@@ -217,7 +215,7 @@ class VppIpMPrefix():
}
else:
prefix = {
- 'af': IpAddressFamily.ADDRESS_IP4,
+ 'af': VppEnum.vl_api_address_family_t.ADDRESS_IP4,
'grp_address': {
'ip4': {
'address': self.ip_gaddr.packed