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