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

from neutron.common import rpc as n_rpc
from neutron.openstack.common import log as logging
from neutron.plugins.cisco.common import cisco_constants as c_constants

LOG = logging.getLogger(__name__)


class L3RouterJointAgentNotifyAPI(n_rpc.RpcProxy):
    """API for plugin to notify Cisco cfg agent."""
    BASE_RPC_API_VERSION = '1.0'

    def __init__(self, l3plugin, topic=c_constants.CFG_AGENT_L3_ROUTING):
        super(L3RouterJointAgentNotifyAPI, self).__init__(
            topic=topic, default_version=self.BASE_RPC_API_VERSION)
        self._l3plugin = l3plugin

    def _host_notification(self, context, method, payload, host,
                           topic=None):
        """Notify the cfg agent that is handling the hosting device."""
        LOG.debug('Notify Cisco cfg agent at %(host)s the message '
                  '%(method)s', {'host': host, 'method': method})
        self.cast(context,
                  self.make_msg(method, payload=payload),
                  topic='%s.%s' % (self.topic if topic is None else topic,
                                   host))

    def _agent_notification(self, context, method, routers, operation, data):
        """Notify individual Cisco cfg agents."""
        admin_context = context if context.is_admin else context.elevated()
        for router in routers:
            if router['hosting_device'] is None:
                continue
            agents = self._l3plugin.get_cfg_agents_for_hosting_devices(
                    admin_context, [router['hosting_device']['id']],
                    admin_state_up=True, active=True, schedule=True)
            for agent in agents:
                LOG.debug('Notify %(agent_type)s at %(topic)s.%(host)s the '
                          'message %(method)s',
                          {'agent_type': agent.agent_type,
                           'topic': c_constants.CFG_AGENT_L3_ROUTING,
                           'host': agent.host,
                           'method': method})
                self.cast(context,
                          self.make_msg(method, routers=[router['id']]),
                          topic='%s.%s' % (c_constants.CFG_AGENT_L3_ROUTING,
                                           agent.host))

    def router_deleted(self, context, router):
        """Notifies agents about a deleted router."""
        self._agent_notification(context, 'router_deleted', [router],
                                 operation=None, data=None)

    def routers_updated(self, context, routers, operation=None, data=None):
        """Notifies agents about configuration changes to routers.

        This includes operations performed on the router like when a
        router interface is added or removed.
        """
        if routers:
            self._agent_notification(context, 'routers_updated', routers,
                                     operation, data)

    def hosting_devices_removed(self, context, hosting_data, deconfigure,
                                host):
        """Notify cfg agent that some hosting devices have been removed.

        This notification informs the cfg agent in <host> that the
        hosting devices in the <hosting_data> dictionary have been removed
        from the hosting device pool. The <hosting_data> dictionary also
        contains the ids of the affected logical resources for each hosting
        devices:
             {'hd_id1': {'routers': [id1, id2, ...],
                         'fw': [id1, ...],
                         ...},
              'hd_id2': {'routers': [id3, id4, ...]},
                         'fw': [id1, ...],
                         ...},
              ...}
        The <deconfigure> argument is True if any configurations for the
        logical resources should be removed from the hosting devices
        """
        if hosting_data:
            self._host_notification(context, 'hosting_devices_removed',
                                    {'hosting_data': hosting_data,
                                     'deconfigure': deconfigure}, host,
                                    topic=c_constants.CFG_AGENT)

@@ -629,16 +629,44 @@
 nse.%0A#%0A%0A
+from oslo import messaging%0A%0A
 from neu
@@ -896,22 +896,14 @@
 API(
-n_rpc.RpcProxy
+object
 ):%0A 
@@ -957,242 +957,42 @@
 %22%22%22%0A
-    BASE_RPC_API_VERSION = '1.0'%0A%0A    def __init__(self, l3plugin, topic=c_constants.CFG_AGENT_L3_ROUTING):%0A        super(L3RouterJointAgentNotifyAPI, self).__init__(%0A            topic=topic, default_version=self.BASE_RPC_API_VERSION)
+%0A    def __init__(self, l3plugin):
 %0A   
@@ -1026,520 +1026,174 @@
 gin%0A
-%0A
     
-def _host_notification(self, context, method, payload, host,%0A                           topic=None):%0A        %22%22%22Notify the cfg agent that is handling the hosting device.%22%22%22%0A        LOG.debug('Notify Cisco cfg a
+    self.topic = c_constants.CFG_AGENT_L3_ROUTING%0A        tar
 ge
-n
 t 
-at %25(host)s the message '%0A                  '%25(method)s', %7B'host': host, 'method': method%7D)%0A        self.cast(context,%0A                  self.make_msg(method, payload=payload),%0A                  topic='%25s.%25s' %25 (self.topic if topic is None else topic,%0A                                   host)
+= messaging.Target(topic=self.topic, version='1.0')%0A        self.client = n_rpc.get_client(target
 )%0A%0A 
@@ -2110,231 +2110,121 @@
     
-self.cast(context,%0A                          self.make_msg(method, routers=%5Brouter%5B'id'%5D%5D),%0A                          topic='%25s.%25s' %25 (c_constants.CFG_AGENT_L3_ROUTING,%0A                                           agent.host)
+cctxt = self.client.prepare(server=agent.host)%0A                cctxt.cast(context, method, routers=%5Brouter%5B'id'%5D%5D
 )%0A%0A 
@@ -3808,16 +3808,20 @@
      if 
+not 
 hosting_
@@ -3842,171 +3842,194 @@
     
-self._host_notification(context, 'hosting_devices_removed',%0A                                    %7B'hosting_data': hosting_data,%0A                                    
+return%0A%0A        LOG.debug('Notify Cisco cfg agent at %25(host)s the message '%0A                  'hosting_devices_removed', %7B'host': host%7D)%0A%0A        payload = %7B'hosting_data': hosting_data,
  'de
@@ -4056,15 +4056,8 @@
 ure%7D
-, host,
 %0A   
@@ -4065,61 +4065,146 @@
     
-                            topic=c_constants.CFG_AGENT
+cctxt = self.client.prepare(topic=c_constants.CFG_AGENT, server=host)%0A        cctxt.cast(context, 'hosting_devices_removed', payload=payload
 )%0A

Drop RpcProxy usage from cisco.l3