<commit_before># Copyright 2015 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 neutron.db import api as db
from neutron.db import securitygroups_db
from oslo_log import log
import oslo_messaging
from neutron.common import constants as n_const
from neutron.common import rpc as n_rpc
from neutron.openstack.common.gettextutils import _LI, _
from neutron.extensions import portbindings
from neutron.plugins.common import constants
from neutron.plugins.ml2 import driver_api

from mech_vmware_dvs import endpoints
from mech_vmware_dvs import compute_util
from mech_vmware_dvs import config
from mech_vmware_dvs import exceptions
from mech_vmware_dvs import util

CONF = config.CONF
LOG = log.getLogger(__name__)


class VMwareDVSMechanismDriver(driver_api.MechanismDriver):
    """Ml2 Mechanism driver for vmware dvs."""

    vif_type = portbindings.VIF_TYPE_DVS
    vif_details = {
        portbindings.CAP_PORT_FILTER: False}

    def initialize(self):
        self.network_map = util.create_network_map_from_config(CONF.ml2_vmware)
        self._bound_ports = set()

        session = db.get_session()
        rules = session.query(securitygroups_db.SecurityGroupRule).all()
        self.sgr_to_sg = dict([(r['id'],
                                r['security_group_id']) for r in rules])

        listener = oslo_messaging.get_notification_listener(
            n_rpc.TRANSPORT,
            targets=[oslo_messaging.Target(topic='vmware_dvs')],
            endpoints=[endpoints.SecurityGroupRuleCreateEndPoint(self),
                       endpoints.SecurityGroupRuleDeleteEndPoint(self),
                       endpoints.SecurityGroupCreateEndPoint(self),
                       endpoints.SecurityGroupDeleteEndPoint(self)],
            executor='eventlet')
        listener.start()

    def create_network_precommit(self, context):
        try:
            dvs = self._lookup_dvs_for_context(context)
        except (exceptions.NoDVSForPhysicalNetwork,
                exceptions.NotSupportedNetworkType) as e:
            LOG.info(_LI('Network %(id)s not created. Reason: %(reason)s') % {
                'id': context.current['id'],
                'reason': e.message})
        except exceptions.InvalidNetwork:
            pass
        else:
            dvs.create_network(context.current, context.network_segments[0])

    def update_network_precommit(self, context):
        try:
            dvs = self._lookup_dvs_for_context(context)
        except (exceptions.NoDVSForPhysicalNetwork,
                exceptions.NotSupportedNetworkType) as e:
            LOG.info(_LI('Network %(id)s not updated. Reason: %(reason)s') % {
                'id': context.current['id'],
                'reason': e.message})
        except exceptions.InvalidNetwork:
            pass
        else:
            dvs.update_network(context.current)

    def delete_network_postcommit(self, context):
        try:
            dvs = self._lookup_dvs_for_context(context)
        except (exceptions.NoDVSForPhysicalNetwork,
                exceptions.NotSupportedNetworkType) as e:
            LOG.info(_LI('Network %(id)s not deleted. Reason: %(reason)s') % {
                'id': context.current['id'],
                'reason': e.message})
        except exceptions.InvalidNetwork:
            pass
        else:
            dvs.delete_network(context.current)

    def update_port_postcommit(self, context):
        if not self._port_belongs_to_vmware(context.current):
            return

        try:
            dvs = self._lookup_dvs_for_context(context.network)
        except exceptions.NotSupportedNetworkType as e:
            LOG.info(_LI('Port %(id)s not updated. Reason: %(reason)s') % {
                'id': context.current['id'],
                'reason': e.message})
        except exceptions.NoDVSForPhysicalNetwork:
            raise exceptions.InvalidSystemState(details=_(
                'Port %(port_id)s belong to VMWare VM, but there is no '
                'mapping from network %(net_id)s to DVS.') % {
                    'port_id': context.current['id'],
                    'net_id': context.network.current['id']})
        else:
            self._update_admin_state_up(dvs, context)

            force = context.original['status'] == 'DOWN'
            self._update_security_groups(dvs, context, force=force)<commit_msg>Closes-Bug: #1234567-Added retry of every operation in driver<commit_after># Copyright 2015 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 neutron.db import api as db
from neutron.db import securitygroups_db
from oslo_log import log
import oslo_messaging
from neutron.common import constants as n_const
from neutron.common import rpc as n_rpc
from neutron.openstack.common.gettextutils import _LI, _
from neutron.extensions import portbindings
from neutron.plugins.common import constants
from neutron.plugins.ml2 import driver_api

from mech_vmware_dvs import endpoints
from mech_vmware_dvs import compute_util
from mech_vmware_dvs import config
from mech_vmware_dvs import exceptions
from mech_vmware_dvs import util

CONF = config.CONF
LOG = log.getLogger(__name__)


class VMwareDVSMechanismDriver(driver_api.MechanismDriver):
    """Ml2 Mechanism driver for vmware dvs."""

    vif_type = portbindings.VIF_TYPE_DVS
    vif_details = {
        portbindings.CAP_PORT_FILTER: False}

    def initialize(self):
        self.network_map = util.create_network_map_from_config(CONF.ml2_vmware)
        self._bound_ports = set()

        session = db.get_session()
        rules = session.query(securitygroups_db.SecurityGroupRule).all()
        self.sgr_to_sg = dict([(r['id'],
                                r['security_group_id']) for r in rules])

        listener = oslo_messaging.get_notification_listener(
            n_rpc.TRANSPORT,
            targets=[oslo_messaging.Target(topic='vmware_dvs')],
            endpoints=[endpoints.SecurityGroupRuleCreateEndPoint(self),
                       endpoints.SecurityGroupRuleDeleteEndPoint(self),
                       endpoints.SecurityGroupCreateEndPoint(self),
                       endpoints.SecurityGroupDeleteEndPoint(self)],
            executor='eventlet')
        listener.start()

    @util.wrap_retry
    def create_network_precommit(self, context):
        try:
            dvs = self._lookup_dvs_for_context(context)
        except (exceptions.NoDVSForPhysicalNetwork,
                exceptions.NotSupportedNetworkType) as e:
            LOG.info(_LI('Network %(id)s not created. Reason: %(reason)s') % {
                'id': context.current['id'],
                'reason': e.message})
        except exceptions.InvalidNetwork:
            pass
        else:
            dvs.create_network(context.current, context.network_segments[0])

    @util.wrap_retry
    def update_network_precommit(self, context):
        try:
            dvs = self._lookup_dvs_for_context(context)
        except (exceptions.NoDVSForPhysicalNetwork,
                exceptions.NotSupportedNetworkType) as e:
            LOG.info(_LI('Network %(id)s not updated. Reason: %(reason)s') % {
                'id': context.current['id'],
                'reason': e.message})
        except exceptions.InvalidNetwork:
            pass
        else:
            dvs.update_network(context.current)

    @util.wrap_retry
    def delete_network_postcommit(self, context):
        try:
            dvs = self._lookup_dvs_for_context(context)
        except (exceptions.NoDVSForPhysicalNetwork,
                exceptions.NotSupportedNetworkType) as e:
            LOG.info(_LI('Network %(id)s not deleted. Reason: %(reason)s') % {
                'id': context.current['id'],
                'reason': e.message})
        except exceptions.InvalidNetwork:
            pass
        else:
            dvs.delete_network(context.current)

    @util.wrap_retry
    def update_port_postcommit(self, context):
        if not self._port_belongs_to_vmware(context.current):
            return

        try:
            dvs = self._lookup_dvs_for_context(context.network)
        except exceptions.NotSupportedNetworkType as e:
            LOG.info(_LI('Port %(id)s not updated. Reason: %(reason)s') % {
                'id': context.current['id'],
                'reason': e.message})
        except exceptions.NoDVSForPhysicalNetwork:
            raise exceptions.InvalidSystemState(details=_(
                'Port %(port_id)s belong to VMWare VM, but there is no '
                'mapping from network %(net_id)s to DVS.') % {
                    'port_id': context.current['id'],
                    'net_id': context.network.current['id']})
        else:
            self._update_admin_state_up(dvs, context)

            force = context.original['status'] == 'DOWN'
            self._update_security_groups(dvs, context, force=force)
{'fraction_non_alphanumeric': 0.08902733118971061, 'fraction_numerical': 0.003114951768488746, 'mean_word_length': 2.5802158273381295, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4479864', 'n_tokens_mistral': 2844, 'n_tokens_neox': 2738, 'n_words': 670}