Create Version and Activation objects for current PNOR image

On BMC boot, create the Software version and activation that
points to the current PNOR version by reading:
/var/lib/phosphor-software-manager/pnor/ro

Resolves openbmc/openbmc#1762

Change-Id: I46ddb37491a1a230699da9f3acbd3fcff23dc538
Signed-off-by: Saqib Khan <khansa@us.ibm.com>
diff --git a/item_updater.cpp b/item_updater.cpp
index 3edfb0c..7a56775 100755
--- a/item_updater.cpp
+++ b/item_updater.cpp
@@ -3,6 +3,7 @@
 #include <fstream>
 #include <phosphor-logging/log.hpp>
 #include <xyz/openbmc_project/Software/Version/server.hpp>
+#include "version.hpp"
 #include "config.h"
 #include "item_updater.hpp"
 #include "activation.hpp"
@@ -101,7 +102,9 @@
 
         fs::path manifestPath(filePath);
         manifestPath /= MANIFEST_FILE;
-        auto extendedVersion = ItemUpdater::getExtendedVersion(manifestPath);
+        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<Activation>(
@@ -123,43 +126,44 @@
     return;
 }
 
-std::string ItemUpdater::getExtendedVersion(const std::string& manifestFilePath)
+void ItemUpdater::processPNORImage()
 {
-    constexpr auto extendedVersionKey = "extended_version=";
-    constexpr auto extendedVersionKeySize = strlen(extendedVersionKey);
 
-    if (manifestFilePath.empty())
+    fs::path pnorTOC(PNOR_RO_ACTIVE_PATH);
+    pnorTOC /= PNOR_TOC_FILE;
+    std::ifstream efile(pnorTOC.c_str());
+    if (efile.good() != 1)
     {
-        log<level::ERR>("Error MANIFESTFilePath is empty");
-        throw std::runtime_error("MANIFESTFilePath is empty");
+        log<level::INFO>("Error PNOR current version is empty");
+        return;
     }
-
-    std::string extendedVersion{};
-    std::ifstream efile;
-    std::string line;
-    efile.exceptions(std::ifstream::failbit
-                     | std::ifstream::badbit
-                     | std::ifstream::eofbit);
-
-    try
-    {
-        efile.open(manifestFilePath);
-        while (getline(efile, line))
-        {
-            if (line.compare(0, extendedVersionKeySize,
-                             extendedVersionKey) == 0)
-            {
-                extendedVersion = line.substr(extendedVersionKeySize);
-                break;
-            }
-        }
-        efile.close();
-    }
-    catch (const std::exception& e)
-    {
-        log<level::ERR>("Error in reading Host MANIFEST file");
-    }
-    return extendedVersion;
+    auto keyValues = Version::getValue(pnorTOC.string(),
+        std::map<std::string, std::string> {{"version", ""},
+        {"extended_version", ""}});
+    std::string version = keyValues.at("version");
+    std::string extendedVersion = keyValues.at("extended_version");
+    auto id = Version::getId(version);
+    auto purpose = server::Version::VersionPurpose::Host;
+    auto path =  std::string{SOFTWARE_OBJPATH} + '/' + id;
+    auto activationState = server::Activation::Activations::Active;
+    activations.insert(std::make_pair(
+                           id,
+                           std::make_unique<Activation>(
+                               bus,
+                               path,
+                               *this,
+                               id,
+                               extendedVersion,
+                               activationState)));
+    versions.insert(std::make_pair(
+                        id,
+                        std::make_unique<Version>(
+                             bus,
+                             path,
+                             version,
+                             purpose,
+                             "")));
+    return;
 }
 
 int ItemUpdater::validateSquashFSImage(const std::string& filePath)