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 # allows [] for wildcards. For that reason huge length limit is set
29 pci_extended_schema
= {"type": "string", "pattern": "^[0-9a-fA-F.:-\\[\\]]{12,40}$"}
30 http_schema
= {"type": "string", "pattern": "^https?://[^'\"=]+$"}
31 bandwidth_schema
= {"type": "string", "pattern": "^[0-9]+ *([MG]bps)?$"}
32 memory_schema
= {"type": "string", "pattern": "^[0-9]+ *([MG]i?[Bb])?$"}
33 integer0_schema
= {"type": "integer", "minimum": 0}
34 integer1_schema
= {"type": "integer", "minimum": 1}
35 path_schema
= {"type": "string", "pattern": "^(\\.){0,2}(/[^/\"':{}\\(\\)]+)+$"}
36 vlan_schema
= {"type": "integer", "minimum": 1, "maximum": 4095}
37 vlan1000_schema
= {"type": "integer", "minimum": 1000, "maximum": 4095}
38 mac_schema
= {"type": "string",
39 "pattern": "^[0-9a-fA-F][02468aceACE](:[0-9a-fA-F]{2}){5}$"} # must be unicast: LSB bit of MSB byte ==0
40 dpid_Schema
= {"type": "string", "pattern": "^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){7}$"}
41 # mac_schema={"type":"string", "pattern":"^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$"}
42 ip_schema
= {"type": "string",
43 "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]?)$"}
44 ip_prefix_schema
= {"type": "string",
45 "pattern": "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}"
46 "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/(30|[12]?[0-9])$"}
47 port_schema
= {"type": "integer", "minimum": 1, "maximum": 65534}
48 object_schema
= {"type": "object"}
49 schema_version_2
= {"type": "integer", "minimum": 2, "maximum": 2}
50 # schema_version_string={"type":"string","enum": ["0.1", "2", "0.2", "3", "0.3"]}
51 log_level_schema
= {"type": "string", "enum": ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]}
52 checksum_schema
= {"type": "string", "pattern": "^[0-9a-fA-F]{32}$"}
53 size_schema
= {"type": "integer", "minimum": 1, "maximum": 100}
54 array_edition_schema
= {
56 "patternProperties": {
59 "additionalProperties": False,
62 nameshort_list_schema
= {
65 "items": nameshort_schema
,
69 ns_instantiate_vdu
= {
70 "title": "ns action instantiate input schema for vdu",
71 "$schema": "http://json-schema.org/draft-04/schema#",
82 "vim-volume-id": name_schema
,
84 "required": ["name", "vim-volume-id"],
85 "additionalProperties": False
95 "ip-address": ip_schema
,
96 "mac-address": mac_schema
,
97 "floating-ip-required": bool_schema
,
100 "additionalProperties": False
105 "additionalProperties": False
108 ip_profile_dns_schema
= {
114 "address": ip_schema
,
116 "required": ["address"],
117 "additionalProperties": False
121 ip_profile_dhcp_schema
= {
124 "enabled": {"type": "boolean"},
125 "count": integer1_schema
,
126 "start-address": ip_schema
128 "additionalProperties": False,
131 ip_profile_schema
= {
132 "title": "ip profile validation schame",
133 "$schema": "http://json-schema.org/draft-04/schema#",
136 "ip-version": {"enum": ["ipv4", "ipv6"]},
137 "subnet-address": ip_prefix_schema
,
138 "gateway-address": ip_schema
,
139 "dns-server": ip_profile_dns_schema
,
140 "dhcp-params": ip_profile_dhcp_schema
,
144 ip_profile_update_schema
= {
145 "title": "ip profile validation schame",
146 "$schema": "http://json-schema.org/draft-04/schema#",
149 "ip-version": {"enum": ["ipv4", "ipv6"]},
150 "subnet-address": {"oneOf": [null_schema
, ip_prefix_schema
]},
151 "gateway-address": {"oneOf": [null_schema
, ip_schema
]},
152 "dns-server": {"oneOf": [null_schema
, ip_profile_dns_schema
]},
154 "dhcp-params": {"oneOf": [null_schema
, ip_profile_dhcp_schema
]},
156 "additionalProperties": False
159 ns_instantiate_internal_vld
= {
160 "title": "ns action instantiate input schema for vdu",
161 "$schema": "http://json-schema.org/draft-04/schema#",
165 "vim-network-name": name_schema
,
166 "ip-profile": ip_profile_update_schema
,
167 "internal-connection-point": {
173 "id-ref": name_schema
,
174 "ip-address": ip_schema
,
175 # "mac-address": mac_schema,
177 "required": ["id-ref"],
179 "additionalProperties": False
183 "required": ["name"],
185 "additionalProperties": False
189 "title": "ns action instantiate input schema",
190 "$schema": "http://json-schema.org/draft-04/schema#",
193 "lcmOperationType": string_schema
,
194 "nsInstanceId": id_schema
,
195 "nsName": name_schema
,
196 "nsDescription": {"oneOf": [description_schema
, {"type": "null"}]},
198 "vimAccountId": id_schema
,
199 "ssh_keys": {"type": "array", "items": {"type": "string"}},
201 "vduImage": name_schema
,
208 "member-vnf-index": name_schema
,
209 "vimAccountId": id_schema
,
213 "items": ns_instantiate_vdu
,
218 "items": ns_instantiate_internal_vld
221 "required": ["member-vnf-index"],
223 "additionalProperties": False
232 "name": string_schema
,
233 "vim-network-name": {"OneOf": [string_schema
, object_schema
]},
234 "ip-profile": object_schema
,
235 "vnfd-connection-point-ref": {
241 "member-vnf-index-ref": name_schema
,
242 "vnfd-connection-point-ref": name_schema
,
243 "ip-address": ip_schema
,
244 # "mac-address": mac_schema,
246 "required": ["member-vnf-index-ref", "vnfd-connection-point-ref"],
248 "additionalProperties": False
252 "required": ["name"],
253 "additionalProperties": False
257 "required": ["nsName", "nsdId", "vimAccountId"],
258 "additionalProperties": False
261 ns_action
= { # TODO for the moment it is only contemplated the vnfd primitive execution
262 "title": "ns action input schema",
263 "$schema": "http://json-schema.org/draft-04/schema#",
266 "lcmOperationType": string_schema
,
267 "nsInstanceId": id_schema
,
268 "member_vnf_index": name_schema
,
269 "vnf_member_index": name_schema
, # TODO for backward compatibility. To remove in future
270 "vdu_id": name_schema
,
271 "primitive": name_schema
,
272 "primitive_params": {"type": "object"},
274 "required": ["primitive", "primitive_params"], # TODO add member_vnf_index
275 "additionalProperties": False
277 ns_scale
= { # TODO for the moment it is only VDU-scaling
278 "title": "ns scale input schema",
279 "$schema": "http://json-schema.org/draft-04/schema#",
282 "lcmOperationType": string_schema
,
283 "nsInstanceId": id_schema
,
284 "scaleType": {"enum": ["SCALE_VNF"]},
288 "vnfInstanceId": name_schema
,
289 "scaleVnfType": {"enum": ["SCALE_OUT", 'SCALE_IN']},
293 "scaling-group-descriptor": name_schema
,
294 "member-vnf-index": name_schema
,
295 "scaling-policy": name_schema
,
297 "required": ["scaling-group-descriptor", "member-vnf-index"],
298 "additionalProperties": False
301 "required": ["scaleVnfType", "scaleByStepData"], # vnfInstanceId
302 "additionalProperties": False
304 "scaleTime": time_schema
,
306 "required": ["scaleType", "scaleVnfData"],
307 "additionalProperties": False
311 schema_version
= {"type": "string", "enum": ["1.0"]}
312 vim_account_edit_schema
= {
313 "title": "vim_account edit input schema",
314 "$schema": "http://json-schema.org/draft-04/schema#",
318 "description": description_schema
,
319 "type": nameshort_schema
, # currently "openvim" or "openstack", can be enlarged with plugins
321 "datacenter": name_schema
,
322 "vim_url": description_schema
,
323 "vim_url_admin": description_schema
,
324 "vim_tenant": name_schema
,
325 "vim_tenant_name": name_schema
,
326 "vim_username": nameshort_schema
,
327 "vim_password": passwd_schema
,
328 "config": {"type": "object"}
330 "additionalProperties": False
332 schema_type
= {"type": "string"}
334 vim_account_new_schema
= {
335 "title": "vim_account creation input schema",
336 "$schema": "http://json-schema.org/draft-04/schema#",
339 "schema_version": schema_version
,
340 "schema_type": schema_type
,
342 "description": description_schema
,
344 "datacenter": name_schema
,
345 "vim_type": {"enum": ["openstack", "openvim", "vmware", "opennebula", "aws"]},
346 "vim_url": description_schema
,
347 # "vim_url_admin": description_schema,
348 # "vim_tenant": name_schema,
349 "vim_tenant_name": name_schema
,
350 "vim_user": nameshort_schema
,
351 "vim_password": passwd_schema
,
352 "config": {"type": "object"}
354 "required": ["name", "vim_url", "vim_type", "vim_user", "vim_password", "vim_tenant_name"],
355 "additionalProperties": False
361 "description": description_schema
,
365 "type": {"type": "string", "enum": ["opendaylight", "floodlight", "onos"]},
366 "version": {"type": "string", "minLength": 1, "maxLength": 12},
367 "user": nameshort_schema
,
368 "password": passwd_schema
371 "title": "sdn controller information schema",
372 "$schema": "http://json-schema.org/draft-04/schema#",
374 "properties": sdn_properties
,
375 "required": ["name", "port", 'ip', 'dpid', 'type'],
376 "additionalProperties": False
379 "title": "sdn controller update information schema",
380 "$schema": "http://json-schema.org/draft-04/schema#",
382 "properties": sdn_properties
,
383 # "required": ["name", "port", 'ip', 'dpid', 'type'],
384 "additionalProperties": False
386 sdn_port_mapping_schema
= {
387 "$schema": "http://json-schema.org/draft-04/schema#",
388 "title": "sdn port mapping information schema",
393 "compute_node": nameshort_schema
,
399 "pci": pci_extended_schema
,
400 "switch_port": nameshort_schema
,
401 "switch_mac": mac_schema
407 "required": ["compute_node", "ports"]
410 sdn_external_port_schema
= {
411 "$schema": "http://json-schema.org/draft-04/schema#",
412 "title": "External port information",
415 "port": {"type": "string", "minLength": 1, "maxLength": 60},
426 "name": nameshort_schema
,
428 "type": {"enum": ["overlay", 'underlay']},
429 "ip_address": ip_schema
,
430 # TODO, add user, password, ssh-key
431 "mac_address": mac_schema
,
432 "vim_network_name": nameshort_schema
, # interface is connected to one vim network, or switch port
433 "vim_network_id": nameshort_schema
,
434 # provide this in case SDN assist must deal with this interface
435 "switch_dpid": dpid_Schema
,
436 "switch_port": nameshort_schema
,
437 "switch_mac": nameshort_schema
,
438 "switch_vlan": vlan_schema
,
440 "required": ["name", "mgmt", "ip_address"],
441 "additionalProperties": False
444 "title": "pdu creation input schema",
445 "$schema": "http://json-schema.org/draft-04/schema#",
448 "name": nameshort_schema
,
449 "type": nameshort_schema
,
450 "description": description_schema
,
451 "shared": bool_schema
,
452 "vims": nameshort_list_schema
,
453 "vim_accounts": nameshort_list_schema
,
456 "items": {"type": pdu_interface
},
460 "required": ["name", "type", "interfaces"],
461 "additionalProperties": False
465 "title": "pdu edit input schema",
466 "$schema": "http://json-schema.org/draft-04/schema#",
469 "name": nameshort_schema
,
470 "type": nameshort_schema
,
471 "description": description_schema
,
472 "shared": bool_schema
,
473 "vims": {"oneOff": [array_edition_schema
, nameshort_list_schema
]},
474 "vim_accounts": {"oneOff": [array_edition_schema
, nameshort_list_schema
]},
475 "interfaces": {"oneOff": [
476 array_edition_schema
,
479 "items": {"type": pdu_interface
},
484 "additionalProperties": False,
490 "$schema": "http://json-schema.org/draft-04/schema#",
491 "title": "New user schema",
494 "username": nameshort_schema
,
495 "password": passwd_schema
,
496 "projects": nameshort_list_schema
,
498 "required": ["username", "password", "projects"],
499 "additionalProperties": False
502 "$schema": "http://json-schema.org/draft-04/schema#",
503 "title": "User edit schema for administrators",
506 "password": passwd_schema
,
509 nameshort_list_schema
,
515 "additionalProperties": False
519 project_new_schema
= {
520 "$schema": "http://json-schema.org/draft-04/schema#",
521 "title": "New project schema for administrators",
524 "name": nameshort_schema
,
525 "admin": bool_schema
,
527 "required": ["name"],
528 "additionalProperties": False
530 project_edit_schema
= {
531 "$schema": "http://json-schema.org/draft-04/schema#",
532 "title": "Project edit schema for administrators",
535 "admin": bool_schema
,
537 "additionalProperties": False,
543 nbi_new_input_schemas
= {
544 "users": user_new_schema
,
545 "projects": project_new_schema
,
546 "vim_accounts": vim_account_new_schema
,
547 "sdns": sdn_new_schema
,
548 "ns_instantiate": ns_instantiate
,
549 "ns_action": ns_action
,
550 "ns_scale": ns_scale
,
551 "pdus": pdu_new_schema
,
554 nbi_edit_input_schemas
= {
555 "users": user_edit_schema
,
556 "projects": project_edit_schema
,
557 "vim_accounts": vim_account_edit_schema
,
558 "sdns": sdn_edit_schema
,
559 "pdus": pdu_edit_schema
,
563 class ValidationError(Exception):
567 def validate_input(indata
, schema_to_use
):
569 Validates input data against json schema
570 :param indata: user input data. Should be a dictionary
571 :param schema_to_use: jsonschema to test
572 :return: None if ok, raises ValidationError exception on error
576 js_v(indata
, schema_to_use
)
578 except js_e
.ValidationError
as e
:
580 error_pos
= "at '" + ":".join(map(str, e
.path
)) + "'"
583 raise ValidationError("Format error {} '{}' ".format(error_pos
, e
.message
))