blob: bad31c43246fa003e7f7bb2ed11b2437a645c0c9 [file] [log] [blame]
Jason M. Bills70304cb2019-03-27 12:03:59 -07001/*
2// Copyright (c) 2019 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16#pragma once
Ed Tanousc5ba4c22022-02-07 09:59:55 -080017
Ed Tanous65e4f1f2022-02-08 00:23:54 -080018#include <nlohmann/json.hpp>
19
Nan Zhoud5c80ad2022-07-11 01:16:31 +000020#include <array>
Ed Tanous80f595e2022-02-14 09:32:05 -080021#include <charconv>
Nan Zhoud5c80ad2022-07-11 01:16:31 +000022#include <cstddef>
Ed Tanous80f595e2022-02-14 09:32:05 -080023#include <iostream>
24#include <numeric>
Ed Tanousc5ba4c22022-02-07 09:59:55 -080025#include <span>
26#include <string>
27#include <string_view>
Nan Zhoud5c80ad2022-07-11 01:16:31 +000028#include <utility>
29
30// IWYU pragma: no_include <stddef.h>
Ed Tanousc5ba4c22022-02-07 09:59:55 -080031
Ed Tanousfffb8c12022-02-07 23:53:03 -080032namespace redfish::registries
Jason M. Bills70304cb2019-03-27 12:03:59 -070033{
Jason M. Bills351d3062019-03-27 12:58:21 -070034struct Header
35{
36 const char* copyright;
37 const char* type;
38 const char* id;
39 const char* name;
40 const char* language;
41 const char* description;
42 const char* registryPrefix;
43 const char* registryVersion;
44 const char* owningEntity;
45};
Jason M. Bills70304cb2019-03-27 12:03:59 -070046
47struct Message
48{
49 const char* description;
50 const char* message;
Gunnar Millse7808c92020-07-08 21:17:44 -050051 const char* messageSeverity;
Ed Tanous271584a2019-07-09 16:24:22 -070052 const size_t numberOfArgs;
Jason M. Bills70304cb2019-03-27 12:03:59 -070053 std::array<const char*, 5> paramTypes;
54 const char* resolution;
55};
56using MessageEntry = std::pair<const char*, const Message>;
Ed Tanousc5ba4c22022-02-07 09:59:55 -080057
Ed Tanous80f595e2022-02-14 09:32:05 -080058inline std::string
59 fillMessageArgs(const std::span<const std::string_view> messageArgs,
60 std::string_view msg)
Ed Tanousc5ba4c22022-02-07 09:59:55 -080061{
Ed Tanous80f595e2022-02-14 09:32:05 -080062 std::string ret;
63 size_t reserve = msg.size();
Ed Tanous26ccae32023-02-16 10:28:44 -080064 for (std::string_view arg : messageArgs)
Ed Tanousc5ba4c22022-02-07 09:59:55 -080065 {
Ed Tanous80f595e2022-02-14 09:32:05 -080066 reserve += arg.size();
Ed Tanousc5ba4c22022-02-07 09:59:55 -080067 }
Ed Tanous80f595e2022-02-14 09:32:05 -080068 ret.reserve(reserve);
69
70 for (size_t stringIndex = msg.find('%'); stringIndex != std::string::npos;
71 stringIndex = msg.find('%'))
72 {
73 ret += msg.substr(0, stringIndex);
74 msg.remove_prefix(stringIndex + 1);
75 size_t number = 0;
76 auto it = std::from_chars(msg.data(), &*msg.end(), number);
77 if (it.ec != std::errc())
78 {
79 return "";
80 }
81 msg.remove_prefix(1);
82 // Redfish message args are 1 indexed.
83 number--;
84 if (number >= messageArgs.size())
85 {
86 return "";
87 }
88 ret += messageArgs[number];
89 }
90 ret += msg;
91 return ret;
Ed Tanousc5ba4c22022-02-07 09:59:55 -080092}
93
Ed Tanous65e4f1f2022-02-08 00:23:54 -080094inline nlohmann::json::object_t
95 getLogFromRegistry(const Header& header,
96 std::span<const MessageEntry> registry, size_t index,
97 std::span<const std::string_view> args)
98{
99 const redfish::registries::MessageEntry& entry = registry[index];
100 // Intentionally make a copy of the string, so we can append in the
101 // parameters.
Krzysztof Grobelny2e30bc22022-09-09 10:13:41 +0200102 std::string msg =
103 redfish::registries::fillMessageArgs(args, entry.second.message);
Ed Tanous65e4f1f2022-02-08 00:23:54 -0800104 nlohmann::json jArgs = nlohmann::json::array();
Ed Tanous26ccae32023-02-16 10:28:44 -0800105 for (std::string_view arg : args)
Ed Tanous65e4f1f2022-02-08 00:23:54 -0800106 {
107 jArgs.push_back(arg);
108 }
109 std::string msgId = header.id;
110 msgId += ".";
111 msgId += entry.first;
112
113 nlohmann::json::object_t response;
114 response["@odata.type"] = "#Message.v1_1_1.Message";
115 response["MessageId"] = std::move(msgId);
116 response["Message"] = std::move(msg);
117 response["MessageArgs"] = std::move(jArgs);
118 response["MessageSeverity"] = entry.second.messageSeverity;
119 response["Resolution"] = entry.second.resolution;
120 return response;
121}
122
Sui Chend1d411f2022-04-10 23:09:36 -0700123const Message* getMessage(std::string_view messageID);
124
125const Message* getMessageFromRegistry(const std::string& messageKey,
126 std::span<const MessageEntry> registry);
127
Ed Tanousfffb8c12022-02-07 23:53:03 -0800128} // namespace redfish::registries