regulators: Add SensorMonitoring::execute()

Implementation and Testing for SensorMonitoring::execute().

Signed-off-by: Bob King <Bob_King@wistron.com>
Change-Id: If2b39b70997cd5cb84adc6c4b601d87556b82f55
diff --git a/phosphor-regulators/src/meson.build b/phosphor-regulators/src/meson.build
index 4adeb58..6f067b5 100644
--- a/phosphor-regulators/src/meson.build
+++ b/phosphor-regulators/src/meson.build
@@ -13,6 +13,7 @@
     'id_map.cpp',
     'pmbus_utils.cpp',
     'rail.cpp',
+    'sensor_monitoring.cpp',
     'system.cpp',
 
     'actions/if_action.cpp',
diff --git a/phosphor-regulators/src/sensor_monitoring.cpp b/phosphor-regulators/src/sensor_monitoring.cpp
new file mode 100644
index 0000000..6135b50
--- /dev/null
+++ b/phosphor-regulators/src/sensor_monitoring.cpp
@@ -0,0 +1,54 @@
+/**
+ * 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 "sensor_monitoring.hpp"
+
+#include "action_environment.hpp"
+#include "action_utils.hpp"
+#include "chassis.hpp"
+#include "device.hpp"
+#include "exception_utils.hpp"
+#include "journal.hpp"
+#include "rail.hpp"
+#include "system.hpp"
+
+#include <exception>
+
+namespace phosphor::power::regulators
+{
+
+void SensorMonitoring::execute(System& system, Chassis& /*chassis*/,
+                               Device& device, Rail& rail)
+{
+    try
+    {
+        // Create ActionEnvironment
+        ActionEnvironment environment{system.getIDMap(), device.getID()};
+
+        // Execute the actions
+        action_utils::execute(actions, environment);
+    }
+    catch (const std::exception& e)
+    {
+        // Log error messages in journal
+        exception_utils::log(e);
+        journal::logErr("Unable to monitor sensors for rail " + rail.getID());
+
+        // TODO: Create error log entry
+    }
+}
+
+} // namespace phosphor::power::regulators
diff --git a/phosphor-regulators/src/sensor_monitoring.hpp b/phosphor-regulators/src/sensor_monitoring.hpp
index 7572436..f3491cd 100644
--- a/phosphor-regulators/src/sensor_monitoring.hpp
+++ b/phosphor-regulators/src/sensor_monitoring.hpp
@@ -24,6 +24,12 @@
 namespace phosphor::power::regulators
 {
 
+// Forward declarations to avoid circular dependencies
+class Chassis;
+class Device;
+class Rail;
+class System;
+
 /**
  * @class SensorMonitoring
  *
@@ -61,12 +67,13 @@
 
     /**
      * Executes the actions to read the sensors for a rail.
+     *
+     * @param system system that contains the chassis
+     * @param chassis chassis that contains the device
+     * @param device device that contains the rail
+     * @param rail rail associated with the sensors
      */
-    void execute()
-    {
-        // TODO: Create ActionEnvironment, execute actions, store sensor values
-        // on D-Bus, catch and handle any exceptions
-    }
+    void execute(System& system, Chassis& chassis, Device& device, Rail& rail);
 
     /**
      * Returns the actions that read the sensors for a rail.
diff --git a/phosphor-regulators/test/sensor_monitoring_tests.cpp b/phosphor-regulators/test/sensor_monitoring_tests.cpp
index 49b8a10..79c3b92 100644
--- a/phosphor-regulators/test/sensor_monitoring_tests.cpp
+++ b/phosphor-regulators/test/sensor_monitoring_tests.cpp
@@ -14,16 +14,38 @@
  * limitations under the License.
  */
 #include "action.hpp"
+#include "chassis.hpp"
+#include "configuration.hpp"
+#include "device.hpp"
+#include "i2c_interface.hpp"
+#include "journal.hpp"
 #include "mock_action.hpp"
+#include "mock_journal.hpp"
+#include "mocked_i2c_interface.hpp"
+#include "pmbus_read_sensor_action.hpp"
+#include "pmbus_utils.hpp"
+#include "presence_detection.hpp"
+#include "rail.hpp"
+#include "rule.hpp"
 #include "sensor_monitoring.hpp"
+#include "system.hpp"
 
+#include <cstdint>
 #include <memory>
+#include <optional>
 #include <utility>
 #include <vector>
 
+#include <gmock/gmock.h>
 #include <gtest/gtest.h>
 
 using namespace phosphor::power::regulators;
+using namespace phosphor::power::regulators::pmbus_utils;
+
+using ::testing::A;
+using ::testing::Return;
+using ::testing::Throw;
+using ::testing::TypedEq;
 
 TEST(SensorMonitoringTests, Constructor)
 {
@@ -36,7 +58,138 @@
 
 TEST(SensorMonitoringTests, Execute)
 {
-    // TODO: Implement test when execute() function is done
+    // Test where works
+    {
+        // Create PMBusReadSensorAction
+        pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::iout};
+        uint8_t command = 0x8C;
+        pmbus_utils::SensorDataFormat format{
+            pmbus_utils::SensorDataFormat::linear_11};
+        std::optional<int8_t> exponent{};
+        std::unique_ptr<PMBusReadSensorAction> action =
+            std::make_unique<PMBusReadSensorAction>(type, command, format,
+                                                    exponent);
+
+        // Create mock I2CInterface.
+        std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
+            std::make_unique<i2c::MockedI2CInterface>();
+        EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
+        EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x8C), A<uint16_t&>()))
+            .Times(1);
+
+        // Create SensorMonitoring
+        std::vector<std::unique_ptr<Action>> actions{};
+        actions.emplace_back(std::move(action));
+        std::unique_ptr<SensorMonitoring> sensorMonitoring =
+            std::make_unique<SensorMonitoring>(std::move(actions));
+        SensorMonitoring* sensorMonitoringPtr = sensorMonitoring.get();
+
+        // Create Rail that contains sensorMonitoring
+        std::unique_ptr<Configuration> configuration{};
+        std::unique_ptr<Rail> rail = std::make_unique<Rail>(
+            "vio2", std::move(configuration), std::move(sensorMonitoring));
+        Rail* railPtr = rail.get();
+
+        // Create Device that contains Rail
+        std::unique_ptr<PresenceDetection> presenceDetection{};
+        std::unique_ptr<Configuration> deviceConfiguration{};
+        std::vector<std::unique_ptr<Rail>> rails{};
+        rails.emplace_back(std::move(rail));
+        std::unique_ptr<Device> device = std::make_unique<Device>(
+            "reg1", true, "/system/chassis/motherboard/reg1",
+            std::move(i2cInterface), std::move(presenceDetection),
+            std::move(deviceConfiguration), std::move(rails));
+        Device* devicePtr = device.get();
+
+        // Create Chassis that contains Device
+        std::vector<std::unique_ptr<Device>> devices{};
+        devices.emplace_back(std::move(device));
+        std::unique_ptr<Chassis> chassis =
+            std::make_unique<Chassis>(1, std::move(devices));
+        Chassis* chassisPtr = chassis.get();
+
+        // Create System that contains Chassis
+        std::vector<std::unique_ptr<Rule>> rules{};
+        std::vector<std::unique_ptr<Chassis>> chassisVec{};
+        chassisVec.emplace_back(std::move(chassis));
+        System system{std::move(rules), std::move(chassisVec)};
+
+        // Execute sensorMonitoring
+        journal::clear();
+        sensorMonitoringPtr->execute(system, *chassisPtr, *devicePtr, *railPtr);
+        EXPECT_EQ(journal::getDebugMessages().size(), 0);
+        EXPECT_EQ(journal::getErrMessages().size(), 0);
+    }
+
+    // Test where fails
+    {
+        // Create PMBusReadSensorAction
+        pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::iout};
+        uint8_t command = 0x8C;
+        pmbus_utils::SensorDataFormat format{
+            pmbus_utils::SensorDataFormat::linear_11};
+        std::optional<int8_t> exponent{};
+        std::unique_ptr<PMBusReadSensorAction> action =
+            std::make_unique<PMBusReadSensorAction>(type, command, format,
+                                                    exponent);
+
+        // Create mock I2CInterface.
+        std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
+            std::make_unique<i2c::MockedI2CInterface>();
+        EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
+        EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x8C), A<uint16_t&>()))
+            .Times(1)
+            .WillOnce(Throw(
+                i2c::I2CException{"Failed to write byte", "/dev/i2c-1", 0x70}));
+
+        // Create SensorMonitoring
+        std::vector<std::unique_ptr<Action>> actions{};
+        actions.emplace_back(std::move(action));
+        std::unique_ptr<SensorMonitoring> sensorMonitoring =
+            std::make_unique<SensorMonitoring>(std::move(actions));
+        SensorMonitoring* sensorMonitoringPtr = sensorMonitoring.get();
+
+        // Create Rail that contains sensorMonitoring
+        std::unique_ptr<Configuration> configuration{};
+        std::unique_ptr<Rail> rail = std::make_unique<Rail>(
+            "vio2", std::move(configuration), std::move(sensorMonitoring));
+        Rail* railPtr = rail.get();
+
+        // Create Device that contains Rail
+        std::unique_ptr<PresenceDetection> presenceDetection{};
+        std::unique_ptr<Configuration> deviceConfiguration{};
+        std::vector<std::unique_ptr<Rail>> rails{};
+        rails.emplace_back(std::move(rail));
+        std::unique_ptr<Device> device = std::make_unique<Device>(
+            "reg1", true, "/system/chassis/motherboard/reg1",
+            std::move(i2cInterface), std::move(presenceDetection),
+            std::move(deviceConfiguration), std::move(rails));
+        Device* devicePtr = device.get();
+
+        // Create Chassis that contains Device
+        std::vector<std::unique_ptr<Device>> devices{};
+        devices.emplace_back(std::move(device));
+        std::unique_ptr<Chassis> chassis =
+            std::make_unique<Chassis>(1, std::move(devices));
+        Chassis* chassisPtr = chassis.get();
+
+        // Create System that contains Chassis
+        std::vector<std::unique_ptr<Rule>> rules{};
+        std::vector<std::unique_ptr<Chassis>> chassisVec{};
+        chassisVec.emplace_back(std::move(chassis));
+        System system{std::move(rules), std::move(chassisVec)};
+
+        // Execute sensorMonitoring
+        journal::clear();
+        sensorMonitoringPtr->execute(system, *chassisPtr, *devicePtr, *railPtr);
+        EXPECT_EQ(journal::getDebugMessages().size(), 0);
+        std::vector<std::string> expectedErrMessages{
+            "I2CException: Failed to write byte: bus /dev/i2c-1, addr 0x70",
+            "ActionError: pmbus_read_sensor: { type: iout, command: 0x8C, "
+            "format: linear_11 }",
+            "Unable to monitor sensors for rail vio2"};
+        EXPECT_EQ(journal::getErrMessages(), expectedErrMessages);
+    }
 }
 
 TEST(SensorMonitoringTests, GetActions)