Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 1 | #!/usr/bin/python3 |
| 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 |
| 21 | * parse_regisries.py script. The definitions contained within |
| 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 | |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 33 | namespace redfish::message_registries::{} |
| 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 | 4d99bbb | 2022-02-17 10:02:57 -0800 | [diff] [blame] | 82 | registry.write("const Header header = {\n") |
| 83 | registry.write(" \"{}\",\n".format(json_dict["@Redfish.Copyright"])) |
| 84 | registry.write(" \"{}\",\n".format(json_dict["@odata.type"])) |
| 85 | registry.write(" \"{}\",\n".format(json_dict["Id"])) |
| 86 | registry.write(" \"{}\",\n".format(json_dict["Name"])) |
| 87 | registry.write(" \"{}\",\n".format(json_dict["Language"])) |
| 88 | registry.write(" \"{}\",\n".format(json_dict["Description"])) |
| 89 | registry.write(" \"{}\",\n".format(json_dict["RegistryPrefix"])) |
| 90 | registry.write(" \"{}\",\n".format(json_dict["RegistryVersion"])) |
| 91 | registry.write(" \"{}\",\n".format(json_dict["OwningEntity"])) |
| 92 | registry.write("};\n") |
Jason M. Bills | 351d306 | 2019-03-27 12:58:21 -0700 | [diff] [blame] | 93 | |
Ed Tanous | 4d99bbb | 2022-02-17 10:02:57 -0800 | [diff] [blame] | 94 | registry.write( |
| 95 | 'constexpr const char* url =\n "{}";\n\n'.format(url)) |
Jason M. Bills | 351d306 | 2019-03-27 12:58:21 -0700 | [diff] [blame] | 96 | # Parse each Message entry |
Ed Tanous | f395daa | 2021-08-02 08:56:24 -0700 | [diff] [blame] | 97 | registry.write( |
Ed Tanous | 4d99bbb | 2022-02-17 10:02:57 -0800 | [diff] [blame] | 98 | "constexpr std::array<MessageEntry, {}> registry =\n".format( |
Ed Tanous | f395daa | 2021-08-02 08:56:24 -0700 | [diff] [blame] | 99 | len(json_dict["Messages"]))) |
Ed Tanous | 4d99bbb | 2022-02-17 10:02:57 -0800 | [diff] [blame] | 100 | registry.write("{\n") |
Ed Tanous | 30a3c43 | 2022-02-07 09:37:21 -0800 | [diff] [blame] | 101 | messages_sorted = sorted(json_dict["Messages"].items()) |
| 102 | for messageId, message in messages_sorted: |
Ed Tanous | 4d99bbb | 2022-02-17 10:02:57 -0800 | [diff] [blame] | 103 | registry.write(" MessageEntry{\n") |
| 104 | registry.write(" \"{}\",\n".format(messageId)) |
| 105 | registry.write(" {\n") |
| 106 | registry.write(" \"{}\",\n".format( |
| 107 | message["Description"])) |
| 108 | registry.write(" \"{}\",\n".format(message["Message"])) |
| 109 | registry.write(" \"{}\",\n".format(message["Severity"])) |
| 110 | registry.write(" \"{}\",\n".format( |
| 111 | message["MessageSeverity"])) |
| 112 | registry.write(" {},\n".format(message["NumberOfArgs"])) |
| 113 | registry.write(" {") |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 114 | paramTypes = message.get("ParamTypes") |
| 115 | if paramTypes: |
| 116 | for paramType in paramTypes: |
Ed Tanous | 4d99bbb | 2022-02-17 10:02:57 -0800 | [diff] [blame] | 117 | registry.write( |
| 118 | "\n \"{}\",".format(paramType)) |
| 119 | registry.write("\n ") |
| 120 | registry.write("},\n") |
| 121 | registry.write(" \"{}\",\n".format( |
| 122 | message["Resolution"])) |
| 123 | registry.write(" }},\n") |
Ed Tanous | 30a3c43 | 2022-02-07 09:37:21 -0800 | [diff] [blame] | 124 | registry.write("\n};") |
| 125 | |
Ed Tanous | 4d99bbb | 2022-02-17 10:02:57 -0800 | [diff] [blame] | 126 | registry.write("\n\nenum class Index\n{\n") |
Ed Tanous | 30a3c43 | 2022-02-07 09:37:21 -0800 | [diff] [blame] | 127 | for index, (messageId, message) in enumerate(messages_sorted): |
| 128 | messageId = messageId[0].lower() + messageId[1:] |
| 129 | registry.write( |
Ed Tanous | 4d99bbb | 2022-02-17 10:02:57 -0800 | [diff] [blame] | 130 | " {} = {},\n".format(messageId, index)) |
| 131 | registry.write("};\n") |
| 132 | registry.write( |
| 133 | "} // namespace redfish::message_registries::") |
| 134 | registry.write("{}\n".format(namespace)) |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 135 | |
| 136 | |
| 137 | def get_privilege_string_from_list(privilege_list): |
| 138 | privilege_string = "{{\n" |
| 139 | for privilege_json in privilege_list: |
| 140 | privileges = privilege_json["Privilege"] |
| 141 | privilege_string += " {" |
| 142 | for privilege in privileges: |
| 143 | if privilege == "NoAuth": |
| 144 | continue |
| 145 | privilege_string += "\"" |
| 146 | privilege_string += privilege |
| 147 | privilege_string += "\",\n" |
| 148 | if privilege != "NoAuth": |
| 149 | privilege_string = privilege_string[:-2] |
| 150 | privilege_string += "}" |
| 151 | privilege_string += ",\n" |
| 152 | privilege_string = privilege_string[:-2] |
| 153 | privilege_string += "\n}}" |
| 154 | return privilege_string |
| 155 | |
Ed Tanous | f395daa | 2021-08-02 08:56:24 -0700 | [diff] [blame] | 156 | |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 157 | def get_variable_name_for_privilege_set(privilege_list): |
| 158 | names = [] |
| 159 | for privilege_json in privilege_list: |
| 160 | privileges = privilege_json["Privilege"] |
| 161 | names.append("And".join(privileges)) |
| 162 | return "Or".join(names) |
| 163 | |
Ed Tanous | f395daa | 2021-08-02 08:56:24 -0700 | [diff] [blame] | 164 | |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 165 | def make_privilege_registry(): |
Ed Tanous | f395daa | 2021-08-02 08:56:24 -0700 | [diff] [blame] | 166 | path, json_file, type_name, url = \ |
Shantappa Teekappanavar | 12778e6 | 2022-01-05 12:00:56 -0600 | [diff] [blame] | 167 | make_getter('Redfish_1.2.0_PrivilegeRegistry.json', |
Ed Tanous | f395daa | 2021-08-02 08:56:24 -0700 | [diff] [blame] | 168 | 'privilege_registry.hpp', 'privilege') |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 169 | with open(path, 'w') as registry: |
| 170 | registry.write("#pragma once\n") |
Ed Tanous | 1cf53df | 2022-02-17 09:09:25 -0800 | [diff] [blame^] | 171 | registry.write(WARNING) |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 172 | |
Ed Tanous | 1cf53df | 2022-02-17 09:09:25 -0800 | [diff] [blame^] | 173 | registry.write("\n// clang-format off\n") |
Ed Tanous | 4d99bbb | 2022-02-17 10:02:57 -0800 | [diff] [blame] | 174 | registry.write("\n#include <privileges.hpp>\n\n") |
| 175 | registry.write("namespace redfish::privileges\n") |
| 176 | registry.write("{\n") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 177 | |
| 178 | privilege_dict = {} |
| 179 | for mapping in json_file["Mappings"]: |
| 180 | # first pass, identify all the unique privilege sets |
| 181 | for operation, privilege_list in mapping["OperationMap"].items(): |
| 182 | privilege_dict[get_privilege_string_from_list( |
| 183 | privilege_list)] = (privilege_list, ) |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 184 | for index, key in enumerate(privilege_dict): |
| 185 | (privilege_list, ) = privilege_dict[key] |
| 186 | name = get_variable_name_for_privilege_set(privilege_list) |
Ed Tanous | f395daa | 2021-08-02 08:56:24 -0700 | [diff] [blame] | 187 | registry.write( |
| 188 | "const std::array<Privileges, {}> ".format( |
| 189 | len(privilege_list))) |
| 190 | registry.write( |
| 191 | "privilegeSet{} = {};\n".format(name, key)) |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 192 | privilege_dict[key] = (privilege_list, name) |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 193 | |
| 194 | for mapping in json_file["Mappings"]: |
| 195 | entity = mapping["Entity"] |
Ed Tanous | 4d99bbb | 2022-02-17 10:02:57 -0800 | [diff] [blame] | 196 | registry.write("// {}\n".format(entity)) |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 197 | for operation, privilege_list in mapping["OperationMap"].items(): |
| 198 | privilege_string = get_privilege_string_from_list( |
| 199 | privilege_list) |
| 200 | operation = operation.lower() |
| 201 | |
Ed Tanous | f395daa | 2021-08-02 08:56:24 -0700 | [diff] [blame] | 202 | registry.write( |
| 203 | "const static auto& {}{} = privilegeSet{};\n".format( |
| 204 | operation, |
| 205 | entity, |
| 206 | privilege_dict[privilege_string][1])) |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 207 | registry.write("\n") |
| 208 | registry.write("} // namespace redfish::privileges\n") |
Ed Tanous | 4d99bbb | 2022-02-17 10:02:57 -0800 | [diff] [blame] | 209 | registry.write("// clang-format on\n") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 210 | |
| 211 | |
| 212 | make_privilege_registry() |