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 |
Ed Tanous | 42079ec | 2024-11-16 13:32:29 -0800 | [diff] [blame] | 5 | from collections import OrderedDict |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 6 | |
Nan Zhou | 6eaf0bd | 2022-08-07 01:18:45 +0000 | [diff] [blame] | 7 | import requests |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 8 | |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 9 | PRAGMA_ONCE = """#pragma once |
| 10 | """ |
Nan Zhou | 043bec0 | 2022-09-20 18:12:13 +0000 | [diff] [blame] | 11 | |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 12 | WARNING = """/**************************************************************** |
Ed Tanous | 1cf53df | 2022-02-17 09:09:25 -0800 | [diff] [blame] | 13 | * READ THIS WARNING FIRST |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 14 | * This is an auto-generated header which contains definitions |
| 15 | * for Redfish DMTF defined messages. |
Ed Tanous | 1cf53df | 2022-02-17 09:09:25 -0800 | [diff] [blame] | 16 | * DO NOT modify this registry outside of running the |
Jason M. Bills | 0e2d069 | 2022-04-01 13:59:05 -0700 | [diff] [blame] | 17 | * parse_registries.py script. The definitions contained within |
Ed Tanous | 1cf53df | 2022-02-17 09:09:25 -0800 | [diff] [blame] | 18 | * this file are owned by DMTF. Any modifications to these files |
| 19 | * should be first pushed to the relevant registry in the DMTF |
| 20 | * github organization. |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 21 | ***************************************************************/""" |
Ed Tanous | 1cf53df | 2022-02-17 09:09:25 -0800 | [diff] [blame] | 22 | |
Ed Tanous | 40e9b92 | 2024-09-10 13:50:16 -0700 | [diff] [blame] | 23 | COPYRIGHT = """// SPDX-License-Identifier: Apache-2.0 |
| 24 | // SPDX-FileCopyrightText: Copyright OpenBMC Authors |
| 25 | """ |
| 26 | |
| 27 | INCLUDES = """ |
Nan Zhou | 01c78a0 | 2022-09-20 18:17:20 +0000 | [diff] [blame] | 28 | #include "registries.hpp" |
| 29 | |
| 30 | #include <array> |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 31 | |
Ed Tanous | 4d99bbb | 2022-02-17 10:02:57 -0800 | [diff] [blame] | 32 | // clang-format off |
| 33 | |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 34 | namespace redfish::registries::{} |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 35 | {{ |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 36 | """ |
Ed Tanous | 40e9b92 | 2024-09-10 13:50:16 -0700 | [diff] [blame] | 37 | |
| 38 | REGISTRY_HEADER = f"{COPYRIGHT}{PRAGMA_ONCE}{WARNING}{INCLUDES}" |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 39 | |
| 40 | SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__)) |
| 41 | |
Jason M. Bills | ac70637 | 2020-02-18 11:36:47 -0800 | [diff] [blame] | 42 | include_path = os.path.realpath( |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 43 | os.path.join(SCRIPT_DIR, "..", "redfish-core", "include", "registries") |
| 44 | ) |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 45 | |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 46 | proxies = {"https": os.environ.get("https_proxy", None)} |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 47 | |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 48 | |
James Feist | e51c710 | 2020-03-17 10:38:18 -0700 | [diff] [blame] | 49 | def make_getter(dmtf_name, header_name, type_name): |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 50 | url = "https://redfish.dmtf.org/registries/{}".format(dmtf_name) |
James Feist | e51c710 | 2020-03-17 10:38:18 -0700 | [diff] [blame] | 51 | dmtf = requests.get(url, proxies=proxies) |
| 52 | dmtf.raise_for_status() |
Ed Tanous | 42079ec | 2024-11-16 13:32:29 -0800 | [diff] [blame] | 53 | json_file = json.loads(dmtf.text, object_pairs_hook=OrderedDict) |
James Feist | e51c710 | 2020-03-17 10:38:18 -0700 | [diff] [blame] | 54 | path = os.path.join(include_path, header_name) |
| 55 | return (path, json_file, type_name, url) |
| 56 | |
| 57 | |
Ed Tanous | 3e5faba | 2023-08-16 15:11:29 -0700 | [diff] [blame] | 58 | def openbmc_local_getter(): |
Milton D. Miller II | 770362f | 2024-12-17 05:28:27 +0000 | [diff] [blame] | 59 | url = "https://raw.githubusercontent.com/openbmc/bmcweb/refs/heads/master/redfish-core/include/registries/openbmc.json" |
Ed Tanous | 3e5faba | 2023-08-16 15:11:29 -0700 | [diff] [blame] | 60 | with open( |
| 61 | os.path.join( |
| 62 | SCRIPT_DIR, |
| 63 | "..", |
| 64 | "redfish-core", |
| 65 | "include", |
| 66 | "registries", |
| 67 | "openbmc.json", |
| 68 | ), |
| 69 | "rb", |
| 70 | ) as json_file: |
| 71 | json_file = json.load(json_file) |
| 72 | |
| 73 | path = os.path.join(include_path, "openbmc_message_registry.hpp") |
| 74 | return (path, json_file, "openbmc", url) |
| 75 | |
| 76 | |
Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 77 | def update_registries(files): |
| 78 | # Remove the old files |
| 79 | for file, json_dict, namespace, url in files: |
| 80 | try: |
| 81 | os.remove(file) |
| 82 | except BaseException: |
| 83 | print("{} not found".format(file)) |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 84 | |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 85 | with open(file, "w") as registry: |
Ed Tanous | 56b8199 | 2024-12-02 10:36:37 -0800 | [diff] [blame] | 86 | |
| 87 | version_split = json_dict["RegistryVersion"].split(".") |
| 88 | |
Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 89 | registry.write(REGISTRY_HEADER.format(namespace)) |
| 90 | # Parse the Registry header info |
Ed Tanous | 5b9ef70 | 2022-02-17 12:19:32 -0800 | [diff] [blame] | 91 | registry.write( |
Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 92 | "const Header header = {{\n" |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 93 | ' "{json_dict[@Redfish.Copyright]}",\n' |
| 94 | ' "{json_dict[@odata.type]}",\n' |
Ed Tanous | 56b8199 | 2024-12-02 10:36:37 -0800 | [diff] [blame] | 95 | " {version_split[0]},\n" |
| 96 | " {version_split[1]},\n" |
| 97 | " {version_split[2]},\n" |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 98 | ' "{json_dict[Name]}",\n' |
| 99 | ' "{json_dict[Language]}",\n' |
| 100 | ' "{json_dict[Description]}",\n' |
| 101 | ' "{json_dict[RegistryPrefix]}",\n' |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 102 | ' "{json_dict[OwningEntity]}",\n' |
Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 103 | "}};\n" |
| 104 | "constexpr const char* url =\n" |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 105 | ' "{url}";\n' |
Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 106 | "\n" |
| 107 | "constexpr std::array registry =\n" |
| 108 | "{{\n".format( |
| 109 | json_dict=json_dict, |
| 110 | url=url, |
Ed Tanous | 56b8199 | 2024-12-02 10:36:37 -0800 | [diff] [blame] | 111 | version_split=version_split, |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 112 | ) |
| 113 | ) |
Ed Tanous | 30a3c43 | 2022-02-07 09:37:21 -0800 | [diff] [blame] | 114 | |
Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 115 | messages_sorted = sorted(json_dict["Messages"].items()) |
| 116 | for messageId, message in messages_sorted: |
| 117 | registry.write( |
| 118 | " MessageEntry{{\n" |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 119 | ' "{messageId}",\n' |
Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 120 | " {{\n" |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 121 | ' "{message[Description]}",\n' |
| 122 | ' "{message[Message]}",\n' |
| 123 | ' "{message[MessageSeverity]}",\n' |
Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 124 | " {message[NumberOfArgs]},\n" |
| 125 | " {{".format( |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 126 | messageId=messageId, message=message |
| 127 | ) |
| 128 | ) |
Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 129 | paramTypes = message.get("ParamTypes") |
| 130 | if paramTypes: |
| 131 | for paramType in paramTypes: |
| 132 | registry.write( |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 133 | '\n "{}",'.format(paramType) |
Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 134 | ) |
| 135 | registry.write("\n },\n") |
| 136 | else: |
| 137 | registry.write("},\n") |
| 138 | registry.write( |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 139 | ' "{message[Resolution]}",\n' |
| 140 | " }}}},\n".format(message=message) |
| 141 | ) |
Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 142 | |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 143 | registry.write("\n};\n\nenum class Index\n{\n") |
Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 144 | for index, (messageId, message) in enumerate(messages_sorted): |
| 145 | messageId = messageId[0].lower() + messageId[1:] |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 146 | registry.write(" {} = {},\n".format(messageId, index)) |
Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 147 | registry.write( |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 148 | "}};\n}} // namespace redfish::registries::{}\n".format( |
| 149 | namespace |
| 150 | ) |
| 151 | ) |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 152 | |
| 153 | |
| 154 | def get_privilege_string_from_list(privilege_list): |
| 155 | privilege_string = "{{\n" |
| 156 | for privilege_json in privilege_list: |
| 157 | privileges = privilege_json["Privilege"] |
| 158 | privilege_string += " {" |
| 159 | for privilege in privileges: |
| 160 | if privilege == "NoAuth": |
| 161 | continue |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 162 | privilege_string += '"' |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 163 | privilege_string += privilege |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 164 | privilege_string += '",\n' |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 165 | if privilege != "NoAuth": |
| 166 | privilege_string = privilege_string[:-2] |
| 167 | privilege_string += "}" |
| 168 | privilege_string += ",\n" |
| 169 | privilege_string = privilege_string[:-2] |
| 170 | privilege_string += "\n}}" |
| 171 | return privilege_string |
| 172 | |
Ed Tanous | f395daa | 2021-08-02 08:56:24 -0700 | [diff] [blame] | 173 | |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 174 | def get_variable_name_for_privilege_set(privilege_list): |
| 175 | names = [] |
| 176 | for privilege_json in privilege_list: |
| 177 | privileges = privilege_json["Privilege"] |
| 178 | names.append("And".join(privileges)) |
| 179 | return "Or".join(names) |
| 180 | |
Ed Tanous | f395daa | 2021-08-02 08:56:24 -0700 | [diff] [blame] | 181 | |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 182 | PRIVILEGE_HEADER = ( |
Ed Tanous | 40e9b92 | 2024-09-10 13:50:16 -0700 | [diff] [blame] | 183 | COPYRIGHT |
| 184 | + PRAGMA_ONCE |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 185 | + WARNING |
| 186 | + """ |
Nan Zhou | 01c78a0 | 2022-09-20 18:17:20 +0000 | [diff] [blame] | 187 | #include "privileges.hpp" |
| 188 | |
| 189 | #include <array> |
Nan Zhou | 043bec0 | 2022-09-20 18:12:13 +0000 | [diff] [blame] | 190 | |
| 191 | // clang-format off |
| 192 | |
| 193 | namespace redfish::privileges |
| 194 | { |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 195 | """ |
| 196 | ) |
Nan Zhou | 043bec0 | 2022-09-20 18:12:13 +0000 | [diff] [blame] | 197 | |
| 198 | |
Ed Tanous | 7ccfe68 | 2024-11-16 14:36:16 -0800 | [diff] [blame] | 199 | def get_response_code(entry_id, entry): |
| 200 | codes = { |
| 201 | "InternalError": "internal_server_error", |
| 202 | "OperationTimeout": "internal_server_error", |
| 203 | "PropertyValueResourceConflict": "conflict", |
| 204 | "ResourceInUse": "service_unavailable", |
| 205 | "ServiceTemporarilyUnavailable": "service_unavailable", |
| 206 | "ResourceCannotBeDeleted": "method_not_allowed", |
| 207 | "PropertyValueModified": "ok", |
| 208 | "InsufficientPrivilege": "forbidden", |
| 209 | "AccountForSessionNoLongerExists": "forbidden", |
| 210 | "ServiceDisabled": "service_unavailable", |
| 211 | "ServiceInUnknownState": "service_unavailable", |
| 212 | "EventSubscriptionLimitExceeded": "service_unavailable", |
| 213 | "ResourceAtUriUnauthorized": "unauthorized", |
| 214 | "SessionTerminated": "ok", |
| 215 | "SubscriptionTerminated": "ok", |
| 216 | "PropertyNotWritable": "forbidden", |
| 217 | "MaximumErrorsExceeded": "internal_server_error", |
| 218 | "GeneralError": "internal_server_error", |
| 219 | "PreconditionFailed": "precondition_failed", |
| 220 | "OperationFailed": "bad_gateway", |
| 221 | "ServiceShuttingDown": "service_unavailable", |
| 222 | "AccountRemoved": "ok", |
| 223 | "PropertyValueExternalConflict": "conflict", |
| 224 | "InsufficientStorage": "insufficient_storage", |
| 225 | "OperationNotAllowed": "method_not_allowed", |
| 226 | "ResourceNotFound": "not_found", |
| 227 | "CouldNotEstablishConnection": "not_found", |
| 228 | "AccessDenied": "forbidden", |
| 229 | "Success": None, |
| 230 | "Created": "created", |
| 231 | "NoValidSession": "forbidden", |
| 232 | "SessionLimitExceeded": "service_unavailable", |
| 233 | "ResourceExhaustion": "service_unavailable", |
| 234 | "AccountModified": "ok", |
| 235 | "PasswordChangeRequired": None, |
| 236 | "ResourceInStandby": "service_unavailable", |
| 237 | "GenerateSecretKeyRequired": "forbidden", |
| 238 | } |
| 239 | |
| 240 | code = codes.get(entry_id, "NOCODE") |
| 241 | if code != "NOCODE": |
| 242 | return code |
| 243 | |
| 244 | return "bad_request" |
| 245 | |
| 246 | |
Ed Tanous | f175c28 | 2024-12-02 15:12:50 -0800 | [diff] [blame] | 247 | def make_error_function( |
| 248 | entry_id, entry, is_header, registry_name, namespace_name |
| 249 | ): |
Ed Tanous | 644cdcb | 2024-11-17 11:12:41 -0800 | [diff] [blame] | 250 | arg_nonstring_types = { |
| 251 | "const boost::urls::url_view_base&": { |
| 252 | "AccessDenied": [1], |
| 253 | "CouldNotEstablishConnection": [1], |
| 254 | "GenerateSecretKeyRequired": [1], |
| 255 | "InvalidObject": [1], |
| 256 | "PasswordChangeRequired": [1], |
| 257 | "PropertyValueResourceConflict": [3], |
| 258 | "ResetRequired": [1], |
| 259 | "ResourceAtUriInUnknownFormat": [1], |
| 260 | "ResourceAtUriUnauthorized": [1], |
| 261 | "ResourceCreationConflict": [1], |
| 262 | "ResourceMissingAtURI": [1], |
| 263 | "SourceDoesNotSupportProtocol": [1], |
| 264 | }, |
| 265 | "const nlohmann::json&": { |
| 266 | "ActionParameterValueError": [1], |
| 267 | "ActionParameterValueFormatError": [1], |
| 268 | "ActionParameterValueTypeError": [1], |
| 269 | "PropertyValueExternalConflict": [2], |
| 270 | "PropertyValueFormatError": [1], |
| 271 | "PropertyValueIncorrect": [2], |
| 272 | "PropertyValueModified": [2], |
| 273 | "PropertyValueNotInList": [1], |
| 274 | "PropertyValueOutOfRange": [1], |
| 275 | "PropertyValueResourceConflict": [2], |
| 276 | "PropertyValueTypeError": [1], |
| 277 | "QueryParameterValueFormatError": [1], |
| 278 | "QueryParameterValueTypeError": [1], |
| 279 | }, |
| 280 | "uint64_t": { |
| 281 | "ArraySizeTooLong": [2], |
| 282 | "InvalidIndex": [1], |
| 283 | "StringValueTooLong": [2], |
Ed Tanous | f175c28 | 2024-12-02 15:12:50 -0800 | [diff] [blame] | 284 | "TaskProgressChanged": [2], |
Ed Tanous | 644cdcb | 2024-11-17 11:12:41 -0800 | [diff] [blame] | 285 | }, |
Ed Tanous | 42079ec | 2024-11-16 13:32:29 -0800 | [diff] [blame] | 286 | } |
| 287 | |
Ed Tanous | 7ccfe68 | 2024-11-16 14:36:16 -0800 | [diff] [blame] | 288 | out = "" |
| 289 | args = [] |
| 290 | argtypes = [] |
| 291 | for arg_index, arg in enumerate(entry.get("ParamTypes", [])): |
| 292 | arg_index += 1 |
Ed Tanous | 644cdcb | 2024-11-17 11:12:41 -0800 | [diff] [blame] | 293 | typename = "std::string_view" |
| 294 | for typestring, entries in arg_nonstring_types.items(): |
| 295 | if arg_index in entries.get(entry_id, []): |
| 296 | typename = typestring |
| 297 | |
Ed Tanous | 7ccfe68 | 2024-11-16 14:36:16 -0800 | [diff] [blame] | 298 | argtypes.append(typename) |
| 299 | args.append(f"{typename} arg{arg_index}") |
| 300 | function_name = entry_id[0].lower() + entry_id[1:] |
| 301 | arg = ", ".join(args) |
| 302 | out += f"nlohmann::json {function_name}({arg})" |
| 303 | |
| 304 | if is_header: |
| 305 | out += ";\n\n" |
| 306 | else: |
| 307 | out += "\n{\n" |
| 308 | to_array_type = "" |
| 309 | if argtypes: |
| 310 | outargs = [] |
| 311 | for index, typename in enumerate(argtypes): |
| 312 | index += 1 |
| 313 | if typename == "const nlohmann::json&": |
| 314 | out += f"std::string arg{index}Str = arg{index}.dump(-1, ' ', true, nlohmann::json::error_handler_t::replace);\n" |
Ed Tanous | 644cdcb | 2024-11-17 11:12:41 -0800 | [diff] [blame] | 315 | elif typename == "uint64_t": |
Ed Tanous | 7ccfe68 | 2024-11-16 14:36:16 -0800 | [diff] [blame] | 316 | out += f"std::string arg{index}Str = std::to_string(arg{index});\n" |
| 317 | |
| 318 | for index, typename in enumerate(argtypes): |
| 319 | index += 1 |
| 320 | if typename == "const boost::urls::url_view_base&": |
| 321 | outargs.append(f"arg{index}.buffer()") |
| 322 | to_array_type = "<std::string_view>" |
| 323 | elif typename == "const nlohmann::json&": |
| 324 | outargs.append(f"arg{index}Str") |
| 325 | to_array_type = "<std::string_view>" |
Ed Tanous | 644cdcb | 2024-11-17 11:12:41 -0800 | [diff] [blame] | 326 | elif typename == "uint64_t": |
Ed Tanous | 7ccfe68 | 2024-11-16 14:36:16 -0800 | [diff] [blame] | 327 | outargs.append(f"arg{index}Str") |
| 328 | to_array_type = "<std::string_view>" |
| 329 | else: |
| 330 | outargs.append(f"arg{index}") |
| 331 | argstring = ", ".join(outargs) |
Ed Tanous | 7ccfe68 | 2024-11-16 14:36:16 -0800 | [diff] [blame] | 332 | |
| 333 | if argtypes: |
| 334 | arg_param = f"std::to_array{to_array_type}({{{argstring}}})" |
| 335 | else: |
| 336 | arg_param = "{}" |
Ed Tanous | f175c28 | 2024-12-02 15:12:50 -0800 | [diff] [blame] | 337 | out += f" return getLog(redfish::registries::{namespace_name}::Index::{function_name}, {arg_param});" |
Ed Tanous | 7ccfe68 | 2024-11-16 14:36:16 -0800 | [diff] [blame] | 338 | out += "\n}\n\n" |
Ed Tanous | f175c28 | 2024-12-02 15:12:50 -0800 | [diff] [blame] | 339 | if registry_name == "Base": |
| 340 | args.insert(0, "crow::Response& res") |
Ed Tanous | 7ccfe68 | 2024-11-16 14:36:16 -0800 | [diff] [blame] | 341 | if entry_id == "InternalError": |
Ed Tanous | f175c28 | 2024-12-02 15:12:50 -0800 | [diff] [blame] | 342 | if is_header: |
| 343 | args.append( |
| 344 | "std::source_location location = std::source_location::current()" |
| 345 | ) |
| 346 | else: |
| 347 | args.append("const std::source_location location") |
| 348 | arg = ", ".join(args) |
| 349 | out += f"void {function_name}({arg})" |
| 350 | if is_header: |
| 351 | out += ";\n" |
Ed Tanous | 7ccfe68 | 2024-11-16 14:36:16 -0800 | [diff] [blame] | 352 | else: |
Ed Tanous | f175c28 | 2024-12-02 15:12:50 -0800 | [diff] [blame] | 353 | out += "\n{\n" |
| 354 | if entry_id == "InternalError": |
| 355 | out += """BMCWEB_LOG_CRITICAL("Internal Error {}({}:{}) `{}`: ", location.file_name(), |
| 356 | location.line(), location.column(), |
| 357 | location.function_name());\n""" |
| 358 | |
| 359 | if entry_id == "ServiceTemporarilyUnavailable": |
| 360 | out += "res.addHeader(boost::beast::http::field::retry_after, arg1);" |
| 361 | |
| 362 | res = get_response_code(entry_id, entry) |
| 363 | if res: |
| 364 | out += f" res.result(boost::beast::http::status::{res});\n" |
| 365 | args_out = ", ".join([f"arg{x+1}" for x in range(len(argtypes))]) |
| 366 | |
| 367 | addMessageToJson = { |
| 368 | "PropertyDuplicate": 1, |
| 369 | "ResourceAlreadyExists": 2, |
| 370 | "CreateFailedMissingReqProperties": 1, |
| 371 | "PropertyValueFormatError": 2, |
| 372 | "PropertyValueNotInList": 2, |
| 373 | "PropertyValueTypeError": 2, |
| 374 | "PropertyValueError": 1, |
| 375 | "PropertyNotWritable": 1, |
| 376 | "PropertyValueModified": 1, |
| 377 | "PropertyMissing": 1, |
| 378 | } |
| 379 | |
| 380 | addMessageToRoot = [ |
| 381 | "SessionTerminated", |
| 382 | "SubscriptionTerminated", |
| 383 | "AccountRemoved", |
| 384 | "Created", |
| 385 | "Success", |
| 386 | "PasswordChangeRequired", |
| 387 | ] |
| 388 | |
| 389 | if entry_id in addMessageToJson: |
| 390 | out += f" addMessageToJson(res.jsonValue, {function_name}({args_out}), arg{addMessageToJson[entry_id]});\n" |
| 391 | elif entry_id in addMessageToRoot: |
| 392 | out += f" addMessageToJsonRoot(res.jsonValue, {function_name}({args_out}));\n" |
| 393 | else: |
| 394 | out += f" addMessageToErrorJson(res.jsonValue, {function_name}({args_out}));\n" |
| 395 | out += "}\n" |
Ed Tanous | 7ccfe68 | 2024-11-16 14:36:16 -0800 | [diff] [blame] | 396 | out += "\n" |
| 397 | return out |
| 398 | |
| 399 | |
Ed Tanous | f175c28 | 2024-12-02 15:12:50 -0800 | [diff] [blame] | 400 | def create_error_registry( |
| 401 | entry, registry_version, registry_name, namespace_name, filename |
| 402 | ): |
Ed Tanous | 42079ec | 2024-11-16 13:32:29 -0800 | [diff] [blame] | 403 | file, json_dict, namespace, url = entry |
Ed Tanous | f175c28 | 2024-12-02 15:12:50 -0800 | [diff] [blame] | 404 | base_filename = filename + "_messages" |
Ed Tanous | 42079ec | 2024-11-16 13:32:29 -0800 | [diff] [blame] | 405 | |
Ed Tanous | 42079ec | 2024-11-16 13:32:29 -0800 | [diff] [blame] | 406 | error_messages_hpp = os.path.join( |
Ed Tanous | f175c28 | 2024-12-02 15:12:50 -0800 | [diff] [blame] | 407 | SCRIPT_DIR, "..", "redfish-core", "include", f"{base_filename}.hpp" |
Ed Tanous | 42079ec | 2024-11-16 13:32:29 -0800 | [diff] [blame] | 408 | ) |
Ed Tanous | f8cca87 | 2024-11-16 22:47:34 -0800 | [diff] [blame] | 409 | messages = json_dict["Messages"] |
| 410 | |
Ed Tanous | 42079ec | 2024-11-16 13:32:29 -0800 | [diff] [blame] | 411 | with open( |
| 412 | error_messages_hpp, |
| 413 | "w", |
| 414 | ) as out: |
| 415 | out.write(PRAGMA_ONCE) |
| 416 | out.write(WARNING) |
| 417 | out.write( |
| 418 | """ |
| 419 | |
| 420 | #include "http_response.hpp" |
| 421 | |
| 422 | #include <boost/url/url_view_base.hpp> |
| 423 | #include <nlohmann/json.hpp> |
| 424 | |
Ed Tanous | 42079ec | 2024-11-16 13:32:29 -0800 | [diff] [blame] | 425 | #include <source_location> |
Ed Tanous | 42079ec | 2024-11-16 13:32:29 -0800 | [diff] [blame] | 426 | #include <string_view> |
| 427 | |
| 428 | // IWYU pragma: no_forward_declare crow::Response |
| 429 | |
| 430 | namespace redfish |
| 431 | { |
| 432 | |
| 433 | namespace messages |
| 434 | { |
Ed Tanous | 42079ec | 2024-11-16 13:32:29 -0800 | [diff] [blame] | 435 | """ |
| 436 | ) |
Ed Tanous | f175c28 | 2024-12-02 15:12:50 -0800 | [diff] [blame] | 437 | if registry_name == "Base": |
| 438 | out.write( |
| 439 | f'constexpr const char* messageVersionPrefix = "{registry_name}.{registry_version}.";' |
| 440 | ) |
| 441 | out.write( |
| 442 | """ |
| 443 | constexpr const char* messageAnnotation = "@Message.ExtendedInfo"; |
Ed Tanous | 42079ec | 2024-11-16 13:32:29 -0800 | [diff] [blame] | 444 | |
Ed Tanous | f175c28 | 2024-12-02 15:12:50 -0800 | [diff] [blame] | 445 | /** |
| 446 | * @brief Moves all error messages from the |source| JSON to |target| |
| 447 | */ |
| 448 | void moveErrorsToErrorJson(nlohmann::json& target, nlohmann::json& source); |
Ed Tanous | 352945b | 2024-11-17 22:45:51 -0800 | [diff] [blame] | 449 | |
Ed Tanous | f175c28 | 2024-12-02 15:12:50 -0800 | [diff] [blame] | 450 | """ |
| 451 | ) |
Ed Tanous | 42079ec | 2024-11-16 13:32:29 -0800 | [diff] [blame] | 452 | for entry_id, entry in messages.items(): |
| 453 | message = entry["Message"] |
| 454 | for index in range(1, 10): |
| 455 | message = message.replace(f"'%{index}'", f"<arg{index}>") |
| 456 | message = message.replace(f"%{index}", f"<arg{index}>") |
| 457 | |
Ed Tanous | f175c28 | 2024-12-02 15:12:50 -0800 | [diff] [blame] | 458 | if registry_name == "Base": |
| 459 | out.write("/**\n") |
| 460 | out.write(f"* @brief Formats {entry_id} message into JSON\n") |
| 461 | out.write(f'* Message body: "{message}"\n') |
| 462 | out.write("*\n") |
| 463 | arg_index = 0 |
| 464 | for arg_index, arg in enumerate(entry.get("ParamTypes", [])): |
| 465 | arg_index += 1 |
Ed Tanous | 42079ec | 2024-11-16 13:32:29 -0800 | [diff] [blame] | 466 | |
Ed Tanous | f175c28 | 2024-12-02 15:12:50 -0800 | [diff] [blame] | 467 | out.write( |
| 468 | f"* @param[in] arg{arg_index} Parameter of message that will replace %{arg_index} in its body.\n" |
| 469 | ) |
| 470 | out.write("*\n") |
Ed Tanous | 42079ec | 2024-11-16 13:32:29 -0800 | [diff] [blame] | 471 | out.write( |
Ed Tanous | f175c28 | 2024-12-02 15:12:50 -0800 | [diff] [blame] | 472 | f"* @returns Message {entry_id} formatted to JSON */\n" |
Ed Tanous | 42079ec | 2024-11-16 13:32:29 -0800 | [diff] [blame] | 473 | ) |
Ed Tanous | 42079ec | 2024-11-16 13:32:29 -0800 | [diff] [blame] | 474 | |
Ed Tanous | f175c28 | 2024-12-02 15:12:50 -0800 | [diff] [blame] | 475 | out.write( |
| 476 | make_error_function( |
| 477 | entry_id, entry, True, registry_name, namespace_name |
| 478 | ) |
| 479 | ) |
Ed Tanous | 42079ec | 2024-11-16 13:32:29 -0800 | [diff] [blame] | 480 | out.write(" }\n") |
| 481 | out.write("}\n") |
Ed Tanous | 7ccfe68 | 2024-11-16 14:36:16 -0800 | [diff] [blame] | 482 | |
| 483 | error_messages_cpp = os.path.join( |
Ed Tanous | f175c28 | 2024-12-02 15:12:50 -0800 | [diff] [blame] | 484 | SCRIPT_DIR, "..", "redfish-core", "src", f"{base_filename}.cpp" |
Ed Tanous | 7ccfe68 | 2024-11-16 14:36:16 -0800 | [diff] [blame] | 485 | ) |
| 486 | with open( |
| 487 | error_messages_cpp, |
| 488 | "w", |
| 489 | ) as out: |
| 490 | out.write(WARNING) |
Ed Tanous | f175c28 | 2024-12-02 15:12:50 -0800 | [diff] [blame] | 491 | out.write(f'\n#include "{base_filename}.hpp"\n') |
Ed Tanous | 847deee | 2024-12-02 15:28:10 -0800 | [diff] [blame] | 492 | headers = [] |
Ed Tanous | 7ccfe68 | 2024-11-16 14:36:16 -0800 | [diff] [blame] | 493 | |
Ed Tanous | 847deee | 2024-12-02 15:28:10 -0800 | [diff] [blame] | 494 | headers.append('"registries.hpp"') |
Ed Tanous | f175c28 | 2024-12-02 15:12:50 -0800 | [diff] [blame] | 495 | if registry_name == "Base": |
| 496 | reg_name_lower = "base" |
Ed Tanous | 847deee | 2024-12-02 15:28:10 -0800 | [diff] [blame] | 497 | headers.append('"http_response.hpp"') |
| 498 | headers.append('"logging.hpp"') |
| 499 | headers.append("<boost/beast/http/field.hpp>") |
| 500 | headers.append("<boost/beast/http/status.hpp>") |
| 501 | headers.append("<boost/url/url_view_base.hpp>") |
| 502 | headers.append("<source_location>") |
Ed Tanous | f175c28 | 2024-12-02 15:12:50 -0800 | [diff] [blame] | 503 | else: |
| 504 | reg_name_lower = namespace_name.lower() |
Ed Tanous | 847deee | 2024-12-02 15:28:10 -0800 | [diff] [blame] | 505 | headers.append(f'"registries/{reg_name_lower}_message_registry.hpp"') |
Ed Tanous | 7ccfe68 | 2024-11-16 14:36:16 -0800 | [diff] [blame] | 506 | |
Ed Tanous | 847deee | 2024-12-02 15:28:10 -0800 | [diff] [blame] | 507 | headers.append("<nlohmann/json.hpp>") |
| 508 | headers.append("<array>") |
| 509 | headers.append("<cstddef>") |
| 510 | headers.append("<span>") |
| 511 | |
Ed Tanous | a8a5bc1 | 2024-12-02 15:43:16 -0800 | [diff] [blame] | 512 | if registry_name not in ("ResourceEvent", "HeartbeatEvent"): |
Ed Tanous | 847deee | 2024-12-02 15:28:10 -0800 | [diff] [blame] | 513 | headers.append("<cstdint>") |
| 514 | headers.append("<string>") |
| 515 | headers.append("<string_view>") |
| 516 | |
| 517 | for header in headers: |
| 518 | out.write(f"#include {header}\n") |
| 519 | |
Ed Tanous | f175c28 | 2024-12-02 15:12:50 -0800 | [diff] [blame] | 520 | out.write( |
| 521 | """ |
Ed Tanous | 7ccfe68 | 2024-11-16 14:36:16 -0800 | [diff] [blame] | 522 | // Clang can't seem to decide whether this header needs to be included or not, |
| 523 | // and is inconsistent. Include it for now |
| 524 | // NOLINTNEXTLINE(misc-include-cleaner) |
| 525 | #include <utility> |
| 526 | |
| 527 | namespace redfish |
| 528 | { |
| 529 | |
| 530 | namespace messages |
| 531 | { |
Ed Tanous | f175c28 | 2024-12-02 15:12:50 -0800 | [diff] [blame] | 532 | """ |
| 533 | ) |
Ed Tanous | 7ccfe68 | 2024-11-16 14:36:16 -0800 | [diff] [blame] | 534 | |
Ed Tanous | f175c28 | 2024-12-02 15:12:50 -0800 | [diff] [blame] | 535 | if registry_name == "Base": |
| 536 | out.write( |
| 537 | """ |
Ed Tanous | 7ccfe68 | 2024-11-16 14:36:16 -0800 | [diff] [blame] | 538 | static void addMessageToErrorJson(nlohmann::json& target, |
| 539 | const nlohmann::json& message) |
| 540 | { |
| 541 | auto& error = target["error"]; |
| 542 | |
| 543 | // If this is the first error message, fill in the information from the |
| 544 | // first error message to the top level struct |
| 545 | if (!error.is_object()) |
| 546 | { |
| 547 | auto messageIdIterator = message.find("MessageId"); |
| 548 | if (messageIdIterator == message.end()) |
| 549 | { |
| 550 | BMCWEB_LOG_CRITICAL( |
| 551 | "Attempt to add error message without MessageId"); |
| 552 | return; |
| 553 | } |
| 554 | |
| 555 | auto messageFieldIterator = message.find("Message"); |
| 556 | if (messageFieldIterator == message.end()) |
| 557 | { |
| 558 | BMCWEB_LOG_CRITICAL("Attempt to add error message without Message"); |
| 559 | return; |
| 560 | } |
| 561 | error["code"] = *messageIdIterator; |
| 562 | error["message"] = *messageFieldIterator; |
| 563 | } |
| 564 | else |
| 565 | { |
| 566 | // More than 1 error occurred, so the message has to be generic |
| 567 | error["code"] = std::string(messageVersionPrefix) + "GeneralError"; |
| 568 | error["message"] = "A general error has occurred. See Resolution for " |
| 569 | "information on how to resolve the error."; |
| 570 | } |
| 571 | |
| 572 | // This check could technically be done in the default construction |
| 573 | // branch above, but because we need the pointer to the extended info field |
| 574 | // anyway, it's more efficient to do it here. |
| 575 | auto& extendedInfo = error[messages::messageAnnotation]; |
| 576 | if (!extendedInfo.is_array()) |
| 577 | { |
| 578 | extendedInfo = nlohmann::json::array(); |
| 579 | } |
| 580 | |
| 581 | extendedInfo.push_back(message); |
| 582 | } |
| 583 | |
| 584 | void moveErrorsToErrorJson(nlohmann::json& target, nlohmann::json& source) |
| 585 | { |
| 586 | if (!source.is_object()) |
| 587 | { |
| 588 | return; |
| 589 | } |
| 590 | auto errorIt = source.find("error"); |
| 591 | if (errorIt == source.end()) |
| 592 | { |
| 593 | // caller puts error message in root |
| 594 | messages::addMessageToErrorJson(target, source); |
| 595 | source.clear(); |
| 596 | return; |
| 597 | } |
| 598 | auto extendedInfoIt = errorIt->find(messages::messageAnnotation); |
| 599 | if (extendedInfoIt == errorIt->end()) |
| 600 | { |
| 601 | return; |
| 602 | } |
| 603 | const nlohmann::json::array_t* extendedInfo = |
| 604 | (*extendedInfoIt).get_ptr<const nlohmann::json::array_t*>(); |
| 605 | if (extendedInfo == nullptr) |
| 606 | { |
| 607 | source.erase(errorIt); |
| 608 | return; |
| 609 | } |
| 610 | for (const nlohmann::json& message : *extendedInfo) |
| 611 | { |
| 612 | addMessageToErrorJson(target, message); |
| 613 | } |
| 614 | source.erase(errorIt); |
| 615 | } |
| 616 | |
| 617 | static void addMessageToJsonRoot(nlohmann::json& target, |
| 618 | const nlohmann::json& message) |
| 619 | { |
| 620 | if (!target[messages::messageAnnotation].is_array()) |
| 621 | { |
| 622 | // Force object to be an array |
| 623 | target[messages::messageAnnotation] = nlohmann::json::array(); |
| 624 | } |
| 625 | |
| 626 | target[messages::messageAnnotation].push_back(message); |
| 627 | } |
| 628 | |
| 629 | static void addMessageToJson(nlohmann::json& target, |
| 630 | const nlohmann::json& message, |
| 631 | std::string_view fieldPath) |
| 632 | { |
| 633 | std::string extendedInfo(fieldPath); |
| 634 | extendedInfo += messages::messageAnnotation; |
| 635 | |
| 636 | nlohmann::json& field = target[extendedInfo]; |
| 637 | if (!field.is_array()) |
| 638 | { |
| 639 | // Force object to be an array |
| 640 | field = nlohmann::json::array(); |
| 641 | } |
| 642 | |
| 643 | // Object exists and it is an array so we can just push in the message |
| 644 | field.push_back(message); |
| 645 | } |
Ed Tanous | 7ccfe68 | 2024-11-16 14:36:16 -0800 | [diff] [blame] | 646 | """ |
Ed Tanous | f175c28 | 2024-12-02 15:12:50 -0800 | [diff] [blame] | 647 | ) |
| 648 | out.write( |
| 649 | """ |
| 650 | static nlohmann::json getLog(redfish::registries::{namespace_name}::Index name, |
| 651 | std::span<const std::string_view> args) |
| 652 | {{ |
| 653 | size_t index = static_cast<size_t>(name); |
| 654 | if (index >= redfish::registries::{namespace_name}::registry.size()) |
| 655 | {{ |
| 656 | return {{}}; |
| 657 | }} |
| 658 | return getLogFromRegistry(redfish::registries::{namespace_name}::header, |
| 659 | redfish::registries::{namespace_name}::registry, index, args); |
| 660 | }} |
| 661 | |
| 662 | """.format( |
| 663 | namespace_name=namespace_name |
| 664 | ) |
Ed Tanous | 7ccfe68 | 2024-11-16 14:36:16 -0800 | [diff] [blame] | 665 | ) |
| 666 | for entry_id, entry in messages.items(): |
| 667 | out.write( |
| 668 | f"""/** |
| 669 | * @internal |
| 670 | * @brief Formats {entry_id} message into JSON |
| 671 | * |
| 672 | * See header file for more information |
| 673 | * @endinternal |
| 674 | */ |
| 675 | """ |
| 676 | ) |
| 677 | message = entry["Message"] |
Ed Tanous | f175c28 | 2024-12-02 15:12:50 -0800 | [diff] [blame] | 678 | out.write( |
| 679 | make_error_function( |
| 680 | entry_id, entry, False, registry_name, namespace_name |
| 681 | ) |
| 682 | ) |
Ed Tanous | 7ccfe68 | 2024-11-16 14:36:16 -0800 | [diff] [blame] | 683 | |
| 684 | out.write(" }\n") |
| 685 | out.write("}\n") |
| 686 | os.system(f"clang-format -i {error_messages_hpp} {error_messages_cpp}") |
Ed Tanous | 42079ec | 2024-11-16 13:32:29 -0800 | [diff] [blame] | 687 | |
| 688 | |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 689 | def make_privilege_registry(): |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 690 | path, json_file, type_name, url = make_getter( |
Gunnar Mills | 5910d94 | 2024-04-16 12:07:17 -0500 | [diff] [blame] | 691 | "Redfish_1.5.0_PrivilegeRegistry.json", |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 692 | "privilege_registry.hpp", |
| 693 | "privilege", |
| 694 | ) |
| 695 | with open(path, "w") as registry: |
Nan Zhou | 043bec0 | 2022-09-20 18:12:13 +0000 | [diff] [blame] | 696 | registry.write(PRIVILEGE_HEADER) |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 697 | |
| 698 | privilege_dict = {} |
| 699 | for mapping in json_file["Mappings"]: |
| 700 | # first pass, identify all the unique privilege sets |
| 701 | for operation, privilege_list in mapping["OperationMap"].items(): |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 702 | privilege_dict[ |
| 703 | get_privilege_string_from_list(privilege_list) |
| 704 | ] = (privilege_list,) |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 705 | for index, key in enumerate(privilege_dict): |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 706 | (privilege_list,) = privilege_dict[key] |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 707 | name = get_variable_name_for_privilege_set(privilege_list) |
Ed Tanous | f395daa | 2021-08-02 08:56:24 -0700 | [diff] [blame] | 708 | registry.write( |
Ed Tanous | 5b9ef70 | 2022-02-17 12:19:32 -0800 | [diff] [blame] | 709 | "const std::array<Privileges, {length}> " |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 710 | "privilegeSet{name} = {key};\n".format( |
| 711 | length=len(privilege_list), name=name, key=key |
| 712 | ) |
Ed Tanous | 5b9ef70 | 2022-02-17 12:19:32 -0800 | [diff] [blame] | 713 | ) |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 714 | privilege_dict[key] = (privilege_list, name) |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 715 | |
| 716 | for mapping in json_file["Mappings"]: |
| 717 | entity = mapping["Entity"] |
Ed Tanous | 4d99bbb | 2022-02-17 10:02:57 -0800 | [diff] [blame] | 718 | registry.write("// {}\n".format(entity)) |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 719 | for operation, privilege_list in mapping["OperationMap"].items(): |
| 720 | privilege_string = get_privilege_string_from_list( |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 721 | privilege_list |
| 722 | ) |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 723 | operation = operation.lower() |
| 724 | |
Ed Tanous | f395daa | 2021-08-02 08:56:24 -0700 | [diff] [blame] | 725 | registry.write( |
| 726 | "const static auto& {}{} = privilegeSet{};\n".format( |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 727 | operation, entity, privilege_dict[privilege_string][1] |
| 728 | ) |
| 729 | ) |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 730 | registry.write("\n") |
Ed Tanous | 5b9ef70 | 2022-02-17 12:19:32 -0800 | [diff] [blame] | 731 | registry.write( |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 732 | "} // namespace redfish::privileges\n// clang-format on\n" |
| 733 | ) |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 734 | |
| 735 | |
Gunnar Mills | 665e760 | 2024-04-10 13:14:41 -0500 | [diff] [blame] | 736 | def to_pascal_case(text): |
| 737 | s = text.replace("_", " ") |
| 738 | s = s.split() |
| 739 | if len(text) == 0: |
| 740 | return text |
| 741 | return "".join(i.capitalize() for i in s[0:]) |
| 742 | |
| 743 | |
Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 744 | def main(): |
Ed Tanous | c8895b0 | 2025-01-03 11:44:08 -0800 | [diff] [blame] | 745 | dmtf_registries = OrderedDict( |
| 746 | [ |
| 747 | ("base", "1.19.0"), |
| 748 | ("composition", "1.1.2"), |
| 749 | ("environmental", "1.0.1"), |
| 750 | ("ethernet_fabric", "1.0.1"), |
| 751 | ("fabric", "1.0.2"), |
| 752 | ("heartbeat_event", "1.0.1"), |
| 753 | ("job_event", "1.0.1"), |
| 754 | ("license", "1.0.3"), |
| 755 | ("log_service", "1.0.1"), |
| 756 | ("network_device", "1.0.3"), |
| 757 | ("platform", "1.0.1"), |
| 758 | ("power", "1.0.1"), |
| 759 | ("resource_event", "1.3.0"), |
| 760 | ("sensor_event", "1.0.1"), |
| 761 | ("storage_device", "1.2.1"), |
| 762 | ("task_event", "1.0.3"), |
| 763 | ("telemetry", "1.0.0"), |
| 764 | ("update", "1.0.2"), |
| 765 | ] |
| 766 | ) |
Gunnar Mills | 665e760 | 2024-04-10 13:14:41 -0500 | [diff] [blame] | 767 | |
Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 768 | parser = argparse.ArgumentParser() |
| 769 | parser.add_argument( |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 770 | "--registries", |
| 771 | type=str, |
Gunnar Mills | 665e760 | 2024-04-10 13:14:41 -0500 | [diff] [blame] | 772 | default="privilege,openbmc," |
Tam Nguyen | 7e6d032 | 2024-12-27 09:47:38 +0700 | [diff] [blame] | 773 | + ",".join([dmtf for dmtf in dmtf_registries]), |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 774 | help="Comma delimited list of registries to update", |
| 775 | ) |
Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 776 | |
| 777 | args = parser.parse_args() |
| 778 | |
| 779 | registries = set(args.registries.split(",")) |
| 780 | files = [] |
Tam Nguyen | 7e6d032 | 2024-12-27 09:47:38 +0700 | [diff] [blame] | 781 | registries_map = OrderedDict() |
Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 782 | |
Tam Nguyen | 7e6d032 | 2024-12-27 09:47:38 +0700 | [diff] [blame] | 783 | for registry, version in dmtf_registries.items(): |
Gunnar Mills | 665e760 | 2024-04-10 13:14:41 -0500 | [diff] [blame] | 784 | if registry in registries: |
| 785 | registry_pascal_case = to_pascal_case(registry) |
| 786 | files.append( |
| 787 | make_getter( |
| 788 | f"{registry_pascal_case}.{version}.json", |
| 789 | f"{registry}_message_registry.hpp", |
| 790 | registry, |
| 791 | ) |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 792 | ) |
Tam Nguyen | 7e6d032 | 2024-12-27 09:47:38 +0700 | [diff] [blame] | 793 | registries_map[registry] = files[-1] |
Ed Tanous | 3e5faba | 2023-08-16 15:11:29 -0700 | [diff] [blame] | 794 | if "openbmc" in registries: |
| 795 | files.append(openbmc_local_getter()) |
Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 796 | |
| 797 | update_registries(files) |
| 798 | |
Tam Nguyen | 7e6d032 | 2024-12-27 09:47:38 +0700 | [diff] [blame] | 799 | if "base" in registries_map: |
| 800 | create_error_registry( |
Ed Tanous | c8895b0 | 2025-01-03 11:44:08 -0800 | [diff] [blame] | 801 | registries_map["base"], |
| 802 | dmtf_registries["base"], |
| 803 | "Base", |
| 804 | "base", |
| 805 | "error", |
Tam Nguyen | 7e6d032 | 2024-12-27 09:47:38 +0700 | [diff] [blame] | 806 | ) |
| 807 | if "heartbeat_event" in registries_map: |
| 808 | create_error_registry( |
| 809 | registries_map["heartbeat_event"], |
| 810 | dmtf_registries["heartbeat_event"], |
| 811 | "HeartbeatEvent", |
| 812 | "heartbeat_event", |
| 813 | "heartbeat", |
| 814 | ) |
| 815 | if "resource_event" in registries_map: |
| 816 | create_error_registry( |
| 817 | registries_map["resource_event"], |
| 818 | dmtf_registries["resource_event"], |
| 819 | "ResourceEvent", |
| 820 | "resource_event", |
| 821 | "resource", |
| 822 | ) |
| 823 | if "task_event" in registries_map: |
| 824 | create_error_registry( |
Ed Tanous | c8895b0 | 2025-01-03 11:44:08 -0800 | [diff] [blame] | 825 | registries_map["task_event"], |
| 826 | dmtf_registries["task_event"], |
| 827 | "TaskEvent", |
| 828 | "task_event", |
| 829 | "task", |
Tam Nguyen | 7e6d032 | 2024-12-27 09:47:38 +0700 | [diff] [blame] | 830 | ) |
| 831 | |
Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 832 | if "privilege" in registries: |
| 833 | make_privilege_registry() |
| 834 | |
| 835 | |
| 836 | if __name__ == "__main__": |
| 837 | main() |