blob: bc546575e38e9da096fb3b9f0eb01282f81f312a [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
Nan Zhou043bec02022-09-20 18:12:13 +00007PRAGMA_ONCE = '''#pragma once
8'''
9
Ed Tanous1cf53df2022-02-17 09:09:25 -080010WARNING = '''/****************************************************************
11 * READ THIS WARNING FIRST
Jason M. Bills70304cb2019-03-27 12:03:59 -070012 * This is an auto-generated header which contains definitions
13 * for Redfish DMTF defined messages.
Ed Tanous1cf53df2022-02-17 09:09:25 -080014 * DO NOT modify this registry outside of running the
Jason M. Bills0e2d0692022-04-01 13:59:05 -070015 * parse_registries.py script. The definitions contained within
Ed Tanous1cf53df2022-02-17 09:09:25 -080016 * this file are owned by DMTF. Any modifications to these files
17 * should be first pushed to the relevant registry in the DMTF
18 * github organization.
19 ***************************************************************/'''
20
Nan Zhou043bec02022-09-20 18:12:13 +000021REGISTRY_HEADER = PRAGMA_ONCE + WARNING + '''
Nan Zhou01c78a02022-09-20 18:17:20 +000022#include "registries.hpp"
23
24#include <array>
Jason M. Bills70304cb2019-03-27 12:03:59 -070025
Ed Tanous4d99bbb2022-02-17 10:02:57 -080026// clang-format off
27
Ed Tanousfffb8c12022-02-07 23:53:03 -080028namespace redfish::registries::{}
Jason M. Bills70304cb2019-03-27 12:03:59 -070029{{
Jason M. Bills70304cb2019-03-27 12:03:59 -070030'''
31
32SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
33
Jason M. Billsac706372020-02-18 11:36:47 -080034include_path = os.path.realpath(
35 os.path.join(
36 SCRIPT_DIR,
37 "..",
38 "redfish-core",
39 "include",
40 "registries"))
Jason M. Bills70304cb2019-03-27 12:03:59 -070041
42proxies = {
43 'https': os.environ.get("https_proxy", None)
44}
45
Jason M. Bills70304cb2019-03-27 12:03:59 -070046
James Feiste51c7102020-03-17 10:38:18 -070047def make_getter(dmtf_name, header_name, type_name):
48 url = 'https://redfish.dmtf.org/registries/{}'.format(dmtf_name)
49 dmtf = requests.get(url, proxies=proxies)
50 dmtf.raise_for_status()
51 json_file = json.loads(dmtf.text)
52 path = os.path.join(include_path, header_name)
53 return (path, json_file, type_name, url)
54
55
56files = []
Jiaqing Zhaof8666862022-06-24 15:01:03 +080057files.append(make_getter('Base.1.13.0.json',
Ed Tanousf395daa2021-08-02 08:56:24 -070058 'base_message_registry.hpp',
59 'base'))
Ed Tanousfc8a2b82021-06-09 16:29:31 -070060files.append(make_getter('TaskEvent.1.0.3.json',
Ed Tanousf395daa2021-08-02 08:56:24 -070061 'task_event_message_registry.hpp',
62 'task_event'))
Sunitha Harish74eec262020-06-25 10:00:01 -050063files.append(make_getter('ResourceEvent.1.0.3.json',
Ed Tanousf395daa2021-08-02 08:56:24 -070064 'resource_event_message_registry.hpp',
65 'resource_event'))
Jason M. Bills70304cb2019-03-27 12:03:59 -070066
67# Remove the old files
Ed Tanous413d8762021-06-09 16:24:19 -070068for file, json_dict, namespace, url in files:
Jason M. Bills70304cb2019-03-27 12:03:59 -070069 try:
70 os.remove(file)
Jason M. Billsac706372020-02-18 11:36:47 -080071 except BaseException:
Jason M. Bills70304cb2019-03-27 12:03:59 -070072 print("{} not found".format(file))
73
74 with open(file, 'w') as registry:
75 registry.write(REGISTRY_HEADER.format(namespace))
Jason M. Bills351d3062019-03-27 12:58:21 -070076 # Parse the Registry header info
Ed Tanous5b9ef702022-02-17 12:19:32 -080077 registry.write(
78 "const Header header = {{\n"
79 " \"{json_dict[@Redfish.Copyright]}\",\n"
80 " \"{json_dict[@odata.type]}\",\n"
81 " \"{json_dict[Id]}\",\n"
82 " \"{json_dict[Name]}\",\n"
83 " \"{json_dict[Language]}\",\n"
84 " \"{json_dict[Description]}\",\n"
85 " \"{json_dict[RegistryPrefix]}\",\n"
86 " \"{json_dict[RegistryVersion]}\",\n"
87 " \"{json_dict[OwningEntity]}\",\n"
88 "}};\n"
89 "constexpr const char* url =\n"
90 " \"{url}\";\n"
91 "\n"
Jason M. Bills0e2d0692022-04-01 13:59:05 -070092 "constexpr std::array registry =\n"
Ed Tanous5b9ef702022-02-17 12:19:32 -080093 "{{\n".format(
94 json_dict=json_dict,
95 url=url,
Ed Tanous5b9ef702022-02-17 12:19:32 -080096 ))
Jason M. Bills351d3062019-03-27 12:58:21 -070097
Ed Tanous30a3c432022-02-07 09:37:21 -080098 messages_sorted = sorted(json_dict["Messages"].items())
99 for messageId, message in messages_sorted:
Ed Tanous5b9ef702022-02-17 12:19:32 -0800100 registry.write(
101 " MessageEntry{{\n"
102 " \"{messageId}\",\n"
103 " {{\n"
104 " \"{message[Description]}\",\n"
105 " \"{message[Message]}\",\n"
Ed Tanous5b9ef702022-02-17 12:19:32 -0800106 " \"{message[MessageSeverity]}\",\n"
107 " {message[NumberOfArgs]},\n"
108 " {{".format(
109 messageId=messageId,
110 message=message
111 ))
Jason M. Bills70304cb2019-03-27 12:03:59 -0700112 paramTypes = message.get("ParamTypes")
113 if paramTypes:
114 for paramType in paramTypes:
Ed Tanous4d99bbb2022-02-17 10:02:57 -0800115 registry.write(
Ed Tanous5b9ef702022-02-17 12:19:32 -0800116 "\n"
117 " \"{}\",".format(paramType)
118 )
119 registry.write("\n },\n")
120 else:
121 registry.write("},\n")
122 registry.write(
123 " \"{message[Resolution]}\",\n"
124 " }}}},\n".format(message=message))
Ed Tanous30a3c432022-02-07 09:37:21 -0800125
Ed Tanous5b9ef702022-02-17 12:19:32 -0800126 registry.write(
127 "\n};\n"
128 "\n"
129 "enum class Index\n"
130 "{\n"
131 )
Ed Tanous30a3c432022-02-07 09:37:21 -0800132 for index, (messageId, message) in enumerate(messages_sorted):
133 messageId = messageId[0].lower() + messageId[1:]
134 registry.write(
Ed Tanous4d99bbb2022-02-17 10:02:57 -0800135 " {} = {},\n".format(messageId, index))
Ed Tanous4d99bbb2022-02-17 10:02:57 -0800136 registry.write(
Ed Tanous5b9ef702022-02-17 12:19:32 -0800137 "}};\n"
Ed Tanousfffb8c12022-02-07 23:53:03 -0800138 "}} // namespace redfish::registries::{}\n"
Ed Tanous5b9ef702022-02-17 12:19:32 -0800139 .format(namespace))
Ed Tanoused398212021-06-09 17:05:54 -0700140
141
142def get_privilege_string_from_list(privilege_list):
143 privilege_string = "{{\n"
144 for privilege_json in privilege_list:
145 privileges = privilege_json["Privilege"]
146 privilege_string += " {"
147 for privilege in privileges:
148 if privilege == "NoAuth":
149 continue
150 privilege_string += "\""
151 privilege_string += privilege
152 privilege_string += "\",\n"
153 if privilege != "NoAuth":
154 privilege_string = privilege_string[:-2]
155 privilege_string += "}"
156 privilege_string += ",\n"
157 privilege_string = privilege_string[:-2]
158 privilege_string += "\n}}"
159 return privilege_string
160
Ed Tanousf395daa2021-08-02 08:56:24 -0700161
Ed Tanoused398212021-06-09 17:05:54 -0700162def get_variable_name_for_privilege_set(privilege_list):
163 names = []
164 for privilege_json in privilege_list:
165 privileges = privilege_json["Privilege"]
166 names.append("And".join(privileges))
167 return "Or".join(names)
168
Ed Tanousf395daa2021-08-02 08:56:24 -0700169
Nan Zhou043bec02022-09-20 18:12:13 +0000170PRIVILEGE_HEADER = PRAGMA_ONCE + WARNING + '''
Nan Zhou01c78a02022-09-20 18:17:20 +0000171#include "privileges.hpp"
172
173#include <array>
Nan Zhou043bec02022-09-20 18:12:13 +0000174
175// clang-format off
176
177namespace redfish::privileges
178{
179'''
180
181
Ed Tanoused398212021-06-09 17:05:54 -0700182def make_privilege_registry():
Ed Tanousf395daa2021-08-02 08:56:24 -0700183 path, json_file, type_name, url = \
Sui Chen8bb5fbc2022-06-21 09:48:57 -0700184 make_getter('Redfish_1.3.0_PrivilegeRegistry.json',
Ed Tanousf395daa2021-08-02 08:56:24 -0700185 'privilege_registry.hpp', 'privilege')
Ed Tanoused398212021-06-09 17:05:54 -0700186 with open(path, 'w') as registry:
Nan Zhou043bec02022-09-20 18:12:13 +0000187 registry.write(PRIVILEGE_HEADER)
Ed Tanoused398212021-06-09 17:05:54 -0700188
189 privilege_dict = {}
190 for mapping in json_file["Mappings"]:
191 # first pass, identify all the unique privilege sets
192 for operation, privilege_list in mapping["OperationMap"].items():
193 privilege_dict[get_privilege_string_from_list(
194 privilege_list)] = (privilege_list, )
Ed Tanoused398212021-06-09 17:05:54 -0700195 for index, key in enumerate(privilege_dict):
196 (privilege_list, ) = privilege_dict[key]
197 name = get_variable_name_for_privilege_set(privilege_list)
Ed Tanousf395daa2021-08-02 08:56:24 -0700198 registry.write(
Ed Tanous5b9ef702022-02-17 12:19:32 -0800199 "const std::array<Privileges, {length}> "
200 "privilegeSet{name} = {key};\n"
201 .format(length=len(privilege_list), name=name, key=key)
202 )
Ed Tanoused398212021-06-09 17:05:54 -0700203 privilege_dict[key] = (privilege_list, name)
Ed Tanoused398212021-06-09 17:05:54 -0700204
205 for mapping in json_file["Mappings"]:
206 entity = mapping["Entity"]
Ed Tanous4d99bbb2022-02-17 10:02:57 -0800207 registry.write("// {}\n".format(entity))
Ed Tanoused398212021-06-09 17:05:54 -0700208 for operation, privilege_list in mapping["OperationMap"].items():
209 privilege_string = get_privilege_string_from_list(
210 privilege_list)
211 operation = operation.lower()
212
Ed Tanousf395daa2021-08-02 08:56:24 -0700213 registry.write(
214 "const static auto& {}{} = privilegeSet{};\n".format(
215 operation,
216 entity,
217 privilege_dict[privilege_string][1]))
Ed Tanoused398212021-06-09 17:05:54 -0700218 registry.write("\n")
Ed Tanous5b9ef702022-02-17 12:19:32 -0800219 registry.write(
220 "} // namespace redfish::privileges\n"
221 "// clang-format on\n")
Ed Tanoused398212021-06-09 17:05:54 -0700222
223
224make_privilege_registry()