blob: da7c3acc6ea5da980e5565d39e867d78e51df713 [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
Ed Tanousa8d8f9d2023-01-26 13:57:00 -080013VERSION = "DSP8010_2022.3"
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",
George Liu7da1c582023-02-21 14:38:49 +080090 "Port",
91 "PortCollection",
Ed Tanous27747912022-09-23 12:50:08 -070092 "Power",
Chicago Duanfe9bd2d2022-09-30 18:03:05 +080093 "PowerSubsystem",
94 "PowerSupply",
95 "PowerSupplyCollection",
Ed Tanous27747912022-09-23 12:50:08 -070096 "Privileges", # Used in Role
97 "Processor",
98 "ProcessorCollection",
99 "RedfishError",
100 "RedfishExtensions",
101 "Redundancy",
102 "Resource",
103 "Role",
104 "RoleCollection",
105 "Sensor",
106 "SensorCollection",
107 "ServiceRoot",
108 "Session",
109 "SessionCollection",
110 "SessionService",
111 "Settings",
112 "SoftwareInventory",
113 "SoftwareInventoryCollection",
114 "Storage",
115 "StorageCollection",
116 "StorageController",
117 "StorageControllerCollection",
118 "Task",
119 "TaskCollection",
120 "TaskService",
121 "TelemetryService",
122 "Thermal",
George Liuf1240b42022-10-28 17:26:15 +0800123 "ThermalMetrics",
Ed Tanous27747912022-09-23 12:50:08 -0700124 "ThermalSubsystem",
125 "Triggers",
126 "TriggersCollection",
127 "UpdateService",
128 "VLanNetworkInterfaceCollection",
129 "VLanNetworkInterface",
130 "VirtualMedia",
131 "VirtualMediaCollection",
132 "odata",
133 "odata-v4",
134 "redfish-error",
135 "redfish-payload-annotations",
136 "redfish-schema",
137 "redfish-schema-v1",
Gunnar Mills349a2ac2021-01-20 22:29:16 -0600138]
139
Ed Tanous683f7272018-07-26 12:47:19 -0700140SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
141
Ed Tanous27747912022-09-23 12:50:08 -0700142proxies = {"https": os.environ.get("https_proxy", None)}
Ed Tanous683f7272018-07-26 12:47:19 -0700143
Ed Tanouscb103132019-10-08 11:34:22 -0700144r = requests.get(
Ed Tanous27747912022-09-23 12:50:08 -0700145 "https://www.dmtf.org/sites/default/files/standards/documents/"
146 + VERSION
147 + ".zip",
148 proxies=proxies,
149)
Ed Tanous683f7272018-07-26 12:47:19 -0700150
151r.raise_for_status()
152
Ed Tanous81d523a2022-05-25 12:00:51 -0700153
Ed Tanous27747912022-09-23 12:50:08 -0700154static_path = os.path.realpath(
155 os.path.join(SCRIPT_DIR, "..", "static", "redfish", "v1")
156)
Ed Tanous683f7272018-07-26 12:47:19 -0700157
Ed Tanous81d523a2022-05-25 12:00:51 -0700158
Ed Tanous27747912022-09-23 12:50:08 -0700159cpp_path = os.path.realpath(
160 os.path.join(SCRIPT_DIR, "..", "redfish-core", "include")
161)
Ed Tanous81d523a2022-05-25 12:00:51 -0700162
163
Ed Tanous683f7272018-07-26 12:47:19 -0700164schema_path = os.path.join(static_path, "schema")
165json_schema_path = os.path.join(static_path, "JsonSchemas")
Ed Tanous118b1c72018-09-13 13:45:51 -0700166metadata_index_path = os.path.join(static_path, "$metadata", "index.xml")
Ed Tanous683f7272018-07-26 12:47:19 -0700167
168zipBytesIO = BytesIO(r.content)
169zip_ref = zipfile.ZipFile(zipBytesIO)
170
Ed Tanous8b564552022-09-23 12:03:18 -0700171
Ed Tanous204c3762022-12-12 09:50:09 -0800172class SchemaVersion:
Patrick Williamsfd06b302022-12-12 10:39:42 -0600173 """
Ed Tanous204c3762022-12-12 09:50:09 -0800174 A Python class for sorting Redfish schema versions. Allows sorting Redfish
175 versions in the way humans expect, by comparing version strings as lists
176 (ie 0_2_0 comes before 0_10_0) in the way humans expect. It does case
177 insensitive schema name comparisons
Patrick Williamsfd06b302022-12-12 10:39:42 -0600178 """
Ed Tanous8b564552022-09-23 12:03:18 -0700179
Ed Tanous204c3762022-12-12 09:50:09 -0800180 def __init__(self, key):
181 key = str.casefold(key)
Ed Tanous8b564552022-09-23 12:03:18 -0700182
Ed Tanous204c3762022-12-12 09:50:09 -0800183 split_tup = key.split(".")
184 self.version_pieces = [split_tup[0]]
185 if len(split_tup) < 2:
186 return
187 version = split_tup[1]
Ed Tanous8b564552022-09-23 12:03:18 -0700188
Ed Tanous204c3762022-12-12 09:50:09 -0800189 if version.startswith("v"):
190 version = version[1:]
191 if any(char.isdigit() for char in version):
Patrick Williamsfd06b302022-12-12 10:39:42 -0600192 self.version_pieces.extend([int(x) for x in version.split("_")])
Ed Tanous8b564552022-09-23 12:03:18 -0700193
Ed Tanous204c3762022-12-12 09:50:09 -0800194 def __lt__(self, other):
195 return self.version_pieces < other.version_pieces
Ed Tanous8b564552022-09-23 12:03:18 -0700196
197
Ed Tanous683f7272018-07-26 12:47:19 -0700198# Remove the old files
Ed Tanous5b5574a2022-09-26 19:53:36 -0700199skip_prefixes = ["Oem", "OpenBMC"]
Ed Tanous683f7272018-07-26 12:47:19 -0700200if os.path.exists(schema_path):
Ed Tanous27747912022-09-23 12:50:08 -0700201 files = [
202 os.path.join(schema_path, f)
203 for f in os.listdir(schema_path)
Ed Tanous5b5574a2022-09-26 19:53:36 -0700204 if not any([f.startswith(prefix) for prefix in skip_prefixes])
Ed Tanous27747912022-09-23 12:50:08 -0700205 ]
James Feistaee8d842018-09-10 16:07:40 -0700206 for f in files:
207 os.remove(f)
Ed Tanous683f7272018-07-26 12:47:19 -0700208if os.path.exists(json_schema_path):
Ed Tanous27747912022-09-23 12:50:08 -0700209 files = [
210 os.path.join(json_schema_path, f)
211 for f in os.listdir(json_schema_path)
Ed Tanous5b5574a2022-09-26 19:53:36 -0700212 if not any([f.startswith(prefix) for prefix in skip_prefixes])
Ed Tanous27747912022-09-23 12:50:08 -0700213 ]
Ed Tanous118b1c72018-09-13 13:45:51 -0700214 for f in files:
Ed Tanous27747912022-09-23 12:50:08 -0700215 if os.path.isfile(f):
Ed Tanous118b1c72018-09-13 13:45:51 -0700216 os.remove(f)
217 else:
218 shutil.rmtree(f)
Ed Tanous8b564552022-09-23 12:03:18 -0700219try:
220 os.remove(metadata_index_path)
221except FileNotFoundError:
222 pass
Ed Tanous683f7272018-07-26 12:47:19 -0700223
Ed Tanous118b1c72018-09-13 13:45:51 -0700224if not os.path.exists(schema_path):
225 os.makedirs(schema_path)
226if not os.path.exists(json_schema_path):
227 os.makedirs(json_schema_path)
Ed Tanous683f7272018-07-26 12:47:19 -0700228
Ed Tanous8b564552022-09-23 12:03:18 -0700229csdl_filenames = []
230json_schema_files = defaultdict(list)
231
Ed Tanous204c3762022-12-12 09:50:09 -0800232for zip_file in zip_ref.infolist():
233 if zip_file.is_dir():
234 continue
235 if zip_file.filename.startswith("csdl/"):
236 csdl_filenames.append(os.path.basename(zip_file.filename))
237 elif zip_file.filename.startswith("json-schema/"):
238 filename = os.path.basename(zip_file.filename)
Ed Tanous8b564552022-09-23 12:03:18 -0700239 filenamesplit = filename.split(".")
240 # exclude schemas again to save flash space
241 if filenamesplit[0] not in include_list:
242 continue
243 json_schema_files[filenamesplit[0]].append(filename)
Ed Tanous204c3762022-12-12 09:50:09 -0800244 elif zip_file.filename.startswith("openapi/"):
Ed Tanous8b564552022-09-23 12:03:18 -0700245 pass
Ed Tanous204c3762022-12-12 09:50:09 -0800246 elif zip_file.filename.startswith("dictionaries/"):
Ed Tanous8b564552022-09-23 12:03:18 -0700247 pass
248
249# sort the json files by version
250for key, value in json_schema_files.items():
Ed Tanous204c3762022-12-12 09:50:09 -0800251 value.sort(key=SchemaVersion, reverse=True)
Ed Tanous8b564552022-09-23 12:03:18 -0700252
253# Create a dictionary ordered by schema name
254json_schema_files = OrderedDict(
Ed Tanous204c3762022-12-12 09:50:09 -0800255 sorted(json_schema_files.items(), key=lambda x: SchemaVersion(x[0]))
Ed Tanous8b564552022-09-23 12:03:18 -0700256)
257
Ed Tanous204c3762022-12-12 09:50:09 -0800258csdl_filenames.sort(key=SchemaVersion)
Ed Tanous27747912022-09-23 12:50:08 -0700259with open(metadata_index_path, "w") as metadata_index:
Ed Tanous27747912022-09-23 12:50:08 -0700260 metadata_index.write('<?xml version="1.0" encoding="UTF-8"?>\n')
Ed Tanous118b1c72018-09-13 13:45:51 -0700261 metadata_index.write(
Ed Tanousf395daa2021-08-02 08:56:24 -0700262 "<edmx:Edmx xmlns:edmx="
Ed Tanous27747912022-09-23 12:50:08 -0700263 '"http://docs.oasis-open.org/odata/ns/edmx"'
264 ' Version="4.0">\n'
265 )
Ed Tanous118b1c72018-09-13 13:45:51 -0700266
Ed Tanous8b564552022-09-23 12:03:18 -0700267 for filename in csdl_filenames:
268 # filename looks like Zone_v1.xml
Ed Tanous8b564552022-09-23 12:03:18 -0700269 with open(os.path.join(schema_path, filename), "wb") as schema_out:
Ed Tanous853c0dc2022-12-21 13:21:52 -0800270 content = zip_ref.read(os.path.join("csdl", filename))
271 content = content.replace(b"\r\n", b"\n")
272
273 schema_out.write(content)
274
275 filenamesplit = filename.split("_")
276 if filenamesplit[0] not in include_list:
277 continue
Ed Tanous8b564552022-09-23 12:03:18 -0700278 metadata_index.write(
279 ' <edmx:Reference Uri="/redfish/v1/schema/'
280 + filename
281 + '">\n'
282 )
Ed Tanous118b1c72018-09-13 13:45:51 -0700283
Ed Tanous8b564552022-09-23 12:03:18 -0700284 xml_root = ET.fromstring(content)
285 edmx = "{http://docs.oasis-open.org/odata/ns/edmx}"
286 edm = "{http://docs.oasis-open.org/odata/ns/edm}"
287 for edmx_child in xml_root:
288 if edmx_child.tag == edmx + "DataServices":
289 for data_child in edmx_child:
290 if data_child.tag == edm + "Schema":
291 namespace = data_child.attrib["Namespace"]
292 if namespace.startswith("RedfishExtensions"):
293 metadata_index.write(
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600294 ' <edmx:Include Namespace="'
Ed Tanous8b564552022-09-23 12:03:18 -0700295 + namespace
296 + '" Alias="Redfish"/>\n'
297 )
Ed Tanous118b1c72018-09-13 13:45:51 -0700298
Ed Tanous8b564552022-09-23 12:03:18 -0700299 else:
300 metadata_index.write(
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600301 ' <edmx:Include Namespace="'
Ed Tanous8b564552022-09-23 12:03:18 -0700302 + namespace
303 + '"/>\n'
304 )
Ed Tanous8b564552022-09-23 12:03:18 -0700305 metadata_index.write(" </edmx:Reference>\n")
Ed Tanous118b1c72018-09-13 13:45:51 -0700306
Ed Tanous27747912022-09-23 12:50:08 -0700307 metadata_index.write(
308 " <edmx:DataServices>\n"
309 " <Schema "
310 'xmlns="http://docs.oasis-open.org/odata/ns/edm" '
311 'Namespace="Service">\n'
312 ' <EntityContainer Name="Service" '
313 'Extends="ServiceRoot.v1_0_0.ServiceContainer"/>\n'
314 " </Schema>\n"
315 " </edmx:DataServices>\n"
316 )
Ed Tanouscb103132019-10-08 11:34:22 -0700317 # TODO:Issue#32 There's a bug in the script that currently deletes this
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500318 # schema (because it's an OEM schema). Because it's the only six, and we
Ed Tanouscb103132019-10-08 11:34:22 -0700319 # don't update schemas very often, we just manually fix it. Need a
320 # permanent fix to the script.
321 metadata_index.write(
Ed Tanous27747912022-09-23 12:50:08 -0700322 ' <edmx:Reference Uri="/redfish/v1/schema/OemManager_v1.xml">\n'
323 )
324 metadata_index.write(' <edmx:Include Namespace="OemManager"/>\n')
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600325 metadata_index.write(" </edmx:Reference>\n")
Gunnar Mills20778992020-02-06 16:36:47 -0600326
327 metadata_index.write(
Ed Tanous27747912022-09-23 12:50:08 -0700328 ' <edmx:Reference Uri="'
329 '/redfish/v1/schema/OemComputerSystem_v1.xml">\n'
330 )
Ed Tanousf395daa2021-08-02 08:56:24 -0700331 metadata_index.write(
Ed Tanous27747912022-09-23 12:50:08 -0700332 ' <edmx:Include Namespace="OemComputerSystem"/>\n'
333 )
Gunnar Mills20778992020-02-06 16:36:47 -0600334 metadata_index.write(" </edmx:Reference>\n")
335
336 metadata_index.write(
Ed Tanous27747912022-09-23 12:50:08 -0700337 ' <edmx:Reference Uri="'
338 '/redfish/v1/schema/OemVirtualMedia_v1.xml">\n'
339 )
Ed Tanousf395daa2021-08-02 08:56:24 -0700340 metadata_index.write(
Ed Tanous27747912022-09-23 12:50:08 -0700341 ' <edmx:Include Namespace="OemVirtualMedia"/>\n'
342 )
Ed Tanousf395daa2021-08-02 08:56:24 -0700343 metadata_index.write(
Ed Tanous27747912022-09-23 12:50:08 -0700344 ' <edmx:Include Namespace="OemVirtualMedia.v1_0_0"/>\n'
345 )
Gunnar Mills20778992020-02-06 16:36:47 -0600346 metadata_index.write(" </edmx:Reference>\n")
347
348 metadata_index.write(
Ed Tanous27747912022-09-23 12:50:08 -0700349 ' <edmx:Reference Uri="'
Ed Tanous5d3a59d2023-02-13 10:24:04 -0800350 '/redfish/v1/schema/OpenBMCAccountService_v1.xml">\n'
Ed Tanous27747912022-09-23 12:50:08 -0700351 )
Ed Tanousf395daa2021-08-02 08:56:24 -0700352 metadata_index.write(
Ed Tanous5c5804d2023-02-13 13:35:04 -0800353 ' <edmx:Include Namespace="OpenBMCAccountService"/>\n'
Ed Tanous27747912022-09-23 12:50:08 -0700354 )
Ed Tanousf395daa2021-08-02 08:56:24 -0700355 metadata_index.write(
Ed Tanous5c5804d2023-02-13 13:35:04 -0800356 ' <edmx:Include Namespace="OpenBMCAccountService.v1_0_0"/>\n'
Ed Tanous27747912022-09-23 12:50:08 -0700357 )
Gunnar Mills20778992020-02-06 16:36:47 -0600358 metadata_index.write(" </edmx:Reference>\n")
359
Ed Tanous118b1c72018-09-13 13:45:51 -0700360 metadata_index.write("</edmx:Edmx>\n")
Ed Tanous683f7272018-07-26 12:47:19 -0700361
Gunnar Mills349a2ac2021-01-20 22:29:16 -0600362
Ed Tanous8b564552022-09-23 12:03:18 -0700363for schema, version in json_schema_files.items():
364 zip_filepath = os.path.join("json-schema", version[0])
Ed Tanous683f7272018-07-26 12:47:19 -0700365 schemadir = os.path.join(json_schema_path, schema)
366 os.makedirs(schemadir)
Ed Tanous118b1c72018-09-13 13:45:51 -0700367
Ed Tanous27747912022-09-23 12:50:08 -0700368 with open(os.path.join(schemadir, schema + ".json"), "wb") as schema_file:
369 schema_file.write(zip_ref.read(zip_filepath).replace(b"\r\n", b"\n"))
Ed Tanous683f7272018-07-26 12:47:19 -0700370
Ed Tanous27747912022-09-23 12:50:08 -0700371with open(os.path.join(cpp_path, "schemas.hpp"), "w") as hpp_file:
Ed Tanous81d523a2022-05-25 12:00:51 -0700372 hpp_file.write(
373 "#pragma once\n"
374 "{WARNING}\n"
375 "// clang-format off\n"
Ed Tanous3d69fed2022-09-26 20:10:42 -0700376 "#include <array>\n"
Ed Tanous81d523a2022-05-25 12:00:51 -0700377 "\n"
378 "namespace redfish\n"
379 "{{\n"
Ed Tanous27747912022-09-23 12:50:08 -0700380 " constexpr std::array schemas {{\n".format(WARNING=WARNING)
Ed Tanous81d523a2022-05-25 12:00:51 -0700381 )
Ed Tanous8b564552022-09-23 12:03:18 -0700382 for schema_file in json_schema_files:
Ed Tanous27747912022-09-23 12:50:08 -0700383 hpp_file.write(' "{}",\n'.format(schema_file))
384
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600385 hpp_file.write(" };\n}\n")
Ed Tanous683f7272018-07-26 12:47:19 -0700386
387zip_ref.close()
Ed Tanous0ec8b832022-03-14 14:56:47 -0700388
389generate_schema_enums.main()
Carson Labrado9e031402022-07-08 20:56:52 +0000390generate_top_collections()
Ed Tanous853c0dc2022-12-21 13:21:52 -0800391
392# Now delete the xml schema files we aren't supporting
393if os.path.exists(schema_path):
394 files = [
395 os.path.join(schema_path, f)
396 for f in os.listdir(schema_path)
Ed Tanous5b5574a2022-09-26 19:53:36 -0700397 if not any([f.startswith(prefix) for prefix in skip_prefixes])
Ed Tanous853c0dc2022-12-21 13:21:52 -0800398 ]
399 for filename in files:
400 # filename will include the absolute path
401 filenamesplit = filename.split("/")
402 name = filenamesplit.pop()
403 namesplit = name.split("_")
404 if namesplit[0] not in include_list:
405 print("excluding schema: " + filename)
406 os.remove(filename)