core: Add node module files

Change-Id: Ie8da970263efafafc45909d77cacaf661804fae7
diff --git a/core/intermediate-node.cpp b/core/intermediate-node.cpp
new file mode 100644
index 0000000..11c37ff
--- /dev/null
+++ b/core/intermediate-node.cpp
@@ -0,0 +1,72 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014,  Regents of the University of California
+ *
+ * This file is part of NSL (NDN Signature Logger).
+ * See AUTHORS.md for complete list of NSL authors and contributors.
+ *
+ * NSL 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.
+ *
+ * NSL 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 a copy of the GNU General Public License along with
+ * NSL, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @author Peizhen Guo <patrick.guopz@gmail.com>
+ */
+#include "intermediate-node.hpp"
+
+namespace nsl {
+
+bool
+IntermediateNode::isFull() const
+{
+  return m_isFull;
+}
+
+
+
+bool
+IntermediateNode::setIsFull(uint64_t number)
+{
+  Index info = this->getIndex();
+  uint64_t num = info.number;
+  uint64_t lev = info.level;
+  if (double(num) + pow(2, lev) <= number)
+    {
+      m_isFull = true;
+      return m_isFull;
+    }
+  else
+    {
+      m_isFull = false;
+      return m_isFull;
+    }
+
+}
+
+
+
+void
+IntermediateNode::computeHash(ndn::ConstBufferPtr hash_l, ndn::ConstBufferPtr hash_r)
+{
+  ndn::Buffer tmp_buf = *hash_l;
+  for (int i = 0; i < hash_r->size(); i++)
+    {
+      tmp_buf.push_back((*hash_r)[i]);
+    }
+  ndn::ConstBufferPtr digest = ndn::crypto::sha256(tmp_buf.buf(), tmp_buf.size());
+  this->setHash(digest);
+}
+
+void IntermediateNode::computeHashOneSide(ndn::ConstBufferPtr hash_l)
+{
+  ndn::ConstBufferPtr digest = ndn::crypto::sha256(hash_l->buf(), hash_l->size());
+  this->setHash(digest);
+}
+
+} // namespace nsl
diff --git a/core/intermediate-node.hpp b/core/intermediate-node.hpp
new file mode 100644
index 0000000..c2c3c0b
--- /dev/null
+++ b/core/intermediate-node.hpp
@@ -0,0 +1,76 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014,  Regents of the University of California
+ *
+ * This file is part of NSL (NDN Signature Logger).
+ * See AUTHORS.md for complete list of NSL authors and contributors.
+ *
+ * NSL 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.
+ *
+ * NSL 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 a copy of the GNU General Public License along with
+ * NSL, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @author Peizhen Guo <patrick.guopz@gmail.com>
+ */
+#ifndef NLS_CORE_INTERMEDIATE_NODE_HPP
+#define NLS_CORE_INTERMEDIATE_NODE_HPP
+
+#include <stddef.h>
+#include <math.h>
+#include <ndn-cxx/util/crypto.hpp>
+#include "node.hpp"
+
+
+namespace nsl {
+
+
+class IntermediateNode : public Node
+{
+public:
+
+  IntermediateNode()
+    : Node()
+  {
+  }
+
+  IntermediateNode(uint64_t sequenceNumber, uint64_t level, time_t timestamp)
+    : Node(sequenceNumber, level, timestamp), m_isFull(false)
+  {
+  }
+
+  IntermediateNode(const IntermediateNode& new_node)
+    :Node(new_node.getIndex().number, new_node.getIndex().level, 0)
+  {
+    m_isFull = new_node.isFull();
+    this->setHash(new_node.getHash());
+  }
+
+  ~IntermediateNode()
+  {
+  }
+
+  bool
+  setIsFull(uint64_t totalLeafNum);
+
+  bool
+  isFull() const;
+
+  void
+  computeHash(ndn::ConstBufferPtr hash_l, ndn::ConstBufferPtr hash_r);
+
+  void
+  computeHashOneSide(ndn::ConstBufferPtr hash_l);
+
+private:
+  bool m_isFull;
+};
+
+} // namespace nsl
+
+#endif // NLS_CORE_INTERMEDIATE_NODE_HPP
diff --git a/core/leaf.cpp b/core/leaf.cpp
new file mode 100644
index 0000000..33ee527
--- /dev/null
+++ b/core/leaf.cpp
@@ -0,0 +1,40 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014,  Regents of the University of California
+ *
+ * This file is part of NSL (NDN Signature Logger).
+ * See AUTHORS.md for complete list of NSL authors and contributors.
+ *
+ * NSL 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.
+ *
+ * NSL 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 a copy of the GNU General Public License along with
+ * NSL, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @author Peizhen Guo <patrick.guopz@gmail.com>
+ */
+#include "leaf.hpp"
+
+namespace nsl{
+
+ndn::ConstBufferPtr
+Leaf::getData() const
+{
+  return m_data;
+}
+
+
+
+void
+Leaf::computeHash()
+{
+  ndn::ConstBufferPtr digest = ndn::crypto::sha256(m_data->buf(), m_data->size());
+  this->setHash(digest);
+}
+
+} // namespace nsl
diff --git a/core/leaf.hpp b/core/leaf.hpp
new file mode 100644
index 0000000..9576c09
--- /dev/null
+++ b/core/leaf.hpp
@@ -0,0 +1,71 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014,  Regents of the University of California
+ *
+ * This file is part of NSL (NDN Signature Logger).
+ * See AUTHORS.md for complete list of NSL authors and contributors.
+ *
+ * NSL 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.
+ *
+ * NSL 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 a copy of the GNU General Public License along with
+ * NSL, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @author Peizhen Guo <patrick.guopz@gmail.com>
+ */
+#ifndef NLS_CORE_LEAF_HPP
+#define NLS_CORE_LEAF_HPP
+
+#include <vector>
+#include <ndn-cxx/util/crypto.hpp>
+#include "node.hpp"
+
+namespace nsl {
+
+class Leaf : public Node
+{
+public:
+
+  Leaf()
+    : Node()
+  {
+  }
+
+
+  Leaf(ndn::ConstBufferPtr data, uint64_t sequenceNumber, uint64_t level, time_t timestamp)
+    : Node(sequenceNumber, level, timestamp), m_data(data)
+  {
+  }
+
+
+  Leaf(const Leaf& new_leaf)
+    : Node(new_leaf.getIndex().number, new_leaf.getIndex().level, new_leaf.getTimestamp())
+  {
+    m_data = new_leaf.getData();
+    this->setHash(new_leaf.getHash());
+  }
+
+
+  ~Leaf()
+  {
+  }
+
+  ndn::ConstBufferPtr
+  getData() const;
+
+
+  void
+  computeHash();
+
+private:
+  ndn::ConstBufferPtr m_data;
+};
+
+} // namespace nsl
+
+#endif // NLS_CORE_LEAF_HPP
diff --git a/core/node.cpp b/core/node.cpp
new file mode 100644
index 0000000..66921cd
--- /dev/null
+++ b/core/node.cpp
@@ -0,0 +1,63 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014,  Regents of the University of California
+ *
+ * This file is part of NSL (NDN Signature Logger).
+ * See AUTHORS.md for complete list of NSL authors and contributors.
+ *
+ * NSL 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.
+ *
+ * NSL 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 a copy of the GNU General Public License along with
+ * NSL, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @author Peizhen Guo <patrick.guopz@gmail.com>
+ */
+#include "node.hpp"
+
+namespace nsl {
+
+Node::Node(uint64_t sequenceNumber, uint64_t level, time_t timestamp)
+{
+  m_index.number = sequenceNumber;
+  m_index.level = level;
+  m_timeStamp = timestamp;
+}
+
+
+const Index&
+Node::getIndex() const
+{
+  return m_index;
+}
+
+
+
+time_t
+Node::getTimestamp() const
+{
+  return m_timeStamp;
+}
+
+
+
+void
+Node::setHash(ndn::ConstBufferPtr digest)
+{
+  m_hash = digest;
+}
+
+
+
+ndn::ConstBufferPtr
+Node::getHash() const
+{
+  return m_hash;
+}
+
+} // namespace nsl
diff --git a/core/node.hpp b/core/node.hpp
new file mode 100644
index 0000000..be2ed92
--- /dev/null
+++ b/core/node.hpp
@@ -0,0 +1,107 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014,  Regents of the University of California
+ *
+ * This file is part of NSL (NDN Signature Logger).
+ * See AUTHORS.md for complete list of NSL authors and contributors.
+ *
+ * NSL 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.
+ *
+ * NSL 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 a copy of the GNU General Public License along with
+ * NSL, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @author Peizhen Guo <patrick.guopz@gmail.com>
+ */
+
+#ifndef NLS_CORE_NODE_HPP
+#define NLS_CORE_NODE_HPP
+#include <stddef.h>
+#include <time.h>
+
+#include <ndn-cxx/util/crypto.hpp>
+
+namespace nsl {
+
+class Index
+{
+public:
+  Index()
+  {
+  }
+
+  Index(const Index& idx)
+    : number(idx.number),
+      level(idx.level)
+  {
+  }
+
+  bool operator<(const Index& other) const
+  {
+    if (number < other.number)
+      {
+        return true;
+      }
+    else if (number == other.number)
+      {
+        return level < other.level;
+      }
+    else
+      {
+        return false;
+      }
+
+  }
+
+public:
+  uint64_t number;
+  uint64_t level;
+};
+
+
+class Node
+{
+public:
+
+  Node()
+  {
+  }
+
+
+  Node(uint64_t sequenceNumber, uint64_t level, time_t timestamp);
+
+
+  ~Node()
+  {
+  }
+
+
+  const Index&
+  getIndex() const;
+
+
+  time_t
+  getTimestamp() const;
+
+
+  void
+  setHash(ndn::ConstBufferPtr digest);
+
+
+  ndn::ConstBufferPtr
+  getHash() const;
+
+private:
+  ndn::ConstBufferPtr m_hash;
+  Index m_index;   // Node index.number starts from 0 (the index of current root)
+  time_t m_timeStamp;
+};
+
+} // namespace nsl
+
+#endif // NLS_CORE_NODE_HPP
diff --git a/tests/core/test-node.cpp b/tests/core/test-node.cpp
new file mode 100644
index 0000000..9476d86
--- /dev/null
+++ b/tests/core/test-node.cpp
@@ -0,0 +1,79 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014,  Regents of the University of California
+ *
+ * This file is part of NSL (NDN Signature Logger).
+ * See AUTHORS.md for complete list of NSL authors and contributors.
+ *
+ * NSL 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.
+ *
+ * NSL 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 a copy of the GNU General Public License along with
+ * NSL, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @author Peizhen Guo <patrick.guopz@gmail.com>
+ */
+#include <stdint.h>
+#include <iostream>
+#include <boost-test.hpp>
+
+#include "leaf.hpp"
+#include "intermediate-node.hpp"
+
+namespace nsl {
+
+
+BOOST_AUTO_TEST_SUITE(NodeTest)
+
+
+BOOST_AUTO_TEST_CASE(LeafTest)
+{
+  //Test the constructor & getFunc
+  Index idx;
+  idx.number = 1;
+  idx.level = 0;
+  ndn::Buffer buffer;
+  for (int i = 0; i < 10; i++)
+    {
+      buffer.push_back(i + 65); // from A to J
+    }
+  ndn::ConstBufferPtr p_buf = boost::make_shared<const ndn::Buffer>(buffer);
+  Leaf leaf_node(p_buf, idx.number, idx.level, 0);
+  BOOST_CHECK(leaf_node.getIndex().number == 1);
+  BOOST_CHECK(leaf_node.getIndex().level == 0);
+  ndn::ConstBufferPtr data = leaf_node.getData();
+  for (int i = 0; i < data->size(); i++)
+    {
+      std::cout<<(*data)[i]<<' ';
+    }
+  std::cout<<"Data Finished"<<std::endl;
+  //Test hash computation
+  leaf_node.computeHash();
+  ndn::ConstBufferPtr hash = leaf_node.getHash();
+  for (int i = 0; i < hash->size(); i++)
+    {
+      std::cout<<int((*hash)[i])<<' ';
+    }
+  std::cout<<"Hash Finished"<<std::endl;
+}
+
+BOOST_AUTO_TEST_CASE(IntermediateNodeTest)
+{
+  //Test update full condition
+  IntermediateNode inter_node(2,1,0);
+  inter_node.setIsFull(4);
+  BOOST_CHECK(inter_node.isFull() == true);
+  inter_node.setIsFull(2);
+  BOOST_CHECK(inter_node.isFull() == false);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+
+
+} // namespace nsl