summaryrefslogtreecommitdiffstats
path: root/entity_map_json.cpp
diff options
context:
space:
mode:
authorPatrick Venture <venture@google.com>2019-08-16 10:50:18 -0700
committerPatrick Venture <venture@google.com>2019-08-20 08:55:01 -0700
commit02e32376817529e960bb918417bb6a55d646a3a3 (patch)
treed3624e105490d5f51dd1df6d762a07d8e1b1a918 /entity_map_json.cpp
parent51d0c40ad6c9181a172fb1fc14aa8d39b66e7ba7 (diff)
downloadphosphor-host-ipmid-02e32376817529e960bb918417bb6a55d646a3a3.tar.gz
phosphor-host-ipmid-02e32376817529e960bb918417bb6a55d646a3a3.zip
entitymap: add json parsing support
Add a json parsing method that given an entity map in the format: [ { "id" : 1, "containerEntityId" : 2, "containerEntityInstance" : 3, "isList" : false, "isLinked" : false, "entities" : [ {"id" : 1, "instance" : 2}, {"id" : 1, "instance" : 3}, {"id" : 1, "instance" : 4}, {"id" : 1, "instance" : 5} ] } ] is constructed into the entity map used by sensorhandler. This is meant as part of the transition from the entity map in YAML to JSON. This is step 2. Step 1 moved access to the object behind a method. This adds JSON validation and parsing. Step 3 will add a file path to check and parse. Step 4 will provide a call to parse that file if present and use its data if non-empty. Tested: The method added has not been tested beyond unit-test validation. Signed-off-by: Patrick Venture <venture@google.com> Change-Id: Ic29f022d3812fa9e3af775d542ad055629fd5a01
Diffstat (limited to 'entity_map_json.cpp')
-rw-r--r--entity_map_json.cpp74
1 files changed, 74 insertions, 0 deletions
diff --git a/entity_map_json.cpp b/entity_map_json.cpp
new file mode 100644
index 0000000..db45b05
--- /dev/null
+++ b/entity_map_json.cpp
@@ -0,0 +1,74 @@
+#include "entity_map_json.hpp"
+
+#include <exception>
+#include <ipmid/types.hpp>
+#include <nlohmann/json.hpp>
+#include <string>
+#include <utility>
+
+namespace ipmi
+{
+namespace sensor
+{
+
+EntityInfoMap buildJsonEntityMap(const nlohmann::json& data)
+{
+ EntityInfoMap builtMap;
+
+ if (data.type() != nlohmann::json::value_t::array)
+ {
+ return builtMap;
+ }
+
+ try
+ {
+ for (const auto& entry : data)
+ {
+ /* It's an array entry with the following fields: id,
+ * containerEntityId, containerEntityInstance, isList, isLinked,
+ * entities[4]
+ */
+ EntityInfo obj;
+ Id recordId = entry.at("id").get<Id>();
+ obj.containerEntityId =
+ entry.at("containerEntityId").get<uint8_t>();
+ obj.containerEntityInstance =
+ entry.at("containerEntityInstance").get<uint8_t>();
+ obj.isList = entry.at("isList").get<bool>();
+ obj.isLinked = entry.at("isLinked").get<bool>();
+
+ auto jsonEntities = entry.at("entities");
+
+ if (jsonEntities.type() != nlohmann::json::value_t::array)
+ {
+ throw std::runtime_error(
+ "Invalid type for entities entry, must be array");
+ }
+ if (jsonEntities.size() != obj.containedEntities.size())
+ {
+ throw std::runtime_error(
+ "Entities must be in pairs of " +
+ std::to_string(obj.containedEntities.size()));
+ }
+
+ for (std::size_t i = 0; i < obj.containedEntities.size(); i++)
+ {
+ obj.containedEntities[i] = std::make_pair(
+ jsonEntities[i].at("id").get<uint8_t>(),
+ jsonEntities[i].at("instance").get<uint8_t>());
+ }
+
+ builtMap.insert({recordId, obj});
+ }
+ }
+ catch (const std::exception& e)
+ {
+ /* If any entry is invalid, the entire file cannot be trusted. */
+ builtMap.clear();
+ }
+
+ return builtMap;
+}
+
+} // namespace sensor
+} // namespace ipmi
OpenPOWER on IntegriCloud