blob: bfe87ef7736b81873998d663ee403165379fe9a3 [file] [log] [blame]
Nan Zhou313c1b72022-03-25 11:47:55 -07001#!/usr/bin/env python3
Jason M. Bills70304cb2019-03-27 12:03:59 -07002import json
Nan Zhou6eaf0bd2022-08-07 01:18:45 +00003import os
Jason M. Bills70304cb2019-03-27 12:03:59 -07004
Nan Zhou6eaf0bd2022-08-07 01:18:45 +00005import requests
Nan Zhou49aa131f2022-09-20 18:41:37 +00006import argparse
Jason M. Bills70304cb2019-03-27 12:03:59 -07007
Nan Zhou043bec02022-09-20 18:12:13 +00008PRAGMA_ONCE = '''#pragma once
9'''
10
Ed Tanous1cf53df2022-02-17 09:09:25 -080011WARNING = '''/****************************************************************
12 * 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.
20 ***************************************************************/'''
21
Nan Zhou043bec02022-09-20 18:12:13 +000022REGISTRY_HEADER = PRAGMA_ONCE + WARNING + '''
Nan Zhou01c78a02022-09-20 18:17:20 +000023#include "registries.hpp"
24
25#include <array>
Jason M. Bills70304cb2019-03-27 12:03:59 -070026
Ed Tanous4d99bbb2022-02-17 10:02:57 -080027// clang-format off
28
Ed Tanousfffb8c12022-02-07 23:53:03 -080029namespace redfish::registries::{}
Jason M. Bills70304cb2019-03-27 12:03:59 -070030{{
Jason M. Bills70304cb2019-03-27 12:03:59 -070031'''
32
33SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
34
Jason M. Billsac706372020-02-18 11:36:47 -080035include_path = os.path.realpath(
36 os.path.join(
37 SCRIPT_DIR,
38 "..",
39 "redfish-core",
40 "include",
41 "registries"))
Jason M. Bills70304cb2019-03-27 12:03:59 -070042
43proxies = {
44 'https': os.environ.get("https_proxy", None)
45}
46
Jason M. Bills70304cb2019-03-27 12:03:59 -070047
James Feiste51c7102020-03-17 10:38:18 -070048def make_getter(dmtf_name, header_name, type_name):
49 url = 'https://redfish.dmtf.org/registries/{}'.format(dmtf_name)
50 dmtf = requests.get(url, proxies=proxies)
51 dmtf.raise_for_status()
52 json_file = json.loads(dmtf.text)
53 path = os.path.join(include_path, header_name)
54 return (path, json_file, type_name, url)
55
56
Nan Zhou49aa131f2022-09-20 18:41:37 +000057def update_registries(files):
58 # Remove the old files
59 for file, json_dict, namespace, url in files:
60 try:
61 os.remove(file)
62 except BaseException:
63 print("{} not found".format(file))
Jason M. Bills70304cb2019-03-27 12:03:59 -070064
Nan Zhou49aa131f2022-09-20 18:41:37 +000065 with open(file, 'w') as registry:
66 registry.write(REGISTRY_HEADER.format(namespace))
67 # Parse the Registry header info
Ed Tanous5b9ef702022-02-17 12:19:32 -080068 registry.write(
Nan Zhou49aa131f2022-09-20 18:41:37 +000069 "const Header header = {{\n"
70 " \"{json_dict[@Redfish.Copyright]}\",\n"
71 " \"{json_dict[@odata.type]}\",\n"
72 " \"{json_dict[Id]}\",\n"
73 " \"{json_dict[Name]}\",\n"
74 " \"{json_dict[Language]}\",\n"
75 " \"{json_dict[Description]}\",\n"
76 " \"{json_dict[RegistryPrefix]}\",\n"
77 " \"{json_dict[RegistryVersion]}\",\n"
78 " \"{json_dict[OwningEntity]}\",\n"
79 "}};\n"
80 "constexpr const char* url =\n"
81 " \"{url}\";\n"
82 "\n"
83 "constexpr std::array registry =\n"
84 "{{\n".format(
85 json_dict=json_dict,
86 url=url,
Ed Tanous5b9ef702022-02-17 12:19:32 -080087 ))
Ed Tanous30a3c432022-02-07 09:37:21 -080088
Nan Zhou49aa131f2022-09-20 18:41:37 +000089 messages_sorted = sorted(json_dict["Messages"].items())
90 for messageId, message in messages_sorted:
91 registry.write(
92 " MessageEntry{{\n"
93 " \"{messageId}\",\n"
94 " {{\n"
95 " \"{message[Description]}\",\n"
96 " \"{message[Message]}\",\n"
97 " \"{message[MessageSeverity]}\",\n"
98 " {message[NumberOfArgs]},\n"
99 " {{".format(
100 messageId=messageId,
101 message=message
102 ))
103 paramTypes = message.get("ParamTypes")
104 if paramTypes:
105 for paramType in paramTypes:
106 registry.write(
107 "\n"
108 " \"{}\",".format(paramType)
109 )
110 registry.write("\n },\n")
111 else:
112 registry.write("},\n")
113 registry.write(
114 " \"{message[Resolution]}\",\n"
115 " }}}},\n".format(message=message))
116
Ed Tanous30a3c432022-02-07 09:37:21 -0800117 registry.write(
Nan Zhou49aa131f2022-09-20 18:41:37 +0000118 "\n};\n"
119 "\n"
120 "enum class Index\n"
121 "{\n"
122 )
123 for index, (messageId, message) in enumerate(messages_sorted):
124 messageId = messageId[0].lower() + messageId[1:]
125 registry.write(
126 " {} = {},\n".format(messageId, index))
127 registry.write(
128 "}};\n"
129 "}} // namespace redfish::registries::{}\n"
130 .format(namespace))
Ed Tanoused398212021-06-09 17:05:54 -0700131
132
133def get_privilege_string_from_list(privilege_list):
134 privilege_string = "{{\n"
135 for privilege_json in privilege_list:
136 privileges = privilege_json["Privilege"]
137 privilege_string += " {"
138 for privilege in privileges:
139 if privilege == "NoAuth":
140 continue
141 privilege_string += "\""
142 privilege_string += privilege
143 privilege_string += "\",\n"
144 if privilege != "NoAuth":
145 privilege_string = privilege_string[:-2]
146 privilege_string += "}"
147 privilege_string += ",\n"
148 privilege_string = privilege_string[:-2]
149 privilege_string += "\n}}"
150 return privilege_string
151
Ed Tanousf395daa2021-08-02 08:56:24 -0700152
Ed Tanoused398212021-06-09 17:05:54 -0700153def get_variable_name_for_privilege_set(privilege_list):
154 names = []
155 for privilege_json in privilege_list:
156 privileges = privilege_json["Privilege"]
157 names.append("And".join(privileges))
158 return "Or".join(names)
159
Ed Tanousf395daa2021-08-02 08:56:24 -0700160
Nan Zhou043bec02022-09-20 18:12:13 +0000161PRIVILEGE_HEADER = PRAGMA_ONCE + WARNING + '''
Nan Zhou01c78a02022-09-20 18:17:20 +0000162#include "privileges.hpp"
163
164#include <array>
Nan Zhou043bec02022-09-20 18:12:13 +0000165
166// clang-format off
167
168namespace redfish::privileges
169{
170'''
171
172
Ed Tanoused398212021-06-09 17:05:54 -0700173def make_privilege_registry():
Ed Tanousf395daa2021-08-02 08:56:24 -0700174 path, json_file, type_name, url = \
Sui Chen8bb5fbc2022-06-21 09:48:57 -0700175 make_getter('Redfish_1.3.0_PrivilegeRegistry.json',
Ed Tanousf395daa2021-08-02 08:56:24 -0700176 'privilege_registry.hpp', 'privilege')
Ed Tanoused398212021-06-09 17:05:54 -0700177 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():
184 privilege_dict[get_privilege_string_from_list(
185 privilege_list)] = (privilege_list, )
Ed Tanoused398212021-06-09 17:05:54 -0700186 for index, key in enumerate(privilege_dict):
187 (privilege_list, ) = privilege_dict[key]
188 name = get_variable_name_for_privilege_set(privilege_list)
Ed Tanousf395daa2021-08-02 08:56:24 -0700189 registry.write(
Ed Tanous5b9ef702022-02-17 12:19:32 -0800190 "const std::array<Privileges, {length}> "
191 "privilegeSet{name} = {key};\n"
192 .format(length=len(privilege_list), name=name, key=key)
193 )
Ed Tanoused398212021-06-09 17:05:54 -0700194 privilege_dict[key] = (privilege_list, name)
Ed Tanoused398212021-06-09 17:05:54 -0700195
196 for mapping in json_file["Mappings"]:
197 entity = mapping["Entity"]
Ed Tanous4d99bbb2022-02-17 10:02:57 -0800198 registry.write("// {}\n".format(entity))
Ed Tanoused398212021-06-09 17:05:54 -0700199 for operation, privilege_list in mapping["OperationMap"].items():
200 privilege_string = get_privilege_string_from_list(
201 privilege_list)
202 operation = operation.lower()
203
Ed Tanousf395daa2021-08-02 08:56:24 -0700204 registry.write(
205 "const static auto& {}{} = privilegeSet{};\n".format(
206 operation,
207 entity,
208 privilege_dict[privilege_string][1]))
Ed Tanoused398212021-06-09 17:05:54 -0700209 registry.write("\n")
Ed Tanous5b9ef702022-02-17 12:19:32 -0800210 registry.write(
211 "} // namespace redfish::privileges\n"
212 "// clang-format on\n")
Ed Tanoused398212021-06-09 17:05:54 -0700213
214
Nan Zhou49aa131f2022-09-20 18:41:37 +0000215def main():
216 parser = argparse.ArgumentParser()
217 parser.add_argument(
218 '--registries', type=str,
219 default="base,task_event,resource_event,privilege",
220 help="Comma delimited list of registries to update")
221
222 args = parser.parse_args()
223
224 registries = set(args.registries.split(","))
225 files = []
226
227 if "base" in registries:
228 files.append(make_getter('Base.1.13.0.json',
229 'base_message_registry.hpp',
230 'base'))
231 if "task_event" in registries:
232 files.append(make_getter('TaskEvent.1.0.3.json',
233 'task_event_message_registry.hpp',
234 'task_event'))
235 if "resource_event" in registries:
236 files.append(make_getter('ResourceEvent.1.0.3.json',
237 'resource_event_message_registry.hpp',
238 'resource_event'))
239
240 update_registries(files)
241
242 if "privilege" in registries:
243 make_privilege_registry()
244
245
246if __name__ == "__main__":
247 main()