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
19 __author__
= "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
21 version_date
= "Mar 2018"
24 Validator of input data using JSON schemas for those items that not contains an OSM yang information model
28 patern_name
= "^[ -~]+$"
29 nameshort_schema
= {"type": "string", "minLength": 1, "maxLength": 60, "pattern": "^[^,;()\\.\\$'\"]+$"}
30 passwd_schema
= {"type": "string", "minLength": 1, "maxLength": 60}
31 name_schema
= {"type": "string", "minLength": 1, "maxLength": 255, "pattern": "^[^,;()'\"]+$"}
32 string_schema
= {"type": "string", "minLength": 1, "maxLength": 255}
33 xml_text_schema
= {"type": "string", "minLength": 1, "maxLength": 1000, "pattern": "^[^']+$"}
34 description_schema
= {"type": ["string", "null"], "maxLength": 255, "pattern": "^[^'\"]+$"}
35 id_schema_fake
= {"type": "string", "minLength": 2, "maxLength": 36}
36 bool_schema
= {"type": "boolean"}
37 null_schema
= {"type": "null"}
38 # "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}$"
39 id_schema
= {"type": "string", "pattern": "^[a-fA-F0-9]{8}(-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}$"}
40 time_schema
= {"type": "string", "pattern": "^[0-9]{4}-[0-1][0-9]-[0-3][0-9]T[0-2][0-9]([0-5]:){2}"}
41 pci_schema
= {"type": "string", "pattern": "^[0-9a-fA-F]{4}(:[0-9a-fA-F]{2}){2}\\.[0-9a-fA-F]$"}
42 # allows [] for wildcards. For that reason huge length limit is set
43 pci_extended_schema
= {"type": "string", "pattern": "^[0-9a-fA-F.:-\\[\\]]{12,40}$"}
44 http_schema
= {"type": "string", "pattern": "^https?://[^'\"=]+$"}
45 bandwidth_schema
= {"type": "string", "pattern": "^[0-9]+ *([MG]bps)?$"}
46 memory_schema
= {"type": "string", "pattern": "^[0-9]+ *([MG]i?[Bb])?$"}
47 integer0_schema
= {"type": "integer", "minimum": 0}
48 integer1_schema
= {"type": "integer", "minimum": 1}
49 path_schema
= {"type": "string", "pattern": "^(\\.){0,2}(/[^/\"':{}\\(\\)]+)+$"}
50 vlan_schema
= {"type": "integer", "minimum": 1, "maximum": 4095}
51 vlan1000_schema
= {"type": "integer", "minimum": 1000, "maximum": 4095}
52 mac_schema
= {"type": "string",
53 "pattern": "^[0-9a-fA-F][02468aceACE](:[0-9a-fA-F]{2}){5}$"} # must be unicast: LSB bit of MSB byte ==0
54 dpid_Schema
= {"type": "string", "pattern": "^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){7}$"}
55 # mac_schema={"type":"string", "pattern":"^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$"}
56 ip_schema
= {"type": "string",
57 "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]?)$"}
58 ip_prefix_schema
= {"type": "string",
59 "pattern": "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}"
60 "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/(30|[12]?[0-9])$"}
61 port_schema
= {"type": "integer", "minimum": 1, "maximum": 65534}
62 object_schema
= {"type": "object"}
63 schema_version_2
= {"type": "integer", "minimum": 2, "maximum": 2}
64 # schema_version_string={"type":"string","enum": ["0.1", "2", "0.2", "3", "0.3"]}
65 log_level_schema
= {"type": "string", "enum": ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]}
66 checksum_schema
= {"type": "string", "pattern": "^[0-9a-fA-F]{32}$"}
67 size_schema
= {"type": "integer", "minimum": 1, "maximum": 100}
68 array_edition_schema
= {
70 "patternProperties": {
73 "additionalProperties": False,
76 nameshort_list_schema
= {
79 "items": nameshort_schema
,
83 ns_instantiate_vdu
= {
84 "title": "ns action instantiate input schema for vdu",
85 "$schema": "http://json-schema.org/draft-04/schema#",
96 "vim-volume-id": name_schema
,
98 "required": ["name", "vim-volume-id"],
99 "additionalProperties": False
109 "ip-address": ip_schema
,
110 "mac-address": mac_schema
,
111 "floating-ip-required": bool_schema
,
113 "required": ["name"],
114 "additionalProperties": False
119 "additionalProperties": False
122 ip_profile_dns_schema
= {
128 "address": ip_schema
,
130 "required": ["address"],
131 "additionalProperties": False
135 ip_profile_dhcp_schema
= {
138 "enabled": {"type": "boolean"},
139 "count": integer1_schema
,
140 "start-address": ip_schema
142 "additionalProperties": False,
145 ip_profile_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": ip_prefix_schema
,
152 "gateway-address": ip_schema
,
153 "dns-server": ip_profile_dns_schema
,
154 "dhcp-params": ip_profile_dhcp_schema
,
158 ip_profile_update_schema
= {
159 "title": "ip profile validation schame",
160 "$schema": "http://json-schema.org/draft-04/schema#",
163 "ip-version": {"enum": ["ipv4", "ipv6"]},
164 "subnet-address": {"oneOf": [null_schema
, ip_prefix_schema
]},
165 "gateway-address": {"oneOf": [null_schema
, ip_schema
]},
166 "dns-server": {"oneOf": [null_schema
, ip_profile_dns_schema
]},
168 "dhcp-params": {"oneOf": [null_schema
, ip_profile_dhcp_schema
]},
170 "additionalProperties": False
173 ns_instantiate_internal_vld
= {
174 "title": "ns action instantiate input schema for vdu",
175 "$schema": "http://json-schema.org/draft-04/schema#",
179 "vim-network-name": name_schema
,
180 "ip-profile": ip_profile_update_schema
,
181 "internal-connection-point": {
187 "id-ref": name_schema
,
188 "ip-address": ip_schema
,
189 # "mac-address": mac_schema,
191 "required": ["id-ref"],
193 "additionalProperties": False
197 "required": ["name"],
199 "additionalProperties": False
203 "title": "ns action instantiate input schema",
204 "$schema": "http://json-schema.org/draft-04/schema#",
207 "lcmOperationType": string_schema
,
208 "nsInstanceId": id_schema
,
209 "netsliceInstanceId": id_schema
,
210 "nsName": name_schema
,
211 "nsDescription": {"oneOf": [description_schema
, {"type": "null"}]},
213 "vimAccountId": id_schema
,
214 "ssh_keys": {"type": "array", "items": {"type": "string"}},
216 "vduImage": name_schema
,
223 "member-vnf-index": name_schema
,
224 "vimAccountId": id_schema
,
228 "items": ns_instantiate_vdu
,
233 "items": ns_instantiate_internal_vld
236 "required": ["member-vnf-index"],
238 "additionalProperties": False
247 "name": string_schema
,
248 "vim-network-name": {"OneOf": [string_schema
, object_schema
]},
249 "ip-profile": object_schema
,
250 "vnfd-connection-point-ref": {
256 "member-vnf-index-ref": name_schema
,
257 "vnfd-connection-point-ref": name_schema
,
258 "ip-address": ip_schema
,
259 # "mac-address": mac_schema,
261 "required": ["member-vnf-index-ref", "vnfd-connection-point-ref"],
263 "additionalProperties": False
267 "required": ["name"],
268 "additionalProperties": False
272 "required": ["nsName", "nsdId", "vimAccountId"],
273 "additionalProperties": False
276 ns_action
= { # TODO for the moment it is only contemplated the vnfd primitive execution
277 "title": "ns action input schema",
278 "$schema": "http://json-schema.org/draft-04/schema#",
281 "lcmOperationType": string_schema
,
282 "nsInstanceId": id_schema
,
283 "member_vnf_index": name_schema
,
284 "vnf_member_index": name_schema
, # TODO for backward compatibility. To remove in future
285 "vdu_id": name_schema
,
286 "primitive": name_schema
,
287 "primitive_params": {"type": "object"},
289 "required": ["primitive", "primitive_params"], # TODO add member_vnf_index
290 "additionalProperties": False
292 ns_scale
= { # TODO for the moment it is only VDU-scaling
293 "title": "ns scale input schema",
294 "$schema": "http://json-schema.org/draft-04/schema#",
297 "lcmOperationType": string_schema
,
298 "nsInstanceId": id_schema
,
299 "scaleType": {"enum": ["SCALE_VNF"]},
303 "vnfInstanceId": name_schema
,
304 "scaleVnfType": {"enum": ["SCALE_OUT", 'SCALE_IN']},
308 "scaling-group-descriptor": name_schema
,
309 "member-vnf-index": name_schema
,
310 "scaling-policy": name_schema
,
312 "required": ["scaling-group-descriptor", "member-vnf-index"],
313 "additionalProperties": False
316 "required": ["scaleVnfType", "scaleByStepData"], # vnfInstanceId
317 "additionalProperties": False
319 "scaleTime": time_schema
,
321 "required": ["scaleType", "scaleVnfData"],
322 "additionalProperties": False
326 schema_version
= {"type": "string", "enum": ["1.0"]}
327 vim_account_edit_schema
= {
328 "title": "vim_account edit input schema",
329 "$schema": "http://json-schema.org/draft-04/schema#",
333 "description": description_schema
,
334 "type": nameshort_schema
, # currently "openvim" or "openstack", can be enlarged with plugins
336 "datacenter": name_schema
,
337 "vim_url": description_schema
,
338 "vim_url_admin": description_schema
,
339 "vim_tenant": name_schema
,
340 "vim_tenant_name": name_schema
,
341 "vim_username": nameshort_schema
,
342 "vim_password": passwd_schema
,
343 "config": {"type": "object"}
345 "additionalProperties": False
347 schema_type
= {"type": "string"}
349 vim_account_new_schema
= {
350 "title": "vim_account creation input schema",
351 "$schema": "http://json-schema.org/draft-04/schema#",
354 "schema_version": schema_version
,
355 "schema_type": schema_type
,
357 "description": description_schema
,
359 "datacenter": name_schema
,
360 "vim_type": {"enum": ["openstack", "openvim", "vmware", "opennebula", "aws"]},
361 "vim_url": description_schema
,
362 # "vim_url_admin": description_schema,
363 # "vim_tenant": name_schema,
364 "vim_tenant_name": name_schema
,
365 "vim_user": nameshort_schema
,
366 "vim_password": passwd_schema
,
367 "config": {"type": "object"}
369 "required": ["name", "vim_url", "vim_type", "vim_user", "vim_password", "vim_tenant_name"],
370 "additionalProperties": False
376 "description": description_schema
,
380 "type": {"type": "string", "enum": ["opendaylight", "floodlight", "onos"]},
381 "version": {"type": "string", "minLength": 1, "maxLength": 12},
382 "user": nameshort_schema
,
383 "password": passwd_schema
386 "title": "sdn controller information schema",
387 "$schema": "http://json-schema.org/draft-04/schema#",
389 "properties": sdn_properties
,
390 "required": ["name", "port", 'ip', 'dpid', 'type'],
391 "additionalProperties": False
394 "title": "sdn controller update information schema",
395 "$schema": "http://json-schema.org/draft-04/schema#",
397 "properties": sdn_properties
,
398 # "required": ["name", "port", 'ip', 'dpid', 'type'],
399 "additionalProperties": False
401 sdn_port_mapping_schema
= {
402 "$schema": "http://json-schema.org/draft-04/schema#",
403 "title": "sdn port mapping information schema",
408 "compute_node": nameshort_schema
,
414 "pci": pci_extended_schema
,
415 "switch_port": nameshort_schema
,
416 "switch_mac": mac_schema
422 "required": ["compute_node", "ports"]
425 sdn_external_port_schema
= {
426 "$schema": "http://json-schema.org/draft-04/schema#",
427 "title": "External port information",
430 "port": {"type": "string", "minLength": 1, "maxLength": 60},
441 "name": nameshort_schema
,
443 "type": {"enum": ["overlay", 'underlay']},
444 "ip-address": ip_schema
,
445 # TODO, add user, password, ssh-key
446 "mac-address": mac_schema
,
447 "vim-network-name": nameshort_schema
, # interface is connected to one vim network, or switch port
448 # TODO "vim-network-id": nameshort_schema,
449 # # provide this in case SDN assist must deal with this interface
450 # "switch-dpid": dpid_Schema,
451 # "switch-port": nameshort_schema,
452 # "switch-mac": nameshort_schema,
453 # "switch-vlan": vlan_schema,
455 "required": ["name", "mgmt", "ip-address"],
456 "additionalProperties": False
459 "title": "pdu creation input schema",
460 "$schema": "http://json-schema.org/draft-04/schema#",
463 "name": nameshort_schema
,
464 "type": nameshort_schema
,
465 "description": description_schema
,
466 "shared": bool_schema
,
467 "vims": nameshort_list_schema
,
468 "vim_accounts": nameshort_list_schema
,
471 "items": pdu_interface
,
475 "required": ["name", "type", "interfaces"],
476 "additionalProperties": False
480 "title": "pdu edit input schema",
481 "$schema": "http://json-schema.org/draft-04/schema#",
484 "name": nameshort_schema
,
485 "type": nameshort_schema
,
486 "description": description_schema
,
487 "shared": bool_schema
,
488 "vims": {"oneOff": [array_edition_schema
, nameshort_list_schema
]},
489 "vim_accounts": {"oneOff": [array_edition_schema
, nameshort_list_schema
]},
490 "interfaces": {"oneOff": [
491 array_edition_schema
,
494 "items": pdu_interface
,
499 "additionalProperties": False,
505 "$schema": "http://json-schema.org/draft-04/schema#",
506 "title": "New user schema",
509 "username": nameshort_schema
,
510 "password": passwd_schema
,
511 "projects": nameshort_list_schema
,
513 "required": ["username", "password", "projects"],
514 "additionalProperties": False
517 "$schema": "http://json-schema.org/draft-04/schema#",
518 "title": "User edit schema for administrators",
521 "password": passwd_schema
,
524 nameshort_list_schema
,
530 "additionalProperties": False
534 project_new_schema
= {
535 "$schema": "http://json-schema.org/draft-04/schema#",
536 "title": "New project schema for administrators",
539 "name": nameshort_schema
,
540 "admin": bool_schema
,
542 "required": ["name"],
543 "additionalProperties": False
545 project_edit_schema
= {
546 "$schema": "http://json-schema.org/draft-04/schema#",
547 "title": "Project edit schema for administrators",
550 "admin": bool_schema
,
552 "additionalProperties": False,
558 nbi_new_input_schemas
= {
559 "users": user_new_schema
,
560 "projects": project_new_schema
,
561 "vim_accounts": vim_account_new_schema
,
562 "sdns": sdn_new_schema
,
563 "ns_instantiate": ns_instantiate
,
564 "ns_action": ns_action
,
565 "ns_scale": ns_scale
,
566 "pdus": pdu_new_schema
,
569 nbi_edit_input_schemas
= {
570 "users": user_edit_schema
,
571 "projects": project_edit_schema
,
572 "vim_accounts": vim_account_edit_schema
,
573 "sdns": sdn_edit_schema
,
574 "pdus": pdu_edit_schema
,
579 "title": "netslice action instantiate input schema",
580 "$schema": "http://json-schema.org/draft-04/schema#",
583 "lcmOperationType": string_schema
,
584 "nsiInstanceId": id_schema
,
585 "nsiName": name_schema
,
586 "nsiDescription": {"oneOf": [description_schema
, {"type": "null"}]},
587 "nstdId": string_schema
,
588 "vimAccountId": id_schema
,
589 "ssh_keys": {"type": "string"},
594 "items": ns_instantiate
597 "required": ["nsiName", "nstdId", "vimAccountId"],
598 "additionalProperties": False
610 class ValidationError(Exception):
611 def __init__(self
, message
, http_code
=HTTPStatus
.UNPROCESSABLE_ENTITY
):
612 self
.http_code
= http_code
613 Exception.__init
__(self
, message
)
616 def validate_input(indata
, schema_to_use
):
618 Validates input data against json schema
619 :param indata: user input data. Should be a dictionary
620 :param schema_to_use: jsonschema to test
621 :return: None if ok, raises ValidationError exception on error
625 js_v(indata
, schema_to_use
)
627 except js_e
.ValidationError
as e
:
629 error_pos
= "at '" + ":".join(map(str, e
.path
)) + "'"
632 raise ValidationError("Format error {} '{}' ".format(error_pos
, e
.message
))
633 except js_e
.SchemaError
:
634 raise ValidationError("Bad json schema {}".format(schema_to_use
), http_code
=HTTPStatus
.INTERNAL_SERVER_ERROR
)