blob: a8b50bfab38d345a6a5700ff998fe645afd2244d [file] [log] [blame]
Jason M. Bills70304cb2019-03-27 12:03:59 -07001#!/usr/bin/python3
2import 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
21 * parse_regisries.py script. The definitions contained within
22 * 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
Jason M. Bills70304cb2019-03-27 12:03:59 -070033namespace redfish::message_registries::{}
34{{
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"
97 "constexpr std::array<MessageEntry, {message_len}> registry =\n"
98 "{{\n".format(
99 json_dict=json_dict,
100 url=url,
101 message_len=len(json_dict["Messages"]),
102 ))
Jason M. Bills351d3062019-03-27 12:58:21 -0700103
Ed Tanous30a3c432022-02-07 09:37:21 -0800104 messages_sorted = sorted(json_dict["Messages"].items())
105 for messageId, message in messages_sorted:
Ed Tanous5b9ef702022-02-17 12:19:32 -0800106 registry.write(
107 " MessageEntry{{\n"
108 " \"{messageId}\",\n"
109 " {{\n"
110 " \"{message[Description]}\",\n"
111 " \"{message[Message]}\",\n"
112 " \"{message[Severity]}\",\n"
113 " \"{message[MessageSeverity]}\",\n"
114 " {message[NumberOfArgs]},\n"
115 " {{".format(
116 messageId=messageId,
117 message=message
118 ))
Jason M. Bills70304cb2019-03-27 12:03:59 -0700119 paramTypes = message.get("ParamTypes")
120 if paramTypes:
121 for paramType in paramTypes:
Ed Tanous4d99bbb2022-02-17 10:02:57 -0800122 registry.write(
Ed Tanous5b9ef702022-02-17 12:19:32 -0800123 "\n"
124 " \"{}\",".format(paramType)
125 )
126 registry.write("\n },\n")
127 else:
128 registry.write("},\n")
129 registry.write(
130 " \"{message[Resolution]}\",\n"
131 " }}}},\n".format(message=message))
Ed Tanous30a3c432022-02-07 09:37:21 -0800132
Ed Tanous5b9ef702022-02-17 12:19:32 -0800133 registry.write(
134 "\n};\n"
135 "\n"
136 "enum class Index\n"
137 "{\n"
138 )
Ed Tanous30a3c432022-02-07 09:37:21 -0800139 for index, (messageId, message) in enumerate(messages_sorted):
140 messageId = messageId[0].lower() + messageId[1:]
141 registry.write(
Ed Tanous4d99bbb2022-02-17 10:02:57 -0800142 " {} = {},\n".format(messageId, index))
Ed Tanous4d99bbb2022-02-17 10:02:57 -0800143 registry.write(
Ed Tanous5b9ef702022-02-17 12:19:32 -0800144 "}};\n"
145 "}} // namespace redfish::message_registries::{}\n"
146 .format(namespace))
Ed Tanoused398212021-06-09 17:05:54 -0700147
148
149def get_privilege_string_from_list(privilege_list):
150 privilege_string = "{{\n"
151 for privilege_json in privilege_list:
152 privileges = privilege_json["Privilege"]
153 privilege_string += " {"
154 for privilege in privileges:
155 if privilege == "NoAuth":
156 continue
157 privilege_string += "\""
158 privilege_string += privilege
159 privilege_string += "\",\n"
160 if privilege != "NoAuth":
161 privilege_string = privilege_string[:-2]
162 privilege_string += "}"
163 privilege_string += ",\n"
164 privilege_string = privilege_string[:-2]
165 privilege_string += "\n}}"
166 return privilege_string
167
Ed Tanousf395daa2021-08-02 08:56:24 -0700168
Ed Tanoused398212021-06-09 17:05:54 -0700169def get_variable_name_for_privilege_set(privilege_list):
170 names = []
171 for privilege_json in privilege_list:
172 privileges = privilege_json["Privilege"]
173 names.append("And".join(privileges))
174 return "Or".join(names)
175
Ed Tanousf395daa2021-08-02 08:56:24 -0700176
Ed Tanoused398212021-06-09 17:05:54 -0700177def make_privilege_registry():
Ed Tanousf395daa2021-08-02 08:56:24 -0700178 path, json_file, type_name, url = \
Shantappa Teekappanavar12778e62022-01-05 12:00:56 -0600179 make_getter('Redfish_1.2.0_PrivilegeRegistry.json',
Ed Tanousf395daa2021-08-02 08:56:24 -0700180 'privilege_registry.hpp', 'privilege')
Ed Tanoused398212021-06-09 17:05:54 -0700181 with open(path, 'w') as registry:
Ed Tanous5b9ef702022-02-17 12:19:32 -0800182 registry.write(
183 "#pragma once\n"
184 "{WARNING}\n"
185 "// clang-format off\n"
186 "\n"
187 "#include <privileges.hpp>\n"
188 "\n"
189 "namespace redfish::privileges\n"
190 "{{\n"
191 .format(
192 WARNING=WARNING,
193 filename=os.path.basename(path)))
Ed Tanoused398212021-06-09 17:05:54 -0700194
195 privilege_dict = {}
196 for mapping in json_file["Mappings"]:
197 # first pass, identify all the unique privilege sets
198 for operation, privilege_list in mapping["OperationMap"].items():
199 privilege_dict[get_privilege_string_from_list(
200 privilege_list)] = (privilege_list, )
Ed Tanoused398212021-06-09 17:05:54 -0700201 for index, key in enumerate(privilege_dict):
202 (privilege_list, ) = privilege_dict[key]
203 name = get_variable_name_for_privilege_set(privilege_list)
Ed Tanousf395daa2021-08-02 08:56:24 -0700204 registry.write(
Ed Tanous5b9ef702022-02-17 12:19:32 -0800205 "const std::array<Privileges, {length}> "
206 "privilegeSet{name} = {key};\n"
207 .format(length=len(privilege_list), name=name, key=key)
208 )
Ed Tanoused398212021-06-09 17:05:54 -0700209 privilege_dict[key] = (privilege_list, name)
Ed Tanoused398212021-06-09 17:05:54 -0700210
211 for mapping in json_file["Mappings"]:
212 entity = mapping["Entity"]
Ed Tanous4d99bbb2022-02-17 10:02:57 -0800213 registry.write("// {}\n".format(entity))
Ed Tanoused398212021-06-09 17:05:54 -0700214 for operation, privilege_list in mapping["OperationMap"].items():
215 privilege_string = get_privilege_string_from_list(
216 privilege_list)
217 operation = operation.lower()
218
Ed Tanousf395daa2021-08-02 08:56:24 -0700219 registry.write(
220 "const static auto& {}{} = privilegeSet{};\n".format(
221 operation,
222 entity,
223 privilege_dict[privilege_string][1]))
Ed Tanoused398212021-06-09 17:05:54 -0700224 registry.write("\n")
Ed Tanous5b9ef702022-02-17 12:19:32 -0800225 registry.write(
226 "} // namespace redfish::privileges\n"
227 "// clang-format on\n")
Ed Tanoused398212021-06-09 17:05:54 -0700228
229
230make_privilege_registry()