communication: sync protocol adapater to add psync

refs: #4082

Change-Id: Ibe4649e709dfbc3cdc1f2afbfc4ff03f75a3f136
diff --git a/tests/test-conf-file-processor.cpp b/tests/test-conf-file-processor.cpp
index 14fce9a..99ef78f 100644
--- a/tests/test-conf-file-processor.cpp
+++ b/tests/test-conf-file-processor.cpp
@@ -44,6 +44,7 @@
   "  lsa-refresh-time 1800\n"
   "  lsa-interest-lifetime 3\n"
   "  router-dead-interval 86400\n"
+  "  sync-protocol psync\n"
   "  sync-interest-lifetime 10000\n"
   "  seq-dir /tmp\n"
   "}\n\n";
@@ -175,6 +176,7 @@
   BOOST_CHECK_EQUAL(conf.getChronosyncPrefix(), ndn::Name("/localhop/ndn/nlsr/sync").appendVersion(ConfParameter::SYNC_VERSION));
   BOOST_CHECK_EQUAL(conf.getLsaPrefix(), "/localhop/ndn/nlsr/LSA");
   BOOST_CHECK_EQUAL(conf.getLsaRefreshTime(), 1800);
+  BOOST_CHECK_EQUAL(conf.getSyncProtocol(), SYNC_PROTOCOL_PSYNC);
   BOOST_CHECK_EQUAL(conf.getLsaInterestLifetime(), ndn::time::seconds(3));
   BOOST_CHECK_EQUAL(conf.getRouterDeadInterval(), 86400);
   BOOST_CHECK_EQUAL(conf.getSyncInterestLifetime(), ndn::time::milliseconds(10000));
diff --git a/tests/test-conf-parameter.cpp b/tests/test-conf-parameter.cpp
index 55a65b1..0fdf631 100644
--- a/tests/test-conf-parameter.cpp
+++ b/tests/test-conf-parameter.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014-2017,  The University of Memphis,
+ * Copyright (c) 2014-2018,  The University of Memphis,
  *                           Regents of the University of California
  *
  * This file is part of NLSR (Named-data Link State Routing).
@@ -53,6 +53,8 @@
 
   cp1.setLsaInterestLifetime(ndn::time::seconds(1));
 
+  cp1.setSyncProtocol(SYNC_PROTOCOL_PSYNC);
+
   cp1.setRouterDeadInterval(10);
 
   cp1.setMaxFacesPerPrefix(50);
@@ -85,6 +87,8 @@
 
   BOOST_CHECK_EQUAL(cp1.getLsaInterestLifetime(), ndn::time::seconds(1));
 
+  BOOST_CHECK_EQUAL(cp1.getSyncProtocol(), SYNC_PROTOCOL_PSYNC);
+
   BOOST_CHECK_EQUAL(cp1.getRouterDeadInterval(), 10);
 
   BOOST_CHECK_EQUAL(cp1.getMaxFacesPerPrefix(), 50);
diff --git a/tests/test-sync-logic-handler.cpp b/tests/test-sync-logic-handler.cpp
index ab9732d..021743c 100644
--- a/tests/test-sync-logic-handler.cpp
+++ b/tests/test-sync-logic-handler.cpp
@@ -60,18 +60,31 @@
     addIdentity(conf.getRouterPrefix());
   }
 
+  template <int32_t N>
+  void
+  setSyncProtocol()
+  {
+    nlsr.getConfParameter().setSyncProtocol(N);
+    conf.setSyncProtocol(N);
+  }
+
+  template <int32_t N>
   void
   receiveUpdate(std::string prefix, uint64_t seqNo, SyncLogicHandler& p_sync)
   {
-    chronosync::MissingDataInfo info = {ndn::Name(prefix).appendNumber(1), 0, seqNo};
-
-    std::vector<chronosync::MissingDataInfo> updates;
-    updates.push_back(info);
-
     this->advanceClocks(ndn::time::milliseconds(1), 10);
     face.sentInterests.clear();
 
-    p_sync.onChronoSyncUpdate(updates);
+    if (N == SYNC_PROTOCOL_CHRONOSYNC) {
+      std::vector<chronosync::MissingDataInfo> updates;
+      updates.push_back({ndn::Name(prefix).appendNumber(1), 0, seqNo});
+      p_sync.m_syncLogic->onChronoSyncUpdate(updates);
+    }
+    else {
+      std::vector<psync::MissingDataInfo> updates;
+      updates.push_back({ndn::Name(prefix), 0, seqNo});
+     p_sync.m_syncLogic->onPSyncUpdate(updates);
+    }
 
     this->advanceClocks(ndn::time::milliseconds(1), 10);
   }
@@ -90,14 +103,19 @@
                                              Lsa::Type::COORDINATE};
 };
 
+using boost::mpl::int_;
+using Protocols = boost::mpl::vector<int_<SYNC_PROTOCOL_CHRONOSYNC>, int_<SYNC_PROTOCOL_PSYNC>>;
+
 BOOST_FIXTURE_TEST_SUITE(TestSyncLogicHandler, SyncLogicFixture)
 
 /* Tests that when SyncLogicHandler receives an LSA of either Name or
    Adjacency type that appears to be newer, it will emit to its signal
    with those LSA details.
  */
-BOOST_AUTO_TEST_CASE(UpdateForOtherLS)
+BOOST_AUTO_TEST_CASE_TEMPLATE(UpdateForOtherLS, SyncProtocol, Protocols)
 {
+  setSyncProtocol<SyncProtocol::value>();
+
   SyncLogicHandler sync{face, testIsLsaNew, conf};
   sync.createSyncLogic(conf.getChronosyncPrefix());
 
@@ -116,7 +134,7 @@
         BOOST_CHECK_EQUAL(sequenceNumber, syncSeqNo);
       });
 
-    receiveUpdate(updateName, syncSeqNo, sync);
+    receiveUpdate<SyncProtocol::value>(updateName, syncSeqNo, sync);
   }
 }
 
@@ -124,8 +142,10 @@
    either Coordinate or Name type that appears to be newer, it will
    emit to its signal with those LSA details.
  */
-BOOST_AUTO_TEST_CASE(UpdateForOtherHR)
+BOOST_AUTO_TEST_CASE_TEMPLATE(UpdateForOtherHR, SyncProtocol, Protocols)
 {
+  setSyncProtocol<SyncProtocol::value>();
+
   conf.setHyperbolicState(HYPERBOLIC_STATE_ON);
 
   SyncLogicHandler sync{face, testIsLsaNew, conf};
@@ -144,7 +164,7 @@
         BOOST_CHECK_EQUAL(sequenceNumber, syncSeqNo);
       });
 
-    receiveUpdate(updateName, syncSeqNo, sync);
+    receiveUpdate<SyncProtocol::value>(updateName, syncSeqNo, sync);
   }
 }
 
@@ -152,8 +172,10 @@
    any type that appears to be newer, it will emit to its signal with
    those LSA details.
  */
-BOOST_AUTO_TEST_CASE(UpdateForOtherHRDry)
+BOOST_AUTO_TEST_CASE_TEMPLATE(UpdateForOtherHRDry, SyncProtocol, Protocols)
 {
+  setSyncProtocol<SyncProtocol::value>();
+
   conf.setHyperbolicState(HYPERBOLIC_STATE_DRY_RUN);
 
   SyncLogicHandler sync{face, testIsLsaNew, conf};
@@ -171,7 +193,7 @@
         BOOST_CHECK_EQUAL(sequenceNumber, syncSeqNo);
       });
 
-    receiveUpdate(updateName, syncSeqNo, sync);
+    receiveUpdate<SyncProtocol::value>(updateName, syncSeqNo, sync);
   }
 }
 
@@ -179,8 +201,10 @@
    details matching this router's details, it will *not* emit to its
    signal those LSA details.
  */
-BOOST_AUTO_TEST_CASE(NoUpdateForSelf)
+BOOST_AUTO_TEST_CASE_TEMPLATE(NoUpdateForSelf, SyncProtocol, Protocols)
 {
+  setSyncProtocol<SyncProtocol::value>();
+
   const uint64_t sequenceNumber = 1;
 
   SyncLogicHandler sync{face, testIsLsaNew, conf};
@@ -197,7 +221,7 @@
         BOOST_FAIL("Updates for self should not be emitted!");
       });
 
-    receiveUpdate(updateName.toUri(), sequenceNumber, sync);
+    receiveUpdate<SyncProtocol::value>(updateName.toUri(), sequenceNumber, sync);
   }
 }
 
@@ -205,8 +229,10 @@
    details that do not match the expected format, it will *not* emit
    to its signal those LSA details.
  */
-BOOST_AUTO_TEST_CASE(MalformedUpdate)
+BOOST_AUTO_TEST_CASE_TEMPLATE(MalformedUpdate, SyncProtocol, Protocols)
 {
+  setSyncProtocol<SyncProtocol::value>();
+
   const uint64_t sequenceNumber = 1;
 
   SyncLogicHandler sync{face, testIsLsaNew, conf};
@@ -221,7 +247,7 @@
         BOOST_FAIL("Malformed updates should not be emitted!");
       });
 
-    receiveUpdate(updateName.toUri(), sequenceNumber, sync);
+    receiveUpdate<SyncProtocol::value>(updateName.toUri(), sequenceNumber, sync);
   }
 }
 
@@ -229,8 +255,10 @@
    details that do not appear to be new, it will *not* emit to its
    signal those LSA details.
  */
-BOOST_AUTO_TEST_CASE(LsaNotNew)
+BOOST_AUTO_TEST_CASE_TEMPLATE(LsaNotNew, SyncProtocol, Protocols)
 {
+  setSyncProtocol<SyncProtocol::value>();
+
   auto testLsaAlwaysFalse = [] (const ndn::Name& routerName, const Lsa::Type& lsaType,
                            const uint64_t& sequenceNumber) {
     return false;
@@ -248,15 +276,16 @@
                            CONFIG_SITE + "/%C1.Router/other-router/" +
                            std::to_string(Lsa::Type::NAME);
 
-  receiveUpdate(updateName, sequenceNumber, sync);
+  receiveUpdate<SyncProtocol::value>(updateName, sequenceNumber, sync);
 }
 
 /* Tests that SyncLogicHandler successfully concatenates configured
    variables together to form the necessary prefixes to advertise
    through ChronoSync.
  */
-BOOST_AUTO_TEST_CASE(UpdatePrefix)
+BOOST_AUTO_TEST_CASE_TEMPLATE(UpdatePrefix, SyncProtocol, Protocols)
 {
+  setSyncProtocol<SyncProtocol::value>();
 
   SyncLogicHandler sync{face, testIsLsaNew, conf};
 
@@ -281,8 +310,9 @@
    NB: This test is as much an Nlsr class test as a
    SyncLogicHandler class test, but it rides the line and ends up here.
  */
-BOOST_AUTO_TEST_CASE(createSyncLogicOnInitialization) // Bug #2649
+BOOST_AUTO_TEST_CASE_TEMPLATE(createSyncLogicOnInitialization, SyncProtocol, Protocols) // Bug #2649
 {
+  setSyncProtocol<SyncProtocol::value>();
   nlsr.initialize();
 
   // Make sure an adjacency LSA has not been built yet
diff --git a/tests/test-sync-protocol-adapter.cpp b/tests/test-sync-protocol-adapter.cpp
new file mode 100644
index 0000000..344f9a0
--- /dev/null
+++ b/tests/test-sync-protocol-adapter.cpp
@@ -0,0 +1,117 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2018,  The University of Memphis,
+ *                           Regents of the University of California,
+ *                           Arizona Board of Regents.
+ *
+ * This file is part of NLSR (Named-data Link State Routing).
+ * See AUTHORS.md for complete list of NLSR authors and contributors.
+ *
+ * NLSR 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.
+ *
+ * NLSR 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
+ * NLSR, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ **/
+
+#include "communication/sync-protocol-adapter.hpp"
+#include "test-common.hpp"
+#include "boost-test.hpp"
+
+#include <ndn-cxx/util/dummy-client-face.hpp>
+
+#include <boost/mpl/int.hpp>
+#include <boost/mpl/vector.hpp>
+
+namespace nlsr {
+namespace test {
+
+using namespace ndn;
+
+class SyncProtocolAdapterFixture : public UnitTestTimeFixture
+{
+public:
+  SyncProtocolAdapterFixture()
+   : syncPrefix("/localhop/ndn/nlsr/sync/")
+   , nameLsaUserPrefix("/localhop/ndn/nlsr/LSA/NAME")
+   , syncInterestLifetime(time::seconds(60))
+  {
+  	syncPrefix.appendVersion(4);
+  }
+
+  template <int32_t T>
+  void
+  addNodes()
+  {
+    for (int i = 0; i < 2; i++) {
+      faces[i] = std::make_shared<ndn::util::DummyClientFace>(m_ioService,
+                                                              util::DummyClientFace::Options{true, true});
+      userPrefixes[i] = Name(nameLsaUserPrefix).appendNumber(i);
+      nodes[i] = std::make_shared<SyncProtocolAdapter>(*faces[i], T, syncPrefix,
+      	                                               userPrefixes[i],
+                                                       syncInterestLifetime,
+                                                       [i, this] (const ndn::Name& updateName,
+                                                                   uint64_t highSeq) {
+                                                         prefixToSeq[i].emplace(updateName, highSeq);
+                                                       });
+    }
+
+    faces[0]->linkTo(*faces[1]);
+    advanceClocks(ndn::time::milliseconds(10), 10);
+  }
+
+public:
+  Name syncPrefix, nameLsaUserPrefix;
+  Name userPrefixes[2];
+  time::milliseconds syncInterestLifetime;
+  std::shared_ptr<ndn::util::DummyClientFace> faces[2];
+  std::shared_ptr<SyncProtocolAdapter> nodes[2];
+  std::map<ndn::Name, uint64_t> prefixToSeq[2];
+};
+
+using boost::mpl::int_;
+using Protocols = boost::mpl::vector<int_<SYNC_PROTOCOL_CHRONOSYNC>, int_<SYNC_PROTOCOL_PSYNC>>;
+
+BOOST_FIXTURE_TEST_SUITE(TestSyncProtocolAdapter, SyncProtocolAdapterFixture)
+
+BOOST_AUTO_TEST_CASE_TEMPLATE(Sync, SyncProtocol, Protocols)
+{
+  addNodes<SyncProtocol::value>();
+
+  nodes[0]->publishUpdate(userPrefixes[0], 10);
+  advanceClocks(ndn::time::milliseconds(1000), 100);
+
+  auto it = prefixToSeq[1].find(userPrefixes[0]);
+  BOOST_CHECK(it != prefixToSeq[1].end());
+  BOOST_CHECK_EQUAL(it->first, userPrefixes[0]);
+  BOOST_CHECK_EQUAL(it->second, 10);
+
+  nodes[1]->publishUpdate(userPrefixes[1], 100);
+  advanceClocks(ndn::time::milliseconds(1000), 100);
+
+  it = prefixToSeq[0].find(userPrefixes[1]);
+  BOOST_CHECK(it != prefixToSeq[0].end());
+  BOOST_CHECK_EQUAL(it->first, userPrefixes[1]);
+  BOOST_CHECK_EQUAL(it->second, 100);
+
+  Name adjLsaUserPrefix("/localhop/ndn/nlsr/LSA/ADJACENCY");
+  nodes[0]->addUserNode(adjLsaUserPrefix);
+  advanceClocks(ndn::time::milliseconds(1000), 100);
+  nodes[0]->publishUpdate(adjLsaUserPrefix, 10);
+  advanceClocks(ndn::time::milliseconds(1000), 100);
+
+  it = prefixToSeq[1].find(adjLsaUserPrefix);
+  BOOST_CHECK(it != prefixToSeq[1].end());
+  BOOST_CHECK_EQUAL(it->first, adjLsaUserPrefix);
+  BOOST_CHECK_EQUAL(it->second, 10);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace test
+} // namespace nlsr
\ No newline at end of file