blob: 509fb7dcc48431af82db4c8155c16cd52ae8e037 [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
Sui Chen141d9432022-02-03 22:01:27 -080012VERSION = "DSP8010_2021.4"
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',
Shantappa Teekappanavar9c929be2021-12-16 19:02:52 -060021 'Cable',
22 'CableCollection',
Gunnar Mills349a2ac2021-01-20 22:29:16 -060023 'Certificate',
24 'CertificateCollection',
25 'CertificateLocations',
26 'CertificateService',
27 'Chassis',
28 'ChassisCollection',
29 'ComputerSystem',
30 'ComputerSystemCollection',
31 'Drive',
32 'DriveCollection',
33 'EthernetInterface',
34 'EthernetInterfaceCollection',
35 'Event',
36 'EventDestination',
37 'EventDestinationCollection',
38 'EventService',
39 'IPAddresses',
40 'JsonSchemaFile',
Ed Tanousf395daa2021-08-02 08:56:24 -070041 'JsonSchemaFileCollection', # redfish/v1/JsonSchemas
Gunnar Mills349a2ac2021-01-20 22:29:16 -060042 'LogEntry',
43 'LogEntryCollection',
44 'LogService',
45 'LogServiceCollection',
46 'Manager',
47 'ManagerAccount',
48 'ManagerAccountCollection',
49 'ManagerCollection',
50 'ManagerNetworkProtocol',
51 'Memory',
52 'MemoryCollection',
53 'Message',
54 'MessageRegistry',
55 'MessageRegistryCollection',
56 'MessageRegistryFile',
57 'MessageRegistryFileCollection',
58 'MetricDefinition',
59 'MetricDefinitionCollection',
60 'MetricReport',
61 'MetricReportCollection',
62 'MetricReportDefinition',
63 'MetricReportDefinitionCollection',
64 'OperatingConfig',
65 'OperatingConfigCollection',
66 'PCIeDevice',
67 'PCIeDeviceCollection',
68 'PCIeFunction',
69 'PCIeFunctionCollection',
sunitakx71b861b2022-01-18 11:05:05 +000070 'PhysicalContext',
Gunnar Mills349a2ac2021-01-20 22:29:16 -060071 'Power',
Ed Tanousf395daa2021-08-02 08:56:24 -070072 'Privileges', # Used in Role
Gunnar Mills349a2ac2021-01-20 22:29:16 -060073 'Processor',
74 'ProcessorCollection',
75 'RedfishError',
76 'RedfishExtensions',
77 'Redundancy',
78 'Resource',
79 'Role',
80 'RoleCollection',
81 'Sensor',
82 'SensorCollection',
83 'ServiceRoot',
84 'Session',
85 'SessionCollection',
86 'SessionService',
87 'Settings',
88 'SoftwareInventory',
89 'SoftwareInventoryCollection',
90 'Storage',
91 'StorageCollection',
92 'StorageController',
93 'StorageControllerCollection',
94 'Task',
95 'TaskCollection',
96 'TaskService',
97 'TelemetryService',
98 'Thermal',
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +020099 'Triggers',
Lukasz Kazmierczak07148cf2021-08-02 11:08:53 +0200100 'TriggersCollection',
Gunnar Mills349a2ac2021-01-20 22:29:16 -0600101 'UpdateService',
102 'VLanNetworkInterfaceCollection',
103 'VLanNetworkInterface',
104 'VirtualMedia',
105 'VirtualMediaCollection',
106 'odata',
107 'odata-v4',
108 'redfish-error',
109 'redfish-payload-annotations',
110 'redfish-schema',
111 'redfish-schema-v1',
112]
113
Ed Tanous683f7272018-07-26 12:47:19 -0700114SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
115
116proxies = {
117 'https': os.environ.get("https_proxy", None)
118}
119
Ed Tanouscb103132019-10-08 11:34:22 -0700120r = requests.get(
121 'https://www.dmtf.org/sites/default/files/standards/documents/' +
122 VERSION +
123 '.zip',
124 proxies=proxies)
Ed Tanous683f7272018-07-26 12:47:19 -0700125
126r.raise_for_status()
127
128static_path = os.path.realpath(os.path.join(SCRIPT_DIR, "..", "static",
129 "redfish", "v1"))
130
131schema_path = os.path.join(static_path, "schema")
132json_schema_path = os.path.join(static_path, "JsonSchemas")
Ed Tanous118b1c72018-09-13 13:45:51 -0700133metadata_index_path = os.path.join(static_path, "$metadata", "index.xml")
Ed Tanous683f7272018-07-26 12:47:19 -0700134
135zipBytesIO = BytesIO(r.content)
136zip_ref = zipfile.ZipFile(zipBytesIO)
137
138# Remove the old files
Szymon Dompked699cf92021-08-11 19:46:31 +0200139skip_prefixes = ('Oem')
Ed Tanous683f7272018-07-26 12:47:19 -0700140if os.path.exists(schema_path):
Szymon Dompked699cf92021-08-11 19:46:31 +0200141 files = [os.path.join(schema_path, f) for f in os.listdir(schema_path)
142 if not f.startswith(skip_prefixes)]
James Feistaee8d842018-09-10 16:07:40 -0700143 for f in files:
144 os.remove(f)
Ed Tanous683f7272018-07-26 12:47:19 -0700145if os.path.exists(json_schema_path):
Szymon Dompked699cf92021-08-11 19:46:31 +0200146 files = [os.path.join(json_schema_path, f) for f in
147 os.listdir(json_schema_path) if not f.startswith(skip_prefixes)]
Ed Tanous118b1c72018-09-13 13:45:51 -0700148 for f in files:
149 if (os.path.isfile(f)):
150 os.remove(f)
151 else:
152 shutil.rmtree(f)
153os.remove(metadata_index_path)
Ed Tanous683f7272018-07-26 12:47:19 -0700154
Ed Tanous118b1c72018-09-13 13:45:51 -0700155if not os.path.exists(schema_path):
156 os.makedirs(schema_path)
157if not os.path.exists(json_schema_path):
158 os.makedirs(json_schema_path)
Ed Tanous683f7272018-07-26 12:47:19 -0700159
Ed Tanous118b1c72018-09-13 13:45:51 -0700160with open(metadata_index_path, 'w') as metadata_index:
161
162 metadata_index.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
163 metadata_index.write(
Ed Tanousf395daa2021-08-02 08:56:24 -0700164 "<edmx:Edmx xmlns:edmx="
165 "\"http://docs.oasis-open.org/odata/ns/edmx\""
166 " Version=\"4.0\">\n")
Ed Tanous118b1c72018-09-13 13:45:51 -0700167
168 for zip_filepath in zip_ref.namelist():
Gunnar Mills60c922d2021-12-01 09:28:53 -0600169 if zip_filepath.startswith('csdl/') and \
Ed Tanousf395daa2021-08-02 08:56:24 -0700170 (zip_filepath != VERSION + "/csdl/") and \
Gunnar Mills60c922d2021-12-01 09:28:53 -0600171 (zip_filepath != "csdl/"):
Ed Tanous118b1c72018-09-13 13:45:51 -0700172 filename = os.path.basename(zip_filepath)
Gunnar Mills349a2ac2021-01-20 22:29:16 -0600173
174 # filename looks like Zone_v1.xml
175 filenamesplit = filename.split("_")
176 if filenamesplit[0] not in include_list:
177 print("excluding schema: " + filename)
178 continue
179
Ed Tanousf395daa2021-08-02 08:56:24 -0700180 with open(os.path.join(schema_path, filename), 'wb') as schema_out:
Ed Tanous118b1c72018-09-13 13:45:51 -0700181
182 metadata_index.write(
Ed Tanouscb103132019-10-08 11:34:22 -0700183 " <edmx:Reference Uri=\"/redfish/v1/schema/" +
184 filename +
185 "\">\n")
Ed Tanous118b1c72018-09-13 13:45:51 -0700186
187 content = zip_ref.read(zip_filepath)
Ed Tanouscb103132019-10-08 11:34:22 -0700188 content = content.replace(b'\r\n', b'\n')
Ed Tanous118b1c72018-09-13 13:45:51 -0700189 xml_root = ET.fromstring(content)
Ed Tanousf395daa2021-08-02 08:56:24 -0700190 edmx = "{http://docs.oasis-open.org/odata/ns/edmx}"
Szymon Dompked699cf92021-08-11 19:46:31 +0200191 edm = "{http://docs.oasis-open.org/odata/ns/edm}"
Ed Tanous118b1c72018-09-13 13:45:51 -0700192 for edmx_child in xml_root:
Ed Tanousf395daa2021-08-02 08:56:24 -0700193 if edmx_child.tag == edmx + "DataServices":
Ed Tanous118b1c72018-09-13 13:45:51 -0700194 for data_child in edmx_child:
Szymon Dompked699cf92021-08-11 19:46:31 +0200195 if data_child.tag == edm + "Schema":
Ed Tanous118b1c72018-09-13 13:45:51 -0700196 namespace = data_child.attrib["Namespace"]
197 if namespace.startswith("RedfishExtensions"):
198 metadata_index.write(
Ed Tanousf395daa2021-08-02 08:56:24 -0700199 " "
200 "<edmx:Include Namespace=\"" +
201 namespace +
202 "\" Alias=\"Redfish\"/>\n"
203 )
Ed Tanous118b1c72018-09-13 13:45:51 -0700204
205 else:
206 metadata_index.write(
Ed Tanousf395daa2021-08-02 08:56:24 -0700207 " "
208 "<edmx:Include Namespace=\""
209 + namespace + "\"/>\n"
210 )
211 schema_out.write(content)
Ed Tanous118b1c72018-09-13 13:45:51 -0700212 metadata_index.write(" </edmx:Reference>\n")
213
Ed Tanousf395daa2021-08-02 08:56:24 -0700214 metadata_index.write(" <edmx:DataServices>\n"
215 " <Schema "
216 "xmlns=\"http://docs.oasis-open.org/odata/ns/edm\" "
217 "Namespace=\"Service\">\n"
218 " <EntityContainer Name=\"Service\" "
219 "Extends=\"ServiceRoot.v1_0_0.ServiceContainer\"/>\n"
220 " </Schema>\n"
221 " </edmx:DataServices>\n"
222 )
Ed Tanouscb103132019-10-08 11:34:22 -0700223 # TODO:Issue#32 There's a bug in the script that currently deletes this
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500224 # schema (because it's an OEM schema). Because it's the only six, and we
Ed Tanouscb103132019-10-08 11:34:22 -0700225 # don't update schemas very often, we just manually fix it. Need a
226 # permanent fix to the script.
227 metadata_index.write(
228 " <edmx:Reference Uri=\"/redfish/v1/schema/OemManager_v1.xml\">\n")
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600229 metadata_index.write(" <edmx:Include Namespace=\"OemManager\"/>\n")
230 metadata_index.write(" </edmx:Reference>\n")
Gunnar Mills20778992020-02-06 16:36:47 -0600231
232 metadata_index.write(
Ed Tanousf395daa2021-08-02 08:56:24 -0700233 " <edmx:Reference Uri=\""
234 "/redfish/v1/schema/OemComputerSystem_v1.xml\">\n")
235 metadata_index.write(
236 " <edmx:Include Namespace=\"OemComputerSystem\"/>\n")
Gunnar Mills20778992020-02-06 16:36:47 -0600237 metadata_index.write(" </edmx:Reference>\n")
238
239 metadata_index.write(
Ed Tanousf395daa2021-08-02 08:56:24 -0700240 " <edmx:Reference Uri=\""
241 "/redfish/v1/schema/OemVirtualMedia_v1.xml\">\n")
242 metadata_index.write(
243 " <edmx:Include Namespace=\"OemVirtualMedia\"/>\n")
244 metadata_index.write(
245 " <edmx:Include Namespace=\"OemVirtualMedia.v1_0_0\"/>\n")
Gunnar Mills20778992020-02-06 16:36:47 -0600246 metadata_index.write(" </edmx:Reference>\n")
247
248 metadata_index.write(
Ed Tanousf395daa2021-08-02 08:56:24 -0700249 " <edmx:Reference Uri=\""
250 "/redfish/v1/schema/OemAccountService_v1.xml\">\n")
251 metadata_index.write(
252 " <edmx:Include Namespace=\"OemAccountService\"/>\n")
253 metadata_index.write(
254 " <edmx:Include Namespace=\"OemAccountService.v1_0_0\"/>\n")
Gunnar Mills20778992020-02-06 16:36:47 -0600255 metadata_index.write(" </edmx:Reference>\n")
256
Ravi Tejae7d68c32020-03-15 13:30:41 -0500257 metadata_index.write(
Sunitha Harish9dc50742020-05-11 00:10:20 -0500258 " <edmx:Reference Uri=\"/redfish/v1/schema/OemSession_v1.xml\">\n")
259 metadata_index.write(" <edmx:Include Namespace=\"OemSession\"/>\n")
Ed Tanousf395daa2021-08-02 08:56:24 -0700260 metadata_index.write(
261 " <edmx:Include Namespace=\"OemSession.v1_0_0\"/>\n")
Sunitha Harish9dc50742020-05-11 00:10:20 -0500262 metadata_index.write(" </edmx:Reference>\n")
263
Ed Tanous118b1c72018-09-13 13:45:51 -0700264 metadata_index.write("</edmx:Edmx>\n")
Ed Tanous683f7272018-07-26 12:47:19 -0700265
266schema_files = {}
267for zip_filepath in zip_ref.namelist():
Gunnar Mills60c922d2021-12-01 09:28:53 -0600268 if zip_filepath.startswith(os.path.join('json-schema/')):
Ed Tanous683f7272018-07-26 12:47:19 -0700269 filename = os.path.basename(zip_filepath)
270 filenamesplit = filename.split(".")
Gunnar Mills349a2ac2021-01-20 22:29:16 -0600271
272 # exclude schemas again to save flash space
273 if filenamesplit[0] not in include_list:
274 continue
275
Ed Tanous683f7272018-07-26 12:47:19 -0700276 if len(filenamesplit) == 3:
277 thisSchemaVersion = schema_files.get(filenamesplit[0], None)
Ed Tanouscb103132019-10-08 11:34:22 -0700278 if thisSchemaVersion is None:
Ed Tanous683f7272018-07-26 12:47:19 -0700279 schema_files[filenamesplit[0]] = filenamesplit[1]
280 else:
281 # need to see if we're a newer version.
282 if list(map(int, filenamesplit[1][1:].split("_"))) > list(map(
283 int, thisSchemaVersion[1:].split("_"))):
284 schema_files[filenamesplit[0]] = filenamesplit[1]
285
286
287for schema, version in schema_files.items():
288 basename = schema + "." + version + ".json"
Gunnar Mills60c922d2021-12-01 09:28:53 -0600289 zip_filepath = os.path.join("json-schema", basename)
Ed Tanous683f7272018-07-26 12:47:19 -0700290 schemadir = os.path.join(json_schema_path, schema)
291 os.makedirs(schemadir)
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600292 location_json = OrderedDict()
293 location_json["Language"] = "en"
294 location_json["PublicationUri"] = (
295 "http://redfish.dmtf.org/schemas/v1/" + schema + ".json")
296 location_json["Uri"] = (
Ed Tanous63faafa2019-01-03 14:09:56 -0800297 "/redfish/v1/JsonSchemas/" + schema + "/" + schema + ".json")
Ed Tanous118b1c72018-09-13 13:45:51 -0700298
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600299 index_json = OrderedDict()
Ed Tanousf395daa2021-08-02 08:56:24 -0700300 index_json["@odata.context"] = \
301 "/redfish/v1/$metadata#JsonSchemaFile.JsonSchemaFile"
Ed Tanous63faafa2019-01-03 14:09:56 -0800302 index_json["@odata.id"] = "/redfish/v1/JsonSchemas/" + schema
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600303 index_json["@odata.type"] = "#JsonSchemaFile.v1_0_2.JsonSchemaFile"
304 index_json["Name"] = schema + " Schema File"
305 index_json["Schema"] = "#" + schema + "." + schema
Ed Tanouscb103132019-10-08 11:34:22 -0700306 index_json["Description"] = schema + " Schema File Location"
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600307 index_json["Id"] = schema
Ed Tanouscb103132019-10-08 11:34:22 -0700308 index_json["Languages"] = ["en"]
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600309 index_json["Languages@odata.count"] = 1
Ed Tanouscb103132019-10-08 11:34:22 -0700310 index_json["Location"] = [location_json]
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600311 index_json["Location@odata.count"] = 1
Ed Tanous118b1c72018-09-13 13:45:51 -0700312
313 with open(os.path.join(schemadir, "index.json"), 'w') as schema_file:
314 json.dump(index_json, schema_file, indent=4)
315 with open(os.path.join(schemadir, schema + ".json"), 'wb') as schema_file:
Ed Tanouscb103132019-10-08 11:34:22 -0700316 schema_file.write(zip_ref.read(zip_filepath).replace(b'\r\n', b'\n'))
Ed Tanous683f7272018-07-26 12:47:19 -0700317
318with open(os.path.join(json_schema_path, "index.json"), 'w') as index_file:
Ed Tanous6f56d0c2019-05-03 17:15:41 -0700319 members = [{"@odata.id": "/redfish/v1/JsonSchemas/" + schema}
Ed Tanous683f7272018-07-26 12:47:19 -0700320 for schema in schema_files]
321
322 members.sort(key=lambda x: x["@odata.id"])
323
324 indexData = OrderedDict()
325
326 indexData["@odata.id"] = "/redfish/v1/JsonSchemas"
327 indexData["@odata.context"] = ("/redfish/v1/$metadata"
328 "#JsonSchemaFileCollection."
329 "JsonSchemaFileCollection")
330 indexData["@odata.type"] = ("#JsonSchemaFileCollection."
331 "JsonSchemaFileCollection")
332 indexData["Name"] = "JsonSchemaFile Collection"
333 indexData["Description"] = "Collection of JsonSchemaFiles"
334 indexData["Members@odata.count"] = len(schema_files)
335 indexData["Members"] = members
336
337 json.dump(indexData, index_file, indent=2)
338
339zip_ref.close()