blob: 369bebf5825bf285d260d04a45481a771f04e2c2 [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
Nan Zhou49aa131f2022-09-20 18:41:37 +000055def update_registries(files):
56 # Remove the old files
57 for file, json_dict, namespace, url in files:
58 try:
59 os.remove(file)
60 except BaseException:
61 print("{} not found".format(file))
Jason M. Bills70304cb2019-03-27 12:03:59 -070062
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -060063 with open(file, "w") as registry:
Nan Zhou49aa131f2022-09-20 18:41:37 +000064 registry.write(REGISTRY_HEADER.format(namespace))
65 # Parse the Registry header info
Ed Tanous5b9ef702022-02-17 12:19:32 -080066 registry.write(
Nan Zhou49aa131f2022-09-20 18:41:37 +000067 "const Header header = {{\n"
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -060068 ' "{json_dict[@Redfish.Copyright]}",\n'
69 ' "{json_dict[@odata.type]}",\n'
70 ' "{json_dict[Id]}",\n'
71 ' "{json_dict[Name]}",\n'
72 ' "{json_dict[Language]}",\n'
73 ' "{json_dict[Description]}",\n'
74 ' "{json_dict[RegistryPrefix]}",\n'
75 ' "{json_dict[RegistryVersion]}",\n'
76 ' "{json_dict[OwningEntity]}",\n'
Nan Zhou49aa131f2022-09-20 18:41:37 +000077 "}};\n"
78 "constexpr const char* url =\n"
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -060079 ' "{url}";\n'
Nan Zhou49aa131f2022-09-20 18:41:37 +000080 "\n"
81 "constexpr std::array registry =\n"
82 "{{\n".format(
83 json_dict=json_dict,
84 url=url,
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -060085 )
86 )
Ed Tanous30a3c432022-02-07 09:37:21 -080087
Nan Zhou49aa131f2022-09-20 18:41:37 +000088 messages_sorted = sorted(json_dict["Messages"].items())
89 for messageId, message in messages_sorted:
90 registry.write(
91 " MessageEntry{{\n"
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -060092 ' "{messageId}",\n'
Nan Zhou49aa131f2022-09-20 18:41:37 +000093 " {{\n"
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -060094 ' "{message[Description]}",\n'
95 ' "{message[Message]}",\n'
96 ' "{message[MessageSeverity]}",\n'
Nan Zhou49aa131f2022-09-20 18:41:37 +000097 " {message[NumberOfArgs]},\n"
98 " {{".format(
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -060099 messageId=messageId, message=message
100 )
101 )
Nan Zhou49aa131f2022-09-20 18:41:37 +0000102 paramTypes = message.get("ParamTypes")
103 if paramTypes:
104 for paramType in paramTypes:
105 registry.write(
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600106 '\n "{}",'.format(paramType)
Nan Zhou49aa131f2022-09-20 18:41:37 +0000107 )
108 registry.write("\n },\n")
109 else:
110 registry.write("},\n")
111 registry.write(
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600112 ' "{message[Resolution]}",\n'
113 " }}}},\n".format(message=message)
114 )
Nan Zhou49aa131f2022-09-20 18:41:37 +0000115
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600116 registry.write("\n};\n\nenum class Index\n{\n")
Nan Zhou49aa131f2022-09-20 18:41:37 +0000117 for index, (messageId, message) in enumerate(messages_sorted):
118 messageId = messageId[0].lower() + messageId[1:]
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600119 registry.write(" {} = {},\n".format(messageId, index))
Nan Zhou49aa131f2022-09-20 18:41:37 +0000120 registry.write(
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600121 "}};\n}} // namespace redfish::registries::{}\n".format(
122 namespace
123 )
124 )
Ed Tanoused398212021-06-09 17:05:54 -0700125
126
127def get_privilege_string_from_list(privilege_list):
128 privilege_string = "{{\n"
129 for privilege_json in privilege_list:
130 privileges = privilege_json["Privilege"]
131 privilege_string += " {"
132 for privilege in privileges:
133 if privilege == "NoAuth":
134 continue
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600135 privilege_string += '"'
Ed Tanoused398212021-06-09 17:05:54 -0700136 privilege_string += privilege
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600137 privilege_string += '",\n'
Ed Tanoused398212021-06-09 17:05:54 -0700138 if privilege != "NoAuth":
139 privilege_string = privilege_string[:-2]
140 privilege_string += "}"
141 privilege_string += ",\n"
142 privilege_string = privilege_string[:-2]
143 privilege_string += "\n}}"
144 return privilege_string
145
Ed Tanousf395daa2021-08-02 08:56:24 -0700146
Ed Tanoused398212021-06-09 17:05:54 -0700147def get_variable_name_for_privilege_set(privilege_list):
148 names = []
149 for privilege_json in privilege_list:
150 privileges = privilege_json["Privilege"]
151 names.append("And".join(privileges))
152 return "Or".join(names)
153
Ed Tanousf395daa2021-08-02 08:56:24 -0700154
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600155PRIVILEGE_HEADER = (
156 PRAGMA_ONCE
157 + WARNING
158 + """
Nan Zhou01c78a02022-09-20 18:17:20 +0000159#include "privileges.hpp"
160
161#include <array>
Nan Zhou043bec02022-09-20 18:12:13 +0000162
163// clang-format off
164
165namespace redfish::privileges
166{
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600167"""
168)
Nan Zhou043bec02022-09-20 18:12:13 +0000169
170
Ed Tanoused398212021-06-09 17:05:54 -0700171def make_privilege_registry():
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600172 path, json_file, type_name, url = make_getter(
173 "Redfish_1.3.0_PrivilegeRegistry.json",
174 "privilege_registry.hpp",
175 "privilege",
176 )
177 with open(path, "w") as registry:
Nan Zhou043bec02022-09-20 18:12:13 +0000178 registry.write(PRIVILEGE_HEADER)
Ed Tanoused398212021-06-09 17:05:54 -0700179
180 privilege_dict = {}
181 for mapping in json_file["Mappings"]:
182 # first pass, identify all the unique privilege sets
183 for operation, privilege_list in mapping["OperationMap"].items():
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600184 privilege_dict[
185 get_privilege_string_from_list(privilege_list)
186 ] = (privilege_list,)
Ed Tanoused398212021-06-09 17:05:54 -0700187 for index, key in enumerate(privilege_dict):
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600188 (privilege_list,) = privilege_dict[key]
Ed Tanoused398212021-06-09 17:05:54 -0700189 name = get_variable_name_for_privilege_set(privilege_list)
Ed Tanousf395daa2021-08-02 08:56:24 -0700190 registry.write(
Ed Tanous5b9ef702022-02-17 12:19:32 -0800191 "const std::array<Privileges, {length}> "
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600192 "privilegeSet{name} = {key};\n".format(
193 length=len(privilege_list), name=name, key=key
194 )
Ed Tanous5b9ef702022-02-17 12:19:32 -0800195 )
Ed Tanoused398212021-06-09 17:05:54 -0700196 privilege_dict[key] = (privilege_list, name)
Ed Tanoused398212021-06-09 17:05:54 -0700197
198 for mapping in json_file["Mappings"]:
199 entity = mapping["Entity"]
Ed Tanous4d99bbb2022-02-17 10:02:57 -0800200 registry.write("// {}\n".format(entity))
Ed Tanoused398212021-06-09 17:05:54 -0700201 for operation, privilege_list in mapping["OperationMap"].items():
202 privilege_string = get_privilege_string_from_list(
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600203 privilege_list
204 )
Ed Tanoused398212021-06-09 17:05:54 -0700205 operation = operation.lower()
206
Ed Tanousf395daa2021-08-02 08:56:24 -0700207 registry.write(
208 "const static auto& {}{} = privilegeSet{};\n".format(
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600209 operation, entity, privilege_dict[privilege_string][1]
210 )
211 )
Ed Tanoused398212021-06-09 17:05:54 -0700212 registry.write("\n")
Ed Tanous5b9ef702022-02-17 12:19:32 -0800213 registry.write(
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600214 "} // namespace redfish::privileges\n// clang-format on\n"
215 )
Ed Tanoused398212021-06-09 17:05:54 -0700216
217
Nan Zhou49aa131f2022-09-20 18:41:37 +0000218def main():
219 parser = argparse.ArgumentParser()
220 parser.add_argument(
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600221 "--registries",
222 type=str,
Nan Zhou49aa131f2022-09-20 18:41:37 +0000223 default="base,task_event,resource_event,privilege",
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600224 help="Comma delimited list of registries to update",
225 )
Nan Zhou49aa131f2022-09-20 18:41:37 +0000226
227 args = parser.parse_args()
228
229 registries = set(args.registries.split(","))
230 files = []
231
232 if "base" in registries:
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600233 files.append(
234 make_getter(
235 "Base.1.13.0.json", "base_message_registry.hpp", "base"
236 )
237 )
Nan Zhou49aa131f2022-09-20 18:41:37 +0000238 if "task_event" in registries:
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600239 files.append(
240 make_getter(
241 "TaskEvent.1.0.3.json",
242 "task_event_message_registry.hpp",
243 "task_event",
244 )
245 )
Nan Zhou49aa131f2022-09-20 18:41:37 +0000246 if "resource_event" in registries:
Patrick Williamsdfa3fdc2022-12-07 07:14:21 -0600247 files.append(
248 make_getter(
249 "ResourceEvent.1.0.3.json",
250 "resource_event_message_registry.hpp",
251 "resource_event",
252 )
253 )
Nan Zhou49aa131f2022-09-20 18:41:37 +0000254
255 update_registries(files)
256
257 if "privilege" in registries:
258 make_privilege_registry()
259
260
261if __name__ == "__main__":
262 main()