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