1 #pragma once
2 
3 #include "config.h"
4 
5 #include "activation.hpp"
6 #include "association_interface.hpp"
7 #include "types.hpp"
8 #include "utils.hpp"
9 #include "version.hpp"
10 
11 #include <filesystem>
12 #include <phosphor-logging/log.hpp>
13 #include <sdbusplus/server.hpp>
14 #include <xyz/openbmc_project/Association/Definitions/server.hpp>
15 #include <xyz/openbmc_project/Collection/DeleteAll/server.hpp>
16 
17 class TestItemUpdater;
18 
19 namespace phosphor
20 {
21 namespace software
22 {
23 namespace updater
24 {
25 
26 class Version;
27 
28 using ItemUpdaterInherit = sdbusplus::server::object::object<
29     sdbusplus::xyz::openbmc_project::Association::server::Definitions>;
30 
31 namespace MatchRules = sdbusplus::bus::match::rules;
32 
33 namespace fs = std::filesystem;
34 
35 /** @class ItemUpdater
36  *  @brief Manages the activation of the PSU version items.
37  */
38 class ItemUpdater : public ItemUpdaterInherit, public AssociationInterface
39 {
40     friend class ::TestItemUpdater;
41 
42   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     {
56         processPSUImage();
57         processStoredImage();
58         getLatestVersionId();
59     }
60 
61     /** @brief Deletes version
62      *
63      *  @param[in] versionId - Id of the version to delete
64      */
65     void erase(const std::string& versionId);
66 
67     /** @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      */
72     void createActiveAssociation(const std::string& path) override;
73 
74     /** @brief Add the functional association to the
75      *  new "running" PSU images
76      *
77      * @param[in]  path - The path to add the association to.
78      */
79     void addFunctionalAssociation(const std::string& path) override;
80 
81     /** @brief Removes the associations from the provided software image path
82      *
83      * @param[in]  path - The path to remove the association from.
84      */
85     void removeAssociation(const std::string& path) override;
86 
87   private:
88     /** @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 
95     using Properties =
96         std::map<std::string, utils::UtilsInterface::PropertyType>;
97 
98     /** @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      */
103     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);
113 
114     /** @brief Create Activation object */
115     std::unique_ptr<Activation> createActivationObject(
116         const std::string& path, const std::string& versionId,
117         const std::string& extVersion, Activation::Status activationStatus,
118         const AssociationList& assocs, const std::string& filePath);
119 
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::
126                                 Version::VersionPurpose versionPurpose);
127 
128     /** @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      * */
132     void createPsuObject(const std::string& psuInventoryPath,
133                          const std::string& psuVersion);
134 
135     /** @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 
143     /**
144      * @brief Create and populate the active PSU Version.
145      */
146     void processPSUImage();
147 
148     /** @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 
154     /** @brief Get the versionId of the latest PSU version */
155     std::optional<std::string> getLatestVersionId();
156 
157     /** @brief Persistent sdbusplus D-Bus bus connection. */
158     sdbusplus::bus::bus& bus;
159 
160     /** @brief Persistent map of Activation D-Bus objects and their
161      * version id */
162     std::map<std::string, std::unique_ptr<Activation>> activations;
163 
164     /** @brief Persistent map of Version D-Bus objects and their
165      * version id */
166     std::map<std::string, std::unique_ptr<Version>> versions;
167 
168     /** @brief The reference map of PSU Inventory objects and the
169      * Activation*/
170     std::map<std::string, const std::unique_ptr<Activation>&>
171         psuPathActivationMap;
172 
173     /** @brief sdbusplus signal match for PSU Software*/
174     sdbusplus::bus::match_t versionMatch;
175 
176     /** @brief sdbusplus signal matches for PSU Inventory */
177     std::vector<sdbusplus::bus::match_t> psuMatches;
178 
179     /** @brief This entry's associations */
180     AssociationList assocs;
181 
182     /** @brief A collection of the version strings */
183     std::set<std::string> versionStrings;
184 };
185 
186 } // namespace updater
187 } // namespace software
188 } // namespace phosphor
189