regulators: Add phase faults to ActionEnvironment

Enhance the ActionEnvironment class to support detecting and logging
redundant phase fault errors.

Add gtests to test the new functions/methods.

Signed-off-by: Shawn McCarney <shawnmm@us.ibm.com>
Change-Id: Id87d811374dd3da616cdeeb780b3575ef96ffbb7
diff --git a/phosphor-regulators/src/actions/action_environment.hpp b/phosphor-regulators/src/actions/action_environment.hpp
index 72aa8f4..7f6c402 100644
--- a/phosphor-regulators/src/actions/action_environment.hpp
+++ b/phosphor-regulators/src/actions/action_environment.hpp
@@ -16,10 +16,13 @@
 #pragma once
 
 #include "id_map.hpp"
+#include "phase_fault.hpp"
 #include "services.hpp"
 
 #include <cstddef> // for size_t
+#include <map>
 #include <optional>
+#include <set>
 #include <stdexcept>
 #include <string>
 
@@ -40,6 +43,9 @@
  *   - current volts value (if any)
  *   - mapping from device and rule IDs to the corresponding objects
  *   - rule call stack depth (to detect infinite recursion)
+ *   - reference to system services
+ *   - faults detected by actions (if any)
+ *   - additional error data captured by actions (if any)
  */
 class ActionEnvironment
 {
@@ -72,6 +78,33 @@
     }
 
     /**
+     * Adds the specified key/value pair to the map of additional error data
+     * that has been captured.
+     *
+     * This data provides more information about an error and will be stored in
+     * the error log.
+     *
+     * @param key key name
+     * @param value value expressed as a string
+     */
+    void addAdditionalErrorData(const std::string& key,
+                                const std::string& value)
+    {
+        additionalErrorData.emplace(key, value);
+    }
+
+    /**
+     * Adds the specified phase fault to the set of faults that have been
+     * detected.
+     *
+     * @param type phase fault type
+     */
+    void addPhaseFault(PhaseFaultType type)
+    {
+        phaseFaults.emplace(type);
+    }
+
+    /**
      * Decrements the rule call stack depth by one.
      *
      * Should be used when a call to a rule returns.  Does nothing if depth is
@@ -86,6 +119,16 @@
     }
 
     /**
+     * Returns the additional error data that has been captured (if any).
+     *
+     * @return additional error data
+     */
+    const std::map<std::string, std::string>& getAdditionalErrorData() const
+    {
+        return additionalErrorData;
+    }
+
+    /**
      * Returns the device with the current device ID.
      *
      * Throws invalid_argument if no device is found with current ID.
@@ -108,6 +151,16 @@
     }
 
     /**
+     * Returns the set of phase faults that have been detected (if any).
+     *
+     * @return phase faults detected
+     */
+    const std::set<PhaseFaultType>& getPhaseFaults() const
+    {
+        return phaseFaults;
+    }
+
+    /**
      * Returns the rule with the specified ID.
      *
      * Throws invalid_argument if no rule is found with specified ID.
@@ -218,6 +271,16 @@
      * Rule call stack depth.
      */
     size_t ruleDepth{0};
+
+    /**
+     * Redundant phase faults that have been detected.
+     */
+    std::set<PhaseFaultType> phaseFaults{};
+
+    /**
+     * Additional error data that has been captured.
+     */
+    std::map<std::string, std::string> additionalErrorData{};
 };
 
 } // namespace phosphor::power::regulators
diff --git a/phosphor-regulators/src/phase_fault.hpp b/phosphor-regulators/src/phase_fault.hpp
new file mode 100644
index 0000000..fbcb255
--- /dev/null
+++ b/phosphor-regulators/src/phase_fault.hpp
@@ -0,0 +1,74 @@
+/**
+ * Copyright © 2021 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 <string>
+
+namespace phosphor::power::regulators
+{
+
+/**
+ * Redundant phase fault type.
+ *
+ * A voltage regulator is sometimes called a "phase controller" because it
+ * controls one or more phases that perform the actual voltage regulation.
+ *
+ * A regulator may have redundant phases.  If a redundant phase fails, the
+ * regulator will continue to provide the desired output voltage.  However, a
+ * phase fault error should be logged warning the user that the regulator has
+ * lost redundancy.
+ */
+enum class PhaseFaultType : unsigned char
+{
+    /**
+     * N phase fault type.
+     *
+     * Regulator has lost all redundant phases.  The regulator is now at
+     * redundancy level N.
+     */
+    n,
+
+    /**
+     * N+1 phase fault type.
+     *
+     * An "N+2" regulator has lost one redundant phase.  The regulator is now at
+     * redundancy level "N+1".
+     */
+    n_plus_1
+};
+
+/**
+ * Returns the name of the specified PhaseFaultType.
+ *
+ * @param type phase fault type
+ * @return phase fault type name
+ */
+inline std::string toString(PhaseFaultType type)
+{
+    std::string name{};
+    switch (type)
+    {
+        case PhaseFaultType::n:
+            name = "n";
+            break;
+        case PhaseFaultType::n_plus_1:
+            name = "n+1";
+            break;
+    }
+    return name;
+}
+
+} // namespace phosphor::power::regulators
diff --git a/phosphor-regulators/test/actions/action_environment_tests.cpp b/phosphor-regulators/test/actions/action_environment_tests.cpp
index de52845..cea1bd2 100644
--- a/phosphor-regulators/test/actions/action_environment_tests.cpp
+++ b/phosphor-regulators/test/actions/action_environment_tests.cpp
@@ -19,6 +19,7 @@
 #include "id_map.hpp"
 #include "mock_services.hpp"
 #include "mocked_i2c_interface.hpp"
+#include "phase_fault.hpp"
 #include "rule.hpp"
 
 #include <cstddef> // for size_t
@@ -53,8 +54,10 @@
     try
     {
         ActionEnvironment env{idMap, "regulator1", services};
+        EXPECT_EQ(env.getAdditionalErrorData().size(), 0);
         EXPECT_EQ(env.getDevice().getID(), "regulator1");
         EXPECT_EQ(env.getDeviceID(), "regulator1");
+        EXPECT_EQ(env.getPhaseFaults().size(), 0);
         EXPECT_EQ(env.getRuleDepth(), 0);
         EXPECT_EQ(env.getVolts().has_value(), false);
     }
@@ -64,6 +67,44 @@
     }
 }
 
+TEST(ActionEnvironmentTests, AddAdditionalErrorData)
+{
+    IDMap idMap{};
+    MockServices services{};
+    ActionEnvironment env{idMap, "", services};
+    EXPECT_EQ(env.getAdditionalErrorData().size(), 0);
+
+    env.addAdditionalErrorData("foo", "foo_value");
+    env.addAdditionalErrorData("bar", "bar_value");
+    EXPECT_EQ(env.getAdditionalErrorData().size(), 2);
+    EXPECT_EQ(env.getAdditionalErrorData().at("foo"), "foo_value");
+    EXPECT_EQ(env.getAdditionalErrorData().at("bar"), "bar_value");
+}
+
+TEST(ActionEnvironmentTests, AddPhaseFault)
+{
+    IDMap idMap{};
+    MockServices services{};
+    ActionEnvironment env{idMap, "", services};
+    EXPECT_EQ(env.getPhaseFaults().size(), 0);
+
+    // Add N phase fault
+    env.addPhaseFault(PhaseFaultType::n);
+    EXPECT_EQ(env.getPhaseFaults().size(), 1);
+    EXPECT_EQ(env.getPhaseFaults().count(PhaseFaultType::n), 1);
+    EXPECT_EQ(env.getPhaseFaults().count(PhaseFaultType::n_plus_1), 0);
+
+    // Add N+1 phase fault
+    env.addPhaseFault(PhaseFaultType::n_plus_1);
+    EXPECT_EQ(env.getPhaseFaults().size(), 2);
+    EXPECT_EQ(env.getPhaseFaults().count(PhaseFaultType::n), 1);
+    EXPECT_EQ(env.getPhaseFaults().count(PhaseFaultType::n_plus_1), 1);
+
+    // Add N+1 phase fault again; should be ignored since stored in a std::set
+    env.addPhaseFault(PhaseFaultType::n_plus_1);
+    EXPECT_EQ(env.getPhaseFaults().size(), 2);
+}
+
 TEST(ActionEnvironmentTests, DecrementRuleDepth)
 {
     IDMap idMap{};
@@ -81,6 +122,22 @@
     EXPECT_EQ(env.getRuleDepth(), 0);
 }
 
+TEST(ActionEnvironmentTests, GetAdditionalErrorData)
+{
+    IDMap idMap{};
+    MockServices services{};
+    ActionEnvironment env{idMap, "", services};
+    EXPECT_EQ(env.getAdditionalErrorData().size(), 0);
+
+    env.addAdditionalErrorData("foo", "foo_value");
+    EXPECT_EQ(env.getAdditionalErrorData().size(), 1);
+    EXPECT_EQ(env.getAdditionalErrorData().at("foo"), "foo_value");
+
+    env.addAdditionalErrorData("bar", "bar_value");
+    EXPECT_EQ(env.getAdditionalErrorData().size(), 2);
+    EXPECT_EQ(env.getAdditionalErrorData().at("bar"), "bar_value");
+}
+
 TEST(ActionEnvironmentTests, GetDevice)
 {
     // Create IDMap
@@ -140,6 +197,20 @@
     EXPECT_EQ(env.getDeviceID(), "regulator1");
 }
 
+TEST(ActionEnvironmentTests, GetPhaseFaults)
+{
+    IDMap idMap{};
+    MockServices services{};
+    ActionEnvironment env{idMap, "", services};
+    EXPECT_EQ(env.getPhaseFaults().size(), 0);
+
+    env.addPhaseFault(PhaseFaultType::n);
+    env.addPhaseFault(PhaseFaultType::n_plus_1);
+    EXPECT_EQ(env.getPhaseFaults().size(), 2);
+    EXPECT_EQ(env.getPhaseFaults().count(PhaseFaultType::n), 1);
+    EXPECT_EQ(env.getPhaseFaults().count(PhaseFaultType::n_plus_1), 1);
+}
+
 TEST(ActionEnvironmentTests, GetRule)
 {
     // Create IDMap
diff --git a/phosphor-regulators/test/meson.build b/phosphor-regulators/test/meson.build
index 3f16812..897e540 100644
--- a/phosphor-regulators/test/meson.build
+++ b/phosphor-regulators/test/meson.build
@@ -14,6 +14,7 @@
     'exception_utils_tests.cpp',
     'ffdc_file_tests.cpp',
     'id_map_tests.cpp',
+    'phase_fault_tests.cpp',
     'pmbus_error_tests.cpp',
     'pmbus_utils_tests.cpp',
     'presence_detection_tests.cpp',
diff --git a/phosphor-regulators/test/phase_fault_tests.cpp b/phosphor-regulators/test/phase_fault_tests.cpp
new file mode 100644
index 0000000..5c18add
--- /dev/null
+++ b/phosphor-regulators/test/phase_fault_tests.cpp
@@ -0,0 +1,26 @@
+/**
+ * Copyright © 2021 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 "phase_fault.hpp"
+
+#include <gtest/gtest.h>
+
+using namespace phosphor::power::regulators;
+
+TEST(PhaseFaultTests, ToString)
+{
+    EXPECT_EQ(toString(PhaseFaultType::n), "n");
+    EXPECT_EQ(toString(PhaseFaultType::n_plus_1), "n+1");
+}