Create regulators IDMap class

Create the IDMap class used in the regulators action framework.

Signed-off-by: Shawn McCarney <shawnmm@us.ibm.com>
Change-Id: I5bd08df04a4b93ba0ebf5f8648084f79ca7c1a1d
diff --git a/phosphor-regulators/src/id_map.hpp b/phosphor-regulators/src/id_map.hpp
new file mode 100644
index 0000000..c4aab0b
--- /dev/null
+++ b/phosphor-regulators/src/id_map.hpp
@@ -0,0 +1,156 @@
+/**
+ * Copyright © 2019 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.
+ */
+#pragma once
+
+#include "device.hpp"
+#include "rail.hpp"
+#include "rule.hpp"
+
+#include <map>
+#include <stdexcept>
+#include <string>
+
+namespace phosphor
+{
+namespace power
+{
+namespace regulators
+{
+
+/**
+ * @class IDMap
+ *
+ * This class provides a mapping from string IDs to the associated Device, Rail,
+ * and Rule objects.
+ */
+class IDMap
+{
+  public:
+    // Specify which compiler-generated methods we want
+    IDMap() = default;
+    IDMap(const IDMap&) = delete;
+    IDMap(IDMap&&) = delete;
+    IDMap& operator=(const IDMap&) = delete;
+    IDMap& operator=(IDMap&&) = delete;
+    ~IDMap() = default;
+
+    /**
+     * Adds the specified device to this IDMap.
+     *
+     * @param device device to add
+     */
+    void addDevice(Device& device)
+    {
+        deviceMap[device.getID()] = &device;
+    }
+
+    /**
+     * Adds the specified rail to this IDMap.
+     *
+     * @param rail rail to add
+     */
+    void addRail(Rail& rail)
+    {
+        railMap[rail.getID()] = &rail;
+    }
+
+    /**
+     * Adds the specified rule to this IDMap.
+     *
+     * @param rule rule to add
+     */
+    void addRule(Rule& rule)
+    {
+        ruleMap[rule.getID()] = &rule;
+    }
+
+    /**
+     * Returns the device with the specified ID.
+     *
+     * Throws invalid_argument if no device is found with specified ID.
+     *
+     * @param id device ID
+     * @return device with specified ID
+     */
+    Device& getDevice(const std::string& id) const
+    {
+        auto it = deviceMap.find(id);
+        if (it == deviceMap.end())
+        {
+            throw std::invalid_argument{"Unable to find device with ID \"" +
+                                        id + '"'};
+        }
+        return *(it->second);
+    }
+
+    /**
+     * Returns the rail with the specified ID.
+     *
+     * Throws invalid_argument if no rail is found with specified ID.
+     *
+     * @param id rail ID
+     * @return rail with specified ID
+     */
+    Rail& getRail(const std::string& id) const
+    {
+        auto it = railMap.find(id);
+        if (it == railMap.end())
+        {
+            throw std::invalid_argument{"Unable to find rail with ID \"" + id +
+                                        '"'};
+        }
+        return *(it->second);
+    }
+
+    /**
+     * Returns the rule with the specified ID.
+     *
+     * Throws invalid_argument if no rule is found with specified ID.
+     *
+     * @param id rule ID
+     * @return rule with specified ID
+     */
+    Rule& getRule(const std::string& id) const
+    {
+        auto it = ruleMap.find(id);
+        if (it == ruleMap.end())
+        {
+            throw std::invalid_argument{"Unable to find rule with ID \"" + id +
+                                        '"'};
+        }
+        return *(it->second);
+    }
+
+  private:
+    /**
+     * Map from device IDs to Device objects.  Does not own the objects.
+     */
+    std::map<std::string, Device*> deviceMap{};
+
+    /**
+     * Map from rail IDs to Rail objects.  Does not own the objects.
+     */
+    std::map<std::string, Rail*> railMap{};
+
+    /**
+     * Map from rule IDs to Rule objects.  Does not own the objects.
+     */
+    std::map<std::string, Rule*> ruleMap{};
+};
+
+} // namespace regulators
+} // namespace power
+} // namespace phosphor
diff --git a/phosphor-regulators/test/id_map_tests.cpp b/phosphor-regulators/test/id_map_tests.cpp
new file mode 100644
index 0000000..7a1307a
--- /dev/null
+++ b/phosphor-regulators/test/id_map_tests.cpp
@@ -0,0 +1,228 @@
+/**
+ * Copyright © 2019 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 "id_map.hpp"
+
+#include <exception>
+#include <stdexcept>
+#include <string>
+
+#include <gtest/gtest.h>
+
+using namespace phosphor::power::regulators;
+
+TEST(IDMapTests, AddDevice)
+{
+    IDMap idMap{};
+
+    // Create device
+    std::string id{"vio_reg"};
+    Device device{id};
+
+    // Verify device is not initially in map
+    EXPECT_THROW(idMap.getDevice(id), std::invalid_argument);
+
+    // Add device to map
+    idMap.addDevice(device);
+
+    // Verify device is now in map
+    try
+    {
+        Device& deviceFound = idMap.getDevice(id);
+        EXPECT_EQ(deviceFound.getID(), id);
+        EXPECT_EQ(&deviceFound, &device);
+    }
+    catch (const std::exception& error)
+    {
+        ADD_FAILURE() << "Should not have caught exception.";
+    }
+
+    // Verify different device is not in map
+    EXPECT_THROW(idMap.getDevice("vio_reg2"), std::invalid_argument);
+}
+
+TEST(IDMapTests, AddRail)
+{
+    IDMap idMap{};
+
+    // Create rail
+    std::string id{"vio0"};
+    Rail rail{id};
+
+    // Verify rail is not initially in map
+    EXPECT_THROW(idMap.getRail(id), std::invalid_argument);
+
+    // Add rail to map
+    idMap.addRail(rail);
+
+    // Verify rail is now in map
+    try
+    {
+        Rail& railFound = idMap.getRail(id);
+        EXPECT_EQ(railFound.getID(), id);
+        EXPECT_EQ(&railFound, &rail);
+    }
+    catch (const std::exception& error)
+    {
+        ADD_FAILURE() << "Should not have caught exception.";
+    }
+
+    // Verify different rail is not in map
+    EXPECT_THROW(idMap.getRail("vcs0"), std::invalid_argument);
+}
+
+TEST(IDMapTests, AddRule)
+{
+    IDMap idMap{};
+
+    // Create rule
+    std::string id{"set_voltage_rule"};
+    Rule rule{id};
+
+    // Verify rule is not initially in map
+    EXPECT_THROW(idMap.getRule(id), std::invalid_argument);
+
+    // Add rule to map
+    idMap.addRule(rule);
+
+    // Verify rule is now in map
+    try
+    {
+        Rule& ruleFound = idMap.getRule(id);
+        EXPECT_EQ(ruleFound.getID(), id);
+        EXPECT_EQ(&ruleFound, &rule);
+    }
+    catch (const std::exception& error)
+    {
+        ADD_FAILURE() << "Should not have caught exception.";
+    }
+
+    // Verify different rule is not in map
+    EXPECT_THROW(idMap.getRule("set_voltage_rule_page0"),
+                 std::invalid_argument);
+}
+
+TEST(IDMapTests, GetDevice)
+{
+    IDMap idMap{};
+
+    // Add a device to the map
+    std::string id{"vio_reg"};
+    Device device{id};
+    idMap.addDevice(device);
+
+    // Test where ID found in map
+    try
+    {
+        Device& deviceFound = idMap.getDevice(id);
+        EXPECT_EQ(deviceFound.getID(), id);
+        EXPECT_EQ(&deviceFound, &device);
+    }
+    catch (const std::exception& error)
+    {
+        ADD_FAILURE() << "Should not have caught exception.";
+    }
+
+    // Test where ID not found in map
+    try
+    {
+        idMap.getDevice("vcs_reg");
+        ADD_FAILURE() << "Should not have reached this line.";
+    }
+    catch (const std::invalid_argument& ia_error)
+    {
+        EXPECT_STREQ(ia_error.what(),
+                     "Unable to find device with ID \"vcs_reg\"");
+    }
+    catch (const std::exception& error)
+    {
+        ADD_FAILURE() << "Should not have caught exception.";
+    }
+}
+
+TEST(IDMapTests, GetRail)
+{
+    IDMap idMap{};
+
+    // Add a rail to the map
+    std::string id{"vio0"};
+    Rail rail{id};
+    idMap.addRail(rail);
+
+    // Test where ID found in map
+    try
+    {
+        Rail& railFound = idMap.getRail(id);
+        EXPECT_EQ(railFound.getID(), id);
+        EXPECT_EQ(&railFound, &rail);
+    }
+    catch (const std::exception& error)
+    {
+        ADD_FAILURE() << "Should not have caught exception.";
+    }
+
+    // Test where ID not found in map
+    try
+    {
+        idMap.getRail("vcs0");
+        ADD_FAILURE() << "Should not have reached this line.";
+    }
+    catch (const std::invalid_argument& ia_error)
+    {
+        EXPECT_STREQ(ia_error.what(), "Unable to find rail with ID \"vcs0\"");
+    }
+    catch (const std::exception& error)
+    {
+        ADD_FAILURE() << "Should not have caught exception.";
+    }
+}
+
+TEST(IDMapTests, GetRule)
+{
+    IDMap idMap{};
+
+    // Add a rule to the map
+    std::string id{"set_voltage_rule"};
+    Rule rule{id};
+    idMap.addRule(rule);
+
+    // Test where ID found in map
+    try
+    {
+        Rule& ruleFound = idMap.getRule(id);
+        EXPECT_EQ(ruleFound.getID(), id);
+        EXPECT_EQ(&ruleFound, &rule);
+    }
+    catch (const std::exception& error)
+    {
+        ADD_FAILURE() << "Should not have caught exception.";
+    }
+
+    // Test where ID not found in map
+    try
+    {
+        idMap.getRule("read_sensors_rule");
+        ADD_FAILURE() << "Should not have reached this line.";
+    }
+    catch (const std::invalid_argument& ia_error)
+    {
+        EXPECT_STREQ(ia_error.what(),
+                     "Unable to find rule with ID \"read_sensors_rule\"");
+    }
+    catch (const std::exception& error)
+    {
+        ADD_FAILURE() << "Should not have caught exception.";
+    }
+}
diff --git a/phosphor-regulators/test/meson.build b/phosphor-regulators/test/meson.build
index 0e32ad7..c89e689 100644
--- a/phosphor-regulators/test/meson.build
+++ b/phosphor-regulators/test/meson.build
@@ -1,6 +1,7 @@
 test('phosphor-regulators-tests',
      executable('phosphor-regulators-tests',
                 'device_tests.cpp',
+                'id_map_tests.cpp',
                 'rail_tests.cpp',
                 'rule_tests.cpp',
                 dependencies: [