further update and rename files

Change-Id: Ie664c5b9e5a764b8706d21cc85b9cec8755dc380
diff --git a/src/ca-detail/ca-state.cpp b/src/ca-detail/ca-state.cpp
new file mode 100644
index 0000000..185fbdd
--- /dev/null
+++ b/src/ca-detail/ca-state.cpp
@@ -0,0 +1,117 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2017-2020, Regents of the University of California.
+ *
+ * This file is part of ndncert, a certificate management system based on NDN.
+ *
+ * ndncert is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License along with
+ * ndncert, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndncert authors and contributors.
+ */
+
+#include "ca-state.hpp"
+#include <ndn-cxx/util/indented-stream.hpp>
+
+namespace ndn {
+namespace ndncert {
+
+std::string statusToString(Status status) {
+  switch (status)
+  {
+  case Status::BEFORE_CHALLENGE:
+    return "Before challenge";
+  case Status::CHALLENGE:
+    return "In challenge";
+  case Status::PENDING:
+    return "Pending after challenge";
+  case Status::SUCCESS:
+    return "Success";
+  case Status::FAILURE:
+    return "Failure";
+  case Status::NOT_STARTED:
+    return "Not started";
+  case Status::ENDED:
+    return "Ended";
+  default:
+    return "Unrecognized status";
+  }
+}
+
+ChallengeState::ChallengeState(const std::string& challengeStatus,
+                               const time::system_clock::TimePoint& challengeTp,
+                               size_t remainingTries, time::seconds remainingTime,
+                               JsonSection&& challengeSecrets)
+    : m_challengeStatus(challengeStatus)
+    , m_timestamp(challengeTp)
+    , m_remainingTries(remainingTries)
+    , m_remainingTime(remainingTime)
+    , m_secrets(std::move(challengeSecrets))
+{
+}
+
+CaState::CaState()
+    : m_requestType(RequestType::NOTINITIALIZED)
+    , m_status(Status::NOT_STARTED)
+{
+}
+
+CaState::CaState(const Name& caName, const std::string& requestId, RequestType requestType, Status status,
+                 const security::Certificate& cert, Block encryptionKey)
+    : m_caPrefix(caName)
+    , m_requestId(requestId)
+    , m_requestType(requestType)
+    , m_status(status)
+    , m_cert(cert)
+    , m_encryptionKey(std::move(encryptionKey))
+{
+}
+
+CaState::CaState(const Name& caName, const std::string& requestId, RequestType requestType, Status status,
+                 const security::Certificate& cert, const std::string& challengeType,
+                 const std::string& challengeStatus, const time::system_clock::TimePoint& challengeTp,
+                 size_t remainingTries, time::seconds remainingTime, JsonSection&& challengeSecrets,
+                 Block encryptionKey)
+    : m_caPrefix(caName)
+    , m_requestId(requestId)
+    , m_requestType(requestType)
+    , m_status(status)
+    , m_cert(cert)
+    , m_encryptionKey(std::move(encryptionKey))
+    , m_challengeType(challengeType)
+    , m_challengeState(ChallengeState(challengeStatus, challengeTp, remainingTries, remainingTime, std::move(challengeSecrets)))
+{
+}
+
+std::ostream&
+operator<<(std::ostream& os, const CaState& request)
+{
+  os << "Request's CA name: " << request.m_caPrefix << "\n";
+  os << "Request's request ID: " << request.m_requestId << "\n";
+  os << "Request's status: " << statusToString(request.m_status) << "\n";
+  os << "Request's challenge type: " << request.m_challengeType << "\n";
+  if (request.m_challengeState) {
+    os << "Challenge Status: " << request.m_challengeState->m_challengeStatus << "\n";
+    os << "Challenge remaining tries:" << request.m_challengeState->m_remainingTries << " times\n";
+    os << "Challenge remaining time: " << request.m_challengeState->m_remainingTime.count() << " seconds\n";
+    os << "Challenge last update: " << time::toIsoString(request.m_challengeState->m_timestamp) << "\n";
+    std::stringstream ss;
+    boost::property_tree::write_json(ss, request.m_challengeState->m_secrets);
+    os << "Challenge secret:\n" << ss.str() << "\n";
+  }
+  os << "Certificate:\n";
+  util::IndentedStream os2(os, "  ");
+  os2 << request.m_cert;
+  return os;
+}
+
+} // namespace ndncert
+} // namespace ndn
diff --git a/src/ca-detail/ca-state.hpp b/src/ca-detail/ca-state.hpp
new file mode 100644
index 0000000..4c99185
--- /dev/null
+++ b/src/ca-detail/ca-state.hpp
@@ -0,0 +1,94 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2017-2020, Regents of the University of California.
+ *
+ * This file is part of ndncert, a certificate management system based on NDN.
+ *
+ * ndncert is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License along with
+ * ndncert, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndncert authors and contributors.
+ */
+
+#ifndef NDNCERT_CA_STATE_HPP
+#define NDNCERT_CA_STATE_HPP
+
+#include "protocol-detail/ndncert-common.hpp"
+
+namespace ndn {
+namespace ndncert {
+
+// NDNCERT Request status enumeration
+enum class Status : uint16_t {
+  BEFORE_CHALLENGE = 0,
+  CHALLENGE = 1,
+  PENDING = 2,
+  SUCCESS = 3,
+  FAILURE = 4,
+  NOT_STARTED = 5,
+  ENDED = 6
+};
+
+// Convert request status to string
+std::string
+statusToString(Status status);
+
+/**
+ * @brief The state maintained by the Challenge modules
+ */
+struct ChallengeState
+{
+  ChallengeState(const std::string& challengeStatus, const time::system_clock::TimePoint& challengeTp,
+                 size_t remainingTries, time::seconds remainingTime,
+                 JsonSection&& challengeSecrets);
+  std::string m_challengeStatus;
+  time::system_clock::TimePoint m_timestamp;
+  size_t m_remainingTries;
+  time::seconds m_remainingTime;
+  JsonSection m_secrets;
+};
+
+/**
+ * @brief Represents a certificate request instance kept by the CA.
+ *
+ * ChallengeModule should take use of ChallengeState to keep state.
+ */
+class CaState
+{
+public:
+  CaState();
+  CaState(const Name& caName, const std::string& requestId, RequestType requestType, Status status,
+          const security::Certificate& cert, Block m_encryptionKey);
+  CaState(const Name& caName, const std::string& requestId, RequestType requestType, Status status,
+          const security::Certificate& cert, const std::string& challengeType,
+          const std::string& challengeStatus, const time::system_clock::TimePoint& challengeTp,
+          size_t remainingTries, time::seconds remainingTime, JsonSection&& challengeSecrets,
+          Block m_encryptionKey);
+
+public:
+  Name m_caPrefix;
+  std::string m_requestId;
+  RequestType m_requestType;
+  Status m_status;
+  security::Certificate m_cert;
+  Block m_encryptionKey;
+
+  std::string m_challengeType;
+  boost::optional<ChallengeState> m_challengeState;
+};
+
+std::ostream&
+operator<<(std::ostream& os, const CaState& request);
+
+} // namespace ndncert
+} // namespace ndn
+
+#endif // NDNCERT_CA_STATE_HPP
diff --git a/src/ca-detail/ca-storage.hpp b/src/ca-detail/ca-storage.hpp
index e18a5bc..e35af1a 100644
--- a/src/ca-detail/ca-storage.hpp
+++ b/src/ca-detail/ca-storage.hpp
@@ -21,7 +21,7 @@
 #ifndef NDNCERT_CA_STORAGE_HPP
 #define NDNCERT_CA_STORAGE_HPP
 
-#include "ca-state.hpp"
+#include "ca-detail/ca-state.hpp"
 
 namespace ndn {
 namespace ndncert {