1 # -*- coding: utf-8 -*-
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 from jsonschema
import validate
as js_v
, exceptions
as js_e
17 from http
import HTTPStatus
18 from copy
import deepcopy
20 __author__
= "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
22 version_date
= "Mar 2018"
25 Validator of input data using JSON schemas for those items that not contains an OSM yang information model
29 patern_name
= "^[ -~]+$"
30 shortname_schema
= {"type": "string", "minLength": 1, "maxLength": 60, "pattern": "^[^,;()\\.\\$'\"]+$"}
31 passwd_schema
= {"type": "string", "minLength": 1, "maxLength": 60}
32 name_schema
= {"type": "string", "minLength": 1, "maxLength": 255, "pattern": "^[^,;()'\"]+$"}
33 string_schema
= {"type": "string", "minLength": 1, "maxLength": 255}
34 xml_text_schema
= {"type": "string", "minLength": 1, "maxLength": 1000, "pattern": "^[^']+$"}
35 description_schema
= {"type": ["string", "null"], "maxLength": 255, "pattern": "^[^'\"]+$"}
36 id_schema_fake
= {"type": "string", "minLength": 2, "maxLength": 36}
37 bool_schema
= {"type": "boolean"}
38 null_schema
= {"type": "null"}
39 # "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}$"
40 id_schema
= {"type": "string", "pattern": "^[a-fA-F0-9]{8}(-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}$"}
41 time_schema
= {"type": "string", "pattern": "^[0-9]{4}-[0-1][0-9]-[0-3][0-9]T[0-2][0-9]([0-5]:){2}"}
42 pci_schema
= {"type": "string", "pattern": "^[0-9a-fA-F]{4}(:[0-9a-fA-F]{2}){2}\\.[0-9a-fA-F]$"}
43 # allows [] for wildcards. For that reason huge length limit is set
44 pci_extended_schema
= {"type": "string", "pattern": "^[0-9a-fA-F.:-\\[\\]]{12,40}$"}
45 http_schema
= {"type": "string", "pattern": "^https?://[^'\"=]+$"}
46 bandwidth_schema
= {"type": "string", "pattern": "^[0-9]+ *([MG]bps)?$"}
47 memory_schema
= {"type": "string", "pattern": "^[0-9]+ *([MG]i?[Bb])?$"}
48 integer0_schema
= {"type": "integer", "minimum": 0}
49 integer1_schema
= {"type": "integer", "minimum": 1}
50 path_schema
= {"type": "string", "pattern": "^(\\.){0,2}(/[^/\"':{}\\(\\)]+)+$"}
51 vlan_schema
= {"type": "integer", "minimum": 1, "maximum": 4095}
52 vlan1000_schema
= {"type": "integer", "minimum": 1000, "maximum": 4095}
53 mac_schema
= {"type": "string",
54 "pattern": "^[0-9a-fA-F][02468aceACE](:[0-9a-fA-F]{2}){5}$"} # must be unicast: LSB bit of MSB byte ==0
55 dpid_Schema
= {"type": "string", "pattern": "^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){7}$"}
56 # mac_schema={"type":"string", "pattern":"^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$"}
57 ip_schema
= {"type": "string",
58 "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]?)$"}
59 ip_prefix_schema
= {"type": "string",
60 "pattern": "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}"
61 "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/(30|[12]?[0-9])$"}
62 port_schema
= {"type": "integer", "minimum": 1, "maximum": 65534}
63 object_schema
= {"type": "object"}
64 schema_version_2
= {"type": "integer", "minimum": 2, "maximum": 2}
65 # schema_version_string={"type":"string","enum": ["0.1", "2", "0.2", "3", "0.3"]}
66 log_level_schema
= {"type": "string", "enum": ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]}
67 checksum_schema
= {"type": "string", "pattern": "^[0-9a-fA-F]{32}$"}
68 size_schema
= {"type": "integer", "minimum": 1, "maximum": 100}
69 array_edition_schema
= {
71 "patternProperties": {
74 "additionalProperties": False,
77 nameshort_list_schema
= {
80 "items": shortname_schema
,
84 ns_instantiate_vdu
= {
85 "title": "ns action instantiate input schema for vdu",
86 "$schema": "http://json-schema.org/draft-04/schema#",
97 "vim-volume-id": name_schema
,
99 "required": ["name", "vim-volume-id"],
100 "additionalProperties": False
110 "ip-address": ip_schema
,
111 "mac-address": mac_schema
,
112 "floating-ip-required": bool_schema
,
114 "required": ["name"],
115 "additionalProperties": False
120 "additionalProperties": False
123 ip_profile_dns_schema
= {
129 "address": ip_schema
,
131 "required": ["address"],
132 "additionalProperties": False
136 ip_profile_dhcp_schema
= {
139 "enabled": {"type": "boolean"},
140 "count": integer1_schema
,
141 "start-address": ip_schema
143 "additionalProperties": False,
146 ip_profile_schema
= {
147 "title": "ip profile validation schame",
148 "$schema": "http://json-schema.org/draft-04/schema#",
151 "ip-version": {"enum": ["ipv4", "ipv6"]},
152 "subnet-address": ip_prefix_schema
,
153 "gateway-address": ip_schema
,
154 "dns-server": ip_profile_dns_schema
,
155 "dhcp-params": ip_profile_dhcp_schema
,
159 ip_profile_update_schema
= {
160 "title": "ip profile validation schame",
161 "$schema": "http://json-schema.org/draft-04/schema#",
164 "ip-version": {"enum": ["ipv4", "ipv6"]},
165 "subnet-address": {"oneOf": [null_schema
, ip_prefix_schema
]},
166 "gateway-address": {"oneOf": [null_schema
, ip_schema
]},
167 "dns-server": {"oneOf": [null_schema
, ip_profile_dns_schema
]},
169 "dhcp-params": {"oneOf": [null_schema
, ip_profile_dhcp_schema
]},
171 "additionalProperties": False
174 ns_instantiate_internal_vld
= {
175 "title": "ns action instantiate input schema for vdu",
176 "$schema": "http://json-schema.org/draft-04/schema#",
180 "vim-network-name": name_schema
,
181 "vim-network-id": name_schema
,
182 "ip-profile": ip_profile_update_schema
,
183 "internal-connection-point": {
189 "id-ref": name_schema
,
190 "ip-address": ip_schema
,
191 # "mac-address": mac_schema,
193 "required": ["id-ref"],
195 "additionalProperties": False
199 "required": ["name"],
201 "additionalProperties": False
204 additional_params_for_vnf
= {
209 "member-vnf-index": name_schema
,
210 "additionalParams": object_schema
,
212 "required": ["member-vnf-index", "additionalParams"],
213 "additionalProperties": False
219 "title": "ns action instantiate input schema",
220 "$schema": "http://json-schema.org/draft-04/schema#",
223 "lcmOperationType": string_schema
,
224 "nsInstanceId": id_schema
,
225 "netsliceInstanceId": id_schema
,
226 "nsName": name_schema
,
227 "nsDescription": {"oneOf": [description_schema
, {"type": "null"}]},
229 "vimAccountId": id_schema
,
230 "additionalParamsForNs": object_schema
,
231 "additionalParamsForVnf": additional_params_for_vnf
,
232 "ssh_keys": {"type": "array", "items": {"type": "string"}},
234 "vduImage": name_schema
,
241 "member-vnf-index": name_schema
,
242 "vimAccountId": id_schema
,
246 "items": ns_instantiate_vdu
,
251 "items": ns_instantiate_internal_vld
254 "required": ["member-vnf-index"],
256 "additionalProperties": False
265 "name": string_schema
,
266 "vim-network-name": {"OneOf": [string_schema
, object_schema
]},
267 "vim-network-id": {"OneOf": [string_schema
, object_schema
]},
268 "ip-profile": object_schema
,
269 "vnfd-connection-point-ref": {
275 "member-vnf-index-ref": name_schema
,
276 "vnfd-connection-point-ref": name_schema
,
277 "ip-address": ip_schema
,
278 # "mac-address": mac_schema,
280 "required": ["member-vnf-index-ref", "vnfd-connection-point-ref"],
282 "additionalProperties": False
286 "required": ["name"],
287 "additionalProperties": False
291 "required": ["nsName", "nsdId", "vimAccountId"],
292 "additionalProperties": False
295 ns_action
= { # TODO for the moment it is only contemplated the vnfd primitive execution
296 "title": "ns action input schema",
297 "$schema": "http://json-schema.org/draft-04/schema#",
300 "lcmOperationType": string_schema
,
301 "nsInstanceId": id_schema
,
302 "member_vnf_index": name_schema
,
303 "vnf_member_index": name_schema
, # TODO for backward compatibility. To remove in future
304 "vdu_id": name_schema
,
305 "primitive": name_schema
,
306 "primitive_params": {"type": "object"},
308 "required": ["primitive", "primitive_params"], # TODO add member_vnf_index
309 "additionalProperties": False
311 ns_scale
= { # TODO for the moment it is only VDU-scaling
312 "title": "ns scale input schema",
313 "$schema": "http://json-schema.org/draft-04/schema#",
316 "lcmOperationType": string_schema
,
317 "nsInstanceId": id_schema
,
318 "scaleType": {"enum": ["SCALE_VNF"]},
322 "vnfInstanceId": name_schema
,
323 "scaleVnfType": {"enum": ["SCALE_OUT", 'SCALE_IN']},
327 "scaling-group-descriptor": name_schema
,
328 "member-vnf-index": name_schema
,
329 "scaling-policy": name_schema
,
331 "required": ["scaling-group-descriptor", "member-vnf-index"],
332 "additionalProperties": False
335 "required": ["scaleVnfType", "scaleByStepData"], # vnfInstanceId
336 "additionalProperties": False
338 "scaleTime": time_schema
,
340 "required": ["scaleType", "scaleVnfData"],
341 "additionalProperties": False
345 schema_version
= {"type": "string", "enum": ["1.0"]}
346 schema_type
= {"type": "string"}
347 vim_account_edit_schema
= {
348 "title": "vim_account edit input schema",
349 "$schema": "http://json-schema.org/draft-04/schema#",
353 "description": description_schema
,
354 "type": shortname_schema
,
356 "datacenter": name_schema
,
357 "vim_url": description_schema
,
358 "vim_url_admin": description_schema
,
359 "vim_tenant": name_schema
,
360 "vim_tenant_name": name_schema
,
361 "vim_username": shortname_schema
,
362 "vim_password": passwd_schema
,
363 "config": {"type": "object"}
365 "additionalProperties": False
368 vim_account_new_schema
= {
369 "title": "vim_account creation input schema",
370 "$schema": "http://json-schema.org/draft-04/schema#",
373 "schema_version": schema_version
,
374 "schema_type": schema_type
,
376 "description": description_schema
,
378 "datacenter": name_schema
,
379 "vim_type": {"enum": ["openstack", "openvim", "vmware", "opennebula", "aws"]},
380 "vim_url": description_schema
,
381 # "vim_url_admin": description_schema,
382 # "vim_tenant": name_schema,
383 "vim_tenant_name": name_schema
,
384 "vim_user": shortname_schema
,
385 "vim_password": passwd_schema
,
386 "config": {"type": "object"}
388 "required": ["name", "vim_url", "vim_type", "vim_user", "vim_password", "vim_tenant_name"],
389 "additionalProperties": False
392 wim_account_edit_schema
= {
393 "title": "wim_account edit input schema",
394 "$schema": "http://json-schema.org/draft-04/schema#",
398 "description": description_schema
,
399 "type": shortname_schema
,
401 "wim_url": description_schema
,
402 "user": shortname_schema
,
403 "password": passwd_schema
,
404 "config": {"type": "object"}
406 "additionalProperties": False
409 wim_account_new_schema
= {
410 "title": "wim_account creation input schema",
411 "$schema": "http://json-schema.org/draft-04/schema#",
414 "schema_version": schema_version
,
415 "schema_type": schema_type
,
417 "description": description_schema
,
419 "wim_type": {"enum": ["tapi", "onos", "odl", "dynpac"]},
420 "wim_url": description_schema
,
421 "user": shortname_schema
,
422 "password": passwd_schema
,
423 "config": {"type": "object"}
425 "required": ["name", "wim_url", "wim_type"],
426 "additionalProperties": False
431 "description": description_schema
,
435 "type": {"type": "string", "enum": ["opendaylight", "floodlight", "onos"]},
436 "version": {"type": "string", "minLength": 1, "maxLength": 12},
437 "user": shortname_schema
,
438 "password": passwd_schema
441 "title": "sdn controller information schema",
442 "$schema": "http://json-schema.org/draft-04/schema#",
444 "properties": sdn_properties
,
445 "required": ["name", "port", 'ip', 'dpid', 'type'],
446 "additionalProperties": False
449 "title": "sdn controller update information schema",
450 "$schema": "http://json-schema.org/draft-04/schema#",
452 "properties": sdn_properties
,
453 # "required": ["name", "port", 'ip', 'dpid', 'type'],
454 "additionalProperties": False
456 sdn_port_mapping_schema
= {
457 "$schema": "http://json-schema.org/draft-04/schema#",
458 "title": "sdn port mapping information schema",
463 "compute_node": shortname_schema
,
469 "pci": pci_extended_schema
,
470 "switch_port": shortname_schema
,
471 "switch_mac": mac_schema
477 "required": ["compute_node", "ports"]
480 sdn_external_port_schema
= {
481 "$schema": "http://json-schema.org/draft-04/schema#",
482 "title": "External port information",
485 "port": {"type": "string", "minLength": 1, "maxLength": 60},
496 "name": shortname_schema
,
498 "type": {"enum": ["overlay", 'underlay']},
499 "ip-address": ip_schema
,
500 # TODO, add user, password, ssh-key
501 "mac-address": mac_schema
,
502 "vim-network-name": shortname_schema
, # interface is connected to one vim network, or switch port
503 "vim-network-id": shortname_schema
,
504 # # provide this in case SDN assist must deal with this interface
505 # "switch-dpid": dpid_Schema,
506 # "switch-port": shortname_schema,
507 # "switch-mac": shortname_schema,
508 # "switch-vlan": vlan_schema,
510 "required": ["name", "mgmt", "ip-address"],
511 "additionalProperties": False
514 "title": "pdu creation input schema",
515 "$schema": "http://json-schema.org/draft-04/schema#",
518 "name": shortname_schema
,
519 "type": shortname_schema
,
520 "description": description_schema
,
521 "shared": bool_schema
,
522 "vims": nameshort_list_schema
,
523 "vim_accounts": nameshort_list_schema
,
526 "items": pdu_interface
,
530 "required": ["name", "type", "interfaces"],
531 "additionalProperties": False
535 "title": "pdu edit input schema",
536 "$schema": "http://json-schema.org/draft-04/schema#",
539 "name": shortname_schema
,
540 "type": shortname_schema
,
541 "description": description_schema
,
542 "shared": bool_schema
,
543 "vims": {"oneOf": [array_edition_schema
, nameshort_list_schema
]},
544 "vim_accounts": {"oneOf": [array_edition_schema
, nameshort_list_schema
]},
545 "interfaces": {"oneOf": [
546 array_edition_schema
,
549 "items": pdu_interface
,
554 "additionalProperties": False,
560 "$schema": "http://json-schema.org/draft-04/schema#",
561 "title": "New user schema",
564 "username": shortname_schema
,
565 "password": passwd_schema
,
566 "projects": nameshort_list_schema
,
568 "required": ["username", "password", "projects"],
569 "additionalProperties": False
572 "$schema": "http://json-schema.org/draft-04/schema#",
573 "title": "User edit schema for administrators",
576 "password": passwd_schema
,
579 nameshort_list_schema
,
585 "additionalProperties": False
589 project_new_schema
= {
590 "$schema": "http://json-schema.org/draft-04/schema#",
591 "title": "New project schema for administrators",
594 "name": shortname_schema
,
595 "admin": bool_schema
,
597 "required": ["name"],
598 "additionalProperties": False
600 project_edit_schema
= {
601 "$schema": "http://json-schema.org/draft-04/schema#",
602 "title": "Project edit schema for administrators",
605 "admin": bool_schema
,
607 "additionalProperties": False,
613 nbi_new_input_schemas
= {
614 "users": user_new_schema
,
615 "projects": project_new_schema
,
616 "vim_accounts": vim_account_new_schema
,
617 "sdns": sdn_new_schema
,
618 "ns_instantiate": ns_instantiate
,
619 "ns_action": ns_action
,
620 "ns_scale": ns_scale
,
621 "pdus": pdu_new_schema
,
624 nbi_edit_input_schemas
= {
625 "users": user_edit_schema
,
626 "projects": project_edit_schema
,
627 "vim_accounts": vim_account_edit_schema
,
628 "sdns": sdn_edit_schema
,
629 "pdus": pdu_edit_schema
,
633 nsi_slice_instantiate
= deepcopy(ns_instantiate
)
634 nsi_slice_instantiate
["title"] = "netslice subnet instantiation params input schema"
635 nsi_slice_instantiate
["properties"]["id"] = name_schema
636 nsi_slice_instantiate
["properties"]["additionalParamsForNsi"] = object_schema
637 nsi_slice_instantiate
["properties"]["additionalParamsForSubnet"] = {
643 "additionalParamsForNs": object_schema
,
644 "additionalParamsForVnf": additional_params_for_vnf
647 "additionalProperties": False
650 del nsi_slice_instantiate
["required"]
651 del nsi_slice_instantiate
["properties"]["additionalParamsForNs"]
652 del nsi_slice_instantiate
["properties"]["additionalParamsForVnf"]
654 nsi_vld_instantiate
= {
655 "title": "netslice vld instantiation params input schema",
656 "$schema": "http://json-schema.org/draft-04/schema#",
659 "name": string_schema
,
660 "vim-network-name": {"OneOf": [string_schema
, object_schema
]},
661 "vim-network-id": {"OneOf": [string_schema
, object_schema
]},
662 "ip-profile": object_schema
,
664 "required": ["name"],
665 "additionalProperties": False
669 "title": "netslice action instantiate input schema",
670 "$schema": "http://json-schema.org/draft-04/schema#",
673 "lcmOperationType": string_schema
,
674 "nsiInstanceId": id_schema
,
675 "nsiName": name_schema
,
676 "nsiDescription": {"oneOf": [description_schema
, {"type": "null"}]},
677 "nstId": string_schema
,
678 "vimAccountId": id_schema
,
679 "ssh_keys": {"type": "string"},
684 "items": nsi_slice_instantiate
689 "items": nsi_vld_instantiate
692 "required": ["nsiName", "nstId", "vimAccountId"],
693 "additionalProperties": False
705 class ValidationError(Exception):
706 def __init__(self
, message
, http_code
=HTTPStatus
.UNPROCESSABLE_ENTITY
):
707 self
.http_code
= http_code
708 Exception.__init
__(self
, message
)
711 def validate_input(indata
, schema_to_use
):
713 Validates input data against json schema
714 :param indata: user input data. Should be a dictionary
715 :param schema_to_use: jsonschema to test
716 :return: None if ok, raises ValidationError exception on error
720 js_v(indata
, schema_to_use
)
722 except js_e
.ValidationError
as e
:
724 error_pos
= "at '" + ":".join(map(str, e
.path
)) + "'"
727 raise ValidationError("Format error {} '{}' ".format(error_pos
, e
.message
))
728 except js_e
.SchemaError
:
729 raise ValidationError("Bad json schema {}".format(schema_to_use
), http_code
=HTTPStatus
.INTERNAL_SERVER_ERROR
)