blob: 4973633a64391d98e6bd96ae2034f9ed9a6b89de [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
Ed Tanous4d99bbb2022-02-17 10:02:57 -080024// clang-format off
25
Jason M. Bills70304cb2019-03-27 12:03:59 -070026namespace redfish::message_registries::{}
27{{
Jason M. Bills70304cb2019-03-27 12:03:59 -070028'''
29
30SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
31
Jason M. Billsac706372020-02-18 11:36:47 -080032include_path = os.path.realpath(
33 os.path.join(
34 SCRIPT_DIR,
35 "..",
36 "redfish-core",
37 "include",
38 "registries"))
Jason M. Bills70304cb2019-03-27 12:03:59 -070039
40proxies = {
41 'https': os.environ.get("https_proxy", None)
42}
43
Jason M. Bills70304cb2019-03-27 12:03:59 -070044
James Feiste51c7102020-03-17 10:38:18 -070045def make_getter(dmtf_name, header_name, type_name):
46 url = 'https://redfish.dmtf.org/registries/{}'.format(dmtf_name)
47 dmtf = requests.get(url, proxies=proxies)
48 dmtf.raise_for_status()
49 json_file = json.loads(dmtf.text)
50 path = os.path.join(include_path, header_name)
51 return (path, json_file, type_name, url)
52
53
54files = []
Sunitha Harish83f984b2021-09-23 04:29:46 -050055files.append(make_getter('Base.1.11.0.json',
Ed Tanousf395daa2021-08-02 08:56:24 -070056 'base_message_registry.hpp',
57 'base'))
Ed Tanousfc8a2b82021-06-09 16:29:31 -070058files.append(make_getter('TaskEvent.1.0.3.json',
Ed Tanousf395daa2021-08-02 08:56:24 -070059 'task_event_message_registry.hpp',
60 'task_event'))
Sunitha Harish74eec262020-06-25 10:00:01 -050061files.append(make_getter('ResourceEvent.1.0.3.json',
Ed Tanousf395daa2021-08-02 08:56:24 -070062 'resource_event_message_registry.hpp',
63 'resource_event'))
Jason M. Bills70304cb2019-03-27 12:03:59 -070064
65# Remove the old files
Ed Tanous413d8762021-06-09 16:24:19 -070066for file, json_dict, namespace, url in files:
Jason M. Bills70304cb2019-03-27 12:03:59 -070067 try:
68 os.remove(file)
Jason M. Billsac706372020-02-18 11:36:47 -080069 except BaseException:
Jason M. Bills70304cb2019-03-27 12:03:59 -070070 print("{} not found".format(file))
71
72 with open(file, 'w') as registry:
73 registry.write(REGISTRY_HEADER.format(namespace))
Jason M. Bills351d3062019-03-27 12:58:21 -070074 # Parse the Registry header info
Ed Tanous4d99bbb2022-02-17 10:02:57 -080075 registry.write("const Header header = {\n")
76 registry.write(" \"{}\",\n".format(json_dict["@Redfish.Copyright"]))
77 registry.write(" \"{}\",\n".format(json_dict["@odata.type"]))
78 registry.write(" \"{}\",\n".format(json_dict["Id"]))
79 registry.write(" \"{}\",\n".format(json_dict["Name"]))
80 registry.write(" \"{}\",\n".format(json_dict["Language"]))
81 registry.write(" \"{}\",\n".format(json_dict["Description"]))
82 registry.write(" \"{}\",\n".format(json_dict["RegistryPrefix"]))
83 registry.write(" \"{}\",\n".format(json_dict["RegistryVersion"]))
84 registry.write(" \"{}\",\n".format(json_dict["OwningEntity"]))
85 registry.write("};\n")
Jason M. Bills351d3062019-03-27 12:58:21 -070086
Ed Tanous4d99bbb2022-02-17 10:02:57 -080087 registry.write(
88 'constexpr const char* url =\n "{}";\n\n'.format(url))
Jason M. Bills351d3062019-03-27 12:58:21 -070089 # Parse each Message entry
Ed Tanousf395daa2021-08-02 08:56:24 -070090 registry.write(
Ed Tanous4d99bbb2022-02-17 10:02:57 -080091 "constexpr std::array<MessageEntry, {}> registry =\n".format(
Ed Tanousf395daa2021-08-02 08:56:24 -070092 len(json_dict["Messages"])))
Ed Tanous4d99bbb2022-02-17 10:02:57 -080093 registry.write("{\n")
Ed Tanous30a3c432022-02-07 09:37:21 -080094 messages_sorted = sorted(json_dict["Messages"].items())
95 for messageId, message in messages_sorted:
Ed Tanous4d99bbb2022-02-17 10:02:57 -080096 registry.write(" MessageEntry{\n")
97 registry.write(" \"{}\",\n".format(messageId))
98 registry.write(" {\n")
99 registry.write(" \"{}\",\n".format(
100 message["Description"]))
101 registry.write(" \"{}\",\n".format(message["Message"]))
102 registry.write(" \"{}\",\n".format(message["Severity"]))
103 registry.write(" \"{}\",\n".format(
104 message["MessageSeverity"]))
105 registry.write(" {},\n".format(message["NumberOfArgs"]))
106 registry.write(" {")
Jason M. Bills70304cb2019-03-27 12:03:59 -0700107 paramTypes = message.get("ParamTypes")
108 if paramTypes:
109 for paramType in paramTypes:
Ed Tanous4d99bbb2022-02-17 10:02:57 -0800110 registry.write(
111 "\n \"{}\",".format(paramType))
112 registry.write("\n ")
113 registry.write("},\n")
114 registry.write(" \"{}\",\n".format(
115 message["Resolution"]))
116 registry.write(" }},\n")
Ed Tanous30a3c432022-02-07 09:37:21 -0800117 registry.write("\n};")
118
Ed Tanous4d99bbb2022-02-17 10:02:57 -0800119 registry.write("\n\nenum class Index\n{\n")
Ed Tanous30a3c432022-02-07 09:37:21 -0800120 for index, (messageId, message) in enumerate(messages_sorted):
121 messageId = messageId[0].lower() + messageId[1:]
122 registry.write(
Ed Tanous4d99bbb2022-02-17 10:02:57 -0800123 " {} = {},\n".format(messageId, index))
124 registry.write("};\n")
125 registry.write(
126 "} // namespace redfish::message_registries::")
127 registry.write("{}\n".format(namespace))
Ed Tanoused398212021-06-09 17:05:54 -0700128
129
130def get_privilege_string_from_list(privilege_list):
131 privilege_string = "{{\n"
132 for privilege_json in privilege_list:
133 privileges = privilege_json["Privilege"]
134 privilege_string += " {"
135 for privilege in privileges:
136 if privilege == "NoAuth":
137 continue
138 privilege_string += "\""
139 privilege_string += privilege
140 privilege_string += "\",\n"
141 if privilege != "NoAuth":
142 privilege_string = privilege_string[:-2]
143 privilege_string += "}"
144 privilege_string += ",\n"
145 privilege_string = privilege_string[:-2]
146 privilege_string += "\n}}"
147 return privilege_string
148
Ed Tanousf395daa2021-08-02 08:56:24 -0700149
Ed Tanoused398212021-06-09 17:05:54 -0700150def get_variable_name_for_privilege_set(privilege_list):
151 names = []
152 for privilege_json in privilege_list:
153 privileges = privilege_json["Privilege"]
154 names.append("And".join(privileges))
155 return "Or".join(names)
156
Ed Tanousf395daa2021-08-02 08:56:24 -0700157
Ed Tanoused398212021-06-09 17:05:54 -0700158def make_privilege_registry():
Ed Tanousf395daa2021-08-02 08:56:24 -0700159 path, json_file, type_name, url = \
Shantappa Teekappanavar12778e62022-01-05 12:00:56 -0600160 make_getter('Redfish_1.2.0_PrivilegeRegistry.json',
Ed Tanousf395daa2021-08-02 08:56:24 -0700161 'privilege_registry.hpp', 'privilege')
Ed Tanoused398212021-06-09 17:05:54 -0700162 with open(path, 'w') as registry:
163 registry.write("#pragma once\n")
Ed Tanousf395daa2021-08-02 08:56:24 -0700164 registry.write(
Ed Tanous4d99bbb2022-02-17 10:02:57 -0800165 "// {} is generated. Do not edit directly\n".format(
Ed Tanousf395daa2021-08-02 08:56:24 -0700166 os.path.basename(path)))
Ed Tanoused398212021-06-09 17:05:54 -0700167
Ed Tanous4d99bbb2022-02-17 10:02:57 -0800168 registry.write("// clang-format off\n")
169 registry.write("\n#include <privileges.hpp>\n\n")
170 registry.write("namespace redfish::privileges\n")
171 registry.write("{\n")
Ed Tanoused398212021-06-09 17:05:54 -0700172
173 privilege_dict = {}
174 for mapping in json_file["Mappings"]:
175 # first pass, identify all the unique privilege sets
176 for operation, privilege_list in mapping["OperationMap"].items():
177 privilege_dict[get_privilege_string_from_list(
178 privilege_list)] = (privilege_list, )
Ed Tanoused398212021-06-09 17:05:54 -0700179 for index, key in enumerate(privilege_dict):
180 (privilege_list, ) = privilege_dict[key]
181 name = get_variable_name_for_privilege_set(privilege_list)
Ed Tanousf395daa2021-08-02 08:56:24 -0700182 registry.write(
183 "const std::array<Privileges, {}> ".format(
184 len(privilege_list)))
185 registry.write(
186 "privilegeSet{} = {};\n".format(name, key))
Ed Tanoused398212021-06-09 17:05:54 -0700187 privilege_dict[key] = (privilege_list, name)
Ed Tanoused398212021-06-09 17:05:54 -0700188
189 for mapping in json_file["Mappings"]:
190 entity = mapping["Entity"]
Ed Tanous4d99bbb2022-02-17 10:02:57 -0800191 registry.write("// {}\n".format(entity))
Ed Tanoused398212021-06-09 17:05:54 -0700192 for operation, privilege_list in mapping["OperationMap"].items():
193 privilege_string = get_privilege_string_from_list(
194 privilege_list)
195 operation = operation.lower()
196
Ed Tanousf395daa2021-08-02 08:56:24 -0700197 registry.write(
198 "const static auto& {}{} = privilegeSet{};\n".format(
199 operation,
200 entity,
201 privilege_dict[privilege_string][1]))
Ed Tanoused398212021-06-09 17:05:54 -0700202 registry.write("\n")
203 registry.write("} // namespace redfish::privileges\n")
Ed Tanous4d99bbb2022-02-17 10:02:57 -0800204 registry.write("// clang-format on\n")
Ed Tanoused398212021-06-09 17:05:54 -0700205
206
207make_privilege_registry()