mgmt: ndns-add-rr-from-file update RR to the latest version

Change-Id: Ia6812e5961bdc885e92cd49fafe2298390667040
Refs: #2259
diff --git a/src/mgmt/management-tool.cpp b/src/mgmt/management-tool.cpp
index 9925aca..c6fc51e 100644
--- a/src/mgmt/management-tool.cpp
+++ b/src/mgmt/management-tool.cpp
@@ -288,11 +288,8 @@
   rrset.setVersion(re.getVersion());
   rrset.setData(data->wireEncode());
 
-  if (m_dbMgr.find(rrset)) {
-    throw Error("Duplicate " + boost::lexical_cast<std::string>(rrset));
-  }
+  checkRrsetVersion(rrset);
   NDNS_LOG_INFO("Added " << rrset);
-
   m_dbMgr.insert(rrset);
 }
 
@@ -388,9 +385,7 @@
   rrset.setVersion(re.getVersion());
   rrset.setData(data->wireEncode());
 
-  if (m_dbMgr.find(rrset)) {
-    throw Error("Duplicate " + boost::lexical_cast<std::string>(rrset));
-  }
+  checkRrsetVersion(rrset);
   NDNS_LOG_INFO("Added " << rrset);
   m_dbMgr.insert(rrset);
 }
@@ -636,5 +631,22 @@
   return true;
 }
 
+void
+ManagementTool::checkRrsetVersion(const Rrset& rrset)
+{
+  Rrset originalRrset(rrset);
+  if (m_dbMgr.find(originalRrset)) {
+    // update only if rrset has a newer version
+    if (originalRrset.getVersion() == rrset.getVersion()) {
+      throw Error("Duplicate: " + boost::lexical_cast<std::string>(originalRrset));
+    }
+    else if (originalRrset.getVersion() > rrset.getVersion()) {
+      throw Error("Newer version exists: " + boost::lexical_cast<std::string>(originalRrset));
+    }
+
+    m_dbMgr.remove(originalRrset);
+  }
+}
+
 } // namespace ndns
 } // namespace ndn