def _get_free_vpcids_on_switches(switch_ip_list):
    '''Get intersect list of free vpcids in list of switches.'''

    session = bc.get_reader_session()

    prev_view = aliased(nexus_models_v2.NexusVPCAlloc)
    query = session.query(prev_view.vpc_id)
    prev_swip = switch_ip_list[0]

    for ip in switch_ip_list[1:]:
        cur_view = aliased(nexus_models_v2.NexusVPCAlloc)
        cur_swip = ip
        query = query.join(
            cur_view,
            sa.and_(
                prev_view.switch_ip == prev_swip,
                prev_view.active == False,  # noqa
                cur_view.switch_ip == cur_swip,
                cur_view.active == False,  # noqa
                prev_view.vpc_id == cur_view.vpc_id))
        prev_view = cur_view
        prev_swip = cur_swip

    unique_vpcids = query.all()
    shuffle(unique_vpcids)
    return unique_vpcids
def _lookup_vpc_allocs(query_type, session=None, order=None, **bfilter):
    """Look up 'query_type' Nexus VPC Allocs matching the filter.

    :param query_type: 'all', 'one' or 'first'
    :param session: db session
    :param order: select what field to order data
    :param bfilter: filter for mappings query
    :returns: VPCs if query gave a result, else
             raise NexusVPCAllocNotFound.
    """

    if session is None:
        session = bc.get_reader_session()

    if order:
        query_method = getattr(
            session.query(nexus_models_v2.NexusVPCAlloc).filter_by(
                **bfilter).order_by(order), query_type)
    else:
        query_method = getattr(
            session.query(nexus_models_v2.NexusVPCAlloc).filter_by(**bfilter),
            query_type)

    try:
        vpcs = query_method()
        if vpcs:
            return vpcs
    except sa_exc.NoResultFound:
        pass

    raise c_exc.NexusVPCAllocNotFound(**bfilter)
def _lookup_vpc_count_min_max(session=None, **bfilter):
    """Look up count/min/max Nexus VPC Allocs for given switch.

    :param session: db session
    :param bfilter: filter for mappings query
    :returns: number of VPCs and min value if query gave a result,
             else raise NexusVPCAllocNotFound.
    """

    if session is None:
        session = bc.get_reader_session()

    try:
        res = session.query(
            func.count(nexus_models_v2.NexusVPCAlloc.vpc_id),
            func.min(nexus_models_v2.NexusVPCAlloc.vpc_id),
            func.max(nexus_models_v2.NexusVPCAlloc.vpc_id),
        ).filter(nexus_models_v2.NexusVPCAlloc.switch_ip ==
                 bfilter['switch_ip']).one()

        count = res[0]
        sw_min = res[1]
        sw_max = res[2]

        return count, sw_min, sw_max

    except sa_exc.NoResultFound:
        pass

    raise c_exc.NexusVPCAllocNotFound(**bfilter)
def get_nve_vni_deviceid_bindings(vni, device_id):
    """Return all the nexus nve bindings for one vni/one device_id."""
    LOG.debug("get_nve_vni_deviceid_bindings() called")
    session = bc.get_reader_session()
    try:
        return (session.query(nexus_models_v2.NexusNVEBinding).filter_by(
            vni=vni, device_id=device_id).all())
    except sa_exc.NoResultFound:
        return None
def get_nve_switch_bindings(switch_ip):
    """Return all the nexus nve bindings for one switch."""
    LOG.debug("get_nve_switch_bindings() called")
    session = bc.get_reader_session()
    try:
        return (session.query(nexus_models_v2.NexusNVEBinding).filter_by(
            switch_ip=switch_ip).all())
    except sa_exc.NoResultFound:
        return None
def get_nve_vni_member_bindings(vni, switch_ip, device_id):
    """Return the nexus nve binding per switch and device_id."""
    LOG.debug("get_nve_vni_member_bindings() called")
    session = bc.get_reader_session()
    try:
        return (session.query(nexus_models_v2.NexusNVEBinding).filter_by(
            vni=vni, switch_ip=switch_ip, device_id=device_id).all())
    except sa_exc.NoResultFound:
        return None
def _lookup_host_mappings(query_type, session=None, **bfilter):
    """Look up 'query_type' Nexus mappings matching the filter.

    :param query_type: 'all', 'one' or 'first'
    :param session: db session
    :param bfilter: filter for mappings query
    :returns: mappings if query gave a result, else
             raise NexusHostMappingNotFound.
    """
    if session is None:
        session = bc.get_reader_session()
    query_method = getattr(
        session.query(nexus_models_v2.NexusHostMapping).filter_by(**bfilter),
        query_type)
    try:
        mappings = query_method()
        if mappings:
            return mappings
    except sa_exc.NoResultFound:
        pass
    raise c_exc.NexusHostMappingNotFound(**bfilter)
def is_provider_vlan(vlan_id):
    session = bc.get_reader_session()
    row = session.query(nexus_models_v2.NexusProviderNetwork).filter_by(
        vlan_id=vlan_id).one_or_none()
    return True if row else False