blob: 91549e00d8ef15b04f324b6588a89308e3483746 [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": "^[^,;()'\"]+$"}
18xml_text_schema = {"type": "string", "minLength": 1, "maxLength": 1000, "pattern": "^[^']+$"}
19description_schema = {"type": ["string", "null"], "maxLength": 255, "pattern": "^[^'\"]+$"}
20id_schema_fake = {"type": "string", "minLength": 2,
tierno2236d202018-05-16 19:05:16 +020021 "maxLength": 36}
22# "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 +010023id_schema = {"type": "string", "pattern": "^[a-fA-F0-9]{8}(-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}$"}
24pci_schema = {"type": "string", "pattern": "^[0-9a-fA-F]{4}(:[0-9a-fA-F]{2}){2}\.[0-9a-fA-F]$"}
25http_schema = {"type": "string", "pattern": "^https?://[^'\"=]+$"}
26bandwidth_schema = {"type": "string", "pattern": "^[0-9]+ *([MG]bps)?$"}
27memory_schema = {"type": "string", "pattern": "^[0-9]+ *([MG]i?[Bb])?$"}
28integer0_schema = {"type": "integer", "minimum": 0}
29integer1_schema = {"type": "integer", "minimum": 1}
30path_schema = {"type": "string", "pattern": "^(\.){0,2}(/[^/\"':{}\(\)]+)+$"}
31vlan_schema = {"type": "integer", "minimum": 1, "maximum": 4095}
32vlan1000_schema = {"type": "integer", "minimum": 1000, "maximum": 4095}
33mac_schema = {"type": "string",
34 "pattern": "^[0-9a-fA-F][02468aceACE](:[0-9a-fA-F]{2}){5}$"} # must be unicast: LSB bit of MSB byte ==0
35# mac_schema={"type":"string", "pattern":"^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$"}
36ip_schema = {"type": "string",
37 "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]?)$"}
38ip_prefix_schema = {"type": "string",
tierno2236d202018-05-16 19:05:16 +020039 "pattern": "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}"
40 "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/(30|[12]?[0-9])$"}
tierno0f98af52018-03-19 10:28:22 +010041port_schema = {"type": "integer", "minimum": 1, "maximum": 65534}
42object_schema = {"type": "object"}
43schema_version_2 = {"type": "integer", "minimum": 2, "maximum": 2}
44# schema_version_string={"type":"string","enum": ["0.1", "2", "0.2", "3", "0.3"]}
45log_level_schema = {"type": "string", "enum": ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]}
46checksum_schema = {"type": "string", "pattern": "^[0-9a-fA-F]{32}$"}
47size_schema = {"type": "integer", "minimum": 1, "maximum": 100}
48
tierno65acb4d2018-04-06 16:42:40 +020049
50ns_instantiate = {
51 "title": "ns action instantiate input schema",
52 "$schema": "http://json-schema.org/draft-04/schema#",
53 "type": "object",
54}
55ns_action = { # TODO for the moment it is only contemplated the vnfd primitive execution
56 "title": "ns action update input schema",
57 "$schema": "http://json-schema.org/draft-04/schema#",
58 "type": "object",
59 "properties": {
tiernof5298be2018-05-16 14:43:57 +020060 "member_vnf_index": name_schema,
61 "vnf_member_index": name_schema, # TODO for backward compatibility. To remove in future
tierno65acb4d2018-04-06 16:42:40 +020062 "primitive": name_schema,
63 "primitive_params": {"type": "object"},
64 },
tiernof5298be2018-05-16 14:43:57 +020065 "required": ["primitive", "primitive_params"], # TODO add member_vnf_index
tierno65acb4d2018-04-06 16:42:40 +020066 "additionalProperties": False
67}
68
69
tierno0f98af52018-03-19 10:28:22 +010070schema_version = {"type": "string", "enum": ["1.0"]}
tierno09c073e2018-04-26 13:36:48 +020071vim_account_edit_schema = {
72 "title": "vim_account edit input schema",
73 "$schema": "http://json-schema.org/draft-04/schema#",
74 "type": "object",
75 "properties": {
76 "name": name_schema,
77 "description": description_schema,
78 "type": nameshort_schema, # currently "openvim" or "openstack", can be enlarged with plugins
79 "vim": name_schema,
80 "datacenter": name_schema,
81 "vim_url": description_schema,
82 "vim_url_admin": description_schema,
83 "vim_tenant": name_schema,
84 "vim_tenant_name": name_schema,
85 "vim_username": nameshort_schema,
86 "vim_password": nameshort_schema,
87 "config": {"type": "object"}
88 },
89 "additionalProperties": False
90}
tierno0f98af52018-03-19 10:28:22 +010091schema_type = {"type": "string"}
92
tierno09c073e2018-04-26 13:36:48 +020093vim_account_new_schema = {
94 "title": "vim_account creation input schema",
tierno0f98af52018-03-19 10:28:22 +010095 "$schema": "http://json-schema.org/draft-04/schema#",
96 "type": "object",
97 "properties": {
98 "schema_version": schema_version,
99 "schema_type": schema_type,
100 "name": name_schema,
101 "description": description_schema,
tierno09c073e2018-04-26 13:36:48 +0200102 "vim": name_schema,
103 "datacenter": name_schema,
tierno0f98af52018-03-19 10:28:22 +0100104 "vim_type": {"enum": ["openstack", "openvim", "vmware", "opennebula", "aws"]},
105 "vim_url": description_schema,
106 # "vim_url_admin": description_schema,
107 # "vim_tenant": name_schema,
108 "vim_tenant_name": name_schema,
109 "vim_user": nameshort_schema,
110 "vim_password": nameshort_schema,
111 "config": {"type": "object"}
112 },
113 "required": ["name", "vim_url", "vim_type", "vim_user", "vim_password", "vim_tenant_name"],
114 "additionalProperties": False
115}
tierno0f98af52018-03-19 10:28:22 +0100116
117
118sdn_properties = {
119 "name": name_schema,
tiernocfb07c62018-05-10 18:30:51 +0200120 "description": description_schema,
tierno0f98af52018-03-19 10:28:22 +0100121 "dpid": {"type": "string", "pattern": "^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){7}$"},
122 "ip": ip_schema,
123 "port": port_schema,
124 "type": {"type": "string", "enum": ["opendaylight", "floodlight", "onos"]},
125 "version": {"type": "string", "minLength": 1, "maxLength": 12},
126 "user": nameshort_schema,
127 "password": passwd_schema
128}
129sdn_new_schema = {
130 "title": "sdn controller information schema",
131 "$schema": "http://json-schema.org/draft-04/schema#",
132 "type": "object",
133 "properties": sdn_properties,
134 "required": ["name", "port", 'ip', 'dpid', 'type'],
135 "additionalProperties": False
136}
137sdn_edit_schema = {
138 "title": "sdn controller update information schema",
139 "$schema": "http://json-schema.org/draft-04/schema#",
140 "type": "object",
141 "properties": sdn_properties,
tiernocfb07c62018-05-10 18:30:51 +0200142 # "required": ["name", "port", 'ip', 'dpid', 'type'],
tierno0f98af52018-03-19 10:28:22 +0100143 "additionalProperties": False
144}
145sdn_port_mapping_schema = {
146 "$schema": "http://json-schema.org/draft-04/schema#",
147 "title": "sdn port mapping information schema",
148 "type": "array",
149 "items": {
150 "type": "object",
151 "properties": {
152 "compute_node": nameshort_schema,
153 "ports": {
154 "type": "array",
155 "items": {
156 "type": "object",
157 "properties": {
158 "pci": pci_schema,
159 "switch_port": nameshort_schema,
160 "switch_mac": mac_schema
161 },
162 "required": ["pci"]
163 }
164 }
165 },
166 "required": ["compute_node", "ports"]
167 }
168}
169sdn_external_port_schema = {
170 "$schema": "http://json-schema.org/draft-04/schema#",
171 "title": "External port ingformation",
172 "type": "object",
173 "properties": {
174 "port": {"type": "string", "minLength": 1, "maxLength": 60},
175 "vlan": vlan_schema,
176 "mac": mac_schema
177 },
178 "required": ["port"]
179}
180
181
182nbi_new_input_schemas = {
tierno09c073e2018-04-26 13:36:48 +0200183 "vim_accounts": vim_account_new_schema,
tierno65acb4d2018-04-06 16:42:40 +0200184 "sdns": sdn_new_schema,
185 "ns_instantiate": ns_instantiate,
186 "ns_action": ns_action,
tierno0f98af52018-03-19 10:28:22 +0100187}
188
189nbi_edit_input_schemas = {
tierno09c073e2018-04-26 13:36:48 +0200190 "vim_accounts": vim_account_edit_schema,
tierno0f98af52018-03-19 10:28:22 +0100191 "sdns": sdn_edit_schema
192}
193
194
195class ValidationError(Exception):
196 pass
197
198
199def validate_input(indata, item, new=True):
200 """
201 Validates input data agains json schema
202 :param indata: user input data. Should be a dictionary
tierno65acb4d2018-04-06 16:42:40 +0200203 :param item: can be users, projects, vims, sdns, ns_xxxxx
tierno0f98af52018-03-19 10:28:22 +0100204 :param new: True if the validation is for creating or False if it is for editing
205 :return: None if ok, raises ValidationError exception otherwise
206 """
207 try:
208 if new:
209 schema_to_use = nbi_new_input_schemas.get(item)
210 else:
211 schema_to_use = nbi_edit_input_schemas.get(item)
212 if schema_to_use:
213 js_v(indata, schema_to_use)
214 return None
215 except js_e.ValidationError as e:
216 if e.path:
217 error_pos = "at '" + ":".join(e.path) + "'"
218 else:
219 error_pos = ""
220 raise ValidationError("Format error {} '{}' ".format(error_pos, e))