X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=resources%2Flibraries%2Fpython%2FVhostUser.py;h=48cce002bd08c64c6cce370e81b36d8ae3c6d409;hb=bb33f1d16f1451253f942da0c55cdea72e71c398;hp=2e0ed1eada19e9b4ec64451abb729d410ac065b7;hpb=7bfb36dfd9284bbca10881e31e14108c7d468b7c;p=csit.git diff --git a/resources/libraries/python/VhostUser.py b/resources/libraries/python/VhostUser.py index 2e0ed1eada..48cce002bd 100644 --- a/resources/libraries/python/VhostUser.py +++ b/resources/libraries/python/VhostUser.py @@ -1,4 +1,4 @@ -# Copyright (c) 2016 Cisco and/or its affiliates. +# Copyright (c) 2019 Cisco and/or its affiliates. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at: @@ -13,13 +13,38 @@ """Vhost-user interfaces library.""" -from resources.libraries.python.VatExecutor import VatExecutor, VatTerminal +from robot.api import logger + +from resources.libraries.python.PapiExecutor import PapiSocketExecutor from resources.libraries.python.topology import NodeType, Topology from resources.libraries.python.InterfaceUtil import InterfaceUtil -class VhostUser(object): - """Vhost-user interfaces""" +class VhostUser: + """Vhost-user interfaces L1 library.""" + + @staticmethod + def _sw_interface_vhost_user_dump(node): + """Get the Vhost-user dump on the given node. + + :param node: Given node to get Vhost dump from. + :type node: dict + :returns: List of Vhost-user interfaces data extracted from Papi + response. + :rtype: list + """ + cmd = u"sw_interface_vhost_user_dump" + + with PapiSocketExecutor(node) as papi_exec: + details = papi_exec.add(cmd).get_details() + + for vhost in details: + vhost[u"interface_name"] = vhost[u"interface_name"] + vhost[u"sock_filename"] = vhost[u"sock_filename"] + + logger.debug(f"VhostUser details:\n{details}") + + return details @staticmethod def vpp_create_vhost_user_interface(node, socket): @@ -31,23 +56,30 @@ class VhostUser(object): :type socket: str :returns: SW interface index. :rtype: int - :raises RuntimeError: If Vhost-user interface creation failed. """ - out = VatExecutor.cmd_from_template(node, 'create_vhost_user_if.vat', - sock=socket) - if out[0].get('retval') == 0: - sw_if_idx = int(out[0].get('sw_if_index')) - if_key = Topology.add_new_port(node, 'vhost') - Topology.update_interface_sw_if_index(node, if_key, sw_if_idx) - ifc_name = InterfaceUtil.vpp_get_interface_name(node, sw_if_idx) - Topology.update_interface_name(node, if_key, ifc_name) - ifc_mac = InterfaceUtil.vpp_get_interface_mac(node, sw_if_idx) - Topology.update_interface_mac_address(node, if_key, ifc_mac) - Topology.update_interface_vhost_socket(node, if_key, socket) - return sw_if_idx - else: - raise RuntimeError('Create Vhost-user interface failed on node ' - '"{}"'.format(node['host'])) + cmd = u"create_vhost_user_if" + err_msg = f"Failed to create Vhost-user interface " \ + f"on host {node[u'host']}" + args = dict( + sock_filename=str(socket).encode(encoding=u"utf-8") + ) + + with PapiSocketExecutor(node) as papi_exec: + sw_if_index = papi_exec.add(cmd, **args).get_sw_if_index(err_msg) + + # Update the Topology: + if_key = Topology.add_new_port(node, u"vhost") + Topology.update_interface_sw_if_index(node, if_key, sw_if_index) + + ifc_name = InterfaceUtil.vpp_get_interface_name(node, sw_if_index) + Topology.update_interface_name(node, if_key, ifc_name) + + ifc_mac = InterfaceUtil.vpp_get_interface_mac(node, sw_if_index) + Topology.update_interface_mac_address(node, if_key, ifc_mac) + + Topology.update_interface_vhost_socket(node, if_key, socket) + + return sw_if_index @staticmethod def get_vhost_user_if_name_by_sock(node, socket): @@ -60,9 +92,9 @@ class VhostUser(object): :returns: Interface name or None if not found. :rtype: str """ - for interface in node['interfaces'].values(): - if interface.get('socket') == socket: - return interface.get('name') + for interface in node[u"interfaces"].values(): + if interface.get(u"socket") == socket: + return interface.get(u"name") return None @staticmethod @@ -71,40 +103,48 @@ class VhostUser(object): interface dump. :param node: VPP node to get interface data from. - :param sw_if_index: Idx of the specific interface. + :param sw_if_index: SW index of the specific interface. :type node: dict :type sw_if_index: str :returns: l2_address of the given interface. :rtype: str """ - - with VatTerminal(node) as vat: - if_data = vat.vat_terminal_exec_cmd_from_template( - "interface_dump.vat") - for iface in if_data[0]: - if iface["sw_if_index"] == sw_if_index: - return ':'.join("%02x" % (b) for b in iface["l2_address"][:6]) - - return None + return InterfaceUtil.vpp_get_interface_mac(node, sw_if_index) @staticmethod def vpp_show_vhost(node): - """Get vhost-user data for the given node. + """Get Vhost-user data for the given node. :param node: VPP node to get interface data from. :type node: dict - :returns: nothing """ - vat = VatExecutor() - vat.execute_script("show_vhost.vat", node, json_out=False) + VhostUser._sw_interface_vhost_user_dump(node) @staticmethod def show_vpp_vhost_on_all_duts(nodes): - """Show Vhost User on all DUTs. + """Show Vhost-user on all DUTs. :param nodes: VPP nodes. :type nodes: dict """ for node in nodes.values(): - if node['type'] == NodeType.DUT: + if node[u"type"] == NodeType.DUT: VhostUser.vpp_show_vhost(node) + + @staticmethod + def vhost_user_dump(node): + """Get vhost-user data for the given node. + + :param node: VPP node to get interface data from. + :type node: dict + :returns: List of dictionaries with all vhost-user interfaces. + :rtype: list + """ + cmd = u"sw_interface_vhost_user_dump" + err_msg = f"Failed to get vhost-user dump on host {node['host']}" + + with PapiSocketExecutor(node) as papi_exec: + details = papi_exec.add(cmd).get_details(err_msg) + + logger.debug(f"Vhost-user details:\n{details}") + return details