<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.

import abc
import re

import six
from oslo import messaging as oslo_messaging
from oslo_log import log
from oslo_concurrency import lockutils
from neutron.common import constants as n_const
from neutron.common import rpc as n_rpc
from neutron import manager
from neutron.extensions import portbindings
from neutron.openstack.common import lockutils
from neutron.plugins.common import constants
from neutron.plugins.ml2 import driver_api, driver_context
from neutron.context import Context

from neutron.openstack.common.gettextutils import _LI, _
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__)

FAKE_PORT_ID = 'fake_id'


@six.add_metaclass(abc.ABCMeta)
class EndPointBase(object):
    event_type_regex = None

    def __init__(self, driver):
        self.driver = driver

    def info(self, ctxt, publisher_id, event_type, payload, metadata):
        if (publisher_id == 'network.manager' and
                re.match(self.event_type_regex)):
            self._execute(ctxt, event_type, payload)

    @abc.abstractmethod
    def _execute(self, ctxt, event_type, payload):
        pass

    def update_security_group(self, ctxt, security_group_id):
        plugin_context = Context.from_dict(ctxt)
        plugin = manager.NeutronManager.get_plugin()
        fake_network = {
            'id': 'fake'
        }
        fake_port = {
            'id': FAKE_PORT_ID,
            'security_groups': [security_group_id]
        }
        context = driver_context.PortContext(
            plugin, plugin_context, fake_port, fake_network, None, None
        )
        for dvs in self.driver.network_map.values():
            self.driver._update_security_groups(dvs, context, force=True)


class SecurityGroupRuleCreateEndPoint(EndPointBase):
    event_type_regex = r'security_group_rule\.create\.end'

    def _execute(self, ctxt, event_type, payload):
        security_group_id = payload['security_group_rule']['security_group_id']
        self.update_security_group(ctxt, security_group_id)


class SecurityGroupRuleDeleteEndPoint(EndPointBase):
    event_type_regex = r'security_group_rule\.delete\.(start|end)'

    sgr_to_sg = {}

    def _execute(self, ctxt, event_type, payload):
        security_group_rule_id = payload['security_group_rule_id']
        if event_type.endswith('start'):
            security_group_id = self.get_security_group_for(
                ctxt,
                security_group_rule_id)
            self.sgr_to_sg[security_group_rule_id] = security_group_id
        else:
            security_group_id = self.sgr_to_sg.pop(security_group_rule_id)
            self.update_security_group(ctxt, security_group_id)
<commit_msg>Correction on rpc filter<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.

import abc
import re

import six
from oslo import messaging as oslo_messaging
from oslo_log import log
from oslo_concurrency import lockutils
from neutron.common import constants as n_const
from neutron.common import rpc as n_rpc
from neutron import manager
from neutron.extensions import portbindings
from neutron.openstack.common import lockutils
from neutron.plugins.common import constants
from neutron.plugins.ml2 import driver_api, driver_context
from neutron.context import Context

from neutron.openstack.common.gettextutils import _LI, _
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__)

FAKE_PORT_ID = 'fake_id'


@six.add_metaclass(abc.ABCMeta)
class EndPointBase(object):
    event_type_regex = None

    def __init__(self, driver):
        self.driver = driver

    def info(self, ctxt, publisher_id, event_type, payload, metadata):
        if re.match(self.event_type_regex, event_type):
            self._execute(ctxt, event_type, payload)

    @abc.abstractmethod
    def _execute(self, ctxt, event_type, payload):
        pass

    def update_security_group(self, ctxt, security_group_id):
        plugin_context = Context.from_dict(ctxt)
        plugin = manager.NeutronManager.get_plugin()
        fake_network = {
            'id': 'fake'
        }
        fake_port = {
            'id': FAKE_PORT_ID,
            'security_groups': [security_group_id]
        }
        context = driver_context.PortContext(
            plugin, plugin_context, fake_port, fake_network, None, None
        )
        for dvs in self.driver.network_map.values():
            self.driver._update_security_groups(dvs, context, force=True)


class SecurityGroupRuleCreateEndPoint(EndPointBase):
    event_type_regex = r'security_group_rule.create\.end'

    def _execute(self, ctxt, event_type, payload):
        security_group_id = payload['security_group_rule']['security_group_id']
        self.update_security_group(ctxt, security_group_id)


class SecurityGroupRuleDeleteEndPoint(EndPointBase):
    event_type_regex = r'security_group_rule\.delete\.(start|end)'

    sgr_to_sg = {}

    def _execute(self, ctxt, event_type, payload):
        security_group_rule_id = payload['security_group_rule_id']
        if event_type.endswith('start'):
            security_group_id = self.get_security_group_for(
                ctxt,
                security_group_rule_id)
            self.sgr_to_sg[security_group_rule_id] = security_group_id
        else:
            security_group_id = self.sgr_to_sg.pop(security_group_rule_id)
            self.update_security_group(ctxt, security_group_id)

{'fraction_non_alphanumeric': 0.0728042019258827, 'fraction_numerical': 0.0026262036766851473, 'mean_word_length': 3.5128373930217247, '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': '7454832', 'n_tokens_mistral': 2046, 'n_tokens_neox': 2001, 'n_words': 541}