blob: b0bf0d4cb1ffeb8cf8a370956415fdac90596cd7 [file] [log] [blame]
Nan Zhou313c1b72022-03-25 11:47:55 -07001#!/usr/bin/env python3
Ed Tanous683f7272018-07-26 12:47:19 -07002import os
Ed Tanous683f7272018-07-26 12:47:19 -07003import shutil
Ed Tanous118b1c72018-09-13 13:45:51 -07004import xml.etree.ElementTree as ET
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -06005import zipfile
6from collections import OrderedDict, defaultdict
7from io import BytesIO
8
Ed Tanous0ec8b832022-03-14 14:56:47 -07009import generate_schema_enums
Patrick Williamsfd06b302022-12-12 10:39:42 -060010import requests
Carson Labrado9e031402022-07-08 20:56:52 +000011from generate_schema_collections import generate_top_collections
Ed Tanous118b1c72018-09-13 13:45:51 -070012
Asmitha Karunanithi009c6452022-09-22 01:07:59 -050013VERSION = "DSP8010_2022.2"
Ed Tanouscb103132019-10-08 11:34:22 -070014
Ed Tanous27747912022-09-23 12:50:08 -070015WARNING = """/****************************************************************
Ed Tanous81d523a2022-05-25 12:00:51 -070016 * READ THIS WARNING FIRST
17 * This is an auto-generated header which contains definitions
18 * for Redfish DMTF defined schemas.
19 * DO NOT modify this registry outside of running the
20 * update_schemas.py script. The definitions contained within
21 * this file are owned by DMTF. Any modifications to these files
22 * should be first pushed to the relevant registry in the DMTF
23 * github organization.
Ed Tanous27747912022-09-23 12:50:08 -070024 ***************************************************************/"""
Ed Tanous81d523a2022-05-25 12:00:51 -070025
Gunnar Mills349a2ac2021-01-20 22:29:16 -060026# To use a new schema, add to list and rerun tool
27include_list = [
Ed Tanous27747912022-09-23 12:50:08 -070028 "AccountService",
29 "ActionInfo",
30 "Assembly",
31 "AttributeRegistry",
32 "Bios",
33 "Cable",
34 "CableCollection",
35 "Certificate",
36 "CertificateCollection",
37 "CertificateLocations",
38 "CertificateService",
39 "Chassis",
40 "ChassisCollection",
41 "ComputerSystem",
42 "ComputerSystemCollection",
43 "Drive",
44 "DriveCollection",
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -060045 "EnvironmentMetrics",
Ed Tanous27747912022-09-23 12:50:08 -070046 "EthernetInterface",
47 "EthernetInterfaceCollection",
48 "Event",
49 "EventDestination",
50 "EventDestinationCollection",
51 "EventService",
Lakshmi Yadlapati71abefe2023-01-10 22:22:09 -060052 "FabricAdapter",
53 "FabricAdapterCollection",
George Liu1a7b3772022-09-29 09:29:18 +080054 "Fan",
55 "FanCollection",
Ed Tanous27747912022-09-23 12:50:08 -070056 "IPAddresses",
57 "JsonSchemaFile",
58 "JsonSchemaFileCollection", # redfish/v1/JsonSchemas
59 "LogEntry",
60 "LogEntryCollection",
61 "LogService",
62 "LogServiceCollection",
63 "Manager",
64 "ManagerAccount",
65 "ManagerAccountCollection",
66 "ManagerCollection",
67 "ManagerDiagnosticData",
68 "ManagerNetworkProtocol",
69 "Memory",
70 "MemoryCollection",
71 "Message",
72 "MessageRegistry",
73 "MessageRegistryCollection",
74 "MessageRegistryFile",
75 "MessageRegistryFileCollection",
76 "MetricDefinition",
77 "MetricDefinitionCollection",
78 "MetricReport",
79 "MetricReportCollection",
80 "MetricReportDefinition",
81 "MetricReportDefinitionCollection",
82 "OperatingConfig",
83 "OperatingConfigCollection",
84 "PCIeDevice",
85 "PCIeDeviceCollection",
86 "PCIeFunction",
87 "PCIeFunctionCollection",
88 "PhysicalContext",
89 "PCIeSlots",
90 "Power",
Chicago Duanfe9bd2d2022-09-30 18:03:05 +080091 "PowerSubsystem",
92 "PowerSupply",
93 "PowerSupplyCollection",
Ed Tanous27747912022-09-23 12:50:08 -070094 "Privileges", # Used in Role
95 "Processor",
96 "ProcessorCollection",
97 "RedfishError",
98 "RedfishExtensions",
99 "Redundancy",
100 "Resource",
101 "Role",
102 "RoleCollection",
103 "Sensor",
104 "SensorCollection",
105 "ServiceRoot",
106 "Session",
107 "SessionCollection",
108 "SessionService",
109 "Settings",
110 "SoftwareInventory",
111 "SoftwareInventoryCollection",
112 "Storage",
113 "StorageCollection",
114 "StorageController",
115 "StorageControllerCollection",
116 "Task",
117 "TaskCollection",
118 "TaskService",
119 "TelemetryService",
120 "Thermal",
George Liuf1240b42022-10-28 17:26:15 +0800121 "ThermalMetrics",
Ed Tanous27747912022-09-23 12:50:08 -0700122 "ThermalSubsystem",
123 "Triggers",
124 "TriggersCollection",
125 "UpdateService",
126 "VLanNetworkInterfaceCollection",
127 "VLanNetworkInterface",
128 "VirtualMedia",
129 "VirtualMediaCollection",
130 "odata",
131 "odata-v4",
132 "redfish-error",
133 "redfish-payload-annotations",
134 "redfish-schema",
135 "redfish-schema-v1",
Gunnar Mills349a2ac2021-01-20 22:29:16 -0600136]
137
Ed Tanous683f7272018-07-26 12:47:19 -0700138SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
139
Ed Tanous27747912022-09-23 12:50:08 -0700140proxies = {"https": os.environ.get("https_proxy", None)}
Ed Tanous683f7272018-07-26 12:47:19 -0700141
Ed Tanouscb103132019-10-08 11:34:22 -0700142r = requests.get(
Ed Tanous27747912022-09-23 12:50:08 -0700143 "https://www.dmtf.org/sites/default/files/standards/documents/"
144 + VERSION
145 + ".zip",
146 proxies=proxies,
147)
Ed Tanous683f7272018-07-26 12:47:19 -0700148
149r.raise_for_status()
150
Ed Tanous81d523a2022-05-25 12:00:51 -0700151
Ed Tanous27747912022-09-23 12:50:08 -0700152static_path = os.path.realpath(
153 os.path.join(SCRIPT_DIR, "..", "static", "redfish", "v1")
154)
Ed Tanous683f7272018-07-26 12:47:19 -0700155
Ed Tanous81d523a2022-05-25 12:00:51 -0700156
Ed Tanous27747912022-09-23 12:50:08 -0700157cpp_path = os.path.realpath(
158 os.path.join(SCRIPT_DIR, "..", "redfish-core", "include")
159)
Ed Tanous81d523a2022-05-25 12:00:51 -0700160
161
Ed Tanous683f7272018-07-26 12:47:19 -0700162schema_path = os.path.join(static_path, "schema")
163json_schema_path = os.path.join(static_path, "JsonSchemas")
Ed Tanous118b1c72018-09-13 13:45:51 -0700164metadata_index_path = os.path.join(static_path, "$metadata", "index.xml")
Ed Tanous683f7272018-07-26 12:47:19 -0700165
166zipBytesIO = BytesIO(r.content)
167zip_ref = zipfile.ZipFile(zipBytesIO)
168
Ed Tanous8b564552022-09-23 12:03:18 -0700169
Ed Tanous204c3762022-12-12 09:50:09 -0800170class SchemaVersion:
Patrick Williamsfd06b302022-12-12 10:39:42 -0600171 """
Ed Tanous204c3762022-12-12 09:50:09 -0800172 A Python class for sorting Redfish schema versions. Allows sorting Redfish
173 versions in the way humans expect, by comparing version strings as lists
174 (ie 0_2_0 comes before 0_10_0) in the way humans expect. It does case
175 insensitive schema name comparisons
Patrick Williamsfd06b302022-12-12 10:39:42 -0600176 """
Ed Tanous8b564552022-09-23 12:03:18 -0700177
Ed Tanous204c3762022-12-12 09:50:09 -0800178 def __init__(self, key):
179 key = str.casefold(key)
Ed Tanous8b564552022-09-23 12:03:18 -0700180
Ed Tanous204c3762022-12-12 09:50:09 -0800181 split_tup = key.split(".")
182 self.version_pieces = [split_tup[0]]
183 if len(split_tup) < 2:
184 return
185 version = split_tup[1]
Ed Tanous8b564552022-09-23 12:03:18 -0700186
Ed Tanous204c3762022-12-12 09:50:09 -0800187 if version.startswith("v"):
188 version = version[1:]
189 if any(char.isdigit() for char in version):
Patrick Williamsfd06b302022-12-12 10:39:42 -0600190 self.version_pieces.extend([int(x) for x in version.split("_")])
Ed Tanous8b564552022-09-23 12:03:18 -0700191
Ed Tanous204c3762022-12-12 09:50:09 -0800192 def __lt__(self, other):
193 return self.version_pieces < other.version_pieces
Ed Tanous8b564552022-09-23 12:03:18 -0700194
195
Ed Tanous683f7272018-07-26 12:47:19 -0700196# Remove the old files
Ed Tanous27747912022-09-23 12:50:08 -0700197skip_prefixes = "Oem"
Ed Tanous683f7272018-07-26 12:47:19 -0700198if os.path.exists(schema_path):
Ed Tanous27747912022-09-23 12:50:08 -0700199 files = [
200 os.path.join(schema_path, f)
201 for f in os.listdir(schema_path)
202 if not f.startswith(skip_prefixes)
203 ]
James Feistaee8d842018-09-10 16:07:40 -0700204 for f in files:
205 os.remove(f)
Ed Tanous683f7272018-07-26 12:47:19 -0700206if os.path.exists(json_schema_path):
Ed Tanous27747912022-09-23 12:50:08 -0700207 files = [
208 os.path.join(json_schema_path, f)
209 for f in os.listdir(json_schema_path)
210 if not f.startswith(skip_prefixes)
211 ]
Ed Tanous118b1c72018-09-13 13:45:51 -0700212 for f in files:
Ed Tanous27747912022-09-23 12:50:08 -0700213 if os.path.isfile(f):
Ed Tanous118b1c72018-09-13 13:45:51 -0700214 os.remove(f)
215 else:
216 shutil.rmtree(f)
Ed Tanous8b564552022-09-23 12:03:18 -0700217try:
218 os.remove(metadata_index_path)
219except FileNotFoundError:
220 pass
Ed Tanous683f7272018-07-26 12:47:19 -0700221
Ed Tanous118b1c72018-09-13 13:45:51 -0700222if not os.path.exists(schema_path):
223 os.makedirs(schema_path)
224if not os.path.exists(json_schema_path):
225 os.makedirs(json_schema_path)
Ed Tanous683f7272018-07-26 12:47:19 -0700226
Ed Tanous8b564552022-09-23 12:03:18 -0700227csdl_filenames = []
228json_schema_files = defaultdict(list)
229
Ed Tanous204c3762022-12-12 09:50:09 -0800230for zip_file in zip_ref.infolist():
231 if zip_file.is_dir():
232 continue
233 if zip_file.filename.startswith("csdl/"):
234 csdl_filenames.append(os.path.basename(zip_file.filename))
235 elif zip_file.filename.startswith("json-schema/"):
236 filename = os.path.basename(zip_file.filename)
Ed Tanous8b564552022-09-23 12:03:18 -0700237 filenamesplit = filename.split(".")
238 # exclude schemas again to save flash space
239 if filenamesplit[0] not in include_list:
240 continue
241 json_schema_files[filenamesplit[0]].append(filename)
Ed Tanous204c3762022-12-12 09:50:09 -0800242 elif zip_file.filename.startswith("openapi/"):
Ed Tanous8b564552022-09-23 12:03:18 -0700243 pass
Ed Tanous204c3762022-12-12 09:50:09 -0800244 elif zip_file.filename.startswith("dictionaries/"):
Ed Tanous8b564552022-09-23 12:03:18 -0700245 pass
246
247# sort the json files by version
248for key, value in json_schema_files.items():
Ed Tanous204c3762022-12-12 09:50:09 -0800249 value.sort(key=SchemaVersion, reverse=True)
Ed Tanous8b564552022-09-23 12:03:18 -0700250
251# Create a dictionary ordered by schema name
252json_schema_files = OrderedDict(
Ed Tanous204c3762022-12-12 09:50:09 -0800253 sorted(json_schema_files.items(), key=lambda x: SchemaVersion(x[0]))
Ed Tanous8b564552022-09-23 12:03:18 -0700254)
255
Ed Tanous204c3762022-12-12 09:50:09 -0800256csdl_filenames.sort(key=SchemaVersion)
Ed Tanous27747912022-09-23 12:50:08 -0700257with open(metadata_index_path, "w") as metadata_index:
Ed Tanous27747912022-09-23 12:50:08 -0700258 metadata_index.write('<?xml version="1.0" encoding="UTF-8"?>\n')
Ed Tanous118b1c72018-09-13 13:45:51 -0700259 metadata_index.write(
Ed Tanousf395daa2021-08-02 08:56:24 -0700260 "<edmx:Edmx xmlns:edmx="
Ed Tanous27747912022-09-23 12:50:08 -0700261 '"http://docs.oasis-open.org/odata/ns/edmx"'
262 ' Version="4.0">\n'
263 )
Ed Tanous118b1c72018-09-13 13:45:51 -0700264
Ed Tanous8b564552022-09-23 12:03:18 -0700265 for filename in csdl_filenames:
266 # filename looks like Zone_v1.xml
Ed Tanous8b564552022-09-23 12:03:18 -0700267 with open(os.path.join(schema_path, filename), "wb") as schema_out:
Ed Tanous853c0dc2022-12-21 13:21:52 -0800268 content = zip_ref.read(os.path.join("csdl", filename))
269 content = content.replace(b"\r\n", b"\n")
270
271 schema_out.write(content)
272
273 filenamesplit = filename.split("_")
274 if filenamesplit[0] not in include_list:
275 continue
Ed Tanous8b564552022-09-23 12:03:18 -0700276 metadata_index.write(
277 ' <edmx:Reference Uri="/redfish/v1/schema/'
278 + filename
279 + '">\n'
280 )
Ed Tanous118b1c72018-09-13 13:45:51 -0700281
Ed Tanous8b564552022-09-23 12:03:18 -0700282 xml_root = ET.fromstring(content)
283 edmx = "{http://docs.oasis-open.org/odata/ns/edmx}"
284 edm = "{http://docs.oasis-open.org/odata/ns/edm}"
285 for edmx_child in xml_root:
286 if edmx_child.tag == edmx + "DataServices":
287 for data_child in edmx_child:
288 if data_child.tag == edm + "Schema":
289 namespace = data_child.attrib["Namespace"]
290 if namespace.startswith("RedfishExtensions"):
291 metadata_index.write(
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600292 ' <edmx:Include Namespace="'
Ed Tanous8b564552022-09-23 12:03:18 -0700293 + namespace
294 + '" Alias="Redfish"/>\n'
295 )
Ed Tanous118b1c72018-09-13 13:45:51 -0700296
Ed Tanous8b564552022-09-23 12:03:18 -0700297 else:
298 metadata_index.write(
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600299 ' <edmx:Include Namespace="'
Ed Tanous8b564552022-09-23 12:03:18 -0700300 + namespace
301 + '"/>\n'
302 )
Ed Tanous8b564552022-09-23 12:03:18 -0700303 metadata_index.write(" </edmx:Reference>\n")
Ed Tanous118b1c72018-09-13 13:45:51 -0700304
Ed Tanous27747912022-09-23 12:50:08 -0700305 metadata_index.write(
306 " <edmx:DataServices>\n"
307 " <Schema "
308 'xmlns="http://docs.oasis-open.org/odata/ns/edm" '
309 'Namespace="Service">\n'
310 ' <EntityContainer Name="Service" '
311 'Extends="ServiceRoot.v1_0_0.ServiceContainer"/>\n'
312 " </Schema>\n"
313 " </edmx:DataServices>\n"
314 )
Ed Tanouscb103132019-10-08 11:34:22 -0700315 # TODO:Issue#32 There's a bug in the script that currently deletes this
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500316 # schema (because it's an OEM schema). Because it's the only six, and we
Ed Tanouscb103132019-10-08 11:34:22 -0700317 # don't update schemas very often, we just manually fix it. Need a
318 # permanent fix to the script.
319 metadata_index.write(
Ed Tanous27747912022-09-23 12:50:08 -0700320 ' <edmx:Reference Uri="/redfish/v1/schema/OemManager_v1.xml">\n'
321 )
322 metadata_index.write(' <edmx:Include Namespace="OemManager"/>\n')
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600323 metadata_index.write(" </edmx:Reference>\n")
Gunnar Mills20778992020-02-06 16:36:47 -0600324
325 metadata_index.write(
Ed Tanous27747912022-09-23 12:50:08 -0700326 ' <edmx:Reference Uri="'
327 '/redfish/v1/schema/OemComputerSystem_v1.xml">\n'
328 )
Ed Tanousf395daa2021-08-02 08:56:24 -0700329 metadata_index.write(
Ed Tanous27747912022-09-23 12:50:08 -0700330 ' <edmx:Include Namespace="OemComputerSystem"/>\n'
331 )
Gunnar Mills20778992020-02-06 16:36:47 -0600332 metadata_index.write(" </edmx:Reference>\n")
333
334 metadata_index.write(
Ed Tanous27747912022-09-23 12:50:08 -0700335 ' <edmx:Reference Uri="'
336 '/redfish/v1/schema/OemVirtualMedia_v1.xml">\n'
337 )
Ed Tanousf395daa2021-08-02 08:56:24 -0700338 metadata_index.write(
Ed Tanous27747912022-09-23 12:50:08 -0700339 ' <edmx:Include Namespace="OemVirtualMedia"/>\n'
340 )
Ed Tanousf395daa2021-08-02 08:56:24 -0700341 metadata_index.write(
Ed Tanous27747912022-09-23 12:50:08 -0700342 ' <edmx:Include Namespace="OemVirtualMedia.v1_0_0"/>\n'
343 )
Gunnar Mills20778992020-02-06 16:36:47 -0600344 metadata_index.write(" </edmx:Reference>\n")
345
346 metadata_index.write(
Ed Tanous27747912022-09-23 12:50:08 -0700347 ' <edmx:Reference Uri="'
348 '/redfish/v1/schema/OemAccountService_v1.xml">\n'
349 )
Ed Tanousf395daa2021-08-02 08:56:24 -0700350 metadata_index.write(
Ed Tanous27747912022-09-23 12:50:08 -0700351 ' <edmx:Include Namespace="OemAccountService"/>\n'
352 )
Ed Tanousf395daa2021-08-02 08:56:24 -0700353 metadata_index.write(
Ed Tanous27747912022-09-23 12:50:08 -0700354 ' <edmx:Include Namespace="OemAccountService.v1_0_0"/>\n'
355 )
Gunnar Mills20778992020-02-06 16:36:47 -0600356 metadata_index.write(" </edmx:Reference>\n")
357
Ravi Tejae7d68c32020-03-15 13:30:41 -0500358 metadata_index.write(
Ed Tanous27747912022-09-23 12:50:08 -0700359 ' <edmx:Reference Uri="/redfish/v1/schema/OemSession_v1.xml">\n'
360 )
361 metadata_index.write(' <edmx:Include Namespace="OemSession"/>\n')
Ed Tanousf395daa2021-08-02 08:56:24 -0700362 metadata_index.write(
Ed Tanous27747912022-09-23 12:50:08 -0700363 ' <edmx:Include Namespace="OemSession.v1_0_0"/>\n'
364 )
Sunitha Harish9dc50742020-05-11 00:10:20 -0500365 metadata_index.write(" </edmx:Reference>\n")
366
Ed Tanous118b1c72018-09-13 13:45:51 -0700367 metadata_index.write("</edmx:Edmx>\n")
Ed Tanous683f7272018-07-26 12:47:19 -0700368
Gunnar Mills349a2ac2021-01-20 22:29:16 -0600369
Ed Tanous8b564552022-09-23 12:03:18 -0700370for schema, version in json_schema_files.items():
371 zip_filepath = os.path.join("json-schema", version[0])
Ed Tanous683f7272018-07-26 12:47:19 -0700372 schemadir = os.path.join(json_schema_path, schema)
373 os.makedirs(schemadir)
Ed Tanous118b1c72018-09-13 13:45:51 -0700374
Ed Tanous27747912022-09-23 12:50:08 -0700375 with open(os.path.join(schemadir, schema + ".json"), "wb") as schema_file:
376 schema_file.write(zip_ref.read(zip_filepath).replace(b"\r\n", b"\n"))
Ed Tanous683f7272018-07-26 12:47:19 -0700377
Ed Tanous27747912022-09-23 12:50:08 -0700378with open(os.path.join(cpp_path, "schemas.hpp"), "w") as hpp_file:
Ed Tanous81d523a2022-05-25 12:00:51 -0700379 hpp_file.write(
380 "#pragma once\n"
381 "{WARNING}\n"
382 "// clang-format off\n"
Ed Tanous3d69fed2022-09-26 20:10:42 -0700383 "#include <array>\n"
Ed Tanous81d523a2022-05-25 12:00:51 -0700384 "\n"
385 "namespace redfish\n"
386 "{{\n"
Ed Tanous27747912022-09-23 12:50:08 -0700387 " constexpr std::array schemas {{\n".format(WARNING=WARNING)
Ed Tanous81d523a2022-05-25 12:00:51 -0700388 )
Ed Tanous8b564552022-09-23 12:03:18 -0700389 for schema_file in json_schema_files:
Ed Tanous27747912022-09-23 12:50:08 -0700390 hpp_file.write(' "{}",\n'.format(schema_file))
391
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600392 hpp_file.write(" };\n}\n")
Ed Tanous683f7272018-07-26 12:47:19 -0700393
394zip_ref.close()
Ed Tanous0ec8b832022-03-14 14:56:47 -0700395
396generate_schema_enums.main()
Carson Labrado9e031402022-07-08 20:56:52 +0000397generate_top_collections()
Ed Tanous853c0dc2022-12-21 13:21:52 -0800398
399# Now delete the xml schema files we aren't supporting
400if os.path.exists(schema_path):
401 files = [
402 os.path.join(schema_path, f)
403 for f in os.listdir(schema_path)
404 if not f.startswith(skip_prefixes)
405 ]
406 for filename in files:
407 # filename will include the absolute path
408 filenamesplit = filename.split("/")
409 name = filenamesplit.pop()
410 namesplit = name.split("_")
411 if namesplit[0] not in include_list:
412 print("excluding schema: " + filename)
413 os.remove(filename)