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/config-validator.cpp b/manager/config-validator.cpp
new file mode 100644
index 0000000..05b0157
--- /dev/null
+++ b/manager/config-validator.cpp
@@ -0,0 +1,177 @@
+#include "config-validator.hpp"
+
+#include "grouplayout.hpp"
+#include "ledlayout.hpp"
+
+#include <phosphor-logging/lg2.hpp>
+#include <sdbusplus/bus.hpp>
+
+using namespace phosphor::led;
+
+namespace phosphor
+{
+namespace led
+{
+
+// Priority for a particular LED needs to stay SAME across all groups
+using PriorityMap =
+ std::unordered_map<std::string,
+ std::optional<phosphor::led::Layout::Action>>;
+
+static bool isUsingGroupPriority(const phosphor::led::GroupMap& ledMap)
+{
+ for (const auto& [_, group] : ledMap)
+ {
+ if (group.priority != 0)
+ {
+ return true;
+ }
+ }
+ return false;
+}
+
+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)
+ {
+ throw ConfigValidationException(
+ error::LedPriorityMismatch, "?", name,
+ "Priority of the LED is not same across groups. Old Priority = " +
+ priorityToString(iter->second) +
+ ", New Priority = " + priorityToString(priority));
+ }
+}
+
+static void validateConfigV1GroupForLedPriority(
+ const std::string groupName,
+ const phosphor::led::Layout::GroupLayout& group, PriorityMap& priorityMap)
+{
+ if (group.priority != 0)
+ {
+ throw ConfigValidationException(
+ error::MixedLedAndGroupPriority, groupName,
+ "Cannot mix group priority and led priority");
+ }
+
+ for (const auto& ledAction : group.actionSet)
+ {
+ if (ledAction.priority == std::nullopt)
+ {
+ throw ConfigValidationException(error::MissingLedPriority,
+ groupName, ledAction.name,
+ "Need valid led priority");
+ }
+
+ // Same LEDs can be part of multiple groups. However, their
+ // priorities across groups need to match.
+ validatePriority(ledAction.name, ledAction.priority, priorityMap);
+ }
+}
+
+static void
+ validateConfigV1ForLedPriority(const phosphor::led::GroupMap& ledMap)
+{
+ PriorityMap priorityMap{};
+ for (const auto& [groupName, group] : ledMap)
+ {
+ validateConfigV1GroupForLedPriority(groupName, group, priorityMap);
+ }
+}
+
+static void validateConfigV1GroupForGroupPriority(
+ const std::string groupName,
+ const phosphor::led::Layout::GroupLayout& group)
+{
+ for (const auto& led : group.actionSet)
+ {
+ if (led.priority != std::nullopt)
+ {
+ throw ConfigValidationException(
+ error::MixedLedAndGroupPriority, groupName, led.name,
+ "Cannot mix group priority and led priority for LED");
+ }
+ }
+
+ if (group.priority == 0)
+ {
+ // group priority 0 is for internal use
+ throw ConfigValidationException(error::InvalidGroupPriority, groupName,
+ "Group Priority cannot be 0");
+ }
+}
+
+static void
+ validateConfigV1ForGroupPriority(const phosphor::led::GroupMap& ledMap)
+{
+ std::set<int> groupPriorities;
+ for (const auto& [_, group] : ledMap)
+ {
+ groupPriorities.insert(group.priority);
+ }
+
+ if (groupPriorities.size() != ledMap.size())
+ {
+ throw ConfigValidationException(
+ error::DuplicateGroupPriority,
+ "When using Group Priority, no 2 Groups may have the same priority");
+ }
+
+ for (const auto& [groupName, group] : ledMap)
+ {
+ validateConfigV1GroupForGroupPriority(groupName, group);
+ }
+}
+
+void validateConfigV1(const GroupMap& ledMap)
+{
+ const bool useGroupPriority = isUsingGroupPriority(ledMap);
+
+ if (useGroupPriority)
+ {
+ validateConfigV1ForGroupPriority(ledMap);
+ }
+ else
+ {
+ validateConfigV1ForLedPriority(ledMap);
+ }
+}
+
+} // namespace led
+} // namespace phosphor
diff --git a/manager/config-validator.hpp b/manager/config-validator.hpp
new file mode 100644
index 0000000..5d9ff5f
--- /dev/null
+++ b/manager/config-validator.hpp
@@ -0,0 +1,68 @@
+#include "grouplayout.hpp"
+#include "ledlayout.hpp"
+
+#include <phosphor-logging/lg2.hpp>
+#include <sdbusplus/bus.hpp>
+
+namespace phosphor
+{
+namespace led
+{
+namespace error
+{
+enum ConfigValidationError
+{
+ // An LED has different priorities assigned to it in different groups
+ LedPriorityMismatch,
+
+ // LED priority was needed but not assigned
+ MissingLedPriority,
+
+ // Mixup of the 2 configuration options
+ MixedLedAndGroupPriority,
+
+ // An invalid group priority was assigned
+ InvalidGroupPriority,
+
+ // Group priorities were not unique
+ DuplicateGroupPriority,
+};
+}
+
+class ConfigValidationException : std::runtime_error
+{
+ public:
+ error::ConfigValidationError reason;
+
+ ConfigValidationException(const error::ConfigValidationError& err,
+ const std::string& msg) :
+ std::runtime_error(msg), reason(err)
+ {
+ lg2::error(msg.c_str());
+ }
+
+ ConfigValidationException(const error::ConfigValidationError& err,
+ const std::string& groupName,
+ const std::string& msg) :
+ std::runtime_error(msg), reason(err)
+ {
+ lg2::error("Configuration Validation Error in Group {GROUP}: {MSG}",
+ "GROUP", groupName, "MSG", msg.c_str());
+ }
+
+ ConfigValidationException(const error::ConfigValidationError& err,
+ const std::string& groupName,
+ const std::string& ledName,
+ const std::string& msg) :
+ std::runtime_error(msg), reason(err)
+ {
+ lg2::error(
+ "Configuration Validation Error in Group {GROUP}, Led {LED}: {MSG}",
+ "GROUP", groupName, "LED", ledName, "MSG", msg.c_str());
+ }
+};
+
+void validateConfigV1(const phosphor::led::GroupMap& ledMap);
+
+} // namespace led
+} // namespace phosphor
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;
diff --git a/manager/led-main.cpp b/manager/led-main.cpp
index a18c793..6d1ef82 100644
--- a/manager/led-main.cpp
+++ b/manager/led-main.cpp
@@ -42,6 +42,8 @@
auto systemLedMap = getSystemLedMap(configFile);
#endif
+ phosphor::led::validateConfigV1(systemLedMap);
+
/** @brief Group manager object */
phosphor::led::Manager manager(bus, systemLedMap, event);
diff --git a/manager/meson.build b/manager/meson.build
index e2452bd..0c97649 100644
--- a/manager/meson.build
+++ b/manager/meson.build
@@ -4,6 +4,7 @@
'manager.cpp',
'serialize.cpp',
'../utils.cpp',
+ 'config-validator.cpp',
]
if get_option('use-json').disabled()