1 # -*- coding: utf-8 -*-
3 from jsonschema
import validate
as js_v
, exceptions
as js_e
5 __author__
= "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
7 version_date
= "Mar 2018"
10 Validator of input data using JSON schemas for those items that not contains an OSM yang information model
14 patern_name
= "^[ -~]+$"
15 passwd_schema
= {"type": "string", "minLength": 1, "maxLength": 60}
16 nameshort_schema
= {"type": "string", "minLength": 1, "maxLength": 60, "pattern": "^[^,;()'\"]+$"}
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,
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}$"
24 id_schema
= {"type": "string", "pattern": "^[a-fA-F0-9]{8}(-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}$"}
25 time_schema
= {"type": "string", "pattern": "^[0-9]{4}-[0-1][0-9]-[0-3][0-9]T[0-2][0-9]([0-5]:){2}"}
26 pci_schema
= {"type": "string", "pattern": "^[0-9a-fA-F]{4}(:[0-9a-fA-F]{2}){2}\.[0-9a-fA-F]$"}
27 http_schema
= {"type": "string", "pattern": "^https?://[^'\"=]+$"}
28 bandwidth_schema
= {"type": "string", "pattern": "^[0-9]+ *([MG]bps)?$"}
29 memory_schema
= {"type": "string", "pattern": "^[0-9]+ *([MG]i?[Bb])?$"}
30 integer0_schema
= {"type": "integer", "minimum": 0}
31 integer1_schema
= {"type": "integer", "minimum": 1}
32 path_schema
= {"type": "string", "pattern": "^(\.){0,2}(/[^/\"':{}\(\)]+)+$"}
33 vlan_schema
= {"type": "integer", "minimum": 1, "maximum": 4095}
34 vlan1000_schema
= {"type": "integer", "minimum": 1000, "maximum": 4095}
35 mac_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}$"}
38 ip_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]?)$"}
40 ip_prefix_schema
= {"type": "string",
41 "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])$"}
43 port_schema
= {"type": "integer", "minimum": 1, "maximum": 65534}
44 object_schema
= {"type": "object"}
45 schema_version_2
= {"type": "integer", "minimum": 2, "maximum": 2}
46 # schema_version_string={"type":"string","enum": ["0.1", "2", "0.2", "3", "0.3"]}
47 log_level_schema
= {"type": "string", "enum": ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]}
48 checksum_schema
= {"type": "string", "pattern": "^[0-9a-fA-F]{32}$"}
49 size_schema
= {"type": "integer", "minimum": 1, "maximum": 100}
53 "title": "ns action instantiate input schema",
54 "$schema": "http://json-schema.org/draft-04/schema#",
57 "nsName": name_schema
,
58 "nsDescription": description_schema
,
60 "vimAccountId": id_schema
,
61 "ssh_keys": {"type": "string"},
68 "member-vnf-index": name_schema
,
69 "vimAccountId": id_schema
,
71 "required": ["member-vnf-index"]
80 "name": string_schema
,
81 "vim-network-name": {"OneOf": [string_schema
, object_schema
]},
82 "ip-profile": object_schema
,
88 "required": ["nsName", "nsdId", "vimAccountId"]
91 ns_action
= { # TODO for the moment it is only contemplated the vnfd primitive execution
92 "title": "ns action input schema",
93 "$schema": "http://json-schema.org/draft-04/schema#",
96 "member_vnf_index": name_schema
,
97 "vnf_member_index": name_schema
, # TODO for backward compatibility. To remove in future
98 "primitive": name_schema
,
99 "primitive_params": {"type": "object"},
101 "required": ["primitive", "primitive_params"], # TODO add member_vnf_index
102 "additionalProperties": False
104 ns_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#",
109 "scaleType": {"enum": ["SCALE_VNF"]},
113 "vnfInstanceId": name_schema
,
114 "scaleVnfType": {"enum": ["SCALE_OUT", 'SCALE_IN']},
118 "scaling-group-descriptor": name_schema
,
119 "member-vnf-index": name_schema
,
120 "scaling-policy": name_schema
,
122 "required": ["scaling-group-descriptor", "member-vnf-index"],
123 "additionalProperties": False
126 "required": ["scaleVnfType", "scaleByStepData"], # vnfInstanceId
127 "additionalProperties": False
129 "scaleTime": time_schema
,
131 "required": ["scaleType", "scaleVnfData"],
132 "additionalProperties": False
136 schema_version
= {"type": "string", "enum": ["1.0"]}
137 vim_account_edit_schema
= {
138 "title": "vim_account edit input schema",
139 "$schema": "http://json-schema.org/draft-04/schema#",
143 "description": description_schema
,
144 "type": nameshort_schema
, # currently "openvim" or "openstack", can be enlarged with plugins
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"}
155 "additionalProperties": False
157 schema_type
= {"type": "string"}
159 vim_account_new_schema
= {
160 "title": "vim_account creation input schema",
161 "$schema": "http://json-schema.org/draft-04/schema#",
164 "schema_version": schema_version
,
165 "schema_type": schema_type
,
167 "description": description_schema
,
169 "datacenter": name_schema
,
170 "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"}
179 "required": ["name", "vim_url", "vim_type", "vim_user", "vim_password", "vim_tenant_name"],
180 "additionalProperties": False
186 "description": description_schema
,
187 "dpid": {"type": "string", "pattern": "^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){7}$"},
190 "type": {"type": "string", "enum": ["opendaylight", "floodlight", "onos"]},
191 "version": {"type": "string", "minLength": 1, "maxLength": 12},
192 "user": nameshort_schema
,
193 "password": passwd_schema
196 "title": "sdn controller information schema",
197 "$schema": "http://json-schema.org/draft-04/schema#",
199 "properties": sdn_properties
,
200 "required": ["name", "port", 'ip', 'dpid', 'type'],
201 "additionalProperties": False
204 "title": "sdn controller update information schema",
205 "$schema": "http://json-schema.org/draft-04/schema#",
207 "properties": sdn_properties
,
208 # "required": ["name", "port", 'ip', 'dpid', 'type'],
209 "additionalProperties": False
211 sdn_port_mapping_schema
= {
212 "$schema": "http://json-schema.org/draft-04/schema#",
213 "title": "sdn port mapping information schema",
218 "compute_node": nameshort_schema
,
225 "switch_port": nameshort_schema
,
226 "switch_mac": mac_schema
232 "required": ["compute_node", "ports"]
235 sdn_external_port_schema
= {
236 "$schema": "http://json-schema.org/draft-04/schema#",
237 "title": "External port ingformation",
240 "port": {"type": "string", "minLength": 1, "maxLength": 60},
248 nbi_new_input_schemas
= {
249 "vim_accounts": vim_account_new_schema
,
250 "sdns": sdn_new_schema
,
251 "ns_instantiate": ns_instantiate
,
252 "ns_action": ns_action
,
256 nbi_edit_input_schemas
= {
257 "vim_accounts": vim_account_edit_schema
,
258 "sdns": sdn_edit_schema
262 class ValidationError(Exception):
266 def validate_input(indata
, item
, new
=True):
268 Validates input data agains json schema
269 :param indata: user input data. Should be a dictionary
270 :param item: can be users, projects, vims, sdns, ns_xxxxx
271 :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
276 schema_to_use
= nbi_new_input_schemas
.get(item
)
278 schema_to_use
= nbi_edit_input_schemas
.get(item
)
280 js_v(indata
, schema_to_use
)
282 except js_e
.ValidationError
as e
:
284 error_pos
= "at '" + ":".join(map(str, e
.path
)) + "'"
287 raise ValidationError("Format error {} '{}' ".format(error_pos
, e
))