Nan Zhou | 313c1b7 | 2022-03-25 11:47:55 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 2 | import json |
Nan Zhou | 6eaf0bd | 2022-08-07 01:18:45 +0000 | [diff] [blame] | 3 | import os |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 4 | |
Nan Zhou | 6eaf0bd | 2022-08-07 01:18:45 +0000 | [diff] [blame] | 5 | import requests |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 6 | |
Ed Tanous | 1cf53df | 2022-02-17 09:09:25 -0800 | [diff] [blame] | 7 | WARNING = '''/**************************************************************** |
| 8 | * READ THIS WARNING FIRST |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 9 | * This is an auto-generated header which contains definitions |
| 10 | * for Redfish DMTF defined messages. |
Ed Tanous | 1cf53df | 2022-02-17 09:09:25 -0800 | [diff] [blame] | 11 | * DO NOT modify this registry outside of running the |
Jason M. Bills | 0e2d069 | 2022-04-01 13:59:05 -0700 | [diff] [blame] | 12 | * parse_registries.py script. The definitions contained within |
Ed Tanous | 1cf53df | 2022-02-17 09:09:25 -0800 | [diff] [blame] | 13 | * this file are owned by DMTF. Any modifications to these files |
| 14 | * should be first pushed to the relevant registry in the DMTF |
| 15 | * github organization. |
| 16 | ***************************************************************/''' |
| 17 | |
| 18 | REGISTRY_HEADER = WARNING + ''' |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 19 | #pragma once |
| 20 | #include <registries.hpp> |
| 21 | |
Ed Tanous | 4d99bbb | 2022-02-17 10:02:57 -0800 | [diff] [blame] | 22 | // clang-format off |
| 23 | |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 24 | namespace redfish::registries::{} |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 25 | {{ |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 26 | ''' |
| 27 | |
| 28 | SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__)) |
| 29 | |
Jason M. Bills | ac70637 | 2020-02-18 11:36:47 -0800 | [diff] [blame] | 30 | include_path = os.path.realpath( |
| 31 | os.path.join( |
| 32 | SCRIPT_DIR, |
| 33 | "..", |
| 34 | "redfish-core", |
| 35 | "include", |
| 36 | "registries")) |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 37 | |
| 38 | proxies = { |
| 39 | 'https': os.environ.get("https_proxy", None) |
| 40 | } |
| 41 | |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 42 | |
James Feist | e51c710 | 2020-03-17 10:38:18 -0700 | [diff] [blame] | 43 | def make_getter(dmtf_name, header_name, type_name): |
| 44 | url = 'https://redfish.dmtf.org/registries/{}'.format(dmtf_name) |
| 45 | dmtf = requests.get(url, proxies=proxies) |
| 46 | dmtf.raise_for_status() |
| 47 | json_file = json.loads(dmtf.text) |
| 48 | path = os.path.join(include_path, header_name) |
| 49 | return (path, json_file, type_name, url) |
| 50 | |
| 51 | |
| 52 | files = [] |
Jiaqing Zhao | f866686 | 2022-06-24 15:01:03 +0800 | [diff] [blame] | 53 | files.append(make_getter('Base.1.13.0.json', |
Ed Tanous | f395daa | 2021-08-02 08:56:24 -0700 | [diff] [blame] | 54 | 'base_message_registry.hpp', |
| 55 | 'base')) |
Ed Tanous | fc8a2b8 | 2021-06-09 16:29:31 -0700 | [diff] [blame] | 56 | files.append(make_getter('TaskEvent.1.0.3.json', |
Ed Tanous | f395daa | 2021-08-02 08:56:24 -0700 | [diff] [blame] | 57 | 'task_event_message_registry.hpp', |
| 58 | 'task_event')) |
Sunitha Harish | 74eec26 | 2020-06-25 10:00:01 -0500 | [diff] [blame] | 59 | files.append(make_getter('ResourceEvent.1.0.3.json', |
Ed Tanous | f395daa | 2021-08-02 08:56:24 -0700 | [diff] [blame] | 60 | 'resource_event_message_registry.hpp', |
| 61 | 'resource_event')) |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 62 | |
| 63 | # Remove the old files |
Ed Tanous | 413d876 | 2021-06-09 16:24:19 -0700 | [diff] [blame] | 64 | for file, json_dict, namespace, url in files: |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 65 | try: |
| 66 | os.remove(file) |
Jason M. Bills | ac70637 | 2020-02-18 11:36:47 -0800 | [diff] [blame] | 67 | except BaseException: |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 68 | print("{} not found".format(file)) |
| 69 | |
| 70 | with open(file, 'w') as registry: |
| 71 | registry.write(REGISTRY_HEADER.format(namespace)) |
Jason M. Bills | 351d306 | 2019-03-27 12:58:21 -0700 | [diff] [blame] | 72 | # Parse the Registry header info |
Ed Tanous | 5b9ef70 | 2022-02-17 12:19:32 -0800 | [diff] [blame] | 73 | registry.write( |
| 74 | "const Header header = {{\n" |
| 75 | " \"{json_dict[@Redfish.Copyright]}\",\n" |
| 76 | " \"{json_dict[@odata.type]}\",\n" |
| 77 | " \"{json_dict[Id]}\",\n" |
| 78 | " \"{json_dict[Name]}\",\n" |
| 79 | " \"{json_dict[Language]}\",\n" |
| 80 | " \"{json_dict[Description]}\",\n" |
| 81 | " \"{json_dict[RegistryPrefix]}\",\n" |
| 82 | " \"{json_dict[RegistryVersion]}\",\n" |
| 83 | " \"{json_dict[OwningEntity]}\",\n" |
| 84 | "}};\n" |
| 85 | "constexpr const char* url =\n" |
| 86 | " \"{url}\";\n" |
| 87 | "\n" |
Jason M. Bills | 0e2d069 | 2022-04-01 13:59:05 -0700 | [diff] [blame] | 88 | "constexpr std::array registry =\n" |
Ed Tanous | 5b9ef70 | 2022-02-17 12:19:32 -0800 | [diff] [blame] | 89 | "{{\n".format( |
| 90 | json_dict=json_dict, |
| 91 | url=url, |
Ed Tanous | 5b9ef70 | 2022-02-17 12:19:32 -0800 | [diff] [blame] | 92 | )) |
Jason M. Bills | 351d306 | 2019-03-27 12:58:21 -0700 | [diff] [blame] | 93 | |
Ed Tanous | 30a3c43 | 2022-02-07 09:37:21 -0800 | [diff] [blame] | 94 | messages_sorted = sorted(json_dict["Messages"].items()) |
| 95 | for messageId, message in messages_sorted: |
Ed Tanous | 5b9ef70 | 2022-02-17 12:19:32 -0800 | [diff] [blame] | 96 | registry.write( |
| 97 | " MessageEntry{{\n" |
| 98 | " \"{messageId}\",\n" |
| 99 | " {{\n" |
| 100 | " \"{message[Description]}\",\n" |
| 101 | " \"{message[Message]}\",\n" |
Ed Tanous | 5b9ef70 | 2022-02-17 12:19:32 -0800 | [diff] [blame] | 102 | " \"{message[MessageSeverity]}\",\n" |
| 103 | " {message[NumberOfArgs]},\n" |
| 104 | " {{".format( |
| 105 | messageId=messageId, |
| 106 | message=message |
| 107 | )) |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 108 | paramTypes = message.get("ParamTypes") |
| 109 | if paramTypes: |
| 110 | for paramType in paramTypes: |
Ed Tanous | 4d99bbb | 2022-02-17 10:02:57 -0800 | [diff] [blame] | 111 | registry.write( |
Ed Tanous | 5b9ef70 | 2022-02-17 12:19:32 -0800 | [diff] [blame] | 112 | "\n" |
| 113 | " \"{}\",".format(paramType) |
| 114 | ) |
| 115 | registry.write("\n },\n") |
| 116 | else: |
| 117 | registry.write("},\n") |
| 118 | registry.write( |
| 119 | " \"{message[Resolution]}\",\n" |
| 120 | " }}}},\n".format(message=message)) |
Ed Tanous | 30a3c43 | 2022-02-07 09:37:21 -0800 | [diff] [blame] | 121 | |
Ed Tanous | 5b9ef70 | 2022-02-17 12:19:32 -0800 | [diff] [blame] | 122 | registry.write( |
| 123 | "\n};\n" |
| 124 | "\n" |
| 125 | "enum class Index\n" |
| 126 | "{\n" |
| 127 | ) |
Ed Tanous | 30a3c43 | 2022-02-07 09:37:21 -0800 | [diff] [blame] | 128 | for index, (messageId, message) in enumerate(messages_sorted): |
| 129 | messageId = messageId[0].lower() + messageId[1:] |
| 130 | registry.write( |
Ed Tanous | 4d99bbb | 2022-02-17 10:02:57 -0800 | [diff] [blame] | 131 | " {} = {},\n".format(messageId, index)) |
Ed Tanous | 4d99bbb | 2022-02-17 10:02:57 -0800 | [diff] [blame] | 132 | registry.write( |
Ed Tanous | 5b9ef70 | 2022-02-17 12:19:32 -0800 | [diff] [blame] | 133 | "}};\n" |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 134 | "}} // namespace redfish::registries::{}\n" |
Ed Tanous | 5b9ef70 | 2022-02-17 12:19:32 -0800 | [diff] [blame] | 135 | .format(namespace)) |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 136 | |
| 137 | |
| 138 | def get_privilege_string_from_list(privilege_list): |
| 139 | privilege_string = "{{\n" |
| 140 | for privilege_json in privilege_list: |
| 141 | privileges = privilege_json["Privilege"] |
| 142 | privilege_string += " {" |
| 143 | for privilege in privileges: |
| 144 | if privilege == "NoAuth": |
| 145 | continue |
| 146 | privilege_string += "\"" |
| 147 | privilege_string += privilege |
| 148 | privilege_string += "\",\n" |
| 149 | if privilege != "NoAuth": |
| 150 | privilege_string = privilege_string[:-2] |
| 151 | privilege_string += "}" |
| 152 | privilege_string += ",\n" |
| 153 | privilege_string = privilege_string[:-2] |
| 154 | privilege_string += "\n}}" |
| 155 | return privilege_string |
| 156 | |
Ed Tanous | f395daa | 2021-08-02 08:56:24 -0700 | [diff] [blame] | 157 | |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 158 | def get_variable_name_for_privilege_set(privilege_list): |
| 159 | names = [] |
| 160 | for privilege_json in privilege_list: |
| 161 | privileges = privilege_json["Privilege"] |
| 162 | names.append("And".join(privileges)) |
| 163 | return "Or".join(names) |
| 164 | |
Ed Tanous | f395daa | 2021-08-02 08:56:24 -0700 | [diff] [blame] | 165 | |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 166 | def make_privilege_registry(): |
Ed Tanous | f395daa | 2021-08-02 08:56:24 -0700 | [diff] [blame] | 167 | path, json_file, type_name, url = \ |
Sui Chen | 8bb5fbc | 2022-06-21 09:48:57 -0700 | [diff] [blame] | 168 | make_getter('Redfish_1.3.0_PrivilegeRegistry.json', |
Ed Tanous | f395daa | 2021-08-02 08:56:24 -0700 | [diff] [blame] | 169 | 'privilege_registry.hpp', 'privilege') |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 170 | with open(path, 'w') as registry: |
Ed Tanous | 5b9ef70 | 2022-02-17 12:19:32 -0800 | [diff] [blame] | 171 | registry.write( |
| 172 | "#pragma once\n" |
| 173 | "{WARNING}\n" |
| 174 | "// clang-format off\n" |
| 175 | "\n" |
| 176 | "#include <privileges.hpp>\n" |
| 177 | "\n" |
| 178 | "namespace redfish::privileges\n" |
| 179 | "{{\n" |
| 180 | .format( |
| 181 | WARNING=WARNING, |
| 182 | filename=os.path.basename(path))) |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 183 | |
| 184 | privilege_dict = {} |
| 185 | for mapping in json_file["Mappings"]: |
| 186 | # first pass, identify all the unique privilege sets |
| 187 | for operation, privilege_list in mapping["OperationMap"].items(): |
| 188 | privilege_dict[get_privilege_string_from_list( |
| 189 | privilege_list)] = (privilege_list, ) |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 190 | for index, key in enumerate(privilege_dict): |
| 191 | (privilege_list, ) = privilege_dict[key] |
| 192 | name = get_variable_name_for_privilege_set(privilege_list) |
Ed Tanous | f395daa | 2021-08-02 08:56:24 -0700 | [diff] [blame] | 193 | registry.write( |
Ed Tanous | 5b9ef70 | 2022-02-17 12:19:32 -0800 | [diff] [blame] | 194 | "const std::array<Privileges, {length}> " |
| 195 | "privilegeSet{name} = {key};\n" |
| 196 | .format(length=len(privilege_list), name=name, key=key) |
| 197 | ) |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 198 | privilege_dict[key] = (privilege_list, name) |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 199 | |
| 200 | for mapping in json_file["Mappings"]: |
| 201 | entity = mapping["Entity"] |
Ed Tanous | 4d99bbb | 2022-02-17 10:02:57 -0800 | [diff] [blame] | 202 | registry.write("// {}\n".format(entity)) |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 203 | for operation, privilege_list in mapping["OperationMap"].items(): |
| 204 | privilege_string = get_privilege_string_from_list( |
| 205 | privilege_list) |
| 206 | operation = operation.lower() |
| 207 | |
Ed Tanous | f395daa | 2021-08-02 08:56:24 -0700 | [diff] [blame] | 208 | registry.write( |
| 209 | "const static auto& {}{} = privilegeSet{};\n".format( |
| 210 | operation, |
| 211 | entity, |
| 212 | privilege_dict[privilege_string][1])) |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 213 | registry.write("\n") |
Ed Tanous | 5b9ef70 | 2022-02-17 12:19:32 -0800 | [diff] [blame] | 214 | registry.write( |
| 215 | "} // namespace redfish::privileges\n" |
| 216 | "// clang-format on\n") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 217 | |
| 218 | |
| 219 | make_privilege_registry() |