Changing API from ndn.cxx to ndn-cpp

Change-Id: Ie0fe7d75e4ed056dec6e9da906f9c8808ad9a0ca
diff --git a/src/endorse-certificate.cpp b/src/endorse-certificate.cpp
index 80ff165..8101eb3 100644
--- a/src/endorse-certificate.cpp
+++ b/src/endorse-certificate.cpp
@@ -10,93 +10,91 @@
 
 #include "endorse-certificate.h"
 #include "exception.h"
-#include <ndn.cxx/helpers/der/der.h>
-#include <ndn.cxx/helpers/der/visitor/simple-visitor.h>
-#include <ndn.cxx/security/certificate/certificate-subdescrpt.h>
+#include "endorse-extension.pb.h"
+#include <boost/date_time/posix_time/posix_time.hpp>
+#include <boost/iostreams/stream.hpp>
 #include "logging.h"
 
 using namespace std;
 using namespace ndn;
-using namespace ndn::security;
+using namespace ndn::ptr_lib;
+using namespace boost::posix_time;
 
 INIT_LOGGER("EndorseCertificate");
 
 ProfileExtension::ProfileExtension(const ProfileData & profileData)
-  : CertificateExtension("1.3.6.1.5.32.2.1", true, *profileData.encodeToWire())
+  : CertificateExtension("1.3.6.1.5.32.2.1", true, profileData.wireEncode())
 {}
 
 ProfileExtension::ProfileExtension(const ProfileExtension& profileExtension)
-  : CertificateExtension("1.3.6.1.5.32.2.1", true, profileExtension.m_extnValue)
+  : CertificateExtension("1.3.6.1.5.32.2.1", true, profileExtension.extensionValue_)
 {}
 
 ProfileExtension::ProfileExtension(const CertificateExtension& extension)
-  : CertificateExtension(extension.getOID(), extension.getCritical(), extension.getValue())
+  : CertificateExtension(extension.getOid(), extension.getIsCritical(), extension.getValue())
 {
-  if(m_extnID != OID("1.3.6.1.5.32.2.1"))
+  if(extensionId_ != OID("1.3.6.1.5.32.2.1"))
     throw LnException("Wrong ProfileExtension Number!");
 }
 
-Ptr<ProfileData>
+shared_ptr<ProfileData>
 ProfileExtension::getProfileData()
 {
-  Ptr<Blob> dataBlob = Ptr<Blob>(new Blob(m_extnValue.buf (), m_extnValue.size ()));
-  return Ptr<ProfileData>(new ProfileData(*Data::decodeFromWire(dataBlob)));
+  Data data;
+  data.wireDecode(extensionValue_.buf(), extensionValue_.size());
+  return make_shared<ProfileData>(data);
 }
 
-EndorseExtension::EndorseExtension(const vector<string>& endorsedList)
-  : CertificateExtension("1.3.6.1.5.32.2.2", true, *EndorseExtension::prepareValue(endorsedList))
+EndorseExtension::EndorseExtension(const vector<string>& endorseList)
+  : CertificateExtension("1.3.6.1.5.32.2.2", true, EndorseExtension::encodeEndorseList(endorseList))
 {}
 
 EndorseExtension::EndorseExtension(const EndorseExtension& endorseExtension)
-  : CertificateExtension("1.3.6.1.5.32.2.2", true, endorseExtension.m_extnValue)
+  : CertificateExtension("1.3.6.1.5.32.2.2", true, endorseExtension.extensionValue_)
 {}
 
 EndorseExtension::EndorseExtension(const CertificateExtension& extension)
-  : CertificateExtension(extension.getOID(), extension.getCritical(), extension.getValue())
+  : CertificateExtension(extension.getOid(), extension.getIsCritical(), extension.getValue())
 {
-  if(m_extnID != OID("1.3.6.1.5.32.2.2"))
+  if(extensionId_ != OID("1.3.6.1.5.32.2.2"))
     throw LnException("Wrong EndorseExtension Number!");
 }
 
 vector<string>
-EndorseExtension::getEndorsedList()
+EndorseExtension::getEndorseList()
 {
-  vector<string> endorsedList;
+  Chronos::EndorseExtensionMsg endorseExtension;
 
   boost::iostreams::stream
-    <boost::iostreams::array_source> is (m_extnValue.buf(), m_extnValue.size());
+    <boost::iostreams::array_source> is ((const char*)extensionValue_.buf(), extensionValue_.size());
+
+  endorseExtension.ParseFromIstream(&is);
+
+  vector<string> endorseList;
+
+  for(int i = 0; i < endorseExtension.endorseentry_size(); i ++)
+    endorseList.push_back(endorseExtension.endorseentry(i).name());
   
-  Ptr<der::DerSequence> root = DynamicCast<der::DerSequence>(der::DerNode::parse(reinterpret_cast<InputIterator &>(is)));
-  const der::DerNodePtrList & children = root->getChildren();
-  der::SimpleVisitor simpleVisitor;
-
-  for(int i = 0; i < children.size(); i++)
-      endorsedList.push_back(boost::any_cast<string>(children[i]->accept(simpleVisitor)));
-
-  return endorsedList;
+  return endorseList;
 }
 
-Ptr<Blob>
-EndorseExtension::prepareValue(const vector<string>& endorsedList)
+Blob
+EndorseExtension::encodeEndorseList(const vector<string>& endorseList)
 {
-  Ptr<der::DerSequence> root = Ptr<der::DerSequence>::Create();
+  Chronos::EndorseExtensionMsg endorseExtension;
   
-  vector<string>::const_iterator it = endorsedList.begin();
-  for(; it != endorsedList.end(); it++)
-    {
-      Ptr<der::DerPrintableString> entry = Ptr<der::DerPrintableString>(new der::DerPrintableString(*it));
-      root->addChild(entry);
-    }
-  
-  blob_stream blobStream;
-  OutputIterator & start = reinterpret_cast<OutputIterator &> (blobStream);
-  root->encode(start);
+  vector<string>::const_iterator it = endorseList.begin();
+  for(; it != endorseList.end(); it++)
+    endorseExtension.add_endorseentry()->set_name(*it);
 
-  return blobStream.buf ();
+  string encoded;
+  endorseExtension.SerializeToString(&encoded);
+  
+  return Blob((const uint8_t*)encoded.c_str(), encoded.size());
 }
 
 EndorseCertificate::EndorseCertificate(const IdentityCertificate& kskCertificate,
-                                       Ptr<ProfileData> profileData,
+                                       const ProfileData& profileData,
                                        const vector<string>& endorseList)
   : Certificate()
   , m_keyName(kskCertificate.getPublicKeyName())
@@ -104,15 +102,18 @@
   , m_profileData(profileData)
   , m_endorseList(endorseList)
 {
+  time_duration now = microsec_clock::universal_time () - ptime(boost::gregorian::date (1970, boost::gregorian::Jan, 1));
+  uint64_t version = (now.total_seconds () << 12) | (0xFFF & (now.fractional_seconds () / 244));
+
   Name dataName = m_keyName;
-  dataName.append("PROFILE-CERT").append(m_signer).appendVersion();
+  dataName.append("PROFILE-CERT").append(m_signer).appendVersion(version);
   setName(dataName);
 
   setNotBefore(kskCertificate.getNotBefore());
   setNotAfter(kskCertificate.getNotAfter());
-  addSubjectDescription(CertificateSubDescrypt("2.5.4.41", m_keyName.toUri()));
+  addSubjectDescription(CertificateSubjectDescription("2.5.4.41", m_keyName.toUri()));
   setPublicKeyInfo(kskCertificate.getPublicKeyInfo());  
-  addExtension(ProfileExtension(*m_profileData));
+  addExtension(ProfileExtension(m_profileData));
   addExtension(EndorseExtension(m_endorseList));
   
   encode();
@@ -127,15 +128,18 @@
   , m_profileData(endorseCertificate.m_profileData)
   , m_endorseList(endorseList)
 {
+  time_duration now = microsec_clock::universal_time () - ptime(boost::gregorian::date (1970, boost::gregorian::Jan, 1));
+  uint64_t version = (now.total_seconds () << 12) | (0xFFF & (now.fractional_seconds () / 244));
+  
   Name dataName = m_keyName;
-  dataName.append("PROFILE-CERT").append(m_signer).appendVersion();
+  dataName.append("PROFILE-CERT").append(m_signer).appendVersion(version);
   setName(dataName);
   
   setNotBefore(endorseCertificate.getNotBefore());
   setNotAfter(endorseCertificate.getNotAfter());
-  addSubjectDescription(CertificateSubDescrypt("2.5.4.41", m_keyName.toUri()));
+  addSubjectDescription(CertificateSubjectDescription("2.5.4.41", m_keyName.toUri()));
   setPublicKeyInfo(endorseCertificate.getPublicKeyInfo());
-  addExtension(ProfileExtension(*m_profileData));
+  addExtension(ProfileExtension(m_profileData));
   addExtension(EndorseExtension(m_endorseList));
 
   encode();
@@ -153,7 +157,7 @@
   : Certificate(data)
 {
   const Name& dataName = data.getName();
-  name::Component certFlag(string("PROFILE-CERT"));  
+  Name::Component certFlag(Name::fromEscapedString("PROFILE-CERT"));  
   int profileIndex = -1;
   for(int i = 0; i < dataName.size(); i++)
     {
@@ -172,18 +176,18 @@
   OID profileExtensionOID("1.3.6.1.5.32.2.1");
   OID endorseExtensionOID("1.3.6.1.5.32.2.2");
 
-  ExtensionList::iterator it = m_extnList.begin();
-  for(; it != m_extnList.end(); it++)
+  ExtensionList::iterator it = extensionList_.begin();
+  for(; it != extensionList_.end(); it++)
     {
-      if(profileExtensionOID == it->getOID())
+      if(profileExtensionOID == it->getOid())
 	{
           ProfileExtension profileExtension(*it);
-	  m_profileData = profileExtension.getProfileData();
+	  m_profileData = *profileExtension.getProfileData();
 	}
-      if(endorseExtensionOID == it->getOID())
+      if(endorseExtensionOID == it->getOid())
         {
           EndorseExtension endorseExtension(*it);
-          m_endorseList = endorseExtension.getEndorsedList();
+          m_endorseList = endorseExtension.getEndorseList();
         }
     }
 }