make test: improve documentation and PEP8 compliance
[vpp.git] / test / vpp_ip_route.py
1 """
2   IP Routes
3
4   object abstractions for representing IP routes in VPP
5 """
6
7 import socket
8
9 # from vnet/vnet/mpls/mpls_types.h
10 MPLS_IETF_MAX_LABEL = 0xfffff
11 MPLS_LABEL_INVALID = MPLS_IETF_MAX_LABEL + 1
12
13
14 class RoutePath:
15
16     def __init__(
17             self,
18             nh_addr,
19             nh_sw_if_index,
20             nh_table_id=0,
21             labels=[],
22             nh_via_label=MPLS_LABEL_INVALID):
23         self.nh_addr = socket.inet_pton(socket.AF_INET, nh_addr)
24         self.nh_itf = nh_sw_if_index
25         self.nh_table_id = nh_table_id
26         self.nh_via_label = nh_via_label
27         self.nh_labels = labels
28
29
30 class IpRoute:
31     """
32     IP Route
33     """
34
35     def __init__(self, test, dest_addr,
36                  dest_addr_len, paths, table_id=0):
37         self._test = test
38         self.paths = paths
39         self.dest_addr = socket.inet_pton(socket.AF_INET, dest_addr)
40         self.dest_addr_len = dest_addr_len
41         self.table_id = table_id
42
43     def add_vpp_config(self):
44         for path in self.paths:
45             self._test.vapi.ip_add_del_route(
46                 self.dest_addr,
47                 self.dest_addr_len,
48                 path.nh_addr,
49                 path.nh_itf,
50                 table_id=self.table_id,
51                 next_hop_out_label_stack=path.nh_labels,
52                 next_hop_n_out_labels=len(
53                     path.nh_labels),
54                 next_hop_via_label=path.nh_via_label)
55
56     def remove_vpp_config(self):
57         for path in self.paths:
58             self._test.vapi.ip_add_del_route(self.dest_addr,
59                                              self.dest_addr_len,
60                                              path.nh_addr,
61                                              path.nh_itf,
62                                              table_id=self.table_id,
63                                              is_add=0)
64
65
66 class MplsIpBind:
67     """
68     MPLS to IP Binding
69     """
70
71     def __init__(self, test, local_label, dest_addr, dest_addr_len):
72         self._test = test
73         self.dest_addr = socket.inet_pton(socket.AF_INET, dest_addr)
74         self.dest_addr_len = dest_addr_len
75         self.local_label = local_label
76
77     def add_vpp_config(self):
78         self._test.vapi.mpls_ip_bind_unbind(self.local_label,
79                                             self.dest_addr,
80                                             self.dest_addr_len)
81
82     def remove_vpp_config(self):
83         self._test.vapi.mpls_ip_bind_unbind(self.local_label,
84                                             self.dest_addr,
85                                             self.dest_addr_len,
86                                             is_bind=0)
87
88
89 class MplsRoute:
90     """
91     MPLS Route
92     """
93
94     def __init__(self, test, local_label, eos_bit, paths, table_id=0):
95         self._test = test
96         self.paths = paths
97         self.local_label = local_label
98         self.eos_bit = eos_bit
99         self.table_id = table_id
100
101     def add_vpp_config(self):
102         for path in self.paths:
103             self._test.vapi.mpls_route_add_del(
104                 self.local_label,
105                 self.eos_bit,
106                 1,
107                 path.nh_addr,
108                 path.nh_itf,
109                 table_id=self.table_id,
110                 next_hop_out_label_stack=path.nh_labels,
111                 next_hop_n_out_labels=len(
112                     path.nh_labels),
113                 next_hop_via_label=path.nh_via_label,
114                 next_hop_table_id=path.nh_table_id)
115
116     def remove_vpp_config(self):
117         for path in self.paths:
118             self._test.vapi.mpls_route_add_del(self.local_label,
119                                                self.eos_bit,
120                                                1,
121                                                path.nh_addr,
122                                                path.nh_itf,
123                                                table_id=self.table_id,
124                                                is_add=0)