blob: f0d70b892049dd0bed068838c8a598abdfea252d [file] [log] [blame]
Nan Zhou313c1b72022-03-25 11:47:55 -07001#!/usr/bin/env python3
Ed Tanous683f7272018-07-26 12:47:19 -07002import requests
3import zipfile
4from io import BytesIO
Ed Tanous8b564552022-09-23 12:03:18 -07005from packaging.version import Version, parse
6
Ed Tanous683f7272018-07-26 12:47:19 -07007import os
Ed Tanous8b564552022-09-23 12:03:18 -07008from collections import OrderedDict, defaultdict
Ed Tanous683f7272018-07-26 12:47:19 -07009import shutil
10import json
11
Ed Tanous118b1c72018-09-13 13:45:51 -070012import xml.etree.ElementTree as ET
13
Asmitha Karunanithi009c6452022-09-22 01:07:59 -050014VERSION = "DSP8010_2022.2"
Ed Tanouscb103132019-10-08 11:34:22 -070015
Ed Tanous27747912022-09-23 12:50:08 -070016WARNING = """/****************************************************************
Ed Tanous81d523a2022-05-25 12:00:51 -070017 * READ THIS WARNING FIRST
18 * This is an auto-generated header which contains definitions
19 * for Redfish DMTF defined schemas.
20 * DO NOT modify this registry outside of running the
21 * update_schemas.py script. The definitions contained within
22 * this file are owned by DMTF. Any modifications to these files
23 * should be first pushed to the relevant registry in the DMTF
24 * github organization.
Ed Tanous27747912022-09-23 12:50:08 -070025 ***************************************************************/"""
Ed Tanous81d523a2022-05-25 12:00:51 -070026
Gunnar Mills349a2ac2021-01-20 22:29:16 -060027# To use a new schema, add to list and rerun tool
28include_list = [
Ed Tanous27747912022-09-23 12:50:08 -070029 "AccountService",
30 "ActionInfo",
31 "Assembly",
32 "AttributeRegistry",
33 "Bios",
34 "Cable",
35 "CableCollection",
36 "Certificate",
37 "CertificateCollection",
38 "CertificateLocations",
39 "CertificateService",
40 "Chassis",
41 "ChassisCollection",
42 "ComputerSystem",
43 "ComputerSystemCollection",
44 "Drive",
45 "DriveCollection",
George Liubf7e67e2022-10-06 09:19:46 +080046 'EnvironmentMetrics',
Ed Tanous27747912022-09-23 12:50:08 -070047 "EthernetInterface",
48 "EthernetInterfaceCollection",
49 "Event",
50 "EventDestination",
51 "EventDestinationCollection",
52 "EventService",
George Liu1a7b3772022-09-29 09:29:18 +080053 "Fan",
54 "FanCollection",
Ed Tanous27747912022-09-23 12:50:08 -070055 "IPAddresses",
56 "JsonSchemaFile",
57 "JsonSchemaFileCollection", # redfish/v1/JsonSchemas
58 "LogEntry",
59 "LogEntryCollection",
60 "LogService",
61 "LogServiceCollection",
62 "Manager",
63 "ManagerAccount",
64 "ManagerAccountCollection",
65 "ManagerCollection",
66 "ManagerDiagnosticData",
67 "ManagerNetworkProtocol",
68 "Memory",
69 "MemoryCollection",
70 "Message",
71 "MessageRegistry",
72 "MessageRegistryCollection",
73 "MessageRegistryFile",
74 "MessageRegistryFileCollection",
75 "MetricDefinition",
76 "MetricDefinitionCollection",
77 "MetricReport",
78 "MetricReportCollection",
79 "MetricReportDefinition",
80 "MetricReportDefinitionCollection",
81 "OperatingConfig",
82 "OperatingConfigCollection",
83 "PCIeDevice",
84 "PCIeDeviceCollection",
85 "PCIeFunction",
86 "PCIeFunctionCollection",
87 "PhysicalContext",
88 "PCIeSlots",
89 "Power",
Chicago Duanfe9bd2d2022-09-30 18:03:05 +080090 "PowerSubsystem",
91 "PowerSupply",
92 "PowerSupplyCollection",
Ed Tanous27747912022-09-23 12:50:08 -070093 "Privileges", # Used in Role
94 "Processor",
95 "ProcessorCollection",
96 "RedfishError",
97 "RedfishExtensions",
98 "Redundancy",
99 "Resource",
100 "Role",
101 "RoleCollection",
102 "Sensor",
103 "SensorCollection",
104 "ServiceRoot",
105 "Session",
106 "SessionCollection",
107 "SessionService",
108 "Settings",
109 "SoftwareInventory",
110 "SoftwareInventoryCollection",
111 "Storage",
112 "StorageCollection",
113 "StorageController",
114 "StorageControllerCollection",
115 "Task",
116 "TaskCollection",
117 "TaskService",
118 "TelemetryService",
119 "Thermal",
120 "ThermalSubsystem",
121 "Triggers",
122 "TriggersCollection",
123 "UpdateService",
124 "VLanNetworkInterfaceCollection",
125 "VLanNetworkInterface",
126 "VirtualMedia",
127 "VirtualMediaCollection",
128 "odata",
129 "odata-v4",
130 "redfish-error",
131 "redfish-payload-annotations",
132 "redfish-schema",
133 "redfish-schema-v1",
Gunnar Mills349a2ac2021-01-20 22:29:16 -0600134]
135
Ed Tanous683f7272018-07-26 12:47:19 -0700136SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
137
Ed Tanous27747912022-09-23 12:50:08 -0700138proxies = {"https": os.environ.get("https_proxy", None)}
Ed Tanous683f7272018-07-26 12:47:19 -0700139
Ed Tanouscb103132019-10-08 11:34:22 -0700140r = requests.get(
Ed Tanous27747912022-09-23 12:50:08 -0700141 "https://www.dmtf.org/sites/default/files/standards/documents/"
142 + VERSION
143 + ".zip",
144 proxies=proxies,
145)
Ed Tanous683f7272018-07-26 12:47:19 -0700146
147r.raise_for_status()
148
Ed Tanous81d523a2022-05-25 12:00:51 -0700149
Ed Tanous27747912022-09-23 12:50:08 -0700150static_path = os.path.realpath(
151 os.path.join(SCRIPT_DIR, "..", "static", "redfish", "v1")
152)
Ed Tanous683f7272018-07-26 12:47:19 -0700153
Ed Tanous81d523a2022-05-25 12:00:51 -0700154
Ed Tanous27747912022-09-23 12:50:08 -0700155cpp_path = os.path.realpath(
156 os.path.join(SCRIPT_DIR, "..", "redfish-core", "include")
157)
Ed Tanous81d523a2022-05-25 12:00:51 -0700158
159
Ed Tanous683f7272018-07-26 12:47:19 -0700160schema_path = os.path.join(static_path, "schema")
161json_schema_path = os.path.join(static_path, "JsonSchemas")
Ed Tanous118b1c72018-09-13 13:45:51 -0700162metadata_index_path = os.path.join(static_path, "$metadata", "index.xml")
Ed Tanous683f7272018-07-26 12:47:19 -0700163
164zipBytesIO = BytesIO(r.content)
165zip_ref = zipfile.ZipFile(zipBytesIO)
166
Ed Tanous8b564552022-09-23 12:03:18 -0700167
168def version_sort_key(key):
169 """
170 Method that computes a sort key that zero pads all numbers, such that
171 version sorting like
172 0_2_0
173 0_10_0
174 sorts in the way humans expect.
175 it also does case insensitive comparisons.
176 """
177 key = str.casefold(key)
178
179 # Decription of this class calls it "Version numbering for anarchists and
180 # software realists.". That seems like exactly what we need here.
181
182 if not any(char.isdigit() for char in key):
183 split_tup = os.path.splitext(key)
184 key = split_tup[0] + ".v0_0_0" + split_tup[1]
185
186 # special case some files that don't seem to follow the naming convention,
187 # and cause sort problems. These need brought up with DMTF TODO(Ed)
188 if key == "odata.4.0.0.json":
189 key = "odata.v4_0_0.json"
190 if key == "redfish-schema.1.0.0.json":
191 key = "redfish-schema.v1_0_0.json"
192
193 return parse(key)
194
195
Ed Tanous683f7272018-07-26 12:47:19 -0700196# Remove the old files
Ed Tanous8b564552022-09-23 12:03:18 -0700197
Ed Tanous27747912022-09-23 12:50:08 -0700198skip_prefixes = "Oem"
Ed Tanous683f7272018-07-26 12:47:19 -0700199if os.path.exists(schema_path):
Ed Tanous27747912022-09-23 12:50:08 -0700200 files = [
201 os.path.join(schema_path, f)
202 for f in os.listdir(schema_path)
203 if not f.startswith(skip_prefixes)
204 ]
James Feistaee8d842018-09-10 16:07:40 -0700205 for f in files:
206 os.remove(f)
Ed Tanous683f7272018-07-26 12:47:19 -0700207if os.path.exists(json_schema_path):
Ed Tanous27747912022-09-23 12:50:08 -0700208 files = [
209 os.path.join(json_schema_path, f)
210 for f in os.listdir(json_schema_path)
211 if not f.startswith(skip_prefixes)
212 ]
Ed Tanous118b1c72018-09-13 13:45:51 -0700213 for f in files:
Ed Tanous27747912022-09-23 12:50:08 -0700214 if os.path.isfile(f):
Ed Tanous118b1c72018-09-13 13:45:51 -0700215 os.remove(f)
216 else:
217 shutil.rmtree(f)
Ed Tanous8b564552022-09-23 12:03:18 -0700218try:
219 os.remove(metadata_index_path)
220except FileNotFoundError:
221 pass
Ed Tanous683f7272018-07-26 12:47:19 -0700222
Ed Tanous118b1c72018-09-13 13:45:51 -0700223if not os.path.exists(schema_path):
224 os.makedirs(schema_path)
225if not os.path.exists(json_schema_path):
226 os.makedirs(json_schema_path)
Ed Tanous683f7272018-07-26 12:47:19 -0700227
Ed Tanous8b564552022-09-23 12:03:18 -0700228csdl_filenames = []
229json_schema_files = defaultdict(list)
230
231for zip_filepath in zip_ref.namelist():
232 if zip_filepath.startswith("csdl/") and (zip_filepath != "csdl/"):
233 csdl_filenames.append(os.path.basename(zip_filepath))
234 elif zip_filepath.startswith("json-schema/"):
235 filename = os.path.basename(zip_filepath)
236 filenamesplit = filename.split(".")
237 # exclude schemas again to save flash space
238 if filenamesplit[0] not in include_list:
239 continue
240 json_schema_files[filenamesplit[0]].append(filename)
241 elif zip_filepath.startswith("openapi/"):
242 pass
243 elif zip_filepath.startswith("dictionaries/"):
244 pass
245
246# sort the json files by version
247for key, value in json_schema_files.items():
248 value.sort(key=version_sort_key, reverse=True)
249
250# Create a dictionary ordered by schema name
251json_schema_files = OrderedDict(
252 sorted(json_schema_files.items(), key=lambda x: version_sort_key(x[0]))
253)
254
255csdl_filenames.sort(key=version_sort_key)
Ed Tanous27747912022-09-23 12:50:08 -0700256with open(metadata_index_path, "w") as metadata_index:
Ed Tanous118b1c72018-09-13 13:45:51 -0700257
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
267 filenamesplit = filename.split("_")
268 if filenamesplit[0] not in include_list:
269 print("excluding schema: " + filename)
270 continue
Gunnar Mills349a2ac2021-01-20 22:29:16 -0600271
Ed Tanous8b564552022-09-23 12:03:18 -0700272 with open(os.path.join(schema_path, filename), "wb") as schema_out:
Gunnar Mills349a2ac2021-01-20 22:29:16 -0600273
Ed Tanous8b564552022-09-23 12:03:18 -0700274 metadata_index.write(
275 ' <edmx:Reference Uri="/redfish/v1/schema/'
276 + filename
277 + '">\n'
278 )
Ed Tanous118b1c72018-09-13 13:45:51 -0700279
Ed Tanous8b564552022-09-23 12:03:18 -0700280 content = zip_ref.read(os.path.join("csdl", filename))
281 content = content.replace(b"\r\n", b"\n")
282 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(
292 " "
293 '<edmx:Include Namespace="'
294 + namespace
295 + '" Alias="Redfish"/>\n'
296 )
Ed Tanous118b1c72018-09-13 13:45:51 -0700297
Ed Tanous8b564552022-09-23 12:03:18 -0700298 else:
299 metadata_index.write(
300 " "
301 '<edmx:Include Namespace="'
302 + namespace
303 + '"/>\n'
304 )
305 schema_out.write(content)
306 metadata_index.write(" </edmx:Reference>\n")
Ed Tanous118b1c72018-09-13 13:45:51 -0700307
Ed Tanous27747912022-09-23 12:50:08 -0700308 metadata_index.write(
309 " <edmx:DataServices>\n"
310 " <Schema "
311 'xmlns="http://docs.oasis-open.org/odata/ns/edm" '
312 'Namespace="Service">\n'
313 ' <EntityContainer Name="Service" '
314 'Extends="ServiceRoot.v1_0_0.ServiceContainer"/>\n'
315 " </Schema>\n"
316 " </edmx:DataServices>\n"
317 )
Ed Tanouscb103132019-10-08 11:34:22 -0700318 # TODO:Issue#32 There's a bug in the script that currently deletes this
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500319 # schema (because it's an OEM schema). Because it's the only six, and we
Ed Tanouscb103132019-10-08 11:34:22 -0700320 # don't update schemas very often, we just manually fix it. Need a
321 # permanent fix to the script.
322 metadata_index.write(
Ed Tanous27747912022-09-23 12:50:08 -0700323 ' <edmx:Reference Uri="/redfish/v1/schema/OemManager_v1.xml">\n'
324 )
325 metadata_index.write(' <edmx:Include Namespace="OemManager"/>\n')
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600326 metadata_index.write(" </edmx:Reference>\n")
Gunnar Mills20778992020-02-06 16:36:47 -0600327
328 metadata_index.write(
Ed Tanous27747912022-09-23 12:50:08 -0700329 ' <edmx:Reference Uri="'
330 '/redfish/v1/schema/OemComputerSystem_v1.xml">\n'
331 )
Ed Tanousf395daa2021-08-02 08:56:24 -0700332 metadata_index.write(
Ed Tanous27747912022-09-23 12:50:08 -0700333 ' <edmx:Include Namespace="OemComputerSystem"/>\n'
334 )
Gunnar Mills20778992020-02-06 16:36:47 -0600335 metadata_index.write(" </edmx:Reference>\n")
336
337 metadata_index.write(
Ed Tanous27747912022-09-23 12:50:08 -0700338 ' <edmx:Reference Uri="'
339 '/redfish/v1/schema/OemVirtualMedia_v1.xml">\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"/>\n'
343 )
Ed Tanousf395daa2021-08-02 08:56:24 -0700344 metadata_index.write(
Ed Tanous27747912022-09-23 12:50:08 -0700345 ' <edmx:Include Namespace="OemVirtualMedia.v1_0_0"/>\n'
346 )
Gunnar Mills20778992020-02-06 16:36:47 -0600347 metadata_index.write(" </edmx:Reference>\n")
348
349 metadata_index.write(
Ed Tanous27747912022-09-23 12:50:08 -0700350 ' <edmx:Reference Uri="'
351 '/redfish/v1/schema/OemAccountService_v1.xml">\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"/>\n'
355 )
Ed Tanousf395daa2021-08-02 08:56:24 -0700356 metadata_index.write(
Ed Tanous27747912022-09-23 12:50:08 -0700357 ' <edmx:Include Namespace="OemAccountService.v1_0_0"/>\n'
358 )
Gunnar Mills20778992020-02-06 16:36:47 -0600359 metadata_index.write(" </edmx:Reference>\n")
360
Ravi Tejae7d68c32020-03-15 13:30:41 -0500361 metadata_index.write(
Ed Tanous27747912022-09-23 12:50:08 -0700362 ' <edmx:Reference Uri="/redfish/v1/schema/OemSession_v1.xml">\n'
363 )
364 metadata_index.write(' <edmx:Include Namespace="OemSession"/>\n')
Ed Tanousf395daa2021-08-02 08:56:24 -0700365 metadata_index.write(
Ed Tanous27747912022-09-23 12:50:08 -0700366 ' <edmx:Include Namespace="OemSession.v1_0_0"/>\n'
367 )
Sunitha Harish9dc50742020-05-11 00:10:20 -0500368 metadata_index.write(" </edmx:Reference>\n")
369
Ed Tanous118b1c72018-09-13 13:45:51 -0700370 metadata_index.write("</edmx:Edmx>\n")
Ed Tanous683f7272018-07-26 12:47:19 -0700371
Gunnar Mills349a2ac2021-01-20 22:29:16 -0600372
Ed Tanous8b564552022-09-23 12:03:18 -0700373for schema, version in json_schema_files.items():
374 zip_filepath = os.path.join("json-schema", version[0])
Ed Tanous683f7272018-07-26 12:47:19 -0700375 schemadir = os.path.join(json_schema_path, schema)
376 os.makedirs(schemadir)
Ed Tanous118b1c72018-09-13 13:45:51 -0700377
Ed Tanous27747912022-09-23 12:50:08 -0700378 with open(os.path.join(schemadir, schema + ".json"), "wb") as schema_file:
379 schema_file.write(zip_ref.read(zip_filepath).replace(b"\r\n", b"\n"))
Ed Tanous683f7272018-07-26 12:47:19 -0700380
Ed Tanous27747912022-09-23 12:50:08 -0700381with open(os.path.join(cpp_path, "schemas.hpp"), "w") as hpp_file:
Ed Tanous81d523a2022-05-25 12:00:51 -0700382 hpp_file.write(
383 "#pragma once\n"
384 "{WARNING}\n"
385 "// clang-format off\n"
Ed Tanous3d69fed2022-09-26 20:10:42 -0700386 "#include <array>\n"
Ed Tanous81d523a2022-05-25 12:00:51 -0700387 "\n"
388 "namespace redfish\n"
389 "{{\n"
Ed Tanous27747912022-09-23 12:50:08 -0700390 " constexpr std::array schemas {{\n".format(WARNING=WARNING)
Ed Tanous81d523a2022-05-25 12:00:51 -0700391 )
Ed Tanous8b564552022-09-23 12:03:18 -0700392 for schema_file in json_schema_files:
Ed Tanous27747912022-09-23 12:50:08 -0700393 hpp_file.write(' "{}",\n'.format(schema_file))
394
395 hpp_file.write(" };\n" "}\n")
Ed Tanous683f7272018-07-26 12:47:19 -0700396
397zip_ref.close()