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