VOL-2172: Deleting tech-profile at ONU
- Handle DELETE_GEM_PORT_REQUEST and DELETE_TCONT_REQ inter-adapter-message.
- Introduce a new OMCI task BrcmTpDeleteTask to delete GemPorts and TCONT
- When tech-profile download is already done, check if it is case
  where new gem-ports are being added dynamically. If new gem-ports
  are detected in TpInstance, add them.
- Rename BrcmTpServiceSpecificTask to BrcmTpSetupTask
- Move setting up of IEEE8021pMapper and ANI MacBridgePortConfigData
  to BrcmTpSetupTask (when TCONT is being setup). There should be
  unique IEEE8021pMapper and ANI MacBridgePortConfigData per TCONT.
- When TCONT is removed, remove IEEE8021pMapper and ANI MacBridgePortConfigData.
- Ensure IEEE8021pMapper and ANI MacBridgePortConfigData have element of
  TP-Table ID in its entity ID.
- GemPort Entity ID set to be same as GemPort ID and not dynamically generated.
- Fix code indentations
- Bump voltha proto version to 2.1.1
- Bump pyvoltha version to 2.3.2

Change-Id: Ie4a90d6cd6916e27a6e32b3727b161252cb80ca3
diff --git a/python/adapters/brcm_openomci_onu/brcm_openomci_onu_handler.py b/python/adapters/brcm_openomci_onu/brcm_openomci_onu_handler.py
index 6b1cec0..ca0a770 100644
--- a/python/adapters/brcm_openomci_onu/brcm_openomci_onu_handler.py
+++ b/python/adapters/brcm_openomci_onu/brcm_openomci_onu_handler.py
@@ -45,13 +45,15 @@
 from voltha_protos.common_pb2 import OperStatus, ConnectStatus, AdminState
 from voltha_protos.openflow_13_pb2 import OFPXMC_OPENFLOW_BASIC, ofp_port, OFPPS_LIVE, OFPPF_FIBER, OFPPF_1GB_FD
 from voltha_protos.inter_container_pb2 import InterAdapterMessageType, \
-    InterAdapterOmciMessage, PortCapability, InterAdapterTechProfileDownloadMessage
+    InterAdapterOmciMessage, PortCapability, InterAdapterTechProfileDownloadMessage, InterAdapterDeleteGemPortMessage, \
+    InterAdapterDeleteTcontMessage
 from voltha_protos.openolt_pb2 import OnuIndication
 from pyvoltha.adapters.extensions.omci.onu_configuration import OMCCVersion
 from pyvoltha.adapters.extensions.omci.onu_device_entry import OnuDeviceEvents, \
     OnuDeviceEntry, IN_SYNC_KEY
 from omci.brcm_mib_download_task import BrcmMibDownloadTask
-from omci.brcm_tp_service_specific_task import BrcmTpServiceSpecificTask
+from omci.brcm_tp_setup_task import BrcmTpSetupTask
+from omci.brcm_tp_delete_task import BrcmTpDeleteTask
 from omci.brcm_uni_lock_task import BrcmUniLockTask
 from omci.brcm_vlan_filter_task import BrcmVlanFilterTask
 from onu_gem_port import OnuGemPort
@@ -167,7 +169,7 @@
 
         assert isinstance(port_no_or_name, int), 'Invalid parameter type'
         return next((uni for uni in self.uni_ports
-                    if uni.port_number == port_no_or_name), None)
+                     if uni.port_number == port_no_or_name), None)
 
     @property
     def pon_port(self):
@@ -181,12 +183,12 @@
         self.log.info('get_ofp_port_info', port_no=port_no, device_id=device.id)
         cap = OFPPF_1GB_FD | OFPPF_FIBER
 
-        hw_addr=mac_str_to_tuple('08:%02x:%02x:%02x:%02x:%02x' %
-                                ((device.parent_port_no >> 8 & 0xff),
-                                  device.parent_port_no & 0xff,
-                                  (port_no >> 16) & 0xff,
-                                  (port_no >> 8) & 0xff,
-                                   port_no & 0xff))
+        hw_addr = mac_str_to_tuple('08:%02x:%02x:%02x:%02x:%02x' %
+                                   ((device.parent_port_no >> 8 & 0xff),
+                                    device.parent_port_no & 0xff,
+                                    (port_no >> 16) & 0xff,
+                                    (port_no >> 8) & 0xff,
+                                    port_no & 0xff))
 
         uni_port = self.uni_port(int(port_no))
         name = device.serial_number + '-' + str(uni_port.mac_bridge_port_num)
@@ -257,14 +259,14 @@
                                            grouped=True, freq_override=False, **kwargs)
             pm_config = self.pm_metrics.make_proto()
             self._onu_omci_device.set_pm_config(self.pm_metrics.omci_pm.openomci_interval_pm)
-            self.log.info("initial-pm-config",device_id=device.id, serial_number=device.serial_number)
+            self.log.info("initial-pm-config", device_id=device.id, serial_number=device.serial_number)
             yield self.core_proxy.device_pm_config_update(pm_config, init=True)
 
             # Note, ONU ID and UNI intf set in add_uni_port method
             self._onu_omci_device.alarm_synchronizer.set_alarm_params(mgr=self.events,
                                                                       ani_ports=[self._pon])
 
-            #Start collecting stats from the device after a brief pause
+            # Start collecting stats from the device after a brief pause
             reactor.callLater(10, self.pm_metrics.start_collector)
 
             # Code to Run OMCI Test Action
@@ -384,7 +386,7 @@
     def _create_gemports(self, uni_id, gem_ports, alloc_id_ref, direction):
         self.log.debug('create-gemport',
                        gem_ports=gem_ports, direction=direction)
-
+        new_gem_ports = []
         for gem_port in gem_ports:
             gemdict = dict()
             gemdict['gemport_id'] = gem_port['gemport_id']
@@ -407,11 +409,14 @@
             gemdict['uni_id'] = uni_id
 
             gem_port = OnuGemPort.create(self, gem_port=gemdict)
+            new_gem_ports.append(gem_port)
 
             self._pon.add_gem_port(gem_port)
 
             self.log.debug('pon-add-gemport', gem_port=gem_port)
 
+        return new_gem_ports
+
     def _execute_queued_vlan_filter_tasks(self, uni_id):
         # During OLT Reboots, ONU Reboots, ONU Disable/Enable, it is seen that vlan_filter
         # task is scheduled even before tp task. So we queue vlan-filter task if tp_task
@@ -431,7 +436,6 @@
         except Exception as e:
             self.log.error("vlan-filter-configuration-failed", uni_id=uni_id, error=e)
 
-
     def _do_tech_profile_configuration(self, uni_id, tp):
         us_scheduler = tp['us_scheduler']
         alloc_id = us_scheduler['alloc_id']
@@ -457,12 +461,13 @@
             try:
                 if tp_path in self._tp_service_specific_task[uni_id]:
                     self.log.info("tech-profile-config-already-in-progress",
-                                   tp_path=tp_path)
+                                  tp_path=tp_path)
                     return
 
                 tpstored = self.kv_client[tp_path]
                 tpstring = tpstored.decode('ascii')
                 tp = json.loads(tpstring)
+
                 self.log.debug("tp-instance", tp=tp)
                 self._do_tech_profile_configuration(uni_id, tp)
 
@@ -475,27 +480,166 @@
                     self._tech_profile_download_done[uni_id][tp_path] = True
                     # Now execute any vlan filter tasks that were queued for later
                     self._execute_queued_vlan_filter_tasks(uni_id)
+
                 @inlineCallbacks
                 def failure(_reason):
                     self.log.warn('tech-profile-config-failure-retrying',
-                                   _reason=_reason)
-                    yield self.core_proxy.device_reason_update(self.device_id, 'tech-profile-config-download-failure-retrying')
+                                  _reason=_reason)
+                    yield self.core_proxy.device_reason_update(self.device_id,
+                                                               'tech-profile-config-download-failure-retrying')
                     if tp_path in self._tp_service_specific_task[uni_id]:
                         del self._tp_service_specific_task[uni_id][tp_path]
                     self._deferred = reactor.callLater(_STARTUP_RETRY_WAIT, self.load_and_configure_tech_profile,
                                                        uni_id, tp_path)
 
                 self.log.info('downloading-tech-profile-configuration')
+                # Extract the current set of TCONT and GEM Ports from the Handler's pon_port that are
+                # relevant to this task's UNI. It won't change. But, the underlying pon_port may change
+                # due to additional tasks on different UNIs. So, it we cannot use the pon_port after
+                # this initializer
+                tconts = []
+                for tcont in list(self.pon_port.tconts.values()):
+                    if tcont.uni_id is not None and tcont.uni_id != uni_id:
+                        continue
+                    tconts.append(tcont)
+
+                gem_ports = []
+                for gem_port in list(self.pon_port.gem_ports.values()):
+                    if gem_port.uni_id is not None and gem_port.uni_id != uni_id:
+                        continue
+                    gem_ports.append(gem_port)
+
+                self.log.debug("tconts-gems-to-install", tconts=tconts, gem_ports=gem_ports)
+
                 self._tp_service_specific_task[uni_id][tp_path] = \
-                       BrcmTpServiceSpecificTask(self.omci_agent, self, uni_id)
+                    BrcmTpSetupTask(self.omci_agent, self, uni_id, tconts, gem_ports, int(tp_path.split("/")[1]))
                 self._deferred = \
-                       self._onu_omci_device.task_runner.queue_task(self._tp_service_specific_task[uni_id][tp_path])
+                    self._onu_omci_device.task_runner.queue_task(self._tp_service_specific_task[uni_id][tp_path])
                 self._deferred.addCallbacks(success, failure)
 
             except Exception as e:
                 self.log.exception("error-loading-tech-profile", e=e)
         else:
             self.log.info("tech-profile-config-already-done")
+            # Could be a case where TP exists but new gem-ports are getting added dynamically
+            tpstored = self.kv_client[tp_path]
+            tpstring = tpstored.decode('ascii')
+            tp = json.loads(tpstring)
+            upstream_gems = []
+            downstream_gems = []
+            # Find out the new Gem ports that are getting added afresh.
+            for gp in tp['upstream_gem_port_attribute_list']:
+                if self.pon_port.gem_port(gp['gemport_id'], "upstream"):
+                    # gem port already exists
+                    continue
+                upstream_gems.append(gp)
+            for gp in tp['downstream_gem_port_attribute_list']:
+                if self.pon_port.gem_port(gp['gemport_id'], "downstream"):
+                    # gem port already exists
+                    continue
+                downstream_gems.append(gp)
+
+            us_scheduler = tp['us_scheduler']
+            alloc_id = us_scheduler['alloc_id']
+
+            if len(upstream_gems) > 0 or len(downstream_gems) > 0:
+                self.log.info("installing-new-gem-ports", upstream_gems=upstream_gems, downstream_gems=downstream_gems)
+                new_upstream_gems = self._create_gemports(uni_id, upstream_gems, alloc_id, "UPSTREAM")
+                new_downstream_gems = self._create_gemports(uni_id, downstream_gems, alloc_id, "DOWNSTREAM")
+                new_gems = []
+                new_gems.extend(new_upstream_gems)
+                new_gems.extend(new_downstream_gems)
+
+                def success(_results):
+                    self.log.info("new-gem-ports-successfully-installed", result=_results)
+
+                def failure(_reason):
+                    self.log.warn('new-gem-port-install-failed--retrying',
+                                  _reason=_reason)
+                    # Remove gem ports from cache. We will re-add them during the retry
+                    for gp in new_gems:
+                        self.pon_port.remove_gem_id(gp.gem_id, gp.direction, False)
+
+                    self._deferred = reactor.callLater(_STARTUP_RETRY_WAIT, self.load_and_configure_tech_profile,
+                                                       uni_id, tp_path)
+
+                self._tp_service_specific_task[uni_id][tp_path] = \
+                    BrcmTpSetupTask(self.omci_agent, self, uni_id, [], new_gems, int(tp_path.split("/")[1]))
+                self._deferred = \
+                    self._onu_omci_device.task_runner.queue_task(self._tp_service_specific_task[uni_id][tp_path])
+                self._deferred.addCallbacks(success, failure)
+
+    def delete_tech_profile(self, uni_id, tp_path, alloc_id=None, gem_port_id=None):
+        try:
+            if self._tech_profile_download_done[uni_id][tp_path] is not True:
+                self.log.error("tp-download-is-not-done-in-order-to-process-tp-delete")
+                return
+
+            if alloc_id is None and gem_port_id is None:
+                self.log.error("alloc-id-and-gem-port-id-are-none")
+                return
+
+            # Extract the current set of TCONT and GEM Ports from the Handler's pon_port that are
+            # relevant to this task's UNI. It won't change. But, the underlying pon_port may change
+            # due to additional tasks on different UNIs. So, it we cannot use the pon_port affter
+            # this initializer
+            tcont = None
+            self.log.debug("tconts", tconts=list(self.pon_port.tconts.values()))
+            for tc in list(self.pon_port.tconts.values()):
+                if tc.alloc_id == alloc_id:
+                    tcont = tc
+                    self.pon_port.remove_tcont(tc.alloc_id, False)
+
+            gem_port = None
+            self.log.debug("gem-ports", gem_ports=list(self.pon_port.gem_ports.values()))
+            for gp in list(self.pon_port.gem_ports.values()):
+                if gp.gem_id == gem_port_id:
+                    gem_port = gp
+                    self.pon_port.remove_gem_id(gp.gem_id, gp.direction, False)
+
+            # tp_path is of the format  <technology>/<table_id>/<uni_port_name>
+            # We need the TP Table ID
+            tp_table_id = int(tp_path.split("/")[1])
+
+            @inlineCallbacks
+            def success(_results):
+                if gem_port_id:
+                    self.log.info("gem-port-delete-done-successfully")
+                if alloc_id:
+                    self.log.info("tcont-delete-done-successfully")
+                    # The deletion of TCONT marks the complete deletion of tech-profile
+                    try:
+                        del self._tech_profile_download_done[uni_id][tp_path]
+                        del self._tp_service_specific_task[uni_id][tp_path]
+                    except Exception as ex:
+                        self.log.error("del-tp-state-info", e=ex)
+
+                # TODO: There could be multiple TP on the UNI, and also the ONU.
+                # TODO: But the below reason updates for the whole device.
+                yield self.core_proxy.device_reason_update(self.device_id, 'tech-profile-config-delete-success')
+
+            @inlineCallbacks
+            def failure(_reason, _uni_id, _tp_table_id, _tcont, _gem_port):
+                self.log.warn('tech-profile-delete-failure-retrying',
+                              _reason=_reason)
+                yield self.core_proxy.device_reason_update(self.device_id,
+                                                           'tech-profile-config-delete-failure-retrying')
+                self._deferred = \
+                    self._onu_omci_device.task_runner.queue_task(self._tp_service_specific_task[uni_id][tp_path])
+                self._deferred.addCallbacks(success, failure)
+
+            self.log.info('deleting-tech-profile-configuration')
+
+            self._tp_service_specific_task[uni_id][tp_path] = \
+                BrcmTpDeleteTask(self.omci_agent, self, uni_id, tp_table_id,
+                                 tcont=tcont, gem_port=gem_port)
+            self._deferred = \
+                self._onu_omci_device.task_runner.queue_task(self._tp_service_specific_task[uni_id][tp_path])
+            self._deferred.addCallbacks(success, failure)
+        except Exception as e:
+            self.log.exception("failed-to-delete-tp",
+                               e=e, uni_id=uni_id, tp_path=tp_path,
+                               alloc_id=alloc_id, gem_port_id=gem_port_id)
 
     def update_pm_config(self, device, pm_config):
         # TODO: This has not been tested
@@ -515,7 +659,7 @@
 
         # no point in pushing omci flows if the device isnt reachable
         if device.connect_status != ConnectStatus.REACHABLE or \
-           device.admin_state != AdminState.ENABLED:
+                device.admin_state != AdminState.ENABLED:
             self.log.warn("device-disabled-or-offline-skipping-flow-update",
                           admin=device.admin_state, connect=device.connect_status)
             return
@@ -551,7 +695,7 @@
                 if is_downstream(_in_port):
                     self.log.debug('downstream-flow', in_port=_in_port, out_port=_out_port)
                     uni_port = self.uni_port(_out_port)
-                    uni_id = _out_port  & 0xF
+                    uni_id = _out_port & 0xF
                 elif is_upstream(_in_port):
                     self.log.debug('upstream-flow', in_port=_in_port, out_port=_out_port)
                     uni_port = self.uni_port(_in_port)
@@ -667,9 +811,7 @@
             except Exception as e:
                 self.log.exception('failed-to-install-flow', e=e, flow=flow)
 
-
-
-    def _add_vlan_filter_task(self, device,uni_id, uni_port, _set_vlan_vid):
+    def _add_vlan_filter_task(self, device, uni_id, uni_port, _set_vlan_vid):
         assert uni_port is not None
         if uni_id in self._tech_profile_download_done and self._tech_profile_download_done[uni_id] != {}:
             @inlineCallbacks
@@ -683,7 +825,8 @@
                 self.log.warn('vlan-tagging-failure', uni_port=uni_port, vlan=_set_vlan_vid)
                 yield self.core_proxy.device_reason_update(self.device_id, 'omci-flows-failed-retrying')
                 self._vlan_filter_task = reactor.callLater(_STARTUP_RETRY_WAIT,
-                                                       self._add_vlan_filter_task, device,uni_port.port_number, uni_port, _set_vlan_vid)
+                                                           self._add_vlan_filter_task, device, uni_port.port_number,
+                                                           uni_port, _set_vlan_vid)
 
             self.log.info('setting-vlan-tag')
             self._vlan_filter_task = BrcmVlanFilterTask(self.omci_agent, self, uni_port, _set_vlan_vid)
@@ -693,7 +836,7 @@
             self.log.info('tp-service-specific-task-not-done-adding-request-to-local-cache',
                           uni_id=uni_id)
             self._queued_vlan_filter_task[uni_id] = {"device": device,
-                                                     "uni_id":uni_id,
+                                                     "uni_id": uni_id,
                                                      "uni_port": uni_port,
                                                      "set_vlan_vid": _set_vlan_vid}
 
@@ -722,7 +865,7 @@
 
                 if onu_indication.oper_state == "up":
                     self.create_interface(onu_indication)
-                elif onu_indication.oper_state == "down" or onu_indication.oper_state=="unreachable":
+                elif onu_indication.oper_state == "down" or onu_indication.oper_state == "unreachable":
                     self.update_interface(onu_indication)
                 else:
                     self.log.error("unknown-onu-indication", onu_id=onu_indication.onu_id,
@@ -735,6 +878,23 @@
 
                 self.load_and_configure_tech_profile(tech_msg.uni_id, tech_msg.path)
 
+            elif request.header.type == InterAdapterMessageType.DELETE_GEM_PORT_REQUEST:
+                del_gem_msg = InterAdapterDeleteGemPortMessage()
+                request.body.Unpack(del_gem_msg)
+                self.log.debug('inter-adapter-recv-del-gem', gem_del_msg=del_gem_msg)
+
+                self.delete_tech_profile(uni_id=del_gem_msg.uni_id,
+                                         gem_port_id=del_gem_msg.gem_port_id,
+                                         tp_path=del_gem_msg.tp_path)
+
+            elif request.header.type == InterAdapterMessageType.DELETE_TCONT_REQUEST:
+                del_tcont_msg = InterAdapterDeleteTcontMessage()
+                request.body.Unpack(del_tcont_msg)
+                self.log.debug('inter-adapter-recv-del-tcont', del_tcont_msg=del_tcont_msg)
+
+                self.delete_tech_profile(uni_id=del_tcont_msg.uni_id,
+                                         alloc_id=del_tcont_msg.alloc_id,
+                                         tp_path=del_tcont_msg.tp_path)
             else:
                 self.log.error("inter-adapter-unhandled-type", request=request)
 
@@ -756,7 +916,7 @@
         self.log.debug('starting-openomci-statemachine')
         self._subscribe_to_events()
         onu_device.reason = "starting-openomci"
-        reactor.callLater(1, self._onu_omci_device.start,onu_device)
+        reactor.callLater(1, self._onu_omci_device.start, onu_device)
         yield self.core_proxy.device_reason_update(self.device_id, onu_device.reason)
         self._heartbeat.enabled = True
 
@@ -840,11 +1000,15 @@
                 self.log.debug('stopping-openomci-statemachine')
                 reactor.callLater(0, self._onu_omci_device.stop)
 
-                # Let TP download happen again
-                for uni_id in self._tp_service_specific_task:
-                    self._tp_service_specific_task[uni_id].clear()
-                for uni_id in self._tech_profile_download_done:
-                    self._tech_profile_download_done[uni_id].clear()
+                # Note: The tech-profile states should not be cleared here.
+                # They will be cleared if a DELETE_TCONT_REQ was triggered from openolt-adapter
+                # as a result of all flow references for the TCONT being removed OR as a result
+                # 'update_interface' call with oper_state as "down".
+
+                # for uni_id in self._tp_service_specific_task:
+                #    self._tp_service_specific_task[uni_id].clear()
+                # for uni_id in self._tech_profile_download_done:
+                #    self._tech_profile_download_done[uni_id].clear()
 
                 self.disable_ports()
                 device.oper_status = OperStatus.UNKNOWN
@@ -1044,7 +1208,8 @@
                     self.log.info('mib-download-success', _results=_results)
                     yield self.enable_ports()
                     yield self.core_proxy.device_state_update(device.id,
-                                            oper_status=OperStatus.ACTIVE, connect_status=ConnectStatus.REACHABLE)
+                                                              oper_status=OperStatus.ACTIVE,
+                                                              connect_status=ConnectStatus.REACHABLE)
                     yield self.core_proxy.device_reason_update(self.device_id, 'initial-mib-downloaded')
                     self._mib_download_task = None
                     yield self.onu_active_event()
@@ -1076,7 +1241,7 @@
         # TODO: Some or parts of this likely need to move to UniPort. especially the format stuff
         uni_name = "uni-{}".format(uni_no)
 
-        mac_bridge_port_num = uni_id + 1 # TODO +1 is only to test non-zero index
+        mac_bridge_port_num = uni_id + 1  # TODO +1 is only to test non-zero index
 
         self.log.debug('uni-port-inputs', uni_no=uni_no, uni_id=uni_id, uni_name=uni_name, uni_type=uni_type,
                        entity_id=entity_id, mac_bridge_port_num=mac_bridge_port_num, serial_number=device.serial_number)
@@ -1093,7 +1258,8 @@
         self._unis[uni_port.port_number] = uni_port
 
         self._onu_omci_device.alarm_synchronizer.set_alarm_params(onu_id=self._onu_indication.onu_id,
-                                                                  uni_ports=self.uni_ports, serial_number=device.serial_number)
+                                                                  uni_ports=self.uni_ports,
+                                                                  serial_number=device.serial_number)
 
     # TODO NEW CORE: Figure out how to gain this knowledge from the olt.  for now cheat terribly.
     def mk_uni_port_num(self, intf_id, onu_id, uni_id):
@@ -1116,22 +1282,21 @@
             raised_ts = arrow.utcnow().timestamp
 
             self.log.debug("onu-indication-context-data",
-                       pon_id=self._onu_indication.intf_id,
-                       onu_id=self._onu_indication.onu_id,
-                       registration_id=self.device_id,
-                       device_id=self.device_id,
-                       onu_serial_number=device.serial_number,
-                       olt_serial_number=olt_serial_number,
-                       raised_ts=raised_ts)
+                           pon_id=self._onu_indication.intf_id,
+                           onu_id=self._onu_indication.onu_id,
+                           registration_id=self.device_id,
+                           device_id=self.device_id,
+                           onu_serial_number=device.serial_number,
+                           olt_serial_number=olt_serial_number,
+                           raised_ts=raised_ts)
 
             self.log.debug("Trying-to-raise-onu-active-event")
             OnuActiveEvent(self.events, self.device_id,
                            self._onu_indication.intf_id,
                            device.serial_number,
                            str(self.device_id),
-                           olt_serial_number,raised_ts,
+                           olt_serial_number, raised_ts,
                            onu_id=self._onu_indication.onu_id).send(True)
         except Exception as active_event_error:
             self.log.exception('onu-activated-event-error',
                                errmsg=active_event_error.message)
-
diff --git a/python/adapters/brcm_openomci_onu/omci/brcm_mib_download_task.py b/python/adapters/brcm_openomci_onu/omci/brcm_mib_download_task.py
index f09379b..e8b0841 100644
--- a/python/adapters/brcm_openomci_onu/omci/brcm_mib_download_task.py
+++ b/python/adapters/brcm_openomci_onu/omci/brcm_mib_download_task.py
@@ -279,51 +279,6 @@
             self.check_status_and_state(results, 'create-mac-bridge-service-profile')
 
             ################################################################################
-            # PON Specific                                                                 #
-            ################################################################################
-            # IEEE 802.1 Mapper Service config - Once per PON
-            #
-            #  EntityID will be referenced by:
-            #            - MAC Bridge Port Configuration Data for the PON port
-            #  References:
-            #            - Nothing at this point. When a GEM port is created, this entity will
-            #              be updated to reference the GEM Interworking TP
-
-            msg = Ieee8021pMapperServiceProfileFrame(self._ieee_mapper_service_profile_entity_id + uni_port.mac_bridge_port_num)
-            frame = msg.create()
-            self.log.debug('openomci-msg', omci_msg=msg)
-            results = yield omci_cc.send(frame)
-            self.check_status_and_state(results, 'create-8021p-mapper-service-profile')
-
-            ################################################################################
-            # Create MAC Bridge Port Configuration Data for the PON port via IEEE 802.1
-            # mapper service. Upon receipt by the ONU, the ONU will create an instance
-            # of the following before returning the response.
-            #
-            #     - MAC bridge port designation data
-            #     - MAC bridge port filter table data
-            #     - MAC bridge port bridge table data
-            #
-            #  EntityID will be referenced by:
-            #            - Implicitly by the VLAN tagging filter data
-            #  References:
-            #            - MAC Bridge Service Profile (the bridge)
-            #            - IEEE 802.1p mapper service profile for PON port
-
-            # TODO: magic. make a static variable for tp_type
-            msg = MacBridgePortConfigurationDataFrame(
-                self._mac_bridge_port_ani_entity_id + uni_port.mac_bridge_port_num,
-                bridge_id_pointer=self._mac_bridge_service_profile_entity_id + uni_port.mac_bridge_port_num,  # Bridge Entity ID
-                port_num= 0xff, # Port ID - unique number within the bridge
-                tp_type=3, # TP Type (IEEE 802.1p mapper service)
-                tp_pointer=self._ieee_mapper_service_profile_entity_id + uni_port.mac_bridge_port_num  # TP ID, 8021p mapper ID
-            )
-            frame = msg.create()
-            self.log.debug('openomci-msg', omci_msg=msg)
-            results = yield omci_cc.send(frame)
-            self.check_status_and_state(results, 'create-mac-bridge-port-configuration-data-8021p-mapper')
-
-            ################################################################################
             # UNI Specific                                                                 #
             ################################################################################
             # MAC Bridge Port config
diff --git a/python/adapters/brcm_openomci_onu/omci/brcm_tp_delete_task.py b/python/adapters/brcm_openomci_onu/omci/brcm_tp_delete_task.py
new file mode 100644
index 0000000..a5619c7
--- /dev/null
+++ b/python/adapters/brcm_openomci_onu/omci/brcm_tp_delete_task.py
@@ -0,0 +1,285 @@
+#
+# Copyright 2018 the original author or authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from __future__ import absolute_import
+import structlog
+from twisted.internet import reactor
+from twisted.internet.defer import inlineCallbacks, TimeoutError, failure
+from pyvoltha.adapters.extensions.omci.omci_me import Ont2G, OmciNullPointer, PriorityQueueFrame, \
+    Ieee8021pMapperServiceProfileFrame, MacBridgePortConfigurationDataFrame
+from pyvoltha.adapters.extensions.omci.tasks.task import Task
+from pyvoltha.adapters.extensions.omci.omci_defs import EntityOperations, ReasonCodes
+from pyvoltha.adapters.extensions.omci.omci_entities import OntG, Tcont, PriorityQueueG
+
+OP = EntityOperations
+RC = ReasonCodes
+
+# The DELETE_TP_TASK_PRIORITY should be higher than SETUP_TP_TASK_PRIORITY
+DELETE_TP_TASK_PRIORITY = 250
+
+
+class TechProfileDeleteFailure(Exception):
+    """
+    This error is raised by default when the delete fails
+    """
+
+
+class TechProfileResourcesFailure(Exception):
+    """
+    This error is raised by when one or more resources required is not available
+    """
+
+
+class BrcmTpDeleteTask(Task):
+    """
+    OpenOMCI Tech-Profile Delete Task
+
+    """
+
+    name = "Broadcom Tech-Profile Delete Task"
+
+    def __init__(self, omci_agent, handler, uni_id, tp_table_id,
+                 tcont=None, gem_port=None):
+        """
+        Class initialization
+
+        :param omci_agent: (OmciAdapterAgent) OMCI Adapter agent
+        :param handler: (BrcmOpenomciOnuHandler) ONU Device Handler Instance
+        :param uni_id: (int) numeric id of the uni port on the onu device, starts at 0
+        :param tp_table_id: (int) Technology Profile Table-ID
+        """
+        log = structlog.get_logger(device_id=handler.device_id, uni_id=uni_id)
+        log.debug('function-entry')
+
+        super(BrcmTpDeleteTask, self).__init__(BrcmTpDeleteTask.name,
+                                               omci_agent,
+                                               handler.device_id,
+                                               priority=DELETE_TP_TASK_PRIORITY,
+                                               exclusive=True)
+
+        self.log = log
+
+        self._onu_device = omci_agent.get_device(handler.device_id)
+        self._local_deferred = None
+
+        self._uni_port = handler.uni_ports[uni_id]
+        assert self._uni_port.uni_id == uni_id
+
+        # Tech Profile Table ID
+        self._tp_table_id = tp_table_id
+
+        self._tcont = tcont
+        self._gem_port = gem_port
+
+        self._alloc_id = self._tcont.alloc_id if self._tcont else None
+        self._gem_port_id = self._gem_port.gem_id if gem_port else None
+
+        # Entity IDs. IDs with values can probably be most anything for most ONUs,
+        #             IDs set to None are discovered/set
+        self._mac_bridge_service_profile_entity_id = \
+            handler.mac_bridge_service_profile_entity_id
+        self._ieee_mapper_service_profile_entity_id = \
+            handler.pon_port.ieee_mapper_service_profile_entity_id
+        self._mac_bridge_port_ani_entity_id = \
+            handler.pon_port.mac_bridge_port_ani_entity_id
+
+    def cancel_deferred(self):
+        self.log.debug('function-entry')
+        super(BrcmTpDeleteTask, self).cancel_deferred()
+
+        d, self._local_deferred = self._local_deferred, None
+        try:
+            if d is not None and not d.called:
+                d.cancel()
+        except:
+            pass
+
+    def start(self):
+        """
+        Start the Tech-Profile Delete
+        """
+        self.log.debug('function-entry')
+        super(BrcmTpDeleteTask, self).start()
+        if self._tcont is not None:
+            self._local_deferred = reactor.callLater(0, self.delete_tcont_and_associated_me)
+        elif self._gem_port is not None:
+            self._local_deferred = reactor.callLater(0, self.delete_gem_port_nw_ctp_and_associated_me)
+        else:
+            raise Exception("both-alloc-id-and-gem-port-id-are-none")
+
+    def stop(self):
+        """
+        Shutdown Tech-Profile delete tasks
+        """
+        self.log.debug('function-entry')
+        self.log.debug('stopping')
+
+        self.cancel_deferred()
+        super(BrcmTpDeleteTask, self).stop()
+
+    def check_status_and_state(self, results, operation=''):
+        """
+        Check the results of an OMCI response.  An exception is thrown
+        if the task was cancelled or an error was detected.
+
+        :param results: (OmciFrame) OMCI Response frame
+        :param operation: (str) what operation was being performed
+        :return: True if successful, False if the entity existed (already created)
+        """
+        self.log.debug('function-entry')
+
+        omci_msg = results.fields['omci_message'].fields
+        status = omci_msg['success_code']
+        error_mask = omci_msg.get('parameter_error_attributes_mask', 'n/a')
+        failed_mask = omci_msg.get('failed_attributes_mask', 'n/a')
+        unsupported_mask = omci_msg.get('unsupported_attributes_mask', 'n/a')
+
+        self.log.debug("OMCI Result", operation=operation, omci_msg=omci_msg, status=status, error_mask=error_mask,
+                       failed_mask=failed_mask, unsupported_mask=unsupported_mask)
+
+        if status == RC.Success:
+            self.strobe_watchdog()
+            return True
+
+        elif status == RC.InstanceExists:
+            return False
+
+        raise TechProfileDeleteFailure(
+            '{} failed with a status of {}, error_mask: {}, failed_mask: {}, '
+            'unsupported_mask: {}'.format(operation, status, error_mask, failed_mask, unsupported_mask))
+
+    @inlineCallbacks
+    def delete_tcont_and_associated_me(self):
+        self.log.debug('function-entry')
+
+        omci_cc = self._onu_device.omci_cc
+
+        try:
+            ################################################################################
+            # Reset TCONT ME
+            ################################################################################
+
+            tcont_idents = self._onu_device.query_mib(Tcont.class_id)
+            self.log.debug('tcont-idents', tcont_idents=tcont_idents)
+            tcont_entity_id = None
+            for k, v in tcont_idents.items():
+                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 == self._alloc_id:
+                    tcont_entity_id = k
+                    break
+
+            if tcont_entity_id is None:
+                self.log.error("tcont-not-found-for-delete", alloc_id=self._alloc_id)
+                return
+
+            self.log.debug('found-tcont', tcont_entity_id=tcont_entity_id, alloc_id=self._alloc_id)
+
+            # Remove the TCONT (rather reset the TCONT to point alloc_id to 0xffff)
+            # The _tcont.remove_from_hardware is already doing check_status_and_state
+            yield self._tcont.remove_from_hardware(omci_cc)
+
+            # At this point, the gem-ports should have been removed already.
+            # Remove the 8021p Mapper and ANI MacBridgePortConfigData
+            yield self._delete__8021p_mapper__ani_mac_bridge_port()
+
+            # There are no other associated MEs now.
+            # There might be TrafficScheduler MEs that point to a TCONT.
+            # TrafficScheduler ME not used currently.
+
+            self.deferred.callback("tech-profile-remove-success--tcont")
+
+        except TimeoutError as e:
+            self.log.warn('rx-timeout-tech-profile', e=e)
+            self.deferred.errback(failure.Failure(e))
+
+        except Exception as e:
+            self.log.exception('omci-delete-tech-profile', e=e)
+            self.deferred.errback(failure.Failure(e))
+
+    @inlineCallbacks
+    def delete_gem_port_nw_ctp_and_associated_me(self):
+        omci_cc = self._onu_device.omci_cc
+        try:
+            ################################################################################
+            # Delete GemPortNetworkCTP and GemPortInterworkingPoint
+            ################################################################################
+            # The _gem_port.remove_from_hardware is already doing check_status_and_state
+            yield self._gem_port.remove_from_hardware(omci_cc)
+
+            self.deferred.callback("tech-profile-remove-success--gem-port")
+
+        except TimeoutError as e:
+            self.log.warn('rx-timeout-tech-profile', e=e)
+            self.deferred.errback(failure.Failure(e))
+
+        except Exception as e:
+            self.log.exception('omci-delete-tech-profile', e=e)
+            self.deferred.errback(failure.Failure(e))
+
+    @inlineCallbacks
+    def _delete__8021p_mapper__ani_mac_bridge_port(self):
+
+        omci_cc = self._onu_device.omci_cc
+
+        try:
+            ################################################################################
+            # Delete 8021p mapper
+            ################################################################################
+            msg = Ieee8021pMapperServiceProfileFrame(
+                self._ieee_mapper_service_profile_entity_id +
+                self._uni_port.mac_bridge_port_num +
+                self._tp_table_id
+            )
+            frame = msg.delete()
+            self.log.debug('openomci-msg', omci_msg=msg)
+            results = yield omci_cc.send(frame)
+            self.check_status_and_state(results, 'delete-8021p-mapper-service-profile')
+
+            ################################################################################
+            # Delete MacBridgePortConfigData
+            ################################################################################
+            # TODO: magic. make a static variable for tp_type
+            msg = MacBridgePortConfigurationDataFrame(
+                self._mac_bridge_port_ani_entity_id + self._uni_port.mac_bridge_port_num + self._tp_table_id,
+                bridge_id_pointer=(
+                        self._mac_bridge_service_profile_entity_id +
+                        self._uni_port.mac_bridge_port_num +
+                        self._tp_table_id
+                ),
+                # Bridge Entity ID
+                port_num=0xff,  # Port ID - unique number within the bridge
+                tp_type=3,  # TP Type (IEEE 802.1p mapper service)
+                tp_pointer=(
+                        self._ieee_mapper_service_profile_entity_id +
+                        self._uni_port.mac_bridge_port_num +
+                        self._tp_table_id
+                )
+                # TP ID, 8021p mapper ID
+            )
+            frame = msg.delete()
+            self.log.debug('openomci-msg', omci_msg=msg)
+            results = yield omci_cc.send(frame)
+            self.check_status_and_state(results, 'delete-mac-bridge-port-configuration-data-8021p-mapper')
+
+        except TimeoutError as e:
+            self.log.warn('rx-timeout-8021p-ani-port-delete', e=e)
+            raise
+
+        except Exception as e:
+            self.log.exception('omci-delete-8021p-ani-port-delete', e=e)
+            raise
diff --git a/python/adapters/brcm_openomci_onu/omci/brcm_tp_service_specific_task.py b/python/adapters/brcm_openomci_onu/omci/brcm_tp_setup_task.py
similarity index 67%
rename from python/adapters/brcm_openomci_onu/omci/brcm_tp_service_specific_task.py
rename to python/adapters/brcm_openomci_onu/omci/brcm_tp_setup_task.py
index 62a1e51..6a1bacb 100644
--- a/python/adapters/brcm_openomci_onu/omci/brcm_tp_service_specific_task.py
+++ b/python/adapters/brcm_openomci_onu/omci/brcm_tp_setup_task.py
@@ -18,16 +18,16 @@
 from twisted.internet import reactor
 from twisted.internet.defer import inlineCallbacks, TimeoutError, failure
 from pyvoltha.adapters.extensions.omci.omci_me import Ont2G, OmciNullPointer, PriorityQueueFrame, \
-    Ieee8021pMapperServiceProfileFrame
+    Ieee8021pMapperServiceProfileFrame, MacBridgePortConfigurationDataFrame
 from pyvoltha.adapters.extensions.omci.tasks.task import Task
 from pyvoltha.adapters.extensions.omci.omci_defs import EntityOperations, ReasonCodes
-from pyvoltha.adapters.extensions.omci.omci_entities import OntG, Tcont, PriorityQueueG
-from pon_port import TASK_PRIORITY
-
+from pyvoltha.adapters.extensions.omci.omci_entities import OntG, Tcont, PriorityQueueG, Ieee8021pMapperServiceProfile
 
 OP = EntityOperations
 RC = ReasonCodes
 
+SETUP_TP_TASK_PRIORITY = 240
+
 
 class TechProfileDownloadFailure(Exception):
     """
@@ -41,7 +41,7 @@
     """
 
 
-class BrcmTpServiceSpecificTask(Task):
+class BrcmTpSetupTask(Task):
     """
     OpenOMCI Tech-Profile Download Task
 
@@ -49,22 +49,23 @@
 
     name = "Broadcom Tech-Profile Download Task"
 
-    def __init__(self, omci_agent, handler, uni_id):
+    def __init__(self, omci_agent, handler, uni_id, tconts, gem_ports, tp_table_id):
         """
         Class initialization
 
         :param omci_agent: (OmciAdapterAgent) OMCI Adapter agent
         :param handler: (BrcmOpenomciOnuHandler) ONU Device Handler Instance
         :param uni_id: (int) numeric id of the uni port on the onu device, starts at 0
+        :param tp_table_id: (int) Technology Profile Table-ID
         """
         log = structlog.get_logger(device_id=handler.device_id, uni_id=uni_id)
         log.debug('function-entry')
 
-        super(BrcmTpServiceSpecificTask, self).__init__(BrcmTpServiceSpecificTask.name,
-                                                        omci_agent,
-                                                        handler.device_id,
-                                                        priority=TASK_PRIORITY,
-                                                        exclusive=True)
+        super(BrcmTpSetupTask, self).__init__(BrcmTpSetupTask.name,
+                                              omci_agent,
+                                              handler.device_id,
+                                              priority=SETUP_TP_TASK_PRIORITY,
+                                              exclusive=True)
 
         self.log = log
 
@@ -74,6 +75,9 @@
         self._uni_port = handler.uni_ports[uni_id]
         assert self._uni_port.uni_id == uni_id
 
+        # Tech Profile Table ID
+        self._tp_table_id = tp_table_id
+
         # Entity IDs. IDs with values can probably be most anything for most ONUs,
         #             IDs set to None are discovered/set
 
@@ -86,26 +90,20 @@
         self._gal_enet_profile_entity_id = \
             handler.gal_enet_profile_entity_id
 
-        # Extract the current set of TCONT and GEM Ports from the Handler's pon_port that are
-        # relevant to this task's UNI. It won't change. But, the underlying pon_port may change
-        # due to additional tasks on different UNIs. So, it we cannot use the pon_port affter
-        # this initializer
         self._tconts = []
-        for tcont in list(handler.pon_port.tconts.values()):
-            if tcont.uni_id is not None and tcont.uni_id != self._uni_port.uni_id: continue
-            self._tconts.append(tcont)
+        for t in tconts:
+            self._tconts.append(t)
 
         self._gem_ports = []
-        for gem_port in list(handler.pon_port.gem_ports.values()):
-            if gem_port.uni_id is not None and gem_port.uni_id != self._uni_port.uni_id: continue
-            self._gem_ports.append(gem_port)
+        for g in gem_ports:
+            self._gem_ports.append(g)
 
         self.tcont_me_to_queue_map = dict()
         self.uni_port_to_queue_map = dict()
 
     def cancel_deferred(self):
         self.log.debug('function-entry')
-        super(BrcmTpServiceSpecificTask, self).cancel_deferred()
+        super(BrcmTpSetupTask, self).cancel_deferred()
 
         d, self._local_deferred = self._local_deferred, None
         try:
@@ -119,7 +117,7 @@
         Start the Tech-Profile Download
         """
         self.log.debug('function-entry')
-        super(BrcmTpServiceSpecificTask, self).start()
+        super(BrcmTpSetupTask, self).start()
         self._local_deferred = reactor.callLater(0, self.perform_service_specific_steps)
 
     def stop(self):
@@ -130,7 +128,7 @@
         self.log.debug('stopping')
 
         self.cancel_deferred()
-        super(BrcmTpServiceSpecificTask, self).stop()
+        super(BrcmTpSetupTask, self).stop()
 
     def check_status_and_state(self, results, operation=''):
         """
@@ -160,8 +158,8 @@
             return False
 
         raise TechProfileDownloadFailure(
-            '{} failed with a status of {}, error_mask: {}, failed_mask: {}, unsupported_mask: {}'
-            .format(operation, status, error_mask, failed_mask, unsupported_mask))
+            '{} failed with a status of {}, error_mask: {}, failed_mask: {}, '
+            'unsupported_mask: {}'.format(operation, status, error_mask, failed_mask, unsupported_mask))
 
     @inlineCallbacks
     def perform_service_specific_steps(self):
@@ -181,6 +179,22 @@
             #  References:
             #            - ONU created TCONT (created on ONU startup)
 
+            # Setup 8021p mapper and ani mac bridge port, if it does not exist
+            # Querying just 8021p mapper ME should be enough since we create and
+            # delete 8021pMapper and ANI Mac Bridge Port together.
+            ieee_8021p_mapper_exists = False
+            ieee_8021p_mapper = self._onu_device.query_mib(Ieee8021pMapperServiceProfile.class_id)
+            for k, v in ieee_8021p_mapper.items():
+                if not isinstance(v, dict):
+                    continue
+                if k == (self._ieee_mapper_service_profile_entity_id +
+                         self._uni_port.mac_bridge_port_num + self._tp_table_id):
+                    ieee_8021p_mapper_exists = True
+
+            if ieee_8021p_mapper_exists is False:
+                self.log.info("setting-up-8021pmapper-ani-mac-bridge-port")
+                yield self._setup__8021p_mapper__ani_mac_bridge_port()
+
             tcont_idents = self._onu_device.query_mib(Tcont.class_id)
             self.log.debug('tcont-idents', tcont_idents=tcont_idents)
 
@@ -266,23 +280,22 @@
                         tcont_me = (related_port & 0xffff0000) >> 16
                         if tcont_me not in self.tcont_me_to_queue_map:
                             self.log.debug("prior-q-related-port-and-tcont-me",
-                                            related_port=related_port,
-                                            tcont_me=tcont_me)
+                                           related_port=related_port,
+                                           tcont_me=tcont_me)
                             self.tcont_me_to_queue_map[tcont_me] = list()
 
                         self.tcont_me_to_queue_map[tcont_me].append(k)
                     else:
                         uni_port = (related_port & 0xffff0000) >> 16
-                        if uni_port ==  self._uni_port.entity_id:
+                        if uni_port == self._uni_port.entity_id:
                             if uni_port not in self.uni_port_to_queue_map:
                                 self.log.debug("prior-q-related-port-and-uni-port-me",
-                                                related_port=related_port,
-                                                uni_port_me=uni_port)
+                                               related_port=related_port,
+                                               uni_port_me=uni_port)
                                 self.uni_port_to_queue_map[uni_port] = list()
 
                             self.uni_port_to_queue_map[uni_port].append(k)
 
-
             self.log.debug("ul-prior-q", ul_prior_q=self.tcont_me_to_queue_map)
             self.log.debug("dl-prior-q", dl_prior_q=self.uni_port_to_queue_map)
 
@@ -293,8 +306,6 @@
                     self.log.error('unknown-tcont-reference', gem_id=gem_port.gem_id)
                     continue
 
-                ul_prior_q_entity_id = None
-                dl_prior_q_entity_id = None
                 if gem_port.direction == "upstream" or \
                         gem_port.direction == "bi-directional":
 
@@ -324,11 +335,12 @@
 
                     # TODO: Need to restore on failure.  Need to check status/results
                     results = yield gem_port.add_to_hardware(omci_cc,
-                                             tcont.entity_id,
-                                             self._ieee_mapper_service_profile_entity_id +
-                                                      self._uni_port.mac_bridge_port_num,
-                                             self._gal_enet_profile_entity_id,
-                                             ul_prior_q_entity_id, dl_prior_q_entity_id)
+                                                             tcont.entity_id,
+                                                             self._ieee_mapper_service_profile_entity_id +
+                                                             self._uni_port.mac_bridge_port_num +
+                                                             self._tp_table_id,
+                                                             self._gal_enet_profile_entity_id,
+                                                             ul_prior_q_entity_id, dl_prior_q_entity_id)
                     self.check_status_and_state(results, 'assign-gem-port')
                 elif gem_port.direction == "downstream":
                     # Downstream is inverse of upstream
@@ -344,10 +356,10 @@
             #
 
             ont2g = self._onu_device.query_mib(Ont2G.class_id)
-            qos_config_flexibility_ie = ont2g.get(0, {}).get('attributes', {}).\
-                                        get('qos_configuration_flexibility', None)
+            qos_config_flexibility_ie = ont2g.get(0, {}).get('attributes', {}). \
+                get('qos_configuration_flexibility', None)
             self.log.debug("qos_config_flexibility",
-                            qos_config_flexibility=qos_config_flexibility_ie)
+                           qos_config_flexibility=qos_config_flexibility_ie)
 
             if qos_config_flexibility_ie & 0b100000:
                 is_related_ports_configurable = True
@@ -364,13 +376,12 @@
                     self.log.debug("updating-pq-priority")
                     related_port = pq_to_related_port[v["pq_entity_id"]]
                     related_port = related_port & 0xffff0000
-                    related_port = related_port | v['priority_q'] # Set priority
+                    related_port = related_port | v['priority_q']  # Set priority
                     msg = PriorityQueueFrame(v["pq_entity_id"], related_port=related_port)
                     frame = msg.set()
                     results = yield omci_cc.send(frame)
                     self.check_status_and_state(results, 'set-priority-queues-priority')
 
-
             ################################################################################
             # Update the IEEE 802.1p Mapper Service Profile config
             #
@@ -381,6 +392,23 @@
             #
 
             gem_entity_ids = [OmciNullPointer] * 8
+            # If IEEE8021pMapper ME existed already, then we need to re-build the
+            # inter-working-tp-pointers for different gem-entity-ids.
+            if ieee_8021p_mapper_exists:
+                self.log.debug("rebuilding-interworking-tp-pointers")
+                for k, v in ieee_8021p_mapper.items():
+                    if not isinstance(v, dict):
+                        continue
+                    # Check the entity-id of the instance matches what we expect
+                    # for this Uni/TechProfileId
+                    if k == (self._ieee_mapper_service_profile_entity_id +
+                             self._uni_port.mac_bridge_port_num + self._tp_table_id):
+                        for i in range(len(gem_entity_ids)):
+                            gem_entity_ids[i] = v.get('attributes', {}). \
+                                get('interwork_tp_pointer_for_p_bit_priority_' + str(i), OmciNullPointer)
+                        self.log.debug("interworking-tp-pointers-rebuilt-after-query-from-onu",
+                                       i_w_tp_ptr=gem_entity_ids)
+
             for gem_port in self._gem_ports:
                 self.log.debug("tp-gem-port", entity_id=gem_port.entity_id, uni_id=gem_port.uni_id)
 
@@ -395,7 +423,9 @@
                     pass
 
             msg = Ieee8021pMapperServiceProfileFrame(
-                self._ieee_mapper_service_profile_entity_id + self._uni_port.mac_bridge_port_num,  # 802.1p mapper Service Mapper Profile ID
+                (self._ieee_mapper_service_profile_entity_id +
+                 self._uni_port.mac_bridge_port_num + self._tp_table_id),
+                # 802.1p mapper Service Mapper Profile ID
                 interwork_tp_pointers=gem_entity_ids  # Interworking TP IDs
             )
             frame = msg.set()
@@ -412,3 +442,74 @@
         except Exception as e:
             self.log.exception('omci-setup-tech-profile', e=e)
             self.deferred.errback(failure.Failure(e))
+
+    @inlineCallbacks
+    def _setup__8021p_mapper__ani_mac_bridge_port(self):
+
+        omci_cc = self._onu_device.omci_cc
+
+        try:
+            ################################################################################
+            # PON Specific                                                                 #
+            ################################################################################
+            # IEEE 802.1 Mapper Service config - One per tech-profile per UNI
+            #
+            #  EntityID will be referenced by:
+            #            - MAC Bridge Port Configuration Data for the PON port and TP ID
+            #  References:
+            #            - Nothing at this point. When a GEM port is created, this entity will
+            #              be updated to reference the GEM Interworking TP
+
+            msg = Ieee8021pMapperServiceProfileFrame(
+                self._ieee_mapper_service_profile_entity_id +
+                self._uni_port.mac_bridge_port_num +
+                self._tp_table_id
+            )
+            frame = msg.create()
+            self.log.debug('openomci-msg', omci_msg=msg)
+            results = yield omci_cc.send(frame)
+            self.check_status_and_state(results, 'create-8021p-mapper-service-profile')
+
+            ################################################################################
+            # Create MAC Bridge Port Configuration Data for the PON port via IEEE 802.1
+            # mapper service and this per TechProfile. Upon receipt by the ONU, the ONU will create an instance
+            # of the following before returning the response.
+            #
+            #     - MAC bridge port designation data
+            #     - MAC bridge port filter table data
+            #     - MAC bridge port bridge table data
+            #
+            #  EntityID will be referenced by:
+            #            - Implicitly by the VLAN tagging filter data
+            #  References:
+            #            - MAC Bridge Service Profile (the bridge)
+            #            - IEEE 802.1p mapper service profile for PON port
+
+            # TODO: magic. make a static variable for tp_type
+            msg = MacBridgePortConfigurationDataFrame(
+                self._mac_bridge_port_ani_entity_id + self._uni_port.mac_bridge_port_num + self._tp_table_id,
+                bridge_id_pointer=(
+                        self._mac_bridge_service_profile_entity_id +
+                        self._uni_port.mac_bridge_port_num),
+                # Bridge Entity ID
+                port_num=0xff,  # Port ID - unique number within the bridge
+                tp_type=3,  # TP Type (IEEE 802.1p mapper service)
+                tp_pointer=(
+                        self._ieee_mapper_service_profile_entity_id +
+                        self._uni_port.mac_bridge_port_num +
+                        self._tp_table_id
+                )
+                # TP ID, 8021p mapper ID
+            )
+            frame = msg.create()
+            self.log.debug('openomci-msg', omci_msg=msg)
+            results = yield omci_cc.send(frame)
+            self.check_status_and_state(results, 'create-mac-bridge-port-configuration-data-8021p-mapper')
+
+        except TimeoutError as e:
+            self.log.warn('rx-timeout-8021p-ani-port-setup', e=e)
+            raise
+
+        except Exception as e:
+            self.log.exception('omci-setup-8021p-ani-port-setup', e=e)
+            raise
diff --git a/python/adapters/brcm_openomci_onu/omci/brcm_uni_lock_task.py b/python/adapters/brcm_openomci_onu/omci/brcm_uni_lock_task.py
index aecefc1..8e9a2e1 100644
--- a/python/adapters/brcm_openomci_onu/omci/brcm_uni_lock_task.py
+++ b/python/adapters/brcm_openomci_onu/omci/brcm_uni_lock_task.py
@@ -47,10 +47,10 @@
         :param priority: (int) OpenOMCI Task priority (0..255) 255 is the highest
         """
         super(BrcmUniLockTask, self).__init__(BrcmUniLockTask.name,
-                                                omci_agent,
-                                                device_id,
-                                                priority=priority,
-                                                exclusive=True)
+                                              omci_agent,
+                                              device_id,
+                                              priority=priority,
+                                              exclusive=True)
         self._device = omci_agent.get_device(device_id)
         self._lock = lock
         self._results = None
@@ -74,7 +74,6 @@
         super(BrcmUniLockTask, self).start()
         self._local_deferred = reactor.callLater(0, self.perform_lock)
 
-
     @inlineCallbacks
     def perform_lock(self):
         """
@@ -98,7 +97,7 @@
             for entity_id in veip_list:
                 veip_value = self._config.veip_entities[entity_id]
                 msg = VeipUniFrame(entity_id,
-                                           attributes=dict(administrative_state=state))
+                                   attributes=dict(administrative_state=state))
                 yield self._send_uni_lock_msg(entity_id, veip_value, msg)
 
             msg = OntGFrame(attributes={'administrative_state': state})
@@ -122,7 +121,6 @@
             self.log.exception('setting-uni-lock-state', e=e)
             self.deferred.errback(failure.Failure(e))
 
-
     @inlineCallbacks
     def _send_uni_lock_msg(self, entity_id, value, me_message):
         frame = me_message.set()
diff --git a/python/adapters/brcm_openomci_onu/onu_gem_port.py b/python/adapters/brcm_openomci_onu/onu_gem_port.py
index baaa27d..5e618cc 100644
--- a/python/adapters/brcm_openomci_onu/onu_gem_port.py
+++ b/python/adapters/brcm_openomci_onu/onu_gem_port.py
@@ -90,7 +90,8 @@
         self.tx_bytes = 0
 
     def __str__(self):
-        return "OnuGemPort - entity_id {}, alloc-id: {}, gem-id: {}, ".format(self.entity_id, self.alloc_id, self.gem_id)
+        return "OnuGemPort - entity_id {}, alloc-id: {}, gem-id: {}, ".format(self.entity_id, self.alloc_id,
+                                                                              self.gem_id)
 
     def __repr__(self):
         return str(self)
diff --git a/python/adapters/brcm_openomci_onu/pon_port.py b/python/adapters/brcm_openomci_onu/pon_port.py
index 5ed28b2..e8eb38d 100644
--- a/python/adapters/brcm_openomci_onu/pon_port.py
+++ b/python/adapters/brcm_openomci_onu/pon_port.py
@@ -48,8 +48,8 @@
         self._admin_state = AdminState.ENABLED
         self._oper_status = OperStatus.ACTIVE
 
-        self._gem_ports = {}                           # gem-id -> GemPort
-        self._tconts = {}                              # alloc-id -> TCont
+        self._gem_ports = {}  # gem-id -> GemPort
+        self._tconts = {}  # alloc-id -> TCont
 
         self.ieee_mapper_service_profile_entity_id = 0x8001
         self.mac_bridge_port_ani_entity_id = 0x2102  # TODO: can we just use the entity id from the anis list?
@@ -126,17 +126,6 @@
         return self._port_number
 
     @property
-    def next_gem_entity_id(self):
-        self.log.debug('function-entry')
-        entity_id = self._next_entity_id
-
-        self._next_entity_id = self._next_entity_id + 1
-        if self._next_entity_id > PonPort.MAX_GEM_ENTITY_ID:
-            self._next_entity_id = PonPort.MIN_GEM_ENTITY_ID
-
-        return entity_id
-
-    @property
     def tconts(self):
         self.log.debug('function-entry')
         return self._tconts
@@ -182,7 +171,7 @@
         # adapter_agent add_port also does an update of port status
         try:
             yield self._handler.core_proxy.port_state_update(self._handler.device_id, self._port.type,
-                                                             self._port.port_no,self._port.oper_status)
+                                                             self._port.port_no, self._port.oper_status)
         except Exception as e:
             self.log.exception('update-port', e=e)
 
@@ -197,10 +186,10 @@
         self.log.debug('function-entry', tcont=tcont.alloc_id)
 
         if not self._valid:
-            return      # Deleting
+            return  # Deleting
 
         if not reflow and tcont.alloc_id in self._tconts:
-            return      # already created
+            return  # already created
 
         self.log.info('add-tcont', tcont=tcont.alloc_id, reflow=reflow)
         self._tconts[tcont.alloc_id] = tcont
@@ -216,17 +205,17 @@
         tcont.traffic_descriptor = new_td
 
         # TODO: Not yet implemented
-        #TODO: How does this affect ONU tcont settings?
-        #try:
+        # TODO: How does this affect ONU tcont settings?
+        # try:
         #    results = yield tcont.add_to_hardware(self._handler.omci)
-        #except Exception as e:
+        # except Exception as e:
         #    self.log.exception('tcont', tcont=tcont, e=e)
         #    # May occur with xPON provisioning, use hw-resync to recover
         #    results = 'resync needed'
         # returnValue(results)
 
     @inlineCallbacks
-    def remove_tcont(self, alloc_id):
+    def remove_tcont(self, alloc_id, remove_from_hw=True):
         self.log.debug('function-entry')
 
         tcont = self._tconts.get(alloc_id)
@@ -236,8 +225,9 @@
 
         try:
             del self._tconts[alloc_id]
-            results = yield tcont.remove_from_hardware(self._handler.openomci.omci_cc)
-            returnValue(results)
+            if remove_from_hw:
+                results = yield tcont.remove_from_hardware(self._handler.openomci.omci_cc)
+                returnValue(results)
 
         except Exception as e:
             self.log.exception('delete', e=e)
@@ -269,18 +259,19 @@
         if not reflow and (gem_port.gem_id, gem_port.direction) in self._gem_ports:
             return  # nop
 
-        # if this is actually a new gem port then issue the next entity_id
-        gem_port.entity_id = self.next_gem_entity_id
+        # The gem_port entity id is set to be same as gem_id
+        gem_port.entity_id = gem_port.gem_id
         self.log.info('add-gem-port', gem_port=gem_port, reflow=reflow)
         self._gem_ports[(gem_port.gem_id, gem_port.direction)] = gem_port
 
     @inlineCallbacks
-    def remove_gem_id(self, gem_id, direction):
+    def remove_gem_id(self, gem_id, direction, remove_from_hw=True):
         """
         Remove a GEM Port from this ONU
 
         :param gem_id: (GemPort) GEM Port to remove
         :param direction: Direction of the gem port
+        :param remove_from_hw: Remove the GemPort from hardware (remove if True else not)
         :return: deferred
         """
         self.log.debug('function-entry', gem_id=gem_id)
@@ -292,11 +283,10 @@
 
         try:
             del self._gem_ports[(gem_id, direction)]
-            results = yield gem_port.remove_from_hardware(self._handler.openomci.omci_cc)
-            returnValue(results)
+            if remove_from_hw:
+                results = yield gem_port.remove_from_hardware(self._handler.openomci.omci_cc)
+                returnValue(results)
 
         except Exception as ex:
             self.log.exception('gem-port-delete', e=ex)
             raise
-
-
diff --git a/python/requirements.txt b/python/requirements.txt
index c8e2a23..02a8ea0 100644
--- a/python/requirements.txt
+++ b/python/requirements.txt
@@ -1,2 +1,2 @@
-voltha-protos==2.1.0
-pyvoltha==2.3.1
+voltha-protos==2.1.1
+pyvoltha==2.3.2