update: Disable PrefixUpdateProcessor when validator is not in config

refs: 2814

Change-Id: I2669dcd4af36ebe09f3e20189a99c812e0e6ad7b
diff --git a/src/conf-file-processor.cpp b/src/conf-file-processor.cpp
index 2bb7aef..c4cb20b 100644
--- a/src/conf-file-processor.cpp
+++ b/src/conf-file-processor.cpp
@@ -612,46 +612,40 @@
 {
   ConfigSection::const_iterator it = section.begin();
 
-  if (it == section.end() || it->first != "validator")
-    {
-      std::cerr << "Error: Expect validator section!" << std::endl;
-      return false;
-    }
+  if (it == section.end() || it->first != "validator") {
+    std::cerr << "Error: Expect validator section!" << std::endl;
+    return false;
+  }
 
   m_nlsr.loadValidator(it->second, m_confFileName);
+
   it++;
+  if (it != section.end() && it->first == "prefix-update-validator") {
+    m_nlsr.getPrefixUpdateProcessor().enable();
+    m_nlsr.getPrefixUpdateProcessor().loadValidator(it->second, m_confFileName);
 
-  if (it == section.end() || it->first != "prefix-update-validator")
-    {
-      std::cerr << "Error: Expect prefix-update-validator section" << std::endl;
-      return false;
-    }
-
-  m_nlsr.getPrefixUpdateProcessor().loadValidator(it->second, m_confFileName);
-  it++;
-
-  for (; it != section.end(); it++)
-    {
+    it++;
+    for (; it != section.end(); it++) {
       using namespace boost::filesystem;
-      if (it->first != "cert-to-publish")
-        {
-          std::cerr << "Error: Expect cert-to-publish!" << std::endl;
-          return false;
-        }
+
+      if (it->first != "cert-to-publish") {
+        std::cerr << "Error: Expect cert-to-publish!" << std::endl;
+        return false;
+      }
 
       std::string file = it->second.data();
       path certfilePath = absolute(file, path(m_confFileName).parent_path());
-      ndn::shared_ptr<ndn::IdentityCertificate> idCert =
+      shared_ptr<ndn::IdentityCertificate> idCert =
         ndn::io::load<ndn::IdentityCertificate>(certfilePath.string());
 
-      if (!static_cast<bool>(idCert))
-        {
-          std::cerr << "Error: Cannot load cert-to-publish: " << file << "!" << std::endl;
-          return false;
-        }
+      if (idCert == nullptr) {
+        std::cerr << "Error: Cannot load cert-to-publish: " << file << "!" << std::endl;
+        return false;
+      }
 
       m_nlsr.loadCertToPublish(idCert);
     }
+  }
 
   return true;
 }
diff --git a/src/update/prefix-update-processor.cpp b/src/update/prefix-update-processor.cpp
index 12e066a..7b3fe89 100644
--- a/src/update/prefix-update-processor.cpp
+++ b/src/update/prefix-update-processor.cpp
@@ -51,6 +51,7 @@
   , m_sync(sync)
   , m_keyChain(keyChain)
   , m_validator(m_face, broadcastPrefix, certificateCache, certStore)
+  , m_isEnabled(false)
   , COMMAND_PREFIX(ndn::Name(Nlsr::LOCALHOST_PREFIX).append(MODULE_COMPONENT))
 {
 }
@@ -68,6 +69,11 @@
 {
   _LOG_TRACE("Received Interest: " << request);
 
+  if (!m_isEnabled) {
+    sendNack(request);
+    return;
+  }
+
   m_validator.validate(request,
                        bind(&PrefixUpdateProcessor::onCommandValidated, this, _1),
                        bind(&PrefixUpdateProcessor::onCommandValidationFailed, this, _1, _2));
@@ -186,6 +192,19 @@
 }
 
 void
+PrefixUpdateProcessor::sendNack(const ndn::Interest& request)
+{
+  ndn::MetaInfo metaInfo;
+  metaInfo.setType(ndn::tlv::ContentType_Nack);
+
+  shared_ptr<ndn::Data> responseData = std::make_shared<ndn::Data>(request.getName());
+  responseData->setMetaInfo(metaInfo);
+
+  m_keyChain.sign(*responseData);
+  m_face.put(*responseData);
+}
+
+void
 PrefixUpdateProcessor::sendResponse(const std::shared_ptr<const ndn::Interest>& request,
                                     uint32_t code,
                                     const std::string& text)
@@ -197,7 +216,7 @@
   ndn::nfd::ControlResponse response(code, text);
   const ndn::Block& encodedControl = response.wireEncode();
 
-  std::shared_ptr<ndn::Data> responseData = ndn::make_shared<ndn::Data>(request->getName());
+  std::shared_ptr<ndn::Data> responseData = std::make_shared<ndn::Data>(request->getName());
   responseData->setContent(encodedControl);
 
   m_keyChain.sign(*responseData);
diff --git a/src/update/prefix-update-processor.hpp b/src/update/prefix-update-processor.hpp
index 6dc6c09..395d161 100644
--- a/src/update/prefix-update-processor.hpp
+++ b/src/update/prefix-update-processor.hpp
@@ -78,11 +78,20 @@
   void
   startListening();
 
+  void
+  enable()
+  {
+    m_isEnabled = true;
+  }
+
 private:
   void
   onInterest(const ndn::Interest& request);
 
   void
+  sendNack(const ndn::Interest& request);
+
+  void
   sendResponse(const std::shared_ptr<const ndn::Interest>& request,
                uint32_t code,
                const std::string& text);
@@ -128,6 +137,7 @@
   SyncLogicHandler& m_sync;
   ndn::KeyChain& m_keyChain;
   Validator m_validator;
+  bool m_isEnabled;
 
   const ndn::Name COMMAND_PREFIX; // /localhost/nlsr/prefix-update