item_updater: Add version dbus object.

Need to create the Version object under itemUpdater
class to retain the version of the active images
after the bmc is rebooted and the image_dir no
longer holds the image files.

Change-Id: Iac78d577b970c6fa766b94041742f77077b14e62
Signed-off-by: Saqib Khan <khansa@us.ibm.com>
diff --git a/item_updater.cpp b/item_updater.cpp
index fa1c38d..41e4196 100755
--- a/item_updater.cpp
+++ b/item_updater.cpp
@@ -30,8 +30,9 @@
                       sdbusplus::message::variant<std::string>>> interfaces;
     m.read(objPath, interfaces);
     std::string path(std::move(objPath));
-
     std::string filePath;
+    auto purpose = server::Version::VersionPurpose::Unknown;
+    std::string version;
 
     for (const auto& intf : interfaces)
     {
@@ -42,16 +43,21 @@
                 if (property.first == "Purpose")
                 {
                     // Only process the Host and System images
-                    std::string value = sdbusplus::message::variant_ns::get<
-                            std::string>(property.second);
-                    if ((value != convertForMessage(
-                            server::Version::VersionPurpose::Host)) &&
-                        (value != convertForMessage(
-                            server::Version::VersionPurpose::System)))
+                    std::string str = sdbusplus::message::variant_ns::get<
+                        std::string>(property.second);
+                    auto value = server::Version::
+                        convertVersionPurposeFromString(str);
+                    if (value == server::Version::VersionPurpose::Host ||
+                        value == server::Version::VersionPurpose::System)
                     {
-                        return;
+                        purpose = value;
                     }
                 }
+                else if (property.first == "Version")
+                {
+                    version = sdbusplus::message::variant_ns::
+                        get<std::string>(property.second);
+                }
             }
         }
         else if (intf.first == FILEPATH_IFACE)
@@ -66,7 +72,8 @@
             }
         }
     }
-    if (filePath.empty())
+    if ((filePath.empty()) || (purpose == server::Version::
+            VersionPurpose::Unknown))
     {
         return;
     }
@@ -120,6 +127,14 @@
                         extendedVersion,
                         activationState)));
     }
+        versions.insert(std::make_pair(
+                            versionId,
+                            std::make_unique<Version>(
+                                bus,
+                                path,
+                                version,
+                                purpose,
+                                filePath)));
     return;
 }
 
diff --git a/item_updater.hpp b/item_updater.hpp
index 82df3e7..233f022 100755
--- a/item_updater.hpp
+++ b/item_updater.hpp
@@ -3,6 +3,7 @@
 #include <sdbusplus/server.hpp>
 #include "activation.hpp"
 #include <xyz/openbmc_project/Common/FactoryReset/server.hpp>
+#include "version.hpp"
 
 namespace openpower
 {
@@ -73,6 +74,10 @@
           * 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<Version>> versions;
+
         /** @brief sdbusplus signal match for Software.Version */
         sdbusplus::bus::match_t versionMatch;
 
diff --git a/version.hpp b/version.hpp
new file mode 100644
index 0000000..573255c
--- /dev/null
+++ b/version.hpp
@@ -0,0 +1,53 @@
+#pragma once
+
+#include <sdbusplus/bus.hpp>
+#include "xyz/openbmc_project/Software/Version/server.hpp"
+#include "xyz/openbmc_project/Common/FilePath/server.hpp"
+
+namespace openpower
+{
+namespace software
+{
+namespace updater
+{
+
+using VersionInherit = sdbusplus::server::object::object<
+    sdbusplus::xyz::openbmc_project::Software::server::Version,
+    sdbusplus::xyz::openbmc_project::Common::server::FilePath>;
+
+/** @class Version
+ *  @brief OpenBMC version software management implementation.
+ *  @details A concrete implementation for xyz.openbmc_project.Software.Version
+ *  DBus API.
+ */
+class Version : public VersionInherit
+{
+    public:
+        /** @brief Constructs Version Software Manager.
+         *
+         * @param[in] bus            - The Dbus bus object
+         * @param[in] objPath        - The Dbus object path
+         * @param[in] versionId      - The version identifier
+         * @param[in] versionPurpose - The version purpose
+         * @param[in] filePath       - The image filesystem path
+         */
+        Version(sdbusplus::bus::bus& bus,
+                const std::string& objPath,
+                const std::string& versionId,
+                VersionPurpose versionPurpose,
+                const std::string& filePath) : VersionInherit(
+                    bus, (objPath).c_str(), true)
+        {
+            // Set properties.
+            purpose(versionPurpose);
+            version(versionId);
+            path(filePath);
+
+            // Emit deferred signal.
+            emit_object_added();
+        }
+};
+
+} // namespace updater
+} // namespace software
+} // namespace openpower