xref: /openbmc/bmcweb/features/redfish/lib/log_services.hpp (revision 5eb468da982d6b02cafc6d3b7e4413d01b28be80)
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 
183ccb3adbSEd Tanous #include "app.hpp"
197a1dbc48SGeorge Liu #include "dbus_utility.hpp"
203ccb3adbSEd Tanous #include "error_messages.hpp"
2168dd075aSAsmitha Karunanithi #include "generated/enums/log_entry.hpp"
22b7028ebfSSpencer Ku #include "gzfile.hpp"
23647b3cdcSGeorge Liu #include "http_utility.hpp"
24b7028ebfSSpencer Ku #include "human_sort.hpp"
253ccb3adbSEd Tanous #include "query.hpp"
264851d45dSJason M. Bills #include "registries.hpp"
274851d45dSJason M. Bills #include "registries/base_message_registry.hpp"
284851d45dSJason M. Bills #include "registries/openbmc_message_registry.hpp"
293ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
3046229577SJames Feist #include "task.hpp"
313ccb3adbSEd Tanous #include "utils/dbus_utils.hpp"
323ccb3adbSEd Tanous #include "utils/time_utils.hpp"
331da66f75SEd Tanous 
34e1f26343SJason M. Bills #include <systemd/sd-journal.h>
358e31778eSAsmitha Karunanithi #include <tinyxml2.h>
36400fd1fbSAdriana Kobylak #include <unistd.h>
37e1f26343SJason M. Bills 
389896eaedSEd Tanous #include <boost/algorithm/string/case_conv.hpp>
3911ba3979SEd Tanous #include <boost/algorithm/string/classification.hpp>
40400fd1fbSAdriana Kobylak #include <boost/algorithm/string/replace.hpp>
414851d45dSJason M. Bills #include <boost/algorithm/string/split.hpp>
4207c8c20dSEd Tanous #include <boost/beast/http/verb.hpp>
431da66f75SEd Tanous #include <boost/container/flat_map.hpp>
441ddcf01aSJason M. Bills #include <boost/system/linux_error.hpp>
45ef4c65b7SEd Tanous #include <boost/url/format.hpp>
46d1bde9e5SKrzysztof Grobelny #include <sdbusplus/asio/property.hpp>
47d1bde9e5SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
481214b7e7SGunnar Mills 
497a1dbc48SGeorge Liu #include <array>
50647b3cdcSGeorge Liu #include <charconv>
514418c7f0SJames Feist #include <filesystem>
5275710de2SXiaochao Ma #include <optional>
5326702d01SEd Tanous #include <span>
54cd225da8SJason M. Bills #include <string_view>
55abf2add6SEd Tanous #include <variant>
561da66f75SEd Tanous 
571da66f75SEd Tanous namespace redfish
581da66f75SEd Tanous {
591da66f75SEd Tanous 
6089492a15SPatrick Williams constexpr const char* crashdumpObject = "com.intel.crashdump";
6189492a15SPatrick Williams constexpr const char* crashdumpPath = "/com/intel/crashdump";
6289492a15SPatrick Williams constexpr const char* crashdumpInterface = "com.intel.crashdump";
6389492a15SPatrick Williams constexpr const char* deleteAllInterface =
645b61b5e8SJason M. Bills     "xyz.openbmc_project.Collection.DeleteAll";
6589492a15SPatrick Williams constexpr const char* crashdumpOnDemandInterface =
66424c4176SJason M. Bills     "com.intel.crashdump.OnDemand";
6789492a15SPatrick Williams constexpr const char* crashdumpTelemetryInterface =
686eda7685SKenny L. Ku     "com.intel.crashdump.Telemetry";
691da66f75SEd Tanous 
708e31778eSAsmitha Karunanithi enum class DumpCreationProgress
718e31778eSAsmitha Karunanithi {
728e31778eSAsmitha Karunanithi     DUMP_CREATE_SUCCESS,
738e31778eSAsmitha Karunanithi     DUMP_CREATE_FAILED,
748e31778eSAsmitha Karunanithi     DUMP_CREATE_INPROGRESS
758e31778eSAsmitha Karunanithi };
768e31778eSAsmitha Karunanithi 
77f6150403SJames Feist namespace fs = std::filesystem;
781da66f75SEd Tanous 
79cb92c03bSAndrew Geissler inline std::string translateSeverityDbusToRedfish(const std::string& s)
80cb92c03bSAndrew Geissler {
81d4d25793SEd Tanous     if ((s == "xyz.openbmc_project.Logging.Entry.Level.Alert") ||
82d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Critical") ||
83d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Emergency") ||
84d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Error"))
85cb92c03bSAndrew Geissler     {
86cb92c03bSAndrew Geissler         return "Critical";
87cb92c03bSAndrew Geissler     }
883174e4dfSEd Tanous     if ((s == "xyz.openbmc_project.Logging.Entry.Level.Debug") ||
89d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Informational") ||
90d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Notice"))
91cb92c03bSAndrew Geissler     {
92cb92c03bSAndrew Geissler         return "OK";
93cb92c03bSAndrew Geissler     }
943174e4dfSEd Tanous     if (s == "xyz.openbmc_project.Logging.Entry.Level.Warning")
95cb92c03bSAndrew Geissler     {
96cb92c03bSAndrew Geissler         return "Warning";
97cb92c03bSAndrew Geissler     }
98cb92c03bSAndrew Geissler     return "";
99cb92c03bSAndrew Geissler }
100cb92c03bSAndrew Geissler 
1019017faf2SAbhishek Patel inline std::optional<bool> getProviderNotifyAction(const std::string& notify)
1029017faf2SAbhishek Patel {
1039017faf2SAbhishek Patel     std::optional<bool> notifyAction;
1049017faf2SAbhishek Patel     if (notify == "xyz.openbmc_project.Logging.Entry.Notify.Notify")
1059017faf2SAbhishek Patel     {
1069017faf2SAbhishek Patel         notifyAction = true;
1079017faf2SAbhishek Patel     }
1089017faf2SAbhishek Patel     else if (notify == "xyz.openbmc_project.Logging.Entry.Notify.Inhibit")
1099017faf2SAbhishek Patel     {
1109017faf2SAbhishek Patel         notifyAction = false;
1119017faf2SAbhishek Patel     }
1129017faf2SAbhishek Patel 
1139017faf2SAbhishek Patel     return notifyAction;
1149017faf2SAbhishek Patel }
1159017faf2SAbhishek Patel 
1167e860f15SJohn Edward Broadbent inline static int getJournalMetadata(sd_journal* journal,
11726ccae32SEd Tanous                                      std::string_view field,
11839e77504SEd Tanous                                      std::string_view& contents)
11916428a1aSJason M. Bills {
12016428a1aSJason M. Bills     const char* data = nullptr;
12116428a1aSJason M. Bills     size_t length = 0;
12216428a1aSJason M. Bills     int ret = 0;
12316428a1aSJason M. Bills     // Get the metadata from the requested field of the journal entry
12446ff87baSEd Tanous     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
12546ff87baSEd Tanous     const void** dataVoid = reinterpret_cast<const void**>(&data);
12646ff87baSEd Tanous 
12746ff87baSEd Tanous     ret = sd_journal_get_data(journal, field.data(), dataVoid, &length);
12816428a1aSJason M. Bills     if (ret < 0)
12916428a1aSJason M. Bills     {
13016428a1aSJason M. Bills         return ret;
13116428a1aSJason M. Bills     }
13239e77504SEd Tanous     contents = std::string_view(data, length);
13316428a1aSJason M. Bills     // Only use the content after the "=" character.
13481ce609eSEd Tanous     contents.remove_prefix(std::min(contents.find('=') + 1, contents.size()));
13516428a1aSJason M. Bills     return ret;
13616428a1aSJason M. Bills }
13716428a1aSJason M. Bills 
1387e860f15SJohn Edward Broadbent inline static int getJournalMetadata(sd_journal* journal,
13926ccae32SEd Tanous                                      std::string_view field, const int& base,
14026ccae32SEd Tanous                                      long int& contents)
14116428a1aSJason M. Bills {
14216428a1aSJason M. Bills     int ret = 0;
14339e77504SEd Tanous     std::string_view metadata;
14416428a1aSJason M. Bills     // Get the metadata from the requested field of the journal entry
14516428a1aSJason M. Bills     ret = getJournalMetadata(journal, field, metadata);
14616428a1aSJason M. Bills     if (ret < 0)
14716428a1aSJason M. Bills     {
14816428a1aSJason M. Bills         return ret;
14916428a1aSJason M. Bills     }
150b01bf299SEd Tanous     contents = strtol(metadata.data(), nullptr, base);
15116428a1aSJason M. Bills     return ret;
15216428a1aSJason M. Bills }
15316428a1aSJason M. Bills 
1547e860f15SJohn Edward Broadbent inline static bool getEntryTimestamp(sd_journal* journal,
1557e860f15SJohn Edward Broadbent                                      std::string& entryTimestamp)
156a3316fc6SZhikuiRen {
157a3316fc6SZhikuiRen     int ret = 0;
158a3316fc6SZhikuiRen     uint64_t timestamp = 0;
159a3316fc6SZhikuiRen     ret = sd_journal_get_realtime_usec(journal, &timestamp);
160a3316fc6SZhikuiRen     if (ret < 0)
161a3316fc6SZhikuiRen     {
162a3316fc6SZhikuiRen         BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
163a3316fc6SZhikuiRen                          << strerror(-ret);
164a3316fc6SZhikuiRen         return false;
165a3316fc6SZhikuiRen     }
166e645c5e6SKonstantin Aladyshev     entryTimestamp = redfish::time_utils::getDateTimeUintUs(timestamp);
1679c620e21SAsmitha Karunanithi     return true;
168a3316fc6SZhikuiRen }
16950b8a43aSEd Tanous 
1707e860f15SJohn Edward Broadbent inline static bool getUniqueEntryID(sd_journal* journal, std::string& entryID,
171e85d6b16SJason M. Bills                                     const bool firstEntry = true)
17216428a1aSJason M. Bills {
17316428a1aSJason M. Bills     int ret = 0;
17416428a1aSJason M. Bills     static uint64_t prevTs = 0;
17516428a1aSJason M. Bills     static int index = 0;
176e85d6b16SJason M. Bills     if (firstEntry)
177e85d6b16SJason M. Bills     {
178e85d6b16SJason M. Bills         prevTs = 0;
179e85d6b16SJason M. Bills     }
180e85d6b16SJason M. Bills 
18116428a1aSJason M. Bills     // Get the entry timestamp
18216428a1aSJason M. Bills     uint64_t curTs = 0;
18316428a1aSJason M. Bills     ret = sd_journal_get_realtime_usec(journal, &curTs);
18416428a1aSJason M. Bills     if (ret < 0)
18516428a1aSJason M. Bills     {
18616428a1aSJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
18716428a1aSJason M. Bills                          << strerror(-ret);
18816428a1aSJason M. Bills         return false;
18916428a1aSJason M. Bills     }
19016428a1aSJason M. Bills     // If the timestamp isn't unique, increment the index
19116428a1aSJason M. Bills     if (curTs == prevTs)
19216428a1aSJason M. Bills     {
19316428a1aSJason M. Bills         index++;
19416428a1aSJason M. Bills     }
19516428a1aSJason M. Bills     else
19616428a1aSJason M. Bills     {
19716428a1aSJason M. Bills         // Otherwise, reset it
19816428a1aSJason M. Bills         index = 0;
19916428a1aSJason M. Bills     }
20016428a1aSJason M. Bills     // Save the timestamp
20116428a1aSJason M. Bills     prevTs = curTs;
20216428a1aSJason M. Bills 
20316428a1aSJason M. Bills     entryID = std::to_string(curTs);
20416428a1aSJason M. Bills     if (index > 0)
20516428a1aSJason M. Bills     {
20616428a1aSJason M. Bills         entryID += "_" + std::to_string(index);
20716428a1aSJason M. Bills     }
20816428a1aSJason M. Bills     return true;
20916428a1aSJason M. Bills }
21016428a1aSJason M. Bills 
211e85d6b16SJason M. Bills static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
212e85d6b16SJason M. Bills                              const bool firstEntry = true)
21395820184SJason M. Bills {
214271584abSEd Tanous     static time_t prevTs = 0;
21595820184SJason M. Bills     static int index = 0;
216e85d6b16SJason M. Bills     if (firstEntry)
217e85d6b16SJason M. Bills     {
218e85d6b16SJason M. Bills         prevTs = 0;
219e85d6b16SJason M. Bills     }
220e85d6b16SJason M. Bills 
22195820184SJason M. Bills     // Get the entry timestamp
222271584abSEd Tanous     std::time_t curTs = 0;
22395820184SJason M. Bills     std::tm timeStruct = {};
22495820184SJason M. Bills     std::istringstream entryStream(logEntry);
22595820184SJason M. Bills     if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
22695820184SJason M. Bills     {
22795820184SJason M. Bills         curTs = std::mktime(&timeStruct);
22895820184SJason M. Bills     }
22995820184SJason M. Bills     // If the timestamp isn't unique, increment the index
23095820184SJason M. Bills     if (curTs == prevTs)
23195820184SJason M. Bills     {
23295820184SJason M. Bills         index++;
23395820184SJason M. Bills     }
23495820184SJason M. Bills     else
23595820184SJason M. Bills     {
23695820184SJason M. Bills         // Otherwise, reset it
23795820184SJason M. Bills         index = 0;
23895820184SJason M. Bills     }
23995820184SJason M. Bills     // Save the timestamp
24095820184SJason M. Bills     prevTs = curTs;
24195820184SJason M. Bills 
24295820184SJason M. Bills     entryID = std::to_string(curTs);
24395820184SJason M. Bills     if (index > 0)
24495820184SJason M. Bills     {
24595820184SJason M. Bills         entryID += "_" + std::to_string(index);
24695820184SJason M. Bills     }
24795820184SJason M. Bills     return true;
24895820184SJason M. Bills }
24995820184SJason M. Bills 
2507e860f15SJohn Edward Broadbent inline static bool
2518d1b46d7Szhanghch05     getTimestampFromID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2528d1b46d7Szhanghch05                        const std::string& entryID, uint64_t& timestamp,
2538d1b46d7Szhanghch05                        uint64_t& index)
25416428a1aSJason M. Bills {
25516428a1aSJason M. Bills     if (entryID.empty())
25616428a1aSJason M. Bills     {
25716428a1aSJason M. Bills         return false;
25816428a1aSJason M. Bills     }
25916428a1aSJason M. Bills     // Convert the unique ID back to a timestamp to find the entry
26039e77504SEd Tanous     std::string_view tsStr(entryID);
26116428a1aSJason M. Bills 
26281ce609eSEd Tanous     auto underscorePos = tsStr.find('_');
26371d5d8dbSEd Tanous     if (underscorePos != std::string_view::npos)
26416428a1aSJason M. Bills     {
26516428a1aSJason M. Bills         // Timestamp has an index
26616428a1aSJason M. Bills         tsStr.remove_suffix(tsStr.size() - underscorePos);
26739e77504SEd Tanous         std::string_view indexStr(entryID);
26816428a1aSJason M. Bills         indexStr.remove_prefix(underscorePos + 1);
26984396af9SPatrick Williams         auto [ptr, ec] = std::from_chars(indexStr.begin(), indexStr.end(),
27084396af9SPatrick Williams                                          index);
271c0bd5e4bSEd Tanous         if (ec != std::errc())
27216428a1aSJason M. Bills         {
2739db4ba25SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
27416428a1aSJason M. Bills             return false;
27516428a1aSJason M. Bills         }
27616428a1aSJason M. Bills     }
27716428a1aSJason M. Bills     // Timestamp has no index
27884396af9SPatrick Williams     auto [ptr, ec] = std::from_chars(tsStr.begin(), tsStr.end(), timestamp);
279c0bd5e4bSEd Tanous     if (ec != std::errc())
28016428a1aSJason M. Bills     {
2819db4ba25SJiaqing Zhao         messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
28216428a1aSJason M. Bills         return false;
28316428a1aSJason M. Bills     }
28416428a1aSJason M. Bills     return true;
28516428a1aSJason M. Bills }
28616428a1aSJason M. Bills 
28795820184SJason M. Bills static bool
28895820184SJason M. Bills     getRedfishLogFiles(std::vector<std::filesystem::path>& redfishLogFiles)
28995820184SJason M. Bills {
29095820184SJason M. Bills     static const std::filesystem::path redfishLogDir = "/var/log";
29195820184SJason M. Bills     static const std::string redfishLogFilename = "redfish";
29295820184SJason M. Bills 
29395820184SJason M. Bills     // Loop through the directory looking for redfish log files
29495820184SJason M. Bills     for (const std::filesystem::directory_entry& dirEnt :
29595820184SJason M. Bills          std::filesystem::directory_iterator(redfishLogDir))
29695820184SJason M. Bills     {
29795820184SJason M. Bills         // If we find a redfish log file, save the path
29895820184SJason M. Bills         std::string filename = dirEnt.path().filename();
29911ba3979SEd Tanous         if (filename.starts_with(redfishLogFilename))
30095820184SJason M. Bills         {
30195820184SJason M. Bills             redfishLogFiles.emplace_back(redfishLogDir / filename);
30295820184SJason M. Bills         }
30395820184SJason M. Bills     }
30495820184SJason M. Bills     // As the log files rotate, they are appended with a ".#" that is higher for
30595820184SJason M. Bills     // the older logs. Since we don't expect more than 10 log files, we
30695820184SJason M. Bills     // can just sort the list to get them in order from newest to oldest
30795820184SJason M. Bills     std::sort(redfishLogFiles.begin(), redfishLogFiles.end());
30895820184SJason M. Bills 
30995820184SJason M. Bills     return !redfishLogFiles.empty();
31095820184SJason M. Bills }
31195820184SJason M. Bills 
31268dd075aSAsmitha Karunanithi inline log_entry::OriginatorTypes
31368dd075aSAsmitha Karunanithi     mapDbusOriginatorTypeToRedfish(const std::string& originatorType)
31468dd075aSAsmitha Karunanithi {
31568dd075aSAsmitha Karunanithi     if (originatorType ==
31668dd075aSAsmitha Karunanithi         "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.Client")
31768dd075aSAsmitha Karunanithi     {
31868dd075aSAsmitha Karunanithi         return log_entry::OriginatorTypes::Client;
31968dd075aSAsmitha Karunanithi     }
32068dd075aSAsmitha Karunanithi     if (originatorType ==
32168dd075aSAsmitha Karunanithi         "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.Internal")
32268dd075aSAsmitha Karunanithi     {
32368dd075aSAsmitha Karunanithi         return log_entry::OriginatorTypes::Internal;
32468dd075aSAsmitha Karunanithi     }
32568dd075aSAsmitha Karunanithi     if (originatorType ==
32668dd075aSAsmitha Karunanithi         "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.SupportingService")
32768dd075aSAsmitha Karunanithi     {
32868dd075aSAsmitha Karunanithi         return log_entry::OriginatorTypes::SupportingService;
32968dd075aSAsmitha Karunanithi     }
33068dd075aSAsmitha Karunanithi     return log_entry::OriginatorTypes::Invalid;
33168dd075aSAsmitha Karunanithi }
33268dd075aSAsmitha Karunanithi 
333aefe3786SClaire Weinan inline void parseDumpEntryFromDbusObject(
3342d613eb6SJiaqing Zhao     const dbus::utility::ManagedObjectType::value_type& object,
335c6fecdabSClaire Weinan     std::string& dumpStatus, uint64_t& size, uint64_t& timestampUs,
33668dd075aSAsmitha Karunanithi     std::string& originatorId, log_entry::OriginatorTypes& originatorType,
337aefe3786SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
338aefe3786SClaire Weinan {
339aefe3786SClaire Weinan     for (const auto& interfaceMap : object.second)
340aefe3786SClaire Weinan     {
341aefe3786SClaire Weinan         if (interfaceMap.first == "xyz.openbmc_project.Common.Progress")
342aefe3786SClaire Weinan         {
343aefe3786SClaire Weinan             for (const auto& propertyMap : interfaceMap.second)
344aefe3786SClaire Weinan             {
345aefe3786SClaire Weinan                 if (propertyMap.first == "Status")
346aefe3786SClaire Weinan                 {
347aefe3786SClaire Weinan                     const auto* status =
348aefe3786SClaire Weinan                         std::get_if<std::string>(&propertyMap.second);
349aefe3786SClaire Weinan                     if (status == nullptr)
350aefe3786SClaire Weinan                     {
351aefe3786SClaire Weinan                         messages::internalError(asyncResp->res);
352aefe3786SClaire Weinan                         break;
353aefe3786SClaire Weinan                     }
354aefe3786SClaire Weinan                     dumpStatus = *status;
355aefe3786SClaire Weinan                 }
356aefe3786SClaire Weinan             }
357aefe3786SClaire Weinan         }
358aefe3786SClaire Weinan         else if (interfaceMap.first == "xyz.openbmc_project.Dump.Entry")
359aefe3786SClaire Weinan         {
360aefe3786SClaire Weinan             for (const auto& propertyMap : interfaceMap.second)
361aefe3786SClaire Weinan             {
362aefe3786SClaire Weinan                 if (propertyMap.first == "Size")
363aefe3786SClaire Weinan                 {
364aefe3786SClaire Weinan                     const auto* sizePtr =
365aefe3786SClaire Weinan                         std::get_if<uint64_t>(&propertyMap.second);
366aefe3786SClaire Weinan                     if (sizePtr == nullptr)
367aefe3786SClaire Weinan                     {
368aefe3786SClaire Weinan                         messages::internalError(asyncResp->res);
369aefe3786SClaire Weinan                         break;
370aefe3786SClaire Weinan                     }
371aefe3786SClaire Weinan                     size = *sizePtr;
372aefe3786SClaire Weinan                     break;
373aefe3786SClaire Weinan                 }
374aefe3786SClaire Weinan             }
375aefe3786SClaire Weinan         }
376aefe3786SClaire Weinan         else if (interfaceMap.first == "xyz.openbmc_project.Time.EpochTime")
377aefe3786SClaire Weinan         {
378aefe3786SClaire Weinan             for (const auto& propertyMap : interfaceMap.second)
379aefe3786SClaire Weinan             {
380aefe3786SClaire Weinan                 if (propertyMap.first == "Elapsed")
381aefe3786SClaire Weinan                 {
382aefe3786SClaire Weinan                     const uint64_t* usecsTimeStamp =
383aefe3786SClaire Weinan                         std::get_if<uint64_t>(&propertyMap.second);
384aefe3786SClaire Weinan                     if (usecsTimeStamp == nullptr)
385aefe3786SClaire Weinan                     {
386aefe3786SClaire Weinan                         messages::internalError(asyncResp->res);
387aefe3786SClaire Weinan                         break;
388aefe3786SClaire Weinan                     }
389c6fecdabSClaire Weinan                     timestampUs = *usecsTimeStamp;
390aefe3786SClaire Weinan                     break;
391aefe3786SClaire Weinan                 }
392aefe3786SClaire Weinan             }
393aefe3786SClaire Weinan         }
39468dd075aSAsmitha Karunanithi         else if (interfaceMap.first ==
39568dd075aSAsmitha Karunanithi                  "xyz.openbmc_project.Common.OriginatedBy")
39668dd075aSAsmitha Karunanithi         {
39768dd075aSAsmitha Karunanithi             for (const auto& propertyMap : interfaceMap.second)
39868dd075aSAsmitha Karunanithi             {
39968dd075aSAsmitha Karunanithi                 if (propertyMap.first == "OriginatorId")
40068dd075aSAsmitha Karunanithi                 {
40168dd075aSAsmitha Karunanithi                     const std::string* id =
40268dd075aSAsmitha Karunanithi                         std::get_if<std::string>(&propertyMap.second);
40368dd075aSAsmitha Karunanithi                     if (id == nullptr)
40468dd075aSAsmitha Karunanithi                     {
40568dd075aSAsmitha Karunanithi                         messages::internalError(asyncResp->res);
40668dd075aSAsmitha Karunanithi                         break;
40768dd075aSAsmitha Karunanithi                     }
40868dd075aSAsmitha Karunanithi                     originatorId = *id;
40968dd075aSAsmitha Karunanithi                 }
41068dd075aSAsmitha Karunanithi 
41168dd075aSAsmitha Karunanithi                 if (propertyMap.first == "OriginatorType")
41268dd075aSAsmitha Karunanithi                 {
41368dd075aSAsmitha Karunanithi                     const std::string* type =
41468dd075aSAsmitha Karunanithi                         std::get_if<std::string>(&propertyMap.second);
41568dd075aSAsmitha Karunanithi                     if (type == nullptr)
41668dd075aSAsmitha Karunanithi                     {
41768dd075aSAsmitha Karunanithi                         messages::internalError(asyncResp->res);
41868dd075aSAsmitha Karunanithi                         break;
41968dd075aSAsmitha Karunanithi                     }
42068dd075aSAsmitha Karunanithi 
42168dd075aSAsmitha Karunanithi                     originatorType = mapDbusOriginatorTypeToRedfish(*type);
42268dd075aSAsmitha Karunanithi                     if (originatorType == log_entry::OriginatorTypes::Invalid)
42368dd075aSAsmitha Karunanithi                     {
42468dd075aSAsmitha Karunanithi                         messages::internalError(asyncResp->res);
42568dd075aSAsmitha Karunanithi                         break;
42668dd075aSAsmitha Karunanithi                     }
42768dd075aSAsmitha Karunanithi                 }
42868dd075aSAsmitha Karunanithi             }
42968dd075aSAsmitha Karunanithi         }
430aefe3786SClaire Weinan     }
431aefe3786SClaire Weinan }
432aefe3786SClaire Weinan 
43321ab404cSNan Zhou static std::string getDumpEntriesPath(const std::string& dumpType)
434fdd26906SClaire Weinan {
435fdd26906SClaire Weinan     std::string entriesPath;
436fdd26906SClaire Weinan 
437fdd26906SClaire Weinan     if (dumpType == "BMC")
438fdd26906SClaire Weinan     {
439fdd26906SClaire Weinan         entriesPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
440fdd26906SClaire Weinan     }
441fdd26906SClaire Weinan     else if (dumpType == "FaultLog")
442fdd26906SClaire Weinan     {
443fdd26906SClaire Weinan         entriesPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/";
444fdd26906SClaire Weinan     }
445fdd26906SClaire Weinan     else if (dumpType == "System")
446fdd26906SClaire Weinan     {
447fdd26906SClaire Weinan         entriesPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
448fdd26906SClaire Weinan     }
449fdd26906SClaire Weinan     else
450fdd26906SClaire Weinan     {
451fdd26906SClaire Weinan         BMCWEB_LOG_ERROR << "getDumpEntriesPath() invalid dump type: "
452fdd26906SClaire Weinan                          << dumpType;
453fdd26906SClaire Weinan     }
454fdd26906SClaire Weinan 
455fdd26906SClaire Weinan     // Returns empty string on error
456fdd26906SClaire Weinan     return entriesPath;
457fdd26906SClaire Weinan }
458fdd26906SClaire Weinan 
4598d1b46d7Szhanghch05 inline void
4608d1b46d7Szhanghch05     getDumpEntryCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4615cb1dd27SAsmitha Karunanithi                            const std::string& dumpType)
4625cb1dd27SAsmitha Karunanithi {
463fdd26906SClaire Weinan     std::string entriesPath = getDumpEntriesPath(dumpType);
464fdd26906SClaire Weinan     if (entriesPath.empty())
4655cb1dd27SAsmitha Karunanithi     {
4665cb1dd27SAsmitha Karunanithi         messages::internalError(asyncResp->res);
4675cb1dd27SAsmitha Karunanithi         return;
4685cb1dd27SAsmitha Karunanithi     }
4695cb1dd27SAsmitha Karunanithi 
470*5eb468daSGeorge Liu     sdbusplus::message::object_path path("/xyz/openbmc_project/dump");
471*5eb468daSGeorge Liu     dbus::utility::getManagedObjects(
472*5eb468daSGeorge Liu         "xyz.openbmc_project.Dump.Manager", path,
473fdd26906SClaire Weinan         [asyncResp, entriesPath,
4745e7e2dc5SEd Tanous          dumpType](const boost::system::error_code& ec,
475*5eb468daSGeorge Liu                    const dbus::utility::ManagedObjectType& objects) {
4765cb1dd27SAsmitha Karunanithi         if (ec)
4775cb1dd27SAsmitha Karunanithi         {
4785cb1dd27SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
4795cb1dd27SAsmitha Karunanithi             messages::internalError(asyncResp->res);
4805cb1dd27SAsmitha Karunanithi             return;
4815cb1dd27SAsmitha Karunanithi         }
4825cb1dd27SAsmitha Karunanithi 
483fdd26906SClaire Weinan         // Remove ending slash
484fdd26906SClaire Weinan         std::string odataIdStr = entriesPath;
485fdd26906SClaire Weinan         if (!odataIdStr.empty())
486fdd26906SClaire Weinan         {
487fdd26906SClaire Weinan             odataIdStr.pop_back();
488fdd26906SClaire Weinan         }
489fdd26906SClaire Weinan 
490fdd26906SClaire Weinan         asyncResp->res.jsonValue["@odata.type"] =
491fdd26906SClaire Weinan             "#LogEntryCollection.LogEntryCollection";
492fdd26906SClaire Weinan         asyncResp->res.jsonValue["@odata.id"] = std::move(odataIdStr);
493fdd26906SClaire Weinan         asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entries";
49489492a15SPatrick Williams         asyncResp->res.jsonValue["Description"] = "Collection of " + dumpType +
49589492a15SPatrick Williams                                                   " Dump Entries";
496fdd26906SClaire Weinan 
4975cb1dd27SAsmitha Karunanithi         nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
4985cb1dd27SAsmitha Karunanithi         entriesArray = nlohmann::json::array();
499b47452b2SAsmitha Karunanithi         std::string dumpEntryPath =
500b47452b2SAsmitha Karunanithi             "/xyz/openbmc_project/dump/" +
501002d39b4SEd Tanous             std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
5025cb1dd27SAsmitha Karunanithi 
503*5eb468daSGeorge Liu         dbus::utility::ManagedObjectType resp(objects);
504002d39b4SEd Tanous         std::sort(resp.begin(), resp.end(), [](const auto& l, const auto& r) {
505002d39b4SEd Tanous             return AlphanumLess<std::string>()(l.first.filename(),
506002d39b4SEd Tanous                                                r.first.filename());
507565dfb6fSClaire Weinan         });
508565dfb6fSClaire Weinan 
5095cb1dd27SAsmitha Karunanithi         for (auto& object : resp)
5105cb1dd27SAsmitha Karunanithi         {
511b47452b2SAsmitha Karunanithi             if (object.first.str.find(dumpEntryPath) == std::string::npos)
5125cb1dd27SAsmitha Karunanithi             {
5135cb1dd27SAsmitha Karunanithi                 continue;
5145cb1dd27SAsmitha Karunanithi             }
515c6fecdabSClaire Weinan             uint64_t timestampUs = 0;
5165cb1dd27SAsmitha Karunanithi             uint64_t size = 0;
51735440d18SAsmitha Karunanithi             std::string dumpStatus;
51868dd075aSAsmitha Karunanithi             std::string originatorId;
51968dd075aSAsmitha Karunanithi             log_entry::OriginatorTypes originatorType =
52068dd075aSAsmitha Karunanithi                 log_entry::OriginatorTypes::Internal;
521433b68b4SJason M. Bills             nlohmann::json::object_t thisEntry;
5222dfd18efSEd Tanous 
5232dfd18efSEd Tanous             std::string entryID = object.first.filename();
5242dfd18efSEd Tanous             if (entryID.empty())
5255cb1dd27SAsmitha Karunanithi             {
5265cb1dd27SAsmitha Karunanithi                 continue;
5275cb1dd27SAsmitha Karunanithi             }
5285cb1dd27SAsmitha Karunanithi 
529c6fecdabSClaire Weinan             parseDumpEntryFromDbusObject(object, dumpStatus, size, timestampUs,
53068dd075aSAsmitha Karunanithi                                          originatorId, originatorType,
531aefe3786SClaire Weinan                                          asyncResp);
5325cb1dd27SAsmitha Karunanithi 
5330fda0f12SGeorge Liu             if (dumpStatus !=
5340fda0f12SGeorge Liu                     "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
53535440d18SAsmitha Karunanithi                 !dumpStatus.empty())
53635440d18SAsmitha Karunanithi             {
53735440d18SAsmitha Karunanithi                 // Dump status is not Complete, no need to enumerate
53835440d18SAsmitha Karunanithi                 continue;
53935440d18SAsmitha Karunanithi             }
54035440d18SAsmitha Karunanithi 
54168dd075aSAsmitha Karunanithi             thisEntry["@odata.type"] = "#LogEntry.v1_11_0.LogEntry";
542fdd26906SClaire Weinan             thisEntry["@odata.id"] = entriesPath + entryID;
5435cb1dd27SAsmitha Karunanithi             thisEntry["Id"] = entryID;
5445cb1dd27SAsmitha Karunanithi             thisEntry["EntryType"] = "Event";
5455cb1dd27SAsmitha Karunanithi             thisEntry["Name"] = dumpType + " Dump Entry";
546bbd80db8SClaire Weinan             thisEntry["Created"] =
547bbd80db8SClaire Weinan                 redfish::time_utils::getDateTimeUintUs(timestampUs);
5485cb1dd27SAsmitha Karunanithi 
54968dd075aSAsmitha Karunanithi             if (!originatorId.empty())
55068dd075aSAsmitha Karunanithi             {
55168dd075aSAsmitha Karunanithi                 thisEntry["Originator"] = originatorId;
55268dd075aSAsmitha Karunanithi                 thisEntry["OriginatorType"] = originatorType;
55368dd075aSAsmitha Karunanithi             }
55468dd075aSAsmitha Karunanithi 
5555cb1dd27SAsmitha Karunanithi             if (dumpType == "BMC")
5565cb1dd27SAsmitha Karunanithi             {
557d337bb72SAsmitha Karunanithi                 thisEntry["DiagnosticDataType"] = "Manager";
55889492a15SPatrick Williams                 thisEntry["AdditionalDataURI"] = entriesPath + entryID +
55989492a15SPatrick Williams                                                  "/attachment";
560fdd26906SClaire Weinan                 thisEntry["AdditionalDataSizeBytes"] = size;
5615cb1dd27SAsmitha Karunanithi             }
5625cb1dd27SAsmitha Karunanithi             else if (dumpType == "System")
5635cb1dd27SAsmitha Karunanithi             {
564d337bb72SAsmitha Karunanithi                 thisEntry["DiagnosticDataType"] = "OEM";
565d337bb72SAsmitha Karunanithi                 thisEntry["OEMDiagnosticDataType"] = "System";
56689492a15SPatrick Williams                 thisEntry["AdditionalDataURI"] = entriesPath + entryID +
56789492a15SPatrick Williams                                                  "/attachment";
568fdd26906SClaire Weinan                 thisEntry["AdditionalDataSizeBytes"] = size;
5695cb1dd27SAsmitha Karunanithi             }
570b2ba3072SPatrick Williams             entriesArray.emplace_back(std::move(thisEntry));
5715cb1dd27SAsmitha Karunanithi         }
572002d39b4SEd Tanous         asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
573*5eb468daSGeorge Liu         });
5745cb1dd27SAsmitha Karunanithi }
5755cb1dd27SAsmitha Karunanithi 
5768d1b46d7Szhanghch05 inline void
577c7a6d660SClaire Weinan     getDumpEntryById(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
5788d1b46d7Szhanghch05                      const std::string& entryID, const std::string& dumpType)
5795cb1dd27SAsmitha Karunanithi {
580fdd26906SClaire Weinan     std::string entriesPath = getDumpEntriesPath(dumpType);
581fdd26906SClaire Weinan     if (entriesPath.empty())
5825cb1dd27SAsmitha Karunanithi     {
5835cb1dd27SAsmitha Karunanithi         messages::internalError(asyncResp->res);
5845cb1dd27SAsmitha Karunanithi         return;
5855cb1dd27SAsmitha Karunanithi     }
5865cb1dd27SAsmitha Karunanithi 
587*5eb468daSGeorge Liu     sdbusplus::message::object_path path("/xyz/openbmc_project/dump");
588*5eb468daSGeorge Liu     dbus::utility::getManagedObjects(
589*5eb468daSGeorge Liu         "xyz.openbmc_project.Dump.Manager", path,
590fdd26906SClaire Weinan         [asyncResp, entryID, dumpType,
5915e7e2dc5SEd Tanous          entriesPath](const boost::system::error_code& ec,
59202cad96eSEd Tanous                       const dbus::utility::ManagedObjectType& resp) {
5935cb1dd27SAsmitha Karunanithi         if (ec)
5945cb1dd27SAsmitha Karunanithi         {
5955cb1dd27SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
5965cb1dd27SAsmitha Karunanithi             messages::internalError(asyncResp->res);
5975cb1dd27SAsmitha Karunanithi             return;
5985cb1dd27SAsmitha Karunanithi         }
5995cb1dd27SAsmitha Karunanithi 
600b47452b2SAsmitha Karunanithi         bool foundDumpEntry = false;
601b47452b2SAsmitha Karunanithi         std::string dumpEntryPath =
602b47452b2SAsmitha Karunanithi             "/xyz/openbmc_project/dump/" +
603002d39b4SEd Tanous             std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
604b47452b2SAsmitha Karunanithi 
6059eb808c1SEd Tanous         for (const auto& objectPath : resp)
6065cb1dd27SAsmitha Karunanithi         {
607b47452b2SAsmitha Karunanithi             if (objectPath.first.str != dumpEntryPath + entryID)
6085cb1dd27SAsmitha Karunanithi             {
6095cb1dd27SAsmitha Karunanithi                 continue;
6105cb1dd27SAsmitha Karunanithi             }
6115cb1dd27SAsmitha Karunanithi 
6125cb1dd27SAsmitha Karunanithi             foundDumpEntry = true;
613c6fecdabSClaire Weinan             uint64_t timestampUs = 0;
6145cb1dd27SAsmitha Karunanithi             uint64_t size = 0;
61535440d18SAsmitha Karunanithi             std::string dumpStatus;
61668dd075aSAsmitha Karunanithi             std::string originatorId;
61768dd075aSAsmitha Karunanithi             log_entry::OriginatorTypes originatorType =
61868dd075aSAsmitha Karunanithi                 log_entry::OriginatorTypes::Internal;
6195cb1dd27SAsmitha Karunanithi 
620aefe3786SClaire Weinan             parseDumpEntryFromDbusObject(objectPath, dumpStatus, size,
62168dd075aSAsmitha Karunanithi                                          timestampUs, originatorId,
62268dd075aSAsmitha Karunanithi                                          originatorType, asyncResp);
6235cb1dd27SAsmitha Karunanithi 
6240fda0f12SGeorge Liu             if (dumpStatus !=
6250fda0f12SGeorge Liu                     "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
62635440d18SAsmitha Karunanithi                 !dumpStatus.empty())
62735440d18SAsmitha Karunanithi             {
62835440d18SAsmitha Karunanithi                 // Dump status is not Complete
62935440d18SAsmitha Karunanithi                 // return not found until status is changed to Completed
630d1bde9e5SKrzysztof Grobelny                 messages::resourceNotFound(asyncResp->res, dumpType + " dump",
631d1bde9e5SKrzysztof Grobelny                                            entryID);
63235440d18SAsmitha Karunanithi                 return;
63335440d18SAsmitha Karunanithi             }
63435440d18SAsmitha Karunanithi 
6355cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["@odata.type"] =
63668dd075aSAsmitha Karunanithi                 "#LogEntry.v1_11_0.LogEntry";
637fdd26906SClaire Weinan             asyncResp->res.jsonValue["@odata.id"] = entriesPath + entryID;
6385cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Id"] = entryID;
6395cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["EntryType"] = "Event";
6405cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
641bbd80db8SClaire Weinan             asyncResp->res.jsonValue["Created"] =
642bbd80db8SClaire Weinan                 redfish::time_utils::getDateTimeUintUs(timestampUs);
6435cb1dd27SAsmitha Karunanithi 
64468dd075aSAsmitha Karunanithi             if (!originatorId.empty())
64568dd075aSAsmitha Karunanithi             {
64668dd075aSAsmitha Karunanithi                 asyncResp->res.jsonValue["Originator"] = originatorId;
64768dd075aSAsmitha Karunanithi                 asyncResp->res.jsonValue["OriginatorType"] = originatorType;
64868dd075aSAsmitha Karunanithi             }
64968dd075aSAsmitha Karunanithi 
6505cb1dd27SAsmitha Karunanithi             if (dumpType == "BMC")
6515cb1dd27SAsmitha Karunanithi             {
652d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager";
653d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["AdditionalDataURI"] =
654fdd26906SClaire Weinan                     entriesPath + entryID + "/attachment";
655fdd26906SClaire Weinan                 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
6565cb1dd27SAsmitha Karunanithi             }
6575cb1dd27SAsmitha Karunanithi             else if (dumpType == "System")
6585cb1dd27SAsmitha Karunanithi             {
659d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM";
660002d39b4SEd Tanous                 asyncResp->res.jsonValue["OEMDiagnosticDataType"] = "System";
661d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["AdditionalDataURI"] =
662fdd26906SClaire Weinan                     entriesPath + entryID + "/attachment";
663fdd26906SClaire Weinan                 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
6645cb1dd27SAsmitha Karunanithi             }
6655cb1dd27SAsmitha Karunanithi         }
666e05aec50SEd Tanous         if (!foundDumpEntry)
667b47452b2SAsmitha Karunanithi         {
668b90d14f2SMyung Bae             BMCWEB_LOG_WARNING << "Can't find Dump Entry " << entryID;
669b90d14f2SMyung Bae             messages::resourceNotFound(asyncResp->res, dumpType + " dump",
670b90d14f2SMyung Bae                                        entryID);
671b47452b2SAsmitha Karunanithi             return;
672b47452b2SAsmitha Karunanithi         }
673*5eb468daSGeorge Liu         });
6745cb1dd27SAsmitha Karunanithi }
6755cb1dd27SAsmitha Karunanithi 
6768d1b46d7Szhanghch05 inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6779878256fSStanley Chu                             const std::string& entryID,
678b47452b2SAsmitha Karunanithi                             const std::string& dumpType)
6795cb1dd27SAsmitha Karunanithi {
680002d39b4SEd Tanous     auto respHandler =
6815e7e2dc5SEd Tanous         [asyncResp, entryID](const boost::system::error_code& ec) {
6825cb1dd27SAsmitha Karunanithi         BMCWEB_LOG_DEBUG << "Dump Entry doDelete callback: Done";
6835cb1dd27SAsmitha Karunanithi         if (ec)
6845cb1dd27SAsmitha Karunanithi         {
6853de8d8baSGeorge Liu             if (ec.value() == EBADR)
6863de8d8baSGeorge Liu             {
6873de8d8baSGeorge Liu                 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
6883de8d8baSGeorge Liu                 return;
6893de8d8baSGeorge Liu             }
6905cb1dd27SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Dump (DBus) doDelete respHandler got error "
691fdd26906SClaire Weinan                              << ec << " entryID=" << entryID;
6925cb1dd27SAsmitha Karunanithi             messages::internalError(asyncResp->res);
6935cb1dd27SAsmitha Karunanithi             return;
6945cb1dd27SAsmitha Karunanithi         }
6955cb1dd27SAsmitha Karunanithi     };
6965cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
6975cb1dd27SAsmitha Karunanithi         respHandler, "xyz.openbmc_project.Dump.Manager",
698b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" +
699b47452b2SAsmitha Karunanithi             std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/" +
700b47452b2SAsmitha Karunanithi             entryID,
7015cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Object.Delete", "Delete");
7025cb1dd27SAsmitha Karunanithi }
7035cb1dd27SAsmitha Karunanithi 
7048e31778eSAsmitha Karunanithi inline DumpCreationProgress
7058e31778eSAsmitha Karunanithi     mapDbusStatusToDumpProgress(const std::string& status)
706a43be80fSAsmitha Karunanithi {
7078e31778eSAsmitha Karunanithi     if (status ==
7088e31778eSAsmitha Karunanithi             "xyz.openbmc_project.Common.Progress.OperationStatus.Failed" ||
7098e31778eSAsmitha Karunanithi         status == "xyz.openbmc_project.Common.Progress.OperationStatus.Aborted")
7108e31778eSAsmitha Karunanithi     {
7118e31778eSAsmitha Karunanithi         return DumpCreationProgress::DUMP_CREATE_FAILED;
7128e31778eSAsmitha Karunanithi     }
7138e31778eSAsmitha Karunanithi     if (status ==
7148e31778eSAsmitha Karunanithi         "xyz.openbmc_project.Common.Progress.OperationStatus.Completed")
7158e31778eSAsmitha Karunanithi     {
7168e31778eSAsmitha Karunanithi         return DumpCreationProgress::DUMP_CREATE_SUCCESS;
7178e31778eSAsmitha Karunanithi     }
7188e31778eSAsmitha Karunanithi     return DumpCreationProgress::DUMP_CREATE_INPROGRESS;
7198e31778eSAsmitha Karunanithi }
7208e31778eSAsmitha Karunanithi 
7218e31778eSAsmitha Karunanithi inline DumpCreationProgress
7228e31778eSAsmitha Karunanithi     getDumpCompletionStatus(const dbus::utility::DBusPropertiesMap& values)
7238e31778eSAsmitha Karunanithi {
7248e31778eSAsmitha Karunanithi     for (const auto& [key, val] : values)
7258e31778eSAsmitha Karunanithi     {
7268e31778eSAsmitha Karunanithi         if (key == "Status")
7278e31778eSAsmitha Karunanithi         {
7288e31778eSAsmitha Karunanithi             const std::string* value = std::get_if<std::string>(&val);
7298e31778eSAsmitha Karunanithi             if (value == nullptr)
7308e31778eSAsmitha Karunanithi             {
7318e31778eSAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "Status property value is null";
7328e31778eSAsmitha Karunanithi                 return DumpCreationProgress::DUMP_CREATE_FAILED;
7338e31778eSAsmitha Karunanithi             }
7348e31778eSAsmitha Karunanithi             return mapDbusStatusToDumpProgress(*value);
7358e31778eSAsmitha Karunanithi         }
7368e31778eSAsmitha Karunanithi     }
7378e31778eSAsmitha Karunanithi     return DumpCreationProgress::DUMP_CREATE_INPROGRESS;
7388e31778eSAsmitha Karunanithi }
7398e31778eSAsmitha Karunanithi 
7408e31778eSAsmitha Karunanithi inline std::string getDumpEntryPath(const std::string& dumpPath)
7418e31778eSAsmitha Karunanithi {
7428e31778eSAsmitha Karunanithi     if (dumpPath == "/xyz/openbmc_project/dump/bmc/entry")
7438e31778eSAsmitha Karunanithi     {
7448e31778eSAsmitha Karunanithi         return "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
7458e31778eSAsmitha Karunanithi     }
7468e31778eSAsmitha Karunanithi     if (dumpPath == "/xyz/openbmc_project/dump/system/entry")
7478e31778eSAsmitha Karunanithi     {
7488e31778eSAsmitha Karunanithi         return "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
7498e31778eSAsmitha Karunanithi     }
7508e31778eSAsmitha Karunanithi     return "";
7518e31778eSAsmitha Karunanithi }
7528e31778eSAsmitha Karunanithi 
7538e31778eSAsmitha Karunanithi inline void createDumpTaskCallback(
7548e31778eSAsmitha Karunanithi     task::Payload&& payload,
7558e31778eSAsmitha Karunanithi     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7568e31778eSAsmitha Karunanithi     const sdbusplus::message::object_path& createdObjPath)
7578e31778eSAsmitha Karunanithi {
7588e31778eSAsmitha Karunanithi     const std::string dumpPath = createdObjPath.parent_path().str;
7598e31778eSAsmitha Karunanithi     const std::string dumpId = createdObjPath.filename();
7608e31778eSAsmitha Karunanithi 
7618e31778eSAsmitha Karunanithi     std::string dumpEntryPath = getDumpEntryPath(dumpPath);
7628e31778eSAsmitha Karunanithi 
7638e31778eSAsmitha Karunanithi     if (dumpEntryPath.empty())
7648e31778eSAsmitha Karunanithi     {
7658e31778eSAsmitha Karunanithi         BMCWEB_LOG_ERROR << "Invalid dump type received";
7668e31778eSAsmitha Karunanithi         messages::internalError(asyncResp->res);
7678e31778eSAsmitha Karunanithi         return;
7688e31778eSAsmitha Karunanithi     }
7698e31778eSAsmitha Karunanithi 
7708e31778eSAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
7718e31778eSAsmitha Karunanithi         [asyncResp, payload, createdObjPath,
7728e31778eSAsmitha Karunanithi          dumpEntryPath{std::move(dumpEntryPath)},
7735e7e2dc5SEd Tanous          dumpId](const boost::system::error_code& ec,
7748e31778eSAsmitha Karunanithi                  const std::string& introspectXml) {
7758e31778eSAsmitha Karunanithi         if (ec)
7768e31778eSAsmitha Karunanithi         {
7778e31778eSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Introspect call failed with error: "
7788e31778eSAsmitha Karunanithi                              << ec.message();
7798e31778eSAsmitha Karunanithi             messages::internalError(asyncResp->res);
7808e31778eSAsmitha Karunanithi             return;
7818e31778eSAsmitha Karunanithi         }
7828e31778eSAsmitha Karunanithi 
7838e31778eSAsmitha Karunanithi         // Check if the created dump object has implemented Progress
7848e31778eSAsmitha Karunanithi         // interface to track dump completion. If yes, fetch the "Status"
7858e31778eSAsmitha Karunanithi         // property of the interface, modify the task state accordingly.
7868e31778eSAsmitha Karunanithi         // Else, return task completed.
7878e31778eSAsmitha Karunanithi         tinyxml2::XMLDocument doc;
7888e31778eSAsmitha Karunanithi 
7898e31778eSAsmitha Karunanithi         doc.Parse(introspectXml.data(), introspectXml.size());
7908e31778eSAsmitha Karunanithi         tinyxml2::XMLNode* pRoot = doc.FirstChildElement("node");
7918e31778eSAsmitha Karunanithi         if (pRoot == nullptr)
7928e31778eSAsmitha Karunanithi         {
7938e31778eSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "XML document failed to parse";
7948e31778eSAsmitha Karunanithi             messages::internalError(asyncResp->res);
7958e31778eSAsmitha Karunanithi             return;
7968e31778eSAsmitha Karunanithi         }
7978e31778eSAsmitha Karunanithi         tinyxml2::XMLElement* interfaceNode =
7988e31778eSAsmitha Karunanithi             pRoot->FirstChildElement("interface");
7998e31778eSAsmitha Karunanithi 
8008e31778eSAsmitha Karunanithi         bool isProgressIntfPresent = false;
8018e31778eSAsmitha Karunanithi         while (interfaceNode != nullptr)
8028e31778eSAsmitha Karunanithi         {
8038e31778eSAsmitha Karunanithi             const char* thisInterfaceName = interfaceNode->Attribute("name");
8048e31778eSAsmitha Karunanithi             if (thisInterfaceName != nullptr)
8058e31778eSAsmitha Karunanithi             {
8068e31778eSAsmitha Karunanithi                 if (thisInterfaceName ==
8078e31778eSAsmitha Karunanithi                     std::string_view("xyz.openbmc_project.Common.Progress"))
8088e31778eSAsmitha Karunanithi                 {
8098e31778eSAsmitha Karunanithi                     interfaceNode =
8108e31778eSAsmitha Karunanithi                         interfaceNode->NextSiblingElement("interface");
8118e31778eSAsmitha Karunanithi                     continue;
8128e31778eSAsmitha Karunanithi                 }
8138e31778eSAsmitha Karunanithi                 isProgressIntfPresent = true;
8148e31778eSAsmitha Karunanithi                 break;
8158e31778eSAsmitha Karunanithi             }
8168e31778eSAsmitha Karunanithi             interfaceNode = interfaceNode->NextSiblingElement("interface");
8178e31778eSAsmitha Karunanithi         }
8188e31778eSAsmitha Karunanithi 
819a43be80fSAsmitha Karunanithi         std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
8208e31778eSAsmitha Karunanithi             [createdObjPath, dumpEntryPath, dumpId, isProgressIntfPresent](
8215e7e2dc5SEd Tanous                 const boost::system::error_code& err, sdbusplus::message_t& msg,
822a43be80fSAsmitha Karunanithi                 const std::shared_ptr<task::TaskData>& taskData) {
823cb13a392SEd Tanous             if (err)
824cb13a392SEd Tanous             {
8258e31778eSAsmitha Karunanithi                 BMCWEB_LOG_ERROR << createdObjPath.str
8268e31778eSAsmitha Karunanithi                                  << ": Error in creating dump";
8278e31778eSAsmitha Karunanithi                 taskData->messages.emplace_back(messages::internalError());
8286145ed6fSAsmitha Karunanithi                 taskData->state = "Cancelled";
8296145ed6fSAsmitha Karunanithi                 return task::completed;
830cb13a392SEd Tanous             }
831b9d36b47SEd Tanous 
8328e31778eSAsmitha Karunanithi             if (isProgressIntfPresent)
833a43be80fSAsmitha Karunanithi             {
8348e31778eSAsmitha Karunanithi                 dbus::utility::DBusPropertiesMap values;
8358e31778eSAsmitha Karunanithi                 std::string prop;
8368e31778eSAsmitha Karunanithi                 msg.read(prop, values);
8378e31778eSAsmitha Karunanithi 
8388e31778eSAsmitha Karunanithi                 DumpCreationProgress dumpStatus =
8398e31778eSAsmitha Karunanithi                     getDumpCompletionStatus(values);
8408e31778eSAsmitha Karunanithi                 if (dumpStatus == DumpCreationProgress::DUMP_CREATE_FAILED)
8418e31778eSAsmitha Karunanithi                 {
8428e31778eSAsmitha Karunanithi                     BMCWEB_LOG_ERROR << createdObjPath.str
8438e31778eSAsmitha Karunanithi                                      << ": Error in creating dump";
8448e31778eSAsmitha Karunanithi                     taskData->state = "Cancelled";
8458e31778eSAsmitha Karunanithi                     return task::completed;
8468e31778eSAsmitha Karunanithi                 }
8478e31778eSAsmitha Karunanithi 
8488e31778eSAsmitha Karunanithi                 if (dumpStatus == DumpCreationProgress::DUMP_CREATE_INPROGRESS)
8498e31778eSAsmitha Karunanithi                 {
8508e31778eSAsmitha Karunanithi                     BMCWEB_LOG_DEBUG << createdObjPath.str
8518e31778eSAsmitha Karunanithi                                      << ": Dump creation task is in progress";
8528e31778eSAsmitha Karunanithi                     return !task::completed;
8538e31778eSAsmitha Karunanithi                 }
8548e31778eSAsmitha Karunanithi             }
8558e31778eSAsmitha Karunanithi 
856a43be80fSAsmitha Karunanithi             nlohmann::json retMessage = messages::success();
857a43be80fSAsmitha Karunanithi             taskData->messages.emplace_back(retMessage);
858a43be80fSAsmitha Karunanithi 
859c51a58eeSEd Tanous             boost::urls::url url = boost::urls::format(
860c51a58eeSEd Tanous                 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/{}", dumpId);
861c51a58eeSEd Tanous 
862c51a58eeSEd Tanous             std::string headerLoc = "Location: ";
863c51a58eeSEd Tanous             headerLoc += url.buffer();
864c51a58eeSEd Tanous 
865002d39b4SEd Tanous             taskData->payload->httpHeaders.emplace_back(std::move(headerLoc));
866a43be80fSAsmitha Karunanithi 
8678e31778eSAsmitha Karunanithi             BMCWEB_LOG_DEBUG << createdObjPath.str
8688e31778eSAsmitha Karunanithi                              << ": Dump creation task completed";
869a43be80fSAsmitha Karunanithi             taskData->state = "Completed";
870b47452b2SAsmitha Karunanithi             return task::completed;
871a43be80fSAsmitha Karunanithi             },
8728e31778eSAsmitha Karunanithi             "type='signal',interface='org.freedesktop.DBus.Properties',"
8738e31778eSAsmitha Karunanithi             "member='PropertiesChanged',path='" +
8748e31778eSAsmitha Karunanithi                 createdObjPath.str + "'");
875a43be80fSAsmitha Karunanithi 
8768e31778eSAsmitha Karunanithi         // The task timer is set to max time limit within which the
8778e31778eSAsmitha Karunanithi         // requested dump will be collected.
8788e31778eSAsmitha Karunanithi         task->startTimer(std::chrono::minutes(6));
879a43be80fSAsmitha Karunanithi         task->populateResp(asyncResp->res);
8808e31778eSAsmitha Karunanithi         task->payload.emplace(payload);
8818e31778eSAsmitha Karunanithi         },
8828e31778eSAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager", createdObjPath,
8838e31778eSAsmitha Karunanithi         "org.freedesktop.DBus.Introspectable", "Introspect");
884a43be80fSAsmitha Karunanithi }
885a43be80fSAsmitha Karunanithi 
8868d1b46d7Szhanghch05 inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8878d1b46d7Szhanghch05                        const crow::Request& req, const std::string& dumpType)
888a43be80fSAsmitha Karunanithi {
889fdd26906SClaire Weinan     std::string dumpPath = getDumpEntriesPath(dumpType);
890fdd26906SClaire Weinan     if (dumpPath.empty())
891a43be80fSAsmitha Karunanithi     {
892a43be80fSAsmitha Karunanithi         messages::internalError(asyncResp->res);
893a43be80fSAsmitha Karunanithi         return;
894a43be80fSAsmitha Karunanithi     }
895a43be80fSAsmitha Karunanithi 
896a43be80fSAsmitha Karunanithi     std::optional<std::string> diagnosticDataType;
897a43be80fSAsmitha Karunanithi     std::optional<std::string> oemDiagnosticDataType;
898a43be80fSAsmitha Karunanithi 
89915ed6780SWilly Tu     if (!redfish::json_util::readJsonAction(
900a43be80fSAsmitha Karunanithi             req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
901a43be80fSAsmitha Karunanithi             "OEMDiagnosticDataType", oemDiagnosticDataType))
902a43be80fSAsmitha Karunanithi     {
903a43be80fSAsmitha Karunanithi         return;
904a43be80fSAsmitha Karunanithi     }
905a43be80fSAsmitha Karunanithi 
906a43be80fSAsmitha Karunanithi     if (dumpType == "System")
907a43be80fSAsmitha Karunanithi     {
908a43be80fSAsmitha Karunanithi         if (!oemDiagnosticDataType || !diagnosticDataType)
909a43be80fSAsmitha Karunanithi         {
9104978b63fSJason M. Bills             BMCWEB_LOG_ERROR
9114978b63fSJason M. Bills                 << "CreateDump action parameter 'DiagnosticDataType'/'OEMDiagnosticDataType' value not found!";
912a43be80fSAsmitha Karunanithi             messages::actionParameterMissing(
913a43be80fSAsmitha Karunanithi                 asyncResp->res, "CollectDiagnosticData",
914a43be80fSAsmitha Karunanithi                 "DiagnosticDataType & OEMDiagnosticDataType");
915a43be80fSAsmitha Karunanithi             return;
916a43be80fSAsmitha Karunanithi         }
9173174e4dfSEd Tanous         if ((*oemDiagnosticDataType != "System") ||
918a43be80fSAsmitha Karunanithi             (*diagnosticDataType != "OEM"))
919a43be80fSAsmitha Karunanithi         {
920a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Wrong parameter values passed";
921ace85d60SEd Tanous             messages::internalError(asyncResp->res);
922a43be80fSAsmitha Karunanithi             return;
923a43be80fSAsmitha Karunanithi         }
9245907571dSAsmitha Karunanithi         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/";
925a43be80fSAsmitha Karunanithi     }
926a43be80fSAsmitha Karunanithi     else if (dumpType == "BMC")
927a43be80fSAsmitha Karunanithi     {
928a43be80fSAsmitha Karunanithi         if (!diagnosticDataType)
929a43be80fSAsmitha Karunanithi         {
9300fda0f12SGeorge Liu             BMCWEB_LOG_ERROR
9310fda0f12SGeorge Liu                 << "CreateDump action parameter 'DiagnosticDataType' not found!";
932a43be80fSAsmitha Karunanithi             messages::actionParameterMissing(
933a43be80fSAsmitha Karunanithi                 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
934a43be80fSAsmitha Karunanithi             return;
935a43be80fSAsmitha Karunanithi         }
9363174e4dfSEd Tanous         if (*diagnosticDataType != "Manager")
937a43be80fSAsmitha Karunanithi         {
938a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR
939a43be80fSAsmitha Karunanithi                 << "Wrong parameter value passed for 'DiagnosticDataType'";
940ace85d60SEd Tanous             messages::internalError(asyncResp->res);
941a43be80fSAsmitha Karunanithi             return;
942a43be80fSAsmitha Karunanithi         }
9435907571dSAsmitha Karunanithi         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/";
9445907571dSAsmitha Karunanithi     }
9455907571dSAsmitha Karunanithi     else
9465907571dSAsmitha Karunanithi     {
9475907571dSAsmitha Karunanithi         BMCWEB_LOG_ERROR << "CreateDump failed. Unknown dump type";
9485907571dSAsmitha Karunanithi         messages::internalError(asyncResp->res);
9495907571dSAsmitha Karunanithi         return;
950a43be80fSAsmitha Karunanithi     }
951a43be80fSAsmitha Karunanithi 
9528e31778eSAsmitha Karunanithi     std::vector<std::pair<std::string, std::variant<std::string, uint64_t>>>
9538e31778eSAsmitha Karunanithi         createDumpParamVec;
9548e31778eSAsmitha Karunanithi 
955f574a8e1SCarson Labrado     if (req.session != nullptr)
956f574a8e1SCarson Labrado     {
95768dd075aSAsmitha Karunanithi         createDumpParamVec.emplace_back(
95868dd075aSAsmitha Karunanithi             "xyz.openbmc_project.Dump.Create.CreateParameters.OriginatorId",
95968dd075aSAsmitha Karunanithi             req.session->clientIp);
96068dd075aSAsmitha Karunanithi         createDumpParamVec.emplace_back(
96168dd075aSAsmitha Karunanithi             "xyz.openbmc_project.Dump.Create.CreateParameters.OriginatorType",
96268dd075aSAsmitha Karunanithi             "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.Client");
963f574a8e1SCarson Labrado     }
96468dd075aSAsmitha Karunanithi 
965a43be80fSAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
9665e7e2dc5SEd Tanous         [asyncResp, payload(task::Payload(req)),
9675e7e2dc5SEd Tanous          dumpPath](const boost::system::error_code& ec,
9685e7e2dc5SEd Tanous                    const sdbusplus::message_t& msg,
9698e31778eSAsmitha Karunanithi                    const sdbusplus::message::object_path& objPath) mutable {
970a43be80fSAsmitha Karunanithi         if (ec)
971a43be80fSAsmitha Karunanithi         {
972a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "CreateDump resp_handler got error " << ec;
9735907571dSAsmitha Karunanithi             const sd_bus_error* dbusError = msg.get_error();
9745907571dSAsmitha Karunanithi             if (dbusError == nullptr)
9755907571dSAsmitha Karunanithi             {
9765907571dSAsmitha Karunanithi                 messages::internalError(asyncResp->res);
9775907571dSAsmitha Karunanithi                 return;
9785907571dSAsmitha Karunanithi             }
9795907571dSAsmitha Karunanithi 
9805907571dSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "CreateDump DBus error: " << dbusError->name
9815907571dSAsmitha Karunanithi                              << " and error msg: " << dbusError->message;
9825907571dSAsmitha Karunanithi             if (std::string_view(
9835907571dSAsmitha Karunanithi                     "xyz.openbmc_project.Common.Error.NotAllowed") ==
9845907571dSAsmitha Karunanithi                 dbusError->name)
9855907571dSAsmitha Karunanithi             {
9865907571dSAsmitha Karunanithi                 messages::resourceInStandby(asyncResp->res);
9875907571dSAsmitha Karunanithi                 return;
9885907571dSAsmitha Karunanithi             }
9895907571dSAsmitha Karunanithi             if (std::string_view(
9905907571dSAsmitha Karunanithi                     "xyz.openbmc_project.Dump.Create.Error.Disabled") ==
9915907571dSAsmitha Karunanithi                 dbusError->name)
9925907571dSAsmitha Karunanithi             {
9935907571dSAsmitha Karunanithi                 messages::serviceDisabled(asyncResp->res, dumpPath);
9945907571dSAsmitha Karunanithi                 return;
9955907571dSAsmitha Karunanithi             }
9965907571dSAsmitha Karunanithi             if (std::string_view(
9975907571dSAsmitha Karunanithi                     "xyz.openbmc_project.Common.Error.Unavailable") ==
9985907571dSAsmitha Karunanithi                 dbusError->name)
9995907571dSAsmitha Karunanithi             {
10005907571dSAsmitha Karunanithi                 messages::resourceInUse(asyncResp->res);
10015907571dSAsmitha Karunanithi                 return;
10025907571dSAsmitha Karunanithi             }
10035907571dSAsmitha Karunanithi             // Other Dbus errors such as:
10045907571dSAsmitha Karunanithi             // xyz.openbmc_project.Common.Error.InvalidArgument &
10055907571dSAsmitha Karunanithi             // org.freedesktop.DBus.Error.InvalidArgs are all related to
10065907571dSAsmitha Karunanithi             // the dbus call that is made here in the bmcweb
10075907571dSAsmitha Karunanithi             // implementation and has nothing to do with the client's
10085907571dSAsmitha Karunanithi             // input in the request. Hence, returning internal error
10095907571dSAsmitha Karunanithi             // back to the client.
1010a43be80fSAsmitha Karunanithi             messages::internalError(asyncResp->res);
1011a43be80fSAsmitha Karunanithi             return;
1012a43be80fSAsmitha Karunanithi         }
10138e31778eSAsmitha Karunanithi         BMCWEB_LOG_DEBUG << "Dump Created. Path: " << objPath.str;
10148e31778eSAsmitha Karunanithi         createDumpTaskCallback(std::move(payload), asyncResp, objPath);
1015a43be80fSAsmitha Karunanithi         },
1016b47452b2SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager",
1017b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" +
1018b47452b2SAsmitha Karunanithi             std::string(boost::algorithm::to_lower_copy(dumpType)),
10198e31778eSAsmitha Karunanithi         "xyz.openbmc_project.Dump.Create", "CreateDump", createDumpParamVec);
1020a43be80fSAsmitha Karunanithi }
1021a43be80fSAsmitha Karunanithi 
10228d1b46d7Szhanghch05 inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
10238d1b46d7Szhanghch05                       const std::string& dumpType)
102480319af1SAsmitha Karunanithi {
1025b47452b2SAsmitha Karunanithi     std::string dumpTypeLowerCopy =
1026b47452b2SAsmitha Karunanithi         std::string(boost::algorithm::to_lower_copy(dumpType));
10278d1b46d7Szhanghch05 
10280d946211SClaire Weinan     crow::connections::systemBus->async_method_call(
10290d946211SClaire Weinan         [asyncResp](const boost::system::error_code& ec) {
103080319af1SAsmitha Karunanithi         if (ec)
103180319af1SAsmitha Karunanithi         {
10320d946211SClaire Weinan             BMCWEB_LOG_ERROR << "clearDump resp_handler got error " << ec;
103380319af1SAsmitha Karunanithi             messages::internalError(asyncResp->res);
103480319af1SAsmitha Karunanithi             return;
103580319af1SAsmitha Karunanithi         }
10360d946211SClaire Weinan         },
10370d946211SClaire Weinan         "xyz.openbmc_project.Dump.Manager",
10380d946211SClaire Weinan         "/xyz/openbmc_project/dump/" + dumpTypeLowerCopy,
10390d946211SClaire Weinan         "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
104080319af1SAsmitha Karunanithi }
104180319af1SAsmitha Karunanithi 
1042b9d36b47SEd Tanous inline static void
1043b9d36b47SEd Tanous     parseCrashdumpParameters(const dbus::utility::DBusPropertiesMap& params,
1044b9d36b47SEd Tanous                              std::string& filename, std::string& timestamp,
1045b9d36b47SEd Tanous                              std::string& logfile)
1046043a0536SJohnathan Mantey {
1047d1bde9e5SKrzysztof Grobelny     const std::string* filenamePtr = nullptr;
1048d1bde9e5SKrzysztof Grobelny     const std::string* timestampPtr = nullptr;
1049d1bde9e5SKrzysztof Grobelny     const std::string* logfilePtr = nullptr;
1050d1bde9e5SKrzysztof Grobelny 
1051d1bde9e5SKrzysztof Grobelny     const bool success = sdbusplus::unpackPropertiesNoThrow(
1052d1bde9e5SKrzysztof Grobelny         dbus_utils::UnpackErrorPrinter(), params, "Timestamp", timestampPtr,
1053d1bde9e5SKrzysztof Grobelny         "Filename", filenamePtr, "Log", logfilePtr);
1054d1bde9e5SKrzysztof Grobelny 
1055d1bde9e5SKrzysztof Grobelny     if (!success)
1056043a0536SJohnathan Mantey     {
1057d1bde9e5SKrzysztof Grobelny         return;
1058043a0536SJohnathan Mantey     }
1059d1bde9e5SKrzysztof Grobelny 
1060d1bde9e5SKrzysztof Grobelny     if (filenamePtr != nullptr)
1061043a0536SJohnathan Mantey     {
1062d1bde9e5SKrzysztof Grobelny         filename = *filenamePtr;
1063d1bde9e5SKrzysztof Grobelny     }
1064d1bde9e5SKrzysztof Grobelny 
1065d1bde9e5SKrzysztof Grobelny     if (timestampPtr != nullptr)
1066043a0536SJohnathan Mantey     {
1067d1bde9e5SKrzysztof Grobelny         timestamp = *timestampPtr;
1068043a0536SJohnathan Mantey     }
1069d1bde9e5SKrzysztof Grobelny 
1070d1bde9e5SKrzysztof Grobelny     if (logfilePtr != nullptr)
1071043a0536SJohnathan Mantey     {
1072d1bde9e5SKrzysztof Grobelny         logfile = *logfilePtr;
1073043a0536SJohnathan Mantey     }
1074043a0536SJohnathan Mantey }
1075043a0536SJohnathan Mantey 
10767e860f15SJohn Edward Broadbent inline void requestRoutesSystemLogServiceCollection(App& app)
10771da66f75SEd Tanous {
1078c4bf6374SJason M. Bills     /**
1079c4bf6374SJason M. Bills      * Functions triggers appropriate requests on DBus
1080c4bf6374SJason M. Bills      */
108122d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/")
1082ed398213SEd Tanous         .privileges(redfish::privileges::getLogServiceCollection)
1083002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1084002d39b4SEd Tanous             [&app](const crow::Request& req,
108522d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
108622d268cbSEd Tanous                    const std::string& systemName) {
10873ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1088c4bf6374SJason M. Bills         {
108945ca1b86SEd Tanous             return;
109045ca1b86SEd Tanous         }
109122d268cbSEd Tanous         if (systemName != "system")
109222d268cbSEd Tanous         {
109322d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
109422d268cbSEd Tanous                                        systemName);
109522d268cbSEd Tanous             return;
109622d268cbSEd Tanous         }
109722d268cbSEd Tanous 
10987e860f15SJohn Edward Broadbent         // Collections don't include the static data added by SubRoute
10997e860f15SJohn Edward Broadbent         // because it has a duplicate entry for members
1100c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1101c4bf6374SJason M. Bills             "#LogServiceCollection.LogServiceCollection";
1102c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
1103029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices";
110445ca1b86SEd Tanous         asyncResp->res.jsonValue["Name"] = "System Log Services Collection";
1105c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] =
1106c4bf6374SJason M. Bills             "Collection of LogServices for this Computer System";
1107002d39b4SEd Tanous         nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
1108c4bf6374SJason M. Bills         logServiceArray = nlohmann::json::array();
11091476687dSEd Tanous         nlohmann::json::object_t eventLog;
11101476687dSEd Tanous         eventLog["@odata.id"] =
11111476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog";
1112b2ba3072SPatrick Williams         logServiceArray.emplace_back(std::move(eventLog));
11135cb1dd27SAsmitha Karunanithi #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
11141476687dSEd Tanous         nlohmann::json::object_t dumpLog;
1115002d39b4SEd Tanous         dumpLog["@odata.id"] = "/redfish/v1/Systems/system/LogServices/Dump";
1116b2ba3072SPatrick Williams         logServiceArray.emplace_back(std::move(dumpLog));
1117c9bb6861Sraviteja-b #endif
1118c9bb6861Sraviteja-b 
1119d53dd41fSJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
11201476687dSEd Tanous         nlohmann::json::object_t crashdump;
11211476687dSEd Tanous         crashdump["@odata.id"] =
11221476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Crashdump";
1123b2ba3072SPatrick Williams         logServiceArray.emplace_back(std::move(crashdump));
1124d53dd41fSJason M. Bills #endif
1125b7028ebfSSpencer Ku 
1126b7028ebfSSpencer Ku #ifdef BMCWEB_ENABLE_REDFISH_HOST_LOGGER
11271476687dSEd Tanous         nlohmann::json::object_t hostlogger;
11281476687dSEd Tanous         hostlogger["@odata.id"] =
11291476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/HostLogger";
1130b2ba3072SPatrick Williams         logServiceArray.emplace_back(std::move(hostlogger));
1131b7028ebfSSpencer Ku #endif
1132c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] =
1133c4bf6374SJason M. Bills             logServiceArray.size();
1134a3316fc6SZhikuiRen 
11357a1dbc48SGeorge Liu         constexpr std::array<std::string_view, 1> interfaces = {
11367a1dbc48SGeorge Liu             "xyz.openbmc_project.State.Boot.PostCode"};
11377a1dbc48SGeorge Liu         dbus::utility::getSubTreePaths(
11387a1dbc48SGeorge Liu             "/", 0, interfaces,
11397a1dbc48SGeorge Liu             [asyncResp](const boost::system::error_code& ec,
1140b9d36b47SEd Tanous                         const dbus::utility::MapperGetSubTreePathsResponse&
1141b9d36b47SEd Tanous                             subtreePath) {
1142a3316fc6SZhikuiRen             if (ec)
1143a3316fc6SZhikuiRen             {
1144a3316fc6SZhikuiRen                 BMCWEB_LOG_ERROR << ec;
1145a3316fc6SZhikuiRen                 return;
1146a3316fc6SZhikuiRen             }
1147a3316fc6SZhikuiRen 
114855f79e6fSEd Tanous             for (const auto& pathStr : subtreePath)
1149a3316fc6SZhikuiRen             {
1150a3316fc6SZhikuiRen                 if (pathStr.find("PostCode") != std::string::npos)
1151a3316fc6SZhikuiRen                 {
115223a21a1cSEd Tanous                     nlohmann::json& logServiceArrayLocal =
1153a3316fc6SZhikuiRen                         asyncResp->res.jsonValue["Members"];
1154613dabeaSEd Tanous                     nlohmann::json::object_t member;
1155613dabeaSEd Tanous                     member["@odata.id"] =
1156613dabeaSEd Tanous                         "/redfish/v1/Systems/system/LogServices/PostCodes";
1157613dabeaSEd Tanous 
1158b2ba3072SPatrick Williams                     logServiceArrayLocal.emplace_back(std::move(member));
1159613dabeaSEd Tanous 
116045ca1b86SEd Tanous                     asyncResp->res.jsonValue["Members@odata.count"] =
116123a21a1cSEd Tanous                         logServiceArrayLocal.size();
1162a3316fc6SZhikuiRen                     return;
1163a3316fc6SZhikuiRen                 }
1164a3316fc6SZhikuiRen             }
11657a1dbc48SGeorge Liu             });
11667e860f15SJohn Edward Broadbent         });
1167c4bf6374SJason M. Bills }
1168c4bf6374SJason M. Bills 
11697e860f15SJohn Edward Broadbent inline void requestRoutesEventLogService(App& app)
1170c4bf6374SJason M. Bills {
117122d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/")
1172ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
1173002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1174002d39b4SEd Tanous             [&app](const crow::Request& req,
117522d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
117622d268cbSEd Tanous                    const std::string& systemName) {
11773ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
117845ca1b86SEd Tanous         {
117945ca1b86SEd Tanous             return;
118045ca1b86SEd Tanous         }
118122d268cbSEd Tanous         if (systemName != "system")
118222d268cbSEd Tanous         {
118322d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
118422d268cbSEd Tanous                                        systemName);
118522d268cbSEd Tanous             return;
118622d268cbSEd Tanous         }
1187c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
1188029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog";
1189c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1190c4bf6374SJason M. Bills             "#LogService.v1_1_0.LogService";
1191c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Event Log Service";
1192002d39b4SEd Tanous         asyncResp->res.jsonValue["Description"] = "System Event Log Service";
1193c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Id"] = "EventLog";
1194c4bf6374SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
11957c8c4058STejas Patil 
11967c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
11972b82937eSEd Tanous             redfish::time_utils::getDateTimeOffsetNow();
11987c8c4058STejas Patil 
11997c8c4058STejas Patil         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
12007c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
12017c8c4058STejas Patil             redfishDateTimeOffset.second;
12027c8c4058STejas Patil 
12031476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
12041476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1205e7d6c8b2SGunnar Mills         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
1206e7d6c8b2SGunnar Mills 
12070fda0f12SGeorge Liu             {"target",
12080fda0f12SGeorge Liu              "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog"}};
12097e860f15SJohn Edward Broadbent         });
1210489640c6SJason M. Bills }
1211489640c6SJason M. Bills 
12127e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogClear(App& app)
1213489640c6SJason M. Bills {
12144978b63fSJason M. Bills     BMCWEB_ROUTE(
12154978b63fSJason M. Bills         app,
121622d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
1217432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
12187e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
121945ca1b86SEd Tanous             [&app](const crow::Request& req,
122022d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
122122d268cbSEd Tanous                    const std::string& systemName) {
12223ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
122345ca1b86SEd Tanous         {
122445ca1b86SEd Tanous             return;
122545ca1b86SEd Tanous         }
122622d268cbSEd Tanous         if (systemName != "system")
122722d268cbSEd Tanous         {
122822d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
122922d268cbSEd Tanous                                        systemName);
123022d268cbSEd Tanous             return;
123122d268cbSEd Tanous         }
1232489640c6SJason M. Bills         // Clear the EventLog by deleting the log files
1233489640c6SJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
1234489640c6SJason M. Bills         if (getRedfishLogFiles(redfishLogFiles))
1235489640c6SJason M. Bills         {
1236489640c6SJason M. Bills             for (const std::filesystem::path& file : redfishLogFiles)
1237489640c6SJason M. Bills             {
1238489640c6SJason M. Bills                 std::error_code ec;
1239489640c6SJason M. Bills                 std::filesystem::remove(file, ec);
1240489640c6SJason M. Bills             }
1241489640c6SJason M. Bills         }
1242489640c6SJason M. Bills 
1243489640c6SJason M. Bills         // Reload rsyslog so it knows to start new log files
1244489640c6SJason M. Bills         crow::connections::systemBus->async_method_call(
12455e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec) {
1246489640c6SJason M. Bills             if (ec)
1247489640c6SJason M. Bills             {
1248002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "Failed to reload rsyslog: " << ec;
1249489640c6SJason M. Bills                 messages::internalError(asyncResp->res);
1250489640c6SJason M. Bills                 return;
1251489640c6SJason M. Bills             }
1252489640c6SJason M. Bills 
1253489640c6SJason M. Bills             messages::success(asyncResp->res);
1254489640c6SJason M. Bills             },
1255489640c6SJason M. Bills             "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1256002d39b4SEd Tanous             "org.freedesktop.systemd1.Manager", "ReloadUnit", "rsyslog.service",
1257002d39b4SEd Tanous             "replace");
12587e860f15SJohn Edward Broadbent         });
1259c4bf6374SJason M. Bills }
1260c4bf6374SJason M. Bills 
1261ac992cdeSJason M. Bills enum class LogParseError
1262ac992cdeSJason M. Bills {
1263ac992cdeSJason M. Bills     success,
1264ac992cdeSJason M. Bills     parseFailed,
1265ac992cdeSJason M. Bills     messageIdNotInRegistry,
1266ac992cdeSJason M. Bills };
1267ac992cdeSJason M. Bills 
1268ac992cdeSJason M. Bills static LogParseError
1269ac992cdeSJason M. Bills     fillEventLogEntryJson(const std::string& logEntryID,
1270b5a76932SEd Tanous                           const std::string& logEntry,
1271de703c5dSJason M. Bills                           nlohmann::json::object_t& logEntryJson)
1272c4bf6374SJason M. Bills {
127395820184SJason M. Bills     // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
1274cd225da8SJason M. Bills     // First get the Timestamp
1275f23b7296SEd Tanous     size_t space = logEntry.find_first_of(' ');
1276cd225da8SJason M. Bills     if (space == std::string::npos)
127795820184SJason M. Bills     {
1278ac992cdeSJason M. Bills         return LogParseError::parseFailed;
127995820184SJason M. Bills     }
1280cd225da8SJason M. Bills     std::string timestamp = logEntry.substr(0, space);
1281cd225da8SJason M. Bills     // Then get the log contents
1282f23b7296SEd Tanous     size_t entryStart = logEntry.find_first_not_of(' ', space);
1283cd225da8SJason M. Bills     if (entryStart == std::string::npos)
1284cd225da8SJason M. Bills     {
1285ac992cdeSJason M. Bills         return LogParseError::parseFailed;
1286cd225da8SJason M. Bills     }
1287cd225da8SJason M. Bills     std::string_view entry(logEntry);
1288cd225da8SJason M. Bills     entry.remove_prefix(entryStart);
1289cd225da8SJason M. Bills     // Use split to separate the entry into its fields
1290cd225da8SJason M. Bills     std::vector<std::string> logEntryFields;
129150ebd4afSEd Tanous     bmcweb::split(logEntryFields, entry, ',');
1292cd225da8SJason M. Bills     // We need at least a MessageId to be valid
129326f6976fSEd Tanous     if (logEntryFields.empty())
1294cd225da8SJason M. Bills     {
1295ac992cdeSJason M. Bills         return LogParseError::parseFailed;
1296cd225da8SJason M. Bills     }
1297cd225da8SJason M. Bills     std::string& messageID = logEntryFields[0];
129895820184SJason M. Bills 
12994851d45dSJason M. Bills     // Get the Message from the MessageRegistry
1300fffb8c1fSEd Tanous     const registries::Message* message = registries::getMessage(messageID);
1301c4bf6374SJason M. Bills 
130254417b02SSui Chen     if (message == nullptr)
1303c4bf6374SJason M. Bills     {
130454417b02SSui Chen         BMCWEB_LOG_WARNING << "Log entry not found in registry: " << logEntry;
1305ac992cdeSJason M. Bills         return LogParseError::messageIdNotInRegistry;
1306c4bf6374SJason M. Bills     }
1307c4bf6374SJason M. Bills 
130854417b02SSui Chen     std::string msg = message->message;
130954417b02SSui Chen 
131015a86ff6SJason M. Bills     // Get the MessageArgs from the log if there are any
131126702d01SEd Tanous     std::span<std::string> messageArgs;
131215a86ff6SJason M. Bills     if (logEntryFields.size() > 1)
131315a86ff6SJason M. Bills     {
131415a86ff6SJason M. Bills         std::string& messageArgsStart = logEntryFields[1];
131515a86ff6SJason M. Bills         // If the first string is empty, assume there are no MessageArgs
131615a86ff6SJason M. Bills         std::size_t messageArgsSize = 0;
131715a86ff6SJason M. Bills         if (!messageArgsStart.empty())
131815a86ff6SJason M. Bills         {
131915a86ff6SJason M. Bills             messageArgsSize = logEntryFields.size() - 1;
132015a86ff6SJason M. Bills         }
132115a86ff6SJason M. Bills 
132223a21a1cSEd Tanous         messageArgs = {&messageArgsStart, messageArgsSize};
1323c4bf6374SJason M. Bills 
13244851d45dSJason M. Bills         // Fill the MessageArgs into the Message
132595820184SJason M. Bills         int i = 0;
132695820184SJason M. Bills         for (const std::string& messageArg : messageArgs)
13274851d45dSJason M. Bills         {
132895820184SJason M. Bills             std::string argStr = "%" + std::to_string(++i);
13294851d45dSJason M. Bills             size_t argPos = msg.find(argStr);
13304851d45dSJason M. Bills             if (argPos != std::string::npos)
13314851d45dSJason M. Bills             {
133295820184SJason M. Bills                 msg.replace(argPos, argStr.length(), messageArg);
13334851d45dSJason M. Bills             }
13344851d45dSJason M. Bills         }
133515a86ff6SJason M. Bills     }
13364851d45dSJason M. Bills 
133795820184SJason M. Bills     // Get the Created time from the timestamp. The log timestamp is in RFC3339
133895820184SJason M. Bills     // format which matches the Redfish format except for the fractional seconds
133995820184SJason M. Bills     // between the '.' and the '+', so just remove them.
1340f23b7296SEd Tanous     std::size_t dot = timestamp.find_first_of('.');
1341f23b7296SEd Tanous     std::size_t plus = timestamp.find_first_of('+');
134295820184SJason M. Bills     if (dot != std::string::npos && plus != std::string::npos)
1343c4bf6374SJason M. Bills     {
134495820184SJason M. Bills         timestamp.erase(dot, plus - dot);
1345c4bf6374SJason M. Bills     }
1346c4bf6374SJason M. Bills 
1347c4bf6374SJason M. Bills     // Fill in the log entry with the gathered data
13489c11a172SVijay Lobo     logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
1349ef4c65b7SEd Tanous     logEntryJson["@odata.id"] = boost::urls::format(
1350ef4c65b7SEd Tanous         "/redfish/v1/Systems/system/LogServices/EventLog/Entries/{}",
1351ef4c65b7SEd Tanous         logEntryID);
135284afc48bSJason M. Bills     logEntryJson["Name"] = "System Event Log Entry";
135384afc48bSJason M. Bills     logEntryJson["Id"] = logEntryID;
135484afc48bSJason M. Bills     logEntryJson["Message"] = std::move(msg);
135584afc48bSJason M. Bills     logEntryJson["MessageId"] = std::move(messageID);
135684afc48bSJason M. Bills     logEntryJson["MessageArgs"] = messageArgs;
135784afc48bSJason M. Bills     logEntryJson["EntryType"] = "Event";
135884afc48bSJason M. Bills     logEntryJson["Severity"] = message->messageSeverity;
135984afc48bSJason M. Bills     logEntryJson["Created"] = std::move(timestamp);
1360ac992cdeSJason M. Bills     return LogParseError::success;
1361c4bf6374SJason M. Bills }
1362c4bf6374SJason M. Bills 
13637e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogEntryCollection(App& app)
1364c4bf6374SJason M. Bills {
136522d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
13668b6a35f0SGunnar Mills         .privileges(redfish::privileges::getLogEntryCollection)
1367002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1368002d39b4SEd Tanous             [&app](const crow::Request& req,
136922d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
137022d268cbSEd Tanous                    const std::string& systemName) {
1371c937d2bfSEd Tanous         query_param::QueryCapabilities capabilities = {
1372c937d2bfSEd Tanous             .canDelegateTop = true,
1373c937d2bfSEd Tanous             .canDelegateSkip = true,
1374c937d2bfSEd Tanous         };
1375c937d2bfSEd Tanous         query_param::Query delegatedQuery;
1376c937d2bfSEd Tanous         if (!redfish::setUpRedfishRouteWithDelegation(
13773ba00073SCarson Labrado                 app, req, asyncResp, delegatedQuery, capabilities))
1378c4bf6374SJason M. Bills         {
1379c4bf6374SJason M. Bills             return;
1380c4bf6374SJason M. Bills         }
138122d268cbSEd Tanous         if (systemName != "system")
138222d268cbSEd Tanous         {
138322d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
138422d268cbSEd Tanous                                        systemName);
138522d268cbSEd Tanous             return;
138622d268cbSEd Tanous         }
138722d268cbSEd Tanous 
13885143f7a5SJiaqing Zhao         size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
13893648c8beSEd Tanous         size_t skip = delegatedQuery.skip.value_or(0);
13903648c8beSEd Tanous 
13917e860f15SJohn Edward Broadbent         // Collections don't include the static data added by SubRoute
13927e860f15SJohn Edward Broadbent         // because it has a duplicate entry for members
1393c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1394c4bf6374SJason M. Bills             "#LogEntryCollection.LogEntryCollection";
1395c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
1396029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1397c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1398c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] =
1399c4bf6374SJason M. Bills             "Collection of System Event Log Entries";
1400cb92c03bSAndrew Geissler 
14014978b63fSJason M. Bills         nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1402c4bf6374SJason M. Bills         logEntryArray = nlohmann::json::array();
14037e860f15SJohn Edward Broadbent         // Go through the log files and create a unique ID for each
14047e860f15SJohn Edward Broadbent         // entry
140595820184SJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
140695820184SJason M. Bills         getRedfishLogFiles(redfishLogFiles);
1407b01bf299SEd Tanous         uint64_t entryCount = 0;
1408cd225da8SJason M. Bills         std::string logEntry;
140995820184SJason M. Bills 
14107e860f15SJohn Edward Broadbent         // Oldest logs are in the last file, so start there and loop
14117e860f15SJohn Edward Broadbent         // backwards
1412002d39b4SEd Tanous         for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1413002d39b4SEd Tanous              it++)
1414c4bf6374SJason M. Bills         {
1415cd225da8SJason M. Bills             std::ifstream logStream(*it);
141695820184SJason M. Bills             if (!logStream.is_open())
1417c4bf6374SJason M. Bills             {
1418c4bf6374SJason M. Bills                 continue;
1419c4bf6374SJason M. Bills             }
1420c4bf6374SJason M. Bills 
1421e85d6b16SJason M. Bills             // Reset the unique ID on the first entry
1422e85d6b16SJason M. Bills             bool firstEntry = true;
142395820184SJason M. Bills             while (std::getline(logStream, logEntry))
142495820184SJason M. Bills             {
1425c4bf6374SJason M. Bills                 std::string idStr;
1426e85d6b16SJason M. Bills                 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1427c4bf6374SJason M. Bills                 {
1428c4bf6374SJason M. Bills                     continue;
1429c4bf6374SJason M. Bills                 }
1430e85d6b16SJason M. Bills                 firstEntry = false;
1431e85d6b16SJason M. Bills 
1432de703c5dSJason M. Bills                 nlohmann::json::object_t bmcLogEntry;
143389492a15SPatrick Williams                 LogParseError status = fillEventLogEntryJson(idStr, logEntry,
143489492a15SPatrick Williams                                                              bmcLogEntry);
1435ac992cdeSJason M. Bills                 if (status == LogParseError::messageIdNotInRegistry)
1436ac992cdeSJason M. Bills                 {
1437ac992cdeSJason M. Bills                     continue;
1438ac992cdeSJason M. Bills                 }
1439ac992cdeSJason M. Bills                 if (status != LogParseError::success)
1440c4bf6374SJason M. Bills                 {
1441c4bf6374SJason M. Bills                     messages::internalError(asyncResp->res);
1442c4bf6374SJason M. Bills                     return;
1443c4bf6374SJason M. Bills                 }
1444de703c5dSJason M. Bills 
1445de703c5dSJason M. Bills                 entryCount++;
1446de703c5dSJason M. Bills                 // Handle paging using skip (number of entries to skip from the
1447de703c5dSJason M. Bills                 // start) and top (number of entries to display)
14483648c8beSEd Tanous                 if (entryCount <= skip || entryCount > skip + top)
1449de703c5dSJason M. Bills                 {
1450de703c5dSJason M. Bills                     continue;
1451de703c5dSJason M. Bills                 }
1452de703c5dSJason M. Bills 
1453b2ba3072SPatrick Williams                 logEntryArray.emplace_back(std::move(bmcLogEntry));
1454c4bf6374SJason M. Bills             }
145595820184SJason M. Bills         }
1456c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
14573648c8beSEd Tanous         if (skip + top < entryCount)
1458c4bf6374SJason M. Bills         {
1459c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Members@odata.nextLink"] =
14604978b63fSJason M. Bills                 "/redfish/v1/Systems/system/LogServices/EventLog/Entries?$skip=" +
14613648c8beSEd Tanous                 std::to_string(skip + top);
1462c4bf6374SJason M. Bills         }
14637e860f15SJohn Edward Broadbent         });
1464897967deSJason M. Bills }
1465897967deSJason M. Bills 
14667e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogEntry(App& app)
1467897967deSJason M. Bills {
14687e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
146922d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
1470ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
14717e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
147245ca1b86SEd Tanous             [&app](const crow::Request& req,
14737e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
147422d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
14753ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
147645ca1b86SEd Tanous         {
147745ca1b86SEd Tanous             return;
147845ca1b86SEd Tanous         }
147922d268cbSEd Tanous 
148022d268cbSEd Tanous         if (systemName != "system")
148122d268cbSEd Tanous         {
148222d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
148322d268cbSEd Tanous                                        systemName);
148422d268cbSEd Tanous             return;
148522d268cbSEd Tanous         }
148622d268cbSEd Tanous 
14877e860f15SJohn Edward Broadbent         const std::string& targetID = param;
14888d1b46d7Szhanghch05 
14897e860f15SJohn Edward Broadbent         // Go through the log files and check the unique ID for each
14907e860f15SJohn Edward Broadbent         // entry to find the target entry
1491897967deSJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
1492897967deSJason M. Bills         getRedfishLogFiles(redfishLogFiles);
1493897967deSJason M. Bills         std::string logEntry;
1494897967deSJason M. Bills 
14957e860f15SJohn Edward Broadbent         // Oldest logs are in the last file, so start there and loop
14967e860f15SJohn Edward Broadbent         // backwards
1497002d39b4SEd Tanous         for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1498002d39b4SEd Tanous              it++)
1499897967deSJason M. Bills         {
1500897967deSJason M. Bills             std::ifstream logStream(*it);
1501897967deSJason M. Bills             if (!logStream.is_open())
1502897967deSJason M. Bills             {
1503897967deSJason M. Bills                 continue;
1504897967deSJason M. Bills             }
1505897967deSJason M. Bills 
1506897967deSJason M. Bills             // Reset the unique ID on the first entry
1507897967deSJason M. Bills             bool firstEntry = true;
1508897967deSJason M. Bills             while (std::getline(logStream, logEntry))
1509897967deSJason M. Bills             {
1510897967deSJason M. Bills                 std::string idStr;
1511897967deSJason M. Bills                 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1512897967deSJason M. Bills                 {
1513897967deSJason M. Bills                     continue;
1514897967deSJason M. Bills                 }
1515897967deSJason M. Bills                 firstEntry = false;
1516897967deSJason M. Bills 
1517897967deSJason M. Bills                 if (idStr == targetID)
1518897967deSJason M. Bills                 {
1519de703c5dSJason M. Bills                     nlohmann::json::object_t bmcLogEntry;
1520ac992cdeSJason M. Bills                     LogParseError status =
1521ac992cdeSJason M. Bills                         fillEventLogEntryJson(idStr, logEntry, bmcLogEntry);
1522ac992cdeSJason M. Bills                     if (status != LogParseError::success)
1523897967deSJason M. Bills                     {
1524897967deSJason M. Bills                         messages::internalError(asyncResp->res);
1525897967deSJason M. Bills                         return;
1526897967deSJason M. Bills                     }
1527d405bb51SJason M. Bills                     asyncResp->res.jsonValue.update(bmcLogEntry);
1528897967deSJason M. Bills                     return;
1529897967deSJason M. Bills                 }
1530897967deSJason M. Bills             }
1531897967deSJason M. Bills         }
1532897967deSJason M. Bills         // Requested ID was not found
15339db4ba25SJiaqing Zhao         messages::resourceNotFound(asyncResp->res, "LogEntry", targetID);
15347e860f15SJohn Edward Broadbent         });
153508a4e4b5SAnthony Wilson }
153608a4e4b5SAnthony Wilson 
15377e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntryCollection(App& app)
153808a4e4b5SAnthony Wilson {
153922d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
1540ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
1541002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1542002d39b4SEd Tanous             [&app](const crow::Request& req,
154322d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
154422d268cbSEd Tanous                    const std::string& systemName) {
15453ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
154645ca1b86SEd Tanous         {
154745ca1b86SEd Tanous             return;
154845ca1b86SEd Tanous         }
154922d268cbSEd Tanous         if (systemName != "system")
155022d268cbSEd Tanous         {
155122d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
155222d268cbSEd Tanous                                        systemName);
155322d268cbSEd Tanous             return;
155422d268cbSEd Tanous         }
155522d268cbSEd Tanous 
15567e860f15SJohn Edward Broadbent         // Collections don't include the static data added by SubRoute
15577e860f15SJohn Edward Broadbent         // because it has a duplicate entry for members
155808a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["@odata.type"] =
155908a4e4b5SAnthony Wilson             "#LogEntryCollection.LogEntryCollection";
156008a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["@odata.id"] =
156108a4e4b5SAnthony Wilson             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
156208a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
156308a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["Description"] =
156408a4e4b5SAnthony Wilson             "Collection of System Event Log Entries";
156508a4e4b5SAnthony Wilson 
1566cb92c03bSAndrew Geissler         // DBus implementation of EventLog/Entries
1567cb92c03bSAndrew Geissler         // Make call to Logging Service to find all log entry objects
1568*5eb468daSGeorge Liu         sdbusplus::message::object_path path("/xyz/openbmc_project/logging");
1569*5eb468daSGeorge Liu         dbus::utility::getManagedObjects(
1570*5eb468daSGeorge Liu             "xyz.openbmc_project.Logging", path,
15715e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec,
1572914e2d5dSEd Tanous                         const dbus::utility::ManagedObjectType& resp) {
1573cb92c03bSAndrew Geissler             if (ec)
1574cb92c03bSAndrew Geissler             {
1575cb92c03bSAndrew Geissler                 // TODO Handle for specific error code
1576cb92c03bSAndrew Geissler                 BMCWEB_LOG_ERROR
1577002d39b4SEd Tanous                     << "getLogEntriesIfaceData resp_handler got error " << ec;
1578cb92c03bSAndrew Geissler                 messages::internalError(asyncResp->res);
1579cb92c03bSAndrew Geissler                 return;
1580cb92c03bSAndrew Geissler             }
1581002d39b4SEd Tanous             nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
1582cb92c03bSAndrew Geissler             entriesArray = nlohmann::json::array();
15839eb808c1SEd Tanous             for (const auto& objectPath : resp)
1584cb92c03bSAndrew Geissler             {
1585914e2d5dSEd Tanous                 const uint32_t* id = nullptr;
1586c419c759SEd Tanous                 const uint64_t* timestamp = nullptr;
1587c419c759SEd Tanous                 const uint64_t* updateTimestamp = nullptr;
1588914e2d5dSEd Tanous                 const std::string* severity = nullptr;
1589914e2d5dSEd Tanous                 const std::string* message = nullptr;
1590914e2d5dSEd Tanous                 const std::string* filePath = nullptr;
15919c11a172SVijay Lobo                 const std::string* resolution = nullptr;
159275710de2SXiaochao Ma                 bool resolved = false;
15939017faf2SAbhishek Patel                 const std::string* notify = nullptr;
15949017faf2SAbhishek Patel 
15959eb808c1SEd Tanous                 for (const auto& interfaceMap : objectPath.second)
1596f86bb901SAdriana Kobylak                 {
1597f86bb901SAdriana Kobylak                     if (interfaceMap.first ==
1598f86bb901SAdriana Kobylak                         "xyz.openbmc_project.Logging.Entry")
1599f86bb901SAdriana Kobylak                     {
1600002d39b4SEd Tanous                         for (const auto& propertyMap : interfaceMap.second)
1601cb92c03bSAndrew Geissler                         {
1602cb92c03bSAndrew Geissler                             if (propertyMap.first == "Id")
1603cb92c03bSAndrew Geissler                             {
1604002d39b4SEd Tanous                                 id = std::get_if<uint32_t>(&propertyMap.second);
1605cb92c03bSAndrew Geissler                             }
1606cb92c03bSAndrew Geissler                             else if (propertyMap.first == "Timestamp")
1607cb92c03bSAndrew Geissler                             {
1608002d39b4SEd Tanous                                 timestamp =
1609002d39b4SEd Tanous                                     std::get_if<uint64_t>(&propertyMap.second);
16107e860f15SJohn Edward Broadbent                             }
1611002d39b4SEd Tanous                             else if (propertyMap.first == "UpdateTimestamp")
16127e860f15SJohn Edward Broadbent                             {
1613002d39b4SEd Tanous                                 updateTimestamp =
1614002d39b4SEd Tanous                                     std::get_if<uint64_t>(&propertyMap.second);
16157e860f15SJohn Edward Broadbent                             }
16167e860f15SJohn Edward Broadbent                             else if (propertyMap.first == "Severity")
16177e860f15SJohn Edward Broadbent                             {
16187e860f15SJohn Edward Broadbent                                 severity = std::get_if<std::string>(
16197e860f15SJohn Edward Broadbent                                     &propertyMap.second);
16207e860f15SJohn Edward Broadbent                             }
16219c11a172SVijay Lobo                             else if (propertyMap.first == "Resolution")
16229c11a172SVijay Lobo                             {
16239c11a172SVijay Lobo                                 resolution = std::get_if<std::string>(
16249c11a172SVijay Lobo                                     &propertyMap.second);
16259c11a172SVijay Lobo                             }
16267e860f15SJohn Edward Broadbent                             else if (propertyMap.first == "Message")
16277e860f15SJohn Edward Broadbent                             {
16287e860f15SJohn Edward Broadbent                                 message = std::get_if<std::string>(
16297e860f15SJohn Edward Broadbent                                     &propertyMap.second);
16307e860f15SJohn Edward Broadbent                             }
16317e860f15SJohn Edward Broadbent                             else if (propertyMap.first == "Resolved")
16327e860f15SJohn Edward Broadbent                             {
1633914e2d5dSEd Tanous                                 const bool* resolveptr =
1634002d39b4SEd Tanous                                     std::get_if<bool>(&propertyMap.second);
16357e860f15SJohn Edward Broadbent                                 if (resolveptr == nullptr)
16367e860f15SJohn Edward Broadbent                                 {
1637002d39b4SEd Tanous                                     messages::internalError(asyncResp->res);
16387e860f15SJohn Edward Broadbent                                     return;
16397e860f15SJohn Edward Broadbent                                 }
16407e860f15SJohn Edward Broadbent                                 resolved = *resolveptr;
16417e860f15SJohn Edward Broadbent                             }
16429017faf2SAbhishek Patel                             else if (propertyMap.first ==
16439017faf2SAbhishek Patel                                      "ServiceProviderNotify")
16449017faf2SAbhishek Patel                             {
16459017faf2SAbhishek Patel                                 notify = std::get_if<std::string>(
16469017faf2SAbhishek Patel                                     &propertyMap.second);
16479017faf2SAbhishek Patel                                 if (notify == nullptr)
16489017faf2SAbhishek Patel                                 {
16499017faf2SAbhishek Patel                                     messages::internalError(asyncResp->res);
16509017faf2SAbhishek Patel                                     return;
16519017faf2SAbhishek Patel                                 }
16529017faf2SAbhishek Patel                             }
16537e860f15SJohn Edward Broadbent                         }
16547e860f15SJohn Edward Broadbent                         if (id == nullptr || message == nullptr ||
16557e860f15SJohn Edward Broadbent                             severity == nullptr)
16567e860f15SJohn Edward Broadbent                         {
16577e860f15SJohn Edward Broadbent                             messages::internalError(asyncResp->res);
16587e860f15SJohn Edward Broadbent                             return;
16597e860f15SJohn Edward Broadbent                         }
16607e860f15SJohn Edward Broadbent                     }
16617e860f15SJohn Edward Broadbent                     else if (interfaceMap.first ==
16627e860f15SJohn Edward Broadbent                              "xyz.openbmc_project.Common.FilePath")
16637e860f15SJohn Edward Broadbent                     {
1664002d39b4SEd Tanous                         for (const auto& propertyMap : interfaceMap.second)
16657e860f15SJohn Edward Broadbent                         {
16667e860f15SJohn Edward Broadbent                             if (propertyMap.first == "Path")
16677e860f15SJohn Edward Broadbent                             {
16687e860f15SJohn Edward Broadbent                                 filePath = std::get_if<std::string>(
16697e860f15SJohn Edward Broadbent                                     &propertyMap.second);
16707e860f15SJohn Edward Broadbent                             }
16717e860f15SJohn Edward Broadbent                         }
16727e860f15SJohn Edward Broadbent                     }
16737e860f15SJohn Edward Broadbent                 }
16747e860f15SJohn Edward Broadbent                 // Object path without the
16757e860f15SJohn Edward Broadbent                 // xyz.openbmc_project.Logging.Entry interface, ignore
16767e860f15SJohn Edward Broadbent                 // and continue.
16777e860f15SJohn Edward Broadbent                 if (id == nullptr || message == nullptr ||
1678c419c759SEd Tanous                     severity == nullptr || timestamp == nullptr ||
1679c419c759SEd Tanous                     updateTimestamp == nullptr)
16807e860f15SJohn Edward Broadbent                 {
16817e860f15SJohn Edward Broadbent                     continue;
16827e860f15SJohn Edward Broadbent                 }
16837e860f15SJohn Edward Broadbent                 entriesArray.push_back({});
16847e860f15SJohn Edward Broadbent                 nlohmann::json& thisEntry = entriesArray.back();
16859c11a172SVijay Lobo                 thisEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
1686ef4c65b7SEd Tanous                 thisEntry["@odata.id"] = boost::urls::format(
1687ef4c65b7SEd Tanous                     "/redfish/v1/Systems/system/LogServices/EventLog/Entries/{}",
1688ef4c65b7SEd Tanous                     std::to_string(*id));
16897e860f15SJohn Edward Broadbent                 thisEntry["Name"] = "System Event Log Entry";
16907e860f15SJohn Edward Broadbent                 thisEntry["Id"] = std::to_string(*id);
16917e860f15SJohn Edward Broadbent                 thisEntry["Message"] = *message;
16927e860f15SJohn Edward Broadbent                 thisEntry["Resolved"] = resolved;
16939c11a172SVijay Lobo                 if ((resolution != nullptr) && (!(*resolution).empty()))
16949c11a172SVijay Lobo                 {
16959c11a172SVijay Lobo                     thisEntry["Resolution"] = *resolution;
16969c11a172SVijay Lobo                 }
16979017faf2SAbhishek Patel                 std::optional<bool> notifyAction =
16989017faf2SAbhishek Patel                     getProviderNotifyAction(*notify);
16999017faf2SAbhishek Patel                 if (notifyAction)
17009017faf2SAbhishek Patel                 {
17019017faf2SAbhishek Patel                     thisEntry["ServiceProviderNotified"] = *notifyAction;
17029017faf2SAbhishek Patel                 }
17037e860f15SJohn Edward Broadbent                 thisEntry["EntryType"] = "Event";
17047e860f15SJohn Edward Broadbent                 thisEntry["Severity"] =
17057e860f15SJohn Edward Broadbent                     translateSeverityDbusToRedfish(*severity);
17067e860f15SJohn Edward Broadbent                 thisEntry["Created"] =
17072b82937eSEd Tanous                     redfish::time_utils::getDateTimeUintMs(*timestamp);
17087e860f15SJohn Edward Broadbent                 thisEntry["Modified"] =
17092b82937eSEd Tanous                     redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
17107e860f15SJohn Edward Broadbent                 if (filePath != nullptr)
17117e860f15SJohn Edward Broadbent                 {
17127e860f15SJohn Edward Broadbent                     thisEntry["AdditionalDataURI"] =
17130fda0f12SGeorge Liu                         "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
17147e860f15SJohn Edward Broadbent                         std::to_string(*id) + "/attachment";
17157e860f15SJohn Edward Broadbent                 }
17167e860f15SJohn Edward Broadbent             }
1717002d39b4SEd Tanous             std::sort(
1718002d39b4SEd Tanous                 entriesArray.begin(), entriesArray.end(),
1719002d39b4SEd Tanous                 [](const nlohmann::json& left, const nlohmann::json& right) {
17207e860f15SJohn Edward Broadbent                 return (left["Id"] <= right["Id"]);
17217e860f15SJohn Edward Broadbent                 });
17227e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Members@odata.count"] =
17237e860f15SJohn Edward Broadbent                 entriesArray.size();
1724*5eb468daSGeorge Liu             });
17257e860f15SJohn Edward Broadbent         });
17267e860f15SJohn Edward Broadbent }
17277e860f15SJohn Edward Broadbent 
17287e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntry(App& app)
17297e860f15SJohn Edward Broadbent {
17307e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
173122d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
1732ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
1733002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1734002d39b4SEd Tanous             [&app](const crow::Request& req,
17357e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
173622d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
17373ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
17387e860f15SJohn Edward Broadbent         {
173945ca1b86SEd Tanous             return;
174045ca1b86SEd Tanous         }
174122d268cbSEd Tanous         if (systemName != "system")
174222d268cbSEd Tanous         {
174322d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
174422d268cbSEd Tanous                                        systemName);
174522d268cbSEd Tanous             return;
174622d268cbSEd Tanous         }
174722d268cbSEd Tanous 
17487e860f15SJohn Edward Broadbent         std::string entryID = param;
17497e860f15SJohn Edward Broadbent         dbus::utility::escapePathForDbus(entryID);
17507e860f15SJohn Edward Broadbent 
17517e860f15SJohn Edward Broadbent         // DBus implementation of EventLog/Entries
17527e860f15SJohn Edward Broadbent         // Make call to Logging Service to find all log entry objects
1753d1bde9e5SKrzysztof Grobelny         sdbusplus::asio::getAllProperties(
1754d1bde9e5SKrzysztof Grobelny             *crow::connections::systemBus, "xyz.openbmc_project.Logging",
1755d1bde9e5SKrzysztof Grobelny             "/xyz/openbmc_project/logging/entry/" + entryID, "",
17565e7e2dc5SEd Tanous             [asyncResp, entryID](const boost::system::error_code& ec,
1757b9d36b47SEd Tanous                                  const dbus::utility::DBusPropertiesMap& resp) {
17587e860f15SJohn Edward Broadbent             if (ec.value() == EBADR)
17597e860f15SJohn Edward Broadbent             {
1760d1bde9e5SKrzysztof Grobelny                 messages::resourceNotFound(asyncResp->res, "EventLogEntry",
1761d1bde9e5SKrzysztof Grobelny                                            entryID);
17627e860f15SJohn Edward Broadbent                 return;
17637e860f15SJohn Edward Broadbent             }
17647e860f15SJohn Edward Broadbent             if (ec)
17657e860f15SJohn Edward Broadbent             {
17660fda0f12SGeorge Liu                 BMCWEB_LOG_ERROR
1767002d39b4SEd Tanous                     << "EventLogEntry (DBus) resp_handler got error " << ec;
17687e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
17697e860f15SJohn Edward Broadbent                 return;
17707e860f15SJohn Edward Broadbent             }
1771914e2d5dSEd Tanous             const uint32_t* id = nullptr;
1772c419c759SEd Tanous             const uint64_t* timestamp = nullptr;
1773c419c759SEd Tanous             const uint64_t* updateTimestamp = nullptr;
1774914e2d5dSEd Tanous             const std::string* severity = nullptr;
1775914e2d5dSEd Tanous             const std::string* message = nullptr;
1776914e2d5dSEd Tanous             const std::string* filePath = nullptr;
17779c11a172SVijay Lobo             const std::string* resolution = nullptr;
17787e860f15SJohn Edward Broadbent             bool resolved = false;
17799017faf2SAbhishek Patel             const std::string* notify = nullptr;
17807e860f15SJohn Edward Broadbent 
1781d1bde9e5SKrzysztof Grobelny             const bool success = sdbusplus::unpackPropertiesNoThrow(
1782d1bde9e5SKrzysztof Grobelny                 dbus_utils::UnpackErrorPrinter(), resp, "Id", id, "Timestamp",
1783d1bde9e5SKrzysztof Grobelny                 timestamp, "UpdateTimestamp", updateTimestamp, "Severity",
17849c11a172SVijay Lobo                 severity, "Message", message, "Resolved", resolved,
17859017faf2SAbhishek Patel                 "Resolution", resolution, "Path", filePath,
17869017faf2SAbhishek Patel                 "ServiceProviderNotify", notify);
1787d1bde9e5SKrzysztof Grobelny 
1788d1bde9e5SKrzysztof Grobelny             if (!success)
178975710de2SXiaochao Ma             {
179075710de2SXiaochao Ma                 messages::internalError(asyncResp->res);
179175710de2SXiaochao Ma                 return;
179275710de2SXiaochao Ma             }
1793d1bde9e5SKrzysztof Grobelny 
1794002d39b4SEd Tanous             if (id == nullptr || message == nullptr || severity == nullptr ||
17959017faf2SAbhishek Patel                 timestamp == nullptr || updateTimestamp == nullptr ||
17969017faf2SAbhishek Patel                 notify == nullptr)
1797f86bb901SAdriana Kobylak             {
1798ae34c8e8SAdriana Kobylak                 messages::internalError(asyncResp->res);
1799271584abSEd Tanous                 return;
1800271584abSEd Tanous             }
18019017faf2SAbhishek Patel 
1802f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["@odata.type"] =
18039c11a172SVijay Lobo                 "#LogEntry.v1_9_0.LogEntry";
1804ef4c65b7SEd Tanous             asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
1805ef4c65b7SEd Tanous                 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/{}",
1806ef4c65b7SEd Tanous                 std::to_string(*id));
180745ca1b86SEd Tanous             asyncResp->res.jsonValue["Name"] = "System Event Log Entry";
1808f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1809f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Message"] = *message;
1810f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Resolved"] = resolved;
18119017faf2SAbhishek Patel             std::optional<bool> notifyAction = getProviderNotifyAction(*notify);
18129017faf2SAbhishek Patel             if (notifyAction)
18139017faf2SAbhishek Patel             {
18149017faf2SAbhishek Patel                 asyncResp->res.jsonValue["ServiceProviderNotified"] =
18159017faf2SAbhishek Patel                     *notifyAction;
18169017faf2SAbhishek Patel             }
18179c11a172SVijay Lobo             if ((resolution != nullptr) && (!(*resolution).empty()))
18189c11a172SVijay Lobo             {
18199c11a172SVijay Lobo                 asyncResp->res.jsonValue["Resolution"] = *resolution;
18209c11a172SVijay Lobo             }
1821f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["EntryType"] = "Event";
1822f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Severity"] =
1823f86bb901SAdriana Kobylak                 translateSeverityDbusToRedfish(*severity);
1824f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Created"] =
18252b82937eSEd Tanous                 redfish::time_utils::getDateTimeUintMs(*timestamp);
1826f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Modified"] =
18272b82937eSEd Tanous                 redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
1828f86bb901SAdriana Kobylak             if (filePath != nullptr)
1829f86bb901SAdriana Kobylak             {
1830f86bb901SAdriana Kobylak                 asyncResp->res.jsonValue["AdditionalDataURI"] =
1831e7dbd530SPotin Lai                     "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1832e7dbd530SPotin Lai                     std::to_string(*id) + "/attachment";
1833f86bb901SAdriana Kobylak             }
1834d1bde9e5SKrzysztof Grobelny             });
18357e860f15SJohn Edward Broadbent         });
1836336e96c6SChicago Duan 
18377e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
183822d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
1839ed398213SEd Tanous         .privileges(redfish::privileges::patchLogEntry)
18407e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::patch)(
184145ca1b86SEd Tanous             [&app](const crow::Request& req,
18427e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
184322d268cbSEd Tanous                    const std::string& systemName, const std::string& entryId) {
18443ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
184545ca1b86SEd Tanous         {
184645ca1b86SEd Tanous             return;
184745ca1b86SEd Tanous         }
184822d268cbSEd Tanous         if (systemName != "system")
184922d268cbSEd Tanous         {
185022d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
185122d268cbSEd Tanous                                        systemName);
185222d268cbSEd Tanous             return;
185322d268cbSEd Tanous         }
185475710de2SXiaochao Ma         std::optional<bool> resolved;
185575710de2SXiaochao Ma 
185615ed6780SWilly Tu         if (!json_util::readJsonPatch(req, asyncResp->res, "Resolved",
18577e860f15SJohn Edward Broadbent                                       resolved))
185875710de2SXiaochao Ma         {
185975710de2SXiaochao Ma             return;
186075710de2SXiaochao Ma         }
186175710de2SXiaochao Ma         BMCWEB_LOG_DEBUG << "Set Resolved";
186275710de2SXiaochao Ma 
186375710de2SXiaochao Ma         crow::connections::systemBus->async_method_call(
18645e7e2dc5SEd Tanous             [asyncResp, entryId](const boost::system::error_code& ec) {
186575710de2SXiaochao Ma             if (ec)
186675710de2SXiaochao Ma             {
186775710de2SXiaochao Ma                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
186875710de2SXiaochao Ma                 messages::internalError(asyncResp->res);
186975710de2SXiaochao Ma                 return;
187075710de2SXiaochao Ma             }
187175710de2SXiaochao Ma             },
187275710de2SXiaochao Ma             "xyz.openbmc_project.Logging",
187375710de2SXiaochao Ma             "/xyz/openbmc_project/logging/entry/" + entryId,
187475710de2SXiaochao Ma             "org.freedesktop.DBus.Properties", "Set",
187575710de2SXiaochao Ma             "xyz.openbmc_project.Logging.Entry", "Resolved",
1876168e20c1SEd Tanous             dbus::utility::DbusVariantType(*resolved));
18777e860f15SJohn Edward Broadbent         });
187875710de2SXiaochao Ma 
18797e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
188022d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
1881ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
1882ed398213SEd Tanous 
1883002d39b4SEd Tanous         .methods(boost::beast::http::verb::delete_)(
1884002d39b4SEd Tanous             [&app](const crow::Request& req,
1885002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
188622d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
18873ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1888336e96c6SChicago Duan         {
188945ca1b86SEd Tanous             return;
189045ca1b86SEd Tanous         }
189122d268cbSEd Tanous         if (systemName != "system")
189222d268cbSEd Tanous         {
189322d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
189422d268cbSEd Tanous                                        systemName);
189522d268cbSEd Tanous             return;
189622d268cbSEd Tanous         }
1897336e96c6SChicago Duan         BMCWEB_LOG_DEBUG << "Do delete single event entries.";
1898336e96c6SChicago Duan 
18997e860f15SJohn Edward Broadbent         std::string entryID = param;
1900336e96c6SChicago Duan 
1901336e96c6SChicago Duan         dbus::utility::escapePathForDbus(entryID);
1902336e96c6SChicago Duan 
1903336e96c6SChicago Duan         // Process response from Logging service.
1904002d39b4SEd Tanous         auto respHandler =
19055e7e2dc5SEd Tanous             [asyncResp, entryID](const boost::system::error_code& ec) {
1906002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "EventLogEntry (DBus) doDelete callback: Done";
1907336e96c6SChicago Duan             if (ec)
1908336e96c6SChicago Duan             {
19093de8d8baSGeorge Liu                 if (ec.value() == EBADR)
19103de8d8baSGeorge Liu                 {
191145ca1b86SEd Tanous                     messages::resourceNotFound(asyncResp->res, "LogEntry",
191245ca1b86SEd Tanous                                                entryID);
19133de8d8baSGeorge Liu                     return;
19143de8d8baSGeorge Liu                 }
1915336e96c6SChicago Duan                 // TODO Handle for specific error code
19160fda0f12SGeorge Liu                 BMCWEB_LOG_ERROR
19170fda0f12SGeorge Liu                     << "EventLogEntry (DBus) doDelete respHandler got error "
1918336e96c6SChicago Duan                     << ec;
1919336e96c6SChicago Duan                 asyncResp->res.result(
1920336e96c6SChicago Duan                     boost::beast::http::status::internal_server_error);
1921336e96c6SChicago Duan                 return;
1922336e96c6SChicago Duan             }
1923336e96c6SChicago Duan 
1924336e96c6SChicago Duan             asyncResp->res.result(boost::beast::http::status::ok);
1925336e96c6SChicago Duan         };
1926336e96c6SChicago Duan 
1927336e96c6SChicago Duan         // Make call to Logging service to request Delete Log
1928336e96c6SChicago Duan         crow::connections::systemBus->async_method_call(
1929336e96c6SChicago Duan             respHandler, "xyz.openbmc_project.Logging",
1930336e96c6SChicago Duan             "/xyz/openbmc_project/logging/entry/" + entryID,
1931336e96c6SChicago Duan             "xyz.openbmc_project.Object.Delete", "Delete");
19327e860f15SJohn Edward Broadbent         });
1933400fd1fbSAdriana Kobylak }
1934400fd1fbSAdriana Kobylak 
19357e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntryDownload(App& app)
1936400fd1fbSAdriana Kobylak {
19370fda0f12SGeorge Liu     BMCWEB_ROUTE(
19380fda0f12SGeorge Liu         app,
193922d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/attachment")
1940ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
19417e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
194245ca1b86SEd Tanous             [&app](const crow::Request& req,
19437e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
194422d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
19453ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
19467e860f15SJohn Edward Broadbent         {
194745ca1b86SEd Tanous             return;
194845ca1b86SEd Tanous         }
194972e21377SMatt Spinler         if (!http_helpers::isContentTypeAllowed(
195099351cd8SEd Tanous                 req.getHeaderValue("Accept"),
19514a0e1a0cSEd Tanous                 http_helpers::ContentType::OctetStream, true))
1952400fd1fbSAdriana Kobylak         {
1953002d39b4SEd Tanous             asyncResp->res.result(boost::beast::http::status::bad_request);
1954400fd1fbSAdriana Kobylak             return;
1955400fd1fbSAdriana Kobylak         }
195622d268cbSEd Tanous         if (systemName != "system")
195722d268cbSEd Tanous         {
195822d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
195922d268cbSEd Tanous                                        systemName);
196022d268cbSEd Tanous             return;
196122d268cbSEd Tanous         }
1962400fd1fbSAdriana Kobylak 
19637e860f15SJohn Edward Broadbent         std::string entryID = param;
1964400fd1fbSAdriana Kobylak         dbus::utility::escapePathForDbus(entryID);
1965400fd1fbSAdriana Kobylak 
1966400fd1fbSAdriana Kobylak         crow::connections::systemBus->async_method_call(
19675e7e2dc5SEd Tanous             [asyncResp, entryID](const boost::system::error_code& ec,
1968400fd1fbSAdriana Kobylak                                  const sdbusplus::message::unix_fd& unixfd) {
1969400fd1fbSAdriana Kobylak             if (ec.value() == EBADR)
1970400fd1fbSAdriana Kobylak             {
1971002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "EventLogAttachment",
1972002d39b4SEd Tanous                                            entryID);
1973400fd1fbSAdriana Kobylak                 return;
1974400fd1fbSAdriana Kobylak             }
1975400fd1fbSAdriana Kobylak             if (ec)
1976400fd1fbSAdriana Kobylak             {
1977400fd1fbSAdriana Kobylak                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1978400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1979400fd1fbSAdriana Kobylak                 return;
1980400fd1fbSAdriana Kobylak             }
1981400fd1fbSAdriana Kobylak 
1982400fd1fbSAdriana Kobylak             int fd = -1;
1983400fd1fbSAdriana Kobylak             fd = dup(unixfd);
1984400fd1fbSAdriana Kobylak             if (fd == -1)
1985400fd1fbSAdriana Kobylak             {
1986400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1987400fd1fbSAdriana Kobylak                 return;
1988400fd1fbSAdriana Kobylak             }
1989400fd1fbSAdriana Kobylak 
1990400fd1fbSAdriana Kobylak             long long int size = lseek(fd, 0, SEEK_END);
1991400fd1fbSAdriana Kobylak             if (size == -1)
1992400fd1fbSAdriana Kobylak             {
1993400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1994400fd1fbSAdriana Kobylak                 return;
1995400fd1fbSAdriana Kobylak             }
1996400fd1fbSAdriana Kobylak 
1997400fd1fbSAdriana Kobylak             // Arbitrary max size of 64kb
1998400fd1fbSAdriana Kobylak             constexpr int maxFileSize = 65536;
1999400fd1fbSAdriana Kobylak             if (size > maxFileSize)
2000400fd1fbSAdriana Kobylak             {
2001002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "File size exceeds maximum allowed size of "
2002400fd1fbSAdriana Kobylak                                  << maxFileSize;
2003400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
2004400fd1fbSAdriana Kobylak                 return;
2005400fd1fbSAdriana Kobylak             }
2006400fd1fbSAdriana Kobylak             std::vector<char> data(static_cast<size_t>(size));
2007400fd1fbSAdriana Kobylak             long long int rc = lseek(fd, 0, SEEK_SET);
2008400fd1fbSAdriana Kobylak             if (rc == -1)
2009400fd1fbSAdriana Kobylak             {
2010400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
2011400fd1fbSAdriana Kobylak                 return;
2012400fd1fbSAdriana Kobylak             }
2013400fd1fbSAdriana Kobylak             rc = read(fd, data.data(), data.size());
2014400fd1fbSAdriana Kobylak             if ((rc == -1) || (rc != size))
2015400fd1fbSAdriana Kobylak             {
2016400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
2017400fd1fbSAdriana Kobylak                 return;
2018400fd1fbSAdriana Kobylak             }
2019400fd1fbSAdriana Kobylak             close(fd);
2020400fd1fbSAdriana Kobylak 
2021400fd1fbSAdriana Kobylak             std::string_view strData(data.data(), data.size());
2022002d39b4SEd Tanous             std::string output = crow::utility::base64encode(strData);
2023400fd1fbSAdriana Kobylak 
2024d9f6c621SEd Tanous             asyncResp->res.addHeader(boost::beast::http::field::content_type,
2025400fd1fbSAdriana Kobylak                                      "application/octet-stream");
2026d9f6c621SEd Tanous             asyncResp->res.addHeader(
2027d9f6c621SEd Tanous                 boost::beast::http::field::content_transfer_encoding, "Base64");
2028400fd1fbSAdriana Kobylak             asyncResp->res.body() = std::move(output);
2029400fd1fbSAdriana Kobylak             },
2030400fd1fbSAdriana Kobylak             "xyz.openbmc_project.Logging",
2031400fd1fbSAdriana Kobylak             "/xyz/openbmc_project/logging/entry/" + entryID,
2032400fd1fbSAdriana Kobylak             "xyz.openbmc_project.Logging.Entry", "GetEntry");
20337e860f15SJohn Edward Broadbent         });
20341da66f75SEd Tanous }
20351da66f75SEd Tanous 
2036b7028ebfSSpencer Ku constexpr const char* hostLoggerFolderPath = "/var/log/console";
2037b7028ebfSSpencer Ku 
2038b7028ebfSSpencer Ku inline bool
2039b7028ebfSSpencer Ku     getHostLoggerFiles(const std::string& hostLoggerFilePath,
2040b7028ebfSSpencer Ku                        std::vector<std::filesystem::path>& hostLoggerFiles)
2041b7028ebfSSpencer Ku {
2042b7028ebfSSpencer Ku     std::error_code ec;
2043b7028ebfSSpencer Ku     std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec);
2044b7028ebfSSpencer Ku     if (ec)
2045b7028ebfSSpencer Ku     {
2046b7028ebfSSpencer Ku         BMCWEB_LOG_ERROR << ec.message();
2047b7028ebfSSpencer Ku         return false;
2048b7028ebfSSpencer Ku     }
2049b7028ebfSSpencer Ku     for (const std::filesystem::directory_entry& it : logPath)
2050b7028ebfSSpencer Ku     {
2051b7028ebfSSpencer Ku         std::string filename = it.path().filename();
2052b7028ebfSSpencer Ku         // Prefix of each log files is "log". Find the file and save the
2053b7028ebfSSpencer Ku         // path
205411ba3979SEd Tanous         if (filename.starts_with("log"))
2055b7028ebfSSpencer Ku         {
2056b7028ebfSSpencer Ku             hostLoggerFiles.emplace_back(it.path());
2057b7028ebfSSpencer Ku         }
2058b7028ebfSSpencer Ku     }
2059b7028ebfSSpencer Ku     // As the log files rotate, they are appended with a ".#" that is higher for
2060b7028ebfSSpencer Ku     // the older logs. Since we start from oldest logs, sort the name in
2061b7028ebfSSpencer Ku     // descending order.
2062b7028ebfSSpencer Ku     std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(),
2063b7028ebfSSpencer Ku               AlphanumLess<std::string>());
2064b7028ebfSSpencer Ku 
2065b7028ebfSSpencer Ku     return true;
2066b7028ebfSSpencer Ku }
2067b7028ebfSSpencer Ku 
206802cad96eSEd Tanous inline bool getHostLoggerEntries(
206902cad96eSEd Tanous     const std::vector<std::filesystem::path>& hostLoggerFiles, uint64_t skip,
207002cad96eSEd Tanous     uint64_t top, std::vector<std::string>& logEntries, size_t& logCount)
2071b7028ebfSSpencer Ku {
2072b7028ebfSSpencer Ku     GzFileReader logFile;
2073b7028ebfSSpencer Ku 
2074b7028ebfSSpencer Ku     // Go though all log files and expose host logs.
2075b7028ebfSSpencer Ku     for (const std::filesystem::path& it : hostLoggerFiles)
2076b7028ebfSSpencer Ku     {
2077b7028ebfSSpencer Ku         if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount))
2078b7028ebfSSpencer Ku         {
2079b7028ebfSSpencer Ku             BMCWEB_LOG_ERROR << "fail to expose host logs";
2080b7028ebfSSpencer Ku             return false;
2081b7028ebfSSpencer Ku         }
2082b7028ebfSSpencer Ku     }
2083b7028ebfSSpencer Ku     // Get lastMessage from constructor by getter
2084b7028ebfSSpencer Ku     std::string lastMessage = logFile.getLastMessage();
2085b7028ebfSSpencer Ku     if (!lastMessage.empty())
2086b7028ebfSSpencer Ku     {
2087b7028ebfSSpencer Ku         logCount++;
2088b7028ebfSSpencer Ku         if (logCount > skip && logCount <= (skip + top))
2089b7028ebfSSpencer Ku         {
2090b7028ebfSSpencer Ku             logEntries.push_back(lastMessage);
2091b7028ebfSSpencer Ku         }
2092b7028ebfSSpencer Ku     }
2093b7028ebfSSpencer Ku     return true;
2094b7028ebfSSpencer Ku }
2095b7028ebfSSpencer Ku 
2096b7028ebfSSpencer Ku inline void fillHostLoggerEntryJson(const std::string& logEntryID,
2097b7028ebfSSpencer Ku                                     const std::string& msg,
20986d6574c9SJason M. Bills                                     nlohmann::json::object_t& logEntryJson)
2099b7028ebfSSpencer Ku {
2100b7028ebfSSpencer Ku     // Fill in the log entry with the gathered data.
21019c11a172SVijay Lobo     logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
2102ef4c65b7SEd Tanous     logEntryJson["@odata.id"] = boost::urls::format(
2103ef4c65b7SEd Tanous         "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/{}",
2104ef4c65b7SEd Tanous         logEntryID);
21056d6574c9SJason M. Bills     logEntryJson["Name"] = "Host Logger Entry";
21066d6574c9SJason M. Bills     logEntryJson["Id"] = logEntryID;
21076d6574c9SJason M. Bills     logEntryJson["Message"] = msg;
21086d6574c9SJason M. Bills     logEntryJson["EntryType"] = "Oem";
21096d6574c9SJason M. Bills     logEntryJson["Severity"] = "OK";
21106d6574c9SJason M. Bills     logEntryJson["OemRecordFormat"] = "Host Logger Entry";
2111b7028ebfSSpencer Ku }
2112b7028ebfSSpencer Ku 
2113b7028ebfSSpencer Ku inline void requestRoutesSystemHostLogger(App& app)
2114b7028ebfSSpencer Ku {
211522d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/")
2116b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogService)
21171476687dSEd Tanous         .methods(boost::beast::http::verb::get)(
21181476687dSEd Tanous             [&app](const crow::Request& req,
211922d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
212022d268cbSEd Tanous                    const std::string& systemName) {
21213ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
212245ca1b86SEd Tanous         {
212345ca1b86SEd Tanous             return;
212445ca1b86SEd Tanous         }
212522d268cbSEd Tanous         if (systemName != "system")
212622d268cbSEd Tanous         {
212722d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
212822d268cbSEd Tanous                                        systemName);
212922d268cbSEd Tanous             return;
213022d268cbSEd Tanous         }
2131b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.id"] =
2132b7028ebfSSpencer Ku             "/redfish/v1/Systems/system/LogServices/HostLogger";
2133b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.type"] =
2134b7028ebfSSpencer Ku             "#LogService.v1_1_0.LogService";
2135b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Name"] = "Host Logger Service";
2136b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Description"] = "Host Logger Service";
2137b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Id"] = "HostLogger";
21381476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
21391476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
2140b7028ebfSSpencer Ku         });
2141b7028ebfSSpencer Ku }
2142b7028ebfSSpencer Ku 
2143b7028ebfSSpencer Ku inline void requestRoutesSystemHostLoggerCollection(App& app)
2144b7028ebfSSpencer Ku {
2145b7028ebfSSpencer Ku     BMCWEB_ROUTE(app,
214622d268cbSEd Tanous                  "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/")
2147b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogEntry)
2148002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2149002d39b4SEd Tanous             [&app](const crow::Request& req,
215022d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
215122d268cbSEd Tanous                    const std::string& systemName) {
2152c937d2bfSEd Tanous         query_param::QueryCapabilities capabilities = {
2153c937d2bfSEd Tanous             .canDelegateTop = true,
2154c937d2bfSEd Tanous             .canDelegateSkip = true,
2155c937d2bfSEd Tanous         };
2156c937d2bfSEd Tanous         query_param::Query delegatedQuery;
2157c937d2bfSEd Tanous         if (!redfish::setUpRedfishRouteWithDelegation(
21583ba00073SCarson Labrado                 app, req, asyncResp, delegatedQuery, capabilities))
2159b7028ebfSSpencer Ku         {
2160b7028ebfSSpencer Ku             return;
2161b7028ebfSSpencer Ku         }
216222d268cbSEd Tanous         if (systemName != "system")
216322d268cbSEd Tanous         {
216422d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
216522d268cbSEd Tanous                                        systemName);
216622d268cbSEd Tanous             return;
216722d268cbSEd Tanous         }
2168b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.id"] =
2169b7028ebfSSpencer Ku             "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
2170b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.type"] =
2171b7028ebfSSpencer Ku             "#LogEntryCollection.LogEntryCollection";
2172b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Name"] = "HostLogger Entries";
2173b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Description"] =
2174b7028ebfSSpencer Ku             "Collection of HostLogger Entries";
21750fda0f12SGeorge Liu         nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2176b7028ebfSSpencer Ku         logEntryArray = nlohmann::json::array();
2177b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Members@odata.count"] = 0;
2178b7028ebfSSpencer Ku 
2179b7028ebfSSpencer Ku         std::vector<std::filesystem::path> hostLoggerFiles;
2180b7028ebfSSpencer Ku         if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2181b7028ebfSSpencer Ku         {
2182b7028ebfSSpencer Ku             BMCWEB_LOG_ERROR << "fail to get host log file path";
2183b7028ebfSSpencer Ku             return;
2184b7028ebfSSpencer Ku         }
21853648c8beSEd Tanous         // If we weren't provided top and skip limits, use the defaults.
21863648c8beSEd Tanous         size_t skip = delegatedQuery.skip.value_or(0);
21875143f7a5SJiaqing Zhao         size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
2188b7028ebfSSpencer Ku         size_t logCount = 0;
2189b7028ebfSSpencer Ku         // This vector only store the entries we want to expose that
2190b7028ebfSSpencer Ku         // control by skip and top.
2191b7028ebfSSpencer Ku         std::vector<std::string> logEntries;
21923648c8beSEd Tanous         if (!getHostLoggerEntries(hostLoggerFiles, skip, top, logEntries,
21933648c8beSEd Tanous                                   logCount))
2194b7028ebfSSpencer Ku         {
2195b7028ebfSSpencer Ku             messages::internalError(asyncResp->res);
2196b7028ebfSSpencer Ku             return;
2197b7028ebfSSpencer Ku         }
2198b7028ebfSSpencer Ku         // If vector is empty, that means skip value larger than total
2199b7028ebfSSpencer Ku         // log count
220026f6976fSEd Tanous         if (logEntries.empty())
2201b7028ebfSSpencer Ku         {
2202b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Members@odata.count"] = logCount;
2203b7028ebfSSpencer Ku             return;
2204b7028ebfSSpencer Ku         }
220526f6976fSEd Tanous         if (!logEntries.empty())
2206b7028ebfSSpencer Ku         {
2207b7028ebfSSpencer Ku             for (size_t i = 0; i < logEntries.size(); i++)
2208b7028ebfSSpencer Ku             {
22096d6574c9SJason M. Bills                 nlohmann::json::object_t hostLogEntry;
22103648c8beSEd Tanous                 fillHostLoggerEntryJson(std::to_string(skip + i), logEntries[i],
22113648c8beSEd Tanous                                         hostLogEntry);
2212b2ba3072SPatrick Williams                 logEntryArray.emplace_back(std::move(hostLogEntry));
2213b7028ebfSSpencer Ku             }
2214b7028ebfSSpencer Ku 
2215b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Members@odata.count"] = logCount;
22163648c8beSEd Tanous             if (skip + top < logCount)
2217b7028ebfSSpencer Ku             {
2218b7028ebfSSpencer Ku                 asyncResp->res.jsonValue["Members@odata.nextLink"] =
22190fda0f12SGeorge Liu                     "/redfish/v1/Systems/system/LogServices/HostLogger/Entries?$skip=" +
22203648c8beSEd Tanous                     std::to_string(skip + top);
2221b7028ebfSSpencer Ku             }
2222b7028ebfSSpencer Ku         }
2223b7028ebfSSpencer Ku         });
2224b7028ebfSSpencer Ku }
2225b7028ebfSSpencer Ku 
2226b7028ebfSSpencer Ku inline void requestRoutesSystemHostLoggerLogEntry(App& app)
2227b7028ebfSSpencer Ku {
2228b7028ebfSSpencer Ku     BMCWEB_ROUTE(
222922d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/<str>/")
2230b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogEntry)
2231b7028ebfSSpencer Ku         .methods(boost::beast::http::verb::get)(
223245ca1b86SEd Tanous             [&app](const crow::Request& req,
2233b7028ebfSSpencer Ku                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
223422d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
22353ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
223645ca1b86SEd Tanous         {
223745ca1b86SEd Tanous             return;
223845ca1b86SEd Tanous         }
223922d268cbSEd Tanous         if (systemName != "system")
224022d268cbSEd Tanous         {
224122d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
224222d268cbSEd Tanous                                        systemName);
224322d268cbSEd Tanous             return;
224422d268cbSEd Tanous         }
2245b7028ebfSSpencer Ku         const std::string& targetID = param;
2246b7028ebfSSpencer Ku 
2247b7028ebfSSpencer Ku         uint64_t idInt = 0;
2248ca45aa3cSEd Tanous 
224984396af9SPatrick Williams         auto [ptr, ec] = std::from_chars(&*targetID.begin(), &*targetID.end(),
225084396af9SPatrick Williams                                          idInt);
22519db4ba25SJiaqing Zhao         if (ec == std::errc::invalid_argument ||
22529db4ba25SJiaqing Zhao             ec == std::errc::result_out_of_range)
2253b7028ebfSSpencer Ku         {
22549db4ba25SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "LogEntry", param);
2255b7028ebfSSpencer Ku             return;
2256b7028ebfSSpencer Ku         }
2257b7028ebfSSpencer Ku 
2258b7028ebfSSpencer Ku         std::vector<std::filesystem::path> hostLoggerFiles;
2259b7028ebfSSpencer Ku         if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2260b7028ebfSSpencer Ku         {
2261b7028ebfSSpencer Ku             BMCWEB_LOG_ERROR << "fail to get host log file path";
2262b7028ebfSSpencer Ku             return;
2263b7028ebfSSpencer Ku         }
2264b7028ebfSSpencer Ku 
2265b7028ebfSSpencer Ku         size_t logCount = 0;
22663648c8beSEd Tanous         size_t top = 1;
2267b7028ebfSSpencer Ku         std::vector<std::string> logEntries;
2268b7028ebfSSpencer Ku         // We can get specific entry by skip and top. For example, if we
2269b7028ebfSSpencer Ku         // want to get nth entry, we can set skip = n-1 and top = 1 to
2270b7028ebfSSpencer Ku         // get that entry
2271002d39b4SEd Tanous         if (!getHostLoggerEntries(hostLoggerFiles, idInt, top, logEntries,
2272002d39b4SEd Tanous                                   logCount))
2273b7028ebfSSpencer Ku         {
2274b7028ebfSSpencer Ku             messages::internalError(asyncResp->res);
2275b7028ebfSSpencer Ku             return;
2276b7028ebfSSpencer Ku         }
2277b7028ebfSSpencer Ku 
2278b7028ebfSSpencer Ku         if (!logEntries.empty())
2279b7028ebfSSpencer Ku         {
22806d6574c9SJason M. Bills             nlohmann::json::object_t hostLogEntry;
22816d6574c9SJason M. Bills             fillHostLoggerEntryJson(targetID, logEntries[0], hostLogEntry);
22826d6574c9SJason M. Bills             asyncResp->res.jsonValue.update(hostLogEntry);
2283b7028ebfSSpencer Ku             return;
2284b7028ebfSSpencer Ku         }
2285b7028ebfSSpencer Ku 
2286b7028ebfSSpencer Ku         // Requested ID was not found
22879db4ba25SJiaqing Zhao         messages::resourceNotFound(asyncResp->res, "LogEntry", param);
2288b7028ebfSSpencer Ku         });
2289b7028ebfSSpencer Ku }
2290b7028ebfSSpencer Ku 
2291dd72e87bSClaire Weinan inline void handleBMCLogServicesCollectionGet(
2292fdd26906SClaire Weinan     crow::App& app, const crow::Request& req,
2293fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
22941da66f75SEd Tanous {
22953ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
229645ca1b86SEd Tanous     {
229745ca1b86SEd Tanous         return;
229845ca1b86SEd Tanous     }
22997e860f15SJohn Edward Broadbent     // Collections don't include the static data added by SubRoute
23007e860f15SJohn Edward Broadbent     // because it has a duplicate entry for members
2301e1f26343SJason M. Bills     asyncResp->res.jsonValue["@odata.type"] =
23021da66f75SEd Tanous         "#LogServiceCollection.LogServiceCollection";
2303e1f26343SJason M. Bills     asyncResp->res.jsonValue["@odata.id"] =
2304e1f26343SJason M. Bills         "/redfish/v1/Managers/bmc/LogServices";
2305002d39b4SEd Tanous     asyncResp->res.jsonValue["Name"] = "Open BMC Log Services Collection";
2306e1f26343SJason M. Bills     asyncResp->res.jsonValue["Description"] =
23071da66f75SEd Tanous         "Collection of LogServices for this Manager";
2308002d39b4SEd Tanous     nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
2309c4bf6374SJason M. Bills     logServiceArray = nlohmann::json::array();
2310fdd26906SClaire Weinan 
2311c4bf6374SJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
2312613dabeaSEd Tanous     nlohmann::json::object_t journal;
2313613dabeaSEd Tanous     journal["@odata.id"] = "/redfish/v1/Managers/bmc/LogServices/Journal";
2314b2ba3072SPatrick Williams     logServiceArray.emplace_back(std::move(journal));
2315c4bf6374SJason M. Bills #endif
2316fdd26906SClaire Weinan 
2317fdd26906SClaire Weinan     asyncResp->res.jsonValue["Members@odata.count"] = logServiceArray.size();
2318fdd26906SClaire Weinan 
2319fdd26906SClaire Weinan #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
232015912159SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
23217a1dbc48SGeorge Liu         "xyz.openbmc_project.Collection.DeleteAll"};
23227a1dbc48SGeorge Liu     dbus::utility::getSubTreePaths(
23237a1dbc48SGeorge Liu         "/xyz/openbmc_project/dump", 0, interfaces,
2324fdd26906SClaire Weinan         [asyncResp](
23257a1dbc48SGeorge Liu             const boost::system::error_code& ec,
2326fdd26906SClaire Weinan             const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
2327fdd26906SClaire Weinan         if (ec)
2328fdd26906SClaire Weinan         {
2329fdd26906SClaire Weinan             BMCWEB_LOG_ERROR
2330dd72e87bSClaire Weinan                 << "handleBMCLogServicesCollectionGet respHandler got error "
2331fdd26906SClaire Weinan                 << ec;
2332fdd26906SClaire Weinan             // Assume that getting an error simply means there are no dump
2333fdd26906SClaire Weinan             // LogServices. Return without adding any error response.
2334fdd26906SClaire Weinan             return;
2335fdd26906SClaire Weinan         }
2336fdd26906SClaire Weinan 
2337fdd26906SClaire Weinan         nlohmann::json& logServiceArrayLocal =
2338fdd26906SClaire Weinan             asyncResp->res.jsonValue["Members"];
2339fdd26906SClaire Weinan 
2340fdd26906SClaire Weinan         for (const std::string& path : subTreePaths)
2341fdd26906SClaire Weinan         {
2342fdd26906SClaire Weinan             if (path == "/xyz/openbmc_project/dump/bmc")
2343fdd26906SClaire Weinan             {
2344613dabeaSEd Tanous                 nlohmann::json::object_t member;
2345613dabeaSEd Tanous                 member["@odata.id"] =
2346613dabeaSEd Tanous                     "/redfish/v1/Managers/bmc/LogServices/Dump";
2347b2ba3072SPatrick Williams                 logServiceArrayLocal.emplace_back(std::move(member));
2348fdd26906SClaire Weinan             }
2349fdd26906SClaire Weinan             else if (path == "/xyz/openbmc_project/dump/faultlog")
2350fdd26906SClaire Weinan             {
2351613dabeaSEd Tanous                 nlohmann::json::object_t member;
2352613dabeaSEd Tanous                 member["@odata.id"] =
2353613dabeaSEd Tanous                     "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2354b2ba3072SPatrick Williams                 logServiceArrayLocal.emplace_back(std::move(member));
2355fdd26906SClaire Weinan             }
2356fdd26906SClaire Weinan         }
2357fdd26906SClaire Weinan 
2358e1f26343SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] =
2359fdd26906SClaire Weinan             logServiceArrayLocal.size();
23607a1dbc48SGeorge Liu         });
2361fdd26906SClaire Weinan #endif
2362fdd26906SClaire Weinan }
2363fdd26906SClaire Weinan 
2364fdd26906SClaire Weinan inline void requestRoutesBMCLogServiceCollection(App& app)
2365fdd26906SClaire Weinan {
2366fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
2367fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogServiceCollection)
2368fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(
2369dd72e87bSClaire Weinan             std::bind_front(handleBMCLogServicesCollectionGet, std::ref(app)));
2370e1f26343SJason M. Bills }
2371e1f26343SJason M. Bills 
23727e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogService(App& app)
2373e1f26343SJason M. Bills {
23747e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
2375ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
23767e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
237745ca1b86SEd Tanous             [&app](const crow::Request& req,
237845ca1b86SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
23793ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
23807e860f15SJohn Edward Broadbent         {
238145ca1b86SEd Tanous             return;
238245ca1b86SEd Tanous         }
2383e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
2384e1f26343SJason M. Bills             "#LogService.v1_1_0.LogService";
23850f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
23860f74e643SEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal";
2387002d39b4SEd Tanous         asyncResp->res.jsonValue["Name"] = "Open BMC Journal Log Service";
2388002d39b4SEd Tanous         asyncResp->res.jsonValue["Description"] = "BMC Journal Log Service";
2389ed34a4adSEd Tanous         asyncResp->res.jsonValue["Id"] = "Journal";
2390e1f26343SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
23917c8c4058STejas Patil 
23927c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
23932b82937eSEd Tanous             redfish::time_utils::getDateTimeOffsetNow();
2394002d39b4SEd Tanous         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
23957c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
23967c8c4058STejas Patil             redfishDateTimeOffset.second;
23977c8c4058STejas Patil 
23981476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
23991476687dSEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
24007e860f15SJohn Edward Broadbent         });
2401e1f26343SJason M. Bills }
2402e1f26343SJason M. Bills 
24033a48b3a2SJason M. Bills static int
24043a48b3a2SJason M. Bills     fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
2405e1f26343SJason M. Bills                                sd_journal* journal,
24063a48b3a2SJason M. Bills                                nlohmann::json::object_t& bmcJournalLogEntryJson)
2407e1f26343SJason M. Bills {
2408e1f26343SJason M. Bills     // Get the Log Entry contents
2409e1f26343SJason M. Bills     int ret = 0;
2410e1f26343SJason M. Bills 
2411a8fe54f0SJason M. Bills     std::string message;
2412a8fe54f0SJason M. Bills     std::string_view syslogID;
2413a8fe54f0SJason M. Bills     ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
2414a8fe54f0SJason M. Bills     if (ret < 0)
2415a8fe54f0SJason M. Bills     {
2416a8fe54f0SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read SYSLOG_IDENTIFIER field: "
2417a8fe54f0SJason M. Bills                          << strerror(-ret);
2418a8fe54f0SJason M. Bills     }
2419a8fe54f0SJason M. Bills     if (!syslogID.empty())
2420a8fe54f0SJason M. Bills     {
2421a8fe54f0SJason M. Bills         message += std::string(syslogID) + ": ";
2422a8fe54f0SJason M. Bills     }
2423a8fe54f0SJason M. Bills 
242439e77504SEd Tanous     std::string_view msg;
242516428a1aSJason M. Bills     ret = getJournalMetadata(journal, "MESSAGE", msg);
2426e1f26343SJason M. Bills     if (ret < 0)
2427e1f26343SJason M. Bills     {
2428e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
2429e1f26343SJason M. Bills         return 1;
2430e1f26343SJason M. Bills     }
2431a8fe54f0SJason M. Bills     message += std::string(msg);
2432e1f26343SJason M. Bills 
2433e1f26343SJason M. Bills     // Get the severity from the PRIORITY field
2434271584abSEd Tanous     long int severity = 8; // Default to an invalid priority
243516428a1aSJason M. Bills     ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
2436e1f26343SJason M. Bills     if (ret < 0)
2437e1f26343SJason M. Bills     {
2438e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
2439e1f26343SJason M. Bills     }
2440e1f26343SJason M. Bills 
2441e1f26343SJason M. Bills     // Get the Created time from the timestamp
244216428a1aSJason M. Bills     std::string entryTimeStr;
244316428a1aSJason M. Bills     if (!getEntryTimestamp(journal, entryTimeStr))
2444e1f26343SJason M. Bills     {
244516428a1aSJason M. Bills         return 1;
2446e1f26343SJason M. Bills     }
2447e1f26343SJason M. Bills 
2448e1f26343SJason M. Bills     // Fill in the log entry with the gathered data
24499c11a172SVijay Lobo     bmcJournalLogEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
2450ef4c65b7SEd Tanous     bmcJournalLogEntryJson["@odata.id"] = boost::urls::format(
2451ef4c65b7SEd Tanous         "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/{}",
2452eddfc437SWilly Tu         bmcJournalLogEntryID);
245384afc48bSJason M. Bills     bmcJournalLogEntryJson["Name"] = "BMC Journal Entry";
245484afc48bSJason M. Bills     bmcJournalLogEntryJson["Id"] = bmcJournalLogEntryID;
245584afc48bSJason M. Bills     bmcJournalLogEntryJson["Message"] = std::move(message);
245684afc48bSJason M. Bills     bmcJournalLogEntryJson["EntryType"] = "Oem";
245784afc48bSJason M. Bills     bmcJournalLogEntryJson["Severity"] = severity <= 2   ? "Critical"
2458738c1e61SPatrick Williams                                          : severity <= 4 ? "Warning"
245984afc48bSJason M. Bills                                                          : "OK";
246084afc48bSJason M. Bills     bmcJournalLogEntryJson["OemRecordFormat"] = "BMC Journal Entry";
246184afc48bSJason M. Bills     bmcJournalLogEntryJson["Created"] = std::move(entryTimeStr);
2462e1f26343SJason M. Bills     return 0;
2463e1f26343SJason M. Bills }
2464e1f26343SJason M. Bills 
24657e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogEntryCollection(App& app)
2466e1f26343SJason M. Bills {
24677e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
2468ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
2469002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2470002d39b4SEd Tanous             [&app](const crow::Request& req,
2471002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2472c937d2bfSEd Tanous         query_param::QueryCapabilities capabilities = {
2473c937d2bfSEd Tanous             .canDelegateTop = true,
2474c937d2bfSEd Tanous             .canDelegateSkip = true,
2475c937d2bfSEd Tanous         };
2476c937d2bfSEd Tanous         query_param::Query delegatedQuery;
2477c937d2bfSEd Tanous         if (!redfish::setUpRedfishRouteWithDelegation(
24783ba00073SCarson Labrado                 app, req, asyncResp, delegatedQuery, capabilities))
2479193ad2faSJason M. Bills         {
2480193ad2faSJason M. Bills             return;
2481193ad2faSJason M. Bills         }
24823648c8beSEd Tanous 
24833648c8beSEd Tanous         size_t skip = delegatedQuery.skip.value_or(0);
24845143f7a5SJiaqing Zhao         size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
24853648c8beSEd Tanous 
24867e860f15SJohn Edward Broadbent         // Collections don't include the static data added by SubRoute
24877e860f15SJohn Edward Broadbent         // because it has a duplicate entry for members
2488e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
2489e1f26343SJason M. Bills             "#LogEntryCollection.LogEntryCollection";
24900f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
24910f74e643SEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2492e1f26343SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
2493e1f26343SJason M. Bills         asyncResp->res.jsonValue["Description"] =
2494e1f26343SJason M. Bills             "Collection of BMC Journal Entries";
24950fda0f12SGeorge Liu         nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2496e1f26343SJason M. Bills         logEntryArray = nlohmann::json::array();
2497e1f26343SJason M. Bills 
24987e860f15SJohn Edward Broadbent         // Go through the journal and use the timestamp to create a
24997e860f15SJohn Edward Broadbent         // unique ID for each entry
2500e1f26343SJason M. Bills         sd_journal* journalTmp = nullptr;
2501e1f26343SJason M. Bills         int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2502e1f26343SJason M. Bills         if (ret < 0)
2503e1f26343SJason M. Bills         {
2504002d39b4SEd Tanous             BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2505f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2506e1f26343SJason M. Bills             return;
2507e1f26343SJason M. Bills         }
25080fda0f12SGeorge Liu         std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
25090fda0f12SGeorge Liu             journalTmp, sd_journal_close);
2510e1f26343SJason M. Bills         journalTmp = nullptr;
2511b01bf299SEd Tanous         uint64_t entryCount = 0;
2512e85d6b16SJason M. Bills         // Reset the unique ID on the first entry
2513e85d6b16SJason M. Bills         bool firstEntry = true;
2514e1f26343SJason M. Bills         SD_JOURNAL_FOREACH(journal.get())
2515e1f26343SJason M. Bills         {
2516193ad2faSJason M. Bills             entryCount++;
25177e860f15SJohn Edward Broadbent             // Handle paging using skip (number of entries to skip from
25187e860f15SJohn Edward Broadbent             // the start) and top (number of entries to display)
25193648c8beSEd Tanous             if (entryCount <= skip || entryCount > skip + top)
2520193ad2faSJason M. Bills             {
2521193ad2faSJason M. Bills                 continue;
2522193ad2faSJason M. Bills             }
2523193ad2faSJason M. Bills 
252416428a1aSJason M. Bills             std::string idStr;
2525e85d6b16SJason M. Bills             if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2526e1f26343SJason M. Bills             {
2527e1f26343SJason M. Bills                 continue;
2528e1f26343SJason M. Bills             }
2529e85d6b16SJason M. Bills             firstEntry = false;
2530e85d6b16SJason M. Bills 
25313a48b3a2SJason M. Bills             nlohmann::json::object_t bmcJournalLogEntry;
2532c4bf6374SJason M. Bills             if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2533c4bf6374SJason M. Bills                                            bmcJournalLogEntry) != 0)
2534e1f26343SJason M. Bills             {
2535f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
2536e1f26343SJason M. Bills                 return;
2537e1f26343SJason M. Bills             }
2538b2ba3072SPatrick Williams             logEntryArray.emplace_back(std::move(bmcJournalLogEntry));
2539e1f26343SJason M. Bills         }
2540193ad2faSJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
25413648c8beSEd Tanous         if (skip + top < entryCount)
2542193ad2faSJason M. Bills         {
2543193ad2faSJason M. Bills             asyncResp->res.jsonValue["Members@odata.nextLink"] =
25440fda0f12SGeorge Liu                 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
25453648c8beSEd Tanous                 std::to_string(skip + top);
2546193ad2faSJason M. Bills         }
25477e860f15SJohn Edward Broadbent         });
2548e1f26343SJason M. Bills }
2549e1f26343SJason M. Bills 
25507e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogEntry(App& app)
2551e1f26343SJason M. Bills {
25527e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
25537e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
2554ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
25557e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
255645ca1b86SEd Tanous             [&app](const crow::Request& req,
25577e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
25587e860f15SJohn Edward Broadbent                    const std::string& entryID) {
25593ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
256045ca1b86SEd Tanous         {
256145ca1b86SEd Tanous             return;
256245ca1b86SEd Tanous         }
2563e1f26343SJason M. Bills         // Convert the unique ID back to a timestamp to find the entry
2564e1f26343SJason M. Bills         uint64_t ts = 0;
2565271584abSEd Tanous         uint64_t index = 0;
25668d1b46d7Szhanghch05         if (!getTimestampFromID(asyncResp, entryID, ts, index))
2567e1f26343SJason M. Bills         {
256816428a1aSJason M. Bills             return;
2569e1f26343SJason M. Bills         }
2570e1f26343SJason M. Bills 
2571e1f26343SJason M. Bills         sd_journal* journalTmp = nullptr;
2572e1f26343SJason M. Bills         int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2573e1f26343SJason M. Bills         if (ret < 0)
2574e1f26343SJason M. Bills         {
2575002d39b4SEd Tanous             BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2576f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2577e1f26343SJason M. Bills             return;
2578e1f26343SJason M. Bills         }
2579002d39b4SEd Tanous         std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2580002d39b4SEd Tanous             journalTmp, sd_journal_close);
2581e1f26343SJason M. Bills         journalTmp = nullptr;
25827e860f15SJohn Edward Broadbent         // Go to the timestamp in the log and move to the entry at the
25837e860f15SJohn Edward Broadbent         // index tracking the unique ID
2584af07e3f5SJason M. Bills         std::string idStr;
2585af07e3f5SJason M. Bills         bool firstEntry = true;
2586e1f26343SJason M. Bills         ret = sd_journal_seek_realtime_usec(journal.get(), ts);
25872056b6d1SManojkiran Eda         if (ret < 0)
25882056b6d1SManojkiran Eda         {
25892056b6d1SManojkiran Eda             BMCWEB_LOG_ERROR << "failed to seek to an entry in journal"
25902056b6d1SManojkiran Eda                              << strerror(-ret);
25912056b6d1SManojkiran Eda             messages::internalError(asyncResp->res);
25922056b6d1SManojkiran Eda             return;
25932056b6d1SManojkiran Eda         }
2594271584abSEd Tanous         for (uint64_t i = 0; i <= index; i++)
2595e1f26343SJason M. Bills         {
2596e1f26343SJason M. Bills             sd_journal_next(journal.get());
2597af07e3f5SJason M. Bills             if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2598af07e3f5SJason M. Bills             {
2599af07e3f5SJason M. Bills                 messages::internalError(asyncResp->res);
2600af07e3f5SJason M. Bills                 return;
2601af07e3f5SJason M. Bills             }
2602af07e3f5SJason M. Bills             firstEntry = false;
2603af07e3f5SJason M. Bills         }
2604c4bf6374SJason M. Bills         // Confirm that the entry ID matches what was requested
2605af07e3f5SJason M. Bills         if (idStr != entryID)
2606c4bf6374SJason M. Bills         {
26079db4ba25SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
2608c4bf6374SJason M. Bills             return;
2609c4bf6374SJason M. Bills         }
2610c4bf6374SJason M. Bills 
26113a48b3a2SJason M. Bills         nlohmann::json::object_t bmcJournalLogEntry;
2612c4bf6374SJason M. Bills         if (fillBMCJournalLogEntryJson(entryID, journal.get(),
26133a48b3a2SJason M. Bills                                        bmcJournalLogEntry) != 0)
2614e1f26343SJason M. Bills         {
2615f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2616e1f26343SJason M. Bills             return;
2617e1f26343SJason M. Bills         }
2618d405bb51SJason M. Bills         asyncResp->res.jsonValue.update(bmcJournalLogEntry);
26197e860f15SJohn Edward Broadbent         });
2620c9bb6861Sraviteja-b }
2621c9bb6861Sraviteja-b 
2622fdd26906SClaire Weinan inline void
2623fdd26906SClaire Weinan     getDumpServiceInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2624fdd26906SClaire Weinan                        const std::string& dumpType)
2625c9bb6861Sraviteja-b {
2626fdd26906SClaire Weinan     std::string dumpPath;
2627fdd26906SClaire Weinan     std::string overWritePolicy;
2628fdd26906SClaire Weinan     bool collectDiagnosticDataSupported = false;
2629fdd26906SClaire Weinan 
2630fdd26906SClaire Weinan     if (dumpType == "BMC")
263145ca1b86SEd Tanous     {
2632fdd26906SClaire Weinan         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump";
2633fdd26906SClaire Weinan         overWritePolicy = "WrapsWhenFull";
2634fdd26906SClaire Weinan         collectDiagnosticDataSupported = true;
2635fdd26906SClaire Weinan     }
2636fdd26906SClaire Weinan     else if (dumpType == "FaultLog")
2637fdd26906SClaire Weinan     {
2638fdd26906SClaire Weinan         dumpPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2639fdd26906SClaire Weinan         overWritePolicy = "Unknown";
2640fdd26906SClaire Weinan         collectDiagnosticDataSupported = false;
2641fdd26906SClaire Weinan     }
2642fdd26906SClaire Weinan     else if (dumpType == "System")
2643fdd26906SClaire Weinan     {
2644fdd26906SClaire Weinan         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump";
2645fdd26906SClaire Weinan         overWritePolicy = "WrapsWhenFull";
2646fdd26906SClaire Weinan         collectDiagnosticDataSupported = true;
2647fdd26906SClaire Weinan     }
2648fdd26906SClaire Weinan     else
2649fdd26906SClaire Weinan     {
2650fdd26906SClaire Weinan         BMCWEB_LOG_ERROR << "getDumpServiceInfo() invalid dump type: "
2651fdd26906SClaire Weinan                          << dumpType;
2652fdd26906SClaire Weinan         messages::internalError(asyncResp->res);
265345ca1b86SEd Tanous         return;
265445ca1b86SEd Tanous     }
2655fdd26906SClaire Weinan 
2656fdd26906SClaire Weinan     asyncResp->res.jsonValue["@odata.id"] = dumpPath;
2657fdd26906SClaire Weinan     asyncResp->res.jsonValue["@odata.type"] = "#LogService.v1_2_0.LogService";
2658c9bb6861Sraviteja-b     asyncResp->res.jsonValue["Name"] = "Dump LogService";
2659fdd26906SClaire Weinan     asyncResp->res.jsonValue["Description"] = dumpType + " Dump LogService";
2660fdd26906SClaire Weinan     asyncResp->res.jsonValue["Id"] = std::filesystem::path(dumpPath).filename();
2661fdd26906SClaire Weinan     asyncResp->res.jsonValue["OverWritePolicy"] = std::move(overWritePolicy);
26627c8c4058STejas Patil 
26637c8c4058STejas Patil     std::pair<std::string, std::string> redfishDateTimeOffset =
26642b82937eSEd Tanous         redfish::time_utils::getDateTimeOffsetNow();
26650fda0f12SGeorge Liu     asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
26667c8c4058STejas Patil     asyncResp->res.jsonValue["DateTimeLocalOffset"] =
26677c8c4058STejas Patil         redfishDateTimeOffset.second;
26687c8c4058STejas Patil 
2669fdd26906SClaire Weinan     asyncResp->res.jsonValue["Entries"]["@odata.id"] = dumpPath + "/Entries";
2670fdd26906SClaire Weinan 
2671fdd26906SClaire Weinan     if (collectDiagnosticDataSupported)
2672fdd26906SClaire Weinan     {
2673002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
26741476687dSEd Tanous                                 ["target"] =
2675fdd26906SClaire Weinan             dumpPath + "/Actions/LogService.CollectDiagnosticData";
2676fdd26906SClaire Weinan     }
26770d946211SClaire Weinan 
26780d946211SClaire Weinan     constexpr std::array<std::string_view, 1> interfaces = {deleteAllInterface};
26790d946211SClaire Weinan     dbus::utility::getSubTreePaths(
26800d946211SClaire Weinan         "/xyz/openbmc_project/dump", 0, interfaces,
26810d946211SClaire Weinan         [asyncResp, dumpType, dumpPath](
26820d946211SClaire Weinan             const boost::system::error_code& ec,
26830d946211SClaire Weinan             const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
26840d946211SClaire Weinan         if (ec)
26850d946211SClaire Weinan         {
26860d946211SClaire Weinan             BMCWEB_LOG_ERROR << "getDumpServiceInfo respHandler got error "
26870d946211SClaire Weinan                              << ec;
26880d946211SClaire Weinan             // Assume that getting an error simply means there are no dump
26890d946211SClaire Weinan             // LogServices. Return without adding any error response.
26900d946211SClaire Weinan             return;
26910d946211SClaire Weinan         }
26920d946211SClaire Weinan 
26930d946211SClaire Weinan         const std::string dbusDumpPath =
26940d946211SClaire Weinan             "/xyz/openbmc_project/dump/" +
26950d946211SClaire Weinan             boost::algorithm::to_lower_copy(dumpType);
26960d946211SClaire Weinan 
26970d946211SClaire Weinan         for (const std::string& path : subTreePaths)
26980d946211SClaire Weinan         {
26990d946211SClaire Weinan             if (path == dbusDumpPath)
27000d946211SClaire Weinan             {
27010d946211SClaire Weinan                 asyncResp->res
27020d946211SClaire Weinan                     .jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
27030d946211SClaire Weinan                     dumpPath + "/Actions/LogService.ClearLog";
27040d946211SClaire Weinan                 break;
27050d946211SClaire Weinan             }
27060d946211SClaire Weinan         }
27070d946211SClaire Weinan         });
2708c9bb6861Sraviteja-b }
2709c9bb6861Sraviteja-b 
2710fdd26906SClaire Weinan inline void handleLogServicesDumpServiceGet(
2711fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2712fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
27137e860f15SJohn Edward Broadbent {
27143ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
271545ca1b86SEd Tanous     {
271645ca1b86SEd Tanous         return;
271745ca1b86SEd Tanous     }
2718fdd26906SClaire Weinan     getDumpServiceInfo(asyncResp, dumpType);
2719fdd26906SClaire Weinan }
2720c9bb6861Sraviteja-b 
272122d268cbSEd Tanous inline void handleLogServicesDumpServiceComputerSystemGet(
272222d268cbSEd Tanous     crow::App& app, const crow::Request& req,
272322d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
272422d268cbSEd Tanous     const std::string& chassisId)
272522d268cbSEd Tanous {
272622d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
272722d268cbSEd Tanous     {
272822d268cbSEd Tanous         return;
272922d268cbSEd Tanous     }
273022d268cbSEd Tanous     if (chassisId != "system")
273122d268cbSEd Tanous     {
273222d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
273322d268cbSEd Tanous         return;
273422d268cbSEd Tanous     }
273522d268cbSEd Tanous     getDumpServiceInfo(asyncResp, "System");
273622d268cbSEd Tanous }
273722d268cbSEd Tanous 
2738fdd26906SClaire Weinan inline void handleLogServicesDumpEntriesCollectionGet(
2739fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2740fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2741fdd26906SClaire Weinan {
2742fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2743fdd26906SClaire Weinan     {
2744fdd26906SClaire Weinan         return;
2745fdd26906SClaire Weinan     }
2746fdd26906SClaire Weinan     getDumpEntryCollection(asyncResp, dumpType);
2747fdd26906SClaire Weinan }
2748fdd26906SClaire Weinan 
274922d268cbSEd Tanous inline void handleLogServicesDumpEntriesCollectionComputerSystemGet(
275022d268cbSEd Tanous     crow::App& app, const crow::Request& req,
275122d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
275222d268cbSEd Tanous     const std::string& chassisId)
275322d268cbSEd Tanous {
275422d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
275522d268cbSEd Tanous     {
275622d268cbSEd Tanous         return;
275722d268cbSEd Tanous     }
275822d268cbSEd Tanous     if (chassisId != "system")
275922d268cbSEd Tanous     {
276022d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
276122d268cbSEd Tanous         return;
276222d268cbSEd Tanous     }
276322d268cbSEd Tanous     getDumpEntryCollection(asyncResp, "System");
276422d268cbSEd Tanous }
276522d268cbSEd Tanous 
2766fdd26906SClaire Weinan inline void handleLogServicesDumpEntryGet(
2767fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2768fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2769fdd26906SClaire Weinan     const std::string& dumpId)
2770fdd26906SClaire Weinan {
2771fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2772fdd26906SClaire Weinan     {
2773fdd26906SClaire Weinan         return;
2774fdd26906SClaire Weinan     }
2775fdd26906SClaire Weinan     getDumpEntryById(asyncResp, dumpId, dumpType);
2776fdd26906SClaire Weinan }
277722d268cbSEd Tanous inline void handleLogServicesDumpEntryComputerSystemGet(
277822d268cbSEd Tanous     crow::App& app, const crow::Request& req,
277922d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
278022d268cbSEd Tanous     const std::string& chassisId, const std::string& dumpId)
278122d268cbSEd Tanous {
278222d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
278322d268cbSEd Tanous     {
278422d268cbSEd Tanous         return;
278522d268cbSEd Tanous     }
278622d268cbSEd Tanous     if (chassisId != "system")
278722d268cbSEd Tanous     {
278822d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
278922d268cbSEd Tanous         return;
279022d268cbSEd Tanous     }
279122d268cbSEd Tanous     getDumpEntryById(asyncResp, dumpId, "System");
279222d268cbSEd Tanous }
2793fdd26906SClaire Weinan 
2794fdd26906SClaire Weinan inline void handleLogServicesDumpEntryDelete(
2795fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2796fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2797fdd26906SClaire Weinan     const std::string& dumpId)
2798fdd26906SClaire Weinan {
2799fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2800fdd26906SClaire Weinan     {
2801fdd26906SClaire Weinan         return;
2802fdd26906SClaire Weinan     }
2803fdd26906SClaire Weinan     deleteDumpEntry(asyncResp, dumpId, dumpType);
2804fdd26906SClaire Weinan }
2805fdd26906SClaire Weinan 
280622d268cbSEd Tanous inline void handleLogServicesDumpEntryComputerSystemDelete(
280722d268cbSEd Tanous     crow::App& app, const crow::Request& req,
280822d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
280922d268cbSEd Tanous     const std::string& chassisId, const std::string& dumpId)
281022d268cbSEd Tanous {
281122d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
281222d268cbSEd Tanous     {
281322d268cbSEd Tanous         return;
281422d268cbSEd Tanous     }
281522d268cbSEd Tanous     if (chassisId != "system")
281622d268cbSEd Tanous     {
281722d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
281822d268cbSEd Tanous         return;
281922d268cbSEd Tanous     }
282022d268cbSEd Tanous     deleteDumpEntry(asyncResp, dumpId, "System");
282122d268cbSEd Tanous }
282222d268cbSEd Tanous 
2823fdd26906SClaire Weinan inline void handleLogServicesDumpCollectDiagnosticDataPost(
2824fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2825fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2826fdd26906SClaire Weinan {
2827fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2828fdd26906SClaire Weinan     {
2829fdd26906SClaire Weinan         return;
2830fdd26906SClaire Weinan     }
2831fdd26906SClaire Weinan     createDump(asyncResp, req, dumpType);
2832fdd26906SClaire Weinan }
2833fdd26906SClaire Weinan 
283422d268cbSEd Tanous inline void handleLogServicesDumpCollectDiagnosticDataComputerSystemPost(
283522d268cbSEd Tanous     crow::App& app, const crow::Request& req,
283622d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
283722d268cbSEd Tanous     const std::string& chassisId)
283822d268cbSEd Tanous {
283922d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
284022d268cbSEd Tanous     {
284122d268cbSEd Tanous         return;
284222d268cbSEd Tanous     }
284322d268cbSEd Tanous     if (chassisId != "system")
284422d268cbSEd Tanous     {
284522d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
284622d268cbSEd Tanous         return;
284722d268cbSEd Tanous     }
284822d268cbSEd Tanous     createDump(asyncResp, req, "System");
284922d268cbSEd Tanous }
285022d268cbSEd Tanous 
2851fdd26906SClaire Weinan inline void handleLogServicesDumpClearLogPost(
2852fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2853fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2854fdd26906SClaire Weinan {
2855fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2856fdd26906SClaire Weinan     {
2857fdd26906SClaire Weinan         return;
2858fdd26906SClaire Weinan     }
2859fdd26906SClaire Weinan     clearDump(asyncResp, dumpType);
2860fdd26906SClaire Weinan }
2861fdd26906SClaire Weinan 
286222d268cbSEd Tanous inline void handleLogServicesDumpClearLogComputerSystemPost(
286322d268cbSEd Tanous     crow::App& app, const crow::Request& req,
286422d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
286522d268cbSEd Tanous     const std::string& chassisId)
286622d268cbSEd Tanous {
286722d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
286822d268cbSEd Tanous     {
286922d268cbSEd Tanous         return;
287022d268cbSEd Tanous     }
287122d268cbSEd Tanous     if (chassisId != "system")
287222d268cbSEd Tanous     {
287322d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
287422d268cbSEd Tanous         return;
287522d268cbSEd Tanous     }
287622d268cbSEd Tanous     clearDump(asyncResp, "System");
287722d268cbSEd Tanous }
287822d268cbSEd Tanous 
2879fdd26906SClaire Weinan inline void requestRoutesBMCDumpService(App& app)
2880fdd26906SClaire Weinan {
2881fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
2882fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogService)
2883fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2884fdd26906SClaire Weinan             handleLogServicesDumpServiceGet, std::ref(app), "BMC"));
2885fdd26906SClaire Weinan }
2886fdd26906SClaire Weinan 
2887fdd26906SClaire Weinan inline void requestRoutesBMCDumpEntryCollection(App& app)
2888fdd26906SClaire Weinan {
2889fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
2890fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogEntryCollection)
2891fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2892fdd26906SClaire Weinan             handleLogServicesDumpEntriesCollectionGet, std::ref(app), "BMC"));
2893c9bb6861Sraviteja-b }
2894c9bb6861Sraviteja-b 
28957e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpEntry(App& app)
2896c9bb6861Sraviteja-b {
28977e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
28987e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
2899ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
2900fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2901fdd26906SClaire Weinan             handleLogServicesDumpEntryGet, std::ref(app), "BMC"));
2902fdd26906SClaire Weinan 
29037e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
29047e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
2905ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
2906fdd26906SClaire Weinan         .methods(boost::beast::http::verb::delete_)(std::bind_front(
2907fdd26906SClaire Weinan             handleLogServicesDumpEntryDelete, std::ref(app), "BMC"));
2908c9bb6861Sraviteja-b }
2909c9bb6861Sraviteja-b 
29107e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpCreate(App& app)
2911c9bb6861Sraviteja-b {
29120fda0f12SGeorge Liu     BMCWEB_ROUTE(
29130fda0f12SGeorge Liu         app,
29140fda0f12SGeorge Liu         "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
2915ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
29167e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
2917fdd26906SClaire Weinan             std::bind_front(handleLogServicesDumpCollectDiagnosticDataPost,
2918fdd26906SClaire Weinan                             std::ref(app), "BMC"));
2919a43be80fSAsmitha Karunanithi }
2920a43be80fSAsmitha Karunanithi 
29217e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpClear(App& app)
292280319af1SAsmitha Karunanithi {
29230fda0f12SGeorge Liu     BMCWEB_ROUTE(
29240fda0f12SGeorge Liu         app,
29250fda0f12SGeorge Liu         "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog/")
2926ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
2927fdd26906SClaire Weinan         .methods(boost::beast::http::verb::post)(std::bind_front(
2928fdd26906SClaire Weinan             handleLogServicesDumpClearLogPost, std::ref(app), "BMC"));
292945ca1b86SEd Tanous }
2930fdd26906SClaire Weinan 
2931fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpService(App& app)
2932fdd26906SClaire Weinan {
2933fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/")
2934fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogService)
2935fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2936fdd26906SClaire Weinan             handleLogServicesDumpServiceGet, std::ref(app), "FaultLog"));
2937fdd26906SClaire Weinan }
2938fdd26906SClaire Weinan 
2939fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpEntryCollection(App& app)
2940fdd26906SClaire Weinan {
2941fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/")
2942fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogEntryCollection)
2943fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(
2944fdd26906SClaire Weinan             std::bind_front(handleLogServicesDumpEntriesCollectionGet,
2945fdd26906SClaire Weinan                             std::ref(app), "FaultLog"));
2946fdd26906SClaire Weinan }
2947fdd26906SClaire Weinan 
2948fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpEntry(App& app)
2949fdd26906SClaire Weinan {
2950fdd26906SClaire Weinan     BMCWEB_ROUTE(app,
2951fdd26906SClaire Weinan                  "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
2952fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogEntry)
2953fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2954fdd26906SClaire Weinan             handleLogServicesDumpEntryGet, std::ref(app), "FaultLog"));
2955fdd26906SClaire Weinan 
2956fdd26906SClaire Weinan     BMCWEB_ROUTE(app,
2957fdd26906SClaire Weinan                  "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
2958fdd26906SClaire Weinan         .privileges(redfish::privileges::deleteLogEntry)
2959fdd26906SClaire Weinan         .methods(boost::beast::http::verb::delete_)(std::bind_front(
2960fdd26906SClaire Weinan             handleLogServicesDumpEntryDelete, std::ref(app), "FaultLog"));
2961fdd26906SClaire Weinan }
2962fdd26906SClaire Weinan 
2963fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpClear(App& app)
2964fdd26906SClaire Weinan {
2965fdd26906SClaire Weinan     BMCWEB_ROUTE(
2966fdd26906SClaire Weinan         app,
2967fdd26906SClaire Weinan         "/redfish/v1/Managers/bmc/LogServices/FaultLog/Actions/LogService.ClearLog/")
2968fdd26906SClaire Weinan         .privileges(redfish::privileges::postLogService)
2969fdd26906SClaire Weinan         .methods(boost::beast::http::verb::post)(std::bind_front(
2970fdd26906SClaire Weinan             handleLogServicesDumpClearLogPost, std::ref(app), "FaultLog"));
29715cb1dd27SAsmitha Karunanithi }
29725cb1dd27SAsmitha Karunanithi 
29737e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpService(App& app)
29745cb1dd27SAsmitha Karunanithi {
297522d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/")
2976ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
29776ab9ad54SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
297822d268cbSEd Tanous             handleLogServicesDumpServiceComputerSystemGet, std::ref(app)));
29795cb1dd27SAsmitha Karunanithi }
29805cb1dd27SAsmitha Karunanithi 
29817e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpEntryCollection(App& app)
29827e860f15SJohn Edward Broadbent {
298322d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/")
2984ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
298522d268cbSEd Tanous         .methods(boost::beast::http::verb::get)(std::bind_front(
298622d268cbSEd Tanous             handleLogServicesDumpEntriesCollectionComputerSystemGet,
298722d268cbSEd Tanous             std::ref(app)));
29885cb1dd27SAsmitha Karunanithi }
29895cb1dd27SAsmitha Karunanithi 
29907e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpEntry(App& app)
29915cb1dd27SAsmitha Karunanithi {
29927e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
299322d268cbSEd Tanous                  "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
2994ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
29956ab9ad54SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
299622d268cbSEd Tanous             handleLogServicesDumpEntryComputerSystemGet, std::ref(app)));
29978d1b46d7Szhanghch05 
29987e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
299922d268cbSEd Tanous                  "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
3000ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
30016ab9ad54SClaire Weinan         .methods(boost::beast::http::verb::delete_)(std::bind_front(
300222d268cbSEd Tanous             handleLogServicesDumpEntryComputerSystemDelete, std::ref(app)));
30035cb1dd27SAsmitha Karunanithi }
3004c9bb6861Sraviteja-b 
30057e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpCreate(App& app)
3006c9bb6861Sraviteja-b {
30070fda0f12SGeorge Liu     BMCWEB_ROUTE(
30080fda0f12SGeorge Liu         app,
300922d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
3010ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
301122d268cbSEd Tanous         .methods(boost::beast::http::verb::post)(std::bind_front(
301222d268cbSEd Tanous             handleLogServicesDumpCollectDiagnosticDataComputerSystemPost,
301322d268cbSEd Tanous             std::ref(app)));
3014a43be80fSAsmitha Karunanithi }
3015a43be80fSAsmitha Karunanithi 
30167e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpClear(App& app)
3017a43be80fSAsmitha Karunanithi {
30180fda0f12SGeorge Liu     BMCWEB_ROUTE(
30190fda0f12SGeorge Liu         app,
302022d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.ClearLog/")
3021ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
30226ab9ad54SClaire Weinan         .methods(boost::beast::http::verb::post)(std::bind_front(
302322d268cbSEd Tanous             handleLogServicesDumpClearLogComputerSystemPost, std::ref(app)));
3024013487e5Sraviteja-b }
3025013487e5Sraviteja-b 
30267e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpService(App& app)
30271da66f75SEd Tanous {
30283946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
30293946028dSAppaRao Puli     // method for security reasons.
30301da66f75SEd Tanous     /**
30311da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
30321da66f75SEd Tanous      */
303322d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/")
3034ed398213SEd Tanous         // This is incorrect, should be:
3035ed398213SEd Tanous         //.privileges(redfish::privileges::getLogService)
3036432a890cSEd Tanous         .privileges({{"ConfigureManager"}})
3037002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
3038002d39b4SEd Tanous             [&app](const crow::Request& req,
303922d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
304022d268cbSEd Tanous                    const std::string& systemName) {
30413ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
304245ca1b86SEd Tanous         {
304345ca1b86SEd Tanous             return;
304445ca1b86SEd Tanous         }
304522d268cbSEd Tanous         if (systemName != "system")
304622d268cbSEd Tanous         {
304722d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
304822d268cbSEd Tanous                                        systemName);
304922d268cbSEd Tanous             return;
305022d268cbSEd Tanous         }
305122d268cbSEd Tanous 
30527e860f15SJohn Edward Broadbent         // Copy over the static data to include the entries added by
30537e860f15SJohn Edward Broadbent         // SubRoute
30540f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
3055424c4176SJason M. Bills             "/redfish/v1/Systems/system/LogServices/Crashdump";
3056e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
30578e6c099aSJason M. Bills             "#LogService.v1_2_0.LogService";
30584f50ae4bSGunnar Mills         asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
30594f50ae4bSGunnar Mills         asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
306015b89725SV-Sanjana         asyncResp->res.jsonValue["Id"] = "Crashdump";
3061e1f26343SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
3062e1f26343SJason M. Bills         asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
30637c8c4058STejas Patil 
30647c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
30652b82937eSEd Tanous             redfish::time_utils::getDateTimeOffsetNow();
30667c8c4058STejas Patil         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
30677c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
30687c8c4058STejas Patil             redfishDateTimeOffset.second;
30697c8c4058STejas Patil 
30701476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
3071ef4c65b7SEd Tanous             "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
3072002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
30731476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog";
3074002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
30751476687dSEd Tanous                                 ["target"] =
30761476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData";
30777e860f15SJohn Edward Broadbent         });
30781da66f75SEd Tanous }
30791da66f75SEd Tanous 
30807e860f15SJohn Edward Broadbent void inline requestRoutesCrashdumpClear(App& app)
30815b61b5e8SJason M. Bills {
30820fda0f12SGeorge Liu     BMCWEB_ROUTE(
30830fda0f12SGeorge Liu         app,
308422d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.ClearLog/")
3085ed398213SEd Tanous         // This is incorrect, should be:
3086ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
3087432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
30887e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
308945ca1b86SEd Tanous             [&app](const crow::Request& req,
309022d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
309122d268cbSEd Tanous                    const std::string& systemName) {
30923ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
309345ca1b86SEd Tanous         {
309445ca1b86SEd Tanous             return;
309545ca1b86SEd Tanous         }
309622d268cbSEd Tanous         if (systemName != "system")
309722d268cbSEd Tanous         {
309822d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
309922d268cbSEd Tanous                                        systemName);
310022d268cbSEd Tanous             return;
310122d268cbSEd Tanous         }
31025b61b5e8SJason M. Bills         crow::connections::systemBus->async_method_call(
31035e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec,
3104cb13a392SEd Tanous                         const std::string&) {
31055b61b5e8SJason M. Bills             if (ec)
31065b61b5e8SJason M. Bills             {
31075b61b5e8SJason M. Bills                 messages::internalError(asyncResp->res);
31085b61b5e8SJason M. Bills                 return;
31095b61b5e8SJason M. Bills             }
31105b61b5e8SJason M. Bills             messages::success(asyncResp->res);
31115b61b5e8SJason M. Bills             },
3112002d39b4SEd Tanous             crashdumpObject, crashdumpPath, deleteAllInterface, "DeleteAll");
31137e860f15SJohn Edward Broadbent         });
31145b61b5e8SJason M. Bills }
31155b61b5e8SJason M. Bills 
31168d1b46d7Szhanghch05 static void
31178d1b46d7Szhanghch05     logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
31188d1b46d7Szhanghch05                       const std::string& logID, nlohmann::json& logEntryJson)
3119e855dd28SJason M. Bills {
3120043a0536SJohnathan Mantey     auto getStoredLogCallback =
3121b9d36b47SEd Tanous         [asyncResp, logID,
31225e7e2dc5SEd Tanous          &logEntryJson](const boost::system::error_code& ec,
3123b9d36b47SEd Tanous                         const dbus::utility::DBusPropertiesMap& params) {
3124e855dd28SJason M. Bills         if (ec)
3125e855dd28SJason M. Bills         {
3126e855dd28SJason M. Bills             BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
31271ddcf01aSJason M. Bills             if (ec.value() ==
31281ddcf01aSJason M. Bills                 boost::system::linux_error::bad_request_descriptor)
31291ddcf01aSJason M. Bills             {
3130002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
31311ddcf01aSJason M. Bills             }
31321ddcf01aSJason M. Bills             else
31331ddcf01aSJason M. Bills             {
3134e855dd28SJason M. Bills                 messages::internalError(asyncResp->res);
31351ddcf01aSJason M. Bills             }
3136e855dd28SJason M. Bills             return;
3137e855dd28SJason M. Bills         }
3138043a0536SJohnathan Mantey 
3139043a0536SJohnathan Mantey         std::string timestamp{};
3140043a0536SJohnathan Mantey         std::string filename{};
3141043a0536SJohnathan Mantey         std::string logfile{};
31422c70f800SEd Tanous         parseCrashdumpParameters(params, filename, timestamp, logfile);
3143043a0536SJohnathan Mantey 
3144043a0536SJohnathan Mantey         if (filename.empty() || timestamp.empty())
3145e855dd28SJason M. Bills         {
31469db4ba25SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
3147e855dd28SJason M. Bills             return;
3148e855dd28SJason M. Bills         }
3149e855dd28SJason M. Bills 
3150043a0536SJohnathan Mantey         std::string crashdumpURI =
3151e855dd28SJason M. Bills             "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
3152043a0536SJohnathan Mantey             logID + "/" + filename;
315384afc48bSJason M. Bills         nlohmann::json::object_t logEntry;
31549c11a172SVijay Lobo         logEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
3155ef4c65b7SEd Tanous         logEntry["@odata.id"] = boost::urls::format(
3156ef4c65b7SEd Tanous             "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/{}",
3157ef4c65b7SEd Tanous             logID);
315884afc48bSJason M. Bills         logEntry["Name"] = "CPU Crashdump";
315984afc48bSJason M. Bills         logEntry["Id"] = logID;
316084afc48bSJason M. Bills         logEntry["EntryType"] = "Oem";
316184afc48bSJason M. Bills         logEntry["AdditionalDataURI"] = std::move(crashdumpURI);
316284afc48bSJason M. Bills         logEntry["DiagnosticDataType"] = "OEM";
316384afc48bSJason M. Bills         logEntry["OEMDiagnosticDataType"] = "PECICrashdump";
316484afc48bSJason M. Bills         logEntry["Created"] = std::move(timestamp);
31652b20ef6eSJason M. Bills 
31662b20ef6eSJason M. Bills         // If logEntryJson references an array of LogEntry resources
31672b20ef6eSJason M. Bills         // ('Members' list), then push this as a new entry, otherwise set it
31682b20ef6eSJason M. Bills         // directly
31692b20ef6eSJason M. Bills         if (logEntryJson.is_array())
31702b20ef6eSJason M. Bills         {
31712b20ef6eSJason M. Bills             logEntryJson.push_back(logEntry);
31722b20ef6eSJason M. Bills             asyncResp->res.jsonValue["Members@odata.count"] =
31732b20ef6eSJason M. Bills                 logEntryJson.size();
31742b20ef6eSJason M. Bills         }
31752b20ef6eSJason M. Bills         else
31762b20ef6eSJason M. Bills         {
3177d405bb51SJason M. Bills             logEntryJson.update(logEntry);
31782b20ef6eSJason M. Bills         }
3179e855dd28SJason M. Bills     };
3180d1bde9e5SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
3181d1bde9e5SKrzysztof Grobelny         *crow::connections::systemBus, crashdumpObject,
3182d1bde9e5SKrzysztof Grobelny         crashdumpPath + std::string("/") + logID, crashdumpInterface,
3183d1bde9e5SKrzysztof Grobelny         std::move(getStoredLogCallback));
3184e855dd28SJason M. Bills }
3185e855dd28SJason M. Bills 
31867e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpEntryCollection(App& app)
31871da66f75SEd Tanous {
31883946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
31893946028dSAppaRao Puli     // method for security reasons.
31901da66f75SEd Tanous     /**
31911da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
31921da66f75SEd Tanous      */
31937e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
319422d268cbSEd Tanous                  "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/")
3195ed398213SEd Tanous         // This is incorrect, should be.
3196ed398213SEd Tanous         //.privileges(redfish::privileges::postLogEntryCollection)
3197432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
3198002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
3199002d39b4SEd Tanous             [&app](const crow::Request& req,
320022d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
320122d268cbSEd Tanous                    const std::string& systemName) {
32023ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
320345ca1b86SEd Tanous         {
320445ca1b86SEd Tanous             return;
320545ca1b86SEd Tanous         }
320622d268cbSEd Tanous         if (systemName != "system")
320722d268cbSEd Tanous         {
320822d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
320922d268cbSEd Tanous                                        systemName);
321022d268cbSEd Tanous             return;
321122d268cbSEd Tanous         }
321222d268cbSEd Tanous 
32137a1dbc48SGeorge Liu         constexpr std::array<std::string_view, 1> interfaces = {
32147a1dbc48SGeorge Liu             crashdumpInterface};
32157a1dbc48SGeorge Liu         dbus::utility::getSubTreePaths(
32167a1dbc48SGeorge Liu             "/", 0, interfaces,
32177a1dbc48SGeorge Liu             [asyncResp](const boost::system::error_code& ec,
32182b20ef6eSJason M. Bills                         const std::vector<std::string>& resp) {
32191da66f75SEd Tanous             if (ec)
32201da66f75SEd Tanous             {
32211da66f75SEd Tanous                 if (ec.value() !=
32221da66f75SEd Tanous                     boost::system::errc::no_such_file_or_directory)
32231da66f75SEd Tanous                 {
32241da66f75SEd Tanous                     BMCWEB_LOG_DEBUG << "failed to get entries ec: "
32251da66f75SEd Tanous                                      << ec.message();
3226f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
32271da66f75SEd Tanous                     return;
32281da66f75SEd Tanous                 }
32291da66f75SEd Tanous             }
3230e1f26343SJason M. Bills             asyncResp->res.jsonValue["@odata.type"] =
32311da66f75SEd Tanous                 "#LogEntryCollection.LogEntryCollection";
32320f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
3233424c4176SJason M. Bills                 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
3234002d39b4SEd Tanous             asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
3235e1f26343SJason M. Bills             asyncResp->res.jsonValue["Description"] =
3236424c4176SJason M. Bills                 "Collection of Crashdump Entries";
3237002d39b4SEd Tanous             asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3238a2dd60a6SBrandon Kim             asyncResp->res.jsonValue["Members@odata.count"] = 0;
32392b20ef6eSJason M. Bills 
32402b20ef6eSJason M. Bills             for (const std::string& path : resp)
32411da66f75SEd Tanous             {
32422b20ef6eSJason M. Bills                 const sdbusplus::message::object_path objPath(path);
3243e855dd28SJason M. Bills                 // Get the log ID
32442b20ef6eSJason M. Bills                 std::string logID = objPath.filename();
32452b20ef6eSJason M. Bills                 if (logID.empty())
32461da66f75SEd Tanous                 {
3247e855dd28SJason M. Bills                     continue;
32481da66f75SEd Tanous                 }
3249e855dd28SJason M. Bills                 // Add the log entry to the array
32502b20ef6eSJason M. Bills                 logCrashdumpEntry(asyncResp, logID,
32512b20ef6eSJason M. Bills                                   asyncResp->res.jsonValue["Members"]);
32521da66f75SEd Tanous             }
32537a1dbc48SGeorge Liu             });
32547e860f15SJohn Edward Broadbent         });
32551da66f75SEd Tanous }
32561da66f75SEd Tanous 
32577e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpEntry(App& app)
32581da66f75SEd Tanous {
32593946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
32603946028dSAppaRao Puli     // method for security reasons.
32611da66f75SEd Tanous 
32627e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
326322d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/")
3264ed398213SEd Tanous         // this is incorrect, should be
3265ed398213SEd Tanous         // .privileges(redfish::privileges::getLogEntry)
3266432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
32677e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
326845ca1b86SEd Tanous             [&app](const crow::Request& req,
32697e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
327022d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
32713ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
327245ca1b86SEd Tanous         {
327345ca1b86SEd Tanous             return;
327445ca1b86SEd Tanous         }
327522d268cbSEd Tanous         if (systemName != "system")
327622d268cbSEd Tanous         {
327722d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
327822d268cbSEd Tanous                                        systemName);
327922d268cbSEd Tanous             return;
328022d268cbSEd Tanous         }
32817e860f15SJohn Edward Broadbent         const std::string& logID = param;
3282e855dd28SJason M. Bills         logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
32837e860f15SJohn Edward Broadbent         });
3284e855dd28SJason M. Bills }
3285e855dd28SJason M. Bills 
32867e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpFile(App& app)
3287e855dd28SJason M. Bills {
32883946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
32893946028dSAppaRao Puli     // method for security reasons.
32907e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
32917e860f15SJohn Edward Broadbent         app,
329222d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/<str>/")
3293ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
32947e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
3295a4ce114aSNan Zhou             [](const crow::Request& req,
32967e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
329722d268cbSEd Tanous                const std::string& systemName, const std::string& logID,
329822d268cbSEd Tanous                const std::string& fileName) {
32992a9beeedSShounak Mitra         // Do not call getRedfishRoute here since the crashdump file is not a
33002a9beeedSShounak Mitra         // Redfish resource.
330122d268cbSEd Tanous 
330222d268cbSEd Tanous         if (systemName != "system")
330322d268cbSEd Tanous         {
330422d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
330522d268cbSEd Tanous                                        systemName);
330622d268cbSEd Tanous             return;
330722d268cbSEd Tanous         }
330822d268cbSEd Tanous 
3309043a0536SJohnathan Mantey         auto getStoredLogCallback =
331039662a3bSEd Tanous             [asyncResp, logID, fileName, url(boost::urls::url(req.url()))](
33115e7e2dc5SEd Tanous                 const boost::system::error_code& ec,
3312002d39b4SEd Tanous                 const std::vector<
3313002d39b4SEd Tanous                     std::pair<std::string, dbus::utility::DbusVariantType>>&
33147e860f15SJohn Edward Broadbent                     resp) {
33151da66f75SEd Tanous             if (ec)
33161da66f75SEd Tanous             {
3317002d39b4SEd Tanous                 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
3318f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
33191da66f75SEd Tanous                 return;
33201da66f75SEd Tanous             }
3321e855dd28SJason M. Bills 
3322043a0536SJohnathan Mantey             std::string dbusFilename{};
3323043a0536SJohnathan Mantey             std::string dbusTimestamp{};
3324043a0536SJohnathan Mantey             std::string dbusFilepath{};
3325043a0536SJohnathan Mantey 
3326002d39b4SEd Tanous             parseCrashdumpParameters(resp, dbusFilename, dbusTimestamp,
3327002d39b4SEd Tanous                                      dbusFilepath);
3328043a0536SJohnathan Mantey 
3329043a0536SJohnathan Mantey             if (dbusFilename.empty() || dbusTimestamp.empty() ||
3330043a0536SJohnathan Mantey                 dbusFilepath.empty())
33311da66f75SEd Tanous             {
33329db4ba25SJiaqing Zhao                 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
33331da66f75SEd Tanous                 return;
33341da66f75SEd Tanous             }
3335e855dd28SJason M. Bills 
3336043a0536SJohnathan Mantey             // Verify the file name parameter is correct
3337043a0536SJohnathan Mantey             if (fileName != dbusFilename)
3338043a0536SJohnathan Mantey             {
33399db4ba25SJiaqing Zhao                 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
3340043a0536SJohnathan Mantey                 return;
3341043a0536SJohnathan Mantey             }
3342043a0536SJohnathan Mantey 
3343043a0536SJohnathan Mantey             if (!std::filesystem::exists(dbusFilepath))
3344043a0536SJohnathan Mantey             {
33459db4ba25SJiaqing Zhao                 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
3346043a0536SJohnathan Mantey                 return;
3347043a0536SJohnathan Mantey             }
3348002d39b4SEd Tanous             std::ifstream ifs(dbusFilepath, std::ios::in | std::ios::binary);
3349002d39b4SEd Tanous             asyncResp->res.body() =
3350002d39b4SEd Tanous                 std::string(std::istreambuf_iterator<char>{ifs}, {});
3351043a0536SJohnathan Mantey 
33527e860f15SJohn Edward Broadbent             // Configure this to be a file download when accessed
33537e860f15SJohn Edward Broadbent             // from a browser
3354d9f6c621SEd Tanous             asyncResp->res.addHeader(
3355d9f6c621SEd Tanous                 boost::beast::http::field::content_disposition, "attachment");
33561da66f75SEd Tanous         };
3357d1bde9e5SKrzysztof Grobelny         sdbusplus::asio::getAllProperties(
3358d1bde9e5SKrzysztof Grobelny             *crow::connections::systemBus, crashdumpObject,
3359d1bde9e5SKrzysztof Grobelny             crashdumpPath + std::string("/") + logID, crashdumpInterface,
3360d1bde9e5SKrzysztof Grobelny             std::move(getStoredLogCallback));
33617e860f15SJohn Edward Broadbent         });
33621da66f75SEd Tanous }
33631da66f75SEd Tanous 
3364c5a4c82aSJason M. Bills enum class OEMDiagnosticType
3365c5a4c82aSJason M. Bills {
3366c5a4c82aSJason M. Bills     onDemand,
3367c5a4c82aSJason M. Bills     telemetry,
3368c5a4c82aSJason M. Bills     invalid,
3369c5a4c82aSJason M. Bills };
3370c5a4c82aSJason M. Bills 
337126ccae32SEd Tanous inline OEMDiagnosticType getOEMDiagnosticType(std::string_view oemDiagStr)
3372c5a4c82aSJason M. Bills {
3373c5a4c82aSJason M. Bills     if (oemDiagStr == "OnDemand")
3374c5a4c82aSJason M. Bills     {
3375c5a4c82aSJason M. Bills         return OEMDiagnosticType::onDemand;
3376c5a4c82aSJason M. Bills     }
3377c5a4c82aSJason M. Bills     if (oemDiagStr == "Telemetry")
3378c5a4c82aSJason M. Bills     {
3379c5a4c82aSJason M. Bills         return OEMDiagnosticType::telemetry;
3380c5a4c82aSJason M. Bills     }
3381c5a4c82aSJason M. Bills 
3382c5a4c82aSJason M. Bills     return OEMDiagnosticType::invalid;
3383c5a4c82aSJason M. Bills }
3384c5a4c82aSJason M. Bills 
33857e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpCollect(App& app)
33861da66f75SEd Tanous {
33873946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
33883946028dSAppaRao Puli     // method for security reasons.
33890fda0f12SGeorge Liu     BMCWEB_ROUTE(
33900fda0f12SGeorge Liu         app,
339122d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData/")
3392ed398213SEd Tanous         // The below is incorrect;  Should be ConfigureManager
3393ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
3394432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
3395002d39b4SEd Tanous         .methods(boost::beast::http::verb::post)(
3396002d39b4SEd Tanous             [&app](const crow::Request& req,
339722d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
339822d268cbSEd Tanous                    const std::string& systemName) {
33993ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
340045ca1b86SEd Tanous         {
340145ca1b86SEd Tanous             return;
340245ca1b86SEd Tanous         }
340322d268cbSEd Tanous 
340422d268cbSEd Tanous         if (systemName != "system")
340522d268cbSEd Tanous         {
340622d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
340722d268cbSEd Tanous                                        systemName);
340822d268cbSEd Tanous             return;
340922d268cbSEd Tanous         }
341022d268cbSEd Tanous 
34118e6c099aSJason M. Bills         std::string diagnosticDataType;
34128e6c099aSJason M. Bills         std::string oemDiagnosticDataType;
341315ed6780SWilly Tu         if (!redfish::json_util::readJsonAction(
3414002d39b4SEd Tanous                 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
3415002d39b4SEd Tanous                 "OEMDiagnosticDataType", oemDiagnosticDataType))
34168e6c099aSJason M. Bills         {
34178e6c099aSJason M. Bills             return;
34188e6c099aSJason M. Bills         }
34198e6c099aSJason M. Bills 
34208e6c099aSJason M. Bills         if (diagnosticDataType != "OEM")
34218e6c099aSJason M. Bills         {
34228e6c099aSJason M. Bills             BMCWEB_LOG_ERROR
34238e6c099aSJason M. Bills                 << "Only OEM DiagnosticDataType supported for Crashdump";
34248e6c099aSJason M. Bills             messages::actionParameterValueFormatError(
34258e6c099aSJason M. Bills                 asyncResp->res, diagnosticDataType, "DiagnosticDataType",
34268e6c099aSJason M. Bills                 "CollectDiagnosticData");
34278e6c099aSJason M. Bills             return;
34288e6c099aSJason M. Bills         }
34298e6c099aSJason M. Bills 
3430c5a4c82aSJason M. Bills         OEMDiagnosticType oemDiagType =
3431c5a4c82aSJason M. Bills             getOEMDiagnosticType(oemDiagnosticDataType);
3432c5a4c82aSJason M. Bills 
3433c5a4c82aSJason M. Bills         std::string iface;
3434c5a4c82aSJason M. Bills         std::string method;
3435c5a4c82aSJason M. Bills         std::string taskMatchStr;
3436c5a4c82aSJason M. Bills         if (oemDiagType == OEMDiagnosticType::onDemand)
3437c5a4c82aSJason M. Bills         {
3438c5a4c82aSJason M. Bills             iface = crashdumpOnDemandInterface;
3439c5a4c82aSJason M. Bills             method = "GenerateOnDemandLog";
3440c5a4c82aSJason M. Bills             taskMatchStr = "type='signal',"
3441c5a4c82aSJason M. Bills                            "interface='org.freedesktop.DBus.Properties',"
3442c5a4c82aSJason M. Bills                            "member='PropertiesChanged',"
3443c5a4c82aSJason M. Bills                            "arg0namespace='com.intel.crashdump'";
3444c5a4c82aSJason M. Bills         }
3445c5a4c82aSJason M. Bills         else if (oemDiagType == OEMDiagnosticType::telemetry)
3446c5a4c82aSJason M. Bills         {
3447c5a4c82aSJason M. Bills             iface = crashdumpTelemetryInterface;
3448c5a4c82aSJason M. Bills             method = "GenerateTelemetryLog";
3449c5a4c82aSJason M. Bills             taskMatchStr = "type='signal',"
3450c5a4c82aSJason M. Bills                            "interface='org.freedesktop.DBus.Properties',"
3451c5a4c82aSJason M. Bills                            "member='PropertiesChanged',"
3452c5a4c82aSJason M. Bills                            "arg0namespace='com.intel.crashdump'";
3453c5a4c82aSJason M. Bills         }
3454c5a4c82aSJason M. Bills         else
3455c5a4c82aSJason M. Bills         {
3456c5a4c82aSJason M. Bills             BMCWEB_LOG_ERROR << "Unsupported OEMDiagnosticDataType: "
3457c5a4c82aSJason M. Bills                              << oemDiagnosticDataType;
3458c5a4c82aSJason M. Bills             messages::actionParameterValueFormatError(
3459002d39b4SEd Tanous                 asyncResp->res, oemDiagnosticDataType, "OEMDiagnosticDataType",
3460002d39b4SEd Tanous                 "CollectDiagnosticData");
3461c5a4c82aSJason M. Bills             return;
3462c5a4c82aSJason M. Bills         }
3463c5a4c82aSJason M. Bills 
3464c5a4c82aSJason M. Bills         auto collectCrashdumpCallback =
3465c5a4c82aSJason M. Bills             [asyncResp, payload(task::Payload(req)),
34665e7e2dc5SEd Tanous              taskMatchStr](const boost::system::error_code& ec,
346798be3e39SEd Tanous                            const std::string&) mutable {
34681da66f75SEd Tanous             if (ec)
34691da66f75SEd Tanous             {
3470002d39b4SEd Tanous                 if (ec.value() == boost::system::errc::operation_not_supported)
34711da66f75SEd Tanous                 {
3472f12894f8SJason M. Bills                     messages::resourceInStandby(asyncResp->res);
34731da66f75SEd Tanous                 }
34744363d3b2SJason M. Bills                 else if (ec.value() ==
34754363d3b2SJason M. Bills                          boost::system::errc::device_or_resource_busy)
34764363d3b2SJason M. Bills                 {
3477002d39b4SEd Tanous                     messages::serviceTemporarilyUnavailable(asyncResp->res,
3478002d39b4SEd Tanous                                                             "60");
34794363d3b2SJason M. Bills                 }
34801da66f75SEd Tanous                 else
34811da66f75SEd Tanous                 {
3482f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
34831da66f75SEd Tanous                 }
34841da66f75SEd Tanous                 return;
34851da66f75SEd Tanous             }
3486002d39b4SEd Tanous             std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
34875e7e2dc5SEd Tanous                 [](const boost::system::error_code& err, sdbusplus::message_t&,
3488002d39b4SEd Tanous                    const std::shared_ptr<task::TaskData>& taskData) {
348966afe4faSJames Feist                 if (!err)
349066afe4faSJames Feist                 {
3491002d39b4SEd Tanous                     taskData->messages.emplace_back(messages::taskCompletedOK(
3492e5d5006bSJames Feist                         std::to_string(taskData->index)));
3493831d6b09SJames Feist                     taskData->state = "Completed";
349466afe4faSJames Feist                 }
349532898ceaSJames Feist                 return task::completed;
349666afe4faSJames Feist                 },
3497c5a4c82aSJason M. Bills                 taskMatchStr);
3498c5a4c82aSJason M. Bills 
349946229577SJames Feist             task->startTimer(std::chrono::minutes(5));
350046229577SJames Feist             task->populateResp(asyncResp->res);
350198be3e39SEd Tanous             task->payload.emplace(std::move(payload));
35021da66f75SEd Tanous         };
35038e6c099aSJason M. Bills 
35041da66f75SEd Tanous         crow::connections::systemBus->async_method_call(
3505002d39b4SEd Tanous             std::move(collectCrashdumpCallback), crashdumpObject, crashdumpPath,
3506002d39b4SEd Tanous             iface, method);
35077e860f15SJohn Edward Broadbent         });
35086eda7685SKenny L. Ku }
35096eda7685SKenny L. Ku 
3510cb92c03bSAndrew Geissler /**
3511cb92c03bSAndrew Geissler  * DBusLogServiceActionsClear class supports POST method for ClearLog action.
3512cb92c03bSAndrew Geissler  */
35137e860f15SJohn Edward Broadbent inline void requestRoutesDBusLogServiceActionsClear(App& app)
3514cb92c03bSAndrew Geissler {
3515cb92c03bSAndrew Geissler     /**
3516cb92c03bSAndrew Geissler      * Function handles POST method request.
3517cb92c03bSAndrew Geissler      * The Clear Log actions does not require any parameter.The action deletes
3518cb92c03bSAndrew Geissler      * all entries found in the Entries collection for this Log Service.
3519cb92c03bSAndrew Geissler      */
35207e860f15SJohn Edward Broadbent 
35210fda0f12SGeorge Liu     BMCWEB_ROUTE(
35220fda0f12SGeorge Liu         app,
352322d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
3524ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
35257e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
352645ca1b86SEd Tanous             [&app](const crow::Request& req,
352722d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
352822d268cbSEd Tanous                    const std::string& systemName) {
35293ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
353045ca1b86SEd Tanous         {
353145ca1b86SEd Tanous             return;
353245ca1b86SEd Tanous         }
353322d268cbSEd Tanous         if (systemName != "system")
353422d268cbSEd Tanous         {
353522d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
353622d268cbSEd Tanous                                        systemName);
353722d268cbSEd Tanous             return;
353822d268cbSEd Tanous         }
3539cb92c03bSAndrew Geissler         BMCWEB_LOG_DEBUG << "Do delete all entries.";
3540cb92c03bSAndrew Geissler 
3541cb92c03bSAndrew Geissler         // Process response from Logging service.
35425e7e2dc5SEd Tanous         auto respHandler = [asyncResp](const boost::system::error_code& ec) {
3543002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "doClearLog resp_handler callback: Done";
3544cb92c03bSAndrew Geissler             if (ec)
3545cb92c03bSAndrew Geissler             {
3546cb92c03bSAndrew Geissler                 // TODO Handle for specific error code
3547002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "doClearLog resp_handler got error " << ec;
3548cb92c03bSAndrew Geissler                 asyncResp->res.result(
3549cb92c03bSAndrew Geissler                     boost::beast::http::status::internal_server_error);
3550cb92c03bSAndrew Geissler                 return;
3551cb92c03bSAndrew Geissler             }
3552cb92c03bSAndrew Geissler 
3553002d39b4SEd Tanous             asyncResp->res.result(boost::beast::http::status::no_content);
3554cb92c03bSAndrew Geissler         };
3555cb92c03bSAndrew Geissler 
3556cb92c03bSAndrew Geissler         // Make call to Logging service to request Clear Log
3557cb92c03bSAndrew Geissler         crow::connections::systemBus->async_method_call(
35582c70f800SEd Tanous             respHandler, "xyz.openbmc_project.Logging",
3559cb92c03bSAndrew Geissler             "/xyz/openbmc_project/logging",
3560cb92c03bSAndrew Geissler             "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
35617e860f15SJohn Edward Broadbent         });
3562cb92c03bSAndrew Geissler }
3563a3316fc6SZhikuiRen 
3564a3316fc6SZhikuiRen /****************************************************
3565a3316fc6SZhikuiRen  * Redfish PostCode interfaces
3566a3316fc6SZhikuiRen  * using DBUS interface: getPostCodesTS
3567a3316fc6SZhikuiRen  ******************************************************/
35687e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesLogService(App& app)
3569a3316fc6SZhikuiRen {
357022d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/")
3571ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
3572002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
3573002d39b4SEd Tanous             [&app](const crow::Request& req,
357422d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
357522d268cbSEd Tanous                    const std::string& systemName) {
35763ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
357745ca1b86SEd Tanous         {
357845ca1b86SEd Tanous             return;
357945ca1b86SEd Tanous         }
358022d268cbSEd Tanous         if (systemName != "system")
358122d268cbSEd Tanous         {
358222d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
358322d268cbSEd Tanous                                        systemName);
358422d268cbSEd Tanous             return;
358522d268cbSEd Tanous         }
35861476687dSEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
35871476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/PostCodes";
35881476687dSEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
35891476687dSEd Tanous             "#LogService.v1_1_0.LogService";
35901476687dSEd Tanous         asyncResp->res.jsonValue["Name"] = "POST Code Log Service";
35911476687dSEd Tanous         asyncResp->res.jsonValue["Description"] = "POST Code Log Service";
3592ed34a4adSEd Tanous         asyncResp->res.jsonValue["Id"] = "PostCodes";
35931476687dSEd Tanous         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
35941476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
35951476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
35967c8c4058STejas Patil 
35977c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
35982b82937eSEd Tanous             redfish::time_utils::getDateTimeOffsetNow();
35990fda0f12SGeorge Liu         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
36007c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
36017c8c4058STejas Patil             redfishDateTimeOffset.second;
36027c8c4058STejas Patil 
3603a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
36047e860f15SJohn Edward Broadbent             {"target",
36050fda0f12SGeorge Liu              "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog"}};
36067e860f15SJohn Edward Broadbent         });
3607a3316fc6SZhikuiRen }
3608a3316fc6SZhikuiRen 
36097e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesClear(App& app)
3610a3316fc6SZhikuiRen {
36110fda0f12SGeorge Liu     BMCWEB_ROUTE(
36120fda0f12SGeorge Liu         app,
361322d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/PostCodes/Actions/LogService.ClearLog/")
3614ed398213SEd Tanous         // The following privilege is incorrect;  It should be ConfigureManager
3615ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
3616432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
36177e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
361845ca1b86SEd Tanous             [&app](const crow::Request& req,
361922d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
362022d268cbSEd Tanous                    const std::string& systemName) {
36213ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
362245ca1b86SEd Tanous         {
362345ca1b86SEd Tanous             return;
362445ca1b86SEd Tanous         }
362522d268cbSEd Tanous         if (systemName != "system")
362622d268cbSEd Tanous         {
362722d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
362822d268cbSEd Tanous                                        systemName);
362922d268cbSEd Tanous             return;
363022d268cbSEd Tanous         }
3631a3316fc6SZhikuiRen         BMCWEB_LOG_DEBUG << "Do delete all postcodes entries.";
3632a3316fc6SZhikuiRen 
3633a3316fc6SZhikuiRen         // Make call to post-code service to request clear all
3634a3316fc6SZhikuiRen         crow::connections::systemBus->async_method_call(
36355e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec) {
3636a3316fc6SZhikuiRen             if (ec)
3637a3316fc6SZhikuiRen             {
3638a3316fc6SZhikuiRen                 // TODO Handle for specific error code
3639002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "doClearPostCodes resp_handler got error "
36407e860f15SJohn Edward Broadbent                                  << ec;
3641002d39b4SEd Tanous                 asyncResp->res.result(
3642002d39b4SEd Tanous                     boost::beast::http::status::internal_server_error);
3643a3316fc6SZhikuiRen                 messages::internalError(asyncResp->res);
3644a3316fc6SZhikuiRen                 return;
3645a3316fc6SZhikuiRen             }
3646a3316fc6SZhikuiRen             },
364715124765SJonathan Doman             "xyz.openbmc_project.State.Boot.PostCode0",
364815124765SJonathan Doman             "/xyz/openbmc_project/State/Boot/PostCode0",
3649a3316fc6SZhikuiRen             "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
36507e860f15SJohn Edward Broadbent         });
3651a3316fc6SZhikuiRen }
3652a3316fc6SZhikuiRen 
36536f284d24SJiaqing Zhao /**
36546f284d24SJiaqing Zhao  * @brief Parse post code ID and get the current value and index value
36556f284d24SJiaqing Zhao  *        eg: postCodeID=B1-2, currentValue=1, index=2
36566f284d24SJiaqing Zhao  *
36576f284d24SJiaqing Zhao  * @param[in]  postCodeID     Post Code ID
36586f284d24SJiaqing Zhao  * @param[out] currentValue   Current value
36596f284d24SJiaqing Zhao  * @param[out] index          Index value
36606f284d24SJiaqing Zhao  *
36616f284d24SJiaqing Zhao  * @return bool true if the parsing is successful, false the parsing fails
36626f284d24SJiaqing Zhao  */
36636f284d24SJiaqing Zhao inline static bool parsePostCode(const std::string& postCodeID,
36646f284d24SJiaqing Zhao                                  uint64_t& currentValue, uint16_t& index)
36656f284d24SJiaqing Zhao {
36666f284d24SJiaqing Zhao     std::vector<std::string> split;
366750ebd4afSEd Tanous     bmcweb::split(split, postCodeID, '-');
36686f284d24SJiaqing Zhao     if (split.size() != 2 || split[0].length() < 2 || split[0].front() != 'B')
36696f284d24SJiaqing Zhao     {
36706f284d24SJiaqing Zhao         return false;
36716f284d24SJiaqing Zhao     }
36726f284d24SJiaqing Zhao 
367384396af9SPatrick Williams     auto start = std::next(split[0].begin());
367484396af9SPatrick Williams     auto end = split[0].end();
367584396af9SPatrick Williams     auto [ptrIndex, ecIndex] = std::from_chars(&*start, &*end, index);
36766f284d24SJiaqing Zhao 
367784396af9SPatrick Williams     if (ptrIndex != &*end || ecIndex != std::errc())
36786f284d24SJiaqing Zhao     {
36796f284d24SJiaqing Zhao         return false;
36806f284d24SJiaqing Zhao     }
36816f284d24SJiaqing Zhao 
368284396af9SPatrick Williams     start = split[1].begin();
368384396af9SPatrick Williams     end = split[1].end();
36846f284d24SJiaqing Zhao 
368584396af9SPatrick Williams     auto [ptrValue, ecValue] = std::from_chars(&*start, &*end, currentValue);
36866f284d24SJiaqing Zhao 
368784396af9SPatrick Williams     return ptrValue == &*end && ecValue == std::errc();
36886f284d24SJiaqing Zhao }
36896f284d24SJiaqing Zhao 
36906f284d24SJiaqing Zhao static bool fillPostCodeEntry(
3691ac106bf6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
36926c9a279eSManojkiran Eda     const boost::container::flat_map<
36936c9a279eSManojkiran Eda         uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
3694a3316fc6SZhikuiRen     const uint16_t bootIndex, const uint64_t codeIndex = 0,
3695a3316fc6SZhikuiRen     const uint64_t skip = 0, const uint64_t top = 0)
3696a3316fc6SZhikuiRen {
3697a3316fc6SZhikuiRen     // Get the Message from the MessageRegistry
3698fffb8c1fSEd Tanous     const registries::Message* message =
3699fffb8c1fSEd Tanous         registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
3700a3316fc6SZhikuiRen 
3701a3316fc6SZhikuiRen     uint64_t currentCodeIndex = 0;
3702a3316fc6SZhikuiRen     uint64_t firstCodeTimeUs = 0;
37036c9a279eSManojkiran Eda     for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
37046c9a279eSManojkiran Eda              code : postcode)
3705a3316fc6SZhikuiRen     {
3706a3316fc6SZhikuiRen         currentCodeIndex++;
3707a3316fc6SZhikuiRen         std::string postcodeEntryID =
3708a3316fc6SZhikuiRen             "B" + std::to_string(bootIndex) + "-" +
3709a3316fc6SZhikuiRen             std::to_string(currentCodeIndex); // 1 based index in EntryID string
3710a3316fc6SZhikuiRen 
3711a3316fc6SZhikuiRen         uint64_t usecSinceEpoch = code.first;
3712a3316fc6SZhikuiRen         uint64_t usTimeOffset = 0;
3713a3316fc6SZhikuiRen 
3714a3316fc6SZhikuiRen         if (1 == currentCodeIndex)
3715a3316fc6SZhikuiRen         { // already incremented
3716a3316fc6SZhikuiRen             firstCodeTimeUs = code.first;
3717a3316fc6SZhikuiRen         }
3718a3316fc6SZhikuiRen         else
3719a3316fc6SZhikuiRen         {
3720a3316fc6SZhikuiRen             usTimeOffset = code.first - firstCodeTimeUs;
3721a3316fc6SZhikuiRen         }
3722a3316fc6SZhikuiRen 
3723a3316fc6SZhikuiRen         // skip if no specific codeIndex is specified and currentCodeIndex does
3724a3316fc6SZhikuiRen         // not fall between top and skip
3725a3316fc6SZhikuiRen         if ((codeIndex == 0) &&
3726a3316fc6SZhikuiRen             (currentCodeIndex <= skip || currentCodeIndex > top))
3727a3316fc6SZhikuiRen         {
3728a3316fc6SZhikuiRen             continue;
3729a3316fc6SZhikuiRen         }
3730a3316fc6SZhikuiRen 
37314e0453b1SGunnar Mills         // skip if a specific codeIndex is specified and does not match the
3732a3316fc6SZhikuiRen         // currentIndex
3733a3316fc6SZhikuiRen         if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
3734a3316fc6SZhikuiRen         {
3735a3316fc6SZhikuiRen             // This is done for simplicity. 1st entry is needed to calculate
3736a3316fc6SZhikuiRen             // time offset. To improve efficiency, one can get to the entry
3737a3316fc6SZhikuiRen             // directly (possibly with flatmap's nth method)
3738a3316fc6SZhikuiRen             continue;
3739a3316fc6SZhikuiRen         }
3740a3316fc6SZhikuiRen 
3741a3316fc6SZhikuiRen         // currentCodeIndex is within top and skip or equal to specified code
3742a3316fc6SZhikuiRen         // index
3743a3316fc6SZhikuiRen 
3744a3316fc6SZhikuiRen         // Get the Created time from the timestamp
3745a3316fc6SZhikuiRen         std::string entryTimeStr;
37462a025611SKonstantin Aladyshev         entryTimeStr = redfish::time_utils::getDateTimeUintUs(usecSinceEpoch);
3747a3316fc6SZhikuiRen 
3748a3316fc6SZhikuiRen         // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
3749a3316fc6SZhikuiRen         std::ostringstream hexCode;
3750a3316fc6SZhikuiRen         hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
37516c9a279eSManojkiran Eda                 << std::get<0>(code.second);
3752a3316fc6SZhikuiRen         std::ostringstream timeOffsetStr;
3753a3316fc6SZhikuiRen         // Set Fixed -Point Notation
3754a3316fc6SZhikuiRen         timeOffsetStr << std::fixed;
3755a3316fc6SZhikuiRen         // Set precision to 4 digits
3756a3316fc6SZhikuiRen         timeOffsetStr << std::setprecision(4);
3757a3316fc6SZhikuiRen         // Add double to stream
3758a3316fc6SZhikuiRen         timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
3759a3316fc6SZhikuiRen         std::vector<std::string> messageArgs = {
3760a3316fc6SZhikuiRen             std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()};
3761a3316fc6SZhikuiRen 
3762a3316fc6SZhikuiRen         // Get MessageArgs template from message registry
3763a3316fc6SZhikuiRen         std::string msg;
3764a3316fc6SZhikuiRen         if (message != nullptr)
3765a3316fc6SZhikuiRen         {
3766a3316fc6SZhikuiRen             msg = message->message;
3767a3316fc6SZhikuiRen 
3768a3316fc6SZhikuiRen             // fill in this post code value
3769a3316fc6SZhikuiRen             int i = 0;
3770a3316fc6SZhikuiRen             for (const std::string& messageArg : messageArgs)
3771a3316fc6SZhikuiRen             {
3772a3316fc6SZhikuiRen                 std::string argStr = "%" + std::to_string(++i);
3773a3316fc6SZhikuiRen                 size_t argPos = msg.find(argStr);
3774a3316fc6SZhikuiRen                 if (argPos != std::string::npos)
3775a3316fc6SZhikuiRen                 {
3776a3316fc6SZhikuiRen                     msg.replace(argPos, argStr.length(), messageArg);
3777a3316fc6SZhikuiRen                 }
3778a3316fc6SZhikuiRen             }
3779a3316fc6SZhikuiRen         }
3780a3316fc6SZhikuiRen 
3781d4342a92STim Lee         // Get Severity template from message registry
3782d4342a92STim Lee         std::string severity;
3783d4342a92STim Lee         if (message != nullptr)
3784d4342a92STim Lee         {
37855f2b84eeSEd Tanous             severity = message->messageSeverity;
3786d4342a92STim Lee         }
3787d4342a92STim Lee 
37886f284d24SJiaqing Zhao         // Format entry
37896f284d24SJiaqing Zhao         nlohmann::json::object_t bmcLogEntry;
37909c11a172SVijay Lobo         bmcLogEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
3791ef4c65b7SEd Tanous         bmcLogEntry["@odata.id"] = boost::urls::format(
3792ef4c65b7SEd Tanous             "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/{}",
3793ef4c65b7SEd Tanous             postcodeEntryID);
379484afc48bSJason M. Bills         bmcLogEntry["Name"] = "POST Code Log Entry";
379584afc48bSJason M. Bills         bmcLogEntry["Id"] = postcodeEntryID;
379684afc48bSJason M. Bills         bmcLogEntry["Message"] = std::move(msg);
379784afc48bSJason M. Bills         bmcLogEntry["MessageId"] = "OpenBMC.0.2.BIOSPOSTCode";
379884afc48bSJason M. Bills         bmcLogEntry["MessageArgs"] = std::move(messageArgs);
379984afc48bSJason M. Bills         bmcLogEntry["EntryType"] = "Event";
380084afc48bSJason M. Bills         bmcLogEntry["Severity"] = std::move(severity);
380184afc48bSJason M. Bills         bmcLogEntry["Created"] = entryTimeStr;
3802647b3cdcSGeorge Liu         if (!std::get<std::vector<uint8_t>>(code.second).empty())
3803647b3cdcSGeorge Liu         {
3804647b3cdcSGeorge Liu             bmcLogEntry["AdditionalDataURI"] =
3805647b3cdcSGeorge Liu                 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3806647b3cdcSGeorge Liu                 postcodeEntryID + "/attachment";
3807647b3cdcSGeorge Liu         }
38086f284d24SJiaqing Zhao 
38096f284d24SJiaqing Zhao         // codeIndex is only specified when querying single entry, return only
38106f284d24SJiaqing Zhao         // that entry in this case
38116f284d24SJiaqing Zhao         if (codeIndex != 0)
38126f284d24SJiaqing Zhao         {
3813ac106bf6SEd Tanous             asyncResp->res.jsonValue.update(bmcLogEntry);
38146f284d24SJiaqing Zhao             return true;
3815a3316fc6SZhikuiRen         }
38166f284d24SJiaqing Zhao 
3817ac106bf6SEd Tanous         nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
3818b2ba3072SPatrick Williams         logEntryArray.emplace_back(std::move(bmcLogEntry));
38196f284d24SJiaqing Zhao     }
38206f284d24SJiaqing Zhao 
38216f284d24SJiaqing Zhao     // Return value is always false when querying multiple entries
38226f284d24SJiaqing Zhao     return false;
3823a3316fc6SZhikuiRen }
3824a3316fc6SZhikuiRen 
3825ac106bf6SEd Tanous static void
3826ac106bf6SEd Tanous     getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
38276f284d24SJiaqing Zhao                         const std::string& entryId)
3828a3316fc6SZhikuiRen {
38296f284d24SJiaqing Zhao     uint16_t bootIndex = 0;
38306f284d24SJiaqing Zhao     uint64_t codeIndex = 0;
38316f284d24SJiaqing Zhao     if (!parsePostCode(entryId, codeIndex, bootIndex))
38326f284d24SJiaqing Zhao     {
38336f284d24SJiaqing Zhao         // Requested ID was not found
3834ac106bf6SEd Tanous         messages::resourceNotFound(asyncResp->res, "LogEntry", entryId);
38356f284d24SJiaqing Zhao         return;
38366f284d24SJiaqing Zhao     }
38376f284d24SJiaqing Zhao 
38386f284d24SJiaqing Zhao     if (bootIndex == 0 || codeIndex == 0)
38396f284d24SJiaqing Zhao     {
38406f284d24SJiaqing Zhao         // 0 is an invalid index
3841ac106bf6SEd Tanous         messages::resourceNotFound(asyncResp->res, "LogEntry", entryId);
38426f284d24SJiaqing Zhao         return;
38436f284d24SJiaqing Zhao     }
38446f284d24SJiaqing Zhao 
3845a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
3846ac106bf6SEd Tanous         [asyncResp, entryId, bootIndex,
38475e7e2dc5SEd Tanous          codeIndex](const boost::system::error_code& ec,
38486c9a279eSManojkiran Eda                     const boost::container::flat_map<
38496c9a279eSManojkiran Eda                         uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
38506c9a279eSManojkiran Eda                         postcode) {
3851a3316fc6SZhikuiRen         if (ec)
3852a3316fc6SZhikuiRen         {
3853a3316fc6SZhikuiRen             BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3854ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
3855a3316fc6SZhikuiRen             return;
3856a3316fc6SZhikuiRen         }
3857a3316fc6SZhikuiRen 
3858a3316fc6SZhikuiRen         if (postcode.empty())
3859a3316fc6SZhikuiRen         {
3860ac106bf6SEd Tanous             messages::resourceNotFound(asyncResp->res, "LogEntry", entryId);
3861a3316fc6SZhikuiRen             return;
3862a3316fc6SZhikuiRen         }
3863a3316fc6SZhikuiRen 
3864ac106bf6SEd Tanous         if (!fillPostCodeEntry(asyncResp, postcode, bootIndex, codeIndex))
38656f284d24SJiaqing Zhao         {
3866ac106bf6SEd Tanous             messages::resourceNotFound(asyncResp->res, "LogEntry", entryId);
38676f284d24SJiaqing Zhao             return;
38686f284d24SJiaqing Zhao         }
3869a3316fc6SZhikuiRen         },
387015124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
387115124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
3872a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3873a3316fc6SZhikuiRen         bootIndex);
3874a3316fc6SZhikuiRen }
3875a3316fc6SZhikuiRen 
3876ac106bf6SEd Tanous static void
3877ac106bf6SEd Tanous     getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3878ac106bf6SEd Tanous                        const uint16_t bootIndex, const uint16_t bootCount,
3879ac106bf6SEd Tanous                        const uint64_t entryCount, size_t skip, size_t top)
3880a3316fc6SZhikuiRen {
3881a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
3882ac106bf6SEd Tanous         [asyncResp, bootIndex, bootCount, entryCount, skip,
38835e7e2dc5SEd Tanous          top](const boost::system::error_code& ec,
38846c9a279eSManojkiran Eda               const boost::container::flat_map<
38856c9a279eSManojkiran Eda                   uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
38866c9a279eSManojkiran Eda                   postcode) {
3887a3316fc6SZhikuiRen         if (ec)
3888a3316fc6SZhikuiRen         {
3889a3316fc6SZhikuiRen             BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3890ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
3891a3316fc6SZhikuiRen             return;
3892a3316fc6SZhikuiRen         }
3893a3316fc6SZhikuiRen 
3894a3316fc6SZhikuiRen         uint64_t endCount = entryCount;
3895a3316fc6SZhikuiRen         if (!postcode.empty())
3896a3316fc6SZhikuiRen         {
3897a3316fc6SZhikuiRen             endCount = entryCount + postcode.size();
38983648c8beSEd Tanous             if (skip < endCount && (top + skip) > entryCount)
3899a3316fc6SZhikuiRen             {
390089492a15SPatrick Williams                 uint64_t thisBootSkip = std::max(static_cast<uint64_t>(skip),
390189492a15SPatrick Williams                                                  entryCount) -
39023648c8beSEd Tanous                                         entryCount;
3903a3316fc6SZhikuiRen                 uint64_t thisBootTop =
39043648c8beSEd Tanous                     std::min(static_cast<uint64_t>(top + skip), endCount) -
39053648c8beSEd Tanous                     entryCount;
3906a3316fc6SZhikuiRen 
3907ac106bf6SEd Tanous                 fillPostCodeEntry(asyncResp, postcode, bootIndex, 0,
3908ac106bf6SEd Tanous                                   thisBootSkip, thisBootTop);
3909a3316fc6SZhikuiRen             }
3910ac106bf6SEd Tanous             asyncResp->res.jsonValue["Members@odata.count"] = endCount;
3911a3316fc6SZhikuiRen         }
3912a3316fc6SZhikuiRen 
3913a3316fc6SZhikuiRen         // continue to previous bootIndex
3914a3316fc6SZhikuiRen         if (bootIndex < bootCount)
3915a3316fc6SZhikuiRen         {
3916ac106bf6SEd Tanous             getPostCodeForBoot(asyncResp, static_cast<uint16_t>(bootIndex + 1),
3917a3316fc6SZhikuiRen                                bootCount, endCount, skip, top);
3918a3316fc6SZhikuiRen         }
391981584abeSJiaqing Zhao         else if (skip + top < endCount)
3920a3316fc6SZhikuiRen         {
3921ac106bf6SEd Tanous             asyncResp->res.jsonValue["Members@odata.nextLink"] =
39220fda0f12SGeorge Liu                 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries?$skip=" +
3923a3316fc6SZhikuiRen                 std::to_string(skip + top);
3924a3316fc6SZhikuiRen         }
3925a3316fc6SZhikuiRen         },
392615124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
392715124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
3928a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3929a3316fc6SZhikuiRen         bootIndex);
3930a3316fc6SZhikuiRen }
3931a3316fc6SZhikuiRen 
39328d1b46d7Szhanghch05 static void
3933ac106bf6SEd Tanous     getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
39343648c8beSEd Tanous                          size_t skip, size_t top)
3935a3316fc6SZhikuiRen {
3936a3316fc6SZhikuiRen     uint64_t entryCount = 0;
39371e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint16_t>(
39381e1e598dSJonathan Doman         *crow::connections::systemBus,
39391e1e598dSJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
39401e1e598dSJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
39411e1e598dSJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount",
3942ac106bf6SEd Tanous         [asyncResp, entryCount, skip, top](const boost::system::error_code& ec,
39431e1e598dSJonathan Doman                                            const uint16_t bootCount) {
3944a3316fc6SZhikuiRen         if (ec)
3945a3316fc6SZhikuiRen         {
3946a3316fc6SZhikuiRen             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3947ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
3948a3316fc6SZhikuiRen             return;
3949a3316fc6SZhikuiRen         }
3950ac106bf6SEd Tanous         getPostCodeForBoot(asyncResp, 1, bootCount, entryCount, skip, top);
39511e1e598dSJonathan Doman         });
3952a3316fc6SZhikuiRen }
3953a3316fc6SZhikuiRen 
39547e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesEntryCollection(App& app)
3955a3316fc6SZhikuiRen {
39567e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
395722d268cbSEd Tanous                  "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/")
3958ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
39597e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
396045ca1b86SEd Tanous             [&app](const crow::Request& req,
396122d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
396222d268cbSEd Tanous                    const std::string& systemName) {
3963c937d2bfSEd Tanous         query_param::QueryCapabilities capabilities = {
3964c937d2bfSEd Tanous             .canDelegateTop = true,
3965c937d2bfSEd Tanous             .canDelegateSkip = true,
3966c937d2bfSEd Tanous         };
3967c937d2bfSEd Tanous         query_param::Query delegatedQuery;
3968c937d2bfSEd Tanous         if (!redfish::setUpRedfishRouteWithDelegation(
39693ba00073SCarson Labrado                 app, req, asyncResp, delegatedQuery, capabilities))
397045ca1b86SEd Tanous         {
397145ca1b86SEd Tanous             return;
397245ca1b86SEd Tanous         }
397322d268cbSEd Tanous 
397422d268cbSEd Tanous         if (systemName != "system")
397522d268cbSEd Tanous         {
397622d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
397722d268cbSEd Tanous                                        systemName);
397822d268cbSEd Tanous             return;
397922d268cbSEd Tanous         }
3980a3316fc6SZhikuiRen         asyncResp->res.jsonValue["@odata.type"] =
3981a3316fc6SZhikuiRen             "#LogEntryCollection.LogEntryCollection";
3982a3316fc6SZhikuiRen         asyncResp->res.jsonValue["@odata.id"] =
3983a3316fc6SZhikuiRen             "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3984a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3985a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Description"] =
3986a3316fc6SZhikuiRen             "Collection of POST Code Log Entries";
3987a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3988a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Members@odata.count"] = 0;
39893648c8beSEd Tanous         size_t skip = delegatedQuery.skip.value_or(0);
39905143f7a5SJiaqing Zhao         size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
39913648c8beSEd Tanous         getCurrentBootNumber(asyncResp, skip, top);
39927e860f15SJohn Edward Broadbent         });
3993a3316fc6SZhikuiRen }
3994a3316fc6SZhikuiRen 
3995647b3cdcSGeorge Liu inline void requestRoutesPostCodesEntryAdditionalData(App& app)
3996647b3cdcSGeorge Liu {
39970fda0f12SGeorge Liu     BMCWEB_ROUTE(
39980fda0f12SGeorge Liu         app,
399922d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/attachment/")
4000647b3cdcSGeorge Liu         .privileges(redfish::privileges::getLogEntry)
4001647b3cdcSGeorge Liu         .methods(boost::beast::http::verb::get)(
400245ca1b86SEd Tanous             [&app](const crow::Request& req,
4003647b3cdcSGeorge Liu                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
400422d268cbSEd Tanous                    const std::string& systemName,
4005647b3cdcSGeorge Liu                    const std::string& postCodeID) {
40063ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
400745ca1b86SEd Tanous         {
400845ca1b86SEd Tanous             return;
400945ca1b86SEd Tanous         }
401072e21377SMatt Spinler         if (!http_helpers::isContentTypeAllowed(
401199351cd8SEd Tanous                 req.getHeaderValue("Accept"),
40124a0e1a0cSEd Tanous                 http_helpers::ContentType::OctetStream, true))
4013647b3cdcSGeorge Liu         {
4014002d39b4SEd Tanous             asyncResp->res.result(boost::beast::http::status::bad_request);
4015647b3cdcSGeorge Liu             return;
4016647b3cdcSGeorge Liu         }
401722d268cbSEd Tanous         if (systemName != "system")
401822d268cbSEd Tanous         {
401922d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
402022d268cbSEd Tanous                                        systemName);
402122d268cbSEd Tanous             return;
402222d268cbSEd Tanous         }
4023647b3cdcSGeorge Liu 
4024647b3cdcSGeorge Liu         uint64_t currentValue = 0;
4025647b3cdcSGeorge Liu         uint16_t index = 0;
4026647b3cdcSGeorge Liu         if (!parsePostCode(postCodeID, currentValue, index))
4027647b3cdcSGeorge Liu         {
4028002d39b4SEd Tanous             messages::resourceNotFound(asyncResp->res, "LogEntry", postCodeID);
4029647b3cdcSGeorge Liu             return;
4030647b3cdcSGeorge Liu         }
4031647b3cdcSGeorge Liu 
4032647b3cdcSGeorge Liu         crow::connections::systemBus->async_method_call(
4033647b3cdcSGeorge Liu             [asyncResp, postCodeID, currentValue](
40345e7e2dc5SEd Tanous                 const boost::system::error_code& ec,
4035002d39b4SEd Tanous                 const std::vector<std::tuple<uint64_t, std::vector<uint8_t>>>&
4036002d39b4SEd Tanous                     postcodes) {
4037647b3cdcSGeorge Liu             if (ec.value() == EBADR)
4038647b3cdcSGeorge Liu             {
4039002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry",
4040002d39b4SEd Tanous                                            postCodeID);
4041647b3cdcSGeorge Liu                 return;
4042647b3cdcSGeorge Liu             }
4043647b3cdcSGeorge Liu             if (ec)
4044647b3cdcSGeorge Liu             {
4045647b3cdcSGeorge Liu                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
4046647b3cdcSGeorge Liu                 messages::internalError(asyncResp->res);
4047647b3cdcSGeorge Liu                 return;
4048647b3cdcSGeorge Liu             }
4049647b3cdcSGeorge Liu 
4050647b3cdcSGeorge Liu             size_t value = static_cast<size_t>(currentValue) - 1;
4051002d39b4SEd Tanous             if (value == std::string::npos || postcodes.size() < currentValue)
4052647b3cdcSGeorge Liu             {
4053a7405d5fSGunnar Mills                 BMCWEB_LOG_WARNING << "Wrong currentValue value";
4054002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry",
4055002d39b4SEd Tanous                                            postCodeID);
4056647b3cdcSGeorge Liu                 return;
4057647b3cdcSGeorge Liu             }
4058647b3cdcSGeorge Liu 
40599eb808c1SEd Tanous             const auto& [tID, c] = postcodes[value];
406046ff87baSEd Tanous             if (c.empty())
4061647b3cdcSGeorge Liu             {
4062a7405d5fSGunnar Mills                 BMCWEB_LOG_WARNING << "No found post code data";
4063002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry",
4064002d39b4SEd Tanous                                            postCodeID);
4065647b3cdcSGeorge Liu                 return;
4066647b3cdcSGeorge Liu             }
406746ff87baSEd Tanous             // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
406846ff87baSEd Tanous             const char* d = reinterpret_cast<const char*>(c.data());
406946ff87baSEd Tanous             std::string_view strData(d, c.size());
4070647b3cdcSGeorge Liu 
4071d9f6c621SEd Tanous             asyncResp->res.addHeader(boost::beast::http::field::content_type,
4072647b3cdcSGeorge Liu                                      "application/octet-stream");
4073d9f6c621SEd Tanous             asyncResp->res.addHeader(
4074d9f6c621SEd Tanous                 boost::beast::http::field::content_transfer_encoding, "Base64");
4075002d39b4SEd Tanous             asyncResp->res.body() = crow::utility::base64encode(strData);
4076647b3cdcSGeorge Liu             },
4077647b3cdcSGeorge Liu             "xyz.openbmc_project.State.Boot.PostCode0",
4078647b3cdcSGeorge Liu             "/xyz/openbmc_project/State/Boot/PostCode0",
4079002d39b4SEd Tanous             "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes", index);
4080647b3cdcSGeorge Liu         });
4081647b3cdcSGeorge Liu }
4082647b3cdcSGeorge Liu 
40837e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesEntry(App& app)
4084a3316fc6SZhikuiRen {
40857e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
408622d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/")
4087ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
40887e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
408945ca1b86SEd Tanous             [&app](const crow::Request& req,
40907e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
409122d268cbSEd Tanous                    const std::string& systemName, const std::string& targetID) {
40923ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
409345ca1b86SEd Tanous         {
409445ca1b86SEd Tanous             return;
409545ca1b86SEd Tanous         }
409622d268cbSEd Tanous         if (systemName != "system")
409722d268cbSEd Tanous         {
409822d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
409922d268cbSEd Tanous                                        systemName);
410022d268cbSEd Tanous             return;
410122d268cbSEd Tanous         }
410222d268cbSEd Tanous 
41036f284d24SJiaqing Zhao         getPostCodeForEntry(asyncResp, targetID);
41047e860f15SJohn Edward Broadbent         });
4105a3316fc6SZhikuiRen }
4106a3316fc6SZhikuiRen 
41071da66f75SEd Tanous } // namespace redfish
4108