]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Fix NVP plugin to send notifications for gateway-less subnets
authorarmando-migliaccio <amigliaccio@nicira.com>
Wed, 4 Sep 2013 20:11:43 +0000 (13:11 -0700)
committerarmando-migliaccio <amigliaccio@nicira.com>
Thu, 5 Sep 2013 00:53:54 +0000 (17:53 -0700)
It was noted that an update notification should be sent
regardless; this patch addresses that. Since there is
no longer the need to distinguish on whether to send
the RPC message or not, the operation has been factored
out to avoid code duplication.

Closes-Bug: 1220881

Change-Id: If553a84b7221f8c98d758654d317217a909c43dc

neutron/plugins/nicira/dhcp_meta/rpc.py
neutron/tests/unit/nicira/test_agent_scheduler.py
neutron/tests/unit/openvswitch/test_agent_scheduler.py

index b8d09baf48578a500b6455917b68cb703eea854d..d5146dab03614862823fc0173110ba230855ff16 100644 (file)
@@ -77,11 +77,7 @@ def handle_port_dhcp_access(plugin, context, port_data, action):
     if active_port:
         subnet_id = port_data['fixed_ips'][0]['subnet_id']
         subnet = plugin.get_subnet(context, subnet_id)
-        if (cfg.CONF.dhcp_agent_notification and subnet.get('gateway_ip')
-            or action == 'delete_port'):
-            dhcp_notifier = dhcp_rpc_agent_api.DhcpAgentNotifyAPI()
-            dhcp_notifier.notify(
-                context, {'subnet': subnet}, 'subnet.update.end')
+        _notify_rpc_agent(context, {'subnet': subnet}, 'subnet.update.end')
 
 
 def handle_port_metadata_access(context, port, is_delete=False):
@@ -207,12 +203,8 @@ def _create_metadata_access_network(plugin, context, router_id):
         # as it will be removed with the network
         plugin.delete_network(context, meta_net['id'])
 
-    if cfg.CONF.dhcp_agent_notification:
-        # We need to send a notification to the dhcp agent in
-        # order to start the metadata agent proxy
-        dhcp_notifier = dhcp_rpc_agent_api.DhcpAgentNotifyAPI()
-        dhcp_notifier.notify(context, {'network': meta_net},
-                             'network.create.end')
+    # Tell to start the metadata agent proxy
+    _notify_rpc_agent(context, {'network': meta_net}, 'network.create.end')
 
 
 def _destroy_metadata_access_network(plugin, context, router_id, ports):
@@ -235,11 +227,12 @@ def _destroy_metadata_access_network(plugin, context, router_id, ports):
         # must re-add the router interface
         plugin.add_router_interface(context, router_id,
                                     {'subnet_id': meta_sub_id})
+     # Tell to stop the metadata agent proxy
+    _notify_rpc_agent(
+        context, {'network': {'id': meta_net_id}}, 'network.delete.end')
+
 
+def _notify_rpc_agent(context, payload, event):
     if cfg.CONF.dhcp_agent_notification:
-        # We need to send a notification to the dhcp agent in
-        # order to stop the metadata agent proxy
         dhcp_notifier = dhcp_rpc_agent_api.DhcpAgentNotifyAPI()
-        dhcp_notifier.notify(context,
-                             {'network': {'id': meta_net_id}},
-                             'network.delete.end')
+        dhcp_notifier.notify(context, payload, event)
index 7bd2af700b142a255a15e802afc54edef4970199..d5d05ffb7e18d2a3b535db1194ce8088a65d44ae 100644 (file)
@@ -14,7 +14,9 @@
 #    under the License.
 
 import mock
+from oslo.config import cfg
 
+from neutron.common import constants
 from neutron.common.test_lib import test_config
 from neutron.plugins.nicira.common import sync
 from neutron.tests.unit.nicira import fake_nvpapiclient
@@ -50,6 +52,7 @@ class NVPDhcpAgentNotifierTestCase(test_base.OvsDhcpAgentNotifierTestCase):
         self.addCleanup(self.fc.reset_all)
         self.addCleanup(patch_sync.stop)
         self.addCleanup(self.mock_nvpapi.stop)
+        self.addCleanup(cfg.CONF.reset)
 
     def _notification_mocks(self, hosts, mock_dhcp, net, subnet, port):
         host_calls = {}
@@ -75,3 +78,23 @@ class NVPDhcpAgentNotifierTestCase(test_base.OvsDhcpAgentNotifierTestCase):
                     topic='dhcp_agent.' + host)]
             host_calls[host] = expected_calls
         return host_calls
+
+    def _test_gateway_subnet_notification(self, gateway='10.0.0.1'):
+        cfg.CONF.set_override('metadata_mode', 'dhcp_host_route', 'NVP')
+        hosts = ['hosta']
+        [mock_dhcp, net, subnet, port] = self._network_port_create(
+            hosts, gateway=gateway, owner=constants.DEVICE_OWNER_DHCP)
+        found = False
+        for call, topic in mock_dhcp.call_args_list:
+            method = call[1]['method']
+            if method == 'subnet_update_end':
+                found = True
+                break
+        self.assertTrue(found)
+        self.assertEqual(subnet['subnet']['gateway_ip'], gateway)
+
+    def test_gatewayless_subnet_notification(self):
+        self._test_gateway_subnet_notification(gateway=None)
+
+    def test_subnet_with_gateway_notification(self):
+        self._test_gateway_subnet_notification()
index 726a7f2779b2c3f52b26f32930de17a59845ec51..af1c6a14816d8c45e0492d580b7649556fca253a 100644 (file)
@@ -1018,7 +1018,8 @@ class OvsDhcpAgentNotifierTestCase(OvsAgentSchedulerTestCaseBase):
                     payload={'admin_state_up': False}),
                 topic='dhcp_agent.' + DHCP_HOSTA)
 
-    def _network_port_create(self, hosts):
+    def _network_port_create(
+        self, hosts, gateway=attributes.ATTR_NOT_SPECIFIED, owner=None):
         for host in hosts:
             self._register_one_agent_state(
                 {'binary': 'neutron-dhcp-agent',
@@ -1030,9 +1031,17 @@ class OvsDhcpAgentNotifierTestCase(OvsAgentSchedulerTestCaseBase):
         with mock.patch.object(self.dhcp_notifier, 'cast') as mock_dhcp:
             with self.network(do_delete=False) as net1:
                 with self.subnet(network=net1,
+                                 gateway_ip=gateway,
                                  do_delete=False) as subnet1:
-                    with self.port(subnet=subnet1, no_delete=True) as port:
-                        return [mock_dhcp, net1, subnet1, port]
+                    if owner:
+                        with self.port(subnet=subnet1,
+                                       no_delete=True,
+                                       device_owner=owner) as port:
+                            return [mock_dhcp, net1, subnet1, port]
+                    else:
+                        with self.port(subnet=subnet1,
+                                       no_delete=True) as port:
+                            return [mock_dhcp, net1, subnet1, port]
 
     def _notification_mocks(self, hosts, mock_dhcp, net, subnet, port):
         host_calls = {}