item_updater: Add version dbus object.

Implementing the Version object under item_updater helps
to retain the version of the active images after the bmc
is rebooted and the image_dir no longer holds version file.

Change-Id: I55349bbc62b212b4e034239f4f6743129c8b1070
Signed-off-by: Saqib Khan <khansa@us.ibm.com>
diff --git a/Makefile.am b/Makefile.am
index 7d65c7a..261cd6b 100755
--- a/Makefile.am
+++ b/Makefile.am
@@ -37,6 +37,7 @@
 
 phosphor_image_updater_SOURCES = \
 	activation.cpp \
+	version.cpp \
 	item_updater.cpp \
 	item_updater_main.cpp
 
diff --git a/item_updater.cpp b/item_updater.cpp
index bc382e8..9e909d9 100644
--- a/item_updater.cpp
+++ b/item_updater.cpp
@@ -5,6 +5,7 @@
 #include "item_updater.hpp"
 #include "xyz/openbmc_project/Software/Version/server.hpp"
 #include <experimental/filesystem>
+#include "version.hpp"
 
 namespace phosphor
 {
@@ -24,6 +25,8 @@
 void ItemUpdater::createActivation(sdbusplus::message::message& msg)
 {
     sdbusplus::message::object_path objPath;
+    auto purpose = server::Version::VersionPurpose::Unknown;
+    std::string version;
     std::map<std::string,
              std::map<std::string,
                       sdbusplus::message::variant<std::string>>> interfaces;
@@ -32,28 +35,31 @@
 
     for (const auto& intf : interfaces)
     {
-        if (intf.first.compare(VERSION_IFACE))
+        if (intf.first == VERSION_IFACE)
         {
-            continue;
-        }
-
-        for (const auto& property : intf.second)
-        {
-            if (!property.first.compare("Purpose"))
+            for (const auto& property : intf.second)
             {
-                // Only process the BMC images
-                auto value = sdbusplus::message::variant_ns::get<std::string>(
-                        property.second);
-                if (value !=
-                    convertForMessage(server::Version::VersionPurpose::BMC) &&
-                    value !=
-                    convertForMessage(server::Version::VersionPurpose::System))
+                if (property.first == "Purpose")
                 {
-                    return;
+                    std::string str = sdbusplus::message::variant_ns::
+                        get<std::string>(property.second);
+                    purpose = server::Version::
+                        convertVersionPurposeFromString(str);
+                }
+                else if (property.first == "Version")
+                {
+                    version = sdbusplus::message::variant_ns::
+                        get<std::string>(property.second);
                 }
             }
         }
     }
+    if (version.empty() ||
+        (purpose != server::Version::VersionPurpose::BMC &&
+        purpose != server::Version::VersionPurpose::System))
+    {
+        return;
+    }
 
     // Version id is the last item in the path
     auto pos = path.rfind("/");
@@ -81,12 +87,21 @@
             activationState = server::Activation::Activations::Active;
         }
         activations.insert(std::make_pair(
+                               versionId,
+                               std::make_unique<Activation>(
+                                        bus,
+                                        path,
                                         versionId,
-                                        std::make_unique<Activation>(
-                                            bus,
-                                            path,
-                                            versionId,
-                                            activationState)));
+                                        activationState)));
+        versions.insert(std::make_pair(
+                            versionId,
+                            std::make_unique<phosphor::software::
+                                manager::Version>(
+                                bus,
+                                path,
+                                version,
+                                purpose,
+                                "")));
     }
     return;
 }
diff --git a/item_updater.hpp b/item_updater.hpp
index 14737fb..705b320 100644
--- a/item_updater.hpp
+++ b/item_updater.hpp
@@ -2,6 +2,7 @@
 
 #include <sdbusplus/server.hpp>
 #include "activation.hpp"
+#include "version.hpp"
 
 namespace phosphor
 {
@@ -79,6 +80,11 @@
           * version id */
         std::map<std::string, std::unique_ptr<Activation>> activations;
 
+        /** @brief Persistent map of Version dbus objects and their
+          * version id */
+        std::map<std::string, std::unique_ptr<phosphor::software::
+            manager::Version>> versions;
+
         /** @brief sdbusplus signal match for Software.Version */
         sdbusplus::bus::match_t versionMatch;