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 nameshort_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": nameshort_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
205 "title": "ns action instantiate input schema",
206 "$schema": "http://json-schema.org/draft-04/schema#",
209 "lcmOperationType": string_schema
,
210 "nsInstanceId": id_schema
,
211 "netsliceInstanceId": id_schema
,
212 "nsName": name_schema
,
213 "nsDescription": {"oneOf": [description_schema
, {"type": "null"}]},
215 "vimAccountId": id_schema
,
216 "ssh_keys": {"type": "array", "items": {"type": "string"}},
218 "vduImage": name_schema
,
225 "member-vnf-index": name_schema
,
226 "vimAccountId": id_schema
,
230 "items": ns_instantiate_vdu
,
235 "items": ns_instantiate_internal_vld
238 "required": ["member-vnf-index"],
240 "additionalProperties": False
249 "name": string_schema
,
250 "vim-network-name": {"OneOf": [string_schema
, object_schema
]},
251 "vim-network-id": {"OneOf": [string_schema
, object_schema
]},
252 "ip-profile": object_schema
,
253 "vnfd-connection-point-ref": {
259 "member-vnf-index-ref": name_schema
,
260 "vnfd-connection-point-ref": name_schema
,
261 "ip-address": ip_schema
,
262 # "mac-address": mac_schema,
264 "required": ["member-vnf-index-ref", "vnfd-connection-point-ref"],
266 "additionalProperties": False
270 "required": ["name"],
271 "additionalProperties": False
275 "required": ["nsName", "nsdId", "vimAccountId"],
276 "additionalProperties": False
279 ns_action
= { # TODO for the moment it is only contemplated the vnfd primitive execution
280 "title": "ns action input schema",
281 "$schema": "http://json-schema.org/draft-04/schema#",
284 "lcmOperationType": string_schema
,
285 "nsInstanceId": id_schema
,
286 "member_vnf_index": name_schema
,
287 "vnf_member_index": name_schema
, # TODO for backward compatibility. To remove in future
288 "vdu_id": name_schema
,
289 "primitive": name_schema
,
290 "primitive_params": {"type": "object"},
292 "required": ["primitive", "primitive_params"], # TODO add member_vnf_index
293 "additionalProperties": False
295 ns_scale
= { # TODO for the moment it is only VDU-scaling
296 "title": "ns scale input schema",
297 "$schema": "http://json-schema.org/draft-04/schema#",
300 "lcmOperationType": string_schema
,
301 "nsInstanceId": id_schema
,
302 "scaleType": {"enum": ["SCALE_VNF"]},
306 "vnfInstanceId": name_schema
,
307 "scaleVnfType": {"enum": ["SCALE_OUT", 'SCALE_IN']},
311 "scaling-group-descriptor": name_schema
,
312 "member-vnf-index": name_schema
,
313 "scaling-policy": name_schema
,
315 "required": ["scaling-group-descriptor", "member-vnf-index"],
316 "additionalProperties": False
319 "required": ["scaleVnfType", "scaleByStepData"], # vnfInstanceId
320 "additionalProperties": False
322 "scaleTime": time_schema
,
324 "required": ["scaleType", "scaleVnfData"],
325 "additionalProperties": False
329 schema_version
= {"type": "string", "enum": ["1.0"]}
330 schema_type
= {"type": "string"}
331 vim_account_edit_schema
= {
332 "title": "vim_account edit input schema",
333 "$schema": "http://json-schema.org/draft-04/schema#",
337 "description": description_schema
,
338 "type": nameshort_schema
,
340 "datacenter": name_schema
,
341 "vim_url": description_schema
,
342 "vim_url_admin": description_schema
,
343 "vim_tenant": name_schema
,
344 "vim_tenant_name": name_schema
,
345 "vim_username": nameshort_schema
,
346 "vim_password": passwd_schema
,
347 "config": {"type": "object"}
349 "additionalProperties": False
352 vim_account_new_schema
= {
353 "title": "vim_account creation input schema",
354 "$schema": "http://json-schema.org/draft-04/schema#",
357 "schema_version": schema_version
,
358 "schema_type": schema_type
,
360 "description": description_schema
,
362 "datacenter": name_schema
,
363 "vim_type": {"enum": ["openstack", "openvim", "vmware", "opennebula", "aws"]},
364 "vim_url": description_schema
,
365 # "vim_url_admin": description_schema,
366 # "vim_tenant": name_schema,
367 "vim_tenant_name": name_schema
,
368 "vim_user": nameshort_schema
,
369 "vim_password": passwd_schema
,
370 "config": {"type": "object"}
372 "required": ["name", "vim_url", "vim_type", "vim_user", "vim_password", "vim_tenant_name"],
373 "additionalProperties": False
376 wim_account_edit_schema
= {
377 "title": "wim_account edit input schema",
378 "$schema": "http://json-schema.org/draft-04/schema#",
382 "description": description_schema
,
383 "type": nameshort_schema
,
385 "wim_url": description_schema
,
386 "user": nameshort_schema
,
387 "password": passwd_schema
,
388 "config": {"type": "object"}
390 "additionalProperties": False
393 wim_account_new_schema
= {
394 "title": "wim_account creation input schema",
395 "$schema": "http://json-schema.org/draft-04/schema#",
398 "schema_version": schema_version
,
399 "schema_type": schema_type
,
401 "description": description_schema
,
403 "wim_type": {"enum": ["tapi", "onos", "odl", "dynpac"]},
404 "wim_url": description_schema
,
405 "user": nameshort_schema
,
406 "password": passwd_schema
,
407 "config": {"type": "object"}
409 "required": ["name", "wim_url", "wim_type"],
410 "additionalProperties": False
415 "description": description_schema
,
419 "type": {"type": "string", "enum": ["opendaylight", "floodlight", "onos"]},
420 "version": {"type": "string", "minLength": 1, "maxLength": 12},
421 "user": nameshort_schema
,
422 "password": passwd_schema
425 "title": "sdn controller information schema",
426 "$schema": "http://json-schema.org/draft-04/schema#",
428 "properties": sdn_properties
,
429 "required": ["name", "port", 'ip', 'dpid', 'type'],
430 "additionalProperties": False
433 "title": "sdn controller update information schema",
434 "$schema": "http://json-schema.org/draft-04/schema#",
436 "properties": sdn_properties
,
437 # "required": ["name", "port", 'ip', 'dpid', 'type'],
438 "additionalProperties": False
440 sdn_port_mapping_schema
= {
441 "$schema": "http://json-schema.org/draft-04/schema#",
442 "title": "sdn port mapping information schema",
447 "compute_node": nameshort_schema
,
453 "pci": pci_extended_schema
,
454 "switch_port": nameshort_schema
,
455 "switch_mac": mac_schema
461 "required": ["compute_node", "ports"]
464 sdn_external_port_schema
= {
465 "$schema": "http://json-schema.org/draft-04/schema#",
466 "title": "External port information",
469 "port": {"type": "string", "minLength": 1, "maxLength": 60},
480 "name": nameshort_schema
,
482 "type": {"enum": ["overlay", 'underlay']},
483 "ip-address": ip_schema
,
484 # TODO, add user, password, ssh-key
485 "mac-address": mac_schema
,
486 "vim-network-name": nameshort_schema
, # interface is connected to one vim network, or switch port
487 "vim-network-id": nameshort_schema
,
488 # # provide this in case SDN assist must deal with this interface
489 # "switch-dpid": dpid_Schema,
490 # "switch-port": nameshort_schema,
491 # "switch-mac": nameshort_schema,
492 # "switch-vlan": vlan_schema,
494 "required": ["name", "mgmt", "ip-address"],
495 "additionalProperties": False
498 "title": "pdu creation input schema",
499 "$schema": "http://json-schema.org/draft-04/schema#",
502 "name": nameshort_schema
,
503 "type": nameshort_schema
,
504 "description": description_schema
,
505 "shared": bool_schema
,
506 "vims": nameshort_list_schema
,
507 "vim_accounts": nameshort_list_schema
,
510 "items": pdu_interface
,
514 "required": ["name", "type", "interfaces"],
515 "additionalProperties": False
519 "title": "pdu edit input schema",
520 "$schema": "http://json-schema.org/draft-04/schema#",
523 "name": nameshort_schema
,
524 "type": nameshort_schema
,
525 "description": description_schema
,
526 "shared": bool_schema
,
527 "vims": {"oneOf": [array_edition_schema
, nameshort_list_schema
]},
528 "vim_accounts": {"oneOf": [array_edition_schema
, nameshort_list_schema
]},
529 "interfaces": {"oneOf": [
530 array_edition_schema
,
533 "items": pdu_interface
,
538 "additionalProperties": False,
544 "$schema": "http://json-schema.org/draft-04/schema#",
545 "title": "New user schema",
548 "username": nameshort_schema
,
549 "password": passwd_schema
,
550 "projects": nameshort_list_schema
,
552 "required": ["username", "password", "projects"],
553 "additionalProperties": False
556 "$schema": "http://json-schema.org/draft-04/schema#",
557 "title": "User edit schema for administrators",
560 "password": passwd_schema
,
563 nameshort_list_schema
,
569 "additionalProperties": False
573 project_new_schema
= {
574 "$schema": "http://json-schema.org/draft-04/schema#",
575 "title": "New project schema for administrators",
578 "name": nameshort_schema
,
579 "admin": bool_schema
,
581 "required": ["name"],
582 "additionalProperties": False
584 project_edit_schema
= {
585 "$schema": "http://json-schema.org/draft-04/schema#",
586 "title": "Project edit schema for administrators",
589 "admin": bool_schema
,
591 "additionalProperties": False,
597 nbi_new_input_schemas
= {
598 "users": user_new_schema
,
599 "projects": project_new_schema
,
600 "vim_accounts": vim_account_new_schema
,
601 "sdns": sdn_new_schema
,
602 "ns_instantiate": ns_instantiate
,
603 "ns_action": ns_action
,
604 "ns_scale": ns_scale
,
605 "pdus": pdu_new_schema
,
608 nbi_edit_input_schemas
= {
609 "users": user_edit_schema
,
610 "projects": project_edit_schema
,
611 "vim_accounts": vim_account_edit_schema
,
612 "sdns": sdn_edit_schema
,
613 "pdus": pdu_edit_schema
,
617 nsi_slice_instantiate
= deepcopy(ns_instantiate
)
618 nsi_slice_instantiate
["title"] = "netslice subnet instantiation params input schema"
619 nsi_slice_instantiate
["properties"]["id"] = name_schema
620 del nsi_slice_instantiate
["required"]
622 nsi_vld_instantiate
= {
623 "title": "netslice vld instantiation params input schema",
624 "$schema": "http://json-schema.org/draft-04/schema#",
627 "name": string_schema
,
628 "vim-network-name": {"OneOf": [string_schema
, object_schema
]},
629 "vim-network-id": {"OneOf": [string_schema
, object_schema
]},
630 "ip-profile": object_schema
,
632 "required": ["name"],
633 "additionalProperties": False
637 "title": "netslice action instantiate input schema",
638 "$schema": "http://json-schema.org/draft-04/schema#",
641 "lcmOperationType": string_schema
,
642 "nsiInstanceId": id_schema
,
643 "nsiName": name_schema
,
644 "nsiDescription": {"oneOf": [description_schema
, {"type": "null"}]},
645 "nstId": string_schema
,
646 "vimAccountId": id_schema
,
647 "ssh_keys": {"type": "string"},
652 "items": nsi_slice_instantiate
657 "items": nsi_vld_instantiate
660 "required": ["nsiName", "nstId", "vimAccountId"],
661 "additionalProperties": False
673 class ValidationError(Exception):
674 def __init__(self
, message
, http_code
=HTTPStatus
.UNPROCESSABLE_ENTITY
):
675 self
.http_code
= http_code
676 Exception.__init
__(self
, message
)
679 def validate_input(indata
, schema_to_use
):
681 Validates input data against json schema
682 :param indata: user input data. Should be a dictionary
683 :param schema_to_use: jsonschema to test
684 :return: None if ok, raises ValidationError exception on error
688 js_v(indata
, schema_to_use
)
690 except js_e
.ValidationError
as e
:
692 error_pos
= "at '" + ":".join(map(str, e
.path
)) + "'"
695 raise ValidationError("Format error {} '{}' ".format(error_pos
, e
.message
))
696 except js_e
.SchemaError
:
697 raise ValidationError("Bad json schema {}".format(schema_to_use
), http_code
=HTTPStatus
.INTERNAL_SERVER_ERROR
)