xref: /openbmc/bmcweb/redfish-core/include/registries.hpp (revision bd79bce8c3f1deb1fb2773868b9ece25233cf27b)
170304cb5SJason M. Bills /*
270304cb5SJason M. Bills // Copyright (c) 2019 Intel Corporation
370304cb5SJason M. Bills //
470304cb5SJason M. Bills // Licensed under the Apache License, Version 2.0 (the "License");
570304cb5SJason M. Bills // you may not use this file except in compliance with the License.
670304cb5SJason M. Bills // You may obtain a copy of the License at
770304cb5SJason M. Bills //
870304cb5SJason M. Bills //      http://www.apache.org/licenses/LICENSE-2.0
970304cb5SJason M. Bills //
1070304cb5SJason M. Bills // Unless required by applicable law or agreed to in writing, software
1170304cb5SJason M. Bills // distributed under the License is distributed on an "AS IS" BASIS,
1270304cb5SJason M. Bills // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1370304cb5SJason M. Bills // See the License for the specific language governing permissions and
1470304cb5SJason M. Bills // limitations under the License.
1570304cb5SJason M. Bills */
1670304cb5SJason M. Bills #pragma once
17c5ba4c27SEd Tanous 
1865e4f1f7SEd Tanous #include <nlohmann/json.hpp>
1965e4f1f7SEd Tanous 
20d5c80ad9SNan Zhou #include <array>
2180f595e7SEd Tanous #include <charconv>
22d5c80ad9SNan Zhou #include <cstddef>
2380f595e7SEd Tanous #include <numeric>
24c5ba4c27SEd Tanous #include <span>
25c5ba4c27SEd Tanous #include <string>
26c5ba4c27SEd Tanous #include <string_view>
27d5c80ad9SNan Zhou #include <utility>
28d5c80ad9SNan Zhou 
29d5c80ad9SNan Zhou // IWYU pragma: no_include <stddef.h>
30c5ba4c27SEd Tanous 
31fffb8c1fSEd Tanous namespace redfish::registries
3270304cb5SJason M. Bills {
33351d3063SJason M. Bills struct Header
34351d3063SJason M. Bills {
35351d3063SJason M. Bills     const char* copyright;
36351d3063SJason M. Bills     const char* type;
37351d3063SJason M. Bills     const char* id;
38351d3063SJason M. Bills     const char* name;
39351d3063SJason M. Bills     const char* language;
40351d3063SJason M. Bills     const char* description;
41351d3063SJason M. Bills     const char* registryPrefix;
42351d3063SJason M. Bills     const char* registryVersion;
43351d3063SJason M. Bills     const char* owningEntity;
44351d3063SJason M. Bills };
4570304cb5SJason M. Bills 
4670304cb5SJason M. Bills struct Message
4770304cb5SJason M. Bills {
4870304cb5SJason M. Bills     const char* description;
4970304cb5SJason M. Bills     const char* message;
50e7808c93SGunnar Mills     const char* messageSeverity;
51271584abSEd Tanous     const size_t numberOfArgs;
5270304cb5SJason M. Bills     std::array<const char*, 5> paramTypes;
5370304cb5SJason M. Bills     const char* resolution;
5470304cb5SJason M. Bills };
5570304cb5SJason M. Bills using MessageEntry = std::pair<const char*, const Message>;
56c5ba4c27SEd Tanous 
57*bd79bce8SPatrick Williams inline std::string fillMessageArgs(
58*bd79bce8SPatrick Williams     const std::span<const std::string_view> messageArgs, std::string_view msg)
59c5ba4c27SEd Tanous {
6080f595e7SEd Tanous     std::string ret;
6180f595e7SEd Tanous     size_t reserve = msg.size();
6226ccae32SEd Tanous     for (std::string_view arg : messageArgs)
63c5ba4c27SEd Tanous     {
6480f595e7SEd Tanous         reserve += arg.size();
65c5ba4c27SEd Tanous     }
6680f595e7SEd Tanous     ret.reserve(reserve);
6780f595e7SEd Tanous 
6880f595e7SEd Tanous     for (size_t stringIndex = msg.find('%'); stringIndex != std::string::npos;
6980f595e7SEd Tanous          stringIndex = msg.find('%'))
7080f595e7SEd Tanous     {
7180f595e7SEd Tanous         ret += msg.substr(0, stringIndex);
7280f595e7SEd Tanous         msg.remove_prefix(stringIndex + 1);
7380f595e7SEd Tanous         size_t number = 0;
7480f595e7SEd Tanous         auto it = std::from_chars(msg.data(), &*msg.end(), number);
7580f595e7SEd Tanous         if (it.ec != std::errc())
7680f595e7SEd Tanous         {
7780f595e7SEd Tanous             return "";
78c5ba4c27SEd Tanous         }
7980f595e7SEd Tanous         msg.remove_prefix(1);
8080f595e7SEd Tanous         // Redfish message args are 1 indexed.
8180f595e7SEd Tanous         number--;
8280f595e7SEd Tanous         if (number >= messageArgs.size())
8380f595e7SEd Tanous         {
8480f595e7SEd Tanous             return "";
8580f595e7SEd Tanous         }
8680f595e7SEd Tanous         ret += messageArgs[number];
8780f595e7SEd Tanous     }
8880f595e7SEd Tanous     ret += msg;
8980f595e7SEd Tanous     return ret;
90c5ba4c27SEd Tanous }
91c5ba4c27SEd Tanous 
92*bd79bce8SPatrick Williams inline nlohmann::json::object_t getLogFromRegistry(
93*bd79bce8SPatrick Williams     const Header& header, std::span<const MessageEntry> registry, size_t index,
9465e4f1f7SEd Tanous     std::span<const std::string_view> args)
9565e4f1f7SEd Tanous {
9665e4f1f7SEd Tanous     const redfish::registries::MessageEntry& entry = registry[index];
9765e4f1f7SEd Tanous     // Intentionally make a copy of the string, so we can append in the
9865e4f1f7SEd Tanous     // parameters.
992e30bc2dSKrzysztof Grobelny     std::string msg =
1002e30bc2dSKrzysztof Grobelny         redfish::registries::fillMessageArgs(args, entry.second.message);
10165e4f1f7SEd Tanous     nlohmann::json jArgs = nlohmann::json::array();
10226ccae32SEd Tanous     for (std::string_view arg : args)
10365e4f1f7SEd Tanous     {
10465e4f1f7SEd Tanous         jArgs.push_back(arg);
10565e4f1f7SEd Tanous     }
10665e4f1f7SEd Tanous     std::string msgId = header.id;
10765e4f1f7SEd Tanous     msgId += ".";
10865e4f1f7SEd Tanous     msgId += entry.first;
10965e4f1f7SEd Tanous 
11065e4f1f7SEd Tanous     nlohmann::json::object_t response;
11165e4f1f7SEd Tanous     response["@odata.type"] = "#Message.v1_1_1.Message";
11265e4f1f7SEd Tanous     response["MessageId"] = std::move(msgId);
11365e4f1f7SEd Tanous     response["Message"] = std::move(msg);
11465e4f1f7SEd Tanous     response["MessageArgs"] = std::move(jArgs);
11565e4f1f7SEd Tanous     response["MessageSeverity"] = entry.second.messageSeverity;
11665e4f1f7SEd Tanous     response["Resolution"] = entry.second.resolution;
11765e4f1f7SEd Tanous     return response;
11865e4f1f7SEd Tanous }
11965e4f1f7SEd Tanous 
120d1d411f9SSui Chen const Message* getMessage(std::string_view messageID);
121d1d411f9SSui Chen 
122d1d411f9SSui Chen const Message* getMessageFromRegistry(const std::string& messageKey,
123d1d411f9SSui Chen                                       std::span<const MessageEntry> registry);
124d1d411f9SSui Chen 
125fffb8c1fSEd Tanous } // namespace redfish::registries
126