Once the ONU is marked as down, all the TP states are cleared in
'update_interface' method. Post this if it receives any Gem Port
or Tcont Delete message from the openolt adapter, they are not
honored. This information is not cleared from internal cache
in such scenarios. This leads issues to subsequent TP Add failures
(once the ONU comes back up), because the data on the ONU and
in internal cache are out-of-sync.
If Tcont/Gem Port delete arrives to openonu adapter from
openolt adapter, clear it from internal cache no matter what.
Openolt adapter is the authority of all the PON resources
and makes it is simpler for openonu adapter to just honor
the request related to these resources rather than using
complex intelligence.

Change-Id: I9326f9fa8438e5bb95746ee3c8bfb98c14033184
diff --git a/python/adapters/brcm_openomci_onu/omci/brcm_tp_setup_task.py b/python/adapters/brcm_openomci_onu/omci/brcm_tp_setup_task.py
index fbbe86a..13be4f5 100644
--- a/python/adapters/brcm_openomci_onu/omci/brcm_tp_setup_task.py
+++ b/python/adapters/brcm_openomci_onu/omci/brcm_tp_setup_task.py
@@ -210,8 +210,16 @@
                         if not isinstance(v, dict):
                             continue
                         alloc_check = v.get('attributes', {}).get('alloc_id', 0)
-                        # Some onu report both to indicate an available tcont
-                        if alloc_check == 0xFF or alloc_check == 0xFFFF:
+                        if alloc_check == tcont.alloc_id:
+                            # If any Tcont entity already refers to the alloc-id we want to use,
+                            # lets choose that Tcont entity.
+                            # This Tcont will be re-added to ONU and that is fine. The ONU reports that
+                            # the entity already exists, which is not an error.
+                            free_entity_id = k
+                            self.log.debug("tcont-entity-already-exists-on-onu-for-this-alloc-id",
+                                           tcont_entity_id=k, alloc_id=tcont.alloc_id)
+                            break
+                        elif alloc_check == 0xFF or alloc_check == 0xFFFF:
                             free_entity_id = k
                             break