Limit the number of Active pnor versions

- We need to limit the number of pnor versions because
  pnor chip has limited space. If we run out of space
  and the user tries another pnor code update then the
  update will fail.
- Currently we can only hold two images on pnor at this
  point. As we continue to decrease the size of pnor image
  we will increment the number of active pnor chips we can
  hold.
- This check will need to be removed once we have complete
  issue #1402 which will automatically monitor the flash
  usage and delete any image accordingly.

Resolves openbmc/openbmc#2155

Change-Id: Id62ba8554fba1ddb78286f70b0c3482a7beb8a6b
Signed-off-by: Saqib Khan <khansa@us.ibm.com>
diff --git a/item_updater.cpp b/item_updater.cpp
index c6726d3..dd313a8 100755
--- a/item_updater.cpp
+++ b/item_updater.cpp
@@ -355,6 +355,33 @@
     activations.erase(entryId);
 }
 
+// TODO: openbmc/openbmc#1402 Monitor flash usage
+void ItemUpdater::freeSpace()
+{
+    std::size_t count = 0;
+    decltype(activations.begin()->second->redundancyPriority.get()->priority())
+            highestPriority = 0;
+    decltype(activations.begin()->second->versionId) highestPriorityVersion;
+    for (const auto& iter : activations)
+    {
+        if (iter.second.get()->activation() == server::Activation::Activations::Active)
+        {
+            count++;
+            if (iter.second->redundancyPriority.get()->priority() > highestPriority)
+            {
+                highestPriority = iter.second->redundancyPriority.get()->priority();
+                highestPriorityVersion = iter.second->versionId;
+            }
+        }
+    }
+    // Remove the pnor version with highest priority since the PNOR
+    // can't hold more than 2 versions.
+    if (count >= ACTIVE_PNOR_MAX_ALLOWED)
+    {
+        erase(highestPriorityVersion);
+    }
+}
+
 } // namespace updater
 } // namespace software
 } // namespace openpower