activation: Make activation flow common

The activation flow has a lot of duplicate code such as checking
the digital signatures and creating associations, with a few
differences depending on the choice of bmc layout configuration.

This makes the code hard to maintain since changes and additions
to the activation flow need to be done on more than one place, and
also the function becomes bigger and harder to follow. This would
be made worse when additional layout supports are added such as
eMMC code update support.

Make the flow common and move the specific code to its subdirectory.
This requires create a new function to handle the end of the
update so that we support implementations that are async (need to
wait for systemd service files to finish).

Still need an if/else statement to differentiate the implementations
that are synchronous like the static layout and the async ones like
the ubi layout since the async ones have the flash write function
return immediately and can call the flash write success later on,
for the sync ones need to set the activation value to Active after
calling flash write success:
Async (ubi):
  activation(Activating)
     flashWrite()
  return Activating
  onFlashWriteSuccess()
     activation(Active)

Synchronous (static):
  activation(Activating)
      flashWrite()
      onFlashWriteSuccess()
          activation(Active)
  return Active

By making the code common, the static layout gains some additional
features which may not be of consequence due to the short duration
of the update, but does not hurt and allow us to remove the if/else
blocks:
- Progress interface
- Blocks transition interface
- Updatable association before reboot is done

The static layout will also subscribe to systemd signals even
though they're not used, but again this is to keep the code as
common as possible.

Tested: Verified code update worked and expected d-bus interfaces
        and values were set during ubi and static update.

Change-Id: I20a0b752fe3905cca5b6220c88f65eb64d155d75
Signed-off-by: Adriana Kobylak <anoo@us.ibm.com>
diff --git a/activation.cpp b/activation.cpp
index f891877..291ce7c 100644
--- a/activation.cpp
+++ b/activation.cpp
@@ -111,97 +111,6 @@
         }
 #endif
 
-#ifdef UBIFS_LAYOUT
-        if (rwVolumeCreated == false && roVolumeCreated == false)
-        {
-            // Enable systemd signals
-            Activation::subscribeToSystemdSignals();
-
-            parent.freeSpace(*this);
-
-            if (!activationProgress)
-            {
-                activationProgress =
-                    std::make_unique<ActivationProgress>(bus, path);
-            }
-
-            if (!activationBlocksTransition)
-            {
-                activationBlocksTransition =
-                    std::make_unique<ActivationBlocksTransition>(bus, path);
-            }
-
-#ifdef WANT_SIGNATURE_VERIFY
-            fs::path uploadDir(IMG_UPLOAD_DIR);
-            if (!verifySignature(uploadDir / versionId, SIGNED_IMAGE_CONF_PATH))
-            {
-                onVerifyFailed();
-                // Stop the activation process, if fieldMode is enabled.
-                if (parent.control::FieldMode::fieldModeEnabled())
-                {
-                    // Cleanup
-                    activationBlocksTransition.reset(nullptr);
-                    activationProgress.reset(nullptr);
-                    return softwareServer::Activation::activation(
-                        softwareServer::Activation::Activations::Failed);
-                }
-            }
-#endif
-
-            flashWrite();
-
-            activationProgress->progress(10);
-        }
-        else if (rwVolumeCreated == true && roVolumeCreated == true)
-        {
-            if (ubootEnvVarsUpdated == false)
-            {
-                activationProgress->progress(90);
-
-                storePurpose(
-                    versionId,
-                    parent.versions.find(versionId)->second->purpose());
-                if (!redundancyPriority)
-                {
-                    redundancyPriority = std::make_unique<RedundancyPriority>(
-                        bus, path, *this, 0);
-                }
-            }
-            else
-            {
-                activationProgress->progress(100);
-
-                activationBlocksTransition.reset(nullptr);
-                activationProgress.reset(nullptr);
-
-                rwVolumeCreated = false;
-                roVolumeCreated = false;
-                ubootEnvVarsUpdated = false;
-                Activation::unsubscribeFromSystemdSignals();
-
-                // Remove version object from image manager
-                Activation::deleteImageManagerObject();
-
-                // Create active association
-                parent.createActiveAssociation(path);
-
-                // Create updateable association as this
-                // can be re-programmed.
-                parent.createUpdateableAssociation(path);
-
-                if (Activation::checkApplyTimeImmediate() == true)
-                {
-                    log<level::INFO>("Image Active. ApplyTime is immediate, "
-                                     "rebooting BMC.");
-                    Activation::rebootBmc();
-                }
-
-                return softwareServer::Activation::activation(
-                    softwareServer::Activation::Activations::Active);
-            }
-        }
-#else // !UBIFS_LAYOUT
-
 #ifdef WANT_SIGNATURE_VERIFY
         fs::path uploadDir(IMG_UPLOAD_DIR);
         if (!verifySignature(uploadDir / versionId, SIGNED_IMAGE_CONF_PATH))
@@ -215,35 +124,35 @@
             }
         }
 #endif
+
+        if (!activationProgress)
+        {
+            activationProgress =
+                std::make_unique<ActivationProgress>(bus, path);
+        }
+
+        if (!activationBlocksTransition)
+        {
+            activationBlocksTransition =
+                std::make_unique<ActivationBlocksTransition>(bus, path);
+        }
+
+        activationProgress->progress(10);
+
         parent.freeSpace(*this);
 
+        // Enable systemd signals
+        Activation::subscribeToSystemdSignals();
+
         flashWrite();
 
-        storePurpose(versionId,
-                     parent.versions.find(versionId)->second->purpose());
-        if (!redundancyPriority)
-        {
-            redundancyPriority =
-                std::make_unique<RedundancyPriority>(bus, path, *this, 0);
-        }
+#ifdef UBIFS_LAYOUT
 
-        // Remove version object from image manager
-        Activation::deleteImageManagerObject();
+        return softwareServer::Activation::activation(value);
 
-        // Create active association
-        parent.createActiveAssociation(path);
+#else // !UBIFS_LAYOUT
 
-        if (Activation::checkApplyTimeImmediate() == true)
-        {
-            log<level::INFO>("Image Active. ApplyTime is immediate, "
-                             "rebooting BMC.");
-            Activation::rebootBmc();
-        }
-        else
-        {
-            log<level::INFO>("BMC image ready, need reboot to get activated.");
-        }
-
+        onFlashWriteSuccess();
         return softwareServer::Activation::activation(
             softwareServer::Activation::Activations::Active);
 #endif
@@ -256,6 +165,50 @@
     return softwareServer::Activation::activation(value);
 }
 
+void Activation::onFlashWriteSuccess()
+{
+    activationProgress->progress(100);
+
+    activationBlocksTransition.reset(nullptr);
+    activationProgress.reset(nullptr);
+
+    rwVolumeCreated = false;
+    roVolumeCreated = false;
+    ubootEnvVarsUpdated = false;
+    Activation::unsubscribeFromSystemdSignals();
+
+    storePurpose(versionId, parent.versions.find(versionId)->second->purpose());
+
+    if (!redundancyPriority)
+    {
+        redundancyPriority =
+            std::make_unique<RedundancyPriority>(bus, path, *this, 0);
+    }
+
+    // Remove version object from image manager
+    Activation::deleteImageManagerObject();
+
+    // Create active association
+    parent.createActiveAssociation(path);
+
+    // Create updateable association as this
+    // can be re-programmed.
+    parent.createUpdateableAssociation(path);
+
+    if (Activation::checkApplyTimeImmediate() == true)
+    {
+        log<level::INFO>("Image Active. ApplyTime is immediate, "
+                         "rebooting BMC.");
+        Activation::rebootBmc();
+    }
+    else
+    {
+        log<level::INFO>("BMC image ready, need reboot to get activated.");
+    }
+
+    activation(softwareServer::Activation::Activations::Active);
+}
+
 void Activation::deleteImageManagerObject()
 {
     // Call the Delete object for <versionID> inside image_manager