blob: a694549474c0754b03d2bab804c78596830f4af3 [file] [log] [blame]
Ed Tanous683f7272018-07-26 12:47:19 -07001#!/usr/bin/python3
2import requests
3import zipfile
4from io import BytesIO
5import os
6from collections import defaultdict
7from collections import OrderedDict
8from distutils.version import StrictVersion
9import shutil
10import json
James Feistaee8d842018-09-10 16:07:40 -070011import glob
Ed Tanous683f7272018-07-26 12:47:19 -070012
Ed Tanous118b1c72018-09-13 13:45:51 -070013import xml.etree.ElementTree as ET
14
Gunnar Mills10f270b2021-05-19 15:34:06 -050015VERSION = "DSP8010_2021.1"
Ed Tanouscb103132019-10-08 11:34:22 -070016
Gunnar Mills349a2ac2021-01-20 22:29:16 -060017# To use a new schema, add to list and rerun tool
18include_list = [
19 'AccountService',
20 'ActionInfo',
21 'Assembly',
22 'AttributeRegistry',
23 'Bios',
24 'Certificate',
25 'CertificateCollection',
26 'CertificateLocations',
27 'CertificateService',
28 'Chassis',
29 'ChassisCollection',
30 'ComputerSystem',
31 'ComputerSystemCollection',
32 'Drive',
33 'DriveCollection',
34 'EthernetInterface',
35 'EthernetInterfaceCollection',
36 'Event',
37 'EventDestination',
38 'EventDestinationCollection',
39 'EventService',
40 'IPAddresses',
41 'JsonSchemaFile',
42 'JsonSchemaFileCollection', #redfish/v1/JsonSchemas
43 'LogEntry',
44 'LogEntryCollection',
45 'LogService',
46 'LogServiceCollection',
47 'Manager',
48 'ManagerAccount',
49 'ManagerAccountCollection',
50 'ManagerCollection',
51 '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',
71 'Power',
72 'Processor',
73 'ProcessorCollection',
74 'RedfishError',
75 'RedfishExtensions',
76 'Redundancy',
77 'Resource',
78 'Role',
79 'RoleCollection',
80 'Sensor',
81 'SensorCollection',
82 'ServiceRoot',
83 'Session',
84 'SessionCollection',
85 'SessionService',
86 'Settings',
87 'SoftwareInventory',
88 'SoftwareInventoryCollection',
89 'Storage',
90 'StorageCollection',
91 'StorageController',
92 'StorageControllerCollection',
93 'Task',
94 'TaskCollection',
95 'TaskService',
96 'TelemetryService',
97 'Thermal',
98 '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
136if os.path.exists(schema_path):
James Feistaee8d842018-09-10 16:07:40 -0700137 files = glob.glob(os.path.join(schema_path, '[!Oem]*'))
138 for f in files:
139 os.remove(f)
Ed Tanous683f7272018-07-26 12:47:19 -0700140if os.path.exists(json_schema_path):
Ed Tanous118b1c72018-09-13 13:45:51 -0700141 files = glob.glob(os.path.join(json_schema_path, '[!Oem]*'))
142 for f in files:
143 if (os.path.isfile(f)):
144 os.remove(f)
145 else:
146 shutil.rmtree(f)
147os.remove(metadata_index_path)
Ed Tanous683f7272018-07-26 12:47:19 -0700148
Ed Tanous118b1c72018-09-13 13:45:51 -0700149if not os.path.exists(schema_path):
150 os.makedirs(schema_path)
151if not os.path.exists(json_schema_path):
152 os.makedirs(json_schema_path)
Ed Tanous683f7272018-07-26 12:47:19 -0700153
Ed Tanous118b1c72018-09-13 13:45:51 -0700154with open(metadata_index_path, 'w') as metadata_index:
155
156 metadata_index.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
157 metadata_index.write(
158 "<edmx:Edmx xmlns:edmx=\"http://docs.oasis-open.org/odata/ns/edmx\" Version=\"4.0\">\n")
159
160 for zip_filepath in zip_ref.namelist():
Ed Tanouscb103132019-10-08 11:34:22 -0700161 if zip_filepath.startswith(VERSION +
Ed Tanouscb103132019-10-08 11:34:22 -0700162 '/csdl/') & (zip_filepath != VERSION +
163 "/csdl/") & (zip_filepath != VERSION +
Ed Tanouscb103132019-10-08 11:34:22 -0700164 "/csdl/"):
Ed Tanous118b1c72018-09-13 13:45:51 -0700165 filename = os.path.basename(zip_filepath)
Gunnar Mills349a2ac2021-01-20 22:29:16 -0600166
167 # filename looks like Zone_v1.xml
168 filenamesplit = filename.split("_")
169 if filenamesplit[0] not in include_list:
170 print("excluding schema: " + filename)
171 continue
172
Ed Tanous118b1c72018-09-13 13:45:51 -0700173 with open(os.path.join(schema_path, filename), 'wb') as schema_file:
174
175 metadata_index.write(
Ed Tanouscb103132019-10-08 11:34:22 -0700176 " <edmx:Reference Uri=\"/redfish/v1/schema/" +
177 filename +
178 "\">\n")
Ed Tanous118b1c72018-09-13 13:45:51 -0700179
180 content = zip_ref.read(zip_filepath)
Ed Tanouscb103132019-10-08 11:34:22 -0700181 content = content.replace(b'\r\n', b'\n')
Ed Tanous118b1c72018-09-13 13:45:51 -0700182 xml_root = ET.fromstring(content)
183
184 for edmx_child in xml_root:
185
186 if edmx_child.tag == "{http://docs.oasis-open.org/odata/ns/edmx}DataServices":
187 for data_child in edmx_child:
188 if data_child.tag == "{http://docs.oasis-open.org/odata/ns/edm}Schema":
189 namespace = data_child.attrib["Namespace"]
190 if namespace.startswith("RedfishExtensions"):
191 metadata_index.write(
192 " <edmx:Include Namespace=\"" + namespace + "\" Alias=\"Redfish\"/>\n")
193
194 else:
195 metadata_index.write(
196 " <edmx:Include Namespace=\"" + namespace + "\"/>\n")
197 schema_file.write(content)
198 metadata_index.write(" </edmx:Reference>\n")
199
200 metadata_index.write(""" <edmx:DataServices>
201 <Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="Service">
202 <EntityContainer Name="Service" Extends="ServiceRoot.v1_0_0.ServiceContainer"/>
203 </Schema>
204 </edmx:DataServices>
205""")
Ed Tanouscb103132019-10-08 11:34:22 -0700206 # TODO:Issue#32 There's a bug in the script that currently deletes this
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500207 # schema (because it's an OEM schema). Because it's the only six, and we
Ed Tanouscb103132019-10-08 11:34:22 -0700208 # don't update schemas very often, we just manually fix it. Need a
209 # permanent fix to the script.
210 metadata_index.write(
211 " <edmx:Reference Uri=\"/redfish/v1/schema/OemManager_v1.xml\">\n")
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600212 metadata_index.write(" <edmx:Include Namespace=\"OemManager\"/>\n")
213 metadata_index.write(" </edmx:Reference>\n")
Gunnar Mills20778992020-02-06 16:36:47 -0600214
215 metadata_index.write(
Gunnar Mills20778992020-02-06 16:36:47 -0600216 " <edmx:Reference Uri=\"/redfish/v1/schema/OemComputerSystem_v1.xml\">\n")
217 metadata_index.write(" <edmx:Include Namespace=\"OemComputerSystem\"/>\n")
218 metadata_index.write(" </edmx:Reference>\n")
219
220 metadata_index.write(
221 " <edmx:Reference Uri=\"/redfish/v1/schema/OemVirtualMedia_v1.xml\">\n")
222 metadata_index.write(" <edmx:Include Namespace=\"OemVirtualMedia\"/>\n")
223 metadata_index.write(" <edmx:Include Namespace=\"OemVirtualMedia.v1_0_0\"/>\n")
224 metadata_index.write(" </edmx:Reference>\n")
225
226 metadata_index.write(
227 " <edmx:Reference Uri=\"/redfish/v1/schema/OemAccountService_v1.xml\">\n")
228 metadata_index.write(" <edmx:Include Namespace=\"OemAccountService\"/>\n")
229 metadata_index.write(" <edmx:Include Namespace=\"OemAccountService.v1_0_0\"/>\n")
230 metadata_index.write(" </edmx:Reference>\n")
231
Ravi Tejae7d68c32020-03-15 13:30:41 -0500232 metadata_index.write(
Sunitha Harish9dc50742020-05-11 00:10:20 -0500233 " <edmx:Reference Uri=\"/redfish/v1/schema/OemSession_v1.xml\">\n")
234 metadata_index.write(" <edmx:Include Namespace=\"OemSession\"/>\n")
235 metadata_index.write(" <edmx:Include Namespace=\"OemSession.v1_0_0\"/>\n")
236 metadata_index.write(" </edmx:Reference>\n")
237
Ed Tanous118b1c72018-09-13 13:45:51 -0700238 metadata_index.write("</edmx:Edmx>\n")
Ed Tanous683f7272018-07-26 12:47:19 -0700239
240schema_files = {}
241for zip_filepath in zip_ref.namelist():
Gunnar Millsa778c022020-05-12 12:20:36 -0500242 if zip_filepath.startswith(os.path.join(VERSION, 'json-schema/')):
Ed Tanous683f7272018-07-26 12:47:19 -0700243 filename = os.path.basename(zip_filepath)
244 filenamesplit = filename.split(".")
Gunnar Mills349a2ac2021-01-20 22:29:16 -0600245
246 # exclude schemas again to save flash space
247 if filenamesplit[0] not in include_list:
248 continue
249
Ed Tanous683f7272018-07-26 12:47:19 -0700250 if len(filenamesplit) == 3:
251 thisSchemaVersion = schema_files.get(filenamesplit[0], None)
Ed Tanouscb103132019-10-08 11:34:22 -0700252 if thisSchemaVersion is None:
Ed Tanous683f7272018-07-26 12:47:19 -0700253 schema_files[filenamesplit[0]] = filenamesplit[1]
254 else:
255 # need to see if we're a newer version.
256 if list(map(int, filenamesplit[1][1:].split("_"))) > list(map(
257 int, thisSchemaVersion[1:].split("_"))):
258 schema_files[filenamesplit[0]] = filenamesplit[1]
259
260
261for schema, version in schema_files.items():
262 basename = schema + "." + version + ".json"
Gunnar Millsa778c022020-05-12 12:20:36 -0500263 zip_filepath = os.path.join(VERSION, "json-schema", basename)
Ed Tanous683f7272018-07-26 12:47:19 -0700264 schemadir = os.path.join(json_schema_path, schema)
265 os.makedirs(schemadir)
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600266 location_json = OrderedDict()
267 location_json["Language"] = "en"
268 location_json["PublicationUri"] = (
269 "http://redfish.dmtf.org/schemas/v1/" + schema + ".json")
270 location_json["Uri"] = (
Ed Tanous63faafa2019-01-03 14:09:56 -0800271 "/redfish/v1/JsonSchemas/" + schema + "/" + schema + ".json")
Ed Tanous118b1c72018-09-13 13:45:51 -0700272
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600273 index_json = OrderedDict()
274 index_json["@odata.context"] = "/redfish/v1/$metadata#JsonSchemaFile.JsonSchemaFile"
Ed Tanous63faafa2019-01-03 14:09:56 -0800275 index_json["@odata.id"] = "/redfish/v1/JsonSchemas/" + schema
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600276 index_json["@odata.type"] = "#JsonSchemaFile.v1_0_2.JsonSchemaFile"
277 index_json["Name"] = schema + " Schema File"
278 index_json["Schema"] = "#" + schema + "." + schema
Ed Tanouscb103132019-10-08 11:34:22 -0700279 index_json["Description"] = schema + " Schema File Location"
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600280 index_json["Id"] = schema
Ed Tanouscb103132019-10-08 11:34:22 -0700281 index_json["Languages"] = ["en"]
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600282 index_json["Languages@odata.count"] = 1
Ed Tanouscb103132019-10-08 11:34:22 -0700283 index_json["Location"] = [location_json]
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600284 index_json["Location@odata.count"] = 1
Ed Tanous118b1c72018-09-13 13:45:51 -0700285
286 with open(os.path.join(schemadir, "index.json"), 'w') as schema_file:
287 json.dump(index_json, schema_file, indent=4)
288 with open(os.path.join(schemadir, schema + ".json"), 'wb') as schema_file:
Ed Tanouscb103132019-10-08 11:34:22 -0700289 schema_file.write(zip_ref.read(zip_filepath).replace(b'\r\n', b'\n'))
Ed Tanous683f7272018-07-26 12:47:19 -0700290
291with open(os.path.join(json_schema_path, "index.json"), 'w') as index_file:
Ed Tanous6f56d0c2019-05-03 17:15:41 -0700292 members = [{"@odata.id": "/redfish/v1/JsonSchemas/" + schema}
Ed Tanous683f7272018-07-26 12:47:19 -0700293 for schema in schema_files]
294
295 members.sort(key=lambda x: x["@odata.id"])
296
297 indexData = OrderedDict()
298
299 indexData["@odata.id"] = "/redfish/v1/JsonSchemas"
300 indexData["@odata.context"] = ("/redfish/v1/$metadata"
301 "#JsonSchemaFileCollection."
302 "JsonSchemaFileCollection")
303 indexData["@odata.type"] = ("#JsonSchemaFileCollection."
304 "JsonSchemaFileCollection")
305 indexData["Name"] = "JsonSchemaFile Collection"
306 indexData["Description"] = "Collection of JsonSchemaFiles"
307 indexData["Members@odata.count"] = len(schema_files)
308 indexData["Members"] = members
309
310 json.dump(indexData, index_file, indent=2)
311
312zip_ref.close()