control: Update base action object with zones and groups used
The base action object stores the groups and zones configured for an
action to use and run against. When a trigger fires, it will run the
action against each zone configured. Each action will have its list of
groups to use that it can iterate over appropriately. This will be
updated in the next commit to no longer pass a reference to a group when
running an action and to have each action iterate over the groups
configured for them to use.
Change-Id: Ifcd1a260e38af87c5f3bb2a3d44813e71743abc6
Signed-off-by: Matthew Barth <msbarth@us.ibm.com>
diff --git a/control/json/actions/action.hpp b/control/json/actions/action.hpp
index 05fc874..0369ec9 100644
--- a/control/json/actions/action.hpp
+++ b/control/json/actions/action.hpp
@@ -74,13 +74,21 @@
* @brief Function used in creating action objects
*
* @param[in] jsonObj - JSON object for the action
+ * @param[in] groups - Groups of dbus objects the action uses
+ * @param[in] zones - Zones the action runs against
*
- * Creates an action object given the JSON configuration
+ * Creates an action object given the JSON configuration, list of groups and
+ * sets the zones the action should run against.
*/
template <typename T>
-std::unique_ptr<T> createAction(const json& jsonObj)
+std::unique_ptr<T>
+ createAction(const json& jsonObj, const std::vector<Group>& groups,
+ std::vector<std::reference_wrapper<Zone>>& zones)
{
- return std::make_unique<T>(jsonObj);
+ // Create the action and set its list of zones
+ auto action = std::make_unique<T>(jsonObj, groups);
+ action->setZones(zones);
+ return action;
}
/**
@@ -101,17 +109,31 @@
/**
* @brief Base action object
*
+ * @param[in] jsonObj - JSON object containing name and any profiles
+ * @param[in] groups - Groups of dbus objects the action uses
+ *
* All actions derived from this base action object must be given a name
* that uniquely identifies the action. Optionally, a configured action can
* have a list of explicit profiles it should be included in, otherwise
* always include the action where no profiles are given.
- *
- * @param[in] jsonObj - JSON object containing name and any profiles
*/
- explicit ActionBase(const json& jsonObj) : ConfigBase(jsonObj)
+ ActionBase(const json& jsonObj, const std::vector<Group>& groups) :
+ ConfigBase(jsonObj), _groups(groups)
{}
/**
+ * @brief Set the zones the action is run against
+ *
+ * @param[in] zones - Zones the action runs against
+ *
+ * By default, the zones are set when the action object is created
+ */
+ virtual void setZones(std::vector<std::reference_wrapper<Zone>>& zones)
+ {
+ _zones = zones;
+ }
+
+ /**
* @brief Run the action
*
* Run the action function associated to the derived action object
@@ -122,6 +144,27 @@
* @param[in] group - Group of dbus objects the action runs against
*/
virtual void run(Zone& zone, const Group& group) = 0;
+
+ /**
+ * @brief Trigger the action to run against all of its zones
+ *
+ * This is the function used by triggers to run the actions against all the
+ * zones that were configured for the action to run against.
+ */
+ void run()
+ {
+ // TODO Remove passing a reference of a group to run the action
+ std::for_each(_zones.begin(), _zones.end(),
+ [this](Zone& zone) { this->run(zone, _groups.front()); });
+ }
+
+ protected:
+ /* Groups configured on the action */
+ const std::vector<Group> _groups;
+
+ private:
+ /* Zones configured on the action */
+ std::vector<std::reference_wrapper<Zone>> _zones;
};
/**
@@ -180,6 +223,8 @@
*
* @param[in] name - Name of the action to create/get
* @param[in] jsonObj - JSON object for the action
+ * @param[in] groups - Groups of dbus objects the action uses
+ * @param[in] zones - Zones the action runs against
*
* @return Pointer to the action object.
*/
@@ -191,7 +236,7 @@
auto it = actions.find(name);
if (it != actions.end())
{
- return it->second(jsonObj);
+ return it->second(jsonObj, groups, zones);
}
else
{
@@ -210,8 +255,10 @@
private:
/* Map to store the available actions and their creation functions */
- static inline std::map<
- std::string, std::function<std::unique_ptr<ActionBase>(const json&)>>
+ static inline std::map<std::string,
+ std::function<std::unique_ptr<ActionBase>(
+ const json&, const std::vector<Group>&,
+ std::vector<std::reference_wrapper<Zone>>&)>>
actions;
};
diff --git a/control/json/actions/count_state_target.cpp b/control/json/actions/count_state_target.cpp
index 432137b..3bbcd02 100644
--- a/control/json/actions/count_state_target.cpp
+++ b/control/json/actions/count_state_target.cpp
@@ -29,7 +29,9 @@
using json = nlohmann::json;
-CountStateTarget::CountStateTarget(const json& jsonObj) : ActionBase(jsonObj)
+CountStateTarget::CountStateTarget(const json& jsonObj,
+ const std::vector<Group>& groups) :
+ ActionBase(jsonObj, groups)
{
setCount(jsonObj);
setState(jsonObj);
diff --git a/control/json/actions/count_state_target.hpp b/control/json/actions/count_state_target.hpp
index a17f44a..8493844 100644
--- a/control/json/actions/count_state_target.hpp
+++ b/control/json/actions/count_state_target.hpp
@@ -53,8 +53,9 @@
* @brief Set target when a number of members are at a given state
*
* @param[in] jsonObj - JSON configuration of this action
+ * @param[in] groups - Groups of dbus objects the action uses
*/
- explicit CountStateTarget(const json& jsonObj);
+ CountStateTarget(const json& jsonObj, const std::vector<Group>& groups);
/**
* @brief Run the action
diff --git a/control/json/actions/default_floor.cpp b/control/json/actions/default_floor.cpp
index 18c434e..360cc17 100644
--- a/control/json/actions/default_floor.cpp
+++ b/control/json/actions/default_floor.cpp
@@ -28,7 +28,9 @@
using json = nlohmann::json;
-DefaultFloor::DefaultFloor(const json& jsonObj) : ActionBase(jsonObj)
+DefaultFloor::DefaultFloor(const json& jsonObj,
+ const std::vector<Group>& groups) :
+ ActionBase(jsonObj, groups)
{
// There are no JSON configuration parameters for this action
}
diff --git a/control/json/actions/default_floor.hpp b/control/json/actions/default_floor.hpp
index f5c994b..3c84bcc 100644
--- a/control/json/actions/default_floor.hpp
+++ b/control/json/actions/default_floor.hpp
@@ -50,8 +50,9 @@
* @brief Default the fan floor
*
* @param[in] jsonObj - JSON configuration of this action
+ * @param[in] groups - Groups of dbus objects the action uses
*/
- explicit DefaultFloor(const json& jsonObj);
+ DefaultFloor(const json& jsonObj, const std::vector<Group>& groups);
/**
* @brief Run the action
diff --git a/control/json/actions/missing_owner_target.cpp b/control/json/actions/missing_owner_target.cpp
index 3b081b0..4ba33ab 100644
--- a/control/json/actions/missing_owner_target.cpp
+++ b/control/json/actions/missing_owner_target.cpp
@@ -32,8 +32,9 @@
using json = nlohmann::json;
using namespace phosphor::logging;
-MissingOwnerTarget::MissingOwnerTarget(const json& jsonObj) :
- ActionBase(jsonObj)
+MissingOwnerTarget::MissingOwnerTarget(const json& jsonObj,
+ const std::vector<Group>& groups) :
+ ActionBase(jsonObj, groups)
{
setTarget(jsonObj);
}
diff --git a/control/json/actions/missing_owner_target.hpp b/control/json/actions/missing_owner_target.hpp
index 0bf6c84..ccbdf9c 100644
--- a/control/json/actions/missing_owner_target.hpp
+++ b/control/json/actions/missing_owner_target.hpp
@@ -52,8 +52,9 @@
* @brief Set target on an owner missing
*
* @param[in] jsonObj - JSON configuration of this action
+ * @param[in] groups - Groups of dbus objects the action uses
*/
- explicit MissingOwnerTarget(const json& jsonObj);
+ MissingOwnerTarget(const json& jsonObj, const std::vector<Group>& groups);
/**
* @brief Run the action
diff --git a/control/json/actions/net_target_decrease.cpp b/control/json/actions/net_target_decrease.cpp
index 4664238..6fcd1e9 100644
--- a/control/json/actions/net_target_decrease.cpp
+++ b/control/json/actions/net_target_decrease.cpp
@@ -34,7 +34,9 @@
using json = nlohmann::json;
using namespace phosphor::logging;
-NetTargetDecrease::NetTargetDecrease(const json& jsonObj) : ActionBase(jsonObj)
+NetTargetDecrease::NetTargetDecrease(const json& jsonObj,
+ const std::vector<Group>& groups) :
+ ActionBase(jsonObj, groups)
{
setState(jsonObj);
setDelta(jsonObj);
diff --git a/control/json/actions/net_target_decrease.hpp b/control/json/actions/net_target_decrease.hpp
index cc09705..0346cf2 100644
--- a/control/json/actions/net_target_decrease.hpp
+++ b/control/json/actions/net_target_decrease.hpp
@@ -56,8 +56,9 @@
* @brief Determine/Set the net target decrease
*
* @param[in] jsonObj - JSON configuration of this action
+ * @param[in] groups - Groups of dbus objects the action uses
*/
- explicit NetTargetDecrease(const json& jsonObj);
+ NetTargetDecrease(const json& jsonObj, const std::vector<Group>& groups);
/**
* @brief Run the action
diff --git a/control/json/actions/net_target_increase.cpp b/control/json/actions/net_target_increase.cpp
index ccfd897..2240121 100644
--- a/control/json/actions/net_target_increase.cpp
+++ b/control/json/actions/net_target_increase.cpp
@@ -34,7 +34,9 @@
using json = nlohmann::json;
using namespace phosphor::logging;
-NetTargetIncrease::NetTargetIncrease(const json& jsonObj) : ActionBase(jsonObj)
+NetTargetIncrease::NetTargetIncrease(const json& jsonObj,
+ const std::vector<Group>& groups) :
+ ActionBase(jsonObj, groups)
{
setState(jsonObj);
setDelta(jsonObj);
diff --git a/control/json/actions/net_target_increase.hpp b/control/json/actions/net_target_increase.hpp
index 8c6a043..7992695 100644
--- a/control/json/actions/net_target_increase.hpp
+++ b/control/json/actions/net_target_increase.hpp
@@ -56,8 +56,9 @@
* @brief Determine/Set the net target increase
*
* @param[in] jsonObj - JSON configuration of this action
+ * @param[in] groups - Groups of dbus objects the action uses
*/
- explicit NetTargetIncrease(const json& jsonObj);
+ NetTargetIncrease(const json& jsonObj, const std::vector<Group>& groups);
/**
* @brief Run the action
diff --git a/control/json/actions/request_target_base.cpp b/control/json/actions/request_target_base.cpp
index 5f53de3..5000ea8 100644
--- a/control/json/actions/request_target_base.cpp
+++ b/control/json/actions/request_target_base.cpp
@@ -32,7 +32,9 @@
using json = nlohmann::json;
using namespace phosphor::logging;
-RequestTargetBase::RequestTargetBase(const json& jsonObj) : ActionBase(jsonObj)
+RequestTargetBase::RequestTargetBase(const json& jsonObj,
+ const std::vector<Group>& groups) :
+ ActionBase(jsonObj, groups)
{
// There are no JSON configuration parameters for this action
}
diff --git a/control/json/actions/request_target_base.hpp b/control/json/actions/request_target_base.hpp
index 85e5ed9..289afdc 100644
--- a/control/json/actions/request_target_base.hpp
+++ b/control/json/actions/request_target_base.hpp
@@ -55,8 +55,9 @@
* @brief Update the requested target base
*
* @param[in] jsonObj - JSON configuration of this action
+ * @param[in] groups - Groups of dbus objects the action uses
*/
- explicit RequestTargetBase(const json& jsonObj);
+ RequestTargetBase(const json& jsonObj, const std::vector<Group>& groups);
/**
* @brief Run the action
diff --git a/control/json/manager.cpp b/control/json/manager.cpp
index b85bd32..d02f0f5 100644
--- a/control/json/manager.cpp
+++ b/control/json/manager.cpp
@@ -194,7 +194,7 @@
std::for_each(actions.begin(), actions.end(),
[zone = std::ref(std::get<Zone&>(data.second)),
group = std::cref(std::get<const Group&>(data.second))](
- auto& action) { action->run(zone, group); });
+ auto& action) { action->run(); });
// Remove oneshot timers after they expired
if (data.first == TimerType::oneshot)