blob: eb6318f91d55a0b2e1ae56ca0e1c2e4e84ee29af [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 Kazmierczak1b7e6962021-08-02 13:40:27 +020096 'Triggers',
Lukasz Kazmierczak07148cf2021-08-02 11:08:53 +020097 'TriggersCollection',
Gunnar Mills349a2ac2021-01-20 22:29:16 -060098 'UpdateService',
99 'VLanNetworkInterfaceCollection',
100 'VLanNetworkInterface',
101 'VirtualMedia',
102 'VirtualMediaCollection',
103 'odata',
104 'odata-v4',
105 'redfish-error',
106 'redfish-payload-annotations',
107 'redfish-schema',
108 'redfish-schema-v1',
109]
110
Ed Tanous683f7272018-07-26 12:47:19 -0700111SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
112
113proxies = {
114 'https': os.environ.get("https_proxy", None)
115}
116
Ed Tanouscb103132019-10-08 11:34:22 -0700117r = requests.get(
118 'https://www.dmtf.org/sites/default/files/standards/documents/' +
119 VERSION +
120 '.zip',
121 proxies=proxies)
Ed Tanous683f7272018-07-26 12:47:19 -0700122
123r.raise_for_status()
124
125static_path = os.path.realpath(os.path.join(SCRIPT_DIR, "..", "static",
126 "redfish", "v1"))
127
128schema_path = os.path.join(static_path, "schema")
129json_schema_path = os.path.join(static_path, "JsonSchemas")
Ed Tanous118b1c72018-09-13 13:45:51 -0700130metadata_index_path = os.path.join(static_path, "$metadata", "index.xml")
Ed Tanous683f7272018-07-26 12:47:19 -0700131
132zipBytesIO = BytesIO(r.content)
133zip_ref = zipfile.ZipFile(zipBytesIO)
134
135# Remove the old files
Szymon Dompked699cf92021-08-11 19:46:31 +0200136skip_prefixes = ('Oem')
Ed Tanous683f7272018-07-26 12:47:19 -0700137if os.path.exists(schema_path):
Szymon Dompked699cf92021-08-11 19:46:31 +0200138 files = [os.path.join(schema_path, f) for f in os.listdir(schema_path)
139 if not f.startswith(skip_prefixes)]
James Feistaee8d842018-09-10 16:07:40 -0700140 for f in files:
141 os.remove(f)
Ed Tanous683f7272018-07-26 12:47:19 -0700142if os.path.exists(json_schema_path):
Szymon Dompked699cf92021-08-11 19:46:31 +0200143 files = [os.path.join(json_schema_path, f) for f in
144 os.listdir(json_schema_path) if not f.startswith(skip_prefixes)]
Ed Tanous118b1c72018-09-13 13:45:51 -0700145 for f in files:
146 if (os.path.isfile(f)):
147 os.remove(f)
148 else:
149 shutil.rmtree(f)
150os.remove(metadata_index_path)
Ed Tanous683f7272018-07-26 12:47:19 -0700151
Ed Tanous118b1c72018-09-13 13:45:51 -0700152if not os.path.exists(schema_path):
153 os.makedirs(schema_path)
154if not os.path.exists(json_schema_path):
155 os.makedirs(json_schema_path)
Ed Tanous683f7272018-07-26 12:47:19 -0700156
Ed Tanous118b1c72018-09-13 13:45:51 -0700157with open(metadata_index_path, 'w') as metadata_index:
158
159 metadata_index.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
160 metadata_index.write(
Ed Tanousf395daa2021-08-02 08:56:24 -0700161 "<edmx:Edmx xmlns:edmx="
162 "\"http://docs.oasis-open.org/odata/ns/edmx\""
163 " Version=\"4.0\">\n")
Ed Tanous118b1c72018-09-13 13:45:51 -0700164
165 for zip_filepath in zip_ref.namelist():
Gunnar Mills60c922d2021-12-01 09:28:53 -0600166 if zip_filepath.startswith('csdl/') and \
Ed Tanousf395daa2021-08-02 08:56:24 -0700167 (zip_filepath != VERSION + "/csdl/") and \
Gunnar Mills60c922d2021-12-01 09:28:53 -0600168 (zip_filepath != "csdl/"):
Ed Tanous118b1c72018-09-13 13:45:51 -0700169 filename = os.path.basename(zip_filepath)
Gunnar Mills349a2ac2021-01-20 22:29:16 -0600170
171 # filename looks like Zone_v1.xml
172 filenamesplit = filename.split("_")
173 if filenamesplit[0] not in include_list:
174 print("excluding schema: " + filename)
175 continue
176
Ed Tanousf395daa2021-08-02 08:56:24 -0700177 with open(os.path.join(schema_path, filename), 'wb') as schema_out:
Ed Tanous118b1c72018-09-13 13:45:51 -0700178
179 metadata_index.write(
Ed Tanouscb103132019-10-08 11:34:22 -0700180 " <edmx:Reference Uri=\"/redfish/v1/schema/" +
181 filename +
182 "\">\n")
Ed Tanous118b1c72018-09-13 13:45:51 -0700183
184 content = zip_ref.read(zip_filepath)
Ed Tanouscb103132019-10-08 11:34:22 -0700185 content = content.replace(b'\r\n', b'\n')
Ed Tanous118b1c72018-09-13 13:45:51 -0700186 xml_root = ET.fromstring(content)
Ed Tanousf395daa2021-08-02 08:56:24 -0700187 edmx = "{http://docs.oasis-open.org/odata/ns/edmx}"
Szymon Dompked699cf92021-08-11 19:46:31 +0200188 edm = "{http://docs.oasis-open.org/odata/ns/edm}"
Ed Tanous118b1c72018-09-13 13:45:51 -0700189 for edmx_child in xml_root:
Ed Tanousf395daa2021-08-02 08:56:24 -0700190 if edmx_child.tag == edmx + "DataServices":
Ed Tanous118b1c72018-09-13 13:45:51 -0700191 for data_child in edmx_child:
Szymon Dompked699cf92021-08-11 19:46:31 +0200192 if data_child.tag == edm + "Schema":
Ed Tanous118b1c72018-09-13 13:45:51 -0700193 namespace = data_child.attrib["Namespace"]
194 if namespace.startswith("RedfishExtensions"):
195 metadata_index.write(
Ed Tanousf395daa2021-08-02 08:56:24 -0700196 " "
197 "<edmx:Include Namespace=\"" +
198 namespace +
199 "\" Alias=\"Redfish\"/>\n"
200 )
Ed Tanous118b1c72018-09-13 13:45:51 -0700201
202 else:
203 metadata_index.write(
Ed Tanousf395daa2021-08-02 08:56:24 -0700204 " "
205 "<edmx:Include Namespace=\""
206 + namespace + "\"/>\n"
207 )
208 schema_out.write(content)
Ed Tanous118b1c72018-09-13 13:45:51 -0700209 metadata_index.write(" </edmx:Reference>\n")
210
Ed Tanousf395daa2021-08-02 08:56:24 -0700211 metadata_index.write(" <edmx:DataServices>\n"
212 " <Schema "
213 "xmlns=\"http://docs.oasis-open.org/odata/ns/edm\" "
214 "Namespace=\"Service\">\n"
215 " <EntityContainer Name=\"Service\" "
216 "Extends=\"ServiceRoot.v1_0_0.ServiceContainer\"/>\n"
217 " </Schema>\n"
218 " </edmx:DataServices>\n"
219 )
Ed Tanouscb103132019-10-08 11:34:22 -0700220 # TODO:Issue#32 There's a bug in the script that currently deletes this
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500221 # schema (because it's an OEM schema). Because it's the only six, and we
Ed Tanouscb103132019-10-08 11:34:22 -0700222 # don't update schemas very often, we just manually fix it. Need a
223 # permanent fix to the script.
224 metadata_index.write(
225 " <edmx:Reference Uri=\"/redfish/v1/schema/OemManager_v1.xml\">\n")
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600226 metadata_index.write(" <edmx:Include Namespace=\"OemManager\"/>\n")
227 metadata_index.write(" </edmx:Reference>\n")
Gunnar Mills20778992020-02-06 16:36:47 -0600228
229 metadata_index.write(
Ed Tanousf395daa2021-08-02 08:56:24 -0700230 " <edmx:Reference Uri=\""
231 "/redfish/v1/schema/OemComputerSystem_v1.xml\">\n")
232 metadata_index.write(
233 " <edmx:Include Namespace=\"OemComputerSystem\"/>\n")
Gunnar Mills20778992020-02-06 16:36:47 -0600234 metadata_index.write(" </edmx:Reference>\n")
235
236 metadata_index.write(
Ed Tanousf395daa2021-08-02 08:56:24 -0700237 " <edmx:Reference Uri=\""
238 "/redfish/v1/schema/OemVirtualMedia_v1.xml\">\n")
239 metadata_index.write(
240 " <edmx:Include Namespace=\"OemVirtualMedia\"/>\n")
241 metadata_index.write(
242 " <edmx:Include Namespace=\"OemVirtualMedia.v1_0_0\"/>\n")
Gunnar Mills20778992020-02-06 16:36:47 -0600243 metadata_index.write(" </edmx:Reference>\n")
244
245 metadata_index.write(
Ed Tanousf395daa2021-08-02 08:56:24 -0700246 " <edmx:Reference Uri=\""
247 "/redfish/v1/schema/OemAccountService_v1.xml\">\n")
248 metadata_index.write(
249 " <edmx:Include Namespace=\"OemAccountService\"/>\n")
250 metadata_index.write(
251 " <edmx:Include Namespace=\"OemAccountService.v1_0_0\"/>\n")
Gunnar Mills20778992020-02-06 16:36:47 -0600252 metadata_index.write(" </edmx:Reference>\n")
253
Ravi Tejae7d68c32020-03-15 13:30:41 -0500254 metadata_index.write(
Sunitha Harish9dc50742020-05-11 00:10:20 -0500255 " <edmx:Reference Uri=\"/redfish/v1/schema/OemSession_v1.xml\">\n")
256 metadata_index.write(" <edmx:Include Namespace=\"OemSession\"/>\n")
Ed Tanousf395daa2021-08-02 08:56:24 -0700257 metadata_index.write(
258 " <edmx:Include Namespace=\"OemSession.v1_0_0\"/>\n")
Sunitha Harish9dc50742020-05-11 00:10:20 -0500259 metadata_index.write(" </edmx:Reference>\n")
260
Ed Tanous118b1c72018-09-13 13:45:51 -0700261 metadata_index.write("</edmx:Edmx>\n")
Ed Tanous683f7272018-07-26 12:47:19 -0700262
263schema_files = {}
264for zip_filepath in zip_ref.namelist():
Gunnar Mills60c922d2021-12-01 09:28:53 -0600265 if zip_filepath.startswith(os.path.join('json-schema/')):
Ed Tanous683f7272018-07-26 12:47:19 -0700266 filename = os.path.basename(zip_filepath)
267 filenamesplit = filename.split(".")
Gunnar Mills349a2ac2021-01-20 22:29:16 -0600268
269 # exclude schemas again to save flash space
270 if filenamesplit[0] not in include_list:
271 continue
272
Ed Tanous683f7272018-07-26 12:47:19 -0700273 if len(filenamesplit) == 3:
274 thisSchemaVersion = schema_files.get(filenamesplit[0], None)
Ed Tanouscb103132019-10-08 11:34:22 -0700275 if thisSchemaVersion is None:
Ed Tanous683f7272018-07-26 12:47:19 -0700276 schema_files[filenamesplit[0]] = filenamesplit[1]
277 else:
278 # need to see if we're a newer version.
279 if list(map(int, filenamesplit[1][1:].split("_"))) > list(map(
280 int, thisSchemaVersion[1:].split("_"))):
281 schema_files[filenamesplit[0]] = filenamesplit[1]
282
283
284for schema, version in schema_files.items():
285 basename = schema + "." + version + ".json"
Gunnar Mills60c922d2021-12-01 09:28:53 -0600286 zip_filepath = os.path.join("json-schema", basename)
Ed Tanous683f7272018-07-26 12:47:19 -0700287 schemadir = os.path.join(json_schema_path, schema)
288 os.makedirs(schemadir)
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600289 location_json = OrderedDict()
290 location_json["Language"] = "en"
291 location_json["PublicationUri"] = (
292 "http://redfish.dmtf.org/schemas/v1/" + schema + ".json")
293 location_json["Uri"] = (
Ed Tanous63faafa2019-01-03 14:09:56 -0800294 "/redfish/v1/JsonSchemas/" + schema + "/" + schema + ".json")
Ed Tanous118b1c72018-09-13 13:45:51 -0700295
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600296 index_json = OrderedDict()
Ed Tanousf395daa2021-08-02 08:56:24 -0700297 index_json["@odata.context"] = \
298 "/redfish/v1/$metadata#JsonSchemaFile.JsonSchemaFile"
Ed Tanous63faafa2019-01-03 14:09:56 -0800299 index_json["@odata.id"] = "/redfish/v1/JsonSchemas/" + schema
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600300 index_json["@odata.type"] = "#JsonSchemaFile.v1_0_2.JsonSchemaFile"
301 index_json["Name"] = schema + " Schema File"
302 index_json["Schema"] = "#" + schema + "." + schema
Ed Tanouscb103132019-10-08 11:34:22 -0700303 index_json["Description"] = schema + " Schema File Location"
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600304 index_json["Id"] = schema
Ed Tanouscb103132019-10-08 11:34:22 -0700305 index_json["Languages"] = ["en"]
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600306 index_json["Languages@odata.count"] = 1
Ed Tanouscb103132019-10-08 11:34:22 -0700307 index_json["Location"] = [location_json]
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600308 index_json["Location@odata.count"] = 1
Ed Tanous118b1c72018-09-13 13:45:51 -0700309
310 with open(os.path.join(schemadir, "index.json"), 'w') as schema_file:
311 json.dump(index_json, schema_file, indent=4)
312 with open(os.path.join(schemadir, schema + ".json"), 'wb') as schema_file:
Ed Tanouscb103132019-10-08 11:34:22 -0700313 schema_file.write(zip_ref.read(zip_filepath).replace(b'\r\n', b'\n'))
Ed Tanous683f7272018-07-26 12:47:19 -0700314
315with open(os.path.join(json_schema_path, "index.json"), 'w') as index_file:
Ed Tanous6f56d0c2019-05-03 17:15:41 -0700316 members = [{"@odata.id": "/redfish/v1/JsonSchemas/" + schema}
Ed Tanous683f7272018-07-26 12:47:19 -0700317 for schema in schema_files]
318
319 members.sort(key=lambda x: x["@odata.id"])
320
321 indexData = OrderedDict()
322
323 indexData["@odata.id"] = "/redfish/v1/JsonSchemas"
324 indexData["@odata.context"] = ("/redfish/v1/$metadata"
325 "#JsonSchemaFileCollection."
326 "JsonSchemaFileCollection")
327 indexData["@odata.type"] = ("#JsonSchemaFileCollection."
328 "JsonSchemaFileCollection")
329 indexData["Name"] = "JsonSchemaFile Collection"
330 indexData["Description"] = "Collection of JsonSchemaFiles"
331 indexData["Members@odata.count"] = len(schema_files)
332 indexData["Members"] = members
333
334 json.dump(indexData, index_file, indent=2)
335
336zip_ref.close()