# Path: os_vif/exception.py
# class ExceptionBase(Exception):
# class LibraryNotInitialized(ExceptionBase):
# class NoMatchingPlugin(ExceptionBase):
# class NoMatchingPortProfileClass(ExceptionBase):
# class NoSupportedPortProfileVersion(ExceptionBase):
# class NoMatchingVIFClass(ExceptionBase):
# class NoSupportedVIFVersion(ExceptionBase):
# class PlugException(ExceptionBase):
# class UnplugException(ExceptionBase):
# class NetworkMissingPhysicalNetwork(ExceptionBase):
# class NetworkInterfaceNotFound(ExceptionBase):
# class NetworkInterfaceTypeNotDefined(ExceptionBase):
# class ExternalImport(ExceptionBase):
# class NotImplementedForOS(ExceptionBase):
#     def __init__(self, message=None, **kwargs):
#     def format_message(self):
# 
# Path: os_vif/internal/ip/ip_command.py
# class IpCommand(metaclass=abc.ABCMeta):
#     TYPE_VETH = 'veth'
#     TYPE_VLAN = 'vlan'
#     TYPE_BRIDGE = 'bridge'
#     def set(self, device, check_exit_code=None, state=None, mtu=None,
#             address=None, promisc=None, master=None):
#     def add(self, device, dev_type, check_exit_code=None, peer=None, link=None,
#             vlan_id=None, ageing=None):
#     def delete(self, device, check_exit_code=None):
#     def exists(self, device):
# 
# Path: os_vif/utils.py
# def set_mask(data, mask):
# def unset_mask(data, mask, bit_size=32):


from oslo_log import log as logging
from oslo_utils import excutils
from pyroute2 import iproute
from pyroute2.netlink import exceptions as ipexc
from pyroute2.netlink.rtnl import ifinfmsg
from os_vif import exception
from os_vif.internal.ip import ip_command
from os_vif import utils
class PyRoute2(ip_command.IpCommand):

    def _ip_link(self, ip, command, check_exit_code, **kwargs):
        try:
            LOG.debug('pyroute2 command %(command)s, arguments %(args)s' %
                      {'command': command, 'args': kwargs})
            return ip.link(command, **kwargs)
        except ipexc.NetlinkError as e:
            with excutils.save_and_reraise_exception() as ctx:
                if e.code in check_exit_code:
                    LOG.error('NetlinkError was raised, code %s, message: %s' %
                              (e.code, str(e)))
                    ctx.reraise = False

    def set(self, device, check_exit_code=None, state=None, mtu=None,
            address=None, promisc=None, master=None):
        check_exit_code = check_exit_code or []
        with iproute.IPRoute() as ip:
            idx = self.lookup_interface(ip, device)
            args = {'index': idx}
            if state:
                args['state'] = state
            if mtu:
                args['mtu'] = mtu
            if address:
                args['address'] = address
            if promisc is not None:
                flags = ip.link('get', index=idx)[0]['flags']
                args['flags'] = (utils.set_mask(flags, ifinfmsg.IFF_PROMISC)
                                 if promisc is True else

# Path: os_vif/exception.py
# class ExceptionBase(Exception):
# class LibraryNotInitialized(ExceptionBase):
# class NoMatchingPlugin(ExceptionBase):
# class NoMatchingPortProfileClass(ExceptionBase):
# class NoSupportedPortProfileVersion(ExceptionBase):
# class NoMatchingVIFClass(ExceptionBase):
# class NoSupportedVIFVersion(ExceptionBase):
# class PlugException(ExceptionBase):
# class UnplugException(ExceptionBase):
# class NetworkMissingPhysicalNetwork(ExceptionBase):
# class NetworkInterfaceNotFound(ExceptionBase):
# class NetworkInterfaceTypeNotDefined(ExceptionBase):
# class ExternalImport(ExceptionBase):
# class NotImplementedForOS(ExceptionBase):
#     def __init__(self, message=None, **kwargs):
#     def format_message(self):
# 
# Path: os_vif/internal/ip/ip_command.py
# class IpCommand(metaclass=abc.ABCMeta):
#     TYPE_VETH = 'veth'
#     TYPE_VLAN = 'vlan'
#     TYPE_BRIDGE = 'bridge'
#     def set(self, device, check_exit_code=None, state=None, mtu=None,
#             address=None, promisc=None, master=None):
#     def add(self, device, dev_type, check_exit_code=None, peer=None, link=None,
#             vlan_id=None, ageing=None):
#     def delete(self, device, check_exit_code=None):
#     def exists(self, device):
# 
# Path: os_vif/utils.py
# def set_mask(data, mask):
# def unset_mask(data, mask, bit_size=32):


# Path: os_vif/internal/ip/linux/impl_pyroute2.py
from oslo_log import log as logging
from oslo_utils import excutils
from pyroute2 import iproute
from pyroute2.netlink import exceptions as ipexc
from pyroute2.netlink.rtnl import ifinfmsg
from os_vif import exception
from os_vif.internal.ip import ip_command
from os_vif import utils

class PyRoute2(ip_command.IpCommand):

    def _ip_link(self, ip, command, check_exit_code, **kwargs):
        try:
            LOG.debug('pyroute2 command %(command)s, arguments %(args)s' %
                      {'command': command, 'args': kwargs})
            return ip.link(command, **kwargs)
        except ipexc.NetlinkError as e:
            with excutils.save_and_reraise_exception() as ctx:
                if e.code in check_exit_code:
                    LOG.error('NetlinkError was raised, code %s, message: %s' %
                              (e.code, str(e)))
                    ctx.reraise = False

    def set(self, device, check_exit_code=None, state=None, mtu=None,
            address=None, promisc=None, master=None):
        check_exit_code = check_exit_code or []
        with iproute.IPRoute() as ip:
            idx = self.lookup_interface(ip, device)
            args = {'index': idx}
            if state:
                args['state'] = state
            if mtu:
                args['mtu'] = mtu
            if address:
                args['address'] = address
            if promisc is not None:
                flags = ip.link('get', index=idx)[0]['flags']
                args['flags'] = (utils.set_mask(flags, ifinfmsg.IFF_PROMISC)
                                 if promisc is True else

                                 utils.unset_mask(flags, ifinfmsg.IFF_PROMISC))