regulators: Implement log_phase_fault action

Implement the new log_phase_fault action in the JSON configuration file.

Create gtests to test the new class.

Signed-off-by: Shawn McCarney <shawnmm@us.ibm.com>
Change-Id: I53130b029967d0a0d7b7fdd9b12f69035b9f085e
diff --git a/phosphor-regulators/src/actions/log_phase_fault_action.hpp b/phosphor-regulators/src/actions/log_phase_fault_action.hpp
new file mode 100644
index 0000000..c0690b4
--- /dev/null
+++ b/phosphor-regulators/src/actions/log_phase_fault_action.hpp
@@ -0,0 +1,95 @@
+/**
+ * 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 "action.hpp"
+#include "action_environment.hpp"
+#include "phase_fault.hpp"
+
+#include <string>
+
+namespace phosphor::power::regulators
+{
+
+/**
+ * @class LogPhaseFaultAction
+ *
+ * Logs a redundant phase fault error for a voltage regulator.
+ *
+ * Implements the log_phase_fault action in the JSON config file.
+ */
+class LogPhaseFaultAction : public Action
+{
+  public:
+    // Specify which compiler-generated methods we want
+    LogPhaseFaultAction() = delete;
+    LogPhaseFaultAction(const LogPhaseFaultAction&) = delete;
+    LogPhaseFaultAction(LogPhaseFaultAction&&) = delete;
+    LogPhaseFaultAction& operator=(const LogPhaseFaultAction&) = delete;
+    LogPhaseFaultAction& operator=(LogPhaseFaultAction&&) = delete;
+    virtual ~LogPhaseFaultAction() = default;
+
+    /**
+     * Constructor.
+     *
+     * @param type phase fault type
+     */
+    explicit LogPhaseFaultAction(PhaseFaultType type) : type{type}
+    {
+    }
+
+    /**
+     * Executes this action.
+     *
+     * Adds the phase fault to the set that have been detected.
+     *
+     * @param environment action execution environment
+     * @return true
+     */
+    virtual bool execute(ActionEnvironment& environment) override
+    {
+        environment.addPhaseFault(type);
+        return true;
+    }
+
+    /**
+     * Returns the phase fault type.
+     *
+     * @return phase fault type
+     */
+    PhaseFaultType getType() const
+    {
+        return type;
+    }
+
+    /**
+     * Returns a string description of this action.
+     *
+     * @return description of action
+     */
+    virtual std::string toString() const override
+    {
+        return "log_phase_fault: { type: " + regulators::toString(type) + " }";
+    }
+
+  private:
+    /**
+     * Phase fault type.
+     */
+    const PhaseFaultType type;
+};
+
+} // namespace phosphor::power::regulators
diff --git a/phosphor-regulators/test/actions/log_phase_fault_action_tests.cpp b/phosphor-regulators/test/actions/log_phase_fault_action_tests.cpp
new file mode 100644
index 0000000..74cf11a
--- /dev/null
+++ b/phosphor-regulators/test/actions/log_phase_fault_action_tests.cpp
@@ -0,0 +1,73 @@
+/**
+ * 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 "action_environment.hpp"
+#include "id_map.hpp"
+#include "log_phase_fault_action.hpp"
+#include "mock_services.hpp"
+#include "phase_fault.hpp"
+
+#include <gtest/gtest.h>
+
+using namespace phosphor::power::regulators;
+
+TEST(LogPhaseFaultActionTests, Constructor)
+{
+    LogPhaseFaultAction action{PhaseFaultType::n};
+    EXPECT_EQ(action.getType(), PhaseFaultType::n);
+}
+
+TEST(LogPhaseFaultActionTests, Execute)
+{
+    // Test with "n" phase fault type
+    {
+        IDMap idMap{};
+        MockServices services{};
+        ActionEnvironment env{idMap, "", services};
+        EXPECT_EQ(env.getPhaseFaults().size(), 0);
+
+        LogPhaseFaultAction action{PhaseFaultType::n};
+        EXPECT_EQ(action.execute(env), true);
+
+        EXPECT_EQ(env.getPhaseFaults().size(), 1);
+        EXPECT_EQ(env.getPhaseFaults().count(PhaseFaultType::n), 1);
+    }
+
+    // Test with "n+1" phase fault type
+    {
+        IDMap idMap{};
+        MockServices services{};
+        ActionEnvironment env{idMap, "", services};
+        EXPECT_EQ(env.getPhaseFaults().size(), 0);
+
+        LogPhaseFaultAction action{PhaseFaultType::n_plus_1};
+        EXPECT_EQ(action.execute(env), true);
+
+        EXPECT_EQ(env.getPhaseFaults().size(), 1);
+        EXPECT_EQ(env.getPhaseFaults().count(PhaseFaultType::n_plus_1), 1);
+    }
+}
+
+TEST(LogPhaseFaultActionTests, GetType)
+{
+    LogPhaseFaultAction action{PhaseFaultType::n_plus_1};
+    EXPECT_EQ(action.getType(), PhaseFaultType::n_plus_1);
+}
+
+TEST(LogPhaseFaultActionTests, ToString)
+{
+    LogPhaseFaultAction action{PhaseFaultType::n_plus_1};
+    EXPECT_EQ(action.toString(), "log_phase_fault: { type: n+1 }");
+}
diff --git a/phosphor-regulators/test/meson.build b/phosphor-regulators/test/meson.build
index 897e540..57200a4 100644
--- a/phosphor-regulators/test/meson.build
+++ b/phosphor-regulators/test/meson.build
@@ -40,6 +40,7 @@
     'actions/i2c_write_byte_action_tests.cpp',
     'actions/i2c_write_bytes_action_tests.cpp',
     'actions/if_action_tests.cpp',
+    'actions/log_phase_fault_action_tests.cpp',
     'actions/not_action_tests.cpp',
     'actions/or_action_tests.cpp',
     'actions/pmbus_read_sensor_action_tests.cpp',