Use IP address types on UDP encap API
[vpp.git] / test / vpp_ip.py
1 """
2   IP Types
3
4 """
5
6 from ipaddress import ip_address
7
8
9 class IpAddressFamily:
10     ADDRESS_IP4 = 0
11     ADDRESS_IP6 = 1
12
13
14 INVALID_INDEX = 0xffffffff
15
16
17 def compare_ip_address(api_address, py_address):
18     if 4 is py_address.version:
19         if py_address.packed == api_address.ip4.address:
20             return True
21     else:
22         if py_address.packed == api_address.ip6.address:
23             return True
24     return False
25
26
27 class VppIpAddressUnion():
28     def __init__(self, addr):
29         self.addr = addr
30         self.ip_addr = ip_address(unicode(self.addr))
31
32     @property
33     def version(self):
34         return self.ip_addr.version
35
36     @property
37     def address(self):
38         return self.addr
39
40     def encode(self):
41         if self.ip_addr.version is 6:
42             return {
43                 'ip6': {
44                     'address': self.ip_addr.packed
45                 },
46             }
47         else:
48             return {
49                 'ip4': {
50                     'address': self.ip_addr.packed
51                 },
52             }
53
54
55 class VppIpAddress():
56     def __init__(self, addr):
57         self.addr = VppIpAddressUnion(addr)
58
59     def encode(self):
60         if self.addr.version is 6:
61             return {
62                 'af': IpAddressFamily.ADDRESS_IP6,
63                 'un': self.addr.encode()
64             }
65         else:
66             return {
67                 'af': IpAddressFamily.ADDRESS_IP4,
68                 'un': self.addr.encode()
69             }
70
71     @property
72     def address(self):
73         return self.addr.address
74
75
76 class VppIpPrefix():
77     def __init__(self, addr, len):
78         self.addr = VppIpAddress(addr)
79         self.len = len
80
81     def __eq__(self, other):
82         if self.addr == other.addr and self.len == other.len:
83             return True
84         return False
85
86     def encode(self):
87         return {'address': self.addr.encode(),
88                 'address_length': self.len}
89
90     @property
91     def address(self):
92         return self.addr.address
93
94
95 class VppIpMPrefix():
96     def __init__(self, saddr, gaddr, len):
97         self.saddr = saddr
98         self.gaddr = gaddr
99         self.len = len
100         self.ip_saddr = ip_address(unicode(self.saddr))
101         self.ip_gaddr = ip_address(unicode(self.gaddr))
102
103     def encode(self):
104
105         if 6 is self.ip_saddr.version:
106             prefix = {
107                 'af': IpAddressFamily.ADDRESS_IP6,
108                 'grp_address': {
109                     'ip6': {
110                         'address': self.ip_gaddr.packed
111                     },
112                 },
113                 'src_address': {
114                     'ip6': {
115                         'address': self.ip_saddr.packed
116                     },
117                 },
118                 'grp_address_length': self.len,
119             }
120         else:
121             prefix = {
122                 'af': IpAddressFamily.ADDRESS_IP4,
123                 'grp_address': {
124                     'ip4': {
125                         'address': self.ip_gaddr.packed
126                     },
127                 },
128                 'src_address': {
129                     'ip4': {
130                         'address': self.ip_saddr.packed
131                     },
132                 },
133                 'grp_address_length': self.len,
134             }
135         return prefix