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