# Path: neutron_taas/services/taas/drivers/linux/sriov_nic_exceptions.py
# class SriovNicSwitchDriverInvocationError(qexception.Invalid):
# class PciDeviceNotFoundById(qexception.NotFound):
# class PciSlotNotFound(qexception.NotFound):


import os
import glob
import re
from oslo_log import log as logging
from neutron.agent.linux import utils
from neutron_lib.api.definitions import portbindings
from neutron_taas.services.taas.drivers.linux import sriov_nic_exceptions \
    as taas_exc
        """Get the interface name based on a VF's pci address.

        The returned interface name is either the parent PF's or that of
        the VF itself based on the argument of pf_interface.
        """
        dev_path = self._get_sysfs_netdev_path(pci_addr, pf_interface)
        try:
            dev_info = os.listdir(dev_path)
            return dev_info.pop()
        except Exception:
            raise taas_exc.PciDeviceNotFoundById(id=pci_addr)

    def get_mac_by_pci_address(self, pci_addr, pf_interface=False):
        """Get the MAC address of the nic based on its PCI address.

        Raises PciDeviceNotFoundById in case the pci device is not a NIC
        """
        dev_path = self._get_sysfs_netdev_path(pci_addr, pf_interface)
        if_name = self.get_ifname_by_pci_address(pci_addr, pf_interface)
        addr_file = os.path.join(dev_path, if_name, 'address')

        try:
            with open(addr_file) as f:
                mac = next(f).strip()
                return mac
        except (IOError, StopIteration) as e:
            LOG.warning("Could not find the expected sysfs file for "
                        "determining the MAC address of the PCI device "
                        "%(addr)s. May not be a NIC. Error: %(e)s",
                        {'addr': pci_addr, 'e': e})

# Path: neutron_taas/services/taas/drivers/linux/sriov_nic_exceptions.py
# class SriovNicSwitchDriverInvocationError(qexception.Invalid):
# class PciDeviceNotFoundById(qexception.NotFound):
# class PciSlotNotFound(qexception.NotFound):


# Path: neutron_taas/services/taas/drivers/linux/sriov_nic_utils.py
import os
import glob
import re
from oslo_log import log as logging
from neutron.agent.linux import utils
from neutron_lib.api.definitions import portbindings
from neutron_taas.services.taas.drivers.linux import sriov_nic_exceptions \
    as taas_exc

        """Get the interface name based on a VF's pci address.

        The returned interface name is either the parent PF's or that of
        the VF itself based on the argument of pf_interface.
        """
        dev_path = self._get_sysfs_netdev_path(pci_addr, pf_interface)
        try:
            dev_info = os.listdir(dev_path)
            return dev_info.pop()
        except Exception:
            raise taas_exc.PciDeviceNotFoundById(id=pci_addr)

    def get_mac_by_pci_address(self, pci_addr, pf_interface=False):
        """Get the MAC address of the nic based on its PCI address.

        Raises PciDeviceNotFoundById in case the pci device is not a NIC
        """
        dev_path = self._get_sysfs_netdev_path(pci_addr, pf_interface)
        if_name = self.get_ifname_by_pci_address(pci_addr, pf_interface)
        addr_file = os.path.join(dev_path, if_name, 'address')

        try:
            with open(addr_file) as f:
                mac = next(f).strip()
                return mac
        except (IOError, StopIteration) as e:
            LOG.warning("Could not find the expected sysfs file for "
                        "determining the MAC address of the PCI device "
                        "%(addr)s. May not be a NIC. Error: %(e)s",
                        {'addr': pci_addr, 'e': e})

            raise taas_exc.PciDeviceNotFoundById(id=pci_addr)