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