PLDM: Restrain from adding duplicate TL PDRs

This commit inhibits adding the duplicate terminus locator PDRs
to the repo. When the PDRs are removed based on the
terminus handle, the terminus locator PDR info is also
deleted from the map.

Tested with the normal poweron.

Change-Id: Ib973a41dc42324f9b5453a5f1a6592dc306729ff
Signed-off-by: Pavithra Barithaya <pavithra.b@ibm.com>
diff --git a/host-bmc/host_pdr_handler.cpp b/host-bmc/host_pdr_handler.cpp
index e74affc..0cc8a1a 100644
--- a/host-bmc/host_pdr_handler.cpp
+++ b/host-bmc/host_pdr_handler.cpp
@@ -16,7 +16,6 @@
 
 namespace pldm
 {
-
 using namespace pldm::dbus_api;
 using namespace pldm::responder::events;
 using namespace pldm::utils;
@@ -479,6 +478,17 @@
                     {
                         tlValid = false;
                     }
+                    for (const auto& terminusMap : tlPDRInfo)
+                    {
+                        if ((terminusHandle == (terminusMap.first)) &&
+                            (get<1>(terminusMap.second) == tlEid) &&
+                            (get<2>(terminusMap.second) == tlpdr->validity))
+                        {
+                            // TL PDR already present with same validity don't
+                            // add the PDR to the repo just return
+                            return;
+                        }
+                    }
                     tlPDRInfo.insert_or_assign(
                         tlpdr->terminus_handle,
                         std::make_tuple(tlpdr->tid, tlEid, tlpdr->validity));
diff --git a/libpldmresponder/platform.cpp b/libpldmresponder/platform.cpp
index fe825d7..4728bb6 100644
--- a/libpldmresponder/platform.cpp
+++ b/libpldmresponder/platform.cpp
@@ -27,7 +27,6 @@
 {
 namespace platform
 {
-
 using InternalFailure =
     sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
 
@@ -552,13 +551,18 @@
             // We cannot get the Repo change event from the Terminus
             // that is not already added to the BMC repository
 
-            for (const auto& [terminusHandle, terminusInfo] :
-                 hostPDRHandler->tlPDRInfo)
+            for (auto it = hostPDRHandler->tlPDRInfo.cbegin();
+                 it != hostPDRHandler->tlPDRInfo.cend();)
             {
-                if (std::get<0>(terminusInfo) == tid)
+                if (std::get<0>(it->second) == tid)
                 {
                     pldm_pdr_remove_pdrs_by_terminus_handle(pdrRepo.getPdr(),
-                                                            terminusHandle);
+                                                            it->first);
+                    hostPDRHandler->tlPDRInfo.erase(it++);
+                }
+                else
+                {
+                    ++it;
                 }
             }
         }