FIX: Remove old restart sequence - Honeycomb
[csit.git] / resources / libraries / python / Tap.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 """Tap utilities library."""
15
16 from resources.libraries.python.VatExecutor import VatTerminal
17 from resources.libraries.python.InterfaceUtil import InterfaceUtil
18 from resources.libraries.python.topology import Topology
19
20
21 class Tap(object):
22     """Tap utilities."""
23
24     @staticmethod
25     def add_tap_interface(node, tap_name, mac=None):
26         """Add tap interface with name and optionally with MAC.
27
28         :param node: Node to add tap on.
29         :param tap_name: Tap interface name for linux tap.
30         :param mac: Optional MAC address for VPP tap.
31         :type node: dict
32         :type tap_name: str
33         :type mac: str
34         :returns: Returns a interface index.
35         :rtype: int
36         """
37         command = 'create'
38         if mac is not None:
39             args = 'tapname {} mac {}'.format(tap_name, mac)
40         else:
41             args = 'tapname {}'.format(tap_name)
42         with VatTerminal(node) as vat:
43             resp = vat.vat_terminal_exec_cmd_from_template('tap.vat',
44                                                            tap_command=command,
45                                                            tap_arguments=args)
46         sw_if_idx = resp[0]['sw_if_index']
47         if_key = Topology.add_new_port(node, 'tap')
48         Topology.update_interface_sw_if_index(node, if_key, sw_if_idx)
49         ifc_name = Tap.vpp_get_tap_interface_name(node, sw_if_idx)
50         Topology.update_interface_name(node, if_key, ifc_name)
51         if mac is None:
52             mac = Tap.vpp_get_tap_interface_mac(node, sw_if_idx)
53         Topology.update_interface_mac_address(node, if_key, mac)
54         Topology.update_interface_tap_dev_name(node, if_key, tap_name)
55
56         return sw_if_idx
57
58     @staticmethod
59     def modify_tap_interface(node, if_index, tap_name, mac=None):
60         """Modify tap interface like linux interface name or VPP MAC.
61
62         :param node: Node to modify tap on.
63         :param if_index: Index of tap interface to be modified.
64         :param tap_name: Tap interface name for linux tap.
65         :param mac: Optional MAC address for VPP tap.
66         :type node: dict
67         :type if_index: int
68         :type tap_name: str
69         :type mac: str
70         :returns: Returns a interface index.
71         :rtype: int
72         """
73         command = 'modify'
74         if mac is not None:
75             args = 'sw_if_index {} tapname {} mac {}'.format(
76                 if_index, tap_name, mac)
77         else:
78             args = 'sw_if_index {} tapname {}'.format(if_index, tap_name)
79         with VatTerminal(node) as vat:
80             resp = vat.vat_terminal_exec_cmd_from_template('tap.vat',
81                                                            tap_command=command,
82                                                            tap_arguments=args)
83         if_key = Topology.get_interface_by_sw_index(node, if_index)
84         Topology.update_interface_tap_dev_name(node, if_key, tap_name)
85         if mac:
86             Topology.update_interface_mac_address(node, if_key, mac)
87
88         return resp[0]['sw_if_index']
89
90     @staticmethod
91     def delete_tap_interface(node, if_index):
92         """Delete tap interface.
93
94         :param node: Node to delete tap on.
95         :param if_index: Index of tap interface to be deleted.
96         :type node: dict
97         :type if_index: int
98         :raises RuntimeError: Deletion was not successful.
99         """
100         command = 'delete'
101         args = 'sw_if_index {}'.format(if_index)
102         with VatTerminal(node) as vat:
103             resp = vat.vat_terminal_exec_cmd_from_template('tap.vat',
104                                                            tap_command=command,
105                                                            tap_arguments=args)
106             if int(resp[0]['retval']) != 0:
107                 raise RuntimeError(
108                     'Could not remove tap interface: {}'.format(resp))
109         if_key = Topology.get_interface_sw_index(node, if_index)
110         Topology.remove_port(node, if_key)
111
112     @staticmethod
113     def check_tap_present(node, tap_name):
114         """Check whether specific tap interface exists.
115
116         :param node: Node to check tap on.
117         :param tap_name: Tap interface name for linux tap.
118         :type node: dict
119         :type tap_name: str
120         :raises RuntimeError: Specified interface was not found.
121         """
122         tap_if = InterfaceUtil.tap_dump(node, tap_name)
123         if not tap_if:
124             raise RuntimeError(
125                 'Tap interface :{} does not exist'.format(tap_name))
126
127     @staticmethod
128     def vpp_get_tap_interface_name(node, sw_if_idx):
129         """Get VPP tap interface name from hardware interfaces dump.
130
131         :param node: DUT node.
132         :param sw_if_idx: DUT node.
133         :type node: dict
134         :type sw_if_idx: int
135         :returns: VPP tap interface name.
136         :rtype: str
137         """
138         with VatTerminal(node, json_param=False) as vat:
139             response = vat.vat_terminal_exec_cmd_from_template(
140                 'show_hardware_detail.vat')
141
142         for line in str(response[0]).splitlines():
143             if line.startswith('tap-'):
144                 line_split = line.split()
145                 if line_split[1] == sw_if_idx:
146                     return line_split[0]
147
148         return None
149
150     @staticmethod
151     def vpp_get_tap_interface_mac(node, sw_if_idx):
152         """Get tap interface MAC address from hardware interfaces dump.
153
154         :param node: DUT node.
155         :param sw_if_idx: DUT node.
156         :type node: dict
157         :type sw_if_idx: int
158         :returns: Tap interface MAC address.
159         :rtype: str
160         """
161         with VatTerminal(node, json_param=False) as vat:
162             response = vat.vat_terminal_exec_cmd_from_template(
163                 'show_hardware_detail.vat')
164
165         tap_if_match = False
166         for line in str(response[0]).splitlines():
167             if tap_if_match:
168                 line_split = line.split()
169                 return line_split[-1]
170             if line.startswith('tap-'):
171                 line_split = line.split()
172                 if line_split[1] == sw_if_idx:
173                     tap_if_match = True
174
175         return None