blob: de560d80bba475de9da2fa9d048e29514bbcdc17 [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
Jason M. Bills70304cb2019-03-27 12:03:59 -07006
Ed Tanous1cf53df2022-02-17 09:09:25 -08007WARNING = '''/****************************************************************
8 * READ THIS WARNING FIRST
Jason M. Bills70304cb2019-03-27 12:03:59 -07009 * This is an auto-generated header which contains definitions
10 * for Redfish DMTF defined messages.
Ed Tanous1cf53df2022-02-17 09:09:25 -080011 * DO NOT modify this registry outside of running the
Jason M. Bills0e2d0692022-04-01 13:59:05 -070012 * parse_registries.py script. The definitions contained within
Ed Tanous1cf53df2022-02-17 09:09:25 -080013 * this file are owned by DMTF. Any modifications to these files
14 * should be first pushed to the relevant registry in the DMTF
15 * github organization.
16 ***************************************************************/'''
17
18REGISTRY_HEADER = WARNING + '''
Jason M. Bills70304cb2019-03-27 12:03:59 -070019#pragma once
20#include <registries.hpp>
21
Ed Tanous4d99bbb2022-02-17 10:02:57 -080022// clang-format off
23
Ed Tanousfffb8c12022-02-07 23:53:03 -080024namespace redfish::registries::{}
Jason M. Bills70304cb2019-03-27 12:03:59 -070025{{
Jason M. Bills70304cb2019-03-27 12:03:59 -070026'''
27
28SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
29
Jason M. Billsac706372020-02-18 11:36:47 -080030include_path = os.path.realpath(
31 os.path.join(
32 SCRIPT_DIR,
33 "..",
34 "redfish-core",
35 "include",
36 "registries"))
Jason M. Bills70304cb2019-03-27 12:03:59 -070037
38proxies = {
39 'https': os.environ.get("https_proxy", None)
40}
41
Jason M. Bills70304cb2019-03-27 12:03:59 -070042
James Feiste51c7102020-03-17 10:38:18 -070043def make_getter(dmtf_name, header_name, type_name):
44 url = 'https://redfish.dmtf.org/registries/{}'.format(dmtf_name)
45 dmtf = requests.get(url, proxies=proxies)
46 dmtf.raise_for_status()
47 json_file = json.loads(dmtf.text)
48 path = os.path.join(include_path, header_name)
49 return (path, json_file, type_name, url)
50
51
52files = []
Jiaqing Zhaof8666862022-06-24 15:01:03 +080053files.append(make_getter('Base.1.13.0.json',
Ed Tanousf395daa2021-08-02 08:56:24 -070054 'base_message_registry.hpp',
55 'base'))
Ed Tanousfc8a2b82021-06-09 16:29:31 -070056files.append(make_getter('TaskEvent.1.0.3.json',
Ed Tanousf395daa2021-08-02 08:56:24 -070057 'task_event_message_registry.hpp',
58 'task_event'))
Sunitha Harish74eec262020-06-25 10:00:01 -050059files.append(make_getter('ResourceEvent.1.0.3.json',
Ed Tanousf395daa2021-08-02 08:56:24 -070060 'resource_event_message_registry.hpp',
61 'resource_event'))
Jason M. Bills70304cb2019-03-27 12:03:59 -070062
63# Remove the old files
Ed Tanous413d8762021-06-09 16:24:19 -070064for file, json_dict, namespace, url in files:
Jason M. Bills70304cb2019-03-27 12:03:59 -070065 try:
66 os.remove(file)
Jason M. Billsac706372020-02-18 11:36:47 -080067 except BaseException:
Jason M. Bills70304cb2019-03-27 12:03:59 -070068 print("{} not found".format(file))
69
70 with open(file, 'w') as registry:
71 registry.write(REGISTRY_HEADER.format(namespace))
Jason M. Bills351d3062019-03-27 12:58:21 -070072 # Parse the Registry header info
Ed Tanous5b9ef702022-02-17 12:19:32 -080073 registry.write(
74 "const Header header = {{\n"
75 " \"{json_dict[@Redfish.Copyright]}\",\n"
76 " \"{json_dict[@odata.type]}\",\n"
77 " \"{json_dict[Id]}\",\n"
78 " \"{json_dict[Name]}\",\n"
79 " \"{json_dict[Language]}\",\n"
80 " \"{json_dict[Description]}\",\n"
81 " \"{json_dict[RegistryPrefix]}\",\n"
82 " \"{json_dict[RegistryVersion]}\",\n"
83 " \"{json_dict[OwningEntity]}\",\n"
84 "}};\n"
85 "constexpr const char* url =\n"
86 " \"{url}\";\n"
87 "\n"
Jason M. Bills0e2d0692022-04-01 13:59:05 -070088 "constexpr std::array registry =\n"
Ed Tanous5b9ef702022-02-17 12:19:32 -080089 "{{\n".format(
90 json_dict=json_dict,
91 url=url,
Ed Tanous5b9ef702022-02-17 12:19:32 -080092 ))
Jason M. Bills351d3062019-03-27 12:58:21 -070093
Ed Tanous30a3c432022-02-07 09:37:21 -080094 messages_sorted = sorted(json_dict["Messages"].items())
95 for messageId, message in messages_sorted:
Ed Tanous5b9ef702022-02-17 12:19:32 -080096 registry.write(
97 " MessageEntry{{\n"
98 " \"{messageId}\",\n"
99 " {{\n"
100 " \"{message[Description]}\",\n"
101 " \"{message[Message]}\",\n"
Ed Tanous5b9ef702022-02-17 12:19:32 -0800102 " \"{message[MessageSeverity]}\",\n"
103 " {message[NumberOfArgs]},\n"
104 " {{".format(
105 messageId=messageId,
106 message=message
107 ))
Jason M. Bills70304cb2019-03-27 12:03:59 -0700108 paramTypes = message.get("ParamTypes")
109 if paramTypes:
110 for paramType in paramTypes:
Ed Tanous4d99bbb2022-02-17 10:02:57 -0800111 registry.write(
Ed Tanous5b9ef702022-02-17 12:19:32 -0800112 "\n"
113 " \"{}\",".format(paramType)
114 )
115 registry.write("\n },\n")
116 else:
117 registry.write("},\n")
118 registry.write(
119 " \"{message[Resolution]}\",\n"
120 " }}}},\n".format(message=message))
Ed Tanous30a3c432022-02-07 09:37:21 -0800121
Ed Tanous5b9ef702022-02-17 12:19:32 -0800122 registry.write(
123 "\n};\n"
124 "\n"
125 "enum class Index\n"
126 "{\n"
127 )
Ed Tanous30a3c432022-02-07 09:37:21 -0800128 for index, (messageId, message) in enumerate(messages_sorted):
129 messageId = messageId[0].lower() + messageId[1:]
130 registry.write(
Ed Tanous4d99bbb2022-02-17 10:02:57 -0800131 " {} = {},\n".format(messageId, index))
Ed Tanous4d99bbb2022-02-17 10:02:57 -0800132 registry.write(
Ed Tanous5b9ef702022-02-17 12:19:32 -0800133 "}};\n"
Ed Tanousfffb8c12022-02-07 23:53:03 -0800134 "}} // namespace redfish::registries::{}\n"
Ed Tanous5b9ef702022-02-17 12:19:32 -0800135 .format(namespace))
Ed Tanoused398212021-06-09 17:05:54 -0700136
137
138def get_privilege_string_from_list(privilege_list):
139 privilege_string = "{{\n"
140 for privilege_json in privilege_list:
141 privileges = privilege_json["Privilege"]
142 privilege_string += " {"
143 for privilege in privileges:
144 if privilege == "NoAuth":
145 continue
146 privilege_string += "\""
147 privilege_string += privilege
148 privilege_string += "\",\n"
149 if privilege != "NoAuth":
150 privilege_string = privilege_string[:-2]
151 privilege_string += "}"
152 privilege_string += ",\n"
153 privilege_string = privilege_string[:-2]
154 privilege_string += "\n}}"
155 return privilege_string
156
Ed Tanousf395daa2021-08-02 08:56:24 -0700157
Ed Tanoused398212021-06-09 17:05:54 -0700158def get_variable_name_for_privilege_set(privilege_list):
159 names = []
160 for privilege_json in privilege_list:
161 privileges = privilege_json["Privilege"]
162 names.append("And".join(privileges))
163 return "Or".join(names)
164
Ed Tanousf395daa2021-08-02 08:56:24 -0700165
Ed Tanoused398212021-06-09 17:05:54 -0700166def make_privilege_registry():
Ed Tanousf395daa2021-08-02 08:56:24 -0700167 path, json_file, type_name, url = \
Sui Chen8bb5fbc2022-06-21 09:48:57 -0700168 make_getter('Redfish_1.3.0_PrivilegeRegistry.json',
Ed Tanousf395daa2021-08-02 08:56:24 -0700169 'privilege_registry.hpp', 'privilege')
Ed Tanoused398212021-06-09 17:05:54 -0700170 with open(path, 'w') as registry:
Ed Tanous5b9ef702022-02-17 12:19:32 -0800171 registry.write(
172 "#pragma once\n"
173 "{WARNING}\n"
174 "// clang-format off\n"
175 "\n"
176 "#include <privileges.hpp>\n"
177 "\n"
178 "namespace redfish::privileges\n"
179 "{{\n"
180 .format(
181 WARNING=WARNING,
182 filename=os.path.basename(path)))
Ed Tanoused398212021-06-09 17:05:54 -0700183
184 privilege_dict = {}
185 for mapping in json_file["Mappings"]:
186 # first pass, identify all the unique privilege sets
187 for operation, privilege_list in mapping["OperationMap"].items():
188 privilege_dict[get_privilege_string_from_list(
189 privilege_list)] = (privilege_list, )
Ed Tanoused398212021-06-09 17:05:54 -0700190 for index, key in enumerate(privilege_dict):
191 (privilege_list, ) = privilege_dict[key]
192 name = get_variable_name_for_privilege_set(privilege_list)
Ed Tanousf395daa2021-08-02 08:56:24 -0700193 registry.write(
Ed Tanous5b9ef702022-02-17 12:19:32 -0800194 "const std::array<Privileges, {length}> "
195 "privilegeSet{name} = {key};\n"
196 .format(length=len(privilege_list), name=name, key=key)
197 )
Ed Tanoused398212021-06-09 17:05:54 -0700198 privilege_dict[key] = (privilege_list, name)
Ed Tanoused398212021-06-09 17:05:54 -0700199
200 for mapping in json_file["Mappings"]:
201 entity = mapping["Entity"]
Ed Tanous4d99bbb2022-02-17 10:02:57 -0800202 registry.write("// {}\n".format(entity))
Ed Tanoused398212021-06-09 17:05:54 -0700203 for operation, privilege_list in mapping["OperationMap"].items():
204 privilege_string = get_privilege_string_from_list(
205 privilege_list)
206 operation = operation.lower()
207
Ed Tanousf395daa2021-08-02 08:56:24 -0700208 registry.write(
209 "const static auto& {}{} = privilegeSet{};\n".format(
210 operation,
211 entity,
212 privilege_dict[privilege_string][1]))
Ed Tanoused398212021-06-09 17:05:54 -0700213 registry.write("\n")
Ed Tanous5b9ef702022-02-17 12:19:32 -0800214 registry.write(
215 "} // namespace redfish::privileges\n"
216 "// clang-format on\n")
Ed Tanoused398212021-06-09 17:05:54 -0700217
218
219make_privilege_registry()