Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 1 | #!/usr/bin/python3 |
| 2 | import requests |
| 3 | import zipfile |
| 4 | from io import BytesIO |
| 5 | import os |
| 6 | from collections import defaultdict |
| 7 | from collections import OrderedDict |
| 8 | from distutils.version import StrictVersion |
| 9 | import shutil |
| 10 | import json |
James Feist | aee8d84 | 2018-09-10 16:07:40 -0700 | [diff] [blame] | 11 | import glob |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 12 | |
Ed Tanous | 118b1c7 | 2018-09-13 13:45:51 -0700 | [diff] [blame] | 13 | import xml.etree.ElementTree as ET |
| 14 | |
Gunnar Mills | 10f270b | 2021-05-19 15:34:06 -0500 | [diff] [blame] | 15 | VERSION = "DSP8010_2021.1" |
Ed Tanous | cb10313 | 2019-10-08 11:34:22 -0700 | [diff] [blame] | 16 | |
Gunnar Mills | 349a2ac | 2021-01-20 22:29:16 -0600 | [diff] [blame] | 17 | # To use a new schema, add to list and rerun tool |
| 18 | include_list = [ |
| 19 | 'AccountService', |
| 20 | 'ActionInfo', |
| 21 | 'Assembly', |
| 22 | 'AttributeRegistry', |
| 23 | 'Bios', |
| 24 | 'Certificate', |
| 25 | 'CertificateCollection', |
| 26 | 'CertificateLocations', |
| 27 | 'CertificateService', |
| 28 | 'Chassis', |
| 29 | 'ChassisCollection', |
| 30 | 'ComputerSystem', |
| 31 | 'ComputerSystemCollection', |
| 32 | 'Drive', |
| 33 | 'DriveCollection', |
| 34 | 'EthernetInterface', |
| 35 | 'EthernetInterfaceCollection', |
| 36 | 'Event', |
| 37 | 'EventDestination', |
| 38 | 'EventDestinationCollection', |
| 39 | 'EventService', |
| 40 | 'IPAddresses', |
| 41 | 'JsonSchemaFile', |
| 42 | 'JsonSchemaFileCollection', #redfish/v1/JsonSchemas |
| 43 | 'LogEntry', |
| 44 | 'LogEntryCollection', |
| 45 | 'LogService', |
| 46 | 'LogServiceCollection', |
| 47 | 'Manager', |
| 48 | 'ManagerAccount', |
| 49 | 'ManagerAccountCollection', |
| 50 | 'ManagerCollection', |
| 51 | 'ManagerNetworkProtocol', |
| 52 | 'Memory', |
| 53 | 'MemoryCollection', |
| 54 | 'Message', |
| 55 | 'MessageRegistry', |
| 56 | 'MessageRegistryCollection', |
| 57 | 'MessageRegistryFile', |
| 58 | 'MessageRegistryFileCollection', |
| 59 | 'MetricDefinition', |
| 60 | 'MetricDefinitionCollection', |
| 61 | 'MetricReport', |
| 62 | 'MetricReportCollection', |
| 63 | 'MetricReportDefinition', |
| 64 | 'MetricReportDefinitionCollection', |
| 65 | 'OperatingConfig', |
| 66 | 'OperatingConfigCollection', |
| 67 | 'PCIeDevice', |
| 68 | 'PCIeDeviceCollection', |
| 69 | 'PCIeFunction', |
| 70 | 'PCIeFunctionCollection', |
| 71 | 'Power', |
| 72 | 'Processor', |
| 73 | 'ProcessorCollection', |
| 74 | 'RedfishError', |
| 75 | 'RedfishExtensions', |
| 76 | 'Redundancy', |
| 77 | 'Resource', |
| 78 | 'Role', |
| 79 | 'RoleCollection', |
| 80 | 'Sensor', |
| 81 | 'SensorCollection', |
| 82 | 'ServiceRoot', |
| 83 | 'Session', |
| 84 | 'SessionCollection', |
| 85 | 'SessionService', |
| 86 | 'Settings', |
| 87 | 'SoftwareInventory', |
| 88 | 'SoftwareInventoryCollection', |
| 89 | 'Storage', |
| 90 | 'StorageCollection', |
| 91 | 'StorageController', |
| 92 | 'StorageControllerCollection', |
| 93 | 'Task', |
| 94 | 'TaskCollection', |
| 95 | 'TaskService', |
| 96 | 'TelemetryService', |
| 97 | 'Thermal', |
| 98 | 'UpdateService', |
| 99 | 'VLanNetworkInterfaceCollection', |
| 100 | 'VLanNetworkInterface', |
| 101 | 'VirtualMedia', |
| 102 | 'VirtualMediaCollection', |
| 103 | 'odata', |
| 104 | 'odata-v4', |
| 105 | 'redfish-error', |
| 106 | 'redfish-payload-annotations', |
| 107 | 'redfish-schema', |
| 108 | 'redfish-schema-v1', |
| 109 | ] |
| 110 | |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 111 | SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__)) |
| 112 | |
| 113 | proxies = { |
| 114 | 'https': os.environ.get("https_proxy", None) |
| 115 | } |
| 116 | |
Ed Tanous | cb10313 | 2019-10-08 11:34:22 -0700 | [diff] [blame] | 117 | r = requests.get( |
| 118 | 'https://www.dmtf.org/sites/default/files/standards/documents/' + |
| 119 | VERSION + |
| 120 | '.zip', |
| 121 | proxies=proxies) |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 122 | |
| 123 | r.raise_for_status() |
| 124 | |
| 125 | static_path = os.path.realpath(os.path.join(SCRIPT_DIR, "..", "static", |
| 126 | "redfish", "v1")) |
| 127 | |
| 128 | schema_path = os.path.join(static_path, "schema") |
| 129 | json_schema_path = os.path.join(static_path, "JsonSchemas") |
Ed Tanous | 118b1c7 | 2018-09-13 13:45:51 -0700 | [diff] [blame] | 130 | metadata_index_path = os.path.join(static_path, "$metadata", "index.xml") |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 131 | |
| 132 | zipBytesIO = BytesIO(r.content) |
| 133 | zip_ref = zipfile.ZipFile(zipBytesIO) |
| 134 | |
| 135 | # Remove the old files |
| 136 | if os.path.exists(schema_path): |
James Feist | aee8d84 | 2018-09-10 16:07:40 -0700 | [diff] [blame] | 137 | files = glob.glob(os.path.join(schema_path, '[!Oem]*')) |
| 138 | for f in files: |
| 139 | os.remove(f) |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 140 | if os.path.exists(json_schema_path): |
Ed Tanous | 118b1c7 | 2018-09-13 13:45:51 -0700 | [diff] [blame] | 141 | files = glob.glob(os.path.join(json_schema_path, '[!Oem]*')) |
| 142 | for f in files: |
| 143 | if (os.path.isfile(f)): |
| 144 | os.remove(f) |
| 145 | else: |
| 146 | shutil.rmtree(f) |
| 147 | os.remove(metadata_index_path) |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 148 | |
Ed Tanous | 118b1c7 | 2018-09-13 13:45:51 -0700 | [diff] [blame] | 149 | if not os.path.exists(schema_path): |
| 150 | os.makedirs(schema_path) |
| 151 | if not os.path.exists(json_schema_path): |
| 152 | os.makedirs(json_schema_path) |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 153 | |
Ed Tanous | 118b1c7 | 2018-09-13 13:45:51 -0700 | [diff] [blame] | 154 | with open(metadata_index_path, 'w') as metadata_index: |
| 155 | |
| 156 | metadata_index.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n") |
| 157 | metadata_index.write( |
| 158 | "<edmx:Edmx xmlns:edmx=\"http://docs.oasis-open.org/odata/ns/edmx\" Version=\"4.0\">\n") |
| 159 | |
| 160 | for zip_filepath in zip_ref.namelist(): |
Ed Tanous | cb10313 | 2019-10-08 11:34:22 -0700 | [diff] [blame] | 161 | if zip_filepath.startswith(VERSION + |
Ed Tanous | cb10313 | 2019-10-08 11:34:22 -0700 | [diff] [blame] | 162 | '/csdl/') & (zip_filepath != VERSION + |
| 163 | "/csdl/") & (zip_filepath != VERSION + |
Ed Tanous | cb10313 | 2019-10-08 11:34:22 -0700 | [diff] [blame] | 164 | "/csdl/"): |
Ed Tanous | 118b1c7 | 2018-09-13 13:45:51 -0700 | [diff] [blame] | 165 | filename = os.path.basename(zip_filepath) |
Gunnar Mills | 349a2ac | 2021-01-20 22:29:16 -0600 | [diff] [blame] | 166 | |
| 167 | # filename looks like Zone_v1.xml |
| 168 | filenamesplit = filename.split("_") |
| 169 | if filenamesplit[0] not in include_list: |
| 170 | print("excluding schema: " + filename) |
| 171 | continue |
| 172 | |
Ed Tanous | 118b1c7 | 2018-09-13 13:45:51 -0700 | [diff] [blame] | 173 | with open(os.path.join(schema_path, filename), 'wb') as schema_file: |
| 174 | |
| 175 | metadata_index.write( |
Ed Tanous | cb10313 | 2019-10-08 11:34:22 -0700 | [diff] [blame] | 176 | " <edmx:Reference Uri=\"/redfish/v1/schema/" + |
| 177 | filename + |
| 178 | "\">\n") |
Ed Tanous | 118b1c7 | 2018-09-13 13:45:51 -0700 | [diff] [blame] | 179 | |
| 180 | content = zip_ref.read(zip_filepath) |
Ed Tanous | cb10313 | 2019-10-08 11:34:22 -0700 | [diff] [blame] | 181 | content = content.replace(b'\r\n', b'\n') |
Ed Tanous | 118b1c7 | 2018-09-13 13:45:51 -0700 | [diff] [blame] | 182 | xml_root = ET.fromstring(content) |
| 183 | |
| 184 | for edmx_child in xml_root: |
| 185 | |
| 186 | if edmx_child.tag == "{http://docs.oasis-open.org/odata/ns/edmx}DataServices": |
| 187 | for data_child in edmx_child: |
| 188 | if data_child.tag == "{http://docs.oasis-open.org/odata/ns/edm}Schema": |
| 189 | namespace = data_child.attrib["Namespace"] |
| 190 | if namespace.startswith("RedfishExtensions"): |
| 191 | metadata_index.write( |
| 192 | " <edmx:Include Namespace=\"" + namespace + "\" Alias=\"Redfish\"/>\n") |
| 193 | |
| 194 | else: |
| 195 | metadata_index.write( |
| 196 | " <edmx:Include Namespace=\"" + namespace + "\"/>\n") |
| 197 | schema_file.write(content) |
| 198 | metadata_index.write(" </edmx:Reference>\n") |
| 199 | |
| 200 | metadata_index.write(""" <edmx:DataServices> |
| 201 | <Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="Service"> |
| 202 | <EntityContainer Name="Service" Extends="ServiceRoot.v1_0_0.ServiceContainer"/> |
| 203 | </Schema> |
| 204 | </edmx:DataServices> |
| 205 | """) |
Ed Tanous | cb10313 | 2019-10-08 11:34:22 -0700 | [diff] [blame] | 206 | # TODO:Issue#32 There's a bug in the script that currently deletes this |
Asmitha Karunanithi | d337bb7 | 2020-09-21 10:34:02 -0500 | [diff] [blame] | 207 | # schema (because it's an OEM schema). Because it's the only six, and we |
Ed Tanous | cb10313 | 2019-10-08 11:34:22 -0700 | [diff] [blame] | 208 | # don't update schemas very often, we just manually fix it. Need a |
| 209 | # permanent fix to the script. |
| 210 | metadata_index.write( |
| 211 | " <edmx:Reference Uri=\"/redfish/v1/schema/OemManager_v1.xml\">\n") |
Marri Devender Rao | d45d2d0 | 2019-01-21 10:11:34 -0600 | [diff] [blame] | 212 | metadata_index.write(" <edmx:Include Namespace=\"OemManager\"/>\n") |
| 213 | metadata_index.write(" </edmx:Reference>\n") |
Gunnar Mills | 2077899 | 2020-02-06 16:36:47 -0600 | [diff] [blame] | 214 | |
| 215 | metadata_index.write( |
Gunnar Mills | 2077899 | 2020-02-06 16:36:47 -0600 | [diff] [blame] | 216 | " <edmx:Reference Uri=\"/redfish/v1/schema/OemComputerSystem_v1.xml\">\n") |
| 217 | metadata_index.write(" <edmx:Include Namespace=\"OemComputerSystem\"/>\n") |
| 218 | metadata_index.write(" </edmx:Reference>\n") |
| 219 | |
| 220 | metadata_index.write( |
| 221 | " <edmx:Reference Uri=\"/redfish/v1/schema/OemVirtualMedia_v1.xml\">\n") |
| 222 | metadata_index.write(" <edmx:Include Namespace=\"OemVirtualMedia\"/>\n") |
| 223 | metadata_index.write(" <edmx:Include Namespace=\"OemVirtualMedia.v1_0_0\"/>\n") |
| 224 | metadata_index.write(" </edmx:Reference>\n") |
| 225 | |
| 226 | metadata_index.write( |
| 227 | " <edmx:Reference Uri=\"/redfish/v1/schema/OemAccountService_v1.xml\">\n") |
| 228 | metadata_index.write(" <edmx:Include Namespace=\"OemAccountService\"/>\n") |
| 229 | metadata_index.write(" <edmx:Include Namespace=\"OemAccountService.v1_0_0\"/>\n") |
| 230 | metadata_index.write(" </edmx:Reference>\n") |
| 231 | |
Ravi Teja | e7d68c3 | 2020-03-15 13:30:41 -0500 | [diff] [blame] | 232 | metadata_index.write( |
Sunitha Harish | 9dc5074 | 2020-05-11 00:10:20 -0500 | [diff] [blame] | 233 | " <edmx:Reference Uri=\"/redfish/v1/schema/OemSession_v1.xml\">\n") |
| 234 | metadata_index.write(" <edmx:Include Namespace=\"OemSession\"/>\n") |
| 235 | metadata_index.write(" <edmx:Include Namespace=\"OemSession.v1_0_0\"/>\n") |
| 236 | metadata_index.write(" </edmx:Reference>\n") |
| 237 | |
Ed Tanous | 118b1c7 | 2018-09-13 13:45:51 -0700 | [diff] [blame] | 238 | metadata_index.write("</edmx:Edmx>\n") |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 239 | |
| 240 | schema_files = {} |
| 241 | for zip_filepath in zip_ref.namelist(): |
Gunnar Mills | a778c02 | 2020-05-12 12:20:36 -0500 | [diff] [blame] | 242 | if zip_filepath.startswith(os.path.join(VERSION, 'json-schema/')): |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 243 | filename = os.path.basename(zip_filepath) |
| 244 | filenamesplit = filename.split(".") |
Gunnar Mills | 349a2ac | 2021-01-20 22:29:16 -0600 | [diff] [blame] | 245 | |
| 246 | # exclude schemas again to save flash space |
| 247 | if filenamesplit[0] not in include_list: |
| 248 | continue |
| 249 | |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 250 | if len(filenamesplit) == 3: |
| 251 | thisSchemaVersion = schema_files.get(filenamesplit[0], None) |
Ed Tanous | cb10313 | 2019-10-08 11:34:22 -0700 | [diff] [blame] | 252 | if thisSchemaVersion is None: |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 253 | schema_files[filenamesplit[0]] = filenamesplit[1] |
| 254 | else: |
| 255 | # need to see if we're a newer version. |
| 256 | if list(map(int, filenamesplit[1][1:].split("_"))) > list(map( |
| 257 | int, thisSchemaVersion[1:].split("_"))): |
| 258 | schema_files[filenamesplit[0]] = filenamesplit[1] |
| 259 | |
| 260 | |
| 261 | for schema, version in schema_files.items(): |
| 262 | basename = schema + "." + version + ".json" |
Gunnar Mills | a778c02 | 2020-05-12 12:20:36 -0500 | [diff] [blame] | 263 | zip_filepath = os.path.join(VERSION, "json-schema", basename) |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 264 | schemadir = os.path.join(json_schema_path, schema) |
| 265 | os.makedirs(schemadir) |
Marri Devender Rao | d45d2d0 | 2019-01-21 10:11:34 -0600 | [diff] [blame] | 266 | location_json = OrderedDict() |
| 267 | location_json["Language"] = "en" |
| 268 | location_json["PublicationUri"] = ( |
| 269 | "http://redfish.dmtf.org/schemas/v1/" + schema + ".json") |
| 270 | location_json["Uri"] = ( |
Ed Tanous | 63faafa | 2019-01-03 14:09:56 -0800 | [diff] [blame] | 271 | "/redfish/v1/JsonSchemas/" + schema + "/" + schema + ".json") |
Ed Tanous | 118b1c7 | 2018-09-13 13:45:51 -0700 | [diff] [blame] | 272 | |
Marri Devender Rao | d45d2d0 | 2019-01-21 10:11:34 -0600 | [diff] [blame] | 273 | index_json = OrderedDict() |
| 274 | index_json["@odata.context"] = "/redfish/v1/$metadata#JsonSchemaFile.JsonSchemaFile" |
Ed Tanous | 63faafa | 2019-01-03 14:09:56 -0800 | [diff] [blame] | 275 | index_json["@odata.id"] = "/redfish/v1/JsonSchemas/" + schema |
Marri Devender Rao | d45d2d0 | 2019-01-21 10:11:34 -0600 | [diff] [blame] | 276 | index_json["@odata.type"] = "#JsonSchemaFile.v1_0_2.JsonSchemaFile" |
| 277 | index_json["Name"] = schema + " Schema File" |
| 278 | index_json["Schema"] = "#" + schema + "." + schema |
Ed Tanous | cb10313 | 2019-10-08 11:34:22 -0700 | [diff] [blame] | 279 | index_json["Description"] = schema + " Schema File Location" |
Marri Devender Rao | d45d2d0 | 2019-01-21 10:11:34 -0600 | [diff] [blame] | 280 | index_json["Id"] = schema |
Ed Tanous | cb10313 | 2019-10-08 11:34:22 -0700 | [diff] [blame] | 281 | index_json["Languages"] = ["en"] |
Marri Devender Rao | d45d2d0 | 2019-01-21 10:11:34 -0600 | [diff] [blame] | 282 | index_json["Languages@odata.count"] = 1 |
Ed Tanous | cb10313 | 2019-10-08 11:34:22 -0700 | [diff] [blame] | 283 | index_json["Location"] = [location_json] |
Marri Devender Rao | d45d2d0 | 2019-01-21 10:11:34 -0600 | [diff] [blame] | 284 | index_json["Location@odata.count"] = 1 |
Ed Tanous | 118b1c7 | 2018-09-13 13:45:51 -0700 | [diff] [blame] | 285 | |
| 286 | with open(os.path.join(schemadir, "index.json"), 'w') as schema_file: |
| 287 | json.dump(index_json, schema_file, indent=4) |
| 288 | with open(os.path.join(schemadir, schema + ".json"), 'wb') as schema_file: |
Ed Tanous | cb10313 | 2019-10-08 11:34:22 -0700 | [diff] [blame] | 289 | schema_file.write(zip_ref.read(zip_filepath).replace(b'\r\n', b'\n')) |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 290 | |
| 291 | with open(os.path.join(json_schema_path, "index.json"), 'w') as index_file: |
Ed Tanous | 6f56d0c | 2019-05-03 17:15:41 -0700 | [diff] [blame] | 292 | members = [{"@odata.id": "/redfish/v1/JsonSchemas/" + schema} |
Ed Tanous | 683f727 | 2018-07-26 12:47:19 -0700 | [diff] [blame] | 293 | for schema in schema_files] |
| 294 | |
| 295 | members.sort(key=lambda x: x["@odata.id"]) |
| 296 | |
| 297 | indexData = OrderedDict() |
| 298 | |
| 299 | indexData["@odata.id"] = "/redfish/v1/JsonSchemas" |
| 300 | indexData["@odata.context"] = ("/redfish/v1/$metadata" |
| 301 | "#JsonSchemaFileCollection." |
| 302 | "JsonSchemaFileCollection") |
| 303 | indexData["@odata.type"] = ("#JsonSchemaFileCollection." |
| 304 | "JsonSchemaFileCollection") |
| 305 | indexData["Name"] = "JsonSchemaFile Collection" |
| 306 | indexData["Description"] = "Collection of JsonSchemaFiles" |
| 307 | indexData["Members@odata.count"] = len(schema_files) |
| 308 | indexData["Members"] = members |
| 309 | |
| 310 | json.dump(indexData, index_file, indent=2) |
| 311 | |
| 312 | zip_ref.close() |