regulators: Implements support for chassis

Implements support for parsing the chassis JSON elements in the
configuration file parser.

Signed-off-by: Bob King <Bob_King@wistron.com>
Change-Id: I26e7a3f118c9fc6e0e302be20bb3e0ea63f9e1fd
diff --git a/phosphor-regulators/src/config_file_parser.cpp b/phosphor-regulators/src/config_file_parser.cpp
index 1d64c96..99a2d15 100644
--- a/phosphor-regulators/src/config_file_parser.cpp
+++ b/phosphor-regulators/src/config_file_parser.cpp
@@ -179,18 +179,64 @@
     return actions;
 }
 
+std::unique_ptr<Chassis> parseChassis(const json& element)
+{
+    verifyIsObject(element);
+    unsigned int propertyCount{0};
+
+    // Optional comments property; value not stored
+    if (element.contains("comments"))
+    {
+        ++propertyCount;
+    }
+
+    // Required number property
+    const json& numberElement = getRequiredProperty(element, "number");
+    unsigned int number = parseUnsignedInteger(numberElement);
+    if (number < 1)
+    {
+        throw std::invalid_argument{"Invalid chassis number: Must be > 0"};
+    }
+    ++propertyCount;
+
+    // Optional devices property
+    std::vector<std::unique_ptr<Device>> devices{};
+    auto devicesIt = element.find("devices");
+    if (devicesIt != element.end())
+    {
+        devices = parseDeviceArray(*devicesIt);
+        ++propertyCount;
+    }
+
+    // Verify no invalid properties exist
+    verifyPropertyCount(element, propertyCount);
+
+    return std::make_unique<Chassis>(number, std::move(devices));
+}
+
 std::vector<std::unique_ptr<Chassis>> parseChassisArray(const json& element)
 {
     verifyIsArray(element);
     std::vector<std::unique_ptr<Chassis>> chassis;
-    // TODO: Not implemented yet
-    // for (auto& chassisElement : element)
-    // {
-    //     chassis.emplace_back(parseChassis(chassisElement));
-    // }
+    for (auto& chassisElement : element)
+    {
+        chassis.emplace_back(parseChassis(chassisElement));
+    }
     return chassis;
 }
 
+std::vector<std::unique_ptr<Device>> parseDeviceArray(const json& element)
+{
+    verifyIsArray(element);
+    std::vector<std::unique_ptr<Device>> devices;
+    // TODO: Not implemented yet
+    // for (auto& deviceElement : element)
+    // {
+    //     devices.emplace_back(parseDevice(deviceElement));
+    // }
+    return devices;
+}
+
 std::vector<uint8_t> parseHexByteArray(const json& element)
 {
     verifyIsArray(element);
diff --git a/phosphor-regulators/src/config_file_parser.hpp b/phosphor-regulators/src/config_file_parser.hpp
index 0a931d8..3ebab4b 100644
--- a/phosphor-regulators/src/config_file_parser.hpp
+++ b/phosphor-regulators/src/config_file_parser.hpp
@@ -17,6 +17,7 @@
 
 #include "action.hpp"
 #include "chassis.hpp"
+#include "device.hpp"
 #include "i2c_write_bit_action.hpp"
 #include "i2c_write_byte_action.hpp"
 #include "i2c_write_bytes_action.hpp"
@@ -172,6 +173,18 @@
 }
 
 /**
+ * Parses a JSON element containing a chassis.
+ *
+ * Returns the corresponding C++ Chassis object.
+ *
+ * Throws an exception if parsing fails.
+ *
+ * @param element JSON element
+ * @return Chassis object
+ */
+std::unique_ptr<Chassis> parseChassis(const nlohmann::json& element);
+
+/**
  * Parses a JSON element containing an array of chassis.
  *
  * Returns the corresponding C++ Chassis objects.
@@ -185,6 +198,19 @@
     parseChassisArray(const nlohmann::json& element);
 
 /**
+ * Parses a JSON element containing an array of devices.
+ *
+ * Returns the corresponding C++ Device objects.
+ *
+ * Throws an exception if parsing fails.
+ *
+ * @param element JSON element
+ * @return vector of Device objects
+ */
+std::vector<std::unique_ptr<Device>>
+    parseDeviceArray(const nlohmann::json& element);
+
+/**
  * Parses a JSON element containing a double (floating point number).
  *
  * Returns the corresponding C++ double value.
@@ -431,6 +457,26 @@
 }
 
 /**
+ * Parses a JSON element containing an unsigned integer.
+ *
+ * Returns the corresponding C++ unsigned int value.
+ *
+ * Throws an exception if parsing fails.
+ *
+ * @param element JSON element
+ * @return unsigned int value
+ */
+inline unsigned int parseUnsignedInteger(const nlohmann::json& element)
+{
+    // Verify element contains an unsigned integer
+    if (!element.is_number_unsigned())
+    {
+        throw std::invalid_argument{"Element is not an unsigned integer"};
+    }
+    return element.get<unsigned int>();
+}
+
+/**
  * Verifies that the specified JSON element is a JSON array.
  *
  * Throws an invalid_argument exception if the element is not an array.