xref: /openbmc/openpower-vpd-parser/vpd-manager/include/backup_restore.hpp (revision 43fedabc032ba7c209b58111b6c35c7d95a9f14e)
1 #pragma once
2 
3 #include "types.hpp"
4 
5 #include <nlohmann/json.hpp>
6 
7 #include <tuple>
8 
9 namespace vpd
10 {
11 
12 // Backup and restore operation status.
13 enum class BackupAndRestoreStatus : uint8_t
14 {
15     NotStarted,
16     Invoked,
17     Completed
18 };
19 
20 /**
21  * @brief class to implement backup and restore VPD.
22  *
23  */
24 
25 class BackupAndRestore
26 {
27   public:
28     // delete functions
29     BackupAndRestore() = delete;
30     BackupAndRestore(const BackupAndRestore&) = delete;
31     BackupAndRestore& operator=(const BackupAndRestore&) = delete;
32     BackupAndRestore(BackupAndRestore&&) = delete;
33     BackupAndRestore& operator=(BackupAndRestore&&) = delete;
34 
35     /**
36      * @brief Constructor.
37      *
38      * @param[in] i_sysCfgJsonObj - System config JSON object.
39      *
40      * @throw std::runtime_error in case constructor failure.
41      */
42     BackupAndRestore(const nlohmann::json& i_sysCfgJsonObj);
43 
44     /**
45      * @brief Default destructor.
46      */
47     ~BackupAndRestore() = default;
48 
49     /**
50      * @brief An API to backup and restore VPD.
51      *
52      * Note: This API works on the keywords declared in the backup and restore
53      * config JSON. Restore or backup action could be triggered for each
54      * keyword, based on the keyword's value present in the source and
55      * destination keyword.
56      *
57      * Restore source keyword's value with destination keyword's value,
58      * when source keyword has default value but
59      * destination's keyword has non default value.
60      *
61      * Backup the source keyword value to the destination's keyword's value,
62      * when source keyword has non default value but
63      * destination's keyword has default value.
64      *
65      * @return Tuple of updated source and destination VPD map variant.
66      */
67     std::tuple<types::VPDMapVariant, types::VPDMapVariant> backupAndRestore();
68 
69     /**
70      * @brief An API to set backup and restore status.
71      *
72      * @param[in] i_status - Status to set.
73      */
74     static void setBackupAndRestoreStatus(
75         const BackupAndRestoreStatus& i_status);
76 
77   private:
78     /**
79      * @brief An API to handle backup and restore of IPZ type VPD.
80      *
81      * @param[in,out] io_srcVpdMap - Source VPD map.
82      * @param[in,out] io_dstVpdMap - Destination VPD map.
83      * @param[in] i_srcPath - Source EEPROM file path or inventory path.
84      * @param[in] i_dstPath - Destination EEPROM file path or inventory path.
85      *
86      * @throw std::runtime_error
87      */
88     void backupAndRestoreIpzVpd(
89         types::IPZVpdMap& io_srcVpdMap, types::IPZVpdMap& io_dstVpdMap,
90         const std::string& i_srcPath, const std::string& i_dstPath);
91 
92     // System JSON config JSON object.
93     nlohmann::json m_sysCfgJsonObj{};
94 
95     // Backup and restore config JSON object.
96     nlohmann::json m_backupAndRestoreCfgJsonObj{};
97 
98     // Backup and restore status.
99     static BackupAndRestoreStatus m_backupAndRestoreStatus;
100 };
101 
102 } // namespace vpd
103