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