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