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