16620e98dSDeepak Kodihalli #pragma once
26620e98dSDeepak Kodihalli
3a680d1efSPatrick Venture #include "config.h"
4a680d1efSPatrick Venture
56620e98dSDeepak Kodihalli #include <cereal/archives/json.hpp>
6*cf5d024bSGeorge Liu #include <phosphor-logging/lg2.hpp>
7a83db30eSBrad Bishop
8e6b21c74SKun Yi #include <filesystem>
96620e98dSDeepak Kodihalli #include <fstream>
1086ad3c67SJayanth Othayoth
117dfd08f8SBrad Bishop namespace phosphor
127dfd08f8SBrad Bishop {
137dfd08f8SBrad Bishop namespace inventory
147dfd08f8SBrad Bishop {
157dfd08f8SBrad Bishop namespace manager
166620e98dSDeepak Kodihalli {
176620e98dSDeepak Kodihalli
18e6b21c74SKun Yi namespace fs = std::filesystem;
196620e98dSDeepak Kodihalli
20a2485504SBrad Bishop namespace detail
21a2485504SBrad Bishop {
getStoragePath(const std::string & path,const std::string & iface)22a2485504SBrad Bishop inline fs::path getStoragePath(const std::string& path,
23a2485504SBrad Bishop const std::string& iface)
24a2485504SBrad Bishop {
25a2485504SBrad Bishop auto p = fs::path(PIM_PERSIST_PATH);
26a2485504SBrad Bishop p /= fs::path(path).relative_path();
27a2485504SBrad Bishop p /= fs::path(iface).relative_path();
28a2485504SBrad Bishop return p;
29a2485504SBrad Bishop }
30a2485504SBrad Bishop } // namespace detail
31a2485504SBrad Bishop
327dfd08f8SBrad Bishop struct SerialOps
337dfd08f8SBrad Bishop {
347dfd08f8SBrad Bishop /** @brief Serialize inventory item path
357dfd08f8SBrad Bishop * Serializing only path for an empty interface to be consistent
367dfd08f8SBrad Bishop * interfaces.
377dfd08f8SBrad Bishop * @param[in] path - DBus object path
387dfd08f8SBrad Bishop * @param[in] iface - Inventory interface name
397dfd08f8SBrad Bishop */
serializephosphor::inventory::manager::SerialOps407dfd08f8SBrad Bishop static void serialize(const std::string& path, const std::string& iface)
417dfd08f8SBrad Bishop {
42a2485504SBrad Bishop auto p = detail::getStoragePath(path, iface);
43a2485504SBrad Bishop fs::create_directories(p.parent_path());
447dfd08f8SBrad Bishop std::ofstream os(p, std::ios::binary);
457dfd08f8SBrad Bishop }
466620e98dSDeepak Kodihalli
476620e98dSDeepak Kodihalli /** @brief Serialize inventory item
486620e98dSDeepak Kodihalli *
496620e98dSDeepak Kodihalli * @param[in] path - DBus object path
506620e98dSDeepak Kodihalli * @param[in] iface - Inventory interface name
516620e98dSDeepak Kodihalli * @param[in] object - Object to be serialized
526620e98dSDeepak Kodihalli */
536620e98dSDeepak Kodihalli template <typename T>
serializephosphor::inventory::manager::SerialOps547dfd08f8SBrad Bishop static void serialize(const std::string& path, const std::string& iface,
557dfd08f8SBrad Bishop const T& object)
566620e98dSDeepak Kodihalli {
57a2485504SBrad Bishop auto p = detail::getStoragePath(path, iface);
58a2485504SBrad Bishop fs::create_directories(p.parent_path());
596620e98dSDeepak Kodihalli std::ofstream os(p, std::ios::binary);
606620e98dSDeepak Kodihalli cereal::JSONOutputArchive oarchive(os);
616620e98dSDeepak Kodihalli oarchive(object);
626620e98dSDeepak Kodihalli }
636620e98dSDeepak Kodihalli
deserializephosphor::inventory::manager::SerialOps647dfd08f8SBrad Bishop static void deserialize(const std::string&, const std::string&)
65fa23d704SMarri Devender Rao {
667dfd08f8SBrad Bishop // This is intentionally a noop.
67fa23d704SMarri Devender Rao }
68fa23d704SMarri Devender Rao
69b28990f3SDeepak Kodihalli /** @brief Deserialize inventory item
70b28990f3SDeepak Kodihalli *
71b28990f3SDeepak Kodihalli * @param[in] path - DBus object path
72b28990f3SDeepak Kodihalli * @param[in] iface - Inventory interface name
73b28990f3SDeepak Kodihalli * @param[in] object - Object to be serialized
74b28990f3SDeepak Kodihalli */
75b28990f3SDeepak Kodihalli template <typename T>
deserializephosphor::inventory::manager::SerialOps767dfd08f8SBrad Bishop static void deserialize(const std::string& path, const std::string& iface,
777dfd08f8SBrad Bishop T& object)
78b28990f3SDeepak Kodihalli {
79ded627c4SMatt Spinler auto p = detail::getStoragePath(path, iface);
8086ad3c67SJayanth Othayoth try
8186ad3c67SJayanth Othayoth {
82b28990f3SDeepak Kodihalli if (fs::exists(p))
83b28990f3SDeepak Kodihalli {
84b28990f3SDeepak Kodihalli std::ifstream is(p, std::ios::in | std::ios::binary);
85b28990f3SDeepak Kodihalli cereal::JSONInputArchive iarchive(is);
86b28990f3SDeepak Kodihalli iarchive(object);
87b28990f3SDeepak Kodihalli }
88b28990f3SDeepak Kodihalli }
893e2d9641SPatrick Williams catch (const cereal::Exception& e)
9086ad3c67SJayanth Othayoth {
91*cf5d024bSGeorge Liu lg2::error("Deserialization failed: {ERROR}", "ERROR", e);
9286ad3c67SJayanth Othayoth fs::remove(p);
9386ad3c67SJayanth Othayoth }
9486ad3c67SJayanth Othayoth }
957dfd08f8SBrad Bishop };
967dfd08f8SBrad Bishop } // namespace manager
977dfd08f8SBrad Bishop } // namespace inventory
987dfd08f8SBrad Bishop } // namespace phosphor
99