blob: f81f45ca1ad5d934ab14346801ab28154dbf6f27 [file] [log] [blame]
tierno0f98af52018-03-19 10:28:22 +01001# -*- coding: utf-8 -*-
2
3from jsonschema import validate as js_v, exceptions as js_e
tierno36ec8602018-11-02 17:27:11 +01004from http import HTTPStatus
tierno0f98af52018-03-19 10:28:22 +01005
6__author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
7__version__ = "0.1"
8version_date = "Mar 2018"
9
10"""
11Validator of input data using JSON schemas for those items that not contains an OSM yang information model
12"""
13
14# Basis schemas
15patern_name = "^[ -~]+$"
tierno87006042018-10-24 12:50:20 +020016nameshort_schema = {"type": "string", "minLength": 1, "maxLength": 60, "pattern": "^[^,;()\\.\\$'\"]+$"}
tierno0f98af52018-03-19 10:28:22 +010017passwd_schema = {"type": "string", "minLength": 1, "maxLength": 60}
tierno0f98af52018-03-19 10:28:22 +010018name_schema = {"type": "string", "minLength": 1, "maxLength": 255, "pattern": "^[^,;()'\"]+$"}
tierno0da52252018-06-27 15:47:22 +020019string_schema = {"type": "string", "minLength": 1, "maxLength": 255}
tierno0f98af52018-03-19 10:28:22 +010020xml_text_schema = {"type": "string", "minLength": 1, "maxLength": 1000, "pattern": "^[^']+$"}
21description_schema = {"type": ["string", "null"], "maxLength": 255, "pattern": "^[^'\"]+$"}
tierno441dbbf2018-07-10 12:52:48 +020022id_schema_fake = {"type": "string", "minLength": 2, "maxLength": 36}
23bool_schema = {"type": "boolean"}
24null_schema = {"type": "null"}
tierno2236d202018-05-16 19:05:16 +020025# "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}$"
tierno0f98af52018-03-19 10:28:22 +010026id_schema = {"type": "string", "pattern": "^[a-fA-F0-9]{8}(-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}$"}
tiernof759d822018-06-11 18:54:54 +020027time_schema = {"type": "string", "pattern": "^[0-9]{4}-[0-1][0-9]-[0-3][0-9]T[0-2][0-9]([0-5]:){2}"}
tierno87006042018-10-24 12:50:20 +020028pci_schema = {"type": "string", "pattern": "^[0-9a-fA-F]{4}(:[0-9a-fA-F]{2}){2}\\.[0-9a-fA-F]$"}
tierno42fce592018-11-02 09:23:43 +010029# allows [] for wildcards. For that reason huge length limit is set
30pci_extended_schema = {"type": "string", "pattern": "^[0-9a-fA-F.:-\\[\\]]{12,40}$"}
tierno0f98af52018-03-19 10:28:22 +010031http_schema = {"type": "string", "pattern": "^https?://[^'\"=]+$"}
32bandwidth_schema = {"type": "string", "pattern": "^[0-9]+ *([MG]bps)?$"}
33memory_schema = {"type": "string", "pattern": "^[0-9]+ *([MG]i?[Bb])?$"}
34integer0_schema = {"type": "integer", "minimum": 0}
35integer1_schema = {"type": "integer", "minimum": 1}
tierno87006042018-10-24 12:50:20 +020036path_schema = {"type": "string", "pattern": "^(\\.){0,2}(/[^/\"':{}\\(\\)]+)+$"}
tierno0f98af52018-03-19 10:28:22 +010037vlan_schema = {"type": "integer", "minimum": 1, "maximum": 4095}
38vlan1000_schema = {"type": "integer", "minimum": 1000, "maximum": 4095}
39mac_schema = {"type": "string",
40 "pattern": "^[0-9a-fA-F][02468aceACE](:[0-9a-fA-F]{2}){5}$"} # must be unicast: LSB bit of MSB byte ==0
tiernocb83c942018-09-24 17:28:13 +020041dpid_Schema = {"type": "string", "pattern": "^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){7}$"}
tierno0f98af52018-03-19 10:28:22 +010042# mac_schema={"type":"string", "pattern":"^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$"}
43ip_schema = {"type": "string",
tierno87006042018-10-24 12:50:20 +020044 "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]?)$"}
tierno0f98af52018-03-19 10:28:22 +010045ip_prefix_schema = {"type": "string",
tierno87006042018-10-24 12:50:20 +020046 "pattern": "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}"
tierno2236d202018-05-16 19:05:16 +020047 "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/(30|[12]?[0-9])$"}
tierno0f98af52018-03-19 10:28:22 +010048port_schema = {"type": "integer", "minimum": 1, "maximum": 65534}
49object_schema = {"type": "object"}
50schema_version_2 = {"type": "integer", "minimum": 2, "maximum": 2}
51# schema_version_string={"type":"string","enum": ["0.1", "2", "0.2", "3", "0.3"]}
52log_level_schema = {"type": "string", "enum": ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]}
53checksum_schema = {"type": "string", "pattern": "^[0-9a-fA-F]{32}$"}
54size_schema = {"type": "integer", "minimum": 1, "maximum": 100}
tiernocd54a4a2018-09-12 16:40:35 +020055array_edition_schema = {
56 "type": "object",
57 "patternProperties": {
tierno87006042018-10-24 12:50:20 +020058 "^\\$": "Any"
tiernocd54a4a2018-09-12 16:40:35 +020059 },
60 "additionalProperties": False,
61 "minProperties": 1,
62}
tiernocb83c942018-09-24 17:28:13 +020063nameshort_list_schema = {
64 "type": "array",
65 "minItems": 1,
66 "items": nameshort_schema,
67}
tiernocd54a4a2018-09-12 16:40:35 +020068
tierno0f98af52018-03-19 10:28:22 +010069
tierno441dbbf2018-07-10 12:52:48 +020070ns_instantiate_vdu = {
71 "title": "ns action instantiate input schema for vdu",
72 "$schema": "http://json-schema.org/draft-04/schema#",
73 "type": "object",
74 "properties": {
75 "id": name_schema,
76 "volume": {
77 "type": "array",
78 "minItems": 1,
79 "items": {
80 "type": "object",
81 "properties": {
82 "name": name_schema,
83 "vim-volume-id": name_schema,
84 },
85 "required": ["name", "vim-volume-id"],
86 "additionalProperties": False
87 }
88 },
89 "interface": {
90 "type": "array",
91 "minItems": 1,
92 "items": {
93 "type": "object",
94 "properties": {
95 "name": name_schema,
96 "ip-address": ip_schema,
97 "mac-address": mac_schema,
98 "floating-ip-required": bool_schema,
99 },
100 "required": ["name"],
101 "additionalProperties": False
102 }
103 }
104 },
105 "required": ["id"],
106 "additionalProperties": False
107}
108
109ip_profile_dns_schema = {
110 "type": "array",
111 "minItems": 1,
112 "items": {
113 "type": "object",
114 "properties": {
115 "address": ip_schema,
116 },
117 "required": ["address"],
118 "additionalProperties": False
119 }
120}
121
122ip_profile_dhcp_schema = {
123 "type": "object",
124 "properties": {
125 "enabled": {"type": "boolean"},
126 "count": integer1_schema,
127 "start-address": ip_schema
128 },
129 "additionalProperties": False,
130}
131
132ip_profile_schema = {
133 "title": "ip profile validation schame",
134 "$schema": "http://json-schema.org/draft-04/schema#",
135 "type": "object",
136 "properties": {
137 "ip-version": {"enum": ["ipv4", "ipv6"]},
138 "subnet-address": ip_prefix_schema,
139 "gateway-address": ip_schema,
140 "dns-server": ip_profile_dns_schema,
141 "dhcp-params": ip_profile_dhcp_schema,
142 }
143}
144
145ip_profile_update_schema = {
146 "title": "ip profile validation schame",
147 "$schema": "http://json-schema.org/draft-04/schema#",
148 "type": "object",
149 "properties": {
150 "ip-version": {"enum": ["ipv4", "ipv6"]},
151 "subnet-address": {"oneOf": [null_schema, ip_prefix_schema]},
152 "gateway-address": {"oneOf": [null_schema, ip_schema]},
153 "dns-server": {"oneOf": [null_schema, ip_profile_dns_schema]},
154
155 "dhcp-params": {"oneOf": [null_schema, ip_profile_dhcp_schema]},
156 },
157 "additionalProperties": False
158}
159
160ns_instantiate_internal_vld = {
161 "title": "ns action instantiate input schema for vdu",
162 "$schema": "http://json-schema.org/draft-04/schema#",
163 "type": "object",
164 "properties": {
165 "name": name_schema,
166 "vim-network-name": name_schema,
167 "ip-profile": ip_profile_update_schema,
168 "internal-connection-point": {
169 "type": "array",
170 "minItems": 1,
171 "items": {
172 "type": "object",
173 "properties": {
174 "id-ref": name_schema,
175 "ip-address": ip_schema,
tiernob7c6f2a2018-09-03 14:32:10 +0200176 # "mac-address": mac_schema,
tierno441dbbf2018-07-10 12:52:48 +0200177 },
tiernob7c6f2a2018-09-03 14:32:10 +0200178 "required": ["id-ref"],
179 "minProperties": 2,
tierno441dbbf2018-07-10 12:52:48 +0200180 "additionalProperties": False
181 },
182 }
183 },
184 "required": ["name"],
185 "minProperties": 2,
186 "additionalProperties": False
187}
tierno65acb4d2018-04-06 16:42:40 +0200188
189ns_instantiate = {
190 "title": "ns action instantiate input schema",
191 "$schema": "http://json-schema.org/draft-04/schema#",
192 "type": "object",
tierno0da52252018-06-27 15:47:22 +0200193 "properties": {
tiernob24258a2018-10-04 18:39:49 +0200194 "lcmOperationType": string_schema,
195 "nsInstanceId": id_schema,
tierno0da52252018-06-27 15:47:22 +0200196 "nsName": name_schema,
tierno441dbbf2018-07-10 12:52:48 +0200197 "nsDescription": {"oneOf": [description_schema, {"type": "null"}]},
tierno0da52252018-06-27 15:47:22 +0200198 "nsdId": id_schema,
199 "vimAccountId": id_schema,
tiernofc5089c2018-10-31 09:28:11 +0100200 "ssh_keys": {"type": "array", "items": {"type": "string"}},
tierno441dbbf2018-07-10 12:52:48 +0200201 "nsr_id": id_schema,
tiernocc103432018-10-19 14:10:35 +0200202 "vduImage": name_schema,
tierno0da52252018-06-27 15:47:22 +0200203 "vnf": {
204 "type": "array",
205 "minItems": 1,
206 "items": {
207 "type": "object",
208 "properties": {
209 "member-vnf-index": name_schema,
210 "vimAccountId": id_schema,
tierno441dbbf2018-07-10 12:52:48 +0200211 "vdu": {
212 "type": "array",
213 "minItems": 1,
214 "items": ns_instantiate_vdu,
215 },
216 "internal-vld": {
217 "type": "array",
218 "minItems": 1,
219 "items": ns_instantiate_internal_vld
220 }
tierno0da52252018-06-27 15:47:22 +0200221 },
tierno441dbbf2018-07-10 12:52:48 +0200222 "required": ["member-vnf-index"],
223 "minProperties": 2,
224 "additionalProperties": False
tierno0da52252018-06-27 15:47:22 +0200225 }
226 },
227 "vld": {
228 "type": "array",
229 "minItems": 1,
230 "items": {
231 "type": "object",
232 "properties": {
233 "name": string_schema,
234 "vim-network-name": {"OneOf": [string_schema, object_schema]},
235 "ip-profile": object_schema,
tiernob7c6f2a2018-09-03 14:32:10 +0200236 "vnfd-connection-point-ref": {
237 "type": "array",
238 "minItems": 1,
239 "items": {
240 "type": "object",
241 "properties": {
242 "member-vnf-index-ref": name_schema,
243 "vnfd-connection-point-ref": name_schema,
244 "ip-address": ip_schema,
245 # "mac-address": mac_schema,
246 },
247 "required": ["member-vnf-index-ref", "vnfd-connection-point-ref"],
248 "minProperties": 3,
249 "additionalProperties": False
250 },
251 }
tierno0da52252018-06-27 15:47:22 +0200252 },
tierno441dbbf2018-07-10 12:52:48 +0200253 "required": ["name"],
254 "additionalProperties": False
tierno0da52252018-06-27 15:47:22 +0200255 }
256 },
257 },
tierno441dbbf2018-07-10 12:52:48 +0200258 "required": ["nsName", "nsdId", "vimAccountId"],
259 "additionalProperties": False
tierno65acb4d2018-04-06 16:42:40 +0200260}
tierno0da52252018-06-27 15:47:22 +0200261
tierno65acb4d2018-04-06 16:42:40 +0200262ns_action = { # TODO for the moment it is only contemplated the vnfd primitive execution
tiernof759d822018-06-11 18:54:54 +0200263 "title": "ns action input schema",
tierno65acb4d2018-04-06 16:42:40 +0200264 "$schema": "http://json-schema.org/draft-04/schema#",
265 "type": "object",
266 "properties": {
tiernob24258a2018-10-04 18:39:49 +0200267 "lcmOperationType": string_schema,
268 "nsInstanceId": id_schema,
tiernof5298be2018-05-16 14:43:57 +0200269 "member_vnf_index": name_schema,
270 "vnf_member_index": name_schema, # TODO for backward compatibility. To remove in future
tierno7ce1db92018-07-25 12:50:52 +0200271 "vdu_id": name_schema,
tierno65acb4d2018-04-06 16:42:40 +0200272 "primitive": name_schema,
273 "primitive_params": {"type": "object"},
274 },
tiernof5298be2018-05-16 14:43:57 +0200275 "required": ["primitive", "primitive_params"], # TODO add member_vnf_index
tierno65acb4d2018-04-06 16:42:40 +0200276 "additionalProperties": False
277}
tiernof759d822018-06-11 18:54:54 +0200278ns_scale = { # TODO for the moment it is only VDU-scaling
279 "title": "ns scale input schema",
280 "$schema": "http://json-schema.org/draft-04/schema#",
281 "type": "object",
282 "properties": {
tiernob24258a2018-10-04 18:39:49 +0200283 "lcmOperationType": string_schema,
284 "nsInstanceId": id_schema,
tiernof759d822018-06-11 18:54:54 +0200285 "scaleType": {"enum": ["SCALE_VNF"]},
286 "scaleVnfData": {
287 "type": "object",
288 "properties": {
289 "vnfInstanceId": name_schema,
290 "scaleVnfType": {"enum": ["SCALE_OUT", 'SCALE_IN']},
291 "scaleByStepData": {
292 "type": "object",
293 "properties": {
294 "scaling-group-descriptor": name_schema,
295 "member-vnf-index": name_schema,
296 "scaling-policy": name_schema,
297 },
298 "required": ["scaling-group-descriptor", "member-vnf-index"],
299 "additionalProperties": False
300 },
301 },
302 "required": ["scaleVnfType", "scaleByStepData"], # vnfInstanceId
303 "additionalProperties": False
304 },
305 "scaleTime": time_schema,
306 },
307 "required": ["scaleType", "scaleVnfData"],
308 "additionalProperties": False
309}
tierno65acb4d2018-04-06 16:42:40 +0200310
311
tierno0f98af52018-03-19 10:28:22 +0100312schema_version = {"type": "string", "enum": ["1.0"]}
tierno09c073e2018-04-26 13:36:48 +0200313vim_account_edit_schema = {
314 "title": "vim_account edit input schema",
315 "$schema": "http://json-schema.org/draft-04/schema#",
316 "type": "object",
317 "properties": {
318 "name": name_schema,
319 "description": description_schema,
320 "type": nameshort_schema, # currently "openvim" or "openstack", can be enlarged with plugins
321 "vim": name_schema,
322 "datacenter": name_schema,
323 "vim_url": description_schema,
324 "vim_url_admin": description_schema,
325 "vim_tenant": name_schema,
326 "vim_tenant_name": name_schema,
327 "vim_username": nameshort_schema,
tiernocd54a4a2018-09-12 16:40:35 +0200328 "vim_password": passwd_schema,
tierno09c073e2018-04-26 13:36:48 +0200329 "config": {"type": "object"}
330 },
331 "additionalProperties": False
332}
tierno0f98af52018-03-19 10:28:22 +0100333schema_type = {"type": "string"}
334
tierno09c073e2018-04-26 13:36:48 +0200335vim_account_new_schema = {
336 "title": "vim_account creation input schema",
tierno0f98af52018-03-19 10:28:22 +0100337 "$schema": "http://json-schema.org/draft-04/schema#",
338 "type": "object",
339 "properties": {
340 "schema_version": schema_version,
341 "schema_type": schema_type,
342 "name": name_schema,
343 "description": description_schema,
tierno09c073e2018-04-26 13:36:48 +0200344 "vim": name_schema,
345 "datacenter": name_schema,
tierno0f98af52018-03-19 10:28:22 +0100346 "vim_type": {"enum": ["openstack", "openvim", "vmware", "opennebula", "aws"]},
347 "vim_url": description_schema,
348 # "vim_url_admin": description_schema,
349 # "vim_tenant": name_schema,
350 "vim_tenant_name": name_schema,
351 "vim_user": nameshort_schema,
tiernocd54a4a2018-09-12 16:40:35 +0200352 "vim_password": passwd_schema,
tierno0f98af52018-03-19 10:28:22 +0100353 "config": {"type": "object"}
354 },
355 "required": ["name", "vim_url", "vim_type", "vim_user", "vim_password", "vim_tenant_name"],
356 "additionalProperties": False
357}
tierno0f98af52018-03-19 10:28:22 +0100358
359
360sdn_properties = {
361 "name": name_schema,
tiernocfb07c62018-05-10 18:30:51 +0200362 "description": description_schema,
tiernocb83c942018-09-24 17:28:13 +0200363 "dpid": dpid_Schema,
tierno0f98af52018-03-19 10:28:22 +0100364 "ip": ip_schema,
365 "port": port_schema,
366 "type": {"type": "string", "enum": ["opendaylight", "floodlight", "onos"]},
367 "version": {"type": "string", "minLength": 1, "maxLength": 12},
368 "user": nameshort_schema,
369 "password": passwd_schema
370}
371sdn_new_schema = {
372 "title": "sdn controller information schema",
373 "$schema": "http://json-schema.org/draft-04/schema#",
374 "type": "object",
375 "properties": sdn_properties,
376 "required": ["name", "port", 'ip', 'dpid', 'type'],
377 "additionalProperties": False
378}
379sdn_edit_schema = {
380 "title": "sdn controller update information schema",
381 "$schema": "http://json-schema.org/draft-04/schema#",
382 "type": "object",
383 "properties": sdn_properties,
tiernocfb07c62018-05-10 18:30:51 +0200384 # "required": ["name", "port", 'ip', 'dpid', 'type'],
tierno0f98af52018-03-19 10:28:22 +0100385 "additionalProperties": False
386}
387sdn_port_mapping_schema = {
388 "$schema": "http://json-schema.org/draft-04/schema#",
389 "title": "sdn port mapping information schema",
390 "type": "array",
391 "items": {
392 "type": "object",
393 "properties": {
394 "compute_node": nameshort_schema,
395 "ports": {
396 "type": "array",
397 "items": {
398 "type": "object",
399 "properties": {
tierno42fce592018-11-02 09:23:43 +0100400 "pci": pci_extended_schema,
tierno0f98af52018-03-19 10:28:22 +0100401 "switch_port": nameshort_schema,
402 "switch_mac": mac_schema
403 },
404 "required": ["pci"]
405 }
406 }
407 },
408 "required": ["compute_node", "ports"]
409 }
410}
411sdn_external_port_schema = {
412 "$schema": "http://json-schema.org/draft-04/schema#",
tiernocd54a4a2018-09-12 16:40:35 +0200413 "title": "External port information",
tierno0f98af52018-03-19 10:28:22 +0100414 "type": "object",
415 "properties": {
416 "port": {"type": "string", "minLength": 1, "maxLength": 60},
417 "vlan": vlan_schema,
418 "mac": mac_schema
419 },
420 "required": ["port"]
421}
422
tiernocb83c942018-09-24 17:28:13 +0200423# PDUs
424pdu_interface = {
425 "type": "object",
426 "properties": {
427 "name": nameshort_schema,
428 "mgmt": bool_schema,
429 "type": {"enum": ["overlay", 'underlay']},
tierno36ec8602018-11-02 17:27:11 +0100430 "ip-address": ip_schema,
tiernocb83c942018-09-24 17:28:13 +0200431 # TODO, add user, password, ssh-key
tierno36ec8602018-11-02 17:27:11 +0100432 "mac-address": mac_schema,
433 "vim-network-name": nameshort_schema, # interface is connected to one vim network, or switch port
434 # TODO "vim-network-id": nameshort_schema,
435 # # provide this in case SDN assist must deal with this interface
436 # "switch-dpid": dpid_Schema,
437 # "switch-port": nameshort_schema,
438 # "switch-mac": nameshort_schema,
439 # "switch-vlan": vlan_schema,
tiernocb83c942018-09-24 17:28:13 +0200440 },
tierno36ec8602018-11-02 17:27:11 +0100441 "required": ["name", "mgmt", "ip-address"],
tiernocb83c942018-09-24 17:28:13 +0200442 "additionalProperties": False
tiernocd54a4a2018-09-12 16:40:35 +0200443}
tiernocb83c942018-09-24 17:28:13 +0200444pdu_new_schema = {
445 "title": "pdu creation input schema",
446 "$schema": "http://json-schema.org/draft-04/schema#",
447 "type": "object",
448 "properties": {
449 "name": nameshort_schema,
450 "type": nameshort_schema,
451 "description": description_schema,
452 "shared": bool_schema,
453 "vims": nameshort_list_schema,
454 "vim_accounts": nameshort_list_schema,
455 "interfaces": {
456 "type": "array",
tierno36ec8602018-11-02 17:27:11 +0100457 "items": pdu_interface,
tiernocb83c942018-09-24 17:28:13 +0200458 "minItems": 1
459 }
460 },
461 "required": ["name", "type", "interfaces"],
462 "additionalProperties": False
463}
464
465pdu_edit_schema = {
466 "title": "pdu edit input schema",
467 "$schema": "http://json-schema.org/draft-04/schema#",
468 "type": "object",
469 "properties": {
470 "name": nameshort_schema,
471 "type": nameshort_schema,
472 "description": description_schema,
473 "shared": bool_schema,
474 "vims": {"oneOff": [array_edition_schema, nameshort_list_schema]},
475 "vim_accounts": {"oneOff": [array_edition_schema, nameshort_list_schema]},
476 "interfaces": {"oneOff": [
477 array_edition_schema,
478 {
479 "type": "array",
tierno36ec8602018-11-02 17:27:11 +0100480 "items": pdu_interface,
tiernocb83c942018-09-24 17:28:13 +0200481 "minItems": 1
482 }
483 ]}
484 },
485 "additionalProperties": False,
486 "minProperties": 1
487}
488
489# USERS
tiernocd54a4a2018-09-12 16:40:35 +0200490user_new_schema = {
491 "$schema": "http://json-schema.org/draft-04/schema#",
492 "title": "New user schema",
493 "type": "object",
494 "properties": {
495 "username": nameshort_schema,
496 "password": passwd_schema,
tiernocb83c942018-09-24 17:28:13 +0200497 "projects": nameshort_list_schema,
tiernocd54a4a2018-09-12 16:40:35 +0200498 },
499 "required": ["username", "password", "projects"],
500 "additionalProperties": False
501}
502user_edit_schema = {
503 "$schema": "http://json-schema.org/draft-04/schema#",
504 "title": "User edit schema for administrators",
505 "type": "object",
506 "properties": {
507 "password": passwd_schema,
508 "projects": {
509 "oneOff": [
tiernocb83c942018-09-24 17:28:13 +0200510 nameshort_list_schema,
tiernocd54a4a2018-09-12 16:40:35 +0200511 array_edition_schema
512 ]
513 },
tiernocb83c942018-09-24 17:28:13 +0200514 },
515 "minProperties": 1,
516 "additionalProperties": False
tiernocd54a4a2018-09-12 16:40:35 +0200517}
518
519# PROJECTS
520project_new_schema = {
521 "$schema": "http://json-schema.org/draft-04/schema#",
522 "title": "New project schema for administrators",
523 "type": "object",
524 "properties": {
525 "name": nameshort_schema,
526 "admin": bool_schema,
527 },
528 "required": ["name"],
529 "additionalProperties": False
530}
531project_edit_schema = {
532 "$schema": "http://json-schema.org/draft-04/schema#",
533 "title": "Project edit schema for administrators",
534 "type": "object",
535 "properties": {
536 "admin": bool_schema,
537 },
538 "additionalProperties": False,
539 "minProperties": 1
540}
541
542# GLOBAL SCHEMAS
tierno0f98af52018-03-19 10:28:22 +0100543
544nbi_new_input_schemas = {
tiernocd54a4a2018-09-12 16:40:35 +0200545 "users": user_new_schema,
546 "projects": project_new_schema,
tierno09c073e2018-04-26 13:36:48 +0200547 "vim_accounts": vim_account_new_schema,
tierno65acb4d2018-04-06 16:42:40 +0200548 "sdns": sdn_new_schema,
549 "ns_instantiate": ns_instantiate,
550 "ns_action": ns_action,
tiernocb83c942018-09-24 17:28:13 +0200551 "ns_scale": ns_scale,
552 "pdus": pdu_new_schema,
tierno0f98af52018-03-19 10:28:22 +0100553}
554
555nbi_edit_input_schemas = {
tiernocd54a4a2018-09-12 16:40:35 +0200556 "users": user_edit_schema,
557 "projects": project_edit_schema,
tierno09c073e2018-04-26 13:36:48 +0200558 "vim_accounts": vim_account_edit_schema,
tiernocb83c942018-09-24 17:28:13 +0200559 "sdns": sdn_edit_schema,
560 "pdus": pdu_edit_schema,
tierno0f98af52018-03-19 10:28:22 +0100561}
562
563
564class ValidationError(Exception):
tierno36ec8602018-11-02 17:27:11 +0100565 def __init__(self, message, http_code=HTTPStatus.UNPROCESSABLE_ENTITY):
566 self.http_code = http_code
567 Exception.__init__(self, message)
tierno0f98af52018-03-19 10:28:22 +0100568
569
tiernob24258a2018-10-04 18:39:49 +0200570def validate_input(indata, schema_to_use):
tierno0f98af52018-03-19 10:28:22 +0100571 """
tiernocd54a4a2018-09-12 16:40:35 +0200572 Validates input data against json schema
tierno0f98af52018-03-19 10:28:22 +0100573 :param indata: user input data. Should be a dictionary
tiernob24258a2018-10-04 18:39:49 +0200574 :param schema_to_use: jsonschema to test
575 :return: None if ok, raises ValidationError exception on error
tierno0f98af52018-03-19 10:28:22 +0100576 """
577 try:
tierno0f98af52018-03-19 10:28:22 +0100578 if schema_to_use:
579 js_v(indata, schema_to_use)
580 return None
581 except js_e.ValidationError as e:
582 if e.path:
tierno0da52252018-06-27 15:47:22 +0200583 error_pos = "at '" + ":".join(map(str, e.path)) + "'"
tierno0f98af52018-03-19 10:28:22 +0100584 else:
585 error_pos = ""
tierno441dbbf2018-07-10 12:52:48 +0200586 raise ValidationError("Format error {} '{}' ".format(error_pos, e.message))
tierno36ec8602018-11-02 17:27:11 +0100587 except js_e.SchemaError:
588 raise ValidationError("Bad json schema {}".format(schema_to_use), http_code=HTTPStatus.INTERNAL_SERVER_ERROR)