| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| 3 | from jsonschema import validate as js_v, exceptions as js_e |
| 4 | |
| 5 | __author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>" |
| 6 | __version__ = "0.1" |
| 7 | version_date = "Mar 2018" |
| 8 | |
| 9 | """ |
| 10 | Validator of input data using JSON schemas for those items that not contains an OSM yang information model |
| 11 | """ |
| 12 | |
| 13 | # Basis schemas |
| 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 | xml_text_schema = {"type": "string", "minLength": 1, "maxLength": 1000, "pattern": "^[^']+$"} |
| 19 | description_schema = {"type": ["string", "null"], "maxLength": 255, "pattern": "^[^'\"]+$"} |
| 20 | id_schema_fake = {"type": "string", "minLength": 2, |
| 21 | "maxLength": 36} # "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}$" |
| 22 | id_schema = {"type": "string", "pattern": "^[a-fA-F0-9]{8}(-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}$"} |
| 23 | pci_schema = {"type": "string", "pattern": "^[0-9a-fA-F]{4}(:[0-9a-fA-F]{2}){2}\.[0-9a-fA-F]$"} |
| 24 | http_schema = {"type": "string", "pattern": "^https?://[^'\"=]+$"} |
| 25 | bandwidth_schema = {"type": "string", "pattern": "^[0-9]+ *([MG]bps)?$"} |
| 26 | memory_schema = {"type": "string", "pattern": "^[0-9]+ *([MG]i?[Bb])?$"} |
| 27 | integer0_schema = {"type": "integer", "minimum": 0} |
| 28 | integer1_schema = {"type": "integer", "minimum": 1} |
| 29 | path_schema = {"type": "string", "pattern": "^(\.){0,2}(/[^/\"':{}\(\)]+)+$"} |
| 30 | vlan_schema = {"type": "integer", "minimum": 1, "maximum": 4095} |
| 31 | vlan1000_schema = {"type": "integer", "minimum": 1000, "maximum": 4095} |
| 32 | mac_schema = {"type": "string", |
| 33 | "pattern": "^[0-9a-fA-F][02468aceACE](:[0-9a-fA-F]{2}){5}$"} # must be unicast: LSB bit of MSB byte ==0 |
| 34 | # mac_schema={"type":"string", "pattern":"^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$"} |
| 35 | ip_schema = {"type": "string", |
| 36 | "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]?)$"} |
| 37 | ip_prefix_schema = {"type": "string", |
| 38 | "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]?)/(30|[12]?[0-9])$"} |
| 39 | port_schema = {"type": "integer", "minimum": 1, "maximum": 65534} |
| 40 | object_schema = {"type": "object"} |
| 41 | schema_version_2 = {"type": "integer", "minimum": 2, "maximum": 2} |
| 42 | # schema_version_string={"type":"string","enum": ["0.1", "2", "0.2", "3", "0.3"]} |
| 43 | log_level_schema = {"type": "string", "enum": ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]} |
| 44 | checksum_schema = {"type": "string", "pattern": "^[0-9a-fA-F]{32}$"} |
| 45 | size_schema = {"type": "integer", "minimum": 1, "maximum": 100} |
| 46 | |
| 47 | schema_version = {"type": "string", "enum": ["1.0"]} |
| 48 | schema_type = {"type": "string"} |
| 49 | |
| 50 | vim_new_schema = { |
| 51 | "title": "vims new user input schema", |
| 52 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 53 | "type": "object", |
| 54 | "properties": { |
| 55 | "schema_version": schema_version, |
| 56 | "schema_type": schema_type, |
| 57 | "name": name_schema, |
| 58 | "description": description_schema, |
| 59 | "vim_type": {"enum": ["openstack", "openvim", "vmware", "opennebula", "aws"]}, |
| 60 | "vim_url": description_schema, |
| 61 | # "vim_url_admin": description_schema, |
| 62 | # "vim_tenant": name_schema, |
| 63 | "vim_tenant_name": name_schema, |
| 64 | "vim_user": nameshort_schema, |
| 65 | "vim_password": nameshort_schema, |
| 66 | "config": {"type": "object"} |
| 67 | }, |
| 68 | "required": ["name", "vim_url", "vim_type", "vim_user", "vim_password", "vim_tenant_name"], |
| 69 | "additionalProperties": False |
| 70 | } |
| 71 | vim_edit_schema = { |
| 72 | "title": "datacenter edit nformation schema", |
| 73 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 74 | "type": "object", |
| 75 | "properties": { |
| 76 | "name": name_schema, |
| 77 | "description": description_schema, |
| 78 | "type": nameshort_schema, # currently "openvim" or "openstack", can be enlarged with plugins |
| 79 | "vim_url": description_schema, |
| 80 | "vim_url_admin": description_schema, |
| 81 | "vim_tenant": name_schema, |
| 82 | "vim_tenant_name": name_schema, |
| 83 | "vim_username": nameshort_schema, |
| 84 | "vim_password": nameshort_schema, |
| 85 | "config": {"type": "object"} |
| 86 | }, |
| 87 | "additionalProperties": False |
| 88 | } |
| 89 | |
| 90 | |
| 91 | sdn_properties = { |
| 92 | "name": name_schema, |
| 93 | "dpid": {"type": "string", "pattern": "^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){7}$"}, |
| 94 | "ip": ip_schema, |
| 95 | "port": port_schema, |
| 96 | "type": {"type": "string", "enum": ["opendaylight", "floodlight", "onos"]}, |
| 97 | "version": {"type": "string", "minLength": 1, "maxLength": 12}, |
| 98 | "user": nameshort_schema, |
| 99 | "password": passwd_schema |
| 100 | } |
| 101 | sdn_new_schema = { |
| 102 | "title": "sdn controller information schema", |
| 103 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 104 | "type": "object", |
| 105 | "properties": sdn_properties, |
| 106 | "required": ["name", "port", 'ip', 'dpid', 'type'], |
| 107 | "additionalProperties": False |
| 108 | } |
| 109 | sdn_edit_schema = { |
| 110 | "title": "sdn controller update information schema", |
| 111 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 112 | "type": "object", |
| 113 | "properties": sdn_properties, |
| 114 | "required": ["name", "port", 'ip', 'dpid', 'type'], |
| 115 | "additionalProperties": False |
| 116 | } |
| 117 | sdn_port_mapping_schema = { |
| 118 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 119 | "title": "sdn port mapping information schema", |
| 120 | "type": "array", |
| 121 | "items": { |
| 122 | "type": "object", |
| 123 | "properties": { |
| 124 | "compute_node": nameshort_schema, |
| 125 | "ports": { |
| 126 | "type": "array", |
| 127 | "items": { |
| 128 | "type": "object", |
| 129 | "properties": { |
| 130 | "pci": pci_schema, |
| 131 | "switch_port": nameshort_schema, |
| 132 | "switch_mac": mac_schema |
| 133 | }, |
| 134 | "required": ["pci"] |
| 135 | } |
| 136 | } |
| 137 | }, |
| 138 | "required": ["compute_node", "ports"] |
| 139 | } |
| 140 | } |
| 141 | sdn_external_port_schema = { |
| 142 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 143 | "title": "External port ingformation", |
| 144 | "type": "object", |
| 145 | "properties": { |
| 146 | "port": {"type": "string", "minLength": 1, "maxLength": 60}, |
| 147 | "vlan": vlan_schema, |
| 148 | "mac": mac_schema |
| 149 | }, |
| 150 | "required": ["port"] |
| 151 | } |
| 152 | |
| 153 | |
| 154 | nbi_new_input_schemas = { |
| 155 | "vims": vim_new_schema, |
| 156 | "sdns": sdn_new_schema |
| 157 | } |
| 158 | |
| 159 | nbi_edit_input_schemas = { |
| 160 | "vims": vim_edit_schema, |
| 161 | "sdns": sdn_edit_schema |
| 162 | } |
| 163 | |
| 164 | |
| 165 | class ValidationError(Exception): |
| 166 | pass |
| 167 | |
| 168 | |
| 169 | def validate_input(indata, item, new=True): |
| 170 | """ |
| 171 | Validates input data agains json schema |
| 172 | :param indata: user input data. Should be a dictionary |
| 173 | :param item: can be users, projects, vims, sdns |
| 174 | :param new: True if the validation is for creating or False if it is for editing |
| 175 | :return: None if ok, raises ValidationError exception otherwise |
| 176 | """ |
| 177 | try: |
| 178 | if new: |
| 179 | schema_to_use = nbi_new_input_schemas.get(item) |
| 180 | else: |
| 181 | schema_to_use = nbi_edit_input_schemas.get(item) |
| 182 | if schema_to_use: |
| 183 | js_v(indata, schema_to_use) |
| 184 | return None |
| 185 | except js_e.ValidationError as e: |
| 186 | if e.path: |
| 187 | error_pos = "at '" + ":".join(e.path) + "'" |
| 188 | else: |
| 189 | error_pos = "" |
| 190 | raise ValidationError("Format error {} '{}' ".format(error_pos, e)) |