blob: 903afb7b5cf1a755a2f9dd083e81be5bdb3775cc [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',
96 'UpdateService',
97 'VLanNetworkInterfaceCollection',
98 'VLanNetworkInterface',
99 'VirtualMedia',
100 'VirtualMediaCollection',
101 'odata',
102 'odata-v4',
103 'redfish-error',
104 'redfish-payload-annotations',
105 'redfish-schema',
106 'redfish-schema-v1',
107]
108
Ed Tanous683f7272018-07-26 12:47:19 -0700109SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
110
111proxies = {
112 'https': os.environ.get("https_proxy", None)
113}
114
Ed Tanouscb103132019-10-08 11:34:22 -0700115r = requests.get(
116 'https://www.dmtf.org/sites/default/files/standards/documents/' +
117 VERSION +
118 '.zip',
119 proxies=proxies)
Ed Tanous683f7272018-07-26 12:47:19 -0700120
121r.raise_for_status()
122
123static_path = os.path.realpath(os.path.join(SCRIPT_DIR, "..", "static",
124 "redfish", "v1"))
125
126schema_path = os.path.join(static_path, "schema")
127json_schema_path = os.path.join(static_path, "JsonSchemas")
Ed Tanous118b1c72018-09-13 13:45:51 -0700128metadata_index_path = os.path.join(static_path, "$metadata", "index.xml")
Ed Tanous683f7272018-07-26 12:47:19 -0700129
130zipBytesIO = BytesIO(r.content)
131zip_ref = zipfile.ZipFile(zipBytesIO)
132
133# Remove the old files
Szymon Dompked699cf92021-08-11 19:46:31 +0200134skip_prefixes = ('Oem')
Ed Tanous683f7272018-07-26 12:47:19 -0700135if os.path.exists(schema_path):
Szymon Dompked699cf92021-08-11 19:46:31 +0200136 files = [os.path.join(schema_path, f) for f in os.listdir(schema_path)
137 if not f.startswith(skip_prefixes)]
James Feistaee8d842018-09-10 16:07:40 -0700138 for f in files:
139 os.remove(f)
Ed Tanous683f7272018-07-26 12:47:19 -0700140if os.path.exists(json_schema_path):
Szymon Dompked699cf92021-08-11 19:46:31 +0200141 files = [os.path.join(json_schema_path, f) for f in
142 os.listdir(json_schema_path) if not f.startswith(skip_prefixes)]
Ed Tanous118b1c72018-09-13 13:45:51 -0700143 for f in files:
144 if (os.path.isfile(f)):
145 os.remove(f)
146 else:
147 shutil.rmtree(f)
148os.remove(metadata_index_path)
Ed Tanous683f7272018-07-26 12:47:19 -0700149
Ed Tanous118b1c72018-09-13 13:45:51 -0700150if not os.path.exists(schema_path):
151 os.makedirs(schema_path)
152if not os.path.exists(json_schema_path):
153 os.makedirs(json_schema_path)
Ed Tanous683f7272018-07-26 12:47:19 -0700154
Ed Tanous118b1c72018-09-13 13:45:51 -0700155with 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(
Ed Tanousf395daa2021-08-02 08:56:24 -0700159 "<edmx:Edmx xmlns:edmx="
160 "\"http://docs.oasis-open.org/odata/ns/edmx\""
161 " Version=\"4.0\">\n")
Ed Tanous118b1c72018-09-13 13:45:51 -0700162
163 for zip_filepath in zip_ref.namelist():
Gunnar Mills60c922d2021-12-01 09:28:53 -0600164 if zip_filepath.startswith('csdl/') and \
Ed Tanousf395daa2021-08-02 08:56:24 -0700165 (zip_filepath != VERSION + "/csdl/") and \
Gunnar Mills60c922d2021-12-01 09:28:53 -0600166 (zip_filepath != "csdl/"):
Ed Tanous118b1c72018-09-13 13:45:51 -0700167 filename = os.path.basename(zip_filepath)
Gunnar Mills349a2ac2021-01-20 22:29:16 -0600168
169 # filename looks like Zone_v1.xml
170 filenamesplit = filename.split("_")
171 if filenamesplit[0] not in include_list:
172 print("excluding schema: " + filename)
173 continue
174
Ed Tanousf395daa2021-08-02 08:56:24 -0700175 with open(os.path.join(schema_path, filename), 'wb') as schema_out:
Ed Tanous118b1c72018-09-13 13:45:51 -0700176
177 metadata_index.write(
Ed Tanouscb103132019-10-08 11:34:22 -0700178 " <edmx:Reference Uri=\"/redfish/v1/schema/" +
179 filename +
180 "\">\n")
Ed Tanous118b1c72018-09-13 13:45:51 -0700181
182 content = zip_ref.read(zip_filepath)
Ed Tanouscb103132019-10-08 11:34:22 -0700183 content = content.replace(b'\r\n', b'\n')
Ed Tanous118b1c72018-09-13 13:45:51 -0700184 xml_root = ET.fromstring(content)
Ed Tanousf395daa2021-08-02 08:56:24 -0700185 edmx = "{http://docs.oasis-open.org/odata/ns/edmx}"
Szymon Dompked699cf92021-08-11 19:46:31 +0200186 edm = "{http://docs.oasis-open.org/odata/ns/edm}"
Ed Tanous118b1c72018-09-13 13:45:51 -0700187 for edmx_child in xml_root:
Ed Tanousf395daa2021-08-02 08:56:24 -0700188 if edmx_child.tag == edmx + "DataServices":
Ed Tanous118b1c72018-09-13 13:45:51 -0700189 for data_child in edmx_child:
Szymon Dompked699cf92021-08-11 19:46:31 +0200190 if data_child.tag == edm + "Schema":
Ed Tanous118b1c72018-09-13 13:45:51 -0700191 namespace = data_child.attrib["Namespace"]
192 if namespace.startswith("RedfishExtensions"):
193 metadata_index.write(
Ed Tanousf395daa2021-08-02 08:56:24 -0700194 " "
195 "<edmx:Include Namespace=\"" +
196 namespace +
197 "\" Alias=\"Redfish\"/>\n"
198 )
Ed Tanous118b1c72018-09-13 13:45:51 -0700199
200 else:
201 metadata_index.write(
Ed Tanousf395daa2021-08-02 08:56:24 -0700202 " "
203 "<edmx:Include Namespace=\""
204 + namespace + "\"/>\n"
205 )
206 schema_out.write(content)
Ed Tanous118b1c72018-09-13 13:45:51 -0700207 metadata_index.write(" </edmx:Reference>\n")
208
Ed Tanousf395daa2021-08-02 08:56:24 -0700209 metadata_index.write(" <edmx:DataServices>\n"
210 " <Schema "
211 "xmlns=\"http://docs.oasis-open.org/odata/ns/edm\" "
212 "Namespace=\"Service\">\n"
213 " <EntityContainer Name=\"Service\" "
214 "Extends=\"ServiceRoot.v1_0_0.ServiceContainer\"/>\n"
215 " </Schema>\n"
216 " </edmx:DataServices>\n"
217 )
Ed Tanouscb103132019-10-08 11:34:22 -0700218 # TODO:Issue#32 There's a bug in the script that currently deletes this
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500219 # schema (because it's an OEM schema). Because it's the only six, and we
Ed Tanouscb103132019-10-08 11:34:22 -0700220 # don't update schemas very often, we just manually fix it. Need a
221 # permanent fix to the script.
222 metadata_index.write(
223 " <edmx:Reference Uri=\"/redfish/v1/schema/OemManager_v1.xml\">\n")
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600224 metadata_index.write(" <edmx:Include Namespace=\"OemManager\"/>\n")
225 metadata_index.write(" </edmx:Reference>\n")
Gunnar Mills20778992020-02-06 16:36:47 -0600226
227 metadata_index.write(
Ed Tanousf395daa2021-08-02 08:56:24 -0700228 " <edmx:Reference Uri=\""
229 "/redfish/v1/schema/OemComputerSystem_v1.xml\">\n")
230 metadata_index.write(
231 " <edmx:Include Namespace=\"OemComputerSystem\"/>\n")
Gunnar Mills20778992020-02-06 16:36:47 -0600232 metadata_index.write(" </edmx:Reference>\n")
233
234 metadata_index.write(
Ed Tanousf395daa2021-08-02 08:56:24 -0700235 " <edmx:Reference Uri=\""
236 "/redfish/v1/schema/OemVirtualMedia_v1.xml\">\n")
237 metadata_index.write(
238 " <edmx:Include Namespace=\"OemVirtualMedia\"/>\n")
239 metadata_index.write(
240 " <edmx:Include Namespace=\"OemVirtualMedia.v1_0_0\"/>\n")
Gunnar Mills20778992020-02-06 16:36:47 -0600241 metadata_index.write(" </edmx:Reference>\n")
242
243 metadata_index.write(
Ed Tanousf395daa2021-08-02 08:56:24 -0700244 " <edmx:Reference Uri=\""
245 "/redfish/v1/schema/OemAccountService_v1.xml\">\n")
246 metadata_index.write(
247 " <edmx:Include Namespace=\"OemAccountService\"/>\n")
248 metadata_index.write(
249 " <edmx:Include Namespace=\"OemAccountService.v1_0_0\"/>\n")
Gunnar Mills20778992020-02-06 16:36:47 -0600250 metadata_index.write(" </edmx:Reference>\n")
251
Ravi Tejae7d68c32020-03-15 13:30:41 -0500252 metadata_index.write(
Sunitha Harish9dc50742020-05-11 00:10:20 -0500253 " <edmx:Reference Uri=\"/redfish/v1/schema/OemSession_v1.xml\">\n")
254 metadata_index.write(" <edmx:Include Namespace=\"OemSession\"/>\n")
Ed Tanousf395daa2021-08-02 08:56:24 -0700255 metadata_index.write(
256 " <edmx:Include Namespace=\"OemSession.v1_0_0\"/>\n")
Sunitha Harish9dc50742020-05-11 00:10:20 -0500257 metadata_index.write(" </edmx:Reference>\n")
258
Ed Tanous118b1c72018-09-13 13:45:51 -0700259 metadata_index.write("</edmx:Edmx>\n")
Ed Tanous683f7272018-07-26 12:47:19 -0700260
261schema_files = {}
262for zip_filepath in zip_ref.namelist():
Gunnar Mills60c922d2021-12-01 09:28:53 -0600263 if zip_filepath.startswith(os.path.join('json-schema/')):
Ed Tanous683f7272018-07-26 12:47:19 -0700264 filename = os.path.basename(zip_filepath)
265 filenamesplit = filename.split(".")
Gunnar Mills349a2ac2021-01-20 22:29:16 -0600266
267 # exclude schemas again to save flash space
268 if filenamesplit[0] not in include_list:
269 continue
270
Ed Tanous683f7272018-07-26 12:47:19 -0700271 if len(filenamesplit) == 3:
272 thisSchemaVersion = schema_files.get(filenamesplit[0], None)
Ed Tanouscb103132019-10-08 11:34:22 -0700273 if thisSchemaVersion is None:
Ed Tanous683f7272018-07-26 12:47:19 -0700274 schema_files[filenamesplit[0]] = filenamesplit[1]
275 else:
276 # need to see if we're a newer version.
277 if list(map(int, filenamesplit[1][1:].split("_"))) > list(map(
278 int, thisSchemaVersion[1:].split("_"))):
279 schema_files[filenamesplit[0]] = filenamesplit[1]
280
281
282for schema, version in schema_files.items():
283 basename = schema + "." + version + ".json"
Gunnar Mills60c922d2021-12-01 09:28:53 -0600284 zip_filepath = os.path.join("json-schema", basename)
Ed Tanous683f7272018-07-26 12:47:19 -0700285 schemadir = os.path.join(json_schema_path, schema)
286 os.makedirs(schemadir)
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600287 location_json = OrderedDict()
288 location_json["Language"] = "en"
289 location_json["PublicationUri"] = (
290 "http://redfish.dmtf.org/schemas/v1/" + schema + ".json")
291 location_json["Uri"] = (
Ed Tanous63faafa2019-01-03 14:09:56 -0800292 "/redfish/v1/JsonSchemas/" + schema + "/" + schema + ".json")
Ed Tanous118b1c72018-09-13 13:45:51 -0700293
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600294 index_json = OrderedDict()
Ed Tanousf395daa2021-08-02 08:56:24 -0700295 index_json["@odata.context"] = \
296 "/redfish/v1/$metadata#JsonSchemaFile.JsonSchemaFile"
Ed Tanous63faafa2019-01-03 14:09:56 -0800297 index_json["@odata.id"] = "/redfish/v1/JsonSchemas/" + schema
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600298 index_json["@odata.type"] = "#JsonSchemaFile.v1_0_2.JsonSchemaFile"
299 index_json["Name"] = schema + " Schema File"
300 index_json["Schema"] = "#" + schema + "." + schema
Ed Tanouscb103132019-10-08 11:34:22 -0700301 index_json["Description"] = schema + " Schema File Location"
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600302 index_json["Id"] = schema
Ed Tanouscb103132019-10-08 11:34:22 -0700303 index_json["Languages"] = ["en"]
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600304 index_json["Languages@odata.count"] = 1
Ed Tanouscb103132019-10-08 11:34:22 -0700305 index_json["Location"] = [location_json]
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600306 index_json["Location@odata.count"] = 1
Ed Tanous118b1c72018-09-13 13:45:51 -0700307
308 with open(os.path.join(schemadir, "index.json"), 'w') as schema_file:
309 json.dump(index_json, schema_file, indent=4)
310 with open(os.path.join(schemadir, schema + ".json"), 'wb') as schema_file:
Ed Tanouscb103132019-10-08 11:34:22 -0700311 schema_file.write(zip_ref.read(zip_filepath).replace(b'\r\n', b'\n'))
Ed Tanous683f7272018-07-26 12:47:19 -0700312
313with open(os.path.join(json_schema_path, "index.json"), 'w') as index_file:
Ed Tanous6f56d0c2019-05-03 17:15:41 -0700314 members = [{"@odata.id": "/redfish/v1/JsonSchemas/" + schema}
Ed Tanous683f7272018-07-26 12:47:19 -0700315 for schema in schema_files]
316
317 members.sort(key=lambda x: x["@odata.id"])
318
319 indexData = OrderedDict()
320
321 indexData["@odata.id"] = "/redfish/v1/JsonSchemas"
322 indexData["@odata.context"] = ("/redfish/v1/$metadata"
323 "#JsonSchemaFileCollection."
324 "JsonSchemaFileCollection")
325 indexData["@odata.type"] = ("#JsonSchemaFileCollection."
326 "JsonSchemaFileCollection")
327 indexData["Name"] = "JsonSchemaFile Collection"
328 indexData["Description"] = "Collection of JsonSchemaFiles"
329 indexData["Members@odata.count"] = len(schema_files)
330 indexData["Members"] = members
331
332 json.dump(indexData, index_file, indent=2)
333
334zip_ref.close()