Update the CertificateRequest and JSON helper functions to the newest packet format

Change-Id: I28dd2753dfc5d8dda658be455e3804445a9efc26
diff --git a/src/certificate-request.cpp b/src/certificate-request.cpp
index 80b5a45..d1a4545 100644
--- a/src/certificate-request.cpp
+++ b/src/certificate-request.cpp
@@ -29,52 +29,24 @@
                                        const security::v2::Certificate& cert)
   : m_caName(caName)
   , m_requestId(requestId)
-  , m_status(Pending)
   , m_cert(static_cast<const Data&>(cert))
 {
 }
 
 CertificateRequest::CertificateRequest(const Name& caName,
                                        const std::string& requestId,
-                                       const ApplicationStatus& status,
+                                       const std::string& status,
                                        const std::string& challengeType,
-                                       const std::string& challengeStatus,
-                                       const std::string& challengeDefinedField,
+                                       const std::string& challengeSecrets,
                                        const security::v2::Certificate& cert)
   : m_caName(caName)
   , m_requestId(requestId)
   , m_status(status)
   , m_challengeType(challengeType)
-  , m_challengeStatus(challengeStatus)
-  , m_challengeDefinedField(challengeDefinedField)
   , m_cert(static_cast<const Data&>(cert))
 {
-}
-
-std::ostream&
-operator<<(std::ostream& os, CertificateRequest::ApplicationStatus status)
-{
-  std::string statusString;
-  switch (status) {
-    case CertificateRequest::Pending: {
-      statusString = "pending";
-      break;
-    }
-    case CertificateRequest::Verifying: {
-      statusString = "verifying";
-      break;
-    }
-    case CertificateRequest::Success: {
-      statusString = "success";
-      break;
-    }
-    case CertificateRequest::Failure: {
-      statusString = "failure";
-      break;
-    }
-  }
-  os << statusString;
-  return os;
+  std::istringstream ss(challengeSecrets);
+  boost::property_tree::json_parser::read_json(ss, m_challengeSecrets);
 }
 
 std::ostream&
@@ -84,16 +56,14 @@
   os << "  " << request.getCaName() << "\n";
   os << "Request ID:\n";
   os << "  " << request.getRequestId() << "\n";
-  os << "Request Status:\n";
-  os << "  " << request.getStatus() << "\n";
+  if (request.getStatus() != "") {
+    os << "Request Status:\n";
+    os << "  " << request.getStatus() << "\n";
+  }
   if (request.getChallengeType() != "") {
     os << "Request Challenge Type:\n";
     os << "  " << request.getChallengeType() << "\n";
   }
-  if (request.getChallengeStatus() != "") {
-    os << "Request Challenge Status:\n";
-    os << "  " << request.getChallengeStatus() << "\n";
-  }
   os << "Certificate:\n";
   util::IndentedStream os2(os, "  ");
   os2 << request.getCert();
diff --git a/src/certificate-request.hpp b/src/certificate-request.hpp
index b8d1ccd..35fb2f7 100644
--- a/src/certificate-request.hpp
+++ b/src/certificate-request.hpp
@@ -27,6 +27,8 @@
 namespace ndn {
 namespace ndncert {
 
+typedef boost::property_tree::ptree JsonSection;
+
 /**
  * @brief Represents a certificate request instance.
  *
@@ -37,20 +39,12 @@
 class CertificateRequest
 {
 public:
-  enum ApplicationStatus {
-    Pending = 0,
-    Verifying = 1,
-    Success = 2,
-    Failure = 3
-  };
-
-public:
   CertificateRequest(const Name& caName, const std::string& requestId,
                      const security::v2::Certificate& cert);
 
   CertificateRequest(const Name& caName, const std::string& requestId,
-                     const ApplicationStatus& status, const std::string& challengeType,
-                     const std::string& challengeStatus, const std::string& challengeDefinedField,
+                     const std::string& status, const std::string& challengeType,
+                     const std::string& challengeDefinedField,
                      const security::v2::Certificate& certBlock);
 
   const Name&
@@ -65,7 +59,7 @@
     return m_requestId;
   }
 
-  const ApplicationStatus&
+  const std::string&
   getStatus() const
   {
     return m_status;
@@ -77,22 +71,10 @@
     return m_challengeType;
   }
 
-  const std::string&
-  getChallengeDefinedField() const
+  const JsonSection&
+  getChallengeSecrets() const
   {
-    return m_challengeDefinedField;
-  }
-
-  const std::string&
-  getChallengeInstruction() const
-  {
-    return m_challengeInstruction;
-  }
-
-  const std::string&
-  getChallengeStatus() const
-  {
-    return m_challengeStatus;
+    return m_challengeSecrets;
   }
 
   const security::v2::Certificate&
@@ -101,11 +83,8 @@
     return m_cert;
   }
 
-  /**
-   * These setters should only be invoked by ChallengeModule
-   */
   void
-  setStatus(const ApplicationStatus& status)
+  setStatus(const std::string& status)
   {
     m_status = status;
   }
@@ -117,58 +96,28 @@
   }
 
   void
-  setChallengeStatus(const std::string& challengeStatus)
+  setChallengeSecrets(const JsonSection& challengeSecrets)
   {
-    m_challengeStatus = challengeStatus;
-  }
-
-  void
-  setChallengeDefinedField(const std::string& challengeDefinedField)
-  {
-    m_challengeDefinedField = challengeDefinedField;
-  }
-
-  void
-  setChallengeInstruction(const std::string& challengeInstruction)
-  {
-    m_challengeInstruction = challengeInstruction;
+    m_challengeSecrets = challengeSecrets;
   }
 
 private:
   Name m_caName;
   std::string m_requestId;
-  ApplicationStatus m_status;
+  std::string m_status;
   std::string m_challengeType;
 
   /**
-   * @brief Defined by ChallengeModule to indicate the verification status.
-   *
-   * This field will be stored by CA.
-   */
-  std::string m_challengeStatus;
-
-  /**
    * @brief Defined by ChallengeModule to store secret information.
    *
    * This field will be stored by CA.
    */
-  std::string m_challengeDefinedField;
-
-  /**
-   * @brief Defined by ChallengeModule to indicate end entity the next step.
-   *
-   * This field will be presented to end entity.
-   * This field will NOT be stored by CA.
-   */
-  std::string m_challengeInstruction;
+  JsonSection m_challengeSecrets;
 
   security::v2::Certificate m_cert;
 };
 
 std::ostream&
-operator<<(std::ostream& os, CertificateRequest::ApplicationStatus status);
-
-std::ostream&
 operator<<(std::ostream& os, const CertificateRequest& request);
 
 } // namespace ndncert
diff --git a/src/json-helper.cpp b/src/json-helper.cpp
index 609b426..a7cc897 100644
--- a/src/json-helper.cpp
+++ b/src/json-helper.cpp
@@ -36,18 +36,15 @@
 }
 
 const JsonSection
-genResponseNewJson(const CertificateRequest& request,
-                   const std::list<std::tuple<std::string, std::string>> challenges)
+genResponseNewJson(const std::string& requestId, const std::list<std::string>& challenges)
 {
   JsonSection root;
   JsonSection challengesSection;
-  root.put(JSON_STATUS, boost::lexical_cast<std::string>(request.getStatus()));
-  root.put(JSON_REQUEST_ID, request.getRequestId());
+  root.put(JSON_REQUEST_ID, requestId);
 
   for (const auto& entry : challenges) {
     JsonSection challenge;
-    challenge.put(JSON_CHALLENGE_TYPE, std::get<0>(entry));
-    challenge.put(JSON_CHALLENGE_INSTRUCTION, std::get<1>(entry));
+    challenge.put(JSON_CHALLENGE_TYPE, entry);
     challengesSection.push_back(std::make_pair("", challenge));
   }
   root.add_child(JSON_CHALLENGES, challengesSection);
@@ -56,13 +53,16 @@
 }
 
 const JsonSection
-genResponsePollJson(const CertificateRequest& request)
+genResponseChallengeJson(const std::string& requestId, const std::string& challengeType,
+                         const std::string& status, const Name& name)
 {
   JsonSection root;
-  root.put(JSON_STATUS, boost::lexical_cast<std::string>(request.getStatus()));
-  root.put(JSON_CHALLENGE_TYPE, request.getChallengeType());
-  root.put(JSON_CHALLENGE_STATUS, request.getChallengeStatus());
-  root.put(JSON_CHALLENGE_INSTRUCTION, request.getChallengeInstruction());
+  root.put(JSON_REQUEST_ID, requestId);
+  root.put(JSON_CHALLENGE_TYPE, challengeType);
+  root.put(JSON_STATUS, status);
+  if (name.toUri() != "") {
+    root.put(JSON_CERTIFICATE, name.toUri());
+  }
   return root;
 }
 
diff --git a/src/json-helper.hpp b/src/json-helper.hpp
index fe3ba80..c34c631 100644
--- a/src/json-helper.hpp
+++ b/src/json-helper.hpp
@@ -26,25 +26,22 @@
 namespace ndn {
 namespace ndncert {
 
-const std::string JSON_IDNENTIFIER = "Identifier";
-const std::string JSON_CA_INFO = "CA-Info";
-const std::string JSON_STATUS = "Status";
-const std::string JSON_REQUEST_ID = "Request-ID";
-const std::string JSON_CHALLENGES = "Challenges";
-const std::string JSON_CHALLENGE_TYPE = "Challenge-Type";
-const std::string JSON_CHALLENGE_INSTRUCTION = "Challenge-Instruction";
-const std::string JSON_CHALLENGE_STATUS = "Challenge-Status";
-const std::string JSON_ERROR_INFO = "Error-Info";
-
-typedef boost::property_tree::ptree JsonSection;
+const std::string JSON_IDNENTIFIER = "identifier";
+const std::string JSON_CA_INFO = "ca-info";
+const std::string JSON_STATUS = "status";
+const std::string JSON_REQUEST_ID = "request-id";
+const std::string JSON_CHALLENGES = "challenges";
+const std::string JSON_CHALLENGE_TYPE = "challenge-type";
+const std::string JSON_ERROR_INFO = "error-info";
+const std::string JSON_CERTIFICATE = "certificate";
 
 /**
  * @brief Generate JSON file to response PROBE insterest
  *
  * Target JSON format:
  * {
- *   "Identifier": "",
- *   "CA-Info": ""
+ *   "identifier": "",
+ *   "ca-info": ""
  * }
  */
 const JsonSection
@@ -55,46 +52,54 @@
  *
  * Target JSON format:
  * {
- *   "Status": "",
- *   "Request-ID": "",
- *   "Challenges": [
+ *   "request-id": "",
+ *   "challenges": [
  *     {
- *       "Challenge-Type": "",
- *       "Challenge-Instruction": ""
+ *       "challenge-type": ""
  *     },
  *     {
- *       "Challenge-Type": "",
- *       "Challenge-Instruction": ""
+ *       "challenge-type": ""
  *     },
  *     ...
  *   ]
  * }
  */
 const JsonSection
-genResponseNewJson(const CertificateRequest& request,
-                   const std::list<std::tuple<std::string, std::string>> challenges);
+genResponseNewJson(const std::string& requestId, const std::list<std::string>& challenges);
 
 /**
- * @brief Generate JSON file to response POLL interest
+ * @brief Generate JSON file to response _SELECT, _VALIDATE, and _STATUS interest
+ *
+ * if certificate name is not present:
  *
  * Target JSON format:
  * {
- *   "Status": "",
- *   "Challenge-Type": "",
- *   "Challenge-Status": "",
- *   "Challenge-Instruction": ""
+ *   "request-id": "@p requestId",
+ *   "challenge-type": "@p challengeType",
+ *   "status": "@p status"
+ * }
+ *
+ * if certificate name is present:
+ *
+ * Target JSON format:
+ * {
+ *   "request-id": "@p requestId",
+ *   "challenge-type": "@p challengeType",
+ *   "status": "@p status",
+ *   "certificate":"@p name"
  * }
  */
 const JsonSection
-genResponsePollJson(const CertificateRequest& request);
+genResponseChallengeJson(const std::string& requestId, const std::string& challengeType,
+                         const std::string& status, const Name& name = Name());
 
 /**
  * @brief Generate JSON file when there is an Error
  *
  * Target JSON format:
  * {
- *   "Status": "",
- *   "Error-Info": ""
+ *   "status": "",
+ *   "error-info": ""
  * }
  */
 const JsonSection