blob: bb966e23b2b79abafa9f7ae0231ea0cc6a0044d8 [file] [log] [blame]
tierno0f98af52018-03-19 10:28:22 +01001# -*- coding: utf-8 -*-
2
tiernod125caf2018-11-22 16:05:54 +00003# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12# implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
tierno0f98af52018-03-19 10:28:22 +010016from jsonschema import validate as js_v, exceptions as js_e
tierno36ec8602018-11-02 17:27:11 +010017from http import HTTPStatus
garciadeblasdaf8cc52018-11-30 14:17:20 +010018from copy import deepcopy
tierno0f98af52018-03-19 10:28:22 +010019
20__author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
21__version__ = "0.1"
22version_date = "Mar 2018"
23
24"""
25Validator of input data using JSON schemas for those items that not contains an OSM yang information model
26"""
27
28# Basis schemas
29patern_name = "^[ -~]+$"
tierno87006042018-10-24 12:50:20 +020030nameshort_schema = {"type": "string", "minLength": 1, "maxLength": 60, "pattern": "^[^,;()\\.\\$'\"]+$"}
tierno0f98af52018-03-19 10:28:22 +010031passwd_schema = {"type": "string", "minLength": 1, "maxLength": 60}
tierno0f98af52018-03-19 10:28:22 +010032name_schema = {"type": "string", "minLength": 1, "maxLength": 255, "pattern": "^[^,;()'\"]+$"}
tierno0da52252018-06-27 15:47:22 +020033string_schema = {"type": "string", "minLength": 1, "maxLength": 255}
tierno0f98af52018-03-19 10:28:22 +010034xml_text_schema = {"type": "string", "minLength": 1, "maxLength": 1000, "pattern": "^[^']+$"}
35description_schema = {"type": ["string", "null"], "maxLength": 255, "pattern": "^[^'\"]+$"}
tierno441dbbf2018-07-10 12:52:48 +020036id_schema_fake = {"type": "string", "minLength": 2, "maxLength": 36}
37bool_schema = {"type": "boolean"}
38null_schema = {"type": "null"}
tierno2236d202018-05-16 19:05:16 +020039# "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 +010040id_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 +020041time_schema = {"type": "string", "pattern": "^[0-9]{4}-[0-1][0-9]-[0-3][0-9]T[0-2][0-9]([0-5]:){2}"}
tierno87006042018-10-24 12:50:20 +020042pci_schema = {"type": "string", "pattern": "^[0-9a-fA-F]{4}(:[0-9a-fA-F]{2}){2}\\.[0-9a-fA-F]$"}
tierno42fce592018-11-02 09:23:43 +010043# allows [] for wildcards. For that reason huge length limit is set
44pci_extended_schema = {"type": "string", "pattern": "^[0-9a-fA-F.:-\\[\\]]{12,40}$"}
tierno0f98af52018-03-19 10:28:22 +010045http_schema = {"type": "string", "pattern": "^https?://[^'\"=]+$"}
46bandwidth_schema = {"type": "string", "pattern": "^[0-9]+ *([MG]bps)?$"}
47memory_schema = {"type": "string", "pattern": "^[0-9]+ *([MG]i?[Bb])?$"}
48integer0_schema = {"type": "integer", "minimum": 0}
49integer1_schema = {"type": "integer", "minimum": 1}
tierno87006042018-10-24 12:50:20 +020050path_schema = {"type": "string", "pattern": "^(\\.){0,2}(/[^/\"':{}\\(\\)]+)+$"}
tierno0f98af52018-03-19 10:28:22 +010051vlan_schema = {"type": "integer", "minimum": 1, "maximum": 4095}
52vlan1000_schema = {"type": "integer", "minimum": 1000, "maximum": 4095}
53mac_schema = {"type": "string",
54 "pattern": "^[0-9a-fA-F][02468aceACE](:[0-9a-fA-F]{2}){5}$"} # must be unicast: LSB bit of MSB byte ==0
tiernocb83c942018-09-24 17:28:13 +020055dpid_Schema = {"type": "string", "pattern": "^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){7}$"}
tierno0f98af52018-03-19 10:28:22 +010056# mac_schema={"type":"string", "pattern":"^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$"}
57ip_schema = {"type": "string",
tierno87006042018-10-24 12:50:20 +020058 "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]?)$"}
tierno0f98af52018-03-19 10:28:22 +010059ip_prefix_schema = {"type": "string",
tierno87006042018-10-24 12:50:20 +020060 "pattern": "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}"
tierno2236d202018-05-16 19:05:16 +020061 "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/(30|[12]?[0-9])$"}
tierno0f98af52018-03-19 10:28:22 +010062port_schema = {"type": "integer", "minimum": 1, "maximum": 65534}
63object_schema = {"type": "object"}
64schema_version_2 = {"type": "integer", "minimum": 2, "maximum": 2}
65# schema_version_string={"type":"string","enum": ["0.1", "2", "0.2", "3", "0.3"]}
66log_level_schema = {"type": "string", "enum": ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]}
67checksum_schema = {"type": "string", "pattern": "^[0-9a-fA-F]{32}$"}
68size_schema = {"type": "integer", "minimum": 1, "maximum": 100}
tiernocd54a4a2018-09-12 16:40:35 +020069array_edition_schema = {
70 "type": "object",
71 "patternProperties": {
tierno87006042018-10-24 12:50:20 +020072 "^\\$": "Any"
tiernocd54a4a2018-09-12 16:40:35 +020073 },
74 "additionalProperties": False,
75 "minProperties": 1,
76}
tiernocb83c942018-09-24 17:28:13 +020077nameshort_list_schema = {
78 "type": "array",
79 "minItems": 1,
80 "items": nameshort_schema,
81}
tiernocd54a4a2018-09-12 16:40:35 +020082
tierno0f98af52018-03-19 10:28:22 +010083
tierno441dbbf2018-07-10 12:52:48 +020084ns_instantiate_vdu = {
85 "title": "ns action instantiate input schema for vdu",
86 "$schema": "http://json-schema.org/draft-04/schema#",
87 "type": "object",
88 "properties": {
89 "id": name_schema,
90 "volume": {
91 "type": "array",
92 "minItems": 1,
93 "items": {
94 "type": "object",
95 "properties": {
96 "name": name_schema,
97 "vim-volume-id": name_schema,
98 },
99 "required": ["name", "vim-volume-id"],
100 "additionalProperties": False
101 }
102 },
103 "interface": {
104 "type": "array",
105 "minItems": 1,
106 "items": {
107 "type": "object",
108 "properties": {
109 "name": name_schema,
110 "ip-address": ip_schema,
111 "mac-address": mac_schema,
112 "floating-ip-required": bool_schema,
113 },
114 "required": ["name"],
115 "additionalProperties": False
116 }
117 }
118 },
119 "required": ["id"],
120 "additionalProperties": False
121}
122
123ip_profile_dns_schema = {
124 "type": "array",
125 "minItems": 1,
126 "items": {
127 "type": "object",
128 "properties": {
129 "address": ip_schema,
130 },
131 "required": ["address"],
132 "additionalProperties": False
133 }
134}
135
136ip_profile_dhcp_schema = {
137 "type": "object",
138 "properties": {
139 "enabled": {"type": "boolean"},
140 "count": integer1_schema,
141 "start-address": ip_schema
142 },
143 "additionalProperties": False,
144}
145
146ip_profile_schema = {
147 "title": "ip profile validation schame",
148 "$schema": "http://json-schema.org/draft-04/schema#",
149 "type": "object",
150 "properties": {
151 "ip-version": {"enum": ["ipv4", "ipv6"]},
152 "subnet-address": ip_prefix_schema,
153 "gateway-address": ip_schema,
154 "dns-server": ip_profile_dns_schema,
155 "dhcp-params": ip_profile_dhcp_schema,
156 }
157}
158
159ip_profile_update_schema = {
160 "title": "ip profile validation schame",
161 "$schema": "http://json-schema.org/draft-04/schema#",
162 "type": "object",
163 "properties": {
164 "ip-version": {"enum": ["ipv4", "ipv6"]},
165 "subnet-address": {"oneOf": [null_schema, ip_prefix_schema]},
166 "gateway-address": {"oneOf": [null_schema, ip_schema]},
167 "dns-server": {"oneOf": [null_schema, ip_profile_dns_schema]},
168
169 "dhcp-params": {"oneOf": [null_schema, ip_profile_dhcp_schema]},
170 },
171 "additionalProperties": False
172}
173
174ns_instantiate_internal_vld = {
175 "title": "ns action instantiate input schema for vdu",
176 "$schema": "http://json-schema.org/draft-04/schema#",
177 "type": "object",
178 "properties": {
179 "name": name_schema,
180 "vim-network-name": name_schema,
181 "ip-profile": ip_profile_update_schema,
182 "internal-connection-point": {
183 "type": "array",
184 "minItems": 1,
185 "items": {
186 "type": "object",
187 "properties": {
188 "id-ref": name_schema,
189 "ip-address": ip_schema,
tiernob7c6f2a2018-09-03 14:32:10 +0200190 # "mac-address": mac_schema,
tierno441dbbf2018-07-10 12:52:48 +0200191 },
tiernob7c6f2a2018-09-03 14:32:10 +0200192 "required": ["id-ref"],
193 "minProperties": 2,
tierno441dbbf2018-07-10 12:52:48 +0200194 "additionalProperties": False
195 },
196 }
197 },
198 "required": ["name"],
199 "minProperties": 2,
200 "additionalProperties": False
201}
tierno65acb4d2018-04-06 16:42:40 +0200202
203ns_instantiate = {
204 "title": "ns action instantiate input schema",
205 "$schema": "http://json-schema.org/draft-04/schema#",
206 "type": "object",
tierno0da52252018-06-27 15:47:22 +0200207 "properties": {
tiernob24258a2018-10-04 18:39:49 +0200208 "lcmOperationType": string_schema,
209 "nsInstanceId": id_schema,
Felipe Vicens07f31722018-10-29 15:16:44 +0100210 "netsliceInstanceId": id_schema,
tierno0da52252018-06-27 15:47:22 +0200211 "nsName": name_schema,
tierno441dbbf2018-07-10 12:52:48 +0200212 "nsDescription": {"oneOf": [description_schema, {"type": "null"}]},
tierno0da52252018-06-27 15:47:22 +0200213 "nsdId": id_schema,
214 "vimAccountId": id_schema,
tiernofc5089c2018-10-31 09:28:11 +0100215 "ssh_keys": {"type": "array", "items": {"type": "string"}},
tierno441dbbf2018-07-10 12:52:48 +0200216 "nsr_id": id_schema,
tiernocc103432018-10-19 14:10:35 +0200217 "vduImage": name_schema,
tierno0da52252018-06-27 15:47:22 +0200218 "vnf": {
219 "type": "array",
220 "minItems": 1,
221 "items": {
222 "type": "object",
223 "properties": {
224 "member-vnf-index": name_schema,
225 "vimAccountId": id_schema,
tierno441dbbf2018-07-10 12:52:48 +0200226 "vdu": {
227 "type": "array",
228 "minItems": 1,
229 "items": ns_instantiate_vdu,
230 },
231 "internal-vld": {
232 "type": "array",
233 "minItems": 1,
234 "items": ns_instantiate_internal_vld
235 }
tierno0da52252018-06-27 15:47:22 +0200236 },
tierno441dbbf2018-07-10 12:52:48 +0200237 "required": ["member-vnf-index"],
238 "minProperties": 2,
239 "additionalProperties": False
tierno0da52252018-06-27 15:47:22 +0200240 }
241 },
242 "vld": {
243 "type": "array",
244 "minItems": 1,
245 "items": {
246 "type": "object",
247 "properties": {
248 "name": string_schema,
249 "vim-network-name": {"OneOf": [string_schema, object_schema]},
250 "ip-profile": object_schema,
tiernob7c6f2a2018-09-03 14:32:10 +0200251 "vnfd-connection-point-ref": {
252 "type": "array",
253 "minItems": 1,
254 "items": {
255 "type": "object",
256 "properties": {
257 "member-vnf-index-ref": name_schema,
258 "vnfd-connection-point-ref": name_schema,
259 "ip-address": ip_schema,
260 # "mac-address": mac_schema,
261 },
262 "required": ["member-vnf-index-ref", "vnfd-connection-point-ref"],
263 "minProperties": 3,
264 "additionalProperties": False
265 },
266 }
tierno0da52252018-06-27 15:47:22 +0200267 },
tierno441dbbf2018-07-10 12:52:48 +0200268 "required": ["name"],
269 "additionalProperties": False
tierno0da52252018-06-27 15:47:22 +0200270 }
271 },
272 },
tierno441dbbf2018-07-10 12:52:48 +0200273 "required": ["nsName", "nsdId", "vimAccountId"],
274 "additionalProperties": False
tierno65acb4d2018-04-06 16:42:40 +0200275}
tierno0da52252018-06-27 15:47:22 +0200276
tierno65acb4d2018-04-06 16:42:40 +0200277ns_action = { # TODO for the moment it is only contemplated the vnfd primitive execution
tiernof759d822018-06-11 18:54:54 +0200278 "title": "ns action input schema",
tierno65acb4d2018-04-06 16:42:40 +0200279 "$schema": "http://json-schema.org/draft-04/schema#",
280 "type": "object",
281 "properties": {
tiernob24258a2018-10-04 18:39:49 +0200282 "lcmOperationType": string_schema,
283 "nsInstanceId": id_schema,
tiernof5298be2018-05-16 14:43:57 +0200284 "member_vnf_index": name_schema,
285 "vnf_member_index": name_schema, # TODO for backward compatibility. To remove in future
tierno7ce1db92018-07-25 12:50:52 +0200286 "vdu_id": name_schema,
tierno65acb4d2018-04-06 16:42:40 +0200287 "primitive": name_schema,
288 "primitive_params": {"type": "object"},
289 },
tiernof5298be2018-05-16 14:43:57 +0200290 "required": ["primitive", "primitive_params"], # TODO add member_vnf_index
tierno65acb4d2018-04-06 16:42:40 +0200291 "additionalProperties": False
292}
tiernof759d822018-06-11 18:54:54 +0200293ns_scale = { # TODO for the moment it is only VDU-scaling
294 "title": "ns scale input schema",
295 "$schema": "http://json-schema.org/draft-04/schema#",
296 "type": "object",
297 "properties": {
tiernob24258a2018-10-04 18:39:49 +0200298 "lcmOperationType": string_schema,
299 "nsInstanceId": id_schema,
tiernof759d822018-06-11 18:54:54 +0200300 "scaleType": {"enum": ["SCALE_VNF"]},
301 "scaleVnfData": {
302 "type": "object",
303 "properties": {
304 "vnfInstanceId": name_schema,
305 "scaleVnfType": {"enum": ["SCALE_OUT", 'SCALE_IN']},
306 "scaleByStepData": {
307 "type": "object",
308 "properties": {
309 "scaling-group-descriptor": name_schema,
310 "member-vnf-index": name_schema,
311 "scaling-policy": name_schema,
312 },
313 "required": ["scaling-group-descriptor", "member-vnf-index"],
314 "additionalProperties": False
315 },
316 },
317 "required": ["scaleVnfType", "scaleByStepData"], # vnfInstanceId
318 "additionalProperties": False
319 },
320 "scaleTime": time_schema,
321 },
322 "required": ["scaleType", "scaleVnfData"],
323 "additionalProperties": False
324}
tierno65acb4d2018-04-06 16:42:40 +0200325
326
tierno0f98af52018-03-19 10:28:22 +0100327schema_version = {"type": "string", "enum": ["1.0"]}
tierno55ba2e62018-12-11 17:22:22 +0000328schema_type = {"type": "string"}
tierno09c073e2018-04-26 13:36:48 +0200329vim_account_edit_schema = {
330 "title": "vim_account edit input schema",
331 "$schema": "http://json-schema.org/draft-04/schema#",
332 "type": "object",
333 "properties": {
334 "name": name_schema,
335 "description": description_schema,
tierno55ba2e62018-12-11 17:22:22 +0000336 "type": nameshort_schema,
tierno09c073e2018-04-26 13:36:48 +0200337 "vim": name_schema,
338 "datacenter": name_schema,
339 "vim_url": description_schema,
340 "vim_url_admin": description_schema,
341 "vim_tenant": name_schema,
342 "vim_tenant_name": name_schema,
343 "vim_username": nameshort_schema,
tiernocd54a4a2018-09-12 16:40:35 +0200344 "vim_password": passwd_schema,
tierno09c073e2018-04-26 13:36:48 +0200345 "config": {"type": "object"}
346 },
347 "additionalProperties": False
348}
tierno0f98af52018-03-19 10:28:22 +0100349
tierno09c073e2018-04-26 13:36:48 +0200350vim_account_new_schema = {
351 "title": "vim_account creation input schema",
tierno0f98af52018-03-19 10:28:22 +0100352 "$schema": "http://json-schema.org/draft-04/schema#",
353 "type": "object",
354 "properties": {
355 "schema_version": schema_version,
356 "schema_type": schema_type,
357 "name": name_schema,
358 "description": description_schema,
tierno09c073e2018-04-26 13:36:48 +0200359 "vim": name_schema,
360 "datacenter": name_schema,
tierno0f98af52018-03-19 10:28:22 +0100361 "vim_type": {"enum": ["openstack", "openvim", "vmware", "opennebula", "aws"]},
362 "vim_url": description_schema,
363 # "vim_url_admin": description_schema,
364 # "vim_tenant": name_schema,
365 "vim_tenant_name": name_schema,
366 "vim_user": nameshort_schema,
tiernocd54a4a2018-09-12 16:40:35 +0200367 "vim_password": passwd_schema,
tierno0f98af52018-03-19 10:28:22 +0100368 "config": {"type": "object"}
369 },
370 "required": ["name", "vim_url", "vim_type", "vim_user", "vim_password", "vim_tenant_name"],
371 "additionalProperties": False
372}
tierno0f98af52018-03-19 10:28:22 +0100373
tierno55ba2e62018-12-11 17:22:22 +0000374wim_account_edit_schema = {
375 "title": "wim_account edit input schema",
376 "$schema": "http://json-schema.org/draft-04/schema#",
377 "type": "object",
378 "properties": {
379 "name": name_schema,
380 "description": description_schema,
381 "type": nameshort_schema,
382 "wim": name_schema,
383 "wim_url": description_schema,
384 "user": nameshort_schema,
385 "password": passwd_schema,
386 "config": {"type": "object"}
387 },
388 "additionalProperties": False
389}
390
391wim_account_new_schema = {
392 "title": "wim_account creation input schema",
393 "$schema": "http://json-schema.org/draft-04/schema#",
394 "type": "object",
395 "properties": {
396 "schema_version": schema_version,
397 "schema_type": schema_type,
398 "name": name_schema,
399 "description": description_schema,
400 "wim": name_schema,
401 "wim_type": {"enum": ["tapi", "onos", "odl", "dynpac"]},
402 "wim_url": description_schema,
403 "user": nameshort_schema,
404 "password": passwd_schema,
405 "config": {"type": "object"}
406 },
407 "required": ["name", "wim_url", "wim_type"],
408 "additionalProperties": False
409}
tierno0f98af52018-03-19 10:28:22 +0100410
411sdn_properties = {
412 "name": name_schema,
tiernocfb07c62018-05-10 18:30:51 +0200413 "description": description_schema,
tiernocb83c942018-09-24 17:28:13 +0200414 "dpid": dpid_Schema,
tierno0f98af52018-03-19 10:28:22 +0100415 "ip": ip_schema,
416 "port": port_schema,
417 "type": {"type": "string", "enum": ["opendaylight", "floodlight", "onos"]},
418 "version": {"type": "string", "minLength": 1, "maxLength": 12},
419 "user": nameshort_schema,
420 "password": passwd_schema
421}
422sdn_new_schema = {
423 "title": "sdn controller information schema",
424 "$schema": "http://json-schema.org/draft-04/schema#",
425 "type": "object",
426 "properties": sdn_properties,
427 "required": ["name", "port", 'ip', 'dpid', 'type'],
428 "additionalProperties": False
429}
430sdn_edit_schema = {
431 "title": "sdn controller update information schema",
432 "$schema": "http://json-schema.org/draft-04/schema#",
433 "type": "object",
434 "properties": sdn_properties,
tiernocfb07c62018-05-10 18:30:51 +0200435 # "required": ["name", "port", 'ip', 'dpid', 'type'],
tierno0f98af52018-03-19 10:28:22 +0100436 "additionalProperties": False
437}
438sdn_port_mapping_schema = {
439 "$schema": "http://json-schema.org/draft-04/schema#",
440 "title": "sdn port mapping information schema",
441 "type": "array",
442 "items": {
443 "type": "object",
444 "properties": {
445 "compute_node": nameshort_schema,
446 "ports": {
447 "type": "array",
448 "items": {
449 "type": "object",
450 "properties": {
tierno42fce592018-11-02 09:23:43 +0100451 "pci": pci_extended_schema,
tierno0f98af52018-03-19 10:28:22 +0100452 "switch_port": nameshort_schema,
453 "switch_mac": mac_schema
454 },
455 "required": ["pci"]
456 }
457 }
458 },
459 "required": ["compute_node", "ports"]
460 }
461}
462sdn_external_port_schema = {
463 "$schema": "http://json-schema.org/draft-04/schema#",
tiernocd54a4a2018-09-12 16:40:35 +0200464 "title": "External port information",
tierno0f98af52018-03-19 10:28:22 +0100465 "type": "object",
466 "properties": {
467 "port": {"type": "string", "minLength": 1, "maxLength": 60},
468 "vlan": vlan_schema,
469 "mac": mac_schema
470 },
471 "required": ["port"]
472}
473
tiernocb83c942018-09-24 17:28:13 +0200474# PDUs
475pdu_interface = {
476 "type": "object",
477 "properties": {
478 "name": nameshort_schema,
479 "mgmt": bool_schema,
480 "type": {"enum": ["overlay", 'underlay']},
tierno36ec8602018-11-02 17:27:11 +0100481 "ip-address": ip_schema,
tiernocb83c942018-09-24 17:28:13 +0200482 # TODO, add user, password, ssh-key
tierno36ec8602018-11-02 17:27:11 +0100483 "mac-address": mac_schema,
484 "vim-network-name": nameshort_schema, # interface is connected to one vim network, or switch port
485 # TODO "vim-network-id": nameshort_schema,
486 # # provide this in case SDN assist must deal with this interface
487 # "switch-dpid": dpid_Schema,
488 # "switch-port": nameshort_schema,
489 # "switch-mac": nameshort_schema,
490 # "switch-vlan": vlan_schema,
tiernocb83c942018-09-24 17:28:13 +0200491 },
tierno36ec8602018-11-02 17:27:11 +0100492 "required": ["name", "mgmt", "ip-address"],
tiernocb83c942018-09-24 17:28:13 +0200493 "additionalProperties": False
tiernocd54a4a2018-09-12 16:40:35 +0200494}
tiernocb83c942018-09-24 17:28:13 +0200495pdu_new_schema = {
496 "title": "pdu creation input schema",
497 "$schema": "http://json-schema.org/draft-04/schema#",
498 "type": "object",
499 "properties": {
500 "name": nameshort_schema,
501 "type": nameshort_schema,
502 "description": description_schema,
503 "shared": bool_schema,
504 "vims": nameshort_list_schema,
505 "vim_accounts": nameshort_list_schema,
506 "interfaces": {
507 "type": "array",
tierno36ec8602018-11-02 17:27:11 +0100508 "items": pdu_interface,
tiernocb83c942018-09-24 17:28:13 +0200509 "minItems": 1
510 }
511 },
512 "required": ["name", "type", "interfaces"],
513 "additionalProperties": False
514}
515
516pdu_edit_schema = {
517 "title": "pdu edit input schema",
518 "$schema": "http://json-schema.org/draft-04/schema#",
519 "type": "object",
520 "properties": {
521 "name": nameshort_schema,
522 "type": nameshort_schema,
523 "description": description_schema,
524 "shared": bool_schema,
garciadeblas84bdd552018-11-30 22:59:45 +0100525 "vims": {"oneOf": [array_edition_schema, nameshort_list_schema]},
526 "vim_accounts": {"oneOf": [array_edition_schema, nameshort_list_schema]},
527 "interfaces": {"oneOf": [
tiernocb83c942018-09-24 17:28:13 +0200528 array_edition_schema,
529 {
530 "type": "array",
tierno36ec8602018-11-02 17:27:11 +0100531 "items": pdu_interface,
tiernocb83c942018-09-24 17:28:13 +0200532 "minItems": 1
533 }
534 ]}
535 },
536 "additionalProperties": False,
537 "minProperties": 1
538}
539
540# USERS
tiernocd54a4a2018-09-12 16:40:35 +0200541user_new_schema = {
542 "$schema": "http://json-schema.org/draft-04/schema#",
543 "title": "New user schema",
544 "type": "object",
545 "properties": {
546 "username": nameshort_schema,
547 "password": passwd_schema,
tiernocb83c942018-09-24 17:28:13 +0200548 "projects": nameshort_list_schema,
tiernocd54a4a2018-09-12 16:40:35 +0200549 },
550 "required": ["username", "password", "projects"],
551 "additionalProperties": False
552}
553user_edit_schema = {
554 "$schema": "http://json-schema.org/draft-04/schema#",
555 "title": "User edit schema for administrators",
556 "type": "object",
557 "properties": {
558 "password": passwd_schema,
559 "projects": {
garciadeblas84bdd552018-11-30 22:59:45 +0100560 "oneOf": [
tiernocb83c942018-09-24 17:28:13 +0200561 nameshort_list_schema,
tiernocd54a4a2018-09-12 16:40:35 +0200562 array_edition_schema
563 ]
564 },
tiernocb83c942018-09-24 17:28:13 +0200565 },
566 "minProperties": 1,
567 "additionalProperties": False
tiernocd54a4a2018-09-12 16:40:35 +0200568}
569
570# PROJECTS
571project_new_schema = {
572 "$schema": "http://json-schema.org/draft-04/schema#",
573 "title": "New project schema for administrators",
574 "type": "object",
575 "properties": {
576 "name": nameshort_schema,
577 "admin": bool_schema,
578 },
579 "required": ["name"],
580 "additionalProperties": False
581}
582project_edit_schema = {
583 "$schema": "http://json-schema.org/draft-04/schema#",
584 "title": "Project edit schema for administrators",
585 "type": "object",
586 "properties": {
587 "admin": bool_schema,
588 },
589 "additionalProperties": False,
590 "minProperties": 1
591}
592
593# GLOBAL SCHEMAS
tierno0f98af52018-03-19 10:28:22 +0100594
595nbi_new_input_schemas = {
tiernocd54a4a2018-09-12 16:40:35 +0200596 "users": user_new_schema,
597 "projects": project_new_schema,
tierno09c073e2018-04-26 13:36:48 +0200598 "vim_accounts": vim_account_new_schema,
tierno65acb4d2018-04-06 16:42:40 +0200599 "sdns": sdn_new_schema,
600 "ns_instantiate": ns_instantiate,
601 "ns_action": ns_action,
tiernocb83c942018-09-24 17:28:13 +0200602 "ns_scale": ns_scale,
603 "pdus": pdu_new_schema,
tierno0f98af52018-03-19 10:28:22 +0100604}
605
606nbi_edit_input_schemas = {
tiernocd54a4a2018-09-12 16:40:35 +0200607 "users": user_edit_schema,
608 "projects": project_edit_schema,
tierno09c073e2018-04-26 13:36:48 +0200609 "vim_accounts": vim_account_edit_schema,
tiernocb83c942018-09-24 17:28:13 +0200610 "sdns": sdn_edit_schema,
611 "pdus": pdu_edit_schema,
tierno0f98af52018-03-19 10:28:22 +0100612}
613
Felipe Vicens07f31722018-10-29 15:16:44 +0100614# NETSLICE SCHEMAS
garciadeblasdaf8cc52018-11-30 14:17:20 +0100615nsi_slice_instantiate = deepcopy(ns_instantiate)
616nsi_slice_instantiate["title"] = "netslice subnet instantiation params input schema"
617nsi_slice_instantiate["properties"]["id"] = name_schema
Felipe Vicensc8bbaaa2018-12-01 04:42:40 +0100618del nsi_slice_instantiate["required"]
garciadeblasdaf8cc52018-11-30 14:17:20 +0100619
620nsi_vld_instantiate = {
621 "title": "netslice vld instantiation params input schema",
622 "$schema": "http://json-schema.org/draft-04/schema#",
623 "type": "object",
624 "properties": {
625 "name": string_schema,
626 "vim-network-name": {"OneOf": [string_schema, object_schema]},
627 "ip-profile": object_schema,
628 },
629 "required": ["name"],
630 "additionalProperties": False
631}
632
Felipe Vicens07f31722018-10-29 15:16:44 +0100633nsi_instantiate = {
634 "title": "netslice action instantiate input schema",
635 "$schema": "http://json-schema.org/draft-04/schema#",
636 "type": "object",
637 "properties": {
638 "lcmOperationType": string_schema,
639 "nsiInstanceId": id_schema,
640 "nsiName": name_schema,
641 "nsiDescription": {"oneOf": [description_schema, {"type": "null"}]},
tierno9e5eea32018-11-29 09:42:09 +0000642 "nstId": string_schema,
Felipe Vicens07f31722018-10-29 15:16:44 +0100643 "vimAccountId": id_schema,
644 "ssh_keys": {"type": "string"},
645 "nsi_id": id_schema,
garciadeblasdaf8cc52018-11-30 14:17:20 +0100646 "netslice-subnet": {
Felipe Vicens07f31722018-10-29 15:16:44 +0100647 "type": "array",
648 "minItems": 1,
garciadeblasdaf8cc52018-11-30 14:17:20 +0100649 "items": nsi_slice_instantiate
650 },
651 "netslice-vld": {
652 "type": "array",
653 "minItems": 1,
654 "items": nsi_vld_instantiate
Felipe Vicens07f31722018-10-29 15:16:44 +0100655 },
656 },
Felipe Vicensc8bbaaa2018-12-01 04:42:40 +0100657 "required": ["nsiName", "nstId", "vimAccountId"],
Felipe Vicens07f31722018-10-29 15:16:44 +0100658 "additionalProperties": False
659}
660
661nsi_action = {
662
663}
664
665nsi_terminate = {
666
667}
668
tierno0f98af52018-03-19 10:28:22 +0100669
670class ValidationError(Exception):
tierno36ec8602018-11-02 17:27:11 +0100671 def __init__(self, message, http_code=HTTPStatus.UNPROCESSABLE_ENTITY):
672 self.http_code = http_code
673 Exception.__init__(self, message)
tierno0f98af52018-03-19 10:28:22 +0100674
675
tiernob24258a2018-10-04 18:39:49 +0200676def validate_input(indata, schema_to_use):
tierno0f98af52018-03-19 10:28:22 +0100677 """
tiernocd54a4a2018-09-12 16:40:35 +0200678 Validates input data against json schema
tierno0f98af52018-03-19 10:28:22 +0100679 :param indata: user input data. Should be a dictionary
tiernob24258a2018-10-04 18:39:49 +0200680 :param schema_to_use: jsonschema to test
681 :return: None if ok, raises ValidationError exception on error
tierno0f98af52018-03-19 10:28:22 +0100682 """
683 try:
tierno0f98af52018-03-19 10:28:22 +0100684 if schema_to_use:
685 js_v(indata, schema_to_use)
686 return None
687 except js_e.ValidationError as e:
688 if e.path:
tierno0da52252018-06-27 15:47:22 +0200689 error_pos = "at '" + ":".join(map(str, e.path)) + "'"
tierno0f98af52018-03-19 10:28:22 +0100690 else:
691 error_pos = ""
tierno441dbbf2018-07-10 12:52:48 +0200692 raise ValidationError("Format error {} '{}' ".format(error_pos, e.message))
tierno36ec8602018-11-02 17:27:11 +0100693 except js_e.SchemaError:
694 raise ValidationError("Bad json schema {}".format(schema_to_use), http_code=HTTPStatus.INTERNAL_SERVER_ERROR)