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