Nan Zhou | 313c1b7 | 2022-03-25 11:47:55 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 2 | import argparse |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 3 | import json |
Nan Zhou | 6eaf0bd | 2022-08-07 01:18:45 +0000 | [diff] [blame] | 4 | import os |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 5 | |
Nan Zhou | 6eaf0bd | 2022-08-07 01:18:45 +0000 | [diff] [blame] | 6 | import requests |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 7 | |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 8 | PRAGMA_ONCE = """#pragma once |
| 9 | """ |
Nan Zhou | 043bec0 | 2022-09-20 18:12:13 +0000 | [diff] [blame] | 10 | |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 11 | WARNING = """/**************************************************************** |
Ed Tanous | 1cf53df | 2022-02-17 09:09:25 -0800 | [diff] [blame] | 12 | * READ THIS WARNING FIRST |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 13 | * This is an auto-generated header which contains definitions |
| 14 | * for Redfish DMTF defined messages. |
Ed Tanous | 1cf53df | 2022-02-17 09:09:25 -0800 | [diff] [blame] | 15 | * DO NOT modify this registry outside of running the |
Jason M. Bills | 0e2d069 | 2022-04-01 13:59:05 -0700 | [diff] [blame] | 16 | * parse_registries.py script. The definitions contained within |
Ed Tanous | 1cf53df | 2022-02-17 09:09:25 -0800 | [diff] [blame] | 17 | * this file are owned by DMTF. Any modifications to these files |
| 18 | * should be first pushed to the relevant registry in the DMTF |
| 19 | * github organization. |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 20 | ***************************************************************/""" |
Ed Tanous | 1cf53df | 2022-02-17 09:09:25 -0800 | [diff] [blame] | 21 | |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 22 | REGISTRY_HEADER = ( |
| 23 | PRAGMA_ONCE |
| 24 | + WARNING |
| 25 | + """ |
Nan Zhou | 01c78a0 | 2022-09-20 18:17:20 +0000 | [diff] [blame] | 26 | #include "registries.hpp" |
| 27 | |
| 28 | #include <array> |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 29 | |
Ed Tanous | 4d99bbb | 2022-02-17 10:02:57 -0800 | [diff] [blame] | 30 | // clang-format off |
| 31 | |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 32 | namespace redfish::registries::{} |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 33 | {{ |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 34 | """ |
| 35 | ) |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 36 | |
| 37 | SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__)) |
| 38 | |
Jason M. Bills | ac70637 | 2020-02-18 11:36:47 -0800 | [diff] [blame] | 39 | include_path = os.path.realpath( |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 40 | os.path.join(SCRIPT_DIR, "..", "redfish-core", "include", "registries") |
| 41 | ) |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 42 | |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 43 | proxies = {"https": os.environ.get("https_proxy", None)} |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 44 | |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 45 | |
James Feist | e51c710 | 2020-03-17 10:38:18 -0700 | [diff] [blame] | 46 | def make_getter(dmtf_name, header_name, type_name): |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 47 | url = "https://redfish.dmtf.org/registries/{}".format(dmtf_name) |
James Feist | e51c710 | 2020-03-17 10:38:18 -0700 | [diff] [blame] | 48 | dmtf = requests.get(url, proxies=proxies) |
| 49 | dmtf.raise_for_status() |
| 50 | json_file = json.loads(dmtf.text) |
| 51 | path = os.path.join(include_path, header_name) |
| 52 | return (path, json_file, type_name, url) |
| 53 | |
| 54 | |
Ed Tanous | 3e5faba | 2023-08-16 15:11:29 -0700 | [diff] [blame] | 55 | def openbmc_local_getter(): |
| 56 | url = "" |
| 57 | with open( |
| 58 | os.path.join( |
| 59 | SCRIPT_DIR, |
| 60 | "..", |
| 61 | "redfish-core", |
| 62 | "include", |
| 63 | "registries", |
| 64 | "openbmc.json", |
| 65 | ), |
| 66 | "rb", |
| 67 | ) as json_file: |
| 68 | json_file = json.load(json_file) |
| 69 | |
| 70 | path = os.path.join(include_path, "openbmc_message_registry.hpp") |
| 71 | return (path, json_file, "openbmc", url) |
| 72 | |
| 73 | |
Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 74 | def update_registries(files): |
| 75 | # Remove the old files |
| 76 | for file, json_dict, namespace, url in files: |
| 77 | try: |
| 78 | os.remove(file) |
| 79 | except BaseException: |
| 80 | print("{} not found".format(file)) |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 81 | |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 82 | with open(file, "w") as registry: |
Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 83 | registry.write(REGISTRY_HEADER.format(namespace)) |
| 84 | # Parse the Registry header info |
Ed Tanous | 5b9ef70 | 2022-02-17 12:19:32 -0800 | [diff] [blame] | 85 | registry.write( |
Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 86 | "const Header header = {{\n" |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 87 | ' "{json_dict[@Redfish.Copyright]}",\n' |
| 88 | ' "{json_dict[@odata.type]}",\n' |
| 89 | ' "{json_dict[Id]}",\n' |
| 90 | ' "{json_dict[Name]}",\n' |
| 91 | ' "{json_dict[Language]}",\n' |
| 92 | ' "{json_dict[Description]}",\n' |
| 93 | ' "{json_dict[RegistryPrefix]}",\n' |
| 94 | ' "{json_dict[RegistryVersion]}",\n' |
| 95 | ' "{json_dict[OwningEntity]}",\n' |
Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 96 | "}};\n" |
| 97 | "constexpr const char* url =\n" |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 98 | ' "{url}";\n' |
Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 99 | "\n" |
| 100 | "constexpr std::array registry =\n" |
| 101 | "{{\n".format( |
| 102 | json_dict=json_dict, |
| 103 | url=url, |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 104 | ) |
| 105 | ) |
Ed Tanous | 30a3c43 | 2022-02-07 09:37:21 -0800 | [diff] [blame] | 106 | |
Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 107 | messages_sorted = sorted(json_dict["Messages"].items()) |
| 108 | for messageId, message in messages_sorted: |
| 109 | registry.write( |
| 110 | " MessageEntry{{\n" |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 111 | ' "{messageId}",\n' |
Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 112 | " {{\n" |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 113 | ' "{message[Description]}",\n' |
| 114 | ' "{message[Message]}",\n' |
| 115 | ' "{message[MessageSeverity]}",\n' |
Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 116 | " {message[NumberOfArgs]},\n" |
| 117 | " {{".format( |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 118 | messageId=messageId, message=message |
| 119 | ) |
| 120 | ) |
Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 121 | paramTypes = message.get("ParamTypes") |
| 122 | if paramTypes: |
| 123 | for paramType in paramTypes: |
| 124 | registry.write( |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 125 | '\n "{}",'.format(paramType) |
Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 126 | ) |
| 127 | registry.write("\n },\n") |
| 128 | else: |
| 129 | registry.write("},\n") |
| 130 | registry.write( |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 131 | ' "{message[Resolution]}",\n' |
| 132 | " }}}},\n".format(message=message) |
| 133 | ) |
Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 134 | |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 135 | registry.write("\n};\n\nenum class Index\n{\n") |
Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 136 | for index, (messageId, message) in enumerate(messages_sorted): |
| 137 | messageId = messageId[0].lower() + messageId[1:] |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 138 | registry.write(" {} = {},\n".format(messageId, index)) |
Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 139 | registry.write( |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 140 | "}};\n}} // namespace redfish::registries::{}\n".format( |
| 141 | namespace |
| 142 | ) |
| 143 | ) |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 144 | |
| 145 | |
| 146 | def get_privilege_string_from_list(privilege_list): |
| 147 | privilege_string = "{{\n" |
| 148 | for privilege_json in privilege_list: |
| 149 | privileges = privilege_json["Privilege"] |
| 150 | privilege_string += " {" |
| 151 | for privilege in privileges: |
| 152 | if privilege == "NoAuth": |
| 153 | continue |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 154 | privilege_string += '"' |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 155 | privilege_string += privilege |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 156 | privilege_string += '",\n' |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 157 | if privilege != "NoAuth": |
| 158 | privilege_string = privilege_string[:-2] |
| 159 | privilege_string += "}" |
| 160 | privilege_string += ",\n" |
| 161 | privilege_string = privilege_string[:-2] |
| 162 | privilege_string += "\n}}" |
| 163 | return privilege_string |
| 164 | |
Ed Tanous | f395daa | 2021-08-02 08:56:24 -0700 | [diff] [blame] | 165 | |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 166 | def get_variable_name_for_privilege_set(privilege_list): |
| 167 | names = [] |
| 168 | for privilege_json in privilege_list: |
| 169 | privileges = privilege_json["Privilege"] |
| 170 | names.append("And".join(privileges)) |
| 171 | return "Or".join(names) |
| 172 | |
Ed Tanous | f395daa | 2021-08-02 08:56:24 -0700 | [diff] [blame] | 173 | |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 174 | PRIVILEGE_HEADER = ( |
| 175 | PRAGMA_ONCE |
| 176 | + WARNING |
| 177 | + """ |
Nan Zhou | 01c78a0 | 2022-09-20 18:17:20 +0000 | [diff] [blame] | 178 | #include "privileges.hpp" |
| 179 | |
| 180 | #include <array> |
Nan Zhou | 043bec0 | 2022-09-20 18:12:13 +0000 | [diff] [blame] | 181 | |
| 182 | // clang-format off |
| 183 | |
| 184 | namespace redfish::privileges |
| 185 | { |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 186 | """ |
| 187 | ) |
Nan Zhou | 043bec0 | 2022-09-20 18:12:13 +0000 | [diff] [blame] | 188 | |
| 189 | |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 190 | def make_privilege_registry(): |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 191 | path, json_file, type_name, url = make_getter( |
Gunnar Mills | 5910d94 | 2024-04-16 12:07:17 -0500 | [diff] [blame] | 192 | "Redfish_1.5.0_PrivilegeRegistry.json", |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 193 | "privilege_registry.hpp", |
| 194 | "privilege", |
| 195 | ) |
| 196 | with open(path, "w") as registry: |
Nan Zhou | 043bec0 | 2022-09-20 18:12:13 +0000 | [diff] [blame] | 197 | registry.write(PRIVILEGE_HEADER) |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 198 | |
| 199 | privilege_dict = {} |
| 200 | for mapping in json_file["Mappings"]: |
| 201 | # first pass, identify all the unique privilege sets |
| 202 | for operation, privilege_list in mapping["OperationMap"].items(): |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 203 | privilege_dict[ |
| 204 | get_privilege_string_from_list(privilege_list) |
| 205 | ] = (privilege_list,) |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 206 | for index, key in enumerate(privilege_dict): |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 207 | (privilege_list,) = privilege_dict[key] |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 208 | name = get_variable_name_for_privilege_set(privilege_list) |
Ed Tanous | f395daa | 2021-08-02 08:56:24 -0700 | [diff] [blame] | 209 | registry.write( |
Ed Tanous | 5b9ef70 | 2022-02-17 12:19:32 -0800 | [diff] [blame] | 210 | "const std::array<Privileges, {length}> " |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 211 | "privilegeSet{name} = {key};\n".format( |
| 212 | length=len(privilege_list), name=name, key=key |
| 213 | ) |
Ed Tanous | 5b9ef70 | 2022-02-17 12:19:32 -0800 | [diff] [blame] | 214 | ) |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 215 | privilege_dict[key] = (privilege_list, name) |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 216 | |
| 217 | for mapping in json_file["Mappings"]: |
| 218 | entity = mapping["Entity"] |
Ed Tanous | 4d99bbb | 2022-02-17 10:02:57 -0800 | [diff] [blame] | 219 | registry.write("// {}\n".format(entity)) |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 220 | for operation, privilege_list in mapping["OperationMap"].items(): |
| 221 | privilege_string = get_privilege_string_from_list( |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 222 | privilege_list |
| 223 | ) |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 224 | operation = operation.lower() |
| 225 | |
Ed Tanous | f395daa | 2021-08-02 08:56:24 -0700 | [diff] [blame] | 226 | registry.write( |
| 227 | "const static auto& {}{} = privilegeSet{};\n".format( |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 228 | operation, entity, privilege_dict[privilege_string][1] |
| 229 | ) |
| 230 | ) |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 231 | registry.write("\n") |
Ed Tanous | 5b9ef70 | 2022-02-17 12:19:32 -0800 | [diff] [blame] | 232 | registry.write( |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 233 | "} // namespace redfish::privileges\n// clang-format on\n" |
| 234 | ) |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 235 | |
| 236 | |
Gunnar Mills | 665e760 | 2024-04-10 13:14:41 -0500 | [diff] [blame] | 237 | def to_pascal_case(text): |
| 238 | s = text.replace("_", " ") |
| 239 | s = s.split() |
| 240 | if len(text) == 0: |
| 241 | return text |
| 242 | return "".join(i.capitalize() for i in s[0:]) |
| 243 | |
| 244 | |
Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 245 | def main(): |
Gunnar Mills | 665e760 | 2024-04-10 13:14:41 -0500 | [diff] [blame] | 246 | dmtf_registries = ( |
Gunnar Mills | d8d5fc3 | 2024-06-18 10:52:56 -0500 | [diff] [blame] | 247 | ("base", "1.18.1"), |
Gunnar Mills | 665e760 | 2024-04-10 13:14:41 -0500 | [diff] [blame] | 248 | ("composition", "1.1.2"), |
| 249 | ("environmental", "1.0.1"), |
| 250 | ("ethernet_fabric", "1.0.1"), |
| 251 | ("fabric", "1.0.2"), |
| 252 | ("heartbeat_event", "1.0.1"), |
| 253 | ("job_event", "1.0.1"), |
| 254 | ("license", "1.0.3"), |
| 255 | ("log_service", "1.0.1"), |
| 256 | ("network_device", "1.0.3"), |
| 257 | ("platform", "1.0.1"), |
| 258 | ("power", "1.0.1"), |
| 259 | ("resource_event", "1.3.0"), |
| 260 | ("sensor_event", "1.0.1"), |
| 261 | ("storage_device", "1.2.1"), |
| 262 | ("task_event", "1.0.3"), |
| 263 | ("telemetry", "1.0.0"), |
| 264 | ("update", "1.0.2"), |
| 265 | ) |
| 266 | |
Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 267 | parser = argparse.ArgumentParser() |
| 268 | parser.add_argument( |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 269 | "--registries", |
| 270 | type=str, |
Gunnar Mills | 665e760 | 2024-04-10 13:14:41 -0500 | [diff] [blame] | 271 | default="privilege,openbmc," |
| 272 | + ",".join([dmtf[0] for dmtf in dmtf_registries]), |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 273 | help="Comma delimited list of registries to update", |
| 274 | ) |
Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 275 | |
| 276 | args = parser.parse_args() |
| 277 | |
| 278 | registries = set(args.registries.split(",")) |
| 279 | files = [] |
| 280 | |
Gunnar Mills | 665e760 | 2024-04-10 13:14:41 -0500 | [diff] [blame] | 281 | for registry, version in dmtf_registries: |
| 282 | if registry in registries: |
| 283 | registry_pascal_case = to_pascal_case(registry) |
| 284 | files.append( |
| 285 | make_getter( |
| 286 | f"{registry_pascal_case}.{version}.json", |
| 287 | f"{registry}_message_registry.hpp", |
| 288 | registry, |
| 289 | ) |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 290 | ) |
Ed Tanous | 3e5faba | 2023-08-16 15:11:29 -0700 | [diff] [blame] | 291 | if "openbmc" in registries: |
| 292 | files.append(openbmc_local_getter()) |
Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 293 | |
| 294 | update_registries(files) |
| 295 | |
| 296 | if "privilege" in registries: |
| 297 | make_privilege_registry() |
| 298 | |
| 299 | |
| 300 | if __name__ == "__main__": |
| 301 | main() |