| Nan Zhou | 313c1b7 | 2022-03-25 11:47:55 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 | 
| Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 2 | import argparse | 
| Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 3 | import json | 
| Nan Zhou | 6eaf0bd | 2022-08-07 01:18:45 +0000 | [diff] [blame] | 4 | import os | 
| Ed Tanous | 42079ec | 2024-11-16 13:32:29 -0800 | [diff] [blame^] | 5 | from collections import OrderedDict | 
| Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 6 |  | 
| Nan Zhou | 6eaf0bd | 2022-08-07 01:18:45 +0000 | [diff] [blame] | 7 | import requests | 
| Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 8 |  | 
| Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 9 | PRAGMA_ONCE = """#pragma once | 
|  | 10 | """ | 
| Nan Zhou | 043bec0 | 2022-09-20 18:12:13 +0000 | [diff] [blame] | 11 |  | 
| Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 12 | WARNING = """/**************************************************************** | 
| Ed Tanous | 1cf53df | 2022-02-17 09:09:25 -0800 | [diff] [blame] | 13 | *                 READ THIS WARNING FIRST | 
| Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 14 | * This is an auto-generated header which contains definitions | 
|  | 15 | * for Redfish DMTF defined messages. | 
| Ed Tanous | 1cf53df | 2022-02-17 09:09:25 -0800 | [diff] [blame] | 16 | * DO NOT modify this registry outside of running the | 
| Jason M. Bills | 0e2d069 | 2022-04-01 13:59:05 -0700 | [diff] [blame] | 17 | * parse_registries.py script.  The definitions contained within | 
| Ed Tanous | 1cf53df | 2022-02-17 09:09:25 -0800 | [diff] [blame] | 18 | * this file are owned by DMTF.  Any modifications to these files | 
|  | 19 | * should be first pushed to the relevant registry in the DMTF | 
|  | 20 | * github organization. | 
| Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 21 | ***************************************************************/""" | 
| Ed Tanous | 1cf53df | 2022-02-17 09:09:25 -0800 | [diff] [blame] | 22 |  | 
| Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 23 | REGISTRY_HEADER = ( | 
|  | 24 | PRAGMA_ONCE | 
|  | 25 | + WARNING | 
|  | 26 | + """ | 
| Nan Zhou | 01c78a0 | 2022-09-20 18:17:20 +0000 | [diff] [blame] | 27 | #include "registries.hpp" | 
|  | 28 |  | 
|  | 29 | #include <array> | 
| Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 30 |  | 
| Ed Tanous | 4d99bbb | 2022-02-17 10:02:57 -0800 | [diff] [blame] | 31 | // clang-format off | 
|  | 32 |  | 
| Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 33 | namespace redfish::registries::{} | 
| Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 34 | {{ | 
| Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 35 | """ | 
|  | 36 | ) | 
| Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 37 |  | 
|  | 38 | SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__)) | 
|  | 39 |  | 
| Jason M. Bills | ac70637 | 2020-02-18 11:36:47 -0800 | [diff] [blame] | 40 | include_path = os.path.realpath( | 
| Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 41 | os.path.join(SCRIPT_DIR, "..", "redfish-core", "include", "registries") | 
|  | 42 | ) | 
| Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 43 |  | 
| Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 44 | proxies = {"https": os.environ.get("https_proxy", None)} | 
| Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 45 |  | 
| Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 46 |  | 
| James Feist | e51c710 | 2020-03-17 10:38:18 -0700 | [diff] [blame] | 47 | def make_getter(dmtf_name, header_name, type_name): | 
| Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 48 | url = "https://redfish.dmtf.org/registries/{}".format(dmtf_name) | 
| James Feist | e51c710 | 2020-03-17 10:38:18 -0700 | [diff] [blame] | 49 | dmtf = requests.get(url, proxies=proxies) | 
|  | 50 | dmtf.raise_for_status() | 
| Ed Tanous | 42079ec | 2024-11-16 13:32:29 -0800 | [diff] [blame^] | 51 | json_file = json.loads(dmtf.text, object_pairs_hook=OrderedDict) | 
| James Feist | e51c710 | 2020-03-17 10:38:18 -0700 | [diff] [blame] | 52 | path = os.path.join(include_path, header_name) | 
|  | 53 | return (path, json_file, type_name, url) | 
|  | 54 |  | 
|  | 55 |  | 
| Ed Tanous | 3e5faba | 2023-08-16 15:11:29 -0700 | [diff] [blame] | 56 | def openbmc_local_getter(): | 
|  | 57 | url = "" | 
|  | 58 | with open( | 
|  | 59 | os.path.join( | 
|  | 60 | SCRIPT_DIR, | 
|  | 61 | "..", | 
|  | 62 | "redfish-core", | 
|  | 63 | "include", | 
|  | 64 | "registries", | 
|  | 65 | "openbmc.json", | 
|  | 66 | ), | 
|  | 67 | "rb", | 
|  | 68 | ) as json_file: | 
|  | 69 | json_file = json.load(json_file) | 
|  | 70 |  | 
|  | 71 | path = os.path.join(include_path, "openbmc_message_registry.hpp") | 
|  | 72 | return (path, json_file, "openbmc", url) | 
|  | 73 |  | 
|  | 74 |  | 
| Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 75 | def update_registries(files): | 
|  | 76 | # Remove the old files | 
|  | 77 | for file, json_dict, namespace, url in files: | 
|  | 78 | try: | 
|  | 79 | os.remove(file) | 
|  | 80 | except BaseException: | 
|  | 81 | print("{} not found".format(file)) | 
| Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 82 |  | 
| Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 83 | with open(file, "w") as registry: | 
| Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 84 | registry.write(REGISTRY_HEADER.format(namespace)) | 
|  | 85 | # Parse the Registry header info | 
| Ed Tanous | 5b9ef70 | 2022-02-17 12:19:32 -0800 | [diff] [blame] | 86 | registry.write( | 
| Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 87 | "const Header header = {{\n" | 
| Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 88 | '    "{json_dict[@Redfish.Copyright]}",\n' | 
|  | 89 | '    "{json_dict[@odata.type]}",\n' | 
|  | 90 | '    "{json_dict[Id]}",\n' | 
|  | 91 | '    "{json_dict[Name]}",\n' | 
|  | 92 | '    "{json_dict[Language]}",\n' | 
|  | 93 | '    "{json_dict[Description]}",\n' | 
|  | 94 | '    "{json_dict[RegistryPrefix]}",\n' | 
|  | 95 | '    "{json_dict[RegistryVersion]}",\n' | 
|  | 96 | '    "{json_dict[OwningEntity]}",\n' | 
| Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 97 | "}};\n" | 
|  | 98 | "constexpr const char* url =\n" | 
| Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 99 | '    "{url}";\n' | 
| Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 100 | "\n" | 
|  | 101 | "constexpr std::array registry =\n" | 
|  | 102 | "{{\n".format( | 
|  | 103 | json_dict=json_dict, | 
|  | 104 | url=url, | 
| Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 105 | ) | 
|  | 106 | ) | 
| Ed Tanous | 30a3c43 | 2022-02-07 09:37:21 -0800 | [diff] [blame] | 107 |  | 
| Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 108 | messages_sorted = sorted(json_dict["Messages"].items()) | 
|  | 109 | for messageId, message in messages_sorted: | 
|  | 110 | registry.write( | 
|  | 111 | "    MessageEntry{{\n" | 
| Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 112 | '        "{messageId}",\n' | 
| Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 113 | "        {{\n" | 
| Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 114 | '            "{message[Description]}",\n' | 
|  | 115 | '            "{message[Message]}",\n' | 
|  | 116 | '            "{message[MessageSeverity]}",\n' | 
| Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 117 | "            {message[NumberOfArgs]},\n" | 
|  | 118 | "            {{".format( | 
| Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 119 | messageId=messageId, message=message | 
|  | 120 | ) | 
|  | 121 | ) | 
| Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 122 | paramTypes = message.get("ParamTypes") | 
|  | 123 | if paramTypes: | 
|  | 124 | for paramType in paramTypes: | 
|  | 125 | registry.write( | 
| Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 126 | '\n                "{}",'.format(paramType) | 
| Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 127 | ) | 
|  | 128 | registry.write("\n            },\n") | 
|  | 129 | else: | 
|  | 130 | registry.write("},\n") | 
|  | 131 | registry.write( | 
| Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 132 | '            "{message[Resolution]}",\n' | 
|  | 133 | "        }}}},\n".format(message=message) | 
|  | 134 | ) | 
| Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 135 |  | 
| Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 136 | registry.write("\n};\n\nenum class Index\n{\n") | 
| Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 137 | for index, (messageId, message) in enumerate(messages_sorted): | 
|  | 138 | messageId = messageId[0].lower() + messageId[1:] | 
| Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 139 | registry.write("    {} = {},\n".format(messageId, index)) | 
| Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 140 | registry.write( | 
| Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 141 | "}};\n}} // namespace redfish::registries::{}\n".format( | 
|  | 142 | namespace | 
|  | 143 | ) | 
|  | 144 | ) | 
| Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 145 |  | 
|  | 146 |  | 
|  | 147 | def get_privilege_string_from_list(privilege_list): | 
|  | 148 | privilege_string = "{{\n" | 
|  | 149 | for privilege_json in privilege_list: | 
|  | 150 | privileges = privilege_json["Privilege"] | 
|  | 151 | privilege_string += "    {" | 
|  | 152 | for privilege in privileges: | 
|  | 153 | if privilege == "NoAuth": | 
|  | 154 | continue | 
| Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 155 | privilege_string += '"' | 
| Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 156 | privilege_string += privilege | 
| Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 157 | privilege_string += '",\n' | 
| Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 158 | if privilege != "NoAuth": | 
|  | 159 | privilege_string = privilege_string[:-2] | 
|  | 160 | privilege_string += "}" | 
|  | 161 | privilege_string += ",\n" | 
|  | 162 | privilege_string = privilege_string[:-2] | 
|  | 163 | privilege_string += "\n}}" | 
|  | 164 | return privilege_string | 
|  | 165 |  | 
| Ed Tanous | f395daa | 2021-08-02 08:56:24 -0700 | [diff] [blame] | 166 |  | 
| Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 167 | def get_variable_name_for_privilege_set(privilege_list): | 
|  | 168 | names = [] | 
|  | 169 | for privilege_json in privilege_list: | 
|  | 170 | privileges = privilege_json["Privilege"] | 
|  | 171 | names.append("And".join(privileges)) | 
|  | 172 | return "Or".join(names) | 
|  | 173 |  | 
| Ed Tanous | f395daa | 2021-08-02 08:56:24 -0700 | [diff] [blame] | 174 |  | 
| Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 175 | PRIVILEGE_HEADER = ( | 
|  | 176 | PRAGMA_ONCE | 
|  | 177 | + WARNING | 
|  | 178 | + """ | 
| Nan Zhou | 01c78a0 | 2022-09-20 18:17:20 +0000 | [diff] [blame] | 179 | #include "privileges.hpp" | 
|  | 180 |  | 
|  | 181 | #include <array> | 
| Nan Zhou | 043bec0 | 2022-09-20 18:12:13 +0000 | [diff] [blame] | 182 |  | 
|  | 183 | // clang-format off | 
|  | 184 |  | 
|  | 185 | namespace redfish::privileges | 
|  | 186 | { | 
| Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 187 | """ | 
|  | 188 | ) | 
| Nan Zhou | 043bec0 | 2022-09-20 18:12:13 +0000 | [diff] [blame] | 189 |  | 
|  | 190 |  | 
| Ed Tanous | 42079ec | 2024-11-16 13:32:29 -0800 | [diff] [blame^] | 191 | def get_old_index(entry): | 
|  | 192 | old_order = [ | 
|  | 193 | "ResourceInUse", | 
|  | 194 | "MalformedJSON", | 
|  | 195 | "ResourceMissingAtURI", | 
|  | 196 | "ActionParameterValueFormatError", | 
|  | 197 | "ActionParameterValueNotInList", | 
|  | 198 | "InternalError", | 
|  | 199 | "UnrecognizedRequestBody", | 
|  | 200 | "ResourceAtUriUnauthorized", | 
|  | 201 | "ActionParameterUnknown", | 
|  | 202 | "ResourceCannotBeDeleted", | 
|  | 203 | "PropertyDuplicate", | 
|  | 204 | "ServiceTemporarilyUnavailable", | 
|  | 205 | "ResourceAlreadyExists", | 
|  | 206 | "AccountForSessionNoLongerExists", | 
|  | 207 | "CreateFailedMissingReqProperties", | 
|  | 208 | "PropertyValueFormatError", | 
|  | 209 | "PropertyValueNotInList", | 
|  | 210 | "PropertyValueOutOfRange", | 
|  | 211 | "ResourceAtUriInUnknownFormat", | 
|  | 212 | "ServiceDisabled", | 
|  | 213 | "ServiceInUnknownState", | 
|  | 214 | "EventSubscriptionLimitExceeded", | 
|  | 215 | "ActionParameterMissing", | 
|  | 216 | "StringValueTooLong", | 
|  | 217 | "SessionTerminated", | 
|  | 218 | "SubscriptionTerminated", | 
|  | 219 | "ResourceTypeIncompatible", | 
|  | 220 | "ResetRequired", | 
|  | 221 | "ChassisPowerStateOnRequired", | 
|  | 222 | "ChassisPowerStateOffRequired", | 
|  | 223 | "PropertyValueConflict", | 
|  | 224 | "PropertyValueResourceConflict", | 
|  | 225 | "PropertyValueExternalConflict", | 
|  | 226 | "PropertyValueIncorrect", | 
|  | 227 | "ResourceCreationConflict", | 
|  | 228 | "MaximumErrorsExceeded", | 
|  | 229 | "PreconditionFailed", | 
|  | 230 | "PreconditionRequired", | 
|  | 231 | "OperationFailed", | 
|  | 232 | "OperationTimeout", | 
|  | 233 | "PropertyValueTypeError", | 
|  | 234 | "PropertyValueError", | 
|  | 235 | "ResourceNotFound", | 
|  | 236 | "CouldNotEstablishConnection", | 
|  | 237 | "PropertyNotWritable", | 
|  | 238 | "QueryParameterValueTypeError", | 
|  | 239 | "ServiceShuttingDown", | 
|  | 240 | "ActionParameterDuplicate", | 
|  | 241 | "ActionParameterNotSupported", | 
|  | 242 | "SourceDoesNotSupportProtocol", | 
|  | 243 | "StrictAccountTypes", | 
|  | 244 | "AccountRemoved", | 
|  | 245 | "AccessDenied", | 
|  | 246 | "QueryNotSupported", | 
|  | 247 | "CreateLimitReachedForResource", | 
|  | 248 | "GeneralError", | 
|  | 249 | "Success", | 
|  | 250 | "Created", | 
|  | 251 | "NoOperation", | 
|  | 252 | "PropertyUnknown", | 
|  | 253 | "NoValidSession", | 
|  | 254 | "InvalidObject", | 
|  | 255 | "ResourceInStandby", | 
|  | 256 | "ActionParameterValueTypeError", | 
|  | 257 | "ActionParameterValueError", | 
|  | 258 | "SessionLimitExceeded", | 
|  | 259 | "ActionNotSupported", | 
|  | 260 | "InvalidIndex", | 
|  | 261 | "EmptyJSON", | 
|  | 262 | "QueryNotSupportedOnResource", | 
|  | 263 | "QueryNotSupportedOnOperation", | 
|  | 264 | "QueryCombinationInvalid", | 
|  | 265 | "EventBufferExceeded", | 
|  | 266 | "InsufficientPrivilege", | 
|  | 267 | "PropertyValueModified", | 
|  | 268 | "AccountNotModified", | 
|  | 269 | "QueryParameterValueFormatError", | 
|  | 270 | "PropertyMissing", | 
|  | 271 | "ResourceExhaustion", | 
|  | 272 | "AccountModified", | 
|  | 273 | "QueryParameterOutOfRange", | 
|  | 274 | "PasswordChangeRequired", | 
|  | 275 | "InvalidUpload", | 
|  | 276 | "InsufficientStorage", | 
|  | 277 | "OperationNotAllowed", | 
|  | 278 | "ArraySizeTooLong", | 
|  | 279 | "Invalid File", | 
|  | 280 | "GenerateSecretKeyRequired", | 
|  | 281 | ] | 
|  | 282 |  | 
|  | 283 | if entry[0] in old_order: | 
|  | 284 | return old_order.index(entry[0]) | 
|  | 285 | else: | 
|  | 286 | return 999999 | 
|  | 287 |  | 
|  | 288 |  | 
|  | 289 | def create_error_registry(entry): | 
|  | 290 |  | 
|  | 291 | arg_as_url = { | 
|  | 292 | "AccessDenied": [1], | 
|  | 293 | "CouldNotEstablishConnection": [1], | 
|  | 294 | "GenerateSecretKeyRequired": [1], | 
|  | 295 | "InvalidObject": [1], | 
|  | 296 | "PasswordChangeRequired": [1], | 
|  | 297 | "PropertyValueResourceConflict": [3], | 
|  | 298 | "ResetRequired": [1], | 
|  | 299 | "ResourceAtUriInUnknownFormat": [1], | 
|  | 300 | "ResourceAtUriUnauthorized": [1], | 
|  | 301 | "ResourceCreationConflict": [1], | 
|  | 302 | "ResourceMissingAtURI": [1], | 
|  | 303 | "SourceDoesNotSupportProtocol": [1], | 
|  | 304 | } | 
|  | 305 |  | 
|  | 306 | arg_as_json = { | 
|  | 307 | "ActionParameterValueError": [1], | 
|  | 308 | "ActionParameterValueFormatError": [1], | 
|  | 309 | "ActionParameterValueTypeError": [1], | 
|  | 310 | "PropertyValueExternalConflict": [2], | 
|  | 311 | "PropertyValueFormatError": [1], | 
|  | 312 | "PropertyValueIncorrect": [2], | 
|  | 313 | "PropertyValueModified": [2], | 
|  | 314 | "PropertyValueNotInList": [1], | 
|  | 315 | "PropertyValueOutOfRange": [1], | 
|  | 316 | "PropertyValueResourceConflict": [2], | 
|  | 317 | "PropertyValueTypeError": [1], | 
|  | 318 | "QueryParameterValueFormatError": [1], | 
|  | 319 | "QueryParameterValueTypeError": [1], | 
|  | 320 | } | 
|  | 321 |  | 
|  | 322 | arg_as_int = { | 
|  | 323 | "StringValueTooLong": [2], | 
|  | 324 | } | 
|  | 325 |  | 
|  | 326 | arg_as_uint64 = { | 
|  | 327 | "ArraySizeTooLong": [2], | 
|  | 328 | } | 
|  | 329 | arg_as_int64 = { | 
|  | 330 | "InvalidIndex": [1], | 
|  | 331 | } | 
|  | 332 |  | 
|  | 333 | file, json_dict, namespace, url = entry | 
|  | 334 |  | 
|  | 335 | # Note, this message doesn't exist in DMTF.  Needs cleaned up at some point | 
|  | 336 | json_dict["Messages"]["InvalidUpload"] = { | 
|  | 337 | "Message": "Invalid file uploaded to %1: %2.*", | 
|  | 338 | "ParamTypes": ["string", "string"], | 
|  | 339 | } | 
|  | 340 |  | 
|  | 341 | messages = OrderedDict( | 
|  | 342 | sorted(json_dict["Messages"].items(), key=get_old_index) | 
|  | 343 | ) | 
|  | 344 | error_messages_hpp = os.path.join( | 
|  | 345 | SCRIPT_DIR, "..", "redfish-core", "include", "error_messages.hpp" | 
|  | 346 | ) | 
|  | 347 | with open( | 
|  | 348 | error_messages_hpp, | 
|  | 349 | "w", | 
|  | 350 | ) as out: | 
|  | 351 | out.write(PRAGMA_ONCE) | 
|  | 352 | out.write(WARNING) | 
|  | 353 | out.write( | 
|  | 354 | """ | 
|  | 355 |  | 
|  | 356 | #include "http_response.hpp" | 
|  | 357 |  | 
|  | 358 | #include <boost/url/url_view_base.hpp> | 
|  | 359 | #include <nlohmann/json.hpp> | 
|  | 360 |  | 
|  | 361 | #include <cstdint> | 
|  | 362 | #include <source_location> | 
|  | 363 | #include <string> | 
|  | 364 | #include <string_view> | 
|  | 365 |  | 
|  | 366 | // IWYU pragma: no_forward_declare crow::Response | 
|  | 367 |  | 
|  | 368 | namespace redfish | 
|  | 369 | { | 
|  | 370 |  | 
|  | 371 | namespace messages | 
|  | 372 | { | 
|  | 373 |  | 
|  | 374 | constexpr const char* messageVersionPrefix = "Base.1.11.0."; | 
|  | 375 | constexpr const char* messageAnnotation = "@Message.ExtendedInfo"; | 
|  | 376 |  | 
|  | 377 | /** | 
|  | 378 | * @brief Moves all error messages from the |source| JSON to |target| | 
|  | 379 | */ | 
|  | 380 | void moveErrorsToErrorJson(nlohmann::json& target, nlohmann::json& source); | 
|  | 381 |  | 
|  | 382 | """ | 
|  | 383 | ) | 
|  | 384 |  | 
|  | 385 | for entry_id, entry in messages.items(): | 
|  | 386 | message = entry["Message"] | 
|  | 387 | for index in range(1, 10): | 
|  | 388 | message = message.replace(f"'%{index}'", f"<arg{index}>") | 
|  | 389 | message = message.replace(f"%{index}", f"<arg{index}>") | 
|  | 390 |  | 
|  | 391 | out.write("/**\n") | 
|  | 392 | out.write(f"* @brief Formats {entry_id} message into JSON\n") | 
|  | 393 | out.write(f'* Message body: "{message}"\n') | 
|  | 394 | out.write("*\n") | 
|  | 395 | arg_index = 0 | 
|  | 396 | for arg_index, arg in enumerate(entry.get("ParamTypes", [])): | 
|  | 397 | arg_index += 1 | 
|  | 398 |  | 
|  | 399 | out.write( | 
|  | 400 | f"* @param[in] arg{arg_index} Parameter of message that will replace %{arg_index} in its body.\n" | 
|  | 401 | ) | 
|  | 402 | out.write("*\n") | 
|  | 403 | out.write(f"* @returns Message {entry_id} formatted to JSON */\n") | 
|  | 404 |  | 
|  | 405 | args = [] | 
|  | 406 | for arg_index, arg in enumerate(entry.get("ParamTypes", [])): | 
|  | 407 | arg_index += 1 | 
|  | 408 | if arg_index in arg_as_url.get(entry_id, []): | 
|  | 409 | typename = "const boost::urls::url_view_base&" | 
|  | 410 | elif arg_index in arg_as_json.get(entry_id, []): | 
|  | 411 | typename = "const nlohmann::json&" | 
|  | 412 | elif arg_index in arg_as_int.get(entry_id, []): | 
|  | 413 | typename = "int" | 
|  | 414 | elif arg_index in arg_as_uint64.get(entry_id, []): | 
|  | 415 | typename = "uint64_t" | 
|  | 416 | elif arg_index in arg_as_int64.get(entry_id, []): | 
|  | 417 | typename = "int64_t" | 
|  | 418 | else: | 
|  | 419 | typename = "std::string_view" | 
|  | 420 | args.append(f"{typename} arg{arg_index}") | 
|  | 421 | function_name = entry_id[0].lower() + entry_id[1:] | 
|  | 422 | arg = ", ".join(args) | 
|  | 423 | out.write(f"nlohmann::json {function_name}({arg});\n\n") | 
|  | 424 | args.insert(0, "crow::Response& res") | 
|  | 425 | if entry_id == "InternalError": | 
|  | 426 | args.append( | 
|  | 427 | "std::source_location location = std::source_location::current()" | 
|  | 428 | ) | 
|  | 429 | arg = ", ".join(args) | 
|  | 430 | out.write(f"void {function_name}({arg});\n\n") | 
|  | 431 | out.write("    }\n") | 
|  | 432 | out.write("}\n") | 
|  | 433 | os.system(f"clang-format -i {error_messages_hpp}") | 
|  | 434 |  | 
|  | 435 |  | 
| Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 436 | def make_privilege_registry(): | 
| Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 437 | path, json_file, type_name, url = make_getter( | 
| Gunnar Mills | 5910d94 | 2024-04-16 12:07:17 -0500 | [diff] [blame] | 438 | "Redfish_1.5.0_PrivilegeRegistry.json", | 
| Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 439 | "privilege_registry.hpp", | 
|  | 440 | "privilege", | 
|  | 441 | ) | 
|  | 442 | with open(path, "w") as registry: | 
| Nan Zhou | 043bec0 | 2022-09-20 18:12:13 +0000 | [diff] [blame] | 443 | registry.write(PRIVILEGE_HEADER) | 
| Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 444 |  | 
|  | 445 | privilege_dict = {} | 
|  | 446 | for mapping in json_file["Mappings"]: | 
|  | 447 | # first pass, identify all the unique privilege sets | 
|  | 448 | for operation, privilege_list in mapping["OperationMap"].items(): | 
| Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 449 | privilege_dict[ | 
|  | 450 | get_privilege_string_from_list(privilege_list) | 
|  | 451 | ] = (privilege_list,) | 
| Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 452 | for index, key in enumerate(privilege_dict): | 
| Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 453 | (privilege_list,) = privilege_dict[key] | 
| Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 454 | name = get_variable_name_for_privilege_set(privilege_list) | 
| Ed Tanous | f395daa | 2021-08-02 08:56:24 -0700 | [diff] [blame] | 455 | registry.write( | 
| Ed Tanous | 5b9ef70 | 2022-02-17 12:19:32 -0800 | [diff] [blame] | 456 | "const std::array<Privileges, {length}> " | 
| Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 457 | "privilegeSet{name} = {key};\n".format( | 
|  | 458 | length=len(privilege_list), name=name, key=key | 
|  | 459 | ) | 
| Ed Tanous | 5b9ef70 | 2022-02-17 12:19:32 -0800 | [diff] [blame] | 460 | ) | 
| Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 461 | privilege_dict[key] = (privilege_list, name) | 
| Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 462 |  | 
|  | 463 | for mapping in json_file["Mappings"]: | 
|  | 464 | entity = mapping["Entity"] | 
| Ed Tanous | 4d99bbb | 2022-02-17 10:02:57 -0800 | [diff] [blame] | 465 | registry.write("// {}\n".format(entity)) | 
| Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 466 | for operation, privilege_list in mapping["OperationMap"].items(): | 
|  | 467 | privilege_string = get_privilege_string_from_list( | 
| Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 468 | privilege_list | 
|  | 469 | ) | 
| Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 470 | operation = operation.lower() | 
|  | 471 |  | 
| Ed Tanous | f395daa | 2021-08-02 08:56:24 -0700 | [diff] [blame] | 472 | registry.write( | 
|  | 473 | "const static auto& {}{} = privilegeSet{};\n".format( | 
| Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 474 | operation, entity, privilege_dict[privilege_string][1] | 
|  | 475 | ) | 
|  | 476 | ) | 
| Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 477 | registry.write("\n") | 
| Ed Tanous | 5b9ef70 | 2022-02-17 12:19:32 -0800 | [diff] [blame] | 478 | registry.write( | 
| Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 479 | "} // namespace redfish::privileges\n// clang-format on\n" | 
|  | 480 | ) | 
| Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 481 |  | 
|  | 482 |  | 
| Gunnar Mills | 665e760 | 2024-04-10 13:14:41 -0500 | [diff] [blame] | 483 | def to_pascal_case(text): | 
|  | 484 | s = text.replace("_", " ") | 
|  | 485 | s = s.split() | 
|  | 486 | if len(text) == 0: | 
|  | 487 | return text | 
|  | 488 | return "".join(i.capitalize() for i in s[0:]) | 
|  | 489 |  | 
|  | 490 |  | 
| Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 491 | def main(): | 
| Gunnar Mills | 665e760 | 2024-04-10 13:14:41 -0500 | [diff] [blame] | 492 | dmtf_registries = ( | 
| Jishnu CM | b575cae | 2024-10-01 01:33:49 -0500 | [diff] [blame] | 493 | ("base", "1.19.0"), | 
| Gunnar Mills | 665e760 | 2024-04-10 13:14:41 -0500 | [diff] [blame] | 494 | ("composition", "1.1.2"), | 
|  | 495 | ("environmental", "1.0.1"), | 
|  | 496 | ("ethernet_fabric", "1.0.1"), | 
|  | 497 | ("fabric", "1.0.2"), | 
|  | 498 | ("heartbeat_event", "1.0.1"), | 
|  | 499 | ("job_event", "1.0.1"), | 
|  | 500 | ("license", "1.0.3"), | 
|  | 501 | ("log_service", "1.0.1"), | 
|  | 502 | ("network_device", "1.0.3"), | 
|  | 503 | ("platform", "1.0.1"), | 
|  | 504 | ("power", "1.0.1"), | 
|  | 505 | ("resource_event", "1.3.0"), | 
|  | 506 | ("sensor_event", "1.0.1"), | 
|  | 507 | ("storage_device", "1.2.1"), | 
|  | 508 | ("task_event", "1.0.3"), | 
|  | 509 | ("telemetry", "1.0.0"), | 
|  | 510 | ("update", "1.0.2"), | 
|  | 511 | ) | 
|  | 512 |  | 
| Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 513 | parser = argparse.ArgumentParser() | 
|  | 514 | parser.add_argument( | 
| Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 515 | "--registries", | 
|  | 516 | type=str, | 
| Gunnar Mills | 665e760 | 2024-04-10 13:14:41 -0500 | [diff] [blame] | 517 | default="privilege,openbmc," | 
|  | 518 | + ",".join([dmtf[0] for dmtf in dmtf_registries]), | 
| Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 519 | help="Comma delimited list of registries to update", | 
|  | 520 | ) | 
| Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 521 |  | 
|  | 522 | args = parser.parse_args() | 
|  | 523 |  | 
|  | 524 | registries = set(args.registries.split(",")) | 
|  | 525 | files = [] | 
|  | 526 |  | 
| Gunnar Mills | 665e760 | 2024-04-10 13:14:41 -0500 | [diff] [blame] | 527 | for registry, version in dmtf_registries: | 
|  | 528 | if registry in registries: | 
|  | 529 | registry_pascal_case = to_pascal_case(registry) | 
|  | 530 | files.append( | 
|  | 531 | make_getter( | 
|  | 532 | f"{registry_pascal_case}.{version}.json", | 
|  | 533 | f"{registry}_message_registry.hpp", | 
|  | 534 | registry, | 
|  | 535 | ) | 
| Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 536 | ) | 
| Ed Tanous | 3e5faba | 2023-08-16 15:11:29 -0700 | [diff] [blame] | 537 | if "openbmc" in registries: | 
|  | 538 | files.append(openbmc_local_getter()) | 
| Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 539 |  | 
|  | 540 | update_registries(files) | 
|  | 541 |  | 
| Ed Tanous | 42079ec | 2024-11-16 13:32:29 -0800 | [diff] [blame^] | 542 | create_error_registry(files[0]) | 
|  | 543 |  | 
| Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 544 | if "privilege" in registries: | 
|  | 545 | make_privilege_registry() | 
|  | 546 |  | 
|  | 547 |  | 
|  | 548 | if __name__ == "__main__": | 
|  | 549 | main() |