summaryrefslogtreecommitdiffstats
path: root/test/vpp_ip.py
diff options
context:
space:
mode:
authorNeale Ranns <nranns@cisco.com>2018-08-08 01:06:40 -0700
committerOle Trøan <otroan@employees.org>2018-08-10 08:26:00 +0000
commitd0df49f26eabf2f534b567f3370e50c4e804aeea (patch)
treeeea014e723f00b4e00e57937fc5f6e043e2162f9 /test/vpp_ip.py
parent404d88edce63a635b58ba54d21d91735ff933584 (diff)
Use IP address types on UDP encap API
Change-Id: I3c714c519b6d0009329b50947ce250c18ee2a85a Signed-off-by: Neale Ranns <nranns@cisco.com>
Diffstat (limited to 'test/vpp_ip.py')
-rw-r--r--test/vpp_ip.py135
1 files changed, 135 insertions, 0 deletions
diff --git a/test/vpp_ip.py b/test/vpp_ip.py
new file mode 100644
index 00000000000..912d8430d81
--- /dev/null
+++ b/test/vpp_ip.py
@@ -0,0 +1,135 @@
+"""
+ IP Types
+
+"""
+
+from ipaddress import ip_address
+
+
+class IpAddressFamily:
+ ADDRESS_IP4 = 0
+ ADDRESS_IP6 = 1
+
+
+INVALID_INDEX = 0xffffffff
+
+
+def compare_ip_address(api_address, py_address):
+ if 4 is py_address.version:
+ if py_address.packed == api_address.ip4.address:
+ return True
+ else:
+ if py_address.packed == api_address.ip6.address:
+ return True
+ return False
+
+
+class VppIpAddressUnion():
+ def __init__(self, addr):
+ self.addr = addr
+ self.ip_addr = ip_address(unicode(self.addr))
+
+ @property
+ def version(self):
+ return self.ip_addr.version
+
+ @property
+ def address(self):
+ return self.addr
+
+ def encode(self):
+ if self.ip_addr.version is 6:
+ return {
+ 'ip6': {
+ 'address': self.ip_addr.packed
+ },
+ }
+ else:
+ return {
+ 'ip4': {
+ 'address': self.ip_addr.packed
+ },
+ }
+
+
+class VppIpAddress():
+ def __init__(self, addr):
+ self.addr = VppIpAddressUnion(addr)
+
+ def encode(self):
+ if self.addr.version is 6:
+ return {
+ 'af': IpAddressFamily.ADDRESS_IP6,
+ 'un': self.addr.encode()
+ }
+ else:
+ return {
+ 'af': IpAddressFamily.ADDRESS_IP4,
+ 'un': self.addr.encode()
+ }
+
+ @property
+ def address(self):
+ return self.addr.address
+
+
+class VppIpPrefix():
+ def __init__(self, addr, len):
+ self.addr = VppIpAddress(addr)
+ self.len = len
+
+ def __eq__(self, other):
+ if self.addr == other.addr and self.len == other.len:
+ return True
+ return False
+
+ def encode(self):
+ return {'address': self.addr.encode(),
+ 'address_length': self.len}
+
+ @property
+ def address(self):
+ return self.addr.address
+
+
+class VppIpMPrefix():
+ def __init__(self, saddr, gaddr, len):
+ self.saddr = saddr
+ self.gaddr = gaddr
+ self.len = len
+ self.ip_saddr = ip_address(unicode(self.saddr))
+ self.ip_gaddr = ip_address(unicode(self.gaddr))
+
+ def encode(self):
+
+ if 6 is self.ip_saddr.version:
+ prefix = {
+ 'af': IpAddressFamily.ADDRESS_IP6,
+ 'grp_address': {
+ 'ip6': {
+ 'address': self.ip_gaddr.packed
+ },
+ },
+ 'src_address': {
+ 'ip6': {
+ 'address': self.ip_saddr.packed
+ },
+ },
+ 'grp_address_length': self.len,
+ }
+ else:
+ prefix = {
+ 'af': IpAddressFamily.ADDRESS_IP4,
+ 'grp_address': {
+ 'ip4': {
+ 'address': self.ip_gaddr.packed
+ },
+ },
+ 'src_address': {
+ 'ip4': {
+ 'address': self.ip_saddr.packed
+ },
+ },
+ 'grp_address_length': self.len,
+ }
+ return prefix