regulators: Add detectPhaseFaults() to System

Add a detectPhaseFaults() method to the System class.  This method
detects redundant phase faults in all regulator devices in the system.

Add a gtest test case to exercise the new code.

Signed-off-by: Shawn McCarney <shawnmm@us.ibm.com>
Change-Id: I61b897b6b563280a2c0440aa2f35f3eb6c5de232
diff --git a/phosphor-regulators/src/system.cpp b/phosphor-regulators/src/system.cpp
index a36d1ee..6332686 100644
--- a/phosphor-regulators/src/system.cpp
+++ b/phosphor-regulators/src/system.cpp
@@ -70,6 +70,15 @@
     }
 }
 
+void System::detectPhaseFaults(Services& services)
+{
+    // Detect phase faults in regulator devices in each chassis
+    for (std::unique_ptr<Chassis>& oneChassis : chassis)
+    {
+        oneChassis->detectPhaseFaults(services, *this);
+    }
+}
+
 void System::monitorSensors(Services& services)
 {
     // Monitor sensors in each chassis
diff --git a/phosphor-regulators/src/system.hpp b/phosphor-regulators/src/system.hpp
index 16100c2..caa3804 100644
--- a/phosphor-regulators/src/system.hpp
+++ b/phosphor-regulators/src/system.hpp
@@ -93,6 +93,15 @@
     void configure(Services& services);
 
     /**
+     * Detect redundant phase faults in regulator devices in the system.
+     *
+     * This method should be called every 15 seconds.
+     *
+     * @param services system services like error logging and the journal
+     */
+    void detectPhaseFaults(Services& services);
+
+    /**
      * Returns the chassis in the system.
      *
      * @return chassis
diff --git a/phosphor-regulators/test/system_tests.cpp b/phosphor-regulators/test/system_tests.cpp
index 53127bb..720620d 100644
--- a/phosphor-regulators/test/system_tests.cpp
+++ b/phosphor-regulators/test/system_tests.cpp
@@ -19,12 +19,14 @@
 #include "device.hpp"
 #include "i2c_interface.hpp"
 #include "id_map.hpp"
+#include "log_phase_fault_action.hpp"
 #include "mock_action.hpp"
 #include "mock_error_logging.hpp"
 #include "mock_journal.hpp"
 #include "mock_sensors.hpp"
 #include "mock_services.hpp"
 #include "mocked_i2c_interface.hpp"
+#include "phase_fault.hpp"
 #include "phase_fault_detection.hpp"
 #include "presence_detection.hpp"
 #include "rail.hpp"
@@ -251,6 +253,99 @@
     system.configure(services);
 }
 
+TEST(SystemTests, DetectPhaseFaults)
+{
+    // Create mock services with the following expectations:
+    // - 2 error messages in journal for N phase fault detected in reg0
+    // - 2 error messages in journal for N phase fault detected in reg1
+    // - 1 N phase fault error logged for reg0
+    // - 1 N phase fault error logged for reg1
+    MockServices services{};
+    MockJournal& journal = services.getMockJournal();
+    EXPECT_CALL(journal,
+                logError("n phase fault detected in regulator reg0: count=1"))
+        .Times(1);
+    EXPECT_CALL(journal,
+                logError("n phase fault detected in regulator reg0: count=2"))
+        .Times(1);
+    EXPECT_CALL(journal,
+                logError("n phase fault detected in regulator reg1: count=1"))
+        .Times(1);
+    EXPECT_CALL(journal,
+                logError("n phase fault detected in regulator reg1: count=2"))
+        .Times(1);
+    MockErrorLogging& errorLogging = services.getMockErrorLogging();
+    EXPECT_CALL(errorLogging, logPhaseFault).Times(2);
+
+    std::vector<std::unique_ptr<Chassis>> chassisVec{};
+
+    // Create Chassis 1 with regulator reg0
+    {
+        // Create PhaseFaultDetection
+        auto action = std::make_unique<LogPhaseFaultAction>(PhaseFaultType::n);
+        std::vector<std::unique_ptr<Action>> actions{};
+        actions.push_back(std::move(action));
+        auto phaseFaultDetection =
+            std::make_unique<PhaseFaultDetection>(std::move(actions));
+
+        // Create Device
+        auto i2cInterface = std::make_unique<i2c::MockedI2CInterface>();
+        std::unique_ptr<PresenceDetection> presenceDetection{};
+        std::unique_ptr<Configuration> configuration{};
+        auto device = std::make_unique<Device>(
+            "reg0", true,
+            "/xyz/openbmc_project/inventory/system/chassis1/motherboard/"
+            "reg0",
+            std::move(i2cInterface), std::move(presenceDetection),
+            std::move(configuration), std::move(phaseFaultDetection));
+
+        // Create Chassis
+        std::vector<std::unique_ptr<Device>> devices{};
+        devices.emplace_back(std::move(device));
+        auto chassis = std::make_unique<Chassis>(1, chassisInvPath + '1',
+                                                 std::move(devices));
+        chassisVec.emplace_back(std::move(chassis));
+    }
+
+    // Create Chassis 2 with regulator reg1
+    {
+        // Create PhaseFaultDetection
+        auto action = std::make_unique<LogPhaseFaultAction>(PhaseFaultType::n);
+        std::vector<std::unique_ptr<Action>> actions{};
+        actions.push_back(std::move(action));
+        auto phaseFaultDetection =
+            std::make_unique<PhaseFaultDetection>(std::move(actions));
+
+        // Create Device
+        auto i2cInterface = std::make_unique<i2c::MockedI2CInterface>();
+        std::unique_ptr<PresenceDetection> presenceDetection{};
+        std::unique_ptr<Configuration> configuration{};
+        auto device = std::make_unique<Device>(
+            "reg1", true,
+            "/xyz/openbmc_project/inventory/system/chassis2/motherboard/"
+            "reg1",
+            std::move(i2cInterface), std::move(presenceDetection),
+            std::move(configuration), std::move(phaseFaultDetection));
+
+        // Create Chassis
+        std::vector<std::unique_ptr<Device>> devices{};
+        devices.emplace_back(std::move(device));
+        auto chassis = std::make_unique<Chassis>(2, chassisInvPath + '2',
+                                                 std::move(devices));
+        chassisVec.emplace_back(std::move(chassis));
+    }
+
+    // Create System that contains Chassis
+    std::vector<std::unique_ptr<Rule>> rules{};
+    System system{std::move(rules), std::move(chassisVec)};
+
+    // Call detectPhaseFaults() 5 times
+    for (int i = 1; i <= 5; ++i)
+    {
+        system.detectPhaseFaults(services);
+    }
+}
+
 TEST(SystemTests, GetChassis)
 {
     // Specify an empty rules vector