ip: Replace Sematics for Interface IP addresses
[vpp.git] / test / vpp_neighbor.py
1 """
2   Neighbour Entries
3
4   object abstractions for ARP and ND
5 """
6
7 from ipaddress import ip_address
8 from vpp_object import VppObject
9 from vpp_papi import mac_pton, VppEnum
10 try:
11     text_type = unicode
12 except NameError:
13     text_type = str
14
15
16 def find_nbr(test, sw_if_index, nbr_addr, is_static=0, mac=None):
17     ip_addr = ip_address(text_type(nbr_addr))
18     e = VppEnum.vl_api_ip_neighbor_flags_t
19     if 6 == ip_addr.version:
20         af = VppEnum.vl_api_address_family_t.ADDRESS_IP6
21     else:
22         af = VppEnum.vl_api_address_family_t.ADDRESS_IP4
23     nbrs = test.vapi.ip_neighbor_dump(sw_if_index=sw_if_index, af=af)
24
25     for n in nbrs:
26         if ip_addr == n.neighbor.ip_address and \
27            is_static == (n.neighbor.flags & e.IP_API_NEIGHBOR_FLAG_STATIC):
28             if mac:
29                 if mac == str(n.neighbor.mac_address):
30                     return True
31             else:
32                 return True
33     return False
34
35
36 class VppNeighbor(VppObject):
37     """
38     ARP Entry
39     """
40
41     def __init__(self, test, sw_if_index, mac_addr, nbr_addr,
42                  is_static=False, is_no_fib_entry=False):
43         self._test = test
44         self.sw_if_index = sw_if_index
45         self.mac_addr = mac_addr
46         self.nbr_addr = nbr_addr
47
48         e = VppEnum.vl_api_ip_neighbor_flags_t
49         self.flags = e.IP_API_NEIGHBOR_FLAG_NONE
50         if is_static:
51             self.flags |= e.IP_API_NEIGHBOR_FLAG_STATIC
52         if is_no_fib_entry:
53             self.flags |= e.IP_API_NEIGHBOR_FLAG_NO_FIB_ENTRY
54
55     def add_vpp_config(self):
56         r = self._test.vapi.ip_neighbor_add_del(
57             self.sw_if_index,
58             self.mac_addr,
59             self.nbr_addr,
60             is_add=1,
61             flags=self.flags)
62         self.stats_index = r.stats_index
63         self._test.registry.register(self, self._test.logger)
64         return self
65
66     def remove_vpp_config(self):
67         self._test.vapi.ip_neighbor_add_del(
68             self.sw_if_index,
69             self.mac_addr,
70             self.nbr_addr,
71             is_add=0,
72             flags=self.flags)
73
74     def is_static(self):
75         e = VppEnum.vl_api_ip_neighbor_flags_t
76         return (self.flags & e.IP_API_NEIGHBOR_FLAG_STATIC)
77
78     def query_vpp_config(self):
79         return find_nbr(self._test,
80                         self.sw_if_index,
81                         self.nbr_addr,
82                         self.is_static())
83
84     def object_id(self):
85         return ("%d:%s" % (self.sw_if_index, self.nbr_addr))
86
87     def get_stats(self):
88         c = self._test.statistics.get_counter("/net/adjacency")
89         return c[0][self.stats_index]