src: configuration file parsing

used boost::property_tree::info_parser for parsing nlsr's configuration file and
changed configuration command style to info command style. Removed tokenizer from
nlsr

Refs: #1542

Change-Id: If017ddd7eef5caa59b33940bfc27a71aa4de266b
diff --git a/src/route/fib.cpp b/src/route/fib.cpp
index fc823c1..a68d4d6 100644
--- a/src/route/fib.cpp
+++ b/src/route/fib.cpp
@@ -1,8 +1,10 @@
 #include <list>
 #include <cmath>
+#include <ndn-cxx/common.hpp>
 
 #include "nlsr.hpp"
 #include "nexthop-list.hpp"
+#include "face-map.hpp"
 #include "fib.hpp"
 
 
@@ -45,19 +47,16 @@
   std::list<FibEntry>::iterator it = std::find_if(m_table.begin(),
                                                   m_table.end(),
                                                   bind(&fibEntryNameCompare, _1, name));
-  if (it != m_table.end())
-  {
+  if (it != m_table.end()) {
     std::cout << "Entry found with Seq Num: " << feSeqNum << std::endl;
-    if (it->getSeqNo() == feSeqNum)
-    {
+    if (it->getSeqNo() == feSeqNum) {
       std::cout << "Refreshing the FIB entry" << std::endl;
       for (std::list<NextHop>::iterator nhit =
              (*it).getNexthopList().getNextHops().begin();
-           nhit != (*it).getNexthopList().getNextHops().end(); nhit++)
-      {
+           nhit != (*it).getNexthopList().getNextHops().end(); nhit++) {
         // add entry to NDN-FIB
-        registerPrefixInNfd(it->getName(), nhit->getConnectingFace(),
-                            std::ceil(nhit->getRouteCost()));
+        registerPrefix(it->getName(), nhit->getConnectingFaceUri(),
+                       std::ceil(nhit->getRouteCost()), m_refreshTime);
       }
       // increase sequence number and schedule refresh again
       it->setSeqNo(feSeqNum + 1);
@@ -74,23 +73,19 @@
   std::list<FibEntry>::iterator it = std::find_if(m_table.begin(),
                                                   m_table.end(),
                                                   bind(&fibEntryNameCompare, _1, name));
-  if (it != m_table.end())
-  {
+  if (it != m_table.end()) {
     for (std::list<NextHop>::iterator nhit =
            (*it).getNexthopList().getNextHops().begin();
-         nhit != (*it).getNexthopList().getNextHops().end(); nhit++)
-    {
+         nhit != (*it).getNexthopList().getNextHops().end(); nhit++) {
       //remove entry from NDN-FIB
-      if (!m_nlsr.getAdjacencyList().isNeighbor(it->getName()))
-      {
-        unregisterPrefixFromNfd(it->getName(), nhit->getConnectingFace());
+      if (!m_nlsr.getAdjacencyList().isNeighbor(it->getName())) {
+        unregisterPrefix(it->getName(), nhit->getConnectingFaceUri());
       }
       else
       {
-        if (m_nlsr.getAdjacencyList().getAdjacent(it->getName()).getConnectingFace() !=
-            nhit->getConnectingFace())
-        {
-          unregisterPrefixFromNfd(it->getName(), nhit->getConnectingFace());
+        if (m_nlsr.getAdjacencyList().getAdjacent(it->getName()).getConnectingFaceUri() !=
+            nhit->getConnectingFaceUri()) {
+          unregisterPrefix(it->getName(), nhit->getConnectingFaceUri());
         }
       }
     }
@@ -112,20 +107,17 @@
   std::list<FibEntry>::iterator it = std::find_if(m_table.begin(),
                                                   m_table.end(),
                                                   bind(&fibEntryNameCompare, _1, name));
-  if (it == m_table.end())
-  {
-    if (nextHopList.getSize() > 0)
-    {
+  if (it == m_table.end()) {
+    if (nextHopList.getSize() > 0) {
       nextHopList.sort();
       FibEntry newEntry(name);
       std::list<NextHop> nhl = nextHopList.getNextHops();
       std::list<NextHop>::iterator nhit = nhl.begin();
-      for (int i = startFace; i < endFace && nhit != nhl.end(); ++nhit, i++)
-      {
+      for (int i = startFace; i < endFace && nhit != nhl.end(); ++nhit, i++) {
         newEntry.getNexthopList().addNextHop((*nhit));
         //Add entry to NDN-FIB
-        registerPrefixInNfd(name, nhit->getConnectingFace(),
-                            std::ceil(nhit->getRouteCost()));
+        registerPrefix(name, nhit->getConnectingFaceUri(),
+                       std::ceil(nhit->getRouteCost()), m_refreshTime);
       }
       newEntry.getNexthopList().sort();
       newEntry.setTimeToRefresh(m_refreshTime);
@@ -134,30 +126,26 @@
       m_table.push_back(newEntry);
     }
   }
-  else
-  {
+  else {
     std::cout << "Old FIB Entry" << std::endl;
-    if (nextHopList.getSize() > 0)
-    {
+    if (nextHopList.getSize() > 0) {
       nextHopList.sort();
-      if (!it->isEqualNextHops(nextHopList))
-      {
+      if (!it->isEqualNextHops(nextHopList)) {
         std::list<NextHop> nhl = nextHopList.getNextHops();
         std::list<NextHop>::iterator nhit = nhl.begin();
         // Add first Entry to NDN-FIB
-        registerPrefixInNfd(name, nhit->getConnectingFace(),
-                            std::ceil(nhit->getRouteCost()));
-        removeHop(it->getNexthopList(), nhit->getConnectingFace(), name);
+        registerPrefix(name, nhit->getConnectingFaceUri(),
+                       std::ceil(nhit->getRouteCost()), m_refreshTime);
+        removeHop(it->getNexthopList(), nhit->getConnectingFaceUri(), name);
         it->getNexthopList().reset();
         it->getNexthopList().addNextHop((*nhit));
         ++startFace;
         ++nhit;
-        for (int i = startFace; i < endFace && nhit != nhl.end(); ++nhit, i++)
-        {
+        for (int i = startFace; i < endFace && nhit != nhl.end(); ++nhit, i++) {
           it->getNexthopList().addNextHop((*nhit));
           //Add Entry to NDN_FIB
-          registerPrefixInNfd(name, nhit->getConnectingFace(),
-                              std::ceil(nhit->getRouteCost()));
+          registerPrefix(name, nhit->getConnectingFaceUri(),
+                         std::ceil(nhit->getRouteCost()), m_refreshTime);
         }
       }
       it->setTimeToRefresh(m_refreshTime);
@@ -168,8 +156,7 @@
       (*it).setExpiringEventId(scheduleEntryRefreshing(it->getName() ,
                                                        it->getSeqNo(), m_refreshTime));
     }
-    else
-    {
+    else {
       remove(name);
     }
   }
@@ -181,33 +168,27 @@
 Fib::clean()
 {
   for (std::list<FibEntry>::iterator it = m_table.begin(); it != m_table.end();
-       ++it)
-  {
+       ++it) {
     std::cout << "Cancellling Scheduled event" << std::endl;
     std::cout << "Name: " << it->getName() << "Seq num: " << it->getSeqNo() <<
               std::endl;
     cancelScheduledExpiringEvent((*it).getExpiringEventId());
     for (std::list<NextHop>::iterator nhit =
            (*it).getNexthopList().getNextHops().begin();
-         nhit != (*it).getNexthopList().getNextHops().end(); nhit++)
-    {
+         nhit != (*it).getNexthopList().getNextHops().end(); nhit++) {
       //Remove entry from NDN-FIB
-      if (!m_nlsr.getAdjacencyList().isNeighbor(it->getName()))
-      {
-        unregisterPrefixFromNfd(it->getName(), nhit->getConnectingFace());
+      if (!m_nlsr.getAdjacencyList().isNeighbor(it->getName())) {
+        unregisterPrefix(it->getName(), nhit->getConnectingFaceUri());
       }
-      else
-      {
-        if (m_nlsr.getAdjacencyList().getAdjacent(it->getName()).getConnectingFace() !=
-            nhit->getConnectingFace())
-        {
-          unregisterPrefixFromNfd(it->getName(), nhit->getConnectingFace());
+      else {
+        if (m_nlsr.getAdjacencyList().getAdjacent(it->getName()).getConnectingFaceUri() !=
+            nhit->getConnectingFaceUri()) {
+          unregisterPrefix(it->getName(), nhit->getConnectingFaceUri());
         }
       }
     }
   }
-  if (m_table.size() > 0)
-  {
+  if (m_table.size() > 0) {
     m_table.clear();
   }
 }
@@ -217,37 +198,30 @@
                              uint32_t maxFacesPerPrefix)
 {
   int endFace = 0;
-  if ((maxFacesPerPrefix == 0) || (nextHopList.getSize() <= maxFacesPerPrefix))
-  {
+  if ((maxFacesPerPrefix == 0) || (nextHopList.getSize() <= maxFacesPerPrefix)) {
     return nextHopList.getSize();
   }
-  else
-  {
+  else {
     return maxFacesPerPrefix;
   }
   return endFace;
 }
 
 void
-Fib::removeHop(NexthopList& nl, uint32_t doNotRemoveHopFaceId,
+Fib::removeHop(NexthopList& nl, const std::string& doNotRemoveHopFaceUri,
                const ndn::Name& name)
 {
   for (std::list<NextHop>::iterator it = nl.getNextHops().begin();
-       it != nl.getNextHops().end();   ++it)
-  {
-    if (it->getConnectingFace() != doNotRemoveHopFaceId)
-    {
+       it != nl.getNextHops().end();   ++it) {
+    if (it->getConnectingFaceUri() != doNotRemoveHopFaceUri) {
       //Remove FIB Entry from NDN-FIB
-      if (!m_nlsr.getAdjacencyList().isNeighbor(name))
-      {
-        unregisterPrefixFromNfd(name, it->getConnectingFace());
+      if (!m_nlsr.getAdjacencyList().isNeighbor(name)) {
+        unregisterPrefix(name, it->getConnectingFaceUri());
       }
-      else
-      {
-        if (m_nlsr.getAdjacencyList().getAdjacent(name).getConnectingFace() !=
-            it->getConnectingFace())
-        {
-          unregisterPrefixFromNfd(name, it->getConnectingFace());
+      else {
+        if (m_nlsr.getAdjacencyList().getAdjacent(name).getConnectingFaceUri() !=
+            it->getConnectingFaceUri()) {
+          unregisterPrefix(name, it->getConnectingFaceUri());
         }
       }
     }
@@ -255,43 +229,88 @@
 }
 
 void
-Fib::registerPrefixInNfd(const ndn::Name& namePrefix, uint64_t faceId,
-                         uint64_t faceCost)
+Fib::registerPrefix(const ndn::Name& namePrefix, const std::string& faceUri,
+                    uint64_t faceCost, uint64_t timeout)
+{
+  ndn::nfd::ControlParameters faceParameters;
+  faceParameters
+  .setUri(faceUri);
+
+  m_controller.start<ndn::nfd::FaceCreateCommand>(faceParameters,
+                                                  ndn::bind(&Fib::registerPrefixInNfd, this,_1,
+                                                            namePrefix, faceCost, timeout),
+                                                  ndn::bind(&Fib::onFailure, this, _1, _2,
+                                                             "Failed in name registration"));
+  
+}
+
+void
+Fib::registerPrefixInNfd(const ndn::nfd::ControlParameters& faceCreateResult, 
+                         const ndn::Name& namePrefix, uint64_t faceCost, uint64_t timeout)
 {
   ndn::nfd::ControlParameters controlParameters;
   controlParameters
-  .setName(namePrefix)
-  .setCost(faceCost)
-  .setFaceId(faceId)
-  .setExpirationPeriod(ndn::time::milliseconds(m_refreshTime * 1000))
-  .setOrigin(128);
+    .setName(namePrefix)
+    .setFaceId(faceCreateResult.getFaceId())
+    .setCost(faceCost)
+    .setExpirationPeriod(ndn::time::milliseconds(timeout * 1000))
+    .setOrigin(128);
   m_controller.start<ndn::nfd::RibRegisterCommand>(controlParameters,
-                                                   ndn::bind(&Fib::onSuccess, this, _1,
-                                                             "Successful in name registration"),
+                                                   ndn::bind(&Fib::onRegistration, this, _1,
+                                                             "Successful in name registration",
+                                                             faceCreateResult.getUri()),
                                                    ndn::bind(&Fib::onFailure, this, _1, _2,
                                                              "Failed in name registration"));
 }
 
 void
-Fib::unregisterPrefixFromNfd(const ndn::Name& namePrefix, uint64_t faceId)
+Fib::unregisterPrefix(const ndn::Name& namePrefix, const std::string& faceUri)
 {
-  ndn::nfd::ControlParameters controlParameters;
-  controlParameters
-  .setName(namePrefix)
-  .setFaceId(faceId)
-  .setOrigin(128);
-  m_controller.start<ndn::nfd::RibUnregisterCommand>(controlParameters,
+  uint32_t faceId = m_faceMap.getFaceId(faceUri);
+  if (faceId > 0) {
+    ndn::nfd::ControlParameters controlParameters;
+    controlParameters
+      .setName(namePrefix)
+      .setFaceId(faceId)
+      .setOrigin(128);
+    m_controller.start<ndn::nfd::RibUnregisterCommand>(controlParameters,
                                                      ndn::bind(&Fib::onSuccess, this, _1,
                                                                "Successful in unregistering name"),
                                                      ndn::bind(&Fib::onFailure, this, _1, _2,
                                                                "Failed in unregistering name"));
+  }
 }
 
 void
+Fib::setStrategy(const ndn::Name& name, const std::string& strategy)
+{
+  ndn::nfd::ControlParameters parameters;
+  parameters
+    .setName(name)
+    .setStrategy(strategy);
+
+  m_controller.start<ndn::nfd::StrategyChoiceSetCommand>(parameters,
+                                                         bind(&Fib::onSuccess, this, _1,
+                                                              "Successfully set strategy choice"),
+                                                         bind(&Fib::onFailure, this, _1, _2,
+                                                              "Failed to set strategy choice"));
+}
+
+void
+Fib::onRegistration(const ndn::nfd::ControlParameters& commandSuccessResult,
+                    const std::string& message, const std::string& faceUri)
+{
+  //std::cout << message << ": " << commandSuccessResult << std::endl;
+  m_faceMap.update(faceUri, commandSuccessResult.getFaceId());
+  m_faceMap.print();
+}
+
+
+void
 Fib::onSuccess(const ndn::nfd::ControlParameters& commandSuccessResult,
                const std::string& message)
 {
-  std::cout << message << ": " << commandSuccessResult << std::endl;
+  //std::cout << message << ": " << commandSuccessResult << std::endl;
 }
 
 void
@@ -307,8 +326,7 @@
 {
   cout << "-------------------FIB-----------------------------" << endl;
   for (std::list<FibEntry>::iterator it = m_table.begin(); it != m_table.end();
-       ++it)
-  {
+       ++it) {
     cout << (*it);
   }
 }