blob: af62962439d64af073f1bb8bf5b6a322d58daeb2 [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
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',
Sui Chen23952512022-02-08 13:49:36 -080050 'ManagerDiagnosticData',
Gunnar Mills349a2ac2021-01-20 22:29:16 -060051 'ManagerNetworkProtocol',
52 'Memory',
53 'MemoryCollection',
54 'Message',
55 'MessageRegistry',
56 'MessageRegistryCollection',
57 'MessageRegistryFile',
58 'MessageRegistryFileCollection',
59 'MetricDefinition',
60 'MetricDefinitionCollection',
61 'MetricReport',
62 'MetricReportCollection',
63 'MetricReportDefinition',
64 'MetricReportDefinitionCollection',
65 'OperatingConfig',
66 'OperatingConfigCollection',
67 'PCIeDevice',
68 'PCIeDeviceCollection',
69 'PCIeFunction',
70 'PCIeFunctionCollection',
sunitakx71b861b2022-01-18 11:05:05 +000071 'PhysicalContext',
Ed Tanous08eca732022-06-09 14:32:18 -070072 'PCIeSlots',
Gunnar Mills349a2ac2021-01-20 22:29:16 -060073 'Power',
Ed Tanousf395daa2021-08-02 08:56:24 -070074 'Privileges', # Used in Role
Gunnar Mills349a2ac2021-01-20 22:29:16 -060075 'Processor',
76 'ProcessorCollection',
77 'RedfishError',
78 'RedfishExtensions',
79 'Redundancy',
80 'Resource',
81 'Role',
82 'RoleCollection',
83 'Sensor',
84 'SensorCollection',
85 'ServiceRoot',
86 'Session',
87 'SessionCollection',
88 'SessionService',
89 'Settings',
90 'SoftwareInventory',
91 'SoftwareInventoryCollection',
92 'Storage',
93 'StorageCollection',
94 'StorageController',
95 'StorageControllerCollection',
96 'Task',
97 'TaskCollection',
98 'TaskService',
99 'TelemetryService',
100 'Thermal',
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +0200101 'Triggers',
Lukasz Kazmierczak07148cf2021-08-02 11:08:53 +0200102 'TriggersCollection',
Gunnar Mills349a2ac2021-01-20 22:29:16 -0600103 'UpdateService',
104 'VLanNetworkInterfaceCollection',
105 'VLanNetworkInterface',
106 'VirtualMedia',
107 'VirtualMediaCollection',
108 'odata',
109 'odata-v4',
110 'redfish-error',
111 'redfish-payload-annotations',
112 'redfish-schema',
113 'redfish-schema-v1',
114]
115
Ed Tanous683f7272018-07-26 12:47:19 -0700116SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
117
118proxies = {
119 'https': os.environ.get("https_proxy", None)
120}
121
Ed Tanouscb103132019-10-08 11:34:22 -0700122r = requests.get(
123 'https://www.dmtf.org/sites/default/files/standards/documents/' +
124 VERSION +
125 '.zip',
126 proxies=proxies)
Ed Tanous683f7272018-07-26 12:47:19 -0700127
128r.raise_for_status()
129
130static_path = os.path.realpath(os.path.join(SCRIPT_DIR, "..", "static",
131 "redfish", "v1"))
132
133schema_path = os.path.join(static_path, "schema")
134json_schema_path = os.path.join(static_path, "JsonSchemas")
Ed Tanous118b1c72018-09-13 13:45:51 -0700135metadata_index_path = os.path.join(static_path, "$metadata", "index.xml")
Ed Tanous683f7272018-07-26 12:47:19 -0700136
137zipBytesIO = BytesIO(r.content)
138zip_ref = zipfile.ZipFile(zipBytesIO)
139
140# Remove the old files
Szymon Dompked699cf92021-08-11 19:46:31 +0200141skip_prefixes = ('Oem')
Ed Tanous683f7272018-07-26 12:47:19 -0700142if os.path.exists(schema_path):
Szymon Dompked699cf92021-08-11 19:46:31 +0200143 files = [os.path.join(schema_path, f) for f in os.listdir(schema_path)
144 if not f.startswith(skip_prefixes)]
James Feistaee8d842018-09-10 16:07:40 -0700145 for f in files:
146 os.remove(f)
Ed Tanous683f7272018-07-26 12:47:19 -0700147if os.path.exists(json_schema_path):
Szymon Dompked699cf92021-08-11 19:46:31 +0200148 files = [os.path.join(json_schema_path, f) for f in
149 os.listdir(json_schema_path) if not f.startswith(skip_prefixes)]
Ed Tanous118b1c72018-09-13 13:45:51 -0700150 for f in files:
151 if (os.path.isfile(f)):
152 os.remove(f)
153 else:
154 shutil.rmtree(f)
155os.remove(metadata_index_path)
Ed Tanous683f7272018-07-26 12:47:19 -0700156
Ed Tanous118b1c72018-09-13 13:45:51 -0700157if not os.path.exists(schema_path):
158 os.makedirs(schema_path)
159if not os.path.exists(json_schema_path):
160 os.makedirs(json_schema_path)
Ed Tanous683f7272018-07-26 12:47:19 -0700161
Ed Tanous118b1c72018-09-13 13:45:51 -0700162with open(metadata_index_path, 'w') as metadata_index:
163
164 metadata_index.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
165 metadata_index.write(
Ed Tanousf395daa2021-08-02 08:56:24 -0700166 "<edmx:Edmx xmlns:edmx="
167 "\"http://docs.oasis-open.org/odata/ns/edmx\""
168 " Version=\"4.0\">\n")
Ed Tanous118b1c72018-09-13 13:45:51 -0700169
170 for zip_filepath in zip_ref.namelist():
Gunnar Mills60c922d2021-12-01 09:28:53 -0600171 if zip_filepath.startswith('csdl/') and \
Ed Tanousf395daa2021-08-02 08:56:24 -0700172 (zip_filepath != VERSION + "/csdl/") and \
Gunnar Mills60c922d2021-12-01 09:28:53 -0600173 (zip_filepath != "csdl/"):
Ed Tanous118b1c72018-09-13 13:45:51 -0700174 filename = os.path.basename(zip_filepath)
Gunnar Mills349a2ac2021-01-20 22:29:16 -0600175
176 # filename looks like Zone_v1.xml
177 filenamesplit = filename.split("_")
178 if filenamesplit[0] not in include_list:
179 print("excluding schema: " + filename)
180 continue
181
Ed Tanousf395daa2021-08-02 08:56:24 -0700182 with open(os.path.join(schema_path, filename), 'wb') as schema_out:
Ed Tanous118b1c72018-09-13 13:45:51 -0700183
184 metadata_index.write(
Ed Tanouscb103132019-10-08 11:34:22 -0700185 " <edmx:Reference Uri=\"/redfish/v1/schema/" +
186 filename +
187 "\">\n")
Ed Tanous118b1c72018-09-13 13:45:51 -0700188
189 content = zip_ref.read(zip_filepath)
Ed Tanouscb103132019-10-08 11:34:22 -0700190 content = content.replace(b'\r\n', b'\n')
Ed Tanous118b1c72018-09-13 13:45:51 -0700191 xml_root = ET.fromstring(content)
Ed Tanousf395daa2021-08-02 08:56:24 -0700192 edmx = "{http://docs.oasis-open.org/odata/ns/edmx}"
Szymon Dompked699cf92021-08-11 19:46:31 +0200193 edm = "{http://docs.oasis-open.org/odata/ns/edm}"
Ed Tanous118b1c72018-09-13 13:45:51 -0700194 for edmx_child in xml_root:
Ed Tanousf395daa2021-08-02 08:56:24 -0700195 if edmx_child.tag == edmx + "DataServices":
Ed Tanous118b1c72018-09-13 13:45:51 -0700196 for data_child in edmx_child:
Szymon Dompked699cf92021-08-11 19:46:31 +0200197 if data_child.tag == edm + "Schema":
Ed Tanous118b1c72018-09-13 13:45:51 -0700198 namespace = data_child.attrib["Namespace"]
199 if namespace.startswith("RedfishExtensions"):
200 metadata_index.write(
Ed Tanousf395daa2021-08-02 08:56:24 -0700201 " "
202 "<edmx:Include Namespace=\"" +
203 namespace +
204 "\" Alias=\"Redfish\"/>\n"
205 )
Ed Tanous118b1c72018-09-13 13:45:51 -0700206
207 else:
208 metadata_index.write(
Ed Tanousf395daa2021-08-02 08:56:24 -0700209 " "
210 "<edmx:Include Namespace=\""
211 + namespace + "\"/>\n"
212 )
213 schema_out.write(content)
Ed Tanous118b1c72018-09-13 13:45:51 -0700214 metadata_index.write(" </edmx:Reference>\n")
215
Ed Tanousf395daa2021-08-02 08:56:24 -0700216 metadata_index.write(" <edmx:DataServices>\n"
217 " <Schema "
218 "xmlns=\"http://docs.oasis-open.org/odata/ns/edm\" "
219 "Namespace=\"Service\">\n"
220 " <EntityContainer Name=\"Service\" "
221 "Extends=\"ServiceRoot.v1_0_0.ServiceContainer\"/>\n"
222 " </Schema>\n"
223 " </edmx:DataServices>\n"
224 )
Ed Tanouscb103132019-10-08 11:34:22 -0700225 # TODO:Issue#32 There's a bug in the script that currently deletes this
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500226 # schema (because it's an OEM schema). Because it's the only six, and we
Ed Tanouscb103132019-10-08 11:34:22 -0700227 # don't update schemas very often, we just manually fix it. Need a
228 # permanent fix to the script.
229 metadata_index.write(
230 " <edmx:Reference Uri=\"/redfish/v1/schema/OemManager_v1.xml\">\n")
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600231 metadata_index.write(" <edmx:Include Namespace=\"OemManager\"/>\n")
232 metadata_index.write(" </edmx:Reference>\n")
Gunnar Mills20778992020-02-06 16:36:47 -0600233
234 metadata_index.write(
Ed Tanousf395daa2021-08-02 08:56:24 -0700235 " <edmx:Reference Uri=\""
236 "/redfish/v1/schema/OemComputerSystem_v1.xml\">\n")
237 metadata_index.write(
238 " <edmx:Include Namespace=\"OemComputerSystem\"/>\n")
Gunnar Mills20778992020-02-06 16:36:47 -0600239 metadata_index.write(" </edmx:Reference>\n")
240
241 metadata_index.write(
Ed Tanousf395daa2021-08-02 08:56:24 -0700242 " <edmx:Reference Uri=\""
243 "/redfish/v1/schema/OemVirtualMedia_v1.xml\">\n")
244 metadata_index.write(
245 " <edmx:Include Namespace=\"OemVirtualMedia\"/>\n")
246 metadata_index.write(
247 " <edmx:Include Namespace=\"OemVirtualMedia.v1_0_0\"/>\n")
Gunnar Mills20778992020-02-06 16:36:47 -0600248 metadata_index.write(" </edmx:Reference>\n")
249
250 metadata_index.write(
Ed Tanousf395daa2021-08-02 08:56:24 -0700251 " <edmx:Reference Uri=\""
252 "/redfish/v1/schema/OemAccountService_v1.xml\">\n")
253 metadata_index.write(
254 " <edmx:Include Namespace=\"OemAccountService\"/>\n")
255 metadata_index.write(
256 " <edmx:Include Namespace=\"OemAccountService.v1_0_0\"/>\n")
Gunnar Mills20778992020-02-06 16:36:47 -0600257 metadata_index.write(" </edmx:Reference>\n")
258
Ravi Tejae7d68c32020-03-15 13:30:41 -0500259 metadata_index.write(
Sunitha Harish9dc50742020-05-11 00:10:20 -0500260 " <edmx:Reference Uri=\"/redfish/v1/schema/OemSession_v1.xml\">\n")
261 metadata_index.write(" <edmx:Include Namespace=\"OemSession\"/>\n")
Ed Tanousf395daa2021-08-02 08:56:24 -0700262 metadata_index.write(
263 " <edmx:Include Namespace=\"OemSession.v1_0_0\"/>\n")
Sunitha Harish9dc50742020-05-11 00:10:20 -0500264 metadata_index.write(" </edmx:Reference>\n")
265
Ed Tanous118b1c72018-09-13 13:45:51 -0700266 metadata_index.write("</edmx:Edmx>\n")
Ed Tanous683f7272018-07-26 12:47:19 -0700267
268schema_files = {}
269for zip_filepath in zip_ref.namelist():
Gunnar Mills60c922d2021-12-01 09:28:53 -0600270 if zip_filepath.startswith(os.path.join('json-schema/')):
Ed Tanous683f7272018-07-26 12:47:19 -0700271 filename = os.path.basename(zip_filepath)
272 filenamesplit = filename.split(".")
Gunnar Mills349a2ac2021-01-20 22:29:16 -0600273
274 # exclude schemas again to save flash space
275 if filenamesplit[0] not in include_list:
276 continue
277
Ed Tanous683f7272018-07-26 12:47:19 -0700278 if len(filenamesplit) == 3:
279 thisSchemaVersion = schema_files.get(filenamesplit[0], None)
Ed Tanouscb103132019-10-08 11:34:22 -0700280 if thisSchemaVersion is None:
Ed Tanous683f7272018-07-26 12:47:19 -0700281 schema_files[filenamesplit[0]] = filenamesplit[1]
282 else:
283 # need to see if we're a newer version.
284 if list(map(int, filenamesplit[1][1:].split("_"))) > list(map(
285 int, thisSchemaVersion[1:].split("_"))):
286 schema_files[filenamesplit[0]] = filenamesplit[1]
287
288
289for schema, version in schema_files.items():
290 basename = schema + "." + version + ".json"
Gunnar Mills60c922d2021-12-01 09:28:53 -0600291 zip_filepath = os.path.join("json-schema", basename)
Ed Tanous683f7272018-07-26 12:47:19 -0700292 schemadir = os.path.join(json_schema_path, schema)
293 os.makedirs(schemadir)
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600294 location_json = OrderedDict()
295 location_json["Language"] = "en"
296 location_json["PublicationUri"] = (
297 "http://redfish.dmtf.org/schemas/v1/" + schema + ".json")
298 location_json["Uri"] = (
Ed Tanous63faafa2019-01-03 14:09:56 -0800299 "/redfish/v1/JsonSchemas/" + schema + "/" + schema + ".json")
Ed Tanous118b1c72018-09-13 13:45:51 -0700300
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600301 index_json = OrderedDict()
Ed Tanousf395daa2021-08-02 08:56:24 -0700302 index_json["@odata.context"] = \
303 "/redfish/v1/$metadata#JsonSchemaFile.JsonSchemaFile"
Ed Tanous63faafa2019-01-03 14:09:56 -0800304 index_json["@odata.id"] = "/redfish/v1/JsonSchemas/" + schema
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600305 index_json["@odata.type"] = "#JsonSchemaFile.v1_0_2.JsonSchemaFile"
306 index_json["Name"] = schema + " Schema File"
307 index_json["Schema"] = "#" + schema + "." + schema
Ed Tanouscb103132019-10-08 11:34:22 -0700308 index_json["Description"] = schema + " Schema File Location"
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600309 index_json["Id"] = schema
Ed Tanouscb103132019-10-08 11:34:22 -0700310 index_json["Languages"] = ["en"]
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600311 index_json["Languages@odata.count"] = 1
Ed Tanouscb103132019-10-08 11:34:22 -0700312 index_json["Location"] = [location_json]
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600313 index_json["Location@odata.count"] = 1
Ed Tanous118b1c72018-09-13 13:45:51 -0700314
315 with open(os.path.join(schemadir, "index.json"), 'w') as schema_file:
316 json.dump(index_json, schema_file, indent=4)
317 with open(os.path.join(schemadir, schema + ".json"), 'wb') as schema_file:
Ed Tanouscb103132019-10-08 11:34:22 -0700318 schema_file.write(zip_ref.read(zip_filepath).replace(b'\r\n', b'\n'))
Ed Tanous683f7272018-07-26 12:47:19 -0700319
320with open(os.path.join(json_schema_path, "index.json"), 'w') as index_file:
Ed Tanous6f56d0c2019-05-03 17:15:41 -0700321 members = [{"@odata.id": "/redfish/v1/JsonSchemas/" + schema}
Ed Tanous683f7272018-07-26 12:47:19 -0700322 for schema in schema_files]
323
324 members.sort(key=lambda x: x["@odata.id"])
325
326 indexData = OrderedDict()
327
328 indexData["@odata.id"] = "/redfish/v1/JsonSchemas"
329 indexData["@odata.context"] = ("/redfish/v1/$metadata"
330 "#JsonSchemaFileCollection."
331 "JsonSchemaFileCollection")
332 indexData["@odata.type"] = ("#JsonSchemaFileCollection."
333 "JsonSchemaFileCollection")
334 indexData["Name"] = "JsonSchemaFile Collection"
335 indexData["Description"] = "Collection of JsonSchemaFiles"
336 indexData["Members@odata.count"] = len(schema_files)
337 indexData["Members"] = members
338
339 json.dump(indexData, index_file, indent=2)
340
341zip_ref.close()