Persist user requested host state

Resolves openbmc/openbmc#1785

Change-Id: I5f23ce50dc357489c7b7eece8bab3bfd6a61ffae
Signed-off-by: Dhruvaraj Subhashchandran <dhruvaraj@in.ibm.com>
diff --git a/host_state_serialize.cpp b/host_state_serialize.cpp
new file mode 100644
index 0000000..d5a3a98
--- /dev/null
+++ b/host_state_serialize.cpp
@@ -0,0 +1,68 @@
+#include <cereal/types/string.hpp>
+#include <cereal/types/vector.hpp>
+#include <cereal/types/tuple.hpp>
+#include <cereal/archives/json.hpp>
+#include <fstream>
+#include "host_state_serialize.hpp"
+#include "host_state_manager.hpp"
+
+namespace phosphor
+{
+namespace state
+{
+namespace manager
+{
+/** @brief Function required by Cereal to perform serialization.
+ *  @tparam Archive - Cereal archive type (binary in our case).
+ *  @param[in] archive - reference to Cereal archive.
+ *  @param[in] host - const reference to host.
+ */
+template<class Archive>
+void save(Archive& archive, const Host& host)
+{
+    archive(convertForMessage(host.sdbusplus::xyz::openbmc_project::
+           State::server::Host::requestedHostTransition()));
+}
+
+/** @brief Function required by Cereal to perform deserialization.
+ *  @tparam Archive - Cereal archive type (binary in our case).
+ *  @param[in] archive - reference to Cereal archive.
+ *  @param[in] host - reference to host.
+ */
+template<class Archive>
+void load(Archive& archive, Host& host)
+{
+    using namespace
+        sdbusplus::xyz::openbmc_project::State::server;
+
+    Host::Transition requestedHostTransition{};
+
+    std::string str;
+    archive(str);
+    requestedHostTransition = Host::convertTransitionFromString(
+                 str);
+    host.requestedHostTransition(requestedHostTransition);
+}
+
+fs::path serialize(const Host& host, const fs::path& dir)
+{
+    std::ofstream os(dir.c_str(), std::ios::binary);
+    cereal::JSONOutputArchive oarchive(os);
+    oarchive(host);
+    return dir;
+}
+
+bool deserialize(const fs::path& path, Host& host)
+{
+    if (fs::exists(path))
+    {
+        std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
+        cereal::JSONInputArchive iarchive(is);
+        iarchive(host);
+        return true;
+    }
+    return false;
+}
+} //namespace manager
+} // namespace state
+} // namespace phosphor