#    Copyright 2012 Nicira, Inc
#
#    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
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import os

import fixtures
from lxml import etree
import mock
import os_vif
from os_vif import exception as osv_exception
from os_vif import objects as osv_objects
from os_vif.objects import fields as osv_fields
from oslo_concurrency import processutils
from oslo_config import cfg
import six

from nova import exception
from nova.network import linux_net
from nova.network import model as network_model
from nova import objects
from nova.pci import utils as pci_utils
from nova import test
from nova.tests.unit import matchers
from nova.tests.unit.virt import fakelibosinfo
from nova.tests.unit.virt.libvirt import fakelibvirt
from nova import utils
from nova.virt.libvirt import config as vconfig
from nova.virt.libvirt import host
from nova.virt.libvirt import vif

CONF = cfg.CONF

MIN_LIBVIRT_VHOSTUSER_MQ = vif.MIN_LIBVIRT_VHOSTUSER_MQ


class LibvirtVifTestCase(test.NoDBTestCase):

    gateway_bridge_4 = network_model.IP(address='101.168.1.1', type='gateway')
    dns_bridge_4 = network_model.IP(address='8.8.8.8', type=None)
    ips_bridge_4 = [network_model.IP(address='101.168.1.9', type=None)]
    subnet_bridge_4 = network_model.Subnet(
        cidr='101.168.1.0/24',
        dns=[dns_bridge_4],
        gateway=gateway_bridge_4,
        routes=None,
        dhcp_server='191.168.1.1')

    gateway_bridge_6 = network_model.IP(address='101:1db9::1', type='gateway')
    subnet_bridge_6 = network_model.Subnet(cidr='101:1db9::/64',
        dns=None,
        gateway=gateway_bridge_6,
        ips=None,
        routes=None)

    network_bridge = network_model.Network(id='network-id-xxx-yyy-zzz',
        bridge='br0',
        label=None,
        subnets=[subnet_bridge_4, subnet_bridge_6],
        bridge_interface='eth0',
        vlan=99, mtu=9000)

    vif_bridge = network_model.VIF(id='vif-xxx-yyy-zzz',
        address='ca:fe:de:ad:be:ef',
        network=network_bridge,
        type=network_model.VIF_TYPE_BRIDGE,
        devname='tap-xxx-yyy-zzz',
        ovs_interfaceid=None)

    network_bridge_neutron = network_model.Network(id='network-id-xxx-yyy-zzz',
        bridge=None,
        label=None,
        subnets=[subnet_bridge_4, subnet_bridge_6],
        bridge_interface='eth0',
        vlan=99)

    vif_bridge_neutron = network_model.VIF(id='vif-xxx-yyy-zzz',
        address='ca:fe:de:ad:be:ef',
        network=network_bridge_neutron,
        type=None,
        devname='tap-xxx-yyy-zzz',
        ovs_interfaceid='aaa-bbb-ccc')

    network_ovs = network_model.Network(id='network-id-xxx-yyy-zzz',
        bridge='br0',
        label=None,
        subnets=[subnet_bridge_4, subnet_bridge_6],
        bridge_interface=None,
        vlan=99, mtu=1000)

    network_ivs = network_model.Network(id='network-id-xxx-yyy-zzz',
        bridge='br0',
        label=None,
        subnets=[subnet_bridge_4, subnet_bridge_6],
        bridge_interface=None,
        vlan=99)

    vif_agilio_ovs = network_model.VIF(id='vif-xxx-yyy-zzz',
        address='ca:fe:de:ad:be:ef',
        network=network_ovs,
        type=network_model.VIF_TYPE_AGILIO_OVS,
        details={'port_filter': False},
        devname='tap-xxx-yyy-zzz',
        ovs_interfaceid='aaa-bbb-ccc')

    vif_agilio_ovs_direct = network_model.VIF(id='vif-xxx-yyy-zzz',
        address='ca:fe:de:ad:be:ef',
        network=network_ovs,
        type=network_model.VIF_TYPE_AGILIO_OVS,
        vnic_type=network_model.VNIC_TYPE_DIRECT,
        ovs_interfaceid='aaa-bbb-ccc',
        devname='tap-xxx-yyy-zzz',
        profile={'pci_slot': '0000:0a:00.1'})

    vif_agilio_ovs_forwarder = network_model.VIF(id='vif-xxx-yyy-zzz',
        address='ca:fe:de:ad:be:ef',
        network=network_ovs,
        type=network_model.VIF_TYPE_AGILIO_OVS,
        vnic_type=network_model.VNIC_TYPE_VIRTIO_FORWARDER,
        profile={'pci_slot': '0000:0a:00.1'},
        details={
            network_model.VIF_DETAILS_VHOSTUSER_MODE: 'client',
            network_model.VIF_DETAILS_VHOSTUSER_SOCKET: '/tmp/usv-xxx-yyy-zzz',
            network_model.VIF_DETAILS_VHOSTUSER_OVS_PLUG: True},
        ovs_interfaceid='aaa-bbb-ccc', mtu=1500)

    vif_ovs = network_model.VIF(id='vif-xxx-yyy-zzz',
        address='ca:fe:de:ad:be:ef',
        network=network_ovs,
        type=network_model.VIF_TYPE_OVS,
        details={'port_filter': False},
        devname='tap-xxx-yyy-zzz',
        ovs_interfaceid='aaa-bbb-ccc')

    vif_ovs_hybrid = network_model.VIF(id='vif-xxx-yyy-zzz',
        address='ca:fe:de:ad:be:ef',
        network=network_ovs,
        type=network_model.VIF_TYPE_OVS,
        details={'ovs_hybrid_plug': True,
                 'port_filter': True},
        devname='tap-xxx-yyy-zzz',
        ovs_interfaceid='aaa-bbb-ccc')

    vif_ovs_filter_cap = network_model.VIF(id='vif-xxx-yyy-zzz',
        address='ca:fe:de:ad:be:ef',
        network=network_ovs,
        type=network_model.VIF_TYPE_OVS,
        details={'port_filter': True},
        devname='tap-xxx-yyy-zzz',
        ovs_interfaceid='aaa-bbb-ccc')

    vif_ovs_legacy = network_model.VIF(id='vif-xxx-yyy-zzz',
        address='ca:fe:de:ad:be:ef',
        network=network_ovs,
        type=None,
        devname=None,
        ovs_interfaceid=None)

    vif_ivs = network_model.VIF(id='vif-xxx-yyy-zzz',
        address='ca:fe:de:ad:be:ef',
        network=network_ivs,
        type=network_model.VIF_TYPE_IVS,
        devname='tap-xxx-yyy-zzz',
        ovs_interfaceid='aaa-bbb-ccc')

    vif_ivs_filter_cap = network_model.VIF(id='vif-xxx-yyy-zzz',
        address='ca:fe:de:ad:be:ef',
        network=network_ivs,
        type=network_model.VIF_TYPE_IVS,
        details={'port_filter': True},
        devname='tap-xxx-yyy-zzz',
        ovs_interfaceid='aaa-bbb-ccc')

    vif_ivs_hybrid = network_model.VIF(id='vif-xxx-yyy-zzz',
        address='ca:fe:de:ad:be:ef',
        network=network_ivs,
        type=network_model.VIF_TYPE_IVS,
        details={
            'port_filter': True,
            'ovs_hybrid_plug': True},
        devname='tap-xxx-yyy-zzz',
        ovs_interfaceid='aaa-bbb-ccc')

    vif_ivs_legacy = network_model.VIF(id='vif-xxx-yyy-zzz',
        address='ca:fe:de:ad:be:ef',
        network=network_ovs,
        type=None,
        devname=None,
        ovs_interfaceid='aaa')

    vif_none = network_model.VIF(id='vif-xxx-yyy-zzz',
        address='ca:fe:de:ad:be:ef',
        network=network_bridge,
        type=None,
        devname='tap-xxx-yyy-zzz',
        ovs_interfaceid=None)

    network_8021 = network_model.Network(id='network-id-xxx-yyy-zzz',
        bridge=None,
        label=None,
        subnets=[subnet_bridge_4,
                 subnet_bridge_6],
        interface='eth0',
        vlan=99)

    vif_8021qbh = network_model.VIF(id='vif-xxx-yyy-zzz',
        address='ca:fe:de:ad:be:ef',
        network=network_8021,
        type=network_model.VIF_TYPE_802_QBH,
        vnic_type=network_model.VNIC_TYPE_DIRECT,
        ovs_interfaceid=None,
        details={
            network_model.VIF_DETAILS_PROFILEID:
            'MyPortProfile'},
        profile={'pci_vendor_info': '1137:0043',
                 'pci_slot': '0000:0a:00.1',
                 'physical_network': 'phynet1'})

    vif_hw_veb = network_model.VIF(id='vif-xxx-yyy-zzz',
        address='ca:fe:de:ad:be:ef',
        network=network_8021,
        type=network_model.VIF_TYPE_HW_VEB,
        vnic_type=network_model.VNIC_TYPE_DIRECT,
        ovs_interfaceid=None,
        details={
            network_model.VIF_DETAILS_VLAN: 100},
        profile={'pci_vendor_info': '1137:0043',
                 'pci_slot': '0000:0a:00.1',
                 'physical_network': 'phynet1'})

    vif_hostdev_physical = network_model.VIF(id='vif-xxx-yyy-zzz',
        address='ca:fe:de:ad:be:ef',
        network=network_8021,
        type=network_model.VIF_TYPE_HOSTDEV,
        vnic_type=network_model.VNIC_TYPE_DIRECT_PHYSICAL,
        ovs_interfaceid=None,
        profile={'pci_vendor_info': '1137:0043',
                 'pci_slot': '0000:0a:00.1',
                 'physical_network': 'phynet1'})

    vif_hw_veb_macvtap = network_model.VIF(id='vif-xxx-yyy-zzz',
        address='ca:fe:de:ad:be:ef',
        network=network_8021,
        type=network_model.VIF_TYPE_HW_VEB,
        vnic_type=network_model.VNIC_TYPE_MACVTAP,
        ovs_interfaceid=None,
        details={network_model.VIF_DETAILS_VLAN: 100},
        profile={'pci_vendor_info': '1137:0043',
                 'pci_slot': '0000:0a:00.1',
                 'physical_network': 'phynet1'})

    vif_8021qbg = network_model.VIF(id='vif-xxx-yyy-zzz',
        address='ca:fe:de:ad:be:ef',
        network=network_8021,
        type=network_model.VIF_TYPE_802_QBG,
        ovs_interfaceid=None,
        qbg_params=network_model.VIF8021QbgParams(
        managerid="xxx-yyy-zzz",
        typeid="aaa-bbb-ccc",
        typeidversion="1",
        instanceid="ddd-eee-fff"))

    network_midonet = network_model.Network(id='network-id-xxx-yyy-zzz',
        label=None,
        bridge=None,
        subnets=[subnet_bridge_4],
        interface='eth0')