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