<commit_before># Copyright 2016 Mirantis, Inc.  All rights reserved.
#
#    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.
#

from oslo_log import log as logging

from neutron._i18n import _LE
from neutron.callbacks import registry
from neutron.callbacks import resources
from neutron.db import api as db_api

from neutron.common import _deprecate
from neutron.db.models import provisioning_block as pb_model
from neutron.db import models_v2

LOG = logging.getLogger(__name__)
PROVISIONING_COMPLETE = 'provisioning_complete'
# identifiers for the various entities that participate in provisioning
DHCP_ENTITY = 'DHCP'
L2_AGENT_ENTITY = 'L2'
_RESOURCE_TO_MODEL_MAP = {resources.PORT: models_v2.Port}

_deprecate._moved_global('ProvisioningBlock', new_module=pb_model)


def add_model_for_resource(resource, model):
    """Adds a mapping between a callback resource and a DB model."""
    _RESOURCE_TO_MODEL_MAP[resource] = model


@db_api.retry_if_session_inactive()
def add_provisioning_component(context, object_id, object_type, entity):
    """Adds a provisioning block by an entity to a given object.

    Adds a provisioning block to the DB for object_id with an identifier
    of the entity that is doing the provisioning. While an object has these
    provisioning blocks present, this module will not emit any callback events
    indicating that provisioning has completed. Any logic that depends on
    multiple disjoint components may use these blocks and subscribe to the
    PROVISIONING_COMPLETE event to know when all components have completed.

    :param context: neutron api request context
    :param object_id: ID of object that has been provisioned
    :param object_type: callback resource type of the object
    :param entity: The entity that has provisioned the object
    """
    log_dict = {'entity': entity, 'oid': object_id, 'otype': object_type}
    # we get an object's ID, so we need to convert that into a standard attr id
    standard_attr_id = _get_standard_attr_id(context, object_id, object_type)
    if not standard_attr_id:
        return
    record = context.session.query(pb_model.ProvisioningBlock).filter_by(
        standard_attr_id=standard_attr_id, entity=entity).first()
    if record:
        # an entry could be leftover from a previous transition that hasn't
        # yet been provisioned. (e.g. multiple updates in a short period)
        LOG.debug("Ignored duplicate provisioning block setup for %(otype)s "
                  "%(oid)s by entity %(entity)s.", log_dict)
        return
    with context.session.begin(subtransactions=True):
        record = pb_model.ProvisioningBlock(standard_attr_id=standard_attr_id,
                                            entity=entity)
        context.session.add(record)
    LOG.debug("Transition to ACTIVE for %(otype)s object %(oid)s "
              "will not be triggered until provisioned by entity %(entity)s.",
              log_dict)


@db_api.retry_if_session_inactive()
def remove_provisioning_component(context, object_id, object_type, entity,
                                  standard_attr_id=None):
    """Removes a provisioning block for an object with triggering a callback.

    Removes a provisioning block without triggering a callback. A user of this
    module should call this when a block is no longer correct. If the block has
    been satisfied, the 'provisioning_complete' method should be called.

    :param context: neutron api request context
    :param object_id: ID of object that has been provisioned
    :param object_type: callback resource type of the object
    :param entity: The entity that has provisioned the object
    :param standard_attr_id: Optional ID to pass to the function to avoid the
                             extra DB lookup to translate the object_id into
                             the standard_attr_id.
    :return: boolean indicating whether or not a record was deleted
    """
    with context.session.begin(subtransactions=True):
        standard_attr_id = standard_attr_id or _get_standard_attr_id(
            context, object_id, object_type)
        if not standard_attr_id:
            return False
        record = context.session.query(pb_model.ProvisioningBlock).filter_by(
            standard_attr_id=standard_attr_id, entity=entity).first()
        if record:
            context.session.delete(record)
            return True
        return False

<commit_msg>Fix a bad docstring in provisioning blocks module<commit_after># Copyright 2016 Mirantis, Inc.  All rights reserved.
#
#    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.
#

from oslo_log import log as logging

from neutron._i18n import _LE
from neutron.callbacks import registry
from neutron.callbacks import resources
from neutron.db import api as db_api

from neutron.common import _deprecate
from neutron.db.models import provisioning_block as pb_model
from neutron.db import models_v2

LOG = logging.getLogger(__name__)
PROVISIONING_COMPLETE = 'provisioning_complete'
# identifiers for the various entities that participate in provisioning
DHCP_ENTITY = 'DHCP'
L2_AGENT_ENTITY = 'L2'
_RESOURCE_TO_MODEL_MAP = {resources.PORT: models_v2.Port}

_deprecate._moved_global('ProvisioningBlock', new_module=pb_model)


def add_model_for_resource(resource, model):
    """Adds a mapping between a callback resource and a DB model."""
    _RESOURCE_TO_MODEL_MAP[resource] = model


@db_api.retry_if_session_inactive()
def add_provisioning_component(context, object_id, object_type, entity):
    """Adds a provisioning block by an entity to a given object.

    Adds a provisioning block to the DB for object_id with an identifier
    of the entity that is doing the provisioning. While an object has these
    provisioning blocks present, this module will not emit any callback events
    indicating that provisioning has completed. Any logic that depends on
    multiple disjoint components may use these blocks and subscribe to the
    PROVISIONING_COMPLETE event to know when all components have completed.

    :param context: neutron api request context
    :param object_id: ID of object that has been provisioned
    :param object_type: callback resource type of the object
    :param entity: The entity that has provisioned the object
    """
    log_dict = {'entity': entity, 'oid': object_id, 'otype': object_type}
    # we get an object's ID, so we need to convert that into a standard attr id
    standard_attr_id = _get_standard_attr_id(context, object_id, object_type)
    if not standard_attr_id:
        return
    record = context.session.query(pb_model.ProvisioningBlock).filter_by(
        standard_attr_id=standard_attr_id, entity=entity).first()
    if record:
        # an entry could be leftover from a previous transition that hasn't
        # yet been provisioned. (e.g. multiple updates in a short period)
        LOG.debug("Ignored duplicate provisioning block setup for %(otype)s "
                  "%(oid)s by entity %(entity)s.", log_dict)
        return
    with context.session.begin(subtransactions=True):
        record = pb_model.ProvisioningBlock(standard_attr_id=standard_attr_id,
                                            entity=entity)
        context.session.add(record)
    LOG.debug("Transition to ACTIVE for %(otype)s object %(oid)s "
              "will not be triggered until provisioned by entity %(entity)s.",
              log_dict)


@db_api.retry_if_session_inactive()
def remove_provisioning_component(context, object_id, object_type, entity,
                                  standard_attr_id=None):
    """Remove a provisioning block for an object without triggering a callback.

    Removes a provisioning block without triggering a callback. A user of this
    module should call this when a block is no longer correct. If the block has
    been satisfied, the 'provisioning_complete' method should be called.

    :param context: neutron api request context
    :param object_id: ID of object that has been provisioned
    :param object_type: callback resource type of the object
    :param entity: The entity that has provisioned the object
    :param standard_attr_id: Optional ID to pass to the function to avoid the
                             extra DB lookup to translate the object_id into
                             the standard_attr_id.
    :return: boolean indicating whether or not a record was deleted
    """
    with context.session.begin(subtransactions=True):
        standard_attr_id = standard_attr_id or _get_standard_attr_id(
            context, object_id, object_type)
        if not standard_attr_id:
            return False
        record = context.session.query(pb_model.ProvisioningBlock).filter_by(
            standard_attr_id=standard_attr_id, entity=entity).first()
        if record:
            context.session.delete(record)
            return True
        return False


{'fraction_non_alphanumeric': 0.058835381825508765, 'fraction_numerical': 0.0028208744710860366, 'mean_word_length': 3.334934497816594, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2743005', 'n_tokens_mistral': 2671, 'n_tokens_neox': 2619, 'n_words': 1033}