xref: /openbmc/bmcweb/scripts/parse_registries.py (revision 70304cb594859b3862eeecc0a16c8e6e9a126530)
1#!/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
16REGISTRY_HEADER = '''/*
17// Copyright (c) 2019 Intel Corporation
18//
19// Licensed under the Apache License, Version 2.0 (the "License");
20// you may not use this file except in compliance with the License.
21// You may obtain a copy of the License at
22//
23//      http://www.apache.org/licenses/LICENSE-2.0
24//
25// Unless required by applicable law or agreed to in writing, software
26// distributed under the License is distributed on an "AS IS" BASIS,
27// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28// See the License for the specific language governing permissions and
29// limitations under the License.
30*/
31/****************************************************************
32 * This is an auto-generated header which contains definitions
33 * for Redfish DMTF defined messages.
34 ***************************************************************/
35#pragma once
36#include <registries.hpp>
37
38namespace redfish::message_registries::{}
39{{
40
41const std::array registry = {{
42'''
43
44SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
45
46include_path = os.path.realpath(os.path.join(SCRIPT_DIR, "..", "redfish-core", "include"))
47
48proxies = {
49    'https': os.environ.get("https_proxy", None)
50}
51
52base_file = requests.get('https://redfish.dmtf.org/registries/Base.1.4.0.json', proxies=proxies)
53base_file.raise_for_status()
54base_json = json.loads(base_file.text)
55base_path = os.path.join(include_path, "base_message_registry.hpp")
56
57files = [(base_path, base_json, "base")]
58
59# Remove the old files
60for file, json, namespace in files:
61    try:
62        os.remove(file)
63    except:
64        print("{} not found".format(file))
65
66    with open(file, 'w') as registry:
67        registry.write(REGISTRY_HEADER.format(namespace))
68        for messageId, message in sorted(json["Messages"].items()):
69            registry.write("MessageEntry{")
70            registry.write("\"{}\",".format(messageId))
71            registry.write("{")
72            registry.write(".description = \"{}\",".format(message["Description"]))
73            registry.write(".message = \"{}\",".format(message["Message"]))
74            registry.write(".severity = \"{}\",".format(message["Severity"]))
75            registry.write(".numberOfArgs = {},".format(message["NumberOfArgs"]))
76            registry.write(".paramTypes = {")
77            paramTypes = message.get("ParamTypes")
78            if paramTypes:
79                for paramType in paramTypes:
80                    registry.write("\"{}\",".format(paramType))
81            registry.write("},")
82            registry.write(".resolution = \"{}\",".format(message["Resolution"]))
83            registry.write("}},")
84        registry.write("};}\n")
85    subprocess.check_call(["clang-format", "-i", file])
86