| 1 |
# Copyright 2018 Ericsson |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
|
| 15 |
import uuid |
| 16 |
|
| 17 |
import os_traits |
| 18 |
|
| 19 |
from neutron_lib._i18n import _ |
| 20 |
from neutron_lib import constants as const |
| 21 |
from neutron_lib.placement import constants as place_const |
| 22 |
|
| 23 |
|
| 24 |
def physnet_trait(physnet): |
| 25 |
"""A Placement trait name to represent being connected to a physnet. |
| 26 |
|
| 27 |
:param physnet: The physnet name. |
| 28 |
:returns: The trait name representing the physnet. |
| 29 |
""" |
| 30 |
return os_traits.normalize_name( |
| 31 |
f'{place_const.TRAIT_PREFIX_PHYSNET}{physnet}') |
| 32 |
|
| 33 |
|
| 34 |
def vnic_type_trait(vnic_type): |
| 35 |
"""A Placement trait name to represent support for a vnic_type. |
| 36 |
|
| 37 |
:param physnet: The vnic_type. |
| 38 |
:returns: The trait name representing the vnic_type. |
| 39 |
""" |
| 40 |
return os_traits.normalize_name( |
| 41 |
f'{place_const.TRAIT_PREFIX_VNIC_TYPE}{vnic_type}') |
| 42 |
|
| 43 |
|
| 44 |
def six_uuid5(namespace, name): |
| 45 |
"""A uuid.uuid5 variant that takes utf-8 'name' both in Python 2 and 3. |
| 46 |
|
| 47 |
:param namespace: A UUID object used as a namespace in the generation of a |
| 48 |
v5 UUID. |
| 49 |
:param name: Any string (either bytecode or unicode) used as a name in the |
| 50 |
generation of a v5 UUID. |
| 51 |
:returns: A v5 UUID object. |
| 52 |
""" |
| 53 |
|
| 54 |
# NOTE(bence romsics): |
| 55 |
# uuid.uuid5() behaves seemingly consistently but still incompatibly |
| 56 |
# different in cPython 2 and 3. Both expects the 'name' parameter to have |
| 57 |
# the type of the default string literal in each language version. |
| 58 |
# That is: |
| 59 |
# The cPython 2 variant expects a byte string. |
| 60 |
# The cPython 3 variant expects a unicode string. |
| 61 |
# Which types are called respectively 'str' and 'str' for the sake of |
| 62 |
# confusion. But the sha1() hash inside uuid5() always needs a byte string, |
| 63 |
# so we have to treat the two versions asymmetrically. See also: |
| 64 |
# |
| 65 |
# cPython 2.7: |
| 66 |
# https://github.com/python/cpython/blob |
| 67 |
# /ea9a0994cd0f4bd37799b045c34097eb21662b3d/Lib/uuid.py#L603 |
| 68 |
# cPython 3.6: |
| 69 |
# https://github.com/python/cpython/blob |
| 70 |
# /e9e2fd75ccbc6e9a5221cf3525e39e9d042d843f/Lib/uuid.py#L628 |
| 71 |
return uuid.uuid5(namespace=namespace, name=name) |
| 72 |
|
| 73 |
|
| 74 |
# NOTE(bence romsics): The spec said: "Agent resource providers shall |
| 75 |
# be identified by their already existing Neutron agent UUIDs [...]" |
| 76 |
# |
| 77 |
# https://review.opendev.org/#/c/508149/14/specs/rocky |
| 78 |
# /minimum-bandwidth-allocation-placement-api.rst@465 |
| 79 |
# |
| 80 |
# However we forgot that agent UUIDs are not stable through a few |
| 81 |
# admin operations like after a manual 'openstack network agent |
| 82 |
# delete'. Here we make up a stable UUID instead. |
| 83 |
def agent_resource_provider_uuid(namespace, host): |
| 84 |
"""Generate a stable UUID for an agent. |
| 85 |
|
| 86 |
:param namespace: A UUID object identifying a mechanism driver (including |
| 87 |
its agent). |
| 88 |
:param host: The hostname of the agent. |
| 89 |
:returns: A unique and stable UUID identifying an agent. |
| 90 |
""" |
| 91 |
return six_uuid5(namespace=namespace, name=host) |
| 92 |
|
| 93 |
|
| 94 |
def device_resource_provider_uuid(namespace, host, device, separator=':'): |
| 95 |
"""Generate a stable UUID for a physical network device. |
| 96 |
|
| 97 |
:param namespace: A UUID object identifying a mechanism driver (including |
| 98 |
its agent). |
| 99 |
:param host: The hostname of the agent. |
| 100 |
:param device: A host-unique name of the physical network device. |
| 101 |
:param separator: A string used in assembling a name for uuid5(). Choose |
| 102 |
one that cannot occur either in 'host' or 'device'. |
| 103 |
Optional. |
| 104 |
:returns: A unique and stable UUID identifying a physical network device. |
| 105 |
""" |
| 106 |
name = separator.join([host, device]) |
| 107 |
return six_uuid5(namespace=namespace, name=name) |
| 108 |
|
| 109 |
|
| 110 |
def resource_request_group_uuid(namespace, qos_rules, separator=':'): |
| 111 |
"""Generate a stable UUID for a resource request group. |
| 112 |
|
| 113 |
:param namespace: A UUID object identifying a port. |
| 114 |
:param qos_rules: A list of QoS rules contributing to the group. |
| 115 |
:param separator: A string used in assembling a name for uuid5(). Optional. |
| 116 |
:returns: A unique and stable UUID identifying a resource request group. |
| 117 |
""" |
| 118 |
name = separator.join([rule.id for rule in qos_rules]) |
| 119 |
return six_uuid5(namespace=namespace, name=name) |
| 120 |
|
| 121 |
|
| 122 |
def _parse_rp_rate(rate_str): |
| 123 |
"""Parse the config string value to an non-negative integer. |
| 124 |
|
| 125 |
:param rate_str: A decimal string representation of an non-negative |
| 126 |
integer, allowing the empty string. |
| 127 |
:raises: ValueError on invalid input. |
| 128 |
:returns: The value as an integer or None if not set in config. |
| 129 |
""" |
| 130 |
try: |
| 131 |
rate = None |
| 132 |
if rate_str: |
| 133 |
rate = int(rate_str) |
| 134 |
if rate < 0: |
| 135 |
raise ValueError() |
| 136 |
except ValueError as e: |
| 137 |
raise ValueError(_( |
| 138 |
'Cannot parse string value to an integer. ' |
| 139 |
'Expected: non-negative integer value, got: %s') % |
| 140 |
rate_str) from e |
| 141 |
return rate |
| 142 |
|
| 143 |
|
| 144 |
def _parse_rp_options(options, dict_keys): |
| 145 |
"""Parse the config string tuples and map them to dict of dicts. |
| 146 |
|
| 147 |
:param options: The list of string tuples separated with ':' |
| 148 |
in following format '[<str>]:[<rate0>:...:<rateN>]'. |
| 149 |
First element of the tuple is a string that will be used as |
| 150 |
a key of an outer dictionary. If not specified, defaults to |
| 151 |
an empty string. A rate is an optional, non-negative int. |
| 152 |
If rate values are provided they must match the length of |
| 153 |
dict_keys. If a rate value is not specified, it defaults to |
| 154 |
None. |
| 155 |
:param dict_keys: A tuple of strings containing names of inner dictionary |
| 156 |
keys that are going to be mapped to rate values from |
| 157 |
options tuple. |
| 158 |
:raises: ValueError on invalid input. |
| 159 |
:returns: The fully parsed config as a dict of dicts. |
| 160 |
""" |
| 161 |
rv = {} |
| 162 |
for option in options: |
| 163 |
if ':' not in option: |
| 164 |
option += ':' * len(dict_keys) |
| 165 |
try: |
| 166 |
values = option.split(':') |
| 167 |
tuple_len = len(dict_keys) + 1 |
| 168 |
if len(values) != tuple_len: |
| 169 |
raise ValueError() |
| 170 |
name = values.pop(0) |
| 171 |
except ValueError as e: |
| 172 |
raise ValueError( |
| 173 |
_('Expected a tuple with %d values, got: %s') |
| 174 |
% (tuple_len, option)) from e |
| 175 |
if name in rv: |
| 176 |
raise ValueError(_( |
| 177 |
'Same resource name listed multiple times: "%s"') % name) |
| 178 |
rv[name] = dict(zip(dict_keys, |
| 179 |
[_parse_rp_rate(v) for v in values])) |
| 180 |
return rv |
| 181 |
|
| 182 |
|
| 183 |
def parse_rp_bandwidths(bandwidths): |
| 184 |
"""Parse and validate config option: resource_provider_bandwidths. |
| 185 |
|
| 186 |
Input in the config:: |
| 187 |
|
| 188 |
resource_provider_bandwidths = eth0:10000:10000,eth1::10000,eth2::,eth3 |
| 189 |
|
| 190 |
Input here:: |
| 191 |
|
| 192 |
['eth0:10000:10000', 'eth1::10000', 'eth2::', 'eth3'] |
| 193 |
|
| 194 |
Output:: |
| 195 |
|
| 196 |
{ |
| 197 |
'eth0': {'egress': 10000, 'ingress': 10000}, |
| 198 |
'eth1': {'egress': None, 'ingress': 10000}, |
| 199 |
'eth2': {'egress': None, 'ingress': None}, |
| 200 |
'eth3': {'egress': None, 'ingress': None}, |
| 201 |
} |
| 202 |
|
| 203 |
:param bandwidths: The list of 'interface:egress:ingress' bandwidth |
| 204 |
config options as pre-parsed by oslo_config. |
| 205 |
:raises: ValueError on invalid input. |
| 206 |
:returns: The fully parsed bandwidth config as a dict of dicts. |
| 207 |
""" |
| 208 |
try: |
| 209 |
return _parse_rp_options( |
| 210 |
bandwidths, |
| 211 |
(const.EGRESS_DIRECTION, const.INGRESS_DIRECTION)) |
| 212 |
except ValueError as e: |
| 213 |
raise ValueError(_( |
| 214 |
"Cannot parse resource_provider_bandwidths. %s") % e) from e |
| 215 |
|
| 216 |
|
| 217 |
def _rp_pp_set_default_hypervisor(cfg, host): |
| 218 |
# If the user omitted hypervisor name we need to replace '' key with the |
| 219 |
# value of a host parameter. Before we do that, ensure that we won't |
| 220 |
# override a key which already exists in the dict. |
| 221 |
if cfg.get('') and cfg.get(host): |
| 222 |
raise ValueError(_( |
| 223 |
'Found configuration for "%s" hypervisor and one without ' |
| 224 |
'hypervisor name specified that would override it.') % host) |
| 225 |
if cfg.get(''): |
| 226 |
cfg[host] = cfg.pop('') |
| 227 |
|
| 228 |
|
| 229 |
def parse_rp_pp_with_direction(pkt_rates, host): |
| 230 |
"""Parse and validate: resource_provider_packet_processing_with_direction. |
| 231 |
|
| 232 |
Input in the config:: |
| 233 |
|
| 234 |
resource_provider_packet_processing_with_direction = |
| 235 |
host0:10000:10000,host1::10000,host2::,host3,:0:0 |
| 236 |
|
| 237 |
Input here:: |
| 238 |
|
| 239 |
['host0:10000:10000', 'host1::10000', 'host2::', 'host3', ':0:0'] |
| 240 |
|
| 241 |
Output:: |
| 242 |
|
| 243 |
{ |
| 244 |
'host0': {'egress': 10000, 'ingress': 10000}, |
| 245 |
'host1': {'egress': None, 'ingress': 10000}, |
| 246 |
'host2': {'egress': None, 'ingress': None}, |
| 247 |
'host3': {'egress': None, 'ingress': None}, |
| 248 |
'<host>': {'egress': 0, 'ingress': 0}, |
| 249 |
} |
| 250 |
|
| 251 |
:param pkt_rates: The list of 'hypervisor:egress:ingress' pkt rate |
| 252 |
config options as pre-parsed by oslo_config. |
| 253 |
:param host: Hostname that will be used as a default key value if the user |
| 254 |
did not provide hypervisor name. |
| 255 |
:raises: ValueError on invalid input. |
| 256 |
:returns: The fully parsed pkt rate config as a dict of dicts. |
| 257 |
""" |
| 258 |
|
| 259 |
try: |
| 260 |
cfg = _parse_rp_options( |
| 261 |
pkt_rates, |
| 262 |
(const.EGRESS_DIRECTION, const.INGRESS_DIRECTION)) |
| 263 |
_rp_pp_set_default_hypervisor(cfg, host) |
| 264 |
except ValueError as e: |
| 265 |
raise ValueError(_( |
| 266 |
"Cannot parse " |
| 267 |
"resource_provider_packet_processing_with_direction. %s") |
| 268 |
% e) from e |
| 269 |
|
| 270 |
return cfg |
| 271 |
|
| 272 |
|
| 273 |
def parse_rp_pp_without_direction(pkt_rates, host): |
| 274 |
"""Parse: resource_provider_packet_processing_without_direction. |
| 275 |
|
| 276 |
Input in the config:: |
| 277 |
|
| 278 |
resource_provider_packet_processing_without_direction = |
| 279 |
host0:10000,host1:,host2,:0 |
| 280 |
|
| 281 |
Input here:: |
| 282 |
|
| 283 |
['host0:10000', 'host1:', 'host2', ':0'] |
| 284 |
|
| 285 |
Output:: |
| 286 |
|
| 287 |
{ |
| 288 |
'host0': {'any': 10000}, |
| 289 |
'host1': {'any': None}, |
| 290 |
'host2': {'any': None}, |
| 291 |
'<DEFAULT.host>': {'any': 0}, |
| 292 |
} |
| 293 |
|
| 294 |
:param pkt_rates: The list of 'hypervisor:pkt_rate' config options |
| 295 |
as pre-parsed by oslo_config. |
| 296 |
:param host: Hostname that will be used as a default key value if the user |
| 297 |
did not provide hypervisor name. |
| 298 |
:raises: ValueError on invalid input. |
| 299 |
:returns: The fully parsed pkt rate config as a dict of dicts. |
| 300 |
""" |
| 301 |
|
| 302 |
try: |
| 303 |
cfg = _parse_rp_options(pkt_rates, (const.ANY_DIRECTION,)) |
| 304 |
_rp_pp_set_default_hypervisor(cfg, host) |
| 305 |
except ValueError as e: |
| 306 |
raise ValueError(_( |
| 307 |
"Cannot parse " |
| 308 |
"resource_provider_packet_processing_without_direction. %s") |
| 309 |
% e) from e |
| 310 |
|
| 311 |
return cfg |
| 312 |
|
| 313 |
|
| 314 |
def parse_rp_inventory_defaults(inventory_defaults): |
| 315 |
"""Parse and validate config option: parse_rp_inventory_defaults. |
| 316 |
|
| 317 |
Cast the dict values to the proper numerical types. |
| 318 |
|
| 319 |
Input in the config:: |
| 320 |
|
| 321 |
resource_provider_inventory_defaults = allocation_ratio:1.0,min_unit:1 |
| 322 |
|
| 323 |
Input here:: |
| 324 |
|
| 325 |
{ |
| 326 |
'allocation_ratio': '1.0', |
| 327 |
'min_unit': '1', |
| 328 |
} |
| 329 |
|
| 330 |
Output here:: |
| 331 |
|
| 332 |
{ |
| 333 |
'allocation_ratio': 1.0, |
| 334 |
'min_unit': 1, |
| 335 |
} |
| 336 |
|
| 337 |
:param inventory_defaults: The dict of inventory parameters and values (as |
| 338 |
strings) as pre-parsed by oslo_config. |
| 339 |
:raises: ValueError on invalid input. |
| 340 |
:returns: The fully parsed inventory parameters and values (as numerical |
| 341 |
values) as a dict. |
| 342 |
""" |
| 343 |
|
| 344 |
unexpected_options = (set(inventory_defaults.keys()) - |
| 345 |
place_const.INVENTORY_OPTIONS) |
| 346 |
if unexpected_options: |
| 347 |
raise ValueError(_( |
| 348 |
'Cannot parse inventory_defaults. Unexpected options: %s') % |
| 349 |
','.join(unexpected_options)) |
| 350 |
|
| 351 |
# allocation_ratio is a float |
| 352 |
try: |
| 353 |
if 'allocation_ratio' in inventory_defaults: |
| 354 |
inventory_defaults['allocation_ratio'] = float( |
| 355 |
inventory_defaults['allocation_ratio']) |
| 356 |
if inventory_defaults['allocation_ratio'] < 0: |
| 357 |
raise ValueError() |
| 358 |
except ValueError as e: |
| 359 |
raise ValueError(_( |
| 360 |
'Cannot parse inventory_defaults.allocation_ratio. ' |
| 361 |
'Expected: non-negative float, got: %s') % |
| 362 |
inventory_defaults['allocation_ratio']) from e |
| 363 |
|
| 364 |
# the others are ints |
| 365 |
for key in ('min_unit', 'max_unit', 'reserved', 'step_size'): |
| 366 |
try: |
| 367 |
if key in inventory_defaults: |
| 368 |
inventory_defaults[key] = int(inventory_defaults[key]) |
| 369 |
if inventory_defaults[key] < 0: |
| 370 |
raise ValueError() |
| 371 |
except ValueError as e: |
| 372 |
raise ValueError(_( |
| 373 |
'Cannot parse inventory_defaults.%(key)s. ' |
| 374 |
'Expected: non-negative int, got: %(got)s') % { |
| 375 |
'key': key, |
| 376 |
'got': inventory_defaults[key], |
| 377 |
}) from e |
| 378 |
|
| 379 |
return inventory_defaults |