Browse Source

use get_port_binding_by_status_and_host from lib

The get_port_binding_by_status_and_host function was rehomed into
neutron-lib with https://review.openstack.org/#/c/580786/ and released
in neutron-lib 1.18.0. This patch consumes the function by removing it
in neutron and replacing all uses with lib's version.

NeutronLibImpact

Change-Id: Iac3246d0eb59709749e0b7e857091447d11a0133
tags/13.0.0.0rc1
Boden R 10 months ago
parent
commit
d7942945c9

+ 1
- 1
neutron/agent/rpc.py View File

@@ -23,6 +23,7 @@ from neutron_lib.callbacks import events as callback_events
23 23
 from neutron_lib.callbacks import registry
24 24
 from neutron_lib.callbacks import resources as callback_resources
25 25
 from neutron_lib import constants
26
+from neutron_lib.plugins import utils
26 27
 from oslo_log import log as logging
27 28
 import oslo_messaging
28 29
 from oslo_utils import uuidutils
@@ -31,7 +32,6 @@ from neutron.agent import resource_cache
31 32
 from neutron.api.rpc.callbacks import resources
32 33
 from neutron.common import constants as n_const
33 34
 from neutron.common import rpc as n_rpc
34
-from neutron.common import utils
35 35
 from neutron import objects
36 36
 
37 37
 LOG = logging.getLogger(__name__)

+ 4
- 5
neutron/common/exceptions.py View File

@@ -18,6 +18,10 @@ from neutron_lib import exceptions as e
18 18
 from neutron._i18n import _
19 19
 
20 20
 
21
+# TODO(boden): remove rpc shims
22
+PortBindingNotFound = e.PortBindingNotFound
23
+
24
+
21 25
 class SubnetPoolNotFound(e.NotFound):
22 26
     message = _("Subnet pool %(subnetpool_id)s could not be found.")
23 27
 
@@ -336,11 +340,6 @@ class FailedToAddQdiscToDevice(e.NeutronException):
336 340
                 "to device %(device)s.")
337 341
 
338 342
 
339
-class PortBindingNotFound(e.NotFound):
340
-    message = _("Binding for port %(port_id)s for host %(host)s could not be "
341
-                "found.")
342
-
343
-
344 343
 class PortBindingAlreadyActive(e.Conflict):
345 344
     message = _("Binding for port %(port_id)s on host %(host)s is already "
346 345
                 "active.")

+ 0
- 35
neutron/common/utils.py View File

@@ -33,7 +33,6 @@ import uuid
33 33
 import eventlet
34 34
 from eventlet.green import subprocess
35 35
 import netaddr
36
-from neutron_lib.api.definitions import portbindings_extended as pb_ext
37 36
 from neutron_lib import constants as n_const
38 37
 from neutron_lib.utils import helpers
39 38
 from oslo_config import cfg
@@ -45,7 +44,6 @@ import six
45 44
 import neutron
46 45
 from neutron._i18n import _
47 46
 from neutron.api import api_common
48
-from neutron.common import exceptions
49 47
 from neutron.db import api as db_api
50 48
 
51 49
 TIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ"
@@ -788,39 +786,6 @@ def bits_to_kilobits(value, base):
788 786
     return int((value + (base - 1)) / base)
789 787
 
790 788
 
791
-def get_port_binding_by_status_and_host(bindings, status, host='',
792
-                                        raise_if_not_found=False,
793
-                                        port_id=None):
794
-    """Returns from an iterable the binding with the specified status and host.
795
-
796
-    The input iterable can contain zero or one binding in status ACTIVE
797
-    and zero or many bindings in status INACTIVE. As a consequence, to
798
-    unequivocally retrieve an inactive binding, the caller must specify a non
799
-    empty value for host. If host is the empty string, the first binding
800
-    satisfying the specified status will be returned. If no binding is found
801
-    with the specified status and host, None is returned or PortBindingNotFound
802
-    is raised if raise_if_not_found is True
803
-
804
-    :param bindings: An iterable containing port bindings
805
-    :param status: The status of the port binding to return. Possible values
806
-                   are ACTIVE or INACTIVE as defined in
807
-                   :file:`neutron_lib/constants.py`.
808
-    :param host: str representing the host of the binding to return.
809
-    :param raise_if_not_found: If a binding is not found and this parameter is
810
-                               True, a PortBindingNotFound exception is raised
811
-    :param port_id: The id of the binding's port
812
-    :returns: The searched for port binding or None if it is not found
813
-    :raises: PortBindingNotFound if the binding is not found and
814
-             raise_if_not_found is True
815
-    """
816
-    for binding in bindings:
817
-        if binding[pb_ext.STATUS] == status:
818
-            if not host or binding[pb_ext.HOST] == host:
819
-                return binding
820
-    if raise_if_not_found:
821
-        raise exceptions.PortBindingNotFound(port_id=port_id, host=host)
822
-
823
-
824 789
 def disable_extension_by_service_plugin(core_plugin, service_plugin):
825 790
     if ('filter-validation' in core_plugin.supported_extension_aliases and
826 791
             not api_common.is_filter_validation_supported(service_plugin)):

+ 12
- 12
neutron/plugins/ml2/plugin.py View File

@@ -254,7 +254,7 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
254 254
                           **kwargs):
255 255
         port_id = object_id
256 256
         port = db.get_port(context, port_id)
257
-        port_binding = utils.get_port_binding_by_status_and_host(
257
+        port_binding = p_utils.get_port_binding_by_status_and_host(
258 258
             getattr(port, 'port_bindings', []), const.ACTIVE)
259 259
         if not port or not port_binding:
260 260
             LOG.debug("Port %s was deleted so its status cannot be updated.",
@@ -510,7 +510,7 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
510 510
             # mechanism driver update_port_*commit() calls.
511 511
             try:
512 512
                 port_db = self._get_port(plugin_context, port_id)
513
-                cur_binding = utils.get_port_binding_by_status_and_host(
513
+                cur_binding = p_utils.get_port_binding_by_status_and_host(
514 514
                     port_db.port_bindings, const.ACTIVE)
515 515
             except exc.PortNotFound:
516 516
                 port_db, cur_binding = None, None
@@ -558,7 +558,7 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
558 558
             cur_context_binding = cur_binding
559 559
             if new_binding.status == const.INACTIVE:
560 560
                 cur_context_binding = (
561
-                    utils.get_port_binding_by_status_and_host(
561
+                    p_utils.get_port_binding_by_status_and_host(
562 562
                         port_db.port_bindings, const.INACTIVE,
563 563
                         host=new_binding.host))
564 564
             cur_context = driver_context.PortContext(
@@ -665,7 +665,7 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
665 665
     @resource_extend.extends([port_def.COLLECTION_NAME])
666 666
     def _ml2_extend_port_dict_binding(port_res, port_db):
667 667
         plugin = directory.get_plugin()
668
-        port_binding = utils.get_port_binding_by_status_and_host(
668
+        port_binding = p_utils.get_port_binding_by_status_and_host(
669 669
             port_db.port_bindings, const.ACTIVE)
670 670
         # None when called during unit tests for other plugins.
671 671
         if port_binding:
@@ -1339,7 +1339,7 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
1339 1339
                         original_port=original_port)
1340 1340
         with db_api.context_manager.writer.using(context):
1341 1341
             port_db = self._get_port(context, id)
1342
-            binding = utils.get_port_binding_by_status_and_host(
1342
+            binding = p_utils.get_port_binding_by_status_and_host(
1343 1343
                 port_db.port_bindings, const.ACTIVE)
1344 1344
             if not binding:
1345 1345
                 raise exc.PortNotFound(port_id=id)
@@ -1558,7 +1558,7 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
1558 1558
         with db_api.context_manager.writer.using(context):
1559 1559
             try:
1560 1560
                 port_db = self._get_port(context, id)
1561
-                binding = utils.get_port_binding_by_status_and_host(
1561
+                binding = p_utils.get_port_binding_by_status_and_host(
1562 1562
                     port_db.port_bindings, const.ACTIVE,
1563 1563
                     raise_if_not_found=True, port_id=id)
1564 1564
             except exc.PortNotFound:
@@ -1678,7 +1678,7 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
1678 1678
                 # related attribute port_binding could disappear in
1679 1679
                 # concurrent port deletion.
1680 1680
                 # It's not an error condition.
1681
-                binding = utils.get_port_binding_by_status_and_host(
1681
+                binding = p_utils.get_port_binding_by_status_and_host(
1682 1682
                     port_db.port_bindings, const.ACTIVE)
1683 1683
                 if not binding:
1684 1684
                     LOG.info("Binding info for port %s was not found, "
@@ -1722,7 +1722,7 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
1722 1722
                         plugin_context, port['id'], host)
1723 1723
                     bindlevelhost_match = host
1724 1724
                 else:
1725
-                    binding = utils.get_port_binding_by_status_and_host(
1725
+                    binding = p_utils.get_port_binding_by_status_and_host(
1726 1726
                         port_db.port_bindings, const.ACTIVE)
1727 1727
                     bindlevelhost_match = binding.host if binding else None
1728 1728
                 if not binding:
@@ -1808,7 +1808,7 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
1808 1808
                 # listening for db events can modify the port if necessary
1809 1809
                 context.session.flush()
1810 1810
                 updated_port = self._make_port_dict(port)
1811
-                binding = utils.get_port_binding_by_status_and_host(
1811
+                binding = p_utils.get_port_binding_by_status_and_host(
1812 1812
                     port.port_bindings, const.ACTIVE, raise_if_not_found=True,
1813 1813
                     port_id=port_id)
1814 1814
                 levels = db.get_binding_levels(context, port.id, binding.host)
@@ -2061,7 +2061,7 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
2061 2061
                     port_id=port_id, host=attrs[pbe_ext.HOST])
2062 2062
             status = const.ACTIVE
2063 2063
             is_active_binding = True
2064
-            active_binding = utils.get_port_binding_by_status_and_host(
2064
+            active_binding = p_utils.get_port_binding_by_status_and_host(
2065 2065
                 port_db.port_bindings, const.ACTIVE)
2066 2066
             if active_binding:
2067 2067
                 status = const.INACTIVE
@@ -2173,12 +2173,12 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
2173 2173
                 port_id = port_id['port_id']
2174 2174
             port_db = self._get_port(context, port_id)
2175 2175
             self._validate_compute_port(port_db)
2176
-            active_binding = utils.get_port_binding_by_status_and_host(
2176
+            active_binding = p_utils.get_port_binding_by_status_and_host(
2177 2177
                 port_db.port_bindings, const.ACTIVE)
2178 2178
             if host == (active_binding and active_binding.host):
2179 2179
                 raise n_exc.PortBindingAlreadyActive(port_id=port_id,
2180 2180
                                                      host=host)
2181
-            inactive_binding = utils.get_port_binding_by_status_and_host(
2181
+            inactive_binding = p_utils.get_port_binding_by_status_and_host(
2182 2182
                 port_db.port_bindings, const.INACTIVE, host=host)
2183 2183
             if not inactive_binding or inactive_binding.host != host:
2184 2184
                 raise n_exc.PortBindingNotFound(port_id=port_id, host=host)

+ 1
- 1
neutron/services/logapi/common/validators.py View File

@@ -17,10 +17,10 @@ from neutron_lib.api.definitions import portbindings
17 17
 from neutron_lib import constants
18 18
 from neutron_lib.plugins import constants as plugin_const
19 19
 from neutron_lib.plugins import directory
20
+from neutron_lib.plugins import utils
20 21
 from oslo_log import log as logging
21 22
 from sqlalchemy.orm import exc as orm_exc
22 23
 
23
-from neutron.common import utils
24 24
 from neutron.db import _utils as db_utils
25 25
 from neutron.db.models import securitygroup as sg_db
26 26
 from neutron.objects import ports

+ 1
- 1
neutron/services/qos/drivers/manager.py View File

@@ -14,6 +14,7 @@ from neutron_lib.api.definitions import portbindings
14 14
 from neutron_lib.callbacks import events
15 15
 from neutron_lib.callbacks import registry
16 16
 from neutron_lib import constants as lib_constants
17
+from neutron_lib.plugins import utils
17 18
 from neutron_lib.services.qos import constants as qos_consts
18 19
 from oslo_log import log as logging
19 20
 
@@ -23,7 +24,6 @@ from neutron.api.rpc.callbacks import resources
23 24
 from neutron.api.rpc.handlers import resources_rpc
24 25
 from neutron.common import constants
25 26
 from neutron.common import exceptions
26
-from neutron.common import utils
27 27
 from neutron.objects.qos import policy as policy_object
28 28
 
29 29
 

+ 0
- 32
neutron/tests/unit/common/test_utils.py View File

@@ -21,18 +21,14 @@ import ddt
21 21
 import eventlet
22 22
 import mock
23 23
 import netaddr
24
-from neutron_lib.api.definitions import portbindings_extended as pb_ext
25 24
 from neutron_lib import constants
26 25
 from oslo_log import log as logging
27
-from oslo_utils import uuidutils
28 26
 import six
29 27
 import testscenarios
30 28
 import testtools
31 29
 
32 30
 from neutron.common import constants as common_constants
33
-from neutron.common import exceptions
34 31
 from neutron.common import utils
35
-from neutron.objects import ports
36 32
 from neutron.tests import base
37 33
 from neutron.tests.unit import tests
38 34
 
@@ -528,31 +524,3 @@ class TestIECUnitConversions(BaseUnitConversionTest, base.BaseTestCase):
528 524
                 expected_kilobits,
529 525
                 utils.bits_to_kilobits(input_bits, self.base_unit)
530 526
             )
531
-
532
-
533
-class TestGetPortBindingByStatusAndHost(base.BaseTestCase):
534
-
535
-    def test_get_port_binding_by_status_and_host(self):
536
-        bindings = []
537
-        self.assertIsNone(utils.get_port_binding_by_status_and_host(
538
-                              bindings, constants.INACTIVE))
539
-        bindings.extend([ports.PortBinding(
540
-                             port_id=uuidutils.generate_uuid(), host='host-1',
541
-                             status=constants.INACTIVE),
542
-                         ports.PortBinding(
543
-                             port_id=uuidutils.generate_uuid(), host='host-2',
544
-                             status=constants.INACTIVE)])
545
-        self.assertEqual(
546
-            'host-1', utils.get_port_binding_by_status_and_host(
547
-                          bindings,
548
-                          constants.INACTIVE)[pb_ext.HOST])
549
-        self.assertEqual(
550
-            'host-2', utils.get_port_binding_by_status_and_host(
551
-                          bindings,
552
-                          constants.INACTIVE,
553
-                          host='host-2')[pb_ext.HOST])
554
-        self.assertIsNone(utils.get_port_binding_by_status_and_host(
555
-                              bindings, constants.ACTIVE))
556
-        self.assertRaises(exceptions.PortBindingNotFound,
557
-                          utils.get_port_binding_by_status_and_host, bindings,
558
-                          constants.ACTIVE, 'host', True, 'port_id')

+ 6
- 5
neutron/tests/unit/plugins/ml2/test_plugin.py View File

@@ -35,6 +35,7 @@ from neutron_lib import fixture
35 35
 from neutron_lib.plugins import constants as plugin_constants
36 36
 from neutron_lib.plugins import directory
37 37
 from neutron_lib.plugins.ml2 import api as driver_api
38
+from neutron_lib.plugins import utils as p_utils
38 39
 from oslo_config import cfg
39 40
 from oslo_db import exception as db_exc
40 41
 from oslo_utils import uuidutils
@@ -1810,7 +1811,7 @@ class TestMl2PortBinding(Ml2PluginV2TestCase,
1810 1811
         # create a port and delete it so we have an expired mechanism context
1811 1812
         with self.port() as port:
1812 1813
             plugin = directory.get_plugin()
1813
-            binding = utils.get_port_binding_by_status_and_host(
1814
+            binding = p_utils.get_port_binding_by_status_and_host(
1814 1815
                 plugin._get_port(self.context,
1815 1816
                                  port['port']['id']).port_bindings,
1816 1817
                 constants.ACTIVE)
@@ -1833,7 +1834,7 @@ class TestMl2PortBinding(Ml2PluginV2TestCase,
1833 1834
     def _create_port_and_bound_context(self, port_vif_type, bound_vif_type):
1834 1835
         with self.port() as port:
1835 1836
             plugin = directory.get_plugin()
1836
-            binding = utils.get_port_binding_by_status_and_host(
1837
+            binding = p_utils.get_port_binding_by_status_and_host(
1837 1838
                 plugin._get_port(self.context,
1838 1839
                                  port['port']['id']).port_bindings,
1839 1840
                 constants.ACTIVE)
@@ -1950,7 +1951,7 @@ class TestMl2PortBinding(Ml2PluginV2TestCase,
1950 1951
     def test_update_port_binding_host_id_none(self):
1951 1952
         with self.port() as port:
1952 1953
             plugin = directory.get_plugin()
1953
-            binding = utils.get_port_binding_by_status_and_host(
1954
+            binding = p_utils.get_port_binding_by_status_and_host(
1954 1955
                 plugin._get_port(self.context,
1955 1956
                                  port['port']['id']).port_bindings,
1956 1957
                 constants.ACTIVE)
@@ -1972,7 +1973,7 @@ class TestMl2PortBinding(Ml2PluginV2TestCase,
1972 1973
     def test_update_port_binding_host_id_not_changed(self):
1973 1974
         with self.port() as port:
1974 1975
             plugin = directory.get_plugin()
1975
-            binding = utils.get_port_binding_by_status_and_host(
1976
+            binding = p_utils.get_port_binding_by_status_and_host(
1976 1977
                 plugin._get_port(self.context,
1977 1978
                                  port['port']['id']).port_bindings,
1978 1979
                 constants.ACTIVE)
@@ -2961,7 +2962,7 @@ class TestML2Segments(Ml2PluginV2TestCase):
2961 2962
             # add writer here to make sure that the following operations are
2962 2963
             # performed in the same session
2963 2964
             with db_api.context_manager.writer.using(self.context):
2964
-                binding = utils.get_port_binding_by_status_and_host(
2965
+                binding = p_utils.get_port_binding_by_status_and_host(
2965 2966
                     plugin._get_port(self.context,
2966 2967
                                      port['port']['id']).port_bindings,
2967 2968
                     constants.ACTIVE)

+ 1
- 1
neutron/tests/unit/plugins/ml2/test_port_binding.py View File

@@ -19,12 +19,12 @@ from neutron_lib.api.definitions import portbindings_extended as pbe_ext
19 19
 from neutron_lib import constants as const
20 20
 from neutron_lib import context
21 21
 from neutron_lib.plugins import directory
22
+from neutron_lib.plugins import utils
22 23
 from oslo_config import cfg
23 24
 from oslo_serialization import jsonutils
24 25
 import webob.exc
25 26
 
26 27
 from neutron.common import exceptions
27
-from neutron.common import utils
28 28
 from neutron.conf.plugins.ml2 import config
29 29
 from neutron.conf.plugins.ml2.drivers import driver_type
30 30
 from neutron.plugins.ml2 import driver_context

Loading…
Cancel
Save