Update Uboot environment variables when a version is deleted or updated

- We need to update the uboot env variables to point to the
  version that has the lowest priority when
  the version with lowest priority gets deleted or when
  the version with lowest priority gets a higher priority

Resolves openbmc/openbmc#2372

Change-Id: If43105c9ee2c3ada8aeb19939a428eb05e621ee9
Signed-off-by: Saqib Khan <khansa@us.ibm.com>
diff --git a/item_updater.cpp b/item_updater.cpp
index 1cafb03..e2de36f 100644
--- a/item_updater.cpp
+++ b/item_updater.cpp
@@ -332,6 +332,7 @@
     }
 
     this->activations.erase(entryId);
+    ItemUpdater::resetUbootEnvVars();
 }
 
 void ItemUpdater::deleteAll()
@@ -580,6 +581,46 @@
     return true;
 }
 
+void ItemUpdater::resetUbootEnvVars()
+{
+    decltype(activations.begin()->second->redundancyPriority.get()->priority())
+             lowestPriority = std::numeric_limits<uint8_t>::max();
+    decltype(activations.begin()->second->versionId) lowestPriorityVersion;
+    for (const auto& intf : activations)
+    {
+        if (!intf.second->redundancyPriority.get())
+        {
+            // Skip this version if the redundancyPriority is not initialized.
+            continue;
+        }
+
+        if (intf.second->redundancyPriority.get()->priority()
+            <= lowestPriority)
+        {
+            lowestPriority = intf.second->redundancyPriority.get()->priority();
+            lowestPriorityVersion = intf.second->versionId;
+        }
+    }
+
+    // TODO: openbmc/openbmc#2369 Add recovery policy to updateubootvars
+    //       unit template.
+    auto method = bus.new_method_call(
+            SYSTEMD_BUSNAME,
+            SYSTEMD_PATH,
+            SYSTEMD_INTERFACE,
+            "StartUnit");
+    auto updateEnvVarsFile = "obmc-flash-bmc-updateubootvars@" +
+                             lowestPriorityVersion + ".service";
+    method.append(updateEnvVarsFile, "replace");
+    auto result = bus.call(method);
+
+    //Check that the bus call didn't result in an error
+    if (result.is_method_error())
+    {
+        log<level::ERR>("Failed to update u-boot env variables");
+    }
+}
+
 } // namespace updater
 } // namespace software
 } // namespace phosphor