Add Manager class

This class will hold the IBM interface objects.

It has callbacks on the interfaces added and removed
signals for the actual logging entries so it knows when
to add and remove the IBM interface objects.

It can hold multiple interface objects per logging entry
by using a map of std::experimental::any objects.

Future commits will add more functionality, such as doing
the actual creation of the interface objects and checking
for existing error logs entries on startup.

Change-Id: I1feef9e3416380a0bc9f94e470204eeac5464200
Signed-off-by: Matt Spinler <spinler@us.ibm.com>
diff --git a/Makefile.am b/Makefile.am
index f7f46a6..76e9c1b 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -3,4 +3,16 @@
 sbin_PROGRAMS = ibm-log-manager
 
 ibm_log_manager_SOURCES = \
-	main.cpp
+	main.cpp \
+	manager.cpp
+
+ibm_log_manager_CXX_FLAGS =  \
+	$(PHOSPHOR_DBUS_INTERFACES_CFLAGS) \
+	$(SDBUSPLUS_CFLAGS) \
+	$(PHOSPHOR_LOGGING_CFLAGS)
+
+ibm_log_manager_LDFLAGS = \
+	-lstdc++fs \
+	$(PHOSPHOR_DBUS_INTERFACES_LIBS) \
+	$(SDBUSPLUS_LIBS) \
+	$(PHOSPHOR_LOGGING_LIBS)
diff --git a/configure.ac b/configure.ac
index 04100d9..6c88d68 100644
--- a/configure.ac
+++ b/configure.ac
@@ -10,11 +10,23 @@
 AC_PROG_INSTALL
 AC_PROG_MAKE_SET
 
+PKG_CHECK_MODULES([PHOSPHOR_DBUS_INTERFACES], [phosphor-dbus-interfaces],,\
+    AC_MSG_ERROR(["Requires openbmc/phosphor-dbus-interfaces package."]))
+PKG_CHECK_MODULES([SDBUSPLUS], [sdbusplus],,
+    AC_MSG_ERROR(["Requires openbmc/sdbusplus package."]))
+PKG_CHECK_MODULES([PHOSPHOR_LOGGING], [phosphor-logging],,\
+    AC_MSG_ERROR(["Requires openbmc/phosphor-logging package."]))
+
 # Checks for typedefs, structures, and compiler characteristics.
 AX_CXX_COMPILE_STDCXX_14([noext])
 AX_APPEND_COMPILE_FLAGS([-Wall -Werror], [CXXFLAGS])
 
 LT_INIT
 
+AC_DEFINE(LOGGING_PATH, "/xyz/openbmc_project/logging",
+          [The xyz log manager DBus object path])
+AC_DEFINE(LOGGING_IFACE, "xyz.openbmc_project.Logging.Entry",
+          [The xyz log entry interface])
+
 AC_CONFIG_FILES([Makefile])
 AC_OUTPUT
diff --git a/dbus.hpp b/dbus.hpp
new file mode 100644
index 0000000..54dfd4a
--- /dev/null
+++ b/dbus.hpp
@@ -0,0 +1,25 @@
+#pragma once
+
+#include <map>
+#include <sdbusplus/server.hpp>
+#include <string>
+#include <vector>
+
+namespace ibm
+{
+namespace logging
+{
+
+using DbusInterface = std::string;
+using DbusProperty = std::string;
+using Value = sdbusplus::message::variant<bool, uint32_t, uint64_t,
+                                          std::string,
+                                          std::vector<std::string>>;
+
+using DbusPropertyMap = std::map<DbusProperty, Value>;
+using DbusInterfaceMap = std::map<DbusInterface, DbusPropertyMap>;
+using DbusInterfaceList = std::vector<DbusInterface>;
+
+}
+}
+
diff --git a/interfaces.hpp b/interfaces.hpp
new file mode 100644
index 0000000..a388f7e
--- /dev/null
+++ b/interfaces.hpp
@@ -0,0 +1,14 @@
+#pragma once
+
+namespace ibm
+{
+namespace logging
+{
+
+enum class InterfaceType
+{
+    POLICY
+};
+
+}
+}
diff --git a/manager.cpp b/manager.cpp
new file mode 100644
index 0000000..73d77ed
--- /dev/null
+++ b/manager.cpp
@@ -0,0 +1,93 @@
+/**
+ * Copyright © 2018 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "config.h"
+#include "manager.hpp"
+
+namespace ibm
+{
+namespace logging
+{
+
+Manager::Manager(sdbusplus::bus::bus& bus) :
+        bus(bus),
+        addMatch(
+                bus,
+                sdbusplus::bus::match::rules::interfacesAdded() +
+                sdbusplus::bus::match::rules::path_namespace(LOGGING_PATH),
+                std::bind(std::mem_fn(&Manager::interfaceAdded),
+                          this, std::placeholders::_1)),
+        removeMatch(
+                bus,
+                sdbusplus::bus::match::rules::interfacesRemoved() +
+                sdbusplus::bus::match::rules::path_namespace(LOGGING_PATH),
+                std::bind(std::mem_fn(&Manager::interfaceRemoved),
+                          this, std::placeholders::_1))
+{
+    //TODO: createAll();
+}
+
+void Manager::interfaceAdded(sdbusplus::message::message& msg)
+{
+    sdbusplus::message::object_path path;
+    DbusInterfaceMap interfaces;
+
+    msg.read(path, interfaces);
+
+    //Find the Logging.Entry interface with all of its properties
+    //to pass to create().
+    auto propertyMap = std::find_if(
+            interfaces.begin(),
+            interfaces.end(),
+            [](const auto& i)
+            {
+                return i.first == LOGGING_IFACE;
+            });
+
+    if (propertyMap != interfaces.end())
+    {
+        //TODO: create(path, propertyMap->second);
+    }
+}
+
+void Manager::interfaceRemoved(sdbusplus::message::message& msg)
+{
+    sdbusplus::message::object_path path;
+    DbusInterfaceList interfaces;
+
+    msg.read(path, interfaces);
+
+    //If the Logging.Entry interface was removed, then remove
+    //our object
+
+    auto i = std::find(
+            interfaces.begin(),
+            interfaces.end(),
+            LOGGING_IFACE);
+
+    if (i != interfaces.end())
+    {
+        auto id = getEntryID(path);
+
+        auto entry = entries.find(id);
+        if (entry != entries.end())
+        {
+            entries.erase(entry);
+        }
+    }
+}
+
+}
+}
diff --git a/manager.hpp b/manager.hpp
new file mode 100644
index 0000000..f779939
--- /dev/null
+++ b/manager.hpp
@@ -0,0 +1,105 @@
+#pragma once
+
+#include <experimental/any>
+#include <experimental/filesystem>
+#include <map>
+#include <sdbusplus/bus.hpp>
+#include "dbus.hpp"
+#include "interfaces.hpp"
+
+namespace ibm
+{
+namespace logging
+{
+
+
+/**
+ * @class Manager
+ *
+ * This class hosts IBM specific interfaces for the error logging
+ * entry objects.  It watches for interfaces added and removed
+ * signals to know when to create and delete objects.  Handling the
+ * xyz.openbmc_project.Logging service going away is done at the
+ * systemd service level where this app will be stopped too.
+ */
+class Manager
+{
+    public:
+
+        Manager() = delete;
+        ~Manager() = default;
+        Manager(const Manager&) = delete;
+        Manager& operator=(const Manager&) = delete;
+        Manager(Manager&&) = delete;
+        Manager& operator=(Manager&&) = delete;
+
+        /**
+         * Constructor
+         *
+         * @param[in] bus - the D-Bus bus object
+         */
+        explicit Manager(sdbusplus::bus::bus& bus);
+
+    private:
+
+        /**
+         * The callback for an interfaces added signal
+         *
+         * Creates the IBM interfaces for the log entry
+         * that was just created.
+         *
+         * @param[in] msg - the sdbusplus message
+         */
+        void interfaceAdded(sdbusplus::message::message& msg);
+
+        /**
+         * The callback for an interfaces removed signal
+         *
+         * Removes the IBM interfaces for the log entry
+         * that was just removed.
+         *
+         * @param[in] msg - the sdbusplus message
+         */
+        void interfaceRemoved(sdbusplus::message::message& msg);
+
+        /**
+         * Returns the entry ID for a log
+         *
+         * @param[in] objectPath - the object path of the log
+         *
+         * @return uint32_t - the ID
+         */
+        inline uint32_t getEntryID(const std::string& objectPath)
+        {
+            std::experimental::filesystem::path path(objectPath);
+            return std::stoul(path.filename());
+        }
+
+        /**
+         * The sdbusplus bus object
+         */
+        sdbusplus::bus::bus& bus;
+
+        /**
+         * The match object for interfacesAdded
+         */
+        sdbusplus::bus::match_t addMatch;
+
+        /**
+         * The match object for interfacesRemoved
+         */
+        sdbusplus::bus::match_t removeMatch;
+
+        using EntryID = uint32_t;
+        using InterfaceMap = std::map<InterfaceType, std::experimental::any>;
+        using EntryMap = std::map<EntryID, InterfaceMap>;
+
+        /**
+         * A map of the error log IDs to their IBM interface objects.
+         * There may be multiple interfaces per ID.
+         */
+        EntryMap entries;
+};
+
+}
+}