avoid k8scluster deletion when in use
[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 = {"type": "string", "minLength": 1, "maxLength": 60, "pattern": "^[^,;()\\.\\$'\"]+$"}
32 passwd_schema = {"type": "string", "minLength": 1, "maxLength": 60}
33 name_schema = {"type": "string", "minLength": 1, "maxLength": 255, "pattern": "^[^,;()'\"]+$"}
34 string_schema = {"type": "string", "minLength": 1, "maxLength": 255}
35 xml_text_schema = {"type": "string", "minLength": 1, "maxLength": 1000, "pattern": "^[^']+$"}
36 description_schema = {"type": ["string", "null"], "maxLength": 255, "pattern": "^[^'\"]+$"}
37 id_schema_fake = {"type": "string", "minLength": 2, "maxLength": 36}
38 bool_schema = {"type": "boolean"}
39 null_schema = {"type": "null"}
40 # "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}$"
41 id_schema = {"type": "string", "pattern": "^[a-fA-F0-9]{8}(-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}$"}
42 time_schema = {"type": "string", "pattern": "^[0-9]{4}-[0-1][0-9]-[0-3][0-9]T[0-2][0-9]([0-5]:){2}"}
43 pci_schema = {"type": "string", "pattern": "^[0-9a-fA-F]{4}(:[0-9a-fA-F]{2}){2}\\.[0-9a-fA-F]$"}
44 # allows [] for wildcards. For that reason huge length limit is set
45 pci_extended_schema = {"type": "string", "pattern": "^[0-9a-fA-F.:-\\[\\]]{12,40}$"}
46 http_schema = {"type": "string", "pattern": "^https?://[^'\"=]+$"}
47 bandwidth_schema = {"type": "string", "pattern": "^[0-9]+ *([MG]bps)?$"}
48 memory_schema = {"type": "string", "pattern": "^[0-9]+ *([MG]i?[Bb])?$"}
49 integer0_schema = {"type": "integer", "minimum": 0}
50 integer1_schema = {"type": "integer", "minimum": 1}
51 path_schema = {"type": "string", "pattern": "^(\\.){0,2}(/[^/\"':{}\\(\\)]+)+$"}
52 vlan_schema = {"type": "integer", "minimum": 1, "maximum": 4095}
53 vlan1000_schema = {"type": "integer", "minimum": 1000, "maximum": 4095}
54 mac_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
56 dpid_Schema = {"type": "string", "pattern": "^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){7}$"}
57 # mac_schema={"type":"string", "pattern":"^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$"}
58 ip_schema = {"type": "string",
59 "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]?)$"}
60 ip_prefix_schema = {"type": "string",
61 "pattern": "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}"
62 "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/(30|[12]?[0-9])$"}
63 port_schema = {"type": "integer", "minimum": 1, "maximum": 65534}
64 object_schema = {"type": "object"}
65 schema_version_2 = {"type": "integer", "minimum": 2, "maximum": 2}
66 # schema_version_string={"type":"string","enum": ["0.1", "2", "0.2", "3", "0.3"]}
67 log_level_schema = {"type": "string", "enum": ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]}
68 checksum_schema = {"type": "string", "pattern": "^[0-9a-fA-F]{32}$"}
69 size_schema = {"type": "integer", "minimum": 1, "maximum": 100}
70 array_edition_schema = {
71 "type": "object",
72 "patternProperties": {
73 "^\\$": {}
74 },
75 "additionalProperties": False,
76 "minProperties": 1,
77 }
78 nameshort_list_schema = {
79 "type": "array",
80 "minItems": 1,
81 "items": shortname_schema,
82 }
83
84
85 ns_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
124 ip_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
137 ip_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
147 ip_profile_schema = {
148 "title": "ip profile validation schema",
149 "$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
160 ip_profile_update_schema = {
161 "title": "ip profile validation schema",
162 "$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
175 provider_network_schema = {
176 "title": "provider network validation schema",
177 "$schema": "http://json-schema.org/draft-04/schema#",
178 "type": "object",
179 "properties": {
180 "physical-network": name_schema,
181 "segmentation-id": name_schema,
182 "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,
196 },
197 "additionalProperties": True
198 }
199
200 ns_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,
207 "vim-network-id": name_schema,
208 "ip-profile": ip_profile_update_schema,
209 "provider-network": provider_network_schema,
210 "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,
218 # "mac-address": mac_schema,
219 },
220 "required": ["id-ref"],
221 "minProperties": 2,
222 "additionalProperties": False
223 },
224 }
225 },
226 "required": ["name"],
227 "minProperties": 2,
228 "additionalProperties": False
229 }
230
231 additional_params_for_vnf = {
232 "type": "array",
233 "items": {
234 "type": "object",
235 "properties": {
236 "member-vnf-index": name_schema,
237 "additionalParams": object_schema,
238 "k8s-namespace": name_schema,
239 "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,
258 "kdu_model": name_schema,
259 "k8s-namespace": name_schema,
260 },
261 "required": ["kdu_name"],
262 "minProperties": 2,
263 "additionalProperties": False,
264 },
265 },
266 },
267 "required": ["member-vnf-index"],
268 "minProperties": 2,
269 "additionalProperties": False
270 }
271 }
272
273 ns_instantiate = {
274 "title": "ns action instantiate input schema",
275 "$schema": "http://json-schema.org/draft-04/schema#",
276 "type": "object",
277 "properties": {
278 "lcmOperationType": string_schema,
279 "nsInstanceId": id_schema,
280 "netsliceInstanceId": id_schema,
281 "nsName": name_schema,
282 "nsDescription": {"oneOf": [description_schema, null_schema]},
283 "nsdId": id_schema,
284 "vimAccountId": id_schema,
285 "wimAccountId": {"OneOf": [id_schema, bool_schema, null_schema]},
286 "placement-engine": string_schema,
287 "placement-constraints": object_schema,
288 "additionalParamsForNs": object_schema,
289 "additionalParamsForVnf": additional_params_for_vnf,
290 "k8s-namespace": name_schema,
291 "ssh_keys": {"type": "array", "items": {"type": "string"}},
292 "timeout_ns_deploy": integer1_schema,
293 "nsr_id": id_schema,
294 "vduImage": name_schema,
295 "vnf": {
296 "type": "array",
297 "minItems": 1,
298 "items": {
299 "type": "object",
300 "properties": {
301 "member-vnf-index": name_schema,
302 "vimAccountId": id_schema,
303 "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 }
313 },
314 "required": ["member-vnf-index"],
315 "minProperties": 2,
316 "additionalProperties": False
317 }
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]},
327 "vim-network-id": {"OneOf": [string_schema, object_schema]},
328 "ns-net": object_schema,
329 "wimAccountId": {"OneOf": [id_schema, bool_schema, null_schema]},
330 "ip-profile": object_schema,
331 "provider-network": provider_network_schema,
332 "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 }
348 },
349 "required": ["name"],
350 "additionalProperties": False
351 }
352 },
353 },
354 "required": ["nsName", "nsdId", "vimAccountId"],
355 "additionalProperties": False
356 }
357
358 ns_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
372 ns_action = { # TODO for the moment it is only contemplated the vnfd primitive execution
373 "title": "ns action input schema",
374 "$schema": "http://json-schema.org/draft-04/schema#",
375 "type": "object",
376 "properties": {
377 "lcmOperationType": string_schema,
378 "nsInstanceId": id_schema,
379 "member_vnf_index": name_schema,
380 "vnf_member_index": name_schema, # TODO for backward compatibility. To remove in future
381 "vdu_id": name_schema,
382 "vdu_count_index": integer0_schema,
383 "kdu_name": name_schema,
384 "primitive": name_schema,
385 "timeout_ns_action": integer1_schema,
386 "primitive_params": {"type": "object"},
387 },
388 "required": ["primitive", "primitive_params"], # TODO add member_vnf_index
389 "additionalProperties": False
390 }
391 ns_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": {
396 "lcmOperationType": string_schema,
397 "nsInstanceId": id_schema,
398 "scaleType": {"enum": ["SCALE_VNF"]},
399 "timeout_ns_scale": integer1_schema,
400 "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 }
424
425
426 schema_version = {"type": "string", "enum": ["1.0"]}
427 schema_type = {"type": "string"}
428 vim_type = shortname_schema # {"enum": ["openstack", "openvim", "vmware", "opennebula", "aws", "azure", "fos"]}
429
430 vim_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,
437 "vim": name_schema,
438 "datacenter": name_schema,
439 "vim_type": vim_type,
440 "vim_url": description_schema,
441 # "vim_url_admin": description_schema,
442 # "vim_tenant": name_schema,
443 "vim_tenant_name": name_schema,
444 "vim_user": shortname_schema,
445 "vim_password": passwd_schema,
446 "config": {"type": "object"}
447 },
448 "additionalProperties": False
449 }
450
451 vim_account_new_schema = {
452 "title": "vim_account creation input schema",
453 "$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,
460 "vim": name_schema,
461 "datacenter": name_schema,
462 "vim_type": vim_type,
463 "vim_url": description_schema,
464 # "vim_url_admin": description_schema,
465 # "vim_tenant": name_schema,
466 "vim_tenant_name": name_schema,
467 "vim_user": shortname_schema,
468 "vim_password": passwd_schema,
469 "config": {"type": "object"}
470 },
471 "required": ["name", "vim_url", "vim_type", "vim_user", "vim_password", "vim_tenant_name"],
472 "additionalProperties": False
473 }
474
475 wim_type = shortname_schema # {"enum": ["ietfl2vpn", "onos", "odl", "dynpac", "fake"]}
476
477 wim_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,
484 "wim": name_schema,
485 "wim_type": wim_type,
486 "wim_url": description_schema,
487 "user": shortname_schema,
488 "password": passwd_schema,
489 "config": {"type": "object"}
490 },
491 "additionalProperties": False
492 }
493
494 wim_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,
504 "wim_type": wim_type,
505 "wim_url": description_schema,
506 "user": shortname_schema,
507 "password": passwd_schema,
508 "config": {
509 "type": "object",
510 "patternProperties": {
511 ".": {"not": {"type": "null"}}
512 }
513 }
514 },
515 "required": ["name", "wim_url", "wim_type"],
516 "additionalProperties": False
517 }
518
519 sdn_properties = {
520 "name": name_schema,
521 "type": {"type": "string"},
522 "url": {"type": "string"},
523 "user": shortname_schema,
524 "password": passwd_schema,
525 "config": {"type": "object"},
526 "description": description_schema,
527 # The folowing are deprecated. Maintanied for backward compatibility
528 "dpid": dpid_Schema,
529 "ip": ip_schema,
530 "port": port_schema,
531 "version": {"type": "string", "minLength": 1, "maxLength": 12},
532 }
533 sdn_new_schema = {
534 "title": "sdn controller information schema",
535 "$schema": "http://json-schema.org/draft-04/schema#",
536 "type": "object",
537 "properties": sdn_properties,
538 "required": ["name", 'type'],
539 "additionalProperties": False
540 }
541 sdn_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,
546 # "required": ["name", "port", 'ip', 'dpid', 'type'],
547 "additionalProperties": False
548 }
549 sdn_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": {
556 "compute_node": shortname_schema,
557 "ports": {
558 "type": "array",
559 "items": {
560 "type": "object",
561 "properties": {
562 "pci": pci_extended_schema,
563 "switch_port": shortname_schema,
564 "switch_mac": mac_schema
565 },
566 "required": ["pci"]
567 }
568 }
569 },
570 "required": ["compute_node", "ports"]
571 }
572 }
573 sdn_external_port_schema = {
574 "$schema": "http://json-schema.org/draft-04/schema#",
575 "title": "External port information",
576 "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
585 # K8s Clusters
586 k8scluster_nets_schema = {
587 "title": "k8scluster nets input schema",
588 "$schema": "http://json-schema.org/draft-04/schema#",
589 "type": "object",
590 "patternProperties": {".": {"oneOf": [name_schema, null_schema]}},
591 "minProperties": 1,
592 "additionalProperties": False
593 }
594 k8scluster_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 }
613 k8scluster_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
631 k8srepo_types = {"enum": ["helm-chart", "juju-bundle"]}
632 k8srepo_properties = {
633 "name": name_schema,
634 "description": description_schema,
635 "type": k8srepo_types,
636 "url": description_schema,
637 }
638 k8srepo_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 }
646 k8srepo_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
654 # PDUs
655 pdu_interface = {
656 "type": "object",
657 "properties": {
658 "name": shortname_schema,
659 "mgmt": bool_schema,
660 "type": {"enum": ["overlay", 'underlay']},
661 "ip-address": ip_schema,
662 # TODO, add user, password, ssh-key
663 "mac-address": mac_schema,
664 "vim-network-name": shortname_schema, # interface is connected to one vim network, or switch port
665 "vim-network-id": shortname_schema,
666 # # provide this in case SDN assist must deal with this interface
667 # "switch-dpid": dpid_Schema,
668 # "switch-port": shortname_schema,
669 # "switch-mac": shortname_schema,
670 # "switch-vlan": vlan_schema,
671 },
672 "required": ["name", "mgmt", "ip-address"],
673 "additionalProperties": False
674 }
675 pdu_new_schema = {
676 "title": "pdu creation input schema",
677 "$schema": "http://json-schema.org/draft-04/schema#",
678 "type": "object",
679 "properties": {
680 "name": shortname_schema,
681 "type": shortname_schema,
682 "description": description_schema,
683 "shared": bool_schema,
684 "vims": nameshort_list_schema,
685 "vim_accounts": nameshort_list_schema,
686 "interfaces": {
687 "type": "array",
688 "items": pdu_interface,
689 "minItems": 1
690 }
691 },
692 "required": ["name", "type", "interfaces"],
693 "additionalProperties": False
694 }
695 pdu_edit_schema = {
696 "title": "pdu edit input schema",
697 "$schema": "http://json-schema.org/draft-04/schema#",
698 "type": "object",
699 "properties": {
700 "name": shortname_schema,
701 "type": shortname_schema,
702 "description": description_schema,
703 "shared": bool_schema,
704 "vims": {"oneOf": [array_edition_schema, nameshort_list_schema]},
705 "vim_accounts": {"oneOf": [array_edition_schema, nameshort_list_schema]},
706 "interfaces": {"oneOf": [
707 array_edition_schema,
708 {
709 "type": "array",
710 "items": pdu_interface,
711 "minItems": 1
712 }
713 ]}
714 },
715 "additionalProperties": False,
716 "minProperties": 1
717 }
718
719 # VNF PKG OPERATIONS
720 vnfpkgop_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
735 # USERS
736 project_role_mappings = {
737 "title": "list pf projects/roles",
738 "$schema": "http://json-schema.org/draft-04/schema#",
739 "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
748 },
749 "minItems": 1
750 }
751 project_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
765 }
766 user_new_schema = {
767 "$schema": "http://json-schema.org/draft-04/schema#",
768 "title": "New user schema",
769 "type": "object",
770 "properties": {
771 "username": shortname_schema,
772 "domain_name": shortname_schema,
773 "password": passwd_schema,
774 "projects": nameshort_list_schema,
775 "project_role_mappings": project_role_mappings,
776 },
777 "required": ["username", "password"],
778 "additionalProperties": False
779 }
780 user_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,
786 "username": shortname_schema, # To allow User Name modification
787 "projects": {
788 "oneOf": [
789 nameshort_list_schema,
790 array_edition_schema
791 ]
792 },
793 "project_role_mappings": project_role_mappings,
794 "add_project_role_mappings": project_role_mappings,
795 "remove_project_role_mappings": project_role_mappings_optional,
796 },
797 "minProperties": 1,
798 "additionalProperties": False
799 }
800
801 # PROJECTS
802 topics_with_quota = ["vnfds", "nsds", "nsts", "pdus", "nsrs", "nsis", "vim_accounts", "wim_accounts", "sdns",
803 "k8sclusters", "k8srepos"]
804 project_new_schema = {
805 "$schema": "http://json-schema.org/draft-04/schema#",
806 "title": "New project schema for administrators",
807 "type": "object",
808 "properties": {
809 "name": shortname_schema,
810 "admin": bool_schema,
811 "domain_name": shortname_schema,
812 "quotas": {
813 "type": "object",
814 "properties": {topic: integer0_schema for topic in topics_with_quota},
815 "additionalProperties": False
816 },
817 },
818 "required": ["name"],
819 "additionalProperties": False
820 }
821 project_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,
827 "name": shortname_schema, # To allow Project Name modification
828 "quotas": {
829 "type": "object",
830 "properties": {topic: {"oneOf": [integer0_schema, null_schema]} for topic in topics_with_quota},
831 "additionalProperties": False
832 },
833 },
834 "additionalProperties": False,
835 "minProperties": 1
836 }
837
838 # ROLES
839 roles_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,
845 "permissions": {
846 "type": "object",
847 "patternProperties": {
848 ".": bool_schema,
849 },
850 # "minProperties": 1,
851 }
852 },
853 "required": ["name"],
854 "additionalProperties": False
855 }
856 roles_edit_schema = {
857 "$schema": "http://json-schema.org/draft-04/schema#",
858 "title": "Roles edit schema for administrators",
859 "type": "object",
860 "properties": {
861 "name": shortname_schema,
862 "permissions": {
863 "type": "object",
864 "patternProperties": {
865 ".": {
866 "oneOf": [bool_schema, null_schema]
867 }
868 },
869 # "minProperties": 1,
870 }
871 },
872 "additionalProperties": False,
873 "minProperties": 1
874 }
875
876 # GLOBAL SCHEMAS
877
878 nbi_new_input_schemas = {
879 "users": user_new_schema,
880 "projects": project_new_schema,
881 "vim_accounts": vim_account_new_schema,
882 "sdns": sdn_new_schema,
883 "ns_instantiate": ns_instantiate,
884 "ns_action": ns_action,
885 "ns_scale": ns_scale,
886 "pdus": pdu_new_schema,
887 }
888
889 nbi_edit_input_schemas = {
890 "users": user_edit_schema,
891 "projects": project_edit_schema,
892 "vim_accounts": vim_account_edit_schema,
893 "sdns": sdn_edit_schema,
894 "pdus": pdu_edit_schema,
895 }
896
897 # NETSLICE SCHEMAS
898 nsi_subnet_instantiate = deepcopy(ns_instantiate)
899 nsi_subnet_instantiate["title"] = "netslice subnet instantiation params input schema"
900 nsi_subnet_instantiate["properties"]["id"] = name_schema
901 del nsi_subnet_instantiate["required"]
902
903 nsi_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]},
910 "vim-network-id": {"OneOf": [string_schema, object_schema]},
911 "ip-profile": object_schema,
912 },
913 "required": ["name"],
914 "additionalProperties": False
915 }
916
917 nsi_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,
923 "netsliceInstanceId": id_schema,
924 "nsiName": name_schema,
925 "nsiDescription": {"oneOf": [description_schema, null_schema]},
926 "nstId": string_schema,
927 "vimAccountId": id_schema,
928 "timeout_nsi_deploy": integer1_schema,
929 "ssh_keys": {"type": "string"},
930 "nsi_id": id_schema,
931 "additionalParamsForNsi": object_schema,
932 "netslice-subnet": {
933 "type": "array",
934 "minItems": 1,
935 "items": nsi_subnet_instantiate
936 },
937 "netslice-vld": {
938 "type": "array",
939 "minItems": 1,
940 "items": nsi_vld_instantiate
941 },
942 },
943 "required": ["nsiName", "nstId", "vimAccountId"],
944 "additionalProperties": False
945 }
946
947 nsi_action = {
948
949 }
950
951 nsi_terminate = {
952
953 }
954
955
956 class ValidationError(Exception):
957 def __init__(self, message, http_code=HTTPStatus.UNPROCESSABLE_ENTITY):
958 self.http_code = http_code
959 Exception.__init__(self, message)
960
961
962 def validate_input(indata, schema_to_use):
963 """
964 Validates input data against json schema
965 :param indata: user input data. Should be a dictionary
966 :param schema_to_use: jsonschema to test
967 :return: None if ok, raises ValidationError exception on error
968 """
969 try:
970 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:
975 error_pos = "at '" + ":".join(map(str, e.path)) + "'"
976 else:
977 error_pos = ""
978 raise ValidationError("Format error {} '{}' ".format(error_pos, e.message))
979 except js_e.SchemaError:
980 raise ValidationError("Bad json schema {}".format(schema_to_use), http_code=HTTPStatus.INTERNAL_SERVER_ERROR)
981
982
983 def 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
992 except (TypeError, ValueError, AttributeError):
993 return False