'Raise exception if there are any subnets associated with segment.'

@registry.receives(resources.SEGMENT, [events.BEFORE_DELETE])
def _prevent_segment_delete_with_subnet_associated(self, resource, event, trigger, context, segment, for_net_delete=False):

    if for_net_delete:
        return
    with db_api.context_manager.reader.using(context):
        segment_id = segment['id']
        query = context.session.query(models_v2.Subnet.id)
        query = query.filter((models_v2.Subnet.segment_id == segment_id))
        subnet_ids = [s[0] for s in query]
    if subnet_ids:
        reason = (_('The    segment    is    still    associated    with    subnet(s)    %s') % ',    '.join(subnet_ids))
        raise exceptions.SegmentInUse(segment_id=segment_id, reason=reason)
