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