xref: /openbmc/bmcweb/features/redfish/lib/log_services.hpp (revision 400fd1fb4020028ad58b1cfd965f26d21f63b8c8)
11da66f75SEd Tanous /*
21da66f75SEd Tanous // Copyright (c) 2018 Intel Corporation
31da66f75SEd Tanous //
41da66f75SEd Tanous // Licensed under the Apache License, Version 2.0 (the "License");
51da66f75SEd Tanous // you may not use this file except in compliance with the License.
61da66f75SEd Tanous // You may obtain a copy of the License at
71da66f75SEd Tanous //
81da66f75SEd Tanous //      http://www.apache.org/licenses/LICENSE-2.0
91da66f75SEd Tanous //
101da66f75SEd Tanous // Unless required by applicable law or agreed to in writing, software
111da66f75SEd Tanous // distributed under the License is distributed on an "AS IS" BASIS,
121da66f75SEd Tanous // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131da66f75SEd Tanous // See the License for the specific language governing permissions and
141da66f75SEd Tanous // limitations under the License.
151da66f75SEd Tanous */
161da66f75SEd Tanous #pragma once
171da66f75SEd Tanous 
181da66f75SEd Tanous #include "node.hpp"
194851d45dSJason M. Bills #include "registries.hpp"
204851d45dSJason M. Bills #include "registries/base_message_registry.hpp"
214851d45dSJason M. Bills #include "registries/openbmc_message_registry.hpp"
2246229577SJames Feist #include "task.hpp"
231da66f75SEd Tanous 
24e1f26343SJason M. Bills #include <systemd/sd-journal.h>
25*400fd1fbSAdriana Kobylak #include <unistd.h>
26e1f26343SJason M. Bills 
27*400fd1fbSAdriana Kobylak #include <boost/algorithm/string/replace.hpp>
284851d45dSJason M. Bills #include <boost/algorithm/string/split.hpp>
294851d45dSJason M. Bills #include <boost/beast/core/span.hpp>
30*400fd1fbSAdriana Kobylak #include <boost/beast/http.hpp>
311da66f75SEd Tanous #include <boost/container/flat_map.hpp>
321ddcf01aSJason M. Bills #include <boost/system/linux_error.hpp>
33cb92c03bSAndrew Geissler #include <error_messages.hpp>
341214b7e7SGunnar Mills 
354418c7f0SJames Feist #include <filesystem>
3675710de2SXiaochao Ma #include <optional>
37cd225da8SJason M. Bills #include <string_view>
38abf2add6SEd Tanous #include <variant>
391da66f75SEd Tanous 
401da66f75SEd Tanous namespace redfish
411da66f75SEd Tanous {
421da66f75SEd Tanous 
435b61b5e8SJason M. Bills constexpr char const* crashdumpObject = "com.intel.crashdump";
445b61b5e8SJason M. Bills constexpr char const* crashdumpPath = "/com/intel/crashdump";
455b61b5e8SJason M. Bills constexpr char const* crashdumpInterface = "com.intel.crashdump";
465b61b5e8SJason M. Bills constexpr char const* deleteAllInterface =
475b61b5e8SJason M. Bills     "xyz.openbmc_project.Collection.DeleteAll";
485b61b5e8SJason M. Bills constexpr char const* crashdumpOnDemandInterface =
49424c4176SJason M. Bills     "com.intel.crashdump.OnDemand";
505b61b5e8SJason M. Bills constexpr char const* crashdumpRawPECIInterface =
51424c4176SJason M. Bills     "com.intel.crashdump.SendRawPeci";
526eda7685SKenny L. Ku constexpr char const* crashdumpTelemetryInterface =
536eda7685SKenny L. Ku     "com.intel.crashdump.Telemetry";
541da66f75SEd Tanous 
554851d45dSJason M. Bills namespace message_registries
564851d45dSJason M. Bills {
574851d45dSJason M. Bills static const Message* getMessageFromRegistry(
584851d45dSJason M. Bills     const std::string& messageKey,
594851d45dSJason M. Bills     const boost::beast::span<const MessageEntry> registry)
604851d45dSJason M. Bills {
614851d45dSJason M. Bills     boost::beast::span<const MessageEntry>::const_iterator messageIt =
624851d45dSJason M. Bills         std::find_if(registry.cbegin(), registry.cend(),
634851d45dSJason M. Bills                      [&messageKey](const MessageEntry& messageEntry) {
644851d45dSJason M. Bills                          return !std::strcmp(messageEntry.first,
654851d45dSJason M. Bills                                              messageKey.c_str());
664851d45dSJason M. Bills                      });
674851d45dSJason M. Bills     if (messageIt != registry.cend())
684851d45dSJason M. Bills     {
694851d45dSJason M. Bills         return &messageIt->second;
704851d45dSJason M. Bills     }
714851d45dSJason M. Bills 
724851d45dSJason M. Bills     return nullptr;
734851d45dSJason M. Bills }
744851d45dSJason M. Bills 
754851d45dSJason M. Bills static const Message* getMessage(const std::string_view& messageID)
764851d45dSJason M. Bills {
774851d45dSJason M. Bills     // Redfish MessageIds are in the form
784851d45dSJason M. Bills     // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find
794851d45dSJason M. Bills     // the right Message
804851d45dSJason M. Bills     std::vector<std::string> fields;
814851d45dSJason M. Bills     fields.reserve(4);
824851d45dSJason M. Bills     boost::split(fields, messageID, boost::is_any_of("."));
834851d45dSJason M. Bills     std::string& registryName = fields[0];
844851d45dSJason M. Bills     std::string& messageKey = fields[3];
854851d45dSJason M. Bills 
864851d45dSJason M. Bills     // Find the right registry and check it for the MessageKey
874851d45dSJason M. Bills     if (std::string(base::header.registryPrefix) == registryName)
884851d45dSJason M. Bills     {
894851d45dSJason M. Bills         return getMessageFromRegistry(
904851d45dSJason M. Bills             messageKey, boost::beast::span<const MessageEntry>(base::registry));
914851d45dSJason M. Bills     }
924851d45dSJason M. Bills     if (std::string(openbmc::header.registryPrefix) == registryName)
934851d45dSJason M. Bills     {
944851d45dSJason M. Bills         return getMessageFromRegistry(
954851d45dSJason M. Bills             messageKey,
964851d45dSJason M. Bills             boost::beast::span<const MessageEntry>(openbmc::registry));
974851d45dSJason M. Bills     }
984851d45dSJason M. Bills     return nullptr;
994851d45dSJason M. Bills }
1004851d45dSJason M. Bills } // namespace message_registries
1014851d45dSJason M. Bills 
102f6150403SJames Feist namespace fs = std::filesystem;
1031da66f75SEd Tanous 
104cb92c03bSAndrew Geissler using GetManagedPropertyType = boost::container::flat_map<
10519bd78d9SPatrick Williams     std::string, std::variant<std::string, bool, uint8_t, int16_t, uint16_t,
106cb92c03bSAndrew Geissler                               int32_t, uint32_t, int64_t, uint64_t, double>>;
107cb92c03bSAndrew Geissler 
108cb92c03bSAndrew Geissler using GetManagedObjectsType = boost::container::flat_map<
109cb92c03bSAndrew Geissler     sdbusplus::message::object_path,
110cb92c03bSAndrew Geissler     boost::container::flat_map<std::string, GetManagedPropertyType>>;
111cb92c03bSAndrew Geissler 
112cb92c03bSAndrew Geissler inline std::string translateSeverityDbusToRedfish(const std::string& s)
113cb92c03bSAndrew Geissler {
114d4d25793SEd Tanous     if ((s == "xyz.openbmc_project.Logging.Entry.Level.Alert") ||
115d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Critical") ||
116d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Emergency") ||
117d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Error"))
118cb92c03bSAndrew Geissler     {
119cb92c03bSAndrew Geissler         return "Critical";
120cb92c03bSAndrew Geissler     }
1213174e4dfSEd Tanous     if ((s == "xyz.openbmc_project.Logging.Entry.Level.Debug") ||
122d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Informational") ||
123d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Notice"))
124cb92c03bSAndrew Geissler     {
125cb92c03bSAndrew Geissler         return "OK";
126cb92c03bSAndrew Geissler     }
1273174e4dfSEd Tanous     if (s == "xyz.openbmc_project.Logging.Entry.Level.Warning")
128cb92c03bSAndrew Geissler     {
129cb92c03bSAndrew Geissler         return "Warning";
130cb92c03bSAndrew Geissler     }
131cb92c03bSAndrew Geissler     return "";
132cb92c03bSAndrew Geissler }
133cb92c03bSAndrew Geissler 
13416428a1aSJason M. Bills static int getJournalMetadata(sd_journal* journal,
13539e77504SEd Tanous                               const std::string_view& field,
13639e77504SEd Tanous                               std::string_view& contents)
13716428a1aSJason M. Bills {
13816428a1aSJason M. Bills     const char* data = nullptr;
13916428a1aSJason M. Bills     size_t length = 0;
14016428a1aSJason M. Bills     int ret = 0;
14116428a1aSJason M. Bills     // Get the metadata from the requested field of the journal entry
142271584abSEd Tanous     ret = sd_journal_get_data(journal, field.data(),
143271584abSEd Tanous                               reinterpret_cast<const void**>(&data), &length);
14416428a1aSJason M. Bills     if (ret < 0)
14516428a1aSJason M. Bills     {
14616428a1aSJason M. Bills         return ret;
14716428a1aSJason M. Bills     }
14839e77504SEd Tanous     contents = std::string_view(data, length);
14916428a1aSJason M. Bills     // Only use the content after the "=" character.
15081ce609eSEd Tanous     contents.remove_prefix(std::min(contents.find('=') + 1, contents.size()));
15116428a1aSJason M. Bills     return ret;
15216428a1aSJason M. Bills }
15316428a1aSJason M. Bills 
15416428a1aSJason M. Bills static int getJournalMetadata(sd_journal* journal,
15539e77504SEd Tanous                               const std::string_view& field, const int& base,
156271584abSEd Tanous                               long int& contents)
15716428a1aSJason M. Bills {
15816428a1aSJason M. Bills     int ret = 0;
15939e77504SEd Tanous     std::string_view metadata;
16016428a1aSJason M. Bills     // Get the metadata from the requested field of the journal entry
16116428a1aSJason M. Bills     ret = getJournalMetadata(journal, field, metadata);
16216428a1aSJason M. Bills     if (ret < 0)
16316428a1aSJason M. Bills     {
16416428a1aSJason M. Bills         return ret;
16516428a1aSJason M. Bills     }
166b01bf299SEd Tanous     contents = strtol(metadata.data(), nullptr, base);
16716428a1aSJason M. Bills     return ret;
16816428a1aSJason M. Bills }
16916428a1aSJason M. Bills 
170a3316fc6SZhikuiRen static bool getEntryTimestamp(sd_journal* journal, std::string& entryTimestamp)
171a3316fc6SZhikuiRen {
172a3316fc6SZhikuiRen     int ret = 0;
173a3316fc6SZhikuiRen     uint64_t timestamp = 0;
174a3316fc6SZhikuiRen     ret = sd_journal_get_realtime_usec(journal, &timestamp);
175a3316fc6SZhikuiRen     if (ret < 0)
176a3316fc6SZhikuiRen     {
177a3316fc6SZhikuiRen         BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
178a3316fc6SZhikuiRen                          << strerror(-ret);
179a3316fc6SZhikuiRen         return false;
180a3316fc6SZhikuiRen     }
1819c620e21SAsmitha Karunanithi     entryTimestamp = crow::utility::getDateTime(
1829c620e21SAsmitha Karunanithi         static_cast<std::time_t>(timestamp / 1000 / 1000));
1839c620e21SAsmitha Karunanithi     return true;
184a3316fc6SZhikuiRen }
185a3316fc6SZhikuiRen 
18616428a1aSJason M. Bills static bool getSkipParam(crow::Response& res, const crow::Request& req,
187271584abSEd Tanous                          uint64_t& skip)
18816428a1aSJason M. Bills {
1895a7e877eSJames Feist     boost::urls::url_view::params_type::iterator it =
1905a7e877eSJames Feist         req.urlParams.find("$skip");
1915a7e877eSJames Feist     if (it != req.urlParams.end())
19216428a1aSJason M. Bills     {
1935a7e877eSJames Feist         std::string skipParam = it->value();
19416428a1aSJason M. Bills         char* ptr = nullptr;
1955a7e877eSJames Feist         skip = std::strtoul(skipParam.c_str(), &ptr, 10);
1965a7e877eSJames Feist         if (skipParam.empty() || *ptr != '\0')
19716428a1aSJason M. Bills         {
19816428a1aSJason M. Bills 
19916428a1aSJason M. Bills             messages::queryParameterValueTypeError(res, std::string(skipParam),
20016428a1aSJason M. Bills                                                    "$skip");
20116428a1aSJason M. Bills             return false;
20216428a1aSJason M. Bills         }
20316428a1aSJason M. Bills     }
20416428a1aSJason M. Bills     return true;
20516428a1aSJason M. Bills }
20616428a1aSJason M. Bills 
207271584abSEd Tanous static constexpr const uint64_t maxEntriesPerPage = 1000;
20816428a1aSJason M. Bills static bool getTopParam(crow::Response& res, const crow::Request& req,
209271584abSEd Tanous                         uint64_t& top)
21016428a1aSJason M. Bills {
2115a7e877eSJames Feist     boost::urls::url_view::params_type::iterator it =
2125a7e877eSJames Feist         req.urlParams.find("$top");
2135a7e877eSJames Feist     if (it != req.urlParams.end())
21416428a1aSJason M. Bills     {
2155a7e877eSJames Feist         std::string topParam = it->value();
21616428a1aSJason M. Bills         char* ptr = nullptr;
2175a7e877eSJames Feist         top = std::strtoul(topParam.c_str(), &ptr, 10);
2185a7e877eSJames Feist         if (topParam.empty() || *ptr != '\0')
21916428a1aSJason M. Bills         {
22016428a1aSJason M. Bills             messages::queryParameterValueTypeError(res, std::string(topParam),
22116428a1aSJason M. Bills                                                    "$top");
22216428a1aSJason M. Bills             return false;
22316428a1aSJason M. Bills         }
224271584abSEd Tanous         if (top < 1U || top > maxEntriesPerPage)
22516428a1aSJason M. Bills         {
22616428a1aSJason M. Bills 
22716428a1aSJason M. Bills             messages::queryParameterOutOfRange(
22816428a1aSJason M. Bills                 res, std::to_string(top), "$top",
22916428a1aSJason M. Bills                 "1-" + std::to_string(maxEntriesPerPage));
23016428a1aSJason M. Bills             return false;
23116428a1aSJason M. Bills         }
23216428a1aSJason M. Bills     }
23316428a1aSJason M. Bills     return true;
23416428a1aSJason M. Bills }
23516428a1aSJason M. Bills 
236e85d6b16SJason M. Bills static bool getUniqueEntryID(sd_journal* journal, std::string& entryID,
237e85d6b16SJason M. Bills                              const bool firstEntry = true)
23816428a1aSJason M. Bills {
23916428a1aSJason M. Bills     int ret = 0;
24016428a1aSJason M. Bills     static uint64_t prevTs = 0;
24116428a1aSJason M. Bills     static int index = 0;
242e85d6b16SJason M. Bills     if (firstEntry)
243e85d6b16SJason M. Bills     {
244e85d6b16SJason M. Bills         prevTs = 0;
245e85d6b16SJason M. Bills     }
246e85d6b16SJason M. Bills 
24716428a1aSJason M. Bills     // Get the entry timestamp
24816428a1aSJason M. Bills     uint64_t curTs = 0;
24916428a1aSJason M. Bills     ret = sd_journal_get_realtime_usec(journal, &curTs);
25016428a1aSJason M. Bills     if (ret < 0)
25116428a1aSJason M. Bills     {
25216428a1aSJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
25316428a1aSJason M. Bills                          << strerror(-ret);
25416428a1aSJason M. Bills         return false;
25516428a1aSJason M. Bills     }
25616428a1aSJason M. Bills     // If the timestamp isn't unique, increment the index
25716428a1aSJason M. Bills     if (curTs == prevTs)
25816428a1aSJason M. Bills     {
25916428a1aSJason M. Bills         index++;
26016428a1aSJason M. Bills     }
26116428a1aSJason M. Bills     else
26216428a1aSJason M. Bills     {
26316428a1aSJason M. Bills         // Otherwise, reset it
26416428a1aSJason M. Bills         index = 0;
26516428a1aSJason M. Bills     }
26616428a1aSJason M. Bills     // Save the timestamp
26716428a1aSJason M. Bills     prevTs = curTs;
26816428a1aSJason M. Bills 
26916428a1aSJason M. Bills     entryID = std::to_string(curTs);
27016428a1aSJason M. Bills     if (index > 0)
27116428a1aSJason M. Bills     {
27216428a1aSJason M. Bills         entryID += "_" + std::to_string(index);
27316428a1aSJason M. Bills     }
27416428a1aSJason M. Bills     return true;
27516428a1aSJason M. Bills }
27616428a1aSJason M. Bills 
277e85d6b16SJason M. Bills static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
278e85d6b16SJason M. Bills                              const bool firstEntry = true)
27995820184SJason M. Bills {
280271584abSEd Tanous     static time_t prevTs = 0;
28195820184SJason M. Bills     static int index = 0;
282e85d6b16SJason M. Bills     if (firstEntry)
283e85d6b16SJason M. Bills     {
284e85d6b16SJason M. Bills         prevTs = 0;
285e85d6b16SJason M. Bills     }
286e85d6b16SJason M. Bills 
28795820184SJason M. Bills     // Get the entry timestamp
288271584abSEd Tanous     std::time_t curTs = 0;
28995820184SJason M. Bills     std::tm timeStruct = {};
29095820184SJason M. Bills     std::istringstream entryStream(logEntry);
29195820184SJason M. Bills     if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
29295820184SJason M. Bills     {
29395820184SJason M. Bills         curTs = std::mktime(&timeStruct);
29495820184SJason M. Bills     }
29595820184SJason M. Bills     // If the timestamp isn't unique, increment the index
29695820184SJason M. Bills     if (curTs == prevTs)
29795820184SJason M. Bills     {
29895820184SJason M. Bills         index++;
29995820184SJason M. Bills     }
30095820184SJason M. Bills     else
30195820184SJason M. Bills     {
30295820184SJason M. Bills         // Otherwise, reset it
30395820184SJason M. Bills         index = 0;
30495820184SJason M. Bills     }
30595820184SJason M. Bills     // Save the timestamp
30695820184SJason M. Bills     prevTs = curTs;
30795820184SJason M. Bills 
30895820184SJason M. Bills     entryID = std::to_string(curTs);
30995820184SJason M. Bills     if (index > 0)
31095820184SJason M. Bills     {
31195820184SJason M. Bills         entryID += "_" + std::to_string(index);
31295820184SJason M. Bills     }
31395820184SJason M. Bills     return true;
31495820184SJason M. Bills }
31595820184SJason M. Bills 
31616428a1aSJason M. Bills static bool getTimestampFromID(crow::Response& res, const std::string& entryID,
317271584abSEd Tanous                                uint64_t& timestamp, uint64_t& index)
31816428a1aSJason M. Bills {
31916428a1aSJason M. Bills     if (entryID.empty())
32016428a1aSJason M. Bills     {
32116428a1aSJason M. Bills         return false;
32216428a1aSJason M. Bills     }
32316428a1aSJason M. Bills     // Convert the unique ID back to a timestamp to find the entry
32439e77504SEd Tanous     std::string_view tsStr(entryID);
32516428a1aSJason M. Bills 
32681ce609eSEd Tanous     auto underscorePos = tsStr.find('_');
32716428a1aSJason M. Bills     if (underscorePos != tsStr.npos)
32816428a1aSJason M. Bills     {
32916428a1aSJason M. Bills         // Timestamp has an index
33016428a1aSJason M. Bills         tsStr.remove_suffix(tsStr.size() - underscorePos);
33139e77504SEd Tanous         std::string_view indexStr(entryID);
33216428a1aSJason M. Bills         indexStr.remove_prefix(underscorePos + 1);
33316428a1aSJason M. Bills         std::size_t pos;
33416428a1aSJason M. Bills         try
33516428a1aSJason M. Bills         {
33639e77504SEd Tanous             index = std::stoul(std::string(indexStr), &pos);
33716428a1aSJason M. Bills         }
338271584abSEd Tanous         catch (std::invalid_argument&)
33916428a1aSJason M. Bills         {
34016428a1aSJason M. Bills             messages::resourceMissingAtURI(res, entryID);
34116428a1aSJason M. Bills             return false;
34216428a1aSJason M. Bills         }
343271584abSEd Tanous         catch (std::out_of_range&)
34416428a1aSJason M. Bills         {
34516428a1aSJason M. Bills             messages::resourceMissingAtURI(res, entryID);
34616428a1aSJason M. Bills             return false;
34716428a1aSJason M. Bills         }
34816428a1aSJason M. Bills         if (pos != indexStr.size())
34916428a1aSJason M. Bills         {
35016428a1aSJason M. Bills             messages::resourceMissingAtURI(res, entryID);
35116428a1aSJason M. Bills             return false;
35216428a1aSJason M. Bills         }
35316428a1aSJason M. Bills     }
35416428a1aSJason M. Bills     // Timestamp has no index
35516428a1aSJason M. Bills     std::size_t pos;
35616428a1aSJason M. Bills     try
35716428a1aSJason M. Bills     {
35839e77504SEd Tanous         timestamp = std::stoull(std::string(tsStr), &pos);
35916428a1aSJason M. Bills     }
360271584abSEd Tanous     catch (std::invalid_argument&)
36116428a1aSJason M. Bills     {
36216428a1aSJason M. Bills         messages::resourceMissingAtURI(res, entryID);
36316428a1aSJason M. Bills         return false;
36416428a1aSJason M. Bills     }
365271584abSEd Tanous     catch (std::out_of_range&)
36616428a1aSJason M. Bills     {
36716428a1aSJason M. Bills         messages::resourceMissingAtURI(res, entryID);
36816428a1aSJason M. Bills         return false;
36916428a1aSJason M. Bills     }
37016428a1aSJason M. Bills     if (pos != tsStr.size())
37116428a1aSJason M. Bills     {
37216428a1aSJason M. Bills         messages::resourceMissingAtURI(res, entryID);
37316428a1aSJason M. Bills         return false;
37416428a1aSJason M. Bills     }
37516428a1aSJason M. Bills     return true;
37616428a1aSJason M. Bills }
37716428a1aSJason M. Bills 
37895820184SJason M. Bills static bool
37995820184SJason M. Bills     getRedfishLogFiles(std::vector<std::filesystem::path>& redfishLogFiles)
38095820184SJason M. Bills {
38195820184SJason M. Bills     static const std::filesystem::path redfishLogDir = "/var/log";
38295820184SJason M. Bills     static const std::string redfishLogFilename = "redfish";
38395820184SJason M. Bills 
38495820184SJason M. Bills     // Loop through the directory looking for redfish log files
38595820184SJason M. Bills     for (const std::filesystem::directory_entry& dirEnt :
38695820184SJason M. Bills          std::filesystem::directory_iterator(redfishLogDir))
38795820184SJason M. Bills     {
38895820184SJason M. Bills         // If we find a redfish log file, save the path
38995820184SJason M. Bills         std::string filename = dirEnt.path().filename();
39095820184SJason M. Bills         if (boost::starts_with(filename, redfishLogFilename))
39195820184SJason M. Bills         {
39295820184SJason M. Bills             redfishLogFiles.emplace_back(redfishLogDir / filename);
39395820184SJason M. Bills         }
39495820184SJason M. Bills     }
39595820184SJason M. Bills     // As the log files rotate, they are appended with a ".#" that is higher for
39695820184SJason M. Bills     // the older logs. Since we don't expect more than 10 log files, we
39795820184SJason M. Bills     // can just sort the list to get them in order from newest to oldest
39895820184SJason M. Bills     std::sort(redfishLogFiles.begin(), redfishLogFiles.end());
39995820184SJason M. Bills 
40095820184SJason M. Bills     return !redfishLogFiles.empty();
40195820184SJason M. Bills }
40295820184SJason M. Bills 
4035cb1dd27SAsmitha Karunanithi inline void getDumpEntryCollection(std::shared_ptr<AsyncResp>& asyncResp,
4045cb1dd27SAsmitha Karunanithi                                    const std::string& dumpType)
4055cb1dd27SAsmitha Karunanithi {
4065cb1dd27SAsmitha Karunanithi     std::string dumpPath;
4075cb1dd27SAsmitha Karunanithi     if (dumpType == "BMC")
4085cb1dd27SAsmitha Karunanithi     {
4095cb1dd27SAsmitha Karunanithi         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
4105cb1dd27SAsmitha Karunanithi     }
4115cb1dd27SAsmitha Karunanithi     else if (dumpType == "System")
4125cb1dd27SAsmitha Karunanithi     {
4135cb1dd27SAsmitha Karunanithi         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
4145cb1dd27SAsmitha Karunanithi     }
4155cb1dd27SAsmitha Karunanithi     else
4165cb1dd27SAsmitha Karunanithi     {
4175cb1dd27SAsmitha Karunanithi         BMCWEB_LOG_ERROR << "Invalid dump type" << dumpType;
4185cb1dd27SAsmitha Karunanithi         messages::internalError(asyncResp->res);
4195cb1dd27SAsmitha Karunanithi         return;
4205cb1dd27SAsmitha Karunanithi     }
4215cb1dd27SAsmitha Karunanithi 
4225cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
4235cb1dd27SAsmitha Karunanithi         [asyncResp, dumpPath, dumpType](const boost::system::error_code ec,
4245cb1dd27SAsmitha Karunanithi                                         GetManagedObjectsType& resp) {
4255cb1dd27SAsmitha Karunanithi             if (ec)
4265cb1dd27SAsmitha Karunanithi             {
4275cb1dd27SAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
4285cb1dd27SAsmitha Karunanithi                 messages::internalError(asyncResp->res);
4295cb1dd27SAsmitha Karunanithi                 return;
4305cb1dd27SAsmitha Karunanithi             }
4315cb1dd27SAsmitha Karunanithi 
4325cb1dd27SAsmitha Karunanithi             nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
4335cb1dd27SAsmitha Karunanithi             entriesArray = nlohmann::json::array();
434b47452b2SAsmitha Karunanithi             std::string dumpEntryPath =
435b47452b2SAsmitha Karunanithi                 "/xyz/openbmc_project/dump/" +
436b47452b2SAsmitha Karunanithi                 std::string(boost::algorithm::to_lower_copy(dumpType)) +
437b47452b2SAsmitha Karunanithi                 "/entry/";
4385cb1dd27SAsmitha Karunanithi 
4395cb1dd27SAsmitha Karunanithi             for (auto& object : resp)
4405cb1dd27SAsmitha Karunanithi             {
441b47452b2SAsmitha Karunanithi                 if (object.first.str.find(dumpEntryPath) == std::string::npos)
4425cb1dd27SAsmitha Karunanithi                 {
4435cb1dd27SAsmitha Karunanithi                     continue;
4445cb1dd27SAsmitha Karunanithi                 }
4455cb1dd27SAsmitha Karunanithi                 std::time_t timestamp;
4465cb1dd27SAsmitha Karunanithi                 uint64_t size = 0;
4475cb1dd27SAsmitha Karunanithi                 entriesArray.push_back({});
4485cb1dd27SAsmitha Karunanithi                 nlohmann::json& thisEntry = entriesArray.back();
4492dfd18efSEd Tanous 
4502dfd18efSEd Tanous                 std::string entryID = object.first.filename();
4512dfd18efSEd Tanous                 if (entryID.empty())
4525cb1dd27SAsmitha Karunanithi                 {
4535cb1dd27SAsmitha Karunanithi                     continue;
4545cb1dd27SAsmitha Karunanithi                 }
4555cb1dd27SAsmitha Karunanithi 
4565cb1dd27SAsmitha Karunanithi                 for (auto& interfaceMap : object.second)
4575cb1dd27SAsmitha Karunanithi                 {
4585cb1dd27SAsmitha Karunanithi                     if (interfaceMap.first == "xyz.openbmc_project.Dump.Entry")
4595cb1dd27SAsmitha Karunanithi                     {
4605cb1dd27SAsmitha Karunanithi 
4615cb1dd27SAsmitha Karunanithi                         for (auto& propertyMap : interfaceMap.second)
4625cb1dd27SAsmitha Karunanithi                         {
4635cb1dd27SAsmitha Karunanithi                             if (propertyMap.first == "Size")
4645cb1dd27SAsmitha Karunanithi                             {
4655cb1dd27SAsmitha Karunanithi                                 auto sizePtr =
4665cb1dd27SAsmitha Karunanithi                                     std::get_if<uint64_t>(&propertyMap.second);
4675cb1dd27SAsmitha Karunanithi                                 if (sizePtr == nullptr)
4685cb1dd27SAsmitha Karunanithi                                 {
4695cb1dd27SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
4705cb1dd27SAsmitha Karunanithi                                     break;
4715cb1dd27SAsmitha Karunanithi                                 }
4725cb1dd27SAsmitha Karunanithi                                 size = *sizePtr;
4735cb1dd27SAsmitha Karunanithi                                 break;
4745cb1dd27SAsmitha Karunanithi                             }
4755cb1dd27SAsmitha Karunanithi                         }
4765cb1dd27SAsmitha Karunanithi                     }
4775cb1dd27SAsmitha Karunanithi                     else if (interfaceMap.first ==
4785cb1dd27SAsmitha Karunanithi                              "xyz.openbmc_project.Time.EpochTime")
4795cb1dd27SAsmitha Karunanithi                     {
4805cb1dd27SAsmitha Karunanithi 
4815cb1dd27SAsmitha Karunanithi                         for (auto& propertyMap : interfaceMap.second)
4825cb1dd27SAsmitha Karunanithi                         {
4835cb1dd27SAsmitha Karunanithi                             if (propertyMap.first == "Elapsed")
4845cb1dd27SAsmitha Karunanithi                             {
4855cb1dd27SAsmitha Karunanithi                                 const uint64_t* usecsTimeStamp =
4865cb1dd27SAsmitha Karunanithi                                     std::get_if<uint64_t>(&propertyMap.second);
4875cb1dd27SAsmitha Karunanithi                                 if (usecsTimeStamp == nullptr)
4885cb1dd27SAsmitha Karunanithi                                 {
4895cb1dd27SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
4905cb1dd27SAsmitha Karunanithi                                     break;
4915cb1dd27SAsmitha Karunanithi                                 }
4925cb1dd27SAsmitha Karunanithi                                 timestamp =
4935cb1dd27SAsmitha Karunanithi                                     static_cast<std::time_t>(*usecsTimeStamp);
4945cb1dd27SAsmitha Karunanithi                                 break;
4955cb1dd27SAsmitha Karunanithi                             }
4965cb1dd27SAsmitha Karunanithi                         }
4975cb1dd27SAsmitha Karunanithi                     }
4985cb1dd27SAsmitha Karunanithi                 }
4995cb1dd27SAsmitha Karunanithi 
500d337bb72SAsmitha Karunanithi                 thisEntry["@odata.type"] = "#LogEntry.v1_7_0.LogEntry";
5015cb1dd27SAsmitha Karunanithi                 thisEntry["@odata.id"] = dumpPath + entryID;
5025cb1dd27SAsmitha Karunanithi                 thisEntry["Id"] = entryID;
5035cb1dd27SAsmitha Karunanithi                 thisEntry["EntryType"] = "Event";
5045cb1dd27SAsmitha Karunanithi                 thisEntry["Created"] = crow::utility::getDateTime(timestamp);
5055cb1dd27SAsmitha Karunanithi                 thisEntry["Name"] = dumpType + " Dump Entry";
5065cb1dd27SAsmitha Karunanithi 
507d337bb72SAsmitha Karunanithi                 thisEntry["AdditionalDataSizeBytes"] = size;
5085cb1dd27SAsmitha Karunanithi 
5095cb1dd27SAsmitha Karunanithi                 if (dumpType == "BMC")
5105cb1dd27SAsmitha Karunanithi                 {
511d337bb72SAsmitha Karunanithi                     thisEntry["DiagnosticDataType"] = "Manager";
512d337bb72SAsmitha Karunanithi                     thisEntry["AdditionalDataURI"] =
513d337bb72SAsmitha Karunanithi                         "/redfish/v1/Managers/bmc/LogServices/Dump/"
514d337bb72SAsmitha Karunanithi                         "attachment/" +
515d337bb72SAsmitha Karunanithi                         entryID;
5165cb1dd27SAsmitha Karunanithi                 }
5175cb1dd27SAsmitha Karunanithi                 else if (dumpType == "System")
5185cb1dd27SAsmitha Karunanithi                 {
519d337bb72SAsmitha Karunanithi                     thisEntry["DiagnosticDataType"] = "OEM";
520d337bb72SAsmitha Karunanithi                     thisEntry["OEMDiagnosticDataType"] = "System";
521d337bb72SAsmitha Karunanithi                     thisEntry["AdditionalDataURI"] =
522d337bb72SAsmitha Karunanithi                         "/redfish/v1/Systems/system/LogServices/Dump/"
523d337bb72SAsmitha Karunanithi                         "attachment/" +
524d337bb72SAsmitha Karunanithi                         entryID;
5255cb1dd27SAsmitha Karunanithi                 }
5265cb1dd27SAsmitha Karunanithi             }
5275cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Members@odata.count"] =
5285cb1dd27SAsmitha Karunanithi                 entriesArray.size();
5295cb1dd27SAsmitha Karunanithi         },
5305cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
5315cb1dd27SAsmitha Karunanithi         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
5325cb1dd27SAsmitha Karunanithi }
5335cb1dd27SAsmitha Karunanithi 
5345cb1dd27SAsmitha Karunanithi inline void getDumpEntryById(std::shared_ptr<AsyncResp>& asyncResp,
5355cb1dd27SAsmitha Karunanithi                              const std::string& entryID,
5365cb1dd27SAsmitha Karunanithi                              const std::string& dumpType)
5375cb1dd27SAsmitha Karunanithi {
5385cb1dd27SAsmitha Karunanithi     std::string dumpPath;
5395cb1dd27SAsmitha Karunanithi     if (dumpType == "BMC")
5405cb1dd27SAsmitha Karunanithi     {
5415cb1dd27SAsmitha Karunanithi         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
5425cb1dd27SAsmitha Karunanithi     }
5435cb1dd27SAsmitha Karunanithi     else if (dumpType == "System")
5445cb1dd27SAsmitha Karunanithi     {
5455cb1dd27SAsmitha Karunanithi         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
5465cb1dd27SAsmitha Karunanithi     }
5475cb1dd27SAsmitha Karunanithi     else
5485cb1dd27SAsmitha Karunanithi     {
5495cb1dd27SAsmitha Karunanithi         BMCWEB_LOG_ERROR << "Invalid dump type" << dumpType;
5505cb1dd27SAsmitha Karunanithi         messages::internalError(asyncResp->res);
5515cb1dd27SAsmitha Karunanithi         return;
5525cb1dd27SAsmitha Karunanithi     }
5535cb1dd27SAsmitha Karunanithi 
5545cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
5555cb1dd27SAsmitha Karunanithi         [asyncResp, entryID, dumpPath, dumpType](
5565cb1dd27SAsmitha Karunanithi             const boost::system::error_code ec, GetManagedObjectsType& resp) {
5575cb1dd27SAsmitha Karunanithi             if (ec)
5585cb1dd27SAsmitha Karunanithi             {
5595cb1dd27SAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
5605cb1dd27SAsmitha Karunanithi                 messages::internalError(asyncResp->res);
5615cb1dd27SAsmitha Karunanithi                 return;
5625cb1dd27SAsmitha Karunanithi             }
5635cb1dd27SAsmitha Karunanithi 
564b47452b2SAsmitha Karunanithi             bool foundDumpEntry = false;
565b47452b2SAsmitha Karunanithi             std::string dumpEntryPath =
566b47452b2SAsmitha Karunanithi                 "/xyz/openbmc_project/dump/" +
567b47452b2SAsmitha Karunanithi                 std::string(boost::algorithm::to_lower_copy(dumpType)) +
568b47452b2SAsmitha Karunanithi                 "/entry/";
569b47452b2SAsmitha Karunanithi 
5705cb1dd27SAsmitha Karunanithi             for (auto& objectPath : resp)
5715cb1dd27SAsmitha Karunanithi             {
572b47452b2SAsmitha Karunanithi                 if (objectPath.first.str != dumpEntryPath + entryID)
5735cb1dd27SAsmitha Karunanithi                 {
5745cb1dd27SAsmitha Karunanithi                     continue;
5755cb1dd27SAsmitha Karunanithi                 }
5765cb1dd27SAsmitha Karunanithi 
5775cb1dd27SAsmitha Karunanithi                 foundDumpEntry = true;
5785cb1dd27SAsmitha Karunanithi                 std::time_t timestamp;
5795cb1dd27SAsmitha Karunanithi                 uint64_t size = 0;
5805cb1dd27SAsmitha Karunanithi 
5815cb1dd27SAsmitha Karunanithi                 for (auto& interfaceMap : objectPath.second)
5825cb1dd27SAsmitha Karunanithi                 {
5835cb1dd27SAsmitha Karunanithi                     if (interfaceMap.first == "xyz.openbmc_project.Dump.Entry")
5845cb1dd27SAsmitha Karunanithi                     {
5855cb1dd27SAsmitha Karunanithi                         for (auto& propertyMap : interfaceMap.second)
5865cb1dd27SAsmitha Karunanithi                         {
5875cb1dd27SAsmitha Karunanithi                             if (propertyMap.first == "Size")
5885cb1dd27SAsmitha Karunanithi                             {
5895cb1dd27SAsmitha Karunanithi                                 auto sizePtr =
5905cb1dd27SAsmitha Karunanithi                                     std::get_if<uint64_t>(&propertyMap.second);
5915cb1dd27SAsmitha Karunanithi                                 if (sizePtr == nullptr)
5925cb1dd27SAsmitha Karunanithi                                 {
5935cb1dd27SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
5945cb1dd27SAsmitha Karunanithi                                     break;
5955cb1dd27SAsmitha Karunanithi                                 }
5965cb1dd27SAsmitha Karunanithi                                 size = *sizePtr;
5975cb1dd27SAsmitha Karunanithi                                 break;
5985cb1dd27SAsmitha Karunanithi                             }
5995cb1dd27SAsmitha Karunanithi                         }
6005cb1dd27SAsmitha Karunanithi                     }
6015cb1dd27SAsmitha Karunanithi                     else if (interfaceMap.first ==
6025cb1dd27SAsmitha Karunanithi                              "xyz.openbmc_project.Time.EpochTime")
6035cb1dd27SAsmitha Karunanithi                     {
6045cb1dd27SAsmitha Karunanithi                         for (auto& propertyMap : interfaceMap.second)
6055cb1dd27SAsmitha Karunanithi                         {
6065cb1dd27SAsmitha Karunanithi                             if (propertyMap.first == "Elapsed")
6075cb1dd27SAsmitha Karunanithi                             {
6085cb1dd27SAsmitha Karunanithi                                 const uint64_t* usecsTimeStamp =
6095cb1dd27SAsmitha Karunanithi                                     std::get_if<uint64_t>(&propertyMap.second);
6105cb1dd27SAsmitha Karunanithi                                 if (usecsTimeStamp == nullptr)
6115cb1dd27SAsmitha Karunanithi                                 {
6125cb1dd27SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
6135cb1dd27SAsmitha Karunanithi                                     break;
6145cb1dd27SAsmitha Karunanithi                                 }
6155cb1dd27SAsmitha Karunanithi                                 timestamp =
6165cb1dd27SAsmitha Karunanithi                                     static_cast<std::time_t>(*usecsTimeStamp);
6175cb1dd27SAsmitha Karunanithi                                 break;
6185cb1dd27SAsmitha Karunanithi                             }
6195cb1dd27SAsmitha Karunanithi                         }
6205cb1dd27SAsmitha Karunanithi                     }
6215cb1dd27SAsmitha Karunanithi                 }
6225cb1dd27SAsmitha Karunanithi 
6235cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.type"] =
624d337bb72SAsmitha Karunanithi                     "#LogEntry.v1_7_0.LogEntry";
6255cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.id"] = dumpPath + entryID;
6265cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Id"] = entryID;
6275cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["EntryType"] = "Event";
6285cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Created"] =
6295cb1dd27SAsmitha Karunanithi                     crow::utility::getDateTime(timestamp);
6305cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
6315cb1dd27SAsmitha Karunanithi 
632d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
6335cb1dd27SAsmitha Karunanithi 
6345cb1dd27SAsmitha Karunanithi                 if (dumpType == "BMC")
6355cb1dd27SAsmitha Karunanithi                 {
636d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager";
637d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["AdditionalDataURI"] =
6385cb1dd27SAsmitha Karunanithi                         "/redfish/v1/Managers/bmc/LogServices/Dump/"
6395cb1dd27SAsmitha Karunanithi                         "attachment/" +
6405cb1dd27SAsmitha Karunanithi                         entryID;
6415cb1dd27SAsmitha Karunanithi                 }
6425cb1dd27SAsmitha Karunanithi                 else if (dumpType == "System")
6435cb1dd27SAsmitha Karunanithi                 {
644d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM";
645d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["OEMDiagnosticDataType"] =
6465cb1dd27SAsmitha Karunanithi                         "System";
647d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["AdditionalDataURI"] =
6485cb1dd27SAsmitha Karunanithi                         "/redfish/v1/Systems/system/LogServices/Dump/"
6495cb1dd27SAsmitha Karunanithi                         "attachment/" +
6505cb1dd27SAsmitha Karunanithi                         entryID;
6515cb1dd27SAsmitha Karunanithi                 }
6525cb1dd27SAsmitha Karunanithi             }
653b47452b2SAsmitha Karunanithi             if (foundDumpEntry == false)
654b47452b2SAsmitha Karunanithi             {
655b47452b2SAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "Can't find Dump Entry";
656b47452b2SAsmitha Karunanithi                 messages::internalError(asyncResp->res);
657b47452b2SAsmitha Karunanithi                 return;
658b47452b2SAsmitha Karunanithi             }
6595cb1dd27SAsmitha Karunanithi         },
6605cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
6615cb1dd27SAsmitha Karunanithi         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
6625cb1dd27SAsmitha Karunanithi }
6635cb1dd27SAsmitha Karunanithi 
6649878256fSStanley Chu inline void deleteDumpEntry(const std::shared_ptr<AsyncResp>& asyncResp,
6659878256fSStanley Chu                             const std::string& entryID,
666b47452b2SAsmitha Karunanithi                             const std::string& dumpType)
6675cb1dd27SAsmitha Karunanithi {
6683de8d8baSGeorge Liu     auto respHandler = [asyncResp,
6693de8d8baSGeorge Liu                         entryID](const boost::system::error_code ec) {
6705cb1dd27SAsmitha Karunanithi         BMCWEB_LOG_DEBUG << "Dump Entry doDelete callback: Done";
6715cb1dd27SAsmitha Karunanithi         if (ec)
6725cb1dd27SAsmitha Karunanithi         {
6733de8d8baSGeorge Liu             if (ec.value() == EBADR)
6743de8d8baSGeorge Liu             {
6753de8d8baSGeorge Liu                 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
6763de8d8baSGeorge Liu                 return;
6773de8d8baSGeorge Liu             }
6785cb1dd27SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Dump (DBus) doDelete respHandler got error "
6795cb1dd27SAsmitha Karunanithi                              << ec;
6805cb1dd27SAsmitha Karunanithi             messages::internalError(asyncResp->res);
6815cb1dd27SAsmitha Karunanithi             return;
6825cb1dd27SAsmitha Karunanithi         }
6835cb1dd27SAsmitha Karunanithi     };
6845cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
6855cb1dd27SAsmitha Karunanithi         respHandler, "xyz.openbmc_project.Dump.Manager",
686b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" +
687b47452b2SAsmitha Karunanithi             std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/" +
688b47452b2SAsmitha Karunanithi             entryID,
6895cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Object.Delete", "Delete");
6905cb1dd27SAsmitha Karunanithi }
6915cb1dd27SAsmitha Karunanithi 
692a43be80fSAsmitha Karunanithi inline void createDumpTaskCallback(const crow::Request& req,
693b5a76932SEd Tanous                                    const std::shared_ptr<AsyncResp>& asyncResp,
694a43be80fSAsmitha Karunanithi                                    const uint32_t& dumpId,
695a43be80fSAsmitha Karunanithi                                    const std::string& dumpPath,
696a43be80fSAsmitha Karunanithi                                    const std::string& dumpType)
697a43be80fSAsmitha Karunanithi {
698a43be80fSAsmitha Karunanithi     std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
6996145ed6fSAsmitha Karunanithi         [dumpId, dumpPath, dumpType](
700a43be80fSAsmitha Karunanithi             boost::system::error_code err, sdbusplus::message::message& m,
701a43be80fSAsmitha Karunanithi             const std::shared_ptr<task::TaskData>& taskData) {
702cb13a392SEd Tanous             if (err)
703cb13a392SEd Tanous             {
7046145ed6fSAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "Error in creating a dump";
7056145ed6fSAsmitha Karunanithi                 taskData->state = "Cancelled";
7066145ed6fSAsmitha Karunanithi                 return task::completed;
707cb13a392SEd Tanous             }
708a43be80fSAsmitha Karunanithi             std::vector<std::pair<
709a43be80fSAsmitha Karunanithi                 std::string,
710a43be80fSAsmitha Karunanithi                 std::vector<std::pair<std::string, std::variant<std::string>>>>>
711a43be80fSAsmitha Karunanithi                 interfacesList;
712a43be80fSAsmitha Karunanithi 
713a43be80fSAsmitha Karunanithi             sdbusplus::message::object_path objPath;
714a43be80fSAsmitha Karunanithi 
715a43be80fSAsmitha Karunanithi             m.read(objPath, interfacesList);
716a43be80fSAsmitha Karunanithi 
717b47452b2SAsmitha Karunanithi             if (objPath.str ==
718b47452b2SAsmitha Karunanithi                 "/xyz/openbmc_project/dump/" +
719b47452b2SAsmitha Karunanithi                     std::string(boost::algorithm::to_lower_copy(dumpType)) +
720b47452b2SAsmitha Karunanithi                     "/entry/" + std::to_string(dumpId))
721a43be80fSAsmitha Karunanithi             {
722a43be80fSAsmitha Karunanithi                 nlohmann::json retMessage = messages::success();
723a43be80fSAsmitha Karunanithi                 taskData->messages.emplace_back(retMessage);
724a43be80fSAsmitha Karunanithi 
725a43be80fSAsmitha Karunanithi                 std::string headerLoc =
726a43be80fSAsmitha Karunanithi                     "Location: " + dumpPath + std::to_string(dumpId);
727a43be80fSAsmitha Karunanithi                 taskData->payload->httpHeaders.emplace_back(
728a43be80fSAsmitha Karunanithi                     std::move(headerLoc));
729a43be80fSAsmitha Karunanithi 
730a43be80fSAsmitha Karunanithi                 taskData->state = "Completed";
731b47452b2SAsmitha Karunanithi                 return task::completed;
7326145ed6fSAsmitha Karunanithi             }
733a43be80fSAsmitha Karunanithi             return task::completed;
734a43be80fSAsmitha Karunanithi         },
735a43be80fSAsmitha Karunanithi         "type='signal',interface='org.freedesktop.DBus."
736a43be80fSAsmitha Karunanithi         "ObjectManager',"
737a43be80fSAsmitha Karunanithi         "member='InterfacesAdded', "
738a43be80fSAsmitha Karunanithi         "path='/xyz/openbmc_project/dump'");
739a43be80fSAsmitha Karunanithi 
740a43be80fSAsmitha Karunanithi     task->startTimer(std::chrono::minutes(3));
741a43be80fSAsmitha Karunanithi     task->populateResp(asyncResp->res);
742a43be80fSAsmitha Karunanithi     task->payload.emplace(req);
743a43be80fSAsmitha Karunanithi }
744a43be80fSAsmitha Karunanithi 
745a43be80fSAsmitha Karunanithi inline void createDump(crow::Response& res, const crow::Request& req,
746a43be80fSAsmitha Karunanithi                        const std::string& dumpType)
747a43be80fSAsmitha Karunanithi {
748a43be80fSAsmitha Karunanithi     std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
749a43be80fSAsmitha Karunanithi 
750a43be80fSAsmitha Karunanithi     std::string dumpPath;
751a43be80fSAsmitha Karunanithi     if (dumpType == "BMC")
752a43be80fSAsmitha Karunanithi     {
753a43be80fSAsmitha Karunanithi         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
754a43be80fSAsmitha Karunanithi     }
755a43be80fSAsmitha Karunanithi     else if (dumpType == "System")
756a43be80fSAsmitha Karunanithi     {
757a43be80fSAsmitha Karunanithi         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
758a43be80fSAsmitha Karunanithi     }
759a43be80fSAsmitha Karunanithi     else
760a43be80fSAsmitha Karunanithi     {
761a43be80fSAsmitha Karunanithi         BMCWEB_LOG_ERROR << "Invalid dump type: " << dumpType;
762a43be80fSAsmitha Karunanithi         messages::internalError(asyncResp->res);
763a43be80fSAsmitha Karunanithi         return;
764a43be80fSAsmitha Karunanithi     }
765a43be80fSAsmitha Karunanithi 
766a43be80fSAsmitha Karunanithi     std::optional<std::string> diagnosticDataType;
767a43be80fSAsmitha Karunanithi     std::optional<std::string> oemDiagnosticDataType;
768a43be80fSAsmitha Karunanithi 
769a43be80fSAsmitha Karunanithi     if (!redfish::json_util::readJson(
770a43be80fSAsmitha Karunanithi             req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
771a43be80fSAsmitha Karunanithi             "OEMDiagnosticDataType", oemDiagnosticDataType))
772a43be80fSAsmitha Karunanithi     {
773a43be80fSAsmitha Karunanithi         return;
774a43be80fSAsmitha Karunanithi     }
775a43be80fSAsmitha Karunanithi 
776a43be80fSAsmitha Karunanithi     if (dumpType == "System")
777a43be80fSAsmitha Karunanithi     {
778a43be80fSAsmitha Karunanithi         if (!oemDiagnosticDataType || !diagnosticDataType)
779a43be80fSAsmitha Karunanithi         {
780a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "CreateDump action parameter "
781a43be80fSAsmitha Karunanithi                                 "'DiagnosticDataType'/"
782a43be80fSAsmitha Karunanithi                                 "'OEMDiagnosticDataType' value not found!";
783a43be80fSAsmitha Karunanithi             messages::actionParameterMissing(
784a43be80fSAsmitha Karunanithi                 asyncResp->res, "CollectDiagnosticData",
785a43be80fSAsmitha Karunanithi                 "DiagnosticDataType & OEMDiagnosticDataType");
786a43be80fSAsmitha Karunanithi             return;
787a43be80fSAsmitha Karunanithi         }
7883174e4dfSEd Tanous         if ((*oemDiagnosticDataType != "System") ||
789a43be80fSAsmitha Karunanithi             (*diagnosticDataType != "OEM"))
790a43be80fSAsmitha Karunanithi         {
791a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Wrong parameter values passed";
792a43be80fSAsmitha Karunanithi             messages::invalidObject(asyncResp->res,
793a43be80fSAsmitha Karunanithi                                     "System Dump creation parameters");
794a43be80fSAsmitha Karunanithi             return;
795a43be80fSAsmitha Karunanithi         }
796a43be80fSAsmitha Karunanithi     }
797a43be80fSAsmitha Karunanithi     else if (dumpType == "BMC")
798a43be80fSAsmitha Karunanithi     {
799a43be80fSAsmitha Karunanithi         if (!diagnosticDataType)
800a43be80fSAsmitha Karunanithi         {
801a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "CreateDump action parameter "
802a43be80fSAsmitha Karunanithi                                 "'DiagnosticDataType' not found!";
803a43be80fSAsmitha Karunanithi             messages::actionParameterMissing(
804a43be80fSAsmitha Karunanithi                 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
805a43be80fSAsmitha Karunanithi             return;
806a43be80fSAsmitha Karunanithi         }
8073174e4dfSEd Tanous         if (*diagnosticDataType != "Manager")
808a43be80fSAsmitha Karunanithi         {
809a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR
810a43be80fSAsmitha Karunanithi                 << "Wrong parameter value passed for 'DiagnosticDataType'";
811a43be80fSAsmitha Karunanithi             messages::invalidObject(asyncResp->res,
812a43be80fSAsmitha Karunanithi                                     "BMC Dump creation parameters");
813a43be80fSAsmitha Karunanithi             return;
814a43be80fSAsmitha Karunanithi         }
815a43be80fSAsmitha Karunanithi     }
816a43be80fSAsmitha Karunanithi 
817a43be80fSAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
818a43be80fSAsmitha Karunanithi         [asyncResp, req, dumpPath, dumpType](const boost::system::error_code ec,
819a43be80fSAsmitha Karunanithi                                              const uint32_t& dumpId) {
820a43be80fSAsmitha Karunanithi             if (ec)
821a43be80fSAsmitha Karunanithi             {
822a43be80fSAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "CreateDump resp_handler got error " << ec;
823a43be80fSAsmitha Karunanithi                 messages::internalError(asyncResp->res);
824a43be80fSAsmitha Karunanithi                 return;
825a43be80fSAsmitha Karunanithi             }
826a43be80fSAsmitha Karunanithi             BMCWEB_LOG_DEBUG << "Dump Created. Id: " << dumpId;
827a43be80fSAsmitha Karunanithi 
828a43be80fSAsmitha Karunanithi             createDumpTaskCallback(req, asyncResp, dumpId, dumpPath, dumpType);
829a43be80fSAsmitha Karunanithi         },
830b47452b2SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager",
831b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" +
832b47452b2SAsmitha Karunanithi             std::string(boost::algorithm::to_lower_copy(dumpType)),
833a43be80fSAsmitha Karunanithi         "xyz.openbmc_project.Dump.Create", "CreateDump");
834a43be80fSAsmitha Karunanithi }
835a43be80fSAsmitha Karunanithi 
836b47452b2SAsmitha Karunanithi inline void clearDump(crow::Response& res, const std::string& dumpType)
83780319af1SAsmitha Karunanithi {
838b47452b2SAsmitha Karunanithi     std::string dumpTypeLowerCopy =
839b47452b2SAsmitha Karunanithi         std::string(boost::algorithm::to_lower_copy(dumpType));
84080319af1SAsmitha Karunanithi     std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
84180319af1SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
842b47452b2SAsmitha Karunanithi         [asyncResp, dumpType](const boost::system::error_code ec,
84380319af1SAsmitha Karunanithi                               const std::vector<std::string>& subTreePaths) {
84480319af1SAsmitha Karunanithi             if (ec)
84580319af1SAsmitha Karunanithi             {
84680319af1SAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "resp_handler got error " << ec;
84780319af1SAsmitha Karunanithi                 messages::internalError(asyncResp->res);
84880319af1SAsmitha Karunanithi                 return;
84980319af1SAsmitha Karunanithi             }
85080319af1SAsmitha Karunanithi 
85180319af1SAsmitha Karunanithi             for (const std::string& path : subTreePaths)
85280319af1SAsmitha Karunanithi             {
8532dfd18efSEd Tanous                 sdbusplus::message::object_path objPath(path);
8542dfd18efSEd Tanous                 std::string logID = objPath.filename();
8552dfd18efSEd Tanous                 if (logID.empty())
85680319af1SAsmitha Karunanithi                 {
8572dfd18efSEd Tanous                     continue;
85880319af1SAsmitha Karunanithi                 }
8592dfd18efSEd Tanous                 deleteDumpEntry(asyncResp, logID, dumpType);
86080319af1SAsmitha Karunanithi             }
86180319af1SAsmitha Karunanithi         },
86280319af1SAsmitha Karunanithi         "xyz.openbmc_project.ObjectMapper",
86380319af1SAsmitha Karunanithi         "/xyz/openbmc_project/object_mapper",
86480319af1SAsmitha Karunanithi         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
865b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" + dumpTypeLowerCopy, 0,
866b47452b2SAsmitha Karunanithi         std::array<std::string, 1>{"xyz.openbmc_project.Dump.Entry." +
867b47452b2SAsmitha Karunanithi                                    dumpType});
86880319af1SAsmitha Karunanithi }
86980319af1SAsmitha Karunanithi 
8702c70f800SEd Tanous static void parseCrashdumpParameters(
871043a0536SJohnathan Mantey     const std::vector<std::pair<std::string, VariantType>>& params,
872043a0536SJohnathan Mantey     std::string& filename, std::string& timestamp, std::string& logfile)
873043a0536SJohnathan Mantey {
874043a0536SJohnathan Mantey     for (auto property : params)
875043a0536SJohnathan Mantey     {
876043a0536SJohnathan Mantey         if (property.first == "Timestamp")
877043a0536SJohnathan Mantey         {
878043a0536SJohnathan Mantey             const std::string* value =
8798d78b7a9SPatrick Williams                 std::get_if<std::string>(&property.second);
880043a0536SJohnathan Mantey             if (value != nullptr)
881043a0536SJohnathan Mantey             {
882043a0536SJohnathan Mantey                 timestamp = *value;
883043a0536SJohnathan Mantey             }
884043a0536SJohnathan Mantey         }
885043a0536SJohnathan Mantey         else if (property.first == "Filename")
886043a0536SJohnathan Mantey         {
887043a0536SJohnathan Mantey             const std::string* value =
8888d78b7a9SPatrick Williams                 std::get_if<std::string>(&property.second);
889043a0536SJohnathan Mantey             if (value != nullptr)
890043a0536SJohnathan Mantey             {
891043a0536SJohnathan Mantey                 filename = *value;
892043a0536SJohnathan Mantey             }
893043a0536SJohnathan Mantey         }
894043a0536SJohnathan Mantey         else if (property.first == "Log")
895043a0536SJohnathan Mantey         {
896043a0536SJohnathan Mantey             const std::string* value =
8978d78b7a9SPatrick Williams                 std::get_if<std::string>(&property.second);
898043a0536SJohnathan Mantey             if (value != nullptr)
899043a0536SJohnathan Mantey             {
900043a0536SJohnathan Mantey                 logfile = *value;
901043a0536SJohnathan Mantey             }
902043a0536SJohnathan Mantey         }
903043a0536SJohnathan Mantey     }
904043a0536SJohnathan Mantey }
905043a0536SJohnathan Mantey 
906a3316fc6SZhikuiRen constexpr char const* postCodeIface = "xyz.openbmc_project.State.Boot.PostCode";
907c4bf6374SJason M. Bills class SystemLogServiceCollection : public Node
9081da66f75SEd Tanous {
9091da66f75SEd Tanous   public:
91052cc112dSEd Tanous     SystemLogServiceCollection(App& app) :
911029573d4SEd Tanous         Node(app, "/redfish/v1/Systems/system/LogServices/")
912c4bf6374SJason M. Bills     {
913c4bf6374SJason M. Bills         entityPrivileges = {
914c4bf6374SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
915c4bf6374SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
916c4bf6374SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
917c4bf6374SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
918c4bf6374SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
919c4bf6374SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
920c4bf6374SJason M. Bills     }
921c4bf6374SJason M. Bills 
922c4bf6374SJason M. Bills   private:
923c4bf6374SJason M. Bills     /**
924c4bf6374SJason M. Bills      * Functions triggers appropriate requests on DBus
925c4bf6374SJason M. Bills      */
926cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
927cb13a392SEd Tanous                const std::vector<std::string>&) override
928c4bf6374SJason M. Bills     {
929c4bf6374SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
930c4bf6374SJason M. Bills         // Collections don't include the static data added by SubRoute because
931c4bf6374SJason M. Bills         // it has a duplicate entry for members
932c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
933c4bf6374SJason M. Bills             "#LogServiceCollection.LogServiceCollection";
934c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
935029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices";
936c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "System Log Services Collection";
937c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] =
938c4bf6374SJason M. Bills             "Collection of LogServices for this Computer System";
939c4bf6374SJason M. Bills         nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
940c4bf6374SJason M. Bills         logServiceArray = nlohmann::json::array();
941029573d4SEd Tanous         logServiceArray.push_back(
942029573d4SEd Tanous             {{"@odata.id", "/redfish/v1/Systems/system/LogServices/EventLog"}});
9435cb1dd27SAsmitha Karunanithi #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
944c9bb6861Sraviteja-b         logServiceArray.push_back(
9455cb1dd27SAsmitha Karunanithi             {{"@odata.id", "/redfish/v1/Systems/system/LogServices/Dump"}});
946c9bb6861Sraviteja-b #endif
947c9bb6861Sraviteja-b 
948d53dd41fSJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
949d53dd41fSJason M. Bills         logServiceArray.push_back(
950cb92c03bSAndrew Geissler             {{"@odata.id",
951424c4176SJason M. Bills               "/redfish/v1/Systems/system/LogServices/Crashdump"}});
952d53dd41fSJason M. Bills #endif
953c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] =
954c4bf6374SJason M. Bills             logServiceArray.size();
955a3316fc6SZhikuiRen 
956a3316fc6SZhikuiRen         crow::connections::systemBus->async_method_call(
957a3316fc6SZhikuiRen             [asyncResp](const boost::system::error_code ec,
958a3316fc6SZhikuiRen                         const std::vector<std::string>& subtreePath) {
959a3316fc6SZhikuiRen                 if (ec)
960a3316fc6SZhikuiRen                 {
961a3316fc6SZhikuiRen                     BMCWEB_LOG_ERROR << ec;
962a3316fc6SZhikuiRen                     return;
963a3316fc6SZhikuiRen                 }
964a3316fc6SZhikuiRen 
965a3316fc6SZhikuiRen                 for (auto& pathStr : subtreePath)
966a3316fc6SZhikuiRen                 {
967a3316fc6SZhikuiRen                     if (pathStr.find("PostCode") != std::string::npos)
968a3316fc6SZhikuiRen                     {
96923a21a1cSEd Tanous                         nlohmann::json& logServiceArrayLocal =
970a3316fc6SZhikuiRen                             asyncResp->res.jsonValue["Members"];
97123a21a1cSEd Tanous                         logServiceArrayLocal.push_back(
972a3316fc6SZhikuiRen                             {{"@odata.id", "/redfish/v1/Systems/system/"
973a3316fc6SZhikuiRen                                            "LogServices/PostCodes"}});
974a3316fc6SZhikuiRen                         asyncResp->res.jsonValue["Members@odata.count"] =
97523a21a1cSEd Tanous                             logServiceArrayLocal.size();
976a3316fc6SZhikuiRen                         return;
977a3316fc6SZhikuiRen                     }
978a3316fc6SZhikuiRen                 }
979a3316fc6SZhikuiRen             },
980a3316fc6SZhikuiRen             "xyz.openbmc_project.ObjectMapper",
981a3316fc6SZhikuiRen             "/xyz/openbmc_project/object_mapper",
982a3316fc6SZhikuiRen             "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", 0,
983a3316fc6SZhikuiRen             std::array<const char*, 1>{postCodeIface});
984c4bf6374SJason M. Bills     }
985c4bf6374SJason M. Bills };
986c4bf6374SJason M. Bills 
987c4bf6374SJason M. Bills class EventLogService : public Node
988c4bf6374SJason M. Bills {
989c4bf6374SJason M. Bills   public:
99052cc112dSEd Tanous     EventLogService(App& app) :
991029573d4SEd Tanous         Node(app, "/redfish/v1/Systems/system/LogServices/EventLog/")
992c4bf6374SJason M. Bills     {
993c4bf6374SJason M. Bills         entityPrivileges = {
994c4bf6374SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
995c4bf6374SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
996c4bf6374SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
997c4bf6374SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
998c4bf6374SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
999c4bf6374SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1000c4bf6374SJason M. Bills     }
1001c4bf6374SJason M. Bills 
1002c4bf6374SJason M. Bills   private:
1003cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
1004cb13a392SEd Tanous                const std::vector<std::string>&) override
1005c4bf6374SJason M. Bills     {
1006c4bf6374SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1007c4bf6374SJason M. Bills 
1008c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
1009029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog";
1010c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1011c4bf6374SJason M. Bills             "#LogService.v1_1_0.LogService";
1012c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Event Log Service";
1013c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] = "System Event Log Service";
1014c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Id"] = "EventLog";
1015c4bf6374SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
1016c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Entries"] = {
1017c4bf6374SJason M. Bills             {"@odata.id",
1018029573d4SEd Tanous              "/redfish/v1/Systems/system/LogServices/EventLog/Entries"}};
1019e7d6c8b2SGunnar Mills         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
1020e7d6c8b2SGunnar Mills 
1021e7d6c8b2SGunnar Mills             {"target", "/redfish/v1/Systems/system/LogServices/EventLog/"
1022e7d6c8b2SGunnar Mills                        "Actions/LogService.ClearLog"}};
1023489640c6SJason M. Bills     }
1024489640c6SJason M. Bills };
1025489640c6SJason M. Bills 
10261f56a3a6STim Lee class JournalEventLogClear : public Node
1027489640c6SJason M. Bills {
1028489640c6SJason M. Bills   public:
102952cc112dSEd Tanous     JournalEventLogClear(App& app) :
1030489640c6SJason M. Bills         Node(app, "/redfish/v1/Systems/system/LogServices/EventLog/Actions/"
1031489640c6SJason M. Bills                   "LogService.ClearLog/")
1032489640c6SJason M. Bills     {
1033489640c6SJason M. Bills         entityPrivileges = {
1034489640c6SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1035489640c6SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1036489640c6SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1037489640c6SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1038489640c6SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1039489640c6SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
1040489640c6SJason M. Bills     }
1041489640c6SJason M. Bills 
1042489640c6SJason M. Bills   private:
1043cb13a392SEd Tanous     void doPost(crow::Response& res, const crow::Request&,
1044cb13a392SEd Tanous                 const std::vector<std::string>&) override
1045489640c6SJason M. Bills     {
1046489640c6SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1047489640c6SJason M. Bills 
1048489640c6SJason M. Bills         // Clear the EventLog by deleting the log files
1049489640c6SJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
1050489640c6SJason M. Bills         if (getRedfishLogFiles(redfishLogFiles))
1051489640c6SJason M. Bills         {
1052489640c6SJason M. Bills             for (const std::filesystem::path& file : redfishLogFiles)
1053489640c6SJason M. Bills             {
1054489640c6SJason M. Bills                 std::error_code ec;
1055489640c6SJason M. Bills                 std::filesystem::remove(file, ec);
1056489640c6SJason M. Bills             }
1057489640c6SJason M. Bills         }
1058489640c6SJason M. Bills 
1059489640c6SJason M. Bills         // Reload rsyslog so it knows to start new log files
1060489640c6SJason M. Bills         crow::connections::systemBus->async_method_call(
1061489640c6SJason M. Bills             [asyncResp](const boost::system::error_code ec) {
1062489640c6SJason M. Bills                 if (ec)
1063489640c6SJason M. Bills                 {
1064489640c6SJason M. Bills                     BMCWEB_LOG_ERROR << "Failed to reload rsyslog: " << ec;
1065489640c6SJason M. Bills                     messages::internalError(asyncResp->res);
1066489640c6SJason M. Bills                     return;
1067489640c6SJason M. Bills                 }
1068489640c6SJason M. Bills 
1069489640c6SJason M. Bills                 messages::success(asyncResp->res);
1070489640c6SJason M. Bills             },
1071489640c6SJason M. Bills             "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1072489640c6SJason M. Bills             "org.freedesktop.systemd1.Manager", "ReloadUnit", "rsyslog.service",
1073489640c6SJason M. Bills             "replace");
1074c4bf6374SJason M. Bills     }
1075c4bf6374SJason M. Bills };
1076c4bf6374SJason M. Bills 
107795820184SJason M. Bills static int fillEventLogEntryJson(const std::string& logEntryID,
1078b5a76932SEd Tanous                                  const std::string& logEntry,
107995820184SJason M. Bills                                  nlohmann::json& logEntryJson)
1080c4bf6374SJason M. Bills {
108195820184SJason M. Bills     // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
1082cd225da8SJason M. Bills     // First get the Timestamp
1083f23b7296SEd Tanous     size_t space = logEntry.find_first_of(' ');
1084cd225da8SJason M. Bills     if (space == std::string::npos)
108595820184SJason M. Bills     {
108695820184SJason M. Bills         return 1;
108795820184SJason M. Bills     }
1088cd225da8SJason M. Bills     std::string timestamp = logEntry.substr(0, space);
1089cd225da8SJason M. Bills     // Then get the log contents
1090f23b7296SEd Tanous     size_t entryStart = logEntry.find_first_not_of(' ', space);
1091cd225da8SJason M. Bills     if (entryStart == std::string::npos)
1092cd225da8SJason M. Bills     {
1093cd225da8SJason M. Bills         return 1;
1094cd225da8SJason M. Bills     }
1095cd225da8SJason M. Bills     std::string_view entry(logEntry);
1096cd225da8SJason M. Bills     entry.remove_prefix(entryStart);
1097cd225da8SJason M. Bills     // Use split to separate the entry into its fields
1098cd225da8SJason M. Bills     std::vector<std::string> logEntryFields;
1099cd225da8SJason M. Bills     boost::split(logEntryFields, entry, boost::is_any_of(","),
1100cd225da8SJason M. Bills                  boost::token_compress_on);
1101cd225da8SJason M. Bills     // We need at least a MessageId to be valid
1102cd225da8SJason M. Bills     if (logEntryFields.size() < 1)
1103cd225da8SJason M. Bills     {
1104cd225da8SJason M. Bills         return 1;
1105cd225da8SJason M. Bills     }
1106cd225da8SJason M. Bills     std::string& messageID = logEntryFields[0];
110795820184SJason M. Bills 
11084851d45dSJason M. Bills     // Get the Message from the MessageRegistry
11094851d45dSJason M. Bills     const message_registries::Message* message =
11104851d45dSJason M. Bills         message_registries::getMessage(messageID);
1111c4bf6374SJason M. Bills 
11124851d45dSJason M. Bills     std::string msg;
11134851d45dSJason M. Bills     std::string severity;
11144851d45dSJason M. Bills     if (message != nullptr)
1115c4bf6374SJason M. Bills     {
11164851d45dSJason M. Bills         msg = message->message;
11174851d45dSJason M. Bills         severity = message->severity;
1118c4bf6374SJason M. Bills     }
1119c4bf6374SJason M. Bills 
112015a86ff6SJason M. Bills     // Get the MessageArgs from the log if there are any
112115a86ff6SJason M. Bills     boost::beast::span<std::string> messageArgs;
112215a86ff6SJason M. Bills     if (logEntryFields.size() > 1)
112315a86ff6SJason M. Bills     {
112415a86ff6SJason M. Bills         std::string& messageArgsStart = logEntryFields[1];
112515a86ff6SJason M. Bills         // If the first string is empty, assume there are no MessageArgs
112615a86ff6SJason M. Bills         std::size_t messageArgsSize = 0;
112715a86ff6SJason M. Bills         if (!messageArgsStart.empty())
112815a86ff6SJason M. Bills         {
112915a86ff6SJason M. Bills             messageArgsSize = logEntryFields.size() - 1;
113015a86ff6SJason M. Bills         }
113115a86ff6SJason M. Bills 
113223a21a1cSEd Tanous         messageArgs = {&messageArgsStart, messageArgsSize};
1133c4bf6374SJason M. Bills 
11344851d45dSJason M. Bills         // Fill the MessageArgs into the Message
113595820184SJason M. Bills         int i = 0;
113695820184SJason M. Bills         for (const std::string& messageArg : messageArgs)
11374851d45dSJason M. Bills         {
113895820184SJason M. Bills             std::string argStr = "%" + std::to_string(++i);
11394851d45dSJason M. Bills             size_t argPos = msg.find(argStr);
11404851d45dSJason M. Bills             if (argPos != std::string::npos)
11414851d45dSJason M. Bills             {
114295820184SJason M. Bills                 msg.replace(argPos, argStr.length(), messageArg);
11434851d45dSJason M. Bills             }
11444851d45dSJason M. Bills         }
114515a86ff6SJason M. Bills     }
11464851d45dSJason M. Bills 
114795820184SJason M. Bills     // Get the Created time from the timestamp. The log timestamp is in RFC3339
114895820184SJason M. Bills     // format which matches the Redfish format except for the fractional seconds
114995820184SJason M. Bills     // between the '.' and the '+', so just remove them.
1150f23b7296SEd Tanous     std::size_t dot = timestamp.find_first_of('.');
1151f23b7296SEd Tanous     std::size_t plus = timestamp.find_first_of('+');
115295820184SJason M. Bills     if (dot != std::string::npos && plus != std::string::npos)
1153c4bf6374SJason M. Bills     {
115495820184SJason M. Bills         timestamp.erase(dot, plus - dot);
1155c4bf6374SJason M. Bills     }
1156c4bf6374SJason M. Bills 
1157c4bf6374SJason M. Bills     // Fill in the log entry with the gathered data
115895820184SJason M. Bills     logEntryJson = {
1159cb92c03bSAndrew Geissler         {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
1160029573d4SEd Tanous         {"@odata.id",
1161897967deSJason M. Bills          "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
116295820184SJason M. Bills              logEntryID},
1163c4bf6374SJason M. Bills         {"Name", "System Event Log Entry"},
116495820184SJason M. Bills         {"Id", logEntryID},
116595820184SJason M. Bills         {"Message", std::move(msg)},
116695820184SJason M. Bills         {"MessageId", std::move(messageID)},
1167f23b7296SEd Tanous         {"MessageArgs", messageArgs},
1168c4bf6374SJason M. Bills         {"EntryType", "Event"},
116995820184SJason M. Bills         {"Severity", std::move(severity)},
117095820184SJason M. Bills         {"Created", std::move(timestamp)}};
1171c4bf6374SJason M. Bills     return 0;
1172c4bf6374SJason M. Bills }
1173c4bf6374SJason M. Bills 
117427062605SAnthony Wilson class JournalEventLogEntryCollection : public Node
1175c4bf6374SJason M. Bills {
1176c4bf6374SJason M. Bills   public:
117752cc112dSEd Tanous     JournalEventLogEntryCollection(App& app) :
1178029573d4SEd Tanous         Node(app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
1179c4bf6374SJason M. Bills     {
1180c4bf6374SJason M. Bills         entityPrivileges = {
1181c4bf6374SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1182c4bf6374SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1183c4bf6374SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1184c4bf6374SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1185c4bf6374SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1186c4bf6374SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1187c4bf6374SJason M. Bills     }
1188c4bf6374SJason M. Bills 
1189c4bf6374SJason M. Bills   private:
1190c4bf6374SJason M. Bills     void doGet(crow::Response& res, const crow::Request& req,
1191cb13a392SEd Tanous                const std::vector<std::string>&) override
1192c4bf6374SJason M. Bills     {
1193c4bf6374SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1194271584abSEd Tanous         uint64_t skip = 0;
1195271584abSEd Tanous         uint64_t top = maxEntriesPerPage; // Show max entries by default
1196c4bf6374SJason M. Bills         if (!getSkipParam(asyncResp->res, req, skip))
1197c4bf6374SJason M. Bills         {
1198c4bf6374SJason M. Bills             return;
1199c4bf6374SJason M. Bills         }
1200c4bf6374SJason M. Bills         if (!getTopParam(asyncResp->res, req, top))
1201c4bf6374SJason M. Bills         {
1202c4bf6374SJason M. Bills             return;
1203c4bf6374SJason M. Bills         }
1204c4bf6374SJason M. Bills         // Collections don't include the static data added by SubRoute because
1205c4bf6374SJason M. Bills         // it has a duplicate entry for members
1206c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1207c4bf6374SJason M. Bills             "#LogEntryCollection.LogEntryCollection";
1208c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
1209029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1210c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1211c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] =
1212c4bf6374SJason M. Bills             "Collection of System Event Log Entries";
1213cb92c03bSAndrew Geissler 
1214c4bf6374SJason M. Bills         nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1215c4bf6374SJason M. Bills         logEntryArray = nlohmann::json::array();
121695820184SJason M. Bills         // Go through the log files and create a unique ID for each entry
121795820184SJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
121895820184SJason M. Bills         getRedfishLogFiles(redfishLogFiles);
1219b01bf299SEd Tanous         uint64_t entryCount = 0;
1220cd225da8SJason M. Bills         std::string logEntry;
122195820184SJason M. Bills 
122295820184SJason M. Bills         // Oldest logs are in the last file, so start there and loop backwards
1223cd225da8SJason M. Bills         for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1224cd225da8SJason M. Bills              it++)
1225c4bf6374SJason M. Bills         {
1226cd225da8SJason M. Bills             std::ifstream logStream(*it);
122795820184SJason M. Bills             if (!logStream.is_open())
1228c4bf6374SJason M. Bills             {
1229c4bf6374SJason M. Bills                 continue;
1230c4bf6374SJason M. Bills             }
1231c4bf6374SJason M. Bills 
1232e85d6b16SJason M. Bills             // Reset the unique ID on the first entry
1233e85d6b16SJason M. Bills             bool firstEntry = true;
123495820184SJason M. Bills             while (std::getline(logStream, logEntry))
123595820184SJason M. Bills             {
1236c4bf6374SJason M. Bills                 entryCount++;
1237c4bf6374SJason M. Bills                 // Handle paging using skip (number of entries to skip from the
1238c4bf6374SJason M. Bills                 // start) and top (number of entries to display)
1239c4bf6374SJason M. Bills                 if (entryCount <= skip || entryCount > skip + top)
1240c4bf6374SJason M. Bills                 {
1241c4bf6374SJason M. Bills                     continue;
1242c4bf6374SJason M. Bills                 }
1243c4bf6374SJason M. Bills 
1244c4bf6374SJason M. Bills                 std::string idStr;
1245e85d6b16SJason M. Bills                 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1246c4bf6374SJason M. Bills                 {
1247c4bf6374SJason M. Bills                     continue;
1248c4bf6374SJason M. Bills                 }
1249c4bf6374SJason M. Bills 
1250e85d6b16SJason M. Bills                 if (firstEntry)
1251e85d6b16SJason M. Bills                 {
1252e85d6b16SJason M. Bills                     firstEntry = false;
1253e85d6b16SJason M. Bills                 }
1254e85d6b16SJason M. Bills 
1255c4bf6374SJason M. Bills                 logEntryArray.push_back({});
1256c4bf6374SJason M. Bills                 nlohmann::json& bmcLogEntry = logEntryArray.back();
125795820184SJason M. Bills                 if (fillEventLogEntryJson(idStr, logEntry, bmcLogEntry) != 0)
1258c4bf6374SJason M. Bills                 {
1259c4bf6374SJason M. Bills                     messages::internalError(asyncResp->res);
1260c4bf6374SJason M. Bills                     return;
1261c4bf6374SJason M. Bills                 }
1262c4bf6374SJason M. Bills             }
126395820184SJason M. Bills         }
1264c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
1265c4bf6374SJason M. Bills         if (skip + top < entryCount)
1266c4bf6374SJason M. Bills         {
1267c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Members@odata.nextLink"] =
126895820184SJason M. Bills                 "/redfish/v1/Systems/system/LogServices/EventLog/"
126995820184SJason M. Bills                 "Entries?$skip=" +
1270c4bf6374SJason M. Bills                 std::to_string(skip + top);
1271c4bf6374SJason M. Bills         }
127208a4e4b5SAnthony Wilson     }
127308a4e4b5SAnthony Wilson };
127408a4e4b5SAnthony Wilson 
1275897967deSJason M. Bills class JournalEventLogEntry : public Node
1276897967deSJason M. Bills {
1277897967deSJason M. Bills   public:
127852cc112dSEd Tanous     JournalEventLogEntry(App& app) :
1279897967deSJason M. Bills         Node(app,
1280897967deSJason M. Bills              "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/",
1281897967deSJason M. Bills              std::string())
1282897967deSJason M. Bills     {
1283897967deSJason M. Bills         entityPrivileges = {
1284897967deSJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1285897967deSJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1286897967deSJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1287897967deSJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1288897967deSJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1289897967deSJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1290897967deSJason M. Bills     }
1291897967deSJason M. Bills 
1292897967deSJason M. Bills   private:
1293cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
1294897967deSJason M. Bills                const std::vector<std::string>& params) override
1295897967deSJason M. Bills     {
1296897967deSJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1297897967deSJason M. Bills         if (params.size() != 1)
1298897967deSJason M. Bills         {
1299897967deSJason M. Bills             messages::internalError(asyncResp->res);
1300897967deSJason M. Bills             return;
1301897967deSJason M. Bills         }
1302897967deSJason M. Bills         const std::string& targetID = params[0];
1303897967deSJason M. Bills 
1304897967deSJason M. Bills         // Go through the log files and check the unique ID for each entry to
1305897967deSJason M. Bills         // find the target entry
1306897967deSJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
1307897967deSJason M. Bills         getRedfishLogFiles(redfishLogFiles);
1308897967deSJason M. Bills         std::string logEntry;
1309897967deSJason M. Bills 
1310897967deSJason M. Bills         // Oldest logs are in the last file, so start there and loop backwards
1311897967deSJason M. Bills         for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1312897967deSJason M. Bills              it++)
1313897967deSJason M. Bills         {
1314897967deSJason M. Bills             std::ifstream logStream(*it);
1315897967deSJason M. Bills             if (!logStream.is_open())
1316897967deSJason M. Bills             {
1317897967deSJason M. Bills                 continue;
1318897967deSJason M. Bills             }
1319897967deSJason M. Bills 
1320897967deSJason M. Bills             // Reset the unique ID on the first entry
1321897967deSJason M. Bills             bool firstEntry = true;
1322897967deSJason M. Bills             while (std::getline(logStream, logEntry))
1323897967deSJason M. Bills             {
1324897967deSJason M. Bills                 std::string idStr;
1325897967deSJason M. Bills                 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1326897967deSJason M. Bills                 {
1327897967deSJason M. Bills                     continue;
1328897967deSJason M. Bills                 }
1329897967deSJason M. Bills 
1330897967deSJason M. Bills                 if (firstEntry)
1331897967deSJason M. Bills                 {
1332897967deSJason M. Bills                     firstEntry = false;
1333897967deSJason M. Bills                 }
1334897967deSJason M. Bills 
1335897967deSJason M. Bills                 if (idStr == targetID)
1336897967deSJason M. Bills                 {
1337897967deSJason M. Bills                     if (fillEventLogEntryJson(idStr, logEntry,
1338897967deSJason M. Bills                                               asyncResp->res.jsonValue) != 0)
1339897967deSJason M. Bills                     {
1340897967deSJason M. Bills                         messages::internalError(asyncResp->res);
1341897967deSJason M. Bills                         return;
1342897967deSJason M. Bills                     }
1343897967deSJason M. Bills                     return;
1344897967deSJason M. Bills                 }
1345897967deSJason M. Bills             }
1346897967deSJason M. Bills         }
1347897967deSJason M. Bills         // Requested ID was not found
1348897967deSJason M. Bills         messages::resourceMissingAtURI(asyncResp->res, targetID);
1349897967deSJason M. Bills     }
1350897967deSJason M. Bills };
1351897967deSJason M. Bills 
135208a4e4b5SAnthony Wilson class DBusEventLogEntryCollection : public Node
135308a4e4b5SAnthony Wilson {
135408a4e4b5SAnthony Wilson   public:
135552cc112dSEd Tanous     DBusEventLogEntryCollection(App& app) :
135608a4e4b5SAnthony Wilson         Node(app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
135708a4e4b5SAnthony Wilson     {
135808a4e4b5SAnthony Wilson         entityPrivileges = {
135908a4e4b5SAnthony Wilson             {boost::beast::http::verb::get, {{"Login"}}},
136008a4e4b5SAnthony Wilson             {boost::beast::http::verb::head, {{"Login"}}},
136108a4e4b5SAnthony Wilson             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
136208a4e4b5SAnthony Wilson             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
136308a4e4b5SAnthony Wilson             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
136408a4e4b5SAnthony Wilson             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
136508a4e4b5SAnthony Wilson     }
136608a4e4b5SAnthony Wilson 
136708a4e4b5SAnthony Wilson   private:
1368cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
1369cb13a392SEd Tanous                const std::vector<std::string>&) override
137008a4e4b5SAnthony Wilson     {
137108a4e4b5SAnthony Wilson         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
137208a4e4b5SAnthony Wilson 
137308a4e4b5SAnthony Wilson         // Collections don't include the static data added by SubRoute because
137408a4e4b5SAnthony Wilson         // it has a duplicate entry for members
137508a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["@odata.type"] =
137608a4e4b5SAnthony Wilson             "#LogEntryCollection.LogEntryCollection";
137708a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["@odata.id"] =
137808a4e4b5SAnthony Wilson             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
137908a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
138008a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["Description"] =
138108a4e4b5SAnthony Wilson             "Collection of System Event Log Entries";
138208a4e4b5SAnthony Wilson 
1383cb92c03bSAndrew Geissler         // DBus implementation of EventLog/Entries
1384cb92c03bSAndrew Geissler         // Make call to Logging Service to find all log entry objects
1385cb92c03bSAndrew Geissler         crow::connections::systemBus->async_method_call(
1386cb92c03bSAndrew Geissler             [asyncResp](const boost::system::error_code ec,
1387cb92c03bSAndrew Geissler                         GetManagedObjectsType& resp) {
1388cb92c03bSAndrew Geissler                 if (ec)
1389cb92c03bSAndrew Geissler                 {
1390cb92c03bSAndrew Geissler                     // TODO Handle for specific error code
1391cb92c03bSAndrew Geissler                     BMCWEB_LOG_ERROR
1392cb92c03bSAndrew Geissler                         << "getLogEntriesIfaceData resp_handler got error "
1393cb92c03bSAndrew Geissler                         << ec;
1394cb92c03bSAndrew Geissler                     messages::internalError(asyncResp->res);
1395cb92c03bSAndrew Geissler                     return;
1396cb92c03bSAndrew Geissler                 }
1397cb92c03bSAndrew Geissler                 nlohmann::json& entriesArray =
1398cb92c03bSAndrew Geissler                     asyncResp->res.jsonValue["Members"];
1399cb92c03bSAndrew Geissler                 entriesArray = nlohmann::json::array();
1400cb92c03bSAndrew Geissler                 for (auto& objectPath : resp)
1401cb92c03bSAndrew Geissler                 {
140266664f25SEd Tanous                     uint32_t* id = nullptr;
140366664f25SEd Tanous                     std::time_t timestamp{};
1404d139c236SGeorge Liu                     std::time_t updateTimestamp{};
140566664f25SEd Tanous                     std::string* severity = nullptr;
140666664f25SEd Tanous                     std::string* message = nullptr;
1407f86bb901SAdriana Kobylak                     std::string* filePath = nullptr;
140875710de2SXiaochao Ma                     bool resolved = false;
1409f86bb901SAdriana Kobylak                     for (auto& interfaceMap : objectPath.second)
1410f86bb901SAdriana Kobylak                     {
1411f86bb901SAdriana Kobylak                         if (interfaceMap.first ==
1412f86bb901SAdriana Kobylak                             "xyz.openbmc_project.Logging.Entry")
1413f86bb901SAdriana Kobylak                         {
1414cb92c03bSAndrew Geissler                             for (auto& propertyMap : interfaceMap.second)
1415cb92c03bSAndrew Geissler                             {
1416cb92c03bSAndrew Geissler                                 if (propertyMap.first == "Id")
1417cb92c03bSAndrew Geissler                                 {
1418f86bb901SAdriana Kobylak                                     id = std::get_if<uint32_t>(
1419f86bb901SAdriana Kobylak                                         &propertyMap.second);
1420cb92c03bSAndrew Geissler                                 }
1421cb92c03bSAndrew Geissler                                 else if (propertyMap.first == "Timestamp")
1422cb92c03bSAndrew Geissler                                 {
1423cb92c03bSAndrew Geissler                                     const uint64_t* millisTimeStamp =
1424f86bb901SAdriana Kobylak                                         std::get_if<uint64_t>(
1425f86bb901SAdriana Kobylak                                             &propertyMap.second);
1426ae34c8e8SAdriana Kobylak                                     if (millisTimeStamp != nullptr)
1427ebd45906SGeorge Liu                                     {
1428d139c236SGeorge Liu                                         timestamp = crow::utility::getTimestamp(
1429cb92c03bSAndrew Geissler                                             *millisTimeStamp);
1430d139c236SGeorge Liu                                     }
1431ebd45906SGeorge Liu                                 }
1432d139c236SGeorge Liu                                 else if (propertyMap.first == "UpdateTimestamp")
1433d139c236SGeorge Liu                                 {
1434d139c236SGeorge Liu                                     const uint64_t* millisTimeStamp =
1435f86bb901SAdriana Kobylak                                         std::get_if<uint64_t>(
1436f86bb901SAdriana Kobylak                                             &propertyMap.second);
1437ae34c8e8SAdriana Kobylak                                     if (millisTimeStamp != nullptr)
1438ebd45906SGeorge Liu                                     {
1439ebd45906SGeorge Liu                                         updateTimestamp =
1440ebd45906SGeorge Liu                                             crow::utility::getTimestamp(
1441d139c236SGeorge Liu                                                 *millisTimeStamp);
1442cb92c03bSAndrew Geissler                                     }
1443ebd45906SGeorge Liu                                 }
1444cb92c03bSAndrew Geissler                                 else if (propertyMap.first == "Severity")
1445cb92c03bSAndrew Geissler                                 {
1446cb92c03bSAndrew Geissler                                     severity = std::get_if<std::string>(
1447cb92c03bSAndrew Geissler                                         &propertyMap.second);
1448cb92c03bSAndrew Geissler                                 }
1449cb92c03bSAndrew Geissler                                 else if (propertyMap.first == "Message")
1450cb92c03bSAndrew Geissler                                 {
1451cb92c03bSAndrew Geissler                                     message = std::get_if<std::string>(
1452cb92c03bSAndrew Geissler                                         &propertyMap.second);
1453ae34c8e8SAdriana Kobylak                                 }
145475710de2SXiaochao Ma                                 else if (propertyMap.first == "Resolved")
145575710de2SXiaochao Ma                                 {
145675710de2SXiaochao Ma                                     bool* resolveptr =
145775710de2SXiaochao Ma                                         std::get_if<bool>(&propertyMap.second);
145875710de2SXiaochao Ma                                     if (resolveptr == nullptr)
145975710de2SXiaochao Ma                                     {
146075710de2SXiaochao Ma                                         messages::internalError(asyncResp->res);
146175710de2SXiaochao Ma                                         return;
146275710de2SXiaochao Ma                                     }
146375710de2SXiaochao Ma                                     resolved = *resolveptr;
146475710de2SXiaochao Ma                                 }
1465ae34c8e8SAdriana Kobylak                             }
1466ae34c8e8SAdriana Kobylak                             if (id == nullptr || message == nullptr ||
1467ae34c8e8SAdriana Kobylak                                 severity == nullptr)
1468cb92c03bSAndrew Geissler                             {
14690f1d8ed9SAsmitha Karunanithi                                 messages::internalError(asyncResp->res);
1470ae34c8e8SAdriana Kobylak                                 return;
1471cb92c03bSAndrew Geissler                             }
1472f86bb901SAdriana Kobylak                         }
1473f86bb901SAdriana Kobylak                         else if (interfaceMap.first ==
1474f86bb901SAdriana Kobylak                                  "xyz.openbmc_project.Common.FilePath")
1475f86bb901SAdriana Kobylak                         {
1476f86bb901SAdriana Kobylak                             for (auto& propertyMap : interfaceMap.second)
1477f86bb901SAdriana Kobylak                             {
1478f86bb901SAdriana Kobylak                                 if (propertyMap.first == "Path")
1479f86bb901SAdriana Kobylak                                 {
1480f86bb901SAdriana Kobylak                                     filePath = std::get_if<std::string>(
1481f86bb901SAdriana Kobylak                                         &propertyMap.second);
1482f86bb901SAdriana Kobylak                                 }
1483f86bb901SAdriana Kobylak                             }
1484f86bb901SAdriana Kobylak                         }
1485f86bb901SAdriana Kobylak                     }
1486f86bb901SAdriana Kobylak                     // Object path without the xyz.openbmc_project.Logging.Entry
1487f86bb901SAdriana Kobylak                     // interface, ignore and continue.
1488f86bb901SAdriana Kobylak                     if (id == nullptr || message == nullptr ||
1489f86bb901SAdriana Kobylak                         severity == nullptr)
1490f86bb901SAdriana Kobylak                     {
1491f86bb901SAdriana Kobylak                         continue;
1492f86bb901SAdriana Kobylak                     }
1493f86bb901SAdriana Kobylak                     entriesArray.push_back({});
1494f86bb901SAdriana Kobylak                     nlohmann::json& thisEntry = entriesArray.back();
1495f86bb901SAdriana Kobylak                     thisEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
1496f86bb901SAdriana Kobylak                     thisEntry["@odata.id"] = "/redfish/v1/Systems/system/"
1497f86bb901SAdriana Kobylak                                              "LogServices/EventLog/Entries/" +
1498f86bb901SAdriana Kobylak                                              std::to_string(*id);
1499f86bb901SAdriana Kobylak                     thisEntry["Name"] = "System Event Log Entry";
1500f86bb901SAdriana Kobylak                     thisEntry["Id"] = std::to_string(*id);
1501f86bb901SAdriana Kobylak                     thisEntry["Message"] = *message;
1502f86bb901SAdriana Kobylak                     thisEntry["Resolved"] = resolved;
1503f86bb901SAdriana Kobylak                     thisEntry["EntryType"] = "Event";
1504f86bb901SAdriana Kobylak                     thisEntry["Severity"] =
1505f86bb901SAdriana Kobylak                         translateSeverityDbusToRedfish(*severity);
1506f86bb901SAdriana Kobylak                     thisEntry["Created"] =
1507f86bb901SAdriana Kobylak                         crow::utility::getDateTime(timestamp);
1508f86bb901SAdriana Kobylak                     thisEntry["Modified"] =
1509f86bb901SAdriana Kobylak                         crow::utility::getDateTime(updateTimestamp);
1510f86bb901SAdriana Kobylak                     if (filePath != nullptr)
1511f86bb901SAdriana Kobylak                     {
1512f86bb901SAdriana Kobylak                         thisEntry["AdditionalDataURI"] =
1513cb92c03bSAndrew Geissler                             "/redfish/v1/Systems/system/LogServices/EventLog/"
1514f86bb901SAdriana Kobylak                             "attachment/" +
1515f86bb901SAdriana Kobylak                             std::to_string(*id);
1516cb92c03bSAndrew Geissler                     }
1517cb92c03bSAndrew Geissler                 }
1518cb92c03bSAndrew Geissler                 std::sort(entriesArray.begin(), entriesArray.end(),
1519cb92c03bSAndrew Geissler                           [](const nlohmann::json& left,
1520cb92c03bSAndrew Geissler                              const nlohmann::json& right) {
1521cb92c03bSAndrew Geissler                               return (left["Id"] <= right["Id"]);
1522cb92c03bSAndrew Geissler                           });
1523cb92c03bSAndrew Geissler                 asyncResp->res.jsonValue["Members@odata.count"] =
1524cb92c03bSAndrew Geissler                     entriesArray.size();
1525cb92c03bSAndrew Geissler             },
1526cb92c03bSAndrew Geissler             "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
1527cb92c03bSAndrew Geissler             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1528c4bf6374SJason M. Bills     }
1529c4bf6374SJason M. Bills };
1530c4bf6374SJason M. Bills 
153108a4e4b5SAnthony Wilson class DBusEventLogEntry : public Node
1532c4bf6374SJason M. Bills {
1533c4bf6374SJason M. Bills   public:
153452cc112dSEd Tanous     DBusEventLogEntry(App& app) :
1535c4bf6374SJason M. Bills         Node(app,
1536029573d4SEd Tanous              "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/",
1537029573d4SEd Tanous              std::string())
1538c4bf6374SJason M. Bills     {
1539c4bf6374SJason M. Bills         entityPrivileges = {
1540c4bf6374SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1541c4bf6374SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1542c4bf6374SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1543c4bf6374SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1544c4bf6374SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1545c4bf6374SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1546c4bf6374SJason M. Bills     }
1547c4bf6374SJason M. Bills 
1548c4bf6374SJason M. Bills   private:
1549cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
1550c4bf6374SJason M. Bills                const std::vector<std::string>& params) override
1551c4bf6374SJason M. Bills     {
1552c4bf6374SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1553029573d4SEd Tanous         if (params.size() != 1)
1554c4bf6374SJason M. Bills         {
1555c4bf6374SJason M. Bills             messages::internalError(asyncResp->res);
1556c4bf6374SJason M. Bills             return;
1557c4bf6374SJason M. Bills         }
1558ae34c8e8SAdriana Kobylak         std::string entryID = params[0];
1559ae34c8e8SAdriana Kobylak         dbus::utility::escapePathForDbus(entryID);
1560cb92c03bSAndrew Geissler 
1561cb92c03bSAndrew Geissler         // DBus implementation of EventLog/Entries
1562cb92c03bSAndrew Geissler         // Make call to Logging Service to find all log entry objects
1563cb92c03bSAndrew Geissler         crow::connections::systemBus->async_method_call(
1564cb92c03bSAndrew Geissler             [asyncResp, entryID](const boost::system::error_code ec,
1565cb92c03bSAndrew Geissler                                  GetManagedPropertyType& resp) {
1566ae34c8e8SAdriana Kobylak                 if (ec.value() == EBADR)
1567ae34c8e8SAdriana Kobylak                 {
1568ae34c8e8SAdriana Kobylak                     messages::resourceNotFound(asyncResp->res, "EventLogEntry",
1569ae34c8e8SAdriana Kobylak                                                entryID);
1570ae34c8e8SAdriana Kobylak                     return;
1571ae34c8e8SAdriana Kobylak                 }
1572cb92c03bSAndrew Geissler                 if (ec)
1573cb92c03bSAndrew Geissler                 {
1574cb92c03bSAndrew Geissler                     BMCWEB_LOG_ERROR
1575cb92c03bSAndrew Geissler                         << "EventLogEntry (DBus) resp_handler got error " << ec;
1576cb92c03bSAndrew Geissler                     messages::internalError(asyncResp->res);
1577cb92c03bSAndrew Geissler                     return;
1578cb92c03bSAndrew Geissler                 }
157966664f25SEd Tanous                 uint32_t* id = nullptr;
158066664f25SEd Tanous                 std::time_t timestamp{};
1581d139c236SGeorge Liu                 std::time_t updateTimestamp{};
158266664f25SEd Tanous                 std::string* severity = nullptr;
158366664f25SEd Tanous                 std::string* message = nullptr;
1584f86bb901SAdriana Kobylak                 std::string* filePath = nullptr;
158575710de2SXiaochao Ma                 bool resolved = false;
1586d139c236SGeorge Liu 
1587cb92c03bSAndrew Geissler                 for (auto& propertyMap : resp)
1588cb92c03bSAndrew Geissler                 {
1589cb92c03bSAndrew Geissler                     if (propertyMap.first == "Id")
1590cb92c03bSAndrew Geissler                     {
1591cb92c03bSAndrew Geissler                         id = std::get_if<uint32_t>(&propertyMap.second);
1592cb92c03bSAndrew Geissler                     }
1593cb92c03bSAndrew Geissler                     else if (propertyMap.first == "Timestamp")
1594cb92c03bSAndrew Geissler                     {
1595cb92c03bSAndrew Geissler                         const uint64_t* millisTimeStamp =
1596cb92c03bSAndrew Geissler                             std::get_if<uint64_t>(&propertyMap.second);
1597ae34c8e8SAdriana Kobylak                         if (millisTimeStamp != nullptr)
1598ebd45906SGeorge Liu                         {
1599cb92c03bSAndrew Geissler                             timestamp =
1600d139c236SGeorge Liu                                 crow::utility::getTimestamp(*millisTimeStamp);
1601d139c236SGeorge Liu                         }
1602ebd45906SGeorge Liu                     }
1603d139c236SGeorge Liu                     else if (propertyMap.first == "UpdateTimestamp")
1604d139c236SGeorge Liu                     {
1605d139c236SGeorge Liu                         const uint64_t* millisTimeStamp =
1606d139c236SGeorge Liu                             std::get_if<uint64_t>(&propertyMap.second);
1607ae34c8e8SAdriana Kobylak                         if (millisTimeStamp != nullptr)
1608ebd45906SGeorge Liu                         {
1609d139c236SGeorge Liu                             updateTimestamp =
1610d139c236SGeorge Liu                                 crow::utility::getTimestamp(*millisTimeStamp);
1611cb92c03bSAndrew Geissler                         }
1612ebd45906SGeorge Liu                     }
1613cb92c03bSAndrew Geissler                     else if (propertyMap.first == "Severity")
1614cb92c03bSAndrew Geissler                     {
1615cb92c03bSAndrew Geissler                         severity =
1616cb92c03bSAndrew Geissler                             std::get_if<std::string>(&propertyMap.second);
1617cb92c03bSAndrew Geissler                     }
1618cb92c03bSAndrew Geissler                     else if (propertyMap.first == "Message")
1619cb92c03bSAndrew Geissler                     {
1620cb92c03bSAndrew Geissler                         message = std::get_if<std::string>(&propertyMap.second);
1621cb92c03bSAndrew Geissler                     }
162275710de2SXiaochao Ma                     else if (propertyMap.first == "Resolved")
162375710de2SXiaochao Ma                     {
162475710de2SXiaochao Ma                         bool* resolveptr =
162575710de2SXiaochao Ma                             std::get_if<bool>(&propertyMap.second);
162675710de2SXiaochao Ma                         if (resolveptr == nullptr)
162775710de2SXiaochao Ma                         {
162875710de2SXiaochao Ma                             messages::internalError(asyncResp->res);
162975710de2SXiaochao Ma                             return;
163075710de2SXiaochao Ma                         }
163175710de2SXiaochao Ma                         resolved = *resolveptr;
163275710de2SXiaochao Ma                     }
1633f86bb901SAdriana Kobylak                     else if (propertyMap.first == "Path")
1634f86bb901SAdriana Kobylak                     {
1635f86bb901SAdriana Kobylak                         filePath =
1636f86bb901SAdriana Kobylak                             std::get_if<std::string>(&propertyMap.second);
1637f86bb901SAdriana Kobylak                     }
1638cb92c03bSAndrew Geissler                 }
1639271584abSEd Tanous                 if (id == nullptr || message == nullptr || severity == nullptr)
1640271584abSEd Tanous                 {
1641ae34c8e8SAdriana Kobylak                     messages::internalError(asyncResp->res);
1642271584abSEd Tanous                     return;
1643271584abSEd Tanous                 }
1644f86bb901SAdriana Kobylak                 asyncResp->res.jsonValue["@odata.type"] =
1645f86bb901SAdriana Kobylak                     "#LogEntry.v1_8_0.LogEntry";
1646f86bb901SAdriana Kobylak                 asyncResp->res.jsonValue["@odata.id"] =
1647f86bb901SAdriana Kobylak                     "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1648f86bb901SAdriana Kobylak                     std::to_string(*id);
1649f86bb901SAdriana Kobylak                 asyncResp->res.jsonValue["Name"] = "System Event Log Entry";
1650f86bb901SAdriana Kobylak                 asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1651f86bb901SAdriana Kobylak                 asyncResp->res.jsonValue["Message"] = *message;
1652f86bb901SAdriana Kobylak                 asyncResp->res.jsonValue["Resolved"] = resolved;
1653f86bb901SAdriana Kobylak                 asyncResp->res.jsonValue["EntryType"] = "Event";
1654f86bb901SAdriana Kobylak                 asyncResp->res.jsonValue["Severity"] =
1655f86bb901SAdriana Kobylak                     translateSeverityDbusToRedfish(*severity);
1656f86bb901SAdriana Kobylak                 asyncResp->res.jsonValue["Created"] =
1657f86bb901SAdriana Kobylak                     crow::utility::getDateTime(timestamp);
1658f86bb901SAdriana Kobylak                 asyncResp->res.jsonValue["Modified"] =
1659f86bb901SAdriana Kobylak                     crow::utility::getDateTime(updateTimestamp);
1660f86bb901SAdriana Kobylak                 if (filePath != nullptr)
1661f86bb901SAdriana Kobylak                 {
1662f86bb901SAdriana Kobylak                     asyncResp->res.jsonValue["AdditionalDataURI"] =
1663cb92c03bSAndrew Geissler                         "/redfish/v1/Systems/system/LogServices/EventLog/"
1664f86bb901SAdriana Kobylak                         "attachment/" +
1665f86bb901SAdriana Kobylak                         std::to_string(*id);
1666f86bb901SAdriana Kobylak                 }
1667cb92c03bSAndrew Geissler             },
1668cb92c03bSAndrew Geissler             "xyz.openbmc_project.Logging",
1669cb92c03bSAndrew Geissler             "/xyz/openbmc_project/logging/entry/" + entryID,
1670f86bb901SAdriana Kobylak             "org.freedesktop.DBus.Properties", "GetAll", "");
1671c4bf6374SJason M. Bills     }
1672336e96c6SChicago Duan 
167375710de2SXiaochao Ma     void doPatch(crow::Response& res, const crow::Request& req,
167475710de2SXiaochao Ma                  const std::vector<std::string>& params) override
167575710de2SXiaochao Ma     {
167675710de2SXiaochao Ma         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
167775710de2SXiaochao Ma 
167875710de2SXiaochao Ma         if (params.size() != 1)
167975710de2SXiaochao Ma         {
168075710de2SXiaochao Ma             messages::internalError(asyncResp->res);
168175710de2SXiaochao Ma             return;
168275710de2SXiaochao Ma         }
168375710de2SXiaochao Ma         std::string entryId = params[0];
168475710de2SXiaochao Ma 
168575710de2SXiaochao Ma         std::optional<bool> resolved;
168675710de2SXiaochao Ma 
168775710de2SXiaochao Ma         if (!json_util::readJson(req, res, "Resolved", resolved))
168875710de2SXiaochao Ma         {
168975710de2SXiaochao Ma             return;
169075710de2SXiaochao Ma         }
169175710de2SXiaochao Ma 
169275710de2SXiaochao Ma         if (resolved)
169375710de2SXiaochao Ma         {
169475710de2SXiaochao Ma             BMCWEB_LOG_DEBUG << "Set Resolved";
169575710de2SXiaochao Ma 
169675710de2SXiaochao Ma             crow::connections::systemBus->async_method_call(
169775710de2SXiaochao Ma                 [asyncResp, resolved,
169875710de2SXiaochao Ma                  entryId](const boost::system::error_code ec) {
169975710de2SXiaochao Ma                     if (ec)
170075710de2SXiaochao Ma                     {
170175710de2SXiaochao Ma                         BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
170275710de2SXiaochao Ma                         messages::internalError(asyncResp->res);
170375710de2SXiaochao Ma                         return;
170475710de2SXiaochao Ma                     }
170575710de2SXiaochao Ma                 },
170675710de2SXiaochao Ma                 "xyz.openbmc_project.Logging",
170775710de2SXiaochao Ma                 "/xyz/openbmc_project/logging/entry/" + entryId,
170875710de2SXiaochao Ma                 "org.freedesktop.DBus.Properties", "Set",
170975710de2SXiaochao Ma                 "xyz.openbmc_project.Logging.Entry", "Resolved",
171075710de2SXiaochao Ma                 std::variant<bool>(*resolved));
171175710de2SXiaochao Ma         }
171275710de2SXiaochao Ma     }
171375710de2SXiaochao Ma 
1714cb13a392SEd Tanous     void doDelete(crow::Response& res, const crow::Request&,
1715336e96c6SChicago Duan                   const std::vector<std::string>& params) override
1716336e96c6SChicago Duan     {
1717336e96c6SChicago Duan 
1718336e96c6SChicago Duan         BMCWEB_LOG_DEBUG << "Do delete single event entries.";
1719336e96c6SChicago Duan 
1720336e96c6SChicago Duan         auto asyncResp = std::make_shared<AsyncResp>(res);
1721336e96c6SChicago Duan 
1722336e96c6SChicago Duan         if (params.size() != 1)
1723336e96c6SChicago Duan         {
1724336e96c6SChicago Duan             messages::internalError(asyncResp->res);
1725336e96c6SChicago Duan             return;
1726336e96c6SChicago Duan         }
1727336e96c6SChicago Duan         std::string entryID = params[0];
1728336e96c6SChicago Duan 
1729336e96c6SChicago Duan         dbus::utility::escapePathForDbus(entryID);
1730336e96c6SChicago Duan 
1731336e96c6SChicago Duan         // Process response from Logging service.
17323de8d8baSGeorge Liu         auto respHandler = [asyncResp,
17333de8d8baSGeorge Liu                             entryID](const boost::system::error_code ec) {
1734336e96c6SChicago Duan             BMCWEB_LOG_DEBUG << "EventLogEntry (DBus) doDelete callback: Done";
1735336e96c6SChicago Duan             if (ec)
1736336e96c6SChicago Duan             {
17373de8d8baSGeorge Liu                 if (ec.value() == EBADR)
17383de8d8baSGeorge Liu                 {
17393de8d8baSGeorge Liu                     messages::resourceNotFound(asyncResp->res, "LogEntry",
17403de8d8baSGeorge Liu                                                entryID);
17413de8d8baSGeorge Liu                     return;
17423de8d8baSGeorge Liu                 }
1743336e96c6SChicago Duan                 // TODO Handle for specific error code
1744336e96c6SChicago Duan                 BMCWEB_LOG_ERROR
1745336e96c6SChicago Duan                     << "EventLogEntry (DBus) doDelete respHandler got error "
1746336e96c6SChicago Duan                     << ec;
1747336e96c6SChicago Duan                 asyncResp->res.result(
1748336e96c6SChicago Duan                     boost::beast::http::status::internal_server_error);
1749336e96c6SChicago Duan                 return;
1750336e96c6SChicago Duan             }
1751336e96c6SChicago Duan 
1752336e96c6SChicago Duan             asyncResp->res.result(boost::beast::http::status::ok);
1753336e96c6SChicago Duan         };
1754336e96c6SChicago Duan 
1755336e96c6SChicago Duan         // Make call to Logging service to request Delete Log
1756336e96c6SChicago Duan         crow::connections::systemBus->async_method_call(
1757336e96c6SChicago Duan             respHandler, "xyz.openbmc_project.Logging",
1758336e96c6SChicago Duan             "/xyz/openbmc_project/logging/entry/" + entryID,
1759336e96c6SChicago Duan             "xyz.openbmc_project.Object.Delete", "Delete");
1760336e96c6SChicago Duan     }
1761c4bf6374SJason M. Bills };
1762c4bf6374SJason M. Bills 
1763*400fd1fbSAdriana Kobylak class DBusEventLogEntryDownload : public Node
1764*400fd1fbSAdriana Kobylak {
1765*400fd1fbSAdriana Kobylak   public:
1766*400fd1fbSAdriana Kobylak     DBusEventLogEntryDownload(App& app) :
1767*400fd1fbSAdriana Kobylak         Node(
1768*400fd1fbSAdriana Kobylak             app,
1769*400fd1fbSAdriana Kobylak             "/redfish/v1/Systems/system/LogServices/EventLog/attachment/<str>/",
1770*400fd1fbSAdriana Kobylak             std::string())
1771*400fd1fbSAdriana Kobylak     {
1772*400fd1fbSAdriana Kobylak         entityPrivileges = {
1773*400fd1fbSAdriana Kobylak             {boost::beast::http::verb::get, {{"Login"}}},
1774*400fd1fbSAdriana Kobylak             {boost::beast::http::verb::head, {{"Login"}}},
1775*400fd1fbSAdriana Kobylak             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1776*400fd1fbSAdriana Kobylak             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1777*400fd1fbSAdriana Kobylak             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1778*400fd1fbSAdriana Kobylak             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1779*400fd1fbSAdriana Kobylak     }
1780*400fd1fbSAdriana Kobylak 
1781*400fd1fbSAdriana Kobylak   private:
1782*400fd1fbSAdriana Kobylak     void doGet(crow::Response& res, const crow::Request& req,
1783*400fd1fbSAdriana Kobylak                const std::vector<std::string>& params) override
1784*400fd1fbSAdriana Kobylak     {
1785*400fd1fbSAdriana Kobylak         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1786*400fd1fbSAdriana Kobylak         if (params.size() != 1)
1787*400fd1fbSAdriana Kobylak         {
1788*400fd1fbSAdriana Kobylak             messages::internalError(asyncResp->res);
1789*400fd1fbSAdriana Kobylak             return;
1790*400fd1fbSAdriana Kobylak         }
1791*400fd1fbSAdriana Kobylak 
1792*400fd1fbSAdriana Kobylak         std::string_view acceptHeader = req.getHeaderValue("Accept");
1793*400fd1fbSAdriana Kobylak         // The iterators in boost/http/rfc7230.hpp end the string if '/' is
1794*400fd1fbSAdriana Kobylak         // found, so replace it with arbitrary character '|' which is not part
1795*400fd1fbSAdriana Kobylak         // of the Accept header syntax.
1796*400fd1fbSAdriana Kobylak         std::string acceptStr =
1797*400fd1fbSAdriana Kobylak             boost::replace_all_copy(std::string(acceptHeader), "/", "|");
1798*400fd1fbSAdriana Kobylak         boost::beast::http::ext_list acceptTypes{acceptStr};
1799*400fd1fbSAdriana Kobylak         bool supported = false;
1800*400fd1fbSAdriana Kobylak         for (const auto& type : acceptTypes)
1801*400fd1fbSAdriana Kobylak         {
1802*400fd1fbSAdriana Kobylak             if ((type.first == "*|*") ||
1803*400fd1fbSAdriana Kobylak                 (type.first == "application|octet-stream"))
1804*400fd1fbSAdriana Kobylak             {
1805*400fd1fbSAdriana Kobylak                 supported = true;
1806*400fd1fbSAdriana Kobylak                 break;
1807*400fd1fbSAdriana Kobylak             }
1808*400fd1fbSAdriana Kobylak         }
1809*400fd1fbSAdriana Kobylak         if (!supported)
1810*400fd1fbSAdriana Kobylak         {
1811*400fd1fbSAdriana Kobylak             asyncResp->res.result(boost::beast::http::status::bad_request);
1812*400fd1fbSAdriana Kobylak             return;
1813*400fd1fbSAdriana Kobylak         }
1814*400fd1fbSAdriana Kobylak 
1815*400fd1fbSAdriana Kobylak         std::string entryID = params[0];
1816*400fd1fbSAdriana Kobylak         dbus::utility::escapePathForDbus(entryID);
1817*400fd1fbSAdriana Kobylak 
1818*400fd1fbSAdriana Kobylak         crow::connections::systemBus->async_method_call(
1819*400fd1fbSAdriana Kobylak             [asyncResp, entryID](const boost::system::error_code ec,
1820*400fd1fbSAdriana Kobylak                                  const sdbusplus::message::unix_fd& unixfd) {
1821*400fd1fbSAdriana Kobylak                 if (ec.value() == EBADR)
1822*400fd1fbSAdriana Kobylak                 {
1823*400fd1fbSAdriana Kobylak                     messages::resourceNotFound(asyncResp->res,
1824*400fd1fbSAdriana Kobylak                                                "EventLogAttachment", entryID);
1825*400fd1fbSAdriana Kobylak                     return;
1826*400fd1fbSAdriana Kobylak                 }
1827*400fd1fbSAdriana Kobylak                 if (ec)
1828*400fd1fbSAdriana Kobylak                 {
1829*400fd1fbSAdriana Kobylak                     BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1830*400fd1fbSAdriana Kobylak                     messages::internalError(asyncResp->res);
1831*400fd1fbSAdriana Kobylak                     return;
1832*400fd1fbSAdriana Kobylak                 }
1833*400fd1fbSAdriana Kobylak 
1834*400fd1fbSAdriana Kobylak                 int fd = -1;
1835*400fd1fbSAdriana Kobylak                 fd = dup(unixfd);
1836*400fd1fbSAdriana Kobylak                 if (fd == -1)
1837*400fd1fbSAdriana Kobylak                 {
1838*400fd1fbSAdriana Kobylak                     messages::internalError(asyncResp->res);
1839*400fd1fbSAdriana Kobylak                     return;
1840*400fd1fbSAdriana Kobylak                 }
1841*400fd1fbSAdriana Kobylak 
1842*400fd1fbSAdriana Kobylak                 long long int size = lseek(fd, 0, SEEK_END);
1843*400fd1fbSAdriana Kobylak                 if (size == -1)
1844*400fd1fbSAdriana Kobylak                 {
1845*400fd1fbSAdriana Kobylak                     messages::internalError(asyncResp->res);
1846*400fd1fbSAdriana Kobylak                     return;
1847*400fd1fbSAdriana Kobylak                 }
1848*400fd1fbSAdriana Kobylak 
1849*400fd1fbSAdriana Kobylak                 // Arbitrary max size of 64kb
1850*400fd1fbSAdriana Kobylak                 constexpr int maxFileSize = 65536;
1851*400fd1fbSAdriana Kobylak                 if (size > maxFileSize)
1852*400fd1fbSAdriana Kobylak                 {
1853*400fd1fbSAdriana Kobylak                     BMCWEB_LOG_ERROR
1854*400fd1fbSAdriana Kobylak                         << "File size exceeds maximum allowed size of "
1855*400fd1fbSAdriana Kobylak                         << maxFileSize;
1856*400fd1fbSAdriana Kobylak                     messages::internalError(asyncResp->res);
1857*400fd1fbSAdriana Kobylak                     return;
1858*400fd1fbSAdriana Kobylak                 }
1859*400fd1fbSAdriana Kobylak                 std::vector<char> data(static_cast<size_t>(size));
1860*400fd1fbSAdriana Kobylak                 long long int rc = lseek(fd, 0, SEEK_SET);
1861*400fd1fbSAdriana Kobylak                 if (rc == -1)
1862*400fd1fbSAdriana Kobylak                 {
1863*400fd1fbSAdriana Kobylak                     messages::internalError(asyncResp->res);
1864*400fd1fbSAdriana Kobylak                     return;
1865*400fd1fbSAdriana Kobylak                 }
1866*400fd1fbSAdriana Kobylak                 rc = read(fd, data.data(), data.size());
1867*400fd1fbSAdriana Kobylak                 if ((rc == -1) || (rc != size))
1868*400fd1fbSAdriana Kobylak                 {
1869*400fd1fbSAdriana Kobylak                     messages::internalError(asyncResp->res);
1870*400fd1fbSAdriana Kobylak                     return;
1871*400fd1fbSAdriana Kobylak                 }
1872*400fd1fbSAdriana Kobylak                 close(fd);
1873*400fd1fbSAdriana Kobylak 
1874*400fd1fbSAdriana Kobylak                 std::string_view strData(data.data(), data.size());
1875*400fd1fbSAdriana Kobylak                 std::string output = crow::utility::base64encode(strData);
1876*400fd1fbSAdriana Kobylak 
1877*400fd1fbSAdriana Kobylak                 asyncResp->res.addHeader("Content-Type",
1878*400fd1fbSAdriana Kobylak                                          "application/octet-stream");
1879*400fd1fbSAdriana Kobylak                 asyncResp->res.addHeader("Content-Transfer-Encoding", "Base64");
1880*400fd1fbSAdriana Kobylak                 asyncResp->res.body() = std::move(output);
1881*400fd1fbSAdriana Kobylak             },
1882*400fd1fbSAdriana Kobylak             "xyz.openbmc_project.Logging",
1883*400fd1fbSAdriana Kobylak             "/xyz/openbmc_project/logging/entry/" + entryID,
1884*400fd1fbSAdriana Kobylak             "xyz.openbmc_project.Logging.Entry", "GetEntry");
1885*400fd1fbSAdriana Kobylak     }
1886*400fd1fbSAdriana Kobylak };
1887*400fd1fbSAdriana Kobylak 
1888c4bf6374SJason M. Bills class BMCLogServiceCollection : public Node
1889c4bf6374SJason M. Bills {
1890c4bf6374SJason M. Bills   public:
189152cc112dSEd Tanous     BMCLogServiceCollection(App& app) :
18924ed77cd5SEd Tanous         Node(app, "/redfish/v1/Managers/bmc/LogServices/")
18931da66f75SEd Tanous     {
18941da66f75SEd Tanous         entityPrivileges = {
1895e1f26343SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1896e1f26343SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1897e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1898e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1899e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1900e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
19011da66f75SEd Tanous     }
19021da66f75SEd Tanous 
19031da66f75SEd Tanous   private:
19041da66f75SEd Tanous     /**
19051da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
19061da66f75SEd Tanous      */
1907cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
1908cb13a392SEd Tanous                const std::vector<std::string>&) override
19091da66f75SEd Tanous     {
1910e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
19111da66f75SEd Tanous         // Collections don't include the static data added by SubRoute because
19121da66f75SEd Tanous         // it has a duplicate entry for members
1913e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
19141da66f75SEd Tanous             "#LogServiceCollection.LogServiceCollection";
1915e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
1916e1f26343SJason M. Bills             "/redfish/v1/Managers/bmc/LogServices";
1917e1f26343SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Open BMC Log Services Collection";
1918e1f26343SJason M. Bills         asyncResp->res.jsonValue["Description"] =
19191da66f75SEd Tanous             "Collection of LogServices for this Manager";
1920c4bf6374SJason M. Bills         nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
1921c4bf6374SJason M. Bills         logServiceArray = nlohmann::json::array();
19225cb1dd27SAsmitha Karunanithi #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
19235cb1dd27SAsmitha Karunanithi         logServiceArray.push_back(
19245cb1dd27SAsmitha Karunanithi             {{"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Dump"}});
19255cb1dd27SAsmitha Karunanithi #endif
1926c4bf6374SJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
1927c4bf6374SJason M. Bills         logServiceArray.push_back(
192808a4e4b5SAnthony Wilson             {{"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Journal"}});
1929c4bf6374SJason M. Bills #endif
1930e1f26343SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] =
1931c4bf6374SJason M. Bills             logServiceArray.size();
19321da66f75SEd Tanous     }
19331da66f75SEd Tanous };
19341da66f75SEd Tanous 
1935c4bf6374SJason M. Bills class BMCJournalLogService : public Node
19361da66f75SEd Tanous {
19371da66f75SEd Tanous   public:
193852cc112dSEd Tanous     BMCJournalLogService(App& app) :
1939c4bf6374SJason M. Bills         Node(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
1940e1f26343SJason M. Bills     {
1941e1f26343SJason M. Bills         entityPrivileges = {
1942e1f26343SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1943e1f26343SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1944e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1945e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1946e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1947e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1948e1f26343SJason M. Bills     }
1949e1f26343SJason M. Bills 
1950e1f26343SJason M. Bills   private:
1951cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
1952cb13a392SEd Tanous                const std::vector<std::string>&) override
1953e1f26343SJason M. Bills     {
1954e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1955e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1956e1f26343SJason M. Bills             "#LogService.v1_1_0.LogService";
19570f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
19580f74e643SEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal";
1959c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Open BMC Journal Log Service";
1960c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] = "BMC Journal Log Service";
1961c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Id"] = "BMC Journal";
1962e1f26343SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
1963cd50aa42SJason M. Bills         asyncResp->res.jsonValue["Entries"] = {
1964cd50aa42SJason M. Bills             {"@odata.id",
1965086be238SEd Tanous              "/redfish/v1/Managers/bmc/LogServices/Journal/Entries"}};
1966e1f26343SJason M. Bills     }
1967e1f26343SJason M. Bills };
1968e1f26343SJason M. Bills 
1969c4bf6374SJason M. Bills static int fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
1970e1f26343SJason M. Bills                                       sd_journal* journal,
1971c4bf6374SJason M. Bills                                       nlohmann::json& bmcJournalLogEntryJson)
1972e1f26343SJason M. Bills {
1973e1f26343SJason M. Bills     // Get the Log Entry contents
1974e1f26343SJason M. Bills     int ret = 0;
1975e1f26343SJason M. Bills 
1976a8fe54f0SJason M. Bills     std::string message;
1977a8fe54f0SJason M. Bills     std::string_view syslogID;
1978a8fe54f0SJason M. Bills     ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
1979a8fe54f0SJason M. Bills     if (ret < 0)
1980a8fe54f0SJason M. Bills     {
1981a8fe54f0SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read SYSLOG_IDENTIFIER field: "
1982a8fe54f0SJason M. Bills                          << strerror(-ret);
1983a8fe54f0SJason M. Bills     }
1984a8fe54f0SJason M. Bills     if (!syslogID.empty())
1985a8fe54f0SJason M. Bills     {
1986a8fe54f0SJason M. Bills         message += std::string(syslogID) + ": ";
1987a8fe54f0SJason M. Bills     }
1988a8fe54f0SJason M. Bills 
198939e77504SEd Tanous     std::string_view msg;
199016428a1aSJason M. Bills     ret = getJournalMetadata(journal, "MESSAGE", msg);
1991e1f26343SJason M. Bills     if (ret < 0)
1992e1f26343SJason M. Bills     {
1993e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
1994e1f26343SJason M. Bills         return 1;
1995e1f26343SJason M. Bills     }
1996a8fe54f0SJason M. Bills     message += std::string(msg);
1997e1f26343SJason M. Bills 
1998e1f26343SJason M. Bills     // Get the severity from the PRIORITY field
1999271584abSEd Tanous     long int severity = 8; // Default to an invalid priority
200016428a1aSJason M. Bills     ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
2001e1f26343SJason M. Bills     if (ret < 0)
2002e1f26343SJason M. Bills     {
2003e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
2004e1f26343SJason M. Bills     }
2005e1f26343SJason M. Bills 
2006e1f26343SJason M. Bills     // Get the Created time from the timestamp
200716428a1aSJason M. Bills     std::string entryTimeStr;
200816428a1aSJason M. Bills     if (!getEntryTimestamp(journal, entryTimeStr))
2009e1f26343SJason M. Bills     {
201016428a1aSJason M. Bills         return 1;
2011e1f26343SJason M. Bills     }
2012e1f26343SJason M. Bills 
2013e1f26343SJason M. Bills     // Fill in the log entry with the gathered data
2014c4bf6374SJason M. Bills     bmcJournalLogEntryJson = {
2015cb92c03bSAndrew Geissler         {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
2016c4bf6374SJason M. Bills         {"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/" +
2017c4bf6374SJason M. Bills                           bmcJournalLogEntryID},
2018e1f26343SJason M. Bills         {"Name", "BMC Journal Entry"},
2019c4bf6374SJason M. Bills         {"Id", bmcJournalLogEntryID},
2020a8fe54f0SJason M. Bills         {"Message", std::move(message)},
2021e1f26343SJason M. Bills         {"EntryType", "Oem"},
2022738c1e61SPatrick Williams         {"Severity", severity <= 2   ? "Critical"
2023738c1e61SPatrick Williams                      : severity <= 4 ? "Warning"
2024738c1e61SPatrick Williams                                      : "OK"},
2025086be238SEd Tanous         {"OemRecordFormat", "BMC Journal Entry"},
2026e1f26343SJason M. Bills         {"Created", std::move(entryTimeStr)}};
2027e1f26343SJason M. Bills     return 0;
2028e1f26343SJason M. Bills }
2029e1f26343SJason M. Bills 
2030c4bf6374SJason M. Bills class BMCJournalLogEntryCollection : public Node
2031e1f26343SJason M. Bills {
2032e1f26343SJason M. Bills   public:
203352cc112dSEd Tanous     BMCJournalLogEntryCollection(App& app) :
2034c4bf6374SJason M. Bills         Node(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
2035e1f26343SJason M. Bills     {
2036e1f26343SJason M. Bills         entityPrivileges = {
2037e1f26343SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
2038e1f26343SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
2039e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2040e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2041e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2042e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
2043e1f26343SJason M. Bills     }
2044e1f26343SJason M. Bills 
2045e1f26343SJason M. Bills   private:
2046e1f26343SJason M. Bills     void doGet(crow::Response& res, const crow::Request& req,
2047cb13a392SEd Tanous                const std::vector<std::string>&) override
2048e1f26343SJason M. Bills     {
2049e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
2050193ad2faSJason M. Bills         static constexpr const long maxEntriesPerPage = 1000;
2051271584abSEd Tanous         uint64_t skip = 0;
2052271584abSEd Tanous         uint64_t top = maxEntriesPerPage; // Show max entries by default
205316428a1aSJason M. Bills         if (!getSkipParam(asyncResp->res, req, skip))
2054193ad2faSJason M. Bills         {
2055193ad2faSJason M. Bills             return;
2056193ad2faSJason M. Bills         }
205716428a1aSJason M. Bills         if (!getTopParam(asyncResp->res, req, top))
2058193ad2faSJason M. Bills         {
2059193ad2faSJason M. Bills             return;
2060193ad2faSJason M. Bills         }
2061e1f26343SJason M. Bills         // Collections don't include the static data added by SubRoute because
2062e1f26343SJason M. Bills         // it has a duplicate entry for members
2063e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
2064e1f26343SJason M. Bills             "#LogEntryCollection.LogEntryCollection";
20650f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
20660f74e643SEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2067e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
2068c4bf6374SJason M. Bills             "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2069e1f26343SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
2070e1f26343SJason M. Bills         asyncResp->res.jsonValue["Description"] =
2071e1f26343SJason M. Bills             "Collection of BMC Journal Entries";
20720f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
20730f74e643SEd Tanous             "/redfish/v1/Managers/bmc/LogServices/BmcLog/Entries";
2074e1f26343SJason M. Bills         nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2075e1f26343SJason M. Bills         logEntryArray = nlohmann::json::array();
2076e1f26343SJason M. Bills 
2077e1f26343SJason M. Bills         // Go through the journal and use the timestamp to create a unique ID
2078e1f26343SJason M. Bills         // for each entry
2079e1f26343SJason M. Bills         sd_journal* journalTmp = nullptr;
2080e1f26343SJason M. Bills         int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2081e1f26343SJason M. Bills         if (ret < 0)
2082e1f26343SJason M. Bills         {
2083e1f26343SJason M. Bills             BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2084f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2085e1f26343SJason M. Bills             return;
2086e1f26343SJason M. Bills         }
2087e1f26343SJason M. Bills         std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2088e1f26343SJason M. Bills             journalTmp, sd_journal_close);
2089e1f26343SJason M. Bills         journalTmp = nullptr;
2090b01bf299SEd Tanous         uint64_t entryCount = 0;
2091e85d6b16SJason M. Bills         // Reset the unique ID on the first entry
2092e85d6b16SJason M. Bills         bool firstEntry = true;
2093e1f26343SJason M. Bills         SD_JOURNAL_FOREACH(journal.get())
2094e1f26343SJason M. Bills         {
2095193ad2faSJason M. Bills             entryCount++;
2096193ad2faSJason M. Bills             // Handle paging using skip (number of entries to skip from the
2097193ad2faSJason M. Bills             // start) and top (number of entries to display)
2098193ad2faSJason M. Bills             if (entryCount <= skip || entryCount > skip + top)
2099193ad2faSJason M. Bills             {
2100193ad2faSJason M. Bills                 continue;
2101193ad2faSJason M. Bills             }
2102193ad2faSJason M. Bills 
210316428a1aSJason M. Bills             std::string idStr;
2104e85d6b16SJason M. Bills             if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2105e1f26343SJason M. Bills             {
2106e1f26343SJason M. Bills                 continue;
2107e1f26343SJason M. Bills             }
2108e1f26343SJason M. Bills 
2109e85d6b16SJason M. Bills             if (firstEntry)
2110e85d6b16SJason M. Bills             {
2111e85d6b16SJason M. Bills                 firstEntry = false;
2112e85d6b16SJason M. Bills             }
2113e85d6b16SJason M. Bills 
2114e1f26343SJason M. Bills             logEntryArray.push_back({});
2115c4bf6374SJason M. Bills             nlohmann::json& bmcJournalLogEntry = logEntryArray.back();
2116c4bf6374SJason M. Bills             if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2117c4bf6374SJason M. Bills                                            bmcJournalLogEntry) != 0)
2118e1f26343SJason M. Bills             {
2119f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
2120e1f26343SJason M. Bills                 return;
2121e1f26343SJason M. Bills             }
2122e1f26343SJason M. Bills         }
2123193ad2faSJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
2124193ad2faSJason M. Bills         if (skip + top < entryCount)
2125193ad2faSJason M. Bills         {
2126193ad2faSJason M. Bills             asyncResp->res.jsonValue["Members@odata.nextLink"] =
2127c4bf6374SJason M. Bills                 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
2128193ad2faSJason M. Bills                 std::to_string(skip + top);
2129193ad2faSJason M. Bills         }
2130e1f26343SJason M. Bills     }
2131e1f26343SJason M. Bills };
2132e1f26343SJason M. Bills 
2133c4bf6374SJason M. Bills class BMCJournalLogEntry : public Node
2134e1f26343SJason M. Bills {
2135e1f26343SJason M. Bills   public:
213652cc112dSEd Tanous     BMCJournalLogEntry(App& app) :
2137c4bf6374SJason M. Bills         Node(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/",
2138e1f26343SJason M. Bills              std::string())
2139e1f26343SJason M. Bills     {
2140e1f26343SJason M. Bills         entityPrivileges = {
2141e1f26343SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
2142e1f26343SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
2143e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2144e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2145e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2146e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
2147e1f26343SJason M. Bills     }
2148e1f26343SJason M. Bills 
2149e1f26343SJason M. Bills   private:
2150cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
2151e1f26343SJason M. Bills                const std::vector<std::string>& params) override
2152e1f26343SJason M. Bills     {
2153e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
2154e1f26343SJason M. Bills         if (params.size() != 1)
2155e1f26343SJason M. Bills         {
2156f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2157e1f26343SJason M. Bills             return;
2158e1f26343SJason M. Bills         }
215916428a1aSJason M. Bills         const std::string& entryID = params[0];
2160e1f26343SJason M. Bills         // Convert the unique ID back to a timestamp to find the entry
2161e1f26343SJason M. Bills         uint64_t ts = 0;
2162271584abSEd Tanous         uint64_t index = 0;
216316428a1aSJason M. Bills         if (!getTimestampFromID(asyncResp->res, entryID, ts, index))
2164e1f26343SJason M. Bills         {
216516428a1aSJason M. Bills             return;
2166e1f26343SJason M. Bills         }
2167e1f26343SJason M. Bills 
2168e1f26343SJason M. Bills         sd_journal* journalTmp = nullptr;
2169e1f26343SJason M. Bills         int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2170e1f26343SJason M. Bills         if (ret < 0)
2171e1f26343SJason M. Bills         {
2172e1f26343SJason M. Bills             BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2173f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2174e1f26343SJason M. Bills             return;
2175e1f26343SJason M. Bills         }
2176e1f26343SJason M. Bills         std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2177e1f26343SJason M. Bills             journalTmp, sd_journal_close);
2178e1f26343SJason M. Bills         journalTmp = nullptr;
2179e1f26343SJason M. Bills         // Go to the timestamp in the log and move to the entry at the index
2180af07e3f5SJason M. Bills         // tracking the unique ID
2181af07e3f5SJason M. Bills         std::string idStr;
2182af07e3f5SJason M. Bills         bool firstEntry = true;
2183e1f26343SJason M. Bills         ret = sd_journal_seek_realtime_usec(journal.get(), ts);
21842056b6d1SManojkiran Eda         if (ret < 0)
21852056b6d1SManojkiran Eda         {
21862056b6d1SManojkiran Eda             BMCWEB_LOG_ERROR << "failed to seek to an entry in journal"
21872056b6d1SManojkiran Eda                              << strerror(-ret);
21882056b6d1SManojkiran Eda             messages::internalError(asyncResp->res);
21892056b6d1SManojkiran Eda             return;
21902056b6d1SManojkiran Eda         }
2191271584abSEd Tanous         for (uint64_t i = 0; i <= index; i++)
2192e1f26343SJason M. Bills         {
2193e1f26343SJason M. Bills             sd_journal_next(journal.get());
2194af07e3f5SJason M. Bills             if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2195af07e3f5SJason M. Bills             {
2196af07e3f5SJason M. Bills                 messages::internalError(asyncResp->res);
2197af07e3f5SJason M. Bills                 return;
2198af07e3f5SJason M. Bills             }
2199af07e3f5SJason M. Bills             if (firstEntry)
2200af07e3f5SJason M. Bills             {
2201af07e3f5SJason M. Bills                 firstEntry = false;
2202af07e3f5SJason M. Bills             }
2203e1f26343SJason M. Bills         }
2204c4bf6374SJason M. Bills         // Confirm that the entry ID matches what was requested
2205af07e3f5SJason M. Bills         if (idStr != entryID)
2206c4bf6374SJason M. Bills         {
2207c4bf6374SJason M. Bills             messages::resourceMissingAtURI(asyncResp->res, entryID);
2208c4bf6374SJason M. Bills             return;
2209c4bf6374SJason M. Bills         }
2210c4bf6374SJason M. Bills 
2211c4bf6374SJason M. Bills         if (fillBMCJournalLogEntryJson(entryID, journal.get(),
2212e1f26343SJason M. Bills                                        asyncResp->res.jsonValue) != 0)
2213e1f26343SJason M. Bills         {
2214f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2215e1f26343SJason M. Bills             return;
2216e1f26343SJason M. Bills         }
2217e1f26343SJason M. Bills     }
2218e1f26343SJason M. Bills };
2219e1f26343SJason M. Bills 
22205cb1dd27SAsmitha Karunanithi class BMCDumpService : public Node
2221c9bb6861Sraviteja-b {
2222c9bb6861Sraviteja-b   public:
222352cc112dSEd Tanous     BMCDumpService(App& app) :
22245cb1dd27SAsmitha Karunanithi         Node(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
2225c9bb6861Sraviteja-b     {
2226c9bb6861Sraviteja-b         entityPrivileges = {
2227c9bb6861Sraviteja-b             {boost::beast::http::verb::get, {{"Login"}}},
2228c9bb6861Sraviteja-b             {boost::beast::http::verb::head, {{"Login"}}},
2229c9bb6861Sraviteja-b             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2230c9bb6861Sraviteja-b             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2231c9bb6861Sraviteja-b             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2232c9bb6861Sraviteja-b             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
2233c9bb6861Sraviteja-b     }
2234c9bb6861Sraviteja-b 
2235c9bb6861Sraviteja-b   private:
2236cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
2237cb13a392SEd Tanous                const std::vector<std::string>&) override
2238c9bb6861Sraviteja-b     {
2239c9bb6861Sraviteja-b         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
2240c9bb6861Sraviteja-b 
2241c9bb6861Sraviteja-b         asyncResp->res.jsonValue["@odata.id"] =
22425cb1dd27SAsmitha Karunanithi             "/redfish/v1/Managers/bmc/LogServices/Dump";
2243c9bb6861Sraviteja-b         asyncResp->res.jsonValue["@odata.type"] =
2244d337bb72SAsmitha Karunanithi             "#LogService.v1_2_0.LogService";
2245c9bb6861Sraviteja-b         asyncResp->res.jsonValue["Name"] = "Dump LogService";
22465cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["Description"] = "BMC Dump LogService";
22475cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["Id"] = "Dump";
2248c9bb6861Sraviteja-b         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
2249c9bb6861Sraviteja-b         asyncResp->res.jsonValue["Entries"] = {
22505cb1dd27SAsmitha Karunanithi             {"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Dump/Entries"}};
22515cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["Actions"] = {
22525cb1dd27SAsmitha Karunanithi             {"#LogService.ClearLog",
22535cb1dd27SAsmitha Karunanithi              {{"target", "/redfish/v1/Managers/bmc/LogServices/Dump/"
22545cb1dd27SAsmitha Karunanithi                          "Actions/LogService.ClearLog"}}},
2255d337bb72SAsmitha Karunanithi             {"#LogService.CollectDiagnosticData",
2256d337bb72SAsmitha Karunanithi              {{"target", "/redfish/v1/Managers/bmc/LogServices/Dump/"
2257d337bb72SAsmitha Karunanithi                          "Actions/LogService.CollectDiagnosticData"}}}};
2258c9bb6861Sraviteja-b     }
2259c9bb6861Sraviteja-b };
2260c9bb6861Sraviteja-b 
22615cb1dd27SAsmitha Karunanithi class BMCDumpEntryCollection : public Node
2262c9bb6861Sraviteja-b {
2263c9bb6861Sraviteja-b   public:
226452cc112dSEd Tanous     BMCDumpEntryCollection(App& app) :
22655cb1dd27SAsmitha Karunanithi         Node(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
2266c9bb6861Sraviteja-b     {
2267c9bb6861Sraviteja-b         entityPrivileges = {
2268c9bb6861Sraviteja-b             {boost::beast::http::verb::get, {{"Login"}}},
2269c9bb6861Sraviteja-b             {boost::beast::http::verb::head, {{"Login"}}},
2270c9bb6861Sraviteja-b             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2271c9bb6861Sraviteja-b             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2272c9bb6861Sraviteja-b             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2273c9bb6861Sraviteja-b             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
2274c9bb6861Sraviteja-b     }
2275c9bb6861Sraviteja-b 
2276c9bb6861Sraviteja-b   private:
2277c9bb6861Sraviteja-b     /**
2278c9bb6861Sraviteja-b      * Functions triggers appropriate requests on DBus
2279c9bb6861Sraviteja-b      */
2280cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
2281cb13a392SEd Tanous                const std::vector<std::string>&) override
2282c9bb6861Sraviteja-b     {
2283c9bb6861Sraviteja-b         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
2284c9bb6861Sraviteja-b 
2285c9bb6861Sraviteja-b         asyncResp->res.jsonValue["@odata.type"] =
2286c9bb6861Sraviteja-b             "#LogEntryCollection.LogEntryCollection";
2287c9bb6861Sraviteja-b         asyncResp->res.jsonValue["@odata.id"] =
22885cb1dd27SAsmitha Karunanithi             "/redfish/v1/Managers/bmc/LogServices/Dump/Entries";
22895cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["Name"] = "BMC Dump Entries";
2290c9bb6861Sraviteja-b         asyncResp->res.jsonValue["Description"] =
22915cb1dd27SAsmitha Karunanithi             "Collection of BMC Dump Entries";
2292c9bb6861Sraviteja-b 
22935cb1dd27SAsmitha Karunanithi         getDumpEntryCollection(asyncResp, "BMC");
2294c9bb6861Sraviteja-b     }
2295c9bb6861Sraviteja-b };
2296c9bb6861Sraviteja-b 
22975cb1dd27SAsmitha Karunanithi class BMCDumpEntry : public Node
2298c9bb6861Sraviteja-b {
2299c9bb6861Sraviteja-b   public:
230052cc112dSEd Tanous     BMCDumpEntry(App& app) :
23015cb1dd27SAsmitha Karunanithi         Node(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/",
2302c9bb6861Sraviteja-b              std::string())
2303c9bb6861Sraviteja-b     {
2304c9bb6861Sraviteja-b         entityPrivileges = {
2305c9bb6861Sraviteja-b             {boost::beast::http::verb::get, {{"Login"}}},
2306c9bb6861Sraviteja-b             {boost::beast::http::verb::head, {{"Login"}}},
2307c9bb6861Sraviteja-b             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2308c9bb6861Sraviteja-b             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2309c9bb6861Sraviteja-b             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2310c9bb6861Sraviteja-b             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
2311c9bb6861Sraviteja-b     }
2312c9bb6861Sraviteja-b 
2313c9bb6861Sraviteja-b   private:
2314cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
2315c9bb6861Sraviteja-b                const std::vector<std::string>& params) override
2316c9bb6861Sraviteja-b     {
2317c9bb6861Sraviteja-b         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
2318c9bb6861Sraviteja-b         if (params.size() != 1)
2319c9bb6861Sraviteja-b         {
2320c9bb6861Sraviteja-b             messages::internalError(asyncResp->res);
2321c9bb6861Sraviteja-b             return;
2322c9bb6861Sraviteja-b         }
23235cb1dd27SAsmitha Karunanithi         getDumpEntryById(asyncResp, params[0], "BMC");
2324c9bb6861Sraviteja-b     }
2325c9bb6861Sraviteja-b 
2326cb13a392SEd Tanous     void doDelete(crow::Response& res, const crow::Request&,
2327c9bb6861Sraviteja-b                   const std::vector<std::string>& params) override
2328c9bb6861Sraviteja-b     {
23295cb1dd27SAsmitha Karunanithi         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
2330c9bb6861Sraviteja-b         if (params.size() != 1)
2331c9bb6861Sraviteja-b         {
2332c9bb6861Sraviteja-b             messages::internalError(asyncResp->res);
2333c9bb6861Sraviteja-b             return;
2334c9bb6861Sraviteja-b         }
23359878256fSStanley Chu         deleteDumpEntry(asyncResp, params[0], "bmc");
23365cb1dd27SAsmitha Karunanithi     }
23375cb1dd27SAsmitha Karunanithi };
2338c9bb6861Sraviteja-b 
2339a43be80fSAsmitha Karunanithi class BMCDumpCreate : public Node
2340a43be80fSAsmitha Karunanithi {
2341a43be80fSAsmitha Karunanithi   public:
234252cc112dSEd Tanous     BMCDumpCreate(App& app) :
2343a43be80fSAsmitha Karunanithi         Node(app, "/redfish/v1/Managers/bmc/LogServices/Dump/"
2344d337bb72SAsmitha Karunanithi                   "Actions/"
2345d337bb72SAsmitha Karunanithi                   "LogService.CollectDiagnosticData/")
2346a43be80fSAsmitha Karunanithi     {
2347a43be80fSAsmitha Karunanithi         entityPrivileges = {
2348a43be80fSAsmitha Karunanithi             {boost::beast::http::verb::get, {{"Login"}}},
2349a43be80fSAsmitha Karunanithi             {boost::beast::http::verb::head, {{"Login"}}},
2350a43be80fSAsmitha Karunanithi             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2351a43be80fSAsmitha Karunanithi             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2352a43be80fSAsmitha Karunanithi             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2353a43be80fSAsmitha Karunanithi             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
2354a43be80fSAsmitha Karunanithi     }
2355a43be80fSAsmitha Karunanithi 
2356a43be80fSAsmitha Karunanithi   private:
2357a43be80fSAsmitha Karunanithi     void doPost(crow::Response& res, const crow::Request& req,
2358cb13a392SEd Tanous                 const std::vector<std::string>&) override
2359a43be80fSAsmitha Karunanithi     {
2360a43be80fSAsmitha Karunanithi         createDump(res, req, "BMC");
2361a43be80fSAsmitha Karunanithi     }
2362a43be80fSAsmitha Karunanithi };
2363a43be80fSAsmitha Karunanithi 
236480319af1SAsmitha Karunanithi class BMCDumpClear : public Node
236580319af1SAsmitha Karunanithi {
236680319af1SAsmitha Karunanithi   public:
236752cc112dSEd Tanous     BMCDumpClear(App& app) :
236880319af1SAsmitha Karunanithi         Node(app, "/redfish/v1/Managers/bmc/LogServices/Dump/"
236980319af1SAsmitha Karunanithi                   "Actions/"
237080319af1SAsmitha Karunanithi                   "LogService.ClearLog/")
237180319af1SAsmitha Karunanithi     {
237280319af1SAsmitha Karunanithi         entityPrivileges = {
237380319af1SAsmitha Karunanithi             {boost::beast::http::verb::get, {{"Login"}}},
237480319af1SAsmitha Karunanithi             {boost::beast::http::verb::head, {{"Login"}}},
237580319af1SAsmitha Karunanithi             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
237680319af1SAsmitha Karunanithi             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
237780319af1SAsmitha Karunanithi             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
237880319af1SAsmitha Karunanithi             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
237980319af1SAsmitha Karunanithi     }
238080319af1SAsmitha Karunanithi 
238180319af1SAsmitha Karunanithi   private:
2382cb13a392SEd Tanous     void doPost(crow::Response& res, const crow::Request&,
2383cb13a392SEd Tanous                 const std::vector<std::string>&) override
238480319af1SAsmitha Karunanithi     {
2385b47452b2SAsmitha Karunanithi         clearDump(res, "BMC");
238680319af1SAsmitha Karunanithi     }
238780319af1SAsmitha Karunanithi };
238880319af1SAsmitha Karunanithi 
23895cb1dd27SAsmitha Karunanithi class SystemDumpService : public Node
2390c9bb6861Sraviteja-b {
23915cb1dd27SAsmitha Karunanithi   public:
239252cc112dSEd Tanous     SystemDumpService(App& app) :
23935cb1dd27SAsmitha Karunanithi         Node(app, "/redfish/v1/Systems/system/LogServices/Dump/")
23945cb1dd27SAsmitha Karunanithi     {
23955cb1dd27SAsmitha Karunanithi         entityPrivileges = {
23965cb1dd27SAsmitha Karunanithi             {boost::beast::http::verb::get, {{"Login"}}},
23975cb1dd27SAsmitha Karunanithi             {boost::beast::http::verb::head, {{"Login"}}},
23985cb1dd27SAsmitha Karunanithi             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
23995cb1dd27SAsmitha Karunanithi             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
24005cb1dd27SAsmitha Karunanithi             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
24015cb1dd27SAsmitha Karunanithi             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
24025cb1dd27SAsmitha Karunanithi     }
24035cb1dd27SAsmitha Karunanithi 
24045cb1dd27SAsmitha Karunanithi   private:
2405cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
2406cb13a392SEd Tanous                const std::vector<std::string>&) override
24075cb1dd27SAsmitha Karunanithi     {
24085cb1dd27SAsmitha Karunanithi         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
24095cb1dd27SAsmitha Karunanithi 
24105cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["@odata.id"] =
24115cb1dd27SAsmitha Karunanithi             "/redfish/v1/Systems/system/LogServices/Dump";
24125cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["@odata.type"] =
2413d337bb72SAsmitha Karunanithi             "#LogService.v1_2_0.LogService";
24145cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["Name"] = "Dump LogService";
24155cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["Description"] = "System Dump LogService";
24165cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["Id"] = "Dump";
24175cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
24185cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["Entries"] = {
24195cb1dd27SAsmitha Karunanithi             {"@odata.id",
24205cb1dd27SAsmitha Karunanithi              "/redfish/v1/Systems/system/LogServices/Dump/Entries"}};
24215cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["Actions"] = {
24225cb1dd27SAsmitha Karunanithi             {"#LogService.ClearLog",
24235cb1dd27SAsmitha Karunanithi              {{"target", "/redfish/v1/Systems/system/LogServices/Dump/Actions/"
24245cb1dd27SAsmitha Karunanithi                          "LogService.ClearLog"}}},
2425d337bb72SAsmitha Karunanithi             {"#LogService.CollectDiagnosticData",
2426d337bb72SAsmitha Karunanithi              {{"target", "/redfish/v1/Systems/system/LogServices/Dump/Actions/"
2427d337bb72SAsmitha Karunanithi                          "LogService.CollectDiagnosticData"}}}};
24285cb1dd27SAsmitha Karunanithi     }
24295cb1dd27SAsmitha Karunanithi };
24305cb1dd27SAsmitha Karunanithi 
24315cb1dd27SAsmitha Karunanithi class SystemDumpEntryCollection : public Node
24325cb1dd27SAsmitha Karunanithi {
24335cb1dd27SAsmitha Karunanithi   public:
243452cc112dSEd Tanous     SystemDumpEntryCollection(App& app) :
24355cb1dd27SAsmitha Karunanithi         Node(app, "/redfish/v1/Systems/system/LogServices/Dump/Entries/")
24365cb1dd27SAsmitha Karunanithi     {
24375cb1dd27SAsmitha Karunanithi         entityPrivileges = {
24385cb1dd27SAsmitha Karunanithi             {boost::beast::http::verb::get, {{"Login"}}},
24395cb1dd27SAsmitha Karunanithi             {boost::beast::http::verb::head, {{"Login"}}},
24405cb1dd27SAsmitha Karunanithi             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
24415cb1dd27SAsmitha Karunanithi             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
24425cb1dd27SAsmitha Karunanithi             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
24435cb1dd27SAsmitha Karunanithi             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
24445cb1dd27SAsmitha Karunanithi     }
24455cb1dd27SAsmitha Karunanithi 
24465cb1dd27SAsmitha Karunanithi   private:
24475cb1dd27SAsmitha Karunanithi     /**
24485cb1dd27SAsmitha Karunanithi      * Functions triggers appropriate requests on DBus
24495cb1dd27SAsmitha Karunanithi      */
2450cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
2451cb13a392SEd Tanous                const std::vector<std::string>&) override
24525cb1dd27SAsmitha Karunanithi     {
24535cb1dd27SAsmitha Karunanithi         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
24545cb1dd27SAsmitha Karunanithi 
24555cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["@odata.type"] =
24565cb1dd27SAsmitha Karunanithi             "#LogEntryCollection.LogEntryCollection";
24575cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["@odata.id"] =
24585cb1dd27SAsmitha Karunanithi             "/redfish/v1/Systems/system/LogServices/Dump/Entries";
24595cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["Name"] = "System Dump Entries";
24605cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["Description"] =
24615cb1dd27SAsmitha Karunanithi             "Collection of System Dump Entries";
24625cb1dd27SAsmitha Karunanithi 
24635cb1dd27SAsmitha Karunanithi         getDumpEntryCollection(asyncResp, "System");
24645cb1dd27SAsmitha Karunanithi     }
24655cb1dd27SAsmitha Karunanithi };
24665cb1dd27SAsmitha Karunanithi 
24675cb1dd27SAsmitha Karunanithi class SystemDumpEntry : public Node
24685cb1dd27SAsmitha Karunanithi {
24695cb1dd27SAsmitha Karunanithi   public:
247052cc112dSEd Tanous     SystemDumpEntry(App& app) :
24715cb1dd27SAsmitha Karunanithi         Node(app, "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/",
24725cb1dd27SAsmitha Karunanithi              std::string())
24735cb1dd27SAsmitha Karunanithi     {
24745cb1dd27SAsmitha Karunanithi         entityPrivileges = {
24755cb1dd27SAsmitha Karunanithi             {boost::beast::http::verb::get, {{"Login"}}},
24765cb1dd27SAsmitha Karunanithi             {boost::beast::http::verb::head, {{"Login"}}},
24775cb1dd27SAsmitha Karunanithi             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
24785cb1dd27SAsmitha Karunanithi             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
24795cb1dd27SAsmitha Karunanithi             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
24805cb1dd27SAsmitha Karunanithi             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
24815cb1dd27SAsmitha Karunanithi     }
24825cb1dd27SAsmitha Karunanithi 
24835cb1dd27SAsmitha Karunanithi   private:
2484cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
24855cb1dd27SAsmitha Karunanithi                const std::vector<std::string>& params) override
24865cb1dd27SAsmitha Karunanithi     {
24875cb1dd27SAsmitha Karunanithi         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
24885cb1dd27SAsmitha Karunanithi         if (params.size() != 1)
24895cb1dd27SAsmitha Karunanithi         {
2490c9bb6861Sraviteja-b             messages::internalError(asyncResp->res);
2491c9bb6861Sraviteja-b             return;
2492c9bb6861Sraviteja-b         }
24935cb1dd27SAsmitha Karunanithi         getDumpEntryById(asyncResp, params[0], "System");
24945cb1dd27SAsmitha Karunanithi     }
2495c9bb6861Sraviteja-b 
2496cb13a392SEd Tanous     void doDelete(crow::Response& res, const crow::Request&,
24975cb1dd27SAsmitha Karunanithi                   const std::vector<std::string>& params) override
2498c9bb6861Sraviteja-b     {
24995cb1dd27SAsmitha Karunanithi         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
25005cb1dd27SAsmitha Karunanithi         if (params.size() != 1)
2501c9bb6861Sraviteja-b         {
25025cb1dd27SAsmitha Karunanithi             messages::internalError(asyncResp->res);
2503c9bb6861Sraviteja-b             return;
2504c9bb6861Sraviteja-b         }
25059878256fSStanley Chu         deleteDumpEntry(asyncResp, params[0], "system");
2506c9bb6861Sraviteja-b     }
2507c9bb6861Sraviteja-b };
2508c9bb6861Sraviteja-b 
2509a43be80fSAsmitha Karunanithi class SystemDumpCreate : public Node
2510a43be80fSAsmitha Karunanithi {
2511a43be80fSAsmitha Karunanithi   public:
251252cc112dSEd Tanous     SystemDumpCreate(App& app) :
2513a43be80fSAsmitha Karunanithi         Node(app, "/redfish/v1/Systems/system/LogServices/Dump/"
2514d337bb72SAsmitha Karunanithi                   "Actions/"
2515d337bb72SAsmitha Karunanithi                   "LogService.CollectDiagnosticData/")
2516a43be80fSAsmitha Karunanithi     {
2517a43be80fSAsmitha Karunanithi         entityPrivileges = {
2518a43be80fSAsmitha Karunanithi             {boost::beast::http::verb::get, {{"Login"}}},
2519a43be80fSAsmitha Karunanithi             {boost::beast::http::verb::head, {{"Login"}}},
2520a43be80fSAsmitha Karunanithi             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2521a43be80fSAsmitha Karunanithi             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2522a43be80fSAsmitha Karunanithi             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2523a43be80fSAsmitha Karunanithi             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
2524a43be80fSAsmitha Karunanithi     }
2525a43be80fSAsmitha Karunanithi 
2526a43be80fSAsmitha Karunanithi   private:
2527a43be80fSAsmitha Karunanithi     void doPost(crow::Response& res, const crow::Request& req,
2528cb13a392SEd Tanous                 const std::vector<std::string>&) override
2529a43be80fSAsmitha Karunanithi     {
2530a43be80fSAsmitha Karunanithi         createDump(res, req, "System");
2531a43be80fSAsmitha Karunanithi     }
2532a43be80fSAsmitha Karunanithi };
2533a43be80fSAsmitha Karunanithi 
2534013487e5Sraviteja-b class SystemDumpClear : public Node
2535013487e5Sraviteja-b {
2536013487e5Sraviteja-b   public:
253752cc112dSEd Tanous     SystemDumpClear(App& app) :
253880319af1SAsmitha Karunanithi         Node(app, "/redfish/v1/Systems/system/LogServices/Dump/"
2539013487e5Sraviteja-b                   "Actions/"
2540013487e5Sraviteja-b                   "LogService.ClearLog/")
2541013487e5Sraviteja-b     {
2542013487e5Sraviteja-b         entityPrivileges = {
2543013487e5Sraviteja-b             {boost::beast::http::verb::get, {{"Login"}}},
2544013487e5Sraviteja-b             {boost::beast::http::verb::head, {{"Login"}}},
254580319af1SAsmitha Karunanithi             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
254680319af1SAsmitha Karunanithi             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
254780319af1SAsmitha Karunanithi             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2548013487e5Sraviteja-b             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
2549013487e5Sraviteja-b     }
2550013487e5Sraviteja-b 
2551013487e5Sraviteja-b   private:
2552cb13a392SEd Tanous     void doPost(crow::Response& res, const crow::Request&,
2553cb13a392SEd Tanous                 const std::vector<std::string>&) override
2554013487e5Sraviteja-b     {
2555b47452b2SAsmitha Karunanithi         clearDump(res, "System");
2556013487e5Sraviteja-b     }
2557013487e5Sraviteja-b };
2558013487e5Sraviteja-b 
2559424c4176SJason M. Bills class CrashdumpService : public Node
2560e1f26343SJason M. Bills {
2561e1f26343SJason M. Bills   public:
256252cc112dSEd Tanous     CrashdumpService(App& app) :
2563424c4176SJason M. Bills         Node(app, "/redfish/v1/Systems/system/LogServices/Crashdump/")
25641da66f75SEd Tanous     {
25653946028dSAppaRao Puli         // Note: Deviated from redfish privilege registry for GET & HEAD
25663946028dSAppaRao Puli         // method for security reasons.
25671da66f75SEd Tanous         entityPrivileges = {
25683946028dSAppaRao Puli             {boost::beast::http::verb::get, {{"ConfigureComponents"}}},
25693946028dSAppaRao Puli             {boost::beast::http::verb::head, {{"ConfigureComponents"}}},
2570e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2571e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2572e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2573e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
25741da66f75SEd Tanous     }
25751da66f75SEd Tanous 
25761da66f75SEd Tanous   private:
25771da66f75SEd Tanous     /**
25781da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
25791da66f75SEd Tanous      */
2580cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
2581cb13a392SEd Tanous                const std::vector<std::string>&) override
25821da66f75SEd Tanous     {
2583e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
25841da66f75SEd Tanous         // Copy over the static data to include the entries added by SubRoute
25850f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
2586424c4176SJason M. Bills             "/redfish/v1/Systems/system/LogServices/Crashdump";
2587e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
25888e6c099aSJason M. Bills             "#LogService.v1_2_0.LogService";
25894f50ae4bSGunnar Mills         asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
25904f50ae4bSGunnar Mills         asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
25914f50ae4bSGunnar Mills         asyncResp->res.jsonValue["Id"] = "Oem Crashdump";
2592e1f26343SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
2593e1f26343SJason M. Bills         asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
2594cd50aa42SJason M. Bills         asyncResp->res.jsonValue["Entries"] = {
2595cd50aa42SJason M. Bills             {"@odata.id",
2596424c4176SJason M. Bills              "/redfish/v1/Systems/system/LogServices/Crashdump/Entries"}};
2597e1f26343SJason M. Bills         asyncResp->res.jsonValue["Actions"] = {
25985b61b5e8SJason M. Bills             {"#LogService.ClearLog",
25995b61b5e8SJason M. Bills              {{"target", "/redfish/v1/Systems/system/LogServices/Crashdump/"
26005b61b5e8SJason M. Bills                          "Actions/LogService.ClearLog"}}},
26018e6c099aSJason M. Bills             {"#LogService.CollectDiagnosticData",
2602424c4176SJason M. Bills              {{"target", "/redfish/v1/Systems/system/LogServices/Crashdump/"
26038e6c099aSJason M. Bills                          "Actions/LogService.CollectDiagnosticData"}}}};
26041da66f75SEd Tanous 
26051da66f75SEd Tanous #ifdef BMCWEB_ENABLE_REDFISH_RAW_PECI
26068e6c099aSJason M. Bills         asyncResp->res.jsonValue["Actions"]["Oem"] = {
2607424c4176SJason M. Bills             {"#Crashdump.SendRawPeci",
260808a4e4b5SAnthony Wilson              {{"target", "/redfish/v1/Systems/system/LogServices/Crashdump/"
26098e6c099aSJason M. Bills                          "Actions/Oem/Crashdump.SendRawPeci"}}}};
26101da66f75SEd Tanous #endif
26111da66f75SEd Tanous     }
26121da66f75SEd Tanous };
26131da66f75SEd Tanous 
26145b61b5e8SJason M. Bills class CrashdumpClear : public Node
26155b61b5e8SJason M. Bills {
26165b61b5e8SJason M. Bills   public:
261752cc112dSEd Tanous     CrashdumpClear(App& app) :
26185b61b5e8SJason M. Bills         Node(app, "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/"
26195b61b5e8SJason M. Bills                   "LogService.ClearLog/")
26205b61b5e8SJason M. Bills     {
26213946028dSAppaRao Puli         // Note: Deviated from redfish privilege registry for GET & HEAD
26223946028dSAppaRao Puli         // method for security reasons.
26235b61b5e8SJason M. Bills         entityPrivileges = {
26243946028dSAppaRao Puli             {boost::beast::http::verb::get, {{"ConfigureComponents"}}},
26253946028dSAppaRao Puli             {boost::beast::http::verb::head, {{"ConfigureComponents"}}},
26265b61b5e8SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
26275b61b5e8SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
26285b61b5e8SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
26295b61b5e8SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
26305b61b5e8SJason M. Bills     }
26315b61b5e8SJason M. Bills 
26325b61b5e8SJason M. Bills   private:
2633cb13a392SEd Tanous     void doPost(crow::Response& res, const crow::Request&,
2634cb13a392SEd Tanous                 const std::vector<std::string>&) override
26355b61b5e8SJason M. Bills     {
26365b61b5e8SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
26375b61b5e8SJason M. Bills 
26385b61b5e8SJason M. Bills         crow::connections::systemBus->async_method_call(
26395b61b5e8SJason M. Bills             [asyncResp](const boost::system::error_code ec,
2640cb13a392SEd Tanous                         const std::string&) {
26415b61b5e8SJason M. Bills                 if (ec)
26425b61b5e8SJason M. Bills                 {
26435b61b5e8SJason M. Bills                     messages::internalError(asyncResp->res);
26445b61b5e8SJason M. Bills                     return;
26455b61b5e8SJason M. Bills                 }
26465b61b5e8SJason M. Bills                 messages::success(asyncResp->res);
26475b61b5e8SJason M. Bills             },
26485b61b5e8SJason M. Bills             crashdumpObject, crashdumpPath, deleteAllInterface, "DeleteAll");
26495b61b5e8SJason M. Bills     }
26505b61b5e8SJason M. Bills };
26515b61b5e8SJason M. Bills 
2652b5a76932SEd Tanous static void logCrashdumpEntry(const std::shared_ptr<AsyncResp>& asyncResp,
2653e855dd28SJason M. Bills                               const std::string& logID,
2654e855dd28SJason M. Bills                               nlohmann::json& logEntryJson)
2655e855dd28SJason M. Bills {
2656043a0536SJohnathan Mantey     auto getStoredLogCallback =
2657043a0536SJohnathan Mantey         [asyncResp, logID, &logEntryJson](
2658e855dd28SJason M. Bills             const boost::system::error_code ec,
2659043a0536SJohnathan Mantey             const std::vector<std::pair<std::string, VariantType>>& params) {
2660e855dd28SJason M. Bills             if (ec)
2661e855dd28SJason M. Bills             {
2662e855dd28SJason M. Bills                 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
26631ddcf01aSJason M. Bills                 if (ec.value() ==
26641ddcf01aSJason M. Bills                     boost::system::linux_error::bad_request_descriptor)
26651ddcf01aSJason M. Bills                 {
2666043a0536SJohnathan Mantey                     messages::resourceNotFound(asyncResp->res, "LogEntry",
2667043a0536SJohnathan Mantey                                                logID);
26681ddcf01aSJason M. Bills                 }
26691ddcf01aSJason M. Bills                 else
26701ddcf01aSJason M. Bills                 {
2671e855dd28SJason M. Bills                     messages::internalError(asyncResp->res);
26721ddcf01aSJason M. Bills                 }
2673e855dd28SJason M. Bills                 return;
2674e855dd28SJason M. Bills             }
2675043a0536SJohnathan Mantey 
2676043a0536SJohnathan Mantey             std::string timestamp{};
2677043a0536SJohnathan Mantey             std::string filename{};
2678043a0536SJohnathan Mantey             std::string logfile{};
26792c70f800SEd Tanous             parseCrashdumpParameters(params, filename, timestamp, logfile);
2680043a0536SJohnathan Mantey 
2681043a0536SJohnathan Mantey             if (filename.empty() || timestamp.empty())
2682e855dd28SJason M. Bills             {
2683043a0536SJohnathan Mantey                 messages::resourceMissingAtURI(asyncResp->res, logID);
2684e855dd28SJason M. Bills                 return;
2685e855dd28SJason M. Bills             }
2686e855dd28SJason M. Bills 
2687043a0536SJohnathan Mantey             std::string crashdumpURI =
2688e855dd28SJason M. Bills                 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
2689043a0536SJohnathan Mantey                 logID + "/" + filename;
26908e6c099aSJason M. Bills             logEntryJson = {{"@odata.type", "#LogEntry.v1_7_0.LogEntry"},
2691043a0536SJohnathan Mantey                             {"@odata.id", "/redfish/v1/Systems/system/"
2692043a0536SJohnathan Mantey                                           "LogServices/Crashdump/Entries/" +
2693e855dd28SJason M. Bills                                               logID},
2694e855dd28SJason M. Bills                             {"Name", "CPU Crashdump"},
2695e855dd28SJason M. Bills                             {"Id", logID},
2696e855dd28SJason M. Bills                             {"EntryType", "Oem"},
26978e6c099aSJason M. Bills                             {"AdditionalDataURI", std::move(crashdumpURI)},
26988e6c099aSJason M. Bills                             {"DiagnosticDataType", "OEM"},
26998e6c099aSJason M. Bills                             {"OEMDiagnosticDataType", "PECICrashdump"},
2700043a0536SJohnathan Mantey                             {"Created", std::move(timestamp)}};
2701e855dd28SJason M. Bills         };
2702e855dd28SJason M. Bills     crow::connections::systemBus->async_method_call(
27035b61b5e8SJason M. Bills         std::move(getStoredLogCallback), crashdumpObject,
27045b61b5e8SJason M. Bills         crashdumpPath + std::string("/") + logID,
2705043a0536SJohnathan Mantey         "org.freedesktop.DBus.Properties", "GetAll", crashdumpInterface);
2706e855dd28SJason M. Bills }
2707e855dd28SJason M. Bills 
2708424c4176SJason M. Bills class CrashdumpEntryCollection : public Node
27091da66f75SEd Tanous {
27101da66f75SEd Tanous   public:
271152cc112dSEd Tanous     CrashdumpEntryCollection(App& app) :
2712424c4176SJason M. Bills         Node(app, "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/")
27131da66f75SEd Tanous     {
27143946028dSAppaRao Puli         // Note: Deviated from redfish privilege registry for GET & HEAD
27153946028dSAppaRao Puli         // method for security reasons.
27161da66f75SEd Tanous         entityPrivileges = {
27173946028dSAppaRao Puli             {boost::beast::http::verb::get, {{"ConfigureComponents"}}},
27183946028dSAppaRao Puli             {boost::beast::http::verb::head, {{"ConfigureComponents"}}},
2719e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2720e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2721e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2722e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
27231da66f75SEd Tanous     }
27241da66f75SEd Tanous 
27251da66f75SEd Tanous   private:
27261da66f75SEd Tanous     /**
27271da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
27281da66f75SEd Tanous      */
2729cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
2730cb13a392SEd Tanous                const std::vector<std::string>&) override
27311da66f75SEd Tanous     {
2732e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
27331da66f75SEd Tanous         // Collections don't include the static data added by SubRoute because
27341da66f75SEd Tanous         // it has a duplicate entry for members
2735e1f26343SJason M. Bills         auto getLogEntriesCallback = [asyncResp](
2736e1f26343SJason M. Bills                                          const boost::system::error_code ec,
27371da66f75SEd Tanous                                          const std::vector<std::string>& resp) {
27381da66f75SEd Tanous             if (ec)
27391da66f75SEd Tanous             {
27401da66f75SEd Tanous                 if (ec.value() !=
27411da66f75SEd Tanous                     boost::system::errc::no_such_file_or_directory)
27421da66f75SEd Tanous                 {
27431da66f75SEd Tanous                     BMCWEB_LOG_DEBUG << "failed to get entries ec: "
27441da66f75SEd Tanous                                      << ec.message();
2745f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
27461da66f75SEd Tanous                     return;
27471da66f75SEd Tanous                 }
27481da66f75SEd Tanous             }
2749e1f26343SJason M. Bills             asyncResp->res.jsonValue["@odata.type"] =
27501da66f75SEd Tanous                 "#LogEntryCollection.LogEntryCollection";
27510f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
2752424c4176SJason M. Bills                 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
2753424c4176SJason M. Bills             asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
2754e1f26343SJason M. Bills             asyncResp->res.jsonValue["Description"] =
2755424c4176SJason M. Bills                 "Collection of Crashdump Entries";
2756e1f26343SJason M. Bills             nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2757e1f26343SJason M. Bills             logEntryArray = nlohmann::json::array();
2758e855dd28SJason M. Bills             std::vector<std::string> logIDs;
2759e855dd28SJason M. Bills             // Get the list of log entries and build up an empty array big
2760e855dd28SJason M. Bills             // enough to hold them
27611da66f75SEd Tanous             for (const std::string& objpath : resp)
27621da66f75SEd Tanous             {
2763e855dd28SJason M. Bills                 // Get the log ID
2764f23b7296SEd Tanous                 std::size_t lastPos = objpath.rfind('/');
2765e855dd28SJason M. Bills                 if (lastPos == std::string::npos)
27661da66f75SEd Tanous                 {
2767e855dd28SJason M. Bills                     continue;
27681da66f75SEd Tanous                 }
2769e855dd28SJason M. Bills                 logIDs.emplace_back(objpath.substr(lastPos + 1));
2770e855dd28SJason M. Bills 
2771e855dd28SJason M. Bills                 // Add a space for the log entry to the array
2772e855dd28SJason M. Bills                 logEntryArray.push_back({});
2773e855dd28SJason M. Bills             }
2774e855dd28SJason M. Bills             // Now go through and set up async calls to fill in the entries
2775e855dd28SJason M. Bills             size_t index = 0;
2776e855dd28SJason M. Bills             for (const std::string& logID : logIDs)
2777e855dd28SJason M. Bills             {
2778e855dd28SJason M. Bills                 // Add the log entry to the array
2779e855dd28SJason M. Bills                 logCrashdumpEntry(asyncResp, logID, logEntryArray[index++]);
27801da66f75SEd Tanous             }
2781e1f26343SJason M. Bills             asyncResp->res.jsonValue["Members@odata.count"] =
2782e1f26343SJason M. Bills                 logEntryArray.size();
27831da66f75SEd Tanous         };
27841da66f75SEd Tanous         crow::connections::systemBus->async_method_call(
27851da66f75SEd Tanous             std::move(getLogEntriesCallback),
27861da66f75SEd Tanous             "xyz.openbmc_project.ObjectMapper",
27871da66f75SEd Tanous             "/xyz/openbmc_project/object_mapper",
27881da66f75SEd Tanous             "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "", 0,
27895b61b5e8SJason M. Bills             std::array<const char*, 1>{crashdumpInterface});
27901da66f75SEd Tanous     }
27911da66f75SEd Tanous };
27921da66f75SEd Tanous 
2793424c4176SJason M. Bills class CrashdumpEntry : public Node
27941da66f75SEd Tanous {
27951da66f75SEd Tanous   public:
279652cc112dSEd Tanous     CrashdumpEntry(App& app) :
2797d53dd41fSJason M. Bills         Node(app,
2798424c4176SJason M. Bills              "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/",
27991da66f75SEd Tanous              std::string())
28001da66f75SEd Tanous     {
28013946028dSAppaRao Puli         // Note: Deviated from redfish privilege registry for GET & HEAD
28023946028dSAppaRao Puli         // method for security reasons.
28031da66f75SEd Tanous         entityPrivileges = {
28043946028dSAppaRao Puli             {boost::beast::http::verb::get, {{"ConfigureComponents"}}},
28053946028dSAppaRao Puli             {boost::beast::http::verb::head, {{"ConfigureComponents"}}},
2806e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2807e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2808e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2809e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
28101da66f75SEd Tanous     }
28111da66f75SEd Tanous 
28121da66f75SEd Tanous   private:
2813cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
28141da66f75SEd Tanous                const std::vector<std::string>& params) override
28151da66f75SEd Tanous     {
2816e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
28171da66f75SEd Tanous         if (params.size() != 1)
28181da66f75SEd Tanous         {
2819f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
28201da66f75SEd Tanous             return;
28211da66f75SEd Tanous         }
2822e855dd28SJason M. Bills         const std::string& logID = params[0];
2823e855dd28SJason M. Bills         logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
2824e855dd28SJason M. Bills     }
2825e855dd28SJason M. Bills };
2826e855dd28SJason M. Bills 
2827e855dd28SJason M. Bills class CrashdumpFile : public Node
2828e855dd28SJason M. Bills {
2829e855dd28SJason M. Bills   public:
283052cc112dSEd Tanous     CrashdumpFile(App& app) :
2831e855dd28SJason M. Bills         Node(app,
2832e855dd28SJason M. Bills              "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/"
2833e855dd28SJason M. Bills              "<str>/",
2834e855dd28SJason M. Bills              std::string(), std::string())
2835e855dd28SJason M. Bills     {
28363946028dSAppaRao Puli         // Note: Deviated from redfish privilege registry for GET & HEAD
28373946028dSAppaRao Puli         // method for security reasons.
2838e855dd28SJason M. Bills         entityPrivileges = {
28393946028dSAppaRao Puli             {boost::beast::http::verb::get, {{"ConfigureComponents"}}},
28403946028dSAppaRao Puli             {boost::beast::http::verb::head, {{"ConfigureComponents"}}},
2841e855dd28SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2842e855dd28SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2843e855dd28SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2844e855dd28SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
2845e855dd28SJason M. Bills     }
2846e855dd28SJason M. Bills 
2847e855dd28SJason M. Bills   private:
2848cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
2849e855dd28SJason M. Bills                const std::vector<std::string>& params) override
2850e855dd28SJason M. Bills     {
2851e855dd28SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
2852e855dd28SJason M. Bills         if (params.size() != 2)
2853e855dd28SJason M. Bills         {
2854e855dd28SJason M. Bills             messages::internalError(asyncResp->res);
2855e855dd28SJason M. Bills             return;
2856e855dd28SJason M. Bills         }
2857e855dd28SJason M. Bills         const std::string& logID = params[0];
2858e855dd28SJason M. Bills         const std::string& fileName = params[1];
2859e855dd28SJason M. Bills 
2860043a0536SJohnathan Mantey         auto getStoredLogCallback =
2861043a0536SJohnathan Mantey             [asyncResp, logID, fileName](
2862abf2add6SEd Tanous                 const boost::system::error_code ec,
2863043a0536SJohnathan Mantey                 const std::vector<std::pair<std::string, VariantType>>& resp) {
28641da66f75SEd Tanous                 if (ec)
28651da66f75SEd Tanous                 {
2866043a0536SJohnathan Mantey                     BMCWEB_LOG_DEBUG << "failed to get log ec: "
2867043a0536SJohnathan Mantey                                      << ec.message();
2868f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
28691da66f75SEd Tanous                     return;
28701da66f75SEd Tanous                 }
2871e855dd28SJason M. Bills 
2872043a0536SJohnathan Mantey                 std::string dbusFilename{};
2873043a0536SJohnathan Mantey                 std::string dbusTimestamp{};
2874043a0536SJohnathan Mantey                 std::string dbusFilepath{};
2875043a0536SJohnathan Mantey 
28762c70f800SEd Tanous                 parseCrashdumpParameters(resp, dbusFilename, dbusTimestamp,
2877043a0536SJohnathan Mantey                                          dbusFilepath);
2878043a0536SJohnathan Mantey 
2879043a0536SJohnathan Mantey                 if (dbusFilename.empty() || dbusTimestamp.empty() ||
2880043a0536SJohnathan Mantey                     dbusFilepath.empty())
28811da66f75SEd Tanous                 {
2882e855dd28SJason M. Bills                     messages::resourceMissingAtURI(asyncResp->res, fileName);
28831da66f75SEd Tanous                     return;
28841da66f75SEd Tanous                 }
2885e855dd28SJason M. Bills 
2886043a0536SJohnathan Mantey                 // Verify the file name parameter is correct
2887043a0536SJohnathan Mantey                 if (fileName != dbusFilename)
2888043a0536SJohnathan Mantey                 {
2889043a0536SJohnathan Mantey                     messages::resourceMissingAtURI(asyncResp->res, fileName);
2890043a0536SJohnathan Mantey                     return;
2891043a0536SJohnathan Mantey                 }
2892043a0536SJohnathan Mantey 
2893043a0536SJohnathan Mantey                 if (!std::filesystem::exists(dbusFilepath))
2894043a0536SJohnathan Mantey                 {
2895043a0536SJohnathan Mantey                     messages::resourceMissingAtURI(asyncResp->res, fileName);
2896043a0536SJohnathan Mantey                     return;
2897043a0536SJohnathan Mantey                 }
2898043a0536SJohnathan Mantey                 std::ifstream ifs(dbusFilepath, std::ios::in |
2899043a0536SJohnathan Mantey                                                     std::ios::binary |
2900043a0536SJohnathan Mantey                                                     std::ios::ate);
2901043a0536SJohnathan Mantey                 std::ifstream::pos_type fileSize = ifs.tellg();
2902043a0536SJohnathan Mantey                 if (fileSize < 0)
2903043a0536SJohnathan Mantey                 {
2904043a0536SJohnathan Mantey                     messages::generalError(asyncResp->res);
2905043a0536SJohnathan Mantey                     return;
2906043a0536SJohnathan Mantey                 }
2907043a0536SJohnathan Mantey                 ifs.seekg(0, std::ios::beg);
2908043a0536SJohnathan Mantey 
2909043a0536SJohnathan Mantey                 auto crashData = std::make_unique<char[]>(
2910043a0536SJohnathan Mantey                     static_cast<unsigned int>(fileSize));
2911043a0536SJohnathan Mantey 
2912043a0536SJohnathan Mantey                 ifs.read(crashData.get(), static_cast<int>(fileSize));
2913043a0536SJohnathan Mantey 
2914043a0536SJohnathan Mantey                 // The cast to std::string is intentional in order to use the
2915043a0536SJohnathan Mantey                 // assign() that applies move mechanics
2916043a0536SJohnathan Mantey                 asyncResp->res.body().assign(
2917043a0536SJohnathan Mantey                     static_cast<std::string>(crashData.get()));
2918043a0536SJohnathan Mantey 
2919043a0536SJohnathan Mantey                 // Configure this to be a file download when accessed from
2920043a0536SJohnathan Mantey                 // a browser
2921e855dd28SJason M. Bills                 asyncResp->res.addHeader("Content-Disposition", "attachment");
29221da66f75SEd Tanous             };
29231da66f75SEd Tanous         crow::connections::systemBus->async_method_call(
29245b61b5e8SJason M. Bills             std::move(getStoredLogCallback), crashdumpObject,
29255b61b5e8SJason M. Bills             crashdumpPath + std::string("/") + logID,
2926043a0536SJohnathan Mantey             "org.freedesktop.DBus.Properties", "GetAll", crashdumpInterface);
29271da66f75SEd Tanous     }
29281da66f75SEd Tanous };
29291da66f75SEd Tanous 
29308e6c099aSJason M. Bills class CrashdumpCollect : public Node
29311da66f75SEd Tanous {
29321da66f75SEd Tanous   public:
29338e6c099aSJason M. Bills     CrashdumpCollect(App& app) :
29348e6c099aSJason M. Bills         Node(app, "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/"
29358e6c099aSJason M. Bills                   "LogService.CollectDiagnosticData/")
29361da66f75SEd Tanous     {
29373946028dSAppaRao Puli         // Note: Deviated from redfish privilege registry for GET & HEAD
29383946028dSAppaRao Puli         // method for security reasons.
29391da66f75SEd Tanous         entityPrivileges = {
29403946028dSAppaRao Puli             {boost::beast::http::verb::get, {{"ConfigureComponents"}}},
29413946028dSAppaRao Puli             {boost::beast::http::verb::head, {{"ConfigureComponents"}}},
29423946028dSAppaRao Puli             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
29433946028dSAppaRao Puli             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
29443946028dSAppaRao Puli             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
29453946028dSAppaRao Puli             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
29461da66f75SEd Tanous     }
29471da66f75SEd Tanous 
29481da66f75SEd Tanous   private:
29491da66f75SEd Tanous     void doPost(crow::Response& res, const crow::Request& req,
2950cb13a392SEd Tanous                 const std::vector<std::string>&) override
29511da66f75SEd Tanous     {
2952e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
29531da66f75SEd Tanous 
29548e6c099aSJason M. Bills         std::string diagnosticDataType;
29558e6c099aSJason M. Bills         std::string oemDiagnosticDataType;
29568e6c099aSJason M. Bills         if (!redfish::json_util::readJson(
29578e6c099aSJason M. Bills                 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
29588e6c099aSJason M. Bills                 "OEMDiagnosticDataType", oemDiagnosticDataType))
29598e6c099aSJason M. Bills         {
29608e6c099aSJason M. Bills             return;
29618e6c099aSJason M. Bills         }
29628e6c099aSJason M. Bills 
29638e6c099aSJason M. Bills         if (diagnosticDataType != "OEM")
29648e6c099aSJason M. Bills         {
29658e6c099aSJason M. Bills             BMCWEB_LOG_ERROR
29668e6c099aSJason M. Bills                 << "Only OEM DiagnosticDataType supported for Crashdump";
29678e6c099aSJason M. Bills             messages::actionParameterValueFormatError(
29688e6c099aSJason M. Bills                 asyncResp->res, diagnosticDataType, "DiagnosticDataType",
29698e6c099aSJason M. Bills                 "CollectDiagnosticData");
29708e6c099aSJason M. Bills             return;
29718e6c099aSJason M. Bills         }
29728e6c099aSJason M. Bills 
29738e6c099aSJason M. Bills         auto collectCrashdumpCallback = [asyncResp, req](
29748e6c099aSJason M. Bills                                             const boost::system::error_code ec,
2975cb13a392SEd Tanous                                             const std::string&) {
29761da66f75SEd Tanous             if (ec)
29771da66f75SEd Tanous             {
297846229577SJames Feist                 if (ec.value() == boost::system::errc::operation_not_supported)
29791da66f75SEd Tanous                 {
2980f12894f8SJason M. Bills                     messages::resourceInStandby(asyncResp->res);
29811da66f75SEd Tanous                 }
29824363d3b2SJason M. Bills                 else if (ec.value() ==
29834363d3b2SJason M. Bills                          boost::system::errc::device_or_resource_busy)
29844363d3b2SJason M. Bills                 {
29854363d3b2SJason M. Bills                     messages::serviceTemporarilyUnavailable(asyncResp->res,
29864363d3b2SJason M. Bills                                                             "60");
29874363d3b2SJason M. Bills                 }
29881da66f75SEd Tanous                 else
29891da66f75SEd Tanous                 {
2990f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
29911da66f75SEd Tanous                 }
29921da66f75SEd Tanous                 return;
29931da66f75SEd Tanous             }
299446229577SJames Feist             std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
299566afe4faSJames Feist                 [](boost::system::error_code err, sdbusplus::message::message&,
299666afe4faSJames Feist                    const std::shared_ptr<task::TaskData>& taskData) {
299766afe4faSJames Feist                     if (!err)
299866afe4faSJames Feist                     {
2999e5d5006bSJames Feist                         taskData->messages.emplace_back(
3000e5d5006bSJames Feist                             messages::taskCompletedOK(
3001e5d5006bSJames Feist                                 std::to_string(taskData->index)));
3002831d6b09SJames Feist                         taskData->state = "Completed";
300366afe4faSJames Feist                     }
300432898ceaSJames Feist                     return task::completed;
300566afe4faSJames Feist                 },
300646229577SJames Feist                 "type='signal',interface='org.freedesktop.DBus.Properties',"
300746229577SJames Feist                 "member='PropertiesChanged',arg0namespace='com.intel."
300846229577SJames Feist                 "crashdump'");
300946229577SJames Feist             task->startTimer(std::chrono::minutes(5));
301046229577SJames Feist             task->populateResp(asyncResp->res);
3011fe306728SJames Feist             task->payload.emplace(req);
30121da66f75SEd Tanous         };
30138e6c099aSJason M. Bills 
30148e6c099aSJason M. Bills         if (oemDiagnosticDataType == "OnDemand")
30158e6c099aSJason M. Bills         {
30161da66f75SEd Tanous             crow::connections::systemBus->async_method_call(
30178e6c099aSJason M. Bills                 std::move(collectCrashdumpCallback), crashdumpObject,
30188e6c099aSJason M. Bills                 crashdumpPath, crashdumpOnDemandInterface,
30198e6c099aSJason M. Bills                 "GenerateOnDemandLog");
30201da66f75SEd Tanous         }
30218e6c099aSJason M. Bills         else if (oemDiagnosticDataType == "Telemetry")
30226eda7685SKenny L. Ku         {
30238e6c099aSJason M. Bills             crow::connections::systemBus->async_method_call(
30248e6c099aSJason M. Bills                 std::move(collectCrashdumpCallback), crashdumpObject,
30258e6c099aSJason M. Bills                 crashdumpPath, crashdumpTelemetryInterface,
30268e6c099aSJason M. Bills                 "GenerateTelemetryLog");
30276eda7685SKenny L. Ku         }
30286eda7685SKenny L. Ku         else
30296eda7685SKenny L. Ku         {
30308e6c099aSJason M. Bills             BMCWEB_LOG_ERROR << "Unsupported OEMDiagnosticDataType: "
30318e6c099aSJason M. Bills                              << oemDiagnosticDataType;
30328e6c099aSJason M. Bills             messages::actionParameterValueFormatError(
30338e6c099aSJason M. Bills                 asyncResp->res, oemDiagnosticDataType, "OEMDiagnosticDataType",
30348e6c099aSJason M. Bills                 "CollectDiagnosticData");
30356eda7685SKenny L. Ku             return;
30366eda7685SKenny L. Ku         }
30376eda7685SKenny L. Ku     }
30386eda7685SKenny L. Ku };
30396eda7685SKenny L. Ku 
3040e1f26343SJason M. Bills class SendRawPECI : public Node
30411da66f75SEd Tanous {
30421da66f75SEd Tanous   public:
304352cc112dSEd Tanous     SendRawPECI(App& app) :
3044424c4176SJason M. Bills         Node(app,
3045424c4176SJason M. Bills              "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/Oem/"
3046424c4176SJason M. Bills              "Crashdump.SendRawPeci/")
30471da66f75SEd Tanous     {
30483946028dSAppaRao Puli         // Note: Deviated from redfish privilege registry for GET & HEAD
30493946028dSAppaRao Puli         // method for security reasons.
30501da66f75SEd Tanous         entityPrivileges = {
30511da66f75SEd Tanous             {boost::beast::http::verb::get, {{"ConfigureComponents"}}},
30521da66f75SEd Tanous             {boost::beast::http::verb::head, {{"ConfigureComponents"}}},
30531da66f75SEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
30541da66f75SEd Tanous             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
30551da66f75SEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
30561da66f75SEd Tanous             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
30571da66f75SEd Tanous     }
30581da66f75SEd Tanous 
30591da66f75SEd Tanous   private:
30601da66f75SEd Tanous     void doPost(crow::Response& res, const crow::Request& req,
3061cb13a392SEd Tanous                 const std::vector<std::string>&) override
30621da66f75SEd Tanous     {
3063e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
30648724c297SKarthick Sundarrajan         std::vector<std::vector<uint8_t>> peciCommands;
30658724c297SKarthick Sundarrajan 
30668724c297SKarthick Sundarrajan         if (!json_util::readJson(req, res, "PECICommands", peciCommands))
30678724c297SKarthick Sundarrajan         {
30688724c297SKarthick Sundarrajan             return;
30698724c297SKarthick Sundarrajan         }
30708724c297SKarthick Sundarrajan         uint32_t idx = 0;
30718724c297SKarthick Sundarrajan         for (auto const& cmd : peciCommands)
30728724c297SKarthick Sundarrajan         {
30738724c297SKarthick Sundarrajan             if (cmd.size() < 3)
30748724c297SKarthick Sundarrajan             {
30758724c297SKarthick Sundarrajan                 std::string s("[");
30768724c297SKarthick Sundarrajan                 for (auto const& val : cmd)
30778724c297SKarthick Sundarrajan                 {
30788724c297SKarthick Sundarrajan                     if (val != *cmd.begin())
30798724c297SKarthick Sundarrajan                     {
30808724c297SKarthick Sundarrajan                         s += ",";
30818724c297SKarthick Sundarrajan                     }
30828724c297SKarthick Sundarrajan                     s += std::to_string(val);
30838724c297SKarthick Sundarrajan                 }
30848724c297SKarthick Sundarrajan                 s += "]";
30858724c297SKarthick Sundarrajan                 messages::actionParameterValueFormatError(
30868724c297SKarthick Sundarrajan                     res, s, "PECICommands[" + std::to_string(idx) + "]",
30878724c297SKarthick Sundarrajan                     "SendRawPeci");
30888724c297SKarthick Sundarrajan                 return;
30898724c297SKarthick Sundarrajan             }
30908724c297SKarthick Sundarrajan             idx++;
30918724c297SKarthick Sundarrajan         }
30921da66f75SEd Tanous         // Callback to return the Raw PECI response
3093e1f26343SJason M. Bills         auto sendRawPECICallback =
3094e1f26343SJason M. Bills             [asyncResp](const boost::system::error_code ec,
30958724c297SKarthick Sundarrajan                         const std::vector<std::vector<uint8_t>>& resp) {
30961da66f75SEd Tanous                 if (ec)
30971da66f75SEd Tanous                 {
30988724c297SKarthick Sundarrajan                     BMCWEB_LOG_DEBUG << "failed to process PECI commands ec: "
30991da66f75SEd Tanous                                      << ec.message();
3100f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
31011da66f75SEd Tanous                     return;
31021da66f75SEd Tanous                 }
3103e1f26343SJason M. Bills                 asyncResp->res.jsonValue = {{"Name", "PECI Command Response"},
31041da66f75SEd Tanous                                             {"PECIResponse", resp}};
31051da66f75SEd Tanous             };
31061da66f75SEd Tanous         // Call the SendRawPECI command with the provided data
31071da66f75SEd Tanous         crow::connections::systemBus->async_method_call(
31085b61b5e8SJason M. Bills             std::move(sendRawPECICallback), crashdumpObject, crashdumpPath,
31098724c297SKarthick Sundarrajan             crashdumpRawPECIInterface, "SendRawPeci", peciCommands);
31101da66f75SEd Tanous     }
31111da66f75SEd Tanous };
31121da66f75SEd Tanous 
3113cb92c03bSAndrew Geissler /**
3114cb92c03bSAndrew Geissler  * DBusLogServiceActionsClear class supports POST method for ClearLog action.
3115cb92c03bSAndrew Geissler  */
3116cb92c03bSAndrew Geissler class DBusLogServiceActionsClear : public Node
3117cb92c03bSAndrew Geissler {
3118cb92c03bSAndrew Geissler   public:
311952cc112dSEd Tanous     DBusLogServiceActionsClear(App& app) :
3120cb92c03bSAndrew Geissler         Node(app, "/redfish/v1/Systems/system/LogServices/EventLog/Actions/"
31217af91514SGunnar Mills                   "LogService.ClearLog/")
3122cb92c03bSAndrew Geissler     {
3123cb92c03bSAndrew Geissler         entityPrivileges = {
3124cb92c03bSAndrew Geissler             {boost::beast::http::verb::get, {{"Login"}}},
3125cb92c03bSAndrew Geissler             {boost::beast::http::verb::head, {{"Login"}}},
3126cb92c03bSAndrew Geissler             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
3127cb92c03bSAndrew Geissler             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
3128cb92c03bSAndrew Geissler             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
3129cb92c03bSAndrew Geissler             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
3130cb92c03bSAndrew Geissler     }
3131cb92c03bSAndrew Geissler 
3132cb92c03bSAndrew Geissler   private:
3133cb92c03bSAndrew Geissler     /**
3134cb92c03bSAndrew Geissler      * Function handles POST method request.
3135cb92c03bSAndrew Geissler      * The Clear Log actions does not require any parameter.The action deletes
3136cb92c03bSAndrew Geissler      * all entries found in the Entries collection for this Log Service.
3137cb92c03bSAndrew Geissler      */
3138cb13a392SEd Tanous     void doPost(crow::Response& res, const crow::Request&,
3139cb13a392SEd Tanous                 const std::vector<std::string>&) override
3140cb92c03bSAndrew Geissler     {
3141cb92c03bSAndrew Geissler         BMCWEB_LOG_DEBUG << "Do delete all entries.";
3142cb92c03bSAndrew Geissler 
3143cb92c03bSAndrew Geissler         auto asyncResp = std::make_shared<AsyncResp>(res);
3144cb92c03bSAndrew Geissler         // Process response from Logging service.
31452c70f800SEd Tanous         auto respHandler = [asyncResp](const boost::system::error_code ec) {
3146cb92c03bSAndrew Geissler             BMCWEB_LOG_DEBUG << "doClearLog resp_handler callback: Done";
3147cb92c03bSAndrew Geissler             if (ec)
3148cb92c03bSAndrew Geissler             {
3149cb92c03bSAndrew Geissler                 // TODO Handle for specific error code
3150cb92c03bSAndrew Geissler                 BMCWEB_LOG_ERROR << "doClearLog resp_handler got error " << ec;
3151cb92c03bSAndrew Geissler                 asyncResp->res.result(
3152cb92c03bSAndrew Geissler                     boost::beast::http::status::internal_server_error);
3153cb92c03bSAndrew Geissler                 return;
3154cb92c03bSAndrew Geissler             }
3155cb92c03bSAndrew Geissler 
3156cb92c03bSAndrew Geissler             asyncResp->res.result(boost::beast::http::status::no_content);
3157cb92c03bSAndrew Geissler         };
3158cb92c03bSAndrew Geissler 
3159cb92c03bSAndrew Geissler         // Make call to Logging service to request Clear Log
3160cb92c03bSAndrew Geissler         crow::connections::systemBus->async_method_call(
31612c70f800SEd Tanous             respHandler, "xyz.openbmc_project.Logging",
3162cb92c03bSAndrew Geissler             "/xyz/openbmc_project/logging",
3163cb92c03bSAndrew Geissler             "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
3164cb92c03bSAndrew Geissler     }
3165cb92c03bSAndrew Geissler };
3166a3316fc6SZhikuiRen 
3167a3316fc6SZhikuiRen /****************************************************
3168a3316fc6SZhikuiRen  * Redfish PostCode interfaces
3169a3316fc6SZhikuiRen  * using DBUS interface: getPostCodesTS
3170a3316fc6SZhikuiRen  ******************************************************/
3171a3316fc6SZhikuiRen class PostCodesLogService : public Node
3172a3316fc6SZhikuiRen {
3173a3316fc6SZhikuiRen   public:
317452cc112dSEd Tanous     PostCodesLogService(App& app) :
3175a3316fc6SZhikuiRen         Node(app, "/redfish/v1/Systems/system/LogServices/PostCodes/")
3176a3316fc6SZhikuiRen     {
3177a3316fc6SZhikuiRen         entityPrivileges = {
3178a3316fc6SZhikuiRen             {boost::beast::http::verb::get, {{"Login"}}},
3179a3316fc6SZhikuiRen             {boost::beast::http::verb::head, {{"Login"}}},
3180a3316fc6SZhikuiRen             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
3181a3316fc6SZhikuiRen             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
3182a3316fc6SZhikuiRen             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
3183a3316fc6SZhikuiRen             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
3184a3316fc6SZhikuiRen     }
3185a3316fc6SZhikuiRen 
3186a3316fc6SZhikuiRen   private:
3187cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
3188cb13a392SEd Tanous                const std::vector<std::string>&) override
3189a3316fc6SZhikuiRen     {
3190a3316fc6SZhikuiRen         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
3191a3316fc6SZhikuiRen 
3192a3316fc6SZhikuiRen         asyncResp->res.jsonValue = {
3193a3316fc6SZhikuiRen             {"@odata.id", "/redfish/v1/Systems/system/LogServices/PostCodes"},
3194a3316fc6SZhikuiRen             {"@odata.type", "#LogService.v1_1_0.LogService"},
3195a3316fc6SZhikuiRen             {"Name", "POST Code Log Service"},
3196a3316fc6SZhikuiRen             {"Description", "POST Code Log Service"},
3197a3316fc6SZhikuiRen             {"Id", "BIOS POST Code Log"},
3198a3316fc6SZhikuiRen             {"OverWritePolicy", "WrapsWhenFull"},
3199a3316fc6SZhikuiRen             {"Entries",
3200a3316fc6SZhikuiRen              {{"@odata.id",
3201a3316fc6SZhikuiRen                "/redfish/v1/Systems/system/LogServices/PostCodes/Entries"}}}};
3202a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
3203a3316fc6SZhikuiRen             {"target", "/redfish/v1/Systems/system/LogServices/PostCodes/"
3204a3316fc6SZhikuiRen                        "Actions/LogService.ClearLog"}};
3205a3316fc6SZhikuiRen     }
3206a3316fc6SZhikuiRen };
3207a3316fc6SZhikuiRen 
3208a3316fc6SZhikuiRen class PostCodesClear : public Node
3209a3316fc6SZhikuiRen {
3210a3316fc6SZhikuiRen   public:
321152cc112dSEd Tanous     PostCodesClear(App& app) :
3212a3316fc6SZhikuiRen         Node(app, "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/"
3213a3316fc6SZhikuiRen                   "LogService.ClearLog/")
3214a3316fc6SZhikuiRen     {
3215a3316fc6SZhikuiRen         entityPrivileges = {
3216a3316fc6SZhikuiRen             {boost::beast::http::verb::get, {{"Login"}}},
3217a3316fc6SZhikuiRen             {boost::beast::http::verb::head, {{"Login"}}},
32183946028dSAppaRao Puli             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
32193946028dSAppaRao Puli             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
32203946028dSAppaRao Puli             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
32213946028dSAppaRao Puli             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
3222a3316fc6SZhikuiRen     }
3223a3316fc6SZhikuiRen 
3224a3316fc6SZhikuiRen   private:
3225cb13a392SEd Tanous     void doPost(crow::Response& res, const crow::Request&,
3226cb13a392SEd Tanous                 const std::vector<std::string>&) override
3227a3316fc6SZhikuiRen     {
3228a3316fc6SZhikuiRen         BMCWEB_LOG_DEBUG << "Do delete all postcodes entries.";
3229a3316fc6SZhikuiRen 
3230a3316fc6SZhikuiRen         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
3231a3316fc6SZhikuiRen         // Make call to post-code service to request clear all
3232a3316fc6SZhikuiRen         crow::connections::systemBus->async_method_call(
3233a3316fc6SZhikuiRen             [asyncResp](const boost::system::error_code ec) {
3234a3316fc6SZhikuiRen                 if (ec)
3235a3316fc6SZhikuiRen                 {
3236a3316fc6SZhikuiRen                     // TODO Handle for specific error code
3237a3316fc6SZhikuiRen                     BMCWEB_LOG_ERROR
3238a3316fc6SZhikuiRen                         << "doClearPostCodes resp_handler got error " << ec;
3239a3316fc6SZhikuiRen                     asyncResp->res.result(
3240a3316fc6SZhikuiRen                         boost::beast::http::status::internal_server_error);
3241a3316fc6SZhikuiRen                     messages::internalError(asyncResp->res);
3242a3316fc6SZhikuiRen                     return;
3243a3316fc6SZhikuiRen                 }
3244a3316fc6SZhikuiRen             },
324515124765SJonathan Doman             "xyz.openbmc_project.State.Boot.PostCode0",
324615124765SJonathan Doman             "/xyz/openbmc_project/State/Boot/PostCode0",
3247a3316fc6SZhikuiRen             "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
3248a3316fc6SZhikuiRen     }
3249a3316fc6SZhikuiRen };
3250a3316fc6SZhikuiRen 
3251a3316fc6SZhikuiRen static void fillPostCodeEntry(
3252b5a76932SEd Tanous     const std::shared_ptr<AsyncResp>& aResp,
32536c9a279eSManojkiran Eda     const boost::container::flat_map<
32546c9a279eSManojkiran Eda         uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
3255a3316fc6SZhikuiRen     const uint16_t bootIndex, const uint64_t codeIndex = 0,
3256a3316fc6SZhikuiRen     const uint64_t skip = 0, const uint64_t top = 0)
3257a3316fc6SZhikuiRen {
3258a3316fc6SZhikuiRen     // Get the Message from the MessageRegistry
3259a3316fc6SZhikuiRen     const message_registries::Message* message =
3260a3316fc6SZhikuiRen         message_registries::getMessage("OpenBMC.0.1.BIOSPOSTCode");
3261a3316fc6SZhikuiRen 
3262a3316fc6SZhikuiRen     uint64_t currentCodeIndex = 0;
3263a3316fc6SZhikuiRen     nlohmann::json& logEntryArray = aResp->res.jsonValue["Members"];
3264a3316fc6SZhikuiRen 
3265a3316fc6SZhikuiRen     uint64_t firstCodeTimeUs = 0;
32666c9a279eSManojkiran Eda     for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
32676c9a279eSManojkiran Eda              code : postcode)
3268a3316fc6SZhikuiRen     {
3269a3316fc6SZhikuiRen         currentCodeIndex++;
3270a3316fc6SZhikuiRen         std::string postcodeEntryID =
3271a3316fc6SZhikuiRen             "B" + std::to_string(bootIndex) + "-" +
3272a3316fc6SZhikuiRen             std::to_string(currentCodeIndex); // 1 based index in EntryID string
3273a3316fc6SZhikuiRen 
3274a3316fc6SZhikuiRen         uint64_t usecSinceEpoch = code.first;
3275a3316fc6SZhikuiRen         uint64_t usTimeOffset = 0;
3276a3316fc6SZhikuiRen 
3277a3316fc6SZhikuiRen         if (1 == currentCodeIndex)
3278a3316fc6SZhikuiRen         { // already incremented
3279a3316fc6SZhikuiRen             firstCodeTimeUs = code.first;
3280a3316fc6SZhikuiRen         }
3281a3316fc6SZhikuiRen         else
3282a3316fc6SZhikuiRen         {
3283a3316fc6SZhikuiRen             usTimeOffset = code.first - firstCodeTimeUs;
3284a3316fc6SZhikuiRen         }
3285a3316fc6SZhikuiRen 
3286a3316fc6SZhikuiRen         // skip if no specific codeIndex is specified and currentCodeIndex does
3287a3316fc6SZhikuiRen         // not fall between top and skip
3288a3316fc6SZhikuiRen         if ((codeIndex == 0) &&
3289a3316fc6SZhikuiRen             (currentCodeIndex <= skip || currentCodeIndex > top))
3290a3316fc6SZhikuiRen         {
3291a3316fc6SZhikuiRen             continue;
3292a3316fc6SZhikuiRen         }
3293a3316fc6SZhikuiRen 
32944e0453b1SGunnar Mills         // skip if a specific codeIndex is specified and does not match the
3295a3316fc6SZhikuiRen         // currentIndex
3296a3316fc6SZhikuiRen         if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
3297a3316fc6SZhikuiRen         {
3298a3316fc6SZhikuiRen             // This is done for simplicity. 1st entry is needed to calculate
3299a3316fc6SZhikuiRen             // time offset. To improve efficiency, one can get to the entry
3300a3316fc6SZhikuiRen             // directly (possibly with flatmap's nth method)
3301a3316fc6SZhikuiRen             continue;
3302a3316fc6SZhikuiRen         }
3303a3316fc6SZhikuiRen 
3304a3316fc6SZhikuiRen         // currentCodeIndex is within top and skip or equal to specified code
3305a3316fc6SZhikuiRen         // index
3306a3316fc6SZhikuiRen 
3307a3316fc6SZhikuiRen         // Get the Created time from the timestamp
3308a3316fc6SZhikuiRen         std::string entryTimeStr;
33099c620e21SAsmitha Karunanithi         entryTimeStr = crow::utility::getDateTime(
33109c620e21SAsmitha Karunanithi             static_cast<std::time_t>(usecSinceEpoch / 1000 / 1000));
3311a3316fc6SZhikuiRen 
3312a3316fc6SZhikuiRen         // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
3313a3316fc6SZhikuiRen         std::ostringstream hexCode;
3314a3316fc6SZhikuiRen         hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
33156c9a279eSManojkiran Eda                 << std::get<0>(code.second);
3316a3316fc6SZhikuiRen         std::ostringstream timeOffsetStr;
3317a3316fc6SZhikuiRen         // Set Fixed -Point Notation
3318a3316fc6SZhikuiRen         timeOffsetStr << std::fixed;
3319a3316fc6SZhikuiRen         // Set precision to 4 digits
3320a3316fc6SZhikuiRen         timeOffsetStr << std::setprecision(4);
3321a3316fc6SZhikuiRen         // Add double to stream
3322a3316fc6SZhikuiRen         timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
3323a3316fc6SZhikuiRen         std::vector<std::string> messageArgs = {
3324a3316fc6SZhikuiRen             std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()};
3325a3316fc6SZhikuiRen 
3326a3316fc6SZhikuiRen         // Get MessageArgs template from message registry
3327a3316fc6SZhikuiRen         std::string msg;
3328a3316fc6SZhikuiRen         if (message != nullptr)
3329a3316fc6SZhikuiRen         {
3330a3316fc6SZhikuiRen             msg = message->message;
3331a3316fc6SZhikuiRen 
3332a3316fc6SZhikuiRen             // fill in this post code value
3333a3316fc6SZhikuiRen             int i = 0;
3334a3316fc6SZhikuiRen             for (const std::string& messageArg : messageArgs)
3335a3316fc6SZhikuiRen             {
3336a3316fc6SZhikuiRen                 std::string argStr = "%" + std::to_string(++i);
3337a3316fc6SZhikuiRen                 size_t argPos = msg.find(argStr);
3338a3316fc6SZhikuiRen                 if (argPos != std::string::npos)
3339a3316fc6SZhikuiRen                 {
3340a3316fc6SZhikuiRen                     msg.replace(argPos, argStr.length(), messageArg);
3341a3316fc6SZhikuiRen                 }
3342a3316fc6SZhikuiRen             }
3343a3316fc6SZhikuiRen         }
3344a3316fc6SZhikuiRen 
3345d4342a92STim Lee         // Get Severity template from message registry
3346d4342a92STim Lee         std::string severity;
3347d4342a92STim Lee         if (message != nullptr)
3348d4342a92STim Lee         {
3349d4342a92STim Lee             severity = message->severity;
3350d4342a92STim Lee         }
3351d4342a92STim Lee 
3352a3316fc6SZhikuiRen         // add to AsyncResp
3353a3316fc6SZhikuiRen         logEntryArray.push_back({});
3354a3316fc6SZhikuiRen         nlohmann::json& bmcLogEntry = logEntryArray.back();
3355743e9a1fSGunnar Mills         bmcLogEntry = {{"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
3356a3316fc6SZhikuiRen                        {"@odata.id", "/redfish/v1/Systems/system/LogServices/"
3357a3316fc6SZhikuiRen                                      "PostCodes/Entries/" +
3358a3316fc6SZhikuiRen                                          postcodeEntryID},
3359a3316fc6SZhikuiRen                        {"Name", "POST Code Log Entry"},
3360a3316fc6SZhikuiRen                        {"Id", postcodeEntryID},
3361a3316fc6SZhikuiRen                        {"Message", std::move(msg)},
3362a3316fc6SZhikuiRen                        {"MessageId", "OpenBMC.0.1.BIOSPOSTCode"},
3363a3316fc6SZhikuiRen                        {"MessageArgs", std::move(messageArgs)},
3364a3316fc6SZhikuiRen                        {"EntryType", "Event"},
3365a3316fc6SZhikuiRen                        {"Severity", std::move(severity)},
33669c620e21SAsmitha Karunanithi                        {"Created", entryTimeStr}};
3367a3316fc6SZhikuiRen     }
3368a3316fc6SZhikuiRen }
3369a3316fc6SZhikuiRen 
3370b5a76932SEd Tanous static void getPostCodeForEntry(const std::shared_ptr<AsyncResp>& aResp,
3371a3316fc6SZhikuiRen                                 const uint16_t bootIndex,
3372a3316fc6SZhikuiRen                                 const uint64_t codeIndex)
3373a3316fc6SZhikuiRen {
3374a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
33756c9a279eSManojkiran Eda         [aResp, bootIndex,
33766c9a279eSManojkiran Eda          codeIndex](const boost::system::error_code ec,
33776c9a279eSManojkiran Eda                     const boost::container::flat_map<
33786c9a279eSManojkiran Eda                         uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
33796c9a279eSManojkiran Eda                         postcode) {
3380a3316fc6SZhikuiRen             if (ec)
3381a3316fc6SZhikuiRen             {
3382a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3383a3316fc6SZhikuiRen                 messages::internalError(aResp->res);
3384a3316fc6SZhikuiRen                 return;
3385a3316fc6SZhikuiRen             }
3386a3316fc6SZhikuiRen 
3387a3316fc6SZhikuiRen             // skip the empty postcode boots
3388a3316fc6SZhikuiRen             if (postcode.empty())
3389a3316fc6SZhikuiRen             {
3390a3316fc6SZhikuiRen                 return;
3391a3316fc6SZhikuiRen             }
3392a3316fc6SZhikuiRen 
3393a3316fc6SZhikuiRen             fillPostCodeEntry(aResp, postcode, bootIndex, codeIndex);
3394a3316fc6SZhikuiRen 
3395a3316fc6SZhikuiRen             aResp->res.jsonValue["Members@odata.count"] =
3396a3316fc6SZhikuiRen                 aResp->res.jsonValue["Members"].size();
3397a3316fc6SZhikuiRen         },
339815124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
339915124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
3400a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3401a3316fc6SZhikuiRen         bootIndex);
3402a3316fc6SZhikuiRen }
3403a3316fc6SZhikuiRen 
3404b5a76932SEd Tanous static void getPostCodeForBoot(const std::shared_ptr<AsyncResp>& aResp,
3405a3316fc6SZhikuiRen                                const uint16_t bootIndex,
3406a3316fc6SZhikuiRen                                const uint16_t bootCount,
3407a3316fc6SZhikuiRen                                const uint64_t entryCount, const uint64_t skip,
3408a3316fc6SZhikuiRen                                const uint64_t top)
3409a3316fc6SZhikuiRen {
3410a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
3411a3316fc6SZhikuiRen         [aResp, bootIndex, bootCount, entryCount, skip,
3412a3316fc6SZhikuiRen          top](const boost::system::error_code ec,
34136c9a279eSManojkiran Eda               const boost::container::flat_map<
34146c9a279eSManojkiran Eda                   uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
34156c9a279eSManojkiran Eda                   postcode) {
3416a3316fc6SZhikuiRen             if (ec)
3417a3316fc6SZhikuiRen             {
3418a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3419a3316fc6SZhikuiRen                 messages::internalError(aResp->res);
3420a3316fc6SZhikuiRen                 return;
3421a3316fc6SZhikuiRen             }
3422a3316fc6SZhikuiRen 
3423a3316fc6SZhikuiRen             uint64_t endCount = entryCount;
3424a3316fc6SZhikuiRen             if (!postcode.empty())
3425a3316fc6SZhikuiRen             {
3426a3316fc6SZhikuiRen                 endCount = entryCount + postcode.size();
3427a3316fc6SZhikuiRen 
3428a3316fc6SZhikuiRen                 if ((skip < endCount) && ((top + skip) > entryCount))
3429a3316fc6SZhikuiRen                 {
3430a3316fc6SZhikuiRen                     uint64_t thisBootSkip =
3431a3316fc6SZhikuiRen                         std::max(skip, entryCount) - entryCount;
3432a3316fc6SZhikuiRen                     uint64_t thisBootTop =
3433a3316fc6SZhikuiRen                         std::min(top + skip, endCount) - entryCount;
3434a3316fc6SZhikuiRen 
3435a3316fc6SZhikuiRen                     fillPostCodeEntry(aResp, postcode, bootIndex, 0,
3436a3316fc6SZhikuiRen                                       thisBootSkip, thisBootTop);
3437a3316fc6SZhikuiRen                 }
3438a3316fc6SZhikuiRen                 aResp->res.jsonValue["Members@odata.count"] = endCount;
3439a3316fc6SZhikuiRen             }
3440a3316fc6SZhikuiRen 
3441a3316fc6SZhikuiRen             // continue to previous bootIndex
3442a3316fc6SZhikuiRen             if (bootIndex < bootCount)
3443a3316fc6SZhikuiRen             {
3444a3316fc6SZhikuiRen                 getPostCodeForBoot(aResp, static_cast<uint16_t>(bootIndex + 1),
3445a3316fc6SZhikuiRen                                    bootCount, endCount, skip, top);
3446a3316fc6SZhikuiRen             }
3447a3316fc6SZhikuiRen             else
3448a3316fc6SZhikuiRen             {
3449a3316fc6SZhikuiRen                 aResp->res.jsonValue["Members@odata.nextLink"] =
3450a3316fc6SZhikuiRen                     "/redfish/v1/Systems/system/LogServices/PostCodes/"
3451a3316fc6SZhikuiRen                     "Entries?$skip=" +
3452a3316fc6SZhikuiRen                     std::to_string(skip + top);
3453a3316fc6SZhikuiRen             }
3454a3316fc6SZhikuiRen         },
345515124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
345615124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
3457a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3458a3316fc6SZhikuiRen         bootIndex);
3459a3316fc6SZhikuiRen }
3460a3316fc6SZhikuiRen 
3461b5a76932SEd Tanous static void getCurrentBootNumber(const std::shared_ptr<AsyncResp>& aResp,
3462a3316fc6SZhikuiRen                                  const uint64_t skip, const uint64_t top)
3463a3316fc6SZhikuiRen {
3464a3316fc6SZhikuiRen     uint64_t entryCount = 0;
3465a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
3466a3316fc6SZhikuiRen         [aResp, entryCount, skip,
3467a3316fc6SZhikuiRen          top](const boost::system::error_code ec,
3468a3316fc6SZhikuiRen               const std::variant<uint16_t>& bootCount) {
3469a3316fc6SZhikuiRen             if (ec)
3470a3316fc6SZhikuiRen             {
3471a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3472a3316fc6SZhikuiRen                 messages::internalError(aResp->res);
3473a3316fc6SZhikuiRen                 return;
3474a3316fc6SZhikuiRen             }
3475a3316fc6SZhikuiRen             auto pVal = std::get_if<uint16_t>(&bootCount);
3476a3316fc6SZhikuiRen             if (pVal)
3477a3316fc6SZhikuiRen             {
3478a3316fc6SZhikuiRen                 getPostCodeForBoot(aResp, 1, *pVal, entryCount, skip, top);
3479a3316fc6SZhikuiRen             }
3480a3316fc6SZhikuiRen             else
3481a3316fc6SZhikuiRen             {
3482a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "Post code boot index failed.";
3483a3316fc6SZhikuiRen             }
3484a3316fc6SZhikuiRen         },
348515124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
348615124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
3487a3316fc6SZhikuiRen         "org.freedesktop.DBus.Properties", "Get",
3488a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount");
3489a3316fc6SZhikuiRen }
3490a3316fc6SZhikuiRen 
3491a3316fc6SZhikuiRen class PostCodesEntryCollection : public Node
3492a3316fc6SZhikuiRen {
3493a3316fc6SZhikuiRen   public:
349452cc112dSEd Tanous     PostCodesEntryCollection(App& app) :
3495a3316fc6SZhikuiRen         Node(app, "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/")
3496a3316fc6SZhikuiRen     {
3497a3316fc6SZhikuiRen         entityPrivileges = {
3498a3316fc6SZhikuiRen             {boost::beast::http::verb::get, {{"Login"}}},
3499a3316fc6SZhikuiRen             {boost::beast::http::verb::head, {{"Login"}}},
3500a3316fc6SZhikuiRen             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
3501a3316fc6SZhikuiRen             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
3502a3316fc6SZhikuiRen             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
3503a3316fc6SZhikuiRen             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
3504a3316fc6SZhikuiRen     }
3505a3316fc6SZhikuiRen 
3506a3316fc6SZhikuiRen   private:
3507a3316fc6SZhikuiRen     void doGet(crow::Response& res, const crow::Request& req,
3508cb13a392SEd Tanous                const std::vector<std::string>&) override
3509a3316fc6SZhikuiRen     {
3510a3316fc6SZhikuiRen         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
3511a3316fc6SZhikuiRen 
3512a3316fc6SZhikuiRen         asyncResp->res.jsonValue["@odata.type"] =
3513a3316fc6SZhikuiRen             "#LogEntryCollection.LogEntryCollection";
3514a3316fc6SZhikuiRen         asyncResp->res.jsonValue["@odata.id"] =
3515a3316fc6SZhikuiRen             "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3516a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3517a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Description"] =
3518a3316fc6SZhikuiRen             "Collection of POST Code Log Entries";
3519a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3520a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Members@odata.count"] = 0;
3521a3316fc6SZhikuiRen 
3522a3316fc6SZhikuiRen         uint64_t skip = 0;
3523a3316fc6SZhikuiRen         uint64_t top = maxEntriesPerPage; // Show max entries by default
3524a3316fc6SZhikuiRen         if (!getSkipParam(asyncResp->res, req, skip))
3525a3316fc6SZhikuiRen         {
3526a3316fc6SZhikuiRen             return;
3527a3316fc6SZhikuiRen         }
3528a3316fc6SZhikuiRen         if (!getTopParam(asyncResp->res, req, top))
3529a3316fc6SZhikuiRen         {
3530a3316fc6SZhikuiRen             return;
3531a3316fc6SZhikuiRen         }
3532a3316fc6SZhikuiRen         getCurrentBootNumber(asyncResp, skip, top);
3533a3316fc6SZhikuiRen     }
3534a3316fc6SZhikuiRen };
3535a3316fc6SZhikuiRen 
3536a3316fc6SZhikuiRen class PostCodesEntry : public Node
3537a3316fc6SZhikuiRen {
3538a3316fc6SZhikuiRen   public:
353952cc112dSEd Tanous     PostCodesEntry(App& app) :
3540a3316fc6SZhikuiRen         Node(app,
3541a3316fc6SZhikuiRen              "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/",
3542a3316fc6SZhikuiRen              std::string())
3543a3316fc6SZhikuiRen     {
3544a3316fc6SZhikuiRen         entityPrivileges = {
3545a3316fc6SZhikuiRen             {boost::beast::http::verb::get, {{"Login"}}},
3546a3316fc6SZhikuiRen             {boost::beast::http::verb::head, {{"Login"}}},
3547a3316fc6SZhikuiRen             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
3548a3316fc6SZhikuiRen             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
3549a3316fc6SZhikuiRen             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
3550a3316fc6SZhikuiRen             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
3551a3316fc6SZhikuiRen     }
3552a3316fc6SZhikuiRen 
3553a3316fc6SZhikuiRen   private:
3554cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
3555a3316fc6SZhikuiRen                const std::vector<std::string>& params) override
3556a3316fc6SZhikuiRen     {
3557a3316fc6SZhikuiRen         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
3558a3316fc6SZhikuiRen         if (params.size() != 1)
3559a3316fc6SZhikuiRen         {
3560a3316fc6SZhikuiRen             messages::internalError(asyncResp->res);
3561a3316fc6SZhikuiRen             return;
3562a3316fc6SZhikuiRen         }
3563a3316fc6SZhikuiRen 
3564a3316fc6SZhikuiRen         const std::string& targetID = params[0];
3565a3316fc6SZhikuiRen 
3566a3316fc6SZhikuiRen         size_t bootPos = targetID.find('B');
3567a3316fc6SZhikuiRen         if (bootPos == std::string::npos)
3568a3316fc6SZhikuiRen         {
3569a3316fc6SZhikuiRen             // Requested ID was not found
3570a3316fc6SZhikuiRen             messages::resourceMissingAtURI(asyncResp->res, targetID);
3571a3316fc6SZhikuiRen             return;
3572a3316fc6SZhikuiRen         }
3573a3316fc6SZhikuiRen         std::string_view bootIndexStr(targetID);
3574a3316fc6SZhikuiRen         bootIndexStr.remove_prefix(bootPos + 1);
3575a3316fc6SZhikuiRen         uint16_t bootIndex = 0;
3576a3316fc6SZhikuiRen         uint64_t codeIndex = 0;
3577a3316fc6SZhikuiRen         size_t dashPos = bootIndexStr.find('-');
3578a3316fc6SZhikuiRen 
3579a3316fc6SZhikuiRen         if (dashPos == std::string::npos)
3580a3316fc6SZhikuiRen         {
3581a3316fc6SZhikuiRen             return;
3582a3316fc6SZhikuiRen         }
3583a3316fc6SZhikuiRen         std::string_view codeIndexStr(bootIndexStr);
3584a3316fc6SZhikuiRen         bootIndexStr.remove_suffix(dashPos);
3585a3316fc6SZhikuiRen         codeIndexStr.remove_prefix(dashPos + 1);
3586a3316fc6SZhikuiRen 
3587a3316fc6SZhikuiRen         bootIndex = static_cast<uint16_t>(
358823a21a1cSEd Tanous             strtoul(std::string(bootIndexStr).c_str(), nullptr, 0));
358923a21a1cSEd Tanous         codeIndex = strtoul(std::string(codeIndexStr).c_str(), nullptr, 0);
3590a3316fc6SZhikuiRen         if (bootIndex == 0 || codeIndex == 0)
3591a3316fc6SZhikuiRen         {
3592a3316fc6SZhikuiRen             BMCWEB_LOG_DEBUG << "Get Post Code invalid entry string "
3593a3316fc6SZhikuiRen                              << params[0];
3594a3316fc6SZhikuiRen         }
3595a3316fc6SZhikuiRen 
3596a3316fc6SZhikuiRen         asyncResp->res.jsonValue["@odata.type"] = "#LogEntry.v1_4_0.LogEntry";
3597a3316fc6SZhikuiRen         asyncResp->res.jsonValue["@odata.id"] =
3598a3316fc6SZhikuiRen             "/redfish/v1/Systems/system/LogServices/PostCodes/"
3599a3316fc6SZhikuiRen             "Entries";
3600a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3601a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Description"] =
3602a3316fc6SZhikuiRen             "Collection of POST Code Log Entries";
3603a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3604a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Members@odata.count"] = 0;
3605a3316fc6SZhikuiRen 
3606a3316fc6SZhikuiRen         getPostCodeForEntry(asyncResp, bootIndex, codeIndex);
3607a3316fc6SZhikuiRen     }
3608a3316fc6SZhikuiRen };
3609a3316fc6SZhikuiRen 
36101da66f75SEd Tanous } // namespace redfish
3611