blob: 3ce4173965fce72ca3dff57b0d1f82d204268a54 [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',
Ed Tanousf395daa2021-08-02 08:56:24 -070042 'JsonSchemaFileCollection', # redfish/v1/JsonSchemas
Gunnar Mills349a2ac2021-01-20 22:29:16 -060043 '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',
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',
99 'UpdateService',
100 'VLanNetworkInterfaceCollection',
101 'VLanNetworkInterface',
102 'VirtualMedia',
103 'VirtualMediaCollection',
104 'odata',
105 'odata-v4',
106 'redfish-error',
107 'redfish-payload-annotations',
108 'redfish-schema',
109 'redfish-schema-v1',
110]
111
Ed Tanous683f7272018-07-26 12:47:19 -0700112SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
113
114proxies = {
115 'https': os.environ.get("https_proxy", None)
116}
117
Ed Tanouscb103132019-10-08 11:34:22 -0700118r = requests.get(
119 'https://www.dmtf.org/sites/default/files/standards/documents/' +
120 VERSION +
121 '.zip',
122 proxies=proxies)
Ed Tanous683f7272018-07-26 12:47:19 -0700123
124r.raise_for_status()
125
126static_path = os.path.realpath(os.path.join(SCRIPT_DIR, "..", "static",
127 "redfish", "v1"))
128
129schema_path = os.path.join(static_path, "schema")
130json_schema_path = os.path.join(static_path, "JsonSchemas")
Ed Tanous118b1c72018-09-13 13:45:51 -0700131metadata_index_path = os.path.join(static_path, "$metadata", "index.xml")
Ed Tanous683f7272018-07-26 12:47:19 -0700132
133zipBytesIO = BytesIO(r.content)
134zip_ref = zipfile.ZipFile(zipBytesIO)
135
136# Remove the old files
137if os.path.exists(schema_path):
James Feistaee8d842018-09-10 16:07:40 -0700138 files = glob.glob(os.path.join(schema_path, '[!Oem]*'))
139 for f in files:
140 os.remove(f)
Ed Tanous683f7272018-07-26 12:47:19 -0700141if os.path.exists(json_schema_path):
Ed Tanous118b1c72018-09-13 13:45:51 -0700142 files = glob.glob(os.path.join(json_schema_path, '[!Oem]*'))
143 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():
Ed Tanousf395daa2021-08-02 08:56:24 -0700164 if zip_filepath.startswith(VERSION + '/csdl/') and \
165 (zip_filepath != VERSION + "/csdl/") and \
166 (zip_filepath != VERSION + "/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}"
Ed Tanous118b1c72018-09-13 13:45:51 -0700186 for edmx_child in xml_root:
Ed Tanousf395daa2021-08-02 08:56:24 -0700187 if edmx_child.tag == edmx + "DataServices":
Ed Tanous118b1c72018-09-13 13:45:51 -0700188 for data_child in edmx_child:
Ed Tanousf395daa2021-08-02 08:56:24 -0700189 if data_child.tag == edmx + "Schema":
Ed Tanous118b1c72018-09-13 13:45:51 -0700190 namespace = data_child.attrib["Namespace"]
191 if namespace.startswith("RedfishExtensions"):
192 metadata_index.write(
Ed Tanousf395daa2021-08-02 08:56:24 -0700193 " "
194 "<edmx:Include Namespace=\"" +
195 namespace +
196 "\" Alias=\"Redfish\"/>\n"
197 )
Ed Tanous118b1c72018-09-13 13:45:51 -0700198
199 else:
200 metadata_index.write(
Ed Tanousf395daa2021-08-02 08:56:24 -0700201 " "
202 "<edmx:Include Namespace=\""
203 + namespace + "\"/>\n"
204 )
205 schema_out.write(content)
Ed Tanous118b1c72018-09-13 13:45:51 -0700206 metadata_index.write(" </edmx:Reference>\n")
207
Ed Tanousf395daa2021-08-02 08:56:24 -0700208 metadata_index.write(" <edmx:DataServices>\n"
209 " <Schema "
210 "xmlns=\"http://docs.oasis-open.org/odata/ns/edm\" "
211 "Namespace=\"Service\">\n"
212 " <EntityContainer Name=\"Service\" "
213 "Extends=\"ServiceRoot.v1_0_0.ServiceContainer\"/>\n"
214 " </Schema>\n"
215 " </edmx:DataServices>\n"
216 )
Ed Tanouscb103132019-10-08 11:34:22 -0700217 # TODO:Issue#32 There's a bug in the script that currently deletes this
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500218 # schema (because it's an OEM schema). Because it's the only six, and we
Ed Tanouscb103132019-10-08 11:34:22 -0700219 # don't update schemas very often, we just manually fix it. Need a
220 # permanent fix to the script.
221 metadata_index.write(
222 " <edmx:Reference Uri=\"/redfish/v1/schema/OemManager_v1.xml\">\n")
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600223 metadata_index.write(" <edmx:Include Namespace=\"OemManager\"/>\n")
224 metadata_index.write(" </edmx:Reference>\n")
Gunnar Mills20778992020-02-06 16:36:47 -0600225
226 metadata_index.write(
Ed Tanousf395daa2021-08-02 08:56:24 -0700227 " <edmx:Reference Uri=\""
228 "/redfish/v1/schema/OemComputerSystem_v1.xml\">\n")
229 metadata_index.write(
230 " <edmx:Include Namespace=\"OemComputerSystem\"/>\n")
Gunnar Mills20778992020-02-06 16:36:47 -0600231 metadata_index.write(" </edmx:Reference>\n")
232
233 metadata_index.write(
Ed Tanousf395daa2021-08-02 08:56:24 -0700234 " <edmx:Reference Uri=\""
235 "/redfish/v1/schema/OemVirtualMedia_v1.xml\">\n")
236 metadata_index.write(
237 " <edmx:Include Namespace=\"OemVirtualMedia\"/>\n")
238 metadata_index.write(
239 " <edmx:Include Namespace=\"OemVirtualMedia.v1_0_0\"/>\n")
Gunnar Mills20778992020-02-06 16:36:47 -0600240 metadata_index.write(" </edmx:Reference>\n")
241
242 metadata_index.write(
Ed Tanousf395daa2021-08-02 08:56:24 -0700243 " <edmx:Reference Uri=\""
244 "/redfish/v1/schema/OemAccountService_v1.xml\">\n")
245 metadata_index.write(
246 " <edmx:Include Namespace=\"OemAccountService\"/>\n")
247 metadata_index.write(
248 " <edmx:Include Namespace=\"OemAccountService.v1_0_0\"/>\n")
Gunnar Mills20778992020-02-06 16:36:47 -0600249 metadata_index.write(" </edmx:Reference>\n")
250
Ravi Tejae7d68c32020-03-15 13:30:41 -0500251 metadata_index.write(
Sunitha Harish9dc50742020-05-11 00:10:20 -0500252 " <edmx:Reference Uri=\"/redfish/v1/schema/OemSession_v1.xml\">\n")
253 metadata_index.write(" <edmx:Include Namespace=\"OemSession\"/>\n")
Ed Tanousf395daa2021-08-02 08:56:24 -0700254 metadata_index.write(
255 " <edmx:Include Namespace=\"OemSession.v1_0_0\"/>\n")
Sunitha Harish9dc50742020-05-11 00:10:20 -0500256 metadata_index.write(" </edmx:Reference>\n")
257
Ed Tanous118b1c72018-09-13 13:45:51 -0700258 metadata_index.write("</edmx:Edmx>\n")
Ed Tanous683f7272018-07-26 12:47:19 -0700259
260schema_files = {}
261for zip_filepath in zip_ref.namelist():
Gunnar Millsa778c022020-05-12 12:20:36 -0500262 if zip_filepath.startswith(os.path.join(VERSION, 'json-schema/')):
Ed Tanous683f7272018-07-26 12:47:19 -0700263 filename = os.path.basename(zip_filepath)
264 filenamesplit = filename.split(".")
Gunnar Mills349a2ac2021-01-20 22:29:16 -0600265
266 # exclude schemas again to save flash space
267 if filenamesplit[0] not in include_list:
268 continue
269
Ed Tanous683f7272018-07-26 12:47:19 -0700270 if len(filenamesplit) == 3:
271 thisSchemaVersion = schema_files.get(filenamesplit[0], None)
Ed Tanouscb103132019-10-08 11:34:22 -0700272 if thisSchemaVersion is None:
Ed Tanous683f7272018-07-26 12:47:19 -0700273 schema_files[filenamesplit[0]] = filenamesplit[1]
274 else:
275 # need to see if we're a newer version.
276 if list(map(int, filenamesplit[1][1:].split("_"))) > list(map(
277 int, thisSchemaVersion[1:].split("_"))):
278 schema_files[filenamesplit[0]] = filenamesplit[1]
279
280
281for schema, version in schema_files.items():
282 basename = schema + "." + version + ".json"
Gunnar Millsa778c022020-05-12 12:20:36 -0500283 zip_filepath = os.path.join(VERSION, "json-schema", basename)
Ed Tanous683f7272018-07-26 12:47:19 -0700284 schemadir = os.path.join(json_schema_path, schema)
285 os.makedirs(schemadir)
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600286 location_json = OrderedDict()
287 location_json["Language"] = "en"
288 location_json["PublicationUri"] = (
289 "http://redfish.dmtf.org/schemas/v1/" + schema + ".json")
290 location_json["Uri"] = (
Ed Tanous63faafa2019-01-03 14:09:56 -0800291 "/redfish/v1/JsonSchemas/" + schema + "/" + schema + ".json")
Ed Tanous118b1c72018-09-13 13:45:51 -0700292
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600293 index_json = OrderedDict()
Ed Tanousf395daa2021-08-02 08:56:24 -0700294 index_json["@odata.context"] = \
295 "/redfish/v1/$metadata#JsonSchemaFile.JsonSchemaFile"
Ed Tanous63faafa2019-01-03 14:09:56 -0800296 index_json["@odata.id"] = "/redfish/v1/JsonSchemas/" + schema
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600297 index_json["@odata.type"] = "#JsonSchemaFile.v1_0_2.JsonSchemaFile"
298 index_json["Name"] = schema + " Schema File"
299 index_json["Schema"] = "#" + schema + "." + schema
Ed Tanouscb103132019-10-08 11:34:22 -0700300 index_json["Description"] = schema + " Schema File Location"
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600301 index_json["Id"] = schema
Ed Tanouscb103132019-10-08 11:34:22 -0700302 index_json["Languages"] = ["en"]
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600303 index_json["Languages@odata.count"] = 1
Ed Tanouscb103132019-10-08 11:34:22 -0700304 index_json["Location"] = [location_json]
Marri Devender Raod45d2d02019-01-21 10:11:34 -0600305 index_json["Location@odata.count"] = 1
Ed Tanous118b1c72018-09-13 13:45:51 -0700306
307 with open(os.path.join(schemadir, "index.json"), 'w') as schema_file:
308 json.dump(index_json, schema_file, indent=4)
309 with open(os.path.join(schemadir, schema + ".json"), 'wb') as schema_file:
Ed Tanouscb103132019-10-08 11:34:22 -0700310 schema_file.write(zip_ref.read(zip_filepath).replace(b'\r\n', b'\n'))
Ed Tanous683f7272018-07-26 12:47:19 -0700311
312with open(os.path.join(json_schema_path, "index.json"), 'w') as index_file:
Ed Tanous6f56d0c2019-05-03 17:15:41 -0700313 members = [{"@odata.id": "/redfish/v1/JsonSchemas/" + schema}
Ed Tanous683f7272018-07-26 12:47:19 -0700314 for schema in schema_files]
315
316 members.sort(key=lambda x: x["@odata.id"])
317
318 indexData = OrderedDict()
319
320 indexData["@odata.id"] = "/redfish/v1/JsonSchemas"
321 indexData["@odata.context"] = ("/redfish/v1/$metadata"
322 "#JsonSchemaFileCollection."
323 "JsonSchemaFileCollection")
324 indexData["@odata.type"] = ("#JsonSchemaFileCollection."
325 "JsonSchemaFileCollection")
326 indexData["Name"] = "JsonSchemaFile Collection"
327 indexData["Description"] = "Collection of JsonSchemaFiles"
328 indexData["Members@odata.count"] = len(schema_files)
329 indexData["Members"] = members
330
331 json.dump(indexData, index_file, indent=2)
332
333zip_ref.close()