| 1 |
# Copyright 2013 VMware, Inc. All rights reserved. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 |
# not use this file except in compliance with the License. You may obtain |
| 5 |
# 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, WITHOUT |
| 11 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 |
# License for the specific language governing permissions and limitations |
| 13 |
# under the License. |
| 14 |
|
| 15 |
from oslo_config import cfg |
| 16 |
from webob import exc |
| 17 |
|
| 18 |
from neutron_lib._i18n import _ |
| 19 |
from neutron_lib.api import validators |
| 20 |
from neutron_lib.exceptions import allowedaddresspairs as exceptions |
| 21 |
|
| 22 |
|
| 23 |
def _validate_allowed_address_pairs(address_pairs, valid_values=None): |
| 24 |
"""Validates a list of allowed address pair dicts. |
| 25 |
|
| 26 |
Validation herein requires the caller to have registered the |
| 27 |
max_allowed_address_pair oslo config option in the global CONF prior |
| 28 |
to having this validator used. |
| 29 |
|
| 30 |
:param address_pairs: A list of address pair dicts to validate. |
| 31 |
:param valid_values: Not used. |
| 32 |
:returns: None |
| 33 |
:raises: AllowedAddressPairExhausted if the address pairs requested |
| 34 |
exceeds cfg.CONF.max_allowed_address_pair. AllowedAddressPairsMissingIP |
| 35 |
if any address pair dicts are missing and IP address. |
| 36 |
DuplicateAddressPairInRequest if duplicated IPs are in the list of |
| 37 |
address pair dicts. Otherwise a HTTPBadRequest is raised if any of |
| 38 |
the address pairs are invalid. |
| 39 |
""" |
| 40 |
unique_check = {} |
| 41 |
if not isinstance(address_pairs, list): |
| 42 |
raise exc.HTTPBadRequest( |
| 43 |
_("Allowed address pairs must be a list.")) |
| 44 |
if len(address_pairs) > cfg.CONF.max_allowed_address_pair: |
| 45 |
raise exceptions.AllowedAddressPairExhausted( |
| 46 |
quota=cfg.CONF.max_allowed_address_pair) |
| 47 |
|
| 48 |
for address_pair in address_pairs: |
| 49 |
msg = validators.validate_dict(address_pair) |
| 50 |
if msg: |
| 51 |
return msg |
| 52 |
# mac_address is optional, if not set we use the mac on the port |
| 53 |
if 'mac_address' in address_pair: |
| 54 |
msg = validators.validate_mac_address(address_pair['mac_address']) |
| 55 |
if msg: |
| 56 |
raise exc.HTTPBadRequest(msg) |
| 57 |
if 'ip_address' not in address_pair: |
| 58 |
raise exceptions.AllowedAddressPairsMissingIP() |
| 59 |
|
| 60 |
mac = address_pair.get('mac_address') |
| 61 |
ip_address = address_pair['ip_address'] |
| 62 |
if (mac, ip_address) not in unique_check: |
| 63 |
unique_check[(mac, ip_address)] = None |
| 64 |
else: |
| 65 |
raise exceptions.DuplicateAddressPairInRequest( |
| 66 |
mac_address=mac, ip_address=ip_address) |
| 67 |
|
| 68 |
invalid_attrs = set(address_pair.keys()) - {'mac_address', |
| 69 |
'ip_address'} |
| 70 |
if invalid_attrs: |
| 71 |
msg = (_("Unrecognized attribute(s) '%s'") % |
| 72 |
', '.join(set(address_pair.keys()) - |
| 73 |
{'mac_address', 'ip_address'})) |
| 74 |
raise exc.HTTPBadRequest(msg) |
| 75 |
|
| 76 |
if '/' in ip_address: |
| 77 |
msg = validators.validate_subnet(ip_address) |
| 78 |
else: |
| 79 |
msg = validators.validate_ip_address(ip_address) |
| 80 |
if msg: |
| 81 |
raise exc.HTTPBadRequest(msg) |