| 1 |
# Copyright 2016 OpenStack Foundation |
| 2 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 3 |
# not use this file except in compliance with the License. You may obtain |
| 4 |
# a copy of the License at |
| 5 |
# |
| 6 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
# |
| 8 |
# Unless required by applicable law or agreed to in writing, software |
| 9 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 10 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 11 |
# License for the specific language governing permissions and limitations |
| 12 |
# under the License. |
| 13 |
|
| 14 |
import itertools |
| 15 |
import uuid |
| 16 |
|
| 17 |
import netaddr |
| 18 |
from oslo_serialization import jsonutils |
| 19 |
from oslo_versionedobjects import fields as obj_fields |
| 20 |
|
| 21 |
from neutron_lib._i18n import _ |
| 22 |
from neutron_lib import constants as lib_constants |
| 23 |
from neutron_lib.db import constants as lib_db_const |
| 24 |
from neutron_lib.objects import exceptions as o_exc |
| 25 |
from neutron_lib.utils import net as net_utils |
| 26 |
|
| 27 |
|
| 28 |
class HARouterEnumField(obj_fields.AutoTypedField): |
| 29 |
AUTO_TYPE = obj_fields.Enum(valid_values=lib_constants.VALID_HA_STATES) |
| 30 |
|
| 31 |
|
| 32 |
class IPV6ModeEnumField(obj_fields.AutoTypedField): |
| 33 |
AUTO_TYPE = obj_fields.Enum(valid_values=lib_constants.IPV6_MODES) |
| 34 |
|
| 35 |
|
| 36 |
class RangeConstrainedInteger(obj_fields.Integer): |
| 37 |
def __init__(self, start, end, **kwargs): |
| 38 |
try: |
| 39 |
self._start = int(start) |
| 40 |
self._end = int(end) |
| 41 |
except (TypeError, ValueError) as e: |
| 42 |
raise o_exc.NeutronRangeConstrainedIntegerInvalidLimit( |
| 43 |
start=start, end=end) from e |
| 44 |
super().__init__(**kwargs) |
| 45 |
|
| 46 |
def coerce(self, obj, attr, value): |
| 47 |
if not isinstance(value, int): |
| 48 |
msg = _("Field value %s is not an integer") % value |
| 49 |
raise ValueError(msg) |
| 50 |
if not self._start <= value <= self._end: |
| 51 |
msg = _("Field value %s is invalid") % value |
| 52 |
raise ValueError(msg) |
| 53 |
return super().coerce(obj, attr, value) |
| 54 |
|
| 55 |
|
| 56 |
class IPNetworkPrefixLen(RangeConstrainedInteger): |
| 57 |
"""IP network (CIDR) prefix length custom Enum""" |
| 58 |
|
| 59 |
def __init__(self, **kwargs): |
| 60 |
super().__init__(start=0, end=lib_constants.IPv6_BITS, **kwargs) |
| 61 |
|
| 62 |
|
| 63 |
class IPNetworkPrefixLenField(obj_fields.AutoTypedField): |
| 64 |
AUTO_TYPE = IPNetworkPrefixLen() |
| 65 |
|
| 66 |
|
| 67 |
class PortRanges(obj_fields.FieldType): |
| 68 |
@staticmethod |
| 69 |
def _is_port_acceptable(port): |
| 70 |
start = lib_constants.PORT_RANGE_MIN |
| 71 |
end = lib_constants.PORT_RANGE_MAX |
| 72 |
return start <= port <= end |
| 73 |
|
| 74 |
def get_schema(self): |
| 75 |
return {'type': ['string', 'integer']} |
| 76 |
|
| 77 |
def _validate_port(self, attr, value): |
| 78 |
if self._is_port_acceptable(value): |
| 79 |
return |
| 80 |
raise ValueError( |
| 81 |
_('The port %(value)s does not respect the ' |
| 82 |
'range (%(min)s, %(max)s) in field %(attr)s') |
| 83 |
% {'attr': attr, |
| 84 |
'value': value, |
| 85 |
'min': lib_constants.PORT_RANGE_MIN, |
| 86 |
'max': lib_constants.PORT_RANGE_MAX}) |
| 87 |
|
| 88 |
def coerce(self, obj, attr, value): |
| 89 |
if isinstance(value, int): |
| 90 |
self._validate_port(attr, value) |
| 91 |
return value |
| 92 |
if isinstance(value, str): |
| 93 |
if value.isnumeric(): |
| 94 |
self._validate_port(attr, int(value)) |
| 95 |
return value |
| 96 |
values = value.split(':') |
| 97 |
if len(values) == 2: |
| 98 |
start, end = list(map(int, values)) |
| 99 |
if start > end: |
| 100 |
raise ValueError( |
| 101 |
_('The first port %(start)s must be less or equals ' |
| 102 |
'than the second port %(end)s of the port range ' |
| 103 |
'configuration %(value)s' |
| 104 |
'in field %(attr)s.') % { |
| 105 |
'attr': attr, |
| 106 |
'value': value, |
| 107 |
'start': start, |
| 108 |
'end': end}) |
| 109 |
self._validate_port(attr, start) |
| 110 |
self._validate_port(attr, end) |
| 111 |
return value |
| 112 |
raise ValueError( |
| 113 |
_('The field %(attr)s must be in the format PORT_RANGE or' |
| 114 |
'PORT_RANGE:PORT_RANGE (two numeric values separated ' |
| 115 |
'by a colon), and PORT_RANGE must be a numeric ' |
| 116 |
'value and respect the range ' |
| 117 |
'(%(min)s, %(max)s).') % { |
| 118 |
'attr': attr, |
| 119 |
'min': lib_constants.PORT_RANGE_MIN, |
| 120 |
'max': lib_constants.PORT_RANGE_MAX}) |
| 121 |
raise ValueError( |
| 122 |
_('An string/int PORT_RANGE or a string with ' |
| 123 |
'PORT_RANGE:PORT_RANGE format is ' |
| 124 |
'expected in field %(attr)s, not a %(type)s') % { |
| 125 |
'attr': attr, 'type': value}) |
| 126 |
|
| 127 |
|
| 128 |
class PortRangesField(obj_fields.AutoTypedField): |
| 129 |
AUTO_TYPE = PortRanges() |
| 130 |
|
| 131 |
|
| 132 |
class PortRange(RangeConstrainedInteger): |
| 133 |
def __init__(self, start=lib_constants.PORT_RANGE_MIN, **kwargs): |
| 134 |
super().__init__(start=start, |
| 135 |
end=lib_constants.PORT_RANGE_MAX, |
| 136 |
**kwargs) |
| 137 |
|
| 138 |
|
| 139 |
class PortRangeField(obj_fields.AutoTypedField): |
| 140 |
AUTO_TYPE = PortRange() |
| 141 |
|
| 142 |
|
| 143 |
class PortRangeWith0Field(obj_fields.AutoTypedField): |
| 144 |
AUTO_TYPE = PortRange(start=0) |
| 145 |
|
| 146 |
|
| 147 |
class VlanIdRange(RangeConstrainedInteger): |
| 148 |
def __init__(self, **kwargs): |
| 149 |
super().__init__(start=lib_constants.MIN_VLAN_TAG, |
| 150 |
end=lib_constants.MAX_VLAN_TAG, |
| 151 |
**kwargs) |
| 152 |
|
| 153 |
|
| 154 |
class VlanIdRangeField(obj_fields.AutoTypedField): |
| 155 |
AUTO_TYPE = VlanIdRange() |
| 156 |
|
| 157 |
|
| 158 |
class ListOfIPNetworksField(obj_fields.AutoTypedField): |
| 159 |
AUTO_TYPE = obj_fields.List(obj_fields.IPNetwork()) |
| 160 |
|
| 161 |
|
| 162 |
class SetOfUUIDsField(obj_fields.AutoTypedField): |
| 163 |
AUTO_TYPE = obj_fields.Set(obj_fields.UUID()) |
| 164 |
|
| 165 |
|
| 166 |
class DomainName(obj_fields.String): |
| 167 |
def coerce(self, obj, attr, value): |
| 168 |
if not isinstance(value, str): |
| 169 |
msg = _("Field value %s is not a string") % value |
| 170 |
raise ValueError(msg) |
| 171 |
if len(value) > lib_db_const.FQDN_FIELD_SIZE: |
| 172 |
msg = _("Domain name %s is too long") % value |
| 173 |
raise ValueError(msg) |
| 174 |
return super().coerce(obj, attr, value) |
| 175 |
|
| 176 |
|
| 177 |
class DomainNameField(obj_fields.AutoTypedField): |
| 178 |
AUTO_TYPE = DomainName() |
| 179 |
|
| 180 |
|
| 181 |
class IntegerEnum(obj_fields.Integer): |
| 182 |
def __init__(self, valid_values=None, **kwargs): |
| 183 |
if not valid_values: |
| 184 |
msg = _("No possible values specified") |
| 185 |
raise ValueError(msg) |
| 186 |
for value in valid_values: |
| 187 |
if not isinstance(value, int): |
| 188 |
msg = _("Possible value %s is not an integer") % value |
| 189 |
raise ValueError(msg) |
| 190 |
self._valid_values = valid_values |
| 191 |
super().__init__(**kwargs) |
| 192 |
|
| 193 |
def coerce(self, obj, attr, value): |
| 194 |
if not isinstance(value, int): |
| 195 |
msg = _("Field value %s is not an integer") % value |
| 196 |
raise ValueError(msg) |
| 197 |
if value not in self._valid_values: |
| 198 |
msg = ( |
| 199 |
_("Field value %(value)s is not in the list " |
| 200 |
"of valid values: %(values)s") % |
| 201 |
{'value': value, 'values': self._valid_values} |
| 202 |
) |
| 203 |
raise ValueError(msg) |
| 204 |
return super().coerce(obj, attr, value) |
| 205 |
|
| 206 |
|
| 207 |
class IPVersionEnum(IntegerEnum): |
| 208 |
"""IP version integer Enum""" |
| 209 |
|
| 210 |
def __init__(self, **kwargs): |
| 211 |
super().__init__( |
| 212 |
valid_values=lib_constants.IP_ALLOWED_VERSIONS, **kwargs) |
| 213 |
|
| 214 |
|
| 215 |
class IPVersionEnumField(obj_fields.AutoTypedField): |
| 216 |
AUTO_TYPE = IPVersionEnum() |
| 217 |
|
| 218 |
|
| 219 |
class DscpMark(IntegerEnum): |
| 220 |
def __init__(self, valid_values=None, **kwargs): |
| 221 |
super().__init__(valid_values=lib_constants.VALID_DSCP_MARKS) |
| 222 |
|
| 223 |
|
| 224 |
class DscpMarkField(obj_fields.AutoTypedField): |
| 225 |
AUTO_TYPE = DscpMark() |
| 226 |
|
| 227 |
|
| 228 |
class FlowDirectionEnumField(obj_fields.AutoTypedField): |
| 229 |
AUTO_TYPE = obj_fields.Enum(valid_values=lib_constants.VALID_DIRECTIONS) |
| 230 |
|
| 231 |
|
| 232 |
class FlowDirectionAndAnyEnumField(obj_fields.AutoTypedField): |
| 233 |
AUTO_TYPE = obj_fields.Enum( |
| 234 |
valid_values=lib_constants.VALID_DIRECTIONS_AND_ANY) |
| 235 |
|
| 236 |
|
| 237 |
class IpamAllocationStatusEnumField(obj_fields.AutoTypedField): |
| 238 |
AUTO_TYPE = obj_fields.Enum( |
| 239 |
valid_values=lib_constants.VALID_IPAM_ALLOCATION_STATUSES) |
| 240 |
|
| 241 |
|
| 242 |
class EtherTypeEnumField(obj_fields.AutoTypedField): |
| 243 |
AUTO_TYPE = obj_fields.Enum(valid_values=lib_constants.VALID_ETHERTYPES) |
| 244 |
|
| 245 |
|
| 246 |
class IpProtocolEnum(obj_fields.Enum): |
| 247 |
"""IP protocol number Enum""" |
| 248 |
|
| 249 |
def __init__(self, **kwargs): |
| 250 |
super().__init__( |
| 251 |
valid_values=list( |
| 252 |
itertools.chain( |
| 253 |
lib_constants.IP_PROTOCOL_MAP.keys(), |
| 254 |
[str(v) for v in range(256)] |
| 255 |
) |
| 256 |
), |
| 257 |
**kwargs) |
| 258 |
|
| 259 |
|
| 260 |
class PortBindingStatusEnumField(obj_fields.AutoTypedField): |
| 261 |
AUTO_TYPE = obj_fields.Enum( |
| 262 |
valid_values=lib_constants.PORT_BINDING_STATUSES) |
| 263 |
|
| 264 |
|
| 265 |
class IpProtocolEnumField(obj_fields.AutoTypedField): |
| 266 |
AUTO_TYPE = IpProtocolEnum() |
| 267 |
|
| 268 |
|
| 269 |
class MACAddress(obj_fields.FieldType): |
| 270 |
"""MACAddress custom field. |
| 271 |
|
| 272 |
This custom field is different from the one provided by |
| 273 |
oslo.versionedobjects library: it uses netaddr.EUI type instead of strings. |
| 274 |
""" |
| 275 |
|
| 276 |
def coerce(self, obj, attr, value): |
| 277 |
if not isinstance(value, netaddr.EUI): |
| 278 |
msg = _("Field value %s is not a netaddr.EUI") % value |
| 279 |
raise ValueError(msg) |
| 280 |
return super().coerce(obj, attr, value) |
| 281 |
|
| 282 |
@staticmethod |
| 283 |
def to_primitive(obj, attr, value): |
| 284 |
return str(value) |
| 285 |
|
| 286 |
@staticmethod |
| 287 |
def from_primitive(obj, attr, value): |
| 288 |
try: |
| 289 |
return net_utils.AuthenticEUI(value) |
| 290 |
except Exception as e: |
| 291 |
msg = _("Field value %s is not a netaddr.EUI") % value |
| 292 |
raise ValueError(msg) from e |
| 293 |
|
| 294 |
|
| 295 |
class MACAddressField(obj_fields.AutoTypedField): |
| 296 |
AUTO_TYPE = MACAddress() |
| 297 |
|
| 298 |
|
| 299 |
class DictOfMiscValues(obj_fields.FieldType): |
| 300 |
"""DictOfMiscValues custom field |
| 301 |
|
| 302 |
This custom field is handling dictionary with miscellaneous value types, |
| 303 |
including integer, float, boolean and list and nested dictionaries. |
| 304 |
""" |
| 305 |
@staticmethod |
| 306 |
def coerce(obj, attr, value): |
| 307 |
if isinstance(value, dict): |
| 308 |
return value |
| 309 |
if isinstance(value, str): |
| 310 |
try: |
| 311 |
return jsonutils.loads(value) |
| 312 |
except Exception as e: |
| 313 |
msg = _("Field value %s is not stringified JSON") % value |
| 314 |
raise ValueError(msg) from e |
| 315 |
msg = (_("Field value %s is not type of dict or stringified JSON") |
| 316 |
% value) |
| 317 |
raise ValueError(msg) |
| 318 |
|
| 319 |
@staticmethod |
| 320 |
def from_primitive(obj, attr, value): |
| 321 |
return DictOfMiscValues.coerce(obj, attr, value) |
| 322 |
|
| 323 |
@staticmethod |
| 324 |
def to_primitive(obj, attr, value): |
| 325 |
return jsonutils.dumps(value) |
| 326 |
|
| 327 |
@staticmethod |
| 328 |
def stringify(value): |
| 329 |
return jsonutils.dumps(value) |
| 330 |
|
| 331 |
|
| 332 |
class DictOfMiscValuesField(obj_fields.AutoTypedField): |
| 333 |
AUTO_TYPE = DictOfMiscValues |
| 334 |
|
| 335 |
|
| 336 |
class ListOfDictOfMiscValuesField(obj_fields.AutoTypedField): |
| 337 |
AUTO_TYPE = obj_fields.List(DictOfMiscValuesField()) |
| 338 |
|
| 339 |
|
| 340 |
class IPNetwork(obj_fields.FieldType): |
| 341 |
"""IPNetwork custom field. |
| 342 |
|
| 343 |
This custom field is different from the one provided by |
| 344 |
oslo.versionedobjects library: it does not reset string representation for |
| 345 |
the field. |
| 346 |
""" |
| 347 |
|
| 348 |
def coerce(self, obj, attr, value): |
| 349 |
if not isinstance(value, netaddr.IPNetwork): |
| 350 |
msg = _("Field value %s is not a netaddr.IPNetwork") % value |
| 351 |
raise ValueError(msg) |
| 352 |
return super().coerce(obj, attr, value) |
| 353 |
|
| 354 |
@staticmethod |
| 355 |
def to_primitive(obj, attr, value): |
| 356 |
return str(value) |
| 357 |
|
| 358 |
@staticmethod |
| 359 |
def from_primitive(obj, attr, value): |
| 360 |
try: |
| 361 |
return net_utils.AuthenticIPNetwork(value) |
| 362 |
except Exception as e: |
| 363 |
msg = _("Field value %s is not a netaddr.IPNetwork") % value |
| 364 |
raise ValueError(msg) from e |
| 365 |
|
| 366 |
|
| 367 |
class IPNetworkField(obj_fields.AutoTypedField): |
| 368 |
AUTO_TYPE = IPNetwork() |
| 369 |
|
| 370 |
|
| 371 |
class UUID(obj_fields.UUID): |
| 372 |
def coerce(self, obj, attr, value): |
| 373 |
uuid.UUID(str(value)) |
| 374 |
return str(value) |
| 375 |
|
| 376 |
|
| 377 |
class UUIDField(obj_fields.AutoTypedField): |
| 378 |
AUTO_TYPE = UUID() |
| 379 |
|
| 380 |
|
| 381 |
class FloatingIPStatusEnumField(obj_fields.AutoTypedField): |
| 382 |
AUTO_TYPE = obj_fields.Enum( |
| 383 |
valid_values=lib_constants.VALID_FLOATINGIP_STATUS) |
| 384 |
|
| 385 |
|
| 386 |
class RouterStatusEnumField(obj_fields.AutoTypedField): |
| 387 |
AUTO_TYPE = obj_fields.Enum( |
| 388 |
valid_values=lib_constants.VALID_ROUTER_STATUS) |
| 389 |
|
| 390 |
|
| 391 |
class NetworkSegmentRangeNetworkTypeEnumField(obj_fields.AutoTypedField): |
| 392 |
AUTO_TYPE = obj_fields.Enum( |
| 393 |
valid_values=lib_constants.NETWORK_SEGMENT_RANGE_TYPES) |
| 394 |
|
| 395 |
|
| 396 |
class NumaAffinityPoliciesEnumField(obj_fields.AutoTypedField): |
| 397 |
AUTO_TYPE = obj_fields.Enum(valid_values=lib_constants.PORT_NUMA_POLICIES) |
| 398 |
|
| 399 |
|
| 400 |
class PortHardwareOffloadTypeEnumField(obj_fields.AutoTypedField): |
| 401 |
AUTO_TYPE = obj_fields.Enum(valid_values=lib_constants.VALID_HWOL_TYPES) |