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