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;
};