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