def _try_allocate_new_tc_class(context, qos):
        select_range = context.session.query(
            QosTcClassRange).join(Qos).with_lockmode('update').first()
        if select_range:
            new_tc_class = select_range['first']
            if select_range['first'] == select_range['last']:
                LOG.debug("No more free tc class id in this slice, deleting "
                          "range.")
                context.session.delete(select_range)
            else:
                # Increse the first class id in this range
                select_range['first'] = new_tc_class + 1
            return new_tc_class
        raise ext_qos.AllocateTCClassFailure(qos_id=qos.id)