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