blob: b8953b553d5036d73a498a257780319ba9a975e6 [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,
Felipe Vicens07f31722018-10-29 15:16:44 +0100196 "netsliceInstanceId": id_schema,
tierno0da52252018-06-27 15:47:22 +0200197 "nsName": name_schema,
tierno441dbbf2018-07-10 12:52:48 +0200198 "nsDescription": {"oneOf": [description_schema, {"type": "null"}]},
tierno0da52252018-06-27 15:47:22 +0200199 "nsdId": id_schema,
200 "vimAccountId": id_schema,
tiernofc5089c2018-10-31 09:28:11 +0100201 "ssh_keys": {"type": "array", "items": {"type": "string"}},
tierno441dbbf2018-07-10 12:52:48 +0200202 "nsr_id": id_schema,
tiernocc103432018-10-19 14:10:35 +0200203 "vduImage": name_schema,
tierno0da52252018-06-27 15:47:22 +0200204 "vnf": {
205 "type": "array",
206 "minItems": 1,
207 "items": {
208 "type": "object",
209 "properties": {
210 "member-vnf-index": name_schema,
211 "vimAccountId": id_schema,
tierno441dbbf2018-07-10 12:52:48 +0200212 "vdu": {
213 "type": "array",
214 "minItems": 1,
215 "items": ns_instantiate_vdu,
216 },
217 "internal-vld": {
218 "type": "array",
219 "minItems": 1,
220 "items": ns_instantiate_internal_vld
221 }
tierno0da52252018-06-27 15:47:22 +0200222 },
tierno441dbbf2018-07-10 12:52:48 +0200223 "required": ["member-vnf-index"],
224 "minProperties": 2,
225 "additionalProperties": False
tierno0da52252018-06-27 15:47:22 +0200226 }
227 },
228 "vld": {
229 "type": "array",
230 "minItems": 1,
231 "items": {
232 "type": "object",
233 "properties": {
234 "name": string_schema,
235 "vim-network-name": {"OneOf": [string_schema, object_schema]},
236 "ip-profile": object_schema,
tiernob7c6f2a2018-09-03 14:32:10 +0200237 "vnfd-connection-point-ref": {
238 "type": "array",
239 "minItems": 1,
240 "items": {
241 "type": "object",
242 "properties": {
243 "member-vnf-index-ref": name_schema,
244 "vnfd-connection-point-ref": name_schema,
245 "ip-address": ip_schema,
246 # "mac-address": mac_schema,
247 },
248 "required": ["member-vnf-index-ref", "vnfd-connection-point-ref"],
249 "minProperties": 3,
250 "additionalProperties": False
251 },
252 }
tierno0da52252018-06-27 15:47:22 +0200253 },
tierno441dbbf2018-07-10 12:52:48 +0200254 "required": ["name"],
255 "additionalProperties": False
tierno0da52252018-06-27 15:47:22 +0200256 }
257 },
258 },
tierno441dbbf2018-07-10 12:52:48 +0200259 "required": ["nsName", "nsdId", "vimAccountId"],
260 "additionalProperties": False
tierno65acb4d2018-04-06 16:42:40 +0200261}
tierno0da52252018-06-27 15:47:22 +0200262
tierno65acb4d2018-04-06 16:42:40 +0200263ns_action = { # TODO for the moment it is only contemplated the vnfd primitive execution
tiernof759d822018-06-11 18:54:54 +0200264 "title": "ns action input schema",
tierno65acb4d2018-04-06 16:42:40 +0200265 "$schema": "http://json-schema.org/draft-04/schema#",
266 "type": "object",
267 "properties": {
tiernob24258a2018-10-04 18:39:49 +0200268 "lcmOperationType": string_schema,
269 "nsInstanceId": id_schema,
tiernof5298be2018-05-16 14:43:57 +0200270 "member_vnf_index": name_schema,
271 "vnf_member_index": name_schema, # TODO for backward compatibility. To remove in future
tierno7ce1db92018-07-25 12:50:52 +0200272 "vdu_id": name_schema,
tierno65acb4d2018-04-06 16:42:40 +0200273 "primitive": name_schema,
274 "primitive_params": {"type": "object"},
275 },
tiernof5298be2018-05-16 14:43:57 +0200276 "required": ["primitive", "primitive_params"], # TODO add member_vnf_index
tierno65acb4d2018-04-06 16:42:40 +0200277 "additionalProperties": False
278}
tiernof759d822018-06-11 18:54:54 +0200279ns_scale = { # TODO for the moment it is only VDU-scaling
280 "title": "ns scale input schema",
281 "$schema": "http://json-schema.org/draft-04/schema#",
282 "type": "object",
283 "properties": {
tiernob24258a2018-10-04 18:39:49 +0200284 "lcmOperationType": string_schema,
285 "nsInstanceId": id_schema,
tiernof759d822018-06-11 18:54:54 +0200286 "scaleType": {"enum": ["SCALE_VNF"]},
287 "scaleVnfData": {
288 "type": "object",
289 "properties": {
290 "vnfInstanceId": name_schema,
291 "scaleVnfType": {"enum": ["SCALE_OUT", 'SCALE_IN']},
292 "scaleByStepData": {
293 "type": "object",
294 "properties": {
295 "scaling-group-descriptor": name_schema,
296 "member-vnf-index": name_schema,
297 "scaling-policy": name_schema,
298 },
299 "required": ["scaling-group-descriptor", "member-vnf-index"],
300 "additionalProperties": False
301 },
302 },
303 "required": ["scaleVnfType", "scaleByStepData"], # vnfInstanceId
304 "additionalProperties": False
305 },
306 "scaleTime": time_schema,
307 },
308 "required": ["scaleType", "scaleVnfData"],
309 "additionalProperties": False
310}
tierno65acb4d2018-04-06 16:42:40 +0200311
312
tierno0f98af52018-03-19 10:28:22 +0100313schema_version = {"type": "string", "enum": ["1.0"]}
tierno09c073e2018-04-26 13:36:48 +0200314vim_account_edit_schema = {
315 "title": "vim_account edit input schema",
316 "$schema": "http://json-schema.org/draft-04/schema#",
317 "type": "object",
318 "properties": {
319 "name": name_schema,
320 "description": description_schema,
321 "type": nameshort_schema, # currently "openvim" or "openstack", can be enlarged with plugins
322 "vim": name_schema,
323 "datacenter": name_schema,
324 "vim_url": description_schema,
325 "vim_url_admin": description_schema,
326 "vim_tenant": name_schema,
327 "vim_tenant_name": name_schema,
328 "vim_username": nameshort_schema,
tiernocd54a4a2018-09-12 16:40:35 +0200329 "vim_password": passwd_schema,
tierno09c073e2018-04-26 13:36:48 +0200330 "config": {"type": "object"}
331 },
332 "additionalProperties": False
333}
tierno0f98af52018-03-19 10:28:22 +0100334schema_type = {"type": "string"}
335
tierno09c073e2018-04-26 13:36:48 +0200336vim_account_new_schema = {
337 "title": "vim_account creation input schema",
tierno0f98af52018-03-19 10:28:22 +0100338 "$schema": "http://json-schema.org/draft-04/schema#",
339 "type": "object",
340 "properties": {
341 "schema_version": schema_version,
342 "schema_type": schema_type,
343 "name": name_schema,
344 "description": description_schema,
tierno09c073e2018-04-26 13:36:48 +0200345 "vim": name_schema,
346 "datacenter": name_schema,
tierno0f98af52018-03-19 10:28:22 +0100347 "vim_type": {"enum": ["openstack", "openvim", "vmware", "opennebula", "aws"]},
348 "vim_url": description_schema,
349 # "vim_url_admin": description_schema,
350 # "vim_tenant": name_schema,
351 "vim_tenant_name": name_schema,
352 "vim_user": nameshort_schema,
tiernocd54a4a2018-09-12 16:40:35 +0200353 "vim_password": passwd_schema,
tierno0f98af52018-03-19 10:28:22 +0100354 "config": {"type": "object"}
355 },
356 "required": ["name", "vim_url", "vim_type", "vim_user", "vim_password", "vim_tenant_name"],
357 "additionalProperties": False
358}
tierno0f98af52018-03-19 10:28:22 +0100359
360
361sdn_properties = {
362 "name": name_schema,
tiernocfb07c62018-05-10 18:30:51 +0200363 "description": description_schema,
tiernocb83c942018-09-24 17:28:13 +0200364 "dpid": dpid_Schema,
tierno0f98af52018-03-19 10:28:22 +0100365 "ip": ip_schema,
366 "port": port_schema,
367 "type": {"type": "string", "enum": ["opendaylight", "floodlight", "onos"]},
368 "version": {"type": "string", "minLength": 1, "maxLength": 12},
369 "user": nameshort_schema,
370 "password": passwd_schema
371}
372sdn_new_schema = {
373 "title": "sdn controller information schema",
374 "$schema": "http://json-schema.org/draft-04/schema#",
375 "type": "object",
376 "properties": sdn_properties,
377 "required": ["name", "port", 'ip', 'dpid', 'type'],
378 "additionalProperties": False
379}
380sdn_edit_schema = {
381 "title": "sdn controller update information schema",
382 "$schema": "http://json-schema.org/draft-04/schema#",
383 "type": "object",
384 "properties": sdn_properties,
tiernocfb07c62018-05-10 18:30:51 +0200385 # "required": ["name", "port", 'ip', 'dpid', 'type'],
tierno0f98af52018-03-19 10:28:22 +0100386 "additionalProperties": False
387}
388sdn_port_mapping_schema = {
389 "$schema": "http://json-schema.org/draft-04/schema#",
390 "title": "sdn port mapping information schema",
391 "type": "array",
392 "items": {
393 "type": "object",
394 "properties": {
395 "compute_node": nameshort_schema,
396 "ports": {
397 "type": "array",
398 "items": {
399 "type": "object",
400 "properties": {
tierno42fce592018-11-02 09:23:43 +0100401 "pci": pci_extended_schema,
tierno0f98af52018-03-19 10:28:22 +0100402 "switch_port": nameshort_schema,
403 "switch_mac": mac_schema
404 },
405 "required": ["pci"]
406 }
407 }
408 },
409 "required": ["compute_node", "ports"]
410 }
411}
412sdn_external_port_schema = {
413 "$schema": "http://json-schema.org/draft-04/schema#",
tiernocd54a4a2018-09-12 16:40:35 +0200414 "title": "External port information",
tierno0f98af52018-03-19 10:28:22 +0100415 "type": "object",
416 "properties": {
417 "port": {"type": "string", "minLength": 1, "maxLength": 60},
418 "vlan": vlan_schema,
419 "mac": mac_schema
420 },
421 "required": ["port"]
422}
423
tiernocb83c942018-09-24 17:28:13 +0200424# PDUs
425pdu_interface = {
426 "type": "object",
427 "properties": {
428 "name": nameshort_schema,
429 "mgmt": bool_schema,
430 "type": {"enum": ["overlay", 'underlay']},
tierno36ec8602018-11-02 17:27:11 +0100431 "ip-address": ip_schema,
tiernocb83c942018-09-24 17:28:13 +0200432 # TODO, add user, password, ssh-key
tierno36ec8602018-11-02 17:27:11 +0100433 "mac-address": mac_schema,
434 "vim-network-name": nameshort_schema, # interface is connected to one vim network, or switch port
435 # TODO "vim-network-id": nameshort_schema,
436 # # provide this in case SDN assist must deal with this interface
437 # "switch-dpid": dpid_Schema,
438 # "switch-port": nameshort_schema,
439 # "switch-mac": nameshort_schema,
440 # "switch-vlan": vlan_schema,
tiernocb83c942018-09-24 17:28:13 +0200441 },
tierno36ec8602018-11-02 17:27:11 +0100442 "required": ["name", "mgmt", "ip-address"],
tiernocb83c942018-09-24 17:28:13 +0200443 "additionalProperties": False
tiernocd54a4a2018-09-12 16:40:35 +0200444}
tiernocb83c942018-09-24 17:28:13 +0200445pdu_new_schema = {
446 "title": "pdu creation input schema",
447 "$schema": "http://json-schema.org/draft-04/schema#",
448 "type": "object",
449 "properties": {
450 "name": nameshort_schema,
451 "type": nameshort_schema,
452 "description": description_schema,
453 "shared": bool_schema,
454 "vims": nameshort_list_schema,
455 "vim_accounts": nameshort_list_schema,
456 "interfaces": {
457 "type": "array",
tierno36ec8602018-11-02 17:27:11 +0100458 "items": pdu_interface,
tiernocb83c942018-09-24 17:28:13 +0200459 "minItems": 1
460 }
461 },
462 "required": ["name", "type", "interfaces"],
463 "additionalProperties": False
464}
465
466pdu_edit_schema = {
467 "title": "pdu edit input schema",
468 "$schema": "http://json-schema.org/draft-04/schema#",
469 "type": "object",
470 "properties": {
471 "name": nameshort_schema,
472 "type": nameshort_schema,
473 "description": description_schema,
474 "shared": bool_schema,
475 "vims": {"oneOff": [array_edition_schema, nameshort_list_schema]},
476 "vim_accounts": {"oneOff": [array_edition_schema, nameshort_list_schema]},
477 "interfaces": {"oneOff": [
478 array_edition_schema,
479 {
480 "type": "array",
tierno36ec8602018-11-02 17:27:11 +0100481 "items": pdu_interface,
tiernocb83c942018-09-24 17:28:13 +0200482 "minItems": 1
483 }
484 ]}
485 },
486 "additionalProperties": False,
487 "minProperties": 1
488}
489
490# USERS
tiernocd54a4a2018-09-12 16:40:35 +0200491user_new_schema = {
492 "$schema": "http://json-schema.org/draft-04/schema#",
493 "title": "New user schema",
494 "type": "object",
495 "properties": {
496 "username": nameshort_schema,
497 "password": passwd_schema,
tiernocb83c942018-09-24 17:28:13 +0200498 "projects": nameshort_list_schema,
tiernocd54a4a2018-09-12 16:40:35 +0200499 },
500 "required": ["username", "password", "projects"],
501 "additionalProperties": False
502}
503user_edit_schema = {
504 "$schema": "http://json-schema.org/draft-04/schema#",
505 "title": "User edit schema for administrators",
506 "type": "object",
507 "properties": {
508 "password": passwd_schema,
509 "projects": {
510 "oneOff": [
tiernocb83c942018-09-24 17:28:13 +0200511 nameshort_list_schema,
tiernocd54a4a2018-09-12 16:40:35 +0200512 array_edition_schema
513 ]
514 },
tiernocb83c942018-09-24 17:28:13 +0200515 },
516 "minProperties": 1,
517 "additionalProperties": False
tiernocd54a4a2018-09-12 16:40:35 +0200518}
519
520# PROJECTS
521project_new_schema = {
522 "$schema": "http://json-schema.org/draft-04/schema#",
523 "title": "New project schema for administrators",
524 "type": "object",
525 "properties": {
526 "name": nameshort_schema,
527 "admin": bool_schema,
528 },
529 "required": ["name"],
530 "additionalProperties": False
531}
532project_edit_schema = {
533 "$schema": "http://json-schema.org/draft-04/schema#",
534 "title": "Project edit schema for administrators",
535 "type": "object",
536 "properties": {
537 "admin": bool_schema,
538 },
539 "additionalProperties": False,
540 "minProperties": 1
541}
542
543# GLOBAL SCHEMAS
tierno0f98af52018-03-19 10:28:22 +0100544
545nbi_new_input_schemas = {
tiernocd54a4a2018-09-12 16:40:35 +0200546 "users": user_new_schema,
547 "projects": project_new_schema,
tierno09c073e2018-04-26 13:36:48 +0200548 "vim_accounts": vim_account_new_schema,
tierno65acb4d2018-04-06 16:42:40 +0200549 "sdns": sdn_new_schema,
550 "ns_instantiate": ns_instantiate,
551 "ns_action": ns_action,
tiernocb83c942018-09-24 17:28:13 +0200552 "ns_scale": ns_scale,
553 "pdus": pdu_new_schema,
tierno0f98af52018-03-19 10:28:22 +0100554}
555
556nbi_edit_input_schemas = {
tiernocd54a4a2018-09-12 16:40:35 +0200557 "users": user_edit_schema,
558 "projects": project_edit_schema,
tierno09c073e2018-04-26 13:36:48 +0200559 "vim_accounts": vim_account_edit_schema,
tiernocb83c942018-09-24 17:28:13 +0200560 "sdns": sdn_edit_schema,
561 "pdus": pdu_edit_schema,
tierno0f98af52018-03-19 10:28:22 +0100562}
563
Felipe Vicens07f31722018-10-29 15:16:44 +0100564# NETSLICE SCHEMAS
565nsi_instantiate = {
566 "title": "netslice action instantiate input schema",
567 "$schema": "http://json-schema.org/draft-04/schema#",
568 "type": "object",
569 "properties": {
570 "lcmOperationType": string_schema,
571 "nsiInstanceId": id_schema,
572 "nsiName": name_schema,
573 "nsiDescription": {"oneOf": [description_schema, {"type": "null"}]},
574 "nstdId": string_schema,
575 "vimAccountId": id_schema,
576 "ssh_keys": {"type": "string"},
577 "nsi_id": id_schema,
578 "ns": {
579 "type": "array",
580 "minItems": 1,
581 "items": ns_instantiate
582 },
583 },
584 "required": ["nsiName", "nstdId", "vimAccountId"],
585 "additionalProperties": False
586}
587
588nsi_action = {
589
590}
591
592nsi_terminate = {
593
594}
595
tierno0f98af52018-03-19 10:28:22 +0100596
597class ValidationError(Exception):
tierno36ec8602018-11-02 17:27:11 +0100598 def __init__(self, message, http_code=HTTPStatus.UNPROCESSABLE_ENTITY):
599 self.http_code = http_code
600 Exception.__init__(self, message)
tierno0f98af52018-03-19 10:28:22 +0100601
602
tiernob24258a2018-10-04 18:39:49 +0200603def validate_input(indata, schema_to_use):
tierno0f98af52018-03-19 10:28:22 +0100604 """
tiernocd54a4a2018-09-12 16:40:35 +0200605 Validates input data against json schema
tierno0f98af52018-03-19 10:28:22 +0100606 :param indata: user input data. Should be a dictionary
tiernob24258a2018-10-04 18:39:49 +0200607 :param schema_to_use: jsonschema to test
608 :return: None if ok, raises ValidationError exception on error
tierno0f98af52018-03-19 10:28:22 +0100609 """
610 try:
tierno0f98af52018-03-19 10:28:22 +0100611 if schema_to_use:
612 js_v(indata, schema_to_use)
613 return None
614 except js_e.ValidationError as e:
615 if e.path:
tierno0da52252018-06-27 15:47:22 +0200616 error_pos = "at '" + ":".join(map(str, e.path)) + "'"
tierno0f98af52018-03-19 10:28:22 +0100617 else:
618 error_pos = ""
tierno441dbbf2018-07-10 12:52:48 +0200619 raise ValidationError("Format error {} '{}' ".format(error_pos, e.message))
tierno36ec8602018-11-02 17:27:11 +0100620 except js_e.SchemaError:
621 raise ValidationError("Bad json schema {}".format(schema_to_use), http_code=HTTPStatus.INTERNAL_SERVER_ERROR)