blob: b713454215b93d29248748e96a5370834250612f [file] [log] [blame]
Lei YU01539e72019-07-31 10:57:38 +08001#pragma once
2
3#include "config.h"
4
5#include "activation.hpp"
Lei YU7f2a2152019-09-16 16:50:18 +08006#include "association_interface.hpp"
Lei YU91029442019-08-01 15:57:31 +08007#include "types.hpp"
Lei YU5e0dcb32019-08-02 18:04:34 +08008#include "utils.hpp"
Lei YU01539e72019-07-31 10:57:38 +08009#include "version.hpp"
10
Lei YU58c26e32019-09-27 17:52:06 +080011#include <filesystem>
Lei YU5e0dcb32019-08-02 18:04:34 +080012#include <phosphor-logging/log.hpp>
Lei YU01539e72019-07-31 10:57:38 +080013#include <sdbusplus/server.hpp>
Lei YU91029442019-08-01 15:57:31 +080014#include <xyz/openbmc_project/Association/Definitions/server.hpp>
Lei YU01539e72019-07-31 10:57:38 +080015#include <xyz/openbmc_project/Collection/DeleteAll/server.hpp>
16
Lei YUf77189f2019-08-07 14:26:30 +080017class TestItemUpdater;
18
Lei YU01539e72019-07-31 10:57:38 +080019namespace phosphor
20{
21namespace software
22{
23namespace updater
24{
25
26class Version;
27
28using ItemUpdaterInherit = sdbusplus::server::object::object<
Lei YUa5c47bb2019-09-29 11:28:53 +080029 sdbusplus::xyz::openbmc_project::Association::server::Definitions>;
30
Lei YU01539e72019-07-31 10:57:38 +080031namespace MatchRules = sdbusplus::bus::match::rules;
32
Lei YU58c26e32019-09-27 17:52:06 +080033namespace fs = std::filesystem;
34
Lei YU01539e72019-07-31 10:57:38 +080035/** @class ItemUpdater
36 * @brief Manages the activation of the PSU version items.
37 */
Lei YU7f2a2152019-09-16 16:50:18 +080038class ItemUpdater : public ItemUpdaterInherit, public AssociationInterface
Lei YU01539e72019-07-31 10:57:38 +080039{
Lei YUf77189f2019-08-07 14:26:30 +080040 friend class ::TestItemUpdater;
41
Lei YU01539e72019-07-31 10:57:38 +080042 public:
43 /** @brief Constructs ItemUpdater
44 *
45 * @param[in] bus - The D-Bus bus object
46 * @param[in] path - The D-Bus path
47 */
48 ItemUpdater(sdbusplus::bus::bus& bus, const std::string& path) :
49 ItemUpdaterInherit(bus, path.c_str()), bus(bus),
50 versionMatch(bus,
51 MatchRules::interfacesAdded() +
52 MatchRules::path(SOFTWARE_OBJPATH),
53 std::bind(std::mem_fn(&ItemUpdater::createActivation),
54 this, std::placeholders::_1))
55 {
Lei YUad90ad52019-08-06 11:19:28 +080056 processPSUImage();
Lei YU58c26e32019-09-27 17:52:06 +080057 processStoredImage();
Lei YU63f9e712019-10-12 15:16:55 +080058 syncToLatestImage();
Lei YU01539e72019-07-31 10:57:38 +080059 }
60
61 /** @brief Deletes version
62 *
63 * @param[in] versionId - Id of the version to delete
64 */
Lei YUa5c47bb2019-09-29 11:28:53 +080065 void erase(const std::string& versionId);
Lei YU01539e72019-07-31 10:57:38 +080066
Lei YU91029442019-08-01 15:57:31 +080067 /** @brief Creates an active association to the
68 * newly active software image
69 *
70 * @param[in] path - The path to create the association to.
71 */
Lei YU7f2a2152019-09-16 16:50:18 +080072 void createActiveAssociation(const std::string& path) override;
Lei YU91029442019-08-01 15:57:31 +080073
Lei YUad90ad52019-08-06 11:19:28 +080074 /** @brief Add the functional association to the
Lei YU91029442019-08-01 15:57:31 +080075 * new "running" PSU images
76 *
Lei YUad90ad52019-08-06 11:19:28 +080077 * @param[in] path - The path to add the association to.
Lei YU91029442019-08-01 15:57:31 +080078 */
Lei YU7f2a2152019-09-16 16:50:18 +080079 void addFunctionalAssociation(const std::string& path) override;
Lei YU91029442019-08-01 15:57:31 +080080
81 /** @brief Removes the associations from the provided software image path
82 *
83 * @param[in] path - The path to remove the association from.
84 */
Lei YU7f2a2152019-09-16 16:50:18 +080085 void removeAssociation(const std::string& path) override;
Lei YU91029442019-08-01 15:57:31 +080086
Lei YU7f2a2152019-09-16 16:50:18 +080087 private:
Lei YU01539e72019-07-31 10:57:38 +080088 /** @brief Callback function for Software.Version match.
89 * @details Creates an Activation D-Bus object.
90 *
91 * @param[in] msg - Data associated with subscribed signal
92 */
93 void createActivation(sdbusplus::message::message& msg);
94
Lei YUa2c2cd72019-08-09 15:54:10 +080095 using Properties =
96 std::map<std::string, utils::UtilsInterface::PropertyType>;
97
Lei YUad90ad52019-08-06 11:19:28 +080098 /** @brief Callback function for PSU inventory match.
99 * @details Update an Activation D-Bus object for PSU inventory.
100 *
101 * @param[in] msg - Data associated with subscribed signal
102 */
Lei YUa2c2cd72019-08-09 15:54:10 +0800103 void onPsuInventoryChangedMsg(sdbusplus::message::message& msg);
104
105 /** @brief Callback function for PSU inventory match.
106 * @details Update an Activation D-Bus object for PSU inventory.
107 *
108 * @param[in] psuPath - The PSU inventory path
109 * @param[in] properties - The updated properties
110 */
111 void onPsuInventoryChanged(const std::string& psuPath,
112 const Properties& properties);
Lei YUad90ad52019-08-06 11:19:28 +0800113
Lei YU01539e72019-07-31 10:57:38 +0800114 /** @brief Create Activation object */
115 std::unique_ptr<Activation> createActivationObject(
116 const std::string& path, const std::string& versionId,
Lei YU58c26e32019-09-27 17:52:06 +0800117 const std::string& extVersion, Activation::Status activationStatus,
Lei YU99301372019-09-29 16:27:12 +0800118 const AssociationList& assocs, const std::string& filePath);
Lei YU01539e72019-07-31 10:57:38 +0800119
120 /** @brief Create Version object */
121 std::unique_ptr<Version>
122 createVersionObject(const std::string& objPath,
123 const std::string& versionId,
124 const std::string& versionString,
125 sdbusplus::xyz::openbmc_project::Software::server::
Lei YU99301372019-09-29 16:27:12 +0800126 Version::VersionPurpose versionPurpose);
Lei YU01539e72019-07-31 10:57:38 +0800127
Lei YUbd3b0072019-08-08 13:09:50 +0800128 /** @brief Create Activation and Version object for PSU inventory
129 * @details If the same version exists for multiple PSUs, just add
130 * related association, instead of creating new objects.
131 * */
Lei YUad90ad52019-08-06 11:19:28 +0800132 void createPsuObject(const std::string& psuInventoryPath,
133 const std::string& psuVersion);
134
Lei YUbd3b0072019-08-08 13:09:50 +0800135 /** @brief Remove Activation and Version object for PSU inventory
136 * @details If the same version exists for mutliple PSUs, just remove
137 * related association.
138 * If the version has no association, the Activation and
139 * Version object will be removed
140 */
141 void removePsuObject(const std::string& psuInventoryPath);
142
Lei YUad90ad52019-08-06 11:19:28 +0800143 /**
144 * @brief Create and populate the active PSU Version.
145 */
146 void processPSUImage();
147
Lei YU58c26e32019-09-27 17:52:06 +0800148 /** @brief Create PSU Version from stored images */
149 void processStoredImage();
150
151 /** @brief Scan a directory and create PSU Version from stored images */
152 void scanDirectory(const fs::path& p);
153
Lei YU65207482019-10-11 16:39:36 +0800154 /** @brief Get the versionId of the latest PSU version */
155 std::optional<std::string> getLatestVersionId();
156
Lei YU63f9e712019-10-12 15:16:55 +0800157 /** @brief Update PSUs to the latest version */
158 void syncToLatestImage();
159
160 /** @brief Invoke the activation via DBus */
161 void invokeActivation(const std::unique_ptr<Activation>& activation);
162
Lei YU01539e72019-07-31 10:57:38 +0800163 /** @brief Persistent sdbusplus D-Bus bus connection. */
164 sdbusplus::bus::bus& bus;
165
166 /** @brief Persistent map of Activation D-Bus objects and their
167 * version id */
168 std::map<std::string, std::unique_ptr<Activation>> activations;
169
170 /** @brief Persistent map of Version D-Bus objects and their
171 * version id */
172 std::map<std::string, std::unique_ptr<Version>> versions;
173
Lei YUbd3b0072019-08-08 13:09:50 +0800174 /** @brief The reference map of PSU Inventory objects and the
175 * Activation*/
176 std::map<std::string, const std::unique_ptr<Activation>&>
177 psuPathActivationMap;
178
Lei YUad90ad52019-08-06 11:19:28 +0800179 /** @brief sdbusplus signal match for PSU Software*/
Lei YU01539e72019-07-31 10:57:38 +0800180 sdbusplus::bus::match_t versionMatch;
Lei YU91029442019-08-01 15:57:31 +0800181
Lei YUad90ad52019-08-06 11:19:28 +0800182 /** @brief sdbusplus signal matches for PSU Inventory */
183 std::vector<sdbusplus::bus::match_t> psuMatches;
184
Lei YU91029442019-08-01 15:57:31 +0800185 /** @brief This entry's associations */
186 AssociationList assocs;
Lei YU65207482019-10-11 16:39:36 +0800187
188 /** @brief A collection of the version strings */
189 std::set<std::string> versionStrings;
Lei YU01539e72019-07-31 10:57:38 +0800190};
191
192} // namespace updater
193} // namespace software
194} // namespace phosphor