xref: /openbmc/bmcweb/features/redfish/lib/log_services.hpp (revision 4e0453b11cf4780b06c73643ce31e6bc1809b7cf)
11da66f75SEd Tanous /*
21da66f75SEd Tanous // Copyright (c) 2018 Intel Corporation
31da66f75SEd Tanous //
41da66f75SEd Tanous // Licensed under the Apache License, Version 2.0 (the "License");
51da66f75SEd Tanous // you may not use this file except in compliance with the License.
61da66f75SEd Tanous // You may obtain a copy of the License at
71da66f75SEd Tanous //
81da66f75SEd Tanous //      http://www.apache.org/licenses/LICENSE-2.0
91da66f75SEd Tanous //
101da66f75SEd Tanous // Unless required by applicable law or agreed to in writing, software
111da66f75SEd Tanous // distributed under the License is distributed on an "AS IS" BASIS,
121da66f75SEd Tanous // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131da66f75SEd Tanous // See the License for the specific language governing permissions and
141da66f75SEd Tanous // limitations under the License.
151da66f75SEd Tanous */
161da66f75SEd Tanous #pragma once
171da66f75SEd Tanous 
181da66f75SEd Tanous #include "node.hpp"
194851d45dSJason M. Bills #include "registries.hpp"
204851d45dSJason M. Bills #include "registries/base_message_registry.hpp"
214851d45dSJason M. Bills #include "registries/openbmc_message_registry.hpp"
2246229577SJames Feist #include "task.hpp"
231da66f75SEd Tanous 
24e1f26343SJason M. Bills #include <systemd/sd-journal.h>
25e1f26343SJason M. Bills 
264851d45dSJason M. Bills #include <boost/algorithm/string/split.hpp>
274851d45dSJason M. Bills #include <boost/beast/core/span.hpp>
281da66f75SEd Tanous #include <boost/container/flat_map.hpp>
291ddcf01aSJason M. Bills #include <boost/system/linux_error.hpp>
300657843aSraviteja-b #include <dump_offload.hpp>
31cb92c03bSAndrew Geissler #include <error_messages.hpp>
321214b7e7SGunnar Mills 
334418c7f0SJames Feist #include <filesystem>
34cd225da8SJason M. Bills #include <string_view>
35abf2add6SEd Tanous #include <variant>
361da66f75SEd Tanous 
371da66f75SEd Tanous namespace redfish
381da66f75SEd Tanous {
391da66f75SEd Tanous 
405b61b5e8SJason M. Bills constexpr char const* crashdumpObject = "com.intel.crashdump";
415b61b5e8SJason M. Bills constexpr char const* crashdumpPath = "/com/intel/crashdump";
425b61b5e8SJason M. Bills constexpr char const* crashdumpInterface = "com.intel.crashdump";
435b61b5e8SJason M. Bills constexpr char const* deleteAllInterface =
445b61b5e8SJason M. Bills     "xyz.openbmc_project.Collection.DeleteAll";
455b61b5e8SJason M. Bills constexpr char const* crashdumpOnDemandInterface =
46424c4176SJason M. Bills     "com.intel.crashdump.OnDemand";
475b61b5e8SJason M. Bills constexpr char const* crashdumpRawPECIInterface =
48424c4176SJason M. Bills     "com.intel.crashdump.SendRawPeci";
496eda7685SKenny L. Ku constexpr char const* crashdumpTelemetryInterface =
506eda7685SKenny L. Ku     "com.intel.crashdump.Telemetry";
511da66f75SEd Tanous 
524851d45dSJason M. Bills namespace message_registries
534851d45dSJason M. Bills {
544851d45dSJason M. Bills static const Message* getMessageFromRegistry(
554851d45dSJason M. Bills     const std::string& messageKey,
564851d45dSJason M. Bills     const boost::beast::span<const MessageEntry> registry)
574851d45dSJason M. Bills {
584851d45dSJason M. Bills     boost::beast::span<const MessageEntry>::const_iterator messageIt =
594851d45dSJason M. Bills         std::find_if(registry.cbegin(), registry.cend(),
604851d45dSJason M. Bills                      [&messageKey](const MessageEntry& messageEntry) {
614851d45dSJason M. Bills                          return !std::strcmp(messageEntry.first,
624851d45dSJason M. Bills                                              messageKey.c_str());
634851d45dSJason M. Bills                      });
644851d45dSJason M. Bills     if (messageIt != registry.cend())
654851d45dSJason M. Bills     {
664851d45dSJason M. Bills         return &messageIt->second;
674851d45dSJason M. Bills     }
684851d45dSJason M. Bills 
694851d45dSJason M. Bills     return nullptr;
704851d45dSJason M. Bills }
714851d45dSJason M. Bills 
724851d45dSJason M. Bills static const Message* getMessage(const std::string_view& messageID)
734851d45dSJason M. Bills {
744851d45dSJason M. Bills     // Redfish MessageIds are in the form
754851d45dSJason M. Bills     // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find
764851d45dSJason M. Bills     // the right Message
774851d45dSJason M. Bills     std::vector<std::string> fields;
784851d45dSJason M. Bills     fields.reserve(4);
794851d45dSJason M. Bills     boost::split(fields, messageID, boost::is_any_of("."));
804851d45dSJason M. Bills     std::string& registryName = fields[0];
814851d45dSJason M. Bills     std::string& messageKey = fields[3];
824851d45dSJason M. Bills 
834851d45dSJason M. Bills     // Find the right registry and check it for the MessageKey
844851d45dSJason M. Bills     if (std::string(base::header.registryPrefix) == registryName)
854851d45dSJason M. Bills     {
864851d45dSJason M. Bills         return getMessageFromRegistry(
874851d45dSJason M. Bills             messageKey, boost::beast::span<const MessageEntry>(base::registry));
884851d45dSJason M. Bills     }
894851d45dSJason M. Bills     if (std::string(openbmc::header.registryPrefix) == registryName)
904851d45dSJason M. Bills     {
914851d45dSJason M. Bills         return getMessageFromRegistry(
924851d45dSJason M. Bills             messageKey,
934851d45dSJason M. Bills             boost::beast::span<const MessageEntry>(openbmc::registry));
944851d45dSJason M. Bills     }
954851d45dSJason M. Bills     return nullptr;
964851d45dSJason M. Bills }
974851d45dSJason M. Bills } // namespace message_registries
984851d45dSJason M. Bills 
99f6150403SJames Feist namespace fs = std::filesystem;
1001da66f75SEd Tanous 
101cb92c03bSAndrew Geissler using GetManagedPropertyType = boost::container::flat_map<
10219bd78d9SPatrick Williams     std::string, std::variant<std::string, bool, uint8_t, int16_t, uint16_t,
103cb92c03bSAndrew Geissler                               int32_t, uint32_t, int64_t, uint64_t, double>>;
104cb92c03bSAndrew Geissler 
105cb92c03bSAndrew Geissler using GetManagedObjectsType = boost::container::flat_map<
106cb92c03bSAndrew Geissler     sdbusplus::message::object_path,
107cb92c03bSAndrew Geissler     boost::container::flat_map<std::string, GetManagedPropertyType>>;
108cb92c03bSAndrew Geissler 
109cb92c03bSAndrew Geissler inline std::string translateSeverityDbusToRedfish(const std::string& s)
110cb92c03bSAndrew Geissler {
111cb92c03bSAndrew Geissler     if (s == "xyz.openbmc_project.Logging.Entry.Level.Alert")
112cb92c03bSAndrew Geissler     {
113cb92c03bSAndrew Geissler         return "Critical";
114cb92c03bSAndrew Geissler     }
115cb92c03bSAndrew Geissler     else if (s == "xyz.openbmc_project.Logging.Entry.Level.Critical")
116cb92c03bSAndrew Geissler     {
117cb92c03bSAndrew Geissler         return "Critical";
118cb92c03bSAndrew Geissler     }
119cb92c03bSAndrew Geissler     else if (s == "xyz.openbmc_project.Logging.Entry.Level.Debug")
120cb92c03bSAndrew Geissler     {
121cb92c03bSAndrew Geissler         return "OK";
122cb92c03bSAndrew Geissler     }
123cb92c03bSAndrew Geissler     else if (s == "xyz.openbmc_project.Logging.Entry.Level.Emergency")
124cb92c03bSAndrew Geissler     {
125cb92c03bSAndrew Geissler         return "Critical";
126cb92c03bSAndrew Geissler     }
127cb92c03bSAndrew Geissler     else if (s == "xyz.openbmc_project.Logging.Entry.Level.Error")
128cb92c03bSAndrew Geissler     {
129cb92c03bSAndrew Geissler         return "Critical";
130cb92c03bSAndrew Geissler     }
131cb92c03bSAndrew Geissler     else if (s == "xyz.openbmc_project.Logging.Entry.Level.Informational")
132cb92c03bSAndrew Geissler     {
133cb92c03bSAndrew Geissler         return "OK";
134cb92c03bSAndrew Geissler     }
135cb92c03bSAndrew Geissler     else if (s == "xyz.openbmc_project.Logging.Entry.Level.Notice")
136cb92c03bSAndrew Geissler     {
137cb92c03bSAndrew Geissler         return "OK";
138cb92c03bSAndrew Geissler     }
139cb92c03bSAndrew Geissler     else if (s == "xyz.openbmc_project.Logging.Entry.Level.Warning")
140cb92c03bSAndrew Geissler     {
141cb92c03bSAndrew Geissler         return "Warning";
142cb92c03bSAndrew Geissler     }
143cb92c03bSAndrew Geissler     return "";
144cb92c03bSAndrew Geissler }
145cb92c03bSAndrew Geissler 
146c9bb6861Sraviteja-b inline void deleteSystemDumpEntry(crow::Response& res,
147c9bb6861Sraviteja-b                                   const std::string& entryID)
148c9bb6861Sraviteja-b {
149c9bb6861Sraviteja-b     std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
150c9bb6861Sraviteja-b 
151c9bb6861Sraviteja-b     auto respHandler = [asyncResp](const boost::system::error_code ec) {
152c9bb6861Sraviteja-b         BMCWEB_LOG_DEBUG << "System Dump Entry doDelete callback: Done";
153c9bb6861Sraviteja-b         if (ec)
154c9bb6861Sraviteja-b         {
155c9bb6861Sraviteja-b             BMCWEB_LOG_ERROR
156c9bb6861Sraviteja-b                 << "System Dump (DBus) doDelete respHandler got error " << ec;
157c9bb6861Sraviteja-b             asyncResp->res.result(
158c9bb6861Sraviteja-b                 boost::beast::http::status::internal_server_error);
159c9bb6861Sraviteja-b             return;
160c9bb6861Sraviteja-b         }
161c9bb6861Sraviteja-b     };
162c9bb6861Sraviteja-b     crow::connections::systemBus->async_method_call(
163c9bb6861Sraviteja-b         respHandler, "xyz.openbmc_project.Dump.Manager",
164c9bb6861Sraviteja-b         "/xyz/openbmc_project/dump/entry/" + entryID,
165c9bb6861Sraviteja-b         "xyz.openbmc_project.Object.Delete", "Delete");
166c9bb6861Sraviteja-b }
167c9bb6861Sraviteja-b 
16816428a1aSJason M. Bills static int getJournalMetadata(sd_journal* journal,
16939e77504SEd Tanous                               const std::string_view& field,
17039e77504SEd Tanous                               std::string_view& contents)
17116428a1aSJason M. Bills {
17216428a1aSJason M. Bills     const char* data = nullptr;
17316428a1aSJason M. Bills     size_t length = 0;
17416428a1aSJason M. Bills     int ret = 0;
17516428a1aSJason M. Bills     // Get the metadata from the requested field of the journal entry
176271584abSEd Tanous     ret = sd_journal_get_data(journal, field.data(),
177271584abSEd Tanous                               reinterpret_cast<const void**>(&data), &length);
17816428a1aSJason M. Bills     if (ret < 0)
17916428a1aSJason M. Bills     {
18016428a1aSJason M. Bills         return ret;
18116428a1aSJason M. Bills     }
18239e77504SEd Tanous     contents = std::string_view(data, length);
18316428a1aSJason M. Bills     // Only use the content after the "=" character.
18416428a1aSJason M. Bills     contents.remove_prefix(std::min(contents.find("=") + 1, contents.size()));
18516428a1aSJason M. Bills     return ret;
18616428a1aSJason M. Bills }
18716428a1aSJason M. Bills 
18816428a1aSJason M. Bills static int getJournalMetadata(sd_journal* journal,
18939e77504SEd Tanous                               const std::string_view& field, const int& base,
190271584abSEd Tanous                               long int& contents)
19116428a1aSJason M. Bills {
19216428a1aSJason M. Bills     int ret = 0;
19339e77504SEd Tanous     std::string_view metadata;
19416428a1aSJason M. Bills     // Get the metadata from the requested field of the journal entry
19516428a1aSJason M. Bills     ret = getJournalMetadata(journal, field, metadata);
19616428a1aSJason M. Bills     if (ret < 0)
19716428a1aSJason M. Bills     {
19816428a1aSJason M. Bills         return ret;
19916428a1aSJason M. Bills     }
200b01bf299SEd Tanous     contents = strtol(metadata.data(), nullptr, base);
20116428a1aSJason M. Bills     return ret;
20216428a1aSJason M. Bills }
20316428a1aSJason M. Bills 
204a3316fc6SZhikuiRen static bool getTimestampStr(const uint64_t usecSinceEpoch,
205a3316fc6SZhikuiRen                             std::string& entryTimestamp)
20616428a1aSJason M. Bills {
207a3316fc6SZhikuiRen     time_t t = static_cast<time_t>(usecSinceEpoch / 1000 / 1000);
20816428a1aSJason M. Bills     struct tm* loctime = localtime(&t);
20916428a1aSJason M. Bills     char entryTime[64] = {};
21099131cd0SEd Tanous     if (nullptr != loctime)
21116428a1aSJason M. Bills     {
21216428a1aSJason M. Bills         strftime(entryTime, sizeof(entryTime), "%FT%T%z", loctime);
21316428a1aSJason M. Bills     }
21416428a1aSJason M. Bills     // Insert the ':' into the timezone
21539e77504SEd Tanous     std::string_view t1(entryTime);
21639e77504SEd Tanous     std::string_view t2(entryTime);
21716428a1aSJason M. Bills     if (t1.size() > 2 && t2.size() > 2)
21816428a1aSJason M. Bills     {
21916428a1aSJason M. Bills         t1.remove_suffix(2);
22016428a1aSJason M. Bills         t2.remove_prefix(t2.size() - 2);
22116428a1aSJason M. Bills     }
22239e77504SEd Tanous     entryTimestamp = std::string(t1) + ":" + std::string(t2);
22316428a1aSJason M. Bills     return true;
22416428a1aSJason M. Bills }
22516428a1aSJason M. Bills 
226a3316fc6SZhikuiRen static bool getEntryTimestamp(sd_journal* journal, std::string& entryTimestamp)
227a3316fc6SZhikuiRen {
228a3316fc6SZhikuiRen     int ret = 0;
229a3316fc6SZhikuiRen     uint64_t timestamp = 0;
230a3316fc6SZhikuiRen     ret = sd_journal_get_realtime_usec(journal, &timestamp);
231a3316fc6SZhikuiRen     if (ret < 0)
232a3316fc6SZhikuiRen     {
233a3316fc6SZhikuiRen         BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
234a3316fc6SZhikuiRen                          << strerror(-ret);
235a3316fc6SZhikuiRen         return false;
236a3316fc6SZhikuiRen     }
237a3316fc6SZhikuiRen     return getTimestampStr(timestamp, entryTimestamp);
238a3316fc6SZhikuiRen }
239a3316fc6SZhikuiRen 
24016428a1aSJason M. Bills static bool getSkipParam(crow::Response& res, const crow::Request& req,
241271584abSEd Tanous                          uint64_t& skip)
24216428a1aSJason M. Bills {
24316428a1aSJason M. Bills     char* skipParam = req.urlParams.get("$skip");
24416428a1aSJason M. Bills     if (skipParam != nullptr)
24516428a1aSJason M. Bills     {
24616428a1aSJason M. Bills         char* ptr = nullptr;
247271584abSEd Tanous         skip = std::strtoul(skipParam, &ptr, 10);
24816428a1aSJason M. Bills         if (*skipParam == '\0' || *ptr != '\0')
24916428a1aSJason M. Bills         {
25016428a1aSJason M. Bills 
25116428a1aSJason M. Bills             messages::queryParameterValueTypeError(res, std::string(skipParam),
25216428a1aSJason M. Bills                                                    "$skip");
25316428a1aSJason M. Bills             return false;
25416428a1aSJason M. Bills         }
25516428a1aSJason M. Bills     }
25616428a1aSJason M. Bills     return true;
25716428a1aSJason M. Bills }
25816428a1aSJason M. Bills 
259271584abSEd Tanous static constexpr const uint64_t maxEntriesPerPage = 1000;
26016428a1aSJason M. Bills static bool getTopParam(crow::Response& res, const crow::Request& req,
261271584abSEd Tanous                         uint64_t& top)
26216428a1aSJason M. Bills {
26316428a1aSJason M. Bills     char* topParam = req.urlParams.get("$top");
26416428a1aSJason M. Bills     if (topParam != nullptr)
26516428a1aSJason M. Bills     {
26616428a1aSJason M. Bills         char* ptr = nullptr;
267271584abSEd Tanous         top = std::strtoul(topParam, &ptr, 10);
26816428a1aSJason M. Bills         if (*topParam == '\0' || *ptr != '\0')
26916428a1aSJason M. Bills         {
27016428a1aSJason M. Bills             messages::queryParameterValueTypeError(res, std::string(topParam),
27116428a1aSJason M. Bills                                                    "$top");
27216428a1aSJason M. Bills             return false;
27316428a1aSJason M. Bills         }
274271584abSEd Tanous         if (top < 1U || top > maxEntriesPerPage)
27516428a1aSJason M. Bills         {
27616428a1aSJason M. Bills 
27716428a1aSJason M. Bills             messages::queryParameterOutOfRange(
27816428a1aSJason M. Bills                 res, std::to_string(top), "$top",
27916428a1aSJason M. Bills                 "1-" + std::to_string(maxEntriesPerPage));
28016428a1aSJason M. Bills             return false;
28116428a1aSJason M. Bills         }
28216428a1aSJason M. Bills     }
28316428a1aSJason M. Bills     return true;
28416428a1aSJason M. Bills }
28516428a1aSJason M. Bills 
286e85d6b16SJason M. Bills static bool getUniqueEntryID(sd_journal* journal, std::string& entryID,
287e85d6b16SJason M. Bills                              const bool firstEntry = true)
28816428a1aSJason M. Bills {
28916428a1aSJason M. Bills     int ret = 0;
29016428a1aSJason M. Bills     static uint64_t prevTs = 0;
29116428a1aSJason M. Bills     static int index = 0;
292e85d6b16SJason M. Bills     if (firstEntry)
293e85d6b16SJason M. Bills     {
294e85d6b16SJason M. Bills         prevTs = 0;
295e85d6b16SJason M. Bills     }
296e85d6b16SJason M. Bills 
29716428a1aSJason M. Bills     // Get the entry timestamp
29816428a1aSJason M. Bills     uint64_t curTs = 0;
29916428a1aSJason M. Bills     ret = sd_journal_get_realtime_usec(journal, &curTs);
30016428a1aSJason M. Bills     if (ret < 0)
30116428a1aSJason M. Bills     {
30216428a1aSJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
30316428a1aSJason M. Bills                          << strerror(-ret);
30416428a1aSJason M. Bills         return false;
30516428a1aSJason M. Bills     }
30616428a1aSJason M. Bills     // If the timestamp isn't unique, increment the index
30716428a1aSJason M. Bills     if (curTs == prevTs)
30816428a1aSJason M. Bills     {
30916428a1aSJason M. Bills         index++;
31016428a1aSJason M. Bills     }
31116428a1aSJason M. Bills     else
31216428a1aSJason M. Bills     {
31316428a1aSJason M. Bills         // Otherwise, reset it
31416428a1aSJason M. Bills         index = 0;
31516428a1aSJason M. Bills     }
31616428a1aSJason M. Bills     // Save the timestamp
31716428a1aSJason M. Bills     prevTs = curTs;
31816428a1aSJason M. Bills 
31916428a1aSJason M. Bills     entryID = std::to_string(curTs);
32016428a1aSJason M. Bills     if (index > 0)
32116428a1aSJason M. Bills     {
32216428a1aSJason M. Bills         entryID += "_" + std::to_string(index);
32316428a1aSJason M. Bills     }
32416428a1aSJason M. Bills     return true;
32516428a1aSJason M. Bills }
32616428a1aSJason M. Bills 
327e85d6b16SJason M. Bills static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
328e85d6b16SJason M. Bills                              const bool firstEntry = true)
32995820184SJason M. Bills {
330271584abSEd Tanous     static time_t prevTs = 0;
33195820184SJason M. Bills     static int index = 0;
332e85d6b16SJason M. Bills     if (firstEntry)
333e85d6b16SJason M. Bills     {
334e85d6b16SJason M. Bills         prevTs = 0;
335e85d6b16SJason M. Bills     }
336e85d6b16SJason M. Bills 
33795820184SJason M. Bills     // Get the entry timestamp
338271584abSEd Tanous     std::time_t curTs = 0;
33995820184SJason M. Bills     std::tm timeStruct = {};
34095820184SJason M. Bills     std::istringstream entryStream(logEntry);
34195820184SJason M. Bills     if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
34295820184SJason M. Bills     {
34395820184SJason M. Bills         curTs = std::mktime(&timeStruct);
34495820184SJason M. Bills     }
34595820184SJason M. Bills     // If the timestamp isn't unique, increment the index
34695820184SJason M. Bills     if (curTs == prevTs)
34795820184SJason M. Bills     {
34895820184SJason M. Bills         index++;
34995820184SJason M. Bills     }
35095820184SJason M. Bills     else
35195820184SJason M. Bills     {
35295820184SJason M. Bills         // Otherwise, reset it
35395820184SJason M. Bills         index = 0;
35495820184SJason M. Bills     }
35595820184SJason M. Bills     // Save the timestamp
35695820184SJason M. Bills     prevTs = curTs;
35795820184SJason M. Bills 
35895820184SJason M. Bills     entryID = std::to_string(curTs);
35995820184SJason M. Bills     if (index > 0)
36095820184SJason M. Bills     {
36195820184SJason M. Bills         entryID += "_" + std::to_string(index);
36295820184SJason M. Bills     }
36395820184SJason M. Bills     return true;
36495820184SJason M. Bills }
36595820184SJason M. Bills 
36616428a1aSJason M. Bills static bool getTimestampFromID(crow::Response& res, const std::string& entryID,
367271584abSEd Tanous                                uint64_t& timestamp, uint64_t& index)
36816428a1aSJason M. Bills {
36916428a1aSJason M. Bills     if (entryID.empty())
37016428a1aSJason M. Bills     {
37116428a1aSJason M. Bills         return false;
37216428a1aSJason M. Bills     }
37316428a1aSJason M. Bills     // Convert the unique ID back to a timestamp to find the entry
37439e77504SEd Tanous     std::string_view tsStr(entryID);
37516428a1aSJason M. Bills 
37616428a1aSJason M. Bills     auto underscorePos = tsStr.find("_");
37716428a1aSJason M. Bills     if (underscorePos != tsStr.npos)
37816428a1aSJason M. Bills     {
37916428a1aSJason M. Bills         // Timestamp has an index
38016428a1aSJason M. Bills         tsStr.remove_suffix(tsStr.size() - underscorePos);
38139e77504SEd Tanous         std::string_view indexStr(entryID);
38216428a1aSJason M. Bills         indexStr.remove_prefix(underscorePos + 1);
38316428a1aSJason M. Bills         std::size_t pos;
38416428a1aSJason M. Bills         try
38516428a1aSJason M. Bills         {
38639e77504SEd Tanous             index = std::stoul(std::string(indexStr), &pos);
38716428a1aSJason M. Bills         }
388271584abSEd Tanous         catch (std::invalid_argument&)
38916428a1aSJason M. Bills         {
39016428a1aSJason M. Bills             messages::resourceMissingAtURI(res, entryID);
39116428a1aSJason M. Bills             return false;
39216428a1aSJason M. Bills         }
393271584abSEd Tanous         catch (std::out_of_range&)
39416428a1aSJason M. Bills         {
39516428a1aSJason M. Bills             messages::resourceMissingAtURI(res, entryID);
39616428a1aSJason M. Bills             return false;
39716428a1aSJason M. Bills         }
39816428a1aSJason M. Bills         if (pos != indexStr.size())
39916428a1aSJason M. Bills         {
40016428a1aSJason M. Bills             messages::resourceMissingAtURI(res, entryID);
40116428a1aSJason M. Bills             return false;
40216428a1aSJason M. Bills         }
40316428a1aSJason M. Bills     }
40416428a1aSJason M. Bills     // Timestamp has no index
40516428a1aSJason M. Bills     std::size_t pos;
40616428a1aSJason M. Bills     try
40716428a1aSJason M. Bills     {
40839e77504SEd Tanous         timestamp = std::stoull(std::string(tsStr), &pos);
40916428a1aSJason M. Bills     }
410271584abSEd Tanous     catch (std::invalid_argument&)
41116428a1aSJason M. Bills     {
41216428a1aSJason M. Bills         messages::resourceMissingAtURI(res, entryID);
41316428a1aSJason M. Bills         return false;
41416428a1aSJason M. Bills     }
415271584abSEd Tanous     catch (std::out_of_range&)
41616428a1aSJason M. Bills     {
41716428a1aSJason M. Bills         messages::resourceMissingAtURI(res, entryID);
41816428a1aSJason M. Bills         return false;
41916428a1aSJason M. Bills     }
42016428a1aSJason M. Bills     if (pos != tsStr.size())
42116428a1aSJason M. Bills     {
42216428a1aSJason M. Bills         messages::resourceMissingAtURI(res, entryID);
42316428a1aSJason M. Bills         return false;
42416428a1aSJason M. Bills     }
42516428a1aSJason M. Bills     return true;
42616428a1aSJason M. Bills }
42716428a1aSJason M. Bills 
42895820184SJason M. Bills static bool
42995820184SJason M. Bills     getRedfishLogFiles(std::vector<std::filesystem::path>& redfishLogFiles)
43095820184SJason M. Bills {
43195820184SJason M. Bills     static const std::filesystem::path redfishLogDir = "/var/log";
43295820184SJason M. Bills     static const std::string redfishLogFilename = "redfish";
43395820184SJason M. Bills 
43495820184SJason M. Bills     // Loop through the directory looking for redfish log files
43595820184SJason M. Bills     for (const std::filesystem::directory_entry& dirEnt :
43695820184SJason M. Bills          std::filesystem::directory_iterator(redfishLogDir))
43795820184SJason M. Bills     {
43895820184SJason M. Bills         // If we find a redfish log file, save the path
43995820184SJason M. Bills         std::string filename = dirEnt.path().filename();
44095820184SJason M. Bills         if (boost::starts_with(filename, redfishLogFilename))
44195820184SJason M. Bills         {
44295820184SJason M. Bills             redfishLogFiles.emplace_back(redfishLogDir / filename);
44395820184SJason M. Bills         }
44495820184SJason M. Bills     }
44595820184SJason M. Bills     // As the log files rotate, they are appended with a ".#" that is higher for
44695820184SJason M. Bills     // the older logs. Since we don't expect more than 10 log files, we
44795820184SJason M. Bills     // can just sort the list to get them in order from newest to oldest
44895820184SJason M. Bills     std::sort(redfishLogFiles.begin(), redfishLogFiles.end());
44995820184SJason M. Bills 
45095820184SJason M. Bills     return !redfishLogFiles.empty();
45195820184SJason M. Bills }
45295820184SJason M. Bills 
453043a0536SJohnathan Mantey static void ParseCrashdumpParameters(
454043a0536SJohnathan Mantey     const std::vector<std::pair<std::string, VariantType>>& params,
455043a0536SJohnathan Mantey     std::string& filename, std::string& timestamp, std::string& logfile)
456043a0536SJohnathan Mantey {
457043a0536SJohnathan Mantey     for (auto property : params)
458043a0536SJohnathan Mantey     {
459043a0536SJohnathan Mantey         if (property.first == "Timestamp")
460043a0536SJohnathan Mantey         {
461043a0536SJohnathan Mantey             const std::string* value =
4628d78b7a9SPatrick Williams                 std::get_if<std::string>(&property.second);
463043a0536SJohnathan Mantey             if (value != nullptr)
464043a0536SJohnathan Mantey             {
465043a0536SJohnathan Mantey                 timestamp = *value;
466043a0536SJohnathan Mantey             }
467043a0536SJohnathan Mantey         }
468043a0536SJohnathan Mantey         else if (property.first == "Filename")
469043a0536SJohnathan Mantey         {
470043a0536SJohnathan Mantey             const std::string* value =
4718d78b7a9SPatrick Williams                 std::get_if<std::string>(&property.second);
472043a0536SJohnathan Mantey             if (value != nullptr)
473043a0536SJohnathan Mantey             {
474043a0536SJohnathan Mantey                 filename = *value;
475043a0536SJohnathan Mantey             }
476043a0536SJohnathan Mantey         }
477043a0536SJohnathan Mantey         else if (property.first == "Log")
478043a0536SJohnathan Mantey         {
479043a0536SJohnathan Mantey             const std::string* value =
4808d78b7a9SPatrick Williams                 std::get_if<std::string>(&property.second);
481043a0536SJohnathan Mantey             if (value != nullptr)
482043a0536SJohnathan Mantey             {
483043a0536SJohnathan Mantey                 logfile = *value;
484043a0536SJohnathan Mantey             }
485043a0536SJohnathan Mantey         }
486043a0536SJohnathan Mantey     }
487043a0536SJohnathan Mantey }
488043a0536SJohnathan Mantey 
489a3316fc6SZhikuiRen constexpr char const* postCodeIface = "xyz.openbmc_project.State.Boot.PostCode";
490c4bf6374SJason M. Bills class SystemLogServiceCollection : public Node
4911da66f75SEd Tanous {
4921da66f75SEd Tanous   public:
4931da66f75SEd Tanous     template <typename CrowApp>
494c4bf6374SJason M. Bills     SystemLogServiceCollection(CrowApp& app) :
495029573d4SEd Tanous         Node(app, "/redfish/v1/Systems/system/LogServices/")
496c4bf6374SJason M. Bills     {
497c4bf6374SJason M. Bills         entityPrivileges = {
498c4bf6374SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
499c4bf6374SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
500c4bf6374SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
501c4bf6374SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
502c4bf6374SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
503c4bf6374SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
504c4bf6374SJason M. Bills     }
505c4bf6374SJason M. Bills 
506c4bf6374SJason M. Bills   private:
507c4bf6374SJason M. Bills     /**
508c4bf6374SJason M. Bills      * Functions triggers appropriate requests on DBus
509c4bf6374SJason M. Bills      */
510c4bf6374SJason M. Bills     void doGet(crow::Response& res, const crow::Request& req,
511c4bf6374SJason M. Bills                const std::vector<std::string>& params) override
512c4bf6374SJason M. Bills     {
513c4bf6374SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
514c4bf6374SJason M. Bills         // Collections don't include the static data added by SubRoute because
515c4bf6374SJason M. Bills         // it has a duplicate entry for members
516c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
517c4bf6374SJason M. Bills             "#LogServiceCollection.LogServiceCollection";
518c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
519029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices";
520c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "System Log Services Collection";
521c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] =
522c4bf6374SJason M. Bills             "Collection of LogServices for this Computer System";
523c4bf6374SJason M. Bills         nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
524c4bf6374SJason M. Bills         logServiceArray = nlohmann::json::array();
525029573d4SEd Tanous         logServiceArray.push_back(
526029573d4SEd Tanous             {{"@odata.id", "/redfish/v1/Systems/system/LogServices/EventLog"}});
527c9bb6861Sraviteja-b #ifdef BMCWEB_ENABLE_REDFISH_SYSTEMDUMP_LOG
528c9bb6861Sraviteja-b         logServiceArray.push_back(
529c9bb6861Sraviteja-b             {{"@odata.id", "/redfish/v1/Systems/system/LogServices/System"}});
530c9bb6861Sraviteja-b #endif
531c9bb6861Sraviteja-b 
532d53dd41fSJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
533d53dd41fSJason M. Bills         logServiceArray.push_back(
534cb92c03bSAndrew Geissler             {{"@odata.id",
535424c4176SJason M. Bills               "/redfish/v1/Systems/system/LogServices/Crashdump"}});
536d53dd41fSJason M. Bills #endif
537c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] =
538c4bf6374SJason M. Bills             logServiceArray.size();
539a3316fc6SZhikuiRen 
540a3316fc6SZhikuiRen         crow::connections::systemBus->async_method_call(
541a3316fc6SZhikuiRen             [asyncResp](const boost::system::error_code ec,
542a3316fc6SZhikuiRen                         const std::vector<std::string>& subtreePath) {
543a3316fc6SZhikuiRen                 if (ec)
544a3316fc6SZhikuiRen                 {
545a3316fc6SZhikuiRen                     BMCWEB_LOG_ERROR << ec;
546a3316fc6SZhikuiRen                     return;
547a3316fc6SZhikuiRen                 }
548a3316fc6SZhikuiRen 
549a3316fc6SZhikuiRen                 for (auto& pathStr : subtreePath)
550a3316fc6SZhikuiRen                 {
551a3316fc6SZhikuiRen                     if (pathStr.find("PostCode") != std::string::npos)
552a3316fc6SZhikuiRen                     {
553a3316fc6SZhikuiRen                         nlohmann::json& logServiceArray =
554a3316fc6SZhikuiRen                             asyncResp->res.jsonValue["Members"];
555a3316fc6SZhikuiRen                         logServiceArray.push_back(
556a3316fc6SZhikuiRen                             {{"@odata.id", "/redfish/v1/Systems/system/"
557a3316fc6SZhikuiRen                                            "LogServices/PostCodes"}});
558a3316fc6SZhikuiRen                         asyncResp->res.jsonValue["Members@odata.count"] =
559a3316fc6SZhikuiRen                             logServiceArray.size();
560a3316fc6SZhikuiRen                         return;
561a3316fc6SZhikuiRen                     }
562a3316fc6SZhikuiRen                 }
563a3316fc6SZhikuiRen             },
564a3316fc6SZhikuiRen             "xyz.openbmc_project.ObjectMapper",
565a3316fc6SZhikuiRen             "/xyz/openbmc_project/object_mapper",
566a3316fc6SZhikuiRen             "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", 0,
567a3316fc6SZhikuiRen             std::array<const char*, 1>{postCodeIface});
568c4bf6374SJason M. Bills     }
569c4bf6374SJason M. Bills };
570c4bf6374SJason M. Bills 
571c4bf6374SJason M. Bills class EventLogService : public Node
572c4bf6374SJason M. Bills {
573c4bf6374SJason M. Bills   public:
574c4bf6374SJason M. Bills     template <typename CrowApp>
575c4bf6374SJason M. Bills     EventLogService(CrowApp& app) :
576029573d4SEd Tanous         Node(app, "/redfish/v1/Systems/system/LogServices/EventLog/")
577c4bf6374SJason M. Bills     {
578c4bf6374SJason M. Bills         entityPrivileges = {
579c4bf6374SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
580c4bf6374SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
581c4bf6374SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
582c4bf6374SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
583c4bf6374SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
584c4bf6374SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
585c4bf6374SJason M. Bills     }
586c4bf6374SJason M. Bills 
587c4bf6374SJason M. Bills   private:
588c4bf6374SJason M. Bills     void doGet(crow::Response& res, const crow::Request& req,
589c4bf6374SJason M. Bills                const std::vector<std::string>& params) override
590c4bf6374SJason M. Bills     {
591c4bf6374SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
592c4bf6374SJason M. Bills 
593c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
594029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog";
595c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
596c4bf6374SJason M. Bills             "#LogService.v1_1_0.LogService";
597c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Event Log Service";
598c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] = "System Event Log Service";
599c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Id"] = "EventLog";
600c4bf6374SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
601c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Entries"] = {
602c4bf6374SJason M. Bills             {"@odata.id",
603029573d4SEd Tanous              "/redfish/v1/Systems/system/LogServices/EventLog/Entries"}};
604e7d6c8b2SGunnar Mills         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
605e7d6c8b2SGunnar Mills 
606e7d6c8b2SGunnar Mills             {"target", "/redfish/v1/Systems/system/LogServices/EventLog/"
607e7d6c8b2SGunnar Mills                        "Actions/LogService.ClearLog"}};
608489640c6SJason M. Bills     }
609489640c6SJason M. Bills };
610489640c6SJason M. Bills 
6111f56a3a6STim Lee class JournalEventLogClear : public Node
612489640c6SJason M. Bills {
613489640c6SJason M. Bills   public:
6141f56a3a6STim Lee     JournalEventLogClear(CrowApp& app) :
615489640c6SJason M. Bills         Node(app, "/redfish/v1/Systems/system/LogServices/EventLog/Actions/"
616489640c6SJason M. Bills                   "LogService.ClearLog/")
617489640c6SJason M. Bills     {
618489640c6SJason M. Bills         entityPrivileges = {
619489640c6SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
620489640c6SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
621489640c6SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
622489640c6SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
623489640c6SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
624489640c6SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
625489640c6SJason M. Bills     }
626489640c6SJason M. Bills 
627489640c6SJason M. Bills   private:
628489640c6SJason M. Bills     void doPost(crow::Response& res, const crow::Request& req,
629489640c6SJason M. Bills                 const std::vector<std::string>& params) override
630489640c6SJason M. Bills     {
631489640c6SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
632489640c6SJason M. Bills 
633489640c6SJason M. Bills         // Clear the EventLog by deleting the log files
634489640c6SJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
635489640c6SJason M. Bills         if (getRedfishLogFiles(redfishLogFiles))
636489640c6SJason M. Bills         {
637489640c6SJason M. Bills             for (const std::filesystem::path& file : redfishLogFiles)
638489640c6SJason M. Bills             {
639489640c6SJason M. Bills                 std::error_code ec;
640489640c6SJason M. Bills                 std::filesystem::remove(file, ec);
641489640c6SJason M. Bills             }
642489640c6SJason M. Bills         }
643489640c6SJason M. Bills 
644489640c6SJason M. Bills         // Reload rsyslog so it knows to start new log files
645489640c6SJason M. Bills         crow::connections::systemBus->async_method_call(
646489640c6SJason M. Bills             [asyncResp](const boost::system::error_code ec) {
647489640c6SJason M. Bills                 if (ec)
648489640c6SJason M. Bills                 {
649489640c6SJason M. Bills                     BMCWEB_LOG_ERROR << "Failed to reload rsyslog: " << ec;
650489640c6SJason M. Bills                     messages::internalError(asyncResp->res);
651489640c6SJason M. Bills                     return;
652489640c6SJason M. Bills                 }
653489640c6SJason M. Bills 
654489640c6SJason M. Bills                 messages::success(asyncResp->res);
655489640c6SJason M. Bills             },
656489640c6SJason M. Bills             "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
657489640c6SJason M. Bills             "org.freedesktop.systemd1.Manager", "ReloadUnit", "rsyslog.service",
658489640c6SJason M. Bills             "replace");
659c4bf6374SJason M. Bills     }
660c4bf6374SJason M. Bills };
661c4bf6374SJason M. Bills 
66295820184SJason M. Bills static int fillEventLogEntryJson(const std::string& logEntryID,
66395820184SJason M. Bills                                  const std::string logEntry,
66495820184SJason M. Bills                                  nlohmann::json& logEntryJson)
665c4bf6374SJason M. Bills {
66695820184SJason M. Bills     // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
667cd225da8SJason M. Bills     // First get the Timestamp
668cd225da8SJason M. Bills     size_t space = logEntry.find_first_of(" ");
669cd225da8SJason M. Bills     if (space == std::string::npos)
67095820184SJason M. Bills     {
67195820184SJason M. Bills         return 1;
67295820184SJason M. Bills     }
673cd225da8SJason M. Bills     std::string timestamp = logEntry.substr(0, space);
674cd225da8SJason M. Bills     // Then get the log contents
675cd225da8SJason M. Bills     size_t entryStart = logEntry.find_first_not_of(" ", space);
676cd225da8SJason M. Bills     if (entryStart == std::string::npos)
677cd225da8SJason M. Bills     {
678cd225da8SJason M. Bills         return 1;
679cd225da8SJason M. Bills     }
680cd225da8SJason M. Bills     std::string_view entry(logEntry);
681cd225da8SJason M. Bills     entry.remove_prefix(entryStart);
682cd225da8SJason M. Bills     // Use split to separate the entry into its fields
683cd225da8SJason M. Bills     std::vector<std::string> logEntryFields;
684cd225da8SJason M. Bills     boost::split(logEntryFields, entry, boost::is_any_of(","),
685cd225da8SJason M. Bills                  boost::token_compress_on);
686cd225da8SJason M. Bills     // We need at least a MessageId to be valid
687cd225da8SJason M. Bills     if (logEntryFields.size() < 1)
688cd225da8SJason M. Bills     {
689cd225da8SJason M. Bills         return 1;
690cd225da8SJason M. Bills     }
691cd225da8SJason M. Bills     std::string& messageID = logEntryFields[0];
69295820184SJason M. Bills 
6934851d45dSJason M. Bills     // Get the Message from the MessageRegistry
6944851d45dSJason M. Bills     const message_registries::Message* message =
6954851d45dSJason M. Bills         message_registries::getMessage(messageID);
696c4bf6374SJason M. Bills 
6974851d45dSJason M. Bills     std::string msg;
6984851d45dSJason M. Bills     std::string severity;
6994851d45dSJason M. Bills     if (message != nullptr)
700c4bf6374SJason M. Bills     {
7014851d45dSJason M. Bills         msg = message->message;
7024851d45dSJason M. Bills         severity = message->severity;
703c4bf6374SJason M. Bills     }
704c4bf6374SJason M. Bills 
70515a86ff6SJason M. Bills     // Get the MessageArgs from the log if there are any
70615a86ff6SJason M. Bills     boost::beast::span<std::string> messageArgs;
70715a86ff6SJason M. Bills     if (logEntryFields.size() > 1)
70815a86ff6SJason M. Bills     {
70915a86ff6SJason M. Bills         std::string& messageArgsStart = logEntryFields[1];
71015a86ff6SJason M. Bills         // If the first string is empty, assume there are no MessageArgs
71115a86ff6SJason M. Bills         std::size_t messageArgsSize = 0;
71215a86ff6SJason M. Bills         if (!messageArgsStart.empty())
71315a86ff6SJason M. Bills         {
71415a86ff6SJason M. Bills             messageArgsSize = logEntryFields.size() - 1;
71515a86ff6SJason M. Bills         }
71615a86ff6SJason M. Bills 
71715a86ff6SJason M. Bills         messageArgs = boost::beast::span(&messageArgsStart, messageArgsSize);
718c4bf6374SJason M. Bills 
7194851d45dSJason M. Bills         // Fill the MessageArgs into the Message
72095820184SJason M. Bills         int i = 0;
72195820184SJason M. Bills         for (const std::string& messageArg : messageArgs)
7224851d45dSJason M. Bills         {
72395820184SJason M. Bills             std::string argStr = "%" + std::to_string(++i);
7244851d45dSJason M. Bills             size_t argPos = msg.find(argStr);
7254851d45dSJason M. Bills             if (argPos != std::string::npos)
7264851d45dSJason M. Bills             {
72795820184SJason M. Bills                 msg.replace(argPos, argStr.length(), messageArg);
7284851d45dSJason M. Bills             }
7294851d45dSJason M. Bills         }
73015a86ff6SJason M. Bills     }
7314851d45dSJason M. Bills 
73295820184SJason M. Bills     // Get the Created time from the timestamp. The log timestamp is in RFC3339
73395820184SJason M. Bills     // format which matches the Redfish format except for the fractional seconds
73495820184SJason M. Bills     // between the '.' and the '+', so just remove them.
73595820184SJason M. Bills     std::size_t dot = timestamp.find_first_of(".");
73695820184SJason M. Bills     std::size_t plus = timestamp.find_first_of("+");
73795820184SJason M. Bills     if (dot != std::string::npos && plus != std::string::npos)
738c4bf6374SJason M. Bills     {
73995820184SJason M. Bills         timestamp.erase(dot, plus - dot);
740c4bf6374SJason M. Bills     }
741c4bf6374SJason M. Bills 
742c4bf6374SJason M. Bills     // Fill in the log entry with the gathered data
74395820184SJason M. Bills     logEntryJson = {
744cb92c03bSAndrew Geissler         {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
745029573d4SEd Tanous         {"@odata.id",
746897967deSJason M. Bills          "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
74795820184SJason M. Bills              logEntryID},
748c4bf6374SJason M. Bills         {"Name", "System Event Log Entry"},
74995820184SJason M. Bills         {"Id", logEntryID},
75095820184SJason M. Bills         {"Message", std::move(msg)},
75195820184SJason M. Bills         {"MessageId", std::move(messageID)},
752c4bf6374SJason M. Bills         {"MessageArgs", std::move(messageArgs)},
753c4bf6374SJason M. Bills         {"EntryType", "Event"},
75495820184SJason M. Bills         {"Severity", std::move(severity)},
75595820184SJason M. Bills         {"Created", std::move(timestamp)}};
756c4bf6374SJason M. Bills     return 0;
757c4bf6374SJason M. Bills }
758c4bf6374SJason M. Bills 
75927062605SAnthony Wilson class JournalEventLogEntryCollection : public Node
760c4bf6374SJason M. Bills {
761c4bf6374SJason M. Bills   public:
762c4bf6374SJason M. Bills     template <typename CrowApp>
76327062605SAnthony Wilson     JournalEventLogEntryCollection(CrowApp& app) :
764029573d4SEd Tanous         Node(app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
765c4bf6374SJason M. Bills     {
766c4bf6374SJason M. Bills         entityPrivileges = {
767c4bf6374SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
768c4bf6374SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
769c4bf6374SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
770c4bf6374SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
771c4bf6374SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
772c4bf6374SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
773c4bf6374SJason M. Bills     }
774c4bf6374SJason M. Bills 
775c4bf6374SJason M. Bills   private:
776c4bf6374SJason M. Bills     void doGet(crow::Response& res, const crow::Request& req,
777c4bf6374SJason M. Bills                const std::vector<std::string>& params) override
778c4bf6374SJason M. Bills     {
779c4bf6374SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
780271584abSEd Tanous         uint64_t skip = 0;
781271584abSEd Tanous         uint64_t top = maxEntriesPerPage; // Show max entries by default
782c4bf6374SJason M. Bills         if (!getSkipParam(asyncResp->res, req, skip))
783c4bf6374SJason M. Bills         {
784c4bf6374SJason M. Bills             return;
785c4bf6374SJason M. Bills         }
786c4bf6374SJason M. Bills         if (!getTopParam(asyncResp->res, req, top))
787c4bf6374SJason M. Bills         {
788c4bf6374SJason M. Bills             return;
789c4bf6374SJason M. Bills         }
790c4bf6374SJason M. Bills         // Collections don't include the static data added by SubRoute because
791c4bf6374SJason M. Bills         // it has a duplicate entry for members
792c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
793c4bf6374SJason M. Bills             "#LogEntryCollection.LogEntryCollection";
794c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
795029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
796c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
797c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] =
798c4bf6374SJason M. Bills             "Collection of System Event Log Entries";
799cb92c03bSAndrew Geissler 
800c4bf6374SJason M. Bills         nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
801c4bf6374SJason M. Bills         logEntryArray = nlohmann::json::array();
80295820184SJason M. Bills         // Go through the log files and create a unique ID for each entry
80395820184SJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
80495820184SJason M. Bills         getRedfishLogFiles(redfishLogFiles);
805b01bf299SEd Tanous         uint64_t entryCount = 0;
806cd225da8SJason M. Bills         std::string logEntry;
80795820184SJason M. Bills 
80895820184SJason M. Bills         // Oldest logs are in the last file, so start there and loop backwards
809cd225da8SJason M. Bills         for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
810cd225da8SJason M. Bills              it++)
811c4bf6374SJason M. Bills         {
812cd225da8SJason M. Bills             std::ifstream logStream(*it);
81395820184SJason M. Bills             if (!logStream.is_open())
814c4bf6374SJason M. Bills             {
815c4bf6374SJason M. Bills                 continue;
816c4bf6374SJason M. Bills             }
817c4bf6374SJason M. Bills 
818e85d6b16SJason M. Bills             // Reset the unique ID on the first entry
819e85d6b16SJason M. Bills             bool firstEntry = true;
82095820184SJason M. Bills             while (std::getline(logStream, logEntry))
82195820184SJason M. Bills             {
822c4bf6374SJason M. Bills                 entryCount++;
823c4bf6374SJason M. Bills                 // Handle paging using skip (number of entries to skip from the
824c4bf6374SJason M. Bills                 // start) and top (number of entries to display)
825c4bf6374SJason M. Bills                 if (entryCount <= skip || entryCount > skip + top)
826c4bf6374SJason M. Bills                 {
827c4bf6374SJason M. Bills                     continue;
828c4bf6374SJason M. Bills                 }
829c4bf6374SJason M. Bills 
830c4bf6374SJason M. Bills                 std::string idStr;
831e85d6b16SJason M. Bills                 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
832c4bf6374SJason M. Bills                 {
833c4bf6374SJason M. Bills                     continue;
834c4bf6374SJason M. Bills                 }
835c4bf6374SJason M. Bills 
836e85d6b16SJason M. Bills                 if (firstEntry)
837e85d6b16SJason M. Bills                 {
838e85d6b16SJason M. Bills                     firstEntry = false;
839e85d6b16SJason M. Bills                 }
840e85d6b16SJason M. Bills 
841c4bf6374SJason M. Bills                 logEntryArray.push_back({});
842c4bf6374SJason M. Bills                 nlohmann::json& bmcLogEntry = logEntryArray.back();
84395820184SJason M. Bills                 if (fillEventLogEntryJson(idStr, logEntry, bmcLogEntry) != 0)
844c4bf6374SJason M. Bills                 {
845c4bf6374SJason M. Bills                     messages::internalError(asyncResp->res);
846c4bf6374SJason M. Bills                     return;
847c4bf6374SJason M. Bills                 }
848c4bf6374SJason M. Bills             }
84995820184SJason M. Bills         }
850c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
851c4bf6374SJason M. Bills         if (skip + top < entryCount)
852c4bf6374SJason M. Bills         {
853c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Members@odata.nextLink"] =
85495820184SJason M. Bills                 "/redfish/v1/Systems/system/LogServices/EventLog/"
85595820184SJason M. Bills                 "Entries?$skip=" +
856c4bf6374SJason M. Bills                 std::to_string(skip + top);
857c4bf6374SJason M. Bills         }
85808a4e4b5SAnthony Wilson     }
85908a4e4b5SAnthony Wilson };
86008a4e4b5SAnthony Wilson 
861897967deSJason M. Bills class JournalEventLogEntry : public Node
862897967deSJason M. Bills {
863897967deSJason M. Bills   public:
864897967deSJason M. Bills     JournalEventLogEntry(CrowApp& app) :
865897967deSJason M. Bills         Node(app,
866897967deSJason M. Bills              "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/",
867897967deSJason M. Bills              std::string())
868897967deSJason M. Bills     {
869897967deSJason M. Bills         entityPrivileges = {
870897967deSJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
871897967deSJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
872897967deSJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
873897967deSJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
874897967deSJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
875897967deSJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
876897967deSJason M. Bills     }
877897967deSJason M. Bills 
878897967deSJason M. Bills   private:
879897967deSJason M. Bills     void doGet(crow::Response& res, const crow::Request& req,
880897967deSJason M. Bills                const std::vector<std::string>& params) override
881897967deSJason M. Bills     {
882897967deSJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
883897967deSJason M. Bills         if (params.size() != 1)
884897967deSJason M. Bills         {
885897967deSJason M. Bills             messages::internalError(asyncResp->res);
886897967deSJason M. Bills             return;
887897967deSJason M. Bills         }
888897967deSJason M. Bills         const std::string& targetID = params[0];
889897967deSJason M. Bills 
890897967deSJason M. Bills         // Go through the log files and check the unique ID for each entry to
891897967deSJason M. Bills         // find the target entry
892897967deSJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
893897967deSJason M. Bills         getRedfishLogFiles(redfishLogFiles);
894897967deSJason M. Bills         std::string logEntry;
895897967deSJason M. Bills 
896897967deSJason M. Bills         // Oldest logs are in the last file, so start there and loop backwards
897897967deSJason M. Bills         for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
898897967deSJason M. Bills              it++)
899897967deSJason M. Bills         {
900897967deSJason M. Bills             std::ifstream logStream(*it);
901897967deSJason M. Bills             if (!logStream.is_open())
902897967deSJason M. Bills             {
903897967deSJason M. Bills                 continue;
904897967deSJason M. Bills             }
905897967deSJason M. Bills 
906897967deSJason M. Bills             // Reset the unique ID on the first entry
907897967deSJason M. Bills             bool firstEntry = true;
908897967deSJason M. Bills             while (std::getline(logStream, logEntry))
909897967deSJason M. Bills             {
910897967deSJason M. Bills                 std::string idStr;
911897967deSJason M. Bills                 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
912897967deSJason M. Bills                 {
913897967deSJason M. Bills                     continue;
914897967deSJason M. Bills                 }
915897967deSJason M. Bills 
916897967deSJason M. Bills                 if (firstEntry)
917897967deSJason M. Bills                 {
918897967deSJason M. Bills                     firstEntry = false;
919897967deSJason M. Bills                 }
920897967deSJason M. Bills 
921897967deSJason M. Bills                 if (idStr == targetID)
922897967deSJason M. Bills                 {
923897967deSJason M. Bills                     if (fillEventLogEntryJson(idStr, logEntry,
924897967deSJason M. Bills                                               asyncResp->res.jsonValue) != 0)
925897967deSJason M. Bills                     {
926897967deSJason M. Bills                         messages::internalError(asyncResp->res);
927897967deSJason M. Bills                         return;
928897967deSJason M. Bills                     }
929897967deSJason M. Bills                     return;
930897967deSJason M. Bills                 }
931897967deSJason M. Bills             }
932897967deSJason M. Bills         }
933897967deSJason M. Bills         // Requested ID was not found
934897967deSJason M. Bills         messages::resourceMissingAtURI(asyncResp->res, targetID);
935897967deSJason M. Bills     }
936897967deSJason M. Bills };
937897967deSJason M. Bills 
93808a4e4b5SAnthony Wilson class DBusEventLogEntryCollection : public Node
93908a4e4b5SAnthony Wilson {
94008a4e4b5SAnthony Wilson   public:
94108a4e4b5SAnthony Wilson     template <typename CrowApp>
94208a4e4b5SAnthony Wilson     DBusEventLogEntryCollection(CrowApp& app) :
94308a4e4b5SAnthony Wilson         Node(app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
94408a4e4b5SAnthony Wilson     {
94508a4e4b5SAnthony Wilson         entityPrivileges = {
94608a4e4b5SAnthony Wilson             {boost::beast::http::verb::get, {{"Login"}}},
94708a4e4b5SAnthony Wilson             {boost::beast::http::verb::head, {{"Login"}}},
94808a4e4b5SAnthony Wilson             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
94908a4e4b5SAnthony Wilson             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
95008a4e4b5SAnthony Wilson             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
95108a4e4b5SAnthony Wilson             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
95208a4e4b5SAnthony Wilson     }
95308a4e4b5SAnthony Wilson 
95408a4e4b5SAnthony Wilson   private:
95508a4e4b5SAnthony Wilson     void doGet(crow::Response& res, const crow::Request& req,
95608a4e4b5SAnthony Wilson                const std::vector<std::string>& params) override
95708a4e4b5SAnthony Wilson     {
95808a4e4b5SAnthony Wilson         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
95908a4e4b5SAnthony Wilson 
96008a4e4b5SAnthony Wilson         // Collections don't include the static data added by SubRoute because
96108a4e4b5SAnthony Wilson         // it has a duplicate entry for members
96208a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["@odata.type"] =
96308a4e4b5SAnthony Wilson             "#LogEntryCollection.LogEntryCollection";
96408a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["@odata.id"] =
96508a4e4b5SAnthony Wilson             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
96608a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
96708a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["Description"] =
96808a4e4b5SAnthony Wilson             "Collection of System Event Log Entries";
96908a4e4b5SAnthony Wilson 
970cb92c03bSAndrew Geissler         // DBus implementation of EventLog/Entries
971cb92c03bSAndrew Geissler         // Make call to Logging Service to find all log entry objects
972cb92c03bSAndrew Geissler         crow::connections::systemBus->async_method_call(
973cb92c03bSAndrew Geissler             [asyncResp](const boost::system::error_code ec,
974cb92c03bSAndrew Geissler                         GetManagedObjectsType& resp) {
975cb92c03bSAndrew Geissler                 if (ec)
976cb92c03bSAndrew Geissler                 {
977cb92c03bSAndrew Geissler                     // TODO Handle for specific error code
978cb92c03bSAndrew Geissler                     BMCWEB_LOG_ERROR
979cb92c03bSAndrew Geissler                         << "getLogEntriesIfaceData resp_handler got error "
980cb92c03bSAndrew Geissler                         << ec;
981cb92c03bSAndrew Geissler                     messages::internalError(asyncResp->res);
982cb92c03bSAndrew Geissler                     return;
983cb92c03bSAndrew Geissler                 }
984cb92c03bSAndrew Geissler                 nlohmann::json& entriesArray =
985cb92c03bSAndrew Geissler                     asyncResp->res.jsonValue["Members"];
986cb92c03bSAndrew Geissler                 entriesArray = nlohmann::json::array();
987cb92c03bSAndrew Geissler                 for (auto& objectPath : resp)
988cb92c03bSAndrew Geissler                 {
989cb92c03bSAndrew Geissler                     for (auto& interfaceMap : objectPath.second)
990cb92c03bSAndrew Geissler                     {
991cb92c03bSAndrew Geissler                         if (interfaceMap.first !=
992cb92c03bSAndrew Geissler                             "xyz.openbmc_project.Logging.Entry")
993cb92c03bSAndrew Geissler                         {
994cb92c03bSAndrew Geissler                             BMCWEB_LOG_DEBUG << "Bailing early on "
995cb92c03bSAndrew Geissler                                              << interfaceMap.first;
996cb92c03bSAndrew Geissler                             continue;
997cb92c03bSAndrew Geissler                         }
998cb92c03bSAndrew Geissler                         entriesArray.push_back({});
999cb92c03bSAndrew Geissler                         nlohmann::json& thisEntry = entriesArray.back();
100066664f25SEd Tanous                         uint32_t* id = nullptr;
100166664f25SEd Tanous                         std::time_t timestamp{};
100266664f25SEd Tanous                         std::string* severity = nullptr;
100366664f25SEd Tanous                         std::string* message = nullptr;
1004cb92c03bSAndrew Geissler                         for (auto& propertyMap : interfaceMap.second)
1005cb92c03bSAndrew Geissler                         {
1006cb92c03bSAndrew Geissler                             if (propertyMap.first == "Id")
1007cb92c03bSAndrew Geissler                             {
10088d78b7a9SPatrick Williams                                 id = std::get_if<uint32_t>(&propertyMap.second);
1009cb92c03bSAndrew Geissler                                 if (id == nullptr)
1010cb92c03bSAndrew Geissler                                 {
1011cb92c03bSAndrew Geissler                                     messages::propertyMissing(asyncResp->res,
1012cb92c03bSAndrew Geissler                                                               "Id");
1013cb92c03bSAndrew Geissler                                 }
1014cb92c03bSAndrew Geissler                             }
1015cb92c03bSAndrew Geissler                             else if (propertyMap.first == "Timestamp")
1016cb92c03bSAndrew Geissler                             {
1017cb92c03bSAndrew Geissler                                 const uint64_t* millisTimeStamp =
1018cb92c03bSAndrew Geissler                                     std::get_if<uint64_t>(&propertyMap.second);
1019cb92c03bSAndrew Geissler                                 if (millisTimeStamp == nullptr)
1020cb92c03bSAndrew Geissler                                 {
1021cb92c03bSAndrew Geissler                                     messages::propertyMissing(asyncResp->res,
1022cb92c03bSAndrew Geissler                                                               "Timestamp");
1023271584abSEd Tanous                                     continue;
1024cb92c03bSAndrew Geissler                                 }
1025cb92c03bSAndrew Geissler                                 // Retrieve Created property with format:
1026cb92c03bSAndrew Geissler                                 // yyyy-mm-ddThh:mm:ss
1027cb92c03bSAndrew Geissler                                 std::chrono::milliseconds chronoTimeStamp(
1028cb92c03bSAndrew Geissler                                     *millisTimeStamp);
1029271584abSEd Tanous                                 timestamp = std::chrono::duration_cast<
1030271584abSEd Tanous                                                 std::chrono::duration<int>>(
1031271584abSEd Tanous                                                 chronoTimeStamp)
1032cb92c03bSAndrew Geissler                                                 .count();
1033cb92c03bSAndrew Geissler                             }
1034cb92c03bSAndrew Geissler                             else if (propertyMap.first == "Severity")
1035cb92c03bSAndrew Geissler                             {
1036cb92c03bSAndrew Geissler                                 severity = std::get_if<std::string>(
1037cb92c03bSAndrew Geissler                                     &propertyMap.second);
1038cb92c03bSAndrew Geissler                                 if (severity == nullptr)
1039cb92c03bSAndrew Geissler                                 {
1040cb92c03bSAndrew Geissler                                     messages::propertyMissing(asyncResp->res,
1041cb92c03bSAndrew Geissler                                                               "Severity");
1042cb92c03bSAndrew Geissler                                 }
1043cb92c03bSAndrew Geissler                             }
1044cb92c03bSAndrew Geissler                             else if (propertyMap.first == "Message")
1045cb92c03bSAndrew Geissler                             {
1046cb92c03bSAndrew Geissler                                 message = std::get_if<std::string>(
1047cb92c03bSAndrew Geissler                                     &propertyMap.second);
1048cb92c03bSAndrew Geissler                                 if (message == nullptr)
1049cb92c03bSAndrew Geissler                                 {
1050cb92c03bSAndrew Geissler                                     messages::propertyMissing(asyncResp->res,
1051cb92c03bSAndrew Geissler                                                               "Message");
1052cb92c03bSAndrew Geissler                                 }
1053cb92c03bSAndrew Geissler                             }
1054cb92c03bSAndrew Geissler                         }
1055cb92c03bSAndrew Geissler                         thisEntry = {
1056cb92c03bSAndrew Geissler                             {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
1057cb92c03bSAndrew Geissler                             {"@odata.id",
1058cb92c03bSAndrew Geissler                              "/redfish/v1/Systems/system/LogServices/EventLog/"
1059cb92c03bSAndrew Geissler                              "Entries/" +
1060cb92c03bSAndrew Geissler                                  std::to_string(*id)},
106127062605SAnthony Wilson                             {"Name", "System Event Log Entry"},
1062cb92c03bSAndrew Geissler                             {"Id", std::to_string(*id)},
1063cb92c03bSAndrew Geissler                             {"Message", *message},
1064cb92c03bSAndrew Geissler                             {"EntryType", "Event"},
1065cb92c03bSAndrew Geissler                             {"Severity",
1066cb92c03bSAndrew Geissler                              translateSeverityDbusToRedfish(*severity)},
1067cb92c03bSAndrew Geissler                             {"Created", crow::utility::getDateTime(timestamp)}};
1068cb92c03bSAndrew Geissler                     }
1069cb92c03bSAndrew Geissler                 }
1070cb92c03bSAndrew Geissler                 std::sort(entriesArray.begin(), entriesArray.end(),
1071cb92c03bSAndrew Geissler                           [](const nlohmann::json& left,
1072cb92c03bSAndrew Geissler                              const nlohmann::json& right) {
1073cb92c03bSAndrew Geissler                               return (left["Id"] <= right["Id"]);
1074cb92c03bSAndrew Geissler                           });
1075cb92c03bSAndrew Geissler                 asyncResp->res.jsonValue["Members@odata.count"] =
1076cb92c03bSAndrew Geissler                     entriesArray.size();
1077cb92c03bSAndrew Geissler             },
1078cb92c03bSAndrew Geissler             "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
1079cb92c03bSAndrew Geissler             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1080c4bf6374SJason M. Bills     }
1081c4bf6374SJason M. Bills };
1082c4bf6374SJason M. Bills 
108308a4e4b5SAnthony Wilson class DBusEventLogEntry : public Node
1084c4bf6374SJason M. Bills {
1085c4bf6374SJason M. Bills   public:
108608a4e4b5SAnthony Wilson     DBusEventLogEntry(CrowApp& app) :
1087c4bf6374SJason M. Bills         Node(app,
1088029573d4SEd Tanous              "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/",
1089029573d4SEd Tanous              std::string())
1090c4bf6374SJason M. Bills     {
1091c4bf6374SJason M. Bills         entityPrivileges = {
1092c4bf6374SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1093c4bf6374SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1094c4bf6374SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1095c4bf6374SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1096c4bf6374SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1097c4bf6374SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1098c4bf6374SJason M. Bills     }
1099c4bf6374SJason M. Bills 
1100c4bf6374SJason M. Bills   private:
1101c4bf6374SJason M. Bills     void doGet(crow::Response& res, const crow::Request& req,
1102c4bf6374SJason M. Bills                const std::vector<std::string>& params) override
1103c4bf6374SJason M. Bills     {
1104c4bf6374SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1105029573d4SEd Tanous         if (params.size() != 1)
1106c4bf6374SJason M. Bills         {
1107c4bf6374SJason M. Bills             messages::internalError(asyncResp->res);
1108c4bf6374SJason M. Bills             return;
1109c4bf6374SJason M. Bills         }
1110029573d4SEd Tanous         const std::string& entryID = params[0];
1111cb92c03bSAndrew Geissler 
1112cb92c03bSAndrew Geissler         // DBus implementation of EventLog/Entries
1113cb92c03bSAndrew Geissler         // Make call to Logging Service to find all log entry objects
1114cb92c03bSAndrew Geissler         crow::connections::systemBus->async_method_call(
1115cb92c03bSAndrew Geissler             [asyncResp, entryID](const boost::system::error_code ec,
1116cb92c03bSAndrew Geissler                                  GetManagedPropertyType& resp) {
1117cb92c03bSAndrew Geissler                 if (ec)
1118cb92c03bSAndrew Geissler                 {
1119cb92c03bSAndrew Geissler                     BMCWEB_LOG_ERROR
1120cb92c03bSAndrew Geissler                         << "EventLogEntry (DBus) resp_handler got error " << ec;
1121cb92c03bSAndrew Geissler                     messages::internalError(asyncResp->res);
1122cb92c03bSAndrew Geissler                     return;
1123cb92c03bSAndrew Geissler                 }
112466664f25SEd Tanous                 uint32_t* id = nullptr;
112566664f25SEd Tanous                 std::time_t timestamp{};
112666664f25SEd Tanous                 std::string* severity = nullptr;
112766664f25SEd Tanous                 std::string* message = nullptr;
1128cb92c03bSAndrew Geissler                 for (auto& propertyMap : resp)
1129cb92c03bSAndrew Geissler                 {
1130cb92c03bSAndrew Geissler                     if (propertyMap.first == "Id")
1131cb92c03bSAndrew Geissler                     {
1132cb92c03bSAndrew Geissler                         id = std::get_if<uint32_t>(&propertyMap.second);
1133cb92c03bSAndrew Geissler                         if (id == nullptr)
1134cb92c03bSAndrew Geissler                         {
1135cb92c03bSAndrew Geissler                             messages::propertyMissing(asyncResp->res, "Id");
1136cb92c03bSAndrew Geissler                         }
1137cb92c03bSAndrew Geissler                     }
1138cb92c03bSAndrew Geissler                     else if (propertyMap.first == "Timestamp")
1139cb92c03bSAndrew Geissler                     {
1140cb92c03bSAndrew Geissler                         const uint64_t* millisTimeStamp =
1141cb92c03bSAndrew Geissler                             std::get_if<uint64_t>(&propertyMap.second);
1142cb92c03bSAndrew Geissler                         if (millisTimeStamp == nullptr)
1143cb92c03bSAndrew Geissler                         {
1144cb92c03bSAndrew Geissler                             messages::propertyMissing(asyncResp->res,
1145cb92c03bSAndrew Geissler                                                       "Timestamp");
1146271584abSEd Tanous                             continue;
1147cb92c03bSAndrew Geissler                         }
1148cb92c03bSAndrew Geissler                         // Retrieve Created property with format:
1149cb92c03bSAndrew Geissler                         // yyyy-mm-ddThh:mm:ss
1150cb92c03bSAndrew Geissler                         std::chrono::milliseconds chronoTimeStamp(
1151cb92c03bSAndrew Geissler                             *millisTimeStamp);
1152cb92c03bSAndrew Geissler                         timestamp =
1153271584abSEd Tanous                             std::chrono::duration_cast<
1154271584abSEd Tanous                                 std::chrono::duration<int>>(chronoTimeStamp)
1155cb92c03bSAndrew Geissler                                 .count();
1156cb92c03bSAndrew Geissler                     }
1157cb92c03bSAndrew Geissler                     else if (propertyMap.first == "Severity")
1158cb92c03bSAndrew Geissler                     {
1159cb92c03bSAndrew Geissler                         severity =
1160cb92c03bSAndrew Geissler                             std::get_if<std::string>(&propertyMap.second);
1161cb92c03bSAndrew Geissler                         if (severity == nullptr)
1162cb92c03bSAndrew Geissler                         {
1163cb92c03bSAndrew Geissler                             messages::propertyMissing(asyncResp->res,
1164cb92c03bSAndrew Geissler                                                       "Severity");
1165cb92c03bSAndrew Geissler                         }
1166cb92c03bSAndrew Geissler                     }
1167cb92c03bSAndrew Geissler                     else if (propertyMap.first == "Message")
1168cb92c03bSAndrew Geissler                     {
1169cb92c03bSAndrew Geissler                         message = std::get_if<std::string>(&propertyMap.second);
1170cb92c03bSAndrew Geissler                         if (message == nullptr)
1171cb92c03bSAndrew Geissler                         {
1172cb92c03bSAndrew Geissler                             messages::propertyMissing(asyncResp->res,
1173cb92c03bSAndrew Geissler                                                       "Message");
1174cb92c03bSAndrew Geissler                         }
1175cb92c03bSAndrew Geissler                     }
1176cb92c03bSAndrew Geissler                 }
1177271584abSEd Tanous                 if (id == nullptr || message == nullptr || severity == nullptr)
1178271584abSEd Tanous                 {
1179271584abSEd Tanous                     return;
1180271584abSEd Tanous                 }
1181cb92c03bSAndrew Geissler                 asyncResp->res.jsonValue = {
1182cb92c03bSAndrew Geissler                     {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
1183cb92c03bSAndrew Geissler                     {"@odata.id",
1184cb92c03bSAndrew Geissler                      "/redfish/v1/Systems/system/LogServices/EventLog/"
1185cb92c03bSAndrew Geissler                      "Entries/" +
1186cb92c03bSAndrew Geissler                          std::to_string(*id)},
118727062605SAnthony Wilson                     {"Name", "System Event Log Entry"},
1188cb92c03bSAndrew Geissler                     {"Id", std::to_string(*id)},
1189cb92c03bSAndrew Geissler                     {"Message", *message},
1190cb92c03bSAndrew Geissler                     {"EntryType", "Event"},
1191cb92c03bSAndrew Geissler                     {"Severity", translateSeverityDbusToRedfish(*severity)},
119208a4e4b5SAnthony Wilson                     {"Created", crow::utility::getDateTime(timestamp)}};
1193cb92c03bSAndrew Geissler             },
1194cb92c03bSAndrew Geissler             "xyz.openbmc_project.Logging",
1195cb92c03bSAndrew Geissler             "/xyz/openbmc_project/logging/entry/" + entryID,
1196cb92c03bSAndrew Geissler             "org.freedesktop.DBus.Properties", "GetAll",
1197cb92c03bSAndrew Geissler             "xyz.openbmc_project.Logging.Entry");
1198c4bf6374SJason M. Bills     }
1199336e96c6SChicago Duan 
1200336e96c6SChicago Duan     void doDelete(crow::Response& res, const crow::Request& req,
1201336e96c6SChicago Duan                   const std::vector<std::string>& params) override
1202336e96c6SChicago Duan     {
1203336e96c6SChicago Duan 
1204336e96c6SChicago Duan         BMCWEB_LOG_DEBUG << "Do delete single event entries.";
1205336e96c6SChicago Duan 
1206336e96c6SChicago Duan         auto asyncResp = std::make_shared<AsyncResp>(res);
1207336e96c6SChicago Duan 
1208336e96c6SChicago Duan         if (params.size() != 1)
1209336e96c6SChicago Duan         {
1210336e96c6SChicago Duan             messages::internalError(asyncResp->res);
1211336e96c6SChicago Duan             return;
1212336e96c6SChicago Duan         }
1213336e96c6SChicago Duan         std::string entryID = params[0];
1214336e96c6SChicago Duan 
1215336e96c6SChicago Duan         dbus::utility::escapePathForDbus(entryID);
1216336e96c6SChicago Duan 
1217336e96c6SChicago Duan         // Process response from Logging service.
1218336e96c6SChicago Duan         auto respHandler = [asyncResp](const boost::system::error_code ec) {
1219336e96c6SChicago Duan             BMCWEB_LOG_DEBUG << "EventLogEntry (DBus) doDelete callback: Done";
1220336e96c6SChicago Duan             if (ec)
1221336e96c6SChicago Duan             {
1222336e96c6SChicago Duan                 // TODO Handle for specific error code
1223336e96c6SChicago Duan                 BMCWEB_LOG_ERROR
1224336e96c6SChicago Duan                     << "EventLogEntry (DBus) doDelete respHandler got error "
1225336e96c6SChicago Duan                     << ec;
1226336e96c6SChicago Duan                 asyncResp->res.result(
1227336e96c6SChicago Duan                     boost::beast::http::status::internal_server_error);
1228336e96c6SChicago Duan                 return;
1229336e96c6SChicago Duan             }
1230336e96c6SChicago Duan 
1231336e96c6SChicago Duan             asyncResp->res.result(boost::beast::http::status::ok);
1232336e96c6SChicago Duan         };
1233336e96c6SChicago Duan 
1234336e96c6SChicago Duan         // Make call to Logging service to request Delete Log
1235336e96c6SChicago Duan         crow::connections::systemBus->async_method_call(
1236336e96c6SChicago Duan             respHandler, "xyz.openbmc_project.Logging",
1237336e96c6SChicago Duan             "/xyz/openbmc_project/logging/entry/" + entryID,
1238336e96c6SChicago Duan             "xyz.openbmc_project.Object.Delete", "Delete");
1239336e96c6SChicago Duan     }
1240c4bf6374SJason M. Bills };
1241c4bf6374SJason M. Bills 
1242c4bf6374SJason M. Bills class BMCLogServiceCollection : public Node
1243c4bf6374SJason M. Bills {
1244c4bf6374SJason M. Bills   public:
1245c4bf6374SJason M. Bills     template <typename CrowApp>
1246c4bf6374SJason M. Bills     BMCLogServiceCollection(CrowApp& app) :
12474ed77cd5SEd Tanous         Node(app, "/redfish/v1/Managers/bmc/LogServices/")
12481da66f75SEd Tanous     {
12491da66f75SEd Tanous         entityPrivileges = {
1250e1f26343SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1251e1f26343SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1252e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1253e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1254e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1255e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
12561da66f75SEd Tanous     }
12571da66f75SEd Tanous 
12581da66f75SEd Tanous   private:
12591da66f75SEd Tanous     /**
12601da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
12611da66f75SEd Tanous      */
12621da66f75SEd Tanous     void doGet(crow::Response& res, const crow::Request& req,
12631da66f75SEd Tanous                const std::vector<std::string>& params) override
12641da66f75SEd Tanous     {
1265e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
12661da66f75SEd Tanous         // Collections don't include the static data added by SubRoute because
12671da66f75SEd Tanous         // it has a duplicate entry for members
1268e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
12691da66f75SEd Tanous             "#LogServiceCollection.LogServiceCollection";
1270e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
1271e1f26343SJason M. Bills             "/redfish/v1/Managers/bmc/LogServices";
1272e1f26343SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Open BMC Log Services Collection";
1273e1f26343SJason M. Bills         asyncResp->res.jsonValue["Description"] =
12741da66f75SEd Tanous             "Collection of LogServices for this Manager";
1275c4bf6374SJason M. Bills         nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
1276c4bf6374SJason M. Bills         logServiceArray = nlohmann::json::array();
1277c4bf6374SJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
1278c4bf6374SJason M. Bills         logServiceArray.push_back(
127908a4e4b5SAnthony Wilson             {{"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Journal"}});
1280c4bf6374SJason M. Bills #endif
1281e1f26343SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] =
1282c4bf6374SJason M. Bills             logServiceArray.size();
12831da66f75SEd Tanous     }
12841da66f75SEd Tanous };
12851da66f75SEd Tanous 
1286c4bf6374SJason M. Bills class BMCJournalLogService : public Node
12871da66f75SEd Tanous {
12881da66f75SEd Tanous   public:
12891da66f75SEd Tanous     template <typename CrowApp>
1290c4bf6374SJason M. Bills     BMCJournalLogService(CrowApp& app) :
1291c4bf6374SJason M. Bills         Node(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
1292e1f26343SJason M. Bills     {
1293e1f26343SJason M. Bills         entityPrivileges = {
1294e1f26343SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1295e1f26343SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1296e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1297e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1298e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1299e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1300e1f26343SJason M. Bills     }
1301e1f26343SJason M. Bills 
1302e1f26343SJason M. Bills   private:
1303e1f26343SJason M. Bills     void doGet(crow::Response& res, const crow::Request& req,
1304e1f26343SJason M. Bills                const std::vector<std::string>& params) override
1305e1f26343SJason M. Bills     {
1306e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1307e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1308e1f26343SJason M. Bills             "#LogService.v1_1_0.LogService";
13090f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
13100f74e643SEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal";
1311c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Open BMC Journal Log Service";
1312c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] = "BMC Journal Log Service";
1313c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Id"] = "BMC Journal";
1314e1f26343SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
1315cd50aa42SJason M. Bills         asyncResp->res.jsonValue["Entries"] = {
1316cd50aa42SJason M. Bills             {"@odata.id",
1317086be238SEd Tanous              "/redfish/v1/Managers/bmc/LogServices/Journal/Entries"}};
1318e1f26343SJason M. Bills     }
1319e1f26343SJason M. Bills };
1320e1f26343SJason M. Bills 
1321c4bf6374SJason M. Bills static int fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
1322e1f26343SJason M. Bills                                       sd_journal* journal,
1323c4bf6374SJason M. Bills                                       nlohmann::json& bmcJournalLogEntryJson)
1324e1f26343SJason M. Bills {
1325e1f26343SJason M. Bills     // Get the Log Entry contents
1326e1f26343SJason M. Bills     int ret = 0;
1327e1f26343SJason M. Bills 
132839e77504SEd Tanous     std::string_view msg;
132916428a1aSJason M. Bills     ret = getJournalMetadata(journal, "MESSAGE", msg);
1330e1f26343SJason M. Bills     if (ret < 0)
1331e1f26343SJason M. Bills     {
1332e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
1333e1f26343SJason M. Bills         return 1;
1334e1f26343SJason M. Bills     }
1335e1f26343SJason M. Bills 
1336e1f26343SJason M. Bills     // Get the severity from the PRIORITY field
1337271584abSEd Tanous     long int severity = 8; // Default to an invalid priority
133816428a1aSJason M. Bills     ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
1339e1f26343SJason M. Bills     if (ret < 0)
1340e1f26343SJason M. Bills     {
1341e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
1342e1f26343SJason M. Bills     }
1343e1f26343SJason M. Bills 
1344e1f26343SJason M. Bills     // Get the Created time from the timestamp
134516428a1aSJason M. Bills     std::string entryTimeStr;
134616428a1aSJason M. Bills     if (!getEntryTimestamp(journal, entryTimeStr))
1347e1f26343SJason M. Bills     {
134816428a1aSJason M. Bills         return 1;
1349e1f26343SJason M. Bills     }
1350e1f26343SJason M. Bills 
1351e1f26343SJason M. Bills     // Fill in the log entry with the gathered data
1352c4bf6374SJason M. Bills     bmcJournalLogEntryJson = {
1353cb92c03bSAndrew Geissler         {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
1354c4bf6374SJason M. Bills         {"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/" +
1355c4bf6374SJason M. Bills                           bmcJournalLogEntryID},
1356e1f26343SJason M. Bills         {"Name", "BMC Journal Entry"},
1357c4bf6374SJason M. Bills         {"Id", bmcJournalLogEntryID},
135816428a1aSJason M. Bills         {"Message", msg},
1359e1f26343SJason M. Bills         {"EntryType", "Oem"},
1360e1f26343SJason M. Bills         {"Severity",
1361b6a61a5eSJason M. Bills          severity <= 2 ? "Critical" : severity <= 4 ? "Warning" : "OK"},
1362086be238SEd Tanous         {"OemRecordFormat", "BMC Journal Entry"},
1363e1f26343SJason M. Bills         {"Created", std::move(entryTimeStr)}};
1364e1f26343SJason M. Bills     return 0;
1365e1f26343SJason M. Bills }
1366e1f26343SJason M. Bills 
1367c4bf6374SJason M. Bills class BMCJournalLogEntryCollection : public Node
1368e1f26343SJason M. Bills {
1369e1f26343SJason M. Bills   public:
1370e1f26343SJason M. Bills     template <typename CrowApp>
1371c4bf6374SJason M. Bills     BMCJournalLogEntryCollection(CrowApp& app) :
1372c4bf6374SJason M. Bills         Node(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
1373e1f26343SJason M. Bills     {
1374e1f26343SJason M. Bills         entityPrivileges = {
1375e1f26343SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1376e1f26343SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1377e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1378e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1379e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1380e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1381e1f26343SJason M. Bills     }
1382e1f26343SJason M. Bills 
1383e1f26343SJason M. Bills   private:
1384e1f26343SJason M. Bills     void doGet(crow::Response& res, const crow::Request& req,
1385e1f26343SJason M. Bills                const std::vector<std::string>& params) override
1386e1f26343SJason M. Bills     {
1387e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1388193ad2faSJason M. Bills         static constexpr const long maxEntriesPerPage = 1000;
1389271584abSEd Tanous         uint64_t skip = 0;
1390271584abSEd Tanous         uint64_t top = maxEntriesPerPage; // Show max entries by default
139116428a1aSJason M. Bills         if (!getSkipParam(asyncResp->res, req, skip))
1392193ad2faSJason M. Bills         {
1393193ad2faSJason M. Bills             return;
1394193ad2faSJason M. Bills         }
139516428a1aSJason M. Bills         if (!getTopParam(asyncResp->res, req, top))
1396193ad2faSJason M. Bills         {
1397193ad2faSJason M. Bills             return;
1398193ad2faSJason M. Bills         }
1399e1f26343SJason M. Bills         // Collections don't include the static data added by SubRoute because
1400e1f26343SJason M. Bills         // it has a duplicate entry for members
1401e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1402e1f26343SJason M. Bills             "#LogEntryCollection.LogEntryCollection";
14030f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
14040f74e643SEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
1405e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
1406c4bf6374SJason M. Bills             "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
1407e1f26343SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
1408e1f26343SJason M. Bills         asyncResp->res.jsonValue["Description"] =
1409e1f26343SJason M. Bills             "Collection of BMC Journal Entries";
14100f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
14110f74e643SEd Tanous             "/redfish/v1/Managers/bmc/LogServices/BmcLog/Entries";
1412e1f26343SJason M. Bills         nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1413e1f26343SJason M. Bills         logEntryArray = nlohmann::json::array();
1414e1f26343SJason M. Bills 
1415e1f26343SJason M. Bills         // Go through the journal and use the timestamp to create a unique ID
1416e1f26343SJason M. Bills         // for each entry
1417e1f26343SJason M. Bills         sd_journal* journalTmp = nullptr;
1418e1f26343SJason M. Bills         int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
1419e1f26343SJason M. Bills         if (ret < 0)
1420e1f26343SJason M. Bills         {
1421e1f26343SJason M. Bills             BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
1422f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1423e1f26343SJason M. Bills             return;
1424e1f26343SJason M. Bills         }
1425e1f26343SJason M. Bills         std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
1426e1f26343SJason M. Bills             journalTmp, sd_journal_close);
1427e1f26343SJason M. Bills         journalTmp = nullptr;
1428b01bf299SEd Tanous         uint64_t entryCount = 0;
1429e85d6b16SJason M. Bills         // Reset the unique ID on the first entry
1430e85d6b16SJason M. Bills         bool firstEntry = true;
1431e1f26343SJason M. Bills         SD_JOURNAL_FOREACH(journal.get())
1432e1f26343SJason M. Bills         {
1433193ad2faSJason M. Bills             entryCount++;
1434193ad2faSJason M. Bills             // Handle paging using skip (number of entries to skip from the
1435193ad2faSJason M. Bills             // start) and top (number of entries to display)
1436193ad2faSJason M. Bills             if (entryCount <= skip || entryCount > skip + top)
1437193ad2faSJason M. Bills             {
1438193ad2faSJason M. Bills                 continue;
1439193ad2faSJason M. Bills             }
1440193ad2faSJason M. Bills 
144116428a1aSJason M. Bills             std::string idStr;
1442e85d6b16SJason M. Bills             if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
1443e1f26343SJason M. Bills             {
1444e1f26343SJason M. Bills                 continue;
1445e1f26343SJason M. Bills             }
1446e1f26343SJason M. Bills 
1447e85d6b16SJason M. Bills             if (firstEntry)
1448e85d6b16SJason M. Bills             {
1449e85d6b16SJason M. Bills                 firstEntry = false;
1450e85d6b16SJason M. Bills             }
1451e85d6b16SJason M. Bills 
1452e1f26343SJason M. Bills             logEntryArray.push_back({});
1453c4bf6374SJason M. Bills             nlohmann::json& bmcJournalLogEntry = logEntryArray.back();
1454c4bf6374SJason M. Bills             if (fillBMCJournalLogEntryJson(idStr, journal.get(),
1455c4bf6374SJason M. Bills                                            bmcJournalLogEntry) != 0)
1456e1f26343SJason M. Bills             {
1457f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
1458e1f26343SJason M. Bills                 return;
1459e1f26343SJason M. Bills             }
1460e1f26343SJason M. Bills         }
1461193ad2faSJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
1462193ad2faSJason M. Bills         if (skip + top < entryCount)
1463193ad2faSJason M. Bills         {
1464193ad2faSJason M. Bills             asyncResp->res.jsonValue["Members@odata.nextLink"] =
1465c4bf6374SJason M. Bills                 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
1466193ad2faSJason M. Bills                 std::to_string(skip + top);
1467193ad2faSJason M. Bills         }
1468e1f26343SJason M. Bills     }
1469e1f26343SJason M. Bills };
1470e1f26343SJason M. Bills 
1471c4bf6374SJason M. Bills class BMCJournalLogEntry : public Node
1472e1f26343SJason M. Bills {
1473e1f26343SJason M. Bills   public:
1474c4bf6374SJason M. Bills     BMCJournalLogEntry(CrowApp& app) :
1475c4bf6374SJason M. Bills         Node(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/",
1476e1f26343SJason M. Bills              std::string())
1477e1f26343SJason M. Bills     {
1478e1f26343SJason M. Bills         entityPrivileges = {
1479e1f26343SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1480e1f26343SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1481e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1482e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1483e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1484e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1485e1f26343SJason M. Bills     }
1486e1f26343SJason M. Bills 
1487e1f26343SJason M. Bills   private:
1488e1f26343SJason M. Bills     void doGet(crow::Response& res, const crow::Request& req,
1489e1f26343SJason M. Bills                const std::vector<std::string>& params) override
1490e1f26343SJason M. Bills     {
1491e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1492e1f26343SJason M. Bills         if (params.size() != 1)
1493e1f26343SJason M. Bills         {
1494f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1495e1f26343SJason M. Bills             return;
1496e1f26343SJason M. Bills         }
149716428a1aSJason M. Bills         const std::string& entryID = params[0];
1498e1f26343SJason M. Bills         // Convert the unique ID back to a timestamp to find the entry
1499e1f26343SJason M. Bills         uint64_t ts = 0;
1500271584abSEd Tanous         uint64_t index = 0;
150116428a1aSJason M. Bills         if (!getTimestampFromID(asyncResp->res, entryID, ts, index))
1502e1f26343SJason M. Bills         {
150316428a1aSJason M. Bills             return;
1504e1f26343SJason M. Bills         }
1505e1f26343SJason M. Bills 
1506e1f26343SJason M. Bills         sd_journal* journalTmp = nullptr;
1507e1f26343SJason M. Bills         int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
1508e1f26343SJason M. Bills         if (ret < 0)
1509e1f26343SJason M. Bills         {
1510e1f26343SJason M. Bills             BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
1511f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1512e1f26343SJason M. Bills             return;
1513e1f26343SJason M. Bills         }
1514e1f26343SJason M. Bills         std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
1515e1f26343SJason M. Bills             journalTmp, sd_journal_close);
1516e1f26343SJason M. Bills         journalTmp = nullptr;
1517e1f26343SJason M. Bills         // Go to the timestamp in the log and move to the entry at the index
1518af07e3f5SJason M. Bills         // tracking the unique ID
1519af07e3f5SJason M. Bills         std::string idStr;
1520af07e3f5SJason M. Bills         bool firstEntry = true;
1521e1f26343SJason M. Bills         ret = sd_journal_seek_realtime_usec(journal.get(), ts);
15222056b6d1SManojkiran Eda         if (ret < 0)
15232056b6d1SManojkiran Eda         {
15242056b6d1SManojkiran Eda             BMCWEB_LOG_ERROR << "failed to seek to an entry in journal"
15252056b6d1SManojkiran Eda                              << strerror(-ret);
15262056b6d1SManojkiran Eda             messages::internalError(asyncResp->res);
15272056b6d1SManojkiran Eda             return;
15282056b6d1SManojkiran Eda         }
1529271584abSEd Tanous         for (uint64_t i = 0; i <= index; i++)
1530e1f26343SJason M. Bills         {
1531e1f26343SJason M. Bills             sd_journal_next(journal.get());
1532af07e3f5SJason M. Bills             if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
1533af07e3f5SJason M. Bills             {
1534af07e3f5SJason M. Bills                 messages::internalError(asyncResp->res);
1535af07e3f5SJason M. Bills                 return;
1536af07e3f5SJason M. Bills             }
1537af07e3f5SJason M. Bills             if (firstEntry)
1538af07e3f5SJason M. Bills             {
1539af07e3f5SJason M. Bills                 firstEntry = false;
1540af07e3f5SJason M. Bills             }
1541e1f26343SJason M. Bills         }
1542c4bf6374SJason M. Bills         // Confirm that the entry ID matches what was requested
1543af07e3f5SJason M. Bills         if (idStr != entryID)
1544c4bf6374SJason M. Bills         {
1545c4bf6374SJason M. Bills             messages::resourceMissingAtURI(asyncResp->res, entryID);
1546c4bf6374SJason M. Bills             return;
1547c4bf6374SJason M. Bills         }
1548c4bf6374SJason M. Bills 
1549c4bf6374SJason M. Bills         if (fillBMCJournalLogEntryJson(entryID, journal.get(),
1550e1f26343SJason M. Bills                                        asyncResp->res.jsonValue) != 0)
1551e1f26343SJason M. Bills         {
1552f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1553e1f26343SJason M. Bills             return;
1554e1f26343SJason M. Bills         }
1555e1f26343SJason M. Bills     }
1556e1f26343SJason M. Bills };
1557e1f26343SJason M. Bills 
1558c9bb6861Sraviteja-b class SystemDumpService : public Node
1559c9bb6861Sraviteja-b {
1560c9bb6861Sraviteja-b   public:
1561c9bb6861Sraviteja-b     template <typename CrowApp>
1562c9bb6861Sraviteja-b     SystemDumpService(CrowApp& app) :
1563c9bb6861Sraviteja-b         Node(app, "/redfish/v1/Systems/system/LogServices/System/")
1564c9bb6861Sraviteja-b     {
1565c9bb6861Sraviteja-b         entityPrivileges = {
1566c9bb6861Sraviteja-b             {boost::beast::http::verb::get, {{"Login"}}},
1567c9bb6861Sraviteja-b             {boost::beast::http::verb::head, {{"Login"}}},
1568c9bb6861Sraviteja-b             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1569c9bb6861Sraviteja-b             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1570c9bb6861Sraviteja-b             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1571c9bb6861Sraviteja-b             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1572c9bb6861Sraviteja-b     }
1573c9bb6861Sraviteja-b 
1574c9bb6861Sraviteja-b   private:
1575c9bb6861Sraviteja-b     void doGet(crow::Response& res, const crow::Request& req,
1576c9bb6861Sraviteja-b                const std::vector<std::string>& params) override
1577c9bb6861Sraviteja-b     {
1578c9bb6861Sraviteja-b         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1579c9bb6861Sraviteja-b 
1580c9bb6861Sraviteja-b         asyncResp->res.jsonValue["@odata.id"] =
1581c9bb6861Sraviteja-b             "/redfish/v1/Systems/system/LogServices/System";
1582c9bb6861Sraviteja-b         asyncResp->res.jsonValue["@odata.type"] =
1583c9bb6861Sraviteja-b             "#LogService.v1_1_0.LogService";
1584c9bb6861Sraviteja-b         asyncResp->res.jsonValue["Name"] = "Dump Log Service";
1585c9bb6861Sraviteja-b         asyncResp->res.jsonValue["Description"] = "System Dump Log Service";
1586c9bb6861Sraviteja-b         asyncResp->res.jsonValue["Id"] = "System";
1587c9bb6861Sraviteja-b         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
1588c9bb6861Sraviteja-b         asyncResp->res.jsonValue["LogEntryTypes"] = "Dump";
1589c9bb6861Sraviteja-b         asyncResp->res.jsonValue["Oem"]["DumpType"] = "System";
1590c9bb6861Sraviteja-b 
1591c9bb6861Sraviteja-b         asyncResp->res.jsonValue["Entries"] = {
1592c9bb6861Sraviteja-b             {"@odata.id",
1593c9bb6861Sraviteja-b              "/redfish/v1/Systems/system/LogServices/System/Entries"}};
1594c9bb6861Sraviteja-b         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
1595c9bb6861Sraviteja-b             {"target", "/redfish/v1/Systems/system/LogServices/System/"
1596c9bb6861Sraviteja-b                        "Actions/LogService.ClearLog"}};
1597c9bb6861Sraviteja-b         asyncResp->res.jsonValue["Actions"]["#LogService.CreateLog"] = {
1598c9bb6861Sraviteja-b             {"target", "/redfish/v1/Systems/system/LogServices/System/"
1599c9bb6861Sraviteja-b                        "Actions/LogService.CreateLog"}};
1600c9bb6861Sraviteja-b     }
1601c9bb6861Sraviteja-b };
1602c9bb6861Sraviteja-b 
1603c9bb6861Sraviteja-b class SystemDumpEntryCollection : public Node
1604c9bb6861Sraviteja-b {
1605c9bb6861Sraviteja-b   public:
1606c9bb6861Sraviteja-b     template <typename CrowApp>
1607c9bb6861Sraviteja-b     SystemDumpEntryCollection(CrowApp& app) :
1608c9bb6861Sraviteja-b         Node(app, "/redfish/v1/Systems/system/LogServices/System/Entries/")
1609c9bb6861Sraviteja-b     {
1610c9bb6861Sraviteja-b         entityPrivileges = {
1611c9bb6861Sraviteja-b             {boost::beast::http::verb::get, {{"Login"}}},
1612c9bb6861Sraviteja-b             {boost::beast::http::verb::head, {{"Login"}}},
1613c9bb6861Sraviteja-b             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1614c9bb6861Sraviteja-b             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1615c9bb6861Sraviteja-b             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1616c9bb6861Sraviteja-b             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1617c9bb6861Sraviteja-b     }
1618c9bb6861Sraviteja-b 
1619c9bb6861Sraviteja-b   private:
1620c9bb6861Sraviteja-b     /**
1621c9bb6861Sraviteja-b      * Functions triggers appropriate requests on DBus
1622c9bb6861Sraviteja-b      */
1623c9bb6861Sraviteja-b     void doGet(crow::Response& res, const crow::Request& req,
1624c9bb6861Sraviteja-b                const std::vector<std::string>& params) override
1625c9bb6861Sraviteja-b     {
1626c9bb6861Sraviteja-b         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1627c9bb6861Sraviteja-b 
1628c9bb6861Sraviteja-b         asyncResp->res.jsonValue["@odata.type"] =
1629c9bb6861Sraviteja-b             "#LogEntryCollection.LogEntryCollection";
1630c9bb6861Sraviteja-b         asyncResp->res.jsonValue["@odata.id"] =
1631c9bb6861Sraviteja-b             "/redfish/v1/Systems/system/LogServices/System/Entries";
1632c9bb6861Sraviteja-b         asyncResp->res.jsonValue["Name"] = "System Dump Entries";
1633c9bb6861Sraviteja-b         asyncResp->res.jsonValue["Description"] =
1634c9bb6861Sraviteja-b             "Collection of System Dump Entries";
1635c9bb6861Sraviteja-b 
1636c9bb6861Sraviteja-b         crow::connections::systemBus->async_method_call(
1637c9bb6861Sraviteja-b             [asyncResp](const boost::system::error_code ec,
1638c9bb6861Sraviteja-b                         const crow::openbmc_mapper::GetSubTreeType& resp) {
1639c9bb6861Sraviteja-b                 if (ec)
1640c9bb6861Sraviteja-b                 {
1641c9bb6861Sraviteja-b                     BMCWEB_LOG_ERROR << " resp_handler got error " << ec;
1642c9bb6861Sraviteja-b                     messages::internalError(asyncResp->res);
1643c9bb6861Sraviteja-b                     return;
1644c9bb6861Sraviteja-b                 }
1645c9bb6861Sraviteja-b 
1646c9bb6861Sraviteja-b                 nlohmann::json& logArray = asyncResp->res.jsonValue["Members"];
1647c9bb6861Sraviteja-b                 logArray = nlohmann::json::array();
1648c9bb6861Sraviteja-b                 for (auto& object : resp)
1649c9bb6861Sraviteja-b                 {
1650c9bb6861Sraviteja-b                     const std::string& path =
1651c9bb6861Sraviteja-b                         static_cast<const std::string&>(object.first);
1652c9bb6861Sraviteja-b                     std::size_t lastPos = path.rfind("/");
1653c9bb6861Sraviteja-b                     if (lastPos == std::string::npos)
1654c9bb6861Sraviteja-b                     {
1655c9bb6861Sraviteja-b                         continue;
1656c9bb6861Sraviteja-b                     }
1657c9bb6861Sraviteja-b                     std::string logID = path.substr(lastPos + 1);
1658c9bb6861Sraviteja-b                     logArray.push_back(
1659c9bb6861Sraviteja-b                         {{"@odata.id", "/redfish/v1/Systems/system/LogServices/"
1660c9bb6861Sraviteja-b                                        "System/Entries/" +
1661c9bb6861Sraviteja-b                                            logID}});
1662c9bb6861Sraviteja-b                 }
1663c9bb6861Sraviteja-b                 asyncResp->res.jsonValue["Members@odata.count"] =
1664c9bb6861Sraviteja-b                     logArray.size();
1665c9bb6861Sraviteja-b             },
1666c9bb6861Sraviteja-b             "xyz.openbmc_project.ObjectMapper",
1667c9bb6861Sraviteja-b             "/xyz/openbmc_project/object_mapper",
1668c9bb6861Sraviteja-b             "xyz.openbmc_project.ObjectMapper", "GetSubTree",
1669c9bb6861Sraviteja-b             "/xyz/openbmc_project/dump", 0,
1670c9bb6861Sraviteja-b             std::array<const char*, 1>{
1671c9bb6861Sraviteja-b                 "xyz.openbmc_project.Dump.Entry.System"});
1672c9bb6861Sraviteja-b     }
1673c9bb6861Sraviteja-b };
1674c9bb6861Sraviteja-b 
1675c9bb6861Sraviteja-b class SystemDumpEntry : public Node
1676c9bb6861Sraviteja-b {
1677c9bb6861Sraviteja-b   public:
1678c9bb6861Sraviteja-b     SystemDumpEntry(CrowApp& app) :
1679c9bb6861Sraviteja-b         Node(app,
1680c9bb6861Sraviteja-b              "/redfish/v1/Systems/system/LogServices/System/Entries/<str>/",
1681c9bb6861Sraviteja-b              std::string())
1682c9bb6861Sraviteja-b     {
1683c9bb6861Sraviteja-b         entityPrivileges = {
1684c9bb6861Sraviteja-b             {boost::beast::http::verb::get, {{"Login"}}},
1685c9bb6861Sraviteja-b             {boost::beast::http::verb::head, {{"Login"}}},
1686c9bb6861Sraviteja-b             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1687c9bb6861Sraviteja-b             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1688c9bb6861Sraviteja-b             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1689c9bb6861Sraviteja-b             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1690c9bb6861Sraviteja-b     }
1691c9bb6861Sraviteja-b 
1692c9bb6861Sraviteja-b   private:
1693c9bb6861Sraviteja-b     void doGet(crow::Response& res, const crow::Request& req,
1694c9bb6861Sraviteja-b                const std::vector<std::string>& params) override
1695c9bb6861Sraviteja-b     {
1696c9bb6861Sraviteja-b         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1697c9bb6861Sraviteja-b         if (params.size() != 1)
1698c9bb6861Sraviteja-b         {
1699c9bb6861Sraviteja-b             messages::internalError(asyncResp->res);
1700c9bb6861Sraviteja-b             return;
1701c9bb6861Sraviteja-b         }
1702c9bb6861Sraviteja-b         const std::string& entryID = params[0];
1703c9bb6861Sraviteja-b         crow::connections::systemBus->async_method_call(
1704c9bb6861Sraviteja-b             [asyncResp, entryID](const boost::system::error_code ec,
1705c9bb6861Sraviteja-b                                  GetManagedObjectsType& resp) {
1706c9bb6861Sraviteja-b                 if (ec)
1707c9bb6861Sraviteja-b                 {
1708c9bb6861Sraviteja-b                     BMCWEB_LOG_ERROR
1709c9bb6861Sraviteja-b                         << "SystemDumpEntry resp_handler got error " << ec;
1710c9bb6861Sraviteja-b                     messages::internalError(asyncResp->res);
1711c9bb6861Sraviteja-b                     return;
1712c9bb6861Sraviteja-b                 }
1713c9bb6861Sraviteja-b 
1714c9bb6861Sraviteja-b                 for (auto& objectPath : resp)
1715c9bb6861Sraviteja-b                 {
1716c9bb6861Sraviteja-b                     if (objectPath.first.str.find(
1717c9bb6861Sraviteja-b                             "/xyz/openbmc_project/dump/entry/" + entryID) ==
1718c9bb6861Sraviteja-b                         std::string::npos)
1719c9bb6861Sraviteja-b                     {
1720c9bb6861Sraviteja-b                         continue;
1721c9bb6861Sraviteja-b                     }
1722c9bb6861Sraviteja-b 
1723c9bb6861Sraviteja-b                     bool foundSystemDumpEntry = false;
1724c9bb6861Sraviteja-b                     for (auto& interfaceMap : objectPath.second)
1725c9bb6861Sraviteja-b                     {
1726c9bb6861Sraviteja-b                         if (interfaceMap.first ==
1727c9bb6861Sraviteja-b                             "xyz.openbmc_project.Dump.Entry.System")
1728c9bb6861Sraviteja-b                         {
1729c9bb6861Sraviteja-b                             foundSystemDumpEntry = true;
1730c9bb6861Sraviteja-b                             break;
1731c9bb6861Sraviteja-b                         }
1732c9bb6861Sraviteja-b                     }
1733c9bb6861Sraviteja-b                     if (foundSystemDumpEntry == false)
1734c9bb6861Sraviteja-b                     {
1735c9bb6861Sraviteja-b                         BMCWEB_LOG_DEBUG << "Can't find System Dump Entry";
1736c9bb6861Sraviteja-b                         messages::internalError(asyncResp->res);
1737c9bb6861Sraviteja-b                         return;
1738c9bb6861Sraviteja-b                     }
1739c9bb6861Sraviteja-b 
1740c9bb6861Sraviteja-b                     std::string timestamp{};
1741c9bb6861Sraviteja-b                     uint64_t size = 0;
1742c9bb6861Sraviteja-b 
1743c9bb6861Sraviteja-b                     for (auto& interfaceMap : objectPath.second)
1744c9bb6861Sraviteja-b                     {
1745c9bb6861Sraviteja-b                         if (interfaceMap.first ==
1746c9bb6861Sraviteja-b                             "xyz.openbmc_project.Dump.Entry")
1747c9bb6861Sraviteja-b                         {
1748c9bb6861Sraviteja-b                             for (auto& propertyMap : interfaceMap.second)
1749c9bb6861Sraviteja-b                             {
1750c9bb6861Sraviteja-b                                 if (propertyMap.first == "Size")
1751c9bb6861Sraviteja-b                                 {
1752c9bb6861Sraviteja-b                                     auto sizePtr = std::get_if<uint64_t>(
1753c9bb6861Sraviteja-b                                         &propertyMap.second);
1754c9bb6861Sraviteja-b                                     if (sizePtr == nullptr)
1755c9bb6861Sraviteja-b                                     {
1756c9bb6861Sraviteja-b                                         messages::propertyMissing(
1757c9bb6861Sraviteja-b                                             asyncResp->res, "Size");
1758c9bb6861Sraviteja-b                                         break;
1759c9bb6861Sraviteja-b                                     }
1760c9bb6861Sraviteja-b                                     size = *sizePtr;
1761c9bb6861Sraviteja-b                                     break;
1762c9bb6861Sraviteja-b                                 }
1763c9bb6861Sraviteja-b                             }
1764c9bb6861Sraviteja-b                         }
1765c9bb6861Sraviteja-b                         else if (interfaceMap.first ==
1766c9bb6861Sraviteja-b                                  "xyz.openbmc_project.Time.EpochTime")
1767c9bb6861Sraviteja-b                         {
1768c9bb6861Sraviteja-b                             for (auto& propertyMap : interfaceMap.second)
1769c9bb6861Sraviteja-b                             {
1770c9bb6861Sraviteja-b                                 if (propertyMap.first == "Elapsed")
1771c9bb6861Sraviteja-b                                 {
1772c9bb6861Sraviteja-b                                     const uint64_t* usecsTimeStamp =
1773c9bb6861Sraviteja-b                                         std::get_if<uint64_t>(
1774c9bb6861Sraviteja-b                                             &propertyMap.second);
1775c9bb6861Sraviteja-b                                     if (usecsTimeStamp == nullptr)
1776c9bb6861Sraviteja-b                                     {
1777c9bb6861Sraviteja-b                                         messages::propertyMissing(
1778c9bb6861Sraviteja-b                                             asyncResp->res, "Elapsed");
1779c9bb6861Sraviteja-b                                         break;
1780c9bb6861Sraviteja-b                                     }
1781c9bb6861Sraviteja-b                                     getTimestampStr(*usecsTimeStamp, timestamp);
1782c9bb6861Sraviteja-b                                     break;
1783c9bb6861Sraviteja-b                                 }
1784c9bb6861Sraviteja-b                             }
1785c9bb6861Sraviteja-b                         }
1786c9bb6861Sraviteja-b                     }
1787c9bb6861Sraviteja-b                     asyncResp->res.jsonValue = {
1788c9bb6861Sraviteja-b                         {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
1789c9bb6861Sraviteja-b                         {"@odata.id",
1790c9bb6861Sraviteja-b                          "/redfish/v1/Systems/system/LogServices/System/"
1791c9bb6861Sraviteja-b                          "Entries/" +
1792c9bb6861Sraviteja-b                              entryID},
1793c9bb6861Sraviteja-b                         {"Name", "System Dump Entry"},
1794c9bb6861Sraviteja-b                         {"Id", entryID},
1795c9bb6861Sraviteja-b                         {"SizeInB", size},
1796c9bb6861Sraviteja-b                         {"EntryType", "Dump"},
1797c9bb6861Sraviteja-b                         {"EntryCode", "User generated dump"},
1798c9bb6861Sraviteja-b                         {"Created", timestamp}};
1799c9bb6861Sraviteja-b 
1800c9bb6861Sraviteja-b                     asyncResp->res
1801c9bb6861Sraviteja-b                         .jsonValue["Actions"]["#LogEntry.DownloadLog"] = {
1802c9bb6861Sraviteja-b                         {"target",
1803c9bb6861Sraviteja-b                          "/redfish/v1/Systems/system/LogServices/System/"
1804c9bb6861Sraviteja-b                          "Entries/" +
1805c9bb6861Sraviteja-b                              entryID + "/Actions/LogEntry.DownloadLog"}};
1806c9bb6861Sraviteja-b                 }
1807c9bb6861Sraviteja-b             },
1808c9bb6861Sraviteja-b             "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
1809c9bb6861Sraviteja-b             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1810c9bb6861Sraviteja-b     }
1811c9bb6861Sraviteja-b 
1812c9bb6861Sraviteja-b     void doDelete(crow::Response& res, const crow::Request& req,
1813c9bb6861Sraviteja-b                   const std::vector<std::string>& params) override
1814c9bb6861Sraviteja-b     {
1815c9bb6861Sraviteja-b         BMCWEB_LOG_DEBUG << "Do delete single dump entry";
1816c9bb6861Sraviteja-b 
1817c9bb6861Sraviteja-b         auto asyncResp = std::make_shared<AsyncResp>(res);
1818c9bb6861Sraviteja-b 
1819c9bb6861Sraviteja-b         if (params.size() != 1)
1820c9bb6861Sraviteja-b         {
1821c9bb6861Sraviteja-b             messages::internalError(asyncResp->res);
1822c9bb6861Sraviteja-b             return;
1823c9bb6861Sraviteja-b         }
1824c9bb6861Sraviteja-b         std::string entryID = params[0];
1825c9bb6861Sraviteja-b 
1826c9bb6861Sraviteja-b         crow::connections::systemBus->async_method_call(
1827c9bb6861Sraviteja-b             [asyncResp,
1828c9bb6861Sraviteja-b              entryID](const boost::system::error_code ec,
1829c9bb6861Sraviteja-b                       const crow::openbmc_mapper::GetSubTreeType& resp) {
1830c9bb6861Sraviteja-b                 if (ec)
1831c9bb6861Sraviteja-b                 {
1832c9bb6861Sraviteja-b                     BMCWEB_LOG_ERROR << " resp_handler got error " << ec;
1833c9bb6861Sraviteja-b                     messages::internalError(asyncResp->res);
1834c9bb6861Sraviteja-b                     return;
1835c9bb6861Sraviteja-b                 }
1836c9bb6861Sraviteja-b 
1837c9bb6861Sraviteja-b                 for (auto& object : resp)
1838c9bb6861Sraviteja-b                 {
1839c9bb6861Sraviteja-b                     const std::string& path =
1840c9bb6861Sraviteja-b                         static_cast<const std::string&>(object.first);
1841c9bb6861Sraviteja-b 
1842c9bb6861Sraviteja-b                     std::size_t pos = path.rfind(
1843c9bb6861Sraviteja-b                         "/xyz/openbmc_project/dump/entry/" + entryID);
1844c9bb6861Sraviteja-b                     if (pos != std::string::npos)
1845c9bb6861Sraviteja-b                     {
1846c9bb6861Sraviteja-b                         deleteSystemDumpEntry(asyncResp->res, entryID);
1847c9bb6861Sraviteja-b                         return;
1848c9bb6861Sraviteja-b                     }
1849c9bb6861Sraviteja-b                 }
1850c9bb6861Sraviteja-b             },
1851c9bb6861Sraviteja-b             "xyz.openbmc_project.ObjectMapper",
1852c9bb6861Sraviteja-b             "/xyz/openbmc_project/object_mapper",
1853c9bb6861Sraviteja-b             "xyz.openbmc_project.ObjectMapper", "GetSubTree",
1854c9bb6861Sraviteja-b             "/xyz/openbmc_project/dump", 0,
1855c9bb6861Sraviteja-b             std::array<const char*, 1>{
1856c9bb6861Sraviteja-b                 "xyz.openbmc_project.Dump.Entry.System"});
1857c9bb6861Sraviteja-b     }
1858c9bb6861Sraviteja-b };
1859c9bb6861Sraviteja-b 
18600657843aSraviteja-b class SystemDumpEntryDownload : public Node
18610657843aSraviteja-b {
18620657843aSraviteja-b   public:
18630657843aSraviteja-b     SystemDumpEntryDownload(CrowApp& app) :
18640657843aSraviteja-b         Node(app,
18650657843aSraviteja-b              "/redfish/v1/Systems/system/LogServices/System/Entries/<str>/"
18660657843aSraviteja-b              "Actions/"
18670657843aSraviteja-b              "LogEntry.DownloadLog/",
18680657843aSraviteja-b              std::string())
18690657843aSraviteja-b     {
18700657843aSraviteja-b         entityPrivileges = {
18710657843aSraviteja-b             {boost::beast::http::verb::get, {{"Login"}}},
18720657843aSraviteja-b             {boost::beast::http::verb::head, {{"Login"}}},
18730657843aSraviteja-b             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
18740657843aSraviteja-b     }
18750657843aSraviteja-b 
18760657843aSraviteja-b   private:
18770657843aSraviteja-b     void doPost(crow::Response& res, const crow::Request& req,
18780657843aSraviteja-b                 const std::vector<std::string>& params) override
18790657843aSraviteja-b     {
18800657843aSraviteja-b         if (params.size() != 1)
18810657843aSraviteja-b         {
18820657843aSraviteja-b             messages::internalError(res);
18830657843aSraviteja-b             return;
18840657843aSraviteja-b         }
18850657843aSraviteja-b         const std::string& entryID = params[0];
18860657843aSraviteja-b         crow::obmc_dump::handleDumpOffloadUrl(req, res, entryID);
18870657843aSraviteja-b     }
18880657843aSraviteja-b };
18890657843aSraviteja-b 
1890013487e5Sraviteja-b class SystemDumpClear : public Node
1891013487e5Sraviteja-b {
1892013487e5Sraviteja-b   public:
1893013487e5Sraviteja-b     SystemDumpClear(CrowApp& app) :
1894013487e5Sraviteja-b         Node(app, "/redfish/v1/Systems/system/LogServices/System/"
1895013487e5Sraviteja-b                   "Actions/"
1896013487e5Sraviteja-b                   "LogService.ClearLog/")
1897013487e5Sraviteja-b     {
1898013487e5Sraviteja-b         entityPrivileges = {
1899013487e5Sraviteja-b             {boost::beast::http::verb::get, {{"Login"}}},
1900013487e5Sraviteja-b             {boost::beast::http::verb::head, {{"Login"}}},
1901013487e5Sraviteja-b             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1902013487e5Sraviteja-b     }
1903013487e5Sraviteja-b 
1904013487e5Sraviteja-b   private:
1905013487e5Sraviteja-b     void doPost(crow::Response& res, const crow::Request& req,
1906013487e5Sraviteja-b                 const std::vector<std::string>& params) override
1907013487e5Sraviteja-b     {
1908013487e5Sraviteja-b 
1909013487e5Sraviteja-b         auto asyncResp = std::make_shared<AsyncResp>(res);
1910013487e5Sraviteja-b         crow::connections::systemBus->async_method_call(
1911013487e5Sraviteja-b             [asyncResp](const boost::system::error_code ec,
1912013487e5Sraviteja-b                         const std::vector<std::string>& dumpList) {
1913013487e5Sraviteja-b                 if (ec)
1914013487e5Sraviteja-b                 {
1915013487e5Sraviteja-b                     messages::internalError(asyncResp->res);
1916013487e5Sraviteja-b                     return;
1917013487e5Sraviteja-b                 }
1918013487e5Sraviteja-b 
1919013487e5Sraviteja-b                 for (const std::string& objectPath : dumpList)
1920013487e5Sraviteja-b                 {
1921013487e5Sraviteja-b                     std::size_t pos = objectPath.rfind("/");
1922013487e5Sraviteja-b                     if (pos != std::string::npos)
1923013487e5Sraviteja-b                     {
1924013487e5Sraviteja-b                         std::string logID = objectPath.substr(pos + 1);
1925013487e5Sraviteja-b                         deleteSystemDumpEntry(asyncResp->res, logID);
1926013487e5Sraviteja-b                     }
1927013487e5Sraviteja-b                 }
1928013487e5Sraviteja-b             },
1929013487e5Sraviteja-b             "xyz.openbmc_project.ObjectMapper",
1930013487e5Sraviteja-b             "/xyz/openbmc_project/object_mapper",
1931013487e5Sraviteja-b             "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
1932013487e5Sraviteja-b             "/xyz/openbmc_project/dump", 0,
1933013487e5Sraviteja-b             std::array<const char*, 1>{
1934013487e5Sraviteja-b                 "xyz.openbmc_project.Dump.Entry.System"});
1935013487e5Sraviteja-b     }
1936013487e5Sraviteja-b };
1937013487e5Sraviteja-b 
1938424c4176SJason M. Bills class CrashdumpService : public Node
1939e1f26343SJason M. Bills {
1940e1f26343SJason M. Bills   public:
1941e1f26343SJason M. Bills     template <typename CrowApp>
1942424c4176SJason M. Bills     CrashdumpService(CrowApp& app) :
1943424c4176SJason M. Bills         Node(app, "/redfish/v1/Systems/system/LogServices/Crashdump/")
19441da66f75SEd Tanous     {
19453946028dSAppaRao Puli         // Note: Deviated from redfish privilege registry for GET & HEAD
19463946028dSAppaRao Puli         // method for security reasons.
19471da66f75SEd Tanous         entityPrivileges = {
19483946028dSAppaRao Puli             {boost::beast::http::verb::get, {{"ConfigureComponents"}}},
19493946028dSAppaRao Puli             {boost::beast::http::verb::head, {{"ConfigureComponents"}}},
1950e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1951e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1952e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1953e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
19541da66f75SEd Tanous     }
19551da66f75SEd Tanous 
19561da66f75SEd Tanous   private:
19571da66f75SEd Tanous     /**
19581da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
19591da66f75SEd Tanous      */
19601da66f75SEd Tanous     void doGet(crow::Response& res, const crow::Request& req,
19611da66f75SEd Tanous                const std::vector<std::string>& params) override
19621da66f75SEd Tanous     {
1963e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
19641da66f75SEd Tanous         // Copy over the static data to include the entries added by SubRoute
19650f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
1966424c4176SJason M. Bills             "/redfish/v1/Systems/system/LogServices/Crashdump";
1967e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1968e1f26343SJason M. Bills             "#LogService.v1_1_0.LogService";
19694f50ae4bSGunnar Mills         asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
19704f50ae4bSGunnar Mills         asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
19714f50ae4bSGunnar Mills         asyncResp->res.jsonValue["Id"] = "Oem Crashdump";
1972e1f26343SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
1973e1f26343SJason M. Bills         asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
1974cd50aa42SJason M. Bills         asyncResp->res.jsonValue["Entries"] = {
1975cd50aa42SJason M. Bills             {"@odata.id",
1976424c4176SJason M. Bills              "/redfish/v1/Systems/system/LogServices/Crashdump/Entries"}};
1977e1f26343SJason M. Bills         asyncResp->res.jsonValue["Actions"] = {
19785b61b5e8SJason M. Bills             {"#LogService.ClearLog",
19795b61b5e8SJason M. Bills              {{"target", "/redfish/v1/Systems/system/LogServices/Crashdump/"
19805b61b5e8SJason M. Bills                          "Actions/LogService.ClearLog"}}},
19811da66f75SEd Tanous             {"Oem",
1982424c4176SJason M. Bills              {{"#Crashdump.OnDemand",
1983424c4176SJason M. Bills                {{"target", "/redfish/v1/Systems/system/LogServices/Crashdump/"
19846eda7685SKenny L. Ku                            "Actions/Oem/Crashdump.OnDemand"}}},
19856eda7685SKenny L. Ku               {"#Crashdump.Telemetry",
19866eda7685SKenny L. Ku                {{"target", "/redfish/v1/Systems/system/LogServices/Crashdump/"
19876eda7685SKenny L. Ku                            "Actions/Oem/Crashdump.Telemetry"}}}}}};
19881da66f75SEd Tanous 
19891da66f75SEd Tanous #ifdef BMCWEB_ENABLE_REDFISH_RAW_PECI
1990e1f26343SJason M. Bills         asyncResp->res.jsonValue["Actions"]["Oem"].push_back(
1991424c4176SJason M. Bills             {"#Crashdump.SendRawPeci",
199208a4e4b5SAnthony Wilson              {{"target", "/redfish/v1/Systems/system/LogServices/Crashdump/"
1993424c4176SJason M. Bills                          "Actions/Oem/Crashdump.SendRawPeci"}}});
19941da66f75SEd Tanous #endif
19951da66f75SEd Tanous     }
19961da66f75SEd Tanous };
19971da66f75SEd Tanous 
19985b61b5e8SJason M. Bills class CrashdumpClear : public Node
19995b61b5e8SJason M. Bills {
20005b61b5e8SJason M. Bills   public:
20015b61b5e8SJason M. Bills     CrashdumpClear(CrowApp& app) :
20025b61b5e8SJason M. Bills         Node(app, "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/"
20035b61b5e8SJason M. Bills                   "LogService.ClearLog/")
20045b61b5e8SJason M. Bills     {
20053946028dSAppaRao Puli         // Note: Deviated from redfish privilege registry for GET & HEAD
20063946028dSAppaRao Puli         // method for security reasons.
20075b61b5e8SJason M. Bills         entityPrivileges = {
20083946028dSAppaRao Puli             {boost::beast::http::verb::get, {{"ConfigureComponents"}}},
20093946028dSAppaRao Puli             {boost::beast::http::verb::head, {{"ConfigureComponents"}}},
20105b61b5e8SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
20115b61b5e8SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
20125b61b5e8SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
20135b61b5e8SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
20145b61b5e8SJason M. Bills     }
20155b61b5e8SJason M. Bills 
20165b61b5e8SJason M. Bills   private:
20175b61b5e8SJason M. Bills     void doPost(crow::Response& res, const crow::Request& req,
20185b61b5e8SJason M. Bills                 const std::vector<std::string>& params) override
20195b61b5e8SJason M. Bills     {
20205b61b5e8SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
20215b61b5e8SJason M. Bills 
20225b61b5e8SJason M. Bills         crow::connections::systemBus->async_method_call(
20235b61b5e8SJason M. Bills             [asyncResp](const boost::system::error_code ec,
20245b61b5e8SJason M. Bills                         const std::string& resp) {
20255b61b5e8SJason M. Bills                 if (ec)
20265b61b5e8SJason M. Bills                 {
20275b61b5e8SJason M. Bills                     messages::internalError(asyncResp->res);
20285b61b5e8SJason M. Bills                     return;
20295b61b5e8SJason M. Bills                 }
20305b61b5e8SJason M. Bills                 messages::success(asyncResp->res);
20315b61b5e8SJason M. Bills             },
20325b61b5e8SJason M. Bills             crashdumpObject, crashdumpPath, deleteAllInterface, "DeleteAll");
20335b61b5e8SJason M. Bills     }
20345b61b5e8SJason M. Bills };
20355b61b5e8SJason M. Bills 
2036e855dd28SJason M. Bills static void logCrashdumpEntry(std::shared_ptr<AsyncResp> asyncResp,
2037e855dd28SJason M. Bills                               const std::string& logID,
2038e855dd28SJason M. Bills                               nlohmann::json& logEntryJson)
2039e855dd28SJason M. Bills {
2040043a0536SJohnathan Mantey     auto getStoredLogCallback =
2041043a0536SJohnathan Mantey         [asyncResp, logID, &logEntryJson](
2042e855dd28SJason M. Bills             const boost::system::error_code ec,
2043043a0536SJohnathan Mantey             const std::vector<std::pair<std::string, VariantType>>& params) {
2044e855dd28SJason M. Bills             if (ec)
2045e855dd28SJason M. Bills             {
2046e855dd28SJason M. Bills                 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
20471ddcf01aSJason M. Bills                 if (ec.value() ==
20481ddcf01aSJason M. Bills                     boost::system::linux_error::bad_request_descriptor)
20491ddcf01aSJason M. Bills                 {
2050043a0536SJohnathan Mantey                     messages::resourceNotFound(asyncResp->res, "LogEntry",
2051043a0536SJohnathan Mantey                                                logID);
20521ddcf01aSJason M. Bills                 }
20531ddcf01aSJason M. Bills                 else
20541ddcf01aSJason M. Bills                 {
2055e855dd28SJason M. Bills                     messages::internalError(asyncResp->res);
20561ddcf01aSJason M. Bills                 }
2057e855dd28SJason M. Bills                 return;
2058e855dd28SJason M. Bills             }
2059043a0536SJohnathan Mantey 
2060043a0536SJohnathan Mantey             std::string timestamp{};
2061043a0536SJohnathan Mantey             std::string filename{};
2062043a0536SJohnathan Mantey             std::string logfile{};
2063043a0536SJohnathan Mantey             ParseCrashdumpParameters(params, filename, timestamp, logfile);
2064043a0536SJohnathan Mantey 
2065043a0536SJohnathan Mantey             if (filename.empty() || timestamp.empty())
2066e855dd28SJason M. Bills             {
2067043a0536SJohnathan Mantey                 messages::resourceMissingAtURI(asyncResp->res, logID);
2068e855dd28SJason M. Bills                 return;
2069e855dd28SJason M. Bills             }
2070e855dd28SJason M. Bills 
2071043a0536SJohnathan Mantey             std::string crashdumpURI =
2072e855dd28SJason M. Bills                 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
2073043a0536SJohnathan Mantey                 logID + "/" + filename;
2074043a0536SJohnathan Mantey             logEntryJson = {{"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
2075043a0536SJohnathan Mantey                             {"@odata.id", "/redfish/v1/Systems/system/"
2076043a0536SJohnathan Mantey                                           "LogServices/Crashdump/Entries/" +
2077e855dd28SJason M. Bills                                               logID},
2078e855dd28SJason M. Bills                             {"Name", "CPU Crashdump"},
2079e855dd28SJason M. Bills                             {"Id", logID},
2080e855dd28SJason M. Bills                             {"EntryType", "Oem"},
2081e855dd28SJason M. Bills                             {"OemRecordFormat", "Crashdump URI"},
2082043a0536SJohnathan Mantey                             {"Message", std::move(crashdumpURI)},
2083043a0536SJohnathan Mantey                             {"Created", std::move(timestamp)}};
2084e855dd28SJason M. Bills         };
2085e855dd28SJason M. Bills     crow::connections::systemBus->async_method_call(
20865b61b5e8SJason M. Bills         std::move(getStoredLogCallback), crashdumpObject,
20875b61b5e8SJason M. Bills         crashdumpPath + std::string("/") + logID,
2088043a0536SJohnathan Mantey         "org.freedesktop.DBus.Properties", "GetAll", crashdumpInterface);
2089e855dd28SJason M. Bills }
2090e855dd28SJason M. Bills 
2091424c4176SJason M. Bills class CrashdumpEntryCollection : public Node
20921da66f75SEd Tanous {
20931da66f75SEd Tanous   public:
20941da66f75SEd Tanous     template <typename CrowApp>
2095424c4176SJason M. Bills     CrashdumpEntryCollection(CrowApp& app) :
2096424c4176SJason M. Bills         Node(app, "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/")
20971da66f75SEd Tanous     {
20983946028dSAppaRao Puli         // Note: Deviated from redfish privilege registry for GET & HEAD
20993946028dSAppaRao Puli         // method for security reasons.
21001da66f75SEd Tanous         entityPrivileges = {
21013946028dSAppaRao Puli             {boost::beast::http::verb::get, {{"ConfigureComponents"}}},
21023946028dSAppaRao Puli             {boost::beast::http::verb::head, {{"ConfigureComponents"}}},
2103e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2104e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2105e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2106e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
21071da66f75SEd Tanous     }
21081da66f75SEd Tanous 
21091da66f75SEd Tanous   private:
21101da66f75SEd Tanous     /**
21111da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
21121da66f75SEd Tanous      */
21131da66f75SEd Tanous     void doGet(crow::Response& res, const crow::Request& req,
21141da66f75SEd Tanous                const std::vector<std::string>& params) override
21151da66f75SEd Tanous     {
2116e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
21171da66f75SEd Tanous         // Collections don't include the static data added by SubRoute because
21181da66f75SEd Tanous         // it has a duplicate entry for members
2119e1f26343SJason M. Bills         auto getLogEntriesCallback = [asyncResp](
2120e1f26343SJason M. Bills                                          const boost::system::error_code ec,
21211da66f75SEd Tanous                                          const std::vector<std::string>& resp) {
21221da66f75SEd Tanous             if (ec)
21231da66f75SEd Tanous             {
21241da66f75SEd Tanous                 if (ec.value() !=
21251da66f75SEd Tanous                     boost::system::errc::no_such_file_or_directory)
21261da66f75SEd Tanous                 {
21271da66f75SEd Tanous                     BMCWEB_LOG_DEBUG << "failed to get entries ec: "
21281da66f75SEd Tanous                                      << ec.message();
2129f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
21301da66f75SEd Tanous                     return;
21311da66f75SEd Tanous                 }
21321da66f75SEd Tanous             }
2133e1f26343SJason M. Bills             asyncResp->res.jsonValue["@odata.type"] =
21341da66f75SEd Tanous                 "#LogEntryCollection.LogEntryCollection";
21350f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
2136424c4176SJason M. Bills                 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
2137424c4176SJason M. Bills             asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
2138e1f26343SJason M. Bills             asyncResp->res.jsonValue["Description"] =
2139424c4176SJason M. Bills                 "Collection of Crashdump Entries";
2140e1f26343SJason M. Bills             nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2141e1f26343SJason M. Bills             logEntryArray = nlohmann::json::array();
2142e855dd28SJason M. Bills             std::vector<std::string> logIDs;
2143e855dd28SJason M. Bills             // Get the list of log entries and build up an empty array big
2144e855dd28SJason M. Bills             // enough to hold them
21451da66f75SEd Tanous             for (const std::string& objpath : resp)
21461da66f75SEd Tanous             {
2147e855dd28SJason M. Bills                 // Get the log ID
21484ed77cd5SEd Tanous                 std::size_t lastPos = objpath.rfind("/");
2149e855dd28SJason M. Bills                 if (lastPos == std::string::npos)
21501da66f75SEd Tanous                 {
2151e855dd28SJason M. Bills                     continue;
21521da66f75SEd Tanous                 }
2153e855dd28SJason M. Bills                 logIDs.emplace_back(objpath.substr(lastPos + 1));
2154e855dd28SJason M. Bills 
2155e855dd28SJason M. Bills                 // Add a space for the log entry to the array
2156e855dd28SJason M. Bills                 logEntryArray.push_back({});
2157e855dd28SJason M. Bills             }
2158e855dd28SJason M. Bills             // Now go through and set up async calls to fill in the entries
2159e855dd28SJason M. Bills             size_t index = 0;
2160e855dd28SJason M. Bills             for (const std::string& logID : logIDs)
2161e855dd28SJason M. Bills             {
2162e855dd28SJason M. Bills                 // Add the log entry to the array
2163e855dd28SJason M. Bills                 logCrashdumpEntry(asyncResp, logID, logEntryArray[index++]);
21641da66f75SEd Tanous             }
2165e1f26343SJason M. Bills             asyncResp->res.jsonValue["Members@odata.count"] =
2166e1f26343SJason M. Bills                 logEntryArray.size();
21671da66f75SEd Tanous         };
21681da66f75SEd Tanous         crow::connections::systemBus->async_method_call(
21691da66f75SEd Tanous             std::move(getLogEntriesCallback),
21701da66f75SEd Tanous             "xyz.openbmc_project.ObjectMapper",
21711da66f75SEd Tanous             "/xyz/openbmc_project/object_mapper",
21721da66f75SEd Tanous             "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "", 0,
21735b61b5e8SJason M. Bills             std::array<const char*, 1>{crashdumpInterface});
21741da66f75SEd Tanous     }
21751da66f75SEd Tanous };
21761da66f75SEd Tanous 
2177424c4176SJason M. Bills class CrashdumpEntry : public Node
21781da66f75SEd Tanous {
21791da66f75SEd Tanous   public:
2180424c4176SJason M. Bills     CrashdumpEntry(CrowApp& app) :
2181d53dd41fSJason M. Bills         Node(app,
2182424c4176SJason M. Bills              "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/",
21831da66f75SEd Tanous              std::string())
21841da66f75SEd Tanous     {
21853946028dSAppaRao Puli         // Note: Deviated from redfish privilege registry for GET & HEAD
21863946028dSAppaRao Puli         // method for security reasons.
21871da66f75SEd Tanous         entityPrivileges = {
21883946028dSAppaRao Puli             {boost::beast::http::verb::get, {{"ConfigureComponents"}}},
21893946028dSAppaRao Puli             {boost::beast::http::verb::head, {{"ConfigureComponents"}}},
2190e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2191e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2192e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2193e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
21941da66f75SEd Tanous     }
21951da66f75SEd Tanous 
21961da66f75SEd Tanous   private:
21971da66f75SEd Tanous     void doGet(crow::Response& res, const crow::Request& req,
21981da66f75SEd Tanous                const std::vector<std::string>& params) override
21991da66f75SEd Tanous     {
2200e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
22011da66f75SEd Tanous         if (params.size() != 1)
22021da66f75SEd Tanous         {
2203f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
22041da66f75SEd Tanous             return;
22051da66f75SEd Tanous         }
2206e855dd28SJason M. Bills         const std::string& logID = params[0];
2207e855dd28SJason M. Bills         logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
2208e855dd28SJason M. Bills     }
2209e855dd28SJason M. Bills };
2210e855dd28SJason M. Bills 
2211e855dd28SJason M. Bills class CrashdumpFile : public Node
2212e855dd28SJason M. Bills {
2213e855dd28SJason M. Bills   public:
2214e855dd28SJason M. Bills     CrashdumpFile(CrowApp& app) :
2215e855dd28SJason M. Bills         Node(app,
2216e855dd28SJason M. Bills              "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/"
2217e855dd28SJason M. Bills              "<str>/",
2218e855dd28SJason M. Bills              std::string(), std::string())
2219e855dd28SJason M. Bills     {
22203946028dSAppaRao Puli         // Note: Deviated from redfish privilege registry for GET & HEAD
22213946028dSAppaRao Puli         // method for security reasons.
2222e855dd28SJason M. Bills         entityPrivileges = {
22233946028dSAppaRao Puli             {boost::beast::http::verb::get, {{"ConfigureComponents"}}},
22243946028dSAppaRao Puli             {boost::beast::http::verb::head, {{"ConfigureComponents"}}},
2225e855dd28SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2226e855dd28SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2227e855dd28SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2228e855dd28SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
2229e855dd28SJason M. Bills     }
2230e855dd28SJason M. Bills 
2231e855dd28SJason M. Bills   private:
2232e855dd28SJason M. Bills     void doGet(crow::Response& res, const crow::Request& req,
2233e855dd28SJason M. Bills                const std::vector<std::string>& params) override
2234e855dd28SJason M. Bills     {
2235e855dd28SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
2236e855dd28SJason M. Bills         if (params.size() != 2)
2237e855dd28SJason M. Bills         {
2238e855dd28SJason M. Bills             messages::internalError(asyncResp->res);
2239e855dd28SJason M. Bills             return;
2240e855dd28SJason M. Bills         }
2241e855dd28SJason M. Bills         const std::string& logID = params[0];
2242e855dd28SJason M. Bills         const std::string& fileName = params[1];
2243e855dd28SJason M. Bills 
2244043a0536SJohnathan Mantey         auto getStoredLogCallback =
2245043a0536SJohnathan Mantey             [asyncResp, logID, fileName](
2246abf2add6SEd Tanous                 const boost::system::error_code ec,
2247043a0536SJohnathan Mantey                 const std::vector<std::pair<std::string, VariantType>>& resp) {
22481da66f75SEd Tanous                 if (ec)
22491da66f75SEd Tanous                 {
2250043a0536SJohnathan Mantey                     BMCWEB_LOG_DEBUG << "failed to get log ec: "
2251043a0536SJohnathan Mantey                                      << ec.message();
2252f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
22531da66f75SEd Tanous                     return;
22541da66f75SEd Tanous                 }
2255e855dd28SJason M. Bills 
2256043a0536SJohnathan Mantey                 std::string dbusFilename{};
2257043a0536SJohnathan Mantey                 std::string dbusTimestamp{};
2258043a0536SJohnathan Mantey                 std::string dbusFilepath{};
2259043a0536SJohnathan Mantey 
2260043a0536SJohnathan Mantey                 ParseCrashdumpParameters(resp, dbusFilename, dbusTimestamp,
2261043a0536SJohnathan Mantey                                          dbusFilepath);
2262043a0536SJohnathan Mantey 
2263043a0536SJohnathan Mantey                 if (dbusFilename.empty() || dbusTimestamp.empty() ||
2264043a0536SJohnathan Mantey                     dbusFilepath.empty())
22651da66f75SEd Tanous                 {
2266e855dd28SJason M. Bills                     messages::resourceMissingAtURI(asyncResp->res, fileName);
22671da66f75SEd Tanous                     return;
22681da66f75SEd Tanous                 }
2269e855dd28SJason M. Bills 
2270043a0536SJohnathan Mantey                 // Verify the file name parameter is correct
2271043a0536SJohnathan Mantey                 if (fileName != dbusFilename)
2272043a0536SJohnathan Mantey                 {
2273043a0536SJohnathan Mantey                     messages::resourceMissingAtURI(asyncResp->res, fileName);
2274043a0536SJohnathan Mantey                     return;
2275043a0536SJohnathan Mantey                 }
2276043a0536SJohnathan Mantey 
2277043a0536SJohnathan Mantey                 if (!std::filesystem::exists(dbusFilepath))
2278043a0536SJohnathan Mantey                 {
2279043a0536SJohnathan Mantey                     messages::resourceMissingAtURI(asyncResp->res, fileName);
2280043a0536SJohnathan Mantey                     return;
2281043a0536SJohnathan Mantey                 }
2282043a0536SJohnathan Mantey                 std::ifstream ifs(dbusFilepath, std::ios::in |
2283043a0536SJohnathan Mantey                                                     std::ios::binary |
2284043a0536SJohnathan Mantey                                                     std::ios::ate);
2285043a0536SJohnathan Mantey                 std::ifstream::pos_type fileSize = ifs.tellg();
2286043a0536SJohnathan Mantey                 if (fileSize < 0)
2287043a0536SJohnathan Mantey                 {
2288043a0536SJohnathan Mantey                     messages::generalError(asyncResp->res);
2289043a0536SJohnathan Mantey                     return;
2290043a0536SJohnathan Mantey                 }
2291043a0536SJohnathan Mantey                 ifs.seekg(0, std::ios::beg);
2292043a0536SJohnathan Mantey 
2293043a0536SJohnathan Mantey                 auto crashData = std::make_unique<char[]>(
2294043a0536SJohnathan Mantey                     static_cast<unsigned int>(fileSize));
2295043a0536SJohnathan Mantey 
2296043a0536SJohnathan Mantey                 ifs.read(crashData.get(), static_cast<int>(fileSize));
2297043a0536SJohnathan Mantey 
2298043a0536SJohnathan Mantey                 // The cast to std::string is intentional in order to use the
2299043a0536SJohnathan Mantey                 // assign() that applies move mechanics
2300043a0536SJohnathan Mantey                 asyncResp->res.body().assign(
2301043a0536SJohnathan Mantey                     static_cast<std::string>(crashData.get()));
2302043a0536SJohnathan Mantey 
2303043a0536SJohnathan Mantey                 // Configure this to be a file download when accessed from
2304043a0536SJohnathan Mantey                 // a browser
2305e855dd28SJason M. Bills                 asyncResp->res.addHeader("Content-Disposition", "attachment");
23061da66f75SEd Tanous             };
23071da66f75SEd Tanous         crow::connections::systemBus->async_method_call(
23085b61b5e8SJason M. Bills             std::move(getStoredLogCallback), crashdumpObject,
23095b61b5e8SJason M. Bills             crashdumpPath + std::string("/") + logID,
2310043a0536SJohnathan Mantey             "org.freedesktop.DBus.Properties", "GetAll", crashdumpInterface);
23111da66f75SEd Tanous     }
23121da66f75SEd Tanous };
23131da66f75SEd Tanous 
2314424c4176SJason M. Bills class OnDemandCrashdump : public Node
23151da66f75SEd Tanous {
23161da66f75SEd Tanous   public:
2317424c4176SJason M. Bills     OnDemandCrashdump(CrowApp& app) :
2318424c4176SJason M. Bills         Node(app,
2319424c4176SJason M. Bills              "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/Oem/"
2320424c4176SJason M. Bills              "Crashdump.OnDemand/")
23211da66f75SEd Tanous     {
23223946028dSAppaRao Puli         // Note: Deviated from redfish privilege registry for GET & HEAD
23233946028dSAppaRao Puli         // method for security reasons.
23241da66f75SEd Tanous         entityPrivileges = {
23253946028dSAppaRao Puli             {boost::beast::http::verb::get, {{"ConfigureComponents"}}},
23263946028dSAppaRao Puli             {boost::beast::http::verb::head, {{"ConfigureComponents"}}},
23273946028dSAppaRao Puli             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
23283946028dSAppaRao Puli             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
23293946028dSAppaRao Puli             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
23303946028dSAppaRao Puli             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
23311da66f75SEd Tanous     }
23321da66f75SEd Tanous 
23331da66f75SEd Tanous   private:
23341da66f75SEd Tanous     void doPost(crow::Response& res, const crow::Request& req,
23351da66f75SEd Tanous                 const std::vector<std::string>& params) override
23361da66f75SEd Tanous     {
2337e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
23381da66f75SEd Tanous 
2339fe306728SJames Feist         auto generateonDemandLogCallback = [asyncResp,
2340fe306728SJames Feist                                             req](const boost::system::error_code
234146229577SJames Feist                                                      ec,
23421da66f75SEd Tanous                                                  const std::string& resp) {
23431da66f75SEd Tanous             if (ec)
23441da66f75SEd Tanous             {
234546229577SJames Feist                 if (ec.value() == boost::system::errc::operation_not_supported)
23461da66f75SEd Tanous                 {
2347f12894f8SJason M. Bills                     messages::resourceInStandby(asyncResp->res);
23481da66f75SEd Tanous                 }
23494363d3b2SJason M. Bills                 else if (ec.value() ==
23504363d3b2SJason M. Bills                          boost::system::errc::device_or_resource_busy)
23514363d3b2SJason M. Bills                 {
23524363d3b2SJason M. Bills                     messages::serviceTemporarilyUnavailable(asyncResp->res,
23534363d3b2SJason M. Bills                                                             "60");
23544363d3b2SJason M. Bills                 }
23551da66f75SEd Tanous                 else
23561da66f75SEd Tanous                 {
2357f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
23581da66f75SEd Tanous                 }
23591da66f75SEd Tanous                 return;
23601da66f75SEd Tanous             }
236146229577SJames Feist             std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
236266afe4faSJames Feist                 [](boost::system::error_code err, sdbusplus::message::message&,
236366afe4faSJames Feist                    const std::shared_ptr<task::TaskData>& taskData) {
236466afe4faSJames Feist                     if (!err)
236566afe4faSJames Feist                     {
2366e5d5006bSJames Feist                         taskData->messages.emplace_back(
2367e5d5006bSJames Feist                             messages::taskCompletedOK(
2368e5d5006bSJames Feist                                 std::to_string(taskData->index)));
2369831d6b09SJames Feist                         taskData->state = "Completed";
237066afe4faSJames Feist                     }
237132898ceaSJames Feist                     return task::completed;
237266afe4faSJames Feist                 },
237346229577SJames Feist                 "type='signal',interface='org.freedesktop.DBus.Properties',"
237446229577SJames Feist                 "member='PropertiesChanged',arg0namespace='com.intel."
237546229577SJames Feist                 "crashdump'");
237646229577SJames Feist             task->startTimer(std::chrono::minutes(5));
237746229577SJames Feist             task->populateResp(asyncResp->res);
2378fe306728SJames Feist             task->payload.emplace(req);
23791da66f75SEd Tanous         };
23801da66f75SEd Tanous         crow::connections::systemBus->async_method_call(
23815b61b5e8SJason M. Bills             std::move(generateonDemandLogCallback), crashdumpObject,
23825b61b5e8SJason M. Bills             crashdumpPath, crashdumpOnDemandInterface, "GenerateOnDemandLog");
23831da66f75SEd Tanous     }
23841da66f75SEd Tanous };
23851da66f75SEd Tanous 
23866eda7685SKenny L. Ku class TelemetryCrashdump : public Node
23876eda7685SKenny L. Ku {
23886eda7685SKenny L. Ku   public:
23896eda7685SKenny L. Ku     TelemetryCrashdump(CrowApp& app) :
23906eda7685SKenny L. Ku         Node(app,
23916eda7685SKenny L. Ku              "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/Oem/"
23926eda7685SKenny L. Ku              "Crashdump.Telemetry/")
23936eda7685SKenny L. Ku     {
23946eda7685SKenny L. Ku         // Note: Deviated from redfish privilege registry for GET & HEAD
23956eda7685SKenny L. Ku         // method for security reasons.
23966eda7685SKenny L. Ku         entityPrivileges = {
23976eda7685SKenny L. Ku             {boost::beast::http::verb::get, {{"ConfigureComponents"}}},
23986eda7685SKenny L. Ku             {boost::beast::http::verb::head, {{"ConfigureComponents"}}},
23996eda7685SKenny L. Ku             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
24006eda7685SKenny L. Ku             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
24016eda7685SKenny L. Ku             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
24026eda7685SKenny L. Ku             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
24036eda7685SKenny L. Ku     }
24046eda7685SKenny L. Ku 
24056eda7685SKenny L. Ku   private:
24066eda7685SKenny L. Ku     void doPost(crow::Response& res, const crow::Request& req,
24076eda7685SKenny L. Ku                 const std::vector<std::string>& params) override
24086eda7685SKenny L. Ku     {
24096eda7685SKenny L. Ku         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
24106eda7685SKenny L. Ku 
24116eda7685SKenny L. Ku         auto generateTelemetryLogCallback = [asyncResp, req](
24126eda7685SKenny L. Ku                                                 const boost::system::error_code
24136eda7685SKenny L. Ku                                                     ec,
24146eda7685SKenny L. Ku                                                 const std::string& resp) {
24156eda7685SKenny L. Ku             if (ec)
24166eda7685SKenny L. Ku             {
24176eda7685SKenny L. Ku                 if (ec.value() == boost::system::errc::operation_not_supported)
24186eda7685SKenny L. Ku                 {
24196eda7685SKenny L. Ku                     messages::resourceInStandby(asyncResp->res);
24206eda7685SKenny L. Ku                 }
24216eda7685SKenny L. Ku                 else if (ec.value() ==
24226eda7685SKenny L. Ku                          boost::system::errc::device_or_resource_busy)
24236eda7685SKenny L. Ku                 {
24246eda7685SKenny L. Ku                     messages::serviceTemporarilyUnavailable(asyncResp->res,
24256eda7685SKenny L. Ku                                                             "60");
24266eda7685SKenny L. Ku                 }
24276eda7685SKenny L. Ku                 else
24286eda7685SKenny L. Ku                 {
24296eda7685SKenny L. Ku                     messages::internalError(asyncResp->res);
24306eda7685SKenny L. Ku                 }
24316eda7685SKenny L. Ku                 return;
24326eda7685SKenny L. Ku             }
24336eda7685SKenny L. Ku             std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
24346eda7685SKenny L. Ku                 [](boost::system::error_code err, sdbusplus::message::message&,
24356eda7685SKenny L. Ku                    const std::shared_ptr<task::TaskData>& taskData) {
24366eda7685SKenny L. Ku                     if (!err)
24376eda7685SKenny L. Ku                     {
24386eda7685SKenny L. Ku                         taskData->messages.emplace_back(
24396eda7685SKenny L. Ku                             messages::taskCompletedOK(
24406eda7685SKenny L. Ku                                 std::to_string(taskData->index)));
24416eda7685SKenny L. Ku                         taskData->state = "Completed";
24426eda7685SKenny L. Ku                     }
24436eda7685SKenny L. Ku                     return task::completed;
24446eda7685SKenny L. Ku                 },
24456eda7685SKenny L. Ku                 "type='signal',interface='org.freedesktop.DBus.Properties',"
24466eda7685SKenny L. Ku                 "member='PropertiesChanged',arg0namespace='com.intel."
24476eda7685SKenny L. Ku                 "crashdump'");
24486eda7685SKenny L. Ku             task->startTimer(std::chrono::minutes(5));
24496eda7685SKenny L. Ku             task->populateResp(asyncResp->res);
24506eda7685SKenny L. Ku             task->payload.emplace(req);
24516eda7685SKenny L. Ku         };
24526eda7685SKenny L. Ku         crow::connections::systemBus->async_method_call(
24536eda7685SKenny L. Ku             std::move(generateTelemetryLogCallback), crashdumpObject,
24546eda7685SKenny L. Ku             crashdumpPath, crashdumpTelemetryInterface, "GenerateTelemetryLog");
24556eda7685SKenny L. Ku     }
24566eda7685SKenny L. Ku };
24576eda7685SKenny L. Ku 
2458e1f26343SJason M. Bills class SendRawPECI : public Node
24591da66f75SEd Tanous {
24601da66f75SEd Tanous   public:
2461e1f26343SJason M. Bills     SendRawPECI(CrowApp& app) :
2462424c4176SJason M. Bills         Node(app,
2463424c4176SJason M. Bills              "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/Oem/"
2464424c4176SJason M. Bills              "Crashdump.SendRawPeci/")
24651da66f75SEd Tanous     {
24663946028dSAppaRao Puli         // Note: Deviated from redfish privilege registry for GET & HEAD
24673946028dSAppaRao Puli         // method for security reasons.
24681da66f75SEd Tanous         entityPrivileges = {
24691da66f75SEd Tanous             {boost::beast::http::verb::get, {{"ConfigureComponents"}}},
24701da66f75SEd Tanous             {boost::beast::http::verb::head, {{"ConfigureComponents"}}},
24711da66f75SEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
24721da66f75SEd Tanous             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
24731da66f75SEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
24741da66f75SEd Tanous             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
24751da66f75SEd Tanous     }
24761da66f75SEd Tanous 
24771da66f75SEd Tanous   private:
24781da66f75SEd Tanous     void doPost(crow::Response& res, const crow::Request& req,
24791da66f75SEd Tanous                 const std::vector<std::string>& params) override
24801da66f75SEd Tanous     {
2481e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
24828724c297SKarthick Sundarrajan         std::vector<std::vector<uint8_t>> peciCommands;
24838724c297SKarthick Sundarrajan 
24848724c297SKarthick Sundarrajan         nlohmann::json reqJson =
24858724c297SKarthick Sundarrajan             nlohmann::json::parse(req.body, nullptr, false);
24868724c297SKarthick Sundarrajan         if (reqJson.find("PECICommands") != reqJson.end())
24878724c297SKarthick Sundarrajan         {
24888724c297SKarthick Sundarrajan             if (!json_util::readJson(req, res, "PECICommands", peciCommands))
24898724c297SKarthick Sundarrajan             {
24908724c297SKarthick Sundarrajan                 return;
24918724c297SKarthick Sundarrajan             }
24928724c297SKarthick Sundarrajan             uint32_t idx = 0;
24938724c297SKarthick Sundarrajan             for (auto const& cmd : peciCommands)
24948724c297SKarthick Sundarrajan             {
24958724c297SKarthick Sundarrajan                 if (cmd.size() < 3)
24968724c297SKarthick Sundarrajan                 {
24978724c297SKarthick Sundarrajan                     std::string s("[");
24988724c297SKarthick Sundarrajan                     for (auto const& val : cmd)
24998724c297SKarthick Sundarrajan                     {
25008724c297SKarthick Sundarrajan                         if (val != *cmd.begin())
25018724c297SKarthick Sundarrajan                         {
25028724c297SKarthick Sundarrajan                             s += ",";
25038724c297SKarthick Sundarrajan                         }
25048724c297SKarthick Sundarrajan                         s += std::to_string(val);
25058724c297SKarthick Sundarrajan                     }
25068724c297SKarthick Sundarrajan                     s += "]";
25078724c297SKarthick Sundarrajan                     messages::actionParameterValueFormatError(
25088724c297SKarthick Sundarrajan                         res, s, "PECICommands[" + std::to_string(idx) + "]",
25098724c297SKarthick Sundarrajan                         "SendRawPeci");
25108724c297SKarthick Sundarrajan                     return;
25118724c297SKarthick Sundarrajan                 }
25128724c297SKarthick Sundarrajan                 idx++;
25138724c297SKarthick Sundarrajan             }
25148724c297SKarthick Sundarrajan         }
25158724c297SKarthick Sundarrajan         else
25168724c297SKarthick Sundarrajan         {
25178724c297SKarthick Sundarrajan             /* This interface is deprecated */
2518b1556427SEd Tanous             uint8_t clientAddress = 0;
2519b1556427SEd Tanous             uint8_t readLength = 0;
25201da66f75SEd Tanous             std::vector<uint8_t> peciCommand;
2521b1556427SEd Tanous             if (!json_util::readJson(req, res, "ClientAddress", clientAddress,
2522b1556427SEd Tanous                                      "ReadLength", readLength, "PECICommand",
2523b1556427SEd Tanous                                      peciCommand))
25241da66f75SEd Tanous             {
25251da66f75SEd Tanous                 return;
25261da66f75SEd Tanous             }
25278724c297SKarthick Sundarrajan             peciCommands.push_back({clientAddress, 0, readLength});
25288724c297SKarthick Sundarrajan             peciCommands[0].insert(peciCommands[0].end(), peciCommand.begin(),
25298724c297SKarthick Sundarrajan                                    peciCommand.end());
25308724c297SKarthick Sundarrajan         }
25311da66f75SEd Tanous         // Callback to return the Raw PECI response
2532e1f26343SJason M. Bills         auto sendRawPECICallback =
2533e1f26343SJason M. Bills             [asyncResp](const boost::system::error_code ec,
25348724c297SKarthick Sundarrajan                         const std::vector<std::vector<uint8_t>>& resp) {
25351da66f75SEd Tanous                 if (ec)
25361da66f75SEd Tanous                 {
25378724c297SKarthick Sundarrajan                     BMCWEB_LOG_DEBUG << "failed to process PECI commands ec: "
25381da66f75SEd Tanous                                      << ec.message();
2539f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
25401da66f75SEd Tanous                     return;
25411da66f75SEd Tanous                 }
2542e1f26343SJason M. Bills                 asyncResp->res.jsonValue = {{"Name", "PECI Command Response"},
25431da66f75SEd Tanous                                             {"PECIResponse", resp}};
25441da66f75SEd Tanous             };
25451da66f75SEd Tanous         // Call the SendRawPECI command with the provided data
25461da66f75SEd Tanous         crow::connections::systemBus->async_method_call(
25475b61b5e8SJason M. Bills             std::move(sendRawPECICallback), crashdumpObject, crashdumpPath,
25488724c297SKarthick Sundarrajan             crashdumpRawPECIInterface, "SendRawPeci", peciCommands);
25491da66f75SEd Tanous     }
25501da66f75SEd Tanous };
25511da66f75SEd Tanous 
2552cb92c03bSAndrew Geissler /**
2553cb92c03bSAndrew Geissler  * DBusLogServiceActionsClear class supports POST method for ClearLog action.
2554cb92c03bSAndrew Geissler  */
2555cb92c03bSAndrew Geissler class DBusLogServiceActionsClear : public Node
2556cb92c03bSAndrew Geissler {
2557cb92c03bSAndrew Geissler   public:
2558cb92c03bSAndrew Geissler     DBusLogServiceActionsClear(CrowApp& app) :
2559cb92c03bSAndrew Geissler         Node(app, "/redfish/v1/Systems/system/LogServices/EventLog/Actions/"
25607af91514SGunnar Mills                   "LogService.ClearLog/")
2561cb92c03bSAndrew Geissler     {
2562cb92c03bSAndrew Geissler         entityPrivileges = {
2563cb92c03bSAndrew Geissler             {boost::beast::http::verb::get, {{"Login"}}},
2564cb92c03bSAndrew Geissler             {boost::beast::http::verb::head, {{"Login"}}},
2565cb92c03bSAndrew Geissler             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2566cb92c03bSAndrew Geissler             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2567cb92c03bSAndrew Geissler             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2568cb92c03bSAndrew Geissler             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
2569cb92c03bSAndrew Geissler     }
2570cb92c03bSAndrew Geissler 
2571cb92c03bSAndrew Geissler   private:
2572cb92c03bSAndrew Geissler     /**
2573cb92c03bSAndrew Geissler      * Function handles POST method request.
2574cb92c03bSAndrew Geissler      * The Clear Log actions does not require any parameter.The action deletes
2575cb92c03bSAndrew Geissler      * all entries found in the Entries collection for this Log Service.
2576cb92c03bSAndrew Geissler      */
2577cb92c03bSAndrew Geissler     void doPost(crow::Response& res, const crow::Request& req,
2578cb92c03bSAndrew Geissler                 const std::vector<std::string>& params) override
2579cb92c03bSAndrew Geissler     {
2580cb92c03bSAndrew Geissler         BMCWEB_LOG_DEBUG << "Do delete all entries.";
2581cb92c03bSAndrew Geissler 
2582cb92c03bSAndrew Geissler         auto asyncResp = std::make_shared<AsyncResp>(res);
2583cb92c03bSAndrew Geissler         // Process response from Logging service.
2584cb92c03bSAndrew Geissler         auto resp_handler = [asyncResp](const boost::system::error_code ec) {
2585cb92c03bSAndrew Geissler             BMCWEB_LOG_DEBUG << "doClearLog resp_handler callback: Done";
2586cb92c03bSAndrew Geissler             if (ec)
2587cb92c03bSAndrew Geissler             {
2588cb92c03bSAndrew Geissler                 // TODO Handle for specific error code
2589cb92c03bSAndrew Geissler                 BMCWEB_LOG_ERROR << "doClearLog resp_handler got error " << ec;
2590cb92c03bSAndrew Geissler                 asyncResp->res.result(
2591cb92c03bSAndrew Geissler                     boost::beast::http::status::internal_server_error);
2592cb92c03bSAndrew Geissler                 return;
2593cb92c03bSAndrew Geissler             }
2594cb92c03bSAndrew Geissler 
2595cb92c03bSAndrew Geissler             asyncResp->res.result(boost::beast::http::status::no_content);
2596cb92c03bSAndrew Geissler         };
2597cb92c03bSAndrew Geissler 
2598cb92c03bSAndrew Geissler         // Make call to Logging service to request Clear Log
2599cb92c03bSAndrew Geissler         crow::connections::systemBus->async_method_call(
2600cb92c03bSAndrew Geissler             resp_handler, "xyz.openbmc_project.Logging",
2601cb92c03bSAndrew Geissler             "/xyz/openbmc_project/logging",
2602cb92c03bSAndrew Geissler             "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
2603cb92c03bSAndrew Geissler     }
2604cb92c03bSAndrew Geissler };
2605a3316fc6SZhikuiRen 
2606a3316fc6SZhikuiRen /****************************************************
2607a3316fc6SZhikuiRen  * Redfish PostCode interfaces
2608a3316fc6SZhikuiRen  * using DBUS interface: getPostCodesTS
2609a3316fc6SZhikuiRen  ******************************************************/
2610a3316fc6SZhikuiRen class PostCodesLogService : public Node
2611a3316fc6SZhikuiRen {
2612a3316fc6SZhikuiRen   public:
2613a3316fc6SZhikuiRen     PostCodesLogService(CrowApp& app) :
2614a3316fc6SZhikuiRen         Node(app, "/redfish/v1/Systems/system/LogServices/PostCodes/")
2615a3316fc6SZhikuiRen     {
2616a3316fc6SZhikuiRen         entityPrivileges = {
2617a3316fc6SZhikuiRen             {boost::beast::http::verb::get, {{"Login"}}},
2618a3316fc6SZhikuiRen             {boost::beast::http::verb::head, {{"Login"}}},
2619a3316fc6SZhikuiRen             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2620a3316fc6SZhikuiRen             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2621a3316fc6SZhikuiRen             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2622a3316fc6SZhikuiRen             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
2623a3316fc6SZhikuiRen     }
2624a3316fc6SZhikuiRen 
2625a3316fc6SZhikuiRen   private:
2626a3316fc6SZhikuiRen     void doGet(crow::Response& res, const crow::Request& req,
2627a3316fc6SZhikuiRen                const std::vector<std::string>& params) override
2628a3316fc6SZhikuiRen     {
2629a3316fc6SZhikuiRen         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
2630a3316fc6SZhikuiRen 
2631a3316fc6SZhikuiRen         asyncResp->res.jsonValue = {
2632a3316fc6SZhikuiRen             {"@odata.id", "/redfish/v1/Systems/system/LogServices/PostCodes"},
2633a3316fc6SZhikuiRen             {"@odata.type", "#LogService.v1_1_0.LogService"},
2634a3316fc6SZhikuiRen             {"@odata.context", "/redfish/v1/$metadata#LogService.LogService"},
2635a3316fc6SZhikuiRen             {"Name", "POST Code Log Service"},
2636a3316fc6SZhikuiRen             {"Description", "POST Code Log Service"},
2637a3316fc6SZhikuiRen             {"Id", "BIOS POST Code Log"},
2638a3316fc6SZhikuiRen             {"OverWritePolicy", "WrapsWhenFull"},
2639a3316fc6SZhikuiRen             {"Entries",
2640a3316fc6SZhikuiRen              {{"@odata.id",
2641a3316fc6SZhikuiRen                "/redfish/v1/Systems/system/LogServices/PostCodes/Entries"}}}};
2642a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
2643a3316fc6SZhikuiRen             {"target", "/redfish/v1/Systems/system/LogServices/PostCodes/"
2644a3316fc6SZhikuiRen                        "Actions/LogService.ClearLog"}};
2645a3316fc6SZhikuiRen     }
2646a3316fc6SZhikuiRen };
2647a3316fc6SZhikuiRen 
2648a3316fc6SZhikuiRen class PostCodesClear : public Node
2649a3316fc6SZhikuiRen {
2650a3316fc6SZhikuiRen   public:
2651a3316fc6SZhikuiRen     PostCodesClear(CrowApp& app) :
2652a3316fc6SZhikuiRen         Node(app, "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/"
2653a3316fc6SZhikuiRen                   "LogService.ClearLog/")
2654a3316fc6SZhikuiRen     {
2655a3316fc6SZhikuiRen         entityPrivileges = {
2656a3316fc6SZhikuiRen             {boost::beast::http::verb::get, {{"Login"}}},
2657a3316fc6SZhikuiRen             {boost::beast::http::verb::head, {{"Login"}}},
26583946028dSAppaRao Puli             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
26593946028dSAppaRao Puli             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
26603946028dSAppaRao Puli             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
26613946028dSAppaRao Puli             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
2662a3316fc6SZhikuiRen     }
2663a3316fc6SZhikuiRen 
2664a3316fc6SZhikuiRen   private:
2665a3316fc6SZhikuiRen     void doPost(crow::Response& res, const crow::Request& req,
2666a3316fc6SZhikuiRen                 const std::vector<std::string>& params) override
2667a3316fc6SZhikuiRen     {
2668a3316fc6SZhikuiRen         BMCWEB_LOG_DEBUG << "Do delete all postcodes entries.";
2669a3316fc6SZhikuiRen 
2670a3316fc6SZhikuiRen         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
2671a3316fc6SZhikuiRen         // Make call to post-code service to request clear all
2672a3316fc6SZhikuiRen         crow::connections::systemBus->async_method_call(
2673a3316fc6SZhikuiRen             [asyncResp](const boost::system::error_code ec) {
2674a3316fc6SZhikuiRen                 if (ec)
2675a3316fc6SZhikuiRen                 {
2676a3316fc6SZhikuiRen                     // TODO Handle for specific error code
2677a3316fc6SZhikuiRen                     BMCWEB_LOG_ERROR
2678a3316fc6SZhikuiRen                         << "doClearPostCodes resp_handler got error " << ec;
2679a3316fc6SZhikuiRen                     asyncResp->res.result(
2680a3316fc6SZhikuiRen                         boost::beast::http::status::internal_server_error);
2681a3316fc6SZhikuiRen                     messages::internalError(asyncResp->res);
2682a3316fc6SZhikuiRen                     return;
2683a3316fc6SZhikuiRen                 }
2684a3316fc6SZhikuiRen             },
2685a3316fc6SZhikuiRen             "xyz.openbmc_project.State.Boot.PostCode",
2686a3316fc6SZhikuiRen             "/xyz/openbmc_project/State/Boot/PostCode",
2687a3316fc6SZhikuiRen             "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
2688a3316fc6SZhikuiRen     }
2689a3316fc6SZhikuiRen };
2690a3316fc6SZhikuiRen 
2691a3316fc6SZhikuiRen static void fillPostCodeEntry(
2692a3316fc6SZhikuiRen     std::shared_ptr<AsyncResp> aResp,
2693a3316fc6SZhikuiRen     const boost::container::flat_map<uint64_t, uint64_t>& postcode,
2694a3316fc6SZhikuiRen     const uint16_t bootIndex, const uint64_t codeIndex = 0,
2695a3316fc6SZhikuiRen     const uint64_t skip = 0, const uint64_t top = 0)
2696a3316fc6SZhikuiRen {
2697a3316fc6SZhikuiRen     // Get the Message from the MessageRegistry
2698a3316fc6SZhikuiRen     const message_registries::Message* message =
2699a3316fc6SZhikuiRen         message_registries::getMessage("OpenBMC.0.1.BIOSPOSTCode");
2700a3316fc6SZhikuiRen 
2701a3316fc6SZhikuiRen     uint64_t currentCodeIndex = 0;
2702a3316fc6SZhikuiRen     nlohmann::json& logEntryArray = aResp->res.jsonValue["Members"];
2703a3316fc6SZhikuiRen 
2704a3316fc6SZhikuiRen     uint64_t firstCodeTimeUs = 0;
2705a3316fc6SZhikuiRen     for (const std::pair<uint64_t, uint64_t>& code : postcode)
2706a3316fc6SZhikuiRen     {
2707a3316fc6SZhikuiRen         currentCodeIndex++;
2708a3316fc6SZhikuiRen         std::string postcodeEntryID =
2709a3316fc6SZhikuiRen             "B" + std::to_string(bootIndex) + "-" +
2710a3316fc6SZhikuiRen             std::to_string(currentCodeIndex); // 1 based index in EntryID string
2711a3316fc6SZhikuiRen 
2712a3316fc6SZhikuiRen         uint64_t usecSinceEpoch = code.first;
2713a3316fc6SZhikuiRen         uint64_t usTimeOffset = 0;
2714a3316fc6SZhikuiRen 
2715a3316fc6SZhikuiRen         if (1 == currentCodeIndex)
2716a3316fc6SZhikuiRen         { // already incremented
2717a3316fc6SZhikuiRen             firstCodeTimeUs = code.first;
2718a3316fc6SZhikuiRen         }
2719a3316fc6SZhikuiRen         else
2720a3316fc6SZhikuiRen         {
2721a3316fc6SZhikuiRen             usTimeOffset = code.first - firstCodeTimeUs;
2722a3316fc6SZhikuiRen         }
2723a3316fc6SZhikuiRen 
2724a3316fc6SZhikuiRen         // skip if no specific codeIndex is specified and currentCodeIndex does
2725a3316fc6SZhikuiRen         // not fall between top and skip
2726a3316fc6SZhikuiRen         if ((codeIndex == 0) &&
2727a3316fc6SZhikuiRen             (currentCodeIndex <= skip || currentCodeIndex > top))
2728a3316fc6SZhikuiRen         {
2729a3316fc6SZhikuiRen             continue;
2730a3316fc6SZhikuiRen         }
2731a3316fc6SZhikuiRen 
2732*4e0453b1SGunnar Mills         // skip if a specific codeIndex is specified and does not match the
2733a3316fc6SZhikuiRen         // currentIndex
2734a3316fc6SZhikuiRen         if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
2735a3316fc6SZhikuiRen         {
2736a3316fc6SZhikuiRen             // This is done for simplicity. 1st entry is needed to calculate
2737a3316fc6SZhikuiRen             // time offset. To improve efficiency, one can get to the entry
2738a3316fc6SZhikuiRen             // directly (possibly with flatmap's nth method)
2739a3316fc6SZhikuiRen             continue;
2740a3316fc6SZhikuiRen         }
2741a3316fc6SZhikuiRen 
2742a3316fc6SZhikuiRen         // currentCodeIndex is within top and skip or equal to specified code
2743a3316fc6SZhikuiRen         // index
2744a3316fc6SZhikuiRen 
2745a3316fc6SZhikuiRen         // Get the Created time from the timestamp
2746a3316fc6SZhikuiRen         std::string entryTimeStr;
2747a3316fc6SZhikuiRen         if (!getTimestampStr(usecSinceEpoch, entryTimeStr))
2748a3316fc6SZhikuiRen         {
2749a3316fc6SZhikuiRen             continue;
2750a3316fc6SZhikuiRen         }
2751a3316fc6SZhikuiRen 
2752a3316fc6SZhikuiRen         // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
2753a3316fc6SZhikuiRen         std::ostringstream hexCode;
2754a3316fc6SZhikuiRen         hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
2755a3316fc6SZhikuiRen                 << code.second;
2756a3316fc6SZhikuiRen         std::ostringstream timeOffsetStr;
2757a3316fc6SZhikuiRen         // Set Fixed -Point Notation
2758a3316fc6SZhikuiRen         timeOffsetStr << std::fixed;
2759a3316fc6SZhikuiRen         // Set precision to 4 digits
2760a3316fc6SZhikuiRen         timeOffsetStr << std::setprecision(4);
2761a3316fc6SZhikuiRen         // Add double to stream
2762a3316fc6SZhikuiRen         timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
2763a3316fc6SZhikuiRen         std::vector<std::string> messageArgs = {
2764a3316fc6SZhikuiRen             std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()};
2765a3316fc6SZhikuiRen 
2766a3316fc6SZhikuiRen         // Get MessageArgs template from message registry
2767a3316fc6SZhikuiRen         std::string msg;
2768a3316fc6SZhikuiRen         if (message != nullptr)
2769a3316fc6SZhikuiRen         {
2770a3316fc6SZhikuiRen             msg = message->message;
2771a3316fc6SZhikuiRen 
2772a3316fc6SZhikuiRen             // fill in this post code value
2773a3316fc6SZhikuiRen             int i = 0;
2774a3316fc6SZhikuiRen             for (const std::string& messageArg : messageArgs)
2775a3316fc6SZhikuiRen             {
2776a3316fc6SZhikuiRen                 std::string argStr = "%" + std::to_string(++i);
2777a3316fc6SZhikuiRen                 size_t argPos = msg.find(argStr);
2778a3316fc6SZhikuiRen                 if (argPos != std::string::npos)
2779a3316fc6SZhikuiRen                 {
2780a3316fc6SZhikuiRen                     msg.replace(argPos, argStr.length(), messageArg);
2781a3316fc6SZhikuiRen                 }
2782a3316fc6SZhikuiRen             }
2783a3316fc6SZhikuiRen         }
2784a3316fc6SZhikuiRen 
2785d4342a92STim Lee         // Get Severity template from message registry
2786d4342a92STim Lee         std::string severity;
2787d4342a92STim Lee         if (message != nullptr)
2788d4342a92STim Lee         {
2789d4342a92STim Lee             severity = message->severity;
2790d4342a92STim Lee         }
2791d4342a92STim Lee 
2792a3316fc6SZhikuiRen         // add to AsyncResp
2793a3316fc6SZhikuiRen         logEntryArray.push_back({});
2794a3316fc6SZhikuiRen         nlohmann::json& bmcLogEntry = logEntryArray.back();
2795a3316fc6SZhikuiRen         bmcLogEntry = {
2796a3316fc6SZhikuiRen             {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
2797a3316fc6SZhikuiRen             {"@odata.context", "/redfish/v1/$metadata#LogEntry.LogEntry"},
2798a3316fc6SZhikuiRen             {"@odata.id", "/redfish/v1/Systems/system/LogServices/"
2799a3316fc6SZhikuiRen                           "PostCodes/Entries/" +
2800a3316fc6SZhikuiRen                               postcodeEntryID},
2801a3316fc6SZhikuiRen             {"Name", "POST Code Log Entry"},
2802a3316fc6SZhikuiRen             {"Id", postcodeEntryID},
2803a3316fc6SZhikuiRen             {"Message", std::move(msg)},
2804a3316fc6SZhikuiRen             {"MessageId", "OpenBMC.0.1.BIOSPOSTCode"},
2805a3316fc6SZhikuiRen             {"MessageArgs", std::move(messageArgs)},
2806a3316fc6SZhikuiRen             {"EntryType", "Event"},
2807a3316fc6SZhikuiRen             {"Severity", std::move(severity)},
2808a3316fc6SZhikuiRen             {"Created", std::move(entryTimeStr)}};
2809a3316fc6SZhikuiRen     }
2810a3316fc6SZhikuiRen }
2811a3316fc6SZhikuiRen 
2812a3316fc6SZhikuiRen static void getPostCodeForEntry(std::shared_ptr<AsyncResp> aResp,
2813a3316fc6SZhikuiRen                                 const uint16_t bootIndex,
2814a3316fc6SZhikuiRen                                 const uint64_t codeIndex)
2815a3316fc6SZhikuiRen {
2816a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
2817a3316fc6SZhikuiRen         [aResp, bootIndex, codeIndex](
2818a3316fc6SZhikuiRen             const boost::system::error_code ec,
2819a3316fc6SZhikuiRen             const boost::container::flat_map<uint64_t, uint64_t>& postcode) {
2820a3316fc6SZhikuiRen             if (ec)
2821a3316fc6SZhikuiRen             {
2822a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
2823a3316fc6SZhikuiRen                 messages::internalError(aResp->res);
2824a3316fc6SZhikuiRen                 return;
2825a3316fc6SZhikuiRen             }
2826a3316fc6SZhikuiRen 
2827a3316fc6SZhikuiRen             // skip the empty postcode boots
2828a3316fc6SZhikuiRen             if (postcode.empty())
2829a3316fc6SZhikuiRen             {
2830a3316fc6SZhikuiRen                 return;
2831a3316fc6SZhikuiRen             }
2832a3316fc6SZhikuiRen 
2833a3316fc6SZhikuiRen             fillPostCodeEntry(aResp, postcode, bootIndex, codeIndex);
2834a3316fc6SZhikuiRen 
2835a3316fc6SZhikuiRen             aResp->res.jsonValue["Members@odata.count"] =
2836a3316fc6SZhikuiRen                 aResp->res.jsonValue["Members"].size();
2837a3316fc6SZhikuiRen         },
2838a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode",
2839a3316fc6SZhikuiRen         "/xyz/openbmc_project/State/Boot/PostCode",
2840a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
2841a3316fc6SZhikuiRen         bootIndex);
2842a3316fc6SZhikuiRen }
2843a3316fc6SZhikuiRen 
2844a3316fc6SZhikuiRen static void getPostCodeForBoot(std::shared_ptr<AsyncResp> aResp,
2845a3316fc6SZhikuiRen                                const uint16_t bootIndex,
2846a3316fc6SZhikuiRen                                const uint16_t bootCount,
2847a3316fc6SZhikuiRen                                const uint64_t entryCount, const uint64_t skip,
2848a3316fc6SZhikuiRen                                const uint64_t top)
2849a3316fc6SZhikuiRen {
2850a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
2851a3316fc6SZhikuiRen         [aResp, bootIndex, bootCount, entryCount, skip,
2852a3316fc6SZhikuiRen          top](const boost::system::error_code ec,
2853a3316fc6SZhikuiRen               const boost::container::flat_map<uint64_t, uint64_t>& postcode) {
2854a3316fc6SZhikuiRen             if (ec)
2855a3316fc6SZhikuiRen             {
2856a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
2857a3316fc6SZhikuiRen                 messages::internalError(aResp->res);
2858a3316fc6SZhikuiRen                 return;
2859a3316fc6SZhikuiRen             }
2860a3316fc6SZhikuiRen 
2861a3316fc6SZhikuiRen             uint64_t endCount = entryCount;
2862a3316fc6SZhikuiRen             if (!postcode.empty())
2863a3316fc6SZhikuiRen             {
2864a3316fc6SZhikuiRen                 endCount = entryCount + postcode.size();
2865a3316fc6SZhikuiRen 
2866a3316fc6SZhikuiRen                 if ((skip < endCount) && ((top + skip) > entryCount))
2867a3316fc6SZhikuiRen                 {
2868a3316fc6SZhikuiRen                     uint64_t thisBootSkip =
2869a3316fc6SZhikuiRen                         std::max(skip, entryCount) - entryCount;
2870a3316fc6SZhikuiRen                     uint64_t thisBootTop =
2871a3316fc6SZhikuiRen                         std::min(top + skip, endCount) - entryCount;
2872a3316fc6SZhikuiRen 
2873a3316fc6SZhikuiRen                     fillPostCodeEntry(aResp, postcode, bootIndex, 0,
2874a3316fc6SZhikuiRen                                       thisBootSkip, thisBootTop);
2875a3316fc6SZhikuiRen                 }
2876a3316fc6SZhikuiRen                 aResp->res.jsonValue["Members@odata.count"] = endCount;
2877a3316fc6SZhikuiRen             }
2878a3316fc6SZhikuiRen 
2879a3316fc6SZhikuiRen             // continue to previous bootIndex
2880a3316fc6SZhikuiRen             if (bootIndex < bootCount)
2881a3316fc6SZhikuiRen             {
2882a3316fc6SZhikuiRen                 getPostCodeForBoot(aResp, static_cast<uint16_t>(bootIndex + 1),
2883a3316fc6SZhikuiRen                                    bootCount, endCount, skip, top);
2884a3316fc6SZhikuiRen             }
2885a3316fc6SZhikuiRen             else
2886a3316fc6SZhikuiRen             {
2887a3316fc6SZhikuiRen                 aResp->res.jsonValue["Members@odata.nextLink"] =
2888a3316fc6SZhikuiRen                     "/redfish/v1/Systems/system/LogServices/PostCodes/"
2889a3316fc6SZhikuiRen                     "Entries?$skip=" +
2890a3316fc6SZhikuiRen                     std::to_string(skip + top);
2891a3316fc6SZhikuiRen             }
2892a3316fc6SZhikuiRen         },
2893a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode",
2894a3316fc6SZhikuiRen         "/xyz/openbmc_project/State/Boot/PostCode",
2895a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
2896a3316fc6SZhikuiRen         bootIndex);
2897a3316fc6SZhikuiRen }
2898a3316fc6SZhikuiRen 
2899a3316fc6SZhikuiRen static void getCurrentBootNumber(std::shared_ptr<AsyncResp> aResp,
2900a3316fc6SZhikuiRen                                  const uint64_t skip, const uint64_t top)
2901a3316fc6SZhikuiRen {
2902a3316fc6SZhikuiRen     uint64_t entryCount = 0;
2903a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
2904a3316fc6SZhikuiRen         [aResp, entryCount, skip,
2905a3316fc6SZhikuiRen          top](const boost::system::error_code ec,
2906a3316fc6SZhikuiRen               const std::variant<uint16_t>& bootCount) {
2907a3316fc6SZhikuiRen             if (ec)
2908a3316fc6SZhikuiRen             {
2909a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
2910a3316fc6SZhikuiRen                 messages::internalError(aResp->res);
2911a3316fc6SZhikuiRen                 return;
2912a3316fc6SZhikuiRen             }
2913a3316fc6SZhikuiRen             auto pVal = std::get_if<uint16_t>(&bootCount);
2914a3316fc6SZhikuiRen             if (pVal)
2915a3316fc6SZhikuiRen             {
2916a3316fc6SZhikuiRen                 getPostCodeForBoot(aResp, 1, *pVal, entryCount, skip, top);
2917a3316fc6SZhikuiRen             }
2918a3316fc6SZhikuiRen             else
2919a3316fc6SZhikuiRen             {
2920a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "Post code boot index failed.";
2921a3316fc6SZhikuiRen             }
2922a3316fc6SZhikuiRen         },
2923a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode",
2924a3316fc6SZhikuiRen         "/xyz/openbmc_project/State/Boot/PostCode",
2925a3316fc6SZhikuiRen         "org.freedesktop.DBus.Properties", "Get",
2926a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount");
2927a3316fc6SZhikuiRen }
2928a3316fc6SZhikuiRen 
2929a3316fc6SZhikuiRen class PostCodesEntryCollection : public Node
2930a3316fc6SZhikuiRen {
2931a3316fc6SZhikuiRen   public:
2932a3316fc6SZhikuiRen     template <typename CrowApp>
2933a3316fc6SZhikuiRen     PostCodesEntryCollection(CrowApp& app) :
2934a3316fc6SZhikuiRen         Node(app, "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/")
2935a3316fc6SZhikuiRen     {
2936a3316fc6SZhikuiRen         entityPrivileges = {
2937a3316fc6SZhikuiRen             {boost::beast::http::verb::get, {{"Login"}}},
2938a3316fc6SZhikuiRen             {boost::beast::http::verb::head, {{"Login"}}},
2939a3316fc6SZhikuiRen             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2940a3316fc6SZhikuiRen             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2941a3316fc6SZhikuiRen             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2942a3316fc6SZhikuiRen             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
2943a3316fc6SZhikuiRen     }
2944a3316fc6SZhikuiRen 
2945a3316fc6SZhikuiRen   private:
2946a3316fc6SZhikuiRen     void doGet(crow::Response& res, const crow::Request& req,
2947a3316fc6SZhikuiRen                const std::vector<std::string>& params) override
2948a3316fc6SZhikuiRen     {
2949a3316fc6SZhikuiRen         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
2950a3316fc6SZhikuiRen 
2951a3316fc6SZhikuiRen         asyncResp->res.jsonValue["@odata.type"] =
2952a3316fc6SZhikuiRen             "#LogEntryCollection.LogEntryCollection";
2953a3316fc6SZhikuiRen         asyncResp->res.jsonValue["@odata.context"] =
2954a3316fc6SZhikuiRen             "/redfish/v1/"
2955a3316fc6SZhikuiRen             "$metadata#LogEntryCollection.LogEntryCollection";
2956a3316fc6SZhikuiRen         asyncResp->res.jsonValue["@odata.id"] =
2957a3316fc6SZhikuiRen             "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
2958a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
2959a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Description"] =
2960a3316fc6SZhikuiRen             "Collection of POST Code Log Entries";
2961a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
2962a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Members@odata.count"] = 0;
2963a3316fc6SZhikuiRen 
2964a3316fc6SZhikuiRen         uint64_t skip = 0;
2965a3316fc6SZhikuiRen         uint64_t top = maxEntriesPerPage; // Show max entries by default
2966a3316fc6SZhikuiRen         if (!getSkipParam(asyncResp->res, req, skip))
2967a3316fc6SZhikuiRen         {
2968a3316fc6SZhikuiRen             return;
2969a3316fc6SZhikuiRen         }
2970a3316fc6SZhikuiRen         if (!getTopParam(asyncResp->res, req, top))
2971a3316fc6SZhikuiRen         {
2972a3316fc6SZhikuiRen             return;
2973a3316fc6SZhikuiRen         }
2974a3316fc6SZhikuiRen         getCurrentBootNumber(asyncResp, skip, top);
2975a3316fc6SZhikuiRen     }
2976a3316fc6SZhikuiRen };
2977a3316fc6SZhikuiRen 
2978a3316fc6SZhikuiRen class PostCodesEntry : public Node
2979a3316fc6SZhikuiRen {
2980a3316fc6SZhikuiRen   public:
2981a3316fc6SZhikuiRen     PostCodesEntry(CrowApp& app) :
2982a3316fc6SZhikuiRen         Node(app,
2983a3316fc6SZhikuiRen              "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/",
2984a3316fc6SZhikuiRen              std::string())
2985a3316fc6SZhikuiRen     {
2986a3316fc6SZhikuiRen         entityPrivileges = {
2987a3316fc6SZhikuiRen             {boost::beast::http::verb::get, {{"Login"}}},
2988a3316fc6SZhikuiRen             {boost::beast::http::verb::head, {{"Login"}}},
2989a3316fc6SZhikuiRen             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2990a3316fc6SZhikuiRen             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2991a3316fc6SZhikuiRen             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2992a3316fc6SZhikuiRen             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
2993a3316fc6SZhikuiRen     }
2994a3316fc6SZhikuiRen 
2995a3316fc6SZhikuiRen   private:
2996a3316fc6SZhikuiRen     void doGet(crow::Response& res, const crow::Request& req,
2997a3316fc6SZhikuiRen                const std::vector<std::string>& params) override
2998a3316fc6SZhikuiRen     {
2999a3316fc6SZhikuiRen         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
3000a3316fc6SZhikuiRen         if (params.size() != 1)
3001a3316fc6SZhikuiRen         {
3002a3316fc6SZhikuiRen             messages::internalError(asyncResp->res);
3003a3316fc6SZhikuiRen             return;
3004a3316fc6SZhikuiRen         }
3005a3316fc6SZhikuiRen 
3006a3316fc6SZhikuiRen         const std::string& targetID = params[0];
3007a3316fc6SZhikuiRen 
3008a3316fc6SZhikuiRen         size_t bootPos = targetID.find('B');
3009a3316fc6SZhikuiRen         if (bootPos == std::string::npos)
3010a3316fc6SZhikuiRen         {
3011a3316fc6SZhikuiRen             // Requested ID was not found
3012a3316fc6SZhikuiRen             messages::resourceMissingAtURI(asyncResp->res, targetID);
3013a3316fc6SZhikuiRen             return;
3014a3316fc6SZhikuiRen         }
3015a3316fc6SZhikuiRen         std::string_view bootIndexStr(targetID);
3016a3316fc6SZhikuiRen         bootIndexStr.remove_prefix(bootPos + 1);
3017a3316fc6SZhikuiRen         uint16_t bootIndex = 0;
3018a3316fc6SZhikuiRen         uint64_t codeIndex = 0;
3019a3316fc6SZhikuiRen         size_t dashPos = bootIndexStr.find('-');
3020a3316fc6SZhikuiRen 
3021a3316fc6SZhikuiRen         if (dashPos == std::string::npos)
3022a3316fc6SZhikuiRen         {
3023a3316fc6SZhikuiRen             return;
3024a3316fc6SZhikuiRen         }
3025a3316fc6SZhikuiRen         std::string_view codeIndexStr(bootIndexStr);
3026a3316fc6SZhikuiRen         bootIndexStr.remove_suffix(dashPos);
3027a3316fc6SZhikuiRen         codeIndexStr.remove_prefix(dashPos + 1);
3028a3316fc6SZhikuiRen 
3029a3316fc6SZhikuiRen         bootIndex = static_cast<uint16_t>(
3030a3316fc6SZhikuiRen             strtoul(std::string(bootIndexStr).c_str(), NULL, 0));
3031a3316fc6SZhikuiRen         codeIndex = strtoul(std::string(codeIndexStr).c_str(), NULL, 0);
3032a3316fc6SZhikuiRen         if (bootIndex == 0 || codeIndex == 0)
3033a3316fc6SZhikuiRen         {
3034a3316fc6SZhikuiRen             BMCWEB_LOG_DEBUG << "Get Post Code invalid entry string "
3035a3316fc6SZhikuiRen                              << params[0];
3036a3316fc6SZhikuiRen         }
3037a3316fc6SZhikuiRen 
3038a3316fc6SZhikuiRen         asyncResp->res.jsonValue["@odata.type"] = "#LogEntry.v1_4_0.LogEntry";
3039a3316fc6SZhikuiRen         asyncResp->res.jsonValue["@odata.context"] =
3040a3316fc6SZhikuiRen             "/redfish/v1/$metadata#LogEntry.LogEntry";
3041a3316fc6SZhikuiRen         asyncResp->res.jsonValue["@odata.id"] =
3042a3316fc6SZhikuiRen             "/redfish/v1/Systems/system/LogServices/PostCodes/"
3043a3316fc6SZhikuiRen             "Entries";
3044a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3045a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Description"] =
3046a3316fc6SZhikuiRen             "Collection of POST Code Log Entries";
3047a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3048a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Members@odata.count"] = 0;
3049a3316fc6SZhikuiRen 
3050a3316fc6SZhikuiRen         getPostCodeForEntry(asyncResp, bootIndex, codeIndex);
3051a3316fc6SZhikuiRen     }
3052a3316fc6SZhikuiRen };
3053a3316fc6SZhikuiRen 
30541da66f75SEd Tanous } // namespace redfish
3055