PapiExecutor always verifies
[csit.git] / resources / libraries / python / VhostUser.py
1 # Copyright (c) 2019 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 """Vhost-user interfaces library."""
15
16 from robot.api import logger
17
18 from resources.libraries.python.PapiExecutor import PapiExecutor
19 from resources.libraries.python.topology import NodeType, Topology
20 from resources.libraries.python.InterfaceUtil import InterfaceUtil
21
22
23 class VhostUser(object):
24     """Vhost-user interfaces L1 library."""
25
26     @staticmethod
27     def _sw_interface_vhost_user_dump(node):
28         """Get the Vhost-user dump on the given node.
29
30         :param node: Given node to get Vhost dump from.
31         :type node: dict
32         :returns: List of Vhost-user interfaces data extracted from Papi
33             response.
34         :rtype: list
35         """
36         cmd = "sw_interface_vhost_user_dump"
37         with PapiExecutor(node) as papi_exec:
38             details = papi_exec.add(cmd).get_details()
39
40         for vhost in details:
41             vhost["interface_name"] = vhost["interface_name"].rstrip('\x00')
42             vhost["sock_filename"] = vhost["sock_filename"].rstrip('\x00')
43
44         logger.debug("VhostUser details:\n{details}".format(details=details))
45
46         return details
47
48     @staticmethod
49     def vpp_create_vhost_user_interface(node, socket):
50         """Create Vhost-user interface on VPP node.
51
52         :param node: Node to create Vhost-user interface on.
53         :param socket: Vhost-user interface socket path.
54         :type node: dict
55         :type socket: str
56         :returns: SW interface index.
57         :rtype: int
58         """
59         cmd = 'create_vhost_user_if'
60         err_msg = 'Failed to create Vhost-user interface on host {host}'.format(
61             host=node['host'])
62         args = dict(
63             sock_filename=str(socket)
64         )
65         with PapiExecutor(node) as papi_exec:
66             sw_if_index = papi_exec.add(cmd, **args).get_sw_if_index(err_msg)
67
68         # Update the Topology:
69         if_key = Topology.add_new_port(node, 'vhost')
70         Topology.update_interface_sw_if_index(node, if_key, sw_if_index)
71
72         ifc_name = InterfaceUtil.vpp_get_interface_name(node, sw_if_index)
73         Topology.update_interface_name(node, if_key, ifc_name)
74
75         ifc_mac = InterfaceUtil.vpp_get_interface_mac(node, sw_if_index)
76         Topology.update_interface_mac_address(node, if_key, ifc_mac)
77
78         Topology.update_interface_vhost_socket(node, if_key, socket)
79
80         return sw_if_index
81
82     @staticmethod
83     def get_vhost_user_if_name_by_sock(node, socket):
84         """Get Vhost-user interface name by socket.
85
86         :param node: Node to get Vhost-user interface name on.
87         :param socket: Vhost-user interface socket path.
88         :type node: dict
89         :type socket: str
90         :returns: Interface name or None if not found.
91         :rtype: str
92         """
93         for interface in node['interfaces'].values():
94             if interface.get('socket') == socket:
95                 return interface.get('name')
96         return None
97
98     @staticmethod
99     def get_vhost_user_mac_by_sw_index(node, sw_if_index):
100         """Get Vhost-user l2_address for the given interface from actual
101         interface dump.
102
103         :param node: VPP node to get interface data from.
104         :param sw_if_index: SW index of the specific interface.
105         :type node: dict
106         :type sw_if_index: str
107         :returns: l2_address of the given interface.
108         :rtype: str
109         """
110
111         return InterfaceUtil.vpp_get_interface_mac(node, sw_if_index)
112
113     @staticmethod
114     def vpp_show_vhost(node):
115         """Get Vhost-user data for the given node.
116
117         :param node: VPP node to get interface data from.
118         :type node: dict
119         """
120         VhostUser._sw_interface_vhost_user_dump(node)
121
122     @staticmethod
123     def show_vpp_vhost_on_all_duts(nodes):
124         """Show Vhost-user on all DUTs.
125
126         :param nodes: VPP nodes.
127         :type nodes: dict
128         """
129         for node in nodes.values():
130             if node['type'] == NodeType.DUT:
131                 VhostUser.vpp_show_vhost(node)