src: Make conf keys with default values optional

refs: #1949

Change-Id: Ia9ba8707d27f2db96264f0480ffc72fd44a75099
diff --git a/src/conf-file-processor.cpp b/src/conf-file-processor.cpp
index 3b6285b..1b5dc61 100644
--- a/src/conf-file-processor.cpp
+++ b/src/conf-file-processor.cpp
@@ -255,60 +255,46 @@
     return false;
   }
 
-  try {
-    int32_t lsaRefreshTime = section.get<int32_t>("lsa-refresh-time");
-    if (lsaRefreshTime >= LSA_REFRESH_TIME_MIN &&
-        lsaRefreshTime <= LSA_REFRESH_TIME_MAX) {
-      m_nlsr.getConfParameter().setLsaRefreshTime(lsaRefreshTime);
-    }
-    else {
-      std::cerr << "Wrong value for lsa-refresh-time ";
-      std::cerr << "Allowed value: " << LSA_REFRESH_TIME_MIN << "-";;
-      std::cerr << LSA_REFRESH_TIME_MAX << std::endl;
-      return false;
-    }
+  // lsa-refresh-time
+  int32_t lsaRefreshTime = section.get<int32_t>("lsa-refresh-time", LSA_REFRESH_TIME_DEFAULT);
+
+  if (lsaRefreshTime >= LSA_REFRESH_TIME_MIN && lsaRefreshTime <= LSA_REFRESH_TIME_MAX) {
+    m_nlsr.getConfParameter().setLsaRefreshTime(lsaRefreshTime);
   }
-  catch (const std::exception& ex) {
-    std::cerr << ex.what() << std::endl;
+  else {
+    std::cerr << "Wrong value for lsa-refresh-time ";
+    std::cerr << "Allowed value: " << LSA_REFRESH_TIME_MIN << "-";;
+    std::cerr << LSA_REFRESH_TIME_MAX << std::endl;
+
+    return false;
+  }
+
+  // router-dead-interval
+  int32_t routerDeadInterval = section.get<int32_t>("router-dead-interval", (2*lsaRefreshTime));
+
+  if (routerDeadInterval > m_nlsr.getConfParameter().getLsaRefreshTime()) {
+    m_nlsr.getConfParameter().setRouterDeadInterval(routerDeadInterval);
+  }
+  else {
+    std::cerr << "Value of router-dead-interval must be larger than lsa-refresh-time" << std::endl;
+    return false;
+  }
+
+  // lsa-interest-lifetime
+  int lifetime = section.get<int>("lsa-interest-lifetime", LSA_INTEREST_LIFETIME_DEFAULT);
+
+  if (lifetime >= LSA_INTEREST_LIFETIME_MIN && lifetime <= LSA_INTEREST_LIFETIME_MAX) {
+    m_nlsr.getConfParameter().setLsaInterestLifetime(ndn::time::seconds(lifetime));
+  }
+  else {
+    std::cerr << "Wrong value for lsa-interest-timeout. "
+              << "Allowed value:" << LSA_INTEREST_LIFETIME_MIN << "-"
+              << LSA_INTEREST_LIFETIME_MAX << std::endl;
+
     return false;
   }
 
   try {
-    int32_t routerDeadInterval = section.get<int32_t>("router-dead-interval");
-
-    if (routerDeadInterval > m_nlsr.getConfParameter().getLsaRefreshTime()) {
-      m_nlsr.getConfParameter().setRouterDeadInterval(routerDeadInterval);
-    }
-    else {
-      std::cerr << "Value of router-dead-interval must be larger than lsa-refresh-time"
-                << std::endl;
-      return false;
-    }
-  }
-  catch (const std::exception& ex) {
-    // Variable is optional so default value (2 * lsa-refresh-time) will be used;
-    // Continue processing file
-  }
-
-  try {
-    int lifetime = section.get<int>("lsa-interest-lifetime");
-    if (lifetime >= LSA_INTEREST_LIFETIME_MIN && lifetime <= LSA_INTEREST_LIFETIME_MAX) {
-      m_nlsr.getConfParameter().setLsaInterestLifetime(ndn::time::seconds(lifetime));
-    }
-    else {
-      std::cerr << "Wrong value for lsa-interest-timeout. "
-                << "Allowed value:" << LSA_INTEREST_LIFETIME_MIN << "-"
-                << LSA_INTEREST_LIFETIME_MAX << std::endl;
-      return false;
-    }
-  }
-  catch (const std::exception& ex) {
-    std::cerr << ex.what() << std::endl;
-    // non-critical error. default value is 4
-  }
-
-  try {
-
     std::string logLevel = section.get<string>("log-level");
 
     if (isValidLogLevel(logLevel)) {
@@ -421,51 +407,46 @@
 bool
 ConfFileProcessor::processConfSectionNeighbors(const ConfigSection& section)
 {
-  try {
-    int retrials = section.get<int>("hello-retries");
-    if (retrials >= HELLO_RETRIES_MIN && retrials <= HELLO_RETRIES_MAX) {
-      m_nlsr.getConfParameter().setInterestRetryNumber(retrials);
-    }
-    else {
-      std::cerr << "Wrong value for hello-retries. ";
-      std::cerr << "Allowed value:" << HELLO_RETRIES_MIN << "-";
-      std::cerr << HELLO_RETRIES_MAX << std::endl;
-      return false;
-    }
+  // hello-retries
+  int retrials = section.get<int>("hello-retries", HELLO_RETRIES_DEFAULT);
+
+  if (retrials >= HELLO_RETRIES_MIN && retrials <= HELLO_RETRIES_MAX) {
+    m_nlsr.getConfParameter().setInterestRetryNumber(retrials);
   }
-  catch (const std::exception& ex) {
-    std::cerr << ex.what() << std::endl;
+  else {
+    std::cerr << "Wrong value for hello-retries." << std::endl;
+    std::cerr << "Allowed value:" << HELLO_RETRIES_MIN << "-";
+    std::cerr << HELLO_RETRIES_MAX << std::endl;
+
     return false;
   }
-  try {
-    int timeOut = section.get<int>("hello-timeout");
-    if (timeOut >= HELLO_TIMEOUT_MIN && timeOut <= HELLO_TIMEOUT_MAX) {
-      m_nlsr.getConfParameter().setInterestResendTime(timeOut);
-    }
-    else {
-      std::cerr << "Wrong value for hello-timeout. ";
-      std::cerr << "Allowed value:" << HELLO_TIMEOUT_MIN << "-";
-      std::cerr << HELLO_TIMEOUT_MAX << std::endl;
-      return false;
-    }
+
+  // hello-timeout
+  int timeOut = section.get<int>("hello-timeout", HELLO_TIMEOUT_DEFAULT);
+
+  if (timeOut >= HELLO_TIMEOUT_MIN && timeOut <= HELLO_TIMEOUT_MAX) {
+    m_nlsr.getConfParameter().setInterestResendTime(timeOut);
   }
-  catch (const std::exception& ex) {
-    std::cerr << ex.what() << std::endl;
+  else {
+    std::cerr << "Wrong value for hello-timeout. ";
+    std::cerr << "Allowed value:" << HELLO_TIMEOUT_MIN << "-";
+    std::cerr << HELLO_TIMEOUT_MAX << std::endl;
+
+    return false;
   }
-  try {
-    int interval = section.get<int>("hello-interval");
-    if (interval >= HELLO_INTERVAL_MIN && interval <= HELLO_INTERVAL_MAX) {
-      m_nlsr.getConfParameter().setInfoInterestInterval(interval);
-    }
-    else {
-      std::cerr << "Wrong value for hello-interval. ";
-      std::cerr << "Allowed value:" << HELLO_INTERVAL_MIN << "-";
-      std::cerr << HELLO_INTERVAL_MAX << std::endl;
-      return false;
-    }
+
+  // hello-interval
+  int interval = section.get<int>("hello-interval", HELLO_INTERVAL_DEFAULT);
+
+  if (interval >= HELLO_INTERVAL_MIN && interval <= HELLO_INTERVAL_MAX) {
+    m_nlsr.getConfParameter().setInfoInterestInterval(interval);
   }
-  catch (const std::exception& ex) {
-    std::cerr << ex.what() << std::endl;
+  else {
+    std::cerr << "Wrong value for hello-interval. ";
+    std::cerr << "Allowed value:" << HELLO_INTERVAL_MIN << "-";
+    std::cerr << HELLO_INTERVAL_MAX << std::endl;
+
+    return false;
   }
 
   // Event intervals
@@ -532,26 +513,22 @@
 bool
 ConfFileProcessor::processConfSectionHyperbolic(const ConfigSection& section)
 {
-  std::string state;
-  try {
-    state= section.get<string>("state","off");
-    if (boost::iequals(state, "off")) {
-      m_nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_OFF);
-    }
-    else if (boost::iequals(state, "on")) {
-        m_nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_ON);
-    }
-    else if (state == "dry-run") {
-      m_nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_DRY_RUN);
-    }
-    else {
-      std::cerr << "Wrong format for hyperbolic state." << std::endl;
-      std::cerr << "Allowed value: off, on, dry-run" << std::endl;
-      return false;
-    }
+  // state
+  std::string state = section.get<string>("state", "off");
+
+  if (boost::iequals(state, "off")) {
+    m_nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_OFF);
   }
-  catch (const std::exception& ex) {
-    std::cerr << ex.what() << std::endl;
+  else if (boost::iequals(state, "on")) {
+    m_nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_ON);
+  }
+  else if (state == "dry-run") {
+    m_nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_DRY_RUN);
+  }
+  else {
+    std::cerr << "Wrong format for hyperbolic state." << std::endl;
+    std::cerr << "Allowed value: off, on, dry-run" << std::endl;
+
     return false;
   }
 
@@ -581,22 +558,18 @@
 bool
 ConfFileProcessor::processConfSectionFib(const ConfigSection& section)
 {
-  try {
-    int maxFacesPerPrefixNumber =
-      section.get<int>("max-faces-per-prefix");
-    if (maxFacesPerPrefixNumber >= MAX_FACES_PER_PREFIX_MIN &&
-        maxFacesPerPrefixNumber <= MAX_FACES_PER_PREFIX_MAX)
-    {
-      m_nlsr.getConfParameter().setMaxFacesPerPrefix(maxFacesPerPrefixNumber);
-    }
-    else {
-      std::cerr << "Wrong value for max-faces-per-prefix. ";
-      std::cerr << MAX_FACES_PER_PREFIX_MIN << std::endl;
-      return false;
-    }
+  // max-faces-per-prefix
+  int maxFacesPerPrefix = section.get<int>("max-faces-per-prefix", MAX_FACES_PER_PREFIX_DEFAULT);
+
+  if (maxFacesPerPrefix >= MAX_FACES_PER_PREFIX_MIN &&
+      maxFacesPerPrefix <= MAX_FACES_PER_PREFIX_MAX)
+  {
+    m_nlsr.getConfParameter().setMaxFacesPerPrefix(maxFacesPerPrefix);
   }
-  catch (const std::exception& ex) {
-    cerr << ex.what() << endl;
+  else {
+    std::cerr << "Wrong value for max-faces-per-prefix. ";
+    std::cerr << MAX_FACES_PER_PREFIX_MIN << std::endl;
+
     return false;
   }
 
diff --git a/src/conf-parameter.hpp b/src/conf-parameter.hpp
index e25c4d3..4edf33e 100644
--- a/src/conf-parameter.hpp
+++ b/src/conf-parameter.hpp
@@ -83,13 +83,15 @@
 
 enum {
   MAX_FACES_PER_PREFIX_MIN = 0,
+  MAX_FACES_PER_PREFIX_DEFAULT = 0,
   MAX_FACES_PER_PREFIX_MAX = 60
 };
 
 enum {
   HYPERBOLIC_STATE_OFF = 0,
   HYPERBOLIC_STATE_ON = 1,
-  HYPERBOLIC_STATE_DRY_RUN = 2
+  HYPERBOLIC_STATE_DRY_RUN = 2,
+  HYPERBOLIC_STATE_DEFAULT = 0
 };
 
 class ConfParameter
@@ -189,7 +191,6 @@
   setLsaRefreshTime(int32_t lrt)
   {
     m_lsaRefreshTime = lrt;
-    m_routerDeadInterval = 2 * m_lsaRefreshTime;
   }
 
   int32_t
@@ -247,12 +248,12 @@
   }
 
   void
-  setRouterDeadInterval(int64_t rdt)
+  setRouterDeadInterval(int32_t rdt)
   {
     m_routerDeadInterval = rdt;
   }
 
-  int64_t
+  int32_t
   getRouterDeadInterval() const
   {
     return m_routerDeadInterval;
@@ -422,13 +423,12 @@
   uint32_t m_routingCalcInterval;
 
   ndn::time::seconds m_lsaInterestLifetime;
-  int64_t  m_routerDeadInterval;
+  int32_t  m_routerDeadInterval;
   std::string m_logLevel;
 
   uint32_t m_interestRetryNumber;
   int32_t  m_interestResendTime;
 
-
   int32_t  m_infoInterestInterval;
 
   int32_t m_hyperbolicState;
diff --git a/tests/test-conf-file-processor.cpp b/tests/test-conf-file-processor.cpp
index ab24215..a86a965 100644
--- a/tests/test-conf-file-processor.cpp
+++ b/tests/test-conf-file-processor.cpp
@@ -195,6 +195,12 @@
     return m_logFileName;
   }
 
+  void
+  commentOut(const std::string& key, std::string& config)
+  {
+    boost::replace_all(config, key, ";" + key);
+  }
+
 public:
   shared_ptr<ndn::DummyFace> face;
   Nlsr nlsr;
@@ -364,38 +370,75 @@
   BOOST_CHECK_EQUAL(conf.getCorTheta(), 1.45);
 }
 
-BOOST_AUTO_TEST_CASE(DefaultValues)
+BOOST_AUTO_TEST_CASE(DefaultValuesGeneral)
 {
-  // Missing adj-lsa-build-interval
-  const std::string SECTION_NEIGHBORS_DEFAULT_VALUES =
-  "neighbors\n"
-  "{\n"
-  "  hello-retries 3\n"
-  "  hello-timeout 1\n"
-  "  hello-interval  60\n\n"
-  "  first-hello-interval  6\n"
-  "  neighbor\n"
-  "  {\n"
-  "    name /ndn/memphis.edu/cs/castor\n"
-  "    face-uri  udp4://localhost\n"
-  "    link-cost 20\n"
-  "  }\n\n"
-  "  neighbor\n"
-  "  {\n"
-  "    name /ndn/memphis.edu/cs/mira\n"
-  "    face-uri  udp4://localhost\n"
-  "    link-cost 30\n"
-  "  }\n"
-  "}\n\n";
+  std::string config = SECTION_GENERAL;
 
-  processConfigurationString(SECTION_NEIGHBORS_DEFAULT_VALUES);
+  commentOut("lsa-refresh-time", config);
+  commentOut("lsa-interest-lifetime", config);
+  commentOut("router-dead-interval", config);
+
+  BOOST_CHECK_EQUAL(processConfigurationString(config), true);
 
   ConfParameter& conf = nlsr.getConfParameter();
 
+  BOOST_CHECK_EQUAL(conf.getLsaRefreshTime(), static_cast<int32_t>(LSA_REFRESH_TIME_DEFAULT));
+  BOOST_CHECK_EQUAL(conf.getLsaInterestLifetime(),
+                    static_cast<ndn::time::seconds>(LSA_INTEREST_LIFETIME_DEFAULT));
+  BOOST_CHECK_EQUAL(conf.getRouterDeadInterval(), (2*conf.getLsaRefreshTime()));
+}
+
+BOOST_AUTO_TEST_CASE(DefaultValuesNeighbors)
+{
+  std::string config = SECTION_NEIGHBORS;
+
+  commentOut("hello-retries", config);
+  commentOut("hello-timeout", config);
+  commentOut("hello-interval", config);
+  commentOut("first-hello-interval", config);
+  commentOut("adj-lsa-build-interval", config);
+
+  BOOST_CHECK_EQUAL(processConfigurationString(config), true);
+
+  ConfParameter& conf = nlsr.getConfParameter();
+
+  BOOST_CHECK_EQUAL(conf.getInterestRetryNumber(), static_cast<uint32_t>(HELLO_RETRIES_DEFAULT));
+  BOOST_CHECK_EQUAL(conf.getInterestResendTime(), static_cast<int32_t>(HELLO_TIMEOUT_DEFAULT));
+  BOOST_CHECK_EQUAL(conf.getInfoInterestInterval(), static_cast<int32_t>(HELLO_INTERVAL_DEFAULT));
+  BOOST_CHECK_EQUAL(conf.getFirstHelloInterval(),
+                    static_cast<uint32_t>(FIRST_HELLO_INTERVAL_DEFAULT));
   BOOST_CHECK_EQUAL(conf.getAdjLsaBuildInterval(),
                     static_cast<uint32_t>(ADJ_LSA_BUILD_INTERVAL_DEFAULT));
+}
 
-  BOOST_CHECK_EQUAL(conf.getFirstHelloInterval(), 6);
+BOOST_AUTO_TEST_CASE(DefaultValuesFib)
+{
+  std::string config = SECTION_FIB;
+
+  commentOut("max-faces-per-prefix", config);
+  commentOut("routing-calc-interval", config);
+
+  BOOST_CHECK_EQUAL(processConfigurationString(config), true);
+
+  ConfParameter& conf = nlsr.getConfParameter();
+
+  BOOST_CHECK_EQUAL(conf.getMaxFacesPerPrefix(),
+                    static_cast<uint32_t>(MAX_FACES_PER_PREFIX_DEFAULT));
+  BOOST_CHECK_EQUAL(conf.getRoutingCalcInterval(),
+                    static_cast<uint32_t>(ROUTING_CALC_INTERVAL_DEFAULT));
+}
+
+BOOST_AUTO_TEST_CASE(DefaultValuesHyperbolic)
+{
+  std::string config = SECTION_HYPERBOLIC_ON;
+
+  commentOut("state", config);
+
+  BOOST_CHECK_EQUAL(processConfigurationString(config), true);
+
+  ConfParameter& conf = nlsr.getConfParameter();
+
+  BOOST_CHECK_EQUAL(conf.getHyperbolicState(), static_cast<int32_t>(HYPERBOLIC_STATE_DEFAULT));
 }
 
 BOOST_AUTO_TEST_CASE(OutOfRangeValue)