Copy the Yamale schema.yaml file into the source-code verbatim. This allows the tool to run without any external (file) dependencies, using the built-in yamale_schema if the -s/--schema flag is not specified

This commit is contained in:
Pim van Pelt
2022-04-03 15:53:00 +00:00
parent a561dc64b1
commit 53a7935168
4 changed files with 91 additions and 18 deletions

View File

@ -30,6 +30,7 @@ from config.bondethernet import validate_bondethernets
from config.interface import validate_interfaces from config.interface import validate_interfaces
from config.bridgedomain import validate_bridgedomains from config.bridgedomain import validate_bridgedomains
from config.vxlan_tunnel import validate_vxlan_tunnels from config.vxlan_tunnel import validate_vxlan_tunnels
from config.schema import yamale_schema
from yamale.validators import DefaultValidators, Validator from yamale.validators import DefaultValidators, Validator
import ipaddress import ipaddress
@ -73,23 +74,24 @@ class Validator(object):
if not yaml: if not yaml:
return ret_rv, ret_msgs return ret_rv, ret_msgs
if self.schema: try:
try: validators = DefaultValidators.copy()
validators[IPInterfaceWithPrefixLength.tag] = IPInterfaceWithPrefixLength
if self.schema:
self.logger.debug("Validating against schema %s" % self.schema) self.logger.debug("Validating against schema %s" % self.schema)
validators = DefaultValidators.copy()
validators[IPInterfaceWithPrefixLength.tag] = IPInterfaceWithPrefixLength
schema = yamale.make_schema(self.schema, validators=validators) schema = yamale.make_schema(self.schema, validators=validators)
data = yamale.make_data(content=str(yaml)) else:
yamale.validate(schema, data) self.logger.debug("Validating against built-in schema")
self.logger.debug("Schema correctly validated by yamale") schema = yamale.make_schema(content=yamale_schema, validators=validators)
except ValueError as e: data = yamale.make_data(content=str(yaml))
ret_rv = False yamale.validate(schema, data)
for result in e.results: self.logger.debug("Schema correctly validated by yamale")
for error in result.errors: except ValueError as e:
ret_msgs.extend(['yamale: %s' % error]) ret_rv = False
return ret_rv, ret_msgs for result in e.results:
else: for error in result.errors:
self.logger.warning("Schema validation disabled") ret_msgs.extend(['yamale: %s' % error])
return ret_rv, ret_msgs
self.logger.debug("Validating Semantics...") self.logger.debug("Validating Semantics...")

68
config/schema.py Normal file
View File

@ -0,0 +1,68 @@
#
# Copyright (c) 2022 Pim van Pelt
#
# 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.
#
### NOTE(pim): The source of truth of this string lives in ../schema.yaml
### Make sure to include it here, verbatim, if it ever changes.
yamale_schema = r"""
interfaces: map(include('interface'),key=str(),required=False)
bondethernets: map(include('bondethernet'),key=str(matches='BondEthernet[0-9]+'),required=False)
loopbacks: map(include('loopback'),key=str(matches='loop[0-9]+'),required=False)
bridgedomains: map(include('bridgedomain'),key=str(matches='bd[0-9]+'),required=False)
vxlan_tunnels: map(include('vxlan'),key=str(matches='vxlan_tunnel[0-9]+'),required=False)
---
vxlan:
description: str(exclude='\'"',len=64,required=False)
local: ip()
remote: ip()
vni: int(min=1,max=16777215)
---
bridgedomain:
description: str(exclude='\'"',len=64,required=False)
mtu: int(min=128,max=9216,required=False)
bvi: str(matches='loop[0-9]+',required=False)
interfaces: list(str(),required=False)
---
loopback:
description: str(exclude='\'"',len=64,required=False)
lcp: str(max=15,matches='[a-z]+[a-z0-9-]*',required=False)
mtu: int(min=128,max=9216,required=False)
addresses: list(ip_interface(),min=1,max=6,required=False)
---
bondethernet:
description: str(exclude='\'"',len=64,required=False)
interfaces: list(str(matches='.*GigabitEthernet[0-9]+/[0-9]+/[0-9]+'))
---
interface:
description: str(exclude='\'"',len=64,required=False)
mac: mac(required=False)
lcp: str(max=15,matches='[a-z]+[a-z0-9-]*',required=False)
mtu: int(min=128,max=9216,required=False)
addresses: list(ip_interface(),min=1,max=6,required=False)
sub-interfaces: map(include('sub-interface'),key=int(min=1,max=4294967295),required=False)
l2xc: str(required=False)
---
sub-interface:
description: str(exclude='\'"',len=64,required=False)
lcp: str(max=15,matches='[a-z]+[a-z0-9-]*',required=False)
mtu: int(min=128,max=9216,required=False)
addresses: list(ip_interface(),required=False)
encapsulation: include('encapsulation',required=False)
l2xc: str(required=False)
---
encapsulation:
dot1q: int(min=1,max=4095,required=False)
dot1ad: int(min=1,max=4095,required=False)
inner-dot1q: int(min=1,max=4095,required=False)
exact-match: bool(required=False)
"""

View File

@ -1,3 +1,6 @@
### NOTE(pim): This file is the source of truth for the Yamale schema validator.
### Make sure to copy this file into config/schema.py's yamale_schema
### when it is changed here.
interfaces: map(include('interface'),key=str(),required=False) interfaces: map(include('interface'),key=str(),required=False)
bondethernets: map(include('bondethernet'),key=str(matches='BondEthernet[0-9]+'),required=False) bondethernets: map(include('bondethernet'),key=str(matches='BondEthernet[0-9]+'),required=False)
loopbacks: map(include('loopback'),key=str(matches='loop[0-9]+'),required=False) loopbacks: map(include('loopback'),key=str(matches='loop[0-9]+'),required=False)

6
vppcfg
View File

@ -36,18 +36,18 @@ def main():
subparsers = parser.add_subparsers(dest='command') subparsers = parser.add_subparsers(dest='command')
check_p = subparsers.add_parser('check', help="check given YAML config for validity (no VPP)") check_p = subparsers.add_parser('check', help="check given YAML config for validity (no VPP)")
check_p.add_argument('-s', '--schema', dest='schema', type=str, default='./schema.yaml', help="""YAML schema validation file""") check_p.add_argument('-s', '--schema', dest='schema', type=str, help="""YAML schema validation file, default to use built-in""")
check_p.add_argument('-c', '--config', dest='config', required=True, type=str, help="""YAML configuration file for vppcfg""") check_p.add_argument('-c', '--config', dest='config', required=True, type=str, help="""YAML configuration file for vppcfg""")
dump_p = subparsers.add_parser('dump', help="dump current running VPP configuration (VPP readonly)") dump_p = subparsers.add_parser('dump', help="dump current running VPP configuration (VPP readonly)")
plan_p = subparsers.add_parser('plan', help="plan changes from current VPP dataplane to target config (VPP readonly)") plan_p = subparsers.add_parser('plan', help="plan changes from current VPP dataplane to target config (VPP readonly)")
plan_p.add_argument('-s', '--schema', dest='schema', type=str, default='./schema.yaml', help="""YAML schema validation file""") plan_p.add_argument('-s', '--schema', dest='schema', type=str, help="""YAML schema validation file, default to use built-in""")
plan_p.add_argument('-c', '--config', dest='config', required=True, type=str, help="""YAML configuration file for vppcfg""") plan_p.add_argument('-c', '--config', dest='config', required=True, type=str, help="""YAML configuration file for vppcfg""")
plan_p.add_argument('-o', '--output', dest='outfile', required=False, default='-', type=str, help="""Output file for VPP CLI commands, default stdout""") plan_p.add_argument('-o', '--output', dest='outfile', required=False, default='-', type=str, help="""Output file for VPP CLI commands, default stdout""")
apply_p = subparsers.add_parser('apply', help="apply changes from current VPP dataplane to target config") apply_p = subparsers.add_parser('apply', help="apply changes from current VPP dataplane to target config")
apply_p.add_argument('-s', '--schema', dest='schema', type=str, default='./schema.yaml', help="""YAML schema validation file""") apply_p.add_argument('-s', '--schema', dest='schema', type=str, help="""YAML schema validation file, default to use built-in""")
apply_p.add_argument('-c', '--config', dest='config', required=True, type=str, help="""YAML configuration file for vppcfg""") apply_p.add_argument('-c', '--config', dest='config', required=True, type=str, help="""YAML configuration file for vppcfg""")
args = parser.parse_args() args = parser.parse_args()