VOL-1451 Initial checkin of openonu build

Produced docker container capable of building and running
openonu/brcm_openonci_onu.  Copied over current onu code
and resolved all imports by copying into the local source tree.

Change-Id: Ib9785d37afc65b7d32ecf74aee2456352626e2b6
diff --git a/python/adapters/brcm_openomci_onu/omci/__init__.py b/python/adapters/brcm_openomci_onu/omci/__init__.py
new file mode 100644
index 0000000..b0fb0b2
--- /dev/null
+++ b/python/adapters/brcm_openomci_onu/omci/__init__.py
@@ -0,0 +1,13 @@
+# Copyright 2017-present Open Networking Foundation
+#
+# 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.
diff --git a/python/adapters/brcm_openomci_onu/omci/brcm_capabilities_task.py b/python/adapters/brcm_openomci_onu/omci/brcm_capabilities_task.py
new file mode 100644
index 0000000..6bf5b93
--- /dev/null
+++ b/python/adapters/brcm_openomci_onu/omci/brcm_capabilities_task.py
@@ -0,0 +1,155 @@
+#
+# 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.
+
+import structlog
+from voltha.extensions.omci.tasks.onu_capabilities_task import OnuCapabilitiesTask
+from twisted.internet.defer import failure
+
+
+class BrcmCapabilitiesTask(OnuCapabilitiesTask):
+    """
+    OpenOMCI MIB Capabilities Task - BROADCOM ONUs
+
+    This task requests information on supported MEs via the OMCI (ME#287)
+    Managed entity.
+
+    This task should be ran after MIB Synchronization and before any MIB
+    Downloads to the ONU.
+
+    Upon completion, the Task deferred callback is invoked with dictionary
+    containing the supported managed entities and message types.
+
+    results = {
+                'supported-managed-entities': {set of supported managed entities},
+                'supported-message-types': {set of supported message types}
+              }
+    """
+    def __init__(self, omci_agent, device_id):
+        """
+        Class initialization
+
+        :param omci_agent: (OmciAdapterAgent) OMCI Adapter agent
+        :param device_id: (str) ONU Device ID
+        """
+        self.log = structlog.get_logger(device_id=device_id)
+        self.log.debug('function-entry')
+
+        super(BrcmCapabilitiesTask, self).__init__(omci_agent, device_id)
+        self._omci_managed = False      # TODO: Look up capabilities/model number
+
+    @property
+    def supported_managed_entities(self):
+        """
+        Return a set of the Managed Entity class IDs supported on this ONU
+
+        None is returned if not MEs have been discovered
+
+        :return: (set of ints)
+        """
+        self.log.debug('function-entry')
+
+        if self._omci_managed:
+            return super(BrcmCapabilitiesTask, self).supported_managed_entities
+
+        # TODO: figure out why broadcom wont answer for ME 287 to get this.  otherwise manually fill in
+        me_1287800f1 = [
+            2, 5, 6, 7, 11, 24, 45, 46, 47, 48, 49, 50, 51, 52, 78, 79, 84, 89, 130,
+            131, 133, 134, 135, 136, 137, 148, 157, 158, 159, 162, 163, 164, 171, 240,
+            241, 242, 256, 257, 262, 263, 264, 266, 268, 272, 273, 274, 276, 277, 278,
+            279, 280, 281, 287, 296, 297, 298, 307, 308, 309, 310, 311, 312, 321, 322,
+            329, 330, 332, 334, 336, 340, 341, 342, 343, 347, 348, 425, 426
+        ]
+        return frozenset(list(me_1287800f1))
+
+    @property
+    def supported_message_types(self):
+        """
+        Return a set of the Message Types supported on this ONU
+
+        None is returned if no message types have been discovered
+
+        :return: (set of EntityOperations)
+        """
+        self.log.debug('function-entry')
+
+        if self._omci_managed:
+            return super(BrcmCapabilitiesTask, self).supported_message_types
+
+        # TODO: figure out why broadcom wont answer for ME 287 to get this.  otherwise manually fill in
+        from voltha.extensions.omci.omci_entities import EntityOperations
+        op_11287800f1 = [
+            EntityOperations.Create,
+            EntityOperations.CreateComplete,
+            EntityOperations.Delete,
+            EntityOperations.Set,
+            EntityOperations.Get,
+            EntityOperations.GetComplete,
+            EntityOperations.GetAllAlarms,
+            EntityOperations.GetAllAlarmsNext,
+            EntityOperations.MibUpload,
+            EntityOperations.MibUploadNext,
+            EntityOperations.MibReset,
+            EntityOperations.AlarmNotification,
+            EntityOperations.AttributeValueChange,
+            EntityOperations.Test,
+            EntityOperations.StartSoftwareDownload,
+            EntityOperations.DownloadSection,
+            EntityOperations.EndSoftwareDownload,
+            EntityOperations.ActivateSoftware,
+            EntityOperations.CommitSoftware,
+            EntityOperations.SynchronizeTime,
+            EntityOperations.Reboot,
+            EntityOperations.GetNext,
+        ]
+        return frozenset(op_11287800f1)
+
+    def perform_get_capabilities(self):
+        """
+        Perform the MIB Capabilities sequence.
+
+        The sequence is to perform a Get request with the attribute mask equal
+        to 'me_type_table'.  The response to this request will carry the size
+        of (number of get-next sequences).
+
+        Then a loop is entered and get-next commands are sent for each sequence
+        requested.
+        """
+        self.log.debug('function-entry')
+
+        self.log.info('perform-get')
+
+        if self._omci_managed:
+            # Return generator deferred/results
+            return super(BrcmCapabilitiesTask, self).perform_get_capabilities()
+
+        # Fixed values, no need to query
+        try:
+            self._supported_entities = self.supported_managed_entities
+            self._supported_msg_types = self.supported_message_types
+
+            self.log.debug('get-success',
+                           supported_entities=self.supported_managed_entities,
+                           supported_msg_types=self.supported_message_types)
+            results = {
+                'supported-managed-entities': self.supported_managed_entities,
+                'supported-message-types': self.supported_message_types
+            }
+            self.deferred.callback(results)
+
+        except Exception as e:
+            self.log.exception('get-failed', e=e)
+            self.deferred.errback(failure.Failure(e))
+
+
diff --git a/python/adapters/brcm_openomci_onu/omci/brcm_get_mds_task.py b/python/adapters/brcm_openomci_onu/omci/brcm_get_mds_task.py
new file mode 100644
index 0000000..eabf356
--- /dev/null
+++ b/python/adapters/brcm_openomci_onu/omci/brcm_get_mds_task.py
@@ -0,0 +1,61 @@
+#
+# 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.
+
+import structlog
+from voltha.extensions.omci.tasks.get_mds_task import GetMdsTask
+
+
+class BrcmGetMdsTask(GetMdsTask):
+    """
+    OpenOMCI Get MIB Data Sync value task - Broadcom ONU
+
+    On successful completion, this task will call the 'callback' method of the
+    deferred returned by the start method and return the value of the MIB
+    Data Sync attribute of the ONT Data ME
+    """
+    name = "BRCM: Get MDS Task"
+
+    def __init__(self, omci_agent, device_id):
+        """
+        Class initialization
+
+        :param omci_agent: (OmciAdapterAgent) OMCI Adapter agent
+        :param device_id: (str) ONU Device ID
+        """
+        self.log = structlog.get_logger(device_id=device_id)
+        self.log.debug('function-entry')
+
+        super(BrcmGetMdsTask, self).__init__(omci_agent, device_id)
+
+        self.name = BrcmGetMdsTask.name
+        self._device = omci_agent.get_device(device_id)
+        self._omci_managed = False      # TODO: Look up capabilities/model number/check handler
+
+    def perform_get_mds(self):
+        """
+        Get the 'mib_data_sync' attribute of the ONU
+        """
+        self.log.debug('function-entry')
+        self.log.info('perform-get-mds')
+
+        if self._omci_managed:
+            return super(BrcmGetMdsTask, self).perform_get_mds()
+
+        # Non-OMCI managed BRCM ONUs always return 0 for MDS, use the MIB
+        # sync value and depend on an accelerated mib resync to do the
+        # proper comparison
+
+        self.deferred.callback(self._device.mib_synchronizer.mib_data_sync)
+
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
new file mode 100644
index 0000000..3341219
--- /dev/null
+++ b/python/adapters/brcm_openomci_onu/omci/brcm_mib_download_task.py
@@ -0,0 +1,449 @@
+#
+# 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.
+
+import structlog
+from common.frameio.frameio import hexify
+from twisted.internet import reactor
+from twisted.internet.defer import inlineCallbacks, returnValue, TimeoutError, failure
+from voltha.extensions.omci.omci_me import *
+from voltha.extensions.omci.tasks.task import Task
+from voltha.extensions.omci.omci_defs import *
+from voltha.adapters.brcm_openomci_onu.uni_port import *
+from voltha.adapters.brcm_openomci_onu.pon_port \
+    import BRDCM_DEFAULT_VLAN, TASK_PRIORITY, DEFAULT_TPID, DEFAULT_GEM_PAYLOAD
+
+OP = EntityOperations
+RC = ReasonCodes
+
+
+class MibDownloadFailure(Exception):
+    """
+    This error is raised by default when the download fails
+    """
+
+
+class MibResourcesFailure(Exception):
+    """
+    This error is raised by when one or more resources required is not available
+    """
+
+
+class BrcmMibDownloadTask(Task):
+    """
+    OpenOMCI MIB Download Example
+
+    This task takes the legacy OMCI 'script' for provisioning the Broadcom ONU
+    and converts it to run as a Task on the OpenOMCI Task runner.  This is
+    in order to begin to decompose service instantiation in preparation for
+    Technology Profile work.
+
+    Once technology profiles are ready, some of this task may hang around or
+    be moved into OpenOMCI if there are any very common settings/configs to do
+    for any profile that may be provided in the v2.0 release
+
+    Currently, the only service tech profiles expected by v2.0 will be for AT&T
+    residential data service and DT residential data service.
+    """
+
+    name = "Broadcom MIB Download Example Task"
+
+    def __init__(self, omci_agent, handler):
+        """
+        Class initialization
+
+        :param omci_agent: (OmciAdapterAgent) OMCI Adapter agent
+        :param device_id: (str) ONU Device ID
+        """
+
+        self.log = structlog.get_logger(device_id=handler.device_id)
+        self.log.debug('function-entry')
+
+        super(BrcmMibDownloadTask, self).__init__(BrcmMibDownloadTask.name,
+                                                  omci_agent,
+                                                  handler.device_id,
+                                                  priority=TASK_PRIORITY)
+        self._handler = handler
+        self._onu_device = omci_agent.get_device(handler.device_id)
+        self._local_deferred = None
+
+        # Frame size
+        self._max_gem_payload = DEFAULT_GEM_PAYLOAD
+
+        self._pon = handler.pon_port
+
+        # Defaults
+        self._input_tpid = DEFAULT_TPID
+        self._output_tpid = DEFAULT_TPID
+
+        self._vlan_tcis_1 = BRDCM_DEFAULT_VLAN
+        self._cvid = BRDCM_DEFAULT_VLAN
+        self._vlan_config_entity_id = self._vlan_tcis_1
+
+        # 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 = \
+            self._handler.mac_bridge_service_profile_entity_id
+        self._ieee_mapper_service_profile_entity_id = \
+            self._pon.ieee_mapper_service_profile_entity_id
+        self._mac_bridge_port_ani_entity_id = \
+            self._pon.mac_bridge_port_ani_entity_id
+        self._gal_enet_profile_entity_id = \
+            self._handler.gal_enet_profile_entity_id
+
+        self._free_ul_prior_q_entity_ids = set()
+        self._free_dl_prior_q_entity_ids = set()
+
+    def cancel_deferred(self):
+        self.log.debug('function-entry')
+        super(BrcmMibDownloadTask, 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 MIB Download
+        """
+        self.log.debug('function-entry')
+        super(BrcmMibDownloadTask, self).start()
+        self._local_deferred = reactor.callLater(0, self.perform_mib_download)
+
+    def stop(self):
+        """
+        Shutdown MIB Synchronization tasks
+        """
+        self.log.debug('function-entry')
+        self.log.debug('stopping')
+
+        self.cancel_deferred()
+        super(BrcmMibDownloadTask, 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: %s", 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 MibDownloadFailure('{} failed with a status of {}, error_mask: {}, failed_mask: {}, unsupported_mask: {}'
+                                 .format(operation, status, error_mask, failed_mask, unsupported_mask))
+
+    @inlineCallbacks
+    def perform_mib_download(self):
+        """
+        Send the commands to minimally configure the PON, Bridge, and
+        UNI ports for this device. The application of any service flows
+        and other characteristics are done as needed.
+        """
+        try:
+            self.log.debug('function-entry')
+            self.log.info('perform-download')
+
+            device = self._handler.adapter_agent.get_device(self.device_id)
+
+            if self._handler.enabled and len(self._handler.uni_ports) > 0:
+                device.reason = 'performing-initial-mib-download'
+                self._handler.adapter_agent.update_device(device)
+
+            try:
+                # Lock the UNI ports to prevent any alarms during initial configuration
+                # of the ONU
+                self.strobe_watchdog()
+
+                # Provision the initial bridge configuration
+                yield self.perform_initial_bridge_setup()
+
+                for uni_port in self._handler.uni_ports:
+                    yield self.enable_uni(uni_port, True)
+
+                    # Provision the initial bridge configuration
+                    yield self.perform_uni_initial_bridge_setup(uni_port)
+
+                    # And re-enable the UNIs if needed
+                    yield self.enable_uni(uni_port, False)
+
+                self.deferred.callback('initial-download-success')
+
+            except TimeoutError as e:
+                self.log.error('initial-download-failure', e=e)
+                self.deferred.errback(failure.Failure(e))
+
+            except Exception as e:
+                self.log.exception('initial-download-failure', e=e)
+                self.deferred.errback(failure.Failure(e))
+
+            else:
+                e = MibResourcesFailure('Required resources are not available',
+                                        len(self._handler.uni_ports))
+                self.deferred.errback(failure.Failure(e))
+        except BaseException as e:
+            self.log.debug('@thyy_mib_check:', exception=e)
+
+    @inlineCallbacks
+    def perform_initial_bridge_setup(self):
+        self.log.debug('function-entry')
+
+        omci_cc = self._onu_device.omci_cc
+        # TODO: too many magic numbers
+
+        try:
+            ########################################################################################
+            # Create GalEthernetProfile - Once per ONU/PON interface
+            #
+            #  EntityID will be referenced by:
+            #            - GemInterworkingTp
+            #  References:
+            #            - Nothing
+
+            msg = GalEthernetProfileFrame(
+                self._gal_enet_profile_entity_id,
+                max_gem_payload_size=self._max_gem_payload
+            )
+            frame = msg.create()
+            self.log.debug('openomci-msg', omci_msg=msg)
+            results = yield omci_cc.send(frame)
+            self.check_status_and_state(results, 'create-gal-ethernet-profile')
+
+        except TimeoutError as e:
+            self.log.warn('rx-timeout-0', e=e)
+            raise
+
+        except Exception as e:
+            self.log.exception('omci-setup-0', e=e)
+            raise
+
+        returnValue(None)
+
+    @inlineCallbacks
+    def perform_uni_initial_bridge_setup(self, uni_port):
+        self.log.debug('function-entry')
+        omci_cc = self._onu_device.omci_cc
+        frame = None
+        try:
+            ################################################################################
+            # Common - PON and/or UNI                                                      #
+            ################################################################################
+            # MAC Bridge Service Profile
+            #
+            #  EntityID will be referenced by:
+            #            - MAC Bridge Port Configuration Data (PON & UNI)
+            #  References:
+            #            - Nothing
+
+            # TODO: magic. event if static, assign to a meaningful variable name
+            attributes = {
+                'spanning_tree_ind': False,
+                'learning_ind': True,
+                'priority': 0x8000,
+                'max_age': 20 * 256,
+                'hello_time': 2 * 256,
+                'forward_delay': 15 * 256,
+                'unknown_mac_address_discard': True
+            }
+            msg = MacBridgeServiceProfileFrame(
+                self._mac_bridge_service_profile_entity_id + uni_port.mac_bridge_port_num,
+                attributes
+            )
+            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-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-part-1')
+
+            ################################################################################
+            # VLAN Tagging Filter config
+            #
+            #  EntityID will be referenced by:
+            #            - Nothing
+            #  References:
+            #            - MacBridgePortConfigurationData for the ANI/PON side
+            #
+            # Set anything, this request will not be used when using Extended Vlan
+
+            # TODO: magic. make a static variable for forward_op
+            msg = VlanTaggingFilterDataFrame(
+                self._mac_bridge_port_ani_entity_id + uni_port.mac_bridge_port_num,  # Entity ID
+                vlan_tcis=[self._vlan_tcis_1],        # VLAN IDs
+                forward_operation=0x10
+            )
+            frame = msg.create()
+            self.log.debug('openomci-msg', omci_msg=msg)
+            results = yield omci_cc.send(frame)
+            self.check_status_and_state(results, 'create-vlan-tagging-filter-data')
+
+            ################################################################################
+            # UNI Specific                                                                 #
+            ################################################################################
+            # MAC Bridge Port config
+            # This configuration is for Ethernet UNI
+            #
+            #  EntityID will be referenced by:
+            #            - Nothing
+            #  References:
+            #            - MAC Bridge Service Profile (the bridge)
+            #            - PPTP Ethernet or VEIP UNI
+
+            # TODO: do this for all uni/ports...
+            # TODO: magic. make a static variable for tp_type
+
+            # default to PPTP
+            tp_type = None
+            if uni_port.type is UniType.VEIP:
+                tp_type = 11
+            elif uni_port.type is UniType.PPTP:
+                tp_type = 1
+            else:
+                tp_type = 1
+
+            msg = MacBridgePortConfigurationDataFrame(
+                uni_port.entity_id,            # Entity ID - This is read-only/set-by-create !!!
+                bridge_id_pointer=self._mac_bridge_service_profile_entity_id + uni_port.mac_bridge_port_num,  # Bridge Entity ID
+                port_num=uni_port.mac_bridge_port_num,   # Port ID
+                tp_type=tp_type,                         # PPTP Ethernet or VEIP UNI
+                tp_pointer=uni_port.entity_id            # Ethernet UNI 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-part-2')
+
+        except TimeoutError as e:
+            self.log.warn('rx-timeout-1', e=e)
+            raise
+
+        except Exception as e:
+            self.log.exception('omci-setup-1', e=e)
+            raise
+
+        returnValue(None)
+
+    @inlineCallbacks
+    def enable_uni(self, uni_port, force_lock):
+        """
+        Lock or unlock a single uni port
+
+        :param uni_port: UniPort to admin up/down
+        :param force_lock: (boolean) If True, force lock regardless of enabled state
+        """
+        self.log.debug('function-entry')
+
+        omci_cc = self._onu_device.omci_cc
+        frame = None
+
+        ################################################################################
+        #  Lock/Unlock UNI  -  0 to Unlock, 1 to lock
+        #
+        #  EntityID is referenced by:
+        #            - MAC bridge port configuration data for the UNI side
+        #  References:
+        #            - Nothing
+        try:
+            state = 1 if force_lock or not uni_port.enabled else 0
+            msg = None
+            if uni_port.type is UniType.PPTP:
+                msg = PptpEthernetUniFrame(uni_port.entity_id,
+                                           attributes=dict(administrative_state=state))
+            elif uni_port.type is UniType.VEIP:
+                msg = VeipUniFrame(uni_port.entity_id,
+                                   attributes=dict(administrative_state=state))
+            else:
+                self.log.warn('unknown-uni-type', uni_port=uni_port)
+
+            if msg:
+               frame = msg.set()
+               self.log.debug('openomci-msg', omci_msg=msg)
+               results = yield omci_cc.send(frame)
+               self.check_status_and_state(results, 'set-pptp-ethernet-uni-lock-restore')
+
+        except TimeoutError as e:
+            self.log.warn('rx-timeout', e=e)
+            raise
+
+        except Exception as e:
+            self.log.exception('omci-failure', e=e)
+            raise
+
+        returnValue(None)
diff --git a/python/adapters/brcm_openomci_onu/omci/brcm_mib_sync.py b/python/adapters/brcm_openomci_onu/omci/brcm_mib_sync.py
new file mode 100644
index 0000000..1898c52
--- /dev/null
+++ b/python/adapters/brcm_openomci_onu/omci/brcm_mib_sync.py
@@ -0,0 +1,77 @@
+#
+# 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.
+
+import structlog
+from twisted.internet import reactor
+from voltha.extensions.omci.state_machines.mib_sync import MibSynchronizer
+
+log = structlog.get_logger()
+
+class BrcmMibSynchronizer(MibSynchronizer):
+    """
+    OpenOMCI MIB Synchronizer state machine for Broadcom ONUs
+    """
+
+    def __init__(self, agent, device_id, mib_sync_tasks, db,
+                 advertise_events=False):
+        """
+        Class initialization
+
+        :param agent: (OpenOmciAgent) Agent
+        :param device_id: (str) ONU Device ID
+        :param db: (MibDbVolatileDict) MIB Database
+        :param mib_sync_tasks: (dict) Tasks to run
+        :param advertise_events: (bool) Advertise events on OpenOMCI Event Bus
+        """
+        self.log = structlog.get_logger(device_id=device_id)
+        self.log.debug('function-entry')
+
+        super(BrcmMibSynchronizer, self).__init__(agent, device_id, mib_sync_tasks, db,
+                                                  advertise_events=advertise_events)
+
+    def on_enter_starting(self):
+        """
+        Given resync and mib update is questionable (see below) flag the ONU as a new device which forces a mib
+        reset and a mib upload
+        """
+        self.log.warn('db-sync-not-supported-forcing-reset')
+        self._last_mib_db_sync_value = None
+        super(BrcmMibSynchronizer, self).on_enter_starting()
+
+    def on_enter_auditing(self):
+        """
+        Perform a MIB Audit.  Currently this is broken on BRCM based onu and its never in sync and continuously
+        retries. On disable/enable it never enables becaues its never in sync.  Effectively disable the function so
+        disable/enable works and we can figure out whats going on
+
+        Oddly enough this is only an issue with MibVolatileDict
+        """
+        # TODO: Actually fix resync
+        self.log.warn('audit-resync-not-supported')
+
+        self._deferred = reactor.callLater(0, self.success)
+
+    def on_enter_examining_mds(self):
+        """
+        Examine MIB difference counter between onu and voltha.  Currently same problem as on_enter_auditing.
+        examine mds is always mismatched and causing disable/enable to fail
+
+        Oddly enough this is only an issue with MibVolatileDict
+        """
+        # TODO: Actually fix resync
+        self.log.warn('examine-mds-resync-not-supported')
+
+        self._deferred = reactor.callLater(0, self.success)
+
diff --git a/python/adapters/brcm_openomci_onu/omci/brcm_tp_service_specific_task.py b/python/adapters/brcm_openomci_onu/omci/brcm_tp_service_specific_task.py
new file mode 100644
index 0000000..ff0bd30
--- /dev/null
+++ b/python/adapters/brcm_openomci_onu/omci/brcm_tp_service_specific_task.py
@@ -0,0 +1,482 @@
+#
+# 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.
+
+import structlog
+from common.frameio.frameio import hexify
+from twisted.internet import reactor
+from twisted.internet.defer import inlineCallbacks, returnValue, TimeoutError, failure
+from voltha.extensions.omci.omci_me import *
+from voltha.extensions.omci.tasks.task import Task
+from voltha.extensions.omci.omci_defs import *
+from voltha.adapters.brcm_openomci_onu.uni_port import *
+from voltha.adapters.brcm_openomci_onu.pon_port \
+    import BRDCM_DEFAULT_VLAN, TASK_PRIORITY, DEFAULT_TPID, DEFAULT_GEM_PAYLOAD
+
+OP = EntityOperations
+RC = ReasonCodes
+
+
+class TechProfileDownloadFailure(Exception):
+    """
+    This error is raised by default when the download fails
+    """
+
+
+class TechProfileResourcesFailure(Exception):
+    """
+    This error is raised by when one or more resources required is not available
+    """
+
+
+class BrcmTpServiceSpecificTask(Task):
+    """
+    OpenOMCI Tech-Profile Download Task
+
+    """
+
+    name = "Broadcom Tech-Profile Download Task"
+
+    def __init__(self, omci_agent, handler, uni_id):
+        """
+        Class initialization
+
+        :param omci_agent: (OmciAdapterAgent) OMCI Adapter agent
+        :param device_id: (str) ONU Device 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)
+
+        self.log = log
+
+        self._onu_device = omci_agent.get_device(handler.device_id)
+        self._local_deferred = None
+
+        # Frame size
+        self._max_gem_payload = DEFAULT_GEM_PAYLOAD
+
+        self._uni_port = handler.uni_ports[uni_id]
+        assert self._uni_port.uni_id == uni_id
+
+        # Port numbers
+        self._input_tpid = DEFAULT_TPID
+        self._output_tpid = DEFAULT_TPID
+
+        self._vlan_tcis_1 = BRDCM_DEFAULT_VLAN
+        self._cvid = BRDCM_DEFAULT_VLAN
+        self._vlan_config_entity_id = self._vlan_tcis_1
+
+        # 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
+        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 handler.pon_port.tconts.itervalues():
+            if tcont.uni_id is not None and tcont.uni_id != self._uni_port.uni_id: continue
+            self._tconts.append(tcont)
+
+        self._gem_ports = []
+        for gem_port in handler.pon_port.gem_ports.itervalues():
+            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)
+
+        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()
+
+        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 Download
+        """
+        self.log.debug('function-entry')
+        super(BrcmTpServiceSpecificTask, self).start()
+        self._local_deferred = reactor.callLater(0, self.perform_service_specific_steps)
+
+    def stop(self):
+        """
+        Shutdown Tech-Profile download tasks
+        """
+        self.log.debug('function-entry')
+        self.log.debug('stopping')
+
+        self.cancel_deferred()
+        super(BrcmTpServiceSpecificTask, 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: %s", 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 TechProfileDownloadFailure(
+            '{} 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):
+        self.log.debug('function-entry')
+
+        omci_cc = self._onu_device.omci_cc
+
+        try:
+            ################################################################################
+            # TCONTS
+            #
+            #  EntityID will be referenced by:
+            #            - GemPortNetworkCtp
+            #  References:
+            #            - ONU created TCONT (created on ONU startup)
+
+            tcont_idents = self._onu_device.query_mib(Tcont.class_id)
+            self.log.debug('tcont-idents', tcont_idents=tcont_idents)
+
+            for tcont in self._tconts:
+                self.log.debug('tcont-loop', tcont=tcont)
+
+                if tcont.entity_id is None:
+                    free_entity_id = None
+                    for k, v in tcont_idents.items():
+                        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:
+                            free_entity_id = k
+                            break
+
+                    self.log.debug('tcont-loop-free', free_entity_id=free_entity_id, alloc_id=tcont.alloc_id)
+
+                    if free_entity_id is None:
+                        self.log.error('no-available-tconts')
+                        break
+
+                    # Also assign entity id within tcont object
+                    results = yield tcont.add_to_hardware(omci_cc, free_entity_id)
+                    self.check_status_and_state(results, 'new-tcont-added')
+                else:
+                    # likely already added given entity_id is set, but no harm in doing it again
+                    results = yield tcont.add_to_hardware(omci_cc, tcont.entity_id)
+                    self.check_status_and_state(results, 'existing-tcont-added')
+
+            ################################################################################
+            # GEMS  (GemPortNetworkCtp and GemInterworkingTp)
+            #
+            #  For both of these MEs, the entity_id is the GEM Port ID. The entity id of the
+            #  GemInterworkingTp ME could be different since it has an attribute to specify
+            #  the GemPortNetworkCtp entity id.
+            #
+            #        for the GemPortNetworkCtp ME
+            #
+            #  GemPortNetworkCtp
+            #    EntityID will be referenced by:
+            #              - GemInterworkingTp
+            #    References:
+            #              - TCONT
+            #              - Hardcoded upstream TM Entity ID
+            #              - (Possibly in Future) Upstream Traffic descriptor profile pointer
+            #
+            #  GemInterworkingTp
+            #    EntityID will be referenced by:
+            #              - Ieee8021pMapperServiceProfile
+            #    References:
+            #              - GemPortNetworkCtp
+            #              - Ieee8021pMapperServiceProfile
+            #              - GalEthernetProfile
+            #
+
+            onu_g = self._onu_device.query_mib(OntG.class_id)
+            # If the traffic management option attribute in the ONU-G ME is 0
+            # (priority controlled) or 2 (priority and rate controlled), this
+            # pointer specifies the priority queue ME serving this GEM port
+            # network CTP. If the traffic management option attribute is 1
+            # (rate controlled), this attribute redundantly points to the
+            # T-CONT serving this GEM port network CTP.
+            traffic_mgmt_opt = \
+                onu_g.get('attributes', {}).get('traffic_management_options', 0)
+            self.log.debug("traffic-mgmt-option", traffic_mgmt_opt=traffic_mgmt_opt)
+
+            prior_q = self._onu_device.query_mib(PriorityQueueG.class_id)
+            for k, v in prior_q.items():
+                self.log.debug("prior-q", k=k, v=v)
+
+                try:
+                    _ = iter(v)
+                except TypeError:
+                    continue
+
+                if 'instance_id' in v:
+                    related_port = v['attributes']['related_port']
+                    if v['instance_id'] & 0b1000000000000000:
+                        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)
+                            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 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)
+                                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)
+
+            for gem_port in self._gem_ports:
+                # TODO: Traffic descriptor will be available after meter bands are available
+                tcont = gem_port.tcont
+                if tcont is None:
+                    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":
+
+                    # Sort the priority queue list in order of priority.
+                    # 0 is highest priority and 0x0fff is lowest.
+                    self.tcont_me_to_queue_map[tcont.entity_id].sort()
+                    self.uni_port_to_queue_map[self._uni_port.entity_id].sort()
+                    # Get the priority queue associated with p-bit that is
+                    # mapped to the gem port.
+                    # p-bit-7 is highest priority and p-bit-0 is lowest
+                    # Gem port associated with p-bit-7 should be mapped to
+                    # highest priority queue and gem port associated with p-bit-0
+                    # should be mapped to lowest priority queue.
+                    # The self.tcont_me_to_queue_map and self.uni_port_to_queue_map
+                    # have priority queue entities ordered in descending order
+                    # of priority
+                    for i, p in enumerate(gem_port.pbit_map):
+                        if p == '1':
+                            ul_prior_q_entity_id = \
+                                self.tcont_me_to_queue_map[tcont.entity_id][i]
+                            dl_prior_q_entity_id = \
+                                self.uni_port_to_queue_map[self._uni_port.entity_id][i]
+                            break
+
+                    assert ul_prior_q_entity_id is not None and \
+                           dl_prior_q_entity_id is not None
+
+                    # 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)
+                    self.check_status_and_state(results, 'assign-gem-port')
+                elif gem_port.direction == "downstream":
+                    # Downstream is inverse of upstream
+                    # TODO: could also be a case of multicast. Not supported for now
+                    self.log.debug("skipping-downstream-gem", gem_port=gem_port)
+                    pass
+
+            ################################################################################
+            # Update the IEEE 802.1p Mapper Service Profile config
+            #
+            #  EntityID was created prior to this call. This is a set
+            #
+            #  References:
+            #            - Gem Interwork TPs are set here
+            #
+
+            gem_entity_ids = [OmciNullPointer] * 8
+            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)
+
+                if gem_port.direction == "upstream" or \
+                        gem_port.direction == "bi-directional":
+                    for i, p in enumerate(reversed(gem_port.pbit_map)):
+                        if p == '1':
+                            gem_entity_ids[i] = gem_port.entity_id
+                elif gem_port.direction == "downstream":
+                    # Downstream gem port p-bit mapper is inverse of upstream
+                    # TODO: Could also be a case of multicast. Not supported for now
+                    pass
+
+            msg = Ieee8021pMapperServiceProfileFrame(
+                self._ieee_mapper_service_profile_entity_id + self._uni_port.mac_bridge_port_num,  # 802.1p mapper Service Mapper Profile ID
+                interwork_tp_pointers=gem_entity_ids  # Interworking TP IDs
+            )
+            frame = msg.set()
+            self.log.debug('openomci-msg', omci_msg=msg)
+            results = yield omci_cc.send(frame)
+            self.check_status_and_state(results, 'set-8021p-mapper-service-profile-ul')
+
+            ################################################################################
+            # Create Extended VLAN Tagging Operation config (PON-side)
+            #
+            #  EntityID relates to the VLAN TCIS
+            #  References:
+            #            - VLAN TCIS from previously created VLAN Tagging filter data
+            #            - PPTP Ethernet or VEIP UNI
+            #
+
+            # TODO: do this for all uni/ports...
+            # TODO: magic.  static variable for assoc_type
+
+            # default to PPTP
+            if self._uni_port.type is UniType.VEIP:
+                association_type = 10
+            elif self._uni_port.type is UniType.PPTP:
+                association_type = 2
+            else:
+                association_type = 2
+
+            attributes = dict(
+                association_type=association_type,                  # Assoc Type, PPTP/VEIP Ethernet UNI
+                associated_me_pointer=self._uni_port.entity_id,      # Assoc ME, PPTP/VEIP Entity Id
+
+                # See VOL-1311 - Need to set table during create to avoid exception
+                # trying to read back table during post-create-read-missing-attributes
+                # But, because this is a R/W attribute. Some ONU may not accept the
+                # value during create. It is repeated again in a set below.
+                input_tpid=self._input_tpid,  # input TPID
+                output_tpid=self._output_tpid,  # output TPID
+            )
+
+            msg = ExtendedVlanTaggingOperationConfigurationDataFrame(
+                self._mac_bridge_service_profile_entity_id + self._uni_port.mac_bridge_port_num,  # Bridge Entity ID
+                attributes=attributes
+            )
+
+            frame = msg.create()
+            self.log.debug('openomci-msg', omci_msg=msg)
+            results = yield omci_cc.send(frame)
+            self.check_status_and_state(results, 'create-extended-vlan-tagging-operation-configuration-data')
+
+            attributes = dict(
+                # Specifies the TPIDs in use and that operations in the downstream direction are
+                # inverse to the operations in the upstream direction
+                input_tpid=self._input_tpid,    # input TPID
+                output_tpid=self._output_tpid,  # output TPID
+                downstream_mode=0,              # inverse of upstream
+            )
+
+            msg = ExtendedVlanTaggingOperationConfigurationDataFrame(
+                self._mac_bridge_service_profile_entity_id + self._uni_port.mac_bridge_port_num,  # Bridge Entity ID
+                attributes=attributes
+            )
+
+            frame = msg.set()
+            self.log.debug('openomci-msg', omci_msg=msg)
+            results = yield omci_cc.send(frame)
+            self.check_status_and_state(results, 'set-extended-vlan-tagging-operation-configuration-data')
+
+            attributes = dict(
+                # parameters: Entity Id ( 0x900), Filter Inner Vlan Id(0x1000-4096,do not filter on Inner vid,
+                #             Treatment Inner Vlan Id : 2
+
+                # Update uni side extended vlan filter
+                # filter for untagged
+                # probably for eapol
+                # TODO: lots of magic
+                # TODO: magic 0x1000 / 4096?
+                received_frame_vlan_tagging_operation_table=
+                VlanTaggingOperation(
+                    filter_outer_priority=15,  # This entry is not a double-tag rule
+                    filter_outer_vid=4096,     # Do not filter on the outer VID value
+                    filter_outer_tpid_de=0,    # Do not filter on the outer TPID field
+
+                    filter_inner_priority=15,
+                    filter_inner_vid=4096,
+                    filter_inner_tpid_de=0,
+                    filter_ether_type=0,
+
+                    treatment_tags_to_remove=0,
+                    treatment_outer_priority=15,
+                    treatment_outer_vid=0,
+                    treatment_outer_tpid_de=0,
+
+                    treatment_inner_priority=0,
+                    treatment_inner_vid=self._cvid,
+                    treatment_inner_tpid_de=4,
+                )
+            )
+
+            msg = ExtendedVlanTaggingOperationConfigurationDataFrame(
+                self._mac_bridge_service_profile_entity_id + self._uni_port.mac_bridge_port_num,  # Bridge Entity ID
+                attributes=attributes
+            )
+
+            frame = msg.set()
+            self.log.debug('openomci-msg', omci_msg=msg)
+            results = yield omci_cc.send(frame)
+            self.check_status_and_state(results, 'set-extended-vlan-tagging-operation-configuration-data-table')
+
+            self.deferred.callback("tech-profile-download-success")
+
+        except TimeoutError as e:
+            self.log.warn('rx-timeout-2', e=e)
+            self.deferred.errback(failure.Failure(e))
+
+        except Exception as e:
+            self.log.exception('omci-setup-2', e=e)
+            self.deferred.errback(failure.Failure(e))
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
new file mode 100644
index 0000000..c304a27
--- /dev/null
+++ b/python/adapters/brcm_openomci_onu/omci/brcm_uni_lock_task.py
@@ -0,0 +1,140 @@
+#
+# 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 voltha.extensions.omci.tasks.task import Task
+from twisted.internet import reactor
+from twisted.internet.defer import inlineCallbacks, failure, returnValue
+from voltha.extensions.omci.omci_defs import ReasonCodes, EntityOperations
+from voltha.extensions.omci.omci_me import OntGFrame
+from voltha.extensions.omci.omci_me import PptpEthernetUniFrame, VeipUniFrame
+
+RC = ReasonCodes
+OP = EntityOperations
+
+
+class BrcmUniLockException(Exception):
+    pass
+
+
+class BrcmUniLockTask(Task):
+    """
+    Lock or unlock all discovered UNI/PPTP on the ONU
+    """
+    task_priority = 200
+    name = "Broadcom UNI Lock Task"
+
+    def __init__(self, omci_agent, device_id, lock=True, priority=task_priority):
+        """
+        Class initialization
+
+        :param omci_agent: (OmciAdapterAgent) OMCI Adapter agent
+        :param device_id: (str) ONU Device ID
+        :param lock: (bool) If true administratively lock all the UNI.  If false unlock
+        :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)
+        self._device = omci_agent.get_device(device_id)
+        self._lock = lock
+        self._results = None
+        self._local_deferred = None
+        self._config = self._device.configuration
+
+    def cancel_deferred(self):
+        super(BrcmUniLockTask, 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 UNI/PPTP Lock/Unlock Task
+        """
+        super(BrcmUniLockTask, self).start()
+        self._local_deferred = reactor.callLater(0, self.perform_lock)
+
+
+    @inlineCallbacks
+    def perform_lock(self):
+        """
+        Perform the lock/unlock
+        """
+        self.log.info('setting-uni-lock-state', lock=self._lock)
+
+        try:
+            state = 1 if self._lock else 0
+
+            # lock the whole ont and all the pptp.  some onu dont causing odd behavior.
+            msg = OntGFrame(attributes={'administrative_state': state})
+            frame = msg.set()
+            self.log.debug('openomci-msg', omci_msg=msg)
+            results = yield self._device.omci_cc.send(frame)
+            self.strobe_watchdog()
+
+            status = results.fields['omci_message'].fields['success_code']
+            self.log.info('response-status', status=status)
+
+            # Success?
+            if status in (RC.Success.value, RC.InstanceExists):
+                self.log.debug('set-lock-ontg', lock=self._lock)
+            else:
+                self.log.warn('cannot-set-lock-ontg', lock=self._lock)
+
+            pptp_list = sorted(self._config.pptp_entities) if self._config.pptp_entities else []
+            veip_list = sorted(self._config.veip_entities) if self._config.veip_entities else []
+
+            for entity_id in pptp_list:
+                pptp_value = self._config.pptp_entities[entity_id]
+                msg = PptpEthernetUniFrame(entity_id,
+                                           attributes=dict(administrative_state=state))
+                self._send_uni_lock_msg(entity_id, pptp_value, msg)
+
+            for entity_id in veip_list:
+                veip_value = self._config.veip_entities[entity_id]
+                msg = VeipUniFrame(entity_id,
+                                           attributes=dict(administrative_state=state))
+                self._send_uni_lock_msg(entity_id, veip_value, msg)
+
+            self.deferred.callback(self)
+
+        except Exception as e:
+            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()
+        self.log.debug('openomci-msg', omci_msg=me_message)
+        results = yield self._device.omci_cc.send(frame)
+        self.strobe_watchdog()
+
+        status = results.fields['omci_message'].fields['success_code']
+        self.log.info('response-status', status=status)
+
+        # Success?
+        if status in (RC.Success.value, RC.InstanceExists):
+            self.log.debug('set-lock-uni', uni=entity_id, value=value, lock=self._lock)
+        else:
+            self.log.warn('cannot-set-lock-uni', uni=entity_id, value=value, lock=self._lock)
+
+        returnValue(None)
diff --git a/python/adapters/brcm_openomci_onu/omci/brcm_vlan_filter_task.py b/python/adapters/brcm_openomci_onu/omci/brcm_vlan_filter_task.py
new file mode 100644
index 0000000..6c665c7
--- /dev/null
+++ b/python/adapters/brcm_openomci_onu/omci/brcm_vlan_filter_task.py
@@ -0,0 +1,216 @@
+#
+# 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 voltha.extensions.omci.tasks.task import Task
+from twisted.internet import reactor
+from twisted.internet.defer import inlineCallbacks, failure, returnValue
+from voltha.extensions.omci.omci_defs import ReasonCodes, EntityOperations
+from voltha.extensions.omci.omci_me import *
+
+RC = ReasonCodes
+OP = EntityOperations
+
+
+class BrcmVlanFilterException(Exception):
+    pass
+
+
+class BrcmVlanFilterTask(Task):
+    """
+    Apply Vlan Tagging Filter Data and Extended VLAN Tagging Operation Configuration on an ANI and UNI
+    """
+    task_priority = 200
+    name = "Broadcom VLAN Filter Task"
+
+    def __init__(self, omci_agent, device_id, uni_port, set_vlan_id, priority=task_priority):
+        """
+        Class initialization
+
+        :param omci_agent: (OmciAdapterAgent) OMCI Adapter agent
+        :param device_id: (str) ONU Device ID
+        :param set_vlan_id: (int) VLAN to filter for and set
+        :param priority: (int) OpenOMCI Task priority (0..255) 255 is the highest
+        """
+
+        self.log = structlog.get_logger(device_id=device_id, uni_port=uni_port.port_number)
+
+        super(BrcmVlanFilterTask, self).__init__(BrcmVlanFilterTask.name,
+                                                 omci_agent,
+                                                 device_id,
+                                                 priority=priority,
+                                                 exclusive=True)
+        self._device = omci_agent.get_device(device_id)
+        self._uni_port = uni_port
+        self._set_vlan_id = set_vlan_id
+        self._results = None
+        self._local_deferred = None
+        self._config = self._device.configuration
+
+    def cancel_deferred(self):
+        super(BrcmVlanFilterTask, 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 Vlan Tagging Task
+        """
+        super(BrcmVlanFilterTask, self).start()
+        self._local_deferred = reactor.callLater(0, self.perform_vlan_tagging)
+
+    @inlineCallbacks
+    def perform_vlan_tagging(self):
+        """
+        Perform the vlan tagging
+        """
+        self.log.info('setting-vlan-tagging')
+
+        try:
+            # TODO: parameterize these from the handler, or objects in the handler
+            # TODO: make this a member of the onu gem port or the uni port
+            _mac_bridge_service_profile_entity_id = 0x201
+            _mac_bridge_port_ani_entity_id = 0x2102  # TODO: can we just use the entity id from the anis list?
+            # Delete bridge ani side vlan filter
+            msg = VlanTaggingFilterDataFrame(_mac_bridge_port_ani_entity_id + self._uni_port.mac_bridge_port_num)
+            frame = msg.delete()
+            self.log.debug('openomci-msg', omci_msg=msg)
+            self.strobe_watchdog()
+            results = yield self._device.omci_cc.send(frame)
+            self.check_status_and_state(results, 'flow-delete-vlan-tagging-filter-data')
+
+            # Re-Create bridge ani side vlan filter
+            msg = VlanTaggingFilterDataFrame(
+                _mac_bridge_port_ani_entity_id + self._uni_port.mac_bridge_port_num,  # Entity ID
+                vlan_tcis=[self._set_vlan_id],  # VLAN IDs
+                forward_operation=0x10
+            )
+            frame = msg.create()
+            self.log.debug('openomci-msg', omci_msg=msg)
+            self.strobe_watchdog()
+            results = yield self._device.omci_cc.send(frame)
+            self.check_status_and_state(results, 'flow-create-vlan-tagging-filter-data')
+
+            # Re-Create bridge ani side vlan filter
+
+            # Update uni side extended vlan filter
+            # filter for untagged
+            # probably for eapol
+            # TODO: Create constants for the operation values.  See omci spec
+            attributes = dict(
+                received_frame_vlan_tagging_operation_table=
+                VlanTaggingOperation(
+                    filter_outer_priority=15,
+                    filter_outer_vid=4096,
+                    filter_outer_tpid_de=0,
+
+                    filter_inner_priority=15,
+                    filter_inner_vid=4096,
+                    filter_inner_tpid_de=0,
+                    filter_ether_type=0,
+
+                    treatment_tags_to_remove=0,
+                    treatment_outer_priority=15,
+                    treatment_outer_vid=0,
+                    treatment_outer_tpid_de=0,
+
+                    treatment_inner_priority=0,
+                    treatment_inner_vid=self._set_vlan_id,
+                    treatment_inner_tpid_de=4
+                )
+            )
+            msg = ExtendedVlanTaggingOperationConfigurationDataFrame(
+                _mac_bridge_service_profile_entity_id + self._uni_port.mac_bridge_port_num,  # Bridge Entity ID
+                attributes=attributes  # See above
+            )
+            frame = msg.set()
+            self.log.debug('openomci-msg', omci_msg=msg)
+            self.strobe_watchdog()
+            results = yield self._device.omci_cc.send(frame)
+            self.check_status_and_state(results,
+                                        'flow-set-ext-vlan-tagging-op-config-data-untagged')
+
+            # Update uni side extended vlan filter
+            # filter for vlan 0
+            # TODO: Create constants for the operation values.  See omci spec
+            attributes = dict(
+                received_frame_vlan_tagging_operation_table=
+                VlanTaggingOperation(
+                    filter_outer_priority=15,  # This entry is not a double-tag rule
+                    filter_outer_vid=4096,  # Do not filter on the outer VID value
+                    filter_outer_tpid_de=0,  # Do not filter on the outer TPID field
+
+                    filter_inner_priority=8,  # Filter on inner vlan
+                    filter_inner_vid=0x0,  # Look for vlan 0
+                    filter_inner_tpid_de=0,  # Do not filter on inner TPID field
+                    filter_ether_type=0,  # Do not filter on EtherType
+
+                    treatment_tags_to_remove=1,
+                    treatment_outer_priority=15,
+                    treatment_outer_vid=0,
+                    treatment_outer_tpid_de=0,
+
+                    treatment_inner_priority=8,  # Add an inner tag and insert this value as the priority
+                    treatment_inner_vid=self._set_vlan_id,  # use this value as the VID in the inner VLAN tag
+                    treatment_inner_tpid_de=4,  # set TPID
+                )
+            )
+            msg = ExtendedVlanTaggingOperationConfigurationDataFrame(
+                _mac_bridge_service_profile_entity_id + self._uni_port.mac_bridge_port_num,  # Bridge Entity ID
+                attributes=attributes  # See above
+            )
+            frame = msg.set()
+            self.log.debug('openomci-msg', omci_msg=msg)
+            self.strobe_watchdog()
+            results = yield self._device.omci_cc.send(frame)
+            self.check_status_and_state(results,
+                                        'flow-set-ext-vlan-tagging-op-config-data-zero-tagged')
+
+            self.deferred.callback(self)
+
+        except Exception as e:
+            self.log.exception('setting-vlan-tagging', e=e)
+            self.deferred.errback(failure.Failure(e))
+
+    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)
+        """
+
+        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: %s", 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