PAPI: Expose API enums to tests / applications
[vpp.git] / test / vpp_ip.py
1 """
2   IP Types
3
4 """
5
6 from ipaddress import ip_address
7 from socket import AF_INET, AF_INET6
8 from vpp_papi import VppEnum
9
10
11 class DpoProto:
12     DPO_PROTO_IP4 = 0
13     DPO_PROTO_IP6 = 1
14     DPO_PROTO_MPLS = 2
15     DPO_PROTO_ETHERNET = 3
16     DPO_PROTO_BIER = 4
17     DPO_PROTO_NSH = 5
18
19
20 INVALID_INDEX = 0xffffffff
21
22
23 class VppIpAddressUnion():
24     def __init__(self, addr):
25         self.addr = addr
26         self.ip_addr = ip_address(unicode(self.addr))
27
28     def encode(self):
29         if self.version is 6:
30             return {
31                 'ip6': {
32                     'address': self.ip_addr.packed
33                 },
34             }
35         else:
36             return {
37                 'ip4': {
38                     'address': self.ip_addr.packed
39                 },
40             }
41
42     @property
43     def version(self):
44         return self.ip_addr.version
45
46     @property
47     def address(self):
48         return self.addr
49
50     @property
51     def length(self):
52         if self.version is 6:
53             return 128
54         else:
55             return 32
56
57     @property
58     def bytes(self):
59         return self.ip_addr.packed
60
61     def __eq__(self, other):
62         if isinstance(other, self.__class__):
63             return self.ip_addr == other.ip_addr
64         elif hasattr(other, "ip4") and hasattr(other, "ip6"):
65             # vl_api_address_union_t
66             if 4 is self.version:
67                 return self.ip_addr.packed == other.ip4.address
68             else:
69                 return self.ip_addr.packed == other.ip6.address
70         else:
71             raise Exception("Comparing VppIpAddresUnions:%s"
72                             " with unknown type: %s" %
73                             (self, other))
74
75         return False
76
77
78 class VppIpAddress():
79     def __init__(self, addr):
80         self.addr = VppIpAddressUnion(addr)
81
82     def encode(self):
83         if self.addr.version is 6:
84             return {
85                 'af': VppEnum.vl_api_address_family_t.ADDRESS_IP6,
86                 'un': self.addr.encode()
87             }
88         else:
89             return {
90                 'af': VppEnum.vl_api_address_family_t.ADDRESS_IP4,
91                 'un': self.addr.encode()
92             }
93
94     def __eq__(self, other):
95         if isinstance(other, self.__class__):
96             return self.addr == other.addr
97         elif hasattr(other, "af") and hasattr(other, "un"):
98             # a vp_api_address_t
99             if 4 is self.version:
100                 return other.af == \
101                     VppEnum.vl_api_address_family_t.ADDRESS_IP4 and \
102                     other.un == self.addr
103             else:
104                 return other.af == \
105                     VppEnum.vl_api_address_family_t.ADDRESS_IP6 and \
106                     other.un == self.addr
107         else:
108             raise Exception("Comparing VppIpAddress:%s with unknown type: %s" %
109                             (self, other))
110         return False
111
112     def __ne__(self, other):
113         return not (self == other)
114
115     def __str__(self):
116         return self.address
117
118     @property
119     def bytes(self):
120         return self.addr.bytes
121
122     @property
123     def address(self):
124         return self.addr.address
125
126     @property
127     def length(self):
128         return self.addr.length
129
130     @property
131     def version(self):
132         return self.addr.version
133
134     @property
135     def is_ip6(self):
136         return (self.version == 6)
137
138     @property
139     def af(self):
140         if self.version == 6:
141             return AF_INET6
142         else:
143             return AF_INET
144
145     @property
146     def dpo_proto(self):
147         if self.version is 6:
148             return DpoProto.DPO_PROTO_IP6
149         else:
150             return DpoProto.DPO_PROTO_IP4
151
152
153 class VppIpPrefix():
154     def __init__(self, addr, len):
155         self.addr = VppIpAddress(addr)
156         self.len = len
157
158     def __eq__(self, other):
159         if self.addr == other.addr and self.len == other.len:
160             return True
161         return False
162
163     def encode(self):
164         return {'address': self.addr.encode(),
165                 'address_length': self.len}
166
167     @property
168     def address(self):
169         return self.addr.address
170
171     @property
172     def length(self):
173         return self.len
174
175     def __str__(self):
176         return "%s/%d" % (self.address, self.length)
177
178     def __eq__(self, other):
179         if isinstance(other, self.__class__):
180             return (self.len == other.len and self.addr == other.addr)
181         elif hasattr(other, "address") and hasattr(other, "address_length"):
182             # vl_api_prefix_t
183             return self.len == other.address_length and \
184                 self.addr == other.address
185         else:
186             raise Exception("Comparing VppIpPrefix:%s with unknown type: %s" %
187                             (self, other))
188         return False
189
190
191 class VppIpMPrefix():
192     def __init__(self, saddr, gaddr, len):
193         self.saddr = saddr
194         self.gaddr = gaddr
195         self.len = len
196         self.ip_saddr = ip_address(unicode(self.saddr))
197         self.ip_gaddr = ip_address(unicode(self.gaddr))
198
199     def encode(self):
200
201         if 6 is self.ip_saddr.version:
202             prefix = {
203                 'af': VppEnum.vl_api_address_family_t.ADDRESS_IP6,
204                 'grp_address': {
205                     'ip6': {
206                         'address': self.ip_gaddr.packed
207                     },
208                 },
209                 'src_address': {
210                     'ip6': {
211                         'address': self.ip_saddr.packed
212                     },
213                 },
214                 'grp_address_length': self.len,
215             }
216         else:
217             prefix = {
218                 'af': VppEnum.vl_api_address_family_t.ADDRESS_IP4,
219                 'grp_address': {
220                     'ip4': {
221                         'address': self.ip_gaddr.packed
222                     },
223                 },
224                 'src_address': {
225                     'ip4': {
226                         'address': self.ip_saddr.packed
227                     },
228                 },
229                 'grp_address_length': self.len,
230             }
231         return prefix