OpenPOWER: Ignore already reported resource dump

There are cases when host is reporting already reported
resource dump entries. This commit add fix for duplicate
dump entries.

Test:
Create multiple resource dumps in host
Only one will be reported on BMC
Delete it
While deleting, next entry may get reported multiple times
With this fix that duplicate is eliminated
Feb 01 03:51:00 z2437cbmc phosphor-dump-manager[6244]: Resouce Dump Notify: Id(1174405122) Size(3952)
Feb 01 03:51:00 z2437cbmc phosphor-dump-manager[6244]: Resouce Dump Notify: Updating dumpId(5) Id(1174405122) Size(3952)
Feb 01 03:51:00 z2437cbmc phosphor-dump-manager[6244]: Resouce Dump Notify: Id(1174405122) Size(3952)
Feb 01 03:51:00 z2437cbmc phosphor-dump-manager[6244]: Resource dump entry with source dump id(1174405122) is already present with entry id(5)

Signed-off-by: Dhruvaraj Subhashchandran <dhruvaraj@in.ibm.com>
Change-Id: I86afb6d6916f80ee2412a3e7313aa52b87d30aa7
diff --git a/dump-extensions/openpower-dumps/dump_manager_resource.cpp b/dump-extensions/openpower-dumps/dump_manager_resource.cpp
index 27bf010..521b0b8 100644
--- a/dump-extensions/openpower-dumps/dump_manager_resource.cpp
+++ b/dump-extensions/openpower-dumps/dump_manager_resource.cpp
@@ -28,24 +28,50 @@
     // Get the timestamp
     std::time_t timeStamp = std::time(nullptr);
 
-    // If there is an entry with this sourceId or an invalid id
-    // update that.
-    // If host is sending the source id before the completion
-    // the source id will be updated by the transport layer with host.
-    // if not the source id will stay as invalid one.
+    // If there is an entry with invalid id update that.
+    // If there a completed one with same source id ignore it
+    // if there is no invalid id, create new entry
+    openpower::dump::resource::Entry* upEntry = NULL;
     for (auto& entry : entries)
     {
         openpower::dump::resource::Entry* resEntry =
             dynamic_cast<openpower::dump::resource::Entry*>(entry.second.get());
-        if ((resEntry->status() ==
-             phosphor::dump::OperationStatus::InProgress) &&
-            ((resEntry->sourceDumpId() == dumpId) ||
-             (resEntry->sourceDumpId() == INVALID_SOURCE_ID)))
+
+        // If there is already a completed entry with input source id then
+        // ignore this notification.
+        if ((resEntry->sourceDumpId() == dumpId) &&
+            (resEntry->status() == phosphor::dump::OperationStatus::Completed))
         {
-            resEntry->update(timeStamp, size, dumpId);
+            log<level::INFO>(
+                fmt::format("Resource dump entry with source dump id({}) is "
+                            "already present with entry id({})",
+                            dumpId, resEntry->getDumpId())
+                    .c_str());
             return;
         }
+
+        // Save the first entry with INVALID_SOURCE_ID
+        // but continue in the loop to make sure the
+        // new entry is not duplicate
+        if ((resEntry->status() ==
+             phosphor::dump::OperationStatus::InProgress) &&
+            (resEntry->sourceDumpId() == INVALID_SOURCE_ID) &&
+            (upEntry == NULL))
+        {
+            upEntry = resEntry;
+        }
     }
+    if (upEntry != NULL)
+    {
+        log<level::INFO>(
+            fmt::format("Resource Dump Notify: Updating dumpId({}) "
+                        "with source Id({}) Size({})",
+                        upEntry->getDumpId(), dumpId, size)
+                .c_str());
+        upEntry->update(timeStamp, size, dumpId);
+        return;
+    }
+
     // Get the id
     auto id = lastEntryId + 1;
     auto idString = std::to_string(id);
@@ -53,6 +79,10 @@
 
     try
     {
+        log<level::INFO>(fmt::format("Resouce Dump Notify: creating new dump "
+                                     "entry dumpId({}) Id({}) Size({})",
+                                     id, dumpId, size)
+                             .c_str());
         entries.insert(std::make_pair(
             id, std::make_unique<resource::Entry>(
                     bus, objPath.c_str(), id, timeStamp, size, dumpId,