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", |
Ed Tanous | 42079ec | 2024-11-16 13:32:29 -0800 | [diff] [blame] | 275 | "InsufficientStorage", |
| 276 | "OperationNotAllowed", |
| 277 | "ArraySizeTooLong", |
| 278 | "Invalid File", |
| 279 | "GenerateSecretKeyRequired", |
| 280 | ] |
| 281 | |
| 282 | if entry[0] in old_order: |
| 283 | return old_order.index(entry[0]) |
| 284 | else: |
| 285 | return 999999 |
| 286 | |
| 287 | |
| 288 | def create_error_registry(entry): |
| 289 | |
| 290 | arg_as_url = { |
| 291 | "AccessDenied": [1], |
| 292 | "CouldNotEstablishConnection": [1], |
| 293 | "GenerateSecretKeyRequired": [1], |
| 294 | "InvalidObject": [1], |
| 295 | "PasswordChangeRequired": [1], |
| 296 | "PropertyValueResourceConflict": [3], |
| 297 | "ResetRequired": [1], |
| 298 | "ResourceAtUriInUnknownFormat": [1], |
| 299 | "ResourceAtUriUnauthorized": [1], |
| 300 | "ResourceCreationConflict": [1], |
| 301 | "ResourceMissingAtURI": [1], |
| 302 | "SourceDoesNotSupportProtocol": [1], |
| 303 | } |
| 304 | |
| 305 | arg_as_json = { |
| 306 | "ActionParameterValueError": [1], |
| 307 | "ActionParameterValueFormatError": [1], |
| 308 | "ActionParameterValueTypeError": [1], |
| 309 | "PropertyValueExternalConflict": [2], |
| 310 | "PropertyValueFormatError": [1], |
| 311 | "PropertyValueIncorrect": [2], |
| 312 | "PropertyValueModified": [2], |
| 313 | "PropertyValueNotInList": [1], |
| 314 | "PropertyValueOutOfRange": [1], |
| 315 | "PropertyValueResourceConflict": [2], |
| 316 | "PropertyValueTypeError": [1], |
| 317 | "QueryParameterValueFormatError": [1], |
| 318 | "QueryParameterValueTypeError": [1], |
| 319 | } |
| 320 | |
| 321 | arg_as_int = { |
| 322 | "StringValueTooLong": [2], |
| 323 | } |
| 324 | |
| 325 | arg_as_uint64 = { |
| 326 | "ArraySizeTooLong": [2], |
| 327 | } |
| 328 | arg_as_int64 = { |
| 329 | "InvalidIndex": [1], |
| 330 | } |
| 331 | |
| 332 | file, json_dict, namespace, url = entry |
| 333 | |
Ed Tanous | 42079ec | 2024-11-16 13:32:29 -0800 | [diff] [blame] | 334 | messages = OrderedDict( |
| 335 | sorted(json_dict["Messages"].items(), key=get_old_index) |
| 336 | ) |
| 337 | error_messages_hpp = os.path.join( |
| 338 | SCRIPT_DIR, "..", "redfish-core", "include", "error_messages.hpp" |
| 339 | ) |
| 340 | with open( |
| 341 | error_messages_hpp, |
| 342 | "w", |
| 343 | ) as out: |
| 344 | out.write(PRAGMA_ONCE) |
| 345 | out.write(WARNING) |
| 346 | out.write( |
| 347 | """ |
| 348 | |
| 349 | #include "http_response.hpp" |
| 350 | |
| 351 | #include <boost/url/url_view_base.hpp> |
| 352 | #include <nlohmann/json.hpp> |
| 353 | |
| 354 | #include <cstdint> |
| 355 | #include <source_location> |
| 356 | #include <string> |
| 357 | #include <string_view> |
Ed Tanous | c87294a | 2024-11-16 22:17:12 -0800 | [diff] [blame^] | 358 | #include <utility> |
Ed Tanous | 42079ec | 2024-11-16 13:32:29 -0800 | [diff] [blame] | 359 | |
| 360 | // IWYU pragma: no_forward_declare crow::Response |
| 361 | |
| 362 | namespace redfish |
| 363 | { |
| 364 | |
| 365 | namespace messages |
| 366 | { |
| 367 | |
| 368 | constexpr const char* messageVersionPrefix = "Base.1.11.0."; |
| 369 | constexpr const char* messageAnnotation = "@Message.ExtendedInfo"; |
| 370 | |
| 371 | /** |
| 372 | * @brief Moves all error messages from the |source| JSON to |target| |
| 373 | */ |
| 374 | void moveErrorsToErrorJson(nlohmann::json& target, nlohmann::json& source); |
| 375 | |
| 376 | """ |
| 377 | ) |
| 378 | |
| 379 | for entry_id, entry in messages.items(): |
| 380 | message = entry["Message"] |
| 381 | for index in range(1, 10): |
| 382 | message = message.replace(f"'%{index}'", f"<arg{index}>") |
| 383 | message = message.replace(f"%{index}", f"<arg{index}>") |
| 384 | |
| 385 | out.write("/**\n") |
| 386 | out.write(f"* @brief Formats {entry_id} message into JSON\n") |
| 387 | out.write(f'* Message body: "{message}"\n') |
| 388 | out.write("*\n") |
| 389 | arg_index = 0 |
| 390 | for arg_index, arg in enumerate(entry.get("ParamTypes", [])): |
| 391 | arg_index += 1 |
| 392 | |
| 393 | out.write( |
| 394 | f"* @param[in] arg{arg_index} Parameter of message that will replace %{arg_index} in its body.\n" |
| 395 | ) |
| 396 | out.write("*\n") |
| 397 | out.write(f"* @returns Message {entry_id} formatted to JSON */\n") |
| 398 | |
| 399 | args = [] |
| 400 | for arg_index, arg in enumerate(entry.get("ParamTypes", [])): |
| 401 | arg_index += 1 |
| 402 | if arg_index in arg_as_url.get(entry_id, []): |
| 403 | typename = "const boost::urls::url_view_base&" |
| 404 | elif arg_index in arg_as_json.get(entry_id, []): |
| 405 | typename = "const nlohmann::json&" |
| 406 | elif arg_index in arg_as_int.get(entry_id, []): |
| 407 | typename = "int" |
| 408 | elif arg_index in arg_as_uint64.get(entry_id, []): |
| 409 | typename = "uint64_t" |
| 410 | elif arg_index in arg_as_int64.get(entry_id, []): |
| 411 | typename = "int64_t" |
| 412 | else: |
| 413 | typename = "std::string_view" |
| 414 | args.append(f"{typename} arg{arg_index}") |
| 415 | function_name = entry_id[0].lower() + entry_id[1:] |
| 416 | arg = ", ".join(args) |
| 417 | out.write(f"nlohmann::json {function_name}({arg});\n\n") |
| 418 | args.insert(0, "crow::Response& res") |
| 419 | if entry_id == "InternalError": |
| 420 | args.append( |
| 421 | "std::source_location location = std::source_location::current()" |
| 422 | ) |
| 423 | arg = ", ".join(args) |
| 424 | out.write(f"void {function_name}({arg});\n\n") |
| 425 | out.write(" }\n") |
| 426 | out.write("}\n") |
| 427 | os.system(f"clang-format -i {error_messages_hpp}") |
| 428 | |
| 429 | |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 430 | def make_privilege_registry(): |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 431 | path, json_file, type_name, url = make_getter( |
Gunnar Mills | 5910d94 | 2024-04-16 12:07:17 -0500 | [diff] [blame] | 432 | "Redfish_1.5.0_PrivilegeRegistry.json", |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 433 | "privilege_registry.hpp", |
| 434 | "privilege", |
| 435 | ) |
| 436 | with open(path, "w") as registry: |
Nan Zhou | 043bec0 | 2022-09-20 18:12:13 +0000 | [diff] [blame] | 437 | registry.write(PRIVILEGE_HEADER) |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 438 | |
| 439 | privilege_dict = {} |
| 440 | for mapping in json_file["Mappings"]: |
| 441 | # first pass, identify all the unique privilege sets |
| 442 | for operation, privilege_list in mapping["OperationMap"].items(): |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 443 | privilege_dict[ |
| 444 | get_privilege_string_from_list(privilege_list) |
| 445 | ] = (privilege_list,) |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 446 | for index, key in enumerate(privilege_dict): |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 447 | (privilege_list,) = privilege_dict[key] |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 448 | name = get_variable_name_for_privilege_set(privilege_list) |
Ed Tanous | f395daa | 2021-08-02 08:56:24 -0700 | [diff] [blame] | 449 | registry.write( |
Ed Tanous | 5b9ef70 | 2022-02-17 12:19:32 -0800 | [diff] [blame] | 450 | "const std::array<Privileges, {length}> " |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 451 | "privilegeSet{name} = {key};\n".format( |
| 452 | length=len(privilege_list), name=name, key=key |
| 453 | ) |
Ed Tanous | 5b9ef70 | 2022-02-17 12:19:32 -0800 | [diff] [blame] | 454 | ) |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 455 | privilege_dict[key] = (privilege_list, name) |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 456 | |
| 457 | for mapping in json_file["Mappings"]: |
| 458 | entity = mapping["Entity"] |
Ed Tanous | 4d99bbb | 2022-02-17 10:02:57 -0800 | [diff] [blame] | 459 | registry.write("// {}\n".format(entity)) |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 460 | for operation, privilege_list in mapping["OperationMap"].items(): |
| 461 | privilege_string = get_privilege_string_from_list( |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 462 | privilege_list |
| 463 | ) |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 464 | operation = operation.lower() |
| 465 | |
Ed Tanous | f395daa | 2021-08-02 08:56:24 -0700 | [diff] [blame] | 466 | registry.write( |
| 467 | "const static auto& {}{} = privilegeSet{};\n".format( |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 468 | operation, entity, privilege_dict[privilege_string][1] |
| 469 | ) |
| 470 | ) |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 471 | registry.write("\n") |
Ed Tanous | 5b9ef70 | 2022-02-17 12:19:32 -0800 | [diff] [blame] | 472 | registry.write( |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 473 | "} // namespace redfish::privileges\n// clang-format on\n" |
| 474 | ) |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 475 | |
| 476 | |
Gunnar Mills | 665e760 | 2024-04-10 13:14:41 -0500 | [diff] [blame] | 477 | def to_pascal_case(text): |
| 478 | s = text.replace("_", " ") |
| 479 | s = s.split() |
| 480 | if len(text) == 0: |
| 481 | return text |
| 482 | return "".join(i.capitalize() for i in s[0:]) |
| 483 | |
| 484 | |
Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 485 | def main(): |
Gunnar Mills | 665e760 | 2024-04-10 13:14:41 -0500 | [diff] [blame] | 486 | dmtf_registries = ( |
Jishnu CM | b575cae | 2024-10-01 01:33:49 -0500 | [diff] [blame] | 487 | ("base", "1.19.0"), |
Gunnar Mills | 665e760 | 2024-04-10 13:14:41 -0500 | [diff] [blame] | 488 | ("composition", "1.1.2"), |
| 489 | ("environmental", "1.0.1"), |
| 490 | ("ethernet_fabric", "1.0.1"), |
| 491 | ("fabric", "1.0.2"), |
| 492 | ("heartbeat_event", "1.0.1"), |
| 493 | ("job_event", "1.0.1"), |
| 494 | ("license", "1.0.3"), |
| 495 | ("log_service", "1.0.1"), |
| 496 | ("network_device", "1.0.3"), |
| 497 | ("platform", "1.0.1"), |
| 498 | ("power", "1.0.1"), |
| 499 | ("resource_event", "1.3.0"), |
| 500 | ("sensor_event", "1.0.1"), |
| 501 | ("storage_device", "1.2.1"), |
| 502 | ("task_event", "1.0.3"), |
| 503 | ("telemetry", "1.0.0"), |
| 504 | ("update", "1.0.2"), |
| 505 | ) |
| 506 | |
Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 507 | parser = argparse.ArgumentParser() |
| 508 | parser.add_argument( |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 509 | "--registries", |
| 510 | type=str, |
Gunnar Mills | 665e760 | 2024-04-10 13:14:41 -0500 | [diff] [blame] | 511 | default="privilege,openbmc," |
| 512 | + ",".join([dmtf[0] for dmtf in dmtf_registries]), |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 513 | help="Comma delimited list of registries to update", |
| 514 | ) |
Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 515 | |
| 516 | args = parser.parse_args() |
| 517 | |
| 518 | registries = set(args.registries.split(",")) |
| 519 | files = [] |
| 520 | |
Gunnar Mills | 665e760 | 2024-04-10 13:14:41 -0500 | [diff] [blame] | 521 | for registry, version in dmtf_registries: |
| 522 | if registry in registries: |
| 523 | registry_pascal_case = to_pascal_case(registry) |
| 524 | files.append( |
| 525 | make_getter( |
| 526 | f"{registry_pascal_case}.{version}.json", |
| 527 | f"{registry}_message_registry.hpp", |
| 528 | registry, |
| 529 | ) |
Patrick Williams | dfa3fdc | 2022-12-07 07:14:21 -0600 | [diff] [blame] | 530 | ) |
Ed Tanous | 3e5faba | 2023-08-16 15:11:29 -0700 | [diff] [blame] | 531 | if "openbmc" in registries: |
| 532 | files.append(openbmc_local_getter()) |
Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 533 | |
| 534 | update_registries(files) |
| 535 | |
Ed Tanous | 42079ec | 2024-11-16 13:32:29 -0800 | [diff] [blame] | 536 | create_error_registry(files[0]) |
| 537 | |
Nan Zhou | 49aa131f | 2022-09-20 18:41:37 +0000 | [diff] [blame] | 538 | if "privilege" in registries: |
| 539 | make_privilege_registry() |
| 540 | |
| 541 | |
| 542 | if __name__ == "__main__": |
| 543 | main() |