Feature 10908: Update Charms in Running VNF instances
[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 "updateType": {
451 "enum": ["CHANGE_VNFPKG", "REMOVE_VNF", "MODIFY_VNF_INFORMATION"]
452 },
453 "modifyVnfInfoData": {
454 "type": "object",
455 "properties": {
456 "vnfInstanceId": id_schema,
457 "vnfdId": id_schema,
458 },
459 "required": ["vnfInstanceId", "vnfdId"],
460 },
461 "removeVnfInstanceId": id_schema,
462 "changeVnfPackageData": {
463 "type": "object",
464 "properties": {
465 "vnfInstanceId": id_schema,
466 "vnfdId": id_schema,
467 },
468 "required": ["vnfInstanceId", "vnfdId"],
469 },
470 },
471 "required": ["updateType"],
472 "additionalProperties": False,
473 }
474
475 ns_action = { # TODO for the moment it is only contemplated the vnfd primitive execution
476 "title": "ns action input schema",
477 "$schema": "http://json-schema.org/draft-04/schema#",
478 "type": "object",
479 "properties": {
480 "lcmOperationType": string_schema,
481 "nsInstanceId": id_schema,
482 "member_vnf_index": name_schema,
483 "vnf_member_index": name_schema, # TODO for backward compatibility. To remove in future
484 "vdu_id": name_schema,
485 "vdu_count_index": integer0_schema,
486 "kdu_name": name_schema,
487 "primitive": name_schema,
488 "timeout_ns_action": integer1_schema,
489 "primitive_params": {"type": "object"},
490 },
491 "required": ["primitive", "primitive_params"], # TODO add member_vnf_index
492 "additionalProperties": False,
493 }
494 ns_scale = { # TODO for the moment it is only VDU-scaling
495 "title": "ns scale input schema",
496 "$schema": "http://json-schema.org/draft-04/schema#",
497 "type": "object",
498 "properties": {
499 "lcmOperationType": string_schema,
500 "nsInstanceId": id_schema,
501 "scaleType": {"enum": ["SCALE_VNF"]},
502 "timeout_ns_scale": integer1_schema,
503 "scaleVnfData": {
504 "type": "object",
505 "properties": {
506 "vnfInstanceId": name_schema,
507 "scaleVnfType": {"enum": ["SCALE_OUT", "SCALE_IN"]},
508 "scaleByStepData": {
509 "type": "object",
510 "properties": {
511 "scaling-group-descriptor": name_schema,
512 "member-vnf-index": name_schema,
513 "scaling-policy": name_schema,
514 },
515 "required": ["scaling-group-descriptor", "member-vnf-index"],
516 "additionalProperties": False,
517 },
518 },
519 "required": ["scaleVnfType", "scaleByStepData"], # vnfInstanceId
520 "additionalProperties": False,
521 },
522 "scaleTime": time_schema,
523 },
524 "required": ["scaleType", "scaleVnfData"],
525 "additionalProperties": False,
526 }
527
528
529 schema_version = {"type": "string", "enum": ["1.0"]}
530 schema_type = {"type": "string"}
531 vim_type = shortname_schema # {"enum": ["openstack", "openvim", "vmware", "opennebula", "aws", "azure", "fos"]}
532
533 vim_account_edit_schema = {
534 "title": "vim_account edit input schema",
535 "$schema": "http://json-schema.org/draft-04/schema#",
536 "type": "object",
537 "properties": {
538 "name": name_schema,
539 "description": description_schema,
540 "vim": name_schema,
541 "datacenter": name_schema,
542 "vim_type": vim_type,
543 "vim_url": description_schema,
544 # "vim_url_admin": description_schema,
545 # "vim_tenant": name_schema,
546 "vim_tenant_name": name_schema,
547 "vim_user": string_schema,
548 "vim_password": passwd_schema,
549 "vca": id_schema,
550 "config": {"type": "object"},
551 "prometheus-config": {"type": "object"},
552 },
553 "additionalProperties": False,
554 }
555
556 vim_account_new_schema = {
557 "title": "vim_account creation input schema",
558 "$schema": "http://json-schema.org/draft-04/schema#",
559 "type": "object",
560 "properties": {
561 "schema_version": schema_version,
562 "schema_type": schema_type,
563 "name": name_schema,
564 "description": description_schema,
565 "vim": name_schema,
566 "datacenter": name_schema,
567 "vim_type": vim_type,
568 "vim_url": description_schema,
569 # "vim_url_admin": description_schema,
570 # "vim_tenant": name_schema,
571 "vim_tenant_name": name_schema,
572 "vim_user": string_schema,
573 "vim_password": passwd_schema,
574 "vca": id_schema,
575 "config": {"type": "object"},
576 "prometheus-config": {"type": "object"},
577 },
578 "required": [
579 "name",
580 "vim_url",
581 "vim_type",
582 "vim_user",
583 "vim_password",
584 "vim_tenant_name",
585 ],
586 "additionalProperties": False,
587 }
588
589 wim_type = shortname_schema # {"enum": ["ietfl2vpn", "onos", "odl", "dynpac", "fake"]}
590
591 wim_account_edit_schema = {
592 "title": "wim_account edit input schema",
593 "$schema": "http://json-schema.org/draft-04/schema#",
594 "type": "object",
595 "properties": {
596 "name": name_schema,
597 "description": description_schema,
598 "wim": name_schema,
599 "wim_type": wim_type,
600 "wim_url": description_schema,
601 "user": string_schema,
602 "password": passwd_schema,
603 "config": {"type": "object"},
604 },
605 "additionalProperties": False,
606 }
607
608 wim_account_new_schema = {
609 "title": "wim_account creation input schema",
610 "$schema": "http://json-schema.org/draft-04/schema#",
611 "type": "object",
612 "properties": {
613 "schema_version": schema_version,
614 "schema_type": schema_type,
615 "name": name_schema,
616 "description": description_schema,
617 "wim": name_schema,
618 "wim_type": wim_type,
619 "wim_url": description_schema,
620 "user": string_schema,
621 "password": passwd_schema,
622 "config": {
623 "type": "object",
624 "patternProperties": {".": {"not": {"type": "null"}}},
625 },
626 },
627 "required": ["name", "wim_url", "wim_type"],
628 "additionalProperties": False,
629 }
630
631 sdn_properties = {
632 "name": name_schema,
633 "type": {"type": "string"},
634 "url": {"type": "string"},
635 "user": string_schema,
636 "password": passwd_schema,
637 "config": {"type": "object"},
638 "description": description_schema,
639 # The folowing are deprecated. Maintanied for backward compatibility
640 "dpid": dpid_Schema,
641 "ip": ip_schema,
642 "port": port_schema,
643 "version": {"type": "string", "minLength": 1, "maxLength": 12},
644 }
645 sdn_new_schema = {
646 "title": "sdn controller information schema",
647 "$schema": "http://json-schema.org/draft-04/schema#",
648 "type": "object",
649 "properties": sdn_properties,
650 "required": ["name", "type"],
651 "additionalProperties": False,
652 }
653 sdn_edit_schema = {
654 "title": "sdn controller update information schema",
655 "$schema": "http://json-schema.org/draft-04/schema#",
656 "type": "object",
657 "properties": sdn_properties,
658 # "required": ["name", "port", 'ip', 'dpid', 'type'],
659 "additionalProperties": False,
660 }
661 sdn_port_mapping_schema = {
662 "$schema": "http://json-schema.org/draft-04/schema#",
663 "title": "sdn port mapping information schema",
664 "type": "array",
665 "items": {
666 "type": "object",
667 "properties": {
668 "compute_node": shortname_schema,
669 "ports": {
670 "type": "array",
671 "items": {
672 "type": "object",
673 "properties": {
674 "pci": pci_extended_schema,
675 "switch_port": shortname_schema,
676 "switch_mac": mac_schema,
677 },
678 "required": ["pci"],
679 },
680 },
681 },
682 "required": ["compute_node", "ports"],
683 },
684 }
685 sdn_external_port_schema = {
686 "$schema": "http://json-schema.org/draft-04/schema#",
687 "title": "External port information",
688 "type": "object",
689 "properties": {
690 "port": {"type": "string", "minLength": 1, "maxLength": 60},
691 "vlan": vlan_schema,
692 "mac": mac_schema,
693 },
694 "required": ["port"],
695 }
696
697 # K8s Clusters
698 k8scluster_nets_schema = {
699 "title": "k8scluster nets input schema",
700 "$schema": "http://json-schema.org/draft-04/schema#",
701 "type": "object",
702 "patternProperties": {".": {"oneOf": [name_schema, null_schema]}},
703 "minProperties": 1,
704 "additionalProperties": False,
705 }
706 k8scluster_new_schema = {
707 "title": "k8scluster creation input schema",
708 "$schema": "http://json-schema.org/draft-04/schema#",
709 "type": "object",
710 "properties": {
711 "schema_version": schema_version,
712 "schema_type": schema_type,
713 "name": name_schema,
714 "description": description_schema,
715 "credentials": object_schema,
716 "vim_account": id_schema,
717 "vca_id": id_schema,
718 "k8s_version": string_schema,
719 "nets": k8scluster_nets_schema,
720 "namespace": name_schema,
721 "cni": nameshort_list_schema,
722 },
723 "required": ["name", "credentials", "vim_account", "k8s_version", "nets"],
724 "additionalProperties": False,
725 }
726 k8scluster_edit_schema = {
727 "title": "vim_account edit 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 "credentials": object_schema,
734 "vim_account": id_schema,
735 "vca_id": id_schema,
736 "k8s_version": string_schema,
737 "nets": k8scluster_nets_schema,
738 "namespace": name_schema,
739 "cni": nameshort_list_schema,
740 },
741 "additionalProperties": False,
742 }
743
744 # VCA
745 vca_new_schema = {
746 "title": "vca creation input schema",
747 "$schema": "http://json-schema.org/draft-04/schema#",
748 "type": "object",
749 "properties": {
750 "schema_version": schema_version,
751 "schema_type": schema_type,
752 "name": name_schema,
753 "description": description_schema,
754 "endpoints": description_list_schema,
755 "user": string_schema,
756 "secret": passwd_schema,
757 "cacert": long_description_schema,
758 "lxd-cloud": shortname_schema,
759 "lxd-credentials": shortname_schema,
760 "k8s-cloud": shortname_schema,
761 "k8s-credentials": shortname_schema,
762 "model-config": object_schema,
763 },
764 "required": [
765 "name",
766 "endpoints",
767 "user",
768 "secret",
769 "cacert",
770 "lxd-cloud",
771 "lxd-credentials",
772 "k8s-cloud",
773 "k8s-credentials",
774 ],
775 "additionalProperties": False,
776 }
777 vca_edit_schema = {
778 "title": "vca creation input schema",
779 "$schema": "http://json-schema.org/draft-04/schema#",
780 "type": "object",
781 "properties": {
782 "name": name_schema,
783 "description": description_schema,
784 "endpoints": description_list_schema,
785 "port": integer1_schema,
786 "user": string_schema,
787 "secret": passwd_schema,
788 "cacert": long_description_schema,
789 "lxd-cloud": shortname_schema,
790 "lxd-credentials": shortname_schema,
791 "k8s-cloud": shortname_schema,
792 "k8s-credentials": shortname_schema,
793 "model-config": object_schema,
794 },
795 "additionalProperties": False,
796 }
797
798 # K8s Repos
799 k8srepo_types = {"enum": ["helm-chart", "juju-bundle"]}
800 k8srepo_properties = {
801 "name": name_schema,
802 "description": description_schema,
803 "type": k8srepo_types,
804 "url": description_schema,
805 }
806 k8srepo_new_schema = {
807 "title": "k8scluster creation input schema",
808 "$schema": "http://json-schema.org/draft-04/schema#",
809 "type": "object",
810 "properties": k8srepo_properties,
811 "required": ["name", "type", "url"],
812 "additionalProperties": False,
813 }
814 k8srepo_edit_schema = {
815 "title": "vim_account edit input schema",
816 "$schema": "http://json-schema.org/draft-04/schema#",
817 "type": "object",
818 "properties": k8srepo_properties,
819 "additionalProperties": False,
820 }
821
822 # OSM Repos
823 osmrepo_types = {"enum": ["osm"]}
824 osmrepo_properties = {
825 "name": name_schema,
826 "description": description_schema,
827 "type": osmrepo_types,
828 "url": description_schema
829 # "user": string_schema,
830 # "password": passwd_schema
831 }
832 osmrepo_new_schema = {
833 "title": "osm repo creation input schema",
834 "$schema": "http://json-schema.org/draft-04/schema#",
835 "type": "object",
836 "properties": osmrepo_properties,
837 "required": ["name", "type", "url"],
838 "additionalProperties": False,
839 }
840 osmrepo_edit_schema = {
841 "title": "osm repo edit input schema",
842 "$schema": "http://json-schema.org/draft-04/schema#",
843 "type": "object",
844 "properties": osmrepo_properties,
845 "additionalProperties": False,
846 }
847
848 # PDUs
849 pdu_interface = {
850 "type": "object",
851 "properties": {
852 "name": shortname_schema,
853 "mgmt": bool_schema,
854 "type": {"enum": ["overlay", "underlay"]},
855 "ip-address": {"oneOf": [ip_schema, ipv6_schema]},
856 # TODO, add user, password, ssh-key
857 "mac-address": mac_schema,
858 "vim-network-name": shortname_schema, # interface is connected to one vim network, or switch port
859 "vim-network-id": shortname_schema,
860 # # provide this in case SDN assist must deal with this interface
861 # "switch-dpid": dpid_Schema,
862 # "switch-port": shortname_schema,
863 # "switch-mac": shortname_schema,
864 # "switch-vlan": vlan_schema,
865 },
866 "required": ["name", "mgmt", "ip-address"],
867 "additionalProperties": False,
868 }
869 pdu_new_schema = {
870 "title": "pdu creation input schema",
871 "$schema": "http://json-schema.org/draft-04/schema#",
872 "type": "object",
873 "properties": {
874 "name": shortname_schema,
875 "type": shortname_schema,
876 "description": description_schema,
877 "shared": bool_schema,
878 "vims": nameshort_list_schema,
879 "vim_accounts": nameshort_list_schema,
880 "interfaces": {"type": "array", "items": pdu_interface, "minItems": 1},
881 },
882 "required": ["name", "type", "interfaces"],
883 "additionalProperties": False,
884 }
885 pdu_edit_schema = {
886 "title": "pdu edit input schema",
887 "$schema": "http://json-schema.org/draft-04/schema#",
888 "type": "object",
889 "properties": {
890 "name": shortname_schema,
891 "type": shortname_schema,
892 "description": description_schema,
893 "shared": bool_schema,
894 "vims": {"oneOf": [array_edition_schema, nameshort_list_schema]},
895 "vim_accounts": {"oneOf": [array_edition_schema, nameshort_list_schema]},
896 "interfaces": {
897 "oneOf": [
898 array_edition_schema,
899 {"type": "array", "items": pdu_interface, "minItems": 1},
900 ]
901 },
902 },
903 "additionalProperties": False,
904 "minProperties": 1,
905 }
906
907 # VNF PKG OPERATIONS
908 vnfpkgop_new_schema = {
909 "title": "VNF PKG operation creation input schema",
910 "$schema": "http://json-schema.org/draft-04/schema#",
911 "type": "object",
912 "properties": {
913 "lcmOperationType": string_schema,
914 "vnfPkgId": id_schema,
915 "kdu_name": name_schema,
916 "primitive": name_schema,
917 "primitive_params": {"type": "object"},
918 },
919 "required": [
920 "lcmOperationType",
921 "vnfPkgId",
922 "kdu_name",
923 "primitive",
924 "primitive_params",
925 ],
926 "additionalProperties": False,
927 }
928
929 # USERS
930 project_role_mappings = {
931 "title": "list pf projects/roles",
932 "$schema": "http://json-schema.org/draft-04/schema#",
933 "type": "array",
934 "items": {
935 "type": "object",
936 "properties": {"project": shortname_schema, "role": shortname_schema},
937 "required": ["project", "role"],
938 "additionalProperties": False,
939 },
940 "minItems": 1,
941 }
942 project_role_mappings_optional = {
943 "title": "list of projects/roles or projects only",
944 "$schema": "http://json-schema.org/draft-04/schema#",
945 "type": "array",
946 "items": {
947 "type": "object",
948 "properties": {"project": shortname_schema, "role": shortname_schema},
949 "required": ["project"],
950 "additionalProperties": False,
951 },
952 "minItems": 1,
953 }
954 user_new_schema = {
955 "$schema": "http://json-schema.org/draft-04/schema#",
956 "title": "New user schema",
957 "type": "object",
958 "properties": {
959 "username": string_schema,
960 "domain_name": shortname_schema,
961 "password": passwd_schema,
962 "projects": nameshort_list_schema,
963 "project_role_mappings": project_role_mappings,
964 },
965 "required": ["username", "password"],
966 "additionalProperties": False,
967 }
968 user_edit_schema = {
969 "$schema": "http://json-schema.org/draft-04/schema#",
970 "title": "User edit schema for administrators",
971 "type": "object",
972 "properties": {
973 "password": passwd_schema,
974 "old_password": passwd_schema,
975 "username": string_schema, # To allow User Name modification
976 "projects": {"oneOf": [nameshort_list_schema, array_edition_schema]},
977 "project_role_mappings": project_role_mappings,
978 "add_project_role_mappings": project_role_mappings,
979 "remove_project_role_mappings": project_role_mappings_optional,
980 },
981 "minProperties": 1,
982 "additionalProperties": False,
983 }
984
985 # PROJECTS
986 topics_with_quota = [
987 "vnfds",
988 "nsds",
989 "slice_templates",
990 "pduds",
991 "ns_instances",
992 "slice_instances",
993 "vim_accounts",
994 "wim_accounts",
995 "sdn_controllers",
996 "k8sclusters",
997 "vca",
998 "k8srepos",
999 "osmrepos",
1000 "ns_subscriptions",
1001 ]
1002 project_new_schema = {
1003 "$schema": "http://json-schema.org/draft-04/schema#",
1004 "title": "New project schema for administrators",
1005 "type": "object",
1006 "properties": {
1007 "name": shortname_schema,
1008 "admin": bool_schema,
1009 "domain_name": shortname_schema,
1010 "quotas": {
1011 "type": "object",
1012 "properties": {topic: integer0_schema for topic in topics_with_quota},
1013 "additionalProperties": False,
1014 },
1015 },
1016 "required": ["name"],
1017 "additionalProperties": False,
1018 }
1019 project_edit_schema = {
1020 "$schema": "http://json-schema.org/draft-04/schema#",
1021 "title": "Project edit schema for administrators",
1022 "type": "object",
1023 "properties": {
1024 "admin": bool_schema,
1025 "name": shortname_schema, # To allow Project Name modification
1026 "quotas": {
1027 "type": "object",
1028 "properties": {
1029 topic: {"oneOf": [integer0_schema, null_schema]}
1030 for topic in topics_with_quota
1031 },
1032 "additionalProperties": False,
1033 },
1034 },
1035 "additionalProperties": False,
1036 "minProperties": 1,
1037 }
1038
1039 # ROLES
1040 roles_new_schema = {
1041 "$schema": "http://json-schema.org/draft-04/schema#",
1042 "title": "New role schema for administrators",
1043 "type": "object",
1044 "properties": {
1045 "name": shortname_schema,
1046 "permissions": {
1047 "type": "object",
1048 "patternProperties": {
1049 ".": bool_schema,
1050 },
1051 # "minProperties": 1,
1052 },
1053 },
1054 "required": ["name"],
1055 "additionalProperties": False,
1056 }
1057 roles_edit_schema = {
1058 "$schema": "http://json-schema.org/draft-04/schema#",
1059 "title": "Roles edit schema for administrators",
1060 "type": "object",
1061 "properties": {
1062 "name": shortname_schema,
1063 "permissions": {
1064 "type": "object",
1065 "patternProperties": {".": {"oneOf": [bool_schema, null_schema]}},
1066 # "minProperties": 1,
1067 },
1068 },
1069 "additionalProperties": False,
1070 "minProperties": 1,
1071 }
1072
1073 # GLOBAL SCHEMAS
1074
1075 nbi_new_input_schemas = {
1076 "users": user_new_schema,
1077 "projects": project_new_schema,
1078 "vim_accounts": vim_account_new_schema,
1079 "sdns": sdn_new_schema,
1080 "ns_instantiate": ns_instantiate,
1081 "ns_action": ns_action,
1082 "ns_scale": ns_scale,
1083 "ns_update": ns_update,
1084 "pdus": pdu_new_schema,
1085 }
1086
1087 nbi_edit_input_schemas = {
1088 "users": user_edit_schema,
1089 "projects": project_edit_schema,
1090 "vim_accounts": vim_account_edit_schema,
1091 "sdns": sdn_edit_schema,
1092 "pdus": pdu_edit_schema,
1093 }
1094
1095 # NETSLICE SCHEMAS
1096 nsi_subnet_instantiate = deepcopy(ns_instantiate)
1097 nsi_subnet_instantiate["title"] = "netslice subnet instantiation params input schema"
1098 nsi_subnet_instantiate["properties"]["id"] = name_schema
1099 del nsi_subnet_instantiate["required"]
1100
1101 nsi_vld_instantiate = {
1102 "title": "netslice vld instantiation params input schema",
1103 "$schema": "http://json-schema.org/draft-04/schema#",
1104 "type": "object",
1105 "properties": {
1106 "name": string_schema,
1107 "vim-network-name": {"oneOf": [string_schema, object_schema]},
1108 "vim-network-id": {"oneOf": [string_schema, object_schema]},
1109 "ip-profile": object_schema,
1110 },
1111 "required": ["name"],
1112 "additionalProperties": False,
1113 }
1114
1115 nsi_instantiate = {
1116 "title": "netslice action instantiate input schema",
1117 "$schema": "http://json-schema.org/draft-04/schema#",
1118 "type": "object",
1119 "properties": {
1120 "lcmOperationType": string_schema,
1121 "netsliceInstanceId": id_schema,
1122 "nsiName": name_schema,
1123 "nsiDescription": {"oneOf": [description_schema, null_schema]},
1124 "nstId": string_schema,
1125 "vimAccountId": id_schema,
1126 "timeout_nsi_deploy": integer1_schema,
1127 "ssh_keys": {"type": "array", "items": {"type": "string"}},
1128 "nsi_id": id_schema,
1129 "additionalParamsForNsi": object_schema,
1130 "netslice-subnet": {
1131 "type": "array",
1132 "minItems": 1,
1133 "items": nsi_subnet_instantiate,
1134 },
1135 "netslice-vld": {"type": "array", "minItems": 1, "items": nsi_vld_instantiate},
1136 },
1137 "required": ["nsiName", "nstId", "vimAccountId"],
1138 "additionalProperties": False,
1139 }
1140
1141 nsi_action = {}
1142
1143 nsi_terminate = {}
1144
1145 nsinstancesubscriptionfilter_schema = {
1146 "title": "instance identifier schema",
1147 "$schema": "http://json-schema.org/draft-07/schema#",
1148 "type": "object",
1149 "properties": {
1150 "nsdIds": {"type": "array"},
1151 "vnfdIds": {"type": "array"},
1152 "pnfdIds": {"type": "array"},
1153 "nsInstanceIds": {"type": "array"},
1154 "nsInstanceNames": {"type": "array"},
1155 },
1156 }
1157
1158 nslcmsub_schema = {
1159 "title": "nslcmsubscription input schema",
1160 "$schema": "http://json-schema.org/draft-07/schema#",
1161 "type": "object",
1162 "properties": {
1163 "nsInstanceSubscriptionFilter": nsinstancesubscriptionfilter_schema,
1164 "notificationTypes": {
1165 "type": "array",
1166 "items": {
1167 "enum": [
1168 "NsLcmOperationOccurrenceNotification",
1169 "NsChangeNotification",
1170 "NsIdentifierCreationNotification",
1171 "NsIdentifierDeletionNotification",
1172 ]
1173 },
1174 },
1175 "operationTypes": {
1176 "type": "array",
1177 "items": {"enum": ["INSTANTIATE", "SCALE", "TERMINATE", "UPDATE", "HEAL"]},
1178 },
1179 "operationStates": {
1180 "type": "array",
1181 "items": {
1182 "enum": [
1183 "PROCESSING",
1184 "COMPLETED",
1185 "PARTIALLY_COMPLETED",
1186 "FAILED",
1187 "FAILED_TEMP",
1188 "ROLLING_BACK",
1189 "ROLLED_BACK",
1190 ]
1191 },
1192 },
1193 "nsComponentTypes": {"type": "array", "items": {"enum": ["VNF", "NS", "PNF"]}},
1194 "lcmOpNameImpactingNsComponent": {
1195 "type": "array",
1196 "items": {
1197 "enum": [
1198 "VNF_INSTANTIATE",
1199 "VNF_SCALE",
1200 "VNF_SCALE_TO_LEVEL",
1201 "VNF_CHANGE_FLAVOUR",
1202 "VNF_TERMINATE",
1203 "VNF_HEAL",
1204 "VNF_OPERATE",
1205 "VNF_CHANGE_EXT_CONN",
1206 "VNF_MODIFY_INFO",
1207 "NS_INSTANTIATE",
1208 "NS_SCALE",
1209 "NS_UPDATE",
1210 "NS_TERMINATE",
1211 "NS_HEAL",
1212 ]
1213 },
1214 },
1215 "lcmOpOccStatusImpactingNsComponent": {
1216 "type": "array",
1217 "items": {
1218 "enum": [
1219 "START",
1220 "COMPLETED",
1221 "PARTIALLY_COMPLETED",
1222 "FAILED",
1223 "ROLLED_BACK",
1224 ]
1225 },
1226 },
1227 },
1228 "allOf": [
1229 {
1230 "if": {
1231 "properties": {
1232 "notificationTypes": {
1233 "contains": {"const": "NsLcmOperationOccurrenceNotification"}
1234 }
1235 },
1236 },
1237 "then": {
1238 "anyOf": [
1239 {"required": ["operationTypes"]},
1240 {"required": ["operationStates"]},
1241 ]
1242 },
1243 },
1244 {
1245 "if": {
1246 "properties": {
1247 "notificationTypes": {"contains": {"const": "NsChangeNotification"}}
1248 },
1249 },
1250 "then": {
1251 "anyOf": [
1252 {"required": ["nsComponentTypes"]},
1253 {"required": ["lcmOpNameImpactingNsComponent"]},
1254 {"required": ["lcmOpOccStatusImpactingNsComponent"]},
1255 ]
1256 },
1257 },
1258 ],
1259 }
1260
1261 authentication_schema = {
1262 "title": "authentication schema for subscription",
1263 "$schema": "http://json-schema.org/draft-07/schema#",
1264 "type": "object",
1265 "properties": {
1266 "authType": {"enum": ["basic"]},
1267 "paramsBasic": {
1268 "type": "object",
1269 "properties": {
1270 "userName": string_schema,
1271 "password": passwd_schema,
1272 },
1273 },
1274 },
1275 }
1276
1277 subscription = {
1278 "title": "subscription input schema",
1279 "$schema": "http://json-schema.org/draft-07/schema#",
1280 "type": "object",
1281 "properties": {
1282 "filter": nslcmsub_schema,
1283 "CallbackUri": description_schema,
1284 "authentication": authentication_schema,
1285 },
1286 "required": ["CallbackUri"],
1287 }
1288
1289
1290 class ValidationError(Exception):
1291 def __init__(self, message, http_code=HTTPStatus.UNPROCESSABLE_ENTITY):
1292 self.http_code = http_code
1293 Exception.__init__(self, message)
1294
1295
1296 def validate_input(indata, schema_to_use):
1297 """
1298 Validates input data against json schema
1299 :param indata: user input data. Should be a dictionary
1300 :param schema_to_use: jsonschema to test
1301 :return: None if ok, raises ValidationError exception on error
1302 """
1303 try:
1304 if schema_to_use:
1305 js_v(indata, schema_to_use)
1306 return None
1307 except js_e.ValidationError as e:
1308 if e.path:
1309 error_pos = "at '" + ":".join(map(str, e.path)) + "'"
1310 else:
1311 error_pos = ""
1312 raise ValidationError("Format error {} '{}' ".format(error_pos, e.message))
1313 except js_e.SchemaError:
1314 raise ValidationError(
1315 "Bad json schema {}".format(schema_to_use),
1316 http_code=HTTPStatus.INTERNAL_SERVER_ERROR,
1317 )
1318
1319
1320 def is_valid_uuid(x):
1321 """
1322 Test for a valid UUID
1323 :param x: string to test
1324 :return: True if x is a valid uuid, False otherwise
1325 """
1326 try:
1327 if UUID(x):
1328 return True
1329 except (TypeError, ValueError, AttributeError):
1330 return False