Python3: resources and libraries
[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 PapiSocketExecutor
19 from resources.libraries.python.topology import NodeType, Topology
20 from resources.libraries.python.InterfaceUtil import InterfaceUtil
21
22
23 class VhostUser:
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 = u"sw_interface_vhost_user_dump"
37
38         with PapiSocketExecutor(node) as papi_exec:
39             details = papi_exec.add(cmd).get_details()
40
41         for vhost in details:
42             vhost[u"interface_name"] = vhost[u"interface_name"]
43             vhost[u"sock_filename"] = vhost[u"sock_filename"]
44
45         logger.debug(f"VhostUser details:\n{details}")
46
47         return details
48
49     @staticmethod
50     def vpp_create_vhost_user_interface(node, socket):
51         """Create Vhost-user interface on VPP node.
52
53         :param node: Node to create Vhost-user interface on.
54         :param socket: Vhost-user interface socket path.
55         :type node: dict
56         :type socket: str
57         :returns: SW interface index.
58         :rtype: int
59         """
60         cmd = u"create_vhost_user_if"
61         err_msg = f"Failed to create Vhost-user interface " \
62             f"on host {node[u'host']}"
63         args = dict(
64             sock_filename=str(socket).encode(encoding=u"utf-8")
65         )
66
67         with PapiSocketExecutor(node) as papi_exec:
68             sw_if_index = papi_exec.add(cmd, **args).get_sw_if_index(err_msg)
69
70         # Update the Topology:
71         if_key = Topology.add_new_port(node, u"vhost")
72         Topology.update_interface_sw_if_index(node, if_key, sw_if_index)
73
74         ifc_name = InterfaceUtil.vpp_get_interface_name(node, sw_if_index)
75         Topology.update_interface_name(node, if_key, ifc_name)
76
77         ifc_mac = InterfaceUtil.vpp_get_interface_mac(node, sw_if_index)
78         Topology.update_interface_mac_address(node, if_key, ifc_mac)
79
80         Topology.update_interface_vhost_socket(node, if_key, socket)
81
82         return sw_if_index
83
84     @staticmethod
85     def get_vhost_user_if_name_by_sock(node, socket):
86         """Get Vhost-user interface name by socket.
87
88         :param node: Node to get Vhost-user interface name on.
89         :param socket: Vhost-user interface socket path.
90         :type node: dict
91         :type socket: str
92         :returns: Interface name or None if not found.
93         :rtype: str
94         """
95         for interface in node[u"interfaces"].values():
96             if interface.get(u"socket") == socket:
97                 return interface.get(u"name")
98         return None
99
100     @staticmethod
101     def get_vhost_user_mac_by_sw_index(node, sw_if_index):
102         """Get Vhost-user l2_address for the given interface from actual
103         interface dump.
104
105         :param node: VPP node to get interface data from.
106         :param sw_if_index: SW index of the specific interface.
107         :type node: dict
108         :type sw_if_index: str
109         :returns: l2_address of the given interface.
110         :rtype: str
111         """
112         return InterfaceUtil.vpp_get_interface_mac(node, sw_if_index)
113
114     @staticmethod
115     def vpp_show_vhost(node):
116         """Get Vhost-user data for the given node.
117
118         :param node: VPP node to get interface data from.
119         :type node: dict
120         """
121         VhostUser._sw_interface_vhost_user_dump(node)
122
123     @staticmethod
124     def show_vpp_vhost_on_all_duts(nodes):
125         """Show Vhost-user on all DUTs.
126
127         :param nodes: VPP nodes.
128         :type nodes: dict
129         """
130         for node in nodes.values():
131             if node[u"type"] == NodeType.DUT:
132                 VhostUser.vpp_show_vhost(node)
133
134     @staticmethod
135     def vhost_user_dump(node):
136         """Get vhost-user data for the given node.
137
138         :param node: VPP node to get interface data from.
139         :type node: dict
140         :returns: List of dictionaries with all vhost-user interfaces.
141         :rtype: list
142         """
143         cmd = u"sw_interface_vhost_user_dump"
144         err_msg = f"Failed to get vhost-user dump on host {node['host']}"
145
146         with PapiSocketExecutor(node) as papi_exec:
147             details = papi_exec.add(cmd).get_details(err_msg)
148
149         logger.debug(f"Vhost-user details:\n{details}")
150         return details