Data Validation Plugins
Data validation plugins responsible for validating loaded data to make sure it adheres to model or schema requirements.
By default TTR uses yangson
plugin for data validation, alternatively TTR object
validator
attribute can be used to specify plugin to use.
Validation step is optional, if no models provided, data not validated. However, if required data models can be used to make sure that correct data provided prior to performing rendering step.
There are two types of validation plugins:
Plugins to load models
Plugins to validate the actual data
Yangson Models Loader
Reference name yangson
This plugin loads YANG models into yangson DataModel objects.
YANG models must sit within their own directories, each such a directory used to create JSON library for Yangson to load models from.
Directory name, main YANG model file name and module name must be the same, directory name used as a reference name for the model.
For example, this is directory tree with YANG models inside:
|-- Models
|-- interface
|-- ietf-inet-types@2013-07-15.yang
|-- interface.yang
|-- vrf
|-- vrf.yang
Above directory structure translated to two models named interface
and vrf
, these
names can be used to reference models in data for validation, e.g.:
- interface: Gi1/1
description: Customer A
vid: 100
ip: 10.0.0.1
mask: 255.255.255.0
vrf: cust_a
device: R1
template: interfaces.cisco_ios
model: interface # YANG model name to validate this data item
For reference, YANG model Models/interface/interface.yang
file content is:
module interface {
yang-version "1.1";
namespace "http://ttr/test-1";
import ietf-inet-types {
prefix inet;
}
typedef ipmask {
type string {
pattern '([0-9]{1,3}.){3}[0-9]{1,3}';
}
description
"Pattern to match strings like 255.255.255.0 or 255.0.0.0";
}
prefix "ttr";
leaf interface {
mandatory true;
type string;
}
leaf template {
mandatory true;
type string;
}
leaf device {
mandatory true;
type string;
}
leaf description{
type string;
}
leaf vid {
type int32;
}
leaf ip {
type inet:ipv4-address;
}
leaf mask {
type ipmask;
}
leaf vrf {
type string;
}
}
- ttr.plugins.models.yangson_model_loader.load(models_dict, models_dir, **kwargs)
Creates JSON-encoded YANG library data [RFC7895] and instantiates data model object out of it.
- Parameters
models_dir – (str) OS path to directory with YANG models modules subdirectories, each subdirectory models loaded to form single DataModel and added to models_dict under directory name key.
models_dict – (dict) dictionary to store loaded model object at
kwargs – (dict) any additional arguments ignored
return – None
Yangson Data Validation
Reference name yangson
This plugin relies on Yangson library for data instance validation using YANG models.
- ttr.plugins.validate.validate_yangson.validate(data, model_content, model_name, validation_scope='all', content_type='all', on_fail='raise')
Validate data for compliance with YANG modules.
- Parameters
data – (dict) dictionary data to validate
model_content – (obj) Fully instantiated Yangson DataModel object
model_name – (str) name of the model
content_type – (str) optional, content type as per https://yangson.labs.nic.cz/enumerations.html supported - all, config, nonconfig
validation_scope – (str) optional, validation scope as per https://yangson.labs.nic.cz/enumerations.html supported - all, semantics, syntax
on_fail – (str) action to do if validation fails -
raise
(default) orlog
Returns:
True if validation succeeded
False if validation failed and
on_fail
is “log”Raises
RuntimeError
exception if validation failed andon_fail
is “raise”