blob: 3579c114b154615b0f901ae6d1892d0d5aa40421 [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,
gcalvino17d5b732018-12-17 16:26:21 +0100181 "vim-network-id": name_schema,
tierno441dbbf2018-07-10 12:52:48 +0200182 "ip-profile": ip_profile_update_schema,
183 "internal-connection-point": {
184 "type": "array",
185 "minItems": 1,
186 "items": {
187 "type": "object",
188 "properties": {
189 "id-ref": name_schema,
190 "ip-address": ip_schema,
tiernob7c6f2a2018-09-03 14:32:10 +0200191 # "mac-address": mac_schema,
tierno441dbbf2018-07-10 12:52:48 +0200192 },
tiernob7c6f2a2018-09-03 14:32:10 +0200193 "required": ["id-ref"],
194 "minProperties": 2,
tierno441dbbf2018-07-10 12:52:48 +0200195 "additionalProperties": False
196 },
197 }
198 },
199 "required": ["name"],
200 "minProperties": 2,
201 "additionalProperties": False
202}
tierno65acb4d2018-04-06 16:42:40 +0200203
204ns_instantiate = {
205 "title": "ns action instantiate input schema",
206 "$schema": "http://json-schema.org/draft-04/schema#",
207 "type": "object",
tierno0da52252018-06-27 15:47:22 +0200208 "properties": {
tiernob24258a2018-10-04 18:39:49 +0200209 "lcmOperationType": string_schema,
210 "nsInstanceId": id_schema,
Felipe Vicens07f31722018-10-29 15:16:44 +0100211 "netsliceInstanceId": id_schema,
tierno0da52252018-06-27 15:47:22 +0200212 "nsName": name_schema,
tierno441dbbf2018-07-10 12:52:48 +0200213 "nsDescription": {"oneOf": [description_schema, {"type": "null"}]},
tierno0da52252018-06-27 15:47:22 +0200214 "nsdId": id_schema,
215 "vimAccountId": id_schema,
tiernofc5089c2018-10-31 09:28:11 +0100216 "ssh_keys": {"type": "array", "items": {"type": "string"}},
tierno441dbbf2018-07-10 12:52:48 +0200217 "nsr_id": id_schema,
tiernocc103432018-10-19 14:10:35 +0200218 "vduImage": name_schema,
tierno0da52252018-06-27 15:47:22 +0200219 "vnf": {
220 "type": "array",
221 "minItems": 1,
222 "items": {
223 "type": "object",
224 "properties": {
225 "member-vnf-index": name_schema,
226 "vimAccountId": id_schema,
tierno441dbbf2018-07-10 12:52:48 +0200227 "vdu": {
228 "type": "array",
229 "minItems": 1,
230 "items": ns_instantiate_vdu,
231 },
232 "internal-vld": {
233 "type": "array",
234 "minItems": 1,
235 "items": ns_instantiate_internal_vld
236 }
tierno0da52252018-06-27 15:47:22 +0200237 },
tierno441dbbf2018-07-10 12:52:48 +0200238 "required": ["member-vnf-index"],
239 "minProperties": 2,
240 "additionalProperties": False
tierno0da52252018-06-27 15:47:22 +0200241 }
242 },
243 "vld": {
244 "type": "array",
245 "minItems": 1,
246 "items": {
247 "type": "object",
248 "properties": {
249 "name": string_schema,
250 "vim-network-name": {"OneOf": [string_schema, object_schema]},
gcalvino17d5b732018-12-17 16:26:21 +0100251 "vim-network-id": {"OneOf": [string_schema, object_schema]},
tierno0da52252018-06-27 15:47:22 +0200252 "ip-profile": object_schema,
tiernob7c6f2a2018-09-03 14:32:10 +0200253 "vnfd-connection-point-ref": {
254 "type": "array",
255 "minItems": 1,
256 "items": {
257 "type": "object",
258 "properties": {
259 "member-vnf-index-ref": name_schema,
260 "vnfd-connection-point-ref": name_schema,
261 "ip-address": ip_schema,
262 # "mac-address": mac_schema,
263 },
264 "required": ["member-vnf-index-ref", "vnfd-connection-point-ref"],
265 "minProperties": 3,
266 "additionalProperties": False
267 },
268 }
tierno0da52252018-06-27 15:47:22 +0200269 },
tierno441dbbf2018-07-10 12:52:48 +0200270 "required": ["name"],
271 "additionalProperties": False
tierno0da52252018-06-27 15:47:22 +0200272 }
273 },
274 },
tierno441dbbf2018-07-10 12:52:48 +0200275 "required": ["nsName", "nsdId", "vimAccountId"],
276 "additionalProperties": False
tierno65acb4d2018-04-06 16:42:40 +0200277}
tierno0da52252018-06-27 15:47:22 +0200278
tierno65acb4d2018-04-06 16:42:40 +0200279ns_action = { # TODO for the moment it is only contemplated the vnfd primitive execution
tiernof759d822018-06-11 18:54:54 +0200280 "title": "ns action input schema",
tierno65acb4d2018-04-06 16:42:40 +0200281 "$schema": "http://json-schema.org/draft-04/schema#",
282 "type": "object",
283 "properties": {
tiernob24258a2018-10-04 18:39:49 +0200284 "lcmOperationType": string_schema,
285 "nsInstanceId": id_schema,
tiernof5298be2018-05-16 14:43:57 +0200286 "member_vnf_index": name_schema,
287 "vnf_member_index": name_schema, # TODO for backward compatibility. To remove in future
tierno7ce1db92018-07-25 12:50:52 +0200288 "vdu_id": name_schema,
tierno65acb4d2018-04-06 16:42:40 +0200289 "primitive": name_schema,
290 "primitive_params": {"type": "object"},
291 },
tiernof5298be2018-05-16 14:43:57 +0200292 "required": ["primitive", "primitive_params"], # TODO add member_vnf_index
tierno65acb4d2018-04-06 16:42:40 +0200293 "additionalProperties": False
294}
tiernof759d822018-06-11 18:54:54 +0200295ns_scale = { # TODO for the moment it is only VDU-scaling
296 "title": "ns scale input schema",
297 "$schema": "http://json-schema.org/draft-04/schema#",
298 "type": "object",
299 "properties": {
tiernob24258a2018-10-04 18:39:49 +0200300 "lcmOperationType": string_schema,
301 "nsInstanceId": id_schema,
tiernof759d822018-06-11 18:54:54 +0200302 "scaleType": {"enum": ["SCALE_VNF"]},
303 "scaleVnfData": {
304 "type": "object",
305 "properties": {
306 "vnfInstanceId": name_schema,
307 "scaleVnfType": {"enum": ["SCALE_OUT", 'SCALE_IN']},
308 "scaleByStepData": {
309 "type": "object",
310 "properties": {
311 "scaling-group-descriptor": name_schema,
312 "member-vnf-index": name_schema,
313 "scaling-policy": name_schema,
314 },
315 "required": ["scaling-group-descriptor", "member-vnf-index"],
316 "additionalProperties": False
317 },
318 },
319 "required": ["scaleVnfType", "scaleByStepData"], # vnfInstanceId
320 "additionalProperties": False
321 },
322 "scaleTime": time_schema,
323 },
324 "required": ["scaleType", "scaleVnfData"],
325 "additionalProperties": False
326}
tierno65acb4d2018-04-06 16:42:40 +0200327
328
tierno0f98af52018-03-19 10:28:22 +0100329schema_version = {"type": "string", "enum": ["1.0"]}
tierno55ba2e62018-12-11 17:22:22 +0000330schema_type = {"type": "string"}
tierno09c073e2018-04-26 13:36:48 +0200331vim_account_edit_schema = {
332 "title": "vim_account edit input schema",
333 "$schema": "http://json-schema.org/draft-04/schema#",
334 "type": "object",
335 "properties": {
336 "name": name_schema,
337 "description": description_schema,
tierno55ba2e62018-12-11 17:22:22 +0000338 "type": nameshort_schema,
tierno09c073e2018-04-26 13:36:48 +0200339 "vim": name_schema,
340 "datacenter": name_schema,
341 "vim_url": description_schema,
342 "vim_url_admin": description_schema,
343 "vim_tenant": name_schema,
344 "vim_tenant_name": name_schema,
345 "vim_username": nameshort_schema,
tiernocd54a4a2018-09-12 16:40:35 +0200346 "vim_password": passwd_schema,
tierno09c073e2018-04-26 13:36:48 +0200347 "config": {"type": "object"}
348 },
349 "additionalProperties": False
350}
tierno0f98af52018-03-19 10:28:22 +0100351
tierno09c073e2018-04-26 13:36:48 +0200352vim_account_new_schema = {
353 "title": "vim_account creation input schema",
tierno0f98af52018-03-19 10:28:22 +0100354 "$schema": "http://json-schema.org/draft-04/schema#",
355 "type": "object",
356 "properties": {
357 "schema_version": schema_version,
358 "schema_type": schema_type,
359 "name": name_schema,
360 "description": description_schema,
tierno09c073e2018-04-26 13:36:48 +0200361 "vim": name_schema,
362 "datacenter": name_schema,
tierno0f98af52018-03-19 10:28:22 +0100363 "vim_type": {"enum": ["openstack", "openvim", "vmware", "opennebula", "aws"]},
364 "vim_url": description_schema,
365 # "vim_url_admin": description_schema,
366 # "vim_tenant": name_schema,
367 "vim_tenant_name": name_schema,
368 "vim_user": nameshort_schema,
tiernocd54a4a2018-09-12 16:40:35 +0200369 "vim_password": passwd_schema,
tierno0f98af52018-03-19 10:28:22 +0100370 "config": {"type": "object"}
371 },
372 "required": ["name", "vim_url", "vim_type", "vim_user", "vim_password", "vim_tenant_name"],
373 "additionalProperties": False
374}
tierno0f98af52018-03-19 10:28:22 +0100375
tierno55ba2e62018-12-11 17:22:22 +0000376wim_account_edit_schema = {
377 "title": "wim_account edit input schema",
378 "$schema": "http://json-schema.org/draft-04/schema#",
379 "type": "object",
380 "properties": {
381 "name": name_schema,
382 "description": description_schema,
383 "type": nameshort_schema,
384 "wim": name_schema,
385 "wim_url": description_schema,
386 "user": nameshort_schema,
387 "password": passwd_schema,
388 "config": {"type": "object"}
389 },
390 "additionalProperties": False
391}
392
393wim_account_new_schema = {
394 "title": "wim_account creation input schema",
395 "$schema": "http://json-schema.org/draft-04/schema#",
396 "type": "object",
397 "properties": {
398 "schema_version": schema_version,
399 "schema_type": schema_type,
400 "name": name_schema,
401 "description": description_schema,
402 "wim": name_schema,
403 "wim_type": {"enum": ["tapi", "onos", "odl", "dynpac"]},
404 "wim_url": description_schema,
405 "user": nameshort_schema,
406 "password": passwd_schema,
407 "config": {"type": "object"}
408 },
409 "required": ["name", "wim_url", "wim_type"],
410 "additionalProperties": False
411}
tierno0f98af52018-03-19 10:28:22 +0100412
413sdn_properties = {
414 "name": name_schema,
tiernocfb07c62018-05-10 18:30:51 +0200415 "description": description_schema,
tiernocb83c942018-09-24 17:28:13 +0200416 "dpid": dpid_Schema,
tierno0f98af52018-03-19 10:28:22 +0100417 "ip": ip_schema,
418 "port": port_schema,
419 "type": {"type": "string", "enum": ["opendaylight", "floodlight", "onos"]},
420 "version": {"type": "string", "minLength": 1, "maxLength": 12},
421 "user": nameshort_schema,
422 "password": passwd_schema
423}
424sdn_new_schema = {
425 "title": "sdn controller information schema",
426 "$schema": "http://json-schema.org/draft-04/schema#",
427 "type": "object",
428 "properties": sdn_properties,
429 "required": ["name", "port", 'ip', 'dpid', 'type'],
430 "additionalProperties": False
431}
432sdn_edit_schema = {
433 "title": "sdn controller update information schema",
434 "$schema": "http://json-schema.org/draft-04/schema#",
435 "type": "object",
436 "properties": sdn_properties,
tiernocfb07c62018-05-10 18:30:51 +0200437 # "required": ["name", "port", 'ip', 'dpid', 'type'],
tierno0f98af52018-03-19 10:28:22 +0100438 "additionalProperties": False
439}
440sdn_port_mapping_schema = {
441 "$schema": "http://json-schema.org/draft-04/schema#",
442 "title": "sdn port mapping information schema",
443 "type": "array",
444 "items": {
445 "type": "object",
446 "properties": {
447 "compute_node": nameshort_schema,
448 "ports": {
449 "type": "array",
450 "items": {
451 "type": "object",
452 "properties": {
tierno42fce592018-11-02 09:23:43 +0100453 "pci": pci_extended_schema,
tierno0f98af52018-03-19 10:28:22 +0100454 "switch_port": nameshort_schema,
455 "switch_mac": mac_schema
456 },
457 "required": ["pci"]
458 }
459 }
460 },
461 "required": ["compute_node", "ports"]
462 }
463}
464sdn_external_port_schema = {
465 "$schema": "http://json-schema.org/draft-04/schema#",
tiernocd54a4a2018-09-12 16:40:35 +0200466 "title": "External port information",
tierno0f98af52018-03-19 10:28:22 +0100467 "type": "object",
468 "properties": {
469 "port": {"type": "string", "minLength": 1, "maxLength": 60},
470 "vlan": vlan_schema,
471 "mac": mac_schema
472 },
473 "required": ["port"]
474}
475
tiernocb83c942018-09-24 17:28:13 +0200476# PDUs
477pdu_interface = {
478 "type": "object",
479 "properties": {
480 "name": nameshort_schema,
481 "mgmt": bool_schema,
482 "type": {"enum": ["overlay", 'underlay']},
tierno36ec8602018-11-02 17:27:11 +0100483 "ip-address": ip_schema,
tiernocb83c942018-09-24 17:28:13 +0200484 # TODO, add user, password, ssh-key
tierno36ec8602018-11-02 17:27:11 +0100485 "mac-address": mac_schema,
486 "vim-network-name": nameshort_schema, # interface is connected to one vim network, or switch port
gcalvino17d5b732018-12-17 16:26:21 +0100487 "vim-network-id": nameshort_schema,
tierno36ec8602018-11-02 17:27:11 +0100488 # # provide this in case SDN assist must deal with this interface
489 # "switch-dpid": dpid_Schema,
490 # "switch-port": nameshort_schema,
491 # "switch-mac": nameshort_schema,
492 # "switch-vlan": vlan_schema,
tiernocb83c942018-09-24 17:28:13 +0200493 },
tierno36ec8602018-11-02 17:27:11 +0100494 "required": ["name", "mgmt", "ip-address"],
tiernocb83c942018-09-24 17:28:13 +0200495 "additionalProperties": False
tiernocd54a4a2018-09-12 16:40:35 +0200496}
tiernocb83c942018-09-24 17:28:13 +0200497pdu_new_schema = {
498 "title": "pdu creation input schema",
499 "$schema": "http://json-schema.org/draft-04/schema#",
500 "type": "object",
501 "properties": {
502 "name": nameshort_schema,
503 "type": nameshort_schema,
504 "description": description_schema,
505 "shared": bool_schema,
506 "vims": nameshort_list_schema,
507 "vim_accounts": nameshort_list_schema,
508 "interfaces": {
509 "type": "array",
tierno36ec8602018-11-02 17:27:11 +0100510 "items": pdu_interface,
tiernocb83c942018-09-24 17:28:13 +0200511 "minItems": 1
512 }
513 },
514 "required": ["name", "type", "interfaces"],
515 "additionalProperties": False
516}
517
518pdu_edit_schema = {
519 "title": "pdu edit input schema",
520 "$schema": "http://json-schema.org/draft-04/schema#",
521 "type": "object",
522 "properties": {
523 "name": nameshort_schema,
524 "type": nameshort_schema,
525 "description": description_schema,
526 "shared": bool_schema,
garciadeblas84bdd552018-11-30 22:59:45 +0100527 "vims": {"oneOf": [array_edition_schema, nameshort_list_schema]},
528 "vim_accounts": {"oneOf": [array_edition_schema, nameshort_list_schema]},
529 "interfaces": {"oneOf": [
tiernocb83c942018-09-24 17:28:13 +0200530 array_edition_schema,
531 {
532 "type": "array",
tierno36ec8602018-11-02 17:27:11 +0100533 "items": pdu_interface,
tiernocb83c942018-09-24 17:28:13 +0200534 "minItems": 1
535 }
536 ]}
537 },
538 "additionalProperties": False,
539 "minProperties": 1
540}
541
542# USERS
tiernocd54a4a2018-09-12 16:40:35 +0200543user_new_schema = {
544 "$schema": "http://json-schema.org/draft-04/schema#",
545 "title": "New user schema",
546 "type": "object",
547 "properties": {
548 "username": nameshort_schema,
549 "password": passwd_schema,
tiernocb83c942018-09-24 17:28:13 +0200550 "projects": nameshort_list_schema,
tiernocd54a4a2018-09-12 16:40:35 +0200551 },
552 "required": ["username", "password", "projects"],
553 "additionalProperties": False
554}
555user_edit_schema = {
556 "$schema": "http://json-schema.org/draft-04/schema#",
557 "title": "User edit schema for administrators",
558 "type": "object",
559 "properties": {
560 "password": passwd_schema,
561 "projects": {
garciadeblas84bdd552018-11-30 22:59:45 +0100562 "oneOf": [
tiernocb83c942018-09-24 17:28:13 +0200563 nameshort_list_schema,
tiernocd54a4a2018-09-12 16:40:35 +0200564 array_edition_schema
565 ]
566 },
tiernocb83c942018-09-24 17:28:13 +0200567 },
568 "minProperties": 1,
569 "additionalProperties": False
tiernocd54a4a2018-09-12 16:40:35 +0200570}
571
572# PROJECTS
573project_new_schema = {
574 "$schema": "http://json-schema.org/draft-04/schema#",
575 "title": "New project schema for administrators",
576 "type": "object",
577 "properties": {
578 "name": nameshort_schema,
579 "admin": bool_schema,
580 },
581 "required": ["name"],
582 "additionalProperties": False
583}
584project_edit_schema = {
585 "$schema": "http://json-schema.org/draft-04/schema#",
586 "title": "Project edit schema for administrators",
587 "type": "object",
588 "properties": {
589 "admin": bool_schema,
590 },
591 "additionalProperties": False,
592 "minProperties": 1
593}
594
595# GLOBAL SCHEMAS
tierno0f98af52018-03-19 10:28:22 +0100596
597nbi_new_input_schemas = {
tiernocd54a4a2018-09-12 16:40:35 +0200598 "users": user_new_schema,
599 "projects": project_new_schema,
tierno09c073e2018-04-26 13:36:48 +0200600 "vim_accounts": vim_account_new_schema,
tierno65acb4d2018-04-06 16:42:40 +0200601 "sdns": sdn_new_schema,
602 "ns_instantiate": ns_instantiate,
603 "ns_action": ns_action,
tiernocb83c942018-09-24 17:28:13 +0200604 "ns_scale": ns_scale,
605 "pdus": pdu_new_schema,
tierno0f98af52018-03-19 10:28:22 +0100606}
607
608nbi_edit_input_schemas = {
tiernocd54a4a2018-09-12 16:40:35 +0200609 "users": user_edit_schema,
610 "projects": project_edit_schema,
tierno09c073e2018-04-26 13:36:48 +0200611 "vim_accounts": vim_account_edit_schema,
tiernocb83c942018-09-24 17:28:13 +0200612 "sdns": sdn_edit_schema,
613 "pdus": pdu_edit_schema,
tierno0f98af52018-03-19 10:28:22 +0100614}
615
Felipe Vicens07f31722018-10-29 15:16:44 +0100616# NETSLICE SCHEMAS
garciadeblasdaf8cc52018-11-30 14:17:20 +0100617nsi_slice_instantiate = deepcopy(ns_instantiate)
618nsi_slice_instantiate["title"] = "netslice subnet instantiation params input schema"
619nsi_slice_instantiate["properties"]["id"] = name_schema
Felipe Vicensc8bbaaa2018-12-01 04:42:40 +0100620del nsi_slice_instantiate["required"]
garciadeblasdaf8cc52018-11-30 14:17:20 +0100621
622nsi_vld_instantiate = {
623 "title": "netslice vld instantiation params input schema",
624 "$schema": "http://json-schema.org/draft-04/schema#",
625 "type": "object",
626 "properties": {
627 "name": string_schema,
628 "vim-network-name": {"OneOf": [string_schema, object_schema]},
gcalvino17d5b732018-12-17 16:26:21 +0100629 "vim-network-id": {"OneOf": [string_schema, object_schema]},
garciadeblasdaf8cc52018-11-30 14:17:20 +0100630 "ip-profile": object_schema,
631 },
632 "required": ["name"],
633 "additionalProperties": False
634}
635
Felipe Vicens07f31722018-10-29 15:16:44 +0100636nsi_instantiate = {
637 "title": "netslice action instantiate input schema",
638 "$schema": "http://json-schema.org/draft-04/schema#",
639 "type": "object",
640 "properties": {
641 "lcmOperationType": string_schema,
642 "nsiInstanceId": id_schema,
643 "nsiName": name_schema,
644 "nsiDescription": {"oneOf": [description_schema, {"type": "null"}]},
tierno9e5eea32018-11-29 09:42:09 +0000645 "nstId": string_schema,
Felipe Vicens07f31722018-10-29 15:16:44 +0100646 "vimAccountId": id_schema,
647 "ssh_keys": {"type": "string"},
648 "nsi_id": id_schema,
garciadeblasdaf8cc52018-11-30 14:17:20 +0100649 "netslice-subnet": {
Felipe Vicens07f31722018-10-29 15:16:44 +0100650 "type": "array",
651 "minItems": 1,
garciadeblasdaf8cc52018-11-30 14:17:20 +0100652 "items": nsi_slice_instantiate
653 },
654 "netslice-vld": {
655 "type": "array",
656 "minItems": 1,
657 "items": nsi_vld_instantiate
Felipe Vicens07f31722018-10-29 15:16:44 +0100658 },
659 },
Felipe Vicensc8bbaaa2018-12-01 04:42:40 +0100660 "required": ["nsiName", "nstId", "vimAccountId"],
Felipe Vicens07f31722018-10-29 15:16:44 +0100661 "additionalProperties": False
662}
663
664nsi_action = {
665
666}
667
668nsi_terminate = {
669
670}
671
tierno0f98af52018-03-19 10:28:22 +0100672
673class ValidationError(Exception):
tierno36ec8602018-11-02 17:27:11 +0100674 def __init__(self, message, http_code=HTTPStatus.UNPROCESSABLE_ENTITY):
675 self.http_code = http_code
676 Exception.__init__(self, message)
tierno0f98af52018-03-19 10:28:22 +0100677
678
tiernob24258a2018-10-04 18:39:49 +0200679def validate_input(indata, schema_to_use):
tierno0f98af52018-03-19 10:28:22 +0100680 """
tiernocd54a4a2018-09-12 16:40:35 +0200681 Validates input data against json schema
tierno0f98af52018-03-19 10:28:22 +0100682 :param indata: user input data. Should be a dictionary
tiernob24258a2018-10-04 18:39:49 +0200683 :param schema_to_use: jsonschema to test
684 :return: None if ok, raises ValidationError exception on error
tierno0f98af52018-03-19 10:28:22 +0100685 """
686 try:
tierno0f98af52018-03-19 10:28:22 +0100687 if schema_to_use:
688 js_v(indata, schema_to_use)
689 return None
690 except js_e.ValidationError as e:
691 if e.path:
tierno0da52252018-06-27 15:47:22 +0200692 error_pos = "at '" + ":".join(map(str, e.path)) + "'"
tierno0f98af52018-03-19 10:28:22 +0100693 else:
694 error_pos = ""
tierno441dbbf2018-07-10 12:52:48 +0200695 raise ValidationError("Format error {} '{}' ".format(error_pos, e.message))
tierno36ec8602018-11-02 17:27:11 +0100696 except js_e.SchemaError:
697 raise ValidationError("Bad json schema {}".format(schema_to_use), http_code=HTTPStatus.INTERNAL_SERVER_ERROR)