Make OpenPower specific dump an optional feature

Make all OpenPower dumps as optional feature and should be
enabled in the configuration. System dump is the currently
implemented example of OpenPower dump. BMC dump will be
enabled by default.

To enable
openpower-dumps-extension=enabled

Test:
Tested all existing dump operations using automated
testing.

Signed-off-by: Dhruvaraj Subhashchandran <dhruvaraj@in.ibm.com>
Change-Id: If20b4711dfcf02a2a8ea23848f7409576813c832
diff --git a/dump-extensions/default/default.cpp b/dump-extensions/default/default.cpp
new file mode 100644
index 0000000..4c56897
--- /dev/null
+++ b/dump-extensions/default/default.cpp
@@ -0,0 +1,11 @@
+#include "dump-extensions.hpp"
+
+namespace phosphor
+{
+namespace dump
+{
+void loadExtensions(sdbusplus::bus::bus&, DumpManagerList&)
+{
+}
+} // namespace dump
+} // namespace phosphor
diff --git a/dump-extensions/default/meson.build b/dump-extensions/default/meson.build
new file mode 100644
index 0000000..dc86093
--- /dev/null
+++ b/dump-extensions/default/meson.build
@@ -0,0 +1,5 @@
+# SPDX-License-Identifier: Apache-2.0
+
+phosphor_dump_manager_sources += [
+        'dump-extensions/default/default.cpp',
+    ]
diff --git a/dump-extensions/meson.build b/dump-extensions/meson.build
new file mode 100644
index 0000000..8f58f2c
--- /dev/null
+++ b/dump-extensions/meson.build
@@ -0,0 +1,7 @@
+# SPDX-License-Identifier: Apache-2.0
+
+if get_option('openpower-dumps-extension').enabled()
+    subdir('openpower-dumps')
+else
+    subdir('default')
+endif
diff --git a/dump-extensions/openpower-dumps/dump-extensions.cpp b/dump-extensions/openpower-dumps/dump-extensions.cpp
new file mode 100644
index 0000000..6a707fa
--- /dev/null
+++ b/dump-extensions/openpower-dumps/dump-extensions.cpp
@@ -0,0 +1,19 @@
+#include "config.h"
+
+#include "dump-extensions.hpp"
+
+#include "dump_manager_system.hpp"
+
+namespace phosphor
+{
+namespace dump
+{
+
+void loadExtensions(sdbusplus::bus::bus& bus, DumpManagerList& dumpList)
+{
+
+    dumpList.push_back(std::make_unique<phosphor::dump::system::Manager>(
+        bus, SYSTEM_DUMP_OBJPATH, SYSTEM_DUMP_OBJ_ENTRY));
+}
+} // namespace dump
+} // namespace phosphor
diff --git a/dump-extensions/openpower-dumps/dump_manager_system.cpp b/dump-extensions/openpower-dumps/dump_manager_system.cpp
new file mode 100644
index 0000000..865ed91
--- /dev/null
+++ b/dump-extensions/openpower-dumps/dump_manager_system.cpp
@@ -0,0 +1,58 @@
+#include "config.h"
+
+#include "dump_manager_system.hpp"
+
+#include "system_dump_entry.hpp"
+
+#include <phosphor-logging/elog.hpp>
+
+namespace phosphor
+{
+namespace dump
+{
+namespace system
+{
+
+using namespace phosphor::logging;
+
+void Manager::notify(NewDump::DumpType dumpType, uint32_t dumpId, uint64_t size)
+{
+
+    if (dumpType != NewDump::DumpType::System)
+    {
+        log<level::ERR>("Only system dump is supported",
+                        entry("DUMPTYPE=%d", dumpType));
+        return;
+    }
+    // Get the timestamp
+    auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
+                  std::chrono::system_clock::now().time_since_epoch())
+                  .count();
+    // Get the id
+    auto id = lastEntryId + 1;
+    auto idString = std::to_string(id);
+    auto objPath = fs::path(baseEntryPath) / idString;
+    entries.insert(std::make_pair(
+        id, std::make_unique<system::Entry>(bus, objPath.c_str(), id, ms, size,
+                                            dumpId, *this)));
+    lastEntryId++;
+}
+
+uint32_t Manager::createDump()
+{
+    constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
+    constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1";
+    constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
+    constexpr auto DIAG_MOD_TARGET = "obmc-host-diagnostic-mode@0.target";
+    auto b = sdbusplus::bus::new_default();
+    auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
+                                      SYSTEMD_INTERFACE, "StartUnit");
+    method.append(DIAG_MOD_TARGET); // unit to activate
+    method.append("replace");
+    bus.call_noreply(method);
+    return ++lastEntryId;
+}
+
+} // namespace system
+} // namespace dump
+} // namespace phosphor
diff --git a/dump-extensions/openpower-dumps/dump_manager_system.hpp b/dump-extensions/openpower-dumps/dump_manager_system.hpp
new file mode 100644
index 0000000..441611d
--- /dev/null
+++ b/dump-extensions/openpower-dumps/dump_manager_system.hpp
@@ -0,0 +1,75 @@
+#pragma once
+
+#include "dump_manager.hpp"
+#include "dump_utils.hpp"
+#include "xyz/openbmc_project/Dump/NewDump/server.hpp"
+
+#include <sdbusplus/bus.hpp>
+#include <sdbusplus/server/object.hpp>
+#include <xyz/openbmc_project/Dump/Create/server.hpp>
+
+namespace phosphor
+{
+namespace dump
+{
+namespace system
+{
+
+using NotifyIface = sdbusplus::server::object::object<
+    sdbusplus::xyz::openbmc_project::Dump::server::Create,
+    sdbusplus::xyz::openbmc_project::Dump::server::NewDump>;
+
+/** @class Manager
+ *  @brief System Dump  manager implementation.
+ *  @details A concrete implementation for the
+ *  xyz.openbmc_project.Dump.Notify DBus API
+ */
+class Manager : virtual public NotifyIface,
+                virtual public phosphor::dump::Manager
+{
+  public:
+    Manager() = delete;
+    Manager(const Manager&) = default;
+    Manager& operator=(const Manager&) = delete;
+    Manager(Manager&&) = delete;
+    Manager& operator=(Manager&&) = delete;
+    virtual ~Manager() = default;
+
+    /** @brief Constructor to put object onto bus at a dbus path.
+     *  @param[in] bus - Bus to attach to.
+     *  @param[in] event - Dump manager sd_event loop.
+     *  @param[in] path - Path to attach at.
+     *  @param[in] baseEntryPath - Base path of the dump entry.
+     */
+    Manager(sdbusplus::bus::bus& bus, const char* path,
+            const std::string& baseEntryPath) :
+        NotifyIface(bus, path),
+        phosphor::dump::Manager(bus, path, baseEntryPath)
+    {
+    }
+
+    void restore() override
+    {
+        // TODO #2597  Implement the restore to restore the dump entries
+        // after the service restart.
+    }
+
+    /** @brief Notify the system dump manager about creation of a new dump.
+     *  @param[in] dumpType - Type of the Dump.
+     *  @param[in] dumpId - Id from the source of the dump.
+     *  @param[in] size - Size of the dump.
+     */
+    void notify(NewDump::DumpType dumpType, uint32_t dumpId,
+                uint64_t size) override;
+
+    /** @brief Implementation for CreateDump
+     *  Method to create Dump.
+     *
+     *  @return id - The Dump entry id number.
+     */
+    uint32_t createDump() override;
+};
+
+} // namespace system
+} // namespace dump
+} // namespace phosphor
diff --git a/dump-extensions/openpower-dumps/meson.build b/dump-extensions/openpower-dumps/meson.build
new file mode 100644
index 0000000..308e167
--- /dev/null
+++ b/dump-extensions/openpower-dumps/meson.build
@@ -0,0 +1,7 @@
+# SPDX-License-Identifier: Apache-2.0
+
+phosphor_dump_manager_sources += [
+        'dump-extensions/openpower-dumps/dump-extensions.cpp',
+        'dump-extensions/openpower-dumps/dump_manager_system.cpp',
+        'dump-extensions/openpower-dumps/system_dump_entry.cpp'
+    ]
diff --git a/dump-extensions/openpower-dumps/system_dump_entry.cpp b/dump-extensions/openpower-dumps/system_dump_entry.cpp
new file mode 100644
index 0000000..53d8788
--- /dev/null
+++ b/dump-extensions/openpower-dumps/system_dump_entry.cpp
@@ -0,0 +1,20 @@
+#include "system_dump_entry.hpp"
+
+#include "offload-extensions.hpp"
+
+namespace phosphor
+{
+namespace dump
+{
+namespace system
+{
+
+void Entry::initiateOffload(std::string uri)
+{
+    phosphor::dump::Entry::initiateOffload(uri);
+    phosphor::dump::host::requestOffload(sourceDumpId());
+}
+
+} // namespace system
+} // namespace dump
+} // namespace phosphor
diff --git a/dump-extensions/openpower-dumps/system_dump_entry.hpp b/dump-extensions/openpower-dumps/system_dump_entry.hpp
new file mode 100644
index 0000000..895ca37
--- /dev/null
+++ b/dump-extensions/openpower-dumps/system_dump_entry.hpp
@@ -0,0 +1,68 @@
+#pragma once
+
+#include "dump_entry.hpp"
+#include "xyz/openbmc_project/Dump/Entry/System/server.hpp"
+
+#include <sdbusplus/bus.hpp>
+#include <sdbusplus/server/object.hpp>
+
+namespace phosphor
+{
+namespace dump
+{
+namespace system
+{
+template <typename T>
+using ServerObject = typename sdbusplus::server::object::object<T>;
+
+using EntryIfaces = sdbusplus::server::object::object<
+    sdbusplus::xyz::openbmc_project::Dump::Entry::server::System>;
+
+namespace fs = std::experimental::filesystem;
+
+class Manager;
+
+/** @class Entry
+ *  @brief System Dump Entry implementation.
+ *  @details A concrete implementation for the
+ *  xyz.openbmc_project.Dump.Entry DBus API
+ */
+class Entry : virtual public EntryIfaces, virtual public phosphor::dump::Entry
+{
+  public:
+    Entry() = delete;
+    Entry(const Entry&) = delete;
+    Entry& operator=(const Entry&) = delete;
+    Entry(Entry&&) = delete;
+    Entry& operator=(Entry&&) = delete;
+    ~Entry() = default;
+
+    /** @brief Constructor for the Dump Entry Object
+     *  @param[in] bus - Bus to attach to.
+     *  @param[in] objPath - Object path to attach to
+     *  @param[in] dumpId - Dump id.
+     *  @param[in] timeStamp - Dump creation timestamp
+     *             since the epoch.
+     *  @param[in] dumpSize - Dump size in bytes.
+     *  @param[in] sourceId - DumpId provided by the source..
+     *  @param[in] parent - The dump entry's parent.
+     */
+    Entry(sdbusplus::bus::bus& bus, const std::string& objPath, uint32_t dumpId,
+          uint64_t timeStamp, uint64_t dumpSize, const uint32_t sourceId,
+          phosphor::dump::Manager& parent) :
+        EntryIfaces(bus, objPath.c_str(), true),
+        phosphor::dump::Entry(bus, objPath.c_str(), dumpId, timeStamp, dumpSize,
+                              parent)
+    {
+        sourceDumpId(sourceId);
+    };
+
+    /** @brief Method to initiate the offload of dump
+     *  @param[in] uri - URI to offload dump.
+     */
+    void initiateOffload(std::string uri);
+};
+
+} // namespace system
+} // namespace dump
+} // namespace phosphor