entity_association_tree: Keep tree always updated

This commit keeps the entity association tree updated across Host
reboots. It deletes the Host entries from the entity associaton
tree when Host is powered off.

Without this fix the entity association tree keeps on growing with
duplicate Host entries during each power on(PDR exchange) resulting
a crash of pldm daemon after few power on-off sequence

Change-Id: I8fdc258195aef998d10e33577f3ec98da48b3d40
Signed-off-by: Sampa Misra <sampmisr@in.ibm.com>
diff --git a/host-bmc/host_pdr_handler.cpp b/host-bmc/host_pdr_handler.cpp
index e6bf7d7..b4ef645 100644
--- a/host-bmc/host_pdr_handler.cpp
+++ b/host-bmc/host_pdr_handler.cpp
@@ -25,11 +25,12 @@
                                sdeventplus::Event& event, pldm_pdr* repo,
                                const std::string& eventsJsonsDir,
                                pldm_entity_association_tree* entityTree,
+                               pldm_entity_association_tree* bmcEntityTree,
                                Requester& requester, bool verbose) :
     mctp_fd(mctp_fd),
     mctp_eid(mctp_eid), event(event), repo(repo),
     stateSensorHandler(eventsJsonsDir), entityTree(entityTree),
-    requester(requester), verbose(verbose)
+    bmcEntityTree(bmcEntityTree), requester(requester), verbose(verbose)
 {
     fs::path hostFruJson(fs::path(HOST_JSONS_DIR) / fruJson);
     if (fs::exists(hostFruJson))
@@ -69,7 +70,8 @@
         pldm::utils::DBusHandler::getBus(),
         propertiesChanged("/xyz/openbmc_project/state/host0",
                           "xyz.openbmc_project.State.Host"),
-        [this, repo](sdbusplus::message::message& msg) {
+        [this, repo, entityTree,
+         bmcEntityTree](sdbusplus::message::message& msg) {
             DbusChangedProps props{};
             std::string intf;
             msg.read(intf, props);
@@ -81,6 +83,9 @@
                 if (propVal == "xyz.openbmc_project.State.Host.HostState.Off")
                 {
                     pldm_pdr_remove_remote_pdrs(repo);
+                    pldm_entity_association_tree_destroy_root(entityTree);
+                    pldm_entity_association_tree_copy_root(bmcEntityTree,
+                                                           entityTree);
                     this->sensorMap.clear();
                 }
             }