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 nameshort_schema
= {"type": "string", "minLength": 1, "maxLength": 60, "pattern": "^[^,;()\.\$'\"]+$"}
16 passwd_schema
= {"type": "string", "minLength": 1, "maxLength": 60}
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 dpid_Schema
= {"type": "string", "pattern": "^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){7}$"}
39 # mac_schema={"type":"string", "pattern":"^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$"}
40 ip_schema
= {"type": "string",
41 "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]?)$"}
42 ip_prefix_schema
= {"type": "string",
43 "pattern": "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}"
44 "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/(30|[12]?[0-9])$"}
45 port_schema
= {"type": "integer", "minimum": 1, "maximum": 65534}
46 object_schema
= {"type": "object"}
47 schema_version_2
= {"type": "integer", "minimum": 2, "maximum": 2}
48 # schema_version_string={"type":"string","enum": ["0.1", "2", "0.2", "3", "0.3"]}
49 log_level_schema
= {"type": "string", "enum": ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]}
50 checksum_schema
= {"type": "string", "pattern": "^[0-9a-fA-F]{32}$"}
51 size_schema
= {"type": "integer", "minimum": 1, "maximum": 100}
52 array_edition_schema
= {
54 "patternProperties": {
57 "additionalProperties": False,
60 nameshort_list_schema
= {
63 "items": nameshort_schema
,
67 ns_instantiate_vdu
= {
68 "title": "ns action instantiate input schema for vdu",
69 "$schema": "http://json-schema.org/draft-04/schema#",
80 "vim-volume-id": name_schema
,
82 "required": ["name", "vim-volume-id"],
83 "additionalProperties": False
93 "ip-address": ip_schema
,
94 "mac-address": mac_schema
,
95 "floating-ip-required": bool_schema
,
98 "additionalProperties": False
103 "additionalProperties": False
106 ip_profile_dns_schema
= {
112 "address": ip_schema
,
114 "required": ["address"],
115 "additionalProperties": False
119 ip_profile_dhcp_schema
= {
122 "enabled": {"type": "boolean"},
123 "count": integer1_schema
,
124 "start-address": ip_schema
126 "additionalProperties": False,
129 ip_profile_schema
= {
130 "title": "ip profile validation schame",
131 "$schema": "http://json-schema.org/draft-04/schema#",
134 "ip-version": {"enum": ["ipv4", "ipv6"]},
135 "subnet-address": ip_prefix_schema
,
136 "gateway-address": ip_schema
,
137 "dns-server": ip_profile_dns_schema
,
138 "dhcp-params": ip_profile_dhcp_schema
,
142 ip_profile_update_schema
= {
143 "title": "ip profile validation schame",
144 "$schema": "http://json-schema.org/draft-04/schema#",
147 "ip-version": {"enum": ["ipv4", "ipv6"]},
148 "subnet-address": {"oneOf": [null_schema
, ip_prefix_schema
]},
149 "gateway-address": {"oneOf": [null_schema
, ip_schema
]},
150 "dns-server": {"oneOf": [null_schema
, ip_profile_dns_schema
]},
152 "dhcp-params": {"oneOf": [null_schema
, ip_profile_dhcp_schema
]},
154 "additionalProperties": False
157 ns_instantiate_internal_vld
= {
158 "title": "ns action instantiate input schema for vdu",
159 "$schema": "http://json-schema.org/draft-04/schema#",
163 "vim-network-name": name_schema
,
164 "ip-profile": ip_profile_update_schema
,
165 "internal-connection-point": {
171 "id-ref": name_schema
,
172 "ip-address": ip_schema
,
173 # "mac-address": mac_schema,
175 "required": ["id-ref"],
177 "additionalProperties": False
181 "required": ["name"],
183 "additionalProperties": False
187 "title": "ns action instantiate input schema",
188 "$schema": "http://json-schema.org/draft-04/schema#",
191 "nsName": name_schema
,
192 "nsDescription": {"oneOf": [description_schema
, {"type": "null"}]},
194 "vimAccountId": id_schema
,
195 "ssh_keys": {"type": "string"},
203 "member-vnf-index": name_schema
,
204 "vimAccountId": id_schema
,
208 "items": ns_instantiate_vdu
,
213 "items": ns_instantiate_internal_vld
216 "required": ["member-vnf-index"],
218 "additionalProperties": False
227 "name": string_schema
,
228 "vim-network-name": {"OneOf": [string_schema
, object_schema
]},
229 "ip-profile": object_schema
,
230 "vnfd-connection-point-ref": {
236 "member-vnf-index-ref": name_schema
,
237 "vnfd-connection-point-ref": name_schema
,
238 "ip-address": ip_schema
,
239 # "mac-address": mac_schema,
241 "required": ["member-vnf-index-ref", "vnfd-connection-point-ref"],
243 "additionalProperties": False
247 "required": ["name"],
248 "additionalProperties": False
252 "required": ["nsName", "nsdId", "vimAccountId"],
253 "additionalProperties": False
256 ns_action
= { # TODO for the moment it is only contemplated the vnfd primitive execution
257 "title": "ns action input schema",
258 "$schema": "http://json-schema.org/draft-04/schema#",
261 "member_vnf_index": name_schema
,
262 "vnf_member_index": name_schema
, # TODO for backward compatibility. To remove in future
263 "vdu_id": name_schema
,
264 "primitive": name_schema
,
265 "primitive_params": {"type": "object"},
267 "required": ["primitive", "primitive_params"], # TODO add member_vnf_index
268 "additionalProperties": False
270 ns_scale
= { # TODO for the moment it is only VDU-scaling
271 "title": "ns scale input schema",
272 "$schema": "http://json-schema.org/draft-04/schema#",
275 "scaleType": {"enum": ["SCALE_VNF"]},
279 "vnfInstanceId": name_schema
,
280 "scaleVnfType": {"enum": ["SCALE_OUT", 'SCALE_IN']},
284 "scaling-group-descriptor": name_schema
,
285 "member-vnf-index": name_schema
,
286 "scaling-policy": name_schema
,
288 "required": ["scaling-group-descriptor", "member-vnf-index"],
289 "additionalProperties": False
292 "required": ["scaleVnfType", "scaleByStepData"], # vnfInstanceId
293 "additionalProperties": False
295 "scaleTime": time_schema
,
297 "required": ["scaleType", "scaleVnfData"],
298 "additionalProperties": False
302 schema_version
= {"type": "string", "enum": ["1.0"]}
303 vim_account_edit_schema
= {
304 "title": "vim_account edit input schema",
305 "$schema": "http://json-schema.org/draft-04/schema#",
309 "description": description_schema
,
310 "type": nameshort_schema
, # currently "openvim" or "openstack", can be enlarged with plugins
312 "datacenter": name_schema
,
313 "vim_url": description_schema
,
314 "vim_url_admin": description_schema
,
315 "vim_tenant": name_schema
,
316 "vim_tenant_name": name_schema
,
317 "vim_username": nameshort_schema
,
318 "vim_password": passwd_schema
,
319 "config": {"type": "object"}
321 "additionalProperties": False
323 schema_type
= {"type": "string"}
325 vim_account_new_schema
= {
326 "title": "vim_account creation input schema",
327 "$schema": "http://json-schema.org/draft-04/schema#",
330 "schema_version": schema_version
,
331 "schema_type": schema_type
,
333 "description": description_schema
,
335 "datacenter": name_schema
,
336 "vim_type": {"enum": ["openstack", "openvim", "vmware", "opennebula", "aws"]},
337 "vim_url": description_schema
,
338 # "vim_url_admin": description_schema,
339 # "vim_tenant": name_schema,
340 "vim_tenant_name": name_schema
,
341 "vim_user": nameshort_schema
,
342 "vim_password": passwd_schema
,
343 "config": {"type": "object"}
345 "required": ["name", "vim_url", "vim_type", "vim_user", "vim_password", "vim_tenant_name"],
346 "additionalProperties": False
352 "description": description_schema
,
356 "type": {"type": "string", "enum": ["opendaylight", "floodlight", "onos"]},
357 "version": {"type": "string", "minLength": 1, "maxLength": 12},
358 "user": nameshort_schema
,
359 "password": passwd_schema
362 "title": "sdn controller information schema",
363 "$schema": "http://json-schema.org/draft-04/schema#",
365 "properties": sdn_properties
,
366 "required": ["name", "port", 'ip', 'dpid', 'type'],
367 "additionalProperties": False
370 "title": "sdn controller update information schema",
371 "$schema": "http://json-schema.org/draft-04/schema#",
373 "properties": sdn_properties
,
374 # "required": ["name", "port", 'ip', 'dpid', 'type'],
375 "additionalProperties": False
377 sdn_port_mapping_schema
= {
378 "$schema": "http://json-schema.org/draft-04/schema#",
379 "title": "sdn port mapping information schema",
384 "compute_node": nameshort_schema
,
391 "switch_port": nameshort_schema
,
392 "switch_mac": mac_schema
398 "required": ["compute_node", "ports"]
401 sdn_external_port_schema
= {
402 "$schema": "http://json-schema.org/draft-04/schema#",
403 "title": "External port information",
406 "port": {"type": "string", "minLength": 1, "maxLength": 60},
417 "name": nameshort_schema
,
419 "type": {"enum": ["overlay", 'underlay']},
420 "ip_address": ip_schema
,
421 # TODO, add user, password, ssh-key
422 "mac_address": mac_schema
,
423 "vim_network_name": nameshort_schema
, # interface is connected to one vim network, or switch port
424 "vim_network_id": nameshort_schema
,
425 # provide this in case SDN assist must deal with this interface
426 "switch_dpid": dpid_Schema
,
427 "switch_port": nameshort_schema
,
428 "switch_mac": nameshort_schema
,
429 "switch_vlan": vlan_schema
,
431 "required": ["name", "mgmt", "ip_address"],
432 "additionalProperties": False
435 "title": "pdu creation input schema",
436 "$schema": "http://json-schema.org/draft-04/schema#",
439 "name": nameshort_schema
,
440 "type": nameshort_schema
,
441 "description": description_schema
,
442 "shared": bool_schema
,
443 "vims": nameshort_list_schema
,
444 "vim_accounts": nameshort_list_schema
,
447 "items": {"type": pdu_interface
},
451 "required": ["name", "type", "interfaces"],
452 "additionalProperties": False
456 "title": "pdu edit input schema",
457 "$schema": "http://json-schema.org/draft-04/schema#",
460 "name": nameshort_schema
,
461 "type": nameshort_schema
,
462 "description": description_schema
,
463 "shared": bool_schema
,
464 "vims": {"oneOff": [array_edition_schema
, nameshort_list_schema
]},
465 "vim_accounts": {"oneOff": [array_edition_schema
, nameshort_list_schema
]},
466 "interfaces": {"oneOff": [
467 array_edition_schema
,
470 "items": {"type": pdu_interface
},
475 "additionalProperties": False,
481 "$schema": "http://json-schema.org/draft-04/schema#",
482 "title": "New user schema",
485 "username": nameshort_schema
,
486 "password": passwd_schema
,
487 "projects": nameshort_list_schema
,
489 "required": ["username", "password", "projects"],
490 "additionalProperties": False
493 "$schema": "http://json-schema.org/draft-04/schema#",
494 "title": "User edit schema for administrators",
497 "password": passwd_schema
,
500 nameshort_list_schema
,
506 "additionalProperties": False
510 project_new_schema
= {
511 "$schema": "http://json-schema.org/draft-04/schema#",
512 "title": "New project schema for administrators",
515 "name": nameshort_schema
,
516 "admin": bool_schema
,
518 "required": ["name"],
519 "additionalProperties": False
521 project_edit_schema
= {
522 "$schema": "http://json-schema.org/draft-04/schema#",
523 "title": "Project edit schema for administrators",
526 "admin": bool_schema
,
528 "additionalProperties": False,
534 nbi_new_input_schemas
= {
535 "users": user_new_schema
,
536 "projects": project_new_schema
,
537 "vim_accounts": vim_account_new_schema
,
538 "sdns": sdn_new_schema
,
539 "ns_instantiate": ns_instantiate
,
540 "ns_action": ns_action
,
541 "ns_scale": ns_scale
,
542 "pdus": pdu_new_schema
,
545 nbi_edit_input_schemas
= {
546 "users": user_edit_schema
,
547 "projects": project_edit_schema
,
548 "vim_accounts": vim_account_edit_schema
,
549 "sdns": sdn_edit_schema
,
550 "pdus": pdu_edit_schema
,
554 class ValidationError(Exception):
558 def validate_input(indata
, item
, new
=True):
560 Validates input data against json schema
561 :param indata: user input data. Should be a dictionary
562 :param item: can be users, projects, vims, sdns, ns_xxxxx
563 :param new: True if the validation is for creating or False if it is for editing
564 :return: None if ok, raises ValidationError exception otherwise
568 schema_to_use
= nbi_new_input_schemas
.get(item
)
570 schema_to_use
= nbi_edit_input_schemas
.get(item
)
572 js_v(indata
, schema_to_use
)
574 except js_e
.ValidationError
as e
:
576 error_pos
= "at '" + ":".join(map(str, e
.path
)) + "'"
579 raise ValidationError("Format error {} '{}' ".format(error_pos
, e
.message
))