Move Active BMC version object to item_updater.

- There needs to be only one version class for both
  BMC and PNOR images. Thus removing the BMCVERSION
  class and moving the functionality to MANAGER class.

Change-Id: I6dda818960588cfd9ed345fe93068531efc7a877
Signed-off-by: Saqib Khan <khansa@us.ibm.com>
diff --git a/Makefile.am b/Makefile.am
index 261cd6b..e652205 100755
--- a/Makefile.am
+++ b/Makefile.am
@@ -2,7 +2,6 @@
 
 # Build these headers, don't install them
 noinst_HEADERS = \
-	bmc_version.hpp \
 	download_manager.hpp \
 	watch.hpp \
 	version.hpp \
@@ -16,7 +15,6 @@
 	phosphor-image-updater
 
 phosphor_version_software_manager_SOURCES = \
-	bmc_version.cpp \
 	image_manager_main.cpp \
 	watch.cpp \
 	version.cpp \
diff --git a/bmc_version.cpp b/bmc_version.cpp
deleted file mode 100644
index 463b2c4..0000000
--- a/bmc_version.cpp
+++ /dev/null
@@ -1,57 +0,0 @@
-#include <iostream>
-#include <string>
-#include <sstream>
-#include <fstream>
-#include <stdexcept>
-#include "bmc_version.hpp"
-
-namespace phosphor
-{
-namespace software
-{
-namespace manager
-{
-
-const std::string BMCVersion::getVersion() const
-{
-    // Get version from /etc/os-release.
-    std::string versionKey = "VERSION_ID=";
-    std::string version{};
-    std::ifstream efile;
-    std::string line;
-    efile.open("/etc/os-release");
-
-    while (getline(efile, line))
-    {
-        if (line.substr(0, versionKey.size()).find(versionKey)
-            != std::string::npos)
-        {
-            // This line looks like VERSION_ID="v1.99.0-353-ga3b8a0a-dirty".
-            // So grab everything in quotes.
-            std::size_t pos = line.find_first_of('"') + 1;
-            version = line.substr(pos, line.find_last_of('"') - pos);
-            break;
-        }
-    }
-    efile.close();
-    return version;
-}
-
-const std::string BMCVersion::getId() const
-{
-    auto version = getVersion();
-    std::stringstream hexId;
-
-    if (version.empty())
-    {
-        throw std::runtime_error("Software version is empty");
-    }
-
-    // Only want 8 hex digits.
-    hexId << std::hex << ((std::hash<std::string> {}(version)) & 0xFFFFFFFF);
-    return hexId.str();
-}
-
-} // namespace manager
-} // namespace software
-} // namepsace phosphor
diff --git a/bmc_version.hpp b/bmc_version.hpp
deleted file mode 100644
index 6ac785b..0000000
--- a/bmc_version.hpp
+++ /dev/null
@@ -1,66 +0,0 @@
-#pragma once
-
-#include <sdbusplus/bus.hpp>
-#include "xyz/openbmc_project/Software/Version/server.hpp"
-
-namespace phosphor
-{
-namespace software
-{
-namespace manager
-{
-
-using BMCVersionInherit = sdbusplus::server::object::object<
-    sdbusplus::xyz::openbmc_project::Software::server::Version>;
-
-/** @class BMCVersion
- *  @brief OpenBMC version software management implementation
- *         for the active BMC software image.
- *  @details A concrete implementation for xyz.openbmc_project.Software.Version
- *           DBus API.
- */
-class BMCVersion : public BMCVersionInherit
-{
-    public:
-        /** @brief Constructs BMC Version Software Manager for
-         *         the active BMC software image.
-         *
-         * @note This constructor passes 'true' to the base class in order to
-         *       defer dbus object registration until we can
-         *       set our properties
-         *
-         * @param[in] bus        - The Dbus bus object
-         * @param[in] objPath    - The Dbus object path
-         */
-        BMCVersion(sdbusplus::bus::bus& bus,
-                   const char* objPath) : BMCVersionInherit(
-                       bus, (std::string{objPath} + '/' + getId()).c_str(),
-                       true)
-        {
-            // Set properties.
-            purpose(VersionPurpose::BMC);
-            version(getVersion());
-
-            // Emit deferred signal.
-            emit_object_added();
-        }
-
-    private:
-        /**
-         * @brief Get the code version identifier.
-         *
-         * @return The version identifier.
-         **/
-        const std::string getVersion() const;
-
-        /**
-         * @brief Get the Software Version id.
-         *
-         * @return The id.
-         **/
-        const std::string getId() const;
-};
-
-} // namespace manager
-} // namespace software
-} // namespace phosphor
diff --git a/image_manager.cpp b/image_manager.cpp
index d4dff1b..605050c 100644
--- a/image_manager.cpp
+++ b/image_manager.cpp
@@ -130,7 +130,6 @@
 
     std::transform(purposeString.begin(), purposeString.end(),
                    purposeString.begin(), ::tolower);
-
     auto purpose = Version::VersionPurpose::Unknown;
     if (purposeString.compare("bmc") == 0)
     {
diff --git a/image_manager.hpp b/image_manager.hpp
index ac44539..6e45c7c 100644
--- a/image_manager.hpp
+++ b/image_manager.hpp
@@ -20,7 +20,7 @@
          *
          * @param[in] bus - The Dbus bus object
          */
-        Manager(sdbusplus::bus::bus& bus) : bus(bus) {};
+        Manager(sdbusplus::bus::bus& bus) : bus(bus){};
 
         /**
          * @brief Verify and untar the tarball. Verify the manifest file.
@@ -48,7 +48,6 @@
          */
         static int unTar(const std::string& tarballFilePath,
                          const std::string& extractDirPath);
-
 };
 
 } // namespace manager
diff --git a/image_manager_main.cpp b/image_manager_main.cpp
index d0cac65..3aeefbe 100644
--- a/image_manager_main.cpp
+++ b/image_manager_main.cpp
@@ -3,7 +3,6 @@
 #include <sdbusplus/bus.hpp>
 #include <phosphor-logging/log.hpp>
 #include "config.h"
-#include "bmc_version.hpp"
 #include "watch.hpp"
 #include "image_manager.hpp"
 
@@ -16,8 +15,6 @@
 
     sdbusplus::server::manager::manager objManager(bus,
             SOFTWARE_OBJPATH);
-    phosphor::software::manager::BMCVersion manager(bus,
-            SOFTWARE_OBJPATH);
     bus.request_name(VERSION_BUSNAME);
 
     try
diff --git a/item_updater.cpp b/item_updater.cpp
index 9e909d9..efff65f 100644
--- a/item_updater.cpp
+++ b/item_updater.cpp
@@ -106,6 +106,31 @@
     return;
 }
 
+void ItemUpdater::processBMCImage()
+{
+    auto purpose = server::Version::VersionPurpose::BMC;
+    auto version = phosphor::software::manager::Version::getBMCVersion();
+    auto id = phosphor::software::manager::Version::getId(version);
+    auto path =  std::string{SOFTWARE_OBJPATH} + '/' + id;
+    activations.insert(std::make_pair(
+                           id,
+                           std::make_unique<Activation>(
+                               bus,
+                               path,
+                               id,
+                               server::Activation::Activations::Active)));
+    versions.insert(std::make_pair(
+                        id,
+                        std::make_unique<phosphor::software::
+                             manager::Version>(
+                             bus,
+                             path,
+                             version,
+                             purpose,
+                             "")));
+    return;
+}
+
 ItemUpdater::ActivationStatus ItemUpdater::validateSquashFSImage(
              const std::string& versionId)
 {
diff --git a/item_updater.hpp b/item_updater.hpp
index 705b320..082dbf7 100644
--- a/item_updater.hpp
+++ b/item_updater.hpp
@@ -51,8 +51,14 @@
                                     this,
                                     std::placeholders::_1))
         {
+            processBMCImage();
         };
 
+    /**
+     * @brief Create and populate the active BMC Version.
+     */
+    void processBMCImage();
+
     private:
         /** @brief Callback function for Software.Version match.
          *  @details Creates an Activation dbus object.
diff --git a/test/Makefile.am b/test/Makefile.am
index 187aedf..ab98534 100755
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -8,8 +8,10 @@
 
 # Build/add utest to test suite
 utest_CPPFLAGS = -Igtest $(GTEST_CPPFLAGS) $(AM_CPPFLAGS)
-utest_CXXFLAGS = $(PTHREAD_CFLAGS) $(PHOSPHOR_LOGGING_CFLAGS)
-utest_LDFLAGS = -lgtest_main -lgtest $(PTHREAD_LIBS) $(OESDK_TESTCASE_FLAGS) \
+utest_CXXFLAGS = $(PTHREAD_CFLAGS) $(PHOSPHOR_LOGGING_CFLAGS) \
+	$(PHOSPHOR_DBUS_INTERFACES_CFLAGS)
+utest_LDFLAGS = -lgtest_main -lgtest $(PTHREAD_LIBS) \
+	$(PHOSPHOR_DBUS_INTERFACES_LIBS) $(OESDK_TESTCASE_FLAGS) \
 	$(PHOSPHOR_LOGGING_LIBS) -lstdc++fs
 
 utest_SOURCES = utest.cpp
diff --git a/version.cpp b/version.cpp
index 4260a89..24f201c 100644
--- a/version.cpp
+++ b/version.cpp
@@ -72,6 +72,35 @@
     return hexId.str();
 }
 
+std::string Version::getBMCVersion()
+{
+    std::string versionKey = "VERSION_ID=";
+    std::string version{};
+    std::ifstream efile;
+    std::string line;
+    efile.open("/etc/os-release");
+
+    while (getline(efile, line))
+    {
+        if (line.substr(0, versionKey.size()).find(versionKey) !=
+            std::string::npos)
+        {
+            std::size_t pos = line.find_first_of('"') + 1;
+            version = line.substr(pos, line.find_last_of('"') - pos);
+            break;
+        }
+    }
+    efile.close();
+
+    if (version.empty())
+    {
+        log<level::ERR>("Error BMC current version is empty");
+        throw std::runtime_error("BMC current version is empty");
+    }
+
+    return version;
+}
+
 } // namespace manager
 } // namespace software
 } // namepsace phosphor
diff --git a/version.hpp b/version.hpp
index 75289a3..eca6670 100755
--- a/version.hpp
+++ b/version.hpp
@@ -62,6 +62,12 @@
          **/
         static std::string getId(const std::string& version);
 
+        /**
+         * @brief Get the active bmc version identifier.
+         *
+         * @return The version identifier.
+         */
+        static std::string getBMCVersion();
 };
 
 } // namespace manager