regulators: Build IDMap for system

The IDMap class is used to map string IDs to the corresponding C++
Device, Rail, and Rule objects.

The IDMap class is complete and tested, but it was not previously being
populated except in testcases.

Add new methods to the System, Chassis, and Device classes to populate
the IDMap with all the ID -> object mappings in the system.

Signed-off-by: Shawn McCarney <shawnmm@us.ibm.com>
Change-Id: I80f39b663b011ca643c91f7281ff50c956631331
diff --git a/phosphor-regulators/src/chassis.cpp b/phosphor-regulators/src/chassis.cpp
new file mode 100644
index 0000000..3787365
--- /dev/null
+++ b/phosphor-regulators/src/chassis.cpp
@@ -0,0 +1,31 @@
+/**
+ * Copyright © 2020 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "chassis.hpp"
+
+namespace phosphor::power::regulators
+{
+
+void Chassis::addToIDMap(IDMap& idMap)
+{
+    // Add devices and their rails to the map
+    for (std::unique_ptr<Device>& device : devices)
+    {
+        device->addToIDMap(idMap);
+    }
+}
+
+} // namespace phosphor::power::regulators
diff --git a/phosphor-regulators/src/chassis.hpp b/phosphor-regulators/src/chassis.hpp
index d9eec81..1fe74cb 100644
--- a/phosphor-regulators/src/chassis.hpp
+++ b/phosphor-regulators/src/chassis.hpp
@@ -16,6 +16,7 @@
 #pragma once
 
 #include "device.hpp"
+#include "id_map.hpp"
 
 #include <memory>
 #include <stdexcept>
@@ -74,6 +75,13 @@
     }
 
     /**
+     * Adds the Device and Rail objects in this chassis to the specified IDMap.
+     *
+     * @param idMap mapping from IDs to the associated Device/Rail/Rule objects
+     */
+    void addToIDMap(IDMap& idMap);
+
+    /**
      * Returns the devices within this chassis, if any.
      *
      * The vector contains regulator devices and any related devices
diff --git a/phosphor-regulators/src/device.cpp b/phosphor-regulators/src/device.cpp
new file mode 100644
index 0000000..3e7d5a4
--- /dev/null
+++ b/phosphor-regulators/src/device.cpp
@@ -0,0 +1,34 @@
+/**
+ * Copyright © 2020 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "device.hpp"
+
+namespace phosphor::power::regulators
+{
+
+void Device::addToIDMap(IDMap& idMap)
+{
+    // Add this device to the map
+    idMap.addDevice(*this);
+
+    // Add rails to the map
+    for (std::unique_ptr<Rail>& rail : rails)
+    {
+        idMap.addRail(*rail);
+    }
+}
+
+} // namespace phosphor::power::regulators
diff --git a/phosphor-regulators/src/device.hpp b/phosphor-regulators/src/device.hpp
index f7ad179..919899a 100644
--- a/phosphor-regulators/src/device.hpp
+++ b/phosphor-regulators/src/device.hpp
@@ -17,6 +17,7 @@
 
 #include "configuration.hpp"
 #include "i2c_interface.hpp"
+#include "id_map.hpp"
 #include "presence_detection.hpp"
 #include "rail.hpp"
 
@@ -72,6 +73,15 @@
     }
 
     /**
+     * Adds this Device object to the specified IDMap.
+     *
+     * Also adds any Rail objects in this Device to the IDMap.
+     *
+     * @param idMap mapping from IDs to the associated Device/Rail/Rule objects
+     */
+    void addToIDMap(IDMap& idMap);
+
+    /**
      * Returns the configuration changes to apply to this device, if any.
      *
      * @return Pointer to Configuration object.  Will equal nullptr if no
diff --git a/phosphor-regulators/src/meson.build b/phosphor-regulators/src/meson.build
index 3caa543..aa8921c 100644
--- a/phosphor-regulators/src/meson.build
+++ b/phosphor-regulators/src/meson.build
@@ -4,9 +4,12 @@
 )
 
 phosphor_regulators_library_source_files = [
+    'chassis.cpp',
     'config_file_parser.cpp',
+    'device.cpp',
     'id_map.cpp',
     'pmbus_utils.cpp',
+    'system.cpp',
 
     'actions/if_action.cpp',
     'actions/i2c_compare_bit_action.cpp',
diff --git a/phosphor-regulators/src/system.cpp b/phosphor-regulators/src/system.cpp
new file mode 100644
index 0000000..edaa1ed
--- /dev/null
+++ b/phosphor-regulators/src/system.cpp
@@ -0,0 +1,37 @@
+/**
+ * Copyright © 2020 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "system.hpp"
+
+namespace phosphor::power::regulators
+{
+
+void System::buildIDMap()
+{
+    // Add rules to the map
+    for (std::unique_ptr<Rule>& rule : rules)
+    {
+        idMap.addRule(*rule);
+    }
+
+    // Add devices and rails in each chassis to the map
+    for (std::unique_ptr<Chassis>& oneChassis : chassis)
+    {
+        oneChassis->addToIDMap(idMap);
+    }
+}
+
+} // namespace phosphor::power::regulators
diff --git a/phosphor-regulators/src/system.hpp b/phosphor-regulators/src/system.hpp
index 3256c8d..b0346ac 100644
--- a/phosphor-regulators/src/system.hpp
+++ b/phosphor-regulators/src/system.hpp
@@ -56,6 +56,7 @@
         rules{std::move(rules)},
         chassis{std::move(chassis)}
     {
+        buildIDMap();
     }
 
     /**
@@ -93,6 +94,13 @@
 
   private:
     /**
+     * Builds the IDMap for the system.
+     *
+     * Adds the Device, Rail, and Rule objects in the system to the map.
+     */
+    void buildIDMap();
+
+    /**
      * Rules used to monitor and control regulators in the system.
      */
     std::vector<std::unique_ptr<Rule>> rules{};
diff --git a/phosphor-regulators/test/chassis_tests.cpp b/phosphor-regulators/test/chassis_tests.cpp
index 707fff5..0f66d24 100644
--- a/phosphor-regulators/test/chassis_tests.cpp
+++ b/phosphor-regulators/test/chassis_tests.cpp
@@ -16,6 +16,7 @@
 #include "chassis.hpp"
 #include "device.hpp"
 #include "i2c_interface.hpp"
+#include "id_map.hpp"
 #include "test_utils.hpp"
 
 #include <memory>
@@ -41,12 +42,8 @@
     {
         // Create vector of Device objects
         std::vector<std::unique_ptr<Device>> devices{};
-        devices.push_back(std::make_unique<Device>(
-            "vdd_reg1", true, "/system/chassis/motherboard/reg1",
-            std::move(createI2CInterface())));
-        devices.push_back(std::make_unique<Device>(
-            "vdd_reg2", true, "/system/chassis/motherboard/reg2",
-            std::move(createI2CInterface())));
+        devices.emplace_back(createDevice("vdd_reg1"));
+        devices.emplace_back(createDevice("vdd_reg2"));
 
         // Create Chassis
         Chassis chassis{1, std::move(devices)};
@@ -70,6 +67,34 @@
     }
 }
 
+TEST(ChassisTests, AddToIDMap)
+{
+    // Create vector of Device objects
+    std::vector<std::unique_ptr<Device>> devices{};
+    devices.emplace_back(createDevice("reg1", {"rail1"}));
+    devices.emplace_back(createDevice("reg2", {"rail2a", "rail2b"}));
+    devices.emplace_back(createDevice("reg3"));
+
+    // Create Chassis
+    Chassis chassis{1, std::move(devices)};
+
+    // Add Device and Rail objects within the Chassis to an IDMap
+    IDMap idMap{};
+    chassis.addToIDMap(idMap);
+
+    // Verify all Devices are in the IDMap
+    EXPECT_NO_THROW(idMap.getDevice("reg1"));
+    EXPECT_NO_THROW(idMap.getDevice("reg2"));
+    EXPECT_NO_THROW(idMap.getDevice("reg3"));
+    EXPECT_THROW(idMap.getDevice("reg4"), std::invalid_argument);
+
+    // Verify all Rails are in the IDMap
+    EXPECT_NO_THROW(idMap.getRail("rail1"));
+    EXPECT_NO_THROW(idMap.getRail("rail2a"));
+    EXPECT_NO_THROW(idMap.getRail("rail2b"));
+    EXPECT_THROW(idMap.getRail("rail3"), std::invalid_argument);
+}
+
 TEST(ChassisTests, GetDevices)
 {
     // Test where no devices were specified in constructor
@@ -82,12 +107,8 @@
     {
         // Create vector of Device objects
         std::vector<std::unique_ptr<Device>> devices{};
-        devices.push_back(std::make_unique<Device>(
-            "vdd_reg1", true, "/system/chassis/motherboard/reg1",
-            std::move(createI2CInterface())));
-        devices.push_back(std::make_unique<Device>(
-            "vdd_reg2", true, "/system/chassis/motherboard/reg2",
-            std::move(createI2CInterface())));
+        devices.emplace_back(createDevice("vdd_reg1"));
+        devices.emplace_back(createDevice("vdd_reg2"));
 
         // Create Chassis
         Chassis chassis{1, std::move(devices)};
diff --git a/phosphor-regulators/test/device_tests.cpp b/phosphor-regulators/test/device_tests.cpp
index 2ae4d6f..c4875bf 100644
--- a/phosphor-regulators/test/device_tests.cpp
+++ b/phosphor-regulators/test/device_tests.cpp
@@ -17,6 +17,7 @@
 #include "configuration.hpp"
 #include "device.hpp"
 #include "i2c_interface.hpp"
+#include "id_map.hpp"
 #include "mock_action.hpp"
 #include "presence_detection.hpp"
 #include "rail.hpp"
@@ -95,6 +96,39 @@
     }
 }
 
+TEST(DeviceTests, AddToIDMap)
+{
+    std::unique_ptr<PresenceDetection> presenceDetection{};
+    std::unique_ptr<Configuration> configuration{};
+
+    // Create vector of Rail objects
+    std::vector<std::unique_ptr<Rail>> rails{};
+    rails.push_back(std::make_unique<Rail>("vdd0"));
+    rails.push_back(std::make_unique<Rail>("vdd1"));
+
+    // Create Device
+    Device device{"vdd_reg",
+                  false,
+                  "/system/chassis/motherboard/reg2",
+                  std::move(createI2CInterface()),
+                  std::move(presenceDetection),
+                  std::move(configuration),
+                  std::move(rails)};
+
+    // Add Device and Rail objects to an IDMap
+    IDMap idMap{};
+    device.addToIDMap(idMap);
+
+    // Verify Device is in the IDMap
+    EXPECT_NO_THROW(idMap.getDevice("vdd_reg"));
+    EXPECT_THROW(idMap.getDevice("vio_reg"), std::invalid_argument);
+
+    // Verify all Rails are in the IDMap
+    EXPECT_NO_THROW(idMap.getRail("vdd0"));
+    EXPECT_NO_THROW(idMap.getRail("vdd1"));
+    EXPECT_THROW(idMap.getRail("vdd2"), std::invalid_argument);
+}
+
 TEST(DeviceTests, GetConfiguration)
 {
     // Test where Configuration was not specified in constructor
diff --git a/phosphor-regulators/test/system_tests.cpp b/phosphor-regulators/test/system_tests.cpp
index b5f33f1..0e5b8db 100644
--- a/phosphor-regulators/test/system_tests.cpp
+++ b/phosphor-regulators/test/system_tests.cpp
@@ -13,38 +13,44 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-#include "action.hpp"
 #include "chassis.hpp"
-#include "mock_action.hpp"
+#include "device.hpp"
+#include "id_map.hpp"
+#include "rail.hpp"
 #include "rule.hpp"
 #include "system.hpp"
+#include "test_utils.hpp"
 
 #include <memory>
+#include <stdexcept>
 #include <utility>
 #include <vector>
 
 #include <gtest/gtest.h>
 
 using namespace phosphor::power::regulators;
+using namespace phosphor::power::regulators::test_utils;
 
 TEST(SystemTests, Constructor)
 {
     // Create Rules
     std::vector<std::unique_ptr<Rule>> rules{};
-    std::vector<std::unique_ptr<Action>> actions{};
-    actions.emplace_back(std::make_unique<MockAction>());
-    rules.emplace_back(
-        std::make_unique<Rule>("set_voltage_rule", std::move(actions)));
+    rules.emplace_back(createRule("set_voltage_rule"));
 
     // Create Chassis
     std::vector<std::unique_ptr<Chassis>> chassis{};
-    chassis.emplace_back(std::make_unique<Chassis>(1));
+    std::vector<std::unique_ptr<Device>> devices{};
+    devices.emplace_back(createDevice("reg1", {"rail1"}));
+    chassis.emplace_back(std::make_unique<Chassis>(1, std::move(devices)));
 
     // Create System
     System system{std::move(rules), std::move(chassis)};
     EXPECT_EQ(system.getChassis().size(), 1);
     EXPECT_EQ(system.getChassis()[0]->getNumber(), 1);
-    // TODO: Add tests for IDMap once code to populate it is implemented
+    EXPECT_NO_THROW(system.getIDMap().getRule("set_voltage_rule"));
+    EXPECT_NO_THROW(system.getIDMap().getDevice("reg1"));
+    EXPECT_NO_THROW(system.getIDMap().getRail("rail1"));
+    EXPECT_THROW(system.getIDMap().getRail("rail2"), std::invalid_argument);
     EXPECT_EQ(system.getRules().size(), 1);
     EXPECT_EQ(system.getRules()[0]->getID(), "set_voltage_rule");
 }
@@ -68,20 +74,59 @@
 
 TEST(SystemTests, GetIDMap)
 {
-    // TODO: Code to build IDMap is not implemented yet
+    // Create Rules
+    std::vector<std::unique_ptr<Rule>> rules{};
+    rules.emplace_back(createRule("set_voltage_rule"));
+    rules.emplace_back(createRule("read_sensors_rule"));
+
+    // Create Chassis
+    std::vector<std::unique_ptr<Chassis>> chassis{};
+    {
+        // Chassis 1
+        std::vector<std::unique_ptr<Device>> devices{};
+        devices.emplace_back(createDevice("reg1", {"rail1"}));
+        devices.emplace_back(createDevice("reg2", {"rail2a", "rail2b"}));
+        chassis.emplace_back(std::make_unique<Chassis>(1, std::move(devices)));
+    }
+    {
+        // Chassis 2
+        std::vector<std::unique_ptr<Device>> devices{};
+        devices.emplace_back(createDevice("reg3", {"rail3a", "rail3b"}));
+        devices.emplace_back(createDevice("reg4"));
+        chassis.emplace_back(std::make_unique<Chassis>(2, std::move(devices)));
+    }
+
+    // Create System
+    System system{std::move(rules), std::move(chassis)};
+    const IDMap& idMap = system.getIDMap();
+
+    // Verify all Rules are in the IDMap
+    EXPECT_NO_THROW(idMap.getRule("set_voltage_rule"));
+    EXPECT_NO_THROW(idMap.getRule("read_sensors_rule"));
+    EXPECT_THROW(idMap.getRule("set_voltage_rule2"), std::invalid_argument);
+
+    // Verify all Devices are in the IDMap
+    EXPECT_NO_THROW(idMap.getDevice("reg1"));
+    EXPECT_NO_THROW(idMap.getDevice("reg2"));
+    EXPECT_NO_THROW(idMap.getDevice("reg3"));
+    EXPECT_NO_THROW(idMap.getDevice("reg4"));
+    EXPECT_THROW(idMap.getDevice("reg5"), std::invalid_argument);
+
+    // Verify all Rails are in the IDMap
+    EXPECT_NO_THROW(idMap.getRail("rail1"));
+    EXPECT_NO_THROW(idMap.getRail("rail2a"));
+    EXPECT_NO_THROW(idMap.getRail("rail2b"));
+    EXPECT_NO_THROW(idMap.getRail("rail3a"));
+    EXPECT_NO_THROW(idMap.getRail("rail3b"));
+    EXPECT_THROW(idMap.getRail("rail4"), std::invalid_argument);
 }
 
 TEST(SystemTests, GetRules)
 {
     // Create Rules
     std::vector<std::unique_ptr<Rule>> rules{};
-    std::vector<std::unique_ptr<Action>> actions{};
-    actions.emplace_back(std::make_unique<MockAction>());
-    rules.emplace_back(
-        std::make_unique<Rule>("set_voltage_rule", std::move(actions)));
-    actions.emplace_back(std::make_unique<MockAction>());
-    rules.emplace_back(
-        std::make_unique<Rule>("read_sensors_rule", std::move(actions)));
+    rules.emplace_back(createRule("set_voltage_rule"));
+    rules.emplace_back(createRule("read_sensors_rule"));
 
     // Create Chassis
     std::vector<std::unique_ptr<Chassis>> chassis{};
diff --git a/phosphor-regulators/test/test_utils.hpp b/phosphor-regulators/test/test_utils.hpp
index cbb322a..313d9f7 100644
--- a/phosphor-regulators/test/test_utils.hpp
+++ b/phosphor-regulators/test/test_utils.hpp
@@ -13,9 +13,20 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+#include "action.hpp"
+#include "chassis.hpp"
+#include "configuration.hpp"
+#include "device.hpp"
 #include "i2c_interface.hpp"
+#include "mock_action.hpp"
+#include "presence_detection.hpp"
+#include "rail.hpp"
+#include "rule.hpp"
 
 #include <memory>
+#include <string>
+#include <utility>
+#include <vector>
 
 namespace phosphor::power::regulators::test_utils
 {
@@ -30,4 +41,52 @@
     return i2c::create(1, 0x70, i2c::I2CInterface::InitialState::CLOSED);
 }
 
+/**
+ * Creates a Device object with the specified ID.
+ *
+ * Creates Rail objects within the Device if railIDs is specified.
+ *
+ * @param id device ID
+ * @param railIDs rail IDs (optional)
+ * @return Device object
+ */
+inline std::unique_ptr<Device>
+    createDevice(const std::string& id,
+                 const std::vector<std::string>& railIDs = {})
+{
+    // Create Rails (if any)
+    std::vector<std::unique_ptr<Rail>> rails{};
+    for (const std::string& railID : railIDs)
+    {
+        rails.emplace_back(std::make_unique<Rail>(railID));
+    }
+
+    // Create Device
+    bool isRegulator = true;
+    std::string fru = "/system/chassis/motherboard/reg1";
+    std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
+    std::unique_ptr<PresenceDetection> presenceDetection{};
+    std::unique_ptr<Configuration> configuration{};
+    return std::make_unique<Device>(id, isRegulator, fru,
+                                    std::move(i2cInterface),
+                                    std::move(presenceDetection),
+                                    std::move(configuration), std::move(rails));
+}
+
+/**
+ * Creates a Rule object with the specified ID.
+ *
+ * @param id rule ID
+ * @return Rule object
+ */
+inline std::unique_ptr<Rule> createRule(const std::string& id)
+{
+    // Create actions
+    std::vector<std::unique_ptr<Action>> actions{};
+    actions.emplace_back(std::make_unique<MockAction>());
+
+    // Create Rule
+    return std::make_unique<Rule>(id, std::move(actions));
+}
+
 } // namespace phosphor::power::regulators::test_utils