Implement create function in event manager

Implement the logging event interface.

Create the dbus event object based on the event type.

Change-Id: Idfa9e5c43f170d904fd25f22d73e0509b1785fc9
Signed-off-by: Ratan Gupta <ratagupt@in.ibm.com>
diff --git a/src/Makefile.am b/src/Makefile.am
index 94f81b8..73ebca8 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -15,7 +15,8 @@
 phosphor_dbus_monitor_LDADD = \
 	$(SDBUSPLUS_LIBS) \
 	$(PHOSPHOR_DBUS_INTERFACES_LIBS) \
-	$(PHOSPHOR_LOGGING_LIBS)
+	$(PHOSPHOR_LOGGING_LIBS) \
+	-lstdc++fs
 phosphor_dbus_monitor_CXXFLAGS = \
 	$(SDBUSPLUS_CFLAGS) \
 	$(PHOSPHOR_DBUS_INTERFACES_CFLAGS) \
diff --git a/src/event.hpp b/src/event.hpp
index ca256a7..ad02986 100644
--- a/src/event.hpp
+++ b/src/event.hpp
@@ -2,6 +2,9 @@
 
 #include <phosphor-logging/log.hpp>
 #include "callback.hpp"
+#include "event_manager.hpp"
+
+#include <sstream>
 
 namespace phosphor
 {
@@ -113,7 +116,13 @@
         void createEvent(
             const std::string& path,
             const std::string& property,
-            const any_ns::any& value) const override {}
+            const any_ns::any& value) const override
+        {
+            std::stringstream ss {};
+            ss << any_ns::any_cast<T>(value);
+            phosphor::events::getManager().create(
+                name, message, path, property, ss.str());
+        }
 
         /** @brief Event Name */
         std::string name;
diff --git a/src/event_entry.hpp b/src/event_entry.hpp
new file mode 100644
index 0000000..7be66d9
--- /dev/null
+++ b/src/event_entry.hpp
@@ -0,0 +1,62 @@
+#pragma once
+
+#include "sdbusplus.hpp"
+#include "xyz/openbmc_project/Logging/Event/server.hpp"
+
+#include <sdbusplus/bus.hpp>
+#include <sdbusplus/server/object.hpp>
+
+namespace phosphor
+{
+namespace events
+{
+
+using namespace phosphor::dbus::monitoring;
+
+using EntryIface = sdbusplus::server::object::object <
+                        sdbusplus::xyz::openbmc_project::Logging::server::Event >;
+
+/** @class Entry
+ *  @brief OpenBMC Event entry implementation.
+ *  @details A concrete implementation for the
+ *  xyz.openbmc_project.Event.Entry.
+ */
+class Entry : public EntryIface
+{
+    public:
+        Entry() = delete;
+        Entry(const Entry&) = delete;
+        Entry& operator=(const Entry&) = delete;
+        Entry(Entry&&) = delete;
+        Entry& operator=(Entry&&) = delete;
+        virtual ~Entry() = default;
+
+        /** @brief Constructor to put object onto bus at a dbus path.
+         *  @param[in] path - Path to attach at.
+         *  @param[in] eventId - The event entry id.
+         *  @param[in] timestamp - timestamp when the event created.
+         *  @param[in] msg - The message of the event.
+         *  @param[in] metaData - The event metadata.
+         */
+        Entry(
+            const std::string& path,
+            uint64_t eventTimestamp,
+            std::string&& msg,
+            std::vector<std::string>&& metaData) :
+                EntryIface(SDBusPlus::getBus(), path.c_str(), true),
+                objectPath(path)
+            {
+                timestamp(eventTimestamp);
+                message(msg);
+                additionalData(metaData);
+                // Emit deferred signal.
+                this->emit_object_added();
+            }
+
+        /** @brief Path of Object. */
+        std::string objectPath;
+
+};
+
+} // namespace events
+} // namespace phosphor
diff --git a/src/event_manager.cpp b/src/event_manager.cpp
index ef2098a..9102137 100644
--- a/src/event_manager.cpp
+++ b/src/event_manager.cpp
@@ -13,9 +13,12 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+
+#include "config.h"
 #include "event.hpp"
 #include "event_manager.hpp"
-#include "config.h"
+
+#include <experimental/filesystem>
 
 namespace phosphor
 {
@@ -24,11 +27,46 @@
 
 void Manager::create(
     const std::string& eventName,
+    const std::string& eventMessage,
     const std::string& objectPath,
     const std::string& propertyName,
-    const std::string& propertyvalue)
+    const std::string& propertyValue)
 {
-    // TODO Implement it in later commit.
+    using namespace std::string_literals;
+    namespace fs = std::experimental::filesystem;
+
+    auto msg = eventMessage;
+    std::vector<std::string> additionalData;
+
+    auto propVal = propertyName + "=" + propertyValue;
+    auto path = "path="s + objectPath;
+
+    additionalData.push_back(std::move(path));
+    additionalData.push_back(std::move(propVal));
+
+    auto& eventQueue = eventMap[eventName];
+
+    // get the last event entry for this event
+    // to generate the id.
+    auto id = 0;
+    if (eventQueue.size() > 0)
+    {
+        fs::path path(eventQueue.back()->objectPath);
+        id = std::stoi(std::string(path.filename().c_str()));
+        id ++;
+    }
+
+    auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
+                  std::chrono::system_clock::now().time_since_epoch()).count();
+
+    auto objPath =  std::string(OBJ_EVENT) + '/' + eventName + '/' +
+                        std::to_string(id);
+
+    eventQueue.emplace(std::make_unique<Entry>(
+                       objPath,
+                       ms, // Milliseconds since 1970
+                       std::move(msg),
+                       std::move(additionalData)));
 }
 
 Manager& getManager()
diff --git a/src/event_manager.hpp b/src/event_manager.hpp
index 3d1f8d1..7184852 100644
--- a/src/event_manager.hpp
+++ b/src/event_manager.hpp
@@ -1,5 +1,10 @@
 #pragma once
 
+#include "event_entry.hpp"
+
+#include <map>
+#include <memory>
+#include <queue>
 #include <sdbusplus/bus.hpp>
 
 namespace phosphor
@@ -24,16 +29,26 @@
          *  @detail Add the objectPath,propertyName, propertyValue
          *          as additional data of the event object.
          *  @param[in] eventName - Name of the event.
+         *  @param[in] eventMessage - Message for the event.
          *  @param[in] objectPath - Path of the D-Bus object.
          *  @param[in] propertyName - Name of the property.
          *  @param[in] propertyValue - Value of the property.
          */
         void create(
             const std::string& eventName,
+            const std::string& eventMessage,
             const std::string& objectPath,
             const std::string& propertyName,
             const std::string& propertyValue);
 
+    private:
+        using EventName = std::string;
+        /** @brief Queue of events */
+        using EventQueue = std::queue<std::unique_ptr<Entry>>;
+
+        using EventMap = std::map<EventName, EventQueue>;
+        /** @brief Map of event name  and the list of events **/
+        EventMap eventMap;
 };
 
 Manager& getManager();
diff --git a/src/test/Makefile.am b/src/test/Makefile.am
index 5a4d20d..717bacc 100644
--- a/src/test/Makefile.am
+++ b/src/test/Makefile.am
@@ -197,4 +197,6 @@
 	$(PHOSPHOR_LOGGING_LIBS) \
 	$(builddir)/../journal.o \
 	$(builddir)/../elog.o \
-	$(builddir)/../resolve_errors.o
+	$(builddir)/../resolve_errors.o \
+	$(builddir)/../event_manager.o \
+	-lstdc++fs