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
19 from uuid
import UUID
# To test for valid UUID
21 __author__
= "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
23 version_date
= "Mar 2018"
26 Validator of input data using JSON schemas for those items that not contains an OSM yang information model
30 patern_name
= "^[ -~]+$"
31 shortname_schema
= {"type": "string", "minLength": 1, "maxLength": 60, "pattern": "^[^,;()\\.\\$'\"]+$"}
32 passwd_schema
= {"type": "string", "minLength": 1, "maxLength": 60}
33 name_schema
= {"type": "string", "minLength": 1, "maxLength": 255, "pattern": "^[^,;()'\"]+$"}
34 string_schema
= {"type": "string", "minLength": 1, "maxLength": 255}
35 xml_text_schema
= {"type": "string", "minLength": 1, "maxLength": 1000, "pattern": "^[^']+$"}
36 description_schema
= {"type": ["string", "null"], "maxLength": 255, "pattern": "^[^'\"]+$"}
37 id_schema_fake
= {"type": "string", "minLength": 2, "maxLength": 36}
38 bool_schema
= {"type": "boolean"}
39 null_schema
= {"type": "null"}
40 # "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}$"
41 id_schema
= {"type": "string", "pattern": "^[a-fA-F0-9]{8}(-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}$"}
42 time_schema
= {"type": "string", "pattern": "^[0-9]{4}-[0-1][0-9]-[0-3][0-9]T[0-2][0-9]([0-5]:){2}"}
43 pci_schema
= {"type": "string", "pattern": "^[0-9a-fA-F]{4}(:[0-9a-fA-F]{2}){2}\\.[0-9a-fA-F]$"}
44 # allows [] for wildcards. For that reason huge length limit is set
45 pci_extended_schema
= {"type": "string", "pattern": "^[0-9a-fA-F.:-\\[\\]]{12,40}$"}
46 http_schema
= {"type": "string", "pattern": "^https?://[^'\"=]+$"}
47 bandwidth_schema
= {"type": "string", "pattern": "^[0-9]+ *([MG]bps)?$"}
48 memory_schema
= {"type": "string", "pattern": "^[0-9]+ *([MG]i?[Bb])?$"}
49 integer0_schema
= {"type": "integer", "minimum": 0}
50 integer1_schema
= {"type": "integer", "minimum": 1}
51 path_schema
= {"type": "string", "pattern": "^(\\.){0,2}(/[^/\"':{}\\(\\)]+)+$"}
52 vlan_schema
= {"type": "integer", "minimum": 1, "maximum": 4095}
53 vlan1000_schema
= {"type": "integer", "minimum": 1000, "maximum": 4095}
54 mac_schema
= {"type": "string",
55 "pattern": "^[0-9a-fA-F][02468aceACE](:[0-9a-fA-F]{2}){5}$"} # must be unicast: LSB bit of MSB byte ==0
56 dpid_Schema
= {"type": "string", "pattern": "^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){7}$"}
57 # mac_schema={"type":"string", "pattern":"^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$"}
58 ip_schema
= {"type": "string",
59 "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]?)$"}
60 ip_prefix_schema
= {"type": "string",
61 "pattern": "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}"
62 "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/(30|[12]?[0-9])$"}
63 port_schema
= {"type": "integer", "minimum": 1, "maximum": 65534}
64 object_schema
= {"type": "object"}
65 schema_version_2
= {"type": "integer", "minimum": 2, "maximum": 2}
66 # schema_version_string={"type":"string","enum": ["0.1", "2", "0.2", "3", "0.3"]}
67 log_level_schema
= {"type": "string", "enum": ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]}
68 checksum_schema
= {"type": "string", "pattern": "^[0-9a-fA-F]{32}$"}
69 size_schema
= {"type": "integer", "minimum": 1, "maximum": 100}
70 array_edition_schema
= {
72 "patternProperties": {
75 "additionalProperties": False,
78 nameshort_list_schema
= {
81 "items": shortname_schema
,
85 ns_instantiate_vdu
= {
86 "title": "ns action instantiate input schema for vdu",
87 "$schema": "http://json-schema.org/draft-04/schema#",
98 "vim-volume-id": name_schema
,
100 "required": ["name", "vim-volume-id"],
101 "additionalProperties": False
111 "ip-address": ip_schema
,
112 "mac-address": mac_schema
,
113 "floating-ip-required": bool_schema
,
115 "required": ["name"],
116 "additionalProperties": False
121 "additionalProperties": False
124 ip_profile_dns_schema
= {
130 "address": ip_schema
,
132 "required": ["address"],
133 "additionalProperties": False
137 ip_profile_dhcp_schema
= {
140 "enabled": {"type": "boolean"},
141 "count": integer1_schema
,
142 "start-address": ip_schema
144 "additionalProperties": False,
147 ip_profile_schema
= {
148 "title": "ip profile validation schame",
149 "$schema": "http://json-schema.org/draft-04/schema#",
152 "ip-version": {"enum": ["ipv4", "ipv6"]},
153 "subnet-address": ip_prefix_schema
,
154 "gateway-address": ip_schema
,
155 "dns-server": ip_profile_dns_schema
,
156 "dhcp-params": ip_profile_dhcp_schema
,
160 ip_profile_update_schema
= {
161 "title": "ip profile validation schame",
162 "$schema": "http://json-schema.org/draft-04/schema#",
165 "ip-version": {"enum": ["ipv4", "ipv6"]},
166 "subnet-address": {"oneOf": [null_schema
, ip_prefix_schema
]},
167 "gateway-address": {"oneOf": [null_schema
, ip_schema
]},
168 "dns-server": {"oneOf": [null_schema
, ip_profile_dns_schema
]},
170 "dhcp-params": {"oneOf": [null_schema
, ip_profile_dhcp_schema
]},
172 "additionalProperties": False
175 ns_instantiate_internal_vld
= {
176 "title": "ns action instantiate input schema for vdu",
177 "$schema": "http://json-schema.org/draft-04/schema#",
181 "vim-network-name": name_schema
,
182 "vim-network-id": name_schema
,
183 "ip-profile": ip_profile_update_schema
,
184 "internal-connection-point": {
190 "id-ref": name_schema
,
191 "ip-address": ip_schema
,
192 # "mac-address": mac_schema,
194 "required": ["id-ref"],
196 "additionalProperties": False
200 "required": ["name"],
202 "additionalProperties": False
205 additional_params_for_vnf
= {
210 "member-vnf-index": name_schema
,
211 "additionalParams": object_schema
,
213 "required": ["member-vnf-index", "additionalParams"],
214 "additionalProperties": False
220 "title": "ns action instantiate input schema",
221 "$schema": "http://json-schema.org/draft-04/schema#",
224 "lcmOperationType": string_schema
,
225 "nsInstanceId": id_schema
,
226 "netsliceInstanceId": id_schema
,
227 "nsName": name_schema
,
228 "nsDescription": {"oneOf": [description_schema
, null_schema
]},
230 "vimAccountId": id_schema
,
231 "wimAccountId": {"OneOf": [id_schema
, bool_schema
, null_schema
]},
232 "additionalParamsForNs": object_schema
,
233 "additionalParamsForVnf": additional_params_for_vnf
,
234 "ssh_keys": {"type": "array", "items": {"type": "string"}},
236 "vduImage": name_schema
,
243 "member-vnf-index": name_schema
,
244 "vimAccountId": id_schema
,
248 "items": ns_instantiate_vdu
,
253 "items": ns_instantiate_internal_vld
256 "required": ["member-vnf-index"],
258 "additionalProperties": False
267 "name": string_schema
,
268 "vim-network-name": {"OneOf": [string_schema
, object_schema
]},
269 "vim-network-id": {"OneOf": [string_schema
, object_schema
]},
270 "ns-net": object_schema
,
271 "wimAccountId": {"OneOf": [id_schema
, bool_schema
, null_schema
]},
272 "ip-profile": object_schema
,
273 "vnfd-connection-point-ref": {
279 "member-vnf-index-ref": name_schema
,
280 "vnfd-connection-point-ref": name_schema
,
281 "ip-address": ip_schema
,
282 # "mac-address": mac_schema,
284 "required": ["member-vnf-index-ref", "vnfd-connection-point-ref"],
286 "additionalProperties": False
290 "required": ["name"],
291 "additionalProperties": False
295 "required": ["nsName", "nsdId", "vimAccountId"],
296 "additionalProperties": False
299 ns_action
= { # TODO for the moment it is only contemplated the vnfd primitive execution
300 "title": "ns action input schema",
301 "$schema": "http://json-schema.org/draft-04/schema#",
304 "lcmOperationType": string_schema
,
305 "nsInstanceId": id_schema
,
306 "member_vnf_index": name_schema
,
307 "vnf_member_index": name_schema
, # TODO for backward compatibility. To remove in future
308 "vdu_id": name_schema
,
309 "vdu_count_index": integer0_schema
,
310 "primitive": name_schema
,
311 "primitive_params": {"type": "object"},
313 "required": ["primitive", "primitive_params"], # TODO add member_vnf_index
314 "additionalProperties": False
316 ns_scale
= { # TODO for the moment it is only VDU-scaling
317 "title": "ns scale input schema",
318 "$schema": "http://json-schema.org/draft-04/schema#",
321 "lcmOperationType": string_schema
,
322 "nsInstanceId": id_schema
,
323 "scaleType": {"enum": ["SCALE_VNF"]},
327 "vnfInstanceId": name_schema
,
328 "scaleVnfType": {"enum": ["SCALE_OUT", 'SCALE_IN']},
332 "scaling-group-descriptor": name_schema
,
333 "member-vnf-index": name_schema
,
334 "scaling-policy": name_schema
,
336 "required": ["scaling-group-descriptor", "member-vnf-index"],
337 "additionalProperties": False
340 "required": ["scaleVnfType", "scaleByStepData"], # vnfInstanceId
341 "additionalProperties": False
343 "scaleTime": time_schema
,
345 "required": ["scaleType", "scaleVnfData"],
346 "additionalProperties": False
350 schema_version
= {"type": "string", "enum": ["1.0"]}
351 schema_type
= {"type": "string"}
352 vim_account_edit_schema
= {
353 "title": "vim_account edit input schema",
354 "$schema": "http://json-schema.org/draft-04/schema#",
358 "description": description_schema
,
359 "type": shortname_schema
,
361 "datacenter": name_schema
,
362 "vim_url": description_schema
,
363 "vim_url_admin": description_schema
,
364 "vim_tenant": name_schema
,
365 "vim_tenant_name": name_schema
,
366 "vim_username": shortname_schema
,
367 "vim_password": passwd_schema
,
368 "config": {"type": "object"}
370 "additionalProperties": False
373 vim_account_new_schema
= {
374 "title": "vim_account creation input schema",
375 "$schema": "http://json-schema.org/draft-04/schema#",
378 "schema_version": schema_version
,
379 "schema_type": schema_type
,
381 "description": description_schema
,
383 "datacenter": name_schema
,
384 "vim_type": {"enum": ["openstack", "openvim", "vmware", "opennebula", "aws", "azure", "fos"]},
385 "vim_url": description_schema
,
386 # "vim_url_admin": description_schema,
387 # "vim_tenant": name_schema,
388 "vim_tenant_name": name_schema
,
389 "vim_user": shortname_schema
,
390 "vim_password": passwd_schema
,
391 "config": {"type": "object"}
393 "required": ["name", "vim_url", "vim_type", "vim_user", "vim_password", "vim_tenant_name"],
394 "additionalProperties": False
397 wim_account_edit_schema
= {
398 "title": "wim_account edit input schema",
399 "$schema": "http://json-schema.org/draft-04/schema#",
403 "description": description_schema
,
404 "type": shortname_schema
,
406 "wim_url": description_schema
,
407 "user": shortname_schema
,
408 "password": passwd_schema
,
409 "config": {"type": "object"}
411 "additionalProperties": False
414 wim_account_new_schema
= {
415 "title": "wim_account creation input schema",
416 "$schema": "http://json-schema.org/draft-04/schema#",
419 "schema_version": schema_version
,
420 "schema_type": schema_type
,
422 "description": description_schema
,
424 "wim_type": {"enum": ["tapi", "onos", "odl", "dynpac", "fake"]},
425 "wim_url": description_schema
,
426 "user": shortname_schema
,
427 "password": passwd_schema
,
430 "patternProperties": {
431 ".": {"not": {"type": "null"}}
435 "required": ["name", "wim_url", "wim_type"],
436 "additionalProperties": False
441 "description": description_schema
,
445 "type": {"type": "string", "enum": ["opendaylight", "floodlight", "onos"]},
446 "version": {"type": "string", "minLength": 1, "maxLength": 12},
447 "user": shortname_schema
,
448 "password": passwd_schema
451 "title": "sdn controller information schema",
452 "$schema": "http://json-schema.org/draft-04/schema#",
454 "properties": sdn_properties
,
455 "required": ["name", "port", 'ip', 'dpid', 'type'],
456 "additionalProperties": False
459 "title": "sdn controller update information schema",
460 "$schema": "http://json-schema.org/draft-04/schema#",
462 "properties": sdn_properties
,
463 # "required": ["name", "port", 'ip', 'dpid', 'type'],
464 "additionalProperties": False
466 sdn_port_mapping_schema
= {
467 "$schema": "http://json-schema.org/draft-04/schema#",
468 "title": "sdn port mapping information schema",
473 "compute_node": shortname_schema
,
479 "pci": pci_extended_schema
,
480 "switch_port": shortname_schema
,
481 "switch_mac": mac_schema
487 "required": ["compute_node", "ports"]
490 sdn_external_port_schema
= {
491 "$schema": "http://json-schema.org/draft-04/schema#",
492 "title": "External port information",
495 "port": {"type": "string", "minLength": 1, "maxLength": 60},
506 "name": shortname_schema
,
508 "type": {"enum": ["overlay", 'underlay']},
509 "ip-address": ip_schema
,
510 # TODO, add user, password, ssh-key
511 "mac-address": mac_schema
,
512 "vim-network-name": shortname_schema
, # interface is connected to one vim network, or switch port
513 "vim-network-id": shortname_schema
,
514 # # provide this in case SDN assist must deal with this interface
515 # "switch-dpid": dpid_Schema,
516 # "switch-port": shortname_schema,
517 # "switch-mac": shortname_schema,
518 # "switch-vlan": vlan_schema,
520 "required": ["name", "mgmt", "ip-address"],
521 "additionalProperties": False
524 "title": "pdu creation input schema",
525 "$schema": "http://json-schema.org/draft-04/schema#",
528 "name": shortname_schema
,
529 "type": shortname_schema
,
530 "description": description_schema
,
531 "shared": bool_schema
,
532 "vims": nameshort_list_schema
,
533 "vim_accounts": nameshort_list_schema
,
536 "items": pdu_interface
,
540 "required": ["name", "type", "interfaces"],
541 "additionalProperties": False
545 "title": "pdu edit input schema",
546 "$schema": "http://json-schema.org/draft-04/schema#",
549 "name": shortname_schema
,
550 "type": shortname_schema
,
551 "description": description_schema
,
552 "shared": bool_schema
,
553 "vims": {"oneOf": [array_edition_schema
, nameshort_list_schema
]},
554 "vim_accounts": {"oneOf": [array_edition_schema
, nameshort_list_schema
]},
555 "interfaces": {"oneOf": [
556 array_edition_schema
,
559 "items": pdu_interface
,
564 "additionalProperties": False,
569 project_role_mappings
= {
570 "title": "list pf projects/roles",
571 "$schema": "http://json-schema.org/draft-04/schema#",
576 "project": shortname_schema
,
577 "role": shortname_schema
579 "required": ["project", "role"],
580 "additionalProperties": False
584 project_role_mappings_optional
= {
585 "title": "list of projects/roles or projects only",
586 "$schema": "http://json-schema.org/draft-04/schema#",
591 "project": shortname_schema
,
592 "role": shortname_schema
594 "required": ["project"],
595 "additionalProperties": False
600 "$schema": "http://json-schema.org/draft-04/schema#",
601 "title": "New user schema",
604 "username": shortname_schema
,
605 "password": passwd_schema
,
606 "projects": nameshort_list_schema
,
607 "project_role_mappings": project_role_mappings
,
609 "required": ["username", "password"],
610 "additionalProperties": False
613 "$schema": "http://json-schema.org/draft-04/schema#",
614 "title": "User edit schema for administrators",
617 "password": passwd_schema
,
618 "username": shortname_schema
, # To allow User Name modification
621 nameshort_list_schema
,
625 "project_role_mappings": project_role_mappings
,
626 "add_project_role_mappings": project_role_mappings
,
627 "remove_project_role_mappings": project_role_mappings_optional
,
630 "additionalProperties": False
634 topics_with_quota
= ["vnfds", "nsds", "nsts", "pdus", "nsrs", "nsis", "vim_accounts", "wim_accounts", "sdns"]
635 project_new_schema
= {
636 "$schema": "http://json-schema.org/draft-04/schema#",
637 "title": "New project schema for administrators",
640 "name": shortname_schema
,
641 "admin": bool_schema
,
644 "properties": {topic
: integer0_schema
for topic
in topics_with_quota
},
645 "additionalProperties": False
648 "required": ["name"],
649 "additionalProperties": False
651 project_edit_schema
= {
652 "$schema": "http://json-schema.org/draft-04/schema#",
653 "title": "Project edit schema for administrators",
656 "admin": bool_schema
,
657 "name": shortname_schema
, # To allow Project Name modification
660 "properties": {topic
: {"oneOf": [integer0_schema
, null_schema
]} for topic
in topics_with_quota
},
661 "additionalProperties": False
664 "additionalProperties": False,
670 "$schema": "http://json-schema.org/draft-04/schema#",
671 "title": "New role schema for administrators",
674 "name": shortname_schema
,
677 "patternProperties": {
680 # "minProperties": 1,
683 "required": ["name"],
684 "additionalProperties": False
686 roles_edit_schema
= {
687 "$schema": "http://json-schema.org/draft-04/schema#",
688 "title": "Roles edit schema for administrators",
691 "name": shortname_schema
,
694 "patternProperties": {
696 "oneOf": [bool_schema
, null_schema
]
699 # "minProperties": 1,
702 "additionalProperties": False,
708 nbi_new_input_schemas
= {
709 "users": user_new_schema
,
710 "projects": project_new_schema
,
711 "vim_accounts": vim_account_new_schema
,
712 "sdns": sdn_new_schema
,
713 "ns_instantiate": ns_instantiate
,
714 "ns_action": ns_action
,
715 "ns_scale": ns_scale
,
716 "pdus": pdu_new_schema
,
719 nbi_edit_input_schemas
= {
720 "users": user_edit_schema
,
721 "projects": project_edit_schema
,
722 "vim_accounts": vim_account_edit_schema
,
723 "sdns": sdn_edit_schema
,
724 "pdus": pdu_edit_schema
,
728 nsi_subnet_instantiate
= deepcopy(ns_instantiate
)
729 nsi_subnet_instantiate
["title"] = "netslice subnet instantiation params input schema"
730 nsi_subnet_instantiate
["properties"]["id"] = name_schema
731 del nsi_subnet_instantiate
["required"]
733 nsi_vld_instantiate
= {
734 "title": "netslice vld instantiation params input schema",
735 "$schema": "http://json-schema.org/draft-04/schema#",
738 "name": string_schema
,
739 "vim-network-name": {"OneOf": [string_schema
, object_schema
]},
740 "vim-network-id": {"OneOf": [string_schema
, object_schema
]},
741 "ip-profile": object_schema
,
743 "required": ["name"],
744 "additionalProperties": False
748 "title": "netslice action instantiate input schema",
749 "$schema": "http://json-schema.org/draft-04/schema#",
752 "lcmOperationType": string_schema
,
753 "netsliceInstanceId": id_schema
,
754 "nsiName": name_schema
,
755 "nsiDescription": {"oneOf": [description_schema
, null_schema
]},
756 "nstId": string_schema
,
757 "vimAccountId": id_schema
,
758 "ssh_keys": {"type": "string"},
760 "additionalParamsForNsi": object_schema
,
764 "items": nsi_subnet_instantiate
769 "items": nsi_vld_instantiate
772 "required": ["nsiName", "nstId", "vimAccountId"],
773 "additionalProperties": False
785 class ValidationError(Exception):
786 def __init__(self
, message
, http_code
=HTTPStatus
.UNPROCESSABLE_ENTITY
):
787 self
.http_code
= http_code
788 Exception.__init
__(self
, message
)
791 def validate_input(indata
, schema_to_use
):
793 Validates input data against json schema
794 :param indata: user input data. Should be a dictionary
795 :param schema_to_use: jsonschema to test
796 :return: None if ok, raises ValidationError exception on error
800 js_v(indata
, schema_to_use
)
802 except js_e
.ValidationError
as e
:
804 error_pos
= "at '" + ":".join(map(str, e
.path
)) + "'"
807 raise ValidationError("Format error {} '{}' ".format(error_pos
, e
.message
))
808 except js_e
.SchemaError
:
809 raise ValidationError("Bad json schema {}".format(schema_to_use
), http_code
=HTTPStatus
.INTERNAL_SERVER_ERROR
)
812 def is_valid_uuid(x
):
814 Test for a valid UUID
815 :param x: string to test
816 :return: True if x is a valid uuid, False otherwise
821 except (TypeError, ValueError, AttributeError):