blob: 70003a96e308904542c42802cc4913966dd7e61b [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,
21 "maxLength": 36} # "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}$"
22id_schema = {"type": "string", "pattern": "^[a-fA-F0-9]{8}(-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}$"}
23pci_schema = {"type": "string", "pattern": "^[0-9a-fA-F]{4}(:[0-9a-fA-F]{2}){2}\.[0-9a-fA-F]$"}
24http_schema = {"type": "string", "pattern": "^https?://[^'\"=]+$"}
25bandwidth_schema = {"type": "string", "pattern": "^[0-9]+ *([MG]bps)?$"}
26memory_schema = {"type": "string", "pattern": "^[0-9]+ *([MG]i?[Bb])?$"}
27integer0_schema = {"type": "integer", "minimum": 0}
28integer1_schema = {"type": "integer", "minimum": 1}
29path_schema = {"type": "string", "pattern": "^(\.){0,2}(/[^/\"':{}\(\)]+)+$"}
30vlan_schema = {"type": "integer", "minimum": 1, "maximum": 4095}
31vlan1000_schema = {"type": "integer", "minimum": 1000, "maximum": 4095}
32mac_schema = {"type": "string",
33 "pattern": "^[0-9a-fA-F][02468aceACE](:[0-9a-fA-F]{2}){5}$"} # must be unicast: LSB bit of MSB byte ==0
34# mac_schema={"type":"string", "pattern":"^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$"}
35ip_schema = {"type": "string",
36 "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]?)$"}
37ip_prefix_schema = {"type": "string",
38 "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]?)/(30|[12]?[0-9])$"}
39port_schema = {"type": "integer", "minimum": 1, "maximum": 65534}
40object_schema = {"type": "object"}
41schema_version_2 = {"type": "integer", "minimum": 2, "maximum": 2}
42# schema_version_string={"type":"string","enum": ["0.1", "2", "0.2", "3", "0.3"]}
43log_level_schema = {"type": "string", "enum": ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]}
44checksum_schema = {"type": "string", "pattern": "^[0-9a-fA-F]{32}$"}
45size_schema = {"type": "integer", "minimum": 1, "maximum": 100}
46
tierno65acb4d2018-04-06 16:42:40 +020047
48ns_instantiate = {
49 "title": "ns action instantiate input schema",
50 "$schema": "http://json-schema.org/draft-04/schema#",
51 "type": "object",
52}
53ns_action = { # TODO for the moment it is only contemplated the vnfd primitive execution
54 "title": "ns action update input schema",
55 "$schema": "http://json-schema.org/draft-04/schema#",
56 "type": "object",
57 "properties": {
tiernof5298be2018-05-16 14:43:57 +020058 "member_vnf_index": name_schema,
59 "vnf_member_index": name_schema, # TODO for backward compatibility. To remove in future
tierno65acb4d2018-04-06 16:42:40 +020060 "primitive": name_schema,
61 "primitive_params": {"type": "object"},
62 },
tiernof5298be2018-05-16 14:43:57 +020063 "required": ["primitive", "primitive_params"], # TODO add member_vnf_index
tierno65acb4d2018-04-06 16:42:40 +020064 "additionalProperties": False
65}
66
67
tierno0f98af52018-03-19 10:28:22 +010068schema_version = {"type": "string", "enum": ["1.0"]}
tierno09c073e2018-04-26 13:36:48 +020069vim_account_edit_schema = {
70 "title": "vim_account edit input schema",
71 "$schema": "http://json-schema.org/draft-04/schema#",
72 "type": "object",
73 "properties": {
74 "name": name_schema,
75 "description": description_schema,
76 "type": nameshort_schema, # currently "openvim" or "openstack", can be enlarged with plugins
77 "vim": name_schema,
78 "datacenter": name_schema,
79 "vim_url": description_schema,
80 "vim_url_admin": description_schema,
81 "vim_tenant": name_schema,
82 "vim_tenant_name": name_schema,
83 "vim_username": nameshort_schema,
84 "vim_password": nameshort_schema,
85 "config": {"type": "object"}
86 },
87 "additionalProperties": False
88}
tierno0f98af52018-03-19 10:28:22 +010089schema_type = {"type": "string"}
90
tierno09c073e2018-04-26 13:36:48 +020091vim_account_new_schema = {
92 "title": "vim_account creation input schema",
tierno0f98af52018-03-19 10:28:22 +010093 "$schema": "http://json-schema.org/draft-04/schema#",
94 "type": "object",
95 "properties": {
96 "schema_version": schema_version,
97 "schema_type": schema_type,
98 "name": name_schema,
99 "description": description_schema,
tierno09c073e2018-04-26 13:36:48 +0200100 "vim": name_schema,
101 "datacenter": name_schema,
tierno0f98af52018-03-19 10:28:22 +0100102 "vim_type": {"enum": ["openstack", "openvim", "vmware", "opennebula", "aws"]},
103 "vim_url": description_schema,
104 # "vim_url_admin": description_schema,
105 # "vim_tenant": name_schema,
106 "vim_tenant_name": name_schema,
107 "vim_user": nameshort_schema,
108 "vim_password": nameshort_schema,
109 "config": {"type": "object"}
110 },
111 "required": ["name", "vim_url", "vim_type", "vim_user", "vim_password", "vim_tenant_name"],
112 "additionalProperties": False
113}
tierno0f98af52018-03-19 10:28:22 +0100114
115
116sdn_properties = {
117 "name": name_schema,
tiernocfb07c62018-05-10 18:30:51 +0200118 "description": description_schema,
tierno0f98af52018-03-19 10:28:22 +0100119 "dpid": {"type": "string", "pattern": "^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){7}$"},
120 "ip": ip_schema,
121 "port": port_schema,
122 "type": {"type": "string", "enum": ["opendaylight", "floodlight", "onos"]},
123 "version": {"type": "string", "minLength": 1, "maxLength": 12},
124 "user": nameshort_schema,
125 "password": passwd_schema
126}
127sdn_new_schema = {
128 "title": "sdn controller information schema",
129 "$schema": "http://json-schema.org/draft-04/schema#",
130 "type": "object",
131 "properties": sdn_properties,
132 "required": ["name", "port", 'ip', 'dpid', 'type'],
133 "additionalProperties": False
134}
135sdn_edit_schema = {
136 "title": "sdn controller update information schema",
137 "$schema": "http://json-schema.org/draft-04/schema#",
138 "type": "object",
139 "properties": sdn_properties,
tiernocfb07c62018-05-10 18:30:51 +0200140 # "required": ["name", "port", 'ip', 'dpid', 'type'],
tierno0f98af52018-03-19 10:28:22 +0100141 "additionalProperties": False
142}
143sdn_port_mapping_schema = {
144 "$schema": "http://json-schema.org/draft-04/schema#",
145 "title": "sdn port mapping information schema",
146 "type": "array",
147 "items": {
148 "type": "object",
149 "properties": {
150 "compute_node": nameshort_schema,
151 "ports": {
152 "type": "array",
153 "items": {
154 "type": "object",
155 "properties": {
156 "pci": pci_schema,
157 "switch_port": nameshort_schema,
158 "switch_mac": mac_schema
159 },
160 "required": ["pci"]
161 }
162 }
163 },
164 "required": ["compute_node", "ports"]
165 }
166}
167sdn_external_port_schema = {
168 "$schema": "http://json-schema.org/draft-04/schema#",
169 "title": "External port ingformation",
170 "type": "object",
171 "properties": {
172 "port": {"type": "string", "minLength": 1, "maxLength": 60},
173 "vlan": vlan_schema,
174 "mac": mac_schema
175 },
176 "required": ["port"]
177}
178
179
180nbi_new_input_schemas = {
tierno09c073e2018-04-26 13:36:48 +0200181 "vim_accounts": vim_account_new_schema,
tierno65acb4d2018-04-06 16:42:40 +0200182 "sdns": sdn_new_schema,
183 "ns_instantiate": ns_instantiate,
184 "ns_action": ns_action,
tierno0f98af52018-03-19 10:28:22 +0100185}
186
187nbi_edit_input_schemas = {
tierno09c073e2018-04-26 13:36:48 +0200188 "vim_accounts": vim_account_edit_schema,
tierno0f98af52018-03-19 10:28:22 +0100189 "sdns": sdn_edit_schema
190}
191
192
193class ValidationError(Exception):
194 pass
195
196
197def validate_input(indata, item, new=True):
198 """
199 Validates input data agains json schema
200 :param indata: user input data. Should be a dictionary
tierno65acb4d2018-04-06 16:42:40 +0200201 :param item: can be users, projects, vims, sdns, ns_xxxxx
tierno0f98af52018-03-19 10:28:22 +0100202 :param new: True if the validation is for creating or False if it is for editing
203 :return: None if ok, raises ValidationError exception otherwise
204 """
205 try:
206 if new:
207 schema_to_use = nbi_new_input_schemas.get(item)
208 else:
209 schema_to_use = nbi_edit_input_schemas.get(item)
210 if schema_to_use:
211 js_v(indata, schema_to_use)
212 return None
213 except js_e.ValidationError as e:
214 if e.path:
215 error_pos = "at '" + ":".join(e.path) + "'"
216 else:
217 error_pos = ""
218 raise ValidationError("Format error {} '{}' ".format(error_pos, e))