monitor:JSON: Parse fan monitoring conditions
Optional conditions can be used to include monitoring a fan or not based
on the results of the condition function configured. These condition
functions have their own required set of parameters, so parsing the JSON
for these conditions are done per supported condition function. This
adds the JSON parsing support for the current condition functions.
Tested:
`propertiesMatch` JSON parameters parsed into condition function
Any required parameters missing throws an exception
The `propertiesMatch` condition functions the same using JSON
Change-Id: I0f843951f4e83f2a5d44068820694010538788c1
Signed-off-by: Matthew Barth <msbarth@us.ibm.com>
diff --git a/monitor/conditions.cpp b/monitor/conditions.cpp
index 09f5852..19e9cd8 100644
--- a/monitor/conditions.cpp
+++ b/monitor/conditions.cpp
@@ -1,6 +1,10 @@
#include "conditions.hpp"
#include "sdbusplus.hpp"
+#include "types.hpp"
+
+#include <nlohmann/json.hpp>
+#include <phosphor-logging/log.hpp>
#include <algorithm>
@@ -13,6 +17,9 @@
namespace condition
{
+using json = nlohmann::json;
+using namespace phosphor::logging;
+
Condition propertiesMatch(std::vector<PropertyState>&& propStates)
{
return [pStates = std::move(propStates)](sdbusplus::bus::bus& bus) {
@@ -26,6 +33,58 @@
};
}
+Condition getPropertiesMatch(const json& condParams)
+{
+ if (!condParams.contains("properties"))
+ {
+ // Log error on missing required parameter
+ log<level::ERR>(
+ "Missing fan monitor condition properties",
+ entry("NAME=%s", condParams["name"].get<std::string>().c_str()));
+ throw std::runtime_error("Missing fan monitor condition properties");
+ }
+ std::vector<PropertyState> propStates;
+ for (auto& param : condParams["properties"])
+ {
+ if (!param.contains("object") || !param.contains("interface") ||
+ !param.contains("property"))
+ {
+ // Log error on missing required parameters
+ log<level::ERR>("Missing propertiesMatch condition parameters",
+ entry("REQUIRED_PARAMETERS=%s",
+ "{object, interface, property}"));
+ throw std::runtime_error(
+ "Missing propertiesMatch condition parameters");
+ }
+
+ auto propAttrs = param["property"];
+ if (!propAttrs.contains("name") || !propAttrs.contains("value"))
+ {
+ // Log error on missing required parameters
+ log<level::ERR>(
+ "Missing propertiesMatch condition property attributes",
+ entry("REQUIRED_ATTRIBUTES=%s", "{name, value}"));
+ throw std::runtime_error(
+ "Missing propertiesMatch condition property attributes");
+ }
+
+ std::string type = "";
+ if (propAttrs.contains("type"))
+ {
+ type = propAttrs["type"].get<std::string>();
+ }
+
+ // Add property for propertiesMatch condition
+ propStates.emplace_back(PropertyState(
+ {param["object"].get<std::string>(),
+ param["interface"].get<std::string>(),
+ propAttrs["name"].get<std::string>()},
+ JsonTypeHandler::getPropValue(propAttrs["value"], type)));
+ }
+
+ return make_condition(condition::propertiesMatch(std::move(propStates)));
+}
+
} // namespace condition
} // namespace monitor
} // namespace fan