Browse Source

Merge "Extend ml2 plugin to notify nova of port status"

tags/6.0.0.0b1
Zuul 8 months ago
parent
commit
19cb5bb097
2 changed files with 79 additions and 17 deletions
  1. 31
    13
      networking_ovn/ml2/mech_driver.py
  2. 48
    4
      networking_ovn/tests/unit/ml2/test_mech_driver.py

+ 31
- 13
networking_ovn/ml2/mech_driver.py View File

@@ -35,6 +35,7 @@ from oslo_utils import timeutils
35 35
 
36 36
 from neutron.common import utils as n_utils
37 37
 from neutron.db import provisioning_blocks
38
+from neutron.plugins.ml2 import db as ml2_db
38 39
 from neutron.services.segments import db as segment_service_db
39 40
 
40 41
 from networking_ovn._i18n import _
@@ -394,7 +395,8 @@ class OVNMechanismDriver(api.MechanismDriver):
394 395
             return
395 396
         utils.validate_and_get_data_from_binding_profile(port)
396 397
         if self._is_port_provisioning_required(port, context.host):
397
-            self._insert_port_provisioning_block(context._plugin_context, port)
398
+            self._insert_port_provisioning_block(context._plugin_context,
399
+                                                 port['id'])
398 400
 
399 401
         db_rev.create_initial_revision(port['id'],
400 402
                                        ovn_const.TYPE_PORTS,
@@ -438,13 +440,12 @@ class OVNMechanismDriver(api.MechanismDriver):
438 440
 
439 441
         return True
440 442
 
441
-    def _insert_port_provisioning_block(self, context, port):
443
+    def _insert_port_provisioning_block(self, context, port_id):
442 444
         # Insert a provisioning block to prevent the port from
443 445
         # transitioning to active until OVN reports back that
444 446
         # the port is up.
445 447
         provisioning_blocks.add_provisioning_component(
446
-            context,
447
-            port['id'], resources.PORT,
448
+            context, port_id, resources.PORT,
448 449
             provisioning_blocks.L2_AGENT_ENTITY
449 450
         )
450 451
 
@@ -509,7 +510,8 @@ class OVNMechanismDriver(api.MechanismDriver):
509 510
         utils.validate_and_get_data_from_binding_profile(port)
510 511
         if self._is_port_provisioning_required(port, context.host,
511 512
                                                context.original_host):
512
-            self._insert_port_provisioning_block(context._plugin_context, port)
513
+            self._insert_port_provisioning_block(context._plugin_context,
514
+                                                 port['id'])
513 515
 
514 516
         if utils.is_lsp_router_port(port):
515 517
             # handle the case when an existing port is added to a
@@ -772,12 +774,18 @@ class OVNMechanismDriver(api.MechanismDriver):
772 774
             # becasue the router ports are unbind so, for OVN we are
773 775
             # forcing the status here. Maybe it's something that we can
774 776
             # change in core Neutron in the future.
775
-            port = self._plugin.get_port(admin_context, port_id)
776
-            if port.get('device_owner') in (const.DEVICE_OWNER_ROUTER_INTF,
777
-                                            const.DEVICE_OWNER_DVR_INTERFACE,
778
-                                            const.DEVICE_OWNER_ROUTER_HA_INTF):
777
+            db_port = ml2_db.get_port(admin_context, port_id)
778
+            if not db_port:
779
+                return
780
+
781
+            if db_port.device_owner in (const.DEVICE_OWNER_ROUTER_INTF,
782
+                                        const.DEVICE_OWNER_DVR_INTERFACE,
783
+                                        const.DEVICE_OWNER_ROUTER_HA_INTF):
779 784
                 self._plugin.update_port_status(admin_context, port_id,
780 785
                                                 const.PORT_STATUS_ACTIVE)
786
+            elif db_port.device_owner.startswith(
787
+                    const.DEVICE_OWNER_COMPUTE_PREFIX):
788
+                self._plugin.nova_notifier.notify_port_active_direct(db_port)
781 789
         except (os_db_exc.DBReferenceError, n_exc.PortNotFound):
782 790
             LOG.debug('Port not found during OVN status up report: %s',
783 791
                       port_id)
@@ -792,11 +800,21 @@ class OVNMechanismDriver(api.MechanismDriver):
792 800
         self._update_dnat_entry_if_needed(port_id, False)
793 801
         admin_context = n_context.get_admin_context()
794 802
         try:
795
-            port = self._plugin.get_port(admin_context, port_id)
796
-            self._insert_port_provisioning_block(admin_context, port)
797
-            self._plugin.update_port_status(admin_context,
798
-                                            port['id'],
803
+            db_port = ml2_db.get_port(admin_context, port_id)
804
+            if not db_port:
805
+                return
806
+
807
+            self._insert_port_provisioning_block(admin_context, port_id)
808
+            self._plugin.update_port_status(admin_context, port_id,
799 809
                                             const.PORT_STATUS_DOWN)
810
+
811
+            if db_port.device_owner.startswith(
812
+                    const.DEVICE_OWNER_COMPUTE_PREFIX):
813
+                self._plugin.nova_notifier.record_port_status_changed(
814
+                    db_port, const.PORT_STATUS_ACTIVE, const.PORT_STATUS_DOWN,
815
+                    None)
816
+                self._plugin.nova_notifier.send_port_status(
817
+                    None, None, db_port)
800 818
         except (os_db_exc.DBReferenceError, n_exc.PortNotFound):
801 819
             LOG.debug("Port not found during OVN status down report: %s",
802 820
                       port_id)

+ 48
- 4
networking_ovn/tests/unit/ml2/test_mech_driver.py View File

@@ -693,11 +693,14 @@ class TestOVNMechanismDriver(test_plugin.Ml2PluginV2TestCase):
693 693
                     self.assertEqual(
694 694
                         1, self.nb_ovn.update_address_set.call_count)
695 695
 
696
-    def test_set_port_status_up(self):
696
+    def _test_set_port_status_up(self, is_compute_port=False):
697
+        port_device_owner = 'compute:nova' if is_compute_port else ''
698
+        self.mech_driver._plugin.nova_notifier = mock.Mock()
697 699
         with self.network(set_context=True, tenant_id='test') as net1, \
698 700
             self.subnet(network=net1) as subnet1, \
699 701
             self.port(subnet=subnet1, set_context=True,
700
-                      tenant_id='test') as port1, \
702
+                      tenant_id='test',
703
+                      device_owner=port_device_owner) as port1, \
701 704
             mock.patch('neutron.db.provisioning_blocks.'
702 705
                        'provisioning_complete') as pc, \
703 706
             mock.patch.object(
@@ -715,11 +718,30 @@ class TestOVNMechanismDriver(test_plugin.Ml2PluginV2TestCase):
715 718
                 upd_subport.assert_called_once_with(port1['port']['id'])
716 719
                 ude.assert_called_once_with(port1['port']['id'])
717 720
 
718
-    def test_set_port_status_down(self):
721
+                # If the port does NOT bellong to compute, do not notify Nova
722
+                # about it's status changes
723
+                if not is_compute_port:
724
+                    self.mech_driver._plugin.nova_notifier.\
725
+                        notify_port_active_direct.assert_not_called()
726
+                else:
727
+                    self.mech_driver._plugin.nova_notifier.\
728
+                        notify_port_active_direct.assert_called_once_with(
729
+                            mock.ANY)
730
+
731
+    def test_set_port_status_up(self):
732
+        self._test_set_port_status_up(is_compute_port=False)
733
+
734
+    def test_set_compute_port_status_up(self):
735
+        self._test_set_port_status_up(is_compute_port=True)
736
+
737
+    def _test_set_port_status_down(self, is_compute_port=False):
738
+        port_device_owner = 'compute:nova' if is_compute_port else ''
739
+        self.mech_driver._plugin.nova_notifier = mock.Mock()
719 740
         with self.network(set_context=True, tenant_id='test') as net1, \
720 741
             self.subnet(network=net1) as subnet1, \
721 742
             self.port(subnet=subnet1, set_context=True,
722
-                      tenant_id='test') as port1, \
743
+                      tenant_id='test',
744
+                      device_owner=port_device_owner) as port1, \
723 745
             mock.patch('neutron.db.provisioning_blocks.'
724 746
                        'add_provisioning_component') as apc, \
725 747
             mock.patch.object(self.mech_driver,
@@ -733,6 +755,28 @@ class TestOVNMechanismDriver(test_plugin.Ml2PluginV2TestCase):
733 755
                 )
734 756
                 ude.assert_called_once_with(port1['port']['id'], False)
735 757
 
758
+                # If the port does NOT bellong to compute, do not notify Nova
759
+                # about it's status changes
760
+                if not is_compute_port:
761
+                    self.mech_driver._plugin.nova_notifier.\
762
+                        record_port_status_changed.assert_not_called()
763
+                    self.mech_driver._plugin.nova_notifier.\
764
+                        send_port_status.assert_not_called()
765
+                else:
766
+                    self.mech_driver._plugin.nova_notifier.\
767
+                        record_port_status_changed.assert_called_once_with(
768
+                            mock.ANY, const.PORT_STATUS_ACTIVE,
769
+                            const.PORT_STATUS_DOWN, None)
770
+                    self.mech_driver._plugin.nova_notifier.\
771
+                        send_port_status.assert_called_once_with(
772
+                            None, None, mock.ANY)
773
+
774
+    def test_set_port_status_down(self):
775
+        self._test_set_port_status_down(is_compute_port=False)
776
+
777
+    def test_set_compute_port_status_down(self):
778
+        self._test_set_port_status_down(is_compute_port=True)
779
+
736 780
     def test_set_port_status_down_not_found(self):
737 781
         with mock.patch('neutron.db.provisioning_blocks.'
738 782
                         'add_provisioning_component') as apc, \

Loading…
Cancel
Save