Microsemi adapter provisions default rule.

Change-Id: I5cb249c4f7277dbd2e3fc303f2fe40442fcb6608
diff --git a/voltha/adapters/microsemi_olt/ActivationWatcher.py b/voltha/adapters/microsemi_olt/ActivationWatcher.py
index 7fe30cd..ee0e309 100644
--- a/voltha/adapters/microsemi_olt/ActivationWatcher.py
+++ b/voltha/adapters/microsemi_olt/ActivationWatcher.py
@@ -13,12 +13,25 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 #
+from struct import pack, unpack
+
 from scapy.automaton import ATMT
 import structlog
 from voltha.adapters.microsemi_olt.BaseOltAutomaton import BaseOltAutomaton
 from voltha.adapters.microsemi_olt.PAS5211 import PAS5211EventOnuActivation, PAS5211MsgGetActivationAuthMode, \
-    PAS5211MsgGetActivationAuthModeResponse, PON_ACTIVATION_AUTH_AUTO, PON_ENABLE, PAS5211MsgSetOnuOmciPortId, \
-    PAS5211MsgSetOnuOmciPortIdResponse, PAS5211MsgSendFrame, PON_PORT_PON, PAS5211MsgSendFrameResponse
+    PAS5211MsgGetActivationAuthModeResponse, PAS5211MsgSetOnuOmciPortId, \
+    PAS5211MsgSetOnuOmciPortIdResponse, PAS5211MsgSendFrame, PAS5211MsgSendFrameResponse, \
+    PAS5211MsgGetLogicalObjectStatus, PAS5211MsgGetLogicalObjectStatusResponse, PAS5211MsgSetOnuAllocId, \
+    PAS5211MsgGetDbaMode, PAS5211MsgGetDbaModeResponse, PAS5211MsgSendDbaAlgorithmMsg, \
+    PAS5211MsgSendDbaAlgorithmMsgResponse, PAS5211EventDbaAlgorithm, PAS5211MsgSetPortIdConfig, \
+    PAS5211MsgSetPortIdConfigResponse, PAS5211MsgGetOnuIdByPortId, PAS5211MsgGetOnuIdByPortIdResponse, \
+    PAS5211SetVlanUplinkConfiguration, PAS5211SetVlanUplinkConfigurationResponse, PAS5211MsgSetOnuAllocIdResponse
+from voltha.adapters.microsemi_olt.PAS5211_constants import PON_ACTIVATION_AUTH_AUTO, PON_ENABLE, PON_PORT_PON, \
+    PON_LOGICAL_OBJECT_TYPE_ALLOC_ID, PON_LOGICAL_OBJECT_TYPE_ONU_ID_BY_ALLOC_ID, PON_TRUE, \
+    PMC_OFAL_MAX_BI_DIRECTIONAL_FLOW_PER_ONU, PMC_OFAL_START_FLOW_ID_BASE, PON_DBA_MODE_RUNNING, \
+    PYTHAGORAS_UPDATE_AID_SLA, SLA_be_bw_gros, SLA_gr_bw_gros, SLA_gr_bw_fine, SLA_be_bw_fine, PYTHAGORAS_DBA_DATA_COS, \
+    PYTHAGORAS_DBA_STATUS_REPORT_NSR, PYTHAGORAS_SET_SLA_RESP_SIZE, PON_PORT_TYPE_GEM, PON_PORT_DESTINATION_CNI0, \
+    PON_FALSE, PON_DISABLE
 from voltha.extensions.omci.omci_entities import CircuitPack
 from voltha.extensions.omci.omci_frame import OmciFrame
 from voltha.extensions.omci.omci_messages import OmciGet, OmciGetResponse
@@ -29,6 +42,14 @@
 log = structlog.get_logger()
 _verbose = False
 
+ALLOC_ID = 1000
+
+def alloc_id(onu_id):
+    for i in range(0, PMC_OFAL_MAX_BI_DIRECTIONAL_FLOW_PER_ONU):
+        alloc_id = PMC_OFAL_START_FLOW_ID_BASE + \
+                   (onu_id * PMC_OFAL_MAX_BI_DIRECTIONAL_FLOW_PER_ONU) + i
+        yield alloc_id
+
 def hexstring(string):
     return ":".join("{:02x}".format(ord(c)) for c in string)
 
@@ -39,6 +60,8 @@
     onu_session_id = None
     port_id = None
     channel_id = None
+    alloc_id = None
+    vendor = None
 
     def parse_args(self, debug=0, store=0, **kwargs):
         self.onu_id = kwargs.pop('onu_id')
@@ -46,6 +69,7 @@
         self.onu_session_id = kwargs.pop('onu_session_id')
         self.port_id = self.onu_id
         self.channel_id = kwargs.pop('channel_id')
+        self.alloc_id = alloc_id(self.onu_id)
 
         if self.onu_id is None or self.serial_number is None or \
                 self.onu_session_id is None or self.channel_id is None:
@@ -85,13 +109,45 @@
     def wait_omci_get(self):
         pass
 
+    @ATMT.state()
+    def wait_logical_object_status(self):
+        pass
+
+    @ATMT.state()
+    def wait_set_alloc_id(self):
+        pass
+
+    @ATMT.state()
+    def wait_dba_mode(self):
+        pass
+
+    @ATMT.state()
+    def wait_send_dba_alg_msg(self):
+        pass
+
+    @ATMT.state()
+    def wait_dba_alg_event(self):
+        pass
+
+    @ATMT.state()
+    def wait_set_port_id_config(self):
+        pass
+
+    @ATMT.state()
+    def wait_get_onu_id_by_port_id(self):
+        pass
+
+    @ATMT.state()
+    def wait_set_vlan_uplink_config(self):
+        pass
+
     @ATMT.state(final=1)
     def end(self):
         pass
 
     @ATMT.state(error=1)
-    def error(self):
-        pass
+    def error(self, msg):
+        log.error(msg)
 
     """
     Utility Methods
@@ -114,6 +170,21 @@
         log.error(msg)
         raise self.error()
 
+    def detect_onu(self):
+        log.info("Activated {} ONT".format(self.vendor))
+        self.create_port(self.vendor)
+        print self.channel_id
+        print self.onu_id
+        try:
+            self.device.onu_detected(
+                parent_port_no=self.channel_id,
+                child_device_type='%s_onu' % self.vendor.lower(),
+                onu_id=self.onu_id,
+                serial_number=hexstring(self.serial_number)
+            )
+        except Exception as e:
+            print e
+
     """
     Transitions
     """
@@ -128,7 +199,7 @@
     # Transitions from wait_get_auth_mode
     @ATMT.timeout(wait_get_auth_mode, 3)
     def timeout_get_auth_mode(self):
-        self.error('Could not get auth mode for OLT {}; dropping activation event for {}'
+        raise self.error('Could not get auth mode for OLT {}; dropping activation event for {}'
                    .format(self.target, hexstring(self.serial_number)))
 
     @ATMT.receive_condition(wait_get_auth_mode)
@@ -152,7 +223,7 @@
     # Transitions from wait_omci_port_id
     @ATMT.timeout(wait_omci_port_id, 3)
     def timeout_omci_port_id(self):
-        self.error('Could not set omci port id for OLT {}; dropping activation event for {}'
+        raise self.error('Could not set omci port id for OLT {}; dropping activation event for {}'
                    .format(self.target, hexstring(self.serial_number)))
 
     @ATMT.receive_condition(wait_omci_port_id)
@@ -180,6 +251,7 @@
         omci_frame = PAS5211MsgSendFrame(port_type=PON_PORT_PON, port_id=self.port_id,
                                          management_frame=PON_ENABLE, frame=frame)
 
+
         self.send(self.px(omci_frame))
 
         raise self.wait_send_frame()
@@ -187,7 +259,7 @@
     # Transitions from wait_send_frame
     @ATMT.timeout(wait_send_frame, 3)
     def timeout_send_frame(self):
-        self.error('Could not send omci to OLT {}; dropping activation event for {}'
+        raise self.error('Could not send omci to OLT {}; dropping activation event for {}'
                    .format(self.target, hexstring(self.serial_number)))
 
     @ATMT.receive_condition(wait_send_frame)
@@ -198,22 +270,180 @@
     # Transitions from wait_omci_get
     @ATMT.timeout(wait_omci_get, 3)
     def timeout_send_frame(self):
-        self.error('Did not receive omci get event from OLT {}; dropping activation event for {}'
+        raise self.error('Did not receive omci get event from OLT {}; dropping activation event for {}'
                    .format(self.target, hexstring(self.serial_number)))
 
     @ATMT.receive_condition(wait_omci_get)
     def wait_for_omci_get(self, pkt):
         if OmciGetResponse in pkt:
-            vendor = pkt['OmciGetResponse'].data['vendor_id']
-            log.info("Activated {} ONT".format(vendor))
-            self.create_port(vendor)
+            self.allocId = self.alloc_id.next()
+            self.vendor = pkt['OmciGetResponse'].data['vendor_id']
+            l_obj_status = PAS5211MsgGetLogicalObjectStatus(
+                            type=PON_LOGICAL_OBJECT_TYPE_ALLOC_ID,
+                            value=self.allocId)
+            self.send(self.px(l_obj_status))
+            raise self.wait_logical_object_status()
 
-            self.device.onu_detected(
-                parent_port_no=self.channel_id,
-                child_device_type='%s_onu' % vendor.lower(),
-                onu_id=self.port_id,
+    # Transitions from wait_logical_object_status
+    @ATMT.timeout(wait_logical_object_status, 3)
+    def timeout_logical_object_status(self):
+        raise self.error('Did not receive info about alloc id status for {}; dropping activation event for {}'
+                   .format(self.target, hexstring(self.serial_number)))
+
+    @ATMT.receive_condition(wait_logical_object_status)
+    def wait_for_logical_object_status(self, pkt):
+        if PAS5211MsgGetLogicalObjectStatusResponse in pkt:
+            if pkt.type == PON_LOGICAL_OBJECT_TYPE_ALLOC_ID:
+                if pkt.return_value == 0:
+                    # alloc-id not set
+                    set_alloc_id = PAS5211MsgSetOnuAllocId(
+                                    alloc_id=self.allocId,
+                                    allocate=PON_ENABLE
+                                )
+                    self.onu_id = -1
+                    self.port_id = self.allocId
+                    self.send(self.px(set_alloc_id))
+                    raise self.wait_set_alloc_id()
+                else:
+                    l_obj_status = PAS5211MsgGetLogicalObjectStatus(
+                        type=PON_LOGICAL_OBJECT_TYPE_ONU_ID_BY_ALLOC_ID,
+                        value=self.allocId)
+                    self.send(self.px(l_obj_status))
+                    raise self.wait_logical_object_status()
+            elif pkt.type == PON_LOGICAL_OBJECT_TYPE_ONU_ID_BY_ALLOC_ID:
+                # That's your onu id.
+                self.onu_id = pkt.return_value
+                # FIXME Need to iterate to get the port id as
+                # in PMC_OFAL_flow_db.c line 656
+                # UPDATE PORT_ID
+                set_alloc_id = PAS5211MsgSetOnuAllocId(
+                    alloc_id=self.allocId,
+                    allocate=PON_ENABLE
+                )
+                self.send(self.px(set_alloc_id))
+                raise self.wait_for_set_alloc_id() #FIXME are we done? probably not but check
+
+    # Transitions from wait_set_alloc_id
+    @ATMT.timeout(wait_set_alloc_id, 3)
+    def timeout_set_alloc_id(self):
+        raise self.error('Was not able to set alloc id for {}; dropping activation event for {}'
+                   .format(self.target, hexstring(self.serial_number)))
+
+    @ATMT.receive_condition(wait_set_alloc_id)
+    def wait_for_set_alloc_id(self, pkt):
+        if PAS5211MsgSetOnuAllocIdResponse in pkt:
+            self.send(self.px(PAS5211MsgGetDbaMode()))
+            raise self.wait_dba_mode()
+
+    # Transitions from wait for dba mode (See Pythagoras_api.c line 344 & PMC_OFAL.c 2062)
+    @ATMT.timeout(wait_dba_mode, 3)
+    def timeout_wait_dba_mode(self):
+        raise self.error('Did not get DBA mode for {}; dropping activation event for {}'
+                   .format(self.target, hexstring(self.serial_number)))
+
+
+    @ATMT.receive_condition(wait_dba_mode)
+    def wait_for_dba_mode(self, pkt):
+        if PAS5211MsgGetDbaModeResponse in pkt:
+            if pkt.dba_mode != PON_DBA_MODE_RUNNING:
+                raise self.error('DBA is not running; dropping activation event for {}'
+                           .format(hexstring(self.serial_number)))
+
+            data = pack('<LLHHBBBB', PYTHAGORAS_UPDATE_AID_SLA,
+                        self.allocId, SLA_gr_bw_gros, SLA_be_bw_gros,
+                        SLA_gr_bw_fine, SLA_be_bw_fine, PYTHAGORAS_DBA_DATA_COS,
+                        PYTHAGORAS_DBA_STATUS_REPORT_NSR)
+
+            send_dba_alg = PAS5211MsgSendDbaAlgorithmMsg(data=data)
+            self.send(self.px(send_dba_alg))
+            raise self.wait_send_dba_alg_msg()
+
+    # Transitions from wait_send_dba_alg_msg
+    @ATMT.timeout(wait_send_dba_alg_msg, 3)
+    def timeout_wait_for_send_dba_alg_msg(self):
+        raise self.error('Unable to set dba alg params for {}; dropping activation event for {}'
+                         .format(self.target, hexstring(self.serial_number)))
+
+    @ATMT.receive_condition(wait_send_dba_alg_msg)
+    def wait_for_send_dba_alg_msg(self, pkt):
+        if PAS5211MsgSendDbaAlgorithmMsgResponse in pkt:
+            raise self.wait_dba_alg_event()
+
+    # Transitions from wait_dba_alg_event
+    @ATMT.timeout(wait_dba_alg_event, 3)
+    def timeout_wait_for_send_dba_alg_event(self):
+        raise self.error('DBA params ont set for {}; dropping activation event for {}'
+                         .format(self.target, hexstring(self.serial_number)))
+
+    @ATMT.receive_condition(wait_dba_alg_event)
+    def wait_for_send_dba_alg_event(self, pkt):
+        if PAS5211EventDbaAlgorithm in pkt:
+            if pkt.size < PYTHAGORAS_SET_SLA_RESP_SIZE:
+                raise self.error('DBA Event message too small for {}, dropping activation event for {}'
+                                 .format(self.target, hexstring(self.serial_number)))
+
+            (_, aid, _) = unpack('<LLH',pkt.data)
+            if aid == self.allocId:
+                # All is well moving on.
+                # There is some more shit at PYTHAGORAS.c line 395 but fuck it.
+                set_port_id_config = PAS5211MsgSetPortIdConfig(
+                    port_id=self.port_id,
+                    activate=PON_ENABLE,
+                    alloc_id=self.allocId,
+                    type=PON_PORT_TYPE_GEM,
+                    destination=PON_PORT_DESTINATION_CNI0
+                )
+                self.send(self.px(set_port_id_config))
+                raise self.wait_set_port_id_config()
+
+    # Transitions from wait_set_port_id_config
+    @ATMT.timeout(wait_set_port_id_config, 3)
+    def timeout_wait_set_port_id_config(self):
+        raise self.error('Could not set port id config for {}; dropping activation event for {}'
+                         .format(self.target, hexstring(self.serial_number)))
+
+    @ATMT.receive_condition(wait_set_port_id_config)
+    def wait_for_set_port_id_config(self, pkt):
+        if PAS5211MsgSetPortIdConfigResponse in pkt:
+            get_onu_id = PAS5211MsgGetOnuIdByPortId(
+                port_id=self.port_id
             )
+            self.send(self.px(get_onu_id))
+            raise self.wait_get_onu_id_by_port_id()
 
+    # Transistions from wait_get_onu_id_by_port_id
+    @ATMT.timeout(wait_get_onu_id_by_port_id, 3)
+    def timeout_wait_get_onu_id_by_port_id(self):
+        raise self.error('Could not get onu id for {}; dropping activation event for {}'
+                         .format(self.target, hexstring(self.serial_number)))
+
+    @ATMT.receive_condition(wait_get_onu_id_by_port_id)
+    def wait_for_get_onu_id_by_port_id(self, pkt):
+        if PAS5211MsgGetOnuIdByPortIdResponse in pkt:
+            self.onu_id = pkt['PAS5211MsgGetOnuIdByPortIdResponse'].onu_id
+            # There may be more things to do here. but traces indicate that no.
+            # see PAS.c line 977 and onwards.
+            set_vlan_uplink_config = PAS5211SetVlanUplinkConfiguration(
+                port_id=self.port_id,
+                pvid_config_enabled=PON_FALSE,
+                min_cos=0,
+                max_cos=7,
+                de_bit=PON_DISABLE
+            )
+            self.send(self.px(set_vlan_uplink_config))
+            raise self.wait_set_vlan_uplink_config()
+
+    # Transitions from wait_set_vlan_uplink_config
+    @ATMT.timeout(wait_set_vlan_uplink_config, 3)
+    def timeout_wait_set_vlan_uplink_config(self):
+        raise self.error('Could not set vlan uplink config for {}; dropping activation event for {}'
+                                 .format(self.target, hexstring(self.serial_number)))
+
+    @ATMT.receive_condition(wait_set_vlan_uplink_config)
+    def wait_for_set_vlan_uplink_config(self, pkt):
+        if PAS5211SetVlanUplinkConfigurationResponse in pkt:
+            # YAY we made it.
+            self.detect_onu()
             raise self.end()
 
 
diff --git a/voltha/adapters/microsemi_olt/DeviceManager.py b/voltha/adapters/microsemi_olt/DeviceManager.py
index ea2b607..e468a0d 100644
--- a/voltha/adapters/microsemi_olt/DeviceManager.py
+++ b/voltha/adapters/microsemi_olt/DeviceManager.py
@@ -104,11 +104,13 @@
 
     def onu_detected(self, parent_port_no=None,
                         child_device_type=None,
-                        onu_id=None):
+                        onu_id=None,
+                        serial_number=None):
         self.adapter_agent.child_device_detected(
             parent_device_id=self.device.id,
             parent_port_no=parent_port_no,
             child_device_type=child_device_type,
+            serial_number=serial_number,
             proxy_address=Device.ProxyAddress(
                 device_id=self.device.id,
                 channel_id=onu_id
diff --git a/voltha/adapters/microsemi_olt/OltStateMachine.py b/voltha/adapters/microsemi_olt/OltStateMachine.py
index 3c77415..ff61817 100644
--- a/voltha/adapters/microsemi_olt/OltStateMachine.py
+++ b/voltha/adapters/microsemi_olt/OltStateMachine.py
@@ -19,13 +19,14 @@
 from voltha.adapters.microsemi_olt.PAS5211 import PAS5211MsgGetProtocolVersion, PAS5211MsgGetOltVersion, \
     PAS5211MsgGetOltVersionResponse, PAS5211MsgGetProtocolVersionResponse, \
     SnrBurstDelay, RngBurstDelay, GeneralOpticsParams, ResetValues, ResetTimingCtrl, PreambleParams, \
-    PAS5211MsgSetOltOpticsResponse, CHANNELS, PON_OPTICS_VOLTAGE_IF_LVPECL, PON_ENABLE, PON_POLARITY_ACTIVE_HIGH, \
-    PON_SD_SOURCE_LASER_SD, PON_RESET_TYPE_DELAY_BASED, PON_DISABLE, PON_RESET_TYPE_NORMAL_START_BURST_BASED, \
-    PAS5211MsgSetOpticsIoControlResponse, PON_TX_ENABLE_DEFAULT, PAS5211MsgGetGeneralParamResponse, PAS5211MsgAddOltChannel, \
-    PAS5211MsgAddOltChannelResponse, PON_ALARM_LOS, PAS5211MsgSetAlarmConfigResponse, PAS5211MsgGetDbaMode, \
-    PAS5211MsgGetDbaModeResponse, PON_DBA_MODE_LOADED_NOT_RUNNING, PAS5211MsgStartDbaAlgorithm, \
-    PAS5211MsgStartDbaAlgorithmResponse, PON_DBA_MODE_RUNNING, PAS5211MsgSetOltChannelActivationPeriod, \
+    PAS5211MsgSetOltOpticsResponse, CHANNELS, PAS5211MsgSetOpticsIoControlResponse, PAS5211MsgGetGeneralParamResponse, PAS5211MsgAddOltChannel, \
+    PAS5211MsgAddOltChannelResponse, PAS5211MsgSetAlarmConfigResponse, PAS5211MsgGetDbaMode, \
+    PAS5211MsgGetDbaModeResponse, PAS5211MsgStartDbaAlgorithm, \
+    PAS5211MsgStartDbaAlgorithmResponse,  PAS5211MsgSetOltChannelActivationPeriod, \
     PAS5211MsgSetOltChannelActivationPeriodResponse
+from voltha.adapters.microsemi_olt.PAS5211_constants import PON_OPTICS_VOLTAGE_IF_LVPECL, PON_ENABLE, PON_POLARITY_ACTIVE_HIGH, \
+    PON_SD_SOURCE_LASER_SD, PON_RESET_TYPE_DELAY_BASED, PON_DISABLE, PON_RESET_TYPE_NORMAL_START_BURST_BASED, \
+    PON_TX_ENABLE_DEFAULT, PON_ALARM_LOS,  PON_DBA_MODE_LOADED_NOT_RUNNING,  PON_DBA_MODE_RUNNING
 from voltha.adapters.microsemi_olt.PAS5211_utils import general_param, olt_optics_pkt, burst_timing, io_ctrl_optics, \
     alarm_config
 
diff --git a/voltha/adapters/microsemi_olt/PAS5211.py b/voltha/adapters/microsemi_olt/PAS5211.py
index e564a99..d6cb387 100644
--- a/voltha/adapters/microsemi_olt/PAS5211.py
+++ b/voltha/adapters/microsemi_olt/PAS5211.py
@@ -22,12 +22,14 @@
 
 
 from scapy.fields import LEShortField, Field, LEIntField, LESignedIntField, FieldLenField, FieldListField, PacketField, \
-    ByteField, StrFixedLenField, ConditionalField, StrField, MACField, LELongField, LenField
+    ByteField, StrFixedLenField, ConditionalField, StrField, MACField, LELongField, LenField, StrLenField
 from scapy.layers.l2 import DestMACField, ETHER_ANY, Ether
 from scapy.packet import Packet, bind_layers
 from scapy.utils import lhex
 from scapy.volatile import RandSInt
 from scapy.layers.ntp import XLEShortField
+
+from voltha.adapters.microsemi_olt.PAS5211_constants import PON_ENABLE, PON_PORT_PON, PON_FALSE, PON_TRUE
 from voltha.extensions.omci.omci_frame import OmciFrame
 
 """
@@ -36,174 +38,6 @@
 #TODO get range from olt_version message
 CHANNELS=range(0,4)
 
-# from enum PON_true_false_t
-PON_FALSE = 0
-PON_TRUE = 1
-
-# from enum PON_enable_disable_t
-PON_DISABLE = 0
-PON_ENABLE = 1
-
-# from enym PON_mac_t
-PON_MII = 0
-PON_GMII = 1
-PON_TBI = 2
-
-PON_POLARITY_ACTIVE_LOW = 0
-PON_POLARITY_ACTIVE_HIGH = 1
-
-PON_OPTICS_VOLTAGE_IF_UNDEFINED = 0
-PON_OPTICS_VOLTAGE_IF_CML = 1
-PON_OPTICS_VOLTAGE_IF_LVPECL = 2
-
-PON_SD_SOURCE_LASER_SD = 0
-PON_SD_SOURCE_BCDR_LOCK = 1
-PON_SD_SOURCE_BCDR_SD = 2
-
-PON_RESET_TYPE_DELAY_BASED = 0
-PON_RESET_TYPE_SINGLE_RESET = 1
-PON_RESET_TYPE_DOUBLE_RESET = 2
-
-PON_RESET_TYPE_NORMAL_START_BURST_BASED = 0
-PON_RESET_TYPE_NORMAL_END_BURST_BASED = 1
-
-PON_GPIO_LINE_0 = 0
-PON_GPIO_LINE_1 = 1
-PON_GPIO_LINE_2 = 2
-PON_GPIO_LINE_3 = 3
-PON_GPIO_LINE_4 = 4
-PON_GPIO_LINE_5 = 5
-PON_GPIO_LINE_6 = 6
-PON_GPIO_LINE_7 = 7
-def PON_EXT_GPIO_LINE(line):
-    return line + 8
-
-# from enum PON_alarm_t
-PON_ALARM_SOFTWARE_ERROR = 0
-PON_ALARM_LOS = 1
-PON_ALARM_LOSI = 2
-PON_ALARM_DOWI = 3
-PON_ALARM_LOFI = 4
-PON_ALARM_RDII = 5
-PON_ALARM_LOAMI = 6
-PON_ALARM_LCDGI = 7
-PON_ALARM_LOAI = 8
-PON_ALARM_SDI = 9
-PON_ALARM_SFI = 10
-PON_ALARM_PEE = 11
-PON_ALARM_DGI = 12
-PON_ALARM_LOKI = 13
-PON_ALARM_TIWI = 14
-PON_ALARM_TIA = 15
-PON_ALARM_VIRTUAL_SCOPE_ONU_LASER_ALWAYS_ON = 16
-PON_ALARM_VIRTUAL_SCOPE_ONU_SIGNAL_DEGRADATION = 17
-PON_ALARM_VIRTUAL_SCOPE_ONU_EOL = 18
-PON_ALARM_VIRTUAL_SCOPE_ONU_EOL_DATABASE_IS_FULL = 19
-PON_ALARM_AUTH_FAILED_IN_REGISTRATION_ID_MODE = 20
-PON_ALARM_SUFI = 21
-PON_ALARM_LAST_ALARM = 22
-
-# from enum PON_general_parameters_type_t
-PON_COMBINED_LOSI_LOFI 		            = 1000
-PON_TX_ENABLE_DEFAULT 		            = 1001
-
-# Enable or disable False queue full event from DBA
-PON_FALSE_Q_FULL_EVENT_MODE             = 1002
-
-# Set PID_AID_MISMATCH min silence period. 0 - disable, Else - period in secs
-PON_PID_AID_MISMATCH_MIN_SILENCE_PERIOD = 1003
-
-# Set if FW generate clear alarm. 0 - generate clear alarm, Else - don't
-# generate clear alarm
-PON_ENABLE_CLEAR_ALARM                  = 1004
-
-# Enable or disabl send assign alloc id ploam. 0 - disable, 1 - enable
-PON_ASSIGN_ALLOC_ID_PLOAM               = 1005
-
-# BIP error polling period, 200 - 65000, 0 - Disabled, Recommended: 5000
-# (default)
-PON_BIP_ERR_POLLING_PERIOD_MS           = 1006
-
-# Ignore SN when decatived 0 - consider SN (deactivate the onu if received
-# same SN when activated (default)	1 - Ignore
-PON_IGNORE_SN_WHEN_ACTIVE		        = 1007
-
-# 0xffffffff - Disabled (default). Any other value (0 - 0xfffe) indicates
-# that PA delay is enabled, with the specified delay value and included in
-# the US_OVERHEAD PLOAM
-PON_ONU_PRE_ASSIGNED_DELAY		        = 1008
-
-# Enable or disable DS fragmentation, 0 disable, 1 enable
-PON_DS_FRAGMENTATION			        = 1009
-
-# Set if fw report rei alarm when errors is 0, 0 disable (default), 1 enable
-PON_REI_ERRORS_REPORT_ALL		        = 1010
-
-# Set if igonre sfi deactivation, 0 disable (default), 1 enable
-PON_IGNORE_SFI_DEACTIVATION		        = 1011
-
-# Allows to override the allocation overhead set by optic-params
-# configuration. This configuration is only allowed when the the pon channel
-# is disabled
-PON_OVERRIDE_ALLOCATION_OVERHEAD	    = 1012
-
-# Optics timeline offset, -128-127, : this parameter is very sensitive and
-# requires coordination with PMC
-PON_OPTICS_TIMELINE_OFFSET	            = 1013
-
-# Last general meter
-PON_LAST_GENERAL_PARAMETER		        = PON_OPTICS_TIMELINE_OFFSET
-
-# from enum PON_dba_mode_t
-PON_DBA_MODE_NOT_LOADED                 = 0
-PON_DBA_MODE_LOADED_NOT_RUNNING         = 1
-PON_DBA_MODE_RUNNING                    = 2
-PON_DBA_MODE_LAST                       = 3
-
-# from enum type typedef enum PON_port_frame_destination_t
-PON_PORT_PON = 0
-PON_PORT_SYSTEM = 1
-
-# from enum PON_olt_hw_classification_t
-
-PON_OLT_HW_CLASSIFICATION_PAUSE                    = 0
-PON_OLT_HW_CLASSIFICATION_LINK_CONSTRAINT          = 1
-PON_OLT_HW_CLASSIFICATION_IGMP                     = 2
-PON_OLT_HW_CLASSIFICATION_MPCP                     = 3
-PON_OLT_HW_CLASSIFICATION_OAM                      = 4
-PON_OLT_HW_CLASSIFICATION_802_1X                   = 5
-PON_OLT_HW_CLASSIFICATION_PPPOE_DISCOVERY          = 6
-PON_OLT_HW_CLASSIFICATION_PPPOE_SESSION            = 7
-PON_OLT_HW_CLASSIFICATION_DHCP_V4                  = 8
-PON_OLT_HW_CLASSIFICATION_PIM                      = 9
-PON_OLT_HW_CLASSIFICATION_DHCP_V6                  = 10
-PON_OLT_HW_CLASSIFICATION_ICMP_V4                  = 11
-PON_OLT_HW_CLASSIFICATION_MLD                      = 12
-PON_OLT_HW_CLASSIFICATION_ARP                      = 13
-PON_OLT_HW_CLASSIFICATION_CONF_DA                  = 14
-PON_OLT_HW_CLASSIFICATION_CONF_RULE                = 15
-PON_OLT_HW_CLASSIFICATION_DA_EQ_SA                 = 16
-PON_OLT_HW_CLASSIFICATION_DA_EQ_MAC                = 17
-PON_OLT_HW_CLASSIFICATION_DA_EQ_SEC_MAC            = 18
-PON_OLT_HW_CLASSIFICATION_SA_EQ_MAC                = 19
-PON_OLT_HW_CLASSIFICATION_SA_EQ_SEC_MAC            = 20
-PON_OLT_HW_CLASSIFICATION_ETHERNET_MANAGEMENT      = 100
-PON_OLT_HW_CLASSIFICATION_IPV4_LOCAL_MULTICAST     = 101
-PON_OLT_HW_CLASSIFICATION_IPV4_MANAGEMENT          = 102
-PON_OLT_HW_CLASSIFICATION_ALL_IPV4_MULTICAST       = 103
-PON_OLT_HW_CLASSIFICATION_IPV6_LOCAL_MULTICAST     = 104
-PON_OLT_HW_CLASSIFICATION_IPV6_MANAGEMENT          = 105
-PON_OLT_HW_CLASSIFICATION_ALL_IPV6_MULTICAST       = 106
-PON_OLT_HW_CLASSIFICATION_OTHER			           = 107
-PON_OLT_HW_CLASSIFICATION_LAST_RULE                = 108
-
-
-PON_ACTIVATION_AUTH_AUTO 			  				= 0
-PON_ACTIVATION_AUTH_HOST_CONTROLLED_SEPARATE_EVENTS = 1 # Host controlled: Separate events
-PON_ACTIVATION_AUTH_HOST_CONTROLLED_LUMPED_SN   	= 2 # Host controlled: Lumped-SN-Response
-PON_ACTIVATION_AUTH_REGISTRATION_ID_RAW   			= 3 # Registration-ID Raw
-PON_ACTIVATION_AUTH_REGISTRATION_ID_LEARN   		= 4  # Registration-ID Learn
-
 
 class XLESignedIntField(Field):
     def __init__(self, name, default):
@@ -641,14 +475,14 @@
     opcode = 47
     name = "PAS5211MsgSendDbaAlgorithmMsg"
     fields_desc = [
-        LEShortField("id", None),
-        LEShortField("size", None),
-        ByteField("data", None)
+        #LEShortField("id", None),
+        FieldLenField("size", None, fmt="<H", length_of="data"),
+        StrLenField("data", "", length_from=lambda x:x.size)
     ]
 
 class PAS5211MsgSendDbaAlgorithmMsgResponse(PAS5211Msg):
     opcode = 10287
-    name = "PAS5211MsgSendDbaAlgorithmMsgReponse"
+    name = "PAS5211MsgSendDbaAlgorithmMsgResponse"
     fields_desc = []
 
 class PAS5211MsgSetPortIdConfig(PAS5211Msg):
@@ -675,7 +509,7 @@
     name = "PAS5211MsgGetOnuIdByPortId"
     fields_desc = [
         LEShortField("port_id", None),
-        LEShortField("reserved", None)
+        LEShortField("reserved", 0)
     ]
 
 
@@ -693,11 +527,11 @@
     name = "PAS5211SetVlanUplinkConfiguration"
     fields_desc = [
         LEShortField("port_id", None),
-        LEIntField("pvid_config_enabled", None),
+        LEShortField("pvid_config_enabled", None),
         LEShortField("min_cos", None),
         LEShortField("max_cos", None),
         LEIntField("de_bit", None),
-        LEShortField("reserved", None)
+        LEShortField("reserved", 0)
     ]
 
 
@@ -812,8 +646,8 @@
 class PAS5211EventDbaAlgorithm(PAS5211Event):
     name = "PAS5211EventDbaAlgorithm"
     fields_desc = [
-        LEShortField("size", None),
-        ByteField("event_buffer", None) # According to Pythagoras_API.c line 264. Am I certain? No.
+        FieldLenField("size", None, fmt="<H", length_of="data"),
+        StrLenField("data", "", length_from=lambda x: x.size)
     ]
 
 
diff --git a/voltha/adapters/microsemi_olt/PAS5211_constants.py b/voltha/adapters/microsemi_olt/PAS5211_constants.py
index 0c9c866..47db0b7 100644
--- a/voltha/adapters/microsemi_olt/PAS5211_constants.py
+++ b/voltha/adapters/microsemi_olt/PAS5211_constants.py
@@ -146,6 +146,15 @@
 PON_PORT_PON = 0
 PON_PORT_SYSTEM = 1
 
+PON_PORT_TYPE_GEM  = 0
+PON_PORT_TYPE_TDM  = 1
+PON_PORT_TYPE_OMCI = 2
+
+PON_PORT_DESTINATION_CNI0 = 0
+PON_PORT_DESTINATION_CNI1 = 1
+PON_PORT_DESTINATION_CNI2 = 2
+PON_PORT_DESTINATION_CPU  = 3
+
 # from enum PON_olt_hw_classification_t
 
 PON_OLT_HW_CLASSIFICATION_PAUSE                    = 0
@@ -178,3 +187,34 @@
 PON_OLT_HW_CLASSIFICATION_ALL_IPV6_MULTICAST       = 106
 PON_OLT_HW_CLASSIFICATION_OTHER			           = 107
 PON_OLT_HW_CLASSIFICATION_LAST_RULE                = 108
+
+PON_ACTIVATION_AUTH_AUTO 			  				= 0
+PON_ACTIVATION_AUTH_HOST_CONTROLLED_SEPARATE_EVENTS = 1 # Host controlled: Separate events
+PON_ACTIVATION_AUTH_HOST_CONTROLLED_LUMPED_SN   	= 2 # Host controlled: Lumped-SN-Response
+PON_ACTIVATION_AUTH_REGISTRATION_ID_RAW   			= 3 # Registration-ID Raw
+PON_ACTIVATION_AUTH_REGISTRATION_ID_LEARN   		= 4  # Registration-ID Learn
+
+PON_LOGICAL_OBJECT_TYPE_ALLOC_ID	        = 0
+PON_LOGICAL_OBJECT_TYPE_ONU_ID_BY_ALLOC_ID  = 1
+PON_LOGICAL_OBJECT_TYPE_MAX			        = PON_LOGICAL_OBJECT_TYPE_ONU_ID_BY_ALLOC_ID
+
+
+PMC_OFAL_MAX_BI_DIRECTIONAL_FLOW_PER_ONU    = 16
+PMC_OFAL_START_FLOW_ID_BASE                 = 1000
+
+# SLA
+
+PYTHAGORAS_UPDATE_AID_SLA                   = 21
+
+PYTHAGORAS_DBA_DATA_COS                     = 0
+PYTHAGORAS_DBA_STATUS_REPORT_NSR            = 0
+
+PYTHAGORAS_SET_SLA_RESP_SIZE                = 10
+
+# Random values found in PMC code
+SLA_gr_bw_gros = 1;
+SLA_be_bw_gros = 10;
+SLA_gr_bw_fine = 0;
+SLA_be_bw_fine = 0
+
+
diff --git a/voltha/adapters/microsemi_olt/PAS5211_utils.py b/voltha/adapters/microsemi_olt/PAS5211_utils.py
index 709af3f..2437be5 100644
--- a/voltha/adapters/microsemi_olt/PAS5211_utils.py
+++ b/voltha/adapters/microsemi_olt/PAS5211_utils.py
@@ -14,11 +14,12 @@
 # limitations under the License.
 #
 
-from voltha.adapters.microsemi_olt.PAS5211 import PAS5211MsgGetGeneralParam, PON_POLARITY_ACTIVE_LOW, \
-    PON_POLARITY_ACTIVE_HIGH, PAS5211MsgSetOpticsIoControl, BurstTimingCtrl, PAS5211MsgSetOltOptics, PON_ALARM_LOS, \
+from voltha.adapters.microsemi_olt.PAS5211 import PAS5211MsgGetGeneralParam, PAS5211MsgSetOpticsIoControl, \
+    BurstTimingCtrl, PAS5211MsgSetOltOptics, PAS5211MsgSetAlarmConfig
+from voltha.adapters.microsemi_olt.PAS5211_constants import PON_POLARITY_ACTIVE_LOW, \
+    PON_POLARITY_ACTIVE_HIGH, PON_ALARM_LOS, \
     PON_ALARM_LOSI, PON_ALARM_DOWI, PON_ALARM_LOFI, PON_ALARM_RDII, PON_ALARM_LOAMI, PON_ALARM_LCDGI, PON_ALARM_LOAI, \
-    PON_ALARM_SDI, PON_ALARM_SFI, PON_ALARM_PEE, PON_ALARM_DGI, PON_ALARM_LOKI, PON_ALARM_TIWI, PON_ALARM_TIA, \
-    PAS5211MsgSetAlarmConfig
+    PON_ALARM_SDI, PON_ALARM_SFI, PON_ALARM_PEE, PON_ALARM_DGI, PON_ALARM_LOKI, PON_ALARM_TIWI, PON_ALARM_TIA
 from voltha.adapters.microsemi_olt.PAS5211_hardware import PON_ALARM_CODE_LOS, PON_ALARM_CODE_LOSI, PON_ALARM_CODE_DOWI, \
     PON_ALARM_CODE_LOFI, PON_ALARM_CODE_RDII, PON_ALARM_CODE_LOAMI, PON_ALARM_CODE_LCDGI, PON_ALARM_CODE_LOAI, \
     PON_ALARM_CODE_SDI, PON_ALARM_CODE_SFI, PON_ALARM_CODE_PEE, PON_ALARM_CODE_DGI, PON_ALARM_CODE_LOKI, \