Skip to content

Commit 63e83f8

Browse files
authored
Implement _default values in data validation (#2852)
You can use the '_default' type attribute to specify the default value of a missing attribute. Used to implement the default 'type' for community lists. Replaces the hardcoded value in 'expand_community_list'
1 parent ef5e287 commit 63e83f8

File tree

5 files changed

+17
-5
lines changed

5 files changed

+17
-5
lines changed

docs/dev/validation.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ All attributes defined with a dictionary (**mode** in the above example, but not
122122
* **true_value** -- value to use when the parameter is set to *True*
123123
* **_requires** -- a list of modules that must be enabled in global- or node context to allow the use of this attribute. See `vrfs` in `modules/vrf.yml` and `vlans` in `modules/vlan.yml` for more details.
124124
* **_required** (bool) -- the attribute must be present in the parent dictionary[^CRQ]
125+
* **_default** -- the default value of the attribute. Added to the data structure when the attribute is not specified in the lab topology
125126
* **_valid_with** -- a list or dictionary of attributes that can be used with this attribute ([example](inter-attribute-examples)).
126127
* **_invalid_with** -- specifies attributes that cannot be used together with this attribute. Can be:
127128

netsim/data/validate.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,18 @@ def validate_module_can_be_false(
132132
return bool(intersect)
133133

134134
"""
135-
check_required_keys -- checks that the required keys are present in the data structure
135+
check_required_keys -- checks that the required keys are present in the data structure and sets the default
136+
values for missing keys with _default attribute
136137
"""
137138

138139
def check_required_keys(data: Box, attributes: Box, path: str,module: str) -> bool:
139140
result = True
140141
for k,v in attributes.items():
141-
if isinstance(v,Box) and '_required' in v and v._required:
142+
if not isinstance(v,Box):
143+
continue
144+
if '_default' in v and k not in data:
145+
data[k] = v._default
146+
if '_required' in v and v._required:
142147
if k in data:
143148
continue
144149
log.error(

netsim/modules/routing.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ attributes:
4141
_subtype:
4242
type: dict
4343
_keys:
44-
type: { type: str, valid_values: [ standard, extended, large ]}
44+
type:
45+
type: str
46+
valid_values: [ standard, extended, large ]
47+
_required: True
48+
_default: standard
4549
value:
4650
type: list
4751
make_list: True

netsim/modules/routing/clist.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,6 @@ def fix_clist_entry(e_clist: Box, topology: Box) -> None:
8989
"""
9090
def expand_community_list(p_name: str,o_name: str,node: Box,topology: Box) -> typing.Optional[list]:
9191
p_clist = node.routing[o_name][p_name] # Shortcut pointer to current community list
92-
if 'type' not in p_clist:
93-
p_clist.type = 'standard' # Assume the clist filter for standard communities
9492
regexp = False # Figure out whether we need expanded clist
9593
for (p_idx,p_entry) in enumerate(p_clist.value):
9694
try:

tests/topology/expected/rp-clist-expansion.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ groups:
66
routing:
77
community:
88
cg1:
9+
type: standard
910
value:
1011
- action: permit
1112
path:
@@ -136,18 +137,21 @@ provider: libvirt
136137
routing:
137138
community:
138139
cl4:
140+
type: standard
139141
value:
140142
- action: permit
141143
path:
142144
- 65000:104
143145
sequence: 10
144146
cl5:
147+
type: standard
145148
value:
146149
- action: permit
147150
path:
148151
- 65000:100
149152
sequence: 100
150153
cl7:
154+
type: standard
151155
value:
152156
- _value: 65000:106
153157
action: permit

0 commit comments

Comments
 (0)