BMC: Implement Software RedundancyPriority.interface

- Create Redundancy Priority interface after a successful
  PNOR update and set the priority.
- Remove Redundancy Priority interface once the activation
  state changes from being Active.
- Create override function for RedundancyPriority.

Resolves openbmc/openbmc#1755

Change-Id: I160dc4a6cad243d58759c71bbeb218aab841cf7a
Signed-off-by: Saqib Khan <khansa@us.ibm.com>
diff --git a/activation.cpp b/activation.cpp
index 3f8795c..9f9a16e 100644
--- a/activation.cpp
+++ b/activation.cpp
@@ -1,4 +1,5 @@
 #include "activation.hpp"
+#include "item_updater.hpp"
 
 namespace phosphor
 {
@@ -12,6 +13,12 @@
 auto Activation::activation(Activations value) ->
         Activations
 {
+
+    if (value != softwareServer::Activation::Activations::Active)
+    {
+        redundancyPriority.reset(nullptr);
+    }
+
     if (value == softwareServer::Activation::Activations::Activating)
     {
         if (!activationBlocksTransition)
@@ -21,6 +28,21 @@
                                 bus,
                                 path);
         }
+
+        //TODO openbmc/openbmc#1756 - Add Logic for Code Update
+        if (!redundancyPriority)
+        {
+            redundancyPriority =
+                      std::make_unique<RedundancyPriority>(
+                                bus,
+                                path,
+                                *this,
+                                0);
+        }
+
+        activationBlocksTransition.reset(nullptr);
+        return softwareServer::Activation::activation(
+                softwareServer::Activation::Activations::Active);
     }
     else
     {
@@ -49,6 +71,12 @@
     return softwareServer::Activation::requestedActivation(value);
 }
 
+uint8_t RedundancyPriority::priority(uint8_t value)
+{
+    parent.parent.freePriority(value);
+    return softwareServer::RedundancyPriority::priority(value);
+}
+
 } // namespace updater
 } // namespace software
 } // namespace phosphor
diff --git a/activation.hpp b/activation.hpp
index 09183f1..1807134 100644
--- a/activation.hpp
+++ b/activation.hpp
@@ -3,6 +3,7 @@
 #include <sdbusplus/server.hpp>
 #include <xyz/openbmc_project/Software/Activation/server.hpp>
 #include <xyz/openbmc_project/Software/ActivationBlocksTransition/server.hpp>
+#include "xyz/openbmc_project/Software/RedundancyPriority/server.hpp"
 
 namespace phosphor
 {
@@ -15,6 +16,59 @@
     sdbusplus::xyz::openbmc_project::Software::server::Activation>;
 using ActivationBlocksTransitionInherit = sdbusplus::server::object::object<
  sdbusplus::xyz::openbmc_project::Software::server::ActivationBlocksTransition>;
+using RedundancyPriorityInherit = sdbusplus::server::object::object<
+    sdbusplus::xyz::openbmc_project::Software::server::RedundancyPriority>;
+
+class ItemUpdater;
+class Activation;
+class RedundancyPriority;
+
+/** @class RedundancyPriority
+ *  @brief OpenBMC RedundancyPriority implementation
+ *  @details A concrete implementation for
+ *  xyz.openbmc_project.Software.RedundancyPriority DBus API.
+ */
+class RedundancyPriority : public RedundancyPriorityInherit
+{
+    public:
+        /** @brief Constructs RedundancyPriority.
+         *
+         *  @param[in] bus    - The Dbus bus object
+         *  @param[in] path   - The Dbus object path
+         *  @param[in] parent - Parent object.
+         *  @param[in] value  - The redundancyPriority value
+         */
+        RedundancyPriority(sdbusplus::bus::bus& bus,
+                                   const std::string& path,
+                                   Activation& parent,
+                                   uint8_t value) :
+                                   RedundancyPriorityInherit(bus,
+                                   path.c_str(), true),
+                                   parent(parent)
+        {
+            // Set Property
+            priority(value);
+            // Emit deferred signal.
+            emit_object_added();
+        }
+
+        /** @brief Overloaded Priority property set function
+         *
+         *  @param[in] value - uint8_t
+         *
+         *  @return Success or exception thrown
+         */
+        uint8_t priority(uint8_t value) override;
+
+        /** @brief Priority property get function
+         *
+         *  @returns uint8_t - The Priority value
+         */
+        using RedundancyPriorityInherit::priority;
+
+        /** @brief Parent Object. */
+        Activation& parent;
+};
 
 /** @class ActivationBlocksTransition
  *  @brief OpenBMC ActivationBlocksTransition implementation.
@@ -46,16 +100,19 @@
          *
          * @param[in] bus    - The Dbus bus object
          * @param[in] path   - The Dbus object path
+         * @param[in] parent - Parent object.
          * @param[in] versionId  - The software version id
          * @param[in] activationStatus - The status of Activation
          */
         Activation(sdbusplus::bus::bus& bus, const std::string& path,
+                   ItemUpdater& parent,
                    std::string& versionId,
                    sdbusplus::xyz::openbmc_project::Software::
                    server::Activation::Activations activationStatus) :
                    ActivationInherit(bus, path.c_str(), true),
                    bus(bus),
                    path(path),
+                   parent(parent),
                    versionId(versionId)
         {
             // Set Properties.
@@ -87,11 +144,17 @@
         /** @brief Persistent DBus object path */
         std::string path;
 
+        /** @brief Parent Object. */
+        ItemUpdater& parent;
+
         /** @brief Version id */
         std::string versionId;
 
         /** @brief Persistent ActivationBlocksTransition dbus object */
         std::unique_ptr<ActivationBlocksTransition> activationBlocksTransition;
+
+        /** @brief Persistent RedundancyPriority dbus object */
+        std::unique_ptr<RedundancyPriority> redundancyPriority;
 };
 
 } // namespace updater
diff --git a/item_updater.cpp b/item_updater.cpp
index 9c94b19..667c4b6 100644
--- a/item_updater.cpp
+++ b/item_updater.cpp
@@ -106,6 +106,7 @@
                                std::make_unique<Activation>(
                                         bus,
                                         path,
+                                        *this,
                                         versionId,
                                         activationState)));
         versions.insert(std::make_pair(
@@ -132,6 +133,7 @@
                            std::make_unique<Activation>(
                                bus,
                                path,
+                               *this,
                                id,
                                server::Activation::Activations::Active)));
     versions.insert(std::make_pair(
@@ -165,6 +167,21 @@
     }
 }
 
+void ItemUpdater::freePriority(uint8_t value)
+{
+    //TODO openbmc/openbmc#1896 Improve the performance of this function
+    for (const auto& intf : activations)
+    {
+        if(intf.second->redundancyPriority)
+        {
+            if (intf.second->redundancyPriority.get()->priority() == value)
+            {
+                intf.second->redundancyPriority.get()->priority(value+1);
+            }
+        }
+    }
+}
+
 } // namespace updater
 } // namespace software
 } // namespace phosphor
diff --git a/item_updater.hpp b/item_updater.hpp
index fb8e9df..75b2a68 100644
--- a/item_updater.hpp
+++ b/item_updater.hpp
@@ -54,6 +54,15 @@
             processBMCImage();
         };
 
+    /** @brief Sets the given priority free by incrementing
+     *  any existing priority with the same value by 1
+     *
+     *  @param[in] value - The priority that needs to be set free.
+     *
+     *  @return None
+     */
+    void freePriority(uint8_t value);
+
     /**
      * @brief Create and populate the active BMC Version.
      */