config: error on invalid configuration
Since the default led priority is no longer 'Blink', the priority now
has to be explicitly defined when using either group priority or led
priority.
If a configuration does not define the priority, the configuration is
invalid and in the yaml case, phosphor-led-manager should fail to build,
in the json case, the process should exit due to the configuration
error.
The config validation has been extracted into it's own file and made
separate from json config parsing.
So every config will go through the same validation even if its been
created via yaml.
Change-Id: Ifda65942b0768d6c0d3b25076f7a1236b46b3d9f
Signed-off-by: Alexander Hansen <alexander.hansen@9elements.com>
diff --git a/manager/json-parser.hpp b/manager/json-parser.hpp
index 8f2832b..30a06a4 100644
--- a/manager/json-parser.hpp
+++ b/manager/json-parser.hpp
@@ -1,5 +1,6 @@
#include "config.h"
+#include "config-validator.hpp"
#include "grouplayout.hpp"
#include "json-config.hpp"
#include "ledlayout.hpp"
@@ -76,59 +77,7 @@
return phosphor::led::Layout::Action::Blink;
}
-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
- * @param[in] priority - member priority of each group
- * @param[out] priorityMap - std::unordered_map, key:name, value:priority
- *
- * @return
- */
-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())
- {
- priorityMap.emplace(name, priority);
- return;
- }
-
- if (iter->second != priority)
- {
- lg2::error(
- "Priority of LED is not same across all, Name = {NAME}, Old Priority = {OLD_PRIO}, New Priority = {NEW_PRIO}",
- "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");
- }
-}
-
static void loadJsonConfigV1GroupMember(const Json& member,
- PriorityMap& priorityMap,
phosphor::led::ActionSet& ledActions)
{
auto name = member.value("Name", "");
@@ -144,18 +93,13 @@
priority = getAction(priorityStr);
}
- // Same LEDs can be part of multiple groups. However, their
- // priorities across groups need to match.
- validatePriority(name, priority, priorityMap);
-
phosphor::led::Layout::LedAction ledAction{name, action, dutyOn, period,
priority};
ledActions.emplace(ledAction);
}
static void loadJsonConfigV1Group(const Json& entry,
- phosphor::led::GroupMap& ledMap,
- PriorityMap& priorityMap)
+ phosphor::led::GroupMap& ledMap)
{
const Json empty{};
@@ -166,15 +110,16 @@
tmpPath /= groupName;
auto objpath = tmpPath.string();
auto members = entry.value("members", empty);
- int priority = entry.value("Priority", 0);
lg2::debug("config for '{GROUP}'", "GROUP", groupName);
+ int priority = entry.value("Priority", 0);
+
phosphor::led::ActionSet ledActions{};
phosphor::led::Layout::GroupLayout groupLayout{};
for (const auto& member : members)
{
- loadJsonConfigV1GroupMember(member, priorityMap, ledActions);
+ loadJsonConfigV1GroupMember(member, ledActions);
}
// Generated an std::unordered_map of LedGroupNames to std::set of LEDs
@@ -192,7 +137,6 @@
const phosphor::led::GroupMap loadJsonConfigV1(const Json& json)
{
phosphor::led::GroupMap ledMap{};
- PriorityMap priorityMap{};
// define the default JSON as empty
const Json empty{};
@@ -200,7 +144,7 @@
for (const auto& entry : leds)
{
- loadJsonConfigV1Group(entry, ledMap, priorityMap);
+ loadJsonConfigV1Group(entry, ledMap);
}
return ledMap;