blob: 378684392aa1d2aa8ac68b95f32fa24cac299093 [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
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
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"
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"
Ed Tanous5b9ef702022-02-17 12:19:32 -0800112 " \"{message[MessageSeverity]}\",\n"
113 " {message[NumberOfArgs]},\n"
114 " {{".format(
115 messageId=messageId,
116 message=message
117 ))
Jason M. Bills70304cb2019-03-27 12:03:59 -0700118 paramTypes = message.get("ParamTypes")
119 if paramTypes:
120 for paramType in paramTypes:
Ed Tanous4d99bbb2022-02-17 10:02:57 -0800121 registry.write(
Ed Tanous5b9ef702022-02-17 12:19:32 -0800122 "\n"
123 " \"{}\",".format(paramType)
124 )
125 registry.write("\n },\n")
126 else:
127 registry.write("},\n")
128 registry.write(
129 " \"{message[Resolution]}\",\n"
130 " }}}},\n".format(message=message))
Ed Tanous30a3c432022-02-07 09:37:21 -0800131
Ed Tanous5b9ef702022-02-17 12:19:32 -0800132 registry.write(
133 "\n};\n"
134 "\n"
135 "enum class Index\n"
136 "{\n"
137 )
Ed Tanous30a3c432022-02-07 09:37:21 -0800138 for index, (messageId, message) in enumerate(messages_sorted):
139 messageId = messageId[0].lower() + messageId[1:]
140 registry.write(
Ed Tanous4d99bbb2022-02-17 10:02:57 -0800141 " {} = {},\n".format(messageId, index))
Ed Tanous4d99bbb2022-02-17 10:02:57 -0800142 registry.write(
Ed Tanous5b9ef702022-02-17 12:19:32 -0800143 "}};\n"
Ed Tanousfffb8c12022-02-07 23:53:03 -0800144 "}} // namespace redfish::registries::{}\n"
Ed Tanous5b9ef702022-02-17 12:19:32 -0800145 .format(namespace))
Ed Tanoused398212021-06-09 17:05:54 -0700146
147
148def get_privilege_string_from_list(privilege_list):
149 privilege_string = "{{\n"
150 for privilege_json in privilege_list:
151 privileges = privilege_json["Privilege"]
152 privilege_string += " {"
153 for privilege in privileges:
154 if privilege == "NoAuth":
155 continue
156 privilege_string += "\""
157 privilege_string += privilege
158 privilege_string += "\",\n"
159 if privilege != "NoAuth":
160 privilege_string = privilege_string[:-2]
161 privilege_string += "}"
162 privilege_string += ",\n"
163 privilege_string = privilege_string[:-2]
164 privilege_string += "\n}}"
165 return privilege_string
166
Ed Tanousf395daa2021-08-02 08:56:24 -0700167
Ed Tanoused398212021-06-09 17:05:54 -0700168def get_variable_name_for_privilege_set(privilege_list):
169 names = []
170 for privilege_json in privilege_list:
171 privileges = privilege_json["Privilege"]
172 names.append("And".join(privileges))
173 return "Or".join(names)
174
Ed Tanousf395daa2021-08-02 08:56:24 -0700175
Ed Tanoused398212021-06-09 17:05:54 -0700176def make_privilege_registry():
Ed Tanousf395daa2021-08-02 08:56:24 -0700177 path, json_file, type_name, url = \
Shantappa Teekappanavar12778e62022-01-05 12:00:56 -0600178 make_getter('Redfish_1.2.0_PrivilegeRegistry.json',
Ed Tanousf395daa2021-08-02 08:56:24 -0700179 'privilege_registry.hpp', 'privilege')
Ed Tanoused398212021-06-09 17:05:54 -0700180 with open(path, 'w') as registry:
Ed Tanous5b9ef702022-02-17 12:19:32 -0800181 registry.write(
182 "#pragma once\n"
183 "{WARNING}\n"
184 "// clang-format off\n"
185 "\n"
186 "#include <privileges.hpp>\n"
187 "\n"
188 "namespace redfish::privileges\n"
189 "{{\n"
190 .format(
191 WARNING=WARNING,
192 filename=os.path.basename(path)))
Ed Tanoused398212021-06-09 17:05:54 -0700193
194 privilege_dict = {}
195 for mapping in json_file["Mappings"]:
196 # first pass, identify all the unique privilege sets
197 for operation, privilege_list in mapping["OperationMap"].items():
198 privilege_dict[get_privilege_string_from_list(
199 privilege_list)] = (privilege_list, )
Ed Tanoused398212021-06-09 17:05:54 -0700200 for index, key in enumerate(privilege_dict):
201 (privilege_list, ) = privilege_dict[key]
202 name = get_variable_name_for_privilege_set(privilege_list)
Ed Tanousf395daa2021-08-02 08:56:24 -0700203 registry.write(
Ed Tanous5b9ef702022-02-17 12:19:32 -0800204 "const std::array<Privileges, {length}> "
205 "privilegeSet{name} = {key};\n"
206 .format(length=len(privilege_list), name=name, key=key)
207 )
Ed Tanoused398212021-06-09 17:05:54 -0700208 privilege_dict[key] = (privilege_list, name)
Ed Tanoused398212021-06-09 17:05:54 -0700209
210 for mapping in json_file["Mappings"]:
211 entity = mapping["Entity"]
Ed Tanous4d99bbb2022-02-17 10:02:57 -0800212 registry.write("// {}\n".format(entity))
Ed Tanoused398212021-06-09 17:05:54 -0700213 for operation, privilege_list in mapping["OperationMap"].items():
214 privilege_string = get_privilege_string_from_list(
215 privilege_list)
216 operation = operation.lower()
217
Ed Tanousf395daa2021-08-02 08:56:24 -0700218 registry.write(
219 "const static auto& {}{} = privilegeSet{};\n".format(
220 operation,
221 entity,
222 privilege_dict[privilege_string][1]))
Ed Tanoused398212021-06-09 17:05:54 -0700223 registry.write("\n")
Ed Tanous5b9ef702022-02-17 12:19:32 -0800224 registry.write(
225 "} // namespace redfish::privileges\n"
226 "// clang-format on\n")
Ed Tanoused398212021-06-09 17:05:54 -0700227
228
229make_privilege_registry()