Code update flow: Set properties and load squashfs on object creation

Set the Activation properties during the code update flow and
do some checkings:
  Only activate an image that has a value of  Ready or Failed.
  Only validate an image if a corresponding object doesn't exist.
  Move the activating of the partitions from the RequestedActivation
to the Activation function to have it in a central location, and
set the Activation to Activating during that process.

Load the squashfs image when the Activation object is created
since the image can be deleted from the BMC and would not be
able to be activated when requested.

Change-Id: I7eba400ca29461e2f41a8d8889a9825572b98344
Signed-off-by: Adriana Kobylak <anoo@us.ibm.com>
diff --git a/activation.cpp b/activation.cpp
index 4344c7f..81aa5e7 100755
--- a/activation.cpp
+++ b/activation.cpp
@@ -17,6 +17,8 @@
 {
     if (value == softwareServer::Activation::Activations::Activating)
     {
+        softwareServer::Activation::activation(value);
+
         if (!activationBlocksTransition)
         {
             activationBlocksTransition =
@@ -25,33 +27,6 @@
                                 path);
         }
 
-        // Creating a mount point to access squashfs image.
-        constexpr auto squashfsMountService = "obmc-flash-bios-squashfsmount@";
-        auto squashfsMountServiceFile = std::string(squashfsMountService) +
-                                                    versionId + ".service";
-        auto method = bus.new_method_call(
-                SYSTEMD_BUSNAME,
-                SYSTEMD_PATH,
-                SYSTEMD_INTERFACE,
-                "StartUnit");
-        method.append(squashfsMountServiceFile,
-                      "replace");
-        bus.call_noreply(method);
-    }
-    else
-    {
-        activationBlocksTransition.reset(nullptr);
-    }
-    return softwareServer::Activation::activation(value);
-}
-
-auto Activation::requestedActivation(RequestedActivations value) ->
-        RequestedActivations
-{
-    if ((value == softwareServer::Activation::RequestedActivations::Active) &&
-        (softwareServer::Activation::requestedActivation() !=
-                  softwareServer::Activation::RequestedActivations::Active))
-    {
         constexpr auto ubimountService = "obmc-flash-bios-ubimount@";
         auto ubimountServiceFile = std::string(ubimountService) +
                                    versionId +
@@ -102,15 +77,39 @@
                 fs::create_directory_symlink(PNOR_PRSV, PNOR_PRSV_ACTIVE_PATH);
             }
 
-            Activation::activation(
+            return softwareServer::Activation::activation(
                     softwareServer::Activation::Activations::Active);
         }
         else
         {
-            Activation::activation(
+            return softwareServer::Activation::activation(
                     softwareServer::Activation::Activations::Failed);
         }
     }
+    else
+    {
+        activationBlocksTransition.reset(nullptr);
+        return softwareServer::Activation::activation(value);
+    }
+}
+
+auto Activation::requestedActivation(RequestedActivations value) ->
+        RequestedActivations
+{
+    if ((value == softwareServer::Activation::RequestedActivations::Active) &&
+        (softwareServer::Activation::requestedActivation() !=
+                  softwareServer::Activation::RequestedActivations::Active))
+    {
+        if ((softwareServer::Activation::activation() ==
+                    softwareServer::Activation::Activations::Ready) ||
+            (softwareServer::Activation::activation() ==
+                    softwareServer::Activation::Activations::Failed))
+        {
+            Activation::activation(
+                    softwareServer::Activation::Activations::Activating);
+
+        }
+    }
     return softwareServer::Activation::requestedActivation(value);
 }
 
diff --git a/item_updater.cpp b/item_updater.cpp
index 3f72aeb..9c26b9d 100755
--- a/item_updater.cpp
+++ b/item_updater.cpp
@@ -66,15 +66,32 @@
 
         auto versionId = resp.substr(pos + 1);
 
-        // Determine the Activation state by processing the given image dir.
-        auto activationState = server::Activation::Activations::Invalid;
-        if (ItemUpdater::validateSquashFSImage(versionId) == 0)
-        {
-            activationState = server::Activation::Activations::Ready;
-        }
-
         if (updater->activations.find(versionId) == updater->activations.end())
         {
+            // Determine the Activation state by processing the given image dir.
+            auto activationState = server::Activation::Activations::Invalid;
+            if (ItemUpdater::validateSquashFSImage(versionId) == 0)
+            {
+                activationState = server::Activation::Activations::Ready;
+
+                // Load the squashfs image to pnor so that it is available to be
+                // activated when requested.
+                // This is done since the image on the BMC can be removed.
+                constexpr auto squashfsMountService =
+                                    "obmc-flash-bios-squashfsmount@";
+                auto squashfsMountServiceFile =
+                                    std::string(squashfsMountService) +
+                                    versionId +
+                                    ".service";
+                auto method = updater->busItem.new_method_call(
+                                    SYSTEMD_BUSNAME,
+                                    SYSTEMD_PATH,
+                                    SYSTEMD_INTERFACE,
+                                    "StartUnit");
+                method.append(squashfsMountServiceFile, "replace");
+                updater->busItem.call_noreply(method);
+            }
+
             updater->activations.insert(std::make_pair(
                     versionId,
                     std::make_unique<Activation>(