blob: 308d872c0d2294b1c5029b3e72368362b675c8cb [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": "^[^'\"]+$"}
21id_schema_fake = {"type": "string", "minLength": 2,
tierno2236d202018-05-16 19:05:16 +020022 "maxLength": 36}
23# "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 +010024id_schema = {"type": "string", "pattern": "^[a-fA-F0-9]{8}(-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}$"}
25pci_schema = {"type": "string", "pattern": "^[0-9a-fA-F]{4}(:[0-9a-fA-F]{2}){2}\.[0-9a-fA-F]$"}
26http_schema = {"type": "string", "pattern": "^https?://[^'\"=]+$"}
27bandwidth_schema = {"type": "string", "pattern": "^[0-9]+ *([MG]bps)?$"}
28memory_schema = {"type": "string", "pattern": "^[0-9]+ *([MG]i?[Bb])?$"}
29integer0_schema = {"type": "integer", "minimum": 0}
30integer1_schema = {"type": "integer", "minimum": 1}
31path_schema = {"type": "string", "pattern": "^(\.){0,2}(/[^/\"':{}\(\)]+)+$"}
32vlan_schema = {"type": "integer", "minimum": 1, "maximum": 4095}
33vlan1000_schema = {"type": "integer", "minimum": 1000, "maximum": 4095}
34mac_schema = {"type": "string",
35 "pattern": "^[0-9a-fA-F][02468aceACE](:[0-9a-fA-F]{2}){5}$"} # must be unicast: LSB bit of MSB byte ==0
36# mac_schema={"type":"string", "pattern":"^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$"}
37ip_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]?)$"}
39ip_prefix_schema = {"type": "string",
tierno2236d202018-05-16 19:05:16 +020040 "pattern": "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}"
41 "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/(30|[12]?[0-9])$"}
tierno0f98af52018-03-19 10:28:22 +010042port_schema = {"type": "integer", "minimum": 1, "maximum": 65534}
43object_schema = {"type": "object"}
44schema_version_2 = {"type": "integer", "minimum": 2, "maximum": 2}
45# schema_version_string={"type":"string","enum": ["0.1", "2", "0.2", "3", "0.3"]}
46log_level_schema = {"type": "string", "enum": ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]}
47checksum_schema = {"type": "string", "pattern": "^[0-9a-fA-F]{32}$"}
48size_schema = {"type": "integer", "minimum": 1, "maximum": 100}
49
tierno65acb4d2018-04-06 16:42:40 +020050
51ns_instantiate = {
52 "title": "ns action instantiate input schema",
53 "$schema": "http://json-schema.org/draft-04/schema#",
54 "type": "object",
tierno0da52252018-06-27 15:47:22 +020055 "properties": {
56 "nsName": name_schema,
57 "nsDescription": description_schema,
58 "nsdId": id_schema,
59 "vimAccountId": id_schema,
60 "ssh_keys": {"type": "string"},
61 "vnf": {
62 "type": "array",
63 "minItems": 1,
64 "items": {
65 "type": "object",
66 "properties": {
67 "member-vnf-index": name_schema,
68 "vimAccountId": id_schema,
69 },
70 "required": ["member-vnf-index"]
71 }
72 },
73 "vld": {
74 "type": "array",
75 "minItems": 1,
76 "items": {
77 "type": "object",
78 "properties": {
79 "name": string_schema,
80 "vim-network-name": {"OneOf": [string_schema, object_schema]},
81 "ip-profile": object_schema,
82 },
83 "required": ["name"]
84 }
85 },
86 },
87 "required": ["nsName", "nsdId", "vimAccountId"]
tierno65acb4d2018-04-06 16:42:40 +020088}
tierno0da52252018-06-27 15:47:22 +020089
tierno65acb4d2018-04-06 16:42:40 +020090ns_action = { # TODO for the moment it is only contemplated the vnfd primitive execution
91 "title": "ns action update input schema",
92 "$schema": "http://json-schema.org/draft-04/schema#",
93 "type": "object",
94 "properties": {
tiernof5298be2018-05-16 14:43:57 +020095 "member_vnf_index": name_schema,
96 "vnf_member_index": name_schema, # TODO for backward compatibility. To remove in future
tierno65acb4d2018-04-06 16:42:40 +020097 "primitive": name_schema,
98 "primitive_params": {"type": "object"},
99 },
tiernof5298be2018-05-16 14:43:57 +0200100 "required": ["primitive", "primitive_params"], # TODO add member_vnf_index
tierno65acb4d2018-04-06 16:42:40 +0200101 "additionalProperties": False
102}
103
104
tierno0f98af52018-03-19 10:28:22 +0100105schema_version = {"type": "string", "enum": ["1.0"]}
tierno09c073e2018-04-26 13:36:48 +0200106vim_account_edit_schema = {
107 "title": "vim_account edit input schema",
108 "$schema": "http://json-schema.org/draft-04/schema#",
109 "type": "object",
110 "properties": {
111 "name": name_schema,
112 "description": description_schema,
113 "type": nameshort_schema, # currently "openvim" or "openstack", can be enlarged with plugins
114 "vim": name_schema,
115 "datacenter": name_schema,
116 "vim_url": description_schema,
117 "vim_url_admin": description_schema,
118 "vim_tenant": name_schema,
119 "vim_tenant_name": name_schema,
120 "vim_username": nameshort_schema,
121 "vim_password": nameshort_schema,
122 "config": {"type": "object"}
123 },
124 "additionalProperties": False
125}
tierno0f98af52018-03-19 10:28:22 +0100126schema_type = {"type": "string"}
127
tierno09c073e2018-04-26 13:36:48 +0200128vim_account_new_schema = {
129 "title": "vim_account creation input schema",
tierno0f98af52018-03-19 10:28:22 +0100130 "$schema": "http://json-schema.org/draft-04/schema#",
131 "type": "object",
132 "properties": {
133 "schema_version": schema_version,
134 "schema_type": schema_type,
135 "name": name_schema,
136 "description": description_schema,
tierno09c073e2018-04-26 13:36:48 +0200137 "vim": name_schema,
138 "datacenter": name_schema,
tierno0f98af52018-03-19 10:28:22 +0100139 "vim_type": {"enum": ["openstack", "openvim", "vmware", "opennebula", "aws"]},
140 "vim_url": description_schema,
141 # "vim_url_admin": description_schema,
142 # "vim_tenant": name_schema,
143 "vim_tenant_name": name_schema,
144 "vim_user": nameshort_schema,
145 "vim_password": nameshort_schema,
146 "config": {"type": "object"}
147 },
148 "required": ["name", "vim_url", "vim_type", "vim_user", "vim_password", "vim_tenant_name"],
149 "additionalProperties": False
150}
tierno0f98af52018-03-19 10:28:22 +0100151
152
153sdn_properties = {
154 "name": name_schema,
tiernocfb07c62018-05-10 18:30:51 +0200155 "description": description_schema,
tierno0f98af52018-03-19 10:28:22 +0100156 "dpid": {"type": "string", "pattern": "^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){7}$"},
157 "ip": ip_schema,
158 "port": port_schema,
159 "type": {"type": "string", "enum": ["opendaylight", "floodlight", "onos"]},
160 "version": {"type": "string", "minLength": 1, "maxLength": 12},
161 "user": nameshort_schema,
162 "password": passwd_schema
163}
164sdn_new_schema = {
165 "title": "sdn controller information schema",
166 "$schema": "http://json-schema.org/draft-04/schema#",
167 "type": "object",
168 "properties": sdn_properties,
169 "required": ["name", "port", 'ip', 'dpid', 'type'],
170 "additionalProperties": False
171}
172sdn_edit_schema = {
173 "title": "sdn controller update information schema",
174 "$schema": "http://json-schema.org/draft-04/schema#",
175 "type": "object",
176 "properties": sdn_properties,
tiernocfb07c62018-05-10 18:30:51 +0200177 # "required": ["name", "port", 'ip', 'dpid', 'type'],
tierno0f98af52018-03-19 10:28:22 +0100178 "additionalProperties": False
179}
180sdn_port_mapping_schema = {
181 "$schema": "http://json-schema.org/draft-04/schema#",
182 "title": "sdn port mapping information schema",
183 "type": "array",
184 "items": {
185 "type": "object",
186 "properties": {
187 "compute_node": nameshort_schema,
188 "ports": {
189 "type": "array",
190 "items": {
191 "type": "object",
192 "properties": {
193 "pci": pci_schema,
194 "switch_port": nameshort_schema,
195 "switch_mac": mac_schema
196 },
197 "required": ["pci"]
198 }
199 }
200 },
201 "required": ["compute_node", "ports"]
202 }
203}
204sdn_external_port_schema = {
205 "$schema": "http://json-schema.org/draft-04/schema#",
206 "title": "External port ingformation",
207 "type": "object",
208 "properties": {
209 "port": {"type": "string", "minLength": 1, "maxLength": 60},
210 "vlan": vlan_schema,
211 "mac": mac_schema
212 },
213 "required": ["port"]
214}
215
216
217nbi_new_input_schemas = {
tierno09c073e2018-04-26 13:36:48 +0200218 "vim_accounts": vim_account_new_schema,
tierno65acb4d2018-04-06 16:42:40 +0200219 "sdns": sdn_new_schema,
220 "ns_instantiate": ns_instantiate,
221 "ns_action": ns_action,
tierno0f98af52018-03-19 10:28:22 +0100222}
223
224nbi_edit_input_schemas = {
tierno09c073e2018-04-26 13:36:48 +0200225 "vim_accounts": vim_account_edit_schema,
tierno0f98af52018-03-19 10:28:22 +0100226 "sdns": sdn_edit_schema
227}
228
229
230class ValidationError(Exception):
231 pass
232
233
234def validate_input(indata, item, new=True):
235 """
236 Validates input data agains json schema
237 :param indata: user input data. Should be a dictionary
tierno65acb4d2018-04-06 16:42:40 +0200238 :param item: can be users, projects, vims, sdns, ns_xxxxx
tierno0f98af52018-03-19 10:28:22 +0100239 :param new: True if the validation is for creating or False if it is for editing
240 :return: None if ok, raises ValidationError exception otherwise
241 """
242 try:
243 if new:
244 schema_to_use = nbi_new_input_schemas.get(item)
245 else:
246 schema_to_use = nbi_edit_input_schemas.get(item)
247 if schema_to_use:
248 js_v(indata, schema_to_use)
249 return None
250 except js_e.ValidationError as e:
251 if e.path:
tierno0da52252018-06-27 15:47:22 +0200252 error_pos = "at '" + ":".join(map(str, e.path)) + "'"
tierno0f98af52018-03-19 10:28:22 +0100253 else:
254 error_pos = ""
255 raise ValidationError("Format error {} '{}' ".format(error_pos, e))