blob: 8c300eda2e3f2a8f6172f34530143825de7ef66f [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 + '''
Jason M. Bills70304cb2019-03-27 12:03:59 -070022#include <registries.hpp>
23
Ed Tanous4d99bbb2022-02-17 10:02:57 -080024// clang-format off
25
Ed Tanousfffb8c12022-02-07 23:53:03 -080026namespace redfish::registries::{}
Jason M. Bills70304cb2019-03-27 12:03:59 -070027{{
Jason M. Bills70304cb2019-03-27 12:03:59 -070028'''
29
30SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
31
Jason M. Billsac706372020-02-18 11:36:47 -080032include_path = os.path.realpath(
33 os.path.join(
34 SCRIPT_DIR,
35 "..",
36 "redfish-core",
37 "include",
38 "registries"))
Jason M. Bills70304cb2019-03-27 12:03:59 -070039
40proxies = {
41 'https': os.environ.get("https_proxy", None)
42}
43
Jason M. Bills70304cb2019-03-27 12:03:59 -070044
James Feiste51c7102020-03-17 10:38:18 -070045def make_getter(dmtf_name, header_name, type_name):
46 url = 'https://redfish.dmtf.org/registries/{}'.format(dmtf_name)
47 dmtf = requests.get(url, proxies=proxies)
48 dmtf.raise_for_status()
49 json_file = json.loads(dmtf.text)
50 path = os.path.join(include_path, header_name)
51 return (path, json_file, type_name, url)
52
53
54files = []
Jiaqing Zhaof8666862022-06-24 15:01:03 +080055files.append(make_getter('Base.1.13.0.json',
Ed Tanousf395daa2021-08-02 08:56:24 -070056 'base_message_registry.hpp',
57 'base'))
Ed Tanousfc8a2b82021-06-09 16:29:31 -070058files.append(make_getter('TaskEvent.1.0.3.json',
Ed Tanousf395daa2021-08-02 08:56:24 -070059 'task_event_message_registry.hpp',
60 'task_event'))
Sunitha Harish74eec262020-06-25 10:00:01 -050061files.append(make_getter('ResourceEvent.1.0.3.json',
Ed Tanousf395daa2021-08-02 08:56:24 -070062 'resource_event_message_registry.hpp',
63 'resource_event'))
Jason M. Bills70304cb2019-03-27 12:03:59 -070064
65# Remove the old files
Ed Tanous413d8762021-06-09 16:24:19 -070066for file, json_dict, namespace, url in files:
Jason M. Bills70304cb2019-03-27 12:03:59 -070067 try:
68 os.remove(file)
Jason M. Billsac706372020-02-18 11:36:47 -080069 except BaseException:
Jason M. Bills70304cb2019-03-27 12:03:59 -070070 print("{} not found".format(file))
71
72 with open(file, 'w') as registry:
73 registry.write(REGISTRY_HEADER.format(namespace))
Jason M. Bills351d3062019-03-27 12:58:21 -070074 # Parse the Registry header info
Ed Tanous5b9ef702022-02-17 12:19:32 -080075 registry.write(
76 "const Header header = {{\n"
77 " \"{json_dict[@Redfish.Copyright]}\",\n"
78 " \"{json_dict[@odata.type]}\",\n"
79 " \"{json_dict[Id]}\",\n"
80 " \"{json_dict[Name]}\",\n"
81 " \"{json_dict[Language]}\",\n"
82 " \"{json_dict[Description]}\",\n"
83 " \"{json_dict[RegistryPrefix]}\",\n"
84 " \"{json_dict[RegistryVersion]}\",\n"
85 " \"{json_dict[OwningEntity]}\",\n"
86 "}};\n"
87 "constexpr const char* url =\n"
88 " \"{url}\";\n"
89 "\n"
Jason M. Bills0e2d0692022-04-01 13:59:05 -070090 "constexpr std::array registry =\n"
Ed Tanous5b9ef702022-02-17 12:19:32 -080091 "{{\n".format(
92 json_dict=json_dict,
93 url=url,
Ed Tanous5b9ef702022-02-17 12:19:32 -080094 ))
Jason M. Bills351d3062019-03-27 12:58:21 -070095
Ed Tanous30a3c432022-02-07 09:37:21 -080096 messages_sorted = sorted(json_dict["Messages"].items())
97 for messageId, message in messages_sorted:
Ed Tanous5b9ef702022-02-17 12:19:32 -080098 registry.write(
99 " MessageEntry{{\n"
100 " \"{messageId}\",\n"
101 " {{\n"
102 " \"{message[Description]}\",\n"
103 " \"{message[Message]}\",\n"
Ed Tanous5b9ef702022-02-17 12:19:32 -0800104 " \"{message[MessageSeverity]}\",\n"
105 " {message[NumberOfArgs]},\n"
106 " {{".format(
107 messageId=messageId,
108 message=message
109 ))
Jason M. Bills70304cb2019-03-27 12:03:59 -0700110 paramTypes = message.get("ParamTypes")
111 if paramTypes:
112 for paramType in paramTypes:
Ed Tanous4d99bbb2022-02-17 10:02:57 -0800113 registry.write(
Ed Tanous5b9ef702022-02-17 12:19:32 -0800114 "\n"
115 " \"{}\",".format(paramType)
116 )
117 registry.write("\n },\n")
118 else:
119 registry.write("},\n")
120 registry.write(
121 " \"{message[Resolution]}\",\n"
122 " }}}},\n".format(message=message))
Ed Tanous30a3c432022-02-07 09:37:21 -0800123
Ed Tanous5b9ef702022-02-17 12:19:32 -0800124 registry.write(
125 "\n};\n"
126 "\n"
127 "enum class Index\n"
128 "{\n"
129 )
Ed Tanous30a3c432022-02-07 09:37:21 -0800130 for index, (messageId, message) in enumerate(messages_sorted):
131 messageId = messageId[0].lower() + messageId[1:]
132 registry.write(
Ed Tanous4d99bbb2022-02-17 10:02:57 -0800133 " {} = {},\n".format(messageId, index))
Ed Tanous4d99bbb2022-02-17 10:02:57 -0800134 registry.write(
Ed Tanous5b9ef702022-02-17 12:19:32 -0800135 "}};\n"
Ed Tanousfffb8c12022-02-07 23:53:03 -0800136 "}} // namespace redfish::registries::{}\n"
Ed Tanous5b9ef702022-02-17 12:19:32 -0800137 .format(namespace))
Ed Tanoused398212021-06-09 17:05:54 -0700138
139
140def get_privilege_string_from_list(privilege_list):
141 privilege_string = "{{\n"
142 for privilege_json in privilege_list:
143 privileges = privilege_json["Privilege"]
144 privilege_string += " {"
145 for privilege in privileges:
146 if privilege == "NoAuth":
147 continue
148 privilege_string += "\""
149 privilege_string += privilege
150 privilege_string += "\",\n"
151 if privilege != "NoAuth":
152 privilege_string = privilege_string[:-2]
153 privilege_string += "}"
154 privilege_string += ",\n"
155 privilege_string = privilege_string[:-2]
156 privilege_string += "\n}}"
157 return privilege_string
158
Ed Tanousf395daa2021-08-02 08:56:24 -0700159
Ed Tanoused398212021-06-09 17:05:54 -0700160def get_variable_name_for_privilege_set(privilege_list):
161 names = []
162 for privilege_json in privilege_list:
163 privileges = privilege_json["Privilege"]
164 names.append("And".join(privileges))
165 return "Or".join(names)
166
Ed Tanousf395daa2021-08-02 08:56:24 -0700167
Nan Zhou043bec02022-09-20 18:12:13 +0000168PRIVILEGE_HEADER = PRAGMA_ONCE + WARNING + '''
169#include <privileges.hpp>
170
171// clang-format off
172
173namespace redfish::privileges
174{
175'''
176
177
Ed Tanoused398212021-06-09 17:05:54 -0700178def make_privilege_registry():
Ed Tanousf395daa2021-08-02 08:56:24 -0700179 path, json_file, type_name, url = \
Sui Chen8bb5fbc2022-06-21 09:48:57 -0700180 make_getter('Redfish_1.3.0_PrivilegeRegistry.json',
Ed Tanousf395daa2021-08-02 08:56:24 -0700181 'privilege_registry.hpp', 'privilege')
Ed Tanoused398212021-06-09 17:05:54 -0700182 with open(path, 'w') as registry:
Nan Zhou043bec02022-09-20 18:12:13 +0000183 registry.write(PRIVILEGE_HEADER)
Ed Tanoused398212021-06-09 17:05:54 -0700184
185 privilege_dict = {}
186 for mapping in json_file["Mappings"]:
187 # first pass, identify all the unique privilege sets
188 for operation, privilege_list in mapping["OperationMap"].items():
189 privilege_dict[get_privilege_string_from_list(
190 privilege_list)] = (privilege_list, )
Ed Tanoused398212021-06-09 17:05:54 -0700191 for index, key in enumerate(privilege_dict):
192 (privilege_list, ) = privilege_dict[key]
193 name = get_variable_name_for_privilege_set(privilege_list)
Ed Tanousf395daa2021-08-02 08:56:24 -0700194 registry.write(
Ed Tanous5b9ef702022-02-17 12:19:32 -0800195 "const std::array<Privileges, {length}> "
196 "privilegeSet{name} = {key};\n"
197 .format(length=len(privilege_list), name=name, key=key)
198 )
Ed Tanoused398212021-06-09 17:05:54 -0700199 privilege_dict[key] = (privilege_list, name)
Ed Tanoused398212021-06-09 17:05:54 -0700200
201 for mapping in json_file["Mappings"]:
202 entity = mapping["Entity"]
Ed Tanous4d99bbb2022-02-17 10:02:57 -0800203 registry.write("// {}\n".format(entity))
Ed Tanoused398212021-06-09 17:05:54 -0700204 for operation, privilege_list in mapping["OperationMap"].items():
205 privilege_string = get_privilege_string_from_list(
206 privilege_list)
207 operation = operation.lower()
208
Ed Tanousf395daa2021-08-02 08:56:24 -0700209 registry.write(
210 "const static auto& {}{} = privilegeSet{};\n".format(
211 operation,
212 entity,
213 privilege_dict[privilege_string][1]))
Ed Tanoused398212021-06-09 17:05:54 -0700214 registry.write("\n")
Ed Tanous5b9ef702022-02-17 12:19:32 -0800215 registry.write(
216 "} // namespace redfish::privileges\n"
217 "// clang-format on\n")
Ed Tanoused398212021-06-09 17:05:54 -0700218
219
220make_privilege_registry()