Implement OriginatedBy interface in dump entry dbus obj

This new interface "OriginatedBy" will be implemented
by all the dump entry dbus objects. It contains a property
"OriginatorId" which stores the unique id of the user that
has initiated the dump. The unique id in this case is a string
that contains the ip address of the client that initiated
the dump.

The dbus interface change for the same is at:
[1] https://gerrit.openbmc-project.xyz/c/openbmc/phosphor-dbus-interfaces/+/47057

Tested By:

[1] busctl call xyz.openbmc_project.Dump.Manager /xyz/openbmc_project/dump/bmc xyz.openbmc_project.Dump.Create CreateDump a{sv} 2 "xyz.openbmc_project.Dump.Create.CreateParameters.OriginatorId" s "<unique-id>" "xyz.openbmc_project.Dump.Create.CreateParameters.OriginatorType" s "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.Client" o "/xyz/openbmc_project/dump/bmc/entry/2"

[2] busctl --verbose call xyz.openbmc_project.Dump.Manager /xyz/openbmc_project/dump/resource xyz.openbmc_project.Dump.Create CreateDump a{sv} 4 "com.ibm.Dump.Create.CreateParameters.VSPString" s "vsp" "com.ibm.Dump.Create.CreateParameters.Password" s "password" "com.ibm.Dump.Create.CreateParameters.OriginatorId" s "<unique-id>" "xyz.openbmc_project.Dump.Create.CreateParameters.OriginatorType" s "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.Client"
MESSAGE "o" {
        OBJECT_PATH "/xyz/openbmc_project/dump/resource/entry/1";
};

[3] busctl call xyz.openbmc_project.Dump.Manager /xyz/openbmc_project/dump/system xyz.openbmc_project.Dump.Create CreateDump a{sv} 2 "com.ibm.Dump.Create.CreateParameters.OriginatorId" s "<unique-id>" "xyz.openbmc_project.Dump.Create.CreateParameters.OriginatorType" s "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.Client" o "/xyz/openbmc_project/dump/system/entry/1"

Signed-off-by: Asmitha Karunanithi <asmitk01@in.ibm.com>
Change-Id: I23c9f769fd39cd84e042d6effbb3d71c7af4e889
diff --git a/bmc_dump_entry.hpp b/bmc_dump_entry.hpp
index 964f3a7..267a0ef 100644
--- a/bmc_dump_entry.hpp
+++ b/bmc_dump_entry.hpp
@@ -23,6 +23,9 @@
 using EntryIfaces = sdbusplus::server::object_t<
     sdbusplus::xyz::openbmc_project::Dump::Entry::server::BMC>;
 
+using originatorTypes = sdbusplus::xyz::openbmc_project::Common::server::
+    OriginatedBy::OriginatorTypes;
+
 class Manager;
 
 /** @class Entry
@@ -49,16 +52,18 @@
      *  @param[in] fileSize - Dump file size in bytes.
      *  @param[in] file - Name of dump file.
      *  @param[in] status - status of the dump.
+     *  @param[in] originatorId - Id of the originator of the dump
+     *  @param[in] originatorType - Originator type
      *  @param[in] parent - The dump entry's parent.
      */
     Entry(sdbusplus::bus_t& bus, const std::string& objPath, uint32_t dumpId,
           uint64_t timeStamp, uint64_t fileSize,
           const std::filesystem::path& file,
-          phosphor::dump::OperationStatus status,
-          phosphor::dump::Manager& parent) :
+          phosphor::dump::OperationStatus status, std::string originatorId,
+          originatorTypes originatorType, phosphor::dump::Manager& parent) :
         EntryIfaces(bus, objPath.c_str(), EntryIfaces::action::defer_emit),
         phosphor::dump::Entry(bus, objPath.c_str(), dumpId, timeStamp, fileSize,
-                              status, parent),
+                              status, originatorId, originatorType, parent),
         file(file)
     {
         // Emit deferred signal.
diff --git a/dump-extensions/openpower-dumps/dump_manager_resource.cpp b/dump-extensions/openpower-dumps/dump_manager_resource.cpp
index a151961..604e796 100644
--- a/dump-extensions/openpower-dumps/dump_manager_resource.cpp
+++ b/dump-extensions/openpower-dumps/dump_manager_resource.cpp
@@ -80,6 +80,9 @@
     auto idString = std::to_string(id);
     auto objPath = std::filesystem::path(baseEntryPath) / idString;
 
+    // TODO: Get the originator Id, type from the persisted file.
+    // For now replacing it with null
+
     try
     {
         log<level::INFO>(fmt::format("Resouce Dump Notify: creating new dump "
@@ -90,7 +93,8 @@
             id, std::make_unique<resource::Entry>(
                     bus, objPath.c_str(), id, timeStamp, size, dumpId,
                     std::string(), std::string(),
-                    phosphor::dump::OperationStatus::Completed, *this)));
+                    phosphor::dump::OperationStatus::Completed, std::string(),
+                    originatorTypes::Internal, *this)));
     }
     catch (const std::invalid_argument& e)
     {
@@ -192,13 +196,20 @@
         }
     }
 
+    // Get the originator id and type from params
+    std::string originatorId;
+    originatorTypes originatorType;
+
+    phosphor::dump::extractOriginatorProperties(params, originatorId,
+                                                originatorType);
+
     try
     {
         entries.insert(std::make_pair(
             id, std::make_unique<resource::Entry>(
                     bus, objPath.c_str(), id, timeStamp, 0, INVALID_SOURCE_ID,
                     vspString, pwd, phosphor::dump::OperationStatus::InProgress,
-                    *this)));
+                    originatorId, originatorType, *this)));
     }
     catch (const std::invalid_argument& e)
     {
diff --git a/dump-extensions/openpower-dumps/dump_manager_system.cpp b/dump-extensions/openpower-dumps/dump_manager_system.cpp
index 7310dec..4aa3049 100644
--- a/dump-extensions/openpower-dumps/dump_manager_system.cpp
+++ b/dump-extensions/openpower-dumps/dump_manager_system.cpp
@@ -53,12 +53,15 @@
     auto idString = std::to_string(id);
     auto objPath = std::filesystem::path(baseEntryPath) / idString;
 
+    // TODO: Get the originator Id, Type from the persisted file.
+    // For now replacing it with null
     try
     {
         entries.insert(std::make_pair(
             id, std::make_unique<system::Entry>(
                     bus, objPath.c_str(), id, timeStamp, size, dumpId,
-                    phosphor::dump::OperationStatus::Completed, *this)));
+                    phosphor::dump::OperationStatus::Completed, std::string(),
+                    originatorTypes::Internal, *this)));
     }
     catch (const std::invalid_argument& e)
     {
@@ -83,9 +86,10 @@
     constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
     constexpr auto DIAG_MOD_TARGET = "obmc-host-crash@0.target";
 
-    if (!params.empty())
+    if (params.size() > CREATE_DUMP_MAX_PARAMS)
     {
-        log<level::WARNING>("System dump accepts no additional parameters");
+        log<level::WARNING>(
+            "System dump accepts not more than 2 additional parameters");
     }
 
     using NotAllowed =
@@ -100,6 +104,13 @@
         return std::string();
     }
 
+    // Get the originator id and type from params
+    std::string originatorId;
+    originatorTypes originatorType;
+
+    phosphor::dump::extractOriginatorProperties(params, originatorId,
+                                                originatorType);
+
     auto b = sdbusplus::bus::new_default();
     auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
                                       SYSTEMD_INTERFACE, "StartUnit");
@@ -120,7 +131,8 @@
         entries.insert(std::make_pair(
             id, std::make_unique<system::Entry>(
                     bus, objPath.c_str(), id, timeStamp, 0, INVALID_SOURCE_ID,
-                    phosphor::dump::OperationStatus::InProgress, *this)));
+                    phosphor::dump::OperationStatus::InProgress, originatorId,
+                    originatorType, *this)));
     }
     catch (const std::invalid_argument& e)
     {
diff --git a/dump-extensions/openpower-dumps/resource_dump_entry.hpp b/dump-extensions/openpower-dumps/resource_dump_entry.hpp
index 065438c..9515dc2 100644
--- a/dump-extensions/openpower-dumps/resource_dump_entry.hpp
+++ b/dump-extensions/openpower-dumps/resource_dump_entry.hpp
@@ -2,6 +2,7 @@
 
 #include "com/ibm/Dump/Entry/Resource/server.hpp"
 #include "dump_entry.hpp"
+#include "xyz/openbmc_project/Common/OriginatedBy/server.hpp"
 
 #include <sdbusplus/bus.hpp>
 #include <sdbusplus/server/object.hpp>
@@ -18,8 +19,12 @@
 using ServerObject = typename sdbusplus::server::object_t<T>;
 
 using EntryIfaces = sdbusplus::server::object_t<
+    sdbusplus::xyz::openbmc_project::Common::server::OriginatedBy,
     sdbusplus::com::ibm::Dump::Entry::server::Resource>;
 
+using originatorTypes = sdbusplus::xyz::openbmc_project::Common::server::
+    OriginatedBy::OriginatorTypes;
+
 class Manager;
 
 /** @class Entry
@@ -49,16 +54,18 @@
      *  @param[in] vspStr- Input to host to generate the resource dump.
      *  @param[in] pwd - Password needed by host to validate the request.
      *  @param[in] status - status  of the dump.
+     *  @param[in] originatorId - Id of the originator of the dump
+     *  @param[in] originatorType - Originator type
      *  @param[in] parent - The dump entry's parent.
      */
     Entry(sdbusplus::bus_t& bus, const std::string& objPath, uint32_t dumpId,
           uint64_t timeStamp, uint64_t dumpSize, const uint32_t sourceId,
           std::string vspStr, std::string pwd,
-          phosphor::dump::OperationStatus status,
-          phosphor::dump::Manager& parent) :
+          phosphor::dump::OperationStatus status, std::string originatorId,
+          originatorTypes originatorType, phosphor::dump::Manager& parent) :
         EntryIfaces(bus, objPath.c_str(), EntryIfaces::action::defer_emit),
         phosphor::dump::Entry(bus, objPath.c_str(), dumpId, timeStamp, dumpSize,
-                              status, parent)
+                              status, originatorId, originatorType, parent)
     {
         sourceDumpId(sourceId);
         vspString(vspStr);
diff --git a/dump-extensions/openpower-dumps/system_dump_entry.hpp b/dump-extensions/openpower-dumps/system_dump_entry.hpp
index bc247c9..ef3453e 100644
--- a/dump-extensions/openpower-dumps/system_dump_entry.hpp
+++ b/dump-extensions/openpower-dumps/system_dump_entry.hpp
@@ -1,6 +1,7 @@
 #pragma once
 
 #include "dump_entry.hpp"
+#include "xyz/openbmc_project/Common/OriginatedBy/server.hpp"
 #include "xyz/openbmc_project/Dump/Entry/System/server.hpp"
 
 #include <sdbusplus/bus.hpp>
@@ -16,8 +17,12 @@
 using ServerObject = typename sdbusplus::server::object_t<T>;
 
 using EntryIfaces = sdbusplus::server::object_t<
+    sdbusplus::xyz::openbmc_project::Common::server::OriginatedBy,
     sdbusplus::xyz::openbmc_project::Dump::Entry::server::System>;
 
+using originatorTypes = sdbusplus::xyz::openbmc_project::Common::server::
+    OriginatedBy::OriginatorTypes;
+
 class Manager;
 
 /** @class Entry
@@ -44,15 +49,17 @@
      *  @param[in] dumpSize - Dump size in bytes.
      *  @param[in] sourceId - DumpId provided by the source.
      *  @param[in] status - status  of the dump.
+     *  @param[in] originatorId - Id of the originator of the dump
+     *  @param[in] originatorType - Originator type
      *  @param[in] parent - The dump entry's parent.
      */
     Entry(sdbusplus::bus_t& bus, const std::string& objPath, uint32_t dumpId,
           uint64_t timeStamp, uint64_t dumpSize, const uint32_t sourceId,
-          phosphor::dump::OperationStatus status,
-          phosphor::dump::Manager& parent) :
+          phosphor::dump::OperationStatus status, std::string originatorId,
+          originatorTypes originatorType, phosphor::dump::Manager& parent) :
         EntryIfaces(bus, objPath.c_str(), EntryIfaces::action::defer_emit),
         phosphor::dump::Entry(bus, objPath.c_str(), dumpId, timeStamp, dumpSize,
-                              status, parent)
+                              status, originatorId, originatorType, parent)
     {
         sourceDumpId(sourceId);
         // Emit deferred signal.
diff --git a/dump_entry.hpp b/dump_entry.hpp
index cf15157..9f4c2a6 100644
--- a/dump_entry.hpp
+++ b/dump_entry.hpp
@@ -1,5 +1,6 @@
 #pragma once
 
+#include "xyz/openbmc_project/Common/OriginatedBy/server.hpp"
 #include "xyz/openbmc_project/Common/Progress/server.hpp"
 #include "xyz/openbmc_project/Dump/Entry/server.hpp"
 #include "xyz/openbmc_project/Object/Delete/server.hpp"
@@ -23,6 +24,7 @@
 // from sdbusplus::xyz::openbmc_project::Common::server::Progress
 // #ibm-openbmc/2809
 using EntryIfaces = sdbusplus::server::object_t<
+    sdbusplus::xyz::openbmc_project::Common::server::OriginatedBy,
     sdbusplus::xyz::openbmc_project::Common::server::Progress,
     sdbusplus::xyz::openbmc_project::Dump::server::Entry,
     sdbusplus::xyz::openbmc_project::Object::server::Delete,
@@ -31,6 +33,9 @@
 using OperationStatus =
     sdbusplus::xyz::openbmc_project::Common::server::Progress::OperationStatus;
 
+using originatorTypes = sdbusplus::xyz::openbmc_project::Common::server::
+    OriginatedBy::OriginatorTypes;
+
 class Manager;
 
 /** @class Entry
@@ -55,14 +60,19 @@
      *  @param[in] timeStamp - Dump creation timestamp
      *             since the epoch.
      *  @param[in] dumpSize - Dump file size in bytes.
+     *  @param[in] originId - Id of the originator of the dump
+     *  @param[in] originType - Originator type
      *  @param[in] parent - The dump entry's parent.
      */
     Entry(sdbusplus::bus_t& bus, const std::string& objPath, uint32_t dumpId,
           uint64_t timeStamp, uint64_t dumpSize, OperationStatus dumpStatus,
-          Manager& parent) :
+          std::string originId, originatorTypes originType, Manager& parent) :
         EntryIfaces(bus, objPath.c_str(), EntryIfaces::action::emit_no_signals),
         parent(parent), id(dumpId)
     {
+        originatorId(originId);
+        originatorType(originType);
+
         size(dumpSize);
         status(dumpStatus);
 
diff --git a/dump_manager.hpp b/dump_manager.hpp
index 5045ff4..c605cdc 100644
--- a/dump_manager.hpp
+++ b/dump_manager.hpp
@@ -6,6 +6,8 @@
 #include <sdbusplus/bus.hpp>
 #include <sdbusplus/server/object.hpp>
 
+#define CREATE_DUMP_MAX_PARAMS 2
+
 namespace phosphor
 {
 namespace dump
diff --git a/dump_manager_bmc.cpp b/dump_manager_bmc.cpp
index 66f780d..d5dd987 100644
--- a/dump_manager_bmc.cpp
+++ b/dump_manager_bmc.cpp
@@ -41,10 +41,19 @@
 sdbusplus::message::object_path
     Manager::createDump(phosphor::dump::DumpCreateParams params)
 {
-    if (!params.empty())
+    if (params.size() > CREATE_DUMP_MAX_PARAMS)
     {
-        log<level::WARNING>("BMC dump accepts no additional parameters");
+        log<level::WARNING>(
+            "BMC dump accepts not more than 2 additional parameters");
     }
+
+    // Get the originator id and type from params
+    std::string originatorId;
+    originatorTypes originatorType;
+
+    phosphor::dump::extractOriginatorProperties(params, originatorId,
+                                                originatorType);
+
     std::vector<std::string> paths;
     auto id = captureDump(Type::UserRequested, paths);
 
@@ -61,7 +70,8 @@
         entries.insert(std::make_pair(
             id, std::make_unique<bmc::Entry>(
                     bus, objPath.c_str(), id, timeStamp, 0, std::string(),
-                    phosphor::dump::OperationStatus::InProgress, *this)));
+                    phosphor::dump::OperationStatus::InProgress, originatorId,
+                    originatorType, *this)));
     }
     catch (const std::invalid_argument& e)
     {
@@ -168,13 +178,16 @@
     // Entry Object path.
     auto objPath = std::filesystem::path(baseEntryPath) / std::to_string(id);
 
+    // TODO: Get the persisted originator id & type
+    // For now, replacing it with null
     try
     {
         entries.insert(std::make_pair(
             id, std::make_unique<bmc::Entry>(
                     bus, objPath.c_str(), id, timestamp,
                     std::filesystem::file_size(file), file,
-                    phosphor::dump::OperationStatus::Completed, *this)));
+                    phosphor::dump::OperationStatus::Completed, std::string(),
+                    originatorTypes::Internal, *this)));
     }
     catch (const std::invalid_argument& e)
     {
diff --git a/dump_manager_faultlog.cpp b/dump_manager_faultlog.cpp
index a718472..7bb8352 100644
--- a/dump_manager_faultlog.cpp
+++ b/dump_manager_faultlog.cpp
@@ -2,6 +2,7 @@
 
 #include "dump_manager_faultlog.hpp"
 
+#include "dump_utils.hpp"
 #include "faultlog_dump_entry.hpp"
 
 #include <fmt/core.h>
@@ -46,6 +47,13 @@
         log<level::INFO>("Got additional parameters");
     }
 
+    // Get the originator id and type from params
+    std::string originatorId;
+    originatorTypes originatorType;
+
+    phosphor::dump::extractOriginatorProperties(params, originatorId,
+                                                originatorType);
+
     // Get the id
     auto id = lastEntryId + 1;
     auto idString = std::to_string(id);
@@ -89,12 +97,13 @@
                 std::chrono::system_clock::now().time_since_epoch())
                 .count();
 
-        entries.insert(std::make_pair(
-            id,
-            std::make_unique<faultlog::Entry>(
-                bus, objPath.c_str(), id, timestamp,
-                std::filesystem::file_size(faultLogFilePath), faultLogFilePath,
-                phosphor::dump::OperationStatus::Completed, *this)));
+        entries.insert(
+            std::make_pair(id, std::make_unique<faultlog::Entry>(
+                                   bus, objPath.c_str(), id, timestamp,
+                                   std::filesystem::file_size(faultLogFilePath),
+                                   faultLogFilePath,
+                                   phosphor::dump::OperationStatus::Completed,
+                                   originatorId, originatorType, *this)));
     }
     catch (const std::invalid_argument& e)
     {
diff --git a/dump_utils.hpp b/dump_utils.hpp
index 1a18844..6661bd4 100644
--- a/dump_utils.hpp
+++ b/dump_utils.hpp
@@ -1,9 +1,17 @@
 #pragma once
 
+#include "dump_manager.hpp"
+
+#include <fmt/core.h>
 #include <systemd/sd-event.h>
 #include <unistd.h>
 
+#include <com/ibm/Dump/Create/server.hpp>
+#include <phosphor-logging/elog-errors.hpp>
+#include <phosphor-logging/elog.hpp>
 #include <sdbusplus/bus.hpp>
+#include <xyz/openbmc_project/Common/error.hpp>
+#include <xyz/openbmc_project/Dump/Create/server.hpp>
 #include <xyz/openbmc_project/State/Boot/Progress/server.hpp>
 
 #include <memory>
@@ -16,6 +24,9 @@
 using BootProgress = sdbusplus::xyz::openbmc_project::State::Boot::server::
     Progress::ProgressStages;
 
+using namespace phosphor::logging;
+using namespace sdbusplus::xyz::openbmc_project::Common::Error;
+
 /* Need a custom deleter for freeing up sd_event */
 struct EventDeleter
 {
@@ -92,5 +103,72 @@
  */
 bool isHostRunning();
 
+inline void extractOriginatorProperties(phosphor::dump::DumpCreateParams params,
+                                        std::string& originatorId,
+                                        originatorTypes& originatorType)
+{
+    using InvalidArgument =
+        sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument;
+    using Argument = xyz::openbmc_project::Common::InvalidArgument;
+    using CreateParametersXYZ =
+        sdbusplus::xyz::openbmc_project::Dump::server::Create::CreateParameters;
+
+    auto iter = params.find(
+        sdbusplus::xyz::openbmc_project::Dump::server::Create::
+            convertCreateParametersToString(CreateParametersXYZ::OriginatorId));
+    if (iter == params.end())
+    {
+        log<level::INFO>("OriginatorId is not provided");
+    }
+    else
+    {
+        try
+        {
+            originatorId = std::get<std::string>(iter->second);
+        }
+        catch (const std::bad_variant_access& e)
+        {
+            // Exception will be raised if the input is not string
+            log<level::ERR>(
+                fmt::format(
+                    "An invalid  originatorId passed. It should be a string, "
+                    "errormsg({})",
+                    e.what())
+                    .c_str());
+            elog<InvalidArgument>(Argument::ARGUMENT_NAME("ORIGINATOR_ID"),
+                                  Argument::ARGUMENT_VALUE("INVALID INPUT"));
+        }
+    }
+
+    iter = params.find(sdbusplus::xyz::openbmc_project::Dump::server::Create::
+                           convertCreateParametersToString(
+                               CreateParametersXYZ::OriginatorType));
+    if (iter == params.end())
+    {
+        log<level::INFO>("OriginatorType is not provided. Replacing the string "
+                         "with the default value");
+        originatorType = originatorTypes::Internal;
+    }
+    else
+    {
+        try
+        {
+            std::string type = std::get<std::string>(iter->second);
+            originatorType = sdbusplus::xyz::openbmc_project::Common::server::
+                OriginatedBy::convertOriginatorTypesFromString(type);
+        }
+        catch (const std::bad_variant_access& e)
+        {
+            // Exception will be raised if the input is not string
+            log<level::ERR>(fmt::format("An invalid originatorType passed, "
+                                        "errormsg({})",
+                                        e.what())
+                                .c_str());
+            elog<InvalidArgument>(Argument::ARGUMENT_NAME("ORIGINATOR_TYPE"),
+                                  Argument::ARGUMENT_VALUE("INVALID INPUT"));
+        }
+    }
+}
+
 } // namespace dump
 } // namespace phosphor
diff --git a/faultlog_dump_entry.hpp b/faultlog_dump_entry.hpp
index 47299ca..efb6150 100644
--- a/faultlog_dump_entry.hpp
+++ b/faultlog_dump_entry.hpp
@@ -44,16 +44,18 @@
      *  @param[in] fileSize - Dump file size in bytes.
      *  @param[in] file - Full path of dump file.
      *  @param[in] status - status of the dump.
+     *  @param[in] originatorId - Id of the originator of the dump
+     *  @param[in] originatorType - Originator type
      *  @param[in] parent - The dump entry's parent.
      */
     Entry(sdbusplus::bus_t& bus, const std::string& objPath, uint32_t dumpId,
           uint64_t timeStamp, uint64_t fileSize,
           const std::filesystem::path& file,
-          phosphor::dump::OperationStatus status,
-          phosphor::dump::Manager& parent) :
+          phosphor::dump::OperationStatus status, std::string originatorId,
+          originatorTypes originatorType, phosphor::dump::Manager& parent) :
         EntryIfaces(bus, objPath.c_str(), EntryIfaces::action::defer_emit),
         phosphor::dump::Entry(bus, objPath.c_str(), dumpId, timeStamp, fileSize,
-                              status, parent),
+                              status, originatorId, originatorType, parent),
         file(file)
     {
         // Emit deferred signal.