1 # -*- coding: utf-8 -*-
3 from jsonschema
import validate
as js_v
, exceptions
as js_e
4 from http
import HTTPStatus
6 __author__
= "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
8 version_date
= "Mar 2018"
11 Validator of input data using JSON schemas for those items that not contains an OSM yang information model
15 patern_name
= "^[ -~]+$"
16 nameshort_schema
= {"type": "string", "minLength": 1, "maxLength": 60, "pattern": "^[^,;()\\.\\$'\"]+$"}
17 passwd_schema
= {"type": "string", "minLength": 1, "maxLength": 60}
18 name_schema
= {"type": "string", "minLength": 1, "maxLength": 255, "pattern": "^[^,;()'\"]+$"}
19 string_schema
= {"type": "string", "minLength": 1, "maxLength": 255}
20 xml_text_schema
= {"type": "string", "minLength": 1, "maxLength": 1000, "pattern": "^[^']+$"}
21 description_schema
= {"type": ["string", "null"], "maxLength": 255, "pattern": "^[^'\"]+$"}
22 id_schema_fake
= {"type": "string", "minLength": 2, "maxLength": 36}
23 bool_schema
= {"type": "boolean"}
24 null_schema
= {"type": "null"}
25 # "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}$"
26 id_schema
= {"type": "string", "pattern": "^[a-fA-F0-9]{8}(-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}$"}
27 time_schema
= {"type": "string", "pattern": "^[0-9]{4}-[0-1][0-9]-[0-3][0-9]T[0-2][0-9]([0-5]:){2}"}
28 pci_schema
= {"type": "string", "pattern": "^[0-9a-fA-F]{4}(:[0-9a-fA-F]{2}){2}\\.[0-9a-fA-F]$"}
29 # allows [] for wildcards. For that reason huge length limit is set
30 pci_extended_schema
= {"type": "string", "pattern": "^[0-9a-fA-F.:-\\[\\]]{12,40}$"}
31 http_schema
= {"type": "string", "pattern": "^https?://[^'\"=]+$"}
32 bandwidth_schema
= {"type": "string", "pattern": "^[0-9]+ *([MG]bps)?$"}
33 memory_schema
= {"type": "string", "pattern": "^[0-9]+ *([MG]i?[Bb])?$"}
34 integer0_schema
= {"type": "integer", "minimum": 0}
35 integer1_schema
= {"type": "integer", "minimum": 1}
36 path_schema
= {"type": "string", "pattern": "^(\\.){0,2}(/[^/\"':{}\\(\\)]+)+$"}
37 vlan_schema
= {"type": "integer", "minimum": 1, "maximum": 4095}
38 vlan1000_schema
= {"type": "integer", "minimum": 1000, "maximum": 4095}
39 mac_schema
= {"type": "string",
40 "pattern": "^[0-9a-fA-F][02468aceACE](:[0-9a-fA-F]{2}){5}$"} # must be unicast: LSB bit of MSB byte ==0
41 dpid_Schema
= {"type": "string", "pattern": "^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){7}$"}
42 # mac_schema={"type":"string", "pattern":"^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$"}
43 ip_schema
= {"type": "string",
44 "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]?)$"}
45 ip_prefix_schema
= {"type": "string",
46 "pattern": "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}"
47 "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/(30|[12]?[0-9])$"}
48 port_schema
= {"type": "integer", "minimum": 1, "maximum": 65534}
49 object_schema
= {"type": "object"}
50 schema_version_2
= {"type": "integer", "minimum": 2, "maximum": 2}
51 # schema_version_string={"type":"string","enum": ["0.1", "2", "0.2", "3", "0.3"]}
52 log_level_schema
= {"type": "string", "enum": ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]}
53 checksum_schema
= {"type": "string", "pattern": "^[0-9a-fA-F]{32}$"}
54 size_schema
= {"type": "integer", "minimum": 1, "maximum": 100}
55 array_edition_schema
= {
57 "patternProperties": {
60 "additionalProperties": False,
63 nameshort_list_schema
= {
66 "items": nameshort_schema
,
70 ns_instantiate_vdu
= {
71 "title": "ns action instantiate input schema for vdu",
72 "$schema": "http://json-schema.org/draft-04/schema#",
83 "vim-volume-id": name_schema
,
85 "required": ["name", "vim-volume-id"],
86 "additionalProperties": False
96 "ip-address": ip_schema
,
97 "mac-address": mac_schema
,
98 "floating-ip-required": bool_schema
,
100 "required": ["name"],
101 "additionalProperties": False
106 "additionalProperties": False
109 ip_profile_dns_schema
= {
115 "address": ip_schema
,
117 "required": ["address"],
118 "additionalProperties": False
122 ip_profile_dhcp_schema
= {
125 "enabled": {"type": "boolean"},
126 "count": integer1_schema
,
127 "start-address": ip_schema
129 "additionalProperties": False,
132 ip_profile_schema
= {
133 "title": "ip profile validation schame",
134 "$schema": "http://json-schema.org/draft-04/schema#",
137 "ip-version": {"enum": ["ipv4", "ipv6"]},
138 "subnet-address": ip_prefix_schema
,
139 "gateway-address": ip_schema
,
140 "dns-server": ip_profile_dns_schema
,
141 "dhcp-params": ip_profile_dhcp_schema
,
145 ip_profile_update_schema
= {
146 "title": "ip profile validation schame",
147 "$schema": "http://json-schema.org/draft-04/schema#",
150 "ip-version": {"enum": ["ipv4", "ipv6"]},
151 "subnet-address": {"oneOf": [null_schema
, ip_prefix_schema
]},
152 "gateway-address": {"oneOf": [null_schema
, ip_schema
]},
153 "dns-server": {"oneOf": [null_schema
, ip_profile_dns_schema
]},
155 "dhcp-params": {"oneOf": [null_schema
, ip_profile_dhcp_schema
]},
157 "additionalProperties": False
160 ns_instantiate_internal_vld
= {
161 "title": "ns action instantiate input schema for vdu",
162 "$schema": "http://json-schema.org/draft-04/schema#",
166 "vim-network-name": name_schema
,
167 "ip-profile": ip_profile_update_schema
,
168 "internal-connection-point": {
174 "id-ref": name_schema
,
175 "ip-address": ip_schema
,
176 # "mac-address": mac_schema,
178 "required": ["id-ref"],
180 "additionalProperties": False
184 "required": ["name"],
186 "additionalProperties": False
190 "title": "ns action instantiate input schema",
191 "$schema": "http://json-schema.org/draft-04/schema#",
194 "lcmOperationType": string_schema
,
195 "nsInstanceId": id_schema
,
196 "netsliceInstanceId": id_schema
,
197 "nsName": name_schema
,
198 "nsDescription": {"oneOf": [description_schema
, {"type": "null"}]},
200 "vimAccountId": id_schema
,
201 "ssh_keys": {"type": "array", "items": {"type": "string"}},
203 "vduImage": name_schema
,
210 "member-vnf-index": name_schema
,
211 "vimAccountId": id_schema
,
215 "items": ns_instantiate_vdu
,
220 "items": ns_instantiate_internal_vld
223 "required": ["member-vnf-index"],
225 "additionalProperties": False
234 "name": string_schema
,
235 "vim-network-name": {"OneOf": [string_schema
, object_schema
]},
236 "ip-profile": object_schema
,
237 "vnfd-connection-point-ref": {
243 "member-vnf-index-ref": name_schema
,
244 "vnfd-connection-point-ref": name_schema
,
245 "ip-address": ip_schema
,
246 # "mac-address": mac_schema,
248 "required": ["member-vnf-index-ref", "vnfd-connection-point-ref"],
250 "additionalProperties": False
254 "required": ["name"],
255 "additionalProperties": False
259 "required": ["nsName", "nsdId", "vimAccountId"],
260 "additionalProperties": False
263 ns_action
= { # TODO for the moment it is only contemplated the vnfd primitive execution
264 "title": "ns action input schema",
265 "$schema": "http://json-schema.org/draft-04/schema#",
268 "lcmOperationType": string_schema
,
269 "nsInstanceId": id_schema
,
270 "member_vnf_index": name_schema
,
271 "vnf_member_index": name_schema
, # TODO for backward compatibility. To remove in future
272 "vdu_id": name_schema
,
273 "primitive": name_schema
,
274 "primitive_params": {"type": "object"},
276 "required": ["primitive", "primitive_params"], # TODO add member_vnf_index
277 "additionalProperties": False
279 ns_scale
= { # TODO for the moment it is only VDU-scaling
280 "title": "ns scale input schema",
281 "$schema": "http://json-schema.org/draft-04/schema#",
284 "lcmOperationType": string_schema
,
285 "nsInstanceId": id_schema
,
286 "scaleType": {"enum": ["SCALE_VNF"]},
290 "vnfInstanceId": name_schema
,
291 "scaleVnfType": {"enum": ["SCALE_OUT", 'SCALE_IN']},
295 "scaling-group-descriptor": name_schema
,
296 "member-vnf-index": name_schema
,
297 "scaling-policy": name_schema
,
299 "required": ["scaling-group-descriptor", "member-vnf-index"],
300 "additionalProperties": False
303 "required": ["scaleVnfType", "scaleByStepData"], # vnfInstanceId
304 "additionalProperties": False
306 "scaleTime": time_schema
,
308 "required": ["scaleType", "scaleVnfData"],
309 "additionalProperties": False
313 schema_version
= {"type": "string", "enum": ["1.0"]}
314 vim_account_edit_schema
= {
315 "title": "vim_account edit input schema",
316 "$schema": "http://json-schema.org/draft-04/schema#",
320 "description": description_schema
,
321 "type": nameshort_schema
, # currently "openvim" or "openstack", can be enlarged with plugins
323 "datacenter": name_schema
,
324 "vim_url": description_schema
,
325 "vim_url_admin": description_schema
,
326 "vim_tenant": name_schema
,
327 "vim_tenant_name": name_schema
,
328 "vim_username": nameshort_schema
,
329 "vim_password": passwd_schema
,
330 "config": {"type": "object"}
332 "additionalProperties": False
334 schema_type
= {"type": "string"}
336 vim_account_new_schema
= {
337 "title": "vim_account creation input schema",
338 "$schema": "http://json-schema.org/draft-04/schema#",
341 "schema_version": schema_version
,
342 "schema_type": schema_type
,
344 "description": description_schema
,
346 "datacenter": name_schema
,
347 "vim_type": {"enum": ["openstack", "openvim", "vmware", "opennebula", "aws"]},
348 "vim_url": description_schema
,
349 # "vim_url_admin": description_schema,
350 # "vim_tenant": name_schema,
351 "vim_tenant_name": name_schema
,
352 "vim_user": nameshort_schema
,
353 "vim_password": passwd_schema
,
354 "config": {"type": "object"}
356 "required": ["name", "vim_url", "vim_type", "vim_user", "vim_password", "vim_tenant_name"],
357 "additionalProperties": False
363 "description": description_schema
,
367 "type": {"type": "string", "enum": ["opendaylight", "floodlight", "onos"]},
368 "version": {"type": "string", "minLength": 1, "maxLength": 12},
369 "user": nameshort_schema
,
370 "password": passwd_schema
373 "title": "sdn controller information schema",
374 "$schema": "http://json-schema.org/draft-04/schema#",
376 "properties": sdn_properties
,
377 "required": ["name", "port", 'ip', 'dpid', 'type'],
378 "additionalProperties": False
381 "title": "sdn controller update information schema",
382 "$schema": "http://json-schema.org/draft-04/schema#",
384 "properties": sdn_properties
,
385 # "required": ["name", "port", 'ip', 'dpid', 'type'],
386 "additionalProperties": False
388 sdn_port_mapping_schema
= {
389 "$schema": "http://json-schema.org/draft-04/schema#",
390 "title": "sdn port mapping information schema",
395 "compute_node": nameshort_schema
,
401 "pci": pci_extended_schema
,
402 "switch_port": nameshort_schema
,
403 "switch_mac": mac_schema
409 "required": ["compute_node", "ports"]
412 sdn_external_port_schema
= {
413 "$schema": "http://json-schema.org/draft-04/schema#",
414 "title": "External port information",
417 "port": {"type": "string", "minLength": 1, "maxLength": 60},
428 "name": nameshort_schema
,
430 "type": {"enum": ["overlay", 'underlay']},
431 "ip-address": ip_schema
,
432 # TODO, add user, password, ssh-key
433 "mac-address": mac_schema
,
434 "vim-network-name": nameshort_schema
, # interface is connected to one vim network, or switch port
435 # TODO "vim-network-id": nameshort_schema,
436 # # provide this in case SDN assist must deal with this interface
437 # "switch-dpid": dpid_Schema,
438 # "switch-port": nameshort_schema,
439 # "switch-mac": nameshort_schema,
440 # "switch-vlan": vlan_schema,
442 "required": ["name", "mgmt", "ip-address"],
443 "additionalProperties": False
446 "title": "pdu creation input schema",
447 "$schema": "http://json-schema.org/draft-04/schema#",
450 "name": nameshort_schema
,
451 "type": nameshort_schema
,
452 "description": description_schema
,
453 "shared": bool_schema
,
454 "vims": nameshort_list_schema
,
455 "vim_accounts": nameshort_list_schema
,
458 "items": pdu_interface
,
462 "required": ["name", "type", "interfaces"],
463 "additionalProperties": False
467 "title": "pdu edit input schema",
468 "$schema": "http://json-schema.org/draft-04/schema#",
471 "name": nameshort_schema
,
472 "type": nameshort_schema
,
473 "description": description_schema
,
474 "shared": bool_schema
,
475 "vims": {"oneOff": [array_edition_schema
, nameshort_list_schema
]},
476 "vim_accounts": {"oneOff": [array_edition_schema
, nameshort_list_schema
]},
477 "interfaces": {"oneOff": [
478 array_edition_schema
,
481 "items": pdu_interface
,
486 "additionalProperties": False,
492 "$schema": "http://json-schema.org/draft-04/schema#",
493 "title": "New user schema",
496 "username": nameshort_schema
,
497 "password": passwd_schema
,
498 "projects": nameshort_list_schema
,
500 "required": ["username", "password", "projects"],
501 "additionalProperties": False
504 "$schema": "http://json-schema.org/draft-04/schema#",
505 "title": "User edit schema for administrators",
508 "password": passwd_schema
,
511 nameshort_list_schema
,
517 "additionalProperties": False
521 project_new_schema
= {
522 "$schema": "http://json-schema.org/draft-04/schema#",
523 "title": "New project schema for administrators",
526 "name": nameshort_schema
,
527 "admin": bool_schema
,
529 "required": ["name"],
530 "additionalProperties": False
532 project_edit_schema
= {
533 "$schema": "http://json-schema.org/draft-04/schema#",
534 "title": "Project edit schema for administrators",
537 "admin": bool_schema
,
539 "additionalProperties": False,
545 nbi_new_input_schemas
= {
546 "users": user_new_schema
,
547 "projects": project_new_schema
,
548 "vim_accounts": vim_account_new_schema
,
549 "sdns": sdn_new_schema
,
550 "ns_instantiate": ns_instantiate
,
551 "ns_action": ns_action
,
552 "ns_scale": ns_scale
,
553 "pdus": pdu_new_schema
,
556 nbi_edit_input_schemas
= {
557 "users": user_edit_schema
,
558 "projects": project_edit_schema
,
559 "vim_accounts": vim_account_edit_schema
,
560 "sdns": sdn_edit_schema
,
561 "pdus": pdu_edit_schema
,
566 "title": "netslice action instantiate input schema",
567 "$schema": "http://json-schema.org/draft-04/schema#",
570 "lcmOperationType": string_schema
,
571 "nsiInstanceId": id_schema
,
572 "nsiName": name_schema
,
573 "nsiDescription": {"oneOf": [description_schema
, {"type": "null"}]},
574 "nstdId": string_schema
,
575 "vimAccountId": id_schema
,
576 "ssh_keys": {"type": "string"},
581 "items": ns_instantiate
584 "required": ["nsiName", "nstdId", "vimAccountId"],
585 "additionalProperties": False
597 class ValidationError(Exception):
598 def __init__(self
, message
, http_code
=HTTPStatus
.UNPROCESSABLE_ENTITY
):
599 self
.http_code
= http_code
600 Exception.__init
__(self
, message
)
603 def validate_input(indata
, schema_to_use
):
605 Validates input data against json schema
606 :param indata: user input data. Should be a dictionary
607 :param schema_to_use: jsonschema to test
608 :return: None if ok, raises ValidationError exception on error
612 js_v(indata
, schema_to_use
)
614 except js_e
.ValidationError
as e
:
616 error_pos
= "at '" + ":".join(map(str, e
.path
)) + "'"
619 raise ValidationError("Format error {} '{}' ".format(error_pos
, e
.message
))
620 except js_e
.SchemaError
:
621 raise ValidationError("Bad json schema {}".format(schema_to_use
), http_code
=HTTPStatus
.INTERNAL_SERVER_ERROR
)