ff675f39a99c0e234ce150619f0d1d607ebd3c91
[csit.git] / resources / libraries / python / Routing.py
1 # Copyright (c) 2016 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 """Routing utilities library."""
15
16 from resources.libraries.python.VatExecutor import VatTerminal
17 from resources.libraries.python.topology import Topology
18 from resources.libraries.python.ssh import exec_cmd_no_error
19
20
21 class Routing(object):
22
23     """Routing utilities."""
24
25     # pylint: disable=too-many-arguments
26     @staticmethod
27     def vpp_route_add(node, network, prefix_len, gateway=None,
28                       interface=None, use_sw_index=True, resolve_attempts=10,
29                       count=1, vrf=None, lookup_vrf=None):
30         """Add route to the VPP node.
31
32         :param node: Node to add route on.
33         :param network: Route destination network address.
34         :param prefix_len: Route destination network prefix length.
35         :param gateway: Route gateway address.
36         :param interface: Route interface.
37         :param vrf: VRF table ID (Optional).
38         :param use_sw_index: Use sw_if_index in VAT command.
39         :param resolve_attempts: Resolve attempts IP route add parameter.
40         :param count: number of IP addresses to add starting from network IP
41         with same prefix (increment is 1). If None, then is not used.
42         :param lookup_vrf: VRF table ID for lookup.
43         :type node: dict
44         :type network: str
45         :type prefix_len: int
46         :type gateway: str
47         :type interface: str
48         :type use_sw_index: bool
49         :type resolve_attempts: int
50         :type count: int
51         :type vrf: int
52         :type lookup_vrf: int
53         """
54         if use_sw_index:
55             int_cmd = ('sw_if_index {}'.
56                        format(Topology.get_interface_sw_index(node, interface)))
57         else:
58             int_cmd = interface
59
60         rap = 'resolve-attempts {}'.format(resolve_attempts) \
61             if resolve_attempts else ''
62
63         via = 'via {}'.format(gateway) if gateway else ''
64
65         cnt = 'count {}'.format(count) \
66             if count else ''
67
68         vrf = 'vrf {}'.format(vrf) if vrf else ''
69
70         lookup_vrf = 'lookup-in-vrf {}'.format(lookup_vrf) if lookup_vrf else ''
71
72         with VatTerminal(node, json_param=False) as vat:
73             vat.vat_terminal_exec_cmd_from_template('add_route.vat',
74                                                     network=network,
75                                                     prefix_length=prefix_len,
76                                                     via=via,
77                                                     vrf=vrf,
78                                                     interface=int_cmd,
79                                                     resolve_attempts=rap,
80                                                     count=cnt,
81                                                     lookup_vrf=lookup_vrf)
82
83     @staticmethod
84     def add_fib_table(node, network, prefix_len, fib_id, place):
85         """Create new FIB table according to ID.
86
87         :param node: Node to add FIB on.
88         :param network: IP address to add to the FIB table.
89         :param prefix_len: IP address prefix length.
90         :param fib_id: FIB table ID.
91         :param place: Possible variants are local, drop.
92         :type node: dict
93         :type network: str
94         :type prefix_len: int
95         :type fib_id: int
96         :type place: str
97         """
98         with VatTerminal(node) as vat:
99             vat.vat_terminal_exec_cmd_from_template('add_fib_table.vat',
100                                                     network=network,
101                                                     prefix_length=prefix_len,
102                                                     fib_number=fib_id,
103                                                     where=place)
104
105     @staticmethod
106     def add_route(node, ip_addr, prefix, gateway, namespace=None):
107         """Add route in namespace.
108
109         :param node: Node where to execute command.
110         :param ip_addr: Route destination IP address.
111         :param prefix: IP prefix.
112         :param namespace: Execute command in namespace. Optional.
113         :param gateway: Gateway address.
114         :type node: dict
115         :type ip_addr: str
116         :type prefix: int
117         :type gateway: str
118         :type namespace: str
119         """
120         if namespace is not None:
121             cmd = 'ip netns exec {} ip route add {}/{} via {}'.format(
122                 namespace, ip_addr, prefix, gateway)
123         else:
124             cmd = 'ip route add {}/{} via {}'.format(ip_addr, prefix, gateway)
125         exec_cmd_no_error(node, cmd, sudo=True)