blob: 70d0fec20f7c6fe68dd31c24c411edc91a1b0c3f [file] [log] [blame]
Nan Zhou313c1b72022-03-25 11:47:55 -07001#!/usr/bin/env python3
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -06002import argparse
Jason M. Bills70304cb2019-03-27 12:03:59 -07003import json
Nan Zhou6eaf0bd2022-08-07 01:18:45 +00004import os
Jason M. Bills70304cb2019-03-27 12:03:59 -07005
Nan Zhou6eaf0bd2022-08-07 01:18:45 +00006import requests
Jason M. Bills70304cb2019-03-27 12:03:59 -07007
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -06008PRAGMA_ONCE = """#pragma once
9"""
Nan Zhou043bec02022-09-20 18:12:13 +000010
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -060011WARNING = """/****************************************************************
Ed Tanous1cf53df2022-02-17 09:09:25 -080012 * READ THIS WARNING FIRST
Jason M. Bills70304cb2019-03-27 12:03:59 -070013 * This is an auto-generated header which contains definitions
14 * for Redfish DMTF defined messages.
Ed Tanous1cf53df2022-02-17 09:09:25 -080015 * DO NOT modify this registry outside of running the
Jason M. Bills0e2d0692022-04-01 13:59:05 -070016 * parse_registries.py script. The definitions contained within
Ed Tanous1cf53df2022-02-17 09:09:25 -080017 * this file are owned by DMTF. Any modifications to these files
18 * should be first pushed to the relevant registry in the DMTF
19 * github organization.
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -060020 ***************************************************************/"""
Ed Tanous1cf53df2022-02-17 09:09:25 -080021
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -060022REGISTRY_HEADER = (
23 PRAGMA_ONCE
24 + WARNING
25 + """
Nan Zhou01c78a02022-09-20 18:17:20 +000026#include "registries.hpp"
27
28#include <array>
Jason M. Bills70304cb2019-03-27 12:03:59 -070029
Ed Tanous4d99bbb2022-02-17 10:02:57 -080030// clang-format off
31
Ed Tanousfffb8c12022-02-07 23:53:03 -080032namespace redfish::registries::{}
Jason M. Bills70304cb2019-03-27 12:03:59 -070033{{
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -060034"""
35)
Jason M. Bills70304cb2019-03-27 12:03:59 -070036
37SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
38
Jason M. Billsac706372020-02-18 11:36:47 -080039include_path = os.path.realpath(
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -060040 os.path.join(SCRIPT_DIR, "..", "redfish-core", "include", "registries")
41)
Jason M. Bills70304cb2019-03-27 12:03:59 -070042
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -060043proxies = {"https": os.environ.get("https_proxy", None)}
Jason M. Bills70304cb2019-03-27 12:03:59 -070044
Jason M. Bills70304cb2019-03-27 12:03:59 -070045
James Feiste51c7102020-03-17 10:38:18 -070046def make_getter(dmtf_name, header_name, type_name):
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -060047 url = "https://redfish.dmtf.org/registries/{}".format(dmtf_name)
James Feiste51c7102020-03-17 10:38:18 -070048 dmtf = requests.get(url, proxies=proxies)
49 dmtf.raise_for_status()
50 json_file = json.loads(dmtf.text)
51 path = os.path.join(include_path, header_name)
52 return (path, json_file, type_name, url)
53
54
Ed Tanous3e5faba2023-08-16 15:11:29 -070055def openbmc_local_getter():
56 url = ""
57 with open(
58 os.path.join(
59 SCRIPT_DIR,
60 "..",
61 "redfish-core",
62 "include",
63 "registries",
64 "openbmc.json",
65 ),
66 "rb",
67 ) as json_file:
68 json_file = json.load(json_file)
69
70 path = os.path.join(include_path, "openbmc_message_registry.hpp")
71 return (path, json_file, "openbmc", url)
72
73
Nan Zhou49aa131f2022-09-20 18:41:37 +000074def update_registries(files):
75 # Remove the old files
76 for file, json_dict, namespace, url in files:
77 try:
78 os.remove(file)
79 except BaseException:
80 print("{} not found".format(file))
Jason M. Bills70304cb2019-03-27 12:03:59 -070081
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -060082 with open(file, "w") as registry:
Nan Zhou49aa131f2022-09-20 18:41:37 +000083 registry.write(REGISTRY_HEADER.format(namespace))
84 # Parse the Registry header info
Ed Tanous5b9ef702022-02-17 12:19:32 -080085 registry.write(
Nan Zhou49aa131f2022-09-20 18:41:37 +000086 "const Header header = {{\n"
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -060087 ' "{json_dict[@Redfish.Copyright]}",\n'
88 ' "{json_dict[@odata.type]}",\n'
89 ' "{json_dict[Id]}",\n'
90 ' "{json_dict[Name]}",\n'
91 ' "{json_dict[Language]}",\n'
92 ' "{json_dict[Description]}",\n'
93 ' "{json_dict[RegistryPrefix]}",\n'
94 ' "{json_dict[RegistryVersion]}",\n'
95 ' "{json_dict[OwningEntity]}",\n'
Nan Zhou49aa131f2022-09-20 18:41:37 +000096 "}};\n"
97 "constexpr const char* url =\n"
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -060098 ' "{url}";\n'
Nan Zhou49aa131f2022-09-20 18:41:37 +000099 "\n"
100 "constexpr std::array registry =\n"
101 "{{\n".format(
102 json_dict=json_dict,
103 url=url,
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600104 )
105 )
Ed Tanous30a3c432022-02-07 09:37:21 -0800106
Nan Zhou49aa131f2022-09-20 18:41:37 +0000107 messages_sorted = sorted(json_dict["Messages"].items())
108 for messageId, message in messages_sorted:
109 registry.write(
110 " MessageEntry{{\n"
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600111 ' "{messageId}",\n'
Nan Zhou49aa131f2022-09-20 18:41:37 +0000112 " {{\n"
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600113 ' "{message[Description]}",\n'
114 ' "{message[Message]}",\n'
115 ' "{message[MessageSeverity]}",\n'
Nan Zhou49aa131f2022-09-20 18:41:37 +0000116 " {message[NumberOfArgs]},\n"
117 " {{".format(
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600118 messageId=messageId, message=message
119 )
120 )
Nan Zhou49aa131f2022-09-20 18:41:37 +0000121 paramTypes = message.get("ParamTypes")
122 if paramTypes:
123 for paramType in paramTypes:
124 registry.write(
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600125 '\n "{}",'.format(paramType)
Nan Zhou49aa131f2022-09-20 18:41:37 +0000126 )
127 registry.write("\n },\n")
128 else:
129 registry.write("},\n")
130 registry.write(
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600131 ' "{message[Resolution]}",\n'
132 " }}}},\n".format(message=message)
133 )
Nan Zhou49aa131f2022-09-20 18:41:37 +0000134
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600135 registry.write("\n};\n\nenum class Index\n{\n")
Nan Zhou49aa131f2022-09-20 18:41:37 +0000136 for index, (messageId, message) in enumerate(messages_sorted):
137 messageId = messageId[0].lower() + messageId[1:]
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600138 registry.write(" {} = {},\n".format(messageId, index))
Nan Zhou49aa131f2022-09-20 18:41:37 +0000139 registry.write(
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600140 "}};\n}} // namespace redfish::registries::{}\n".format(
141 namespace
142 )
143 )
Ed Tanoused398212021-06-09 17:05:54 -0700144
145
146def get_privilege_string_from_list(privilege_list):
147 privilege_string = "{{\n"
148 for privilege_json in privilege_list:
149 privileges = privilege_json["Privilege"]
150 privilege_string += " {"
151 for privilege in privileges:
152 if privilege == "NoAuth":
153 continue
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600154 privilege_string += '"'
Ed Tanoused398212021-06-09 17:05:54 -0700155 privilege_string += privilege
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600156 privilege_string += '",\n'
Ed Tanoused398212021-06-09 17:05:54 -0700157 if privilege != "NoAuth":
158 privilege_string = privilege_string[:-2]
159 privilege_string += "}"
160 privilege_string += ",\n"
161 privilege_string = privilege_string[:-2]
162 privilege_string += "\n}}"
163 return privilege_string
164
Ed Tanousf395daa2021-08-02 08:56:24 -0700165
Ed Tanoused398212021-06-09 17:05:54 -0700166def get_variable_name_for_privilege_set(privilege_list):
167 names = []
168 for privilege_json in privilege_list:
169 privileges = privilege_json["Privilege"]
170 names.append("And".join(privileges))
171 return "Or".join(names)
172
Ed Tanousf395daa2021-08-02 08:56:24 -0700173
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600174PRIVILEGE_HEADER = (
175 PRAGMA_ONCE
176 + WARNING
177 + """
Nan Zhou01c78a02022-09-20 18:17:20 +0000178#include "privileges.hpp"
179
180#include <array>
Nan Zhou043bec02022-09-20 18:12:13 +0000181
182// clang-format off
183
184namespace redfish::privileges
185{
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600186"""
187)
Nan Zhou043bec02022-09-20 18:12:13 +0000188
189
Ed Tanoused398212021-06-09 17:05:54 -0700190def make_privilege_registry():
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600191 path, json_file, type_name, url = make_getter(
Gunnar Mills5910d942024-04-16 12:07:17 -0500192 "Redfish_1.5.0_PrivilegeRegistry.json",
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600193 "privilege_registry.hpp",
194 "privilege",
195 )
196 with open(path, "w") as registry:
Nan Zhou043bec02022-09-20 18:12:13 +0000197 registry.write(PRIVILEGE_HEADER)
Ed Tanoused398212021-06-09 17:05:54 -0700198
199 privilege_dict = {}
200 for mapping in json_file["Mappings"]:
201 # first pass, identify all the unique privilege sets
202 for operation, privilege_list in mapping["OperationMap"].items():
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600203 privilege_dict[
204 get_privilege_string_from_list(privilege_list)
205 ] = (privilege_list,)
Ed Tanoused398212021-06-09 17:05:54 -0700206 for index, key in enumerate(privilege_dict):
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600207 (privilege_list,) = privilege_dict[key]
Ed Tanoused398212021-06-09 17:05:54 -0700208 name = get_variable_name_for_privilege_set(privilege_list)
Ed Tanousf395daa2021-08-02 08:56:24 -0700209 registry.write(
Ed Tanous5b9ef702022-02-17 12:19:32 -0800210 "const std::array<Privileges, {length}> "
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600211 "privilegeSet{name} = {key};\n".format(
212 length=len(privilege_list), name=name, key=key
213 )
Ed Tanous5b9ef702022-02-17 12:19:32 -0800214 )
Ed Tanoused398212021-06-09 17:05:54 -0700215 privilege_dict[key] = (privilege_list, name)
Ed Tanoused398212021-06-09 17:05:54 -0700216
217 for mapping in json_file["Mappings"]:
218 entity = mapping["Entity"]
Ed Tanous4d99bbb2022-02-17 10:02:57 -0800219 registry.write("// {}\n".format(entity))
Ed Tanoused398212021-06-09 17:05:54 -0700220 for operation, privilege_list in mapping["OperationMap"].items():
221 privilege_string = get_privilege_string_from_list(
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600222 privilege_list
223 )
Ed Tanoused398212021-06-09 17:05:54 -0700224 operation = operation.lower()
225
Ed Tanousf395daa2021-08-02 08:56:24 -0700226 registry.write(
227 "const static auto& {}{} = privilegeSet{};\n".format(
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600228 operation, entity, privilege_dict[privilege_string][1]
229 )
230 )
Ed Tanoused398212021-06-09 17:05:54 -0700231 registry.write("\n")
Ed Tanous5b9ef702022-02-17 12:19:32 -0800232 registry.write(
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600233 "} // namespace redfish::privileges\n// clang-format on\n"
234 )
Ed Tanoused398212021-06-09 17:05:54 -0700235
236
Gunnar Mills665e7602024-04-10 13:14:41 -0500237def to_pascal_case(text):
238 s = text.replace("_", " ")
239 s = s.split()
240 if len(text) == 0:
241 return text
242 return "".join(i.capitalize() for i in s[0:])
243
244
Nan Zhou49aa131f2022-09-20 18:41:37 +0000245def main():
Gunnar Mills665e7602024-04-10 13:14:41 -0500246 dmtf_registries = (
Gunnar Millsd8d5fc32024-06-18 10:52:56 -0500247 ("base", "1.18.1"),
Gunnar Mills665e7602024-04-10 13:14:41 -0500248 ("composition", "1.1.2"),
249 ("environmental", "1.0.1"),
250 ("ethernet_fabric", "1.0.1"),
251 ("fabric", "1.0.2"),
252 ("heartbeat_event", "1.0.1"),
253 ("job_event", "1.0.1"),
254 ("license", "1.0.3"),
255 ("log_service", "1.0.1"),
256 ("network_device", "1.0.3"),
257 ("platform", "1.0.1"),
258 ("power", "1.0.1"),
259 ("resource_event", "1.3.0"),
260 ("sensor_event", "1.0.1"),
261 ("storage_device", "1.2.1"),
262 ("task_event", "1.0.3"),
263 ("telemetry", "1.0.0"),
264 ("update", "1.0.2"),
265 )
266
Nan Zhou49aa131f2022-09-20 18:41:37 +0000267 parser = argparse.ArgumentParser()
268 parser.add_argument(
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600269 "--registries",
270 type=str,
Gunnar Mills665e7602024-04-10 13:14:41 -0500271 default="privilege,openbmc,"
272 + ",".join([dmtf[0] for dmtf in dmtf_registries]),
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600273 help="Comma delimited list of registries to update",
274 )
Nan Zhou49aa131f2022-09-20 18:41:37 +0000275
276 args = parser.parse_args()
277
278 registries = set(args.registries.split(","))
279 files = []
280
Gunnar Mills665e7602024-04-10 13:14:41 -0500281 for registry, version in dmtf_registries:
282 if registry in registries:
283 registry_pascal_case = to_pascal_case(registry)
284 files.append(
285 make_getter(
286 f"{registry_pascal_case}.{version}.json",
287 f"{registry}_message_registry.hpp",
288 registry,
289 )
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600290 )
Ed Tanous3e5faba2023-08-16 15:11:29 -0700291 if "openbmc" in registries:
292 files.append(openbmc_local_getter())
Nan Zhou49aa131f2022-09-20 18:41:37 +0000293
294 update_registries(files)
295
296 if "privilege" in registries:
297 make_privilege_registry()
298
299
300if __name__ == "__main__":
301 main()