blob: 8334eaefd94baccf09624a1b93abb98227c17bcb [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 Tanousfe4b3092021-06-14 12:45:49 -070016REGISTRY_HEADER = '''
Jason M. Bills70304cb2019-03-27 12:03:59 -070017/****************************************************************
18 * This is an auto-generated header which contains definitions
19 * for Redfish DMTF defined messages.
20 ***************************************************************/
21#pragma once
22#include <registries.hpp>
23
24namespace redfish::message_registries::{}
25{{
Jason M. Bills70304cb2019-03-27 12:03:59 -070026'''
27
28SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
29
Jason M. Billsac706372020-02-18 11:36:47 -080030include_path = os.path.realpath(
31 os.path.join(
32 SCRIPT_DIR,
33 "..",
34 "redfish-core",
35 "include",
36 "registries"))
Jason M. Bills70304cb2019-03-27 12:03:59 -070037
38proxies = {
39 'https': os.environ.get("https_proxy", None)
40}
41
Jason M. Bills70304cb2019-03-27 12:03:59 -070042
James Feiste51c7102020-03-17 10:38:18 -070043def make_getter(dmtf_name, header_name, type_name):
44 url = 'https://redfish.dmtf.org/registries/{}'.format(dmtf_name)
45 dmtf = requests.get(url, proxies=proxies)
46 dmtf.raise_for_status()
47 json_file = json.loads(dmtf.text)
48 path = os.path.join(include_path, header_name)
49 return (path, json_file, type_name, url)
50
51
Ed Tanoused398212021-06-09 17:05:54 -070052def clang_format(filename):
Ed Tanous01caf622022-02-08 12:28:37 -080053 subprocess.check_call(["clang-format", "-i", filename])
Ed Tanoused398212021-06-09 17:05:54 -070054
55
James Feiste51c7102020-03-17 10:38:18 -070056files = []
Sunitha Harish83f984b2021-09-23 04:29:46 -050057files.append(make_getter('Base.1.11.0.json',
Ed Tanousf395daa2021-08-02 08:56:24 -070058 'base_message_registry.hpp',
59 'base'))
Ed Tanousfc8a2b82021-06-09 16:29:31 -070060files.append(make_getter('TaskEvent.1.0.3.json',
Ed Tanousf395daa2021-08-02 08:56:24 -070061 'task_event_message_registry.hpp',
62 'task_event'))
Sunitha Harish74eec262020-06-25 10:00:01 -050063files.append(make_getter('ResourceEvent.1.0.3.json',
Ed Tanousf395daa2021-08-02 08:56:24 -070064 'resource_event_message_registry.hpp',
65 'resource_event'))
Jason M. Bills70304cb2019-03-27 12:03:59 -070066
67# Remove the old files
Ed Tanous413d8762021-06-09 16:24:19 -070068for file, json_dict, namespace, url in files:
Jason M. Bills70304cb2019-03-27 12:03:59 -070069 try:
70 os.remove(file)
Jason M. Billsac706372020-02-18 11:36:47 -080071 except BaseException:
Jason M. Bills70304cb2019-03-27 12:03:59 -070072 print("{} not found".format(file))
73
74 with open(file, 'w') as registry:
75 registry.write(REGISTRY_HEADER.format(namespace))
Jason M. Bills351d3062019-03-27 12:58:21 -070076 # Parse the Registry header info
77 registry.write("const Header header = {")
Ed Tanous413d8762021-06-09 16:24:19 -070078 registry.write("\"{}\",".format(json_dict["@Redfish.Copyright"]))
79 registry.write("\"{}\",".format(json_dict["@odata.type"]))
80 registry.write("\"{}\",".format(json_dict["Id"]))
81 registry.write("\"{}\",".format(json_dict["Name"]))
82 registry.write("\"{}\",".format(json_dict["Language"]))
83 registry.write("\"{}\",".format(json_dict["Description"]))
84 registry.write("\"{}\",".format(json_dict["RegistryPrefix"]))
85 registry.write("\"{}\",".format(json_dict["RegistryVersion"]))
86 registry.write("\"{}\",".format(json_dict["OwningEntity"]))
Jason M. Bills351d3062019-03-27 12:58:21 -070087 registry.write("};")
88
James Feiste51c7102020-03-17 10:38:18 -070089 registry.write('constexpr const char * url = "{}";\n\n'.format(url))
Jason M. Bills351d3062019-03-27 12:58:21 -070090 # Parse each Message entry
Ed Tanousf395daa2021-08-02 08:56:24 -070091 registry.write(
92 "constexpr std::array<MessageEntry, {}> registry = {{".format(
93 len(json_dict["Messages"])))
Ed Tanous30a3c432022-02-07 09:37:21 -080094
95 messages_sorted = sorted(json_dict["Messages"].items())
96 for messageId, message in messages_sorted:
Jason M. Bills70304cb2019-03-27 12:03:59 -070097 registry.write("MessageEntry{")
98 registry.write("\"{}\",".format(messageId))
99 registry.write("{")
Ed Tanous271584a2019-07-09 16:24:22 -0700100 registry.write("\"{}\",".format(message["Description"]))
101 registry.write("\"{}\",".format(message["Message"]))
102 registry.write("\"{}\",".format(message["Severity"]))
Gunnar Millse7808c92020-07-08 21:17:44 -0500103 registry.write("\"{}\",".format(message["MessageSeverity"]))
Ed Tanous271584a2019-07-09 16:24:22 -0700104 registry.write("{},".format(message["NumberOfArgs"]))
105 registry.write("{")
Jason M. Bills70304cb2019-03-27 12:03:59 -0700106 paramTypes = message.get("ParamTypes")
107 if paramTypes:
108 for paramType in paramTypes:
109 registry.write("\"{}\",".format(paramType))
110 registry.write("},")
Ed Tanous271584a2019-07-09 16:24:22 -0700111 registry.write("\"{}\",".format(message["Resolution"]))
Jason M. Bills70304cb2019-03-27 12:03:59 -0700112 registry.write("}},")
Ed Tanous30a3c432022-02-07 09:37:21 -0800113
114 registry.write("\n};")
115
116 registry.write("\n\nenum class Index {\n")
117 for index, (messageId, message) in enumerate(messages_sorted):
118 messageId = messageId[0].lower() + messageId[1:]
119 registry.write(
120 "{} = {},\n".format(messageId, index))
121 registry.write("};")
122 registry.write("}")
123
Ed Tanoused398212021-06-09 17:05:54 -0700124 clang_format(file)
125
126
127def 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
135 privilege_string += "\""
136 privilege_string += privilege
137 privilege_string += "\",\n"
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 Tanousf395daa2021-08-02 08:56:24 -0700146
Ed Tanoused398212021-06-09 17:05:54 -0700147def 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 Tanousf395daa2021-08-02 08:56:24 -0700154
Ed Tanoused398212021-06-09 17:05:54 -0700155def make_privilege_registry():
Ed Tanousf395daa2021-08-02 08:56:24 -0700156 path, json_file, type_name, url = \
Shantappa Teekappanavar12778e62022-01-05 12:00:56 -0600157 make_getter('Redfish_1.2.0_PrivilegeRegistry.json',
Ed Tanousf395daa2021-08-02 08:56:24 -0700158 'privilege_registry.hpp', 'privilege')
Ed Tanoused398212021-06-09 17:05:54 -0700159 with open(path, 'w') as registry:
160 registry.write("#pragma once\n")
Ed Tanousf395daa2021-08-02 08:56:24 -0700161 registry.write(
162 "//{} is generated. Do not edit directly\n".format(
163 os.path.basename(path)))
Ed Tanoused398212021-06-09 17:05:54 -0700164
165 registry.write("#include <privileges.hpp>\n\n")
166 registry.write("namespace redfish::privileges{\n")
167
168 privilege_dict = {}
169 for mapping in json_file["Mappings"]:
170 # first pass, identify all the unique privilege sets
171 for operation, privilege_list in mapping["OperationMap"].items():
172 privilege_dict[get_privilege_string_from_list(
173 privilege_list)] = (privilege_list, )
174 registry.write("// clang-format off\n")
175 for index, key in enumerate(privilege_dict):
176 (privilege_list, ) = privilege_dict[key]
177 name = get_variable_name_for_privilege_set(privilege_list)
Ed Tanousf395daa2021-08-02 08:56:24 -0700178 registry.write(
179 "const std::array<Privileges, {}> ".format(
180 len(privilege_list)))
181 registry.write(
182 "privilegeSet{} = {};\n".format(name, key))
Ed Tanoused398212021-06-09 17:05:54 -0700183 privilege_dict[key] = (privilege_list, name)
184 registry.write("// clang-format on\n\n")
185
186 for mapping in json_file["Mappings"]:
187 entity = mapping["Entity"]
188 registry.write("//{}\n".format(entity))
189 for operation, privilege_list in mapping["OperationMap"].items():
190 privilege_string = get_privilege_string_from_list(
191 privilege_list)
192 operation = operation.lower()
193
Ed Tanousf395daa2021-08-02 08:56:24 -0700194 registry.write(
195 "const static auto& {}{} = privilegeSet{};\n".format(
196 operation,
197 entity,
198 privilege_dict[privilege_string][1]))
Ed Tanoused398212021-06-09 17:05:54 -0700199 registry.write("\n")
200 registry.write("} // namespace redfish::privileges\n")
201 clang_format(path)
202
203
204make_privilege_registry()