blob: 7c074d294a0cafbf22e15aa61e20e87bbe7618b5 [file] [log] [blame]
tierno0f98af52018-03-19 10:28:22 +01001# -*- coding: utf-8 -*-
2
tiernod125caf2018-11-22 16:05:54 +00003# 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
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
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
12# implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
tierno0f98af52018-03-19 10:28:22 +010016from jsonschema import validate as js_v, exceptions as js_e
tierno36ec8602018-11-02 17:27:11 +010017from http import HTTPStatus
garciadeblasdaf8cc52018-11-30 14:17:20 +010018from copy import deepcopy
delacruzramoc061f562019-04-05 11:00:02 +020019from uuid import UUID # To test for valid UUID
tierno0f98af52018-03-19 10:28:22 +010020
21__author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
22__version__ = "0.1"
23version_date = "Mar 2018"
24
25"""
26Validator of input data using JSON schemas for those items that not contains an OSM yang information model
27"""
28
29# Basis schemas
30patern_name = "^[ -~]+$"
tiernofd160572019-01-21 10:41:37 +000031shortname_schema = {"type": "string", "minLength": 1, "maxLength": 60, "pattern": "^[^,;()\\.\\$'\"]+$"}
tierno0f98af52018-03-19 10:28:22 +010032passwd_schema = {"type": "string", "minLength": 1, "maxLength": 60}
tierno0f98af52018-03-19 10:28:22 +010033name_schema = {"type": "string", "minLength": 1, "maxLength": 255, "pattern": "^[^,;()'\"]+$"}
tierno0da52252018-06-27 15:47:22 +020034string_schema = {"type": "string", "minLength": 1, "maxLength": 255}
tierno0f98af52018-03-19 10:28:22 +010035xml_text_schema = {"type": "string", "minLength": 1, "maxLength": 1000, "pattern": "^[^']+$"}
36description_schema = {"type": ["string", "null"], "maxLength": 255, "pattern": "^[^'\"]+$"}
tierno441dbbf2018-07-10 12:52:48 +020037id_schema_fake = {"type": "string", "minLength": 2, "maxLength": 36}
38bool_schema = {"type": "boolean"}
39null_schema = {"type": "null"}
tierno2236d202018-05-16 19:05:16 +020040# "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}$"
tierno0f98af52018-03-19 10:28:22 +010041id_schema = {"type": "string", "pattern": "^[a-fA-F0-9]{8}(-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}$"}
tiernof759d822018-06-11 18:54:54 +020042time_schema = {"type": "string", "pattern": "^[0-9]{4}-[0-1][0-9]-[0-3][0-9]T[0-2][0-9]([0-5]:){2}"}
tierno87006042018-10-24 12:50:20 +020043pci_schema = {"type": "string", "pattern": "^[0-9a-fA-F]{4}(:[0-9a-fA-F]{2}){2}\\.[0-9a-fA-F]$"}
tierno42fce592018-11-02 09:23:43 +010044# allows [] for wildcards. For that reason huge length limit is set
45pci_extended_schema = {"type": "string", "pattern": "^[0-9a-fA-F.:-\\[\\]]{12,40}$"}
tierno0f98af52018-03-19 10:28:22 +010046http_schema = {"type": "string", "pattern": "^https?://[^'\"=]+$"}
47bandwidth_schema = {"type": "string", "pattern": "^[0-9]+ *([MG]bps)?$"}
48memory_schema = {"type": "string", "pattern": "^[0-9]+ *([MG]i?[Bb])?$"}
49integer0_schema = {"type": "integer", "minimum": 0}
50integer1_schema = {"type": "integer", "minimum": 1}
tierno87006042018-10-24 12:50:20 +020051path_schema = {"type": "string", "pattern": "^(\\.){0,2}(/[^/\"':{}\\(\\)]+)+$"}
tierno0f98af52018-03-19 10:28:22 +010052vlan_schema = {"type": "integer", "minimum": 1, "maximum": 4095}
53vlan1000_schema = {"type": "integer", "minimum": 1000, "maximum": 4095}
54mac_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
tiernocb83c942018-09-24 17:28:13 +020056dpid_Schema = {"type": "string", "pattern": "^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){7}$"}
tierno0f98af52018-03-19 10:28:22 +010057# mac_schema={"type":"string", "pattern":"^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$"}
58ip_schema = {"type": "string",
tierno87006042018-10-24 12:50:20 +020059 "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]?)$"}
tierno0f98af52018-03-19 10:28:22 +010060ip_prefix_schema = {"type": "string",
tierno87006042018-10-24 12:50:20 +020061 "pattern": "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}"
tierno2236d202018-05-16 19:05:16 +020062 "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/(30|[12]?[0-9])$"}
tierno0f98af52018-03-19 10:28:22 +010063port_schema = {"type": "integer", "minimum": 1, "maximum": 65534}
64object_schema = {"type": "object"}
65schema_version_2 = {"type": "integer", "minimum": 2, "maximum": 2}
66# schema_version_string={"type":"string","enum": ["0.1", "2", "0.2", "3", "0.3"]}
67log_level_schema = {"type": "string", "enum": ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]}
68checksum_schema = {"type": "string", "pattern": "^[0-9a-fA-F]{32}$"}
69size_schema = {"type": "integer", "minimum": 1, "maximum": 100}
tiernocd54a4a2018-09-12 16:40:35 +020070array_edition_schema = {
71 "type": "object",
72 "patternProperties": {
tiernofd160572019-01-21 10:41:37 +000073 "^\\$": {}
tiernocd54a4a2018-09-12 16:40:35 +020074 },
75 "additionalProperties": False,
76 "minProperties": 1,
77}
tiernocb83c942018-09-24 17:28:13 +020078nameshort_list_schema = {
79 "type": "array",
80 "minItems": 1,
tiernofd160572019-01-21 10:41:37 +000081 "items": shortname_schema,
tiernocb83c942018-09-24 17:28:13 +020082}
tiernocd54a4a2018-09-12 16:40:35 +020083
tierno0f98af52018-03-19 10:28:22 +010084
tierno441dbbf2018-07-10 12:52:48 +020085ns_instantiate_vdu = {
86 "title": "ns action instantiate input schema for vdu",
87 "$schema": "http://json-schema.org/draft-04/schema#",
88 "type": "object",
89 "properties": {
90 "id": name_schema,
91 "volume": {
92 "type": "array",
93 "minItems": 1,
94 "items": {
95 "type": "object",
96 "properties": {
97 "name": name_schema,
98 "vim-volume-id": name_schema,
99 },
100 "required": ["name", "vim-volume-id"],
101 "additionalProperties": False
102 }
103 },
104 "interface": {
105 "type": "array",
106 "minItems": 1,
107 "items": {
108 "type": "object",
109 "properties": {
110 "name": name_schema,
111 "ip-address": ip_schema,
112 "mac-address": mac_schema,
113 "floating-ip-required": bool_schema,
114 },
115 "required": ["name"],
116 "additionalProperties": False
117 }
118 }
119 },
120 "required": ["id"],
121 "additionalProperties": False
122}
123
124ip_profile_dns_schema = {
125 "type": "array",
126 "minItems": 1,
127 "items": {
128 "type": "object",
129 "properties": {
130 "address": ip_schema,
131 },
132 "required": ["address"],
133 "additionalProperties": False
134 }
135}
136
137ip_profile_dhcp_schema = {
138 "type": "object",
139 "properties": {
140 "enabled": {"type": "boolean"},
141 "count": integer1_schema,
142 "start-address": ip_schema
143 },
144 "additionalProperties": False,
145}
146
147ip_profile_schema = {
tierno2a929042020-03-10 16:34:25 +0000148 "title": "ip profile validation schema",
tierno441dbbf2018-07-10 12:52:48 +0200149 "$schema": "http://json-schema.org/draft-04/schema#",
150 "type": "object",
151 "properties": {
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,
157 }
158}
159
160ip_profile_update_schema = {
tierno2a929042020-03-10 16:34:25 +0000161 "title": "ip profile validation schema",
tierno441dbbf2018-07-10 12:52:48 +0200162 "$schema": "http://json-schema.org/draft-04/schema#",
163 "type": "object",
164 "properties": {
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]},
169
170 "dhcp-params": {"oneOf": [null_schema, ip_profile_dhcp_schema]},
171 },
172 "additionalProperties": False
173}
174
kbsub21f03f52019-10-17 16:26:56 +0000175provider_network_schema = {
tierno2a929042020-03-10 16:34:25 +0000176 "title": "provider network validation schema",
kbsub21f03f52019-10-17 16:26:56 +0000177 "$schema": "http://json-schema.org/draft-04/schema#",
178 "type": "object",
179 "properties": {
180 "physical-network": name_schema,
181 "segmentation-id": name_schema,
tierno2a929042020-03-10 16:34:25 +0000182 "sdn-ports": { # external ports to append to the SDN-assist network
183 "type": "array",
184 "items": {
185 "type": "object",
186 "properties": {
187 "switch_id": shortname_schema,
188 "switch_port": shortname_schema,
189 "mac_address": mac_schema,
190 "vlan": vlan_schema,
191 },
192 "additionalProperties": True
193 }
194 },
195 "network-type": shortname_schema,
kbsub21f03f52019-10-17 16:26:56 +0000196 },
tierno2a929042020-03-10 16:34:25 +0000197 "additionalProperties": True
kbsub21f03f52019-10-17 16:26:56 +0000198}
199
tierno441dbbf2018-07-10 12:52:48 +0200200ns_instantiate_internal_vld = {
201 "title": "ns action instantiate input schema for vdu",
202 "$schema": "http://json-schema.org/draft-04/schema#",
203 "type": "object",
204 "properties": {
205 "name": name_schema,
206 "vim-network-name": name_schema,
gcalvino17d5b732018-12-17 16:26:21 +0100207 "vim-network-id": name_schema,
tierno441dbbf2018-07-10 12:52:48 +0200208 "ip-profile": ip_profile_update_schema,
kbsub21f03f52019-10-17 16:26:56 +0000209 "provider-network": provider_network_schema,
tierno441dbbf2018-07-10 12:52:48 +0200210 "internal-connection-point": {
211 "type": "array",
212 "minItems": 1,
213 "items": {
214 "type": "object",
215 "properties": {
216 "id-ref": name_schema,
217 "ip-address": ip_schema,
tiernob7c6f2a2018-09-03 14:32:10 +0200218 # "mac-address": mac_schema,
tierno441dbbf2018-07-10 12:52:48 +0200219 },
tiernob7c6f2a2018-09-03 14:32:10 +0200220 "required": ["id-ref"],
221 "minProperties": 2,
tierno441dbbf2018-07-10 12:52:48 +0200222 "additionalProperties": False
223 },
224 }
225 },
226 "required": ["name"],
227 "minProperties": 2,
228 "additionalProperties": False
229}
tierno65acb4d2018-04-06 16:42:40 +0200230
tiernofd160572019-01-21 10:41:37 +0000231additional_params_for_vnf = {
232 "type": "array",
233 "items": {
234 "type": "object",
235 "properties": {
236 "member-vnf-index": name_schema,
237 "additionalParams": object_schema,
tierno54db2e42020-04-06 15:29:42 +0000238 "k8s-namespace": name_schema,
tierno714954e2019-11-29 13:43:26 +0000239 "additionalParamsForVdu": {
240 "type": "array",
241 "items": {
242 "type": "object",
243 "properties": {
244 "vdu_id": name_schema,
245 "additionalParams": object_schema,
246 },
247 "required": ["vdu_id", "additionalParams"],
248 "additionalProperties": False,
249 },
250 },
251 "additionalParamsForKdu": {
252 "type": "array",
253 "items": {
254 "type": "object",
255 "properties": {
256 "kdu_name": name_schema,
257 "additionalParams": object_schema,
tierno54db2e42020-04-06 15:29:42 +0000258 "kdu_model": name_schema,
259 "k8s-namespace": name_schema,
tierno714954e2019-11-29 13:43:26 +0000260 },
tierno54db2e42020-04-06 15:29:42 +0000261 "required": ["kdu_name"],
262 "minProperties": 2,
tierno714954e2019-11-29 13:43:26 +0000263 "additionalProperties": False,
264 },
265 },
tiernofd160572019-01-21 10:41:37 +0000266 },
tierno714954e2019-11-29 13:43:26 +0000267 "required": ["member-vnf-index"],
268 "minProperties": 2,
tiernofd160572019-01-21 10:41:37 +0000269 "additionalProperties": False
270 }
tiernofd160572019-01-21 10:41:37 +0000271}
272
tierno65acb4d2018-04-06 16:42:40 +0200273ns_instantiate = {
274 "title": "ns action instantiate input schema",
275 "$schema": "http://json-schema.org/draft-04/schema#",
276 "type": "object",
tierno0da52252018-06-27 15:47:22 +0200277 "properties": {
tiernob24258a2018-10-04 18:39:49 +0200278 "lcmOperationType": string_schema,
279 "nsInstanceId": id_schema,
Felipe Vicens07f31722018-10-29 15:16:44 +0100280 "netsliceInstanceId": id_schema,
tierno0da52252018-06-27 15:47:22 +0200281 "nsName": name_schema,
tierno4f9d4ae2019-03-20 17:24:11 +0000282 "nsDescription": {"oneOf": [description_schema, null_schema]},
tierno0da52252018-06-27 15:47:22 +0200283 "nsdId": id_schema,
284 "vimAccountId": id_schema,
tierno4f9d4ae2019-03-20 17:24:11 +0000285 "wimAccountId": {"OneOf": [id_schema, bool_schema, null_schema]},
magnussonlf318b302020-01-20 18:38:18 +0100286 "placement-engine": string_schema,
287 "placement-constraints": object_schema,
tiernobee085c2018-12-12 17:03:04 +0000288 "additionalParamsForNs": object_schema,
tiernofd160572019-01-21 10:41:37 +0000289 "additionalParamsForVnf": additional_params_for_vnf,
tierno54db2e42020-04-06 15:29:42 +0000290 "k8s-namespace": name_schema,
tiernofc5089c2018-10-31 09:28:11 +0100291 "ssh_keys": {"type": "array", "items": {"type": "string"}},
tiernoa8186a52020-01-14 15:54:48 +0000292 "timeout_ns_deploy": integer1_schema,
tierno441dbbf2018-07-10 12:52:48 +0200293 "nsr_id": id_schema,
tiernocc103432018-10-19 14:10:35 +0200294 "vduImage": name_schema,
tierno0da52252018-06-27 15:47:22 +0200295 "vnf": {
296 "type": "array",
297 "minItems": 1,
298 "items": {
299 "type": "object",
300 "properties": {
301 "member-vnf-index": name_schema,
302 "vimAccountId": id_schema,
tierno441dbbf2018-07-10 12:52:48 +0200303 "vdu": {
304 "type": "array",
305 "minItems": 1,
306 "items": ns_instantiate_vdu,
307 },
308 "internal-vld": {
309 "type": "array",
310 "minItems": 1,
311 "items": ns_instantiate_internal_vld
312 }
tierno0da52252018-06-27 15:47:22 +0200313 },
tierno441dbbf2018-07-10 12:52:48 +0200314 "required": ["member-vnf-index"],
315 "minProperties": 2,
316 "additionalProperties": False
tierno0da52252018-06-27 15:47:22 +0200317 }
318 },
319 "vld": {
320 "type": "array",
321 "minItems": 1,
322 "items": {
323 "type": "object",
324 "properties": {
325 "name": string_schema,
326 "vim-network-name": {"OneOf": [string_schema, object_schema]},
gcalvino17d5b732018-12-17 16:26:21 +0100327 "vim-network-id": {"OneOf": [string_schema, object_schema]},
Felipe Vicens09e65422019-01-22 15:06:46 +0100328 "ns-net": object_schema,
tierno4f9d4ae2019-03-20 17:24:11 +0000329 "wimAccountId": {"OneOf": [id_schema, bool_schema, null_schema]},
tierno0da52252018-06-27 15:47:22 +0200330 "ip-profile": object_schema,
kbsub21f03f52019-10-17 16:26:56 +0000331 "provider-network": provider_network_schema,
tiernob7c6f2a2018-09-03 14:32:10 +0200332 "vnfd-connection-point-ref": {
333 "type": "array",
334 "minItems": 1,
335 "items": {
336 "type": "object",
337 "properties": {
338 "member-vnf-index-ref": name_schema,
339 "vnfd-connection-point-ref": name_schema,
340 "ip-address": ip_schema,
341 # "mac-address": mac_schema,
342 },
343 "required": ["member-vnf-index-ref", "vnfd-connection-point-ref"],
344 "minProperties": 3,
345 "additionalProperties": False
346 },
347 }
tierno0da52252018-06-27 15:47:22 +0200348 },
tierno441dbbf2018-07-10 12:52:48 +0200349 "required": ["name"],
350 "additionalProperties": False
tierno0da52252018-06-27 15:47:22 +0200351 }
352 },
353 },
tierno441dbbf2018-07-10 12:52:48 +0200354 "required": ["nsName", "nsdId", "vimAccountId"],
355 "additionalProperties": False
tierno65acb4d2018-04-06 16:42:40 +0200356}
tierno0da52252018-06-27 15:47:22 +0200357
tierno1c38f2f2020-03-24 11:51:39 +0000358ns_terminate = {
359 "title": "ns terminate input schema",
360 "$schema": "http://json-schema.org/draft-04/schema#",
361 "type": "object",
362 "properties": {
363 "lcmOperationType": string_schema,
364 "nsInstanceId": id_schema,
365 "autoremove": bool_schema,
366 "timeout_ns_terminate": integer1_schema,
367 "skip_terminate_primitives": bool_schema,
368 },
369 "additionalProperties": False
370}
371
tierno65acb4d2018-04-06 16:42:40 +0200372ns_action = { # TODO for the moment it is only contemplated the vnfd primitive execution
tiernof759d822018-06-11 18:54:54 +0200373 "title": "ns action input schema",
tierno65acb4d2018-04-06 16:42:40 +0200374 "$schema": "http://json-schema.org/draft-04/schema#",
375 "type": "object",
376 "properties": {
tiernob24258a2018-10-04 18:39:49 +0200377 "lcmOperationType": string_schema,
378 "nsInstanceId": id_schema,
tiernof5298be2018-05-16 14:43:57 +0200379 "member_vnf_index": name_schema,
380 "vnf_member_index": name_schema, # TODO for backward compatibility. To remove in future
tierno7ce1db92018-07-25 12:50:52 +0200381 "vdu_id": name_schema,
tiernof0637052019-03-07 16:26:47 +0000382 "vdu_count_index": integer0_schema,
tierno9cb7d672019-10-30 12:13:48 +0000383 "kdu_name": name_schema,
tierno65acb4d2018-04-06 16:42:40 +0200384 "primitive": name_schema,
tierno50c8e6e2020-04-02 15:40:12 +0000385 "timeout_ns_action": integer1_schema,
tierno65acb4d2018-04-06 16:42:40 +0200386 "primitive_params": {"type": "object"},
387 },
tiernof5298be2018-05-16 14:43:57 +0200388 "required": ["primitive", "primitive_params"], # TODO add member_vnf_index
tierno65acb4d2018-04-06 16:42:40 +0200389 "additionalProperties": False
390}
tiernof759d822018-06-11 18:54:54 +0200391ns_scale = { # TODO for the moment it is only VDU-scaling
392 "title": "ns scale input schema",
393 "$schema": "http://json-schema.org/draft-04/schema#",
394 "type": "object",
395 "properties": {
tiernob24258a2018-10-04 18:39:49 +0200396 "lcmOperationType": string_schema,
397 "nsInstanceId": id_schema,
tiernof759d822018-06-11 18:54:54 +0200398 "scaleType": {"enum": ["SCALE_VNF"]},
tierno50c8e6e2020-04-02 15:40:12 +0000399 "timeout_ns_scale": integer1_schema,
tiernof759d822018-06-11 18:54:54 +0200400 "scaleVnfData": {
401 "type": "object",
402 "properties": {
403 "vnfInstanceId": name_schema,
404 "scaleVnfType": {"enum": ["SCALE_OUT", 'SCALE_IN']},
405 "scaleByStepData": {
406 "type": "object",
407 "properties": {
408 "scaling-group-descriptor": name_schema,
409 "member-vnf-index": name_schema,
410 "scaling-policy": name_schema,
411 },
412 "required": ["scaling-group-descriptor", "member-vnf-index"],
413 "additionalProperties": False
414 },
415 },
416 "required": ["scaleVnfType", "scaleByStepData"], # vnfInstanceId
417 "additionalProperties": False
418 },
419 "scaleTime": time_schema,
420 },
421 "required": ["scaleType", "scaleVnfData"],
422 "additionalProperties": False
423}
tierno65acb4d2018-04-06 16:42:40 +0200424
425
tierno0f98af52018-03-19 10:28:22 +0100426schema_version = {"type": "string", "enum": ["1.0"]}
tierno55ba2e62018-12-11 17:22:22 +0000427schema_type = {"type": "string"}
tiernob3d0a0e2019-11-13 15:57:51 +0000428vim_type = shortname_schema # {"enum": ["openstack", "openvim", "vmware", "opennebula", "aws", "azure", "fos"]}
delacruzramo3a144c92019-10-25 09:46:33 +0200429
tierno09c073e2018-04-26 13:36:48 +0200430vim_account_edit_schema = {
431 "title": "vim_account edit input schema",
432 "$schema": "http://json-schema.org/draft-04/schema#",
433 "type": "object",
434 "properties": {
435 "name": name_schema,
436 "description": description_schema,
tierno09c073e2018-04-26 13:36:48 +0200437 "vim": name_schema,
438 "datacenter": name_schema,
delacruzramo3a144c92019-10-25 09:46:33 +0200439 "vim_type": vim_type,
tierno09c073e2018-04-26 13:36:48 +0200440 "vim_url": description_schema,
delacruzramo3a144c92019-10-25 09:46:33 +0200441 # "vim_url_admin": description_schema,
442 # "vim_tenant": name_schema,
tierno09c073e2018-04-26 13:36:48 +0200443 "vim_tenant_name": name_schema,
delacruzramo3a144c92019-10-25 09:46:33 +0200444 "vim_user": shortname_schema,
tiernocd54a4a2018-09-12 16:40:35 +0200445 "vim_password": passwd_schema,
tierno09c073e2018-04-26 13:36:48 +0200446 "config": {"type": "object"}
447 },
448 "additionalProperties": False
449}
tierno0f98af52018-03-19 10:28:22 +0100450
tierno09c073e2018-04-26 13:36:48 +0200451vim_account_new_schema = {
452 "title": "vim_account creation input schema",
tierno0f98af52018-03-19 10:28:22 +0100453 "$schema": "http://json-schema.org/draft-04/schema#",
454 "type": "object",
455 "properties": {
456 "schema_version": schema_version,
457 "schema_type": schema_type,
458 "name": name_schema,
459 "description": description_schema,
tierno09c073e2018-04-26 13:36:48 +0200460 "vim": name_schema,
461 "datacenter": name_schema,
delacruzramo3a144c92019-10-25 09:46:33 +0200462 "vim_type": vim_type,
tierno0f98af52018-03-19 10:28:22 +0100463 "vim_url": description_schema,
464 # "vim_url_admin": description_schema,
465 # "vim_tenant": name_schema,
466 "vim_tenant_name": name_schema,
tiernofd160572019-01-21 10:41:37 +0000467 "vim_user": shortname_schema,
tiernocd54a4a2018-09-12 16:40:35 +0200468 "vim_password": passwd_schema,
tierno0f98af52018-03-19 10:28:22 +0100469 "config": {"type": "object"}
470 },
471 "required": ["name", "vim_url", "vim_type", "vim_user", "vim_password", "vim_tenant_name"],
472 "additionalProperties": False
473}
tierno0f98af52018-03-19 10:28:22 +0100474
tierno85807722019-12-20 14:11:35 +0000475wim_type = shortname_schema # {"enum": ["ietfl2vpn", "onos", "odl", "dynpac", "fake"]}
delacruzramo3a144c92019-10-25 09:46:33 +0200476
tierno55ba2e62018-12-11 17:22:22 +0000477wim_account_edit_schema = {
478 "title": "wim_account edit input schema",
479 "$schema": "http://json-schema.org/draft-04/schema#",
480 "type": "object",
481 "properties": {
482 "name": name_schema,
483 "description": description_schema,
tierno55ba2e62018-12-11 17:22:22 +0000484 "wim": name_schema,
delacruzramo3a144c92019-10-25 09:46:33 +0200485 "wim_type": wim_type,
tierno55ba2e62018-12-11 17:22:22 +0000486 "wim_url": description_schema,
tiernofd160572019-01-21 10:41:37 +0000487 "user": shortname_schema,
tierno55ba2e62018-12-11 17:22:22 +0000488 "password": passwd_schema,
489 "config": {"type": "object"}
490 },
491 "additionalProperties": False
492}
493
494wim_account_new_schema = {
495 "title": "wim_account creation input schema",
496 "$schema": "http://json-schema.org/draft-04/schema#",
497 "type": "object",
498 "properties": {
499 "schema_version": schema_version,
500 "schema_type": schema_type,
501 "name": name_schema,
502 "description": description_schema,
503 "wim": name_schema,
delacruzramo3a144c92019-10-25 09:46:33 +0200504 "wim_type": wim_type,
tierno55ba2e62018-12-11 17:22:22 +0000505 "wim_url": description_schema,
tiernofd160572019-01-21 10:41:37 +0000506 "user": shortname_schema,
tierno55ba2e62018-12-11 17:22:22 +0000507 "password": passwd_schema,
tiernof0637052019-03-07 16:26:47 +0000508 "config": {
509 "type": "object",
510 "patternProperties": {
511 ".": {"not": {"type": "null"}}
512 }
513 }
tierno55ba2e62018-12-11 17:22:22 +0000514 },
515 "required": ["name", "wim_url", "wim_type"],
516 "additionalProperties": False
517}
tierno0f98af52018-03-19 10:28:22 +0100518
519sdn_properties = {
520 "name": name_schema,
tierno7adaeb02019-12-17 16:46:12 +0000521 "type": {"type": "string"},
522 "url": {"type": "string"},
523 "user": shortname_schema,
524 "password": passwd_schema,
525 "config": {"type": "object"},
tiernocfb07c62018-05-10 18:30:51 +0200526 "description": description_schema,
tierno7adaeb02019-12-17 16:46:12 +0000527 # The folowing are deprecated. Maintanied for backward compatibility
tiernocb83c942018-09-24 17:28:13 +0200528 "dpid": dpid_Schema,
tierno0f98af52018-03-19 10:28:22 +0100529 "ip": ip_schema,
530 "port": port_schema,
tierno0f98af52018-03-19 10:28:22 +0100531 "version": {"type": "string", "minLength": 1, "maxLength": 12},
tierno0f98af52018-03-19 10:28:22 +0100532}
533sdn_new_schema = {
534 "title": "sdn controller information schema",
535 "$schema": "http://json-schema.org/draft-04/schema#",
536 "type": "object",
537 "properties": sdn_properties,
tierno7adaeb02019-12-17 16:46:12 +0000538 "required": ["name", 'type'],
tierno0f98af52018-03-19 10:28:22 +0100539 "additionalProperties": False
540}
541sdn_edit_schema = {
542 "title": "sdn controller update information schema",
543 "$schema": "http://json-schema.org/draft-04/schema#",
544 "type": "object",
545 "properties": sdn_properties,
tiernocfb07c62018-05-10 18:30:51 +0200546 # "required": ["name", "port", 'ip', 'dpid', 'type'],
tierno0f98af52018-03-19 10:28:22 +0100547 "additionalProperties": False
548}
549sdn_port_mapping_schema = {
550 "$schema": "http://json-schema.org/draft-04/schema#",
551 "title": "sdn port mapping information schema",
552 "type": "array",
553 "items": {
554 "type": "object",
555 "properties": {
tiernofd160572019-01-21 10:41:37 +0000556 "compute_node": shortname_schema,
tierno0f98af52018-03-19 10:28:22 +0100557 "ports": {
558 "type": "array",
559 "items": {
560 "type": "object",
561 "properties": {
tierno42fce592018-11-02 09:23:43 +0100562 "pci": pci_extended_schema,
tiernofd160572019-01-21 10:41:37 +0000563 "switch_port": shortname_schema,
tierno0f98af52018-03-19 10:28:22 +0100564 "switch_mac": mac_schema
565 },
566 "required": ["pci"]
567 }
568 }
569 },
570 "required": ["compute_node", "ports"]
571 }
572}
573sdn_external_port_schema = {
574 "$schema": "http://json-schema.org/draft-04/schema#",
tiernocd54a4a2018-09-12 16:40:35 +0200575 "title": "External port information",
tierno0f98af52018-03-19 10:28:22 +0100576 "type": "object",
577 "properties": {
578 "port": {"type": "string", "minLength": 1, "maxLength": 60},
579 "vlan": vlan_schema,
580 "mac": mac_schema
581 },
582 "required": ["port"]
583}
584
delacruzramofe598fe2019-10-23 18:25:11 +0200585# K8s Clusters
586k8scluster_nets_schema = {
587 "title": "k8scluster nets input schema",
588 "$schema": "http://json-schema.org/draft-04/schema#",
589 "type": "object",
tierno8c81ab02020-02-07 10:18:30 +0000590 "patternProperties": {".": {"oneOf": [name_schema, null_schema]}},
delacruzramofe598fe2019-10-23 18:25:11 +0200591 "minProperties": 1,
592 "additionalProperties": False
593}
594k8scluster_new_schema = {
595 "title": "k8scluster creation input schema",
596 "$schema": "http://json-schema.org/draft-04/schema#",
597 "type": "object",
598 "properties": {
599 "schema_version": schema_version,
600 "schema_type": schema_type,
601 "name": name_schema,
602 "description": description_schema,
603 "credentials": object_schema,
604 "vim_account": id_schema,
605 "k8s_version": string_schema,
606 "nets": k8scluster_nets_schema,
607 "namespace": name_schema,
608 "cni": nameshort_list_schema,
609 },
610 "required": ["name", "credentials", "vim_account", "k8s_version", "nets"],
611 "additionalProperties": False
612}
613k8scluster_edit_schema = {
614 "title": "vim_account edit input schema",
615 "$schema": "http://json-schema.org/draft-04/schema#",
616 "type": "object",
617 "properties": {
618 "name": name_schema,
619 "description": description_schema,
620 "credentials": object_schema,
621 "vim_account": id_schema,
622 "k8s_version": string_schema,
623 "nets": k8scluster_nets_schema,
624 "namespace": name_schema,
625 "cni": nameshort_list_schema,
626 },
627 "additionalProperties": False
628}
629
630# K8s Repos
tierno332e0802019-12-03 23:50:49 +0000631k8srepo_types = {"enum": ["helm-chart", "juju-bundle"]}
delacruzramofe598fe2019-10-23 18:25:11 +0200632k8srepo_properties = {
633 "name": name_schema,
634 "description": description_schema,
635 "type": k8srepo_types,
636 "url": description_schema,
637}
638k8srepo_new_schema = {
639 "title": "k8scluster creation input schema",
640 "$schema": "http://json-schema.org/draft-04/schema#",
641 "type": "object",
642 "properties": k8srepo_properties,
643 "required": ["name", "type", "url"],
644 "additionalProperties": False
645}
646k8srepo_edit_schema = {
647 "title": "vim_account edit input schema",
648 "$schema": "http://json-schema.org/draft-04/schema#",
649 "type": "object",
650 "properties": k8srepo_properties,
651 "additionalProperties": False
652}
653
tiernocb83c942018-09-24 17:28:13 +0200654# PDUs
655pdu_interface = {
656 "type": "object",
657 "properties": {
tiernofd160572019-01-21 10:41:37 +0000658 "name": shortname_schema,
tiernocb83c942018-09-24 17:28:13 +0200659 "mgmt": bool_schema,
660 "type": {"enum": ["overlay", 'underlay']},
tierno36ec8602018-11-02 17:27:11 +0100661 "ip-address": ip_schema,
tiernocb83c942018-09-24 17:28:13 +0200662 # TODO, add user, password, ssh-key
tierno36ec8602018-11-02 17:27:11 +0100663 "mac-address": mac_schema,
tiernofd160572019-01-21 10:41:37 +0000664 "vim-network-name": shortname_schema, # interface is connected to one vim network, or switch port
665 "vim-network-id": shortname_schema,
tierno36ec8602018-11-02 17:27:11 +0100666 # # provide this in case SDN assist must deal with this interface
667 # "switch-dpid": dpid_Schema,
tiernofd160572019-01-21 10:41:37 +0000668 # "switch-port": shortname_schema,
669 # "switch-mac": shortname_schema,
tierno36ec8602018-11-02 17:27:11 +0100670 # "switch-vlan": vlan_schema,
tiernocb83c942018-09-24 17:28:13 +0200671 },
tierno36ec8602018-11-02 17:27:11 +0100672 "required": ["name", "mgmt", "ip-address"],
tiernocb83c942018-09-24 17:28:13 +0200673 "additionalProperties": False
tiernocd54a4a2018-09-12 16:40:35 +0200674}
tiernocb83c942018-09-24 17:28:13 +0200675pdu_new_schema = {
676 "title": "pdu creation input schema",
677 "$schema": "http://json-schema.org/draft-04/schema#",
678 "type": "object",
679 "properties": {
tiernofd160572019-01-21 10:41:37 +0000680 "name": shortname_schema,
681 "type": shortname_schema,
tiernocb83c942018-09-24 17:28:13 +0200682 "description": description_schema,
683 "shared": bool_schema,
684 "vims": nameshort_list_schema,
685 "vim_accounts": nameshort_list_schema,
686 "interfaces": {
687 "type": "array",
tierno36ec8602018-11-02 17:27:11 +0100688 "items": pdu_interface,
tiernocb83c942018-09-24 17:28:13 +0200689 "minItems": 1
690 }
691 },
692 "required": ["name", "type", "interfaces"],
693 "additionalProperties": False
694}
tiernocb83c942018-09-24 17:28:13 +0200695pdu_edit_schema = {
696 "title": "pdu edit input schema",
697 "$schema": "http://json-schema.org/draft-04/schema#",
698 "type": "object",
699 "properties": {
tiernofd160572019-01-21 10:41:37 +0000700 "name": shortname_schema,
701 "type": shortname_schema,
tiernocb83c942018-09-24 17:28:13 +0200702 "description": description_schema,
703 "shared": bool_schema,
garciadeblas84bdd552018-11-30 22:59:45 +0100704 "vims": {"oneOf": [array_edition_schema, nameshort_list_schema]},
705 "vim_accounts": {"oneOf": [array_edition_schema, nameshort_list_schema]},
706 "interfaces": {"oneOf": [
tiernocb83c942018-09-24 17:28:13 +0200707 array_edition_schema,
708 {
709 "type": "array",
tierno36ec8602018-11-02 17:27:11 +0100710 "items": pdu_interface,
tiernocb83c942018-09-24 17:28:13 +0200711 "minItems": 1
712 }
713 ]}
714 },
715 "additionalProperties": False,
716 "minProperties": 1
717}
718
delacruzramo271d2002019-12-02 21:00:37 +0100719# VNF PKG OPERATIONS
720vnfpkgop_new_schema = {
721 "title": "VNF PKG operation creation input schema",
722 "$schema": "http://json-schema.org/draft-04/schema#",
723 "type": "object",
724 "properties": {
725 "lcmOperationType": string_schema,
726 "vnfPkgId": id_schema,
727 "kdu_name": name_schema,
728 "primitive": name_schema,
729 "primitive_params": {"type": "object"},
730 },
731 "required": ["lcmOperationType", "vnfPkgId", "kdu_name", "primitive", "primitive_params"],
732 "additionalProperties": False
733}
734
tiernocb83c942018-09-24 17:28:13 +0200735# USERS
tiernocf042d32019-06-13 09:06:40 +0000736project_role_mappings = {
737 "title": "list pf projects/roles",
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100738 "$schema": "http://json-schema.org/draft-04/schema#",
tiernocf042d32019-06-13 09:06:40 +0000739 "type": "array",
740 "items": {
741 "type": "object",
742 "properties": {
743 "project": shortname_schema,
744 "role": shortname_schema
745 },
746 "required": ["project", "role"],
747 "additionalProperties": False
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100748 },
tiernocf042d32019-06-13 09:06:40 +0000749 "minItems": 1
750}
751project_role_mappings_optional = {
752 "title": "list of projects/roles or projects only",
753 "$schema": "http://json-schema.org/draft-04/schema#",
754 "type": "array",
755 "items": {
756 "type": "object",
757 "properties": {
758 "project": shortname_schema,
759 "role": shortname_schema
760 },
761 "required": ["project"],
762 "additionalProperties": False
763 },
764 "minItems": 1
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100765}
tiernocd54a4a2018-09-12 16:40:35 +0200766user_new_schema = {
767 "$schema": "http://json-schema.org/draft-04/schema#",
768 "title": "New user schema",
769 "type": "object",
770 "properties": {
tiernofd160572019-01-21 10:41:37 +0000771 "username": shortname_schema,
tiernoad6d5332020-02-19 14:29:49 +0000772 "domain_name": shortname_schema,
tiernocd54a4a2018-09-12 16:40:35 +0200773 "password": passwd_schema,
tiernocb83c942018-09-24 17:28:13 +0200774 "projects": nameshort_list_schema,
tiernocf042d32019-06-13 09:06:40 +0000775 "project_role_mappings": project_role_mappings,
tiernocd54a4a2018-09-12 16:40:35 +0200776 },
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100777 "required": ["username", "password"],
tiernocd54a4a2018-09-12 16:40:35 +0200778 "additionalProperties": False
779}
780user_edit_schema = {
781 "$schema": "http://json-schema.org/draft-04/schema#",
782 "title": "User edit schema for administrators",
783 "type": "object",
784 "properties": {
785 "password": passwd_schema,
delacruzramoc061f562019-04-05 11:00:02 +0200786 "username": shortname_schema, # To allow User Name modification
tiernocd54a4a2018-09-12 16:40:35 +0200787 "projects": {
garciadeblas84bdd552018-11-30 22:59:45 +0100788 "oneOf": [
tiernocb83c942018-09-24 17:28:13 +0200789 nameshort_list_schema,
tiernocd54a4a2018-09-12 16:40:35 +0200790 array_edition_schema
791 ]
792 },
tiernocf042d32019-06-13 09:06:40 +0000793 "project_role_mappings": project_role_mappings,
794 "add_project_role_mappings": project_role_mappings,
795 "remove_project_role_mappings": project_role_mappings_optional,
tiernocb83c942018-09-24 17:28:13 +0200796 },
797 "minProperties": 1,
798 "additionalProperties": False
tiernocd54a4a2018-09-12 16:40:35 +0200799}
800
801# PROJECTS
delacruzramofe598fe2019-10-23 18:25:11 +0200802topics_with_quota = ["vnfds", "nsds", "nsts", "pdus", "nsrs", "nsis", "vim_accounts", "wim_accounts", "sdns",
803 "k8sclusters", "k8srepos"]
tiernocd54a4a2018-09-12 16:40:35 +0200804project_new_schema = {
805 "$schema": "http://json-schema.org/draft-04/schema#",
806 "title": "New project schema for administrators",
807 "type": "object",
808 "properties": {
tiernofd160572019-01-21 10:41:37 +0000809 "name": shortname_schema,
tiernocd54a4a2018-09-12 16:40:35 +0200810 "admin": bool_schema,
tiernoad6d5332020-02-19 14:29:49 +0000811 "domain_name": shortname_schema,
delacruzramo32bab472019-09-13 12:24:22 +0200812 "quotas": {
813 "type": "object",
814 "properties": {topic: integer0_schema for topic in topics_with_quota},
815 "additionalProperties": False
816 },
tiernocd54a4a2018-09-12 16:40:35 +0200817 },
818 "required": ["name"],
819 "additionalProperties": False
820}
821project_edit_schema = {
822 "$schema": "http://json-schema.org/draft-04/schema#",
823 "title": "Project edit schema for administrators",
824 "type": "object",
825 "properties": {
826 "admin": bool_schema,
delacruzramoc061f562019-04-05 11:00:02 +0200827 "name": shortname_schema, # To allow Project Name modification
delacruzramo32bab472019-09-13 12:24:22 +0200828 "quotas": {
829 "type": "object",
830 "properties": {topic: {"oneOf": [integer0_schema, null_schema]} for topic in topics_with_quota},
831 "additionalProperties": False
832 },
tiernocd54a4a2018-09-12 16:40:35 +0200833 },
834 "additionalProperties": False,
835 "minProperties": 1
836}
837
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100838# ROLES
839roles_new_schema = {
840 "$schema": "http://json-schema.org/draft-04/schema#",
841 "title": "New role schema for administrators",
842 "type": "object",
843 "properties": {
844 "name": shortname_schema,
tierno1f029d82019-06-13 22:37:04 +0000845 "permissions": {
846 "type": "object",
847 "patternProperties": {
848 ".": bool_schema,
849 },
850 # "minProperties": 1,
851 }
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100852 },
tierno1f029d82019-06-13 22:37:04 +0000853 "required": ["name"],
854 "additionalProperties": False
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100855}
856roles_edit_schema = {
857 "$schema": "http://json-schema.org/draft-04/schema#",
858 "title": "Roles edit schema for administrators",
859 "type": "object",
860 "properties": {
tierno1f029d82019-06-13 22:37:04 +0000861 "name": shortname_schema,
862 "permissions": {
863 "type": "object",
864 "patternProperties": {
865 ".": {
866 "oneOf": [bool_schema, null_schema]
867 }
868 },
869 # "minProperties": 1,
870 }
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100871 },
tierno1f029d82019-06-13 22:37:04 +0000872 "additionalProperties": False,
Eduardo Sousa5c01e192019-05-08 02:35:47 +0100873 "minProperties": 1
874}
875
tiernocd54a4a2018-09-12 16:40:35 +0200876# GLOBAL SCHEMAS
tierno0f98af52018-03-19 10:28:22 +0100877
878nbi_new_input_schemas = {
tiernocd54a4a2018-09-12 16:40:35 +0200879 "users": user_new_schema,
880 "projects": project_new_schema,
tierno09c073e2018-04-26 13:36:48 +0200881 "vim_accounts": vim_account_new_schema,
tierno65acb4d2018-04-06 16:42:40 +0200882 "sdns": sdn_new_schema,
883 "ns_instantiate": ns_instantiate,
884 "ns_action": ns_action,
tiernocb83c942018-09-24 17:28:13 +0200885 "ns_scale": ns_scale,
886 "pdus": pdu_new_schema,
tierno0f98af52018-03-19 10:28:22 +0100887}
888
889nbi_edit_input_schemas = {
tiernocd54a4a2018-09-12 16:40:35 +0200890 "users": user_edit_schema,
891 "projects": project_edit_schema,
tierno09c073e2018-04-26 13:36:48 +0200892 "vim_accounts": vim_account_edit_schema,
tiernocb83c942018-09-24 17:28:13 +0200893 "sdns": sdn_edit_schema,
894 "pdus": pdu_edit_schema,
tierno0f98af52018-03-19 10:28:22 +0100895}
896
Felipe Vicens07f31722018-10-29 15:16:44 +0100897# NETSLICE SCHEMAS
tierno032916c2019-03-22 13:27:12 +0000898nsi_subnet_instantiate = deepcopy(ns_instantiate)
899nsi_subnet_instantiate["title"] = "netslice subnet instantiation params input schema"
900nsi_subnet_instantiate["properties"]["id"] = name_schema
901del nsi_subnet_instantiate["required"]
garciadeblasdaf8cc52018-11-30 14:17:20 +0100902
903nsi_vld_instantiate = {
904 "title": "netslice vld instantiation params input schema",
905 "$schema": "http://json-schema.org/draft-04/schema#",
906 "type": "object",
907 "properties": {
908 "name": string_schema,
909 "vim-network-name": {"OneOf": [string_schema, object_schema]},
gcalvino17d5b732018-12-17 16:26:21 +0100910 "vim-network-id": {"OneOf": [string_schema, object_schema]},
garciadeblasdaf8cc52018-11-30 14:17:20 +0100911 "ip-profile": object_schema,
912 },
delacruzramoc061f562019-04-05 11:00:02 +0200913 "required": ["name"],
garciadeblasdaf8cc52018-11-30 14:17:20 +0100914 "additionalProperties": False
915}
916
Felipe Vicens07f31722018-10-29 15:16:44 +0100917nsi_instantiate = {
918 "title": "netslice action instantiate input schema",
919 "$schema": "http://json-schema.org/draft-04/schema#",
920 "type": "object",
921 "properties": {
922 "lcmOperationType": string_schema,
Felipe Vicens126af572019-06-05 19:13:04 +0200923 "netsliceInstanceId": id_schema,
Felipe Vicens07f31722018-10-29 15:16:44 +0100924 "nsiName": name_schema,
tierno4f9d4ae2019-03-20 17:24:11 +0000925 "nsiDescription": {"oneOf": [description_schema, null_schema]},
tierno9e5eea32018-11-29 09:42:09 +0000926 "nstId": string_schema,
Felipe Vicens07f31722018-10-29 15:16:44 +0100927 "vimAccountId": id_schema,
tiernoa8186a52020-01-14 15:54:48 +0000928 "timeout_nsi_deploy": integer1_schema,
Felipe Vicens07f31722018-10-29 15:16:44 +0100929 "ssh_keys": {"type": "string"},
930 "nsi_id": id_schema,
tierno032916c2019-03-22 13:27:12 +0000931 "additionalParamsForNsi": object_schema,
garciadeblasdaf8cc52018-11-30 14:17:20 +0100932 "netslice-subnet": {
Felipe Vicens07f31722018-10-29 15:16:44 +0100933 "type": "array",
934 "minItems": 1,
tierno032916c2019-03-22 13:27:12 +0000935 "items": nsi_subnet_instantiate
garciadeblasdaf8cc52018-11-30 14:17:20 +0100936 },
937 "netslice-vld": {
938 "type": "array",
939 "minItems": 1,
940 "items": nsi_vld_instantiate
Felipe Vicens07f31722018-10-29 15:16:44 +0100941 },
942 },
Felipe Vicensc8bbaaa2018-12-01 04:42:40 +0100943 "required": ["nsiName", "nstId", "vimAccountId"],
Felipe Vicens07f31722018-10-29 15:16:44 +0100944 "additionalProperties": False
945}
946
947nsi_action = {
948
949}
950
951nsi_terminate = {
delacruzramoc061f562019-04-05 11:00:02 +0200952
Felipe Vicens07f31722018-10-29 15:16:44 +0100953}
954
tierno0f98af52018-03-19 10:28:22 +0100955
956class ValidationError(Exception):
tierno36ec8602018-11-02 17:27:11 +0100957 def __init__(self, message, http_code=HTTPStatus.UNPROCESSABLE_ENTITY):
958 self.http_code = http_code
959 Exception.__init__(self, message)
tierno0f98af52018-03-19 10:28:22 +0100960
961
tiernob24258a2018-10-04 18:39:49 +0200962def validate_input(indata, schema_to_use):
tierno0f98af52018-03-19 10:28:22 +0100963 """
tiernocd54a4a2018-09-12 16:40:35 +0200964 Validates input data against json schema
tierno0f98af52018-03-19 10:28:22 +0100965 :param indata: user input data. Should be a dictionary
tiernob24258a2018-10-04 18:39:49 +0200966 :param schema_to_use: jsonschema to test
967 :return: None if ok, raises ValidationError exception on error
tierno0f98af52018-03-19 10:28:22 +0100968 """
969 try:
tierno0f98af52018-03-19 10:28:22 +0100970 if schema_to_use:
971 js_v(indata, schema_to_use)
972 return None
973 except js_e.ValidationError as e:
974 if e.path:
tierno0da52252018-06-27 15:47:22 +0200975 error_pos = "at '" + ":".join(map(str, e.path)) + "'"
tierno0f98af52018-03-19 10:28:22 +0100976 else:
977 error_pos = ""
tierno441dbbf2018-07-10 12:52:48 +0200978 raise ValidationError("Format error {} '{}' ".format(error_pos, e.message))
tierno36ec8602018-11-02 17:27:11 +0100979 except js_e.SchemaError:
980 raise ValidationError("Bad json schema {}".format(schema_to_use), http_code=HTTPStatus.INTERNAL_SERVER_ERROR)
delacruzramoc061f562019-04-05 11:00:02 +0200981
982
983def is_valid_uuid(x):
984 """
985 Test for a valid UUID
986 :param x: string to test
987 :return: True if x is a valid uuid, False otherwise
988 """
989 try:
990 if UUID(x):
991 return True
tiernobdebce92019-07-01 15:36:49 +0000992 except (TypeError, ValueError, AttributeError):
delacruzramoc061f562019-04-05 11:00:02 +0200993 return False