Fix bug 576 about ssh keys
[osm/NBI.git] / osm_nbi / validation.py
1 # -*- coding: utf-8 -*-
2
3 from jsonschema import validate as js_v, exceptions as js_e
4
5 __author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
6 __version__ = "0.1"
7 version_date = "Mar 2018"
8
9 """
10 Validator of input data using JSON schemas for those items that not contains an OSM yang information model
11 """
12
13 # Basis schemas
14 patern_name = "^[ -~]+$"
15 nameshort_schema = {"type": "string", "minLength": 1, "maxLength": 60, "pattern": "^[^,;()\\.\\$'\"]+$"}
16 passwd_schema = {"type": "string", "minLength": 1, "maxLength": 60}
17 name_schema = {"type": "string", "minLength": 1, "maxLength": 255, "pattern": "^[^,;()'\"]+$"}
18 string_schema = {"type": "string", "minLength": 1, "maxLength": 255}
19 xml_text_schema = {"type": "string", "minLength": 1, "maxLength": 1000, "pattern": "^[^']+$"}
20 description_schema = {"type": ["string", "null"], "maxLength": 255, "pattern": "^[^'\"]+$"}
21 id_schema_fake = {"type": "string", "minLength": 2, "maxLength": 36}
22 bool_schema = {"type": "boolean"}
23 null_schema = {"type": "null"}
24 # "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}$"
25 id_schema = {"type": "string", "pattern": "^[a-fA-F0-9]{8}(-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}$"}
26 time_schema = {"type": "string", "pattern": "^[0-9]{4}-[0-1][0-9]-[0-3][0-9]T[0-2][0-9]([0-5]:){2}"}
27 pci_schema = {"type": "string", "pattern": "^[0-9a-fA-F]{4}(:[0-9a-fA-F]{2}){2}\\.[0-9a-fA-F]$"}
28 http_schema = {"type": "string", "pattern": "^https?://[^'\"=]+$"}
29 bandwidth_schema = {"type": "string", "pattern": "^[0-9]+ *([MG]bps)?$"}
30 memory_schema = {"type": "string", "pattern": "^[0-9]+ *([MG]i?[Bb])?$"}
31 integer0_schema = {"type": "integer", "minimum": 0}
32 integer1_schema = {"type": "integer", "minimum": 1}
33 path_schema = {"type": "string", "pattern": "^(\\.){0,2}(/[^/\"':{}\\(\\)]+)+$"}
34 vlan_schema = {"type": "integer", "minimum": 1, "maximum": 4095}
35 vlan1000_schema = {"type": "integer", "minimum": 1000, "maximum": 4095}
36 mac_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 dpid_Schema = {"type": "string", "pattern": "^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){7}$"}
39 # mac_schema={"type":"string", "pattern":"^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$"}
40 ip_schema = {"type": "string",
41 "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]?)$"}
42 ip_prefix_schema = {"type": "string",
43 "pattern": "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}"
44 "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/(30|[12]?[0-9])$"}
45 port_schema = {"type": "integer", "minimum": 1, "maximum": 65534}
46 object_schema = {"type": "object"}
47 schema_version_2 = {"type": "integer", "minimum": 2, "maximum": 2}
48 # schema_version_string={"type":"string","enum": ["0.1", "2", "0.2", "3", "0.3"]}
49 log_level_schema = {"type": "string", "enum": ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]}
50 checksum_schema = {"type": "string", "pattern": "^[0-9a-fA-F]{32}$"}
51 size_schema = {"type": "integer", "minimum": 1, "maximum": 100}
52 array_edition_schema = {
53 "type": "object",
54 "patternProperties": {
55 "^\\$": "Any"
56 },
57 "additionalProperties": False,
58 "minProperties": 1,
59 }
60 nameshort_list_schema = {
61 "type": "array",
62 "minItems": 1,
63 "items": nameshort_schema,
64 }
65
66
67 ns_instantiate_vdu = {
68 "title": "ns action instantiate input schema for vdu",
69 "$schema": "http://json-schema.org/draft-04/schema#",
70 "type": "object",
71 "properties": {
72 "id": name_schema,
73 "volume": {
74 "type": "array",
75 "minItems": 1,
76 "items": {
77 "type": "object",
78 "properties": {
79 "name": name_schema,
80 "vim-volume-id": name_schema,
81 },
82 "required": ["name", "vim-volume-id"],
83 "additionalProperties": False
84 }
85 },
86 "interface": {
87 "type": "array",
88 "minItems": 1,
89 "items": {
90 "type": "object",
91 "properties": {
92 "name": name_schema,
93 "ip-address": ip_schema,
94 "mac-address": mac_schema,
95 "floating-ip-required": bool_schema,
96 },
97 "required": ["name"],
98 "additionalProperties": False
99 }
100 }
101 },
102 "required": ["id"],
103 "additionalProperties": False
104 }
105
106 ip_profile_dns_schema = {
107 "type": "array",
108 "minItems": 1,
109 "items": {
110 "type": "object",
111 "properties": {
112 "address": ip_schema,
113 },
114 "required": ["address"],
115 "additionalProperties": False
116 }
117 }
118
119 ip_profile_dhcp_schema = {
120 "type": "object",
121 "properties": {
122 "enabled": {"type": "boolean"},
123 "count": integer1_schema,
124 "start-address": ip_schema
125 },
126 "additionalProperties": False,
127 }
128
129 ip_profile_schema = {
130 "title": "ip profile validation schame",
131 "$schema": "http://json-schema.org/draft-04/schema#",
132 "type": "object",
133 "properties": {
134 "ip-version": {"enum": ["ipv4", "ipv6"]},
135 "subnet-address": ip_prefix_schema,
136 "gateway-address": ip_schema,
137 "dns-server": ip_profile_dns_schema,
138 "dhcp-params": ip_profile_dhcp_schema,
139 }
140 }
141
142 ip_profile_update_schema = {
143 "title": "ip profile validation schame",
144 "$schema": "http://json-schema.org/draft-04/schema#",
145 "type": "object",
146 "properties": {
147 "ip-version": {"enum": ["ipv4", "ipv6"]},
148 "subnet-address": {"oneOf": [null_schema, ip_prefix_schema]},
149 "gateway-address": {"oneOf": [null_schema, ip_schema]},
150 "dns-server": {"oneOf": [null_schema, ip_profile_dns_schema]},
151
152 "dhcp-params": {"oneOf": [null_schema, ip_profile_dhcp_schema]},
153 },
154 "additionalProperties": False
155 }
156
157 ns_instantiate_internal_vld = {
158 "title": "ns action instantiate input schema for vdu",
159 "$schema": "http://json-schema.org/draft-04/schema#",
160 "type": "object",
161 "properties": {
162 "name": name_schema,
163 "vim-network-name": name_schema,
164 "ip-profile": ip_profile_update_schema,
165 "internal-connection-point": {
166 "type": "array",
167 "minItems": 1,
168 "items": {
169 "type": "object",
170 "properties": {
171 "id-ref": name_schema,
172 "ip-address": ip_schema,
173 # "mac-address": mac_schema,
174 },
175 "required": ["id-ref"],
176 "minProperties": 2,
177 "additionalProperties": False
178 },
179 }
180 },
181 "required": ["name"],
182 "minProperties": 2,
183 "additionalProperties": False
184 }
185
186 ns_instantiate = {
187 "title": "ns action instantiate input schema",
188 "$schema": "http://json-schema.org/draft-04/schema#",
189 "type": "object",
190 "properties": {
191 "lcmOperationType": string_schema,
192 "nsInstanceId": id_schema,
193 "nsName": name_schema,
194 "nsDescription": {"oneOf": [description_schema, {"type": "null"}]},
195 "nsdId": id_schema,
196 "vimAccountId": id_schema,
197 "ssh_keys": {"type": "array", "items": {"type": "string"}},
198 "nsr_id": id_schema,
199 "vnf": {
200 "type": "array",
201 "minItems": 1,
202 "items": {
203 "type": "object",
204 "properties": {
205 "member-vnf-index": name_schema,
206 "vimAccountId": id_schema,
207 "vdu": {
208 "type": "array",
209 "minItems": 1,
210 "items": ns_instantiate_vdu,
211 },
212 "internal-vld": {
213 "type": "array",
214 "minItems": 1,
215 "items": ns_instantiate_internal_vld
216 }
217 },
218 "required": ["member-vnf-index"],
219 "minProperties": 2,
220 "additionalProperties": False
221 }
222 },
223 "vld": {
224 "type": "array",
225 "minItems": 1,
226 "items": {
227 "type": "object",
228 "properties": {
229 "name": string_schema,
230 "vim-network-name": {"OneOf": [string_schema, object_schema]},
231 "ip-profile": object_schema,
232 "vnfd-connection-point-ref": {
233 "type": "array",
234 "minItems": 1,
235 "items": {
236 "type": "object",
237 "properties": {
238 "member-vnf-index-ref": name_schema,
239 "vnfd-connection-point-ref": name_schema,
240 "ip-address": ip_schema,
241 # "mac-address": mac_schema,
242 },
243 "required": ["member-vnf-index-ref", "vnfd-connection-point-ref"],
244 "minProperties": 3,
245 "additionalProperties": False
246 },
247 }
248 },
249 "required": ["name"],
250 "additionalProperties": False
251 }
252 },
253 },
254 "required": ["nsName", "nsdId", "vimAccountId"],
255 "additionalProperties": False
256 }
257
258 ns_action = { # TODO for the moment it is only contemplated the vnfd primitive execution
259 "title": "ns action input schema",
260 "$schema": "http://json-schema.org/draft-04/schema#",
261 "type": "object",
262 "properties": {
263 "lcmOperationType": string_schema,
264 "nsInstanceId": id_schema,
265 "member_vnf_index": name_schema,
266 "vnf_member_index": name_schema, # TODO for backward compatibility. To remove in future
267 "vdu_id": name_schema,
268 "primitive": name_schema,
269 "primitive_params": {"type": "object"},
270 },
271 "required": ["primitive", "primitive_params"], # TODO add member_vnf_index
272 "additionalProperties": False
273 }
274 ns_scale = { # TODO for the moment it is only VDU-scaling
275 "title": "ns scale input schema",
276 "$schema": "http://json-schema.org/draft-04/schema#",
277 "type": "object",
278 "properties": {
279 "lcmOperationType": string_schema,
280 "nsInstanceId": id_schema,
281 "scaleType": {"enum": ["SCALE_VNF"]},
282 "scaleVnfData": {
283 "type": "object",
284 "properties": {
285 "vnfInstanceId": name_schema,
286 "scaleVnfType": {"enum": ["SCALE_OUT", 'SCALE_IN']},
287 "scaleByStepData": {
288 "type": "object",
289 "properties": {
290 "scaling-group-descriptor": name_schema,
291 "member-vnf-index": name_schema,
292 "scaling-policy": name_schema,
293 },
294 "required": ["scaling-group-descriptor", "member-vnf-index"],
295 "additionalProperties": False
296 },
297 },
298 "required": ["scaleVnfType", "scaleByStepData"], # vnfInstanceId
299 "additionalProperties": False
300 },
301 "scaleTime": time_schema,
302 },
303 "required": ["scaleType", "scaleVnfData"],
304 "additionalProperties": False
305 }
306
307
308 schema_version = {"type": "string", "enum": ["1.0"]}
309 vim_account_edit_schema = {
310 "title": "vim_account edit input schema",
311 "$schema": "http://json-schema.org/draft-04/schema#",
312 "type": "object",
313 "properties": {
314 "name": name_schema,
315 "description": description_schema,
316 "type": nameshort_schema, # currently "openvim" or "openstack", can be enlarged with plugins
317 "vim": name_schema,
318 "datacenter": name_schema,
319 "vim_url": description_schema,
320 "vim_url_admin": description_schema,
321 "vim_tenant": name_schema,
322 "vim_tenant_name": name_schema,
323 "vim_username": nameshort_schema,
324 "vim_password": passwd_schema,
325 "config": {"type": "object"}
326 },
327 "additionalProperties": False
328 }
329 schema_type = {"type": "string"}
330
331 vim_account_new_schema = {
332 "title": "vim_account creation input schema",
333 "$schema": "http://json-schema.org/draft-04/schema#",
334 "type": "object",
335 "properties": {
336 "schema_version": schema_version,
337 "schema_type": schema_type,
338 "name": name_schema,
339 "description": description_schema,
340 "vim": name_schema,
341 "datacenter": name_schema,
342 "vim_type": {"enum": ["openstack", "openvim", "vmware", "opennebula", "aws"]},
343 "vim_url": description_schema,
344 # "vim_url_admin": description_schema,
345 # "vim_tenant": name_schema,
346 "vim_tenant_name": name_schema,
347 "vim_user": nameshort_schema,
348 "vim_password": passwd_schema,
349 "config": {"type": "object"}
350 },
351 "required": ["name", "vim_url", "vim_type", "vim_user", "vim_password", "vim_tenant_name"],
352 "additionalProperties": False
353 }
354
355
356 sdn_properties = {
357 "name": name_schema,
358 "description": description_schema,
359 "dpid": dpid_Schema,
360 "ip": ip_schema,
361 "port": port_schema,
362 "type": {"type": "string", "enum": ["opendaylight", "floodlight", "onos"]},
363 "version": {"type": "string", "minLength": 1, "maxLength": 12},
364 "user": nameshort_schema,
365 "password": passwd_schema
366 }
367 sdn_new_schema = {
368 "title": "sdn controller information schema",
369 "$schema": "http://json-schema.org/draft-04/schema#",
370 "type": "object",
371 "properties": sdn_properties,
372 "required": ["name", "port", 'ip', 'dpid', 'type'],
373 "additionalProperties": False
374 }
375 sdn_edit_schema = {
376 "title": "sdn controller update information schema",
377 "$schema": "http://json-schema.org/draft-04/schema#",
378 "type": "object",
379 "properties": sdn_properties,
380 # "required": ["name", "port", 'ip', 'dpid', 'type'],
381 "additionalProperties": False
382 }
383 sdn_port_mapping_schema = {
384 "$schema": "http://json-schema.org/draft-04/schema#",
385 "title": "sdn port mapping information schema",
386 "type": "array",
387 "items": {
388 "type": "object",
389 "properties": {
390 "compute_node": nameshort_schema,
391 "ports": {
392 "type": "array",
393 "items": {
394 "type": "object",
395 "properties": {
396 "pci": pci_schema,
397 "switch_port": nameshort_schema,
398 "switch_mac": mac_schema
399 },
400 "required": ["pci"]
401 }
402 }
403 },
404 "required": ["compute_node", "ports"]
405 }
406 }
407 sdn_external_port_schema = {
408 "$schema": "http://json-schema.org/draft-04/schema#",
409 "title": "External port information",
410 "type": "object",
411 "properties": {
412 "port": {"type": "string", "minLength": 1, "maxLength": 60},
413 "vlan": vlan_schema,
414 "mac": mac_schema
415 },
416 "required": ["port"]
417 }
418
419 # PDUs
420 pdu_interface = {
421 "type": "object",
422 "properties": {
423 "name": nameshort_schema,
424 "mgmt": bool_schema,
425 "type": {"enum": ["overlay", 'underlay']},
426 "ip_address": ip_schema,
427 # TODO, add user, password, ssh-key
428 "mac_address": mac_schema,
429 "vim_network_name": nameshort_schema, # interface is connected to one vim network, or switch port
430 "vim_network_id": nameshort_schema,
431 # provide this in case SDN assist must deal with this interface
432 "switch_dpid": dpid_Schema,
433 "switch_port": nameshort_schema,
434 "switch_mac": nameshort_schema,
435 "switch_vlan": vlan_schema,
436 },
437 "required": ["name", "mgmt", "ip_address"],
438 "additionalProperties": False
439 }
440 pdu_new_schema = {
441 "title": "pdu creation input schema",
442 "$schema": "http://json-schema.org/draft-04/schema#",
443 "type": "object",
444 "properties": {
445 "name": nameshort_schema,
446 "type": nameshort_schema,
447 "description": description_schema,
448 "shared": bool_schema,
449 "vims": nameshort_list_schema,
450 "vim_accounts": nameshort_list_schema,
451 "interfaces": {
452 "type": "array",
453 "items": {"type": pdu_interface},
454 "minItems": 1
455 }
456 },
457 "required": ["name", "type", "interfaces"],
458 "additionalProperties": False
459 }
460
461 pdu_edit_schema = {
462 "title": "pdu edit input schema",
463 "$schema": "http://json-schema.org/draft-04/schema#",
464 "type": "object",
465 "properties": {
466 "name": nameshort_schema,
467 "type": nameshort_schema,
468 "description": description_schema,
469 "shared": bool_schema,
470 "vims": {"oneOff": [array_edition_schema, nameshort_list_schema]},
471 "vim_accounts": {"oneOff": [array_edition_schema, nameshort_list_schema]},
472 "interfaces": {"oneOff": [
473 array_edition_schema,
474 {
475 "type": "array",
476 "items": {"type": pdu_interface},
477 "minItems": 1
478 }
479 ]}
480 },
481 "additionalProperties": False,
482 "minProperties": 1
483 }
484
485 # USERS
486 user_new_schema = {
487 "$schema": "http://json-schema.org/draft-04/schema#",
488 "title": "New user schema",
489 "type": "object",
490 "properties": {
491 "username": nameshort_schema,
492 "password": passwd_schema,
493 "projects": nameshort_list_schema,
494 },
495 "required": ["username", "password", "projects"],
496 "additionalProperties": False
497 }
498 user_edit_schema = {
499 "$schema": "http://json-schema.org/draft-04/schema#",
500 "title": "User edit schema for administrators",
501 "type": "object",
502 "properties": {
503 "password": passwd_schema,
504 "projects": {
505 "oneOff": [
506 nameshort_list_schema,
507 array_edition_schema
508 ]
509 },
510 },
511 "minProperties": 1,
512 "additionalProperties": False
513 }
514
515 # PROJECTS
516 project_new_schema = {
517 "$schema": "http://json-schema.org/draft-04/schema#",
518 "title": "New project schema for administrators",
519 "type": "object",
520 "properties": {
521 "name": nameshort_schema,
522 "admin": bool_schema,
523 },
524 "required": ["name"],
525 "additionalProperties": False
526 }
527 project_edit_schema = {
528 "$schema": "http://json-schema.org/draft-04/schema#",
529 "title": "Project edit schema for administrators",
530 "type": "object",
531 "properties": {
532 "admin": bool_schema,
533 },
534 "additionalProperties": False,
535 "minProperties": 1
536 }
537
538 # GLOBAL SCHEMAS
539
540 nbi_new_input_schemas = {
541 "users": user_new_schema,
542 "projects": project_new_schema,
543 "vim_accounts": vim_account_new_schema,
544 "sdns": sdn_new_schema,
545 "ns_instantiate": ns_instantiate,
546 "ns_action": ns_action,
547 "ns_scale": ns_scale,
548 "pdus": pdu_new_schema,
549 }
550
551 nbi_edit_input_schemas = {
552 "users": user_edit_schema,
553 "projects": project_edit_schema,
554 "vim_accounts": vim_account_edit_schema,
555 "sdns": sdn_edit_schema,
556 "pdus": pdu_edit_schema,
557 }
558
559
560 class ValidationError(Exception):
561 pass
562
563
564 def validate_input(indata, schema_to_use):
565 """
566 Validates input data against json schema
567 :param indata: user input data. Should be a dictionary
568 :param schema_to_use: jsonschema to test
569 :return: None if ok, raises ValidationError exception on error
570 """
571 try:
572 if schema_to_use:
573 js_v(indata, schema_to_use)
574 return None
575 except js_e.ValidationError as e:
576 if e.path:
577 error_pos = "at '" + ":".join(map(str, e.path)) + "'"
578 else:
579 error_pos = ""
580 raise ValidationError("Format error {} '{}' ".format(error_pos, e.message))