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 http
import HTTPStatus
18 from jsonschema
import exceptions
as js_e
, validate
as js_v
20 __author__
= "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
22 version_date
= "Jun 2020"
25 Validator of input data using JSON schemas
33 "pattern": "^[^,;()'\"]+$",
35 string_schema
= {"type": "string", "minLength": 1, "maxLength": 255}
36 ssh_key_schema
= {"type": "string", "minLength": 1}
39 "pattern": "^[a-fA-F0-9]{8}(-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}$",
41 bool_schema
= {"type": "boolean"}
42 null_schema
= {"type": "null"}
43 object_schema
= {"type": "object"}
45 deploy_item_schema
= {
46 "title": "deploy item validation. Each vld, vdu, flavor, image, ...",
47 "$schema": "http://json-schema.org/draft-04/schema#",
51 "vim_info": object_schema
,
52 "common_id": string_schema
,
54 "additionalProperties": True,
59 "items": deploy_item_schema
,
63 "title": "deploy.vnf.item validation",
64 "$schema": "http://json-schema.org/draft-04/schema#",
68 "vdur": deploy_item_list
,
69 "vld": deploy_item_list
,
71 "additionalProperties": True,
75 deploy_action_schema
= {
76 "title": "deploy.action validation",
77 "$schema": "http://json-schema.org/draft-04/schema#",
80 "action": {"enum": ["inject_ssh_key"]},
81 "key": ssh_key_schema
,
82 "user": string_schema
,
83 "password": string_schema
,
85 "additionalProperties": False,
86 "required": ["action"],
90 "title": "deploy input validation",
91 "$schema": "http://json-schema.org/draft-04/schema#",
94 "action_id": string_schema
,
95 "cloud_init_content": object_schema
,
96 "name": string_schema
,
97 "action": deploy_action_schema
,
100 "items": deploy_vnf_schema
,
102 "image": deploy_item_list
,
103 "flavor": deploy_item_list
,
107 "vld": deploy_item_list
,
110 "affinity-or-anti-affinity-group": deploy_item_list
,
112 "additionalProperties": False,
116 "$schema": "http://json-schema.org/draft-04/schema#",
123 "vim_name": name_schema
,
124 "member-vnf-index": name_schema
,
127 "additionalProperties": True,
132 class ValidationError(Exception):
133 def __init__(self
, message
, http_code
=HTTPStatus
.UNPROCESSABLE_ENTITY
):
134 self
.http_code
= http_code
135 Exception.__init
__(self
, message
)
138 def validate_input(indata
, schema_to_use
):
140 Validates input data against json schema
141 :param indata: user input data. Should be a dictionary
142 :param schema_to_use: jsonschema to test
143 :return: None if ok, raises ValidationError exception on error
147 js_v(indata
, schema_to_use
)
150 except js_e
.ValidationError
as e
:
152 error_pos
= "at '" + ":".join(map(str, e
.path
)) + "'"
156 raise ValidationError("Format error {} '{}' ".format(error_pos
, e
.message
))
157 except js_e
.SchemaError
:
158 raise ValidationError(
159 "Bad json schema {}".format(schema_to_use
),
160 http_code
=HTTPStatus
.INTERNAL_SERVER_ERROR
,