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) 2020 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'''
41
42SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
43
44include_path = os.path.realpath(
45    os.path.join(
46        SCRIPT_DIR,
47        "..",
48        "redfish-core",
49        "include",
50        "registries"))
51
52proxies = {
53    'https': os.environ.get("https_proxy", None)
54}
55
56
57def make_getter(dmtf_name, header_name, type_name):
58    url = 'https://redfish.dmtf.org/registries/{}'.format(dmtf_name)
59    dmtf = requests.get(url, proxies=proxies)
60    dmtf.raise_for_status()
61    json_file = json.loads(dmtf.text)
62    path = os.path.join(include_path, header_name)
63    return (path, json_file, type_name, url)
64
65
66files = []
67files.append(make_getter('Base.1.4.0.json',
68                         'base_message_registry.hpp', 'base'))
69files.append(make_getter('TaskEvent.1.0.1.json',
70                         'task_event_message_registry.hpp', 'task_event'))
71
72# Remove the old files
73for file, json, namespace, url in files:
74    try:
75        os.remove(file)
76    except BaseException:
77        print("{} not found".format(file))
78
79    with open(file, 'w') as registry:
80        registry.write(REGISTRY_HEADER.format(namespace))
81        # Parse the Registry header info
82        registry.write("const Header header = {")
83        registry.write("\"{}\",".format(json["@Redfish.Copyright"]))
84        registry.write("\"{}\",".format(json["@odata.type"]))
85        registry.write("\"{}\",".format(json["Id"]))
86        registry.write("\"{}\",".format(json["Name"]))
87        registry.write("\"{}\",".format(json["Language"]))
88        registry.write("\"{}\",".format(json["Description"]))
89        registry.write("\"{}\",".format(json["RegistryPrefix"]))
90        registry.write("\"{}\",".format(json["RegistryVersion"]))
91        registry.write("\"{}\",".format(json["OwningEntity"]))
92        registry.write("};")
93
94        registry.write('constexpr const char * url = "{}";\n\n'.format(url))
95        # Parse each Message entry
96        registry.write("constexpr std::array<MessageEntry, {}> registry = {{".format(
97            len(json["Messages"])))
98        for messageId, message in sorted(json["Messages"].items()):
99            registry.write("MessageEntry{")
100            registry.write("\"{}\",".format(messageId))
101            registry.write("{")
102            registry.write("\"{}\",".format(message["Description"]))
103            registry.write("\"{}\",".format(message["Message"]))
104            registry.write("\"{}\",".format(message["Severity"]))
105            registry.write("{},".format(message["NumberOfArgs"]))
106            registry.write("{")
107            paramTypes = message.get("ParamTypes")
108            if paramTypes:
109                for paramType in paramTypes:
110                    registry.write("\"{}\",".format(paramType))
111            registry.write("},")
112            registry.write("\"{}\",".format(message["Resolution"]))
113            registry.write("}},")
114        registry.write("};}\n")
115    subprocess.check_call(["clang-format-6.0", "-i", file])
116