blob: 63b441eaa37c0ceb63bd88291357f06de5a0bcb8 [file] [log] [blame]
Ed Tanous683f7272018-07-26 12:47:19 -07001#!/usr/bin/python3
2import requests
3import zipfile
4from io import BytesIO
5import os
Ed Tanous683f7272018-07-26 12:47:19 -07006from collections import OrderedDict
Ed Tanous683f7272018-07-26 12:47:19 -07007import shutil
8import json
9
Ed Tanous118b1c72018-09-13 13:45:51 -070010import xml.etree.ElementTree as ET
11
Gunnar Mills60c922d2021-12-01 09:28:53 -060012VERSION = "DSP8010_2021.3"
Ed Tanouscb103132019-10-08 11:34:22 -070013
Gunnar Mills349a2ac2021-01-20 22:29:16 -060014# To use a new schema, add to list and rerun tool
15include_list = [
16 'AccountService',
17 'ActionInfo',
18 'Assembly',
19 'AttributeRegistry',
20 'Bios',
21 'Certificate',
22 'CertificateCollection',
23 'CertificateLocations',
24 'CertificateService',
25 'Chassis',
26 'ChassisCollection',
27 'ComputerSystem',
28 'ComputerSystemCollection',
29 'Drive',
30 'DriveCollection',
31 'EthernetInterface',
32 'EthernetInterfaceCollection',
33 'Event',
34 'EventDestination',
35 'EventDestinationCollection',
36 'EventService',
37 'IPAddresses',
38 'JsonSchemaFile',
Ed Tanousf395daa2021-08-02 08:56:24 -070039 'JsonSchemaFileCollection', # redfish/v1/JsonSchemas
Gunnar Mills349a2ac2021-01-20 22:29:16 -060040 'LogEntry',
41 'LogEntryCollection',
42 'LogService',
43 'LogServiceCollection',
44 'Manager',
45 'ManagerAccount',
46 'ManagerAccountCollection',
47 'ManagerCollection',
48 'ManagerNetworkProtocol',
49 'Memory',
50 'MemoryCollection',
51 'Message',
52 'MessageRegistry',
53 'MessageRegistryCollection',
54 'MessageRegistryFile',
55 'MessageRegistryFileCollection',
56 'MetricDefinition',
57 'MetricDefinitionCollection',
58 'MetricReport',
59 'MetricReportCollection',
60 'MetricReportDefinition',
61 'MetricReportDefinitionCollection',
62 'OperatingConfig',
63 'OperatingConfigCollection',
64 'PCIeDevice',
65 'PCIeDeviceCollection',
66 'PCIeFunction',
67 'PCIeFunctionCollection',
68 'Power',
Ed Tanousf395daa2021-08-02 08:56:24 -070069 'Privileges', # Used in Role
Gunnar Mills349a2ac2021-01-20 22:29:16 -060070 'Processor',
71 'ProcessorCollection',
72 'RedfishError',
73 'RedfishExtensions',
74 'Redundancy',
75 'Resource',
76 'Role',
77 'RoleCollection',
78 'Sensor',
79 'SensorCollection',
80 'ServiceRoot',
81 'Session',
82 'SessionCollection',
83 'SessionService',
84 'Settings',
85 'SoftwareInventory',
86 'SoftwareInventoryCollection',
87 'Storage',
88 'StorageCollection',
89 'StorageController',
90 'StorageControllerCollection',
91 'Task',
92 'TaskCollection',
93 'TaskService',
94 'TelemetryService',
95 'Thermal',
Lukasz Kazmierczak07148cf2021-08-02 11:08:53 +020096 'TriggersCollection',
Gunnar Mills349a2ac2021-01-20 22:29:16 -060097 'UpdateService',
98 'VLanNetworkInterfaceCollection',
99 'VLanNetworkInterface',
100 'VirtualMedia',
101 'VirtualMediaCollection',
102 'odata',
103 'odata-v4',
104 'redfish-error',
105 'redfish-payload-annotations',
106 'redfish-schema',
107 'redfish-schema-v1',
108]
109
Ed Tanous683f7272018-07-26 12:47:19 -0700110SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
111
112proxies = {
113 'https': os.environ.get("https_proxy", None)
114}
115
Ed Tanouscb103132019-10-08 11:34:22 -0700116r = requests.get(
117 'https://www.dmtf.org/sites/default/files/standards/documents/' +
118 VERSION +
119 '.zip',
120 proxies=proxies)
Ed Tanous683f7272018-07-26 12:47:19 -0700121
122r.raise_for_status()
123
124static_path = os.path.realpath(os.path.join(SCRIPT_DIR, "..", "static",
125 "redfish", "v1"))
126
127schema_path = os.path.join(static_path, "schema")
128json_schema_path = os.path.join(static_path, "JsonSchemas")
Ed Tanous118b1c72018-09-13 13:45:51 -0700129metadata_index_path = os.path.join(static_path, "$metadata", "index.xml")
Ed Tanous683f7272018-07-26 12:47:19 -0700130
131zipBytesIO = BytesIO(r.content)
132zip_ref = zipfile.ZipFile(zipBytesIO)
133
134# Remove the old files
Szymon Dompked699cf92021-08-11 19:46:31 +0200135skip_prefixes = ('Oem')
Ed Tanous683f7272018-07-26 12:47:19 -0700136if os.path.exists(schema_path):
Szymon Dompked699cf92021-08-11 19:46:31 +0200137 files = [os.path.join(schema_path, f) for f in os.listdir(schema_path)
138 if not f.startswith(skip_prefixes)]
James Feistaee8d842018-09-10 16:07:40 -0700139 for f in files:
140 os.remove(f)
Ed Tanous683f7272018-07-26 12:47:19 -0700141if os.path.exists(json_schema_path):
Szymon Dompked699cf92021-08-11 19:46:31 +0200142 files = [os.path.join(json_schema_path, f) for f in
143 os.listdir(json_schema_path) if not f.startswith(skip_prefixes)]
Ed Tanous118b1c72018-09-13 13:45:51 -0700144 for f in files:
145 if (os.path.isfile(f)):
146 os.remove(f)
147 else:
148 shutil.rmtree(f)
149os.remove(metadata_index_path)
Ed Tanous683f7272018-07-26 12:47:19 -0700150
Ed Tanous118b1c72018-09-13 13:45:51 -0700151if not os.path.exists(schema_path):
152 os.makedirs(schema_path)
153if not os.path.exists(json_schema_path):
154 os.makedirs(json_schema_path)
Ed Tanous683f7272018-07-26 12:47:19 -0700155
Ed Tanous118b1c72018-09-13 13:45:51 -0700156with open(metadata_index_path, 'w') as metadata_index:
157
158 metadata_index.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
159 metadata_index.write(
Ed Tanousf395daa2021-08-02 08:56:24 -0700160 "<edmx:Edmx xmlns:edmx="
161 "\"http://docs.oasis-open.org/odata/ns/edmx\""
162 " Version=\"4.0\">\n")
Ed Tanous118b1c72018-09-13 13:45:51 -0700163
164 for zip_filepath in zip_ref.namelist():
Gunnar Mills60c922d2021-12-01 09:28:53 -0600165 if zip_filepath.startswith('csdl/') and \
Ed Tanousf395daa2021-08-02 08:56:24 -0700166 (zip_filepath != VERSION + "/csdl/") and \
Gunnar Mills60c922d2021-12-01 09:28:53 -0600167 (zip_filepath != "csdl/"):
Ed Tanous118b1c72018-09-13 13:45:51 -0700168 filename = os.path.basename(zip_filepath)
Gunnar Mills349a2ac2021-01-20 22:29:16 -0600169
170 # filename looks like Zone_v1.xml
171 filenamesplit = filename.split("_")
172 if filenamesplit[0] not in include_list:
173 print("excluding schema: " + filename)
174 continue
175
Ed Tanousf395daa2021-08-02 08:56:24 -0700176 with open(os.path.join(schema_path, filename), 'wb') as schema_out:
Ed Tanous118b1c72018-09-13 13:45:51 -0700177
178 metadata_index.write(
Ed Tanouscb103132019-10-08 11:34:22 -0700179 " <edmx:Reference Uri=\"/redfish/v1/schema/" +
180 filename +
181 "\">\n")
Ed Tanous118b1c72018-09-13 13:45:51 -0700182
183 content = zip_ref.read(zip_filepath)
Ed Tanouscb103132019-10-08 11:34:22 -0700184 content = content.replace(b'\r\n', b'\n')
Ed Tanous118b1c72018-09-13 13:45:51 -0700185 xml_root = ET.fromstring(content)
Ed Tanousf395daa2021-08-02 08:56:24 -0700186 edmx = "{http://docs.oasis-open.org/odata/ns/edmx}"
Szymon Dompked699cf92021-08-11 19:46:31 +0200187 edm = "{http://docs.oasis-open.org/odata/ns/edm}"
Ed Tanous118b1c72018-09-13 13:45:51 -0700188 for edmx_child in xml_root:
Ed Tanousf395daa2021-08-02 08:56:24 -0700189 if edmx_child.tag == edmx + "DataServices":
Ed Tanous118b1c72018-09-13 13:45:51 -0700190 for data_child in edmx_child:
Szymon Dompked699cf92021-08-11 19:46:31 +0200191 if data_child.tag == edm + "Schema":
Ed Tanous118b1c72018-09-13 13:45:51 -0700192 namespace = data_child.attrib["Namespace"]
193 if namespace.startswith("RedfishExtensions"):
194 metadata_index.write(
Ed Tanousf395daa2021-08-02 08:56:24 -0700195 " "
196 "<edmx:Include Namespace=\"" +
197 namespace +
198 "\" Alias=\"Redfish\"/>\n"
199 )
Ed Tanous118b1c72018-09-13 13:45:51 -0700200
201 else:
202 metadata_index.write(
Ed Tanousf395daa2021-08-02 08:56:24 -0700203 " "
204 "<edmx:Include Namespace=\""
205 + namespace + "\"/>\n"
206 )
207 schema_out.write(content)
Ed Tanous118b1c72018-09-13 13:45:51 -0700208 metadata_index.write(" </edmx:Reference>\n")
209
Ed Tanousf395daa2021-08-02 08:56:24 -0700210 metadata_index.write(" <edmx:DataServices>\n"
211 " <Schema "
212 "xmlns=\"http://docs.oasis-open.org/odata/ns/edm\" "
213 "Namespace=\"Service\">\n"
214 " <EntityContainer Name=\"Service\" "
215 "Extends=\"ServiceRoot.v1_0_0.ServiceContainer\"/>\n"
216 " </Schema>\n"
217 " </edmx:DataServices>\n"
218 )
Ed Tanouscb103132019-10-08 11:34:22 -0700219 # TODO:Issue#32 There's a bug in the script that currently deletes this
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500220 # schema (because it's an OEM schema). Because it's the only six, and we
Ed Tanouscb103132019-10-08 11:34:22 -0700221 # don't update schemas very often, we just manually fix it. Need a
222 # permanent fix to the script.
223 metadata_index.write(
224 " <edmx:Reference Uri=\"/redfish/v1/schema/OemManager_v1.xml\">\n")
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600225 metadata_index.write(" <edmx:Include Namespace=\"OemManager\"/>\n")
226 metadata_index.write(" </edmx:Reference>\n")
Gunnar Mills20778992020-02-06 16:36:47 -0600227
228 metadata_index.write(
Ed Tanousf395daa2021-08-02 08:56:24 -0700229 " <edmx:Reference Uri=\""
230 "/redfish/v1/schema/OemComputerSystem_v1.xml\">\n")
231 metadata_index.write(
232 " <edmx:Include Namespace=\"OemComputerSystem\"/>\n")
Gunnar Mills20778992020-02-06 16:36:47 -0600233 metadata_index.write(" </edmx:Reference>\n")
234
235 metadata_index.write(
Ed Tanousf395daa2021-08-02 08:56:24 -0700236 " <edmx:Reference Uri=\""
237 "/redfish/v1/schema/OemVirtualMedia_v1.xml\">\n")
238 metadata_index.write(
239 " <edmx:Include Namespace=\"OemVirtualMedia\"/>\n")
240 metadata_index.write(
241 " <edmx:Include Namespace=\"OemVirtualMedia.v1_0_0\"/>\n")
Gunnar Mills20778992020-02-06 16:36:47 -0600242 metadata_index.write(" </edmx:Reference>\n")
243
244 metadata_index.write(
Ed Tanousf395daa2021-08-02 08:56:24 -0700245 " <edmx:Reference Uri=\""
246 "/redfish/v1/schema/OemAccountService_v1.xml\">\n")
247 metadata_index.write(
248 " <edmx:Include Namespace=\"OemAccountService\"/>\n")
249 metadata_index.write(
250 " <edmx:Include Namespace=\"OemAccountService.v1_0_0\"/>\n")
Gunnar Mills20778992020-02-06 16:36:47 -0600251 metadata_index.write(" </edmx:Reference>\n")
252
Ravi Tejae7d68c32020-03-15 13:30:41 -0500253 metadata_index.write(
Sunitha Harish9dc50742020-05-11 00:10:20 -0500254 " <edmx:Reference Uri=\"/redfish/v1/schema/OemSession_v1.xml\">\n")
255 metadata_index.write(" <edmx:Include Namespace=\"OemSession\"/>\n")
Ed Tanousf395daa2021-08-02 08:56:24 -0700256 metadata_index.write(
257 " <edmx:Include Namespace=\"OemSession.v1_0_0\"/>\n")
Sunitha Harish9dc50742020-05-11 00:10:20 -0500258 metadata_index.write(" </edmx:Reference>\n")
259
Ed Tanous118b1c72018-09-13 13:45:51 -0700260 metadata_index.write("</edmx:Edmx>\n")
Ed Tanous683f7272018-07-26 12:47:19 -0700261
262schema_files = {}
263for zip_filepath in zip_ref.namelist():
Gunnar Mills60c922d2021-12-01 09:28:53 -0600264 if zip_filepath.startswith(os.path.join('json-schema/')):
Ed Tanous683f7272018-07-26 12:47:19 -0700265 filename = os.path.basename(zip_filepath)
266 filenamesplit = filename.split(".")
Gunnar Mills349a2ac2021-01-20 22:29:16 -0600267
268 # exclude schemas again to save flash space
269 if filenamesplit[0] not in include_list:
270 continue
271
Ed Tanous683f7272018-07-26 12:47:19 -0700272 if len(filenamesplit) == 3:
273 thisSchemaVersion = schema_files.get(filenamesplit[0], None)
Ed Tanouscb103132019-10-08 11:34:22 -0700274 if thisSchemaVersion is None:
Ed Tanous683f7272018-07-26 12:47:19 -0700275 schema_files[filenamesplit[0]] = filenamesplit[1]
276 else:
277 # need to see if we're a newer version.
278 if list(map(int, filenamesplit[1][1:].split("_"))) > list(map(
279 int, thisSchemaVersion[1:].split("_"))):
280 schema_files[filenamesplit[0]] = filenamesplit[1]
281
282
283for schema, version in schema_files.items():
284 basename = schema + "." + version + ".json"
Gunnar Mills60c922d2021-12-01 09:28:53 -0600285 zip_filepath = os.path.join("json-schema", basename)
Ed Tanous683f7272018-07-26 12:47:19 -0700286 schemadir = os.path.join(json_schema_path, schema)
287 os.makedirs(schemadir)
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600288 location_json = OrderedDict()
289 location_json["Language"] = "en"
290 location_json["PublicationUri"] = (
291 "http://redfish.dmtf.org/schemas/v1/" + schema + ".json")
292 location_json["Uri"] = (
Ed Tanous63faafa2019-01-03 14:09:56 -0800293 "/redfish/v1/JsonSchemas/" + schema + "/" + schema + ".json")
Ed Tanous118b1c72018-09-13 13:45:51 -0700294
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600295 index_json = OrderedDict()
Ed Tanousf395daa2021-08-02 08:56:24 -0700296 index_json["@odata.context"] = \
297 "/redfish/v1/$metadata#JsonSchemaFile.JsonSchemaFile"
Ed Tanous63faafa2019-01-03 14:09:56 -0800298 index_json["@odata.id"] = "/redfish/v1/JsonSchemas/" + schema
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600299 index_json["@odata.type"] = "#JsonSchemaFile.v1_0_2.JsonSchemaFile"
300 index_json["Name"] = schema + " Schema File"
301 index_json["Schema"] = "#" + schema + "." + schema
Ed Tanouscb103132019-10-08 11:34:22 -0700302 index_json["Description"] = schema + " Schema File Location"
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600303 index_json["Id"] = schema
Ed Tanouscb103132019-10-08 11:34:22 -0700304 index_json["Languages"] = ["en"]
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600305 index_json["Languages@odata.count"] = 1
Ed Tanouscb103132019-10-08 11:34:22 -0700306 index_json["Location"] = [location_json]
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600307 index_json["Location@odata.count"] = 1
Ed Tanous118b1c72018-09-13 13:45:51 -0700308
309 with open(os.path.join(schemadir, "index.json"), 'w') as schema_file:
310 json.dump(index_json, schema_file, indent=4)
311 with open(os.path.join(schemadir, schema + ".json"), 'wb') as schema_file:
Ed Tanouscb103132019-10-08 11:34:22 -0700312 schema_file.write(zip_ref.read(zip_filepath).replace(b'\r\n', b'\n'))
Ed Tanous683f7272018-07-26 12:47:19 -0700313
314with open(os.path.join(json_schema_path, "index.json"), 'w') as index_file:
Ed Tanous6f56d0c2019-05-03 17:15:41 -0700315 members = [{"@odata.id": "/redfish/v1/JsonSchemas/" + schema}
Ed Tanous683f7272018-07-26 12:47:19 -0700316 for schema in schema_files]
317
318 members.sort(key=lambda x: x["@odata.id"])
319
320 indexData = OrderedDict()
321
322 indexData["@odata.id"] = "/redfish/v1/JsonSchemas"
323 indexData["@odata.context"] = ("/redfish/v1/$metadata"
324 "#JsonSchemaFileCollection."
325 "JsonSchemaFileCollection")
326 indexData["@odata.type"] = ("#JsonSchemaFileCollection."
327 "JsonSchemaFileCollection")
328 indexData["Name"] = "JsonSchemaFile Collection"
329 indexData["Description"] = "Collection of JsonSchemaFiles"
330 indexData["Members@odata.count"] = len(schema_files)
331 indexData["Members"] = members
332
333 json.dump(indexData, index_file, indent=2)
334
335zip_ref.close()