forked from micado-scale/component_submitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmicado_validator.py
executable file
·219 lines (167 loc) · 7.57 KB
/
micado_validator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
"""
MiCADO Submission Engine TOSCA Validator
-----------------------------------------
Validate ToscaTemplate objects to ensure syntactic and semantic compatibility
with custom defined and TOSCA normative types.
Further validates a ToscaTemplate which has already passed validation steps
set out by the OpenStack ToscaParser. Currently validation checks exist for
repositories and the requirements and relationships of custom defined types.
"""
from toscaparser.tosca_template import ToscaTemplate
import utils
import logging
logger=logging.getLogger("adaptor."+__name__)
class ValidationError(Exception):
"""Base error for validation"""
class MultiError(ValidationError):
"""Errors occured during validation..."""
def __init__(self, error_set, msg=None):
super().__init__()
msg = msg if msg else ""
self.msg = "\n--{}--".format(msg)
for error in error_set:
self.msg += "\n {}".format(error)
self.msg += "\n----{}".format("-"*len(msg))
def __str__(self):
"""Overload __str__ to return msg when printing/logging"""
return self.msg
def validation(tpl):
""" The validation process
Runs validation steps on the given TOSCA Template, and builds an error
list. Raises a MultiError on failed validation. On success, says so.
:param tpl: The ToscaTemplate to validate
:type tpl: ToscaTemplate <toscaparser.tosca_template.ToscaTemplate>
:raises: TypeError, MultiError
Usage:
>>> from micado_validator import Validator
Successful validation:
>>> Validator().validation(<toscaparser.tosca_template.ToscaTemplate>)
'ToscaTemplate passed compatibility validation'
Errors during validation:
>>> Validator(<toscaparser.tosca_template.ToscaTemplate>)
micado_validator.MultiError:
----
(...list of errors...)
"""
if not isinstance(tpl, ToscaTemplate):
logger.error("Got a non-ToscaTemplate object!")
raise TypeError("Not a ToscaTemplate object")
custom_types = tuple(tpl.topology_template.custom_defs.keys())
errors = set()
for node in tpl.nodetemplates:
errors.update(validate_repositories(node, tpl.repositories))
if _is_custom(node, custom_types):
errors.update(validate_requirements(node))
errors.update(validate_relationships(node))
errors.update(validate_relationship_properties(node))
if errors:
logger.error("Incompatible ToscaTemplate!")
raise MultiError(sorted(errors))#, "Validation Errors!")
else:
logger.info("ToscaTemplate object passed compatibility validation.")
return "ToscaTemplate passed compatibility validation"
def validate_repositories(node, repositories):
""" Validate repository names
Checks to see if repositories have been defined at the top level, and if
nodes reference those repositories correctly. Returns errors if not.
"""
repository_names = [repository.name for repository in repositories]
if not repository_names:
return {"[*TPL] No repositories found!"}
repositories = _key_search("repository", node.entity_tpl)
return {
"[NODE: {}] Repository <{}> not defined!".format(node.name, repo)
for repo in repositories if repo not in repository_names
}
def validate_requirements(node):
""" Validate requirements and their syntax
Checks that requirements in custom_types and in node definitions are
defined as one-item lists and that node definition requirements correctly
reference requirements defined in custom_types. Returns errors if not.
"""
type_reqs = node.type_definition.requirements
node_reqs = node.requirements
type_req_names = _get_requirement_names(type_reqs)
node_req_names = _get_requirement_names(node_reqs)
msg = "Too many requirements per list item!"
if len(type_reqs) != len(type_req_names):
return {"[CUSTOM TYPE: {}] {}".format(node.type, msg)}
elif len(node_reqs) != len(node_req_names):
return {"[NODE: {}] {}".format(node.name, msg)}
return {
"[NODE: {}] Requirement <{}> not defined!".format(node.name, req)
for req in node_req_names if req not in type_req_names
}
def validate_relationships(node):
""" Validate relationships
Checks that relationships used in node definitions correctly reference
relationships defined in TOSCA normative or custom types. Returns errors
if not.
"""
type_reqs = node.type_definition.requirements
node_reqs = node.requirements
errors = set()
for node_req in node_reqs:
relationships = _key_search(["relationship", "type"], node_req)
supported_relationships = \
[_key_search(["relationship", "type"], type_req)
for type_req in type_reqs]
errors.update({
"[NODE: {}] "
"Relationship <{}> not supported!".format(node.name, relationship)
for relationship in relationships
if relationship not in str(supported_relationships)
})
return errors
def validate_relationship_properties(node):
""" Validate relationship properties
Checks that relationships defined properties required by their definition
in TOSCA normative or custom types. Returns errors if not.
"""
errors = set()
for req, prop, relation in _get_required_properties(node):
if not _has_property(req, prop, relation):
errors.update({
"[NODE: {}] Relationship <{}> "
"missing property <{}>".format(node.name, relation, prop)
})
return errors
def _is_custom(node, custom_types):
""" Determine if node is of a custom type """
return True if node.type in custom_types else False
def _has_property(requirements, prop, rel_type):
""" Check if a requirement has the correct properties and type """
for requirement_dict in requirements:
for requirement in requirement_dict.values():
if isinstance(requirement, str):
return True
relation = requirement.get("relationship")
if isinstance(relation, dict) and rel_type in relation.get("type"):
if prop in str(requirement_dict):
return True
return False
def _get_requirement_names(req_dict):
""" Get requirement names """
return [requirement for requirements in
[list(req.keys()) for req in req_dict]
for requirement in requirements]
def _get_required_properties(node):
""" Generate required properties """
for relation in node.related.values():
for prop, prop_obj in relation.get_properties_def().items():
if prop_obj.required:
yield (node.requirements, prop, relation.type)
def _key_search(query, node):
""" Search through the raw data of a node for a value given a key """
def flatten_pairs(nest):
""" Recursively crawl through a nested dictionary """
for key, val in nest.items():
if isinstance(val, dict):
yield from flatten_pairs(val)
elif isinstance(val, list):
for listitem in val:
if isinstance(listitem, dict):
yield from flatten_pairs(listitem)
else:
yield key, val
return [val for key, val in flatten_pairs(node) if key in query]