stats: python vpp_stats rewrite to access stat segment directly
[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     nbrs = test.vapi.ip_neighbor_dump(sw_if_index=sw_if_index,
20                                       af=ip_addr.vapi_af)
21
22     for n in nbrs:
23         if sw_if_index == n.neighbor.sw_if_index and \
24            ip_addr == n.neighbor.ip_address and \
25            is_static == (n.neighbor.flags & e.IP_API_NEIGHBOR_FLAG_STATIC):
26             if mac:
27                 if mac == str(n.neighbor.mac_address):
28                     return True
29             else:
30                 return True
31     return False
32
33
34 class VppNeighbor(VppObject):
35     """
36     ARP Entry
37     """
38
39     def __init__(self, test, sw_if_index, mac_addr, nbr_addr,
40                  is_static=False, is_no_fib_entry=False):
41         self._test = test
42         self.sw_if_index = sw_if_index
43         self.mac_addr = mac_addr
44         self.nbr_addr = nbr_addr
45
46         e = VppEnum.vl_api_ip_neighbor_flags_t
47         self.flags = e.IP_API_NEIGHBOR_FLAG_NONE
48         if is_static:
49             self.flags |= e.IP_API_NEIGHBOR_FLAG_STATIC
50         if is_no_fib_entry:
51             self.flags |= e.IP_API_NEIGHBOR_FLAG_NO_FIB_ENTRY
52
53     def add_vpp_config(self):
54         r = self._test.vapi.ip_neighbor_add_del(
55             self.sw_if_index,
56             self.mac_addr,
57             self.nbr_addr,
58             is_add=1,
59             flags=self.flags)
60         self.stats_index = r.stats_index
61         self._test.registry.register(self, self._test.logger)
62         return self
63
64     def remove_vpp_config(self):
65         self._test.vapi.ip_neighbor_add_del(
66             self.sw_if_index,
67             self.mac_addr,
68             self.nbr_addr,
69             is_add=0,
70             flags=self.flags)
71
72     def is_static(self):
73         e = VppEnum.vl_api_ip_neighbor_flags_t
74         return (self.flags & e.IP_API_NEIGHBOR_FLAG_STATIC)
75
76     def query_vpp_config(self):
77         return find_nbr(self._test,
78                         self.sw_if_index,
79                         self.nbr_addr,
80                         self.is_static())
81
82     def object_id(self):
83         return ("%d:%s" % (self.sw_if_index, self.nbr_addr))
84
85     def get_stats(self):
86         c = self._test.statistics["/net/adjacency"]
87         return c[0][self.stats_index]