blob: cb249ba539a37c13197f449098f87ced56dee1a0 [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}$"}
tiernof759d822018-06-11 18:54:54 +020025time_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 +010026pci_schema = {"type": "string", "pattern": "^[0-9a-fA-F]{4}(:[0-9a-fA-F]{2}){2}\.[0-9a-fA-F]$"}
27http_schema = {"type": "string", "pattern": "^https?://[^'\"=]+$"}
28bandwidth_schema = {"type": "string", "pattern": "^[0-9]+ *([MG]bps)?$"}
29memory_schema = {"type": "string", "pattern": "^[0-9]+ *([MG]i?[Bb])?$"}
30integer0_schema = {"type": "integer", "minimum": 0}
31integer1_schema = {"type": "integer", "minimum": 1}
32path_schema = {"type": "string", "pattern": "^(\.){0,2}(/[^/\"':{}\(\)]+)+$"}
33vlan_schema = {"type": "integer", "minimum": 1, "maximum": 4095}
34vlan1000_schema = {"type": "integer", "minimum": 1000, "maximum": 4095}
35mac_schema = {"type": "string",
36 "pattern": "^[0-9a-fA-F][02468aceACE](:[0-9a-fA-F]{2}){5}$"} # must be unicast: LSB bit of MSB byte ==0
37# mac_schema={"type":"string", "pattern":"^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$"}
38ip_schema = {"type": "string",
39 "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]?)$"}
40ip_prefix_schema = {"type": "string",
tierno2236d202018-05-16 19:05:16 +020041 "pattern": "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}"
42 "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/(30|[12]?[0-9])$"}
tierno0f98af52018-03-19 10:28:22 +010043port_schema = {"type": "integer", "minimum": 1, "maximum": 65534}
44object_schema = {"type": "object"}
45schema_version_2 = {"type": "integer", "minimum": 2, "maximum": 2}
46# schema_version_string={"type":"string","enum": ["0.1", "2", "0.2", "3", "0.3"]}
47log_level_schema = {"type": "string", "enum": ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]}
48checksum_schema = {"type": "string", "pattern": "^[0-9a-fA-F]{32}$"}
49size_schema = {"type": "integer", "minimum": 1, "maximum": 100}
50
tierno65acb4d2018-04-06 16:42:40 +020051
52ns_instantiate = {
53 "title": "ns action instantiate input schema",
54 "$schema": "http://json-schema.org/draft-04/schema#",
55 "type": "object",
tierno0da52252018-06-27 15:47:22 +020056 "properties": {
57 "nsName": name_schema,
58 "nsDescription": description_schema,
59 "nsdId": id_schema,
60 "vimAccountId": id_schema,
61 "ssh_keys": {"type": "string"},
62 "vnf": {
63 "type": "array",
64 "minItems": 1,
65 "items": {
66 "type": "object",
67 "properties": {
68 "member-vnf-index": name_schema,
69 "vimAccountId": id_schema,
70 },
71 "required": ["member-vnf-index"]
72 }
73 },
74 "vld": {
75 "type": "array",
76 "minItems": 1,
77 "items": {
78 "type": "object",
79 "properties": {
80 "name": string_schema,
81 "vim-network-name": {"OneOf": [string_schema, object_schema]},
82 "ip-profile": object_schema,
83 },
84 "required": ["name"]
85 }
86 },
87 },
88 "required": ["nsName", "nsdId", "vimAccountId"]
tierno65acb4d2018-04-06 16:42:40 +020089}
tierno0da52252018-06-27 15:47:22 +020090
tierno65acb4d2018-04-06 16:42:40 +020091ns_action = { # TODO for the moment it is only contemplated the vnfd primitive execution
tiernof759d822018-06-11 18:54:54 +020092 "title": "ns action input schema",
tierno65acb4d2018-04-06 16:42:40 +020093 "$schema": "http://json-schema.org/draft-04/schema#",
94 "type": "object",
95 "properties": {
tiernof5298be2018-05-16 14:43:57 +020096 "member_vnf_index": name_schema,
97 "vnf_member_index": name_schema, # TODO for backward compatibility. To remove in future
tierno65acb4d2018-04-06 16:42:40 +020098 "primitive": name_schema,
99 "primitive_params": {"type": "object"},
100 },
tiernof5298be2018-05-16 14:43:57 +0200101 "required": ["primitive", "primitive_params"], # TODO add member_vnf_index
tierno65acb4d2018-04-06 16:42:40 +0200102 "additionalProperties": False
103}
tiernof759d822018-06-11 18:54:54 +0200104ns_scale = { # TODO for the moment it is only VDU-scaling
105 "title": "ns scale input schema",
106 "$schema": "http://json-schema.org/draft-04/schema#",
107 "type": "object",
108 "properties": {
109 "scaleType": {"enum": ["SCALE_VNF"]},
110 "scaleVnfData": {
111 "type": "object",
112 "properties": {
113 "vnfInstanceId": name_schema,
114 "scaleVnfType": {"enum": ["SCALE_OUT", 'SCALE_IN']},
115 "scaleByStepData": {
116 "type": "object",
117 "properties": {
118 "scaling-group-descriptor": name_schema,
119 "member-vnf-index": name_schema,
120 "scaling-policy": name_schema,
121 },
122 "required": ["scaling-group-descriptor", "member-vnf-index"],
123 "additionalProperties": False
124 },
125 },
126 "required": ["scaleVnfType", "scaleByStepData"], # vnfInstanceId
127 "additionalProperties": False
128 },
129 "scaleTime": time_schema,
130 },
131 "required": ["scaleType", "scaleVnfData"],
132 "additionalProperties": False
133}
tierno65acb4d2018-04-06 16:42:40 +0200134
135
tierno0f98af52018-03-19 10:28:22 +0100136schema_version = {"type": "string", "enum": ["1.0"]}
tierno09c073e2018-04-26 13:36:48 +0200137vim_account_edit_schema = {
138 "title": "vim_account edit input schema",
139 "$schema": "http://json-schema.org/draft-04/schema#",
140 "type": "object",
141 "properties": {
142 "name": name_schema,
143 "description": description_schema,
144 "type": nameshort_schema, # currently "openvim" or "openstack", can be enlarged with plugins
145 "vim": name_schema,
146 "datacenter": name_schema,
147 "vim_url": description_schema,
148 "vim_url_admin": description_schema,
149 "vim_tenant": name_schema,
150 "vim_tenant_name": name_schema,
151 "vim_username": nameshort_schema,
152 "vim_password": nameshort_schema,
153 "config": {"type": "object"}
154 },
155 "additionalProperties": False
156}
tierno0f98af52018-03-19 10:28:22 +0100157schema_type = {"type": "string"}
158
tierno09c073e2018-04-26 13:36:48 +0200159vim_account_new_schema = {
160 "title": "vim_account creation input schema",
tierno0f98af52018-03-19 10:28:22 +0100161 "$schema": "http://json-schema.org/draft-04/schema#",
162 "type": "object",
163 "properties": {
164 "schema_version": schema_version,
165 "schema_type": schema_type,
166 "name": name_schema,
167 "description": description_schema,
tierno09c073e2018-04-26 13:36:48 +0200168 "vim": name_schema,
169 "datacenter": name_schema,
tierno0f98af52018-03-19 10:28:22 +0100170 "vim_type": {"enum": ["openstack", "openvim", "vmware", "opennebula", "aws"]},
171 "vim_url": description_schema,
172 # "vim_url_admin": description_schema,
173 # "vim_tenant": name_schema,
174 "vim_tenant_name": name_schema,
175 "vim_user": nameshort_schema,
176 "vim_password": nameshort_schema,
177 "config": {"type": "object"}
178 },
179 "required": ["name", "vim_url", "vim_type", "vim_user", "vim_password", "vim_tenant_name"],
180 "additionalProperties": False
181}
tierno0f98af52018-03-19 10:28:22 +0100182
183
184sdn_properties = {
185 "name": name_schema,
tiernocfb07c62018-05-10 18:30:51 +0200186 "description": description_schema,
tierno0f98af52018-03-19 10:28:22 +0100187 "dpid": {"type": "string", "pattern": "^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){7}$"},
188 "ip": ip_schema,
189 "port": port_schema,
190 "type": {"type": "string", "enum": ["opendaylight", "floodlight", "onos"]},
191 "version": {"type": "string", "minLength": 1, "maxLength": 12},
192 "user": nameshort_schema,
193 "password": passwd_schema
194}
195sdn_new_schema = {
196 "title": "sdn controller information schema",
197 "$schema": "http://json-schema.org/draft-04/schema#",
198 "type": "object",
199 "properties": sdn_properties,
200 "required": ["name", "port", 'ip', 'dpid', 'type'],
201 "additionalProperties": False
202}
203sdn_edit_schema = {
204 "title": "sdn controller update information schema",
205 "$schema": "http://json-schema.org/draft-04/schema#",
206 "type": "object",
207 "properties": sdn_properties,
tiernocfb07c62018-05-10 18:30:51 +0200208 # "required": ["name", "port", 'ip', 'dpid', 'type'],
tierno0f98af52018-03-19 10:28:22 +0100209 "additionalProperties": False
210}
211sdn_port_mapping_schema = {
212 "$schema": "http://json-schema.org/draft-04/schema#",
213 "title": "sdn port mapping information schema",
214 "type": "array",
215 "items": {
216 "type": "object",
217 "properties": {
218 "compute_node": nameshort_schema,
219 "ports": {
220 "type": "array",
221 "items": {
222 "type": "object",
223 "properties": {
224 "pci": pci_schema,
225 "switch_port": nameshort_schema,
226 "switch_mac": mac_schema
227 },
228 "required": ["pci"]
229 }
230 }
231 },
232 "required": ["compute_node", "ports"]
233 }
234}
235sdn_external_port_schema = {
236 "$schema": "http://json-schema.org/draft-04/schema#",
237 "title": "External port ingformation",
238 "type": "object",
239 "properties": {
240 "port": {"type": "string", "minLength": 1, "maxLength": 60},
241 "vlan": vlan_schema,
242 "mac": mac_schema
243 },
244 "required": ["port"]
245}
246
247
248nbi_new_input_schemas = {
tierno09c073e2018-04-26 13:36:48 +0200249 "vim_accounts": vim_account_new_schema,
tierno65acb4d2018-04-06 16:42:40 +0200250 "sdns": sdn_new_schema,
251 "ns_instantiate": ns_instantiate,
252 "ns_action": ns_action,
tiernof759d822018-06-11 18:54:54 +0200253 "ns_scale": ns_scale
tierno0f98af52018-03-19 10:28:22 +0100254}
255
256nbi_edit_input_schemas = {
tierno09c073e2018-04-26 13:36:48 +0200257 "vim_accounts": vim_account_edit_schema,
tierno0f98af52018-03-19 10:28:22 +0100258 "sdns": sdn_edit_schema
259}
260
261
262class ValidationError(Exception):
263 pass
264
265
266def validate_input(indata, item, new=True):
267 """
268 Validates input data agains json schema
269 :param indata: user input data. Should be a dictionary
tierno65acb4d2018-04-06 16:42:40 +0200270 :param item: can be users, projects, vims, sdns, ns_xxxxx
tierno0f98af52018-03-19 10:28:22 +0100271 :param new: True if the validation is for creating or False if it is for editing
272 :return: None if ok, raises ValidationError exception otherwise
273 """
274 try:
275 if new:
276 schema_to_use = nbi_new_input_schemas.get(item)
277 else:
278 schema_to_use = nbi_edit_input_schemas.get(item)
279 if schema_to_use:
280 js_v(indata, schema_to_use)
281 return None
282 except js_e.ValidationError as e:
283 if e.path:
tierno0da52252018-06-27 15:47:22 +0200284 error_pos = "at '" + ":".join(map(str, e.path)) + "'"
tierno0f98af52018-03-19 10:28:22 +0100285 else:
286 error_pos = ""
287 raise ValidationError("Format error {} '{}' ".format(error_pos, e))