| #!/usr/bin/env python3 |
| import argparse |
| import json |
| import os |
| from collections import OrderedDict |
| |
| import requests |
| |
| PRAGMA_ONCE = """#pragma once |
| """ |
| |
| WARNING = """/**************************************************************** |
| * READ THIS WARNING FIRST |
| * This is an auto-generated header which contains definitions |
| * for Redfish DMTF defined messages. |
| * DO NOT modify this registry outside of running the |
| * parse_registries.py script. The definitions contained within |
| * this file are owned by DMTF. Any modifications to these files |
| * should be first pushed to the relevant registry in the DMTF |
| * github organization. |
| ***************************************************************/""" |
| |
| REGISTRY_HEADER = ( |
| PRAGMA_ONCE |
| + WARNING |
| + """ |
| #include "registries.hpp" |
| |
| #include <array> |
| |
| // clang-format off |
| |
| namespace redfish::registries::{} |
| {{ |
| """ |
| ) |
| |
| SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__)) |
| |
| include_path = os.path.realpath( |
| os.path.join(SCRIPT_DIR, "..", "redfish-core", "include", "registries") |
| ) |
| |
| proxies = {"https": os.environ.get("https_proxy", None)} |
| |
| |
| def make_getter(dmtf_name, header_name, type_name): |
| url = "https://redfish.dmtf.org/registries/{}".format(dmtf_name) |
| dmtf = requests.get(url, proxies=proxies) |
| dmtf.raise_for_status() |
| json_file = json.loads(dmtf.text, object_pairs_hook=OrderedDict) |
| path = os.path.join(include_path, header_name) |
| return (path, json_file, type_name, url) |
| |
| |
| def openbmc_local_getter(): |
| url = "" |
| with open( |
| os.path.join( |
| SCRIPT_DIR, |
| "..", |
| "redfish-core", |
| "include", |
| "registries", |
| "openbmc.json", |
| ), |
| "rb", |
| ) as json_file: |
| json_file = json.load(json_file) |
| |
| path = os.path.join(include_path, "openbmc_message_registry.hpp") |
| return (path, json_file, "openbmc", url) |
| |
| |
| def update_registries(files): |
| # Remove the old files |
| for file, json_dict, namespace, url in files: |
| try: |
| os.remove(file) |
| except BaseException: |
| print("{} not found".format(file)) |
| |
| with open(file, "w") as registry: |
| registry.write(REGISTRY_HEADER.format(namespace)) |
| # Parse the Registry header info |
| registry.write( |
| "const Header header = {{\n" |
| ' "{json_dict[@Redfish.Copyright]}",\n' |
| ' "{json_dict[@odata.type]}",\n' |
| ' "{json_dict[Id]}",\n' |
| ' "{json_dict[Name]}",\n' |
| ' "{json_dict[Language]}",\n' |
| ' "{json_dict[Description]}",\n' |
| ' "{json_dict[RegistryPrefix]}",\n' |
| ' "{json_dict[RegistryVersion]}",\n' |
| ' "{json_dict[OwningEntity]}",\n' |
| "}};\n" |
| "constexpr const char* url =\n" |
| ' "{url}";\n' |
| "\n" |
| "constexpr std::array registry =\n" |
| "{{\n".format( |
| json_dict=json_dict, |
| url=url, |
| ) |
| ) |
| |
| messages_sorted = sorted(json_dict["Messages"].items()) |
| for messageId, message in messages_sorted: |
| registry.write( |
| " MessageEntry{{\n" |
| ' "{messageId}",\n' |
| " {{\n" |
| ' "{message[Description]}",\n' |
| ' "{message[Message]}",\n' |
| ' "{message[MessageSeverity]}",\n' |
| " {message[NumberOfArgs]},\n" |
| " {{".format( |
| messageId=messageId, message=message |
| ) |
| ) |
| paramTypes = message.get("ParamTypes") |
| if paramTypes: |
| for paramType in paramTypes: |
| registry.write( |
| '\n "{}",'.format(paramType) |
| ) |
| registry.write("\n },\n") |
| else: |
| registry.write("},\n") |
| registry.write( |
| ' "{message[Resolution]}",\n' |
| " }}}},\n".format(message=message) |
| ) |
| |
| registry.write("\n};\n\nenum class Index\n{\n") |
| for index, (messageId, message) in enumerate(messages_sorted): |
| messageId = messageId[0].lower() + messageId[1:] |
| registry.write(" {} = {},\n".format(messageId, index)) |
| registry.write( |
| "}};\n}} // namespace redfish::registries::{}\n".format( |
| namespace |
| ) |
| ) |
| |
| |
| def get_privilege_string_from_list(privilege_list): |
| privilege_string = "{{\n" |
| for privilege_json in privilege_list: |
| privileges = privilege_json["Privilege"] |
| privilege_string += " {" |
| for privilege in privileges: |
| if privilege == "NoAuth": |
| continue |
| privilege_string += '"' |
| privilege_string += privilege |
| privilege_string += '",\n' |
| if privilege != "NoAuth": |
| privilege_string = privilege_string[:-2] |
| privilege_string += "}" |
| privilege_string += ",\n" |
| privilege_string = privilege_string[:-2] |
| privilege_string += "\n}}" |
| return privilege_string |
| |
| |
| def get_variable_name_for_privilege_set(privilege_list): |
| names = [] |
| for privilege_json in privilege_list: |
| privileges = privilege_json["Privilege"] |
| names.append("And".join(privileges)) |
| return "Or".join(names) |
| |
| |
| PRIVILEGE_HEADER = ( |
| PRAGMA_ONCE |
| + WARNING |
| + """ |
| #include "privileges.hpp" |
| |
| #include <array> |
| |
| // clang-format off |
| |
| namespace redfish::privileges |
| { |
| """ |
| ) |
| |
| |
| def get_old_index(entry): |
| old_order = [ |
| "ResourceInUse", |
| "MalformedJSON", |
| "ResourceMissingAtURI", |
| "ActionParameterValueFormatError", |
| "ActionParameterValueNotInList", |
| "InternalError", |
| "UnrecognizedRequestBody", |
| "ResourceAtUriUnauthorized", |
| "ActionParameterUnknown", |
| "ResourceCannotBeDeleted", |
| "PropertyDuplicate", |
| "ServiceTemporarilyUnavailable", |
| "ResourceAlreadyExists", |
| "AccountForSessionNoLongerExists", |
| "CreateFailedMissingReqProperties", |
| "PropertyValueFormatError", |
| "PropertyValueNotInList", |
| "PropertyValueOutOfRange", |
| "ResourceAtUriInUnknownFormat", |
| "ServiceDisabled", |
| "ServiceInUnknownState", |
| "EventSubscriptionLimitExceeded", |
| "ActionParameterMissing", |
| "StringValueTooLong", |
| "SessionTerminated", |
| "SubscriptionTerminated", |
| "ResourceTypeIncompatible", |
| "ResetRequired", |
| "ChassisPowerStateOnRequired", |
| "ChassisPowerStateOffRequired", |
| "PropertyValueConflict", |
| "PropertyValueResourceConflict", |
| "PropertyValueExternalConflict", |
| "PropertyValueIncorrect", |
| "ResourceCreationConflict", |
| "MaximumErrorsExceeded", |
| "PreconditionFailed", |
| "PreconditionRequired", |
| "OperationFailed", |
| "OperationTimeout", |
| "PropertyValueTypeError", |
| "PropertyValueError", |
| "ResourceNotFound", |
| "CouldNotEstablishConnection", |
| "PropertyNotWritable", |
| "QueryParameterValueTypeError", |
| "ServiceShuttingDown", |
| "ActionParameterDuplicate", |
| "ActionParameterNotSupported", |
| "SourceDoesNotSupportProtocol", |
| "StrictAccountTypes", |
| "AccountRemoved", |
| "AccessDenied", |
| "QueryNotSupported", |
| "CreateLimitReachedForResource", |
| "GeneralError", |
| "Success", |
| "Created", |
| "NoOperation", |
| "PropertyUnknown", |
| "NoValidSession", |
| "InvalidObject", |
| "ResourceInStandby", |
| "ActionParameterValueTypeError", |
| "ActionParameterValueError", |
| "SessionLimitExceeded", |
| "ActionNotSupported", |
| "InvalidIndex", |
| "EmptyJSON", |
| "QueryNotSupportedOnResource", |
| "QueryNotSupportedOnOperation", |
| "QueryCombinationInvalid", |
| "EventBufferExceeded", |
| "InsufficientPrivilege", |
| "PropertyValueModified", |
| "AccountNotModified", |
| "QueryParameterValueFormatError", |
| "PropertyMissing", |
| "ResourceExhaustion", |
| "AccountModified", |
| "QueryParameterOutOfRange", |
| "PasswordChangeRequired", |
| "InvalidUpload", |
| "InsufficientStorage", |
| "OperationNotAllowed", |
| "ArraySizeTooLong", |
| "Invalid File", |
| "GenerateSecretKeyRequired", |
| ] |
| |
| if entry[0] in old_order: |
| return old_order.index(entry[0]) |
| else: |
| return 999999 |
| |
| |
| def create_error_registry(entry): |
| |
| arg_as_url = { |
| "AccessDenied": [1], |
| "CouldNotEstablishConnection": [1], |
| "GenerateSecretKeyRequired": [1], |
| "InvalidObject": [1], |
| "PasswordChangeRequired": [1], |
| "PropertyValueResourceConflict": [3], |
| "ResetRequired": [1], |
| "ResourceAtUriInUnknownFormat": [1], |
| "ResourceAtUriUnauthorized": [1], |
| "ResourceCreationConflict": [1], |
| "ResourceMissingAtURI": [1], |
| "SourceDoesNotSupportProtocol": [1], |
| } |
| |
| arg_as_json = { |
| "ActionParameterValueError": [1], |
| "ActionParameterValueFormatError": [1], |
| "ActionParameterValueTypeError": [1], |
| "PropertyValueExternalConflict": [2], |
| "PropertyValueFormatError": [1], |
| "PropertyValueIncorrect": [2], |
| "PropertyValueModified": [2], |
| "PropertyValueNotInList": [1], |
| "PropertyValueOutOfRange": [1], |
| "PropertyValueResourceConflict": [2], |
| "PropertyValueTypeError": [1], |
| "QueryParameterValueFormatError": [1], |
| "QueryParameterValueTypeError": [1], |
| } |
| |
| arg_as_int = { |
| "StringValueTooLong": [2], |
| } |
| |
| arg_as_uint64 = { |
| "ArraySizeTooLong": [2], |
| } |
| arg_as_int64 = { |
| "InvalidIndex": [1], |
| } |
| |
| file, json_dict, namespace, url = entry |
| |
| # Note, this message doesn't exist in DMTF. Needs cleaned up at some point |
| json_dict["Messages"]["InvalidUpload"] = { |
| "Message": "Invalid file uploaded to %1: %2.*", |
| "ParamTypes": ["string", "string"], |
| } |
| |
| messages = OrderedDict( |
| sorted(json_dict["Messages"].items(), key=get_old_index) |
| ) |
| error_messages_hpp = os.path.join( |
| SCRIPT_DIR, "..", "redfish-core", "include", "error_messages.hpp" |
| ) |
| with open( |
| error_messages_hpp, |
| "w", |
| ) as out: |
| out.write(PRAGMA_ONCE) |
| out.write(WARNING) |
| out.write( |
| """ |
| |
| #include "http_response.hpp" |
| |
| #include <boost/url/url_view_base.hpp> |
| #include <nlohmann/json.hpp> |
| |
| #include <cstdint> |
| #include <source_location> |
| #include <string> |
| #include <string_view> |
| |
| // IWYU pragma: no_forward_declare crow::Response |
| |
| namespace redfish |
| { |
| |
| namespace messages |
| { |
| |
| constexpr const char* messageVersionPrefix = "Base.1.11.0."; |
| constexpr const char* messageAnnotation = "@Message.ExtendedInfo"; |
| |
| /** |
| * @brief Moves all error messages from the |source| JSON to |target| |
| */ |
| void moveErrorsToErrorJson(nlohmann::json& target, nlohmann::json& source); |
| |
| """ |
| ) |
| |
| for entry_id, entry in messages.items(): |
| message = entry["Message"] |
| for index in range(1, 10): |
| message = message.replace(f"'%{index}'", f"<arg{index}>") |
| message = message.replace(f"%{index}", f"<arg{index}>") |
| |
| out.write("/**\n") |
| out.write(f"* @brief Formats {entry_id} message into JSON\n") |
| out.write(f'* Message body: "{message}"\n') |
| out.write("*\n") |
| arg_index = 0 |
| for arg_index, arg in enumerate(entry.get("ParamTypes", [])): |
| arg_index += 1 |
| |
| out.write( |
| f"* @param[in] arg{arg_index} Parameter of message that will replace %{arg_index} in its body.\n" |
| ) |
| out.write("*\n") |
| out.write(f"* @returns Message {entry_id} formatted to JSON */\n") |
| |
| args = [] |
| for arg_index, arg in enumerate(entry.get("ParamTypes", [])): |
| arg_index += 1 |
| if arg_index in arg_as_url.get(entry_id, []): |
| typename = "const boost::urls::url_view_base&" |
| elif arg_index in arg_as_json.get(entry_id, []): |
| typename = "const nlohmann::json&" |
| elif arg_index in arg_as_int.get(entry_id, []): |
| typename = "int" |
| elif arg_index in arg_as_uint64.get(entry_id, []): |
| typename = "uint64_t" |
| elif arg_index in arg_as_int64.get(entry_id, []): |
| typename = "int64_t" |
| else: |
| typename = "std::string_view" |
| args.append(f"{typename} arg{arg_index}") |
| function_name = entry_id[0].lower() + entry_id[1:] |
| arg = ", ".join(args) |
| out.write(f"nlohmann::json {function_name}({arg});\n\n") |
| args.insert(0, "crow::Response& res") |
| if entry_id == "InternalError": |
| args.append( |
| "std::source_location location = std::source_location::current()" |
| ) |
| arg = ", ".join(args) |
| out.write(f"void {function_name}({arg});\n\n") |
| out.write(" }\n") |
| out.write("}\n") |
| os.system(f"clang-format -i {error_messages_hpp}") |
| |
| |
| def make_privilege_registry(): |
| path, json_file, type_name, url = make_getter( |
| "Redfish_1.5.0_PrivilegeRegistry.json", |
| "privilege_registry.hpp", |
| "privilege", |
| ) |
| with open(path, "w") as registry: |
| registry.write(PRIVILEGE_HEADER) |
| |
| privilege_dict = {} |
| for mapping in json_file["Mappings"]: |
| # first pass, identify all the unique privilege sets |
| for operation, privilege_list in mapping["OperationMap"].items(): |
| privilege_dict[ |
| get_privilege_string_from_list(privilege_list) |
| ] = (privilege_list,) |
| for index, key in enumerate(privilege_dict): |
| (privilege_list,) = privilege_dict[key] |
| name = get_variable_name_for_privilege_set(privilege_list) |
| registry.write( |
| "const std::array<Privileges, {length}> " |
| "privilegeSet{name} = {key};\n".format( |
| length=len(privilege_list), name=name, key=key |
| ) |
| ) |
| privilege_dict[key] = (privilege_list, name) |
| |
| for mapping in json_file["Mappings"]: |
| entity = mapping["Entity"] |
| registry.write("// {}\n".format(entity)) |
| for operation, privilege_list in mapping["OperationMap"].items(): |
| privilege_string = get_privilege_string_from_list( |
| privilege_list |
| ) |
| operation = operation.lower() |
| |
| registry.write( |
| "const static auto& {}{} = privilegeSet{};\n".format( |
| operation, entity, privilege_dict[privilege_string][1] |
| ) |
| ) |
| registry.write("\n") |
| registry.write( |
| "} // namespace redfish::privileges\n// clang-format on\n" |
| ) |
| |
| |
| def to_pascal_case(text): |
| s = text.replace("_", " ") |
| s = s.split() |
| if len(text) == 0: |
| return text |
| return "".join(i.capitalize() for i in s[0:]) |
| |
| |
| def main(): |
| dmtf_registries = ( |
| ("base", "1.19.0"), |
| ("composition", "1.1.2"), |
| ("environmental", "1.0.1"), |
| ("ethernet_fabric", "1.0.1"), |
| ("fabric", "1.0.2"), |
| ("heartbeat_event", "1.0.1"), |
| ("job_event", "1.0.1"), |
| ("license", "1.0.3"), |
| ("log_service", "1.0.1"), |
| ("network_device", "1.0.3"), |
| ("platform", "1.0.1"), |
| ("power", "1.0.1"), |
| ("resource_event", "1.3.0"), |
| ("sensor_event", "1.0.1"), |
| ("storage_device", "1.2.1"), |
| ("task_event", "1.0.3"), |
| ("telemetry", "1.0.0"), |
| ("update", "1.0.2"), |
| ) |
| |
| parser = argparse.ArgumentParser() |
| parser.add_argument( |
| "--registries", |
| type=str, |
| default="privilege,openbmc," |
| + ",".join([dmtf[0] for dmtf in dmtf_registries]), |
| help="Comma delimited list of registries to update", |
| ) |
| |
| args = parser.parse_args() |
| |
| registries = set(args.registries.split(",")) |
| files = [] |
| |
| for registry, version in dmtf_registries: |
| if registry in registries: |
| registry_pascal_case = to_pascal_case(registry) |
| files.append( |
| make_getter( |
| f"{registry_pascal_case}.{version}.json", |
| f"{registry}_message_registry.hpp", |
| registry, |
| ) |
| ) |
| if "openbmc" in registries: |
| files.append(openbmc_local_getter()) |
| |
| update_registries(files) |
| |
| create_error_registry(files[0]) |
| |
| if "privilege" in registries: |
| make_privilege_registry() |
| |
| |
| if __name__ == "__main__": |
| main() |