xref: /openbmc/bmcweb/features/redfish/lib/log_services.hpp (revision de8d94a3dfda08ef6a80a245e2a31df8ec2496d2)
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>
25400fd1fbSAdriana Kobylak #include <unistd.h>
26e1f26343SJason M. Bills 
27400fd1fbSAdriana 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>
30400fd1fbSAdriana 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";
506eda7685SKenny L. Ku constexpr char const* crashdumpTelemetryInterface =
516eda7685SKenny L. Ku     "com.intel.crashdump.Telemetry";
521da66f75SEd Tanous 
534851d45dSJason M. Bills namespace message_registries
544851d45dSJason M. Bills {
554851d45dSJason M. Bills static const Message* getMessageFromRegistry(
564851d45dSJason M. Bills     const std::string& messageKey,
574851d45dSJason M. Bills     const boost::beast::span<const MessageEntry> registry)
584851d45dSJason M. Bills {
594851d45dSJason M. Bills     boost::beast::span<const MessageEntry>::const_iterator messageIt =
604851d45dSJason M. Bills         std::find_if(registry.cbegin(), registry.cend(),
614851d45dSJason M. Bills                      [&messageKey](const MessageEntry& messageEntry) {
624851d45dSJason M. Bills                          return !std::strcmp(messageEntry.first,
634851d45dSJason M. Bills                                              messageKey.c_str());
644851d45dSJason M. Bills                      });
654851d45dSJason M. Bills     if (messageIt != registry.cend())
664851d45dSJason M. Bills     {
674851d45dSJason M. Bills         return &messageIt->second;
684851d45dSJason M. Bills     }
694851d45dSJason M. Bills 
704851d45dSJason M. Bills     return nullptr;
714851d45dSJason M. Bills }
724851d45dSJason M. Bills 
734851d45dSJason M. Bills static const Message* getMessage(const std::string_view& messageID)
744851d45dSJason M. Bills {
754851d45dSJason M. Bills     // Redfish MessageIds are in the form
764851d45dSJason M. Bills     // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find
774851d45dSJason M. Bills     // the right Message
784851d45dSJason M. Bills     std::vector<std::string> fields;
794851d45dSJason M. Bills     fields.reserve(4);
804851d45dSJason M. Bills     boost::split(fields, messageID, boost::is_any_of("."));
814851d45dSJason M. Bills     std::string& registryName = fields[0];
824851d45dSJason M. Bills     std::string& messageKey = fields[3];
834851d45dSJason M. Bills 
844851d45dSJason M. Bills     // Find the right registry and check it for the MessageKey
854851d45dSJason M. Bills     if (std::string(base::header.registryPrefix) == registryName)
864851d45dSJason M. Bills     {
874851d45dSJason M. Bills         return getMessageFromRegistry(
884851d45dSJason M. Bills             messageKey, boost::beast::span<const MessageEntry>(base::registry));
894851d45dSJason M. Bills     }
904851d45dSJason M. Bills     if (std::string(openbmc::header.registryPrefix) == registryName)
914851d45dSJason M. Bills     {
924851d45dSJason M. Bills         return getMessageFromRegistry(
934851d45dSJason M. Bills             messageKey,
944851d45dSJason M. Bills             boost::beast::span<const MessageEntry>(openbmc::registry));
954851d45dSJason M. Bills     }
964851d45dSJason M. Bills     return nullptr;
974851d45dSJason M. Bills }
984851d45dSJason M. Bills } // namespace message_registries
994851d45dSJason M. Bills 
100f6150403SJames Feist namespace fs = std::filesystem;
1011da66f75SEd Tanous 
102cb92c03bSAndrew Geissler using GetManagedPropertyType = boost::container::flat_map<
10319bd78d9SPatrick Williams     std::string, std::variant<std::string, bool, uint8_t, int16_t, uint16_t,
104cb92c03bSAndrew Geissler                               int32_t, uint32_t, int64_t, uint64_t, double>>;
105cb92c03bSAndrew Geissler 
106cb92c03bSAndrew Geissler using GetManagedObjectsType = boost::container::flat_map<
107cb92c03bSAndrew Geissler     sdbusplus::message::object_path,
108cb92c03bSAndrew Geissler     boost::container::flat_map<std::string, GetManagedPropertyType>>;
109cb92c03bSAndrew Geissler 
110cb92c03bSAndrew Geissler inline std::string translateSeverityDbusToRedfish(const std::string& s)
111cb92c03bSAndrew Geissler {
112d4d25793SEd Tanous     if ((s == "xyz.openbmc_project.Logging.Entry.Level.Alert") ||
113d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Critical") ||
114d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Emergency") ||
115d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Error"))
116cb92c03bSAndrew Geissler     {
117cb92c03bSAndrew Geissler         return "Critical";
118cb92c03bSAndrew Geissler     }
1193174e4dfSEd Tanous     if ((s == "xyz.openbmc_project.Logging.Entry.Level.Debug") ||
120d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Informational") ||
121d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Notice"))
122cb92c03bSAndrew Geissler     {
123cb92c03bSAndrew Geissler         return "OK";
124cb92c03bSAndrew Geissler     }
1253174e4dfSEd Tanous     if (s == "xyz.openbmc_project.Logging.Entry.Level.Warning")
126cb92c03bSAndrew Geissler     {
127cb92c03bSAndrew Geissler         return "Warning";
128cb92c03bSAndrew Geissler     }
129cb92c03bSAndrew Geissler     return "";
130cb92c03bSAndrew Geissler }
131cb92c03bSAndrew Geissler 
13216428a1aSJason M. Bills static int getJournalMetadata(sd_journal* journal,
13339e77504SEd Tanous                               const std::string_view& field,
13439e77504SEd Tanous                               std::string_view& contents)
13516428a1aSJason M. Bills {
13616428a1aSJason M. Bills     const char* data = nullptr;
13716428a1aSJason M. Bills     size_t length = 0;
13816428a1aSJason M. Bills     int ret = 0;
13916428a1aSJason M. Bills     // Get the metadata from the requested field of the journal entry
140271584abSEd Tanous     ret = sd_journal_get_data(journal, field.data(),
141271584abSEd Tanous                               reinterpret_cast<const void**>(&data), &length);
14216428a1aSJason M. Bills     if (ret < 0)
14316428a1aSJason M. Bills     {
14416428a1aSJason M. Bills         return ret;
14516428a1aSJason M. Bills     }
14639e77504SEd Tanous     contents = std::string_view(data, length);
14716428a1aSJason M. Bills     // Only use the content after the "=" character.
14881ce609eSEd Tanous     contents.remove_prefix(std::min(contents.find('=') + 1, contents.size()));
14916428a1aSJason M. Bills     return ret;
15016428a1aSJason M. Bills }
15116428a1aSJason M. Bills 
15216428a1aSJason M. Bills static int getJournalMetadata(sd_journal* journal,
15339e77504SEd Tanous                               const std::string_view& field, const int& base,
154271584abSEd Tanous                               long int& contents)
15516428a1aSJason M. Bills {
15616428a1aSJason M. Bills     int ret = 0;
15739e77504SEd Tanous     std::string_view metadata;
15816428a1aSJason M. Bills     // Get the metadata from the requested field of the journal entry
15916428a1aSJason M. Bills     ret = getJournalMetadata(journal, field, metadata);
16016428a1aSJason M. Bills     if (ret < 0)
16116428a1aSJason M. Bills     {
16216428a1aSJason M. Bills         return ret;
16316428a1aSJason M. Bills     }
164b01bf299SEd Tanous     contents = strtol(metadata.data(), nullptr, base);
16516428a1aSJason M. Bills     return ret;
16616428a1aSJason M. Bills }
16716428a1aSJason M. Bills 
168a3316fc6SZhikuiRen static bool getEntryTimestamp(sd_journal* journal, std::string& entryTimestamp)
169a3316fc6SZhikuiRen {
170a3316fc6SZhikuiRen     int ret = 0;
171a3316fc6SZhikuiRen     uint64_t timestamp = 0;
172a3316fc6SZhikuiRen     ret = sd_journal_get_realtime_usec(journal, &timestamp);
173a3316fc6SZhikuiRen     if (ret < 0)
174a3316fc6SZhikuiRen     {
175a3316fc6SZhikuiRen         BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
176a3316fc6SZhikuiRen                          << strerror(-ret);
177a3316fc6SZhikuiRen         return false;
178a3316fc6SZhikuiRen     }
1799c620e21SAsmitha Karunanithi     entryTimestamp = crow::utility::getDateTime(
1809c620e21SAsmitha Karunanithi         static_cast<std::time_t>(timestamp / 1000 / 1000));
1819c620e21SAsmitha Karunanithi     return true;
182a3316fc6SZhikuiRen }
183a3316fc6SZhikuiRen 
1848d1b46d7Szhanghch05 static bool getSkipParam(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1858d1b46d7Szhanghch05                          const crow::Request& req, uint64_t& skip)
18616428a1aSJason M. Bills {
1875a7e877eSJames Feist     boost::urls::url_view::params_type::iterator it =
1885a7e877eSJames Feist         req.urlParams.find("$skip");
1895a7e877eSJames Feist     if (it != req.urlParams.end())
19016428a1aSJason M. Bills     {
1915a7e877eSJames Feist         std::string skipParam = it->value();
19216428a1aSJason M. Bills         char* ptr = nullptr;
1935a7e877eSJames Feist         skip = std::strtoul(skipParam.c_str(), &ptr, 10);
1945a7e877eSJames Feist         if (skipParam.empty() || *ptr != '\0')
19516428a1aSJason M. Bills         {
19616428a1aSJason M. Bills 
1978d1b46d7Szhanghch05             messages::queryParameterValueTypeError(
1988d1b46d7Szhanghch05                 asyncResp->res, std::string(skipParam), "$skip");
19916428a1aSJason M. Bills             return false;
20016428a1aSJason M. Bills         }
20116428a1aSJason M. Bills     }
20216428a1aSJason M. Bills     return true;
20316428a1aSJason M. Bills }
20416428a1aSJason M. Bills 
205271584abSEd Tanous static constexpr const uint64_t maxEntriesPerPage = 1000;
2068d1b46d7Szhanghch05 static bool getTopParam(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2078d1b46d7Szhanghch05                         const crow::Request& req, uint64_t& top)
20816428a1aSJason M. Bills {
2095a7e877eSJames Feist     boost::urls::url_view::params_type::iterator it =
2105a7e877eSJames Feist         req.urlParams.find("$top");
2115a7e877eSJames Feist     if (it != req.urlParams.end())
21216428a1aSJason M. Bills     {
2135a7e877eSJames Feist         std::string topParam = it->value();
21416428a1aSJason M. Bills         char* ptr = nullptr;
2155a7e877eSJames Feist         top = std::strtoul(topParam.c_str(), &ptr, 10);
2165a7e877eSJames Feist         if (topParam.empty() || *ptr != '\0')
21716428a1aSJason M. Bills         {
2188d1b46d7Szhanghch05             messages::queryParameterValueTypeError(
2198d1b46d7Szhanghch05                 asyncResp->res, std::string(topParam), "$top");
22016428a1aSJason M. Bills             return false;
22116428a1aSJason M. Bills         }
222271584abSEd Tanous         if (top < 1U || top > maxEntriesPerPage)
22316428a1aSJason M. Bills         {
22416428a1aSJason M. Bills 
22516428a1aSJason M. Bills             messages::queryParameterOutOfRange(
2268d1b46d7Szhanghch05                 asyncResp->res, std::to_string(top), "$top",
22716428a1aSJason M. Bills                 "1-" + std::to_string(maxEntriesPerPage));
22816428a1aSJason M. Bills             return false;
22916428a1aSJason M. Bills         }
23016428a1aSJason M. Bills     }
23116428a1aSJason M. Bills     return true;
23216428a1aSJason M. Bills }
23316428a1aSJason M. Bills 
234e85d6b16SJason M. Bills static bool getUniqueEntryID(sd_journal* journal, std::string& entryID,
235e85d6b16SJason M. Bills                              const bool firstEntry = true)
23616428a1aSJason M. Bills {
23716428a1aSJason M. Bills     int ret = 0;
23816428a1aSJason M. Bills     static uint64_t prevTs = 0;
23916428a1aSJason M. Bills     static int index = 0;
240e85d6b16SJason M. Bills     if (firstEntry)
241e85d6b16SJason M. Bills     {
242e85d6b16SJason M. Bills         prevTs = 0;
243e85d6b16SJason M. Bills     }
244e85d6b16SJason M. Bills 
24516428a1aSJason M. Bills     // Get the entry timestamp
24616428a1aSJason M. Bills     uint64_t curTs = 0;
24716428a1aSJason M. Bills     ret = sd_journal_get_realtime_usec(journal, &curTs);
24816428a1aSJason M. Bills     if (ret < 0)
24916428a1aSJason M. Bills     {
25016428a1aSJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
25116428a1aSJason M. Bills                          << strerror(-ret);
25216428a1aSJason M. Bills         return false;
25316428a1aSJason M. Bills     }
25416428a1aSJason M. Bills     // If the timestamp isn't unique, increment the index
25516428a1aSJason M. Bills     if (curTs == prevTs)
25616428a1aSJason M. Bills     {
25716428a1aSJason M. Bills         index++;
25816428a1aSJason M. Bills     }
25916428a1aSJason M. Bills     else
26016428a1aSJason M. Bills     {
26116428a1aSJason M. Bills         // Otherwise, reset it
26216428a1aSJason M. Bills         index = 0;
26316428a1aSJason M. Bills     }
26416428a1aSJason M. Bills     // Save the timestamp
26516428a1aSJason M. Bills     prevTs = curTs;
26616428a1aSJason M. Bills 
26716428a1aSJason M. Bills     entryID = std::to_string(curTs);
26816428a1aSJason M. Bills     if (index > 0)
26916428a1aSJason M. Bills     {
27016428a1aSJason M. Bills         entryID += "_" + std::to_string(index);
27116428a1aSJason M. Bills     }
27216428a1aSJason M. Bills     return true;
27316428a1aSJason M. Bills }
27416428a1aSJason M. Bills 
275e85d6b16SJason M. Bills static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
276e85d6b16SJason M. Bills                              const bool firstEntry = true)
27795820184SJason M. Bills {
278271584abSEd Tanous     static time_t prevTs = 0;
27995820184SJason M. Bills     static int index = 0;
280e85d6b16SJason M. Bills     if (firstEntry)
281e85d6b16SJason M. Bills     {
282e85d6b16SJason M. Bills         prevTs = 0;
283e85d6b16SJason M. Bills     }
284e85d6b16SJason M. Bills 
28595820184SJason M. Bills     // Get the entry timestamp
286271584abSEd Tanous     std::time_t curTs = 0;
28795820184SJason M. Bills     std::tm timeStruct = {};
28895820184SJason M. Bills     std::istringstream entryStream(logEntry);
28995820184SJason M. Bills     if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
29095820184SJason M. Bills     {
29195820184SJason M. Bills         curTs = std::mktime(&timeStruct);
29295820184SJason M. Bills     }
29395820184SJason M. Bills     // If the timestamp isn't unique, increment the index
29495820184SJason M. Bills     if (curTs == prevTs)
29595820184SJason M. Bills     {
29695820184SJason M. Bills         index++;
29795820184SJason M. Bills     }
29895820184SJason M. Bills     else
29995820184SJason M. Bills     {
30095820184SJason M. Bills         // Otherwise, reset it
30195820184SJason M. Bills         index = 0;
30295820184SJason M. Bills     }
30395820184SJason M. Bills     // Save the timestamp
30495820184SJason M. Bills     prevTs = curTs;
30595820184SJason M. Bills 
30695820184SJason M. Bills     entryID = std::to_string(curTs);
30795820184SJason M. Bills     if (index > 0)
30895820184SJason M. Bills     {
30995820184SJason M. Bills         entryID += "_" + std::to_string(index);
31095820184SJason M. Bills     }
31195820184SJason M. Bills     return true;
31295820184SJason M. Bills }
31395820184SJason M. Bills 
3148d1b46d7Szhanghch05 static bool
3158d1b46d7Szhanghch05     getTimestampFromID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3168d1b46d7Szhanghch05                        const std::string& entryID, uint64_t& timestamp,
3178d1b46d7Szhanghch05                        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         {
3408d1b46d7Szhanghch05             messages::resourceMissingAtURI(asyncResp->res, entryID);
34116428a1aSJason M. Bills             return false;
34216428a1aSJason M. Bills         }
343271584abSEd Tanous         catch (std::out_of_range&)
34416428a1aSJason M. Bills         {
3458d1b46d7Szhanghch05             messages::resourceMissingAtURI(asyncResp->res, entryID);
34616428a1aSJason M. Bills             return false;
34716428a1aSJason M. Bills         }
34816428a1aSJason M. Bills         if (pos != indexStr.size())
34916428a1aSJason M. Bills         {
3508d1b46d7Szhanghch05             messages::resourceMissingAtURI(asyncResp->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     {
3628d1b46d7Szhanghch05         messages::resourceMissingAtURI(asyncResp->res, entryID);
36316428a1aSJason M. Bills         return false;
36416428a1aSJason M. Bills     }
365271584abSEd Tanous     catch (std::out_of_range&)
36616428a1aSJason M. Bills     {
3678d1b46d7Szhanghch05         messages::resourceMissingAtURI(asyncResp->res, entryID);
36816428a1aSJason M. Bills         return false;
36916428a1aSJason M. Bills     }
37016428a1aSJason M. Bills     if (pos != tsStr.size())
37116428a1aSJason M. Bills     {
3728d1b46d7Szhanghch05         messages::resourceMissingAtURI(asyncResp->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 
4038d1b46d7Szhanghch05 inline void
4048d1b46d7Szhanghch05     getDumpEntryCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4055cb1dd27SAsmitha Karunanithi                            const std::string& dumpType)
4065cb1dd27SAsmitha Karunanithi {
4075cb1dd27SAsmitha Karunanithi     std::string dumpPath;
4085cb1dd27SAsmitha Karunanithi     if (dumpType == "BMC")
4095cb1dd27SAsmitha Karunanithi     {
4105cb1dd27SAsmitha Karunanithi         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
4115cb1dd27SAsmitha Karunanithi     }
4125cb1dd27SAsmitha Karunanithi     else if (dumpType == "System")
4135cb1dd27SAsmitha Karunanithi     {
4145cb1dd27SAsmitha Karunanithi         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
4155cb1dd27SAsmitha Karunanithi     }
4165cb1dd27SAsmitha Karunanithi     else
4175cb1dd27SAsmitha Karunanithi     {
4185cb1dd27SAsmitha Karunanithi         BMCWEB_LOG_ERROR << "Invalid dump type" << dumpType;
4195cb1dd27SAsmitha Karunanithi         messages::internalError(asyncResp->res);
4205cb1dd27SAsmitha Karunanithi         return;
4215cb1dd27SAsmitha Karunanithi     }
4225cb1dd27SAsmitha Karunanithi 
4235cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
4245cb1dd27SAsmitha Karunanithi         [asyncResp, dumpPath, dumpType](const boost::system::error_code ec,
4255cb1dd27SAsmitha Karunanithi                                         GetManagedObjectsType& resp) {
4265cb1dd27SAsmitha Karunanithi             if (ec)
4275cb1dd27SAsmitha Karunanithi             {
4285cb1dd27SAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
4295cb1dd27SAsmitha Karunanithi                 messages::internalError(asyncResp->res);
4305cb1dd27SAsmitha Karunanithi                 return;
4315cb1dd27SAsmitha Karunanithi             }
4325cb1dd27SAsmitha Karunanithi 
4335cb1dd27SAsmitha Karunanithi             nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
4345cb1dd27SAsmitha Karunanithi             entriesArray = nlohmann::json::array();
435b47452b2SAsmitha Karunanithi             std::string dumpEntryPath =
436b47452b2SAsmitha Karunanithi                 "/xyz/openbmc_project/dump/" +
437b47452b2SAsmitha Karunanithi                 std::string(boost::algorithm::to_lower_copy(dumpType)) +
438b47452b2SAsmitha Karunanithi                 "/entry/";
4395cb1dd27SAsmitha Karunanithi 
4405cb1dd27SAsmitha Karunanithi             for (auto& object : resp)
4415cb1dd27SAsmitha Karunanithi             {
442b47452b2SAsmitha Karunanithi                 if (object.first.str.find(dumpEntryPath) == std::string::npos)
4435cb1dd27SAsmitha Karunanithi                 {
4445cb1dd27SAsmitha Karunanithi                     continue;
4455cb1dd27SAsmitha Karunanithi                 }
4465cb1dd27SAsmitha Karunanithi                 std::time_t timestamp;
4475cb1dd27SAsmitha Karunanithi                 uint64_t size = 0;
4485cb1dd27SAsmitha Karunanithi                 entriesArray.push_back({});
4495cb1dd27SAsmitha Karunanithi                 nlohmann::json& thisEntry = entriesArray.back();
4502dfd18efSEd Tanous 
4512dfd18efSEd Tanous                 std::string entryID = object.first.filename();
4522dfd18efSEd Tanous                 if (entryID.empty())
4535cb1dd27SAsmitha Karunanithi                 {
4545cb1dd27SAsmitha Karunanithi                     continue;
4555cb1dd27SAsmitha Karunanithi                 }
4565cb1dd27SAsmitha Karunanithi 
4575cb1dd27SAsmitha Karunanithi                 for (auto& interfaceMap : object.second)
4585cb1dd27SAsmitha Karunanithi                 {
4595cb1dd27SAsmitha Karunanithi                     if (interfaceMap.first == "xyz.openbmc_project.Dump.Entry")
4605cb1dd27SAsmitha Karunanithi                     {
4615cb1dd27SAsmitha Karunanithi 
4625cb1dd27SAsmitha Karunanithi                         for (auto& propertyMap : interfaceMap.second)
4635cb1dd27SAsmitha Karunanithi                         {
4645cb1dd27SAsmitha Karunanithi                             if (propertyMap.first == "Size")
4655cb1dd27SAsmitha Karunanithi                             {
4665cb1dd27SAsmitha Karunanithi                                 auto sizePtr =
4675cb1dd27SAsmitha Karunanithi                                     std::get_if<uint64_t>(&propertyMap.second);
4685cb1dd27SAsmitha Karunanithi                                 if (sizePtr == nullptr)
4695cb1dd27SAsmitha Karunanithi                                 {
4705cb1dd27SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
4715cb1dd27SAsmitha Karunanithi                                     break;
4725cb1dd27SAsmitha Karunanithi                                 }
4735cb1dd27SAsmitha Karunanithi                                 size = *sizePtr;
4745cb1dd27SAsmitha Karunanithi                                 break;
4755cb1dd27SAsmitha Karunanithi                             }
4765cb1dd27SAsmitha Karunanithi                         }
4775cb1dd27SAsmitha Karunanithi                     }
4785cb1dd27SAsmitha Karunanithi                     else if (interfaceMap.first ==
4795cb1dd27SAsmitha Karunanithi                              "xyz.openbmc_project.Time.EpochTime")
4805cb1dd27SAsmitha Karunanithi                     {
4815cb1dd27SAsmitha Karunanithi 
4825cb1dd27SAsmitha Karunanithi                         for (auto& propertyMap : interfaceMap.second)
4835cb1dd27SAsmitha Karunanithi                         {
4845cb1dd27SAsmitha Karunanithi                             if (propertyMap.first == "Elapsed")
4855cb1dd27SAsmitha Karunanithi                             {
4865cb1dd27SAsmitha Karunanithi                                 const uint64_t* usecsTimeStamp =
4875cb1dd27SAsmitha Karunanithi                                     std::get_if<uint64_t>(&propertyMap.second);
4885cb1dd27SAsmitha Karunanithi                                 if (usecsTimeStamp == nullptr)
4895cb1dd27SAsmitha Karunanithi                                 {
4905cb1dd27SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
4915cb1dd27SAsmitha Karunanithi                                     break;
4925cb1dd27SAsmitha Karunanithi                                 }
4935cb1dd27SAsmitha Karunanithi                                 timestamp =
4945cb1dd27SAsmitha Karunanithi                                     static_cast<std::time_t>(*usecsTimeStamp);
4955cb1dd27SAsmitha Karunanithi                                 break;
4965cb1dd27SAsmitha Karunanithi                             }
4975cb1dd27SAsmitha Karunanithi                         }
4985cb1dd27SAsmitha Karunanithi                     }
4995cb1dd27SAsmitha Karunanithi                 }
5005cb1dd27SAsmitha Karunanithi 
501d337bb72SAsmitha Karunanithi                 thisEntry["@odata.type"] = "#LogEntry.v1_7_0.LogEntry";
5025cb1dd27SAsmitha Karunanithi                 thisEntry["@odata.id"] = dumpPath + entryID;
5035cb1dd27SAsmitha Karunanithi                 thisEntry["Id"] = entryID;
5045cb1dd27SAsmitha Karunanithi                 thisEntry["EntryType"] = "Event";
5055cb1dd27SAsmitha Karunanithi                 thisEntry["Created"] = crow::utility::getDateTime(timestamp);
5065cb1dd27SAsmitha Karunanithi                 thisEntry["Name"] = dumpType + " Dump Entry";
5075cb1dd27SAsmitha Karunanithi 
508d337bb72SAsmitha Karunanithi                 thisEntry["AdditionalDataSizeBytes"] = size;
5095cb1dd27SAsmitha Karunanithi 
5105cb1dd27SAsmitha Karunanithi                 if (dumpType == "BMC")
5115cb1dd27SAsmitha Karunanithi                 {
512d337bb72SAsmitha Karunanithi                     thisEntry["DiagnosticDataType"] = "Manager";
513d337bb72SAsmitha Karunanithi                     thisEntry["AdditionalDataURI"] =
514*de8d94a3SAbhishek Patel                         "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/" +
515*de8d94a3SAbhishek Patel                         entryID + "/attachment";
5165cb1dd27SAsmitha Karunanithi                 }
5175cb1dd27SAsmitha Karunanithi                 else if (dumpType == "System")
5185cb1dd27SAsmitha Karunanithi                 {
519d337bb72SAsmitha Karunanithi                     thisEntry["DiagnosticDataType"] = "OEM";
520d337bb72SAsmitha Karunanithi                     thisEntry["OEMDiagnosticDataType"] = "System";
521d337bb72SAsmitha Karunanithi                     thisEntry["AdditionalDataURI"] =
522*de8d94a3SAbhishek Patel                         "/redfish/v1/Systems/system/LogServices/Dump/Entries/" +
523*de8d94a3SAbhishek Patel                         entryID + "/attachment";
5245cb1dd27SAsmitha Karunanithi                 }
5255cb1dd27SAsmitha Karunanithi             }
5265cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Members@odata.count"] =
5275cb1dd27SAsmitha Karunanithi                 entriesArray.size();
5285cb1dd27SAsmitha Karunanithi         },
5295cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
5305cb1dd27SAsmitha Karunanithi         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
5315cb1dd27SAsmitha Karunanithi }
5325cb1dd27SAsmitha Karunanithi 
5338d1b46d7Szhanghch05 inline void
5348d1b46d7Szhanghch05     getDumpEntryById(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
5358d1b46d7Szhanghch05                      const std::string& entryID, const std::string& dumpType)
5365cb1dd27SAsmitha Karunanithi {
5375cb1dd27SAsmitha Karunanithi     std::string dumpPath;
5385cb1dd27SAsmitha Karunanithi     if (dumpType == "BMC")
5395cb1dd27SAsmitha Karunanithi     {
5405cb1dd27SAsmitha Karunanithi         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
5415cb1dd27SAsmitha Karunanithi     }
5425cb1dd27SAsmitha Karunanithi     else if (dumpType == "System")
5435cb1dd27SAsmitha Karunanithi     {
5445cb1dd27SAsmitha Karunanithi         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
5455cb1dd27SAsmitha Karunanithi     }
5465cb1dd27SAsmitha Karunanithi     else
5475cb1dd27SAsmitha Karunanithi     {
5485cb1dd27SAsmitha Karunanithi         BMCWEB_LOG_ERROR << "Invalid dump type" << dumpType;
5495cb1dd27SAsmitha Karunanithi         messages::internalError(asyncResp->res);
5505cb1dd27SAsmitha Karunanithi         return;
5515cb1dd27SAsmitha Karunanithi     }
5525cb1dd27SAsmitha Karunanithi 
5535cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
5545cb1dd27SAsmitha Karunanithi         [asyncResp, entryID, dumpPath, dumpType](
5555cb1dd27SAsmitha Karunanithi             const boost::system::error_code ec, GetManagedObjectsType& resp) {
5565cb1dd27SAsmitha Karunanithi             if (ec)
5575cb1dd27SAsmitha Karunanithi             {
5585cb1dd27SAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
5595cb1dd27SAsmitha Karunanithi                 messages::internalError(asyncResp->res);
5605cb1dd27SAsmitha Karunanithi                 return;
5615cb1dd27SAsmitha Karunanithi             }
5625cb1dd27SAsmitha Karunanithi 
563b47452b2SAsmitha Karunanithi             bool foundDumpEntry = false;
564b47452b2SAsmitha Karunanithi             std::string dumpEntryPath =
565b47452b2SAsmitha Karunanithi                 "/xyz/openbmc_project/dump/" +
566b47452b2SAsmitha Karunanithi                 std::string(boost::algorithm::to_lower_copy(dumpType)) +
567b47452b2SAsmitha Karunanithi                 "/entry/";
568b47452b2SAsmitha Karunanithi 
5695cb1dd27SAsmitha Karunanithi             for (auto& objectPath : resp)
5705cb1dd27SAsmitha Karunanithi             {
571b47452b2SAsmitha Karunanithi                 if (objectPath.first.str != dumpEntryPath + entryID)
5725cb1dd27SAsmitha Karunanithi                 {
5735cb1dd27SAsmitha Karunanithi                     continue;
5745cb1dd27SAsmitha Karunanithi                 }
5755cb1dd27SAsmitha Karunanithi 
5765cb1dd27SAsmitha Karunanithi                 foundDumpEntry = true;
5775cb1dd27SAsmitha Karunanithi                 std::time_t timestamp;
5785cb1dd27SAsmitha Karunanithi                 uint64_t size = 0;
5795cb1dd27SAsmitha Karunanithi 
5805cb1dd27SAsmitha Karunanithi                 for (auto& interfaceMap : objectPath.second)
5815cb1dd27SAsmitha Karunanithi                 {
5825cb1dd27SAsmitha Karunanithi                     if (interfaceMap.first == "xyz.openbmc_project.Dump.Entry")
5835cb1dd27SAsmitha Karunanithi                     {
5845cb1dd27SAsmitha Karunanithi                         for (auto& propertyMap : interfaceMap.second)
5855cb1dd27SAsmitha Karunanithi                         {
5865cb1dd27SAsmitha Karunanithi                             if (propertyMap.first == "Size")
5875cb1dd27SAsmitha Karunanithi                             {
5885cb1dd27SAsmitha Karunanithi                                 auto sizePtr =
5895cb1dd27SAsmitha Karunanithi                                     std::get_if<uint64_t>(&propertyMap.second);
5905cb1dd27SAsmitha Karunanithi                                 if (sizePtr == nullptr)
5915cb1dd27SAsmitha Karunanithi                                 {
5925cb1dd27SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
5935cb1dd27SAsmitha Karunanithi                                     break;
5945cb1dd27SAsmitha Karunanithi                                 }
5955cb1dd27SAsmitha Karunanithi                                 size = *sizePtr;
5965cb1dd27SAsmitha Karunanithi                                 break;
5975cb1dd27SAsmitha Karunanithi                             }
5985cb1dd27SAsmitha Karunanithi                         }
5995cb1dd27SAsmitha Karunanithi                     }
6005cb1dd27SAsmitha Karunanithi                     else if (interfaceMap.first ==
6015cb1dd27SAsmitha Karunanithi                              "xyz.openbmc_project.Time.EpochTime")
6025cb1dd27SAsmitha Karunanithi                     {
6035cb1dd27SAsmitha Karunanithi                         for (auto& propertyMap : interfaceMap.second)
6045cb1dd27SAsmitha Karunanithi                         {
6055cb1dd27SAsmitha Karunanithi                             if (propertyMap.first == "Elapsed")
6065cb1dd27SAsmitha Karunanithi                             {
6075cb1dd27SAsmitha Karunanithi                                 const uint64_t* usecsTimeStamp =
6085cb1dd27SAsmitha Karunanithi                                     std::get_if<uint64_t>(&propertyMap.second);
6095cb1dd27SAsmitha Karunanithi                                 if (usecsTimeStamp == nullptr)
6105cb1dd27SAsmitha Karunanithi                                 {
6115cb1dd27SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
6125cb1dd27SAsmitha Karunanithi                                     break;
6135cb1dd27SAsmitha Karunanithi                                 }
6145cb1dd27SAsmitha Karunanithi                                 timestamp =
6155cb1dd27SAsmitha Karunanithi                                     static_cast<std::time_t>(*usecsTimeStamp);
6165cb1dd27SAsmitha Karunanithi                                 break;
6175cb1dd27SAsmitha Karunanithi                             }
6185cb1dd27SAsmitha Karunanithi                         }
6195cb1dd27SAsmitha Karunanithi                     }
6205cb1dd27SAsmitha Karunanithi                 }
6215cb1dd27SAsmitha Karunanithi 
6225cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.type"] =
623d337bb72SAsmitha Karunanithi                     "#LogEntry.v1_7_0.LogEntry";
6245cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.id"] = dumpPath + entryID;
6255cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Id"] = entryID;
6265cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["EntryType"] = "Event";
6275cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Created"] =
6285cb1dd27SAsmitha Karunanithi                     crow::utility::getDateTime(timestamp);
6295cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
6305cb1dd27SAsmitha Karunanithi 
631d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
6325cb1dd27SAsmitha Karunanithi 
6335cb1dd27SAsmitha Karunanithi                 if (dumpType == "BMC")
6345cb1dd27SAsmitha Karunanithi                 {
635d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager";
636d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["AdditionalDataURI"] =
637*de8d94a3SAbhishek Patel                         "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/" +
638*de8d94a3SAbhishek Patel                         entryID + "/attachment";
6395cb1dd27SAsmitha Karunanithi                 }
6405cb1dd27SAsmitha Karunanithi                 else if (dumpType == "System")
6415cb1dd27SAsmitha Karunanithi                 {
642d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM";
643d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["OEMDiagnosticDataType"] =
6445cb1dd27SAsmitha Karunanithi                         "System";
645d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["AdditionalDataURI"] =
646*de8d94a3SAbhishek Patel                         "/redfish/v1/Systems/system/LogServices/Dump/Entries/" +
647*de8d94a3SAbhishek Patel                         entryID + "/attachment";
6485cb1dd27SAsmitha Karunanithi                 }
6495cb1dd27SAsmitha Karunanithi             }
650b47452b2SAsmitha Karunanithi             if (foundDumpEntry == false)
651b47452b2SAsmitha Karunanithi             {
652b47452b2SAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "Can't find Dump Entry";
653b47452b2SAsmitha Karunanithi                 messages::internalError(asyncResp->res);
654b47452b2SAsmitha Karunanithi                 return;
655b47452b2SAsmitha Karunanithi             }
6565cb1dd27SAsmitha Karunanithi         },
6575cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
6585cb1dd27SAsmitha Karunanithi         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
6595cb1dd27SAsmitha Karunanithi }
6605cb1dd27SAsmitha Karunanithi 
6618d1b46d7Szhanghch05 inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6629878256fSStanley Chu                             const std::string& entryID,
663b47452b2SAsmitha Karunanithi                             const std::string& dumpType)
6645cb1dd27SAsmitha Karunanithi {
6653de8d8baSGeorge Liu     auto respHandler = [asyncResp,
6663de8d8baSGeorge Liu                         entryID](const boost::system::error_code ec) {
6675cb1dd27SAsmitha Karunanithi         BMCWEB_LOG_DEBUG << "Dump Entry doDelete callback: Done";
6685cb1dd27SAsmitha Karunanithi         if (ec)
6695cb1dd27SAsmitha Karunanithi         {
6703de8d8baSGeorge Liu             if (ec.value() == EBADR)
6713de8d8baSGeorge Liu             {
6723de8d8baSGeorge Liu                 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
6733de8d8baSGeorge Liu                 return;
6743de8d8baSGeorge Liu             }
6755cb1dd27SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Dump (DBus) doDelete respHandler got error "
6765cb1dd27SAsmitha Karunanithi                              << ec;
6775cb1dd27SAsmitha Karunanithi             messages::internalError(asyncResp->res);
6785cb1dd27SAsmitha Karunanithi             return;
6795cb1dd27SAsmitha Karunanithi         }
6805cb1dd27SAsmitha Karunanithi     };
6815cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
6825cb1dd27SAsmitha Karunanithi         respHandler, "xyz.openbmc_project.Dump.Manager",
683b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" +
684b47452b2SAsmitha Karunanithi             std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/" +
685b47452b2SAsmitha Karunanithi             entryID,
6865cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Object.Delete", "Delete");
6875cb1dd27SAsmitha Karunanithi }
6885cb1dd27SAsmitha Karunanithi 
6898d1b46d7Szhanghch05 inline void
6908d1b46d7Szhanghch05     createDumpTaskCallback(const crow::Request& req,
6918d1b46d7Szhanghch05                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6928d1b46d7Szhanghch05                            const uint32_t& dumpId, const std::string& dumpPath,
693a43be80fSAsmitha Karunanithi                            const std::string& dumpType)
694a43be80fSAsmitha Karunanithi {
695a43be80fSAsmitha Karunanithi     std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
6966145ed6fSAsmitha Karunanithi         [dumpId, dumpPath, dumpType](
697a43be80fSAsmitha Karunanithi             boost::system::error_code err, sdbusplus::message::message& m,
698a43be80fSAsmitha Karunanithi             const std::shared_ptr<task::TaskData>& taskData) {
699cb13a392SEd Tanous             if (err)
700cb13a392SEd Tanous             {
7016145ed6fSAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "Error in creating a dump";
7026145ed6fSAsmitha Karunanithi                 taskData->state = "Cancelled";
7036145ed6fSAsmitha Karunanithi                 return task::completed;
704cb13a392SEd Tanous             }
705a43be80fSAsmitha Karunanithi             std::vector<std::pair<
706a43be80fSAsmitha Karunanithi                 std::string,
707a43be80fSAsmitha Karunanithi                 std::vector<std::pair<std::string, std::variant<std::string>>>>>
708a43be80fSAsmitha Karunanithi                 interfacesList;
709a43be80fSAsmitha Karunanithi 
710a43be80fSAsmitha Karunanithi             sdbusplus::message::object_path objPath;
711a43be80fSAsmitha Karunanithi 
712a43be80fSAsmitha Karunanithi             m.read(objPath, interfacesList);
713a43be80fSAsmitha Karunanithi 
714b47452b2SAsmitha Karunanithi             if (objPath.str ==
715b47452b2SAsmitha Karunanithi                 "/xyz/openbmc_project/dump/" +
716b47452b2SAsmitha Karunanithi                     std::string(boost::algorithm::to_lower_copy(dumpType)) +
717b47452b2SAsmitha Karunanithi                     "/entry/" + std::to_string(dumpId))
718a43be80fSAsmitha Karunanithi             {
719a43be80fSAsmitha Karunanithi                 nlohmann::json retMessage = messages::success();
720a43be80fSAsmitha Karunanithi                 taskData->messages.emplace_back(retMessage);
721a43be80fSAsmitha Karunanithi 
722a43be80fSAsmitha Karunanithi                 std::string headerLoc =
723a43be80fSAsmitha Karunanithi                     "Location: " + dumpPath + std::to_string(dumpId);
724a43be80fSAsmitha Karunanithi                 taskData->payload->httpHeaders.emplace_back(
725a43be80fSAsmitha Karunanithi                     std::move(headerLoc));
726a43be80fSAsmitha Karunanithi 
727a43be80fSAsmitha Karunanithi                 taskData->state = "Completed";
728b47452b2SAsmitha Karunanithi                 return task::completed;
7296145ed6fSAsmitha Karunanithi             }
730a43be80fSAsmitha Karunanithi             return task::completed;
731a43be80fSAsmitha Karunanithi         },
732a43be80fSAsmitha Karunanithi         "type='signal',interface='org.freedesktop.DBus."
733a43be80fSAsmitha Karunanithi         "ObjectManager',"
734a43be80fSAsmitha Karunanithi         "member='InterfacesAdded', "
735a43be80fSAsmitha Karunanithi         "path='/xyz/openbmc_project/dump'");
736a43be80fSAsmitha Karunanithi 
737a43be80fSAsmitha Karunanithi     task->startTimer(std::chrono::minutes(3));
738a43be80fSAsmitha Karunanithi     task->populateResp(asyncResp->res);
739a43be80fSAsmitha Karunanithi     task->payload.emplace(req);
740a43be80fSAsmitha Karunanithi }
741a43be80fSAsmitha Karunanithi 
7428d1b46d7Szhanghch05 inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7438d1b46d7Szhanghch05                        const crow::Request& req, const std::string& dumpType)
744a43be80fSAsmitha Karunanithi {
745a43be80fSAsmitha Karunanithi 
746a43be80fSAsmitha Karunanithi     std::string dumpPath;
747a43be80fSAsmitha Karunanithi     if (dumpType == "BMC")
748a43be80fSAsmitha Karunanithi     {
749a43be80fSAsmitha Karunanithi         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
750a43be80fSAsmitha Karunanithi     }
751a43be80fSAsmitha Karunanithi     else if (dumpType == "System")
752a43be80fSAsmitha Karunanithi     {
753a43be80fSAsmitha Karunanithi         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
754a43be80fSAsmitha Karunanithi     }
755a43be80fSAsmitha Karunanithi     else
756a43be80fSAsmitha Karunanithi     {
757a43be80fSAsmitha Karunanithi         BMCWEB_LOG_ERROR << "Invalid dump type: " << dumpType;
758a43be80fSAsmitha Karunanithi         messages::internalError(asyncResp->res);
759a43be80fSAsmitha Karunanithi         return;
760a43be80fSAsmitha Karunanithi     }
761a43be80fSAsmitha Karunanithi 
762a43be80fSAsmitha Karunanithi     std::optional<std::string> diagnosticDataType;
763a43be80fSAsmitha Karunanithi     std::optional<std::string> oemDiagnosticDataType;
764a43be80fSAsmitha Karunanithi 
765a43be80fSAsmitha Karunanithi     if (!redfish::json_util::readJson(
766a43be80fSAsmitha Karunanithi             req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
767a43be80fSAsmitha Karunanithi             "OEMDiagnosticDataType", oemDiagnosticDataType))
768a43be80fSAsmitha Karunanithi     {
769a43be80fSAsmitha Karunanithi         return;
770a43be80fSAsmitha Karunanithi     }
771a43be80fSAsmitha Karunanithi 
772a43be80fSAsmitha Karunanithi     if (dumpType == "System")
773a43be80fSAsmitha Karunanithi     {
774a43be80fSAsmitha Karunanithi         if (!oemDiagnosticDataType || !diagnosticDataType)
775a43be80fSAsmitha Karunanithi         {
776a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "CreateDump action parameter "
777a43be80fSAsmitha Karunanithi                                 "'DiagnosticDataType'/"
778a43be80fSAsmitha Karunanithi                                 "'OEMDiagnosticDataType' value not found!";
779a43be80fSAsmitha Karunanithi             messages::actionParameterMissing(
780a43be80fSAsmitha Karunanithi                 asyncResp->res, "CollectDiagnosticData",
781a43be80fSAsmitha Karunanithi                 "DiagnosticDataType & OEMDiagnosticDataType");
782a43be80fSAsmitha Karunanithi             return;
783a43be80fSAsmitha Karunanithi         }
7843174e4dfSEd Tanous         if ((*oemDiagnosticDataType != "System") ||
785a43be80fSAsmitha Karunanithi             (*diagnosticDataType != "OEM"))
786a43be80fSAsmitha Karunanithi         {
787a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Wrong parameter values passed";
788a43be80fSAsmitha Karunanithi             messages::invalidObject(asyncResp->res,
789a43be80fSAsmitha Karunanithi                                     "System Dump creation parameters");
790a43be80fSAsmitha Karunanithi             return;
791a43be80fSAsmitha Karunanithi         }
792a43be80fSAsmitha Karunanithi     }
793a43be80fSAsmitha Karunanithi     else if (dumpType == "BMC")
794a43be80fSAsmitha Karunanithi     {
795a43be80fSAsmitha Karunanithi         if (!diagnosticDataType)
796a43be80fSAsmitha Karunanithi         {
797a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "CreateDump action parameter "
798a43be80fSAsmitha Karunanithi                                 "'DiagnosticDataType' not found!";
799a43be80fSAsmitha Karunanithi             messages::actionParameterMissing(
800a43be80fSAsmitha Karunanithi                 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
801a43be80fSAsmitha Karunanithi             return;
802a43be80fSAsmitha Karunanithi         }
8033174e4dfSEd Tanous         if (*diagnosticDataType != "Manager")
804a43be80fSAsmitha Karunanithi         {
805a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR
806a43be80fSAsmitha Karunanithi                 << "Wrong parameter value passed for 'DiagnosticDataType'";
807a43be80fSAsmitha Karunanithi             messages::invalidObject(asyncResp->res,
808a43be80fSAsmitha Karunanithi                                     "BMC Dump creation parameters");
809a43be80fSAsmitha Karunanithi             return;
810a43be80fSAsmitha Karunanithi         }
811a43be80fSAsmitha Karunanithi     }
812a43be80fSAsmitha Karunanithi 
813a43be80fSAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
814a43be80fSAsmitha Karunanithi         [asyncResp, req, dumpPath, dumpType](const boost::system::error_code ec,
815a43be80fSAsmitha Karunanithi                                              const uint32_t& dumpId) {
816a43be80fSAsmitha Karunanithi             if (ec)
817a43be80fSAsmitha Karunanithi             {
818a43be80fSAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "CreateDump resp_handler got error " << ec;
819a43be80fSAsmitha Karunanithi                 messages::internalError(asyncResp->res);
820a43be80fSAsmitha Karunanithi                 return;
821a43be80fSAsmitha Karunanithi             }
822a43be80fSAsmitha Karunanithi             BMCWEB_LOG_DEBUG << "Dump Created. Id: " << dumpId;
823a43be80fSAsmitha Karunanithi 
824a43be80fSAsmitha Karunanithi             createDumpTaskCallback(req, asyncResp, dumpId, dumpPath, dumpType);
825a43be80fSAsmitha Karunanithi         },
826b47452b2SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager",
827b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" +
828b47452b2SAsmitha Karunanithi             std::string(boost::algorithm::to_lower_copy(dumpType)),
829a43be80fSAsmitha Karunanithi         "xyz.openbmc_project.Dump.Create", "CreateDump");
830a43be80fSAsmitha Karunanithi }
831a43be80fSAsmitha Karunanithi 
8328d1b46d7Szhanghch05 inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8338d1b46d7Szhanghch05                       const std::string& dumpType)
83480319af1SAsmitha Karunanithi {
835b47452b2SAsmitha Karunanithi     std::string dumpTypeLowerCopy =
836b47452b2SAsmitha Karunanithi         std::string(boost::algorithm::to_lower_copy(dumpType));
8378d1b46d7Szhanghch05 
83880319af1SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
839b47452b2SAsmitha Karunanithi         [asyncResp, dumpType](const boost::system::error_code ec,
84080319af1SAsmitha Karunanithi                               const std::vector<std::string>& subTreePaths) {
84180319af1SAsmitha Karunanithi             if (ec)
84280319af1SAsmitha Karunanithi             {
84380319af1SAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "resp_handler got error " << ec;
84480319af1SAsmitha Karunanithi                 messages::internalError(asyncResp->res);
84580319af1SAsmitha Karunanithi                 return;
84680319af1SAsmitha Karunanithi             }
84780319af1SAsmitha Karunanithi 
84880319af1SAsmitha Karunanithi             for (const std::string& path : subTreePaths)
84980319af1SAsmitha Karunanithi             {
8502dfd18efSEd Tanous                 sdbusplus::message::object_path objPath(path);
8512dfd18efSEd Tanous                 std::string logID = objPath.filename();
8522dfd18efSEd Tanous                 if (logID.empty())
85380319af1SAsmitha Karunanithi                 {
8542dfd18efSEd Tanous                     continue;
85580319af1SAsmitha Karunanithi                 }
8562dfd18efSEd Tanous                 deleteDumpEntry(asyncResp, logID, dumpType);
85780319af1SAsmitha Karunanithi             }
85880319af1SAsmitha Karunanithi         },
85980319af1SAsmitha Karunanithi         "xyz.openbmc_project.ObjectMapper",
86080319af1SAsmitha Karunanithi         "/xyz/openbmc_project/object_mapper",
86180319af1SAsmitha Karunanithi         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
862b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" + dumpTypeLowerCopy, 0,
863b47452b2SAsmitha Karunanithi         std::array<std::string, 1>{"xyz.openbmc_project.Dump.Entry." +
864b47452b2SAsmitha Karunanithi                                    dumpType});
86580319af1SAsmitha Karunanithi }
86680319af1SAsmitha Karunanithi 
8672c70f800SEd Tanous static void parseCrashdumpParameters(
868043a0536SJohnathan Mantey     const std::vector<std::pair<std::string, VariantType>>& params,
869043a0536SJohnathan Mantey     std::string& filename, std::string& timestamp, std::string& logfile)
870043a0536SJohnathan Mantey {
871043a0536SJohnathan Mantey     for (auto property : params)
872043a0536SJohnathan Mantey     {
873043a0536SJohnathan Mantey         if (property.first == "Timestamp")
874043a0536SJohnathan Mantey         {
875043a0536SJohnathan Mantey             const std::string* value =
8768d78b7a9SPatrick Williams                 std::get_if<std::string>(&property.second);
877043a0536SJohnathan Mantey             if (value != nullptr)
878043a0536SJohnathan Mantey             {
879043a0536SJohnathan Mantey                 timestamp = *value;
880043a0536SJohnathan Mantey             }
881043a0536SJohnathan Mantey         }
882043a0536SJohnathan Mantey         else if (property.first == "Filename")
883043a0536SJohnathan Mantey         {
884043a0536SJohnathan Mantey             const std::string* value =
8858d78b7a9SPatrick Williams                 std::get_if<std::string>(&property.second);
886043a0536SJohnathan Mantey             if (value != nullptr)
887043a0536SJohnathan Mantey             {
888043a0536SJohnathan Mantey                 filename = *value;
889043a0536SJohnathan Mantey             }
890043a0536SJohnathan Mantey         }
891043a0536SJohnathan Mantey         else if (property.first == "Log")
892043a0536SJohnathan Mantey         {
893043a0536SJohnathan Mantey             const std::string* value =
8948d78b7a9SPatrick Williams                 std::get_if<std::string>(&property.second);
895043a0536SJohnathan Mantey             if (value != nullptr)
896043a0536SJohnathan Mantey             {
897043a0536SJohnathan Mantey                 logfile = *value;
898043a0536SJohnathan Mantey             }
899043a0536SJohnathan Mantey         }
900043a0536SJohnathan Mantey     }
901043a0536SJohnathan Mantey }
902043a0536SJohnathan Mantey 
903a3316fc6SZhikuiRen constexpr char const* postCodeIface = "xyz.openbmc_project.State.Boot.PostCode";
904c4bf6374SJason M. Bills class SystemLogServiceCollection : public Node
9051da66f75SEd Tanous {
9061da66f75SEd Tanous   public:
90752cc112dSEd Tanous     SystemLogServiceCollection(App& app) :
908029573d4SEd Tanous         Node(app, "/redfish/v1/Systems/system/LogServices/")
909c4bf6374SJason M. Bills     {
910c4bf6374SJason M. Bills         entityPrivileges = {
911c4bf6374SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
912c4bf6374SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
913c4bf6374SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
914c4bf6374SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
915c4bf6374SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
916c4bf6374SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
917c4bf6374SJason M. Bills     }
918c4bf6374SJason M. Bills 
919c4bf6374SJason M. Bills   private:
920c4bf6374SJason M. Bills     /**
921c4bf6374SJason M. Bills      * Functions triggers appropriate requests on DBus
922c4bf6374SJason M. Bills      */
9238d1b46d7Szhanghch05     void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
9248d1b46d7Szhanghch05                const crow::Request&, const std::vector<std::string>&) override
925c4bf6374SJason M. Bills     {
926c4bf6374SJason M. Bills         // Collections don't include the static data added by SubRoute because
927c4bf6374SJason M. Bills         // it has a duplicate entry for members
928c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
929c4bf6374SJason M. Bills             "#LogServiceCollection.LogServiceCollection";
930c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
931029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices";
932c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "System Log Services Collection";
933c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] =
934c4bf6374SJason M. Bills             "Collection of LogServices for this Computer System";
935c4bf6374SJason M. Bills         nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
936c4bf6374SJason M. Bills         logServiceArray = nlohmann::json::array();
937029573d4SEd Tanous         logServiceArray.push_back(
938029573d4SEd Tanous             {{"@odata.id", "/redfish/v1/Systems/system/LogServices/EventLog"}});
9395cb1dd27SAsmitha Karunanithi #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
940c9bb6861Sraviteja-b         logServiceArray.push_back(
9415cb1dd27SAsmitha Karunanithi             {{"@odata.id", "/redfish/v1/Systems/system/LogServices/Dump"}});
942c9bb6861Sraviteja-b #endif
943c9bb6861Sraviteja-b 
944d53dd41fSJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
945d53dd41fSJason M. Bills         logServiceArray.push_back(
946cb92c03bSAndrew Geissler             {{"@odata.id",
947424c4176SJason M. Bills               "/redfish/v1/Systems/system/LogServices/Crashdump"}});
948d53dd41fSJason M. Bills #endif
949c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] =
950c4bf6374SJason M. Bills             logServiceArray.size();
951a3316fc6SZhikuiRen 
952a3316fc6SZhikuiRen         crow::connections::systemBus->async_method_call(
953a3316fc6SZhikuiRen             [asyncResp](const boost::system::error_code ec,
954a3316fc6SZhikuiRen                         const std::vector<std::string>& subtreePath) {
955a3316fc6SZhikuiRen                 if (ec)
956a3316fc6SZhikuiRen                 {
957a3316fc6SZhikuiRen                     BMCWEB_LOG_ERROR << ec;
958a3316fc6SZhikuiRen                     return;
959a3316fc6SZhikuiRen                 }
960a3316fc6SZhikuiRen 
961a3316fc6SZhikuiRen                 for (auto& pathStr : subtreePath)
962a3316fc6SZhikuiRen                 {
963a3316fc6SZhikuiRen                     if (pathStr.find("PostCode") != std::string::npos)
964a3316fc6SZhikuiRen                     {
96523a21a1cSEd Tanous                         nlohmann::json& logServiceArrayLocal =
966a3316fc6SZhikuiRen                             asyncResp->res.jsonValue["Members"];
96723a21a1cSEd Tanous                         logServiceArrayLocal.push_back(
968a3316fc6SZhikuiRen                             {{"@odata.id", "/redfish/v1/Systems/system/"
969a3316fc6SZhikuiRen                                            "LogServices/PostCodes"}});
970a3316fc6SZhikuiRen                         asyncResp->res.jsonValue["Members@odata.count"] =
97123a21a1cSEd Tanous                             logServiceArrayLocal.size();
972a3316fc6SZhikuiRen                         return;
973a3316fc6SZhikuiRen                     }
974a3316fc6SZhikuiRen                 }
975a3316fc6SZhikuiRen             },
976a3316fc6SZhikuiRen             "xyz.openbmc_project.ObjectMapper",
977a3316fc6SZhikuiRen             "/xyz/openbmc_project/object_mapper",
978a3316fc6SZhikuiRen             "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", 0,
979a3316fc6SZhikuiRen             std::array<const char*, 1>{postCodeIface});
980c4bf6374SJason M. Bills     }
981c4bf6374SJason M. Bills };
982c4bf6374SJason M. Bills 
983c4bf6374SJason M. Bills class EventLogService : public Node
984c4bf6374SJason M. Bills {
985c4bf6374SJason M. Bills   public:
98652cc112dSEd Tanous     EventLogService(App& app) :
987029573d4SEd Tanous         Node(app, "/redfish/v1/Systems/system/LogServices/EventLog/")
988c4bf6374SJason M. Bills     {
989c4bf6374SJason M. Bills         entityPrivileges = {
990c4bf6374SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
991c4bf6374SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
992c4bf6374SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
993c4bf6374SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
994c4bf6374SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
995c4bf6374SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
996c4bf6374SJason M. Bills     }
997c4bf6374SJason M. Bills 
998c4bf6374SJason M. Bills   private:
9998d1b46d7Szhanghch05     void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
10008d1b46d7Szhanghch05                const crow::Request&, const std::vector<std::string>&) override
1001c4bf6374SJason M. Bills     {
1002c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
1003029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog";
1004c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1005c4bf6374SJason M. Bills             "#LogService.v1_1_0.LogService";
1006c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Event Log Service";
1007c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] = "System Event Log Service";
1008c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Id"] = "EventLog";
1009c4bf6374SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
1010c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Entries"] = {
1011c4bf6374SJason M. Bills             {"@odata.id",
1012029573d4SEd Tanous              "/redfish/v1/Systems/system/LogServices/EventLog/Entries"}};
1013e7d6c8b2SGunnar Mills         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
1014e7d6c8b2SGunnar Mills 
1015e7d6c8b2SGunnar Mills             {"target", "/redfish/v1/Systems/system/LogServices/EventLog/"
1016e7d6c8b2SGunnar Mills                        "Actions/LogService.ClearLog"}};
1017489640c6SJason M. Bills     }
1018489640c6SJason M. Bills };
1019489640c6SJason M. Bills 
10201f56a3a6STim Lee class JournalEventLogClear : public Node
1021489640c6SJason M. Bills {
1022489640c6SJason M. Bills   public:
102352cc112dSEd Tanous     JournalEventLogClear(App& app) :
1024489640c6SJason M. Bills         Node(app, "/redfish/v1/Systems/system/LogServices/EventLog/Actions/"
1025489640c6SJason M. Bills                   "LogService.ClearLog/")
1026489640c6SJason M. Bills     {
1027489640c6SJason M. Bills         entityPrivileges = {
1028489640c6SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1029489640c6SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1030489640c6SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1031489640c6SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1032489640c6SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1033489640c6SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
1034489640c6SJason M. Bills     }
1035489640c6SJason M. Bills 
1036489640c6SJason M. Bills   private:
10378d1b46d7Szhanghch05     void doPost(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
10388d1b46d7Szhanghch05                 const crow::Request&, const std::vector<std::string>&) override
1039489640c6SJason M. Bills     {
1040489640c6SJason M. Bills 
1041489640c6SJason M. Bills         // Clear the EventLog by deleting the log files
1042489640c6SJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
1043489640c6SJason M. Bills         if (getRedfishLogFiles(redfishLogFiles))
1044489640c6SJason M. Bills         {
1045489640c6SJason M. Bills             for (const std::filesystem::path& file : redfishLogFiles)
1046489640c6SJason M. Bills             {
1047489640c6SJason M. Bills                 std::error_code ec;
1048489640c6SJason M. Bills                 std::filesystem::remove(file, ec);
1049489640c6SJason M. Bills             }
1050489640c6SJason M. Bills         }
1051489640c6SJason M. Bills 
1052489640c6SJason M. Bills         // Reload rsyslog so it knows to start new log files
1053489640c6SJason M. Bills         crow::connections::systemBus->async_method_call(
1054489640c6SJason M. Bills             [asyncResp](const boost::system::error_code ec) {
1055489640c6SJason M. Bills                 if (ec)
1056489640c6SJason M. Bills                 {
1057489640c6SJason M. Bills                     BMCWEB_LOG_ERROR << "Failed to reload rsyslog: " << ec;
1058489640c6SJason M. Bills                     messages::internalError(asyncResp->res);
1059489640c6SJason M. Bills                     return;
1060489640c6SJason M. Bills                 }
1061489640c6SJason M. Bills 
1062489640c6SJason M. Bills                 messages::success(asyncResp->res);
1063489640c6SJason M. Bills             },
1064489640c6SJason M. Bills             "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1065489640c6SJason M. Bills             "org.freedesktop.systemd1.Manager", "ReloadUnit", "rsyslog.service",
1066489640c6SJason M. Bills             "replace");
1067c4bf6374SJason M. Bills     }
1068c4bf6374SJason M. Bills };
1069c4bf6374SJason M. Bills 
107095820184SJason M. Bills static int fillEventLogEntryJson(const std::string& logEntryID,
1071b5a76932SEd Tanous                                  const std::string& logEntry,
107295820184SJason M. Bills                                  nlohmann::json& logEntryJson)
1073c4bf6374SJason M. Bills {
107495820184SJason M. Bills     // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
1075cd225da8SJason M. Bills     // First get the Timestamp
1076f23b7296SEd Tanous     size_t space = logEntry.find_first_of(' ');
1077cd225da8SJason M. Bills     if (space == std::string::npos)
107895820184SJason M. Bills     {
107995820184SJason M. Bills         return 1;
108095820184SJason M. Bills     }
1081cd225da8SJason M. Bills     std::string timestamp = logEntry.substr(0, space);
1082cd225da8SJason M. Bills     // Then get the log contents
1083f23b7296SEd Tanous     size_t entryStart = logEntry.find_first_not_of(' ', space);
1084cd225da8SJason M. Bills     if (entryStart == std::string::npos)
1085cd225da8SJason M. Bills     {
1086cd225da8SJason M. Bills         return 1;
1087cd225da8SJason M. Bills     }
1088cd225da8SJason M. Bills     std::string_view entry(logEntry);
1089cd225da8SJason M. Bills     entry.remove_prefix(entryStart);
1090cd225da8SJason M. Bills     // Use split to separate the entry into its fields
1091cd225da8SJason M. Bills     std::vector<std::string> logEntryFields;
1092cd225da8SJason M. Bills     boost::split(logEntryFields, entry, boost::is_any_of(","),
1093cd225da8SJason M. Bills                  boost::token_compress_on);
1094cd225da8SJason M. Bills     // We need at least a MessageId to be valid
1095cd225da8SJason M. Bills     if (logEntryFields.size() < 1)
1096cd225da8SJason M. Bills     {
1097cd225da8SJason M. Bills         return 1;
1098cd225da8SJason M. Bills     }
1099cd225da8SJason M. Bills     std::string& messageID = logEntryFields[0];
110095820184SJason M. Bills 
11014851d45dSJason M. Bills     // Get the Message from the MessageRegistry
11024851d45dSJason M. Bills     const message_registries::Message* message =
11034851d45dSJason M. Bills         message_registries::getMessage(messageID);
1104c4bf6374SJason M. Bills 
11054851d45dSJason M. Bills     std::string msg;
11064851d45dSJason M. Bills     std::string severity;
11074851d45dSJason M. Bills     if (message != nullptr)
1108c4bf6374SJason M. Bills     {
11094851d45dSJason M. Bills         msg = message->message;
11104851d45dSJason M. Bills         severity = message->severity;
1111c4bf6374SJason M. Bills     }
1112c4bf6374SJason M. Bills 
111315a86ff6SJason M. Bills     // Get the MessageArgs from the log if there are any
111415a86ff6SJason M. Bills     boost::beast::span<std::string> messageArgs;
111515a86ff6SJason M. Bills     if (logEntryFields.size() > 1)
111615a86ff6SJason M. Bills     {
111715a86ff6SJason M. Bills         std::string& messageArgsStart = logEntryFields[1];
111815a86ff6SJason M. Bills         // If the first string is empty, assume there are no MessageArgs
111915a86ff6SJason M. Bills         std::size_t messageArgsSize = 0;
112015a86ff6SJason M. Bills         if (!messageArgsStart.empty())
112115a86ff6SJason M. Bills         {
112215a86ff6SJason M. Bills             messageArgsSize = logEntryFields.size() - 1;
112315a86ff6SJason M. Bills         }
112415a86ff6SJason M. Bills 
112523a21a1cSEd Tanous         messageArgs = {&messageArgsStart, messageArgsSize};
1126c4bf6374SJason M. Bills 
11274851d45dSJason M. Bills         // Fill the MessageArgs into the Message
112895820184SJason M. Bills         int i = 0;
112995820184SJason M. Bills         for (const std::string& messageArg : messageArgs)
11304851d45dSJason M. Bills         {
113195820184SJason M. Bills             std::string argStr = "%" + std::to_string(++i);
11324851d45dSJason M. Bills             size_t argPos = msg.find(argStr);
11334851d45dSJason M. Bills             if (argPos != std::string::npos)
11344851d45dSJason M. Bills             {
113595820184SJason M. Bills                 msg.replace(argPos, argStr.length(), messageArg);
11364851d45dSJason M. Bills             }
11374851d45dSJason M. Bills         }
113815a86ff6SJason M. Bills     }
11394851d45dSJason M. Bills 
114095820184SJason M. Bills     // Get the Created time from the timestamp. The log timestamp is in RFC3339
114195820184SJason M. Bills     // format which matches the Redfish format except for the fractional seconds
114295820184SJason M. Bills     // between the '.' and the '+', so just remove them.
1143f23b7296SEd Tanous     std::size_t dot = timestamp.find_first_of('.');
1144f23b7296SEd Tanous     std::size_t plus = timestamp.find_first_of('+');
114595820184SJason M. Bills     if (dot != std::string::npos && plus != std::string::npos)
1146c4bf6374SJason M. Bills     {
114795820184SJason M. Bills         timestamp.erase(dot, plus - dot);
1148c4bf6374SJason M. Bills     }
1149c4bf6374SJason M. Bills 
1150c4bf6374SJason M. Bills     // Fill in the log entry with the gathered data
115195820184SJason M. Bills     logEntryJson = {
1152cb92c03bSAndrew Geissler         {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
1153029573d4SEd Tanous         {"@odata.id",
1154897967deSJason M. Bills          "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
115595820184SJason M. Bills              logEntryID},
1156c4bf6374SJason M. Bills         {"Name", "System Event Log Entry"},
115795820184SJason M. Bills         {"Id", logEntryID},
115895820184SJason M. Bills         {"Message", std::move(msg)},
115995820184SJason M. Bills         {"MessageId", std::move(messageID)},
1160f23b7296SEd Tanous         {"MessageArgs", messageArgs},
1161c4bf6374SJason M. Bills         {"EntryType", "Event"},
116295820184SJason M. Bills         {"Severity", std::move(severity)},
116395820184SJason M. Bills         {"Created", std::move(timestamp)}};
1164c4bf6374SJason M. Bills     return 0;
1165c4bf6374SJason M. Bills }
1166c4bf6374SJason M. Bills 
116727062605SAnthony Wilson class JournalEventLogEntryCollection : public Node
1168c4bf6374SJason M. Bills {
1169c4bf6374SJason M. Bills   public:
117052cc112dSEd Tanous     JournalEventLogEntryCollection(App& app) :
1171029573d4SEd Tanous         Node(app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
1172c4bf6374SJason M. Bills     {
1173c4bf6374SJason M. Bills         entityPrivileges = {
1174c4bf6374SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1175c4bf6374SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1176c4bf6374SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1177c4bf6374SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1178c4bf6374SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1179c4bf6374SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1180c4bf6374SJason M. Bills     }
1181c4bf6374SJason M. Bills 
1182c4bf6374SJason M. Bills   private:
11838d1b46d7Szhanghch05     void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
11848d1b46d7Szhanghch05                const crow::Request& req,
1185cb13a392SEd Tanous                const std::vector<std::string>&) override
1186c4bf6374SJason M. Bills     {
1187271584abSEd Tanous         uint64_t skip = 0;
1188271584abSEd Tanous         uint64_t top = maxEntriesPerPage; // Show max entries by default
11898d1b46d7Szhanghch05         if (!getSkipParam(asyncResp, req, skip))
1190c4bf6374SJason M. Bills         {
1191c4bf6374SJason M. Bills             return;
1192c4bf6374SJason M. Bills         }
11938d1b46d7Szhanghch05         if (!getTopParam(asyncResp, req, top))
1194c4bf6374SJason M. Bills         {
1195c4bf6374SJason M. Bills             return;
1196c4bf6374SJason M. Bills         }
1197c4bf6374SJason M. Bills         // Collections don't include the static data added by SubRoute because
1198c4bf6374SJason M. Bills         // it has a duplicate entry for members
1199c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1200c4bf6374SJason M. Bills             "#LogEntryCollection.LogEntryCollection";
1201c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
1202029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1203c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1204c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] =
1205c4bf6374SJason M. Bills             "Collection of System Event Log Entries";
1206cb92c03bSAndrew Geissler 
1207c4bf6374SJason M. Bills         nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1208c4bf6374SJason M. Bills         logEntryArray = nlohmann::json::array();
120995820184SJason M. Bills         // Go through the log files and create a unique ID for each entry
121095820184SJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
121195820184SJason M. Bills         getRedfishLogFiles(redfishLogFiles);
1212b01bf299SEd Tanous         uint64_t entryCount = 0;
1213cd225da8SJason M. Bills         std::string logEntry;
121495820184SJason M. Bills 
121595820184SJason M. Bills         // Oldest logs are in the last file, so start there and loop backwards
1216cd225da8SJason M. Bills         for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1217cd225da8SJason M. Bills              it++)
1218c4bf6374SJason M. Bills         {
1219cd225da8SJason M. Bills             std::ifstream logStream(*it);
122095820184SJason M. Bills             if (!logStream.is_open())
1221c4bf6374SJason M. Bills             {
1222c4bf6374SJason M. Bills                 continue;
1223c4bf6374SJason M. Bills             }
1224c4bf6374SJason M. Bills 
1225e85d6b16SJason M. Bills             // Reset the unique ID on the first entry
1226e85d6b16SJason M. Bills             bool firstEntry = true;
122795820184SJason M. Bills             while (std::getline(logStream, logEntry))
122895820184SJason M. Bills             {
1229c4bf6374SJason M. Bills                 entryCount++;
1230c4bf6374SJason M. Bills                 // Handle paging using skip (number of entries to skip from the
1231c4bf6374SJason M. Bills                 // start) and top (number of entries to display)
1232c4bf6374SJason M. Bills                 if (entryCount <= skip || entryCount > skip + top)
1233c4bf6374SJason M. Bills                 {
1234c4bf6374SJason M. Bills                     continue;
1235c4bf6374SJason M. Bills                 }
1236c4bf6374SJason M. Bills 
1237c4bf6374SJason M. Bills                 std::string idStr;
1238e85d6b16SJason M. Bills                 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1239c4bf6374SJason M. Bills                 {
1240c4bf6374SJason M. Bills                     continue;
1241c4bf6374SJason M. Bills                 }
1242c4bf6374SJason M. Bills 
1243e85d6b16SJason M. Bills                 if (firstEntry)
1244e85d6b16SJason M. Bills                 {
1245e85d6b16SJason M. Bills                     firstEntry = false;
1246e85d6b16SJason M. Bills                 }
1247e85d6b16SJason M. Bills 
1248c4bf6374SJason M. Bills                 logEntryArray.push_back({});
1249c4bf6374SJason M. Bills                 nlohmann::json& bmcLogEntry = logEntryArray.back();
125095820184SJason M. Bills                 if (fillEventLogEntryJson(idStr, logEntry, bmcLogEntry) != 0)
1251c4bf6374SJason M. Bills                 {
1252c4bf6374SJason M. Bills                     messages::internalError(asyncResp->res);
1253c4bf6374SJason M. Bills                     return;
1254c4bf6374SJason M. Bills                 }
1255c4bf6374SJason M. Bills             }
125695820184SJason M. Bills         }
1257c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
1258c4bf6374SJason M. Bills         if (skip + top < entryCount)
1259c4bf6374SJason M. Bills         {
1260c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Members@odata.nextLink"] =
126195820184SJason M. Bills                 "/redfish/v1/Systems/system/LogServices/EventLog/"
126295820184SJason M. Bills                 "Entries?$skip=" +
1263c4bf6374SJason M. Bills                 std::to_string(skip + top);
1264c4bf6374SJason M. Bills         }
126508a4e4b5SAnthony Wilson     }
126608a4e4b5SAnthony Wilson };
126708a4e4b5SAnthony Wilson 
1268897967deSJason M. Bills class JournalEventLogEntry : public Node
1269897967deSJason M. Bills {
1270897967deSJason M. Bills   public:
127152cc112dSEd Tanous     JournalEventLogEntry(App& app) :
1272897967deSJason M. Bills         Node(app,
1273897967deSJason M. Bills              "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/",
1274897967deSJason M. Bills              std::string())
1275897967deSJason M. Bills     {
1276897967deSJason M. Bills         entityPrivileges = {
1277897967deSJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1278897967deSJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1279897967deSJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1280897967deSJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1281897967deSJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1282897967deSJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1283897967deSJason M. Bills     }
1284897967deSJason M. Bills 
1285897967deSJason M. Bills   private:
12868d1b46d7Szhanghch05     void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
12878d1b46d7Szhanghch05                const crow::Request&,
1288897967deSJason M. Bills                const std::vector<std::string>& params) override
1289897967deSJason M. Bills     {
12908d1b46d7Szhanghch05 
1291897967deSJason M. Bills         if (params.size() != 1)
1292897967deSJason M. Bills         {
1293897967deSJason M. Bills             messages::internalError(asyncResp->res);
1294897967deSJason M. Bills             return;
1295897967deSJason M. Bills         }
1296897967deSJason M. Bills         const std::string& targetID = params[0];
1297897967deSJason M. Bills 
1298897967deSJason M. Bills         // Go through the log files and check the unique ID for each entry to
1299897967deSJason M. Bills         // find the target entry
1300897967deSJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
1301897967deSJason M. Bills         getRedfishLogFiles(redfishLogFiles);
1302897967deSJason M. Bills         std::string logEntry;
1303897967deSJason M. Bills 
1304897967deSJason M. Bills         // Oldest logs are in the last file, so start there and loop backwards
1305897967deSJason M. Bills         for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1306897967deSJason M. Bills              it++)
1307897967deSJason M. Bills         {
1308897967deSJason M. Bills             std::ifstream logStream(*it);
1309897967deSJason M. Bills             if (!logStream.is_open())
1310897967deSJason M. Bills             {
1311897967deSJason M. Bills                 continue;
1312897967deSJason M. Bills             }
1313897967deSJason M. Bills 
1314897967deSJason M. Bills             // Reset the unique ID on the first entry
1315897967deSJason M. Bills             bool firstEntry = true;
1316897967deSJason M. Bills             while (std::getline(logStream, logEntry))
1317897967deSJason M. Bills             {
1318897967deSJason M. Bills                 std::string idStr;
1319897967deSJason M. Bills                 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1320897967deSJason M. Bills                 {
1321897967deSJason M. Bills                     continue;
1322897967deSJason M. Bills                 }
1323897967deSJason M. Bills 
1324897967deSJason M. Bills                 if (firstEntry)
1325897967deSJason M. Bills                 {
1326897967deSJason M. Bills                     firstEntry = false;
1327897967deSJason M. Bills                 }
1328897967deSJason M. Bills 
1329897967deSJason M. Bills                 if (idStr == targetID)
1330897967deSJason M. Bills                 {
1331897967deSJason M. Bills                     if (fillEventLogEntryJson(idStr, logEntry,
1332897967deSJason M. Bills                                               asyncResp->res.jsonValue) != 0)
1333897967deSJason M. Bills                     {
1334897967deSJason M. Bills                         messages::internalError(asyncResp->res);
1335897967deSJason M. Bills                         return;
1336897967deSJason M. Bills                     }
1337897967deSJason M. Bills                     return;
1338897967deSJason M. Bills                 }
1339897967deSJason M. Bills             }
1340897967deSJason M. Bills         }
1341897967deSJason M. Bills         // Requested ID was not found
1342897967deSJason M. Bills         messages::resourceMissingAtURI(asyncResp->res, targetID);
1343897967deSJason M. Bills     }
1344897967deSJason M. Bills };
1345897967deSJason M. Bills 
134608a4e4b5SAnthony Wilson class DBusEventLogEntryCollection : public Node
134708a4e4b5SAnthony Wilson {
134808a4e4b5SAnthony Wilson   public:
134952cc112dSEd Tanous     DBusEventLogEntryCollection(App& app) :
135008a4e4b5SAnthony Wilson         Node(app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
135108a4e4b5SAnthony Wilson     {
135208a4e4b5SAnthony Wilson         entityPrivileges = {
135308a4e4b5SAnthony Wilson             {boost::beast::http::verb::get, {{"Login"}}},
135408a4e4b5SAnthony Wilson             {boost::beast::http::verb::head, {{"Login"}}},
135508a4e4b5SAnthony Wilson             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
135608a4e4b5SAnthony Wilson             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
135708a4e4b5SAnthony Wilson             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
135808a4e4b5SAnthony Wilson             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
135908a4e4b5SAnthony Wilson     }
136008a4e4b5SAnthony Wilson 
136108a4e4b5SAnthony Wilson   private:
13628d1b46d7Szhanghch05     void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
13638d1b46d7Szhanghch05                const crow::Request&, const std::vector<std::string>&) override
136408a4e4b5SAnthony Wilson     {
136508a4e4b5SAnthony Wilson 
136608a4e4b5SAnthony Wilson         // Collections don't include the static data added by SubRoute because
136708a4e4b5SAnthony Wilson         // it has a duplicate entry for members
136808a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["@odata.type"] =
136908a4e4b5SAnthony Wilson             "#LogEntryCollection.LogEntryCollection";
137008a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["@odata.id"] =
137108a4e4b5SAnthony Wilson             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
137208a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
137308a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["Description"] =
137408a4e4b5SAnthony Wilson             "Collection of System Event Log Entries";
137508a4e4b5SAnthony Wilson 
1376cb92c03bSAndrew Geissler         // DBus implementation of EventLog/Entries
1377cb92c03bSAndrew Geissler         // Make call to Logging Service to find all log entry objects
1378cb92c03bSAndrew Geissler         crow::connections::systemBus->async_method_call(
1379cb92c03bSAndrew Geissler             [asyncResp](const boost::system::error_code ec,
1380cb92c03bSAndrew Geissler                         GetManagedObjectsType& resp) {
1381cb92c03bSAndrew Geissler                 if (ec)
1382cb92c03bSAndrew Geissler                 {
1383cb92c03bSAndrew Geissler                     // TODO Handle for specific error code
1384cb92c03bSAndrew Geissler                     BMCWEB_LOG_ERROR
1385cb92c03bSAndrew Geissler                         << "getLogEntriesIfaceData resp_handler got error "
1386cb92c03bSAndrew Geissler                         << ec;
1387cb92c03bSAndrew Geissler                     messages::internalError(asyncResp->res);
1388cb92c03bSAndrew Geissler                     return;
1389cb92c03bSAndrew Geissler                 }
1390cb92c03bSAndrew Geissler                 nlohmann::json& entriesArray =
1391cb92c03bSAndrew Geissler                     asyncResp->res.jsonValue["Members"];
1392cb92c03bSAndrew Geissler                 entriesArray = nlohmann::json::array();
1393cb92c03bSAndrew Geissler                 for (auto& objectPath : resp)
1394cb92c03bSAndrew Geissler                 {
139566664f25SEd Tanous                     uint32_t* id = nullptr;
139666664f25SEd Tanous                     std::time_t timestamp{};
1397d139c236SGeorge Liu                     std::time_t updateTimestamp{};
139866664f25SEd Tanous                     std::string* severity = nullptr;
139966664f25SEd Tanous                     std::string* message = nullptr;
1400f86bb901SAdriana Kobylak                     std::string* filePath = nullptr;
140175710de2SXiaochao Ma                     bool resolved = false;
1402f86bb901SAdriana Kobylak                     for (auto& interfaceMap : objectPath.second)
1403f86bb901SAdriana Kobylak                     {
1404f86bb901SAdriana Kobylak                         if (interfaceMap.first ==
1405f86bb901SAdriana Kobylak                             "xyz.openbmc_project.Logging.Entry")
1406f86bb901SAdriana Kobylak                         {
1407cb92c03bSAndrew Geissler                             for (auto& propertyMap : interfaceMap.second)
1408cb92c03bSAndrew Geissler                             {
1409cb92c03bSAndrew Geissler                                 if (propertyMap.first == "Id")
1410cb92c03bSAndrew Geissler                                 {
1411f86bb901SAdriana Kobylak                                     id = std::get_if<uint32_t>(
1412f86bb901SAdriana Kobylak                                         &propertyMap.second);
1413cb92c03bSAndrew Geissler                                 }
1414cb92c03bSAndrew Geissler                                 else if (propertyMap.first == "Timestamp")
1415cb92c03bSAndrew Geissler                                 {
1416cb92c03bSAndrew Geissler                                     const uint64_t* millisTimeStamp =
1417f86bb901SAdriana Kobylak                                         std::get_if<uint64_t>(
1418f86bb901SAdriana Kobylak                                             &propertyMap.second);
1419ae34c8e8SAdriana Kobylak                                     if (millisTimeStamp != nullptr)
1420ebd45906SGeorge Liu                                     {
1421d139c236SGeorge Liu                                         timestamp = crow::utility::getTimestamp(
1422cb92c03bSAndrew Geissler                                             *millisTimeStamp);
1423d139c236SGeorge Liu                                     }
1424ebd45906SGeorge Liu                                 }
1425d139c236SGeorge Liu                                 else if (propertyMap.first == "UpdateTimestamp")
1426d139c236SGeorge Liu                                 {
1427d139c236SGeorge Liu                                     const uint64_t* millisTimeStamp =
1428f86bb901SAdriana Kobylak                                         std::get_if<uint64_t>(
1429f86bb901SAdriana Kobylak                                             &propertyMap.second);
1430ae34c8e8SAdriana Kobylak                                     if (millisTimeStamp != nullptr)
1431ebd45906SGeorge Liu                                     {
1432ebd45906SGeorge Liu                                         updateTimestamp =
1433ebd45906SGeorge Liu                                             crow::utility::getTimestamp(
1434d139c236SGeorge Liu                                                 *millisTimeStamp);
1435cb92c03bSAndrew Geissler                                     }
1436ebd45906SGeorge Liu                                 }
1437cb92c03bSAndrew Geissler                                 else if (propertyMap.first == "Severity")
1438cb92c03bSAndrew Geissler                                 {
1439cb92c03bSAndrew Geissler                                     severity = std::get_if<std::string>(
1440cb92c03bSAndrew Geissler                                         &propertyMap.second);
1441cb92c03bSAndrew Geissler                                 }
1442cb92c03bSAndrew Geissler                                 else if (propertyMap.first == "Message")
1443cb92c03bSAndrew Geissler                                 {
1444cb92c03bSAndrew Geissler                                     message = std::get_if<std::string>(
1445cb92c03bSAndrew Geissler                                         &propertyMap.second);
1446ae34c8e8SAdriana Kobylak                                 }
144775710de2SXiaochao Ma                                 else if (propertyMap.first == "Resolved")
144875710de2SXiaochao Ma                                 {
144975710de2SXiaochao Ma                                     bool* resolveptr =
145075710de2SXiaochao Ma                                         std::get_if<bool>(&propertyMap.second);
145175710de2SXiaochao Ma                                     if (resolveptr == nullptr)
145275710de2SXiaochao Ma                                     {
145375710de2SXiaochao Ma                                         messages::internalError(asyncResp->res);
145475710de2SXiaochao Ma                                         return;
145575710de2SXiaochao Ma                                     }
145675710de2SXiaochao Ma                                     resolved = *resolveptr;
145775710de2SXiaochao Ma                                 }
1458ae34c8e8SAdriana Kobylak                             }
1459ae34c8e8SAdriana Kobylak                             if (id == nullptr || message == nullptr ||
1460ae34c8e8SAdriana Kobylak                                 severity == nullptr)
1461cb92c03bSAndrew Geissler                             {
14620f1d8ed9SAsmitha Karunanithi                                 messages::internalError(asyncResp->res);
1463ae34c8e8SAdriana Kobylak                                 return;
1464cb92c03bSAndrew Geissler                             }
1465f86bb901SAdriana Kobylak                         }
1466f86bb901SAdriana Kobylak                         else if (interfaceMap.first ==
1467f86bb901SAdriana Kobylak                                  "xyz.openbmc_project.Common.FilePath")
1468f86bb901SAdriana Kobylak                         {
1469f86bb901SAdriana Kobylak                             for (auto& propertyMap : interfaceMap.second)
1470f86bb901SAdriana Kobylak                             {
1471f86bb901SAdriana Kobylak                                 if (propertyMap.first == "Path")
1472f86bb901SAdriana Kobylak                                 {
1473f86bb901SAdriana Kobylak                                     filePath = std::get_if<std::string>(
1474f86bb901SAdriana Kobylak                                         &propertyMap.second);
1475f86bb901SAdriana Kobylak                                 }
1476f86bb901SAdriana Kobylak                             }
1477f86bb901SAdriana Kobylak                         }
1478f86bb901SAdriana Kobylak                     }
1479f86bb901SAdriana Kobylak                     // Object path without the xyz.openbmc_project.Logging.Entry
1480f86bb901SAdriana Kobylak                     // interface, ignore and continue.
1481f86bb901SAdriana Kobylak                     if (id == nullptr || message == nullptr ||
1482f86bb901SAdriana Kobylak                         severity == nullptr)
1483f86bb901SAdriana Kobylak                     {
1484f86bb901SAdriana Kobylak                         continue;
1485f86bb901SAdriana Kobylak                     }
1486f86bb901SAdriana Kobylak                     entriesArray.push_back({});
1487f86bb901SAdriana Kobylak                     nlohmann::json& thisEntry = entriesArray.back();
1488f86bb901SAdriana Kobylak                     thisEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
1489f86bb901SAdriana Kobylak                     thisEntry["@odata.id"] = "/redfish/v1/Systems/system/"
1490f86bb901SAdriana Kobylak                                              "LogServices/EventLog/Entries/" +
1491f86bb901SAdriana Kobylak                                              std::to_string(*id);
1492f86bb901SAdriana Kobylak                     thisEntry["Name"] = "System Event Log Entry";
1493f86bb901SAdriana Kobylak                     thisEntry["Id"] = std::to_string(*id);
1494f86bb901SAdriana Kobylak                     thisEntry["Message"] = *message;
1495f86bb901SAdriana Kobylak                     thisEntry["Resolved"] = resolved;
1496f86bb901SAdriana Kobylak                     thisEntry["EntryType"] = "Event";
1497f86bb901SAdriana Kobylak                     thisEntry["Severity"] =
1498f86bb901SAdriana Kobylak                         translateSeverityDbusToRedfish(*severity);
1499f86bb901SAdriana Kobylak                     thisEntry["Created"] =
1500f86bb901SAdriana Kobylak                         crow::utility::getDateTime(timestamp);
1501f86bb901SAdriana Kobylak                     thisEntry["Modified"] =
1502f86bb901SAdriana Kobylak                         crow::utility::getDateTime(updateTimestamp);
1503f86bb901SAdriana Kobylak                     if (filePath != nullptr)
1504f86bb901SAdriana Kobylak                     {
1505f86bb901SAdriana Kobylak                         thisEntry["AdditionalDataURI"] =
1506cb92c03bSAndrew Geissler                             "/redfish/v1/Systems/system/LogServices/EventLog/"
1507*de8d94a3SAbhishek Patel                             "Entries/" +
1508*de8d94a3SAbhishek Patel                             std::to_string(*id) + "/attachment";
1509cb92c03bSAndrew Geissler                     }
1510cb92c03bSAndrew Geissler                 }
1511cb92c03bSAndrew Geissler                 std::sort(entriesArray.begin(), entriesArray.end(),
1512cb92c03bSAndrew Geissler                           [](const nlohmann::json& left,
1513cb92c03bSAndrew Geissler                              const nlohmann::json& right) {
1514cb92c03bSAndrew Geissler                               return (left["Id"] <= right["Id"]);
1515cb92c03bSAndrew Geissler                           });
1516cb92c03bSAndrew Geissler                 asyncResp->res.jsonValue["Members@odata.count"] =
1517cb92c03bSAndrew Geissler                     entriesArray.size();
1518cb92c03bSAndrew Geissler             },
1519cb92c03bSAndrew Geissler             "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
1520cb92c03bSAndrew Geissler             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1521c4bf6374SJason M. Bills     }
1522c4bf6374SJason M. Bills };
1523c4bf6374SJason M. Bills 
152408a4e4b5SAnthony Wilson class DBusEventLogEntry : public Node
1525c4bf6374SJason M. Bills {
1526c4bf6374SJason M. Bills   public:
152752cc112dSEd Tanous     DBusEventLogEntry(App& app) :
1528c4bf6374SJason M. Bills         Node(app,
1529029573d4SEd Tanous              "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/",
1530029573d4SEd Tanous              std::string())
1531c4bf6374SJason M. Bills     {
1532c4bf6374SJason M. Bills         entityPrivileges = {
1533c4bf6374SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1534c4bf6374SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1535c4bf6374SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1536c4bf6374SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1537c4bf6374SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1538c4bf6374SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1539c4bf6374SJason M. Bills     }
1540c4bf6374SJason M. Bills 
1541c4bf6374SJason M. Bills   private:
15428d1b46d7Szhanghch05     void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
15438d1b46d7Szhanghch05                const crow::Request&,
1544c4bf6374SJason M. Bills                const std::vector<std::string>& params) override
1545c4bf6374SJason M. Bills     {
15468d1b46d7Szhanghch05 
1547029573d4SEd Tanous         if (params.size() != 1)
1548c4bf6374SJason M. Bills         {
1549c4bf6374SJason M. Bills             messages::internalError(asyncResp->res);
1550c4bf6374SJason M. Bills             return;
1551c4bf6374SJason M. Bills         }
1552ae34c8e8SAdriana Kobylak         std::string entryID = params[0];
1553ae34c8e8SAdriana Kobylak         dbus::utility::escapePathForDbus(entryID);
1554cb92c03bSAndrew Geissler 
1555cb92c03bSAndrew Geissler         // DBus implementation of EventLog/Entries
1556cb92c03bSAndrew Geissler         // Make call to Logging Service to find all log entry objects
1557cb92c03bSAndrew Geissler         crow::connections::systemBus->async_method_call(
1558cb92c03bSAndrew Geissler             [asyncResp, entryID](const boost::system::error_code ec,
1559cb92c03bSAndrew Geissler                                  GetManagedPropertyType& resp) {
1560ae34c8e8SAdriana Kobylak                 if (ec.value() == EBADR)
1561ae34c8e8SAdriana Kobylak                 {
1562ae34c8e8SAdriana Kobylak                     messages::resourceNotFound(asyncResp->res, "EventLogEntry",
1563ae34c8e8SAdriana Kobylak                                                entryID);
1564ae34c8e8SAdriana Kobylak                     return;
1565ae34c8e8SAdriana Kobylak                 }
1566cb92c03bSAndrew Geissler                 if (ec)
1567cb92c03bSAndrew Geissler                 {
1568cb92c03bSAndrew Geissler                     BMCWEB_LOG_ERROR
1569cb92c03bSAndrew Geissler                         << "EventLogEntry (DBus) resp_handler got error " << ec;
1570cb92c03bSAndrew Geissler                     messages::internalError(asyncResp->res);
1571cb92c03bSAndrew Geissler                     return;
1572cb92c03bSAndrew Geissler                 }
157366664f25SEd Tanous                 uint32_t* id = nullptr;
157466664f25SEd Tanous                 std::time_t timestamp{};
1575d139c236SGeorge Liu                 std::time_t updateTimestamp{};
157666664f25SEd Tanous                 std::string* severity = nullptr;
157766664f25SEd Tanous                 std::string* message = nullptr;
1578f86bb901SAdriana Kobylak                 std::string* filePath = nullptr;
157975710de2SXiaochao Ma                 bool resolved = false;
1580d139c236SGeorge Liu 
1581cb92c03bSAndrew Geissler                 for (auto& propertyMap : resp)
1582cb92c03bSAndrew Geissler                 {
1583cb92c03bSAndrew Geissler                     if (propertyMap.first == "Id")
1584cb92c03bSAndrew Geissler                     {
1585cb92c03bSAndrew Geissler                         id = std::get_if<uint32_t>(&propertyMap.second);
1586cb92c03bSAndrew Geissler                     }
1587cb92c03bSAndrew Geissler                     else if (propertyMap.first == "Timestamp")
1588cb92c03bSAndrew Geissler                     {
1589cb92c03bSAndrew Geissler                         const uint64_t* millisTimeStamp =
1590cb92c03bSAndrew Geissler                             std::get_if<uint64_t>(&propertyMap.second);
1591ae34c8e8SAdriana Kobylak                         if (millisTimeStamp != nullptr)
1592ebd45906SGeorge Liu                         {
1593cb92c03bSAndrew Geissler                             timestamp =
1594d139c236SGeorge Liu                                 crow::utility::getTimestamp(*millisTimeStamp);
1595d139c236SGeorge Liu                         }
1596ebd45906SGeorge Liu                     }
1597d139c236SGeorge Liu                     else if (propertyMap.first == "UpdateTimestamp")
1598d139c236SGeorge Liu                     {
1599d139c236SGeorge Liu                         const uint64_t* millisTimeStamp =
1600d139c236SGeorge Liu                             std::get_if<uint64_t>(&propertyMap.second);
1601ae34c8e8SAdriana Kobylak                         if (millisTimeStamp != nullptr)
1602ebd45906SGeorge Liu                         {
1603d139c236SGeorge Liu                             updateTimestamp =
1604d139c236SGeorge Liu                                 crow::utility::getTimestamp(*millisTimeStamp);
1605cb92c03bSAndrew Geissler                         }
1606ebd45906SGeorge Liu                     }
1607cb92c03bSAndrew Geissler                     else if (propertyMap.first == "Severity")
1608cb92c03bSAndrew Geissler                     {
1609cb92c03bSAndrew Geissler                         severity =
1610cb92c03bSAndrew Geissler                             std::get_if<std::string>(&propertyMap.second);
1611cb92c03bSAndrew Geissler                     }
1612cb92c03bSAndrew Geissler                     else if (propertyMap.first == "Message")
1613cb92c03bSAndrew Geissler                     {
1614cb92c03bSAndrew Geissler                         message = std::get_if<std::string>(&propertyMap.second);
1615cb92c03bSAndrew Geissler                     }
161675710de2SXiaochao Ma                     else if (propertyMap.first == "Resolved")
161775710de2SXiaochao Ma                     {
161875710de2SXiaochao Ma                         bool* resolveptr =
161975710de2SXiaochao Ma                             std::get_if<bool>(&propertyMap.second);
162075710de2SXiaochao Ma                         if (resolveptr == nullptr)
162175710de2SXiaochao Ma                         {
162275710de2SXiaochao Ma                             messages::internalError(asyncResp->res);
162375710de2SXiaochao Ma                             return;
162475710de2SXiaochao Ma                         }
162575710de2SXiaochao Ma                         resolved = *resolveptr;
162675710de2SXiaochao Ma                     }
1627f86bb901SAdriana Kobylak                     else if (propertyMap.first == "Path")
1628f86bb901SAdriana Kobylak                     {
1629f86bb901SAdriana Kobylak                         filePath =
1630f86bb901SAdriana Kobylak                             std::get_if<std::string>(&propertyMap.second);
1631f86bb901SAdriana Kobylak                     }
1632cb92c03bSAndrew Geissler                 }
1633271584abSEd Tanous                 if (id == nullptr || message == nullptr || severity == nullptr)
1634271584abSEd Tanous                 {
1635ae34c8e8SAdriana Kobylak                     messages::internalError(asyncResp->res);
1636271584abSEd Tanous                     return;
1637271584abSEd Tanous                 }
1638f86bb901SAdriana Kobylak                 asyncResp->res.jsonValue["@odata.type"] =
1639f86bb901SAdriana Kobylak                     "#LogEntry.v1_8_0.LogEntry";
1640f86bb901SAdriana Kobylak                 asyncResp->res.jsonValue["@odata.id"] =
1641f86bb901SAdriana Kobylak                     "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1642f86bb901SAdriana Kobylak                     std::to_string(*id);
1643f86bb901SAdriana Kobylak                 asyncResp->res.jsonValue["Name"] = "System Event Log Entry";
1644f86bb901SAdriana Kobylak                 asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1645f86bb901SAdriana Kobylak                 asyncResp->res.jsonValue["Message"] = *message;
1646f86bb901SAdriana Kobylak                 asyncResp->res.jsonValue["Resolved"] = resolved;
1647f86bb901SAdriana Kobylak                 asyncResp->res.jsonValue["EntryType"] = "Event";
1648f86bb901SAdriana Kobylak                 asyncResp->res.jsonValue["Severity"] =
1649f86bb901SAdriana Kobylak                     translateSeverityDbusToRedfish(*severity);
1650f86bb901SAdriana Kobylak                 asyncResp->res.jsonValue["Created"] =
1651f86bb901SAdriana Kobylak                     crow::utility::getDateTime(timestamp);
1652f86bb901SAdriana Kobylak                 asyncResp->res.jsonValue["Modified"] =
1653f86bb901SAdriana Kobylak                     crow::utility::getDateTime(updateTimestamp);
1654f86bb901SAdriana Kobylak                 if (filePath != nullptr)
1655f86bb901SAdriana Kobylak                 {
1656f86bb901SAdriana Kobylak                     asyncResp->res.jsonValue["AdditionalDataURI"] =
1657cb92c03bSAndrew Geissler                         "/redfish/v1/Systems/system/LogServices/EventLog/"
1658*de8d94a3SAbhishek Patel                         "Entries/" +
1659*de8d94a3SAbhishek Patel                         std::to_string(*id) + "/attachment";
1660f86bb901SAdriana Kobylak                 }
1661cb92c03bSAndrew Geissler             },
1662cb92c03bSAndrew Geissler             "xyz.openbmc_project.Logging",
1663cb92c03bSAndrew Geissler             "/xyz/openbmc_project/logging/entry/" + entryID,
1664f86bb901SAdriana Kobylak             "org.freedesktop.DBus.Properties", "GetAll", "");
1665c4bf6374SJason M. Bills     }
1666336e96c6SChicago Duan 
16678d1b46d7Szhanghch05     void doPatch(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
16688d1b46d7Szhanghch05                  const crow::Request& req,
166975710de2SXiaochao Ma                  const std::vector<std::string>& params) override
167075710de2SXiaochao Ma     {
167175710de2SXiaochao Ma 
167275710de2SXiaochao Ma         if (params.size() != 1)
167375710de2SXiaochao Ma         {
167475710de2SXiaochao Ma             messages::internalError(asyncResp->res);
167575710de2SXiaochao Ma             return;
167675710de2SXiaochao Ma         }
167775710de2SXiaochao Ma         std::string entryId = params[0];
167875710de2SXiaochao Ma 
167975710de2SXiaochao Ma         std::optional<bool> resolved;
168075710de2SXiaochao Ma 
16818d1b46d7Szhanghch05         if (!json_util::readJson(req, asyncResp->res, "Resolved", resolved))
168275710de2SXiaochao Ma         {
168375710de2SXiaochao Ma             return;
168475710de2SXiaochao Ma         }
168575710de2SXiaochao Ma 
168675710de2SXiaochao Ma         if (resolved)
168775710de2SXiaochao Ma         {
168875710de2SXiaochao Ma             BMCWEB_LOG_DEBUG << "Set Resolved";
168975710de2SXiaochao Ma 
169075710de2SXiaochao Ma             crow::connections::systemBus->async_method_call(
169175710de2SXiaochao Ma                 [asyncResp, resolved,
169275710de2SXiaochao Ma                  entryId](const boost::system::error_code ec) {
169375710de2SXiaochao Ma                     if (ec)
169475710de2SXiaochao Ma                     {
169575710de2SXiaochao Ma                         BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
169675710de2SXiaochao Ma                         messages::internalError(asyncResp->res);
169775710de2SXiaochao Ma                         return;
169875710de2SXiaochao Ma                     }
169975710de2SXiaochao Ma                 },
170075710de2SXiaochao Ma                 "xyz.openbmc_project.Logging",
170175710de2SXiaochao Ma                 "/xyz/openbmc_project/logging/entry/" + entryId,
170275710de2SXiaochao Ma                 "org.freedesktop.DBus.Properties", "Set",
170375710de2SXiaochao Ma                 "xyz.openbmc_project.Logging.Entry", "Resolved",
170475710de2SXiaochao Ma                 std::variant<bool>(*resolved));
170575710de2SXiaochao Ma         }
170675710de2SXiaochao Ma     }
170775710de2SXiaochao Ma 
17088d1b46d7Szhanghch05     void doDelete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
17098d1b46d7Szhanghch05                   const crow::Request&,
1710336e96c6SChicago Duan                   const std::vector<std::string>& params) override
1711336e96c6SChicago Duan     {
1712336e96c6SChicago Duan 
1713336e96c6SChicago Duan         BMCWEB_LOG_DEBUG << "Do delete single event entries.";
1714336e96c6SChicago Duan 
1715336e96c6SChicago Duan         if (params.size() != 1)
1716336e96c6SChicago Duan         {
1717336e96c6SChicago Duan             messages::internalError(asyncResp->res);
1718336e96c6SChicago Duan             return;
1719336e96c6SChicago Duan         }
1720336e96c6SChicago Duan         std::string entryID = params[0];
1721336e96c6SChicago Duan 
1722336e96c6SChicago Duan         dbus::utility::escapePathForDbus(entryID);
1723336e96c6SChicago Duan 
1724336e96c6SChicago Duan         // Process response from Logging service.
17253de8d8baSGeorge Liu         auto respHandler = [asyncResp,
17263de8d8baSGeorge Liu                             entryID](const boost::system::error_code ec) {
1727336e96c6SChicago Duan             BMCWEB_LOG_DEBUG << "EventLogEntry (DBus) doDelete callback: Done";
1728336e96c6SChicago Duan             if (ec)
1729336e96c6SChicago Duan             {
17303de8d8baSGeorge Liu                 if (ec.value() == EBADR)
17313de8d8baSGeorge Liu                 {
17323de8d8baSGeorge Liu                     messages::resourceNotFound(asyncResp->res, "LogEntry",
17333de8d8baSGeorge Liu                                                entryID);
17343de8d8baSGeorge Liu                     return;
17353de8d8baSGeorge Liu                 }
1736336e96c6SChicago Duan                 // TODO Handle for specific error code
1737336e96c6SChicago Duan                 BMCWEB_LOG_ERROR
1738336e96c6SChicago Duan                     << "EventLogEntry (DBus) doDelete respHandler got error "
1739336e96c6SChicago Duan                     << ec;
1740336e96c6SChicago Duan                 asyncResp->res.result(
1741336e96c6SChicago Duan                     boost::beast::http::status::internal_server_error);
1742336e96c6SChicago Duan                 return;
1743336e96c6SChicago Duan             }
1744336e96c6SChicago Duan 
1745336e96c6SChicago Duan             asyncResp->res.result(boost::beast::http::status::ok);
1746336e96c6SChicago Duan         };
1747336e96c6SChicago Duan 
1748336e96c6SChicago Duan         // Make call to Logging service to request Delete Log
1749336e96c6SChicago Duan         crow::connections::systemBus->async_method_call(
1750336e96c6SChicago Duan             respHandler, "xyz.openbmc_project.Logging",
1751336e96c6SChicago Duan             "/xyz/openbmc_project/logging/entry/" + entryID,
1752336e96c6SChicago Duan             "xyz.openbmc_project.Object.Delete", "Delete");
1753336e96c6SChicago Duan     }
1754c4bf6374SJason M. Bills };
1755c4bf6374SJason M. Bills 
1756400fd1fbSAdriana Kobylak class DBusEventLogEntryDownload : public Node
1757400fd1fbSAdriana Kobylak {
1758400fd1fbSAdriana Kobylak   public:
1759400fd1fbSAdriana Kobylak     DBusEventLogEntryDownload(App& app) :
1760*de8d94a3SAbhishek Patel         Node(app,
1761*de8d94a3SAbhishek Patel              "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/"
1762*de8d94a3SAbhishek Patel              "attachment",
1763400fd1fbSAdriana Kobylak              std::string())
1764400fd1fbSAdriana Kobylak     {
1765400fd1fbSAdriana Kobylak         entityPrivileges = {
1766400fd1fbSAdriana Kobylak             {boost::beast::http::verb::get, {{"Login"}}},
1767400fd1fbSAdriana Kobylak             {boost::beast::http::verb::head, {{"Login"}}},
1768400fd1fbSAdriana Kobylak             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1769400fd1fbSAdriana Kobylak             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1770400fd1fbSAdriana Kobylak             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1771400fd1fbSAdriana Kobylak             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1772400fd1fbSAdriana Kobylak     }
1773400fd1fbSAdriana Kobylak 
1774400fd1fbSAdriana Kobylak   private:
17758d1b46d7Szhanghch05     void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
17768d1b46d7Szhanghch05                const crow::Request& req,
1777400fd1fbSAdriana Kobylak                const std::vector<std::string>& params) override
1778400fd1fbSAdriana Kobylak     {
1779400fd1fbSAdriana Kobylak         if (params.size() != 1)
1780400fd1fbSAdriana Kobylak         {
1781400fd1fbSAdriana Kobylak             messages::internalError(asyncResp->res);
1782400fd1fbSAdriana Kobylak             return;
1783400fd1fbSAdriana Kobylak         }
1784400fd1fbSAdriana Kobylak 
1785400fd1fbSAdriana Kobylak         std::string_view acceptHeader = req.getHeaderValue("Accept");
1786400fd1fbSAdriana Kobylak         // The iterators in boost/http/rfc7230.hpp end the string if '/' is
1787400fd1fbSAdriana Kobylak         // found, so replace it with arbitrary character '|' which is not part
1788400fd1fbSAdriana Kobylak         // of the Accept header syntax.
1789400fd1fbSAdriana Kobylak         std::string acceptStr =
1790400fd1fbSAdriana Kobylak             boost::replace_all_copy(std::string(acceptHeader), "/", "|");
1791400fd1fbSAdriana Kobylak         boost::beast::http::ext_list acceptTypes{acceptStr};
1792400fd1fbSAdriana Kobylak         bool supported = false;
1793400fd1fbSAdriana Kobylak         for (const auto& type : acceptTypes)
1794400fd1fbSAdriana Kobylak         {
1795400fd1fbSAdriana Kobylak             if ((type.first == "*|*") ||
1796400fd1fbSAdriana Kobylak                 (type.first == "application|octet-stream"))
1797400fd1fbSAdriana Kobylak             {
1798400fd1fbSAdriana Kobylak                 supported = true;
1799400fd1fbSAdriana Kobylak                 break;
1800400fd1fbSAdriana Kobylak             }
1801400fd1fbSAdriana Kobylak         }
1802400fd1fbSAdriana Kobylak         if (!supported)
1803400fd1fbSAdriana Kobylak         {
1804400fd1fbSAdriana Kobylak             asyncResp->res.result(boost::beast::http::status::bad_request);
1805400fd1fbSAdriana Kobylak             return;
1806400fd1fbSAdriana Kobylak         }
1807400fd1fbSAdriana Kobylak 
1808400fd1fbSAdriana Kobylak         std::string entryID = params[0];
1809400fd1fbSAdriana Kobylak         dbus::utility::escapePathForDbus(entryID);
1810400fd1fbSAdriana Kobylak 
1811400fd1fbSAdriana Kobylak         crow::connections::systemBus->async_method_call(
1812400fd1fbSAdriana Kobylak             [asyncResp, entryID](const boost::system::error_code ec,
1813400fd1fbSAdriana Kobylak                                  const sdbusplus::message::unix_fd& unixfd) {
1814400fd1fbSAdriana Kobylak                 if (ec.value() == EBADR)
1815400fd1fbSAdriana Kobylak                 {
1816400fd1fbSAdriana Kobylak                     messages::resourceNotFound(asyncResp->res,
1817400fd1fbSAdriana Kobylak                                                "EventLogAttachment", entryID);
1818400fd1fbSAdriana Kobylak                     return;
1819400fd1fbSAdriana Kobylak                 }
1820400fd1fbSAdriana Kobylak                 if (ec)
1821400fd1fbSAdriana Kobylak                 {
1822400fd1fbSAdriana Kobylak                     BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1823400fd1fbSAdriana Kobylak                     messages::internalError(asyncResp->res);
1824400fd1fbSAdriana Kobylak                     return;
1825400fd1fbSAdriana Kobylak                 }
1826400fd1fbSAdriana Kobylak 
1827400fd1fbSAdriana Kobylak                 int fd = -1;
1828400fd1fbSAdriana Kobylak                 fd = dup(unixfd);
1829400fd1fbSAdriana Kobylak                 if (fd == -1)
1830400fd1fbSAdriana Kobylak                 {
1831400fd1fbSAdriana Kobylak                     messages::internalError(asyncResp->res);
1832400fd1fbSAdriana Kobylak                     return;
1833400fd1fbSAdriana Kobylak                 }
1834400fd1fbSAdriana Kobylak 
1835400fd1fbSAdriana Kobylak                 long long int size = lseek(fd, 0, SEEK_END);
1836400fd1fbSAdriana Kobylak                 if (size == -1)
1837400fd1fbSAdriana Kobylak                 {
1838400fd1fbSAdriana Kobylak                     messages::internalError(asyncResp->res);
1839400fd1fbSAdriana Kobylak                     return;
1840400fd1fbSAdriana Kobylak                 }
1841400fd1fbSAdriana Kobylak 
1842400fd1fbSAdriana Kobylak                 // Arbitrary max size of 64kb
1843400fd1fbSAdriana Kobylak                 constexpr int maxFileSize = 65536;
1844400fd1fbSAdriana Kobylak                 if (size > maxFileSize)
1845400fd1fbSAdriana Kobylak                 {
1846400fd1fbSAdriana Kobylak                     BMCWEB_LOG_ERROR
1847400fd1fbSAdriana Kobylak                         << "File size exceeds maximum allowed size of "
1848400fd1fbSAdriana Kobylak                         << maxFileSize;
1849400fd1fbSAdriana Kobylak                     messages::internalError(asyncResp->res);
1850400fd1fbSAdriana Kobylak                     return;
1851400fd1fbSAdriana Kobylak                 }
1852400fd1fbSAdriana Kobylak                 std::vector<char> data(static_cast<size_t>(size));
1853400fd1fbSAdriana Kobylak                 long long int rc = lseek(fd, 0, SEEK_SET);
1854400fd1fbSAdriana Kobylak                 if (rc == -1)
1855400fd1fbSAdriana Kobylak                 {
1856400fd1fbSAdriana Kobylak                     messages::internalError(asyncResp->res);
1857400fd1fbSAdriana Kobylak                     return;
1858400fd1fbSAdriana Kobylak                 }
1859400fd1fbSAdriana Kobylak                 rc = read(fd, data.data(), data.size());
1860400fd1fbSAdriana Kobylak                 if ((rc == -1) || (rc != size))
1861400fd1fbSAdriana Kobylak                 {
1862400fd1fbSAdriana Kobylak                     messages::internalError(asyncResp->res);
1863400fd1fbSAdriana Kobylak                     return;
1864400fd1fbSAdriana Kobylak                 }
1865400fd1fbSAdriana Kobylak                 close(fd);
1866400fd1fbSAdriana Kobylak 
1867400fd1fbSAdriana Kobylak                 std::string_view strData(data.data(), data.size());
1868400fd1fbSAdriana Kobylak                 std::string output = crow::utility::base64encode(strData);
1869400fd1fbSAdriana Kobylak 
1870400fd1fbSAdriana Kobylak                 asyncResp->res.addHeader("Content-Type",
1871400fd1fbSAdriana Kobylak                                          "application/octet-stream");
1872400fd1fbSAdriana Kobylak                 asyncResp->res.addHeader("Content-Transfer-Encoding", "Base64");
1873400fd1fbSAdriana Kobylak                 asyncResp->res.body() = std::move(output);
1874400fd1fbSAdriana Kobylak             },
1875400fd1fbSAdriana Kobylak             "xyz.openbmc_project.Logging",
1876400fd1fbSAdriana Kobylak             "/xyz/openbmc_project/logging/entry/" + entryID,
1877400fd1fbSAdriana Kobylak             "xyz.openbmc_project.Logging.Entry", "GetEntry");
1878400fd1fbSAdriana Kobylak     }
1879400fd1fbSAdriana Kobylak };
1880400fd1fbSAdriana Kobylak 
1881c4bf6374SJason M. Bills class BMCLogServiceCollection : public Node
1882c4bf6374SJason M. Bills {
1883c4bf6374SJason M. Bills   public:
188452cc112dSEd Tanous     BMCLogServiceCollection(App& app) :
18854ed77cd5SEd Tanous         Node(app, "/redfish/v1/Managers/bmc/LogServices/")
18861da66f75SEd Tanous     {
18871da66f75SEd Tanous         entityPrivileges = {
1888e1f26343SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1889e1f26343SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1890e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1891e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1892e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1893e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
18941da66f75SEd Tanous     }
18951da66f75SEd Tanous 
18961da66f75SEd Tanous   private:
18971da66f75SEd Tanous     /**
18981da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
18991da66f75SEd Tanous      */
19008d1b46d7Szhanghch05     void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
19018d1b46d7Szhanghch05                const crow::Request&, const std::vector<std::string>&) override
19021da66f75SEd Tanous     {
19038d1b46d7Szhanghch05 
19041da66f75SEd Tanous         // Collections don't include the static data added by SubRoute because
19051da66f75SEd Tanous         // it has a duplicate entry for members
1906e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
19071da66f75SEd Tanous             "#LogServiceCollection.LogServiceCollection";
1908e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
1909e1f26343SJason M. Bills             "/redfish/v1/Managers/bmc/LogServices";
1910e1f26343SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Open BMC Log Services Collection";
1911e1f26343SJason M. Bills         asyncResp->res.jsonValue["Description"] =
19121da66f75SEd Tanous             "Collection of LogServices for this Manager";
1913c4bf6374SJason M. Bills         nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
1914c4bf6374SJason M. Bills         logServiceArray = nlohmann::json::array();
19155cb1dd27SAsmitha Karunanithi #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
19165cb1dd27SAsmitha Karunanithi         logServiceArray.push_back(
19175cb1dd27SAsmitha Karunanithi             {{"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Dump"}});
19185cb1dd27SAsmitha Karunanithi #endif
1919c4bf6374SJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
1920c4bf6374SJason M. Bills         logServiceArray.push_back(
192108a4e4b5SAnthony Wilson             {{"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Journal"}});
1922c4bf6374SJason M. Bills #endif
1923e1f26343SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] =
1924c4bf6374SJason M. Bills             logServiceArray.size();
19251da66f75SEd Tanous     }
19261da66f75SEd Tanous };
19271da66f75SEd Tanous 
1928c4bf6374SJason M. Bills class BMCJournalLogService : public Node
19291da66f75SEd Tanous {
19301da66f75SEd Tanous   public:
193152cc112dSEd Tanous     BMCJournalLogService(App& app) :
1932c4bf6374SJason M. Bills         Node(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
1933e1f26343SJason M. Bills     {
1934e1f26343SJason M. Bills         entityPrivileges = {
1935e1f26343SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1936e1f26343SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1937e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1938e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1939e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1940e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1941e1f26343SJason M. Bills     }
1942e1f26343SJason M. Bills 
1943e1f26343SJason M. Bills   private:
19448d1b46d7Szhanghch05     void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
19458d1b46d7Szhanghch05                const crow::Request&, const std::vector<std::string>&) override
1946e1f26343SJason M. Bills     {
19478d1b46d7Szhanghch05 
1948e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1949e1f26343SJason M. Bills             "#LogService.v1_1_0.LogService";
19500f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
19510f74e643SEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal";
1952c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Open BMC Journal Log Service";
1953c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] = "BMC Journal Log Service";
1954c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Id"] = "BMC Journal";
1955e1f26343SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
1956cd50aa42SJason M. Bills         asyncResp->res.jsonValue["Entries"] = {
1957cd50aa42SJason M. Bills             {"@odata.id",
1958086be238SEd Tanous              "/redfish/v1/Managers/bmc/LogServices/Journal/Entries"}};
1959e1f26343SJason M. Bills     }
1960e1f26343SJason M. Bills };
1961e1f26343SJason M. Bills 
1962c4bf6374SJason M. Bills static int fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
1963e1f26343SJason M. Bills                                       sd_journal* journal,
1964c4bf6374SJason M. Bills                                       nlohmann::json& bmcJournalLogEntryJson)
1965e1f26343SJason M. Bills {
1966e1f26343SJason M. Bills     // Get the Log Entry contents
1967e1f26343SJason M. Bills     int ret = 0;
1968e1f26343SJason M. Bills 
1969a8fe54f0SJason M. Bills     std::string message;
1970a8fe54f0SJason M. Bills     std::string_view syslogID;
1971a8fe54f0SJason M. Bills     ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
1972a8fe54f0SJason M. Bills     if (ret < 0)
1973a8fe54f0SJason M. Bills     {
1974a8fe54f0SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read SYSLOG_IDENTIFIER field: "
1975a8fe54f0SJason M. Bills                          << strerror(-ret);
1976a8fe54f0SJason M. Bills     }
1977a8fe54f0SJason M. Bills     if (!syslogID.empty())
1978a8fe54f0SJason M. Bills     {
1979a8fe54f0SJason M. Bills         message += std::string(syslogID) + ": ";
1980a8fe54f0SJason M. Bills     }
1981a8fe54f0SJason M. Bills 
198239e77504SEd Tanous     std::string_view msg;
198316428a1aSJason M. Bills     ret = getJournalMetadata(journal, "MESSAGE", msg);
1984e1f26343SJason M. Bills     if (ret < 0)
1985e1f26343SJason M. Bills     {
1986e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
1987e1f26343SJason M. Bills         return 1;
1988e1f26343SJason M. Bills     }
1989a8fe54f0SJason M. Bills     message += std::string(msg);
1990e1f26343SJason M. Bills 
1991e1f26343SJason M. Bills     // Get the severity from the PRIORITY field
1992271584abSEd Tanous     long int severity = 8; // Default to an invalid priority
199316428a1aSJason M. Bills     ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
1994e1f26343SJason M. Bills     if (ret < 0)
1995e1f26343SJason M. Bills     {
1996e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
1997e1f26343SJason M. Bills     }
1998e1f26343SJason M. Bills 
1999e1f26343SJason M. Bills     // Get the Created time from the timestamp
200016428a1aSJason M. Bills     std::string entryTimeStr;
200116428a1aSJason M. Bills     if (!getEntryTimestamp(journal, entryTimeStr))
2002e1f26343SJason M. Bills     {
200316428a1aSJason M. Bills         return 1;
2004e1f26343SJason M. Bills     }
2005e1f26343SJason M. Bills 
2006e1f26343SJason M. Bills     // Fill in the log entry with the gathered data
2007c4bf6374SJason M. Bills     bmcJournalLogEntryJson = {
2008cb92c03bSAndrew Geissler         {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
2009c4bf6374SJason M. Bills         {"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/" +
2010c4bf6374SJason M. Bills                           bmcJournalLogEntryID},
2011e1f26343SJason M. Bills         {"Name", "BMC Journal Entry"},
2012c4bf6374SJason M. Bills         {"Id", bmcJournalLogEntryID},
2013a8fe54f0SJason M. Bills         {"Message", std::move(message)},
2014e1f26343SJason M. Bills         {"EntryType", "Oem"},
2015738c1e61SPatrick Williams         {"Severity", severity <= 2   ? "Critical"
2016738c1e61SPatrick Williams                      : severity <= 4 ? "Warning"
2017738c1e61SPatrick Williams                                      : "OK"},
2018086be238SEd Tanous         {"OemRecordFormat", "BMC Journal Entry"},
2019e1f26343SJason M. Bills         {"Created", std::move(entryTimeStr)}};
2020e1f26343SJason M. Bills     return 0;
2021e1f26343SJason M. Bills }
2022e1f26343SJason M. Bills 
2023c4bf6374SJason M. Bills class BMCJournalLogEntryCollection : public Node
2024e1f26343SJason M. Bills {
2025e1f26343SJason M. Bills   public:
202652cc112dSEd Tanous     BMCJournalLogEntryCollection(App& app) :
2027c4bf6374SJason M. Bills         Node(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
2028e1f26343SJason M. Bills     {
2029e1f26343SJason M. Bills         entityPrivileges = {
2030e1f26343SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
2031e1f26343SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
2032e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2033e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2034e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2035e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
2036e1f26343SJason M. Bills     }
2037e1f26343SJason M. Bills 
2038e1f26343SJason M. Bills   private:
20398d1b46d7Szhanghch05     void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
20408d1b46d7Szhanghch05                const crow::Request& req,
2041cb13a392SEd Tanous                const std::vector<std::string>&) override
2042e1f26343SJason M. Bills     {
20438d1b46d7Szhanghch05 
2044193ad2faSJason M. Bills         static constexpr const long maxEntriesPerPage = 1000;
2045271584abSEd Tanous         uint64_t skip = 0;
2046271584abSEd Tanous         uint64_t top = maxEntriesPerPage; // Show max entries by default
20478d1b46d7Szhanghch05         if (!getSkipParam(asyncResp, req, skip))
2048193ad2faSJason M. Bills         {
2049193ad2faSJason M. Bills             return;
2050193ad2faSJason M. Bills         }
20518d1b46d7Szhanghch05         if (!getTopParam(asyncResp, req, top))
2052193ad2faSJason M. Bills         {
2053193ad2faSJason M. Bills             return;
2054193ad2faSJason M. Bills         }
2055e1f26343SJason M. Bills         // Collections don't include the static data added by SubRoute because
2056e1f26343SJason M. Bills         // it has a duplicate entry for members
2057e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
2058e1f26343SJason M. Bills             "#LogEntryCollection.LogEntryCollection";
20590f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
20600f74e643SEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2061e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
2062c4bf6374SJason M. Bills             "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2063e1f26343SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
2064e1f26343SJason M. Bills         asyncResp->res.jsonValue["Description"] =
2065e1f26343SJason M. Bills             "Collection of BMC Journal Entries";
20660f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
20670f74e643SEd Tanous             "/redfish/v1/Managers/bmc/LogServices/BmcLog/Entries";
2068e1f26343SJason M. Bills         nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2069e1f26343SJason M. Bills         logEntryArray = nlohmann::json::array();
2070e1f26343SJason M. Bills 
2071e1f26343SJason M. Bills         // Go through the journal and use the timestamp to create a unique ID
2072e1f26343SJason M. Bills         // for each entry
2073e1f26343SJason M. Bills         sd_journal* journalTmp = nullptr;
2074e1f26343SJason M. Bills         int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2075e1f26343SJason M. Bills         if (ret < 0)
2076e1f26343SJason M. Bills         {
2077e1f26343SJason M. Bills             BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2078f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2079e1f26343SJason M. Bills             return;
2080e1f26343SJason M. Bills         }
2081e1f26343SJason M. Bills         std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2082e1f26343SJason M. Bills             journalTmp, sd_journal_close);
2083e1f26343SJason M. Bills         journalTmp = nullptr;
2084b01bf299SEd Tanous         uint64_t entryCount = 0;
2085e85d6b16SJason M. Bills         // Reset the unique ID on the first entry
2086e85d6b16SJason M. Bills         bool firstEntry = true;
2087e1f26343SJason M. Bills         SD_JOURNAL_FOREACH(journal.get())
2088e1f26343SJason M. Bills         {
2089193ad2faSJason M. Bills             entryCount++;
2090193ad2faSJason M. Bills             // Handle paging using skip (number of entries to skip from the
2091193ad2faSJason M. Bills             // start) and top (number of entries to display)
2092193ad2faSJason M. Bills             if (entryCount <= skip || entryCount > skip + top)
2093193ad2faSJason M. Bills             {
2094193ad2faSJason M. Bills                 continue;
2095193ad2faSJason M. Bills             }
2096193ad2faSJason M. Bills 
209716428a1aSJason M. Bills             std::string idStr;
2098e85d6b16SJason M. Bills             if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2099e1f26343SJason M. Bills             {
2100e1f26343SJason M. Bills                 continue;
2101e1f26343SJason M. Bills             }
2102e1f26343SJason M. Bills 
2103e85d6b16SJason M. Bills             if (firstEntry)
2104e85d6b16SJason M. Bills             {
2105e85d6b16SJason M. Bills                 firstEntry = false;
2106e85d6b16SJason M. Bills             }
2107e85d6b16SJason M. Bills 
2108e1f26343SJason M. Bills             logEntryArray.push_back({});
2109c4bf6374SJason M. Bills             nlohmann::json& bmcJournalLogEntry = logEntryArray.back();
2110c4bf6374SJason M. Bills             if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2111c4bf6374SJason M. Bills                                            bmcJournalLogEntry) != 0)
2112e1f26343SJason M. Bills             {
2113f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
2114e1f26343SJason M. Bills                 return;
2115e1f26343SJason M. Bills             }
2116e1f26343SJason M. Bills         }
2117193ad2faSJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
2118193ad2faSJason M. Bills         if (skip + top < entryCount)
2119193ad2faSJason M. Bills         {
2120193ad2faSJason M. Bills             asyncResp->res.jsonValue["Members@odata.nextLink"] =
2121c4bf6374SJason M. Bills                 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
2122193ad2faSJason M. Bills                 std::to_string(skip + top);
2123193ad2faSJason M. Bills         }
2124e1f26343SJason M. Bills     }
2125e1f26343SJason M. Bills };
2126e1f26343SJason M. Bills 
2127c4bf6374SJason M. Bills class BMCJournalLogEntry : public Node
2128e1f26343SJason M. Bills {
2129e1f26343SJason M. Bills   public:
213052cc112dSEd Tanous     BMCJournalLogEntry(App& app) :
2131c4bf6374SJason M. Bills         Node(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/",
2132e1f26343SJason M. Bills              std::string())
2133e1f26343SJason M. Bills     {
2134e1f26343SJason M. Bills         entityPrivileges = {
2135e1f26343SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
2136e1f26343SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
2137e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2138e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2139e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2140e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
2141e1f26343SJason M. Bills     }
2142e1f26343SJason M. Bills 
2143e1f26343SJason M. Bills   private:
21448d1b46d7Szhanghch05     void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
21458d1b46d7Szhanghch05                const crow::Request&,
2146e1f26343SJason M. Bills                const std::vector<std::string>& params) override
2147e1f26343SJason M. Bills     {
21488d1b46d7Szhanghch05 
2149e1f26343SJason M. Bills         if (params.size() != 1)
2150e1f26343SJason M. Bills         {
2151f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2152e1f26343SJason M. Bills             return;
2153e1f26343SJason M. Bills         }
215416428a1aSJason M. Bills         const std::string& entryID = params[0];
2155e1f26343SJason M. Bills         // Convert the unique ID back to a timestamp to find the entry
2156e1f26343SJason M. Bills         uint64_t ts = 0;
2157271584abSEd Tanous         uint64_t index = 0;
21588d1b46d7Szhanghch05         if (!getTimestampFromID(asyncResp, entryID, ts, index))
2159e1f26343SJason M. Bills         {
216016428a1aSJason M. Bills             return;
2161e1f26343SJason M. Bills         }
2162e1f26343SJason M. Bills 
2163e1f26343SJason M. Bills         sd_journal* journalTmp = nullptr;
2164e1f26343SJason M. Bills         int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2165e1f26343SJason M. Bills         if (ret < 0)
2166e1f26343SJason M. Bills         {
2167e1f26343SJason M. Bills             BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2168f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2169e1f26343SJason M. Bills             return;
2170e1f26343SJason M. Bills         }
2171e1f26343SJason M. Bills         std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2172e1f26343SJason M. Bills             journalTmp, sd_journal_close);
2173e1f26343SJason M. Bills         journalTmp = nullptr;
2174e1f26343SJason M. Bills         // Go to the timestamp in the log and move to the entry at the index
2175af07e3f5SJason M. Bills         // tracking the unique ID
2176af07e3f5SJason M. Bills         std::string idStr;
2177af07e3f5SJason M. Bills         bool firstEntry = true;
2178e1f26343SJason M. Bills         ret = sd_journal_seek_realtime_usec(journal.get(), ts);
21792056b6d1SManojkiran Eda         if (ret < 0)
21802056b6d1SManojkiran Eda         {
21812056b6d1SManojkiran Eda             BMCWEB_LOG_ERROR << "failed to seek to an entry in journal"
21822056b6d1SManojkiran Eda                              << strerror(-ret);
21832056b6d1SManojkiran Eda             messages::internalError(asyncResp->res);
21842056b6d1SManojkiran Eda             return;
21852056b6d1SManojkiran Eda         }
2186271584abSEd Tanous         for (uint64_t i = 0; i <= index; i++)
2187e1f26343SJason M. Bills         {
2188e1f26343SJason M. Bills             sd_journal_next(journal.get());
2189af07e3f5SJason M. Bills             if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2190af07e3f5SJason M. Bills             {
2191af07e3f5SJason M. Bills                 messages::internalError(asyncResp->res);
2192af07e3f5SJason M. Bills                 return;
2193af07e3f5SJason M. Bills             }
2194af07e3f5SJason M. Bills             if (firstEntry)
2195af07e3f5SJason M. Bills             {
2196af07e3f5SJason M. Bills                 firstEntry = false;
2197af07e3f5SJason M. Bills             }
2198e1f26343SJason M. Bills         }
2199c4bf6374SJason M. Bills         // Confirm that the entry ID matches what was requested
2200af07e3f5SJason M. Bills         if (idStr != entryID)
2201c4bf6374SJason M. Bills         {
2202c4bf6374SJason M. Bills             messages::resourceMissingAtURI(asyncResp->res, entryID);
2203c4bf6374SJason M. Bills             return;
2204c4bf6374SJason M. Bills         }
2205c4bf6374SJason M. Bills 
2206c4bf6374SJason M. Bills         if (fillBMCJournalLogEntryJson(entryID, journal.get(),
2207e1f26343SJason M. Bills                                        asyncResp->res.jsonValue) != 0)
2208e1f26343SJason M. Bills         {
2209f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2210e1f26343SJason M. Bills             return;
2211e1f26343SJason M. Bills         }
2212e1f26343SJason M. Bills     }
2213e1f26343SJason M. Bills };
2214e1f26343SJason M. Bills 
22155cb1dd27SAsmitha Karunanithi class BMCDumpService : public Node
2216c9bb6861Sraviteja-b {
2217c9bb6861Sraviteja-b   public:
221852cc112dSEd Tanous     BMCDumpService(App& app) :
22195cb1dd27SAsmitha Karunanithi         Node(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
2220c9bb6861Sraviteja-b     {
2221c9bb6861Sraviteja-b         entityPrivileges = {
2222c9bb6861Sraviteja-b             {boost::beast::http::verb::get, {{"Login"}}},
2223c9bb6861Sraviteja-b             {boost::beast::http::verb::head, {{"Login"}}},
2224c9bb6861Sraviteja-b             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2225c9bb6861Sraviteja-b             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2226c9bb6861Sraviteja-b             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2227c9bb6861Sraviteja-b             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
2228c9bb6861Sraviteja-b     }
2229c9bb6861Sraviteja-b 
2230c9bb6861Sraviteja-b   private:
22318d1b46d7Szhanghch05     void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
22328d1b46d7Szhanghch05                const crow::Request&, const std::vector<std::string>&) override
2233c9bb6861Sraviteja-b     {
2234c9bb6861Sraviteja-b 
2235c9bb6861Sraviteja-b         asyncResp->res.jsonValue["@odata.id"] =
22365cb1dd27SAsmitha Karunanithi             "/redfish/v1/Managers/bmc/LogServices/Dump";
2237c9bb6861Sraviteja-b         asyncResp->res.jsonValue["@odata.type"] =
2238d337bb72SAsmitha Karunanithi             "#LogService.v1_2_0.LogService";
2239c9bb6861Sraviteja-b         asyncResp->res.jsonValue["Name"] = "Dump LogService";
22405cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["Description"] = "BMC Dump LogService";
22415cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["Id"] = "Dump";
2242c9bb6861Sraviteja-b         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
2243c9bb6861Sraviteja-b         asyncResp->res.jsonValue["Entries"] = {
22445cb1dd27SAsmitha Karunanithi             {"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Dump/Entries"}};
22455cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["Actions"] = {
22465cb1dd27SAsmitha Karunanithi             {"#LogService.ClearLog",
22475cb1dd27SAsmitha Karunanithi              {{"target", "/redfish/v1/Managers/bmc/LogServices/Dump/"
22485cb1dd27SAsmitha Karunanithi                          "Actions/LogService.ClearLog"}}},
2249d337bb72SAsmitha Karunanithi             {"#LogService.CollectDiagnosticData",
2250d337bb72SAsmitha Karunanithi              {{"target", "/redfish/v1/Managers/bmc/LogServices/Dump/"
2251d337bb72SAsmitha Karunanithi                          "Actions/LogService.CollectDiagnosticData"}}}};
2252c9bb6861Sraviteja-b     }
2253c9bb6861Sraviteja-b };
2254c9bb6861Sraviteja-b 
22555cb1dd27SAsmitha Karunanithi class BMCDumpEntryCollection : public Node
2256c9bb6861Sraviteja-b {
2257c9bb6861Sraviteja-b   public:
225852cc112dSEd Tanous     BMCDumpEntryCollection(App& app) :
22595cb1dd27SAsmitha Karunanithi         Node(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
2260c9bb6861Sraviteja-b     {
2261c9bb6861Sraviteja-b         entityPrivileges = {
2262c9bb6861Sraviteja-b             {boost::beast::http::verb::get, {{"Login"}}},
2263c9bb6861Sraviteja-b             {boost::beast::http::verb::head, {{"Login"}}},
2264c9bb6861Sraviteja-b             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2265c9bb6861Sraviteja-b             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2266c9bb6861Sraviteja-b             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2267c9bb6861Sraviteja-b             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
2268c9bb6861Sraviteja-b     }
2269c9bb6861Sraviteja-b 
2270c9bb6861Sraviteja-b   private:
2271c9bb6861Sraviteja-b     /**
2272c9bb6861Sraviteja-b      * Functions triggers appropriate requests on DBus
2273c9bb6861Sraviteja-b      */
22748d1b46d7Szhanghch05     void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
22758d1b46d7Szhanghch05                const crow::Request&, const std::vector<std::string>&) override
2276c9bb6861Sraviteja-b     {
2277c9bb6861Sraviteja-b 
2278c9bb6861Sraviteja-b         asyncResp->res.jsonValue["@odata.type"] =
2279c9bb6861Sraviteja-b             "#LogEntryCollection.LogEntryCollection";
2280c9bb6861Sraviteja-b         asyncResp->res.jsonValue["@odata.id"] =
22815cb1dd27SAsmitha Karunanithi             "/redfish/v1/Managers/bmc/LogServices/Dump/Entries";
22825cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["Name"] = "BMC Dump Entries";
2283c9bb6861Sraviteja-b         asyncResp->res.jsonValue["Description"] =
22845cb1dd27SAsmitha Karunanithi             "Collection of BMC Dump Entries";
2285c9bb6861Sraviteja-b 
22865cb1dd27SAsmitha Karunanithi         getDumpEntryCollection(asyncResp, "BMC");
2287c9bb6861Sraviteja-b     }
2288c9bb6861Sraviteja-b };
2289c9bb6861Sraviteja-b 
22905cb1dd27SAsmitha Karunanithi class BMCDumpEntry : public Node
2291c9bb6861Sraviteja-b {
2292c9bb6861Sraviteja-b   public:
229352cc112dSEd Tanous     BMCDumpEntry(App& app) :
22945cb1dd27SAsmitha Karunanithi         Node(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/",
2295c9bb6861Sraviteja-b              std::string())
2296c9bb6861Sraviteja-b     {
2297c9bb6861Sraviteja-b         entityPrivileges = {
2298c9bb6861Sraviteja-b             {boost::beast::http::verb::get, {{"Login"}}},
2299c9bb6861Sraviteja-b             {boost::beast::http::verb::head, {{"Login"}}},
2300c9bb6861Sraviteja-b             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2301c9bb6861Sraviteja-b             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2302c9bb6861Sraviteja-b             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2303c9bb6861Sraviteja-b             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
2304c9bb6861Sraviteja-b     }
2305c9bb6861Sraviteja-b 
2306c9bb6861Sraviteja-b   private:
23078d1b46d7Szhanghch05     void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
23088d1b46d7Szhanghch05                const crow::Request&,
2309c9bb6861Sraviteja-b                const std::vector<std::string>& params) override
2310c9bb6861Sraviteja-b     {
23118d1b46d7Szhanghch05 
2312c9bb6861Sraviteja-b         if (params.size() != 1)
2313c9bb6861Sraviteja-b         {
2314c9bb6861Sraviteja-b             messages::internalError(asyncResp->res);
2315c9bb6861Sraviteja-b             return;
2316c9bb6861Sraviteja-b         }
23175cb1dd27SAsmitha Karunanithi         getDumpEntryById(asyncResp, params[0], "BMC");
2318c9bb6861Sraviteja-b     }
2319c9bb6861Sraviteja-b 
23208d1b46d7Szhanghch05     void doDelete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
23218d1b46d7Szhanghch05                   const crow::Request&,
2322c9bb6861Sraviteja-b                   const std::vector<std::string>& params) override
2323c9bb6861Sraviteja-b     {
23248d1b46d7Szhanghch05 
2325c9bb6861Sraviteja-b         if (params.size() != 1)
2326c9bb6861Sraviteja-b         {
2327c9bb6861Sraviteja-b             messages::internalError(asyncResp->res);
2328c9bb6861Sraviteja-b             return;
2329c9bb6861Sraviteja-b         }
23309878256fSStanley Chu         deleteDumpEntry(asyncResp, params[0], "bmc");
23315cb1dd27SAsmitha Karunanithi     }
23325cb1dd27SAsmitha Karunanithi };
2333c9bb6861Sraviteja-b 
2334a43be80fSAsmitha Karunanithi class BMCDumpCreate : public Node
2335a43be80fSAsmitha Karunanithi {
2336a43be80fSAsmitha Karunanithi   public:
233752cc112dSEd Tanous     BMCDumpCreate(App& app) :
2338a43be80fSAsmitha Karunanithi         Node(app, "/redfish/v1/Managers/bmc/LogServices/Dump/"
2339d337bb72SAsmitha Karunanithi                   "Actions/"
2340d337bb72SAsmitha Karunanithi                   "LogService.CollectDiagnosticData/")
2341a43be80fSAsmitha Karunanithi     {
2342a43be80fSAsmitha Karunanithi         entityPrivileges = {
2343a43be80fSAsmitha Karunanithi             {boost::beast::http::verb::get, {{"Login"}}},
2344a43be80fSAsmitha Karunanithi             {boost::beast::http::verb::head, {{"Login"}}},
2345a43be80fSAsmitha Karunanithi             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2346a43be80fSAsmitha Karunanithi             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2347a43be80fSAsmitha Karunanithi             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2348a43be80fSAsmitha Karunanithi             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
2349a43be80fSAsmitha Karunanithi     }
2350a43be80fSAsmitha Karunanithi 
2351a43be80fSAsmitha Karunanithi   private:
23528d1b46d7Szhanghch05     void doPost(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
23538d1b46d7Szhanghch05                 const crow::Request& req,
2354cb13a392SEd Tanous                 const std::vector<std::string>&) override
2355a43be80fSAsmitha Karunanithi     {
23568d1b46d7Szhanghch05         createDump(asyncResp, req, "BMC");
2357a43be80fSAsmitha Karunanithi     }
2358a43be80fSAsmitha Karunanithi };
2359a43be80fSAsmitha Karunanithi 
236080319af1SAsmitha Karunanithi class BMCDumpClear : public Node
236180319af1SAsmitha Karunanithi {
236280319af1SAsmitha Karunanithi   public:
236352cc112dSEd Tanous     BMCDumpClear(App& app) :
236480319af1SAsmitha Karunanithi         Node(app, "/redfish/v1/Managers/bmc/LogServices/Dump/"
236580319af1SAsmitha Karunanithi                   "Actions/"
236680319af1SAsmitha Karunanithi                   "LogService.ClearLog/")
236780319af1SAsmitha Karunanithi     {
236880319af1SAsmitha Karunanithi         entityPrivileges = {
236980319af1SAsmitha Karunanithi             {boost::beast::http::verb::get, {{"Login"}}},
237080319af1SAsmitha Karunanithi             {boost::beast::http::verb::head, {{"Login"}}},
237180319af1SAsmitha Karunanithi             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
237280319af1SAsmitha Karunanithi             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
237380319af1SAsmitha Karunanithi             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
237480319af1SAsmitha Karunanithi             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
237580319af1SAsmitha Karunanithi     }
237680319af1SAsmitha Karunanithi 
237780319af1SAsmitha Karunanithi   private:
23788d1b46d7Szhanghch05     void doPost(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
23798d1b46d7Szhanghch05                 const crow::Request&, const std::vector<std::string>&) override
238080319af1SAsmitha Karunanithi     {
23818d1b46d7Szhanghch05         clearDump(asyncResp, "BMC");
238280319af1SAsmitha Karunanithi     }
238380319af1SAsmitha Karunanithi };
238480319af1SAsmitha Karunanithi 
23855cb1dd27SAsmitha Karunanithi class SystemDumpService : public Node
2386c9bb6861Sraviteja-b {
23875cb1dd27SAsmitha Karunanithi   public:
238852cc112dSEd Tanous     SystemDumpService(App& app) :
23895cb1dd27SAsmitha Karunanithi         Node(app, "/redfish/v1/Systems/system/LogServices/Dump/")
23905cb1dd27SAsmitha Karunanithi     {
23915cb1dd27SAsmitha Karunanithi         entityPrivileges = {
23925cb1dd27SAsmitha Karunanithi             {boost::beast::http::verb::get, {{"Login"}}},
23935cb1dd27SAsmitha Karunanithi             {boost::beast::http::verb::head, {{"Login"}}},
23945cb1dd27SAsmitha Karunanithi             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
23955cb1dd27SAsmitha Karunanithi             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
23965cb1dd27SAsmitha Karunanithi             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
23975cb1dd27SAsmitha Karunanithi             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
23985cb1dd27SAsmitha Karunanithi     }
23995cb1dd27SAsmitha Karunanithi 
24005cb1dd27SAsmitha Karunanithi   private:
24018d1b46d7Szhanghch05     void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
24028d1b46d7Szhanghch05                const crow::Request&, const std::vector<std::string>&) override
24035cb1dd27SAsmitha Karunanithi     {
24045cb1dd27SAsmitha Karunanithi 
24055cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["@odata.id"] =
24065cb1dd27SAsmitha Karunanithi             "/redfish/v1/Systems/system/LogServices/Dump";
24075cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["@odata.type"] =
2408d337bb72SAsmitha Karunanithi             "#LogService.v1_2_0.LogService";
24095cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["Name"] = "Dump LogService";
24105cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["Description"] = "System Dump LogService";
24115cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["Id"] = "Dump";
24125cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
24135cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["Entries"] = {
24145cb1dd27SAsmitha Karunanithi             {"@odata.id",
24155cb1dd27SAsmitha Karunanithi              "/redfish/v1/Systems/system/LogServices/Dump/Entries"}};
24165cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["Actions"] = {
24175cb1dd27SAsmitha Karunanithi             {"#LogService.ClearLog",
24185cb1dd27SAsmitha Karunanithi              {{"target", "/redfish/v1/Systems/system/LogServices/Dump/Actions/"
24195cb1dd27SAsmitha Karunanithi                          "LogService.ClearLog"}}},
2420d337bb72SAsmitha Karunanithi             {"#LogService.CollectDiagnosticData",
2421d337bb72SAsmitha Karunanithi              {{"target", "/redfish/v1/Systems/system/LogServices/Dump/Actions/"
2422d337bb72SAsmitha Karunanithi                          "LogService.CollectDiagnosticData"}}}};
24235cb1dd27SAsmitha Karunanithi     }
24245cb1dd27SAsmitha Karunanithi };
24255cb1dd27SAsmitha Karunanithi 
24265cb1dd27SAsmitha Karunanithi class SystemDumpEntryCollection : public Node
24275cb1dd27SAsmitha Karunanithi {
24285cb1dd27SAsmitha Karunanithi   public:
242952cc112dSEd Tanous     SystemDumpEntryCollection(App& app) :
24305cb1dd27SAsmitha Karunanithi         Node(app, "/redfish/v1/Systems/system/LogServices/Dump/Entries/")
24315cb1dd27SAsmitha Karunanithi     {
24325cb1dd27SAsmitha Karunanithi         entityPrivileges = {
24335cb1dd27SAsmitha Karunanithi             {boost::beast::http::verb::get, {{"Login"}}},
24345cb1dd27SAsmitha Karunanithi             {boost::beast::http::verb::head, {{"Login"}}},
24355cb1dd27SAsmitha Karunanithi             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
24365cb1dd27SAsmitha Karunanithi             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
24375cb1dd27SAsmitha Karunanithi             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
24385cb1dd27SAsmitha Karunanithi             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
24395cb1dd27SAsmitha Karunanithi     }
24405cb1dd27SAsmitha Karunanithi 
24415cb1dd27SAsmitha Karunanithi   private:
24425cb1dd27SAsmitha Karunanithi     /**
24435cb1dd27SAsmitha Karunanithi      * Functions triggers appropriate requests on DBus
24445cb1dd27SAsmitha Karunanithi      */
24458d1b46d7Szhanghch05     void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
24468d1b46d7Szhanghch05                const crow::Request&, const std::vector<std::string>&) override
24475cb1dd27SAsmitha Karunanithi     {
24485cb1dd27SAsmitha Karunanithi 
24495cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["@odata.type"] =
24505cb1dd27SAsmitha Karunanithi             "#LogEntryCollection.LogEntryCollection";
24515cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["@odata.id"] =
24525cb1dd27SAsmitha Karunanithi             "/redfish/v1/Systems/system/LogServices/Dump/Entries";
24535cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["Name"] = "System Dump Entries";
24545cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["Description"] =
24555cb1dd27SAsmitha Karunanithi             "Collection of System Dump Entries";
24565cb1dd27SAsmitha Karunanithi 
24575cb1dd27SAsmitha Karunanithi         getDumpEntryCollection(asyncResp, "System");
24585cb1dd27SAsmitha Karunanithi     }
24595cb1dd27SAsmitha Karunanithi };
24605cb1dd27SAsmitha Karunanithi 
24615cb1dd27SAsmitha Karunanithi class SystemDumpEntry : public Node
24625cb1dd27SAsmitha Karunanithi {
24635cb1dd27SAsmitha Karunanithi   public:
246452cc112dSEd Tanous     SystemDumpEntry(App& app) :
24655cb1dd27SAsmitha Karunanithi         Node(app, "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/",
24665cb1dd27SAsmitha Karunanithi              std::string())
24675cb1dd27SAsmitha Karunanithi     {
24685cb1dd27SAsmitha Karunanithi         entityPrivileges = {
24695cb1dd27SAsmitha Karunanithi             {boost::beast::http::verb::get, {{"Login"}}},
24705cb1dd27SAsmitha Karunanithi             {boost::beast::http::verb::head, {{"Login"}}},
24715cb1dd27SAsmitha Karunanithi             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
24725cb1dd27SAsmitha Karunanithi             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
24735cb1dd27SAsmitha Karunanithi             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
24745cb1dd27SAsmitha Karunanithi             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
24755cb1dd27SAsmitha Karunanithi     }
24765cb1dd27SAsmitha Karunanithi 
24775cb1dd27SAsmitha Karunanithi   private:
24788d1b46d7Szhanghch05     void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
24798d1b46d7Szhanghch05                const crow::Request&,
24805cb1dd27SAsmitha Karunanithi                const std::vector<std::string>& params) override
24815cb1dd27SAsmitha Karunanithi     {
24828d1b46d7Szhanghch05 
24835cb1dd27SAsmitha Karunanithi         if (params.size() != 1)
24845cb1dd27SAsmitha Karunanithi         {
2485c9bb6861Sraviteja-b             messages::internalError(asyncResp->res);
2486c9bb6861Sraviteja-b             return;
2487c9bb6861Sraviteja-b         }
24885cb1dd27SAsmitha Karunanithi         getDumpEntryById(asyncResp, params[0], "System");
24895cb1dd27SAsmitha Karunanithi     }
2490c9bb6861Sraviteja-b 
24918d1b46d7Szhanghch05     void doDelete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
24928d1b46d7Szhanghch05                   const crow::Request&,
24935cb1dd27SAsmitha Karunanithi                   const std::vector<std::string>& params) override
2494c9bb6861Sraviteja-b     {
24958d1b46d7Szhanghch05 
24965cb1dd27SAsmitha Karunanithi         if (params.size() != 1)
2497c9bb6861Sraviteja-b         {
24985cb1dd27SAsmitha Karunanithi             messages::internalError(asyncResp->res);
2499c9bb6861Sraviteja-b             return;
2500c9bb6861Sraviteja-b         }
25019878256fSStanley Chu         deleteDumpEntry(asyncResp, params[0], "system");
2502c9bb6861Sraviteja-b     }
2503c9bb6861Sraviteja-b };
2504c9bb6861Sraviteja-b 
2505a43be80fSAsmitha Karunanithi class SystemDumpCreate : public Node
2506a43be80fSAsmitha Karunanithi {
2507a43be80fSAsmitha Karunanithi   public:
250852cc112dSEd Tanous     SystemDumpCreate(App& app) :
2509a43be80fSAsmitha Karunanithi         Node(app, "/redfish/v1/Systems/system/LogServices/Dump/"
2510d337bb72SAsmitha Karunanithi                   "Actions/"
2511d337bb72SAsmitha Karunanithi                   "LogService.CollectDiagnosticData/")
2512a43be80fSAsmitha Karunanithi     {
2513a43be80fSAsmitha Karunanithi         entityPrivileges = {
2514a43be80fSAsmitha Karunanithi             {boost::beast::http::verb::get, {{"Login"}}},
2515a43be80fSAsmitha Karunanithi             {boost::beast::http::verb::head, {{"Login"}}},
2516a43be80fSAsmitha Karunanithi             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2517a43be80fSAsmitha Karunanithi             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2518a43be80fSAsmitha Karunanithi             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2519a43be80fSAsmitha Karunanithi             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
2520a43be80fSAsmitha Karunanithi     }
2521a43be80fSAsmitha Karunanithi 
2522a43be80fSAsmitha Karunanithi   private:
25238d1b46d7Szhanghch05     void doPost(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
25248d1b46d7Szhanghch05                 const crow::Request& req,
2525cb13a392SEd Tanous                 const std::vector<std::string>&) override
2526a43be80fSAsmitha Karunanithi     {
25278d1b46d7Szhanghch05         createDump(asyncResp, req, "System");
2528a43be80fSAsmitha Karunanithi     }
2529a43be80fSAsmitha Karunanithi };
2530a43be80fSAsmitha Karunanithi 
2531013487e5Sraviteja-b class SystemDumpClear : public Node
2532013487e5Sraviteja-b {
2533013487e5Sraviteja-b   public:
253452cc112dSEd Tanous     SystemDumpClear(App& app) :
253580319af1SAsmitha Karunanithi         Node(app, "/redfish/v1/Systems/system/LogServices/Dump/"
2536013487e5Sraviteja-b                   "Actions/"
2537013487e5Sraviteja-b                   "LogService.ClearLog/")
2538013487e5Sraviteja-b     {
2539013487e5Sraviteja-b         entityPrivileges = {
2540013487e5Sraviteja-b             {boost::beast::http::verb::get, {{"Login"}}},
2541013487e5Sraviteja-b             {boost::beast::http::verb::head, {{"Login"}}},
254280319af1SAsmitha Karunanithi             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
254380319af1SAsmitha Karunanithi             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
254480319af1SAsmitha Karunanithi             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2545013487e5Sraviteja-b             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
2546013487e5Sraviteja-b     }
2547013487e5Sraviteja-b 
2548013487e5Sraviteja-b   private:
25498d1b46d7Szhanghch05     void doPost(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
25508d1b46d7Szhanghch05                 const crow::Request&, const std::vector<std::string>&) override
2551013487e5Sraviteja-b     {
25528d1b46d7Szhanghch05         clearDump(asyncResp, "System");
2553013487e5Sraviteja-b     }
2554013487e5Sraviteja-b };
2555013487e5Sraviteja-b 
2556424c4176SJason M. Bills class CrashdumpService : public Node
2557e1f26343SJason M. Bills {
2558e1f26343SJason M. Bills   public:
255952cc112dSEd Tanous     CrashdumpService(App& app) :
2560424c4176SJason M. Bills         Node(app, "/redfish/v1/Systems/system/LogServices/Crashdump/")
25611da66f75SEd Tanous     {
25623946028dSAppaRao Puli         // Note: Deviated from redfish privilege registry for GET & HEAD
25633946028dSAppaRao Puli         // method for security reasons.
25641da66f75SEd Tanous         entityPrivileges = {
25653946028dSAppaRao Puli             {boost::beast::http::verb::get, {{"ConfigureComponents"}}},
25663946028dSAppaRao Puli             {boost::beast::http::verb::head, {{"ConfigureComponents"}}},
2567e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2568e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2569e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2570e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
25711da66f75SEd Tanous     }
25721da66f75SEd Tanous 
25731da66f75SEd Tanous   private:
25741da66f75SEd Tanous     /**
25751da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
25761da66f75SEd Tanous      */
25778d1b46d7Szhanghch05     void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
25788d1b46d7Szhanghch05                const crow::Request&, const std::vector<std::string>&) override
25791da66f75SEd Tanous     {
25808d1b46d7Szhanghch05 
25811da66f75SEd Tanous         // Copy over the static data to include the entries added by SubRoute
25820f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
2583424c4176SJason M. Bills             "/redfish/v1/Systems/system/LogServices/Crashdump";
2584e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
25858e6c099aSJason M. Bills             "#LogService.v1_2_0.LogService";
25864f50ae4bSGunnar Mills         asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
25874f50ae4bSGunnar Mills         asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
25884f50ae4bSGunnar Mills         asyncResp->res.jsonValue["Id"] = "Oem Crashdump";
2589e1f26343SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
2590e1f26343SJason M. Bills         asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
2591cd50aa42SJason M. Bills         asyncResp->res.jsonValue["Entries"] = {
2592cd50aa42SJason M. Bills             {"@odata.id",
2593424c4176SJason M. Bills              "/redfish/v1/Systems/system/LogServices/Crashdump/Entries"}};
2594e1f26343SJason M. Bills         asyncResp->res.jsonValue["Actions"] = {
25955b61b5e8SJason M. Bills             {"#LogService.ClearLog",
25965b61b5e8SJason M. Bills              {{"target", "/redfish/v1/Systems/system/LogServices/Crashdump/"
25975b61b5e8SJason M. Bills                          "Actions/LogService.ClearLog"}}},
25988e6c099aSJason M. Bills             {"#LogService.CollectDiagnosticData",
2599424c4176SJason M. Bills              {{"target", "/redfish/v1/Systems/system/LogServices/Crashdump/"
26008e6c099aSJason M. Bills                          "Actions/LogService.CollectDiagnosticData"}}}};
26011da66f75SEd Tanous     }
26021da66f75SEd Tanous };
26031da66f75SEd Tanous 
26045b61b5e8SJason M. Bills class CrashdumpClear : public Node
26055b61b5e8SJason M. Bills {
26065b61b5e8SJason M. Bills   public:
260752cc112dSEd Tanous     CrashdumpClear(App& app) :
26085b61b5e8SJason M. Bills         Node(app, "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/"
26095b61b5e8SJason M. Bills                   "LogService.ClearLog/")
26105b61b5e8SJason M. Bills     {
26113946028dSAppaRao Puli         // Note: Deviated from redfish privilege registry for GET & HEAD
26123946028dSAppaRao Puli         // method for security reasons.
26135b61b5e8SJason M. Bills         entityPrivileges = {
26143946028dSAppaRao Puli             {boost::beast::http::verb::get, {{"ConfigureComponents"}}},
26153946028dSAppaRao Puli             {boost::beast::http::verb::head, {{"ConfigureComponents"}}},
26165b61b5e8SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
26175b61b5e8SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
26185b61b5e8SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
26195b61b5e8SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
26205b61b5e8SJason M. Bills     }
26215b61b5e8SJason M. Bills 
26225b61b5e8SJason M. Bills   private:
26238d1b46d7Szhanghch05     void doPost(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
26248d1b46d7Szhanghch05                 const crow::Request&, const std::vector<std::string>&) override
26255b61b5e8SJason M. Bills     {
26265b61b5e8SJason M. Bills 
26275b61b5e8SJason M. Bills         crow::connections::systemBus->async_method_call(
26285b61b5e8SJason M. Bills             [asyncResp](const boost::system::error_code ec,
2629cb13a392SEd Tanous                         const std::string&) {
26305b61b5e8SJason M. Bills                 if (ec)
26315b61b5e8SJason M. Bills                 {
26325b61b5e8SJason M. Bills                     messages::internalError(asyncResp->res);
26335b61b5e8SJason M. Bills                     return;
26345b61b5e8SJason M. Bills                 }
26355b61b5e8SJason M. Bills                 messages::success(asyncResp->res);
26365b61b5e8SJason M. Bills             },
26375b61b5e8SJason M. Bills             crashdumpObject, crashdumpPath, deleteAllInterface, "DeleteAll");
26385b61b5e8SJason M. Bills     }
26395b61b5e8SJason M. Bills };
26405b61b5e8SJason M. Bills 
26418d1b46d7Szhanghch05 static void
26428d1b46d7Szhanghch05     logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
26438d1b46d7Szhanghch05                       const std::string& logID, nlohmann::json& logEntryJson)
2644e855dd28SJason M. Bills {
2645043a0536SJohnathan Mantey     auto getStoredLogCallback =
2646043a0536SJohnathan Mantey         [asyncResp, logID, &logEntryJson](
2647e855dd28SJason M. Bills             const boost::system::error_code ec,
2648043a0536SJohnathan Mantey             const std::vector<std::pair<std::string, VariantType>>& params) {
2649e855dd28SJason M. Bills             if (ec)
2650e855dd28SJason M. Bills             {
2651e855dd28SJason M. Bills                 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
26521ddcf01aSJason M. Bills                 if (ec.value() ==
26531ddcf01aSJason M. Bills                     boost::system::linux_error::bad_request_descriptor)
26541ddcf01aSJason M. Bills                 {
2655043a0536SJohnathan Mantey                     messages::resourceNotFound(asyncResp->res, "LogEntry",
2656043a0536SJohnathan Mantey                                                logID);
26571ddcf01aSJason M. Bills                 }
26581ddcf01aSJason M. Bills                 else
26591ddcf01aSJason M. Bills                 {
2660e855dd28SJason M. Bills                     messages::internalError(asyncResp->res);
26611ddcf01aSJason M. Bills                 }
2662e855dd28SJason M. Bills                 return;
2663e855dd28SJason M. Bills             }
2664043a0536SJohnathan Mantey 
2665043a0536SJohnathan Mantey             std::string timestamp{};
2666043a0536SJohnathan Mantey             std::string filename{};
2667043a0536SJohnathan Mantey             std::string logfile{};
26682c70f800SEd Tanous             parseCrashdumpParameters(params, filename, timestamp, logfile);
2669043a0536SJohnathan Mantey 
2670043a0536SJohnathan Mantey             if (filename.empty() || timestamp.empty())
2671e855dd28SJason M. Bills             {
2672043a0536SJohnathan Mantey                 messages::resourceMissingAtURI(asyncResp->res, logID);
2673e855dd28SJason M. Bills                 return;
2674e855dd28SJason M. Bills             }
2675e855dd28SJason M. Bills 
2676043a0536SJohnathan Mantey             std::string crashdumpURI =
2677e855dd28SJason M. Bills                 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
2678043a0536SJohnathan Mantey                 logID + "/" + filename;
26798e6c099aSJason M. Bills             logEntryJson = {{"@odata.type", "#LogEntry.v1_7_0.LogEntry"},
2680043a0536SJohnathan Mantey                             {"@odata.id", "/redfish/v1/Systems/system/"
2681043a0536SJohnathan Mantey                                           "LogServices/Crashdump/Entries/" +
2682e855dd28SJason M. Bills                                               logID},
2683e855dd28SJason M. Bills                             {"Name", "CPU Crashdump"},
2684e855dd28SJason M. Bills                             {"Id", logID},
2685e855dd28SJason M. Bills                             {"EntryType", "Oem"},
26868e6c099aSJason M. Bills                             {"AdditionalDataURI", std::move(crashdumpURI)},
26878e6c099aSJason M. Bills                             {"DiagnosticDataType", "OEM"},
26888e6c099aSJason M. Bills                             {"OEMDiagnosticDataType", "PECICrashdump"},
2689043a0536SJohnathan Mantey                             {"Created", std::move(timestamp)}};
2690e855dd28SJason M. Bills         };
2691e855dd28SJason M. Bills     crow::connections::systemBus->async_method_call(
26925b61b5e8SJason M. Bills         std::move(getStoredLogCallback), crashdumpObject,
26935b61b5e8SJason M. Bills         crashdumpPath + std::string("/") + logID,
2694043a0536SJohnathan Mantey         "org.freedesktop.DBus.Properties", "GetAll", crashdumpInterface);
2695e855dd28SJason M. Bills }
2696e855dd28SJason M. Bills 
2697424c4176SJason M. Bills class CrashdumpEntryCollection : public Node
26981da66f75SEd Tanous {
26991da66f75SEd Tanous   public:
270052cc112dSEd Tanous     CrashdumpEntryCollection(App& app) :
2701424c4176SJason M. Bills         Node(app, "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/")
27021da66f75SEd Tanous     {
27033946028dSAppaRao Puli         // Note: Deviated from redfish privilege registry for GET & HEAD
27043946028dSAppaRao Puli         // method for security reasons.
27051da66f75SEd Tanous         entityPrivileges = {
27063946028dSAppaRao Puli             {boost::beast::http::verb::get, {{"ConfigureComponents"}}},
27073946028dSAppaRao Puli             {boost::beast::http::verb::head, {{"ConfigureComponents"}}},
2708e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2709e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2710e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2711e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
27121da66f75SEd Tanous     }
27131da66f75SEd Tanous 
27141da66f75SEd Tanous   private:
27151da66f75SEd Tanous     /**
27161da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
27171da66f75SEd Tanous      */
27188d1b46d7Szhanghch05     void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
27198d1b46d7Szhanghch05                const crow::Request&, const std::vector<std::string>&) override
27201da66f75SEd Tanous     {
27218d1b46d7Szhanghch05 
27221da66f75SEd Tanous         // Collections don't include the static data added by SubRoute because
27231da66f75SEd Tanous         // it has a duplicate entry for members
2724e1f26343SJason M. Bills         auto getLogEntriesCallback = [asyncResp](
2725e1f26343SJason M. Bills                                          const boost::system::error_code ec,
27261da66f75SEd Tanous                                          const std::vector<std::string>& resp) {
27271da66f75SEd Tanous             if (ec)
27281da66f75SEd Tanous             {
27291da66f75SEd Tanous                 if (ec.value() !=
27301da66f75SEd Tanous                     boost::system::errc::no_such_file_or_directory)
27311da66f75SEd Tanous                 {
27321da66f75SEd Tanous                     BMCWEB_LOG_DEBUG << "failed to get entries ec: "
27331da66f75SEd Tanous                                      << ec.message();
2734f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
27351da66f75SEd Tanous                     return;
27361da66f75SEd Tanous                 }
27371da66f75SEd Tanous             }
2738e1f26343SJason M. Bills             asyncResp->res.jsonValue["@odata.type"] =
27391da66f75SEd Tanous                 "#LogEntryCollection.LogEntryCollection";
27400f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
2741424c4176SJason M. Bills                 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
2742424c4176SJason M. Bills             asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
2743e1f26343SJason M. Bills             asyncResp->res.jsonValue["Description"] =
2744424c4176SJason M. Bills                 "Collection of Crashdump Entries";
2745e1f26343SJason M. Bills             nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2746e1f26343SJason M. Bills             logEntryArray = nlohmann::json::array();
2747e855dd28SJason M. Bills             std::vector<std::string> logIDs;
2748e855dd28SJason M. Bills             // Get the list of log entries and build up an empty array big
2749e855dd28SJason M. Bills             // enough to hold them
27501da66f75SEd Tanous             for (const std::string& objpath : resp)
27511da66f75SEd Tanous             {
2752e855dd28SJason M. Bills                 // Get the log ID
2753f23b7296SEd Tanous                 std::size_t lastPos = objpath.rfind('/');
2754e855dd28SJason M. Bills                 if (lastPos == std::string::npos)
27551da66f75SEd Tanous                 {
2756e855dd28SJason M. Bills                     continue;
27571da66f75SEd Tanous                 }
2758e855dd28SJason M. Bills                 logIDs.emplace_back(objpath.substr(lastPos + 1));
2759e855dd28SJason M. Bills 
2760e855dd28SJason M. Bills                 // Add a space for the log entry to the array
2761e855dd28SJason M. Bills                 logEntryArray.push_back({});
2762e855dd28SJason M. Bills             }
2763e855dd28SJason M. Bills             // Now go through and set up async calls to fill in the entries
2764e855dd28SJason M. Bills             size_t index = 0;
2765e855dd28SJason M. Bills             for (const std::string& logID : logIDs)
2766e855dd28SJason M. Bills             {
2767e855dd28SJason M. Bills                 // Add the log entry to the array
2768e855dd28SJason M. Bills                 logCrashdumpEntry(asyncResp, logID, logEntryArray[index++]);
27691da66f75SEd Tanous             }
2770e1f26343SJason M. Bills             asyncResp->res.jsonValue["Members@odata.count"] =
2771e1f26343SJason M. Bills                 logEntryArray.size();
27721da66f75SEd Tanous         };
27731da66f75SEd Tanous         crow::connections::systemBus->async_method_call(
27741da66f75SEd Tanous             std::move(getLogEntriesCallback),
27751da66f75SEd Tanous             "xyz.openbmc_project.ObjectMapper",
27761da66f75SEd Tanous             "/xyz/openbmc_project/object_mapper",
27771da66f75SEd Tanous             "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "", 0,
27785b61b5e8SJason M. Bills             std::array<const char*, 1>{crashdumpInterface});
27791da66f75SEd Tanous     }
27801da66f75SEd Tanous };
27811da66f75SEd Tanous 
2782424c4176SJason M. Bills class CrashdumpEntry : public Node
27831da66f75SEd Tanous {
27841da66f75SEd Tanous   public:
278552cc112dSEd Tanous     CrashdumpEntry(App& app) :
2786d53dd41fSJason M. Bills         Node(app,
2787424c4176SJason M. Bills              "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/",
27881da66f75SEd Tanous              std::string())
27891da66f75SEd Tanous     {
27903946028dSAppaRao Puli         // Note: Deviated from redfish privilege registry for GET & HEAD
27913946028dSAppaRao Puli         // method for security reasons.
27921da66f75SEd Tanous         entityPrivileges = {
27933946028dSAppaRao Puli             {boost::beast::http::verb::get, {{"ConfigureComponents"}}},
27943946028dSAppaRao Puli             {boost::beast::http::verb::head, {{"ConfigureComponents"}}},
2795e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2796e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2797e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2798e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
27991da66f75SEd Tanous     }
28001da66f75SEd Tanous 
28011da66f75SEd Tanous   private:
28028d1b46d7Szhanghch05     void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
28038d1b46d7Szhanghch05                const crow::Request&,
28041da66f75SEd Tanous                const std::vector<std::string>& params) override
28051da66f75SEd Tanous     {
28068d1b46d7Szhanghch05 
28071da66f75SEd Tanous         if (params.size() != 1)
28081da66f75SEd Tanous         {
2809f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
28101da66f75SEd Tanous             return;
28111da66f75SEd Tanous         }
2812e855dd28SJason M. Bills         const std::string& logID = params[0];
2813e855dd28SJason M. Bills         logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
2814e855dd28SJason M. Bills     }
2815e855dd28SJason M. Bills };
2816e855dd28SJason M. Bills 
2817e855dd28SJason M. Bills class CrashdumpFile : public Node
2818e855dd28SJason M. Bills {
2819e855dd28SJason M. Bills   public:
282052cc112dSEd Tanous     CrashdumpFile(App& app) :
2821e855dd28SJason M. Bills         Node(app,
2822e855dd28SJason M. Bills              "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/"
2823e855dd28SJason M. Bills              "<str>/",
2824e855dd28SJason M. Bills              std::string(), std::string())
2825e855dd28SJason M. Bills     {
28263946028dSAppaRao Puli         // Note: Deviated from redfish privilege registry for GET & HEAD
28273946028dSAppaRao Puli         // method for security reasons.
2828e855dd28SJason M. Bills         entityPrivileges = {
28293946028dSAppaRao Puli             {boost::beast::http::verb::get, {{"ConfigureComponents"}}},
28303946028dSAppaRao Puli             {boost::beast::http::verb::head, {{"ConfigureComponents"}}},
2831e855dd28SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2832e855dd28SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2833e855dd28SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2834e855dd28SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
2835e855dd28SJason M. Bills     }
2836e855dd28SJason M. Bills 
2837e855dd28SJason M. Bills   private:
28388d1b46d7Szhanghch05     void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
28398d1b46d7Szhanghch05                const crow::Request&,
2840e855dd28SJason M. Bills                const std::vector<std::string>& params) override
2841e855dd28SJason M. Bills     {
28428d1b46d7Szhanghch05 
2843e855dd28SJason M. Bills         if (params.size() != 2)
2844e855dd28SJason M. Bills         {
2845e855dd28SJason M. Bills             messages::internalError(asyncResp->res);
2846e855dd28SJason M. Bills             return;
2847e855dd28SJason M. Bills         }
2848e855dd28SJason M. Bills         const std::string& logID = params[0];
2849e855dd28SJason M. Bills         const std::string& fileName = params[1];
2850e855dd28SJason M. Bills 
2851043a0536SJohnathan Mantey         auto getStoredLogCallback =
2852043a0536SJohnathan Mantey             [asyncResp, logID, fileName](
2853abf2add6SEd Tanous                 const boost::system::error_code ec,
2854043a0536SJohnathan Mantey                 const std::vector<std::pair<std::string, VariantType>>& resp) {
28551da66f75SEd Tanous                 if (ec)
28561da66f75SEd Tanous                 {
2857043a0536SJohnathan Mantey                     BMCWEB_LOG_DEBUG << "failed to get log ec: "
2858043a0536SJohnathan Mantey                                      << ec.message();
2859f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
28601da66f75SEd Tanous                     return;
28611da66f75SEd Tanous                 }
2862e855dd28SJason M. Bills 
2863043a0536SJohnathan Mantey                 std::string dbusFilename{};
2864043a0536SJohnathan Mantey                 std::string dbusTimestamp{};
2865043a0536SJohnathan Mantey                 std::string dbusFilepath{};
2866043a0536SJohnathan Mantey 
28672c70f800SEd Tanous                 parseCrashdumpParameters(resp, dbusFilename, dbusTimestamp,
2868043a0536SJohnathan Mantey                                          dbusFilepath);
2869043a0536SJohnathan Mantey 
2870043a0536SJohnathan Mantey                 if (dbusFilename.empty() || dbusTimestamp.empty() ||
2871043a0536SJohnathan Mantey                     dbusFilepath.empty())
28721da66f75SEd Tanous                 {
2873e855dd28SJason M. Bills                     messages::resourceMissingAtURI(asyncResp->res, fileName);
28741da66f75SEd Tanous                     return;
28751da66f75SEd Tanous                 }
2876e855dd28SJason M. Bills 
2877043a0536SJohnathan Mantey                 // Verify the file name parameter is correct
2878043a0536SJohnathan Mantey                 if (fileName != dbusFilename)
2879043a0536SJohnathan Mantey                 {
2880043a0536SJohnathan Mantey                     messages::resourceMissingAtURI(asyncResp->res, fileName);
2881043a0536SJohnathan Mantey                     return;
2882043a0536SJohnathan Mantey                 }
2883043a0536SJohnathan Mantey 
2884043a0536SJohnathan Mantey                 if (!std::filesystem::exists(dbusFilepath))
2885043a0536SJohnathan Mantey                 {
2886043a0536SJohnathan Mantey                     messages::resourceMissingAtURI(asyncResp->res, fileName);
2887043a0536SJohnathan Mantey                     return;
2888043a0536SJohnathan Mantey                 }
2889043a0536SJohnathan Mantey                 std::ifstream ifs(dbusFilepath, std::ios::in |
2890043a0536SJohnathan Mantey                                                     std::ios::binary |
2891043a0536SJohnathan Mantey                                                     std::ios::ate);
2892043a0536SJohnathan Mantey                 std::ifstream::pos_type fileSize = ifs.tellg();
2893043a0536SJohnathan Mantey                 if (fileSize < 0)
2894043a0536SJohnathan Mantey                 {
2895043a0536SJohnathan Mantey                     messages::generalError(asyncResp->res);
2896043a0536SJohnathan Mantey                     return;
2897043a0536SJohnathan Mantey                 }
2898043a0536SJohnathan Mantey                 ifs.seekg(0, std::ios::beg);
2899043a0536SJohnathan Mantey 
2900043a0536SJohnathan Mantey                 auto crashData = std::make_unique<char[]>(
2901043a0536SJohnathan Mantey                     static_cast<unsigned int>(fileSize));
2902043a0536SJohnathan Mantey 
2903043a0536SJohnathan Mantey                 ifs.read(crashData.get(), static_cast<int>(fileSize));
2904043a0536SJohnathan Mantey 
2905043a0536SJohnathan Mantey                 // The cast to std::string is intentional in order to use the
2906043a0536SJohnathan Mantey                 // assign() that applies move mechanics
2907043a0536SJohnathan Mantey                 asyncResp->res.body().assign(
2908043a0536SJohnathan Mantey                     static_cast<std::string>(crashData.get()));
2909043a0536SJohnathan Mantey 
2910043a0536SJohnathan Mantey                 // Configure this to be a file download when accessed from
2911043a0536SJohnathan Mantey                 // a browser
2912e855dd28SJason M. Bills                 asyncResp->res.addHeader("Content-Disposition", "attachment");
29131da66f75SEd Tanous             };
29141da66f75SEd Tanous         crow::connections::systemBus->async_method_call(
29155b61b5e8SJason M. Bills             std::move(getStoredLogCallback), crashdumpObject,
29165b61b5e8SJason M. Bills             crashdumpPath + std::string("/") + logID,
2917043a0536SJohnathan Mantey             "org.freedesktop.DBus.Properties", "GetAll", crashdumpInterface);
29181da66f75SEd Tanous     }
29191da66f75SEd Tanous };
29201da66f75SEd Tanous 
29218e6c099aSJason M. Bills class CrashdumpCollect : public Node
29221da66f75SEd Tanous {
29231da66f75SEd Tanous   public:
29248e6c099aSJason M. Bills     CrashdumpCollect(App& app) :
29258e6c099aSJason M. Bills         Node(app, "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/"
29268e6c099aSJason M. Bills                   "LogService.CollectDiagnosticData/")
29271da66f75SEd Tanous     {
29283946028dSAppaRao Puli         // Note: Deviated from redfish privilege registry for GET & HEAD
29293946028dSAppaRao Puli         // method for security reasons.
29301da66f75SEd Tanous         entityPrivileges = {
29313946028dSAppaRao Puli             {boost::beast::http::verb::get, {{"ConfigureComponents"}}},
29323946028dSAppaRao Puli             {boost::beast::http::verb::head, {{"ConfigureComponents"}}},
29333946028dSAppaRao Puli             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
29343946028dSAppaRao Puli             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
29353946028dSAppaRao Puli             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
29363946028dSAppaRao Puli             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
29371da66f75SEd Tanous     }
29381da66f75SEd Tanous 
29391da66f75SEd Tanous   private:
29408d1b46d7Szhanghch05     void doPost(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
29418d1b46d7Szhanghch05                 const crow::Request& req,
2942cb13a392SEd Tanous                 const std::vector<std::string>&) override
29431da66f75SEd Tanous     {
29441da66f75SEd Tanous 
29458e6c099aSJason M. Bills         std::string diagnosticDataType;
29468e6c099aSJason M. Bills         std::string oemDiagnosticDataType;
29478e6c099aSJason M. Bills         if (!redfish::json_util::readJson(
29488e6c099aSJason M. Bills                 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
29498e6c099aSJason M. Bills                 "OEMDiagnosticDataType", oemDiagnosticDataType))
29508e6c099aSJason M. Bills         {
29518e6c099aSJason M. Bills             return;
29528e6c099aSJason M. Bills         }
29538e6c099aSJason M. Bills 
29548e6c099aSJason M. Bills         if (diagnosticDataType != "OEM")
29558e6c099aSJason M. Bills         {
29568e6c099aSJason M. Bills             BMCWEB_LOG_ERROR
29578e6c099aSJason M. Bills                 << "Only OEM DiagnosticDataType supported for Crashdump";
29588e6c099aSJason M. Bills             messages::actionParameterValueFormatError(
29598e6c099aSJason M. Bills                 asyncResp->res, diagnosticDataType, "DiagnosticDataType",
29608e6c099aSJason M. Bills                 "CollectDiagnosticData");
29618e6c099aSJason M. Bills             return;
29628e6c099aSJason M. Bills         }
29638e6c099aSJason M. Bills 
29648e6c099aSJason M. Bills         auto collectCrashdumpCallback = [asyncResp, req](
29658e6c099aSJason M. Bills                                             const boost::system::error_code ec,
2966cb13a392SEd Tanous                                             const std::string&) {
29671da66f75SEd Tanous             if (ec)
29681da66f75SEd Tanous             {
296946229577SJames Feist                 if (ec.value() == boost::system::errc::operation_not_supported)
29701da66f75SEd Tanous                 {
2971f12894f8SJason M. Bills                     messages::resourceInStandby(asyncResp->res);
29721da66f75SEd Tanous                 }
29734363d3b2SJason M. Bills                 else if (ec.value() ==
29744363d3b2SJason M. Bills                          boost::system::errc::device_or_resource_busy)
29754363d3b2SJason M. Bills                 {
29764363d3b2SJason M. Bills                     messages::serviceTemporarilyUnavailable(asyncResp->res,
29774363d3b2SJason M. Bills                                                             "60");
29784363d3b2SJason M. Bills                 }
29791da66f75SEd Tanous                 else
29801da66f75SEd Tanous                 {
2981f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
29821da66f75SEd Tanous                 }
29831da66f75SEd Tanous                 return;
29841da66f75SEd Tanous             }
298546229577SJames Feist             std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
298666afe4faSJames Feist                 [](boost::system::error_code err, sdbusplus::message::message&,
298766afe4faSJames Feist                    const std::shared_ptr<task::TaskData>& taskData) {
298866afe4faSJames Feist                     if (!err)
298966afe4faSJames Feist                     {
2990e5d5006bSJames Feist                         taskData->messages.emplace_back(
2991e5d5006bSJames Feist                             messages::taskCompletedOK(
2992e5d5006bSJames Feist                                 std::to_string(taskData->index)));
2993831d6b09SJames Feist                         taskData->state = "Completed";
299466afe4faSJames Feist                     }
299532898ceaSJames Feist                     return task::completed;
299666afe4faSJames Feist                 },
299746229577SJames Feist                 "type='signal',interface='org.freedesktop.DBus.Properties',"
299846229577SJames Feist                 "member='PropertiesChanged',arg0namespace='com.intel."
299946229577SJames Feist                 "crashdump'");
300046229577SJames Feist             task->startTimer(std::chrono::minutes(5));
300146229577SJames Feist             task->populateResp(asyncResp->res);
3002fe306728SJames Feist             task->payload.emplace(req);
30031da66f75SEd Tanous         };
30048e6c099aSJason M. Bills 
30058e6c099aSJason M. Bills         if (oemDiagnosticDataType == "OnDemand")
30068e6c099aSJason M. Bills         {
30071da66f75SEd Tanous             crow::connections::systemBus->async_method_call(
30088e6c099aSJason M. Bills                 std::move(collectCrashdumpCallback), crashdumpObject,
30098e6c099aSJason M. Bills                 crashdumpPath, crashdumpOnDemandInterface,
30108e6c099aSJason M. Bills                 "GenerateOnDemandLog");
30111da66f75SEd Tanous         }
30128e6c099aSJason M. Bills         else if (oemDiagnosticDataType == "Telemetry")
30136eda7685SKenny L. Ku         {
30148e6c099aSJason M. Bills             crow::connections::systemBus->async_method_call(
30158e6c099aSJason M. Bills                 std::move(collectCrashdumpCallback), crashdumpObject,
30168e6c099aSJason M. Bills                 crashdumpPath, crashdumpTelemetryInterface,
30178e6c099aSJason M. Bills                 "GenerateTelemetryLog");
30186eda7685SKenny L. Ku         }
30196eda7685SKenny L. Ku         else
30206eda7685SKenny L. Ku         {
30218e6c099aSJason M. Bills             BMCWEB_LOG_ERROR << "Unsupported OEMDiagnosticDataType: "
30228e6c099aSJason M. Bills                              << oemDiagnosticDataType;
30238e6c099aSJason M. Bills             messages::actionParameterValueFormatError(
30248e6c099aSJason M. Bills                 asyncResp->res, oemDiagnosticDataType, "OEMDiagnosticDataType",
30258e6c099aSJason M. Bills                 "CollectDiagnosticData");
30266eda7685SKenny L. Ku             return;
30276eda7685SKenny L. Ku         }
30286eda7685SKenny L. Ku     }
30296eda7685SKenny L. Ku };
30306eda7685SKenny L. Ku 
3031cb92c03bSAndrew Geissler /**
3032cb92c03bSAndrew Geissler  * DBusLogServiceActionsClear class supports POST method for ClearLog action.
3033cb92c03bSAndrew Geissler  */
3034cb92c03bSAndrew Geissler class DBusLogServiceActionsClear : public Node
3035cb92c03bSAndrew Geissler {
3036cb92c03bSAndrew Geissler   public:
303752cc112dSEd Tanous     DBusLogServiceActionsClear(App& app) :
3038cb92c03bSAndrew Geissler         Node(app, "/redfish/v1/Systems/system/LogServices/EventLog/Actions/"
30397af91514SGunnar Mills                   "LogService.ClearLog/")
3040cb92c03bSAndrew Geissler     {
3041cb92c03bSAndrew Geissler         entityPrivileges = {
3042cb92c03bSAndrew Geissler             {boost::beast::http::verb::get, {{"Login"}}},
3043cb92c03bSAndrew Geissler             {boost::beast::http::verb::head, {{"Login"}}},
3044cb92c03bSAndrew Geissler             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
3045cb92c03bSAndrew Geissler             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
3046cb92c03bSAndrew Geissler             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
3047cb92c03bSAndrew Geissler             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
3048cb92c03bSAndrew Geissler     }
3049cb92c03bSAndrew Geissler 
3050cb92c03bSAndrew Geissler   private:
3051cb92c03bSAndrew Geissler     /**
3052cb92c03bSAndrew Geissler      * Function handles POST method request.
3053cb92c03bSAndrew Geissler      * The Clear Log actions does not require any parameter.The action deletes
3054cb92c03bSAndrew Geissler      * all entries found in the Entries collection for this Log Service.
3055cb92c03bSAndrew Geissler      */
30568d1b46d7Szhanghch05     void doPost(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
30578d1b46d7Szhanghch05                 const crow::Request&, const std::vector<std::string>&) override
3058cb92c03bSAndrew Geissler     {
3059cb92c03bSAndrew Geissler         BMCWEB_LOG_DEBUG << "Do delete all entries.";
3060cb92c03bSAndrew Geissler 
3061cb92c03bSAndrew Geissler         // Process response from Logging service.
30622c70f800SEd Tanous         auto respHandler = [asyncResp](const boost::system::error_code ec) {
3063cb92c03bSAndrew Geissler             BMCWEB_LOG_DEBUG << "doClearLog resp_handler callback: Done";
3064cb92c03bSAndrew Geissler             if (ec)
3065cb92c03bSAndrew Geissler             {
3066cb92c03bSAndrew Geissler                 // TODO Handle for specific error code
3067cb92c03bSAndrew Geissler                 BMCWEB_LOG_ERROR << "doClearLog resp_handler got error " << ec;
3068cb92c03bSAndrew Geissler                 asyncResp->res.result(
3069cb92c03bSAndrew Geissler                     boost::beast::http::status::internal_server_error);
3070cb92c03bSAndrew Geissler                 return;
3071cb92c03bSAndrew Geissler             }
3072cb92c03bSAndrew Geissler 
3073cb92c03bSAndrew Geissler             asyncResp->res.result(boost::beast::http::status::no_content);
3074cb92c03bSAndrew Geissler         };
3075cb92c03bSAndrew Geissler 
3076cb92c03bSAndrew Geissler         // Make call to Logging service to request Clear Log
3077cb92c03bSAndrew Geissler         crow::connections::systemBus->async_method_call(
30782c70f800SEd Tanous             respHandler, "xyz.openbmc_project.Logging",
3079cb92c03bSAndrew Geissler             "/xyz/openbmc_project/logging",
3080cb92c03bSAndrew Geissler             "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
3081cb92c03bSAndrew Geissler     }
3082cb92c03bSAndrew Geissler };
3083a3316fc6SZhikuiRen 
3084a3316fc6SZhikuiRen /****************************************************
3085a3316fc6SZhikuiRen  * Redfish PostCode interfaces
3086a3316fc6SZhikuiRen  * using DBUS interface: getPostCodesTS
3087a3316fc6SZhikuiRen  ******************************************************/
3088a3316fc6SZhikuiRen class PostCodesLogService : public Node
3089a3316fc6SZhikuiRen {
3090a3316fc6SZhikuiRen   public:
309152cc112dSEd Tanous     PostCodesLogService(App& app) :
3092a3316fc6SZhikuiRen         Node(app, "/redfish/v1/Systems/system/LogServices/PostCodes/")
3093a3316fc6SZhikuiRen     {
3094a3316fc6SZhikuiRen         entityPrivileges = {
3095a3316fc6SZhikuiRen             {boost::beast::http::verb::get, {{"Login"}}},
3096a3316fc6SZhikuiRen             {boost::beast::http::verb::head, {{"Login"}}},
3097a3316fc6SZhikuiRen             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
3098a3316fc6SZhikuiRen             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
3099a3316fc6SZhikuiRen             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
3100a3316fc6SZhikuiRen             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
3101a3316fc6SZhikuiRen     }
3102a3316fc6SZhikuiRen 
3103a3316fc6SZhikuiRen   private:
31048d1b46d7Szhanghch05     void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
31058d1b46d7Szhanghch05                const crow::Request&, const std::vector<std::string>&) override
3106a3316fc6SZhikuiRen     {
3107a3316fc6SZhikuiRen 
3108a3316fc6SZhikuiRen         asyncResp->res.jsonValue = {
3109a3316fc6SZhikuiRen             {"@odata.id", "/redfish/v1/Systems/system/LogServices/PostCodes"},
3110a3316fc6SZhikuiRen             {"@odata.type", "#LogService.v1_1_0.LogService"},
3111a3316fc6SZhikuiRen             {"Name", "POST Code Log Service"},
3112a3316fc6SZhikuiRen             {"Description", "POST Code Log Service"},
3113a3316fc6SZhikuiRen             {"Id", "BIOS POST Code Log"},
3114a3316fc6SZhikuiRen             {"OverWritePolicy", "WrapsWhenFull"},
3115a3316fc6SZhikuiRen             {"Entries",
3116a3316fc6SZhikuiRen              {{"@odata.id",
3117a3316fc6SZhikuiRen                "/redfish/v1/Systems/system/LogServices/PostCodes/Entries"}}}};
3118a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
3119a3316fc6SZhikuiRen             {"target", "/redfish/v1/Systems/system/LogServices/PostCodes/"
3120a3316fc6SZhikuiRen                        "Actions/LogService.ClearLog"}};
3121a3316fc6SZhikuiRen     }
3122a3316fc6SZhikuiRen };
3123a3316fc6SZhikuiRen 
3124a3316fc6SZhikuiRen class PostCodesClear : public Node
3125a3316fc6SZhikuiRen {
3126a3316fc6SZhikuiRen   public:
312752cc112dSEd Tanous     PostCodesClear(App& app) :
3128a3316fc6SZhikuiRen         Node(app, "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/"
3129a3316fc6SZhikuiRen                   "LogService.ClearLog/")
3130a3316fc6SZhikuiRen     {
3131a3316fc6SZhikuiRen         entityPrivileges = {
3132a3316fc6SZhikuiRen             {boost::beast::http::verb::get, {{"Login"}}},
3133a3316fc6SZhikuiRen             {boost::beast::http::verb::head, {{"Login"}}},
31343946028dSAppaRao Puli             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
31353946028dSAppaRao Puli             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
31363946028dSAppaRao Puli             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
31373946028dSAppaRao Puli             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
3138a3316fc6SZhikuiRen     }
3139a3316fc6SZhikuiRen 
3140a3316fc6SZhikuiRen   private:
31418d1b46d7Szhanghch05     void doPost(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
31428d1b46d7Szhanghch05                 const crow::Request&, const std::vector<std::string>&) override
3143a3316fc6SZhikuiRen     {
3144a3316fc6SZhikuiRen         BMCWEB_LOG_DEBUG << "Do delete all postcodes entries.";
3145a3316fc6SZhikuiRen 
3146a3316fc6SZhikuiRen         // Make call to post-code service to request clear all
3147a3316fc6SZhikuiRen         crow::connections::systemBus->async_method_call(
3148a3316fc6SZhikuiRen             [asyncResp](const boost::system::error_code ec) {
3149a3316fc6SZhikuiRen                 if (ec)
3150a3316fc6SZhikuiRen                 {
3151a3316fc6SZhikuiRen                     // TODO Handle for specific error code
3152a3316fc6SZhikuiRen                     BMCWEB_LOG_ERROR
3153a3316fc6SZhikuiRen                         << "doClearPostCodes resp_handler got error " << ec;
3154a3316fc6SZhikuiRen                     asyncResp->res.result(
3155a3316fc6SZhikuiRen                         boost::beast::http::status::internal_server_error);
3156a3316fc6SZhikuiRen                     messages::internalError(asyncResp->res);
3157a3316fc6SZhikuiRen                     return;
3158a3316fc6SZhikuiRen                 }
3159a3316fc6SZhikuiRen             },
316015124765SJonathan Doman             "xyz.openbmc_project.State.Boot.PostCode0",
316115124765SJonathan Doman             "/xyz/openbmc_project/State/Boot/PostCode0",
3162a3316fc6SZhikuiRen             "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
3163a3316fc6SZhikuiRen     }
3164a3316fc6SZhikuiRen };
3165a3316fc6SZhikuiRen 
3166a3316fc6SZhikuiRen static void fillPostCodeEntry(
31678d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& aResp,
31686c9a279eSManojkiran Eda     const boost::container::flat_map<
31696c9a279eSManojkiran Eda         uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
3170a3316fc6SZhikuiRen     const uint16_t bootIndex, const uint64_t codeIndex = 0,
3171a3316fc6SZhikuiRen     const uint64_t skip = 0, const uint64_t top = 0)
3172a3316fc6SZhikuiRen {
3173a3316fc6SZhikuiRen     // Get the Message from the MessageRegistry
3174a3316fc6SZhikuiRen     const message_registries::Message* message =
31754a0bf539SManojkiran Eda         message_registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
3176a3316fc6SZhikuiRen 
3177a3316fc6SZhikuiRen     uint64_t currentCodeIndex = 0;
3178a3316fc6SZhikuiRen     nlohmann::json& logEntryArray = aResp->res.jsonValue["Members"];
3179a3316fc6SZhikuiRen 
3180a3316fc6SZhikuiRen     uint64_t firstCodeTimeUs = 0;
31816c9a279eSManojkiran Eda     for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
31826c9a279eSManojkiran Eda              code : postcode)
3183a3316fc6SZhikuiRen     {
3184a3316fc6SZhikuiRen         currentCodeIndex++;
3185a3316fc6SZhikuiRen         std::string postcodeEntryID =
3186a3316fc6SZhikuiRen             "B" + std::to_string(bootIndex) + "-" +
3187a3316fc6SZhikuiRen             std::to_string(currentCodeIndex); // 1 based index in EntryID string
3188a3316fc6SZhikuiRen 
3189a3316fc6SZhikuiRen         uint64_t usecSinceEpoch = code.first;
3190a3316fc6SZhikuiRen         uint64_t usTimeOffset = 0;
3191a3316fc6SZhikuiRen 
3192a3316fc6SZhikuiRen         if (1 == currentCodeIndex)
3193a3316fc6SZhikuiRen         { // already incremented
3194a3316fc6SZhikuiRen             firstCodeTimeUs = code.first;
3195a3316fc6SZhikuiRen         }
3196a3316fc6SZhikuiRen         else
3197a3316fc6SZhikuiRen         {
3198a3316fc6SZhikuiRen             usTimeOffset = code.first - firstCodeTimeUs;
3199a3316fc6SZhikuiRen         }
3200a3316fc6SZhikuiRen 
3201a3316fc6SZhikuiRen         // skip if no specific codeIndex is specified and currentCodeIndex does
3202a3316fc6SZhikuiRen         // not fall between top and skip
3203a3316fc6SZhikuiRen         if ((codeIndex == 0) &&
3204a3316fc6SZhikuiRen             (currentCodeIndex <= skip || currentCodeIndex > top))
3205a3316fc6SZhikuiRen         {
3206a3316fc6SZhikuiRen             continue;
3207a3316fc6SZhikuiRen         }
3208a3316fc6SZhikuiRen 
32094e0453b1SGunnar Mills         // skip if a specific codeIndex is specified and does not match the
3210a3316fc6SZhikuiRen         // currentIndex
3211a3316fc6SZhikuiRen         if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
3212a3316fc6SZhikuiRen         {
3213a3316fc6SZhikuiRen             // This is done for simplicity. 1st entry is needed to calculate
3214a3316fc6SZhikuiRen             // time offset. To improve efficiency, one can get to the entry
3215a3316fc6SZhikuiRen             // directly (possibly with flatmap's nth method)
3216a3316fc6SZhikuiRen             continue;
3217a3316fc6SZhikuiRen         }
3218a3316fc6SZhikuiRen 
3219a3316fc6SZhikuiRen         // currentCodeIndex is within top and skip or equal to specified code
3220a3316fc6SZhikuiRen         // index
3221a3316fc6SZhikuiRen 
3222a3316fc6SZhikuiRen         // Get the Created time from the timestamp
3223a3316fc6SZhikuiRen         std::string entryTimeStr;
32249c620e21SAsmitha Karunanithi         entryTimeStr = crow::utility::getDateTime(
32259c620e21SAsmitha Karunanithi             static_cast<std::time_t>(usecSinceEpoch / 1000 / 1000));
3226a3316fc6SZhikuiRen 
3227a3316fc6SZhikuiRen         // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
3228a3316fc6SZhikuiRen         std::ostringstream hexCode;
3229a3316fc6SZhikuiRen         hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
32306c9a279eSManojkiran Eda                 << std::get<0>(code.second);
3231a3316fc6SZhikuiRen         std::ostringstream timeOffsetStr;
3232a3316fc6SZhikuiRen         // Set Fixed -Point Notation
3233a3316fc6SZhikuiRen         timeOffsetStr << std::fixed;
3234a3316fc6SZhikuiRen         // Set precision to 4 digits
3235a3316fc6SZhikuiRen         timeOffsetStr << std::setprecision(4);
3236a3316fc6SZhikuiRen         // Add double to stream
3237a3316fc6SZhikuiRen         timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
3238a3316fc6SZhikuiRen         std::vector<std::string> messageArgs = {
3239a3316fc6SZhikuiRen             std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()};
3240a3316fc6SZhikuiRen 
3241a3316fc6SZhikuiRen         // Get MessageArgs template from message registry
3242a3316fc6SZhikuiRen         std::string msg;
3243a3316fc6SZhikuiRen         if (message != nullptr)
3244a3316fc6SZhikuiRen         {
3245a3316fc6SZhikuiRen             msg = message->message;
3246a3316fc6SZhikuiRen 
3247a3316fc6SZhikuiRen             // fill in this post code value
3248a3316fc6SZhikuiRen             int i = 0;
3249a3316fc6SZhikuiRen             for (const std::string& messageArg : messageArgs)
3250a3316fc6SZhikuiRen             {
3251a3316fc6SZhikuiRen                 std::string argStr = "%" + std::to_string(++i);
3252a3316fc6SZhikuiRen                 size_t argPos = msg.find(argStr);
3253a3316fc6SZhikuiRen                 if (argPos != std::string::npos)
3254a3316fc6SZhikuiRen                 {
3255a3316fc6SZhikuiRen                     msg.replace(argPos, argStr.length(), messageArg);
3256a3316fc6SZhikuiRen                 }
3257a3316fc6SZhikuiRen             }
3258a3316fc6SZhikuiRen         }
3259a3316fc6SZhikuiRen 
3260d4342a92STim Lee         // Get Severity template from message registry
3261d4342a92STim Lee         std::string severity;
3262d4342a92STim Lee         if (message != nullptr)
3263d4342a92STim Lee         {
3264d4342a92STim Lee             severity = message->severity;
3265d4342a92STim Lee         }
3266d4342a92STim Lee 
3267a3316fc6SZhikuiRen         // add to AsyncResp
3268a3316fc6SZhikuiRen         logEntryArray.push_back({});
3269a3316fc6SZhikuiRen         nlohmann::json& bmcLogEntry = logEntryArray.back();
3270743e9a1fSGunnar Mills         bmcLogEntry = {{"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
3271a3316fc6SZhikuiRen                        {"@odata.id", "/redfish/v1/Systems/system/LogServices/"
3272a3316fc6SZhikuiRen                                      "PostCodes/Entries/" +
3273a3316fc6SZhikuiRen                                          postcodeEntryID},
3274a3316fc6SZhikuiRen                        {"Name", "POST Code Log Entry"},
3275a3316fc6SZhikuiRen                        {"Id", postcodeEntryID},
3276a3316fc6SZhikuiRen                        {"Message", std::move(msg)},
32774a0bf539SManojkiran Eda                        {"MessageId", "OpenBMC.0.2.BIOSPOSTCode"},
3278a3316fc6SZhikuiRen                        {"MessageArgs", std::move(messageArgs)},
3279a3316fc6SZhikuiRen                        {"EntryType", "Event"},
3280a3316fc6SZhikuiRen                        {"Severity", std::move(severity)},
32819c620e21SAsmitha Karunanithi                        {"Created", entryTimeStr}};
3282a3316fc6SZhikuiRen     }
3283a3316fc6SZhikuiRen }
3284a3316fc6SZhikuiRen 
32858d1b46d7Szhanghch05 static void getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3286a3316fc6SZhikuiRen                                 const uint16_t bootIndex,
3287a3316fc6SZhikuiRen                                 const uint64_t codeIndex)
3288a3316fc6SZhikuiRen {
3289a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
32906c9a279eSManojkiran Eda         [aResp, bootIndex,
32916c9a279eSManojkiran Eda          codeIndex](const boost::system::error_code ec,
32926c9a279eSManojkiran Eda                     const boost::container::flat_map<
32936c9a279eSManojkiran Eda                         uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
32946c9a279eSManojkiran Eda                         postcode) {
3295a3316fc6SZhikuiRen             if (ec)
3296a3316fc6SZhikuiRen             {
3297a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3298a3316fc6SZhikuiRen                 messages::internalError(aResp->res);
3299a3316fc6SZhikuiRen                 return;
3300a3316fc6SZhikuiRen             }
3301a3316fc6SZhikuiRen 
3302a3316fc6SZhikuiRen             // skip the empty postcode boots
3303a3316fc6SZhikuiRen             if (postcode.empty())
3304a3316fc6SZhikuiRen             {
3305a3316fc6SZhikuiRen                 return;
3306a3316fc6SZhikuiRen             }
3307a3316fc6SZhikuiRen 
3308a3316fc6SZhikuiRen             fillPostCodeEntry(aResp, postcode, bootIndex, codeIndex);
3309a3316fc6SZhikuiRen 
3310a3316fc6SZhikuiRen             aResp->res.jsonValue["Members@odata.count"] =
3311a3316fc6SZhikuiRen                 aResp->res.jsonValue["Members"].size();
3312a3316fc6SZhikuiRen         },
331315124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
331415124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
3315a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3316a3316fc6SZhikuiRen         bootIndex);
3317a3316fc6SZhikuiRen }
3318a3316fc6SZhikuiRen 
33198d1b46d7Szhanghch05 static void getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3320a3316fc6SZhikuiRen                                const uint16_t bootIndex,
3321a3316fc6SZhikuiRen                                const uint16_t bootCount,
3322a3316fc6SZhikuiRen                                const uint64_t entryCount, const uint64_t skip,
3323a3316fc6SZhikuiRen                                const uint64_t top)
3324a3316fc6SZhikuiRen {
3325a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
3326a3316fc6SZhikuiRen         [aResp, bootIndex, bootCount, entryCount, skip,
3327a3316fc6SZhikuiRen          top](const boost::system::error_code ec,
33286c9a279eSManojkiran Eda               const boost::container::flat_map<
33296c9a279eSManojkiran Eda                   uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
33306c9a279eSManojkiran Eda                   postcode) {
3331a3316fc6SZhikuiRen             if (ec)
3332a3316fc6SZhikuiRen             {
3333a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3334a3316fc6SZhikuiRen                 messages::internalError(aResp->res);
3335a3316fc6SZhikuiRen                 return;
3336a3316fc6SZhikuiRen             }
3337a3316fc6SZhikuiRen 
3338a3316fc6SZhikuiRen             uint64_t endCount = entryCount;
3339a3316fc6SZhikuiRen             if (!postcode.empty())
3340a3316fc6SZhikuiRen             {
3341a3316fc6SZhikuiRen                 endCount = entryCount + postcode.size();
3342a3316fc6SZhikuiRen 
3343a3316fc6SZhikuiRen                 if ((skip < endCount) && ((top + skip) > entryCount))
3344a3316fc6SZhikuiRen                 {
3345a3316fc6SZhikuiRen                     uint64_t thisBootSkip =
3346a3316fc6SZhikuiRen                         std::max(skip, entryCount) - entryCount;
3347a3316fc6SZhikuiRen                     uint64_t thisBootTop =
3348a3316fc6SZhikuiRen                         std::min(top + skip, endCount) - entryCount;
3349a3316fc6SZhikuiRen 
3350a3316fc6SZhikuiRen                     fillPostCodeEntry(aResp, postcode, bootIndex, 0,
3351a3316fc6SZhikuiRen                                       thisBootSkip, thisBootTop);
3352a3316fc6SZhikuiRen                 }
3353a3316fc6SZhikuiRen                 aResp->res.jsonValue["Members@odata.count"] = endCount;
3354a3316fc6SZhikuiRen             }
3355a3316fc6SZhikuiRen 
3356a3316fc6SZhikuiRen             // continue to previous bootIndex
3357a3316fc6SZhikuiRen             if (bootIndex < bootCount)
3358a3316fc6SZhikuiRen             {
3359a3316fc6SZhikuiRen                 getPostCodeForBoot(aResp, static_cast<uint16_t>(bootIndex + 1),
3360a3316fc6SZhikuiRen                                    bootCount, endCount, skip, top);
3361a3316fc6SZhikuiRen             }
3362a3316fc6SZhikuiRen             else
3363a3316fc6SZhikuiRen             {
3364a3316fc6SZhikuiRen                 aResp->res.jsonValue["Members@odata.nextLink"] =
3365a3316fc6SZhikuiRen                     "/redfish/v1/Systems/system/LogServices/PostCodes/"
3366a3316fc6SZhikuiRen                     "Entries?$skip=" +
3367a3316fc6SZhikuiRen                     std::to_string(skip + top);
3368a3316fc6SZhikuiRen             }
3369a3316fc6SZhikuiRen         },
337015124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
337115124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
3372a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3373a3316fc6SZhikuiRen         bootIndex);
3374a3316fc6SZhikuiRen }
3375a3316fc6SZhikuiRen 
33768d1b46d7Szhanghch05 static void
33778d1b46d7Szhanghch05     getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3378a3316fc6SZhikuiRen                          const uint64_t skip, const uint64_t top)
3379a3316fc6SZhikuiRen {
3380a3316fc6SZhikuiRen     uint64_t entryCount = 0;
3381a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
3382a3316fc6SZhikuiRen         [aResp, entryCount, skip,
3383a3316fc6SZhikuiRen          top](const boost::system::error_code ec,
3384a3316fc6SZhikuiRen               const std::variant<uint16_t>& bootCount) {
3385a3316fc6SZhikuiRen             if (ec)
3386a3316fc6SZhikuiRen             {
3387a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3388a3316fc6SZhikuiRen                 messages::internalError(aResp->res);
3389a3316fc6SZhikuiRen                 return;
3390a3316fc6SZhikuiRen             }
3391a3316fc6SZhikuiRen             auto pVal = std::get_if<uint16_t>(&bootCount);
3392a3316fc6SZhikuiRen             if (pVal)
3393a3316fc6SZhikuiRen             {
3394a3316fc6SZhikuiRen                 getPostCodeForBoot(aResp, 1, *pVal, entryCount, skip, top);
3395a3316fc6SZhikuiRen             }
3396a3316fc6SZhikuiRen             else
3397a3316fc6SZhikuiRen             {
3398a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "Post code boot index failed.";
3399a3316fc6SZhikuiRen             }
3400a3316fc6SZhikuiRen         },
340115124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
340215124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
3403a3316fc6SZhikuiRen         "org.freedesktop.DBus.Properties", "Get",
3404a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount");
3405a3316fc6SZhikuiRen }
3406a3316fc6SZhikuiRen 
3407a3316fc6SZhikuiRen class PostCodesEntryCollection : public Node
3408a3316fc6SZhikuiRen {
3409a3316fc6SZhikuiRen   public:
341052cc112dSEd Tanous     PostCodesEntryCollection(App& app) :
3411a3316fc6SZhikuiRen         Node(app, "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/")
3412a3316fc6SZhikuiRen     {
3413a3316fc6SZhikuiRen         entityPrivileges = {
3414a3316fc6SZhikuiRen             {boost::beast::http::verb::get, {{"Login"}}},
3415a3316fc6SZhikuiRen             {boost::beast::http::verb::head, {{"Login"}}},
3416a3316fc6SZhikuiRen             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
3417a3316fc6SZhikuiRen             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
3418a3316fc6SZhikuiRen             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
3419a3316fc6SZhikuiRen             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
3420a3316fc6SZhikuiRen     }
3421a3316fc6SZhikuiRen 
3422a3316fc6SZhikuiRen   private:
34238d1b46d7Szhanghch05     void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
34248d1b46d7Szhanghch05                const crow::Request& req,
3425cb13a392SEd Tanous                const std::vector<std::string>&) override
3426a3316fc6SZhikuiRen     {
3427a3316fc6SZhikuiRen 
3428a3316fc6SZhikuiRen         asyncResp->res.jsonValue["@odata.type"] =
3429a3316fc6SZhikuiRen             "#LogEntryCollection.LogEntryCollection";
3430a3316fc6SZhikuiRen         asyncResp->res.jsonValue["@odata.id"] =
3431a3316fc6SZhikuiRen             "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3432a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3433a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Description"] =
3434a3316fc6SZhikuiRen             "Collection of POST Code Log Entries";
3435a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3436a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Members@odata.count"] = 0;
3437a3316fc6SZhikuiRen 
3438a3316fc6SZhikuiRen         uint64_t skip = 0;
3439a3316fc6SZhikuiRen         uint64_t top = maxEntriesPerPage; // Show max entries by default
34408d1b46d7Szhanghch05         if (!getSkipParam(asyncResp, req, skip))
3441a3316fc6SZhikuiRen         {
3442a3316fc6SZhikuiRen             return;
3443a3316fc6SZhikuiRen         }
34448d1b46d7Szhanghch05         if (!getTopParam(asyncResp, req, top))
3445a3316fc6SZhikuiRen         {
3446a3316fc6SZhikuiRen             return;
3447a3316fc6SZhikuiRen         }
3448a3316fc6SZhikuiRen         getCurrentBootNumber(asyncResp, skip, top);
3449a3316fc6SZhikuiRen     }
3450a3316fc6SZhikuiRen };
3451a3316fc6SZhikuiRen 
3452a3316fc6SZhikuiRen class PostCodesEntry : public Node
3453a3316fc6SZhikuiRen {
3454a3316fc6SZhikuiRen   public:
345552cc112dSEd Tanous     PostCodesEntry(App& app) :
3456a3316fc6SZhikuiRen         Node(app,
3457a3316fc6SZhikuiRen              "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/",
3458a3316fc6SZhikuiRen              std::string())
3459a3316fc6SZhikuiRen     {
3460a3316fc6SZhikuiRen         entityPrivileges = {
3461a3316fc6SZhikuiRen             {boost::beast::http::verb::get, {{"Login"}}},
3462a3316fc6SZhikuiRen             {boost::beast::http::verb::head, {{"Login"}}},
3463a3316fc6SZhikuiRen             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
3464a3316fc6SZhikuiRen             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
3465a3316fc6SZhikuiRen             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
3466a3316fc6SZhikuiRen             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
3467a3316fc6SZhikuiRen     }
3468a3316fc6SZhikuiRen 
3469a3316fc6SZhikuiRen   private:
34708d1b46d7Szhanghch05     void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
34718d1b46d7Szhanghch05                const crow::Request&,
3472a3316fc6SZhikuiRen                const std::vector<std::string>& params) override
3473a3316fc6SZhikuiRen     {
34748d1b46d7Szhanghch05 
3475a3316fc6SZhikuiRen         if (params.size() != 1)
3476a3316fc6SZhikuiRen         {
3477a3316fc6SZhikuiRen             messages::internalError(asyncResp->res);
3478a3316fc6SZhikuiRen             return;
3479a3316fc6SZhikuiRen         }
3480a3316fc6SZhikuiRen 
3481a3316fc6SZhikuiRen         const std::string& targetID = params[0];
3482a3316fc6SZhikuiRen 
3483a3316fc6SZhikuiRen         size_t bootPos = targetID.find('B');
3484a3316fc6SZhikuiRen         if (bootPos == std::string::npos)
3485a3316fc6SZhikuiRen         {
3486a3316fc6SZhikuiRen             // Requested ID was not found
3487a3316fc6SZhikuiRen             messages::resourceMissingAtURI(asyncResp->res, targetID);
3488a3316fc6SZhikuiRen             return;
3489a3316fc6SZhikuiRen         }
3490a3316fc6SZhikuiRen         std::string_view bootIndexStr(targetID);
3491a3316fc6SZhikuiRen         bootIndexStr.remove_prefix(bootPos + 1);
3492a3316fc6SZhikuiRen         uint16_t bootIndex = 0;
3493a3316fc6SZhikuiRen         uint64_t codeIndex = 0;
3494a3316fc6SZhikuiRen         size_t dashPos = bootIndexStr.find('-');
3495a3316fc6SZhikuiRen 
3496a3316fc6SZhikuiRen         if (dashPos == std::string::npos)
3497a3316fc6SZhikuiRen         {
3498a3316fc6SZhikuiRen             return;
3499a3316fc6SZhikuiRen         }
3500a3316fc6SZhikuiRen         std::string_view codeIndexStr(bootIndexStr);
3501a3316fc6SZhikuiRen         bootIndexStr.remove_suffix(dashPos);
3502a3316fc6SZhikuiRen         codeIndexStr.remove_prefix(dashPos + 1);
3503a3316fc6SZhikuiRen 
3504a3316fc6SZhikuiRen         bootIndex = static_cast<uint16_t>(
350523a21a1cSEd Tanous             strtoul(std::string(bootIndexStr).c_str(), nullptr, 0));
350623a21a1cSEd Tanous         codeIndex = strtoul(std::string(codeIndexStr).c_str(), nullptr, 0);
3507a3316fc6SZhikuiRen         if (bootIndex == 0 || codeIndex == 0)
3508a3316fc6SZhikuiRen         {
3509a3316fc6SZhikuiRen             BMCWEB_LOG_DEBUG << "Get Post Code invalid entry string "
3510a3316fc6SZhikuiRen                              << params[0];
3511a3316fc6SZhikuiRen         }
3512a3316fc6SZhikuiRen 
3513a3316fc6SZhikuiRen         asyncResp->res.jsonValue["@odata.type"] = "#LogEntry.v1_4_0.LogEntry";
3514a3316fc6SZhikuiRen         asyncResp->res.jsonValue["@odata.id"] =
3515a3316fc6SZhikuiRen             "/redfish/v1/Systems/system/LogServices/PostCodes/"
3516a3316fc6SZhikuiRen             "Entries";
3517a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3518a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Description"] =
3519a3316fc6SZhikuiRen             "Collection of POST Code Log Entries";
3520a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3521a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Members@odata.count"] = 0;
3522a3316fc6SZhikuiRen 
3523a3316fc6SZhikuiRen         getPostCodeForEntry(asyncResp, bootIndex, codeIndex);
3524a3316fc6SZhikuiRen     }
3525a3316fc6SZhikuiRen };
3526a3316fc6SZhikuiRen 
35271da66f75SEd Tanous } // namespace redfish
3528