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{};