blob: 0477e6092ea9a5745fd0b81ab4f79c54ed486528 [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 YUffb36532019-10-15 13:55:24 +080038class ItemUpdater : public ItemUpdaterInherit,
39 public AssociationInterface,
40 public ActivationListener
Lei YU01539e72019-07-31 10:57:38 +080041{
Lei YUf77189f2019-08-07 14:26:30 +080042 friend class ::TestItemUpdater;
43
Lei YU01539e72019-07-31 10:57:38 +080044 public:
45 /** @brief Constructs ItemUpdater
46 *
47 * @param[in] bus - The D-Bus bus object
48 * @param[in] path - The D-Bus path
49 */
50 ItemUpdater(sdbusplus::bus::bus& bus, const std::string& path) :
51 ItemUpdaterInherit(bus, path.c_str()), bus(bus),
52 versionMatch(bus,
53 MatchRules::interfacesAdded() +
54 MatchRules::path(SOFTWARE_OBJPATH),
55 std::bind(std::mem_fn(&ItemUpdater::createActivation),
56 this, std::placeholders::_1))
57 {
Lei YUad90ad52019-08-06 11:19:28 +080058 processPSUImage();
Lei YU58c26e32019-09-27 17:52:06 +080059 processStoredImage();
Lei YU63f9e712019-10-12 15:16:55 +080060 syncToLatestImage();
Lei YU01539e72019-07-31 10:57:38 +080061 }
62
63 /** @brief Deletes version
64 *
65 * @param[in] versionId - Id of the version to delete
66 */
Lei YUa5c47bb2019-09-29 11:28:53 +080067 void erase(const std::string& versionId);
Lei YU01539e72019-07-31 10:57:38 +080068
Lei YU91029442019-08-01 15:57:31 +080069 /** @brief Creates an active association to the
70 * newly active software image
71 *
72 * @param[in] path - The path to create the association to.
73 */
Lei YU7f2a2152019-09-16 16:50:18 +080074 void createActiveAssociation(const std::string& path) override;
Lei YU91029442019-08-01 15:57:31 +080075
Lei YUad90ad52019-08-06 11:19:28 +080076 /** @brief Add the functional association to the
Lei YU91029442019-08-01 15:57:31 +080077 * new "running" PSU images
78 *
Lei YUad90ad52019-08-06 11:19:28 +080079 * @param[in] path - The path to add the association to.
Lei YU91029442019-08-01 15:57:31 +080080 */
Lei YU7f2a2152019-09-16 16:50:18 +080081 void addFunctionalAssociation(const std::string& path) override;
Lei YU91029442019-08-01 15:57:31 +080082
83 /** @brief Removes the associations from the provided software image path
84 *
85 * @param[in] path - The path to remove the association from.
86 */
Lei YU7f2a2152019-09-16 16:50:18 +080087 void removeAssociation(const std::string& path) override;
Lei YU91029442019-08-01 15:57:31 +080088
Lei YUffb36532019-10-15 13:55:24 +080089 /** @brief Notify a PSU is updated
90 *
91 * @param[in] versionId - The versionId of the activation
92 * @param[in] psuInventoryPath - The PSU inventory path that is updated
93 */
94 void onUpdateDone(const std::string& versionId,
95 const std::string& psuInventoryPath) override;
96
Lei YU7f2a2152019-09-16 16:50:18 +080097 private:
Lei YU01539e72019-07-31 10:57:38 +080098 /** @brief Callback function for Software.Version match.
99 * @details Creates an Activation D-Bus object.
100 *
101 * @param[in] msg - Data associated with subscribed signal
102 */
103 void createActivation(sdbusplus::message::message& msg);
104
Lei YUa2c2cd72019-08-09 15:54:10 +0800105 using Properties =
106 std::map<std::string, utils::UtilsInterface::PropertyType>;
107
Lei YUad90ad52019-08-06 11:19:28 +0800108 /** @brief Callback function for PSU inventory match.
109 * @details Update an Activation D-Bus object for PSU inventory.
110 *
111 * @param[in] msg - Data associated with subscribed signal
112 */
Lei YUa2c2cd72019-08-09 15:54:10 +0800113 void onPsuInventoryChangedMsg(sdbusplus::message::message& msg);
114
115 /** @brief Callback function for PSU inventory match.
116 * @details Update an Activation D-Bus object for PSU inventory.
117 *
118 * @param[in] psuPath - The PSU inventory path
119 * @param[in] properties - The updated properties
120 */
121 void onPsuInventoryChanged(const std::string& psuPath,
122 const Properties& properties);
Lei YUad90ad52019-08-06 11:19:28 +0800123
Lei YU01539e72019-07-31 10:57:38 +0800124 /** @brief Create Activation object */
125 std::unique_ptr<Activation> createActivationObject(
126 const std::string& path, const std::string& versionId,
Lei YU58c26e32019-09-27 17:52:06 +0800127 const std::string& extVersion, Activation::Status activationStatus,
Lei YU99301372019-09-29 16:27:12 +0800128 const AssociationList& assocs, const std::string& filePath);
Lei YU01539e72019-07-31 10:57:38 +0800129
130 /** @brief Create Version object */
131 std::unique_ptr<Version>
132 createVersionObject(const std::string& objPath,
133 const std::string& versionId,
134 const std::string& versionString,
135 sdbusplus::xyz::openbmc_project::Software::server::
Lei YU99301372019-09-29 16:27:12 +0800136 Version::VersionPurpose versionPurpose);
Lei YU01539e72019-07-31 10:57:38 +0800137
Lei YUbd3b0072019-08-08 13:09:50 +0800138 /** @brief Create Activation and Version object for PSU inventory
139 * @details If the same version exists for multiple PSUs, just add
140 * related association, instead of creating new objects.
141 * */
Lei YUad90ad52019-08-06 11:19:28 +0800142 void createPsuObject(const std::string& psuInventoryPath,
143 const std::string& psuVersion);
144
Lei YUbd3b0072019-08-08 13:09:50 +0800145 /** @brief Remove Activation and Version object for PSU inventory
146 * @details If the same version exists for mutliple PSUs, just remove
147 * related association.
148 * If the version has no association, the Activation and
149 * Version object will be removed
150 */
151 void removePsuObject(const std::string& psuInventoryPath);
152
Lei YUad90ad52019-08-06 11:19:28 +0800153 /**
154 * @brief Create and populate the active PSU Version.
155 */
156 void processPSUImage();
157
Lei YU58c26e32019-09-27 17:52:06 +0800158 /** @brief Create PSU Version from stored images */
159 void processStoredImage();
160
161 /** @brief Scan a directory and create PSU Version from stored images */
162 void scanDirectory(const fs::path& p);
163
Lei YU65207482019-10-11 16:39:36 +0800164 /** @brief Get the versionId of the latest PSU version */
165 std::optional<std::string> getLatestVersionId();
166
Lei YU63f9e712019-10-12 15:16:55 +0800167 /** @brief Update PSUs to the latest version */
168 void syncToLatestImage();
169
170 /** @brief Invoke the activation via DBus */
171 void invokeActivation(const std::unique_ptr<Activation>& activation);
172
Lei YU01539e72019-07-31 10:57:38 +0800173 /** @brief Persistent sdbusplus D-Bus bus connection. */
174 sdbusplus::bus::bus& bus;
175
176 /** @brief Persistent map of Activation D-Bus objects and their
177 * version id */
178 std::map<std::string, std::unique_ptr<Activation>> activations;
179
180 /** @brief Persistent map of Version D-Bus objects and their
181 * version id */
182 std::map<std::string, std::unique_ptr<Version>> versions;
183
Lei YUbd3b0072019-08-08 13:09:50 +0800184 /** @brief The reference map of PSU Inventory objects and the
185 * Activation*/
186 std::map<std::string, const std::unique_ptr<Activation>&>
187 psuPathActivationMap;
188
Lei YUad90ad52019-08-06 11:19:28 +0800189 /** @brief sdbusplus signal match for PSU Software*/
Lei YU01539e72019-07-31 10:57:38 +0800190 sdbusplus::bus::match_t versionMatch;
Lei YU91029442019-08-01 15:57:31 +0800191
Lei YUad90ad52019-08-06 11:19:28 +0800192 /** @brief sdbusplus signal matches for PSU Inventory */
193 std::vector<sdbusplus::bus::match_t> psuMatches;
194
Lei YU91029442019-08-01 15:57:31 +0800195 /** @brief This entry's associations */
196 AssociationList assocs;
Lei YU65207482019-10-11 16:39:36 +0800197
198 /** @brief A collection of the version strings */
199 std::set<std::string> versionStrings;
Lei YU1517f5f2019-10-14 16:44:42 +0800200
201 /** @brief A struct to hold the PSU present status and model */
202 struct psuStatus
203 {
204 bool present;
205 std::string model;
206 };
207
208 /** @brief The map of PSU inventory path and the psuStatus
209 *
210 * It is used to handle psu inventory changed event, that only create psu
211 * software object when a PSU is present and the model is retrieved */
212 std::map<std::string, psuStatus> psuStatusMap;
Lei YU01539e72019-07-31 10:57:38 +0800213};
214
215} // namespace updater
216} // namespace software
217} // namespace phosphor