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