Skip to content

Commit

Permalink
Use converters and validators from neutron-lib
Browse files Browse the repository at this point in the history
Related-Blueprint: neutron-lib

Change-Id: I6b9079e9e703c6fd75adbed3846e7257685433e8
  • Loading branch information
HenryGessau committed Apr 24, 2016
1 parent 4148a34 commit 78fff41
Show file tree
Hide file tree
Showing 52 changed files with 307 additions and 1,691 deletions.
716 changes: 94 additions & 622 deletions neutron/api/v2/attributes.py

Large diffs are not rendered by default.

28 changes: 19 additions & 9 deletions neutron/common/_deprecate.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,33 @@


class _DeprecateSubset(object):
additional = {}

def __init__(self, my_globals, other_mod):
self.other_mod = other_mod
self.my_globals = copy.copy(my_globals)

@classmethod
def and_also(cls, name, other_mod):
cls.additional[name] = other_mod

def __getattr__(self, name):
a = self.my_globals.get(name)
if (not name.startswith("__") and not inspect.ismodule(a) and
name in vars(self.other_mod)):
if not name.startswith("__") and not inspect.ismodule(a):
other_mod = self.additional.get(name) or self.other_mod
if name in vars(other_mod):

# These should be enabled after most have been cleaned up
# in neutron proper, which may not happen during the busy M-3.

debtcollector.deprecate(
name,
message='moved to neutron_lib',
version='mitaka',
removal_version='newton',
stacklevel=4)
debtcollector.deprecate(
name,
message='moved to %s' % other_mod.__name__,
version='mitaka',
removal_version='newton',
stacklevel=4)

return vars(self.other_mod)[name]
return vars(other_mod)[name]

try:
return self.my_globals[name]
Expand Down
5 changes: 2 additions & 3 deletions neutron/common/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import sys

from keystoneauth1 import loading as ks_loading
from neutron_lib.api import validators
from oslo_config import cfg
from oslo_db import options as db_options
from oslo_log import log as logging
Expand All @@ -28,7 +29,6 @@
from oslo_service import wsgi

from neutron._i18n import _, _LI
from neutron.api.v2 import attributes
from neutron.common import constants
from neutron.common import utils
from neutron import policy
Expand Down Expand Up @@ -241,8 +241,7 @@ def init(args, **kwargs):
n_rpc.init(cfg.CONF)

# Validate that the base_mac is of the correct format
msg = attributes._validate_regex(cfg.CONF.base_mac,
attributes.MAC_PATTERN)
msg = validators.validate_regex(cfg.CONF.base_mac, validators.MAC_PATTERN)
if msg:
msg = _("Base MAC: %s") % msg
raise Exception(msg)
Expand Down
3 changes: 2 additions & 1 deletion neutron/db/agents_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import datetime

from eventlet import greenthread
from neutron_lib.api import converters
from neutron_lib import constants
from oslo_config import cfg
from oslo_db import exception as db_exc
Expand Down Expand Up @@ -282,7 +283,7 @@ def get_agents(self, context, filters=None, fields=None):
filters=filters, fields=fields)
alive = filters and filters.get('alive', None)
if alive:
alive = attributes.convert_to_boolean(alive[0])
alive = converters.convert_to_boolean(alive[0])
agents = [agent for agent in agents if agent['alive'] == alive]
return agents

Expand Down
8 changes: 4 additions & 4 deletions neutron/db/allowedaddresspairs_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
# under the License.
#

import sqlalchemy as sa

from neutron_lib.api import validators
from oslo_db import exception as db_exc
import sqlalchemy as sa
from sqlalchemy import orm

from neutron.api.v2 import attributes as attr
Expand Down Expand Up @@ -43,7 +43,7 @@ class AllowedAddressPairsMixin(object):

def _process_create_allowed_address_pairs(self, context, port,
allowed_address_pairs):
if not attr.is_attr_set(allowed_address_pairs):
if not validators.is_attr_set(allowed_address_pairs):
return []
try:
with context.session.begin(subtransactions=True):
Expand Down Expand Up @@ -95,7 +95,7 @@ def _make_allowed_address_pairs_dict(self, allowed_address_pairs,
return self._fields(res, fields)

def _has_address_pairs(self, port):
return (attr.is_attr_set(port['port'][addr_pair.ADDRESS_PAIRS])
return (validators.is_attr_set(port['port'][addr_pair.ADDRESS_PAIRS])
and port['port'][addr_pair.ADDRESS_PAIRS] != [])

def _check_update_has_allowed_address_pairs(self, port):
Expand Down
5 changes: 3 additions & 2 deletions neutron/db/db_base_plugin_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import functools

from neutron_lib.api import validators
from neutron_lib import constants
from neutron_lib import exceptions as n_exc
from oslo_config import cfg
Expand Down Expand Up @@ -309,9 +310,9 @@ def _make_subnet_args(self, detail, subnet, subnetpool_id):
'gateway_ip': gateway_ip,
'description': subnet.get('description')}
if subnet['ip_version'] == 6 and subnet['enable_dhcp']:
if attributes.is_attr_set(subnet['ipv6_ra_mode']):
if validators.is_attr_set(subnet['ipv6_ra_mode']):
args['ipv6_ra_mode'] = subnet['ipv6_ra_mode']
if attributes.is_attr_set(subnet['ipv6_address_mode']):
if validators.is_attr_set(subnet['ipv6_address_mode']):
args['ipv6_address_mode'] = subnet['ipv6_address_mode']
return args

Expand Down
35 changes: 18 additions & 17 deletions neutron/db/db_base_plugin_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import functools

import netaddr
from neutron_lib.api import validators
from neutron_lib import constants
from neutron_lib import exceptions as exc
from oslo_config import cfg
Expand Down Expand Up @@ -231,8 +232,8 @@ def _validate_ipv6_attributes(self, subnet, cur_subnet):
if cur_subnet:
self._validate_ipv6_update_dhcp(subnet, cur_subnet)
return
ra_mode_set = attributes.is_attr_set(subnet.get('ipv6_ra_mode'))
address_mode_set = attributes.is_attr_set(
ra_mode_set = validators.is_attr_set(subnet.get('ipv6_ra_mode'))
address_mode_set = validators.is_attr_set(
subnet.get('ipv6_address_mode'))
self._validate_ipv6_dhcp(ra_mode_set, address_mode_set,
subnet['enable_dhcp'])
Expand Down Expand Up @@ -274,16 +275,16 @@ def _validate_ipv6_update_dhcp(self, subnet, cur_subnet):
msg = _("Cannot disable enable_dhcp with "
"ipv6 attributes set")

ra_mode_set = attributes.is_attr_set(subnet.get('ipv6_ra_mode'))
address_mode_set = attributes.is_attr_set(
ra_mode_set = validators.is_attr_set(subnet.get('ipv6_ra_mode'))
address_mode_set = validators.is_attr_set(
subnet.get('ipv6_address_mode'))

if ra_mode_set or address_mode_set:
raise exc.InvalidInput(error_message=msg)

old_ra_mode_set = attributes.is_attr_set(
old_ra_mode_set = validators.is_attr_set(
cur_subnet.get('ipv6_ra_mode'))
old_address_mode_set = attributes.is_attr_set(
old_address_mode_set = validators.is_attr_set(
cur_subnet.get('ipv6_address_mode'))

if old_ra_mode_set or old_address_mode_set:
Expand Down Expand Up @@ -439,7 +440,7 @@ def _validate_subnet(self, context, s, cur_subnet=None):

ip_ver = s['ip_version']

if attributes.is_attr_set(s.get('cidr')):
if validators.is_attr_set(s.get('cidr')):
self._validate_ip_version(ip_ver, s['cidr'], 'cidr')

# TODO(watanabe.isao): After we found a way to avoid the re-sync
Expand All @@ -466,7 +467,7 @@ def _validate_subnet(self, context, s, cur_subnet=None):
"if enable_dhcp is True.")
raise exc.InvalidInput(error_message=error_message)

if attributes.is_attr_set(s.get('gateway_ip')):
if validators.is_attr_set(s.get('gateway_ip')):
self._validate_ip_version(ip_ver, s['gateway_ip'], 'gateway_ip')
is_gateway_not_valid = (
ipam.utils.check_gateway_invalid_in_subnet(
Expand All @@ -491,7 +492,7 @@ def _validate_subnet(self, context, s, cur_subnet=None):
ip_address=cur_subnet['gateway_ip'],
port_id=allocated['port_id'])

if attributes.is_attr_set(s.get('dns_nameservers')):
if validators.is_attr_set(s.get('dns_nameservers')):
if len(s['dns_nameservers']) > cfg.CONF.max_dns_nameservers:
raise n_exc.DNSNameServersExhausted(
subnet_id=s.get('id', _('new subnet')),
Expand All @@ -505,7 +506,7 @@ def _validate_subnet(self, context, s, cur_subnet=None):
dns))
self._validate_ip_version(ip_ver, dns, 'dns_nameserver')

if attributes.is_attr_set(s.get('host_routes')):
if validators.is_attr_set(s.get('host_routes')):
if len(s['host_routes']) > cfg.CONF.max_subnet_host_routes:
raise n_exc.HostRoutesExhausted(
subnet_id=s.get('id', _('new subnet')),
Expand All @@ -515,11 +516,11 @@ def _validate_subnet(self, context, s, cur_subnet=None):
self._validate_host_route(rt, ip_ver)

if ip_ver == 4:
if attributes.is_attr_set(s.get('ipv6_ra_mode')):
if validators.is_attr_set(s.get('ipv6_ra_mode')):
raise exc.InvalidInput(
error_message=(_("ipv6_ra_mode is not valid when "
"ip_version is 4")))
if attributes.is_attr_set(s.get('ipv6_address_mode')):
if validators.is_attr_set(s.get('ipv6_address_mode')):
raise exc.InvalidInput(
error_message=(_("ipv6_address_mode is not valid when "
"ip_version is 4")))
Expand Down Expand Up @@ -626,11 +627,11 @@ def _get_subnetpool_id(self, context, subnet):
return

cidr = subnet.get('cidr')
if attributes.is_attr_set(cidr):
if validators.is_attr_set(cidr):
ip_version = netaddr.IPNetwork(cidr).version
else:
ip_version = subnet.get('ip_version')
if not attributes.is_attr_set(ip_version):
if not validators.is_attr_set(ip_version):
msg = _('ip_version must be specified in the absence of '
'cidr and subnetpool_id')
raise exc.BadRequest(resource='subnets', msg=msg)
Expand Down Expand Up @@ -658,8 +659,8 @@ def create_subnet(self, context, subnet):
s = subnet['subnet']
cidr = s.get('cidr', constants.ATTR_NOT_SPECIFIED)
prefixlen = s.get('prefixlen', constants.ATTR_NOT_SPECIFIED)
has_cidr = attributes.is_attr_set(cidr)
has_prefixlen = attributes.is_attr_set(prefixlen)
has_cidr = validators.is_attr_set(cidr)
has_prefixlen = validators.is_attr_set(prefixlen)

if has_cidr and has_prefixlen:
msg = _('cidr and prefixlen must not be supplied together')
Expand Down Expand Up @@ -915,7 +916,7 @@ def _validate_address_scope_id(self, context, address_scope_id,
- the address family of the subnetpool and address scope
are the same
"""
if not attributes.is_attr_set(address_scope_id):
if not validators.is_attr_set(address_scope_id):
return

if not self.is_address_scope_owned_by_tenant(context,
Expand Down
4 changes: 2 additions & 2 deletions neutron/db/dns_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
# License for the specific language governing permissions and limitations
# under the License.

from neutron_lib.api import validators
from neutron_lib import exceptions as n_exc
from oslo_config import cfg
from oslo_log import log as logging
import sqlalchemy as sa
from sqlalchemy import orm

from neutron._i18n import _, _LE
from neutron.api.v2 import attributes
from neutron.common import utils
from neutron.db import db_base_plugin_v2
from neutron.db import l3_db
Expand Down Expand Up @@ -154,7 +154,7 @@ def _process_dns_floatingip_create_precommit(self, context,
floatingip_data, req_data):
# expects to be called within a plugin's session
dns_domain = req_data.get(dns.DNSDOMAIN)
if not attributes.is_attr_set(dns_domain):
if not validators.is_attr_set(dns_domain):
return
if not self.dns_driver:
return
Expand Down
5 changes: 3 additions & 2 deletions neutron/db/external_net_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# License for the specific language governing permissions and limitations
# under the License.

from neutron_lib.api import validators
from neutron_lib import constants as l3_constants
from neutron_lib import exceptions as n_exc
import sqlalchemy as sa
Expand Down Expand Up @@ -118,7 +119,7 @@ def _extend_network_dict_l3(self, network_res, network_db):

def _process_l3_create(self, context, net_data, req_data):
external = req_data.get(external_net.EXTERNAL)
external_set = attributes.is_attr_set(external)
external_set = validators.is_attr_set(external)

if not external_set:
return
Expand Down Expand Up @@ -157,7 +158,7 @@ def _process_l3_update(self, context, net_data, req_data, allow_all=True):

new_value = req_data.get(external_net.EXTERNAL)
net_id = net_data['id']
if not attributes.is_attr_set(new_value):
if not validators.is_attr_set(new_value):
return

if net_data.get(external_net.EXTERNAL) == new_value:
Expand Down
14 changes: 7 additions & 7 deletions neutron/db/ipam_backend_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import itertools

import netaddr
from neutron_lib.api import validators
from neutron_lib import constants as const
from neutron_lib import exceptions as exc
from oslo_config import cfg
Expand All @@ -25,7 +26,6 @@
from sqlalchemy.orm import exc as orm_exc

from neutron._i18n import _, _LI
from neutron.api.v2 import attributes
from neutron.common import constants
from neutron.common import exceptions as n_exc
from neutron.common import ipv6_utils
Expand Down Expand Up @@ -79,8 +79,8 @@ def validate_pools_with_subnetpool(self, subnet):
Allocation pools can be set for specific subnet request only
"""
has_allocpool = attributes.is_attr_set(subnet['allocation_pools'])
is_any_subnetpool_request = not attributes.is_attr_set(subnet['cidr'])
has_allocpool = validators.is_attr_set(subnet['allocation_pools'])
is_any_subnetpool_request = not validators.is_attr_set(subnet['cidr'])
if is_any_subnetpool_request and has_allocpool:
reason = _("allocation_pools allowed only "
"for specific subnet requests.")
Expand All @@ -89,7 +89,7 @@ def validate_pools_with_subnetpool(self, subnet):
def _validate_ip_version_with_subnetpool(self, subnet, subnetpool):
"""Validates ip version for subnet_pool and requested subnet"""
ip_version = subnet.get('ip_version')
has_ip_version = attributes.is_attr_set(ip_version)
has_ip_version = validators.is_attr_set(ip_version)
if has_ip_version and ip_version != subnetpool.ip_version:
args = {'req_ver': str(subnet['ip_version']),
'pool_ver': str(subnetpool.ip_version)}
Expand Down Expand Up @@ -350,7 +350,7 @@ def generate_pools(self, cidr, gateway_ip):

def _prepare_allocation_pools(self, allocation_pools, cidr, gateway_ip):
"""Returns allocation pools represented as list of IPRanges"""
if not attributes.is_attr_set(allocation_pools):
if not validators.is_attr_set(allocation_pools):
return self.generate_pools(cidr, gateway_ip)

ip_range_pools = self.pools_to_ip_range(allocation_pools)
Expand Down Expand Up @@ -450,15 +450,15 @@ def _save_subnet(self, context,
context.session.add(subnet)
# NOTE(changzhi) Store DNS nameservers with order into DB one
# by one when create subnet with DNS nameservers
if attributes.is_attr_set(dns_nameservers):
if validators.is_attr_set(dns_nameservers):
for order, server in enumerate(dns_nameservers):
dns = models_v2.DNSNameServer(
address=server,
order=order,
subnet_id=subnet.id)
context.session.add(dns)

if attributes.is_attr_set(host_routes):
if validators.is_attr_set(host_routes):
for rt in host_routes:
route = models_v2.SubnetRoute(
subnet_id=subnet.id,
Expand Down
4 changes: 2 additions & 2 deletions neutron/db/l3_dvr_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# under the License.
import collections

from neutron_lib.api import validators
from neutron_lib import constants as const
from neutron_lib import exceptions as n_exc
from oslo_config import cfg
Expand All @@ -22,7 +23,6 @@
import six

from neutron._i18n import _, _LI, _LW
from neutron.api.v2 import attributes
from neutron.callbacks import events
from neutron.callbacks import exceptions
from neutron.callbacks import registry
Expand Down Expand Up @@ -952,6 +952,6 @@ def is_distributed_router(router):
except AttributeError:
# if not, try to see if it is a request body
requested_router_type = router.get('distributed')
if attributes.is_attr_set(requested_router_type):
if validators.is_attr_set(requested_router_type):
return requested_router_type
return cfg.CONF.router_distributed
Loading

0 comments on commit 78fff41

Please sign in to comment.