1 #include "config.h"
2 
3 #include "dump_manager_resource.hpp"
4 
5 #include "dump_utils.hpp"
6 #include "resource_dump_entry.hpp"
7 #include "xyz/openbmc_project/Common/error.hpp"
8 
9 #include <phosphor-logging/elog-errors.hpp>
10 #include <phosphor-logging/elog.hpp>
11 
12 namespace openpower
13 {
14 namespace dump
15 {
16 namespace resource
17 {
18 
19 using namespace phosphor::logging;
20 using InternalFailure =
21     sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
22 
23 void Manager::notify(uint32_t dumpId, uint64_t size)
24 {
25     // Get the timestamp
26     std::time_t timeStamp = std::time(nullptr);
27 
28     // If there is an entry with this sourceId or an invalid id
29     // update that.
30     // If host is sending the source id before the completion
31     // the source id will be updated by the transport layer with host.
32     // if not the source id will stay as invalid one.
33     for (auto& entry : entries)
34     {
35         openpower::dump::resource::Entry* resEntry =
36             dynamic_cast<openpower::dump::resource::Entry*>(entry.second.get());
37         if ((resEntry->status() ==
38              phosphor::dump::OperationStatus::InProgress) &&
39             ((resEntry->sourceDumpId() == dumpId) ||
40              (resEntry->sourceDumpId() == INVALID_SOURCE_ID)))
41         {
42             resEntry->update(timeStamp, size, dumpId);
43             return;
44         }
45     }
46     // Get the id
47     auto id = lastEntryId + 1;
48     auto idString = std::to_string(id);
49     auto objPath = fs::path(baseEntryPath) / idString;
50 
51     try
52     {
53         entries.insert(std::make_pair(
54             id, std::make_unique<resource::Entry>(
55                     bus, objPath.c_str(), id, timeStamp, size, dumpId,
56                     std::string(), std::string(),
57                     phosphor::dump::OperationStatus::Completed, *this)));
58     }
59     catch (const std::invalid_argument& e)
60     {
61         log<level::ERR>(e.what());
62         log<level::ERR>("Error in creating resource dump entry",
63                         entry("OBJECTPATH=%s", objPath.c_str()),
64                         entry("ID=%d", id), entry("TIMESTAMP=%ull", timeStamp),
65                         entry("SIZE=%d", size), entry("SOURCEID=%d", dumpId));
66         report<InternalFailure>();
67         return;
68     }
69     lastEntryId++;
70 }
71 
72 sdbusplus::message::object_path
73     Manager::createDump(std::map<std::string, std::string> params)
74 {
75 
76     using NotAllowed =
77         sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed;
78     using Reason = xyz::openbmc_project::Common::NotAllowed::REASON;
79 
80     // Allow creating resource dump only when the host is up.
81     if (!phosphor::dump::isHostRunning())
82     {
83         elog<NotAllowed>(
84             Reason("Resource dump can be initiated only when the host is up"));
85         return std::string();
86     }
87     using CreateParameters =
88         sdbusplus::com::ibm::Dump::server::Create::CreateParameters;
89 
90     auto id = lastEntryId + 1;
91     auto idString = std::to_string(id);
92     auto objPath = fs::path(baseEntryPath) / idString;
93     std::time_t timeStamp = std::time(nullptr);
94 
95     std::string vspString = params[sdbusplus::com::ibm::Dump::server::Create::
96                                        convertCreateParametersToString(
97                                            CreateParameters::VSPString)];
98     std::string pwd =
99         params[sdbusplus::com::ibm::Dump::server::Create::
100                    convertCreateParametersToString(CreateParameters::Password)];
101 
102     try
103     {
104         entries.insert(std::make_pair(
105             id, std::make_unique<resource::Entry>(
106                     bus, objPath.c_str(), id, timeStamp, 0, INVALID_SOURCE_ID,
107                     vspString, pwd, phosphor::dump::OperationStatus::InProgress,
108                     *this)));
109     }
110     catch (const std::invalid_argument& e)
111     {
112         log<level::ERR>(e.what());
113         log<level::ERR>("Error in creating resource dump entry",
114                         entry("OBJECTPATH=%s", objPath.c_str()),
115                         entry("VSPSTRING=%s", vspString.c_str()),
116                         entry("ID=%d", id));
117         elog<InternalFailure>();
118         return std::string();
119     }
120     lastEntryId++;
121     return objPath.string();
122 }
123 
124 } // namespace resource
125 } // namespace dump
126 } // namespace openpower
127