2 from ipaddress import ip_network
4 from vpp_object import VppObject
7 class VppLispLocatorSet(VppObject):
8 """ Represents LISP locator set in VPP """
10 def __init__(self, test, ls_name):
12 self._ls_name = ls_name
22 def add_vpp_config(self):
23 self.test.vapi.lisp_add_del_locator_set(locator_set_name=self._ls_name)
24 self._test.registry.register(self, self.test.logger)
26 def get_lisp_locator_sets_dump_entry(self):
27 result = self.test.vapi.lisp_locator_set_dump()
29 if ls.ls_name.strip('\x00') == self._ls_name:
33 def query_vpp_config(self):
34 return self.get_lisp_locator_sets_dump_entry() is not None
36 def remove_vpp_config(self):
37 self.test.vapi.lisp_add_del_locator_set(locator_set_name=self._ls_name,
41 return 'lisp-locator-set-%s' % self._ls_name
44 class VppLispLocator(VppObject):
45 """ Represents LISP locator in VPP """
47 def __init__(self, test, sw_if_index, ls_name, priority=1, weight=1):
49 self._sw_if_index = sw_if_index
50 self._ls_name = ls_name
51 self._priority = priority
56 """ Test which created this locator """
61 """ Locator set name """
65 def sw_if_index(self):
66 return self._sw_if_index
76 def add_vpp_config(self):
77 self.test.vapi.lisp_add_del_locator(locator_set_name=self._ls_name,
78 sw_if_index=self._sw_if_index,
79 priority=self._priority,
81 self._test.registry.register(self, self.test.logger)
83 def get_lisp_locator_dump_entry(self):
84 locators = self.test.vapi.lisp_locator_dump(
85 is_index_set=0, ls_name=self._ls_name)
86 for locator in locators:
87 if locator.sw_if_index == self._sw_if_index:
91 def query_vpp_config(self):
92 locator = self.get_lisp_locator_dump_entry()
93 return locator is not None
95 def remove_vpp_config(self):
96 self.test.vapi.lisp_add_del_locator(
97 locator_set_name=self._ls_name, sw_if_index=self._sw_if_index,
98 priority=self._priority, weight=self._weight, is_add=0)
99 self._test.registry.register(self, self.test.logger)
102 return 'lisp-locator-%s-%d' % (self._ls_name, self._sw_if_index)
105 class LispEIDType(object):
111 class LispKeyIdType(object):
117 class LispEID(object):
118 """ Lisp endpoint identifier """
119 def __init__(self, eid):
123 # find out whether EID is ip prefix, or MAC
125 self.prefix = ip_network(self.eid)
126 self._type = LispEIDType.PREFIX
129 if self.eid.count(":") == 5: # MAC address
131 self._type = LispEIDType.MAC
133 raise Exception('Unsupported EID format {!s}!'.format(eid))
141 if self.eid_type == LispEIDType.PREFIX:
143 elif self.eid_type == LispEIDType.MAC:
145 elif self.eid_type == LispEIDType.NSH:
146 return Exception('Unimplemented')
150 if self.eid_type == LispEIDType.PREFIX:
151 return {"type": self._type, "address": {"prefix": self.prefix}}
152 elif self.eid_type == LispEIDType.MAC:
153 return {"type": self._type, "address": {"mac": self.mac}}
154 elif self.eid_type == LispEIDType.NSH:
155 return Exception('Unimplemented')
158 class LispKey(object):
160 def __init__(self, key_type, key):
161 self._key_type = key_type
166 return {"id": self._key_type, "key": self._key}
169 class VppLispMapping(VppObject):
170 """ Represents common features for remote and local LISP mapping in VPP """
172 def __init__(self, test, eid, vni=0, priority=1, weight=1):
173 self._eid = LispEID(eid)
175 self._priority = priority
176 self._weight = weight
193 return self._priority
199 def get_lisp_mapping_dump_entry(self):
200 return self.test.vapi.lisp_eid_table_dump(
201 eid_set=1, vni=self._vni, eid=self._eid.packed)
203 def query_vpp_config(self):
204 mapping = self.get_lisp_mapping_dump_entry()
208 return 'lisp-mapping-[%s]-%s-%s-%s' % (
209 self.vni, self.eid.address, self.priority, self.weight)
212 class VppLocalMapping(VppLispMapping):
213 """ LISP Local mapping """
214 def __init__(self, test, eid, ls_name, vni=0, priority=1, weight=1,
215 key_id=LispKeyIdType.NONE, key=''):
216 super(VppLocalMapping, self).__init__(test, eid, vni, priority, weight)
217 self._ls_name = ls_name
218 self._key = LispKey(key_id, key)
232 def add_vpp_config(self):
233 self.test.vapi.lisp_add_del_local_eid(
234 locator_set_name=self._ls_name, eid=self._eid.packed,
235 vni=self._vni, key=self._key.packed)
236 self._test.registry.register(self, self.test.logger)
238 def remove_vpp_config(self):
239 self.test.vapi.lisp_add_del_local_eid(
240 locator_set_name=self._ls_name, eid=self._eid.packed,
241 vni=self._vni, is_add=0)
244 return 'lisp-eid-local-mapping-%s[%d]' % (self._eid.address, self._vni)
247 class LispRemoteLocator(object):
248 def __init__(self, addr, priority=1, weight=1):
250 self.priority = priority
255 return {"priority": self.priority, "weight": self.weight,
256 "ip_address": self.addr}
259 class VppRemoteMapping(VppLispMapping):
261 def __init__(self, test, eid, rlocs=None, vni=0, priority=1, weight=1):
262 super(VppRemoteMapping, self).__init__(test, eid, vni, priority,
269 for rloc in self._rlocs:
270 rlocs.append(rloc.packed)
273 def add_vpp_config(self):
274 self.test.vapi.lisp_add_del_remote_mapping(
275 rlocs=self.rlocs, deid=self._eid.packed,
276 vni=self._vni, rloc_num=len(self._rlocs))
277 self._test.registry.register(self, self.test.logger)
279 def remove_vpp_config(self):
280 self.test.vapi.lisp_add_del_remote_mapping(
281 deid=self._eid.packed, vni=self._vni, is_add=0, rloc_num=0)
284 return 'lisp-eid-remote-mapping-%s[%d]' % (self._eid.address,
288 class VppLispAdjacency(VppObject):
289 """ Represents LISP adjacency in VPP """
291 def __init__(self, test, leid, reid, vni=0):
292 self._leid = LispEID(leid)
293 self._reid = LispEID(reid)
294 if self._leid.eid_type != self._reid.eid_type:
295 raise Exception('remote and local EID are different types!')
315 def add_vpp_config(self):
316 self.test.vapi.lisp_add_del_adjacency(
317 leid=self._leid.packed, reid=self._reid.packed, vni=self._vni)
318 self._test.registry.register(self, self.test.logger)
321 def eid_equal(eid, eid_api):
322 if eid.eid_type != eid_api.type:
325 if eid_api.type == LispEIDType.PREFIX:
326 if eid.address.prefixlen != eid_api.address.prefix.prefixlen:
329 if eid.address != eid_api.address:
334 def query_vpp_config(self):
335 res = self.test.vapi.lisp_adjacencies_get(vni=self._vni)
336 for adj in res.adjacencies:
337 if self.eid_equal(self._leid, adj.leid) and \
338 self.eid_equal(self._reid, adj.reid):
342 def remove_vpp_config(self):
343 self.test.vapi.lisp_add_del_adjacency(
344 leid=self._leid.packed, reid=self._reid.packed,
345 vni=self._vni, is_add=0)
348 return 'lisp-adjacency-%s-%s[%d]' % (self._leid, self._reid, self._vni)