blob: 684c55fd2f469b3997d4dd2d20fad6110b683d74 [file] [log] [blame]
Patrick Williams1334b7b2021-02-22 17:15:12 -06001#include "config.h"
Ratan Gupta212f53e2018-04-30 17:28:05 +05302
3#include "snmp_serialize.hpp"
Patrick Williams1334b7b2021-02-22 17:15:12 -06004
Ratan Gupta212f53e2018-04-30 17:28:05 +05305#include "snmp_client.hpp"
Patrick Williams1334b7b2021-02-22 17:15:12 -06006
7#include <cereal/archives/binary.hpp>
8#include <cereal/types/string.hpp>
9#include <cereal/types/vector.hpp>
George Liu4caedfb2022-05-10 16:26:53 +080010#include <phosphor-logging/lg2.hpp>
Patrick Williams1334b7b2021-02-22 17:15:12 -060011
12#include <fstream>
Ratan Gupta212f53e2018-04-30 17:28:05 +053013
14// Register class version
15// From cereal documentation;
16// "This macro should be placed at global scope"
17CEREAL_CLASS_VERSION(phosphor::network::snmp::Client, CLASS_VERSION);
18
19namespace phosphor
20{
21namespace network
22{
23namespace snmp
24{
25
Ratan Gupta212f53e2018-04-30 17:28:05 +053026/** @brief Function required by Cereal to perform serialization.
27 * @tparam Archive - Cereal archive type (binary in our case).
28 * @param[in] archive - reference to Cereal archive.
29 * @param[in] manager - const reference to snmp manager info.
30 * @param[in] version - Class version that enables handling
31 * a serialized data across code levels
32 */
33template <class Archive>
Ratan Gupta34d129a2021-12-04 21:04:51 +053034void save(Archive& archive, const Client& manager,
35 const std::uint32_t /*version*/)
Ratan Gupta212f53e2018-04-30 17:28:05 +053036{
37 archive(manager.address(), manager.port());
38}
39
40/** @brief Function required by Cereal to perform deserialization.
41 * @tparam Archive - Cereal archive type (binary in our case).
42 * @param[in] archive - reference to Cereal archive.
43 * @param[in] manager - reference to snmp manager info.
44 * @param[in] version - Class version that enables handling
45 * a serialized data across code levels
46 */
47template <class Archive>
Ratan Gupta34d129a2021-12-04 21:04:51 +053048void load(Archive& archive, Client& manager, const std::uint32_t /*version*/)
Ratan Gupta212f53e2018-04-30 17:28:05 +053049{
50 std::string ipaddress{};
51 uint16_t port{};
52
53 archive(ipaddress, port);
54
55 manager.address(ipaddress);
56 manager.port(port);
57}
58
Ratan Guptaa7ff3852018-11-16 14:05:57 +053059fs::path serialize(Id id, const Client& manager, const fs::path& dir)
Ratan Gupta212f53e2018-04-30 17:28:05 +053060{
61 fs::path fileName = dir;
62 fs::create_directories(dir);
Ratan Guptaa7ff3852018-11-16 14:05:57 +053063 fileName /= std::to_string(id);
Ratan Gupta212f53e2018-04-30 17:28:05 +053064
65 std::ofstream os(fileName.string(), std::ios::binary);
66 cereal::BinaryOutputArchive oarchive(os);
67 oarchive(manager);
68 return fileName;
69}
70
71bool deserialize(const fs::path& path, Client& manager)
72{
73 try
74 {
75 if (fs::exists(path))
76 {
77 std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
78 cereal::BinaryInputArchive iarchive(is);
79 iarchive(manager);
80 return true;
81 }
82 return false;
83 }
Patrick Williamsc3600d82021-10-06 12:38:25 -050084 catch (const cereal::Exception& e)
Ratan Gupta212f53e2018-04-30 17:28:05 +053085 {
George Liu4caedfb2022-05-10 16:26:53 +080086 lg2::error("Deserialization failed: {ERROR}", "ERROR", e);
Ratan Gupta212f53e2018-04-30 17:28:05 +053087 std::error_code ec;
88 fs::remove(path, ec);
89 return false;
90 }
91 catch (const fs::filesystem_error& e)
92 {
93 return false;
94 }
95}
96
97} // namespace snmp
98} // namespace network
99} // namespace phosphor