blob: 3d66a0353cdda48cdc4ab7305cc77c47d1ea4652 [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 Tanous4f48d5f2021-06-21 08:27:45 -070053 subprocess.check_call(["clang-format-12", "-i", filename])
Ed Tanoused398212021-06-09 17:05:54 -070054
55
James Feiste51c7102020-03-17 10:38:18 -070056files = []
Ed Tanousfc8a2b82021-06-09 16:29:31 -070057files.append(make_getter('Base.1.10.0.json',
James Feiste51c7102020-03-17 10:38:18 -070058 'base_message_registry.hpp', 'base'))
Ed Tanousfc8a2b82021-06-09 16:29:31 -070059files.append(make_getter('TaskEvent.1.0.3.json',
James Feiste51c7102020-03-17 10:38:18 -070060 'task_event_message_registry.hpp', 'task_event'))
Sunitha Harish74eec262020-06-25 10:00:01 -050061files.append(make_getter('ResourceEvent.1.0.3.json',
62 'resource_event_message_registry.hpp', 'resource_event'))
Jason M. Bills70304cb2019-03-27 12:03:59 -070063
64# Remove the old files
Ed Tanous413d8762021-06-09 16:24:19 -070065for file, json_dict, namespace, url in files:
Jason M. Bills70304cb2019-03-27 12:03:59 -070066 try:
67 os.remove(file)
Jason M. Billsac706372020-02-18 11:36:47 -080068 except BaseException:
Jason M. Bills70304cb2019-03-27 12:03:59 -070069 print("{} not found".format(file))
70
71 with open(file, 'w') as registry:
72 registry.write(REGISTRY_HEADER.format(namespace))
Jason M. Bills351d3062019-03-27 12:58:21 -070073 # Parse the Registry header info
74 registry.write("const Header header = {")
Ed Tanous413d8762021-06-09 16:24:19 -070075 registry.write("\"{}\",".format(json_dict["@Redfish.Copyright"]))
76 registry.write("\"{}\",".format(json_dict["@odata.type"]))
77 registry.write("\"{}\",".format(json_dict["Id"]))
78 registry.write("\"{}\",".format(json_dict["Name"]))
79 registry.write("\"{}\",".format(json_dict["Language"]))
80 registry.write("\"{}\",".format(json_dict["Description"]))
81 registry.write("\"{}\",".format(json_dict["RegistryPrefix"]))
82 registry.write("\"{}\",".format(json_dict["RegistryVersion"]))
83 registry.write("\"{}\",".format(json_dict["OwningEntity"]))
Jason M. Bills351d3062019-03-27 12:58:21 -070084 registry.write("};")
85
James Feiste51c7102020-03-17 10:38:18 -070086 registry.write('constexpr const char * url = "{}";\n\n'.format(url))
Jason M. Bills351d3062019-03-27 12:58:21 -070087 # Parse each Message entry
Jason M. Billsac706372020-02-18 11:36:47 -080088 registry.write("constexpr std::array<MessageEntry, {}> registry = {{".format(
Ed Tanous413d8762021-06-09 16:24:19 -070089 len(json_dict["Messages"])))
90 for messageId, message in sorted(json_dict["Messages"].items()):
Jason M. Bills70304cb2019-03-27 12:03:59 -070091 registry.write("MessageEntry{")
92 registry.write("\"{}\",".format(messageId))
93 registry.write("{")
Ed Tanous271584a2019-07-09 16:24:22 -070094 registry.write("\"{}\",".format(message["Description"]))
95 registry.write("\"{}\",".format(message["Message"]))
96 registry.write("\"{}\",".format(message["Severity"]))
Gunnar Millse7808c92020-07-08 21:17:44 -050097 registry.write("\"{}\",".format(message["MessageSeverity"]))
Ed Tanous271584a2019-07-09 16:24:22 -070098 registry.write("{},".format(message["NumberOfArgs"]))
99 registry.write("{")
Jason M. Bills70304cb2019-03-27 12:03:59 -0700100 paramTypes = message.get("ParamTypes")
101 if paramTypes:
102 for paramType in paramTypes:
103 registry.write("\"{}\",".format(paramType))
104 registry.write("},")
Ed Tanous271584a2019-07-09 16:24:22 -0700105 registry.write("\"{}\",".format(message["Resolution"]))
Jason M. Bills70304cb2019-03-27 12:03:59 -0700106 registry.write("}},")
107 registry.write("};}\n")
Ed Tanoused398212021-06-09 17:05:54 -0700108 clang_format(file)
109
110
111def get_privilege_string_from_list(privilege_list):
112 privilege_string = "{{\n"
113 for privilege_json in privilege_list:
114 privileges = privilege_json["Privilege"]
115 privilege_string += " {"
116 for privilege in privileges:
117 if privilege == "NoAuth":
118 continue
119 privilege_string += "\""
120 privilege_string += privilege
121 privilege_string += "\",\n"
122 if privilege != "NoAuth":
123 privilege_string = privilege_string[:-2]
124 privilege_string += "}"
125 privilege_string += ",\n"
126 privilege_string = privilege_string[:-2]
127 privilege_string += "\n}}"
128 return privilege_string
129
130def get_variable_name_for_privilege_set(privilege_list):
131 names = []
132 for privilege_json in privilege_list:
133 privileges = privilege_json["Privilege"]
134 names.append("And".join(privileges))
135 return "Or".join(names)
136
137def make_privilege_registry():
138 path, json_file, type_name, url = make_getter('Redfish_1.1.0_PrivilegeRegistry.json',
139 'privilege_registry.hpp', 'privilege')
140 with open(path, 'w') as registry:
141 registry.write("#pragma once\n")
142 registry.write("//{} is generated. Do not edit directly\n".format(os.path.basename(path)))
143
144 registry.write("#include <privileges.hpp>\n\n")
145 registry.write("namespace redfish::privileges{\n")
146
147 privilege_dict = {}
148 for mapping in json_file["Mappings"]:
149 # first pass, identify all the unique privilege sets
150 for operation, privilege_list in mapping["OperationMap"].items():
151 privilege_dict[get_privilege_string_from_list(
152 privilege_list)] = (privilege_list, )
153 registry.write("// clang-format off\n")
154 for index, key in enumerate(privilege_dict):
155 (privilege_list, ) = privilege_dict[key]
156 name = get_variable_name_for_privilege_set(privilege_list)
157 registry.write("const std::array<Privileges, {}> privilegeSet{} = {};\n".format(
158 len(privilege_list), name, key))
159 privilege_dict[key] = (privilege_list, name)
160 registry.write("// clang-format on\n\n")
161
162 for mapping in json_file["Mappings"]:
163 entity = mapping["Entity"]
164 registry.write("//{}\n".format(entity))
165 for operation, privilege_list in mapping["OperationMap"].items():
166 privilege_string = get_privilege_string_from_list(
167 privilege_list)
168 operation = operation.lower()
169
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700170 registry.write("const static auto& {}{} = privilegeSet{};\n".format(
Ed Tanoused398212021-06-09 17:05:54 -0700171 operation, entity , privilege_dict[privilege_string][1]))
172 registry.write("\n")
173 registry.write("} // namespace redfish::privileges\n")
174 clang_format(path)
175
176
177make_privilege_registry()