Feature 10909: Heal operation for VDU
[osm/NBI.git] / osm_nbi / validation.py
1 # -*- coding: utf-8 -*-
2
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
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
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
20
21 __author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
22 __version__ = "0.1"
23 version_date = "Mar 2018"
24
25 """
26 Validator of input data using JSON schemas for those items that not contains an OSM yang information model
27 """
28
29 # Basis schemas
30 patern_name = "^[ -~]+$"
31 shortname_schema = {
32 "type": "string",
33 "minLength": 1,
34 "maxLength": 60,
35 "pattern": "^[^,;()\\.\\$'\"]+$",
36 }
37 passwd_schema = {"type": "string", "minLength": 1, "maxLength": 60}
38 name_schema = {
39 "type": "string",
40 "minLength": 1,
41 "maxLength": 255,
42 "pattern": "^[^,;()'\"]+$",
43 }
44 string_schema = {"type": "string", "minLength": 1, "maxLength": 255}
45 xml_text_schema = {
46 "type": "string",
47 "minLength": 1,
48 "maxLength": 1000,
49 "pattern": "^[^']+$",
50 }
51 description_schema = {
52 "type": ["string", "null"],
53 "maxLength": 255,
54 "pattern": "^[^'\"]+$",
55 }
56 long_description_schema = {
57 "type": ["string", "null"],
58 "maxLength": 3000,
59 "pattern": "^[^'\"]+$",
60 }
61 id_schema_fake = {"type": "string", "minLength": 2, "maxLength": 36}
62 bool_schema = {"type": "boolean"}
63 null_schema = {"type": "null"}
64 # "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}$"
65 id_schema = {
66 "type": "string",
67 "pattern": "^[a-fA-F0-9]{8}(-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}$",
68 }
69 time_schema = {
70 "type": "string",
71 "pattern": "^[0-9]{4}-[0-1][0-9]-[0-3][0-9]T[0-2][0-9]([0-5]:){2}",
72 }
73 pci_schema = {
74 "type": "string",
75 "pattern": "^[0-9a-fA-F]{4}(:[0-9a-fA-F]{2}){2}\\.[0-9a-fA-F]$",
76 }
77 # allows [] for wildcards. For that reason huge length limit is set
78 pci_extended_schema = {"type": "string", "pattern": "^[0-9a-fA-F.:-\\[\\]]{12,40}$"}
79 http_schema = {"type": "string", "pattern": "^(https?|http)://[^'\"=]+$"}
80 bandwidth_schema = {"type": "string", "pattern": "^[0-9]+ *([MG]bps)?$"}
81 memory_schema = {"type": "string", "pattern": "^[0-9]+ *([MG]i?[Bb])?$"}
82 integer0_schema = {"type": "integer", "minimum": 0}
83 integer1_schema = {"type": "integer", "minimum": 1}
84 path_schema = {"type": "string", "pattern": "^(\\.){0,2}(/[^/\"':{}\\(\\)]+)+$"}
85 vlan_schema = {"type": "integer", "minimum": 1, "maximum": 4095}
86 vlan1000_schema = {"type": "integer", "minimum": 1000, "maximum": 4095}
87 mac_schema = {
88 "type": "string",
89 "pattern": "^[0-9a-fA-F][02468aceACE](:[0-9a-fA-F]{2}){5}$",
90 } # must be unicast: LSB bit of MSB byte ==0
91 dpid_Schema = {"type": "string", "pattern": "^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){7}$"}
92 # mac_schema={"type":"string", "pattern":"^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$"}
93 ip_schema = {
94 "type": "string",
95 "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]?)$",
96 }
97 ipv6_schema = {
98 "type": "string",
99 "pattern": "(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))",
100 }
101 ip_prefix_schema = {
102 "type": "string",
103 "pattern": "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}"
104 "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/(30|[12]?[0-9])$",
105 }
106 port_schema = {"type": "integer", "minimum": 1, "maximum": 65534}
107 object_schema = {"type": "object"}
108 schema_version_2 = {"type": "integer", "minimum": 2, "maximum": 2}
109 # schema_version_string={"type":"string","enum": ["0.1", "2", "0.2", "3", "0.3"]}
110 log_level_schema = {
111 "type": "string",
112 "enum": ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
113 }
114 checksum_schema = {"type": "string", "pattern": "^[0-9a-fA-F]{32}$"}
115 size_schema = {"type": "integer", "minimum": 1, "maximum": 100}
116 array_edition_schema = {
117 "type": "object",
118 "patternProperties": {"^\\$": {}},
119 "additionalProperties": False,
120 "minProperties": 1,
121 }
122 nameshort_list_schema = {
123 "type": "array",
124 "minItems": 1,
125 "items": shortname_schema,
126 }
127
128 description_list_schema = {
129 "type": "array",
130 "minItems": 1,
131 "items": description_schema,
132 }
133
134 ns_instantiate_vdu = {
135 "title": "ns action instantiate input schema for vdu",
136 "$schema": "http://json-schema.org/draft-04/schema#",
137 "type": "object",
138 "properties": {
139 "id": name_schema,
140 "volume": {
141 "type": "array",
142 "minItems": 1,
143 "items": {
144 "type": "object",
145 "properties": {
146 "name": name_schema,
147 "vim-volume-id": name_schema,
148 },
149 "required": ["name", "vim-volume-id"],
150 "additionalProperties": False,
151 },
152 },
153 "interface": {
154 "type": "array",
155 "minItems": 1,
156 "items": {
157 "type": "object",
158 "properties": {
159 "name": name_schema,
160 "ip-address": {"oneOf": [ip_schema, ipv6_schema]},
161 "mac-address": mac_schema,
162 "floating-ip-required": bool_schema,
163 },
164 "required": ["name"],
165 "additionalProperties": False,
166 },
167 },
168 },
169 "required": ["id"],
170 "additionalProperties": False,
171 }
172
173 ip_profile_dns_schema = {
174 "type": "array",
175 "minItems": 1,
176 "items": {
177 "type": "object",
178 "properties": {
179 "address": {"oneOf": [ip_schema, ipv6_schema]},
180 },
181 "required": ["address"],
182 "additionalProperties": False,
183 },
184 }
185
186 ip_profile_dhcp_schema = {
187 "type": "object",
188 "properties": {
189 "enabled": {"type": "boolean"},
190 "count": integer1_schema,
191 "start-address": ip_schema,
192 },
193 "additionalProperties": False,
194 }
195
196 ip_profile_schema = {
197 "title": "ip profile validation schema",
198 "$schema": "http://json-schema.org/draft-04/schema#",
199 "type": "object",
200 "properties": {
201 "ip-version": {"enum": ["ipv4", "ipv6"]},
202 "subnet-address": ip_prefix_schema,
203 "gateway-address": ip_schema,
204 "dns-server": ip_profile_dns_schema,
205 "dhcp-params": ip_profile_dhcp_schema,
206 },
207 }
208
209 ip_profile_update_schema = {
210 "title": "ip profile validation schema",
211 "$schema": "http://json-schema.org/draft-04/schema#",
212 "type": "object",
213 "properties": {
214 "ip-version": {"enum": ["ipv4", "ipv6"]},
215 "subnet-address": {"oneOf": [null_schema, ip_prefix_schema]},
216 "gateway-address": {"oneOf": [null_schema, ip_schema]},
217 "dns-server": {"oneOf": [null_schema, ip_profile_dns_schema]},
218 "dhcp-params": {"oneOf": [null_schema, ip_profile_dhcp_schema]},
219 },
220 "additionalProperties": False,
221 }
222
223 provider_network_schema = {
224 "title": "provider network validation schema",
225 "$schema": "http://json-schema.org/draft-04/schema#",
226 "type": "object",
227 "properties": {
228 "physical-network": name_schema,
229 "segmentation-id": name_schema,
230 "sdn-ports": { # external ports to append to the SDN-assist network
231 "type": "array",
232 "items": {
233 "type": "object",
234 "properties": {
235 "switch_id": shortname_schema,
236 "switch_port": shortname_schema,
237 "mac_address": mac_schema,
238 "vlan": vlan_schema,
239 },
240 "additionalProperties": True,
241 },
242 },
243 "network-type": shortname_schema,
244 },
245 "additionalProperties": True,
246 }
247
248 ns_instantiate_internal_vld = {
249 "title": "ns action instantiate input schema for vld",
250 "$schema": "http://json-schema.org/draft-04/schema#",
251 "type": "object",
252 "properties": {
253 "name": name_schema,
254 "vim-network-name": name_schema,
255 "vim-network-id": name_schema,
256 "ip-profile": ip_profile_update_schema,
257 "provider-network": provider_network_schema,
258 "internal-connection-point": {
259 "type": "array",
260 "minItems": 1,
261 "items": {
262 "type": "object",
263 "properties": {
264 "id-ref": name_schema,
265 "ip-address": ip_schema,
266 # "mac-address": mac_schema,
267 },
268 "required": ["id-ref"],
269 "minProperties": 2,
270 "additionalProperties": False,
271 },
272 },
273 },
274 "required": ["name"],
275 "minProperties": 2,
276 "additionalProperties": False,
277 }
278
279 additional_params_for_vnf = {
280 "type": "array",
281 "items": {
282 "type": "object",
283 "properties": {
284 "member-vnf-index": name_schema,
285 "additionalParams": object_schema,
286 "k8s-namespace": name_schema,
287 "config-units": integer1_schema, # number of configuration units of this vnf, by default 1
288 "additionalParamsForVdu": {
289 "type": "array",
290 "items": {
291 "type": "object",
292 "properties": {
293 "vdu_id": name_schema,
294 "additionalParams": object_schema,
295 "config-units": integer1_schema, # number of configuration units of this vdu, by default 1
296 },
297 "required": ["vdu_id"],
298 "minProperties": 2,
299 "additionalProperties": False,
300 },
301 },
302 "additionalParamsForKdu": {
303 "type": "array",
304 "items": {
305 "type": "object",
306 "properties": {
307 "kdu_name": name_schema,
308 "additionalParams": object_schema,
309 "kdu_model": name_schema,
310 "k8s-namespace": name_schema,
311 "config-units": integer1_schema, # number of configuration units of this knf, by default 1
312 "kdu-deployment-name": name_schema,
313 },
314 "required": ["kdu_name"],
315 "minProperties": 2,
316 "additionalProperties": False,
317 },
318 },
319 "affinity-or-anti-affinity-group": {
320 "type": "array",
321 "items": {
322 "type": "object",
323 "properties": {
324 "id": name_schema,
325 "vim-affinity-group-id": name_schema,
326 },
327 "required": ["id"],
328 "minProperties": 2,
329 "additionalProperties": False,
330 },
331 },
332 },
333 "required": ["member-vnf-index"],
334 "minProperties": 2,
335 "additionalProperties": False,
336 },
337 }
338
339 ns_instantiate = {
340 "title": "ns action instantiate input schema",
341 "$schema": "http://json-schema.org/draft-04/schema#",
342 "type": "object",
343 "properties": {
344 "lcmOperationType": string_schema,
345 "nsInstanceId": id_schema,
346 "netsliceInstanceId": id_schema,
347 "nsName": name_schema,
348 "nsDescription": {"oneOf": [description_schema, null_schema]},
349 "nsdId": id_schema,
350 "vimAccountId": id_schema,
351 "wimAccountId": {"oneOf": [id_schema, bool_schema, null_schema]},
352 "placement-engine": string_schema,
353 "placement-constraints": object_schema,
354 "additionalParamsForNs": object_schema,
355 "additionalParamsForVnf": additional_params_for_vnf,
356 "config-units": integer1_schema, # number of configuration units of this ns, by default 1
357 "k8s-namespace": name_schema,
358 "ssh_keys": {"type": "array", "items": {"type": "string"}},
359 "timeout_ns_deploy": integer1_schema,
360 "nsr_id": id_schema,
361 "vduImage": name_schema,
362 "vnf": {
363 "type": "array",
364 "minItems": 1,
365 "items": {
366 "type": "object",
367 "properties": {
368 "member-vnf-index": name_schema,
369 "vimAccountId": id_schema,
370 "vdu": {
371 "type": "array",
372 "minItems": 1,
373 "items": ns_instantiate_vdu,
374 },
375 "internal-vld": {
376 "type": "array",
377 "minItems": 1,
378 "items": ns_instantiate_internal_vld,
379 },
380 },
381 "required": ["member-vnf-index"],
382 "minProperties": 2,
383 "additionalProperties": False,
384 },
385 },
386 "vld": {
387 "type": "array",
388 "minItems": 1,
389 "items": {
390 "type": "object",
391 "properties": {
392 "name": string_schema,
393 "vim-network-name": {"oneOf": [string_schema, object_schema]},
394 "vim-network-id": {"oneOf": [string_schema, object_schema]},
395 "ns-net": object_schema,
396 "wimAccountId": {"oneOf": [id_schema, bool_schema, null_schema]},
397 "ip-profile": object_schema,
398 "provider-network": provider_network_schema,
399 "vnfd-connection-point-ref": {
400 "type": "array",
401 "minItems": 1,
402 "items": {
403 "type": "object",
404 "properties": {
405 "member-vnf-index-ref": name_schema,
406 "vnfd-connection-point-ref": name_schema,
407 "ip-address": {"oneOf": [ip_schema, ipv6_schema]},
408 # "mac-address": mac_schema,
409 },
410 "required": [
411 "member-vnf-index-ref",
412 "vnfd-connection-point-ref",
413 ],
414 "minProperties": 3,
415 "additionalProperties": False,
416 },
417 },
418 },
419 "required": ["name"],
420 "additionalProperties": False,
421 },
422 },
423 },
424 "required": ["nsName", "nsdId", "vimAccountId"],
425 "additionalProperties": False,
426 }
427
428 ns_terminate = {
429 "title": "ns terminate input schema",
430 "$schema": "http://json-schema.org/draft-04/schema#",
431 "type": "object",
432 "properties": {
433 "lcmOperationType": string_schema,
434 "nsInstanceId": id_schema,
435 "autoremove": bool_schema,
436 "timeout_ns_terminate": integer1_schema,
437 "skip_terminate_primitives": bool_schema,
438 "netsliceInstanceId": id_schema,
439 },
440 "additionalProperties": False,
441 }
442
443 ns_update = {
444 "title": "ns update input schema",
445 "$schema": "http://json-schema.org/draft-04/schema#",
446 "type": "object",
447 "properties": {
448 "lcmOperationType": string_schema,
449 "nsInstanceId": id_schema,
450 "timeout_ns_update": integer1_schema,
451 "updateType": {
452 "enum": ["CHANGE_VNFPKG", "REMOVE_VNF", "MODIFY_VNF_INFORMATION"]
453 },
454 "modifyVnfInfoData": {
455 "type": "object",
456 "properties": {
457 "vnfInstanceId": id_schema,
458 "vnfdId": id_schema,
459 },
460 "required": ["vnfInstanceId", "vnfdId"],
461 },
462 "removeVnfInstanceId": id_schema,
463 "changeVnfPackageData": {
464 "type": "object",
465 "properties": {
466 "vnfInstanceId": id_schema,
467 "vnfdId": id_schema,
468 },
469 "required": ["vnfInstanceId", "vnfdId"],
470 },
471 },
472 "required": ["updateType"],
473 "additionalProperties": False,
474 }
475
476 ns_action = { # TODO for the moment it is only contemplated the vnfd primitive execution
477 "title": "ns action input schema",
478 "$schema": "http://json-schema.org/draft-04/schema#",
479 "type": "object",
480 "properties": {
481 "lcmOperationType": string_schema,
482 "nsInstanceId": id_schema,
483 "member_vnf_index": name_schema,
484 "vnf_member_index": name_schema, # TODO for backward compatibility. To remove in future
485 "vdu_id": name_schema,
486 "vdu_count_index": integer0_schema,
487 "kdu_name": name_schema,
488 "primitive": name_schema,
489 "timeout_ns_action": integer1_schema,
490 "primitive_params": {"type": "object"},
491 },
492 "required": ["primitive", "primitive_params"], # TODO add member_vnf_index
493 "additionalProperties": False,
494 }
495
496 ns_scale = { # TODO for the moment it is only VDU-scaling
497 "title": "ns scale input schema",
498 "$schema": "http://json-schema.org/draft-04/schema#",
499 "type": "object",
500 "properties": {
501 "lcmOperationType": string_schema,
502 "nsInstanceId": id_schema,
503 "scaleType": {"enum": ["SCALE_VNF"]},
504 "timeout_ns_scale": integer1_schema,
505 "scaleVnfData": {
506 "type": "object",
507 "properties": {
508 "vnfInstanceId": name_schema,
509 "scaleVnfType": {"enum": ["SCALE_OUT", "SCALE_IN"]},
510 "scaleByStepData": {
511 "type": "object",
512 "properties": {
513 "scaling-group-descriptor": name_schema,
514 "member-vnf-index": name_schema,
515 "scaling-policy": name_schema,
516 },
517 "required": ["scaling-group-descriptor", "member-vnf-index"],
518 "additionalProperties": False,
519 },
520 },
521 "required": ["scaleVnfType", "scaleByStepData"], # vnfInstanceId
522 "additionalProperties": False,
523 },
524 "scaleTime": time_schema,
525 },
526 "required": ["scaleType", "scaleVnfData"],
527 "additionalProperties": False,
528 }
529
530 ns_migrate = {
531 "title": "ns migrate input schema",
532 "$schema": "http://json-schema.org/draft-04/schema#",
533 "type": "object",
534 "properties": {
535 "lcmOperationType": string_schema,
536 "nsInstanceId": id_schema,
537 "vnfInstanceId": id_schema,
538 "migrateToHost": string_schema,
539 "vdu": {
540 "type": "object",
541 "properties": {
542 "vduId": name_schema,
543 "vduCountIndex": integer0_schema,
544 },
545 "required": ["vduId"],
546 "additionalProperties": False,
547 },
548 },
549 "required": ["vnfInstanceId"],
550 "additionalProperties": False
551 }
552
553 ns_heal = {
554 "title": "ns heal input schema",
555 "$schema": "http://json-schema.org/draft-04/schema#",
556 "type": "object",
557 "properties": {
558 "lcmOperationType": string_schema,
559 "nsInstanceId": id_schema,
560 "timeout_ns_heal": integer1_schema,
561 "healVnfData": {
562 "type": "array",
563 "items": {
564 "type": "object",
565 "properties": {
566 "vnfInstanceId": id_schema,
567 "cause": description_schema,
568 "additionalParams": {
569 "type": "object",
570 "properties": {
571 "run-day1": bool_schema,
572 "vdu": {
573 "type": "array",
574 "items": {
575 "type": "object",
576 "properties": {
577 "run-day1": bool_schema,
578 "vdu-id": name_schema,
579 "count-index": integer0_schema,
580 },
581 "required": ["vdu-id"],
582 "additionalProperties": False,
583 },
584 },
585 },
586 "additionalProperties": False,
587 },
588 },
589 "required": ["vnfInstanceId"],
590 "additionalProperties": False,
591 },
592 },
593 },
594 "required": ["healVnfData"],
595 "additionalProperties": False,
596 }
597
598 schema_version = {"type": "string", "enum": ["1.0"]}
599 schema_type = {"type": "string"}
600 vim_type = shortname_schema # {"enum": ["openstack", "openvim", "vmware", "opennebula", "aws", "azure", "fos"]}
601
602 vim_account_edit_schema = {
603 "title": "vim_account edit input schema",
604 "$schema": "http://json-schema.org/draft-04/schema#",
605 "type": "object",
606 "properties": {
607 "name": name_schema,
608 "description": description_schema,
609 "vim": name_schema,
610 "datacenter": name_schema,
611 "vim_type": vim_type,
612 "vim_url": description_schema,
613 # "vim_url_admin": description_schema,
614 # "vim_tenant": name_schema,
615 "vim_tenant_name": name_schema,
616 "vim_user": string_schema,
617 "vim_password": passwd_schema,
618 "vca": id_schema,
619 "config": {"type": "object"},
620 "prometheus-config": {"type": "object"},
621 },
622 "additionalProperties": False,
623 }
624
625 vim_account_new_schema = {
626 "title": "vim_account creation input schema",
627 "$schema": "http://json-schema.org/draft-04/schema#",
628 "type": "object",
629 "properties": {
630 "schema_version": schema_version,
631 "schema_type": schema_type,
632 "name": name_schema,
633 "description": description_schema,
634 "vim": name_schema,
635 "datacenter": name_schema,
636 "vim_type": vim_type,
637 "vim_url": description_schema,
638 # "vim_url_admin": description_schema,
639 # "vim_tenant": name_schema,
640 "vim_tenant_name": name_schema,
641 "vim_user": string_schema,
642 "vim_password": passwd_schema,
643 "vca": id_schema,
644 "config": {"type": "object"},
645 "prometheus-config": {"type": "object"},
646 },
647 "required": [
648 "name",
649 "vim_url",
650 "vim_type",
651 "vim_user",
652 "vim_password",
653 "vim_tenant_name",
654 ],
655 "additionalProperties": False,
656 }
657
658 wim_type = shortname_schema # {"enum": ["ietfl2vpn", "onos", "odl", "dynpac", "fake"]}
659
660 wim_account_edit_schema = {
661 "title": "wim_account edit input schema",
662 "$schema": "http://json-schema.org/draft-04/schema#",
663 "type": "object",
664 "properties": {
665 "name": name_schema,
666 "description": description_schema,
667 "wim": name_schema,
668 "wim_type": wim_type,
669 "wim_url": description_schema,
670 "user": string_schema,
671 "password": passwd_schema,
672 "config": {"type": "object"},
673 },
674 "additionalProperties": False,
675 }
676
677 wim_account_new_schema = {
678 "title": "wim_account creation input schema",
679 "$schema": "http://json-schema.org/draft-04/schema#",
680 "type": "object",
681 "properties": {
682 "schema_version": schema_version,
683 "schema_type": schema_type,
684 "name": name_schema,
685 "description": description_schema,
686 "wim": name_schema,
687 "wim_type": wim_type,
688 "wim_url": description_schema,
689 "user": string_schema,
690 "password": passwd_schema,
691 "config": {
692 "type": "object",
693 "patternProperties": {".": {"not": {"type": "null"}}},
694 },
695 },
696 "required": ["name", "wim_url", "wim_type"],
697 "additionalProperties": False,
698 }
699
700 sdn_properties = {
701 "name": name_schema,
702 "type": {"type": "string"},
703 "url": {"type": "string"},
704 "user": string_schema,
705 "password": passwd_schema,
706 "config": {"type": "object"},
707 "description": description_schema,
708 # The folowing are deprecated. Maintanied for backward compatibility
709 "dpid": dpid_Schema,
710 "ip": ip_schema,
711 "port": port_schema,
712 "version": {"type": "string", "minLength": 1, "maxLength": 12},
713 }
714 sdn_new_schema = {
715 "title": "sdn controller information schema",
716 "$schema": "http://json-schema.org/draft-04/schema#",
717 "type": "object",
718 "properties": sdn_properties,
719 "required": ["name", "type"],
720 "additionalProperties": False,
721 }
722 sdn_edit_schema = {
723 "title": "sdn controller update information schema",
724 "$schema": "http://json-schema.org/draft-04/schema#",
725 "type": "object",
726 "properties": sdn_properties,
727 # "required": ["name", "port", 'ip', 'dpid', 'type'],
728 "additionalProperties": False,
729 }
730 sdn_port_mapping_schema = {
731 "$schema": "http://json-schema.org/draft-04/schema#",
732 "title": "sdn port mapping information schema",
733 "type": "array",
734 "items": {
735 "type": "object",
736 "properties": {
737 "compute_node": shortname_schema,
738 "ports": {
739 "type": "array",
740 "items": {
741 "type": "object",
742 "properties": {
743 "pci": pci_extended_schema,
744 "switch_port": shortname_schema,
745 "switch_mac": mac_schema,
746 },
747 "required": ["pci"],
748 },
749 },
750 },
751 "required": ["compute_node", "ports"],
752 },
753 }
754 sdn_external_port_schema = {
755 "$schema": "http://json-schema.org/draft-04/schema#",
756 "title": "External port information",
757 "type": "object",
758 "properties": {
759 "port": {"type": "string", "minLength": 1, "maxLength": 60},
760 "vlan": vlan_schema,
761 "mac": mac_schema,
762 },
763 "required": ["port"],
764 }
765
766 # K8s Clusters
767 k8scluster_deploy_method_schema = {
768 "$schema": "http://json-schema.org/draft-04/schema#",
769 "title": "Deployment methods for K8s cluster",
770 "type": "object",
771 "properties": {
772 "helm-chart": {"type": "boolean"},
773 "juju-bundle": {"type": "boolean"},
774 "helm-chart-v3": {"type": "boolean"},
775 },
776 "additionalProperties": False,
777 "minProperties": 3,
778 }
779 k8scluster_nets_schema = {
780 "title": "k8scluster nets input schema",
781 "$schema": "http://json-schema.org/draft-04/schema#",
782 "type": "object",
783 "patternProperties": {".": {"oneOf": [name_schema, null_schema]}},
784 "minProperties": 1,
785 "additionalProperties": False,
786 }
787 k8scluster_new_schema = {
788 "title": "k8scluster creation input schema",
789 "$schema": "http://json-schema.org/draft-04/schema#",
790 "type": "object",
791 "properties": {
792 "schema_version": schema_version,
793 "schema_type": schema_type,
794 "name": name_schema,
795 "description": description_schema,
796 "credentials": object_schema,
797 "vim_account": id_schema,
798 "vca_id": id_schema,
799 "k8s_version": string_schema,
800 "nets": k8scluster_nets_schema,
801 "deployment_methods": k8scluster_deploy_method_schema,
802 "namespace": name_schema,
803 "cni": nameshort_list_schema,
804 },
805 "required": ["name", "credentials", "vim_account", "k8s_version", "nets"],
806 "additionalProperties": False,
807 }
808 k8scluster_edit_schema = {
809 "title": "vim_account edit input schema",
810 "$schema": "http://json-schema.org/draft-04/schema#",
811 "type": "object",
812 "properties": {
813 "name": name_schema,
814 "description": description_schema,
815 "credentials": object_schema,
816 "vim_account": id_schema,
817 "vca_id": id_schema,
818 "k8s_version": string_schema,
819 "nets": k8scluster_nets_schema,
820 "namespace": name_schema,
821 "cni": nameshort_list_schema,
822 },
823 "additionalProperties": False,
824 }
825
826 # VCA
827 vca_new_schema = {
828 "title": "vca creation input schema",
829 "$schema": "http://json-schema.org/draft-04/schema#",
830 "type": "object",
831 "properties": {
832 "schema_version": schema_version,
833 "schema_type": schema_type,
834 "name": name_schema,
835 "description": description_schema,
836 "endpoints": description_list_schema,
837 "user": string_schema,
838 "secret": passwd_schema,
839 "cacert": long_description_schema,
840 "lxd-cloud": shortname_schema,
841 "lxd-credentials": shortname_schema,
842 "k8s-cloud": shortname_schema,
843 "k8s-credentials": shortname_schema,
844 "model-config": object_schema,
845 },
846 "required": [
847 "name",
848 "endpoints",
849 "user",
850 "secret",
851 "cacert",
852 "lxd-cloud",
853 "lxd-credentials",
854 "k8s-cloud",
855 "k8s-credentials",
856 ],
857 "additionalProperties": False,
858 }
859 vca_edit_schema = {
860 "title": "vca creation input schema",
861 "$schema": "http://json-schema.org/draft-04/schema#",
862 "type": "object",
863 "properties": {
864 "name": name_schema,
865 "description": description_schema,
866 "endpoints": description_list_schema,
867 "port": integer1_schema,
868 "user": string_schema,
869 "secret": passwd_schema,
870 "cacert": long_description_schema,
871 "lxd-cloud": shortname_schema,
872 "lxd-credentials": shortname_schema,
873 "k8s-cloud": shortname_schema,
874 "k8s-credentials": shortname_schema,
875 "model-config": object_schema,
876 },
877 "additionalProperties": False,
878 }
879
880 # K8s Repos
881 k8srepo_types = {"enum": ["helm-chart", "juju-bundle"]}
882 k8srepo_properties = {
883 "name": name_schema,
884 "description": description_schema,
885 "type": k8srepo_types,
886 "url": description_schema,
887 }
888 k8srepo_new_schema = {
889 "title": "k8scluster creation input schema",
890 "$schema": "http://json-schema.org/draft-04/schema#",
891 "type": "object",
892 "properties": k8srepo_properties,
893 "required": ["name", "type", "url"],
894 "additionalProperties": False,
895 }
896 k8srepo_edit_schema = {
897 "title": "vim_account edit input schema",
898 "$schema": "http://json-schema.org/draft-04/schema#",
899 "type": "object",
900 "properties": k8srepo_properties,
901 "additionalProperties": False,
902 }
903
904 # OSM Repos
905 osmrepo_types = {"enum": ["osm"]}
906 osmrepo_properties = {
907 "name": name_schema,
908 "description": description_schema,
909 "type": osmrepo_types,
910 "url": description_schema
911 # "user": string_schema,
912 # "password": passwd_schema
913 }
914 osmrepo_new_schema = {
915 "title": "osm repo creation input schema",
916 "$schema": "http://json-schema.org/draft-04/schema#",
917 "type": "object",
918 "properties": osmrepo_properties,
919 "required": ["name", "type", "url"],
920 "additionalProperties": False,
921 }
922 osmrepo_edit_schema = {
923 "title": "osm repo edit input schema",
924 "$schema": "http://json-schema.org/draft-04/schema#",
925 "type": "object",
926 "properties": osmrepo_properties,
927 "additionalProperties": False,
928 }
929
930 # PDUs
931 pdu_interface = {
932 "type": "object",
933 "properties": {
934 "name": shortname_schema,
935 "mgmt": bool_schema,
936 "type": {"enum": ["overlay", "underlay"]},
937 "ip-address": {"oneOf": [ip_schema, ipv6_schema]},
938 # TODO, add user, password, ssh-key
939 "mac-address": mac_schema,
940 "vim-network-name": shortname_schema, # interface is connected to one vim network, or switch port
941 "vim-network-id": shortname_schema,
942 # # provide this in case SDN assist must deal with this interface
943 # "switch-dpid": dpid_Schema,
944 # "switch-port": shortname_schema,
945 # "switch-mac": shortname_schema,
946 # "switch-vlan": vlan_schema,
947 },
948 "required": ["name", "mgmt", "ip-address"],
949 "additionalProperties": False,
950 }
951 pdu_new_schema = {
952 "title": "pdu creation input schema",
953 "$schema": "http://json-schema.org/draft-04/schema#",
954 "type": "object",
955 "properties": {
956 "name": shortname_schema,
957 "type": shortname_schema,
958 "description": description_schema,
959 "shared": bool_schema,
960 "vims": nameshort_list_schema,
961 "vim_accounts": nameshort_list_schema,
962 "interfaces": {"type": "array", "items": pdu_interface, "minItems": 1},
963 },
964 "required": ["name", "type", "interfaces"],
965 "additionalProperties": False,
966 }
967 pdu_edit_schema = {
968 "title": "pdu edit input schema",
969 "$schema": "http://json-schema.org/draft-04/schema#",
970 "type": "object",
971 "properties": {
972 "name": shortname_schema,
973 "type": shortname_schema,
974 "description": description_schema,
975 "shared": bool_schema,
976 "vims": {"oneOf": [array_edition_schema, nameshort_list_schema]},
977 "vim_accounts": {"oneOf": [array_edition_schema, nameshort_list_schema]},
978 "interfaces": {
979 "oneOf": [
980 array_edition_schema,
981 {"type": "array", "items": pdu_interface, "minItems": 1},
982 ]
983 },
984 },
985 "additionalProperties": False,
986 "minProperties": 1,
987 }
988
989 # VNF PKG OPERATIONS
990 vnfpkgop_new_schema = {
991 "title": "VNF PKG operation creation input schema",
992 "$schema": "http://json-schema.org/draft-04/schema#",
993 "type": "object",
994 "properties": {
995 "lcmOperationType": string_schema,
996 "vnfPkgId": id_schema,
997 "kdu_name": name_schema,
998 "primitive": name_schema,
999 "primitive_params": {"type": "object"},
1000 },
1001 "required": [
1002 "lcmOperationType",
1003 "vnfPkgId",
1004 "kdu_name",
1005 "primitive",
1006 "primitive_params",
1007 ],
1008 "additionalProperties": False,
1009 }
1010
1011 # USERS
1012 project_role_mappings = {
1013 "title": "list pf projects/roles",
1014 "$schema": "http://json-schema.org/draft-04/schema#",
1015 "type": "array",
1016 "items": {
1017 "type": "object",
1018 "properties": {"project": shortname_schema, "role": shortname_schema},
1019 "required": ["project", "role"],
1020 "additionalProperties": False,
1021 },
1022 "minItems": 1,
1023 }
1024 project_role_mappings_optional = {
1025 "title": "list of projects/roles or projects only",
1026 "$schema": "http://json-schema.org/draft-04/schema#",
1027 "type": "array",
1028 "items": {
1029 "type": "object",
1030 "properties": {"project": shortname_schema, "role": shortname_schema},
1031 "required": ["project"],
1032 "additionalProperties": False,
1033 },
1034 "minItems": 1,
1035 }
1036 user_new_schema = {
1037 "$schema": "http://json-schema.org/draft-04/schema#",
1038 "title": "New user schema",
1039 "type": "object",
1040 "properties": {
1041 "username": string_schema,
1042 "domain_name": shortname_schema,
1043 "password": passwd_schema,
1044 "projects": nameshort_list_schema,
1045 "project_role_mappings": project_role_mappings,
1046 },
1047 "required": ["username", "password"],
1048 "additionalProperties": False,
1049 }
1050 user_edit_schema = {
1051 "$schema": "http://json-schema.org/draft-04/schema#",
1052 "title": "User edit schema for administrators",
1053 "type": "object",
1054 "properties": {
1055 "password": passwd_schema,
1056 "old_password": passwd_schema,
1057 "username": string_schema, # To allow User Name modification
1058 "projects": {"oneOf": [nameshort_list_schema, array_edition_schema]},
1059 "project_role_mappings": project_role_mappings,
1060 "add_project_role_mappings": project_role_mappings,
1061 "remove_project_role_mappings": project_role_mappings_optional,
1062 },
1063 "minProperties": 1,
1064 "additionalProperties": False,
1065 }
1066
1067 # PROJECTS
1068 topics_with_quota = [
1069 "vnfds",
1070 "nsds",
1071 "slice_templates",
1072 "pduds",
1073 "ns_instances",
1074 "slice_instances",
1075 "vim_accounts",
1076 "wim_accounts",
1077 "sdn_controllers",
1078 "k8sclusters",
1079 "vca",
1080 "k8srepos",
1081 "osmrepos",
1082 "ns_subscriptions",
1083 ]
1084 project_new_schema = {
1085 "$schema": "http://json-schema.org/draft-04/schema#",
1086 "title": "New project schema for administrators",
1087 "type": "object",
1088 "properties": {
1089 "name": shortname_schema,
1090 "admin": bool_schema,
1091 "domain_name": shortname_schema,
1092 "quotas": {
1093 "type": "object",
1094 "properties": {topic: integer0_schema for topic in topics_with_quota},
1095 "additionalProperties": False,
1096 },
1097 },
1098 "required": ["name"],
1099 "additionalProperties": False,
1100 }
1101 project_edit_schema = {
1102 "$schema": "http://json-schema.org/draft-04/schema#",
1103 "title": "Project edit schema for administrators",
1104 "type": "object",
1105 "properties": {
1106 "admin": bool_schema,
1107 "name": shortname_schema, # To allow Project Name modification
1108 "quotas": {
1109 "type": "object",
1110 "properties": {
1111 topic: {"oneOf": [integer0_schema, null_schema]}
1112 for topic in topics_with_quota
1113 },
1114 "additionalProperties": False,
1115 },
1116 },
1117 "additionalProperties": False,
1118 "minProperties": 1,
1119 }
1120
1121 # ROLES
1122 roles_new_schema = {
1123 "$schema": "http://json-schema.org/draft-04/schema#",
1124 "title": "New role schema for administrators",
1125 "type": "object",
1126 "properties": {
1127 "name": shortname_schema,
1128 "permissions": {
1129 "type": "object",
1130 "patternProperties": {
1131 ".": bool_schema,
1132 },
1133 # "minProperties": 1,
1134 },
1135 },
1136 "required": ["name"],
1137 "additionalProperties": False,
1138 }
1139 roles_edit_schema = {
1140 "$schema": "http://json-schema.org/draft-04/schema#",
1141 "title": "Roles edit schema for administrators",
1142 "type": "object",
1143 "properties": {
1144 "name": shortname_schema,
1145 "permissions": {
1146 "type": "object",
1147 "patternProperties": {".": {"oneOf": [bool_schema, null_schema]}},
1148 # "minProperties": 1,
1149 },
1150 },
1151 "additionalProperties": False,
1152 "minProperties": 1,
1153 }
1154
1155 # GLOBAL SCHEMAS
1156
1157 nbi_new_input_schemas = {
1158 "users": user_new_schema,
1159 "projects": project_new_schema,
1160 "vim_accounts": vim_account_new_schema,
1161 "sdns": sdn_new_schema,
1162 "ns_instantiate": ns_instantiate,
1163 "ns_action": ns_action,
1164 "ns_scale": ns_scale,
1165 "ns_update": ns_update,
1166 "ns_heal": ns_heal,
1167 "pdus": pdu_new_schema,
1168 }
1169
1170 nbi_edit_input_schemas = {
1171 "users": user_edit_schema,
1172 "projects": project_edit_schema,
1173 "vim_accounts": vim_account_edit_schema,
1174 "sdns": sdn_edit_schema,
1175 "pdus": pdu_edit_schema,
1176 }
1177
1178 # NETSLICE SCHEMAS
1179 nsi_subnet_instantiate = deepcopy(ns_instantiate)
1180 nsi_subnet_instantiate["title"] = "netslice subnet instantiation params input schema"
1181 nsi_subnet_instantiate["properties"]["id"] = name_schema
1182 del nsi_subnet_instantiate["required"]
1183
1184 nsi_vld_instantiate = {
1185 "title": "netslice vld instantiation params input schema",
1186 "$schema": "http://json-schema.org/draft-04/schema#",
1187 "type": "object",
1188 "properties": {
1189 "name": string_schema,
1190 "vim-network-name": {"oneOf": [string_schema, object_schema]},
1191 "vim-network-id": {"oneOf": [string_schema, object_schema]},
1192 "ip-profile": object_schema,
1193 },
1194 "required": ["name"],
1195 "additionalProperties": False,
1196 }
1197
1198 nsi_instantiate = {
1199 "title": "netslice action instantiate input schema",
1200 "$schema": "http://json-schema.org/draft-04/schema#",
1201 "type": "object",
1202 "properties": {
1203 "lcmOperationType": string_schema,
1204 "netsliceInstanceId": id_schema,
1205 "nsiName": name_schema,
1206 "nsiDescription": {"oneOf": [description_schema, null_schema]},
1207 "nstId": string_schema,
1208 "vimAccountId": id_schema,
1209 "timeout_nsi_deploy": integer1_schema,
1210 "ssh_keys": {"type": "array", "items": {"type": "string"}},
1211 "nsi_id": id_schema,
1212 "additionalParamsForNsi": object_schema,
1213 "netslice-subnet": {
1214 "type": "array",
1215 "minItems": 1,
1216 "items": nsi_subnet_instantiate,
1217 },
1218 "netslice-vld": {"type": "array", "minItems": 1, "items": nsi_vld_instantiate},
1219 },
1220 "required": ["nsiName", "nstId", "vimAccountId"],
1221 "additionalProperties": False,
1222 }
1223
1224 nsi_action = {}
1225
1226 nsi_terminate = {}
1227
1228 nsinstancesubscriptionfilter_schema = {
1229 "title": "instance identifier schema",
1230 "$schema": "http://json-schema.org/draft-07/schema#",
1231 "type": "object",
1232 "properties": {
1233 "nsdIds": {"type": "array"},
1234 "vnfdIds": {"type": "array"},
1235 "pnfdIds": {"type": "array"},
1236 "nsInstanceIds": {"type": "array"},
1237 "nsInstanceNames": {"type": "array"},
1238 },
1239 }
1240
1241 nslcmsub_schema = {
1242 "title": "nslcmsubscription input schema",
1243 "$schema": "http://json-schema.org/draft-07/schema#",
1244 "type": "object",
1245 "properties": {
1246 "nsInstanceSubscriptionFilter": nsinstancesubscriptionfilter_schema,
1247 "notificationTypes": {
1248 "type": "array",
1249 "items": {
1250 "enum": [
1251 "NsLcmOperationOccurrenceNotification",
1252 "NsChangeNotification",
1253 "NsIdentifierCreationNotification",
1254 "NsIdentifierDeletionNotification",
1255 ]
1256 },
1257 },
1258 "operationTypes": {
1259 "type": "array",
1260 "items": {"enum": ["INSTANTIATE", "SCALE", "TERMINATE", "UPDATE", "HEAL"]},
1261 },
1262 "operationStates": {
1263 "type": "array",
1264 "items": {
1265 "enum": [
1266 "PROCESSING",
1267 "COMPLETED",
1268 "PARTIALLY_COMPLETED",
1269 "FAILED",
1270 "FAILED_TEMP",
1271 "ROLLING_BACK",
1272 "ROLLED_BACK",
1273 ]
1274 },
1275 },
1276 "nsComponentTypes": {"type": "array", "items": {"enum": ["VNF", "NS", "PNF"]}},
1277 "lcmOpNameImpactingNsComponent": {
1278 "type": "array",
1279 "items": {
1280 "enum": [
1281 "VNF_INSTANTIATE",
1282 "VNF_SCALE",
1283 "VNF_SCALE_TO_LEVEL",
1284 "VNF_CHANGE_FLAVOUR",
1285 "VNF_TERMINATE",
1286 "VNF_HEAL",
1287 "VNF_OPERATE",
1288 "VNF_CHANGE_EXT_CONN",
1289 "VNF_MODIFY_INFO",
1290 "NS_INSTANTIATE",
1291 "NS_SCALE",
1292 "NS_UPDATE",
1293 "NS_TERMINATE",
1294 "NS_HEAL",
1295 ]
1296 },
1297 },
1298 "lcmOpOccStatusImpactingNsComponent": {
1299 "type": "array",
1300 "items": {
1301 "enum": [
1302 "START",
1303 "COMPLETED",
1304 "PARTIALLY_COMPLETED",
1305 "FAILED",
1306 "ROLLED_BACK",
1307 ]
1308 },
1309 },
1310 },
1311 "allOf": [
1312 {
1313 "if": {
1314 "properties": {
1315 "notificationTypes": {
1316 "contains": {"const": "NsLcmOperationOccurrenceNotification"}
1317 }
1318 },
1319 },
1320 "then": {
1321 "anyOf": [
1322 {"required": ["operationTypes"]},
1323 {"required": ["operationStates"]},
1324 ]
1325 },
1326 },
1327 {
1328 "if": {
1329 "properties": {
1330 "notificationTypes": {"contains": {"const": "NsChangeNotification"}}
1331 },
1332 },
1333 "then": {
1334 "anyOf": [
1335 {"required": ["nsComponentTypes"]},
1336 {"required": ["lcmOpNameImpactingNsComponent"]},
1337 {"required": ["lcmOpOccStatusImpactingNsComponent"]},
1338 ]
1339 },
1340 },
1341 ],
1342 }
1343
1344 authentication_schema = {
1345 "title": "authentication schema for subscription",
1346 "$schema": "http://json-schema.org/draft-07/schema#",
1347 "type": "object",
1348 "properties": {
1349 "authType": {"enum": ["basic"]},
1350 "paramsBasic": {
1351 "type": "object",
1352 "properties": {
1353 "userName": string_schema,
1354 "password": passwd_schema,
1355 },
1356 },
1357 },
1358 }
1359
1360 subscription = {
1361 "title": "subscription input schema",
1362 "$schema": "http://json-schema.org/draft-07/schema#",
1363 "type": "object",
1364 "properties": {
1365 "filter": nslcmsub_schema,
1366 "CallbackUri": description_schema,
1367 "authentication": authentication_schema,
1368 },
1369 "required": ["CallbackUri"],
1370 }
1371
1372 vnflcmsub_schema = {
1373 "title": "vnflcmsubscription input schema",
1374 "$schema": "http://json-schema.org/draft-07/schema#",
1375 "type": "object",
1376 "properties": {
1377 "VnfInstanceSubscriptionFilter": {
1378 "type": "object",
1379 "properties": {
1380 "vnfdIds": {"type": "array"},
1381 "vnfInstanceIds": {"type": "array"},
1382 },
1383 },
1384 "notificationTypes": {
1385 "type": "array",
1386 "items": {
1387 "enum": [
1388 "VnfIdentifierCreationNotification",
1389 "VnfLcmOperationOccurrenceNotification",
1390 "VnfIdentifierDeletionNotification"
1391 ]
1392 }
1393 },
1394 "operationTypes": {
1395 "type": "array",
1396 "items": {
1397 "enum": [
1398 "INSTANTIATE", "SCALE", "SCALE_TO_LEVEL", "CHANGE_FLAVOUR", "TERMINATE",
1399 "HEAL", "OPERATE", "CHANGE_EXT_CONN", "MODIFY_INFO", "CREATE_SNAPSHOT",
1400 "REVERT_TO_SNAPSHOT", "CHANGE_VNFPKG"
1401 ]
1402 }
1403 },
1404 "operationStates": {
1405 "type": "array",
1406 "items": {
1407 "enum": [
1408 "STARTING", "PROCESSING", "COMPLETED", "FAILED_TEMP", "FAILED",
1409 "ROLLING_BACK", "ROLLED_BACK"
1410 ]
1411 }
1412 }
1413 },
1414 "required": ["VnfInstanceSubscriptionFilter", "notificationTypes"]
1415 }
1416
1417 vnf_subscription = {
1418 "title": "vnf subscription input schema",
1419 "$schema": "http://json-schema.org/draft-07/schema#",
1420 "type": "object",
1421 "properties": {
1422 "filter": vnflcmsub_schema,
1423 "CallbackUri": description_schema,
1424 "authentication": authentication_schema
1425 },
1426 "required": ["filter", "CallbackUri"]
1427 }
1428
1429
1430 class ValidationError(Exception):
1431 def __init__(self, message, http_code=HTTPStatus.UNPROCESSABLE_ENTITY):
1432 self.http_code = http_code
1433 Exception.__init__(self, message)
1434
1435
1436 def validate_input(indata, schema_to_use):
1437 """
1438 Validates input data against json schema
1439 :param indata: user input data. Should be a dictionary
1440 :param schema_to_use: jsonschema to test
1441 :return: None if ok, raises ValidationError exception on error
1442 """
1443 try:
1444 if schema_to_use:
1445 js_v(indata, schema_to_use)
1446 return None
1447 except js_e.ValidationError as e:
1448 if e.path:
1449 error_pos = "at '" + ":".join(map(str, e.path)) + "'"
1450 else:
1451 error_pos = ""
1452 raise ValidationError("Format error {} '{}' ".format(error_pos, e.message))
1453 except js_e.SchemaError:
1454 raise ValidationError(
1455 "Bad json schema {}".format(schema_to_use),
1456 http_code=HTTPStatus.INTERNAL_SERVER_ERROR,
1457 )
1458
1459
1460 def is_valid_uuid(x):
1461 """
1462 Test for a valid UUID
1463 :param x: string to test
1464 :return: True if x is a valid uuid, False otherwise
1465 """
1466 try:
1467 if UUID(x):
1468 return True
1469 except (TypeError, ValueError, AttributeError):
1470 return False