CSIT-748 vnf-agent integration
[csit.git] / resources / libraries / python / Tap.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 """Tap utilities library."""
15
16 from resources.libraries.python.VatExecutor import VatTerminal
17 from resources.libraries.python.InterfaceUtil import InterfaceUtil
18
19
20 class Tap(object):
21     """Tap utilities."""
22
23     @staticmethod
24     def add_tap_interface(node, tap_name, mac=None):
25         """Add tap interface with name and optionally with MAC.
26
27         :param node: Node to add tap on.
28         :param tap_name: Tap interface name for linux tap.
29         :param mac: Optional MAC address for VPP tap.
30         :type node: dict
31         :type tap_name: str
32         :type mac: str
33         :return: Returns a interface index.
34         :rtype: int
35         """
36         command = 'connect'
37         if mac is not None:
38             args = 'tapname {} mac {}'.format(tap_name, mac)
39         else:
40             args = 'tapname {}'.format(tap_name)
41         with VatTerminal(node) as vat:
42             resp = vat.vat_terminal_exec_cmd_from_template('tap.vat',
43                                                            tap_command=command,
44                                                            tap_arguments=args)
45         return resp[0]['sw_if_index']
46
47     @staticmethod
48     def modify_tap_interface(node, if_index, tap_name, mac=None):
49         """Modify tap interface like linux interface name or VPP MAC.
50
51         :param node: Node to modify tap on.
52         :param if_index: Index of tap interface to be modified.
53         :param tap_name: Tap interface name for linux tap.
54         :param mac: Optional MAC address for VPP tap.
55         :type node: dict
56         :type if_index: int
57         :type tap_name: str
58         :type mac: str
59         :return: Returns a interface index.
60         :rtype: int
61         """
62         command = 'modify'
63         if mac is not None:
64             args = 'sw_if_index {} tapname {} mac {}'.format(
65                 if_index, tap_name, mac)
66         else:
67             args = 'sw_if_index {} tapname {}'.format(if_index, tap_name)
68         with VatTerminal(node) as vat:
69             resp = vat.vat_terminal_exec_cmd_from_template('tap.vat',
70                                                            tap_command=command,
71                                                            tap_arguments=args)
72         return resp[0]['sw_if_index']
73
74     @staticmethod
75     def delete_tap_interface(node, if_index):
76         """Delete tap interface.
77
78         :param node: Node to delete tap on.
79         :param if_index: Index of tap interface to be deleted.
80         :type node: dict
81         :type if_index: int
82         :raises RuntimeError: Deletion was not successful.
83         """
84         command = 'delete'
85         args = 'sw_if_index {}'.format(if_index)
86         with VatTerminal(node) as vat:
87             resp = vat.vat_terminal_exec_cmd_from_template('tap.vat',
88                                                            tap_command=command,
89                                                            tap_arguments=args)
90             if int(resp[0]['retval']) != 0:
91                 raise RuntimeError(
92                     'Could not remove tap interface: {}'.format(resp))
93
94     @staticmethod
95     def check_tap_present(node, tap_name):
96         """Check whether specific tap interface exists.
97
98         :param node: Node to check tap on.
99         :param tap_name: Tap interface name for linux tap.
100         :type node: dict
101         :type tap_name: str
102         :raises RuntimeError: Specified interface was not found.
103         """
104         tap_if = InterfaceUtil.tap_dump(node, tap_name)
105         if len(tap_if) == 0:
106             raise RuntimeError(
107                 'Tap interface :{} does not exist'.format(tap_name))