manager: led priority is optional

The default priority was "Blink" but now the priority is optional.

This helps to implement group priority.
So if someone wants to use group priority they can do so, without the
default led priority getting in the way.

The led priority can still be assigned as "Blink" explicitly if needed.

Change-Id: I9336487bcc2edc0b3308ee76eb5330b8222b7f25
Signed-off-by: Alexander Hansen <alexander.hansen@9elements.com>
diff --git a/manager/json-parser.hpp b/manager/json-parser.hpp
index e92dfbc..e17568b 100644
--- a/manager/json-parser.hpp
+++ b/manager/json-parser.hpp
@@ -20,7 +20,8 @@
 // Priority for a particular LED needs to stay SAME across all groups
 // phosphor::led::Layout::Action can only be one of `Blink` and `On`
 using PriorityMap =
-    std::unordered_map<std::string, phosphor::led::Layout::Action>;
+    std::unordered_map<std::string,
+                       std::optional<phosphor::led::Layout::Action>>;
 
 /** @brief Parse LED JSON file and output Json object
  *
@@ -65,6 +66,25 @@
                              : phosphor::led::Layout::Action::On;
 }
 
+static std::string priorityToString(
+    const std::optional<phosphor::led::Layout::Action>& priority)
+{
+    if (!priority.has_value())
+    {
+        return "none";
+    }
+    switch (priority.value())
+    {
+        case phosphor::led::Layout::Action::Off:
+            return "Off";
+        case phosphor::led::Layout::Action::On:
+            return "On";
+        case phosphor::led::Layout::Action::Blink:
+            return "Blink";
+    }
+    return "?";
+}
+
 /** @brief Validate the Priority of an LED is same across ALL groups
  *
  *  @param[in] name - led name member of each group
@@ -73,9 +93,10 @@
  *
  *  @return
  */
-void validatePriority(const std::string& name,
-                      const phosphor::led::Layout::Action& priority,
-                      PriorityMap& priorityMap)
+void validatePriority(
+    const std::string& name,
+    const std::optional<phosphor::led::Layout::Action>& priority,
+    PriorityMap& priorityMap)
 {
     auto iter = priorityMap.find(name);
     if (iter == priorityMap.end())
@@ -88,8 +109,8 @@
     {
         lg2::error(
             "Priority of LED is not same across all, Name = {NAME}, Old Priority = {OLD_PRIO}, New Priority = {NEW_PRIO}",
-            "NAME", name, "OLD_PRIO", int(iter->second), "NEW_PRIO",
-            int(priority));
+            "NAME", name, "OLD_PRIO", priorityToString(iter->second),
+            "NEW_PRIO", priorityToString(priority));
 
         throw std::runtime_error(
             "Priority of at least one LED is not same across groups");
@@ -105,8 +126,13 @@
     uint8_t dutyOn = member.value("DutyOn", 50);
     uint16_t period = member.value("Period", 0);
 
-    // Since only have Blink/On and default priority is Blink
-    auto priority = getAction(member.value("Priority", "Blink"));
+    const std::string priorityStr = member.value("Priority", "");
+    std::optional<phosphor::led::Layout::Action> priority = std::nullopt;
+
+    if (!priorityStr.empty())
+    {
+        priority = getAction(priorityStr);
+    }
 
     // Same LEDs can be part of multiple groups. However, their
     // priorities across groups need to match.
diff --git a/manager/ledlayout.hpp b/manager/ledlayout.hpp
index c11500f..a609df6 100644
--- a/manager/ledlayout.hpp
+++ b/manager/ledlayout.hpp
@@ -27,7 +27,7 @@
     Action action;
     uint8_t dutyOn;
     uint16_t period;
-    Action priority;
+    std::optional<Action> priority;
 
     // Order LEDs such that same LEDs are grouped next to
     // each other and the same LEDs are in priority order
diff --git a/manager/manager.cpp b/manager/manager.cpp
index 18d257b..a5ac6b3 100644
--- a/manager/manager.cpp
+++ b/manager/manager.cpp
@@ -26,7 +26,9 @@
 
     auto currentAction = newState[action.name];
 
-    if (currentAction.action == action.priority)
+    const bool hasPriority = currentAction.priority.has_value();
+
+    if (hasPriority && currentAction.action == action.priority)
     {
         // if the current action is already the priority action,
         // we cannot override it
diff --git a/test/utest-led-json.cpp b/test/utest-led-json.cpp
index 36eb404..afd4505 100644
--- a/test/utest-led-json.cpp
+++ b/test/utest-led-json.cpp
@@ -26,7 +26,7 @@
         ASSERT_EQ(group.action, phosphor::led::Layout::Action::On);
         ASSERT_EQ(group.dutyOn, 50);
         ASSERT_EQ(group.period, 0);
-        ASSERT_EQ(group.priority, phosphor::led::Layout::Action::Blink);
+        ASSERT_EQ(group.priority, std::nullopt);
     }
 
     for (const auto& group : powerOnActions)
@@ -45,14 +45,14 @@
             ASSERT_EQ(group.action, phosphor::led::Layout::Action::Blink);
             ASSERT_EQ(group.dutyOn, 50);
             ASSERT_EQ(group.period, 1000);
-            ASSERT_EQ(group.priority, phosphor::led::Layout::Action::Blink);
+            ASSERT_EQ(group.priority, std::nullopt);
         }
         else if (group.name == "rear_id")
         {
             ASSERT_EQ(group.action, phosphor::led::Layout::Action::Blink);
             ASSERT_EQ(group.dutyOn, 50);
             ASSERT_EQ(group.period, 1000);
-            ASSERT_EQ(group.priority, phosphor::led::Layout::Action::Blink);
+            ASSERT_EQ(group.priority, std::nullopt);
         }
         else
         {