xref: /openbmc/openpower-vpd-parser/vpd-manager/include/backup_restore.hpp (revision eea9c20de4496258b4dd928be12d88b2448d94ee)
1 #pragma once
2 
3 #include "logger.hpp"
4 #include "types.hpp"
5 
6 #include <nlohmann/json.hpp>
7 
8 #include <tuple>
9 
10 namespace vpd
11 {
12 
13 // Backup and restore operation status.
14 enum class BackupAndRestoreStatus : uint8_t
15 {
16     NotStarted,
17     Invoked,
18     Completed
19 };
20 
21 /**
22  * @brief class to implement backup and restore VPD.
23  *
24  */
25 
26 class BackupAndRestore
27 {
28   public:
29     // delete functions
30     BackupAndRestore() = delete;
31     BackupAndRestore(const BackupAndRestore&) = delete;
32     BackupAndRestore& operator=(const BackupAndRestore&) = delete;
33     BackupAndRestore(BackupAndRestore&&) = delete;
34     BackupAndRestore& operator=(BackupAndRestore&&) = delete;
35 
36     /**
37      * @brief Constructor.
38      *
39      * @param[in] i_sysCfgJsonObj - System config JSON object.
40      *
41      * @throw std::runtime_error in case constructor failure.
42      */
43     BackupAndRestore(const nlohmann::json& i_sysCfgJsonObj);
44 
45     /**
46      * @brief Default destructor.
47      */
48     ~BackupAndRestore() = default;
49 
50     /**
51      * @brief An API to backup and restore VPD.
52      *
53      * Note: This API works on the keywords declared in the backup and restore
54      * config JSON. Restore or backup action could be triggered for each
55      * keyword, based on the keyword's value present in the source and
56      * destination keyword.
57      *
58      * Restore source keyword's value with destination keyword's value,
59      * when source keyword has default value but
60      * destination's keyword has non default value.
61      *
62      * Backup the source keyword value to the destination's keyword's value,
63      * when source keyword has non default value but
64      * destination's keyword has default value.
65      *
66      * @return Tuple of updated source and destination VPD map variant.
67      */
68     std::tuple<types::VPDMapVariant, types::VPDMapVariant> backupAndRestore();
69 
70     /**
71      * @brief An API to set backup and restore status.
72      *
73      * @param[in] i_status - Status to set.
74      */
75     static void setBackupAndRestoreStatus(
76         const BackupAndRestoreStatus& i_status);
77 
78     /**
79      * @brief An API to update keyword's value on primary or backup path.
80      *
81      * Updates the keyword's value based on the following,
82      * 1. If provided i_fruPath is primary(source) path in the backup restore
83      * config JSON, then API updates VPD on the backup(destination) path.
84      * 2. If i_fruPath is backup path, then API updates the VPD on the
85      * primary path.
86      *
87      * Note: The above condition is only valid,
88      * 1. If system's primary & backup VPD is on EEPROM path(and should be found
89      * in the backup and restore config JSON).
90      * 2. If the input record and keyword are found in the backup and restore
91      * config JSON.
92      *
93      * @param[in] i_fruPath - EEPROM path of the FRU.
94      * @param[in] i_paramsToWriteData - Input details.
95      *
96      * @return On success returns number of bytes written, -1 on failure.
97      */
98     int updateKeywordOnPrimaryOrBackupPath(
99         const std::string& i_fruPath,
100         const types::WriteVpdParams& i_paramsToWriteData) const noexcept;
101 
102   private:
103     /**
104      * @brief An API to handle backup and restore of IPZ type VPD.
105      *
106      * @param[in,out] io_srcVpdMap - Source VPD map.
107      * @param[in,out] io_dstVpdMap - Destination VPD map.
108      *
109      * @throw std::runtime_error
110      */
111     void backupAndRestoreIpzVpd(types::IPZVpdMap& io_srcVpdMap,
112                                 types::IPZVpdMap& io_dstVpdMap);
113 
114     /**
115      * @brief Get source and destination dbus service name.
116      *
117      * This API extracts the source and destination Dbus service names from the
118      * system configuration JSON using the inventory paths stored in
119      * m_srcInvPath and m_dstInvPath.
120      *
121      * @param[out] o_srcServiceName - Source service name.
122      * @param[out] o_dstServiceName - Destination service name.
123      *
124      * @return A tuple containing the source and destination Dbus service name
125      * on successful retrieval, or an empty tuple otherwise..
126      */
127     std::tuple<std::string, std::string> getSrcAndDstServiceName()
128         const noexcept;
129 
130     /**
131      * @brief Retrieve EEPROM and inventory object paths.
132      *
133      * This API retrieves the EEPROM and inventory object paths for the given
134      * location (source or destination) from the backup and restore
135      * configuration and the system configuration JSONs.
136      *
137      * @param[in] i_location - Source or destination location.
138      *
139      * @return A tuple containing the EEPROM and inventory paths on successful
140      *         retrieval, or an empty tuple otherwise.
141      */
142     types::EepromInventoryPaths getFruAndInvPaths(
143         const std::string& i_location) const noexcept;
144 
145     /**
146      * @brief Extract and validate IPZ type record details.
147      *
148      * This API extracts the source and destination record name, keyword name,
149      * and default value from the input JSON object. It also validates that the
150      * extracted source and destination records are present in the provided VPD
151      * maps only when the maps are provided.
152      *
153      * @param[in] i_aRecordKwInfo - Json object containing record and keyword
154      * details.
155      * @param[out] o_srcDstRecordKeywordInfo - Tuple containing (source record
156      * name, source keyword name, destination record name, destination keyword
157      * name, default value).
158      * @param[in] i_srcVpdMap - Optional source IPZ VPD map.
159      * @param[in] i_dstVpdMap - Optional destination IPZ VPD map.
160      *
161      * @return true if record details are successfully extracted and validated,
162      *         false otherwise.
163      */
164     bool extractAndValidateIpzRecordDetails(
165         const auto& i_aRecordKwInfo,
166         types::SrcDstRecordDetails o_srcDstRecordKeywordInfo,
167         const std::optional<types::IPZVpdMap>& i_srcVpdMap,
168         const std::optional<types::IPZVpdMap>& i_dstVpdMap) const noexcept;
169 
170     /**
171      * @brief Retrieve the binary and string values of a keyword for IPZ type.
172      *
173      * This API returns a tuple containing the binary and string values for the
174      * given record and keyword. If the input VPD map is not empty, the keyword
175      * value is extracted from the map; otherwise, the keyword value is
176      * retrieved through a D-Bus query.
177      *
178      * @param[in] i_recordKwName - Tuple of record and keyword name.
179      * @param[in] i_vpdMap - IPZ VPD map.
180      * @param[in] i_serviceName - Dbus service name.
181      *
182      * @return A tuple containing the keyword's binary value and its string
183      *         representation on success, or an empty tuple on failure.
184      */
185     types::BinaryStringKwValuePair getBinaryAndStrIpzKwValue(
186         const types::IpzType& i_recordKwName, const types::IPZVpdMap& i_vpdMap,
187         const std::string& i_serviceName) const noexcept;
188 
189     /**
190      * @brief Synchronize a keyword value to EEPROM for IPZ type.
191      *
192      * This API updates the specified record's keyword value on the given
193      * EEPROM. On success, it updates the corresponding string value in the
194      * provided VPD map if not empty.
195      *
196      * @param[in] i_fruPath - EEPROM path.
197      * @param[in] i_recordKwName - Tuple of record and keyword name.
198      * @param[in] i_binaryStrValue -  Tuple of Keyword value in Binary and
199      * string format.
200      * @param[out] o_vpdMap - IPZ VPD map.
201      */
202     void syncIpzData(const std::string& i_fruPath,
203                      const types::IpzType& i_recordKwName,
204                      const types::BinaryStringKwValuePair& i_binaryStrValue,
205                      types::IPZVpdMap& o_vpdMap) const noexcept;
206 
207     /* @brief API to check if the JSON parsed is valid.
208      *
209      * The API check for required details in the parsed JSON and flags any error
210      * if not found with mandatory tags to process backup and restore.
211      *
212      * @return True if valid, false otherwise.
213      */
214     bool isJsonValid();
215 
216     // System JSON config JSON object.
217     nlohmann::json m_sysCfgJsonObj{};
218 
219     // Backup and restore config JSON object.
220     nlohmann::json m_backupAndRestoreCfgJsonObj{};
221 
222     // Backup and restore status.
223     static BackupAndRestoreStatus m_backupAndRestoreStatus;
224 
225     // Shared pointer to Logger object
226     std::shared_ptr<Logger> m_logger;
227 
228     // Source path
229     std::string m_srcFruPath{};
230     std::string m_srcInvPath{};
231 
232     // Destination path
233     std::string m_dstFruPath{};
234     std::string m_dstInvPath{};
235 };
236 
237 } // namespace vpd
238