blob: 604276ab3c3e9c03e1df70697e558b85f5ecf33e [file] [log] [blame]
Jason M. Bills70304cb2019-03-27 12:03:59 -07001#!/usr/bin/python3
2import requests
3import zipfile
4from io import BytesIO
5import os
6from collections import defaultdict
7from collections import OrderedDict
8from distutils.version import StrictVersion
9import shutil
10import json
11import glob
12import subprocess
13
14import xml.etree.ElementTree as ET
15
Ed Tanous1cf53df2022-02-17 09:09:25 -080016WARNING = '''/****************************************************************
17 * READ THIS WARNING FIRST
Jason M. Bills70304cb2019-03-27 12:03:59 -070018 * This is an auto-generated header which contains definitions
19 * for Redfish DMTF defined messages.
Ed Tanous1cf53df2022-02-17 09:09:25 -080020 * 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
27REGISTRY_HEADER = WARNING + '''
Jason M. Bills70304cb2019-03-27 12:03:59 -070028#pragma once
29#include <registries.hpp>
30
Ed Tanous4d99bbb2022-02-17 10:02:57 -080031// clang-format off
32
Jason M. Bills70304cb2019-03-27 12:03:59 -070033namespace redfish::message_registries::{}
34{{
Jason M. Bills70304cb2019-03-27 12:03:59 -070035'''
36
37SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
38
Jason M. Billsac706372020-02-18 11:36:47 -080039include_path = os.path.realpath(
40 os.path.join(
41 SCRIPT_DIR,
42 "..",
43 "redfish-core",
44 "include",
45 "registries"))
Jason M. Bills70304cb2019-03-27 12:03:59 -070046
47proxies = {
48 'https': os.environ.get("https_proxy", None)
49}
50
Jason M. Bills70304cb2019-03-27 12:03:59 -070051
James Feiste51c7102020-03-17 10:38:18 -070052def 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
61files = []
Sunitha Harish83f984b2021-09-23 04:29:46 -050062files.append(make_getter('Base.1.11.0.json',
Ed Tanousf395daa2021-08-02 08:56:24 -070063 'base_message_registry.hpp',
64 'base'))
Ed Tanousfc8a2b82021-06-09 16:29:31 -070065files.append(make_getter('TaskEvent.1.0.3.json',
Ed Tanousf395daa2021-08-02 08:56:24 -070066 'task_event_message_registry.hpp',
67 'task_event'))
Sunitha Harish74eec262020-06-25 10:00:01 -050068files.append(make_getter('ResourceEvent.1.0.3.json',
Ed Tanousf395daa2021-08-02 08:56:24 -070069 'resource_event_message_registry.hpp',
70 'resource_event'))
Jason M. Bills70304cb2019-03-27 12:03:59 -070071
72# Remove the old files
Ed Tanous413d8762021-06-09 16:24:19 -070073for file, json_dict, namespace, url in files:
Jason M. Bills70304cb2019-03-27 12:03:59 -070074 try:
75 os.remove(file)
Jason M. Billsac706372020-02-18 11:36:47 -080076 except BaseException:
Jason M. Bills70304cb2019-03-27 12:03:59 -070077 print("{} not found".format(file))
78
79 with open(file, 'w') as registry:
80 registry.write(REGISTRY_HEADER.format(namespace))
Jason M. Bills351d3062019-03-27 12:58:21 -070081 # Parse the Registry header info
Ed Tanous4d99bbb2022-02-17 10:02:57 -080082 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. Bills351d3062019-03-27 12:58:21 -070093
Ed Tanous4d99bbb2022-02-17 10:02:57 -080094 registry.write(
95 'constexpr const char* url =\n "{}";\n\n'.format(url))
Jason M. Bills351d3062019-03-27 12:58:21 -070096 # Parse each Message entry
Ed Tanousf395daa2021-08-02 08:56:24 -070097 registry.write(
Ed Tanous4d99bbb2022-02-17 10:02:57 -080098 "constexpr std::array<MessageEntry, {}> registry =\n".format(
Ed Tanousf395daa2021-08-02 08:56:24 -070099 len(json_dict["Messages"])))
Ed Tanous4d99bbb2022-02-17 10:02:57 -0800100 registry.write("{\n")
Ed Tanous30a3c432022-02-07 09:37:21 -0800101 messages_sorted = sorted(json_dict["Messages"].items())
102 for messageId, message in messages_sorted:
Ed Tanous4d99bbb2022-02-17 10:02:57 -0800103 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. Bills70304cb2019-03-27 12:03:59 -0700114 paramTypes = message.get("ParamTypes")
115 if paramTypes:
116 for paramType in paramTypes:
Ed Tanous4d99bbb2022-02-17 10:02:57 -0800117 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 Tanous30a3c432022-02-07 09:37:21 -0800124 registry.write("\n};")
125
Ed Tanous4d99bbb2022-02-17 10:02:57 -0800126 registry.write("\n\nenum class Index\n{\n")
Ed Tanous30a3c432022-02-07 09:37:21 -0800127 for index, (messageId, message) in enumerate(messages_sorted):
128 messageId = messageId[0].lower() + messageId[1:]
129 registry.write(
Ed Tanous4d99bbb2022-02-17 10:02:57 -0800130 " {} = {},\n".format(messageId, index))
131 registry.write("};\n")
132 registry.write(
133 "} // namespace redfish::message_registries::")
134 registry.write("{}\n".format(namespace))
Ed Tanoused398212021-06-09 17:05:54 -0700135
136
137def 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 Tanousf395daa2021-08-02 08:56:24 -0700156
Ed Tanoused398212021-06-09 17:05:54 -0700157def 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 Tanousf395daa2021-08-02 08:56:24 -0700164
Ed Tanoused398212021-06-09 17:05:54 -0700165def make_privilege_registry():
Ed Tanousf395daa2021-08-02 08:56:24 -0700166 path, json_file, type_name, url = \
Shantappa Teekappanavar12778e62022-01-05 12:00:56 -0600167 make_getter('Redfish_1.2.0_PrivilegeRegistry.json',
Ed Tanousf395daa2021-08-02 08:56:24 -0700168 'privilege_registry.hpp', 'privilege')
Ed Tanoused398212021-06-09 17:05:54 -0700169 with open(path, 'w') as registry:
170 registry.write("#pragma once\n")
Ed Tanous1cf53df2022-02-17 09:09:25 -0800171 registry.write(WARNING)
Ed Tanoused398212021-06-09 17:05:54 -0700172
Ed Tanous1cf53df2022-02-17 09:09:25 -0800173 registry.write("\n// clang-format off\n")
Ed Tanous4d99bbb2022-02-17 10:02:57 -0800174 registry.write("\n#include <privileges.hpp>\n\n")
175 registry.write("namespace redfish::privileges\n")
176 registry.write("{\n")
Ed Tanoused398212021-06-09 17:05:54 -0700177
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 Tanoused398212021-06-09 17:05:54 -0700184 for index, key in enumerate(privilege_dict):
185 (privilege_list, ) = privilege_dict[key]
186 name = get_variable_name_for_privilege_set(privilege_list)
Ed Tanousf395daa2021-08-02 08:56:24 -0700187 registry.write(
188 "const std::array<Privileges, {}> ".format(
189 len(privilege_list)))
190 registry.write(
191 "privilegeSet{} = {};\n".format(name, key))
Ed Tanoused398212021-06-09 17:05:54 -0700192 privilege_dict[key] = (privilege_list, name)
Ed Tanoused398212021-06-09 17:05:54 -0700193
194 for mapping in json_file["Mappings"]:
195 entity = mapping["Entity"]
Ed Tanous4d99bbb2022-02-17 10:02:57 -0800196 registry.write("// {}\n".format(entity))
Ed Tanoused398212021-06-09 17:05:54 -0700197 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 Tanousf395daa2021-08-02 08:56:24 -0700202 registry.write(
203 "const static auto& {}{} = privilegeSet{};\n".format(
204 operation,
205 entity,
206 privilege_dict[privilege_string][1]))
Ed Tanoused398212021-06-09 17:05:54 -0700207 registry.write("\n")
208 registry.write("} // namespace redfish::privileges\n")
Ed Tanous4d99bbb2022-02-17 10:02:57 -0800209 registry.write("// clang-format on\n")
Ed Tanoused398212021-06-09 17:05:54 -0700210
211
212make_privilege_registry()