1 # -*- coding: utf-8 -*-
3 from jsonschema
import validate
as js_v
, exceptions
as js_e
5 __author__
= "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
7 version_date
= "Mar 2018"
10 Validator of input data using JSON schemas for those items that not contains an OSM yang information model
14 patern_name
= "^[ -~]+$"
15 passwd_schema
= {"type": "string", "minLength": 1, "maxLength": 60}
16 nameshort_schema
= {"type": "string", "minLength": 1, "maxLength": 60, "pattern": "^[^,;()'\"]+$"}
17 name_schema
= {"type": "string", "minLength": 1, "maxLength": 255, "pattern": "^[^,;()'\"]+$"}
18 string_schema
= {"type": "string", "minLength": 1, "maxLength": 255}
19 xml_text_schema
= {"type": "string", "minLength": 1, "maxLength": 1000, "pattern": "^[^']+$"}
20 description_schema
= {"type": ["string", "null"], "maxLength": 255, "pattern": "^[^'\"]+$"}
21 id_schema_fake
= {"type": "string", "minLength": 2, "maxLength": 36}
22 bool_schema
= {"type": "boolean"}
23 null_schema
= {"type": "null"}
24 # "pattern": "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$"
25 id_schema
= {"type": "string", "pattern": "^[a-fA-F0-9]{8}(-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}$"}
26 time_schema
= {"type": "string", "pattern": "^[0-9]{4}-[0-1][0-9]-[0-3][0-9]T[0-2][0-9]([0-5]:){2}"}
27 pci_schema
= {"type": "string", "pattern": "^[0-9a-fA-F]{4}(:[0-9a-fA-F]{2}){2}\.[0-9a-fA-F]$"}
28 http_schema
= {"type": "string", "pattern": "^https?://[^'\"=]+$"}
29 bandwidth_schema
= {"type": "string", "pattern": "^[0-9]+ *([MG]bps)?$"}
30 memory_schema
= {"type": "string", "pattern": "^[0-9]+ *([MG]i?[Bb])?$"}
31 integer0_schema
= {"type": "integer", "minimum": 0}
32 integer1_schema
= {"type": "integer", "minimum": 1}
33 path_schema
= {"type": "string", "pattern": "^(\.){0,2}(/[^/\"':{}\(\)]+)+$"}
34 vlan_schema
= {"type": "integer", "minimum": 1, "maximum": 4095}
35 vlan1000_schema
= {"type": "integer", "minimum": 1000, "maximum": 4095}
36 mac_schema
= {"type": "string",
37 "pattern": "^[0-9a-fA-F][02468aceACE](:[0-9a-fA-F]{2}){5}$"} # must be unicast: LSB bit of MSB byte ==0
38 # mac_schema={"type":"string", "pattern":"^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$"}
39 ip_schema
= {"type": "string",
40 "pattern": "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"}
41 ip_prefix_schema
= {"type": "string",
42 "pattern": "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}"
43 "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/(30|[12]?[0-9])$"}
44 port_schema
= {"type": "integer", "minimum": 1, "maximum": 65534}
45 object_schema
= {"type": "object"}
46 schema_version_2
= {"type": "integer", "minimum": 2, "maximum": 2}
47 # schema_version_string={"type":"string","enum": ["0.1", "2", "0.2", "3", "0.3"]}
48 log_level_schema
= {"type": "string", "enum": ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]}
49 checksum_schema
= {"type": "string", "pattern": "^[0-9a-fA-F]{32}$"}
50 size_schema
= {"type": "integer", "minimum": 1, "maximum": 100}
52 ns_instantiate_vdu
= {
53 "title": "ns action instantiate input schema for vdu",
54 "$schema": "http://json-schema.org/draft-04/schema#",
65 "vim-volume-id": name_schema
,
67 "required": ["name", "vim-volume-id"],
68 "additionalProperties": False
78 "ip-address": ip_schema
,
79 "mac-address": mac_schema
,
80 "floating-ip-required": bool_schema
,
83 "additionalProperties": False
88 "additionalProperties": False
91 ip_profile_dns_schema
= {
99 "required": ["address"],
100 "additionalProperties": False
104 ip_profile_dhcp_schema
= {
107 "enabled": {"type": "boolean"},
108 "count": integer1_schema
,
109 "start-address": ip_schema
111 "additionalProperties": False,
114 ip_profile_schema
= {
115 "title": "ip profile validation schame",
116 "$schema": "http://json-schema.org/draft-04/schema#",
119 "ip-version": {"enum": ["ipv4", "ipv6"]},
120 "subnet-address": ip_prefix_schema
,
121 "gateway-address": ip_schema
,
122 "dns-server": ip_profile_dns_schema
,
123 "dhcp-params": ip_profile_dhcp_schema
,
127 ip_profile_update_schema
= {
128 "title": "ip profile validation schame",
129 "$schema": "http://json-schema.org/draft-04/schema#",
132 "ip-version": {"enum": ["ipv4", "ipv6"]},
133 "subnet-address": {"oneOf": [null_schema
, ip_prefix_schema
]},
134 "gateway-address": {"oneOf": [null_schema
, ip_schema
]},
135 "dns-server": {"oneOf": [null_schema
, ip_profile_dns_schema
]},
137 "dhcp-params": {"oneOf": [null_schema
, ip_profile_dhcp_schema
]},
139 "additionalProperties": False
142 ns_instantiate_internal_vld
= {
143 "title": "ns action instantiate input schema for vdu",
144 "$schema": "http://json-schema.org/draft-04/schema#",
148 "vim-network-name": name_schema
,
149 "ip-profile": ip_profile_update_schema
,
150 "internal-connection-point": {
156 "id-ref": name_schema
,
157 "ip-address": ip_schema
,
159 "required": ["id-ref", "ip-address"],
160 "additionalProperties": False
164 "required": ["name"],
166 "additionalProperties": False
170 "title": "ns action instantiate input schema",
171 "$schema": "http://json-schema.org/draft-04/schema#",
174 "nsName": name_schema
,
175 "nsDescription": {"oneOf": [description_schema
, {"type": "null"}]},
177 "vimAccountId": id_schema
,
178 "ssh_keys": {"type": "string"},
186 "member-vnf-index": name_schema
,
187 "vimAccountId": id_schema
,
191 "items": ns_instantiate_vdu
,
196 "items": ns_instantiate_internal_vld
199 "required": ["member-vnf-index"],
201 "additionalProperties": False
210 "name": string_schema
,
211 "vim-network-name": {"OneOf": [string_schema
, object_schema
]},
212 "ip-profile": object_schema
,
214 "required": ["name"],
215 "additionalProperties": False
219 "required": ["nsName", "nsdId", "vimAccountId"],
220 "additionalProperties": False
223 ns_action
= { # TODO for the moment it is only contemplated the vnfd primitive execution
224 "title": "ns action input schema",
225 "$schema": "http://json-schema.org/draft-04/schema#",
228 "member_vnf_index": name_schema
,
229 "vnf_member_index": name_schema
, # TODO for backward compatibility. To remove in future
230 "primitive": name_schema
,
231 "primitive_params": {"type": "object"},
233 "required": ["primitive", "primitive_params"], # TODO add member_vnf_index
234 "additionalProperties": False
236 ns_scale
= { # TODO for the moment it is only VDU-scaling
237 "title": "ns scale input schema",
238 "$schema": "http://json-schema.org/draft-04/schema#",
241 "scaleType": {"enum": ["SCALE_VNF"]},
245 "vnfInstanceId": name_schema
,
246 "scaleVnfType": {"enum": ["SCALE_OUT", 'SCALE_IN']},
250 "scaling-group-descriptor": name_schema
,
251 "member-vnf-index": name_schema
,
252 "scaling-policy": name_schema
,
254 "required": ["scaling-group-descriptor", "member-vnf-index"],
255 "additionalProperties": False
258 "required": ["scaleVnfType", "scaleByStepData"], # vnfInstanceId
259 "additionalProperties": False
261 "scaleTime": time_schema
,
263 "required": ["scaleType", "scaleVnfData"],
264 "additionalProperties": False
268 schema_version
= {"type": "string", "enum": ["1.0"]}
269 vim_account_edit_schema
= {
270 "title": "vim_account edit input schema",
271 "$schema": "http://json-schema.org/draft-04/schema#",
275 "description": description_schema
,
276 "type": nameshort_schema
, # currently "openvim" or "openstack", can be enlarged with plugins
278 "datacenter": name_schema
,
279 "vim_url": description_schema
,
280 "vim_url_admin": description_schema
,
281 "vim_tenant": name_schema
,
282 "vim_tenant_name": name_schema
,
283 "vim_username": nameshort_schema
,
284 "vim_password": nameshort_schema
,
285 "config": {"type": "object"}
287 "additionalProperties": False
289 schema_type
= {"type": "string"}
291 vim_account_new_schema
= {
292 "title": "vim_account creation input schema",
293 "$schema": "http://json-schema.org/draft-04/schema#",
296 "schema_version": schema_version
,
297 "schema_type": schema_type
,
299 "description": description_schema
,
301 "datacenter": name_schema
,
302 "vim_type": {"enum": ["openstack", "openvim", "vmware", "opennebula", "aws"]},
303 "vim_url": description_schema
,
304 # "vim_url_admin": description_schema,
305 # "vim_tenant": name_schema,
306 "vim_tenant_name": name_schema
,
307 "vim_user": nameshort_schema
,
308 "vim_password": nameshort_schema
,
309 "config": {"type": "object"}
311 "required": ["name", "vim_url", "vim_type", "vim_user", "vim_password", "vim_tenant_name"],
312 "additionalProperties": False
318 "description": description_schema
,
319 "dpid": {"type": "string", "pattern": "^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){7}$"},
322 "type": {"type": "string", "enum": ["opendaylight", "floodlight", "onos"]},
323 "version": {"type": "string", "minLength": 1, "maxLength": 12},
324 "user": nameshort_schema
,
325 "password": passwd_schema
328 "title": "sdn controller information schema",
329 "$schema": "http://json-schema.org/draft-04/schema#",
331 "properties": sdn_properties
,
332 "required": ["name", "port", 'ip', 'dpid', 'type'],
333 "additionalProperties": False
336 "title": "sdn controller update information schema",
337 "$schema": "http://json-schema.org/draft-04/schema#",
339 "properties": sdn_properties
,
340 # "required": ["name", "port", 'ip', 'dpid', 'type'],
341 "additionalProperties": False
343 sdn_port_mapping_schema
= {
344 "$schema": "http://json-schema.org/draft-04/schema#",
345 "title": "sdn port mapping information schema",
350 "compute_node": nameshort_schema
,
357 "switch_port": nameshort_schema
,
358 "switch_mac": mac_schema
364 "required": ["compute_node", "ports"]
367 sdn_external_port_schema
= {
368 "$schema": "http://json-schema.org/draft-04/schema#",
369 "title": "External port ingformation",
372 "port": {"type": "string", "minLength": 1, "maxLength": 60},
380 nbi_new_input_schemas
= {
381 "vim_accounts": vim_account_new_schema
,
382 "sdns": sdn_new_schema
,
383 "ns_instantiate": ns_instantiate
,
384 "ns_action": ns_action
,
388 nbi_edit_input_schemas
= {
389 "vim_accounts": vim_account_edit_schema
,
390 "sdns": sdn_edit_schema
394 class ValidationError(Exception):
398 def validate_input(indata
, item
, new
=True):
400 Validates input data agains json schema
401 :param indata: user input data. Should be a dictionary
402 :param item: can be users, projects, vims, sdns, ns_xxxxx
403 :param new: True if the validation is for creating or False if it is for editing
404 :return: None if ok, raises ValidationError exception otherwise
408 schema_to_use
= nbi_new_input_schemas
.get(item
)
410 schema_to_use
= nbi_edit_input_schemas
.get(item
)
412 js_v(indata
, schema_to_use
)
414 except js_e
.ValidationError
as e
:
416 error_pos
= "at '" + ":".join(map(str, e
.path
)) + "'"
419 raise ValidationError("Format error {} '{}' ".format(error_pos
, e
.message
))