16620e98dSDeepak Kodihalli #pragma once 26620e98dSDeepak Kodihalli 3a680d1efSPatrick Venture #include "config.h" 4a680d1efSPatrick Venture 56620e98dSDeepak Kodihalli #include <cereal/archives/json.hpp> 66620e98dSDeepak Kodihalli #include <experimental/filesystem> 76620e98dSDeepak Kodihalli #include <fstream> 886ad3c67SJayanth Othayoth #include <phosphor-logging/log.hpp> 986ad3c67SJayanth Othayoth 10*7dfd08f8SBrad Bishop namespace phosphor 11*7dfd08f8SBrad Bishop { 12*7dfd08f8SBrad Bishop namespace inventory 13*7dfd08f8SBrad Bishop { 14*7dfd08f8SBrad Bishop namespace manager 156620e98dSDeepak Kodihalli { 166620e98dSDeepak Kodihalli 176620e98dSDeepak Kodihalli namespace fs = std::experimental::filesystem; 186620e98dSDeepak Kodihalli 19*7dfd08f8SBrad Bishop struct SerialOps 20*7dfd08f8SBrad Bishop { 21*7dfd08f8SBrad Bishop /** @brief Serialize inventory item path 22*7dfd08f8SBrad Bishop * Serializing only path for an empty interface to be consistent 23*7dfd08f8SBrad Bishop * interfaces. 24*7dfd08f8SBrad Bishop * @param[in] path - DBus object path 25*7dfd08f8SBrad Bishop * @param[in] iface - Inventory interface name 26*7dfd08f8SBrad Bishop */ 27*7dfd08f8SBrad Bishop static void serialize(const std::string& path, const std::string& iface) 28*7dfd08f8SBrad Bishop { 29*7dfd08f8SBrad Bishop fs::path p(PIM_PERSIST_PATH); 30*7dfd08f8SBrad Bishop p /= path; 31*7dfd08f8SBrad Bishop fs::create_directories(p); 32*7dfd08f8SBrad Bishop p /= iface; 33*7dfd08f8SBrad Bishop std::ofstream os(p, std::ios::binary); 34*7dfd08f8SBrad Bishop } 356620e98dSDeepak Kodihalli 366620e98dSDeepak Kodihalli /** @brief Serialize inventory item 376620e98dSDeepak Kodihalli * 386620e98dSDeepak Kodihalli * @param[in] path - DBus object path 396620e98dSDeepak Kodihalli * @param[in] iface - Inventory interface name 406620e98dSDeepak Kodihalli * @param[in] object - Object to be serialized 416620e98dSDeepak Kodihalli */ 426620e98dSDeepak Kodihalli template <typename T> 43*7dfd08f8SBrad Bishop static void serialize(const std::string& path, const std::string& iface, 44*7dfd08f8SBrad Bishop const T& object) 456620e98dSDeepak Kodihalli { 466620e98dSDeepak Kodihalli fs::path p(PIM_PERSIST_PATH); 476620e98dSDeepak Kodihalli p /= path; 486620e98dSDeepak Kodihalli fs::create_directories(p); 496620e98dSDeepak Kodihalli p /= iface; 506620e98dSDeepak Kodihalli std::ofstream os(p, std::ios::binary); 516620e98dSDeepak Kodihalli cereal::JSONOutputArchive oarchive(os); 526620e98dSDeepak Kodihalli oarchive(object); 536620e98dSDeepak Kodihalli } 546620e98dSDeepak Kodihalli 55*7dfd08f8SBrad Bishop static void deserialize(const std::string&, const std::string&) 56fa23d704SMarri Devender Rao { 57*7dfd08f8SBrad Bishop // This is intentionally a noop. 58fa23d704SMarri Devender Rao } 59fa23d704SMarri Devender Rao 60b28990f3SDeepak Kodihalli /** @brief Deserialize inventory item 61b28990f3SDeepak Kodihalli * 62b28990f3SDeepak Kodihalli * @param[in] path - DBus object path 63b28990f3SDeepak Kodihalli * @param[in] iface - Inventory interface name 64b28990f3SDeepak Kodihalli * @param[in] object - Object to be serialized 65b28990f3SDeepak Kodihalli */ 66b28990f3SDeepak Kodihalli template <typename T> 67*7dfd08f8SBrad Bishop static void deserialize(const std::string& path, const std::string& iface, 68*7dfd08f8SBrad Bishop T& object) 69b28990f3SDeepak Kodihalli { 70b28990f3SDeepak Kodihalli fs::path p(PIM_PERSIST_PATH); 71b28990f3SDeepak Kodihalli p /= path; 72b28990f3SDeepak Kodihalli p /= iface; 7386ad3c67SJayanth Othayoth try 7486ad3c67SJayanth Othayoth { 75b28990f3SDeepak Kodihalli if (fs::exists(p)) 76b28990f3SDeepak Kodihalli { 77b28990f3SDeepak Kodihalli std::ifstream is(p, std::ios::in | std::ios::binary); 78b28990f3SDeepak Kodihalli cereal::JSONInputArchive iarchive(is); 79b28990f3SDeepak Kodihalli iarchive(object); 80b28990f3SDeepak Kodihalli } 81b28990f3SDeepak Kodihalli } 8286ad3c67SJayanth Othayoth catch (cereal::Exception& e) 8386ad3c67SJayanth Othayoth { 84*7dfd08f8SBrad Bishop phosphor::logging::log<phosphor::logging::level::ERR>(e.what()); 8586ad3c67SJayanth Othayoth fs::remove(p); 8686ad3c67SJayanth Othayoth } 8786ad3c67SJayanth Othayoth } 88*7dfd08f8SBrad Bishop }; 89*7dfd08f8SBrad Bishop } // namespace manager 90*7dfd08f8SBrad Bishop } // namespace inventory 91*7dfd08f8SBrad Bishop } // namespace phosphor 92