xref: /openbmc/bmcweb/features/redfish/lib/log_services.hpp (revision 62598e31d0988d589506d5091bd38f72d61faf5e)
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     {
162*62598e31SEd Tanous         BMCWEB_LOG_ERROR("Failed to read entry timestamp: {}", strerror(-ret));
163a3316fc6SZhikuiRen         return false;
164a3316fc6SZhikuiRen     }
165e645c5e6SKonstantin Aladyshev     entryTimestamp = redfish::time_utils::getDateTimeUintUs(timestamp);
1669c620e21SAsmitha Karunanithi     return true;
167a3316fc6SZhikuiRen }
16850b8a43aSEd Tanous 
1697e860f15SJohn Edward Broadbent inline static bool getUniqueEntryID(sd_journal* journal, std::string& entryID,
170e85d6b16SJason M. Bills                                     const bool firstEntry = true)
17116428a1aSJason M. Bills {
17216428a1aSJason M. Bills     int ret = 0;
17316428a1aSJason M. Bills     static uint64_t prevTs = 0;
17416428a1aSJason M. Bills     static int index = 0;
175e85d6b16SJason M. Bills     if (firstEntry)
176e85d6b16SJason M. Bills     {
177e85d6b16SJason M. Bills         prevTs = 0;
178e85d6b16SJason M. Bills     }
179e85d6b16SJason M. Bills 
18016428a1aSJason M. Bills     // Get the entry timestamp
18116428a1aSJason M. Bills     uint64_t curTs = 0;
18216428a1aSJason M. Bills     ret = sd_journal_get_realtime_usec(journal, &curTs);
18316428a1aSJason M. Bills     if (ret < 0)
18416428a1aSJason M. Bills     {
185*62598e31SEd Tanous         BMCWEB_LOG_ERROR("Failed to read entry timestamp: {}", strerror(-ret));
18616428a1aSJason M. Bills         return false;
18716428a1aSJason M. Bills     }
18816428a1aSJason M. Bills     // If the timestamp isn't unique, increment the index
18916428a1aSJason M. Bills     if (curTs == prevTs)
19016428a1aSJason M. Bills     {
19116428a1aSJason M. Bills         index++;
19216428a1aSJason M. Bills     }
19316428a1aSJason M. Bills     else
19416428a1aSJason M. Bills     {
19516428a1aSJason M. Bills         // Otherwise, reset it
19616428a1aSJason M. Bills         index = 0;
19716428a1aSJason M. Bills     }
19816428a1aSJason M. Bills     // Save the timestamp
19916428a1aSJason M. Bills     prevTs = curTs;
20016428a1aSJason M. Bills 
20116428a1aSJason M. Bills     entryID = std::to_string(curTs);
20216428a1aSJason M. Bills     if (index > 0)
20316428a1aSJason M. Bills     {
20416428a1aSJason M. Bills         entryID += "_" + std::to_string(index);
20516428a1aSJason M. Bills     }
20616428a1aSJason M. Bills     return true;
20716428a1aSJason M. Bills }
20816428a1aSJason M. Bills 
209e85d6b16SJason M. Bills static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
210e85d6b16SJason M. Bills                              const bool firstEntry = true)
21195820184SJason M. Bills {
212271584abSEd Tanous     static time_t prevTs = 0;
21395820184SJason M. Bills     static int index = 0;
214e85d6b16SJason M. Bills     if (firstEntry)
215e85d6b16SJason M. Bills     {
216e85d6b16SJason M. Bills         prevTs = 0;
217e85d6b16SJason M. Bills     }
218e85d6b16SJason M. Bills 
21995820184SJason M. Bills     // Get the entry timestamp
220271584abSEd Tanous     std::time_t curTs = 0;
22195820184SJason M. Bills     std::tm timeStruct = {};
22295820184SJason M. Bills     std::istringstream entryStream(logEntry);
22395820184SJason M. Bills     if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
22495820184SJason M. Bills     {
22595820184SJason M. Bills         curTs = std::mktime(&timeStruct);
22695820184SJason M. Bills     }
22795820184SJason M. Bills     // If the timestamp isn't unique, increment the index
22895820184SJason M. Bills     if (curTs == prevTs)
22995820184SJason M. Bills     {
23095820184SJason M. Bills         index++;
23195820184SJason M. Bills     }
23295820184SJason M. Bills     else
23395820184SJason M. Bills     {
23495820184SJason M. Bills         // Otherwise, reset it
23595820184SJason M. Bills         index = 0;
23695820184SJason M. Bills     }
23795820184SJason M. Bills     // Save the timestamp
23895820184SJason M. Bills     prevTs = curTs;
23995820184SJason M. Bills 
24095820184SJason M. Bills     entryID = std::to_string(curTs);
24195820184SJason M. Bills     if (index > 0)
24295820184SJason M. Bills     {
24395820184SJason M. Bills         entryID += "_" + std::to_string(index);
24495820184SJason M. Bills     }
24595820184SJason M. Bills     return true;
24695820184SJason M. Bills }
24795820184SJason M. Bills 
2487e860f15SJohn Edward Broadbent inline static bool
2498d1b46d7Szhanghch05     getTimestampFromID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2508d1b46d7Szhanghch05                        const std::string& entryID, uint64_t& timestamp,
2518d1b46d7Szhanghch05                        uint64_t& index)
25216428a1aSJason M. Bills {
25316428a1aSJason M. Bills     if (entryID.empty())
25416428a1aSJason M. Bills     {
25516428a1aSJason M. Bills         return false;
25616428a1aSJason M. Bills     }
25716428a1aSJason M. Bills     // Convert the unique ID back to a timestamp to find the entry
25839e77504SEd Tanous     std::string_view tsStr(entryID);
25916428a1aSJason M. Bills 
26081ce609eSEd Tanous     auto underscorePos = tsStr.find('_');
26171d5d8dbSEd Tanous     if (underscorePos != std::string_view::npos)
26216428a1aSJason M. Bills     {
26316428a1aSJason M. Bills         // Timestamp has an index
26416428a1aSJason M. Bills         tsStr.remove_suffix(tsStr.size() - underscorePos);
26539e77504SEd Tanous         std::string_view indexStr(entryID);
26616428a1aSJason M. Bills         indexStr.remove_prefix(underscorePos + 1);
26784396af9SPatrick Williams         auto [ptr, ec] = std::from_chars(indexStr.begin(), indexStr.end(),
26884396af9SPatrick Williams                                          index);
269c0bd5e4bSEd Tanous         if (ec != std::errc())
27016428a1aSJason M. Bills         {
2719db4ba25SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
27216428a1aSJason M. Bills             return false;
27316428a1aSJason M. Bills         }
27416428a1aSJason M. Bills     }
27516428a1aSJason M. Bills     // Timestamp has no index
27684396af9SPatrick Williams     auto [ptr, ec] = std::from_chars(tsStr.begin(), tsStr.end(), timestamp);
277c0bd5e4bSEd Tanous     if (ec != std::errc())
27816428a1aSJason M. Bills     {
2799db4ba25SJiaqing Zhao         messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
28016428a1aSJason M. Bills         return false;
28116428a1aSJason M. Bills     }
28216428a1aSJason M. Bills     return true;
28316428a1aSJason M. Bills }
28416428a1aSJason M. Bills 
28595820184SJason M. Bills static bool
28695820184SJason M. Bills     getRedfishLogFiles(std::vector<std::filesystem::path>& redfishLogFiles)
28795820184SJason M. Bills {
28895820184SJason M. Bills     static const std::filesystem::path redfishLogDir = "/var/log";
28995820184SJason M. Bills     static const std::string redfishLogFilename = "redfish";
29095820184SJason M. Bills 
29195820184SJason M. Bills     // Loop through the directory looking for redfish log files
29295820184SJason M. Bills     for (const std::filesystem::directory_entry& dirEnt :
29395820184SJason M. Bills          std::filesystem::directory_iterator(redfishLogDir))
29495820184SJason M. Bills     {
29595820184SJason M. Bills         // If we find a redfish log file, save the path
29695820184SJason M. Bills         std::string filename = dirEnt.path().filename();
29711ba3979SEd Tanous         if (filename.starts_with(redfishLogFilename))
29895820184SJason M. Bills         {
29995820184SJason M. Bills             redfishLogFiles.emplace_back(redfishLogDir / filename);
30095820184SJason M. Bills         }
30195820184SJason M. Bills     }
30295820184SJason M. Bills     // As the log files rotate, they are appended with a ".#" that is higher for
30395820184SJason M. Bills     // the older logs. Since we don't expect more than 10 log files, we
30495820184SJason M. Bills     // can just sort the list to get them in order from newest to oldest
30595820184SJason M. Bills     std::sort(redfishLogFiles.begin(), redfishLogFiles.end());
30695820184SJason M. Bills 
30795820184SJason M. Bills     return !redfishLogFiles.empty();
30895820184SJason M. Bills }
30995820184SJason M. Bills 
31068dd075aSAsmitha Karunanithi inline log_entry::OriginatorTypes
31168dd075aSAsmitha Karunanithi     mapDbusOriginatorTypeToRedfish(const std::string& originatorType)
31268dd075aSAsmitha Karunanithi {
31368dd075aSAsmitha Karunanithi     if (originatorType ==
31468dd075aSAsmitha Karunanithi         "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.Client")
31568dd075aSAsmitha Karunanithi     {
31668dd075aSAsmitha Karunanithi         return log_entry::OriginatorTypes::Client;
31768dd075aSAsmitha Karunanithi     }
31868dd075aSAsmitha Karunanithi     if (originatorType ==
31968dd075aSAsmitha Karunanithi         "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.Internal")
32068dd075aSAsmitha Karunanithi     {
32168dd075aSAsmitha Karunanithi         return log_entry::OriginatorTypes::Internal;
32268dd075aSAsmitha Karunanithi     }
32368dd075aSAsmitha Karunanithi     if (originatorType ==
32468dd075aSAsmitha Karunanithi         "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.SupportingService")
32568dd075aSAsmitha Karunanithi     {
32668dd075aSAsmitha Karunanithi         return log_entry::OriginatorTypes::SupportingService;
32768dd075aSAsmitha Karunanithi     }
32868dd075aSAsmitha Karunanithi     return log_entry::OriginatorTypes::Invalid;
32968dd075aSAsmitha Karunanithi }
33068dd075aSAsmitha Karunanithi 
331aefe3786SClaire Weinan inline void parseDumpEntryFromDbusObject(
3322d613eb6SJiaqing Zhao     const dbus::utility::ManagedObjectType::value_type& object,
333c6fecdabSClaire Weinan     std::string& dumpStatus, uint64_t& size, uint64_t& timestampUs,
33468dd075aSAsmitha Karunanithi     std::string& originatorId, log_entry::OriginatorTypes& originatorType,
335aefe3786SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
336aefe3786SClaire Weinan {
337aefe3786SClaire Weinan     for (const auto& interfaceMap : object.second)
338aefe3786SClaire Weinan     {
339aefe3786SClaire Weinan         if (interfaceMap.first == "xyz.openbmc_project.Common.Progress")
340aefe3786SClaire Weinan         {
341aefe3786SClaire Weinan             for (const auto& propertyMap : interfaceMap.second)
342aefe3786SClaire Weinan             {
343aefe3786SClaire Weinan                 if (propertyMap.first == "Status")
344aefe3786SClaire Weinan                 {
345aefe3786SClaire Weinan                     const auto* status =
346aefe3786SClaire Weinan                         std::get_if<std::string>(&propertyMap.second);
347aefe3786SClaire Weinan                     if (status == nullptr)
348aefe3786SClaire Weinan                     {
349aefe3786SClaire Weinan                         messages::internalError(asyncResp->res);
350aefe3786SClaire Weinan                         break;
351aefe3786SClaire Weinan                     }
352aefe3786SClaire Weinan                     dumpStatus = *status;
353aefe3786SClaire Weinan                 }
354aefe3786SClaire Weinan             }
355aefe3786SClaire Weinan         }
356aefe3786SClaire Weinan         else if (interfaceMap.first == "xyz.openbmc_project.Dump.Entry")
357aefe3786SClaire Weinan         {
358aefe3786SClaire Weinan             for (const auto& propertyMap : interfaceMap.second)
359aefe3786SClaire Weinan             {
360aefe3786SClaire Weinan                 if (propertyMap.first == "Size")
361aefe3786SClaire Weinan                 {
362aefe3786SClaire Weinan                     const auto* sizePtr =
363aefe3786SClaire Weinan                         std::get_if<uint64_t>(&propertyMap.second);
364aefe3786SClaire Weinan                     if (sizePtr == nullptr)
365aefe3786SClaire Weinan                     {
366aefe3786SClaire Weinan                         messages::internalError(asyncResp->res);
367aefe3786SClaire Weinan                         break;
368aefe3786SClaire Weinan                     }
369aefe3786SClaire Weinan                     size = *sizePtr;
370aefe3786SClaire Weinan                     break;
371aefe3786SClaire Weinan                 }
372aefe3786SClaire Weinan             }
373aefe3786SClaire Weinan         }
374aefe3786SClaire Weinan         else if (interfaceMap.first == "xyz.openbmc_project.Time.EpochTime")
375aefe3786SClaire Weinan         {
376aefe3786SClaire Weinan             for (const auto& propertyMap : interfaceMap.second)
377aefe3786SClaire Weinan             {
378aefe3786SClaire Weinan                 if (propertyMap.first == "Elapsed")
379aefe3786SClaire Weinan                 {
380aefe3786SClaire Weinan                     const uint64_t* usecsTimeStamp =
381aefe3786SClaire Weinan                         std::get_if<uint64_t>(&propertyMap.second);
382aefe3786SClaire Weinan                     if (usecsTimeStamp == nullptr)
383aefe3786SClaire Weinan                     {
384aefe3786SClaire Weinan                         messages::internalError(asyncResp->res);
385aefe3786SClaire Weinan                         break;
386aefe3786SClaire Weinan                     }
387c6fecdabSClaire Weinan                     timestampUs = *usecsTimeStamp;
388aefe3786SClaire Weinan                     break;
389aefe3786SClaire Weinan                 }
390aefe3786SClaire Weinan             }
391aefe3786SClaire Weinan         }
39268dd075aSAsmitha Karunanithi         else if (interfaceMap.first ==
39368dd075aSAsmitha Karunanithi                  "xyz.openbmc_project.Common.OriginatedBy")
39468dd075aSAsmitha Karunanithi         {
39568dd075aSAsmitha Karunanithi             for (const auto& propertyMap : interfaceMap.second)
39668dd075aSAsmitha Karunanithi             {
39768dd075aSAsmitha Karunanithi                 if (propertyMap.first == "OriginatorId")
39868dd075aSAsmitha Karunanithi                 {
39968dd075aSAsmitha Karunanithi                     const std::string* id =
40068dd075aSAsmitha Karunanithi                         std::get_if<std::string>(&propertyMap.second);
40168dd075aSAsmitha Karunanithi                     if (id == nullptr)
40268dd075aSAsmitha Karunanithi                     {
40368dd075aSAsmitha Karunanithi                         messages::internalError(asyncResp->res);
40468dd075aSAsmitha Karunanithi                         break;
40568dd075aSAsmitha Karunanithi                     }
40668dd075aSAsmitha Karunanithi                     originatorId = *id;
40768dd075aSAsmitha Karunanithi                 }
40868dd075aSAsmitha Karunanithi 
40968dd075aSAsmitha Karunanithi                 if (propertyMap.first == "OriginatorType")
41068dd075aSAsmitha Karunanithi                 {
41168dd075aSAsmitha Karunanithi                     const std::string* type =
41268dd075aSAsmitha Karunanithi                         std::get_if<std::string>(&propertyMap.second);
41368dd075aSAsmitha Karunanithi                     if (type == nullptr)
41468dd075aSAsmitha Karunanithi                     {
41568dd075aSAsmitha Karunanithi                         messages::internalError(asyncResp->res);
41668dd075aSAsmitha Karunanithi                         break;
41768dd075aSAsmitha Karunanithi                     }
41868dd075aSAsmitha Karunanithi 
41968dd075aSAsmitha Karunanithi                     originatorType = mapDbusOriginatorTypeToRedfish(*type);
42068dd075aSAsmitha Karunanithi                     if (originatorType == log_entry::OriginatorTypes::Invalid)
42168dd075aSAsmitha Karunanithi                     {
42268dd075aSAsmitha Karunanithi                         messages::internalError(asyncResp->res);
42368dd075aSAsmitha Karunanithi                         break;
42468dd075aSAsmitha Karunanithi                     }
42568dd075aSAsmitha Karunanithi                 }
42668dd075aSAsmitha Karunanithi             }
42768dd075aSAsmitha Karunanithi         }
428aefe3786SClaire Weinan     }
429aefe3786SClaire Weinan }
430aefe3786SClaire Weinan 
43121ab404cSNan Zhou static std::string getDumpEntriesPath(const std::string& dumpType)
432fdd26906SClaire Weinan {
433fdd26906SClaire Weinan     std::string entriesPath;
434fdd26906SClaire Weinan 
435fdd26906SClaire Weinan     if (dumpType == "BMC")
436fdd26906SClaire Weinan     {
437fdd26906SClaire Weinan         entriesPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
438fdd26906SClaire Weinan     }
439fdd26906SClaire Weinan     else if (dumpType == "FaultLog")
440fdd26906SClaire Weinan     {
441fdd26906SClaire Weinan         entriesPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/";
442fdd26906SClaire Weinan     }
443fdd26906SClaire Weinan     else if (dumpType == "System")
444fdd26906SClaire Weinan     {
445fdd26906SClaire Weinan         entriesPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
446fdd26906SClaire Weinan     }
447fdd26906SClaire Weinan     else
448fdd26906SClaire Weinan     {
449*62598e31SEd Tanous         BMCWEB_LOG_ERROR("getDumpEntriesPath() invalid dump type: {}",
450*62598e31SEd Tanous                          dumpType);
451fdd26906SClaire Weinan     }
452fdd26906SClaire Weinan 
453fdd26906SClaire Weinan     // Returns empty string on error
454fdd26906SClaire Weinan     return entriesPath;
455fdd26906SClaire Weinan }
456fdd26906SClaire Weinan 
4578d1b46d7Szhanghch05 inline void
4588d1b46d7Szhanghch05     getDumpEntryCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4595cb1dd27SAsmitha Karunanithi                            const std::string& dumpType)
4605cb1dd27SAsmitha Karunanithi {
461fdd26906SClaire Weinan     std::string entriesPath = getDumpEntriesPath(dumpType);
462fdd26906SClaire Weinan     if (entriesPath.empty())
4635cb1dd27SAsmitha Karunanithi     {
4645cb1dd27SAsmitha Karunanithi         messages::internalError(asyncResp->res);
4655cb1dd27SAsmitha Karunanithi         return;
4665cb1dd27SAsmitha Karunanithi     }
4675cb1dd27SAsmitha Karunanithi 
4685eb468daSGeorge Liu     sdbusplus::message::object_path path("/xyz/openbmc_project/dump");
4695eb468daSGeorge Liu     dbus::utility::getManagedObjects(
4705eb468daSGeorge Liu         "xyz.openbmc_project.Dump.Manager", path,
471fdd26906SClaire Weinan         [asyncResp, entriesPath,
4725e7e2dc5SEd Tanous          dumpType](const boost::system::error_code& ec,
4735eb468daSGeorge Liu                    const dbus::utility::ManagedObjectType& objects) {
4745cb1dd27SAsmitha Karunanithi         if (ec)
4755cb1dd27SAsmitha Karunanithi         {
476*62598e31SEd Tanous             BMCWEB_LOG_ERROR("DumpEntry resp_handler got error {}", ec);
4775cb1dd27SAsmitha Karunanithi             messages::internalError(asyncResp->res);
4785cb1dd27SAsmitha Karunanithi             return;
4795cb1dd27SAsmitha Karunanithi         }
4805cb1dd27SAsmitha Karunanithi 
481fdd26906SClaire Weinan         // Remove ending slash
482fdd26906SClaire Weinan         std::string odataIdStr = entriesPath;
483fdd26906SClaire Weinan         if (!odataIdStr.empty())
484fdd26906SClaire Weinan         {
485fdd26906SClaire Weinan             odataIdStr.pop_back();
486fdd26906SClaire Weinan         }
487fdd26906SClaire Weinan 
488fdd26906SClaire Weinan         asyncResp->res.jsonValue["@odata.type"] =
489fdd26906SClaire Weinan             "#LogEntryCollection.LogEntryCollection";
490fdd26906SClaire Weinan         asyncResp->res.jsonValue["@odata.id"] = std::move(odataIdStr);
491fdd26906SClaire Weinan         asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entries";
49289492a15SPatrick Williams         asyncResp->res.jsonValue["Description"] = "Collection of " + dumpType +
49389492a15SPatrick Williams                                                   " Dump Entries";
494fdd26906SClaire Weinan 
4955cb1dd27SAsmitha Karunanithi         nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
4965cb1dd27SAsmitha Karunanithi         entriesArray = nlohmann::json::array();
497b47452b2SAsmitha Karunanithi         std::string dumpEntryPath =
498b47452b2SAsmitha Karunanithi             "/xyz/openbmc_project/dump/" +
499002d39b4SEd Tanous             std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
5005cb1dd27SAsmitha Karunanithi 
5015eb468daSGeorge Liu         dbus::utility::ManagedObjectType resp(objects);
502002d39b4SEd Tanous         std::sort(resp.begin(), resp.end(), [](const auto& l, const auto& r) {
503002d39b4SEd Tanous             return AlphanumLess<std::string>()(l.first.filename(),
504002d39b4SEd Tanous                                                r.first.filename());
505565dfb6fSClaire Weinan         });
506565dfb6fSClaire Weinan 
5075cb1dd27SAsmitha Karunanithi         for (auto& object : resp)
5085cb1dd27SAsmitha Karunanithi         {
509b47452b2SAsmitha Karunanithi             if (object.first.str.find(dumpEntryPath) == std::string::npos)
5105cb1dd27SAsmitha Karunanithi             {
5115cb1dd27SAsmitha Karunanithi                 continue;
5125cb1dd27SAsmitha Karunanithi             }
513c6fecdabSClaire Weinan             uint64_t timestampUs = 0;
5145cb1dd27SAsmitha Karunanithi             uint64_t size = 0;
51535440d18SAsmitha Karunanithi             std::string dumpStatus;
51668dd075aSAsmitha Karunanithi             std::string originatorId;
51768dd075aSAsmitha Karunanithi             log_entry::OriginatorTypes originatorType =
51868dd075aSAsmitha Karunanithi                 log_entry::OriginatorTypes::Internal;
519433b68b4SJason M. Bills             nlohmann::json::object_t thisEntry;
5202dfd18efSEd Tanous 
5212dfd18efSEd Tanous             std::string entryID = object.first.filename();
5222dfd18efSEd Tanous             if (entryID.empty())
5235cb1dd27SAsmitha Karunanithi             {
5245cb1dd27SAsmitha Karunanithi                 continue;
5255cb1dd27SAsmitha Karunanithi             }
5265cb1dd27SAsmitha Karunanithi 
527c6fecdabSClaire Weinan             parseDumpEntryFromDbusObject(object, dumpStatus, size, timestampUs,
52868dd075aSAsmitha Karunanithi                                          originatorId, originatorType,
529aefe3786SClaire Weinan                                          asyncResp);
5305cb1dd27SAsmitha Karunanithi 
5310fda0f12SGeorge Liu             if (dumpStatus !=
5320fda0f12SGeorge Liu                     "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
53335440d18SAsmitha Karunanithi                 !dumpStatus.empty())
53435440d18SAsmitha Karunanithi             {
53535440d18SAsmitha Karunanithi                 // Dump status is not Complete, no need to enumerate
53635440d18SAsmitha Karunanithi                 continue;
53735440d18SAsmitha Karunanithi             }
53835440d18SAsmitha Karunanithi 
53968dd075aSAsmitha Karunanithi             thisEntry["@odata.type"] = "#LogEntry.v1_11_0.LogEntry";
540fdd26906SClaire Weinan             thisEntry["@odata.id"] = entriesPath + entryID;
5415cb1dd27SAsmitha Karunanithi             thisEntry["Id"] = entryID;
5425cb1dd27SAsmitha Karunanithi             thisEntry["EntryType"] = "Event";
5435cb1dd27SAsmitha Karunanithi             thisEntry["Name"] = dumpType + " Dump Entry";
544bbd80db8SClaire Weinan             thisEntry["Created"] =
545bbd80db8SClaire Weinan                 redfish::time_utils::getDateTimeUintUs(timestampUs);
5465cb1dd27SAsmitha Karunanithi 
54768dd075aSAsmitha Karunanithi             if (!originatorId.empty())
54868dd075aSAsmitha Karunanithi             {
54968dd075aSAsmitha Karunanithi                 thisEntry["Originator"] = originatorId;
55068dd075aSAsmitha Karunanithi                 thisEntry["OriginatorType"] = originatorType;
55168dd075aSAsmitha Karunanithi             }
55268dd075aSAsmitha Karunanithi 
5535cb1dd27SAsmitha Karunanithi             if (dumpType == "BMC")
5545cb1dd27SAsmitha Karunanithi             {
555d337bb72SAsmitha Karunanithi                 thisEntry["DiagnosticDataType"] = "Manager";
55689492a15SPatrick Williams                 thisEntry["AdditionalDataURI"] = entriesPath + entryID +
55789492a15SPatrick Williams                                                  "/attachment";
558fdd26906SClaire Weinan                 thisEntry["AdditionalDataSizeBytes"] = size;
5595cb1dd27SAsmitha Karunanithi             }
5605cb1dd27SAsmitha Karunanithi             else if (dumpType == "System")
5615cb1dd27SAsmitha Karunanithi             {
562d337bb72SAsmitha Karunanithi                 thisEntry["DiagnosticDataType"] = "OEM";
563d337bb72SAsmitha Karunanithi                 thisEntry["OEMDiagnosticDataType"] = "System";
56489492a15SPatrick Williams                 thisEntry["AdditionalDataURI"] = entriesPath + entryID +
56589492a15SPatrick Williams                                                  "/attachment";
566fdd26906SClaire Weinan                 thisEntry["AdditionalDataSizeBytes"] = size;
5675cb1dd27SAsmitha Karunanithi             }
568b2ba3072SPatrick Williams             entriesArray.emplace_back(std::move(thisEntry));
5695cb1dd27SAsmitha Karunanithi         }
570002d39b4SEd Tanous         asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
5715eb468daSGeorge Liu         });
5725cb1dd27SAsmitha Karunanithi }
5735cb1dd27SAsmitha Karunanithi 
5748d1b46d7Szhanghch05 inline void
575c7a6d660SClaire Weinan     getDumpEntryById(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
5768d1b46d7Szhanghch05                      const std::string& entryID, const std::string& dumpType)
5775cb1dd27SAsmitha Karunanithi {
578fdd26906SClaire Weinan     std::string entriesPath = getDumpEntriesPath(dumpType);
579fdd26906SClaire Weinan     if (entriesPath.empty())
5805cb1dd27SAsmitha Karunanithi     {
5815cb1dd27SAsmitha Karunanithi         messages::internalError(asyncResp->res);
5825cb1dd27SAsmitha Karunanithi         return;
5835cb1dd27SAsmitha Karunanithi     }
5845cb1dd27SAsmitha Karunanithi 
5855eb468daSGeorge Liu     sdbusplus::message::object_path path("/xyz/openbmc_project/dump");
5865eb468daSGeorge Liu     dbus::utility::getManagedObjects(
5875eb468daSGeorge Liu         "xyz.openbmc_project.Dump.Manager", path,
588fdd26906SClaire Weinan         [asyncResp, entryID, dumpType,
5895e7e2dc5SEd Tanous          entriesPath](const boost::system::error_code& ec,
59002cad96eSEd Tanous                       const dbus::utility::ManagedObjectType& resp) {
5915cb1dd27SAsmitha Karunanithi         if (ec)
5925cb1dd27SAsmitha Karunanithi         {
593*62598e31SEd Tanous             BMCWEB_LOG_ERROR("DumpEntry resp_handler got error {}", ec);
5945cb1dd27SAsmitha Karunanithi             messages::internalError(asyncResp->res);
5955cb1dd27SAsmitha Karunanithi             return;
5965cb1dd27SAsmitha Karunanithi         }
5975cb1dd27SAsmitha Karunanithi 
598b47452b2SAsmitha Karunanithi         bool foundDumpEntry = false;
599b47452b2SAsmitha Karunanithi         std::string dumpEntryPath =
600b47452b2SAsmitha Karunanithi             "/xyz/openbmc_project/dump/" +
601002d39b4SEd Tanous             std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
602b47452b2SAsmitha Karunanithi 
6039eb808c1SEd Tanous         for (const auto& objectPath : resp)
6045cb1dd27SAsmitha Karunanithi         {
605b47452b2SAsmitha Karunanithi             if (objectPath.first.str != dumpEntryPath + entryID)
6065cb1dd27SAsmitha Karunanithi             {
6075cb1dd27SAsmitha Karunanithi                 continue;
6085cb1dd27SAsmitha Karunanithi             }
6095cb1dd27SAsmitha Karunanithi 
6105cb1dd27SAsmitha Karunanithi             foundDumpEntry = true;
611c6fecdabSClaire Weinan             uint64_t timestampUs = 0;
6125cb1dd27SAsmitha Karunanithi             uint64_t size = 0;
61335440d18SAsmitha Karunanithi             std::string dumpStatus;
61468dd075aSAsmitha Karunanithi             std::string originatorId;
61568dd075aSAsmitha Karunanithi             log_entry::OriginatorTypes originatorType =
61668dd075aSAsmitha Karunanithi                 log_entry::OriginatorTypes::Internal;
6175cb1dd27SAsmitha Karunanithi 
618aefe3786SClaire Weinan             parseDumpEntryFromDbusObject(objectPath, dumpStatus, size,
61968dd075aSAsmitha Karunanithi                                          timestampUs, originatorId,
62068dd075aSAsmitha Karunanithi                                          originatorType, asyncResp);
6215cb1dd27SAsmitha Karunanithi 
6220fda0f12SGeorge Liu             if (dumpStatus !=
6230fda0f12SGeorge Liu                     "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
62435440d18SAsmitha Karunanithi                 !dumpStatus.empty())
62535440d18SAsmitha Karunanithi             {
62635440d18SAsmitha Karunanithi                 // Dump status is not Complete
62735440d18SAsmitha Karunanithi                 // return not found until status is changed to Completed
628d1bde9e5SKrzysztof Grobelny                 messages::resourceNotFound(asyncResp->res, dumpType + " dump",
629d1bde9e5SKrzysztof Grobelny                                            entryID);
63035440d18SAsmitha Karunanithi                 return;
63135440d18SAsmitha Karunanithi             }
63235440d18SAsmitha Karunanithi 
6335cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["@odata.type"] =
63468dd075aSAsmitha Karunanithi                 "#LogEntry.v1_11_0.LogEntry";
635fdd26906SClaire Weinan             asyncResp->res.jsonValue["@odata.id"] = entriesPath + entryID;
6365cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Id"] = entryID;
6375cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["EntryType"] = "Event";
6385cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
639bbd80db8SClaire Weinan             asyncResp->res.jsonValue["Created"] =
640bbd80db8SClaire Weinan                 redfish::time_utils::getDateTimeUintUs(timestampUs);
6415cb1dd27SAsmitha Karunanithi 
64268dd075aSAsmitha Karunanithi             if (!originatorId.empty())
64368dd075aSAsmitha Karunanithi             {
64468dd075aSAsmitha Karunanithi                 asyncResp->res.jsonValue["Originator"] = originatorId;
64568dd075aSAsmitha Karunanithi                 asyncResp->res.jsonValue["OriginatorType"] = originatorType;
64668dd075aSAsmitha Karunanithi             }
64768dd075aSAsmitha Karunanithi 
6485cb1dd27SAsmitha Karunanithi             if (dumpType == "BMC")
6495cb1dd27SAsmitha Karunanithi             {
650d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager";
651d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["AdditionalDataURI"] =
652fdd26906SClaire Weinan                     entriesPath + entryID + "/attachment";
653fdd26906SClaire Weinan                 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
6545cb1dd27SAsmitha Karunanithi             }
6555cb1dd27SAsmitha Karunanithi             else if (dumpType == "System")
6565cb1dd27SAsmitha Karunanithi             {
657d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM";
658002d39b4SEd Tanous                 asyncResp->res.jsonValue["OEMDiagnosticDataType"] = "System";
659d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["AdditionalDataURI"] =
660fdd26906SClaire Weinan                     entriesPath + entryID + "/attachment";
661fdd26906SClaire Weinan                 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
6625cb1dd27SAsmitha Karunanithi             }
6635cb1dd27SAsmitha Karunanithi         }
664e05aec50SEd Tanous         if (!foundDumpEntry)
665b47452b2SAsmitha Karunanithi         {
666*62598e31SEd Tanous             BMCWEB_LOG_WARNING("Can't find Dump Entry {}", entryID);
667b90d14f2SMyung Bae             messages::resourceNotFound(asyncResp->res, dumpType + " dump",
668b90d14f2SMyung Bae                                        entryID);
669b47452b2SAsmitha Karunanithi             return;
670b47452b2SAsmitha Karunanithi         }
6715eb468daSGeorge Liu         });
6725cb1dd27SAsmitha Karunanithi }
6735cb1dd27SAsmitha Karunanithi 
6748d1b46d7Szhanghch05 inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6759878256fSStanley Chu                             const std::string& entryID,
676b47452b2SAsmitha Karunanithi                             const std::string& dumpType)
6775cb1dd27SAsmitha Karunanithi {
678002d39b4SEd Tanous     auto respHandler =
6795e7e2dc5SEd Tanous         [asyncResp, entryID](const boost::system::error_code& ec) {
680*62598e31SEd Tanous         BMCWEB_LOG_DEBUG("Dump Entry doDelete callback: Done");
6815cb1dd27SAsmitha Karunanithi         if (ec)
6825cb1dd27SAsmitha Karunanithi         {
6833de8d8baSGeorge Liu             if (ec.value() == EBADR)
6843de8d8baSGeorge Liu             {
6853de8d8baSGeorge Liu                 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
6863de8d8baSGeorge Liu                 return;
6873de8d8baSGeorge Liu             }
688*62598e31SEd Tanous             BMCWEB_LOG_ERROR(
689*62598e31SEd Tanous                 "Dump (DBus) doDelete respHandler got error {} entryID={}", ec,
690*62598e31SEd Tanous                 entryID);
6915cb1dd27SAsmitha Karunanithi             messages::internalError(asyncResp->res);
6925cb1dd27SAsmitha Karunanithi             return;
6935cb1dd27SAsmitha Karunanithi         }
6945cb1dd27SAsmitha Karunanithi     };
6955cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
6965cb1dd27SAsmitha Karunanithi         respHandler, "xyz.openbmc_project.Dump.Manager",
697b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" +
698b47452b2SAsmitha Karunanithi             std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/" +
699b47452b2SAsmitha Karunanithi             entryID,
7005cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Object.Delete", "Delete");
7015cb1dd27SAsmitha Karunanithi }
7025cb1dd27SAsmitha Karunanithi 
7038e31778eSAsmitha Karunanithi inline DumpCreationProgress
7048e31778eSAsmitha Karunanithi     mapDbusStatusToDumpProgress(const std::string& status)
705a43be80fSAsmitha Karunanithi {
7068e31778eSAsmitha Karunanithi     if (status ==
7078e31778eSAsmitha Karunanithi             "xyz.openbmc_project.Common.Progress.OperationStatus.Failed" ||
7088e31778eSAsmitha Karunanithi         status == "xyz.openbmc_project.Common.Progress.OperationStatus.Aborted")
7098e31778eSAsmitha Karunanithi     {
7108e31778eSAsmitha Karunanithi         return DumpCreationProgress::DUMP_CREATE_FAILED;
7118e31778eSAsmitha Karunanithi     }
7128e31778eSAsmitha Karunanithi     if (status ==
7138e31778eSAsmitha Karunanithi         "xyz.openbmc_project.Common.Progress.OperationStatus.Completed")
7148e31778eSAsmitha Karunanithi     {
7158e31778eSAsmitha Karunanithi         return DumpCreationProgress::DUMP_CREATE_SUCCESS;
7168e31778eSAsmitha Karunanithi     }
7178e31778eSAsmitha Karunanithi     return DumpCreationProgress::DUMP_CREATE_INPROGRESS;
7188e31778eSAsmitha Karunanithi }
7198e31778eSAsmitha Karunanithi 
7208e31778eSAsmitha Karunanithi inline DumpCreationProgress
7218e31778eSAsmitha Karunanithi     getDumpCompletionStatus(const dbus::utility::DBusPropertiesMap& values)
7228e31778eSAsmitha Karunanithi {
7238e31778eSAsmitha Karunanithi     for (const auto& [key, val] : values)
7248e31778eSAsmitha Karunanithi     {
7258e31778eSAsmitha Karunanithi         if (key == "Status")
7268e31778eSAsmitha Karunanithi         {
7278e31778eSAsmitha Karunanithi             const std::string* value = std::get_if<std::string>(&val);
7288e31778eSAsmitha Karunanithi             if (value == nullptr)
7298e31778eSAsmitha Karunanithi             {
730*62598e31SEd Tanous                 BMCWEB_LOG_ERROR("Status property value is null");
7318e31778eSAsmitha Karunanithi                 return DumpCreationProgress::DUMP_CREATE_FAILED;
7328e31778eSAsmitha Karunanithi             }
7338e31778eSAsmitha Karunanithi             return mapDbusStatusToDumpProgress(*value);
7348e31778eSAsmitha Karunanithi         }
7358e31778eSAsmitha Karunanithi     }
7368e31778eSAsmitha Karunanithi     return DumpCreationProgress::DUMP_CREATE_INPROGRESS;
7378e31778eSAsmitha Karunanithi }
7388e31778eSAsmitha Karunanithi 
7398e31778eSAsmitha Karunanithi inline std::string getDumpEntryPath(const std::string& dumpPath)
7408e31778eSAsmitha Karunanithi {
7418e31778eSAsmitha Karunanithi     if (dumpPath == "/xyz/openbmc_project/dump/bmc/entry")
7428e31778eSAsmitha Karunanithi     {
7438e31778eSAsmitha Karunanithi         return "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
7448e31778eSAsmitha Karunanithi     }
7458e31778eSAsmitha Karunanithi     if (dumpPath == "/xyz/openbmc_project/dump/system/entry")
7468e31778eSAsmitha Karunanithi     {
7478e31778eSAsmitha Karunanithi         return "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
7488e31778eSAsmitha Karunanithi     }
7498e31778eSAsmitha Karunanithi     return "";
7508e31778eSAsmitha Karunanithi }
7518e31778eSAsmitha Karunanithi 
7528e31778eSAsmitha Karunanithi inline void createDumpTaskCallback(
7538e31778eSAsmitha Karunanithi     task::Payload&& payload,
7548e31778eSAsmitha Karunanithi     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7558e31778eSAsmitha Karunanithi     const sdbusplus::message::object_path& createdObjPath)
7568e31778eSAsmitha Karunanithi {
7578e31778eSAsmitha Karunanithi     const std::string dumpPath = createdObjPath.parent_path().str;
7588e31778eSAsmitha Karunanithi     const std::string dumpId = createdObjPath.filename();
7598e31778eSAsmitha Karunanithi 
7608e31778eSAsmitha Karunanithi     std::string dumpEntryPath = getDumpEntryPath(dumpPath);
7618e31778eSAsmitha Karunanithi 
7628e31778eSAsmitha Karunanithi     if (dumpEntryPath.empty())
7638e31778eSAsmitha Karunanithi     {
764*62598e31SEd Tanous         BMCWEB_LOG_ERROR("Invalid dump type received");
7658e31778eSAsmitha Karunanithi         messages::internalError(asyncResp->res);
7668e31778eSAsmitha Karunanithi         return;
7678e31778eSAsmitha Karunanithi     }
7688e31778eSAsmitha Karunanithi 
7698e31778eSAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
7708e31778eSAsmitha Karunanithi         [asyncResp, payload, createdObjPath,
7718e31778eSAsmitha Karunanithi          dumpEntryPath{std::move(dumpEntryPath)},
7725e7e2dc5SEd Tanous          dumpId](const boost::system::error_code& ec,
7738e31778eSAsmitha Karunanithi                  const std::string& introspectXml) {
7748e31778eSAsmitha Karunanithi         if (ec)
7758e31778eSAsmitha Karunanithi         {
776*62598e31SEd Tanous             BMCWEB_LOG_ERROR("Introspect call failed with error: {}",
777*62598e31SEd Tanous                              ec.message());
7788e31778eSAsmitha Karunanithi             messages::internalError(asyncResp->res);
7798e31778eSAsmitha Karunanithi             return;
7808e31778eSAsmitha Karunanithi         }
7818e31778eSAsmitha Karunanithi 
7828e31778eSAsmitha Karunanithi         // Check if the created dump object has implemented Progress
7838e31778eSAsmitha Karunanithi         // interface to track dump completion. If yes, fetch the "Status"
7848e31778eSAsmitha Karunanithi         // property of the interface, modify the task state accordingly.
7858e31778eSAsmitha Karunanithi         // Else, return task completed.
7868e31778eSAsmitha Karunanithi         tinyxml2::XMLDocument doc;
7878e31778eSAsmitha Karunanithi 
7888e31778eSAsmitha Karunanithi         doc.Parse(introspectXml.data(), introspectXml.size());
7898e31778eSAsmitha Karunanithi         tinyxml2::XMLNode* pRoot = doc.FirstChildElement("node");
7908e31778eSAsmitha Karunanithi         if (pRoot == nullptr)
7918e31778eSAsmitha Karunanithi         {
792*62598e31SEd Tanous             BMCWEB_LOG_ERROR("XML document failed to parse");
7938e31778eSAsmitha Karunanithi             messages::internalError(asyncResp->res);
7948e31778eSAsmitha Karunanithi             return;
7958e31778eSAsmitha Karunanithi         }
7968e31778eSAsmitha Karunanithi         tinyxml2::XMLElement* interfaceNode =
7978e31778eSAsmitha Karunanithi             pRoot->FirstChildElement("interface");
7988e31778eSAsmitha Karunanithi 
7998e31778eSAsmitha Karunanithi         bool isProgressIntfPresent = false;
8008e31778eSAsmitha Karunanithi         while (interfaceNode != nullptr)
8018e31778eSAsmitha Karunanithi         {
8028e31778eSAsmitha Karunanithi             const char* thisInterfaceName = interfaceNode->Attribute("name");
8038e31778eSAsmitha Karunanithi             if (thisInterfaceName != nullptr)
8048e31778eSAsmitha Karunanithi             {
8058e31778eSAsmitha Karunanithi                 if (thisInterfaceName ==
8068e31778eSAsmitha Karunanithi                     std::string_view("xyz.openbmc_project.Common.Progress"))
8078e31778eSAsmitha Karunanithi                 {
8088e31778eSAsmitha Karunanithi                     interfaceNode =
8098e31778eSAsmitha Karunanithi                         interfaceNode->NextSiblingElement("interface");
8108e31778eSAsmitha Karunanithi                     continue;
8118e31778eSAsmitha Karunanithi                 }
8128e31778eSAsmitha Karunanithi                 isProgressIntfPresent = true;
8138e31778eSAsmitha Karunanithi                 break;
8148e31778eSAsmitha Karunanithi             }
8158e31778eSAsmitha Karunanithi             interfaceNode = interfaceNode->NextSiblingElement("interface");
8168e31778eSAsmitha Karunanithi         }
8178e31778eSAsmitha Karunanithi 
818a43be80fSAsmitha Karunanithi         std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
8198e31778eSAsmitha Karunanithi             [createdObjPath, dumpEntryPath, dumpId, isProgressIntfPresent](
8208b24275dSEd Tanous                 const boost::system::error_code& ec2, sdbusplus::message_t& msg,
821a43be80fSAsmitha Karunanithi                 const std::shared_ptr<task::TaskData>& taskData) {
8228b24275dSEd Tanous             if (ec2)
823cb13a392SEd Tanous             {
824*62598e31SEd Tanous                 BMCWEB_LOG_ERROR("{}: Error in creating dump",
825*62598e31SEd Tanous                                  createdObjPath.str);
8268e31778eSAsmitha Karunanithi                 taskData->messages.emplace_back(messages::internalError());
8276145ed6fSAsmitha Karunanithi                 taskData->state = "Cancelled";
8286145ed6fSAsmitha Karunanithi                 return task::completed;
829cb13a392SEd Tanous             }
830b9d36b47SEd Tanous 
8318e31778eSAsmitha Karunanithi             if (isProgressIntfPresent)
832a43be80fSAsmitha Karunanithi             {
8338e31778eSAsmitha Karunanithi                 dbus::utility::DBusPropertiesMap values;
8348e31778eSAsmitha Karunanithi                 std::string prop;
8358e31778eSAsmitha Karunanithi                 msg.read(prop, values);
8368e31778eSAsmitha Karunanithi 
8378e31778eSAsmitha Karunanithi                 DumpCreationProgress dumpStatus =
8388e31778eSAsmitha Karunanithi                     getDumpCompletionStatus(values);
8398e31778eSAsmitha Karunanithi                 if (dumpStatus == DumpCreationProgress::DUMP_CREATE_FAILED)
8408e31778eSAsmitha Karunanithi                 {
841*62598e31SEd Tanous                     BMCWEB_LOG_ERROR("{}: Error in creating dump",
842*62598e31SEd Tanous                                      createdObjPath.str);
8438e31778eSAsmitha Karunanithi                     taskData->state = "Cancelled";
8448e31778eSAsmitha Karunanithi                     return task::completed;
8458e31778eSAsmitha Karunanithi                 }
8468e31778eSAsmitha Karunanithi 
8478e31778eSAsmitha Karunanithi                 if (dumpStatus == DumpCreationProgress::DUMP_CREATE_INPROGRESS)
8488e31778eSAsmitha Karunanithi                 {
849*62598e31SEd Tanous                     BMCWEB_LOG_DEBUG("{}: Dump creation task is in progress",
850*62598e31SEd Tanous                                      createdObjPath.str);
8518e31778eSAsmitha Karunanithi                     return !task::completed;
8528e31778eSAsmitha Karunanithi                 }
8538e31778eSAsmitha Karunanithi             }
8548e31778eSAsmitha Karunanithi 
855a43be80fSAsmitha Karunanithi             nlohmann::json retMessage = messages::success();
856a43be80fSAsmitha Karunanithi             taskData->messages.emplace_back(retMessage);
857a43be80fSAsmitha Karunanithi 
858c51a58eeSEd Tanous             boost::urls::url url = boost::urls::format(
859c51a58eeSEd Tanous                 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/{}", dumpId);
860c51a58eeSEd Tanous 
861c51a58eeSEd Tanous             std::string headerLoc = "Location: ";
862c51a58eeSEd Tanous             headerLoc += url.buffer();
863c51a58eeSEd Tanous 
864002d39b4SEd Tanous             taskData->payload->httpHeaders.emplace_back(std::move(headerLoc));
865a43be80fSAsmitha Karunanithi 
866*62598e31SEd Tanous             BMCWEB_LOG_DEBUG("{}: Dump creation task completed",
867*62598e31SEd Tanous                              createdObjPath.str);
868a43be80fSAsmitha Karunanithi             taskData->state = "Completed";
869b47452b2SAsmitha Karunanithi             return task::completed;
870a43be80fSAsmitha Karunanithi             },
8718e31778eSAsmitha Karunanithi             "type='signal',interface='org.freedesktop.DBus.Properties',"
8728e31778eSAsmitha Karunanithi             "member='PropertiesChanged',path='" +
8738e31778eSAsmitha Karunanithi                 createdObjPath.str + "'");
874a43be80fSAsmitha Karunanithi 
8758e31778eSAsmitha Karunanithi         // The task timer is set to max time limit within which the
8768e31778eSAsmitha Karunanithi         // requested dump will be collected.
8778e31778eSAsmitha Karunanithi         task->startTimer(std::chrono::minutes(6));
878a43be80fSAsmitha Karunanithi         task->populateResp(asyncResp->res);
8798e31778eSAsmitha Karunanithi         task->payload.emplace(payload);
8808e31778eSAsmitha Karunanithi         },
8818e31778eSAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager", createdObjPath,
8828e31778eSAsmitha Karunanithi         "org.freedesktop.DBus.Introspectable", "Introspect");
883a43be80fSAsmitha Karunanithi }
884a43be80fSAsmitha Karunanithi 
8858d1b46d7Szhanghch05 inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8868d1b46d7Szhanghch05                        const crow::Request& req, const std::string& dumpType)
887a43be80fSAsmitha Karunanithi {
888fdd26906SClaire Weinan     std::string dumpPath = getDumpEntriesPath(dumpType);
889fdd26906SClaire Weinan     if (dumpPath.empty())
890a43be80fSAsmitha Karunanithi     {
891a43be80fSAsmitha Karunanithi         messages::internalError(asyncResp->res);
892a43be80fSAsmitha Karunanithi         return;
893a43be80fSAsmitha Karunanithi     }
894a43be80fSAsmitha Karunanithi 
895a43be80fSAsmitha Karunanithi     std::optional<std::string> diagnosticDataType;
896a43be80fSAsmitha Karunanithi     std::optional<std::string> oemDiagnosticDataType;
897a43be80fSAsmitha Karunanithi 
89815ed6780SWilly Tu     if (!redfish::json_util::readJsonAction(
899a43be80fSAsmitha Karunanithi             req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
900a43be80fSAsmitha Karunanithi             "OEMDiagnosticDataType", oemDiagnosticDataType))
901a43be80fSAsmitha Karunanithi     {
902a43be80fSAsmitha Karunanithi         return;
903a43be80fSAsmitha Karunanithi     }
904a43be80fSAsmitha Karunanithi 
905a43be80fSAsmitha Karunanithi     if (dumpType == "System")
906a43be80fSAsmitha Karunanithi     {
907a43be80fSAsmitha Karunanithi         if (!oemDiagnosticDataType || !diagnosticDataType)
908a43be80fSAsmitha Karunanithi         {
909*62598e31SEd Tanous             BMCWEB_LOG_ERROR(
910*62598e31SEd Tanous                 "CreateDump action parameter 'DiagnosticDataType'/'OEMDiagnosticDataType' value not found!");
911a43be80fSAsmitha Karunanithi             messages::actionParameterMissing(
912a43be80fSAsmitha Karunanithi                 asyncResp->res, "CollectDiagnosticData",
913a43be80fSAsmitha Karunanithi                 "DiagnosticDataType & OEMDiagnosticDataType");
914a43be80fSAsmitha Karunanithi             return;
915a43be80fSAsmitha Karunanithi         }
9163174e4dfSEd Tanous         if ((*oemDiagnosticDataType != "System") ||
917a43be80fSAsmitha Karunanithi             (*diagnosticDataType != "OEM"))
918a43be80fSAsmitha Karunanithi         {
919*62598e31SEd Tanous             BMCWEB_LOG_ERROR("Wrong parameter values passed");
920ace85d60SEd Tanous             messages::internalError(asyncResp->res);
921a43be80fSAsmitha Karunanithi             return;
922a43be80fSAsmitha Karunanithi         }
9235907571dSAsmitha Karunanithi         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/";
924a43be80fSAsmitha Karunanithi     }
925a43be80fSAsmitha Karunanithi     else if (dumpType == "BMC")
926a43be80fSAsmitha Karunanithi     {
927a43be80fSAsmitha Karunanithi         if (!diagnosticDataType)
928a43be80fSAsmitha Karunanithi         {
929*62598e31SEd Tanous             BMCWEB_LOG_ERROR(
930*62598e31SEd Tanous                 "CreateDump action parameter 'DiagnosticDataType' not found!");
931a43be80fSAsmitha Karunanithi             messages::actionParameterMissing(
932a43be80fSAsmitha Karunanithi                 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
933a43be80fSAsmitha Karunanithi             return;
934a43be80fSAsmitha Karunanithi         }
9353174e4dfSEd Tanous         if (*diagnosticDataType != "Manager")
936a43be80fSAsmitha Karunanithi         {
937*62598e31SEd Tanous             BMCWEB_LOG_ERROR(
938*62598e31SEd Tanous                 "Wrong parameter value passed for 'DiagnosticDataType'");
939ace85d60SEd Tanous             messages::internalError(asyncResp->res);
940a43be80fSAsmitha Karunanithi             return;
941a43be80fSAsmitha Karunanithi         }
9425907571dSAsmitha Karunanithi         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/";
9435907571dSAsmitha Karunanithi     }
9445907571dSAsmitha Karunanithi     else
9455907571dSAsmitha Karunanithi     {
946*62598e31SEd Tanous         BMCWEB_LOG_ERROR("CreateDump failed. Unknown dump type");
9475907571dSAsmitha Karunanithi         messages::internalError(asyncResp->res);
9485907571dSAsmitha Karunanithi         return;
949a43be80fSAsmitha Karunanithi     }
950a43be80fSAsmitha Karunanithi 
9518e31778eSAsmitha Karunanithi     std::vector<std::pair<std::string, std::variant<std::string, uint64_t>>>
9528e31778eSAsmitha Karunanithi         createDumpParamVec;
9538e31778eSAsmitha Karunanithi 
954f574a8e1SCarson Labrado     if (req.session != nullptr)
955f574a8e1SCarson Labrado     {
95668dd075aSAsmitha Karunanithi         createDumpParamVec.emplace_back(
95768dd075aSAsmitha Karunanithi             "xyz.openbmc_project.Dump.Create.CreateParameters.OriginatorId",
95868dd075aSAsmitha Karunanithi             req.session->clientIp);
95968dd075aSAsmitha Karunanithi         createDumpParamVec.emplace_back(
96068dd075aSAsmitha Karunanithi             "xyz.openbmc_project.Dump.Create.CreateParameters.OriginatorType",
96168dd075aSAsmitha Karunanithi             "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.Client");
962f574a8e1SCarson Labrado     }
96368dd075aSAsmitha Karunanithi 
964a43be80fSAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
9655e7e2dc5SEd Tanous         [asyncResp, payload(task::Payload(req)),
9665e7e2dc5SEd Tanous          dumpPath](const boost::system::error_code& ec,
9675e7e2dc5SEd Tanous                    const sdbusplus::message_t& msg,
9688e31778eSAsmitha Karunanithi                    const sdbusplus::message::object_path& objPath) mutable {
969a43be80fSAsmitha Karunanithi         if (ec)
970a43be80fSAsmitha Karunanithi         {
971*62598e31SEd Tanous             BMCWEB_LOG_ERROR("CreateDump resp_handler got error {}", ec);
9725907571dSAsmitha Karunanithi             const sd_bus_error* dbusError = msg.get_error();
9735907571dSAsmitha Karunanithi             if (dbusError == nullptr)
9745907571dSAsmitha Karunanithi             {
9755907571dSAsmitha Karunanithi                 messages::internalError(asyncResp->res);
9765907571dSAsmitha Karunanithi                 return;
9775907571dSAsmitha Karunanithi             }
9785907571dSAsmitha Karunanithi 
979*62598e31SEd Tanous             BMCWEB_LOG_ERROR("CreateDump DBus error: {} and error msg: {}",
980*62598e31SEd Tanous                              dbusError->name, dbusError->message);
9815907571dSAsmitha Karunanithi             if (std::string_view(
9825907571dSAsmitha Karunanithi                     "xyz.openbmc_project.Common.Error.NotAllowed") ==
9835907571dSAsmitha Karunanithi                 dbusError->name)
9845907571dSAsmitha Karunanithi             {
9855907571dSAsmitha Karunanithi                 messages::resourceInStandby(asyncResp->res);
9865907571dSAsmitha Karunanithi                 return;
9875907571dSAsmitha Karunanithi             }
9885907571dSAsmitha Karunanithi             if (std::string_view(
9895907571dSAsmitha Karunanithi                     "xyz.openbmc_project.Dump.Create.Error.Disabled") ==
9905907571dSAsmitha Karunanithi                 dbusError->name)
9915907571dSAsmitha Karunanithi             {
9925907571dSAsmitha Karunanithi                 messages::serviceDisabled(asyncResp->res, dumpPath);
9935907571dSAsmitha Karunanithi                 return;
9945907571dSAsmitha Karunanithi             }
9955907571dSAsmitha Karunanithi             if (std::string_view(
9965907571dSAsmitha Karunanithi                     "xyz.openbmc_project.Common.Error.Unavailable") ==
9975907571dSAsmitha Karunanithi                 dbusError->name)
9985907571dSAsmitha Karunanithi             {
9995907571dSAsmitha Karunanithi                 messages::resourceInUse(asyncResp->res);
10005907571dSAsmitha Karunanithi                 return;
10015907571dSAsmitha Karunanithi             }
10025907571dSAsmitha Karunanithi             // Other Dbus errors such as:
10035907571dSAsmitha Karunanithi             // xyz.openbmc_project.Common.Error.InvalidArgument &
10045907571dSAsmitha Karunanithi             // org.freedesktop.DBus.Error.InvalidArgs are all related to
10055907571dSAsmitha Karunanithi             // the dbus call that is made here in the bmcweb
10065907571dSAsmitha Karunanithi             // implementation and has nothing to do with the client's
10075907571dSAsmitha Karunanithi             // input in the request. Hence, returning internal error
10085907571dSAsmitha Karunanithi             // back to the client.
1009a43be80fSAsmitha Karunanithi             messages::internalError(asyncResp->res);
1010a43be80fSAsmitha Karunanithi             return;
1011a43be80fSAsmitha Karunanithi         }
1012*62598e31SEd Tanous         BMCWEB_LOG_DEBUG("Dump Created. Path: {}", objPath.str);
10138e31778eSAsmitha Karunanithi         createDumpTaskCallback(std::move(payload), asyncResp, objPath);
1014a43be80fSAsmitha Karunanithi         },
1015b47452b2SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager",
1016b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" +
1017b47452b2SAsmitha Karunanithi             std::string(boost::algorithm::to_lower_copy(dumpType)),
10188e31778eSAsmitha Karunanithi         "xyz.openbmc_project.Dump.Create", "CreateDump", createDumpParamVec);
1019a43be80fSAsmitha Karunanithi }
1020a43be80fSAsmitha Karunanithi 
10218d1b46d7Szhanghch05 inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
10228d1b46d7Szhanghch05                       const std::string& dumpType)
102380319af1SAsmitha Karunanithi {
1024b47452b2SAsmitha Karunanithi     std::string dumpTypeLowerCopy =
1025b47452b2SAsmitha Karunanithi         std::string(boost::algorithm::to_lower_copy(dumpType));
10268d1b46d7Szhanghch05 
10270d946211SClaire Weinan     crow::connections::systemBus->async_method_call(
10280d946211SClaire Weinan         [asyncResp](const boost::system::error_code& ec) {
102980319af1SAsmitha Karunanithi         if (ec)
103080319af1SAsmitha Karunanithi         {
1031*62598e31SEd Tanous             BMCWEB_LOG_ERROR("clearDump resp_handler got error {}", ec);
103280319af1SAsmitha Karunanithi             messages::internalError(asyncResp->res);
103380319af1SAsmitha Karunanithi             return;
103480319af1SAsmitha Karunanithi         }
10350d946211SClaire Weinan         },
10360d946211SClaire Weinan         "xyz.openbmc_project.Dump.Manager",
10370d946211SClaire Weinan         "/xyz/openbmc_project/dump/" + dumpTypeLowerCopy,
10380d946211SClaire Weinan         "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
103980319af1SAsmitha Karunanithi }
104080319af1SAsmitha Karunanithi 
1041b9d36b47SEd Tanous inline static void
1042b9d36b47SEd Tanous     parseCrashdumpParameters(const dbus::utility::DBusPropertiesMap& params,
1043b9d36b47SEd Tanous                              std::string& filename, std::string& timestamp,
1044b9d36b47SEd Tanous                              std::string& logfile)
1045043a0536SJohnathan Mantey {
1046d1bde9e5SKrzysztof Grobelny     const std::string* filenamePtr = nullptr;
1047d1bde9e5SKrzysztof Grobelny     const std::string* timestampPtr = nullptr;
1048d1bde9e5SKrzysztof Grobelny     const std::string* logfilePtr = nullptr;
1049d1bde9e5SKrzysztof Grobelny 
1050d1bde9e5SKrzysztof Grobelny     const bool success = sdbusplus::unpackPropertiesNoThrow(
1051d1bde9e5SKrzysztof Grobelny         dbus_utils::UnpackErrorPrinter(), params, "Timestamp", timestampPtr,
1052d1bde9e5SKrzysztof Grobelny         "Filename", filenamePtr, "Log", logfilePtr);
1053d1bde9e5SKrzysztof Grobelny 
1054d1bde9e5SKrzysztof Grobelny     if (!success)
1055043a0536SJohnathan Mantey     {
1056d1bde9e5SKrzysztof Grobelny         return;
1057043a0536SJohnathan Mantey     }
1058d1bde9e5SKrzysztof Grobelny 
1059d1bde9e5SKrzysztof Grobelny     if (filenamePtr != nullptr)
1060043a0536SJohnathan Mantey     {
1061d1bde9e5SKrzysztof Grobelny         filename = *filenamePtr;
1062d1bde9e5SKrzysztof Grobelny     }
1063d1bde9e5SKrzysztof Grobelny 
1064d1bde9e5SKrzysztof Grobelny     if (timestampPtr != nullptr)
1065043a0536SJohnathan Mantey     {
1066d1bde9e5SKrzysztof Grobelny         timestamp = *timestampPtr;
1067043a0536SJohnathan Mantey     }
1068d1bde9e5SKrzysztof Grobelny 
1069d1bde9e5SKrzysztof Grobelny     if (logfilePtr != nullptr)
1070043a0536SJohnathan Mantey     {
1071d1bde9e5SKrzysztof Grobelny         logfile = *logfilePtr;
1072043a0536SJohnathan Mantey     }
1073043a0536SJohnathan Mantey }
1074043a0536SJohnathan Mantey 
10757e860f15SJohn Edward Broadbent inline void requestRoutesSystemLogServiceCollection(App& app)
10761da66f75SEd Tanous {
1077c4bf6374SJason M. Bills     /**
1078c4bf6374SJason M. Bills      * Functions triggers appropriate requests on DBus
1079c4bf6374SJason M. Bills      */
108022d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/")
1081ed398213SEd Tanous         .privileges(redfish::privileges::getLogServiceCollection)
1082002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1083002d39b4SEd Tanous             [&app](const crow::Request& req,
108422d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
108522d268cbSEd Tanous                    const std::string& systemName) {
10863ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1087c4bf6374SJason M. Bills         {
108845ca1b86SEd Tanous             return;
108945ca1b86SEd Tanous         }
10907f3e84a1SEd Tanous         if constexpr (bmcwebEnableMultiHost)
10917f3e84a1SEd Tanous         {
10927f3e84a1SEd Tanous             // Option currently returns no systems.  TBD
10937f3e84a1SEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
10947f3e84a1SEd Tanous                                        systemName);
10957f3e84a1SEd Tanous             return;
10967f3e84a1SEd Tanous         }
109722d268cbSEd Tanous         if (systemName != "system")
109822d268cbSEd Tanous         {
109922d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
110022d268cbSEd Tanous                                        systemName);
110122d268cbSEd Tanous             return;
110222d268cbSEd Tanous         }
110322d268cbSEd Tanous 
11047e860f15SJohn Edward Broadbent         // Collections don't include the static data added by SubRoute
11057e860f15SJohn Edward Broadbent         // because it has a duplicate entry for members
1106c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1107c4bf6374SJason M. Bills             "#LogServiceCollection.LogServiceCollection";
1108c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
1109029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices";
111045ca1b86SEd Tanous         asyncResp->res.jsonValue["Name"] = "System Log Services Collection";
1111c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] =
1112c4bf6374SJason M. Bills             "Collection of LogServices for this Computer System";
1113002d39b4SEd Tanous         nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
1114c4bf6374SJason M. Bills         logServiceArray = nlohmann::json::array();
11151476687dSEd Tanous         nlohmann::json::object_t eventLog;
11161476687dSEd Tanous         eventLog["@odata.id"] =
11171476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog";
1118b2ba3072SPatrick Williams         logServiceArray.emplace_back(std::move(eventLog));
11195cb1dd27SAsmitha Karunanithi #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
11201476687dSEd Tanous         nlohmann::json::object_t dumpLog;
1121002d39b4SEd Tanous         dumpLog["@odata.id"] = "/redfish/v1/Systems/system/LogServices/Dump";
1122b2ba3072SPatrick Williams         logServiceArray.emplace_back(std::move(dumpLog));
1123c9bb6861Sraviteja-b #endif
1124c9bb6861Sraviteja-b 
1125d53dd41fSJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
11261476687dSEd Tanous         nlohmann::json::object_t crashdump;
11271476687dSEd Tanous         crashdump["@odata.id"] =
11281476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Crashdump";
1129b2ba3072SPatrick Williams         logServiceArray.emplace_back(std::move(crashdump));
1130d53dd41fSJason M. Bills #endif
1131b7028ebfSSpencer Ku 
1132b7028ebfSSpencer Ku #ifdef BMCWEB_ENABLE_REDFISH_HOST_LOGGER
11331476687dSEd Tanous         nlohmann::json::object_t hostlogger;
11341476687dSEd Tanous         hostlogger["@odata.id"] =
11351476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/HostLogger";
1136b2ba3072SPatrick Williams         logServiceArray.emplace_back(std::move(hostlogger));
1137b7028ebfSSpencer Ku #endif
1138c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] =
1139c4bf6374SJason M. Bills             logServiceArray.size();
1140a3316fc6SZhikuiRen 
11417a1dbc48SGeorge Liu         constexpr std::array<std::string_view, 1> interfaces = {
11427a1dbc48SGeorge Liu             "xyz.openbmc_project.State.Boot.PostCode"};
11437a1dbc48SGeorge Liu         dbus::utility::getSubTreePaths(
11447a1dbc48SGeorge Liu             "/", 0, interfaces,
11457a1dbc48SGeorge Liu             [asyncResp](const boost::system::error_code& ec,
1146b9d36b47SEd Tanous                         const dbus::utility::MapperGetSubTreePathsResponse&
1147b9d36b47SEd Tanous                             subtreePath) {
1148a3316fc6SZhikuiRen             if (ec)
1149a3316fc6SZhikuiRen             {
1150*62598e31SEd Tanous                 BMCWEB_LOG_ERROR("{}", ec);
1151a3316fc6SZhikuiRen                 return;
1152a3316fc6SZhikuiRen             }
1153a3316fc6SZhikuiRen 
115455f79e6fSEd Tanous             for (const auto& pathStr : subtreePath)
1155a3316fc6SZhikuiRen             {
1156a3316fc6SZhikuiRen                 if (pathStr.find("PostCode") != std::string::npos)
1157a3316fc6SZhikuiRen                 {
115823a21a1cSEd Tanous                     nlohmann::json& logServiceArrayLocal =
1159a3316fc6SZhikuiRen                         asyncResp->res.jsonValue["Members"];
1160613dabeaSEd Tanous                     nlohmann::json::object_t member;
1161613dabeaSEd Tanous                     member["@odata.id"] =
1162613dabeaSEd Tanous                         "/redfish/v1/Systems/system/LogServices/PostCodes";
1163613dabeaSEd Tanous 
1164b2ba3072SPatrick Williams                     logServiceArrayLocal.emplace_back(std::move(member));
1165613dabeaSEd Tanous 
116645ca1b86SEd Tanous                     asyncResp->res.jsonValue["Members@odata.count"] =
116723a21a1cSEd Tanous                         logServiceArrayLocal.size();
1168a3316fc6SZhikuiRen                     return;
1169a3316fc6SZhikuiRen                 }
1170a3316fc6SZhikuiRen             }
11717a1dbc48SGeorge Liu             });
11727e860f15SJohn Edward Broadbent         });
1173c4bf6374SJason M. Bills }
1174c4bf6374SJason M. Bills 
11757e860f15SJohn Edward Broadbent inline void requestRoutesEventLogService(App& app)
1176c4bf6374SJason M. Bills {
117722d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/")
1178ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
1179002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1180002d39b4SEd Tanous             [&app](const crow::Request& req,
118122d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
118222d268cbSEd Tanous                    const std::string& systemName) {
11833ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
118445ca1b86SEd Tanous         {
118545ca1b86SEd Tanous             return;
118645ca1b86SEd Tanous         }
118722d268cbSEd Tanous         if (systemName != "system")
118822d268cbSEd Tanous         {
118922d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
119022d268cbSEd Tanous                                        systemName);
119122d268cbSEd Tanous             return;
119222d268cbSEd Tanous         }
1193c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
1194029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog";
1195c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1196c4bf6374SJason M. Bills             "#LogService.v1_1_0.LogService";
1197c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Event Log Service";
1198002d39b4SEd Tanous         asyncResp->res.jsonValue["Description"] = "System Event Log Service";
1199c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Id"] = "EventLog";
1200c4bf6374SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
12017c8c4058STejas Patil 
12027c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
12032b82937eSEd Tanous             redfish::time_utils::getDateTimeOffsetNow();
12047c8c4058STejas Patil 
12057c8c4058STejas Patil         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
12067c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
12077c8c4058STejas Patil             redfishDateTimeOffset.second;
12087c8c4058STejas Patil 
12091476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
12101476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1211e7d6c8b2SGunnar Mills         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
1212e7d6c8b2SGunnar Mills 
12130fda0f12SGeorge Liu             {"target",
12140fda0f12SGeorge Liu              "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog"}};
12157e860f15SJohn Edward Broadbent         });
1216489640c6SJason M. Bills }
1217489640c6SJason M. Bills 
12187e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogClear(App& app)
1219489640c6SJason M. Bills {
12204978b63fSJason M. Bills     BMCWEB_ROUTE(
12214978b63fSJason M. Bills         app,
122222d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
1223432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
12247e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
122545ca1b86SEd Tanous             [&app](const crow::Request& req,
122622d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
122722d268cbSEd Tanous                    const std::string& systemName) {
12283ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
122945ca1b86SEd Tanous         {
123045ca1b86SEd Tanous             return;
123145ca1b86SEd Tanous         }
123222d268cbSEd Tanous         if (systemName != "system")
123322d268cbSEd Tanous         {
123422d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
123522d268cbSEd Tanous                                        systemName);
123622d268cbSEd Tanous             return;
123722d268cbSEd Tanous         }
1238489640c6SJason M. Bills         // Clear the EventLog by deleting the log files
1239489640c6SJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
1240489640c6SJason M. Bills         if (getRedfishLogFiles(redfishLogFiles))
1241489640c6SJason M. Bills         {
1242489640c6SJason M. Bills             for (const std::filesystem::path& file : redfishLogFiles)
1243489640c6SJason M. Bills             {
1244489640c6SJason M. Bills                 std::error_code ec;
1245489640c6SJason M. Bills                 std::filesystem::remove(file, ec);
1246489640c6SJason M. Bills             }
1247489640c6SJason M. Bills         }
1248489640c6SJason M. Bills 
1249489640c6SJason M. Bills         // Reload rsyslog so it knows to start new log files
1250489640c6SJason M. Bills         crow::connections::systemBus->async_method_call(
12515e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec) {
1252489640c6SJason M. Bills             if (ec)
1253489640c6SJason M. Bills             {
1254*62598e31SEd Tanous                 BMCWEB_LOG_ERROR("Failed to reload rsyslog: {}", ec);
1255489640c6SJason M. Bills                 messages::internalError(asyncResp->res);
1256489640c6SJason M. Bills                 return;
1257489640c6SJason M. Bills             }
1258489640c6SJason M. Bills 
1259489640c6SJason M. Bills             messages::success(asyncResp->res);
1260489640c6SJason M. Bills             },
1261489640c6SJason M. Bills             "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1262002d39b4SEd Tanous             "org.freedesktop.systemd1.Manager", "ReloadUnit", "rsyslog.service",
1263002d39b4SEd Tanous             "replace");
12647e860f15SJohn Edward Broadbent         });
1265c4bf6374SJason M. Bills }
1266c4bf6374SJason M. Bills 
1267ac992cdeSJason M. Bills enum class LogParseError
1268ac992cdeSJason M. Bills {
1269ac992cdeSJason M. Bills     success,
1270ac992cdeSJason M. Bills     parseFailed,
1271ac992cdeSJason M. Bills     messageIdNotInRegistry,
1272ac992cdeSJason M. Bills };
1273ac992cdeSJason M. Bills 
1274ac992cdeSJason M. Bills static LogParseError
1275ac992cdeSJason M. Bills     fillEventLogEntryJson(const std::string& logEntryID,
1276b5a76932SEd Tanous                           const std::string& logEntry,
1277de703c5dSJason M. Bills                           nlohmann::json::object_t& logEntryJson)
1278c4bf6374SJason M. Bills {
127995820184SJason M. Bills     // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
1280cd225da8SJason M. Bills     // First get the Timestamp
1281f23b7296SEd Tanous     size_t space = logEntry.find_first_of(' ');
1282cd225da8SJason M. Bills     if (space == std::string::npos)
128395820184SJason M. Bills     {
1284ac992cdeSJason M. Bills         return LogParseError::parseFailed;
128595820184SJason M. Bills     }
1286cd225da8SJason M. Bills     std::string timestamp = logEntry.substr(0, space);
1287cd225da8SJason M. Bills     // Then get the log contents
1288f23b7296SEd Tanous     size_t entryStart = logEntry.find_first_not_of(' ', space);
1289cd225da8SJason M. Bills     if (entryStart == std::string::npos)
1290cd225da8SJason M. Bills     {
1291ac992cdeSJason M. Bills         return LogParseError::parseFailed;
1292cd225da8SJason M. Bills     }
1293cd225da8SJason M. Bills     std::string_view entry(logEntry);
1294cd225da8SJason M. Bills     entry.remove_prefix(entryStart);
1295cd225da8SJason M. Bills     // Use split to separate the entry into its fields
1296cd225da8SJason M. Bills     std::vector<std::string> logEntryFields;
129750ebd4afSEd Tanous     bmcweb::split(logEntryFields, entry, ',');
1298cd225da8SJason M. Bills     // We need at least a MessageId to be valid
12991e6deaf6SEd Tanous     auto logEntryIter = logEntryFields.begin();
13001e6deaf6SEd Tanous     if (logEntryIter == logEntryFields.end())
1301cd225da8SJason M. Bills     {
1302ac992cdeSJason M. Bills         return LogParseError::parseFailed;
1303cd225da8SJason M. Bills     }
13041e6deaf6SEd Tanous     std::string& messageID = *logEntryIter;
13054851d45dSJason M. Bills     // Get the Message from the MessageRegistry
1306fffb8c1fSEd Tanous     const registries::Message* message = registries::getMessage(messageID);
1307c4bf6374SJason M. Bills 
13081e6deaf6SEd Tanous     logEntryIter++;
130954417b02SSui Chen     if (message == nullptr)
1310c4bf6374SJason M. Bills     {
1311*62598e31SEd Tanous         BMCWEB_LOG_WARNING("Log entry not found in registry: {}", logEntry);
1312ac992cdeSJason M. Bills         return LogParseError::messageIdNotInRegistry;
1313c4bf6374SJason M. Bills     }
1314c4bf6374SJason M. Bills 
13151e6deaf6SEd Tanous     std::vector<std::string_view> messageArgs(logEntryIter,
13161e6deaf6SEd Tanous                                               logEntryFields.end());
1317c05bba45SEd Tanous     messageArgs.resize(message->numberOfArgs);
1318c05bba45SEd Tanous 
13191e6deaf6SEd Tanous     std::string msg = redfish::registries::fillMessageArgs(messageArgs,
13201e6deaf6SEd Tanous                                                            message->message);
13211e6deaf6SEd Tanous     if (msg.empty())
132215a86ff6SJason M. Bills     {
13231e6deaf6SEd Tanous         return LogParseError::parseFailed;
132415a86ff6SJason M. Bills     }
13254851d45dSJason M. Bills 
132695820184SJason M. Bills     // Get the Created time from the timestamp. The log timestamp is in RFC3339
132795820184SJason M. Bills     // format which matches the Redfish format except for the fractional seconds
132895820184SJason M. Bills     // between the '.' and the '+', so just remove them.
1329f23b7296SEd Tanous     std::size_t dot = timestamp.find_first_of('.');
1330f23b7296SEd Tanous     std::size_t plus = timestamp.find_first_of('+');
133195820184SJason M. Bills     if (dot != std::string::npos && plus != std::string::npos)
1332c4bf6374SJason M. Bills     {
133395820184SJason M. Bills         timestamp.erase(dot, plus - dot);
1334c4bf6374SJason M. Bills     }
1335c4bf6374SJason M. Bills 
1336c4bf6374SJason M. Bills     // Fill in the log entry with the gathered data
13379c11a172SVijay Lobo     logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
1338ef4c65b7SEd Tanous     logEntryJson["@odata.id"] = boost::urls::format(
1339ef4c65b7SEd Tanous         "/redfish/v1/Systems/system/LogServices/EventLog/Entries/{}",
1340ef4c65b7SEd Tanous         logEntryID);
134184afc48bSJason M. Bills     logEntryJson["Name"] = "System Event Log Entry";
134284afc48bSJason M. Bills     logEntryJson["Id"] = logEntryID;
134384afc48bSJason M. Bills     logEntryJson["Message"] = std::move(msg);
134484afc48bSJason M. Bills     logEntryJson["MessageId"] = std::move(messageID);
134584afc48bSJason M. Bills     logEntryJson["MessageArgs"] = messageArgs;
134684afc48bSJason M. Bills     logEntryJson["EntryType"] = "Event";
134784afc48bSJason M. Bills     logEntryJson["Severity"] = message->messageSeverity;
134884afc48bSJason M. Bills     logEntryJson["Created"] = std::move(timestamp);
1349ac992cdeSJason M. Bills     return LogParseError::success;
1350c4bf6374SJason M. Bills }
1351c4bf6374SJason M. Bills 
13527e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogEntryCollection(App& app)
1353c4bf6374SJason M. Bills {
135422d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
13558b6a35f0SGunnar Mills         .privileges(redfish::privileges::getLogEntryCollection)
1356002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1357002d39b4SEd Tanous             [&app](const crow::Request& req,
135822d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
135922d268cbSEd Tanous                    const std::string& systemName) {
1360c937d2bfSEd Tanous         query_param::QueryCapabilities capabilities = {
1361c937d2bfSEd Tanous             .canDelegateTop = true,
1362c937d2bfSEd Tanous             .canDelegateSkip = true,
1363c937d2bfSEd Tanous         };
1364c937d2bfSEd Tanous         query_param::Query delegatedQuery;
1365c937d2bfSEd Tanous         if (!redfish::setUpRedfishRouteWithDelegation(
13663ba00073SCarson Labrado                 app, req, asyncResp, delegatedQuery, capabilities))
1367c4bf6374SJason M. Bills         {
1368c4bf6374SJason M. Bills             return;
1369c4bf6374SJason M. Bills         }
13707f3e84a1SEd Tanous         if constexpr (bmcwebEnableMultiHost)
13717f3e84a1SEd Tanous         {
13727f3e84a1SEd Tanous             // Option currently returns no systems.  TBD
13737f3e84a1SEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
13747f3e84a1SEd Tanous                                        systemName);
13757f3e84a1SEd Tanous             return;
13767f3e84a1SEd Tanous         }
137722d268cbSEd Tanous         if (systemName != "system")
137822d268cbSEd Tanous         {
137922d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
138022d268cbSEd Tanous                                        systemName);
138122d268cbSEd Tanous             return;
138222d268cbSEd Tanous         }
138322d268cbSEd Tanous 
13845143f7a5SJiaqing Zhao         size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
13853648c8beSEd Tanous         size_t skip = delegatedQuery.skip.value_or(0);
13863648c8beSEd Tanous 
13877e860f15SJohn Edward Broadbent         // Collections don't include the static data added by SubRoute
13887e860f15SJohn Edward Broadbent         // because it has a duplicate entry for members
1389c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1390c4bf6374SJason M. Bills             "#LogEntryCollection.LogEntryCollection";
1391c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
1392029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1393c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1394c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] =
1395c4bf6374SJason M. Bills             "Collection of System Event Log Entries";
1396cb92c03bSAndrew Geissler 
13974978b63fSJason M. Bills         nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1398c4bf6374SJason M. Bills         logEntryArray = nlohmann::json::array();
13997e860f15SJohn Edward Broadbent         // Go through the log files and create a unique ID for each
14007e860f15SJohn Edward Broadbent         // entry
140195820184SJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
140295820184SJason M. Bills         getRedfishLogFiles(redfishLogFiles);
1403b01bf299SEd Tanous         uint64_t entryCount = 0;
1404cd225da8SJason M. Bills         std::string logEntry;
140595820184SJason M. Bills 
14067e860f15SJohn Edward Broadbent         // Oldest logs are in the last file, so start there and loop
14077e860f15SJohn Edward Broadbent         // backwards
1408002d39b4SEd Tanous         for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1409002d39b4SEd Tanous              it++)
1410c4bf6374SJason M. Bills         {
1411cd225da8SJason M. Bills             std::ifstream logStream(*it);
141295820184SJason M. Bills             if (!logStream.is_open())
1413c4bf6374SJason M. Bills             {
1414c4bf6374SJason M. Bills                 continue;
1415c4bf6374SJason M. Bills             }
1416c4bf6374SJason M. Bills 
1417e85d6b16SJason M. Bills             // Reset the unique ID on the first entry
1418e85d6b16SJason M. Bills             bool firstEntry = true;
141995820184SJason M. Bills             while (std::getline(logStream, logEntry))
142095820184SJason M. Bills             {
1421c4bf6374SJason M. Bills                 std::string idStr;
1422e85d6b16SJason M. Bills                 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1423c4bf6374SJason M. Bills                 {
1424c4bf6374SJason M. Bills                     continue;
1425c4bf6374SJason M. Bills                 }
1426e85d6b16SJason M. Bills                 firstEntry = false;
1427e85d6b16SJason M. Bills 
1428de703c5dSJason M. Bills                 nlohmann::json::object_t bmcLogEntry;
142989492a15SPatrick Williams                 LogParseError status = fillEventLogEntryJson(idStr, logEntry,
143089492a15SPatrick Williams                                                              bmcLogEntry);
1431ac992cdeSJason M. Bills                 if (status == LogParseError::messageIdNotInRegistry)
1432ac992cdeSJason M. Bills                 {
1433ac992cdeSJason M. Bills                     continue;
1434ac992cdeSJason M. Bills                 }
1435ac992cdeSJason M. Bills                 if (status != LogParseError::success)
1436c4bf6374SJason M. Bills                 {
1437c4bf6374SJason M. Bills                     messages::internalError(asyncResp->res);
1438c4bf6374SJason M. Bills                     return;
1439c4bf6374SJason M. Bills                 }
1440de703c5dSJason M. Bills 
1441de703c5dSJason M. Bills                 entryCount++;
1442de703c5dSJason M. Bills                 // Handle paging using skip (number of entries to skip from the
1443de703c5dSJason M. Bills                 // start) and top (number of entries to display)
14443648c8beSEd Tanous                 if (entryCount <= skip || entryCount > skip + top)
1445de703c5dSJason M. Bills                 {
1446de703c5dSJason M. Bills                     continue;
1447de703c5dSJason M. Bills                 }
1448de703c5dSJason M. Bills 
1449b2ba3072SPatrick Williams                 logEntryArray.emplace_back(std::move(bmcLogEntry));
1450c4bf6374SJason M. Bills             }
145195820184SJason M. Bills         }
1452c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
14533648c8beSEd Tanous         if (skip + top < entryCount)
1454c4bf6374SJason M. Bills         {
1455c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Members@odata.nextLink"] =
14564978b63fSJason M. Bills                 "/redfish/v1/Systems/system/LogServices/EventLog/Entries?$skip=" +
14573648c8beSEd Tanous                 std::to_string(skip + top);
1458c4bf6374SJason M. Bills         }
14597e860f15SJohn Edward Broadbent         });
1460897967deSJason M. Bills }
1461897967deSJason M. Bills 
14627e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogEntry(App& app)
1463897967deSJason M. Bills {
14647e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
146522d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
1466ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
14677e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
146845ca1b86SEd Tanous             [&app](const crow::Request& req,
14697e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
147022d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
14713ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
147245ca1b86SEd Tanous         {
147345ca1b86SEd Tanous             return;
147445ca1b86SEd Tanous         }
14757f3e84a1SEd Tanous         if constexpr (bmcwebEnableMultiHost)
14767f3e84a1SEd Tanous         {
14777f3e84a1SEd Tanous             // Option currently returns no systems.  TBD
14787f3e84a1SEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
14797f3e84a1SEd Tanous                                        systemName);
14807f3e84a1SEd Tanous             return;
14817f3e84a1SEd Tanous         }
148222d268cbSEd Tanous 
148322d268cbSEd Tanous         if (systemName != "system")
148422d268cbSEd Tanous         {
148522d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
148622d268cbSEd Tanous                                        systemName);
148722d268cbSEd Tanous             return;
148822d268cbSEd Tanous         }
148922d268cbSEd Tanous 
14907e860f15SJohn Edward Broadbent         const std::string& targetID = param;
14918d1b46d7Szhanghch05 
14927e860f15SJohn Edward Broadbent         // Go through the log files and check the unique ID for each
14937e860f15SJohn Edward Broadbent         // entry to find the target entry
1494897967deSJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
1495897967deSJason M. Bills         getRedfishLogFiles(redfishLogFiles);
1496897967deSJason M. Bills         std::string logEntry;
1497897967deSJason M. Bills 
14987e860f15SJohn Edward Broadbent         // Oldest logs are in the last file, so start there and loop
14997e860f15SJohn Edward Broadbent         // backwards
1500002d39b4SEd Tanous         for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1501002d39b4SEd Tanous              it++)
1502897967deSJason M. Bills         {
1503897967deSJason M. Bills             std::ifstream logStream(*it);
1504897967deSJason M. Bills             if (!logStream.is_open())
1505897967deSJason M. Bills             {
1506897967deSJason M. Bills                 continue;
1507897967deSJason M. Bills             }
1508897967deSJason M. Bills 
1509897967deSJason M. Bills             // Reset the unique ID on the first entry
1510897967deSJason M. Bills             bool firstEntry = true;
1511897967deSJason M. Bills             while (std::getline(logStream, logEntry))
1512897967deSJason M. Bills             {
1513897967deSJason M. Bills                 std::string idStr;
1514897967deSJason M. Bills                 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1515897967deSJason M. Bills                 {
1516897967deSJason M. Bills                     continue;
1517897967deSJason M. Bills                 }
1518897967deSJason M. Bills                 firstEntry = false;
1519897967deSJason M. Bills 
1520897967deSJason M. Bills                 if (idStr == targetID)
1521897967deSJason M. Bills                 {
1522de703c5dSJason M. Bills                     nlohmann::json::object_t bmcLogEntry;
1523ac992cdeSJason M. Bills                     LogParseError status =
1524ac992cdeSJason M. Bills                         fillEventLogEntryJson(idStr, logEntry, bmcLogEntry);
1525ac992cdeSJason M. Bills                     if (status != LogParseError::success)
1526897967deSJason M. Bills                     {
1527897967deSJason M. Bills                         messages::internalError(asyncResp->res);
1528897967deSJason M. Bills                         return;
1529897967deSJason M. Bills                     }
1530d405bb51SJason M. Bills                     asyncResp->res.jsonValue.update(bmcLogEntry);
1531897967deSJason M. Bills                     return;
1532897967deSJason M. Bills                 }
1533897967deSJason M. Bills             }
1534897967deSJason M. Bills         }
1535897967deSJason M. Bills         // Requested ID was not found
15369db4ba25SJiaqing Zhao         messages::resourceNotFound(asyncResp->res, "LogEntry", targetID);
15377e860f15SJohn Edward Broadbent         });
153808a4e4b5SAnthony Wilson }
153908a4e4b5SAnthony Wilson 
15407e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntryCollection(App& app)
154108a4e4b5SAnthony Wilson {
154222d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
1543ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
1544002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1545002d39b4SEd Tanous             [&app](const crow::Request& req,
154622d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
154722d268cbSEd Tanous                    const std::string& systemName) {
15483ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
154945ca1b86SEd Tanous         {
155045ca1b86SEd Tanous             return;
155145ca1b86SEd Tanous         }
15527f3e84a1SEd Tanous         if constexpr (bmcwebEnableMultiHost)
15537f3e84a1SEd Tanous         {
15547f3e84a1SEd Tanous             // Option currently returns no systems.  TBD
15557f3e84a1SEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
15567f3e84a1SEd Tanous                                        systemName);
15577f3e84a1SEd Tanous             return;
15587f3e84a1SEd Tanous         }
155922d268cbSEd Tanous         if (systemName != "system")
156022d268cbSEd Tanous         {
156122d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
156222d268cbSEd Tanous                                        systemName);
156322d268cbSEd Tanous             return;
156422d268cbSEd Tanous         }
156522d268cbSEd Tanous 
15667e860f15SJohn Edward Broadbent         // Collections don't include the static data added by SubRoute
15677e860f15SJohn Edward Broadbent         // because it has a duplicate entry for members
156808a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["@odata.type"] =
156908a4e4b5SAnthony Wilson             "#LogEntryCollection.LogEntryCollection";
157008a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["@odata.id"] =
157108a4e4b5SAnthony Wilson             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
157208a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
157308a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["Description"] =
157408a4e4b5SAnthony Wilson             "Collection of System Event Log Entries";
157508a4e4b5SAnthony Wilson 
1576cb92c03bSAndrew Geissler         // DBus implementation of EventLog/Entries
1577cb92c03bSAndrew Geissler         // Make call to Logging Service to find all log entry objects
15785eb468daSGeorge Liu         sdbusplus::message::object_path path("/xyz/openbmc_project/logging");
15795eb468daSGeorge Liu         dbus::utility::getManagedObjects(
15805eb468daSGeorge Liu             "xyz.openbmc_project.Logging", path,
15815e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec,
1582914e2d5dSEd Tanous                         const dbus::utility::ManagedObjectType& resp) {
1583cb92c03bSAndrew Geissler             if (ec)
1584cb92c03bSAndrew Geissler             {
1585cb92c03bSAndrew Geissler                 // TODO Handle for specific error code
1586*62598e31SEd Tanous                 BMCWEB_LOG_ERROR(
1587*62598e31SEd Tanous                     "getLogEntriesIfaceData resp_handler got error {}", ec);
1588cb92c03bSAndrew Geissler                 messages::internalError(asyncResp->res);
1589cb92c03bSAndrew Geissler                 return;
1590cb92c03bSAndrew Geissler             }
1591002d39b4SEd Tanous             nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
1592cb92c03bSAndrew Geissler             entriesArray = nlohmann::json::array();
15939eb808c1SEd Tanous             for (const auto& objectPath : resp)
1594cb92c03bSAndrew Geissler             {
1595914e2d5dSEd Tanous                 const uint32_t* id = nullptr;
1596c419c759SEd Tanous                 const uint64_t* timestamp = nullptr;
1597c419c759SEd Tanous                 const uint64_t* updateTimestamp = nullptr;
1598914e2d5dSEd Tanous                 const std::string* severity = nullptr;
1599914e2d5dSEd Tanous                 const std::string* message = nullptr;
1600914e2d5dSEd Tanous                 const std::string* filePath = nullptr;
16019c11a172SVijay Lobo                 const std::string* resolution = nullptr;
160275710de2SXiaochao Ma                 bool resolved = false;
16039017faf2SAbhishek Patel                 const std::string* notify = nullptr;
16049017faf2SAbhishek Patel 
16059eb808c1SEd Tanous                 for (const auto& interfaceMap : objectPath.second)
1606f86bb901SAdriana Kobylak                 {
1607f86bb901SAdriana Kobylak                     if (interfaceMap.first ==
1608f86bb901SAdriana Kobylak                         "xyz.openbmc_project.Logging.Entry")
1609f86bb901SAdriana Kobylak                     {
1610002d39b4SEd Tanous                         for (const auto& propertyMap : interfaceMap.second)
1611cb92c03bSAndrew Geissler                         {
1612cb92c03bSAndrew Geissler                             if (propertyMap.first == "Id")
1613cb92c03bSAndrew Geissler                             {
1614002d39b4SEd Tanous                                 id = std::get_if<uint32_t>(&propertyMap.second);
1615cb92c03bSAndrew Geissler                             }
1616cb92c03bSAndrew Geissler                             else if (propertyMap.first == "Timestamp")
1617cb92c03bSAndrew Geissler                             {
1618002d39b4SEd Tanous                                 timestamp =
1619002d39b4SEd Tanous                                     std::get_if<uint64_t>(&propertyMap.second);
16207e860f15SJohn Edward Broadbent                             }
1621002d39b4SEd Tanous                             else if (propertyMap.first == "UpdateTimestamp")
16227e860f15SJohn Edward Broadbent                             {
1623002d39b4SEd Tanous                                 updateTimestamp =
1624002d39b4SEd Tanous                                     std::get_if<uint64_t>(&propertyMap.second);
16257e860f15SJohn Edward Broadbent                             }
16267e860f15SJohn Edward Broadbent                             else if (propertyMap.first == "Severity")
16277e860f15SJohn Edward Broadbent                             {
16287e860f15SJohn Edward Broadbent                                 severity = std::get_if<std::string>(
16297e860f15SJohn Edward Broadbent                                     &propertyMap.second);
16307e860f15SJohn Edward Broadbent                             }
16319c11a172SVijay Lobo                             else if (propertyMap.first == "Resolution")
16329c11a172SVijay Lobo                             {
16339c11a172SVijay Lobo                                 resolution = std::get_if<std::string>(
16349c11a172SVijay Lobo                                     &propertyMap.second);
16359c11a172SVijay Lobo                             }
16367e860f15SJohn Edward Broadbent                             else if (propertyMap.first == "Message")
16377e860f15SJohn Edward Broadbent                             {
16387e860f15SJohn Edward Broadbent                                 message = std::get_if<std::string>(
16397e860f15SJohn Edward Broadbent                                     &propertyMap.second);
16407e860f15SJohn Edward Broadbent                             }
16417e860f15SJohn Edward Broadbent                             else if (propertyMap.first == "Resolved")
16427e860f15SJohn Edward Broadbent                             {
1643914e2d5dSEd Tanous                                 const bool* resolveptr =
1644002d39b4SEd Tanous                                     std::get_if<bool>(&propertyMap.second);
16457e860f15SJohn Edward Broadbent                                 if (resolveptr == nullptr)
16467e860f15SJohn Edward Broadbent                                 {
1647002d39b4SEd Tanous                                     messages::internalError(asyncResp->res);
16487e860f15SJohn Edward Broadbent                                     return;
16497e860f15SJohn Edward Broadbent                                 }
16507e860f15SJohn Edward Broadbent                                 resolved = *resolveptr;
16517e860f15SJohn Edward Broadbent                             }
16529017faf2SAbhishek Patel                             else if (propertyMap.first ==
16539017faf2SAbhishek Patel                                      "ServiceProviderNotify")
16549017faf2SAbhishek Patel                             {
16559017faf2SAbhishek Patel                                 notify = std::get_if<std::string>(
16569017faf2SAbhishek Patel                                     &propertyMap.second);
16579017faf2SAbhishek Patel                                 if (notify == nullptr)
16589017faf2SAbhishek Patel                                 {
16599017faf2SAbhishek Patel                                     messages::internalError(asyncResp->res);
16609017faf2SAbhishek Patel                                     return;
16619017faf2SAbhishek Patel                                 }
16629017faf2SAbhishek Patel                             }
16637e860f15SJohn Edward Broadbent                         }
16647e860f15SJohn Edward Broadbent                         if (id == nullptr || message == nullptr ||
16657e860f15SJohn Edward Broadbent                             severity == nullptr)
16667e860f15SJohn Edward Broadbent                         {
16677e860f15SJohn Edward Broadbent                             messages::internalError(asyncResp->res);
16687e860f15SJohn Edward Broadbent                             return;
16697e860f15SJohn Edward Broadbent                         }
16707e860f15SJohn Edward Broadbent                     }
16717e860f15SJohn Edward Broadbent                     else if (interfaceMap.first ==
16727e860f15SJohn Edward Broadbent                              "xyz.openbmc_project.Common.FilePath")
16737e860f15SJohn Edward Broadbent                     {
1674002d39b4SEd Tanous                         for (const auto& propertyMap : interfaceMap.second)
16757e860f15SJohn Edward Broadbent                         {
16767e860f15SJohn Edward Broadbent                             if (propertyMap.first == "Path")
16777e860f15SJohn Edward Broadbent                             {
16787e860f15SJohn Edward Broadbent                                 filePath = std::get_if<std::string>(
16797e860f15SJohn Edward Broadbent                                     &propertyMap.second);
16807e860f15SJohn Edward Broadbent                             }
16817e860f15SJohn Edward Broadbent                         }
16827e860f15SJohn Edward Broadbent                     }
16837e860f15SJohn Edward Broadbent                 }
16847e860f15SJohn Edward Broadbent                 // Object path without the
16857e860f15SJohn Edward Broadbent                 // xyz.openbmc_project.Logging.Entry interface, ignore
16867e860f15SJohn Edward Broadbent                 // and continue.
16877e860f15SJohn Edward Broadbent                 if (id == nullptr || message == nullptr ||
1688c419c759SEd Tanous                     severity == nullptr || timestamp == nullptr ||
1689c419c759SEd Tanous                     updateTimestamp == nullptr)
16907e860f15SJohn Edward Broadbent                 {
16917e860f15SJohn Edward Broadbent                     continue;
16927e860f15SJohn Edward Broadbent                 }
16937e860f15SJohn Edward Broadbent                 entriesArray.push_back({});
16947e860f15SJohn Edward Broadbent                 nlohmann::json& thisEntry = entriesArray.back();
16959c11a172SVijay Lobo                 thisEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
1696ef4c65b7SEd Tanous                 thisEntry["@odata.id"] = boost::urls::format(
1697ef4c65b7SEd Tanous                     "/redfish/v1/Systems/system/LogServices/EventLog/Entries/{}",
1698ef4c65b7SEd Tanous                     std::to_string(*id));
16997e860f15SJohn Edward Broadbent                 thisEntry["Name"] = "System Event Log Entry";
17007e860f15SJohn Edward Broadbent                 thisEntry["Id"] = std::to_string(*id);
17017e860f15SJohn Edward Broadbent                 thisEntry["Message"] = *message;
17027e860f15SJohn Edward Broadbent                 thisEntry["Resolved"] = resolved;
17039c11a172SVijay Lobo                 if ((resolution != nullptr) && (!(*resolution).empty()))
17049c11a172SVijay Lobo                 {
17059c11a172SVijay Lobo                     thisEntry["Resolution"] = *resolution;
17069c11a172SVijay Lobo                 }
17079017faf2SAbhishek Patel                 std::optional<bool> notifyAction =
17089017faf2SAbhishek Patel                     getProviderNotifyAction(*notify);
17099017faf2SAbhishek Patel                 if (notifyAction)
17109017faf2SAbhishek Patel                 {
17119017faf2SAbhishek Patel                     thisEntry["ServiceProviderNotified"] = *notifyAction;
17129017faf2SAbhishek Patel                 }
17137e860f15SJohn Edward Broadbent                 thisEntry["EntryType"] = "Event";
17147e860f15SJohn Edward Broadbent                 thisEntry["Severity"] =
17157e860f15SJohn Edward Broadbent                     translateSeverityDbusToRedfish(*severity);
17167e860f15SJohn Edward Broadbent                 thisEntry["Created"] =
17172b82937eSEd Tanous                     redfish::time_utils::getDateTimeUintMs(*timestamp);
17187e860f15SJohn Edward Broadbent                 thisEntry["Modified"] =
17192b82937eSEd Tanous                     redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
17207e860f15SJohn Edward Broadbent                 if (filePath != nullptr)
17217e860f15SJohn Edward Broadbent                 {
17227e860f15SJohn Edward Broadbent                     thisEntry["AdditionalDataURI"] =
17230fda0f12SGeorge Liu                         "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
17247e860f15SJohn Edward Broadbent                         std::to_string(*id) + "/attachment";
17257e860f15SJohn Edward Broadbent                 }
17267e860f15SJohn Edward Broadbent             }
1727002d39b4SEd Tanous             std::sort(
1728002d39b4SEd Tanous                 entriesArray.begin(), entriesArray.end(),
1729002d39b4SEd Tanous                 [](const nlohmann::json& left, const nlohmann::json& right) {
17307e860f15SJohn Edward Broadbent                 return (left["Id"] <= right["Id"]);
17317e860f15SJohn Edward Broadbent                 });
17327e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Members@odata.count"] =
17337e860f15SJohn Edward Broadbent                 entriesArray.size();
17345eb468daSGeorge Liu             });
17357e860f15SJohn Edward Broadbent         });
17367e860f15SJohn Edward Broadbent }
17377e860f15SJohn Edward Broadbent 
17387e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntry(App& app)
17397e860f15SJohn Edward Broadbent {
17407e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
174122d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
1742ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
1743002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1744002d39b4SEd Tanous             [&app](const crow::Request& req,
17457e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
174622d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
17473ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
17487e860f15SJohn Edward Broadbent         {
174945ca1b86SEd Tanous             return;
175045ca1b86SEd Tanous         }
17517f3e84a1SEd Tanous         if constexpr (bmcwebEnableMultiHost)
17527f3e84a1SEd Tanous         {
17537f3e84a1SEd Tanous             // Option currently returns no systems.  TBD
17547f3e84a1SEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
17557f3e84a1SEd Tanous                                        systemName);
17567f3e84a1SEd Tanous             return;
17577f3e84a1SEd Tanous         }
175822d268cbSEd Tanous         if (systemName != "system")
175922d268cbSEd Tanous         {
176022d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
176122d268cbSEd Tanous                                        systemName);
176222d268cbSEd Tanous             return;
176322d268cbSEd Tanous         }
176422d268cbSEd Tanous 
17657e860f15SJohn Edward Broadbent         std::string entryID = param;
17667e860f15SJohn Edward Broadbent         dbus::utility::escapePathForDbus(entryID);
17677e860f15SJohn Edward Broadbent 
17687e860f15SJohn Edward Broadbent         // DBus implementation of EventLog/Entries
17697e860f15SJohn Edward Broadbent         // Make call to Logging Service to find all log entry objects
1770d1bde9e5SKrzysztof Grobelny         sdbusplus::asio::getAllProperties(
1771d1bde9e5SKrzysztof Grobelny             *crow::connections::systemBus, "xyz.openbmc_project.Logging",
1772d1bde9e5SKrzysztof Grobelny             "/xyz/openbmc_project/logging/entry/" + entryID, "",
17735e7e2dc5SEd Tanous             [asyncResp, entryID](const boost::system::error_code& ec,
1774b9d36b47SEd Tanous                                  const dbus::utility::DBusPropertiesMap& resp) {
17757e860f15SJohn Edward Broadbent             if (ec.value() == EBADR)
17767e860f15SJohn Edward Broadbent             {
1777d1bde9e5SKrzysztof Grobelny                 messages::resourceNotFound(asyncResp->res, "EventLogEntry",
1778d1bde9e5SKrzysztof Grobelny                                            entryID);
17797e860f15SJohn Edward Broadbent                 return;
17807e860f15SJohn Edward Broadbent             }
17817e860f15SJohn Edward Broadbent             if (ec)
17827e860f15SJohn Edward Broadbent             {
1783*62598e31SEd Tanous                 BMCWEB_LOG_ERROR(
1784*62598e31SEd Tanous                     "EventLogEntry (DBus) resp_handler got error {}", ec);
17857e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
17867e860f15SJohn Edward Broadbent                 return;
17877e860f15SJohn Edward Broadbent             }
1788914e2d5dSEd Tanous             const uint32_t* id = nullptr;
1789c419c759SEd Tanous             const uint64_t* timestamp = nullptr;
1790c419c759SEd Tanous             const uint64_t* updateTimestamp = nullptr;
1791914e2d5dSEd Tanous             const std::string* severity = nullptr;
1792914e2d5dSEd Tanous             const std::string* message = nullptr;
1793914e2d5dSEd Tanous             const std::string* filePath = nullptr;
17949c11a172SVijay Lobo             const std::string* resolution = nullptr;
17957e860f15SJohn Edward Broadbent             bool resolved = false;
17969017faf2SAbhishek Patel             const std::string* notify = nullptr;
17977e860f15SJohn Edward Broadbent 
1798d1bde9e5SKrzysztof Grobelny             const bool success = sdbusplus::unpackPropertiesNoThrow(
1799d1bde9e5SKrzysztof Grobelny                 dbus_utils::UnpackErrorPrinter(), resp, "Id", id, "Timestamp",
1800d1bde9e5SKrzysztof Grobelny                 timestamp, "UpdateTimestamp", updateTimestamp, "Severity",
18019c11a172SVijay Lobo                 severity, "Message", message, "Resolved", resolved,
18029017faf2SAbhishek Patel                 "Resolution", resolution, "Path", filePath,
18039017faf2SAbhishek Patel                 "ServiceProviderNotify", notify);
1804d1bde9e5SKrzysztof Grobelny 
1805d1bde9e5SKrzysztof Grobelny             if (!success)
180675710de2SXiaochao Ma             {
180775710de2SXiaochao Ma                 messages::internalError(asyncResp->res);
180875710de2SXiaochao Ma                 return;
180975710de2SXiaochao Ma             }
1810d1bde9e5SKrzysztof Grobelny 
1811002d39b4SEd Tanous             if (id == nullptr || message == nullptr || severity == nullptr ||
18129017faf2SAbhishek Patel                 timestamp == nullptr || updateTimestamp == nullptr ||
18139017faf2SAbhishek Patel                 notify == nullptr)
1814f86bb901SAdriana Kobylak             {
1815ae34c8e8SAdriana Kobylak                 messages::internalError(asyncResp->res);
1816271584abSEd Tanous                 return;
1817271584abSEd Tanous             }
18189017faf2SAbhishek Patel 
1819f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["@odata.type"] =
18209c11a172SVijay Lobo                 "#LogEntry.v1_9_0.LogEntry";
1821ef4c65b7SEd Tanous             asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
1822ef4c65b7SEd Tanous                 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/{}",
1823ef4c65b7SEd Tanous                 std::to_string(*id));
182445ca1b86SEd Tanous             asyncResp->res.jsonValue["Name"] = "System Event Log Entry";
1825f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1826f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Message"] = *message;
1827f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Resolved"] = resolved;
18289017faf2SAbhishek Patel             std::optional<bool> notifyAction = getProviderNotifyAction(*notify);
18299017faf2SAbhishek Patel             if (notifyAction)
18309017faf2SAbhishek Patel             {
18319017faf2SAbhishek Patel                 asyncResp->res.jsonValue["ServiceProviderNotified"] =
18329017faf2SAbhishek Patel                     *notifyAction;
18339017faf2SAbhishek Patel             }
18349c11a172SVijay Lobo             if ((resolution != nullptr) && (!(*resolution).empty()))
18359c11a172SVijay Lobo             {
18369c11a172SVijay Lobo                 asyncResp->res.jsonValue["Resolution"] = *resolution;
18379c11a172SVijay Lobo             }
1838f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["EntryType"] = "Event";
1839f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Severity"] =
1840f86bb901SAdriana Kobylak                 translateSeverityDbusToRedfish(*severity);
1841f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Created"] =
18422b82937eSEd Tanous                 redfish::time_utils::getDateTimeUintMs(*timestamp);
1843f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Modified"] =
18442b82937eSEd Tanous                 redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
1845f86bb901SAdriana Kobylak             if (filePath != nullptr)
1846f86bb901SAdriana Kobylak             {
1847f86bb901SAdriana Kobylak                 asyncResp->res.jsonValue["AdditionalDataURI"] =
1848e7dbd530SPotin Lai                     "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1849e7dbd530SPotin Lai                     std::to_string(*id) + "/attachment";
1850f86bb901SAdriana Kobylak             }
1851d1bde9e5SKrzysztof Grobelny             });
18527e860f15SJohn Edward Broadbent         });
1853336e96c6SChicago Duan 
18547e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
185522d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
1856ed398213SEd Tanous         .privileges(redfish::privileges::patchLogEntry)
18577e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::patch)(
185845ca1b86SEd Tanous             [&app](const crow::Request& req,
18597e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
186022d268cbSEd Tanous                    const std::string& systemName, const std::string& entryId) {
18613ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
186245ca1b86SEd Tanous         {
186345ca1b86SEd Tanous             return;
186445ca1b86SEd Tanous         }
18657f3e84a1SEd Tanous         if constexpr (bmcwebEnableMultiHost)
18667f3e84a1SEd Tanous         {
18677f3e84a1SEd Tanous             // Option currently returns no systems.  TBD
18687f3e84a1SEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
18697f3e84a1SEd Tanous                                        systemName);
18707f3e84a1SEd Tanous             return;
18717f3e84a1SEd Tanous         }
187222d268cbSEd Tanous         if (systemName != "system")
187322d268cbSEd Tanous         {
187422d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
187522d268cbSEd Tanous                                        systemName);
187622d268cbSEd Tanous             return;
187722d268cbSEd Tanous         }
187875710de2SXiaochao Ma         std::optional<bool> resolved;
187975710de2SXiaochao Ma 
188015ed6780SWilly Tu         if (!json_util::readJsonPatch(req, asyncResp->res, "Resolved",
18817e860f15SJohn Edward Broadbent                                       resolved))
188275710de2SXiaochao Ma         {
188375710de2SXiaochao Ma             return;
188475710de2SXiaochao Ma         }
1885*62598e31SEd Tanous         BMCWEB_LOG_DEBUG("Set Resolved");
188675710de2SXiaochao Ma 
18879ae226faSGeorge Liu         sdbusplus::asio::setProperty(
18889ae226faSGeorge Liu             *crow::connections::systemBus, "xyz.openbmc_project.Logging",
18899ae226faSGeorge Liu             "/xyz/openbmc_project/logging/entry/" + entryId,
18909ae226faSGeorge Liu             "xyz.openbmc_project.Logging.Entry", "Resolved", *resolved,
18915e7e2dc5SEd Tanous             [asyncResp, entryId](const boost::system::error_code& ec) {
189275710de2SXiaochao Ma             if (ec)
189375710de2SXiaochao Ma             {
1894*62598e31SEd Tanous                 BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
189575710de2SXiaochao Ma                 messages::internalError(asyncResp->res);
189675710de2SXiaochao Ma                 return;
189775710de2SXiaochao Ma             }
18989ae226faSGeorge Liu             });
18997e860f15SJohn Edward Broadbent         });
190075710de2SXiaochao Ma 
19017e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
190222d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
1903ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
1904ed398213SEd Tanous 
1905002d39b4SEd Tanous         .methods(boost::beast::http::verb::delete_)(
1906002d39b4SEd Tanous             [&app](const crow::Request& req,
1907002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
190822d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
19093ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1910336e96c6SChicago Duan         {
191145ca1b86SEd Tanous             return;
191245ca1b86SEd Tanous         }
19137f3e84a1SEd Tanous         if constexpr (bmcwebEnableMultiHost)
19147f3e84a1SEd Tanous         {
19157f3e84a1SEd Tanous             // Option currently returns no systems.  TBD
19167f3e84a1SEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
19177f3e84a1SEd Tanous                                        systemName);
19187f3e84a1SEd Tanous             return;
19197f3e84a1SEd Tanous         }
192022d268cbSEd Tanous         if (systemName != "system")
192122d268cbSEd Tanous         {
192222d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
192322d268cbSEd Tanous                                        systemName);
192422d268cbSEd Tanous             return;
192522d268cbSEd Tanous         }
1926*62598e31SEd Tanous         BMCWEB_LOG_DEBUG("Do delete single event entries.");
1927336e96c6SChicago Duan 
19287e860f15SJohn Edward Broadbent         std::string entryID = param;
1929336e96c6SChicago Duan 
1930336e96c6SChicago Duan         dbus::utility::escapePathForDbus(entryID);
1931336e96c6SChicago Duan 
1932336e96c6SChicago Duan         // Process response from Logging service.
1933002d39b4SEd Tanous         auto respHandler =
19345e7e2dc5SEd Tanous             [asyncResp, entryID](const boost::system::error_code& ec) {
1935*62598e31SEd Tanous             BMCWEB_LOG_DEBUG("EventLogEntry (DBus) doDelete callback: Done");
1936336e96c6SChicago Duan             if (ec)
1937336e96c6SChicago Duan             {
19383de8d8baSGeorge Liu                 if (ec.value() == EBADR)
19393de8d8baSGeorge Liu                 {
194045ca1b86SEd Tanous                     messages::resourceNotFound(asyncResp->res, "LogEntry",
194145ca1b86SEd Tanous                                                entryID);
19423de8d8baSGeorge Liu                     return;
19433de8d8baSGeorge Liu                 }
1944336e96c6SChicago Duan                 // TODO Handle for specific error code
1945*62598e31SEd Tanous                 BMCWEB_LOG_ERROR(
1946*62598e31SEd Tanous                     "EventLogEntry (DBus) doDelete respHandler got error {}",
1947*62598e31SEd Tanous                     ec);
1948336e96c6SChicago Duan                 asyncResp->res.result(
1949336e96c6SChicago Duan                     boost::beast::http::status::internal_server_error);
1950336e96c6SChicago Duan                 return;
1951336e96c6SChicago Duan             }
1952336e96c6SChicago Duan 
1953336e96c6SChicago Duan             asyncResp->res.result(boost::beast::http::status::ok);
1954336e96c6SChicago Duan         };
1955336e96c6SChicago Duan 
1956336e96c6SChicago Duan         // Make call to Logging service to request Delete Log
1957336e96c6SChicago Duan         crow::connections::systemBus->async_method_call(
1958336e96c6SChicago Duan             respHandler, "xyz.openbmc_project.Logging",
1959336e96c6SChicago Duan             "/xyz/openbmc_project/logging/entry/" + entryID,
1960336e96c6SChicago Duan             "xyz.openbmc_project.Object.Delete", "Delete");
19617e860f15SJohn Edward Broadbent         });
1962400fd1fbSAdriana Kobylak }
1963400fd1fbSAdriana Kobylak 
19647e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntryDownload(App& app)
1965400fd1fbSAdriana Kobylak {
19660fda0f12SGeorge Liu     BMCWEB_ROUTE(
19670fda0f12SGeorge Liu         app,
196822d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/attachment")
1969ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
19707e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
197145ca1b86SEd Tanous             [&app](const crow::Request& req,
19727e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
197322d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
19743ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
19757e860f15SJohn Edward Broadbent         {
197645ca1b86SEd Tanous             return;
197745ca1b86SEd Tanous         }
197872e21377SMatt Spinler         if (!http_helpers::isContentTypeAllowed(
197999351cd8SEd Tanous                 req.getHeaderValue("Accept"),
19804a0e1a0cSEd Tanous                 http_helpers::ContentType::OctetStream, true))
1981400fd1fbSAdriana Kobylak         {
1982002d39b4SEd Tanous             asyncResp->res.result(boost::beast::http::status::bad_request);
1983400fd1fbSAdriana Kobylak             return;
1984400fd1fbSAdriana Kobylak         }
19857f3e84a1SEd Tanous         if constexpr (bmcwebEnableMultiHost)
19867f3e84a1SEd Tanous         {
19877f3e84a1SEd Tanous             // Option currently returns no systems.  TBD
19887f3e84a1SEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
19897f3e84a1SEd Tanous                                        systemName);
19907f3e84a1SEd Tanous             return;
19917f3e84a1SEd Tanous         }
199222d268cbSEd Tanous         if (systemName != "system")
199322d268cbSEd Tanous         {
199422d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
199522d268cbSEd Tanous                                        systemName);
199622d268cbSEd Tanous             return;
199722d268cbSEd Tanous         }
1998400fd1fbSAdriana Kobylak 
19997e860f15SJohn Edward Broadbent         std::string entryID = param;
2000400fd1fbSAdriana Kobylak         dbus::utility::escapePathForDbus(entryID);
2001400fd1fbSAdriana Kobylak 
2002400fd1fbSAdriana Kobylak         crow::connections::systemBus->async_method_call(
20035e7e2dc5SEd Tanous             [asyncResp, entryID](const boost::system::error_code& ec,
2004400fd1fbSAdriana Kobylak                                  const sdbusplus::message::unix_fd& unixfd) {
2005400fd1fbSAdriana Kobylak             if (ec.value() == EBADR)
2006400fd1fbSAdriana Kobylak             {
2007002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "EventLogAttachment",
2008002d39b4SEd Tanous                                            entryID);
2009400fd1fbSAdriana Kobylak                 return;
2010400fd1fbSAdriana Kobylak             }
2011400fd1fbSAdriana Kobylak             if (ec)
2012400fd1fbSAdriana Kobylak             {
2013*62598e31SEd Tanous                 BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
2014400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
2015400fd1fbSAdriana Kobylak                 return;
2016400fd1fbSAdriana Kobylak             }
2017400fd1fbSAdriana Kobylak 
2018400fd1fbSAdriana Kobylak             int fd = -1;
2019400fd1fbSAdriana Kobylak             fd = dup(unixfd);
2020400fd1fbSAdriana Kobylak             if (fd == -1)
2021400fd1fbSAdriana Kobylak             {
2022400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
2023400fd1fbSAdriana Kobylak                 return;
2024400fd1fbSAdriana Kobylak             }
2025400fd1fbSAdriana Kobylak 
2026400fd1fbSAdriana Kobylak             long long int size = lseek(fd, 0, SEEK_END);
2027400fd1fbSAdriana Kobylak             if (size == -1)
2028400fd1fbSAdriana Kobylak             {
2029400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
2030400fd1fbSAdriana Kobylak                 return;
2031400fd1fbSAdriana Kobylak             }
2032400fd1fbSAdriana Kobylak 
2033400fd1fbSAdriana Kobylak             // Arbitrary max size of 64kb
2034400fd1fbSAdriana Kobylak             constexpr int maxFileSize = 65536;
2035400fd1fbSAdriana Kobylak             if (size > maxFileSize)
2036400fd1fbSAdriana Kobylak             {
2037*62598e31SEd Tanous                 BMCWEB_LOG_ERROR("File size exceeds maximum allowed size of {}",
2038*62598e31SEd Tanous                                  maxFileSize);
2039400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
2040400fd1fbSAdriana Kobylak                 return;
2041400fd1fbSAdriana Kobylak             }
2042400fd1fbSAdriana Kobylak             std::vector<char> data(static_cast<size_t>(size));
2043400fd1fbSAdriana Kobylak             long long int rc = lseek(fd, 0, SEEK_SET);
2044400fd1fbSAdriana Kobylak             if (rc == -1)
2045400fd1fbSAdriana Kobylak             {
2046400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
2047400fd1fbSAdriana Kobylak                 return;
2048400fd1fbSAdriana Kobylak             }
2049400fd1fbSAdriana Kobylak             rc = read(fd, data.data(), data.size());
2050400fd1fbSAdriana Kobylak             if ((rc == -1) || (rc != size))
2051400fd1fbSAdriana Kobylak             {
2052400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
2053400fd1fbSAdriana Kobylak                 return;
2054400fd1fbSAdriana Kobylak             }
2055400fd1fbSAdriana Kobylak             close(fd);
2056400fd1fbSAdriana Kobylak 
2057400fd1fbSAdriana Kobylak             std::string_view strData(data.data(), data.size());
2058002d39b4SEd Tanous             std::string output = crow::utility::base64encode(strData);
2059400fd1fbSAdriana Kobylak 
2060d9f6c621SEd Tanous             asyncResp->res.addHeader(boost::beast::http::field::content_type,
2061400fd1fbSAdriana Kobylak                                      "application/octet-stream");
2062d9f6c621SEd Tanous             asyncResp->res.addHeader(
2063d9f6c621SEd Tanous                 boost::beast::http::field::content_transfer_encoding, "Base64");
2064400fd1fbSAdriana Kobylak             asyncResp->res.body() = std::move(output);
2065400fd1fbSAdriana Kobylak             },
2066400fd1fbSAdriana Kobylak             "xyz.openbmc_project.Logging",
2067400fd1fbSAdriana Kobylak             "/xyz/openbmc_project/logging/entry/" + entryID,
2068400fd1fbSAdriana Kobylak             "xyz.openbmc_project.Logging.Entry", "GetEntry");
20697e860f15SJohn Edward Broadbent         });
20701da66f75SEd Tanous }
20711da66f75SEd Tanous 
2072b7028ebfSSpencer Ku constexpr const char* hostLoggerFolderPath = "/var/log/console";
2073b7028ebfSSpencer Ku 
2074b7028ebfSSpencer Ku inline bool
2075b7028ebfSSpencer Ku     getHostLoggerFiles(const std::string& hostLoggerFilePath,
2076b7028ebfSSpencer Ku                        std::vector<std::filesystem::path>& hostLoggerFiles)
2077b7028ebfSSpencer Ku {
2078b7028ebfSSpencer Ku     std::error_code ec;
2079b7028ebfSSpencer Ku     std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec);
2080b7028ebfSSpencer Ku     if (ec)
2081b7028ebfSSpencer Ku     {
2082*62598e31SEd Tanous         BMCWEB_LOG_ERROR("{}", ec.message());
2083b7028ebfSSpencer Ku         return false;
2084b7028ebfSSpencer Ku     }
2085b7028ebfSSpencer Ku     for (const std::filesystem::directory_entry& it : logPath)
2086b7028ebfSSpencer Ku     {
2087b7028ebfSSpencer Ku         std::string filename = it.path().filename();
2088b7028ebfSSpencer Ku         // Prefix of each log files is "log". Find the file and save the
2089b7028ebfSSpencer Ku         // path
209011ba3979SEd Tanous         if (filename.starts_with("log"))
2091b7028ebfSSpencer Ku         {
2092b7028ebfSSpencer Ku             hostLoggerFiles.emplace_back(it.path());
2093b7028ebfSSpencer Ku         }
2094b7028ebfSSpencer Ku     }
2095b7028ebfSSpencer Ku     // As the log files rotate, they are appended with a ".#" that is higher for
2096b7028ebfSSpencer Ku     // the older logs. Since we start from oldest logs, sort the name in
2097b7028ebfSSpencer Ku     // descending order.
2098b7028ebfSSpencer Ku     std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(),
2099b7028ebfSSpencer Ku               AlphanumLess<std::string>());
2100b7028ebfSSpencer Ku 
2101b7028ebfSSpencer Ku     return true;
2102b7028ebfSSpencer Ku }
2103b7028ebfSSpencer Ku 
210402cad96eSEd Tanous inline bool getHostLoggerEntries(
210502cad96eSEd Tanous     const std::vector<std::filesystem::path>& hostLoggerFiles, uint64_t skip,
210602cad96eSEd Tanous     uint64_t top, std::vector<std::string>& logEntries, size_t& logCount)
2107b7028ebfSSpencer Ku {
2108b7028ebfSSpencer Ku     GzFileReader logFile;
2109b7028ebfSSpencer Ku 
2110b7028ebfSSpencer Ku     // Go though all log files and expose host logs.
2111b7028ebfSSpencer Ku     for (const std::filesystem::path& it : hostLoggerFiles)
2112b7028ebfSSpencer Ku     {
2113b7028ebfSSpencer Ku         if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount))
2114b7028ebfSSpencer Ku         {
2115*62598e31SEd Tanous             BMCWEB_LOG_ERROR("fail to expose host logs");
2116b7028ebfSSpencer Ku             return false;
2117b7028ebfSSpencer Ku         }
2118b7028ebfSSpencer Ku     }
2119b7028ebfSSpencer Ku     // Get lastMessage from constructor by getter
2120b7028ebfSSpencer Ku     std::string lastMessage = logFile.getLastMessage();
2121b7028ebfSSpencer Ku     if (!lastMessage.empty())
2122b7028ebfSSpencer Ku     {
2123b7028ebfSSpencer Ku         logCount++;
2124b7028ebfSSpencer Ku         if (logCount > skip && logCount <= (skip + top))
2125b7028ebfSSpencer Ku         {
2126b7028ebfSSpencer Ku             logEntries.push_back(lastMessage);
2127b7028ebfSSpencer Ku         }
2128b7028ebfSSpencer Ku     }
2129b7028ebfSSpencer Ku     return true;
2130b7028ebfSSpencer Ku }
2131b7028ebfSSpencer Ku 
2132b7028ebfSSpencer Ku inline void fillHostLoggerEntryJson(const std::string& logEntryID,
2133b7028ebfSSpencer Ku                                     const std::string& msg,
21346d6574c9SJason M. Bills                                     nlohmann::json::object_t& logEntryJson)
2135b7028ebfSSpencer Ku {
2136b7028ebfSSpencer Ku     // Fill in the log entry with the gathered data.
21379c11a172SVijay Lobo     logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
2138ef4c65b7SEd Tanous     logEntryJson["@odata.id"] = boost::urls::format(
2139ef4c65b7SEd Tanous         "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/{}",
2140ef4c65b7SEd Tanous         logEntryID);
21416d6574c9SJason M. Bills     logEntryJson["Name"] = "Host Logger Entry";
21426d6574c9SJason M. Bills     logEntryJson["Id"] = logEntryID;
21436d6574c9SJason M. Bills     logEntryJson["Message"] = msg;
21446d6574c9SJason M. Bills     logEntryJson["EntryType"] = "Oem";
21456d6574c9SJason M. Bills     logEntryJson["Severity"] = "OK";
21466d6574c9SJason M. Bills     logEntryJson["OemRecordFormat"] = "Host Logger Entry";
2147b7028ebfSSpencer Ku }
2148b7028ebfSSpencer Ku 
2149b7028ebfSSpencer Ku inline void requestRoutesSystemHostLogger(App& app)
2150b7028ebfSSpencer Ku {
215122d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/")
2152b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogService)
21531476687dSEd Tanous         .methods(boost::beast::http::verb::get)(
21541476687dSEd Tanous             [&app](const crow::Request& req,
215522d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
215622d268cbSEd Tanous                    const std::string& systemName) {
21573ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
215845ca1b86SEd Tanous         {
215945ca1b86SEd Tanous             return;
216045ca1b86SEd Tanous         }
21617f3e84a1SEd Tanous         if constexpr (bmcwebEnableMultiHost)
21627f3e84a1SEd Tanous         {
21637f3e84a1SEd Tanous             // Option currently returns no systems.  TBD
21647f3e84a1SEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
21657f3e84a1SEd Tanous                                        systemName);
21667f3e84a1SEd Tanous             return;
21677f3e84a1SEd Tanous         }
216822d268cbSEd Tanous         if (systemName != "system")
216922d268cbSEd Tanous         {
217022d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
217122d268cbSEd Tanous                                        systemName);
217222d268cbSEd Tanous             return;
217322d268cbSEd Tanous         }
2174b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.id"] =
2175b7028ebfSSpencer Ku             "/redfish/v1/Systems/system/LogServices/HostLogger";
2176b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.type"] =
2177b7028ebfSSpencer Ku             "#LogService.v1_1_0.LogService";
2178b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Name"] = "Host Logger Service";
2179b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Description"] = "Host Logger Service";
2180b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Id"] = "HostLogger";
21811476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
21821476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
2183b7028ebfSSpencer Ku         });
2184b7028ebfSSpencer Ku }
2185b7028ebfSSpencer Ku 
2186b7028ebfSSpencer Ku inline void requestRoutesSystemHostLoggerCollection(App& app)
2187b7028ebfSSpencer Ku {
2188b7028ebfSSpencer Ku     BMCWEB_ROUTE(app,
218922d268cbSEd Tanous                  "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/")
2190b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogEntry)
2191002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2192002d39b4SEd Tanous             [&app](const crow::Request& req,
219322d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
219422d268cbSEd Tanous                    const std::string& systemName) {
2195c937d2bfSEd Tanous         query_param::QueryCapabilities capabilities = {
2196c937d2bfSEd Tanous             .canDelegateTop = true,
2197c937d2bfSEd Tanous             .canDelegateSkip = true,
2198c937d2bfSEd Tanous         };
2199c937d2bfSEd Tanous         query_param::Query delegatedQuery;
2200c937d2bfSEd Tanous         if (!redfish::setUpRedfishRouteWithDelegation(
22013ba00073SCarson Labrado                 app, req, asyncResp, delegatedQuery, capabilities))
2202b7028ebfSSpencer Ku         {
2203b7028ebfSSpencer Ku             return;
2204b7028ebfSSpencer Ku         }
22057f3e84a1SEd Tanous         if constexpr (bmcwebEnableMultiHost)
22067f3e84a1SEd Tanous         {
22077f3e84a1SEd Tanous             // Option currently returns no systems.  TBD
22087f3e84a1SEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
22097f3e84a1SEd Tanous                                        systemName);
22107f3e84a1SEd Tanous             return;
22117f3e84a1SEd Tanous         }
221222d268cbSEd Tanous         if (systemName != "system")
221322d268cbSEd Tanous         {
221422d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
221522d268cbSEd Tanous                                        systemName);
221622d268cbSEd Tanous             return;
221722d268cbSEd Tanous         }
2218b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.id"] =
2219b7028ebfSSpencer Ku             "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
2220b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.type"] =
2221b7028ebfSSpencer Ku             "#LogEntryCollection.LogEntryCollection";
2222b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Name"] = "HostLogger Entries";
2223b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Description"] =
2224b7028ebfSSpencer Ku             "Collection of HostLogger Entries";
22250fda0f12SGeorge Liu         nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2226b7028ebfSSpencer Ku         logEntryArray = nlohmann::json::array();
2227b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Members@odata.count"] = 0;
2228b7028ebfSSpencer Ku 
2229b7028ebfSSpencer Ku         std::vector<std::filesystem::path> hostLoggerFiles;
2230b7028ebfSSpencer Ku         if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2231b7028ebfSSpencer Ku         {
2232*62598e31SEd Tanous             BMCWEB_LOG_ERROR("fail to get host log file path");
2233b7028ebfSSpencer Ku             return;
2234b7028ebfSSpencer Ku         }
22353648c8beSEd Tanous         // If we weren't provided top and skip limits, use the defaults.
22363648c8beSEd Tanous         size_t skip = delegatedQuery.skip.value_or(0);
22375143f7a5SJiaqing Zhao         size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
2238b7028ebfSSpencer Ku         size_t logCount = 0;
2239b7028ebfSSpencer Ku         // This vector only store the entries we want to expose that
2240b7028ebfSSpencer Ku         // control by skip and top.
2241b7028ebfSSpencer Ku         std::vector<std::string> logEntries;
22423648c8beSEd Tanous         if (!getHostLoggerEntries(hostLoggerFiles, skip, top, logEntries,
22433648c8beSEd Tanous                                   logCount))
2244b7028ebfSSpencer Ku         {
2245b7028ebfSSpencer Ku             messages::internalError(asyncResp->res);
2246b7028ebfSSpencer Ku             return;
2247b7028ebfSSpencer Ku         }
2248b7028ebfSSpencer Ku         // If vector is empty, that means skip value larger than total
2249b7028ebfSSpencer Ku         // log count
225026f6976fSEd Tanous         if (logEntries.empty())
2251b7028ebfSSpencer Ku         {
2252b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Members@odata.count"] = logCount;
2253b7028ebfSSpencer Ku             return;
2254b7028ebfSSpencer Ku         }
225526f6976fSEd Tanous         if (!logEntries.empty())
2256b7028ebfSSpencer Ku         {
2257b7028ebfSSpencer Ku             for (size_t i = 0; i < logEntries.size(); i++)
2258b7028ebfSSpencer Ku             {
22596d6574c9SJason M. Bills                 nlohmann::json::object_t hostLogEntry;
22603648c8beSEd Tanous                 fillHostLoggerEntryJson(std::to_string(skip + i), logEntries[i],
22613648c8beSEd Tanous                                         hostLogEntry);
2262b2ba3072SPatrick Williams                 logEntryArray.emplace_back(std::move(hostLogEntry));
2263b7028ebfSSpencer Ku             }
2264b7028ebfSSpencer Ku 
2265b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Members@odata.count"] = logCount;
22663648c8beSEd Tanous             if (skip + top < logCount)
2267b7028ebfSSpencer Ku             {
2268b7028ebfSSpencer Ku                 asyncResp->res.jsonValue["Members@odata.nextLink"] =
22690fda0f12SGeorge Liu                     "/redfish/v1/Systems/system/LogServices/HostLogger/Entries?$skip=" +
22703648c8beSEd Tanous                     std::to_string(skip + top);
2271b7028ebfSSpencer Ku             }
2272b7028ebfSSpencer Ku         }
2273b7028ebfSSpencer Ku         });
2274b7028ebfSSpencer Ku }
2275b7028ebfSSpencer Ku 
2276b7028ebfSSpencer Ku inline void requestRoutesSystemHostLoggerLogEntry(App& app)
2277b7028ebfSSpencer Ku {
2278b7028ebfSSpencer Ku     BMCWEB_ROUTE(
227922d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/<str>/")
2280b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogEntry)
2281b7028ebfSSpencer Ku         .methods(boost::beast::http::verb::get)(
228245ca1b86SEd Tanous             [&app](const crow::Request& req,
2283b7028ebfSSpencer Ku                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
228422d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
22853ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
228645ca1b86SEd Tanous         {
228745ca1b86SEd Tanous             return;
228845ca1b86SEd Tanous         }
22897f3e84a1SEd Tanous         if constexpr (bmcwebEnableMultiHost)
22907f3e84a1SEd Tanous         {
22917f3e84a1SEd Tanous             // Option currently returns no systems.  TBD
22927f3e84a1SEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
22937f3e84a1SEd Tanous                                        systemName);
22947f3e84a1SEd Tanous             return;
22957f3e84a1SEd Tanous         }
229622d268cbSEd Tanous         if (systemName != "system")
229722d268cbSEd Tanous         {
229822d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
229922d268cbSEd Tanous                                        systemName);
230022d268cbSEd Tanous             return;
230122d268cbSEd Tanous         }
2302b7028ebfSSpencer Ku         const std::string& targetID = param;
2303b7028ebfSSpencer Ku 
2304b7028ebfSSpencer Ku         uint64_t idInt = 0;
2305ca45aa3cSEd Tanous 
230684396af9SPatrick Williams         auto [ptr, ec] = std::from_chars(&*targetID.begin(), &*targetID.end(),
230784396af9SPatrick Williams                                          idInt);
23089db4ba25SJiaqing Zhao         if (ec == std::errc::invalid_argument ||
23099db4ba25SJiaqing Zhao             ec == std::errc::result_out_of_range)
2310b7028ebfSSpencer Ku         {
23119db4ba25SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "LogEntry", param);
2312b7028ebfSSpencer Ku             return;
2313b7028ebfSSpencer Ku         }
2314b7028ebfSSpencer Ku 
2315b7028ebfSSpencer Ku         std::vector<std::filesystem::path> hostLoggerFiles;
2316b7028ebfSSpencer Ku         if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2317b7028ebfSSpencer Ku         {
2318*62598e31SEd Tanous             BMCWEB_LOG_ERROR("fail to get host log file path");
2319b7028ebfSSpencer Ku             return;
2320b7028ebfSSpencer Ku         }
2321b7028ebfSSpencer Ku 
2322b7028ebfSSpencer Ku         size_t logCount = 0;
23233648c8beSEd Tanous         size_t top = 1;
2324b7028ebfSSpencer Ku         std::vector<std::string> logEntries;
2325b7028ebfSSpencer Ku         // We can get specific entry by skip and top. For example, if we
2326b7028ebfSSpencer Ku         // want to get nth entry, we can set skip = n-1 and top = 1 to
2327b7028ebfSSpencer Ku         // get that entry
2328002d39b4SEd Tanous         if (!getHostLoggerEntries(hostLoggerFiles, idInt, top, logEntries,
2329002d39b4SEd Tanous                                   logCount))
2330b7028ebfSSpencer Ku         {
2331b7028ebfSSpencer Ku             messages::internalError(asyncResp->res);
2332b7028ebfSSpencer Ku             return;
2333b7028ebfSSpencer Ku         }
2334b7028ebfSSpencer Ku 
2335b7028ebfSSpencer Ku         if (!logEntries.empty())
2336b7028ebfSSpencer Ku         {
23376d6574c9SJason M. Bills             nlohmann::json::object_t hostLogEntry;
23386d6574c9SJason M. Bills             fillHostLoggerEntryJson(targetID, logEntries[0], hostLogEntry);
23396d6574c9SJason M. Bills             asyncResp->res.jsonValue.update(hostLogEntry);
2340b7028ebfSSpencer Ku             return;
2341b7028ebfSSpencer Ku         }
2342b7028ebfSSpencer Ku 
2343b7028ebfSSpencer Ku         // Requested ID was not found
23449db4ba25SJiaqing Zhao         messages::resourceNotFound(asyncResp->res, "LogEntry", param);
2345b7028ebfSSpencer Ku         });
2346b7028ebfSSpencer Ku }
2347b7028ebfSSpencer Ku 
2348dd72e87bSClaire Weinan inline void handleBMCLogServicesCollectionGet(
2349fdd26906SClaire Weinan     crow::App& app, const crow::Request& req,
2350fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
23511da66f75SEd Tanous {
23523ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
235345ca1b86SEd Tanous     {
235445ca1b86SEd Tanous         return;
235545ca1b86SEd Tanous     }
23567e860f15SJohn Edward Broadbent     // Collections don't include the static data added by SubRoute
23577e860f15SJohn Edward Broadbent     // because it has a duplicate entry for members
2358e1f26343SJason M. Bills     asyncResp->res.jsonValue["@odata.type"] =
23591da66f75SEd Tanous         "#LogServiceCollection.LogServiceCollection";
2360e1f26343SJason M. Bills     asyncResp->res.jsonValue["@odata.id"] =
2361e1f26343SJason M. Bills         "/redfish/v1/Managers/bmc/LogServices";
2362002d39b4SEd Tanous     asyncResp->res.jsonValue["Name"] = "Open BMC Log Services Collection";
2363e1f26343SJason M. Bills     asyncResp->res.jsonValue["Description"] =
23641da66f75SEd Tanous         "Collection of LogServices for this Manager";
2365002d39b4SEd Tanous     nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
2366c4bf6374SJason M. Bills     logServiceArray = nlohmann::json::array();
2367fdd26906SClaire Weinan 
2368c4bf6374SJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
2369613dabeaSEd Tanous     nlohmann::json::object_t journal;
2370613dabeaSEd Tanous     journal["@odata.id"] = "/redfish/v1/Managers/bmc/LogServices/Journal";
2371b2ba3072SPatrick Williams     logServiceArray.emplace_back(std::move(journal));
2372c4bf6374SJason M. Bills #endif
2373fdd26906SClaire Weinan 
2374fdd26906SClaire Weinan     asyncResp->res.jsonValue["Members@odata.count"] = logServiceArray.size();
2375fdd26906SClaire Weinan 
2376fdd26906SClaire Weinan #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
237715912159SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
23787a1dbc48SGeorge Liu         "xyz.openbmc_project.Collection.DeleteAll"};
23797a1dbc48SGeorge Liu     dbus::utility::getSubTreePaths(
23807a1dbc48SGeorge Liu         "/xyz/openbmc_project/dump", 0, interfaces,
2381fdd26906SClaire Weinan         [asyncResp](
23827a1dbc48SGeorge Liu             const boost::system::error_code& ec,
2383fdd26906SClaire Weinan             const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
2384fdd26906SClaire Weinan         if (ec)
2385fdd26906SClaire Weinan         {
2386*62598e31SEd Tanous             BMCWEB_LOG_ERROR(
2387*62598e31SEd Tanous                 "handleBMCLogServicesCollectionGet respHandler got error {}",
2388*62598e31SEd Tanous                 ec);
2389fdd26906SClaire Weinan             // Assume that getting an error simply means there are no dump
2390fdd26906SClaire Weinan             // LogServices. Return without adding any error response.
2391fdd26906SClaire Weinan             return;
2392fdd26906SClaire Weinan         }
2393fdd26906SClaire Weinan 
2394fdd26906SClaire Weinan         nlohmann::json& logServiceArrayLocal =
2395fdd26906SClaire Weinan             asyncResp->res.jsonValue["Members"];
2396fdd26906SClaire Weinan 
2397fdd26906SClaire Weinan         for (const std::string& path : subTreePaths)
2398fdd26906SClaire Weinan         {
2399fdd26906SClaire Weinan             if (path == "/xyz/openbmc_project/dump/bmc")
2400fdd26906SClaire Weinan             {
2401613dabeaSEd Tanous                 nlohmann::json::object_t member;
2402613dabeaSEd Tanous                 member["@odata.id"] =
2403613dabeaSEd Tanous                     "/redfish/v1/Managers/bmc/LogServices/Dump";
2404b2ba3072SPatrick Williams                 logServiceArrayLocal.emplace_back(std::move(member));
2405fdd26906SClaire Weinan             }
2406fdd26906SClaire Weinan             else if (path == "/xyz/openbmc_project/dump/faultlog")
2407fdd26906SClaire Weinan             {
2408613dabeaSEd Tanous                 nlohmann::json::object_t member;
2409613dabeaSEd Tanous                 member["@odata.id"] =
2410613dabeaSEd Tanous                     "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2411b2ba3072SPatrick Williams                 logServiceArrayLocal.emplace_back(std::move(member));
2412fdd26906SClaire Weinan             }
2413fdd26906SClaire Weinan         }
2414fdd26906SClaire Weinan 
2415e1f26343SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] =
2416fdd26906SClaire Weinan             logServiceArrayLocal.size();
24177a1dbc48SGeorge Liu         });
2418fdd26906SClaire Weinan #endif
2419fdd26906SClaire Weinan }
2420fdd26906SClaire Weinan 
2421fdd26906SClaire Weinan inline void requestRoutesBMCLogServiceCollection(App& app)
2422fdd26906SClaire Weinan {
2423fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
2424fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogServiceCollection)
2425fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(
2426dd72e87bSClaire Weinan             std::bind_front(handleBMCLogServicesCollectionGet, std::ref(app)));
2427e1f26343SJason M. Bills }
2428e1f26343SJason M. Bills 
24297e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogService(App& app)
2430e1f26343SJason M. Bills {
24317e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
2432ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
24337e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
243445ca1b86SEd Tanous             [&app](const crow::Request& req,
243545ca1b86SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
24363ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
24377e860f15SJohn Edward Broadbent         {
243845ca1b86SEd Tanous             return;
243945ca1b86SEd Tanous         }
2440e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
2441e1f26343SJason M. Bills             "#LogService.v1_1_0.LogService";
24420f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
24430f74e643SEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal";
2444002d39b4SEd Tanous         asyncResp->res.jsonValue["Name"] = "Open BMC Journal Log Service";
2445002d39b4SEd Tanous         asyncResp->res.jsonValue["Description"] = "BMC Journal Log Service";
2446ed34a4adSEd Tanous         asyncResp->res.jsonValue["Id"] = "Journal";
2447e1f26343SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
24487c8c4058STejas Patil 
24497c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
24502b82937eSEd Tanous             redfish::time_utils::getDateTimeOffsetNow();
2451002d39b4SEd Tanous         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
24527c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
24537c8c4058STejas Patil             redfishDateTimeOffset.second;
24547c8c4058STejas Patil 
24551476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
24561476687dSEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
24577e860f15SJohn Edward Broadbent         });
2458e1f26343SJason M. Bills }
2459e1f26343SJason M. Bills 
24603a48b3a2SJason M. Bills static int
24613a48b3a2SJason M. Bills     fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
2462e1f26343SJason M. Bills                                sd_journal* journal,
24633a48b3a2SJason M. Bills                                nlohmann::json::object_t& bmcJournalLogEntryJson)
2464e1f26343SJason M. Bills {
2465e1f26343SJason M. Bills     // Get the Log Entry contents
2466e1f26343SJason M. Bills     int ret = 0;
2467e1f26343SJason M. Bills 
2468a8fe54f0SJason M. Bills     std::string message;
2469a8fe54f0SJason M. Bills     std::string_view syslogID;
2470a8fe54f0SJason M. Bills     ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
2471a8fe54f0SJason M. Bills     if (ret < 0)
2472a8fe54f0SJason M. Bills     {
2473*62598e31SEd Tanous         BMCWEB_LOG_ERROR("Failed to read SYSLOG_IDENTIFIER field: {}",
2474*62598e31SEd Tanous                          strerror(-ret));
2475a8fe54f0SJason M. Bills     }
2476a8fe54f0SJason M. Bills     if (!syslogID.empty())
2477a8fe54f0SJason M. Bills     {
2478a8fe54f0SJason M. Bills         message += std::string(syslogID) + ": ";
2479a8fe54f0SJason M. Bills     }
2480a8fe54f0SJason M. Bills 
248139e77504SEd Tanous     std::string_view msg;
248216428a1aSJason M. Bills     ret = getJournalMetadata(journal, "MESSAGE", msg);
2483e1f26343SJason M. Bills     if (ret < 0)
2484e1f26343SJason M. Bills     {
2485*62598e31SEd Tanous         BMCWEB_LOG_ERROR("Failed to read MESSAGE field: {}", strerror(-ret));
2486e1f26343SJason M. Bills         return 1;
2487e1f26343SJason M. Bills     }
2488a8fe54f0SJason M. Bills     message += std::string(msg);
2489e1f26343SJason M. Bills 
2490e1f26343SJason M. Bills     // Get the severity from the PRIORITY field
2491271584abSEd Tanous     long int severity = 8; // Default to an invalid priority
249216428a1aSJason M. Bills     ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
2493e1f26343SJason M. Bills     if (ret < 0)
2494e1f26343SJason M. Bills     {
2495*62598e31SEd Tanous         BMCWEB_LOG_ERROR("Failed to read PRIORITY field: {}", strerror(-ret));
2496e1f26343SJason M. Bills     }
2497e1f26343SJason M. Bills 
2498e1f26343SJason M. Bills     // Get the Created time from the timestamp
249916428a1aSJason M. Bills     std::string entryTimeStr;
250016428a1aSJason M. Bills     if (!getEntryTimestamp(journal, entryTimeStr))
2501e1f26343SJason M. Bills     {
250216428a1aSJason M. Bills         return 1;
2503e1f26343SJason M. Bills     }
2504e1f26343SJason M. Bills 
2505e1f26343SJason M. Bills     // Fill in the log entry with the gathered data
25069c11a172SVijay Lobo     bmcJournalLogEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
2507ef4c65b7SEd Tanous     bmcJournalLogEntryJson["@odata.id"] = boost::urls::format(
2508ef4c65b7SEd Tanous         "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/{}",
2509eddfc437SWilly Tu         bmcJournalLogEntryID);
251084afc48bSJason M. Bills     bmcJournalLogEntryJson["Name"] = "BMC Journal Entry";
251184afc48bSJason M. Bills     bmcJournalLogEntryJson["Id"] = bmcJournalLogEntryID;
251284afc48bSJason M. Bills     bmcJournalLogEntryJson["Message"] = std::move(message);
251384afc48bSJason M. Bills     bmcJournalLogEntryJson["EntryType"] = "Oem";
251484afc48bSJason M. Bills     bmcJournalLogEntryJson["Severity"] = severity <= 2   ? "Critical"
2515738c1e61SPatrick Williams                                          : severity <= 4 ? "Warning"
251684afc48bSJason M. Bills                                                          : "OK";
251784afc48bSJason M. Bills     bmcJournalLogEntryJson["OemRecordFormat"] = "BMC Journal Entry";
251884afc48bSJason M. Bills     bmcJournalLogEntryJson["Created"] = std::move(entryTimeStr);
2519e1f26343SJason M. Bills     return 0;
2520e1f26343SJason M. Bills }
2521e1f26343SJason M. Bills 
25227e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogEntryCollection(App& app)
2523e1f26343SJason M. Bills {
25247e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
2525ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
2526002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2527002d39b4SEd Tanous             [&app](const crow::Request& req,
2528002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2529c937d2bfSEd Tanous         query_param::QueryCapabilities capabilities = {
2530c937d2bfSEd Tanous             .canDelegateTop = true,
2531c937d2bfSEd Tanous             .canDelegateSkip = true,
2532c937d2bfSEd Tanous         };
2533c937d2bfSEd Tanous         query_param::Query delegatedQuery;
2534c937d2bfSEd Tanous         if (!redfish::setUpRedfishRouteWithDelegation(
25353ba00073SCarson Labrado                 app, req, asyncResp, delegatedQuery, capabilities))
2536193ad2faSJason M. Bills         {
2537193ad2faSJason M. Bills             return;
2538193ad2faSJason M. Bills         }
25393648c8beSEd Tanous 
25403648c8beSEd Tanous         size_t skip = delegatedQuery.skip.value_or(0);
25415143f7a5SJiaqing Zhao         size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
25423648c8beSEd Tanous 
25437e860f15SJohn Edward Broadbent         // Collections don't include the static data added by SubRoute
25447e860f15SJohn Edward Broadbent         // because it has a duplicate entry for members
2545e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
2546e1f26343SJason M. Bills             "#LogEntryCollection.LogEntryCollection";
25470f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
25480f74e643SEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2549e1f26343SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
2550e1f26343SJason M. Bills         asyncResp->res.jsonValue["Description"] =
2551e1f26343SJason M. Bills             "Collection of BMC Journal Entries";
25520fda0f12SGeorge Liu         nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2553e1f26343SJason M. Bills         logEntryArray = nlohmann::json::array();
2554e1f26343SJason M. Bills 
25557e860f15SJohn Edward Broadbent         // Go through the journal and use the timestamp to create a
25567e860f15SJohn Edward Broadbent         // unique ID for each entry
2557e1f26343SJason M. Bills         sd_journal* journalTmp = nullptr;
2558e1f26343SJason M. Bills         int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2559e1f26343SJason M. Bills         if (ret < 0)
2560e1f26343SJason M. Bills         {
2561*62598e31SEd Tanous             BMCWEB_LOG_ERROR("failed to open journal: {}", strerror(-ret));
2562f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2563e1f26343SJason M. Bills             return;
2564e1f26343SJason M. Bills         }
25650fda0f12SGeorge Liu         std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
25660fda0f12SGeorge Liu             journalTmp, sd_journal_close);
2567e1f26343SJason M. Bills         journalTmp = nullptr;
2568b01bf299SEd Tanous         uint64_t entryCount = 0;
2569e85d6b16SJason M. Bills         // Reset the unique ID on the first entry
2570e85d6b16SJason M. Bills         bool firstEntry = true;
2571e1f26343SJason M. Bills         SD_JOURNAL_FOREACH(journal.get())
2572e1f26343SJason M. Bills         {
2573193ad2faSJason M. Bills             entryCount++;
25747e860f15SJohn Edward Broadbent             // Handle paging using skip (number of entries to skip from
25757e860f15SJohn Edward Broadbent             // the start) and top (number of entries to display)
25763648c8beSEd Tanous             if (entryCount <= skip || entryCount > skip + top)
2577193ad2faSJason M. Bills             {
2578193ad2faSJason M. Bills                 continue;
2579193ad2faSJason M. Bills             }
2580193ad2faSJason M. Bills 
258116428a1aSJason M. Bills             std::string idStr;
2582e85d6b16SJason M. Bills             if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2583e1f26343SJason M. Bills             {
2584e1f26343SJason M. Bills                 continue;
2585e1f26343SJason M. Bills             }
2586e85d6b16SJason M. Bills             firstEntry = false;
2587e85d6b16SJason M. Bills 
25883a48b3a2SJason M. Bills             nlohmann::json::object_t bmcJournalLogEntry;
2589c4bf6374SJason M. Bills             if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2590c4bf6374SJason M. Bills                                            bmcJournalLogEntry) != 0)
2591e1f26343SJason M. Bills             {
2592f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
2593e1f26343SJason M. Bills                 return;
2594e1f26343SJason M. Bills             }
2595b2ba3072SPatrick Williams             logEntryArray.emplace_back(std::move(bmcJournalLogEntry));
2596e1f26343SJason M. Bills         }
2597193ad2faSJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
25983648c8beSEd Tanous         if (skip + top < entryCount)
2599193ad2faSJason M. Bills         {
2600193ad2faSJason M. Bills             asyncResp->res.jsonValue["Members@odata.nextLink"] =
26010fda0f12SGeorge Liu                 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
26023648c8beSEd Tanous                 std::to_string(skip + top);
2603193ad2faSJason M. Bills         }
26047e860f15SJohn Edward Broadbent         });
2605e1f26343SJason M. Bills }
2606e1f26343SJason M. Bills 
26077e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogEntry(App& app)
2608e1f26343SJason M. Bills {
26097e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
26107e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
2611ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
26127e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
261345ca1b86SEd Tanous             [&app](const crow::Request& req,
26147e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
26157e860f15SJohn Edward Broadbent                    const std::string& entryID) {
26163ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
261745ca1b86SEd Tanous         {
261845ca1b86SEd Tanous             return;
261945ca1b86SEd Tanous         }
2620e1f26343SJason M. Bills         // Convert the unique ID back to a timestamp to find the entry
2621e1f26343SJason M. Bills         uint64_t ts = 0;
2622271584abSEd Tanous         uint64_t index = 0;
26238d1b46d7Szhanghch05         if (!getTimestampFromID(asyncResp, entryID, ts, index))
2624e1f26343SJason M. Bills         {
262516428a1aSJason M. Bills             return;
2626e1f26343SJason M. Bills         }
2627e1f26343SJason M. Bills 
2628e1f26343SJason M. Bills         sd_journal* journalTmp = nullptr;
2629e1f26343SJason M. Bills         int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2630e1f26343SJason M. Bills         if (ret < 0)
2631e1f26343SJason M. Bills         {
2632*62598e31SEd Tanous             BMCWEB_LOG_ERROR("failed to open journal: {}", strerror(-ret));
2633f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2634e1f26343SJason M. Bills             return;
2635e1f26343SJason M. Bills         }
2636002d39b4SEd Tanous         std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2637002d39b4SEd Tanous             journalTmp, sd_journal_close);
2638e1f26343SJason M. Bills         journalTmp = nullptr;
26397e860f15SJohn Edward Broadbent         // Go to the timestamp in the log and move to the entry at the
26407e860f15SJohn Edward Broadbent         // index tracking the unique ID
2641af07e3f5SJason M. Bills         std::string idStr;
2642af07e3f5SJason M. Bills         bool firstEntry = true;
2643e1f26343SJason M. Bills         ret = sd_journal_seek_realtime_usec(journal.get(), ts);
26442056b6d1SManojkiran Eda         if (ret < 0)
26452056b6d1SManojkiran Eda         {
2646*62598e31SEd Tanous             BMCWEB_LOG_ERROR("failed to seek to an entry in journal{}",
2647*62598e31SEd Tanous                              strerror(-ret));
26482056b6d1SManojkiran Eda             messages::internalError(asyncResp->res);
26492056b6d1SManojkiran Eda             return;
26502056b6d1SManojkiran Eda         }
2651271584abSEd Tanous         for (uint64_t i = 0; i <= index; i++)
2652e1f26343SJason M. Bills         {
2653e1f26343SJason M. Bills             sd_journal_next(journal.get());
2654af07e3f5SJason M. Bills             if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2655af07e3f5SJason M. Bills             {
2656af07e3f5SJason M. Bills                 messages::internalError(asyncResp->res);
2657af07e3f5SJason M. Bills                 return;
2658af07e3f5SJason M. Bills             }
2659af07e3f5SJason M. Bills             firstEntry = false;
2660af07e3f5SJason M. Bills         }
2661c4bf6374SJason M. Bills         // Confirm that the entry ID matches what was requested
2662af07e3f5SJason M. Bills         if (idStr != entryID)
2663c4bf6374SJason M. Bills         {
26649db4ba25SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
2665c4bf6374SJason M. Bills             return;
2666c4bf6374SJason M. Bills         }
2667c4bf6374SJason M. Bills 
26683a48b3a2SJason M. Bills         nlohmann::json::object_t bmcJournalLogEntry;
2669c4bf6374SJason M. Bills         if (fillBMCJournalLogEntryJson(entryID, journal.get(),
26703a48b3a2SJason M. Bills                                        bmcJournalLogEntry) != 0)
2671e1f26343SJason M. Bills         {
2672f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2673e1f26343SJason M. Bills             return;
2674e1f26343SJason M. Bills         }
2675d405bb51SJason M. Bills         asyncResp->res.jsonValue.update(bmcJournalLogEntry);
26767e860f15SJohn Edward Broadbent         });
2677c9bb6861Sraviteja-b }
2678c9bb6861Sraviteja-b 
2679fdd26906SClaire Weinan inline void
2680fdd26906SClaire Weinan     getDumpServiceInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2681fdd26906SClaire Weinan                        const std::string& dumpType)
2682c9bb6861Sraviteja-b {
2683fdd26906SClaire Weinan     std::string dumpPath;
2684fdd26906SClaire Weinan     std::string overWritePolicy;
2685fdd26906SClaire Weinan     bool collectDiagnosticDataSupported = false;
2686fdd26906SClaire Weinan 
2687fdd26906SClaire Weinan     if (dumpType == "BMC")
268845ca1b86SEd Tanous     {
2689fdd26906SClaire Weinan         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump";
2690fdd26906SClaire Weinan         overWritePolicy = "WrapsWhenFull";
2691fdd26906SClaire Weinan         collectDiagnosticDataSupported = true;
2692fdd26906SClaire Weinan     }
2693fdd26906SClaire Weinan     else if (dumpType == "FaultLog")
2694fdd26906SClaire Weinan     {
2695fdd26906SClaire Weinan         dumpPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2696fdd26906SClaire Weinan         overWritePolicy = "Unknown";
2697fdd26906SClaire Weinan         collectDiagnosticDataSupported = false;
2698fdd26906SClaire Weinan     }
2699fdd26906SClaire Weinan     else if (dumpType == "System")
2700fdd26906SClaire Weinan     {
2701fdd26906SClaire Weinan         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump";
2702fdd26906SClaire Weinan         overWritePolicy = "WrapsWhenFull";
2703fdd26906SClaire Weinan         collectDiagnosticDataSupported = true;
2704fdd26906SClaire Weinan     }
2705fdd26906SClaire Weinan     else
2706fdd26906SClaire Weinan     {
2707*62598e31SEd Tanous         BMCWEB_LOG_ERROR("getDumpServiceInfo() invalid dump type: {}",
2708*62598e31SEd Tanous                          dumpType);
2709fdd26906SClaire Weinan         messages::internalError(asyncResp->res);
271045ca1b86SEd Tanous         return;
271145ca1b86SEd Tanous     }
2712fdd26906SClaire Weinan 
2713fdd26906SClaire Weinan     asyncResp->res.jsonValue["@odata.id"] = dumpPath;
2714fdd26906SClaire Weinan     asyncResp->res.jsonValue["@odata.type"] = "#LogService.v1_2_0.LogService";
2715c9bb6861Sraviteja-b     asyncResp->res.jsonValue["Name"] = "Dump LogService";
2716fdd26906SClaire Weinan     asyncResp->res.jsonValue["Description"] = dumpType + " Dump LogService";
2717fdd26906SClaire Weinan     asyncResp->res.jsonValue["Id"] = std::filesystem::path(dumpPath).filename();
2718fdd26906SClaire Weinan     asyncResp->res.jsonValue["OverWritePolicy"] = std::move(overWritePolicy);
27197c8c4058STejas Patil 
27207c8c4058STejas Patil     std::pair<std::string, std::string> redfishDateTimeOffset =
27212b82937eSEd Tanous         redfish::time_utils::getDateTimeOffsetNow();
27220fda0f12SGeorge Liu     asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
27237c8c4058STejas Patil     asyncResp->res.jsonValue["DateTimeLocalOffset"] =
27247c8c4058STejas Patil         redfishDateTimeOffset.second;
27257c8c4058STejas Patil 
2726fdd26906SClaire Weinan     asyncResp->res.jsonValue["Entries"]["@odata.id"] = dumpPath + "/Entries";
2727fdd26906SClaire Weinan 
2728fdd26906SClaire Weinan     if (collectDiagnosticDataSupported)
2729fdd26906SClaire Weinan     {
2730002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
27311476687dSEd Tanous                                 ["target"] =
2732fdd26906SClaire Weinan             dumpPath + "/Actions/LogService.CollectDiagnosticData";
2733fdd26906SClaire Weinan     }
27340d946211SClaire Weinan 
27350d946211SClaire Weinan     constexpr std::array<std::string_view, 1> interfaces = {deleteAllInterface};
27360d946211SClaire Weinan     dbus::utility::getSubTreePaths(
27370d946211SClaire Weinan         "/xyz/openbmc_project/dump", 0, interfaces,
27380d946211SClaire Weinan         [asyncResp, dumpType, dumpPath](
27390d946211SClaire Weinan             const boost::system::error_code& ec,
27400d946211SClaire Weinan             const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
27410d946211SClaire Weinan         if (ec)
27420d946211SClaire Weinan         {
2743*62598e31SEd Tanous             BMCWEB_LOG_ERROR("getDumpServiceInfo respHandler got error {}", ec);
27440d946211SClaire Weinan             // Assume that getting an error simply means there are no dump
27450d946211SClaire Weinan             // LogServices. Return without adding any error response.
27460d946211SClaire Weinan             return;
27470d946211SClaire Weinan         }
27480d946211SClaire Weinan 
27490d946211SClaire Weinan         const std::string dbusDumpPath =
27500d946211SClaire Weinan             "/xyz/openbmc_project/dump/" +
27510d946211SClaire Weinan             boost::algorithm::to_lower_copy(dumpType);
27520d946211SClaire Weinan 
27530d946211SClaire Weinan         for (const std::string& path : subTreePaths)
27540d946211SClaire Weinan         {
27550d946211SClaire Weinan             if (path == dbusDumpPath)
27560d946211SClaire Weinan             {
27570d946211SClaire Weinan                 asyncResp->res
27580d946211SClaire Weinan                     .jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
27590d946211SClaire Weinan                     dumpPath + "/Actions/LogService.ClearLog";
27600d946211SClaire Weinan                 break;
27610d946211SClaire Weinan             }
27620d946211SClaire Weinan         }
27630d946211SClaire Weinan         });
2764c9bb6861Sraviteja-b }
2765c9bb6861Sraviteja-b 
2766fdd26906SClaire Weinan inline void handleLogServicesDumpServiceGet(
2767fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2768fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
27697e860f15SJohn Edward Broadbent {
27703ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
277145ca1b86SEd Tanous     {
277245ca1b86SEd Tanous         return;
277345ca1b86SEd Tanous     }
2774fdd26906SClaire Weinan     getDumpServiceInfo(asyncResp, dumpType);
2775fdd26906SClaire Weinan }
2776c9bb6861Sraviteja-b 
277722d268cbSEd Tanous inline void handleLogServicesDumpServiceComputerSystemGet(
277822d268cbSEd Tanous     crow::App& app, const crow::Request& req,
277922d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
278022d268cbSEd Tanous     const std::string& chassisId)
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     getDumpServiceInfo(asyncResp, "System");
279222d268cbSEd Tanous }
279322d268cbSEd Tanous 
2794fdd26906SClaire Weinan inline void handleLogServicesDumpEntriesCollectionGet(
2795fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2796fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2797fdd26906SClaire Weinan {
2798fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2799fdd26906SClaire Weinan     {
2800fdd26906SClaire Weinan         return;
2801fdd26906SClaire Weinan     }
2802fdd26906SClaire Weinan     getDumpEntryCollection(asyncResp, dumpType);
2803fdd26906SClaire Weinan }
2804fdd26906SClaire Weinan 
280522d268cbSEd Tanous inline void handleLogServicesDumpEntriesCollectionComputerSystemGet(
280622d268cbSEd Tanous     crow::App& app, const crow::Request& req,
280722d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
280822d268cbSEd Tanous     const std::string& chassisId)
280922d268cbSEd Tanous {
281022d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
281122d268cbSEd Tanous     {
281222d268cbSEd Tanous         return;
281322d268cbSEd Tanous     }
281422d268cbSEd Tanous     if (chassisId != "system")
281522d268cbSEd Tanous     {
281622d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
281722d268cbSEd Tanous         return;
281822d268cbSEd Tanous     }
281922d268cbSEd Tanous     getDumpEntryCollection(asyncResp, "System");
282022d268cbSEd Tanous }
282122d268cbSEd Tanous 
2822fdd26906SClaire Weinan inline void handleLogServicesDumpEntryGet(
2823fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2824fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2825fdd26906SClaire Weinan     const std::string& dumpId)
2826fdd26906SClaire Weinan {
2827fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2828fdd26906SClaire Weinan     {
2829fdd26906SClaire Weinan         return;
2830fdd26906SClaire Weinan     }
2831fdd26906SClaire Weinan     getDumpEntryById(asyncResp, dumpId, dumpType);
2832fdd26906SClaire Weinan }
283322d268cbSEd Tanous inline void handleLogServicesDumpEntryComputerSystemGet(
283422d268cbSEd Tanous     crow::App& app, const crow::Request& req,
283522d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
283622d268cbSEd Tanous     const std::string& chassisId, const std::string& dumpId)
283722d268cbSEd Tanous {
283822d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
283922d268cbSEd Tanous     {
284022d268cbSEd Tanous         return;
284122d268cbSEd Tanous     }
284222d268cbSEd Tanous     if (chassisId != "system")
284322d268cbSEd Tanous     {
284422d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
284522d268cbSEd Tanous         return;
284622d268cbSEd Tanous     }
284722d268cbSEd Tanous     getDumpEntryById(asyncResp, dumpId, "System");
284822d268cbSEd Tanous }
2849fdd26906SClaire Weinan 
2850fdd26906SClaire Weinan inline void handleLogServicesDumpEntryDelete(
2851fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2852fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2853fdd26906SClaire Weinan     const std::string& dumpId)
2854fdd26906SClaire Weinan {
2855fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2856fdd26906SClaire Weinan     {
2857fdd26906SClaire Weinan         return;
2858fdd26906SClaire Weinan     }
2859fdd26906SClaire Weinan     deleteDumpEntry(asyncResp, dumpId, dumpType);
2860fdd26906SClaire Weinan }
2861fdd26906SClaire Weinan 
286222d268cbSEd Tanous inline void handleLogServicesDumpEntryComputerSystemDelete(
286322d268cbSEd Tanous     crow::App& app, const crow::Request& req,
286422d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
286522d268cbSEd Tanous     const std::string& chassisId, const std::string& dumpId)
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     deleteDumpEntry(asyncResp, dumpId, "System");
287722d268cbSEd Tanous }
287822d268cbSEd Tanous 
2879fdd26906SClaire Weinan inline void handleLogServicesDumpCollectDiagnosticDataPost(
2880fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2881fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2882fdd26906SClaire Weinan {
2883fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2884fdd26906SClaire Weinan     {
2885fdd26906SClaire Weinan         return;
2886fdd26906SClaire Weinan     }
2887fdd26906SClaire Weinan     createDump(asyncResp, req, dumpType);
2888fdd26906SClaire Weinan }
2889fdd26906SClaire Weinan 
289022d268cbSEd Tanous inline void handleLogServicesDumpCollectDiagnosticDataComputerSystemPost(
289122d268cbSEd Tanous     crow::App& app, const crow::Request& req,
289222d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
28937f3e84a1SEd Tanous     const std::string& systemName)
289422d268cbSEd Tanous {
289522d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
289622d268cbSEd Tanous     {
289722d268cbSEd Tanous         return;
289822d268cbSEd Tanous     }
28997f3e84a1SEd Tanous 
29007f3e84a1SEd Tanous     if constexpr (bmcwebEnableMultiHost)
290122d268cbSEd Tanous     {
29027f3e84a1SEd Tanous         // Option currently returns no systems.  TBD
29037f3e84a1SEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
29047f3e84a1SEd Tanous                                    systemName);
29057f3e84a1SEd Tanous         return;
29067f3e84a1SEd Tanous     }
29077f3e84a1SEd Tanous     if (systemName != "system")
29087f3e84a1SEd Tanous     {
29097f3e84a1SEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
29107f3e84a1SEd Tanous                                    systemName);
291122d268cbSEd Tanous         return;
291222d268cbSEd Tanous     }
291322d268cbSEd Tanous     createDump(asyncResp, req, "System");
291422d268cbSEd Tanous }
291522d268cbSEd Tanous 
2916fdd26906SClaire Weinan inline void handleLogServicesDumpClearLogPost(
2917fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2918fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2919fdd26906SClaire Weinan {
2920fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2921fdd26906SClaire Weinan     {
2922fdd26906SClaire Weinan         return;
2923fdd26906SClaire Weinan     }
2924fdd26906SClaire Weinan     clearDump(asyncResp, dumpType);
2925fdd26906SClaire Weinan }
2926fdd26906SClaire Weinan 
292722d268cbSEd Tanous inline void handleLogServicesDumpClearLogComputerSystemPost(
292822d268cbSEd Tanous     crow::App& app, const crow::Request& req,
292922d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
29307f3e84a1SEd Tanous     const std::string& systemName)
293122d268cbSEd Tanous {
293222d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
293322d268cbSEd Tanous     {
293422d268cbSEd Tanous         return;
293522d268cbSEd Tanous     }
29367f3e84a1SEd Tanous     if constexpr (bmcwebEnableMultiHost)
293722d268cbSEd Tanous     {
29387f3e84a1SEd Tanous         // Option currently returns no systems.  TBD
29397f3e84a1SEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
29407f3e84a1SEd Tanous                                    systemName);
29417f3e84a1SEd Tanous         return;
29427f3e84a1SEd Tanous     }
29437f3e84a1SEd Tanous     if (systemName != "system")
29447f3e84a1SEd Tanous     {
29457f3e84a1SEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
29467f3e84a1SEd Tanous                                    systemName);
294722d268cbSEd Tanous         return;
294822d268cbSEd Tanous     }
294922d268cbSEd Tanous     clearDump(asyncResp, "System");
295022d268cbSEd Tanous }
295122d268cbSEd Tanous 
2952fdd26906SClaire Weinan inline void requestRoutesBMCDumpService(App& app)
2953fdd26906SClaire Weinan {
2954fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
2955fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogService)
2956fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2957fdd26906SClaire Weinan             handleLogServicesDumpServiceGet, std::ref(app), "BMC"));
2958fdd26906SClaire Weinan }
2959fdd26906SClaire Weinan 
2960fdd26906SClaire Weinan inline void requestRoutesBMCDumpEntryCollection(App& app)
2961fdd26906SClaire Weinan {
2962fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
2963fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogEntryCollection)
2964fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2965fdd26906SClaire Weinan             handleLogServicesDumpEntriesCollectionGet, std::ref(app), "BMC"));
2966c9bb6861Sraviteja-b }
2967c9bb6861Sraviteja-b 
29687e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpEntry(App& app)
2969c9bb6861Sraviteja-b {
29707e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
29717e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
2972ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
2973fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2974fdd26906SClaire Weinan             handleLogServicesDumpEntryGet, std::ref(app), "BMC"));
2975fdd26906SClaire Weinan 
29767e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
29777e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
2978ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
2979fdd26906SClaire Weinan         .methods(boost::beast::http::verb::delete_)(std::bind_front(
2980fdd26906SClaire Weinan             handleLogServicesDumpEntryDelete, std::ref(app), "BMC"));
2981c9bb6861Sraviteja-b }
2982c9bb6861Sraviteja-b 
29837e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpCreate(App& app)
2984c9bb6861Sraviteja-b {
29850fda0f12SGeorge Liu     BMCWEB_ROUTE(
29860fda0f12SGeorge Liu         app,
29870fda0f12SGeorge Liu         "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
2988ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
29897e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
2990fdd26906SClaire Weinan             std::bind_front(handleLogServicesDumpCollectDiagnosticDataPost,
2991fdd26906SClaire Weinan                             std::ref(app), "BMC"));
2992a43be80fSAsmitha Karunanithi }
2993a43be80fSAsmitha Karunanithi 
29947e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpClear(App& app)
299580319af1SAsmitha Karunanithi {
29960fda0f12SGeorge Liu     BMCWEB_ROUTE(
29970fda0f12SGeorge Liu         app,
29980fda0f12SGeorge Liu         "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog/")
2999ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
3000fdd26906SClaire Weinan         .methods(boost::beast::http::verb::post)(std::bind_front(
3001fdd26906SClaire Weinan             handleLogServicesDumpClearLogPost, std::ref(app), "BMC"));
300245ca1b86SEd Tanous }
3003fdd26906SClaire Weinan 
3004fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpService(App& app)
3005fdd26906SClaire Weinan {
3006fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/")
3007fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogService)
3008fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
3009fdd26906SClaire Weinan             handleLogServicesDumpServiceGet, std::ref(app), "FaultLog"));
3010fdd26906SClaire Weinan }
3011fdd26906SClaire Weinan 
3012fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpEntryCollection(App& app)
3013fdd26906SClaire Weinan {
3014fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/")
3015fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogEntryCollection)
3016fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(
3017fdd26906SClaire Weinan             std::bind_front(handleLogServicesDumpEntriesCollectionGet,
3018fdd26906SClaire Weinan                             std::ref(app), "FaultLog"));
3019fdd26906SClaire Weinan }
3020fdd26906SClaire Weinan 
3021fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpEntry(App& app)
3022fdd26906SClaire Weinan {
3023fdd26906SClaire Weinan     BMCWEB_ROUTE(app,
3024fdd26906SClaire Weinan                  "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
3025fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogEntry)
3026fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
3027fdd26906SClaire Weinan             handleLogServicesDumpEntryGet, std::ref(app), "FaultLog"));
3028fdd26906SClaire Weinan 
3029fdd26906SClaire Weinan     BMCWEB_ROUTE(app,
3030fdd26906SClaire Weinan                  "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
3031fdd26906SClaire Weinan         .privileges(redfish::privileges::deleteLogEntry)
3032fdd26906SClaire Weinan         .methods(boost::beast::http::verb::delete_)(std::bind_front(
3033fdd26906SClaire Weinan             handleLogServicesDumpEntryDelete, std::ref(app), "FaultLog"));
3034fdd26906SClaire Weinan }
3035fdd26906SClaire Weinan 
3036fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpClear(App& app)
3037fdd26906SClaire Weinan {
3038fdd26906SClaire Weinan     BMCWEB_ROUTE(
3039fdd26906SClaire Weinan         app,
3040fdd26906SClaire Weinan         "/redfish/v1/Managers/bmc/LogServices/FaultLog/Actions/LogService.ClearLog/")
3041fdd26906SClaire Weinan         .privileges(redfish::privileges::postLogService)
3042fdd26906SClaire Weinan         .methods(boost::beast::http::verb::post)(std::bind_front(
3043fdd26906SClaire Weinan             handleLogServicesDumpClearLogPost, std::ref(app), "FaultLog"));
30445cb1dd27SAsmitha Karunanithi }
30455cb1dd27SAsmitha Karunanithi 
30467e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpService(App& app)
30475cb1dd27SAsmitha Karunanithi {
304822d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/")
3049ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
30506ab9ad54SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
305122d268cbSEd Tanous             handleLogServicesDumpServiceComputerSystemGet, std::ref(app)));
30525cb1dd27SAsmitha Karunanithi }
30535cb1dd27SAsmitha Karunanithi 
30547e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpEntryCollection(App& app)
30557e860f15SJohn Edward Broadbent {
305622d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/")
3057ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
305822d268cbSEd Tanous         .methods(boost::beast::http::verb::get)(std::bind_front(
305922d268cbSEd Tanous             handleLogServicesDumpEntriesCollectionComputerSystemGet,
306022d268cbSEd Tanous             std::ref(app)));
30615cb1dd27SAsmitha Karunanithi }
30625cb1dd27SAsmitha Karunanithi 
30637e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpEntry(App& app)
30645cb1dd27SAsmitha Karunanithi {
30657e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
306622d268cbSEd Tanous                  "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
3067ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
30686ab9ad54SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
306922d268cbSEd Tanous             handleLogServicesDumpEntryComputerSystemGet, std::ref(app)));
30708d1b46d7Szhanghch05 
30717e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
307222d268cbSEd Tanous                  "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
3073ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
30746ab9ad54SClaire Weinan         .methods(boost::beast::http::verb::delete_)(std::bind_front(
307522d268cbSEd Tanous             handleLogServicesDumpEntryComputerSystemDelete, std::ref(app)));
30765cb1dd27SAsmitha Karunanithi }
3077c9bb6861Sraviteja-b 
30787e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpCreate(App& app)
3079c9bb6861Sraviteja-b {
30800fda0f12SGeorge Liu     BMCWEB_ROUTE(
30810fda0f12SGeorge Liu         app,
308222d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
3083ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
308422d268cbSEd Tanous         .methods(boost::beast::http::verb::post)(std::bind_front(
308522d268cbSEd Tanous             handleLogServicesDumpCollectDiagnosticDataComputerSystemPost,
308622d268cbSEd Tanous             std::ref(app)));
3087a43be80fSAsmitha Karunanithi }
3088a43be80fSAsmitha Karunanithi 
30897e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpClear(App& app)
3090a43be80fSAsmitha Karunanithi {
30910fda0f12SGeorge Liu     BMCWEB_ROUTE(
30920fda0f12SGeorge Liu         app,
309322d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.ClearLog/")
3094ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
30956ab9ad54SClaire Weinan         .methods(boost::beast::http::verb::post)(std::bind_front(
309622d268cbSEd Tanous             handleLogServicesDumpClearLogComputerSystemPost, std::ref(app)));
3097013487e5Sraviteja-b }
3098013487e5Sraviteja-b 
30997e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpService(App& app)
31001da66f75SEd Tanous {
31013946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
31023946028dSAppaRao Puli     // method for security reasons.
31031da66f75SEd Tanous     /**
31041da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
31051da66f75SEd Tanous      */
310622d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/")
3107ed398213SEd Tanous         // This is incorrect, should be:
3108ed398213SEd Tanous         //.privileges(redfish::privileges::getLogService)
3109432a890cSEd Tanous         .privileges({{"ConfigureManager"}})
3110002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
3111002d39b4SEd Tanous             [&app](const crow::Request& req,
311222d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
311322d268cbSEd Tanous                    const std::string& systemName) {
31143ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
311545ca1b86SEd Tanous         {
311645ca1b86SEd Tanous             return;
311745ca1b86SEd Tanous         }
31187f3e84a1SEd Tanous         if constexpr (bmcwebEnableMultiHost)
31197f3e84a1SEd Tanous         {
31207f3e84a1SEd Tanous             // Option currently returns no systems.  TBD
31217f3e84a1SEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
31227f3e84a1SEd Tanous                                        systemName);
31237f3e84a1SEd Tanous             return;
31247f3e84a1SEd Tanous         }
312522d268cbSEd Tanous         if (systemName != "system")
312622d268cbSEd Tanous         {
312722d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
312822d268cbSEd Tanous                                        systemName);
312922d268cbSEd Tanous             return;
313022d268cbSEd Tanous         }
313122d268cbSEd Tanous 
31327e860f15SJohn Edward Broadbent         // Copy over the static data to include the entries added by
31337e860f15SJohn Edward Broadbent         // SubRoute
31340f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
3135424c4176SJason M. Bills             "/redfish/v1/Systems/system/LogServices/Crashdump";
3136e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
31378e6c099aSJason M. Bills             "#LogService.v1_2_0.LogService";
31384f50ae4bSGunnar Mills         asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
31394f50ae4bSGunnar Mills         asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
314015b89725SV-Sanjana         asyncResp->res.jsonValue["Id"] = "Crashdump";
3141e1f26343SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
3142e1f26343SJason M. Bills         asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
31437c8c4058STejas Patil 
31447c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
31452b82937eSEd Tanous             redfish::time_utils::getDateTimeOffsetNow();
31467c8c4058STejas Patil         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
31477c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
31487c8c4058STejas Patil             redfishDateTimeOffset.second;
31497c8c4058STejas Patil 
31501476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
3151ef4c65b7SEd Tanous             "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
3152002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
31531476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog";
3154002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
31551476687dSEd Tanous                                 ["target"] =
31561476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData";
31577e860f15SJohn Edward Broadbent         });
31581da66f75SEd Tanous }
31591da66f75SEd Tanous 
31607e860f15SJohn Edward Broadbent void inline requestRoutesCrashdumpClear(App& app)
31615b61b5e8SJason M. Bills {
31620fda0f12SGeorge Liu     BMCWEB_ROUTE(
31630fda0f12SGeorge Liu         app,
316422d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.ClearLog/")
3165ed398213SEd Tanous         // This is incorrect, should be:
3166ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
3167432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
31687e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
316945ca1b86SEd Tanous             [&app](const crow::Request& req,
317022d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
317122d268cbSEd Tanous                    const std::string& systemName) {
31723ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
317345ca1b86SEd Tanous         {
317445ca1b86SEd Tanous             return;
317545ca1b86SEd Tanous         }
31767f3e84a1SEd Tanous         if constexpr (bmcwebEnableMultiHost)
31777f3e84a1SEd Tanous         {
31787f3e84a1SEd Tanous             // Option currently returns no systems.  TBD
31797f3e84a1SEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
31807f3e84a1SEd Tanous                                        systemName);
31817f3e84a1SEd Tanous             return;
31827f3e84a1SEd Tanous         }
318322d268cbSEd Tanous         if (systemName != "system")
318422d268cbSEd Tanous         {
318522d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
318622d268cbSEd Tanous                                        systemName);
318722d268cbSEd Tanous             return;
318822d268cbSEd Tanous         }
31895b61b5e8SJason M. Bills         crow::connections::systemBus->async_method_call(
31905e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec,
3191cb13a392SEd Tanous                         const std::string&) {
31925b61b5e8SJason M. Bills             if (ec)
31935b61b5e8SJason M. Bills             {
31945b61b5e8SJason M. Bills                 messages::internalError(asyncResp->res);
31955b61b5e8SJason M. Bills                 return;
31965b61b5e8SJason M. Bills             }
31975b61b5e8SJason M. Bills             messages::success(asyncResp->res);
31985b61b5e8SJason M. Bills             },
3199002d39b4SEd Tanous             crashdumpObject, crashdumpPath, deleteAllInterface, "DeleteAll");
32007e860f15SJohn Edward Broadbent         });
32015b61b5e8SJason M. Bills }
32025b61b5e8SJason M. Bills 
32038d1b46d7Szhanghch05 static void
32048d1b46d7Szhanghch05     logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
32058d1b46d7Szhanghch05                       const std::string& logID, nlohmann::json& logEntryJson)
3206e855dd28SJason M. Bills {
3207043a0536SJohnathan Mantey     auto getStoredLogCallback =
3208b9d36b47SEd Tanous         [asyncResp, logID,
32095e7e2dc5SEd Tanous          &logEntryJson](const boost::system::error_code& ec,
3210b9d36b47SEd Tanous                         const dbus::utility::DBusPropertiesMap& params) {
3211e855dd28SJason M. Bills         if (ec)
3212e855dd28SJason M. Bills         {
3213*62598e31SEd Tanous             BMCWEB_LOG_DEBUG("failed to get log ec: {}", ec.message());
32141ddcf01aSJason M. Bills             if (ec.value() ==
32151ddcf01aSJason M. Bills                 boost::system::linux_error::bad_request_descriptor)
32161ddcf01aSJason M. Bills             {
3217002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
32181ddcf01aSJason M. Bills             }
32191ddcf01aSJason M. Bills             else
32201ddcf01aSJason M. Bills             {
3221e855dd28SJason M. Bills                 messages::internalError(asyncResp->res);
32221ddcf01aSJason M. Bills             }
3223e855dd28SJason M. Bills             return;
3224e855dd28SJason M. Bills         }
3225043a0536SJohnathan Mantey 
3226043a0536SJohnathan Mantey         std::string timestamp{};
3227043a0536SJohnathan Mantey         std::string filename{};
3228043a0536SJohnathan Mantey         std::string logfile{};
32292c70f800SEd Tanous         parseCrashdumpParameters(params, filename, timestamp, logfile);
3230043a0536SJohnathan Mantey 
3231043a0536SJohnathan Mantey         if (filename.empty() || timestamp.empty())
3232e855dd28SJason M. Bills         {
32339db4ba25SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
3234e855dd28SJason M. Bills             return;
3235e855dd28SJason M. Bills         }
3236e855dd28SJason M. Bills 
3237043a0536SJohnathan Mantey         std::string crashdumpURI =
3238e855dd28SJason M. Bills             "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
3239043a0536SJohnathan Mantey             logID + "/" + filename;
324084afc48bSJason M. Bills         nlohmann::json::object_t logEntry;
32419c11a172SVijay Lobo         logEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
3242ef4c65b7SEd Tanous         logEntry["@odata.id"] = boost::urls::format(
3243ef4c65b7SEd Tanous             "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/{}",
3244ef4c65b7SEd Tanous             logID);
324584afc48bSJason M. Bills         logEntry["Name"] = "CPU Crashdump";
324684afc48bSJason M. Bills         logEntry["Id"] = logID;
324784afc48bSJason M. Bills         logEntry["EntryType"] = "Oem";
324884afc48bSJason M. Bills         logEntry["AdditionalDataURI"] = std::move(crashdumpURI);
324984afc48bSJason M. Bills         logEntry["DiagnosticDataType"] = "OEM";
325084afc48bSJason M. Bills         logEntry["OEMDiagnosticDataType"] = "PECICrashdump";
325184afc48bSJason M. Bills         logEntry["Created"] = std::move(timestamp);
32522b20ef6eSJason M. Bills 
32532b20ef6eSJason M. Bills         // If logEntryJson references an array of LogEntry resources
32542b20ef6eSJason M. Bills         // ('Members' list), then push this as a new entry, otherwise set it
32552b20ef6eSJason M. Bills         // directly
32562b20ef6eSJason M. Bills         if (logEntryJson.is_array())
32572b20ef6eSJason M. Bills         {
32582b20ef6eSJason M. Bills             logEntryJson.push_back(logEntry);
32592b20ef6eSJason M. Bills             asyncResp->res.jsonValue["Members@odata.count"] =
32602b20ef6eSJason M. Bills                 logEntryJson.size();
32612b20ef6eSJason M. Bills         }
32622b20ef6eSJason M. Bills         else
32632b20ef6eSJason M. Bills         {
3264d405bb51SJason M. Bills             logEntryJson.update(logEntry);
32652b20ef6eSJason M. Bills         }
3266e855dd28SJason M. Bills     };
3267d1bde9e5SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
3268d1bde9e5SKrzysztof Grobelny         *crow::connections::systemBus, crashdumpObject,
3269d1bde9e5SKrzysztof Grobelny         crashdumpPath + std::string("/") + logID, crashdumpInterface,
3270d1bde9e5SKrzysztof Grobelny         std::move(getStoredLogCallback));
3271e855dd28SJason M. Bills }
3272e855dd28SJason M. Bills 
32737e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpEntryCollection(App& app)
32741da66f75SEd Tanous {
32753946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
32763946028dSAppaRao Puli     // method for security reasons.
32771da66f75SEd Tanous     /**
32781da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
32791da66f75SEd Tanous      */
32807e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
328122d268cbSEd Tanous                  "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/")
3282ed398213SEd Tanous         // This is incorrect, should be.
3283ed398213SEd Tanous         //.privileges(redfish::privileges::postLogEntryCollection)
3284432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
3285002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
3286002d39b4SEd Tanous             [&app](const crow::Request& req,
328722d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
328822d268cbSEd Tanous                    const std::string& systemName) {
32893ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
329045ca1b86SEd Tanous         {
329145ca1b86SEd Tanous             return;
329245ca1b86SEd Tanous         }
32937f3e84a1SEd Tanous         if constexpr (bmcwebEnableMultiHost)
32947f3e84a1SEd Tanous         {
32957f3e84a1SEd Tanous             // Option currently returns no systems.  TBD
32967f3e84a1SEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
32977f3e84a1SEd Tanous                                        systemName);
32987f3e84a1SEd Tanous             return;
32997f3e84a1SEd Tanous         }
330022d268cbSEd Tanous         if (systemName != "system")
330122d268cbSEd Tanous         {
330222d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
330322d268cbSEd Tanous                                        systemName);
330422d268cbSEd Tanous             return;
330522d268cbSEd Tanous         }
330622d268cbSEd Tanous 
33077a1dbc48SGeorge Liu         constexpr std::array<std::string_view, 1> interfaces = {
33087a1dbc48SGeorge Liu             crashdumpInterface};
33097a1dbc48SGeorge Liu         dbus::utility::getSubTreePaths(
33107a1dbc48SGeorge Liu             "/", 0, interfaces,
33117a1dbc48SGeorge Liu             [asyncResp](const boost::system::error_code& ec,
33122b20ef6eSJason M. Bills                         const std::vector<std::string>& resp) {
33131da66f75SEd Tanous             if (ec)
33141da66f75SEd Tanous             {
33151da66f75SEd Tanous                 if (ec.value() !=
33161da66f75SEd Tanous                     boost::system::errc::no_such_file_or_directory)
33171da66f75SEd Tanous                 {
3318*62598e31SEd Tanous                     BMCWEB_LOG_DEBUG("failed to get entries ec: {}",
3319*62598e31SEd Tanous                                      ec.message());
3320f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
33211da66f75SEd Tanous                     return;
33221da66f75SEd Tanous                 }
33231da66f75SEd Tanous             }
3324e1f26343SJason M. Bills             asyncResp->res.jsonValue["@odata.type"] =
33251da66f75SEd Tanous                 "#LogEntryCollection.LogEntryCollection";
33260f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
3327424c4176SJason M. Bills                 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
3328002d39b4SEd Tanous             asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
3329e1f26343SJason M. Bills             asyncResp->res.jsonValue["Description"] =
3330424c4176SJason M. Bills                 "Collection of Crashdump Entries";
3331002d39b4SEd Tanous             asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3332a2dd60a6SBrandon Kim             asyncResp->res.jsonValue["Members@odata.count"] = 0;
33332b20ef6eSJason M. Bills 
33342b20ef6eSJason M. Bills             for (const std::string& path : resp)
33351da66f75SEd Tanous             {
33362b20ef6eSJason M. Bills                 const sdbusplus::message::object_path objPath(path);
3337e855dd28SJason M. Bills                 // Get the log ID
33382b20ef6eSJason M. Bills                 std::string logID = objPath.filename();
33392b20ef6eSJason M. Bills                 if (logID.empty())
33401da66f75SEd Tanous                 {
3341e855dd28SJason M. Bills                     continue;
33421da66f75SEd Tanous                 }
3343e855dd28SJason M. Bills                 // Add the log entry to the array
33442b20ef6eSJason M. Bills                 logCrashdumpEntry(asyncResp, logID,
33452b20ef6eSJason M. Bills                                   asyncResp->res.jsonValue["Members"]);
33461da66f75SEd Tanous             }
33477a1dbc48SGeorge Liu             });
33487e860f15SJohn Edward Broadbent         });
33491da66f75SEd Tanous }
33501da66f75SEd Tanous 
33517e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpEntry(App& app)
33521da66f75SEd Tanous {
33533946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
33543946028dSAppaRao Puli     // method for security reasons.
33551da66f75SEd Tanous 
33567e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
335722d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/")
3358ed398213SEd Tanous         // this is incorrect, should be
3359ed398213SEd Tanous         // .privileges(redfish::privileges::getLogEntry)
3360432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
33617e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
336245ca1b86SEd Tanous             [&app](const crow::Request& req,
33637e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
336422d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
33653ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
336645ca1b86SEd Tanous         {
336745ca1b86SEd Tanous             return;
336845ca1b86SEd Tanous         }
33697f3e84a1SEd Tanous         if constexpr (bmcwebEnableMultiHost)
33707f3e84a1SEd Tanous         {
33717f3e84a1SEd Tanous             // Option currently returns no systems.  TBD
33727f3e84a1SEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
33737f3e84a1SEd Tanous                                        systemName);
33747f3e84a1SEd Tanous             return;
33757f3e84a1SEd Tanous         }
337622d268cbSEd Tanous         if (systemName != "system")
337722d268cbSEd Tanous         {
337822d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
337922d268cbSEd Tanous                                        systemName);
338022d268cbSEd Tanous             return;
338122d268cbSEd Tanous         }
33827e860f15SJohn Edward Broadbent         const std::string& logID = param;
3383e855dd28SJason M. Bills         logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
33847e860f15SJohn Edward Broadbent         });
3385e855dd28SJason M. Bills }
3386e855dd28SJason M. Bills 
33877e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpFile(App& app)
3388e855dd28SJason M. Bills {
33893946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
33903946028dSAppaRao Puli     // method for security reasons.
33917e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
33927e860f15SJohn Edward Broadbent         app,
339322d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/<str>/")
3394ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
33957e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
3396a4ce114aSNan Zhou             [](const crow::Request& req,
33977e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
339822d268cbSEd Tanous                const std::string& systemName, const std::string& logID,
339922d268cbSEd Tanous                const std::string& fileName) {
34002a9beeedSShounak Mitra         // Do not call getRedfishRoute here since the crashdump file is not a
34012a9beeedSShounak Mitra         // Redfish resource.
340222d268cbSEd Tanous 
34037f3e84a1SEd Tanous         if constexpr (bmcwebEnableMultiHost)
34047f3e84a1SEd Tanous         {
34057f3e84a1SEd Tanous             // Option currently returns no systems.  TBD
34067f3e84a1SEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
34077f3e84a1SEd Tanous                                        systemName);
34087f3e84a1SEd Tanous             return;
34097f3e84a1SEd Tanous         }
341022d268cbSEd Tanous         if (systemName != "system")
341122d268cbSEd Tanous         {
341222d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
341322d268cbSEd Tanous                                        systemName);
341422d268cbSEd Tanous             return;
341522d268cbSEd Tanous         }
341622d268cbSEd Tanous 
3417043a0536SJohnathan Mantey         auto getStoredLogCallback =
341839662a3bSEd Tanous             [asyncResp, logID, fileName, url(boost::urls::url(req.url()))](
34195e7e2dc5SEd Tanous                 const boost::system::error_code& ec,
3420002d39b4SEd Tanous                 const std::vector<
3421002d39b4SEd Tanous                     std::pair<std::string, dbus::utility::DbusVariantType>>&
34227e860f15SJohn Edward Broadbent                     resp) {
34231da66f75SEd Tanous             if (ec)
34241da66f75SEd Tanous             {
3425*62598e31SEd Tanous                 BMCWEB_LOG_DEBUG("failed to get log ec: {}", ec.message());
3426f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
34271da66f75SEd Tanous                 return;
34281da66f75SEd Tanous             }
3429e855dd28SJason M. Bills 
3430043a0536SJohnathan Mantey             std::string dbusFilename{};
3431043a0536SJohnathan Mantey             std::string dbusTimestamp{};
3432043a0536SJohnathan Mantey             std::string dbusFilepath{};
3433043a0536SJohnathan Mantey 
3434002d39b4SEd Tanous             parseCrashdumpParameters(resp, dbusFilename, dbusTimestamp,
3435002d39b4SEd Tanous                                      dbusFilepath);
3436043a0536SJohnathan Mantey 
3437043a0536SJohnathan Mantey             if (dbusFilename.empty() || dbusTimestamp.empty() ||
3438043a0536SJohnathan Mantey                 dbusFilepath.empty())
34391da66f75SEd Tanous             {
34409db4ba25SJiaqing Zhao                 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
34411da66f75SEd Tanous                 return;
34421da66f75SEd Tanous             }
3443e855dd28SJason M. Bills 
3444043a0536SJohnathan Mantey             // Verify the file name parameter is correct
3445043a0536SJohnathan Mantey             if (fileName != dbusFilename)
3446043a0536SJohnathan Mantey             {
34479db4ba25SJiaqing Zhao                 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
3448043a0536SJohnathan Mantey                 return;
3449043a0536SJohnathan Mantey             }
3450043a0536SJohnathan Mantey 
3451043a0536SJohnathan Mantey             if (!std::filesystem::exists(dbusFilepath))
3452043a0536SJohnathan Mantey             {
34539db4ba25SJiaqing Zhao                 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
3454043a0536SJohnathan Mantey                 return;
3455043a0536SJohnathan Mantey             }
3456002d39b4SEd Tanous             std::ifstream ifs(dbusFilepath, std::ios::in | std::ios::binary);
3457002d39b4SEd Tanous             asyncResp->res.body() =
3458002d39b4SEd Tanous                 std::string(std::istreambuf_iterator<char>{ifs}, {});
3459043a0536SJohnathan Mantey 
34607e860f15SJohn Edward Broadbent             // Configure this to be a file download when accessed
34617e860f15SJohn Edward Broadbent             // from a browser
3462d9f6c621SEd Tanous             asyncResp->res.addHeader(
3463d9f6c621SEd Tanous                 boost::beast::http::field::content_disposition, "attachment");
34641da66f75SEd Tanous         };
3465d1bde9e5SKrzysztof Grobelny         sdbusplus::asio::getAllProperties(
3466d1bde9e5SKrzysztof Grobelny             *crow::connections::systemBus, crashdumpObject,
3467d1bde9e5SKrzysztof Grobelny             crashdumpPath + std::string("/") + logID, crashdumpInterface,
3468d1bde9e5SKrzysztof Grobelny             std::move(getStoredLogCallback));
34697e860f15SJohn Edward Broadbent         });
34701da66f75SEd Tanous }
34711da66f75SEd Tanous 
3472c5a4c82aSJason M. Bills enum class OEMDiagnosticType
3473c5a4c82aSJason M. Bills {
3474c5a4c82aSJason M. Bills     onDemand,
3475c5a4c82aSJason M. Bills     telemetry,
3476c5a4c82aSJason M. Bills     invalid,
3477c5a4c82aSJason M. Bills };
3478c5a4c82aSJason M. Bills 
347926ccae32SEd Tanous inline OEMDiagnosticType getOEMDiagnosticType(std::string_view oemDiagStr)
3480c5a4c82aSJason M. Bills {
3481c5a4c82aSJason M. Bills     if (oemDiagStr == "OnDemand")
3482c5a4c82aSJason M. Bills     {
3483c5a4c82aSJason M. Bills         return OEMDiagnosticType::onDemand;
3484c5a4c82aSJason M. Bills     }
3485c5a4c82aSJason M. Bills     if (oemDiagStr == "Telemetry")
3486c5a4c82aSJason M. Bills     {
3487c5a4c82aSJason M. Bills         return OEMDiagnosticType::telemetry;
3488c5a4c82aSJason M. Bills     }
3489c5a4c82aSJason M. Bills 
3490c5a4c82aSJason M. Bills     return OEMDiagnosticType::invalid;
3491c5a4c82aSJason M. Bills }
3492c5a4c82aSJason M. Bills 
34937e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpCollect(App& app)
34941da66f75SEd Tanous {
34953946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
34963946028dSAppaRao Puli     // method for security reasons.
34970fda0f12SGeorge Liu     BMCWEB_ROUTE(
34980fda0f12SGeorge Liu         app,
349922d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData/")
3500ed398213SEd Tanous         // The below is incorrect;  Should be ConfigureManager
3501ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
3502432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
3503002d39b4SEd Tanous         .methods(boost::beast::http::verb::post)(
3504002d39b4SEd Tanous             [&app](const crow::Request& req,
350522d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
350622d268cbSEd Tanous                    const std::string& systemName) {
35073ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
350845ca1b86SEd Tanous         {
350945ca1b86SEd Tanous             return;
351045ca1b86SEd Tanous         }
351122d268cbSEd Tanous 
35127f3e84a1SEd Tanous         if constexpr (bmcwebEnableMultiHost)
35137f3e84a1SEd Tanous         {
35147f3e84a1SEd Tanous             // Option currently returns no systems.  TBD
35157f3e84a1SEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
35167f3e84a1SEd Tanous                                        systemName);
35177f3e84a1SEd Tanous             return;
35187f3e84a1SEd Tanous         }
351922d268cbSEd Tanous         if (systemName != "system")
352022d268cbSEd Tanous         {
352122d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
352222d268cbSEd Tanous                                        systemName);
352322d268cbSEd Tanous             return;
352422d268cbSEd Tanous         }
352522d268cbSEd Tanous 
35268e6c099aSJason M. Bills         std::string diagnosticDataType;
35278e6c099aSJason M. Bills         std::string oemDiagnosticDataType;
352815ed6780SWilly Tu         if (!redfish::json_util::readJsonAction(
3529002d39b4SEd Tanous                 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
3530002d39b4SEd Tanous                 "OEMDiagnosticDataType", oemDiagnosticDataType))
35318e6c099aSJason M. Bills         {
35328e6c099aSJason M. Bills             return;
35338e6c099aSJason M. Bills         }
35348e6c099aSJason M. Bills 
35358e6c099aSJason M. Bills         if (diagnosticDataType != "OEM")
35368e6c099aSJason M. Bills         {
3537*62598e31SEd Tanous             BMCWEB_LOG_ERROR(
3538*62598e31SEd Tanous                 "Only OEM DiagnosticDataType supported for Crashdump");
35398e6c099aSJason M. Bills             messages::actionParameterValueFormatError(
35408e6c099aSJason M. Bills                 asyncResp->res, diagnosticDataType, "DiagnosticDataType",
35418e6c099aSJason M. Bills                 "CollectDiagnosticData");
35428e6c099aSJason M. Bills             return;
35438e6c099aSJason M. Bills         }
35448e6c099aSJason M. Bills 
3545c5a4c82aSJason M. Bills         OEMDiagnosticType oemDiagType =
3546c5a4c82aSJason M. Bills             getOEMDiagnosticType(oemDiagnosticDataType);
3547c5a4c82aSJason M. Bills 
3548c5a4c82aSJason M. Bills         std::string iface;
3549c5a4c82aSJason M. Bills         std::string method;
3550c5a4c82aSJason M. Bills         std::string taskMatchStr;
3551c5a4c82aSJason M. Bills         if (oemDiagType == OEMDiagnosticType::onDemand)
3552c5a4c82aSJason M. Bills         {
3553c5a4c82aSJason M. Bills             iface = crashdumpOnDemandInterface;
3554c5a4c82aSJason M. Bills             method = "GenerateOnDemandLog";
3555c5a4c82aSJason M. Bills             taskMatchStr = "type='signal',"
3556c5a4c82aSJason M. Bills                            "interface='org.freedesktop.DBus.Properties',"
3557c5a4c82aSJason M. Bills                            "member='PropertiesChanged',"
3558c5a4c82aSJason M. Bills                            "arg0namespace='com.intel.crashdump'";
3559c5a4c82aSJason M. Bills         }
3560c5a4c82aSJason M. Bills         else if (oemDiagType == OEMDiagnosticType::telemetry)
3561c5a4c82aSJason M. Bills         {
3562c5a4c82aSJason M. Bills             iface = crashdumpTelemetryInterface;
3563c5a4c82aSJason M. Bills             method = "GenerateTelemetryLog";
3564c5a4c82aSJason M. Bills             taskMatchStr = "type='signal',"
3565c5a4c82aSJason M. Bills                            "interface='org.freedesktop.DBus.Properties',"
3566c5a4c82aSJason M. Bills                            "member='PropertiesChanged',"
3567c5a4c82aSJason M. Bills                            "arg0namespace='com.intel.crashdump'";
3568c5a4c82aSJason M. Bills         }
3569c5a4c82aSJason M. Bills         else
3570c5a4c82aSJason M. Bills         {
3571*62598e31SEd Tanous             BMCWEB_LOG_ERROR("Unsupported OEMDiagnosticDataType: {}",
3572*62598e31SEd Tanous                              oemDiagnosticDataType);
3573c5a4c82aSJason M. Bills             messages::actionParameterValueFormatError(
3574002d39b4SEd Tanous                 asyncResp->res, oemDiagnosticDataType, "OEMDiagnosticDataType",
3575002d39b4SEd Tanous                 "CollectDiagnosticData");
3576c5a4c82aSJason M. Bills             return;
3577c5a4c82aSJason M. Bills         }
3578c5a4c82aSJason M. Bills 
3579c5a4c82aSJason M. Bills         auto collectCrashdumpCallback =
3580c5a4c82aSJason M. Bills             [asyncResp, payload(task::Payload(req)),
35815e7e2dc5SEd Tanous              taskMatchStr](const boost::system::error_code& ec,
358298be3e39SEd Tanous                            const std::string&) mutable {
35831da66f75SEd Tanous             if (ec)
35841da66f75SEd Tanous             {
3585002d39b4SEd Tanous                 if (ec.value() == boost::system::errc::operation_not_supported)
35861da66f75SEd Tanous                 {
3587f12894f8SJason M. Bills                     messages::resourceInStandby(asyncResp->res);
35881da66f75SEd Tanous                 }
35894363d3b2SJason M. Bills                 else if (ec.value() ==
35904363d3b2SJason M. Bills                          boost::system::errc::device_or_resource_busy)
35914363d3b2SJason M. Bills                 {
3592002d39b4SEd Tanous                     messages::serviceTemporarilyUnavailable(asyncResp->res,
3593002d39b4SEd Tanous                                                             "60");
35944363d3b2SJason M. Bills                 }
35951da66f75SEd Tanous                 else
35961da66f75SEd Tanous                 {
3597f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
35981da66f75SEd Tanous                 }
35991da66f75SEd Tanous                 return;
36001da66f75SEd Tanous             }
3601002d39b4SEd Tanous             std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
36028b24275dSEd Tanous                 [](const boost::system::error_code& ec2, sdbusplus::message_t&,
3603002d39b4SEd Tanous                    const std::shared_ptr<task::TaskData>& taskData) {
36048b24275dSEd Tanous                 if (!ec2)
360566afe4faSJames Feist                 {
3606002d39b4SEd Tanous                     taskData->messages.emplace_back(messages::taskCompletedOK(
3607e5d5006bSJames Feist                         std::to_string(taskData->index)));
3608831d6b09SJames Feist                     taskData->state = "Completed";
360966afe4faSJames Feist                 }
361032898ceaSJames Feist                 return task::completed;
361166afe4faSJames Feist                 },
3612c5a4c82aSJason M. Bills                 taskMatchStr);
3613c5a4c82aSJason M. Bills 
361446229577SJames Feist             task->startTimer(std::chrono::minutes(5));
361546229577SJames Feist             task->populateResp(asyncResp->res);
361698be3e39SEd Tanous             task->payload.emplace(std::move(payload));
36171da66f75SEd Tanous         };
36188e6c099aSJason M. Bills 
36191da66f75SEd Tanous         crow::connections::systemBus->async_method_call(
3620002d39b4SEd Tanous             std::move(collectCrashdumpCallback), crashdumpObject, crashdumpPath,
3621002d39b4SEd Tanous             iface, method);
36227e860f15SJohn Edward Broadbent         });
36236eda7685SKenny L. Ku }
36246eda7685SKenny L. Ku 
3625cb92c03bSAndrew Geissler /**
3626cb92c03bSAndrew Geissler  * DBusLogServiceActionsClear class supports POST method for ClearLog action.
3627cb92c03bSAndrew Geissler  */
36287e860f15SJohn Edward Broadbent inline void requestRoutesDBusLogServiceActionsClear(App& app)
3629cb92c03bSAndrew Geissler {
3630cb92c03bSAndrew Geissler     /**
3631cb92c03bSAndrew Geissler      * Function handles POST method request.
3632cb92c03bSAndrew Geissler      * The Clear Log actions does not require any parameter.The action deletes
3633cb92c03bSAndrew Geissler      * all entries found in the Entries collection for this Log Service.
3634cb92c03bSAndrew Geissler      */
36357e860f15SJohn Edward Broadbent 
36360fda0f12SGeorge Liu     BMCWEB_ROUTE(
36370fda0f12SGeorge Liu         app,
363822d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
3639ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
36407e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
364145ca1b86SEd Tanous             [&app](const crow::Request& req,
364222d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
364322d268cbSEd Tanous                    const std::string& systemName) {
36443ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
364545ca1b86SEd Tanous         {
364645ca1b86SEd Tanous             return;
364745ca1b86SEd Tanous         }
36487f3e84a1SEd Tanous         if constexpr (bmcwebEnableMultiHost)
36497f3e84a1SEd Tanous         {
36507f3e84a1SEd Tanous             // Option currently returns no systems.  TBD
36517f3e84a1SEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
36527f3e84a1SEd Tanous                                        systemName);
36537f3e84a1SEd Tanous             return;
36547f3e84a1SEd Tanous         }
365522d268cbSEd Tanous         if (systemName != "system")
365622d268cbSEd Tanous         {
365722d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
365822d268cbSEd Tanous                                        systemName);
365922d268cbSEd Tanous             return;
366022d268cbSEd Tanous         }
3661*62598e31SEd Tanous         BMCWEB_LOG_DEBUG("Do delete all entries.");
3662cb92c03bSAndrew Geissler 
3663cb92c03bSAndrew Geissler         // Process response from Logging service.
36645e7e2dc5SEd Tanous         auto respHandler = [asyncResp](const boost::system::error_code& ec) {
3665*62598e31SEd Tanous             BMCWEB_LOG_DEBUG("doClearLog resp_handler callback: Done");
3666cb92c03bSAndrew Geissler             if (ec)
3667cb92c03bSAndrew Geissler             {
3668cb92c03bSAndrew Geissler                 // TODO Handle for specific error code
3669*62598e31SEd Tanous                 BMCWEB_LOG_ERROR("doClearLog resp_handler got error {}", ec);
3670cb92c03bSAndrew Geissler                 asyncResp->res.result(
3671cb92c03bSAndrew Geissler                     boost::beast::http::status::internal_server_error);
3672cb92c03bSAndrew Geissler                 return;
3673cb92c03bSAndrew Geissler             }
3674cb92c03bSAndrew Geissler 
3675002d39b4SEd Tanous             asyncResp->res.result(boost::beast::http::status::no_content);
3676cb92c03bSAndrew Geissler         };
3677cb92c03bSAndrew Geissler 
3678cb92c03bSAndrew Geissler         // Make call to Logging service to request Clear Log
3679cb92c03bSAndrew Geissler         crow::connections::systemBus->async_method_call(
36802c70f800SEd Tanous             respHandler, "xyz.openbmc_project.Logging",
3681cb92c03bSAndrew Geissler             "/xyz/openbmc_project/logging",
3682cb92c03bSAndrew Geissler             "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
36837e860f15SJohn Edward Broadbent         });
3684cb92c03bSAndrew Geissler }
3685a3316fc6SZhikuiRen 
3686a3316fc6SZhikuiRen /****************************************************
3687a3316fc6SZhikuiRen  * Redfish PostCode interfaces
3688a3316fc6SZhikuiRen  * using DBUS interface: getPostCodesTS
3689a3316fc6SZhikuiRen  ******************************************************/
36907e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesLogService(App& app)
3691a3316fc6SZhikuiRen {
369222d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/")
3693ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
3694002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
3695002d39b4SEd Tanous             [&app](const crow::Request& req,
369622d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
369722d268cbSEd Tanous                    const std::string& systemName) {
36983ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
369945ca1b86SEd Tanous         {
370045ca1b86SEd Tanous             return;
370145ca1b86SEd Tanous         }
37027f3e84a1SEd Tanous         if constexpr (bmcwebEnableMultiHost)
37037f3e84a1SEd Tanous         {
37047f3e84a1SEd Tanous             // Option currently returns no systems.  TBD
37057f3e84a1SEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
37067f3e84a1SEd Tanous                                        systemName);
37077f3e84a1SEd Tanous             return;
37087f3e84a1SEd Tanous         }
370922d268cbSEd Tanous         if (systemName != "system")
371022d268cbSEd Tanous         {
371122d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
371222d268cbSEd Tanous                                        systemName);
371322d268cbSEd Tanous             return;
371422d268cbSEd Tanous         }
37151476687dSEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
37161476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/PostCodes";
37171476687dSEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
37181476687dSEd Tanous             "#LogService.v1_1_0.LogService";
37191476687dSEd Tanous         asyncResp->res.jsonValue["Name"] = "POST Code Log Service";
37201476687dSEd Tanous         asyncResp->res.jsonValue["Description"] = "POST Code Log Service";
3721ed34a4adSEd Tanous         asyncResp->res.jsonValue["Id"] = "PostCodes";
37221476687dSEd Tanous         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
37231476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
37241476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
37257c8c4058STejas Patil 
37267c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
37272b82937eSEd Tanous             redfish::time_utils::getDateTimeOffsetNow();
37280fda0f12SGeorge Liu         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
37297c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
37307c8c4058STejas Patil             redfishDateTimeOffset.second;
37317c8c4058STejas Patil 
3732a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
37337e860f15SJohn Edward Broadbent             {"target",
37340fda0f12SGeorge Liu              "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog"}};
37357e860f15SJohn Edward Broadbent         });
3736a3316fc6SZhikuiRen }
3737a3316fc6SZhikuiRen 
37387e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesClear(App& app)
3739a3316fc6SZhikuiRen {
37400fda0f12SGeorge Liu     BMCWEB_ROUTE(
37410fda0f12SGeorge Liu         app,
374222d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/PostCodes/Actions/LogService.ClearLog/")
3743ed398213SEd Tanous         // The following privilege is incorrect;  It should be ConfigureManager
3744ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
3745432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
37467e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
374745ca1b86SEd Tanous             [&app](const crow::Request& req,
374822d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
374922d268cbSEd Tanous                    const std::string& systemName) {
37503ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
375145ca1b86SEd Tanous         {
375245ca1b86SEd Tanous             return;
375345ca1b86SEd Tanous         }
37547f3e84a1SEd Tanous         if constexpr (bmcwebEnableMultiHost)
37557f3e84a1SEd Tanous         {
37567f3e84a1SEd Tanous             // Option currently returns no systems.  TBD
37577f3e84a1SEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
37587f3e84a1SEd Tanous                                        systemName);
37597f3e84a1SEd Tanous             return;
37607f3e84a1SEd Tanous         }
376122d268cbSEd Tanous         if (systemName != "system")
376222d268cbSEd Tanous         {
376322d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
376422d268cbSEd Tanous                                        systemName);
376522d268cbSEd Tanous             return;
376622d268cbSEd Tanous         }
3767*62598e31SEd Tanous         BMCWEB_LOG_DEBUG("Do delete all postcodes entries.");
3768a3316fc6SZhikuiRen 
3769a3316fc6SZhikuiRen         // Make call to post-code service to request clear all
3770a3316fc6SZhikuiRen         crow::connections::systemBus->async_method_call(
37715e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec) {
3772a3316fc6SZhikuiRen             if (ec)
3773a3316fc6SZhikuiRen             {
3774a3316fc6SZhikuiRen                 // TODO Handle for specific error code
3775*62598e31SEd Tanous                 BMCWEB_LOG_ERROR("doClearPostCodes resp_handler got error {}",
3776*62598e31SEd Tanous                                  ec);
3777002d39b4SEd Tanous                 asyncResp->res.result(
3778002d39b4SEd Tanous                     boost::beast::http::status::internal_server_error);
3779a3316fc6SZhikuiRen                 messages::internalError(asyncResp->res);
3780a3316fc6SZhikuiRen                 return;
3781a3316fc6SZhikuiRen             }
3782a3316fc6SZhikuiRen             },
378315124765SJonathan Doman             "xyz.openbmc_project.State.Boot.PostCode0",
378415124765SJonathan Doman             "/xyz/openbmc_project/State/Boot/PostCode0",
3785a3316fc6SZhikuiRen             "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
37867e860f15SJohn Edward Broadbent         });
3787a3316fc6SZhikuiRen }
3788a3316fc6SZhikuiRen 
37896f284d24SJiaqing Zhao /**
37906f284d24SJiaqing Zhao  * @brief Parse post code ID and get the current value and index value
37916f284d24SJiaqing Zhao  *        eg: postCodeID=B1-2, currentValue=1, index=2
37926f284d24SJiaqing Zhao  *
37936f284d24SJiaqing Zhao  * @param[in]  postCodeID     Post Code ID
37946f284d24SJiaqing Zhao  * @param[out] currentValue   Current value
37956f284d24SJiaqing Zhao  * @param[out] index          Index value
37966f284d24SJiaqing Zhao  *
37976f284d24SJiaqing Zhao  * @return bool true if the parsing is successful, false the parsing fails
37986f284d24SJiaqing Zhao  */
37996f284d24SJiaqing Zhao inline static bool parsePostCode(const std::string& postCodeID,
38006f284d24SJiaqing Zhao                                  uint64_t& currentValue, uint16_t& index)
38016f284d24SJiaqing Zhao {
38026f284d24SJiaqing Zhao     std::vector<std::string> split;
380350ebd4afSEd Tanous     bmcweb::split(split, postCodeID, '-');
38046f284d24SJiaqing Zhao     if (split.size() != 2 || split[0].length() < 2 || split[0].front() != 'B')
38056f284d24SJiaqing Zhao     {
38066f284d24SJiaqing Zhao         return false;
38076f284d24SJiaqing Zhao     }
38086f284d24SJiaqing Zhao 
380984396af9SPatrick Williams     auto start = std::next(split[0].begin());
381084396af9SPatrick Williams     auto end = split[0].end();
381184396af9SPatrick Williams     auto [ptrIndex, ecIndex] = std::from_chars(&*start, &*end, index);
38126f284d24SJiaqing Zhao 
381384396af9SPatrick Williams     if (ptrIndex != &*end || ecIndex != std::errc())
38146f284d24SJiaqing Zhao     {
38156f284d24SJiaqing Zhao         return false;
38166f284d24SJiaqing Zhao     }
38176f284d24SJiaqing Zhao 
381884396af9SPatrick Williams     start = split[1].begin();
381984396af9SPatrick Williams     end = split[1].end();
38206f284d24SJiaqing Zhao 
382184396af9SPatrick Williams     auto [ptrValue, ecValue] = std::from_chars(&*start, &*end, currentValue);
38226f284d24SJiaqing Zhao 
382384396af9SPatrick Williams     return ptrValue == &*end && ecValue == std::errc();
38246f284d24SJiaqing Zhao }
38256f284d24SJiaqing Zhao 
38266f284d24SJiaqing Zhao static bool fillPostCodeEntry(
3827ac106bf6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
38286c9a279eSManojkiran Eda     const boost::container::flat_map<
38296c9a279eSManojkiran Eda         uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
3830a3316fc6SZhikuiRen     const uint16_t bootIndex, const uint64_t codeIndex = 0,
3831a3316fc6SZhikuiRen     const uint64_t skip = 0, const uint64_t top = 0)
3832a3316fc6SZhikuiRen {
3833a3316fc6SZhikuiRen     // Get the Message from the MessageRegistry
3834fffb8c1fSEd Tanous     const registries::Message* message =
3835fffb8c1fSEd Tanous         registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
3836a3316fc6SZhikuiRen 
3837a3316fc6SZhikuiRen     uint64_t currentCodeIndex = 0;
3838a3316fc6SZhikuiRen     uint64_t firstCodeTimeUs = 0;
38396c9a279eSManojkiran Eda     for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
38406c9a279eSManojkiran Eda              code : postcode)
3841a3316fc6SZhikuiRen     {
3842a3316fc6SZhikuiRen         currentCodeIndex++;
3843a3316fc6SZhikuiRen         std::string postcodeEntryID =
3844a3316fc6SZhikuiRen             "B" + std::to_string(bootIndex) + "-" +
3845a3316fc6SZhikuiRen             std::to_string(currentCodeIndex); // 1 based index in EntryID string
3846a3316fc6SZhikuiRen 
3847a3316fc6SZhikuiRen         uint64_t usecSinceEpoch = code.first;
3848a3316fc6SZhikuiRen         uint64_t usTimeOffset = 0;
3849a3316fc6SZhikuiRen 
3850a3316fc6SZhikuiRen         if (1 == currentCodeIndex)
3851a3316fc6SZhikuiRen         { // already incremented
3852a3316fc6SZhikuiRen             firstCodeTimeUs = code.first;
3853a3316fc6SZhikuiRen         }
3854a3316fc6SZhikuiRen         else
3855a3316fc6SZhikuiRen         {
3856a3316fc6SZhikuiRen             usTimeOffset = code.first - firstCodeTimeUs;
3857a3316fc6SZhikuiRen         }
3858a3316fc6SZhikuiRen 
3859a3316fc6SZhikuiRen         // skip if no specific codeIndex is specified and currentCodeIndex does
3860a3316fc6SZhikuiRen         // not fall between top and skip
3861a3316fc6SZhikuiRen         if ((codeIndex == 0) &&
3862a3316fc6SZhikuiRen             (currentCodeIndex <= skip || currentCodeIndex > top))
3863a3316fc6SZhikuiRen         {
3864a3316fc6SZhikuiRen             continue;
3865a3316fc6SZhikuiRen         }
3866a3316fc6SZhikuiRen 
38674e0453b1SGunnar Mills         // skip if a specific codeIndex is specified and does not match the
3868a3316fc6SZhikuiRen         // currentIndex
3869a3316fc6SZhikuiRen         if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
3870a3316fc6SZhikuiRen         {
3871a3316fc6SZhikuiRen             // This is done for simplicity. 1st entry is needed to calculate
3872a3316fc6SZhikuiRen             // time offset. To improve efficiency, one can get to the entry
3873a3316fc6SZhikuiRen             // directly (possibly with flatmap's nth method)
3874a3316fc6SZhikuiRen             continue;
3875a3316fc6SZhikuiRen         }
3876a3316fc6SZhikuiRen 
3877a3316fc6SZhikuiRen         // currentCodeIndex is within top and skip or equal to specified code
3878a3316fc6SZhikuiRen         // index
3879a3316fc6SZhikuiRen 
3880a3316fc6SZhikuiRen         // Get the Created time from the timestamp
3881a3316fc6SZhikuiRen         std::string entryTimeStr;
38822a025611SKonstantin Aladyshev         entryTimeStr = redfish::time_utils::getDateTimeUintUs(usecSinceEpoch);
3883a3316fc6SZhikuiRen 
3884a3316fc6SZhikuiRen         // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
3885a3316fc6SZhikuiRen         std::ostringstream hexCode;
3886a3316fc6SZhikuiRen         hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
38876c9a279eSManojkiran Eda                 << std::get<0>(code.second);
3888a3316fc6SZhikuiRen         std::ostringstream timeOffsetStr;
3889a3316fc6SZhikuiRen         // Set Fixed -Point Notation
3890a3316fc6SZhikuiRen         timeOffsetStr << std::fixed;
3891a3316fc6SZhikuiRen         // Set precision to 4 digits
3892a3316fc6SZhikuiRen         timeOffsetStr << std::setprecision(4);
3893a3316fc6SZhikuiRen         // Add double to stream
3894a3316fc6SZhikuiRen         timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
3895a3316fc6SZhikuiRen 
38961e6deaf6SEd Tanous         std::string bootIndexStr = std::to_string(bootIndex);
38971e6deaf6SEd Tanous         std::string timeOffsetString = timeOffsetStr.str();
38981e6deaf6SEd Tanous         std::string hexCodeStr = hexCode.str();
3899a3316fc6SZhikuiRen 
39001e6deaf6SEd Tanous         std::array<std::string_view, 3> messageArgs = {
39011e6deaf6SEd Tanous             bootIndexStr, timeOffsetString, hexCodeStr};
39021e6deaf6SEd Tanous 
39031e6deaf6SEd Tanous         std::string msg =
39041e6deaf6SEd Tanous             redfish::registries::fillMessageArgs(messageArgs, message->message);
39051e6deaf6SEd Tanous         if (msg.empty())
3906a3316fc6SZhikuiRen         {
39071e6deaf6SEd Tanous             messages::internalError(asyncResp->res);
39081e6deaf6SEd Tanous             return false;
3909a3316fc6SZhikuiRen         }
3910a3316fc6SZhikuiRen 
3911d4342a92STim Lee         // Get Severity template from message registry
3912d4342a92STim Lee         std::string severity;
3913d4342a92STim Lee         if (message != nullptr)
3914d4342a92STim Lee         {
39155f2b84eeSEd Tanous             severity = message->messageSeverity;
3916d4342a92STim Lee         }
3917d4342a92STim Lee 
39186f284d24SJiaqing Zhao         // Format entry
39196f284d24SJiaqing Zhao         nlohmann::json::object_t bmcLogEntry;
39209c11a172SVijay Lobo         bmcLogEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
3921ef4c65b7SEd Tanous         bmcLogEntry["@odata.id"] = boost::urls::format(
3922ef4c65b7SEd Tanous             "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/{}",
3923ef4c65b7SEd Tanous             postcodeEntryID);
392484afc48bSJason M. Bills         bmcLogEntry["Name"] = "POST Code Log Entry";
392584afc48bSJason M. Bills         bmcLogEntry["Id"] = postcodeEntryID;
392684afc48bSJason M. Bills         bmcLogEntry["Message"] = std::move(msg);
392784afc48bSJason M. Bills         bmcLogEntry["MessageId"] = "OpenBMC.0.2.BIOSPOSTCode";
39281e6deaf6SEd Tanous         bmcLogEntry["MessageArgs"] = messageArgs;
392984afc48bSJason M. Bills         bmcLogEntry["EntryType"] = "Event";
393084afc48bSJason M. Bills         bmcLogEntry["Severity"] = std::move(severity);
393184afc48bSJason M. Bills         bmcLogEntry["Created"] = entryTimeStr;
3932647b3cdcSGeorge Liu         if (!std::get<std::vector<uint8_t>>(code.second).empty())
3933647b3cdcSGeorge Liu         {
3934647b3cdcSGeorge Liu             bmcLogEntry["AdditionalDataURI"] =
3935647b3cdcSGeorge Liu                 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3936647b3cdcSGeorge Liu                 postcodeEntryID + "/attachment";
3937647b3cdcSGeorge Liu         }
39386f284d24SJiaqing Zhao 
39396f284d24SJiaqing Zhao         // codeIndex is only specified when querying single entry, return only
39406f284d24SJiaqing Zhao         // that entry in this case
39416f284d24SJiaqing Zhao         if (codeIndex != 0)
39426f284d24SJiaqing Zhao         {
3943ac106bf6SEd Tanous             asyncResp->res.jsonValue.update(bmcLogEntry);
39446f284d24SJiaqing Zhao             return true;
3945a3316fc6SZhikuiRen         }
39466f284d24SJiaqing Zhao 
3947ac106bf6SEd Tanous         nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
3948b2ba3072SPatrick Williams         logEntryArray.emplace_back(std::move(bmcLogEntry));
39496f284d24SJiaqing Zhao     }
39506f284d24SJiaqing Zhao 
39516f284d24SJiaqing Zhao     // Return value is always false when querying multiple entries
39526f284d24SJiaqing Zhao     return false;
3953a3316fc6SZhikuiRen }
3954a3316fc6SZhikuiRen 
3955ac106bf6SEd Tanous static void
3956ac106bf6SEd Tanous     getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
39576f284d24SJiaqing Zhao                         const std::string& entryId)
3958a3316fc6SZhikuiRen {
39596f284d24SJiaqing Zhao     uint16_t bootIndex = 0;
39606f284d24SJiaqing Zhao     uint64_t codeIndex = 0;
39616f284d24SJiaqing Zhao     if (!parsePostCode(entryId, codeIndex, bootIndex))
39626f284d24SJiaqing Zhao     {
39636f284d24SJiaqing Zhao         // Requested ID was not found
3964ac106bf6SEd Tanous         messages::resourceNotFound(asyncResp->res, "LogEntry", entryId);
39656f284d24SJiaqing Zhao         return;
39666f284d24SJiaqing Zhao     }
39676f284d24SJiaqing Zhao 
39686f284d24SJiaqing Zhao     if (bootIndex == 0 || codeIndex == 0)
39696f284d24SJiaqing Zhao     {
39706f284d24SJiaqing Zhao         // 0 is an invalid index
3971ac106bf6SEd Tanous         messages::resourceNotFound(asyncResp->res, "LogEntry", entryId);
39726f284d24SJiaqing Zhao         return;
39736f284d24SJiaqing Zhao     }
39746f284d24SJiaqing Zhao 
3975a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
3976ac106bf6SEd Tanous         [asyncResp, entryId, bootIndex,
39775e7e2dc5SEd Tanous          codeIndex](const boost::system::error_code& ec,
39786c9a279eSManojkiran Eda                     const boost::container::flat_map<
39796c9a279eSManojkiran Eda                         uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
39806c9a279eSManojkiran Eda                         postcode) {
3981a3316fc6SZhikuiRen         if (ec)
3982a3316fc6SZhikuiRen         {
3983*62598e31SEd Tanous             BMCWEB_LOG_DEBUG("DBUS POST CODE PostCode response error");
3984ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
3985a3316fc6SZhikuiRen             return;
3986a3316fc6SZhikuiRen         }
3987a3316fc6SZhikuiRen 
3988a3316fc6SZhikuiRen         if (postcode.empty())
3989a3316fc6SZhikuiRen         {
3990ac106bf6SEd Tanous             messages::resourceNotFound(asyncResp->res, "LogEntry", entryId);
3991a3316fc6SZhikuiRen             return;
3992a3316fc6SZhikuiRen         }
3993a3316fc6SZhikuiRen 
3994ac106bf6SEd Tanous         if (!fillPostCodeEntry(asyncResp, postcode, bootIndex, codeIndex))
39956f284d24SJiaqing Zhao         {
3996ac106bf6SEd Tanous             messages::resourceNotFound(asyncResp->res, "LogEntry", entryId);
39976f284d24SJiaqing Zhao             return;
39986f284d24SJiaqing Zhao         }
3999a3316fc6SZhikuiRen         },
400015124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
400115124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
4002a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
4003a3316fc6SZhikuiRen         bootIndex);
4004a3316fc6SZhikuiRen }
4005a3316fc6SZhikuiRen 
4006ac106bf6SEd Tanous static void
4007ac106bf6SEd Tanous     getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4008ac106bf6SEd Tanous                        const uint16_t bootIndex, const uint16_t bootCount,
4009ac106bf6SEd Tanous                        const uint64_t entryCount, size_t skip, size_t top)
4010a3316fc6SZhikuiRen {
4011a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
4012ac106bf6SEd Tanous         [asyncResp, bootIndex, bootCount, entryCount, skip,
40135e7e2dc5SEd Tanous          top](const boost::system::error_code& ec,
40146c9a279eSManojkiran Eda               const boost::container::flat_map<
40156c9a279eSManojkiran Eda                   uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
40166c9a279eSManojkiran Eda                   postcode) {
4017a3316fc6SZhikuiRen         if (ec)
4018a3316fc6SZhikuiRen         {
4019*62598e31SEd Tanous             BMCWEB_LOG_DEBUG("DBUS POST CODE PostCode response error");
4020ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
4021a3316fc6SZhikuiRen             return;
4022a3316fc6SZhikuiRen         }
4023a3316fc6SZhikuiRen 
4024a3316fc6SZhikuiRen         uint64_t endCount = entryCount;
4025a3316fc6SZhikuiRen         if (!postcode.empty())
4026a3316fc6SZhikuiRen         {
4027a3316fc6SZhikuiRen             endCount = entryCount + postcode.size();
40283648c8beSEd Tanous             if (skip < endCount && (top + skip) > entryCount)
4029a3316fc6SZhikuiRen             {
403089492a15SPatrick Williams                 uint64_t thisBootSkip = std::max(static_cast<uint64_t>(skip),
403189492a15SPatrick Williams                                                  entryCount) -
40323648c8beSEd Tanous                                         entryCount;
4033a3316fc6SZhikuiRen                 uint64_t thisBootTop =
40343648c8beSEd Tanous                     std::min(static_cast<uint64_t>(top + skip), endCount) -
40353648c8beSEd Tanous                     entryCount;
4036a3316fc6SZhikuiRen 
4037ac106bf6SEd Tanous                 fillPostCodeEntry(asyncResp, postcode, bootIndex, 0,
4038ac106bf6SEd Tanous                                   thisBootSkip, thisBootTop);
4039a3316fc6SZhikuiRen             }
4040ac106bf6SEd Tanous             asyncResp->res.jsonValue["Members@odata.count"] = endCount;
4041a3316fc6SZhikuiRen         }
4042a3316fc6SZhikuiRen 
4043a3316fc6SZhikuiRen         // continue to previous bootIndex
4044a3316fc6SZhikuiRen         if (bootIndex < bootCount)
4045a3316fc6SZhikuiRen         {
4046ac106bf6SEd Tanous             getPostCodeForBoot(asyncResp, static_cast<uint16_t>(bootIndex + 1),
4047a3316fc6SZhikuiRen                                bootCount, endCount, skip, top);
4048a3316fc6SZhikuiRen         }
404981584abeSJiaqing Zhao         else if (skip + top < endCount)
4050a3316fc6SZhikuiRen         {
4051ac106bf6SEd Tanous             asyncResp->res.jsonValue["Members@odata.nextLink"] =
40520fda0f12SGeorge Liu                 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries?$skip=" +
4053a3316fc6SZhikuiRen                 std::to_string(skip + top);
4054a3316fc6SZhikuiRen         }
4055a3316fc6SZhikuiRen         },
405615124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
405715124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
4058a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
4059a3316fc6SZhikuiRen         bootIndex);
4060a3316fc6SZhikuiRen }
4061a3316fc6SZhikuiRen 
40628d1b46d7Szhanghch05 static void
4063ac106bf6SEd Tanous     getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
40643648c8beSEd Tanous                          size_t skip, size_t top)
4065a3316fc6SZhikuiRen {
4066a3316fc6SZhikuiRen     uint64_t entryCount = 0;
40671e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint16_t>(
40681e1e598dSJonathan Doman         *crow::connections::systemBus,
40691e1e598dSJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
40701e1e598dSJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
40711e1e598dSJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount",
4072ac106bf6SEd Tanous         [asyncResp, entryCount, skip, top](const boost::system::error_code& ec,
40731e1e598dSJonathan Doman                                            const uint16_t bootCount) {
4074a3316fc6SZhikuiRen         if (ec)
4075a3316fc6SZhikuiRen         {
4076*62598e31SEd Tanous             BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
4077ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
4078a3316fc6SZhikuiRen             return;
4079a3316fc6SZhikuiRen         }
4080ac106bf6SEd Tanous         getPostCodeForBoot(asyncResp, 1, bootCount, entryCount, skip, top);
40811e1e598dSJonathan Doman         });
4082a3316fc6SZhikuiRen }
4083a3316fc6SZhikuiRen 
40847e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesEntryCollection(App& app)
4085a3316fc6SZhikuiRen {
40867e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
408722d268cbSEd Tanous                  "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/")
4088ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
40897e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
409045ca1b86SEd Tanous             [&app](const crow::Request& req,
409122d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
409222d268cbSEd Tanous                    const std::string& systemName) {
4093c937d2bfSEd Tanous         query_param::QueryCapabilities capabilities = {
4094c937d2bfSEd Tanous             .canDelegateTop = true,
4095c937d2bfSEd Tanous             .canDelegateSkip = true,
4096c937d2bfSEd Tanous         };
4097c937d2bfSEd Tanous         query_param::Query delegatedQuery;
4098c937d2bfSEd Tanous         if (!redfish::setUpRedfishRouteWithDelegation(
40993ba00073SCarson Labrado                 app, req, asyncResp, delegatedQuery, capabilities))
410045ca1b86SEd Tanous         {
410145ca1b86SEd Tanous             return;
410245ca1b86SEd Tanous         }
41037f3e84a1SEd Tanous         if constexpr (bmcwebEnableMultiHost)
41047f3e84a1SEd Tanous         {
41057f3e84a1SEd Tanous             // Option currently returns no systems.  TBD
41067f3e84a1SEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
41077f3e84a1SEd Tanous                                        systemName);
41087f3e84a1SEd Tanous             return;
41097f3e84a1SEd Tanous         }
411022d268cbSEd Tanous 
411122d268cbSEd Tanous         if (systemName != "system")
411222d268cbSEd Tanous         {
411322d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
411422d268cbSEd Tanous                                        systemName);
411522d268cbSEd Tanous             return;
411622d268cbSEd Tanous         }
4117a3316fc6SZhikuiRen         asyncResp->res.jsonValue["@odata.type"] =
4118a3316fc6SZhikuiRen             "#LogEntryCollection.LogEntryCollection";
4119a3316fc6SZhikuiRen         asyncResp->res.jsonValue["@odata.id"] =
4120a3316fc6SZhikuiRen             "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
4121a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
4122a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Description"] =
4123a3316fc6SZhikuiRen             "Collection of POST Code Log Entries";
4124a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
4125a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Members@odata.count"] = 0;
41263648c8beSEd Tanous         size_t skip = delegatedQuery.skip.value_or(0);
41275143f7a5SJiaqing Zhao         size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
41283648c8beSEd Tanous         getCurrentBootNumber(asyncResp, skip, top);
41297e860f15SJohn Edward Broadbent         });
4130a3316fc6SZhikuiRen }
4131a3316fc6SZhikuiRen 
4132647b3cdcSGeorge Liu inline void requestRoutesPostCodesEntryAdditionalData(App& app)
4133647b3cdcSGeorge Liu {
41340fda0f12SGeorge Liu     BMCWEB_ROUTE(
41350fda0f12SGeorge Liu         app,
413622d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/attachment/")
4137647b3cdcSGeorge Liu         .privileges(redfish::privileges::getLogEntry)
4138647b3cdcSGeorge Liu         .methods(boost::beast::http::verb::get)(
413945ca1b86SEd Tanous             [&app](const crow::Request& req,
4140647b3cdcSGeorge Liu                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
414122d268cbSEd Tanous                    const std::string& systemName,
4142647b3cdcSGeorge Liu                    const std::string& postCodeID) {
41433ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
414445ca1b86SEd Tanous         {
414545ca1b86SEd Tanous             return;
414645ca1b86SEd Tanous         }
414772e21377SMatt Spinler         if (!http_helpers::isContentTypeAllowed(
414899351cd8SEd Tanous                 req.getHeaderValue("Accept"),
41494a0e1a0cSEd Tanous                 http_helpers::ContentType::OctetStream, true))
4150647b3cdcSGeorge Liu         {
4151002d39b4SEd Tanous             asyncResp->res.result(boost::beast::http::status::bad_request);
4152647b3cdcSGeorge Liu             return;
4153647b3cdcSGeorge Liu         }
41547f3e84a1SEd Tanous         if constexpr (bmcwebEnableMultiHost)
41557f3e84a1SEd Tanous         {
41567f3e84a1SEd Tanous             // Option currently returns no systems.  TBD
41577f3e84a1SEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
41587f3e84a1SEd Tanous                                        systemName);
41597f3e84a1SEd Tanous             return;
41607f3e84a1SEd Tanous         }
416122d268cbSEd Tanous         if (systemName != "system")
416222d268cbSEd Tanous         {
416322d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
416422d268cbSEd Tanous                                        systemName);
416522d268cbSEd Tanous             return;
416622d268cbSEd Tanous         }
4167647b3cdcSGeorge Liu 
4168647b3cdcSGeorge Liu         uint64_t currentValue = 0;
4169647b3cdcSGeorge Liu         uint16_t index = 0;
4170647b3cdcSGeorge Liu         if (!parsePostCode(postCodeID, currentValue, index))
4171647b3cdcSGeorge Liu         {
4172002d39b4SEd Tanous             messages::resourceNotFound(asyncResp->res, "LogEntry", postCodeID);
4173647b3cdcSGeorge Liu             return;
4174647b3cdcSGeorge Liu         }
4175647b3cdcSGeorge Liu 
4176647b3cdcSGeorge Liu         crow::connections::systemBus->async_method_call(
4177647b3cdcSGeorge Liu             [asyncResp, postCodeID, currentValue](
41785e7e2dc5SEd Tanous                 const boost::system::error_code& ec,
4179002d39b4SEd Tanous                 const std::vector<std::tuple<uint64_t, std::vector<uint8_t>>>&
4180002d39b4SEd Tanous                     postcodes) {
4181647b3cdcSGeorge Liu             if (ec.value() == EBADR)
4182647b3cdcSGeorge Liu             {
4183002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry",
4184002d39b4SEd Tanous                                            postCodeID);
4185647b3cdcSGeorge Liu                 return;
4186647b3cdcSGeorge Liu             }
4187647b3cdcSGeorge Liu             if (ec)
4188647b3cdcSGeorge Liu             {
4189*62598e31SEd Tanous                 BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
4190647b3cdcSGeorge Liu                 messages::internalError(asyncResp->res);
4191647b3cdcSGeorge Liu                 return;
4192647b3cdcSGeorge Liu             }
4193647b3cdcSGeorge Liu 
4194647b3cdcSGeorge Liu             size_t value = static_cast<size_t>(currentValue) - 1;
4195002d39b4SEd Tanous             if (value == std::string::npos || postcodes.size() < currentValue)
4196647b3cdcSGeorge Liu             {
4197*62598e31SEd Tanous                 BMCWEB_LOG_WARNING("Wrong currentValue value");
4198002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry",
4199002d39b4SEd Tanous                                            postCodeID);
4200647b3cdcSGeorge Liu                 return;
4201647b3cdcSGeorge Liu             }
4202647b3cdcSGeorge Liu 
42039eb808c1SEd Tanous             const auto& [tID, c] = postcodes[value];
420446ff87baSEd Tanous             if (c.empty())
4205647b3cdcSGeorge Liu             {
4206*62598e31SEd Tanous                 BMCWEB_LOG_WARNING("No found post code data");
4207002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry",
4208002d39b4SEd Tanous                                            postCodeID);
4209647b3cdcSGeorge Liu                 return;
4210647b3cdcSGeorge Liu             }
421146ff87baSEd Tanous             // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
421246ff87baSEd Tanous             const char* d = reinterpret_cast<const char*>(c.data());
421346ff87baSEd Tanous             std::string_view strData(d, c.size());
4214647b3cdcSGeorge Liu 
4215d9f6c621SEd Tanous             asyncResp->res.addHeader(boost::beast::http::field::content_type,
4216647b3cdcSGeorge Liu                                      "application/octet-stream");
4217d9f6c621SEd Tanous             asyncResp->res.addHeader(
4218d9f6c621SEd Tanous                 boost::beast::http::field::content_transfer_encoding, "Base64");
4219002d39b4SEd Tanous             asyncResp->res.body() = crow::utility::base64encode(strData);
4220647b3cdcSGeorge Liu             },
4221647b3cdcSGeorge Liu             "xyz.openbmc_project.State.Boot.PostCode0",
4222647b3cdcSGeorge Liu             "/xyz/openbmc_project/State/Boot/PostCode0",
4223002d39b4SEd Tanous             "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes", index);
4224647b3cdcSGeorge Liu         });
4225647b3cdcSGeorge Liu }
4226647b3cdcSGeorge Liu 
42277e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesEntry(App& app)
4228a3316fc6SZhikuiRen {
42297e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
423022d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/")
4231ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
42327e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
423345ca1b86SEd Tanous             [&app](const crow::Request& req,
42347e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
423522d268cbSEd Tanous                    const std::string& systemName, const std::string& targetID) {
42363ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
423745ca1b86SEd Tanous         {
423845ca1b86SEd Tanous             return;
423945ca1b86SEd Tanous         }
42407f3e84a1SEd Tanous         if constexpr (bmcwebEnableMultiHost)
42417f3e84a1SEd Tanous         {
42427f3e84a1SEd Tanous             // Option currently returns no systems.  TBD
42437f3e84a1SEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
42447f3e84a1SEd Tanous                                        systemName);
42457f3e84a1SEd Tanous             return;
42467f3e84a1SEd Tanous         }
424722d268cbSEd Tanous         if (systemName != "system")
424822d268cbSEd Tanous         {
424922d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
425022d268cbSEd Tanous                                        systemName);
425122d268cbSEd Tanous             return;
425222d268cbSEd Tanous         }
425322d268cbSEd Tanous 
42546f284d24SJiaqing Zhao         getPostCodeForEntry(asyncResp, targetID);
42557e860f15SJohn Edward Broadbent         });
4256a3316fc6SZhikuiRen }
4257a3316fc6SZhikuiRen 
42581da66f75SEd Tanous } // namespace redfish
4259