Merge "BFD validator"

This commit is contained in:
Zuul 2021-10-18 09:00:45 +00:00 committed by Gerrit Code Review
commit f81ee6f64b
3 changed files with 107 additions and 3 deletions

View File

@ -12,6 +12,8 @@
from neutron_lib.api import converters
from neutron_lib.api.definitions import l3
from neutron_lib.api import validators
from neutron_lib.api.validators import bfd as bfd_validator
from neutron_lib import constants
from neutron_lib.db import constants as db_const
@ -30,6 +32,21 @@ BFD_MODE_ASYNC = 'asynchronous'
BFD_MODE_DEMAND = 'demand'
BFD_MODE_ONE_ARM = 'one_arm_echo'
AUTH_TYPE_PWD = 'password' # nosec
AUTH_TYPE_MD5 = 'MD5'
AUTH_TYPE_METIC_MD5 = 'MeticulousMD5'
AUTH_TYPE_SHA1 = 'SHA1'
AUTH_TYPE_METIC_SHA1 = 'MeticulousSHA1'
VALID_AUTH_TYPES = (AUTH_TYPE_PWD, AUTH_TYPE_MD5, AUTH_TYPE_METIC_MD5,
AUTH_TYPE_SHA1, AUTH_TYPE_METIC_SHA1)
VALID_MODES = (BFD_MODE_ASYNC, BFD_MODE_DEMAND, BFD_MODE_ONE_ARM)
validators.add_validator('bfd_mode_validator',
bfd_validator.validate_bfd_mode)
validators.add_validator('bfd_auth_type_validator',
bfd_validator.validate_bfd_auth_type)
RESOURCE_ATTRIBUTE_MAP = {
BFD_MONITORS: {
'id': {'allow_post': False, 'allow_put': False,
@ -51,7 +68,7 @@ RESOURCE_ATTRIBUTE_MAP = {
'required_by_policy': True,
'is_visible': True, 'enforce_policy': True},
'mode': {'allow_post': True, 'allow_put': False,
'validate': {'type:string': db_const.STATUS_FIELD_SIZE},
'validate': {'type:bfd_mode_validator': VALID_MODES},
'default': BFD_MODE_ASYNC, 'is_filter': True,
'is_sort_key': True, 'is_visible': True},
'dst_ip': {'allow_post': True, 'allow_put': False,
@ -83,8 +100,8 @@ RESOURCE_ATTRIBUTE_MAP = {
'is_filter': True, 'is_sort_key': True,
'is_visible': True},
'auth_type': {'allow_post': True, 'allow_put': False,
'validate': {'type:string_or_none':
db_const.NAME_FIELD_SIZE},
'validate': {'type:bfd_auth_type_validator':
VALID_AUTH_TYPES},
'default': constants.ATTR_NOT_SPECIFIED,
'is_visible': True},
'auth_key': {'allow_post': True, 'allow_put': False,

View File

@ -0,0 +1,51 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from neutron_lib.api import validators
from neutron_lib.db import constants as db_const
def validate_bfd_mode(data, valid_modes):
"""Validate BFD monitor mode field
:param data: The data to validate.
:param valid_modes: The mode values that are accepted
:returns: None if data is valid, otherwise a human readable message
indicating why validation failed.
"""
msg = validators.validate_not_empty_string(data,
db_const.STATUS_FIELD_SIZE)
if msg:
return msg
if data not in valid_modes:
msg = (_('BFD monitor mode can be only one of %s') %
(valid_modes,))
return msg
def validate_bfd_auth_type(data, valid_auth_types):
"""Validate BFD monitor auth_type field
:param data: The data to validate.
:param valid_modes: The authenticatio type values that are accepted
:returns: None if data is valid, otherwise a human readable message
indicating why validation failed.
"""
msg = validators.validate_string_or_none(data, db_const.NAME_FIELD_SIZE)
if msg:
return msg
if not data:
return
if data not in valid_auth_types:
msg = (_('BFD monitor aut_type can only one of %s.') %
(valid_auth_types,))
return msg

View File

@ -0,0 +1,36 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from neutron_lib.api.definitions import bfd_monitor as bfd_api
from neutron_lib.api.validators import bfd
from neutron_lib.tests import _base as base
class TestBfdValidators(base.BaseTestCase):
def test_validate_bfd_mode(self):
self.assertIn('Blank strings', bfd.validate_bfd_mode('', None))
for mode in bfd_api.VALID_MODES:
self.assertIsNone(bfd.validate_bfd_mode(mode, bfd_api.VALID_MODES))
expected = 'BFD monitor mode can be only one of'
msg = bfd.validate_bfd_mode('apple', bfd_api.VALID_MODES)
self.assertIn(expected, msg)
def test_validate_bfd_auth_type(self):
self.assertIsNone(bfd.validate_bfd_auth_type('', None))
for a_type in bfd_api.VALID_AUTH_TYPES:
self.assertIsNone(
bfd.validate_bfd_auth_type(a_type, bfd_api.VALID_AUTH_TYPES))
expected = 'BFD monitor aut_type can only one of'
msg = bfd.validate_bfd_auth_type('apple', bfd_api.VALID_AUTH_TYPES)
self.assertIn(expected, msg)