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