767fb3aff035e5a8a7b7550bdf205adad7fd67b7
[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 class Routing(object):
21
22     """Routing utilities."""
23
24     @staticmethod
25     def vpp_route_add(node, network, prefix_len, gateway=None,
26                       interface=None, use_sw_index=True, resolve_attempts=10,
27                       count=1, vrf=None):
28         """Add route to the VPP node.
29
30         :param node: Node to add route on.
31         :param network: Route destination network address.
32         :param prefix_len: Route destination network prefix length.
33         :param gateway: Route gateway address.
34         :param interface: Route interface.
35         :param vrf: VRF table ID (Optional).
36         :param use_sw_index: Use sw_if_index in VAT command.
37         :param resolve_attempts: Resolve attempts IP route add parameter.
38         :param count: number of IP addresses to add starting from network IP
39         with same prefix (increment is 1). If None, then is not used.
40         :type node: dict
41         :type network: str
42         :type prefix_len: int
43         :type gateway: str
44         :type interface: str
45         :type use_sw_index: bool
46         :type resolve_attempts: int
47         :type count: int
48         :type vrf: int
49         """
50         if use_sw_index:
51             int_cmd = ('sw_if_index {}'.
52                        format(Topology.get_interface_sw_index(node, interface)))
53         else:
54             int_cmd = interface
55
56         rap = 'resolve-attempts {}'.format(resolve_attempts) \
57             if resolve_attempts else ''
58
59         via = 'via {}'.format(gateway) if gateway else ''
60
61         cnt = 'count {}'.format(count) \
62             if count else ''
63
64         vrf = 'vrf {}'.format(vrf) if vrf else ''
65
66         with VatTerminal(node, json_param=False) as vat:
67             vat.vat_terminal_exec_cmd_from_template('add_route.vat',
68                                                     network=network,
69                                                     prefix_length=prefix_len,
70                                                     via=via,
71                                                     vrf=vrf,
72                                                     interface=int_cmd,
73                                                     resolve_attempts=rap,
74                                                     count=cnt)
75
76     @staticmethod
77     def add_fib_table(node, network, prefix_len, fib_id, place):
78         """Create new FIB table according to ID.
79
80         :param node: Node to add FIB on.
81         :param network: IP address to add to the FIB table.
82         :param prefix_len: IP address prefix length.
83         :param fib_id: FIB table ID.
84         :param place: Possible variants are local, drop.
85         :type node: dict
86         :type network: str
87         :type prefix_len: int
88         :type fib_id: int
89         :type place: str
90         """
91         with VatTerminal(node) as vat:
92             vat.vat_terminal_exec_cmd_from_template('add_fib_table.vat',
93                                                     network=network,
94                                                     prefix_length=prefix_len,
95                                                     fib_number=fib_id,
96                                                     where=place)
97
98     @staticmethod
99     def add_route(node, ip, prefix, gw, namespace=None):
100         """Add route in namespace.
101
102         :param node: Node where to execute command.
103         :param ip: Route destination IP.
104         :param prefix: IP prefix.
105         :param namespace: Execute command in namespace. Optional
106         :param gw: Gateway.
107         :type node: dict
108         :type ip: str
109         :type prefix: int
110         :type gw: str
111         :type namespace: str
112         """
113         if namespace is not None:
114             cmd = 'ip netns exec {} ip route add {}/{} via {}'.format(
115                 namespace, ip, prefix, gw)
116         else:
117             cmd = 'ip route add {}/{} via {}'.format(ip, prefix, gw)
118         exec_cmd_no_error(node, cmd, sudo=True)