<commit_before># Copyright (c) 2014 Cisco Systems 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 re
import sys

import eventlet

eventlet.monkey_patch()

from oslo.config import cfg

from neutron.agent.common import config
from neutron.agent.linux import ip_lib
from neutron.agent.linux import utils
from neutron.common import config as common_cfg
from neutron.common import rpc
from neutron.common import utils as neutron_utils
from neutron.db import agents_db
from neutron import manager
from neutron.openstack.common.gettextutils import _LE, _LI
from neutron.openstack.common import lockutils
from neutron.openstack.common import log as logging
from neutron.openstack.common import periodic_task
from neutron.openstack.common import service as svc
from neutron.plugins.ml2.drivers.cisco.apic import mechanism_apic as ma
from neutron.plugins.ml2.drivers import type_vlan  # noqa

from neutron import service

ACI_PORT_DESCR_FORMATS = [
    'topology/pod-1/node-(\d+)/sys/conng/path-\[eth(\d+)/(\d+)\]',
    'topology/pod-1/paths-(\d+)/pathep-\[eth(\d+)/(\d+)\]',
]
AGENT_FORCE_UPDATE_COUNT = 100
BINARY_APIC_SERVICE_AGENT = 'neutron-cisco-apic-service-agent'
BINARY_APIC_HOST_AGENT = 'neutron-cisco-apic-host-agent'
TOPIC_APIC_SERVICE = 'apic-service'
TYPE_APIC_SERVICE_AGENT = 'cisco-apic-service-agent'
TYPE_APIC_HOST_AGENT = 'cisco-apic-host-agent'


LOG = logging.getLogger(__name__)


class ApicTopologyService(manager.Manager):

    RPC_API_VERSION = '1.1'

    def __init__(self, host=None):
        if host is None:
            host = neutron_utils.get_hostname()
        super(ApicTopologyService, self).__init__(host=host)

        self.conf = cfg.CONF.ml2_cisco_apic
        self.conn = None
        self.peers = {}
        self.invalid_peers = []
        self.dispatcher = None
        self.state = None
        self.state_agent = None
        self.topic = TOPIC_APIC_SERVICE
        self.apic_manager = ma.APICMechanismDriver.get_apic_manager(False)

    def init_host(self):
        LOG.info(_LI("APIC service agent starting ..."))
        self.state = {
            'binary': BINARY_APIC_SERVICE_AGENT,
            'host': self.host,
            'topic': self.topic,
            'configurations': {},
            'start_flag': True,
            'agent_type': TYPE_APIC_SERVICE_AGENT,
        }

        self.conn = rpc.create_connection(new=True)
        self.dispatcher = [self, agents_db.AgentExtRpcCallback()]
        self.conn.create_consumer(
            self.topic, self.dispatcher, fanout=True)
        self.conn.consume_in_threads()

    def after_start(self):
        LOG.info(_LI("APIC service agent started"))

    def report_send(self, context):
        if not self.state_agent:
            return
        LOG.debug("APIC service agent: sending report state")

        try:
            self.state_agent.report_state(context, self.state)
            self.state.pop('start_flag', None)
        except AttributeError:
            # This means the server does not support report_state
            # ignore it
            return
        except Exception:
            LOG.exception(_LE("APIC service agent: failed in reporting state"))

    @lockutils.synchronized('apic_service')
    def update_link(self, context,
                    host, interface, mac,
                    switch, module, port):
        LOG.debug("APIC service agent: received update_link: %s",
                  ", ".join(map(str,
                                [host, interface, mac, switch, module, port])))

        nlink = (host, interface, mac, switch, module, port)
        clink = self.peers.get((host, interface), None)

        if switch == 0:
            # this is a link delete, remove it
            if clink is not None:
                self.apic_manager.remove_hostlink(*clink)
                self.peers.pop((host, interface))
        else:
            if clink is None:
                # add new link to database
                self.apic_manager.add_hostlink(*nlink)
                self.peers[(host, interface)] = nlink
            elif clink != nlink:
                # delete old link and add new one (don't update in place)
                self.apic_manager.remove_hostlink(*clink)
                self.peers.pop((host, interface))
                self.apic_manager.add_hostlink(*nlink)
                self.peers[(host, interface)] = nlink


class ApicTopologyServiceNotifierApi(rpc.RpcProxy):

    RPC_API_VERSION = '1.1'

    def __init__(self):
        super(ApicTopologyServiceNotifierApi, self).__init__(
            topic=TOPIC_APIC_SERVICE,
            default_version=self.RPC_API_VERSION)

    def update_link(self, context, host, interface, mac, switch, module, port):
        self.fanout_cast(
            context, self.make_msg(
                'update_link',
                host=host, interface=interface, mac=mac,
                switch=switch, module=module, port=port),
            topic=TOPIC_APIC_SERVICE)

    def delete_link(self, context, host, interface):
        self.fanout_cast(
            context, self.make_msg(
                'delete_link',
                host=host, interface=interface, mac=None,
                switch=0, module=0, port=0),
            topic=TOPIC_APIC_SERVICE)


class ApicTopologyAgent(manager.Manager):
    def __init__(self, host=None):
        if host is None:<commit_msg>Drop RpcProxy usage from cisco apic ml2 plugin<commit_after># Copyright (c) 2014 Cisco Systems 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 re
import sys

import eventlet

eventlet.monkey_patch()

from oslo.config import cfg
from oslo import messaging

from neutron.agent.common import config
from neutron.agent.linux import ip_lib
from neutron.agent.linux import utils
from neutron.common import config as common_cfg
from neutron.common import rpc
from neutron.common import utils as neutron_utils
from neutron.db import agents_db
from neutron import manager
from neutron.openstack.common.gettextutils import _LE, _LI
from neutron.openstack.common import lockutils
from neutron.openstack.common import log as logging
from neutron.openstack.common import periodic_task
from neutron.openstack.common import service as svc
from neutron.plugins.ml2.drivers.cisco.apic import mechanism_apic as ma
from neutron.plugins.ml2.drivers import type_vlan  # noqa

from neutron import service

ACI_PORT_DESCR_FORMATS = [
    'topology/pod-1/node-(\d+)/sys/conng/path-\[eth(\d+)/(\d+)\]',
    'topology/pod-1/paths-(\d+)/pathep-\[eth(\d+)/(\d+)\]',
]
AGENT_FORCE_UPDATE_COUNT = 100
BINARY_APIC_SERVICE_AGENT = 'neutron-cisco-apic-service-agent'
BINARY_APIC_HOST_AGENT = 'neutron-cisco-apic-host-agent'
TOPIC_APIC_SERVICE = 'apic-service'
TYPE_APIC_SERVICE_AGENT = 'cisco-apic-service-agent'
TYPE_APIC_HOST_AGENT = 'cisco-apic-host-agent'


LOG = logging.getLogger(__name__)


class ApicTopologyService(manager.Manager):

    RPC_API_VERSION = '1.1'

    def __init__(self, host=None):
        if host is None:
            host = neutron_utils.get_hostname()
        super(ApicTopologyService, self).__init__(host=host)

        self.conf = cfg.CONF.ml2_cisco_apic
        self.conn = None
        self.peers = {}
        self.invalid_peers = []
        self.dispatcher = None
        self.state = None
        self.state_agent = None
        self.topic = TOPIC_APIC_SERVICE
        self.apic_manager = ma.APICMechanismDriver.get_apic_manager(False)

    def init_host(self):
        LOG.info(_LI("APIC service agent starting ..."))
        self.state = {
            'binary': BINARY_APIC_SERVICE_AGENT,
            'host': self.host,
            'topic': self.topic,
            'configurations': {},
            'start_flag': True,
            'agent_type': TYPE_APIC_SERVICE_AGENT,
        }

        self.conn = rpc.create_connection(new=True)
        self.dispatcher = [self, agents_db.AgentExtRpcCallback()]
        self.conn.create_consumer(
            self.topic, self.dispatcher, fanout=True)
        self.conn.consume_in_threads()

    def after_start(self):
        LOG.info(_LI("APIC service agent started"))

    def report_send(self, context):
        if not self.state_agent:
            return
        LOG.debug("APIC service agent: sending report state")

        try:
            self.state_agent.report_state(context, self.state)
            self.state.pop('start_flag', None)
        except AttributeError:
            # This means the server does not support report_state
            # ignore it
            return
        except Exception:
            LOG.exception(_LE("APIC service agent: failed in reporting state"))

    @lockutils.synchronized('apic_service')
    def update_link(self, context,
                    host, interface, mac,
                    switch, module, port):
        LOG.debug("APIC service agent: received update_link: %s",
                  ", ".join(map(str,
                                [host, interface, mac, switch, module, port])))

        nlink = (host, interface, mac, switch, module, port)
        clink = self.peers.get((host, interface), None)

        if switch == 0:
            # this is a link delete, remove it
            if clink is not None:
                self.apic_manager.remove_hostlink(*clink)
                self.peers.pop((host, interface))
        else:
            if clink is None:
                # add new link to database
                self.apic_manager.add_hostlink(*nlink)
                self.peers[(host, interface)] = nlink
            elif clink != nlink:
                # delete old link and add new one (don't update in place)
                self.apic_manager.remove_hostlink(*clink)
                self.peers.pop((host, interface))
                self.apic_manager.add_hostlink(*nlink)
                self.peers[(host, interface)] = nlink


class ApicTopologyServiceNotifierApi(object):

    def __init__(self):
        target = messaging.Target(topic=TOPIC_APIC_SERVICE, version='1.0')
        self.client = rpc.get_client(target)

    def update_link(self, context, host, interface, mac, switch, module, port):
        cctxt = self.client.prepare(version='1.1', fanout=True)
        cctxt.cast(context, 'update_link', host=host, interface=interface,
                   mac=mac, switch=switch, module=module, port=port)

    def delete_link(self, context, host, interface):
        cctxt = self.client.prepare(version='1.1', fanout=True)
        cctxt.cast(context, 'delete_link', host=host, interface=interface,
                   mac=None, switch=0, module=0, port=0)


class ApicTopologyAgent(manager.Manager):
    def __init__(self, host=None):
        if host is None:
{'fraction_non_alphanumeric': 0.09643070108186387, 'fraction_numerical': 0.004514864979981259, 'mean_word_length': 2.589116478141241, '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': '5827741', 'n_tokens_mistral': 3563, 'n_tokens_neox': 3447, 'n_words': 887}