blob: 731a11671a8e76b00c0e5b234de48949fb122a48 [file] [log] [blame]
Nan Zhou313c1b72022-03-25 11:47:55 -07001#!/usr/bin/env python3
Jason M. Bills70304cb2019-03-27 12:03:59 -07002import requests
3import zipfile
4from io import BytesIO
5import os
6from collections import defaultdict
7from collections import OrderedDict
8from distutils.version import StrictVersion
9import shutil
10import json
11import glob
12import subprocess
13
14import xml.etree.ElementTree as ET
15
Ed Tanous1cf53df2022-02-17 09:09:25 -080016WARNING = '''/****************************************************************
17 * READ THIS WARNING FIRST
Jason M. Bills70304cb2019-03-27 12:03:59 -070018 * This is an auto-generated header which contains definitions
19 * for Redfish DMTF defined messages.
Ed Tanous1cf53df2022-02-17 09:09:25 -080020 * DO NOT modify this registry outside of running the
Jason M. Bills0e2d0692022-04-01 13:59:05 -070021 * parse_registries.py script. The definitions contained within
Ed Tanous1cf53df2022-02-17 09:09:25 -080022 * this file are owned by DMTF. Any modifications to these files
23 * should be first pushed to the relevant registry in the DMTF
24 * github organization.
25 ***************************************************************/'''
26
27REGISTRY_HEADER = WARNING + '''
Jason M. Bills70304cb2019-03-27 12:03:59 -070028#pragma once
29#include <registries.hpp>
30
Ed Tanous4d99bbb2022-02-17 10:02:57 -080031// clang-format off
32
Ed Tanousfffb8c12022-02-07 23:53:03 -080033namespace redfish::registries::{}
Jason M. Bills70304cb2019-03-27 12:03:59 -070034{{
Jason M. Bills70304cb2019-03-27 12:03:59 -070035'''
36
37SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
38
Jason M. Billsac706372020-02-18 11:36:47 -080039include_path = os.path.realpath(
40 os.path.join(
41 SCRIPT_DIR,
42 "..",
43 "redfish-core",
44 "include",
45 "registries"))
Jason M. Bills70304cb2019-03-27 12:03:59 -070046
47proxies = {
48 'https': os.environ.get("https_proxy", None)
49}
50
Jason M. Bills70304cb2019-03-27 12:03:59 -070051
James Feiste51c7102020-03-17 10:38:18 -070052def make_getter(dmtf_name, header_name, type_name):
53 url = 'https://redfish.dmtf.org/registries/{}'.format(dmtf_name)
54 dmtf = requests.get(url, proxies=proxies)
55 dmtf.raise_for_status()
56 json_file = json.loads(dmtf.text)
57 path = os.path.join(include_path, header_name)
58 return (path, json_file, type_name, url)
59
60
61files = []
Sunitha Harish83f984b2021-09-23 04:29:46 -050062files.append(make_getter('Base.1.11.0.json',
Ed Tanousf395daa2021-08-02 08:56:24 -070063 'base_message_registry.hpp',
64 'base'))
Ed Tanousfc8a2b82021-06-09 16:29:31 -070065files.append(make_getter('TaskEvent.1.0.3.json',
Ed Tanousf395daa2021-08-02 08:56:24 -070066 'task_event_message_registry.hpp',
67 'task_event'))
Sunitha Harish74eec262020-06-25 10:00:01 -050068files.append(make_getter('ResourceEvent.1.0.3.json',
Ed Tanousf395daa2021-08-02 08:56:24 -070069 'resource_event_message_registry.hpp',
70 'resource_event'))
Jason M. Bills70304cb2019-03-27 12:03:59 -070071
72# Remove the old files
Ed Tanous413d8762021-06-09 16:24:19 -070073for file, json_dict, namespace, url in files:
Jason M. Bills70304cb2019-03-27 12:03:59 -070074 try:
75 os.remove(file)
Jason M. Billsac706372020-02-18 11:36:47 -080076 except BaseException:
Jason M. Bills70304cb2019-03-27 12:03:59 -070077 print("{} not found".format(file))
78
79 with open(file, 'w') as registry:
80 registry.write(REGISTRY_HEADER.format(namespace))
Jason M. Bills351d3062019-03-27 12:58:21 -070081 # Parse the Registry header info
Ed Tanous5b9ef702022-02-17 12:19:32 -080082 registry.write(
83 "const Header header = {{\n"
84 " \"{json_dict[@Redfish.Copyright]}\",\n"
85 " \"{json_dict[@odata.type]}\",\n"
86 " \"{json_dict[Id]}\",\n"
87 " \"{json_dict[Name]}\",\n"
88 " \"{json_dict[Language]}\",\n"
89 " \"{json_dict[Description]}\",\n"
90 " \"{json_dict[RegistryPrefix]}\",\n"
91 " \"{json_dict[RegistryVersion]}\",\n"
92 " \"{json_dict[OwningEntity]}\",\n"
93 "}};\n"
94 "constexpr const char* url =\n"
95 " \"{url}\";\n"
96 "\n"
Jason M. Bills0e2d0692022-04-01 13:59:05 -070097 "constexpr std::array registry =\n"
Ed Tanous5b9ef702022-02-17 12:19:32 -080098 "{{\n".format(
99 json_dict=json_dict,
100 url=url,
Ed Tanous5b9ef702022-02-17 12:19:32 -0800101 ))
Jason M. Bills351d3062019-03-27 12:58:21 -0700102
Ed Tanous30a3c432022-02-07 09:37:21 -0800103 messages_sorted = sorted(json_dict["Messages"].items())
104 for messageId, message in messages_sorted:
Ed Tanous5b9ef702022-02-17 12:19:32 -0800105 registry.write(
106 " MessageEntry{{\n"
107 " \"{messageId}\",\n"
108 " {{\n"
109 " \"{message[Description]}\",\n"
110 " \"{message[Message]}\",\n"
Ed Tanous5b9ef702022-02-17 12:19:32 -0800111 " \"{message[MessageSeverity]}\",\n"
112 " {message[NumberOfArgs]},\n"
113 " {{".format(
114 messageId=messageId,
115 message=message
116 ))
Jason M. Bills70304cb2019-03-27 12:03:59 -0700117 paramTypes = message.get("ParamTypes")
118 if paramTypes:
119 for paramType in paramTypes:
Ed Tanous4d99bbb2022-02-17 10:02:57 -0800120 registry.write(
Ed Tanous5b9ef702022-02-17 12:19:32 -0800121 "\n"
122 " \"{}\",".format(paramType)
123 )
124 registry.write("\n },\n")
125 else:
126 registry.write("},\n")
127 registry.write(
128 " \"{message[Resolution]}\",\n"
129 " }}}},\n".format(message=message))
Ed Tanous30a3c432022-02-07 09:37:21 -0800130
Ed Tanous5b9ef702022-02-17 12:19:32 -0800131 registry.write(
132 "\n};\n"
133 "\n"
134 "enum class Index\n"
135 "{\n"
136 )
Ed Tanous30a3c432022-02-07 09:37:21 -0800137 for index, (messageId, message) in enumerate(messages_sorted):
138 messageId = messageId[0].lower() + messageId[1:]
139 registry.write(
Ed Tanous4d99bbb2022-02-17 10:02:57 -0800140 " {} = {},\n".format(messageId, index))
Ed Tanous4d99bbb2022-02-17 10:02:57 -0800141 registry.write(
Ed Tanous5b9ef702022-02-17 12:19:32 -0800142 "}};\n"
Ed Tanousfffb8c12022-02-07 23:53:03 -0800143 "}} // namespace redfish::registries::{}\n"
Ed Tanous5b9ef702022-02-17 12:19:32 -0800144 .format(namespace))
Ed Tanoused398212021-06-09 17:05:54 -0700145
146
147def get_privilege_string_from_list(privilege_list):
148 privilege_string = "{{\n"
149 for privilege_json in privilege_list:
150 privileges = privilege_json["Privilege"]
151 privilege_string += " {"
152 for privilege in privileges:
153 if privilege == "NoAuth":
154 continue
155 privilege_string += "\""
156 privilege_string += privilege
157 privilege_string += "\",\n"
158 if privilege != "NoAuth":
159 privilege_string = privilege_string[:-2]
160 privilege_string += "}"
161 privilege_string += ",\n"
162 privilege_string = privilege_string[:-2]
163 privilege_string += "\n}}"
164 return privilege_string
165
Ed Tanousf395daa2021-08-02 08:56:24 -0700166
Ed Tanoused398212021-06-09 17:05:54 -0700167def get_variable_name_for_privilege_set(privilege_list):
168 names = []
169 for privilege_json in privilege_list:
170 privileges = privilege_json["Privilege"]
171 names.append("And".join(privileges))
172 return "Or".join(names)
173
Ed Tanousf395daa2021-08-02 08:56:24 -0700174
Ed Tanoused398212021-06-09 17:05:54 -0700175def make_privilege_registry():
Ed Tanousf395daa2021-08-02 08:56:24 -0700176 path, json_file, type_name, url = \
Sui Chen8bb5fbc2022-06-21 09:48:57 -0700177 make_getter('Redfish_1.3.0_PrivilegeRegistry.json',
Ed Tanousf395daa2021-08-02 08:56:24 -0700178 'privilege_registry.hpp', 'privilege')
Ed Tanoused398212021-06-09 17:05:54 -0700179 with open(path, 'w') as registry:
Ed Tanous5b9ef702022-02-17 12:19:32 -0800180 registry.write(
181 "#pragma once\n"
182 "{WARNING}\n"
183 "// clang-format off\n"
184 "\n"
185 "#include <privileges.hpp>\n"
186 "\n"
187 "namespace redfish::privileges\n"
188 "{{\n"
189 .format(
190 WARNING=WARNING,
191 filename=os.path.basename(path)))
Ed Tanoused398212021-06-09 17:05:54 -0700192
193 privilege_dict = {}
194 for mapping in json_file["Mappings"]:
195 # first pass, identify all the unique privilege sets
196 for operation, privilege_list in mapping["OperationMap"].items():
197 privilege_dict[get_privilege_string_from_list(
198 privilege_list)] = (privilege_list, )
Ed Tanoused398212021-06-09 17:05:54 -0700199 for index, key in enumerate(privilege_dict):
200 (privilege_list, ) = privilege_dict[key]
201 name = get_variable_name_for_privilege_set(privilege_list)
Ed Tanousf395daa2021-08-02 08:56:24 -0700202 registry.write(
Ed Tanous5b9ef702022-02-17 12:19:32 -0800203 "const std::array<Privileges, {length}> "
204 "privilegeSet{name} = {key};\n"
205 .format(length=len(privilege_list), name=name, key=key)
206 )
Ed Tanoused398212021-06-09 17:05:54 -0700207 privilege_dict[key] = (privilege_list, name)
Ed Tanoused398212021-06-09 17:05:54 -0700208
209 for mapping in json_file["Mappings"]:
210 entity = mapping["Entity"]
Ed Tanous4d99bbb2022-02-17 10:02:57 -0800211 registry.write("// {}\n".format(entity))
Ed Tanoused398212021-06-09 17:05:54 -0700212 for operation, privilege_list in mapping["OperationMap"].items():
213 privilege_string = get_privilege_string_from_list(
214 privilege_list)
215 operation = operation.lower()
216
Ed Tanousf395daa2021-08-02 08:56:24 -0700217 registry.write(
218 "const static auto& {}{} = privilegeSet{};\n".format(
219 operation,
220 entity,
221 privilege_dict[privilege_string][1]))
Ed Tanoused398212021-06-09 17:05:54 -0700222 registry.write("\n")
Ed Tanous5b9ef702022-02-17 12:19:32 -0800223 registry.write(
224 "} // namespace redfish::privileges\n"
225 "// clang-format on\n")
Ed Tanoused398212021-06-09 17:05:54 -0700226
227
228make_privilege_registry()