Add binding-profile.allocation converter

With the introduction of port-resource-request-groups extension,
format of binding-profile.allocation has changed. This patch
adds a converter that allows to upgrade binding-profile.allocation
to the new format.

Partial-Bug: #1922237
See-Also: https://review.opendev.org/785236
Change-Id: I1b090a0d7a9639aa03f45c6fbd0efea9420ff71f
This commit is contained in:
Przemyslaw Szczerbik 2021-09-27 11:50:53 +02:00
parent 2ebb6d4d5c
commit 65e188eebd
2 changed files with 47 additions and 0 deletions

View File

@ -10,13 +10,17 @@
# License for the specific language governing permissions and limitations
# under the License.
import uuid
import netaddr
from oslo_config import cfg
from oslo_utils import strutils
from neutron_lib._i18n import _
from neutron_lib import constants
from neutron_lib import exceptions as n_exc
from neutron_lib.placement import utils as pl_utils
from neutron_lib.utils import net as net_utils
@ -332,3 +336,22 @@ def convert_to_sanitized_mac_address(mac_address):
return str(netaddr.EUI(mac_address, dialect=netaddr.mac_unix_expanded))
except netaddr.core.AddrFormatError:
return mac_address
def convert_to_sanitized_binding_profile_allocation(allocation, port_id,
min_bw_rules):
"""Return binding-profile.allocation in the new format
:param allocation: binding-profile.allocation attribute containting a
string with RP UUID
:param port_id: ID of the port that is being sanitized
:param min_bw_rules: A list of minimum bandwidth rules associated with the
port.
:return: A dict with allocation in {'<group_uuid>': '<rp_uuid>'} format.
"""
if isinstance(allocation, dict):
return allocation
group_id = str(
pl_utils.resource_request_group_uuid(uuid.UUID(port_id), min_bw_rules))
return {group_id: allocation}

View File

@ -384,3 +384,27 @@ class TestConvertToSanitizedMacAddress(base.BaseTestCase):
self.assertEqual(
'00:11:22:33:44:',
converters.convert_to_sanitized_mac_address('00:11:22:33:44:'))
class TestConvertToSanitizedBindingProfileAllocation(base.BaseTestCase):
RP_UUID = '41d7391e-1f69-11ec-a899-8f9d6d950f8d'
PORT_ID = '64d01804-1f83-11ec-987c-7f6caec3998b'
MIN_BW_RULE_ID = '52441596-1f83-11ec-93c5-9b759591a493'
# uuid -v5 64d01804-1f83-11ec-987c-7f6caec3998b
# 52441596-1f83-11ec-93c5-9b759591a493
GROUP_UUID = '2a1be6ea-15b0-5ac1-9d70-643e2ae306cb'
def test_sanitize_binding_profile_allocation(self):
old_format = self.RP_UUID
new_format = {self.GROUP_UUID: self.RP_UUID}
min_bw_rules = [mock.MagicMock(id=self.MIN_BW_RULE_ID)]
self.assertEqual(
new_format,
converters.convert_to_sanitized_binding_profile_allocation(
old_format, self.PORT_ID, min_bw_rules))
self.assertEqual(
new_format,
converters.convert_to_sanitized_binding_profile_allocation(
new_format, self.PORT_ID, min_bw_rules))