blob: 7707a754fad6588de9e86412a11a4434732a0e2a [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 = "^[ -~]+$"
tiernofd160572019-01-21 10:41:37 +000030shortname_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": {
tiernofd160572019-01-21 10:41:37 +000072 "^\\$": {}
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,
tiernofd160572019-01-21 10:41:37 +000080 "items": shortname_schema,
tiernocb83c942018-09-24 17:28:13 +020081}
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
tiernofd160572019-01-21 10:41:37 +0000204additional_params_for_vnf = {
205 "type": "array",
206 "items": {
207 "type": "object",
208 "properties": {
209 "member-vnf-index": name_schema,
210 "additionalParams": object_schema,
211 },
212 "required": ["member-vnf-index", "additionalParams"],
213 "additionalProperties": False
214 }
215
216}
217
tierno65acb4d2018-04-06 16:42:40 +0200218ns_instantiate = {
219 "title": "ns action instantiate input schema",
220 "$schema": "http://json-schema.org/draft-04/schema#",
221 "type": "object",
tierno0da52252018-06-27 15:47:22 +0200222 "properties": {
tiernob24258a2018-10-04 18:39:49 +0200223 "lcmOperationType": string_schema,
224 "nsInstanceId": id_schema,
Felipe Vicens07f31722018-10-29 15:16:44 +0100225 "netsliceInstanceId": id_schema,
tierno0da52252018-06-27 15:47:22 +0200226 "nsName": name_schema,
tierno4f9d4ae2019-03-20 17:24:11 +0000227 "nsDescription": {"oneOf": [description_schema, null_schema]},
tierno0da52252018-06-27 15:47:22 +0200228 "nsdId": id_schema,
229 "vimAccountId": id_schema,
tierno4f9d4ae2019-03-20 17:24:11 +0000230 "wimAccountId": {"OneOf": [id_schema, bool_schema, null_schema]},
tiernobee085c2018-12-12 17:03:04 +0000231 "additionalParamsForNs": object_schema,
tiernofd160572019-01-21 10:41:37 +0000232 "additionalParamsForVnf": additional_params_for_vnf,
tiernofc5089c2018-10-31 09:28:11 +0100233 "ssh_keys": {"type": "array", "items": {"type": "string"}},
tierno441dbbf2018-07-10 12:52:48 +0200234 "nsr_id": id_schema,
tiernocc103432018-10-19 14:10:35 +0200235 "vduImage": name_schema,
tierno0da52252018-06-27 15:47:22 +0200236 "vnf": {
237 "type": "array",
238 "minItems": 1,
239 "items": {
240 "type": "object",
241 "properties": {
242 "member-vnf-index": name_schema,
243 "vimAccountId": id_schema,
tierno441dbbf2018-07-10 12:52:48 +0200244 "vdu": {
245 "type": "array",
246 "minItems": 1,
247 "items": ns_instantiate_vdu,
248 },
249 "internal-vld": {
250 "type": "array",
251 "minItems": 1,
252 "items": ns_instantiate_internal_vld
253 }
tierno0da52252018-06-27 15:47:22 +0200254 },
tierno441dbbf2018-07-10 12:52:48 +0200255 "required": ["member-vnf-index"],
256 "minProperties": 2,
257 "additionalProperties": False
tierno0da52252018-06-27 15:47:22 +0200258 }
259 },
260 "vld": {
261 "type": "array",
262 "minItems": 1,
263 "items": {
264 "type": "object",
265 "properties": {
266 "name": string_schema,
267 "vim-network-name": {"OneOf": [string_schema, object_schema]},
gcalvino17d5b732018-12-17 16:26:21 +0100268 "vim-network-id": {"OneOf": [string_schema, object_schema]},
tierno4f9d4ae2019-03-20 17:24:11 +0000269 "wimAccountId": {"OneOf": [id_schema, bool_schema, null_schema]},
tierno0da52252018-06-27 15:47:22 +0200270 "ip-profile": object_schema,
tiernob7c6f2a2018-09-03 14:32:10 +0200271 "vnfd-connection-point-ref": {
272 "type": "array",
273 "minItems": 1,
274 "items": {
275 "type": "object",
276 "properties": {
277 "member-vnf-index-ref": name_schema,
278 "vnfd-connection-point-ref": name_schema,
279 "ip-address": ip_schema,
280 # "mac-address": mac_schema,
281 },
282 "required": ["member-vnf-index-ref", "vnfd-connection-point-ref"],
283 "minProperties": 3,
284 "additionalProperties": False
285 },
286 }
tierno0da52252018-06-27 15:47:22 +0200287 },
tierno441dbbf2018-07-10 12:52:48 +0200288 "required": ["name"],
289 "additionalProperties": False
tierno0da52252018-06-27 15:47:22 +0200290 }
291 },
292 },
tierno441dbbf2018-07-10 12:52:48 +0200293 "required": ["nsName", "nsdId", "vimAccountId"],
294 "additionalProperties": False
tierno65acb4d2018-04-06 16:42:40 +0200295}
tierno0da52252018-06-27 15:47:22 +0200296
tierno65acb4d2018-04-06 16:42:40 +0200297ns_action = { # TODO for the moment it is only contemplated the vnfd primitive execution
tiernof759d822018-06-11 18:54:54 +0200298 "title": "ns action input schema",
tierno65acb4d2018-04-06 16:42:40 +0200299 "$schema": "http://json-schema.org/draft-04/schema#",
300 "type": "object",
301 "properties": {
tiernob24258a2018-10-04 18:39:49 +0200302 "lcmOperationType": string_schema,
303 "nsInstanceId": id_schema,
tiernof5298be2018-05-16 14:43:57 +0200304 "member_vnf_index": name_schema,
305 "vnf_member_index": name_schema, # TODO for backward compatibility. To remove in future
tierno7ce1db92018-07-25 12:50:52 +0200306 "vdu_id": name_schema,
tiernof0637052019-03-07 16:26:47 +0000307 "vdu_count_index": integer0_schema,
tierno65acb4d2018-04-06 16:42:40 +0200308 "primitive": name_schema,
309 "primitive_params": {"type": "object"},
310 },
tiernof5298be2018-05-16 14:43:57 +0200311 "required": ["primitive", "primitive_params"], # TODO add member_vnf_index
tierno65acb4d2018-04-06 16:42:40 +0200312 "additionalProperties": False
313}
tiernof759d822018-06-11 18:54:54 +0200314ns_scale = { # TODO for the moment it is only VDU-scaling
315 "title": "ns scale input schema",
316 "$schema": "http://json-schema.org/draft-04/schema#",
317 "type": "object",
318 "properties": {
tiernob24258a2018-10-04 18:39:49 +0200319 "lcmOperationType": string_schema,
320 "nsInstanceId": id_schema,
tiernof759d822018-06-11 18:54:54 +0200321 "scaleType": {"enum": ["SCALE_VNF"]},
322 "scaleVnfData": {
323 "type": "object",
324 "properties": {
325 "vnfInstanceId": name_schema,
326 "scaleVnfType": {"enum": ["SCALE_OUT", 'SCALE_IN']},
327 "scaleByStepData": {
328 "type": "object",
329 "properties": {
330 "scaling-group-descriptor": name_schema,
331 "member-vnf-index": name_schema,
332 "scaling-policy": name_schema,
333 },
334 "required": ["scaling-group-descriptor", "member-vnf-index"],
335 "additionalProperties": False
336 },
337 },
338 "required": ["scaleVnfType", "scaleByStepData"], # vnfInstanceId
339 "additionalProperties": False
340 },
341 "scaleTime": time_schema,
342 },
343 "required": ["scaleType", "scaleVnfData"],
344 "additionalProperties": False
345}
tierno65acb4d2018-04-06 16:42:40 +0200346
347
tierno0f98af52018-03-19 10:28:22 +0100348schema_version = {"type": "string", "enum": ["1.0"]}
tierno55ba2e62018-12-11 17:22:22 +0000349schema_type = {"type": "string"}
tierno09c073e2018-04-26 13:36:48 +0200350vim_account_edit_schema = {
351 "title": "vim_account edit input schema",
352 "$schema": "http://json-schema.org/draft-04/schema#",
353 "type": "object",
354 "properties": {
355 "name": name_schema,
356 "description": description_schema,
tiernofd160572019-01-21 10:41:37 +0000357 "type": shortname_schema,
tierno09c073e2018-04-26 13:36:48 +0200358 "vim": name_schema,
359 "datacenter": name_schema,
360 "vim_url": description_schema,
361 "vim_url_admin": description_schema,
362 "vim_tenant": name_schema,
363 "vim_tenant_name": name_schema,
tiernofd160572019-01-21 10:41:37 +0000364 "vim_username": shortname_schema,
tiernocd54a4a2018-09-12 16:40:35 +0200365 "vim_password": passwd_schema,
tierno09c073e2018-04-26 13:36:48 +0200366 "config": {"type": "object"}
367 },
368 "additionalProperties": False
369}
tierno0f98af52018-03-19 10:28:22 +0100370
tierno09c073e2018-04-26 13:36:48 +0200371vim_account_new_schema = {
372 "title": "vim_account creation input schema",
tierno0f98af52018-03-19 10:28:22 +0100373 "$schema": "http://json-schema.org/draft-04/schema#",
374 "type": "object",
375 "properties": {
376 "schema_version": schema_version,
377 "schema_type": schema_type,
378 "name": name_schema,
379 "description": description_schema,
tierno09c073e2018-04-26 13:36:48 +0200380 "vim": name_schema,
381 "datacenter": name_schema,
tierno0f98af52018-03-19 10:28:22 +0100382 "vim_type": {"enum": ["openstack", "openvim", "vmware", "opennebula", "aws"]},
383 "vim_url": description_schema,
384 # "vim_url_admin": description_schema,
385 # "vim_tenant": name_schema,
386 "vim_tenant_name": name_schema,
tiernofd160572019-01-21 10:41:37 +0000387 "vim_user": shortname_schema,
tiernocd54a4a2018-09-12 16:40:35 +0200388 "vim_password": passwd_schema,
tierno0f98af52018-03-19 10:28:22 +0100389 "config": {"type": "object"}
390 },
391 "required": ["name", "vim_url", "vim_type", "vim_user", "vim_password", "vim_tenant_name"],
392 "additionalProperties": False
393}
tierno0f98af52018-03-19 10:28:22 +0100394
tierno55ba2e62018-12-11 17:22:22 +0000395wim_account_edit_schema = {
396 "title": "wim_account edit input schema",
397 "$schema": "http://json-schema.org/draft-04/schema#",
398 "type": "object",
399 "properties": {
400 "name": name_schema,
401 "description": description_schema,
tiernofd160572019-01-21 10:41:37 +0000402 "type": shortname_schema,
tierno55ba2e62018-12-11 17:22:22 +0000403 "wim": name_schema,
404 "wim_url": description_schema,
tiernofd160572019-01-21 10:41:37 +0000405 "user": shortname_schema,
tierno55ba2e62018-12-11 17:22:22 +0000406 "password": passwd_schema,
407 "config": {"type": "object"}
408 },
409 "additionalProperties": False
410}
411
412wim_account_new_schema = {
413 "title": "wim_account creation input schema",
414 "$schema": "http://json-schema.org/draft-04/schema#",
415 "type": "object",
416 "properties": {
417 "schema_version": schema_version,
418 "schema_type": schema_type,
419 "name": name_schema,
420 "description": description_schema,
421 "wim": name_schema,
422 "wim_type": {"enum": ["tapi", "onos", "odl", "dynpac"]},
423 "wim_url": description_schema,
tiernofd160572019-01-21 10:41:37 +0000424 "user": shortname_schema,
tierno55ba2e62018-12-11 17:22:22 +0000425 "password": passwd_schema,
tiernof0637052019-03-07 16:26:47 +0000426 "config": {
427 "type": "object",
428 "patternProperties": {
429 ".": {"not": {"type": "null"}}
430 }
431 }
tierno55ba2e62018-12-11 17:22:22 +0000432 },
433 "required": ["name", "wim_url", "wim_type"],
434 "additionalProperties": False
435}
tierno0f98af52018-03-19 10:28:22 +0100436
437sdn_properties = {
438 "name": name_schema,
tiernocfb07c62018-05-10 18:30:51 +0200439 "description": description_schema,
tiernocb83c942018-09-24 17:28:13 +0200440 "dpid": dpid_Schema,
tierno0f98af52018-03-19 10:28:22 +0100441 "ip": ip_schema,
442 "port": port_schema,
443 "type": {"type": "string", "enum": ["opendaylight", "floodlight", "onos"]},
444 "version": {"type": "string", "minLength": 1, "maxLength": 12},
tiernofd160572019-01-21 10:41:37 +0000445 "user": shortname_schema,
tierno0f98af52018-03-19 10:28:22 +0100446 "password": passwd_schema
447}
448sdn_new_schema = {
449 "title": "sdn controller information schema",
450 "$schema": "http://json-schema.org/draft-04/schema#",
451 "type": "object",
452 "properties": sdn_properties,
453 "required": ["name", "port", 'ip', 'dpid', 'type'],
454 "additionalProperties": False
455}
456sdn_edit_schema = {
457 "title": "sdn controller update information schema",
458 "$schema": "http://json-schema.org/draft-04/schema#",
459 "type": "object",
460 "properties": sdn_properties,
tiernocfb07c62018-05-10 18:30:51 +0200461 # "required": ["name", "port", 'ip', 'dpid', 'type'],
tierno0f98af52018-03-19 10:28:22 +0100462 "additionalProperties": False
463}
464sdn_port_mapping_schema = {
465 "$schema": "http://json-schema.org/draft-04/schema#",
466 "title": "sdn port mapping information schema",
467 "type": "array",
468 "items": {
469 "type": "object",
470 "properties": {
tiernofd160572019-01-21 10:41:37 +0000471 "compute_node": shortname_schema,
tierno0f98af52018-03-19 10:28:22 +0100472 "ports": {
473 "type": "array",
474 "items": {
475 "type": "object",
476 "properties": {
tierno42fce592018-11-02 09:23:43 +0100477 "pci": pci_extended_schema,
tiernofd160572019-01-21 10:41:37 +0000478 "switch_port": shortname_schema,
tierno0f98af52018-03-19 10:28:22 +0100479 "switch_mac": mac_schema
480 },
481 "required": ["pci"]
482 }
483 }
484 },
485 "required": ["compute_node", "ports"]
486 }
487}
488sdn_external_port_schema = {
489 "$schema": "http://json-schema.org/draft-04/schema#",
tiernocd54a4a2018-09-12 16:40:35 +0200490 "title": "External port information",
tierno0f98af52018-03-19 10:28:22 +0100491 "type": "object",
492 "properties": {
493 "port": {"type": "string", "minLength": 1, "maxLength": 60},
494 "vlan": vlan_schema,
495 "mac": mac_schema
496 },
497 "required": ["port"]
498}
499
tiernocb83c942018-09-24 17:28:13 +0200500# PDUs
501pdu_interface = {
502 "type": "object",
503 "properties": {
tiernofd160572019-01-21 10:41:37 +0000504 "name": shortname_schema,
tiernocb83c942018-09-24 17:28:13 +0200505 "mgmt": bool_schema,
506 "type": {"enum": ["overlay", 'underlay']},
tierno36ec8602018-11-02 17:27:11 +0100507 "ip-address": ip_schema,
tiernocb83c942018-09-24 17:28:13 +0200508 # TODO, add user, password, ssh-key
tierno36ec8602018-11-02 17:27:11 +0100509 "mac-address": mac_schema,
tiernofd160572019-01-21 10:41:37 +0000510 "vim-network-name": shortname_schema, # interface is connected to one vim network, or switch port
511 "vim-network-id": shortname_schema,
tierno36ec8602018-11-02 17:27:11 +0100512 # # provide this in case SDN assist must deal with this interface
513 # "switch-dpid": dpid_Schema,
tiernofd160572019-01-21 10:41:37 +0000514 # "switch-port": shortname_schema,
515 # "switch-mac": shortname_schema,
tierno36ec8602018-11-02 17:27:11 +0100516 # "switch-vlan": vlan_schema,
tiernocb83c942018-09-24 17:28:13 +0200517 },
tierno36ec8602018-11-02 17:27:11 +0100518 "required": ["name", "mgmt", "ip-address"],
tiernocb83c942018-09-24 17:28:13 +0200519 "additionalProperties": False
tiernocd54a4a2018-09-12 16:40:35 +0200520}
tiernocb83c942018-09-24 17:28:13 +0200521pdu_new_schema = {
522 "title": "pdu creation input schema",
523 "$schema": "http://json-schema.org/draft-04/schema#",
524 "type": "object",
525 "properties": {
tiernofd160572019-01-21 10:41:37 +0000526 "name": shortname_schema,
527 "type": shortname_schema,
tiernocb83c942018-09-24 17:28:13 +0200528 "description": description_schema,
529 "shared": bool_schema,
530 "vims": nameshort_list_schema,
531 "vim_accounts": nameshort_list_schema,
532 "interfaces": {
533 "type": "array",
tierno36ec8602018-11-02 17:27:11 +0100534 "items": pdu_interface,
tiernocb83c942018-09-24 17:28:13 +0200535 "minItems": 1
536 }
537 },
538 "required": ["name", "type", "interfaces"],
539 "additionalProperties": False
540}
541
542pdu_edit_schema = {
543 "title": "pdu edit input schema",
544 "$schema": "http://json-schema.org/draft-04/schema#",
545 "type": "object",
546 "properties": {
tiernofd160572019-01-21 10:41:37 +0000547 "name": shortname_schema,
548 "type": shortname_schema,
tiernocb83c942018-09-24 17:28:13 +0200549 "description": description_schema,
550 "shared": bool_schema,
garciadeblas84bdd552018-11-30 22:59:45 +0100551 "vims": {"oneOf": [array_edition_schema, nameshort_list_schema]},
552 "vim_accounts": {"oneOf": [array_edition_schema, nameshort_list_schema]},
553 "interfaces": {"oneOf": [
tiernocb83c942018-09-24 17:28:13 +0200554 array_edition_schema,
555 {
556 "type": "array",
tierno36ec8602018-11-02 17:27:11 +0100557 "items": pdu_interface,
tiernocb83c942018-09-24 17:28:13 +0200558 "minItems": 1
559 }
560 ]}
561 },
562 "additionalProperties": False,
563 "minProperties": 1
564}
565
566# USERS
tiernocd54a4a2018-09-12 16:40:35 +0200567user_new_schema = {
568 "$schema": "http://json-schema.org/draft-04/schema#",
569 "title": "New user schema",
570 "type": "object",
571 "properties": {
tiernofd160572019-01-21 10:41:37 +0000572 "username": shortname_schema,
tiernocd54a4a2018-09-12 16:40:35 +0200573 "password": passwd_schema,
tiernocb83c942018-09-24 17:28:13 +0200574 "projects": nameshort_list_schema,
tiernocd54a4a2018-09-12 16:40:35 +0200575 },
576 "required": ["username", "password", "projects"],
577 "additionalProperties": False
578}
579user_edit_schema = {
580 "$schema": "http://json-schema.org/draft-04/schema#",
581 "title": "User edit schema for administrators",
582 "type": "object",
583 "properties": {
584 "password": passwd_schema,
585 "projects": {
garciadeblas84bdd552018-11-30 22:59:45 +0100586 "oneOf": [
tiernocb83c942018-09-24 17:28:13 +0200587 nameshort_list_schema,
tiernocd54a4a2018-09-12 16:40:35 +0200588 array_edition_schema
589 ]
590 },
tiernocb83c942018-09-24 17:28:13 +0200591 },
592 "minProperties": 1,
593 "additionalProperties": False
tiernocd54a4a2018-09-12 16:40:35 +0200594}
595
596# PROJECTS
597project_new_schema = {
598 "$schema": "http://json-schema.org/draft-04/schema#",
599 "title": "New project schema for administrators",
600 "type": "object",
601 "properties": {
tiernofd160572019-01-21 10:41:37 +0000602 "name": shortname_schema,
tiernocd54a4a2018-09-12 16:40:35 +0200603 "admin": bool_schema,
604 },
605 "required": ["name"],
606 "additionalProperties": False
607}
608project_edit_schema = {
609 "$schema": "http://json-schema.org/draft-04/schema#",
610 "title": "Project edit schema for administrators",
611 "type": "object",
612 "properties": {
613 "admin": bool_schema,
614 },
615 "additionalProperties": False,
616 "minProperties": 1
617}
618
619# GLOBAL SCHEMAS
tierno0f98af52018-03-19 10:28:22 +0100620
621nbi_new_input_schemas = {
tiernocd54a4a2018-09-12 16:40:35 +0200622 "users": user_new_schema,
623 "projects": project_new_schema,
tierno09c073e2018-04-26 13:36:48 +0200624 "vim_accounts": vim_account_new_schema,
tierno65acb4d2018-04-06 16:42:40 +0200625 "sdns": sdn_new_schema,
626 "ns_instantiate": ns_instantiate,
627 "ns_action": ns_action,
tiernocb83c942018-09-24 17:28:13 +0200628 "ns_scale": ns_scale,
629 "pdus": pdu_new_schema,
tierno0f98af52018-03-19 10:28:22 +0100630}
631
632nbi_edit_input_schemas = {
tiernocd54a4a2018-09-12 16:40:35 +0200633 "users": user_edit_schema,
634 "projects": project_edit_schema,
tierno09c073e2018-04-26 13:36:48 +0200635 "vim_accounts": vim_account_edit_schema,
tiernocb83c942018-09-24 17:28:13 +0200636 "sdns": sdn_edit_schema,
637 "pdus": pdu_edit_schema,
tierno0f98af52018-03-19 10:28:22 +0100638}
639
Felipe Vicens07f31722018-10-29 15:16:44 +0100640# NETSLICE SCHEMAS
tierno032916c2019-03-22 13:27:12 +0000641nsi_subnet_instantiate = deepcopy(ns_instantiate)
642nsi_subnet_instantiate["title"] = "netslice subnet instantiation params input schema"
643nsi_subnet_instantiate["properties"]["id"] = name_schema
644del nsi_subnet_instantiate["required"]
garciadeblasdaf8cc52018-11-30 14:17:20 +0100645
646nsi_vld_instantiate = {
647 "title": "netslice vld instantiation params input schema",
648 "$schema": "http://json-schema.org/draft-04/schema#",
649 "type": "object",
650 "properties": {
651 "name": string_schema,
652 "vim-network-name": {"OneOf": [string_schema, object_schema]},
gcalvino17d5b732018-12-17 16:26:21 +0100653 "vim-network-id": {"OneOf": [string_schema, object_schema]},
garciadeblasdaf8cc52018-11-30 14:17:20 +0100654 "ip-profile": object_schema,
655 },
656 "required": ["name"],
657 "additionalProperties": False
658}
659
Felipe Vicens07f31722018-10-29 15:16:44 +0100660nsi_instantiate = {
661 "title": "netslice action instantiate input schema",
662 "$schema": "http://json-schema.org/draft-04/schema#",
663 "type": "object",
664 "properties": {
665 "lcmOperationType": string_schema,
666 "nsiInstanceId": id_schema,
667 "nsiName": name_schema,
tierno4f9d4ae2019-03-20 17:24:11 +0000668 "nsiDescription": {"oneOf": [description_schema, null_schema]},
tierno9e5eea32018-11-29 09:42:09 +0000669 "nstId": string_schema,
Felipe Vicens07f31722018-10-29 15:16:44 +0100670 "vimAccountId": id_schema,
671 "ssh_keys": {"type": "string"},
672 "nsi_id": id_schema,
tierno032916c2019-03-22 13:27:12 +0000673 "additionalParamsForNsi": object_schema,
garciadeblasdaf8cc52018-11-30 14:17:20 +0100674 "netslice-subnet": {
Felipe Vicens07f31722018-10-29 15:16:44 +0100675 "type": "array",
676 "minItems": 1,
tierno032916c2019-03-22 13:27:12 +0000677 "items": nsi_subnet_instantiate
garciadeblasdaf8cc52018-11-30 14:17:20 +0100678 },
679 "netslice-vld": {
680 "type": "array",
681 "minItems": 1,
682 "items": nsi_vld_instantiate
Felipe Vicens07f31722018-10-29 15:16:44 +0100683 },
684 },
Felipe Vicensc8bbaaa2018-12-01 04:42:40 +0100685 "required": ["nsiName", "nstId", "vimAccountId"],
Felipe Vicens07f31722018-10-29 15:16:44 +0100686 "additionalProperties": False
687}
688
689nsi_action = {
690
691}
692
693nsi_terminate = {
694
695}
696
tierno0f98af52018-03-19 10:28:22 +0100697
698class ValidationError(Exception):
tierno36ec8602018-11-02 17:27:11 +0100699 def __init__(self, message, http_code=HTTPStatus.UNPROCESSABLE_ENTITY):
700 self.http_code = http_code
701 Exception.__init__(self, message)
tierno0f98af52018-03-19 10:28:22 +0100702
703
tiernob24258a2018-10-04 18:39:49 +0200704def validate_input(indata, schema_to_use):
tierno0f98af52018-03-19 10:28:22 +0100705 """
tiernocd54a4a2018-09-12 16:40:35 +0200706 Validates input data against json schema
tierno0f98af52018-03-19 10:28:22 +0100707 :param indata: user input data. Should be a dictionary
tiernob24258a2018-10-04 18:39:49 +0200708 :param schema_to_use: jsonschema to test
709 :return: None if ok, raises ValidationError exception on error
tierno0f98af52018-03-19 10:28:22 +0100710 """
711 try:
tierno0f98af52018-03-19 10:28:22 +0100712 if schema_to_use:
713 js_v(indata, schema_to_use)
714 return None
715 except js_e.ValidationError as e:
716 if e.path:
tierno0da52252018-06-27 15:47:22 +0200717 error_pos = "at '" + ":".join(map(str, e.path)) + "'"
tierno0f98af52018-03-19 10:28:22 +0100718 else:
719 error_pos = ""
tierno441dbbf2018-07-10 12:52:48 +0200720 raise ValidationError("Format error {} '{}' ".format(error_pos, e.message))
tierno36ec8602018-11-02 17:27:11 +0100721 except js_e.SchemaError:
722 raise ValidationError("Bad json schema {}".format(schema_to_use), http_code=HTTPStatus.INTERNAL_SERVER_ERROR)