blob: 7cf9d1cb942b6ace500b8e347481110250b94cdb [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
4
5__author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
6__version__ = "0.1"
7version_date = "Mar 2018"
8
9"""
10Validator of input data using JSON schemas for those items that not contains an OSM yang information model
11"""
12
13# Basis schemas
14patern_name = "^[ -~]+$"
15passwd_schema = {"type": "string", "minLength": 1, "maxLength": 60}
16nameshort_schema = {"type": "string", "minLength": 1, "maxLength": 60, "pattern": "^[^,;()'\"]+$"}
17name_schema = {"type": "string", "minLength": 1, "maxLength": 255, "pattern": "^[^,;()'\"]+$"}
tierno0da52252018-06-27 15:47:22 +020018string_schema = {"type": "string", "minLength": 1, "maxLength": 255}
tierno0f98af52018-03-19 10:28:22 +010019xml_text_schema = {"type": "string", "minLength": 1, "maxLength": 1000, "pattern": "^[^']+$"}
20description_schema = {"type": ["string", "null"], "maxLength": 255, "pattern": "^[^'\"]+$"}
tierno441dbbf2018-07-10 12:52:48 +020021id_schema_fake = {"type": "string", "minLength": 2, "maxLength": 36}
22bool_schema = {"type": "boolean"}
23null_schema = {"type": "null"}
tierno2236d202018-05-16 19:05:16 +020024# "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 +010025id_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 +020026time_schema = {"type": "string", "pattern": "^[0-9]{4}-[0-1][0-9]-[0-3][0-9]T[0-2][0-9]([0-5]:){2}"}
tierno0f98af52018-03-19 10:28:22 +010027pci_schema = {"type": "string", "pattern": "^[0-9a-fA-F]{4}(:[0-9a-fA-F]{2}){2}\.[0-9a-fA-F]$"}
28http_schema = {"type": "string", "pattern": "^https?://[^'\"=]+$"}
29bandwidth_schema = {"type": "string", "pattern": "^[0-9]+ *([MG]bps)?$"}
30memory_schema = {"type": "string", "pattern": "^[0-9]+ *([MG]i?[Bb])?$"}
31integer0_schema = {"type": "integer", "minimum": 0}
32integer1_schema = {"type": "integer", "minimum": 1}
33path_schema = {"type": "string", "pattern": "^(\.){0,2}(/[^/\"':{}\(\)]+)+$"}
34vlan_schema = {"type": "integer", "minimum": 1, "maximum": 4095}
35vlan1000_schema = {"type": "integer", "minimum": 1000, "maximum": 4095}
36mac_schema = {"type": "string",
37 "pattern": "^[0-9a-fA-F][02468aceACE](:[0-9a-fA-F]{2}){5}$"} # must be unicast: LSB bit of MSB byte ==0
38# mac_schema={"type":"string", "pattern":"^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$"}
39ip_schema = {"type": "string",
40 "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]?)$"}
41ip_prefix_schema = {"type": "string",
tierno2236d202018-05-16 19:05:16 +020042 "pattern": "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}"
43 "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/(30|[12]?[0-9])$"}
tierno0f98af52018-03-19 10:28:22 +010044port_schema = {"type": "integer", "minimum": 1, "maximum": 65534}
45object_schema = {"type": "object"}
46schema_version_2 = {"type": "integer", "minimum": 2, "maximum": 2}
47# schema_version_string={"type":"string","enum": ["0.1", "2", "0.2", "3", "0.3"]}
48log_level_schema = {"type": "string", "enum": ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]}
49checksum_schema = {"type": "string", "pattern": "^[0-9a-fA-F]{32}$"}
50size_schema = {"type": "integer", "minimum": 1, "maximum": 100}
51
tierno441dbbf2018-07-10 12:52:48 +020052ns_instantiate_vdu = {
53 "title": "ns action instantiate input schema for vdu",
54 "$schema": "http://json-schema.org/draft-04/schema#",
55 "type": "object",
56 "properties": {
57 "id": name_schema,
58 "volume": {
59 "type": "array",
60 "minItems": 1,
61 "items": {
62 "type": "object",
63 "properties": {
64 "name": name_schema,
65 "vim-volume-id": name_schema,
66 },
67 "required": ["name", "vim-volume-id"],
68 "additionalProperties": False
69 }
70 },
71 "interface": {
72 "type": "array",
73 "minItems": 1,
74 "items": {
75 "type": "object",
76 "properties": {
77 "name": name_schema,
78 "ip-address": ip_schema,
79 "mac-address": mac_schema,
80 "floating-ip-required": bool_schema,
81 },
82 "required": ["name"],
83 "additionalProperties": False
84 }
85 }
86 },
87 "required": ["id"],
88 "additionalProperties": False
89}
90
91ip_profile_dns_schema = {
92 "type": "array",
93 "minItems": 1,
94 "items": {
95 "type": "object",
96 "properties": {
97 "address": ip_schema,
98 },
99 "required": ["address"],
100 "additionalProperties": False
101 }
102}
103
104ip_profile_dhcp_schema = {
105 "type": "object",
106 "properties": {
107 "enabled": {"type": "boolean"},
108 "count": integer1_schema,
109 "start-address": ip_schema
110 },
111 "additionalProperties": False,
112}
113
114ip_profile_schema = {
115 "title": "ip profile validation schame",
116 "$schema": "http://json-schema.org/draft-04/schema#",
117 "type": "object",
118 "properties": {
119 "ip-version": {"enum": ["ipv4", "ipv6"]},
120 "subnet-address": ip_prefix_schema,
121 "gateway-address": ip_schema,
122 "dns-server": ip_profile_dns_schema,
123 "dhcp-params": ip_profile_dhcp_schema,
124 }
125}
126
127ip_profile_update_schema = {
128 "title": "ip profile validation schame",
129 "$schema": "http://json-schema.org/draft-04/schema#",
130 "type": "object",
131 "properties": {
132 "ip-version": {"enum": ["ipv4", "ipv6"]},
133 "subnet-address": {"oneOf": [null_schema, ip_prefix_schema]},
134 "gateway-address": {"oneOf": [null_schema, ip_schema]},
135 "dns-server": {"oneOf": [null_schema, ip_profile_dns_schema]},
136
137 "dhcp-params": {"oneOf": [null_schema, ip_profile_dhcp_schema]},
138 },
139 "additionalProperties": False
140}
141
142ns_instantiate_internal_vld = {
143 "title": "ns action instantiate input schema for vdu",
144 "$schema": "http://json-schema.org/draft-04/schema#",
145 "type": "object",
146 "properties": {
147 "name": name_schema,
148 "vim-network-name": name_schema,
149 "ip-profile": ip_profile_update_schema,
150 "internal-connection-point": {
151 "type": "array",
152 "minItems": 1,
153 "items": {
154 "type": "object",
155 "properties": {
156 "id-ref": name_schema,
157 "ip-address": ip_schema,
158 },
159 "required": ["id-ref", "ip-address"],
160 "additionalProperties": False
161 },
162 }
163 },
164 "required": ["name"],
165 "minProperties": 2,
166 "additionalProperties": False
167}
tierno65acb4d2018-04-06 16:42:40 +0200168
169ns_instantiate = {
170 "title": "ns action instantiate input schema",
171 "$schema": "http://json-schema.org/draft-04/schema#",
172 "type": "object",
tierno0da52252018-06-27 15:47:22 +0200173 "properties": {
174 "nsName": name_schema,
tierno441dbbf2018-07-10 12:52:48 +0200175 "nsDescription": {"oneOf": [description_schema, {"type": "null"}]},
tierno0da52252018-06-27 15:47:22 +0200176 "nsdId": id_schema,
177 "vimAccountId": id_schema,
178 "ssh_keys": {"type": "string"},
tierno441dbbf2018-07-10 12:52:48 +0200179 "nsr_id": id_schema,
tierno0da52252018-06-27 15:47:22 +0200180 "vnf": {
181 "type": "array",
182 "minItems": 1,
183 "items": {
184 "type": "object",
185 "properties": {
186 "member-vnf-index": name_schema,
187 "vimAccountId": id_schema,
tierno441dbbf2018-07-10 12:52:48 +0200188 "vdu": {
189 "type": "array",
190 "minItems": 1,
191 "items": ns_instantiate_vdu,
192 },
193 "internal-vld": {
194 "type": "array",
195 "minItems": 1,
196 "items": ns_instantiate_internal_vld
197 }
tierno0da52252018-06-27 15:47:22 +0200198 },
tierno441dbbf2018-07-10 12:52:48 +0200199 "required": ["member-vnf-index"],
200 "minProperties": 2,
201 "additionalProperties": False
tierno0da52252018-06-27 15:47:22 +0200202 }
203 },
204 "vld": {
205 "type": "array",
206 "minItems": 1,
207 "items": {
208 "type": "object",
209 "properties": {
210 "name": string_schema,
211 "vim-network-name": {"OneOf": [string_schema, object_schema]},
212 "ip-profile": object_schema,
213 },
tierno441dbbf2018-07-10 12:52:48 +0200214 "required": ["name"],
215 "additionalProperties": False
tierno0da52252018-06-27 15:47:22 +0200216 }
217 },
218 },
tierno441dbbf2018-07-10 12:52:48 +0200219 "required": ["nsName", "nsdId", "vimAccountId"],
220 "additionalProperties": False
tierno65acb4d2018-04-06 16:42:40 +0200221}
tierno0da52252018-06-27 15:47:22 +0200222
tierno65acb4d2018-04-06 16:42:40 +0200223ns_action = { # TODO for the moment it is only contemplated the vnfd primitive execution
tiernof759d822018-06-11 18:54:54 +0200224 "title": "ns action input schema",
tierno65acb4d2018-04-06 16:42:40 +0200225 "$schema": "http://json-schema.org/draft-04/schema#",
226 "type": "object",
227 "properties": {
tiernof5298be2018-05-16 14:43:57 +0200228 "member_vnf_index": name_schema,
229 "vnf_member_index": name_schema, # TODO for backward compatibility. To remove in future
tierno7ce1db92018-07-25 12:50:52 +0200230 "vdu_id": name_schema,
tierno65acb4d2018-04-06 16:42:40 +0200231 "primitive": name_schema,
232 "primitive_params": {"type": "object"},
233 },
tiernof5298be2018-05-16 14:43:57 +0200234 "required": ["primitive", "primitive_params"], # TODO add member_vnf_index
tierno65acb4d2018-04-06 16:42:40 +0200235 "additionalProperties": False
236}
tiernof759d822018-06-11 18:54:54 +0200237ns_scale = { # TODO for the moment it is only VDU-scaling
238 "title": "ns scale input schema",
239 "$schema": "http://json-schema.org/draft-04/schema#",
240 "type": "object",
241 "properties": {
242 "scaleType": {"enum": ["SCALE_VNF"]},
243 "scaleVnfData": {
244 "type": "object",
245 "properties": {
246 "vnfInstanceId": name_schema,
247 "scaleVnfType": {"enum": ["SCALE_OUT", 'SCALE_IN']},
248 "scaleByStepData": {
249 "type": "object",
250 "properties": {
251 "scaling-group-descriptor": name_schema,
252 "member-vnf-index": name_schema,
253 "scaling-policy": name_schema,
254 },
255 "required": ["scaling-group-descriptor", "member-vnf-index"],
256 "additionalProperties": False
257 },
258 },
259 "required": ["scaleVnfType", "scaleByStepData"], # vnfInstanceId
260 "additionalProperties": False
261 },
262 "scaleTime": time_schema,
263 },
264 "required": ["scaleType", "scaleVnfData"],
265 "additionalProperties": False
266}
tierno65acb4d2018-04-06 16:42:40 +0200267
268
tierno0f98af52018-03-19 10:28:22 +0100269schema_version = {"type": "string", "enum": ["1.0"]}
tierno09c073e2018-04-26 13:36:48 +0200270vim_account_edit_schema = {
271 "title": "vim_account edit input schema",
272 "$schema": "http://json-schema.org/draft-04/schema#",
273 "type": "object",
274 "properties": {
275 "name": name_schema,
276 "description": description_schema,
277 "type": nameshort_schema, # currently "openvim" or "openstack", can be enlarged with plugins
278 "vim": name_schema,
279 "datacenter": name_schema,
280 "vim_url": description_schema,
281 "vim_url_admin": description_schema,
282 "vim_tenant": name_schema,
283 "vim_tenant_name": name_schema,
284 "vim_username": nameshort_schema,
285 "vim_password": nameshort_schema,
286 "config": {"type": "object"}
287 },
288 "additionalProperties": False
289}
tierno0f98af52018-03-19 10:28:22 +0100290schema_type = {"type": "string"}
291
tierno09c073e2018-04-26 13:36:48 +0200292vim_account_new_schema = {
293 "title": "vim_account creation input schema",
tierno0f98af52018-03-19 10:28:22 +0100294 "$schema": "http://json-schema.org/draft-04/schema#",
295 "type": "object",
296 "properties": {
297 "schema_version": schema_version,
298 "schema_type": schema_type,
299 "name": name_schema,
300 "description": description_schema,
tierno09c073e2018-04-26 13:36:48 +0200301 "vim": name_schema,
302 "datacenter": name_schema,
tierno0f98af52018-03-19 10:28:22 +0100303 "vim_type": {"enum": ["openstack", "openvim", "vmware", "opennebula", "aws"]},
304 "vim_url": description_schema,
305 # "vim_url_admin": description_schema,
306 # "vim_tenant": name_schema,
307 "vim_tenant_name": name_schema,
308 "vim_user": nameshort_schema,
309 "vim_password": nameshort_schema,
310 "config": {"type": "object"}
311 },
312 "required": ["name", "vim_url", "vim_type", "vim_user", "vim_password", "vim_tenant_name"],
313 "additionalProperties": False
314}
tierno0f98af52018-03-19 10:28:22 +0100315
316
317sdn_properties = {
318 "name": name_schema,
tiernocfb07c62018-05-10 18:30:51 +0200319 "description": description_schema,
tierno0f98af52018-03-19 10:28:22 +0100320 "dpid": {"type": "string", "pattern": "^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){7}$"},
321 "ip": ip_schema,
322 "port": port_schema,
323 "type": {"type": "string", "enum": ["opendaylight", "floodlight", "onos"]},
324 "version": {"type": "string", "minLength": 1, "maxLength": 12},
325 "user": nameshort_schema,
326 "password": passwd_schema
327}
328sdn_new_schema = {
329 "title": "sdn controller information schema",
330 "$schema": "http://json-schema.org/draft-04/schema#",
331 "type": "object",
332 "properties": sdn_properties,
333 "required": ["name", "port", 'ip', 'dpid', 'type'],
334 "additionalProperties": False
335}
336sdn_edit_schema = {
337 "title": "sdn controller update information schema",
338 "$schema": "http://json-schema.org/draft-04/schema#",
339 "type": "object",
340 "properties": sdn_properties,
tiernocfb07c62018-05-10 18:30:51 +0200341 # "required": ["name", "port", 'ip', 'dpid', 'type'],
tierno0f98af52018-03-19 10:28:22 +0100342 "additionalProperties": False
343}
344sdn_port_mapping_schema = {
345 "$schema": "http://json-schema.org/draft-04/schema#",
346 "title": "sdn port mapping information schema",
347 "type": "array",
348 "items": {
349 "type": "object",
350 "properties": {
351 "compute_node": nameshort_schema,
352 "ports": {
353 "type": "array",
354 "items": {
355 "type": "object",
356 "properties": {
357 "pci": pci_schema,
358 "switch_port": nameshort_schema,
359 "switch_mac": mac_schema
360 },
361 "required": ["pci"]
362 }
363 }
364 },
365 "required": ["compute_node", "ports"]
366 }
367}
368sdn_external_port_schema = {
369 "$schema": "http://json-schema.org/draft-04/schema#",
370 "title": "External port ingformation",
371 "type": "object",
372 "properties": {
373 "port": {"type": "string", "minLength": 1, "maxLength": 60},
374 "vlan": vlan_schema,
375 "mac": mac_schema
376 },
377 "required": ["port"]
378}
379
380
381nbi_new_input_schemas = {
tierno09c073e2018-04-26 13:36:48 +0200382 "vim_accounts": vim_account_new_schema,
tierno65acb4d2018-04-06 16:42:40 +0200383 "sdns": sdn_new_schema,
384 "ns_instantiate": ns_instantiate,
385 "ns_action": ns_action,
tiernof759d822018-06-11 18:54:54 +0200386 "ns_scale": ns_scale
tierno0f98af52018-03-19 10:28:22 +0100387}
388
389nbi_edit_input_schemas = {
tierno09c073e2018-04-26 13:36:48 +0200390 "vim_accounts": vim_account_edit_schema,
tierno0f98af52018-03-19 10:28:22 +0100391 "sdns": sdn_edit_schema
392}
393
394
395class ValidationError(Exception):
396 pass
397
398
399def validate_input(indata, item, new=True):
400 """
401 Validates input data agains json schema
402 :param indata: user input data. Should be a dictionary
tierno65acb4d2018-04-06 16:42:40 +0200403 :param item: can be users, projects, vims, sdns, ns_xxxxx
tierno0f98af52018-03-19 10:28:22 +0100404 :param new: True if the validation is for creating or False if it is for editing
405 :return: None if ok, raises ValidationError exception otherwise
406 """
407 try:
408 if new:
409 schema_to_use = nbi_new_input_schemas.get(item)
410 else:
411 schema_to_use = nbi_edit_input_schemas.get(item)
412 if schema_to_use:
413 js_v(indata, schema_to_use)
414 return None
415 except js_e.ValidationError as e:
416 if e.path:
tierno0da52252018-06-27 15:47:22 +0200417 error_pos = "at '" + ":".join(map(str, e.path)) + "'"
tierno0f98af52018-03-19 10:28:22 +0100418 else:
419 error_pos = ""
tierno441dbbf2018-07-10 12:52:48 +0200420 raise ValidationError("Format error {} '{}' ".format(error_pos, e.message))