Refactor: Make createActivation() common

The function is almost the same for ubi and static layout, except a few
differences that creates the ubi objects.

Add below pure virtual functions for ubi to create ubi specific objects
* createActivationObject()
* createVersionObject()

Then it is possible to move most of the code in createActivation() into
the commone one.

Tested: On the last commit of the patch series, run code update and
        factory reset on Witherspoon and all work fine.

Change-Id: Ieb3e783bc5b251529a55909f9e9f644230b274e7
Signed-off-by: Lei YU <mine260309@gmail.com>
diff --git a/ubi/item_updater_ubi.cpp b/ubi/item_updater_ubi.cpp
index f576b3c..875847f 100644
--- a/ubi/item_updater_ubi.cpp
+++ b/ubi/item_updater_ubi.cpp
@@ -35,110 +35,34 @@
 constexpr auto MBOXD_INTERFACE = "org.openbmc.mboxd";
 constexpr auto MBOXD_PATH = "/org/openbmc/mboxd";
 
-void ItemUpdaterUbi::createActivation(sdbusplus::message::message& m)
+std::unique_ptr<Activation> ItemUpdaterUbi::createActivationObject(
+    const std::string& path, const std::string& versionId,
+    const std::string& extVersion,
+    sdbusplus::xyz::openbmc_project::Software::server::Activation::Activations
+        activationStatus,
+    AssociationList& assocs)
 {
-    using SVersion = server::Version;
-    using VersionPurpose = SVersion::VersionPurpose;
-    namespace msg = sdbusplus::message;
-    namespace variant_ns = msg::variant_ns;
+    return std::make_unique<ActivationUbi>(
+        bus, path, *this, versionId, extVersion, activationStatus, assocs);
+}
 
-    sdbusplus::message::object_path objPath;
-    std::map<std::string, std::map<std::string, msg::variant<std::string>>>
-        interfaces;
-    m.read(objPath, interfaces);
+std::unique_ptr<Version> ItemUpdaterUbi::createVersionObject(
+    const std::string& objPath, const std::string& versionId,
+    const std::string& versionString,
+    sdbusplus::xyz::openbmc_project::Software::server::Version::VersionPurpose
+        versionPurpose,
+    const std::string& filePath)
+{
+    auto version = std::make_unique<Version>(
+        bus, objPath, *this, versionId, versionString, versionPurpose, filePath,
+        std::bind(&ItemUpdaterUbi::erase, this, std::placeholders::_1));
+    version->deleteObject = std::make_unique<Delete>(bus, objPath, *version);
+    return version;
+}
 
-    std::string path(std::move(objPath));
-    std::string filePath;
-    auto purpose = VersionPurpose::Unknown;
-    std::string version;
-
-    for (const auto& intf : interfaces)
-    {
-        if (intf.first == VERSION_IFACE)
-        {
-            for (const auto& property : intf.second)
-            {
-                if (property.first == "Purpose")
-                {
-                    // Only process the Host and System images
-                    auto value = SVersion::convertVersionPurposeFromString(
-                        variant_ns::get<std::string>(property.second));
-
-                    if (value == VersionPurpose::Host ||
-                        value == VersionPurpose::System)
-                    {
-                        purpose = value;
-                    }
-                }
-                else if (property.first == "Version")
-                {
-                    version = variant_ns::get<std::string>(property.second);
-                }
-            }
-        }
-        else if (intf.first == FILEPATH_IFACE)
-        {
-            for (const auto& property : intf.second)
-            {
-                if (property.first == "Path")
-                {
-                    filePath = variant_ns::get<std::string>(property.second);
-                }
-            }
-        }
-    }
-    if ((filePath.empty()) || (purpose == VersionPurpose::Unknown))
-    {
-        return;
-    }
-
-    // Version id is the last item in the path
-    auto pos = path.rfind("/");
-    if (pos == std::string::npos)
-    {
-        log<level::ERR>("No version id found in object path",
-                        entry("OBJPATH=%s", path.c_str()));
-        return;
-    }
-
-    auto versionId = path.substr(pos + 1);
-
-    if (activations.find(versionId) == activations.end())
-    {
-        // Determine the Activation state by processing the given image dir.
-        auto activationState = server::Activation::Activations::Invalid;
-        AssociationList associations = {};
-        if (validateSquashFSImage(filePath) == 0)
-        {
-            activationState = server::Activation::Activations::Ready;
-            // Create an association to the host inventory item
-            associations.emplace_back(std::make_tuple(
-                ACTIVATION_FWD_ASSOCIATION, ACTIVATION_REV_ASSOCIATION,
-                HOST_INVENTORY_PATH));
-        }
-
-        fs::path manifestPath(filePath);
-        manifestPath /= MANIFEST_FILE;
-        std::string extendedVersion =
-            (Version::getValue(
-                 manifestPath.string(),
-                 std::map<std::string, std::string>{{"extended_version", ""}}))
-                .begin()
-                ->second;
-
-        activations.insert(std::make_pair(
-            versionId, std::make_unique<ActivationUbi>(
-                           bus, path, *this, versionId, extendedVersion,
-                           activationState, associations)));
-
-        auto versionPtr = std::make_unique<Version>(
-            bus, path, *this, versionId, version, purpose, filePath,
-            std::bind(&ItemUpdaterUbi::erase, this, std::placeholders::_1));
-        versionPtr->deleteObject =
-            std::make_unique<Delete>(bus, path, *versionPtr);
-        versions.insert(std::make_pair(versionId, std::move(versionPtr)));
-    }
-    return;
+bool ItemUpdaterUbi::validateImage(const std::string& path)
+{
+    return validateSquashFSImage(path) == 0;
 }
 
 void ItemUpdaterUbi::processPNORImage()
diff --git a/ubi/item_updater_ubi.hpp b/ubi/item_updater_ubi.hpp
index ec9d05c..5de51d3 100644
--- a/ubi/item_updater_ubi.hpp
+++ b/ubi/item_updater_ubi.hpp
@@ -48,12 +48,22 @@
     static std::string determineId(const std::string& symlinkPath);
 
   private:
-    /** @brief Callback function for Software.Version match.
-     *  @details Creates an Activation D-Bus object.
-     *
-     * @param[in]  msg       - Data associated with subscribed signal
-     */
-    void createActivation(sdbusplus::message::message& msg) override;
+    std::unique_ptr<Activation> createActivationObject(
+        const std::string& path, const std::string& versionId,
+        const std::string& extVersion,
+        sdbusplus::xyz::openbmc_project::Software::server::Activation::
+            Activations activationStatus,
+        AssociationList& assocs) override;
+
+    std::unique_ptr<Version>
+        createVersionObject(const std::string& objPath,
+                            const std::string& versionId,
+                            const std::string& versionString,
+                            sdbusplus::xyz::openbmc_project::Software::server::
+                                Version::VersionPurpose versionPurpose,
+                            const std::string& filePath) override;
+
+    bool validateImage(const std::string& path) override;
 
     /** @brief Host factory reset - clears PNOR partitions for each
      * Activation D-Bus object */