Add implementation for delete all error log entries in one shot

Resolves openbmc/openbmc#1561.

Change-Id: Iac5aaee1bdf9b87ccce9bf8801468ac5a8f9be6c
Signed-off-by: Nagaraju Goruganti <ngorugan@in.ibm.com>
diff --git a/elog_entry.hpp b/elog_entry.hpp
index b3d5963..24d93cf 100644
--- a/elog_entry.hpp
+++ b/elog_entry.hpp
@@ -19,7 +19,10 @@
 using AssociationList =
      std::vector<std::tuple<std::string, std::string, std::string>>;
 
+namespace internal
+{
 class Manager;
+}
 
 /** @class Entry
  *  @brief OpenBMC logging entry implementation.
@@ -57,7 +60,7 @@
               std::string&& msgErr,
               std::vector<std::string>&& additionalDataErr,
               AssociationList&& objects,
-              Manager& parent) :
+              internal::Manager& parent) :
               EntryIfaces(bus, path.c_str(), true),
               parent(parent)
         {
@@ -87,7 +90,7 @@
         Entry(sdbusplus::bus::bus& bus,
               const std::string& path,
               uint32_t entryId,
-              Manager& parent) :
+              internal::Manager& parent) :
               EntryIfaces(bus, path.c_str(), true),
               parent(parent)
         {
@@ -113,7 +116,7 @@
         AssociationList assocs = {};
 
         /** @brief This entry's parent */
-        Manager& parent;
+        internal::Manager& parent;
 };
 
 } // namespace logging
diff --git a/log_manager.cpp b/log_manager.cpp
index b535039..c1fe383 100644
--- a/log_manager.cpp
+++ b/log_manager.cpp
@@ -23,7 +23,8 @@
 {
 namespace logging
 {
-
+namespace internal
+{
 void Manager::commit(uint64_t transactionId, std::string errMsg)
 {
     if (capped)
@@ -247,5 +248,6 @@
     entryId = *(std::max_element(errorIds.begin(), errorIds.end()));
 }
 
+} // namespace internal
 } // namespace logging
 } // namepsace phosphor
diff --git a/log_manager.hpp b/log_manager.hpp
index f366684..af176f9 100644
--- a/log_manager.hpp
+++ b/log_manager.hpp
@@ -4,6 +4,7 @@
 #include <phosphor-logging/log.hpp>
 #include "elog_entry.hpp"
 #include "xyz/openbmc_project/Logging/Internal/Manager/server.hpp"
+#include "xyz/openbmc_project/Collection/DeleteAll/server.hpp"
 
 namespace phosphor
 {
@@ -13,6 +14,9 @@
 extern const std::map<std::string,std::vector<std::string>> g_errMetaMap;
 extern const std::map<std::string,level> g_errLevelMap;
 
+using DeleteAllIface = sdbusplus::server::object::object <
+                       sdbusplus::xyz::openbmc_project::Collection::server::DeleteAll >;
+
 namespace details
 {
 
@@ -24,6 +28,9 @@
 
 } // namespace details
 
+namespace internal
+{
+
 /** @class Manager
  *  @brief OpenBMC logging manager implementation.
  *  @details A concrete implementation for the
@@ -73,6 +80,20 @@
          */
         void restore();
 
+        /** @brief  Erase all error log entries
+         *
+         */
+        void eraseAll()
+        {
+            auto iter = entries.begin();
+            while (iter != entries.end())
+            {
+                auto entry = iter->first;
+                ++iter;
+                erase(entry);
+            }
+        }
+
     private:
         /** @brief Call metadata handler(s), if any. Handlers may create
          *         associations.
@@ -104,5 +125,46 @@
         bool capped;
 };
 
+} //namespace internal
+
+/** @class Manager
+ *  @brief Implementation for delete all error log entries.
+ *  @details A concrete implementation for the
+ *  xyz.openbmc_project.Collection.DeleteAll
+ */
+class Manager : public DeleteAllIface
+{
+    public:
+        Manager() = delete;
+        Manager(const Manager&) = delete;
+        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.
+         *         Defer signal registration (pass true for deferSignal to the
+         *         base class) until after the properties are set.
+         *  @param[in] bus - Bus to attach to.
+         *  @param[in] path - Path to attach at.
+         *  @param[in] manager - Reference to internal manager object.
+         */
+        Manager(sdbusplus::bus::bus& bus,
+                   const std::string& path,
+                   internal::Manager& manager) :
+            DeleteAllIface(bus, path.c_str(), true),
+            manager(manager) {};
+
+        /** @brief Delete all d-bus objects.
+         */
+        void deleteAll()
+        {
+            manager.eraseAll();
+        }
+    private:
+        /** @brief This is a reference to manager object */
+        internal::Manager& manager;
+};
+
 } // namespace logging
 } // namespace phosphor
diff --git a/log_manager_main.cpp b/log_manager_main.cpp
index b04c0e3..f06a79d 100644
--- a/log_manager_main.cpp
+++ b/log_manager_main.cpp
@@ -11,13 +11,15 @@
     // Add sdbusplus ObjectManager for the 'root' path of the logging manager.
     sdbusplus::server::manager::manager objManager(bus, OBJ_LOGGING);
 
-    phosphor::logging::Manager manager(bus, OBJ_INTERNAL);
+    phosphor::logging::internal::Manager iMgr(bus, OBJ_INTERNAL);
+
+    phosphor::logging::Manager mgr(bus, OBJ_LOGGING, iMgr);
 
     // Create a directory to persist errors.
     std::experimental::filesystem::create_directories(ERRLOG_PERSIST_PATH);
 
     // Recreate error d-bus objects from persisted errors.
-    manager.restore();
+    iMgr.restore();
 
     bus.request_name(BUSNAME_LOGGING);
 
diff --git a/test/serialization_tests.hpp b/test/serialization_tests.hpp
index 111c7f7..a8054d3 100644
--- a/test/serialization_tests.hpp
+++ b/test/serialization_tests.hpp
@@ -16,7 +16,7 @@
 
 char tmplt[] = "/tmp/logging_test.XXXXXX";
 auto bus = sdbusplus::bus::new_default();
-phosphor::logging::Manager manager(bus, OBJ_INTERNAL);
+phosphor::logging::internal::Manager manager(bus, OBJ_INTERNAL);
 
 class TestSerialization : public testing::Test
 {