LISP - implement changes done in VPP-376
[csit.git] / resources / libraries / python / VhostUser.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 """Vhost-user interfaces library."""
15
16 from resources.libraries.python.VatExecutor import VatExecutor, VatTerminal
17
18
19 class VhostUser(object):
20     """Vhost-user interfaces"""
21
22     @staticmethod
23     def vpp_create_vhost_user_interface(node, socket):
24         """Create Vhost-user interface on VPP node.
25
26         :param node: Node to create Vhost-user interface on.
27         :param socket: Vhost-user interface socket path.
28         :type node: dict
29         :type socket: str
30         :return: SW interface index.
31         :rtype: int
32         """
33         out = VatExecutor.cmd_from_template(node, "create_vhost_user_if.vat",
34                                             sock=socket)
35         if out[0].get('retval') == 0:
36             return out[0].get('sw_if_index')
37         else:
38             raise RuntimeError('Create Vhost-user interface failed on node '
39                                '"{}"'.format(node['host']))
40
41     @staticmethod
42     def get_vhost_user_if_name_by_sock(node, socket):
43         """Get Vhost-user interface name by socket.
44
45         :param node: Node to get Vhost-user interface name on.
46         :param socket: Vhost-user interface socket path.
47         :type node: dict
48         :type socket: str
49         :return: Interface name or None if not found.
50         :rtype: str
51         """
52         for interface in node['interfaces'].values():
53             if interface.get('socket') == socket:
54                 return interface.get('name')
55         return None
56
57     @staticmethod
58     def get_vhost_user_mac_by_sw_index(node, sw_if_index):
59         """Get Vhost-user l2_address for the given interface from actual
60         interface dump.
61
62         :param node: VPP node to get interface data from.
63         :param sw_if_index: Idx of the specific interface.
64         :type node: dict
65         :type sw_if_index: str
66         :return: l2_address of the given interface.
67         :rtype: str
68         """
69
70         with VatTerminal(node) as vat:
71             if_data = vat.vat_terminal_exec_cmd_from_template(
72                 "interface_dump.vat")
73         for iface in if_data[0]:
74             if iface["sw_if_index"] == sw_if_index:
75                 return ':'.join("%02x" % (b) for b in iface["l2_address"][:6])
76
77         return None
78
79     @staticmethod
80     def vpp_show_vhost(node):
81         """Get vhost-user data for the given node.
82
83         :param node: VPP node to get interface data from.
84         :type node: dict
85         :return: nothing
86         """
87         vat = VatExecutor()
88         vat.execute_script("show_vhost.vat", node, json_out=False)