xref: /openbmc/phosphor-psu-code-mgmt/src/item_updater.hpp (revision a5c47bb3fd28c9178f3573fc49d6db60c43a0480)
101539e7eSLei YU #pragma once
201539e7eSLei YU 
301539e7eSLei YU #include "config.h"
401539e7eSLei YU 
501539e7eSLei YU #include "activation.hpp"
67f2a2152SLei YU #include "association_interface.hpp"
791029448SLei YU #include "types.hpp"
85e0dcb39SLei YU #include "utils.hpp"
901539e7eSLei YU #include "version.hpp"
1001539e7eSLei YU 
115e0dcb39SLei YU #include <phosphor-logging/log.hpp>
1201539e7eSLei YU #include <sdbusplus/server.hpp>
1391029448SLei YU #include <xyz/openbmc_project/Association/Definitions/server.hpp>
1401539e7eSLei YU #include <xyz/openbmc_project/Collection/DeleteAll/server.hpp>
1501539e7eSLei YU 
16f77189f7SLei YU class TestItemUpdater;
17f77189f7SLei YU 
1801539e7eSLei YU namespace phosphor
1901539e7eSLei YU {
2001539e7eSLei YU namespace software
2101539e7eSLei YU {
2201539e7eSLei YU namespace updater
2301539e7eSLei YU {
2401539e7eSLei YU 
2501539e7eSLei YU class Version;
2601539e7eSLei YU 
2701539e7eSLei YU using ItemUpdaterInherit = sdbusplus::server::object::object<
28*a5c47bb3SLei YU     sdbusplus::xyz::openbmc_project::Association::server::Definitions>;
29*a5c47bb3SLei YU 
3001539e7eSLei YU namespace MatchRules = sdbusplus::bus::match::rules;
3101539e7eSLei YU 
3201539e7eSLei YU /** @class ItemUpdater
3301539e7eSLei YU  *  @brief Manages the activation of the PSU version items.
3401539e7eSLei YU  */
357f2a2152SLei YU class ItemUpdater : public ItemUpdaterInherit, public AssociationInterface
3601539e7eSLei YU {
37f77189f7SLei YU     friend class ::TestItemUpdater;
38f77189f7SLei YU 
3901539e7eSLei YU   public:
4001539e7eSLei YU     /** @brief Constructs ItemUpdater
4101539e7eSLei YU      *
4201539e7eSLei YU      * @param[in] bus    - The D-Bus bus object
4301539e7eSLei YU      * @param[in] path   - The D-Bus path
4401539e7eSLei YU      */
4501539e7eSLei YU     ItemUpdater(sdbusplus::bus::bus& bus, const std::string& path) :
4601539e7eSLei YU         ItemUpdaterInherit(bus, path.c_str()), bus(bus),
4701539e7eSLei YU         versionMatch(bus,
4801539e7eSLei YU                      MatchRules::interfacesAdded() +
4901539e7eSLei YU                          MatchRules::path(SOFTWARE_OBJPATH),
5001539e7eSLei YU                      std::bind(std::mem_fn(&ItemUpdater::createActivation),
5101539e7eSLei YU                                this, std::placeholders::_1))
5201539e7eSLei YU     {
53ad90ad51SLei YU         processPSUImage();
5401539e7eSLei YU     }
5501539e7eSLei YU 
5601539e7eSLei YU     /** @brief Deletes version
5701539e7eSLei YU      *
5801539e7eSLei YU      *  @param[in] versionId - Id of the version to delete
5901539e7eSLei YU      */
60*a5c47bb3SLei YU     void erase(const std::string& versionId);
6101539e7eSLei YU 
6291029448SLei YU     /** @brief Creates an active association to the
6391029448SLei YU      *  newly active software image
6491029448SLei YU      *
6591029448SLei YU      * @param[in]  path - The path to create the association to.
6691029448SLei YU      */
677f2a2152SLei YU     void createActiveAssociation(const std::string& path) override;
6891029448SLei YU 
69ad90ad51SLei YU     /** @brief Add the functional association to the
7091029448SLei YU      *  new "running" PSU images
7191029448SLei YU      *
72ad90ad51SLei YU      * @param[in]  path - The path to add the association to.
7391029448SLei YU      */
747f2a2152SLei YU     void addFunctionalAssociation(const std::string& path) override;
7591029448SLei YU 
7691029448SLei YU     /** @brief Removes the associations from the provided software image path
7791029448SLei YU      *
7891029448SLei YU      * @param[in]  path - The path to remove the association from.
7991029448SLei YU      */
807f2a2152SLei YU     void removeAssociation(const std::string& path) override;
8191029448SLei YU 
827f2a2152SLei YU   private:
8301539e7eSLei YU     /** @brief Callback function for Software.Version match.
8401539e7eSLei YU      *  @details Creates an Activation D-Bus object.
8501539e7eSLei YU      *
8601539e7eSLei YU      * @param[in]  msg       - Data associated with subscribed signal
8701539e7eSLei YU      */
8801539e7eSLei YU     void createActivation(sdbusplus::message::message& msg);
8901539e7eSLei YU 
90a2c2cd72SLei YU     using Properties =
91a2c2cd72SLei YU         std::map<std::string, utils::UtilsInterface::PropertyType>;
92a2c2cd72SLei YU 
93ad90ad51SLei YU     /** @brief Callback function for PSU inventory match.
94ad90ad51SLei YU      *  @details Update an Activation D-Bus object for PSU inventory.
95ad90ad51SLei YU      *
96ad90ad51SLei YU      * @param[in]  msg       - Data associated with subscribed signal
97ad90ad51SLei YU      */
98a2c2cd72SLei YU     void onPsuInventoryChangedMsg(sdbusplus::message::message& msg);
99a2c2cd72SLei YU 
100a2c2cd72SLei YU     /** @brief Callback function for PSU inventory match.
101a2c2cd72SLei YU      *  @details Update an Activation D-Bus object for PSU inventory.
102a2c2cd72SLei YU      *
103a2c2cd72SLei YU      * @param[in]  psuPath - The PSU inventory path
104a2c2cd72SLei YU      * @param[in]  properties - The updated properties
105a2c2cd72SLei YU      */
106a2c2cd72SLei YU     void onPsuInventoryChanged(const std::string& psuPath,
107a2c2cd72SLei YU                                const Properties& properties);
108ad90ad51SLei YU 
10901539e7eSLei YU     /** @brief Create Activation object */
11001539e7eSLei YU     std::unique_ptr<Activation> createActivationObject(
11101539e7eSLei YU         const std::string& path, const std::string& versionId,
11201539e7eSLei YU         const std::string& extVersion,
11301539e7eSLei YU         sdbusplus::xyz::openbmc_project::Software::server::Activation::
11491029448SLei YU             Activations activationStatus,
11591029448SLei YU         const AssociationList& assocs);
11601539e7eSLei YU 
11701539e7eSLei YU     /** @brief Create Version object */
11801539e7eSLei YU     std::unique_ptr<Version>
11901539e7eSLei YU         createVersionObject(const std::string& objPath,
12001539e7eSLei YU                             const std::string& versionId,
12101539e7eSLei YU                             const std::string& versionString,
12201539e7eSLei YU                             sdbusplus::xyz::openbmc_project::Software::server::
12301539e7eSLei YU                                 Version::VersionPurpose versionPurpose,
12401539e7eSLei YU                             const std::string& filePath);
12501539e7eSLei YU 
126bd3b0076SLei YU     /** @brief Create Activation and Version object for PSU inventory
127bd3b0076SLei YU      *  @details If the same version exists for multiple PSUs, just add
128bd3b0076SLei YU      *           related association, instead of creating new objects.
129bd3b0076SLei YU      * */
130ad90ad51SLei YU     void createPsuObject(const std::string& psuInventoryPath,
131ad90ad51SLei YU                          const std::string& psuVersion);
132ad90ad51SLei YU 
133bd3b0076SLei YU     /** @brief Remove Activation and Version object for PSU inventory
134bd3b0076SLei YU      *  @details If the same version exists for mutliple PSUs, just remove
135bd3b0076SLei YU      *           related association.
136bd3b0076SLei YU      *           If the version has no association, the Activation and
137bd3b0076SLei YU      *           Version object will be removed
138bd3b0076SLei YU      */
139bd3b0076SLei YU     void removePsuObject(const std::string& psuInventoryPath);
140bd3b0076SLei YU 
141ad90ad51SLei YU     /**
142ad90ad51SLei YU      * @brief Create and populate the active PSU Version.
143ad90ad51SLei YU      */
144ad90ad51SLei YU     void processPSUImage();
145ad90ad51SLei YU 
14601539e7eSLei YU     /** @brief Persistent sdbusplus D-Bus bus connection. */
14701539e7eSLei YU     sdbusplus::bus::bus& bus;
14801539e7eSLei YU 
14901539e7eSLei YU     /** @brief Persistent map of Activation D-Bus objects and their
15001539e7eSLei YU      * version id */
15101539e7eSLei YU     std::map<std::string, std::unique_ptr<Activation>> activations;
15201539e7eSLei YU 
15301539e7eSLei YU     /** @brief Persistent map of Version D-Bus objects and their
15401539e7eSLei YU      * version id */
15501539e7eSLei YU     std::map<std::string, std::unique_ptr<Version>> versions;
15601539e7eSLei YU 
157bd3b0076SLei YU     /** @brief The reference map of PSU Inventory objects and the
158bd3b0076SLei YU      * Activation*/
159bd3b0076SLei YU     std::map<std::string, const std::unique_ptr<Activation>&>
160bd3b0076SLei YU         psuPathActivationMap;
161bd3b0076SLei YU 
162ad90ad51SLei YU     /** @brief sdbusplus signal match for PSU Software*/
16301539e7eSLei YU     sdbusplus::bus::match_t versionMatch;
16491029448SLei YU 
165ad90ad51SLei YU     /** @brief sdbusplus signal matches for PSU Inventory */
166ad90ad51SLei YU     std::vector<sdbusplus::bus::match_t> psuMatches;
167ad90ad51SLei YU 
16891029448SLei YU     /** @brief This entry's associations */
16991029448SLei YU     AssociationList assocs;
17001539e7eSLei YU };
17101539e7eSLei YU 
17201539e7eSLei YU } // namespace updater
17301539e7eSLei YU } // namespace software
17401539e7eSLei YU } // namespace phosphor
175