Fix Tap failing tests
[csit.git] / resources / libraries / python / Routing.py
1 # Copyright (c) 2018 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     """Routing utilities."""
23
24     @staticmethod
25     def vpp_route_add(node, network, prefix_len, **kwargs):
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 kwargs: Optional key-value arguments:
32
33             gateway: Route gateway address. (str)
34             interface: Route interface. (str)
35             vrf: VRF table ID. (int)
36             use_sw_index: Use sw_if_index in VAT command. (bool)
37             resolve_attempts: Resolve attempts IP route add parameter. (int)
38             count: number of IP addresses to add starting from network IP (int)
39             local: The route is local with same prefix (increment is 1).
40                 If None, then is not used. (bool)
41             lookup_vrf: VRF table ID for lookup. (int)
42             multipath: Enable multipath routing. (bool)
43             weight: Weight value for unequal cost multipath routing. (int)
44
45         :type node: dict
46         :type network: str
47         :type prefix_len: int
48         :type kwargs: dict
49         """
50
51         gateway = kwargs.get("gateway", '')
52         intf = kwargs.get("interface", '')
53         use_sw_index = kwargs.get("use_sw_index", True)
54         resolve_attempts = kwargs.get("resolve_attempts", 10)
55         count = kwargs.get("count", 1)
56         vrf = kwargs.get("vrf", None)
57         l_vrf = kwargs.get("lookup_vrf", None)
58         multipath = kwargs.get("multipath", False)
59         weight = kwargs.get("weight", None)
60         local = kwargs.get("local", False)
61
62         if intf:
63             if use_sw_index:
64                 int_cmd = ('sw_if_index {}'.
65                            format(Topology.get_interface_sw_index(node, intf)))
66             else:
67                 int_cmd = intf
68         else:
69             int_cmd = ''
70
71         with VatTerminal(node, json_param=False) as vat:
72             vat.vat_terminal_exec_cmd_from_template(
73                 'add_route.vat',
74                 network=network,
75                 prefix_length=prefix_len,
76                 via='via {}'.format(gateway) if gateway else '',
77                 vrf='vrf {}'.format(vrf) if vrf else '',
78                 interface=int_cmd,
79                 resolve_attempts='resolve-attempts {}'.
80                 format(resolve_attempts) if resolve_attempts else '',
81                 count='count {}'.format(count) if count else '',
82                 lookup_vrf='lookup-in-vrf {}'.format(l_vrf) if l_vrf else '',
83                 multipath='multipath' if multipath else '',
84                 weight='weight {}'.format(weight) if weight else '',
85                 local='local' if local else '')
86
87     @staticmethod
88     def add_fib_table(node, table_id, ipv6=False):
89         """Create new FIB table according to ID.
90
91         :param node: Node to add FIB on.
92         :param table_id: FIB table ID.
93         :param ipv6: Is this an IPv6 table
94         :type node: dict
95         :type table_id: int
96         :type ipv6: bool
97         """
98         with VatTerminal(node) as vat:
99             vat.vat_terminal_exec_cmd_from_template('add_fib_table.vat',
100                                                     table_id=table_id,
101                                                     ipv6="ipv6" if ipv6 else "")
102
103     @staticmethod
104     def add_route(node, ip_addr, prefix, gateway, namespace=None):
105         """Add route in namespace.
106
107         :param node: Node where to execute command.
108         :param ip_addr: Route destination IP address.
109         :param prefix: IP prefix.
110         :param namespace: Execute command in namespace. Optional.
111         :param gateway: Gateway address.
112         :type node: dict
113         :type ip_addr: str
114         :type prefix: int
115         :type gateway: str
116         :type namespace: str
117         """
118         if namespace is not None:
119             cmd = 'ip netns exec {} ip route add {}/{} via {}'.format(
120                 namespace, ip_addr, prefix, gateway)
121         else:
122             cmd = 'ip route add {}/{} via {}'.format(ip_addr, prefix, gateway)
123         exec_cmd_no_error(node, cmd, sudo=True)