Add certificate field to client config and use bool for probe field in ca config

Change-Id: Ib1dcaf07cdb214ea777a26adaed12e488663b3b3
diff --git a/src/ca-config.cpp b/src/ca-config.cpp
index 8d3f309..1032709 100644
--- a/src/ca-config.cpp
+++ b/src/ca-config.cpp
@@ -52,8 +52,7 @@
   for (; it != caList.end(); it++) {
     CaItem item;
     item.m_caName = Name(it->second.get<std::string>("ca-prefix"));
-    item.m_caInfo = it->second.get<std::string>("ca-info");
-    item.m_probe = it->second.get("probe", "");
+    item.m_probe = it->second.get("probe", false);
     item.m_freshnessPeriod = time::seconds(it->second.get<uint64_t>("issuing-freshness"));
     item.m_validityPeriod = time::days(it->second.get<uint64_t>("validity-period"));
 
@@ -65,7 +64,7 @@
 }
 
 std::list<std::string>
-CaConfig::parseChallengeList(const ConfigSection& section)
+CaConfig::parseChallengeList(const JsonSection& section)
 {
   std::list<std::string> result;
   auto it = section.begin();
diff --git a/src/ca-config.hpp b/src/ca-config.hpp
index ed95cb2..12cc396 100644
--- a/src/ca-config.hpp
+++ b/src/ca-config.hpp
@@ -21,20 +21,17 @@
 #ifndef NDNCERT_CA_CONFIG_HPP
 #define NDNCERT_CA_CONFIG_HPP
 
-#include "ndncert-common.hpp"
+#include "certificate-request.hpp"
 #include <ndn-cxx/security/v2/certificate.hpp>
 
 namespace ndn {
 namespace ndncert {
 
-typedef boost::property_tree::ptree ConfigSection;
-
 class CaItem
 {
 public:
   Name m_caName;
-  std::string m_caInfo;
-  std::string m_probe;
+  bool m_probe;
   time::seconds m_freshnessPeriod;
   time::days m_validityPeriod;
   std::list<std::string> m_supportedChallenges;
@@ -65,13 +62,13 @@
   parse();
 
   std::list<std::string>
-  parseChallengeList(const ConfigSection& configSection);
+  parseChallengeList(const JsonSection& configSection);
 
 public:
   std::list<CaItem> m_caItems;
 
 PUBLIC_WITH_TESTS_ELSE_PRIVATE:
-  ConfigSection m_config;
+  JsonSection m_config;
 };
 
 } // namespace ndncert
diff --git a/src/client-config.cpp b/src/client-config.cpp
index c75615d..7fd5280 100644
--- a/src/client-config.cpp
+++ b/src/client-config.cpp
@@ -19,6 +19,7 @@
  */
 
 #include "client-config.hpp"
+#include <ndn-cxx/util/io.hpp>
 
 namespace ndn {
 namespace ndncert {
@@ -48,11 +49,14 @@
   auto caList = m_config.get_child("ca-list");
   auto it = caList.begin();
   for (; it != caList.end(); it++) {
-    CaItem item;
+    ClientCaItem item;
     item.m_caName = Name(it->second.get<std::string>("ca-prefix"));
     item.m_caInfo = it->second.get<std::string>("ca-info");
     item.m_probe = it->second.get("probe", "");
 
+    std::istringstream ss(it->second.get<std::string>("certificate"));
+    item.m_anchor = *(io::load<security::v2::Certificate>(ss));
+
     auto challengeList = it->second.get_child("supported-challenges");
     item.m_supportedChallenges = parseChallengeList(challengeList);
 
@@ -61,7 +65,7 @@
 }
 
 std::list<std::string>
-ClientConfig::parseChallengeList(const ConfigSection& section)
+ClientConfig::parseChallengeList(const JsonSection& section)
 {
   std::list<std::string> result;
   auto it = section.begin();
@@ -72,43 +76,15 @@
 }
 
 void
-ClientConfig::addNewCaItem(const CaItem& item)
+ClientConfig::addNewCaItem(const ClientCaItem& item)
 {
-  auto& caList = m_config.get_child("ca-list");
-
-  ConfigSection newCaItem;
-  ConfigSection newCaChallengeList;
-  newCaItem.put("ca-prefix", item.m_caName.toUri());
-  newCaItem.put("ca-info", item.m_caInfo);
-  if (item.m_probe != "") {
-    newCaItem.put("probe", item.m_probe);
-  }
-  for (const auto& challengeType : item.m_supportedChallenges) {
-    ConfigSection challengeSection;
-    challengeSection.put("type", challengeType);
-    newCaChallengeList.push_back(std::make_pair("", challengeSection));
-  }
-  newCaItem.add_child("supported-challenges", newCaChallengeList);
-  caList.push_back(std::make_pair("", newCaItem));
-
-  parse();
+  m_caItems.push_back(item);
 }
 
 void
 ClientConfig::removeCaItem(const Name& caName)
 {
-  auto& caList = m_config.get_child("ca-list");
-  auto it = caList.begin();
-  while (it != caList.end()) {
-    if (it->second.get<std::string>("ca-prefix") == caName.toUri()) {
-      it = caList.erase(it);
-      break;
-    }
-    else {
-      it++;
-    }
-  }
-  parse();
+  m_caItems.remove_if([&] (const ClientCaItem& item) {return item.m_caName == caName;});
 }
 
 } // namespace ndncert
diff --git a/src/client-config.hpp b/src/client-config.hpp
index fbb703b..8234c01 100644
--- a/src/client-config.hpp
+++ b/src/client-config.hpp
@@ -21,12 +21,22 @@
 #ifndef NDNCERT_CLIENT_CONFIG_HPP
 #define NDNCERT_CLIENT_CONFIG_HPP
 
-#include "ca-config.hpp"
-#include <boost/filesystem.hpp>
+#include "certificate-request.hpp"
+#include <ndn-cxx/security/v2/certificate.hpp>
 
 namespace ndn {
 namespace ndncert {
 
+class ClientCaItem
+{
+public:
+  Name m_caName;
+  std::string m_caInfo;
+  std::string m_probe;
+  std::list<std::string> m_supportedChallenges;
+  security::v2::Certificate m_anchor;
+};
+
 class ClientConfig
 {
 public:
@@ -41,7 +51,7 @@
   load(const std::string& fileName);
 
   void
-  addNewCaItem(const CaItem& item);
+  addNewCaItem(const ClientCaItem& item);
 
   void
   removeCaItem(const Name& caName);
@@ -51,13 +61,13 @@
   parse();
 
   std::list<std::string>
-  parseChallengeList(const ConfigSection& section);
+  parseChallengeList(const JsonSection& section);
 
 public:
-  std::list<CaItem> m_caItems;
+  std::list<ClientCaItem> m_caItems;
 
 PUBLIC_WITH_TESTS_ELSE_PRIVATE:
-  ConfigSection m_config;
+  JsonSection m_config;
 };
 
 } // namespace ndncert