def _get_mcast_group_for_vni(self, context, vni):
     session = bc.get_tunnel_session(context)
     mcast_grp = (session.query(nexus_models_v2.NexusMcastGroup).
                  filter_by(associated_vni=vni).first())
     if not mcast_grp:
         mcast_grp = self._allocate_mcast_group(session, vni)
     return mcast_grp
    def release_segment(self, context, segment):
        vxlan_vni = segment[api.SEGMENTATION_ID]

        inside = any(lo <= vxlan_vni <= hi for lo, hi in self.tunnel_ranges)

        session = bc.get_tunnel_session(context)
        with session.begin(subtransactions=True):
            query = (session.query(
                nexus_models_v2.NexusVxlanAllocation).
                     filter_by(vxlan_vni=vxlan_vni))
            if inside:
                count = query.update({"allocated": False})
                if count:
                    mcast_row = (
                         session.query(nexus_models_v2.NexusMcastGroup)
                         .filter_by(associated_vni=vxlan_vni).first())
                    session.delete(mcast_row)
                    LOG.debug("Releasing vxlan tunnel %s to pool",
                              vxlan_vni)
            else:
                count = query.delete()
                if count:
                    LOG.debug("Releasing vxlan tunnel %s outside pool",
                              vxlan_vni)

        if not count:
            LOG.warning("vxlan_vni %s not found", vxlan_vni)