HC Test: update HC config file locations
[csit.git] / resources / libraries / python / Memif.py
1 # Copyright (c) 2017 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 """Memif interface library."""
15
16 from resources.libraries.python.ssh import SSH
17 from resources.libraries.python.VatExecutor import VatExecutor, VatTerminal
18
19
20 class Memif(object):
21     """Memif interface class."""
22
23     def __init__(self):
24         pass
25
26     @staticmethod
27     def create_memif_interface(node, socket, mid, role="master"):
28         """Create Memif interface on the given node.
29
30         :param node: Given node to create Memif interface on.
31         :param socket: Memif interface socket path.
32         :param mid: Memif interface ID.
33         :param role: Memif interface role [master|slave]. Default is master.
34         :type node: dict
35         :type socket: str
36         :type mid: str
37         :type role: str
38         :returns: SW interface index.
39         :rtype: int
40         :raises ValueError: If command 'create memif' fails.
41         """
42
43         with VatTerminal(node, json_param=False) as vat:
44             vat.vat_terminal_exec_cmd_from_template(
45                 'memif_create.vat',
46                 socket=socket, id=mid, role=role)
47             if 'sw_if_index' in vat.vat_stdout:
48                 try:
49                     return int(vat.vat_stdout.split()[4])
50                 except KeyError:
51                     raise ValueError('Create Memif interface failed on node '
52                                      '{}"'.format(node['host']))
53             else:
54                 raise ValueError('Create Memif interface failed on node '
55                                  '{}"'.format(node['host']))
56
57     @staticmethod
58     def show_memif(node):
59         """Show Memif data for the given node.
60
61         :param node: Given node to show Memif data on.
62         :type node: dict
63         """
64         vat = VatExecutor()
65         vat.execute_script("memif_dump.vat", node, json_out=False)
66
67     @staticmethod
68     def clear_memif_socks(node, *socks):
69         """Clear Memif sockets for the given node.
70
71         :param node: Given node to clear Memif sockets on.
72         :param socks: Memif sockets.
73         :type node: dict
74         :type socks: list
75         """
76         ssh = SSH()
77         ssh.connect(node)
78
79         for sock in socks:
80             ssh.exec_command_sudo('rm -f {}'.format(sock))