170304cb5SJason M. Bills /* 2*6be832e2SEd Tanous Copyright (c) 2019 Intel Corporation 3*6be832e2SEd Tanous 4*6be832e2SEd Tanous Licensed under the Apache License, Version 2.0 (the "License"); 5*6be832e2SEd Tanous you may not use this file except in compliance with the License. 6*6be832e2SEd Tanous You may obtain a copy of the License at 7*6be832e2SEd Tanous 8*6be832e2SEd Tanous http://www.apache.org/licenses/LICENSE-2.0 9*6be832e2SEd Tanous 10*6be832e2SEd Tanous Unless required by applicable law or agreed to in writing, software 11*6be832e2SEd Tanous distributed under the License is distributed on an "AS IS" BASIS, 12*6be832e2SEd Tanous WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*6be832e2SEd Tanous See the License for the specific language governing permissions and 14*6be832e2SEd Tanous 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 29fffb8c1fSEd Tanous namespace redfish::registries 3070304cb5SJason M. Bills { 31351d3063SJason M. Bills struct Header 32351d3063SJason M. Bills { 33351d3063SJason M. Bills const char* copyright; 34351d3063SJason M. Bills const char* type; 35351d3063SJason M. Bills const char* id; 36351d3063SJason M. Bills const char* name; 37351d3063SJason M. Bills const char* language; 38351d3063SJason M. Bills const char* description; 39351d3063SJason M. Bills const char* registryPrefix; 40351d3063SJason M. Bills const char* registryVersion; 41351d3063SJason M. Bills const char* owningEntity; 42351d3063SJason M. Bills }; 4370304cb5SJason M. Bills 4470304cb5SJason M. Bills struct Message 4570304cb5SJason M. Bills { 4670304cb5SJason M. Bills const char* description; 4770304cb5SJason M. Bills const char* message; 48e7808c93SGunnar Mills const char* messageSeverity; 49271584abSEd Tanous const size_t numberOfArgs; 5070304cb5SJason M. Bills std::array<const char*, 5> paramTypes; 5170304cb5SJason M. Bills const char* resolution; 5270304cb5SJason M. Bills }; 5370304cb5SJason M. Bills using MessageEntry = std::pair<const char*, const Message>; 54c5ba4c27SEd Tanous 55bd79bce8SPatrick Williams inline std::string fillMessageArgs( 56bd79bce8SPatrick Williams const std::span<const std::string_view> messageArgs, std::string_view msg) 57c5ba4c27SEd Tanous { 5880f595e7SEd Tanous std::string ret; 5980f595e7SEd Tanous size_t reserve = msg.size(); 6026ccae32SEd Tanous for (std::string_view arg : messageArgs) 61c5ba4c27SEd Tanous { 6280f595e7SEd Tanous reserve += arg.size(); 63c5ba4c27SEd Tanous } 6480f595e7SEd Tanous ret.reserve(reserve); 6580f595e7SEd Tanous 6680f595e7SEd Tanous for (size_t stringIndex = msg.find('%'); stringIndex != std::string::npos; 6780f595e7SEd Tanous stringIndex = msg.find('%')) 6880f595e7SEd Tanous { 6980f595e7SEd Tanous ret += msg.substr(0, stringIndex); 7080f595e7SEd Tanous msg.remove_prefix(stringIndex + 1); 7180f595e7SEd Tanous size_t number = 0; 7280f595e7SEd Tanous auto it = std::from_chars(msg.data(), &*msg.end(), number); 7380f595e7SEd Tanous if (it.ec != std::errc()) 7480f595e7SEd Tanous { 7580f595e7SEd Tanous return ""; 76c5ba4c27SEd Tanous } 7780f595e7SEd Tanous msg.remove_prefix(1); 7880f595e7SEd Tanous // Redfish message args are 1 indexed. 7980f595e7SEd Tanous number--; 8080f595e7SEd Tanous if (number >= messageArgs.size()) 8180f595e7SEd Tanous { 8280f595e7SEd Tanous return ""; 8380f595e7SEd Tanous } 8480f595e7SEd Tanous ret += messageArgs[number]; 8580f595e7SEd Tanous } 8680f595e7SEd Tanous ret += msg; 8780f595e7SEd Tanous return ret; 88c5ba4c27SEd Tanous } 89c5ba4c27SEd Tanous 90bd79bce8SPatrick Williams inline nlohmann::json::object_t getLogFromRegistry( 91bd79bce8SPatrick Williams const Header& header, std::span<const MessageEntry> registry, size_t index, 9265e4f1f7SEd Tanous std::span<const std::string_view> args) 9365e4f1f7SEd Tanous { 9465e4f1f7SEd Tanous const redfish::registries::MessageEntry& entry = registry[index]; 9565e4f1f7SEd Tanous // Intentionally make a copy of the string, so we can append in the 9665e4f1f7SEd Tanous // parameters. 972e30bc2dSKrzysztof Grobelny std::string msg = 982e30bc2dSKrzysztof Grobelny redfish::registries::fillMessageArgs(args, entry.second.message); 9965e4f1f7SEd Tanous nlohmann::json jArgs = nlohmann::json::array(); 10026ccae32SEd Tanous for (std::string_view arg : args) 10165e4f1f7SEd Tanous { 10265e4f1f7SEd Tanous jArgs.push_back(arg); 10365e4f1f7SEd Tanous } 10465e4f1f7SEd Tanous std::string msgId = header.id; 10565e4f1f7SEd Tanous msgId += "."; 10665e4f1f7SEd Tanous msgId += entry.first; 10765e4f1f7SEd Tanous 10865e4f1f7SEd Tanous nlohmann::json::object_t response; 10965e4f1f7SEd Tanous response["@odata.type"] = "#Message.v1_1_1.Message"; 11065e4f1f7SEd Tanous response["MessageId"] = std::move(msgId); 11165e4f1f7SEd Tanous response["Message"] = std::move(msg); 11265e4f1f7SEd Tanous response["MessageArgs"] = std::move(jArgs); 11365e4f1f7SEd Tanous response["MessageSeverity"] = entry.second.messageSeverity; 11465e4f1f7SEd Tanous response["Resolution"] = entry.second.resolution; 11565e4f1f7SEd Tanous return response; 11665e4f1f7SEd Tanous } 11765e4f1f7SEd Tanous 118d1d411f9SSui Chen const Message* getMessage(std::string_view messageID); 119d1d411f9SSui Chen 120d1d411f9SSui Chen const Message* getMessageFromRegistry(const std::string& messageKey, 121d1d411f9SSui Chen std::span<const MessageEntry> registry); 122d1d411f9SSui Chen 123fffb8c1fSEd Tanous } // namespace redfish::registries 124